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