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