use super::Result;
use std::rc::Rc;
#[cfg(unix)]
type RawLimit = libc::rlim_t;
#[cfg(not(unix))]
type RawLimit = u64;
pub type Limit = RawLimit;
#[cfg(unix)]
const RLIM_INFINITY: Limit = libc::RLIM_INFINITY;
#[cfg(not(unix))]
const RLIM_INFINITY: Limit = Limit::MAX;
pub const INFINITY: Limit = RLIM_INFINITY;
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
pub enum Resource {
AS,
CORE,
CPU,
DATA,
FSIZE,
KQUEUES,
LOCKS,
MEMLOCK,
MSGQUEUE,
NICE,
NOFILE,
NPROC,
RSS,
RTPRIO,
RTTIME,
SBSIZE,
SIGPENDING,
STACK,
SWAP,
}
impl Resource {
pub const ALL: &'static [Resource] = &[
Self::AS,
Self::CORE,
Self::CPU,
Self::DATA,
Self::FSIZE,
Self::KQUEUES,
Self::LOCKS,
Self::MEMLOCK,
Self::MSGQUEUE,
Self::NICE,
Self::NOFILE,
Self::NPROC,
Self::RSS,
Self::RTPRIO,
Self::RTTIME,
Self::SBSIZE,
Self::SIGPENDING,
Self::STACK,
Self::SWAP,
];
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct LimitPair {
pub soft: Limit,
pub hard: Limit,
}
impl LimitPair {
#[must_use]
pub fn soft_exceeds_hard(&self) -> bool {
self.hard != INFINITY && (self.soft == INFINITY || self.soft > self.hard)
}
}
pub trait GetRlimit {
fn getrlimit(&self, resource: Resource) -> Result<LimitPair>;
}
impl<S: GetRlimit> GetRlimit for Rc<S> {
#[inline]
fn getrlimit(&self, resource: Resource) -> Result<LimitPair> {
(self as &S).getrlimit(resource)
}
}
pub trait SetRlimit {
fn setrlimit(&self, resource: Resource, limits: LimitPair) -> Result<()>;
}
impl<S: SetRlimit> SetRlimit for Rc<S> {
#[inline]
fn setrlimit(&self, resource: Resource, limits: LimitPair) -> Result<()> {
(self as &S).setrlimit(resource, limits)
}
}