#![allow(unsafe_code)]
use crate::{backend, io};
use alloc::vec::Vec;
#[cfg(linux_kernel)]
use backend::process::types::RawCpuid;
pub use backend::process::types::RawUid;
pub use backend::process::types::RawGid;
pub use backend::process::types::RawPid;
pub use backend::process::types::RawNonZeroPid;
#[repr(transparent)]
#[derive(Copy, Clone, Eq, PartialEq, Debug, Hash)]
pub struct Uid(RawUid);
#[repr(transparent)]
#[derive(Copy, Clone, Eq, PartialEq, Debug, Hash)]
pub struct Gid(RawGid);
#[repr(transparent)]
#[derive(Copy, Clone, Eq, PartialEq, Debug, Hash)]
pub struct Pid(RawNonZeroPid);
#[cfg(linux_kernel)]
#[repr(transparent)]
#[derive(Copy, Clone, Eq, PartialEq, Debug, Hash)]
pub struct Cpuid(RawCpuid);
impl Uid {
pub const ROOT: Self = Self(0);
#[inline]
pub const unsafe fn from_raw(raw: RawUid) -> Self {
Self(raw)
}
#[inline]
pub const fn as_raw(self) -> RawUid {
self.0
}
#[inline]
pub const fn is_root(self) -> bool {
self.0 == Self::ROOT.0
}
}
impl Gid {
pub const ROOT: Self = Self(0);
#[inline]
pub const unsafe fn from_raw(raw: RawGid) -> Self {
Self(raw)
}
#[inline]
pub const fn as_raw(self) -> RawGid {
self.0
}
#[inline]
pub const fn is_root(self) -> bool {
self.0 == Self::ROOT.0
}
}
impl Pid {
pub const INIT: Self = Self(
unsafe { RawNonZeroPid::new_unchecked(1) },
);
#[inline]
pub const unsafe fn from_raw(raw: RawPid) -> Option<Self> {
match RawNonZeroPid::new(raw) {
Some(pid) => Some(Self(pid)),
None => None,
}
}
#[inline]
pub const unsafe fn from_raw_nonzero(raw: RawNonZeroPid) -> Self {
Self(raw)
}
#[cfg(feature = "std")]
#[inline]
pub fn from_child(child: &std::process::Child) -> Self {
let id = child.id();
debug_assert_ne!(id, 0);
unsafe { Self::from_raw_nonzero(RawNonZeroPid::new_unchecked(id as _)) }
}
#[inline]
pub const fn as_raw_nonzero(self) -> RawNonZeroPid {
self.0
}
#[inline]
pub fn as_raw(pid: Option<Self>) -> RawPid {
pid.map_or(0, |pid| pid.0.get())
}
#[inline]
pub const fn is_init(self) -> bool {
self.0.get() == Self::INIT.0.get()
}
}
#[cfg(linux_kernel)]
impl Cpuid {
#[inline]
pub const unsafe fn from_raw(raw: RawCpuid) -> Self {
Self(raw)
}
#[inline]
pub const fn as_raw(self) -> RawCpuid {
self.0
}
}
#[inline]
#[must_use]
pub fn getuid() -> Uid {
backend::process::syscalls::getuid()
}
#[inline]
#[must_use]
pub fn geteuid() -> Uid {
backend::process::syscalls::geteuid()
}
#[inline]
#[must_use]
pub fn getgid() -> Gid {
backend::process::syscalls::getgid()
}
#[inline]
#[must_use]
pub fn getegid() -> Gid {
backend::process::syscalls::getegid()
}
#[inline]
#[must_use]
pub fn getpid() -> Pid {
backend::process::syscalls::getpid()
}
#[inline]
#[must_use]
pub fn getppid() -> Option<Pid> {
backend::process::syscalls::getppid()
}
#[inline]
pub fn getpgid(pid: Option<Pid>) -> io::Result<Pid> {
backend::process::syscalls::getpgid(pid)
}
#[inline]
pub fn setpgid(pid: Option<Pid>, pgid: Option<Pid>) -> io::Result<()> {
backend::process::syscalls::setpgid(pid, pgid)
}
#[inline]
#[must_use]
pub fn getpgrp() -> Pid {
backend::process::syscalls::getpgrp()
}
#[cfg(not(target_os = "redox"))]
#[inline]
pub fn getsid(pid: Option<Pid>) -> io::Result<Pid> {
backend::process::syscalls::getsid(pid)
}
#[inline]
pub fn setsid() -> io::Result<Pid> {
backend::process::syscalls::setsid()
}
pub fn getgroups() -> io::Result<Vec<Gid>> {
let mut buffer = Vec::new();
buffer.reserve(8);
buffer.resize(buffer.capacity(), Gid::ROOT);
loop {
let ngroups = backend::process::syscalls::getgroups(&mut buffer)?;
let ngroups = ngroups as usize;
assert!(ngroups <= buffer.len());
if ngroups < buffer.len() {
buffer.resize(ngroups, Gid::ROOT);
return Ok(buffer);
}
buffer.reserve(1); buffer.resize(buffer.capacity(), Gid::ROOT);
}
}
pub(crate) fn translate_fchown_args(owner: Option<Uid>, group: Option<Gid>) -> (u32, u32) {
let ow = match owner {
Some(o) => o.as_raw(),
None => u32::MAX,
};
let gr = match group {
Some(g) => g.as_raw(),
None => u32::MAX,
};
(ow, gr)
}