Skip to content

Commit 2583ba1

Browse files
committed
remove prlimit in setrlimit
1 parent c7eb876 commit 2583ba1

File tree

2 files changed

+4
-38
lines changed

2 files changed

+4
-38
lines changed

src/sys/resource.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ pub use libc::rlim_t;
88
cfg_if! {
99
if #[cfg(all(target_os = "linux", target_env = "gnu"))]{
1010
use libc::{__rlimit_resource_t, rlimit, RLIM_INFINITY};
11-
use crate::Error;
1211
}else{
1312
use libc::{c_int, rlimit, RLIM_INFINITY};
1413
}
@@ -117,6 +116,7 @@ libc_enum! {
117116
/// [getrlimit(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/getrlimit.html#tag_16_215)
118117
///
119118
/// [`Resource`]: enum.Resource.html
119+
120120
pub fn getrlimit(resource: Resource) -> Result<(Option<rlim_t>, Option<rlim_t>)> {
121121
let mut old_rlim = rlimit {
122122
rlim_cur: 0,
@@ -168,6 +168,9 @@ pub fn getrlimit(resource: Resource) -> Result<(Option<rlim_t>, Option<rlim_t>)>
168168
/// [setrlimit(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/getrlimit.html#tag_16_215)
169169
///
170170
/// [`Resource`]: enum.Resource.html
171+
///
172+
/// Note: `setrlimit` provides a safe wrapper to libc's `setrlimit`. For the
173+
/// platform that are not compatible, try using `prlimit` to set rlimit.
171174
pub fn setrlimit(
172175
resource: Resource,
173176
soft_limit: Option<rlim_t>,
@@ -179,17 +182,6 @@ pub fn setrlimit(
179182
};
180183
cfg_if! {
181184
if #[cfg(all(target_os = "linux", target_env = "gnu"))]{
182-
// the below implementation is mimicing the similar implementation in golang
183-
// https://go-review.googlesource.com/c/sys/+/230478/2/unix/syscall_linux_arm64.go#176
184-
// seems for some of the architectures, we prefer to use prlimit instead of {g,s}etrlimit
185-
let res = unsafe { libc::prlimit(0, resource as __rlimit_resource_t, &new_rlim as *const _, std::ptr::null_mut()) };
186-
if res == -1 {
187-
match Errno::last() {
188-
Errno::ENOSYS =>{}
189-
e => {return Err(Error::Sys(e));}
190-
}
191-
}
192-
193185
let res = unsafe { libc::setrlimit(resource as __rlimit_resource_t, &new_rlim as *const _) };
194186
}else{
195187
let res = unsafe { libc::setrlimit(resource as c_int, &new_rlim as *const _) };

test/test_resource.rs

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,29 +22,3 @@ pub fn test_resource_limits_nofile() {
2222
let (new_soft_limit, _new_hard_limit) = getrlimit(Resource::RLIMIT_NOFILE).unwrap();
2323
assert_eq!(new_soft_limit, soft_limit);
2424
}
25-
26-
/// Tests the RLIMIT_STACK functionality of getrlimit(), where the resource RLIMIT_STACK refers to
27-
/// the maximum stack size that can be spawned by the current process before SIGSEGV is generated.
28-
///
29-
/// We first save the current stack limits, then newly set the soft limit to the same size as the
30-
/// hard limit. We check to make sure these limits have been updated properly. We then set the
31-
/// stack limits back to the original values, and make sure they have been updated properly.
32-
#[test]
33-
pub fn test_resource_limits_stack() {
34-
let (mut soft_limit, hard_limit) = getrlimit(Resource::RLIMIT_STACK).unwrap();
35-
let orig_limit = (soft_limit, hard_limit);
36-
37-
soft_limit = hard_limit.or(Some(4194304));
38-
setrlimit(Resource::RLIMIT_STACK, soft_limit, hard_limit).unwrap();
39-
40-
let limit2 = getrlimit(Resource::RLIMIT_STACK).unwrap();
41-
42-
assert_eq!(soft_limit, limit2.0);
43-
assert_eq!(hard_limit, limit2.1);
44-
45-
setrlimit(Resource::RLIMIT_STACK, orig_limit.0, orig_limit.1).unwrap();
46-
47-
let final_limit = getrlimit(Resource::RLIMIT_STACK).unwrap();
48-
assert_eq!(orig_limit.0, final_limit.0);
49-
assert_eq!(orig_limit.1, final_limit.1);
50-
}

0 commit comments

Comments
 (0)