Just a quick overview, I'm trying to create a simple frontend for runit's sv , and I don't know how to approach the part of executing commands/actions with elevated privileges.
I initially made a simple wrapper over pkexec:
/// Simple std::Command wrapper over `pkexec` pub fn with_sudo<I, S>(program: impl AsRef<OsStr>, args: I) where I: IntoIterator<Item = S>, S: AsRef<OsStr>, { let output = Command::new("pkexec") .arg(program.as_ref()) .args(args) .output(); match output { Ok(output) => { println!("{}", String::from_utf8_lossy(&output.stdout)); println!("{}", String::from_utf8_lossy(&output.stderr)); } Err(err) => { println!("{err}"); } } } At first I thought this would be a good first one, but I tried using it for something like:
for _i in 0..=5 { cmd::with_sudo("sv", args.clone()); std::thread::sleep(std::time::Duration::from_secs(2)); } which is a close equivalent to sudo sv status /var/service/*. The for loop was there as a quick test to the polling of services' statuses. Well of course it worked, but it asks for auth every time the loop cycles.
I also tried
fn is_root() -> bool { unsafe {libc::getuid() == 0} } pub fn main() -> result::Result<()> { if is_root() { iced::run(App::update, App::view)?; } else { eprintln!("{}", result::AppError::SUDO); return Err(result::AppError::SUDO) } Ok(()) } and then had the build binary ran with sudo, even that gave very weird results, and if I searched it right, that bit has something to do with wayland or something.
I honestly need help on how do I approach this part. I have practically zero knowledge with doing elevated-privilege actions in code.
Just to add on what I would try to achieve (if possible) within the program
- Auth once (till end of session) at the start of application and have the loop that pings
sv statusrun without asking for password every time. - Pool actions like
sv once|up|down|restart|once|hup|term|kill, and enabling service (via symlinks) and then auth once to execute them all.
3 posts - 3 participants
