File: nofile.rs

package info (click to toggle)
rust-rlimit 0.8.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 240 kB
  • sloc: python: 142; sh: 13; makefile: 4
file content (37 lines) | stat: -rw-r--r-- 1,079 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#[cfg(unix)]
mod unix_limits {
    use std::cmp;
    use std::io;

    use rlimit::Resource;

    const DEFAULT_NOFILE_LIMIT: u64 = 16384; // or another number

    /// Try to increase NOFILE limit and return the current soft limit.
    pub fn increase_nofile_limit() -> io::Result<u64> {
        let (soft, hard) = Resource::NOFILE.get()?;
        println!("Before increasing: soft   = {}, hard = {}", soft, hard);

        let target = cmp::min(DEFAULT_NOFILE_LIMIT, hard);
        println!("Try to increase:   target = {}", target);
        Resource::NOFILE.set(target, hard)?;

        let (soft, hard) = Resource::NOFILE.get()?;
        println!("After increasing:  soft   = {}, hard = {}", soft, hard);
        Ok(soft)
    }
}

fn main() {
    #[cfg(unix)]
    {
        match unix_limits::increase_nofile_limit() {
            Ok(soft) => println!("NOFILE limit:      soft   = {}", soft),
            Err(err) => println!("Failed to increase NOFILE limit: {}", err),
        }
    }
    #[cfg(not(unix))]
    {
        println!("Do nothing on non-Unix systems");
    }
}