rlimit/lib.rs
1//! rlimit - Resource limits.
2//!
3//! # Examples
4//!
5//! ## Set resource limit
6//! ```no_run
7//! # #[cfg(unix)]
8//! # {
9//! use rlimit::{setrlimit, Resource};
10//!
11//! const DEFAULT_SOFT_LIMIT: u64 = 4 * 1024 * 1024;
12//! const DEFAULT_HARD_LIMIT: u64 = 8 * 1024 * 1024;
13//! assert!(Resource::FSIZE.set(DEFAULT_SOFT_LIMIT, DEFAULT_HARD_LIMIT).is_ok());
14//!
15//! let soft = 16384;
16//! let hard = soft * 2;
17//! assert!(setrlimit(Resource::NOFILE, soft, hard).is_ok());
18//! # }
19//! ```
20//!
21//! ## Get resource limit
22//! ```no_run
23//! # #[cfg(unix)]
24//! # {
25//! use rlimit::{getrlimit, Resource};
26//!
27//! assert!(Resource::NOFILE.get().is_ok());
28//! assert_eq!(getrlimit(Resource::CPU).unwrap(), (rlimit::INFINITY, rlimit::INFINITY));
29//! # }
30//! ```
31//!
32//! ## Increase NOFILE limit
33//! See the example [nofile](https://github.com/Nugine/rlimit/tree/v0.6.2/examples/nofile.rs).
34//!
35//! You can also use the tools in [`rlimit::utils`][`crate::utils`].
36//!
37//! ```no_run
38//! use rlimit::utils::increase_nofile_limit;
39//! increase_nofile_limit(10240).unwrap();
40//! increase_nofile_limit(u64::MAX).unwrap();
41//! ```
42//!
43//! # Troubleshoot
44//!
45//! ## Failed to increase NOFILE to hard limit on macOS
46//! On macOS, getrlimit by default reports that the hard limit is
47//! unlimited, but there is usually a stricter hard limit discoverable
48//! via sysctl (`kern.maxfilesperproc`). Failing to discover this secret stricter hard limit will
49//! cause the call to setrlimit to fail.
50//!
51//! [`rlimit::utils::increase_nofile_limit`][`crate::utils::increase_nofile_limit`]
52//! respects `kern.maxfilesperproc`.
53//!
54
55#![deny(
56 missing_docs,
57 missing_debug_implementations,
58 clippy::all,
59 clippy::pedantic,
60 clippy::nursery,
61 clippy::cargo
62)]
63
64#[allow(unused_macros)]
65macro_rules! group {
66 ($($tt:tt)*) => {
67 $($tt)*
68 }
69}
70
71#[cfg(unix)]
72group! {
73 mod unix;
74
75 #[doc(inline)]
76 pub use self::unix::*;
77}
78
79pub mod utils;