]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/load-fragment.c
cgroup: kill processes even in cgroups that aren't realized
[thirdparty/systemd.git] / src / load-fragment.c
CommitLineData
d6c9574f 1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
3efd4195 2
a7334b09
LP
3/***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
87f0e418 22#include <linux/oom.h>
3efd4195
LP
23#include <assert.h>
24#include <errno.h>
25#include <string.h>
87f0e418
LP
26#include <unistd.h>
27#include <fcntl.h>
94f04347
LP
28#include <sched.h>
29#include <sys/prctl.h>
15ae422b 30#include <sys/mount.h>
25e870b5 31#include <linux/fs.h>
45fb0699 32#include <sys/stat.h>
3d57c6ab
LP
33#include <sys/time.h>
34#include <sys/resource.h>
3efd4195 35
87f0e418 36#include "unit.h"
3efd4195
LP
37#include "strv.h"
38#include "conf-parser.h"
39#include "load-fragment.h"
16354eff 40#include "log.h"
9eba9da4 41#include "ioprio.h"
94f04347
LP
42#include "securebits.h"
43#include "missing.h"
9e2f7c11 44#include "unit-name.h"
398ef8ba 45#include "bus-errors.h"
3efd4195 46
07459bb6
FF
47#ifndef HAVE_SYSV_COMPAT
48static int config_parse_warn_compat(
49 const char *filename,
50 unsigned line,
51 const char *section,
52 const char *lvalue,
2b583ce6 53 int ltype,
07459bb6
FF
54 const char *rvalue,
55 void *data,
56 void *userdata) {
57
58 log_debug("[%s:%u] Support for option %s= has been disabled at compile time and is ignored", filename, line, lvalue);
59 return 0;
60}
61#endif
62
42f4e3c4 63static int config_parse_deps(
3efd4195
LP
64 const char *filename,
65 unsigned line,
66 const char *section,
67 const char *lvalue,
2b583ce6 68 int ltype,
3efd4195
LP
69 const char *rvalue,
70 void *data,
71 void *userdata) {
72
87f0e418
LP
73 UnitDependency d = PTR_TO_UINT(data);
74 Unit *u = userdata;
3efd4195
LP
75 char *w;
76 size_t l;
77 char *state;
78
79 assert(filename);
80 assert(lvalue);
81 assert(rvalue);
3efd4195 82
f60f22df 83 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
9e2f7c11 84 char *t, *k;
3efd4195 85 int r;
3efd4195
LP
86
87 if (!(t = strndup(w, l)))
88 return -ENOMEM;
89
9e2f7c11 90 k = unit_name_printf(u, t);
3efd4195
LP
91 free(t);
92
9e2f7c11
LP
93 if (!k)
94 return -ENOMEM;
95
701cc384 96 r = unit_add_dependency_by_name(u, d, k, NULL, true);
9e2f7c11 97
c0b34696
LP
98 if (r < 0) {
99 log_error("Failed to add dependency on %s, ignoring: %s", k, strerror(-r));
100 free(k);
101 return 0;
102 }
103
104 free(k);
3efd4195
LP
105 }
106
107 return 0;
108}
109
42f4e3c4 110static int config_parse_names(
87d1515d
LP
111 const char *filename,
112 unsigned line,
113 const char *section,
114 const char *lvalue,
2b583ce6 115 int ltype,
87d1515d
LP
116 const char *rvalue,
117 void *data,
118 void *userdata) {
119
87f0e418 120 Unit *u = userdata;
87d1515d
LP
121 char *w;
122 size_t l;
123 char *state;
124
125 assert(filename);
126 assert(lvalue);
127 assert(rvalue);
128 assert(data);
129
f60f22df 130 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
9e2f7c11 131 char *t, *k;
87d1515d 132 int r;
87d1515d
LP
133
134 if (!(t = strndup(w, l)))
135 return -ENOMEM;
136
9e2f7c11 137 k = unit_name_printf(u, t);
87d1515d 138 free(t);
23a177ef 139
9e2f7c11
LP
140 if (!k)
141 return -ENOMEM;
142
143 r = unit_merge_by_name(u, k);
9e2f7c11 144
c0b34696
LP
145 if (r < 0) {
146 log_error("Failed to add name %s, ignoring: %s", k, strerror(-r));
147 free(k);
148 return 0;
149 }
150
151 free(k);
87d1515d
LP
152 }
153
154 return 0;
155}
156
f2d3769a 157static int config_parse_string_printf(
932921b5
LP
158 const char *filename,
159 unsigned line,
160 const char *section,
161 const char *lvalue,
2b583ce6 162 int ltype,
932921b5
LP
163 const char *rvalue,
164 void *data,
165 void *userdata) {
166
167 Unit *u = userdata;
f2d3769a 168 char **s = data;
932921b5
LP
169 char *k;
170
171 assert(filename);
172 assert(lvalue);
173 assert(rvalue);
f2d3769a
LP
174 assert(s);
175 assert(u);
932921b5
LP
176
177 if (!(k = unit_full_printf(u, rvalue)))
178 return -ENOMEM;
179
f2d3769a 180 free(*s);
932921b5 181 if (*k)
f2d3769a 182 *s = k;
932921b5
LP
183 else {
184 free(k);
f2d3769a 185 *s = NULL;
932921b5
LP
186 }
187
188 return 0;
189}
190
6ea832a2
LP
191static int config_parse_path_printf(
192 const char *filename,
193 unsigned line,
194 const char *section,
195 const char *lvalue,
196 int ltype,
197 const char *rvalue,
198 void *data,
199 void *userdata) {
200
201 Unit *u = userdata;
202 char **s = data;
203 char *k;
204
205 assert(filename);
206 assert(lvalue);
207 assert(rvalue);
208 assert(s);
209 assert(u);
210
211 if (!(k = unit_full_printf(u, rvalue)))
212 return -ENOMEM;
213
214 if (!path_is_absolute(k)) {
215 log_error("[%s:%u] Not an absolute path: %s", filename, line, k);
216 free(k);
217 return -EINVAL;
218 }
219
220 path_kill_slashes(k);
221
222 free(*s);
223 *s = k;
224
225 return 0;
226}
227
42f4e3c4
LP
228static int config_parse_listen(
229 const char *filename,
230 unsigned line,
231 const char *section,
232 const char *lvalue,
2b583ce6 233 int ltype,
42f4e3c4
LP
234 const char *rvalue,
235 void *data,
236 void *userdata) {
237
49f91047 238 SocketPort *p, *tail;
542563ba 239 Socket *s;
16354eff 240
42f4e3c4
LP
241 assert(filename);
242 assert(lvalue);
243 assert(rvalue);
244 assert(data);
245
542563ba
LP
246 s = (Socket*) data;
247
248 if (!(p = new0(SocketPort, 1)))
249 return -ENOMEM;
250
251 if (streq(lvalue, "ListenFIFO")) {
252 p->type = SOCKET_FIFO;
253
254 if (!(p->path = strdup(rvalue))) {
255 free(p);
256 return -ENOMEM;
257 }
01f78473
LP
258
259 path_kill_slashes(p->path);
b0a3f2bc
LP
260
261 } else if (streq(lvalue, "ListenSpecial")) {
262 p->type = SOCKET_SPECIAL;
263
264 if (!(p->path = strdup(rvalue))) {
265 free(p);
266 return -ENOMEM;
267 }
268
269 path_kill_slashes(p->path);
270
916abb21
LP
271 } else if (streq(lvalue, "ListenMessageQueue")) {
272
273 p->type = SOCKET_MQUEUE;
274
275 if (!(p->path = strdup(rvalue))) {
276 free(p);
277 return -ENOMEM;
278 }
279
280 path_kill_slashes(p->path);
281
7a22745a
LP
282 } else if (streq(lvalue, "ListenNetlink")) {
283 p->type = SOCKET_SOCKET;
284
285 if (socket_address_parse_netlink(&p->address, rvalue) < 0) {
286 log_error("[%s:%u] Failed to parse address value, ignoring: %s", filename, line, rvalue);
287 free(p);
288 return 0;
289 }
290
542563ba
LP
291 } else {
292 p->type = SOCKET_SOCKET;
293
bd40a2d8 294 if (socket_address_parse(&p->address, rvalue) < 0) {
c0b34696 295 log_error("[%s:%u] Failed to parse address value, ignoring: %s", filename, line, rvalue);
542563ba 296 free(p);
c0b34696 297 return 0;
542563ba
LP
298 }
299
300 if (streq(lvalue, "ListenStream"))
301 p->address.type = SOCK_STREAM;
302 else if (streq(lvalue, "ListenDatagram"))
303 p->address.type = SOCK_DGRAM;
304 else {
305 assert(streq(lvalue, "ListenSequentialPacket"));
306 p->address.type = SOCK_SEQPACKET;
307 }
308
309 if (socket_address_family(&p->address) != AF_LOCAL && p->address.type == SOCK_SEQPACKET) {
c0b34696 310 log_error("[%s:%u] Address family not supported, ignoring: %s", filename, line, rvalue);
542563ba 311 free(p);
c0b34696 312 return 0;
542563ba 313 }
16354eff
LP
314 }
315
542563ba 316 p->fd = -1;
49f91047
LP
317
318 if (s->ports) {
319 LIST_FIND_TAIL(SocketPort, port, s->ports, tail);
320 LIST_INSERT_AFTER(SocketPort, port, s->ports, tail, p);
321 } else
322 LIST_PREPEND(SocketPort, port, s->ports, p);
542563ba 323
16354eff 324 return 0;
42f4e3c4
LP
325}
326
034c6ed7 327static int config_parse_socket_bind(
42f4e3c4
LP
328 const char *filename,
329 unsigned line,
330 const char *section,
331 const char *lvalue,
2b583ce6 332 int ltype,
42f4e3c4
LP
333 const char *rvalue,
334 void *data,
335 void *userdata) {
336
542563ba 337 Socket *s;
c0120d99 338 SocketAddressBindIPv6Only b;
42f4e3c4
LP
339
340 assert(filename);
341 assert(lvalue);
342 assert(rvalue);
343 assert(data);
344
542563ba
LP
345 s = (Socket*) data;
346
c0120d99
LP
347 if ((b = socket_address_bind_ipv6_only_from_string(rvalue)) < 0) {
348 int r;
349
350 if ((r = parse_boolean(rvalue)) < 0) {
c0b34696
LP
351 log_error("[%s:%u] Failed to parse bind IPv6 only value, ignoring: %s", filename, line, rvalue);
352 return 0;
c0120d99 353 }
42f4e3c4 354
c0120d99
LP
355 s->bind_ipv6_only = r ? SOCKET_ADDRESS_IPV6_ONLY : SOCKET_ADDRESS_BOTH;
356 } else
357 s->bind_ipv6_only = b;
542563ba 358
42f4e3c4
LP
359 return 0;
360}
361
034c6ed7
LP
362static int config_parse_nice(
363 const char *filename,
364 unsigned line,
365 const char *section,
366 const char *lvalue,
2b583ce6 367 int ltype,
034c6ed7
LP
368 const char *rvalue,
369 void *data,
370 void *userdata) {
371
fb33a393 372 ExecContext *c = data;
bd40a2d8 373 int priority;
034c6ed7
LP
374
375 assert(filename);
376 assert(lvalue);
377 assert(rvalue);
378 assert(data);
379
bd40a2d8 380 if (safe_atoi(rvalue, &priority) < 0) {
c0b34696
LP
381 log_error("[%s:%u] Failed to parse nice priority, ignoring: %s. ", filename, line, rvalue);
382 return 0;
034c6ed7
LP
383 }
384
385 if (priority < PRIO_MIN || priority >= PRIO_MAX) {
c0b34696
LP
386 log_error("[%s:%u] Nice priority out of range, ignoring: %s", filename, line, rvalue);
387 return 0;
034c6ed7
LP
388 }
389
fb33a393 390 c->nice = priority;
71155933 391 c->nice_set = true;
fb33a393 392
034c6ed7
LP
393 return 0;
394}
395
dd6c17b1 396static int config_parse_oom_score_adjust(
034c6ed7
LP
397 const char *filename,
398 unsigned line,
399 const char *section,
400 const char *lvalue,
2b583ce6 401 int ltype,
034c6ed7
LP
402 const char *rvalue,
403 void *data,
404 void *userdata) {
405
fb33a393 406 ExecContext *c = data;
bd40a2d8 407 int oa;
034c6ed7
LP
408
409 assert(filename);
410 assert(lvalue);
411 assert(rvalue);
412 assert(data);
413
bd40a2d8 414 if (safe_atoi(rvalue, &oa) < 0) {
dd6c17b1 415 log_error("[%s:%u] Failed to parse the OOM score adjust value, ignoring: %s", filename, line, rvalue);
c0b34696 416 return 0;
034c6ed7
LP
417 }
418
dd6c17b1
LP
419 if (oa < OOM_SCORE_ADJ_MIN || oa > OOM_SCORE_ADJ_MAX) {
420 log_error("[%s:%u] OOM score adjust value out of range, ignoring: %s", filename, line, rvalue);
c0b34696 421 return 0;
034c6ed7
LP
422 }
423
dd6c17b1
LP
424 c->oom_score_adjust = oa;
425 c->oom_score_adjust_set = true;
fb33a393 426
034c6ed7
LP
427 return 0;
428}
429
b5a0699f 430static int config_parse_mode(
034c6ed7
LP
431 const char *filename,
432 unsigned line,
433 const char *section,
434 const char *lvalue,
2b583ce6 435 int ltype,
034c6ed7
LP
436 const char *rvalue,
437 void *data,
438 void *userdata) {
439
440 mode_t *m = data;
441 long l;
442 char *x = NULL;
443
444 assert(filename);
445 assert(lvalue);
446 assert(rvalue);
447 assert(data);
448
449 errno = 0;
450 l = strtol(rvalue, &x, 8);
451 if (!x || *x || errno) {
c0b34696
LP
452 log_error("[%s:%u] Failed to parse mode value, ignoring: %s", filename, line, rvalue);
453 return 0;
034c6ed7
LP
454 }
455
b5a0699f 456 if (l < 0000 || l > 07777) {
c0b34696
LP
457 log_error("[%s:%u] mode value out of range, ignoring: %s", filename, line, rvalue);
458 return 0;
034c6ed7
LP
459 }
460
461 *m = (mode_t) l;
462 return 0;
463}
464
465static int config_parse_exec(
466 const char *filename,
467 unsigned line,
468 const char *section,
469 const char *lvalue,
2b583ce6 470 int ltype,
034c6ed7
LP
471 const char *rvalue,
472 void *data,
473 void *userdata) {
474
61e5d8ed
LP
475 ExecCommand **e = data, *nce;
476 char *path, **n;
034c6ed7 477 unsigned k;
034c6ed7
LP
478
479 assert(filename);
480 assert(lvalue);
481 assert(rvalue);
61e5d8ed 482 assert(e);
034c6ed7 483
6c666e26
LP
484 /* We accept an absolute path as first argument, or
485 * alternatively an absolute prefixed with @ to allow
486 * overriding of argv[0]. */
487
61e5d8ed
LP
488 for (;;) {
489 char *w;
490 size_t l;
491 char *state;
b708e7ce 492 bool honour_argv0 = false, ignore = false;
6c666e26 493
61e5d8ed
LP
494 path = NULL;
495 nce = NULL;
496 n = NULL;
6c666e26 497
61e5d8ed 498 rvalue += strspn(rvalue, WHITESPACE);
034c6ed7 499
61e5d8ed
LP
500 if (rvalue[0] == 0)
501 break;
034c6ed7 502
b708e7ce
LP
503 if (rvalue[0] == '-') {
504 ignore = true;
505 rvalue ++;
506 }
507
508 if (rvalue[0] == '@') {
509 honour_argv0 = true;
510 rvalue ++;
511 }
61e5d8ed 512
b708e7ce 513 if (*rvalue != '/') {
c0b34696
LP
514 log_error("[%s:%u] Invalid executable path in command line, ignoring: %s", filename, line, rvalue);
515 return 0;
6c666e26 516 }
034c6ed7 517
61e5d8ed
LP
518 k = 0;
519 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
f90cf44c 520 if (strncmp(w, ";", MAX(l, 1U)) == 0)
61e5d8ed 521 break;
034c6ed7 522
61e5d8ed
LP
523 k++;
524 }
034c6ed7 525
b708e7ce 526 if (!(n = new(char*, k + !honour_argv0)))
61e5d8ed
LP
527 return -ENOMEM;
528
529 k = 0;
61e5d8ed 530 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
f90cf44c 531 if (strncmp(w, ";", MAX(l, 1U)) == 0)
61e5d8ed
LP
532 break;
533
b708e7ce
LP
534 if (honour_argv0 && w == rvalue) {
535 assert(!path);
536 if (!(path = cunescape_length(w, l)))
61e5d8ed 537 goto fail;
61e5d8ed
LP
538 } else {
539 if (!(n[k++] = cunescape_length(w, l)))
540 goto fail;
541 }
542 }
543
544 n[k] = NULL;
545
546 if (!n[0]) {
c0b34696 547 log_error("[%s:%u] Invalid command line, ignoring: %s", filename, line, rvalue);
61e5d8ed 548 strv_free(n);
c0b34696 549 return 0;
61e5d8ed
LP
550 }
551
552 if (!path)
553 if (!(path = strdup(n[0])))
554 goto fail;
6c666e26 555
61e5d8ed 556 assert(path_is_absolute(path));
6c666e26 557
61e5d8ed
LP
558 if (!(nce = new0(ExecCommand, 1)))
559 goto fail;
560
561 nce->argv = n;
562 nce->path = path;
b708e7ce 563 nce->ignore = ignore;
034c6ed7 564
61e5d8ed 565 path_kill_slashes(nce->path);
034c6ed7 566
61e5d8ed 567 exec_command_append_list(e, nce);
01f78473 568
61e5d8ed
LP
569 rvalue = state;
570 }
034c6ed7
LP
571
572 return 0;
573
574fail:
6c666e26
LP
575 n[k] = NULL;
576 strv_free(n);
577 free(path);
034c6ed7
LP
578 free(nce);
579
580 return -ENOMEM;
581}
582
583static int config_parse_usec(
584 const char *filename,
585 unsigned line,
586 const char *section,
587 const char *lvalue,
2b583ce6 588 int ltype,
034c6ed7
LP
589 const char *rvalue,
590 void *data,
591 void *userdata) {
592
593 usec_t *usec = data;
034c6ed7
LP
594
595 assert(filename);
596 assert(lvalue);
597 assert(rvalue);
598 assert(data);
599
bd40a2d8 600 if (parse_usec(rvalue, usec) < 0) {
c0b34696
LP
601 log_error("[%s:%u] Failed to parse time value, ignoring: %s", filename, line, rvalue);
602 return 0;
034c6ed7
LP
603 }
604
034c6ed7
LP
605 return 0;
606}
607
487393e9
LP
608static DEFINE_CONFIG_PARSE_ENUM(config_parse_service_type, service_type, ServiceType, "Failed to parse service type");
609static DEFINE_CONFIG_PARSE_ENUM(config_parse_service_restart, service_restart, ServiceRestart, "Failed to parse service restart specifier");
034c6ed7 610
47be870b 611static int config_parse_bindtodevice(
acbb0225
LP
612 const char *filename,
613 unsigned line,
614 const char *section,
615 const char *lvalue,
2b583ce6 616 int ltype,
acbb0225
LP
617 const char *rvalue,
618 void *data,
619 void *userdata) {
620
621 Socket *s = data;
622 char *n;
623
624 assert(filename);
625 assert(lvalue);
626 assert(rvalue);
627 assert(data);
628
629 if (rvalue[0] && !streq(rvalue, "*")) {
630 if (!(n = strdup(rvalue)))
631 return -ENOMEM;
632 } else
633 n = NULL;
634
635 free(s->bind_to_device);
636 s->bind_to_device = n;
637
638 return 0;
639}
640
487393e9
LP
641static DEFINE_CONFIG_PARSE_ENUM(config_parse_output, exec_output, ExecOutput, "Failed to parse output specifier");
642static DEFINE_CONFIG_PARSE_ENUM(config_parse_input, exec_input, ExecInput, "Failed to parse input specifier");
87f0e418 643
47be870b 644static int config_parse_facility(
071830ff
LP
645 const char *filename,
646 unsigned line,
647 const char *section,
648 const char *lvalue,
2b583ce6 649 int ltype,
071830ff
LP
650 const char *rvalue,
651 void *data,
652 void *userdata) {
653
071830ff 654
94f04347 655 int *o = data, x;
071830ff
LP
656
657 assert(filename);
658 assert(lvalue);
659 assert(rvalue);
660 assert(data);
661
7d76f312 662 if ((x = log_facility_unshifted_from_string(rvalue)) < 0) {
c0b34696
LP
663 log_error("[%s:%u] Failed to parse log facility, ignoring: %s", filename, line, rvalue);
664 return 0;
0d87eb42 665 }
94f04347 666
7d76f312 667 *o = (x << 3) | LOG_PRI(*o);
071830ff
LP
668
669 return 0;
670}
671
47be870b 672static int config_parse_level(
071830ff
LP
673 const char *filename,
674 unsigned line,
675 const char *section,
676 const char *lvalue,
2b583ce6 677 int ltype,
071830ff
LP
678 const char *rvalue,
679 void *data,
680 void *userdata) {
681
071830ff 682
94f04347 683 int *o = data, x;
071830ff
LP
684
685 assert(filename);
686 assert(lvalue);
687 assert(rvalue);
688 assert(data);
689
0d87eb42 690 if ((x = log_level_from_string(rvalue)) < 0) {
c0b34696
LP
691 log_error("[%s:%u] Failed to parse log level, ignoring: %s", filename, line, rvalue);
692 return 0;
0d87eb42 693 }
071830ff 694
7d76f312 695 *o = (*o & LOG_FACMASK) | x;
94f04347
LP
696 return 0;
697}
698
47be870b 699static int config_parse_io_class(
94f04347
LP
700 const char *filename,
701 unsigned line,
702 const char *section,
703 const char *lvalue,
2b583ce6 704 int ltype,
94f04347
LP
705 const char *rvalue,
706 void *data,
707 void *userdata) {
708
709 ExecContext *c = data;
710 int x;
711
712 assert(filename);
713 assert(lvalue);
714 assert(rvalue);
715 assert(data);
716
0d87eb42 717 if ((x = ioprio_class_from_string(rvalue)) < 0) {
c0b34696
LP
718 log_error("[%s:%u] Failed to parse IO scheduling class, ignoring: %s", filename, line, rvalue);
719 return 0;
0d87eb42 720 }
94f04347
LP
721
722 c->ioprio = IOPRIO_PRIO_VALUE(x, IOPRIO_PRIO_DATA(c->ioprio));
723 c->ioprio_set = true;
724
725 return 0;
726}
727
47be870b 728static int config_parse_io_priority(
94f04347
LP
729 const char *filename,
730 unsigned line,
731 const char *section,
732 const char *lvalue,
2b583ce6 733 int ltype,
94f04347
LP
734 const char *rvalue,
735 void *data,
736 void *userdata) {
737
738 ExecContext *c = data;
739 int i;
740
741 assert(filename);
742 assert(lvalue);
743 assert(rvalue);
744 assert(data);
745
746 if (safe_atoi(rvalue, &i) < 0 || i < 0 || i >= IOPRIO_BE_NR) {
c0b34696
LP
747 log_error("[%s:%u] Failed to parse io priority, ignoring: %s", filename, line, rvalue);
748 return 0;
071830ff
LP
749 }
750
94f04347
LP
751 c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_PRIO_CLASS(c->ioprio), i);
752 c->ioprio_set = true;
753
071830ff
LP
754 return 0;
755}
756
47be870b 757static int config_parse_cpu_sched_policy(
9eba9da4
LP
758 const char *filename,
759 unsigned line,
760 const char *section,
761 const char *lvalue,
2b583ce6 762 int ltype,
9eba9da4
LP
763 const char *rvalue,
764 void *data,
765 void *userdata) {
766
94f04347
LP
767
768 ExecContext *c = data;
769 int x;
770
771 assert(filename);
772 assert(lvalue);
773 assert(rvalue);
774 assert(data);
775
0d87eb42 776 if ((x = sched_policy_from_string(rvalue)) < 0) {
c0b34696
LP
777 log_error("[%s:%u] Failed to parse CPU scheduling policy, ignoring: %s", filename, line, rvalue);
778 return 0;
0d87eb42 779 }
94f04347
LP
780
781 c->cpu_sched_policy = x;
782 c->cpu_sched_set = true;
783
784 return 0;
785}
786
47be870b 787static int config_parse_cpu_sched_prio(
94f04347
LP
788 const char *filename,
789 unsigned line,
790 const char *section,
791 const char *lvalue,
2b583ce6 792 int ltype,
94f04347
LP
793 const char *rvalue,
794 void *data,
795 void *userdata) {
9eba9da4
LP
796
797 ExecContext *c = data;
798 int i;
799
800 assert(filename);
801 assert(lvalue);
802 assert(rvalue);
803 assert(data);
804
94f04347
LP
805 /* On Linux RR/FIFO have the same range */
806 if (safe_atoi(rvalue, &i) < 0 || i < sched_get_priority_min(SCHED_RR) || i > sched_get_priority_max(SCHED_RR)) {
c0b34696
LP
807 log_error("[%s:%u] Failed to parse CPU scheduling priority, ignoring: %s", filename, line, rvalue);
808 return 0;
94f04347 809 }
9eba9da4 810
94f04347
LP
811 c->cpu_sched_priority = i;
812 c->cpu_sched_set = true;
813
814 return 0;
815}
816
47be870b 817static int config_parse_cpu_affinity(
94f04347
LP
818 const char *filename,
819 unsigned line,
820 const char *section,
821 const char *lvalue,
2b583ce6 822 int ltype,
94f04347
LP
823 const char *rvalue,
824 void *data,
825 void *userdata) {
826
827 ExecContext *c = data;
828 char *w;
829 size_t l;
830 char *state;
831
832 assert(filename);
833 assert(lvalue);
834 assert(rvalue);
835 assert(data);
836
f60f22df 837 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
94f04347
LP
838 char *t;
839 int r;
840 unsigned cpu;
841
842 if (!(t = strndup(w, l)))
843 return -ENOMEM;
844
487393e9
LP
845 r = safe_atou(t, &cpu);
846 free(t);
847
82c121a4
LP
848 if (!(c->cpuset))
849 if (!(c->cpuset = cpu_set_malloc(&c->cpuset_ncpus)))
850 return -ENOMEM;
851
82c121a4 852 if (r < 0 || cpu >= c->cpuset_ncpus) {
c0b34696
LP
853 log_error("[%s:%u] Failed to parse CPU affinity, ignoring: %s", filename, line, rvalue);
854 return 0;
9eba9da4 855 }
94f04347 856
82c121a4 857 CPU_SET_S(cpu, CPU_ALLOC_SIZE(c->cpuset_ncpus), c->cpuset);
9eba9da4
LP
858 }
859
94f04347
LP
860 return 0;
861}
862
47be870b 863static int config_parse_capabilities(
94f04347
LP
864 const char *filename,
865 unsigned line,
866 const char *section,
867 const char *lvalue,
2b583ce6 868 int ltype,
94f04347
LP
869 const char *rvalue,
870 void *data,
871 void *userdata) {
872
873 ExecContext *c = data;
874 cap_t cap;
875
876 assert(filename);
877 assert(lvalue);
878 assert(rvalue);
879 assert(data);
880
881 if (!(cap = cap_from_text(rvalue))) {
882 if (errno == ENOMEM)
883 return -ENOMEM;
884
c0b34696
LP
885 log_error("[%s:%u] Failed to parse capabilities, ignoring: %s", filename, line, rvalue);
886 return 0;
94f04347
LP
887 }
888
889 if (c->capabilities)
890 cap_free(c->capabilities);
891 c->capabilities = cap;
892
893 return 0;
894}
895
47be870b 896static int config_parse_secure_bits(
94f04347
LP
897 const char *filename,
898 unsigned line,
899 const char *section,
900 const char *lvalue,
2b583ce6 901 int ltype,
94f04347
LP
902 const char *rvalue,
903 void *data,
904 void *userdata) {
905
906 ExecContext *c = data;
907 char *w;
908 size_t l;
909 char *state;
910
911 assert(filename);
912 assert(lvalue);
913 assert(rvalue);
914 assert(data);
915
f60f22df 916 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
94f04347
LP
917 if (first_word(w, "keep-caps"))
918 c->secure_bits |= SECURE_KEEP_CAPS;
919 else if (first_word(w, "keep-caps-locked"))
920 c->secure_bits |= SECURE_KEEP_CAPS_LOCKED;
921 else if (first_word(w, "no-setuid-fixup"))
922 c->secure_bits |= SECURE_NO_SETUID_FIXUP;
923 else if (first_word(w, "no-setuid-fixup-locked"))
924 c->secure_bits |= SECURE_NO_SETUID_FIXUP_LOCKED;
925 else if (first_word(w, "noroot"))
926 c->secure_bits |= SECURE_NOROOT;
927 else if (first_word(w, "noroot-locked"))
928 c->secure_bits |= SECURE_NOROOT_LOCKED;
9eba9da4 929 else {
c0b34696
LP
930 log_error("[%s:%u] Failed to parse secure bits, ignoring: %s", filename, line, rvalue);
931 return 0;
9eba9da4
LP
932 }
933 }
934
94f04347
LP
935 return 0;
936}
937
47be870b 938static int config_parse_bounding_set(
94f04347
LP
939 const char *filename,
940 unsigned line,
941 const char *section,
942 const char *lvalue,
2b583ce6 943 int ltype,
94f04347
LP
944 const char *rvalue,
945 void *data,
946 void *userdata) {
947
948 ExecContext *c = data;
949 char *w;
950 size_t l;
951 char *state;
260abb78
LP
952 bool invert = false;
953 uint64_t sum = 0;
94f04347
LP
954
955 assert(filename);
956 assert(lvalue);
957 assert(rvalue);
958 assert(data);
959
260abb78
LP
960 if (rvalue[0] == '~') {
961 invert = true;
962 rvalue++;
963 }
964
965 /* Note that we store this inverted internally, since the
966 * kernel wants it like this. But we actually expose it
967 * non-inverted everywhere to have a fully normalized
968 * interface. */
969
f60f22df 970 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
94f04347
LP
971 char *t;
972 int r;
973 cap_value_t cap;
974
975 if (!(t = strndup(w, l)))
976 return -ENOMEM;
977
978 r = cap_from_name(t, &cap);
979 free(t);
980
981 if (r < 0) {
c0b34696
LP
982 log_error("[%s:%u] Failed to parse capability bounding set, ignoring: %s", filename, line, rvalue);
983 return 0;
94f04347
LP
984 }
985
260abb78 986 sum |= ((uint64_t) 1ULL) << (uint64_t) cap;
94f04347 987 }
9eba9da4 988
260abb78
LP
989 if (invert)
990 c->capability_bounding_set_drop |= sum;
991 else
992 c->capability_bounding_set_drop |= ~sum;
993
9eba9da4
LP
994 return 0;
995}
996
03fae018 997static int config_parse_timer_slack_nsec(
9eba9da4
LP
998 const char *filename,
999 unsigned line,
1000 const char *section,
1001 const char *lvalue,
2b583ce6 1002 int ltype,
9eba9da4
LP
1003 const char *rvalue,
1004 void *data,
1005 void *userdata) {
1006
1007 ExecContext *c = data;
94f04347 1008 unsigned long u;
9eba9da4
LP
1009
1010 assert(filename);
1011 assert(lvalue);
1012 assert(rvalue);
1013 assert(data);
1014
bd40a2d8 1015 if (safe_atolu(rvalue, &u) < 0) {
c0b34696
LP
1016 log_error("[%s:%u] Failed to parse time slack value, ignoring: %s", filename, line, rvalue);
1017 return 0;
9eba9da4
LP
1018 }
1019
03fae018 1020 c->timer_slack_nsec = u;
94f04347
LP
1021
1022 return 0;
1023}
1024
1025static int config_parse_limit(
1026 const char *filename,
1027 unsigned line,
1028 const char *section,
1029 const char *lvalue,
2b583ce6 1030 int ltype,
94f04347
LP
1031 const char *rvalue,
1032 void *data,
1033 void *userdata) {
1034
1035 struct rlimit **rl = data;
1036 unsigned long long u;
94f04347
LP
1037
1038 assert(filename);
1039 assert(lvalue);
1040 assert(rvalue);
1041 assert(data);
1042
3d57c6ab
LP
1043 if (streq(rvalue, "infinity"))
1044 u = (unsigned long long) RLIM_INFINITY;
1045 else if (safe_atollu(rvalue, &u) < 0) {
c0b34696
LP
1046 log_error("[%s:%u] Failed to parse resource value, ignoring: %s", filename, line, rvalue);
1047 return 0;
94f04347
LP
1048 }
1049
1050 if (!*rl)
1051 if (!(*rl = new(struct rlimit, 1)))
1052 return -ENOMEM;
9eba9da4 1053
94f04347 1054 (*rl)->rlim_cur = (*rl)->rlim_max = (rlim_t) u;
9eba9da4
LP
1055 return 0;
1056}
1057
8e274523
LP
1058static int config_parse_cgroup(
1059 const char *filename,
1060 unsigned line,
1061 const char *section,
1062 const char *lvalue,
2b583ce6 1063 int ltype,
8e274523
LP
1064 const char *rvalue,
1065 void *data,
1066 void *userdata) {
1067
1068 Unit *u = userdata;
1069 char *w;
1070 size_t l;
1071 char *state;
1072
f60f22df 1073 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
f284f69a 1074 char *t, *k;
8e274523
LP
1075 int r;
1076
f284f69a
LP
1077 t = strndup(w, l);
1078 if (!t)
1079 return -ENOMEM;
1080
1081 k = unit_full_printf(u, t);
1082 free(t);
1083
1084 if (!k)
1085 return -ENOMEM;
1086
1087 t = cunescape(k);
1088 free(k);
1089
1090 if (!t)
8e274523
LP
1091 return -ENOMEM;
1092
1093 r = unit_add_cgroup_from_text(u, t);
1094 free(t);
1095
c0b34696
LP
1096 if (r < 0) {
1097 log_error("[%s:%u] Failed to parse cgroup value, ignoring: %s", filename, line, rvalue);
1098 return 0;
1099 }
8e274523
LP
1100 }
1101
1102 return 0;
1103}
1104
07459bb6 1105#ifdef HAVE_SYSV_COMPAT
a9a1e00a
LP
1106static int config_parse_sysv_priority(
1107 const char *filename,
1108 unsigned line,
1109 const char *section,
1110 const char *lvalue,
2b583ce6 1111 int ltype,
a9a1e00a
LP
1112 const char *rvalue,
1113 void *data,
1114 void *userdata) {
1115
1116 int *priority = data;
bd40a2d8 1117 int i;
a9a1e00a
LP
1118
1119 assert(filename);
1120 assert(lvalue);
1121 assert(rvalue);
1122 assert(data);
1123
bd40a2d8 1124 if (safe_atoi(rvalue, &i) < 0 || i < 0) {
c0b34696
LP
1125 log_error("[%s:%u] Failed to parse SysV start priority, ignoring: %s", filename, line, rvalue);
1126 return 0;
a9a1e00a
LP
1127 }
1128
1129 *priority = (int) i;
1130 return 0;
1131}
07459bb6 1132#endif
a9a1e00a 1133
2ba545f1
LP
1134static int config_parse_fsck_passno(
1135 const char *filename,
1136 unsigned line,
1137 const char *section,
1138 const char *lvalue,
2b583ce6 1139 int ltype,
2ba545f1
LP
1140 const char *rvalue,
1141 void *data,
1142 void *userdata) {
1143
1144 int *passno = data;
bd40a2d8 1145 int i;
2ba545f1
LP
1146
1147 assert(filename);
1148 assert(lvalue);
1149 assert(rvalue);
1150 assert(data);
1151
bd40a2d8 1152 if (safe_atoi(rvalue, &i) || i < 0) {
2ba545f1
LP
1153 log_error("[%s:%u] Failed to parse fsck pass number, ignoring: %s", filename, line, rvalue);
1154 return 0;
1155 }
1156
1157 *passno = (int) i;
1158 return 0;
1159}
1160
487393e9 1161static DEFINE_CONFIG_PARSE_ENUM(config_parse_kill_mode, kill_mode, KillMode, "Failed to parse kill mode");
50159e6a 1162
2e22afe9
LP
1163static int config_parse_kill_signal(
1164 const char *filename,
1165 unsigned line,
1166 const char *section,
1167 const char *lvalue,
2b583ce6 1168 int ltype,
2e22afe9
LP
1169 const char *rvalue,
1170 void *data,
1171 void *userdata) {
1172
1173 int *sig = data;
1174 int r;
1175
1176 assert(filename);
1177 assert(lvalue);
1178 assert(rvalue);
1179 assert(sig);
1180
8a0867d6 1181 if ((r = signal_from_string_try_harder(rvalue)) <= 0) {
c0b34696
LP
1182 log_error("[%s:%u] Failed to parse kill signal, ignoring: %s", filename, line, rvalue);
1183 return 0;
2e22afe9
LP
1184 }
1185
1186 *sig = r;
1187 return 0;
1188}
1189
15ae422b
LP
1190static int config_parse_mount_flags(
1191 const char *filename,
1192 unsigned line,
1193 const char *section,
1194 const char *lvalue,
2b583ce6 1195 int ltype,
15ae422b
LP
1196 const char *rvalue,
1197 void *data,
1198 void *userdata) {
1199
1200 ExecContext *c = data;
1201 char *w;
1202 size_t l;
1203 char *state;
1204 unsigned long flags = 0;
1205
1206 assert(filename);
1207 assert(lvalue);
1208 assert(rvalue);
1209 assert(data);
1210
f60f22df 1211 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
f90cf44c 1212 if (strncmp(w, "shared", MAX(l, 6U)) == 0)
15ae422b 1213 flags |= MS_SHARED;
f90cf44c 1214 else if (strncmp(w, "slave", MAX(l, 5U)) == 0)
15ae422b 1215 flags |= MS_SLAVE;
f90cf44c 1216 else if (strncmp(w, "private", MAX(l, 7U)) == 0)
15ae422b
LP
1217 flags |= MS_PRIVATE;
1218 else {
c0b34696
LP
1219 log_error("[%s:%u] Failed to parse mount flags, ignoring: %s", filename, line, rvalue);
1220 return 0;
15ae422b
LP
1221 }
1222 }
1223
1224 c->mount_flags = flags;
1225 return 0;
1226}
1227
871d7de4
LP
1228static int config_parse_timer(
1229 const char *filename,
1230 unsigned line,
1231 const char *section,
1232 const char *lvalue,
2b583ce6 1233 int ltype,
871d7de4
LP
1234 const char *rvalue,
1235 void *data,
1236 void *userdata) {
1237
1238 Timer *t = data;
1239 usec_t u;
871d7de4
LP
1240 TimerValue *v;
1241 TimerBase b;
1242
1243 assert(filename);
1244 assert(lvalue);
1245 assert(rvalue);
1246 assert(data);
1247
1248 if ((b = timer_base_from_string(lvalue)) < 0) {
c0b34696
LP
1249 log_error("[%s:%u] Failed to parse timer base, ignoring: %s", filename, line, lvalue);
1250 return 0;
871d7de4
LP
1251 }
1252
bd40a2d8 1253 if (parse_usec(rvalue, &u) < 0) {
c0b34696
LP
1254 log_error("[%s:%u] Failed to parse timer value, ignoring: %s", filename, line, rvalue);
1255 return 0;
871d7de4
LP
1256 }
1257
1258 if (!(v = new0(TimerValue, 1)))
1259 return -ENOMEM;
1260
1261 v->base = b;
1262 v->value = u;
1263
1264 LIST_PREPEND(TimerValue, value, t->values, v);
1265
1266 return 0;
1267}
1268
1269static int config_parse_timer_unit(
1270 const char *filename,
1271 unsigned line,
1272 const char *section,
1273 const char *lvalue,
2b583ce6 1274 int ltype,
871d7de4
LP
1275 const char *rvalue,
1276 void *data,
1277 void *userdata) {
1278
1279 Timer *t = data;
1280 int r;
398ef8ba
LP
1281 DBusError error;
1282
1283 assert(filename);
1284 assert(lvalue);
1285 assert(rvalue);
1286 assert(data);
1287
1288 dbus_error_init(&error);
871d7de4
LP
1289
1290 if (endswith(rvalue, ".timer")) {
c0b34696
LP
1291 log_error("[%s:%u] Unit cannot be of type timer, ignoring: %s", filename, line, rvalue);
1292 return 0;
871d7de4
LP
1293 }
1294
398ef8ba 1295 if ((r = manager_load_unit(t->meta.manager, rvalue, NULL, NULL, &t->unit)) < 0) {
c0b34696 1296 log_error("[%s:%u] Failed to load unit %s, ignoring: %s", filename, line, rvalue, bus_error(&error, r));
398ef8ba 1297 dbus_error_free(&error);
c0b34696 1298 return 0;
871d7de4
LP
1299 }
1300
1301 return 0;
1302}
1303
01f78473
LP
1304static int config_parse_path_spec(
1305 const char *filename,
1306 unsigned line,
1307 const char *section,
1308 const char *lvalue,
2b583ce6 1309 int ltype,
01f78473
LP
1310 const char *rvalue,
1311 void *data,
1312 void *userdata) {
1313
1314 Path *p = data;
1315 PathSpec *s;
1316 PathType b;
1317
1318 assert(filename);
1319 assert(lvalue);
1320 assert(rvalue);
1321 assert(data);
1322
1323 if ((b = path_type_from_string(lvalue)) < 0) {
c0b34696
LP
1324 log_error("[%s:%u] Failed to parse path type, ignoring: %s", filename, line, lvalue);
1325 return 0;
01f78473
LP
1326 }
1327
1328 if (!path_is_absolute(rvalue)) {
c0b34696
LP
1329 log_error("[%s:%u] Path is not absolute, ignoring: %s", filename, line, rvalue);
1330 return 0;
01f78473
LP
1331 }
1332
1333 if (!(s = new0(PathSpec, 1)))
1334 return -ENOMEM;
1335
1336 if (!(s->path = strdup(rvalue))) {
1337 free(s);
1338 return -ENOMEM;
1339 }
1340
1341 path_kill_slashes(s->path);
1342
1343 s->type = b;
1344 s->inotify_fd = -1;
1345
1346 LIST_PREPEND(PathSpec, spec, p->specs, s);
1347
1348 return 0;
1349}
1350
1351static int config_parse_path_unit(
1352 const char *filename,
1353 unsigned line,
1354 const char *section,
1355 const char *lvalue,
2b583ce6 1356 int ltype,
01f78473
LP
1357 const char *rvalue,
1358 void *data,
1359 void *userdata) {
1360
1361 Path *t = data;
1362 int r;
398ef8ba
LP
1363 DBusError error;
1364
1365 assert(filename);
1366 assert(lvalue);
1367 assert(rvalue);
1368 assert(data);
1369
1370 dbus_error_init(&error);
01f78473
LP
1371
1372 if (endswith(rvalue, ".path")) {
c0b34696
LP
1373 log_error("[%s:%u] Unit cannot be of type path, ignoring: %s", filename, line, rvalue);
1374 return 0;
01f78473
LP
1375 }
1376
398ef8ba 1377 if ((r = manager_load_unit(t->meta.manager, rvalue, NULL, &error, &t->unit)) < 0) {
c0b34696 1378 log_error("[%s:%u] Failed to load unit %s, ignoring: %s", filename, line, rvalue, bus_error(&error, r));
398ef8ba 1379 dbus_error_free(&error);
c0b34696 1380 return 0;
01f78473
LP
1381 }
1382
1383 return 0;
1384}
1385
d9ff321a
LP
1386static int config_parse_socket_service(
1387 const char *filename,
1388 unsigned line,
1389 const char *section,
1390 const char *lvalue,
2b583ce6 1391 int ltype,
d9ff321a
LP
1392 const char *rvalue,
1393 void *data,
1394 void *userdata) {
1395
1396 Socket *s = data;
1397 int r;
1398 DBusError error;
1399
1400 assert(filename);
1401 assert(lvalue);
1402 assert(rvalue);
1403 assert(data);
1404
1405 dbus_error_init(&error);
1406
f976f3f6
LP
1407 if (!endswith(rvalue, ".service")) {
1408 log_error("[%s:%u] Unit must be of type service, ignoring: %s", filename, line, rvalue);
d9ff321a
LP
1409 return 0;
1410 }
1411
1412 if ((r = manager_load_unit(s->meta.manager, rvalue, NULL, &error, (Unit**) &s->service)) < 0) {
1413 log_error("[%s:%u] Failed to load unit %s, ignoring: %s", filename, line, rvalue, bus_error(&error, r));
1414 dbus_error_free(&error);
1415 return 0;
1416 }
1417
1418 return 0;
1419}
1420
f976f3f6
LP
1421static int config_parse_service_sockets(
1422 const char *filename,
1423 unsigned line,
1424 const char *section,
1425 const char *lvalue,
2b583ce6 1426 int ltype,
f976f3f6
LP
1427 const char *rvalue,
1428 void *data,
1429 void *userdata) {
1430
1431 Service *s = data;
1432 int r;
1433 DBusError error;
1434 char *state, *w;
1435 size_t l;
1436
1437 assert(filename);
1438 assert(lvalue);
1439 assert(rvalue);
1440 assert(data);
1441
1442 dbus_error_init(&error);
1443
1444 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
1445 char *t;
1446 Unit *sock;
1447
1448 if (!(t = strndup(w, l)))
1449 return -ENOMEM;
1450
1451 if (!endswith(t, ".socket")) {
1452 log_error("[%s:%u] Unit must be of type socket, ignoring: %s", filename, line, rvalue);
1453 free(t);
1454 continue;
1455 }
1456
1457 r = manager_load_unit(s->meta.manager, t, NULL, &error, &sock);
1458 free(t);
1459
1460 if (r < 0) {
1461 log_error("[%s:%u] Failed to load unit %s, ignoring: %s", filename, line, rvalue, bus_error(&error, r));
1462 dbus_error_free(&error);
1463 continue;
1464 }
1465
1466 if ((r = set_ensure_allocated(&s->configured_sockets, trivial_hash_func, trivial_compare_func)) < 0)
1467 return r;
1468
1469 if ((r = set_put(s->configured_sockets, sock)) < 0)
1470 return r;
1471 }
1472
1473 return 0;
1474}
1475
ddb26e18
LP
1476static int config_parse_env_file(
1477 const char *filename,
1478 unsigned line,
1479 const char *section,
1480 const char *lvalue,
2b583ce6 1481 int ltype,
ddb26e18
LP
1482 const char *rvalue,
1483 void *data,
1484 void *userdata) {
1485
8c7be95e 1486 char ***env = data, **k;
ddb26e18
LP
1487
1488 assert(filename);
1489 assert(lvalue);
1490 assert(rvalue);
1491 assert(data);
1492
8c7be95e 1493 if (!path_is_absolute(rvalue[0] == '-' ? rvalue + 1 : rvalue)) {
afe4bfe2
LP
1494 log_error("[%s:%u] Path '%s' is not absolute, ignoring.", filename, line, rvalue);
1495 return 0;
1496 }
1497
8c7be95e
LP
1498 if (!(k = strv_append(*env, rvalue)))
1499 return -ENOMEM;
ddb26e18 1500
8c7be95e
LP
1501 strv_free(*env);
1502 *env = k;
ddb26e18 1503
8c7be95e 1504 return 0;
ddb26e18
LP
1505}
1506
4fd5948e
LP
1507static int config_parse_ip_tos(
1508 const char *filename,
1509 unsigned line,
1510 const char *section,
1511 const char *lvalue,
2b583ce6 1512 int ltype,
4fd5948e
LP
1513 const char *rvalue,
1514 void *data,
1515 void *userdata) {
1516
1517 int *ip_tos = data, x;
4fd5948e
LP
1518
1519 assert(filename);
1520 assert(lvalue);
1521 assert(rvalue);
1522 assert(data);
1523
1524 if ((x = ip_tos_from_string(rvalue)) < 0)
bd40a2d8 1525 if (safe_atoi(rvalue, &x) < 0) {
c0b34696
LP
1526 log_error("[%s:%u] Failed to parse IP TOS value, ignoring: %s", filename, line, rvalue);
1527 return 0;
4fd5948e
LP
1528 }
1529
1530 *ip_tos = x;
1531 return 0;
1532}
1533
52661efd
LP
1534static int config_parse_condition_path(
1535 const char *filename,
1536 unsigned line,
1537 const char *section,
1538 const char *lvalue,
2b583ce6 1539 int ltype,
52661efd
LP
1540 const char *rvalue,
1541 void *data,
1542 void *userdata) {
1543
2b583ce6 1544 ConditionType cond = ltype;
52661efd 1545 Unit *u = data;
267632f0 1546 bool trigger, negate;
52661efd
LP
1547 Condition *c;
1548
1549 assert(filename);
1550 assert(lvalue);
1551 assert(rvalue);
1552 assert(data);
1553
267632f0
LP
1554 if ((trigger = rvalue[0] == '|'))
1555 rvalue++;
1556
52661efd
LP
1557 if ((negate = rvalue[0] == '!'))
1558 rvalue++;
1559
1560 if (!path_is_absolute(rvalue)) {
d257ddef 1561 log_error("[%s:%u] Path in condition not absolute, ignoring: %s", filename, line, rvalue);
52661efd
LP
1562 return 0;
1563 }
1564
2b583ce6 1565 if (!(c = condition_new(cond, rvalue, trigger, negate)))
52661efd
LP
1566 return -ENOMEM;
1567
1568 LIST_PREPEND(Condition, conditions, u->meta.conditions, c);
1569 return 0;
1570}
1571
41584525 1572static int config_parse_condition_string(
039655a4
LP
1573 const char *filename,
1574 unsigned line,
1575 const char *section,
1576 const char *lvalue,
2b583ce6 1577 int ltype,
039655a4
LP
1578 const char *rvalue,
1579 void *data,
1580 void *userdata) {
1581
41584525 1582 ConditionType cond = ltype;
039655a4 1583 Unit *u = data;
267632f0 1584 bool trigger, negate;
039655a4
LP
1585 Condition *c;
1586
1587 assert(filename);
1588 assert(lvalue);
1589 assert(rvalue);
1590 assert(data);
1591
267632f0
LP
1592 if ((trigger = rvalue[0] == '|'))
1593 rvalue++;
1594
039655a4
LP
1595 if ((negate = rvalue[0] == '!'))
1596 rvalue++;
1597
41584525 1598 if (!(c = condition_new(cond, rvalue, trigger, negate)))
039655a4
LP
1599 return -ENOMEM;
1600
1601 LIST_PREPEND(Condition, conditions, u->meta.conditions, c);
1602 return 0;
1603}
1604
d257ddef
LP
1605static int config_parse_condition_null(
1606 const char *filename,
1607 unsigned line,
1608 const char *section,
1609 const char *lvalue,
2b583ce6 1610 int ltype,
d257ddef
LP
1611 const char *rvalue,
1612 void *data,
1613 void *userdata) {
1614
1615 Unit *u = data;
1616 Condition *c;
267632f0 1617 bool trigger, negate;
d257ddef
LP
1618 int b;
1619
1620 assert(filename);
1621 assert(lvalue);
1622 assert(rvalue);
1623 assert(data);
1624
267632f0
LP
1625 if ((trigger = rvalue[0] == '|'))
1626 rvalue++;
1627
d257ddef
LP
1628 if ((negate = rvalue[0] == '!'))
1629 rvalue++;
1630
1631 if ((b = parse_boolean(rvalue)) < 0) {
1632 log_error("[%s:%u] Failed to parse boolean value in condition, ignoring: %s", filename, line, rvalue);
1633 return 0;
1634 }
1635
1636 if (!b)
1637 negate = !negate;
1638
267632f0 1639 if (!(c = condition_new(CONDITION_NULL, NULL, trigger, negate)))
d257ddef
LP
1640 return -ENOMEM;
1641
1642 LIST_PREPEND(Condition, conditions, u->meta.conditions, c);
1643 return 0;
1644}
1645
487393e9 1646static DEFINE_CONFIG_PARSE_ENUM(config_parse_notify_access, notify_access, NotifyAccess, "Failed to parse notify access specifier");
c952c6ec 1647
071830ff 1648#define FOLLOW_MAX 8
87f0e418 1649
9e2f7c11 1650static int open_follow(char **filename, FILE **_f, Set *names, char **_final) {
0301abf4 1651 unsigned c = 0;
87f0e418
LP
1652 int fd, r;
1653 FILE *f;
0301abf4 1654 char *id = NULL;
87f0e418
LP
1655
1656 assert(filename);
1657 assert(*filename);
1658 assert(_f);
1659 assert(names);
1660
0301abf4
LP
1661 /* This will update the filename pointer if the loaded file is
1662 * reached by a symlink. The old string will be freed. */
87f0e418 1663
0301abf4 1664 for (;;) {
2c7108c4 1665 char *target, *name;
87f0e418 1666
0301abf4
LP
1667 if (c++ >= FOLLOW_MAX)
1668 return -ELOOP;
1669
b08d03ff
LP
1670 path_kill_slashes(*filename);
1671
87f0e418 1672 /* Add the file name we are currently looking at to
8f05424d
LP
1673 * the names of this unit, but only if it is a valid
1674 * unit name. */
0301abf4 1675 name = file_name_from_path(*filename);
87f0e418 1676
15e11d81 1677 if (unit_name_is_valid(name, true)) {
8f05424d 1678
15e11d81
LP
1679 id = set_get(names, name);
1680 if (!id) {
1681 id = strdup(name);
1682 if (!id)
8f05424d 1683 return -ENOMEM;
87f0e418 1684
15e11d81
LP
1685 r = set_put(names, id);
1686 if (r < 0) {
8f05424d
LP
1687 free(id);
1688 return r;
1689 }
87f0e418 1690 }
87f0e418
LP
1691 }
1692
0301abf4
LP
1693 /* Try to open the file name, but don't if its a symlink */
1694 if ((fd = open(*filename, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW)) >= 0)
87f0e418
LP
1695 break;
1696
0301abf4
LP
1697 if (errno != ELOOP)
1698 return -errno;
1699
87f0e418 1700 /* Hmm, so this is a symlink. Let's read the name, and follow it manually */
2c7108c4 1701 if ((r = readlink_and_make_absolute(*filename, &target)) < 0)
0301abf4 1702 return r;
87f0e418 1703
0301abf4 1704 free(*filename);
2c7108c4 1705 *filename = target;
87f0e418
LP
1706 }
1707
8f05424d 1708 if (!(f = fdopen(fd, "re"))) {
87f0e418 1709 r = -errno;
9e2f7c11 1710 close_nointr_nofail(fd);
0301abf4 1711 return r;
87f0e418
LP
1712 }
1713
1714 *_f = f;
9e2f7c11 1715 *_final = id;
0301abf4 1716 return 0;
87f0e418
LP
1717}
1718
23a177ef
LP
1719static int merge_by_names(Unit **u, Set *names, const char *id) {
1720 char *k;
1721 int r;
1722
1723 assert(u);
1724 assert(*u);
1725 assert(names);
1726
1727 /* Let's try to add in all symlink names we found */
1728 while ((k = set_steal_first(names))) {
1729
1730 /* First try to merge in the other name into our
1731 * unit */
1732 if ((r = unit_merge_by_name(*u, k)) < 0) {
1733 Unit *other;
1734
1735 /* Hmm, we couldn't merge the other unit into
1736 * ours? Then let's try it the other way
1737 * round */
1738
1739 other = manager_get_unit((*u)->meta.manager, k);
1740 free(k);
1741
1742 if (other)
1743 if ((r = unit_merge(other, *u)) >= 0) {
1744 *u = other;
1745 return merge_by_names(u, names, NULL);
1746 }
1747
1748 return r;
1749 }
1750
1751 if (id == k)
1752 unit_choose_id(*u, id);
1753
1754 free(k);
1755 }
1756
1757 return 0;
1758}
1759
e537352b
LP
1760static void dump_items(FILE *f, const ConfigItem *items) {
1761 const ConfigItem *i;
1762 const char *prev_section = NULL;
1763 bool not_first = false;
1764
1765 struct {
1766 ConfigParserCallback callback;
1767 const char *rvalue;
1768 } table[] = {
1769 { config_parse_int, "INTEGER" },
1770 { config_parse_unsigned, "UNSIGNED" },
1771 { config_parse_size, "SIZE" },
1772 { config_parse_bool, "BOOLEAN" },
1773 { config_parse_string, "STRING" },
1774 { config_parse_path, "PATH" },
6ea832a2 1775 { config_parse_path_printf, "PATH" },
e537352b
LP
1776 { config_parse_strv, "STRING [...]" },
1777 { config_parse_nice, "NICE" },
dd6c17b1 1778 { config_parse_oom_score_adjust, "OOMSCOREADJUST" },
e537352b
LP
1779 { config_parse_io_class, "IOCLASS" },
1780 { config_parse_io_priority, "IOPRIORITY" },
1781 { config_parse_cpu_sched_policy, "CPUSCHEDPOLICY" },
1782 { config_parse_cpu_sched_prio, "CPUSCHEDPRIO" },
1783 { config_parse_cpu_affinity, "CPUAFFINITY" },
1784 { config_parse_mode, "MODE" },
ddb26e18 1785 { config_parse_env_file, "FILE" },
e537352b
LP
1786 { config_parse_output, "OUTPUT" },
1787 { config_parse_input, "INPUT" },
1788 { config_parse_facility, "FACILITY" },
1789 { config_parse_level, "LEVEL" },
1790 { config_parse_capabilities, "CAPABILITIES" },
1791 { config_parse_secure_bits, "SECUREBITS" },
1792 { config_parse_bounding_set, "BOUNDINGSET" },
03fae018 1793 { config_parse_timer_slack_nsec, "TIMERSLACK" },
e537352b
LP
1794 { config_parse_limit, "LIMIT" },
1795 { config_parse_cgroup, "CGROUP [...]" },
1796 { config_parse_deps, "UNIT [...]" },
1797 { config_parse_names, "UNIT [...]" },
1798 { config_parse_exec, "PATH [ARGUMENT [...]]" },
1799 { config_parse_service_type, "SERVICETYPE" },
1800 { config_parse_service_restart, "SERVICERESTART" },
07459bb6 1801#ifdef HAVE_SYSV_COMPAT
e537352b 1802 { config_parse_sysv_priority, "SYSVPRIORITY" },
07459bb6
FF
1803#else
1804 { config_parse_warn_compat, "NOTSUPPORTED" },
1805#endif
e537352b 1806 { config_parse_kill_mode, "KILLMODE" },
2e22afe9 1807 { config_parse_kill_signal, "SIGNAL" },
e537352b
LP
1808 { config_parse_listen, "SOCKET [...]" },
1809 { config_parse_socket_bind, "SOCKETBIND" },
93ef5e80
LP
1810 { config_parse_bindtodevice, "NETWORKINTERFACE" },
1811 { config_parse_usec, "SECONDS" },
1812 { config_parse_path_strv, "PATH [...]" },
932921b5 1813 { config_parse_mount_flags, "MOUNTFLAG [...]" },
f2d3769a 1814 { config_parse_string_printf, "STRING" },
871d7de4
LP
1815 { config_parse_timer, "TIMER" },
1816 { config_parse_timer_unit, "NAME" },
c952c6ec
LP
1817 { config_parse_path_spec, "PATH" },
1818 { config_parse_path_unit, "UNIT" },
8b03daeb
LP
1819 { config_parse_notify_access, "ACCESS" },
1820 { config_parse_ip_tos, "TOS" },
52661efd 1821 { config_parse_condition_path, "CONDITION" },
41584525 1822 { config_parse_condition_string, "CONDITION" },
d257ddef 1823 { config_parse_condition_null, "CONDITION" },
e537352b
LP
1824 };
1825
1826 assert(f);
1827 assert(items);
1828
1829 for (i = items; i->lvalue; i++) {
1830 unsigned j;
1831 const char *rvalue = "OTHER";
1832
1833 if (!streq_ptr(i->section, prev_section)) {
1834 if (!not_first)
1835 not_first = true;
1836 else
1837 fputc('\n', f);
1838
1839 fprintf(f, "[%s]\n", i->section);
1840 prev_section = i->section;
1841 }
1842
1843 for (j = 0; j < ELEMENTSOF(table); j++)
1844 if (i->parse == table[j].callback) {
1845 rvalue = table[j].rvalue;
1846 break;
1847 }
1848
1849 fprintf(f, "%s=%s\n", i->lvalue, rvalue);
1850 }
1851}
1852
1853static int load_from_path(Unit *u, const char *path) {
87f0e418
LP
1854
1855 static const char* const section_table[_UNIT_TYPE_MAX] = {
1856 [UNIT_SERVICE] = "Service",
1857 [UNIT_TIMER] = "Timer",
1858 [UNIT_SOCKET] = "Socket",
1859 [UNIT_TARGET] = "Target",
1860 [UNIT_DEVICE] = "Device",
1861 [UNIT_MOUNT] = "Mount",
1862 [UNIT_AUTOMOUNT] = "Automount",
07b0b134 1863 [UNIT_SNAPSHOT] = "Snapshot",
01f78473
LP
1864 [UNIT_SWAP] = "Swap",
1865 [UNIT_PATH] = "Path"
42f4e3c4
LP
1866 };
1867
034c6ed7 1868#define EXEC_CONTEXT_CONFIG_ITEMS(context, section) \
6ea832a2
LP
1869 { "WorkingDirectory", config_parse_path_printf, 0, &(context).working_directory, section }, \
1870 { "RootDirectory", config_parse_path_printf, 0, &(context).root_directory, section }, \
2b583ce6
KS
1871 { "User", config_parse_string_printf, 0, &(context).user, section }, \
1872 { "Group", config_parse_string_printf, 0, &(context).group, section }, \
1873 { "SupplementaryGroups", config_parse_strv, 0, &(context).supplementary_groups, section }, \
1874 { "Nice", config_parse_nice, 0, &(context), section }, \
1875 { "OOMScoreAdjust", config_parse_oom_score_adjust,0, &(context), section }, \
1876 { "IOSchedulingClass", config_parse_io_class, 0, &(context), section }, \
1877 { "IOSchedulingPriority", config_parse_io_priority, 0, &(context), section }, \
1878 { "CPUSchedulingPolicy", config_parse_cpu_sched_policy,0, &(context), section }, \
1879 { "CPUSchedulingPriority", config_parse_cpu_sched_prio, 0, &(context), section }, \
1880 { "CPUSchedulingResetOnFork", config_parse_bool, 0, &(context).cpu_sched_reset_on_fork, section }, \
1881 { "CPUAffinity", config_parse_cpu_affinity, 0, &(context), section }, \
1882 { "UMask", config_parse_mode, 0, &(context).umask, section }, \
1883 { "Environment", config_parse_strv, 0, &(context).environment, section }, \
1884 { "EnvironmentFile", config_parse_env_file, 0, &(context).environment_files, section }, \
1885 { "StandardInput", config_parse_input, 0, &(context).std_input, section }, \
1886 { "StandardOutput", config_parse_output, 0, &(context).std_output, section }, \
1887 { "StandardError", config_parse_output, 0, &(context).std_error, section }, \
6ea832a2
LP
1888 { "TTYPath", config_parse_path_printf, 0, &(context).tty_path, section }, \
1889 { "TTYReset", config_parse_bool, 0, &(context).tty_reset, section }, \
1890 { "TTYVHangup", config_parse_bool, 0, &(context).tty_vhangup, section }, \
1891 { "TTYVTDisallocate", config_parse_bool, 0, &(context).tty_vt_disallocate, section }, \
2b583ce6
KS
1892 { "SyslogIdentifier", config_parse_string_printf, 0, &(context).syslog_identifier, section }, \
1893 { "SyslogFacility", config_parse_facility, 0, &(context).syslog_priority, section }, \
1894 { "SyslogLevel", config_parse_level, 0, &(context).syslog_priority, section }, \
1895 { "SyslogLevelPrefix", config_parse_bool, 0, &(context).syslog_level_prefix, section }, \
1896 { "Capabilities", config_parse_capabilities, 0, &(context), section }, \
1897 { "SecureBits", config_parse_secure_bits, 0, &(context), section }, \
1898 { "CapabilityBoundingSet", config_parse_bounding_set, 0, &(context), section }, \
1899 { "TimerSlackNSec", config_parse_timer_slack_nsec,0, &(context), section }, \
1900 { "LimitCPU", config_parse_limit, 0, &(context).rlimit[RLIMIT_CPU], section }, \
1901 { "LimitFSIZE", config_parse_limit, 0, &(context).rlimit[RLIMIT_FSIZE], section }, \
1902 { "LimitDATA", config_parse_limit, 0, &(context).rlimit[RLIMIT_DATA], section }, \
1903 { "LimitSTACK", config_parse_limit, 0, &(context).rlimit[RLIMIT_STACK], section }, \
1904 { "LimitCORE", config_parse_limit, 0, &(context).rlimit[RLIMIT_CORE], section }, \
1905 { "LimitRSS", config_parse_limit, 0, &(context).rlimit[RLIMIT_RSS], section }, \
1906 { "LimitNOFILE", config_parse_limit, 0, &(context).rlimit[RLIMIT_NOFILE], section }, \
1907 { "LimitAS", config_parse_limit, 0, &(context).rlimit[RLIMIT_AS], section }, \
1908 { "LimitNPROC", config_parse_limit, 0, &(context).rlimit[RLIMIT_NPROC], section }, \
1909 { "LimitMEMLOCK", config_parse_limit, 0, &(context).rlimit[RLIMIT_MEMLOCK], section }, \
1910 { "LimitLOCKS", config_parse_limit, 0, &(context).rlimit[RLIMIT_LOCKS], section }, \
1911 { "LimitSIGPENDING", config_parse_limit, 0, &(context).rlimit[RLIMIT_SIGPENDING], section }, \
1912 { "LimitMSGQUEUE", config_parse_limit, 0, &(context).rlimit[RLIMIT_MSGQUEUE], section }, \
1913 { "LimitNICE", config_parse_limit, 0, &(context).rlimit[RLIMIT_NICE], section }, \
1914 { "LimitRTPRIO", config_parse_limit, 0, &(context).rlimit[RLIMIT_RTPRIO], section }, \
1915 { "LimitRTTIME", config_parse_limit, 0, &(context).rlimit[RLIMIT_RTTIME], section }, \
1916 { "ControlGroup", config_parse_cgroup, 0, u, section }, \
1917 { "ReadWriteDirectories", config_parse_path_strv, 0, &(context).read_write_dirs, section }, \
1918 { "ReadOnlyDirectories", config_parse_path_strv, 0, &(context).read_only_dirs, section }, \
1919 { "InaccessibleDirectories",config_parse_path_strv, 0, &(context).inaccessible_dirs, section }, \
1920 { "PrivateTmp", config_parse_bool, 0, &(context).private_tmp, section }, \
1921 { "MountFlags", config_parse_mount_flags, 0, &(context), section }, \
1922 { "TCPWrapName", config_parse_string_printf, 0, &(context).tcpwrap_name, section }, \
1923 { "PAMName", config_parse_string_printf, 0, &(context).pam_name, section }, \
1924 { "KillMode", config_parse_kill_mode, 0, &(context).kill_mode, section }, \
1925 { "KillSignal", config_parse_kill_signal, 0, &(context).kill_signal, section }, \
1926 { "SendSIGKILL", config_parse_bool, 0, &(context).send_sigkill, section }, \
64747e2d
LP
1927 { "UtmpIdentifier", config_parse_string_printf, 0, &(context).utmp_id, section }, \
1928 { "ControlGroupModify", config_parse_bool, 0, &(context).control_group_modify, section }
034c6ed7 1929
3efd4195 1930 const ConfigItem items[] = {
2b583ce6
KS
1931 { "Names", config_parse_names, 0, u, "Unit" },
1932 { "Description", config_parse_string_printf, 0, &u->meta.description, "Unit" },
1933 { "Requires", config_parse_deps, 0, UINT_TO_PTR(UNIT_REQUIRES), "Unit" },
1934 { "RequiresOverridable", config_parse_deps, 0, UINT_TO_PTR(UNIT_REQUIRES_OVERRIDABLE), "Unit" },
1935 { "Requisite", config_parse_deps, 0, UINT_TO_PTR(UNIT_REQUISITE), "Unit" },
1936 { "RequisiteOverridable", config_parse_deps, 0, UINT_TO_PTR(UNIT_REQUISITE_OVERRIDABLE), "Unit" },
1937 { "Wants", config_parse_deps, 0, UINT_TO_PTR(UNIT_WANTS), "Unit" },
1938 { "BindTo", config_parse_deps, 0, UINT_TO_PTR(UNIT_BIND_TO), "Unit" },
1939 { "Conflicts", config_parse_deps, 0, UINT_TO_PTR(UNIT_CONFLICTS), "Unit" },
1940 { "Before", config_parse_deps, 0, UINT_TO_PTR(UNIT_BEFORE), "Unit" },
1941 { "After", config_parse_deps, 0, UINT_TO_PTR(UNIT_AFTER), "Unit" },
1942 { "OnFailure", config_parse_deps, 0, UINT_TO_PTR(UNIT_ON_FAILURE), "Unit" },
1943 { "StopWhenUnneeded", config_parse_bool, 0, &u->meta.stop_when_unneeded, "Unit" },
1944 { "RefuseManualStart", config_parse_bool, 0, &u->meta.refuse_manual_start, "Unit" },
1945 { "RefuseManualStop", config_parse_bool, 0, &u->meta.refuse_manual_stop, "Unit" },
1946 { "AllowIsolate", config_parse_bool, 0, &u->meta.allow_isolate, "Unit" },
1947 { "DefaultDependencies", config_parse_bool, 0, &u->meta.default_dependencies, "Unit" },
222ae6a8 1948 { "OnFailureIsolate", config_parse_bool, 0, &u->meta.on_failure_isolate, "Unit" },
c8f4d764 1949 { "IgnoreOnIsolate", config_parse_bool, 0, &u->meta.ignore_on_isolate, "Unit" },
7a6000a6 1950 { "IgnoreOnSnapshot", config_parse_bool, 0, &u->meta.ignore_on_snapshot, "Unit" },
2b583ce6
KS
1951 { "JobTimeoutSec", config_parse_usec, 0, &u->meta.job_timeout, "Unit" },
1952 { "ConditionPathExists", config_parse_condition_path, CONDITION_PATH_EXISTS, u, "Unit" },
1953 { "ConditionPathIsDirectory", config_parse_condition_path, CONDITION_PATH_IS_DIRECTORY, u, "Unit" },
1954 { "ConditionDirectoryNotEmpty", config_parse_condition_path, CONDITION_DIRECTORY_NOT_EMPTY, u, "Unit" },
41584525
MS
1955 { "ConditionKernelCommandLine", config_parse_condition_string, CONDITION_KERNEL_COMMAND_LINE, u, "Unit" },
1956 { "ConditionVirtualization", config_parse_condition_string, CONDITION_VIRTUALIZATION, u, "Unit" },
07e833bc 1957 { "ConditionSecurity", config_parse_condition_string, CONDITION_SECURITY, u, "Unit" },
2b583ce6
KS
1958 { "ConditionNull", config_parse_condition_null, 0, u, "Unit" },
1959
6ea832a2 1960 { "PIDFile", config_parse_path_printf, 0, &u->service.pid_file, "Service" },
2b583ce6
KS
1961 { "ExecStartPre", config_parse_exec, 0, u->service.exec_command+SERVICE_EXEC_START_PRE, "Service" },
1962 { "ExecStart", config_parse_exec, 0, u->service.exec_command+SERVICE_EXEC_START, "Service" },
1963 { "ExecStartPost", config_parse_exec, 0, u->service.exec_command+SERVICE_EXEC_START_POST, "Service" },
1964 { "ExecReload", config_parse_exec, 0, u->service.exec_command+SERVICE_EXEC_RELOAD, "Service" },
1965 { "ExecStop", config_parse_exec, 0, u->service.exec_command+SERVICE_EXEC_STOP, "Service" },
1966 { "ExecStopPost", config_parse_exec, 0, u->service.exec_command+SERVICE_EXEC_STOP_POST, "Service" },
1967 { "RestartSec", config_parse_usec, 0, &u->service.restart_usec, "Service" },
1968 { "TimeoutSec", config_parse_usec, 0, &u->service.timeout_usec, "Service" },
1969 { "Type", config_parse_service_type, 0, &u->service.type, "Service" },
1970 { "Restart", config_parse_service_restart, 0, &u->service.restart, "Service" },
1971 { "PermissionsStartOnly", config_parse_bool, 0, &u->service.permissions_start_only, "Service" },
1972 { "RootDirectoryStartOnly", config_parse_bool, 0, &u->service.root_directory_start_only, "Service" },
1973 { "RemainAfterExit", config_parse_bool, 0, &u->service.remain_after_exit, "Service" },
1974 { "GuessMainPID", config_parse_bool, 0, &u->service.guess_main_pid, "Service" },
07459bb6 1975#ifdef HAVE_SYSV_COMPAT
2b583ce6 1976 { "SysVStartPriority", config_parse_sysv_priority, 0, &u->service.sysv_start_priority, "Service" },
07459bb6 1977#else
2b583ce6 1978 { "SysVStartPriority", config_parse_warn_compat, 0, NULL, "Service" },
07459bb6 1979#endif
2b583ce6
KS
1980 { "NonBlocking", config_parse_bool, 0, &u->service.exec_context.non_blocking, "Service" },
1981 { "BusName", config_parse_string_printf, 0, &u->service.bus_name, "Service" },
1982 { "NotifyAccess", config_parse_notify_access, 0, &u->service.notify_access, "Service" },
1983 { "Sockets", config_parse_service_sockets, 0, &u->service, "Service" },
1984 { "FsckPassNo", config_parse_fsck_passno, 0, &u->service.fsck_passno, "Service" },
87f0e418
LP
1985 EXEC_CONTEXT_CONFIG_ITEMS(u->service.exec_context, "Service"),
1986
2b583ce6
KS
1987 { "ListenStream", config_parse_listen, 0, &u->socket, "Socket" },
1988 { "ListenDatagram", config_parse_listen, 0, &u->socket, "Socket" },
1989 { "ListenSequentialPacket", config_parse_listen, 0, &u->socket, "Socket" },
1990 { "ListenFIFO", config_parse_listen, 0, &u->socket, "Socket" },
7a22745a 1991 { "ListenNetlink", config_parse_listen, 0, &u->socket, "Socket" },
b0a3f2bc 1992 { "ListenSpecial", config_parse_listen, 0, &u->socket, "Socket" },
916abb21 1993 { "ListenMessageQueue", config_parse_listen, 0, &u->socket, "Socket" },
2b583ce6
KS
1994 { "BindIPv6Only", config_parse_socket_bind, 0, &u->socket, "Socket" },
1995 { "Backlog", config_parse_unsigned, 0, &u->socket.backlog, "Socket" },
1996 { "BindToDevice", config_parse_bindtodevice, 0, &u->socket, "Socket" },
1997 { "ExecStartPre", config_parse_exec, 0, u->socket.exec_command+SOCKET_EXEC_START_PRE, "Socket" },
1998 { "ExecStartPost", config_parse_exec, 0, u->socket.exec_command+SOCKET_EXEC_START_POST, "Socket" },
1999 { "ExecStopPre", config_parse_exec, 0, u->socket.exec_command+SOCKET_EXEC_STOP_PRE, "Socket" },
2000 { "ExecStopPost", config_parse_exec, 0, u->socket.exec_command+SOCKET_EXEC_STOP_POST, "Socket" },
2001 { "TimeoutSec", config_parse_usec, 0, &u->socket.timeout_usec, "Socket" },
2002 { "DirectoryMode", config_parse_mode, 0, &u->socket.directory_mode, "Socket" },
2003 { "SocketMode", config_parse_mode, 0, &u->socket.socket_mode, "Socket" },
2004 { "Accept", config_parse_bool, 0, &u->socket.accept, "Socket" },
2005 { "MaxConnections", config_parse_unsigned, 0, &u->socket.max_connections, "Socket" },
2006 { "KeepAlive", config_parse_bool, 0, &u->socket.keep_alive, "Socket" },
2007 { "Priority", config_parse_int, 0, &u->socket.priority, "Socket" },
2008 { "ReceiveBuffer", config_parse_size, 0, &u->socket.receive_buffer, "Socket" },
2009 { "SendBuffer", config_parse_size, 0, &u->socket.send_buffer, "Socket" },
2010 { "IPTOS", config_parse_ip_tos, 0, &u->socket.ip_tos, "Socket" },
2011 { "IPTTL", config_parse_int, 0, &u->socket.ip_ttl, "Socket" },
2012 { "Mark", config_parse_int, 0, &u->socket.mark, "Socket" },
2013 { "PipeSize", config_parse_size, 0, &u->socket.pipe_size, "Socket" },
2014 { "FreeBind", config_parse_bool, 0, &u->socket.free_bind, "Socket" },
6b6d2dee 2015 { "Transparent", config_parse_bool, 0, &u->socket.transparent, "Socket" },
ec6370a2 2016 { "Broadcast", config_parse_bool, 0, &u->socket.broadcast, "Socket" },
2b583ce6 2017 { "TCPCongestion", config_parse_string, 0, &u->socket.tcp_congestion, "Socket" },
916abb21
LP
2018 { "MessageQueueMaxMessages", config_parse_long, 0, &u->socket.mq_maxmsg, "Socket" },
2019 { "MessageQueueMessageSize", config_parse_long, 0, &u->socket.mq_msgsize, "Socket" },
2b583ce6 2020 { "Service", config_parse_socket_service, 0, &u->socket, "Socket" },
87f0e418
LP
2021 EXEC_CONTEXT_CONFIG_ITEMS(u->socket.exec_context, "Socket"),
2022
2b583ce6
KS
2023 { "What", config_parse_string, 0, &u->mount.parameters_fragment.what, "Mount" },
2024 { "Where", config_parse_path, 0, &u->mount.where, "Mount" },
2025 { "Options", config_parse_string, 0, &u->mount.parameters_fragment.options, "Mount" },
2026 { "Type", config_parse_string, 0, &u->mount.parameters_fragment.fstype, "Mount" },
2027 { "TimeoutSec", config_parse_usec, 0, &u->mount.timeout_usec, "Mount" },
2028 { "DirectoryMode", config_parse_mode, 0, &u->mount.directory_mode, "Mount" },
e537352b 2029 EXEC_CONTEXT_CONFIG_ITEMS(u->mount.exec_context, "Mount"),
034c6ed7 2030
2b583ce6
KS
2031 { "Where", config_parse_path, 0, &u->automount.where, "Automount" },
2032 { "DirectoryMode", config_parse_mode, 0, &u->automount.directory_mode, "Automount" },
8d567588 2033
2b583ce6
KS
2034 { "What", config_parse_path, 0, &u->swap.parameters_fragment.what, "Swap" },
2035 { "Priority", config_parse_int, 0, &u->swap.parameters_fragment.priority, "Swap" },
2036 { "TimeoutSec", config_parse_usec, 0, &u->swap.timeout_usec, "Swap" },
2292707d 2037 EXEC_CONTEXT_CONFIG_ITEMS(u->swap.exec_context, "Swap"),
01f78473 2038
2b583ce6
KS
2039 { "OnActiveSec", config_parse_timer, 0, &u->timer, "Timer" },
2040 { "OnBootSec", config_parse_timer, 0, &u->timer, "Timer" },
2041 { "OnStartupSec", config_parse_timer, 0, &u->timer, "Timer" },
2042 { "OnUnitActiveSec", config_parse_timer, 0, &u->timer, "Timer" },
2043 { "OnUnitInactiveSec", config_parse_timer, 0, &u->timer, "Timer" },
2044 { "Unit", config_parse_timer_unit, 0, &u->timer, "Timer" },
07b0b134 2045
2b583ce6
KS
2046 { "PathExists", config_parse_path_spec, 0, &u->path, "Path" },
2047 { "PathChanged", config_parse_path_spec, 0, &u->path, "Path" },
2048 { "DirectoryNotEmpty", config_parse_path_spec, 0, &u->path, "Path" },
2049 { "Unit", config_parse_path_unit, 0, &u->path, "Path" },
0e456f97
LP
2050 { "MakeDirectory", config_parse_bool, 0, &u->path.make_directory, "Path" },
2051 { "DirectoryMode", config_parse_mode, 0, &u->path.directory_mode, "Path" },
871d7de4 2052
10e87ee7 2053 /* The [Install] section is ignored here. */
2b583ce6
KS
2054 { "Alias", NULL, 0, NULL, "Install" },
2055 { "WantedBy", NULL, 0, NULL, "Install" },
2056 { "Also", NULL, 0, NULL, "Install" },
10e87ee7 2057
2b583ce6 2058 { NULL, NULL, 0, NULL, NULL }
3efd4195
LP
2059 };
2060
034c6ed7 2061#undef EXEC_CONTEXT_CONFIG_ITEMS
42f4e3c4 2062
10e87ee7 2063 const char *sections[4];
0301abf4 2064 int r;
87f0e418 2065 Set *symlink_names;
23a177ef
LP
2066 FILE *f = NULL;
2067 char *filename = NULL, *id = NULL;
2068 Unit *merged;
45fb0699 2069 struct stat st;
23a177ef 2070
e537352b
LP
2071 if (!u) {
2072 /* Dirty dirty hack. */
2073 dump_items((FILE*) path, items);
2074 return 0;
2075 }
2076
23a177ef 2077 assert(u);
e537352b 2078 assert(path);
3efd4195 2079
09477267 2080 sections[0] = "Unit";
87f0e418 2081 sections[1] = section_table[u->meta.type];
10e87ee7
LP
2082 sections[2] = "Install";
2083 sections[3] = NULL;
42f4e3c4 2084
87f0e418
LP
2085 if (!(symlink_names = set_new(string_hash_func, string_compare_func)))
2086 return -ENOMEM;
3efd4195 2087
036643a2
LP
2088 if (path_is_absolute(path)) {
2089
2090 if (!(filename = strdup(path))) {
2091 r = -ENOMEM;
2092 goto finish;
2093 }
2094
2095 if ((r = open_follow(&filename, &f, symlink_names, &id)) < 0) {
2096 free(filename);
2097 filename = NULL;
2098
2099 if (r != -ENOENT)
2100 goto finish;
2101 }
2102
2103 } else {
2104 char **p;
2105
84e3543e 2106 STRV_FOREACH(p, u->meta.manager->lookup_paths.unit_path) {
036643a2
LP
2107
2108 /* Instead of opening the path right away, we manually
2109 * follow all symlinks and add their name to our unit
2110 * name set while doing so */
2111 if (!(filename = path_make_absolute(path, *p))) {
2112 r = -ENOMEM;
2113 goto finish;
2114 }
2115
fe51822e
LP
2116 if (u->meta.manager->unit_path_cache &&
2117 !set_get(u->meta.manager->unit_path_cache, filename))
2118 r = -ENOENT;
2119 else
2120 r = open_follow(&filename, &f, symlink_names, &id);
2121
2122 if (r < 0) {
036643a2
LP
2123 char *sn;
2124
2125 free(filename);
2126 filename = NULL;
2127
2128 if (r != -ENOENT)
2129 goto finish;
2130
2131 /* Empty the symlink names for the next run */
2132 while ((sn = set_steal_first(symlink_names)))
2133 free(sn);
3efd4195 2134
036643a2
LP
2135 continue;
2136 }
2137
2138 break;
2139 }
2140 }
034c6ed7 2141
036643a2 2142 if (!filename) {
8f05424d 2143 /* Hmm, no suitable file found? */
23a177ef 2144 r = 0;
0301abf4
LP
2145 goto finish;
2146 }
87f0e418 2147
23a177ef
LP
2148 merged = u;
2149 if ((r = merge_by_names(&merged, symlink_names, id)) < 0)
0301abf4 2150 goto finish;
87f0e418 2151
23a177ef 2152 if (merged != u) {
e537352b 2153 u->meta.load_state = UNIT_MERGED;
23a177ef
LP
2154 r = 0;
2155 goto finish;
034c6ed7
LP
2156 }
2157
45fb0699
LP
2158 zero(st);
2159 if (fstat(fileno(f), &st) < 0) {
2160 r = -errno;
2161 goto finish;
2162 }
2163
00dc5d76 2164 if (null_or_empty(&st))
6daf4f90 2165 u->meta.load_state = UNIT_MASKED;
00dc5d76
LP
2166 else {
2167 /* Now, parse the file contents */
2168 if ((r = config_parse(filename, f, sections, items, false, u)) < 0)
2169 goto finish;
2170
2171 u->meta.load_state = UNIT_LOADED;
2172 }
b08d03ff 2173
6be1e7d5
LP
2174 free(u->meta.fragment_path);
2175 u->meta.fragment_path = filename;
0301abf4 2176 filename = NULL;
87f0e418 2177
45fb0699
LP
2178 u->meta.fragment_mtime = timespec_load(&st.st_mtim);
2179
23a177ef 2180 r = 0;
87f0e418
LP
2181
2182finish:
53ec43c6 2183 set_free_free(symlink_names);
0301abf4
LP
2184 free(filename);
2185
23a177ef
LP
2186 if (f)
2187 fclose(f);
2188
0301abf4
LP
2189 return r;
2190}
2191
e537352b 2192int unit_load_fragment(Unit *u) {
23a177ef 2193 int r;
294d81f1
LP
2194 Iterator i;
2195 const char *t;
0301abf4
LP
2196
2197 assert(u);
294d81f1
LP
2198 assert(u->meta.load_state == UNIT_STUB);
2199 assert(u->meta.id);
23a177ef 2200
294d81f1
LP
2201 /* First, try to find the unit under its id. We always look
2202 * for unit files in the default directories, to make it easy
2203 * to override things by placing things in /etc/systemd/system */
2204 if ((r = load_from_path(u, u->meta.id)) < 0)
2205 return r;
2206
2207 /* Try to find an alias we can load this with */
2208 if (u->meta.load_state == UNIT_STUB)
2209 SET_FOREACH(t, u->meta.names, i) {
2210
2211 if (t == u->meta.id)
2212 continue;
2213
2214 if ((r = load_from_path(u, t)) < 0)
2215 return r;
2216
2217 if (u->meta.load_state != UNIT_STUB)
2218 break;
2219 }
23a177ef 2220
294d81f1 2221 /* And now, try looking for it under the suggested (originally linked) path */
6ccb1b44
LP
2222 if (u->meta.load_state == UNIT_STUB && u->meta.fragment_path) {
2223
e537352b 2224 if ((r = load_from_path(u, u->meta.fragment_path)) < 0)
23a177ef 2225 return r;
0301abf4 2226
6ccb1b44
LP
2227 if (u->meta.load_state == UNIT_STUB) {
2228 /* Hmm, this didn't work? Then let's get rid
2229 * of the fragment path stored for us, so that
2230 * we don't point to an invalid location. */
2231 free(u->meta.fragment_path);
2232 u->meta.fragment_path = NULL;
2233 }
2234 }
2235
294d81f1
LP
2236 /* Look for a template */
2237 if (u->meta.load_state == UNIT_STUB && u->meta.instance) {
2238 char *k;
2239
2240 if (!(k = unit_name_template(u->meta.id)))
2241 return -ENOMEM;
2242
2243 r = load_from_path(u, k);
2244 free(k);
0301abf4 2245
294d81f1 2246 if (r < 0)
9e2f7c11 2247 return r;
890f434c 2248
e537352b 2249 if (u->meta.load_state == UNIT_STUB)
23a177ef 2250 SET_FOREACH(t, u->meta.names, i) {
87f0e418 2251
9e2f7c11 2252 if (t == u->meta.id)
23a177ef 2253 continue;
071830ff 2254
294d81f1
LP
2255 if (!(k = unit_name_template(t)))
2256 return -ENOMEM;
2257
2258 r = load_from_path(u, k);
2259 free(k);
2260
2261 if (r < 0)
23a177ef 2262 return r;
890f434c 2263
e537352b 2264 if (u->meta.load_state != UNIT_STUB)
23a177ef
LP
2265 break;
2266 }
071830ff
LP
2267 }
2268
23a177ef 2269 return 0;
3efd4195 2270}
e537352b
LP
2271
2272void unit_dump_config_items(FILE *f) {
2273 /* OK, this wins a prize for extreme ugliness. */
2274
2275 load_from_path(NULL, (const void*) f);
2276}