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