1#[cfg(not(any(
2 target_os = "redox",
3 target_os = "fuchsia",
4 target_os = "illumos",
5 target_os = "haiku"
6)))]
7use nix::sys::resource::{getrlimit, setrlimit, Resource};
8
9/// Tests the RLIMIT_NOFILE functionality of getrlimit(), where the resource RLIMIT_NOFILE refers
10/// to the maximum file descriptor number that can be opened by the process (aka the maximum number
11/// of file descriptors that the process can open, since Linux 4.5).
12///
13/// We first fetch the existing file descriptor maximum values using getrlimit(), then edit the
14/// soft limit to make sure it has a new and distinct value to the hard limit. We then setrlimit()
15/// to put the new soft limit in effect, and then getrlimit() once more to ensure the limits have
16/// been updated.
17#[test]
18#[cfg(not(any(
19 target_os = "redox",
20 target_os = "fuchsia",
21 target_os = "illumos",
22 target_os = "haiku"
23)))]
24pub fn test_resource_limits_nofile() {
25 let (mut soft_limit, hard_limit) =
26 getrlimit(Resource::RLIMIT_NOFILE).unwrap();
27
28 soft_limit -= 1;
29 assert_ne!(soft_limit, hard_limit);
30 setrlimit(Resource::RLIMIT_NOFILE, soft_limit, hard_limit).unwrap();
31
32 let (new_soft_limit, _) = getrlimit(Resource::RLIMIT_NOFILE).unwrap();
33 assert_eq!(new_soft_limit, soft_limit);
34}
35