Skip to content

Commit 27a4626

Browse files
aozmekiksylvestre
authored andcommitted
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 3e2c890 commit 27a4626

2 files changed

Lines changed: 29 additions & 3 deletions

File tree

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

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

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

113114
/// Unblocks a signal from the current thread.
114115
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() });
116+
// nix's Signal has no realtime signals, so build the set from the raw
117+
// signal number.
118+
let mut set = std::mem::MaybeUninit::<libc::sigset_t>::uninit();
119+
// SAFETY: sigemptyset initializes the set before sigaddset and
120+
// assume_init use it.
121+
let set = unsafe {
122+
libc::sigemptyset(set.as_mut_ptr());
123+
if libc::sigaddset(set.as_mut_ptr(), signal.as_raw()) != 0 {
124+
return Err(io::Error::last_os_error());
125+
}
126+
SigSet::from_sigset_t_unchecked(set.assume_init())
127+
};
118128

119129
set.thread_unblock().map_err(Into::into)
120130
}

‎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)