@@ -7,7 +7,7 @@ use std::path::PathBuf;
77
88use clap:: { crate_version, Arg , ArgAction , ArgMatches , Command } ;
99use uu_pgrep:: process:: { walk_process, ProcessInformation } ;
10- use uucore:: { error:: UResult , format_usage, help_about, help_usage} ;
10+ use uucore:: { error:: UResult , format_usage, help_about, help_usage, process :: geteuid } ;
1111
1212const ABOUT : & str = help_about ! ( "pidof.md" ) ;
1313const USAGE : & str = help_usage ! ( "pidof.md" ) ;
@@ -94,21 +94,32 @@ fn collect_matched_pids(matches: &ArgMatches) -> Vec<usize> {
9494 . copied ( )
9595 . collect :: < Vec < _ > > ( ) ;
9696
97+ // Original pidof silently ignores the check-root option if the user is not root.
98+ let check_root = matches. get_flag ( "check-root" ) && geteuid ( ) == 0 ;
99+ let our_root = ProcessInformation :: current_process_info ( )
100+ . unwrap ( )
101+ . root ( )
102+ . unwrap ( ) ;
103+
97104 program_names
98105 . into_iter ( )
99106 . flat_map ( |program| {
100107 let mut processed = Vec :: new ( ) ;
101108 for mut process in collected. clone ( ) {
102- let contains =
103- match_process_name ( & mut process, & program, with_workers, match_scripts) ;
104- let should_omit = arg_omit_pid. contains ( & process. pid ) ;
105-
106- if contains && !should_omit {
107- if matches. get_flag ( "t" ) {
108- processed. extend_from_slice ( & process. thread_ids ( ) ) ;
109- } else {
110- processed. push ( process. pid ) ;
111- }
109+ if !match_process_name ( & mut process, & program, with_workers, match_scripts) {
110+ continue ;
111+ }
112+ if arg_omit_pid. contains ( & process. pid ) {
113+ continue ;
114+ }
115+ if check_root && process. root ( ) . unwrap ( ) != our_root {
116+ continue ;
117+ }
118+
119+ if matches. get_flag ( "t" ) {
120+ processed. extend_from_slice ( & process. thread_ids ( ) ) ;
121+ } else {
122+ processed. push ( process. pid ) ;
112123 }
113124 }
114125
@@ -140,12 +151,13 @@ pub fn uu_app() -> Command {
140151 . index ( 1 )
141152 . action ( ArgAction :: Append ) ,
142153 )
143- // .arg(
144- // Arg::new("c")
145- // .short('c')
146- // .help("Return PIDs with the same root directory")
147- // .action(ArgAction::SetTrue),
148- // )
154+ . arg (
155+ Arg :: new ( "check-root" )
156+ . short ( 'c' )
157+ . long ( "check-root" )
158+ . help ( "Only return PIDs with the same root directory" )
159+ . action ( ArgAction :: SetTrue ) ,
160+ )
149161 . arg (
150162 Arg :: new ( "S" )
151163 . short ( 'S' )
0 commit comments