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