From 2583ba1f7038be4f7432b43ca369d3d89ad7f1c5 Mon Sep 17 00:00:00 2001 From: LMJW Date: Sat, 17 Oct 2020 08:28:17 +1100 Subject: [PATCH] remove prlimit in setrlimit --- src/sys/resource.rs | 16 ++++------------ test/test_resource.rs | 26 -------------------------- 2 files changed, 4 insertions(+), 38 deletions(-) diff --git a/src/sys/resource.rs b/src/sys/resource.rs index 2cd7807b0b..41662c8665 100644 --- a/src/sys/resource.rs +++ b/src/sys/resource.rs @@ -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}; } @@ -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, Option)> { let mut old_rlim = rlimit { rlim_cur: 0, @@ -168,6 +168,9 @@ pub fn getrlimit(resource: Resource) -> Result<(Option, Option)> /// [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, @@ -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 _) }; diff --git a/test/test_resource.rs b/test/test_resource.rs index 9f9441affc..c351656357 100644 --- a/test/test_resource.rs +++ b/test/test_resource.rs @@ -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); -}