Skip to content
Open
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
16 changes: 13 additions & 3 deletions src/uucore/src/lib/features/process/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

// spell-checker:ignore sigwait KTIME timeval itimerval setitimer itimer timerid
// spell-checker:ignore sigevent sigev sigval itimerspec signo clockid sevp
// spell-checker:ignore sigset sigemptyset sigaddset

#[cfg(not(any(target_os = "fuchsia", target_os = "haiku")))]
use libc::pid_t;
Expand Down Expand Up @@ -112,9 +113,18 @@ pub fn timeout_signal_set() -> SigSet {

/// Unblocks a signal from the current thread.
pub fn unblock_signal(signal: RixSignal) -> io::Result<()> {
let mut set = SigSet::empty();
// SAFETY: rustix's Signal is also a valid nix Signal.
set.add(unsafe { Signal::try_from(signal.as_raw()).unwrap_unchecked() });
// nix's Signal has no realtime signals, so build the set from the raw
// signal number.
let mut set = std::mem::MaybeUninit::<libc::sigset_t>::uninit();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sigset_from_signal_value in env.rs does almost the same, could env use a shared uucore helper? fine as a follow-up :)

// SAFETY: sigemptyset initializes the set before sigaddset and
// assume_init use it.
let set = unsafe {
libc::sigemptyset(set.as_mut_ptr());
if libc::sigaddset(set.as_mut_ptr(), signal.as_raw()) != 0 {
return Err(io::Error::last_os_error());
}
SigSet::from_sigset_t_unchecked(set.assume_init())
};

set.thread_unblock().map_err(Into::into)
}
Expand Down
16 changes: 16 additions & 0 deletions tests/by-util/test_timeout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,22 @@ fn test_child_core_dump_not_repeated() {
assert!(!status.core_dumped());
}

/// A child killed by a realtime signal is reported like one killed by
/// SIGTERM: timeout re-raises the signal.
#[test]
#[cfg(target_os = "linux")]
fn test_child_killed_by_realtime_signal() {
use std::os::unix::process::ExitStatusExt;
let ts = TestScenario::new(util_name!());
let sig = libc::SIGRTMIN() + 3;
let status = std::process::Command::new(&ts.bin_path)
.args(["timeout", "5", "sh", "-c", &format!("kill -{sig} $$")])
.current_dir(ts.fixtures.as_string())
.status()
.unwrap();
assert_eq!(status.signal(), Some(sig));
}

#[test]
fn test_preserve_status_even_when_send_signal() {
let (ts, bin) = scenario_with_bin();
Expand Down
Loading