Skip to content

Commit e526489

Browse files
committed
Specify layout of linux-gnu
1 parent 39cf86c commit e526489

File tree

2 files changed

+126
-91
lines changed

2 files changed

+126
-91
lines changed

src/sys/resource.rs

Lines changed: 117 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//! Configure the process resource limits.
2+
use cfg_if::cfg_if;
23
use std::mem;
34

45
#[cfg(any(target_os = "linux"))]
@@ -21,98 +22,127 @@ use crate::Result;
2122

2223
pub use libc::rlim_t;
2324

24-
#[cfg(any(target_os = "linux"))]
25-
libc_enum! {
26-
#[repr(u32)]
27-
pub enum Resource {
28-
/// See detail of each Resource https://man7.org/linux/man-pages/man2/getrlimit.2.html
29-
RLIMIT_AS,
30-
RLIMIT_CORE,
31-
RLIMIT_CPU,
32-
RLIMIT_DATA,
33-
RLIMIT_FSIZE,
34-
RLIMIT_LOCKS,
35-
RLIMIT_MEMLOCK,
36-
RLIMIT_MSGQUEUE,
37-
RLIMIT_NICE,
38-
RLIMIT_NOFILE,
39-
RLIMIT_NPROC,
40-
RLIMIT_RSS,
41-
RLIMIT_RTPRIO,
42-
RLIMIT_RTTIME,
43-
RLIMIT_SIGPENDING,
44-
RLIMIT_STACK,
45-
}
46-
}
47-
48-
#[cfg(any(
49-
target_os = "freebsd",
50-
target_os = "openbsd",
51-
target_os = "netbsd",
52-
target_os = "macos",
53-
target_os = "ios",
54-
target_os = "android",
55-
target_os = "dragonfly",
56-
target_os = "bitrig",
57-
))]
58-
libc_enum! {
59-
#[repr(i32)]
60-
pub enum Resource {
61-
/// See detail of each Resource https://man7.org/linux/man-pages/man2/getrlimit.2.html
62-
/// BSD specific Resource https://www.freebsd.org/cgi/man.cgi?query=setrlimit
63-
RLIMIT_AS,
64-
RLIMIT_CORE,
65-
RLIMIT_CPU,
66-
RLIMIT_DATA,
67-
RLIMIT_FSIZE,
68-
RLIMIT_NOFILE,
69-
RLIMIT_STACK,
70-
71-
// platform specific
72-
#[cfg(target_os = "freebsd")]
73-
RLIMIT_KQUEUES,
74-
75-
#[cfg(any(target_os = "android"))]
76-
RLIMIT_LOCKS,
77-
78-
#[cfg(any(target_os = "android", target_os = "freebsd", target_os = "openbsd"))]
79-
RLIMIT_MEMLOCK,
80-
81-
#[cfg(any(target_os = "android"))]
82-
RLIMIT_MSGQUEUE,
83-
84-
#[cfg(any(target_os = "android"))]
85-
RLIMIT_NICE,
86-
87-
#[cfg(any(target_os = "android", target_os = "freebsd", target_os = "openbsd"))]
88-
RLIMIT_NPROC,
89-
90-
#[cfg(target_os = "freebsd")]
91-
RLIMIT_NPTS,
92-
93-
#[cfg(any(target_os = "android", target_os = "freebsd", target_os = "openbsd"))]
94-
RLIMIT_RSS,
95-
96-
#[cfg(any(target_os = "android"))]
97-
RLIMIT_RTPRIO,
98-
99-
#[cfg(any(target_os = "android"))]
100-
RLIMIT_SIGPENDING,
101-
102-
#[cfg(any(target_os = "freebsd", target_os = "dragonfly"))]
103-
RLIMIT_SBSIZE,
104-
105-
#[cfg(target_os = "freebsd")]
106-
RLIMIT_SWAP,
107-
108-
#[cfg(target_os = "freebsd")]
109-
RLIMIT_VMEM,
25+
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+
}
72+
}
73+
}
74+
}else if #[cfg(any(
75+
target_os = "freebsd",
76+
target_os = "openbsd",
77+
target_os = "netbsd",
78+
target_os = "macos",
79+
target_os = "ios",
80+
target_os = "android",
81+
target_os = "dragonfly",
82+
target_os = "bitrig",
83+
))]{
84+
libc_enum! {
85+
#[repr(i32)]
86+
pub enum Resource {
87+
/// See detail of each Resource https://man7.org/linux/man-pages/man2/getrlimit.2.html
88+
/// BSD specific Resource https://www.freebsd.org/cgi/man.cgi?query=setrlimit
89+
RLIMIT_AS,
90+
RLIMIT_CORE,
91+
RLIMIT_CPU,
92+
RLIMIT_DATA,
93+
RLIMIT_FSIZE,
94+
RLIMIT_NOFILE,
95+
RLIMIT_STACK,
96+
97+
// platform specific
98+
#[cfg(target_os = "freebsd")]
99+
RLIMIT_KQUEUES,
100+
101+
#[cfg(any(target_os = "android"))]
102+
RLIMIT_LOCKS,
103+
104+
#[cfg(any(target_os = "android", target_os = "freebsd", target_os = "openbsd"))]
105+
RLIMIT_MEMLOCK,
106+
107+
#[cfg(any(target_os = "android"))]
108+
RLIMIT_MSGQUEUE,
109+
110+
#[cfg(any(target_os = "android"))]
111+
RLIMIT_NICE,
112+
113+
#[cfg(any(target_os = "android", target_os = "freebsd", target_os = "openbsd"))]
114+
RLIMIT_NPROC,
115+
116+
#[cfg(target_os = "freebsd")]
117+
RLIMIT_NPTS,
118+
119+
#[cfg(any(target_os = "android", target_os = "freebsd", target_os = "openbsd"))]
120+
RLIMIT_RSS,
121+
122+
#[cfg(any(target_os = "android"))]
123+
RLIMIT_RTPRIO,
124+
125+
#[cfg(any(target_os = "android"))]
126+
RLIMIT_SIGPENDING,
127+
128+
#[cfg(any(target_os = "freebsd", target_os = "dragonfly"))]
129+
RLIMIT_SBSIZE,
130+
131+
#[cfg(target_os = "freebsd")]
132+
RLIMIT_SWAP,
133+
134+
#[cfg(target_os = "freebsd")]
135+
RLIMIT_VMEM,
136+
}
137+
}
138+
}else{
139+
// unkown os
110140
}
111141
}
112142

113143
/// Get the current processes resource limits
114144
///
115-
/// A value of None indicates that there's no limit.
145+
/// A value of `None` indicates that there's no limit.
116146
///
117147
/// # Parameters
118148
///

test/test_resource.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,17 @@ pub fn test_resource_limits_stack() {
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);
49+
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+
"");
4954
}

0 commit comments

Comments
 (0)