chiark / gitweb /
update TODO
[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 #include <sys/time.h>
34 #include <sys/resource.h>
35
36 #include "unit.h"
37 #include "strv.h"
38 #include "conf-parser.h"
39 #include "load-fragment.h"
40 #include "log.h"
41 #include "ioprio.h"
42 #include "securebits.h"
43 #include "missing.h"
44 #include "unit-name.h"
45 #include "bus-errors.h"
46
47 #ifndef HAVE_SYSV_COMPAT
48 int config_parse_warn_compat(
49                 const char *filename,
50                 unsigned line,
51                 const char *section,
52                 const char *lvalue,
53                 int ltype,
54                 const char *rvalue,
55                 void *data,
56                 void *userdata) {
57
58         log_debug("[%s:%u] Support for option %s= has been disabled at compile time and is ignored", filename, line, lvalue);
59         return 0;
60 }
61 #endif
62
63 int config_parse_unit_deps(
64                 const char *filename,
65                 unsigned line,
66                 const char *section,
67                 const char *lvalue,
68                 int ltype,
69                 const char *rvalue,
70                 void *data,
71                 void *userdata) {
72
73         UnitDependency d = ltype;
74         Unit *u = userdata;
75         char *w;
76         size_t l;
77         char *state;
78
79         assert(filename);
80         assert(lvalue);
81         assert(rvalue);
82
83         FOREACH_WORD_QUOTED(w, l, rvalue, state) {
84                 char *t, *k;
85                 int r;
86
87                 if (!(t = strndup(w, l)))
88                         return -ENOMEM;
89
90                 k = unit_name_printf(u, t);
91                 free(t);
92
93                 if (!k)
94                         return -ENOMEM;
95
96                 r = unit_add_dependency_by_name(u, d, k, NULL, true);
97
98                 if (r < 0) {
99                         log_error("Failed to add dependency on %s, ignoring: %s", k, strerror(-r));
100                         free(k);
101                         return 0;
102                 }
103
104                 free(k);
105         }
106
107         return 0;
108 }
109
110 int config_parse_unit_names(
111                 const char *filename,
112                 unsigned line,
113                 const char *section,
114                 const char *lvalue,
115                 int ltype,
116                 const char *rvalue,
117                 void *data,
118                 void *userdata) {
119
120         Unit *u = userdata;
121         char *w;
122         size_t l;
123         char *state;
124
125         assert(filename);
126         assert(lvalue);
127         assert(rvalue);
128         assert(data);
129
130         FOREACH_WORD_QUOTED(w, l, rvalue, state) {
131                 char *t, *k;
132                 int r;
133
134                 if (!(t = strndup(w, l)))
135                         return -ENOMEM;
136
137                 k = unit_name_printf(u, t);
138                 free(t);
139
140                 if (!k)
141                         return -ENOMEM;
142
143                 r = unit_merge_by_name(u, k);
144
145                 if (r < 0) {
146                         log_error("Failed to add name %s, ignoring: %s", k, strerror(-r));
147                         free(k);
148                         return 0;
149                 }
150
151                 free(k);
152         }
153
154         return 0;
155 }
156
157 int config_parse_unit_string_printf(
158                 const char *filename,
159                 unsigned line,
160                 const char *section,
161                 const char *lvalue,
162                 int ltype,
163                 const char *rvalue,
164                 void *data,
165                 void *userdata) {
166
167         Unit *u = userdata;
168         char **s = data;
169         char *k;
170
171         assert(filename);
172         assert(lvalue);
173         assert(rvalue);
174         assert(s);
175         assert(u);
176
177         if (!(k = unit_full_printf(u, rvalue)))
178                 return -ENOMEM;
179
180         free(*s);
181         if (*k)
182                 *s = k;
183         else {
184                 free(k);
185                 *s = NULL;
186         }
187
188         return 0;
189 }
190
191 int config_parse_unit_strv_printf(
192                 const char *filename,
193                 unsigned line,
194                 const char *section,
195                 const char *lvalue,
196                 int ltype,
197                 const char *rvalue,
198                 void *data,
199                 void *userdata) {
200
201         Unit *u = userdata;
202         char *k;
203         int r;
204
205         assert(filename);
206         assert(lvalue);
207         assert(rvalue);
208         assert(u);
209
210         k = unit_full_printf(u, rvalue);
211         if (!k)
212                 return -ENOMEM;
213
214         r = config_parse_strv(filename, line, section, lvalue, ltype, k, data, userdata);
215         free(k);
216
217         return r;
218 }
219
220 int config_parse_unit_path_printf(
221                 const char *filename,
222                 unsigned line,
223                 const char *section,
224                 const char *lvalue,
225                 int ltype,
226                 const char *rvalue,
227                 void *data,
228                 void *userdata) {
229
230         Unit *u = userdata;
231         char **s = data;
232         char *k;
233
234         assert(filename);
235         assert(lvalue);
236         assert(rvalue);
237         assert(s);
238         assert(u);
239
240         if (!(k = unit_full_printf(u, rvalue)))
241                 return -ENOMEM;
242
243         if (!path_is_absolute(k)) {
244                 log_error("[%s:%u] Not an absolute path: %s", filename, line, k);
245                 free(k);
246                 return -EINVAL;
247         }
248
249         path_kill_slashes(k);
250
251         free(*s);
252         *s = k;
253
254         return 0;
255 }
256
257 int config_parse_socket_listen(
258                 const char *filename,
259                 unsigned line,
260                 const char *section,
261                 const char *lvalue,
262                 int ltype,
263                 const char *rvalue,
264                 void *data,
265                 void *userdata) {
266
267         SocketPort *p, *tail;
268         Socket *s;
269
270         assert(filename);
271         assert(lvalue);
272         assert(rvalue);
273         assert(data);
274
275         s = (Socket*) data;
276
277         if (!(p = new0(SocketPort, 1)))
278                 return -ENOMEM;
279
280         if (streq(lvalue, "ListenFIFO")) {
281                 p->type = SOCKET_FIFO;
282
283                 if (!(p->path = unit_full_printf(UNIT(s), rvalue))) {
284                         free(p);
285                         return -ENOMEM;
286                 }
287
288                 path_kill_slashes(p->path);
289
290         } else if (streq(lvalue, "ListenSpecial")) {
291                 p->type = SOCKET_SPECIAL;
292
293                 if (!(p->path = unit_full_printf(UNIT(s), rvalue))) {
294                         free(p);
295                         return -ENOMEM;
296                 }
297
298                 path_kill_slashes(p->path);
299
300         } else if (streq(lvalue, "ListenMessageQueue")) {
301
302                 p->type = SOCKET_MQUEUE;
303
304                 if (!(p->path = unit_full_printf(UNIT(s), rvalue))) {
305                         free(p);
306                         return -ENOMEM;
307                 }
308
309                 path_kill_slashes(p->path);
310
311         } else if (streq(lvalue, "ListenNetlink")) {
312                 char  *k;
313                 int r;
314
315                 p->type = SOCKET_SOCKET;
316                 k = unit_full_printf(UNIT(s), rvalue);
317                 r = socket_address_parse_netlink(&p->address, k);
318                 free(k);
319
320                 if (r < 0) {
321                         log_error("[%s:%u] Failed to parse address value, ignoring: %s", filename, line, rvalue);
322                         free(p);
323                         return 0;
324                 }
325
326         } else {
327                 char *k;
328                 int r;
329
330                 p->type = SOCKET_SOCKET;
331                 k = unit_full_printf(UNIT(s), rvalue);
332                 r = socket_address_parse(&p->address, k);
333                 free(k);
334
335                 if (r < 0) {
336                         log_error("[%s:%u] Failed to parse address value, ignoring: %s", filename, line, rvalue);
337                         free(p);
338                         return 0;
339                 }
340
341                 if (streq(lvalue, "ListenStream"))
342                         p->address.type = SOCK_STREAM;
343                 else if (streq(lvalue, "ListenDatagram"))
344                         p->address.type = SOCK_DGRAM;
345                 else {
346                         assert(streq(lvalue, "ListenSequentialPacket"));
347                         p->address.type = SOCK_SEQPACKET;
348                 }
349
350                 if (socket_address_family(&p->address) != AF_LOCAL && p->address.type == SOCK_SEQPACKET) {
351                         log_error("[%s:%u] Address family not supported, ignoring: %s", filename, line, rvalue);
352                         free(p);
353                         return 0;
354                 }
355         }
356
357         p->fd = -1;
358
359         if (s->ports) {
360                 LIST_FIND_TAIL(SocketPort, port, s->ports, tail);
361                 LIST_INSERT_AFTER(SocketPort, port, s->ports, tail, p);
362         } else
363                 LIST_PREPEND(SocketPort, port, s->ports, p);
364
365         return 0;
366 }
367
368 int config_parse_socket_bind(
369                 const char *filename,
370                 unsigned line,
371                 const char *section,
372                 const char *lvalue,
373                 int ltype,
374                 const char *rvalue,
375                 void *data,
376                 void *userdata) {
377
378         Socket *s;
379         SocketAddressBindIPv6Only b;
380
381         assert(filename);
382         assert(lvalue);
383         assert(rvalue);
384         assert(data);
385
386         s = (Socket*) data;
387
388         if ((b = socket_address_bind_ipv6_only_from_string(rvalue)) < 0) {
389                 int r;
390
391                 if ((r = parse_boolean(rvalue)) < 0) {
392                         log_error("[%s:%u] Failed to parse bind IPv6 only value, ignoring: %s", filename, line, rvalue);
393                         return 0;
394                 }
395
396                 s->bind_ipv6_only = r ? SOCKET_ADDRESS_IPV6_ONLY : SOCKET_ADDRESS_BOTH;
397         } else
398                 s->bind_ipv6_only = b;
399
400         return 0;
401 }
402
403 int config_parse_exec_nice(
404                 const char *filename,
405                 unsigned line,
406                 const char *section,
407                 const char *lvalue,
408                 int ltype,
409                 const char *rvalue,
410                 void *data,
411                 void *userdata) {
412
413         ExecContext *c = data;
414         int priority;
415
416         assert(filename);
417         assert(lvalue);
418         assert(rvalue);
419         assert(data);
420
421         if (safe_atoi(rvalue, &priority) < 0) {
422                 log_error("[%s:%u] Failed to parse nice priority, ignoring: %s. ", filename, line, rvalue);
423                 return 0;
424         }
425
426         if (priority < PRIO_MIN || priority >= PRIO_MAX) {
427                 log_error("[%s:%u] Nice priority out of range, ignoring: %s", filename, line, rvalue);
428                 return 0;
429         }
430
431         c->nice = priority;
432         c->nice_set = true;
433
434         return 0;
435 }
436
437 int config_parse_exec_oom_score_adjust(
438                 const char *filename,
439                 unsigned line,
440                 const char *section,
441                 const char *lvalue,
442                 int ltype,
443                 const char *rvalue,
444                 void *data,
445                 void *userdata) {
446
447         ExecContext *c = data;
448         int oa;
449
450         assert(filename);
451         assert(lvalue);
452         assert(rvalue);
453         assert(data);
454
455         if (safe_atoi(rvalue, &oa) < 0) {
456                 log_error("[%s:%u] Failed to parse the OOM score adjust value, ignoring: %s", filename, line, rvalue);
457                 return 0;
458         }
459
460         if (oa < OOM_SCORE_ADJ_MIN || oa > OOM_SCORE_ADJ_MAX) {
461                 log_error("[%s:%u] OOM score adjust value out of range, ignoring: %s", filename, line, rvalue);
462                 return 0;
463         }
464
465         c->oom_score_adjust = oa;
466         c->oom_score_adjust_set = true;
467
468         return 0;
469 }
470
471 int config_parse_exec(
472                 const char *filename,
473                 unsigned line,
474                 const char *section,
475                 const char *lvalue,
476                 int ltype,
477                 const char *rvalue,
478                 void *data,
479                 void *userdata) {
480
481         ExecCommand **e = data, *nce;
482         char *path, **n;
483         unsigned k;
484
485         assert(filename);
486         assert(lvalue);
487         assert(rvalue);
488         assert(e);
489
490         /* We accept an absolute path as first argument, or
491          * alternatively an absolute prefixed with @ to allow
492          * overriding of argv[0]. */
493
494         e += ltype;
495
496         for (;;) {
497                 char *w;
498                 size_t l;
499                 char *state;
500                 bool honour_argv0 = false, ignore = false;
501
502                 path = NULL;
503                 nce = NULL;
504                 n = NULL;
505
506                 rvalue += strspn(rvalue, WHITESPACE);
507
508                 if (rvalue[0] == 0)
509                         break;
510
511                 if (rvalue[0] == '-') {
512                         ignore = true;
513                         rvalue ++;
514                 }
515
516                 if (rvalue[0] == '@') {
517                         honour_argv0 = true;
518                         rvalue ++;
519                 }
520
521                 if (*rvalue != '/') {
522                         log_error("[%s:%u] Invalid executable path in command line, ignoring: %s", filename, line, rvalue);
523                         return 0;
524                 }
525
526                 k = 0;
527                 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
528                         if (strncmp(w, ";", MAX(l, 1U)) == 0)
529                                 break;
530
531                         k++;
532                 }
533
534                 if (!(n = new(char*, k + !honour_argv0)))
535                         return -ENOMEM;
536
537                 k = 0;
538                 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
539                         if (strncmp(w, ";", MAX(l, 1U)) == 0)
540                                 break;
541
542                         if (honour_argv0 && w == rvalue) {
543                                 assert(!path);
544                                 if (!(path = cunescape_length(w, l)))
545                                         goto fail;
546                         } else {
547                                 if (!(n[k++] = cunescape_length(w, l)))
548                                         goto fail;
549                         }
550                 }
551
552                 n[k] = NULL;
553
554                 if (!n[0]) {
555                         log_error("[%s:%u] Invalid command line, ignoring: %s", filename, line, rvalue);
556                         strv_free(n);
557                         return 0;
558                 }
559
560                 if (!path)
561                         if (!(path = strdup(n[0])))
562                                 goto fail;
563
564                 assert(path_is_absolute(path));
565
566                 if (!(nce = new0(ExecCommand, 1)))
567                         goto fail;
568
569                 nce->argv = n;
570                 nce->path = path;
571                 nce->ignore = ignore;
572
573                 path_kill_slashes(nce->path);
574
575                 exec_command_append_list(e, nce);
576
577                 rvalue = state;
578         }
579
580         return 0;
581
582 fail:
583         n[k] = NULL;
584         strv_free(n);
585         free(path);
586         free(nce);
587
588         return -ENOMEM;
589 }
590
591 DEFINE_CONFIG_PARSE_ENUM(config_parse_service_type, service_type, ServiceType, "Failed to parse service type");
592 DEFINE_CONFIG_PARSE_ENUM(config_parse_service_restart, service_restart, ServiceRestart, "Failed to parse service restart specifier");
593
594 int config_parse_socket_bindtodevice(
595                 const char *filename,
596                 unsigned line,
597                 const char *section,
598                 const char *lvalue,
599                 int ltype,
600                 const char *rvalue,
601                 void *data,
602                 void *userdata) {
603
604         Socket *s = data;
605         char *n;
606
607         assert(filename);
608         assert(lvalue);
609         assert(rvalue);
610         assert(data);
611
612         if (rvalue[0] && !streq(rvalue, "*")) {
613                 if (!(n = strdup(rvalue)))
614                         return -ENOMEM;
615         } else
616                 n = NULL;
617
618         free(s->bind_to_device);
619         s->bind_to_device = n;
620
621         return 0;
622 }
623
624 DEFINE_CONFIG_PARSE_ENUM(config_parse_output, exec_output, ExecOutput, "Failed to parse output specifier");
625 DEFINE_CONFIG_PARSE_ENUM(config_parse_input, exec_input, ExecInput, "Failed to parse input specifier");
626
627 int config_parse_facility(
628                 const char *filename,
629                 unsigned line,
630                 const char *section,
631                 const char *lvalue,
632                 int ltype,
633                 const char *rvalue,
634                 void *data,
635                 void *userdata) {
636
637
638         int *o = data, x;
639
640         assert(filename);
641         assert(lvalue);
642         assert(rvalue);
643         assert(data);
644
645         if ((x = log_facility_unshifted_from_string(rvalue)) < 0) {
646                 log_error("[%s:%u] Failed to parse log facility, ignoring: %s", filename, line, rvalue);
647                 return 0;
648         }
649
650         *o = (x << 3) | LOG_PRI(*o);
651
652         return 0;
653 }
654
655 int config_parse_level(
656                 const char *filename,
657                 unsigned line,
658                 const char *section,
659                 const char *lvalue,
660                 int ltype,
661                 const char *rvalue,
662                 void *data,
663                 void *userdata) {
664
665
666         int *o = data, x;
667
668         assert(filename);
669         assert(lvalue);
670         assert(rvalue);
671         assert(data);
672
673         if ((x = log_level_from_string(rvalue)) < 0) {
674                 log_error("[%s:%u] Failed to parse log level, ignoring: %s", filename, line, rvalue);
675                 return 0;
676         }
677
678         *o = (*o & LOG_FACMASK) | x;
679         return 0;
680 }
681
682 int config_parse_exec_io_class(
683                 const char *filename,
684                 unsigned line,
685                 const char *section,
686                 const char *lvalue,
687                 int ltype,
688                 const char *rvalue,
689                 void *data,
690                 void *userdata) {
691
692         ExecContext *c = data;
693         int x;
694
695         assert(filename);
696         assert(lvalue);
697         assert(rvalue);
698         assert(data);
699
700         if ((x = ioprio_class_from_string(rvalue)) < 0) {
701                 log_error("[%s:%u] Failed to parse IO scheduling class, ignoring: %s", filename, line, rvalue);
702                 return 0;
703         }
704
705         c->ioprio = IOPRIO_PRIO_VALUE(x, IOPRIO_PRIO_DATA(c->ioprio));
706         c->ioprio_set = true;
707
708         return 0;
709 }
710
711 int config_parse_exec_io_priority(
712                 const char *filename,
713                 unsigned line,
714                 const char *section,
715                 const char *lvalue,
716                 int ltype,
717                 const char *rvalue,
718                 void *data,
719                 void *userdata) {
720
721         ExecContext *c = data;
722         int i;
723
724         assert(filename);
725         assert(lvalue);
726         assert(rvalue);
727         assert(data);
728
729         if (safe_atoi(rvalue, &i) < 0 || i < 0 || i >= IOPRIO_BE_NR) {
730                 log_error("[%s:%u] Failed to parse io priority, ignoring: %s", filename, line, rvalue);
731                 return 0;
732         }
733
734         c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_PRIO_CLASS(c->ioprio), i);
735         c->ioprio_set = true;
736
737         return 0;
738 }
739
740 int config_parse_exec_cpu_sched_policy(
741                 const char *filename,
742                 unsigned line,
743                 const char *section,
744                 const char *lvalue,
745                 int ltype,
746                 const char *rvalue,
747                 void *data,
748                 void *userdata) {
749
750
751         ExecContext *c = data;
752         int x;
753
754         assert(filename);
755         assert(lvalue);
756         assert(rvalue);
757         assert(data);
758
759         if ((x = sched_policy_from_string(rvalue)) < 0) {
760                 log_error("[%s:%u] Failed to parse CPU scheduling policy, ignoring: %s", filename, line, rvalue);
761                 return 0;
762         }
763
764         c->cpu_sched_policy = x;
765         c->cpu_sched_set = true;
766
767         return 0;
768 }
769
770 int config_parse_exec_cpu_sched_prio(
771                 const char *filename,
772                 unsigned line,
773                 const char *section,
774                 const char *lvalue,
775                 int ltype,
776                 const char *rvalue,
777                 void *data,
778                 void *userdata) {
779
780         ExecContext *c = data;
781         int i;
782
783         assert(filename);
784         assert(lvalue);
785         assert(rvalue);
786         assert(data);
787
788         /* On Linux RR/FIFO have the same range */
789         if (safe_atoi(rvalue, &i) < 0 || i < sched_get_priority_min(SCHED_RR) || i > sched_get_priority_max(SCHED_RR)) {
790                 log_error("[%s:%u] Failed to parse CPU scheduling priority, ignoring: %s", filename, line, rvalue);
791                 return 0;
792         }
793
794         c->cpu_sched_priority = i;
795         c->cpu_sched_set = true;
796
797         return 0;
798 }
799
800 int config_parse_exec_cpu_affinity(
801                 const char *filename,
802                 unsigned line,
803                 const char *section,
804                 const char *lvalue,
805                 int ltype,
806                 const char *rvalue,
807                 void *data,
808                 void *userdata) {
809
810         ExecContext *c = data;
811         char *w;
812         size_t l;
813         char *state;
814
815         assert(filename);
816         assert(lvalue);
817         assert(rvalue);
818         assert(data);
819
820         FOREACH_WORD_QUOTED(w, l, rvalue, state) {
821                 char *t;
822                 int r;
823                 unsigned cpu;
824
825                 if (!(t = strndup(w, l)))
826                         return -ENOMEM;
827
828                 r = safe_atou(t, &cpu);
829                 free(t);
830
831                 if (!(c->cpuset))
832                         if (!(c->cpuset = cpu_set_malloc(&c->cpuset_ncpus)))
833                                 return -ENOMEM;
834
835                 if (r < 0 || cpu >= c->cpuset_ncpus) {
836                         log_error("[%s:%u] Failed to parse CPU affinity, ignoring: %s", filename, line, rvalue);
837                         return 0;
838                 }
839
840                 CPU_SET_S(cpu, CPU_ALLOC_SIZE(c->cpuset_ncpus), c->cpuset);
841         }
842
843         return 0;
844 }
845
846 int config_parse_exec_capabilities(
847                 const char *filename,
848                 unsigned line,
849                 const char *section,
850                 const char *lvalue,
851                 int ltype,
852                 const char *rvalue,
853                 void *data,
854                 void *userdata) {
855
856         ExecContext *c = data;
857         cap_t cap;
858
859         assert(filename);
860         assert(lvalue);
861         assert(rvalue);
862         assert(data);
863
864         if (!(cap = cap_from_text(rvalue))) {
865                 if (errno == ENOMEM)
866                         return -ENOMEM;
867
868                 log_error("[%s:%u] Failed to parse capabilities, ignoring: %s", filename, line, rvalue);
869                 return 0;
870         }
871
872         if (c->capabilities)
873                 cap_free(c->capabilities);
874         c->capabilities = cap;
875
876         return 0;
877 }
878
879 int config_parse_exec_secure_bits(
880                 const char *filename,
881                 unsigned line,
882                 const char *section,
883                 const char *lvalue,
884                 int ltype,
885                 const char *rvalue,
886                 void *data,
887                 void *userdata) {
888
889         ExecContext *c = data;
890         char *w;
891         size_t l;
892         char *state;
893
894         assert(filename);
895         assert(lvalue);
896         assert(rvalue);
897         assert(data);
898
899         FOREACH_WORD_QUOTED(w, l, rvalue, state) {
900                 if (first_word(w, "keep-caps"))
901                         c->secure_bits |= SECURE_KEEP_CAPS;
902                 else if (first_word(w, "keep-caps-locked"))
903                         c->secure_bits |= SECURE_KEEP_CAPS_LOCKED;
904                 else if (first_word(w, "no-setuid-fixup"))
905                         c->secure_bits |= SECURE_NO_SETUID_FIXUP;
906                 else if (first_word(w, "no-setuid-fixup-locked"))
907                         c->secure_bits |= SECURE_NO_SETUID_FIXUP_LOCKED;
908                 else if (first_word(w, "noroot"))
909                         c->secure_bits |= SECURE_NOROOT;
910                 else if (first_word(w, "noroot-locked"))
911                         c->secure_bits |= SECURE_NOROOT_LOCKED;
912                 else {
913                         log_error("[%s:%u] Failed to parse secure bits, ignoring: %s", filename, line, rvalue);
914                         return 0;
915                 }
916         }
917
918         return 0;
919 }
920
921 int config_parse_exec_bounding_set(
922                 const char *filename,
923                 unsigned line,
924                 const char *section,
925                 const char *lvalue,
926                 int ltype,
927                 const char *rvalue,
928                 void *data,
929                 void *userdata) {
930
931         ExecContext *c = data;
932         char *w;
933         size_t l;
934         char *state;
935         bool invert = false;
936         uint64_t sum = 0;
937
938         assert(filename);
939         assert(lvalue);
940         assert(rvalue);
941         assert(data);
942
943         if (rvalue[0] == '~') {
944                 invert = true;
945                 rvalue++;
946         }
947
948         /* Note that we store this inverted internally, since the
949          * kernel wants it like this. But we actually expose it
950          * non-inverted everywhere to have a fully normalized
951          * interface. */
952
953         FOREACH_WORD_QUOTED(w, l, rvalue, state) {
954                 char *t;
955                 int r;
956                 cap_value_t cap;
957
958                 if (!(t = strndup(w, l)))
959                         return -ENOMEM;
960
961                 r = cap_from_name(t, &cap);
962                 free(t);
963
964                 if (r < 0) {
965                         log_error("[%s:%u] Failed to parse capability bounding set, ignoring: %s", filename, line, rvalue);
966                         return 0;
967                 }
968
969                 sum |= ((uint64_t) 1ULL) << (uint64_t) cap;
970         }
971
972         if (invert)
973                 c->capability_bounding_set_drop |= sum;
974         else
975                 c->capability_bounding_set_drop |= ~sum;
976
977         return 0;
978 }
979
980 int config_parse_exec_timer_slack_nsec(
981                 const char *filename,
982                 unsigned line,
983                 const char *section,
984                 const char *lvalue,
985                 int ltype,
986                 const char *rvalue,
987                 void *data,
988                 void *userdata) {
989
990         ExecContext *c = data;
991         unsigned long u;
992
993         assert(filename);
994         assert(lvalue);
995         assert(rvalue);
996         assert(data);
997
998         if (safe_atolu(rvalue, &u) < 0) {
999                 log_error("[%s:%u] Failed to parse time slack value, ignoring: %s", filename, line, rvalue);
1000                 return 0;
1001         }
1002
1003         c->timer_slack_nsec = u;
1004
1005         return 0;
1006 }
1007
1008 int config_parse_limit(
1009                 const char *filename,
1010                 unsigned line,
1011                 const char *section,
1012                 const char *lvalue,
1013                 int ltype,
1014                 const char *rvalue,
1015                 void *data,
1016                 void *userdata) {
1017
1018         struct rlimit **rl = data;
1019         unsigned long long u;
1020
1021         assert(filename);
1022         assert(lvalue);
1023         assert(rvalue);
1024         assert(data);
1025
1026         rl += ltype;
1027
1028         if (streq(rvalue, "infinity"))
1029                 u = (unsigned long long) RLIM_INFINITY;
1030         else if (safe_atollu(rvalue, &u) < 0) {
1031                 log_error("[%s:%u] Failed to parse resource value, ignoring: %s", filename, line, rvalue);
1032                 return 0;
1033         }
1034
1035         if (!*rl)
1036                 if (!(*rl = new(struct rlimit, 1)))
1037                         return -ENOMEM;
1038
1039         (*rl)->rlim_cur = (*rl)->rlim_max = (rlim_t) u;
1040         return 0;
1041 }
1042
1043 int config_parse_unit_cgroup(
1044                 const char *filename,
1045                 unsigned line,
1046                 const char *section,
1047                 const char *lvalue,
1048                 int ltype,
1049                 const char *rvalue,
1050                 void *data,
1051                 void *userdata) {
1052
1053         Unit *u = userdata;
1054         char *w;
1055         size_t l;
1056         char *state;
1057
1058         FOREACH_WORD_QUOTED(w, l, rvalue, state) {
1059                 char *t, *k;
1060                 int r;
1061
1062                 t = strndup(w, l);
1063                 if (!t)
1064                         return -ENOMEM;
1065
1066                 k = unit_full_printf(u, t);
1067                 free(t);
1068
1069                 if (!k)
1070                         return -ENOMEM;
1071
1072                 t = cunescape(k);
1073                 free(k);
1074
1075                 if (!t)
1076                         return -ENOMEM;
1077
1078                 r = unit_add_cgroup_from_text(u, t);
1079                 free(t);
1080
1081                 if (r < 0) {
1082                         log_error("[%s:%u] Failed to parse cgroup value, ignoring: %s", filename, line, rvalue);
1083                         return 0;
1084                 }
1085         }
1086
1087         return 0;
1088 }
1089
1090 #ifdef HAVE_SYSV_COMPAT
1091 int config_parse_sysv_priority(
1092                 const char *filename,
1093                 unsigned line,
1094                 const char *section,
1095                 const char *lvalue,
1096                 int ltype,
1097                 const char *rvalue,
1098                 void *data,
1099                 void *userdata) {
1100
1101         int *priority = data;
1102         int i;
1103
1104         assert(filename);
1105         assert(lvalue);
1106         assert(rvalue);
1107         assert(data);
1108
1109         if (safe_atoi(rvalue, &i) < 0 || i < 0) {
1110                 log_error("[%s:%u] Failed to parse SysV start priority, ignoring: %s", filename, line, rvalue);
1111                 return 0;
1112         }
1113
1114         *priority = (int) i;
1115         return 0;
1116 }
1117 #endif
1118
1119 int config_parse_fsck_passno(
1120                 const char *filename,
1121                 unsigned line,
1122                 const char *section,
1123                 const char *lvalue,
1124                 int ltype,
1125                 const char *rvalue,
1126                 void *data,
1127                 void *userdata) {
1128
1129         int *passno = data;
1130         int i;
1131
1132         assert(filename);
1133         assert(lvalue);
1134         assert(rvalue);
1135         assert(data);
1136
1137         if (safe_atoi(rvalue, &i) || i < 0) {
1138                 log_error("[%s:%u] Failed to parse fsck pass number, ignoring: %s", filename, line, rvalue);
1139                 return 0;
1140         }
1141
1142         *passno = (int) i;
1143         return 0;
1144 }
1145
1146 DEFINE_CONFIG_PARSE_ENUM(config_parse_kill_mode, kill_mode, KillMode, "Failed to parse kill mode");
1147
1148 int config_parse_kill_signal(
1149                 const char *filename,
1150                 unsigned line,
1151                 const char *section,
1152                 const char *lvalue,
1153                 int ltype,
1154                 const char *rvalue,
1155                 void *data,
1156                 void *userdata) {
1157
1158         int *sig = data;
1159         int r;
1160
1161         assert(filename);
1162         assert(lvalue);
1163         assert(rvalue);
1164         assert(sig);
1165
1166         if ((r = signal_from_string_try_harder(rvalue)) <= 0) {
1167                 log_error("[%s:%u] Failed to parse kill signal, ignoring: %s", filename, line, rvalue);
1168                 return 0;
1169         }
1170
1171         *sig = r;
1172         return 0;
1173 }
1174
1175 int config_parse_exec_mount_flags(
1176                 const char *filename,
1177                 unsigned line,
1178                 const char *section,
1179                 const char *lvalue,
1180                 int ltype,
1181                 const char *rvalue,
1182                 void *data,
1183                 void *userdata) {
1184
1185         ExecContext *c = data;
1186         char *w;
1187         size_t l;
1188         char *state;
1189         unsigned long flags = 0;
1190
1191         assert(filename);
1192         assert(lvalue);
1193         assert(rvalue);
1194         assert(data);
1195
1196         FOREACH_WORD_QUOTED(w, l, rvalue, state) {
1197                 if (strncmp(w, "shared", MAX(l, 6U)) == 0)
1198                         flags |= MS_SHARED;
1199                 else if (strncmp(w, "slave", MAX(l, 5U)) == 0)
1200                         flags |= MS_SLAVE;
1201                 else if (strncmp(w, "private", MAX(l, 7U)) == 0)
1202                         flags |= MS_PRIVATE;
1203                 else {
1204                         log_error("[%s:%u] Failed to parse mount flags, ignoring: %s", filename, line, rvalue);
1205                         return 0;
1206                 }
1207         }
1208
1209         c->mount_flags = flags;
1210         return 0;
1211 }
1212
1213 int config_parse_timer(
1214                 const char *filename,
1215                 unsigned line,
1216                 const char *section,
1217                 const char *lvalue,
1218                 int ltype,
1219                 const char *rvalue,
1220                 void *data,
1221                 void *userdata) {
1222
1223         Timer *t = data;
1224         usec_t u;
1225         TimerValue *v;
1226         TimerBase b;
1227
1228         assert(filename);
1229         assert(lvalue);
1230         assert(rvalue);
1231         assert(data);
1232
1233         if ((b = timer_base_from_string(lvalue)) < 0) {
1234                 log_error("[%s:%u] Failed to parse timer base, ignoring: %s", filename, line, lvalue);
1235                 return 0;
1236         }
1237
1238         if (parse_usec(rvalue, &u) < 0) {
1239                 log_error("[%s:%u] Failed to parse timer value, ignoring: %s", filename, line, rvalue);
1240                 return 0;
1241         }
1242
1243         if (!(v = new0(TimerValue, 1)))
1244                 return -ENOMEM;
1245
1246         v->base = b;
1247         v->value = u;
1248
1249         LIST_PREPEND(TimerValue, value, t->values, v);
1250
1251         return 0;
1252 }
1253
1254 int config_parse_timer_unit(
1255                 const char *filename,
1256                 unsigned line,
1257                 const char *section,
1258                 const char *lvalue,
1259                 int ltype,
1260                 const char *rvalue,
1261                 void *data,
1262                 void *userdata) {
1263
1264         Timer *t = data;
1265         int r;
1266         DBusError error;
1267
1268         assert(filename);
1269         assert(lvalue);
1270         assert(rvalue);
1271         assert(data);
1272
1273         dbus_error_init(&error);
1274
1275         if (endswith(rvalue, ".timer")) {
1276                 log_error("[%s:%u] Unit cannot be of type timer, ignoring: %s", filename, line, rvalue);
1277                 return 0;
1278         }
1279
1280         if ((r = manager_load_unit(t->meta.manager, rvalue, NULL, NULL, &t->unit)) < 0) {
1281                 log_error("[%s:%u] Failed to load unit %s, ignoring: %s", filename, line, rvalue, bus_error(&error, r));
1282                 dbus_error_free(&error);
1283                 return 0;
1284         }
1285
1286         return 0;
1287 }
1288
1289 int config_parse_path_spec(
1290                 const char *filename,
1291                 unsigned line,
1292                 const char *section,
1293                 const char *lvalue,
1294                 int ltype,
1295                 const char *rvalue,
1296                 void *data,
1297                 void *userdata) {
1298
1299         Path *p = data;
1300         PathSpec *s;
1301         PathType b;
1302
1303         assert(filename);
1304         assert(lvalue);
1305         assert(rvalue);
1306         assert(data);
1307
1308         if ((b = path_type_from_string(lvalue)) < 0) {
1309                 log_error("[%s:%u] Failed to parse path type, ignoring: %s", filename, line, lvalue);
1310                 return 0;
1311         }
1312
1313         if (!path_is_absolute(rvalue)) {
1314                 log_error("[%s:%u] Path is not absolute, ignoring: %s", filename, line, rvalue);
1315                 return 0;
1316         }
1317
1318         if (!(s = new0(PathSpec, 1)))
1319                 return -ENOMEM;
1320
1321         if (!(s->path = strdup(rvalue))) {
1322                 free(s);
1323                 return -ENOMEM;
1324         }
1325
1326         path_kill_slashes(s->path);
1327
1328         s->type = b;
1329         s->inotify_fd = -1;
1330
1331         LIST_PREPEND(PathSpec, spec, p->specs, s);
1332
1333         return 0;
1334 }
1335
1336 int config_parse_path_unit(
1337                 const char *filename,
1338                 unsigned line,
1339                 const char *section,
1340                 const char *lvalue,
1341                 int ltype,
1342                 const char *rvalue,
1343                 void *data,
1344                 void *userdata) {
1345
1346         Path *t = data;
1347         int r;
1348         DBusError error;
1349
1350         assert(filename);
1351         assert(lvalue);
1352         assert(rvalue);
1353         assert(data);
1354
1355         dbus_error_init(&error);
1356
1357         if (endswith(rvalue, ".path")) {
1358                 log_error("[%s:%u] Unit cannot be of type path, ignoring: %s", filename, line, rvalue);
1359                 return 0;
1360         }
1361
1362         if ((r = manager_load_unit(t->meta.manager, rvalue, NULL, &error, &t->unit)) < 0) {
1363                 log_error("[%s:%u] Failed to load unit %s, ignoring: %s", filename, line, rvalue, bus_error(&error, r));
1364                 dbus_error_free(&error);
1365                 return 0;
1366         }
1367
1368         return 0;
1369 }
1370
1371 int config_parse_socket_service(
1372                 const char *filename,
1373                 unsigned line,
1374                 const char *section,
1375                 const char *lvalue,
1376                 int ltype,
1377                 const char *rvalue,
1378                 void *data,
1379                 void *userdata) {
1380
1381         Socket *s = data;
1382         int r;
1383         DBusError error;
1384
1385         assert(filename);
1386         assert(lvalue);
1387         assert(rvalue);
1388         assert(data);
1389
1390         dbus_error_init(&error);
1391
1392         if (!endswith(rvalue, ".service")) {
1393                 log_error("[%s:%u] Unit must be of type service, ignoring: %s", filename, line, rvalue);
1394                 return 0;
1395         }
1396
1397         if ((r = manager_load_unit(s->meta.manager, rvalue, NULL, &error, (Unit**) &s->service)) < 0) {
1398                 log_error("[%s:%u] Failed to load unit %s, ignoring: %s", filename, line, rvalue, bus_error(&error, r));
1399                 dbus_error_free(&error);
1400                 return 0;
1401         }
1402
1403         return 0;
1404 }
1405
1406 int config_parse_service_sockets(
1407                 const char *filename,
1408                 unsigned line,
1409                 const char *section,
1410                 const char *lvalue,
1411                 int ltype,
1412                 const char *rvalue,
1413                 void *data,
1414                 void *userdata) {
1415
1416         Service *s = data;
1417         int r;
1418         DBusError error;
1419         char *state, *w;
1420         size_t l;
1421
1422         assert(filename);
1423         assert(lvalue);
1424         assert(rvalue);
1425         assert(data);
1426
1427         dbus_error_init(&error);
1428
1429         FOREACH_WORD_QUOTED(w, l, rvalue, state) {
1430                 char *t;
1431                 Unit *sock;
1432
1433                 if (!(t = strndup(w, l)))
1434                         return -ENOMEM;
1435
1436                 if (!endswith(t, ".socket")) {
1437                         log_error("[%s:%u] Unit must be of type socket, ignoring: %s", filename, line, rvalue);
1438                         free(t);
1439                         continue;
1440                 }
1441
1442                 r = manager_load_unit(s->meta.manager, t, NULL, &error, &sock);
1443                 free(t);
1444
1445                 if (r < 0) {
1446                         log_error("[%s:%u] Failed to load unit %s, ignoring: %s", filename, line, rvalue, bus_error(&error, r));
1447                         dbus_error_free(&error);
1448                         continue;
1449                 }
1450
1451                 if ((r = set_ensure_allocated(&s->configured_sockets, trivial_hash_func, trivial_compare_func)) < 0)
1452                         return r;
1453
1454                 if ((r = set_put(s->configured_sockets, sock)) < 0)
1455                         return r;
1456         }
1457
1458         return 0;
1459 }
1460
1461 int config_parse_unit_env_file(
1462                 const char *filename,
1463                 unsigned line,
1464                 const char *section,
1465                 const char *lvalue,
1466                 int ltype,
1467                 const char *rvalue,
1468                 void *data,
1469                 void *userdata) {
1470
1471         char ***env = data, **k;
1472         Unit *u = userdata;
1473         char *s;
1474
1475         assert(filename);
1476         assert(lvalue);
1477         assert(rvalue);
1478         assert(data);
1479
1480         s = unit_full_printf(u, rvalue);
1481         if (!s)
1482                 return -ENOMEM;
1483
1484         if (!path_is_absolute(s[0] == '-' ? s + 1 : s)) {
1485                 log_error("[%s:%u] Path '%s' is not absolute, ignoring.", filename, line, s);
1486                 free(s);
1487                 return 0;
1488         }
1489
1490         k = strv_append(*env, s);
1491         free(s);
1492         if (!k)
1493                 return -ENOMEM;
1494
1495         strv_free(*env);
1496         *env = k;
1497
1498         return 0;
1499 }
1500
1501 int config_parse_ip_tos(
1502                 const char *filename,
1503                 unsigned line,
1504                 const char *section,
1505                 const char *lvalue,
1506                 int ltype,
1507                 const char *rvalue,
1508                 void *data,
1509                 void *userdata) {
1510
1511         int *ip_tos = data, x;
1512
1513         assert(filename);
1514         assert(lvalue);
1515         assert(rvalue);
1516         assert(data);
1517
1518         if ((x = ip_tos_from_string(rvalue)) < 0)
1519                 if (safe_atoi(rvalue, &x) < 0) {
1520                         log_error("[%s:%u] Failed to parse IP TOS value, ignoring: %s", filename, line, rvalue);
1521                         return 0;
1522                 }
1523
1524         *ip_tos = x;
1525         return 0;
1526 }
1527
1528 int config_parse_unit_condition_path(
1529                 const char *filename,
1530                 unsigned line,
1531                 const char *section,
1532                 const char *lvalue,
1533                 int ltype,
1534                 const char *rvalue,
1535                 void *data,
1536                 void *userdata) {
1537
1538         ConditionType cond = ltype;
1539         Unit *u = data;
1540         bool trigger, negate;
1541         Condition *c;
1542
1543         assert(filename);
1544         assert(lvalue);
1545         assert(rvalue);
1546         assert(data);
1547
1548         if ((trigger = rvalue[0] == '|'))
1549                 rvalue++;
1550
1551         if ((negate = rvalue[0] == '!'))
1552                 rvalue++;
1553
1554         if (!path_is_absolute(rvalue)) {
1555                 log_error("[%s:%u] Path in condition not absolute, ignoring: %s", filename, line, rvalue);
1556                 return 0;
1557         }
1558
1559         if (!(c = condition_new(cond, rvalue, trigger, negate)))
1560                 return -ENOMEM;
1561
1562         LIST_PREPEND(Condition, conditions, u->meta.conditions, c);
1563         return 0;
1564 }
1565
1566 int config_parse_unit_condition_string(
1567                 const char *filename,
1568                 unsigned line,
1569                 const char *section,
1570                 const char *lvalue,
1571                 int ltype,
1572                 const char *rvalue,
1573                 void *data,
1574                 void *userdata) {
1575
1576         ConditionType cond = ltype;
1577         Unit *u = data;
1578         bool trigger, negate;
1579         Condition *c;
1580
1581         assert(filename);
1582         assert(lvalue);
1583         assert(rvalue);
1584         assert(data);
1585
1586         if ((trigger = rvalue[0] == '|'))
1587                 rvalue++;
1588
1589         if ((negate = rvalue[0] == '!'))
1590                 rvalue++;
1591
1592         if (!(c = condition_new(cond, rvalue, trigger, negate)))
1593                 return -ENOMEM;
1594
1595         LIST_PREPEND(Condition, conditions, u->meta.conditions, c);
1596         return 0;
1597 }
1598
1599 int config_parse_unit_condition_null(
1600                 const char *filename,
1601                 unsigned line,
1602                 const char *section,
1603                 const char *lvalue,
1604                 int ltype,
1605                 const char *rvalue,
1606                 void *data,
1607                 void *userdata) {
1608
1609         Unit *u = data;
1610         Condition *c;
1611         bool trigger, negate;
1612         int b;
1613
1614         assert(filename);
1615         assert(lvalue);
1616         assert(rvalue);
1617         assert(data);
1618
1619         if ((trigger = rvalue[0] == '|'))
1620                 rvalue++;
1621
1622         if ((negate = rvalue[0] == '!'))
1623                 rvalue++;
1624
1625         if ((b = parse_boolean(rvalue)) < 0) {
1626                 log_error("[%s:%u] Failed to parse boolean value in condition, ignoring: %s", filename, line, rvalue);
1627                 return 0;
1628         }
1629
1630         if (!b)
1631                 negate = !negate;
1632
1633         if (!(c = condition_new(CONDITION_NULL, NULL, trigger, negate)))
1634                 return -ENOMEM;
1635
1636         LIST_PREPEND(Condition, conditions, u->meta.conditions, c);
1637         return 0;
1638 }
1639
1640 DEFINE_CONFIG_PARSE_ENUM(config_parse_notify_access, notify_access, NotifyAccess, "Failed to parse notify access specifier");
1641
1642 #define FOLLOW_MAX 8
1643
1644 static int open_follow(char **filename, FILE **_f, Set *names, char **_final) {
1645         unsigned c = 0;
1646         int fd, r;
1647         FILE *f;
1648         char *id = NULL;
1649
1650         assert(filename);
1651         assert(*filename);
1652         assert(_f);
1653         assert(names);
1654
1655         /* This will update the filename pointer if the loaded file is
1656          * reached by a symlink. The old string will be freed. */
1657
1658         for (;;) {
1659                 char *target, *name;
1660
1661                 if (c++ >= FOLLOW_MAX)
1662                         return -ELOOP;
1663
1664                 path_kill_slashes(*filename);
1665
1666                 /* Add the file name we are currently looking at to
1667                  * the names of this unit, but only if it is a valid
1668                  * unit name. */
1669                 name = file_name_from_path(*filename);
1670
1671                 if (unit_name_is_valid(name, true)) {
1672
1673                         id = set_get(names, name);
1674                         if (!id) {
1675                                 id = strdup(name);
1676                                 if (!id)
1677                                         return -ENOMEM;
1678
1679                                 r = set_put(names, id);
1680                                 if (r < 0) {
1681                                         free(id);
1682                                         return r;
1683                                 }
1684                         }
1685                 }
1686
1687                 /* Try to open the file name, but don't if its a symlink */
1688                 if ((fd = open(*filename, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW)) >= 0)
1689                         break;
1690
1691                 if (errno != ELOOP)
1692                         return -errno;
1693
1694                 /* Hmm, so this is a symlink. Let's read the name, and follow it manually */
1695                 if ((r = readlink_and_make_absolute(*filename, &target)) < 0)
1696                         return r;
1697
1698                 free(*filename);
1699                 *filename = target;
1700         }
1701
1702         if (!(f = fdopen(fd, "re"))) {
1703                 r = -errno;
1704                 close_nointr_nofail(fd);
1705                 return r;
1706         }
1707
1708         *_f = f;
1709         *_final = id;
1710         return 0;
1711 }
1712
1713 static int merge_by_names(Unit **u, Set *names, const char *id) {
1714         char *k;
1715         int r;
1716
1717         assert(u);
1718         assert(*u);
1719         assert(names);
1720
1721         /* Let's try to add in all symlink names we found */
1722         while ((k = set_steal_first(names))) {
1723
1724                 /* First try to merge in the other name into our
1725                  * unit */
1726                 if ((r = unit_merge_by_name(*u, k)) < 0) {
1727                         Unit *other;
1728
1729                         /* Hmm, we couldn't merge the other unit into
1730                          * ours? Then let's try it the other way
1731                          * round */
1732
1733                         other = manager_get_unit((*u)->meta.manager, k);
1734                         free(k);
1735
1736                         if (other)
1737                                 if ((r = unit_merge(other, *u)) >= 0) {
1738                                         *u = other;
1739                                         return merge_by_names(u, names, NULL);
1740                                 }
1741
1742                         return r;
1743                 }
1744
1745                 if (id == k)
1746                         unit_choose_id(*u, id);
1747
1748                 free(k);
1749         }
1750
1751         return 0;
1752 }
1753
1754 static int load_from_path(Unit *u, const char *path) {
1755         int r;
1756         Set *symlink_names;
1757         FILE *f = NULL;
1758         char *filename = NULL, *id = NULL;
1759         Unit *merged;
1760         struct stat st;
1761
1762         assert(u);
1763         assert(path);
1764
1765         symlink_names = set_new(string_hash_func, string_compare_func);
1766         if (!symlink_names)
1767                 return -ENOMEM;
1768
1769         if (path_is_absolute(path)) {
1770
1771                 if (!(filename = strdup(path))) {
1772                         r = -ENOMEM;
1773                         goto finish;
1774                 }
1775
1776                 if ((r = open_follow(&filename, &f, symlink_names, &id)) < 0) {
1777                         free(filename);
1778                         filename = NULL;
1779
1780                         if (r != -ENOENT)
1781                                 goto finish;
1782                 }
1783
1784         } else  {
1785                 char **p;
1786
1787                 STRV_FOREACH(p, u->meta.manager->lookup_paths.unit_path) {
1788
1789                         /* Instead of opening the path right away, we manually
1790                          * follow all symlinks and add their name to our unit
1791                          * name set while doing so */
1792                         if (!(filename = path_make_absolute(path, *p))) {
1793                                 r = -ENOMEM;
1794                                 goto finish;
1795                         }
1796
1797                         if (u->meta.manager->unit_path_cache &&
1798                             !set_get(u->meta.manager->unit_path_cache, filename))
1799                                 r = -ENOENT;
1800                         else
1801                                 r = open_follow(&filename, &f, symlink_names, &id);
1802
1803                         if (r < 0) {
1804                                 char *sn;
1805
1806                                 free(filename);
1807                                 filename = NULL;
1808
1809                                 if (r != -ENOENT)
1810                                         goto finish;
1811
1812                                 /* Empty the symlink names for the next run */
1813                                 while ((sn = set_steal_first(symlink_names)))
1814                                         free(sn);
1815
1816                                 continue;
1817                         }
1818
1819                         break;
1820                 }
1821         }
1822
1823         if (!filename) {
1824                 /* Hmm, no suitable file found? */
1825                 r = 0;
1826                 goto finish;
1827         }
1828
1829         merged = u;
1830         if ((r = merge_by_names(&merged, symlink_names, id)) < 0)
1831                 goto finish;
1832
1833         if (merged != u) {
1834                 u->meta.load_state = UNIT_MERGED;
1835                 r = 0;
1836                 goto finish;
1837         }
1838
1839         zero(st);
1840         if (fstat(fileno(f), &st) < 0) {
1841                 r = -errno;
1842                 goto finish;
1843         }
1844
1845         if (null_or_empty(&st))
1846                 u->meta.load_state = UNIT_MASKED;
1847         else {
1848                 /* Now, parse the file contents */
1849                 r = config_parse(filename, f, UNIT_VTABLE(u)->sections, config_item_perf_lookup, (void*) load_fragment_gperf_lookup, false, u);
1850                 if (r < 0)
1851                         goto finish;
1852
1853                 u->meta.load_state = UNIT_LOADED;
1854         }
1855
1856         free(u->meta.fragment_path);
1857         u->meta.fragment_path = filename;
1858         filename = NULL;
1859
1860         u->meta.fragment_mtime = timespec_load(&st.st_mtim);
1861
1862         r = 0;
1863
1864 finish:
1865         set_free_free(symlink_names);
1866         free(filename);
1867
1868         if (f)
1869                 fclose(f);
1870
1871         return r;
1872 }
1873
1874 int unit_load_fragment(Unit *u) {
1875         int r;
1876         Iterator i;
1877         const char *t;
1878
1879         assert(u);
1880         assert(u->meta.load_state == UNIT_STUB);
1881         assert(u->meta.id);
1882
1883         /* First, try to find the unit under its id. We always look
1884          * for unit files in the default directories, to make it easy
1885          * to override things by placing things in /etc/systemd/system */
1886         if ((r = load_from_path(u, u->meta.id)) < 0)
1887                 return r;
1888
1889         /* Try to find an alias we can load this with */
1890         if (u->meta.load_state == UNIT_STUB)
1891                 SET_FOREACH(t, u->meta.names, i) {
1892
1893                         if (t == u->meta.id)
1894                                 continue;
1895
1896                         if ((r = load_from_path(u, t)) < 0)
1897                                 return r;
1898
1899                         if (u->meta.load_state != UNIT_STUB)
1900                                 break;
1901                 }
1902
1903         /* And now, try looking for it under the suggested (originally linked) path */
1904         if (u->meta.load_state == UNIT_STUB && u->meta.fragment_path) {
1905
1906                 if ((r = load_from_path(u, u->meta.fragment_path)) < 0)
1907                         return r;
1908
1909                 if (u->meta.load_state == UNIT_STUB) {
1910                         /* Hmm, this didn't work? Then let's get rid
1911                          * of the fragment path stored for us, so that
1912                          * we don't point to an invalid location. */
1913                         free(u->meta.fragment_path);
1914                         u->meta.fragment_path = NULL;
1915                 }
1916         }
1917
1918         /* Look for a template */
1919         if (u->meta.load_state == UNIT_STUB && u->meta.instance) {
1920                 char *k;
1921
1922                 if (!(k = unit_name_template(u->meta.id)))
1923                         return -ENOMEM;
1924
1925                 r = load_from_path(u, k);
1926                 free(k);
1927
1928                 if (r < 0)
1929                         return r;
1930
1931                 if (u->meta.load_state == UNIT_STUB)
1932                         SET_FOREACH(t, u->meta.names, i) {
1933
1934                                 if (t == u->meta.id)
1935                                         continue;
1936
1937                                 if (!(k = unit_name_template(t)))
1938                                         return -ENOMEM;
1939
1940                                 r = load_from_path(u, k);
1941                                 free(k);
1942
1943                                 if (r < 0)
1944                                         return r;
1945
1946                                 if (u->meta.load_state != UNIT_STUB)
1947                                         break;
1948                         }
1949         }
1950
1951         return 0;
1952 }
1953
1954 void unit_dump_config_items(FILE *f) {
1955         static const struct {
1956                 const ConfigParserCallback callback;
1957                 const char *rvalue;
1958         } table[] = {
1959                 { config_parse_int,                   "INTEGER" },
1960                 { config_parse_unsigned,              "UNSIGNED" },
1961                 { config_parse_size,                  "SIZE" },
1962                 { config_parse_bool,                  "BOOLEAN" },
1963                 { config_parse_string,                "STRING" },
1964                 { config_parse_path,                  "PATH" },
1965                 { config_parse_unit_path_printf,      "PATH" },
1966                 { config_parse_strv,                  "STRING [...]" },
1967                 { config_parse_exec_nice,             "NICE" },
1968                 { config_parse_exec_oom_score_adjust, "OOMSCOREADJUST" },
1969                 { config_parse_exec_io_class,         "IOCLASS" },
1970                 { config_parse_exec_io_priority,      "IOPRIORITY" },
1971                 { config_parse_exec_cpu_sched_policy, "CPUSCHEDPOLICY" },
1972                 { config_parse_exec_cpu_sched_prio,   "CPUSCHEDPRIO" },
1973                 { config_parse_exec_cpu_affinity,     "CPUAFFINITY" },
1974                 { config_parse_mode,                  "MODE" },
1975                 { config_parse_unit_env_file,         "FILE" },
1976                 { config_parse_output,                "OUTPUT" },
1977                 { config_parse_input,                 "INPUT" },
1978                 { config_parse_facility,              "FACILITY" },
1979                 { config_parse_level,                 "LEVEL" },
1980                 { config_parse_exec_capabilities,     "CAPABILITIES" },
1981                 { config_parse_exec_secure_bits,      "SECUREBITS" },
1982                 { config_parse_exec_bounding_set,     "BOUNDINGSET" },
1983                 { config_parse_exec_timer_slack_nsec, "TIMERSLACK" },
1984                 { config_parse_limit,                 "LIMIT" },
1985                 { config_parse_unit_cgroup,           "CGROUP [...]" },
1986                 { config_parse_unit_deps,             "UNIT [...]" },
1987                 { config_parse_unit_names,            "UNIT [...]" },
1988                 { config_parse_exec,                  "PATH [ARGUMENT [...]]" },
1989                 { config_parse_service_type,          "SERVICETYPE" },
1990                 { config_parse_service_restart,       "SERVICERESTART" },
1991 #ifdef HAVE_SYSV_COMPAT
1992                 { config_parse_sysv_priority,         "SYSVPRIORITY" },
1993 #else
1994                 { config_parse_warn_compat,           "NOTSUPPORTED" },
1995 #endif
1996                 { config_parse_kill_mode,             "KILLMODE" },
1997                 { config_parse_kill_signal,           "SIGNAL" },
1998                 { config_parse_socket_listen,         "SOCKET [...]" },
1999                 { config_parse_socket_bind,           "SOCKETBIND" },
2000                 { config_parse_socket_bindtodevice,   "NETWORKINTERFACE" },
2001                 { config_parse_usec,                  "SECONDS" },
2002                 { config_parse_path_strv,             "PATH [...]" },
2003                 { config_parse_exec_mount_flags,      "MOUNTFLAG [...]" },
2004                 { config_parse_unit_string_printf,    "STRING" },
2005                 { config_parse_timer,                 "TIMER" },
2006                 { config_parse_timer_unit,            "NAME" },
2007                 { config_parse_path_spec,             "PATH" },
2008                 { config_parse_path_unit,             "UNIT" },
2009                 { config_parse_notify_access,         "ACCESS" },
2010                 { config_parse_ip_tos,                "TOS" },
2011                 { config_parse_unit_condition_path,   "CONDITION" },
2012                 { config_parse_unit_condition_string, "CONDITION" },
2013                 { config_parse_unit_condition_null,   "CONDITION" },
2014         };
2015
2016         const char *prev = NULL;
2017         const char *i;
2018
2019         assert(f);
2020
2021         NULSTR_FOREACH(i, load_fragment_gperf_nulstr) {
2022                 const char *rvalue = "OTHER", *lvalue;
2023                 unsigned j;
2024                 size_t prefix_len;
2025                 const char *dot;
2026                 const ConfigPerfItem *p;
2027
2028                 assert_se(p = load_fragment_gperf_lookup(i, strlen(i)));
2029
2030                 dot = strchr(i, '.');
2031                 lvalue = dot ? dot + 1 : i;
2032                 prefix_len = dot-i;
2033
2034                 if (dot)
2035                         if (!prev || strncmp(prev, i, prefix_len+1) != 0) {
2036                                 if (prev)
2037                                         fputc('\n', f);
2038
2039                                 fprintf(f, "[%.*s]\n", (int) prefix_len, i);
2040                         }
2041
2042                 for (j = 0; j < ELEMENTSOF(table); j++)
2043                         if (p->parse == table[j].callback) {
2044                                 rvalue = table[j].rvalue;
2045                                 break;
2046                         }
2047
2048                 fprintf(f, "%s=%s\n", lvalue, rvalue);
2049                 prev = i;
2050         }
2051 }