Skip to content

Commit 9bdb800

Browse files
committed
Add getrlimit(2) and setrlimit(2)
1 parent 5fa5fd4 commit 9bdb800

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

src/sys/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ pub mod memfd;
1515
#[cfg(not(any(target_os = "ios", target_os = "freebsd", target_os = "dragonfly")))]
1616
pub mod ioctl;
1717

18+
pub mod resource;
19+
1820
#[cfg(any(target_os = "linux", target_os = "android"))]
1921
pub mod sendfile;
2022

src/sys/resource.rs

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

test/test.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ mod sys;
1111
mod test_fcntl;
1212
mod test_net;
1313
mod test_nix_path;
14+
mod test_resource;
1415
#[cfg(any(target_os = "linux", target_os = "android"))]
1516
mod test_sendfile;
1617
mod test_stat;

test/test_resource.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use nix::sys::resource::{Resource, getrlimit, setrlimit};
2+
3+
#[test]
4+
pub fn test_resource_limits() {
5+
let mut limit = getrlimit(Resource::RLIMIT_STACK).unwrap();
6+
assert!(limit.rlim_cur != limit.rlim_max);
7+
8+
let orig_limit = limit;
9+
10+
limit.rlim_cur = limit.rlim_max;
11+
setrlimit(Resource::RLIMIT_STACK, limit).unwrap();
12+
13+
let limit2 = getrlimit(Resource::RLIMIT_STACK).unwrap();
14+
assert_eq!(limit.rlim_cur, limit2.rlim_cur);
15+
assert_eq!(limit.rlim_max, limit2.rlim_max);
16+
17+
setrlimit(Resource::RLIMIT_STACK, orig_limit).unwrap();
18+
19+
let final_limit = getrlimit(Resource::RLIMIT_STACK).unwrap();
20+
assert_eq!(orig_limit.rlim_cur, final_limit.rlim_cur);
21+
assert_eq!(orig_limit.rlim_max, final_limit.rlim_max);
22+
}

0 commit comments

Comments
 (0)