1
1
//! Configure the process resource limits.
2
2
use cfg_if:: cfg_if;
3
- use std:: mem;
4
3
5
4
use crate :: errno:: Errno ;
6
5
use crate :: Result ;
@@ -131,22 +130,14 @@ cfg_if! {
131
130
///
132
131
/// [`Resource`]: enum.Resource.html
133
132
pub fn getrlimit ( resource : Resource ) -> Result < ( Option < rlim_t > , Option < rlim_t > ) > {
134
- let mut old_rlim = mem:: MaybeUninit :: < rlimit > :: uninit ( ) ;
133
+ let mut old_rlim = rlimit {
134
+ rlim_cur : 0 ,
135
+ rlim_max : 0 ,
136
+ } ;
135
137
136
138
cfg_if ! {
137
139
if #[ cfg( all( target_os = "linux" , target_env = "gnu" ) ) ] {
138
- // the below implementation is mimicing the similar implementation in golang
139
- // https://go-review.googlesource.com/c/sys/+/230478/2/unix/syscall_linux_arm64.go#176
140
- // seems for some of the architectures, we prefer to use prlimit instead of {g,s}etrlimit
141
-
142
- let res = unsafe { libc:: prlimit( 0 , resource as __rlimit_resource_t, std:: ptr:: null( ) , old_rlim. as_mut_ptr( ) as * mut _) } ;
143
- if res == -1 {
144
- // when error happens, the map will return an Err, the (None, None) is just make compiler
145
- // happy, it will not go through
146
- return Errno :: result( res) . map( |_|{ ( None , None ) } ) ;
147
- }
148
- let res = unsafe { libc:: getrlimit( resource as __rlimit_resource_t, old_rlim. as_mut_ptr( ) as * mut _) } ;
149
-
140
+ let res = unsafe { libc:: getrlimit( resource as __rlimit_resource_t, & mut old_rlim) } ;
150
141
} else if #[ cfg( any(
151
142
target_os = "freebsd" ,
152
143
target_os = "openbsd" ,
@@ -158,11 +149,10 @@ pub fn getrlimit(resource: Resource) -> Result<(Option<rlim_t>, Option<rlim_t>)>
158
149
target_os = "bitrig" ,
159
150
target_os = "linux" , // target_env != "gnu"
160
151
) ) ] {
161
- let res = unsafe { libc:: getrlimit( resource as c_int, old_rlim . as_mut_ptr ( ) as * mut _ ) } ;
152
+ let res = unsafe { libc:: getrlimit( resource as c_int, & mut old_rlim ) } ;
162
153
}
163
154
}
164
155
165
- let old_rlim = unsafe { old_rlim. assume_init ( ) } ;
166
156
Errno :: result ( res) . map ( |_| {
167
157
(
168
158
Some ( old_rlim. rlim_cur ) . filter ( |x| * x != RLIM_INFINITY ) ,
@@ -204,10 +194,10 @@ pub fn setrlimit(
204
194
soft_limit : Option < rlim_t > ,
205
195
hard_limit : Option < rlim_t > ,
206
196
) -> Result < ( ) > {
207
- let mut new_rlim = unsafe { mem :: MaybeUninit :: < rlimit > :: uninit ( ) . assume_init ( ) } ;
208
- new_rlim . rlim_cur = soft_limit. unwrap_or ( RLIM_INFINITY ) ;
209
- new_rlim . rlim_max = hard_limit. unwrap_or ( RLIM_INFINITY ) ;
210
-
197
+ let new_rlim = rlimit {
198
+ rlim_cur : soft_limit. unwrap_or ( RLIM_INFINITY ) ,
199
+ rlim_max : hard_limit. unwrap_or ( RLIM_INFINITY ) ,
200
+ } ;
211
201
cfg_if ! {
212
202
if #[ cfg( all( target_os = "linux" , target_env = "gnu" ) ) ] {
213
203
// the below implementation is mimicing the similar implementation in golang
0 commit comments