| 1 |
use crate::bindings as C; |
| 2 |
use crate::resource::Resource; |
| 3 |
|
| 4 |
use std::{io, mem}; |
| 5 |
|
| 6 |
/// A value indicating no limit. |
| 7 |
/// |
| 8 |
/// This constant is the minimum of the platform's `RLIM_INFINITY` and `u64::MAX`. |
| 9 |
/// On most platforms, `RLIM_INFINITY` is already `u64`, making this cast lossless. |
| 10 |
#[allow(clippy::unnecessary_cast)] |
| 11 |
pub const INFINITY: u64 = C::RLIM_INFINITY as u64; |
| 12 |
|
| 13 |
fn check_supported(resource: Resource) -> io::Result<()> { |
| 14 |
let raw_resource = resource.as_raw(); |
| 15 |
if raw_resource == u8::MAX { |
| 16 |
return Err(io::Error::new(io::ErrorKind::Other, "unsupported resource")); |
| 17 |
} |
| 18 |
Ok(()) |
| 19 |
} |
| 20 |
|
| 21 |
/// Set resource limits. |
| 22 |
/// # Errors |
| 23 |
/// \[Linux\] See <https://man7.org/linux/man-pages/man2/setrlimit.2.html> |
| 24 |
#[allow(clippy::unnecessary_min_or_max)] |
| 25 |
#[inline] |
| 26 |
pub fn setrlimit(resource: Resource, soft: u64, hard: u64) -> io::Result<()> { |
| 27 |
check_supported(resource)?; |
| 28 |
// SAFETY: Values are clamped to INFINITY, which is the maximum value representable |
| 29 |
// by the platform's rlim_t type. On platforms where rlim_t is u64, this is a no-op. |
| 30 |
// On platforms where rlim_t is smaller (e.g., u32), the min() ensures we don't |
| 31 |
// exceed the platform's maximum value before truncating. |
| 32 |
let rlim = C::rlimit { |
| 33 |
rlim_cur: soft.min(INFINITY) as _, |
| 34 |
rlim_max: hard.min(INFINITY) as _, |
| 35 |
}; |
| 36 |
#[allow(clippy::cast_lossless)] |
| 37 |
let ret = unsafe { C::setrlimit(resource.as_raw() as _, &rlim) }; |
| 38 |
if ret == 0 { |
| 39 |
Ok(()) |
| 40 |
} else { |
| 41 |
Err(io::Error::last_os_error()) |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
/// Get resource limits. |
| 46 |
/// # Errors |
| 47 |
/// \[Linux\] See <https://man7.org/linux/man-pages/man2/getrlimit.2.html> |
| 48 |
#[allow(clippy::unnecessary_min_or_max)] |
| 49 |
#[inline] |
| 50 |
pub fn getrlimit(resource: Resource) -> io::Result<(u64, u64)> { |
| 51 |
check_supported(resource)?; |
| 52 |
let mut rlim = unsafe { mem::zeroed() }; |
| 53 |
#[allow(clippy::cast_lossless)] |
| 54 |
let ret = unsafe { C::getrlimit(resource.as_raw() as _, &mut rlim) }; |
| 55 |
|
| 56 |
#[allow(clippy::unnecessary_cast)] |
| 57 |
if ret == 0 { |
| 58 |
// SAFETY: On platforms where rlim_t is u64, this cast is lossless (no-op). |
| 59 |
// On platforms where rlim_t is smaller (e.g., u32), this is a widening cast |
| 60 |
// which is always safe. The min(INFINITY) clamps to our portable maximum. |
| 61 |
let soft = (rlim.rlim_cur as u64).min(INFINITY); |
| 62 |
let hard = (rlim.rlim_max as u64).min(INFINITY); |
| 63 |
Ok((soft, hard)) |
| 64 |
} else { |
| 65 |
Err(io::Error::last_os_error()) |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
/// The type of a process ID |
| 70 |
#[allow(non_camel_case_types)] |
| 71 |
#[cfg_attr(docsrs, doc(cfg(any(target_os = "linux", target_os = "android"))))] |
| 72 |
#[cfg(any(doc, target_os = "linux", target_os = "android"))] |
| 73 |
pub type pid_t = i32; |
| 74 |
|
| 75 |
/// Set and get the resource limits of an arbitrary process. |
| 76 |
/// # Errors |
| 77 |
/// See <https://man7.org/linux/man-pages/man2/prlimit.2.html> |
| 78 |
#[allow(clippy::unnecessary_min_or_max)] |
| 79 |
#[inline] |
| 80 |
#[cfg_attr(docsrs, doc(cfg(any(target_os = "linux", target_os = "android"))))] |
| 81 |
#[cfg(any(doc, rlimit__has_prlimit64))] |
| 82 |
pub fn prlimit( |
| 83 |
pid: pid_t, |
| 84 |
resource: Resource, |
| 85 |
new_limit: Option<(u64, u64)>, |
| 86 |
old_limit: Option<(&mut u64, &mut u64)>, |
| 87 |
) -> io::Result<()> { |
| 88 |
check_supported(resource)?; |
| 89 |
|
| 90 |
// SAFETY: Values are clamped to INFINITY before casting to rlim_t. |
| 91 |
// See setrlimit() for detailed explanation of truncation safety. |
| 92 |
let new_rlim: Option<C::rlimit> = new_limit.map(|(soft, hard)| C::rlimit { |
| 93 |
rlim_cur: soft.min(INFINITY) as _, |
| 94 |
rlim_max: hard.min(INFINITY) as _, |
| 95 |
}); |
| 96 |
|
| 97 |
let new_rlimit_ptr: *const C::rlimit = match new_rlim { |
| 98 |
Some(ref rlim) => rlim, |
| 99 |
None => std::ptr::null(), |
| 100 |
}; |
| 101 |
|
| 102 |
let mut old_rlim: C::rlimit = unsafe { mem::zeroed() }; |
| 103 |
|
| 104 |
let old_rlimit_ptr: *mut C::rlimit = if old_limit.is_some() { |
| 105 |
&mut old_rlim |
| 106 |
} else { |
| 107 |
std::ptr::null_mut() |
| 108 |
}; |
| 109 |
|
| 110 |
#[allow(clippy::cast_lossless)] |
| 111 |
let ret = unsafe { C::prlimit(pid, resource.as_raw() as _, new_rlimit_ptr, old_rlimit_ptr) }; |
| 112 |
|
| 113 |
if ret == 0 { |
| 114 |
#[allow(clippy::unnecessary_cast)] |
| 115 |
if let Some((soft, hard)) = old_limit { |
| 116 |
// SAFETY: See getrlimit() for detailed explanation of cast safety. |
| 117 |
*soft = (old_rlim.rlim_cur as u64).min(INFINITY); |
| 118 |
*hard = (old_rlim.rlim_max as u64).min(INFINITY); |
| 119 |
} |
| 120 |
|
| 121 |
Ok(()) |
| 122 |
} else { |
| 123 |
Err(io::Error::last_os_error()) |
| 124 |
} |
| 125 |
} |