@@ -306,6 +306,33 @@ impl ProcessInformation {
306306 . map_err ( |_| io:: ErrorKind :: InvalidData . into ( ) )
307307 }
308308
309+ fn get_uid_or_gid_field ( & mut self , field : & str , index : usize ) -> Result < u32 , io:: Error > {
310+ self . status ( )
311+ . get ( field)
312+ . ok_or ( io:: ErrorKind :: InvalidData ) ?
313+ . split_whitespace ( )
314+ . nth ( index)
315+ . ok_or ( io:: ErrorKind :: InvalidData ) ?
316+ . parse :: < u32 > ( )
317+ . map_err ( |_| io:: ErrorKind :: InvalidData . into ( ) )
318+ }
319+
320+ pub fn uid ( & mut self ) -> Result < u32 , io:: Error > {
321+ self . get_uid_or_gid_field ( "Uid" , 0 )
322+ }
323+
324+ pub fn euid ( & mut self ) -> Result < u32 , io:: Error > {
325+ self . get_uid_or_gid_field ( "Uid" , 1 )
326+ }
327+
328+ pub fn gid ( & mut self ) -> Result < u32 , io:: Error > {
329+ self . get_uid_or_gid_field ( "Gid" , 0 )
330+ }
331+
332+ pub fn egid ( & mut self ) -> Result < u32 , io:: Error > {
333+ self . get_uid_or_gid_field ( "Gid" , 1 )
334+ }
335+
309336 /// Fetch run state from [ProcessInformation::cached_stat]
310337 ///
311338 /// - [The /proc Filesystem: Table 1-4](https://docs.kernel.org/filesystems/proc.html#id10)
@@ -517,4 +544,17 @@ mod tests {
517544 let case = "83875 (sleep (2) .sh) S 75750 83875 75750 34824 83875 4194304 173 0 0 0 0 0 0 0 20 0 1 0 18366278 23187456 821 18446744073709551615 94424231874560 94424232638561 140734866834816 0 0 0 65536 4 65538 1 0 0 17 6 0 0 0 0 0 94424232876752 94424232924772 94424259932160 140734866837287 140734866837313 140734866837313 140734866841576 0" ;
518545 assert ! ( stat_split( case) [ 1 ] == "sleep (2) .sh" ) ;
519546 }
547+
548+ #[ test]
549+ #[ cfg( target_os = "linux" ) ]
550+ fn test_uid_gid ( ) {
551+ let mut pid_entry = ProcessInformation :: try_new (
552+ PathBuf :: from_str ( & format ! ( "/proc/{}" , current_pid( ) ) ) . unwrap ( ) ,
553+ )
554+ . unwrap ( ) ;
555+ assert_eq ! ( pid_entry. uid( ) . unwrap( ) , uucore:: process:: getuid( ) ) ;
556+ assert_eq ! ( pid_entry. euid( ) . unwrap( ) , uucore:: process:: geteuid( ) ) ;
557+ assert_eq ! ( pid_entry. gid( ) . unwrap( ) , uucore:: process:: getgid( ) ) ;
558+ assert_eq ! ( pid_entry. egid( ) . unwrap( ) , uucore:: process:: getegid( ) ) ;
559+ }
520560}
0 commit comments