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