|
|
@@ -3,6 +3,7 @@ extern crate libc;
|
|
3
|
3
|
use procinfo::pid;
|
|
4
|
4
|
use self::libc::pid_t;
|
|
5
|
5
|
|
|
|
6
|
+/// Datenstruktur für einen Prozess.
|
|
6
|
7
|
struct Process {
|
|
7
|
8
|
name : String,
|
|
8
|
9
|
pid : pid_t,
|
|
|
@@ -11,6 +12,7 @@ struct Process {
|
|
11
|
12
|
|
|
12
|
13
|
impl Process {
|
|
13
|
14
|
|
|
|
15
|
+ /// Erstellt eine Prozess-Datenstruktur aus procinfo::Stat.
|
|
14
|
16
|
fn new(with_pid:pid_t) -> Self {
|
|
15
|
17
|
if let Ok(stat) = pid::stat(with_pid) {
|
|
16
|
18
|
Process{name: stat.command, pid:stat.pid, ppid:stat.ppid}
|
|
|
@@ -19,14 +21,17 @@ impl Process {
|
|
19
|
21
|
}
|
|
20
|
22
|
}
|
|
21
|
23
|
|
|
|
24
|
+ /// Prüft ob das Prozess-Struct ein Elternprozess besitzt.
|
|
22
|
25
|
fn has_parent(&self) -> bool {
|
|
23
|
26
|
self.ppid != 0
|
|
24
|
27
|
}
|
|
25
|
28
|
|
|
|
29
|
+ /// Gibt den Elternprozess zurück.
|
|
26
|
30
|
fn parent(&self) -> Self {
|
|
27
|
31
|
Process::new(self.ppid)
|
|
28
|
32
|
}
|
|
29
|
33
|
|
|
|
34
|
+ /// Prüft ob das Prozess-Struct einen (entfernten) Elternprozess mit dem übergebenen pid hat.
|
|
30
|
35
|
fn has_parent_with_pid(&self, pid: pid_t) -> bool {
|
|
31
|
36
|
if self.pid == pid {
|
|
32
|
37
|
return true
|
|
|
@@ -39,6 +44,7 @@ impl Process {
|
|
39
|
44
|
false
|
|
40
|
45
|
}
|
|
41
|
46
|
|
|
|
47
|
+ /// Gibt über Rekursion über die Eltern eine Prozesskette aus.
|
|
42
|
48
|
fn print_recursive(&self, to_pid:pid_t, output: &mut String) {
|
|
43
|
49
|
|
|
44
|
50
|
if output.len() == 0 {
|
|
|
@@ -53,6 +59,9 @@ impl Process {
|
|
53
|
59
|
}
|
|
54
|
60
|
}
|
|
55
|
61
|
|
|
|
62
|
+/// Erstellt einen 'Process' aus der übergebenen PID und
|
|
|
63
|
+/// Gibt über Rekursion über die Eltern eine Prozesskette aus.
|
|
|
64
|
+/// Fängt mögliche Fehler ab und
|
|
56
|
65
|
pub fn print(pid:pid_t) -> bool {
|
|
57
|
66
|
|
|
58
|
67
|
if let Err(_) = pid::stat(pid) {
|
|
|
@@ -61,7 +70,6 @@ pub fn print(pid:pid_t) -> bool {
|
|
61
|
70
|
}
|
|
62
|
71
|
|
|
63
|
72
|
if let Ok(my_pid) = pid::stat_self() {
|
|
64
|
|
- let mut output = "".to_string();
|
|
65
|
73
|
let my_proc = Process::new(my_pid.pid);
|
|
66
|
74
|
|
|
67
|
75
|
if !my_proc.has_parent_with_pid(pid) {
|
|
|
@@ -69,6 +77,7 @@ pub fn print(pid:pid_t) -> bool {
|
|
69
|
77
|
return false
|
|
70
|
78
|
}
|
|
71
|
79
|
|
|
|
80
|
+ let mut output = String::new();
|
|
72
|
81
|
my_proc.print_recursive(pid, &mut output);
|
|
73
|
82
|
println!("{}", output);
|
|
74
|
83
|
}
|