1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//! `procinfo` is a simple program to demonstrate the use of the [`libproc`](../libproc/index.html)
//! library functions.
//!
//! It prints out info about a process specified by its pid, or the current process if no pid
//! specified.
//!
//! Usage
//! =
//!```
//! procinfo [pid]
//!
//!```
//!
//! Which will produce output similar to:
//! ```
//! Libversion: 1.1
//! Pid: 8484
//! Path: /Users/andrew/workspace/libproc-rs/target/debug/procinfo
//! Name: procinfo
//! Region Filename (at address 0): /Users/andrew/workspace/libproc-rs/target/debug/procinfo
//! There are currently 454 processes active
//! 8496
//! ...
//! ```

extern crate libproc;
extern crate libc;

use std::env;
use std::io::Write;
use libproc::libproc::proc_pid;
use libproc::libproc::pid_rusage::{pidrusage, RUsageInfoV0};
use libproc::processes;

mod c {
    extern crate libc;
    extern {
        pub fn getpid() -> libc::pid_t;
    }
}

fn getpid() -> i32 {
    unsafe {
        c::getpid()
    }
}

fn procinfo(pid: i32) {
    match proc_pid::libversion() {
        Ok((major, minor)) => println!("Libversion: {major}.{minor}"),
        Err(err) => writeln!(&mut std::io::stderr(), "Error: {err}").unwrap()
    }

    println!("Pid: {pid}");

    match proc_pid::pidpath(pid) {
        Ok(path) => println!("Path: {path}"),
        Err(err) => writeln!(&mut std::io::stderr(), "Error: {err}").unwrap()
    }

    match pidrusage::<RUsageInfoV0>(pid) {
        Ok(resource_usage) => println!("Memory Used: {} Bytes", resource_usage.ri_resident_size),
        Err(err) => writeln!(&mut std::io::stderr(), "Error: {err}").unwrap()
    }

    match proc_pid::name(pid) {
        Ok(name) => println!("Name: {name}"),
        Err(err) => writeln!(&mut std::io::stderr(), "Error: {err}").unwrap()
    }

    match proc_pid::regionfilename(pid, 0) {
        Ok(regionfilename) => println!("Region Filename (at address 0): {regionfilename}"),
        Err(err) => writeln!(&mut std::io::stderr(), "Error: {err}").unwrap()
    }

    match processes::pids_by_type(processes::ProcFilter::All) {
        Ok(pids) => {
            println!("There are currently {} processes active", pids.len());
            for pid in pids {
                println!("{pid}");
            }
        },
        Err(err) => writeln!(&mut std::io::stderr(), "Error: {err}").unwrap()
    }
}

fn main() {
    let args: Vec<String> = env::args().collect();

    let pid = if args.len() == 1 {
       getpid()
    } else {
        args[1].clone().parse::<i32>().unwrap()
    };

    procinfo(pid);
}