Skip to content

Commit

Permalink
feat(siginfo)!: extract si_status from siginfo
Browse files Browse the repository at this point in the history
Problem: there is no way to get the exit status or signal of a child
process when handling SIGCHLD

Solution: expose `siginfo_t`'s `si_status` field in `Process` when
extracting info for SIGCHLD

closes vorner#165
  • Loading branch information
LunarLambda committed Nov 13, 2024
1 parent 61e2cf6 commit 6dcb595
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/low_level/extract.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,8 @@ pid_t sighook_signal_pid(const siginfo_t *info) {
uid_t sighook_signal_uid(const siginfo_t *info) {
return info->si_uid;
}

c_int sighook_signal_status(const siginfo_t *info, bool *has_status) {
*has_status = info->si_signo == SIGCHLD;
return info->si_status;
}
7 changes: 7 additions & 0 deletions src/low_level/siginfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ extern "C" {
fn sighook_signal_cause(info: &siginfo_t) -> ICause;
fn sighook_signal_pid(info: &siginfo_t) -> pid_t;
fn sighook_signal_uid(info: &siginfo_t) -> uid_t;
fn sighook_signal_status(info: &siginfo_t, has_status: &mut bool) -> c_int;
}

// Warning: must be in sync with the C code
Expand Down Expand Up @@ -66,6 +67,9 @@ pub struct Process {

/// The user owning the process.
pub uid: uid_t,

/// The exit status of the process, or the signal number that caused it to change state.
pub status: Option<c_int>,
}

impl Process {
Expand All @@ -78,9 +82,12 @@ impl Process {
* and `si_uid` filled in.
*/
unsafe fn extract(info: &siginfo_t) -> Self {
let mut has_status = false;
let status = sighook_signal_status(info, &mut has_status);
Self {
pid: sighook_signal_pid(info),
uid: sighook_signal_uid(info),
status: has_status.then_some(status),
}
}
}
Expand Down

0 comments on commit 6dcb595

Please sign in to comment.