2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
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.
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.
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/>.
21 #include <sys/resource.h>
23 #include "alloc-util.h"
24 #include "extract-word.h"
25 #include "formats-util.h"
28 #include "rlimit-util.h"
29 #include "string-table.h"
30 #include "time-util.h"
32 int setrlimit_closest(int resource
, const struct rlimit
*rlim
) {
33 struct rlimit highest
, fixed
;
37 if (setrlimit(resource
, rlim
) >= 0)
43 /* So we failed to set the desired setrlimit, then let's try
44 * to get as close as we can */
45 assert_se(getrlimit(resource
, &highest
) == 0);
47 fixed
.rlim_cur
= MIN(rlim
->rlim_cur
, highest
.rlim_max
);
48 fixed
.rlim_max
= MIN(rlim
->rlim_max
, highest
.rlim_max
);
50 if (setrlimit(resource
, &fixed
) < 0)
56 static int rlimit_parse_u64(const char *val
, rlim_t
*ret
) {
63 if (streq(val
, "infinity")) {
68 /* setrlimit(2) suggests rlim_t is always 64bit on Linux. */
69 assert_cc(sizeof(rlim_t
) == sizeof(uint64_t));
71 r
= safe_atou64(val
, &u
);
74 if (u
>= (uint64_t) RLIM_INFINITY
)
81 static int rlimit_parse_size(const char *val
, rlim_t
*ret
) {
88 if (streq(val
, "infinity")) {
93 r
= parse_size(val
, 1024, &u
);
96 if (u
>= (uint64_t) RLIM_INFINITY
)
103 static int rlimit_parse_sec(const char *val
, rlim_t
*ret
) {
111 if (streq(val
, "infinity")) {
112 *ret
= RLIM_INFINITY
;
116 r
= parse_sec(val
, &t
);
119 if (t
== USEC_INFINITY
) {
120 *ret
= RLIM_INFINITY
;
124 u
= (uint64_t) DIV_ROUND_UP(t
, USEC_PER_SEC
);
125 if (u
>= (uint64_t) RLIM_INFINITY
)
132 static int rlimit_parse_usec(const char *val
, rlim_t
*ret
) {
139 if (streq(val
, "infinity")) {
140 *ret
= RLIM_INFINITY
;
144 r
= parse_time(val
, &t
, 1);
147 if (t
== USEC_INFINITY
) {
148 *ret
= RLIM_INFINITY
;
156 static int (*const rlimit_parse_table
[_RLIMIT_MAX
])(const char *val
, rlim_t
*ret
) = {
157 [RLIMIT_CPU
] = rlimit_parse_sec
,
158 [RLIMIT_FSIZE
] = rlimit_parse_size
,
159 [RLIMIT_DATA
] = rlimit_parse_size
,
160 [RLIMIT_STACK
] = rlimit_parse_size
,
161 [RLIMIT_CORE
] = rlimit_parse_size
,
162 [RLIMIT_RSS
] = rlimit_parse_size
,
163 [RLIMIT_NOFILE
] = rlimit_parse_u64
,
164 [RLIMIT_AS
] = rlimit_parse_size
,
165 [RLIMIT_NPROC
] = rlimit_parse_u64
,
166 [RLIMIT_MEMLOCK
] = rlimit_parse_size
,
167 [RLIMIT_LOCKS
] = rlimit_parse_u64
,
168 [RLIMIT_SIGPENDING
] = rlimit_parse_u64
,
169 [RLIMIT_MSGQUEUE
] = rlimit_parse_size
,
170 [RLIMIT_NICE
] = rlimit_parse_u64
,
171 [RLIMIT_RTPRIO
] = rlimit_parse_u64
,
172 [RLIMIT_RTTIME
] = rlimit_parse_usec
,
175 int rlimit_parse_one(int resource
, const char *val
, rlim_t
*ret
) {
181 if (resource
>= _RLIMIT_MAX
)
184 return rlimit_parse_table
[resource
](val
, ret
);
187 int rlimit_parse(int resource
, const char *val
, struct rlimit
*ret
) {
188 _cleanup_free_
char *hard
= NULL
, *soft
= NULL
;
195 r
= extract_first_word(&val
, &soft
, ":", EXTRACT_DONT_COALESCE_SEPARATORS
);
201 r
= rlimit_parse_one(resource
, soft
, &sl
);
205 r
= extract_first_word(&val
, &hard
, ":", EXTRACT_DONT_COALESCE_SEPARATORS
);
213 r
= rlimit_parse_one(resource
, hard
, &hl
);
220 *ret
= (struct rlimit
) {
228 int rlimit_format(const struct rlimit
*rl
, char **ret
) {
234 if (rl
->rlim_cur
>= RLIM_INFINITY
&& rl
->rlim_max
>= RLIM_INFINITY
)
235 s
= strdup("infinity");
236 else if (rl
->rlim_cur
>= RLIM_INFINITY
)
237 (void) asprintf(&s
, "infinity:" RLIM_FMT
, rl
->rlim_max
);
238 else if (rl
->rlim_max
>= RLIM_INFINITY
)
239 (void) asprintf(&s
, RLIM_FMT
":infinity", rl
->rlim_cur
);
240 else if (rl
->rlim_cur
== rl
->rlim_max
)
241 (void) asprintf(&s
, RLIM_FMT
, rl
->rlim_cur
);
243 (void) asprintf(&s
, RLIM_FMT
":" RLIM_FMT
, rl
->rlim_cur
, rl
->rlim_max
);
252 static const char* const rlimit_table
[_RLIMIT_MAX
] = {
253 [RLIMIT_CPU
] = "LimitCPU",
254 [RLIMIT_FSIZE
] = "LimitFSIZE",
255 [RLIMIT_DATA
] = "LimitDATA",
256 [RLIMIT_STACK
] = "LimitSTACK",
257 [RLIMIT_CORE
] = "LimitCORE",
258 [RLIMIT_RSS
] = "LimitRSS",
259 [RLIMIT_NOFILE
] = "LimitNOFILE",
260 [RLIMIT_AS
] = "LimitAS",
261 [RLIMIT_NPROC
] = "LimitNPROC",
262 [RLIMIT_MEMLOCK
] = "LimitMEMLOCK",
263 [RLIMIT_LOCKS
] = "LimitLOCKS",
264 [RLIMIT_SIGPENDING
] = "LimitSIGPENDING",
265 [RLIMIT_MSGQUEUE
] = "LimitMSGQUEUE",
266 [RLIMIT_NICE
] = "LimitNICE",
267 [RLIMIT_RTPRIO
] = "LimitRTPRIO",
268 [RLIMIT_RTTIME
] = "LimitRTTIME"
271 DEFINE_STRING_TABLE_LOOKUP(rlimit
, int);