]>
Commit | Line | Data |
---|---|---|
78f22b97 LP |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2010 Lennart Poettering | |
5 | ||
6 | systemd is free software; you can redistribute it and/or modify it | |
7 | under the terms of the GNU Lesser General Public License as published by | |
8 | the Free Software Foundation; either version 2.1 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | systemd is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public License | |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
18 | ***/ | |
19 | ||
11c3a366 TA |
20 | #include <errno.h> |
21 | #include <sys/resource.h> | |
22 | ||
d0a7c5f6 LP |
23 | #include "alloc-util.h" |
24 | #include "extract-word.h" | |
f97b34a6 | 25 | #include "format-util.h" |
93cc7779 | 26 | #include "macro.h" |
78f22b97 LP |
27 | #include "missing.h" |
28 | #include "rlimit-util.h" | |
8b43440b | 29 | #include "string-table.h" |
d0a7c5f6 | 30 | #include "time-util.h" |
78f22b97 LP |
31 | |
32 | int setrlimit_closest(int resource, const struct rlimit *rlim) { | |
33 | struct rlimit highest, fixed; | |
34 | ||
35 | assert(rlim); | |
36 | ||
37 | if (setrlimit(resource, rlim) >= 0) | |
38 | return 0; | |
39 | ||
40 | if (errno != EPERM) | |
41 | return -errno; | |
42 | ||
43 | /* So we failed to set the desired setrlimit, then let's try | |
44 | * to get as close as we can */ | |
c4ad3f43 LP |
45 | if (getrlimit(resource, &highest) < 0) |
46 | return -errno; | |
78f22b97 LP |
47 | |
48 | fixed.rlim_cur = MIN(rlim->rlim_cur, highest.rlim_max); | |
49 | fixed.rlim_max = MIN(rlim->rlim_max, highest.rlim_max); | |
50 | ||
51 | if (setrlimit(resource, &fixed) < 0) | |
52 | return -errno; | |
53 | ||
54 | return 0; | |
55 | } | |
56 | ||
d0a7c5f6 LP |
57 | static int rlimit_parse_u64(const char *val, rlim_t *ret) { |
58 | uint64_t u; | |
59 | int r; | |
60 | ||
61 | assert(val); | |
62 | assert(ret); | |
63 | ||
64 | if (streq(val, "infinity")) { | |
65 | *ret = RLIM_INFINITY; | |
66 | return 0; | |
67 | } | |
68 | ||
69 | /* setrlimit(2) suggests rlim_t is always 64bit on Linux. */ | |
70 | assert_cc(sizeof(rlim_t) == sizeof(uint64_t)); | |
71 | ||
72 | r = safe_atou64(val, &u); | |
73 | if (r < 0) | |
74 | return r; | |
75 | if (u >= (uint64_t) RLIM_INFINITY) | |
76 | return -ERANGE; | |
77 | ||
78 | *ret = (rlim_t) u; | |
79 | return 0; | |
80 | } | |
81 | ||
82 | static int rlimit_parse_size(const char *val, rlim_t *ret) { | |
83 | uint64_t u; | |
84 | int r; | |
85 | ||
86 | assert(val); | |
87 | assert(ret); | |
88 | ||
89 | if (streq(val, "infinity")) { | |
90 | *ret = RLIM_INFINITY; | |
91 | return 0; | |
92 | } | |
93 | ||
94 | r = parse_size(val, 1024, &u); | |
95 | if (r < 0) | |
96 | return r; | |
97 | if (u >= (uint64_t) RLIM_INFINITY) | |
98 | return -ERANGE; | |
99 | ||
100 | *ret = (rlim_t) u; | |
101 | return 0; | |
102 | } | |
103 | ||
104 | static int rlimit_parse_sec(const char *val, rlim_t *ret) { | |
105 | uint64_t u; | |
106 | usec_t t; | |
107 | int r; | |
108 | ||
109 | assert(val); | |
110 | assert(ret); | |
111 | ||
112 | if (streq(val, "infinity")) { | |
113 | *ret = RLIM_INFINITY; | |
114 | return 0; | |
115 | } | |
116 | ||
117 | r = parse_sec(val, &t); | |
118 | if (r < 0) | |
119 | return r; | |
120 | if (t == USEC_INFINITY) { | |
121 | *ret = RLIM_INFINITY; | |
122 | return 0; | |
123 | } | |
124 | ||
125 | u = (uint64_t) DIV_ROUND_UP(t, USEC_PER_SEC); | |
126 | if (u >= (uint64_t) RLIM_INFINITY) | |
127 | return -ERANGE; | |
128 | ||
129 | *ret = (rlim_t) u; | |
130 | return 0; | |
131 | } | |
132 | ||
133 | static int rlimit_parse_usec(const char *val, rlim_t *ret) { | |
134 | usec_t t; | |
135 | int r; | |
136 | ||
137 | assert(val); | |
138 | assert(ret); | |
139 | ||
140 | if (streq(val, "infinity")) { | |
141 | *ret = RLIM_INFINITY; | |
142 | return 0; | |
143 | } | |
144 | ||
145 | r = parse_time(val, &t, 1); | |
146 | if (r < 0) | |
147 | return r; | |
148 | if (t == USEC_INFINITY) { | |
149 | *ret = RLIM_INFINITY; | |
150 | return 0; | |
151 | } | |
152 | ||
153 | *ret = (rlim_t) t; | |
154 | return 0; | |
155 | } | |
156 | ||
29857001 LP |
157 | static int rlimit_parse_nice(const char *val, rlim_t *ret) { |
158 | uint64_t rl; | |
159 | int r; | |
160 | ||
161 | /* So, Linux is weird. The range for RLIMIT_NICE is 40..1, mapping to the nice levels -20..19. However, the | |
162 | * RLIMIT_NICE limit defaults to 0 by the kernel, i.e. a value that maps to nice level 20, which of course is | |
163 | * bogus and does not exist. In order to permit parsing the RLIMIT_NICE of 0 here we hence implement a slight | |
164 | * asymmetry: when parsing as positive nice level we permit 0..19. When parsing as negative nice level, we | |
165 | * permit -20..0. But when parsing as raw resource limit value then we also allow the special value 0. | |
166 | * | |
167 | * Yeah, Linux is quality engineering sometimes... */ | |
168 | ||
169 | if (val[0] == '+') { | |
170 | ||
171 | /* Prefixed with "+": Parse as positive user-friendly nice value */ | |
172 | r = safe_atou64(val + 1, &rl); | |
173 | if (r < 0) | |
174 | return r; | |
175 | ||
176 | if (rl >= PRIO_MAX) | |
177 | return -ERANGE; | |
178 | ||
179 | rl = 20 - rl; | |
180 | ||
181 | } else if (val[0] == '-') { | |
182 | ||
183 | /* Prefixed with "-": Parse as negative user-friendly nice value */ | |
184 | r = safe_atou64(val + 1, &rl); | |
185 | if (r < 0) | |
186 | return r; | |
187 | ||
188 | if (rl > (uint64_t) (-PRIO_MIN)) | |
189 | return -ERANGE; | |
190 | ||
191 | rl = 20 + rl; | |
192 | } else { | |
193 | ||
194 | /* Not prefixed: parse as raw resource limit value */ | |
195 | r = safe_atou64(val, &rl); | |
196 | if (r < 0) | |
197 | return r; | |
198 | ||
199 | if (rl > (uint64_t) (20 - PRIO_MIN)) | |
200 | return -ERANGE; | |
201 | } | |
202 | ||
203 | *ret = (rlim_t) rl; | |
204 | return 0; | |
205 | } | |
206 | ||
d0a7c5f6 LP |
207 | static int (*const rlimit_parse_table[_RLIMIT_MAX])(const char *val, rlim_t *ret) = { |
208 | [RLIMIT_CPU] = rlimit_parse_sec, | |
209 | [RLIMIT_FSIZE] = rlimit_parse_size, | |
210 | [RLIMIT_DATA] = rlimit_parse_size, | |
211 | [RLIMIT_STACK] = rlimit_parse_size, | |
212 | [RLIMIT_CORE] = rlimit_parse_size, | |
213 | [RLIMIT_RSS] = rlimit_parse_size, | |
214 | [RLIMIT_NOFILE] = rlimit_parse_u64, | |
215 | [RLIMIT_AS] = rlimit_parse_size, | |
216 | [RLIMIT_NPROC] = rlimit_parse_u64, | |
217 | [RLIMIT_MEMLOCK] = rlimit_parse_size, | |
218 | [RLIMIT_LOCKS] = rlimit_parse_u64, | |
219 | [RLIMIT_SIGPENDING] = rlimit_parse_u64, | |
220 | [RLIMIT_MSGQUEUE] = rlimit_parse_size, | |
29857001 | 221 | [RLIMIT_NICE] = rlimit_parse_nice, |
d0a7c5f6 LP |
222 | [RLIMIT_RTPRIO] = rlimit_parse_u64, |
223 | [RLIMIT_RTTIME] = rlimit_parse_usec, | |
224 | }; | |
225 | ||
226 | int rlimit_parse_one(int resource, const char *val, rlim_t *ret) { | |
227 | assert(val); | |
228 | assert(ret); | |
229 | ||
230 | if (resource < 0) | |
231 | return -EINVAL; | |
232 | if (resource >= _RLIMIT_MAX) | |
233 | return -EINVAL; | |
234 | ||
235 | return rlimit_parse_table[resource](val, ret); | |
236 | } | |
237 | ||
238 | int rlimit_parse(int resource, const char *val, struct rlimit *ret) { | |
239 | _cleanup_free_ char *hard = NULL, *soft = NULL; | |
240 | rlim_t hl, sl; | |
241 | int r; | |
242 | ||
243 | assert(val); | |
244 | assert(ret); | |
245 | ||
246 | r = extract_first_word(&val, &soft, ":", EXTRACT_DONT_COALESCE_SEPARATORS); | |
247 | if (r < 0) | |
248 | return r; | |
249 | if (r == 0) | |
250 | return -EINVAL; | |
251 | ||
252 | r = rlimit_parse_one(resource, soft, &sl); | |
253 | if (r < 0) | |
254 | return r; | |
255 | ||
256 | r = extract_first_word(&val, &hard, ":", EXTRACT_DONT_COALESCE_SEPARATORS); | |
257 | if (r < 0) | |
258 | return r; | |
259 | if (!isempty(val)) | |
260 | return -EINVAL; | |
261 | if (r == 0) | |
262 | hl = sl; | |
263 | else { | |
264 | r = rlimit_parse_one(resource, hard, &hl); | |
265 | if (r < 0) | |
266 | return r; | |
267 | if (sl > hl) | |
268 | return -EILSEQ; | |
269 | } | |
270 | ||
271 | *ret = (struct rlimit) { | |
272 | .rlim_cur = sl, | |
273 | .rlim_max = hl, | |
274 | }; | |
275 | ||
276 | return 0; | |
277 | } | |
278 | ||
99d4f5e5 LP |
279 | int rlimit_format(const struct rlimit *rl, char **ret) { |
280 | char *s = NULL; | |
281 | ||
282 | assert(rl); | |
283 | assert(ret); | |
284 | ||
285 | if (rl->rlim_cur >= RLIM_INFINITY && rl->rlim_max >= RLIM_INFINITY) | |
286 | s = strdup("infinity"); | |
287 | else if (rl->rlim_cur >= RLIM_INFINITY) | |
288 | (void) asprintf(&s, "infinity:" RLIM_FMT, rl->rlim_max); | |
289 | else if (rl->rlim_max >= RLIM_INFINITY) | |
290 | (void) asprintf(&s, RLIM_FMT ":infinity", rl->rlim_cur); | |
291 | else if (rl->rlim_cur == rl->rlim_max) | |
292 | (void) asprintf(&s, RLIM_FMT, rl->rlim_cur); | |
293 | else | |
294 | (void) asprintf(&s, RLIM_FMT ":" RLIM_FMT, rl->rlim_cur, rl->rlim_max); | |
295 | ||
296 | if (!s) | |
297 | return -ENOMEM; | |
298 | ||
299 | *ret = s; | |
300 | return 0; | |
301 | } | |
302 | ||
78f22b97 LP |
303 | static const char* const rlimit_table[_RLIMIT_MAX] = { |
304 | [RLIMIT_CPU] = "LimitCPU", | |
305 | [RLIMIT_FSIZE] = "LimitFSIZE", | |
306 | [RLIMIT_DATA] = "LimitDATA", | |
307 | [RLIMIT_STACK] = "LimitSTACK", | |
308 | [RLIMIT_CORE] = "LimitCORE", | |
309 | [RLIMIT_RSS] = "LimitRSS", | |
310 | [RLIMIT_NOFILE] = "LimitNOFILE", | |
311 | [RLIMIT_AS] = "LimitAS", | |
312 | [RLIMIT_NPROC] = "LimitNPROC", | |
313 | [RLIMIT_MEMLOCK] = "LimitMEMLOCK", | |
314 | [RLIMIT_LOCKS] = "LimitLOCKS", | |
315 | [RLIMIT_SIGPENDING] = "LimitSIGPENDING", | |
316 | [RLIMIT_MSGQUEUE] = "LimitMSGQUEUE", | |
317 | [RLIMIT_NICE] = "LimitNICE", | |
318 | [RLIMIT_RTPRIO] = "LimitRTPRIO", | |
319 | [RLIMIT_RTTIME] = "LimitRTTIME" | |
320 | }; | |
321 | ||
322 | DEFINE_STRING_TABLE_LOOKUP(rlimit, int); |