rust-pstree/pstree.rs
Paul Osborne 5823d36e84 Updates based on code review by @nastevens
This addresses the following feeback:

Your code looks really good, and very Rustic. I only have a few comments:
· In ProcessRecord you use isize for the pid and ppid. These aren’t really sizes, so I’d go with i32
· Line 102: since all you’re doing is panic!() on Err, you can just use .unwrap()
· Line 107: you could use “filter_map” instead of filter and then “collect” the results. This eliminates the need for “mut records” on line 98
· Line 126: use “filter_map”
· I’d add Cargo support
· You could consider using a map type containing vector types for storing the ppid->pid translation. That would improve performance since the record list wouldn’t getting iterated over multiple times. Unfortunately Rust doesn’t have a multimap yet (see https://github.com/rust-lang/rfcs/issues/784), otherwise you could just use that!

Using a map type for the lookup is not supported with this but will
be evaluated.
2015-03-03 23:51:54 -06:00

162 lines
5.1 KiB
Rust

// A version of pstree targetting linux written in rust!
//
// This is based on the following exercise from the excellent
// book "The Linux Programming Interface" by Michael Kerridsk.
//
//----------------------------------------------------------------------
//
// Write a program that draws a tree showing the hierarchical
// parent-child relationships of all processes on the system, going all
// the way back to init. For each process, the program should display
// the process ID and the command being executed. The output of the
// program should be similar to that produced by pstree(1), although it
// does need not to be as sophisticated. The parent of each process on
// the system can be found by inspecing the PPid: line of all of the
// /proc/PID/status files on the system. Be careful to handle the
// possibilty that a process's parent (and thus its /proc/PID directory)
// disappears during the scan of all /proc/PID directories.
// Implementation Notes
// --------------------
// The linux /proc filesystem is a virtual filesystem that provides information
// about processes running on a linux system among other things. The /proc
// filesystem contains a directory, /proc/<pid>, for each running process in
// the system.
//
// Each process directory has a status file with contents including a bunch
// of different items, notably the process name and its parent process id (ppid).
// And with that information, we can build the process tree.
#![feature(old_io)]
#![feature(old_path)]
use std::old_io::fs::PathExtensions;
use std::old_io::fs;
use std::old_io::File;
use std::old_io::BufferedReader;
#[derive(Clone,Debug)]
struct ProcessRecord {
name: String,
pid: i32,
ppid: i32,
}
#[derive(Clone,Debug)]
struct ProcessTreeNode {
record: ProcessRecord, // the node owns the associated record
children: Vec<ProcessTreeNode>, // nodes own their children
}
#[derive(Clone,Debug)]
struct ProcessTree {
root: ProcessTreeNode, // tree owns ref to root node
}
impl ProcessTreeNode {
// constructor
fn new(record : &ProcessRecord) -> ProcessTreeNode {
ProcessTreeNode { record: (*record).clone(), children: Vec::new() }
}
}
// Given a status file path, return a hashmap with the following form:
// pid -> ProcessRecord
fn get_process_record(status_path: &Path) -> Option<ProcessRecord> {
let mut pid : Option<i32> = None;
let mut ppid : Option<i32> = None;
let mut name : Option<String> = None;
let mut status_file = BufferedReader::new(File::open(status_path));
for line in status_file.lines() {
let unwrapped = line.unwrap(); // need a new lifeline
let parts : Vec<&str> = unwrapped[..].splitn(2, ':').collect();
if parts.len() == 2 {
let key = parts[0].trim();
let value = parts[1].trim();
match key {
"Name" => name = Some(value.to_string()),
"Pid" => pid = value.parse().ok(),
"PPid" => ppid = value.parse().ok(),
_ => (),
}
}
}
return if pid.is_some() && ppid.is_some() && name.is_some() {
Some(ProcessRecord { name: name.unwrap(), pid: pid.unwrap(), ppid: ppid.unwrap() })
} else {
None
}
}
// build a simple struct (ProcessRecord) for each process
fn get_process_records() -> Vec<ProcessRecord> {
let proc_directory = Path::new("/proc");
// find potential process directories under /proc
let proc_directory_contents = fs::readdir(&proc_directory).unwrap();
proc_directory_contents.iter().filter_map(|entry| {
if entry.is_dir() {
let status_path = entry.join("status");
if status_path.exists() {
return get_process_record(&status_path)
}
}
None
}).collect()
}
fn populate_node(node : &mut ProcessTreeNode, records: &Vec<ProcessRecord>) {
// populate the node by finding its children... recursively
let pid = node.record.pid; // avoid binding node as immutable in closure
node.children.extend(
records.iter().filter_map(|record| {
if record.ppid == pid {
let mut child = ProcessTreeNode::new(record);
populate_node(&mut child, records);
Some(child)
} else {
None
}
})
)
}
fn build_process_tree() -> ProcessTree {
let records = get_process_records();
let mut tree = ProcessTree {
root : ProcessTreeNode::new(
&ProcessRecord {
name: "/".to_string(),
pid: 0,
ppid: -1
})
};
// recursively populate all nodes in the tree starting from root (pid 0)
{
let root = &mut tree.root;
populate_node(root, &records);
}
tree
}
fn print_node(node : &ProcessTreeNode, indent_level : i32) {
// print indentation
for _ in (0..indent_level) {
print!(" ");
}
println!("- {} #{}", node.record.name, node.record.pid);
for child in node.children.iter() {
print_node(child, indent_level + 1); // recurse
}
}
fn main() {
let ptree = build_process_tree();
print_node(&(ptree.root), 0)
}