summaryrefslogtreecommitdiff
path: root/src/features.rs
blob: 269d88a0ce7db6c9d26a32296b00a6d91e0e15e5 (plain)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
pub use self::os::*;

#[cfg(target_os = "linux")]
mod os {
    use sys::utsname::uname;

    // Features:
    // * atomic cloexec on socket: 2.6.27
    // * pipe2: 2.6.27
    // * accept4: 2.6.28

    static VERS_UNKNOWN: uint = 1;
    static VERS_2_6_18:  uint = 2;
    static VERS_2_6_27:  uint = 3;
    static VERS_2_6_28:  uint = 4;
    static VERS_3:       uint = 5;

    fn parse_kernel_version() -> uint {
        let u = uname();

        #[inline]
        fn digit(dst: &mut uint, b: u8) {
            *dst *= 10;
            *dst += (b - b'0') as uint;
        }

        let mut curr = 0u;
        let mut major = 0;
        let mut minor = 0;
        let mut patch = 0;

        for b in u.release().bytes() {
            if curr >= 3 {
                break;
            }

            match b {
                b'.' | b'-' => {
                    curr += 1;
                }
                b'0'...b'9' => {
                    match curr {
                        0 => digit(&mut major, b),
                        1 => digit(&mut minor, b),
                        _ => digit(&mut patch, b),
                    }
                }
                _ => break,
            }
        }

        if major >= 3 {
            VERS_3
        } else if major >= 2 {
            if minor >= 7 {
                VERS_UNKNOWN
            } else if minor >= 6 {
                if patch >= 28 {
                    VERS_2_6_28
                } else if patch >= 27 {
                    VERS_2_6_27
                } else {
                    VERS_2_6_18
                }
            } else {
                VERS_UNKNOWN
            }
        } else {
            VERS_UNKNOWN
        }
    }

    fn kernel_version() -> uint {
        static mut KERNEL_VERS: uint = 0;

        unsafe {
            if KERNEL_VERS == 0 {
                KERNEL_VERS = parse_kernel_version();
            }

            KERNEL_VERS
        }
    }

    pub fn socket_atomic_cloexec() -> bool {
        kernel_version() >= VERS_2_6_27
    }

    #[test]
    pub fn test_parsing_kernel_version() {
        assert!(kernel_version() > 0);
    }
}

#[cfg(any(target_os = "macos", target_os = "ios"))]
mod os {
    pub fn socket_atomic_cloexec() -> bool {
        false
    }
}