Debian Codegraph @

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

raw
1 //! libc syscalls supporting `rustix::thread`.
2
3 use crate::backend::c;
4 use crate::backend::conv::ret;
5 use crate::io;
6 #[cfg(not(target_os = "redox"))]
7 use crate::thread::{NanosleepRelativeResult, Timespec};
8 #[cfg(all(target_env = "gnu", fix_y2038))]
9 use crate::timespec::LibcTimespec;
10 use core::mem::MaybeUninit;
11 #[cfg(linux_kernel)]
12 use {
13 crate::backend::conv::{borrowed_fd, ret_c_int, ret_usize},
14 crate::fd::BorrowedFd,
15 crate::pid::Pid,
16 crate::thread::{FutexFlags, FutexOperation},
17 crate::utils::as_mut_ptr,
18 };
19 #[cfg(not(any(
20 apple,
21 freebsdlike,
22 target_os = "emscripten",
23 target_os = "espidf",
24 target_os = "haiku",
25 target_os = "openbsd",
26 target_os = "redox",
27 target_os = "vita",
28 target_os = "wasi",
29 )))]
30 use {crate::thread::ClockId, core::ptr::null_mut};
31
32 #[cfg(all(target_env = "gnu", fix_y2038))]
33 weak!(fn __clock_nanosleep_time64(c::clockid_t, c::c_int, *const LibcTimespec, *mut LibcTimespec) -> c::c_int);
34 #[cfg(all(target_env = "gnu", fix_y2038))]
35 weak!(fn __nanosleep64(*const LibcTimespec, *mut LibcTimespec) -> c::c_int);
36
37 #[cfg(not(any(
38 apple,
39 target_os = "dragonfly",
40 target_os = "emscripten",
41 target_os = "espidf",
42 target_os = "freebsd", // FreeBSD 12 has clock_nanosleep, but libc targets FreeBSD 11.
43 target_os = "haiku",
44 target_os = "openbsd",
45 target_os = "redox",
46 target_os = "vita",
47 target_os = "wasi",
48 )))]
49 #[inline]
50 pub(crate) fn clock_nanosleep_relative(id: ClockId, request: &Timespec) -> NanosleepRelativeResult {
51 // Old 32-bit version: libc has `clock_nanosleep` but it is not y2038 safe
52 // by default. But there may be a `__clock_nanosleep_time64` we can use.
53 #[cfg(fix_y2038)]
54 {
55 #[cfg(target_env = "gnu")]
56 if let Some(libc_clock_nanosleep) = __clock_nanosleep_time64.get() {
57 let flags = 0;
58 let mut remain = MaybeUninit::<LibcTimespec>::uninit();
59
60 unsafe {
61 return match libc_clock_nanosleep(
62 id as c::clockid_t,
63 flags,
64 &request.clone().into(),
65 remain.as_mut_ptr(),
66 ) {
67 0 => NanosleepRelativeResult::Ok,
68 err if err == io::Errno::INTR.0 => {
69 NanosleepRelativeResult::Interrupted(remain.assume_init().into())
70 }
71 err => NanosleepRelativeResult::Err(io::Errno(err)),
72 };
73 }
74 }
75
76 clock_nanosleep_relative_old(id, request)
77 }
78
79 // Main version: libc is y2038 safe and has `clock_nanosleep`.
80 #[cfg(not(fix_y2038))]
81 unsafe {
82 let flags = 0;
83 let mut remain = MaybeUninit::<Timespec>::uninit();
84
85 match c::clock_nanosleep(id as c::clockid_t, flags, request, remain.as_mut_ptr()) {
86 0 => NanosleepRelativeResult::Ok,
87 err if err == io::Errno::INTR.0 => {
88 NanosleepRelativeResult::Interrupted(remain.assume_init())
89 }
90 err => NanosleepRelativeResult::Err(io::Errno(err)),
91 }
92 }
93 }
94
95 #[cfg(all(
96 fix_y2038,
97 not(any(
98 apple,
99 target_os = "emscripten",
100 target_os = "haiku",
101 target_os = "vita"
102 ))
103 ))]
104 fn clock_nanosleep_relative_old(
105 id: crate::clockid::ClockId,
106 request: &Timespec,
107 ) -> NanosleepRelativeResult {
108 let tv_sec = match request.tv_sec.try_into() {
109 Ok(tv_sec) => tv_sec,
110 Err(_) => return NanosleepRelativeResult::Err(io::Errno::OVERFLOW),
111 };
112 let tv_nsec = match request.tv_nsec.try_into() {
113 Ok(tv_nsec) => tv_nsec,
114 Err(_) => return NanosleepRelativeResult::Err(io::Errno::INVAL),
115 };
116 let old_request = c::timespec { tv_sec, tv_nsec };
117 let mut old_remain = MaybeUninit::<c::timespec>::uninit();
118 let flags = 0;
119
120 unsafe {
121 match c::clock_nanosleep(
122 id as c::clockid_t,
123 flags,
124 &old_request,
125 old_remain.as_mut_ptr(),
126 ) {
127 0 => NanosleepRelativeResult::Ok,
128 err if err == io::Errno::INTR.0 => {
129 let old_remain = old_remain.assume_init();
130 let remain = Timespec {
131 tv_sec: old_remain.tv_sec.into(),
132 tv_nsec: old_remain.tv_nsec.into(),
133 };
134 NanosleepRelativeResult::Interrupted(remain)
135 }
136 err => NanosleepRelativeResult::Err(io::Errno(err)),
137 }
138 }
139 }
140
141 #[cfg(not(any(
142 apple,
143 target_os = "dragonfly",
144 target_os = "emscripten",
145 target_os = "espidf",
146 target_os = "freebsd", // FreeBSD 12 has clock_nanosleep, but libc targets FreeBSD 11.
147 target_os = "haiku",
148 target_os = "openbsd",
149 target_os = "redox",
150 target_os = "vita",
151 target_os = "wasi",
152 )))]
153 #[inline]
154 pub(crate) fn clock_nanosleep_absolute(id: ClockId, request: &Timespec) -> io::Result<()> {
155 // Old 32-bit version: libc has `clock_nanosleep` but it is not y2038 safe
156 // by default. But there may be a `__clock_nanosleep_time64` we can use.
157 #[cfg(fix_y2038)]
158 {
159 #[cfg(target_env = "gnu")]
160 if let Some(libc_clock_nanosleep) = __clock_nanosleep_time64.get() {
161 let flags = c::TIMER_ABSTIME;
162 unsafe {
163 return match {
164 libc_clock_nanosleep(
165 id as c::clockid_t,
166 flags,
167 &request.clone().into(),
168 null_mut(),
169 )
170 } {
171 0 => Ok(()),
172 err => Err(io::Errno(err)),
173 };
174 }
175 }
176
177 clock_nanosleep_absolute_old(id, request)
178 }
179
180 // Main version: libc is y2038 safe and has `clock_nanosleep`.
181 #[cfg(not(fix_y2038))]
182 {
183 let flags = c::TIMER_ABSTIME;
184
185 match unsafe { c::clock_nanosleep(id as c::clockid_t, flags as _, request, null_mut()) } {
186 0 => Ok(()),
187 err => Err(io::Errno(err)),
188 }
189 }
190 }
191
192 #[cfg(all(
193 fix_y2038,
194 not(any(
195 apple,
196 target_os = "emscripten",
197 target_os = "haiku",
198 target_os = "vita"
199 ))
200 ))]
201 fn clock_nanosleep_absolute_old(id: crate::clockid::ClockId, request: &Timespec) -> io::Result<()> {
202 let flags = c::TIMER_ABSTIME;
203
204 let old_request = c::timespec {
205 tv_sec: request.tv_sec.try_into().map_err(|_| io::Errno::OVERFLOW)?,
206 tv_nsec: request.tv_nsec.try_into().map_err(|_| io::Errno::INVAL)?,
207 };
208 match unsafe {
209 c::clock_nanosleep(
210 id as c::clockid_t,
211 flags,
212 &old_request,
213 core::ptr::null_mut(),
214 )
215 } {
216 0 => Ok(()),
217 err => Err(io::Errno(err)),
218 }
219 }
220
221 #[cfg(not(target_os = "redox"))]
222 #[inline]
223 pub(crate) fn nanosleep(request: &Timespec) -> NanosleepRelativeResult {
224 // Old 32-bit version: libc has `nanosleep` but it is not y2038 safe by
225 // default. But there may be a `__nanosleep64` we can use.
226 #[cfg(fix_y2038)]
227 {
228 #[cfg(target_env = "gnu")]
229 if let Some(libc_nanosleep) = __nanosleep64.get() {
230 let mut remain = MaybeUninit::<LibcTimespec>::uninit();
231 unsafe {
232 return match ret(libc_nanosleep(&request.clone().into(), remain.as_mut_ptr())) {
233 Ok(()) => NanosleepRelativeResult::Ok,
234 Err(io::Errno::INTR) => {
235 NanosleepRelativeResult::Interrupted(remain.assume_init().into())
236 }
237 Err(err) => NanosleepRelativeResult::Err(err),
238 };
239 }
240 }
241
242 nanosleep_old(request)
243 }
244
245 // Main version: libc is y2038 safe and has `nanosleep`.
246 #[cfg(not(fix_y2038))]
247 unsafe {
248 let mut remain = MaybeUninit::<Timespec>::uninit();
249
250 match ret(c::nanosleep(request, remain.as_mut_ptr())) {
251 Ok(()) => NanosleepRelativeResult::Ok,
252 Err(io::Errno::INTR) => NanosleepRelativeResult::Interrupted(remain.assume_init()),
253 Err(err) => NanosleepRelativeResult::Err(err),
254 }
255 }
256 }
257
258 #[cfg(fix_y2038)]
259 fn nanosleep_old(request: &Timespec) -> NanosleepRelativeResult {
260 let tv_sec = match request.tv_sec.try_into() {
261 Ok(tv_sec) => tv_sec,
262 Err(_) => return NanosleepRelativeResult::Err(io::Errno::OVERFLOW),
263 };
264 let tv_nsec = match request.tv_nsec.try_into() {
265 Ok(tv_nsec) => tv_nsec,
266 Err(_) => return NanosleepRelativeResult::Err(io::Errno::INVAL),
267 };
268 let old_request = c::timespec { tv_sec, tv_nsec };
269 let mut old_remain = MaybeUninit::<c::timespec>::uninit();
270
271 unsafe {
272 match ret(c::nanosleep(&old_request, old_remain.as_mut_ptr())) {
273 Ok(()) => NanosleepRelativeResult::Ok,
274 Err(io::Errno::INTR) => {
275 let old_remain = old_remain.assume_init();
276 let remain = Timespec {
277 tv_sec: old_remain.tv_sec.into(),
278 tv_nsec: old_remain.tv_nsec.into(),
279 };
280 NanosleepRelativeResult::Interrupted(remain)
281 }
282 Err(err) => NanosleepRelativeResult::Err(err),
283 }
284 }
285 }
286
287 #[cfg(linux_kernel)]
288 #[inline]
289 #[must_use]
290 pub(crate) fn gettid() -> Pid {
291 // `gettid` wasn't supported in glibc until 2.30, and musl until 1.2.2,
292 // so use `syscall`.
293 // <https://sourceware.org/bugzilla/show_bug.cgi?id=6399#c62>
294 weak_or_syscall! {
295 fn gettid() via SYS_gettid -> c::pid_t
296 }
297
298 unsafe {
299 let tid = gettid();
300 Pid::from_raw_unchecked(tid)
301 }
302 }
303
304 #[cfg(linux_kernel)]
305 #[inline]
306 pub(crate) fn setns(fd: BorrowedFd<'_>, nstype: c::c_int) -> io::Result<c::c_int> {
307 // `setns` wasn't supported in glibc until 2.14, and musl until 0.9.5,
308 // so use `syscall`.
309 weak_or_syscall! {
310 fn setns(fd: c::c_int, nstype: c::c_int) via SYS_setns -> c::c_int
311 }
312
313 unsafe { ret_c_int(setns(borrowed_fd(fd), nstype)) }
314 }
315
316 #[cfg(linux_kernel)]
317 #[inline]
318 pub(crate) fn unshare(flags: crate::thread::UnshareFlags) -> io::Result<()> {
319 unsafe { ret(c::unshare(flags.bits() as i32)) }
320 }
321
322 #[cfg(linux_kernel)]
323 #[inline]
324 pub(crate) fn capget(
325 header: &mut linux_raw_sys::general::__user_cap_header_struct,
326 data: &mut [MaybeUninit<linux_raw_sys::general::__user_cap_data_struct>],
327 ) -> io::Result<()> {
328 syscall! {
329 fn capget(
330 hdrp: *mut linux_raw_sys::general::__user_cap_header_struct,
331 data: *mut linux_raw_sys::general::__user_cap_data_struct
332 ) via SYS_capget -> c::c_int
333 }
334
335 unsafe {
336 ret(capget(
337 as_mut_ptr(header),
338 data.as_mut_ptr()
339 .cast::<linux_raw_sys::general::__user_cap_data_struct>(),
340 ))
341 }
342 }
343
344 #[cfg(linux_kernel)]
345 #[inline]
346 pub(crate) fn capset(
347 header: &mut linux_raw_sys::general::__user_cap_header_struct,
348 data: &[linux_raw_sys::general::__user_cap_data_struct],
349 ) -> io::Result<()> {
350 syscall! {
351 fn capset(
352 hdrp: *mut linux_raw_sys::general::__user_cap_header_struct,
353 data: *const linux_raw_sys::general::__user_cap_data_struct
354 ) via SYS_capset -> c::c_int
355 }
356
357 unsafe { ret(capset(as_mut_ptr(header), data.as_ptr())) }
358 }
359
360 #[cfg(linux_kernel)]
361 #[inline]
362 pub(crate) fn setuid_thread(uid: crate::ugid::Uid) -> io::Result<()> {
363 syscall! {
364 fn setuid(uid: c::uid_t) via SYS_setuid -> c::c_int
365 }
366
367 unsafe { ret(setuid(uid.as_raw())) }
368 }
369
370 #[cfg(linux_kernel)]
371 #[inline]
372 pub(crate) fn setresuid_thread(
373 ruid: crate::ugid::Uid,
374 euid: crate::ugid::Uid,
375 suid: crate::ugid::Uid,
376 ) -> io::Result<()> {
377 #[cfg(any(target_arch = "x86", target_arch = "arm", target_arch = "sparc"))]
378 const SYS: c::c_long = c::SYS_setresuid32 as c::c_long;
379 #[cfg(not(any(target_arch = "x86", target_arch = "arm", target_arch = "sparc")))]
380 const SYS: c::c_long = c::SYS_setresuid as c::c_long;
381
382 syscall! {
383 fn setresuid(ruid: c::uid_t, euid: c::uid_t, suid: c::uid_t) via SYS -> c::c_int
384 }
385
386 unsafe { ret(setresuid(ruid.as_raw(), euid.as_raw(), suid.as_raw())) }
387 }
388
389 #[cfg(linux_kernel)]
390 #[inline]
391 pub(crate) fn setgid_thread(gid: crate::ugid::Gid) -> io::Result<()> {
392 syscall! {
393 fn setgid(gid: c::gid_t) via SYS_setgid -> c::c_int
394 }
395
396 unsafe { ret(setgid(gid.as_raw())) }
397 }
398
399 #[cfg(linux_kernel)]
400 #[inline]
401 pub(crate) fn setresgid_thread(
402 rgid: crate::ugid::Gid,
403 egid: crate::ugid::Gid,
404 sgid: crate::ugid::Gid,
405 ) -> io::Result<()> {
406 #[cfg(any(target_arch = "x86", target_arch = "arm", target_arch = "sparc"))]
407 const SYS: c::c_long = c::SYS_setresgid32 as c::c_long;
408 #[cfg(not(any(target_arch = "x86", target_arch = "arm", target_arch = "sparc")))]
409 const SYS: c::c_long = c::SYS_setresgid as c::c_long;
410
411 syscall! {
412 fn setresgid(rgid: c::gid_t, egid: c::gid_t, sgid: c::gid_t) via SYS -> c::c_int
413 }
414
415 unsafe { ret(setresgid(rgid.as_raw(), egid.as_raw(), sgid.as_raw())) }
416 }
417
418 // TODO: This could be de-multiplexed.
419 #[cfg(linux_kernel)]
420 pub(crate) unsafe fn futex(
421 uaddr: *mut u32,
422 op: FutexOperation,
423 flags: FutexFlags,
424 val: u32,
425 utime: *const Timespec,
426 uaddr2: *mut u32,
427 val3: u32,
428 ) -> io::Result<usize> {
429 #[cfg(all(
430 target_pointer_width = "32",
431 not(any(target_arch = "aarch64", target_arch = "x86_64"))
432 ))]
433 {
434 // TODO: Upstream this to the libc crate.
435 #[allow(non_upper_case_globals)]
436 const SYS_futex_time64: i32 = linux_raw_sys::general::__NR_futex_time64 as i32;
437
438 syscall! {
439 fn futex_time64(
440 uaddr: *mut u32,
441 futex_op: c::c_int,
442 val: u32,
443 timeout: *const Timespec,
444 uaddr2: *mut u32,
445 val3: u32
446 ) via SYS_futex_time64 -> c::ssize_t
447 }
448
449 ret_usize(futex_time64(
450 uaddr,
451 op as i32 | flags.bits() as i32,
452 val,
453 utime,
454 uaddr2,
455 val3,
456 ))
457 .or_else(|err| {
458 // See the comments in `rustix_clock_gettime_via_syscall` about
459 // emulation.
460 if err == io::Errno::NOSYS {
461 futex_old(uaddr, op, flags, val, utime, uaddr2, val3)
462 } else {
463 Err(err)
464 }
465 })
466 }
467
468 #[cfg(any(
469 target_pointer_width = "64",
470 target_arch = "aarch64",
471 target_arch = "x86_64"
472 ))]
473 {
474 syscall! {
475 fn futex(
476 uaddr: *mut u32,
477 futex_op: c::c_int,
478 val: u32,
479 timeout: *const linux_raw_sys::general::__kernel_timespec,
480 uaddr2: *mut u32,
481 val3: u32
482 ) via SYS_futex -> c::c_long
483 }
484
485 ret_usize(futex(
486 uaddr,
487 op as i32 | flags.bits() as i32,
488 val,
489 utime.cast(),
490 uaddr2,
491 val3,
492 ) as isize)
493 }
494 }
495
496 #[cfg(linux_kernel)]
497 #[cfg(all(
498 target_pointer_width = "32",
499 not(any(target_arch = "aarch64", target_arch = "x86_64"))
500 ))]
501 unsafe fn futex_old(
502 uaddr: *mut u32,
503 op: FutexOperation,
504 flags: FutexFlags,
505 val: u32,
506 utime: *const Timespec,
507 uaddr2: *mut u32,
508 val3: u32,
509 ) -> io::Result<usize> {
510 syscall! {
511 fn futex(
512 uaddr: *mut u32,
513 futex_op: c::c_int,
514 val: u32,
515 timeout: *const linux_raw_sys::general::__kernel_old_timespec,
516 uaddr2: *mut u32,
517 val3: u32
518 ) via SYS_futex -> c::c_long
519 }
520
521 let old_utime = linux_raw_sys::general::__kernel_old_timespec {
522 tv_sec: (*utime).tv_sec.try_into().map_err(|_| io::Errno::INVAL)?,
523 tv_nsec: (*utime).tv_nsec.try_into().map_err(|_| io::Errno::INVAL)?,
524 };
525 ret_usize(futex(
526 uaddr,
527 op as i32 | flags.bits() as i32,
528 val,
529 &old_utime,
530 uaddr2,
531 val3,
532 ) as isize)
533 }
534
535 #[cfg(linux_kernel)]
536 #[inline]
537 pub(crate) fn setgroups_thread(groups: &[crate::ugid::Gid]) -> io::Result<()> {
538 syscall! {
539 fn setgroups(size: c::size_t, list: *const c::gid_t) via SYS_setgroups -> c::c_int
540 }
541 ret(unsafe { setgroups(groups.len(), groups.as_ptr().cast()) })
542 }