1
0
Fork 0
forked from wry/wry

reexec: double-fork to prevent the creation of zombies

This commit is contained in:
Julian Orth 2025-04-25 20:08:02 +02:00
parent 8552c5f1eb
commit fcf4cef6de

View file

@ -12,7 +12,7 @@ use {
}, },
std::{array::from_mut, cell::RefCell, rc::Rc}, std::{array::from_mut, cell::RefCell, rc::Rc},
thiserror::Error, thiserror::Error,
uapi::{OwnedFd, UstrPtr, c, close_range, dup2, pipe2}, uapi::{OwnedFd, UstrPtr, c, close_range, dup2, pipe2, waitpid},
}; };
pub struct JayReexec { pub struct JayReexec {
@ -50,6 +50,12 @@ impl JayReexec {
} }
let (p1, c1) = pipe!(); let (p1, c1) = pipe!();
let (c2, p2) = pipe!(); let (c2, p2) = pipe!();
if let Ok(f) = fork_with_pidfd(false) {
match f {
Forked::Parent { pid, .. } => {
let _ = waitpid(pid, 0);
}
Forked::Child { .. } => {
if let Ok(f) = fork_with_pidfd(false) { if let Ok(f) = fork_with_pidfd(false) {
if let Forked::Child { .. } = f { if let Forked::Child { .. } = f {
drop(p2); drop(p2);
@ -70,11 +76,14 @@ impl JayReexec {
revents: 0, revents: 0,
}; };
let _ = uapi::poll(from_mut(&mut pollfd), -1); let _ = uapi::poll(from_mut(&mut pollfd), -1);
}
}
unsafe { unsafe {
c::_exit(0); c::_exit(0);
} }
} }
} }
}
drop(c1); drop(c1);
let mut pollfd = c::pollfd { let mut pollfd = c::pollfd {
fd: p1.raw(), fd: p1.raw(),