@@ -163,21 +163,7 @@ impl From<ProcType> for processes::ProcFilter {
163163///
164164/// This function is deprecated in favor of
165165/// [`libproc::processes::pids_by_type()`][crate::processes::pids_by_type],
166- /// which lets you specify what PGRP / TTY / UID / RUID / PPID you want to
167- /// filter by.
168- ///
169- /// # Examples
170- ///
171- /// Get the list of running process IDs using `listpids`
172- ///
173- /// ```
174- /// use std::io::Write;
175- /// use libproc::libproc::proc_pid;
176- ///
177- /// if let Ok(pids) = proc_pid::listpids(proc_pid::ProcType::ProcAllPIDS) {
178- /// println!("Found {} processes using listpids()", pids.len());
179- /// }
180- /// ```
166+ /// which lets you specify what PGRP / TTY / UID / RUID / PPID you want to filter by
181167#[ deprecated(
182168 since = "0.13.0" ,
183169 note = "Please use `libproc::processes::pids_by_type()` instead."
@@ -233,7 +219,7 @@ pub fn listpidspath(proc_types: ProcType, path: &str) -> Result<Vec<u32>, String
233219/// // Get the `BSDInfo` for Process of pid 0
234220/// match pidinfo::<BSDInfo>(pid, 0) {
235221/// Ok(info) => assert_eq!(info.pbi_pid as i32, pid),
236- /// Err(err) => assert!(false, "Error retrieving process info: {}", err)
222+ /// Err(err) => eprintln!( "Error retrieving process info: {}", err)
237223/// };
238224/// ```
239225#[ cfg( target_os = "macos" ) ]
@@ -274,7 +260,7 @@ pub fn pidinfo<T: PIDInfo>(_pid: i32, _arg: u64) -> Result<T, String> {
274260/// if am_root() {
275261/// match regionfilename(1, 0) {
276262/// Ok(regionfilename) => println!("Region Filename (at address = 0) of init process PID = 1 is '{}'", regionfilename),
277- /// Err(message ) => panic!(message )
263+ /// Err(err ) => eprintln!("Error: {}", err )
278264/// }
279265/// }
280266/// ```
@@ -305,7 +291,7 @@ pub fn regionfilename(pid: i32, address: u64) -> Result<String, String> {
305291/// if am_root() {
306292/// match regionfilename(1, 0) {
307293/// Ok(regionfilename) => println!("Region Filename (at address = 0) of init process PID = 1 is '{}'", regionfilename),
308- /// Err(message ) => panic!(message )
294+ /// Err(err ) => eprintln!("Error: {}", err )
309295/// }
310296/// }
311297/// ```
@@ -323,7 +309,7 @@ pub fn regionfilename(_pid: i32, _address: u64) -> Result<String, String> {
323309///
324310/// match pidpath(1) {
325311/// Ok(path) => println!("Path of init process with PID = 1 is '{}'", path),
326- /// Err(message ) => assert!(false, " {}", message )
312+ /// Err(err ) => eprintln!("Error: {}", err )
327313/// }
328314/// ```
329315#[ cfg( target_os = "macos" ) ]
@@ -350,8 +336,8 @@ pub fn pidpath(pid: i32) -> Result<String, String> {
350336/// match pidpath(1) {
351337/// Ok(path) => println!("Path of init process with PID = 1 is '{}'", path),
352338/// Err(_) if !am_root() => println!("pidpath() needs to be run as root"),
353- /// Err(message ) if am_root() => assert!(false, " {}", message ),
354- /// _ => assert!(false, "Unknown error")
339+ /// Err(err ) if am_root() => eprintln!("Error: {}", err ),
340+ /// _ => panic!( "Unknown error")
355341/// }
356342/// ```
357343#[ cfg( target_os = "linux" ) ]
@@ -373,12 +359,11 @@ pub fn pidpath(pid: i32) -> Result<String, String> {
373359/// # Examples
374360///
375361/// ```
376- /// use std::io::Write;
377362/// use libproc::libproc::proc_pid;
378363///
379364/// match proc_pid::libversion() {
380365/// Ok((major, minor)) => println!("Libversion: {}.{}", major, minor),
381- /// Err(err) => writeln!(&mut std::io::stderr(), "Error: {}", err).unwrap( )
366+ /// Err(err) => eprintln!( "Error: {}", err)
382367/// }
383368/// ```
384369#[ cfg( target_os = "macos" ) ]
@@ -404,30 +389,28 @@ pub fn libversion() -> Result<(i32, i32), String> {
404389/// # Examples
405390///
406391/// ```
407- /// use std::io::Write;
408392/// use libproc::libproc::proc_pid;
409393///
410394/// match proc_pid::libversion() {
411395/// Ok((major, minor)) => println!("Libversion: {}.{}", major, minor),
412- /// Err(err) => writeln!(&mut std::io::stderr(), "Error: {}", err).unwrap( )
396+ /// Err(err) => eprintln!( "Error: {}", err)
413397/// }
414398/// ```
415399#[ cfg( not( target_os = "macos" ) ) ]
416400pub fn libversion ( ) -> Result < ( i32 , i32 ) , String > {
417401 Err ( "Linux does not use a library, so no library version number" . to_owned ( ) )
418402}
419403
420- /// Get the name of a process
404+ /// Get the name of a process, using it's process id (pid)
421405///
422406/// # Examples
423407///
424408/// ```
425- /// use std::io::Write;
426409/// use libproc::libproc::proc_pid;
427410///
428411/// match proc_pid::name(1) {
429412/// Ok(name) => println!("Name: {}", name),
430- /// Err(err) => writeln!(&mut std::io::stderr(), "Error: {}", err).unwrap( )
413+ /// Err(err) => eprintln!( "Error: {}", err)
431414/// }
432415/// ```
433416#[ cfg( target_os = "macos" ) ]
@@ -456,7 +439,7 @@ pub fn name(pid: i32) -> Result<String, String> {
456439}
457440
458441
459- /// Get the name of a Process using it's Pid
442+ /// Get the name of a process, using it's process id (pid)
460443#[ cfg( target_os = "linux" ) ]
461444pub fn name ( pid : i32 ) -> Result < String , String > {
462445 helpers:: procfile_field ( & format ! ( "/proc/{pid}/status" ) , "Name" )
@@ -470,7 +453,6 @@ pub fn name(pid: i32) -> Result<String, String> {
470453/// # Examples
471454///
472455/// ```
473- /// use std::io::Write;
474456/// use libproc::libproc::proc_pid::{listpidinfo, pidinfo};
475457/// use libproc::libproc::task_info::TaskAllInfo;
476458/// use libproc::libproc::file_info::{ListFDs, ProcFDType};
@@ -524,12 +506,11 @@ pub fn listpidinfo<T: ListPIDInfo>(_pid: i32, _max_len: usize) -> Result<Vec<T::
524506/// # Examples
525507///
526508/// ```
527- /// use std::io::Write;
528509/// use libproc::libproc::proc_pid::pidcwd;
529510///
530511/// match pidcwd(1) {
531512/// Ok(cwd) => println!("The CWD of the process with pid=1 is '{}'", cwd.display()),
532- /// Err(err) => writeln!(&mut std::io::stderr(), "Error: {}", err).unwrap( )
513+ /// Err(err) => eprintln!( "Error: {}", err)
533514/// }
534515/// ```
535516pub fn pidcwd ( _pid : pid_t ) -> Result < PathBuf , String > {
@@ -542,12 +523,11 @@ pub fn pidcwd(_pid: pid_t) -> Result<PathBuf, String> {
542523/// # Examples
543524///
544525/// ```
545- /// use std::io::Write;
546526/// use libproc::libproc::proc_pid::pidcwd;
547527///
548528/// match pidcwd(1) {
549529/// Ok(cwd) => println!("The CWD of the process with pid=1 is '{}'", cwd.display()),
550- /// Err(err) => writeln!(&mut std::io::stderr(), "Error: {}", err).unwrap( )
530+ /// Err(err) => eprintln!( "Error: {}", err)
551531/// }
552532/// ```
553533pub fn pidcwd ( pid : pid_t ) -> Result < PathBuf , String > {
@@ -563,12 +543,11 @@ pub fn pidcwd(pid: pid_t) -> Result<PathBuf, String> {
563543/// # Examples
564544///
565545/// ```
566- /// use std::io::Write;
567546/// use libproc::libproc::proc_pid::cwdself;
568547///
569548/// match cwdself() {
570549/// Ok(cwd) => println!("The CWD of the current process is '{}'", cwd.display()),
571- /// Err(err) => writeln!(&mut std::io::stderr(), "Error: {}", err).unwrap( )
550+ /// Err(err) => eprintln!( "Error: {}", err)
572551/// }
573552/// ```
574553pub fn cwdself ( ) -> Result < PathBuf , String > {
@@ -603,7 +582,6 @@ pub fn am_root() -> bool {
603582// run tests with 'cargo test -- --nocapture' to see the test output
604583#[ cfg( test) ]
605584mod test {
606- #[ cfg( target_os = "linux" ) ]
607585 use std:: process;
608586 use std:: env;
609587
@@ -632,7 +610,6 @@ mod test {
632610 #[ cfg( target_os = "macos" ) ]
633611 #[ test]
634612 fn pidinfo_test ( ) {
635- use std:: process;
636613 let pid = process:: id ( ) as i32 ;
637614
638615 match pidinfo :: < BSDInfo > ( pid, 0 ) {
@@ -644,7 +621,6 @@ mod test {
644621 #[ cfg( target_os = "macos" ) ]
645622 #[ test]
646623 fn taskinfo_test ( ) {
647- use std:: process;
648624 let pid = process:: id ( ) as i32 ;
649625
650626 match pidinfo :: < TaskInfo > ( pid, 0 ) {
@@ -656,7 +632,6 @@ mod test {
656632 #[ cfg( target_os = "macos" ) ]
657633 #[ test]
658634 fn taskallinfo_test ( ) {
659- use std:: process;
660635 let pid = process:: id ( ) as i32 ;
661636
662637 match pidinfo :: < TaskAllInfo > ( pid, 0 ) {
@@ -669,7 +644,6 @@ mod test {
669644 #[ cfg( target_os = "macos" ) ]
670645 #[ test]
671646 fn threadinfo_test ( ) {
672- use std:: process;
673647 let pid = process:: id ( ) as i32 ;
674648
675649 match pidinfo :: < ThreadInfo > ( pid, 0 ) {
@@ -682,7 +656,6 @@ mod test {
682656 #[ cfg( target_os = "macos" ) ]
683657 #[ test]
684658 fn workqueueinfo_test ( ) {
685- use std:: process;
686659 let pid = process:: id ( ) as i32 ;
687660
688661 match pidinfo :: < WorkQueueInfo > ( pid, 0 ) {
@@ -694,7 +667,6 @@ mod test {
694667 #[ cfg( target_os = "macos" ) ]
695668 #[ test]
696669 fn listpidinfo_test ( ) {
697- use std:: process;
698670 let pid = process:: id ( ) as i32 ;
699671
700672 if let Ok ( info) = pidinfo :: < TaskAllInfo > ( pid, 0 ) {
@@ -715,16 +687,9 @@ mod test {
715687
716688 #[ test]
717689 fn name_test ( ) {
718- #[ cfg( target_os = "linux" ) ]
719- let expected_name = "systemd" ;
720- #[ cfg( target_os = "macos" ) ]
721- let expected_name = "launchd" ;
722-
723690 if am_root ( ) || cfg ! ( target_os = "linux" ) {
724- match name ( 1 ) {
725- Ok ( name) => assert_eq ! ( expected_name, name) ,
726- Err ( _) => panic ! ( "Error retrieving process name" )
727- }
691+ assert ! ( & name( process:: id( ) as i32 ) . expect( "Could not get the process name" )
692+ . starts_with( "libproc" ) , "Incorrect process name" ) ;
728693 } else {
729694 println ! ( "Cannot run 'name_test' on macos unless run as root" ) ;
730695 }
0 commit comments