Skip to content

Rework namespace functions and types #1437

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ compiler_builtins = { version = '0.1.49', optional = true }
[target.'cfg(all(not(rustix_use_libc), not(miri), target_os = "linux", any(target_endian = "little", any(target_arch = "s390x", target_arch = "powerpc")), any(target_arch = "arm", all(target_arch = "aarch64", target_pointer_width = "64"), target_arch = "riscv64", all(rustix_use_experimental_asm, target_arch = "powerpc"), all(rustix_use_experimental_asm, target_arch = "powerpc64"), all(rustix_use_experimental_asm, target_arch = "s390x"), all(rustix_use_experimental_asm, target_arch = "mips"), all(rustix_use_experimental_asm, target_arch = "mips32r6"), all(rustix_use_experimental_asm, target_arch = "mips64"), all(rustix_use_experimental_asm, target_arch = "mips64r6"), target_arch = "x86", all(target_arch = "x86_64", target_pointer_width = "64"))))'.dependencies]
linux-raw-sys = { version = "0.9.2", default-features = false, features = ["general", "errno", "ioctl", "no_std", "elf"] }
libc_errno = { package = "errno", version = "0.3.10", default-features = false, optional = true }
libc = { version = "0.2.171", default-features = false, optional = true }
libc = { version = "0.2.174", default-features = false, optional = true }

# Dependencies for platforms where only libc is supported:
#
# On all other Unix-family platforms, and under Miri, we always use the libc
# backend, so enable its dependencies unconditionally.
[target.'cfg(all(not(windows), any(rustix_use_libc, miri, not(all(target_os = "linux", any(target_endian = "little", any(target_arch = "s390x", target_arch = "powerpc")), any(target_arch = "arm", all(target_arch = "aarch64", target_pointer_width = "64"), target_arch = "riscv64", all(rustix_use_experimental_asm, target_arch = "powerpc"), all(rustix_use_experimental_asm, target_arch = "powerpc64"), all(rustix_use_experimental_asm, target_arch = "s390x"), all(rustix_use_experimental_asm, target_arch = "mips"), all(rustix_use_experimental_asm, target_arch = "mips32r6"), all(rustix_use_experimental_asm, target_arch = "mips64"), all(rustix_use_experimental_asm, target_arch = "mips64r6"), target_arch = "x86", all(target_arch = "x86_64", target_pointer_width = "64")))))))'.dependencies]
libc_errno = { package = "errno", version = "0.3.10", default-features = false }
libc = { version = "0.2.171", default-features = false }
libc = { version = "0.2.174", default-features = false }

# Additional dependencies for Linux with the libc backend:
#
Expand All @@ -66,7 +66,7 @@ default-features = false

[dev-dependencies]
tempfile = "3.5.0"
libc = "0.2.171"
libc = "0.2.174"
libc_errno = { package = "errno", version = "0.3.10", default-features = false }
serial_test = "2.0.0"
memoffset = "0.9.0"
Expand Down
9 changes: 9 additions & 0 deletions src/backend/linux_raw/c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,3 +383,12 @@ mod statx_flags {
)
))]
pub(crate) use statx_flags::*;

#[cfg(feature = "thread")]
pub(crate) use linux_raw_sys::ioctl::{
NS_GET_NSTYPE,
NS_GET_OWNER_UID,
NS_GET_PARENT,
NS_GET_USERNS,
// NS_GET_PID_FROM_PIDNS, NS_GET_PID_IN_PIDNS, NS_GET_TGID_FROM_PIDNS, NS_GET_TGID_IN_PIDNS,
};
119 changes: 114 additions & 5 deletions src/ioctl/patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ unsafe impl<const OPCODE: Opcode> Ioctl for NoArg<OPCODE> {

const IS_MUTATING: bool = false;

fn opcode(&self) -> self::Opcode {
fn opcode(&self) -> Opcode {
OPCODE
}

Expand All @@ -52,6 +52,97 @@ unsafe impl<const OPCODE: Opcode> Ioctl for NoArg<OPCODE> {
}
}

/// Implements an `ioctl` with no real arguments that returns an integer.
///
/// To compute a value for the `OPCODE` argument, see the functions in the
/// [`opcode`] module.
///
/// [`opcode`]: crate::ioctl::opcode
pub struct NoArgGetter<const OPCODE: Opcode> {}
impl<const OPCODE: Opcode> fmt::Debug for NoArgGetter<OPCODE> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("NoArgGetter").field(&OPCODE).finish()
}
}
impl<const OPCODE: Opcode> NoArgGetter<OPCODE> {
/// Create a new no-argument-getter `ioctl` object.
///
/// # Safety
///
/// - `OPCODE` must provide a valid opcode.
#[inline]
pub const unsafe fn new() -> Self {
Self {}
}
}
unsafe impl<const OPCODE: Opcode> Ioctl for NoArgGetter<OPCODE> {
type Output = IoctlOutput;

const IS_MUTATING: bool = false;

fn opcode(&self) -> Opcode {
OPCODE
}

fn as_ptr(&mut self) -> *mut c::c_void {
core::ptr::null_mut()
}

unsafe fn output_from_ptr(output: IoctlOutput, _: *mut c::c_void) -> Result<Self::Output> {
Ok(output)
}
}

