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.8.3/examples/nofile.rs).
34//!
35//! You can also use the tool function showed below:
36//!
37//! ```no_run
38//! rlimit::increase_nofile_limit(10240).unwrap();
39//! rlimit::increase_nofile_limit(u64::MAX).unwrap();
40//! ```
41//!
42//! ## Windows
43//!
44//! Windows does not have Unix-like resource limits.
45//! It only supports changing the number of simultaneously open files currently permitted at the stdio level.
46//!
47//! See the official documentation of
48//! [`_getmaxstdio`](https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/getmaxstdio?view=msvc-170)
49//! and
50//! [`_setmaxstdio`](https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/setmaxstdio?view=msvc-170).
51//!
52//! ```no_run
53//! # #[cfg(windows)]
54//! # {
55//! println!("{}", rlimit::getmaxstdio()); // 512
56//! rlimit::setmaxstdio(2048).unwrap();
57//! println!("{}", rlimit::getmaxstdio()); // 2048
58//! # }
59//! ```
60//!
61//! # Troubleshoot
62//!
63//! ## Failed to increase NOFILE to hard limit on macOS
64//! On macOS, getrlimit by default reports that the hard limit is
65//! unlimited, but there is usually a stricter hard limit discoverable
66//! via sysctl (`kern.maxfilesperproc`). Failing to discover this secret stricter hard limit will
67//! cause the call to setrlimit to fail.
68//!
69//! [`rlimit::increase_nofile_limit`][`crate::increase_nofile_limit`]
70//! respects `kern.maxfilesperproc`.
71//!
72
73#![cfg_attr(docsrs, feature(doc_cfg))]
74#![deny(
75 missing_docs,
76 missing_debug_implementations,
77 clippy::all,
78 clippy::pedantic,
79 clippy::nursery,
80 clippy::cargo
81)]
82
83#[allow(unused_macros)]
84macro_rules! group {
85 ($($item:item)*) => {
86 $($item)*
87 }
88}
89
90#[cfg(any(doc, windows))]
91group! {
92 mod windows;
93
94 #[doc(inline)]
95 pub use self::windows::*;
96}
97
98#[cfg(any(doc, unix))]
99group! {
100 mod bindings;
101
102 mod unix;
103 mod resource;
104
105 #[doc(inline)]
106 pub use self::unix::*;
107
108 #[doc(inline)]
109 pub use self::resource::Resource;
110}
111
112#[cfg(any(doc, target_os = "linux"))]
113group! {
114 mod proc_limits;
115
116 #[doc(inline)]
117 pub use self::proc_limits::*;
118}
119
120mod tools;
121#[doc(inline)]
122pub use self::tools::*;