File tree Expand file tree Collapse file tree 6 files changed +31
-41
lines changed Expand file tree Collapse file tree 6 files changed +31
-41
lines changed Original file line number Diff line number Diff line change @@ -24,9 +24,8 @@ path = "src/users.rs"
2424
2525[dependencies ]
2626clap = { workspace = true }
27- uucore = { workspace = true , features = [" utmpx" ] }
27+ uucore = { workspace = true , features = [" utmpx" , " process " ] }
2828fluent = { workspace = true }
29- nix = { workspace = true , features = [" signal" ] }
3029
3130[target .'cfg(target_os = "openbsd")' .dependencies ]
3231utmp-classic = { workspace = true }
Original file line number Diff line number Diff line change @@ -10,12 +10,10 @@ use std::path::Path;
1010
1111use clap:: builder:: ValueParser ;
1212use 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 ;
1713use uucore:: error:: UResult ;
1814use uucore:: format_usage;
15+ #[ cfg( not( target_os = "openbsd" ) ) ]
16+ use uucore:: process:: pid_is_alive;
1917use 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]
5539pub fn uumain ( args : impl uucore:: Args ) -> UResult < ( ) > {
5640 let matches = uucore:: clap_localization:: handle_clap_result ( uu_app ( ) , args) ?;
Original file line number Diff line number Diff line change @@ -25,9 +25,8 @@ path = "src/who.rs"
2525
2626[dependencies ]
2727clap = { workspace = true }
28- uucore = { workspace = true , features = [" utmpx" ] }
28+ uucore = { workspace = true , features = [" utmpx" , " process " ] }
2929fluent = { workspace = true }
30- nix = { workspace = true , features = [" signal" ] }
3130
3231[[bin ]]
3332name = " who"
Original file line number Diff line number Diff line change @@ -11,11 +11,10 @@ use crate::uu_app;
1111use uucore:: display:: Quotable ;
1212use uucore:: error:: { FromIo , UResult } ;
1313use uucore:: libc:: { S_IWGRP , STDIN_FILENO , ttyname} ;
14+ #[ cfg( not( target_os = "openbsd" ) ) ]
15+ use uucore:: process:: pid_is_alive;
1416use uucore:: translate;
1517
16- use nix:: sys:: signal:: kill;
17- use nix:: unistd:: Pid ;
18-
1918use uucore:: utmpx:: { self , UtmpxRecord , time} ;
2019
2120use 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]
198183fn current_tty ( ) -> String {
199184 unsafe {
Original file line number Diff line number Diff 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.
You can’t perform that action at this time.
0 commit comments