limits_rs/
lib.rs

1//! Utilities for determining the limits that an operating system enforces on a given particular
2//! process.
3//!
4//! In its current implementation, this crate allows convenient read of the `/proc/<pid>/limits`
5//! file on GNU/Linux. On any other platform, the provided methods will return an error so that the
6//! user can decide what to do in the absence of information about limits.
7//!
8//! Support for other operating systems and platforms may be added on demand.
9
10use thiserror::Error;
11
12// Support for GNU/Linux
13#[cfg(target_os = "linux")]
14mod linux;
15#[cfg(target_os = "linux")]
16pub use crate::linux::*;
17
18// Placeholder for all other platforms
19#[cfg(not(target_os = "linux"))]
20mod default;
21#[cfg(not(target_os = "linux"))]
22pub use crate::default::*;
23
24/// All methods that can fail in this crate should return `Result<_, Error>`. That is, one of the
25/// variants herein.
26#[derive(Debug, Error)]
27pub enum Error {
28    #[error("Unsupported OS. Could not get process limits.")]
29    UnsupportedOS,
30    #[error("Proc file not found at `{}`: {}", .0, .1)]
31    ProcFileNotFound(String, #[source] std::io::Error),
32}
33
34/// Get the limits for the process in which we are running (our own process id).
35pub fn get_own_limits() -> Result<Limits, crate::Error> {
36    let own_pid = std::process::id();
37
38    get_pid_limits(own_pid)
39}