Skip to content

Commit 8b43093

Browse files
committed
Add getrlimit(2) and setrlimit(2)
1 parent 5fa5fd4 commit 8b43093

File tree

4 files changed

+79
-0
lines changed

4 files changed

+79
-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: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
}

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)