/// Implements an `ioctl` with one real argument that returns an integer.
///
/// To compute a value for the `OPCODE` argument, see the functions in the
/// [`opcode`] module.
///
/// [`opcode`]: crate::ioctl::opcode
pub struct ParameterizedReturnGetter<const OPCODE: Opcode> {
value: *const c::c_void,
}

impl<const OPCODE: Opcode> fmt::Debug for ParameterizedReturnGetter<OPCODE> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("ParameterizedReturnGetter")
.field(&OPCODE)
.field(&self.value)
.finish()
}
}

impl<const OPCODE: Opcode> ParameterizedReturnGetter<OPCODE> {
/// Create a new `ioctl` object.
///
/// # Safety
///
/// - `OPCODE` must provide a valid opcode.
#[inline]
pub const unsafe fn new(value: usize) -> Self {
Self {
value: core::ptr::without_provenance(value),
}
}
}
unsafe impl<const OPCODE: Opcode> Ioctl for ParameterizedReturnGetter<OPCODE> {
type Output = IoctlOutput;

const IS_MUTATING: bool = false;

fn opcode(&self) -> Opcode {
OPCODE
}

fn as_ptr(&mut self) -> *mut c::c_void {
self.value.cast_mut()
}

unsafe fn output_from_ptr(output: IoctlOutput, _: *mut c::c_void) -> Result<Self::Output> {
Ok(output)
}
}

/// Implements the traditional “getter” pattern for `ioctl`s.
///
/// Some `ioctl`s just read data into the userspace. As this is a popular
Expand Down Expand Up @@ -93,7 +184,7 @@ unsafe impl<const OPCODE: Opcode, Output> Ioctl for Getter<OPCODE, Output> {

const IS_MUTATING: bool = true;

fn opcode(&self) -> self::Opcode {
fn opcode(&self) -> Opcode {
OPCODE
}

Expand Down Expand Up @@ -148,7 +239,7 @@ unsafe impl<const OPCODE: Opcode, Input> Ioctl for Setter<OPCODE, Input> {

const IS_MUTATING: bool = false;

fn opcode(&self) -> self::Opcode {
fn opcode(&self) -> Opcode {
OPCODE
}

Expand All @@ -175,6 +266,15 @@ pub struct Updater<'a, const OPCODE: Opcode, Value> {
value: &'a mut Value,
}

impl<'a, const OPCODE: Opcode, Value: fmt::Debug> fmt::Debug for Updater<'a, OPCODE, Value> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("Setter")
.field(&OPCODE)
.field(&self.value)
.finish()
}
}

impl<'a, const OPCODE: Opcode, Value> Updater<'a, OPCODE, Value> {
/// Create a new pointer updater-style `ioctl` object.
///
Expand All @@ -194,7 +294,7 @@ unsafe impl<'a, const OPCODE: Opcode, T> Ioctl for Updater<'a, OPCODE, T> {

const IS_MUTATING: bool = true;

fn opcode(&self) -> self::Opcode {
fn opcode(&self) -> Opcode {
OPCODE
}

Expand All @@ -220,6 +320,15 @@ pub struct IntegerSetter<const OPCODE: Opcode> {
value: *mut c::c_void,
}

impl<const OPCODE: Opcode> fmt::Debug for IntegerSetter<OPCODE> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("IntegerSetter")
.field(&OPCODE)
.field(&self.value)
.finish()
}
}

impl<const OPCODE: Opcode> IntegerSetter<OPCODE> {
/// Create a new integer `Ioctl` helper containing a `usize`.
///
Expand Down Expand Up @@ -251,7 +360,7 @@ unsafe impl<const OPCODE: Opcode> Ioctl for IntegerSetter<OPCODE> {

const IS_MUTATING: bool = false;

fn opcode(&self) -> self::Opcode {
fn opcode(&self) -> Opcode {
OPCODE
}

Expand Down
8 changes: 4 additions & 4 deletions src/thread/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ mod libcap;
#[cfg(linux_kernel)]
mod membarrier;
#[cfg(linux_kernel)]
mod ns;
#[cfg(linux_kernel)]
mod prctl;
#[cfg(any(freebsdlike, linux_kernel, target_os = "fuchsia"))]
mod sched;
mod sched_yield;
#[cfg(linux_kernel)]
mod setns;

#[cfg(not(target_os = "redox"))]
pub use clock::*;
Expand All @@ -29,9 +29,9 @@ pub use libcap::{capabilities, set_capabilities, CapabilityFlags, CapabilitySet,
#[cfg(linux_kernel)]
pub use membarrier::*;
#[cfg(linux_kernel)]
pub use ns::*;
#[cfg(linux_kernel)]
pub use prctl::*;
#[cfg(any(freebsdlike, linux_kernel, target_os = "fuchsia"))]
pub use sched::*;
pub use sched_yield::sched_yield;
#[cfg(linux_kernel)]
pub use setns::*;
Loading
Loading