Skip to content

Commit

Permalink
Address reviewer's comment
Browse files Browse the repository at this point in the history
  • Loading branch information
LMJW committed Oct 9, 2020
1 parent e5eefe9 commit c7eb876
Showing 1 changed file with 81 additions and 111 deletions.
192 changes: 81 additions & 111 deletions src/sys/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,111 +5,98 @@ use crate::errno::Errno;
use crate::Result;

pub use libc::rlim_t;

cfg_if! {
if #[cfg(all(target_os = "linux", target_env = "gnu"))]{
use libc::{__rlimit_resource_t, rlimit, RLIM_INFINITY};
libc_enum!{
#[repr(u32)]
pub enum Resource {
/// See detail of each Resource https://man7.org/linux/man-pages/man2/getrlimit.2.html
RLIMIT_AS,
RLIMIT_CORE,
RLIMIT_CPU,
RLIMIT_DATA,
RLIMIT_FSIZE,
RLIMIT_LOCKS,
RLIMIT_MEMLOCK,
RLIMIT_MSGQUEUE,
RLIMIT_NICE,
RLIMIT_NOFILE,
RLIMIT_NPROC,
RLIMIT_RSS,
RLIMIT_RTPRIO,
RLIMIT_RTTIME,
RLIMIT_SIGPENDING,
RLIMIT_STACK,
}
}
}else if #[cfg(any(
target_os = "freebsd",
target_os = "openbsd",
target_os = "netbsd",
target_os = "macos",
target_os = "ios",
target_os = "android",
target_os = "dragonfly",
target_os = "bitrig",
target_os = "linux", // target_env != "gnu"
))]{
use crate::Error;
}else{
use libc::{c_int, rlimit, RLIM_INFINITY};
}
}

libc_enum! {
#[repr(i32)]
pub enum Resource {
/// See detail of each Resource https://man7.org/linux/man-pages/man2/getrlimit.2.html
/// BSD specific Resource https://www.freebsd.org/cgi/man.cgi?query=setrlimit
#[cfg(not(any(target_os = "netbsd", target_os = "freebsd")))]
RLIMIT_AS,
RLIMIT_CORE,
RLIMIT_CPU,
RLIMIT_DATA,
RLIMIT_FSIZE,
RLIMIT_NOFILE,
RLIMIT_STACK,
libc_enum! {
/// The Resource enum is platform dependent. Check different platform
/// manuals for more details. Some platform links has been provided for
/// earier reference (non-exhaustive).
///
/// * [Linux](https://man7.org/linux/man-pages/man2/getrlimit.2.html)
/// * [freeBSD](https://www.freebsd.org/cgi/man.cgi?query=setrlimit)
// linux-gnu uses u_int as resource enum, which is implemented in libc as
// well.
//
// https://gcc.gnu.org/legacy-ml/gcc/2015-08/msg00441.html
// https://github.com/rust-lang/libc/blob/master/src/unix/linux_like/linux/gnu/mod.rs
#[cfg_attr(all(target_os = "linux", target_env = "gnu"), repr(u32))]
#[cfg_attr(any(
target_os = "freebsd",
target_os = "openbsd",
target_os = "netbsd",
target_os = "macos",
target_os = "ios",
target_os = "android",
target_os = "dragonfly",
all(target_os = "linux", not(target_env = "gnu"))
), repr(i32))]
pub enum Resource {
#[cfg(not(any(target_os = "netbsd", target_os = "freebsd")))]
RLIMIT_AS,
RLIMIT_CORE,
RLIMIT_CPU,
RLIMIT_DATA,
RLIMIT_FSIZE,
RLIMIT_NOFILE,
RLIMIT_STACK,

// platform specific
#[cfg(target_os = "freebsd")]
RLIMIT_KQUEUES,
// platform specific
#[cfg(target_os = "freebsd")]
RLIMIT_KQUEUES,

#[cfg(any(target_os = "android", target_os = "linux"))]
RLIMIT_LOCKS,
#[cfg(any(target_os = "android", target_os = "linux"))]
RLIMIT_LOCKS,

#[cfg(any(target_os = "android", target_os = "freebsd", target_os = "openbsd", target_os = "linux"))]
RLIMIT_MEMLOCK,
#[cfg(any(target_os = "android", target_os = "freebsd", target_os = "openbsd", target_os = "linux"))]
RLIMIT_MEMLOCK,

#[cfg(any(target_os = "android", target_os = "linux"))]
RLIMIT_MSGQUEUE,
#[cfg(any(target_os = "android", target_os = "linux"))]
RLIMIT_MSGQUEUE,

#[cfg(any(target_os = "android", target_os = "linux"))]
RLIMIT_NICE,
#[cfg(any(target_os = "android", target_os = "linux"))]
RLIMIT_NICE,

#[cfg(any(target_os = "android", target_os = "freebsd", target_os = "openbsd", target_os = "linux"))]
RLIMIT_NPROC,
#[cfg(any(target_os = "android", target_os = "freebsd", target_os = "openbsd", target_os = "linux"))]
RLIMIT_NPROC,

#[cfg(target_os = "freebsd")]
RLIMIT_NPTS,
#[cfg(target_os = "freebsd")]
RLIMIT_NPTS,

#[cfg(any(target_os = "android", target_os = "freebsd", target_os = "openbsd", target_os = "linux"))]
RLIMIT_RSS,
#[cfg(any(target_os = "android", target_os = "freebsd", target_os = "openbsd", target_os = "linux"))]
RLIMIT_RSS,

#[cfg(any(target_os = "android", target_os = "linux"))]
RLIMIT_RTPRIO,
#[cfg(any(target_os = "android", target_os = "linux"))]
RLIMIT_RTPRIO,

#[cfg(any(target_os = "linux"))]
RLIMIT_RTTIME,
#[cfg(any(target_os = "linux"))]
RLIMIT_RTTIME,

#[cfg(any(target_os = "android", target_os = "linux"))]
RLIMIT_SIGPENDING,
#[cfg(any(target_os = "android", target_os = "linux"))]
RLIMIT_SIGPENDING,

#[cfg(any(target_os = "freebsd", target_os = "dragonfly"))]
RLIMIT_SBSIZE,
#[cfg(any(target_os = "freebsd", target_os = "dragonfly"))]
RLIMIT_SBSIZE,

#[cfg(target_os = "freebsd")]
RLIMIT_SWAP,
#[cfg(target_os = "freebsd")]
RLIMIT_SWAP,

#[cfg(target_os = "freebsd")]
RLIMIT_VMEM,
}
}
}else{
// unkown os
#[cfg(target_os = "freebsd")]
RLIMIT_VMEM,
}
}

