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