Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion poc/reverse-attach-linux/src/jobs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,17 @@ fn finish(g: &mut Inner, status: Option<ExitStatus>, timed_out: bool, late: Opti
/// The job's shell (our child `pid`) has exited. It is not reaped (`WNOWAIT`),
/// so its pid stays reserved.
fn shell_exited(pid: u32) -> bool {
// A pid that names no process has no child to wait for: same answer as ECHILD below.
let Some(id) = crate::proc::checked_pid(pid) else {
return true;
};
// siginfo_t is 128 bytes on Linux; `si_signo` (first int) is set only when
// a child was found, so a zeroed buffer distinguishes WNOHANG's "not yet".
let mut info = [0i32; 32];
let r = unsafe {
waitid(
P_PID,
pid,
id as u32,
info.as_mut_ptr().cast(),
WEXITED | WNOHANG | WNOWAIT,
)
Expand Down
9 changes: 9 additions & 0 deletions poc/reverse-attach-linux/src/proc_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,15 @@ fn a_pid_that_does_not_exist_signals_nothing_and_does_not_panic() {
assert!(!kill_pid_checked(top + 1, &|_| true));
}

#[test]
fn checked_pid_keeps_valid_pids_and_rejects_the_rest() {
assert_eq!(checked_pid(1), Some(1));
assert_eq!(checked_pid(2_147_483_647), Some(i32::MAX));
assert_eq!(checked_pid(0), None);
assert_eq!(checked_pid(2_147_483_648), None);
assert_eq!(checked_pid(u32::MAX), None);
}

/// Pids above `i32::MAX` (2^31, and `u32::MAX`, which `as i32` makes `-1`) name no process. The
/// call must answer "no" without reaching rustix with a negative id, so debug and release agree.
#[cfg(target_os = "linux")]
Expand Down
Loading