|
| 1 | +use std::mem; |
| 2 | + |
| 3 | +use libc::{self, c_int}; |
| 4 | +pub use libc::{rlimit, RLIM_INFINITY}; |
| 5 | + |
| 6 | +use {Errno, Result}; |
| 7 | + |
| 8 | +#[repr(i32)] |
| 9 | +pub enum Resource { |
| 10 | + // POSIX |
| 11 | + RLIMIT_CORE = libc::RLIMIT_CORE, |
| 12 | + RLIMIT_CPU = libc::RLIMIT_CPU, |
| 13 | + RLIMIT_DATA = libc::RLIMIT_DATA, |
| 14 | + RLIMIT_FSIZE = libc::RLIMIT_FSIZE, |
| 15 | + RLIMIT_NOFILE = libc::RLIMIT_NOFILE, |
| 16 | + RLIMIT_STACK = libc::RLIMIT_STACK, |
| 17 | + RLIMIT_AS = libc::RLIMIT_AS, |
| 18 | + // BSDs and Linux |
| 19 | + #[cfg(all(unix, not(target_os = "solaris")))] |
| 20 | + RLIMIT_MEMLOCK = libc::RLIMIT_MEMLOCK, |
| 21 | + #[cfg(all(unix, not(target_os = "solaris")))] |
| 22 | + RLIMIT_NPROC = libc::RLIMIT_NPROC, |
| 23 | + #[cfg(all(unix, not(target_os = "solaris")))] |
| 24 | + RLIMIT_RSS = libc::RLIMIT_RSS, |
| 25 | + // Linux-only |
| 26 | + #[cfg(any(target_os = "linux", target_os = "android"))] |
| 27 | + RLIMIT_LOCKS = libc::RLIMIT_LOCKS, |
| 28 | + #[cfg(any(target_os = "linux", target_os = "android"))] |
| 29 | + RLIMIT_MSGQUEUE = libc::RLIMIT_MSGQUEUE, |
| 30 | + #[cfg(any(target_os = "linux", target_os = "android"))] |
| 31 | + RLIMIT_NICE = libc::RLIMIT_NICE, |
| 32 | + #[cfg(any(target_os = "linux", target_os = "android"))] |
| 33 | + RLIMIT_RTPRIO = libc::RLIMIT_RTPRIO, |
| 34 | + #[cfg(any(target_os = "linux", target_os = "android"))] |
| 35 | + RLIMIT_RTTIME = libc::RLIMIT_RTTIME, |
| 36 | + #[cfg(any(target_os = "linux", target_os = "android"))] |
| 37 | + RLIMIT_SIGPENDING = libc::RLIMIT_SIGPENDING, |
| 38 | +} |
| 39 | + |
| 40 | +pub fn getrlimit(resource: Resource) -> Result<rlimit> { |
| 41 | + let mut rlim = unsafe { mem::uninitialized() }; |
| 42 | + let res = unsafe { libc::getrlimit(resource as c_int, &mut rlim as *mut _) }; |
| 43 | + Errno::result(res).map(|_| rlim) |
| 44 | +} |
| 45 | + |
| 46 | +pub fn setrlimit(resource: Resource, rlim: rlimit) -> Result<()> { |
| 47 | + let res = unsafe { libc::setrlimit(resource as c_int, &rlim as *const _) }; |
| 48 | + Errno::result(res).map(drop) |
| 49 | +} |
0 commit comments