Use tokio to asynchronously read from pipe

Avoids either `spawn_blocking`, or potentially blocking call in an async
function (though it shouldn't block for long).
This commit is contained in:
Ian Douglas Scott 2024-07-25 09:26:54 -07:00 committed by Ian Douglas Scott
parent 621de17cad
commit 82fb781746
2 changed files with 8 additions and 12 deletions

View file

@ -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

View file

@ -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<u32> {
pub async fn spawn(mut command: Command) -> Option<u32> {
command
.stdin(Stdio::null())
.stdout(Stdio::null())
@ -15,16 +16,11 @@ pub fn spawn(mut command: Command) -> Option<u32> {
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<u32> {
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)