Skip to content

Commit c7eb876

Browse files
committed
Address reviewer's comment
1 parent e5eefe9 commit c7eb876

File tree

1 file changed

+81
-111
lines changed

1 file changed

+81
-111
lines changed

src/sys/resource.rs

Lines changed: 81 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -5,111 +5,98 @@ use crate::errno::Errno;
55
use crate::Result;
66

77
pub use libc::rlim_t;
8-
98
cfg_if! {
109
if #[cfg(all(target_os = "linux", target_env = "gnu"))]{
1110
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{
4513
use libc::{c_int, rlimit, RLIM_INFINITY};
14+
}
15+
}
4616

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,
6050

61-
// platform specific
62-
#[cfg(target_os = "freebsd")]
63-
RLIMIT_KQUEUES,
51+
// platform specific
52+
#[cfg(target_os = "freebsd")]
53+
RLIMIT_KQUEUES,
6454

65-
#[cfg(any(target_os = "android", target_os = "linux"))]
66-
RLIMIT_LOCKS,
55+
#[cfg(any(target_os = "android", target_os = "linux"))]
56+
RLIMIT_LOCKS,
6757

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,
7060

71-
#[cfg(any(target_os = "android", target_os = "linux"))]
72-
RLIMIT_MSGQUEUE,
61+
#[cfg(any(target_os = "android", target_os = "linux"))]
62+
RLIMIT_MSGQUEUE,
7363

74-
#[cfg(any(target_os = "android", target_os = "linux"))]
75-
RLIMIT_NICE,
64+
#[cfg(any(target_os = "android", target_os = "linux"))]
65+
RLIMIT_NICE,
7666

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,
7969

80-
#[cfg(target_os = "freebsd")]
81-
RLIMIT_NPTS,
70+
#[cfg(target_os = "freebsd")]
71+
RLIMIT_NPTS,
8272

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,
8575

86-
#[cfg(any(target_os = "android", target_os = "linux"))]
87-
RLIMIT_RTPRIO,
76+
#[cfg(any(target_os = "android", target_os = "linux"))]
77+
RLIMIT_RTPRIO,
8878

89-
#[cfg(any(target_os = "linux"))]
90-
RLIMIT_RTTIME,
79+
#[cfg(any(target_os = "linux"))]
80+
RLIMIT_RTTIME,
9181

92-
#[cfg(any(target_os = "android", target_os = "linux"))]
93-
RLIMIT_SIGPENDING,
82+
#[cfg(any(target_os = "android", target_os = "linux"))]
83+
RLIMIT_SIGPENDING,
9484

95-
#[cfg(any(target_os = "freebsd", target_os = "dragonfly"))]
96-
RLIMIT_SBSIZE,
85+
#[cfg(any(target_os = "freebsd", target_os = "dragonfly"))]
86+
RLIMIT_SBSIZE,
9787

98-
#[cfg(target_os = "freebsd")]
99-
RLIMIT_SWAP,
88+
#[cfg(target_os = "freebsd")]
89+
RLIMIT_SWAP,
10090

101-
#[cfg(target_os = "freebsd")]
102-
RLIMIT_VMEM,
103-
}
104-
}
105-
}else{
106-
// unkown os
91+
#[cfg(target_os = "freebsd")]
92+
RLIMIT_VMEM,
10793
}
10894
}
10995

11096
/// Get the current processes resource limits
11197
///
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.
113100
///
114101
/// # Parameters
115102
///
@@ -127,7 +114,7 @@ cfg_if! {
127114
///
128115
/// # References
129116
///
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)
131118
///
132119
/// [`Resource`]: enum.Resource.html
133120
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>)>
139126
cfg_if! {
140127
if #[cfg(all(target_os = "linux", target_env = "gnu"))]{
141128
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{
153130
let res = unsafe { libc::getrlimit(resource as c_int, &mut old_rlim) };
154131
}
155132
}
@@ -164,8 +141,6 @@ pub fn getrlimit(resource: Resource) -> Result<(Option<rlim_t>, Option<rlim_t>)>
164141

165142
/// Set the current processes resource limits
166143
///
167-
/// A value of `None` indicates that there's no limit.
168-
///
169144
/// # Parameters
170145
///
171146
/// * `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>)>
175150
/// the current hard limit for non-root users. Note: `None` input will be
176151
/// replaced by constant `RLIM_INFINITY`.
177152
///
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+
///
178156
/// # Examples
179157
///
180-
/// ```no_run
158+
/// ```
181159
/// # use nix::sys::resource::{setrlimit, Resource};
182160
///
183161
/// let soft_limit = Some(1024);
184-
/// let hard_limit = None;
162+
/// let hard_limit = Some(1048576);
185163
/// setrlimit(Resource::RLIMIT_NOFILE, soft_limit, hard_limit).unwrap();
186164
/// ```
187165
///
188166
/// # References
189167
///
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)
191169
///
192170
/// [`Resource`]: enum.Resource.html
193171
pub fn setrlimit(
@@ -206,22 +184,14 @@ pub fn setrlimit(
206184
// seems for some of the architectures, we prefer to use prlimit instead of {g,s}etrlimit
207185
let res = unsafe { libc::prlimit(0, resource as __rlimit_resource_t, &new_rlim as *const _, std::ptr::null_mut()) };
208186
if res == -1 {
209-
return Errno::result(res).map(|_| ());
187+
match Errno::last() {
188+
Errno::ENOSYS =>{}
189+
e => {return Err(Error::Sys(e));}
190+
}
210191
}
211192

212193
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{
225195
let res = unsafe { libc::setrlimit(resource as c_int, &new_rlim as *const _) };
226196
}
227197
}

0 commit comments

Comments
 (0)