rustix/backend/libc/thread/
syscalls.rs

1//! libc syscalls supporting `rustix::thread`.
2
3#[cfg(any(freebsdlike, linux_kernel, target_os = "fuchsia"))]
4use super::types::RawCpuSet;
5use crate::backend::c;
6use crate::backend::conv::ret;
7use crate::io;
8#[cfg(any(freebsdlike, linux_kernel, target_os = "fuchsia"))]
9use crate::pid::Pid;
10#[cfg(not(any(
11    apple,
12    freebsdlike,
13    target_os = "emscripten",
14    target_os = "espidf",
15    target_os = "haiku",
16    target_os = "openbsd",
17    target_os = "redox",
18    target_os = "vita",
19    target_os = "wasi",
20)))]
21use crate::thread::ClockId;
22#[cfg(linux_kernel)]
23use crate::thread::{Cpuid, MembarrierCommand, MembarrierQuery};
24#[cfg(not(target_os = "redox"))]
25use crate::thread::{NanosleepRelativeResult, Timespec};
26#[cfg(all(target_env = "gnu", fix_y2038))]
27use crate::timespec::LibcTimespec;
28#[cfg(not(fix_y2038))]
29use crate::timespec::{as_libc_timespec_mut_ptr, as_libc_timespec_ptr};
30#[cfg(linux_kernel)]
31use crate::utils::option_as_ptr;
32use core::mem::MaybeUninit;
33#[cfg(linux_kernel)]
34use core::sync::atomic::AtomicU32;
35#[cfg(linux_kernel)]
36use {
37    crate::backend::conv::{borrowed_fd, ret_c_int, ret_u32, ret_usize},
38    crate::fd::BorrowedFd,
39    crate::thread::futex,
40    crate::utils::as_mut_ptr,
41};
42
43#[cfg(all(target_env = "gnu", fix_y2038))]
44weak!(fn __clock_nanosleep_time64(c::clockid_t, c::c_int, *const LibcTimespec, *mut LibcTimespec) -> c::c_int);
45#[cfg(all(target_env = "gnu", fix_y2038))]
46weak!(fn __nanosleep64(*const LibcTimespec, *mut LibcTimespec) -> c::c_int);
47
48#[cfg(not(any(
49    apple,
50    target_os = "dragonfly",
51    target_os = "emscripten",
52    target_os = "espidf",
53    target_os = "freebsd", // FreeBSD 12 has clock_nanosleep, but libc targets FreeBSD 11.
54    target_os = "haiku",
55    target_os = "horizon",
56    target_os = "openbsd",
57    target_os = "redox",
58    target_os = "vita",
59    target_os = "wasi",
60)))]
61#[inline]
62pub(crate) fn clock_nanosleep_relative(id: ClockId, request: &Timespec) -> NanosleepRelativeResult {
63    // Old 32-bit version: libc has `clock_nanosleep` but it is not y2038 safe
64    // by default. But there may be a `__clock_nanosleep_time64` we can use.
65    #[cfg(fix_y2038)]
66    {
67        #[cfg(target_env = "gnu")]
68        if let Some(libc_clock_nanosleep) = __clock_nanosleep_time64.get() {
69            let flags = 0;
70            let mut remain = MaybeUninit::<LibcTimespec>::uninit();
71
72            unsafe {
73                return match libc_clock_nanosleep(
74                    id as c::clockid_t,
75                    flags,
76                    &request.clone().into(),
77                    remain.as_mut_ptr(),
78                ) {
79                    0 => NanosleepRelativeResult::Ok,
80                    err if err == io::Errno::INTR.0 => {
81                        NanosleepRelativeResult::Interrupted(remain.assume_init().into())
82                    }
83                    err => NanosleepRelativeResult::Err(io::Errno(err)),
84                };
85            }
86        }
87
88        clock_nanosleep_relative_old(id, request)
89    }
90
91    // Main version: libc is y2038 safe and has `clock_nanosleep`.
92    #[cfg(not(fix_y2038))]
93    unsafe {
94        let flags = 0;
95        let mut remain = MaybeUninit::<Timespec>::uninit();
96
97        match c::clock_nanosleep(
98            id as c::clockid_t,
99            flags,
100            as_libc_timespec_ptr(request),
101            as_libc_timespec_mut_ptr(&mut remain),
102        ) {
103            0 => NanosleepRelativeResult::Ok,
104            err if err == io::Errno::INTR.0 => {
105                NanosleepRelativeResult::Interrupted(remain.assume_init())
106            }
107            err => NanosleepRelativeResult::Err(io::Errno(err)),
108        }
109    }
110}
111
112#[cfg(all(
113    fix_y2038,
114    not(any(
115        apple,
116        target_os = "emscripten",
117        target_os = "haiku",
118        target_os = "horizon",
119        target_os = "vita"
120    ))
121))]
122fn clock_nanosleep_relative_old(
123    id: crate::clockid::ClockId,
124    request: &Timespec,
125) -> NanosleepRelativeResult {
126    let tv_sec = match request.tv_sec.try_into() {
127        Ok(tv_sec) => tv_sec,
128        Err(_) => return NanosleepRelativeResult::Err(io::Errno::OVERFLOW),
129    };
130    let tv_nsec = match request.tv_nsec.try_into() {
131        Ok(tv_nsec) => tv_nsec,
132        Err(_) => return NanosleepRelativeResult::Err(io::Errno::INVAL),
133    };
134    let old_request = c::timespec { tv_sec, tv_nsec };
135    let mut old_remain = MaybeUninit::<c::timespec>::uninit();
136    let flags = 0;
137
138    unsafe {
139        match c::clock_nanosleep(
140            id as c::clockid_t,
141            flags,
142            &old_request,
143            old_remain.as_mut_ptr(),
144        ) {
145            0 => NanosleepRelativeResult::Ok,
146            err if err == io::Errno::INTR.0 => {
147                let old_remain = old_remain.assume_init();
148                let remain = Timespec {
149                    tv_sec: old_remain.tv_sec.into(),
150                    tv_nsec: old_remain.tv_nsec.into(),
151                };
152                NanosleepRelativeResult::Interrupted(remain)
153            }
154            err => NanosleepRelativeResult::Err(io::Errno(err)),
155        }
156    }
157}
158
159#[cfg(not(any(
160    apple,
161    target_os = "dragonfly",
162    target_os = "emscripten",
163    target_os = "espidf",
164    target_os = "freebsd", // FreeBSD 12 has clock_nanosleep, but libc targets FreeBSD 11.
165    target_os = "haiku",
166    target_os = "horizon",
167    target_os = "openbsd",
168    target_os = "redox",
169    target_os = "vita",
170    target_os = "wasi",
171)))]
172#[inline]
173pub(crate) fn clock_nanosleep_absolute(id: ClockId, request: &Timespec) -> io::Result<()> {
174    // Old 32-bit version: libc has `clock_nanosleep` but it is not y2038 safe
175    // by default. But there may be a `__clock_nanosleep_time64` we can use.
176    #[cfg(fix_y2038)]
177    {
178        #[cfg(target_env = "gnu")]
179        if let Some(libc_clock_nanosleep) = __clock_nanosleep_time64.get() {
180            let flags = c::TIMER_ABSTIME;
181            unsafe {
182                return match libc_clock_nanosleep(
183                    id as c::clockid_t,
184                    flags,
185                    &request.clone().into(),
186                    core::ptr::null_mut(),
187                ) {
188                    0 => Ok(()),
189                    err => Err(io::Errno(err)),
190                };
191            }
192        }
193
194        clock_nanosleep_absolute_old(id, request)
195    }
196
197    // Main version: libc is y2038 safe and has `clock_nanosleep`.
198    #[cfg(not(fix_y2038))]
199    {
200        let flags = c::TIMER_ABSTIME;
201
202        match unsafe {
203            c::clock_nanosleep(
204                id as c::clockid_t,
205                flags as _,
206                as_libc_timespec_ptr(request),
207                core::ptr::null_mut(),
208            )
209        } {
210            0 => Ok(()),
211            err => Err(io::Errno(err)),
212        }
213    }
214}
215
216#[cfg(all(
217    fix_y2038,
218    not(any(
219        apple,
220        target_os = "emscripten",
221        target_os = "haiku",
222        target_os = "horizon",
223        target_os = "vita"
224    ))
225))]
226fn clock_nanosleep_absolute_old(id: crate::clockid::ClockId, request: &Timespec) -> io::Result<()> {
227    let flags = c::TIMER_ABSTIME;
228
229    let old_request = c::timespec {
230        tv_sec: request.tv_sec.try_into().map_err(|_| io::Errno::OVERFLOW)?,
231        tv_nsec: request.tv_nsec.try_into().map_err(|_| io::Errno::INVAL)?,
232    };
233    match unsafe {
234        c::clock_nanosleep(
235            id as c::clockid_t,
236            flags,
237            &old_request,
238            core::ptr::null_mut(),
239        )
240    } {
241        0 => Ok(()),
242        err => Err(io::Errno(err)),
243    }
244}
245
246#[cfg(not(target_os = "redox"))]
247#[inline]
248pub(crate) fn nanosleep(request: &Timespec) -> NanosleepRelativeResult {
249    // Old 32-bit version: libc has `nanosleep` but it is not y2038 safe by
250    // default. But there may be a `__nanosleep64` we can use.
251    #[cfg(fix_y2038)]
252    {
253        #[cfg(target_env = "gnu")]
254        if let Some(libc_nanosleep) = __nanosleep64.get() {
255            let mut remain = MaybeUninit::<LibcTimespec>::uninit();
256            unsafe {
257                return match ret(libc_nanosleep(&request.clone().into(), remain.as_mut_ptr())) {
258                    Ok(()) => NanosleepRelativeResult::Ok,
259                    Err(io::Errno::INTR) => {
260                        NanosleepRelativeResult::Interrupted(remain.assume_init().into())
261                    }
262                    Err(err) => NanosleepRelativeResult::Err(err),
263                };
264            }
265        }
266
267        nanosleep_old(request)
268    }
269
270    // Main version: libc is y2038 safe and has `nanosleep`.
271    #[cfg(not(fix_y2038))]
272    unsafe {
273        let mut remain = MaybeUninit::<Timespec>::uninit();
274
275        match ret(c::nanosleep(
276            as_libc_timespec_ptr(request),
277            as_libc_timespec_mut_ptr(&mut remain),
278        )) {
279            Ok(()) => NanosleepRelativeResult::Ok,
280            Err(io::Errno::INTR) => NanosleepRelativeResult::Interrupted(remain.assume_init()),
281            Err(err) => NanosleepRelativeResult::Err(err),
282        }
283    }
284}
285
286#[cfg(fix_y2038)]
287fn nanosleep_old(request: &Timespec) -> NanosleepRelativeResult {
288    let tv_sec = match request.tv_sec.try_into() {
289        Ok(tv_sec) => tv_sec,
290        Err(_) => return NanosleepRelativeResult::Err(io::Errno::OVERFLOW),
291    };
292    let tv_nsec = match request.tv_nsec.try_into() {
293        Ok(tv_nsec) => tv_nsec,
294        Err(_) => return NanosleepRelativeResult::Err(io::Errno::INVAL),
295    };
296    let old_request = c::timespec { tv_sec, tv_nsec };
297    let mut old_remain = MaybeUninit::<c::timespec>::uninit();
298
299    unsafe {
300        match ret(c::nanosleep(&old_request, old_remain.as_mut_ptr())) {
301            Ok(()) => NanosleepRelativeResult::Ok,
302            Err(io::Errno::INTR) => {
303                let old_remain = old_remain.assume_init();
304                let remain = Timespec {
305                    tv_sec: old_remain.tv_sec.into(),
306                    tv_nsec: old_remain.tv_nsec.into(),
307                };
308                NanosleepRelativeResult::Interrupted(remain)
309            }
310            Err(err) => NanosleepRelativeResult::Err(err),
311        }
312    }
313}
314
315#[cfg(linux_kernel)]
316#[inline]
317#[must_use]
318pub(crate) fn gettid() -> Pid {
319    // `gettid` wasn't supported in glibc until 2.30, and musl until 1.2.2,
320    // so use `syscall`.
321    // <https://sourceware.org/bugzilla/show_bug.cgi?id=6399#c62>
322    weak_or_syscall! {
323        fn gettid() via SYS_gettid -> c::pid_t
324    }
325
326    unsafe {
327        let tid = gettid();
328        Pid::from_raw_unchecked(tid)
329    }
330}
331
332#[cfg(linux_kernel)]
333#[inline]
334pub(crate) fn setns(fd: BorrowedFd<'_>, nstype: c::c_int) -> io::Result<c::c_int> {
335    // `setns` wasn't supported in glibc until 2.14, and musl until 0.9.5,
336    // so use `syscall`.
337    weak_or_syscall! {
338        fn setns(fd: c::c_int, nstype: c::c_int) via SYS_setns -> c::c_int
339    }
340
341    unsafe { ret_c_int(setns(borrowed_fd(fd), nstype)) }
342}
343
344#[cfg(linux_kernel)]
345#[inline]
346pub(crate) unsafe fn unshare(flags: crate::thread::UnshareFlags) -> io::Result<()> {
347    ret(c::unshare(flags.bits() as i32))
348}
349
350#[cfg(linux_kernel)]
351#[inline]
352pub(crate) fn capget(
353    header: &mut linux_raw_sys::general::__user_cap_header_struct,
354    data: &mut [MaybeUninit<linux_raw_sys::general::__user_cap_data_struct>],
355) -> io::Result<()> {
356    syscall! {
357        fn capget(
358            hdrp: *mut linux_raw_sys::general::__user_cap_header_struct,
359            data: *mut linux_raw_sys::general::__user_cap_data_struct
360        ) via SYS_capget -> c::c_int
361    }
362
363    unsafe {
364        ret(capget(
365            as_mut_ptr(header),
366            data.as_mut_ptr()
367                .cast::<linux_raw_sys::general::__user_cap_data_struct>(),
368        ))
369    }
370}
371
372#[cfg(linux_kernel)]
373#[inline]
374pub(crate) fn capset(
375    header: &mut linux_raw_sys::general::__user_cap_header_struct,
376    data: &[linux_raw_sys::general::__user_cap_data_struct],
377) -> io::Result<()> {
378    syscall! {
379        fn capset(
380            hdrp: *mut linux_raw_sys::general::__user_cap_header_struct,
381            data: *const linux_raw_sys::general::__user_cap_data_struct
382        ) via SYS_capset -> c::c_int
383    }
384
385    unsafe { ret(capset(as_mut_ptr(header), data.as_ptr())) }
386}
387
388#[cfg(linux_kernel)]
389#[inline]
390pub(crate) fn setuid_thread(uid: crate::ugid::Uid) -> io::Result<()> {
391    syscall! {
392        fn setuid(uid: c::uid_t) via SYS_setuid -> c::c_int
393    }
394
395    unsafe { ret(setuid(uid.as_raw())) }
396}
397
398#[cfg(linux_kernel)]
399#[inline]
400pub(crate) fn setresuid_thread(
401    ruid: crate::ugid::Uid,
402    euid: crate::ugid::Uid,
403    suid: crate::ugid::Uid,
404) -> io::Result<()> {
405    #[cfg(any(target_arch = "x86", target_arch = "arm", target_arch = "sparc"))]
406    const SYS: c::c_long = c::SYS_setresuid32 as c::c_long;
407    #[cfg(not(any(target_arch = "x86", target_arch = "arm", target_arch = "sparc")))]
408    const SYS: c::c_long = c::SYS_setresuid as c::c_long;
409
410    syscall! {
411        fn setresuid(ruid: c::uid_t, euid: c::uid_t, suid: c::uid_t) via SYS -> c::c_int
412    }
413
414    unsafe { ret(setresuid(ruid.as_raw(), euid.as_raw(), suid.as_raw())) }
415}
416
417#[cfg(linux_kernel)]
418#[inline]
419pub(crate) fn setgid_thread(gid: crate::ugid::Gid) -> io::Result<()> {
420    syscall! {
421        fn setgid(gid: c::gid_t) via SYS_setgid -> c::c_int
422    }
423
424    unsafe { ret(setgid(gid.as_raw())) }
425}
426
427#[cfg(linux_kernel)]
428#[inline]
429pub(crate) fn setresgid_thread(
430    rgid: crate::ugid::Gid,
431    egid: crate::ugid::Gid,
432    sgid: crate::ugid::Gid,
433) -> io::Result<()> {
434    #[cfg(any(target_arch = "x86", target_arch = "arm", target_arch = "sparc"))]
435    const SYS: c::c_long = c::SYS_setresgid32 as c::c_long;
436    #[cfg(not(any(target_arch = "x86", target_arch = "arm", target_arch = "sparc")))]
437    const SYS: c::c_long = c::SYS_setresgid as c::c_long;
438
439    syscall! {
440        fn setresgid(rgid: c::gid_t, egid: c::gid_t, sgid: c::gid_t) via SYS -> c::c_int
441    }
442
443    unsafe { ret(setresgid(rgid.as_raw(), egid.as_raw(), sgid.as_raw())) }
444}
445
446/// # Safety
447///
448/// The raw pointers must point to valid aligned memory.
449#[cfg(linux_kernel)]
450pub(crate) unsafe fn futex_val2(
451    uaddr: *const AtomicU32,
452    op: super::futex::Operation,
453    flags: futex::Flags,
454    val: u32,
455    val2: u32,
456    uaddr2: *const AtomicU32,
457    val3: u32,
458) -> io::Result<usize> {
459    // Pass `val2` in the least-significant bytes of the `timeout` argument.
460    // [“the kernel casts the timeout value first to unsigned long, then to
461    // uint32_t”], so we perform that exact conversion in reverse to create
462    // the pointer.
463    //
464    // [“the kernel casts the timeout value first to unsigned long, then to uint32_t”]: https://man7.org/linux/man-pages/man2/futex.2.html
465    let timeout = val2 as usize as *const Timespec;
466
467    #[cfg(all(
468        target_pointer_width = "32",
469        not(any(target_arch = "aarch64", target_arch = "x86_64"))
470    ))]
471    {
472        // TODO: Upstream this to the libc crate.
473        #[allow(non_upper_case_globals)]
474        const SYS_futex_time64: i32 = linux_raw_sys::general::__NR_futex_time64 as i32;
475
476        syscall! {
477            fn futex_time64(
478                uaddr: *const AtomicU32,
479                futex_op: c::c_int,
480                val: u32,
481                timeout: *const Timespec,
482                uaddr2: *const AtomicU32,
483                val3: u32
484            ) via SYS_futex_time64 -> c::ssize_t
485        }
486
487        ret_usize(futex_time64(
488            uaddr,
489            op as i32 | flags.bits() as i32,
490            val,
491            timeout,
492            uaddr2,
493            val3,
494        ))
495    }
496
497    #[cfg(any(
498        target_pointer_width = "64",
499        target_arch = "aarch64",
500        target_arch = "x86_64"
501    ))]
502    {
503        syscall! {
504            fn futex(
505                uaddr: *const AtomicU32,
506                futex_op: c::c_int,
507                val: u32,
508                timeout: *const Timespec,
509                uaddr2: *const AtomicU32,
510                val3: u32
511            ) via SYS_futex -> c::c_long
512        }
513
514        ret_usize(futex(
515            uaddr,
516            op as i32 | flags.bits() as i32,
517            val,
518            timeout.cast(),
519            uaddr2,
520            val3,
521        ) as isize)
522    }
523}
524
525/// # Safety
526///
527/// The raw pointers must point to valid aligned memory.
528#[cfg(linux_kernel)]
529pub(crate) unsafe fn futex_timeout(
530    uaddr: *const AtomicU32,
531    op: super::futex::Operation,
532    flags: futex::Flags,
533    val: u32,
534    timeout: Option<&Timespec>,
535    uaddr2: *const AtomicU32,
536    val3: u32,
537) -> io::Result<usize> {
538    #[cfg(all(
539        target_pointer_width = "32",
540        not(any(target_arch = "aarch64", target_arch = "x86_64"))
541    ))]
542    {
543        // TODO: Upstream this to the libc crate.
544        #[allow(non_upper_case_globals)]
545        const SYS_futex_time64: i32 = linux_raw_sys::general::__NR_futex_time64 as i32;
546
547        syscall! {
548            fn futex_time64(
549                uaddr: *const AtomicU32,
550                futex_op: c::c_int,
551                val: u32,
552                timeout: *const Timespec,
553                uaddr2: *const AtomicU32,
554                val3: u32
555            ) via SYS_futex_time64 -> c::ssize_t
556        }
557
558        ret_usize(futex_time64(
559            uaddr,
560            op as i32 | flags.bits() as i32,
561            val,
562            option_as_ptr(timeout),
563            uaddr2,
564            val3,
565        ))
566        .or_else(|err| {
567            // See the comments in `clock_gettime_via_syscall` about emulation.
568            if err == io::Errno::NOSYS {
569                futex_old_timespec(uaddr, op, flags, val, timeout, uaddr2, val3)
570            } else {
571                Err(err)
572            }
573        })
574    }
575
576    #[cfg(any(
577        target_pointer_width = "64",
578        target_arch = "aarch64",
579        target_arch = "x86_64"
580    ))]
581    {
582        syscall! {
583            fn futex(
584                uaddr: *const AtomicU32,
585                futex_op: c::c_int,
586                val: u32,
587                timeout: *const Timespec,
588                uaddr2: *const AtomicU32,
589                val3: u32
590            ) via SYS_futex -> c::c_long
591        }
592
593        ret_usize(futex(
594            uaddr,
595            op as i32 | flags.bits() as i32,
596            val,
597            option_as_ptr(timeout).cast(),
598            uaddr2,
599            val3,
600        ) as isize)
601    }
602}
603
604/// # Safety
605///
606/// The raw pointers must point to valid aligned memory.
607#[cfg(linux_kernel)]
608#[cfg(all(
609    target_pointer_width = "32",
610    not(any(target_arch = "aarch64", target_arch = "x86_64"))
611))]
612unsafe fn futex_old_timespec(
613    uaddr: *const AtomicU32,
614    op: super::futex::Operation,
615    flags: futex::Flags,
616    val: u32,
617    timeout: Option<&Timespec>,
618    uaddr2: *const AtomicU32,
619    val3: u32,
620) -> io::Result<usize> {
621    syscall! {
622        fn futex(
623            uaddr: *const AtomicU32,
624            futex_op: c::c_int,
625            val: u32,
626            timeout: *const linux_raw_sys::general::__kernel_old_timespec,
627            uaddr2: *const AtomicU32,
628            val3: u32
629        ) via SYS_futex -> c::c_long
630    }
631
632    let old_timeout = if let Some(timeout) = timeout {
633        Some(linux_raw_sys::general::__kernel_old_timespec {
634            tv_sec: timeout.tv_sec.try_into().map_err(|_| io::Errno::INVAL)?,
635            tv_nsec: timeout.tv_nsec.try_into().map_err(|_| io::Errno::INVAL)?,
636        })
637    } else {
638        None
639    };
640    ret_usize(futex(
641        uaddr,
642        op as i32 | flags.bits() as i32,
643        val,
644        option_as_ptr(old_timeout.as_ref()),
645        uaddr2,
646        val3,
647    ) as isize)
648}
649
650#[cfg(linux_kernel)]
651pub(crate) fn futex_waitv(
652    waiters: &[futex::Wait],
653    flags: futex::WaitvFlags,
654    timeout: Option<&Timespec>,
655    clockid: ClockId,
656) -> io::Result<usize> {
657    use futex::Wait as FutexWait;
658    use linux_raw_sys::general::__kernel_clockid_t as clockid_t;
659    syscall! {
660        fn futex_waitv(
661            waiters: *const FutexWait,
662            nr_futexes: c::c_uint,
663            flags: c::c_uint,
664            timeout: *const Timespec,
665            clockid: clockid_t
666        ) via SYS_futex_waitv -> c::c_int
667    }
668
669    let nr_futexes: c::c_uint = waiters.len().try_into().map_err(|_| io::Errno::INVAL)?;
670
671    unsafe {
672        ret_c_int(futex_waitv(
673            waiters.as_ptr(),
674            nr_futexes,
675            flags.bits(),
676            option_as_ptr(timeout).cast(),
677            clockid as _,
678        ))
679        .map(|n| n as usize)
680    }
681}
682
683#[cfg(linux_kernel)]
684#[inline]
685pub(crate) fn setgroups_thread(groups: &[crate::ugid::Gid]) -> io::Result<()> {
686    syscall! {
687        fn setgroups(size: c::size_t, list: *const c::gid_t) via SYS_setgroups -> c::c_int
688    }
689    ret(unsafe { setgroups(groups.len(), groups.as_ptr().cast()) })
690}
691
692#[cfg(any(linux_kernel, target_os = "dragonfly"))]
693#[inline]
694pub(crate) fn sched_getcpu() -> usize {
695    let r = unsafe { c::sched_getcpu() };
696    debug_assert!(r >= 0);
697    r as usize
698}
699
700#[cfg(any(freebsdlike, linux_kernel, target_os = "fuchsia"))]
701#[inline]
702pub(crate) fn sched_getaffinity(pid: Option<Pid>, cpuset: &mut RawCpuSet) -> io::Result<()> {
703    unsafe {
704        ret(c::sched_getaffinity(
705            Pid::as_raw(pid) as _,
706            core::mem::size_of::<RawCpuSet>(),
707            cpuset,
708        ))
709    }
710}
711
712#[cfg(any(freebsdlike, linux_kernel, target_os = "fuchsia"))]
713#[inline]
714pub(crate) fn sched_setaffinity(pid: Option<Pid>, cpuset: &RawCpuSet) -> io::Result<()> {
715    unsafe {
716        ret(c::sched_setaffinity(
717            Pid::as_raw(pid) as _,
718            core::mem::size_of::<RawCpuSet>(),
719            cpuset,
720        ))
721    }
722}
723
724#[inline]
725pub(crate) fn sched_yield() {
726    unsafe {
727        let _ = c::sched_yield();
728    }
729}
730
731// The `membarrier` syscall has a third argument, but it's only used when
732// the `flags` argument is `MEMBARRIER_CMD_FLAG_CPU`.
733#[cfg(linux_kernel)]
734syscall! {
735    fn membarrier_all(
736        cmd: c::c_int,
737        flags: c::c_uint
738    ) via SYS_membarrier -> c::c_int
739}
740
741#[cfg(linux_kernel)]
742pub(crate) fn membarrier_query() -> MembarrierQuery {
743    // glibc does not have a wrapper for `membarrier`; [the documentation]
744    // says to use `syscall`.
745    //
746    // [the documentation]: https://man7.org/linux/man-pages/man2/membarrier.2.html#NOTES
747    const MEMBARRIER_CMD_QUERY: u32 = 0;
748    unsafe {
749        match ret_u32(membarrier_all(MEMBARRIER_CMD_QUERY as i32, 0)) {
750            Ok(query) => MembarrierQuery::from_bits_retain(query),
751            Err(_) => MembarrierQuery::empty(),
752        }
753    }
754}
755
756#[cfg(linux_kernel)]
757pub(crate) fn membarrier(cmd: MembarrierCommand) -> io::Result<()> {
758    unsafe { ret(membarrier_all(cmd as i32, 0)) }
759}
760
761#[cfg(linux_kernel)]
762pub(crate) fn membarrier_cpu(cmd: MembarrierCommand, cpu: Cpuid) -> io::Result<()> {
763    const MEMBARRIER_CMD_FLAG_CPU: u32 = 1;
764
765    syscall! {
766        fn membarrier_cpu(
767            cmd: c::c_int,
768            flags: c::c_uint,
769            cpu_id: c::c_int
770        ) via SYS_membarrier -> c::c_int
771    }
772
773    unsafe {
774        ret(membarrier_cpu(
775            cmd as i32,
776            MEMBARRIER_CMD_FLAG_CPU,
777            bitcast!(cpu.as_raw()),
778        ))
779    }
780}