1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2010 Lennart Poettering
7 #include <sys/resource.h>
9 #include "alloc-util.h"
10 #include "extract-word.h"
11 #include "format-util.h"
14 #include "rlimit-util.h"
15 #include "string-table.h"
16 #include "time-util.h"
18 int setrlimit_closest(int resource
, const struct rlimit
*rlim
) {
19 struct rlimit highest
, fixed
;
23 if (setrlimit(resource
, rlim
) >= 0)
29 /* So we failed to set the desired setrlimit, then let's try
30 * to get as close as we can */
31 if (getrlimit(resource
, &highest
) < 0)
34 /* If the hard limit is unbounded anyway, then the EPERM had other reasons, let's propagate the original EPERM
36 if (highest
.rlim_max
== RLIM_INFINITY
)
39 fixed
.rlim_cur
= MIN(rlim
->rlim_cur
, highest
.rlim_max
);
40 fixed
.rlim_max
= MIN(rlim
->rlim_max
, highest
.rlim_max
);
42 if (setrlimit(resource
, &fixed
) < 0)
48 int setrlimit_closest_all(const struct rlimit
*const *rlim
, int *which_failed
) {
53 /* On failure returns the limit's index that failed in *which_failed, but only if non-NULL */
55 for (i
= 0; i
< _RLIMIT_MAX
; i
++) {
59 r
= setrlimit_closest(i
, rlim
[i
]);
74 static int rlimit_parse_u64(const char *val
, rlim_t
*ret
) {
81 if (streq(val
, "infinity")) {
86 /* setrlimit(2) suggests rlim_t is always 64bit on Linux. */
87 assert_cc(sizeof(rlim_t
) == sizeof(uint64_t));
89 r
= safe_atou64(val
, &u
);
92 if (u
>= (uint64_t) RLIM_INFINITY
)
99 static int rlimit_parse_size(const char *val
, rlim_t
*ret
) {
106 if (streq(val
, "infinity")) {
107 *ret
= RLIM_INFINITY
;
111 r
= parse_size(val
, 1024, &u
);
114 if (u
>= (uint64_t) RLIM_INFINITY
)
121 static int rlimit_parse_sec(const char *val
, rlim_t
*ret
) {
129 if (streq(val
, "infinity")) {
130 *ret
= RLIM_INFINITY
;
134 r
= parse_sec(val
, &t
);
137 if (t
== USEC_INFINITY
) {
138 *ret
= RLIM_INFINITY
;
142 u
= (uint64_t) DIV_ROUND_UP(t
, USEC_PER_SEC
);
143 if (u
>= (uint64_t) RLIM_INFINITY
)
150 static int rlimit_parse_usec(const char *val
, rlim_t
*ret
) {
157 if (streq(val
, "infinity")) {
158 *ret
= RLIM_INFINITY
;
162 r
= parse_time(val
, &t
, 1);
165 if (t
== USEC_INFINITY
) {
166 *ret
= RLIM_INFINITY
;
174 static int rlimit_parse_nice(const char *val
, rlim_t
*ret
) {
178 /* So, Linux is weird. The range for RLIMIT_NICE is 40..1, mapping to the nice levels -20..19. However, the
179 * RLIMIT_NICE limit defaults to 0 by the kernel, i.e. a value that maps to nice level 20, which of course is
180 * bogus and does not exist. In order to permit parsing the RLIMIT_NICE of 0 here we hence implement a slight
181 * asymmetry: when parsing as positive nice level we permit 0..19. When parsing as negative nice level, we
182 * permit -20..0. But when parsing as raw resource limit value then we also allow the special value 0.
184 * Yeah, Linux is quality engineering sometimes... */
188 /* Prefixed with "+": Parse as positive user-friendly nice value */
189 r
= safe_atou64(val
+ 1, &rl
);
198 } else if (val
[0] == '-') {
200 /* Prefixed with "-": Parse as negative user-friendly nice value */
201 r
= safe_atou64(val
+ 1, &rl
);
205 if (rl
> (uint64_t) (-PRIO_MIN
))
211 /* Not prefixed: parse as raw resource limit value */
212 r
= safe_atou64(val
, &rl
);
216 if (rl
> (uint64_t) (20 - PRIO_MIN
))
224 static int (*const rlimit_parse_table
[_RLIMIT_MAX
])(const char *val
, rlim_t
*ret
) = {
225 [RLIMIT_CPU
] = rlimit_parse_sec
,
226 [RLIMIT_FSIZE
] = rlimit_parse_size
,
227 [RLIMIT_DATA
] = rlimit_parse_size
,
228 [RLIMIT_STACK
] = rlimit_parse_size
,
229 [RLIMIT_CORE
] = rlimit_parse_size
,
230 [RLIMIT_RSS
] = rlimit_parse_size
,
231 [RLIMIT_NOFILE
] = rlimit_parse_u64
,
232 [RLIMIT_AS
] = rlimit_parse_size
,
233 [RLIMIT_NPROC
] = rlimit_parse_u64
,
234 [RLIMIT_MEMLOCK
] = rlimit_parse_size
,
235 [RLIMIT_LOCKS
] = rlimit_parse_u64
,
236 [RLIMIT_SIGPENDING
] = rlimit_parse_u64
,
237 [RLIMIT_MSGQUEUE
] = rlimit_parse_size
,
238 [RLIMIT_NICE
] = rlimit_parse_nice
,
239 [RLIMIT_RTPRIO
] = rlimit_parse_u64
,
240 [RLIMIT_RTTIME
] = rlimit_parse_usec
,
243 int rlimit_parse_one(int resource
, const char *val
, rlim_t
*ret
) {
249 if (resource
>= _RLIMIT_MAX
)
252 return rlimit_parse_table
[resource
](val
, ret
);
255 int rlimit_parse(int resource
, const char *val
, struct rlimit
*ret
) {
256 _cleanup_free_
char *hard
= NULL
, *soft
= NULL
;
263 r
= extract_first_word(&val
, &soft
, ":", EXTRACT_DONT_COALESCE_SEPARATORS
);
269 r
= rlimit_parse_one(resource
, soft
, &sl
);
273 r
= extract_first_word(&val
, &hard
, ":", EXTRACT_DONT_COALESCE_SEPARATORS
);
281 r
= rlimit_parse_one(resource
, hard
, &hl
);
288 *ret
= (struct rlimit
) {
296 int rlimit_format(const struct rlimit
*rl
, char **ret
) {
302 if (rl
->rlim_cur
>= RLIM_INFINITY
&& rl
->rlim_max
>= RLIM_INFINITY
)
303 s
= strdup("infinity");
304 else if (rl
->rlim_cur
>= RLIM_INFINITY
)
305 (void) asprintf(&s
, "infinity:" RLIM_FMT
, rl
->rlim_max
);
306 else if (rl
->rlim_max
>= RLIM_INFINITY
)
307 (void) asprintf(&s
, RLIM_FMT
":infinity", rl
->rlim_cur
);
308 else if (rl
->rlim_cur
== rl
->rlim_max
)
309 (void) asprintf(&s
, RLIM_FMT
, rl
->rlim_cur
);
311 (void) asprintf(&s
, RLIM_FMT
":" RLIM_FMT
, rl
->rlim_cur
, rl
->rlim_max
);
320 static const char* const rlimit_table
[_RLIMIT_MAX
] = {
322 [RLIMIT_CORE
] = "CORE",
323 [RLIMIT_CPU
] = "CPU",
324 [RLIMIT_DATA
] = "DATA",
325 [RLIMIT_FSIZE
] = "FSIZE",
326 [RLIMIT_LOCKS
] = "LOCKS",
327 [RLIMIT_MEMLOCK
] = "MEMLOCK",
328 [RLIMIT_MSGQUEUE
] = "MSGQUEUE",
329 [RLIMIT_NICE
] = "NICE",
330 [RLIMIT_NOFILE
] = "NOFILE",
331 [RLIMIT_NPROC
] = "NPROC",
332 [RLIMIT_RSS
] = "RSS",
333 [RLIMIT_RTPRIO
] = "RTPRIO",
334 [RLIMIT_RTTIME
] = "RTTIME",
335 [RLIMIT_SIGPENDING
] = "SIGPENDING",
336 [RLIMIT_STACK
] = "STACK",
339 DEFINE_STRING_TABLE_LOOKUP(rlimit
, int);
341 int rlimit_from_string_harder(const char *s
) {
344 /* The official prefix */
345 suffix
= startswith(s
, "RLIMIT_");
347 return rlimit_from_string(suffix
);
349 /* Our own unit file setting prefix */
350 suffix
= startswith(s
, "Limit");
352 return rlimit_from_string(suffix
);
354 return rlimit_from_string(s
);
357 void rlimit_free_all(struct rlimit
**rl
) {
363 for (i
= 0; i
< _RLIMIT_MAX
; i
++)
364 rl
[i
] = mfree(rl
[i
]);