Skip to content

Commit 7309246

Browse files
committed
Conditional reprs based on the target_os (incomplete)
1 parent bfd2228 commit 7309246

File tree

1 file changed

+134
-18
lines changed

1 file changed

+134
-18
lines changed

src/sys/resource.rs

Lines changed: 134 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,135 @@
11
//! Configure the process resource limits.
22
use std::mem;
33

4-
use libc::{self, c_uint, rlimit, RLIM_INFINITY};
4+
#[cfg(not(any(target_os = "freebsd", target_os = "openbsd", target_os = "netbsd", target_os = "macos")))]
5+
use libc::{self, rlimit, __rlimit_resource_t, RLIM_INFINITY};
6+
7+
#[cfg(any(target_os = "freebsd", target_os = "openbsd", target_os = "netbsd", target_os = "macos"))]
8+
use libc::{self, rlimit, c_int, RLIM_INFINITY};
9+
510
pub use libc::rlim_t;
611

712
use {Errno, Result};
813

14+
#[cfg(target_os = "linux")]
915
libc_enum!{
1016
/// A resource that limits apply to
1117
#[repr(u32)]
1218
pub enum Resource {
1319
// POSIX
14-
/// This is the maximum size of the process's virtual memory (address space). The limit is specified in bytes, and is rounded down to the system page size.
20+
/// This is the maximum size of the process's virtual memory (address space). The limit is
21+
/// specified in bytes, and is rounded down to the system page size.
22+
RLIMIT_AS,
23+
/// This is the maximum size of a core file (see core(5)) in bytes that the process may
24+
/// dump.
25+
RLIMIT_CORE,
26+
/// This is a limit, in seconds, on the amount of CPU time that the process can consume.
27+
RLIMIT_CPU,
28+
/// This is the maximum size of the process's data segment (initialized data, uninitialized
29+
/// data, and heap). The limit is specified in bytes, and is rounded down to the system
30+
/// page size.
31+
RLIMIT_DATA,
32+
/// This is the maximum size in bytes of files that the process may create. Attempts to
33+
/// extend a file beyond this limit result in delivery of a SIGXFSZ signal.
34+
RLIMIT_FSIZE,
35+
/// This specifies a value one greater than the maximum file descriptor number that can be
36+
/// opened by this process.
37+
RLIMIT_NOFILE,
38+
/// This is the maximum size of the process stack, in bytes. Upon reaching this limit, a
39+
/// SIGSEGV signal is generated.
40+
RLIMIT_STACK,
41+
42+
// BSDs and Linux
43+
/// This is the maximum number of bytes of memory that may be locked into RAM. This limit
44+
/// is in effect rounded down to the nearest multiple of the system page size.
45+
#[cfg(any(target_os = "android", target_os = "freebsd", target_os = "linux", target_os = "openbsd"))]
46+
RLIMIT_MEMLOCK,
47+
/// This is a limit on the number of extant process (or, more precisely on Linux, threads)
48+
/// for the real user ID of the calling process.
49+
#[cfg(any(target_os = "android", target_os = "freebsd", target_os = "linux", target_os = "openbsd"))]
50+
RLIMIT_NPROC,
51+
/// This is a limit (in bytes) on the process's resident set (the number of virtual pages resident in RAM).
52+
#[cfg(any(target_os = "android", target_os = "freebsd", target_os = "linux", target_os = "openbsd"))]
53+
RLIMIT_RSS,
54+
55+
// Android and Linux only
56+
/// This is a limit on the combined number of flock(2) locks and fcntl(2) leases that this process may establish.
57+
#[cfg(any(target_os = "android", target_os = "linux"))]
58+
RLIMIT_LOCKS,
59+
/// This is a limit on the number of bytes that can be allocated for POSIX message queues
60+
/// for the real user ID of the calling process.
61+
#[cfg(any(target_os = "android", target_os = "linux"))]
62+
RLIMIT_MSGQUEUE,
63+
/// This specifies a ceiling to which the process's nice value can be raised using
64+
/// setpriority(2) or nice(2). The actual ceiling for the nice value is calculated as 20 -
65+
/// rlim_cur.
66+
#[cfg(any(target_os = "android", target_os = "linux"))]
67+
RLIMIT_NICE,
68+
/// This specifies a ceiling on the real-time priority that may be set for this process
69+
/// using sched_setscheduler(2) and sched_setparam(2).
70+
#[cfg(any(target_os = "android", target_os = "linux"))]
71+
RLIMIT_RTPRIO,
72+
/// This is a limit (in microseconds) on the amount of CPU time that a process scheduled
73+
/// under a real-time scheduling policy may consume without making a blocking system call.
74+
#[cfg(any(target_os = "android", target_os = "linux"))]
75+
RLIMIT_RTTIME,
76+
/// This is a limit on the number of signals that may be queued for the real user ID of the
77+
/// calling process. Both standard and real-time signals are counted for the purpose of
78+
/// checking this limit.
79+
#[cfg(any(target_os = "android", target_os = "linux"))]
80+
RLIMIT_SIGPENDING,
81+
82+
// Available on some BSD
83+
/// The maximum number of kqueues this user id is allowed to create.
84+
#[cfg(target_os = "freebsd")]
85+
RLIMIT_KQUEUES,
86+
/// The maximum number of pseudo-terminals this user id is allowed to create.
87+
#[cfg(target_os = "freebsd")]
88+
RLIMIT_NPTS,
89+
/// The maximum size (in bytes) of socket buffer usage for this user.
90+
#[cfg(any(target_os = "freebsd", target_os = "dragonfly"))]
91+
RLIMIT_SBSIZE,
92+
/// The maximum size (in bytes) of the swap space that may be reserved or used by all of this user id's processes.
93+
#[cfg(target_os = "freebsd")]
94+
RLIMIT_SWAP,
95+
}
96+
}
97+
98+
#[cfg(not(target_os = "linux"))]
99+
libc_enum!{
100+
/// A resource that limits apply to
101+
#[repr(i32)]
102+
pub enum Resource {
103+
// POSIX
104+
/// This is the maximum size of the process's virtual memory (address space). The limit is
105+
/// specified in bytes, and is rounded down to the system page size.
15106
RLIMIT_AS,
16-
/// This is the maximum size of a core file (see core(5)) in bytes that the process may dump.
107+
/// This is the maximum size of a core file (see core(5)) in bytes that the process may
108+
/// dump.
17109
RLIMIT_CORE,
18110
/// This is a limit, in seconds, on the amount of CPU time that the process can consume.
19111
RLIMIT_CPU,
20-
/// This is the maximum size of the process's data segment (initialized data, uninitialized data, and heap). The limit is specified in bytes, and is rounded down to the system page size.
112+
/// This is the maximum size of the process's data segment (initialized data, uninitialized
113+
/// data, and heap). The limit is specified in bytes, and is rounded down to the system
114+
/// page size.
21115
RLIMIT_DATA,
22-
/// This is the maximum size in bytes of files that the process may create. Attempts to extend a file beyond this limit result in delivery of a SIGXFSZ signal.
116+
/// This is the maximum size in bytes of files that the process may create. Attempts to
117+
/// extend a file beyond this limit result in delivery of a SIGXFSZ signal.
23118
RLIMIT_FSIZE,
24-
/// This specifies a value one greater than the maximum file descriptor number that can be opened by this process.
119+
/// This specifies a value one greater than the maximum file descriptor number that can be
120+
/// opened by this process.
25121
RLIMIT_NOFILE,
26-
/// This is the maximum size of the process stack, in bytes. Upon reaching this limit, a SIGSEGV signal is generated.
122+
/// This is the maximum size of the process stack, in bytes. Upon reaching this limit, a
123+
/// SIGSEGV signal is generated.
27124
RLIMIT_STACK,
28125

29126
// BSDs and Linux
30-
/// This is the maximum number of bytes of memory that may be locked into RAM. This limit is in effect rounded down to the nearest multiple of the system page size.
127+
/// This is the maximum number of bytes of memory that may be locked into RAM. This limit
128+
/// is in effect rounded down to the nearest multiple of the system page size.
31129
#[cfg(any(target_os = "android", target_os = "freebsd", target_os = "linux", target_os = "openbsd"))]
32130
RLIMIT_MEMLOCK,
33-
/// This is a limit on the number of extant process (or, more precisely on Linux, threads) for the real user ID of the calling process.
131+
/// This is a limit on the number of extant process (or, more precisely on Linux, threads)
132+
/// for the real user ID of the calling process.
34133
#[cfg(any(target_os = "android", target_os = "freebsd", target_os = "linux", target_os = "openbsd"))]
35134
RLIMIT_NPROC,
36135
/// This is a limit (in bytes) on the process's resident set (the number of virtual pages resident in RAM).
@@ -41,19 +140,26 @@ libc_enum!{
41140
/// This is a limit on the combined number of flock(2) locks and fcntl(2) leases that this process may establish.
42141
#[cfg(any(target_os = "android", target_os = "linux"))]
43142
RLIMIT_LOCKS,
44-
/// This is a limit on the number of bytes that can be allocated for POSIX message queues for the real user ID of the calling process.
143+
/// This is a limit on the number of bytes that can be allocated for POSIX message queues
144+
/// for the real user ID of the calling process.
45145
#[cfg(any(target_os = "android", target_os = "linux"))]
46146
RLIMIT_MSGQUEUE,
47-
/// This specifies a ceiling to which the process's nice value can be raised using setpriority(2) or nice(2). The actual ceiling for the nice value is calculated as 20 - rlim_cur.
147+
/// This specifies a ceiling to which the process's nice value can be raised using
148+
/// setpriority(2) or nice(2). The actual ceiling for the nice value is calculated as 20 -
149+
/// rlim_cur.
48150
#[cfg(any(target_os = "android", target_os = "linux"))]
49151
RLIMIT_NICE,
50-
/// This specifies a ceiling on the real-time priority that may be set for this process using sched_setscheduler(2) and sched_setparam(2).
152+
/// This specifies a ceiling on the real-time priority that may be set for this process
153+
/// using sched_setscheduler(2) and sched_setparam(2).
51154
#[cfg(any(target_os = "android", target_os = "linux"))]
52155
RLIMIT_RTPRIO,
53-
/// This is a limit (in microseconds) on the amount of CPU time that a process scheduled under a real-time scheduling policy may consume without making a blocking system call.
156+
/// This is a limit (in microseconds) on the amount of CPU time that a process scheduled
157+
/// under a real-time scheduling policy may consume without making a blocking system call.
54158
#[cfg(any(target_os = "android", target_os = "linux"))]
55159
RLIMIT_RTTIME,
56-
/// This is a limit on the number of signals that may be queued for the real user ID of the calling process. Both standard and real-time signals are counted for the purpose of checking this limit.
160+
/// This is a limit on the number of signals that may be queued for the real user ID of the
161+
/// calling process. Both standard and real-time signals are counted for the purpose of
162+
/// checking this limit.
57163
#[cfg(any(target_os = "android", target_os = "linux"))]
58164
RLIMIT_SIGPENDING,
59165

@@ -97,8 +203,14 @@ libc_enum!{
97203
///
98204
/// [`Resource`]: enum.Resource.html
99205
pub fn getrlimit(resource: Resource) -> Result<(Option<rlim_t>, Option<rlim_t>)> {
100-
let mut rlim: rlimit = unsafe { mem::MaybeUninit::uninit().assume_init() };
101-
let res = unsafe { libc::getrlimit(resource as c_uint, &mut rlim as *mut _) };
206+
let mut rlim = mem::MaybeUninit::<&rlimit>::uninit();
207+
208+
#[cfg(not(any(target_os = "freebsd", target_os = "macos", target_os = "openbsd", target_os = "netbsd")))]
209+
let res = unsafe { libc::getrlimit(resource as __rlimit_resource_t, rlim.as_mut_ptr() as *mut _) };
210+
#[cfg(any(target_os = "freebsd", target_os = "macos", target_os = "openbsd", target_os = "netbsd"))]
211+
let res = unsafe { libc::getrlimit(resource as c_int, rlim.as_mut_ptr() as *mut _) };
212+
213+
let rlim = unsafe { rlim.assume_init() };
102214
// TODO: use Option::filter after it has been stabilized
103215
Errno::result(res).map(|_| {
104216
(if rlim.rlim_cur != RLIM_INFINITY { Some(rlim.rlim_cur) } else { None },
@@ -133,10 +245,14 @@ pub fn getrlimit(resource: Resource) -> Result<(Option<rlim_t>, Option<rlim_t>)>
133245
///
134246
/// [`Resource`]: enum.Resource.html
135247
pub fn setrlimit(resource: Resource, soft_limit: Option<rlim_t>, hard_limit: Option<rlim_t>) -> Result<()> {
136-
let mut rlim: rlimit = unsafe { mem::MaybeUninit::uninit().assume_init() };
248+
let mut rlim: rlimit = unsafe { mem::zeroed() };
137249
rlim.rlim_cur = soft_limit.unwrap_or(RLIM_INFINITY);
138250
rlim.rlim_max = hard_limit.unwrap_or(RLIM_INFINITY);
139251

140-
let res = unsafe { libc::setrlimit(resource as c_uint, &rlim as *const _) };
252+
#[cfg(not(any(target_os = "freebsd", target_os = "macos", target_os = "openbsd", target_os = "netbsd")))]
253+
let res = unsafe { libc::setrlimit(resource as __rlimit_resource_t, &rlim as *const _) };
254+
#[cfg(any(target_os = "freebsd", target_os = "macos", target_os = "openbsd", target_os = "netbsd"))]
255+
let res = unsafe { libc::setrlimit(resource as c_int, &rlim as *const _) };
256+
141257
Errno::result(res).map(|_| ())
142258
}

0 commit comments

Comments
 (0)