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