]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
a7334b09 LP |
2 | /*** |
3 | This file is part of systemd. | |
4 | ||
5 | Copyright 2010 Lennart Poettering | |
bb112710 | 6 | Copyright 2012 Holger Hans Peter Freyther |
a7334b09 LP |
7 | ***/ |
8 | ||
3efd4195 | 9 | #include <errno.h> |
87f0e418 | 10 | #include <fcntl.h> |
25e870b5 | 11 | #include <linux/fs.h> |
5f5d8eab | 12 | #include <linux/oom.h> |
349cc4a5 | 13 | #if HAVE_SECCOMP |
618234a5 LP |
14 | #include <seccomp.h> |
15 | #endif | |
5f5d8eab LP |
16 | #include <sched.h> |
17 | #include <string.h> | |
3d57c6ab | 18 | #include <sys/resource.h> |
5f5d8eab | 19 | #include <sys/stat.h> |
3efd4195 | 20 | |
5f5d8eab | 21 | #include "af-list.h" |
cf0fbc49 | 22 | #include "alloc-util.h" |
57b7a260 | 23 | #include "all-units.h" |
5f5d8eab LP |
24 | #include "bus-error.h" |
25 | #include "bus-internal.h" | |
26 | #include "bus-util.h" | |
27 | #include "cap-list.h" | |
a103496c | 28 | #include "capability-util.h" |
5f5d8eab | 29 | #include "cgroup.h" |
3efd4195 | 30 | #include "conf-parser.h" |
618234a5 | 31 | #include "cpu-set-util.h" |
5f5d8eab LP |
32 | #include "env-util.h" |
33 | #include "errno-list.h" | |
4f5dd394 | 34 | #include "escape.h" |
3ffd4af2 | 35 | #include "fd-util.h" |
f4f15635 | 36 | #include "fs-util.h" |
08f3be7a | 37 | #include "hexdecoct.h" |
d3070fbd | 38 | #include "io-util.h" |
9eba9da4 | 39 | #include "ioprio.h" |
d3070fbd | 40 | #include "journal-util.h" |
3ffd4af2 | 41 | #include "load-fragment.h" |
5f5d8eab | 42 | #include "log.h" |
94f04347 | 43 | #include "missing.h" |
83555251 | 44 | #include "mount-util.h" |
6bedfcbb | 45 | #include "parse-util.h" |
9eb977db | 46 | #include "path-util.h" |
7b3e062c | 47 | #include "process-util.h" |
349cc4a5 | 48 | #if HAVE_SECCOMP |
57183d11 LP |
49 | #include "seccomp-util.h" |
50 | #endif | |
5f5d8eab | 51 | #include "securebits.h" |
07d46372 | 52 | #include "securebits-util.h" |
5f5d8eab | 53 | #include "signal-util.h" |
e045e325 | 54 | #include "socket-protocol-list.h" |
8fcde012 | 55 | #include "stat-util.h" |
07630cea | 56 | #include "string-util.h" |
5f5d8eab LP |
57 | #include "strv.h" |
58 | #include "unit-name.h" | |
59 | #include "unit-printf.h" | |
66dccd8d | 60 | #include "user-util.h" |
5f5d8eab | 61 | #include "utf8.h" |
49cf4170 | 62 | #include "web-util.h" |
57183d11 | 63 | |
5afe510c LP |
64 | DEFINE_CONFIG_PARSE_ENUM(config_parse_collect_mode, collect_mode, CollectMode, "Failed to parse garbage collection mode"); |
65 | ||
f32b43bd LP |
66 | int config_parse_unit_deps( |
67 | const char *unit, | |
68 | const char *filename, | |
69 | unsigned line, | |
70 | const char *section, | |
71 | unsigned section_line, | |
72 | const char *lvalue, | |
73 | int ltype, | |
74 | const char *rvalue, | |
75 | void *data, | |
76 | void *userdata) { | |
3efd4195 | 77 | |
f975e971 | 78 | UnitDependency d = ltype; |
87f0e418 | 79 | Unit *u = userdata; |
3d793d29 | 80 | const char *p; |
3efd4195 LP |
81 | |
82 | assert(filename); | |
83 | assert(lvalue); | |
84 | assert(rvalue); | |
3efd4195 | 85 | |
3d793d29 | 86 | p = rvalue; |
9ed794a3 | 87 | for (;;) { |
3d793d29 | 88 | _cleanup_free_ char *word = NULL, *k = NULL; |
3efd4195 | 89 | int r; |
3efd4195 | 90 | |
c89f52ac | 91 | r = extract_first_word(&p, &word, NULL, EXTRACT_RETAIN_ESCAPE); |
3d793d29 SS |
92 | if (r == 0) |
93 | break; | |
94 | if (r == -ENOMEM) | |
74051b9b | 95 | return log_oom(); |
3d793d29 SS |
96 | if (r < 0) { |
97 | log_syntax(unit, LOG_ERR, filename, line, r, "Invalid syntax, ignoring: %s", rvalue); | |
98 | break; | |
99 | } | |
3efd4195 | 100 | |
3d793d29 | 101 | r = unit_name_printf(u, word, &k); |
19f6d710 | 102 | if (r < 0) { |
063c4b1a | 103 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", word); |
19f6d710 LP |
104 | continue; |
105 | } | |
9e2f7c11 | 106 | |
eef85c4a | 107 | r = unit_add_dependency_by_name(u, d, k, NULL, true, UNIT_DEPENDENCY_FILE); |
57020a3a | 108 | if (r < 0) |
12ca818f | 109 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to add dependency on %s, ignoring: %m", k); |
3efd4195 LP |
110 | } |
111 | ||
112 | return 0; | |
113 | } | |
114 | ||
f32b43bd LP |
115 | int config_parse_obsolete_unit_deps( |
116 | const char *unit, | |
117 | const char *filename, | |
118 | unsigned line, | |
119 | const char *section, | |
120 | unsigned section_line, | |
121 | const char *lvalue, | |
122 | int ltype, | |
123 | const char *rvalue, | |
124 | void *data, | |
125 | void *userdata) { | |
126 | ||
127 | log_syntax(unit, LOG_WARNING, filename, line, 0, | |
128 | "Unit dependency type %s= is obsolete, replacing by %s=, please update your unit file", lvalue, unit_dependency_to_string(ltype)); | |
129 | ||
130 | return config_parse_unit_deps(unit, filename, line, section, section_line, lvalue, ltype, rvalue, data, userdata); | |
131 | } | |
132 | ||
b02cb41c LP |
133 | int config_parse_unit_string_printf( |
134 | const char *unit, | |
135 | const char *filename, | |
136 | unsigned line, | |
137 | const char *section, | |
138 | unsigned section_line, | |
139 | const char *lvalue, | |
140 | int ltype, | |
141 | const char *rvalue, | |
142 | void *data, | |
143 | void *userdata) { | |
932921b5 | 144 | |
74051b9b | 145 | _cleanup_free_ char *k = NULL; |
b02cb41c | 146 | Unit *u = userdata; |
19f6d710 | 147 | int r; |
932921b5 LP |
148 | |
149 | assert(filename); | |
150 | assert(lvalue); | |
151 | assert(rvalue); | |
f2d3769a | 152 | assert(u); |
932921b5 | 153 | |
19f6d710 | 154 | r = unit_full_printf(u, rvalue, &k); |
b02cb41c | 155 | if (r < 0) { |
063c4b1a | 156 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue); |
b02cb41c LP |
157 | return 0; |
158 | } | |
932921b5 | 159 | |
b02cb41c | 160 | return config_parse_string(unit, filename, line, section, section_line, lvalue, ltype, k, data, userdata); |
932921b5 LP |
161 | } |
162 | ||
12ca818f LP |
163 | int config_parse_unit_strv_printf( |
164 | const char *unit, | |
165 | const char *filename, | |
166 | unsigned line, | |
167 | const char *section, | |
168 | unsigned section_line, | |
169 | const char *lvalue, | |
170 | int ltype, | |
171 | const char *rvalue, | |
172 | void *data, | |
173 | void *userdata) { | |
8fef7659 LP |
174 | |
175 | Unit *u = userdata; | |
74051b9b | 176 | _cleanup_free_ char *k = NULL; |
19f6d710 | 177 | int r; |
8fef7659 LP |
178 | |
179 | assert(filename); | |
180 | assert(lvalue); | |
181 | assert(rvalue); | |
182 | assert(u); | |
183 | ||
19f6d710 | 184 | r = unit_full_printf(u, rvalue, &k); |
12ca818f | 185 | if (r < 0) { |
063c4b1a | 186 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue); |
12ca818f LP |
187 | return 0; |
188 | } | |
8fef7659 | 189 | |
12ca818f | 190 | return config_parse_strv(unit, filename, line, section, section_line, lvalue, ltype, k, data, userdata); |
8fef7659 LP |
191 | } |
192 | ||
5f5d8eab LP |
193 | int config_parse_unit_path_printf( |
194 | const char *unit, | |
195 | const char *filename, | |
196 | unsigned line, | |
197 | const char *section, | |
198 | unsigned section_line, | |
199 | const char *lvalue, | |
200 | int ltype, | |
201 | const char *rvalue, | |
202 | void *data, | |
203 | void *userdata) { | |
6ea832a2 | 204 | |
74051b9b | 205 | _cleanup_free_ char *k = NULL; |
811ba7a0 | 206 | Unit *u = userdata; |
19f6d710 | 207 | int r; |
2c75fb73 | 208 | bool fatal = ltype; |
6ea832a2 LP |
209 | |
210 | assert(filename); | |
211 | assert(lvalue); | |
212 | assert(rvalue); | |
6ea832a2 LP |
213 | assert(u); |
214 | ||
e3c3d676 ZJS |
215 | /* Let's not bother with anything that is too long */ |
216 | if (strlen(rvalue) >= PATH_MAX) { | |
217 | log_syntax(unit, LOG_ERR, filename, line, 0, | |
218 | "%s value too long%s.", | |
219 | lvalue, fatal ? "" : ", ignoring"); | |
220 | return fatal ? -ENAMETOOLONG : 0; | |
221 | } | |
222 | ||
19f6d710 | 223 | r = unit_full_printf(u, rvalue, &k); |
811ba7a0 | 224 | if (r < 0) { |
2c75fb73 | 225 | log_syntax(unit, LOG_ERR, filename, line, r, |
063c4b1a | 226 | "Failed to resolve unit specifiers in '%s'%s: %m", |
e3c3d676 | 227 | rvalue, fatal ? "" : ", ignoring"); |
2c75fb73 | 228 | return fatal ? -ENOEXEC : 0; |
811ba7a0 | 229 | } |
6ea832a2 | 230 | |
811ba7a0 LP |
231 | return config_parse_path(unit, filename, line, section, section_line, lvalue, ltype, k, data, userdata); |
232 | } | |
233 | ||
234 | int config_parse_unit_path_strv_printf( | |
235 | const char *unit, | |
236 | const char *filename, | |
237 | unsigned line, | |
238 | const char *section, | |
239 | unsigned section_line, | |
240 | const char *lvalue, | |
241 | int ltype, | |
242 | const char *rvalue, | |
243 | void *data, | |
244 | void *userdata) { | |
245 | ||
a2a5291b | 246 | char ***x = data; |
811ba7a0 | 247 | Unit *u = userdata; |
811ba7a0 | 248 | int r; |
035fe294 | 249 | const char *p; |
811ba7a0 LP |
250 | |
251 | assert(filename); | |
252 | assert(lvalue); | |
253 | assert(rvalue); | |
254 | assert(u); | |
255 | ||
499295fb | 256 | if (isempty(rvalue)) { |
9f2d41a6 | 257 | *x = strv_free(*x); |
499295fb YW |
258 | return 0; |
259 | } | |
260 | ||
035fe294 ZJS |
261 | for (p = rvalue;;) { |
262 | _cleanup_free_ char *word = NULL, *k = NULL; | |
811ba7a0 | 263 | |
035fe294 ZJS |
264 | r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES); |
265 | if (r == 0) | |
266 | return 0; | |
267 | if (r == -ENOMEM) | |
268 | return log_oom(); | |
269 | if (r < 0) { | |
270 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
271 | "Invalid syntax, ignoring: %s", rvalue); | |
272 | return 0; | |
273 | } | |
811ba7a0 | 274 | |
035fe294 | 275 | r = unit_full_printf(u, word, &k); |
811ba7a0 | 276 | if (r < 0) { |
035fe294 | 277 | log_syntax(unit, LOG_ERR, filename, line, r, |
063c4b1a | 278 | "Failed to resolve unit specifiers in '%s', ignoring: %m", word); |
811ba7a0 LP |
279 | return 0; |
280 | } | |
281 | ||
282 | if (!utf8_is_valid(k)) { | |
0e05ee04 | 283 | log_syntax_invalid_utf8(unit, LOG_ERR, filename, line, rvalue); |
811ba7a0 LP |
284 | return 0; |
285 | } | |
286 | ||
287 | if (!path_is_absolute(k)) { | |
035fe294 ZJS |
288 | log_syntax(unit, LOG_ERR, filename, line, 0, |
289 | "Symlink path is not absolute: %s", k); | |
811ba7a0 LP |
290 | return 0; |
291 | } | |
292 | ||
293 | path_kill_slashes(k); | |
294 | ||
295 | r = strv_push(x, k); | |
296 | if (r < 0) | |
297 | return log_oom(); | |
811ba7a0 LP |
298 | k = NULL; |
299 | } | |
6ea832a2 LP |
300 | } |
301 | ||
e8e581bf ZJS |
302 | int config_parse_socket_listen(const char *unit, |
303 | const char *filename, | |
304 | unsigned line, | |
305 | const char *section, | |
71a61510 | 306 | unsigned section_line, |
e8e581bf ZJS |
307 | const char *lvalue, |
308 | int ltype, | |
309 | const char *rvalue, | |
310 | void *data, | |
311 | void *userdata) { | |
42f4e3c4 | 312 | |
b1389b0d ZJS |
313 | _cleanup_free_ SocketPort *p = NULL; |
314 | SocketPort *tail; | |
542563ba | 315 | Socket *s; |
19f6d710 | 316 | int r; |
16354eff | 317 | |
42f4e3c4 LP |
318 | assert(filename); |
319 | assert(lvalue); | |
320 | assert(rvalue); | |
321 | assert(data); | |
322 | ||
595ed347 | 323 | s = SOCKET(data); |
542563ba | 324 | |
74051b9b LP |
325 | if (isempty(rvalue)) { |
326 | /* An empty assignment removes all ports */ | |
327 | socket_free_ports(s); | |
328 | return 0; | |
329 | } | |
330 | ||
7f110ff9 LP |
331 | p = new0(SocketPort, 1); |
332 | if (!p) | |
74051b9b | 333 | return log_oom(); |
916abb21 | 334 | |
74051b9b | 335 | if (ltype != SOCKET_SOCKET) { |
916abb21 | 336 | |
74051b9b | 337 | p->type = ltype; |
19f6d710 LP |
338 | r = unit_full_printf(UNIT(s), rvalue, &p->path); |
339 | if (r < 0) { | |
063c4b1a | 340 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue); |
12ca818f | 341 | return 0; |
916abb21 LP |
342 | } |
343 | ||
344 | path_kill_slashes(p->path); | |
345 | ||
7a22745a | 346 | } else if (streq(lvalue, "ListenNetlink")) { |
74051b9b | 347 | _cleanup_free_ char *k = NULL; |
1fd45a90 | 348 | |
7a22745a | 349 | p->type = SOCKET_SOCKET; |
19f6d710 | 350 | r = unit_full_printf(UNIT(s), rvalue, &k); |
12ca818f | 351 | if (r < 0) { |
063c4b1a | 352 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue); |
12ca818f LP |
353 | return 0; |
354 | } | |
7a22745a | 355 | |
12ca818f | 356 | r = socket_address_parse_netlink(&p->address, k); |
1fd45a90 | 357 | if (r < 0) { |
063c4b1a | 358 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse address value in '%s', ignoring: %m", k); |
7a22745a LP |
359 | return 0; |
360 | } | |
361 | ||
542563ba | 362 | } else { |
74051b9b | 363 | _cleanup_free_ char *k = NULL; |
1fd45a90 | 364 | |
542563ba | 365 | p->type = SOCKET_SOCKET; |
19f6d710 | 366 | r = unit_full_printf(UNIT(s), rvalue, &k); |
12ca818f | 367 | if (r < 0) { |
063c4b1a | 368 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue); |
12ca818f LP |
369 | return 0; |
370 | } | |
542563ba | 371 | |
12ca818f | 372 | r = socket_address_parse_and_warn(&p->address, k); |
1fd45a90 | 373 | if (r < 0) { |
f847b8b7 | 374 | if (r != -EAFNOSUPPORT) |
063c4b1a | 375 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse address value in '%s', ignoring: %m", k); |
c0b34696 | 376 | return 0; |
542563ba LP |
377 | } |
378 | ||
379 | if (streq(lvalue, "ListenStream")) | |
380 | p->address.type = SOCK_STREAM; | |
381 | else if (streq(lvalue, "ListenDatagram")) | |
382 | p->address.type = SOCK_DGRAM; | |
383 | else { | |
384 | assert(streq(lvalue, "ListenSequentialPacket")); | |
385 | p->address.type = SOCK_SEQPACKET; | |
386 | } | |
387 | ||
388 | if (socket_address_family(&p->address) != AF_LOCAL && p->address.type == SOCK_SEQPACKET) { | |
12ca818f | 389 | log_syntax(unit, LOG_ERR, filename, line, 0, "Address family not supported, ignoring: %s", rvalue); |
c0b34696 | 390 | return 0; |
542563ba | 391 | } |
16354eff LP |
392 | } |
393 | ||
542563ba | 394 | p->fd = -1; |
15087cdb PS |
395 | p->auxiliary_fds = NULL; |
396 | p->n_auxiliary_fds = 0; | |
2e41a51e | 397 | p->socket = s; |
49f91047 | 398 | |
533f8a67 YW |
399 | LIST_FIND_TAIL(port, s->ports, tail); |
400 | LIST_INSERT_AFTER(port, s->ports, tail, p); | |
401 | ||
b1389b0d | 402 | p = NULL; |
542563ba | 403 | |
16354eff | 404 | return 0; |
42f4e3c4 LP |
405 | } |
406 | ||
74bb646e SS |
407 | int config_parse_socket_protocol(const char *unit, |
408 | const char *filename, | |
409 | unsigned line, | |
410 | const char *section, | |
411 | unsigned section_line, | |
412 | const char *lvalue, | |
413 | int ltype, | |
414 | const char *rvalue, | |
415 | void *data, | |
416 | void *userdata) { | |
417 | Socket *s; | |
e045e325 | 418 | int r; |
74bb646e SS |
419 | |
420 | assert(filename); | |
421 | assert(lvalue); | |
422 | assert(rvalue); | |
423 | assert(data); | |
424 | ||
425 | s = SOCKET(data); | |
426 | ||
e045e325 YW |
427 | r = socket_protocol_from_name(rvalue); |
428 | if (r < 0) { | |
063c4b1a | 429 | log_syntax(unit, LOG_ERR, filename, line, r, "Invalid socket protocol '%s', ignoring: %m", rvalue); |
e045e325 YW |
430 | return 0; |
431 | } else if (!IN_SET(r, IPPROTO_UDPLITE, IPPROTO_SCTP)) { | |
74bb646e SS |
432 | log_syntax(unit, LOG_ERR, filename, line, 0, "Socket protocol not supported, ignoring: %s", rvalue); |
433 | return 0; | |
434 | } | |
435 | ||
e045e325 YW |
436 | s->socket_protocol = r; |
437 | ||
74bb646e SS |
438 | return 0; |
439 | } | |
440 | ||
e8e581bf ZJS |
441 | int config_parse_socket_bind(const char *unit, |
442 | const char *filename, | |
443 | unsigned line, | |
444 | const char *section, | |
71a61510 | 445 | unsigned section_line, |
e8e581bf ZJS |
446 | const char *lvalue, |
447 | int ltype, | |
448 | const char *rvalue, | |
449 | void *data, | |
450 | void *userdata) { | |
42f4e3c4 | 451 | |
542563ba | 452 | Socket *s; |
c0120d99 | 453 | SocketAddressBindIPv6Only b; |
42f4e3c4 LP |
454 | |
455 | assert(filename); | |
456 | assert(lvalue); | |
457 | assert(rvalue); | |
458 | assert(data); | |
459 | ||
595ed347 | 460 | s = SOCKET(data); |
542563ba | 461 | |
6f90844f | 462 | b = parse_socket_address_bind_ipv6_only_or_bool(rvalue); |
5198dabc | 463 | if (b < 0) { |
6f90844f YW |
464 | log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse bind IPv6 only value, ignoring: %s", rvalue); |
465 | return 0; | |
466 | } | |
42f4e3c4 | 467 | |
6f90844f | 468 | s->bind_ipv6_only = b; |
542563ba | 469 | |
42f4e3c4 LP |
470 | return 0; |
471 | } | |
472 | ||
41bf0590 LP |
473 | int config_parse_exec_nice( |
474 | const char *unit, | |
475 | const char *filename, | |
476 | unsigned line, | |
477 | const char *section, | |
478 | unsigned section_line, | |
479 | const char *lvalue, | |
480 | int ltype, | |
481 | const char *rvalue, | |
482 | void *data, | |
483 | void *userdata) { | |
034c6ed7 | 484 | |
fb33a393 | 485 | ExecContext *c = data; |
e8e581bf | 486 | int priority, r; |
034c6ed7 LP |
487 | |
488 | assert(filename); | |
489 | assert(lvalue); | |
490 | assert(rvalue); | |
491 | assert(data); | |
492 | ||
de5e6038 YW |
493 | if (isempty(rvalue)) { |
494 | c->nice_set = false; | |
495 | return 0; | |
496 | } | |
497 | ||
41bf0590 | 498 | r = parse_nice(rvalue, &priority); |
e8e581bf | 499 | if (r < 0) { |
41bf0590 LP |
500 | if (r == -ERANGE) |
501 | log_syntax(unit, LOG_ERR, filename, line, r, "Nice priority out of range, ignoring: %s", rvalue); | |
502 | else | |
063c4b1a | 503 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse nice priority '%s', ignoring: %m", rvalue); |
c0b34696 | 504 | return 0; |
034c6ed7 LP |
505 | } |
506 | ||
fb33a393 | 507 | c->nice = priority; |
71155933 | 508 | c->nice_set = true; |
fb33a393 | 509 | |
034c6ed7 LP |
510 | return 0; |
511 | } | |
512 | ||
e9eb2c02 LP |
513 | int config_parse_exec_oom_score_adjust( |
514 | const char* unit, | |
515 | const char *filename, | |
516 | unsigned line, | |
517 | const char *section, | |
518 | unsigned section_line, | |
519 | const char *lvalue, | |
520 | int ltype, | |
521 | const char *rvalue, | |
522 | void *data, | |
523 | void *userdata) { | |
034c6ed7 | 524 | |
fb33a393 | 525 | ExecContext *c = data; |
e8e581bf | 526 | int oa, r; |
034c6ed7 LP |
527 | |
528 | assert(filename); | |
529 | assert(lvalue); | |
530 | assert(rvalue); | |
531 | assert(data); | |
532 | ||
e9eb2c02 LP |
533 | if (isempty(rvalue)) { |
534 | c->oom_score_adjust_set = false; | |
c0b34696 | 535 | return 0; |
034c6ed7 LP |
536 | } |
537 | ||
e9eb2c02 | 538 | r = parse_oom_score_adjust(rvalue, &oa); |
e9eb2c02 | 539 | if (r < 0) { |
063c4b1a YW |
540 | if (r == -ERANGE) |
541 | log_syntax(unit, LOG_ERR, filename, line, r, "OOM score adjust value out of range, ignoring: %s", rvalue); | |
542 | else | |
543 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse the OOM score adjust value '%s', ignoring: %m", rvalue); | |
c0b34696 | 544 | return 0; |
034c6ed7 LP |
545 | } |
546 | ||
dd6c17b1 LP |
547 | c->oom_score_adjust = oa; |
548 | c->oom_score_adjust_set = true; | |
fb33a393 | 549 | |
034c6ed7 LP |
550 | return 0; |
551 | } | |
552 | ||
527b7a42 LP |
553 | int config_parse_exec( |
554 | const char *unit, | |
555 | const char *filename, | |
556 | unsigned line, | |
557 | const char *section, | |
558 | unsigned section_line, | |
559 | const char *lvalue, | |
560 | int ltype, | |
561 | const char *rvalue, | |
562 | void *data, | |
563 | void *userdata) { | |
034c6ed7 | 564 | |
46a0d98a | 565 | ExecCommand **e = data; |
5125e762 | 566 | Unit *u = userdata; |
46a0d98a FB |
567 | const char *p; |
568 | bool semicolon; | |
7f110ff9 | 569 | int r; |
034c6ed7 LP |
570 | |
571 | assert(filename); | |
572 | assert(lvalue); | |
573 | assert(rvalue); | |
61e5d8ed | 574 | assert(e); |
034c6ed7 | 575 | |
74051b9b | 576 | e += ltype; |
c83f1f30 | 577 | rvalue += strspn(rvalue, WHITESPACE); |
c83f1f30 | 578 | |
74051b9b LP |
579 | if (isempty(rvalue)) { |
580 | /* An empty assignment resets the list */ | |
f1acf85a | 581 | *e = exec_command_free_list(*e); |
74051b9b LP |
582 | return 0; |
583 | } | |
584 | ||
bd1b973f | 585 | p = rvalue; |
46a0d98a | 586 | do { |
dea7b6b0 | 587 | _cleanup_free_ char *path = NULL, *firstword = NULL; |
165a31c0 LP |
588 | ExecCommandFlags flags = 0; |
589 | bool ignore = false, separate_argv0 = false; | |
dea7b6b0 | 590 | _cleanup_free_ ExecCommand *nce = NULL; |
46a0d98a FB |
591 | _cleanup_strv_free_ char **n = NULL; |
592 | size_t nlen = 0, nbufsize = 0; | |
5125e762 | 593 | const char *f; |
6c666e26 | 594 | |
46a0d98a FB |
595 | semicolon = false; |
596 | ||
9a82ab95 | 597 | r = extract_first_word_and_warn(&p, &firstword, NULL, EXTRACT_QUOTES|EXTRACT_CUNESCAPE, unit, filename, line, rvalue); |
46a0d98a FB |
598 | if (r <= 0) |
599 | return 0; | |
6c666e26 | 600 | |
46a0d98a | 601 | f = firstword; |
007f48bb | 602 | for (;;) { |
165a31c0 LP |
603 | /* We accept an absolute path as first argument. If it's prefixed with - and the path doesn't |
604 | * exist, we ignore it instead of erroring out; if it's prefixed with @, we allow overriding of | |
605 | * argv[0]; if it's prefixed with +, it will be run with full privileges and no sandboxing; if | |
606 | * it's prefixed with '!' we apply sandboxing, but do not change user/group credentials; if | |
607 | * it's prefixed with '!!', then we apply user/group credentials if the kernel supports ambient | |
608 | * capabilities -- if it doesn't we don't apply the credentials themselves, but do apply most | |
609 | * other sandboxing, with some special exceptions for changing UID. | |
610 | * | |
611 | * The idea is that '!!' may be used to write services that can take benefit of systemd's | |
612 | * UID/GID dropping if the kernel supports ambient creds, but provide an automatic fallback to | |
613 | * privilege dropping within the daemon if the kernel does not offer that. */ | |
614 | ||
615 | if (*f == '-' && !(flags & EXEC_COMMAND_IGNORE_FAILURE)) { | |
616 | flags |= EXEC_COMMAND_IGNORE_FAILURE; | |
46a0d98a | 617 | ignore = true; |
165a31c0 | 618 | } else if (*f == '@' && !separate_argv0) |
46a0d98a | 619 | separate_argv0 = true; |
165a31c0 LP |
620 | else if (*f == '+' && !(flags & (EXEC_COMMAND_FULLY_PRIVILEGED|EXEC_COMMAND_NO_SETUID|EXEC_COMMAND_AMBIENT_MAGIC))) |
621 | flags |= EXEC_COMMAND_FULLY_PRIVILEGED; | |
622 | else if (*f == '!' && !(flags & (EXEC_COMMAND_FULLY_PRIVILEGED|EXEC_COMMAND_NO_SETUID|EXEC_COMMAND_AMBIENT_MAGIC))) | |
623 | flags |= EXEC_COMMAND_NO_SETUID; | |
624 | else if (*f == '!' && !(flags & (EXEC_COMMAND_FULLY_PRIVILEGED|EXEC_COMMAND_AMBIENT_MAGIC))) { | |
625 | flags &= ~EXEC_COMMAND_NO_SETUID; | |
626 | flags |= EXEC_COMMAND_AMBIENT_MAGIC; | |
627 | } else | |
46a0d98a | 628 | break; |
313cefa1 | 629 | f++; |
61e5d8ed | 630 | } |
46a0d98a | 631 | |
5125e762 LP |
632 | r = unit_full_printf(u, f, &path); |
633 | if (r < 0) { | |
bb28e684 | 634 | log_syntax(unit, LOG_ERR, filename, line, r, |
063c4b1a | 635 | "Failed to resolve unit specifiers in '%s'%s: %m", |
bb28e684 ZJS |
636 | f, ignore ? ", ignoring" : ""); |
637 | return ignore ? 0 : -ENOEXEC; | |
5125e762 LP |
638 | } |
639 | ||
640 | if (isempty(path)) { | |
46a0d98a | 641 | /* First word is either "-" or "@" with no command. */ |
bb28e684 | 642 | log_syntax(unit, LOG_ERR, filename, line, 0, |
063c4b1a | 643 | "Empty path in command line%s: '%s'", |
bb28e684 ZJS |
644 | ignore ? ", ignoring" : "", rvalue); |
645 | return ignore ? 0 : -ENOEXEC; | |
b2fadec6 | 646 | } |
5125e762 | 647 | if (!string_is_safe(path)) { |
bb28e684 | 648 | log_syntax(unit, LOG_ERR, filename, line, 0, |
5008da1e ZJS |
649 | "Executable name contains special characters%s: %s", |
650 | ignore ? ", ignoring" : "", path); | |
bb28e684 | 651 | return ignore ? 0 : -ENOEXEC; |
46a0d98a | 652 | } |
5125e762 | 653 | if (endswith(path, "/")) { |
bb28e684 ZJS |
654 | log_syntax(unit, LOG_ERR, filename, line, 0, |
655 | "Executable path specifies a directory%s: %s", | |
5008da1e | 656 | ignore ? ", ignoring" : "", path); |
bb28e684 | 657 | return ignore ? 0 : -ENOEXEC; |
46a0d98a | 658 | } |
61e5d8ed | 659 | |
5008da1e ZJS |
660 | if (!path_is_absolute(path)) { |
661 | const char *prefix; | |
662 | bool found = false; | |
663 | ||
664 | if (!filename_is_valid(path)) { | |
665 | log_syntax(unit, LOG_ERR, filename, line, 0, | |
666 | "Neither a valid executable name nor an absolute path%s: %s", | |
667 | ignore ? ", ignoring" : "", path); | |
668 | return ignore ? 0 : -ENOEXEC; | |
669 | } | |
670 | ||
671 | /* Resolve a single-component name to a full path */ | |
672 | NULSTR_FOREACH(prefix, DEFAULT_PATH_NULSTR) { | |
673 | _cleanup_free_ char *fullpath = NULL; | |
674 | ||
675 | fullpath = strjoin(prefix, "/", path); | |
676 | if (!fullpath) | |
677 | return log_oom(); | |
678 | ||
679 | if (access(fullpath, F_OK) >= 0) { | |
680 | free_and_replace(path, fullpath); | |
681 | found = true; | |
682 | break; | |
683 | } | |
684 | } | |
685 | ||
686 | if (!found) { | |
687 | log_syntax(unit, LOG_ERR, filename, line, 0, | |
688 | "Executable \"%s\" not found in path \"%s\"%s", | |
689 | path, DEFAULT_PATH, ignore ? ", ignoring" : ""); | |
690 | return ignore ? 0 : -ENOEXEC; | |
691 | } | |
692 | } | |
693 | ||
46a0d98a | 694 | if (!separate_argv0) { |
5125e762 LP |
695 | char *w = NULL; |
696 | ||
46a0d98a FB |
697 | if (!GREEDY_REALLOC(n, nbufsize, nlen + 2)) |
698 | return log_oom(); | |
5125e762 LP |
699 | |
700 | w = strdup(path); | |
701 | if (!w) | |
46a0d98a | 702 | return log_oom(); |
5125e762 | 703 | n[nlen++] = w; |
46a0d98a FB |
704 | n[nlen] = NULL; |
705 | } | |
7f110ff9 | 706 | |
46a0d98a FB |
707 | path_kill_slashes(path); |
708 | ||
4b1c1753 | 709 | while (!isempty(p)) { |
5125e762 | 710 | _cleanup_free_ char *word = NULL, *resolved = NULL; |
46a0d98a FB |
711 | |
712 | /* Check explicitly for an unquoted semicolon as | |
713 | * command separator token. */ | |
714 | if (p[0] == ';' && (!p[1] || strchr(WHITESPACE, p[1]))) { | |
313cefa1 | 715 | p++; |
46a0d98a FB |
716 | p += strspn(p, WHITESPACE); |
717 | semicolon = true; | |
718 | break; | |
c8539536 | 719 | } |
7f110ff9 | 720 | |
5125e762 LP |
721 | /* Check for \; explicitly, to not confuse it with \\; or "\;" or "\\;" etc. |
722 | * extract_first_word() would return the same for all of those. */ | |
46a0d98a | 723 | if (p[0] == '\\' && p[1] == ';' && (!p[2] || strchr(WHITESPACE, p[2]))) { |
5125e762 LP |
724 | char *w; |
725 | ||
46a0d98a FB |
726 | p += 2; |
727 | p += strspn(p, WHITESPACE); | |
5125e762 | 728 | |
46a0d98a FB |
729 | if (!GREEDY_REALLOC(n, nbufsize, nlen + 2)) |
730 | return log_oom(); | |
5125e762 LP |
731 | |
732 | w = strdup(";"); | |
733 | if (!w) | |
46a0d98a | 734 | return log_oom(); |
5125e762 | 735 | n[nlen++] = w; |
46a0d98a FB |
736 | n[nlen] = NULL; |
737 | continue; | |
61e5d8ed | 738 | } |
c8539536 | 739 | |
9a82ab95 | 740 | r = extract_first_word_and_warn(&p, &word, NULL, EXTRACT_QUOTES|EXTRACT_CUNESCAPE, unit, filename, line, rvalue); |
46a0d98a FB |
741 | if (r == 0) |
742 | break; | |
5125e762 | 743 | if (r < 0) |
bb28e684 | 744 | return ignore ? 0 : -ENOEXEC; |
5125e762 LP |
745 | |
746 | r = unit_full_printf(u, word, &resolved); | |
747 | if (r < 0) { | |
bb28e684 | 748 | log_syntax(unit, LOG_ERR, filename, line, r, |
063c4b1a | 749 | "Failed to resolve unit specifiers in %s%s: %m", |
bb28e684 ZJS |
750 | word, ignore ? ", ignoring" : ""); |
751 | return ignore ? 0 : -ENOEXEC; | |
5125e762 | 752 | } |
46a0d98a FB |
753 | |
754 | if (!GREEDY_REALLOC(n, nbufsize, nlen + 2)) | |
755 | return log_oom(); | |
1cc6c93a YW |
756 | |
757 | n[nlen++] = TAKE_PTR(resolved); | |
46a0d98a | 758 | n[nlen] = NULL; |
61e5d8ed LP |
759 | } |
760 | ||
46a0d98a | 761 | if (!n || !n[0]) { |
bb28e684 ZJS |
762 | log_syntax(unit, LOG_ERR, filename, line, 0, |
763 | "Empty executable name or zeroeth argument%s: %s", | |
764 | ignore ? ", ignoring" : "", rvalue); | |
765 | return ignore ? 0 : -ENOEXEC; | |
7f110ff9 | 766 | } |
6c666e26 | 767 | |
7f110ff9 | 768 | nce = new0(ExecCommand, 1); |
46a0d98a FB |
769 | if (!nce) |
770 | return log_oom(); | |
61e5d8ed | 771 | |
1cc6c93a YW |
772 | nce->argv = TAKE_PTR(n); |
773 | nce->path = TAKE_PTR(path); | |
165a31c0 | 774 | nce->flags = flags; |
034c6ed7 | 775 | |
61e5d8ed | 776 | exec_command_append_list(e, nce); |
01f78473 | 777 | |
46a0d98a | 778 | /* Do not _cleanup_free_ these. */ |
46a0d98a | 779 | nce = NULL; |
034c6ed7 | 780 | |
46a0d98a FB |
781 | rvalue = p; |
782 | } while (semicolon); | |
034c6ed7 | 783 | |
46a0d98a | 784 | return 0; |
034c6ed7 LP |
785 | } |
786 | ||
f975e971 LP |
787 | DEFINE_CONFIG_PARSE_ENUM(config_parse_service_type, service_type, ServiceType, "Failed to parse service type"); |
788 | DEFINE_CONFIG_PARSE_ENUM(config_parse_service_restart, service_restart, ServiceRestart, "Failed to parse service restart specifier"); | |
034c6ed7 | 789 | |
d31645ad LP |
790 | int config_parse_socket_bindtodevice( |
791 | const char* unit, | |
792 | const char *filename, | |
793 | unsigned line, | |
794 | const char *section, | |
795 | unsigned section_line, | |
796 | const char *lvalue, | |
797 | int ltype, | |
798 | const char *rvalue, | |
799 | void *data, | |
800 | void *userdata) { | |
acbb0225 LP |
801 | |
802 | Socket *s = data; | |
acbb0225 LP |
803 | |
804 | assert(filename); | |
805 | assert(lvalue); | |
806 | assert(rvalue); | |
807 | assert(data); | |
808 | ||
063c4b1a YW |
809 | if (isempty(rvalue) || streq(rvalue, "*")) { |
810 | s->bind_to_device = mfree(s->bind_to_device); | |
811 | return 0; | |
812 | } | |
d31645ad | 813 | |
063c4b1a YW |
814 | if (!ifname_valid(rvalue)) { |
815 | log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid interface name, ignoring: %s", rvalue); | |
816 | return 0; | |
817 | } | |
acbb0225 | 818 | |
063c4b1a | 819 | free_and_strdup(&s->bind_to_device, rvalue); |
acbb0225 LP |
820 | |
821 | return 0; | |
822 | } | |
823 | ||
9bd6a50e LP |
824 | int config_parse_exec_input( |
825 | const char *unit, | |
826 | const char *filename, | |
827 | unsigned line, | |
828 | const char *section, | |
829 | unsigned section_line, | |
830 | const char *lvalue, | |
831 | int ltype, | |
832 | const char *rvalue, | |
833 | void *data, | |
834 | void *userdata) { | |
52c239d7 | 835 | |
52c239d7 | 836 | ExecContext *c = data; |
2038c3f5 LP |
837 | Unit *u = userdata; |
838 | const char *n; | |
839 | ExecInput ei; | |
52c239d7 LB |
840 | int r; |
841 | ||
842 | assert(data); | |
843 | assert(filename); | |
844 | assert(line); | |
845 | assert(rvalue); | |
846 | ||
2038c3f5 LP |
847 | n = startswith(rvalue, "fd:"); |
848 | if (n) { | |
849 | _cleanup_free_ char *resolved = NULL; | |
850 | ||
851 | r = unit_full_printf(u, n, &resolved); | |
852 | if (r < 0) | |
063c4b1a | 853 | return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s': %m", n); |
2038c3f5 LP |
854 | |
855 | if (isempty(resolved)) | |
856 | resolved = mfree(resolved); | |
857 | else if (!fdname_is_valid(resolved)) { | |
858 | log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid file descriptor name: %s", resolved); | |
859 | return -EINVAL; | |
52c239d7 | 860 | } |
9bd6a50e | 861 | |
2038c3f5 LP |
862 | free_and_replace(c->stdio_fdname[STDIN_FILENO], resolved); |
863 | ||
864 | ei = EXEC_INPUT_NAMED_FD; | |
865 | ||
866 | } else if ((n = startswith(rvalue, "file:"))) { | |
867 | _cleanup_free_ char *resolved = NULL; | |
868 | ||
869 | r = unit_full_printf(u, n, &resolved); | |
52c239d7 | 870 | if (r < 0) |
063c4b1a | 871 | return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s': %m", n); |
9bd6a50e | 872 | |
2038c3f5 LP |
873 | if (!path_is_absolute(resolved)) { |
874 | log_syntax(unit, LOG_ERR, filename, line, 0, "file: requires an absolute path name: %s", resolved); | |
875 | return -EINVAL; | |
876 | } | |
877 | ||
99be45a4 | 878 | if (!path_is_normalized(resolved)) { |
2038c3f5 LP |
879 | log_syntax(unit, LOG_ERR, filename, line, 0, "file: requires a normalized path name: %s", resolved); |
880 | return -EINVAL; | |
881 | } | |
882 | ||
883 | free_and_replace(c->stdio_file[STDIN_FILENO], resolved); | |
884 | ||
885 | ei = EXEC_INPUT_FILE; | |
9bd6a50e | 886 | |
52c239d7 | 887 | } else { |
9bd6a50e LP |
888 | ei = exec_input_from_string(rvalue); |
889 | if (ei < 0) { | |
52c239d7 | 890 | log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse input specifier, ignoring: %s", rvalue); |
9bd6a50e LP |
891 | return 0; |
892 | } | |
52c239d7 | 893 | } |
9bd6a50e | 894 | |
2038c3f5 | 895 | c->std_input = ei; |
9bd6a50e | 896 | return 0; |
52c239d7 LB |
897 | } |
898 | ||
08f3be7a LP |
899 | int config_parse_exec_input_text( |
900 | const char *unit, | |
901 | const char *filename, | |
902 | unsigned line, | |
903 | const char *section, | |
904 | unsigned section_line, | |
905 | const char *lvalue, | |
906 | int ltype, | |
907 | const char *rvalue, | |
908 | void *data, | |
909 | void *userdata) { | |
910 | ||
911 | _cleanup_free_ char *unescaped = NULL, *resolved = NULL; | |
912 | ExecContext *c = data; | |
913 | Unit *u = userdata; | |
914 | size_t sz; | |
915 | void *p; | |
916 | int r; | |
917 | ||
918 | assert(data); | |
919 | assert(filename); | |
920 | assert(line); | |
921 | assert(rvalue); | |
922 | ||
923 | if (isempty(rvalue)) { | |
924 | /* Reset if the empty string is assigned */ | |
925 | c->stdin_data = mfree(c->stdin_data); | |
926 | c->stdin_data_size = 0; | |
52c239d7 LB |
927 | return 0; |
928 | } | |
08f3be7a LP |
929 | |
930 | r = cunescape(rvalue, 0, &unescaped); | |
2038c3f5 | 931 | if (r < 0) |
063c4b1a | 932 | return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to decode C escaped text '%s': %m", rvalue); |
08f3be7a LP |
933 | |
934 | r = unit_full_printf(u, unescaped, &resolved); | |
2038c3f5 | 935 | if (r < 0) |
063c4b1a | 936 | return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s': %m", unescaped); |
08f3be7a LP |
937 | |
938 | sz = strlen(resolved); | |
939 | if (c->stdin_data_size + sz + 1 < c->stdin_data_size || /* check for overflow */ | |
940 | c->stdin_data_size + sz + 1 > EXEC_STDIN_DATA_MAX) { | |
2038c3f5 LP |
941 | log_syntax(unit, LOG_ERR, filename, line, 0, "Standard input data too large (%zu), maximum of %zu permitted, ignoring.", c->stdin_data_size + sz, (size_t) EXEC_STDIN_DATA_MAX); |
942 | return -E2BIG; | |
08f3be7a LP |
943 | } |
944 | ||
945 | p = realloc(c->stdin_data, c->stdin_data_size + sz + 1); | |
946 | if (!p) | |
947 | return log_oom(); | |
948 | ||
949 | *((char*) mempcpy((char*) p + c->stdin_data_size, resolved, sz)) = '\n'; | |
950 | ||
951 | c->stdin_data = p; | |
952 | c->stdin_data_size += sz + 1; | |
953 | ||
954 | return 0; | |
52c239d7 LB |
955 | } |
956 | ||
08f3be7a LP |
957 | int config_parse_exec_input_data( |
958 | const char *unit, | |
959 | const char *filename, | |
960 | unsigned line, | |
961 | const char *section, | |
962 | unsigned section_line, | |
963 | const char *lvalue, | |
964 | int ltype, | |
965 | const char *rvalue, | |
966 | void *data, | |
967 | void *userdata) { | |
968 | ||
08f3be7a LP |
969 | _cleanup_free_ void *p = NULL; |
970 | ExecContext *c = data; | |
971 | size_t sz; | |
972 | void *q; | |
973 | int r; | |
974 | ||
975 | assert(data); | |
976 | assert(filename); | |
977 | assert(line); | |
978 | assert(rvalue); | |
979 | ||
980 | if (isempty(rvalue)) { | |
981 | /* Reset if the empty string is assigned */ | |
982 | c->stdin_data = mfree(c->stdin_data); | |
983 | c->stdin_data_size = 0; | |
984 | return 0; | |
985 | } | |
986 | ||
081f36d8 | 987 | r = unbase64mem(rvalue, (size_t) -1, &p, &sz); |
2038c3f5 | 988 | if (r < 0) |
081f36d8 | 989 | return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to decode base64 data, ignoring: %s", rvalue); |
08f3be7a LP |
990 | |
991 | assert(sz > 0); | |
992 | ||
993 | if (c->stdin_data_size + sz < c->stdin_data_size || /* check for overflow */ | |
994 | c->stdin_data_size + sz > EXEC_STDIN_DATA_MAX) { | |
2038c3f5 LP |
995 | log_syntax(unit, LOG_ERR, filename, line, 0, "Standard input data too large (%zu), maximum of %zu permitted, ignoring.", c->stdin_data_size + sz, (size_t) EXEC_STDIN_DATA_MAX); |
996 | return -E2BIG; | |
08f3be7a LP |
997 | } |
998 | ||
999 | q = realloc(c->stdin_data, c->stdin_data_size + sz); | |
1000 | if (!q) | |
1001 | return log_oom(); | |
1002 | ||
1003 | memcpy((uint8_t*) q + c->stdin_data_size, p, sz); | |
1004 | ||
1005 | c->stdin_data = q; | |
1006 | c->stdin_data_size += sz; | |
1007 | ||
1008 | return 0; | |
1009 | } | |
1010 | ||
2038c3f5 LP |
1011 | int config_parse_exec_output( |
1012 | const char *unit, | |
1013 | const char *filename, | |
1014 | unsigned line, | |
1015 | const char *section, | |
1016 | unsigned section_line, | |
1017 | const char *lvalue, | |
1018 | int ltype, | |
1019 | const char *rvalue, | |
1020 | void *data, | |
1021 | void *userdata) { | |
1022 | ||
1023 | _cleanup_free_ char *resolved = NULL; | |
1024 | const char *n; | |
52c239d7 | 1025 | ExecContext *c = data; |
2038c3f5 | 1026 | Unit *u = userdata; |
52c239d7 | 1027 | ExecOutput eo; |
52c239d7 LB |
1028 | int r; |
1029 | ||
1030 | assert(data); | |
1031 | assert(filename); | |
1032 | assert(line); | |
1033 | assert(lvalue); | |
1034 | assert(rvalue); | |
1035 | ||
2038c3f5 LP |
1036 | n = startswith(rvalue, "fd:"); |
1037 | if (n) { | |
1038 | r = unit_full_printf(u, n, &resolved); | |
1039 | if (r < 0) | |
063c4b1a | 1040 | return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s: %m", n); |
2038c3f5 LP |
1041 | |
1042 | if (isempty(resolved)) | |
1043 | resolved = mfree(resolved); | |
1044 | else if (!fdname_is_valid(resolved)) { | |
1045 | log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid file descriptor name: %s", resolved); | |
1046 | return -EINVAL; | |
52c239d7 | 1047 | } |
2038c3f5 | 1048 | |
52c239d7 | 1049 | eo = EXEC_OUTPUT_NAMED_FD; |
2038c3f5 LP |
1050 | |
1051 | } else if ((n = startswith(rvalue, "file:"))) { | |
1052 | ||
1053 | r = unit_full_printf(u, n, &resolved); | |
1054 | if (r < 0) | |
063c4b1a | 1055 | return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s: %m", n); |
2038c3f5 LP |
1056 | |
1057 | if (!path_is_absolute(resolved)) { | |
1058 | log_syntax(unit, LOG_ERR, filename, line, 0, "file: requires an absolute path name: %s", resolved); | |
1059 | return -EINVAL; | |
1060 | } | |
1061 | ||
99be45a4 | 1062 | if (!path_is_normalized(resolved)) { |
2038c3f5 LP |
1063 | log_syntax(unit, LOG_ERR, filename, line, 0, "file: requires a normalized path name, ignoring: %s", resolved); |
1064 | return -EINVAL; | |
1065 | } | |
1066 | ||
1067 | eo = EXEC_OUTPUT_FILE; | |
1068 | ||
52c239d7 LB |
1069 | } else { |
1070 | eo = exec_output_from_string(rvalue); | |
2038c3f5 | 1071 | if (eo < 0) { |
52c239d7 LB |
1072 | log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse output specifier, ignoring: %s", rvalue); |
1073 | return 0; | |
1074 | } | |
1075 | } | |
1076 | ||
1077 | if (streq(lvalue, "StandardOutput")) { | |
2038c3f5 LP |
1078 | if (eo == EXEC_OUTPUT_NAMED_FD) |
1079 | free_and_replace(c->stdio_fdname[STDOUT_FILENO], resolved); | |
1080 | else | |
1081 | free_and_replace(c->stdio_file[STDOUT_FILENO], resolved); | |
1082 | ||
52c239d7 | 1083 | c->std_output = eo; |
2038c3f5 | 1084 | |
52c239d7 | 1085 | } else { |
2038c3f5 LP |
1086 | assert(streq(lvalue, "StandardError")); |
1087 | ||
1088 | if (eo == EXEC_OUTPUT_NAMED_FD) | |
1089 | free_and_replace(c->stdio_fdname[STDERR_FILENO], resolved); | |
1090 | else | |
1091 | free_and_replace(c->stdio_file[STDERR_FILENO], resolved); | |
1092 | ||
1093 | c->std_error = eo; | |
52c239d7 | 1094 | } |
2038c3f5 LP |
1095 | |
1096 | return 0; | |
52c239d7 | 1097 | } |
87f0e418 | 1098 | |
e8e581bf ZJS |
1099 | int config_parse_exec_io_class(const char *unit, |
1100 | const char *filename, | |
1101 | unsigned line, | |
1102 | const char *section, | |
71a61510 | 1103 | unsigned section_line, |
e8e581bf ZJS |
1104 | const char *lvalue, |
1105 | int ltype, | |
1106 | const char *rvalue, | |
1107 | void *data, | |
1108 | void *userdata) { | |
94f04347 LP |
1109 | |
1110 | ExecContext *c = data; | |
1111 | int x; | |
1112 | ||
1113 | assert(filename); | |
1114 | assert(lvalue); | |
1115 | assert(rvalue); | |
1116 | assert(data); | |
1117 | ||
617d253a YW |
1118 | if (isempty(rvalue)) { |
1119 | c->ioprio_set = false; | |
1120 | c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0); | |
1121 | return 0; | |
1122 | } | |
1123 | ||
f8b69d1d MS |
1124 | x = ioprio_class_from_string(rvalue); |
1125 | if (x < 0) { | |
12ca818f | 1126 | log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse IO scheduling class, ignoring: %s", rvalue); |
c0b34696 | 1127 | return 0; |
0d87eb42 | 1128 | } |
94f04347 LP |
1129 | |
1130 | c->ioprio = IOPRIO_PRIO_VALUE(x, IOPRIO_PRIO_DATA(c->ioprio)); | |
1131 | c->ioprio_set = true; | |
1132 | ||
1133 | return 0; | |
1134 | } | |
1135 | ||
e8e581bf ZJS |
1136 | int config_parse_exec_io_priority(const char *unit, |
1137 | const char *filename, | |
1138 | unsigned line, | |
1139 | const char *section, | |
71a61510 | 1140 | unsigned section_line, |
e8e581bf ZJS |
1141 | const char *lvalue, |
1142 | int ltype, | |
1143 | const char *rvalue, | |
1144 | void *data, | |
1145 | void *userdata) { | |
94f04347 LP |
1146 | |
1147 | ExecContext *c = data; | |
e8e581bf | 1148 | int i, r; |
94f04347 LP |
1149 | |
1150 | assert(filename); | |
1151 | assert(lvalue); | |
1152 | assert(rvalue); | |
1153 | assert(data); | |
1154 | ||
617d253a YW |
1155 | if (isempty(rvalue)) { |
1156 | c->ioprio_set = false; | |
1157 | c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0); | |
1158 | return 0; | |
1159 | } | |
1160 | ||
7f452159 LP |
1161 | r = ioprio_parse_priority(rvalue, &i); |
1162 | if (r < 0) { | |
12ca818f | 1163 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse IO priority, ignoring: %s", rvalue); |
c0b34696 | 1164 | return 0; |
071830ff LP |
1165 | } |
1166 | ||
94f04347 LP |
1167 | c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_PRIO_CLASS(c->ioprio), i); |
1168 | c->ioprio_set = true; | |
1169 | ||
071830ff LP |
1170 | return 0; |
1171 | } | |
1172 | ||
e8e581bf ZJS |
1173 | int config_parse_exec_cpu_sched_policy(const char *unit, |
1174 | const char *filename, | |
1175 | unsigned line, | |
1176 | const char *section, | |
71a61510 | 1177 | unsigned section_line, |
e8e581bf ZJS |
1178 | const char *lvalue, |
1179 | int ltype, | |
1180 | const char *rvalue, | |
1181 | void *data, | |
1182 | void *userdata) { | |
9eba9da4 | 1183 | |
94f04347 LP |
1184 | ExecContext *c = data; |
1185 | int x; | |
1186 | ||
1187 | assert(filename); | |
1188 | assert(lvalue); | |
1189 | assert(rvalue); | |
1190 | assert(data); | |
1191 | ||
b00e1a9e YW |
1192 | if (isempty(rvalue)) { |
1193 | c->cpu_sched_set = false; | |
1194 | c->cpu_sched_policy = SCHED_OTHER; | |
1195 | c->cpu_sched_priority = 0; | |
1196 | return 0; | |
1197 | } | |
1198 | ||
f8b69d1d MS |
1199 | x = sched_policy_from_string(rvalue); |
1200 | if (x < 0) { | |
12ca818f | 1201 | log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse CPU scheduling policy, ignoring: %s", rvalue); |
c0b34696 | 1202 | return 0; |
0d87eb42 | 1203 | } |
94f04347 LP |
1204 | |
1205 | c->cpu_sched_policy = x; | |
bb112710 HHPF |
1206 | /* Moving to or from real-time policy? We need to adjust the priority */ |
1207 | c->cpu_sched_priority = CLAMP(c->cpu_sched_priority, sched_get_priority_min(x), sched_get_priority_max(x)); | |
94f04347 LP |
1208 | c->cpu_sched_set = true; |
1209 | ||
1210 | return 0; | |
1211 | } | |
1212 | ||
e8e581bf ZJS |
1213 | int config_parse_exec_cpu_sched_prio(const char *unit, |
1214 | const char *filename, | |
1215 | unsigned line, | |
1216 | const char *section, | |
71a61510 | 1217 | unsigned section_line, |
e8e581bf ZJS |
1218 | const char *lvalue, |
1219 | int ltype, | |
1220 | const char *rvalue, | |
1221 | void *data, | |
1222 | void *userdata) { | |
9eba9da4 LP |
1223 | |
1224 | ExecContext *c = data; | |
e8e581bf | 1225 | int i, min, max, r; |
9eba9da4 LP |
1226 | |
1227 | assert(filename); | |
1228 | assert(lvalue); | |
1229 | assert(rvalue); | |
1230 | assert(data); | |
1231 | ||
e8e581bf ZJS |
1232 | r = safe_atoi(rvalue, &i); |
1233 | if (r < 0) { | |
063c4b1a | 1234 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse CPU scheduling priority, ignoring: %s", rvalue); |
c0b34696 | 1235 | return 0; |
94f04347 | 1236 | } |
9eba9da4 | 1237 | |
bb112710 HHPF |
1238 | /* On Linux RR/FIFO range from 1 to 99 and OTHER/BATCH may only be 0 */ |
1239 | min = sched_get_priority_min(c->cpu_sched_policy); | |
1240 | max = sched_get_priority_max(c->cpu_sched_policy); | |
1241 | ||
1242 | if (i < min || i > max) { | |
12ca818f | 1243 | log_syntax(unit, LOG_ERR, filename, line, 0, "CPU scheduling priority is out of range, ignoring: %s", rvalue); |
bb112710 HHPF |
1244 | return 0; |
1245 | } | |
1246 | ||
94f04347 LP |
1247 | c->cpu_sched_priority = i; |
1248 | c->cpu_sched_set = true; | |
1249 | ||
1250 | return 0; | |
1251 | } | |
1252 | ||
e8e581bf ZJS |
1253 | int config_parse_exec_cpu_affinity(const char *unit, |
1254 | const char *filename, | |
1255 | unsigned line, | |
1256 | const char *section, | |
71a61510 | 1257 | unsigned section_line, |
e8e581bf ZJS |
1258 | const char *lvalue, |
1259 | int ltype, | |
1260 | const char *rvalue, | |
1261 | void *data, | |
1262 | void *userdata) { | |
94f04347 LP |
1263 | |
1264 | ExecContext *c = data; | |
9d5ca7f8 FB |
1265 | _cleanup_cpu_free_ cpu_set_t *cpuset = NULL; |
1266 | int ncpus; | |
94f04347 LP |
1267 | |
1268 | assert(filename); | |
1269 | assert(lvalue); | |
1270 | assert(rvalue); | |
1271 | assert(data); | |
1272 | ||
765d143b | 1273 | ncpus = parse_cpu_set_and_warn(rvalue, &cpuset, unit, filename, line, lvalue); |
9d5ca7f8 FB |
1274 | if (ncpus < 0) |
1275 | return ncpus; | |
487393e9 | 1276 | |
501941aa | 1277 | if (ncpus == 0) { |
9d5ca7f8 | 1278 | /* An empty assignment resets the CPU list */ |
501941aa YW |
1279 | c->cpuset = cpu_set_mfree(c->cpuset); |
1280 | c->cpuset_ncpus = 0; | |
1281 | return 0; | |
1282 | } | |
1283 | ||
1284 | if (!c->cpuset) { | |
ae2a15bc | 1285 | c->cpuset = TAKE_PTR(cpuset); |
501941aa YW |
1286 | c->cpuset_ncpus = (unsigned) ncpus; |
1287 | return 0; | |
1288 | } | |
1289 | ||
1290 | if (c->cpuset_ncpus < (unsigned) ncpus) { | |
1291 | CPU_OR_S(CPU_ALLOC_SIZE(c->cpuset_ncpus), cpuset, c->cpuset, cpuset); | |
1292 | CPU_FREE(c->cpuset); | |
ae2a15bc | 1293 | c->cpuset = TAKE_PTR(cpuset); |
501941aa YW |
1294 | c->cpuset_ncpus = (unsigned) ncpus; |
1295 | return 0; | |
9eba9da4 | 1296 | } |
501941aa YW |
1297 | |
1298 | CPU_OR_S(CPU_ALLOC_SIZE((unsigned) ncpus), c->cpuset, c->cpuset, cpuset); | |
9eba9da4 | 1299 | |
94f04347 LP |
1300 | return 0; |
1301 | } | |
1302 | ||
e8e581bf ZJS |
1303 | int config_parse_exec_secure_bits(const char *unit, |
1304 | const char *filename, | |
1305 | unsigned line, | |
1306 | const char *section, | |
71a61510 | 1307 | unsigned section_line, |
e8e581bf ZJS |
1308 | const char *lvalue, |
1309 | int ltype, | |
1310 | const char *rvalue, | |
1311 | void *data, | |
1312 | void *userdata) { | |
94f04347 LP |
1313 | |
1314 | ExecContext *c = data; | |
035fe294 | 1315 | int r; |
94f04347 LP |
1316 | |
1317 | assert(filename); | |
1318 | assert(lvalue); | |
1319 | assert(rvalue); | |
1320 | assert(data); | |
1321 | ||
74051b9b LP |
1322 | if (isempty(rvalue)) { |
1323 | /* An empty assignment resets the field */ | |
1324 | c->secure_bits = 0; | |
1325 | return 0; | |
1326 | } | |
1327 | ||
07d46372 | 1328 | r = secure_bits_from_string(rvalue); |
07d46372 YW |
1329 | if (r < 0) { |
1330 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
063c4b1a | 1331 | "Failed to parse secure bits, ignoring: %s", rvalue); |
07d46372 YW |
1332 | return 0; |
1333 | } | |
035fe294 | 1334 | |
07d46372 | 1335 | c->secure_bits = r; |
035fe294 | 1336 | |
07d46372 | 1337 | return 0; |
94f04347 LP |
1338 | } |
1339 | ||
a103496c | 1340 | int config_parse_capability_set( |
65dce264 LP |
1341 | const char *unit, |
1342 | const char *filename, | |
1343 | unsigned line, | |
1344 | const char *section, | |
1345 | unsigned section_line, | |
1346 | const char *lvalue, | |
1347 | int ltype, | |
1348 | const char *rvalue, | |
1349 | void *data, | |
1350 | void *userdata) { | |
94f04347 | 1351 | |
a103496c IP |
1352 | uint64_t *capability_set = data; |
1353 | uint64_t sum = 0, initial = 0; | |
260abb78 | 1354 | bool invert = false; |
dd1f5bd0 | 1355 | int r; |
94f04347 LP |
1356 | |
1357 | assert(filename); | |
1358 | assert(lvalue); | |
1359 | assert(rvalue); | |
1360 | assert(data); | |
1361 | ||
260abb78 LP |
1362 | if (rvalue[0] == '~') { |
1363 | invert = true; | |
1364 | rvalue++; | |
1365 | } | |
1366 | ||
70d54d90 | 1367 | if (streq(lvalue, "CapabilityBoundingSet")) |
a103496c | 1368 | initial = CAP_ALL; /* initialized to all bits on */ |
755d4b67 | 1369 | /* else "AmbientCapabilities" initialized to all bits off */ |
260abb78 | 1370 | |
dd1f5bd0 | 1371 | r = capability_set_from_string(rvalue, &sum); |
dd1f5bd0 | 1372 | if (r < 0) { |
063c4b1a | 1373 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse %s= specifier '%s', ignoring: %m", lvalue, rvalue); |
dd1f5bd0 | 1374 | return 0; |
94f04347 | 1375 | } |
9eba9da4 | 1376 | |
a103496c | 1377 | if (sum == 0 || *capability_set == initial) |
c792ec2e IP |
1378 | /* "", "~" or uninitialized data -> replace */ |
1379 | *capability_set = invert ? ~sum : sum; | |
1380 | else { | |
a103496c | 1381 | /* previous data -> merge */ |
c792ec2e IP |
1382 | if (invert) |
1383 | *capability_set &= ~sum; | |
1384 | else | |
1385 | *capability_set |= sum; | |
1386 | } | |
260abb78 | 1387 | |
9eba9da4 LP |
1388 | return 0; |
1389 | } | |
1390 | ||
023a4f67 | 1391 | DEFINE_CONFIG_PARSE_ENUM(config_parse_exec_utmp_mode, exec_utmp_mode, ExecUtmpMode, "Failed to parse utmp mode"); |
f975e971 | 1392 | DEFINE_CONFIG_PARSE_ENUM(config_parse_kill_mode, kill_mode, KillMode, "Failed to parse kill mode"); |
50159e6a | 1393 | |
83555251 LP |
1394 | int config_parse_exec_mount_flags( |
1395 | const char *unit, | |
1396 | const char *filename, | |
1397 | unsigned line, | |
1398 | const char *section, | |
1399 | unsigned section_line, | |
1400 | const char *lvalue, | |
1401 | int ltype, | |
1402 | const char *rvalue, | |
1403 | void *data, | |
1404 | void *userdata) { | |
15ae422b | 1405 | |
e28bb14a | 1406 | ExecContext *c = data; |
c7383828 | 1407 | int r; |
15ae422b LP |
1408 | |
1409 | assert(filename); | |
1410 | assert(lvalue); | |
1411 | assert(rvalue); | |
1412 | assert(data); | |
1413 | ||
c7383828 ZJS |
1414 | r = mount_propagation_flags_from_string(rvalue, &c->mount_flags); |
1415 | if (r < 0) | |
063c4b1a | 1416 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse mount flag %s, ignoring: %m", rvalue); |
e28bb14a | 1417 | |
15ae422b LP |
1418 | return 0; |
1419 | } | |
1420 | ||
5f8640fb LP |
1421 | int config_parse_exec_selinux_context( |
1422 | const char *unit, | |
1423 | const char *filename, | |
1424 | unsigned line, | |
1425 | const char *section, | |
1426 | unsigned section_line, | |
1427 | const char *lvalue, | |
1428 | int ltype, | |
1429 | const char *rvalue, | |
1430 | void *data, | |
1431 | void *userdata) { | |
1432 | ||
1433 | ExecContext *c = data; | |
1434 | Unit *u = userdata; | |
1435 | bool ignore; | |
1436 | char *k; | |
1437 | int r; | |
1438 | ||
1439 | assert(filename); | |
1440 | assert(lvalue); | |
1441 | assert(rvalue); | |
1442 | assert(data); | |
1443 | ||
1444 | if (isempty(rvalue)) { | |
a1e58e8e | 1445 | c->selinux_context = mfree(c->selinux_context); |
5f8640fb LP |
1446 | c->selinux_context_ignore = false; |
1447 | return 0; | |
1448 | } | |
1449 | ||
1450 | if (rvalue[0] == '-') { | |
1451 | ignore = true; | |
1452 | rvalue++; | |
1453 | } else | |
1454 | ignore = false; | |
1455 | ||
18913df9 | 1456 | r = unit_full_printf(u, rvalue, &k); |
5f8640fb | 1457 | if (r < 0) { |
bb28e684 | 1458 | log_syntax(unit, LOG_ERR, filename, line, r, |
063c4b1a YW |
1459 | "Failed to resolve unit specifiers in '%s'%s: %m", |
1460 | rvalue, ignore ? ", ignoring" : ""); | |
bb28e684 | 1461 | return ignore ? 0 : -ENOEXEC; |
5f8640fb LP |
1462 | } |
1463 | ||
063c4b1a | 1464 | free_and_replace(c->selinux_context, k); |
5f8640fb LP |
1465 | c->selinux_context_ignore = ignore; |
1466 | ||
1467 | return 0; | |
1468 | } | |
1469 | ||
eef65bf3 MS |
1470 | int config_parse_exec_apparmor_profile( |
1471 | const char *unit, | |
1472 | const char *filename, | |
1473 | unsigned line, | |
1474 | const char *section, | |
1475 | unsigned section_line, | |
1476 | const char *lvalue, | |
1477 | int ltype, | |
1478 | const char *rvalue, | |
1479 | void *data, | |
1480 | void *userdata) { | |
1481 | ||
1482 | ExecContext *c = data; | |
1483 | Unit *u = userdata; | |
1484 | bool ignore; | |
1485 | char *k; | |
1486 | int r; | |
1487 | ||
1488 | assert(filename); | |
1489 | assert(lvalue); | |
1490 | assert(rvalue); | |
1491 | assert(data); | |
1492 | ||
1493 | if (isempty(rvalue)) { | |
a1e58e8e | 1494 | c->apparmor_profile = mfree(c->apparmor_profile); |
eef65bf3 MS |
1495 | c->apparmor_profile_ignore = false; |
1496 | return 0; | |
1497 | } | |
1498 | ||
1499 | if (rvalue[0] == '-') { | |
1500 | ignore = true; | |
1501 | rvalue++; | |
1502 | } else | |
1503 | ignore = false; | |
1504 | ||
18913df9 | 1505 | r = unit_full_printf(u, rvalue, &k); |
eef65bf3 | 1506 | if (r < 0) { |
bb28e684 | 1507 | log_syntax(unit, LOG_ERR, filename, line, r, |
063c4b1a YW |
1508 | "Failed to resolve unit specifiers in '%s'%s: %m", |
1509 | rvalue, ignore ? ", ignoring" : ""); | |
bb28e684 | 1510 | return ignore ? 0 : -ENOEXEC; |
eef65bf3 MS |
1511 | } |
1512 | ||
063c4b1a | 1513 | free_and_replace(c->apparmor_profile, k); |
eef65bf3 MS |
1514 | c->apparmor_profile_ignore = ignore; |
1515 | ||
1516 | return 0; | |
1517 | } | |
1518 | ||
2ca620c4 WC |
1519 | int config_parse_exec_smack_process_label( |
1520 | const char *unit, | |
1521 | const char *filename, | |
1522 | unsigned line, | |
1523 | const char *section, | |
1524 | unsigned section_line, | |
1525 | const char *lvalue, | |
1526 | int ltype, | |
1527 | const char *rvalue, | |
1528 | void *data, | |
1529 | void *userdata) { | |
1530 | ||
1531 | ExecContext *c = data; | |
1532 | Unit *u = userdata; | |
1533 | bool ignore; | |
1534 | char *k; | |
1535 | int r; | |
1536 | ||
1537 | assert(filename); | |
1538 | assert(lvalue); | |
1539 | assert(rvalue); | |
1540 | assert(data); | |
1541 | ||
1542 | if (isempty(rvalue)) { | |
a1e58e8e | 1543 | c->smack_process_label = mfree(c->smack_process_label); |
2ca620c4 WC |
1544 | c->smack_process_label_ignore = false; |
1545 | return 0; | |
1546 | } | |
1547 | ||
1548 | if (rvalue[0] == '-') { | |
1549 | ignore = true; | |
1550 | rvalue++; | |
1551 | } else | |
1552 | ignore = false; | |
1553 | ||
18913df9 | 1554 | r = unit_full_printf(u, rvalue, &k); |
2ca620c4 | 1555 | if (r < 0) { |
bb28e684 | 1556 | log_syntax(unit, LOG_ERR, filename, line, r, |
063c4b1a YW |
1557 | "Failed to resolve unit specifiers in '%s'%s: %m", |
1558 | rvalue, ignore ? ", ignoring" : ""); | |
bb28e684 | 1559 | return ignore ? 0 : -ENOEXEC; |
2ca620c4 WC |
1560 | } |
1561 | ||
063c4b1a | 1562 | free_and_replace(c->smack_process_label, k); |
2ca620c4 WC |
1563 | c->smack_process_label_ignore = ignore; |
1564 | ||
1565 | return 0; | |
1566 | } | |
1567 | ||
e8e581bf ZJS |
1568 | int config_parse_timer(const char *unit, |
1569 | const char *filename, | |
1570 | unsigned line, | |
1571 | const char *section, | |
71a61510 | 1572 | unsigned section_line, |
e8e581bf ZJS |
1573 | const char *lvalue, |
1574 | int ltype, | |
1575 | const char *rvalue, | |
1576 | void *data, | |
1577 | void *userdata) { | |
871d7de4 LP |
1578 | |
1579 | Timer *t = data; | |
2507992f | 1580 | usec_t usec = 0; |
871d7de4 LP |
1581 | TimerValue *v; |
1582 | TimerBase b; | |
921b5987 | 1583 | _cleanup_(calendar_spec_freep) CalendarSpec *c = NULL; |
2507992f DC |
1584 | Unit *u = userdata; |
1585 | _cleanup_free_ char *k = NULL; | |
1586 | int r; | |
871d7de4 LP |
1587 | |
1588 | assert(filename); | |
1589 | assert(lvalue); | |
1590 | assert(rvalue); | |
1591 | assert(data); | |
1592 | ||
74051b9b LP |
1593 | if (isempty(rvalue)) { |
1594 | /* Empty assignment resets list */ | |
1595 | timer_free_values(t); | |
1596 | return 0; | |
1597 | } | |
1598 | ||
36697dc0 LP |
1599 | b = timer_base_from_string(lvalue); |
1600 | if (b < 0) { | |
12ca818f | 1601 | log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse timer base, ignoring: %s", lvalue); |
c0b34696 | 1602 | return 0; |
871d7de4 LP |
1603 | } |
1604 | ||
2507992f DC |
1605 | r = unit_full_printf(u, rvalue, &k); |
1606 | if (r < 0) { | |
063c4b1a | 1607 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue); |
2507992f DC |
1608 | return 0; |
1609 | } | |
1610 | ||
36697dc0 | 1611 | if (b == TIMER_CALENDAR) { |
2507992f DC |
1612 | if (calendar_spec_from_string(k, &c) < 0) { |
1613 | log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse calendar specification, ignoring: %s", k); | |
36697dc0 LP |
1614 | return 0; |
1615 | } | |
063c4b1a | 1616 | } else |
2507992f DC |
1617 | if (parse_sec(k, &usec) < 0) { |
1618 | log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse timer value, ignoring: %s", k); | |
36697dc0 LP |
1619 | return 0; |
1620 | } | |
871d7de4 | 1621 | |
36697dc0 | 1622 | v = new0(TimerValue, 1); |
921b5987 | 1623 | if (!v) |
74051b9b | 1624 | return log_oom(); |
871d7de4 LP |
1625 | |
1626 | v->base = b; | |
2507992f | 1627 | v->value = usec; |
921b5987 | 1628 | v->calendar_spec = TAKE_PTR(c); |
871d7de4 | 1629 | |
71fda00f | 1630 | LIST_PREPEND(value, t->values, v); |
871d7de4 LP |
1631 | |
1632 | return 0; | |
1633 | } | |
1634 | ||
3ecaa09b LP |
1635 | int config_parse_trigger_unit( |
1636 | const char *unit, | |
1637 | const char *filename, | |
1638 | unsigned line, | |
1639 | const char *section, | |
71a61510 | 1640 | unsigned section_line, |
3ecaa09b LP |
1641 | const char *lvalue, |
1642 | int ltype, | |
1643 | const char *rvalue, | |
1644 | void *data, | |
1645 | void *userdata) { | |
871d7de4 | 1646 | |
74051b9b | 1647 | _cleanup_free_ char *p = NULL; |
3ecaa09b LP |
1648 | Unit *u = data; |
1649 | UnitType type; | |
1650 | int r; | |
398ef8ba LP |
1651 | |
1652 | assert(filename); | |
1653 | assert(lvalue); | |
1654 | assert(rvalue); | |
1655 | assert(data); | |
1656 | ||
eef85c4a | 1657 | if (!hashmap_isempty(u->dependencies[UNIT_TRIGGERS])) { |
12ca818f | 1658 | log_syntax(unit, LOG_ERR, filename, line, 0, "Multiple units to trigger specified, ignoring: %s", rvalue); |
3ecaa09b LP |
1659 | return 0; |
1660 | } | |
871d7de4 | 1661 | |
19f6d710 | 1662 | r = unit_name_printf(u, rvalue, &p); |
12ca818f | 1663 | if (r < 0) { |
063c4b1a | 1664 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue); |
12ca818f LP |
1665 | return 0; |
1666 | } | |
74051b9b | 1667 | |
12ca818f | 1668 | type = unit_name_to_type(p); |
3ecaa09b | 1669 | if (type < 0) { |
12ca818f | 1670 | log_syntax(unit, LOG_ERR, filename, line, 0, "Unit type not valid, ignoring: %s", rvalue); |
c0b34696 | 1671 | return 0; |
871d7de4 | 1672 | } |
49219a1c JR |
1673 | if (unit_has_name(u, p)) { |
1674 | log_syntax(unit, LOG_ERR, filename, line, 0, "Units cannot trigger themselves, ignoring: %s", rvalue); | |
3ecaa09b LP |
1675 | return 0; |
1676 | } | |
1677 | ||
eef85c4a | 1678 | r = unit_add_two_dependencies_by_name(u, UNIT_BEFORE, UNIT_TRIGGERS, p, NULL, true, UNIT_DEPENDENCY_FILE); |
57020a3a | 1679 | if (r < 0) { |
12ca818f | 1680 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to add trigger on %s, ignoring: %m", p); |
c0b34696 | 1681 | return 0; |
871d7de4 LP |
1682 | } |
1683 | ||
1684 | return 0; | |
1685 | } | |
1686 | ||
e8e581bf ZJS |
1687 | int config_parse_path_spec(const char *unit, |
1688 | const char *filename, | |
1689 | unsigned line, | |
1690 | const char *section, | |
71a61510 | 1691 | unsigned section_line, |
e8e581bf ZJS |
1692 | const char *lvalue, |
1693 | int ltype, | |
1694 | const char *rvalue, | |
1695 | void *data, | |
1696 | void *userdata) { | |
01f78473 LP |
1697 | |
1698 | Path *p = data; | |
1699 | PathSpec *s; | |
1700 | PathType b; | |
7fd1b19b | 1701 | _cleanup_free_ char *k = NULL; |
19f6d710 | 1702 | int r; |
01f78473 LP |
1703 | |
1704 | assert(filename); | |
1705 | assert(lvalue); | |
1706 | assert(rvalue); | |
1707 | assert(data); | |
1708 | ||
74051b9b LP |
1709 | if (isempty(rvalue)) { |
1710 | /* Empty assignment clears list */ | |
1711 | path_free_specs(p); | |
1712 | return 0; | |
1713 | } | |
1714 | ||
93e4c84b LP |
1715 | b = path_type_from_string(lvalue); |
1716 | if (b < 0) { | |
12ca818f | 1717 | log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse path type, ignoring: %s", lvalue); |
c0b34696 | 1718 | return 0; |
01f78473 LP |
1719 | } |
1720 | ||
19f6d710 LP |
1721 | r = unit_full_printf(UNIT(p), rvalue, &k); |
1722 | if (r < 0) { | |
063c4b1a | 1723 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue); |
12ca818f | 1724 | return 0; |
487060c2 | 1725 | } |
93e4c84b | 1726 | |
063c4b1a YW |
1727 | path_kill_slashes(k); |
1728 | ||
93e4c84b | 1729 | if (!path_is_absolute(k)) { |
12ca818f | 1730 | log_syntax(unit, LOG_ERR, filename, line, 0, "Path is not absolute, ignoring: %s", k); |
c0b34696 | 1731 | return 0; |
01f78473 LP |
1732 | } |
1733 | ||
93e4c84b | 1734 | s = new0(PathSpec, 1); |
543295ad | 1735 | if (!s) |
93e4c84b | 1736 | return log_oom(); |
01f78473 | 1737 | |
718db961 | 1738 | s->unit = UNIT(p); |
063c4b1a | 1739 | s->path = TAKE_PTR(k); |
01f78473 LP |
1740 | s->type = b; |
1741 | s->inotify_fd = -1; | |
1742 | ||
71fda00f | 1743 | LIST_PREPEND(spec, p->specs, s); |
01f78473 LP |
1744 | |
1745 | return 0; | |
1746 | } | |
1747 | ||
b02cb41c LP |
1748 | int config_parse_socket_service( |
1749 | const char *unit, | |
1750 | const char *filename, | |
1751 | unsigned line, | |
1752 | const char *section, | |
1753 | unsigned section_line, | |
1754 | const char *lvalue, | |
1755 | int ltype, | |
1756 | const char *rvalue, | |
1757 | void *data, | |
1758 | void *userdata) { | |
d9ff321a | 1759 | |
4afd3348 | 1760 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
8dd4c05b | 1761 | _cleanup_free_ char *p = NULL; |
d9ff321a | 1762 | Socket *s = data; |
4ff77f66 | 1763 | Unit *x; |
8dd4c05b | 1764 | int r; |
d9ff321a LP |
1765 | |
1766 | assert(filename); | |
1767 | assert(lvalue); | |
1768 | assert(rvalue); | |
1769 | assert(data); | |
1770 | ||
19f6d710 | 1771 | r = unit_name_printf(UNIT(s), rvalue, &p); |
613b411c | 1772 | if (r < 0) { |
063c4b1a | 1773 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s: %m", rvalue); |
bb28e684 | 1774 | return -ENOEXEC; |
613b411c | 1775 | } |
74051b9b | 1776 | |
613b411c | 1777 | if (!endswith(p, ".service")) { |
bb28e684 ZJS |
1778 | log_syntax(unit, LOG_ERR, filename, line, 0, "Unit must be of type service: %s", rvalue); |
1779 | return -ENOEXEC; | |
d9ff321a LP |
1780 | } |
1781 | ||
613b411c | 1782 | r = manager_load_unit(UNIT(s)->manager, p, NULL, &error, &x); |
4ff77f66 | 1783 | if (r < 0) { |
bb28e684 ZJS |
1784 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to load unit %s: %s", rvalue, bus_error_message(&error, r)); |
1785 | return -ENOEXEC; | |
d9ff321a LP |
1786 | } |
1787 | ||
7f7d01ed | 1788 | unit_ref_set(&s->service, UNIT(s), x); |
4ff77f66 | 1789 | |
d9ff321a LP |
1790 | return 0; |
1791 | } | |
1792 | ||
8dd4c05b LP |
1793 | int config_parse_fdname( |
1794 | const char *unit, | |
1795 | const char *filename, | |
1796 | unsigned line, | |
1797 | const char *section, | |
1798 | unsigned section_line, | |
1799 | const char *lvalue, | |
1800 | int ltype, | |
1801 | const char *rvalue, | |
1802 | void *data, | |
1803 | void *userdata) { | |
1804 | ||
1805 | _cleanup_free_ char *p = NULL; | |
1806 | Socket *s = data; | |
1807 | int r; | |
1808 | ||
1809 | assert(filename); | |
1810 | assert(lvalue); | |
1811 | assert(rvalue); | |
1812 | assert(data); | |
1813 | ||
1814 | if (isempty(rvalue)) { | |
1815 | s->fdname = mfree(s->fdname); | |
1816 | return 0; | |
1817 | } | |
1818 | ||
18913df9 | 1819 | r = unit_full_printf(UNIT(s), rvalue, &p); |
8dd4c05b | 1820 | if (r < 0) { |
063c4b1a | 1821 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue); |
8dd4c05b LP |
1822 | return 0; |
1823 | } | |
1824 | ||
1825 | if (!fdname_is_valid(p)) { | |
1826 | log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid file descriptor name, ignoring: %s", p); | |
1827 | return 0; | |
1828 | } | |
1829 | ||
3b319885 | 1830 | return free_and_replace(s->fdname, p); |
8dd4c05b LP |
1831 | } |
1832 | ||
b02cb41c LP |
1833 | int config_parse_service_sockets( |
1834 | const char *unit, | |
1835 | const char *filename, | |
1836 | unsigned line, | |
1837 | const char *section, | |
1838 | unsigned section_line, | |
1839 | const char *lvalue, | |
1840 | int ltype, | |
1841 | const char *rvalue, | |
1842 | void *data, | |
1843 | void *userdata) { | |
f976f3f6 LP |
1844 | |
1845 | Service *s = data; | |
7b2313f5 | 1846 | const char *p; |
b02cb41c | 1847 | int r; |
f976f3f6 LP |
1848 | |
1849 | assert(filename); | |
1850 | assert(lvalue); | |
1851 | assert(rvalue); | |
1852 | assert(data); | |
1853 | ||
7b2313f5 | 1854 | p = rvalue; |
9ed794a3 | 1855 | for (;;) { |
6a0f3175 | 1856 | _cleanup_free_ char *word = NULL, *k = NULL; |
f976f3f6 | 1857 | |
7b2313f5 SS |
1858 | r = extract_first_word(&p, &word, NULL, 0); |
1859 | if (r == 0) | |
1860 | break; | |
1861 | if (r == -ENOMEM) | |
74051b9b | 1862 | return log_oom(); |
7b2313f5 SS |
1863 | if (r < 0) { |
1864 | log_syntax(unit, LOG_ERR, filename, line, r, "Trailing garbage in sockets, ignoring: %s", rvalue); | |
1865 | break; | |
1866 | } | |
f976f3f6 | 1867 | |
7b2313f5 | 1868 | r = unit_name_printf(UNIT(s), word, &k); |
b02cb41c | 1869 | if (r < 0) { |
063c4b1a | 1870 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", word); |
b02cb41c LP |
1871 | continue; |
1872 | } | |
57020a3a | 1873 | |
b02cb41c | 1874 | if (!endswith(k, ".socket")) { |
12ca818f | 1875 | log_syntax(unit, LOG_ERR, filename, line, 0, "Unit must be of type socket, ignoring: %s", k); |
f976f3f6 LP |
1876 | continue; |
1877 | } | |
1878 | ||
eef85c4a | 1879 | r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_WANTS, UNIT_AFTER, k, NULL, true, UNIT_DEPENDENCY_FILE); |
57020a3a | 1880 | if (r < 0) |
b02cb41c | 1881 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to add dependency on %s, ignoring: %m", k); |
f976f3f6 | 1882 | |
eef85c4a | 1883 | r = unit_add_dependency_by_name(UNIT(s), UNIT_TRIGGERED_BY, k, NULL, true, UNIT_DEPENDENCY_FILE); |
57020a3a | 1884 | if (r < 0) |
b02cb41c | 1885 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to add dependency on %s, ignoring: %m", k); |
f976f3f6 LP |
1886 | } |
1887 | ||
1888 | return 0; | |
1889 | } | |
1890 | ||
b02cb41c LP |
1891 | int config_parse_bus_name( |
1892 | const char *unit, | |
1893 | const char *filename, | |
1894 | unsigned line, | |
1895 | const char *section, | |
1896 | unsigned section_line, | |
1897 | const char *lvalue, | |
1898 | int ltype, | |
1899 | const char *rvalue, | |
1900 | void *data, | |
1901 | void *userdata) { | |
1902 | ||
1903 | _cleanup_free_ char *k = NULL; | |
1904 | Unit *u = userdata; | |
1905 | int r; | |
1906 | ||
1907 | assert(filename); | |
1908 | assert(lvalue); | |
1909 | assert(rvalue); | |
1910 | assert(u); | |
1911 | ||
1912 | r = unit_full_printf(u, rvalue, &k); | |
1913 | if (r < 0) { | |
063c4b1a | 1914 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue); |
b02cb41c LP |
1915 | return 0; |
1916 | } | |
1917 | ||
1918 | if (!service_name_is_valid(k)) { | |
063c4b1a | 1919 | log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid bus name, ignoring: %s", k); |
b02cb41c LP |
1920 | return 0; |
1921 | } | |
1922 | ||
1923 | return config_parse_string(unit, filename, line, section, section_line, lvalue, ltype, k, data, userdata); | |
1924 | } | |
1925 | ||
aad41f08 LP |
1926 | int config_parse_service_timeout( |
1927 | const char *unit, | |
1928 | const char *filename, | |
1929 | unsigned line, | |
1930 | const char *section, | |
1931 | unsigned section_line, | |
1932 | const char *lvalue, | |
1933 | int ltype, | |
1934 | const char *rvalue, | |
1935 | void *data, | |
1936 | void *userdata) { | |
98709151 LN |
1937 | |
1938 | Service *s = userdata; | |
aad41f08 | 1939 | usec_t usec; |
98709151 LN |
1940 | int r; |
1941 | ||
1942 | assert(filename); | |
1943 | assert(lvalue); | |
1944 | assert(rvalue); | |
1945 | assert(s); | |
1946 | ||
aad41f08 | 1947 | /* This is called for three cases: TimeoutSec=, TimeoutStopSec= and TimeoutStartSec=. */ |
98709151 | 1948 | |
aad41f08 LP |
1949 | r = parse_sec(rvalue, &usec); |
1950 | if (r < 0) { | |
1951 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse %s= parameter, ignoring: %s", lvalue, rvalue); | |
1952 | return 0; | |
1953 | } | |
d568a335 | 1954 | |
36c16a7c LP |
1955 | /* Traditionally, these options accepted 0 to disable the timeouts. However, a timeout of 0 suggests it happens |
1956 | * immediately, hence fix this to become USEC_INFINITY instead. This is in-line with how we internally handle | |
1957 | * all other timeouts. */ | |
aad41f08 LP |
1958 | if (usec <= 0) |
1959 | usec = USEC_INFINITY; | |
1960 | ||
1961 | if (!streq(lvalue, "TimeoutStopSec")) { | |
1962 | s->start_timeout_defined = true; | |
1963 | s->timeout_start_usec = usec; | |
1964 | } | |
36c16a7c | 1965 | |
aad41f08 LP |
1966 | if (!streq(lvalue, "TimeoutStartSec")) |
1967 | s->timeout_stop_usec = usec; | |
36c16a7c | 1968 | |
d568a335 | 1969 | return 0; |
98709151 LN |
1970 | } |
1971 | ||
89beff89 LP |
1972 | int config_parse_sec_fix_0( |
1973 | const char *unit, | |
1974 | const char *filename, | |
1975 | unsigned line, | |
1976 | const char *section, | |
1977 | unsigned section_line, | |
1978 | const char *lvalue, | |
1979 | int ltype, | |
1980 | const char *rvalue, | |
1981 | void *data, | |
1982 | void *userdata) { | |
1983 | ||
1984 | usec_t *usec = data; | |
1985 | int r; | |
1986 | ||
1987 | assert(filename); | |
1988 | assert(lvalue); | |
1989 | assert(rvalue); | |
1990 | assert(usec); | |
1991 | ||
1992 | /* This is pretty much like config_parse_sec(), except that this treats a time of 0 as infinity, for | |
1993 | * compatibility with older versions of systemd where 0 instead of infinity was used as indicator to turn off a | |
1994 | * timeout. */ | |
1995 | ||
0004f698 | 1996 | r = parse_sec_fix_0(rvalue, usec); |
89beff89 LP |
1997 | if (r < 0) { |
1998 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse %s= parameter, ignoring: %s", lvalue, rvalue); | |
1999 | return 0; | |
2000 | } | |
2001 | ||
89beff89 LP |
2002 | return 0; |
2003 | } | |
2004 | ||
66dccd8d LP |
2005 | int config_parse_user_group( |
2006 | const char *unit, | |
2007 | const char *filename, | |
2008 | unsigned line, | |
2009 | const char *section, | |
2010 | unsigned section_line, | |
2011 | const char *lvalue, | |
2012 | int ltype, | |
2013 | const char *rvalue, | |
2014 | void *data, | |
2015 | void *userdata) { | |
2016 | ||
063c4b1a YW |
2017 | _cleanup_free_ char *k = NULL; |
2018 | char **user = data; | |
66dccd8d LP |
2019 | Unit *u = userdata; |
2020 | int r; | |
2021 | ||
2022 | assert(filename); | |
2023 | assert(lvalue); | |
2024 | assert(rvalue); | |
2025 | assert(u); | |
2026 | ||
063c4b1a YW |
2027 | if (isempty(rvalue)) { |
2028 | *user = mfree(*user); | |
2029 | return 0; | |
2030 | } | |
66dccd8d | 2031 | |
063c4b1a YW |
2032 | r = unit_full_printf(u, rvalue, &k); |
2033 | if (r < 0) { | |
2034 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s: %m", rvalue); | |
2035 | return -ENOEXEC; | |
66dccd8d LP |
2036 | } |
2037 | ||
063c4b1a YW |
2038 | if (!valid_user_group_name_or_id(k)) { |
2039 | log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid user/group name or numeric ID: %s", k); | |
2040 | return -ENOEXEC; | |
2041 | } | |
66dccd8d | 2042 | |
063c4b1a | 2043 | return free_and_replace(*user, k); |
66dccd8d LP |
2044 | } |
2045 | ||
2046 | int config_parse_user_group_strv( | |
2047 | const char *unit, | |
2048 | const char *filename, | |
2049 | unsigned line, | |
2050 | const char *section, | |
2051 | unsigned section_line, | |
2052 | const char *lvalue, | |
2053 | int ltype, | |
2054 | const char *rvalue, | |
2055 | void *data, | |
2056 | void *userdata) { | |
2057 | ||
2058 | char ***users = data; | |
2059 | Unit *u = userdata; | |
063c4b1a | 2060 | const char *p = rvalue; |
66dccd8d LP |
2061 | int r; |
2062 | ||
2063 | assert(filename); | |
2064 | assert(lvalue); | |
2065 | assert(rvalue); | |
2066 | assert(u); | |
2067 | ||
2068 | if (isempty(rvalue)) { | |
9f2d41a6 | 2069 | *users = strv_free(*users); |
66dccd8d LP |
2070 | return 0; |
2071 | } | |
2072 | ||
66dccd8d LP |
2073 | for (;;) { |
2074 | _cleanup_free_ char *word = NULL, *k = NULL; | |
2075 | ||
9a82ab95 | 2076 | r = extract_first_word(&p, &word, NULL, 0); |
66dccd8d LP |
2077 | if (r == 0) |
2078 | break; | |
2079 | if (r == -ENOMEM) | |
2080 | return log_oom(); | |
2081 | if (r < 0) { | |
bb28e684 ZJS |
2082 | log_syntax(unit, LOG_ERR, filename, line, r, "Invalid syntax: %s", rvalue); |
2083 | return -ENOEXEC; | |
66dccd8d LP |
2084 | } |
2085 | ||
2086 | r = unit_full_printf(u, word, &k); | |
2087 | if (r < 0) { | |
bb28e684 ZJS |
2088 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s: %m", word); |
2089 | return -ENOEXEC; | |
66dccd8d LP |
2090 | } |
2091 | ||
2092 | if (!valid_user_group_name_or_id(k)) { | |
bb28e684 ZJS |
2093 | log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid user/group name or numeric ID: %s", k); |
2094 | return -ENOEXEC; | |
66dccd8d LP |
2095 | } |
2096 | ||
2097 | r = strv_push(users, k); | |
2098 | if (r < 0) | |
2099 | return log_oom(); | |
2100 | ||
2101 | k = NULL; | |
2102 | } | |
2103 | ||
2104 | return 0; | |
2105 | } | |
2106 | ||
5f5d8eab LP |
2107 | int config_parse_working_directory( |
2108 | const char *unit, | |
2109 | const char *filename, | |
2110 | unsigned line, | |
2111 | const char *section, | |
2112 | unsigned section_line, | |
2113 | const char *lvalue, | |
2114 | int ltype, | |
2115 | const char *rvalue, | |
2116 | void *data, | |
2117 | void *userdata) { | |
2118 | ||
2119 | ExecContext *c = data; | |
2120 | Unit *u = userdata; | |
2121 | bool missing_ok; | |
2122 | int r; | |
2123 | ||
2124 | assert(filename); | |
2125 | assert(lvalue); | |
2126 | assert(rvalue); | |
2127 | assert(c); | |
2128 | assert(u); | |
2129 | ||
2130 | if (rvalue[0] == '-') { | |
2131 | missing_ok = true; | |
2132 | rvalue++; | |
2133 | } else | |
2134 | missing_ok = false; | |
2135 | ||
2136 | if (streq(rvalue, "~")) { | |
2137 | c->working_directory_home = true; | |
2138 | c->working_directory = mfree(c->working_directory); | |
2139 | } else { | |
2140 | _cleanup_free_ char *k = NULL; | |
2141 | ||
2142 | r = unit_full_printf(u, rvalue, &k); | |
2143 | if (r < 0) { | |
bb28e684 ZJS |
2144 | log_syntax(unit, LOG_ERR, filename, line, r, |
2145 | "Failed to resolve unit specifiers in working directory path '%s'%s: %m", | |
2146 | rvalue, missing_ok ? ", ignoring" : ""); | |
2147 | return missing_ok ? 0 : -ENOEXEC; | |
5f5d8eab LP |
2148 | } |
2149 | ||
2150 | path_kill_slashes(k); | |
2151 | ||
2152 | if (!utf8_is_valid(k)) { | |
0e05ee04 | 2153 | log_syntax_invalid_utf8(unit, LOG_ERR, filename, line, rvalue); |
bb28e684 | 2154 | return missing_ok ? 0 : -ENOEXEC; |
5f5d8eab LP |
2155 | } |
2156 | ||
2157 | if (!path_is_absolute(k)) { | |
bb28e684 ZJS |
2158 | log_syntax(unit, LOG_ERR, filename, line, 0, |
2159 | "Working directory path '%s' is not absolute%s.", | |
2160 | rvalue, missing_ok ? ", ignoring" : ""); | |
2161 | return missing_ok ? 0 : -ENOEXEC; | |
5f5d8eab LP |
2162 | } |
2163 | ||
5f5d8eab | 2164 | c->working_directory_home = false; |
bb28e684 | 2165 | free_and_replace(c->working_directory, k); |
5f5d8eab LP |
2166 | } |
2167 | ||
2168 | c->working_directory_missing_ok = missing_ok; | |
2169 | return 0; | |
2170 | } | |
2171 | ||
e8e581bf ZJS |
2172 | int config_parse_unit_env_file(const char *unit, |
2173 | const char *filename, | |
2174 | unsigned line, | |
2175 | const char *section, | |
71a61510 | 2176 | unsigned section_line, |
e8e581bf ZJS |
2177 | const char *lvalue, |
2178 | int ltype, | |
2179 | const char *rvalue, | |
2180 | void *data, | |
2181 | void *userdata) { | |
ddb26e18 | 2182 | |
853b8397 | 2183 | char ***env = data; |
8fef7659 | 2184 | Unit *u = userdata; |
19f6d710 | 2185 | _cleanup_free_ char *n = NULL; |
853b8397 | 2186 | int r; |
ddb26e18 LP |
2187 | |
2188 | assert(filename); | |
2189 | assert(lvalue); | |
2190 | assert(rvalue); | |
2191 | assert(data); | |
2192 | ||
74051b9b LP |
2193 | if (isempty(rvalue)) { |
2194 | /* Empty assignment frees the list */ | |
6796073e | 2195 | *env = strv_free(*env); |
74051b9b LP |
2196 | return 0; |
2197 | } | |
2198 | ||
19f6d710 | 2199 | r = unit_full_printf(u, rvalue, &n); |
12ca818f | 2200 | if (r < 0) { |
063c4b1a | 2201 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue); |
12ca818f LP |
2202 | return 0; |
2203 | } | |
8fef7659 | 2204 | |
12ca818f LP |
2205 | if (!path_is_absolute(n[0] == '-' ? n + 1 : n)) { |
2206 | log_syntax(unit, LOG_ERR, filename, line, 0, "Path '%s' is not absolute, ignoring.", n); | |
afe4bfe2 LP |
2207 | return 0; |
2208 | } | |
2209 | ||
12ca818f | 2210 | r = strv_extend(env, n); |
853b8397 LP |
2211 | if (r < 0) |
2212 | return log_oom(); | |
2213 | ||
2214 | return 0; | |
2215 | } | |
2216 | ||
f7f3f5c3 LP |
2217 | int config_parse_environ( |
2218 | const char *unit, | |
2219 | const char *filename, | |
2220 | unsigned line, | |
2221 | const char *section, | |
2222 | unsigned section_line, | |
2223 | const char *lvalue, | |
2224 | int ltype, | |
2225 | const char *rvalue, | |
2226 | void *data, | |
2227 | void *userdata) { | |
853b8397 LP |
2228 | |
2229 | Unit *u = userdata; | |
035fe294 ZJS |
2230 | char ***env = data; |
2231 | const char *p; | |
19f6d710 | 2232 | int r; |
853b8397 LP |
2233 | |
2234 | assert(filename); | |
2235 | assert(lvalue); | |
2236 | assert(rvalue); | |
97d0e5f8 | 2237 | assert(data); |
853b8397 LP |
2238 | |
2239 | if (isempty(rvalue)) { | |
2240 | /* Empty assignment resets the list */ | |
6796073e | 2241 | *env = strv_free(*env); |
853b8397 LP |
2242 | return 0; |
2243 | } | |
2244 | ||
035fe294 ZJS |
2245 | for (p = rvalue;; ) { |
2246 | _cleanup_free_ char *word = NULL, *k = NULL; | |
035fe294 ZJS |
2247 | |
2248 | r = extract_first_word(&p, &word, NULL, EXTRACT_CUNESCAPE|EXTRACT_QUOTES); | |
2249 | if (r == 0) | |
2250 | return 0; | |
2251 | if (r == -ENOMEM) | |
2252 | return log_oom(); | |
12ca818f | 2253 | if (r < 0) { |
035fe294 ZJS |
2254 | log_syntax(unit, LOG_WARNING, filename, line, r, |
2255 | "Invalid syntax, ignoring: %s", rvalue); | |
12ca818f LP |
2256 | return 0; |
2257 | } | |
97d0e5f8 | 2258 | |
035fe294 ZJS |
2259 | if (u) { |
2260 | r = unit_full_printf(u, word, &k); | |
2261 | if (r < 0) { | |
2262 | log_syntax(unit, LOG_ERR, filename, line, r, | |
063c4b1a | 2263 | "Failed to resolve unit specifiers in %s, ignoring: %m", word); |
035fe294 ZJS |
2264 | continue; |
2265 | } | |
ae2a15bc LP |
2266 | } else |
2267 | k = TAKE_PTR(word); | |
853b8397 | 2268 | |
035fe294 ZJS |
2269 | if (!env_assignment_is_valid(k)) { |
2270 | log_syntax(unit, LOG_ERR, filename, line, 0, | |
2271 | "Invalid environment assignment, ignoring: %s", k); | |
853b8397 LP |
2272 | continue; |
2273 | } | |
2274 | ||
54ac3494 ZJS |
2275 | r = strv_env_replace(env, k); |
2276 | if (r < 0) | |
853b8397 | 2277 | return log_oom(); |
f7f3f5c3 | 2278 | |
54ac3494 | 2279 | k = NULL; |
853b8397 | 2280 | } |
ddb26e18 LP |
2281 | } |
2282 | ||
00819cc1 LP |
2283 | int config_parse_pass_environ( |
2284 | const char *unit, | |
2285 | const char *filename, | |
2286 | unsigned line, | |
2287 | const char *section, | |
2288 | unsigned section_line, | |
2289 | const char *lvalue, | |
2290 | int ltype, | |
2291 | const char *rvalue, | |
2292 | void *data, | |
2293 | void *userdata) { | |
b4c14404 | 2294 | |
b4c14404 FB |
2295 | _cleanup_strv_free_ char **n = NULL; |
2296 | size_t nlen = 0, nbufsize = 0; | |
41de9cc2 | 2297 | char*** passenv = data; |
063c4b1a | 2298 | const char *p = rvalue; |
41de9cc2 | 2299 | Unit *u = userdata; |
b4c14404 FB |
2300 | int r; |
2301 | ||
2302 | assert(filename); | |
2303 | assert(lvalue); | |
2304 | assert(rvalue); | |
2305 | assert(data); | |
2306 | ||
2307 | if (isempty(rvalue)) { | |
2308 | /* Empty assignment resets the list */ | |
2309 | *passenv = strv_free(*passenv); | |
2310 | return 0; | |
2311 | } | |
2312 | ||
2313 | for (;;) { | |
41de9cc2 | 2314 | _cleanup_free_ char *word = NULL, *k = NULL; |
b4c14404 | 2315 | |
063c4b1a | 2316 | r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES); |
b4c14404 FB |
2317 | if (r == 0) |
2318 | break; | |
2319 | if (r == -ENOMEM) | |
2320 | return log_oom(); | |
2321 | if (r < 0) { | |
2322 | log_syntax(unit, LOG_ERR, filename, line, r, | |
063c4b1a | 2323 | "Trailing garbage in %s, ignoring: %s", lvalue, rvalue); |
b4c14404 FB |
2324 | break; |
2325 | } | |
2326 | ||
41de9cc2 LP |
2327 | if (u) { |
2328 | r = unit_full_printf(u, word, &k); | |
2329 | if (r < 0) { | |
2330 | log_syntax(unit, LOG_ERR, filename, line, r, | |
063c4b1a | 2331 | "Failed to resolve specifiers in %s, ignoring: %m", word); |
41de9cc2 LP |
2332 | continue; |
2333 | } | |
ae2a15bc LP |
2334 | } else |
2335 | k = TAKE_PTR(word); | |
41de9cc2 LP |
2336 | |
2337 | if (!env_name_is_valid(k)) { | |
2338 | log_syntax(unit, LOG_ERR, filename, line, 0, | |
2339 | "Invalid environment name for %s, ignoring: %s", lvalue, k); | |
b4c14404 FB |
2340 | continue; |
2341 | } | |
2342 | ||
2343 | if (!GREEDY_REALLOC(n, nbufsize, nlen + 2)) | |
2344 | return log_oom(); | |
41de9cc2 | 2345 | |
1cc6c93a | 2346 | n[nlen++] = TAKE_PTR(k); |
b4c14404 | 2347 | n[nlen] = NULL; |
b4c14404 FB |
2348 | } |
2349 | ||
2350 | if (n) { | |
2351 | r = strv_extend_strv(passenv, n, true); | |
2352 | if (r < 0) | |
2353 | return r; | |
2354 | } | |
2355 | ||
2356 | return 0; | |
2357 | } | |
2358 | ||
00819cc1 LP |
2359 | int config_parse_unset_environ( |
2360 | const char *unit, | |
2361 | const char *filename, | |
2362 | unsigned line, | |
2363 | const char *section, | |
2364 | unsigned section_line, | |
2365 | const char *lvalue, | |
2366 | int ltype, | |
2367 | const char *rvalue, | |
2368 | void *data, | |
2369 | void *userdata) { | |
2370 | ||
2371 | _cleanup_strv_free_ char **n = NULL; | |
00819cc1 LP |
2372 | size_t nlen = 0, nbufsize = 0; |
2373 | char*** unsetenv = data; | |
063c4b1a | 2374 | const char *p = rvalue; |
00819cc1 LP |
2375 | Unit *u = userdata; |
2376 | int r; | |
2377 | ||
2378 | assert(filename); | |
2379 | assert(lvalue); | |
2380 | assert(rvalue); | |
2381 | assert(data); | |
2382 | ||
2383 | if (isempty(rvalue)) { | |
2384 | /* Empty assignment resets the list */ | |
2385 | *unsetenv = strv_free(*unsetenv); | |
2386 | return 0; | |
2387 | } | |
2388 | ||
2389 | for (;;) { | |
2390 | _cleanup_free_ char *word = NULL, *k = NULL; | |
2391 | ||
063c4b1a | 2392 | r = extract_first_word(&p, &word, NULL, EXTRACT_CUNESCAPE|EXTRACT_QUOTES); |
00819cc1 LP |
2393 | if (r == 0) |
2394 | break; | |
2395 | if (r == -ENOMEM) | |
2396 | return log_oom(); | |
2397 | if (r < 0) { | |
2398 | log_syntax(unit, LOG_ERR, filename, line, r, | |
063c4b1a | 2399 | "Trailing garbage in %s, ignoring: %s", lvalue, rvalue); |
00819cc1 LP |
2400 | break; |
2401 | } | |
2402 | ||
2403 | if (u) { | |
2404 | r = unit_full_printf(u, word, &k); | |
2405 | if (r < 0) { | |
2406 | log_syntax(unit, LOG_ERR, filename, line, r, | |
063c4b1a | 2407 | "Failed to resolve unit specifiers in %s, ignoring: %m", word); |
00819cc1 LP |
2408 | continue; |
2409 | } | |
ae2a15bc LP |
2410 | } else |
2411 | k = TAKE_PTR(word); | |
00819cc1 LP |
2412 | |
2413 | if (!env_assignment_is_valid(k) && !env_name_is_valid(k)) { | |
2414 | log_syntax(unit, LOG_ERR, filename, line, 0, | |
2415 | "Invalid environment name or assignment %s, ignoring: %s", lvalue, k); | |
2416 | continue; | |
2417 | } | |
2418 | ||
2419 | if (!GREEDY_REALLOC(n, nbufsize, nlen + 2)) | |
2420 | return log_oom(); | |
2421 | ||
1cc6c93a | 2422 | n[nlen++] = TAKE_PTR(k); |
00819cc1 | 2423 | n[nlen] = NULL; |
00819cc1 LP |
2424 | } |
2425 | ||
2426 | if (n) { | |
2427 | r = strv_extend_strv(unsetenv, n, true); | |
2428 | if (r < 0) | |
2429 | return r; | |
2430 | } | |
2431 | ||
2432 | return 0; | |
2433 | } | |
2434 | ||
d3070fbd LP |
2435 | int config_parse_log_extra_fields( |
2436 | const char *unit, | |
2437 | const char *filename, | |
2438 | unsigned line, | |
2439 | const char *section, | |
2440 | unsigned section_line, | |
2441 | const char *lvalue, | |
2442 | int ltype, | |
2443 | const char *rvalue, | |
2444 | void *data, | |
2445 | void *userdata) { | |
2446 | ||
2447 | ExecContext *c = data; | |
2448 | Unit *u = userdata; | |
063c4b1a | 2449 | const char *p = rvalue; |
d3070fbd LP |
2450 | int r; |
2451 | ||
2452 | assert(filename); | |
2453 | assert(lvalue); | |
2454 | assert(rvalue); | |
2455 | assert(c); | |
2456 | ||
2457 | if (isempty(rvalue)) { | |
2458 | exec_context_free_log_extra_fields(c); | |
2459 | return 0; | |
2460 | } | |
2461 | ||
063c4b1a | 2462 | for (;;) { |
d3070fbd LP |
2463 | _cleanup_free_ char *word = NULL, *k = NULL; |
2464 | struct iovec *t; | |
2465 | const char *eq; | |
2466 | ||
2467 | r = extract_first_word(&p, &word, NULL, EXTRACT_CUNESCAPE|EXTRACT_QUOTES); | |
2468 | if (r == 0) | |
063c4b1a | 2469 | return 0; |
d3070fbd LP |
2470 | if (r == -ENOMEM) |
2471 | return log_oom(); | |
2472 | if (r < 0) { | |
2473 | log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid syntax, ignoring: %s", rvalue); | |
2474 | return 0; | |
2475 | } | |
2476 | ||
2477 | r = unit_full_printf(u, word, &k); | |
2478 | if (r < 0) { | |
063c4b1a | 2479 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", word); |
d3070fbd LP |
2480 | continue; |
2481 | } | |
2482 | ||
2483 | eq = strchr(k, '='); | |
2484 | if (!eq) { | |
063c4b1a | 2485 | log_syntax(unit, LOG_ERR, filename, line, 0, "Log field lacks '=' character, ignoring: %s", k); |
d3070fbd LP |
2486 | continue; |
2487 | } | |
2488 | ||
2489 | if (!journal_field_valid(k, eq-k, false)) { | |
063c4b1a | 2490 | log_syntax(unit, LOG_ERR, filename, line, 0, "Log field name is invalid, ignoring: %s", k); |
d3070fbd LP |
2491 | continue; |
2492 | } | |
2493 | ||
aa484f35 | 2494 | t = reallocarray(c->log_extra_fields, c->n_log_extra_fields+1, sizeof(struct iovec)); |
d3070fbd LP |
2495 | if (!t) |
2496 | return log_oom(); | |
2497 | ||
2498 | c->log_extra_fields = t; | |
2499 | c->log_extra_fields[c->n_log_extra_fields++] = IOVEC_MAKE_STRING(k); | |
2500 | ||
2501 | k = NULL; | |
2502 | } | |
d3070fbd LP |
2503 | } |
2504 | ||
e8e581bf ZJS |
2505 | int config_parse_ip_tos(const char *unit, |
2506 | const char *filename, | |
2507 | unsigned line, | |
2508 | const char *section, | |
71a61510 | 2509 | unsigned section_line, |
e8e581bf ZJS |
2510 | const char *lvalue, |
2511 | int ltype, | |
2512 | const char *rvalue, | |
2513 | void *data, | |
2514 | void *userdata) { | |
4fd5948e LP |
2515 | |
2516 | int *ip_tos = data, x; | |
4fd5948e LP |
2517 | |
2518 | assert(filename); | |
2519 | assert(lvalue); | |
2520 | assert(rvalue); | |
2521 | assert(data); | |
2522 | ||
f8b69d1d MS |
2523 | x = ip_tos_from_string(rvalue); |
2524 | if (x < 0) { | |
12ca818f | 2525 | log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse IP TOS value, ignoring: %s", rvalue); |
f8b69d1d MS |
2526 | return 0; |
2527 | } | |
4fd5948e LP |
2528 | |
2529 | *ip_tos = x; | |
2530 | return 0; | |
2531 | } | |
2532 | ||
59fccdc5 LP |
2533 | int config_parse_unit_condition_path( |
2534 | const char *unit, | |
2535 | const char *filename, | |
2536 | unsigned line, | |
2537 | const char *section, | |
2538 | unsigned section_line, | |
2539 | const char *lvalue, | |
2540 | int ltype, | |
2541 | const char *rvalue, | |
2542 | void *data, | |
2543 | void *userdata) { | |
52661efd | 2544 | |
2fbe635a | 2545 | _cleanup_free_ char *p = NULL; |
59fccdc5 LP |
2546 | Condition **list = data, *c; |
2547 | ConditionType t = ltype; | |
2548 | bool trigger, negate; | |
2549 | Unit *u = userdata; | |
19f6d710 | 2550 | int r; |
52661efd LP |
2551 | |
2552 | assert(filename); | |
2553 | assert(lvalue); | |
2554 | assert(rvalue); | |
2555 | assert(data); | |
2556 | ||
74051b9b LP |
2557 | if (isempty(rvalue)) { |
2558 | /* Empty assignment resets the list */ | |
447021aa | 2559 | *list = condition_free_list(*list); |
74051b9b LP |
2560 | return 0; |
2561 | } | |
2562 | ||
ab7f148f LP |
2563 | trigger = rvalue[0] == '|'; |
2564 | if (trigger) | |
267632f0 LP |
2565 | rvalue++; |
2566 | ||
ab7f148f LP |
2567 | negate = rvalue[0] == '!'; |
2568 | if (negate) | |
52661efd LP |
2569 | rvalue++; |
2570 | ||
19f6d710 | 2571 | r = unit_full_printf(u, rvalue, &p); |
59fccdc5 | 2572 | if (r < 0) { |
063c4b1a | 2573 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue); |
59fccdc5 | 2574 | return 0; |
19f6d710 | 2575 | } |
095b2d7a AK |
2576 | |
2577 | if (!path_is_absolute(p)) { | |
12ca818f | 2578 | log_syntax(unit, LOG_ERR, filename, line, 0, "Path in condition not absolute, ignoring: %s", p); |
52661efd LP |
2579 | return 0; |
2580 | } | |
2581 | ||
59fccdc5 | 2582 | c = condition_new(t, p, trigger, negate); |
ab7f148f | 2583 | if (!c) |
74051b9b | 2584 | return log_oom(); |
52661efd | 2585 | |
59fccdc5 | 2586 | LIST_PREPEND(conditions, *list, c); |
52661efd LP |
2587 | return 0; |
2588 | } | |
2589 | ||
59fccdc5 LP |
2590 | int config_parse_unit_condition_string( |
2591 | const char *unit, | |
2592 | const char *filename, | |
2593 | unsigned line, | |
2594 | const char *section, | |
2595 | unsigned section_line, | |
2596 | const char *lvalue, | |
2597 | int ltype, | |
2598 | const char *rvalue, | |
2599 | void *data, | |
2600 | void *userdata) { | |
039655a4 | 2601 | |
2fbe635a | 2602 | _cleanup_free_ char *s = NULL; |
59fccdc5 LP |
2603 | Condition **list = data, *c; |
2604 | ConditionType t = ltype; | |
2605 | bool trigger, negate; | |
2606 | Unit *u = userdata; | |
19f6d710 | 2607 | int r; |
039655a4 LP |
2608 | |
2609 | assert(filename); | |
2610 | assert(lvalue); | |
2611 | assert(rvalue); | |
2612 | assert(data); | |
2613 | ||
74051b9b LP |
2614 | if (isempty(rvalue)) { |
2615 | /* Empty assignment resets the list */ | |
447021aa | 2616 | *list = condition_free_list(*list); |
74051b9b LP |
2617 | return 0; |
2618 | } | |
2619 | ||
c0d6e764 LP |
2620 | trigger = rvalue[0] == '|'; |
2621 | if (trigger) | |
267632f0 LP |
2622 | rvalue++; |
2623 | ||
c0d6e764 LP |
2624 | negate = rvalue[0] == '!'; |
2625 | if (negate) | |
039655a4 LP |
2626 | rvalue++; |
2627 | ||
19f6d710 | 2628 | r = unit_full_printf(u, rvalue, &s); |
59fccdc5 | 2629 | if (r < 0) { |
063c4b1a | 2630 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", rvalue); |
59fccdc5 | 2631 | return 0; |
19f6d710 | 2632 | } |
095b2d7a | 2633 | |
59fccdc5 | 2634 | c = condition_new(t, s, trigger, negate); |
c0d6e764 LP |
2635 | if (!c) |
2636 | return log_oom(); | |
039655a4 | 2637 | |
59fccdc5 | 2638 | LIST_PREPEND(conditions, *list, c); |
039655a4 LP |
2639 | return 0; |
2640 | } | |
2641 | ||
59fccdc5 LP |
2642 | int config_parse_unit_condition_null( |
2643 | const char *unit, | |
2644 | const char *filename, | |
2645 | unsigned line, | |
2646 | const char *section, | |
2647 | unsigned section_line, | |
2648 | const char *lvalue, | |
2649 | int ltype, | |
2650 | const char *rvalue, | |
2651 | void *data, | |
2652 | void *userdata) { | |
d257ddef | 2653 | |
59fccdc5 | 2654 | Condition **list = data, *c; |
267632f0 | 2655 | bool trigger, negate; |
d257ddef LP |
2656 | int b; |
2657 | ||
2658 | assert(filename); | |
2659 | assert(lvalue); | |
2660 | assert(rvalue); | |
2661 | assert(data); | |
2662 | ||
74051b9b LP |
2663 | if (isempty(rvalue)) { |
2664 | /* Empty assignment resets the list */ | |
447021aa | 2665 | *list = condition_free_list(*list); |
74051b9b LP |
2666 | return 0; |
2667 | } | |
2668 | ||
2669 | trigger = rvalue[0] == '|'; | |
2670 | if (trigger) | |
267632f0 LP |
2671 | rvalue++; |
2672 | ||
74051b9b LP |
2673 | negate = rvalue[0] == '!'; |
2674 | if (negate) | |
d257ddef LP |
2675 | rvalue++; |
2676 | ||
74051b9b LP |
2677 | b = parse_boolean(rvalue); |
2678 | if (b < 0) { | |
12ca818f | 2679 | log_syntax(unit, LOG_ERR, filename, line, b, "Failed to parse boolean value in condition, ignoring: %s", rvalue); |
d257ddef LP |
2680 | return 0; |
2681 | } | |
2682 | ||
2683 | if (!b) | |
2684 | negate = !negate; | |
2685 | ||
74051b9b LP |
2686 | c = condition_new(CONDITION_NULL, NULL, trigger, negate); |
2687 | if (!c) | |
2688 | return log_oom(); | |
d257ddef | 2689 | |
59fccdc5 | 2690 | LIST_PREPEND(conditions, *list, c); |
d257ddef LP |
2691 | return 0; |
2692 | } | |
2693 | ||
f975e971 | 2694 | DEFINE_CONFIG_PARSE_ENUM(config_parse_notify_access, notify_access, NotifyAccess, "Failed to parse notify access specifier"); |
87a47f99 | 2695 | DEFINE_CONFIG_PARSE_ENUM(config_parse_emergency_action, emergency_action, EmergencyAction, "Failed to parse failure action specifier"); |
c952c6ec | 2696 | |
a57f7e2c LP |
2697 | int config_parse_unit_requires_mounts_for( |
2698 | const char *unit, | |
2699 | const char *filename, | |
2700 | unsigned line, | |
2701 | const char *section, | |
71a61510 | 2702 | unsigned section_line, |
a57f7e2c LP |
2703 | const char *lvalue, |
2704 | int ltype, | |
2705 | const char *rvalue, | |
2706 | void *data, | |
2707 | void *userdata) { | |
7c8fa05c | 2708 | |
063c4b1a | 2709 | const char *p = rvalue; |
7c8fa05c | 2710 | Unit *u = userdata; |
035fe294 | 2711 | int r; |
7c8fa05c LP |
2712 | |
2713 | assert(filename); | |
2714 | assert(lvalue); | |
2715 | assert(rvalue); | |
2716 | assert(data); | |
2717 | ||
063c4b1a | 2718 | for (;;) { |
744bb5b1 | 2719 | _cleanup_free_ char *word = NULL, *resolved = NULL; |
a57f7e2c | 2720 | |
035fe294 ZJS |
2721 | r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES); |
2722 | if (r == 0) | |
2723 | return 0; | |
2724 | if (r == -ENOMEM) | |
a57f7e2c | 2725 | return log_oom(); |
035fe294 ZJS |
2726 | if (r < 0) { |
2727 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
2728 | "Invalid syntax, ignoring: %s", rvalue); | |
2729 | return 0; | |
2730 | } | |
7c8fa05c | 2731 | |
035fe294 | 2732 | if (!utf8_is_valid(word)) { |
0e05ee04 | 2733 | log_syntax_invalid_utf8(unit, LOG_ERR, filename, line, rvalue); |
a57f7e2c LP |
2734 | continue; |
2735 | } | |
7c8fa05c | 2736 | |
744bb5b1 LP |
2737 | r = unit_full_printf(u, word, &resolved); |
2738 | if (r < 0) { | |
063c4b1a | 2739 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in '%s', ignoring: %m", word); |
744bb5b1 LP |
2740 | continue; |
2741 | } | |
2742 | ||
eef85c4a | 2743 | r = unit_require_mounts_for(u, resolved, UNIT_DEPENDENCY_FILE); |
a57f7e2c | 2744 | if (r < 0) { |
063c4b1a | 2745 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to add required mount '%s', ignoring: %m", resolved); |
a57f7e2c LP |
2746 | continue; |
2747 | } | |
2748 | } | |
7c8fa05c | 2749 | } |
9e372868 | 2750 | |
e8e581bf ZJS |
2751 | int config_parse_documentation(const char *unit, |
2752 | const char *filename, | |
2753 | unsigned line, | |
2754 | const char *section, | |
71a61510 | 2755 | unsigned section_line, |
e8e581bf ZJS |
2756 | const char *lvalue, |
2757 | int ltype, | |
2758 | const char *rvalue, | |
2759 | void *data, | |
2760 | void *userdata) { | |
49dbfa7b LP |
2761 | |
2762 | Unit *u = userdata; | |
2763 | int r; | |
2764 | char **a, **b; | |
2765 | ||
2766 | assert(filename); | |
2767 | assert(lvalue); | |
2768 | assert(rvalue); | |
2769 | assert(u); | |
2770 | ||
74051b9b LP |
2771 | if (isempty(rvalue)) { |
2772 | /* Empty assignment resets the list */ | |
6796073e | 2773 | u->documentation = strv_free(u->documentation); |
74051b9b LP |
2774 | return 0; |
2775 | } | |
2776 | ||
71a61510 | 2777 | r = config_parse_unit_strv_printf(unit, filename, line, section, section_line, lvalue, ltype, |
e8e581bf | 2778 | rvalue, data, userdata); |
49dbfa7b LP |
2779 | if (r < 0) |
2780 | return r; | |
2781 | ||
2782 | for (a = b = u->documentation; a && *a; a++) { | |
2783 | ||
a2e03378 | 2784 | if (documentation_url_is_valid(*a)) |
49dbfa7b LP |
2785 | *(b++) = *a; |
2786 | else { | |
12ca818f | 2787 | log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid URL, ignoring: %s", *a); |
49dbfa7b LP |
2788 | free(*a); |
2789 | } | |
2790 | } | |
f6d2d421 ZJS |
2791 | if (b) |
2792 | *b = NULL; | |
49dbfa7b LP |
2793 | |
2794 | return r; | |
2795 | } | |
2796 | ||
349cc4a5 | 2797 | #if HAVE_SECCOMP |
17df7223 LP |
2798 | int config_parse_syscall_filter( |
2799 | const char *unit, | |
2800 | const char *filename, | |
2801 | unsigned line, | |
2802 | const char *section, | |
2803 | unsigned section_line, | |
2804 | const char *lvalue, | |
2805 | int ltype, | |
2806 | const char *rvalue, | |
2807 | void *data, | |
2808 | void *userdata) { | |
2809 | ||
8351ceae LP |
2810 | ExecContext *c = data; |
2811 | Unit *u = userdata; | |
b5fb3789 | 2812 | bool invert = false; |
8130926d | 2813 | const char *p; |
17df7223 | 2814 | int r; |
8351ceae LP |
2815 | |
2816 | assert(filename); | |
2817 | assert(lvalue); | |
2818 | assert(rvalue); | |
2819 | assert(u); | |
2820 | ||
74051b9b LP |
2821 | if (isempty(rvalue)) { |
2822 | /* Empty assignment resets the list */ | |
8cfa775f | 2823 | c->syscall_filter = hashmap_free(c->syscall_filter); |
17df7223 | 2824 | c->syscall_whitelist = false; |
74051b9b LP |
2825 | return 0; |
2826 | } | |
2827 | ||
8351ceae LP |
2828 | if (rvalue[0] == '~') { |
2829 | invert = true; | |
2830 | rvalue++; | |
2831 | } | |
2832 | ||
17df7223 | 2833 | if (!c->syscall_filter) { |
8cfa775f | 2834 | c->syscall_filter = hashmap_new(NULL); |
17df7223 LP |
2835 | if (!c->syscall_filter) |
2836 | return log_oom(); | |
2837 | ||
c0467cf3 | 2838 | if (invert) |
17df7223 LP |
2839 | /* Allow everything but the ones listed */ |
2840 | c->syscall_whitelist = false; | |
c0467cf3 | 2841 | else { |
17df7223 LP |
2842 | /* Allow nothing but the ones listed */ |
2843 | c->syscall_whitelist = true; | |
8351ceae | 2844 | |
17df7223 | 2845 | /* Accept default syscalls if we are on a whitelist */ |
13d92c63 | 2846 | r = seccomp_parse_syscall_filter("@default", -1, c->syscall_filter, SECCOMP_PARSE_WHITELIST); |
201c1cc2 TM |
2847 | if (r < 0) |
2848 | return r; | |
c0467cf3 | 2849 | } |
8351ceae LP |
2850 | } |
2851 | ||
8130926d LP |
2852 | p = rvalue; |
2853 | for (;;) { | |
8cfa775f YW |
2854 | _cleanup_free_ char *word = NULL, *name = NULL; |
2855 | int num; | |
8351ceae | 2856 | |
8130926d LP |
2857 | r = extract_first_word(&p, &word, NULL, 0); |
2858 | if (r == 0) | |
063c4b1a | 2859 | return 0; |
8130926d | 2860 | if (r == -ENOMEM) |
74051b9b | 2861 | return log_oom(); |
8130926d LP |
2862 | if (r < 0) { |
2863 | log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid syntax, ignoring: %s", rvalue); | |
063c4b1a | 2864 | return 0; |
8130926d | 2865 | } |
8351ceae | 2866 | |
8cfa775f YW |
2867 | r = parse_syscall_and_errno(word, &name, &num); |
2868 | if (r < 0) { | |
2869 | log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse syscall:errno, ignoring: %s", word); | |
2870 | continue; | |
2871 | } | |
2872 | ||
13d92c63 LP |
2873 | r = seccomp_parse_syscall_filter_full(name, num, c->syscall_filter, |
2874 | SECCOMP_PARSE_LOG|SECCOMP_PARSE_PERMISSIVE|(invert ? SECCOMP_PARSE_INVERT : 0)|(c->syscall_whitelist ? SECCOMP_PARSE_WHITELIST : 0), | |
2875 | unit, filename, line); | |
201c1cc2 TM |
2876 | if (r < 0) |
2877 | return r; | |
c0467cf3 | 2878 | } |
17df7223 LP |
2879 | } |
2880 | ||
57183d11 LP |
2881 | int config_parse_syscall_archs( |
2882 | const char *unit, | |
2883 | const char *filename, | |
2884 | unsigned line, | |
2885 | const char *section, | |
2886 | unsigned section_line, | |
2887 | const char *lvalue, | |
2888 | int ltype, | |
2889 | const char *rvalue, | |
2890 | void *data, | |
2891 | void *userdata) { | |
2892 | ||
063c4b1a | 2893 | const char *p = rvalue; |
d3b1c508 | 2894 | Set **archs = data; |
57183d11 LP |
2895 | int r; |
2896 | ||
2897 | if (isempty(rvalue)) { | |
525d3cc7 | 2898 | *archs = set_free(*archs); |
57183d11 LP |
2899 | return 0; |
2900 | } | |
2901 | ||
d5099efc | 2902 | r = set_ensure_allocated(archs, NULL); |
57183d11 LP |
2903 | if (r < 0) |
2904 | return log_oom(); | |
2905 | ||
063c4b1a | 2906 | for (;;) { |
035fe294 | 2907 | _cleanup_free_ char *word = NULL; |
57183d11 LP |
2908 | uint32_t a; |
2909 | ||
035fe294 ZJS |
2910 | r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES); |
2911 | if (r == 0) | |
2912 | return 0; | |
2913 | if (r == -ENOMEM) | |
57183d11 | 2914 | return log_oom(); |
035fe294 ZJS |
2915 | if (r < 0) { |
2916 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
2917 | "Invalid syntax, ignoring: %s", rvalue); | |
2918 | return 0; | |
2919 | } | |
57183d11 | 2920 | |
035fe294 | 2921 | r = seccomp_arch_from_string(word, &a); |
57183d11 | 2922 | if (r < 0) { |
035fe294 ZJS |
2923 | log_syntax(unit, LOG_ERR, filename, line, r, |
2924 | "Failed to parse system call architecture \"%s\", ignoring: %m", word); | |
57183d11 LP |
2925 | continue; |
2926 | } | |
2927 | ||
d3b1c508 | 2928 | r = set_put(*archs, UINT32_TO_PTR(a + 1)); |
57183d11 LP |
2929 | if (r < 0) |
2930 | return log_oom(); | |
2931 | } | |
57183d11 LP |
2932 | } |
2933 | ||
17df7223 LP |
2934 | int config_parse_syscall_errno( |
2935 | const char *unit, | |
2936 | const char *filename, | |
2937 | unsigned line, | |
2938 | const char *section, | |
2939 | unsigned section_line, | |
2940 | const char *lvalue, | |
2941 | int ltype, | |
2942 | const char *rvalue, | |
2943 | void *data, | |
2944 | void *userdata) { | |
2945 | ||
2946 | ExecContext *c = data; | |
2947 | int e; | |
2948 | ||
2949 | assert(filename); | |
2950 | assert(lvalue); | |
2951 | assert(rvalue); | |
2952 | ||
2953 | if (isempty(rvalue)) { | |
2954 | /* Empty assignment resets to KILL */ | |
2955 | c->syscall_errno = 0; | |
2956 | return 0; | |
8351ceae LP |
2957 | } |
2958 | ||
3df90f24 YW |
2959 | e = parse_errno(rvalue); |
2960 | if (e <= 0) { | |
12ca818f | 2961 | log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse error number, ignoring: %s", rvalue); |
17df7223 LP |
2962 | return 0; |
2963 | } | |
8351ceae | 2964 | |
17df7223 | 2965 | c->syscall_errno = e; |
8351ceae LP |
2966 | return 0; |
2967 | } | |
4298d0b5 LP |
2968 | |
2969 | int config_parse_address_families( | |
2970 | const char *unit, | |
2971 | const char *filename, | |
2972 | unsigned line, | |
2973 | const char *section, | |
2974 | unsigned section_line, | |
2975 | const char *lvalue, | |
2976 | int ltype, | |
2977 | const char *rvalue, | |
2978 | void *data, | |
2979 | void *userdata) { | |
2980 | ||
2981 | ExecContext *c = data; | |
4298d0b5 | 2982 | bool invert = false; |
035fe294 | 2983 | const char *p; |
4298d0b5 LP |
2984 | int r; |
2985 | ||
2986 | assert(filename); | |
2987 | assert(lvalue); | |
2988 | assert(rvalue); | |
4298d0b5 LP |
2989 | |
2990 | if (isempty(rvalue)) { | |
2991 | /* Empty assignment resets the list */ | |
525d3cc7 | 2992 | c->address_families = set_free(c->address_families); |
4298d0b5 LP |
2993 | c->address_families_whitelist = false; |
2994 | return 0; | |
2995 | } | |
2996 | ||
2997 | if (rvalue[0] == '~') { | |
2998 | invert = true; | |
2999 | rvalue++; | |
3000 | } | |
3001 | ||
3002 | if (!c->address_families) { | |
d5099efc | 3003 | c->address_families = set_new(NULL); |
4298d0b5 LP |
3004 | if (!c->address_families) |
3005 | return log_oom(); | |
3006 | ||
3007 | c->address_families_whitelist = !invert; | |
3008 | } | |
3009 | ||
035fe294 ZJS |
3010 | for (p = rvalue;;) { |
3011 | _cleanup_free_ char *word = NULL; | |
4298d0b5 LP |
3012 | int af; |
3013 | ||
035fe294 ZJS |
3014 | r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES); |
3015 | if (r == 0) | |
3016 | return 0; | |
3017 | if (r == -ENOMEM) | |
4298d0b5 | 3018 | return log_oom(); |
035fe294 ZJS |
3019 | if (r < 0) { |
3020 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
3021 | "Invalid syntax, ignoring: %s", rvalue); | |
3022 | return 0; | |
3023 | } | |
4298d0b5 | 3024 | |
035fe294 | 3025 | af = af_from_name(word); |
063c4b1a | 3026 | if (af <= 0) { |
035fe294 | 3027 | log_syntax(unit, LOG_ERR, filename, line, 0, |
063c4b1a | 3028 | "Failed to parse address family, ignoring: %s", word); |
4298d0b5 LP |
3029 | continue; |
3030 | } | |
3031 | ||
3032 | /* If we previously wanted to forbid an address family and now | |
035fe294 | 3033 | * we want to allow it, then just remove it from the list. |
4298d0b5 LP |
3034 | */ |
3035 | if (!invert == c->address_families_whitelist) { | |
3036 | r = set_put(c->address_families, INT_TO_PTR(af)); | |
4298d0b5 LP |
3037 | if (r < 0) |
3038 | return log_oom(); | |
3039 | } else | |
3040 | set_remove(c->address_families, INT_TO_PTR(af)); | |
3041 | } | |
4298d0b5 | 3042 | } |
add00535 LP |
3043 | |
3044 | int config_parse_restrict_namespaces( | |
3045 | const char *unit, | |
3046 | const char *filename, | |
3047 | unsigned line, | |
3048 | const char *section, | |
3049 | unsigned section_line, | |
3050 | const char *lvalue, | |
3051 | int ltype, | |
3052 | const char *rvalue, | |
3053 | void *data, | |
3054 | void *userdata) { | |
3055 | ||
3056 | ExecContext *c = data; | |
aa9d574d | 3057 | unsigned long flags; |
add00535 LP |
3058 | bool invert = false; |
3059 | int r; | |
3060 | ||
3061 | if (isempty(rvalue)) { | |
3062 | /* Reset to the default. */ | |
aa9d574d YW |
3063 | c->restrict_namespaces = NAMESPACE_FLAGS_INITIAL; |
3064 | return 0; | |
3065 | } | |
3066 | ||
3067 | /* Boolean parameter ignores the previous settings */ | |
3068 | r = parse_boolean(rvalue); | |
3069 | if (r > 0) { | |
3070 | c->restrict_namespaces = 0; | |
3071 | return 0; | |
3072 | } else if (r == 0) { | |
add00535 LP |
3073 | c->restrict_namespaces = NAMESPACE_FLAGS_ALL; |
3074 | return 0; | |
3075 | } | |
3076 | ||
3077 | if (rvalue[0] == '~') { | |
3078 | invert = true; | |
3079 | rvalue++; | |
3080 | } | |
3081 | ||
aa9d574d YW |
3082 | /* Not a boolean argument, in this case it's a list of namespace types. */ |
3083 | r = namespace_flags_from_string(rvalue, &flags); | |
3084 | if (r < 0) { | |
3085 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse namespace type string, ignoring: %s", rvalue); | |
3086 | return 0; | |
add00535 LP |
3087 | } |
3088 | ||
aa9d574d YW |
3089 | if (c->restrict_namespaces == NAMESPACE_FLAGS_INITIAL) |
3090 | /* Initial assignment. Just set the value. */ | |
3091 | c->restrict_namespaces = invert ? (~flags) & NAMESPACE_FLAGS_ALL : flags; | |
3092 | else | |
3093 | /* Merge the value with the previous one. */ | |
3094 | SET_FLAG(c->restrict_namespaces, flags, !invert); | |
add00535 LP |
3095 | |
3096 | return 0; | |
3097 | } | |
c0467cf3 | 3098 | #endif |
8351ceae | 3099 | |
a016b922 LP |
3100 | int config_parse_unit_slice( |
3101 | const char *unit, | |
3102 | const char *filename, | |
3103 | unsigned line, | |
3104 | const char *section, | |
71a61510 | 3105 | unsigned section_line, |
a016b922 LP |
3106 | const char *lvalue, |
3107 | int ltype, | |
3108 | const char *rvalue, | |
3109 | void *data, | |
3110 | void *userdata) { | |
3111 | ||
063c4b1a | 3112 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
a016b922 | 3113 | _cleanup_free_ char *k = NULL; |
d79200e2 | 3114 | Unit *u = userdata, *slice = NULL; |
a016b922 LP |
3115 | int r; |
3116 | ||
3117 | assert(filename); | |
3118 | assert(lvalue); | |
3119 | assert(rvalue); | |
3120 | assert(u); | |
3121 | ||
19f6d710 | 3122 | r = unit_name_printf(u, rvalue, &k); |
d79200e2 | 3123 | if (r < 0) { |
063c4b1a | 3124 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", rvalue); |
d79200e2 | 3125 | return 0; |
19f6d710 | 3126 | } |
a016b922 | 3127 | |
063c4b1a | 3128 | r = manager_load_unit(u->manager, k, NULL, &error, &slice); |
a016b922 | 3129 | if (r < 0) { |
063c4b1a | 3130 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to load slice unit %s, ignoring: %s", k, bus_error_message(&error, r)); |
a016b922 LP |
3131 | return 0; |
3132 | } | |
3133 | ||
d79200e2 LP |
3134 | r = unit_set_slice(u, slice); |
3135 | if (r < 0) { | |
063c4b1a | 3136 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to assign slice %s to unit %s, ignoring: %m", slice->id, u->id); |
a016b922 LP |
3137 | return 0; |
3138 | } | |
3139 | ||
a016b922 LP |
3140 | return 0; |
3141 | } | |
3142 | ||
4ad49000 LP |
3143 | DEFINE_CONFIG_PARSE_ENUM(config_parse_device_policy, cgroup_device_policy, CGroupDevicePolicy, "Failed to parse device policy"); |
3144 | ||
66ebf6c0 TH |
3145 | int config_parse_cpu_weight( |
3146 | const char *unit, | |
3147 | const char *filename, | |
3148 | unsigned line, | |
3149 | const char *section, | |
3150 | unsigned section_line, | |
3151 | const char *lvalue, | |
3152 | int ltype, | |
3153 | const char *rvalue, | |
3154 | void *data, | |
3155 | void *userdata) { | |
3156 | ||
3157 | uint64_t *weight = data; | |
3158 | int r; | |
3159 | ||
3160 | assert(filename); | |
3161 | assert(lvalue); | |
3162 | assert(rvalue); | |
3163 | ||
3164 | r = cg_weight_parse(rvalue, weight); | |
3165 | if (r < 0) { | |
063c4b1a | 3166 | log_syntax(unit, LOG_ERR, filename, line, r, "Invalid CPU weight '%s', ignoring: %m", rvalue); |
66ebf6c0 TH |
3167 | return 0; |
3168 | } | |
3169 | ||
3170 | return 0; | |
3171 | } | |
3172 | ||
4ad49000 LP |
3173 | int config_parse_cpu_shares( |
3174 | const char *unit, | |
3175 | const char *filename, | |
3176 | unsigned line, | |
3177 | const char *section, | |
71a61510 | 3178 | unsigned section_line, |
4ad49000 LP |
3179 | const char *lvalue, |
3180 | int ltype, | |
3181 | const char *rvalue, | |
3182 | void *data, | |
3183 | void *userdata) { | |
3184 | ||
d53d9474 | 3185 | uint64_t *shares = data; |
95ae05c0 WC |
3186 | int r; |
3187 | ||
3188 | assert(filename); | |
3189 | assert(lvalue); | |
3190 | assert(rvalue); | |
3191 | ||
d53d9474 LP |
3192 | r = cg_cpu_shares_parse(rvalue, shares); |
3193 | if (r < 0) { | |
063c4b1a | 3194 | log_syntax(unit, LOG_ERR, filename, line, r, "Invalid CPU shares '%s', ignoring: %m", rvalue); |
95ae05c0 WC |
3195 | return 0; |
3196 | } | |
3197 | ||
4ad49000 LP |
3198 | return 0; |
3199 | } | |
3200 | ||
b2f8b02e LP |
3201 | int config_parse_cpu_quota( |
3202 | const char *unit, | |
3203 | const char *filename, | |
3204 | unsigned line, | |
3205 | const char *section, | |
3206 | unsigned section_line, | |
3207 | const char *lvalue, | |
3208 | int ltype, | |
3209 | const char *rvalue, | |
3210 | void *data, | |
3211 | void *userdata) { | |
3212 | ||
3213 | CGroupContext *c = data; | |
9184ca48 | 3214 | int r; |
b2f8b02e LP |
3215 | |
3216 | assert(filename); | |
3217 | assert(lvalue); | |
3218 | assert(rvalue); | |
3219 | ||
3220 | if (isempty(rvalue)) { | |
3a43da28 | 3221 | c->cpu_quota_per_sec_usec = USEC_INFINITY; |
b2f8b02e LP |
3222 | return 0; |
3223 | } | |
3224 | ||
5124866d | 3225 | r = parse_percent_unbounded(rvalue); |
9184ca48 | 3226 | if (r <= 0) { |
063c4b1a | 3227 | log_syntax(unit, LOG_ERR, filename, line, r, "Invalid CPU quota '%s', ignoring.", rvalue); |
9a054909 | 3228 | return 0; |
b2f8b02e LP |
3229 | } |
3230 | ||
9184ca48 | 3231 | c->cpu_quota_per_sec_usec = ((usec_t) r * USEC_PER_SEC) / 100U; |
b2f8b02e LP |
3232 | return 0; |
3233 | } | |
3234 | ||
4ad49000 LP |
3235 | int config_parse_memory_limit( |
3236 | const char *unit, | |
3237 | const char *filename, | |
3238 | unsigned line, | |
3239 | const char *section, | |
71a61510 | 3240 | unsigned section_line, |
4ad49000 LP |
3241 | const char *lvalue, |
3242 | int ltype, | |
3243 | const char *rvalue, | |
3244 | void *data, | |
3245 | void *userdata) { | |
3246 | ||
3247 | CGroupContext *c = data; | |
da4d897e | 3248 | uint64_t bytes = CGROUP_LIMIT_MAX; |
4ad49000 LP |
3249 | int r; |
3250 | ||
e57c9ce1 | 3251 | if (!isempty(rvalue) && !streq(rvalue, "infinity")) { |
875ae566 LP |
3252 | |
3253 | r = parse_percent(rvalue); | |
3254 | if (r < 0) { | |
3255 | r = parse_size(rvalue, 1024, &bytes); | |
3256 | if (r < 0) { | |
063c4b1a | 3257 | log_syntax(unit, LOG_ERR, filename, line, r, "Invalid memory limit '%s', ignoring: %m", rvalue); |
875ae566 LP |
3258 | return 0; |
3259 | } | |
3260 | } else | |
d8cf2ac7 | 3261 | bytes = physical_memory_scale(r, 100U); |
875ae566 | 3262 | |
906bdbf5 YW |
3263 | if (bytes >= UINT64_MAX || |
3264 | (bytes <= 0 && !streq(lvalue, "MemorySwapMax"))) { | |
063c4b1a | 3265 | log_syntax(unit, LOG_ERR, filename, line, 0, "Memory limit '%s' out of range, ignoring.", rvalue); |
da4d897e TH |
3266 | return 0; |
3267 | } | |
4ad49000 LP |
3268 | } |
3269 | ||
da4d897e TH |
3270 | if (streq(lvalue, "MemoryLow")) |
3271 | c->memory_low = bytes; | |
3272 | else if (streq(lvalue, "MemoryHigh")) | |
3273 | c->memory_high = bytes; | |
3274 | else if (streq(lvalue, "MemoryMax")) | |
3275 | c->memory_max = bytes; | |
96e131ea WC |
3276 | else if (streq(lvalue, "MemorySwapMax")) |
3277 | c->memory_swap_max = bytes; | |
3278 | else if (streq(lvalue, "MemoryLimit")) | |
da4d897e | 3279 | c->memory_limit = bytes; |
96e131ea WC |
3280 | else |
3281 | return -EINVAL; | |
4ad49000 | 3282 | |
4ad49000 LP |
3283 | return 0; |
3284 | } | |
3285 | ||
03a7b521 LP |
3286 | int config_parse_tasks_max( |
3287 | const char *unit, | |
3288 | const char *filename, | |
3289 | unsigned line, | |
3290 | const char *section, | |
3291 | unsigned section_line, | |
3292 | const char *lvalue, | |
3293 | int ltype, | |
3294 | const char *rvalue, | |
3295 | void *data, | |
3296 | void *userdata) { | |
3297 | ||
f5058264 TH |
3298 | uint64_t *tasks_max = data, v; |
3299 | Unit *u = userdata; | |
03a7b521 LP |
3300 | int r; |
3301 | ||
f5058264 TH |
3302 | if (isempty(rvalue)) { |
3303 | *tasks_max = u->manager->default_tasks_max; | |
3304 | return 0; | |
3305 | } | |
3306 | ||
3307 | if (streq(rvalue, "infinity")) { | |
3308 | *tasks_max = CGROUP_LIMIT_MAX; | |
03a7b521 LP |
3309 | return 0; |
3310 | } | |
3311 | ||
83f8e808 LP |
3312 | r = parse_percent(rvalue); |
3313 | if (r < 0) { | |
f5058264 | 3314 | r = safe_atou64(rvalue, &v); |
83f8e808 | 3315 | if (r < 0) { |
063c4b1a | 3316 | log_syntax(unit, LOG_ERR, filename, line, r, "Invalid maximum tasks value '%s', ignoring: %m", rvalue); |
83f8e808 LP |
3317 | return 0; |
3318 | } | |
3319 | } else | |
f5058264 | 3320 | v = system_tasks_max_scale(r, 100U); |
83f8e808 | 3321 | |
f5058264 | 3322 | if (v <= 0 || v >= UINT64_MAX) { |
063c4b1a | 3323 | log_syntax(unit, LOG_ERR, filename, line, 0, "Maximum tasks value '%s' out of range, ignoring.", rvalue); |
03a7b521 LP |
3324 | return 0; |
3325 | } | |
3326 | ||
f5058264 | 3327 | *tasks_max = v; |
03a7b521 LP |
3328 | return 0; |
3329 | } | |
3330 | ||
02638280 LP |
3331 | int config_parse_delegate( |
3332 | const char *unit, | |
3333 | const char *filename, | |
3334 | unsigned line, | |
3335 | const char *section, | |
3336 | unsigned section_line, | |
3337 | const char *lvalue, | |
3338 | int ltype, | |
3339 | const char *rvalue, | |
3340 | void *data, | |
3341 | void *userdata) { | |
3342 | ||
3343 | CGroupContext *c = data; | |
ecae73d7 | 3344 | UnitType t; |
02638280 LP |
3345 | int r; |
3346 | ||
ecae73d7 ZJS |
3347 | t = unit_name_to_type(unit); |
3348 | assert(t != _UNIT_TYPE_INVALID); | |
3349 | ||
3350 | if (!unit_vtable[t]->can_delegate) { | |
3351 | log_syntax(unit, LOG_ERR, filename, line, 0, "Delegate= setting not supported for this unit type, ignoring."); | |
3352 | return 0; | |
3353 | } | |
3354 | ||
02638280 LP |
3355 | /* We either accept a boolean value, which may be used to turn on delegation for all controllers, or turn it |
3356 | * off for all. Or it takes a list of controller names, in which case we add the specified controllers to the | |
3357 | * mask to delegate. */ | |
3358 | ||
1bdfc7b9 | 3359 | if (isempty(rvalue)) { |
d48013f8 YW |
3360 | /* An empty string resets controllers and set Delegate=yes. */ |
3361 | c->delegate = true; | |
1bdfc7b9 YW |
3362 | c->delegate_controllers = 0; |
3363 | return 0; | |
3364 | } | |
3365 | ||
02638280 LP |
3366 | r = parse_boolean(rvalue); |
3367 | if (r < 0) { | |
3368 | const char *p = rvalue; | |
3369 | CGroupMask mask = 0; | |
3370 | ||
3371 | for (;;) { | |
3372 | _cleanup_free_ char *word = NULL; | |
3373 | CGroupController cc; | |
3374 | ||
3375 | r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES); | |
3376 | if (r == 0) | |
3377 | break; | |
3378 | if (r == -ENOMEM) | |
3379 | return log_oom(); | |
3380 | if (r < 0) { | |
3381 | log_syntax(unit, LOG_ERR, filename, line, r, "Invalid syntax, ignoring: %s", rvalue); | |
ff1b8455 | 3382 | return 0; |
02638280 LP |
3383 | } |
3384 | ||
3385 | cc = cgroup_controller_from_string(word); | |
3386 | if (cc < 0) { | |
063c4b1a | 3387 | log_syntax(unit, LOG_ERR, filename, line, r, "Invalid controller name '%s', ignoring", word); |
02638280 LP |
3388 | continue; |
3389 | } | |
3390 | ||
3391 | mask |= CGROUP_CONTROLLER_TO_MASK(cc); | |
3392 | } | |
3393 | ||
3394 | c->delegate = true; | |
3395 | c->delegate_controllers |= mask; | |
3396 | ||
3397 | } else if (r > 0) { | |
3398 | c->delegate = true; | |
3399 | c->delegate_controllers = _CGROUP_MASK_ALL; | |
3400 | } else { | |
3401 | c->delegate = false; | |
3402 | c->delegate_controllers = 0; | |
3403 | } | |
3404 | ||
3405 | return 0; | |
3406 | } | |
3407 | ||
4ad49000 LP |
3408 | int config_parse_device_allow( |
3409 | const char *unit, | |
3410 | const char *filename, | |
3411 | unsigned line, | |
3412 | const char *section, | |
71a61510 | 3413 | unsigned section_line, |
4ad49000 LP |
3414 | const char *lvalue, |
3415 | int ltype, | |
3416 | const char *rvalue, | |
3417 | void *data, | |
3418 | void *userdata) { | |
3419 | ||
c9f620bf | 3420 | _cleanup_free_ char *path = NULL, *resolved = NULL; |
4ad49000 LP |
3421 | CGroupContext *c = data; |
3422 | CGroupDeviceAllow *a; | |
c9f620bf | 3423 | const char *p = rvalue; |
1116e14c | 3424 | int r; |
4ad49000 LP |
3425 | |
3426 | if (isempty(rvalue)) { | |
3427 | while (c->device_allow) | |
3428 | cgroup_context_free_device_allow(c, c->device_allow); | |
3429 | ||
3430 | return 0; | |
3431 | } | |
3432 | ||
c9f620bf YW |
3433 | r = extract_first_word(&p, &path, NULL, EXTRACT_QUOTES); |
3434 | if (r == -ENOMEM) | |
3435 | return log_oom(); | |
508f63b4 | 3436 | if (r < 0) { |
1116e14c | 3437 | log_syntax(unit, LOG_WARNING, filename, line, r, |
c9f620bf YW |
3438 | "Invalid syntax, ignoring: %s", rvalue); |
3439 | return 0; | |
3440 | } | |
3441 | if (r == 0) { | |
3442 | log_syntax(unit, LOG_WARNING, filename, line, 0, | |
3443 | "Failed to extract device path and rights from '%s', ignoring.", rvalue); | |
20d52ab6 | 3444 | return 0; |
1116e14c NBS |
3445 | } |
3446 | ||
c9f620bf YW |
3447 | r = unit_full_printf(userdata, path, &resolved); |
3448 | if (r < 0) { | |
3449 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
3450 | "Failed to resolve unit specifiers in '%s', ignoring: %m", path); | |
4ad49000 LP |
3451 | return 0; |
3452 | } | |
3453 | ||
c9f620bf YW |
3454 | if (!is_deviceallow_pattern(resolved) && |
3455 | !path_startswith(resolved, "/run/systemd/inaccessible/")) { | |
3456 | log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid device node path '%s', ignoring.", resolved); | |
3457 | return 0; | |
3458 | } | |
4ad49000 | 3459 | |
c9f620bf YW |
3460 | if (!isempty(p) && !in_charset(p, "rwm")) { |
3461 | log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid device rights '%s', ignoring.", p); | |
4ad49000 LP |
3462 | return 0; |
3463 | } | |
3464 | ||
3465 | a = new0(CGroupDeviceAllow, 1); | |
3466 | if (!a) | |
3467 | return log_oom(); | |
3468 | ||
c9f620bf YW |
3469 | a->path = TAKE_PTR(resolved); |
3470 | a->r = isempty(p) || !!strchr(p, 'r'); | |
3471 | a->w = isempty(p) || !!strchr(p, 'w'); | |
3472 | a->m = isempty(p) || !!strchr(p, 'm'); | |
4ad49000 | 3473 | |
71fda00f | 3474 | LIST_PREPEND(device_allow, c->device_allow, a); |
4ad49000 LP |
3475 | return 0; |
3476 | } | |
3477 | ||
13c31542 TH |
3478 | int config_parse_io_weight( |
3479 | const char *unit, | |
3480 | const char *filename, | |
3481 | unsigned line, | |
3482 | const char *section, | |
3483 | unsigned section_line, | |
3484 | const char *lvalue, | |
3485 | int ltype, | |
3486 | const char *rvalue, | |
3487 | void *data, | |
3488 | void *userdata) { | |
3489 | ||
3490 | uint64_t *weight = data; | |
3491 | int r; | |
3492 | ||
3493 | assert(filename); | |
3494 | assert(lvalue); | |
3495 | assert(rvalue); | |
3496 | ||
3497 | r = cg_weight_parse(rvalue, weight); | |
3498 | if (r < 0) { | |
063c4b1a | 3499 | log_syntax(unit, LOG_ERR, filename, line, r, "Invalid IO weight '%s', ignoring.", rvalue); |
13c31542 TH |
3500 | return 0; |
3501 | } | |
3502 | ||
3503 | return 0; | |
3504 | } | |
3505 | ||
3506 | int config_parse_io_device_weight( | |
3507 | const char *unit, | |
3508 | const char *filename, | |
3509 | unsigned line, | |
3510 | const char *section, | |
3511 | unsigned section_line, | |
3512 | const char *lvalue, | |
3513 | int ltype, | |
3514 | const char *rvalue, | |
3515 | void *data, | |
3516 | void *userdata) { | |
3517 | ||
c9f620bf | 3518 | _cleanup_free_ char *path = NULL, *resolved = NULL; |
13c31542 TH |
3519 | CGroupIODeviceWeight *w; |
3520 | CGroupContext *c = data; | |
c9f620bf | 3521 | const char *p = rvalue; |
13c31542 | 3522 | uint64_t u; |
13c31542 TH |
3523 | int r; |
3524 | ||
3525 | assert(filename); | |
3526 | assert(lvalue); | |
3527 | assert(rvalue); | |
3528 | ||
3529 | if (isempty(rvalue)) { | |
3530 | while (c->io_device_weights) | |
3531 | cgroup_context_free_io_device_weight(c, c->io_device_weights); | |
3532 | ||
3533 | return 0; | |
3534 | } | |
3535 | ||
c9f620bf YW |
3536 | r = extract_first_word(&p, &path, NULL, EXTRACT_QUOTES); |
3537 | if (r == -ENOMEM) | |
3538 | return log_oom(); | |
3539 | if (r < 0) { | |
3540 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
3541 | "Invalid syntax, ignoring: %s", rvalue); | |
3542 | return 0; | |
3543 | } | |
3544 | if (r == 0 || isempty(p)) { | |
3545 | log_syntax(unit, LOG_WARNING, filename, line, 0, | |
3546 | "Failed to extract device path and weight from '%s', ignoring.", rvalue); | |
13c31542 TH |
3547 | return 0; |
3548 | } | |
3549 | ||
c9f620bf YW |
3550 | r = unit_full_printf(userdata, path, &resolved); |
3551 | if (r < 0) { | |
3552 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
3553 | "Failed to resolve unit specifiers in '%s', ignoring: %m", path); | |
3554 | return 0; | |
3555 | } | |
13c31542 | 3556 | |
c9f620bf YW |
3557 | if (!path_startswith(resolved, "/dev") && |
3558 | !path_startswith(resolved, "/run/systemd/inaccessible/")) { | |
3559 | log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid device node path '%s', ignoring.", resolved); | |
13c31542 TH |
3560 | return 0; |
3561 | } | |
3562 | ||
c9f620bf | 3563 | r = cg_weight_parse(p, &u); |
13c31542 | 3564 | if (r < 0) { |
c9f620bf | 3565 | log_syntax(unit, LOG_ERR, filename, line, r, "IO weight '%s' invalid, ignoring: %m", p); |
13c31542 TH |
3566 | return 0; |
3567 | } | |
3568 | ||
3569 | assert(u != CGROUP_WEIGHT_INVALID); | |
3570 | ||
3571 | w = new0(CGroupIODeviceWeight, 1); | |
3572 | if (!w) | |
3573 | return log_oom(); | |
3574 | ||
c9f620bf | 3575 | w->path = TAKE_PTR(resolved); |
13c31542 TH |
3576 | w->weight = u; |
3577 | ||
3578 | LIST_PREPEND(device_weights, c->io_device_weights, w); | |
3579 | return 0; | |
3580 | } | |
3581 | ||
3582 | int config_parse_io_limit( | |
3583 | const char *unit, | |
3584 | const char *filename, | |
3585 | unsigned line, | |
3586 | const char *section, | |
3587 | unsigned section_line, | |
3588 | const char *lvalue, | |
3589 | int ltype, | |
3590 | const char *rvalue, | |
3591 | void *data, | |
3592 | void *userdata) { | |
3593 | ||
c9f620bf | 3594 | _cleanup_free_ char *path = NULL, *resolved = NULL; |
13c31542 TH |
3595 | CGroupIODeviceLimit *l = NULL, *t; |
3596 | CGroupContext *c = data; | |
9be57249 | 3597 | CGroupIOLimitType type; |
c9f620bf | 3598 | const char *p = rvalue; |
13c31542 | 3599 | uint64_t num; |
13c31542 TH |
3600 | int r; |
3601 | ||
3602 | assert(filename); | |
3603 | assert(lvalue); | |
3604 | assert(rvalue); | |
3605 | ||
9be57249 TH |
3606 | type = cgroup_io_limit_type_from_string(lvalue); |
3607 | assert(type >= 0); | |
13c31542 TH |
3608 | |
3609 | if (isempty(rvalue)) { | |
3610 | LIST_FOREACH(device_limits, l, c->io_device_limits) | |
9be57249 | 3611 | l->limits[type] = cgroup_io_limit_defaults[type]; |
13c31542 TH |
3612 | return 0; |
3613 | } | |
3614 | ||
c9f620bf YW |
3615 | r = extract_first_word(&p, &path, NULL, EXTRACT_QUOTES); |
3616 | if (r == -ENOMEM) | |
3617 | return log_oom(); | |
3618 | if (r < 0) { | |
3619 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
3620 | "Invalid syntax, ignoring: %s", rvalue); | |
3621 | return 0; | |
3622 | } | |
3623 | if (r == 0 || isempty(p)) { | |
3624 | log_syntax(unit, LOG_WARNING, filename, line, 0, | |
3625 | "Failed to extract device node and bandwidth from '%s', ignoring.", rvalue); | |
13c31542 TH |
3626 | return 0; |
3627 | } | |
3628 | ||
c9f620bf YW |
3629 | r = unit_full_printf(userdata, path, &resolved); |
3630 | if (r < 0) { | |
3631 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
3632 | "Failed to resolve unit specifiers in '%s', ignoring: %m", path); | |
3633 | return 0; | |
3634 | } | |
13c31542 | 3635 | |
c9f620bf YW |
3636 | if (!path_startswith(resolved, "/dev") && |
3637 | !path_startswith(resolved, "/run/systemd/inaccessible/")) { | |
3638 | log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid device node path '%s', ignoring.", resolved); | |
13c31542 TH |
3639 | return 0; |
3640 | } | |
3641 | ||
c9f620bf | 3642 | if (streq("infinity", p)) { |
13c31542 TH |
3643 | num = CGROUP_LIMIT_MAX; |
3644 | } else { | |
c9f620bf | 3645 | r = parse_size(p, 1000, &num); |
13c31542 | 3646 | if (r < 0 || num <= 0) { |
c9f620bf | 3647 | log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid IO limit '%s', ignoring.", p); |
13c31542 TH |
3648 | return 0; |
3649 | } | |
3650 | } | |
3651 | ||
3652 | LIST_FOREACH(device_limits, t, c->io_device_limits) { | |
c9f620bf | 3653 | if (path_equal(resolved, t->path)) { |
13c31542 TH |
3654 | l = t; |
3655 | break; | |
3656 | } | |
3657 | } | |
3658 | ||
3659 | if (!l) { | |
9be57249 TH |
3660 | CGroupIOLimitType ttype; |
3661 | ||
13c31542 TH |
3662 | l = new0(CGroupIODeviceLimit, 1); |
3663 | if (!l) | |
3664 | return log_oom(); | |
3665 | ||
c9f620bf | 3666 | l->path = TAKE_PTR(resolved); |
9be57249 TH |
3667 | for (ttype = 0; ttype < _CGROUP_IO_LIMIT_TYPE_MAX; ttype++) |
3668 | l->limits[ttype] = cgroup_io_limit_defaults[ttype]; | |
13c31542 TH |
3669 | |
3670 | LIST_PREPEND(device_limits, c->io_device_limits, l); | |
3671 | } | |
3672 | ||
9be57249 | 3673 | l->limits[type] = num; |
13c31542 TH |
3674 | |
3675 | return 0; | |
3676 | } | |
3677 | ||
4ad49000 LP |
3678 | int config_parse_blockio_weight( |
3679 | const char *unit, | |
3680 | const char *filename, | |
3681 | unsigned line, | |
3682 | const char *section, | |
71a61510 | 3683 | unsigned section_line, |
4ad49000 LP |
3684 | const char *lvalue, |
3685 | int ltype, | |
3686 | const char *rvalue, | |
3687 | void *data, | |
3688 | void *userdata) { | |
3689 | ||
d53d9474 | 3690 | uint64_t *weight = data; |
95ae05c0 WC |
3691 | int r; |
3692 | ||
3693 | assert(filename); | |
3694 | assert(lvalue); | |
3695 | assert(rvalue); | |
3696 | ||
d53d9474 LP |
3697 | r = cg_blkio_weight_parse(rvalue, weight); |
3698 | if (r < 0) { | |
063c4b1a | 3699 | log_syntax(unit, LOG_ERR, filename, line, r, "Invalid block IO weight '%s', ignoring: %m", rvalue); |
95ae05c0 WC |
3700 | return 0; |
3701 | } | |
3702 | ||
8e7076ca LP |
3703 | return 0; |
3704 | } | |
3705 | ||
3706 | int config_parse_blockio_device_weight( | |
3707 | const char *unit, | |
3708 | const char *filename, | |
3709 | unsigned line, | |
3710 | const char *section, | |
71a61510 | 3711 | unsigned section_line, |
8e7076ca LP |
3712 | const char *lvalue, |
3713 | int ltype, | |
3714 | const char *rvalue, | |
3715 | void *data, | |
3716 | void *userdata) { | |
3717 | ||
c9f620bf | 3718 | _cleanup_free_ char *path = NULL, *resolved = NULL; |
8e7076ca | 3719 | CGroupBlockIODeviceWeight *w; |
4ad49000 | 3720 | CGroupContext *c = data; |
c9f620bf | 3721 | const char *p = rvalue; |
d53d9474 | 3722 | uint64_t u; |
4ad49000 LP |
3723 | int r; |
3724 | ||
3725 | assert(filename); | |
3726 | assert(lvalue); | |
3727 | assert(rvalue); | |
3728 | ||
3729 | if (isempty(rvalue)) { | |
4ad49000 LP |
3730 | while (c->blockio_device_weights) |
3731 | cgroup_context_free_blockio_device_weight(c, c->blockio_device_weights); | |
3732 | ||
3733 | return 0; | |
3734 | } | |
3735 | ||
c9f620bf YW |
3736 | r = extract_first_word(&p, &path, NULL, EXTRACT_QUOTES); |
3737 | if (r == -ENOMEM) | |
3738 | return log_oom(); | |
3739 | if (r < 0) { | |
3740 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
3741 | "Invalid syntax, ignoring: %s", rvalue); | |
3742 | return 0; | |
3743 | } | |
3744 | if (r == 0 || isempty(p)) { | |
3745 | log_syntax(unit, LOG_WARNING, filename, line, 0, | |
3746 | "Failed to extract device node and weight from '%s', ignoring.", rvalue); | |
8e7076ca LP |
3747 | return 0; |
3748 | } | |
4ad49000 | 3749 | |
c9f620bf YW |
3750 | r = unit_full_printf(userdata, path, &resolved); |
3751 | if (r < 0) { | |
3752 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
3753 | "Failed to resolve unit specifiers in '%s', ignoring: %m", path); | |
3754 | return 0; | |
3755 | } | |
4ad49000 | 3756 | |
c9f620bf YW |
3757 | if (!path_startswith(resolved, "/dev") && |
3758 | !path_startswith(resolved, "/run/systemd/inaccessible/")) { | |
3759 | log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid device node path '%s'. Ignoring.", resolved); | |
8e7076ca LP |
3760 | return 0; |
3761 | } | |
4ad49000 | 3762 | |
c9f620bf | 3763 | r = cg_blkio_weight_parse(p, &u); |
d53d9474 | 3764 | if (r < 0) { |
c9f620bf | 3765 | log_syntax(unit, LOG_ERR, filename, line, r, "Invalid block IO weight '%s', ignoring: %m", p); |
4ad49000 LP |
3766 | return 0; |
3767 | } | |
3768 | ||
d53d9474 LP |
3769 | assert(u != CGROUP_BLKIO_WEIGHT_INVALID); |
3770 | ||
8e7076ca LP |
3771 | w = new0(CGroupBlockIODeviceWeight, 1); |
3772 | if (!w) | |
3773 | return log_oom(); | |
4ad49000 | 3774 | |
c9f620bf | 3775 | w->path = TAKE_PTR(resolved); |
d53d9474 | 3776 | w->weight = u; |
4ad49000 | 3777 | |
71fda00f | 3778 | LIST_PREPEND(device_weights, c->blockio_device_weights, w); |
4ad49000 LP |
3779 | return 0; |
3780 | } | |
3781 | ||
3782 | int config_parse_blockio_bandwidth( | |
3783 | const char *unit, | |
3784 | const char *filename, | |
3785 | unsigned line, | |
3786 | const char *section, | |
71a61510 | 3787 | unsigned section_line, |
4ad49000 LP |
3788 | const char *lvalue, |
3789 | int ltype, | |
3790 | const char *rvalue, | |
3791 | void *data, | |
3792 | void *userdata) { | |
3793 | ||
c9f620bf | 3794 | _cleanup_free_ char *path = NULL, *resolved = NULL; |
979d0311 | 3795 | CGroupBlockIODeviceBandwidth *b = NULL, *t; |
4ad49000 | 3796 | CGroupContext *c = data; |
c9f620bf | 3797 | const char *p = rvalue; |
59f448cf | 3798 | uint64_t bytes; |
47c0980d | 3799 | bool read; |
4ad49000 LP |
3800 | int r; |
3801 | ||
3802 | assert(filename); | |
3803 | assert(lvalue); | |
3804 | assert(rvalue); | |
3805 | ||
47c0980d G |
3806 | read = streq("BlockIOReadBandwidth", lvalue); |
3807 | ||
4ad49000 | 3808 | if (isempty(rvalue)) { |
979d0311 TH |
3809 | LIST_FOREACH(device_bandwidths, b, c->blockio_device_bandwidths) { |
3810 | b->rbps = CGROUP_LIMIT_MAX; | |
3811 | b->wbps = CGROUP_LIMIT_MAX; | |
3812 | } | |
4ad49000 LP |
3813 | return 0; |
3814 | } | |
3815 | ||
c9f620bf YW |
3816 | r = extract_first_word(&p, &path, NULL, EXTRACT_QUOTES); |
3817 | if (r == -ENOMEM) | |
3818 | return log_oom(); | |
3819 | if (r < 0) { | |
3820 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
3821 | "Invalid syntax, ignoring: %s", rvalue); | |
3822 | return 0; | |
3823 | } | |
3824 | if (r == 0 || isempty(p)) { | |
3825 | log_syntax(unit, LOG_WARNING, filename, line, 0, | |
3826 | "Failed to extract device node and bandwidth from '%s', ignoring.", rvalue); | |
4ad49000 LP |
3827 | return 0; |
3828 | } | |
3829 | ||
c9f620bf YW |
3830 | r = unit_full_printf(userdata, path, &resolved); |
3831 | if (r < 0) { | |
3832 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
3833 | "Failed to resolve unit specifiers in '%s', ignoring: %m", path); | |
3834 | return 0; | |
3835 | } | |
4ad49000 | 3836 | |
c9f620bf YW |
3837 | if (!path_startswith(resolved, "/dev") && |
3838 | !path_startswith(resolved, "/run/systemd/inaccessible/")) { | |
3839 | log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid device node path '%s', ignoring.", resolved); | |
4ad49000 LP |
3840 | return 0; |
3841 | } | |
3842 | ||
c9f620bf | 3843 | r = parse_size(p, 1000, &bytes); |
4ad49000 | 3844 | if (r < 0 || bytes <= 0) { |
c9f620bf | 3845 | log_syntax(unit, LOG_ERR, filename, line, r, "Invalid Block IO Bandwidth '%s', ignoring.", p); |
4ad49000 LP |
3846 | return 0; |
3847 | } | |
3848 | ||
979d0311 | 3849 | LIST_FOREACH(device_bandwidths, t, c->blockio_device_bandwidths) { |
c9f620bf | 3850 | if (path_equal(resolved, t->path)) { |
979d0311 TH |
3851 | b = t; |
3852 | break; | |
3853 | } | |
3854 | } | |
4ad49000 | 3855 | |
979d0311 TH |
3856 | if (!t) { |
3857 | b = new0(CGroupBlockIODeviceBandwidth, 1); | |
3858 | if (!b) | |
3859 | return log_oom(); | |
3860 | ||
c9f620bf | 3861 | b->path = TAKE_PTR(resolved); |
979d0311 TH |
3862 | b->rbps = CGROUP_LIMIT_MAX; |
3863 | b->wbps = CGROUP_LIMIT_MAX; | |
3864 | ||
3865 | LIST_PREPEND(device_bandwidths, c->blockio_device_bandwidths, b); | |
3866 | } | |
4ad49000 | 3867 | |
979d0311 TH |
3868 | if (read) |
3869 | b->rbps = bytes; | |
3870 | else | |
3871 | b->wbps = bytes; | |
4ad49000 LP |
3872 | |
3873 | return 0; | |
3874 | } | |
3875 | ||
d420282b LP |
3876 | DEFINE_CONFIG_PARSE_ENUM(config_parse_job_mode, job_mode, JobMode, "Failed to parse job mode"); |
3877 | ||
3878 | int config_parse_job_mode_isolate( | |
3879 | const char *unit, | |
3880 | const char *filename, | |
3881 | unsigned line, | |
3882 | const char *section, | |
3883 | unsigned section_line, | |
3884 | const char *lvalue, | |
3885 | int ltype, | |
3886 | const char *rvalue, | |
3887 | void *data, | |
3888 | void *userdata) { | |
3889 | ||
3890 | JobMode *m = data; | |
3891 | int r; | |
3892 | ||
3893 | assert(filename); | |
3894 | assert(lvalue); | |
3895 | assert(rvalue); | |
3896 | ||
3897 | r = parse_boolean(rvalue); | |
3898 | if (r < 0) { | |
12ca818f | 3899 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse boolean, ignoring: %s", rvalue); |
d420282b LP |
3900 | return 0; |
3901 | } | |
3902 | ||
8ab39347 YW |
3903 | log_notice("%s is deprecated. Please use OnFailureJobMode= instead", lvalue); |
3904 | ||
d420282b LP |
3905 | *m = r ? JOB_ISOLATE : JOB_REPLACE; |
3906 | return 0; | |
3907 | } | |
3908 | ||
53f47dfc YW |
3909 | DEFINE_CONFIG_PARSE_ENUM(config_parse_runtime_preserve_mode, exec_preserve_mode, ExecPreserveMode, "Failed to parse runtime directory preserve mode"); |
3910 | ||
3536f49e | 3911 | int config_parse_exec_directories( |
e66cf1a3 LP |
3912 | const char *unit, |
3913 | const char *filename, | |
3914 | unsigned line, | |
3915 | const char *section, | |
3916 | unsigned section_line, | |
3917 | const char *lvalue, | |
3918 | int ltype, | |
3919 | const char *rvalue, | |
3920 | void *data, | |
3921 | void *userdata) { | |
3922 | ||
a2a5291b | 3923 | char***rt = data; |
9b5864d9 | 3924 | Unit *u = userdata; |
035fe294 | 3925 | const char *p; |
e66cf1a3 LP |
3926 | int r; |
3927 | ||
3928 | assert(filename); | |
3929 | assert(lvalue); | |
3930 | assert(rvalue); | |
3931 | assert(data); | |
3932 | ||
3933 | if (isempty(rvalue)) { | |
3934 | /* Empty assignment resets the list */ | |
6796073e | 3935 | *rt = strv_free(*rt); |
e66cf1a3 LP |
3936 | return 0; |
3937 | } | |
3938 | ||
035fe294 ZJS |
3939 | for (p = rvalue;;) { |
3940 | _cleanup_free_ char *word = NULL, *k = NULL; | |
e66cf1a3 | 3941 | |
035fe294 | 3942 | r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES); |
035fe294 | 3943 | if (r == -ENOMEM) |
e66cf1a3 | 3944 | return log_oom(); |
035fe294 ZJS |
3945 | if (r < 0) { |
3946 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
3947 | "Invalid syntax, ignoring: %s", rvalue); | |
3948 | return 0; | |
3949 | } | |
091e9efe LP |
3950 | if (r == 0) |
3951 | return 0; | |
e66cf1a3 | 3952 | |
18913df9 | 3953 | r = unit_full_printf(u, word, &k); |
9b5864d9 | 3954 | if (r < 0) { |
035fe294 | 3955 | log_syntax(unit, LOG_ERR, filename, line, r, |
063c4b1a | 3956 | "Failed to resolve unit specifiers in \"%s\", ignoring: %m", word); |
9b5864d9 MG |
3957 | continue; |
3958 | } | |
3959 | ||
e8865688 | 3960 | if (!path_is_normalized(k)) { |
035fe294 | 3961 | log_syntax(unit, LOG_ERR, filename, line, 0, |
e8865688 LP |
3962 | "%s= path is not normalized, ignoring assignment: %s", lvalue, rvalue); |
3963 | continue; | |
3964 | } | |
3965 | ||
3966 | if (path_is_absolute(k)) { | |
3967 | log_syntax(unit, LOG_ERR, filename, line, 0, | |
3968 | "%s= path is absolute, ignoring assignment: %s", lvalue, rvalue); | |
3969 | continue; | |
3970 | } | |
3971 | ||
3972 | if (path_startswith(k, "private")) { | |
3973 | log_syntax(unit, LOG_ERR, filename, line, 0, | |
3974 | "%s= path can't be 'private', ingoring assignment: %s", lvalue, rvalue); | |
e66cf1a3 LP |
3975 | continue; |
3976 | } | |
3977 | ||
035fe294 | 3978 | r = strv_push(rt, k); |
e66cf1a3 LP |
3979 | if (r < 0) |
3980 | return log_oom(); | |
035fe294 | 3981 | k = NULL; |
e66cf1a3 | 3982 | } |
e66cf1a3 LP |
3983 | } |
3984 | ||
3af00fb8 LP |
3985 | int config_parse_set_status( |
3986 | const char *unit, | |
3987 | const char *filename, | |
3988 | unsigned line, | |
3989 | const char *section, | |
3990 | unsigned section_line, | |
3991 | const char *lvalue, | |
3992 | int ltype, | |
3993 | const char *rvalue, | |
3994 | void *data, | |
3995 | void *userdata) { | |
3996 | ||
3af00fb8 | 3997 | size_t l; |
a2a5291b | 3998 | const char *word, *state; |
3af00fb8 LP |
3999 | int r; |
4000 | ExitStatusSet *status_set = data; | |
4001 | ||
4002 | assert(filename); | |
4003 | assert(lvalue); | |
4004 | assert(rvalue); | |
4005 | assert(data); | |
4006 | ||
3e2d435b | 4007 | /* Empty assignment resets the list */ |
3af00fb8 | 4008 | if (isempty(rvalue)) { |
3e2d435b | 4009 | exit_status_set_free(status_set); |
3af00fb8 LP |
4010 | return 0; |
4011 | } | |
4012 | ||
a2a5291b | 4013 | FOREACH_WORD(word, l, rvalue, state) { |
3af00fb8 LP |
4014 | _cleanup_free_ char *temp; |
4015 | int val; | |
61593865 | 4016 | Set **set; |
3af00fb8 | 4017 | |
a2a5291b | 4018 | temp = strndup(word, l); |
3af00fb8 LP |
4019 | if (!temp) |
4020 | return log_oom(); | |
4021 | ||
4022 | r = safe_atoi(temp, &val); | |
4023 | if (r < 0) { | |
29a3db75 | 4024 | val = signal_from_string(temp); |
3af00fb8 | 4025 | |
1e2fd62d | 4026 | if (val <= 0) { |
12ca818f | 4027 | log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse value, ignoring: %s", word); |
61593865 | 4028 | continue; |
3af00fb8 | 4029 | } |
61593865 | 4030 | set = &status_set->signal; |
3af00fb8 | 4031 | } else { |
1e2fd62d | 4032 | if (val < 0 || val > 255) { |
12ca818f | 4033 | log_syntax(unit, LOG_ERR, filename, line, 0, "Value %d is outside range 0-255, ignoring", val); |
1e2fd62d | 4034 | continue; |
3af00fb8 | 4035 | } |
61593865 | 4036 | set = &status_set->status; |
3af00fb8 | 4037 | } |
1e2fd62d | 4038 | |
61593865 | 4039 | r = set_ensure_allocated(set, NULL); |
1e2fd62d ZJS |
4040 | if (r < 0) |
4041 | return log_oom(); | |
4042 | ||
61593865 | 4043 | r = set_put(*set, INT_TO_PTR(val)); |
063c4b1a YW |
4044 | if (r < 0) |
4045 | return log_oom(); | |
3af00fb8 | 4046 | } |
b2fadec6 | 4047 | if (!isempty(state)) |
12ca818f | 4048 | log_syntax(unit, LOG_ERR, filename, line, 0, "Trailing garbage, ignoring."); |
3af00fb8 LP |
4049 | |
4050 | return 0; | |
4051 | } | |
4052 | ||
94828d2d LP |
4053 | int config_parse_namespace_path_strv( |
4054 | const char *unit, | |
4055 | const char *filename, | |
4056 | unsigned line, | |
4057 | const char *section, | |
4058 | unsigned section_line, | |
4059 | const char *lvalue, | |
4060 | int ltype, | |
4061 | const char *rvalue, | |
4062 | void *data, | |
4063 | void *userdata) { | |
4064 | ||
7b07e993 | 4065 | Unit *u = userdata; |
a2a5291b | 4066 | char*** sv = data; |
063c4b1a | 4067 | const char *p = rvalue; |
94828d2d LP |
4068 | int r; |
4069 | ||
4070 | assert(filename); | |
4071 | assert(lvalue); | |
4072 | assert(rvalue); | |
4073 | assert(data); | |
4074 | ||
4075 | if (isempty(rvalue)) { | |
4076 | /* Empty assignment resets the list */ | |
6796073e | 4077 | *sv = strv_free(*sv); |
94828d2d LP |
4078 | return 0; |
4079 | } | |
4080 | ||
727f76d7 | 4081 | for (;;) { |
7b07e993 | 4082 | _cleanup_free_ char *word = NULL, *resolved = NULL, *joined = NULL; |
20b7a007 LP |
4083 | const char *w; |
4084 | bool ignore_enoent = false, shall_prefix = false; | |
94828d2d | 4085 | |
063c4b1a | 4086 | r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES); |
0293a7a8 EV |
4087 | if (r == 0) |
4088 | break; | |
4089 | if (r == -ENOMEM) | |
4090 | return log_oom(); | |
727f76d7 | 4091 | if (r < 0) { |
7b07e993 | 4092 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to extract first word, ignoring: %s", rvalue); |
727f76d7 EV |
4093 | return 0; |
4094 | } | |
94828d2d | 4095 | |
727f76d7 EV |
4096 | if (!utf8_is_valid(word)) { |
4097 | log_syntax_invalid_utf8(unit, LOG_ERR, filename, line, word); | |
94828d2d LP |
4098 | continue; |
4099 | } | |
4100 | ||
20b7a007 LP |
4101 | w = word; |
4102 | if (startswith(w, "-")) { | |
4103 | ignore_enoent = true; | |
4104 | w++; | |
4105 | } | |
4106 | if (startswith(w, "+")) { | |
4107 | shall_prefix = true; | |
4108 | w++; | |
4109 | } | |
7b07e993 | 4110 | |
20b7a007 | 4111 | r = unit_full_printf(u, w, &resolved); |
7b07e993 | 4112 | if (r < 0) { |
063c4b1a | 4113 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s: %m", w); |
94828d2d LP |
4114 | continue; |
4115 | } | |
4116 | ||
7b07e993 LP |
4117 | if (!path_is_absolute(resolved)) { |
4118 | log_syntax(unit, LOG_ERR, filename, line, 0, "Not an absolute path, ignoring: %s", resolved); | |
4119 | continue; | |
4120 | } | |
4121 | ||
4122 | path_kill_slashes(resolved); | |
94828d2d | 4123 | |
20b7a007 LP |
4124 | joined = strjoin(ignore_enoent ? "-" : "", |
4125 | shall_prefix ? "+" : "", | |
4126 | resolved); | |
7b07e993 LP |
4127 | |
4128 | r = strv_push(sv, joined); | |
94828d2d LP |
4129 | if (r < 0) |
4130 | return log_oom(); | |
4131 | ||
7b07e993 | 4132 | joined = NULL; |
94828d2d LP |
4133 | } |
4134 | ||
4135 | return 0; | |
4136 | } | |
4137 | ||
2abd4e38 YW |
4138 | int config_parse_temporary_filesystems( |
4139 | const char *unit, | |
4140 | const char *filename, | |
4141 | unsigned line, | |
4142 | const char *section, | |
4143 | unsigned section_line, | |
4144 | const char *lvalue, | |
4145 | int ltype, | |
4146 | const char *rvalue, | |
4147 | void *data, | |
4148 | void *userdata) { | |
4149 | ||
4150 | Unit *u = userdata; | |
4151 | ExecContext *c = data; | |
063c4b1a | 4152 | const char *p = rvalue; |
2abd4e38 YW |
4153 | int r; |
4154 | ||
4155 | assert(filename); | |
4156 | assert(lvalue); | |
4157 | assert(rvalue); | |
4158 | assert(data); | |
4159 | ||
4160 | if (isempty(rvalue)) { | |
4161 | /* Empty assignment resets the list */ | |
4162 | temporary_filesystem_free_many(c->temporary_filesystems, c->n_temporary_filesystems); | |
4163 | c->temporary_filesystems = NULL; | |
4164 | c->n_temporary_filesystems = 0; | |
4165 | return 0; | |
4166 | } | |
4167 | ||
2abd4e38 YW |
4168 | for (;;) { |
4169 | _cleanup_free_ char *word = NULL, *path = NULL, *resolved = NULL; | |
4170 | const char *w; | |
4171 | ||
063c4b1a | 4172 | r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES); |
2abd4e38 | 4173 | if (r == 0) |
063c4b1a | 4174 | return 0; |
2abd4e38 YW |
4175 | if (r == -ENOMEM) |
4176 | return log_oom(); | |
4177 | if (r < 0) { | |
4178 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to extract first word, ignoring: %s", rvalue); | |
4179 | return 0; | |
4180 | } | |
4181 | ||
4182 | w = word; | |
4183 | r = extract_first_word(&w, &path, ":", EXTRACT_DONT_COALESCE_SEPARATORS); | |
063c4b1a YW |
4184 | if (r == -ENOMEM) |
4185 | return log_oom(); | |
4186 | if (r < 0) { | |
4187 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to extract first word, ignoring: %s", word); | |
4188 | continue; | |
4189 | } | |
4190 | if (r == 0) { | |
4191 | log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid syntax, ignoring: %s", word); | |
4192 | continue; | |
4193 | } | |
2abd4e38 YW |
4194 | |
4195 | r = unit_full_printf(u, path, &resolved); | |
4196 | if (r < 0) { | |
063c4b1a | 4197 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to resolve unit specifiers in %s, ignoring: %m", path); |
2abd4e38 YW |
4198 | continue; |
4199 | } | |
4200 | ||
4201 | if (!path_is_absolute(resolved)) { | |
4202 | log_syntax(unit, LOG_ERR, filename, line, 0, "Not an absolute path, ignoring: %s", resolved); | |
4203 | continue; | |
4204 | } | |
4205 | ||
4206 | path_kill_slashes(resolved); | |
4207 | ||
4208 | r = temporary_filesystem_add(&c->temporary_filesystems, &c->n_temporary_filesystems, path, w); | |
4209 | if (r == -ENOMEM) | |
4210 | return log_oom(); | |
4211 | if (r < 0) { | |
4212 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse mount options, ignoring: %s", word); | |
4213 | continue; | |
4214 | } | |
4215 | } | |
2abd4e38 YW |
4216 | } |
4217 | ||
d2d6c096 LP |
4218 | int config_parse_bind_paths( |
4219 | const char *unit, | |
4220 | const char *filename, | |
4221 | unsigned line, | |
4222 | const char *section, | |
4223 | unsigned section_line, | |
4224 | const char *lvalue, | |
4225 | int ltype, | |
4226 | const char *rvalue, | |
4227 | void *data, | |
4228 | void *userdata) { | |
4229 | ||
4230 | ExecContext *c = data; | |
42d43f21 | 4231 | Unit *u = userdata; |
d2d6c096 LP |
4232 | const char *p; |
4233 | int r; | |
4234 | ||
4235 | assert(filename); | |
4236 | assert(lvalue); | |
4237 | assert(rvalue); | |
4238 | assert(data); | |
4239 | ||
4240 | if (isempty(rvalue)) { | |
4241 | /* Empty assignment resets the list */ | |
4242 | bind_mount_free_many(c->bind_mounts, c->n_bind_mounts); | |
4243 | c->bind_mounts = NULL; | |
4244 | c->n_bind_mounts = 0; | |
4245 | return 0; | |
4246 | } | |
4247 | ||
4248 | p = rvalue; | |
4249 | for (;;) { | |
4250 | _cleanup_free_ char *source = NULL, *destination = NULL; | |
42d43f21 | 4251 | _cleanup_free_ char *sresolved = NULL, *dresolved = NULL; |
d2d6c096 LP |
4252 | char *s = NULL, *d = NULL; |
4253 | bool rbind = true, ignore_enoent = false; | |
4254 | ||
4255 | r = extract_first_word(&p, &source, ":" WHITESPACE, EXTRACT_QUOTES|EXTRACT_DONT_COALESCE_SEPARATORS); | |
4256 | if (r == 0) | |
4257 | break; | |
4258 | if (r == -ENOMEM) | |
4259 | return log_oom(); | |
4260 | if (r < 0) { | |
4261 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse %s: %s", lvalue, rvalue); | |
4262 | return 0; | |
4263 | } | |
4264 | ||
42d43f21 DC |
4265 | r = unit_full_printf(u, source, &sresolved); |
4266 | if (r < 0) { | |
4267 | log_syntax(unit, LOG_ERR, filename, line, r, | |
063c4b1a | 4268 | "Failed to resolved unit specifiers in \"%s\", ignoring: %m", source); |
42d43f21 DC |
4269 | return 0; |
4270 | } | |
4271 | ||
4272 | s = sresolved; | |
d2d6c096 LP |
4273 | if (s[0] == '-') { |
4274 | ignore_enoent = true; | |
4275 | s++; | |
4276 | } | |
4277 | ||
4278 | if (!utf8_is_valid(s)) { | |
4279 | log_syntax_invalid_utf8(unit, LOG_ERR, filename, line, s); | |
4280 | return 0; | |
4281 | } | |
4282 | if (!path_is_absolute(s)) { | |
4283 | log_syntax(unit, LOG_ERR, filename, line, 0, "Not an absolute source path, ignoring: %s", s); | |
4284 | return 0; | |
4285 | } | |
4286 | ||
4287 | path_kill_slashes(s); | |
4288 | ||
4289 | /* Optionally, the destination is specified. */ | |
4290 | if (p && p[-1] == ':') { | |
4291 | r = extract_first_word(&p, &destination, ":" WHITESPACE, EXTRACT_QUOTES|EXTRACT_DONT_COALESCE_SEPARATORS); | |
4292 | if (r == -ENOMEM) | |
4293 | return log_oom(); | |
4294 | if (r < 0) { | |
4295 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse %s: %s", lvalue, rvalue); | |
4296 | return 0; | |
4297 | } | |
4298 | if (r == 0) { | |
4299 | log_syntax(unit, LOG_ERR, filename, line, 0, "Missing argument after ':': %s", rvalue); | |
4300 | return 0; | |
4301 | } | |
4302 | ||
42d43f21 DC |
4303 | r = unit_full_printf(u, destination, &dresolved); |
4304 | if (r < 0) { | |
4305 | log_syntax(unit, LOG_ERR, filename, line, r, | |
4306 | "Failed to resolved specifiers in \"%s\", ignoring: %m", destination); | |
4307 | return 0; | |
4308 | } | |
4309 | ||
4310 | if (!utf8_is_valid(dresolved)) { | |
4311 | log_syntax_invalid_utf8(unit, LOG_ERR, filename, line, dresolved); | |
d2d6c096 LP |
4312 | return 0; |
4313 | } | |
42d43f21 DC |
4314 | if (!path_is_absolute(dresolved)) { |
4315 | log_syntax(unit, LOG_ERR, filename, line, 0, "Not an absolute destination path, ignoring: %s", dresolved); | |
d2d6c096 LP |
4316 | return 0; |
4317 | } | |
4318 | ||
42d43f21 | 4319 | d = path_kill_slashes(dresolved); |
d2d6c096 LP |
4320 | |
4321 | /* Optionally, there's also a short option string specified */ | |
4322 | if (p && p[-1] == ':') { | |
4323 | _cleanup_free_ char *options = NULL; | |
4324 | ||
4325 | r = extract_first_word(&p, &options, NULL, EXTRACT_QUOTES); | |
4326 | if (r == -ENOMEM) | |
4327 | return log_oom(); | |
4328 | if (r < 0) { | |
4329 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse %s: %s", lvalue, rvalue); | |
4330 | return 0; | |
4331 | } | |
4332 | ||
4333 | if (isempty(options) || streq(options, "rbind")) | |
4334 | rbind = true; | |
4335 | else if (streq(options, "norbind")) | |
4336 | rbind = false; | |
4337 | else { | |
4338 | log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid option string, ignoring setting: %s", options); | |
4339 | return 0; | |
4340 | } | |
4341 | } | |
4342 | } else | |
4343 | d = s; | |
4344 | ||
4345 | r = bind_mount_add(&c->bind_mounts, &c->n_bind_mounts, | |
4346 | &(BindMount) { | |
4347 | .source = s, | |
4348 | .destination = d, | |
4349 | .read_only = !!strstr(lvalue, "ReadOnly"), | |
4350 | .recursive = rbind, | |
4351 | .ignore_enoent = ignore_enoent, | |
4352 | }); | |
4353 | if (r < 0) | |
4354 | return log_oom(); | |
4355 | } | |
4356 | ||
4357 | return 0; | |
4358 | } | |
4359 | ||
f1721625 | 4360 | int config_parse_no_new_privileges( |
760b9d7c LP |
4361 | const char* unit, |
4362 | const char *filename, | |
4363 | unsigned line, | |
4364 | const char *section, | |
4365 | unsigned section_line, | |
4366 | const char *lvalue, | |
4367 | int ltype, | |
4368 | const char *rvalue, | |
4369 | void *data, | |
4370 | void *userdata) { | |
4371 | ||
4372 | ExecContext *c = data; | |
063c4b1a | 4373 | int r; |
760b9d7c LP |
4374 | |
4375 | assert(filename); | |
4376 | assert(lvalue); | |
4377 | assert(rvalue); | |
4378 | assert(data); | |
4379 | ||
063c4b1a YW |
4380 | r = parse_boolean(rvalue); |
4381 | if (r < 0) { | |
4382 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse boolean value, ignoring: %s", rvalue); | |
760b9d7c LP |
4383 | return 0; |
4384 | } | |
4385 | ||
063c4b1a | 4386 | c->no_new_privileges = r; |
760b9d7c LP |
4387 | |
4388 | return 0; | |
4389 | } | |
4390 | ||
1b8689f9 | 4391 | int config_parse_protect_home( |
417116f2 LP |
4392 | const char* unit, |
4393 | const char *filename, | |
4394 | unsigned line, | |
4395 | const char *section, | |
4396 | unsigned section_line, | |
4397 | const char *lvalue, | |
4398 | int ltype, | |
4399 | const char *rvalue, | |
4400 | void *data, | |
4401 | void *userdata) { | |
4402 | ||
4403 | ExecContext *c = data; | |
5e1c6154 | 4404 | ProtectHome h; |
417116f2 LP |
4405 | |
4406 | assert(filename); | |
4407 | assert(lvalue); | |
4408 | assert(rvalue); | |
4409 | assert(data); | |
4410 | ||
4411 | /* Our enum shall be a superset of booleans, hence first try | |
61233823 | 4412 | * to parse as boolean, and then as enum */ |
417116f2 | 4413 | |
5e1c6154 YW |
4414 | h = parse_protect_home_or_bool(rvalue); |
4415 | if (h < 0) { | |
4416 | log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse protect home value, ignoring: %s", rvalue); | |
4417 | return 0; | |
1b8689f9 LP |
4418 | } |
4419 | ||
5e1c6154 YW |
4420 | c->protect_home = h; |
4421 | ||
1b8689f9 LP |
4422 | return 0; |
4423 | } | |
4424 | ||
4425 | int config_parse_protect_system( | |
4426 | const char* unit, | |
4427 | const char *filename, | |
4428 | unsigned line, | |
4429 | const char *section, | |
4430 | unsigned section_line, | |
4431 | const char *lvalue, | |
4432 | int ltype, | |
4433 | const char *rvalue, | |
4434 | void *data, | |
4435 | void *userdata) { | |
4436 | ||
4437 | ExecContext *c = data; | |
03c791aa | 4438 | ProtectSystem s; |
1b8689f9 LP |
4439 | |
4440 | assert(filename); | |
4441 | assert(lvalue); | |
4442 | assert(rvalue); | |
4443 | assert(data); | |
4444 | ||
03c791aa YW |
4445 | s = parse_protect_system_or_bool(rvalue); |
4446 | if (s < 0) { | |
4447 | log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse protect system value, ignoring: %s", rvalue); | |
4448 | return 0; | |
417116f2 LP |
4449 | } |
4450 | ||
03c791aa YW |
4451 | c->protect_system = s; |
4452 | ||
417116f2 LP |
4453 | return 0; |
4454 | } | |
4455 | ||
b1edf445 LP |
4456 | DEFINE_CONFIG_PARSE_ENUM(config_parse_exec_keyring_mode, exec_keyring_mode, ExecKeyringMode, "Failed to parse keyring mode"); |
4457 | ||
eae51da3 LP |
4458 | int config_parse_job_timeout_sec( |
4459 | const char* unit, | |
4460 | const char *filename, | |
4461 | unsigned line, | |
4462 | const char *section, | |
4463 | unsigned section_line, | |
4464 | const char *lvalue, | |
4465 | int ltype, | |
4466 | const char *rvalue, | |
4467 | void *data, | |
4468 | void *userdata) { | |
4469 | ||
4470 | Unit *u = data; | |
4471 | usec_t usec; | |
4472 | int r; | |
4473 | ||
4474 | assert(filename); | |
4475 | assert(lvalue); | |
4476 | assert(rvalue); | |
4477 | assert(u); | |
4478 | ||
4479 | r = parse_sec_fix_0(rvalue, &usec); | |
4480 | if (r < 0) { | |
4481 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse JobTimeoutSec= parameter, ignoring: %s", rvalue); | |
4482 | return 0; | |
4483 | } | |
4484 | ||
4485 | /* If the user explicitly changed JobTimeoutSec= also change JobRunningTimeoutSec=, for compatibility with old | |
c05f3c8f | 4486 | * versions. If JobRunningTimeoutSec= was explicitly set, avoid this however as whatever the user picked should |
eae51da3 LP |
4487 | * count. */ |
4488 | ||
4489 | if (!u->job_running_timeout_set) | |
4490 | u->job_running_timeout = usec; | |
4491 | ||
4492 | u->job_timeout = usec; | |
4493 | ||
4494 | return 0; | |
4495 | } | |
4496 | ||
4497 | int config_parse_job_running_timeout_sec( | |
4498 | const char* unit, | |
4499 | const char *filename, | |
4500 | unsigned line, | |
4501 | const char *section, | |
4502 | unsigned section_line, | |
4503 | const char *lvalue, | |
4504 | int ltype, | |
4505 | const char *rvalue, | |
4506 | void *data, | |
4507 | void *userdata) { | |
4508 | ||
4509 | Unit *u = data; | |
4510 | usec_t usec; | |
4511 | int r; | |
4512 | ||
4513 | assert(filename); | |
4514 | assert(lvalue); | |
4515 | assert(rvalue); | |
4516 | assert(u); | |
4517 | ||
4518 | r = parse_sec_fix_0(rvalue, &usec); | |
4519 | if (r < 0) { | |
4520 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse JobRunningTimeoutSec= parameter, ignoring: %s", rvalue); | |
4521 | return 0; | |
4522 | } | |
4523 | ||
4524 | u->job_running_timeout = usec; | |
4525 | u->job_running_timeout_set = true; | |
4526 | ||
4527 | return 0; | |
4528 | } | |
4529 | ||
071830ff | 4530 | #define FOLLOW_MAX 8 |
87f0e418 | 4531 | |
9e2f7c11 | 4532 | static int open_follow(char **filename, FILE **_f, Set *names, char **_final) { |
a837f088 | 4533 | char *id = NULL; |
0301abf4 | 4534 | unsigned c = 0; |
87f0e418 LP |
4535 | int fd, r; |
4536 | FILE *f; | |
87f0e418 LP |
4537 | |
4538 | assert(filename); | |
4539 | assert(*filename); | |
4540 | assert(_f); | |
4541 | assert(names); | |
4542 | ||
0301abf4 LP |
4543 | /* This will update the filename pointer if the loaded file is |
4544 | * reached by a symlink. The old string will be freed. */ | |
87f0e418 | 4545 | |
0301abf4 | 4546 | for (;;) { |
2c7108c4 | 4547 | char *target, *name; |
87f0e418 | 4548 | |
0301abf4 LP |
4549 | if (c++ >= FOLLOW_MAX) |
4550 | return -ELOOP; | |
4551 | ||
b08d03ff LP |
4552 | path_kill_slashes(*filename); |
4553 | ||
87f0e418 | 4554 | /* Add the file name we are currently looking at to |
8f05424d LP |
4555 | * the names of this unit, but only if it is a valid |
4556 | * unit name. */ | |
2b6bf07d | 4557 | name = basename(*filename); |
7410616c | 4558 | if (unit_name_is_valid(name, UNIT_NAME_ANY)) { |
8f05424d | 4559 | |
15e11d81 LP |
4560 | id = set_get(names, name); |
4561 | if (!id) { | |
4562 | id = strdup(name); | |
4563 | if (!id) | |
8f05424d | 4564 | return -ENOMEM; |
87f0e418 | 4565 | |
ef42202a ZJS |
4566 | r = set_consume(names, id); |
4567 | if (r < 0) | |
8f05424d | 4568 | return r; |
87f0e418 | 4569 | } |
87f0e418 LP |
4570 | } |
4571 | ||
0301abf4 | 4572 | /* Try to open the file name, but don't if its a symlink */ |
9946996c LP |
4573 | fd = open(*filename, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW); |
4574 | if (fd >= 0) | |
87f0e418 LP |
4575 | break; |
4576 | ||
0301abf4 LP |
4577 | if (errno != ELOOP) |
4578 | return -errno; | |
4579 | ||
87f0e418 | 4580 | /* Hmm, so this is a symlink. Let's read the name, and follow it manually */ |
9946996c LP |
4581 | r = readlink_and_make_absolute(*filename, &target); |
4582 | if (r < 0) | |
0301abf4 | 4583 | return r; |
87f0e418 | 4584 | |
0301abf4 | 4585 | free(*filename); |
2c7108c4 | 4586 | *filename = target; |
87f0e418 LP |
4587 | } |
4588 | ||
9946996c LP |
4589 | f = fdopen(fd, "re"); |
4590 | if (!f) { | |
03e334a1 | 4591 | safe_close(fd); |
d4ad27a1 | 4592 | return -errno; |
87f0e418 LP |
4593 | } |
4594 | ||
4595 | *_f = f; | |
9e2f7c11 | 4596 | *_final = id; |
a837f088 | 4597 | |
0301abf4 | 4598 | return 0; |
87f0e418 LP |
4599 | } |
4600 | ||
23a177ef LP |
4601 | static int merge_by_names(Unit **u, Set *names, const char *id) { |
4602 | char *k; | |
4603 | int r; | |
4604 | ||
4605 | assert(u); | |
4606 | assert(*u); | |
4607 | assert(names); | |
4608 | ||
4609 | /* Let's try to add in all symlink names we found */ | |
4610 | while ((k = set_steal_first(names))) { | |
4611 | ||
4612 | /* First try to merge in the other name into our | |
4613 | * unit */ | |
9946996c LP |
4614 | r = unit_merge_by_name(*u, k); |
4615 | if (r < 0) { | |
23a177ef LP |
4616 | Unit *other; |
4617 | ||
4618 | /* Hmm, we couldn't merge the other unit into | |
4619 | * ours? Then let's try it the other way | |
4620 | * round */ | |
4621 | ||
7aad67e7 MS |
4622 | /* If the symlink name we are looking at is unit template, then |
4623 | we must search for instance of this template */ | |
9d3e3406 | 4624 | if (unit_name_is_valid(k, UNIT_NAME_TEMPLATE) && (*u)->instance) { |
7aad67e7 MS |
4625 | _cleanup_free_ char *instance = NULL; |
4626 | ||
4627 | r = unit_name_replace_instance(k, (*u)->instance, &instance); | |
4628 | if (r < 0) | |
4629 | return r; | |
4630 | ||
4631 | other = manager_get_unit((*u)->manager, instance); | |
4632 | } else | |
4633 | other = manager_get_unit((*u)->manager, k); | |
4634 | ||
23a177ef LP |
4635 | free(k); |
4636 | ||
9946996c LP |
4637 | if (other) { |
4638 | r = unit_merge(other, *u); | |
4639 | if (r >= 0) { | |
23a177ef LP |
4640 | *u = other; |
4641 | return merge_by_names(u, names, NULL); | |
4642 | } | |
9946996c | 4643 | } |
23a177ef LP |
4644 | |
4645 | return r; | |
4646 | } | |
4647 | ||
4648 | if (id == k) | |
4649 | unit_choose_id(*u, id); | |
4650 | ||
4651 | free(k); | |
4652 | } | |
4653 | ||
4654 | return 0; | |
4655 | } | |
4656 | ||
e537352b | 4657 | static int load_from_path(Unit *u, const char *path) { |
e48614c4 ZJS |
4658 | _cleanup_set_free_free_ Set *symlink_names = NULL; |
4659 | _cleanup_fclose_ FILE *f = NULL; | |
4660 | _cleanup_free_ char *filename = NULL; | |
4661 | char *id = NULL; | |
23a177ef | 4662 | Unit *merged; |
45fb0699 | 4663 | struct stat st; |
a837f088 | 4664 | int r; |
23a177ef LP |
4665 | |
4666 | assert(u); | |
e537352b | 4667 | assert(path); |
3efd4195 | 4668 | |
d5099efc | 4669 | symlink_names = set_new(&string_hash_ops); |
f975e971 | 4670 | if (!symlink_names) |
87f0e418 | 4671 | return -ENOMEM; |
3efd4195 | 4672 | |
036643a2 LP |
4673 | if (path_is_absolute(path)) { |
4674 | ||
9946996c | 4675 | filename = strdup(path); |
e48614c4 ZJS |
4676 | if (!filename) |
4677 | return -ENOMEM; | |
036643a2 | 4678 | |
9946996c LP |
4679 | r = open_follow(&filename, &f, symlink_names, &id); |
4680 | if (r < 0) { | |
97b11eed | 4681 | filename = mfree(filename); |
036643a2 | 4682 | if (r != -ENOENT) |
e48614c4 | 4683 | return r; |
036643a2 LP |
4684 | } |
4685 | ||
4686 | } else { | |
4687 | char **p; | |
4688 | ||
a3c4eb07 | 4689 | STRV_FOREACH(p, u->manager->lookup_paths.search_path) { |
036643a2 LP |
4690 | |
4691 | /* Instead of opening the path right away, we manually | |
4692 | * follow all symlinks and add their name to our unit | |
4693 | * name set while doing so */ | |
9946996c | 4694 | filename = path_make_absolute(path, *p); |
e48614c4 ZJS |
4695 | if (!filename) |
4696 | return -ENOMEM; | |
036643a2 | 4697 | |
ac155bb8 MS |
4698 | if (u->manager->unit_path_cache && |
4699 | !set_get(u->manager->unit_path_cache, filename)) | |
fe51822e LP |
4700 | r = -ENOENT; |
4701 | else | |
4702 | r = open_follow(&filename, &f, symlink_names, &id); | |
a837f088 LP |
4703 | if (r >= 0) |
4704 | break; | |
4705 | filename = mfree(filename); | |
a1feacf7 ZJS |
4706 | |
4707 | /* ENOENT means that the file is missing or is a dangling symlink. | |
4708 | * ENOTDIR means that one of paths we expect to be is a directory | |
4709 | * is not a directory, we should just ignore that. | |
4710 | * EACCES means that the directory or file permissions are wrong. | |
4711 | */ | |
4712 | if (r == -EACCES) | |
4713 | log_debug_errno(r, "Cannot access \"%s\": %m", filename); | |
4714 | else if (!IN_SET(r, -ENOENT, -ENOTDIR)) | |
a837f088 | 4715 | return r; |
fe51822e | 4716 | |
a837f088 LP |
4717 | /* Empty the symlink names for the next run */ |
4718 | set_clear_free(symlink_names); | |
036643a2 LP |
4719 | } |
4720 | } | |
034c6ed7 | 4721 | |
e48614c4 | 4722 | if (!filename) |
8f05424d | 4723 | /* Hmm, no suitable file found? */ |
e48614c4 | 4724 | return 0; |
87f0e418 | 4725 | |
8a993b61 | 4726 | if (!unit_type_may_alias(u->type) && set_size(symlink_names) > 1) { |
a837f088 LP |
4727 | log_unit_warning(u, "Unit type of %s does not support alias names, refusing loading via symlink.", u->id); |
4728 | return -ELOOP; | |
4729 | } | |
4730 | ||
23a177ef | 4731 | merged = u; |
9946996c LP |
4732 | r = merge_by_names(&merged, symlink_names, id); |
4733 | if (r < 0) | |
e48614c4 | 4734 | return r; |
87f0e418 | 4735 | |
23a177ef | 4736 | if (merged != u) { |
ac155bb8 | 4737 | u->load_state = UNIT_MERGED; |
e48614c4 | 4738 | return 0; |
034c6ed7 LP |
4739 | } |
4740 | ||
e48614c4 ZJS |
4741 | if (fstat(fileno(f), &st) < 0) |
4742 | return -errno; | |
45fb0699 | 4743 | |
3a8db9fe | 4744 | if (null_or_empty(&st)) { |
ac155bb8 | 4745 | u->load_state = UNIT_MASKED; |
3a8db9fe ZJS |
4746 | u->fragment_mtime = 0; |
4747 | } else { | |
c2756a68 | 4748 | u->load_state = UNIT_LOADED; |
3a8db9fe | 4749 | u->fragment_mtime = timespec_load(&st.st_mtim); |
c2756a68 | 4750 | |
00dc5d76 | 4751 | /* Now, parse the file contents */ |
36f822c4 ZJS |
4752 | r = config_parse(u->id, filename, f, |
4753 | UNIT_VTABLE(u)->sections, | |
4754 | config_item_perf_lookup, load_fragment_gperf_lookup, | |
bcde742e | 4755 | CONFIG_PARSE_ALLOW_INCLUDE, u); |
f975e971 | 4756 | if (r < 0) |
e48614c4 | 4757 | return r; |
00dc5d76 | 4758 | } |
b08d03ff | 4759 | |
1cc6c93a | 4760 | free_and_replace(u->fragment_path, filename); |
87f0e418 | 4761 | |
1b64d026 LP |
4762 | if (u->source_path) { |
4763 | if (stat(u->source_path, &st) >= 0) | |
4764 | u->source_mtime = timespec_load(&st.st_mtim); | |
4765 | else | |
4766 | u->source_mtime = 0; | |
4767 | } | |
4768 | ||
e48614c4 | 4769 | return 0; |
0301abf4 LP |
4770 | } |
4771 | ||
e537352b | 4772 | int unit_load_fragment(Unit *u) { |
23a177ef | 4773 | int r; |
294d81f1 LP |
4774 | Iterator i; |
4775 | const char *t; | |
0301abf4 LP |
4776 | |
4777 | assert(u); | |
ac155bb8 MS |
4778 | assert(u->load_state == UNIT_STUB); |
4779 | assert(u->id); | |
23a177ef | 4780 | |
3f5e8115 LP |
4781 | if (u->transient) { |
4782 | u->load_state = UNIT_LOADED; | |
4783 | return 0; | |
4784 | } | |
4785 | ||
294d81f1 LP |
4786 | /* First, try to find the unit under its id. We always look |
4787 | * for unit files in the default directories, to make it easy | |
4788 | * to override things by placing things in /etc/systemd/system */ | |
9946996c LP |
4789 | r = load_from_path(u, u->id); |
4790 | if (r < 0) | |
294d81f1 LP |
4791 | return r; |
4792 | ||
4793 | /* Try to find an alias we can load this with */ | |
abc08d4d | 4794 | if (u->load_state == UNIT_STUB) { |
ac155bb8 | 4795 | SET_FOREACH(t, u->names, i) { |
294d81f1 | 4796 | |
ac155bb8 | 4797 | if (t == u->id) |
294d81f1 LP |
4798 | continue; |
4799 | ||
9946996c LP |
4800 | r = load_from_path(u, t); |
4801 | if (r < 0) | |
294d81f1 LP |
4802 | return r; |
4803 | ||
ac155bb8 | 4804 | if (u->load_state != UNIT_STUB) |
294d81f1 LP |
4805 | break; |
4806 | } | |
abc08d4d | 4807 | } |
23a177ef | 4808 | |
294d81f1 | 4809 | /* And now, try looking for it under the suggested (originally linked) path */ |
ac155bb8 | 4810 | if (u->load_state == UNIT_STUB && u->fragment_path) { |
6ccb1b44 | 4811 | |
9946996c LP |
4812 | r = load_from_path(u, u->fragment_path); |
4813 | if (r < 0) | |
23a177ef | 4814 | return r; |
0301abf4 | 4815 | |
ece174c5 | 4816 | if (u->load_state == UNIT_STUB) |
6ccb1b44 LP |
4817 | /* Hmm, this didn't work? Then let's get rid |
4818 | * of the fragment path stored for us, so that | |
4819 | * we don't point to an invalid location. */ | |
a1e58e8e | 4820 | u->fragment_path = mfree(u->fragment_path); |
6ccb1b44 LP |
4821 | } |
4822 | ||
294d81f1 | 4823 | /* Look for a template */ |
ac155bb8 | 4824 | if (u->load_state == UNIT_STUB && u->instance) { |
7410616c | 4825 | _cleanup_free_ char *k = NULL; |
294d81f1 | 4826 | |
7410616c LP |
4827 | r = unit_name_template(u->id, &k); |
4828 | if (r < 0) | |
4829 | return r; | |
294d81f1 LP |
4830 | |
4831 | r = load_from_path(u, k); | |
bb28e684 ZJS |
4832 | if (r < 0) { |
4833 | if (r == -ENOEXEC) | |
4834 | log_unit_notice(u, "Unit configuration has fatal error, unit will not be started."); | |
9e2f7c11 | 4835 | return r; |
bb28e684 | 4836 | } |
890f434c | 4837 | |
abc08d4d | 4838 | if (u->load_state == UNIT_STUB) { |
ac155bb8 | 4839 | SET_FOREACH(t, u->names, i) { |
bc9fd78c | 4840 | _cleanup_free_ char *z = NULL; |
87f0e418 | 4841 | |
ac155bb8 | 4842 | if (t == u->id) |
23a177ef | 4843 | continue; |
071830ff | 4844 | |
7410616c LP |
4845 | r = unit_name_template(t, &z); |
4846 | if (r < 0) | |
4847 | return r; | |
294d81f1 | 4848 | |
bc9fd78c | 4849 | r = load_from_path(u, z); |
294d81f1 | 4850 | if (r < 0) |
23a177ef | 4851 | return r; |
890f434c | 4852 | |
ac155bb8 | 4853 | if (u->load_state != UNIT_STUB) |
23a177ef LP |
4854 | break; |
4855 | } | |
abc08d4d | 4856 | } |
071830ff LP |
4857 | } |
4858 | ||
23a177ef | 4859 | return 0; |
3efd4195 | 4860 | } |
e537352b LP |
4861 | |
4862 | void unit_dump_config_items(FILE *f) { | |
f975e971 LP |
4863 | static const struct { |
4864 | const ConfigParserCallback callback; | |
4865 | const char *rvalue; | |
4866 | } table[] = { | |
17df7223 | 4867 | { config_parse_warn_compat, "NOTSUPPORTED" }, |
f975e971 LP |
4868 | { config_parse_int, "INTEGER" }, |
4869 | { config_parse_unsigned, "UNSIGNED" }, | |
5556b5fe | 4870 | { config_parse_iec_size, "SIZE" }, |
59f448cf | 4871 | { config_parse_iec_uint64, "SIZE" }, |
5556b5fe | 4872 | { config_parse_si_size, "SIZE" }, |
f975e971 LP |
4873 | { config_parse_bool, "BOOLEAN" }, |
4874 | { config_parse_string, "STRING" }, | |
4875 | { config_parse_path, "PATH" }, | |
4876 | { config_parse_unit_path_printf, "PATH" }, | |
4877 | { config_parse_strv, "STRING [...]" }, | |
4878 | { config_parse_exec_nice, "NICE" }, | |
4879 | { config_parse_exec_oom_score_adjust, "OOMSCOREADJUST" }, | |
4880 | { config_parse_exec_io_class, "IOCLASS" }, | |
4881 | { config_parse_exec_io_priority, "IOPRIORITY" }, | |
4882 | { config_parse_exec_cpu_sched_policy, "CPUSCHEDPOLICY" }, | |
4883 | { config_parse_exec_cpu_sched_prio, "CPUSCHEDPRIO" }, | |
4884 | { config_parse_exec_cpu_affinity, "CPUAFFINITY" }, | |
4885 | { config_parse_mode, "MODE" }, | |
4886 | { config_parse_unit_env_file, "FILE" }, | |
52c239d7 LB |
4887 | { config_parse_exec_output, "OUTPUT" }, |
4888 | { config_parse_exec_input, "INPUT" }, | |
ca37242e LP |
4889 | { config_parse_log_facility, "FACILITY" }, |
4890 | { config_parse_log_level, "LEVEL" }, | |
f975e971 | 4891 | { config_parse_exec_secure_bits, "SECUREBITS" }, |
a103496c | 4892 | { config_parse_capability_set, "BOUNDINGSET" }, |
4f424df7 | 4893 | { config_parse_rlimit, "LIMIT" }, |
f975e971 | 4894 | { config_parse_unit_deps, "UNIT [...]" }, |
f975e971 LP |
4895 | { config_parse_exec, "PATH [ARGUMENT [...]]" }, |
4896 | { config_parse_service_type, "SERVICETYPE" }, | |
4897 | { config_parse_service_restart, "SERVICERESTART" }, | |
f975e971 | 4898 | { config_parse_kill_mode, "KILLMODE" }, |
f757855e | 4899 | { config_parse_signal, "SIGNAL" }, |
f975e971 LP |
4900 | { config_parse_socket_listen, "SOCKET [...]" }, |
4901 | { config_parse_socket_bind, "SOCKETBIND" }, | |
4902 | { config_parse_socket_bindtodevice, "NETWORKINTERFACE" }, | |
7f602784 | 4903 | { config_parse_sec, "SECONDS" }, |
d88a251b | 4904 | { config_parse_nsec, "NANOSECONDS" }, |
94828d2d | 4905 | { config_parse_namespace_path_strv, "PATH [...]" }, |
d2d6c096 | 4906 | { config_parse_bind_paths, "PATH[:PATH[:OPTIONS]] [...]" }, |
7c8fa05c | 4907 | { config_parse_unit_requires_mounts_for, "PATH [...]" }, |
f975e971 LP |
4908 | { config_parse_exec_mount_flags, "MOUNTFLAG [...]" }, |
4909 | { config_parse_unit_string_printf, "STRING" }, | |
3ecaa09b | 4910 | { config_parse_trigger_unit, "UNIT" }, |
f975e971 | 4911 | { config_parse_timer, "TIMER" }, |
f975e971 | 4912 | { config_parse_path_spec, "PATH" }, |
f975e971 LP |
4913 | { config_parse_notify_access, "ACCESS" }, |
4914 | { config_parse_ip_tos, "TOS" }, | |
4915 | { config_parse_unit_condition_path, "CONDITION" }, | |
4916 | { config_parse_unit_condition_string, "CONDITION" }, | |
4917 | { config_parse_unit_condition_null, "CONDITION" }, | |
a016b922 | 4918 | { config_parse_unit_slice, "SLICE" }, |
7f0386f6 LP |
4919 | { config_parse_documentation, "URL" }, |
4920 | { config_parse_service_timeout, "SECONDS" }, | |
87a47f99 | 4921 | { config_parse_emergency_action, "ACTION" }, |
7f0386f6 LP |
4922 | { config_parse_set_status, "STATUS" }, |
4923 | { config_parse_service_sockets, "SOCKETS" }, | |
7f0386f6 | 4924 | { config_parse_environ, "ENVIRON" }, |
349cc4a5 | 4925 | #if HAVE_SECCOMP |
17df7223 | 4926 | { config_parse_syscall_filter, "SYSCALLS" }, |
6a6751fe | 4927 | { config_parse_syscall_archs, "ARCHS" }, |
17df7223 | 4928 | { config_parse_syscall_errno, "ERRNO" }, |
4298d0b5 | 4929 | { config_parse_address_families, "FAMILIES" }, |
add00535 | 4930 | { config_parse_restrict_namespaces, "NAMESPACES" }, |
c0467cf3 | 4931 | #endif |
7f0386f6 | 4932 | { config_parse_cpu_shares, "SHARES" }, |
66ebf6c0 | 4933 | { config_parse_cpu_weight, "WEIGHT" }, |
7f0386f6 LP |
4934 | { config_parse_memory_limit, "LIMIT" }, |
4935 | { config_parse_device_allow, "DEVICE" }, | |
4936 | { config_parse_device_policy, "POLICY" }, | |
13c31542 TH |
4937 | { config_parse_io_limit, "LIMIT" }, |
4938 | { config_parse_io_weight, "WEIGHT" }, | |
4939 | { config_parse_io_device_weight, "DEVICEWEIGHT" }, | |
7f0386f6 LP |
4940 | { config_parse_blockio_bandwidth, "BANDWIDTH" }, |
4941 | { config_parse_blockio_weight, "WEIGHT" }, | |
4942 | { config_parse_blockio_device_weight, "DEVICEWEIGHT" }, | |
4943 | { config_parse_long, "LONG" }, | |
4944 | { config_parse_socket_service, "SERVICE" }, | |
349cc4a5 | 4945 | #if HAVE_SELINUX |
6a6751fe LP |
4946 | { config_parse_exec_selinux_context, "LABEL" }, |
4947 | #endif | |
4948 | { config_parse_job_mode, "MODE" }, | |
4949 | { config_parse_job_mode_isolate, "BOOLEAN" }, | |
4298d0b5 | 4950 | { config_parse_personality, "PERSONALITY" }, |
f975e971 LP |
4951 | }; |
4952 | ||
4953 | const char *prev = NULL; | |
4954 | const char *i; | |
4955 | ||
4956 | assert(f); | |
e537352b | 4957 | |
f975e971 LP |
4958 | NULSTR_FOREACH(i, load_fragment_gperf_nulstr) { |
4959 | const char *rvalue = "OTHER", *lvalue; | |
313b7856 | 4960 | const ConfigPerfItem *p; |
f975e971 LP |
4961 | size_t prefix_len; |
4962 | const char *dot; | |
313b7856 | 4963 | unsigned j; |
f975e971 LP |
4964 | |
4965 | assert_se(p = load_fragment_gperf_lookup(i, strlen(i))); | |
4966 | ||
313b7856 LP |
4967 | /* Hide legacy settings */ |
4968 | if (p->parse == config_parse_warn_compat && | |
4969 | p->ltype == DISABLED_LEGACY) | |
4970 | continue; | |
4971 | ||
4972 | for (j = 0; j < ELEMENTSOF(table); j++) | |
4973 | if (p->parse == table[j].callback) { | |
4974 | rvalue = table[j].rvalue; | |
4975 | break; | |
4976 | } | |
4977 | ||
f975e971 LP |
4978 | dot = strchr(i, '.'); |
4979 | lvalue = dot ? dot + 1 : i; | |
4980 | prefix_len = dot-i; | |
4981 | ||
4982 | if (dot) | |
641906e9 | 4983 | if (!prev || !strneq(prev, i, prefix_len+1)) { |
f975e971 LP |
4984 | if (prev) |
4985 | fputc('\n', f); | |
4986 | ||
4987 | fprintf(f, "[%.*s]\n", (int) prefix_len, i); | |
4988 | } | |
4989 | ||
f975e971 LP |
4990 | fprintf(f, "%s=%s\n", lvalue, rvalue); |
4991 | prev = i; | |
4992 | } | |
e537352b | 4993 | } |