diff --git a/examples/dump.rs b/examples/dump.rs index 93a81c15..cdc337cf 100644 --- a/examples/dump.rs +++ b/examples/dump.rs @@ -6,9 +6,9 @@ fn main() { let prc = if let Some(pid) = pid { println!("Info for pid={}", pid); - procfs::Process::new(pid).unwrap() + procfs::process::Process::new(pid).unwrap() } else { - procfs::Process::myself().unwrap() + procfs::process::Process::myself().unwrap() }; println!("{:#?}", prc); diff --git a/examples/netstat.rs b/examples/netstat.rs index 9bac5549..35a0ab1a 100644 --- a/examples/netstat.rs +++ b/examples/netstat.rs @@ -2,13 +2,13 @@ extern crate procfs; -use procfs::{FDTarget, Process}; +use procfs::process::{FDTarget, Process}; use std::collections::HashMap; fn main() { // get all processes - let all_procs = procfs::all_processes().unwrap(); + let all_procs = procfs::process::all_processes().unwrap(); // build up a map between socket inodes and processes: let mut map: HashMap = HashMap::new(); @@ -23,8 +23,8 @@ fn main() { } // get the tcp table - let tcp = procfs::tcp().unwrap(); - let tcp6 = procfs::tcp6().unwrap(); + let tcp = procfs::net::tcp().unwrap(); + let tcp6 = procfs::net::tcp6().unwrap(); println!( "{:<26} {:<26} {:<15} {:<8} {}", diff --git a/examples/ps.rs b/examples/ps.rs index 4d50b307..f688f87e 100644 --- a/examples/ps.rs +++ b/examples/ps.rs @@ -6,13 +6,13 @@ extern crate procfs; /// It shows all the processes that share the same tty as our self fn main() { - let me = procfs::Process::myself().unwrap(); + let me = procfs::process::Process::myself().unwrap(); let tps = procfs::ticks_per_second().unwrap(); println!("{: >5} {: <8} {: >8} {}", "PID", "TTY", "TIME", "CMD"); let tty = format!("pty/{}", me.stat.tty_nr().1); - for prc in procfs::all_processes().unwrap() { + for prc in procfs::process::all_processes().unwrap() { if prc.stat.tty_nr == me.stat.tty_nr { // total_time is in seconds let total_time = (prc.stat.utime + prc.stat.stime) as f32 / (tps as f32); diff --git a/src/cgroups.rs b/src/cgroups.rs index edd2e029..3aa3bdf0 100644 --- a/src/cgroups.rs +++ b/src/cgroups.rs @@ -1,6 +1,6 @@ use crate::ProcResult; -use super::Process; +use super::process::Process; #[derive(Debug)] /// Container group controller information. diff --git a/src/lib.rs b/src/lib.rs index d18a3581..b66f2549 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -38,71 +38,9 @@ //! //! # Examples //! -//! Here's a small example that prints out all processes that are running on the same tty as the calling -//! process. This is very similar to what "ps" does in its default mode. You can run this example -//! yourself with: +//! Examples can be found in the various modules shown below. //! -//! > cargo run --example=ps //! -//! ```rust -//! let me = procfs::Process::myself().unwrap(); -//! let tps = procfs::ticks_per_second().unwrap(); -//! -//! println!("{: >5} {: <8} {: >8} {}", "PID", "TTY", "TIME", "CMD"); -//! -//! let tty = format!("pty/{}", me.stat.tty_nr().1); -//! for prc in procfs::all_processes().unwrap() { -//! if prc.stat.tty_nr == me.stat.tty_nr { -//! // total_time is in seconds -//! let total_time = -//! (prc.stat.utime + prc.stat.stime) as f32 / (tps as f32); -//! println!( -//! "{: >5} {: <8} {: >8} {}", -//! prc.stat.pid, tty, total_time, prc.stat.comm -//! ); -//! } -//! } -//! ``` -//! -//! Here's another example that will print out all of the open and listening TCP sockets, and their -//! corresponding processes, if know. This mimics the "netstat" utility, but for TCP only. You -//! can run this example yourself with: -//! -//! > cargo run --example=netstat -//! -//! ```rust -//! # use procfs::{Process, FDTarget}; -//! # use std::collections::HashMap; -//! let all_procs = procfs::all_processes().unwrap(); -//! -//! // build up a map between socket inodes and processes: -//! let mut map: HashMap = HashMap::new(); -//! for process in &all_procs { -//! if let Ok(fds) = process.fd() { -//! for fd in fds { -//! if let FDTarget::Socket(inode) = fd.target { -//! map.insert(inode, process); -//! } -//! } -//! } -//! } -//! -//! // get the tcp table -//! let tcp = procfs::tcp().unwrap(); -//! let tcp6 = procfs::tcp6().unwrap(); -//! println!("{:<26} {:<26} {:<15} {:<8} {}", "Local address", "Remote address", "State", "Inode", "PID/Program name"); -//! for entry in tcp.into_iter().chain(tcp6) { -//! // find the process (if any) that has an open FD to this entry's inode -//! let local_address = format!("{}", entry.local_address); -//! let remote_addr = format!("{}", entry.remote_address); -//! let state = format!("{:?}", entry.state); -//! if let Some(process) = map.get(&entry.inode) { -//! println!("{:<26} {:<26} {:<15} {:<8} {}/{}", local_address, remote_addr, state, entry.inode, process.stat.pid, process.stat.comm); -//! } else { -//! // We might not always be able to find the process assocated with this socket -//! println!("{:<26} {:<26} {:<15} {:<8} -", local_address, remote_addr, state, entry.inode); -//! } -//! } #[cfg(unix)] extern crate libc; @@ -320,14 +258,12 @@ pub(crate) fn write_value, T: fmt::Display>(path: P, value: T) -> write_file(path, value.to_string().as_bytes()) } -mod process; -pub use crate::process::*; +pub mod process; mod meminfo; pub use crate::meminfo::*; -mod net; -pub use crate::net::*; +pub mod net; mod cpuinfo; pub use crate::cpuinfo::*; @@ -957,6 +893,7 @@ impl KernelStats { mod tests { extern crate failure; use super::*; + use super::process::Process; #[test] fn test_statics() { diff --git a/src/net.rs b/src/net.rs index dad49ae1..e7295e31 100644 --- a/src/net.rs +++ b/src/net.rs @@ -1,3 +1,49 @@ +//! Information about the networking layer. +//! +//! This module corresponds to the `/proc/net` directory and contains various information about the +//! networking layer. +//! +//! # Example +//! +//! Here's an example that will print out all of the open and listening TCP sockets, and their +//! corresponding processes, if know. This mimics the "netstat" utility, but for TCP only. You +//! can run this example yourself with: +//! +//! > cargo run --example=netstat +//! +//! ```rust +//! # use procfs::process::{Process, FDTarget}; +//! # use std::collections::HashMap; +//! let all_procs = procfs::process::all_processes().unwrap(); +//! +//! // build up a map between socket inodes and processes: +//! let mut map: HashMap = HashMap::new(); +//! for process in &all_procs { +//! if let Ok(fds) = process.fd() { +//! for fd in fds { +//! if let FDTarget::Socket(inode) = fd.target { +//! map.insert(inode, process); +//! } +//! } +//! } +//! } +//! +//! // get the tcp table +//! let tcp = procfs::net::tcp().unwrap(); +//! let tcp6 = procfs::tcp6().unwrap(); +//! println!("{:<26} {:<26} {:<15} {:<8} {}", "Local address", "Remote address", "State", "Inode", "PID/Program name"); +//! for entry in tcp.into_iter().chain(tcp6) { +//! // find the process (if any) that has an open FD to this entry's inode +//! let local_address = format!("{}", entry.local_address); +//! let remote_addr = format!("{}", entry.remote_address); +//! let state = format!("{:?}", entry.state); +//! if let Some(process) = map.get(&entry.inode) { +//! println!("{:<26} {:<26} {:<15} {:<8} {}/{}", local_address, remote_addr, state, entry.inode, process.stat.pid, process.stat.comm); +//! } else { +//! // We might not always be able to find the process assocated with this socket +//! println!("{:<26} {:<26} {:<15} {:<8} -", local_address, remote_addr, state, entry.inode); +//! } +//! } use crate::ProcResult; use crate::FileWrapper; @@ -201,6 +247,7 @@ pub fn udp6() -> ProcResult> { read_udp_table(BufReader::new(file)) } + #[cfg(test)] mod tests { use super::*; diff --git a/src/process.rs b/src/process.rs index e7e2b1f2..0b06ead8 100644 --- a/src/process.rs +++ b/src/process.rs @@ -1,3 +1,38 @@ +//! Functions and structs related to process information +//! +//! The primary source of data for functions in this module is the files in a /proc// +//! directory. If you have a process id, you can use +//! [`Process::new(pid)`](struct.Process.html#method.new), otherwise you can get a +//! list of all running processes using [`all_processes()`](fn.all_processes.html). +//! +//! # Examples +//! +//! Here's a small example that prints out all processes that are running on the same tty as the calling +//! process. This is very similar to what "ps" does in its default mode. You can run this example +//! yourself with: +//! +//! > cargo run --example=ps +//! +//! ```rust +//! let me = procfs::process::Process::myself().unwrap(); +//! let tps = procfs::ticks_per_second().unwrap(); +//! +//! println!("{: >5} {: <8} {: >8} {}", "PID", "TTY", "TIME", "CMD"); +//! +//! let tty = format!("pty/{}", me.stat.tty_nr().1); +//! for prc in procfs::process::all_processes().unwrap() { +//! if prc.stat.tty_nr == me.stat.tty_nr { +//! // total_time is in seconds +//! let total_time = +//! (prc.stat.utime + prc.stat.stime) as f32 / (tps as f32); +//! println!( +//! "{: >5} {: <8} {: >8} {}", +//! prc.stat.pid, tty, total_time, prc.stat.comm +//! ); +//! } +//! } +//! ``` + use super::*; use std::ffi::OsString; @@ -497,7 +532,7 @@ pub struct Io { /// # Example: /// /// ``` -/// # use procfs::Process; +/// # use procfs::process::Process; /// let stats = Process::myself().unwrap().mountstats().unwrap(); /// /// for mount in stats {