Skip to content

Commit 8d48ca3

Browse files
committed
Addressing feedback about inlining documentation and use of nix
1 parent 3a6944a commit 8d48ca3

File tree

6 files changed

+31
-41
lines changed

6 files changed

+31
-41
lines changed

Cargo.lock

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/uu/users/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ path = "src/users.rs"
2424

2525
[dependencies]
2626
clap = { workspace = true }
27-
uucore = { workspace = true, features = ["utmpx"] }
27+
uucore = { workspace = true, features = ["utmpx", "process"] }
2828
fluent = { workspace = true }
29-
nix = { workspace = true, features = ["signal"] }
3029

3130
[target.'cfg(target_os = "openbsd")'.dependencies]
3231
utmp-classic = { workspace = true }

src/uu/users/src/users.rs

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,10 @@ use std::path::Path;
1010

1111
use clap::builder::ValueParser;
1212
use clap::{Arg, Command};
13-
#[cfg(not(target_os = "openbsd"))]
14-
use nix::sys::signal::kill;
15-
#[cfg(not(target_os = "openbsd"))]
16-
use nix::unistd::Pid;
1713
use uucore::error::UResult;
1814
use uucore::format_usage;
15+
#[cfg(not(target_os = "openbsd"))]
16+
use uucore::process::pid_is_alive;
1917
use uucore::translate;
2018

2119
#[cfg(target_os = "openbsd")]
@@ -37,20 +35,6 @@ fn get_long_usage() -> String {
3735
translate!("users-long-usage", "default_path" => default_path)
3836
}
3937

40-
#[inline]
41-
#[cfg(not(target_os = "openbsd"))]
42-
fn pid_is_alive(pid: i32) -> bool {
43-
if pid <= 0 {
44-
return true;
45-
}
46-
47-
match kill(Pid::from_raw(pid), None) {
48-
Ok(()) => true,
49-
Err(nix::errno::Errno::ESRCH) => false,
50-
Err(_) => true,
51-
}
52-
}
53-
5438
#[uucore::main]
5539
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
5640
let matches = uucore::clap_localization::handle_clap_result(uu_app(), args)?;

src/uu/who/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@ path = "src/who.rs"
2525

2626
[dependencies]
2727
clap = { workspace = true }
28-
uucore = { workspace = true, features = ["utmpx"] }
28+
uucore = { workspace = true, features = ["utmpx", "process"] }
2929
fluent = { workspace = true }
30-
nix = { workspace = true, features = ["signal"] }
3130

3231
[[bin]]
3332
name = "who"

src/uu/who/src/platform/unix.rs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@ use crate::uu_app;
1111
use uucore::display::Quotable;
1212
use uucore::error::{FromIo, UResult};
1313
use uucore::libc::{S_IWGRP, STDIN_FILENO, ttyname};
14+
#[cfg(not(target_os = "openbsd"))]
15+
use uucore::process::pid_is_alive;
1416
use uucore::translate;
1517

16-
use nix::sys::signal::kill;
17-
use nix::unistd::Pid;
18-
1918
use uucore::utmpx::{self, UtmpxRecord, time};
2019

2120
use std::borrow::Cow;
@@ -180,20 +179,6 @@ fn time_string(ut: &UtmpxRecord) -> String {
180179
ut.login_time().format(&time_format).unwrap()
181180
}
182181

183-
#[inline]
184-
#[cfg(not(target_os = "openbsd"))]
185-
fn pid_is_alive(pid: i32) -> bool {
186-
if pid <= 0 {
187-
return true;
188-
}
189-
190-
match kill(Pid::from_raw(pid), None) {
191-
Ok(()) => true,
192-
Err(nix::errno::Errno::ESRCH) => false,
193-
Err(_) => true,
194-
}
195-
}
196-
197182
#[inline]
198183
fn current_tty() -> String {
199184
unsafe {

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,31 @@ pub fn getpid() -> pid_t {
5151
unsafe { libc::getpid() }
5252
}
5353

54+
/// Check if a process with the given PID is alive.
55+
///
56+
/// Uses `kill(pid, 0)` which sends signal 0 (null signal) to check process existence
57+
/// without actually sending a signal. This is a standard POSIX technique for checking
58+
/// if a process exists.
59+
///
60+
/// Returns `true` if:
61+
/// - The process exists (kill returns 0)
62+
/// - We lack permission to signal the process (errno != ESRCH)
63+
/// This means the process exists but we can't signal it
64+
///
65+
/// Returns `false` only if:
66+
/// - errno is ESRCH (No such process), confirming the process doesn't exist
67+
///
68+
/// PIDs <= 0 are considered alive for compatibility with utmp records that may
69+
/// contain special or invalid PID values.
70+
#[cfg(not(target_os = "openbsd"))]
71+
pub fn pid_is_alive(pid: i32) -> bool {
72+
if pid <= 0 {
73+
return true;
74+
}
75+
76+
unsafe { libc::kill(pid, 0) == 0 || *libc::__errno_location() != libc::ESRCH }
77+
}
78+
5479
/// `getsid()` returns the session ID of the process with process ID pid.
5580
///
5681
/// If pid is 0, getsid() returns the session ID of the calling process.

0 commit comments

Comments
 (0)