From 956a0dcbdbb44141ebaeb4668a3b6e3c094947ff Mon Sep 17 00:00:00 2001 From: mattsu Date: Thu, 9 Apr 2026 22:04:37 +0900 Subject: [PATCH] tail: replace unsafe libc::kill by rustix Use `rustix::process::test_kill_process` (a safe wrapper around `kill(pid, 0)`) to eliminate the remaining `unsafe` blocks in src/uu/tail/src/platform/unix.rs. This removes the direct `libc::kill` calls and the `get_errno` helper, replacing them with idiomatic `match` on `rustix::io::Errno` variants. --- src/uu/tail/Cargo.toml | 2 +- src/uu/tail/src/platform/unix.rs | 29 +++++++++++++++++++---------- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/uu/tail/Cargo.toml b/src/uu/tail/Cargo.toml index eaf269f1097..5dd0e918c24 100644 --- a/src/uu/tail/Cargo.toml +++ b/src/uu/tail/Cargo.toml @@ -31,7 +31,7 @@ libc = { workspace = true } notify = { workspace = true } [target.'cfg(unix)'.dependencies] -rustix = { workspace = true, features = ["fs"] } +rustix = { workspace = true, features = ["fs", "process"] } [target.'cfg(windows)'.dependencies] windows-sys = { workspace = true, features = [ diff --git a/src/uu/tail/src/platform/unix.rs b/src/uu/tail/src/platform/unix.rs index 3fdbfc241b7..9f3b96d5101 100644 --- a/src/uu/tail/src/platform/unix.rs +++ b/src/uu/tail/src/platform/unix.rs @@ -4,11 +4,11 @@ // file that was distributed with this source code. // spell-checker:ignore (ToDO) stdlib, ISCHR, GETFD -// spell-checker:ignore (options) EPERM, ENOSYS +// spell-checker:ignore (options) EPERM, ENOSYS, NOSYS -use std::io::Error; +use rustix::process::{Pid as RustixPid, test_kill_process}; -pub type Pid = libc::pid_t; +pub type Pid = i32; pub struct ProcessChecker { pid: Pid, @@ -20,7 +20,14 @@ impl ProcessChecker { } pub fn is_dead(&self) -> bool { - unsafe { libc::kill(self.pid, 0) != 0 && get_errno() != libc::EPERM } + let Some(pid) = RustixPid::from_raw(self.pid) else { + return true; + }; + match test_kill_process(pid) { + Ok(()) => false, + Err(rustix::io::Errno::PERM) => false, + Err(_) => true, + } } } @@ -29,12 +36,14 @@ impl Drop for ProcessChecker { } pub fn supports_pid_checks(pid: Pid) -> bool { - unsafe { !(libc::kill(pid, 0) != 0 && get_errno() == libc::ENOSYS) } -} - -#[inline] -fn get_errno() -> i32 { - Error::last_os_error().raw_os_error().unwrap() + let Some(pid) = RustixPid::from_raw(pid) else { + return false; + }; + match test_kill_process(pid) { + Ok(()) => true, + Err(rustix::io::Errno::NOSYS) => false, + Err(_) => true, + } } //pub fn stdin_is_bad_fd() -> bool {