1
0
Fork 0
forked from wry/wry

Merge pull request #445 from mahkoh/jorth/reexec-double-fork

reexec: double-fork to prevent the creation of zombies
This commit is contained in:
mahkoh 2025-04-25 20:10:28 +02:00 committed by GitHub
commit 7eb47a42c2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

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