| 1 |
//! Configure the process resource limits. |
| 2 |
use cfg_if::cfg_if; |
| 3 |
use libc::{c_int, c_long, rusage}; |
| 4 |
|
| 5 |
use crate::errno::Errno; |
| 6 |
use crate::sys::time::TimeVal; |
| 7 |
use crate::Result; |
| 8 |
pub use libc::rlim_t; |
| 9 |
pub use libc::RLIM_INFINITY; |
| 10 |
use std::mem; |
| 11 |
|
| 12 |
cfg_if! { |
| 13 |
if #[cfg(any( |
| 14 |
all(target_os = "linux", any(target_env = "gnu", target_env = "uclibc")), |
| 15 |
target_os = "hurd" |
| 16 |
))]{ |
| 17 |
use libc::{__rlimit_resource_t, rlimit}; |
| 18 |
} else if #[cfg(any( |
| 19 |
bsd, |
| 20 |
target_os = "android", |
| 21 |
target_os = "aix", |
| 22 |
all(target_os = "linux", not(target_env = "gnu")), |
| 23 |
target_os = "cygwin" |
| 24 |
))]{ |
| 25 |
use libc::rlimit; |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
libc_enum! { |
| 30 |
/// Types of process resources. |
| 31 |
/// |
| 32 |
/// The Resource enum is platform dependent. Check different platform |
| 33 |
/// manuals for more details. Some platform links have been provided for |
| 34 |
/// easier reference (non-exhaustive). |
| 35 |
/// |
| 36 |
/// * [Linux](https://man7.org/linux/man-pages/man2/getrlimit.2.html) |
| 37 |
/// * [FreeBSD](https://www.freebsd.org/cgi/man.cgi?query=setrlimit) |
| 38 |
/// * [NetBSD](https://man.netbsd.org/setrlimit.2) |
| 39 |
|
| 40 |
// linux-gnu uses u_int as resource enum, which is implemented in libc as |
| 41 |
// well. |
| 42 |
// |
| 43 |
// https://gcc.gnu.org/legacy-ml/gcc/2015-08/msg00441.html |
| 44 |
// https://github.com/rust-lang/libc/blob/master/src/unix/linux_like/linux/gnu/mod.rs |
| 45 |
#[cfg_attr(any( |
| 46 |
all(target_os = "linux", any(target_env = "gnu", target_env = "uclibc")), |
| 47 |
target_os = "hurd" |
| 48 |
), repr(u32))] |
| 49 |
#[cfg_attr(any( |
| 50 |
bsd, |
| 51 |
target_os = "android", |
| 52 |
target_os = "aix", |
| 53 |
all(target_os = "linux", not(any(target_env = "gnu", target_env = "uclibc"))), |
| 54 |
target_os = "cygwin" |
| 55 |
), repr(i32))] |
| 56 |
#[non_exhaustive] |
| 57 |
pub enum Resource { |
| 58 |
#[cfg(not(any(target_os = "freebsd", netbsdlike)))] |
| 59 |
/// The maximum amount (in bytes) of virtual memory the process is |
| 60 |
/// allowed to map. |
| 61 |
RLIMIT_AS, |
| 62 |
/// The largest size (in bytes) core(5) file that may be created. |
| 63 |
RLIMIT_CORE, |
| 64 |
/// The maximum amount of cpu time (in seconds) to be used by each |
| 65 |
/// process. |
| 66 |
RLIMIT_CPU, |
| 67 |
/// The maximum size (in bytes) of the data segment for a process |
| 68 |
RLIMIT_DATA, |
| 69 |
/// The largest size (in bytes) file that may be created. |
| 70 |
RLIMIT_FSIZE, |
| 71 |
/// The maximum number of open files for this process. |
| 72 |
RLIMIT_NOFILE, |
| 73 |
/// The maximum size (in bytes) of the stack segment for a process. |
| 74 |
RLIMIT_STACK, |
| 75 |
|
| 76 |
#[cfg(target_os = "freebsd")] |
| 77 |
/// The maximum number of kqueues this user id is allowed to create. |
| 78 |
RLIMIT_KQUEUES, |
| 79 |
|
| 80 |
#[cfg(linux_android)] |
| 81 |
/// A limit on the combined number of flock locks and fcntl leases that |
| 82 |
/// this process may establish. |
| 83 |
RLIMIT_LOCKS, |
| 84 |
|
| 85 |
#[cfg(any(linux_android, target_os = "freebsd", netbsdlike))] |
| 86 |
/// The maximum size (in bytes) which a process may lock into memory |
| 87 |
/// using the mlock(2) system call. |
| 88 |
RLIMIT_MEMLOCK, |
| 89 |
|
| 90 |
#[cfg(linux_android)] |
| 91 |
/// A limit on the number of bytes that can be allocated for POSIX |
| 92 |
/// message queues for the real user ID of the calling process. |
| 93 |
RLIMIT_MSGQUEUE, |
| 94 |
|
| 95 |
#[cfg(linux_android)] |
| 96 |
/// A ceiling to which the process's nice value can be raised using |
| 97 |
/// setpriority or nice. |
| 98 |
RLIMIT_NICE, |
| 99 |
|
| 100 |
#[cfg(any( |
| 101 |
linux_android, |
| 102 |
target_os = "freebsd", |
| 103 |
netbsdlike, |
| 104 |
target_os = "aix", |
| 105 |
))] |
| 106 |
/// The maximum number of simultaneous processes for this user id. |
| 107 |
RLIMIT_NPROC, |
| 108 |
|
| 109 |
#[cfg(target_os = "freebsd")] |
| 110 |
/// The maximum number of pseudo-terminals this user id is allowed to |
| 111 |
/// create. |
| 112 |
RLIMIT_NPTS, |
| 113 |
|
| 114 |
#[cfg(any(linux_android, |
| 115 |
target_os = "freebsd", |
| 116 |
netbsdlike, |
| 117 |
target_os = "aix", |
| 118 |
))] |
| 119 |
/// When there is memory pressure and swap is available, prioritize |
| 120 |
/// eviction of a process' resident pages beyond this amount (in bytes). |
| 121 |
RLIMIT_RSS, |
| 122 |
|
| 123 |
#[cfg(linux_android)] |
| 124 |
/// A ceiling on the real-time priority that may be set for this process |
| 125 |
/// using sched_setscheduler and sched_set‐ param. |
| 126 |
RLIMIT_RTPRIO, |
| 127 |
|
| 128 |
#[cfg(any(target_os = "linux"))] |
| 129 |
/// A limit (in microseconds) on the amount of CPU time that a process |
| 130 |
/// scheduled under a real-time scheduling policy may con‐ sume without |
| 131 |
/// making a blocking system call. |
| 132 |
RLIMIT_RTTIME, |
| 133 |
|
| 134 |
#[cfg(linux_android)] |
| 135 |
/// A limit on the number of signals that may be queued for the real |
| 136 |
/// user ID of the calling process. |
| 137 |
RLIMIT_SIGPENDING, |
| 138 |
|
| 139 |
#[cfg(freebsdlike)] |
| 140 |
/// The maximum size (in bytes) of socket buffer usage for this user. |
| 141 |
RLIMIT_SBSIZE, |
| 142 |
|
| 143 |
#[cfg(target_os = "freebsd")] |
| 144 |
/// The maximum size (in bytes) of the swap space that may be reserved |
| 145 |
/// or used by all of this user id's processes. |
| 146 |
RLIMIT_SWAP, |
| 147 |
|
| 148 |
#[cfg(target_os = "freebsd")] |
| 149 |
/// An alias for RLIMIT_AS. |
| 150 |
RLIMIT_VMEM, |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
/// Get the current processes resource limits |
| 155 |
/// |
| 156 |
/// The special value [`RLIM_INFINITY`] indicates that no limit will be |
| 157 |
/// enforced. |
| 158 |
/// |
| 159 |
/// # Parameters |
| 160 |
/// |
| 161 |
/// * `resource`: The [`Resource`] that we want to get the limits of. |
| 162 |
/// |
| 163 |
/// # Examples |
| 164 |
/// |
| 165 |
/// ``` |
| 166 |
/// # use nix::sys::resource::{getrlimit, Resource}; |
| 167 |
/// |
| 168 |
/// let (soft_limit, hard_limit) = getrlimit(Resource::RLIMIT_NOFILE).unwrap(); |
| 169 |
/// println!("current soft_limit: {}", soft_limit); |
| 170 |
/// println!("current hard_limit: {}", hard_limit); |
| 171 |
/// ``` |
| 172 |
/// |
| 173 |
/// # References |
| 174 |
/// |
| 175 |
/// [getrlimit(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/getrlimit.html#tag_16_215) |
| 176 |
/// |
| 177 |
/// [`Resource`]: enum.Resource.html |
| 178 |
pub fn getrlimit(resource: Resource) -> Result<(rlim_t, rlim_t)> { |
| 179 |
let mut old_rlim = mem::MaybeUninit::<rlimit>::uninit(); |
| 180 |
|
| 181 |
cfg_if! { |
| 182 |
if #[cfg(any( |
| 183 |
all(target_os = "linux", any(target_env = "gnu", target_env = "uclibc")), |
| 184 |
target_os = "hurd" |
| 185 |
))] { |
| 186 |
let res = unsafe { libc::getrlimit(resource as __rlimit_resource_t, old_rlim.as_mut_ptr()) }; |
| 187 |
} else { |
| 188 |
let res = unsafe { libc::getrlimit(resource as c_int, old_rlim.as_mut_ptr()) }; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
Errno::result(res).map(|_| { |
| 193 |
let rlimit { rlim_cur, rlim_max } = unsafe { old_rlim.assume_init() }; |
| 194 |
(rlim_cur, rlim_max) |
| 195 |
}) |
| 196 |
} |
| 197 |
|
| 198 |
/// Set the current processes resource limits |
| 199 |
/// |
| 200 |
/// # Parameters |
| 201 |
/// |
| 202 |
/// * `resource`: The [`Resource`] that we want to set the limits of. |
| 203 |
/// * `soft_limit`: The value that the kernel enforces for the corresponding |
| 204 |
/// resource. |
| 205 |
/// * `hard_limit`: The ceiling for the soft limit. Must be lower or equal to |
| 206 |
/// the current hard limit for non-root users. |
| 207 |
/// |
| 208 |
/// The special value [`RLIM_INFINITY`] indicates that no limit will be |
| 209 |
/// enforced. |
| 210 |
/// |
| 211 |
/// # Examples |
| 212 |
/// |
| 213 |
/// ``` |
| 214 |
/// # use nix::sys::resource::{setrlimit, Resource}; |
| 215 |
/// |
| 216 |
/// let soft_limit = 512; |
| 217 |
/// let hard_limit = 1024; |
| 218 |
/// setrlimit(Resource::RLIMIT_NOFILE, soft_limit, hard_limit).unwrap(); |
| 219 |
/// ``` |
| 220 |
/// |
| 221 |
/// # References |
| 222 |
/// |
| 223 |
/// [setrlimit(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/getrlimit.html#tag_16_215) |
| 224 |
/// |
| 225 |
/// [`Resource`]: enum.Resource.html |
| 226 |
/// |
| 227 |
/// Note: `setrlimit` provides a safe wrapper to libc's `setrlimit`. |
| 228 |
pub fn setrlimit( |
| 229 |
resource: Resource, |
| 230 |
soft_limit: rlim_t, |
| 231 |
hard_limit: rlim_t, |
| 232 |
) -> Result<()> { |
| 233 |
let new_rlim = rlimit { |
| 234 |
rlim_cur: soft_limit, |
| 235 |
rlim_max: hard_limit, |
| 236 |
}; |
| 237 |
cfg_if! { |
| 238 |
if #[cfg(any( |
| 239 |
all(target_os = "linux", any(target_env = "gnu", target_env = "uclibc")), |
| 240 |
target_os = "hurd", |
| 241 |
))]{ |
| 242 |
let res = unsafe { libc::setrlimit(resource as __rlimit_resource_t, &new_rlim as *const rlimit) }; |
| 243 |
}else{ |
| 244 |
let res = unsafe { libc::setrlimit(resource as c_int, &new_rlim as *const rlimit) }; |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
Errno::result(res).map(drop) |
| 249 |
} |
| 250 |
|
| 251 |
libc_enum! { |
| 252 |
/// Whose resource usage should be returned by [`getrusage`]. |
| 253 |
#[repr(i32)] |
| 254 |
#[non_exhaustive] |
| 255 |
pub enum UsageWho { |
| 256 |
/// Resource usage for the current process. |
| 257 |
RUSAGE_SELF, |
| 258 |
|
| 259 |
/// Resource usage for all the children that have terminated and been waited for. |
| 260 |
RUSAGE_CHILDREN, |
| 261 |
|
| 262 |
#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd"))] |
| 263 |
/// Resource usage for the calling thread. |
| 264 |
RUSAGE_THREAD, |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
/// Output of `getrusage` with information about resource usage. Some of the fields |
| 269 |
/// may be unused in some platforms, and will be always zeroed out. See their manuals |
| 270 |
/// for details. |
| 271 |
#[repr(transparent)] |
| 272 |
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] |
| 273 |
pub struct Usage(rusage); |
| 274 |
|
| 275 |
impl AsRef<rusage> for Usage { |
| 276 |
fn as_ref(&self) -> &rusage { |
| 277 |
&self.0 |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
impl AsMut<rusage> for Usage { |
| 282 |
fn as_mut(&mut self) -> &mut rusage { |
| 283 |
&mut self.0 |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
impl Usage { |
| 288 |
/// Total amount of time spent executing in user mode. |
| 289 |
pub fn user_time(&self) -> TimeVal { |
| 290 |
TimeVal::from(self.0.ru_utime) |
| 291 |
} |
| 292 |
|
| 293 |
/// Total amount of time spent executing in kernel mode. |
| 294 |
pub fn system_time(&self) -> TimeVal { |
| 295 |
TimeVal::from(self.0.ru_stime) |
| 296 |
} |
| 297 |
|
| 298 |
/// The resident set size at its peak, |
| 299 |
#[cfg_attr(apple_targets, doc = " in bytes.")] |
| 300 |
#[cfg_attr(not(apple_targets), doc = " in kilobytes.")] |
| 301 |
pub fn max_rss(&self) -> c_long { |
| 302 |
self.0.ru_maxrss |
| 303 |
} |
| 304 |
|
| 305 |
/// Integral value expressed in kilobytes times ticks of execution indicating |
| 306 |
/// the amount of text memory shared with other processes. |
| 307 |
pub fn shared_integral(&self) -> c_long { |
| 308 |
self.0.ru_ixrss |
| 309 |
} |
| 310 |
|
| 311 |
/// Integral value expressed in kilobytes times ticks of execution indicating |
| 312 |
/// the amount of unshared memory used by data. |
| 313 |
pub fn unshared_data_integral(&self) -> c_long { |
| 314 |
self.0.ru_idrss |
| 315 |
} |
| 316 |
|
| 317 |
/// Integral value expressed in kilobytes times ticks of execution indicating |
| 318 |
/// the amount of unshared memory used for stack space. |
| 319 |
pub fn unshared_stack_integral(&self) -> c_long { |
| 320 |
self.0.ru_isrss |
| 321 |
} |
| 322 |
|
| 323 |
/// Number of page faults that were served without resorting to I/O, with pages |
| 324 |
/// that have been allocated previously by the kernel. |
| 325 |
pub fn minor_page_faults(&self) -> c_long { |
| 326 |
self.0.ru_minflt |
| 327 |
} |
| 328 |
|
| 329 |
/// Number of page faults that were served through I/O (i.e. swap). |
| 330 |
pub fn major_page_faults(&self) -> c_long { |
| 331 |
self.0.ru_majflt |
| 332 |
} |
| 333 |
|
| 334 |
/// Number of times all of the memory was fully swapped out. |
| 335 |
pub fn full_swaps(&self) -> c_long { |
| 336 |
self.0.ru_nswap |
| 337 |
} |
| 338 |
|
| 339 |
/// Number of times a read was done from a block device. |
| 340 |
pub fn block_reads(&self) -> c_long { |
| 341 |
self.0.ru_inblock |
| 342 |
} |
| 343 |
|
| 344 |
/// Number of times a write was done to a block device. |
| 345 |
pub fn block_writes(&self) -> c_long { |
| 346 |
self.0.ru_oublock |
| 347 |
} |
| 348 |
|
| 349 |
/// Number of IPC messages sent. |
| 350 |
pub fn ipc_sends(&self) -> c_long { |
| 351 |
self.0.ru_msgsnd |
| 352 |
} |
| 353 |
|
| 354 |
/// Number of IPC messages received. |
| 355 |
pub fn ipc_receives(&self) -> c_long { |
| 356 |
self.0.ru_msgrcv |
| 357 |
} |
| 358 |
|
| 359 |
/// Number of signals received. |
| 360 |
pub fn signals(&self) -> c_long { |
| 361 |
self.0.ru_nsignals |
| 362 |
} |
| 363 |
|
| 364 |
/// Number of times a context switch was voluntarily invoked. |
| 365 |
pub fn voluntary_context_switches(&self) -> c_long { |
| 366 |
self.0.ru_nvcsw |
| 367 |
} |
| 368 |
|
| 369 |
/// Number of times a context switch was imposed by the kernel (usually due to |
| 370 |
/// time slice expiring or preemption by a higher priority process). |
| 371 |
pub fn involuntary_context_switches(&self) -> c_long { |
| 372 |
self.0.ru_nivcsw |
| 373 |
} |
| 374 |
} |
| 375 |
|
| 376 |
/// Get usage information for a process, its children or the current thread |
| 377 |
/// |
| 378 |
/// Real time information can be obtained for either the current process or (in some |
| 379 |
/// systems) thread, but information about children processes is only provided for |
| 380 |
/// those that have terminated and been waited for (see [`super::wait::wait`]). |
| 381 |
/// |
| 382 |
/// Some information may be missing depending on the platform, and the way information |
| 383 |
/// is provided for children may also vary. Check the manuals for details. |
| 384 |
/// |
| 385 |
/// # References |
| 386 |
/// |
| 387 |
/// * [getrusage(2)](https://pubs.opengroup.org/onlinepubs/009696699/functions/getrusage.html) |
| 388 |
/// * [Linux](https://man7.org/linux/man-pages/man2/getrusage.2.html) |
| 389 |
/// * [FreeBSD](https://www.freebsd.org/cgi/man.cgi?query=getrusage) |
| 390 |
/// * [NetBSD](https://man.netbsd.org/getrusage.2) |
| 391 |
/// * [MacOS](https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/getrusage.2.html) |
| 392 |
/// |
| 393 |
/// [`UsageWho`]: enum.UsageWho.html |
| 394 |
/// |
| 395 |
/// Note: `getrusage` provides a safe wrapper to libc's [`libc::getrusage`]. |
| 396 |
pub fn getrusage(who: UsageWho) -> Result<Usage> { |
| 397 |
unsafe { |
| 398 |
let mut rusage = mem::MaybeUninit::<rusage>::uninit(); |
| 399 |
let res = libc::getrusage(who as c_int, rusage.as_mut_ptr()); |
| 400 |
Errno::result(res).map(|_| Usage(rusage.assume_init())) |
| 401 |
} |
| 402 |
} |