1 /* SPDX-License-Identifier: LGPL-2.1+ */
4 #include <sys/resource.h>
6 #include "alloc-util.h"
7 #include "extract-word.h"
8 #include "format-util.h"
11 #include "rlimit-util.h"
12 #include "string-table.h"
13 #include "time-util.h"
15 int setrlimit_closest(int resource
, const struct rlimit
*rlim
) {
16 struct rlimit highest
, fixed
;
20 if (setrlimit(resource
, rlim
) >= 0)
26 /* So we failed to set the desired setrlimit, then let's try
27 * to get as close as we can */
28 if (getrlimit(resource
, &highest
) < 0)
31 /* If the hard limit is unbounded anyway, then the EPERM had other reasons, let's propagate the original EPERM
33 if (highest
.rlim_max
== RLIM_INFINITY
)
36 fixed
.rlim_cur
= MIN(rlim
->rlim_cur
, highest
.rlim_max
);
37 fixed
.rlim_max
= MIN(rlim
->rlim_max
, highest
.rlim_max
);
39 if (setrlimit(resource
, &fixed
) < 0)
45 int setrlimit_closest_all(const struct rlimit
*const *rlim
, int *which_failed
) {
50 /* On failure returns the limit's index that failed in *which_failed, but only if non-NULL */
52 for (i
= 0; i
< _RLIMIT_MAX
; i
++) {
56 r
= setrlimit_closest(i
, rlim
[i
]);
71 static int rlimit_parse_u64(const char *val
, rlim_t
*ret
) {
78 if (streq(val
, "infinity")) {
83 /* setrlimit(2) suggests rlim_t is always 64bit on Linux. */
84 assert_cc(sizeof(rlim_t
) == sizeof(uint64_t));
86 r
= safe_atou64(val
, &u
);
89 if (u
>= (uint64_t) RLIM_INFINITY
)
96 static int rlimit_parse_size(const char *val
, rlim_t
*ret
) {
103 if (streq(val
, "infinity")) {
104 *ret
= RLIM_INFINITY
;
108 r
= parse_size(val
, 1024, &u
);
111 if (u
>= (uint64_t) RLIM_INFINITY
)
118 static int rlimit_parse_sec(const char *val
, rlim_t
*ret
) {
126 if (streq(val
, "infinity")) {
127 *ret
= RLIM_INFINITY
;
131 r
= parse_sec(val
, &t
);
134 if (t
== USEC_INFINITY
) {
135 *ret
= RLIM_INFINITY
;
139 u
= (uint64_t) DIV_ROUND_UP(t
, USEC_PER_SEC
);
140 if (u
>= (uint64_t) RLIM_INFINITY
)
147 static int rlimit_parse_usec(const char *val
, rlim_t
*ret
) {
154 if (streq(val
, "infinity")) {
155 *ret
= RLIM_INFINITY
;
159 r
= parse_time(val
, &t
, 1);
162 if (t
== USEC_INFINITY
) {
163 *ret
= RLIM_INFINITY
;
171 static int rlimit_parse_nice(const char *val
, rlim_t
*ret
) {
175 /* So, Linux is weird. The range for RLIMIT_NICE is 40..1, mapping to the nice levels -20..19. However, the
176 * RLIMIT_NICE limit defaults to 0 by the kernel, i.e. a value that maps to nice level 20, which of course is
177 * bogus and does not exist. In order to permit parsing the RLIMIT_NICE of 0 here we hence implement a slight
178 * asymmetry: when parsing as positive nice level we permit 0..19. When parsing as negative nice level, we
179 * permit -20..0. But when parsing as raw resource limit value then we also allow the special value 0.
181 * Yeah, Linux is quality engineering sometimes... */
185 /* Prefixed with "+": Parse as positive user-friendly nice value */
186 r
= safe_atou64(val
+ 1, &rl
);
195 } else if (val
[0] == '-') {
197 /* Prefixed with "-": Parse as negative user-friendly nice value */
198 r
= safe_atou64(val
+ 1, &rl
);
202 if (rl
> (uint64_t) (-PRIO_MIN
))
208 /* Not prefixed: parse as raw resource limit value */
209 r
= safe_atou64(val
, &rl
);
213 if (rl
> (uint64_t) (20 - PRIO_MIN
))
221 static int (*const rlimit_parse_table
[_RLIMIT_MAX
])(const char *val
, rlim_t
*ret
) = {
222 [RLIMIT_CPU
] = rlimit_parse_sec
,
223 [RLIMIT_FSIZE
] = rlimit_parse_size
,
224 [RLIMIT_DATA
] = rlimit_parse_size
,
225 [RLIMIT_STACK
] = rlimit_parse_size
,
226 [RLIMIT_CORE
] = rlimit_parse_size
,
227 [RLIMIT_RSS
] = rlimit_parse_size
,
228 [RLIMIT_NOFILE
] = rlimit_parse_u64
,
229 [RLIMIT_AS
] = rlimit_parse_size
,
230 [RLIMIT_NPROC
] = rlimit_parse_u64
,
231 [RLIMIT_MEMLOCK
] = rlimit_parse_size
,
232 [RLIMIT_LOCKS
] = rlimit_parse_u64
,
233 [RLIMIT_SIGPENDING
] = rlimit_parse_u64
,
234 [RLIMIT_MSGQUEUE
] = rlimit_parse_size
,
235 [RLIMIT_NICE
] = rlimit_parse_nice
,
236 [RLIMIT_RTPRIO
] = rlimit_parse_u64
,
237 [RLIMIT_RTTIME
] = rlimit_parse_usec
,
240 int rlimit_parse_one(int resource
, const char *val
, rlim_t
*ret
) {
246 if (resource
>= _RLIMIT_MAX
)
249 return rlimit_parse_table
[resource
](val
, ret
);
252 int rlimit_parse(int resource
, const char *val
, struct rlimit
*ret
) {
253 _cleanup_free_
char *hard
= NULL
, *soft
= NULL
;
260 r
= extract_first_word(&val
, &soft
, ":", EXTRACT_DONT_COALESCE_SEPARATORS
);
266 r
= rlimit_parse_one(resource
, soft
, &sl
);
270 r
= extract_first_word(&val
, &hard
, ":", EXTRACT_DONT_COALESCE_SEPARATORS
);
278 r
= rlimit_parse_one(resource
, hard
, &hl
);
285 *ret
= (struct rlimit
) {
293 int rlimit_format(const struct rlimit
*rl
, char **ret
) {
299 if (rl
->rlim_cur
>= RLIM_INFINITY
&& rl
->rlim_max
>= RLIM_INFINITY
)
300 s
= strdup("infinity");
301 else if (rl
->rlim_cur
>= RLIM_INFINITY
)
302 (void) asprintf(&s
, "infinity:" RLIM_FMT
, rl
->rlim_max
);
303 else if (rl
->rlim_max
>= RLIM_INFINITY
)
304 (void) asprintf(&s
, RLIM_FMT
":infinity", rl
->rlim_cur
);
305 else if (rl
->rlim_cur
== rl
->rlim_max
)
306 (void) asprintf(&s
, RLIM_FMT
, rl
->rlim_cur
);
308 (void) asprintf(&s
, RLIM_FMT
":" RLIM_FMT
, rl
->rlim_cur
, rl
->rlim_max
);
317 static const char* const rlimit_table
[_RLIMIT_MAX
] = {
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",
336 DEFINE_STRING_TABLE_LOOKUP(rlimit
, int);
338 int rlimit_from_string_harder(const char *s
) {
341 /* The official prefix */
342 suffix
= startswith(s
, "RLIMIT_");
344 return rlimit_from_string(suffix
);
346 /* Our own unit file setting prefix */
347 suffix
= startswith(s
, "Limit");
349 return rlimit_from_string(suffix
);
351 return rlimit_from_string(s
);
354 void rlimit_free_all(struct rlimit
**rl
) {
360 for (i
= 0; i
< _RLIMIT_MAX
; i
++)
361 rl
[i
] = mfree(rl
[i
]);