@@ -5,111 +5,98 @@ use crate::errno::Errno;
5
5
use crate :: Result ;
6
6
7
7
pub use libc:: rlim_t;
8
-
9
8
cfg_if ! {
10
9
if #[ cfg( all( target_os = "linux" , target_env = "gnu" ) ) ] {
11
10
use libc:: { __rlimit_resource_t, rlimit, RLIM_INFINITY } ;
12
- libc_enum!{
13
- #[ repr( u32 ) ]
14
- pub enum Resource {
15
- /// See detail of each Resource https://man7.org/linux/man-pages/man2/getrlimit.2.html
16
- RLIMIT_AS ,
17
- RLIMIT_CORE ,
18
- RLIMIT_CPU ,
19
- RLIMIT_DATA ,
20
- RLIMIT_FSIZE ,
21
- RLIMIT_LOCKS ,
22
- RLIMIT_MEMLOCK ,
23
- RLIMIT_MSGQUEUE ,
24
- RLIMIT_NICE ,
25
- RLIMIT_NOFILE ,
26
- RLIMIT_NPROC ,
27
- RLIMIT_RSS ,
28
- RLIMIT_RTPRIO ,
29
- RLIMIT_RTTIME ,
30
- RLIMIT_SIGPENDING ,
31
- RLIMIT_STACK ,
32
- }
33
- }
34
- } else if #[ cfg( any(
35
- target_os = "freebsd" ,
36
- target_os = "openbsd" ,
37
- target_os = "netbsd" ,
38
- target_os = "macos" ,
39
- target_os = "ios" ,
40
- target_os = "android" ,
41
- target_os = "dragonfly" ,
42
- target_os = "bitrig" ,
43
- target_os = "linux" , // target_env != "gnu"
44
- ) ) ] {
11
+ use crate :: Error ;
12
+ } else{
45
13
use libc:: { c_int, rlimit, RLIM_INFINITY } ;
14
+ }
15
+ }
46
16
47
- libc_enum! {
48
- #[ repr( i32 ) ]
49
- pub enum Resource {
50
- /// See detail of each Resource https://man7.org/linux/man-pages/man2/getrlimit.2.html
51
- /// BSD specific Resource https://www.freebsd.org/cgi/man.cgi?query=setrlimit
52
- #[ cfg( not( any( target_os = "netbsd" , target_os = "freebsd" ) ) ) ]
53
- RLIMIT_AS ,
54
- RLIMIT_CORE ,
55
- RLIMIT_CPU ,
56
- RLIMIT_DATA ,
57
- RLIMIT_FSIZE ,
58
- RLIMIT_NOFILE ,
59
- RLIMIT_STACK ,
17
+ libc_enum ! {
18
+ /// The Resource enum is platform dependent. Check different platform
19
+ /// manuals for more details. Some platform links has been provided for
20
+ /// earier reference (non-exhaustive).
21
+ ///
22
+ /// * [Linux](https://man7.org/linux/man-pages/man2/getrlimit.2.html)
23
+ /// * [freeBSD](https://www.freebsd.org/cgi/man.cgi?query=setrlimit)
24
+
25
+ // linux-gnu uses u_int as resource enum, which is implemented in libc as
26
+ // well.
27
+ //
28
+ // https://gcc.gnu.org/legacy-ml/gcc/2015-08/msg00441.html
29
+ // https://github.com/rust-lang/libc/blob/master/src/unix/linux_like/linux/gnu/mod.rs
30
+ #[ cfg_attr( all( target_os = "linux" , target_env = "gnu" ) , repr( u32 ) ) ]
31
+ #[ cfg_attr( any(
32
+ target_os = "freebsd" ,
33
+ target_os = "openbsd" ,
34
+ target_os = "netbsd" ,
35
+ target_os = "macos" ,
36
+ target_os = "ios" ,
37
+ target_os = "android" ,
38
+ target_os = "dragonfly" ,
39
+ all( target_os = "linux" , not( target_env = "gnu" ) )
40
+ ) , repr( i32 ) ) ]
41
+ pub enum Resource {
42
+ #[ cfg( not( any( target_os = "netbsd" , target_os = "freebsd" ) ) ) ]
43
+ RLIMIT_AS ,
44
+ RLIMIT_CORE ,
45
+ RLIMIT_CPU ,
46
+ RLIMIT_DATA ,
47
+ RLIMIT_FSIZE ,
48
+ RLIMIT_NOFILE ,
49
+ RLIMIT_STACK ,
60
50
61
- // platform specific
62
- #[ cfg( target_os = "freebsd" ) ]
63
- RLIMIT_KQUEUES ,
51
+ // platform specific
52
+ #[ cfg( target_os = "freebsd" ) ]
53
+ RLIMIT_KQUEUES ,
64
54
65
- #[ cfg( any( target_os = "android" , target_os = "linux" ) ) ]
66
- RLIMIT_LOCKS ,
55
+ #[ cfg( any( target_os = "android" , target_os = "linux" ) ) ]
56
+ RLIMIT_LOCKS ,
67
57
68
- #[ cfg( any( target_os = "android" , target_os = "freebsd" , target_os = "openbsd" , target_os = "linux" ) ) ]
69
- RLIMIT_MEMLOCK ,
58
+ #[ cfg( any( target_os = "android" , target_os = "freebsd" , target_os = "openbsd" , target_os = "linux" ) ) ]
59
+ RLIMIT_MEMLOCK ,
70
60
71
- #[ cfg( any( target_os = "android" , target_os = "linux" ) ) ]
72
- RLIMIT_MSGQUEUE ,
61
+ #[ cfg( any( target_os = "android" , target_os = "linux" ) ) ]
62
+ RLIMIT_MSGQUEUE ,
73
63
74
- #[ cfg( any( target_os = "android" , target_os = "linux" ) ) ]
75
- RLIMIT_NICE ,
64
+ #[ cfg( any( target_os = "android" , target_os = "linux" ) ) ]
65
+ RLIMIT_NICE ,
76
66
77
- #[ cfg( any( target_os = "android" , target_os = "freebsd" , target_os = "openbsd" , target_os = "linux" ) ) ]
78
- RLIMIT_NPROC ,
67
+ #[ cfg( any( target_os = "android" , target_os = "freebsd" , target_os = "openbsd" , target_os = "linux" ) ) ]
68
+ RLIMIT_NPROC ,
79
69
80
- #[ cfg( target_os = "freebsd" ) ]
81
- RLIMIT_NPTS ,
70
+ #[ cfg( target_os = "freebsd" ) ]
71
+ RLIMIT_NPTS ,
82
72
83
- #[ cfg( any( target_os = "android" , target_os = "freebsd" , target_os = "openbsd" , target_os = "linux" ) ) ]
84
- RLIMIT_RSS ,
73
+ #[ cfg( any( target_os = "android" , target_os = "freebsd" , target_os = "openbsd" , target_os = "linux" ) ) ]
74
+ RLIMIT_RSS ,
85
75
86
- #[ cfg( any( target_os = "android" , target_os = "linux" ) ) ]
87
- RLIMIT_RTPRIO ,
76
+ #[ cfg( any( target_os = "android" , target_os = "linux" ) ) ]
77
+ RLIMIT_RTPRIO ,
88
78
89
- #[ cfg( any( target_os = "linux" ) ) ]
90
- RLIMIT_RTTIME ,
79
+ #[ cfg( any( target_os = "linux" ) ) ]
80
+ RLIMIT_RTTIME ,
91
81
92
- #[ cfg( any( target_os = "android" , target_os = "linux" ) ) ]
93
- RLIMIT_SIGPENDING ,
82
+ #[ cfg( any( target_os = "android" , target_os = "linux" ) ) ]
83
+ RLIMIT_SIGPENDING ,
94
84
95
- #[ cfg( any( target_os = "freebsd" , target_os = "dragonfly" ) ) ]
96
- RLIMIT_SBSIZE ,
85
+ #[ cfg( any( target_os = "freebsd" , target_os = "dragonfly" ) ) ]
86
+ RLIMIT_SBSIZE ,
97
87
98
- #[ cfg( target_os = "freebsd" ) ]
99
- RLIMIT_SWAP ,
88
+ #[ cfg( target_os = "freebsd" ) ]
89
+ RLIMIT_SWAP ,
100
90
101
- #[ cfg( target_os = "freebsd" ) ]
102
- RLIMIT_VMEM ,
103
- }
104
- }
105
- } else{
106
- // unkown os
91
+ #[ cfg( target_os = "freebsd" ) ]
92
+ RLIMIT_VMEM ,
107
93
}
108
94
}
109
95
110
96
/// Get the current processes resource limits
111
97
///
112
- /// A value of `None` indicates that there's no limit.
98
+ /// A value of `None` indicates the value equals to `RLIM_INFINITY` which means
99
+ /// there is no limit.
113
100
///
114
101
/// # Parameters
115
102
///
@@ -127,7 +114,7 @@ cfg_if! {
127
114
///
128
115
/// # References
129
116
///
130
- /// [getrlimit(2)](https://linux.die.net/man/2/ getrlimit)
117
+ /// [getrlimit(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/ getrlimit.html#tag_16_215 )
131
118
///
132
119
/// [`Resource`]: enum.Resource.html
133
120
pub fn getrlimit ( resource : Resource ) -> Result < ( Option < rlim_t > , Option < rlim_t > ) > {
@@ -139,17 +126,7 @@ pub fn getrlimit(resource: Resource) -> Result<(Option<rlim_t>, Option<rlim_t>)>
139
126
cfg_if ! {
140
127
if #[ cfg( all( target_os = "linux" , target_env = "gnu" ) ) ] {
141
128
let res = unsafe { libc:: getrlimit( resource as __rlimit_resource_t, & mut old_rlim) } ;
142
- } else if #[ cfg( any(
143
- target_os = "freebsd" ,
144
- target_os = "openbsd" ,
145
- target_os = "netbsd" ,
146
- target_os = "macos" ,
147
- target_os = "ios" ,
148
- target_os = "android" ,
149
- target_os = "dragonfly" ,
150
- target_os = "bitrig" ,
151
- target_os = "linux" , // target_env != "gnu"
152
- ) ) ] {
129
+ } else{
153
130
let res = unsafe { libc:: getrlimit( resource as c_int, & mut old_rlim) } ;
154
131
}
155
132
}
@@ -164,8 +141,6 @@ pub fn getrlimit(resource: Resource) -> Result<(Option<rlim_t>, Option<rlim_t>)>
164
141
165
142
/// Set the current processes resource limits
166
143
///
167
- /// A value of `None` indicates that there's no limit.
168
- ///
169
144
/// # Parameters
170
145
///
171
146
/// * `resource`: The [`Resource`] that we want to set the limits of.
@@ -175,19 +150,22 @@ pub fn getrlimit(resource: Resource) -> Result<(Option<rlim_t>, Option<rlim_t>)>
175
150
/// the current hard limit for non-root users. Note: `None` input will be
176
151
/// replaced by constant `RLIM_INFINITY`.
177
152
///
153
+ /// > Note: for some os (linux_gnu), setting hard_limit to `RLIM_INFINITY` can
154
+ /// > results `EPERM` Error. So you will need to set the number explicitly.
155
+ ///
178
156
/// # Examples
179
157
///
180
- /// ```no_run
158
+ /// ```
181
159
/// # use nix::sys::resource::{setrlimit, Resource};
182
160
///
183
161
/// let soft_limit = Some(1024);
184
- /// let hard_limit = None ;
162
+ /// let hard_limit = Some(1048576) ;
185
163
/// setrlimit(Resource::RLIMIT_NOFILE, soft_limit, hard_limit).unwrap();
186
164
/// ```
187
165
///
188
166
/// # References
189
167
///
190
- /// [setrlimit(2)](https://linux.die.net/man/2/setrlimit )
168
+ /// [setrlimit(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/getrlimit.html#tag_16_215 )
191
169
///
192
170
/// [`Resource`]: enum.Resource.html
193
171
pub fn setrlimit (
@@ -206,22 +184,14 @@ pub fn setrlimit(
206
184
// seems for some of the architectures, we prefer to use prlimit instead of {g,s}etrlimit
207
185
let res = unsafe { libc:: prlimit( 0 , resource as __rlimit_resource_t, & new_rlim as * const _, std:: ptr:: null_mut( ) ) } ;
208
186
if res == -1 {
209
- return Errno :: result( res) . map( |_| ( ) ) ;
187
+ match Errno :: last( ) {
188
+ Errno :: ENOSYS =>{ }
189
+ e => { return Err ( Error :: Sys ( e) ) ; }
190
+ }
210
191
}
211
192
212
193
let res = unsafe { libc:: setrlimit( resource as __rlimit_resource_t, & new_rlim as * const _) } ;
213
-
214
- } else if #[ cfg( any(
215
- target_os = "freebsd" ,
216
- target_os = "openbsd" ,
217
- target_os = "netbsd" ,
218
- target_os = "macos" ,
219
- target_os = "ios" ,
220
- target_os = "android" ,
221
- target_os = "dragonfly" ,
222
- target_os = "bitrig" ,
223
- target_os = "linux" , // target_env != "gnu"
224
- ) ) ] {
194
+ } else{
225
195
let res = unsafe { libc:: setrlimit( resource as c_int, & new_rlim as * const _) } ;
226
196
}
227
197
}
0 commit comments