Debian Codegraph @

src/basic/rlimit-util.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "alloc-util.h"
4 #include "errno-util.h"
5 #include "extract-word.h"
6 #include "fd-util.h"
7 #include "fileio.h"
8 #include "format-util.h"
9 #include "log.h"
10 #include "parse-util.h"
11 #include "process-util.h"
12 #include "rlimit-util.h"
13 #include "string-table.h"
14 #include "strv.h"
15 #include "time-util.h"
16
17 int setrlimit_closest(int resource, const struct rlimit *rlim) {
18 struct rlimit highest, fixed;
19
20 assert(rlim);
21
22 if (setrlimit(resource, rlim) >= 0)
23 return 0;
24
25 if (errno != EPERM)
26 return -errno;
27
28 /* So we failed to set the desired setrlimit, then let's try
29 * to get as close as we can */
30 if (getrlimit(resource, &highest) < 0)
31 return -errno;
32
33 /* If the hard limit is unbounded anyway, then the EPERM had other reasons, let's propagate the original EPERM
34 * then */
35 if (highest.rlim_max == RLIM_INFINITY)
36 return -EPERM;
37
38 fixed = (struct rlimit) {
39 .rlim_cur = MIN(rlim->rlim_cur, highest.rlim_max),
40 .rlim_max = MIN(rlim->rlim_max, highest.rlim_max),
41 };
42
43 /* Shortcut things if we wouldn't change anything. */
44 if (fixed.rlim_cur == highest.rlim_cur &&
45 fixed.rlim_max == highest.rlim_max)
46 return 0;
47
48 log_debug("Failed at setting rlimit " RLIM_FMT " for resource RLIMIT_%s. Will attempt setting value " RLIM_FMT " instead.", rlim->rlim_max, rlimit_to_string(resource), fixed.rlim_max);
49
50 return RET_NERRNO(setrlimit(resource, &fixed));
51 }
52
53 int setrlimit_closest_all(const struct rlimit *const *rlim, int *which_failed) {
54 int r;
55
56 assert(rlim);
57
58 /* On failure returns the limit's index that failed in *which_failed, but only if non-NULL */
59
60 for (int i = 0; i < _RLIMIT_MAX; i++) {
61 if (!rlim[i])
62 continue;
63
64 r = setrlimit_closest(i, rlim[i]);
65 if (r < 0) {
66 if (which_failed)
67 *which_failed = i;
68
69 return r;
70 }
71 }
72
73 if (which_failed)
74 *which_failed = -1;
75
76 return 0;
77 }
78
79 static int rlimit_parse_u64(const char *val, rlim_t *ret) {
80 uint64_t u;
81 int r;
82
83 assert(val);
84 assert(ret);
85
86 if (streq(val, "infinity")) {
87 *ret = RLIM_INFINITY;
88 return 0;
89 }
90
91 /* setrlimit(2) suggests rlim_t is always 64-bit on Linux. */
92 assert_cc(sizeof(rlim_t) == sizeof(uint64_t));
93
94 r = safe_atou64(val, &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_size(const char *val, rlim_t *ret) {
105 uint64_t u;
106 int r;
107
108 assert(val);
109 assert(ret);
110
111 if (streq(val, "infinity")) {
112 *ret = RLIM_INFINITY;
113 return 0;
114 }
115
116 r = parse_size(val, 1024, &u);
117 if (r < 0)
118 return r;
119 if (u >= (uint64_t) RLIM_INFINITY)
120 return -ERANGE;
121
122 *ret = (rlim_t) u;
123 return 0;
124 }
125
126 static int rlimit_parse_sec(const char *val, rlim_t *ret) {
127 uint64_t u;
128 usec_t t;
129 int r;
130
131 assert(val);
132 assert(ret);
133
134 r = parse_sec(val, &t);
135 if (r < 0)
136 return r;
137 if (t == USEC_INFINITY) {
138 *ret = RLIM_INFINITY;
139 return 0;
140 }
141
142 u = (uint64_t) DIV_ROUND_UP(t, USEC_PER_SEC);
143 if (u >= (uint64_t) RLIM_INFINITY)
144 return -ERANGE;
145
146 *ret = (rlim_t) u;
147 return 0;
148 }
149
150 static int rlimit_parse_usec(const char *val, rlim_t *ret) {
151 usec_t t;
152 int r;
153
154 assert(val);
155 assert(ret);
156
157 r = parse_time(val, &t, 1);
158 if (r < 0)
159 return r;
160 if (t == USEC_INFINITY) {
161 *ret = RLIM_INFINITY;
162 return 0;
163 }
164
165 *ret = (rlim_t) t;
166 return 0;
167 }
168
169 static int rlimit_parse_nice(const char *val, rlim_t *ret) {
170 uint64_t rl;
171 int r;
172
173 assert(val);
174 assert(ret);
175
176 /* So, Linux is weird. The range for RLIMIT_NICE is 40..1, mapping to the nice levels -20..19. However, the
177 * RLIMIT_NICE limit defaults to 0 by the kernel, i.e. a value that maps to nice level 20, which of course is
178 * bogus and does not exist. In order to permit parsing the RLIMIT_NICE of 0 here we hence implement a slight
179 * asymmetry: when parsing as positive nice level we permit 0..19. When parsing as negative nice level, we
180 * permit -20..0. But when parsing as raw resource limit value then we also allow the special value 0.
181 *
182 * Yeah, Linux is quality engineering sometimes... */
183
184 if (val[0] == '+') {
185
186 /* Prefixed with "+": Parse as positive user-friendly nice value */
187 r = safe_atou64(val + 1, &rl);
188 if (r < 0)
189 return r;
190
191 if (rl >= PRIO_MAX)
192 return -ERANGE;
193
194 rl = 20 - rl;
195
196 } else if (val[0] == '-') {
197
198 /* Prefixed with "-": Parse as negative user-friendly nice value */
199 r = safe_atou64(val + 1, &rl);
200 if (r < 0)
201 return r;
202
203 if (rl > (uint64_t) (-PRIO_MIN))
204 return -ERANGE;
205
206 rl = 20 + rl;
207 } else {
208
209 /* Not prefixed: parse as raw resource limit value */
210 r = safe_atou64(val, &rl);
211 if (r < 0)
212 return r;
213
214 if (rl > (uint64_t) (20 - PRIO_MIN))
215 return -ERANGE;
216 }
217
218 *ret = (rlim_t) rl;
219 return 0;
220 }
221
222 static int (*const rlimit_parse_table[_RLIMIT_MAX])(const char *val, rlim_t *ret) = {
223 [RLIMIT_CPU] = rlimit_parse_sec,
224 [RLIMIT_FSIZE] = rlimit_parse_size,
225 [RLIMIT_DATA] = rlimit_parse_size,
226 [RLIMIT_STACK] = rlimit_parse_size,
227 [RLIMIT_CORE] = rlimit_parse_size,
228 [RLIMIT_RSS] = rlimit_parse_size,
229 [RLIMIT_NOFILE] = rlimit_parse_u64,
230 [RLIMIT_AS] = rlimit_parse_size,
231 [RLIMIT_NPROC] = rlimit_parse_u64,
232 [RLIMIT_MEMLOCK] = rlimit_parse_size,
233 [RLIMIT_LOCKS] = rlimit_parse_u64,
234 [RLIMIT_SIGPENDING] = rlimit_parse_u64,
235 [RLIMIT_MSGQUEUE] = rlimit_parse_size,
236 [RLIMIT_NICE] = rlimit_parse_nice,
237 [RLIMIT_RTPRIO] = rlimit_parse_u64,
238 [RLIMIT_RTTIME] = rlimit_parse_usec,
239 };
240
241 int rlimit_parse_one(int resource, const char *val, rlim_t *ret) {
242 assert(val);
243 assert(ret);
244
245 if (resource < 0)
246 return -EINVAL;
247 if (resource >= _RLIMIT_MAX)
248 return -EINVAL;
249
250 return rlimit_parse_table[resource](val, ret);
251 }
252
253 int rlimit_parse(int resource, const char *val, struct rlimit *ret) {
254 _cleanup_free_ char *hard = NULL, *soft = NULL;
255 rlim_t hl, sl;
256 int r;
257
258 assert(val);
259 assert(ret);
260
261 r = extract_first_word(&val, &soft, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
262 if (r < 0)
263 return r;
264 if (r == 0)
265 return -EINVAL;
266
267 r = rlimit_parse_one(resource, soft, &sl);
268 if (r < 0)
269 return r;
270
271 r = extract_first_word(&val, &hard, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
272 if (r < 0)
273 return r;
274 if (!isempty(val))
275 return -EINVAL;
276 if (r == 0)
277 hl = sl;
278 else {
279 r = rlimit_parse_one(resource, hard, &hl);
280 if (r < 0)
281 return r;
282 if (sl > hl)
283 return -EILSEQ;
284 }
285
286 *ret = (struct rlimit) {
287 .rlim_cur = sl,
288 .rlim_max = hl,
289 };
290
291 return 0;
292 }
293
294 int rlimit_format(const struct rlimit *rl, char **ret) {
295 char *s;
296
297 assert(rl);
298 assert(ret);
299
300 if (rl->rlim_cur >= RLIM_INFINITY && rl->rlim_max >= RLIM_INFINITY)
301 s = strdup("infinity");
302 else if (rl->rlim_cur >= RLIM_INFINITY)
303 s = asprintf_safe("infinity:" RLIM_FMT, rl->rlim_max);
304 else if (rl->rlim_max >= RLIM_INFINITY)
305 s = asprintf_safe(RLIM_FMT ":infinity", rl->rlim_cur);
306 else if (rl->rlim_cur == rl->rlim_max)
307 s = asprintf_safe(RLIM_FMT, rl->rlim_cur);
308 else
309 s = asprintf_safe(RLIM_FMT ":" RLIM_FMT, rl->rlim_cur, rl->rlim_max);
310 if (!s)
311 return -ENOMEM;
312
313 *ret = s;
314 return 0;
315 }
316
317 static const char* const rlimit_table[_RLIMIT_MAX] = {
318 [RLIMIT_AS] = "AS",
319 [RLIMIT_CORE] = "CORE",
320 [RLIMIT_CPU] = "CPU",
321 [RLIMIT_DATA] = "DATA",
322 [RLIMIT_FSIZE] = "FSIZE",
323 [RLIMIT_LOCKS] = "LOCKS",
324 [RLIMIT_MEMLOCK] = "MEMLOCK",
325 [RLIMIT_MSGQUEUE] = "MSGQUEUE",
326 [RLIMIT_NICE] = "NICE",
327 [RLIMIT_NOFILE] = "NOFILE",
328 [RLIMIT_NPROC] = "NPROC",
329 [RLIMIT_RSS] = "RSS",
330 [RLIMIT_RTPRIO] = "RTPRIO",
331 [RLIMIT_RTTIME] = "RTTIME",
332 [RLIMIT_SIGPENDING] = "SIGPENDING",
333 [RLIMIT_STACK] = "STACK",
334 };
335
336 DEFINE_STRING_TABLE_LOOKUP(rlimit, int);
337
338 void rlimits_list(const char *prefix) {
339 FOREACH_ELEMENT(field, rlimit_table)
340 printf("%s%s\n", strempty(prefix), *field);
341 }
342
343 int rlimit_from_string_harder(const char *s) {
344 const char *suffix;
345
346 /* The official prefix */
347 suffix = startswith(s, "RLIMIT_");
348 if (suffix)
349 return rlimit_from_string(suffix);
350
351 /* Our own unit file setting prefix */
352 suffix = startswith(s, "Limit");
353 if (suffix)
354 return rlimit_from_string(suffix);
355
356 return rlimit_from_string(s);
357 }
358
359 void rlimit_free_all(struct rlimit **rl) {
360 free_many((void**) rl, _RLIMIT_MAX);
361 }
362
363 int rlimit_copy_all(struct rlimit* target[static _RLIMIT_MAX], struct rlimit* const source[static _RLIMIT_MAX]) {
364 struct rlimit* copy[_RLIMIT_MAX] = {};
365
366 assert(target);
367 assert(source);
368
369 for (int i = 0; i < _RLIMIT_MAX; i++) {
370 if (!source[i])
371 continue;
372
373 copy[i] = newdup(struct rlimit, source[i], 1);
374 if (!copy[i]) {
375 rlimit_free_all(copy);
376 return -ENOMEM;
377 }
378 }
379
380 memcpy(target, copy, sizeof(struct rlimit*) * _RLIMIT_MAX);
381 return 0;
382 }
383
384 int rlimit_nofile_bump(int limit) {
385 int r;
386
387 /* Bumps the (soft) RLIMIT_NOFILE resource limit as close as possible to the specified limit. If a negative
388 * limit is specified, bumps it to the maximum the kernel and the hard resource limit allows. This call should
389 * be used by all our programs that might need a lot of fds, and that know how to deal with high fd numbers
390 * (i.e. do not use select() — which chokes on fds >= 1024) */
391
392 if (limit < 0)
393 limit = read_nr_open();
394
395 if (limit < 3)
396 limit = 3;
397
398 r = setrlimit_closest(RLIMIT_NOFILE, &RLIMIT_MAKE_CONST(limit));
399 if (r < 0)
400 return log_debug_errno(r, "Failed to set RLIMIT_NOFILE: %m");
401
402 return 0;
403 }
404
405 int rlimit_nofile_safe(void) {
406 struct rlimit rl;
407
408 /* Resets RLIMIT_NOFILE's soft limit FD_SETSIZE (i.e. 1024), for compatibility with software still using
409 * select() */
410
411 if (getrlimit(RLIMIT_NOFILE, &rl) < 0)
412 return log_debug_errno(errno, "Failed to query RLIMIT_NOFILE: %m");
413
414 if (rl.rlim_cur <= FD_SETSIZE)
415 return 0;
416
417 /* So we might have inherited a hard limit that's larger than the kernel's maximum limit as stored in
418 * /proc/sys/fs/nr_open. If we pass this hard limit unmodified to setrlimit(), we'll get EPERM. To
419 * make sure that doesn't happen, let's limit our hard limit to the value from nr_open. */
420 rl.rlim_max = MIN(rl.rlim_max, (rlim_t) read_nr_open());
421 rl.rlim_cur = MIN((rlim_t) FD_SETSIZE, rl.rlim_max);
422 if (setrlimit(RLIMIT_NOFILE, &rl) < 0)
423 return log_debug_errno(errno, "Failed to lower RLIMIT_NOFILE's soft limit to " RLIM_FMT ": %m", rl.rlim_cur);
424
425 return 1;
426 }
427
428 int pid_getrlimit(pid_t pid, int resource, struct rlimit *ret) {
429
430 static const char * const prefix_table[_RLIMIT_MAX] = {
431 [RLIMIT_CPU] = "Max cpu time",
432 [RLIMIT_FSIZE] = "Max file size",
433 [RLIMIT_DATA] = "Max data size",
434 [RLIMIT_STACK] = "Max stack size",
435 [RLIMIT_CORE] = "Max core file size",
436 [RLIMIT_RSS] = "Max resident set",
437 [RLIMIT_NPROC] = "Max processes",
438 [RLIMIT_NOFILE] = "Max open files",
439 [RLIMIT_MEMLOCK] = "Max locked memory",
440 [RLIMIT_AS] = "Max address space",
441 [RLIMIT_LOCKS] = "Max file locks",
442 [RLIMIT_SIGPENDING] = "Max pending signals",
443 [RLIMIT_MSGQUEUE] = "Max msgqueue size",
444 [RLIMIT_NICE] = "Max nice priority",
445 [RLIMIT_RTPRIO] = "Max realtime priority",
446 [RLIMIT_RTTIME] = "Max realtime timeout",
447 };
448
449 int r;
450
451 assert(resource >= 0);
452 assert(resource < _RLIMIT_MAX);
453 assert(pid >= 0);
454 assert(ret);
455
456 if (pid == 0 || pid == getpid_cached())
457 return RET_NERRNO(getrlimit(resource, ret));
458
459 r = RET_NERRNO(prlimit(pid, resource, /* new_limit= */ NULL, ret));
460 if (!ERRNO_IS_NEG_PRIVILEGE(r))
461 return r;
462
463 /* We don't have access? Then try to go via /proc/$PID/limits. Weirdly that's world readable in
464 * contrast to querying the data via prlimit() */
465
466 const char *p = procfs_file_alloca(pid, "limits");
467 _cleanup_free_ char *limits = NULL;
468
469 r = read_full_file(p, &limits, /* ret_size= */ NULL);
470 if (r < 0)
471 return -EPERM; /* propagate original permission error if we can't access the limits file */
472
473 _cleanup_strv_free_ char **l = NULL;
474 l = strv_split_newlines(limits);
475 if (!l)
476 return -ENOMEM;
477
478 STRV_FOREACH(i, strv_skip(l, 1)) {
479 _cleanup_free_ char *soft = NULL, *hard = NULL;
480 uint64_t sv, hv;
481 const char *e;
482
483 e = startswith(*i, prefix_table[resource]);
484 if (!e)
485 continue;
486
487 if (*e != ' ')
488 continue;
489
490 e += strspn(e, WHITESPACE);
491
492 size_t n;
493 n = strcspn(e, WHITESPACE);
494 if (n == 0)
495 continue;
496
497 soft = strndup(e, n);
498 if (!soft)
499 return -ENOMEM;
500
501 e += n;
502 if (*e != ' ')
503 continue;
504
505 e += strspn(e, WHITESPACE);
506 n = strcspn(e, WHITESPACE);
507 if (n == 0)
508 continue;
509
510 hard = strndup(e, n);
511 if (!hard)
512 return -ENOMEM;
513
514 if (streq(soft, "unlimited"))
515 sv = RLIM_INFINITY;
516 else {
517 r = safe_atou64(soft, &sv);
518 if (r < 0)
519 return r;
520 }
521
522 if (streq(hard, "unlimited"))
523 hv = RLIM_INFINITY;
524 else {
525 r = safe_atou64(hard, &hv);
526 if (r < 0)
527 return r;
528 }
529
530 *ret = (struct rlimit) {
531 .rlim_cur = sv,
532 .rlim_max = hv,
533 };
534
535 return 0;
536 }
537
538 return -ENOTRECOVERABLE;
539 }