chiark / gitweb /
units: in order to make the default services parseable by GKeyFile, merge repeated...
[elogind.git] / src / load-fragment.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
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
33 #include "unit.h"
34 #include "strv.h"
35 #include "conf-parser.h"
36 #include "load-fragment.h"
37 #include "log.h"
38 #include "ioprio.h"
39 #include "securebits.h"
40 #include "missing.h"
41 #include "unit-name.h"
42
43 #define COMMENTS "#;\n"
44
45 static int config_parse_deps(
46                 const char *filename,
47                 unsigned line,
48                 const char *section,
49                 const char *lvalue,
50                 const char *rvalue,
51                 void *data,
52                 void *userdata) {
53
54         UnitDependency d = PTR_TO_UINT(data);
55         Unit *u = userdata;
56         char *w;
57         size_t l;
58         char *state;
59
60         assert(filename);
61         assert(lvalue);
62         assert(rvalue);
63
64         FOREACH_WORD_QUOTED(w, l, rvalue, state) {
65                 char *t, *k;
66                 int r;
67
68                 if (!(t = strndup(w, l)))
69                         return -ENOMEM;
70
71                 k = unit_name_printf(u, t);
72                 free(t);
73
74                 if (!k)
75                         return -ENOMEM;
76
77                 r = unit_add_dependency_by_name(u, d, k, NULL, true);
78                 free(k);
79
80                 if (r < 0)
81                         return r;
82         }
83
84         return 0;
85 }
86
87 static int config_parse_names(
88                 const char *filename,
89                 unsigned line,
90                 const char *section,
91                 const char *lvalue,
92                 const char *rvalue,
93                 void *data,
94                 void *userdata) {
95
96         Unit *u = userdata;
97         char *w;
98         size_t l;
99         char *state;
100
101         assert(filename);
102         assert(lvalue);
103         assert(rvalue);
104         assert(data);
105
106         FOREACH_WORD_QUOTED(w, l, rvalue, state) {
107                 char *t, *k;
108                 int r;
109
110                 if (!(t = strndup(w, l)))
111                         return -ENOMEM;
112
113                 k = unit_name_printf(u, t);
114                 free(t);
115
116                 if (!k)
117                         return -ENOMEM;
118
119                 r = unit_merge_by_name(u, k);
120                 free(k);
121
122                 if (r < 0)
123                         return r;
124         }
125
126         return 0;
127 }
128
129 static int config_parse_string_printf(
130                 const char *filename,
131                 unsigned line,
132                 const char *section,
133                 const char *lvalue,
134                 const char *rvalue,
135                 void *data,
136                 void *userdata) {
137
138         Unit *u = userdata;
139         char **s = data;
140         char *k;
141
142         assert(filename);
143         assert(lvalue);
144         assert(rvalue);
145         assert(s);
146         assert(u);
147
148         if (!(k = unit_full_printf(u, rvalue)))
149                 return -ENOMEM;
150
151         free(*s);
152         if (*k)
153                 *s = k;
154         else {
155                 free(k);
156                 *s = NULL;
157         }
158
159         return 0;
160 }
161
162 static int config_parse_listen(
163                 const char *filename,
164                 unsigned line,
165                 const char *section,
166                 const char *lvalue,
167                 const char *rvalue,
168                 void *data,
169                 void *userdata) {
170
171         int r;
172         SocketPort *p;
173         Socket *s;
174
175         assert(filename);
176         assert(lvalue);
177         assert(rvalue);
178         assert(data);
179
180         s = (Socket*) data;
181
182         if (!(p = new0(SocketPort, 1)))
183                 return -ENOMEM;
184
185         if (streq(lvalue, "ListenFIFO")) {
186                 p->type = SOCKET_FIFO;
187
188                 if (!(p->path = strdup(rvalue))) {
189                         free(p);
190                         return -ENOMEM;
191                 }
192
193                 path_kill_slashes(p->path);
194         } else {
195                 p->type = SOCKET_SOCKET;
196
197                 if ((r = socket_address_parse(&p->address, rvalue)) < 0) {
198                         log_error("[%s:%u] Failed to parse address value: %s", filename, line, rvalue);
199                         free(p);
200                         return r;
201                 }
202
203                 if (streq(lvalue, "ListenStream"))
204                         p->address.type = SOCK_STREAM;
205                 else if (streq(lvalue, "ListenDatagram"))
206                         p->address.type = SOCK_DGRAM;
207                 else {
208                         assert(streq(lvalue, "ListenSequentialPacket"));
209                         p->address.type = SOCK_SEQPACKET;
210                 }
211
212                 if (socket_address_family(&p->address) != AF_LOCAL && p->address.type == SOCK_SEQPACKET) {
213                         free(p);
214                         return -EPROTONOSUPPORT;
215                 }
216         }
217
218         p->fd = -1;
219         LIST_PREPEND(SocketPort, port, s->ports, p);
220
221         return 0;
222 }
223
224 static int config_parse_socket_bind(
225                 const char *filename,
226                 unsigned line,
227                 const char *section,
228                 const char *lvalue,
229                 const char *rvalue,
230                 void *data,
231                 void *userdata) {
232
233         Socket *s;
234         SocketAddressBindIPv6Only b;
235
236         assert(filename);
237         assert(lvalue);
238         assert(rvalue);
239         assert(data);
240
241         s = (Socket*) data;
242
243         if ((b = socket_address_bind_ipv6_only_from_string(rvalue)) < 0) {
244                 int r;
245
246                 if ((r = parse_boolean(rvalue)) < 0) {
247                         log_error("[%s:%u] Failed to parse bind IPv6 only value: %s", filename, line, rvalue);
248                         return -EBADMSG;
249                 }
250
251                 s->bind_ipv6_only = r ? SOCKET_ADDRESS_IPV6_ONLY : SOCKET_ADDRESS_BOTH;
252         } else
253                 s->bind_ipv6_only = b;
254
255         return 0;
256 }
257
258 static int config_parse_nice(
259                 const char *filename,
260                 unsigned line,
261                 const char *section,
262                 const char *lvalue,
263                 const char *rvalue,
264                 void *data,
265                 void *userdata) {
266
267         ExecContext *c = data;
268         int priority, r;
269
270         assert(filename);
271         assert(lvalue);
272         assert(rvalue);
273         assert(data);
274
275         if ((r = safe_atoi(rvalue, &priority)) < 0) {
276                 log_error("[%s:%u] Failed to parse nice priority: %s", filename, line, rvalue);
277                 return r;
278         }
279
280         if (priority < PRIO_MIN || priority >= PRIO_MAX) {
281                 log_error("[%s:%u] Nice priority out of range: %s", filename, line, rvalue);
282                 return -ERANGE;
283         }
284
285         c->nice = priority;
286         c->nice_set = false;
287
288         return 0;
289 }
290
291 static int config_parse_oom_adjust(
292                 const char *filename,
293                 unsigned line,
294                 const char *section,
295                 const char *lvalue,
296                 const char *rvalue,
297                 void *data,
298                 void *userdata) {
299
300         ExecContext *c = data;
301         int oa, r;
302
303         assert(filename);
304         assert(lvalue);
305         assert(rvalue);
306         assert(data);
307
308         if ((r = safe_atoi(rvalue, &oa)) < 0) {
309                 log_error("[%s:%u] Failed to parse OOM adjust value: %s", filename, line, rvalue);
310                 return r;
311         }
312
313         if (oa < OOM_DISABLE || oa > OOM_ADJUST_MAX) {
314                 log_error("[%s:%u] OOM adjust value out of range: %s", filename, line, rvalue);
315                 return -ERANGE;
316         }
317
318         c->oom_adjust = oa;
319         c->oom_adjust_set = true;
320
321         return 0;
322 }
323
324 static int config_parse_mode(
325                 const char *filename,
326                 unsigned line,
327                 const char *section,
328                 const char *lvalue,
329                 const char *rvalue,
330                 void *data,
331                 void *userdata) {
332
333         mode_t *m = data;
334         long l;
335         char *x = NULL;
336
337         assert(filename);
338         assert(lvalue);
339         assert(rvalue);
340         assert(data);
341
342         errno = 0;
343         l = strtol(rvalue, &x, 8);
344         if (!x || *x || errno) {
345                 log_error("[%s:%u] Failed to parse mode value: %s", filename, line, rvalue);
346                 return errno ? -errno : -EINVAL;
347         }
348
349         if (l < 0000 || l > 07777) {
350                 log_error("[%s:%u] mode value out of range: %s", filename, line, rvalue);
351                 return -ERANGE;
352         }
353
354         *m = (mode_t) l;
355         return 0;
356 }
357
358 static int config_parse_exec(
359                 const char *filename,
360                 unsigned line,
361                 const char *section,
362                 const char *lvalue,
363                 const char *rvalue,
364                 void *data,
365                 void *userdata) {
366
367         ExecCommand **e = data, *nce;
368         char *path, **n;
369         unsigned k;
370
371         assert(filename);
372         assert(lvalue);
373         assert(rvalue);
374         assert(e);
375
376         /* We accept an absolute path as first argument, or
377          * alternatively an absolute prefixed with @ to allow
378          * overriding of argv[0]. */
379
380         for (;;) {
381                 char *w;
382                 size_t l;
383                 char *state;
384                 bool honour_argv0, write_to_path;
385
386                 path = NULL;
387                 nce = NULL;
388                 n = NULL;
389
390                 rvalue += strspn(rvalue, WHITESPACE);
391
392                 if (rvalue[0] == 0)
393                         break;
394
395                 honour_argv0 = rvalue[0] == '@';
396
397                 if (rvalue[honour_argv0 ? 1 : 0] != '/') {
398                         log_error("[%s:%u] Invalid executable path in command line: %s", filename, line, rvalue);
399                         return -EINVAL;
400                 }
401
402                 k = 0;
403                 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
404                         if (strncmp(w, ";", l) == 0)
405                                 break;
406
407                         k++;
408                 }
409
410                 if (!(n = new(char*, k + (honour_argv0 ? 0 : 1))))
411                         return -ENOMEM;
412
413                 k = 0;
414                 write_to_path = honour_argv0;
415                 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
416                         if (strncmp(w, ";", l) == 0)
417                                 break;
418
419                         if (write_to_path) {
420                                 if (!(path = cunescape_length(w+1, l-1)))
421                                         goto fail;
422                                 write_to_path = false;
423                         } else {
424                                 if (!(n[k++] = cunescape_length(w, l)))
425                                         goto fail;
426                         }
427                 }
428
429                 n[k] = NULL;
430
431                 if (!n[0]) {
432                         log_error("[%s:%u] Invalid command line: %s", filename, line, rvalue);
433                         strv_free(n);
434                         return -EINVAL;
435                 }
436
437                 if (!path)
438                         if (!(path = strdup(n[0])))
439                                 goto fail;
440
441                 assert(path_is_absolute(path));
442
443                 if (!(nce = new0(ExecCommand, 1)))
444                         goto fail;
445
446                 nce->argv = n;
447                 nce->path = path;
448
449                 path_kill_slashes(nce->path);
450
451                 exec_command_append_list(e, nce);
452
453                 rvalue = state;
454         }
455
456         return 0;
457
458 fail:
459         n[k] = NULL;
460         strv_free(n);
461         free(path);
462         free(nce);
463
464         return -ENOMEM;
465 }
466
467 static int config_parse_usec(
468                 const char *filename,
469                 unsigned line,
470                 const char *section,
471                 const char *lvalue,
472                 const char *rvalue,
473                 void *data,
474                 void *userdata) {
475
476         usec_t *usec = data;
477         int r;
478
479         assert(filename);
480         assert(lvalue);
481         assert(rvalue);
482         assert(data);
483
484         if ((r = parse_usec(rvalue, usec)) < 0) {
485                 log_error("[%s:%u] Failed to parse time value: %s", filename, line, rvalue);
486                 return r;
487         }
488
489         return 0;
490 }
491
492 static DEFINE_CONFIG_PARSE_ENUM(config_parse_service_type, service_type, ServiceType, "Failed to parse service type");
493 static DEFINE_CONFIG_PARSE_ENUM(config_parse_service_restart, service_restart, ServiceRestart, "Failed to parse service restart specifier");
494
495 static int config_parse_bindtodevice(
496                 const char *filename,
497                 unsigned line,
498                 const char *section,
499                 const char *lvalue,
500                 const char *rvalue,
501                 void *data,
502                 void *userdata) {
503
504         Socket *s = data;
505         char *n;
506
507         assert(filename);
508         assert(lvalue);
509         assert(rvalue);
510         assert(data);
511
512         if (rvalue[0] && !streq(rvalue, "*")) {
513                 if (!(n = strdup(rvalue)))
514                         return -ENOMEM;
515         } else
516                 n = NULL;
517
518         free(s->bind_to_device);
519         s->bind_to_device = n;
520
521         return 0;
522 }
523
524 static DEFINE_CONFIG_PARSE_ENUM(config_parse_output, exec_output, ExecOutput, "Failed to parse output specifier");
525 static DEFINE_CONFIG_PARSE_ENUM(config_parse_input, exec_input, ExecInput, "Failed to parse input specifier");
526
527 static int config_parse_facility(
528                 const char *filename,
529                 unsigned line,
530                 const char *section,
531                 const char *lvalue,
532                 const char *rvalue,
533                 void *data,
534                 void *userdata) {
535
536
537         int *o = data, x;
538
539         assert(filename);
540         assert(lvalue);
541         assert(rvalue);
542         assert(data);
543
544         if ((x = log_facility_from_string(rvalue)) < 0) {
545                 log_error("[%s:%u] Failed to parse log facility: %s", filename, line, rvalue);
546                 return -EBADMSG;
547         }
548
549         *o = LOG_MAKEPRI(x, LOG_PRI(*o));
550
551         return 0;
552 }
553
554 static int config_parse_level(
555                 const char *filename,
556                 unsigned line,
557                 const char *section,
558                 const char *lvalue,
559                 const char *rvalue,
560                 void *data,
561                 void *userdata) {
562
563
564         int *o = data, x;
565
566         assert(filename);
567         assert(lvalue);
568         assert(rvalue);
569         assert(data);
570
571         if ((x = log_level_from_string(rvalue)) < 0) {
572                 log_error("[%s:%u] Failed to parse log level: %s", filename, line, rvalue);
573                 return -EBADMSG;
574         }
575
576         *o = LOG_MAKEPRI(LOG_FAC(*o), x);
577         return 0;
578 }
579
580 static int config_parse_io_class(
581                 const char *filename,
582                 unsigned line,
583                 const char *section,
584                 const char *lvalue,
585                 const char *rvalue,
586                 void *data,
587                 void *userdata) {
588
589         ExecContext *c = data;
590         int x;
591
592         assert(filename);
593         assert(lvalue);
594         assert(rvalue);
595         assert(data);
596
597         if ((x = ioprio_class_from_string(rvalue)) < 0) {
598                 log_error("[%s:%u] Failed to parse IO scheduling class: %s", filename, line, rvalue);
599                 return -EBADMSG;
600         }
601
602         c->ioprio = IOPRIO_PRIO_VALUE(x, IOPRIO_PRIO_DATA(c->ioprio));
603         c->ioprio_set = true;
604
605         return 0;
606 }
607
608 static int config_parse_io_priority(
609                 const char *filename,
610                 unsigned line,
611                 const char *section,
612                 const char *lvalue,
613                 const char *rvalue,
614                 void *data,
615                 void *userdata) {
616
617         ExecContext *c = data;
618         int i;
619
620         assert(filename);
621         assert(lvalue);
622         assert(rvalue);
623         assert(data);
624
625         if (safe_atoi(rvalue, &i) < 0 || i < 0 || i >= IOPRIO_BE_NR) {
626                 log_error("[%s:%u] Failed to parse io priority: %s", filename, line, rvalue);
627                 return -EBADMSG;
628         }
629
630         c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_PRIO_CLASS(c->ioprio), i);
631         c->ioprio_set = true;
632
633         return 0;
634 }
635
636 static int config_parse_cpu_sched_policy(
637                 const char *filename,
638                 unsigned line,
639                 const char *section,
640                 const char *lvalue,
641                 const char *rvalue,
642                 void *data,
643                 void *userdata) {
644
645
646         ExecContext *c = data;
647         int x;
648
649         assert(filename);
650         assert(lvalue);
651         assert(rvalue);
652         assert(data);
653
654         if ((x = sched_policy_from_string(rvalue)) < 0) {
655                 log_error("[%s:%u] Failed to parse CPU scheduling policy: %s", filename, line, rvalue);
656                 return -EBADMSG;
657         }
658
659         c->cpu_sched_policy = x;
660         c->cpu_sched_set = true;
661
662         return 0;
663 }
664
665 static int config_parse_cpu_sched_prio(
666                 const char *filename,
667                 unsigned line,
668                 const char *section,
669                 const char *lvalue,
670                 const char *rvalue,
671                 void *data,
672                 void *userdata) {
673
674         ExecContext *c = data;
675         int i;
676
677         assert(filename);
678         assert(lvalue);
679         assert(rvalue);
680         assert(data);
681
682         /* On Linux RR/FIFO have the same range */
683         if (safe_atoi(rvalue, &i) < 0 || i < sched_get_priority_min(SCHED_RR) || i > sched_get_priority_max(SCHED_RR)) {
684                 log_error("[%s:%u] Failed to parse CPU scheduling priority: %s", filename, line, rvalue);
685                 return -EBADMSG;
686         }
687
688         c->cpu_sched_priority = i;
689         c->cpu_sched_set = true;
690
691         return 0;
692 }
693
694 static int config_parse_cpu_affinity(
695                 const char *filename,
696                 unsigned line,
697                 const char *section,
698                 const char *lvalue,
699                 const char *rvalue,
700                 void *data,
701                 void *userdata) {
702
703         ExecContext *c = data;
704         char *w;
705         size_t l;
706         char *state;
707
708         assert(filename);
709         assert(lvalue);
710         assert(rvalue);
711         assert(data);
712
713         FOREACH_WORD_QUOTED(w, l, rvalue, state) {
714                 char *t;
715                 int r;
716                 unsigned cpu;
717
718                 if (!(t = strndup(w, l)))
719                         return -ENOMEM;
720
721                 r = safe_atou(t, &cpu);
722                 free(t);
723
724                 if (!(c->cpuset))
725                         if (!(c->cpuset = cpu_set_malloc(&c->cpuset_ncpus)))
726                                 return -ENOMEM;
727
728                 if (r < 0 || cpu >= c->cpuset_ncpus) {
729                         log_error("[%s:%u] Failed to parse CPU affinity: %s", filename, line, rvalue);
730                         return -EBADMSG;
731                 }
732
733                 CPU_SET_S(cpu, CPU_ALLOC_SIZE(c->cpuset_ncpus), c->cpuset);
734         }
735
736         return 0;
737 }
738
739 static int config_parse_capabilities(
740                 const char *filename,
741                 unsigned line,
742                 const char *section,
743                 const char *lvalue,
744                 const char *rvalue,
745                 void *data,
746                 void *userdata) {
747
748         ExecContext *c = data;
749         cap_t cap;
750
751         assert(filename);
752         assert(lvalue);
753         assert(rvalue);
754         assert(data);
755
756         if (!(cap = cap_from_text(rvalue))) {
757                 if (errno == ENOMEM)
758                         return -ENOMEM;
759
760                 log_error("[%s:%u] Failed to parse capabilities: %s", filename, line, rvalue);
761                 return -EBADMSG;
762         }
763
764         if (c->capabilities)
765                 cap_free(c->capabilities);
766         c->capabilities = cap;
767
768         return 0;
769 }
770
771 static int config_parse_secure_bits(
772                 const char *filename,
773                 unsigned line,
774                 const char *section,
775                 const char *lvalue,
776                 const char *rvalue,
777                 void *data,
778                 void *userdata) {
779
780         ExecContext *c = data;
781         char *w;
782         size_t l;
783         char *state;
784
785         assert(filename);
786         assert(lvalue);
787         assert(rvalue);
788         assert(data);
789
790         FOREACH_WORD_QUOTED(w, l, rvalue, state) {
791                 if (first_word(w, "keep-caps"))
792                         c->secure_bits |= SECURE_KEEP_CAPS;
793                 else if (first_word(w, "keep-caps-locked"))
794                         c->secure_bits |= SECURE_KEEP_CAPS_LOCKED;
795                 else if (first_word(w, "no-setuid-fixup"))
796                         c->secure_bits |= SECURE_NO_SETUID_FIXUP;
797                 else if (first_word(w, "no-setuid-fixup-locked"))
798                         c->secure_bits |= SECURE_NO_SETUID_FIXUP_LOCKED;
799                 else if (first_word(w, "noroot"))
800                         c->secure_bits |= SECURE_NOROOT;
801                 else if (first_word(w, "noroot-locked"))
802                         c->secure_bits |= SECURE_NOROOT_LOCKED;
803                 else {
804                         log_error("[%s:%u] Failed to parse secure bits: %s", filename, line, rvalue);
805                         return -EBADMSG;
806                 }
807         }
808
809         return 0;
810 }
811
812 static int config_parse_bounding_set(
813                 const char *filename,
814                 unsigned line,
815                 const char *section,
816                 const char *lvalue,
817                 const char *rvalue,
818                 void *data,
819                 void *userdata) {
820
821         ExecContext *c = data;
822         char *w;
823         size_t l;
824         char *state;
825
826         assert(filename);
827         assert(lvalue);
828         assert(rvalue);
829         assert(data);
830
831         FOREACH_WORD_QUOTED(w, l, rvalue, state) {
832                 char *t;
833                 int r;
834                 cap_value_t cap;
835
836                 if (!(t = strndup(w, l)))
837                         return -ENOMEM;
838
839                 r = cap_from_name(t, &cap);
840                 free(t);
841
842                 if (r < 0) {
843                         log_error("[%s:%u] Failed to parse capability bounding set: %s", filename, line, rvalue);
844                         return -EBADMSG;
845                 }
846
847                 c->capability_bounding_set_drop |= 1 << cap;
848         }
849
850         return 0;
851 }
852
853 static int config_parse_timer_slack_nsec(
854                 const char *filename,
855                 unsigned line,
856                 const char *section,
857                 const char *lvalue,
858                 const char *rvalue,
859                 void *data,
860                 void *userdata) {
861
862         ExecContext *c = data;
863         unsigned long u;
864         int r;
865
866         assert(filename);
867         assert(lvalue);
868         assert(rvalue);
869         assert(data);
870
871         if ((r = safe_atolu(rvalue, &u)) < 0) {
872                 log_error("[%s:%u] Failed to parse time slack value: %s", filename, line, rvalue);
873                 return r;
874         }
875
876         c->timer_slack_nsec = u;
877
878         return 0;
879 }
880
881 static int config_parse_limit(
882                 const char *filename,
883                 unsigned line,
884                 const char *section,
885                 const char *lvalue,
886                 const char *rvalue,
887                 void *data,
888                 void *userdata) {
889
890         struct rlimit **rl = data;
891         unsigned long long u;
892         int r;
893
894         assert(filename);
895         assert(lvalue);
896         assert(rvalue);
897         assert(data);
898
899         if ((r = safe_atollu(rvalue, &u)) < 0) {
900                 log_error("[%s:%u] Failed to parse resource value: %s", filename, line, rvalue);
901                 return r;
902         }
903
904         if (!*rl)
905                 if (!(*rl = new(struct rlimit, 1)))
906                         return -ENOMEM;
907
908         (*rl)->rlim_cur = (*rl)->rlim_max = (rlim_t) u;
909         return 0;
910 }
911
912 static int config_parse_cgroup(
913                 const char *filename,
914                 unsigned line,
915                 const char *section,
916                 const char *lvalue,
917                 const char *rvalue,
918                 void *data,
919                 void *userdata) {
920
921         Unit *u = userdata;
922         char *w;
923         size_t l;
924         char *state;
925
926         FOREACH_WORD_QUOTED(w, l, rvalue, state) {
927                 char *t;
928                 int r;
929
930                 if (!(t = cunescape_length(w, l)))
931                         return -ENOMEM;
932
933                 r = unit_add_cgroup_from_text(u, t);
934                 free(t);
935
936                 if (r < 0)
937                         return r;
938         }
939
940         return 0;
941 }
942
943 static int config_parse_sysv_priority(
944                 const char *filename,
945                 unsigned line,
946                 const char *section,
947                 const char *lvalue,
948                 const char *rvalue,
949                 void *data,
950                 void *userdata) {
951
952         int *priority = data;
953         int r, i;
954
955         assert(filename);
956         assert(lvalue);
957         assert(rvalue);
958         assert(data);
959
960         if ((r = safe_atoi(rvalue, &i)) < 0 || i < 0) {
961                 log_error("[%s:%u] Failed to parse SysV start priority: %s", filename, line, rvalue);
962                 return r;
963         }
964
965         *priority = (int) i;
966         return 0;
967 }
968
969 static DEFINE_CONFIG_PARSE_ENUM(config_parse_kill_mode, kill_mode, KillMode, "Failed to parse kill mode");
970
971 static int config_parse_mount_flags(
972                 const char *filename,
973                 unsigned line,
974                 const char *section,
975                 const char *lvalue,
976                 const char *rvalue,
977                 void *data,
978                 void *userdata) {
979
980         ExecContext *c = data;
981         char *w;
982         size_t l;
983         char *state;
984         unsigned long flags = 0;
985
986         assert(filename);
987         assert(lvalue);
988         assert(rvalue);
989         assert(data);
990
991         FOREACH_WORD_QUOTED(w, l, rvalue, state) {
992                 if (strncmp(w, "shared", l) == 0)
993                         flags |= MS_SHARED;
994                 else if (strncmp(w, "slave", l) == 0)
995                         flags |= MS_SLAVE;
996                 else if (strncmp(w, "private", l) == 0)
997                         flags |= MS_PRIVATE;
998                 else {
999                         log_error("[%s:%u] Failed to parse mount flags: %s", filename, line, rvalue);
1000                         return -EINVAL;
1001                 }
1002         }
1003
1004         c->mount_flags = flags;
1005         return 0;
1006 }
1007
1008 static int config_parse_timer(
1009                 const char *filename,
1010                 unsigned line,
1011                 const char *section,
1012                 const char *lvalue,
1013                 const char *rvalue,
1014                 void *data,
1015                 void *userdata) {
1016
1017         Timer *t = data;
1018         usec_t u;
1019         int r;
1020         TimerValue *v;
1021         TimerBase b;
1022
1023         assert(filename);
1024         assert(lvalue);
1025         assert(rvalue);
1026         assert(data);
1027
1028         if ((b = timer_base_from_string(lvalue)) < 0) {
1029                 log_error("[%s:%u] Failed to parse timer base: %s", filename, line, lvalue);
1030                 return -EINVAL;
1031         }
1032
1033         if ((r = parse_usec(rvalue, &u)) < 0) {
1034                 log_error("[%s:%u] Failed to parse timer value: %s", filename, line, rvalue);
1035                 return r;
1036         }
1037
1038         if (!(v = new0(TimerValue, 1)))
1039                 return -ENOMEM;
1040
1041         v->base = b;
1042         v->value = u;
1043
1044         LIST_PREPEND(TimerValue, value, t->values, v);
1045
1046         return 0;
1047 }
1048
1049 static int config_parse_timer_unit(
1050                 const char *filename,
1051                 unsigned line,
1052                 const char *section,
1053                 const char *lvalue,
1054                 const char *rvalue,
1055                 void *data,
1056                 void *userdata) {
1057
1058         Timer *t = data;
1059         int r;
1060
1061         if (endswith(rvalue, ".timer")) {
1062                 log_error("[%s:%u] Unit cannot be of type timer: %s", filename, line, rvalue);
1063                 return -EINVAL;
1064         }
1065
1066         if ((r = manager_load_unit(t->meta.manager, rvalue, NULL, &t->unit)) < 0) {
1067                 log_error("[%s:%u] Failed to load unit: %s", filename, line, rvalue);
1068                 return r;
1069         }
1070
1071         return 0;
1072 }
1073
1074 static int config_parse_path_spec(
1075                 const char *filename,
1076                 unsigned line,
1077                 const char *section,
1078                 const char *lvalue,
1079                 const char *rvalue,
1080                 void *data,
1081                 void *userdata) {
1082
1083         Path *p = data;
1084         PathSpec *s;
1085         PathType b;
1086
1087         assert(filename);
1088         assert(lvalue);
1089         assert(rvalue);
1090         assert(data);
1091
1092         if ((b = path_type_from_string(lvalue)) < 0) {
1093                 log_error("[%s:%u] Failed to parse path type: %s", filename, line, lvalue);
1094                 return -EINVAL;
1095         }
1096
1097         if (!path_is_absolute(rvalue)) {
1098                 log_error("[%s:%u] Path is not absolute: %s", filename, line, rvalue);
1099                 return -EINVAL;
1100         }
1101
1102         if (!(s = new0(PathSpec, 1)))
1103                 return -ENOMEM;
1104
1105         if (!(s->path = strdup(rvalue))) {
1106                 free(s);
1107                 return -ENOMEM;
1108         }
1109
1110         path_kill_slashes(s->path);
1111
1112         s->type = b;
1113         s->inotify_fd = -1;
1114
1115         LIST_PREPEND(PathSpec, spec, p->specs, s);
1116
1117         return 0;
1118 }
1119
1120 static int config_parse_path_unit(
1121                 const char *filename,
1122                 unsigned line,
1123                 const char *section,
1124                 const char *lvalue,
1125                 const char *rvalue,
1126                 void *data,
1127                 void *userdata) {
1128
1129         Path *t = data;
1130         int r;
1131
1132         if (endswith(rvalue, ".path")) {
1133                 log_error("[%s:%u] Unit cannot be of type path: %s", filename, line, rvalue);
1134                 return -EINVAL;
1135         }
1136
1137         if ((r = manager_load_unit(t->meta.manager, rvalue, NULL, &t->unit)) < 0) {
1138                 log_error("[%s:%u] Failed to load unit: %s", filename, line, rvalue);
1139                 return r;
1140         }
1141
1142         return 0;
1143 }
1144
1145 static int config_parse_env_file(
1146                 const char *filename,
1147                 unsigned line,
1148                 const char *section,
1149                 const char *lvalue,
1150                 const char *rvalue,
1151                 void *data,
1152                 void *userdata) {
1153
1154         FILE *f;
1155         int r;
1156         char ***env = data;
1157
1158         assert(filename);
1159         assert(lvalue);
1160         assert(rvalue);
1161         assert(data);
1162
1163         if (!(f = fopen(rvalue, "re"))) {
1164                 log_error("[%s:%u] Failed to open environment file '%s': %m", filename, line, rvalue);
1165                 return -errno;
1166         }
1167
1168         while (!feof(f)) {
1169                 char l[LINE_MAX], *p;
1170                 char **t;
1171
1172                 if (!fgets(l, sizeof(l), f)) {
1173                         if (feof(f))
1174                                 break;
1175
1176                         r = -errno;
1177                         log_error("[%s:%u] Failed to read environment file '%s': %m", filename, line, rvalue);
1178                         goto finish;
1179                 }
1180
1181                 p = strstrip(l);
1182
1183                 if (!*p)
1184                         continue;
1185
1186                 if (strchr(COMMENTS, *p))
1187                         continue;
1188
1189                 t = strv_env_set(*env, p);
1190                 strv_free(*env);
1191                 *env = t;
1192         }
1193
1194         r = 0;
1195
1196 finish:
1197         if (f)
1198                 fclose(f);
1199
1200         return r;
1201 }
1202
1203 static int config_parse_ip_tos(
1204                 const char *filename,
1205                 unsigned line,
1206                 const char *section,
1207                 const char *lvalue,
1208                 const char *rvalue,
1209                 void *data,
1210                 void *userdata) {
1211
1212         int *ip_tos = data, x;
1213         int r;
1214
1215         assert(filename);
1216         assert(lvalue);
1217         assert(rvalue);
1218         assert(data);
1219
1220         if ((x = ip_tos_from_string(rvalue)) < 0)
1221                 if ((r = safe_atoi(rvalue, &x)) < 0) {
1222                         log_error("[%s:%u] Failed to parse IP TOS value: %s", filename, line, rvalue);
1223                         return r;
1224                 }
1225
1226         *ip_tos = x;
1227         return 0;
1228 }
1229
1230 static DEFINE_CONFIG_PARSE_ENUM(config_parse_notify_access, notify_access, NotifyAccess, "Failed to parse notify access specifier");
1231
1232 #define FOLLOW_MAX 8
1233
1234 static int open_follow(char **filename, FILE **_f, Set *names, char **_final) {
1235         unsigned c = 0;
1236         int fd, r;
1237         FILE *f;
1238         char *id = NULL;
1239
1240         assert(filename);
1241         assert(*filename);
1242         assert(_f);
1243         assert(names);
1244
1245         /* This will update the filename pointer if the loaded file is
1246          * reached by a symlink. The old string will be freed. */
1247
1248         for (;;) {
1249                 char *target, *name;
1250
1251                 if (c++ >= FOLLOW_MAX)
1252                         return -ELOOP;
1253
1254                 path_kill_slashes(*filename);
1255
1256                 /* Add the file name we are currently looking at to
1257                  * the names of this unit */
1258                 name = file_name_from_path(*filename);
1259                 if (!(id = set_get(names, name))) {
1260
1261                         if (!(id = strdup(name)))
1262                                 return -ENOMEM;
1263
1264                         if ((r = set_put(names, id)) < 0) {
1265                                 free(id);
1266                                 return r;
1267                         }
1268                 }
1269
1270                 /* Try to open the file name, but don't if its a symlink */
1271                 if ((fd = open(*filename, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW)) >= 0)
1272                         break;
1273
1274                 if (errno != ELOOP)
1275                         return -errno;
1276
1277                 /* Hmm, so this is a symlink. Let's read the name, and follow it manually */
1278                 if ((r = readlink_and_make_absolute(*filename, &target)) < 0)
1279                         return r;
1280
1281                 free(*filename);
1282                 *filename = target;
1283         }
1284
1285         if (!(f = fdopen(fd, "r"))) {
1286                 r = -errno;
1287                 close_nointr_nofail(fd);
1288                 return r;
1289         }
1290
1291         *_f = f;
1292         *_final = id;
1293         return 0;
1294 }
1295
1296 static int merge_by_names(Unit **u, Set *names, const char *id) {
1297         char *k;
1298         int r;
1299
1300         assert(u);
1301         assert(*u);
1302         assert(names);
1303
1304         /* Let's try to add in all symlink names we found */
1305         while ((k = set_steal_first(names))) {
1306
1307                 /* First try to merge in the other name into our
1308                  * unit */
1309                 if ((r = unit_merge_by_name(*u, k)) < 0) {
1310                         Unit *other;
1311
1312                         /* Hmm, we couldn't merge the other unit into
1313                          * ours? Then let's try it the other way
1314                          * round */
1315
1316                         other = manager_get_unit((*u)->meta.manager, k);
1317                         free(k);
1318
1319                         if (other)
1320                                 if ((r = unit_merge(other, *u)) >= 0) {
1321                                         *u = other;
1322                                         return merge_by_names(u, names, NULL);
1323                                 }
1324
1325                         return r;
1326                 }
1327
1328                 if (id == k)
1329                         unit_choose_id(*u, id);
1330
1331                 free(k);
1332         }
1333
1334         return 0;
1335 }
1336
1337 static void dump_items(FILE *f, const ConfigItem *items) {
1338         const ConfigItem *i;
1339         const char *prev_section = NULL;
1340         bool not_first = false;
1341
1342         struct {
1343                 ConfigParserCallback callback;
1344                 const char *rvalue;
1345         } table[] = {
1346                 { config_parse_int,              "INTEGER" },
1347                 { config_parse_unsigned,         "UNSIGNED" },
1348                 { config_parse_size,             "SIZE" },
1349                 { config_parse_bool,             "BOOLEAN" },
1350                 { config_parse_string,           "STRING" },
1351                 { config_parse_path,             "PATH" },
1352                 { config_parse_strv,             "STRING [...]" },
1353                 { config_parse_nice,             "NICE" },
1354                 { config_parse_oom_adjust,       "OOMADJUST" },
1355                 { config_parse_io_class,         "IOCLASS" },
1356                 { config_parse_io_priority,      "IOPRIORITY" },
1357                 { config_parse_cpu_sched_policy, "CPUSCHEDPOLICY" },
1358                 { config_parse_cpu_sched_prio,   "CPUSCHEDPRIO" },
1359                 { config_parse_cpu_affinity,     "CPUAFFINITY" },
1360                 { config_parse_mode,             "MODE" },
1361                 { config_parse_env_file,         "FILE" },
1362                 { config_parse_output,           "OUTPUT" },
1363                 { config_parse_input,            "INPUT" },
1364                 { config_parse_facility,         "FACILITY" },
1365                 { config_parse_level,            "LEVEL" },
1366                 { config_parse_capabilities,     "CAPABILITIES" },
1367                 { config_parse_secure_bits,      "SECUREBITS" },
1368                 { config_parse_bounding_set,     "BOUNDINGSET" },
1369                 { config_parse_timer_slack_nsec, "TIMERSLACK" },
1370                 { config_parse_limit,            "LIMIT" },
1371                 { config_parse_cgroup,           "CGROUP [...]" },
1372                 { config_parse_deps,             "UNIT [...]" },
1373                 { config_parse_names,            "UNIT [...]" },
1374                 { config_parse_exec,             "PATH [ARGUMENT [...]]" },
1375                 { config_parse_service_type,     "SERVICETYPE" },
1376                 { config_parse_service_restart,  "SERVICERESTART" },
1377                 { config_parse_sysv_priority,    "SYSVPRIORITY" },
1378                 { config_parse_kill_mode,        "KILLMODE" },
1379                 { config_parse_listen,           "SOCKET [...]" },
1380                 { config_parse_socket_bind,      "SOCKETBIND" },
1381                 { config_parse_bindtodevice,     "NETWORKINTERFACE" },
1382                 { config_parse_usec,             "SECONDS" },
1383                 { config_parse_path_strv,        "PATH [...]" },
1384                 { config_parse_mount_flags,      "MOUNTFLAG [...]" },
1385                 { config_parse_string_printf,    "STRING" },
1386                 { config_parse_timer,            "TIMER" },
1387                 { config_parse_timer_unit,       "NAME" },
1388                 { config_parse_path_spec,        "PATH" },
1389                 { config_parse_path_unit,        "UNIT" },
1390                 { config_parse_notify_access,    "ACCESS" },
1391                 { config_parse_ip_tos,           "TOS" },
1392         };
1393
1394         assert(f);
1395         assert(items);
1396
1397         for (i = items; i->lvalue; i++) {
1398                 unsigned j;
1399                 const char *rvalue = "OTHER";
1400
1401                 if (!streq_ptr(i->section, prev_section)) {
1402                         if (!not_first)
1403                                 not_first = true;
1404                         else
1405                                 fputc('\n', f);
1406
1407                         fprintf(f, "[%s]\n", i->section);
1408                         prev_section = i->section;
1409                 }
1410
1411                 for (j = 0; j < ELEMENTSOF(table); j++)
1412                         if (i->parse == table[j].callback) {
1413                                 rvalue = table[j].rvalue;
1414                                 break;
1415                         }
1416
1417                 fprintf(f, "%s=%s\n", i->lvalue, rvalue);
1418         }
1419 }
1420
1421 static int load_from_path(Unit *u, const char *path) {
1422
1423         static const char* const section_table[_UNIT_TYPE_MAX] = {
1424                 [UNIT_SERVICE]   = "Service",
1425                 [UNIT_TIMER]     = "Timer",
1426                 [UNIT_SOCKET]    = "Socket",
1427                 [UNIT_TARGET]    = "Target",
1428                 [UNIT_DEVICE]    = "Device",
1429                 [UNIT_MOUNT]     = "Mount",
1430                 [UNIT_AUTOMOUNT] = "Automount",
1431                 [UNIT_SNAPSHOT]  = "Snapshot",
1432                 [UNIT_SWAP]      = "Swap",
1433                 [UNIT_PATH]      = "Path"
1434         };
1435
1436 #define EXEC_CONTEXT_CONFIG_ITEMS(context, section) \
1437                 { "WorkingDirectory",       config_parse_path,            &(context).working_directory,                    section   }, \
1438                 { "RootDirectory",          config_parse_path,            &(context).root_directory,                       section   }, \
1439                 { "User",                   config_parse_string_printf,   &(context).user,                                 section   }, \
1440                 { "Group",                  config_parse_string_printf,   &(context).group,                                section   }, \
1441                 { "SupplementaryGroups",    config_parse_strv,            &(context).supplementary_groups,                 section   }, \
1442                 { "Nice",                   config_parse_nice,            &(context),                                      section   }, \
1443                 { "OOMAdjust",              config_parse_oom_adjust,      &(context),                                      section   }, \
1444                 { "IOSchedulingClass",      config_parse_io_class,        &(context),                                      section   }, \
1445                 { "IOSchedulingPriority",   config_parse_io_priority,     &(context),                                      section   }, \
1446                 { "CPUSchedulingPolicy",    config_parse_cpu_sched_policy,&(context),                                      section   }, \
1447                 { "CPUSchedulingPriority",  config_parse_cpu_sched_prio,  &(context),                                      section   }, \
1448                 { "CPUSchedulingResetOnFork", config_parse_bool,          &(context).cpu_sched_reset_on_fork,              section   }, \
1449                 { "CPUAffinity",            config_parse_cpu_affinity,    &(context),                                      section   }, \
1450                 { "UMask",                  config_parse_mode,            &(context).umask,                                section   }, \
1451                 { "Environment",            config_parse_strv,            &(context).environment,                          section   }, \
1452                 { "EnvironmentFile",        config_parse_env_file,        &(context).environment,                          section   }, \
1453                 { "StandardInput",          config_parse_input,           &(context).std_input,                            section   }, \
1454                 { "StandardOutput",         config_parse_output,          &(context).std_output,                           section   }, \
1455                 { "StandardError",          config_parse_output,          &(context).std_error,                            section   }, \
1456                 { "TTYPath",                config_parse_path,            &(context).tty_path,                             section   }, \
1457                 { "SyslogIdentifier",       config_parse_string_printf,   &(context).syslog_identifier,                    section   }, \
1458                 { "SyslogFacility",         config_parse_facility,        &(context).syslog_priority,                      section   }, \
1459                 { "SyslogLevel",            config_parse_level,           &(context).syslog_priority,                      section   }, \
1460                 { "SyslogLevelPrefix",      config_parse_bool,            &(context).syslog_level_prefix,                  section   }, \
1461                 { "Capabilities",           config_parse_capabilities,    &(context),                                      section   }, \
1462                 { "SecureBits",             config_parse_secure_bits,     &(context),                                      section   }, \
1463                 { "CapabilityBoundingSetDrop", config_parse_bounding_set, &(context),                                      section   }, \
1464                 { "TimerSlackNSec",         config_parse_timer_slack_nsec,&(context),                                      section   }, \
1465                 { "LimitCPU",               config_parse_limit,           &(context).rlimit[RLIMIT_CPU],                   section   }, \
1466                 { "LimitFSIZE",             config_parse_limit,           &(context).rlimit[RLIMIT_FSIZE],                 section   }, \
1467                 { "LimitDATA",              config_parse_limit,           &(context).rlimit[RLIMIT_DATA],                  section   }, \
1468                 { "LimitSTACK",             config_parse_limit,           &(context).rlimit[RLIMIT_STACK],                 section   }, \
1469                 { "LimitCORE",              config_parse_limit,           &(context).rlimit[RLIMIT_CORE],                  section   }, \
1470                 { "LimitRSS",               config_parse_limit,           &(context).rlimit[RLIMIT_RSS],                   section   }, \
1471                 { "LimitNOFILE",            config_parse_limit,           &(context).rlimit[RLIMIT_NOFILE],                section   }, \
1472                 { "LimitAS",                config_parse_limit,           &(context).rlimit[RLIMIT_AS],                    section   }, \
1473                 { "LimitNPROC",             config_parse_limit,           &(context).rlimit[RLIMIT_NPROC],                 section   }, \
1474                 { "LimitMEMLOCK",           config_parse_limit,           &(context).rlimit[RLIMIT_MEMLOCK],               section   }, \
1475                 { "LimitLOCKS",             config_parse_limit,           &(context).rlimit[RLIMIT_LOCKS],                 section   }, \
1476                 { "LimitSIGPENDING",        config_parse_limit,           &(context).rlimit[RLIMIT_SIGPENDING],            section   }, \
1477                 { "LimitMSGQUEUE",          config_parse_limit,           &(context).rlimit[RLIMIT_MSGQUEUE],              section   }, \
1478                 { "LimitNICE",              config_parse_limit,           &(context).rlimit[RLIMIT_NICE],                  section   }, \
1479                 { "LimitRTPRIO",            config_parse_limit,           &(context).rlimit[RLIMIT_RTPRIO],                section   }, \
1480                 { "LimitRTTIME",            config_parse_limit,           &(context).rlimit[RLIMIT_RTTIME],                section   }, \
1481                 { "ControlGroup",           config_parse_cgroup,          u,                                               section   }, \
1482                 { "ReadWriteDirectories",   config_parse_path_strv,       &(context).read_write_dirs,                      section   }, \
1483                 { "ReadOnlyDirectories",    config_parse_path_strv,       &(context).read_only_dirs,                       section   }, \
1484                 { "InaccessibleDirectories",config_parse_path_strv,       &(context).inaccessible_dirs,                    section   }, \
1485                 { "PrivateTmp",             config_parse_bool,            &(context).private_tmp,                          section   }, \
1486                 { "MountFlags",             config_parse_mount_flags,     &(context),                                      section   }, \
1487                 { "TCPWrapName",            config_parse_string_printf,   &(context).tcpwrap_name,                         section   }, \
1488                 { "PAMName",                config_parse_string_printf,   &(context).pam_name,                             section   }
1489
1490         const ConfigItem items[] = {
1491                 { "Names",                  config_parse_names,           u,                                               "Unit"    },
1492                 { "Description",            config_parse_string_printf,   &u->meta.description,                            "Unit"    },
1493                 { "Requires",               config_parse_deps,            UINT_TO_PTR(UNIT_REQUIRES),                      "Unit"    },
1494                 { "RequiresOverridable",    config_parse_deps,            UINT_TO_PTR(UNIT_REQUIRES_OVERRIDABLE),          "Unit"    },
1495                 { "Requisite",              config_parse_deps,            UINT_TO_PTR(UNIT_REQUISITE),                     "Unit"    },
1496                 { "RequisiteOverridable",   config_parse_deps,            UINT_TO_PTR(UNIT_REQUISITE_OVERRIDABLE),         "Unit"    },
1497                 { "Wants",                  config_parse_deps,            UINT_TO_PTR(UNIT_WANTS),                         "Unit"    },
1498                 { "Conflicts",              config_parse_deps,            UINT_TO_PTR(UNIT_CONFLICTS),                     "Unit"    },
1499                 { "Before",                 config_parse_deps,            UINT_TO_PTR(UNIT_BEFORE),                        "Unit"    },
1500                 { "After",                  config_parse_deps,            UINT_TO_PTR(UNIT_AFTER),                         "Unit"    },
1501                 { "RecursiveStop",          config_parse_bool,            &u->meta.recursive_stop,                         "Unit"    },
1502                 { "StopWhenUnneeded",       config_parse_bool,            &u->meta.stop_when_unneeded,                     "Unit"    },
1503                 { "OnlyByDependency",       config_parse_bool,            &u->meta.only_by_dependency,                     "Unit"    },
1504                 { "DefaultDependencies",    config_parse_bool,            &u->meta.default_dependencies,                   "Unit"    },
1505
1506                 { "PIDFile",                config_parse_path,            &u->service.pid_file,                            "Service" },
1507                 { "ExecStartPre",           config_parse_exec,            u->service.exec_command+SERVICE_EXEC_START_PRE,  "Service" },
1508                 { "ExecStart",              config_parse_exec,            u->service.exec_command+SERVICE_EXEC_START,      "Service" },
1509                 { "ExecStartPost",          config_parse_exec,            u->service.exec_command+SERVICE_EXEC_START_POST, "Service" },
1510                 { "ExecReload",             config_parse_exec,            u->service.exec_command+SERVICE_EXEC_RELOAD,     "Service" },
1511                 { "ExecStop",               config_parse_exec,            u->service.exec_command+SERVICE_EXEC_STOP,       "Service" },
1512                 { "ExecStopPost",           config_parse_exec,            u->service.exec_command+SERVICE_EXEC_STOP_POST,  "Service" },
1513                 { "RestartSec",             config_parse_usec,            &u->service.restart_usec,                        "Service" },
1514                 { "TimeoutSec",             config_parse_usec,            &u->service.timeout_usec,                        "Service" },
1515                 { "Type",                   config_parse_service_type,    &u->service.type,                                "Service" },
1516                 { "Restart",                config_parse_service_restart, &u->service.restart,                             "Service" },
1517                 { "PermissionsStartOnly",   config_parse_bool,            &u->service.permissions_start_only,              "Service" },
1518                 { "RootDirectoryStartOnly", config_parse_bool,            &u->service.root_directory_start_only,           "Service" },
1519                 { "ValidNoProcess",         config_parse_bool,            &u->service.valid_no_process,                    "Service" },
1520                 { "SysVStartPriority",      config_parse_sysv_priority,   &u->service.sysv_start_priority,                 "Service" },
1521                 { "KillMode",               config_parse_kill_mode,       &u->service.kill_mode,                           "Service" },
1522                 { "NonBlocking",            config_parse_bool,            &u->service.exec_context.non_blocking,           "Service" },
1523                 { "BusName",                config_parse_string_printf,   &u->service.bus_name,                            "Service" },
1524                 { "NotifyAccess",           config_parse_notify_access,   &u->service.notify_access,                       "Service" },
1525                 EXEC_CONTEXT_CONFIG_ITEMS(u->service.exec_context, "Service"),
1526
1527                 { "ListenStream",           config_parse_listen,          &u->socket,                                      "Socket"  },
1528                 { "ListenDatagram",         config_parse_listen,          &u->socket,                                      "Socket"  },
1529                 { "ListenSequentialPacket", config_parse_listen,          &u->socket,                                      "Socket"  },
1530                 { "ListenFIFO",             config_parse_listen,          &u->socket,                                      "Socket"  },
1531                 { "BindIPv6Only",           config_parse_socket_bind,     &u->socket,                                      "Socket"  },
1532                 { "Backlog",                config_parse_unsigned,        &u->socket.backlog,                              "Socket"  },
1533                 { "BindToDevice",           config_parse_bindtodevice,    &u->socket,                                      "Socket"  },
1534                 { "ExecStartPre",           config_parse_exec,            u->socket.exec_command+SOCKET_EXEC_START_PRE,    "Socket"  },
1535                 { "ExecStartPost",          config_parse_exec,            u->socket.exec_command+SOCKET_EXEC_START_POST,   "Socket"  },
1536                 { "ExecStopPre",            config_parse_exec,            u->socket.exec_command+SOCKET_EXEC_STOP_PRE,     "Socket"  },
1537                 { "ExecStopPost",           config_parse_exec,            u->socket.exec_command+SOCKET_EXEC_STOP_POST,    "Socket"  },
1538                 { "TimeoutSec",             config_parse_usec,            &u->socket.timeout_usec,                         "Socket"  },
1539                 { "DirectoryMode",          config_parse_mode,            &u->socket.directory_mode,                       "Socket"  },
1540                 { "SocketMode",             config_parse_mode,            &u->socket.socket_mode,                          "Socket"  },
1541                 { "KillMode",               config_parse_kill_mode,       &u->socket.kill_mode,                            "Socket"  },
1542                 { "Accept",                 config_parse_bool,            &u->socket.accept,                               "Socket"  },
1543                 { "MaxConnections",         config_parse_unsigned,        &u->socket.max_connections,                      "Socket"  },
1544                 { "KeepAlive",              config_parse_bool,            &u->socket.keep_alive,                           "Socket"  },
1545                 { "Priority",               config_parse_int,             &u->socket.priority,                             "Socket"  },
1546                 { "ReceiveBuffer",          config_parse_size,            &u->socket.receive_buffer,                       "Socket"  },
1547                 { "SendBuffer",             config_parse_size,            &u->socket.send_buffer,                          "Socket"  },
1548                 { "IPTOS",                  config_parse_ip_tos,          &u->socket.ip_tos,                               "Socket"  },
1549                 { "IPTTL",                  config_parse_int,             &u->socket.ip_ttl,                               "Socket"  },
1550                 { "Mark",                   config_parse_int,             &u->socket.mark,                                 "Socket"  },
1551                 { "PipeSize",               config_parse_size,            &u->socket.pipe_size,                            "Socket"  },
1552                 { "FreeBind",               config_parse_bool,            &u->socket.free_bind,                            "Socket"  },
1553                 EXEC_CONTEXT_CONFIG_ITEMS(u->socket.exec_context, "Socket"),
1554
1555                 { "What",                   config_parse_string,          &u->mount.parameters_fragment.what,              "Mount"   },
1556                 { "Where",                  config_parse_path,            &u->mount.where,                                 "Mount"   },
1557                 { "Options",                config_parse_string,          &u->mount.parameters_fragment.options,           "Mount"   },
1558                 { "Type",                   config_parse_string,          &u->mount.parameters_fragment.fstype,            "Mount"   },
1559                 { "TimeoutSec",             config_parse_usec,            &u->mount.timeout_usec,                          "Mount"   },
1560                 { "KillMode",               config_parse_kill_mode,       &u->mount.kill_mode,                             "Mount"   },
1561                 { "DirectoryMode",          config_parse_mode,            &u->mount.directory_mode,                        "Mount"   },
1562                 EXEC_CONTEXT_CONFIG_ITEMS(u->mount.exec_context, "Mount"),
1563
1564                 { "Where",                  config_parse_path,            &u->automount.where,                             "Automount" },
1565                 { "DirectoryMode",          config_parse_mode,            &u->automount.directory_mode,                    "Automount" },
1566
1567                 { "What",                   config_parse_path,            &u->swap.parameters_fragment.what,               "Swap"    },
1568                 { "Priority",               config_parse_int,             &u->swap.parameters_fragment.priority,           "Swap"    },
1569
1570                 { "OnActiveSec",            config_parse_timer,           &u->timer,                                       "Timer"   },
1571                 { "OnBootSec",              config_parse_timer,           &u->timer,                                       "Timer"   },
1572                 { "OnStartupSec",           config_parse_timer,           &u->timer,                                       "Timer"   },
1573                 { "OnUnitActiveSec",        config_parse_timer,           &u->timer,                                       "Timer"   },
1574                 { "OnUnitInactiveSec",      config_parse_timer,           &u->timer,                                       "Timer"   },
1575                 { "Unit",                   config_parse_timer_unit,      &u->timer,                                       "Timer"   },
1576
1577                 { "PathExists",             config_parse_path_spec,       &u->path,                                        "Path"    },
1578                 { "PathChanged",            config_parse_path_spec,       &u->path,                                        "Path"    },
1579                 { "DirectoryNotEmpty",      config_parse_path_spec,       &u->path,                                        "Path"    },
1580                 { "Unit",                   config_parse_path_unit,       &u->path,                                        "Path"    },
1581
1582                 /* The [Install] section is ignored here. */
1583                 { "Alias",                  NULL,                         NULL,                                            "Install" },
1584                 { "WantedBy",               NULL,                         NULL,                                            "Install" },
1585                 { "Also",                   NULL,                         NULL,                                            "Install" },
1586
1587                 { NULL, NULL, NULL, NULL }
1588         };
1589
1590 #undef EXEC_CONTEXT_CONFIG_ITEMS
1591
1592         const char *sections[4];
1593         int r;
1594         Set *symlink_names;
1595         FILE *f = NULL;
1596         char *filename = NULL, *id = NULL;
1597         Unit *merged;
1598
1599         if (!u) {
1600                 /* Dirty dirty hack. */
1601                 dump_items((FILE*) path, items);
1602                 return 0;
1603         }
1604
1605         assert(u);
1606         assert(path);
1607
1608         sections[0] = "Unit";
1609         sections[1] = section_table[u->meta.type];
1610         sections[2] = "Install";
1611         sections[3] = NULL;
1612
1613         if (!(symlink_names = set_new(string_hash_func, string_compare_func)))
1614                 return -ENOMEM;
1615
1616         if (path_is_absolute(path)) {
1617
1618                 if (!(filename = strdup(path))) {
1619                         r = -ENOMEM;
1620                         goto finish;
1621                 }
1622
1623                 if ((r = open_follow(&filename, &f, symlink_names, &id)) < 0) {
1624                         free(filename);
1625                         filename = NULL;
1626
1627                         if (r != -ENOENT)
1628                                 goto finish;
1629                 }
1630
1631         } else  {
1632                 char **p;
1633
1634                 STRV_FOREACH(p, u->meta.manager->lookup_paths.unit_path) {
1635
1636                         /* Instead of opening the path right away, we manually
1637                          * follow all symlinks and add their name to our unit
1638                          * name set while doing so */
1639                         if (!(filename = path_make_absolute(path, *p))) {
1640                                 r = -ENOMEM;
1641                                 goto finish;
1642                         }
1643
1644                         if ((r = open_follow(&filename, &f, symlink_names, &id)) < 0) {
1645                                 char *sn;
1646
1647                                 free(filename);
1648                                 filename = NULL;
1649
1650                                 if (r != -ENOENT)
1651                                         goto finish;
1652
1653                                 /* Empty the symlink names for the next run */
1654                                 while ((sn = set_steal_first(symlink_names)))
1655                                         free(sn);
1656
1657                                 continue;
1658                         }
1659
1660                         break;
1661                 }
1662         }
1663
1664         if (!filename) {
1665                 r = 0;
1666                 goto finish;
1667         }
1668
1669         merged = u;
1670         if ((r = merge_by_names(&merged, symlink_names, id)) < 0)
1671                 goto finish;
1672
1673         if (merged != u) {
1674                 u->meta.load_state = UNIT_MERGED;
1675                 r = 0;
1676                 goto finish;
1677         }
1678
1679         /* Now, parse the file contents */
1680         if ((r = config_parse(filename, f, sections, items, false, u)) < 0)
1681                 goto finish;
1682
1683         free(u->meta.fragment_path);
1684         u->meta.fragment_path = filename;
1685         filename = NULL;
1686
1687         u->meta.load_state = UNIT_LOADED;
1688         r = 0;
1689
1690 finish:
1691         set_free_free(symlink_names);
1692         free(filename);
1693
1694         if (f)
1695                 fclose(f);
1696
1697         return r;
1698 }
1699
1700 int unit_load_fragment(Unit *u) {
1701         int r;
1702
1703         assert(u);
1704
1705         if (u->meta.fragment_path) {
1706
1707                 if ((r = load_from_path(u, u->meta.fragment_path)) < 0)
1708                         return r;
1709
1710         } else {
1711                 Iterator i;
1712                 const char *t;
1713
1714                 /* Try to find the unit under its id */
1715                 if ((r = load_from_path(u, u->meta.id)) < 0)
1716                         return r;
1717
1718                 /* Try to find an alias we can load this with */
1719                 if (u->meta.load_state == UNIT_STUB)
1720                         SET_FOREACH(t, u->meta.names, i) {
1721
1722                                 if (t == u->meta.id)
1723                                         continue;
1724
1725                                 if ((r = load_from_path(u, t)) < 0)
1726                                         return r;
1727
1728                                 if (u->meta.load_state != UNIT_STUB)
1729                                         break;
1730                         }
1731
1732                 /* Now, follow the same logic, but look for a template */
1733                 if (u->meta.load_state == UNIT_STUB && u->meta.instance) {
1734                         char *k;
1735
1736                         if (!(k = unit_name_template(u->meta.id)))
1737                                 return -ENOMEM;
1738
1739                         r = load_from_path(u, k);
1740                         free(k);
1741
1742                         if (r < 0)
1743                                 return r;
1744
1745                         if (u->meta.load_state == UNIT_STUB)
1746                                 SET_FOREACH(t, u->meta.names, i) {
1747
1748                                         if (t == u->meta.id)
1749                                                 continue;
1750
1751                                         if (!(k = unit_name_template(t)))
1752                                                 return -ENOMEM;
1753
1754                                         r = load_from_path(u, k);
1755                                         free(k);
1756
1757                                         if (r < 0)
1758                                                 return r;
1759
1760                                         if (u->meta.load_state != UNIT_STUB)
1761                                                 break;
1762                                 }
1763                 }
1764         }
1765
1766         return 0;
1767 }
1768
1769 void unit_dump_config_items(FILE *f) {
1770         /* OK, this wins a prize for extreme ugliness. */
1771
1772         load_from_path(NULL, (const void*) f);
1773 }