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:
parent
621de17cad
commit
82fb781746
2 changed files with 8 additions and 12 deletions
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue