1 //! libc syscalls supporting `rustix::process`.
2 
3 use super::types::RawUname;
4 use crate::backend::c;
5 #[cfg(not(target_os = "wasi"))]
6 use crate::backend::conv::ret_infallible;
7 #[cfg(target_os = "linux")]
8 use crate::system::RebootCommand;
9 #[cfg(linux_kernel)]
10 use crate::system::Sysinfo;
11 use core::mem::MaybeUninit;
12 #[cfg(not(any(
13     target_os = "emscripten",
14     target_os = "espidf",
15     target_os = "redox",
16     target_os = "vita",
17     target_os = "wasi"
18 )))]
19 use {crate::backend::conv::ret, crate::io};
20 
21 #[cfg(not(target_os = "wasi"))]
22 #[inline]
uname() -> RawUname23 pub(crate) fn uname() -> RawUname {
24     let mut uname = MaybeUninit::<RawUname>::uninit();
25     unsafe {
26         let r = c::uname(uname.as_mut_ptr());
27 
28         // On POSIX, `uname` is documented to return non-negative on success
29         // instead of the usual 0, though some specific systems do document
30         // that they always use zero allowing us to skip this check.
31         #[cfg(not(any(apple, freebsdlike, linux_like, target_os = "netbsd")))]
32         let r = core::cmp::min(r, 0);
33 
34         ret_infallible(r);
35         uname.assume_init()
36     }
37 }
38 
39 #[cfg(linux_kernel)]
sysinfo() -> Sysinfo40 pub(crate) fn sysinfo() -> Sysinfo {
41     let mut info = MaybeUninit::<Sysinfo>::uninit();
42     unsafe {
43         ret_infallible(c::sysinfo(info.as_mut_ptr()));
44         info.assume_init()
45     }
46 }
47 
48 #[cfg(not(any(
49     target_os = "emscripten",
50     target_os = "espidf",
51     target_os = "redox",
52     target_os = "vita",
53     target_os = "wasi"
54 )))]
sethostname(name: &[u8]) -> io::Result<()>55 pub(crate) fn sethostname(name: &[u8]) -> io::Result<()> {
56     unsafe {
57         ret(c::sethostname(
58             name.as_ptr().cast(),
59             name.len().try_into().map_err(|_| io::Errno::INVAL)?,
60         ))
61     }
62 }
63 
64 #[cfg(target_os = "linux")]
reboot(cmd: RebootCommand) -> io::Result<()>65 pub(crate) fn reboot(cmd: RebootCommand) -> io::Result<()> {
66     unsafe { ret(c::reboot(cmd as i32)) }
67 }
68