use std::fmt;
pub const COMMON_ADMIN_GROUPS: &[&str] = &["sudo", "wheel", "admin"];
#[cfg(feature = "legacy")]
pub const SUBUID_FILE: &str = crate::os::paths::constants::PATH_SYSTEM_SUBUID_FILE;
#[cfg(feature = "legacy")]
pub const SUBGID_FILE: &str = crate::os::paths::constants::PATH_SYSTEM_SUBGID_FILE;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Uid(pub(crate) libc::uid_t);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Gid(pub(crate) libc::gid_t);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Pid(pub(crate) libc::pid_t);
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SubIdRange<T> {
pub start: T,
pub count: u32,
}
pub type SubUidRange = SubIdRange<Uid>;
pub type SubGidRange = SubIdRange<Gid>;
impl SubUidRange {
pub const fn from_raw(start: u32, count: u32) -> Self {
Self { start: Uid::from_raw(start), count }
}
pub const fn as_raw(&self) -> Option<(u32, u32)> {
Some((self.start.as_raw(), self.count))
}
pub const fn is_valid(&self) -> bool {
self.start.as_raw() > 0 && self.count > 0
}
}
impl SubGidRange {
pub const fn from_raw(start: u32, count: u32) -> Self {
Self { start: Gid::from_raw(start), count }
}
pub const fn as_raw(&self) -> Option<(u32, u32)> {
Some((self.start.as_raw(), self.count))
}
pub const fn is_valid(&self) -> bool {
self.start.as_raw() > 0 && self.count > 0
}
}
impl Uid {
pub const fn from_raw(uid: u32) -> Self {
Self(uid as libc::uid_t)
}
pub const fn as_raw(self) -> u32 {
self.0
}
pub const fn is_root(self) -> bool {
self.0 == 0
}
}
impl Gid {
pub const fn from_raw(gid: u32) -> Self {
Self(gid as libc::gid_t)
}
pub const fn as_raw(self) -> u32 {
self.0
}
}
impl Pid {
pub const fn from_raw(pid: i32) -> Self {
Self(pid as libc::pid_t)
}
pub const fn as_raw(self) -> i32 {
self.0
}
}
impl fmt::Display for Uid {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.as_raw().fmt(f)
}
}
impl fmt::Display for Gid {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.as_raw().fmt(f)
}
}
impl fmt::Display for Pid {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.as_raw().fmt(f)
}
}
pub fn getuid() -> Uid {
Uid(unsafe { libc::getuid() })
}
pub fn geteuid() -> Uid {
Uid(unsafe { libc::geteuid() })
}
pub fn getgid() -> Gid {
Gid(unsafe { libc::getgid() })
}
pub fn getegid() -> Gid {
Gid(unsafe { libc::getegid() })
}
pub fn getpid() -> Pid {
Pid(unsafe { libc::getpid() })
}
pub fn getppid() -> Pid {
Pid(unsafe { libc::getppid() })
}
pub fn invoking_uid() -> Uid {
std::env::var_os(crate::os::paths::constants::SUDO_UID_ENV)
.and_then(|s| s.to_string_lossy().parse::<u32>().ok())
.map(Uid::from_raw)
.unwrap_or_else(getuid)
}
pub fn is_root() -> bool {
getuid().is_root()
}
pub fn is_effective_root() -> bool {
geteuid().is_root()
}