1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use std::io::ErrorKind;
use std::path::Path;
use crate::scanner_rust::generic_array::typenum::U192;
use crate::scanner_rust::{ScannerAscii, ScannerError};
#[derive(Default, Debug, Clone)]
pub struct ProcessStatus {
pub real_uid: u32,
pub real_gid: u32,
pub effective_uid: u32,
pub effective_gid: u32,
pub saved_set_uid: u32,
pub saved_set_gid: u32,
pub fs_uid: u32,
pub fs_gid: u32,
}
pub fn get_process_status(pid: u32) -> Result<ProcessStatus, ScannerError> {
let mut status = ProcessStatus::default();
let status_path = Path::new("/proc").join(pid.to_string()).join("status");
let mut sc: ScannerAscii<_, U192> = ScannerAscii::scan_path2(status_path)?;
loop {
let label = sc.next_raw()?.ok_or(ErrorKind::UnexpectedEof)?;
if label.starts_with(b"Uid") {
status.real_uid = sc.next_u32()?.ok_or(ErrorKind::UnexpectedEof)?;
status.effective_uid = sc.next_u32()?.ok_or(ErrorKind::UnexpectedEof)?;
status.saved_set_uid = sc.next_u32()?.ok_or(ErrorKind::UnexpectedEof)?;
status.fs_uid = sc.next_u32()?.ok_or(ErrorKind::UnexpectedEof)?;
break;
} else {
sc.drop_next_line()?.ok_or(ErrorKind::UnexpectedEof)?;
}
}
loop {
let label = sc.next_raw()?.ok_or(ErrorKind::UnexpectedEof)?;
if label.starts_with(b"Gid") {
status.real_gid = sc.next_u32()?.ok_or(ErrorKind::UnexpectedEof)?;
status.effective_gid = sc.next_u32()?.ok_or(ErrorKind::UnexpectedEof)?;
status.saved_set_gid = sc.next_u32()?.ok_or(ErrorKind::UnexpectedEof)?;
status.fs_gid = sc.next_u32()?.ok_or(ErrorKind::UnexpectedEof)?;
break;
} else {
sc.drop_next_line()?.ok_or(ErrorKind::UnexpectedEof)?;
}
}
Ok(status)
}