|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+use procinfo::pid;
|
|
|
2
|
+
|
|
|
3
|
+/// Datenstruktur für einen Prozess.
|
|
|
4
|
+pub struct Process {
|
|
|
5
|
+ name: String,
|
|
|
6
|
+ pid: u32,
|
|
|
7
|
+ ppid: u32,
|
|
|
8
|
+}
|
|
|
9
|
+
|
|
|
10
|
+impl Process {
|
|
|
11
|
+ /// Erstellt eine Prozess-Datenstruktur aus procinfo::Stat.
|
|
|
12
|
+ pub fn new(with_pid: u32) -> Self {
|
|
|
13
|
+ if let Ok(stat) = pid::stat(with_pid) {
|
|
|
14
|
+ Process {
|
|
|
15
|
+ name: stat.command,
|
|
|
16
|
+ pid: stat.pid,
|
|
|
17
|
+ ppid: stat.ppid,
|
|
|
18
|
+ }
|
|
|
19
|
+ } else {
|
|
|
20
|
+ panic!("Internal Error: Process not found")
|
|
|
21
|
+ }
|
|
|
22
|
+ }
|
|
|
23
|
+
|
|
|
24
|
+ /// Erstellt eine Prozess-Datenstruktur aus procinfo::Stat.
|
|
|
25
|
+ pub fn me() -> Self {
|
|
|
26
|
+ if let Ok(my_pid) = pid::stat_self() {
|
|
|
27
|
+ Process::new(my_pid.pid)
|
|
|
28
|
+ } else {
|
|
|
29
|
+ panic!("Internal Error: I don't have a PID but I am running.")
|
|
|
30
|
+ }
|
|
|
31
|
+ }
|
|
|
32
|
+
|
|
|
33
|
+ /// Prüft ob das Prozess-Struct ein Elternprozess besitzt.
|
|
|
34
|
+ pub fn has_parent(&self) -> bool {
|
|
|
35
|
+ self.ppid != 0
|
|
|
36
|
+ }
|
|
|
37
|
+
|
|
|
38
|
+ /// Gibt den Elternprozess zurück.
|
|
|
39
|
+ pub fn parent(&self) -> Self {
|
|
|
40
|
+ Process::new(self.ppid)
|
|
|
41
|
+ }
|
|
|
42
|
+
|
|
|
43
|
+ /// Prüft ob das Prozess-Struct einen (entfernten) Elternprozess mit dem übergebenen pid hat.
|
|
|
44
|
+ pub fn has_parent_with_pid(&self, pid: u32) -> bool {
|
|
|
45
|
+ if self.pid == pid {
|
|
|
46
|
+ return true;
|
|
|
47
|
+ }
|
|
|
48
|
+
|
|
|
49
|
+ if self.has_parent() {
|
|
|
50
|
+ return self.parent().has_parent_with_pid(pid);
|
|
|
51
|
+ }
|
|
|
52
|
+
|
|
|
53
|
+ false
|
|
|
54
|
+ }
|
|
|
55
|
+
|
|
|
56
|
+ /// Gibt über Rekursion über die Eltern eine Prozesskette aus.
|
|
|
57
|
+ pub fn print_recursive(&self, to_pid: u32, output: &mut String) {
|
|
|
58
|
+
|
|
|
59
|
+ if output.len() == 0 {
|
|
|
60
|
+ *output = format!("{}({}){}", self.name, self.pid, output);
|
|
|
61
|
+ } else {
|
|
|
62
|
+ *output = format!("{}({})---{}", self.name, self.pid, output);
|
|
|
63
|
+ }
|
|
|
64
|
+
|
|
|
65
|
+ if self.has_parent() && self.pid != to_pid {
|
|
|
66
|
+ self.parent().print_recursive(to_pid, output);
|
|
|
67
|
+ }
|
|
|
68
|
+ }
|
|
|
69
|
+}
|
|
|
70
|
+
|
|
|
71
|
+/// Geht von eigenem Prozess aus und gibt die Prozesskette bis zum übergebenem PID aus
|
|
|
72
|
+/// und fängt mögliche Fehler ab.
|
|
|
73
|
+pub fn print(pid: u32) -> bool {
|
|
|
74
|
+
|
|
|
75
|
+ if let Err(_) = pid::stat(pid) {
|
|
|
76
|
+ println!("Invalid PID");
|
|
|
77
|
+ return false;
|
|
|
78
|
+ }
|
|
|
79
|
+
|
|
|
80
|
+ let my_proc = Process::me();
|
|
|
81
|
+
|
|
|
82
|
+ if !my_proc.has_parent_with_pid(pid) {
|
|
|
83
|
+ println!("This Process has no parent {}", pid);
|
|
|
84
|
+ return false;
|
|
|
85
|
+ }
|
|
|
86
|
+
|
|
|
87
|
+ let mut output = String::new();
|
|
|
88
|
+ my_proc.print_recursive(pid, &mut output);
|
|
|
89
|
+ println!("{}", output);
|
|
|
90
|
+
|
|
|
91
|
+ true
|
|
|
92
|
+}
|