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