Skip to content

Commit d6cadd4

Browse files
mattsu2020cakebaker
authored andcommitted
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.
1 parent 452cd1e commit d6cadd4

2 files changed

Lines changed: 20 additions & 11 deletions

File tree

src/uu/tail/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ libc = { workspace = true }
3131
notify = { workspace = true }
3232

3333
[target.'cfg(unix)'.dependencies]
34-
rustix = { workspace = true, features = ["fs"] }
34+
rustix = { workspace = true, features = ["fs", "process"] }
3535

3636
[target.'cfg(windows)'.dependencies]
3737
windows-sys = { workspace = true, features = [

src/uu/tail/src/platform/unix.rs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
// file that was distributed with this source code.
55

66
// spell-checker:ignore (ToDO) stdlib, ISCHR, GETFD
7-
// spell-checker:ignore (options) EPERM, ENOSYS
7+
// spell-checker:ignore (options) EPERM, ENOSYS, NOSYS
88

9-
use std::io::Error;
9+
use rustix::process::{Pid as RustixPid, test_kill_process};
1010

11-
pub type Pid = libc::pid_t;
11+
pub type Pid = i32;
1212

1313
pub struct ProcessChecker {
1414
pid: Pid,
@@ -20,7 +20,14 @@ impl ProcessChecker {
2020
}
2121

2222
pub fn is_dead(&self) -> bool {
23-
unsafe { libc::kill(self.pid, 0) != 0 && get_errno() != libc::EPERM }
23+
let Some(pid) = RustixPid::from_raw(self.pid) else {
24+
return true;
25+
};
26+
match test_kill_process(pid) {
27+
Ok(()) => false,
28+
Err(rustix::io::Errno::PERM) => false,
29+
Err(_) => true,
30+
}
2431
}
2532
}
2633

@@ -29,12 +36,14 @@ impl Drop for ProcessChecker {
2936
}
3037

3138
pub fn supports_pid_checks(pid: Pid) -> bool {
32-
unsafe { !(libc::kill(pid, 0) != 0 && get_errno() == libc::ENOSYS) }
33-
}
34-
35-
#[inline]
36-
fn get_errno() -> i32 {
37-
Error::last_os_error().raw_os_error().unwrap()
39+
let Some(pid) = RustixPid::from_raw(pid) else {
40+
return false;
41+
};
42+
match test_kill_process(pid) {
43+
Ok(()) => true,
44+
Err(rustix::io::Errno::NOSYS) => false,
45+
Err(_) => true,
46+
}
3847
}
3948

4049
//pub fn stdin_is_bad_fd() -> bool {

0 commit comments

Comments
 (0)