chiark / gitweb /
path-lookup: monitor /etc/systemd/user for user manager
[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 int config_parse_unit_cgroup_attr(
1643                 const char *filename,
1644                 unsigned line,
1645                 const char *section,
1646                 const char *lvalue,
1647                 int ltype,
1648                 const char *rvalue,
1649                 void *data,
1650                 void *userdata) {
1651
1652         Unit *u = data;
1653         char **l;
1654         int r;
1655
1656         assert(filename);
1657         assert(lvalue);
1658         assert(rvalue);
1659         assert(data);
1660
1661         l = strv_split_quoted(rvalue);
1662         if (!l)
1663                 return -ENOMEM;
1664
1665         if (strv_length(l) != 2) {
1666                 log_error("[%s:%u] Failed to parse cgroup attribute value, ignoring: %s", filename, line, rvalue);
1667                 strv_free(l);
1668                 return 0;
1669         }
1670
1671         r = unit_add_cgroup_attribute(u, NULL, l[0], l[1], NULL);
1672         strv_free(l);
1673
1674         if (r < 0) {
1675                 log_error("[%s:%u] Failed to add cgroup attribute value, ignoring: %s", filename, line, rvalue);
1676                 return 0;
1677         }
1678
1679         return 0;
1680 }
1681
1682 int config_parse_unit_cpu_shares(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata) {
1683         Unit *u = data;
1684         int r;
1685         unsigned long ul;
1686         char *t;
1687
1688         assert(filename);
1689         assert(lvalue);
1690         assert(rvalue);
1691         assert(data);
1692
1693         if (safe_atolu(rvalue, &ul) < 0 || ul < 1) {
1694                 log_error("[%s:%u] Failed to parse CPU shares value, ignoring: %s", filename, line, rvalue);
1695                 return 0;
1696         }
1697
1698         if (asprintf(&t, "%lu", ul) < 0)
1699                 return -ENOMEM;
1700
1701         r = unit_add_cgroup_attribute(u, "cpu", "cpu.shares", t, NULL);
1702         free(t);
1703
1704         if (r < 0) {
1705                 log_error("[%s:%u] Failed to add cgroup attribute value, ignoring: %s", filename, line, rvalue);
1706                 return 0;
1707         }
1708
1709         return 0;
1710 }
1711
1712 int config_parse_unit_memory_limit(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata) {
1713         Unit *u = data;
1714         int r;
1715         off_t sz;
1716         char *t;
1717
1718         assert(filename);
1719         assert(lvalue);
1720         assert(rvalue);
1721         assert(data);
1722
1723         if (parse_bytes(rvalue, &sz) < 0 || sz <= 0) {
1724                 log_error("[%s:%u] Failed to parse memory limit value, ignoring: %s", filename, line, rvalue);
1725                 return 0;
1726         }
1727
1728         if (asprintf(&t, "%llu", (unsigned long long) sz) < 0)
1729                 return -ENOMEM;
1730
1731         r = unit_add_cgroup_attribute(u,
1732                                       "memory",
1733                                       streq(lvalue, "MemorySoftLimit") ? "memory.soft_limit_in_bytes" : "memory.limit_in_bytes",
1734                                       t, NULL);
1735         free(t);
1736
1737         if (r < 0) {
1738                 log_error("[%s:%u] Failed to add cgroup attribute value, ignoring: %s", filename, line, rvalue);
1739                 return 0;
1740         }
1741
1742         return 0;
1743 }
1744
1745 static int device_map(const char *controller, const char *name, const char *value, char **ret) {
1746         char **l;
1747
1748         assert(controller);
1749         assert(name);
1750         assert(value);
1751         assert(ret);
1752
1753         l = strv_split_quoted(value);
1754         if (!l)
1755                 return -ENOMEM;
1756
1757         assert(strv_length(l) >= 1);
1758
1759         if (streq(l[0], "*")) {
1760
1761                 if (asprintf(ret, "a *:*%s%s",
1762                              isempty(l[1]) ? "" : " ", strempty(l[1])) < 0) {
1763                         strv_free(l);
1764                         return -ENOMEM;
1765                 }
1766
1767         } else {
1768                 struct stat st;
1769
1770                 if (stat(l[0], &st) < 0) {
1771                         log_warning("Couldn't stat device %s", l[0]);
1772                         strv_free(l);
1773                         return -errno;
1774                 }
1775
1776                 if (!S_ISCHR(st.st_mode) && !S_ISBLK(st.st_mode)) {
1777                         log_warning("%s is not a device.", l[0]);
1778                         strv_free(l);
1779                         return -ENODEV;
1780                 }
1781
1782                 if (asprintf(ret, "%c %u:%u%s%s",
1783                              S_ISCHR(st.st_mode) ? 'c' : 'b',
1784                              major(st.st_rdev), minor(st.st_rdev),
1785                              isempty(l[1]) ? "" : " ", strempty(l[1])) < 0) {
1786
1787                         strv_free(l);
1788                         return -ENOMEM;
1789                 }
1790         }
1791
1792         strv_free(l);
1793         return 0;
1794 }
1795
1796 int config_parse_unit_device_allow(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata) {
1797         Unit *u = data;
1798         char **l;
1799         int r;
1800         unsigned k;
1801
1802         assert(filename);
1803         assert(lvalue);
1804         assert(rvalue);
1805         assert(data);
1806
1807         l = strv_split_quoted(rvalue);
1808         if (!l)
1809                 return -ENOMEM;
1810
1811         k = strv_length(l);
1812         if (k < 1 || k > 2) {
1813                 log_error("[%s:%u] Failed to parse device value, ignoring: %s", filename, line, rvalue);
1814                 strv_free(l);
1815                 return 0;
1816         }
1817
1818         if (!streq(l[0], "*") && !path_startswith(l[0], "/dev")) {
1819                 log_error("[%s:%u] Device node path not absolute, ignoring: %s", filename, line, rvalue);
1820                 strv_free(l);
1821                 return 0;
1822         }
1823
1824         if (!isempty(l[1]) && !in_charset(l[1], "rwm")) {
1825                 log_error("[%s:%u] Device access string invalid, ignoring: %s", filename, line, rvalue);
1826                 strv_free(l);
1827                 return 0;
1828         }
1829         strv_free(l);
1830
1831         r = unit_add_cgroup_attribute(u, "devices",
1832                                       streq(lvalue, "DeviceAllow") ? "devices.allow" : "devices.deny",
1833                                       rvalue, device_map);
1834
1835         if (r < 0) {
1836                 log_error("[%s:%u] Failed to add cgroup attribute value, ignoring: %s", filename, line, rvalue);
1837                 return 0;
1838         }
1839
1840         return 0;
1841 }
1842
1843 static int blkio_map(const char *controller, const char *name, const char *value, char **ret) {
1844         struct stat st;
1845         char **l;
1846         dev_t d;
1847
1848         assert(controller);
1849         assert(name);
1850         assert(value);
1851         assert(ret);
1852
1853         l = strv_split_quoted(value);
1854         if (!l)
1855                 return -ENOMEM;
1856
1857         assert(strv_length(l) == 2);
1858
1859         if (stat(l[0], &st) < 0) {
1860                 log_warning("Couldn't stat device %s", l[0]);
1861                 strv_free(l);
1862                 return -errno;
1863         }
1864
1865         if (S_ISBLK(st.st_mode))
1866                 d = st.st_rdev;
1867         else if (major(st.st_dev) != 0) {
1868                 /* If this is not a device node then find the block
1869                  * device this file is stored on */
1870                 d = st.st_dev;
1871
1872                 /* If this is a partition, try to get the originating
1873                  * block device */
1874                 block_get_whole_disk(d, &d);
1875         } else {
1876                 log_warning("%s is not a block device and file system block device cannot be determined or is not local.", l[0]);
1877                 strv_free(l);
1878                 return -ENODEV;
1879         }
1880
1881         if (asprintf(ret, "%u:%u %s", major(d), minor(d), l[1]) < 0) {
1882                 strv_free(l);
1883                 return -ENOMEM;
1884         }
1885
1886         strv_free(l);
1887         return 0;
1888 }
1889
1890 int config_parse_unit_blkio_weight(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata) {
1891         Unit *u = data;
1892         int r;
1893         unsigned long ul;
1894         const char *device = NULL, *weight;
1895         unsigned k;
1896         char *t, **l;
1897
1898         assert(filename);
1899         assert(lvalue);
1900         assert(rvalue);
1901         assert(data);
1902
1903         l = strv_split_quoted(rvalue);
1904         if (!l)
1905                 return -ENOMEM;
1906
1907         k = strv_length(l);
1908         if (k < 1 || k > 2) {
1909                 log_error("[%s:%u] Failed to parse weight value, ignoring: %s", filename, line, rvalue);
1910                 strv_free(l);
1911                 return 0;
1912         }
1913
1914         if (k == 1)
1915                 weight = l[0];
1916         else {
1917                 device = l[0];
1918                 weight = l[1];
1919         }
1920
1921         if (device && !path_is_absolute(device)) {
1922                 log_error("[%s:%u] Failed to parse block device node value, ignoring: %s", filename, line, rvalue);
1923                 strv_free(l);
1924                 return 0;
1925         }
1926
1927         if (safe_atolu(weight, &ul) < 0 || ul < 10 || ul > 1000) {
1928                 log_error("[%s:%u] Failed to parse block IO weight value, ignoring: %s", filename, line, rvalue);
1929                 strv_free(l);
1930                 return 0;
1931         }
1932
1933         if (device)
1934                 r = asprintf(&t, "%s %lu", device, ul);
1935         else
1936                 r = asprintf(&t, "%lu", ul);
1937         strv_free(l);
1938
1939         if (r < 0)
1940                 return -ENOMEM;
1941
1942         if (device)
1943                 r = unit_add_cgroup_attribute(u, "blkio", "blkio.weight_device", t, blkio_map);
1944         else
1945                 r = unit_add_cgroup_attribute(u, "blkio", "blkio.weight", t, NULL);
1946         free(t);
1947
1948         if (r < 0) {
1949                 log_error("[%s:%u] Failed to add cgroup attribute value, ignoring: %s", filename, line, rvalue);
1950                 return 0;
1951         }
1952
1953         return 0;
1954 }
1955
1956 int config_parse_unit_blkio_bandwidth(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata) {
1957         Unit *u = data;
1958         int r;
1959         off_t bytes;
1960         unsigned k;
1961         char *t, **l;
1962
1963         assert(filename);
1964         assert(lvalue);
1965         assert(rvalue);
1966         assert(data);
1967
1968         l = strv_split_quoted(rvalue);
1969         if (!l)
1970                 return -ENOMEM;
1971
1972         k = strv_length(l);
1973         if (k != 2) {
1974                 log_error("[%s:%u] Failed to parse bandwidth value, ignoring: %s", filename, line, rvalue);
1975                 strv_free(l);
1976                 return 0;
1977         }
1978
1979         if (!path_is_absolute(l[0])) {
1980                 log_error("[%s:%u] Failed to parse block device node value, ignoring: %s", filename, line, rvalue);
1981                 strv_free(l);
1982                 return 0;
1983         }
1984
1985         if (parse_bytes(l[1], &bytes) < 0 || bytes <= 0) {
1986                 log_error("[%s:%u] Failed to parse block IO bandwith value, ignoring: %s", filename, line, rvalue);
1987                 strv_free(l);
1988                 return 0;
1989         }
1990
1991         r = asprintf(&t, "%s %llu", l[0], (unsigned long long) bytes);
1992         strv_free(l);
1993
1994         if (r < 0)
1995                 return -ENOMEM;
1996
1997         r = unit_add_cgroup_attribute(u, "blkio",
1998                                       streq(lvalue, "BlockIOReadBandwidth") ? "blkio.read_bps_device" : "blkio.write_bps_device",
1999                                       t, blkio_map);
2000         free(t);
2001
2002         if (r < 0) {
2003                 log_error("[%s:%u] Failed to add cgroup attribute value, ignoring: %s", filename, line, rvalue);
2004                 return 0;
2005         }
2006
2007         return 0;
2008 }
2009
2010
2011 #define FOLLOW_MAX 8
2012
2013 static int open_follow(char **filename, FILE **_f, Set *names, char **_final) {
2014         unsigned c = 0;
2015         int fd, r;
2016         FILE *f;
2017         char *id = NULL;
2018
2019         assert(filename);
2020         assert(*filename);
2021         assert(_f);
2022         assert(names);
2023
2024         /* This will update the filename pointer if the loaded file is
2025          * reached by a symlink. The old string will be freed. */
2026
2027         for (;;) {
2028                 char *target, *name;
2029
2030                 if (c++ >= FOLLOW_MAX)
2031                         return -ELOOP;
2032
2033                 path_kill_slashes(*filename);
2034
2035                 /* Add the file name we are currently looking at to
2036                  * the names of this unit, but only if it is a valid
2037                  * unit name. */
2038                 name = file_name_from_path(*filename);
2039
2040                 if (unit_name_is_valid(name, true)) {
2041
2042                         id = set_get(names, name);
2043                         if (!id) {
2044                                 id = strdup(name);
2045                                 if (!id)
2046                                         return -ENOMEM;
2047
2048                                 r = set_put(names, id);
2049                                 if (r < 0) {
2050                                         free(id);
2051                                         return r;
2052                                 }
2053                         }
2054                 }
2055
2056                 /* Try to open the file name, but don't if its a symlink */
2057                 if ((fd = open(*filename, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW)) >= 0)
2058                         break;
2059
2060                 if (errno != ELOOP)
2061                         return -errno;
2062
2063                 /* Hmm, so this is a symlink. Let's read the name, and follow it manually */
2064                 if ((r = readlink_and_make_absolute(*filename, &target)) < 0)
2065                         return r;
2066
2067                 free(*filename);
2068                 *filename = target;
2069         }
2070
2071         if (!(f = fdopen(fd, "re"))) {
2072                 r = -errno;
2073                 close_nointr_nofail(fd);
2074                 return r;
2075         }
2076
2077         *_f = f;
2078         *_final = id;
2079         return 0;
2080 }
2081
2082 static int merge_by_names(Unit **u, Set *names, const char *id) {
2083         char *k;
2084         int r;
2085
2086         assert(u);
2087         assert(*u);
2088         assert(names);
2089
2090         /* Let's try to add in all symlink names we found */
2091         while ((k = set_steal_first(names))) {
2092
2093                 /* First try to merge in the other name into our
2094                  * unit */
2095                 if ((r = unit_merge_by_name(*u, k)) < 0) {
2096                         Unit *other;
2097
2098                         /* Hmm, we couldn't merge the other unit into
2099                          * ours? Then let's try it the other way
2100                          * round */
2101
2102                         other = manager_get_unit((*u)->meta.manager, k);
2103                         free(k);
2104
2105                         if (other)
2106                                 if ((r = unit_merge(other, *u)) >= 0) {
2107                                         *u = other;
2108                                         return merge_by_names(u, names, NULL);
2109                                 }
2110
2111                         return r;
2112                 }
2113
2114                 if (id == k)
2115                         unit_choose_id(*u, id);
2116
2117                 free(k);
2118         }
2119
2120         return 0;
2121 }
2122
2123 static int load_from_path(Unit *u, const char *path) {
2124         int r;
2125         Set *symlink_names;
2126         FILE *f = NULL;
2127         char *filename = NULL, *id = NULL;
2128         Unit *merged;
2129         struct stat st;
2130
2131         assert(u);
2132         assert(path);
2133
2134         symlink_names = set_new(string_hash_func, string_compare_func);
2135         if (!symlink_names)
2136                 return -ENOMEM;
2137
2138         if (path_is_absolute(path)) {
2139
2140                 if (!(filename = strdup(path))) {
2141                         r = -ENOMEM;
2142                         goto finish;
2143                 }
2144
2145                 if ((r = open_follow(&filename, &f, symlink_names, &id)) < 0) {
2146                         free(filename);
2147                         filename = NULL;
2148
2149                         if (r != -ENOENT)
2150                                 goto finish;
2151                 }
2152
2153         } else  {
2154                 char **p;
2155
2156                 STRV_FOREACH(p, u->meta.manager->lookup_paths.unit_path) {
2157
2158                         /* Instead of opening the path right away, we manually
2159                          * follow all symlinks and add their name to our unit
2160                          * name set while doing so */
2161                         if (!(filename = path_make_absolute(path, *p))) {
2162                                 r = -ENOMEM;
2163                                 goto finish;
2164                         }
2165
2166                         if (u->meta.manager->unit_path_cache &&
2167                             !set_get(u->meta.manager->unit_path_cache, filename))
2168                                 r = -ENOENT;
2169                         else
2170                                 r = open_follow(&filename, &f, symlink_names, &id);
2171
2172                         if (r < 0) {
2173                                 char *sn;
2174
2175                                 free(filename);
2176                                 filename = NULL;
2177
2178                                 if (r != -ENOENT)
2179                                         goto finish;
2180
2181                                 /* Empty the symlink names for the next run */
2182                                 while ((sn = set_steal_first(symlink_names)))
2183                                         free(sn);
2184
2185                                 continue;
2186                         }
2187
2188                         break;
2189                 }
2190         }
2191
2192         if (!filename) {
2193                 /* Hmm, no suitable file found? */
2194                 r = 0;
2195                 goto finish;
2196         }
2197
2198         merged = u;
2199         if ((r = merge_by_names(&merged, symlink_names, id)) < 0)
2200                 goto finish;
2201
2202         if (merged != u) {
2203                 u->meta.load_state = UNIT_MERGED;
2204                 r = 0;
2205                 goto finish;
2206         }
2207
2208         zero(st);
2209         if (fstat(fileno(f), &st) < 0) {
2210                 r = -errno;
2211                 goto finish;
2212         }
2213
2214         if (null_or_empty(&st))
2215                 u->meta.load_state = UNIT_MASKED;
2216         else {
2217                 /* Now, parse the file contents */
2218                 r = config_parse(filename, f, UNIT_VTABLE(u)->sections, config_item_perf_lookup, (void*) load_fragment_gperf_lookup, false, u);
2219                 if (r < 0)
2220                         goto finish;
2221
2222                 u->meta.load_state = UNIT_LOADED;
2223         }
2224
2225         free(u->meta.fragment_path);
2226         u->meta.fragment_path = filename;
2227         filename = NULL;
2228
2229         u->meta.fragment_mtime = timespec_load(&st.st_mtim);
2230
2231         r = 0;
2232
2233 finish:
2234         set_free_free(symlink_names);
2235         free(filename);
2236
2237         if (f)
2238                 fclose(f);
2239
2240         return r;
2241 }
2242
2243 int unit_load_fragment(Unit *u) {
2244         int r;
2245         Iterator i;
2246         const char *t;
2247
2248         assert(u);
2249         assert(u->meta.load_state == UNIT_STUB);
2250         assert(u->meta.id);
2251
2252         /* First, try to find the unit under its id. We always look
2253          * for unit files in the default directories, to make it easy
2254          * to override things by placing things in /etc/systemd/system */
2255         if ((r = load_from_path(u, u->meta.id)) < 0)
2256                 return r;
2257
2258         /* Try to find an alias we can load this with */
2259         if (u->meta.load_state == UNIT_STUB)
2260                 SET_FOREACH(t, u->meta.names, i) {
2261
2262                         if (t == u->meta.id)
2263                                 continue;
2264
2265                         if ((r = load_from_path(u, t)) < 0)
2266                                 return r;
2267
2268                         if (u->meta.load_state != UNIT_STUB)
2269                                 break;
2270                 }
2271
2272         /* And now, try looking for it under the suggested (originally linked) path */
2273         if (u->meta.load_state == UNIT_STUB && u->meta.fragment_path) {
2274
2275                 if ((r = load_from_path(u, u->meta.fragment_path)) < 0)
2276                         return r;
2277
2278                 if (u->meta.load_state == UNIT_STUB) {
2279                         /* Hmm, this didn't work? Then let's get rid
2280                          * of the fragment path stored for us, so that
2281                          * we don't point to an invalid location. */
2282                         free(u->meta.fragment_path);
2283                         u->meta.fragment_path = NULL;
2284                 }
2285         }
2286
2287         /* Look for a template */
2288         if (u->meta.load_state == UNIT_STUB && u->meta.instance) {
2289                 char *k;
2290
2291                 if (!(k = unit_name_template(u->meta.id)))
2292                         return -ENOMEM;
2293
2294                 r = load_from_path(u, k);
2295                 free(k);
2296
2297                 if (r < 0)
2298                         return r;
2299
2300                 if (u->meta.load_state == UNIT_STUB)
2301                         SET_FOREACH(t, u->meta.names, i) {
2302
2303                                 if (t == u->meta.id)
2304                                         continue;
2305
2306                                 if (!(k = unit_name_template(t)))
2307                                         return -ENOMEM;
2308
2309                                 r = load_from_path(u, k);
2310                                 free(k);
2311
2312                                 if (r < 0)
2313                                         return r;
2314
2315                                 if (u->meta.load_state != UNIT_STUB)
2316                                         break;
2317                         }
2318         }
2319
2320         return 0;
2321 }
2322
2323 void unit_dump_config_items(FILE *f) {
2324         static const struct {
2325                 const ConfigParserCallback callback;
2326                 const char *rvalue;
2327         } table[] = {
2328                 { config_parse_int,                   "INTEGER" },
2329                 { config_parse_unsigned,              "UNSIGNED" },
2330                 { config_parse_size,                  "SIZE" },
2331                 { config_parse_bool,                  "BOOLEAN" },
2332                 { config_parse_string,                "STRING" },
2333                 { config_parse_path,                  "PATH" },
2334                 { config_parse_unit_path_printf,      "PATH" },
2335                 { config_parse_strv,                  "STRING [...]" },
2336                 { config_parse_exec_nice,             "NICE" },
2337                 { config_parse_exec_oom_score_adjust, "OOMSCOREADJUST" },
2338                 { config_parse_exec_io_class,         "IOCLASS" },
2339                 { config_parse_exec_io_priority,      "IOPRIORITY" },
2340                 { config_parse_exec_cpu_sched_policy, "CPUSCHEDPOLICY" },
2341                 { config_parse_exec_cpu_sched_prio,   "CPUSCHEDPRIO" },
2342                 { config_parse_exec_cpu_affinity,     "CPUAFFINITY" },
2343                 { config_parse_mode,                  "MODE" },
2344                 { config_parse_unit_env_file,         "FILE" },
2345                 { config_parse_output,                "OUTPUT" },
2346                 { config_parse_input,                 "INPUT" },
2347                 { config_parse_facility,              "FACILITY" },
2348                 { config_parse_level,                 "LEVEL" },
2349                 { config_parse_exec_capabilities,     "CAPABILITIES" },
2350                 { config_parse_exec_secure_bits,      "SECUREBITS" },
2351                 { config_parse_exec_bounding_set,     "BOUNDINGSET" },
2352                 { config_parse_exec_timer_slack_nsec, "TIMERSLACK" },
2353                 { config_parse_limit,                 "LIMIT" },
2354                 { config_parse_unit_cgroup,           "CGROUP [...]" },
2355                 { config_parse_unit_deps,             "UNIT [...]" },
2356                 { config_parse_unit_names,            "UNIT [...]" },
2357                 { config_parse_exec,                  "PATH [ARGUMENT [...]]" },
2358                 { config_parse_service_type,          "SERVICETYPE" },
2359                 { config_parse_service_restart,       "SERVICERESTART" },
2360 #ifdef HAVE_SYSV_COMPAT
2361                 { config_parse_sysv_priority,         "SYSVPRIORITY" },
2362 #else
2363                 { config_parse_warn_compat,           "NOTSUPPORTED" },
2364 #endif
2365                 { config_parse_kill_mode,             "KILLMODE" },
2366                 { config_parse_kill_signal,           "SIGNAL" },
2367                 { config_parse_socket_listen,         "SOCKET [...]" },
2368                 { config_parse_socket_bind,           "SOCKETBIND" },
2369                 { config_parse_socket_bindtodevice,   "NETWORKINTERFACE" },
2370                 { config_parse_usec,                  "SECONDS" },
2371                 { config_parse_path_strv,             "PATH [...]" },
2372                 { config_parse_exec_mount_flags,      "MOUNTFLAG [...]" },
2373                 { config_parse_unit_string_printf,    "STRING" },
2374                 { config_parse_timer,                 "TIMER" },
2375                 { config_parse_timer_unit,            "NAME" },
2376                 { config_parse_path_spec,             "PATH" },
2377                 { config_parse_path_unit,             "UNIT" },
2378                 { config_parse_notify_access,         "ACCESS" },
2379                 { config_parse_ip_tos,                "TOS" },
2380                 { config_parse_unit_condition_path,   "CONDITION" },
2381                 { config_parse_unit_condition_string, "CONDITION" },
2382                 { config_parse_unit_condition_null,   "CONDITION" },
2383         };
2384
2385         const char *prev = NULL;
2386         const char *i;
2387
2388         assert(f);
2389
2390         NULSTR_FOREACH(i, load_fragment_gperf_nulstr) {
2391                 const char *rvalue = "OTHER", *lvalue;
2392                 unsigned j;
2393                 size_t prefix_len;
2394                 const char *dot;
2395                 const ConfigPerfItem *p;
2396
2397                 assert_se(p = load_fragment_gperf_lookup(i, strlen(i)));
2398
2399                 dot = strchr(i, '.');
2400                 lvalue = dot ? dot + 1 : i;
2401                 prefix_len = dot-i;
2402
2403                 if (dot)
2404                         if (!prev || strncmp(prev, i, prefix_len+1) != 0) {
2405                                 if (prev)
2406                                         fputc('\n', f);
2407
2408                                 fprintf(f, "[%.*s]\n", (int) prefix_len, i);
2409                         }
2410
2411                 for (j = 0; j < ELEMENTSOF(table); j++)
2412                         if (p->parse == table[j].callback) {
2413                                 rvalue = table[j].rvalue;
2414                                 break;
2415                         }
2416
2417                 fprintf(f, "%s=%s\n", lvalue, rvalue);
2418                 prev = i;
2419         }
2420 }