use std::io;
pub fn get_parent_pid() -> io::Result<u32> {
let ppid = unsafe { libc::getppid() };
if ppid < 0 {
Err(io::Error::last_os_error())
} else {
Ok(ppid as u32)
}
}
pub fn get_user_id() -> io::Result<u32> {
let uid = unsafe { libc::getuid() };
Ok(uid)
}
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();
assert!(uid < u32::MAX, "UID should be valid");
}
#[test]
fn test_is_tty() {
let _ = is_tty(0);
let _ = is_tty(1);
let _ = is_tty(2);
}
}