summaryrefslogtreecommitdiffstats
path: root/vendor/rustix/src/backend/libc/thread/syscalls.rs
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/rustix/src/backend/libc/thread/syscalls.rs')
-rw-r--r--vendor/rustix/src/backend/libc/thread/syscalls.rs135
1 files changed, 88 insertions, 47 deletions
diff --git a/vendor/rustix/src/backend/libc/thread/syscalls.rs b/vendor/rustix/src/backend/libc/thread/syscalls.rs
index adf4bb700..6066fd0d4 100644
--- a/vendor/rustix/src/backend/libc/thread/syscalls.rs
+++ b/vendor/rustix/src/backend/libc/thread/syscalls.rs
@@ -1,18 +1,23 @@
//! libc syscalls supporting `rustix::thread`.
-use super::super::c;
-use super::super::conv::ret;
-#[cfg(any(target_os = "android", target_os = "linux"))]
-use super::super::conv::{borrowed_fd, ret_c_int, syscall_ret};
-use super::super::time::types::LibcTimespec;
-#[cfg(any(target_os = "android", target_os = "linux"))]
-use crate::fd::BorrowedFd;
+use crate::backend::c;
+use crate::backend::conv::ret;
use crate::io;
-#[cfg(any(target_os = "android", target_os = "linux"))]
-use crate::process::{Pid, RawNonZeroPid};
#[cfg(not(target_os = "redox"))]
use crate::thread::{NanosleepRelativeResult, Timespec};
+#[cfg(all(
+ any(target_arch = "arm", target_arch = "mips", target_arch = "x86"),
+ target_env = "gnu",
+))]
+use crate::timespec::LibcTimespec;
use core::mem::MaybeUninit;
+#[cfg(linux_kernel)]
+use {
+ crate::backend::conv::{borrowed_fd, ret_c_int},
+ crate::fd::BorrowedFd,
+ crate::pid::Pid,
+ crate::utils::as_mut_ptr,
+};
#[cfg(not(any(
apple,
freebsdlike,
@@ -47,16 +52,17 @@ weak!(fn __nanosleep64(*const LibcTimespec, *mut LibcTimespec) -> c::c_int);
)))]
#[inline]
pub(crate) fn clock_nanosleep_relative(id: ClockId, request: &Timespec) -> NanosleepRelativeResult {
- let mut remain = MaybeUninit::<LibcTimespec>::uninit();
let flags = 0;
- // 32-bit gnu version: libc has `clock_nanosleep` but it is not y2038 safe by
- // default.
+ // 32-bit gnu version: libc has `clock_nanosleep` but it is not y2038 safe
+ // by default.
#[cfg(all(
any(target_arch = "arm", target_arch = "mips", target_arch = "x86"),
target_env = "gnu",
))]
unsafe {
+ let mut remain = MaybeUninit::<LibcTimespec>::uninit();
+
if let Some(libc_clock_nanosleep) = __clock_nanosleep_time64.get() {
match libc_clock_nanosleep(
id as c::clockid_t,
@@ -81,6 +87,8 @@ pub(crate) fn clock_nanosleep_relative(id: ClockId, request: &Timespec) -> Nanos
target_env = "gnu",
)))]
unsafe {
+ let mut remain = MaybeUninit::<Timespec>::uninit();
+
match c::clock_nanosleep(id as c::clockid_t, flags, request, remain.as_mut_ptr()) {
0 => NanosleepRelativeResult::Ok,
err if err == io::Errno::INTR.0 => {
@@ -96,7 +104,6 @@ pub(crate) fn clock_nanosleep_relative(id: ClockId, request: &Timespec) -> Nanos
target_env = "gnu",
))]
unsafe fn clock_nanosleep_relative_old(id: ClockId, request: &Timespec) -> NanosleepRelativeResult {
- use core::convert::TryInto;
let tv_sec = match request.tv_sec.try_into() {
Ok(tv_sec) => tv_sec,
Err(_) => return NanosleepRelativeResult::Err(io::Errno::OVERFLOW),
@@ -142,8 +149,8 @@ unsafe fn clock_nanosleep_relative_old(id: ClockId, request: &Timespec) -> Nanos
pub(crate) fn clock_nanosleep_absolute(id: ClockId, request: &Timespec) -> io::Result<()> {
let flags = c::TIMER_ABSTIME;
- // 32-bit gnu version: libc has `clock_nanosleep` but it is not y2038 safe by
- // default.
+ // 32-bit gnu version: libc has `clock_nanosleep` but it is not y2038 safe
+ // by default.
#[cfg(all(
any(target_arch = "arm", target_arch = "mips", target_arch = "x86"),
target_env = "gnu",
@@ -182,8 +189,6 @@ pub(crate) fn clock_nanosleep_absolute(id: ClockId, request: &Timespec) -> io::R
target_env = "gnu",
))]
fn clock_nanosleep_absolute_old(id: ClockId, request: &Timespec) -> io::Result<()> {
- use core::convert::TryInto;
-
let flags = c::TIMER_ABSTIME;
let old_request = c::timespec {
@@ -199,8 +204,6 @@ fn clock_nanosleep_absolute_old(id: ClockId, request: &Timespec) -> io::Result<(
#[cfg(not(target_os = "redox"))]
#[inline]
pub(crate) fn nanosleep(request: &Timespec) -> NanosleepRelativeResult {
- let mut remain = MaybeUninit::<LibcTimespec>::uninit();
-
// 32-bit gnu version: libc has `nanosleep` but it is not y2038 safe by
// default.
#[cfg(all(
@@ -208,6 +211,8 @@ pub(crate) fn nanosleep(request: &Timespec) -> NanosleepRelativeResult {
target_env = "gnu",
))]
unsafe {
+ let mut remain = MaybeUninit::<LibcTimespec>::uninit();
+
if let Some(libc_nanosleep) = __nanosleep64.get() {
match ret(libc_nanosleep(&request.clone().into(), remain.as_mut_ptr())) {
Ok(()) => NanosleepRelativeResult::Ok,
@@ -227,6 +232,8 @@ pub(crate) fn nanosleep(request: &Timespec) -> NanosleepRelativeResult {
target_env = "gnu",
)))]
unsafe {
+ let mut remain = MaybeUninit::<Timespec>::uninit();
+
match ret(c::nanosleep(request, remain.as_mut_ptr())) {
Ok(()) => NanosleepRelativeResult::Ok,
Err(io::Errno::INTR) => NanosleepRelativeResult::Interrupted(remain.assume_init()),
@@ -240,7 +247,6 @@ pub(crate) fn nanosleep(request: &Timespec) -> NanosleepRelativeResult {
target_env = "gnu",
))]
unsafe fn nanosleep_old(request: &Timespec) -> NanosleepRelativeResult {
- use core::convert::TryInto;
let tv_sec = match request.tv_sec.try_into() {
Ok(tv_sec) => tv_sec,
Err(_) => return NanosleepRelativeResult::Err(io::Errno::OVERFLOW),
@@ -266,7 +272,7 @@ unsafe fn nanosleep_old(request: &Timespec) -> NanosleepRelativeResult {
}
}
-#[cfg(any(target_os = "android", target_os = "linux"))]
+#[cfg(linux_kernel)]
#[inline]
#[must_use]
pub(crate) fn gettid() -> Pid {
@@ -279,12 +285,11 @@ pub(crate) fn gettid() -> Pid {
unsafe {
let tid = gettid();
- debug_assert_ne!(tid, 0);
- Pid::from_raw_nonzero(RawNonZeroPid::new_unchecked(tid))
+ Pid::from_raw_unchecked(tid)
}
}
-#[cfg(any(target_os = "android", target_os = "linux"))]
+#[cfg(linux_kernel)]
#[inline]
pub(crate) fn setns(fd: BorrowedFd, nstype: c::c_int) -> io::Result<c::c_int> {
// `setns` wasn't supported in glibc until 2.14, and musl until 0.9.5,
@@ -296,68 +301,104 @@ pub(crate) fn setns(fd: BorrowedFd, nstype: c::c_int) -> io::Result<c::c_int> {
unsafe { ret_c_int(setns(borrowed_fd(fd), nstype)) }
}
-#[cfg(any(target_os = "android", target_os = "linux"))]
+#[cfg(linux_kernel)]
#[inline]
pub(crate) fn unshare(flags: crate::thread::UnshareFlags) -> io::Result<()> {
unsafe { ret(c::unshare(flags.bits() as i32)) }
}
-#[cfg(any(target_os = "android", target_os = "linux"))]
+#[cfg(linux_kernel)]
#[inline]
pub(crate) fn capget(
header: &mut linux_raw_sys::general::__user_cap_header_struct,
data: &mut [MaybeUninit<linux_raw_sys::general::__user_cap_data_struct>],
) -> io::Result<()> {
- let header: *mut _ = header;
- unsafe { syscall_ret(c::syscall(c::SYS_capget, header, data.as_mut_ptr())) }
+ syscall! {
+ fn capget(
+ hdrp: *mut linux_raw_sys::general::__user_cap_header_struct,
+ data: *mut linux_raw_sys::general::__user_cap_data_struct
+ ) via SYS_capget -> c::c_int
+ }
+
+ unsafe {
+ ret(capget(
+ as_mut_ptr(header),
+ data.as_mut_ptr()
+ .cast::<linux_raw_sys::general::__user_cap_data_struct>(),
+ ))
+ }
}
-#[cfg(any(target_os = "android", target_os = "linux"))]
+#[cfg(linux_kernel)]
#[inline]
pub(crate) fn capset(
header: &mut linux_raw_sys::general::__user_cap_header_struct,
data: &[linux_raw_sys::general::__user_cap_data_struct],
) -> io::Result<()> {
- let header: *mut _ = header;
- unsafe { syscall_ret(c::syscall(c::SYS_capset, header, data.as_ptr())) }
+ syscall! {
+ fn capset(
+ hdrp: *mut linux_raw_sys::general::__user_cap_header_struct,
+ data: *const linux_raw_sys::general::__user_cap_data_struct
+ ) via SYS_capset -> c::c_int
+ }
+
+ unsafe { ret(capset(as_mut_ptr(header), data.as_ptr())) }
}
-#[cfg(any(target_os = "android", target_os = "linux"))]
+#[cfg(linux_kernel)]
#[inline]
-pub(crate) fn setuid_thread(uid: crate::process::Uid) -> io::Result<()> {
- unsafe { syscall_ret(c::syscall(c::SYS_setuid, uid.as_raw())) }
+pub(crate) fn setuid_thread(uid: crate::ugid::Uid) -> io::Result<()> {
+ syscall! {
+ fn setuid(uid: c::uid_t) via SYS_setuid -> c::c_int
+ }
+
+ unsafe { ret(setuid(uid.as_raw())) }
}
-#[cfg(any(target_os = "android", target_os = "linux"))]
+#[cfg(linux_kernel)]
#[inline]
pub(crate) fn setresuid_thread(
- ruid: crate::process::Uid,
- euid: crate::process::Uid,
- suid: crate::process::Uid,
+ ruid: crate::ugid::Uid,
+ euid: crate::ugid::Uid,
+ suid: crate::ugid::Uid,
) -> io::Result<()> {
#[cfg(any(target_arch = "x86", target_arch = "arm", target_arch = "sparc"))]
const SYS: c::c_long = c::SYS_setresuid32 as c::c_long;
#[cfg(not(any(target_arch = "x86", target_arch = "arm", target_arch = "sparc")))]
const SYS: c::c_long = c::SYS_setresuid as c::c_long;
- unsafe { syscall_ret(c::syscall(SYS, ruid.as_raw(), euid.as_raw(), suid.as_raw())) }
+
+ syscall! {
+ fn setresuid(ruid: c::uid_t, euid: c::uid_t, suid: c::uid_t) via SYS -> c::c_int
+ }
+
+ unsafe { ret(setresuid(ruid.as_raw(), euid.as_raw(), suid.as_raw())) }
}
-#[cfg(any(target_os = "android", target_os = "linux"))]
+#[cfg(linux_kernel)]
#[inline]
-pub(crate) fn setgid_thread(gid: crate::process::Gid) -> io::Result<()> {
- unsafe { syscall_ret(c::syscall(c::SYS_setgid, gid.as_raw())) }
+pub(crate) fn setgid_thread(gid: crate::ugid::Gid) -> io::Result<()> {
+ syscall! {
+ fn setgid(gid: c::gid_t) via SYS_setgid -> c::c_int
+ }
+
+ unsafe { ret(setgid(gid.as_raw())) }
}
-#[cfg(any(target_os = "android", target_os = "linux"))]
+#[cfg(linux_kernel)]
#[inline]
pub(crate) fn setresgid_thread(
- rgid: crate::process::Gid,
- egid: crate::process::Gid,
- sgid: crate::process::Gid,
+ rgid: crate::ugid::Gid,
+ egid: crate::ugid::Gid,
+ sgid: crate::ugid::Gid,
) -> io::Result<()> {
#[cfg(any(target_arch = "x86", target_arch = "arm", target_arch = "sparc"))]
const SYS: c::c_long = c::SYS_setresgid32 as c::c_long;
#[cfg(not(any(target_arch = "x86", target_arch = "arm", target_arch = "sparc")))]
const SYS: c::c_long = c::SYS_setresgid as c::c_long;
- unsafe { syscall_ret(c::syscall(SYS, rgid.as_raw(), egid.as_raw(), sgid.as_raw())) }
+
+ syscall! {
+ fn setresgid(rgid: c::gid_t, egid: c::gid_t, sgid: c::gid_t) via SYS -> c::c_int
+ }
+
+ unsafe { ret(setresgid(rgid.as_raw(), egid.as_raw(), sgid.as_raw())) }
}