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
#![cfg(target_os="linux")]
//! Bindings to kernel/sys.c

//First, let's use linux-api stuff
extern crate linux_api;
use linux_api::{pid_t, uid_t, gid_t};

//Okay, let's do this!
extern "C" {

    ///Returns the thread group ID of the current process
    pub fn getpid() -> pid_t;
    
    ///Returns the parent process's thread group ID
    pub fn getppid() -> pid_t;
    
    ///Returns the current user's ID
    pub fn getuid() -> uid_t;
    
    ///Returns the current effective user's ID
    pub fn geteuid() -> uid_t;
    
    ///Returns the current user's (primary?) group ID
    pub fn getgid() -> gid_t;
    
    ///Returns the current effective user's (primary?) group ID
    pub fn getegid() -> gid_t;
}

#[cfg(test)]
mod test {

    use super::*;

    #[test]
    fn ensure_not_on_process_zero() {
        unsafe { 
            assert!(getpid() >= 0); 
            assert!(getppid() >= 0); 
        }
    }
    
    #[test]
    fn ensure_group_same() {
        unsafe {
            assert_eq!(getgid(), getegid());
        }
    }
    
    #[test]
    fn ensure_user_same() {
        unsafe {
            assert_eq!(getuid(), geteuid());
        }
    }

}