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