Skip to content

Commit 19d3386

Browse files
authored
timeout: use nix kill/setpgid to reduce unsafe (#10120)
* refactor(timeout): replace unsafe libc calls with nix crate equivalents Replace unsafe `libc::kill` and `libc::setpgid` calls in the timeout utility with safer nix crate wrappers (`kill`, `getpid`, `setpgid`) to reduce unsafe code usage and improve overall code safety. This maintains functionality while leveraging Rust's type safety for signal and process group operations. * refactor(timeout): reorder imports for consistency Reorder nix imports to alphabetical order in timeout.rs for better code organization and readability. No functional changes.
1 parent 23b11ea commit 19d3386

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/uu/timeout/src/timeout.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ use uucore::{
2727
signals::{signal_by_name_or_value, signal_name_by_value},
2828
};
2929

30+
use nix::sys::signal::{Signal, kill};
31+
use nix::unistd::{Pid, getpid, setpgid};
32+
3033
pub mod options {
3134
pub static FOREGROUND: &str = "foreground";
3235
pub static KILL_AFTER: &str = "kill-after";
@@ -293,8 +296,8 @@ fn preserve_signal_info(signal: libc::c_int) -> libc::c_int {
293296
// The easiest way to preserve the latter seems to be to kill
294297
// ourselves with whatever signal our child exited with, which is
295298
// what the following is intended to accomplish.
296-
unsafe {
297-
libc::kill(libc::getpid(), signal);
299+
if let Ok(sig) = Signal::try_from(signal) {
300+
let _ = kill(getpid(), Some(sig));
298301
}
299302
signal
300303
}
@@ -315,7 +318,7 @@ fn timeout(
315318
verbose: bool,
316319
) -> UResult<()> {
317320
if !foreground {
318-
unsafe { libc::setpgid(0, 0) };
321+
let _ = setpgid(Pid::from_raw(0), Pid::from_raw(0));
319322
}
320323
#[cfg(unix)]
321324
enable_pipe_errors()?;

0 commit comments

Comments
 (0)