Don't double fork command when running as systemd service since it's not necessary

This commit is contained in:
cyber-sushi 2024-05-23 05:19:21 +02:00
parent 69a2b9be16
commit c79d031fe7

View file

@ -477,36 +477,39 @@ impl EventReader {
}; };
if let Some(user) = user { if let Some(user) = user {
for command in command_list { for command in command_list {
let cmd = if running_as_root { if running_as_root {
let cmd = format!("runuser {} -c '{}'", user, command); match fork() {
cmd Ok(Fork::Child) => {
} match fork() {
else { Ok(Fork::Child) => {
let cmd = format!("systemd-run --user -M {}@ {}", user, command); setsid().unwrap();
cmd Command::new("sh")
}; .arg("-c")
match fork() { .arg(format!("runuser {} -c '{}'", user, command))
Ok(Fork::Child) => { .stdin(Stdio::null())
match fork() { .stdout(Stdio::null())
Ok(Fork::Child) => { .stderr(Stdio::null())
setsid().unwrap(); .spawn()
Command::new("sh") .unwrap();
.arg("-c") std::process::exit(0);
.arg(cmd) }
.stdin(Stdio::null()) Ok(Fork::Parent(_)) => std::process::exit(0),
.stdout(Stdio::null()) Err(_) => std::process::exit(1),
.stderr(Stdio::null()) }
.spawn() }
.unwrap(); Ok(Fork::Parent(_)) => (),
std::process::exit(0); Err(_) => std::process::exit(1),
} }
Ok(Fork::Parent(_)) => std::process::exit(0), } else {
Err(_) => std::process::exit(1), Command::new("sh")
} .arg("-c")
} .arg(format!("systemd-run --user -M {}@ {}", user, command))
Ok(Fork::Parent(_)) => (), .stdin(Stdio::null())
Err(_) => std::process::exit(1), .stdout(Stdio::null())
} .stderr(Stdio::null())
.spawn()
.unwrap();
}
} }
} }
} }