Skip to content

Commit

Permalink
remove prlimit in setrlimit
Browse files Browse the repository at this point in the history
  • Loading branch information
LMJW committed Oct 18, 2020
1 parent c7eb876 commit 2583ba1
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 38 deletions.
16 changes: 4 additions & 12 deletions src/sys/resource.rs
Expand Up @@ -8,7 +8,6 @@ pub use libc::rlim_t;
cfg_if! {
if #[cfg(all(target_os = "linux", target_env = "gnu"))]{
use libc::{__rlimit_resource_t, rlimit, RLIM_INFINITY};
use crate::Error;
}else{
use libc::{c_int, rlimit, RLIM_INFINITY};
}
Expand Down Expand Up @@ -117,6 +116,7 @@ libc_enum! {
/// [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>)> {
let mut old_rlim = rlimit {
rlim_cur: 0,
Expand Down Expand Up @@ -168,6 +168,9 @@ pub fn getrlimit(resource: Resource) -> Result<(Option<rlim_t>, Option<rlim_t>)>
/// [setrlimit(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/getrlimit.html#tag_16_215)
///
/// [`Resource`]: enum.Resource.html
///
/// Note: `setrlimit` provides a safe wrapper to libc's `setrlimit`. For the
/// platform that are not compatible, try using `prlimit` to set rlimit.
pub fn setrlimit(
resource: Resource,
soft_limit: Option<rlim_t>,
Expand All @@ -179,17 +182,6 @@ pub fn setrlimit(
};
cfg_if! {
if #[cfg(all(target_os = "linux", target_env = "gnu"))]{
// the below implementation is mimicing the similar implementation in golang
// https://go-review.googlesource.com/c/sys/+/230478/2/unix/syscall_linux_arm64.go#176
// 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 {
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{
let res = unsafe { libc::setrlimit(resource as c_int, &new_rlim as *const _) };
Expand Down
26 changes: 0 additions & 26 deletions test/test_resource.rs
Expand Up @@ -22,29 +22,3 @@ pub fn test_resource_limits_nofile() {
let (new_soft_limit, _new_hard_limit) = getrlimit(Resource::RLIMIT_NOFILE).unwrap();
assert_eq!(new_soft_limit, soft_limit);
}

/// Tests the RLIMIT_STACK functionality of getrlimit(), where the resource RLIMIT_STACK refers to
/// the maximum stack size that can be spawned by the current process before SIGSEGV is generated.
///
/// We first save the current stack limits, then newly set the soft limit to the same size as the
/// hard limit. We check to make sure these limits have been updated properly. We then set the
/// stack limits back to the original values, and make sure they have been updated properly.
#[test]
pub fn test_resource_limits_stack() {
let (mut soft_limit, hard_limit) = getrlimit(Resource::RLIMIT_STACK).unwrap();
let orig_limit = (soft_limit, hard_limit);

soft_limit = hard_limit.or(Some(4194304));
setrlimit(Resource::RLIMIT_STACK, soft_limit, hard_limit).unwrap();

let limit2 = getrlimit(Resource::RLIMIT_STACK).unwrap();

assert_eq!(soft_limit, limit2.0);
assert_eq!(hard_limit, limit2.1);

setrlimit(Resource::RLIMIT_STACK, orig_limit.0, orig_limit.1).unwrap();

let final_limit = getrlimit(Resource::RLIMIT_STACK).unwrap();
assert_eq!(orig_limit.0, final_limit.0);
assert_eq!(orig_limit.1, final_limit.1);
}

0 comments on commit 2583ba1

Please sign in to comment.