66use clap:: ArgMatches ;
77#[ cfg( target_family = "unix" ) ]
88use nix:: errno:: Errno ;
9- use std:: { cell:: RefCell , path :: PathBuf , rc:: Rc , str :: FromStr } ;
9+ use std:: { cell:: RefCell , rc:: Rc } ;
1010use uu_pgrep:: process:: { ProcessInformation , Teletype } ;
1111
1212// TODO: Temporary add to this file, this function will add to uucore.
@@ -29,25 +29,18 @@ fn getsid(_pid: i32) -> Option<i32> {
2929 Some ( 0 )
3030}
3131
32- // Guessing it matches the current terminal
32+ // Default behavior: select processes with same terminal and same effective user ID
3333pub ( crate ) fn basic_collector (
3434 proc_snapshot : & [ Rc < RefCell < ProcessInformation > > ] ,
3535) -> Vec < Rc < RefCell < ProcessInformation > > > {
3636 let mut result = Vec :: new ( ) ;
3737
38- let current_tty = {
39- // SAFETY: The `libc::getpid` always return i32
40- let proc_path =
41- PathBuf :: from_str ( & format ! ( "/proc/{}/" , unsafe { libc:: getpid( ) } ) ) . unwrap ( ) ;
42- let current_proc_info = ProcessInformation :: try_new ( proc_path) . unwrap ( ) ;
43-
44- current_proc_info. tty ( )
45- } ;
38+ let mut cur = ProcessInformation :: current_process_info ( ) . unwrap ( ) ;
39+ let tty = cur. tty ( ) ;
40+ let euid = cur. euid ( ) . unwrap ( ) ;
4641
4742 for proc_info in proc_snapshot {
48- let proc_ttys = proc_info. borrow ( ) . tty ( ) ;
49-
50- if proc_ttys == current_tty {
43+ if proc_info. borrow ( ) . tty ( ) == tty && proc_info. borrow_mut ( ) . euid ( ) . unwrap ( ) == euid {
5144 result. push ( proc_info. clone ( ) ) ;
5245 }
5346 }
@@ -85,9 +78,14 @@ pub(crate) fn session_collector(
8578 let tty = |proc : & Rc < RefCell < ProcessInformation > > | proc. borrow_mut ( ) . tty ( ) ;
8679
8780 // flag `-d`
88- // TODO: Implementation this collection, guessing it pid=sid
8981 if matches. get_flag ( "d" ) {
90- proc_snapshot. iter ( ) . for_each ( |_| { } ) ;
82+ for proc_info in proc_snapshot {
83+ let pid = proc_info. borrow ( ) . pid ;
84+
85+ if getsid ( pid as i32 ) != Some ( pid as i32 ) {
86+ result. push ( proc_info. clone ( ) ) ;
87+ }
88+ }
9189 }
9290
9391 // flag `-a`
0 commit comments