[][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 (and newer).

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

While previous versions of the library could panic, this current version aims to be panic-free in a many situations as possible. Whenever the procfs crate encounters a bug in its own parsing code, it will return an InternalError error. This should be considered a bug and should be reported. If you encounter a panic, please report that as well.

Cargo features

The following cargo features are available:

  • chrono -- Default. Optional. This feature enables a few methods that return values as DateTime objects.
  • backtrace -- Optional. This feature lets you get a stack trace whenever an InternalError is raised.

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

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

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

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

Container group controller information.

CoredumpFlags

See the coredump_filter() method.

CpuInfo

Represents the data from /proc/cpuinfo.

CpuPressure

CPU pressure information

CpuTime

The amount of time, measured in seconds, the CPU has been in specific states

FDInfo

See the Process::fd() method

InternalError

An internal error in the procfs crate

Io

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

IoPressure

IO pressure information

KernelStats

Kernel/system statistics, from /proc/stat

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.

MemoryPressure

Memory pressure information

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
PressureRecord

Pressure stall information for either CPU, memory, or IO.

Process

Represents a process in /proc/<pid>.

ProcessCgroup

Information about a process cgroup

Stat

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

StatFlags

Kernel flags for a process

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

Possible values for a kernel config option

FDTarget

Describes a file descriptor opened by a process.

MMapPath
ProcError

The various error conditions in the procfs crate.

ProcState

Represents the state of a process.

TcpState

Functions

all_processes

Return a list of all processes

boot_time

The boottime of the system, as a DateTime object.

boot_time_secs

The boottime of the system, in seconds since the epoch

cgroups

Information about the cgroup controllers that are compiled into the kernel

cpu_pressure

Get CPU pressure information

cpuinfo

Get CPU info, from /proc/cpuinfo

io_pressure

Get IO pressure information

kernel_config

Returns a configuration options used to build the currently running kernel

memory_pressure

Get memory pressure information

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

The main error type for the procfs crate.