Debian Codegraph @

.cargo/vendor/rustix/src/backend/libc/system/syscalls.rs

raw
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 use core::mem::MaybeUninit;
10 #[cfg(linux_kernel)]
11 use {
12 crate::backend::conv::c_str, crate::fd::BorrowedFd, crate::ffi::CStr, crate::system::Sysinfo,
13 };
14 #[cfg(not(any(
15 target_os = "emscripten",
16 target_os = "espidf",
17 target_os = "redox",
18 target_os = "vita",
19 target_os = "wasi"
20 )))]
21 use {crate::backend::conv::ret, crate::io};
22
23 #[cfg(not(target_os = "wasi"))]
24 #[inline]
25 pub(crate) fn uname() -> RawUname {
26 let mut uname = MaybeUninit::<RawUname>::uninit();
27 unsafe {
28 let r = c::uname(uname.as_mut_ptr());
29
30 // On POSIX, `uname` is documented to return non-negative on success
31 // instead of the usual 0, though some specific systems do document
32 // that they always use zero allowing us to skip this check.
33 #[cfg(not(any(apple, freebsdlike, linux_like, target_os = "netbsd")))]
34 let r = core::cmp::min(r, 0);
35
36 ret_infallible(r);
37 uname.assume_init()
38 }
39 }
40
41 #[cfg(linux_kernel)]
42 pub(crate) fn sysinfo() -> Sysinfo {
43 let mut info = MaybeUninit::<Sysinfo>::uninit();
44 unsafe {
45 ret_infallible(c::sysinfo(info.as_mut_ptr()));
46 info.assume_init()
47 }
48 }
49
50 #[cfg(not(any(
51 target_os = "emscripten",
52 target_os = "espidf",
53 target_os = "redox",
54 target_os = "vita",
55 target_os = "wasi"
56 )))]
57 pub(crate) fn sethostname(name: &[u8]) -> io::Result<()> {
58 unsafe {
59 ret(c::sethostname(
60 name.as_ptr().cast(),
61 name.len().try_into().map_err(|_| io::Errno::INVAL)?,
62 ))
63 }
64 }
65
66 #[cfg(target_os = "linux")]
67 pub(crate) fn reboot(cmd: RebootCommand) -> io::Result<()> {
68 unsafe { ret(c::reboot(cmd as i32)) }
69 }
70
71 #[cfg(linux_kernel)]
72 #[inline]
73 pub(crate) fn init_module(image: &[u8], param_values: &CStr) -> io::Result<()> {
74 syscall! {
75 fn init_module(
76 module_image: *const c::c_void,
77 len: c::c_ulong,
78 param_values: *const c::c_char
79 ) via SYS_init_module -> c::c_int
80 }
81
82 unsafe {
83 ret(init_module(
84 image.as_ptr().cast(),
85 image.len() as _,
86 c_str(param_values),
87 ))
88 }
89 }
90
91 #[cfg(linux_kernel)]
92 #[inline]
93 pub(crate) fn finit_module(
94 fd: BorrowedFd<'_>,
95 param_values: &CStr,
96 flags: c::c_int,
97 ) -> io::Result<()> {
98 use crate::fd::AsRawFd;
99
100 syscall! {
101 fn finit_module(
102 fd: c::c_int,
103 param_values: *const c::c_char,
104 flags: c::c_int
105 ) via SYS_finit_module -> c::c_int
106 }
107
108 unsafe { ret(finit_module(fd.as_raw_fd(), c_str(param_values), flags)) }
109 }
110
111 #[cfg(linux_kernel)]
112 #[inline]
113 pub(crate) fn delete_module(name: &CStr, flags: c::c_int) -> io::Result<()> {
114 syscall! {
115 fn delete_module(
116 name: *const c::c_char,
117 flags: c::c_int
118 ) via SYS_delete_module -> c::c_int
119 }
120 unsafe { ret(delete_module(c_str(name), flags)) }
121 }