diff --git a/src/desktop.rs b/src/desktop.rs index a4aaf5c0..9ac4a3cc 100644 --- a/src/desktop.rs +++ b/src/desktop.rs @@ -335,7 +335,7 @@ where // https://systemd.io/DESKTOP_ENVIRONMENTS // // Similar to what Gnome sets, for now. - if let Ok(Some(pid)) = tokio::task::spawn_blocking(|| crate::process::spawn(cmd)).await { + if let Some(pid) = crate::process::spawn(cmd).await { if let Ok(session) = zbus::Connection::session().await { if let Ok(systemd_manager) = SystemdMangerProxy::new(&session).await { let _ = systemd_manager diff --git a/src/process.rs b/src/process.rs index a179af78..155cb1d1 100644 --- a/src/process.rs +++ b/src/process.rs @@ -1,9 +1,10 @@ use std::fs::File; use std::io; use std::process::{exit, Command, Stdio}; +use tokio::io::AsyncReadExt; /// Performs a double fork with setsid to spawn and detach a command. -pub fn spawn(mut command: Command) -> Option { +pub async fn spawn(mut command: Command) -> Option { command .stdin(Stdio::null()) .stdout(Stdio::null()) @@ -15,16 +16,11 @@ pub fn spawn(mut command: Command) -> Option { match unsafe { libc::fork() } { // Parent process - child @ 1.. => { - let child = rustix::process::Pid::from_raw(child).unwrap(); - let _res = rustix::process::waitpid(Some(child), rustix::process::WaitOptions::empty()); + 1.. => { + drop(write); // Read PID from pipe - let mut bytes = [0; 4]; - if rustix::io::read(read, &mut bytes) == Ok(4) { - Some(u32::from_ne_bytes(bytes)) - } else { - None - } + let mut read = tokio::net::unix::pipe::Receiver::from_owned_fd(read).unwrap(); + read.read_u32().await.ok() } // Child process @@ -32,7 +28,7 @@ pub fn spawn(mut command: Command) -> Option { let _res = rustix::process::setsid(); if let Ok(child) = command.spawn() { // Write PID to pipe - let _ = rustix::io::write(write, &child.id().to_ne_bytes()); + let _ = rustix::io::write(write, &child.id().to_be_bytes()); } exit(0)