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