]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/rlimit-util.c
Merge pull request #653 from dvdhrm/bus-gold
[thirdparty/systemd.git] / src / basic / rlimit-util.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2010 Lennart Poettering
5
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.
10
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.
15
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/>.
18 ***/
19
20 #include <errno.h>
21 #include <sys/resource.h>
22
23 #include "alloc-util.h"
24 #include "extract-word.h"
25 #include "formats-util.h"
26 #include "macro.h"
27 #include "missing.h"
28 #include "rlimit-util.h"
29 #include "string-table.h"
30 #include "time-util.h"
31
32 int setrlimit_closest(int resource, const struct rlimit *rlim) {
33 struct rlimit highest, fixed;
34
35 assert(rlim);
36
37 if (setrlimit(resource, rlim) >= 0)
38 return 0;
39
40 if (errno != EPERM)
41 return -errno;
42
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);
46
47 fixed.rlim_cur = MIN(rlim->rlim_cur, highest.rlim_max);
48 fixed.rlim_max = MIN(rlim->rlim_max, highest.rlim_max);
49
50 if (setrlimit(resource, &fixed) < 0)
51 return -errno;
52
53 return 0;
54 }
55
56 static int rlimit_parse_u64(const char *val, rlim_t *ret) {
57 uint64_t u;
58 int r;
59
60 assert(val);
61 assert(ret);
62
63 if (streq(val, "infinity")) {
64 *ret = RLIM_INFINITY;
65 return 0;
66 }
67
68 /* setrlimit(2) suggests rlim_t is always 64bit on Linux. */
69 assert_cc(sizeof(rlim_t) == sizeof(uint64_t));
70
71 r = safe_atou64(val, &u);
72 if (r < 0)
73 return r;
74 if (u >= (uint64_t) RLIM_INFINITY)
75 return -ERANGE;
76
77 *ret = (rlim_t) u;
78 return 0;
79 }
80
81 static int rlimit_parse_size(const char *val, rlim_t *ret) {
82 uint64_t u;
83 int r;
84
85 assert(val);
86 assert(ret);
87
88 if (streq(val, "infinity")) {
89 *ret = RLIM_INFINITY;
90 return 0;
91 }
92
93 r = parse_size(val, 1024, &u);
94 if (r < 0)
95 return r;
96 if (u >= (uint64_t) RLIM_INFINITY)
97 return -ERANGE;
98
99 *ret = (rlim_t) u;
100 return 0;
101 }
102
103 static int rlimit_parse_sec(const char *val, rlim_t *ret) {
104 uint64_t u;
105 usec_t t;
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_sec(val, &t);
117 if (r < 0)
118 return r;
119 if (t == USEC_INFINITY) {
120 *ret = RLIM_INFINITY;
121 return 0;
122 }
123
124 u = (uint64_t) DIV_ROUND_UP(t, USEC_PER_SEC);
125 if (u >= (uint64_t) RLIM_INFINITY)
126 return -ERANGE;
127
128 *ret = (rlim_t) u;
129 return 0;
130 }
131
132 static int rlimit_parse_usec(const char *val, rlim_t *ret) {
133 usec_t t;
134 int r;
135
136 assert(val);
137 assert(ret);
138
139 if (streq(val, "infinity")) {
140 *ret = RLIM_INFINITY;
141 return 0;
142 }
143
144 r = parse_time(val, &t, 1);
145 if (r < 0)
146 return r;
147 if (t == USEC_INFINITY) {
148 *ret = RLIM_INFINITY;
149 return 0;
150 }
151
152 *ret = (rlim_t) t;
153 return 0;
154 }
155
156 static int rlimit_parse_nice(const char *val, rlim_t *ret) {
157 uint64_t rl;
158 int r;
159
160 /* So, Linux is weird. The range for RLIMIT_NICE is 40..1, mapping to the nice levels -20..19. However, the
161 * RLIMIT_NICE limit defaults to 0 by the kernel, i.e. a value that maps to nice level 20, which of course is
162 * bogus and does not exist. In order to permit parsing the RLIMIT_NICE of 0 here we hence implement a slight
163 * asymmetry: when parsing as positive nice level we permit 0..19. When parsing as negative nice level, we
164 * permit -20..0. But when parsing as raw resource limit value then we also allow the special value 0.
165 *
166 * Yeah, Linux is quality engineering sometimes... */
167
168 if (val[0] == '+') {
169
170 /* Prefixed with "+": Parse as positive user-friendly nice value */
171 r = safe_atou64(val + 1, &rl);
172 if (r < 0)
173 return r;
174
175 if (rl >= PRIO_MAX)
176 return -ERANGE;
177
178 rl = 20 - rl;
179
180 } else if (val[0] == '-') {
181
182 /* Prefixed with "-": Parse as negative user-friendly nice value */
183 r = safe_atou64(val + 1, &rl);
184 if (r < 0)
185 return r;
186
187 if (rl > (uint64_t) (-PRIO_MIN))
188 return -ERANGE;
189
190 rl = 20 + rl;
191 } else {
192
193 /* Not prefixed: parse as raw resource limit value */
194 r = safe_atou64(val, &rl);
195 if (r < 0)
196 return r;
197
198 if (rl > (uint64_t) (20 - PRIO_MIN))
199 return -ERANGE;
200 }
201
202 *ret = (rlim_t) rl;
203 return 0;
204 }
205
206 static int (*const rlimit_parse_table[_RLIMIT_MAX])(const char *val, rlim_t *ret) = {
207 [RLIMIT_CPU] = rlimit_parse_sec,
208 [RLIMIT_FSIZE] = rlimit_parse_size,
209 [RLIMIT_DATA] = rlimit_parse_size,
210 [RLIMIT_STACK] = rlimit_parse_size,
211 [RLIMIT_CORE] = rlimit_parse_size,
212 [RLIMIT_RSS] = rlimit_parse_size,
213 [RLIMIT_NOFILE] = rlimit_parse_u64,
214 [RLIMIT_AS] = rlimit_parse_size,
215 [RLIMIT_NPROC] = rlimit_parse_u64,
216 [RLIMIT_MEMLOCK] = rlimit_parse_size,
217 [RLIMIT_LOCKS] = rlimit_parse_u64,
218 [RLIMIT_SIGPENDING] = rlimit_parse_u64,
219 [RLIMIT_MSGQUEUE] = rlimit_parse_size,
220 [RLIMIT_NICE] = rlimit_parse_nice,
221 [RLIMIT_RTPRIO] = rlimit_parse_u64,
222 [RLIMIT_RTTIME] = rlimit_parse_usec,
223 };
224
225 int rlimit_parse_one(int resource, const char *val, rlim_t *ret) {
226 assert(val);
227 assert(ret);
228
229 if (resource < 0)
230 return -EINVAL;
231 if (resource >= _RLIMIT_MAX)
232 return -EINVAL;
233
234 return rlimit_parse_table[resource](val, ret);
235 }
236
237 int rlimit_parse(int resource, const char *val, struct rlimit *ret) {
238 _cleanup_free_ char *hard = NULL, *soft = NULL;
239 rlim_t hl, sl;
240 int r;
241
242 assert(val);
243 assert(ret);
244
245 r = extract_first_word(&val, &soft, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
246 if (r < 0)
247 return r;
248 if (r == 0)
249 return -EINVAL;
250
251 r = rlimit_parse_one(resource, soft, &sl);
252 if (r < 0)
253 return r;
254
255 r = extract_first_word(&val, &hard, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
256 if (r < 0)
257 return r;
258 if (!isempty(val))
259 return -EINVAL;
260 if (r == 0)
261 hl = sl;
262 else {
263 r = rlimit_parse_one(resource, hard, &hl);
264 if (r < 0)
265 return r;
266 if (sl > hl)
267 return -EILSEQ;
268 }
269
270 *ret = (struct rlimit) {
271 .rlim_cur = sl,
272 .rlim_max = hl,
273 };
274
275 return 0;
276 }
277
278 int rlimit_format(const struct rlimit *rl, char **ret) {
279 char *s = NULL;
280
281 assert(rl);
282 assert(ret);
283
284 if (rl->rlim_cur >= RLIM_INFINITY && rl->rlim_max >= RLIM_INFINITY)
285 s = strdup("infinity");
286 else if (rl->rlim_cur >= RLIM_INFINITY)
287 (void) asprintf(&s, "infinity:" RLIM_FMT, rl->rlim_max);
288 else if (rl->rlim_max >= RLIM_INFINITY)
289 (void) asprintf(&s, RLIM_FMT ":infinity", rl->rlim_cur);
290 else if (rl->rlim_cur == rl->rlim_max)
291 (void) asprintf(&s, RLIM_FMT, rl->rlim_cur);
292 else
293 (void) asprintf(&s, RLIM_FMT ":" RLIM_FMT, rl->rlim_cur, rl->rlim_max);
294
295 if (!s)
296 return -ENOMEM;
297
298 *ret = s;
299 return 0;
300 }
301
302 static const char* const rlimit_table[_RLIMIT_MAX] = {
303 [RLIMIT_CPU] = "LimitCPU",
304 [RLIMIT_FSIZE] = "LimitFSIZE",
305 [RLIMIT_DATA] = "LimitDATA",
306 [RLIMIT_STACK] = "LimitSTACK",
307 [RLIMIT_CORE] = "LimitCORE",
308 [RLIMIT_RSS] = "LimitRSS",
309 [RLIMIT_NOFILE] = "LimitNOFILE",
310 [RLIMIT_AS] = "LimitAS",
311 [RLIMIT_NPROC] = "LimitNPROC",
312 [RLIMIT_MEMLOCK] = "LimitMEMLOCK",
313 [RLIMIT_LOCKS] = "LimitLOCKS",
314 [RLIMIT_SIGPENDING] = "LimitSIGPENDING",
315 [RLIMIT_MSGQUEUE] = "LimitMSGQUEUE",
316 [RLIMIT_NICE] = "LimitNICE",
317 [RLIMIT_RTPRIO] = "LimitRTPRIO",
318 [RLIMIT_RTTIME] = "LimitRTTIME"
319 };
320
321 DEFINE_STRING_TABLE_LOOKUP(rlimit, int);