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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
use crate::*;
use cfg_if::cfg_if;
use std::{convert::TryInto, ffi::CStr, mem::MaybeUninit, ops::Deref, ptr};

cfg_if! {
    if #[cfg(target_os = "linux")] {
        mod linux;
        pub use linux::*;
    }
}

#[man(umask(2))]
pub fn umask(mask: c::mode_t) -> c::mode_t {
    unsafe { c::umask(mask) }
}

#[man(pipe(2))]
pub fn pipe() -> Result<(OwnedFd, OwnedFd)> {
    let mut buf = [0; 2];
    let res = unsafe { c::pipe(buf.as_mut_ptr()) };
    map_err!(res).map(|_| (OwnedFd::new(buf[0]), OwnedFd::new(buf[1])))
}

/// Wrapper for `libc::utsname`
pub struct UtsName {
    buf: c::utsname,
}

impl UtsName {
    /// Returns `self.sysname` as a `CStr`
    pub fn sysname(&self) -> &CStr {
        unsafe { CStr::from_ptr(self.buf.sysname.as_ptr()) }
    }

    /// Returns `self.nodename` as a `CStr`
    pub fn nodename(&self) -> &CStr {
        unsafe { CStr::from_ptr(self.buf.nodename.as_ptr()) }
    }

    /// Returns `self.release` as a `CStr`
    pub fn release(&self) -> &CStr {
        unsafe { CStr::from_ptr(self.buf.release.as_ptr()) }
    }

    /// Returns `self.version` as a `CStr`
    pub fn version(&self) -> &CStr {
        unsafe { CStr::from_ptr(self.buf.version.as_ptr()) }
    }

    /// Returns `self.machine` as a `CStr`
    pub fn machine(&self) -> &CStr {
        unsafe { CStr::from_ptr(self.buf.machine.as_ptr()) }
    }
}

impl Deref for UtsName {
    type Target = c::utsname;

    fn deref(&self) -> &Self::Target {
        &self.buf
    }
}

#[man(uname(2))]
pub fn uname() -> Result<UtsName> {
    let mut uname = MaybeUninit::uninit();
    let res = unsafe { c::uname(uname.as_mut_ptr()) };
    map_err!(res).map(|_| UtsName {
        buf: unsafe { uname.assume_init() },
    })
}

#[man(daemon(3))]
pub fn daemon(nochdir: bool, noclose: bool) -> Result<()> {
    let res = unsafe { c::daemon(nochdir as _, noclose as _) };
    map_err!(res).map(drop)
}

#[man(sethostname(2))]
pub fn sethostname(buf: &[u8]) -> Result<()> {
    let res = unsafe {
        c::sethostname(buf.as_ptr() as *const _, buf.len().try_into().or(einval())?)
    };
    map_err!(res).map(drop)
}

#[man(gethostname(2))]
///
/// This function returns `libc::ENAMETOOLONG` if the hostname does not fit in the supplied
/// buffer. If the hostname is longer than 255 bytes (excluding the nul byte), then this
/// function always returns `libc::ENAMETOOLONG`.
pub fn gethostname<T: Pod + ?Sized>(buf: &mut T) -> Result<&CStr> {
    // Posix implies: If gethostname returns without an error then
    // - if the buffer does not contain a nul byte then the hostname was truncated
    // - if the buffer contains a nul byte in the last place then the hostname was
    //   possibly truncated
    // - otherwise the buffer contains the hostname
    // In either case, the buffer has been fully initialized up to the first nul byte or
    // the end of the buffer depending on what comes first.
    //
    // Therefore strlen on the buffer is defined as long as we manually insert a
    // nul byte at the end of the buffer after gethostname returns
    //
    // SUSv2 guarantees that "Host names are limited to 255 bytes". Presumably this means
    // 255 bytes excluding the terminating nul byte. Therefore we use an
    // internal buffer of size 257 and manually set the last byte to 0 after
    // gethostname returns. If the first nul byte in the buffer is that very byte, then
    // we assume that there was a truncation and return an error.
    unsafe {
        let buf = as_maybe_uninit_bytes_mut2(buf);
        const SIZE: usize = 257;
        let mut inner = MaybeUninit::<[u8; SIZE]>::uninit();
        let res = c::gethostname(inner.as_mut_ptr() as *mut _, SIZE);
        map_err!(res)?;
        *(inner.as_mut_ptr() as *mut u8).add(SIZE - 1) = 0;
        let cstr = CStr::from_ptr(inner.as_ptr() as *mut c::c_char);
        let bytes = cstr.to_bytes_with_nul();
        if bytes.len() < SIZE && bytes.len() <= buf.len() {
            ptr::copy_nonoverlapping(
                bytes.as_ptr(),
                buf.as_mut_ptr() as *mut _,
                bytes.len(),
            );
            Ok(CStr::from_bytes_with_nul_unchecked(
                buf[..bytes.len()].slice_assume_init_ref(),
            ))
        } else {
            Err(Errno(c::ENAMETOOLONG))
        }
    }
}

#[man(sync(2))]
pub fn sync() {
    unsafe { libc::sync() }
}

#[man(sysconf(3))]
pub fn sysconf(name: c::c_int) -> Result<c::c_long> {
    set_errno(0);
    let res = unsafe { c::sysconf(name) };
    map_err!(res)
}