From 0305a8324e45b470adae218862bb414cd4d1fa8b Mon Sep 17 00:00:00 2001 From: mattsu Date: Thu, 8 Jan 2026 13:42:57 +0900 Subject: [PATCH 1/2] 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. --- src/uu/timeout/src/timeout.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/uu/timeout/src/timeout.rs b/src/uu/timeout/src/timeout.rs index 3e1a35c45b2..66ac88886fe 100644 --- a/src/uu/timeout/src/timeout.rs +++ b/src/uu/timeout/src/timeout.rs @@ -27,6 +27,9 @@ use uucore::{ signals::{signal_by_name_or_value, signal_name_by_value}, }; +use nix::sys::signal::{kill, Signal}; +use nix::unistd::{getpid, setpgid, Pid}; + pub mod options { pub static FOREGROUND: &str = "foreground"; pub static KILL_AFTER: &str = "kill-after"; @@ -293,8 +296,8 @@ fn preserve_signal_info(signal: libc::c_int) -> libc::c_int { // The easiest way to preserve the latter seems to be to kill // ourselves with whatever signal our child exited with, which is // what the following is intended to accomplish. - unsafe { - libc::kill(libc::getpid(), signal); + if let Ok(sig) = Signal::try_from(signal) { + let _ = kill(getpid(), Some(sig)); } signal } @@ -315,7 +318,7 @@ fn timeout( verbose: bool, ) -> UResult<()> { if !foreground { - unsafe { libc::setpgid(0, 0) }; + let _ = setpgid(Pid::from_raw(0), Pid::from_raw(0)); } #[cfg(unix)] enable_pipe_errors()?; From f4646b0c59e2d1531335a39b9d6d540f8ee46d98 Mon Sep 17 00:00:00 2001 From: mattsu Date: Thu, 8 Jan 2026 14:51:45 +0900 Subject: [PATCH 2/2] refactor(timeout): reorder imports for consistency Reorder nix imports to alphabetical order in timeout.rs for better code organization and readability. No functional changes. --- src/uu/timeout/src/timeout.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/uu/timeout/src/timeout.rs b/src/uu/timeout/src/timeout.rs index 66ac88886fe..de20bec83c3 100644 --- a/src/uu/timeout/src/timeout.rs +++ b/src/uu/timeout/src/timeout.rs @@ -27,8 +27,8 @@ use uucore::{ signals::{signal_by_name_or_value, signal_name_by_value}, }; -use nix::sys::signal::{kill, Signal}; -use nix::unistd::{getpid, setpgid, Pid}; +use nix::sys::signal::{Signal, kill}; +use nix::unistd::{Pid, getpid, setpgid}; pub mod options { pub static FOREGROUND: &str = "foreground";