Skip to content

Commit da50e56

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

File tree

1 file changed

+120
-18
lines changed

1 file changed

+120
-18
lines changed

src/sys/resource.rs

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

4-
use libc::{self, c_uint, rlimit, RLIM_INFINITY};
4+
use libc::{self, rlimit, __rlimit_resource_t, RLIM_INFINITY};
55
pub use libc::rlim_t;
66

77
use {Errno, Result};
88

9+
#[cfg(target_os = "linux")]
910
libc_enum!{
1011
/// A resource that limits apply to
1112
#[repr(u32)]
1213
pub enum Resource {
1314
// 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.
15+
/// This is the maximum size of the process's virtual memory (address space). The limit is
16+
/// specified in bytes, and is rounded down to the system page size.
1517
RLIMIT_AS,
16-
/// This is the maximum size of a core file (see core(5)) in bytes that the process may dump.
18+
/// This is the maximum size of a core file (see core(5)) in bytes that the process may
19+
/// dump.
1720
RLIMIT_CORE,
1821
/// This is a limit, in seconds, on the amount of CPU time that the process can consume.
1922
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.
23+
/// This is the maximum size of the process's data segment (initialized data, uninitialized
24+
/// data, and heap). The limit is specified in bytes, and is rounded down to the system
25+
/// page size.
2126
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.
27+
/// This is the maximum size in bytes of files that the process may create. Attempts to
28+
/// extend a file beyond this limit result in delivery of a SIGXFSZ signal.
2329
RLIMIT_FSIZE,
24-
/// This specifies a value one greater than the maximum file descriptor number that can be opened by this process.
30+
/// This specifies a value one greater than the maximum file descriptor number that can be
31+
/// opened by this process.
2532
RLIMIT_NOFILE,
26-
/// This is the maximum size of the process stack, in bytes. Upon reaching this limit, a SIGSEGV signal is generated.
33+
/// This is the maximum size of the process stack, in bytes. Upon reaching this limit, a
34+
/// SIGSEGV signal is generated.
2735
RLIMIT_STACK,
2836

2937
// 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.
38+
/// This is the maximum number of bytes of memory that may be locked into RAM. This limit
39+
/// is in effect rounded down to the nearest multiple of the system page size.
3140
#[cfg(any(target_os = "android", target_os = "freebsd", target_os = "linux", target_os = "openbsd"))]
3241
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.
42+
/// This is a limit on the number of extant process (or, more precisely on Linux, threads)
43+
/// for the real user ID of the calling process.
3444
#[cfg(any(target_os = "android", target_os = "freebsd", target_os = "linux", target_os = "openbsd"))]
3545
RLIMIT_NPROC,
3646
/// This is a limit (in bytes) on the process's resident set (the number of virtual pages resident in RAM).
@@ -41,19 +51,110 @@ libc_enum!{
4151
/// This is a limit on the combined number of flock(2) locks and fcntl(2) leases that this process may establish.
4252
#[cfg(any(target_os = "android", target_os = "linux"))]
4353
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.
54+
/// This is a limit on the number of bytes that can be allocated for POSIX message queues
55+
/// for the real user ID of the calling process.
4556
#[cfg(any(target_os = "android", target_os = "linux"))]
4657
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.
58+
/// This specifies a ceiling to which the process's nice value can be raised using
59+
/// setpriority(2) or nice(2). The actual ceiling for the nice value is calculated as 20 -
60+
/// rlim_cur.
4861
#[cfg(any(target_os = "android", target_os = "linux"))]
4962
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).
63+
/// This specifies a ceiling on the real-time priority that may be set for this process
64+
/// using sched_setscheduler(2) and sched_setparam(2).
5165
#[cfg(any(target_os = "android", target_os = "linux"))]
5266
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.
67+
/// This is a limit (in microseconds) on the amount of CPU time that a process scheduled
68+
/// under a real-time scheduling policy may consume without making a blocking system call.
5469
#[cfg(any(target_os = "android", target_os = "linux"))]
5570
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.
71+
/// This is a limit on the number of signals that may be queued for the real user ID of the
72+
/// calling process. Both standard and real-time signals are counted for the purpose of
73+
/// checking this limit.
74+
#[cfg(any(target_os = "android", target_os = "linux"))]
75+
RLIMIT_SIGPENDING,
76+
77+
// Available on some BSD
78+
/// The maximum number of kqueues this user id is allowed to create.
79+
#[cfg(target_os = "freebsd")]
80+
RLIMIT_KQUEUES,
81+
/// The maximum number of pseudo-terminals this user id is allowed to create.
82+
#[cfg(target_os = "freebsd")]
83+
RLIMIT_NPTS,
84+
/// The maximum size (in bytes) of socket buffer usage for this user.
85+
#[cfg(any(target_os = "freebsd", target_os = "dragonfly"))]
86+
RLIMIT_SBSIZE,
87+
/// The maximum size (in bytes) of the swap space that may be reserved or used by all of this user id's processes.
88+
#[cfg(target_os = "freebsd")]
89+
RLIMIT_SWAP,
90+
}
91+
}
92+
93+
#[cfg(not(target_os = "linux"))]
94+
libc_enum!{
95+
/// A resource that limits apply to
96+
#[repr(i32)]
97+
pub enum Resource {
98+
// POSIX
99+
/// This is the maximum size of the process's virtual memory (address space). The limit is
100+
/// specified in bytes, and is rounded down to the system page size.
101+
RLIMIT_AS,
102+
/// This is the maximum size of a core file (see core(5)) in bytes that the process may
103+
/// dump.
104+
RLIMIT_CORE,
105+
/// This is a limit, in seconds, on the amount of CPU time that the process can consume.
106+
RLIMIT_CPU,
107+
/// This is the maximum size of the process's data segment (initialized data, uninitialized
108+
/// data, and heap). The limit is specified in bytes, and is rounded down to the system
109+
/// page size.
110+
RLIMIT_DATA,
111+
/// This is the maximum size in bytes of files that the process may create. Attempts to
112+
/// extend a file beyond this limit result in delivery of a SIGXFSZ signal.
113+
RLIMIT_FSIZE,
114+
/// This specifies a value one greater than the maximum file descriptor number that can be
115+
/// opened by this process.
116+
RLIMIT_NOFILE,
117+
/// This is the maximum size of the process stack, in bytes. Upon reaching this limit, a
118+
/// SIGSEGV signal is generated.
119+
RLIMIT_STACK,
120+
121+
// BSDs and Linux
122+
/// This is the maximum number of bytes of memory that may be locked into RAM. This limit
123+
/// is in effect rounded down to the nearest multiple of the system page size.
124+
#[cfg(any(target_os = "android", target_os = "freebsd", target_os = "linux", target_os = "openbsd"))]
125+
RLIMIT_MEMLOCK,
126+
/// This is a limit on the number of extant process (or, more precisely on Linux, threads)
127+
/// for the real user ID of the calling process.
128+
#[cfg(any(target_os = "android", target_os = "freebsd", target_os = "linux", target_os = "openbsd"))]
129+
RLIMIT_NPROC,
130+
/// This is a limit (in bytes) on the process's resident set (the number of virtual pages resident in RAM).
131+
#[cfg(any(target_os = "android", target_os = "freebsd", target_os = "linux", target_os = "openbsd"))]
132+
RLIMIT_RSS,
133+
134+
// Android and Linux only
135+
/// This is a limit on the combined number of flock(2) locks and fcntl(2) leases that this process may establish.
136+
#[cfg(any(target_os = "android", target_os = "linux"))]
137+
RLIMIT_LOCKS,
138+
/// This is a limit on the number of bytes that can be allocated for POSIX message queues
139+
/// for the real user ID of the calling process.
140+
#[cfg(any(target_os = "android", target_os = "linux"))]
141+
RLIMIT_MSGQUEUE,
142+
/// This specifies a ceiling to which the process's nice value can be raised using
143+
/// setpriority(2) or nice(2). The actual ceiling for the nice value is calculated as 20 -
144+
/// rlim_cur.
145+
#[cfg(any(target_os = "android", target_os = "linux"))]
146+
RLIMIT_NICE,
147+
/// This specifies a ceiling on the real-time priority that may be set for this process
148+
/// using sched_setscheduler(2) and sched_setparam(2).
149+
#[cfg(any(target_os = "android", target_os = "linux"))]
150+
RLIMIT_RTPRIO,
151+
/// This is a limit (in microseconds) on the amount of CPU time that a process scheduled
152+
/// under a real-time scheduling policy may consume without making a blocking system call.
153+
#[cfg(any(target_os = "android", target_os = "linux"))]
154+
RLIMIT_RTTIME,
155+
/// This is a limit on the number of signals that may be queued for the real user ID of the
156+
/// calling process. Both standard and real-time signals are counted for the purpose of
157+
/// checking this limit.
57158
#[cfg(any(target_os = "android", target_os = "linux"))]
58159
RLIMIT_SIGPENDING,
59160

@@ -97,8 +198,9 @@ libc_enum!{
97198
///
98199
/// [`Resource`]: enum.Resource.html
99200
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 _) };
201+
let mut rlim = mem::MaybeUninit::<&rlimit>::uninit();
202+
let res = unsafe { libc::getrlimit(resource as __rlimit_resource_t, rlim.as_mut_ptr() as *mut _) };
203+
let rlim = unsafe { rlim.assume_init() };
102204
// TODO: use Option::filter after it has been stabilized
103205
Errno::result(res).map(|_| {
104206
(if rlim.rlim_cur != RLIM_INFINITY { Some(rlim.rlim_cur) } else { None },
@@ -133,10 +235,10 @@ pub fn getrlimit(resource: Resource) -> Result<(Option<rlim_t>, Option<rlim_t>)>
133235
///
134236
/// [`Resource`]: enum.Resource.html
135237
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() };
238+
let mut rlim: rlimit = unsafe { mem::zeroed() };
137239
rlim.rlim_cur = soft_limit.unwrap_or(RLIM_INFINITY);
138240
rlim.rlim_max = hard_limit.unwrap_or(RLIM_INFINITY);
139241

140-
let res = unsafe { libc::setrlimit(resource as c_uint, &rlim as *const _) };
242+
let res = unsafe { libc::setrlimit(resource as __rlimit_resource_t, &rlim as *const _) };
141243
Errno::result(res).map(|_| ())
142244
}

0 commit comments

Comments
 (0)