Skip to content

Commit

Permalink
Move some things back into modules
Browse files Browse the repository at this point in the history
While this is a breaking change, I think this is a good move for the
long term, as it will help keep the documentation more organized.
  • Loading branch information
eminence committed Oct 26, 2019
1 parent df24099 commit ac217a9
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 77 deletions.
4 changes: 2 additions & 2 deletions examples/dump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
8 changes: 4 additions & 4 deletions examples/netstat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u32, &Process> = HashMap::new();
Expand All @@ -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} {}",
Expand Down
4 changes: 2 additions & 2 deletions examples/ps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/cgroups.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::ProcResult;

use super::Process;
use super::process::Process;

#[derive(Debug)]
/// Container group controller information.
Expand Down
71 changes: 4 additions & 67 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u32, &Process> = 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;
Expand Down Expand Up @@ -320,14 +258,12 @@ pub(crate) fn write_value<P: AsRef<Path>, 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::*;
Expand Down Expand Up @@ -957,6 +893,7 @@ impl KernelStats {
mod tests {
extern crate failure;
use super::*;
use super::process::Process;

#[test]
fn test_statics() {
Expand Down
47 changes: 47 additions & 0 deletions src/net.rs
Original file line number Diff line number Diff line change
@@ -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<u32, &Process> = 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;
Expand Down Expand Up @@ -201,6 +247,7 @@ pub fn udp6() -> ProcResult<Vec<UdpNetEntry>> {
read_udp_table(BufReader::new(file))
}


#[cfg(test)]
mod tests {
use super::*;
Expand Down
37 changes: 36 additions & 1 deletion src/process.rs
Original file line number Diff line number Diff line change
@@ -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/<pid>/
//! 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;
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit ac217a9

Please sign in to comment.