procfs: Exploring the proc Pseudo-filesystem in Rust
The procfs
crate is a powerful tool for developers working with Rust, designed to interact seamlessly with the /proc
filesystem in Linux-based systems. This pseudo-filesystem, typically mounted at /proc
, serves as an interface to kernel data structures and is a vital component for accessing system information in Linux.
Overview
procfs
aims to provide comprehensive access to the /proc
filesystem. While not all files are currently exposed, the goal is to achieve thorough coverage over time. Comprehensive documentation is available to guide users on supported features, which can be found in the crate’s support.md file.
Example Usage
The crate includes several examples demonstrating its capabilities. One such example replicates the basic functionality of the ps
command, by listing processes running on the same TTY as the executing process. Here’s a snippet to illustrate:
fn main() {
let me = procfs::process::Process::myself().unwrap();
let me_stat = me.stat().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() {
let prc = prc.unwrap();
let stat = prc.stat().unwrap();
if stat.tty_nr == me_stat.tty_nr {
let total_time =
(stat.utime + stat.stime) as f32 / (tps as f32);
println!(
"{: >5} {: <8} {: >8} {}",
stat.pid, tty, total_time, stat.comm
);
}
}
}
Another practical example showcases how to retrieve current memory usage for a process:
use procfs::process::Process;
fn main() {
let me = Process::myself().unwrap();
let me_stat = me.stat().unwrap();
println!("PID: {}", me.pid);
let page_size = procfs::page_size();
println!("Memory page size: {}", page_size);
println!("== Data from /proc/self/stat:");
println!("Total virtual memory used: {} bytes", me_stat.vsize);
println!(
"Total resident set: {} pages ({} bytes)",
me_stat.rss,
me_stat.rss * page_size
);
}
For those seeking even more detail, the self_memory example provides deeper insights into memory fetching techniques.
Cargo Features
To enhance its functionality, procfs
offers several optional Cargo features:
chrono
(Default): Enables methods that returnDateTime
objects.flate2
(Default): Allows parsing of gzip-compressed files, such as/proc/config.gz
.backtrace
: Provides stack traces whenInternalError
occurs.serde1
: Supports serialization and deserialization usingserde 1.0
. Note that this requires a newer rust version than 1.48.0.
Minimum Rust Version
While only tested on Rust's latest stable compiler, procfs
may work with older versions. Detailed information about the minimum supported Rust version can be found here.
Licensing and Contribution
The procfs
crate is dual-licensed under the Apache License 2.0 and the MIT License, allowing flexibility for developers in choosing the terms that best suit their needs.
Contributions to the project are highly encouraged, especially in areas like documentation and compatibility with older kernel versions.
In conclusion, procfs
is a versatile crate that offers developers the tools they need to effectively interact with the /proc
filesystem in Linux, making it an invaluable asset for system-level programming in Rust.