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