Skip to content

Commit ee35925

Browse files
committed
uucore: unblock realtime signals in unblock_signal
nix's Signal has no realtime signals, so converting SIGRTMIN+n to it failed and unwrap_unchecked() hit undefined behaviour when timeout re-raised a realtime signal from its child. Build the set from the raw signal number with libc, as env already does. Fixes #16407
1 parent d8966c1 commit ee35925

2 files changed

Lines changed: 28 additions & 3 deletions

File tree

‎src/uucore/src/lib/features/process/unix.rs‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,18 @@ pub fn timeout_signal_set() -> SigSet {
112112

113113
/// Unblocks a signal from the current thread.
114114
pub fn unblock_signal(signal: RixSignal) -> io::Result<()> {
115-
let mut set = SigSet::empty();
116-
// SAFETY: rustix's Signal is also a valid nix Signal.
117-
set.add(unsafe { Signal::try_from(signal.as_raw()).unwrap_unchecked() });
115+
// nix's Signal has no realtime signals, so build the set from the raw
116+
// signal number.
117+
let mut set = std::mem::MaybeUninit::<libc::sigset_t>::uninit();
118+
// SAFETY: sigemptyset initializes the set before sigaddset and
119+
// assume_init use it.
120+
let set = unsafe {
121+
libc::sigemptyset(set.as_mut_ptr());
122+
if libc::sigaddset(set.as_mut_ptr(), signal.as_raw()) != 0 {
123+
return Err(io::Error::last_os_error());
124+
}
125+
SigSet::from_sigset_t_unchecked(set.assume_init())
126+
};
118127

119128
set.thread_unblock().map_err(Into::into)
120129
}

‎tests/by-util/test_timeout.rs‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,22 @@ fn test_child_core_dump_not_repeated() {
188188
assert!(!status.core_dumped());
189189
}
190190

191+
/// A child killed by a realtime signal is reported like one killed by
192+
/// SIGTERM: timeout re-raises the signal.
193+
#[test]
194+
#[cfg(target_os = "linux")]
195+
fn test_child_killed_by_realtime_signal() {
196+
use std::os::unix::process::ExitStatusExt;
197+
let ts = TestScenario::new(util_name!());
198+
let sig = libc::SIGRTMIN() + 3;
199+
let status = std::process::Command::new(&ts.bin_path)
200+
.args(["timeout", "5", "sh", "-c", &format!("kill -{sig} $$")])
201+
.current_dir(ts.fixtures.as_string())
202+
.status()
203+
.unwrap();
204+
assert_eq!(status.signal(), Some(sig));
205+
}
206+
191207
#[test]
192208
fn test_preserve_status_even_when_send_signal() {
193209
let (ts, bin) = scenario_with_bin();

0 commit comments

Comments
 (0)