Skip to content

Commit 9fd823d

Browse files
committed
Fixing test
1 parent e526489 commit 9fd823d

File tree

2 files changed

+54
-83
lines changed

2 files changed

+54
-83
lines changed

src/sys/resource.rs

Lines changed: 45 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -2,73 +2,34 @@
22
use cfg_if::cfg_if;
33
use std::mem;
44

5-
#[cfg(any(target_os = "linux"))]
6-
use libc::{__rlimit_resource_t, rlimit, RLIM_INFINITY};
7-
8-
#[cfg(any(
9-
target_os = "freebsd",
10-
target_os = "openbsd",
11-
target_os = "netbsd",
12-
target_os = "macos",
13-
target_os = "ios",
14-
target_os = "android",
15-
target_os = "dragonfly",
16-
target_os = "bitrig",
17-
))]
18-
use libc::{c_int, rlimit, RLIM_INFINITY};
19-
205
use crate::errno::Errno;
216
use crate::Result;
227

238
pub use libc::rlim_t;
249

2510
cfg_if! {
26-
if #[cfg(target_os = "linux")]{
27-
if #[cfg(target_env = "gnu")]{
28-
libc_enum!{
29-
#[repr(u32)]
30-
pub enum Resource {
31-
/// See detail of each Resource https://man7.org/linux/man-pages/man2/getrlimit.2.html
32-
RLIMIT_AS,
33-
RLIMIT_CORE,
34-
RLIMIT_CPU,
35-
RLIMIT_DATA,
36-
RLIMIT_FSIZE,
37-
RLIMIT_LOCKS,
38-
RLIMIT_MEMLOCK,
39-
RLIMIT_MSGQUEUE,
40-
RLIMIT_NICE,
41-
RLIMIT_NOFILE,
42-
RLIMIT_NPROC,
43-
RLIMIT_RSS,
44-
RLIMIT_RTPRIO,
45-
RLIMIT_RTTIME,
46-
RLIMIT_SIGPENDING,
47-
RLIMIT_STACK,
48-
}
49-
}
50-
}else{
51-
libc_enum!{
52-
#[repr(i32)]
53-
pub enum Resource {
54-
/// See detail of each Resource https://man7.org/linux/man-pages/man2/getrlimit.2.html
55-
RLIMIT_AS,
56-
RLIMIT_CORE,
57-
RLIMIT_CPU,
58-
RLIMIT_DATA,
59-
RLIMIT_FSIZE,
60-
RLIMIT_LOCKS,
61-
RLIMIT_MEMLOCK,
62-
RLIMIT_MSGQUEUE,
63-
RLIMIT_NICE,
64-
RLIMIT_NOFILE,
65-
RLIMIT_NPROC,
66-
RLIMIT_RSS,
67-
RLIMIT_RTPRIO,
68-
RLIMIT_RTTIME,
69-
RLIMIT_SIGPENDING,
70-
RLIMIT_STACK,
71-
}
11+
if #[cfg(all(target_os = "linux", target_env = "gnu"))]{
12+
use libc::{__rlimit_resource_t, rlimit, RLIM_INFINITY};
13+
libc_enum!{
14+
#[repr(u32)]
15+
pub enum Resource {
16+
/// See detail of each Resource https://man7.org/linux/man-pages/man2/getrlimit.2.html
17+
RLIMIT_AS,
18+
RLIMIT_CORE,
19+
RLIMIT_CPU,
20+
RLIMIT_DATA,
21+
RLIMIT_FSIZE,
22+
RLIMIT_LOCKS,
23+
RLIMIT_MEMLOCK,
24+
RLIMIT_MSGQUEUE,
25+
RLIMIT_NICE,
26+
RLIMIT_NOFILE,
27+
RLIMIT_NPROC,
28+
RLIMIT_RSS,
29+
RLIMIT_RTPRIO,
30+
RLIMIT_RTTIME,
31+
RLIMIT_SIGPENDING,
32+
RLIMIT_STACK,
7233
}
7334
}
7435
}else if #[cfg(any(
@@ -80,7 +41,10 @@ cfg_if! {
8041
target_os = "android",
8142
target_os = "dragonfly",
8243
target_os = "bitrig",
44+
target_os = "linux", // target_env != "gnu"
8345
))]{
46+
use libc::{c_int, rlimit, RLIM_INFINITY};
47+
8448
libc_enum! {
8549
#[repr(i32)]
8650
pub enum Resource {
@@ -98,31 +62,34 @@ cfg_if! {
9862
#[cfg(target_os = "freebsd")]
9963
RLIMIT_KQUEUES,
10064

101-
#[cfg(any(target_os = "android"))]
65+
#[cfg(any(target_os = "android", target_os = "linux"))]
10266
RLIMIT_LOCKS,
10367

104-
#[cfg(any(target_os = "android", target_os = "freebsd", target_os = "openbsd"))]
68+
#[cfg(any(target_os = "android", target_os = "freebsd", target_os = "openbsd", target_os = "linux"))]
10569
RLIMIT_MEMLOCK,
10670

107-
#[cfg(any(target_os = "android"))]
71+
#[cfg(any(target_os = "android", target_os = "linux"))]
10872
RLIMIT_MSGQUEUE,
10973

110-
#[cfg(any(target_os = "android"))]
74+
#[cfg(any(target_os = "android", target_os = "linux"))]
11175
RLIMIT_NICE,
11276

113-
#[cfg(any(target_os = "android", target_os = "freebsd", target_os = "openbsd"))]
77+
#[cfg(any(target_os = "android", target_os = "freebsd", target_os = "openbsd", target_os = "linux"))]
11478
RLIMIT_NPROC,
11579

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

119-
#[cfg(any(target_os = "android", target_os = "freebsd", target_os = "openbsd"))]
83+
#[cfg(any(target_os = "android", target_os = "freebsd", target_os = "openbsd", target_os = "linux"))]
12084
RLIMIT_RSS,
12185

122-
#[cfg(any(target_os = "android"))]
86+
#[cfg(any(target_os = "android", target_os = "linux"))]
12387
RLIMIT_RTPRIO,
12488

125-
#[cfg(any(target_os = "android"))]
89+
#[cfg(any(target_os = "linux"))]
90+
RLIMIT_RTTIME,
91+
92+
#[cfg(any(target_os = "android", target_os = "linux"))]
12693
RLIMIT_SIGPENDING,
12794

12895
#[cfg(any(target_os = "freebsd", target_os = "dragonfly"))]
@@ -166,7 +133,7 @@ cfg_if! {
166133
pub fn getrlimit(resource: Resource) -> Result<(Option<rlim_t>, Option<rlim_t>)> {
167134
let mut rlim = mem::MaybeUninit::<rlimit>::uninit();
168135

169-
#[cfg(target_os = "linux")]
136+
#[cfg(all(target_os = "linux", target_env = "gnu"))]
170137
let res =
171138
unsafe { libc::getrlimit(resource as __rlimit_resource_t, rlim.as_mut_ptr() as *mut _) };
172139
#[cfg(any(
@@ -178,6 +145,7 @@ pub fn getrlimit(resource: Resource) -> Result<(Option<rlim_t>, Option<rlim_t>)>
178145
target_os = "android",
179146
target_os = "dragonfly",
180147
target_os = "bitrig",
148+
target_os = "linux", // target_env != "gnu"
181149
))]
182150
let res = unsafe { libc::getrlimit(resource as c_int, rlim.as_mut_ptr() as *mut _) };
183151

@@ -197,9 +165,11 @@ pub fn getrlimit(resource: Resource) -> Result<(Option<rlim_t>, Option<rlim_t>)>
197165
/// # Parameters
198166
///
199167
/// * `resource`: The [`Resource`] that we want to set the limits of.
200-
/// * `soft_limit`: The value that the kernel enforces for the corresponding resource.
201-
/// * `hard_limit`: The ceiling for the soft limit. Must be lower or equal to the current hard limit
202-
/// for non-root users.
168+
/// * `soft_limit`: The value that the kernel enforces for the corresponding
169+
/// resource. Note: `None` input will be replaced by constant `RLIM_INFINITY`.
170+
/// * `hard_limit`: The ceiling for the soft limit. Must be lower or equal to
171+
/// the current hard limit for non-root users. Note: `None` input will be
172+
/// replaced by constant `RLIM_INFINITY`.
203173
///
204174
/// # Examples
205175
///
@@ -225,7 +195,7 @@ pub fn setrlimit(
225195
rlim.rlim_cur = soft_limit.unwrap_or(RLIM_INFINITY);
226196
rlim.rlim_max = hard_limit.unwrap_or(RLIM_INFINITY);
227197

228-
#[cfg(target_os = "linux")]
198+
#[cfg(all(target_os = "linux", target_env = "gnu"))]
229199
let res = unsafe { libc::setrlimit(resource as __rlimit_resource_t, &rlim as *const _) };
230200
#[cfg(any(
231201
target_os = "freebsd",
@@ -236,6 +206,7 @@ pub fn setrlimit(
236206
target_os = "android",
237207
target_os = "dragonfly",
238208
target_os = "bitrig",
209+
target_os = "linux", // target_env != "gnu"
239210
))]
240211
let res = unsafe { libc::setrlimit(resource as c_int, &rlim as *const _) };
241212
Errno::result(res).map(|_| ())

test/test_resource.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,21 @@ pub fn test_resource_limits_stack() {
3434
let (mut soft_limit, hard_limit) = getrlimit(Resource::RLIMIT_STACK).unwrap();
3535
let orig_limit = (soft_limit, hard_limit);
3636

37-
soft_limit = hard_limit;
37+
soft_limit = hard_limit.or(Some(10000));
3838
setrlimit(Resource::RLIMIT_STACK, soft_limit, hard_limit).unwrap();
3939

4040
let limit2 = getrlimit(Resource::RLIMIT_STACK).unwrap();
41-
// assert_eq!(soft_limit, limit2.0);
42-
// assert_eq!(hard_limit, limit2.1);
41+
assert_eq!(soft_limit, limit2.0);
42+
assert_eq!(hard_limit, limit2.1);
4343

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

4646
let final_limit = getrlimit(Resource::RLIMIT_STACK).unwrap();
47-
// assert_eq!(orig_limit.0, final_limit.0);
48-
// assert_eq!(orig_limit.1, final_limit.1);
47+
assert_eq!(orig_limit.0, final_limit.0);
48+
assert_eq!(orig_limit.1, final_limit.1);
4949

50-
assert_eq!(
51-
format!("softlimit-{:?}, hardlimit-{:?}, limit2.0-{:?}, limit2.1-{:?}, final_limit.0-{:?}, final_limit.0-{:?}"
52-
, soft_limit, hard_limit, limit2.0, limit2.1, final_limit.0, final_limit.1),
53-
"");
50+
// assert_eq!(
51+
// format!("softlimit-{:?}, hardlimit-{:?}, limit2.0-{:?}, limit2.1-{:?}, final_limit.0-{:?}, final_limit.0-{:?}"
52+
// , soft_limit, hard_limit, limit2.0, limit2.1, final_limit.0, final_limit.1),
53+
// "");
5454
}

0 commit comments

Comments
 (0)