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
use std::io;
/// Safe wrapper for `getppid()` - returns parent process `ID`
pub fn get_parent_pid() -> io::Result<u32> {
let ppid = unsafe { libc::getppid() };
if ppid < 0 {
Err(io::Error::last_os_error())
} else {
// Safe: we've verified ppid >= 0
Ok(ppid.try_into().unwrap())
}
}
/// Safe wrapper for `getuid()` - returns user `ID`
pub fn get_user_id() -> io::Result<u32> {
let uid = unsafe { libc::getuid() };
Ok(uid)
}
/// Safe wrapper for `isatty()` - checks if file descriptor is a terminal
#[must_use]
pub fn is_tty(fd: i32) -> bool {
unsafe { libc::isatty(fd) == 1 }
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_get_parent_pid() {
let ppid = get_parent_pid().unwrap();
assert!(ppid > 0, "PPID should be positive");
}
#[test]
fn test_get_user_id() {
let uid = get_user_id().unwrap();
// UID can be 0 (root) or positive
assert!(uid < u32::MAX, "UID should be valid");
}
#[test]
fn test_is_tty() {
// Test with stdin, stdout, stderr
// We can't assert the result since it depends on how tests are run
// But we can ensure it doesn't panic
let _ = is_tty(0);
let _ = is_tty(1);
let _ = is_tty(2);
}
}