#![allow(unsafe_code)]
use crate::{backend, io};
#[cfg(any(target_os = "android", target_os = "linux"))]
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(any(target_os = "android", target_os = "linux"))]
#[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(any(target_os = "android", target_os = "linux"))]
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]
#[must_use]
pub fn getpgrp() -> Pid {
backend::process::syscalls::getpgrp()
}
#[inline]
pub fn setsid() -> io::Result<Pid> {
backend::process::syscalls::setsid()
}
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)
}