diff --git a/src/uucore/src/lib/features/process/unix.rs b/src/uucore/src/lib/features/process/unix.rs index 0f90d0a0e30..4ae4baede6c 100644 --- a/src/uucore/src/lib/features/process/unix.rs +++ b/src/uucore/src/lib/features/process/unix.rs @@ -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; @@ -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::::uninit(); + // 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) } diff --git a/tests/by-util/test_timeout.rs b/tests/by-util/test_timeout.rs index 038cc586573..c38fc81b0ab 100644 --- a/tests/by-util/test_timeout.rs +++ b/tests/by-util/test_timeout.rs @@ -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();