mirror of
https://github.com/posborne/rust-pstree.git
synced 2025-01-28 22:05:02 +01:00
pstree: start to flesh out some basic functionality
Right now, this project is primarily a way to learn rust. If things start coming together, there does seem to be some interest in a library filling the same niche for Rust as psutil does for Python. For now, however, the goal is to write a simple pstree visualization.
This commit is contained in:
commit
76204e72df
3 changed files with 111 additions and 0 deletions
6
.gitignore
vendored
Normal file
6
.gitignore
vendored
Normal file
|
@ -0,0 +1,6 @@
|
|||
# Editor Trash
|
||||
*~
|
||||
\#*\#
|
||||
|
||||
# Binaries
|
||||
pstree
|
21
LICENSE
Normal file
21
LICENSE
Normal file
|
@ -0,0 +1,21 @@
|
|||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2014, Paul Osborne <osbpau@gmail.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
84
pstree.rs
Normal file
84
pstree.rs
Normal file
|
@ -0,0 +1,84 @@
|
|||
// 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.
|
||||
|
||||
use std::io::fs::PathExtensions;
|
||||
use std::io::fs;
|
||||
use std::io::File;
|
||||
use std::io::BufferedReader;
|
||||
use std::collections::hashmap::HashMap;
|
||||
|
||||
fn process_status_file(status_path: &Path) {
|
||||
let mut status_file = BufferedReader::new(File::open(status_path));
|
||||
let mut status_data = HashMap::new();
|
||||
for line in status_file.lines() {
|
||||
let linetext = match line {
|
||||
Err(why) => fail!("{}", why.desc),
|
||||
Ok(l) => l
|
||||
};
|
||||
let parts: Vec<&str> = linetext.as_slice().splitn(2, ':').collect();
|
||||
if parts.len() == 2 {
|
||||
let key = parts[0].trim();
|
||||
let value = parts[1].trim();
|
||||
status_data.insert(key.to_string(), value.to_string());
|
||||
};
|
||||
}
|
||||
|
||||
let name_key = &("Name".to_string());
|
||||
let pid_key = &("Pid".to_string());
|
||||
let ppid_key = &("PPid".to_string());
|
||||
|
||||
if status_data.contains_key(name_key) &&
|
||||
status_data.contains_key(pid_key) &&
|
||||
status_data.contains_key(ppid_key) {
|
||||
println!("{}#{} -> {}",
|
||||
status_data.get(name_key),
|
||||
status_data.get(pid_key),
|
||||
status_data.get(ppid_key));
|
||||
}
|
||||
}
|
||||
|
||||
fn dump_process_info() {
|
||||
let proc_directory = Path::new("/proc");
|
||||
let proc_directory_contents = match fs::readdir(&proc_directory) {
|
||||
Err(why) => fail!("{}", why.desc),
|
||||
Ok(res) => res
|
||||
};
|
||||
for entry in proc_directory_contents.iter() {
|
||||
if entry.is_dir() {
|
||||
let status_path = entry.join("status");
|
||||
if status_path.exists() {
|
||||
process_status_file(&status_path);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
dump_process_info();
|
||||
}
|
Loading…
Reference in a new issue