[][src]Crate procfs

This crate provides to an interface into the linux procfs filesystem, usually mounted at /proc.

This is a pseudo-filesystem which is available on most every linux system and provides an interface to kernel data structures.

Kernel support

Not all fields/data are available in each kernel. Some fields were added in specific kernel releases, and other fields are only present in certain kernel configuration options are enabled. These are represented as Option fields in this crate.

This crate aims to support all 2.6 kernels

Documentation

In almost all cases, the documentation is taken from the proc.5 manual page. This means that sometimes the style of writing is not very "rusty", or may do things like reference related files (instead of referencing related structs). Contributions to improve this are welcome.

Panicing

This crate is not panic-free. It will panic if it encounters data in some unexpected format; this represents a bug in this crate, and should be reported.

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

extern crate procfs;

fn main() {
    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() {
        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

extern crate procfs;

use procfs::{Process, FDTarget};
use std::collections::HashMap;

fn main() {
    let all_procs = procfs::all_processes();

    // 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);
        }
    }
}

Re-exports

pub use crate::sys::kernel::Version as KernelVersion;

Modules

sys

Sysctl is a means of configuring certain aspects of the kernel at run-time, and the /proc/sys/ directory is there so that you don't even need special tools to do it!

Structs

CGroupController
CoredumpFlags
CpuInfo

Represents the data from /proc/cpuinfo.

FDInfo
Io

This struct contains I/O statistics for the process, built from /proc/<pid>/io

LoadAverage

Load average figures.

Meminfo

This struct reports statistics about memory usage on the system, based on the /proc/meminfo file.

MemoryMap

Represents an entry in a /proc/<pid>/maps file.

MountNFSStatistics

Only NFS mounts provide additional statistics in MountStat entries.

MountStat

Mount information from /proc/<pid>/mountstats.

NFSByteCounter

Represents NFS data from /proc/<pid>/mountstats under the section bytes.

NFSEventCounter

Represents NFS data from /proc/<pid>/mountstats under the section events.

NFSOperationStat

Represents NFS data from /proc/<pid>/mountstats under the section of per-op statistics.

NFSServerCaps
Process

Represents a process in /proc/<pid>.

ProcessCgroup
Stat

Status information about the process, based on the /proc/<pid>/stat file.

StatFlags
Status

Status information about the process, based on the /proc/<pid>/status file.

TcpNetEntry

An entry in the TCP socket table

UdpNetEntry

An entry in the UDP socket table

Enums

ConfigSetting
FDTarget
MMapPath
ProcError

Error type for most procfs functions.

ProcState

Represents the state of a process.

TcpState

Functions

all_processes
boot_time

The boottime of the system.

cgroups

Information about the cgroup controllers that are compiled into the kernel

cpuinfo
kernel_config

Returns a configuration options used to build the currently running kernel

meminfo
page_size

Memory page size, in bytes.

tcp

Reads the tcp socket table

tcp6

Reads the tcp6 socket table

ticks_per_second

Return the number of ticks per second.

udp

Reads the udp socket table

udp6

Reads the udp6 socket table

Type Definitions

NFSPerOpStats
ProcResult