2
2
use cfg_if:: cfg_if;
3
3
use std:: mem;
4
4
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
-
20
5
use crate :: errno:: Errno ;
21
6
use crate :: Result ;
22
7
23
8
pub use libc:: rlim_t;
24
9
25
10
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 ,
72
33
}
73
34
}
74
35
} else if #[ cfg( any(
@@ -80,7 +41,10 @@ cfg_if! {
80
41
target_os = "android" ,
81
42
target_os = "dragonfly" ,
82
43
target_os = "bitrig" ,
44
+ target_os = "linux" , // target_env != "gnu"
83
45
) ) ] {
46
+ use libc:: { c_int, rlimit, RLIM_INFINITY } ;
47
+
84
48
libc_enum! {
85
49
#[ repr( i32 ) ]
86
50
pub enum Resource {
@@ -98,31 +62,34 @@ cfg_if! {
98
62
#[ cfg( target_os = "freebsd" ) ]
99
63
RLIMIT_KQUEUES ,
100
64
101
- #[ cfg( any( target_os = "android" ) ) ]
65
+ #[ cfg( any( target_os = "android" , target_os = "linux" ) ) ]
102
66
RLIMIT_LOCKS ,
103
67
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" ) ) ]
105
69
RLIMIT_MEMLOCK ,
106
70
107
- #[ cfg( any( target_os = "android" ) ) ]
71
+ #[ cfg( any( target_os = "android" , target_os = "linux" ) ) ]
108
72
RLIMIT_MSGQUEUE ,
109
73
110
- #[ cfg( any( target_os = "android" ) ) ]
74
+ #[ cfg( any( target_os = "android" , target_os = "linux" ) ) ]
111
75
RLIMIT_NICE ,
112
76
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" ) ) ]
114
78
RLIMIT_NPROC ,
115
79
116
80
#[ cfg( target_os = "freebsd" ) ]
117
81
RLIMIT_NPTS ,
118
82
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" ) ) ]
120
84
RLIMIT_RSS ,
121
85
122
- #[ cfg( any( target_os = "android" ) ) ]
86
+ #[ cfg( any( target_os = "android" , target_os = "linux" ) ) ]
123
87
RLIMIT_RTPRIO ,
124
88
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" ) ) ]
126
93
RLIMIT_SIGPENDING ,
127
94
128
95
#[ cfg( any( target_os = "freebsd" , target_os = "dragonfly" ) ) ]
@@ -166,7 +133,7 @@ cfg_if! {
166
133
pub fn getrlimit ( resource : Resource ) -> Result < ( Option < rlim_t > , Option < rlim_t > ) > {
167
134
let mut rlim = mem:: MaybeUninit :: < rlimit > :: uninit ( ) ;
168
135
169
- #[ cfg( target_os = "linux" ) ]
136
+ #[ cfg( all ( target_os = "linux" , target_env = "gnu" ) ) ]
170
137
let res =
171
138
unsafe { libc:: getrlimit ( resource as __rlimit_resource_t , rlim. as_mut_ptr ( ) as * mut _ ) } ;
172
139
#[ cfg( any(
@@ -178,6 +145,7 @@ pub fn getrlimit(resource: Resource) -> Result<(Option<rlim_t>, Option<rlim_t>)>
178
145
target_os = "android" ,
179
146
target_os = "dragonfly" ,
180
147
target_os = "bitrig" ,
148
+ target_os = "linux" , // target_env != "gnu"
181
149
) ) ]
182
150
let res = unsafe { libc:: getrlimit ( resource as c_int , rlim. as_mut_ptr ( ) as * mut _ ) } ;
183
151
@@ -197,9 +165,11 @@ pub fn getrlimit(resource: Resource) -> Result<(Option<rlim_t>, Option<rlim_t>)>
197
165
/// # Parameters
198
166
///
199
167
/// * `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`.
203
173
///
204
174
/// # Examples
205
175
///
@@ -225,7 +195,7 @@ pub fn setrlimit(
225
195
rlim. rlim_cur = soft_limit. unwrap_or ( RLIM_INFINITY ) ;
226
196
rlim. rlim_max = hard_limit. unwrap_or ( RLIM_INFINITY ) ;
227
197
228
- #[ cfg( target_os = "linux" ) ]
198
+ #[ cfg( all ( target_os = "linux" , target_env = "gnu" ) ) ]
229
199
let res = unsafe { libc:: setrlimit ( resource as __rlimit_resource_t , & rlim as * const _ ) } ;
230
200
#[ cfg( any(
231
201
target_os = "freebsd" ,
@@ -236,6 +206,7 @@ pub fn setrlimit(
236
206
target_os = "android" ,
237
207
target_os = "dragonfly" ,
238
208
target_os = "bitrig" ,
209
+ target_os = "linux" , // target_env != "gnu"
239
210
) ) ]
240
211
let res = unsafe { libc:: setrlimit ( resource as c_int , & rlim as * const _ ) } ;
241
212
Errno :: result ( res) . map ( |_| ( ) )
0 commit comments