#![allow(unsafe_code)]
use crate::imp;
#[cfg(any(linux_raw, all(libc, any(target_os = "android", target_os = "linux"))))]
use imp::process::RawCpuid;
pub use imp::process::RawUid;
pub use imp::process::RawGid;
pub use imp::process::RawPid;
pub use imp::process::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(linux_raw, all(libc, 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(linux_raw, all(libc, 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 {
imp::process::syscalls::getuid()
}
#[inline]
#[must_use]
pub fn geteuid() -> Uid {
imp::process::syscalls::geteuid()
}
#[inline]
#[must_use]
pub fn getgid() -> Gid {
imp::process::syscalls::getgid()
}
#[inline]
#[must_use]
pub fn getegid() -> Gid {
imp::process::syscalls::getegid()
}
#[inline]
#[must_use]
pub fn getpid() -> Pid {
imp::process::syscalls::getpid()
}
#[inline]
#[must_use]
pub fn getppid() -> Option<Pid> {
imp::process::syscalls::getppid()
}