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