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