Skip to content

Commit 9e63cb7

Browse files
bors[bot]Arnavion
andauthored
Merge #1668
1668: Change getrlimit and setrlimit to use rlim_t directly. r=asomers a=Arnavion Fixes #1666 Co-authored-by: Arnavion <[email protected]>
2 parents 342585c + cf628ca commit 9e63cb7

File tree

3 files changed

+23
-21
lines changed

3 files changed

+23
-21
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ This project adheres to [Semantic Versioning](https://semver.org/).
7171
- Removed the the `PATH_MAX` restriction from APIs accepting paths. Paths
7272
will now be allocated on the heap if they are too long. In addition, large
7373
instruction count improvements (~30x) were made to path handling.
74+
- Changed `getrlimit` and `setrlimit` to use `rlim_t` directly
75+
instead of `Option<rlim_t>`.
76+
(#[1668](https://github.com/nix-rust/nix/pull/1668))
7477

7578
### Fixed
7679

src/sys/resource.rs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::mem;
88

99
cfg_if! {
1010
if #[cfg(all(target_os = "linux", any(target_env = "gnu", target_env = "uclibc")))]{
11-
use libc::{__rlimit_resource_t, rlimit, RLIM_INFINITY};
11+
use libc::{__rlimit_resource_t, rlimit};
1212
} else if #[cfg(any(
1313
target_os = "freebsd",
1414
target_os = "openbsd",
@@ -19,7 +19,7 @@ cfg_if! {
1919
target_os = "dragonfly",
2020
all(target_os = "linux", not(target_env = "gnu"))
2121
))]{
22-
use libc::{c_int, rlimit, RLIM_INFINITY};
22+
use libc::{c_int, rlimit};
2323
}
2424
}
2525

@@ -173,8 +173,8 @@ libc_enum! {
173173

174174
/// Get the current processes resource limits
175175
///
176-
/// A value of `None` indicates the value equals to `RLIM_INFINITY` which means
177-
/// there is no limit.
176+
/// The special value `RLIM_INFINITY` indicates that no limit will be
177+
/// enforced.
178178
///
179179
/// # Parameters
180180
///
@@ -186,16 +186,16 @@ libc_enum! {
186186
/// # use nix::sys::resource::{getrlimit, Resource};
187187
///
188188
/// let (soft_limit, hard_limit) = getrlimit(Resource::RLIMIT_NOFILE).unwrap();
189-
/// println!("current soft_limit: {:?}", soft_limit);
190-
/// println!("current hard_limit: {:?}", hard_limit);
189+
/// println!("current soft_limit: {}", soft_limit);
190+
/// println!("current hard_limit: {}", hard_limit);
191191
/// ```
192192
///
193193
/// # References
194194
///
195195
/// [getrlimit(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/getrlimit.html#tag_16_215)
196196
///
197197
/// [`Resource`]: enum.Resource.html
198-
pub fn getrlimit(resource: Resource) -> Result<(Option<rlim_t>, Option<rlim_t>)> {
198+
pub fn getrlimit(resource: Resource) -> Result<(rlim_t, rlim_t)> {
199199
let mut old_rlim = mem::MaybeUninit::<rlimit>::uninit();
200200

201201
cfg_if! {
@@ -208,7 +208,7 @@ pub fn getrlimit(resource: Resource) -> Result<(Option<rlim_t>, Option<rlim_t>)>
208208

209209
Errno::result(res).map(|_| {
210210
let rlimit { rlim_cur, rlim_max } = unsafe { old_rlim.assume_init() };
211-
(Some(rlim_cur), Some(rlim_max))
211+
(rlim_cur, rlim_max)
212212
})
213213
}
214214

@@ -218,21 +218,20 @@ pub fn getrlimit(resource: Resource) -> Result<(Option<rlim_t>, Option<rlim_t>)>
218218
///
219219
/// * `resource`: The [`Resource`] that we want to set the limits of.
220220
/// * `soft_limit`: The value that the kernel enforces for the corresponding
221-
/// resource. Note: `None` input will be replaced by constant `RLIM_INFINITY`.
221+
/// resource.
222222
/// * `hard_limit`: The ceiling for the soft limit. Must be lower or equal to
223-
/// the current hard limit for non-root users. Note: `None` input will be
224-
/// replaced by constant `RLIM_INFINITY`.
223+
/// the current hard limit for non-root users.
225224
///
226-
/// > Note: for some os (linux_gnu), setting hard_limit to `RLIM_INFINITY` can
227-
/// > results `EPERM` Error. So you will need to set the number explicitly.
225+
/// The special value `RLIM_INFINITY` indicates that no limit will be
226+
/// enforced.
228227
///
229228
/// # Examples
230229
///
231230
/// ```
232231
/// # use nix::sys::resource::{setrlimit, Resource};
233232
///
234-
/// let soft_limit = Some(512);
235-
/// let hard_limit = Some(1024);
233+
/// let soft_limit = 512;
234+
/// let hard_limit = 1024;
236235
/// setrlimit(Resource::RLIMIT_NOFILE, soft_limit, hard_limit).unwrap();
237236
/// ```
238237
///
@@ -245,12 +244,12 @@ pub fn getrlimit(resource: Resource) -> Result<(Option<rlim_t>, Option<rlim_t>)>
245244
/// Note: `setrlimit` provides a safe wrapper to libc's `setrlimit`.
246245
pub fn setrlimit(
247246
resource: Resource,
248-
soft_limit: Option<rlim_t>,
249-
hard_limit: Option<rlim_t>,
247+
soft_limit: rlim_t,
248+
hard_limit: rlim_t,
250249
) -> Result<()> {
251250
let new_rlim = rlimit {
252-
rlim_cur: soft_limit.unwrap_or(RLIM_INFINITY),
253-
rlim_max: hard_limit.unwrap_or(RLIM_INFINITY),
251+
rlim_cur: soft_limit,
252+
rlim_max: hard_limit,
254253
};
255254
cfg_if! {
256255
if #[cfg(all(target_os = "linux", any(target_env = "gnu", target_env = "uclibc")))]{

test/test_resource.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ use nix::sys::resource::{getrlimit, setrlimit, Resource};
1212
#[test]
1313
#[cfg(not(any(target_os = "redox", target_os = "fuchsia", target_os = "illumos")))]
1414
pub fn test_resource_limits_nofile() {
15-
let (soft_limit, hard_limit) = getrlimit(Resource::RLIMIT_NOFILE).unwrap();
15+
let (mut soft_limit, hard_limit) = getrlimit(Resource::RLIMIT_NOFILE).unwrap();
1616

17-
let soft_limit = Some(soft_limit.map_or(1024, |v| v - 1));
17+
soft_limit -= 1;
1818
assert_ne!(soft_limit, hard_limit);
1919
setrlimit(Resource::RLIMIT_NOFILE, soft_limit, hard_limit).unwrap();
2020

0 commit comments

Comments
 (0)