]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/load-fragment.c
unit: consider only_by_dependency setting when clients ask whether a unit is startable
[thirdparty/systemd.git] / src / load-fragment.c
CommitLineData
3efd4195
LP
1/*-*- Mode: C; c-basic-offset: 8 -*-*/
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>
3efd4195 33
87f0e418 34#include "unit.h"
3efd4195
LP
35#include "strv.h"
36#include "conf-parser.h"
37#include "load-fragment.h"
16354eff 38#include "log.h"
9eba9da4 39#include "ioprio.h"
94f04347
LP
40#include "securebits.h"
41#include "missing.h"
9e2f7c11 42#include "unit-name.h"
398ef8ba 43#include "bus-errors.h"
3efd4195 44
ddb26e18 45#define COMMENTS "#;\n"
ddb26e18 46
42f4e3c4 47static int config_parse_deps(
3efd4195
LP
48 const char *filename,
49 unsigned line,
50 const char *section,
51 const char *lvalue,
52 const char *rvalue,
53 void *data,
54 void *userdata) {
55
87f0e418
LP
56 UnitDependency d = PTR_TO_UINT(data);
57 Unit *u = userdata;
3efd4195
LP
58 char *w;
59 size_t l;
60 char *state;
61
62 assert(filename);
63 assert(lvalue);
64 assert(rvalue);
3efd4195 65
f60f22df 66 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
9e2f7c11 67 char *t, *k;
3efd4195 68 int r;
3efd4195
LP
69
70 if (!(t = strndup(w, l)))
71 return -ENOMEM;
72
9e2f7c11 73 k = unit_name_printf(u, t);
3efd4195
LP
74 free(t);
75
9e2f7c11
LP
76 if (!k)
77 return -ENOMEM;
78
701cc384 79 r = unit_add_dependency_by_name(u, d, k, NULL, true);
9e2f7c11
LP
80 free(k);
81
3efd4195
LP
82 if (r < 0)
83 return r;
3efd4195
LP
84 }
85
86 return 0;
87}
88
42f4e3c4 89static int config_parse_names(
87d1515d
LP
90 const char *filename,
91 unsigned line,
92 const char *section,
93 const char *lvalue,
94 const char *rvalue,
95 void *data,
96 void *userdata) {
97
87f0e418 98 Unit *u = userdata;
87d1515d
LP
99 char *w;
100 size_t l;
101 char *state;
102
103 assert(filename);
104 assert(lvalue);
105 assert(rvalue);
106 assert(data);
107
f60f22df 108 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
9e2f7c11 109 char *t, *k;
87d1515d 110 int r;
87d1515d
LP
111
112 if (!(t = strndup(w, l)))
113 return -ENOMEM;
114
9e2f7c11 115 k = unit_name_printf(u, t);
87d1515d 116 free(t);
23a177ef 117
9e2f7c11
LP
118 if (!k)
119 return -ENOMEM;
120
121 r = unit_merge_by_name(u, k);
122 free(k);
123
23a177ef
LP
124 if (r < 0)
125 return r;
87d1515d
LP
126 }
127
128 return 0;
129}
130
f2d3769a 131static int config_parse_string_printf(
932921b5
LP
132 const char *filename,
133 unsigned line,
134 const char *section,
135 const char *lvalue,
136 const char *rvalue,
137 void *data,
138 void *userdata) {
139
140 Unit *u = userdata;
f2d3769a 141 char **s = data;
932921b5
LP
142 char *k;
143
144 assert(filename);
145 assert(lvalue);
146 assert(rvalue);
f2d3769a
LP
147 assert(s);
148 assert(u);
932921b5
LP
149
150 if (!(k = unit_full_printf(u, rvalue)))
151 return -ENOMEM;
152
f2d3769a 153 free(*s);
932921b5 154 if (*k)
f2d3769a 155 *s = k;
932921b5
LP
156 else {
157 free(k);
f2d3769a 158 *s = NULL;
932921b5
LP
159 }
160
161 return 0;
162}
163
42f4e3c4
LP
164static int config_parse_listen(
165 const char *filename,
166 unsigned line,
167 const char *section,
168 const char *lvalue,
169 const char *rvalue,
170 void *data,
171 void *userdata) {
172
16354eff 173 int r;
542563ba
LP
174 SocketPort *p;
175 Socket *s;
16354eff 176
42f4e3c4
LP
177 assert(filename);
178 assert(lvalue);
179 assert(rvalue);
180 assert(data);
181
542563ba
LP
182 s = (Socket*) data;
183
184 if (!(p = new0(SocketPort, 1)))
185 return -ENOMEM;
186
187 if (streq(lvalue, "ListenFIFO")) {
188 p->type = SOCKET_FIFO;
189
190 if (!(p->path = strdup(rvalue))) {
191 free(p);
192 return -ENOMEM;
193 }
01f78473
LP
194
195 path_kill_slashes(p->path);
542563ba
LP
196 } else {
197 p->type = SOCKET_SOCKET;
198
199 if ((r = socket_address_parse(&p->address, rvalue)) < 0) {
200 log_error("[%s:%u] Failed to parse address value: %s", filename, line, rvalue);
201 free(p);
202 return r;
203 }
204
205 if (streq(lvalue, "ListenStream"))
206 p->address.type = SOCK_STREAM;
207 else if (streq(lvalue, "ListenDatagram"))
208 p->address.type = SOCK_DGRAM;
209 else {
210 assert(streq(lvalue, "ListenSequentialPacket"));
211 p->address.type = SOCK_SEQPACKET;
212 }
213
214 if (socket_address_family(&p->address) != AF_LOCAL && p->address.type == SOCK_SEQPACKET) {
215 free(p);
216 return -EPROTONOSUPPORT;
217 }
16354eff
LP
218 }
219
542563ba 220 p->fd = -1;
034c6ed7 221 LIST_PREPEND(SocketPort, port, s->ports, p);
542563ba 222
16354eff 223 return 0;
42f4e3c4
LP
224}
225
034c6ed7 226static int config_parse_socket_bind(
42f4e3c4
LP
227 const char *filename,
228 unsigned line,
229 const char *section,
230 const char *lvalue,
231 const char *rvalue,
232 void *data,
233 void *userdata) {
234
542563ba 235 Socket *s;
c0120d99 236 SocketAddressBindIPv6Only b;
42f4e3c4
LP
237
238 assert(filename);
239 assert(lvalue);
240 assert(rvalue);
241 assert(data);
242
542563ba
LP
243 s = (Socket*) data;
244
c0120d99
LP
245 if ((b = socket_address_bind_ipv6_only_from_string(rvalue)) < 0) {
246 int r;
247
248 if ((r = parse_boolean(rvalue)) < 0) {
249 log_error("[%s:%u] Failed to parse bind IPv6 only value: %s", filename, line, rvalue);
250 return -EBADMSG;
251 }
42f4e3c4 252
c0120d99
LP
253 s->bind_ipv6_only = r ? SOCKET_ADDRESS_IPV6_ONLY : SOCKET_ADDRESS_BOTH;
254 } else
255 s->bind_ipv6_only = b;
542563ba 256
42f4e3c4
LP
257 return 0;
258}
259
034c6ed7
LP
260static int config_parse_nice(
261 const char *filename,
262 unsigned line,
263 const char *section,
264 const char *lvalue,
265 const char *rvalue,
266 void *data,
267 void *userdata) {
268
fb33a393
LP
269 ExecContext *c = data;
270 int priority, r;
034c6ed7
LP
271
272 assert(filename);
273 assert(lvalue);
274 assert(rvalue);
275 assert(data);
276
277 if ((r = safe_atoi(rvalue, &priority)) < 0) {
278 log_error("[%s:%u] Failed to parse nice priority: %s", filename, line, rvalue);
279 return r;
280 }
281
282 if (priority < PRIO_MIN || priority >= PRIO_MAX) {
283 log_error("[%s:%u] Nice priority out of range: %s", filename, line, rvalue);
284 return -ERANGE;
285 }
286
fb33a393
LP
287 c->nice = priority;
288 c->nice_set = false;
289
034c6ed7
LP
290 return 0;
291}
292
293static int config_parse_oom_adjust(
294 const char *filename,
295 unsigned line,
296 const char *section,
297 const char *lvalue,
298 const char *rvalue,
299 void *data,
300 void *userdata) {
301
fb33a393
LP
302 ExecContext *c = data;
303 int oa, r;
034c6ed7
LP
304
305 assert(filename);
306 assert(lvalue);
307 assert(rvalue);
308 assert(data);
309
310 if ((r = safe_atoi(rvalue, &oa)) < 0) {
311 log_error("[%s:%u] Failed to parse OOM adjust value: %s", filename, line, rvalue);
312 return r;
313 }
314
315 if (oa < OOM_DISABLE || oa > OOM_ADJUST_MAX) {
316 log_error("[%s:%u] OOM adjust value out of range: %s", filename, line, rvalue);
317 return -ERANGE;
318 }
319
fb33a393
LP
320 c->oom_adjust = oa;
321 c->oom_adjust_set = true;
322
034c6ed7
LP
323 return 0;
324}
325
b5a0699f 326static int config_parse_mode(
034c6ed7
LP
327 const char *filename,
328 unsigned line,
329 const char *section,
330 const char *lvalue,
331 const char *rvalue,
332 void *data,
333 void *userdata) {
334
335 mode_t *m = data;
336 long l;
337 char *x = NULL;
338
339 assert(filename);
340 assert(lvalue);
341 assert(rvalue);
342 assert(data);
343
344 errno = 0;
345 l = strtol(rvalue, &x, 8);
346 if (!x || *x || errno) {
b5a0699f 347 log_error("[%s:%u] Failed to parse mode value: %s", filename, line, rvalue);
034c6ed7
LP
348 return errno ? -errno : -EINVAL;
349 }
350
b5a0699f
LP
351 if (l < 0000 || l > 07777) {
352 log_error("[%s:%u] mode value out of range: %s", filename, line, rvalue);
034c6ed7
LP
353 return -ERANGE;
354 }
355
356 *m = (mode_t) l;
357 return 0;
358}
359
360static int config_parse_exec(
361 const char *filename,
362 unsigned line,
363 const char *section,
364 const char *lvalue,
365 const char *rvalue,
366 void *data,
367 void *userdata) {
368
61e5d8ed
LP
369 ExecCommand **e = data, *nce;
370 char *path, **n;
034c6ed7 371 unsigned k;
034c6ed7
LP
372
373 assert(filename);
374 assert(lvalue);
375 assert(rvalue);
61e5d8ed 376 assert(e);
034c6ed7 377
6c666e26
LP
378 /* We accept an absolute path as first argument, or
379 * alternatively an absolute prefixed with @ to allow
380 * overriding of argv[0]. */
381
61e5d8ed
LP
382 for (;;) {
383 char *w;
384 size_t l;
385 char *state;
b708e7ce 386 bool honour_argv0 = false, ignore = false;
6c666e26 387
61e5d8ed
LP
388 path = NULL;
389 nce = NULL;
390 n = NULL;
6c666e26 391
61e5d8ed 392 rvalue += strspn(rvalue, WHITESPACE);
034c6ed7 393
61e5d8ed
LP
394 if (rvalue[0] == 0)
395 break;
034c6ed7 396
b708e7ce
LP
397 if (rvalue[0] == '-') {
398 ignore = true;
399 rvalue ++;
400 }
401
402 if (rvalue[0] == '@') {
403 honour_argv0 = true;
404 rvalue ++;
405 }
61e5d8ed 406
b708e7ce 407 if (*rvalue != '/') {
61e5d8ed
LP
408 log_error("[%s:%u] Invalid executable path in command line: %s", filename, line, rvalue);
409 return -EINVAL;
6c666e26 410 }
034c6ed7 411
61e5d8ed
LP
412 k = 0;
413 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
414 if (strncmp(w, ";", l) == 0)
415 break;
034c6ed7 416
61e5d8ed
LP
417 k++;
418 }
034c6ed7 419
b708e7ce 420 if (!(n = new(char*, k + !honour_argv0)))
61e5d8ed
LP
421 return -ENOMEM;
422
423 k = 0;
61e5d8ed
LP
424 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
425 if (strncmp(w, ";", l) == 0)
426 break;
427
b708e7ce
LP
428 if (honour_argv0 && w == rvalue) {
429 assert(!path);
430 if (!(path = cunescape_length(w, l)))
61e5d8ed 431 goto fail;
61e5d8ed
LP
432 } else {
433 if (!(n[k++] = cunescape_length(w, l)))
434 goto fail;
435 }
436 }
437
438 n[k] = NULL;
439
440 if (!n[0]) {
441 log_error("[%s:%u] Invalid command line: %s", filename, line, rvalue);
442 strv_free(n);
443 return -EINVAL;
444 }
445
446 if (!path)
447 if (!(path = strdup(n[0])))
448 goto fail;
6c666e26 449
61e5d8ed 450 assert(path_is_absolute(path));
6c666e26 451
61e5d8ed
LP
452 if (!(nce = new0(ExecCommand, 1)))
453 goto fail;
454
455 nce->argv = n;
456 nce->path = path;
b708e7ce 457 nce->ignore = ignore;
034c6ed7 458
61e5d8ed 459 path_kill_slashes(nce->path);
034c6ed7 460
61e5d8ed 461 exec_command_append_list(e, nce);
01f78473 462
61e5d8ed
LP
463 rvalue = state;
464 }
034c6ed7
LP
465
466 return 0;
467
468fail:
6c666e26
LP
469 n[k] = NULL;
470 strv_free(n);
471 free(path);
034c6ed7
LP
472 free(nce);
473
474 return -ENOMEM;
475}
476
477static int config_parse_usec(
478 const char *filename,
479 unsigned line,
480 const char *section,
481 const char *lvalue,
482 const char *rvalue,
483 void *data,
484 void *userdata) {
485
486 usec_t *usec = data;
034c6ed7
LP
487 int r;
488
489 assert(filename);
490 assert(lvalue);
491 assert(rvalue);
492 assert(data);
493
24a6e4a4 494 if ((r = parse_usec(rvalue, usec)) < 0) {
034c6ed7
LP
495 log_error("[%s:%u] Failed to parse time value: %s", filename, line, rvalue);
496 return r;
497 }
498
034c6ed7
LP
499 return 0;
500}
501
487393e9
LP
502static DEFINE_CONFIG_PARSE_ENUM(config_parse_service_type, service_type, ServiceType, "Failed to parse service type");
503static DEFINE_CONFIG_PARSE_ENUM(config_parse_service_restart, service_restart, ServiceRestart, "Failed to parse service restart specifier");
034c6ed7 504
47be870b 505static int config_parse_bindtodevice(
acbb0225
LP
506 const char *filename,
507 unsigned line,
508 const char *section,
509 const char *lvalue,
510 const char *rvalue,
511 void *data,
512 void *userdata) {
513
514 Socket *s = data;
515 char *n;
516
517 assert(filename);
518 assert(lvalue);
519 assert(rvalue);
520 assert(data);
521
522 if (rvalue[0] && !streq(rvalue, "*")) {
523 if (!(n = strdup(rvalue)))
524 return -ENOMEM;
525 } else
526 n = NULL;
527
528 free(s->bind_to_device);
529 s->bind_to_device = n;
530
531 return 0;
532}
533
487393e9
LP
534static DEFINE_CONFIG_PARSE_ENUM(config_parse_output, exec_output, ExecOutput, "Failed to parse output specifier");
535static DEFINE_CONFIG_PARSE_ENUM(config_parse_input, exec_input, ExecInput, "Failed to parse input specifier");
87f0e418 536
47be870b 537static int config_parse_facility(
071830ff
LP
538 const char *filename,
539 unsigned line,
540 const char *section,
541 const char *lvalue,
542 const char *rvalue,
543 void *data,
544 void *userdata) {
545
071830ff 546
94f04347 547 int *o = data, x;
071830ff
LP
548
549 assert(filename);
550 assert(lvalue);
551 assert(rvalue);
552 assert(data);
553
0d87eb42
LP
554 if ((x = log_facility_from_string(rvalue)) < 0) {
555 log_error("[%s:%u] Failed to parse log facility: %s", filename, line, rvalue);
556 return -EBADMSG;
557 }
94f04347
LP
558
559 *o = LOG_MAKEPRI(x, LOG_PRI(*o));
071830ff
LP
560
561 return 0;
562}
563
47be870b 564static int config_parse_level(
071830ff
LP
565 const char *filename,
566 unsigned line,
567 const char *section,
568 const char *lvalue,
569 const char *rvalue,
570 void *data,
571 void *userdata) {
572
071830ff 573
94f04347 574 int *o = data, x;
071830ff
LP
575
576 assert(filename);
577 assert(lvalue);
578 assert(rvalue);
579 assert(data);
580
0d87eb42
LP
581 if ((x = log_level_from_string(rvalue)) < 0) {
582 log_error("[%s:%u] Failed to parse log level: %s", filename, line, rvalue);
583 return -EBADMSG;
584 }
071830ff 585
94f04347
LP
586 *o = LOG_MAKEPRI(LOG_FAC(*o), x);
587 return 0;
588}
589
47be870b 590static int config_parse_io_class(
94f04347
LP
591 const char *filename,
592 unsigned line,
593 const char *section,
594 const char *lvalue,
595 const char *rvalue,
596 void *data,
597 void *userdata) {
598
599 ExecContext *c = data;
600 int x;
601
602 assert(filename);
603 assert(lvalue);
604 assert(rvalue);
605 assert(data);
606
0d87eb42
LP
607 if ((x = ioprio_class_from_string(rvalue)) < 0) {
608 log_error("[%s:%u] Failed to parse IO scheduling class: %s", filename, line, rvalue);
609 return -EBADMSG;
610 }
94f04347
LP
611
612 c->ioprio = IOPRIO_PRIO_VALUE(x, IOPRIO_PRIO_DATA(c->ioprio));
613 c->ioprio_set = true;
614
615 return 0;
616}
617
47be870b 618static int config_parse_io_priority(
94f04347
LP
619 const char *filename,
620 unsigned line,
621 const char *section,
622 const char *lvalue,
623 const char *rvalue,
624 void *data,
625 void *userdata) {
626
627 ExecContext *c = data;
628 int i;
629
630 assert(filename);
631 assert(lvalue);
632 assert(rvalue);
633 assert(data);
634
635 if (safe_atoi(rvalue, &i) < 0 || i < 0 || i >= IOPRIO_BE_NR) {
636 log_error("[%s:%u] Failed to parse io priority: %s", filename, line, rvalue);
637 return -EBADMSG;
071830ff
LP
638 }
639
94f04347
LP
640 c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_PRIO_CLASS(c->ioprio), i);
641 c->ioprio_set = true;
642
071830ff
LP
643 return 0;
644}
645
47be870b 646static int config_parse_cpu_sched_policy(
9eba9da4
LP
647 const char *filename,
648 unsigned line,
649 const char *section,
650 const char *lvalue,
651 const char *rvalue,
652 void *data,
653 void *userdata) {
654
94f04347
LP
655
656 ExecContext *c = data;
657 int x;
658
659 assert(filename);
660 assert(lvalue);
661 assert(rvalue);
662 assert(data);
663
0d87eb42
LP
664 if ((x = sched_policy_from_string(rvalue)) < 0) {
665 log_error("[%s:%u] Failed to parse CPU scheduling policy: %s", filename, line, rvalue);
666 return -EBADMSG;
667 }
94f04347
LP
668
669 c->cpu_sched_policy = x;
670 c->cpu_sched_set = true;
671
672 return 0;
673}
674
47be870b 675static int config_parse_cpu_sched_prio(
94f04347
LP
676 const char *filename,
677 unsigned line,
678 const char *section,
679 const char *lvalue,
680 const char *rvalue,
681 void *data,
682 void *userdata) {
9eba9da4
LP
683
684 ExecContext *c = data;
685 int i;
686
687 assert(filename);
688 assert(lvalue);
689 assert(rvalue);
690 assert(data);
691
94f04347
LP
692 /* On Linux RR/FIFO have the same range */
693 if (safe_atoi(rvalue, &i) < 0 || i < sched_get_priority_min(SCHED_RR) || i > sched_get_priority_max(SCHED_RR)) {
694 log_error("[%s:%u] Failed to parse CPU scheduling priority: %s", filename, line, rvalue);
695 return -EBADMSG;
696 }
9eba9da4 697
94f04347
LP
698 c->cpu_sched_priority = i;
699 c->cpu_sched_set = true;
700
701 return 0;
702}
703
47be870b 704static int config_parse_cpu_affinity(
94f04347
LP
705 const char *filename,
706 unsigned line,
707 const char *section,
708 const char *lvalue,
709 const char *rvalue,
710 void *data,
711 void *userdata) {
712
713 ExecContext *c = data;
714 char *w;
715 size_t l;
716 char *state;
717
718 assert(filename);
719 assert(lvalue);
720 assert(rvalue);
721 assert(data);
722
f60f22df 723 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
94f04347
LP
724 char *t;
725 int r;
726 unsigned cpu;
727
728 if (!(t = strndup(w, l)))
729 return -ENOMEM;
730
487393e9
LP
731 r = safe_atou(t, &cpu);
732 free(t);
733
82c121a4
LP
734 if (!(c->cpuset))
735 if (!(c->cpuset = cpu_set_malloc(&c->cpuset_ncpus)))
736 return -ENOMEM;
737
82c121a4 738 if (r < 0 || cpu >= c->cpuset_ncpus) {
94f04347
LP
739 log_error("[%s:%u] Failed to parse CPU affinity: %s", filename, line, rvalue);
740 return -EBADMSG;
9eba9da4 741 }
94f04347 742
82c121a4 743 CPU_SET_S(cpu, CPU_ALLOC_SIZE(c->cpuset_ncpus), c->cpuset);
9eba9da4
LP
744 }
745
94f04347
LP
746 return 0;
747}
748
47be870b 749static int config_parse_capabilities(
94f04347
LP
750 const char *filename,
751 unsigned line,
752 const char *section,
753 const char *lvalue,
754 const char *rvalue,
755 void *data,
756 void *userdata) {
757
758 ExecContext *c = data;
759 cap_t cap;
760
761 assert(filename);
762 assert(lvalue);
763 assert(rvalue);
764 assert(data);
765
766 if (!(cap = cap_from_text(rvalue))) {
767 if (errno == ENOMEM)
768 return -ENOMEM;
769
770 log_error("[%s:%u] Failed to parse capabilities: %s", filename, line, rvalue);
771 return -EBADMSG;
772 }
773
774 if (c->capabilities)
775 cap_free(c->capabilities);
776 c->capabilities = cap;
777
778 return 0;
779}
780
47be870b 781static int config_parse_secure_bits(
94f04347
LP
782 const char *filename,
783 unsigned line,
784 const char *section,
785 const char *lvalue,
786 const char *rvalue,
787 void *data,
788 void *userdata) {
789
790 ExecContext *c = data;
791 char *w;
792 size_t l;
793 char *state;
794
795 assert(filename);
796 assert(lvalue);
797 assert(rvalue);
798 assert(data);
799
f60f22df 800 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
94f04347
LP
801 if (first_word(w, "keep-caps"))
802 c->secure_bits |= SECURE_KEEP_CAPS;
803 else if (first_word(w, "keep-caps-locked"))
804 c->secure_bits |= SECURE_KEEP_CAPS_LOCKED;
805 else if (first_word(w, "no-setuid-fixup"))
806 c->secure_bits |= SECURE_NO_SETUID_FIXUP;
807 else if (first_word(w, "no-setuid-fixup-locked"))
808 c->secure_bits |= SECURE_NO_SETUID_FIXUP_LOCKED;
809 else if (first_word(w, "noroot"))
810 c->secure_bits |= SECURE_NOROOT;
811 else if (first_word(w, "noroot-locked"))
812 c->secure_bits |= SECURE_NOROOT_LOCKED;
9eba9da4 813 else {
94f04347 814 log_error("[%s:%u] Failed to parse secure bits: %s", filename, line, rvalue);
9eba9da4
LP
815 return -EBADMSG;
816 }
817 }
818
94f04347
LP
819 return 0;
820}
821
47be870b 822static int config_parse_bounding_set(
94f04347
LP
823 const char *filename,
824 unsigned line,
825 const char *section,
826 const char *lvalue,
827 const char *rvalue,
828 void *data,
829 void *userdata) {
830
831 ExecContext *c = data;
832 char *w;
833 size_t l;
834 char *state;
835
836 assert(filename);
837 assert(lvalue);
838 assert(rvalue);
839 assert(data);
840
f60f22df 841 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
94f04347
LP
842 char *t;
843 int r;
844 cap_value_t cap;
845
846 if (!(t = strndup(w, l)))
847 return -ENOMEM;
848
849 r = cap_from_name(t, &cap);
850 free(t);
851
852 if (r < 0) {
853 log_error("[%s:%u] Failed to parse capability bounding set: %s", filename, line, rvalue);
854 return -EBADMSG;
855 }
856
857 c->capability_bounding_set_drop |= 1 << cap;
858 }
9eba9da4
LP
859
860 return 0;
861}
862
03fae018 863static int config_parse_timer_slack_nsec(
9eba9da4
LP
864 const char *filename,
865 unsigned line,
866 const char *section,
867 const char *lvalue,
868 const char *rvalue,
869 void *data,
870 void *userdata) {
871
872 ExecContext *c = data;
94f04347
LP
873 unsigned long u;
874 int r;
9eba9da4
LP
875
876 assert(filename);
877 assert(lvalue);
878 assert(rvalue);
879 assert(data);
880
94f04347
LP
881 if ((r = safe_atolu(rvalue, &u)) < 0) {
882 log_error("[%s:%u] Failed to parse time slack value: %s", filename, line, rvalue);
883 return r;
9eba9da4
LP
884 }
885
03fae018 886 c->timer_slack_nsec = u;
94f04347
LP
887
888 return 0;
889}
890
891static int config_parse_limit(
892 const char *filename,
893 unsigned line,
894 const char *section,
895 const char *lvalue,
896 const char *rvalue,
897 void *data,
898 void *userdata) {
899
900 struct rlimit **rl = data;
901 unsigned long long u;
902 int r;
903
904 assert(filename);
905 assert(lvalue);
906 assert(rvalue);
907 assert(data);
908
909 if ((r = safe_atollu(rvalue, &u)) < 0) {
910 log_error("[%s:%u] Failed to parse resource value: %s", filename, line, rvalue);
911 return r;
912 }
913
914 if (!*rl)
915 if (!(*rl = new(struct rlimit, 1)))
916 return -ENOMEM;
9eba9da4 917
94f04347 918 (*rl)->rlim_cur = (*rl)->rlim_max = (rlim_t) u;
9eba9da4
LP
919 return 0;
920}
921
8e274523
LP
922static int config_parse_cgroup(
923 const char *filename,
924 unsigned line,
925 const char *section,
926 const char *lvalue,
927 const char *rvalue,
928 void *data,
929 void *userdata) {
930
931 Unit *u = userdata;
932 char *w;
933 size_t l;
934 char *state;
935
f60f22df 936 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
8e274523
LP
937 char *t;
938 int r;
939
f60f22df 940 if (!(t = cunescape_length(w, l)))
8e274523
LP
941 return -ENOMEM;
942
943 r = unit_add_cgroup_from_text(u, t);
944 free(t);
945
946 if (r < 0)
947 return r;
948 }
949
950 return 0;
951}
952
a9a1e00a
LP
953static int config_parse_sysv_priority(
954 const char *filename,
955 unsigned line,
956 const char *section,
957 const char *lvalue,
958 const char *rvalue,
959 void *data,
960 void *userdata) {
961
962 int *priority = data;
963 int r, i;
964
965 assert(filename);
966 assert(lvalue);
967 assert(rvalue);
968 assert(data);
969
970 if ((r = safe_atoi(rvalue, &i)) < 0 || i < 0) {
971 log_error("[%s:%u] Failed to parse SysV start priority: %s", filename, line, rvalue);
972 return r;
973 }
974
975 *priority = (int) i;
976 return 0;
977}
978
487393e9 979static DEFINE_CONFIG_PARSE_ENUM(config_parse_kill_mode, kill_mode, KillMode, "Failed to parse kill mode");
50159e6a 980
2e22afe9
LP
981static int config_parse_kill_signal(
982 const char *filename,
983 unsigned line,
984 const char *section,
985 const char *lvalue,
986 const char *rvalue,
987 void *data,
988 void *userdata) {
989
990 int *sig = data;
991 int r;
992
993 assert(filename);
994 assert(lvalue);
995 assert(rvalue);
996 assert(sig);
997
998 if ((r = signal_from_string(rvalue)) <= 0)
999 if (startswith(rvalue, "SIG"))
1000 r = signal_from_string(rvalue+3);
1001
1002 if (r <= 0) {
1003 log_error("[%s:%u] Failed to parse kill signal: %s", filename, line, rvalue);
1004 return -EINVAL;
1005 }
1006
1007 *sig = r;
1008 return 0;
1009}
1010
15ae422b
LP
1011static int config_parse_mount_flags(
1012 const char *filename,
1013 unsigned line,
1014 const char *section,
1015 const char *lvalue,
1016 const char *rvalue,
1017 void *data,
1018 void *userdata) {
1019
1020 ExecContext *c = data;
1021 char *w;
1022 size_t l;
1023 char *state;
1024 unsigned long flags = 0;
1025
1026 assert(filename);
1027 assert(lvalue);
1028 assert(rvalue);
1029 assert(data);
1030
f60f22df 1031 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
15ae422b
LP
1032 if (strncmp(w, "shared", l) == 0)
1033 flags |= MS_SHARED;
1034 else if (strncmp(w, "slave", l) == 0)
1035 flags |= MS_SLAVE;
1036 else if (strncmp(w, "private", l) == 0)
1037 flags |= MS_PRIVATE;
1038 else {
1039 log_error("[%s:%u] Failed to parse mount flags: %s", filename, line, rvalue);
1040 return -EINVAL;
1041 }
1042 }
1043
1044 c->mount_flags = flags;
1045 return 0;
1046}
1047
871d7de4
LP
1048static int config_parse_timer(
1049 const char *filename,
1050 unsigned line,
1051 const char *section,
1052 const char *lvalue,
1053 const char *rvalue,
1054 void *data,
1055 void *userdata) {
1056
1057 Timer *t = data;
1058 usec_t u;
1059 int r;
1060 TimerValue *v;
1061 TimerBase b;
1062
1063 assert(filename);
1064 assert(lvalue);
1065 assert(rvalue);
1066 assert(data);
1067
1068 if ((b = timer_base_from_string(lvalue)) < 0) {
1069 log_error("[%s:%u] Failed to parse timer base: %s", filename, line, lvalue);
1070 return -EINVAL;
1071 }
1072
1073 if ((r = parse_usec(rvalue, &u)) < 0) {
1074 log_error("[%s:%u] Failed to parse timer value: %s", filename, line, rvalue);
1075 return r;
1076 }
1077
1078 if (!(v = new0(TimerValue, 1)))
1079 return -ENOMEM;
1080
1081 v->base = b;
1082 v->value = u;
1083
1084 LIST_PREPEND(TimerValue, value, t->values, v);
1085
1086 return 0;
1087}
1088
1089static int config_parse_timer_unit(
1090 const char *filename,
1091 unsigned line,
1092 const char *section,
1093 const char *lvalue,
1094 const char *rvalue,
1095 void *data,
1096 void *userdata) {
1097
1098 Timer *t = data;
1099 int r;
398ef8ba
LP
1100 DBusError error;
1101
1102 assert(filename);
1103 assert(lvalue);
1104 assert(rvalue);
1105 assert(data);
1106
1107 dbus_error_init(&error);
871d7de4
LP
1108
1109 if (endswith(rvalue, ".timer")) {
1110 log_error("[%s:%u] Unit cannot be of type timer: %s", filename, line, rvalue);
1111 return -EINVAL;
1112 }
1113
398ef8ba
LP
1114 if ((r = manager_load_unit(t->meta.manager, rvalue, NULL, NULL, &t->unit)) < 0) {
1115 log_error("[%s:%u] Failed to load unit %s: %s", filename, line, rvalue, bus_error(&error, r));
1116 dbus_error_free(&error);
871d7de4
LP
1117 return r;
1118 }
1119
1120 return 0;
1121}
1122
01f78473
LP
1123static int config_parse_path_spec(
1124 const char *filename,
1125 unsigned line,
1126 const char *section,
1127 const char *lvalue,
1128 const char *rvalue,
1129 void *data,
1130 void *userdata) {
1131
1132 Path *p = data;
1133 PathSpec *s;
1134 PathType b;
1135
1136 assert(filename);
1137 assert(lvalue);
1138 assert(rvalue);
1139 assert(data);
1140
1141 if ((b = path_type_from_string(lvalue)) < 0) {
1142 log_error("[%s:%u] Failed to parse path type: %s", filename, line, lvalue);
1143 return -EINVAL;
1144 }
1145
1146 if (!path_is_absolute(rvalue)) {
1147 log_error("[%s:%u] Path is not absolute: %s", filename, line, rvalue);
1148 return -EINVAL;
1149 }
1150
1151 if (!(s = new0(PathSpec, 1)))
1152 return -ENOMEM;
1153
1154 if (!(s->path = strdup(rvalue))) {
1155 free(s);
1156 return -ENOMEM;
1157 }
1158
1159 path_kill_slashes(s->path);
1160
1161 s->type = b;
1162 s->inotify_fd = -1;
1163
1164 LIST_PREPEND(PathSpec, spec, p->specs, s);
1165
1166 return 0;
1167}
1168
1169static int config_parse_path_unit(
1170 const char *filename,
1171 unsigned line,
1172 const char *section,
1173 const char *lvalue,
1174 const char *rvalue,
1175 void *data,
1176 void *userdata) {
1177
1178 Path *t = data;
1179 int r;
398ef8ba
LP
1180 DBusError error;
1181
1182 assert(filename);
1183 assert(lvalue);
1184 assert(rvalue);
1185 assert(data);
1186
1187 dbus_error_init(&error);
01f78473
LP
1188
1189 if (endswith(rvalue, ".path")) {
1190 log_error("[%s:%u] Unit cannot be of type path: %s", filename, line, rvalue);
1191 return -EINVAL;
1192 }
1193
398ef8ba
LP
1194 if ((r = manager_load_unit(t->meta.manager, rvalue, NULL, &error, &t->unit)) < 0) {
1195 log_error("[%s:%u] Failed to load unit %s: %s", filename, line, rvalue, bus_error(&error, r));
1196 dbus_error_free(&error);
01f78473
LP
1197 return r;
1198 }
1199
1200 return 0;
1201}
1202
ddb26e18
LP
1203static int config_parse_env_file(
1204 const char *filename,
1205 unsigned line,
1206 const char *section,
1207 const char *lvalue,
1208 const char *rvalue,
1209 void *data,
1210 void *userdata) {
1211
1212 FILE *f;
1213 int r;
1214 char ***env = data;
1215
1216 assert(filename);
1217 assert(lvalue);
1218 assert(rvalue);
1219 assert(data);
1220
1221 if (!(f = fopen(rvalue, "re"))) {
1222 log_error("[%s:%u] Failed to open environment file '%s': %m", filename, line, rvalue);
1223 return -errno;
1224 }
1225
1226 while (!feof(f)) {
1227 char l[LINE_MAX], *p;
1228 char **t;
1229
1230 if (!fgets(l, sizeof(l), f)) {
1231 if (feof(f))
1232 break;
1233
1234 r = -errno;
1235 log_error("[%s:%u] Failed to read environment file '%s': %m", filename, line, rvalue);
1236 goto finish;
1237 }
1238
1239 p = strstrip(l);
1240
1241 if (!*p)
1242 continue;
1243
1244 if (strchr(COMMENTS, *p))
1245 continue;
1246
1247 t = strv_env_set(*env, p);
1248 strv_free(*env);
1249 *env = t;
1250 }
1251
1252 r = 0;
1253
1254finish:
1255 if (f)
1256 fclose(f);
1257
1258 return r;
1259}
1260
4fd5948e
LP
1261static int config_parse_ip_tos(
1262 const char *filename,
1263 unsigned line,
1264 const char *section,
1265 const char *lvalue,
1266 const char *rvalue,
1267 void *data,
1268 void *userdata) {
1269
1270 int *ip_tos = data, x;
1271 int r;
1272
1273 assert(filename);
1274 assert(lvalue);
1275 assert(rvalue);
1276 assert(data);
1277
1278 if ((x = ip_tos_from_string(rvalue)) < 0)
1279 if ((r = safe_atoi(rvalue, &x)) < 0) {
1280 log_error("[%s:%u] Failed to parse IP TOS value: %s", filename, line, rvalue);
1281 return r;
1282 }
1283
1284 *ip_tos = x;
1285 return 0;
1286}
1287
487393e9 1288static DEFINE_CONFIG_PARSE_ENUM(config_parse_notify_access, notify_access, NotifyAccess, "Failed to parse notify access specifier");
c952c6ec 1289
071830ff 1290#define FOLLOW_MAX 8
87f0e418 1291
9e2f7c11 1292static int open_follow(char **filename, FILE **_f, Set *names, char **_final) {
0301abf4 1293 unsigned c = 0;
87f0e418
LP
1294 int fd, r;
1295 FILE *f;
0301abf4 1296 char *id = NULL;
87f0e418
LP
1297
1298 assert(filename);
1299 assert(*filename);
1300 assert(_f);
1301 assert(names);
1302
0301abf4
LP
1303 /* This will update the filename pointer if the loaded file is
1304 * reached by a symlink. The old string will be freed. */
87f0e418 1305
0301abf4 1306 for (;;) {
2c7108c4 1307 char *target, *name;
87f0e418 1308
0301abf4
LP
1309 if (c++ >= FOLLOW_MAX)
1310 return -ELOOP;
1311
b08d03ff
LP
1312 path_kill_slashes(*filename);
1313
87f0e418
LP
1314 /* Add the file name we are currently looking at to
1315 * the names of this unit */
0301abf4
LP
1316 name = file_name_from_path(*filename);
1317 if (!(id = set_get(names, name))) {
87f0e418 1318
0301abf4
LP
1319 if (!(id = strdup(name)))
1320 return -ENOMEM;
87f0e418 1321
0301abf4
LP
1322 if ((r = set_put(names, id)) < 0) {
1323 free(id);
1324 return r;
87f0e418 1325 }
87f0e418
LP
1326 }
1327
0301abf4
LP
1328 /* Try to open the file name, but don't if its a symlink */
1329 if ((fd = open(*filename, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW)) >= 0)
87f0e418
LP
1330 break;
1331
0301abf4
LP
1332 if (errno != ELOOP)
1333 return -errno;
1334
87f0e418 1335 /* Hmm, so this is a symlink. Let's read the name, and follow it manually */
2c7108c4 1336 if ((r = readlink_and_make_absolute(*filename, &target)) < 0)
0301abf4 1337 return r;
87f0e418 1338
0301abf4 1339 free(*filename);
2c7108c4 1340 *filename = target;
87f0e418
LP
1341 }
1342
1343 if (!(f = fdopen(fd, "r"))) {
1344 r = -errno;
9e2f7c11 1345 close_nointr_nofail(fd);
0301abf4 1346 return r;
87f0e418
LP
1347 }
1348
1349 *_f = f;
9e2f7c11 1350 *_final = id;
0301abf4 1351 return 0;
87f0e418
LP
1352}
1353
23a177ef
LP
1354static int merge_by_names(Unit **u, Set *names, const char *id) {
1355 char *k;
1356 int r;
1357
1358 assert(u);
1359 assert(*u);
1360 assert(names);
1361
1362 /* Let's try to add in all symlink names we found */
1363 while ((k = set_steal_first(names))) {
1364
1365 /* First try to merge in the other name into our
1366 * unit */
1367 if ((r = unit_merge_by_name(*u, k)) < 0) {
1368 Unit *other;
1369
1370 /* Hmm, we couldn't merge the other unit into
1371 * ours? Then let's try it the other way
1372 * round */
1373
1374 other = manager_get_unit((*u)->meta.manager, k);
1375 free(k);
1376
1377 if (other)
1378 if ((r = unit_merge(other, *u)) >= 0) {
1379 *u = other;
1380 return merge_by_names(u, names, NULL);
1381 }
1382
1383 return r;
1384 }
1385
1386 if (id == k)
1387 unit_choose_id(*u, id);
1388
1389 free(k);
1390 }
1391
1392 return 0;
1393}
1394
e537352b
LP
1395static void dump_items(FILE *f, const ConfigItem *items) {
1396 const ConfigItem *i;
1397 const char *prev_section = NULL;
1398 bool not_first = false;
1399
1400 struct {
1401 ConfigParserCallback callback;
1402 const char *rvalue;
1403 } table[] = {
1404 { config_parse_int, "INTEGER" },
1405 { config_parse_unsigned, "UNSIGNED" },
1406 { config_parse_size, "SIZE" },
1407 { config_parse_bool, "BOOLEAN" },
1408 { config_parse_string, "STRING" },
1409 { config_parse_path, "PATH" },
1410 { config_parse_strv, "STRING [...]" },
1411 { config_parse_nice, "NICE" },
1412 { config_parse_oom_adjust, "OOMADJUST" },
1413 { config_parse_io_class, "IOCLASS" },
1414 { config_parse_io_priority, "IOPRIORITY" },
1415 { config_parse_cpu_sched_policy, "CPUSCHEDPOLICY" },
1416 { config_parse_cpu_sched_prio, "CPUSCHEDPRIO" },
1417 { config_parse_cpu_affinity, "CPUAFFINITY" },
1418 { config_parse_mode, "MODE" },
ddb26e18 1419 { config_parse_env_file, "FILE" },
e537352b
LP
1420 { config_parse_output, "OUTPUT" },
1421 { config_parse_input, "INPUT" },
1422 { config_parse_facility, "FACILITY" },
1423 { config_parse_level, "LEVEL" },
1424 { config_parse_capabilities, "CAPABILITIES" },
1425 { config_parse_secure_bits, "SECUREBITS" },
1426 { config_parse_bounding_set, "BOUNDINGSET" },
03fae018 1427 { config_parse_timer_slack_nsec, "TIMERSLACK" },
e537352b
LP
1428 { config_parse_limit, "LIMIT" },
1429 { config_parse_cgroup, "CGROUP [...]" },
1430 { config_parse_deps, "UNIT [...]" },
1431 { config_parse_names, "UNIT [...]" },
1432 { config_parse_exec, "PATH [ARGUMENT [...]]" },
1433 { config_parse_service_type, "SERVICETYPE" },
1434 { config_parse_service_restart, "SERVICERESTART" },
1435 { config_parse_sysv_priority, "SYSVPRIORITY" },
1436 { config_parse_kill_mode, "KILLMODE" },
2e22afe9 1437 { config_parse_kill_signal, "SIGNAL" },
e537352b
LP
1438 { config_parse_listen, "SOCKET [...]" },
1439 { config_parse_socket_bind, "SOCKETBIND" },
93ef5e80
LP
1440 { config_parse_bindtodevice, "NETWORKINTERFACE" },
1441 { config_parse_usec, "SECONDS" },
1442 { config_parse_path_strv, "PATH [...]" },
932921b5 1443 { config_parse_mount_flags, "MOUNTFLAG [...]" },
f2d3769a 1444 { config_parse_string_printf, "STRING" },
871d7de4
LP
1445 { config_parse_timer, "TIMER" },
1446 { config_parse_timer_unit, "NAME" },
c952c6ec
LP
1447 { config_parse_path_spec, "PATH" },
1448 { config_parse_path_unit, "UNIT" },
8b03daeb
LP
1449 { config_parse_notify_access, "ACCESS" },
1450 { config_parse_ip_tos, "TOS" },
e537352b
LP
1451 };
1452
1453 assert(f);
1454 assert(items);
1455
1456 for (i = items; i->lvalue; i++) {
1457 unsigned j;
1458 const char *rvalue = "OTHER";
1459
1460 if (!streq_ptr(i->section, prev_section)) {
1461 if (!not_first)
1462 not_first = true;
1463 else
1464 fputc('\n', f);
1465
1466 fprintf(f, "[%s]\n", i->section);
1467 prev_section = i->section;
1468 }
1469
1470 for (j = 0; j < ELEMENTSOF(table); j++)
1471 if (i->parse == table[j].callback) {
1472 rvalue = table[j].rvalue;
1473 break;
1474 }
1475
1476 fprintf(f, "%s=%s\n", i->lvalue, rvalue);
1477 }
1478}
1479
1480static int load_from_path(Unit *u, const char *path) {
87f0e418
LP
1481
1482 static const char* const section_table[_UNIT_TYPE_MAX] = {
1483 [UNIT_SERVICE] = "Service",
1484 [UNIT_TIMER] = "Timer",
1485 [UNIT_SOCKET] = "Socket",
1486 [UNIT_TARGET] = "Target",
1487 [UNIT_DEVICE] = "Device",
1488 [UNIT_MOUNT] = "Mount",
1489 [UNIT_AUTOMOUNT] = "Automount",
07b0b134 1490 [UNIT_SNAPSHOT] = "Snapshot",
01f78473
LP
1491 [UNIT_SWAP] = "Swap",
1492 [UNIT_PATH] = "Path"
42f4e3c4
LP
1493 };
1494
034c6ed7 1495#define EXEC_CONTEXT_CONFIG_ITEMS(context, section) \
9eba9da4
LP
1496 { "WorkingDirectory", config_parse_path, &(context).working_directory, section }, \
1497 { "RootDirectory", config_parse_path, &(context).root_directory, section }, \
f2d3769a
LP
1498 { "User", config_parse_string_printf, &(context).user, section }, \
1499 { "Group", config_parse_string_printf, &(context).group, section }, \
1c01f82b 1500 { "SupplementaryGroups", config_parse_strv, &(context).supplementary_groups, section }, \
fb33a393
LP
1501 { "Nice", config_parse_nice, &(context), section }, \
1502 { "OOMAdjust", config_parse_oom_adjust, &(context), section }, \
9eba9da4 1503 { "IOSchedulingClass", config_parse_io_class, &(context), section }, \
94f04347
LP
1504 { "IOSchedulingPriority", config_parse_io_priority, &(context), section }, \
1505 { "CPUSchedulingPolicy", config_parse_cpu_sched_policy,&(context), section }, \
1506 { "CPUSchedulingPriority", config_parse_cpu_sched_prio, &(context), section }, \
38b48754 1507 { "CPUSchedulingResetOnFork", config_parse_bool, &(context).cpu_sched_reset_on_fork, section }, \
94f04347 1508 { "CPUAffinity", config_parse_cpu_affinity, &(context), section }, \
b5a0699f 1509 { "UMask", config_parse_mode, &(context).umask, section }, \
071830ff 1510 { "Environment", config_parse_strv, &(context).environment, section }, \
ddb26e18 1511 { "EnvironmentFile", config_parse_env_file, &(context).environment, section }, \
80876c20
LP
1512 { "StandardInput", config_parse_input, &(context).std_input, section }, \
1513 { "StandardOutput", config_parse_output, &(context).std_output, section }, \
a4ddf827 1514 { "StandardError", config_parse_output, &(context).std_error, section }, \
80876c20 1515 { "TTYPath", config_parse_path, &(context).tty_path, section }, \
f2d3769a 1516 { "SyslogIdentifier", config_parse_string_printf, &(context).syslog_identifier, section }, \
071830ff 1517 { "SyslogFacility", config_parse_facility, &(context).syslog_priority, section }, \
94f04347 1518 { "SyslogLevel", config_parse_level, &(context).syslog_priority, section }, \
74922904 1519 { "SyslogLevelPrefix", config_parse_bool, &(context).syslog_level_prefix, section }, \
94f04347
LP
1520 { "Capabilities", config_parse_capabilities, &(context), section }, \
1521 { "SecureBits", config_parse_secure_bits, &(context), section }, \
1522 { "CapabilityBoundingSetDrop", config_parse_bounding_set, &(context), section }, \
03fae018 1523 { "TimerSlackNSec", config_parse_timer_slack_nsec,&(context), section }, \
94f04347
LP
1524 { "LimitCPU", config_parse_limit, &(context).rlimit[RLIMIT_CPU], section }, \
1525 { "LimitFSIZE", config_parse_limit, &(context).rlimit[RLIMIT_FSIZE], section }, \
1526 { "LimitDATA", config_parse_limit, &(context).rlimit[RLIMIT_DATA], section }, \
1527 { "LimitSTACK", config_parse_limit, &(context).rlimit[RLIMIT_STACK], section }, \
1528 { "LimitCORE", config_parse_limit, &(context).rlimit[RLIMIT_CORE], section }, \
1529 { "LimitRSS", config_parse_limit, &(context).rlimit[RLIMIT_RSS], section }, \
1530 { "LimitNOFILE", config_parse_limit, &(context).rlimit[RLIMIT_NOFILE], section }, \
1531 { "LimitAS", config_parse_limit, &(context).rlimit[RLIMIT_AS], section }, \
1532 { "LimitNPROC", config_parse_limit, &(context).rlimit[RLIMIT_NPROC], section }, \
1533 { "LimitMEMLOCK", config_parse_limit, &(context).rlimit[RLIMIT_MEMLOCK], section }, \
1534 { "LimitLOCKS", config_parse_limit, &(context).rlimit[RLIMIT_LOCKS], section }, \
1535 { "LimitSIGPENDING", config_parse_limit, &(context).rlimit[RLIMIT_SIGPENDING], section }, \
1536 { "LimitMSGQUEUE", config_parse_limit, &(context).rlimit[RLIMIT_MSGQUEUE], section }, \
1537 { "LimitNICE", config_parse_limit, &(context).rlimit[RLIMIT_NICE], section }, \
1538 { "LimitRTPRIO", config_parse_limit, &(context).rlimit[RLIMIT_RTPRIO], section }, \
451a074f 1539 { "LimitRTTIME", config_parse_limit, &(context).rlimit[RLIMIT_RTTIME], section }, \
15ae422b
LP
1540 { "ControlGroup", config_parse_cgroup, u, section }, \
1541 { "ReadWriteDirectories", config_parse_path_strv, &(context).read_write_dirs, section }, \
1542 { "ReadOnlyDirectories", config_parse_path_strv, &(context).read_only_dirs, section }, \
1543 { "InaccessibleDirectories",config_parse_path_strv, &(context).inaccessible_dirs, section }, \
1544 { "PrivateTmp", config_parse_bool, &(context).private_tmp, section }, \
df1f0afe 1545 { "MountFlags", config_parse_mount_flags, &(context), section }, \
f2d3769a 1546 { "TCPWrapName", config_parse_string_printf, &(context).tcpwrap_name, section }, \
2e22afe9
LP
1547 { "PAMName", config_parse_string_printf, &(context).pam_name, section }, \
1548 { "KillMode", config_parse_kill_mode, &(context).kill_mode, section }, \
1549 { "KillSignal", config_parse_kill_signal, &(context).kill_signal, section }
034c6ed7 1550
3efd4195 1551 const ConfigItem items[] = {
09477267 1552 { "Names", config_parse_names, u, "Unit" },
f2d3769a 1553 { "Description", config_parse_string_printf, &u->meta.description, "Unit" },
09477267
LP
1554 { "Requires", config_parse_deps, UINT_TO_PTR(UNIT_REQUIRES), "Unit" },
1555 { "RequiresOverridable", config_parse_deps, UINT_TO_PTR(UNIT_REQUIRES_OVERRIDABLE), "Unit" },
1556 { "Requisite", config_parse_deps, UINT_TO_PTR(UNIT_REQUISITE), "Unit" },
1557 { "RequisiteOverridable", config_parse_deps, UINT_TO_PTR(UNIT_REQUISITE_OVERRIDABLE), "Unit" },
1558 { "Wants", config_parse_deps, UINT_TO_PTR(UNIT_WANTS), "Unit" },
1559 { "Conflicts", config_parse_deps, UINT_TO_PTR(UNIT_CONFLICTS), "Unit" },
1560 { "Before", config_parse_deps, UINT_TO_PTR(UNIT_BEFORE), "Unit" },
1561 { "After", config_parse_deps, UINT_TO_PTR(UNIT_AFTER), "Unit" },
45fb0699 1562 { "OnFailure", config_parse_deps, UINT_TO_PTR(UNIT_ON_FAILURE), "Unit" },
09477267
LP
1563 { "RecursiveStop", config_parse_bool, &u->meta.recursive_stop, "Unit" },
1564 { "StopWhenUnneeded", config_parse_bool, &u->meta.stop_when_unneeded, "Unit" },
bc0f8771 1565 { "OnlyByDependency", config_parse_bool, &u->meta.only_by_dependency, "Unit" },
a40eb732 1566 { "DefaultDependencies", config_parse_bool, &u->meta.default_dependencies, "Unit" },
3b6fdb5b 1567 { "IgnoreDependencyFailure",config_parse_bool, &u->meta.ignore_dependency_failure, "Unit" },
1c01f82b
LP
1568
1569 { "PIDFile", config_parse_path, &u->service.pid_file, "Service" },
1570 { "ExecStartPre", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START_PRE, "Service" },
1571 { "ExecStart", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START, "Service" },
1572 { "ExecStartPost", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START_POST, "Service" },
1573 { "ExecReload", config_parse_exec, u->service.exec_command+SERVICE_EXEC_RELOAD, "Service" },
1574 { "ExecStop", config_parse_exec, u->service.exec_command+SERVICE_EXEC_STOP, "Service" },
1575 { "ExecStopPost", config_parse_exec, u->service.exec_command+SERVICE_EXEC_STOP_POST, "Service" },
1576 { "RestartSec", config_parse_usec, &u->service.restart_usec, "Service" },
1577 { "TimeoutSec", config_parse_usec, &u->service.timeout_usec, "Service" },
0d87eb42
LP
1578 { "Type", config_parse_service_type, &u->service.type, "Service" },
1579 { "Restart", config_parse_service_restart, &u->service.restart, "Service" },
81a2b7ce
LP
1580 { "PermissionsStartOnly", config_parse_bool, &u->service.permissions_start_only, "Service" },
1581 { "RootDirectoryStartOnly", config_parse_bool, &u->service.root_directory_start_only, "Service" },
8e274523 1582 { "ValidNoProcess", config_parse_bool, &u->service.valid_no_process, "Service" },
a9a1e00a 1583 { "SysVStartPriority", config_parse_sysv_priority, &u->service.sysv_start_priority, "Service" },
b778d555 1584 { "NonBlocking", config_parse_bool, &u->service.exec_context.non_blocking, "Service" },
f2d3769a 1585 { "BusName", config_parse_string_printf, &u->service.bus_name, "Service" },
c952c6ec 1586 { "NotifyAccess", config_parse_notify_access, &u->service.notify_access, "Service" },
87f0e418
LP
1587 EXEC_CONTEXT_CONFIG_ITEMS(u->service.exec_context, "Service"),
1588
1c01f82b
LP
1589 { "ListenStream", config_parse_listen, &u->socket, "Socket" },
1590 { "ListenDatagram", config_parse_listen, &u->socket, "Socket" },
1591 { "ListenSequentialPacket", config_parse_listen, &u->socket, "Socket" },
1592 { "ListenFIFO", config_parse_listen, &u->socket, "Socket" },
1593 { "BindIPv6Only", config_parse_socket_bind, &u->socket, "Socket" },
1594 { "Backlog", config_parse_unsigned, &u->socket.backlog, "Socket" },
acbb0225 1595 { "BindToDevice", config_parse_bindtodevice, &u->socket, "Socket" },
1c01f82b
LP
1596 { "ExecStartPre", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_START_PRE, "Socket" },
1597 { "ExecStartPost", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_START_POST, "Socket" },
1598 { "ExecStopPre", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_STOP_PRE, "Socket" },
1599 { "ExecStopPost", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_STOP_POST, "Socket" },
108736d0 1600 { "TimeoutSec", config_parse_usec, &u->socket.timeout_usec, "Socket" },
b5a0699f
LP
1601 { "DirectoryMode", config_parse_mode, &u->socket.directory_mode, "Socket" },
1602 { "SocketMode", config_parse_mode, &u->socket.socket_mode, "Socket" },
4f2d528d 1603 { "Accept", config_parse_bool, &u->socket.accept, "Socket" },
6cf6bbc2 1604 { "MaxConnections", config_parse_unsigned, &u->socket.max_connections, "Socket" },
4fd5948e
LP
1605 { "KeepAlive", config_parse_bool, &u->socket.keep_alive, "Socket" },
1606 { "Priority", config_parse_int, &u->socket.priority, "Socket" },
1607 { "ReceiveBuffer", config_parse_size, &u->socket.receive_buffer, "Socket" },
1608 { "SendBuffer", config_parse_size, &u->socket.send_buffer, "Socket" },
1609 { "IPTOS", config_parse_ip_tos, &u->socket.ip_tos, "Socket" },
1610 { "IPTTL", config_parse_int, &u->socket.ip_ttl, "Socket" },
1611 { "Mark", config_parse_int, &u->socket.mark, "Socket" },
1612 { "PipeSize", config_parse_size, &u->socket.pipe_size, "Socket" },
1613 { "FreeBind", config_parse_bool, &u->socket.free_bind, "Socket" },
87f0e418
LP
1614 EXEC_CONTEXT_CONFIG_ITEMS(u->socket.exec_context, "Socket"),
1615
e537352b
LP
1616 { "What", config_parse_string, &u->mount.parameters_fragment.what, "Mount" },
1617 { "Where", config_parse_path, &u->mount.where, "Mount" },
1618 { "Options", config_parse_string, &u->mount.parameters_fragment.options, "Mount" },
1619 { "Type", config_parse_string, &u->mount.parameters_fragment.fstype, "Mount" },
1620 { "TimeoutSec", config_parse_usec, &u->mount.timeout_usec, "Mount" },
3e5235b0 1621 { "DirectoryMode", config_parse_mode, &u->mount.directory_mode, "Mount" },
e537352b 1622 EXEC_CONTEXT_CONFIG_ITEMS(u->mount.exec_context, "Mount"),
034c6ed7 1623
8d567588 1624 { "Where", config_parse_path, &u->automount.where, "Automount" },
1cf18f27 1625 { "DirectoryMode", config_parse_mode, &u->automount.directory_mode, "Automount" },
8d567588 1626
01f78473
LP
1627 { "What", config_parse_path, &u->swap.parameters_fragment.what, "Swap" },
1628 { "Priority", config_parse_int, &u->swap.parameters_fragment.priority, "Swap" },
1629
03fae018
LP
1630 { "OnActiveSec", config_parse_timer, &u->timer, "Timer" },
1631 { "OnBootSec", config_parse_timer, &u->timer, "Timer" },
1632 { "OnStartupSec", config_parse_timer, &u->timer, "Timer" },
1633 { "OnUnitActiveSec", config_parse_timer, &u->timer, "Timer" },
1634 { "OnUnitInactiveSec", config_parse_timer, &u->timer, "Timer" },
01f78473 1635 { "Unit", config_parse_timer_unit, &u->timer, "Timer" },
07b0b134 1636
01f78473
LP
1637 { "PathExists", config_parse_path_spec, &u->path, "Path" },
1638 { "PathChanged", config_parse_path_spec, &u->path, "Path" },
1639 { "DirectoryNotEmpty", config_parse_path_spec, &u->path, "Path" },
1640 { "Unit", config_parse_path_unit, &u->path, "Path" },
871d7de4 1641
10e87ee7
LP
1642 /* The [Install] section is ignored here. */
1643 { "Alias", NULL, NULL, "Install" },
1644 { "WantedBy", NULL, NULL, "Install" },
1645 { "Also", NULL, NULL, "Install" },
1646
3efd4195
LP
1647 { NULL, NULL, NULL, NULL }
1648 };
1649
034c6ed7 1650#undef EXEC_CONTEXT_CONFIG_ITEMS
42f4e3c4 1651
10e87ee7 1652 const char *sections[4];
0301abf4 1653 int r;
87f0e418 1654 Set *symlink_names;
23a177ef
LP
1655 FILE *f = NULL;
1656 char *filename = NULL, *id = NULL;
1657 Unit *merged;
45fb0699 1658 struct stat st;
23a177ef 1659
e537352b
LP
1660 if (!u) {
1661 /* Dirty dirty hack. */
1662 dump_items((FILE*) path, items);
1663 return 0;
1664 }
1665
23a177ef 1666 assert(u);
e537352b 1667 assert(path);
3efd4195 1668
09477267 1669 sections[0] = "Unit";
87f0e418 1670 sections[1] = section_table[u->meta.type];
10e87ee7
LP
1671 sections[2] = "Install";
1672 sections[3] = NULL;
42f4e3c4 1673
87f0e418
LP
1674 if (!(symlink_names = set_new(string_hash_func, string_compare_func)))
1675 return -ENOMEM;
3efd4195 1676
036643a2
LP
1677 if (path_is_absolute(path)) {
1678
1679 if (!(filename = strdup(path))) {
1680 r = -ENOMEM;
1681 goto finish;
1682 }
1683
1684 if ((r = open_follow(&filename, &f, symlink_names, &id)) < 0) {
1685 free(filename);
1686 filename = NULL;
1687
1688 if (r != -ENOENT)
1689 goto finish;
1690 }
1691
1692 } else {
1693 char **p;
1694
84e3543e 1695 STRV_FOREACH(p, u->meta.manager->lookup_paths.unit_path) {
036643a2
LP
1696
1697 /* Instead of opening the path right away, we manually
1698 * follow all symlinks and add their name to our unit
1699 * name set while doing so */
1700 if (!(filename = path_make_absolute(path, *p))) {
1701 r = -ENOMEM;
1702 goto finish;
1703 }
1704
fe51822e
LP
1705 if (u->meta.manager->unit_path_cache &&
1706 !set_get(u->meta.manager->unit_path_cache, filename))
1707 r = -ENOENT;
1708 else
1709 r = open_follow(&filename, &f, symlink_names, &id);
1710
1711 if (r < 0) {
036643a2
LP
1712 char *sn;
1713
1714 free(filename);
1715 filename = NULL;
1716
1717 if (r != -ENOENT)
1718 goto finish;
1719
1720 /* Empty the symlink names for the next run */
1721 while ((sn = set_steal_first(symlink_names)))
1722 free(sn);
3efd4195 1723
036643a2
LP
1724 continue;
1725 }
1726
1727 break;
1728 }
1729 }
034c6ed7 1730
036643a2 1731 if (!filename) {
23a177ef 1732 r = 0;
0301abf4
LP
1733 goto finish;
1734 }
87f0e418 1735
23a177ef
LP
1736 merged = u;
1737 if ((r = merge_by_names(&merged, symlink_names, id)) < 0)
0301abf4 1738 goto finish;
87f0e418 1739
23a177ef 1740 if (merged != u) {
e537352b 1741 u->meta.load_state = UNIT_MERGED;
23a177ef
LP
1742 r = 0;
1743 goto finish;
034c6ed7
LP
1744 }
1745
45fb0699
LP
1746 zero(st);
1747 if (fstat(fileno(f), &st) < 0) {
1748 r = -errno;
1749 goto finish;
1750 }
1751
1752 if (!S_ISREG(st.st_mode)) {
1753 r = -ENOENT;
1754 goto finish;
1755 }
1756
23a177ef 1757 /* Now, parse the file contents */
10e87ee7 1758 if ((r = config_parse(filename, f, sections, items, false, u)) < 0)
23a177ef 1759 goto finish;
b08d03ff 1760
6be1e7d5
LP
1761 free(u->meta.fragment_path);
1762 u->meta.fragment_path = filename;
0301abf4 1763 filename = NULL;
87f0e418 1764
45fb0699
LP
1765 u->meta.fragment_mtime = timespec_load(&st.st_mtim);
1766
e537352b 1767 u->meta.load_state = UNIT_LOADED;
23a177ef 1768 r = 0;
87f0e418
LP
1769
1770finish:
53ec43c6 1771 set_free_free(symlink_names);
0301abf4
LP
1772 free(filename);
1773
23a177ef
LP
1774 if (f)
1775 fclose(f);
1776
0301abf4
LP
1777 return r;
1778}
1779
e537352b 1780int unit_load_fragment(Unit *u) {
23a177ef 1781 int r;
0301abf4
LP
1782
1783 assert(u);
23a177ef
LP
1784
1785 if (u->meta.fragment_path) {
1786
e537352b 1787 if ((r = load_from_path(u, u->meta.fragment_path)) < 0)
23a177ef 1788 return r;
0301abf4 1789
23a177ef 1790 } else {
0301abf4 1791 Iterator i;
890f434c 1792 const char *t;
0301abf4 1793
890f434c 1794 /* Try to find the unit under its id */
9e2f7c11
LP
1795 if ((r = load_from_path(u, u->meta.id)) < 0)
1796 return r;
890f434c
LP
1797
1798 /* Try to find an alias we can load this with */
e537352b 1799 if (u->meta.load_state == UNIT_STUB)
23a177ef 1800 SET_FOREACH(t, u->meta.names, i) {
87f0e418 1801
9e2f7c11 1802 if (t == u->meta.id)
23a177ef 1803 continue;
071830ff 1804
e537352b 1805 if ((r = load_from_path(u, t)) < 0)
23a177ef 1806 return r;
890f434c 1807
e537352b 1808 if (u->meta.load_state != UNIT_STUB)
23a177ef
LP
1809 break;
1810 }
9e2f7c11
LP
1811
1812 /* Now, follow the same logic, but look for a template */
1813 if (u->meta.load_state == UNIT_STUB && u->meta.instance) {
1814 char *k;
1815
1816 if (!(k = unit_name_template(u->meta.id)))
1817 return -ENOMEM;
1818
1819 r = load_from_path(u, k);
1820 free(k);
1821
1822 if (r < 0)
1823 return r;
1824
1825 if (u->meta.load_state == UNIT_STUB)
1826 SET_FOREACH(t, u->meta.names, i) {
1827
1828 if (t == u->meta.id)
1829 continue;
1830
1831 if (!(k = unit_name_template(t)))
1832 return -ENOMEM;
1833
1834 r = load_from_path(u, k);
1835 free(k);
1836
1837 if (r < 0)
1838 return r;
1839
1840 if (u->meta.load_state != UNIT_STUB)
1841 break;
1842 }
1843 }
071830ff
LP
1844 }
1845
23a177ef 1846 return 0;
3efd4195 1847}
e537352b
LP
1848
1849void unit_dump_config_items(FILE *f) {
1850 /* OK, this wins a prize for extreme ugliness. */
1851
1852 load_from_path(NULL, (const void*) f);
1853}