File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -16,7 +16,9 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
1616 let sys = System :: new_all ( ) ;
1717
1818 for pid_str in pids {
19- let pid = match pid_str. parse :: < usize > ( ) {
19+ // A pid can also be given as /proc/PID, like in procps-ng.
20+ let number = pid_str. strip_prefix ( "/proc/" ) . unwrap_or ( pid_str) ;
21+ let pid = match number. parse :: < usize > ( ) {
2022 // PIDs start at 1, hence 0 is invalid
2123 Ok ( 0 ) | Err ( _) => {
2224 return Err ( USimpleError :: new (
@@ -29,15 +31,15 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
2931
3032 match sys. process ( Pid :: from ( pid) ) {
3133 Some ( process) => match process. cwd ( ) {
32- Some ( cwd) => println ! ( "{pid }: {}" , cwd. display( ) ) ,
34+ Some ( cwd) => println ! ( "{pid_str }: {}" , cwd. display( ) ) ,
3335 None => {
3436 set_exit_code ( 1 ) ;
35- eprintln ! ( "{pid }: Permission denied" ) ;
37+ eprintln ! ( "{pid_str }: Permission denied" ) ;
3638 }
3739 } ,
3840 None => {
3941 set_exit_code ( 1 ) ;
40- eprintln ! ( "{pid }: No such process" ) ;
42+ eprintln ! ( "{pid_str }: No such process" ) ;
4143 }
4244 }
4345 }
Original file line number Diff line number Diff line change @@ -104,6 +104,32 @@ fn test_invalid_and_valid_pid() {
104104 . stderr_contains ( format ! ( "invalid process id: {invalid_pid}" ) ) ;
105105}
106106
107+ #[ test]
108+ fn test_proc_path_pid ( ) {
109+ let pid = process:: id ( ) ;
110+
111+ new_ucmd ! ( )
112+ . arg ( format ! ( "/proc/{pid}" ) )
113+ . succeeds ( )
114+ . stdout_matches ( & Regex :: new ( & format ! ( "^/proc/{pid}: .+\n $" ) ) . unwrap ( ) ) ;
115+
116+ new_ucmd ! ( )
117+ . arg ( "/proc/999999" )
118+ . fails ( )
119+ . code_is ( 1 )
120+ . no_stdout ( )
121+ . stderr_is ( "/proc/999999: No such process\n " ) ;
122+
123+ for invalid_pid in [ "/proc/0" , "/proc/" , "/proc/self" ] {
124+ new_ucmd ! ( )
125+ . arg ( invalid_pid)
126+ . fails ( )
127+ . code_is ( 1 )
128+ . no_stdout ( )
129+ . stderr_contains ( format ! ( "invalid process id: {invalid_pid}" ) ) ;
130+ }
131+ }
132+
107133#[ test]
108134fn test_invalid_arg ( ) {
109135 new_ucmd ! ( ) . arg ( "--definitely-invalid" ) . fails ( ) . code_is ( 1 ) ;
You can’t perform that action at this time.
0 commit comments