Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change getrlimit and setrlimit to use rlim_t directly. #1668

Merged
merged 1 commit into from
Mar 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<rlim_t>`.
(#[1668](https://github.com/nix-rust/nix/pull/1668))

### Fixed

Expand Down
37 changes: 18 additions & 19 deletions src/sys/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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};
}
}

Expand Down Expand Up @@ -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
///
Expand All @@ -186,16 +186,16 @@ 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
///
/// [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<rlim_t>, Option<rlim_t>)> {
pub fn getrlimit(resource: Resource) -> Result<(rlim_t, rlim_t)> {
let mut old_rlim = mem::MaybeUninit::<rlimit>::uninit();

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

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)
})
}

Expand All @@ -218,21 +218,20 @@ pub fn getrlimit(resource: Resource) -> Result<(Option<rlim_t>, Option<rlim_t>)>
///
/// * `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();
/// ```
///
Expand All @@ -245,12 +244,12 @@ pub fn getrlimit(resource: Resource) -> Result<(Option<rlim_t>, Option<rlim_t>)>
/// Note: `setrlimit` provides a safe wrapper to libc's `setrlimit`.
pub fn setrlimit(
resource: Resource,
soft_limit: Option<rlim_t>,
hard_limit: Option<rlim_t>,
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")))]{
Expand Down
4 changes: 2 additions & 2 deletions test/test_resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down