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