1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
9 #include <sys/resource.h>
11 #include "alloc-util.h"
12 #include "extract-word.h"
13 #include "format-util.h"
16 #include "rlimit-util.h"
17 #include "string-table.h"
18 #include "time-util.h"
20 int setrlimit_closest(int resource
, const struct rlimit
*rlim
) {
21 struct rlimit highest
, fixed
;
25 if (setrlimit(resource
, rlim
) >= 0)
31 /* So we failed to set the desired setrlimit, then let's try
32 * to get as close as we can */
33 if (getrlimit(resource
, &highest
) < 0)
36 /* If the hard limit is unbounded anyway, then the EPERM had other reasons, let's propagate the original EPERM
38 if (highest
.rlim_max
== RLIM_INFINITY
)
41 fixed
.rlim_cur
= MIN(rlim
->rlim_cur
, highest
.rlim_max
);
42 fixed
.rlim_max
= MIN(rlim
->rlim_max
, highest
.rlim_max
);
44 if (setrlimit(resource
, &fixed
) < 0)
50 int setrlimit_closest_all(const struct rlimit
*const *rlim
, int *which_failed
) {
55 /* On failure returns the limit's index that failed in *which_failed, but only if non-NULL */
57 for (i
= 0; i
< _RLIMIT_MAX
; i
++) {
61 r
= setrlimit_closest(i
, rlim
[i
]);
76 static int rlimit_parse_u64(const char *val
, rlim_t
*ret
) {
83 if (streq(val
, "infinity")) {
88 /* setrlimit(2) suggests rlim_t is always 64bit on Linux. */
89 assert_cc(sizeof(rlim_t
) == sizeof(uint64_t));
91 r
= safe_atou64(val
, &u
);
94 if (u
>= (uint64_t) RLIM_INFINITY
)
101 static int rlimit_parse_size(const char *val
, rlim_t
*ret
) {
108 if (streq(val
, "infinity")) {
109 *ret
= RLIM_INFINITY
;
113 r
= parse_size(val
, 1024, &u
);
116 if (u
>= (uint64_t) RLIM_INFINITY
)
123 static int rlimit_parse_sec(const char *val
, rlim_t
*ret
) {
131 if (streq(val
, "infinity")) {
132 *ret
= RLIM_INFINITY
;
136 r
= parse_sec(val
, &t
);
139 if (t
== USEC_INFINITY
) {
140 *ret
= RLIM_INFINITY
;
144 u
= (uint64_t) DIV_ROUND_UP(t
, USEC_PER_SEC
);
145 if (u
>= (uint64_t) RLIM_INFINITY
)
152 static int rlimit_parse_usec(const char *val
, rlim_t
*ret
) {
159 if (streq(val
, "infinity")) {
160 *ret
= RLIM_INFINITY
;
164 r
= parse_time(val
, &t
, 1);
167 if (t
== USEC_INFINITY
) {
168 *ret
= RLIM_INFINITY
;
176 static int rlimit_parse_nice(const char *val
, rlim_t
*ret
) {
180 /* So, Linux is weird. The range for RLIMIT_NICE is 40..1, mapping to the nice levels -20..19. However, the
181 * RLIMIT_NICE limit defaults to 0 by the kernel, i.e. a value that maps to nice level 20, which of course is
182 * bogus and does not exist. In order to permit parsing the RLIMIT_NICE of 0 here we hence implement a slight
183 * asymmetry: when parsing as positive nice level we permit 0..19. When parsing as negative nice level, we
184 * permit -20..0. But when parsing as raw resource limit value then we also allow the special value 0.
186 * Yeah, Linux is quality engineering sometimes... */
190 /* Prefixed with "+": Parse as positive user-friendly nice value */
191 r
= safe_atou64(val
+ 1, &rl
);
200 } else if (val
[0] == '-') {
202 /* Prefixed with "-": Parse as negative user-friendly nice value */
203 r
= safe_atou64(val
+ 1, &rl
);
207 if (rl
> (uint64_t) (-PRIO_MIN
))
213 /* Not prefixed: parse as raw resource limit value */
214 r
= safe_atou64(val
, &rl
);
218 if (rl
> (uint64_t) (20 - PRIO_MIN
))
226 static int (*const rlimit_parse_table
[_RLIMIT_MAX
])(const char *val
, rlim_t
*ret
) = {
227 [RLIMIT_CPU
] = rlimit_parse_sec
,
228 [RLIMIT_FSIZE
] = rlimit_parse_size
,
229 [RLIMIT_DATA
] = rlimit_parse_size
,
230 [RLIMIT_STACK
] = rlimit_parse_size
,
231 [RLIMIT_CORE
] = rlimit_parse_size
,
232 [RLIMIT_RSS
] = rlimit_parse_size
,
233 [RLIMIT_NOFILE
] = rlimit_parse_u64
,
234 [RLIMIT_AS
] = rlimit_parse_size
,
235 [RLIMIT_NPROC
] = rlimit_parse_u64
,
236 [RLIMIT_MEMLOCK
] = rlimit_parse_size
,
237 [RLIMIT_LOCKS
] = rlimit_parse_u64
,
238 [RLIMIT_SIGPENDING
] = rlimit_parse_u64
,
239 [RLIMIT_MSGQUEUE
] = rlimit_parse_size
,
240 [RLIMIT_NICE
] = rlimit_parse_nice
,
241 [RLIMIT_RTPRIO
] = rlimit_parse_u64
,
242 [RLIMIT_RTTIME
] = rlimit_parse_usec
,
245 int rlimit_parse_one(int resource
, const char *val
, rlim_t
*ret
) {
251 if (resource
>= _RLIMIT_MAX
)
254 return rlimit_parse_table
[resource
](val
, ret
);
257 int rlimit_parse(int resource
, const char *val
, struct rlimit
*ret
) {
258 _cleanup_free_
char *hard
= NULL
, *soft
= NULL
;
265 r
= extract_first_word(&val
, &soft
, ":", EXTRACT_DONT_COALESCE_SEPARATORS
);
271 r
= rlimit_parse_one(resource
, soft
, &sl
);
275 r
= extract_first_word(&val
, &hard
, ":", EXTRACT_DONT_COALESCE_SEPARATORS
);
283 r
= rlimit_parse_one(resource
, hard
, &hl
);
290 *ret
= (struct rlimit
) {
298 int rlimit_format(const struct rlimit
*rl
, char **ret
) {
304 if (rl
->rlim_cur
>= RLIM_INFINITY
&& rl
->rlim_max
>= RLIM_INFINITY
)
305 s
= strdup("infinity");
306 else if (rl
->rlim_cur
>= RLIM_INFINITY
)
307 (void) asprintf(&s
, "infinity:" RLIM_FMT
, rl
->rlim_max
);
308 else if (rl
->rlim_max
>= RLIM_INFINITY
)
309 (void) asprintf(&s
, RLIM_FMT
":infinity", rl
->rlim_cur
);
310 else if (rl
->rlim_cur
== rl
->rlim_max
)
311 (void) asprintf(&s
, RLIM_FMT
, rl
->rlim_cur
);
313 (void) asprintf(&s
, RLIM_FMT
":" RLIM_FMT
, rl
->rlim_cur
, rl
->rlim_max
);
322 static const char* const rlimit_table
[_RLIMIT_MAX
] = {
324 [RLIMIT_CORE
] = "CORE",
325 [RLIMIT_CPU
] = "CPU",
326 [RLIMIT_DATA
] = "DATA",
327 [RLIMIT_FSIZE
] = "FSIZE",
328 [RLIMIT_LOCKS
] = "LOCKS",
329 [RLIMIT_MEMLOCK
] = "MEMLOCK",
330 [RLIMIT_MSGQUEUE
] = "MSGQUEUE",
331 [RLIMIT_NICE
] = "NICE",
332 [RLIMIT_NOFILE
] = "NOFILE",
333 [RLIMIT_NPROC
] = "NPROC",
334 [RLIMIT_RSS
] = "RSS",
335 [RLIMIT_RTPRIO
] = "RTPRIO",
336 [RLIMIT_RTTIME
] = "RTTIME",
337 [RLIMIT_SIGPENDING
] = "SIGPENDING",
338 [RLIMIT_STACK
] = "STACK",
341 DEFINE_STRING_TABLE_LOOKUP(rlimit
, int);
343 int rlimit_from_string_harder(const char *s
) {
346 /* The official prefix */
347 suffix
= startswith(s
, "RLIMIT_");
349 return rlimit_from_string(suffix
);
351 /* Our own unit file setting prefix */
352 suffix
= startswith(s
, "Limit");
354 return rlimit_from_string(suffix
);
356 return rlimit_from_string(s
);
359 void rlimit_free_all(struct rlimit
**rl
) {
365 for (i
= 0; i
< _RLIMIT_MAX
; i
++)
366 rl
[i
] = mfree(rl
[i
]);