Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wait for all processes in the process group #7

Merged
merged 2 commits into from Dec 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 43 additions & 9 deletions src/stdlib/child/unix.rs
Expand Up @@ -67,15 +67,49 @@ impl ChildImp {
fn wait_imp(&mut self, flag: WaitPidFlag) -> Result<Option<ExitStatus>> {
let negpid = Pid::from_raw(-self.pgid.as_raw());

// we can't use the safe wrapper directly because it doesn't return the raw status, and we
// need it to convert to the std's ExitStatus.
let mut status: i32 = 0;
match unsafe { libc::waitpid(negpid.into(), &mut status as *mut libc::c_int, flag.bits()) }
{
0 => Ok(None),
res => Errno::result(res)
.map_err(Error::from)
.map(|_| Some(ExitStatus::from_raw(status))),
// Wait for processes in a loop until every process in this
// process group has exited (this ensures that we reap any
// zombies that may have been created if the parent exited after
// spawning children, but didn't wait for those children to
// exit).
let mut parent_exit_status: Option<ExitStatus> = None;
loop {
// we can't use the safe wrapper directly because it doesn't
// return the raw status, and we need it to convert to the
// std's ExitStatus.
let mut status: i32 = 0;
match unsafe {
libc::waitpid(negpid.into(), &mut status as *mut libc::c_int, flag.bits())
} {
0 => {
// Zero should only happen if WNOHANG was passed in,
// and means that no processes have yet to exit.
return Ok(None);
}
-1 => {
match Errno::last() {
Errno::ECHILD => {
// No more children to reap; this is a
// graceful exit.
return Ok(parent_exit_status);
}
errno => {
return Err(Error::from(errno));
}
}
}
pid => {
// *A* process exited. Was it the parent process
// that we started? If so, collect the exit signal,
// otherwise we reaped a zombie process and should
// continue in the loop.
if self.pgid.as_raw() == pid {
parent_exit_status = Some(ExitStatus::from_raw(status));
} else {
// Reaped a zombie child; keep looping.
}
}
};
}
}

Expand Down
49 changes: 41 additions & 8 deletions src/tokio/child/unix.rs
Expand Up @@ -70,14 +70,47 @@ impl ChildImp {
}

fn wait_imp(pgid: i32, flag: WaitPidFlag) -> Result<Option<ExitStatus>> {
// we can't use the safe wrapper directly because it doesn't return the raw status, and we
// need it to convert to the std's ExitStatus.
let mut status: i32 = 0;
match unsafe { libc::waitpid(-pgid, &mut status as *mut libc::c_int, flag.bits()) } {
0 => Ok(None),
res => Errno::result(res)
.map_err(Error::from)
.map(|_| Some(ExitStatus::from_raw(status))),
// Wait for processes in a loop until every process in this
// process group has exited (this ensures that we reap any
// zombies that may have been created if the parent exited after
// spawning children, but didn't wait for those children to
// exit).
let mut parent_exit_status: Option<ExitStatus> = None;
loop {
// we can't use the safe wrapper directly because it doesn't
// return the raw status, and we need it to convert to the
// std's ExitStatus.
let mut status: i32 = 0;
match unsafe { libc::waitpid(-pgid, &mut status as *mut libc::c_int, flag.bits()) } {
0 => {
// Zero should only happen if WNOHANG was passed in,
// and means that no processes have yet to exit.
return Ok(None);
}
-1 => {
match Errno::last() {
Errno::ECHILD => {
// No more children to reap; this is a
// graceful exit.
return Ok(parent_exit_status);
}
errno => {
return Err(Error::from(errno));
}
}
}
pid => {
// *A* process exited. Was it the parent process
// that we started? If so, collect the exit signal,
// otherwise we reaped a zombie process and should
// continue in the loop.
if pgid == pid {
parent_exit_status = Some(ExitStatus::from_raw(status));
} else {
// Reaped a zombie child; keep looping.
}
}
};
}
}

Expand Down