/// Get the current processes resource limits
///
/// A value of `None` indicates that there's no limit.
/// A value of `None` indicates the value equals to `RLIM_INFINITY` which means
/// there is no limit.
///
/// # Parameters
///
Expand All @@ -127,7 +114,7 @@ cfg_if! {
///
/// # References
///
/// [getrlimit(2)](https://linux.die.net/man/2/getrlimit)
/// [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>)> {
Expand All @@ -139,17 +126,7 @@ pub fn getrlimit(resource: Resource) -> Result<(Option<rlim_t>, Option<rlim_t>)>
cfg_if! {
if #[cfg(all(target_os = "linux", target_env = "gnu"))]{
let res = unsafe { libc::getrlimit(resource as __rlimit_resource_t, &mut old_rlim) };
}else if #[cfg(any(
target_os = "freebsd",
target_os = "openbsd",
target_os = "netbsd",
target_os = "macos",
target_os = "ios",
target_os = "android",
target_os = "dragonfly",
target_os = "bitrig",
target_os = "linux", // target_env != "gnu"
))]{
}else{
let res = unsafe { libc::getrlimit(resource as c_int, &mut old_rlim) };
}
}
Expand All @@ -164,8 +141,6 @@ pub fn getrlimit(resource: Resource) -> Result<(Option<rlim_t>, Option<rlim_t>)>

/// Set the current processes resource limits
///
/// A value of `None` indicates that there's no limit.
///
/// # Parameters
///
/// * `resource`: The [`Resource`] that we want to set the limits of.
Expand All @@ -175,19 +150,22 @@ pub fn getrlimit(resource: Resource) -> Result<(Option<rlim_t>, Option<rlim_t>)>
/// the current hard limit for non-root users. Note: `None` input will be
/// replaced by constant `RLIM_INFINITY`.
///
/// > 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.
///
/// # Examples
///
/// ```no_run
/// ```
/// # use nix::sys::resource::{setrlimit, Resource};
///
/// let soft_limit = Some(1024);
/// let hard_limit = None;
/// let hard_limit = Some(1048576);
/// setrlimit(Resource::RLIMIT_NOFILE, soft_limit, hard_limit).unwrap();
/// ```
///
/// # References
///
/// [setrlimit(2)](https://linux.die.net/man/2/setrlimit)
/// [setrlimit(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/getrlimit.html#tag_16_215)
///
/// [`Resource`]: enum.Resource.html
pub fn setrlimit(
Expand All @@ -206,22 +184,14 @@ pub fn setrlimit(
// seems for some of the architectures, we prefer to use prlimit instead of {g,s}etrlimit
let res = unsafe { libc::prlimit(0, resource as __rlimit_resource_t, &new_rlim as *const _, std::ptr::null_mut()) };
if res == -1 {
return Errno::result(res).map(|_| ());
match Errno::last() {
Errno::ENOSYS =>{}
e => {return Err(Error::Sys(e));}
}
}

let res = unsafe { libc::setrlimit(resource as __rlimit_resource_t, &new_rlim as *const _) };

}else if #[cfg(any(
target_os = "freebsd",
target_os = "openbsd",
target_os = "netbsd",
target_os = "macos",
target_os = "ios",
target_os = "android",
target_os = "dragonfly",
target_os = "bitrig",
target_os = "linux", // target_env != "gnu"
))]{
}else{
let res = unsafe { libc::setrlimit(resource as c_int, &new_rlim as *const _) };
}
}
Expand Down

0 comments on commit c7eb876

Please sign in to comment.