]> git.proxmox.com Git - rustc.git/blame - vendor/rustix/src/backend/libc/system/syscalls.rs
New upstream version 1.75.0+dfsg1
[rustc.git] / vendor / rustix / src / backend / libc / system / syscalls.rs
CommitLineData
fe692bf9
FG
1//! libc syscalls supporting `rustix::process`.
2
3use super::types::RawUname;
4use crate::backend::c;
5#[cfg(not(target_os = "wasi"))]
6use crate::backend::conv::ret_infallible;
ed00b5ec
FG
7#[cfg(target_os = "linux")]
8use crate::system::RebootCommand;
fe692bf9
FG
9#[cfg(linux_kernel)]
10use crate::system::Sysinfo;
11use core::mem::MaybeUninit;
add651ee
FG
12#[cfg(not(any(
13 target_os = "emscripten",
14 target_os = "espidf",
15 target_os = "redox",
ed00b5ec 16 target_os = "vita",
add651ee
FG
17 target_os = "wasi"
18)))]
fe692bf9
FG
19use {crate::backend::conv::ret, crate::io};
20
21#[cfg(not(target_os = "wasi"))]
22#[inline]
23pub(crate) fn uname() -> RawUname {
24 let mut uname = MaybeUninit::<RawUname>::uninit();
25 unsafe {
add651ee
FG
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);
fe692bf9
FG
35 uname.assume_init()
36 }
37}
38
39#[cfg(linux_kernel)]
40pub(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
add651ee
FG
48#[cfg(not(any(
49 target_os = "emscripten",
50 target_os = "espidf",
51 target_os = "redox",
ed00b5ec 52 target_os = "vita",
add651ee
FG
53 target_os = "wasi"
54)))]
fe692bf9
FG
55pub(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}
ed00b5ec
FG
63
64#[cfg(target_os = "linux")]
65pub(crate) fn reboot(cmd: RebootCommand) -> io::Result<()> {
66 unsafe { ret(c::reboot(cmd as i32)) }
67}