From cf628ca60907cbd50dfc30286b3ed3cdb6560ab2 Mon Sep 17 00:00:00 2001 From: Arnavion Date: Sun, 13 Mar 2022 17:33:02 -0700 Subject: Change getrlimit and setrlimit to use rlim_t directly. Fixes #1666 --- CHANGELOG.md | 3 +++ src/sys/resource.rs | 37 ++++++++++++++++++------------------- test/test_resource.rs | 4 ++-- 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e2e06b2..7f59926f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -71,6 +71,9 @@ This project adheres to [Semantic Versioning](https://semver.org/). - Removed the the `PATH_MAX` restriction from APIs accepting paths. Paths will now be allocated on the heap if they are too long. In addition, large instruction count improvements (~30x) were made to path handling. +- Changed `getrlimit` and `setrlimit` to use `rlim_t` directly + instead of `Option`. + (#[1668](https://github.com/nix-rust/nix/pull/1668)) ### Fixed diff --git a/src/sys/resource.rs b/src/sys/resource.rs index ba206aa2..fca7418b 100644 --- a/src/sys/resource.rs +++ b/src/sys/resource.rs @@ -8,7 +8,7 @@ use std::mem; cfg_if! { if #[cfg(all(target_os = "linux", any(target_env = "gnu", target_env = "uclibc")))]{ - use libc::{__rlimit_resource_t, rlimit, RLIM_INFINITY}; + use libc::{__rlimit_resource_t, rlimit}; } else if #[cfg(any( target_os = "freebsd", target_os = "openbsd", @@ -19,7 +19,7 @@ cfg_if! { target_os = "dragonfly", all(target_os = "linux", not(target_env = "gnu")) ))]{ - use libc::{c_int, rlimit, RLIM_INFINITY}; + use libc::{c_int, rlimit}; } } @@ -173,8 +173,8 @@ libc_enum! { /// Get the current processes resource limits /// -/// A value of `None` indicates the value equals to `RLIM_INFINITY` which means -/// there is no limit. +/// The special value `RLIM_INFINITY` indicates that no limit will be +/// enforced. /// /// # Parameters /// @@ -186,8 +186,8 @@ libc_enum! { /// # use nix::sys::resource::{getrlimit, Resource}; /// /// let (soft_limit, hard_limit) = getrlimit(Resource::RLIMIT_NOFILE).unwrap(); -/// println!("current soft_limit: {:?}", soft_limit); -/// println!("current hard_limit: {:?}", hard_limit); +/// println!("current soft_limit: {}", soft_limit); +/// println!("current hard_limit: {}", hard_limit); /// ``` /// /// # References @@ -195,7 +195,7 @@ libc_enum! { /// [getrlimit(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/getrlimit.html#tag_16_215) /// /// [`Resource`]: enum.Resource.html -pub fn getrlimit(resource: Resource) -> Result<(Option, Option)> { +pub fn getrlimit(resource: Resource) -> Result<(rlim_t, rlim_t)> { let mut old_rlim = mem::MaybeUninit::::uninit(); cfg_if! { @@ -208,7 +208,7 @@ pub fn getrlimit(resource: Resource) -> Result<(Option, Option)> Errno::result(res).map(|_| { let rlimit { rlim_cur, rlim_max } = unsafe { old_rlim.assume_init() }; - (Some(rlim_cur), Some(rlim_max)) + (rlim_cur, rlim_max) }) } @@ -218,21 +218,20 @@ pub fn getrlimit(resource: Resource) -> Result<(Option, Option)> /// /// * `resource`: The [`Resource`] that we want to set the limits of. /// * `soft_limit`: The value that the kernel enforces for the corresponding -/// resource. Note: `None` input will be replaced by constant `RLIM_INFINITY`. +/// resource. /// * `hard_limit`: The ceiling for the soft limit. Must be lower or equal to -/// the current hard limit for non-root users. Note: `None` input will be -/// replaced by constant `RLIM_INFINITY`. +/// the current hard limit for non-root users. /// -/// > Note: for some os (linux_gnu), setting hard_limit to `RLIM_INFINITY` can -/// > results `EPERM` Error. So you will need to set the number explicitly. +/// The special value `RLIM_INFINITY` indicates that no limit will be +/// enforced. /// /// # Examples /// /// ``` /// # use nix::sys::resource::{setrlimit, Resource}; /// -/// let soft_limit = Some(512); -/// let hard_limit = Some(1024); +/// let soft_limit = 512; +/// let hard_limit = 1024; /// setrlimit(Resource::RLIMIT_NOFILE, soft_limit, hard_limit).unwrap(); /// ``` /// @@ -245,12 +244,12 @@ pub fn getrlimit(resource: Resource) -> Result<(Option, Option)> /// Note: `setrlimit` provides a safe wrapper to libc's `setrlimit`. pub fn setrlimit( resource: Resource, - soft_limit: Option, - hard_limit: Option, + soft_limit: rlim_t, + hard_limit: rlim_t, ) -> Result<()> { let new_rlim = rlimit { - rlim_cur: soft_limit.unwrap_or(RLIM_INFINITY), - rlim_max: hard_limit.unwrap_or(RLIM_INFINITY), + rlim_cur: soft_limit, + rlim_max: hard_limit, }; cfg_if! { if #[cfg(all(target_os = "linux", any(target_env = "gnu", target_env = "uclibc")))]{ diff --git a/test/test_resource.rs b/test/test_resource.rs index 59697500..c89d601e 100644 --- a/test/test_resource.rs +++ b/test/test_resource.rs @@ -12,9 +12,9 @@ use nix::sys::resource::{getrlimit, setrlimit, Resource}; #[test] #[cfg(not(any(target_os = "redox", target_os = "fuchsia", target_os = "illumos")))] pub fn test_resource_limits_nofile() { - let (soft_limit, hard_limit) = getrlimit(Resource::RLIMIT_NOFILE).unwrap(); + let (mut soft_limit, hard_limit) = getrlimit(Resource::RLIMIT_NOFILE).unwrap(); - let soft_limit = Some(soft_limit.map_or(1024, |v| v - 1)); + soft_limit -= 1; assert_ne!(soft_limit, hard_limit); setrlimit(Resource::RLIMIT_NOFILE, soft_limit, hard_limit).unwrap(); -- cgit v1.2.3