chiark / gitweb /
journal: move field index from file into journal object
[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                         free(path);
558                         return 0;
559                 }
560
561                 if (!path)
562                         if (!(path = strdup(n[0])))
563                                 goto fail;
564
565                 assert(path_is_absolute(path));
566
567                 if (!(nce = new0(ExecCommand, 1)))
568                         goto fail;
569
570                 nce->argv = n;
571                 nce->path = path;
572                 nce->ignore = ignore;
573
574                 path_kill_slashes(nce->path);
575
576                 exec_command_append_list(e, nce);
577
578                 rvalue = state;
579         }
580
581         return 0;
582
583 fail:
584         n[k] = NULL;
585         strv_free(n);
586         free(path);
587         free(nce);
588
589         return -ENOMEM;
590 }
591
592 DEFINE_CONFIG_PARSE_ENUM(config_parse_service_type, service_type, ServiceType, "Failed to parse service type");
593 DEFINE_CONFIG_PARSE_ENUM(config_parse_service_restart, service_restart, ServiceRestart, "Failed to parse service restart specifier");
594
595 int config_parse_socket_bindtodevice(
596                 const char *filename,
597                 unsigned line,
598                 const char *section,
599                 const char *lvalue,
600                 int ltype,
601                 const char *rvalue,
602                 void *data,
603                 void *userdata) {
604
605         Socket *s = data;
606         char *n;
607
608         assert(filename);
609         assert(lvalue);
610         assert(rvalue);
611         assert(data);
612
613         if (rvalue[0] && !streq(rvalue, "*")) {
614                 if (!(n = strdup(rvalue)))
615                         return -ENOMEM;
616         } else
617                 n = NULL;
618
619         free(s->bind_to_device);
620         s->bind_to_device = n;
621
622         return 0;
623 }
624
625 DEFINE_CONFIG_PARSE_ENUM(config_parse_output, exec_output, ExecOutput, "Failed to parse output specifier");
626 DEFINE_CONFIG_PARSE_ENUM(config_parse_input, exec_input, ExecInput, "Failed to parse input specifier");
627
628 int config_parse_facility(
629                 const char *filename,
630                 unsigned line,
631                 const char *section,
632                 const char *lvalue,
633                 int ltype,
634                 const char *rvalue,
635                 void *data,
636                 void *userdata) {
637
638
639         int *o = data, x;
640
641         assert(filename);
642         assert(lvalue);
643         assert(rvalue);
644         assert(data);
645
646         if ((x = log_facility_unshifted_from_string(rvalue)) < 0) {
647                 log_error("[%s:%u] Failed to parse log facility, ignoring: %s", filename, line, rvalue);
648                 return 0;
649         }
650
651         *o = (x << 3) | LOG_PRI(*o);
652
653         return 0;
654 }
655
656 int config_parse_level(
657                 const char *filename,
658                 unsigned line,
659                 const char *section,
660                 const char *lvalue,
661                 int ltype,
662                 const char *rvalue,
663                 void *data,
664                 void *userdata) {
665
666
667         int *o = data, x;
668
669         assert(filename);
670         assert(lvalue);
671         assert(rvalue);
672         assert(data);
673
674         if ((x = log_level_from_string(rvalue)) < 0) {
675                 log_error("[%s:%u] Failed to parse log level, ignoring: %s", filename, line, rvalue);
676                 return 0;
677         }
678
679         *o = (*o & LOG_FACMASK) | x;
680         return 0;
681 }
682
683 int config_parse_exec_io_class(
684                 const char *filename,
685                 unsigned line,
686                 const char *section,
687                 const char *lvalue,
688                 int ltype,
689                 const char *rvalue,
690                 void *data,
691                 void *userdata) {
692
693         ExecContext *c = data;
694         int x;
695
696         assert(filename);
697         assert(lvalue);
698         assert(rvalue);
699         assert(data);
700
701         if ((x = ioprio_class_from_string(rvalue)) < 0) {
702                 log_error("[%s:%u] Failed to parse IO scheduling class, ignoring: %s", filename, line, rvalue);
703                 return 0;
704         }
705
706         c->ioprio = IOPRIO_PRIO_VALUE(x, IOPRIO_PRIO_DATA(c->ioprio));
707         c->ioprio_set = true;
708
709         return 0;
710 }
711
712 int config_parse_exec_io_priority(
713                 const char *filename,
714                 unsigned line,
715                 const char *section,
716                 const char *lvalue,
717                 int ltype,
718                 const char *rvalue,
719                 void *data,
720                 void *userdata) {
721
722         ExecContext *c = data;
723         int i;
724
725         assert(filename);
726         assert(lvalue);
727         assert(rvalue);
728         assert(data);
729
730         if (safe_atoi(rvalue, &i) < 0 || i < 0 || i >= IOPRIO_BE_NR) {
731                 log_error("[%s:%u] Failed to parse io priority, ignoring: %s", filename, line, rvalue);
732                 return 0;
733         }
734
735         c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_PRIO_CLASS(c->ioprio), i);
736         c->ioprio_set = true;
737
738         return 0;
739 }
740
741 int config_parse_exec_cpu_sched_policy(
742                 const char *filename,
743                 unsigned line,
744                 const char *section,
745                 const char *lvalue,
746                 int ltype,
747                 const char *rvalue,
748                 void *data,
749                 void *userdata) {
750
751
752         ExecContext *c = data;
753         int x;
754
755         assert(filename);
756         assert(lvalue);
757         assert(rvalue);
758         assert(data);
759
760         if ((x = sched_policy_from_string(rvalue)) < 0) {
761                 log_error("[%s:%u] Failed to parse CPU scheduling policy, ignoring: %s", filename, line, rvalue);
762                 return 0;
763         }
764
765         c->cpu_sched_policy = x;
766         c->cpu_sched_set = true;
767
768         return 0;
769 }
770
771 int config_parse_exec_cpu_sched_prio(
772                 const char *filename,
773                 unsigned line,
774                 const char *section,
775                 const char *lvalue,
776                 int ltype,
777                 const char *rvalue,
778                 void *data,
779                 void *userdata) {
780
781         ExecContext *c = data;
782         int i;
783
784         assert(filename);
785         assert(lvalue);
786         assert(rvalue);
787         assert(data);
788
789         /* On Linux RR/FIFO have the same range */
790         if (safe_atoi(rvalue, &i) < 0 || i < sched_get_priority_min(SCHED_RR) || i > sched_get_priority_max(SCHED_RR)) {
791                 log_error("[%s:%u] Failed to parse CPU scheduling priority, ignoring: %s", filename, line, rvalue);
792                 return 0;
793         }
794
795         c->cpu_sched_priority = i;
796         c->cpu_sched_set = true;
797
798         return 0;
799 }
800
801 int config_parse_exec_cpu_affinity(
802                 const char *filename,
803                 unsigned line,
804                 const char *section,
805                 const char *lvalue,
806                 int ltype,
807                 const char *rvalue,
808                 void *data,
809                 void *userdata) {
810
811         ExecContext *c = data;
812         char *w;
813         size_t l;
814         char *state;
815
816         assert(filename);
817         assert(lvalue);
818         assert(rvalue);
819         assert(data);
820
821         FOREACH_WORD_QUOTED(w, l, rvalue, state) {
822                 char *t;
823                 int r;
824                 unsigned cpu;
825
826                 if (!(t = strndup(w, l)))
827                         return -ENOMEM;
828
829                 r = safe_atou(t, &cpu);
830                 free(t);
831
832                 if (!(c->cpuset))
833                         if (!(c->cpuset = cpu_set_malloc(&c->cpuset_ncpus)))
834                                 return -ENOMEM;
835
836                 if (r < 0 || cpu >= c->cpuset_ncpus) {
837                         log_error("[%s:%u] Failed to parse CPU affinity, ignoring: %s", filename, line, rvalue);
838                         return 0;
839                 }
840
841                 CPU_SET_S(cpu, CPU_ALLOC_SIZE(c->cpuset_ncpus), c->cpuset);
842         }
843
844         return 0;
845 }
846
847 int config_parse_exec_capabilities(
848                 const char *filename,
849                 unsigned line,
850                 const char *section,
851                 const char *lvalue,
852                 int ltype,
853                 const char *rvalue,
854                 void *data,
855                 void *userdata) {
856
857         ExecContext *c = data;
858         cap_t cap;
859
860         assert(filename);
861         assert(lvalue);
862         assert(rvalue);
863         assert(data);
864
865         if (!(cap = cap_from_text(rvalue))) {
866                 if (errno == ENOMEM)
867                         return -ENOMEM;
868
869                 log_error("[%s:%u] Failed to parse capabilities, ignoring: %s", filename, line, rvalue);
870                 return 0;
871         }
872
873         if (c->capabilities)
874                 cap_free(c->capabilities);
875         c->capabilities = cap;
876
877         return 0;
878 }
879
880 int config_parse_exec_secure_bits(
881                 const char *filename,
882                 unsigned line,
883                 const char *section,
884                 const char *lvalue,
885                 int ltype,
886                 const char *rvalue,
887                 void *data,
888                 void *userdata) {
889
890         ExecContext *c = data;
891         char *w;
892         size_t l;
893         char *state;
894
895         assert(filename);
896         assert(lvalue);
897         assert(rvalue);
898         assert(data);
899
900         FOREACH_WORD_QUOTED(w, l, rvalue, state) {
901                 if (first_word(w, "keep-caps"))
902                         c->secure_bits |= SECURE_KEEP_CAPS;
903                 else if (first_word(w, "keep-caps-locked"))
904                         c->secure_bits |= SECURE_KEEP_CAPS_LOCKED;
905                 else if (first_word(w, "no-setuid-fixup"))
906                         c->secure_bits |= SECURE_NO_SETUID_FIXUP;
907                 else if (first_word(w, "no-setuid-fixup-locked"))
908                         c->secure_bits |= SECURE_NO_SETUID_FIXUP_LOCKED;
909                 else if (first_word(w, "noroot"))
910                         c->secure_bits |= SECURE_NOROOT;
911                 else if (first_word(w, "noroot-locked"))
912                         c->secure_bits |= SECURE_NOROOT_LOCKED;
913                 else {
914                         log_error("[%s:%u] Failed to parse secure bits, ignoring: %s", filename, line, rvalue);
915                         return 0;
916                 }
917         }
918
919         return 0;
920 }
921
922 int config_parse_exec_bounding_set(
923                 const char *filename,
924                 unsigned line,
925                 const char *section,
926                 const char *lvalue,
927                 int ltype,
928                 const char *rvalue,
929                 void *data,
930                 void *userdata) {
931
932         ExecContext *c = data;
933         char *w;
934         size_t l;
935         char *state;
936         bool invert = false;
937         uint64_t sum = 0;
938
939         assert(filename);
940         assert(lvalue);
941         assert(rvalue);
942         assert(data);
943
944         if (rvalue[0] == '~') {
945                 invert = true;
946                 rvalue++;
947         }
948
949         /* Note that we store this inverted internally, since the
950          * kernel wants it like this. But we actually expose it
951          * non-inverted everywhere to have a fully normalized
952          * interface. */
953
954         FOREACH_WORD_QUOTED(w, l, rvalue, state) {
955                 char *t;
956                 int r;
957                 cap_value_t cap;
958
959                 if (!(t = strndup(w, l)))
960                         return -ENOMEM;
961
962                 r = cap_from_name(t, &cap);
963                 free(t);
964
965                 if (r < 0) {
966                         log_error("[%s:%u] Failed to parse capability bounding set, ignoring: %s", filename, line, rvalue);
967                         return 0;
968                 }
969
970                 sum |= ((uint64_t) 1ULL) << (uint64_t) cap;
971         }
972
973         if (invert)
974                 c->capability_bounding_set_drop |= sum;
975         else
976                 c->capability_bounding_set_drop |= ~sum;
977
978         return 0;
979 }
980
981 int config_parse_exec_timer_slack_nsec(
982                 const char *filename,
983                 unsigned line,
984                 const char *section,
985                 const char *lvalue,
986                 int ltype,
987                 const char *rvalue,
988                 void *data,
989                 void *userdata) {
990
991         ExecContext *c = data;
992         unsigned long u;
993
994         assert(filename);
995         assert(lvalue);
996         assert(rvalue);
997         assert(data);
998
999         if (safe_atolu(rvalue, &u) < 0) {
1000                 log_error("[%s:%u] Failed to parse time slack value, ignoring: %s", filename, line, rvalue);
1001                 return 0;
1002         }
1003
1004         c->timer_slack_nsec = u;
1005
1006         return 0;
1007 }
1008
1009 int config_parse_limit(
1010                 const char *filename,
1011                 unsigned line,
1012                 const char *section,
1013                 const char *lvalue,
1014                 int ltype,
1015                 const char *rvalue,
1016                 void *data,
1017                 void *userdata) {
1018
1019         struct rlimit **rl = data;
1020         unsigned long long u;
1021
1022         assert(filename);
1023         assert(lvalue);
1024         assert(rvalue);
1025         assert(data);
1026
1027         rl += ltype;
1028
1029         if (streq(rvalue, "infinity"))
1030                 u = (unsigned long long) RLIM_INFINITY;
1031         else if (safe_atollu(rvalue, &u) < 0) {
1032                 log_error("[%s:%u] Failed to parse resource value, ignoring: %s", filename, line, rvalue);
1033                 return 0;
1034         }
1035
1036         if (!*rl)
1037                 if (!(*rl = new(struct rlimit, 1)))
1038                         return -ENOMEM;
1039
1040         (*rl)->rlim_cur = (*rl)->rlim_max = (rlim_t) u;
1041         return 0;
1042 }
1043
1044 int config_parse_unit_cgroup(
1045                 const char *filename,
1046                 unsigned line,
1047                 const char *section,
1048                 const char *lvalue,
1049                 int ltype,
1050                 const char *rvalue,
1051                 void *data,
1052                 void *userdata) {
1053
1054         Unit *u = userdata;
1055         char *w;
1056         size_t l;
1057         char *state;
1058
1059         FOREACH_WORD_QUOTED(w, l, rvalue, state) {
1060                 char *t, *k;
1061                 int r;
1062
1063                 t = strndup(w, l);
1064                 if (!t)
1065                         return -ENOMEM;
1066
1067                 k = unit_full_printf(u, t);
1068                 free(t);
1069
1070                 if (!k)
1071                         return -ENOMEM;
1072
1073                 t = cunescape(k);
1074                 free(k);
1075
1076                 if (!t)
1077                         return -ENOMEM;
1078
1079                 r = unit_add_cgroup_from_text(u, t);
1080                 free(t);
1081
1082                 if (r < 0) {
1083                         log_error("[%s:%u] Failed to parse cgroup value, ignoring: %s", filename, line, rvalue);
1084                         return 0;
1085                 }
1086         }
1087
1088         return 0;
1089 }
1090
1091 #ifdef HAVE_SYSV_COMPAT
1092 int config_parse_sysv_priority(
1093                 const char *filename,
1094                 unsigned line,
1095                 const char *section,
1096                 const char *lvalue,
1097                 int ltype,
1098                 const char *rvalue,
1099                 void *data,
1100                 void *userdata) {
1101
1102         int *priority = data;
1103         int i;
1104
1105         assert(filename);
1106         assert(lvalue);
1107         assert(rvalue);
1108         assert(data);
1109
1110         if (safe_atoi(rvalue, &i) < 0 || i < 0) {
1111                 log_error("[%s:%u] Failed to parse SysV start priority, ignoring: %s", filename, line, rvalue);
1112                 return 0;
1113         }
1114
1115         *priority = (int) i;
1116         return 0;
1117 }
1118 #endif
1119
1120 int config_parse_fsck_passno(
1121                 const char *filename,
1122                 unsigned line,
1123                 const char *section,
1124                 const char *lvalue,
1125                 int ltype,
1126                 const char *rvalue,
1127                 void *data,
1128                 void *userdata) {
1129
1130         int *passno = data;
1131         int i;
1132
1133         assert(filename);
1134         assert(lvalue);
1135         assert(rvalue);
1136         assert(data);
1137
1138         if (safe_atoi(rvalue, &i) || i < 0) {
1139                 log_error("[%s:%u] Failed to parse fsck pass number, ignoring: %s", filename, line, rvalue);
1140                 return 0;
1141         }
1142
1143         *passno = (int) i;
1144         return 0;
1145 }
1146
1147 DEFINE_CONFIG_PARSE_ENUM(config_parse_kill_mode, kill_mode, KillMode, "Failed to parse kill mode");
1148
1149 int config_parse_kill_signal(
1150                 const char *filename,
1151                 unsigned line,
1152                 const char *section,
1153                 const char *lvalue,
1154                 int ltype,
1155                 const char *rvalue,
1156                 void *data,
1157                 void *userdata) {
1158
1159         int *sig = data;
1160         int r;
1161
1162         assert(filename);
1163         assert(lvalue);
1164         assert(rvalue);
1165         assert(sig);
1166
1167         if ((r = signal_from_string_try_harder(rvalue)) <= 0) {
1168                 log_error("[%s:%u] Failed to parse kill signal, ignoring: %s", filename, line, rvalue);
1169                 return 0;
1170         }
1171
1172         *sig = r;
1173         return 0;
1174 }
1175
1176 int config_parse_exec_mount_flags(
1177                 const char *filename,
1178                 unsigned line,
1179                 const char *section,
1180                 const char *lvalue,
1181                 int ltype,
1182                 const char *rvalue,
1183                 void *data,
1184                 void *userdata) {
1185
1186         ExecContext *c = data;
1187         char *w;
1188         size_t l;
1189         char *state;
1190         unsigned long flags = 0;
1191
1192         assert(filename);
1193         assert(lvalue);
1194         assert(rvalue);
1195         assert(data);
1196
1197         FOREACH_WORD_QUOTED(w, l, rvalue, state) {
1198                 if (strncmp(w, "shared", MAX(l, 6U)) == 0)
1199                         flags |= MS_SHARED;
1200                 else if (strncmp(w, "slave", MAX(l, 5U)) == 0)
1201                         flags |= MS_SLAVE;
1202                 else if (strncmp(w, "private", MAX(l, 7U)) == 0)
1203                         flags |= MS_PRIVATE;
1204                 else {
1205                         log_error("[%s:%u] Failed to parse mount flags, ignoring: %s", filename, line, rvalue);
1206                         return 0;
1207                 }
1208         }
1209
1210         c->mount_flags = flags;
1211         return 0;
1212 }
1213
1214 int config_parse_timer(
1215                 const char *filename,
1216                 unsigned line,
1217                 const char *section,
1218                 const char *lvalue,
1219                 int ltype,
1220                 const char *rvalue,
1221                 void *data,
1222                 void *userdata) {
1223
1224         Timer *t = data;
1225         usec_t u;
1226         TimerValue *v;
1227         TimerBase b;
1228
1229         assert(filename);
1230         assert(lvalue);
1231         assert(rvalue);
1232         assert(data);
1233
1234         if ((b = timer_base_from_string(lvalue)) < 0) {
1235                 log_error("[%s:%u] Failed to parse timer base, ignoring: %s", filename, line, lvalue);
1236                 return 0;
1237         }
1238
1239         if (parse_usec(rvalue, &u) < 0) {
1240                 log_error("[%s:%u] Failed to parse timer value, ignoring: %s", filename, line, rvalue);
1241                 return 0;
1242         }
1243
1244         if (!(v = new0(TimerValue, 1)))
1245                 return -ENOMEM;
1246
1247         v->base = b;
1248         v->value = u;
1249
1250         LIST_PREPEND(TimerValue, value, t->values, v);
1251
1252         return 0;
1253 }
1254
1255 int config_parse_timer_unit(
1256                 const char *filename,
1257                 unsigned line,
1258                 const char *section,
1259                 const char *lvalue,
1260                 int ltype,
1261                 const char *rvalue,
1262                 void *data,
1263                 void *userdata) {
1264
1265         Timer *t = data;
1266         int r;
1267         DBusError error;
1268
1269         assert(filename);
1270         assert(lvalue);
1271         assert(rvalue);
1272         assert(data);
1273
1274         dbus_error_init(&error);
1275
1276         if (endswith(rvalue, ".timer")) {
1277                 log_error("[%s:%u] Unit cannot be of type timer, ignoring: %s", filename, line, rvalue);
1278                 return 0;
1279         }
1280
1281         if ((r = manager_load_unit(t->meta.manager, rvalue, NULL, NULL, &t->unit)) < 0) {
1282                 log_error("[%s:%u] Failed to load unit %s, ignoring: %s", filename, line, rvalue, bus_error(&error, r));
1283                 dbus_error_free(&error);
1284                 return 0;
1285         }
1286
1287         return 0;
1288 }
1289
1290 int config_parse_path_spec(
1291                 const char *filename,
1292                 unsigned line,
1293                 const char *section,
1294                 const char *lvalue,
1295                 int ltype,
1296                 const char *rvalue,
1297                 void *data,
1298                 void *userdata) {
1299
1300         Path *p = data;
1301         PathSpec *s;
1302         PathType b;
1303
1304         assert(filename);
1305         assert(lvalue);
1306         assert(rvalue);
1307         assert(data);
1308
1309         if ((b = path_type_from_string(lvalue)) < 0) {
1310                 log_error("[%s:%u] Failed to parse path type, ignoring: %s", filename, line, lvalue);
1311                 return 0;
1312         }
1313
1314         if (!path_is_absolute(rvalue)) {
1315                 log_error("[%s:%u] Path is not absolute, ignoring: %s", filename, line, rvalue);
1316                 return 0;
1317         }
1318
1319         if (!(s = new0(PathSpec, 1)))
1320                 return -ENOMEM;
1321
1322         if (!(s->path = strdup(rvalue))) {
1323                 free(s);
1324                 return -ENOMEM;
1325         }
1326
1327         path_kill_slashes(s->path);
1328
1329         s->type = b;
1330         s->inotify_fd = -1;
1331
1332         LIST_PREPEND(PathSpec, spec, p->specs, s);
1333
1334         return 0;
1335 }
1336
1337 int config_parse_path_unit(
1338                 const char *filename,
1339                 unsigned line,
1340                 const char *section,
1341                 const char *lvalue,
1342                 int ltype,
1343                 const char *rvalue,
1344                 void *data,
1345                 void *userdata) {
1346
1347         Path *t = data;
1348         int r;
1349         DBusError error;
1350
1351         assert(filename);
1352         assert(lvalue);
1353         assert(rvalue);
1354         assert(data);
1355
1356         dbus_error_init(&error);
1357
1358         if (endswith(rvalue, ".path")) {
1359                 log_error("[%s:%u] Unit cannot be of type path, ignoring: %s", filename, line, rvalue);
1360                 return 0;
1361         }
1362
1363         if ((r = manager_load_unit(t->meta.manager, rvalue, NULL, &error, &t->unit)) < 0) {
1364                 log_error("[%s:%u] Failed to load unit %s, ignoring: %s", filename, line, rvalue, bus_error(&error, r));
1365                 dbus_error_free(&error);
1366                 return 0;
1367         }
1368
1369         return 0;
1370 }
1371
1372 int config_parse_socket_service(
1373                 const char *filename,
1374                 unsigned line,
1375                 const char *section,
1376                 const char *lvalue,
1377                 int ltype,
1378                 const char *rvalue,
1379                 void *data,
1380                 void *userdata) {
1381
1382         Socket *s = data;
1383         int r;
1384         DBusError error;
1385
1386         assert(filename);
1387         assert(lvalue);
1388         assert(rvalue);
1389         assert(data);
1390
1391         dbus_error_init(&error);
1392
1393         if (!endswith(rvalue, ".service")) {
1394                 log_error("[%s:%u] Unit must be of type service, ignoring: %s", filename, line, rvalue);
1395                 return 0;
1396         }
1397
1398         if ((r = manager_load_unit(s->meta.manager, rvalue, NULL, &error, (Unit**) &s->service)) < 0) {
1399                 log_error("[%s:%u] Failed to load unit %s, ignoring: %s", filename, line, rvalue, bus_error(&error, r));
1400                 dbus_error_free(&error);
1401                 return 0;
1402         }
1403
1404         return 0;
1405 }
1406
1407 int config_parse_service_sockets(
1408                 const char *filename,
1409                 unsigned line,
1410                 const char *section,
1411                 const char *lvalue,
1412                 int ltype,
1413                 const char *rvalue,
1414                 void *data,
1415                 void *userdata) {
1416
1417         Service *s = data;
1418         int r;
1419         DBusError error;
1420         char *state, *w;
1421         size_t l;
1422
1423         assert(filename);
1424         assert(lvalue);
1425         assert(rvalue);
1426         assert(data);
1427
1428         dbus_error_init(&error);
1429
1430         FOREACH_WORD_QUOTED(w, l, rvalue, state) {
1431                 char *t;
1432                 Unit *sock;
1433
1434                 if (!(t = strndup(w, l)))
1435                         return -ENOMEM;
1436
1437                 if (!endswith(t, ".socket")) {
1438                         log_error("[%s:%u] Unit must be of type socket, ignoring: %s", filename, line, rvalue);
1439                         free(t);
1440                         continue;
1441                 }
1442
1443                 r = manager_load_unit(s->meta.manager, t, NULL, &error, &sock);
1444                 free(t);
1445
1446                 if (r < 0) {
1447                         log_error("[%s:%u] Failed to load unit %s, ignoring: %s", filename, line, rvalue, bus_error(&error, r));
1448                         dbus_error_free(&error);
1449                         continue;
1450                 }
1451
1452                 if ((r = set_ensure_allocated(&s->configured_sockets, trivial_hash_func, trivial_compare_func)) < 0)
1453                         return r;
1454
1455                 if ((r = set_put(s->configured_sockets, sock)) < 0)
1456                         return r;
1457         }
1458
1459         return 0;
1460 }
1461
1462 int config_parse_unit_env_file(
1463                 const char *filename,
1464                 unsigned line,
1465                 const char *section,
1466                 const char *lvalue,
1467                 int ltype,
1468                 const char *rvalue,
1469                 void *data,
1470                 void *userdata) {
1471
1472         char ***env = data, **k;
1473         Unit *u = userdata;
1474         char *s;
1475
1476         assert(filename);
1477         assert(lvalue);
1478         assert(rvalue);
1479         assert(data);
1480
1481         s = unit_full_printf(u, rvalue);
1482         if (!s)
1483                 return -ENOMEM;
1484
1485         if (!path_is_absolute(s[0] == '-' ? s + 1 : s)) {
1486                 log_error("[%s:%u] Path '%s' is not absolute, ignoring.", filename, line, s);
1487                 free(s);
1488                 return 0;
1489         }
1490
1491         k = strv_append(*env, s);
1492         free(s);
1493         if (!k)
1494                 return -ENOMEM;
1495
1496         strv_free(*env);
1497         *env = k;
1498
1499         return 0;
1500 }
1501
1502 int config_parse_ip_tos(
1503                 const char *filename,
1504                 unsigned line,
1505                 const char *section,
1506                 const char *lvalue,
1507                 int ltype,
1508                 const char *rvalue,
1509                 void *data,
1510                 void *userdata) {
1511
1512         int *ip_tos = data, x;
1513
1514         assert(filename);
1515         assert(lvalue);
1516         assert(rvalue);
1517         assert(data);
1518
1519         if ((x = ip_tos_from_string(rvalue)) < 0)
1520                 if (safe_atoi(rvalue, &x) < 0) {
1521                         log_error("[%s:%u] Failed to parse IP TOS value, ignoring: %s", filename, line, rvalue);
1522                         return 0;
1523                 }
1524
1525         *ip_tos = x;
1526         return 0;
1527 }
1528
1529 int config_parse_unit_condition_path(
1530                 const char *filename,
1531                 unsigned line,
1532                 const char *section,
1533                 const char *lvalue,
1534                 int ltype,
1535                 const char *rvalue,
1536                 void *data,
1537                 void *userdata) {
1538
1539         ConditionType cond = ltype;
1540         Unit *u = data;
1541         bool trigger, negate;
1542         Condition *c;
1543
1544         assert(filename);
1545         assert(lvalue);
1546         assert(rvalue);
1547         assert(data);
1548
1549         trigger = rvalue[0] == '|';
1550         if (trigger)
1551                 rvalue++;
1552
1553         negate = rvalue[0] == '!';
1554         if (negate)
1555                 rvalue++;
1556
1557         if (!path_is_absolute(rvalue)) {
1558                 log_error("[%s:%u] Path in condition not absolute, ignoring: %s", filename, line, rvalue);
1559                 return 0;
1560         }
1561
1562         c = condition_new(cond, rvalue, trigger, negate);
1563         if (!c)
1564                 return -ENOMEM;
1565
1566         LIST_PREPEND(Condition, conditions, u->meta.conditions, c);
1567         return 0;
1568 }
1569
1570 int config_parse_unit_condition_string(
1571                 const char *filename,
1572                 unsigned line,
1573                 const char *section,
1574                 const char *lvalue,
1575                 int ltype,
1576                 const char *rvalue,
1577                 void *data,
1578                 void *userdata) {
1579
1580         ConditionType cond = ltype;
1581         Unit *u = data;
1582         bool trigger, negate;
1583         Condition *c;
1584
1585         assert(filename);
1586         assert(lvalue);
1587         assert(rvalue);
1588         assert(data);
1589
1590         if ((trigger = rvalue[0] == '|'))
1591                 rvalue++;
1592
1593         if ((negate = rvalue[0] == '!'))
1594                 rvalue++;
1595
1596         if (!(c = condition_new(cond, rvalue, trigger, negate)))
1597                 return -ENOMEM;
1598
1599         LIST_PREPEND(Condition, conditions, u->meta.conditions, c);
1600         return 0;
1601 }
1602
1603 int config_parse_unit_condition_null(
1604                 const char *filename,
1605                 unsigned line,
1606                 const char *section,
1607                 const char *lvalue,
1608                 int ltype,
1609                 const char *rvalue,
1610                 void *data,
1611                 void *userdata) {
1612
1613         Unit *u = data;
1614         Condition *c;
1615         bool trigger, negate;
1616         int b;
1617
1618         assert(filename);
1619         assert(lvalue);
1620         assert(rvalue);
1621         assert(data);
1622
1623         if ((trigger = rvalue[0] == '|'))
1624                 rvalue++;
1625
1626         if ((negate = rvalue[0] == '!'))
1627                 rvalue++;
1628
1629         if ((b = parse_boolean(rvalue)) < 0) {
1630                 log_error("[%s:%u] Failed to parse boolean value in condition, ignoring: %s", filename, line, rvalue);
1631                 return 0;
1632         }
1633
1634         if (!b)
1635                 negate = !negate;
1636
1637         if (!(c = condition_new(CONDITION_NULL, NULL, trigger, negate)))
1638                 return -ENOMEM;
1639
1640         LIST_PREPEND(Condition, conditions, u->meta.conditions, c);
1641         return 0;
1642 }
1643
1644 DEFINE_CONFIG_PARSE_ENUM(config_parse_notify_access, notify_access, NotifyAccess, "Failed to parse notify access specifier");
1645
1646 int config_parse_unit_cgroup_attr(
1647                 const char *filename,
1648                 unsigned line,
1649                 const char *section,
1650                 const char *lvalue,
1651                 int ltype,
1652                 const char *rvalue,
1653                 void *data,
1654                 void *userdata) {
1655
1656         Unit *u = data;
1657         char **l;
1658         int r;
1659
1660         assert(filename);
1661         assert(lvalue);
1662         assert(rvalue);
1663         assert(data);
1664
1665         l = strv_split_quoted(rvalue);
1666         if (!l)
1667                 return -ENOMEM;
1668
1669         if (strv_length(l) != 2) {
1670                 log_error("[%s:%u] Failed to parse cgroup attribute value, ignoring: %s", filename, line, rvalue);
1671                 strv_free(l);
1672                 return 0;
1673         }
1674
1675         r = unit_add_cgroup_attribute(u, NULL, l[0], l[1], NULL);
1676         strv_free(l);
1677
1678         if (r < 0) {
1679                 log_error("[%s:%u] Failed to add cgroup attribute value, ignoring: %s", filename, line, rvalue);
1680                 return 0;
1681         }
1682
1683         return 0;
1684 }
1685
1686 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) {
1687         Unit *u = data;
1688         int r;
1689         unsigned long ul;
1690         char *t;
1691
1692         assert(filename);
1693         assert(lvalue);
1694         assert(rvalue);
1695         assert(data);
1696
1697         if (safe_atolu(rvalue, &ul) < 0 || ul < 1) {
1698                 log_error("[%s:%u] Failed to parse CPU shares value, ignoring: %s", filename, line, rvalue);
1699                 return 0;
1700         }
1701
1702         if (asprintf(&t, "%lu", ul) < 0)
1703                 return -ENOMEM;
1704
1705         r = unit_add_cgroup_attribute(u, "cpu", "cpu.shares", t, NULL);
1706         free(t);
1707
1708         if (r < 0) {
1709                 log_error("[%s:%u] Failed to add cgroup attribute value, ignoring: %s", filename, line, rvalue);
1710                 return 0;
1711         }
1712
1713         return 0;
1714 }
1715
1716 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) {
1717         Unit *u = data;
1718         int r;
1719         off_t sz;
1720         char *t;
1721
1722         assert(filename);
1723         assert(lvalue);
1724         assert(rvalue);
1725         assert(data);
1726
1727         if (parse_bytes(rvalue, &sz) < 0 || sz <= 0) {
1728                 log_error("[%s:%u] Failed to parse memory limit value, ignoring: %s", filename, line, rvalue);
1729                 return 0;
1730         }
1731
1732         if (asprintf(&t, "%llu", (unsigned long long) sz) < 0)
1733                 return -ENOMEM;
1734
1735         r = unit_add_cgroup_attribute(u,
1736                                       "memory",
1737                                       streq(lvalue, "MemorySoftLimit") ? "memory.soft_limit_in_bytes" : "memory.limit_in_bytes",
1738                                       t, NULL);
1739         free(t);
1740
1741         if (r < 0) {
1742                 log_error("[%s:%u] Failed to add cgroup attribute value, ignoring: %s", filename, line, rvalue);
1743                 return 0;
1744         }
1745
1746         return 0;
1747 }
1748
1749 static int device_map(const char *controller, const char *name, const char *value, char **ret) {
1750         char **l;
1751
1752         assert(controller);
1753         assert(name);
1754         assert(value);
1755         assert(ret);
1756
1757         l = strv_split_quoted(value);
1758         if (!l)
1759                 return -ENOMEM;
1760
1761         assert(strv_length(l) >= 1);
1762
1763         if (streq(l[0], "*")) {
1764
1765                 if (asprintf(ret, "a *:*%s%s",
1766                              isempty(l[1]) ? "" : " ", strempty(l[1])) < 0) {
1767                         strv_free(l);
1768                         return -ENOMEM;
1769                 }
1770
1771         } else {
1772                 struct stat st;
1773
1774                 if (stat(l[0], &st) < 0) {
1775                         log_warning("Couldn't stat device %s", l[0]);
1776                         strv_free(l);
1777                         return -errno;
1778                 }
1779
1780                 if (!S_ISCHR(st.st_mode) && !S_ISBLK(st.st_mode)) {
1781                         log_warning("%s is not a device.", l[0]);
1782                         strv_free(l);
1783                         return -ENODEV;
1784                 }
1785
1786                 if (asprintf(ret, "%c %u:%u%s%s",
1787                              S_ISCHR(st.st_mode) ? 'c' : 'b',
1788                              major(st.st_rdev), minor(st.st_rdev),
1789                              isempty(l[1]) ? "" : " ", strempty(l[1])) < 0) {
1790
1791                         strv_free(l);
1792                         return -ENOMEM;
1793                 }
1794         }
1795
1796         strv_free(l);
1797         return 0;
1798 }
1799
1800 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) {
1801         Unit *u = data;
1802         char **l;
1803         int r;
1804         unsigned k;
1805
1806         assert(filename);
1807         assert(lvalue);
1808         assert(rvalue);
1809         assert(data);
1810
1811         l = strv_split_quoted(rvalue);
1812         if (!l)
1813                 return -ENOMEM;
1814
1815         k = strv_length(l);
1816         if (k < 1 || k > 2) {
1817                 log_error("[%s:%u] Failed to parse device value, ignoring: %s", filename, line, rvalue);
1818                 strv_free(l);
1819                 return 0;
1820         }
1821
1822         if (!streq(l[0], "*") && !path_startswith(l[0], "/dev")) {
1823                 log_error("[%s:%u] Device node path not absolute, ignoring: %s", filename, line, rvalue);
1824                 strv_free(l);
1825                 return 0;
1826         }
1827
1828         if (!isempty(l[1]) && !in_charset(l[1], "rwm")) {
1829                 log_error("[%s:%u] Device access string invalid, ignoring: %s", filename, line, rvalue);
1830                 strv_free(l);
1831                 return 0;
1832         }
1833         strv_free(l);
1834
1835         r = unit_add_cgroup_attribute(u, "devices",
1836                                       streq(lvalue, "DeviceAllow") ? "devices.allow" : "devices.deny",
1837                                       rvalue, device_map);
1838
1839         if (r < 0) {
1840                 log_error("[%s:%u] Failed to add cgroup attribute value, ignoring: %s", filename, line, rvalue);
1841                 return 0;
1842         }
1843
1844         return 0;
1845 }
1846
1847 static int blkio_map(const char *controller, const char *name, const char *value, char **ret) {
1848         struct stat st;
1849         char **l;
1850         dev_t d;
1851
1852         assert(controller);
1853         assert(name);
1854         assert(value);
1855         assert(ret);
1856
1857         l = strv_split_quoted(value);
1858         if (!l)
1859                 return -ENOMEM;
1860
1861         assert(strv_length(l) == 2);
1862
1863         if (stat(l[0], &st) < 0) {
1864                 log_warning("Couldn't stat device %s", l[0]);
1865                 strv_free(l);
1866                 return -errno;
1867         }
1868
1869         if (S_ISBLK(st.st_mode))
1870                 d = st.st_rdev;
1871         else if (major(st.st_dev) != 0) {
1872                 /* If this is not a device node then find the block
1873                  * device this file is stored on */
1874                 d = st.st_dev;
1875
1876                 /* If this is a partition, try to get the originating
1877                  * block device */
1878                 block_get_whole_disk(d, &d);
1879         } else {
1880                 log_warning("%s is not a block device and file system block device cannot be determined or is not local.", l[0]);
1881                 strv_free(l);
1882                 return -ENODEV;
1883         }
1884
1885         if (asprintf(ret, "%u:%u %s", major(d), minor(d), l[1]) < 0) {
1886                 strv_free(l);
1887                 return -ENOMEM;
1888         }
1889
1890         strv_free(l);
1891         return 0;
1892 }
1893
1894 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) {
1895         Unit *u = data;
1896         int r;
1897         unsigned long ul;
1898         const char *device = NULL, *weight;
1899         unsigned k;
1900         char *t, **l;
1901
1902         assert(filename);
1903         assert(lvalue);
1904         assert(rvalue);
1905         assert(data);
1906
1907         l = strv_split_quoted(rvalue);
1908         if (!l)
1909                 return -ENOMEM;
1910
1911         k = strv_length(l);
1912         if (k < 1 || k > 2) {
1913                 log_error("[%s:%u] Failed to parse weight value, ignoring: %s", filename, line, rvalue);
1914                 strv_free(l);
1915                 return 0;
1916         }
1917
1918         if (k == 1)
1919                 weight = l[0];
1920         else {
1921                 device = l[0];
1922                 weight = l[1];
1923         }
1924
1925         if (device && !path_is_absolute(device)) {
1926                 log_error("[%s:%u] Failed to parse block device node value, ignoring: %s", filename, line, rvalue);
1927                 strv_free(l);
1928                 return 0;
1929         }
1930
1931         if (safe_atolu(weight, &ul) < 0 || ul < 10 || ul > 1000) {
1932                 log_error("[%s:%u] Failed to parse block IO weight value, ignoring: %s", filename, line, rvalue);
1933                 strv_free(l);
1934                 return 0;
1935         }
1936
1937         if (device)
1938                 r = asprintf(&t, "%s %lu", device, ul);
1939         else
1940                 r = asprintf(&t, "%lu", ul);
1941         strv_free(l);
1942
1943         if (r < 0)
1944                 return -ENOMEM;
1945
1946         if (device)
1947                 r = unit_add_cgroup_attribute(u, "blkio", "blkio.weight_device", t, blkio_map);
1948         else
1949                 r = unit_add_cgroup_attribute(u, "blkio", "blkio.weight", t, NULL);
1950         free(t);
1951
1952         if (r < 0) {
1953                 log_error("[%s:%u] Failed to add cgroup attribute value, ignoring: %s", filename, line, rvalue);
1954                 return 0;
1955         }
1956
1957         return 0;
1958 }
1959
1960 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) {
1961         Unit *u = data;
1962         int r;
1963         off_t bytes;
1964         unsigned k;
1965         char *t, **l;
1966
1967         assert(filename);
1968         assert(lvalue);
1969         assert(rvalue);
1970         assert(data);
1971
1972         l = strv_split_quoted(rvalue);
1973         if (!l)
1974                 return -ENOMEM;
1975
1976         k = strv_length(l);
1977         if (k != 2) {
1978                 log_error("[%s:%u] Failed to parse bandwidth value, ignoring: %s", filename, line, rvalue);
1979                 strv_free(l);
1980                 return 0;
1981         }
1982
1983         if (!path_is_absolute(l[0])) {
1984                 log_error("[%s:%u] Failed to parse block device node value, ignoring: %s", filename, line, rvalue);
1985                 strv_free(l);
1986                 return 0;
1987         }
1988
1989         if (parse_bytes(l[1], &bytes) < 0 || bytes <= 0) {
1990                 log_error("[%s:%u] Failed to parse block IO bandwith value, ignoring: %s", filename, line, rvalue);
1991                 strv_free(l);
1992                 return 0;
1993         }
1994
1995         r = asprintf(&t, "%s %llu", l[0], (unsigned long long) bytes);
1996         strv_free(l);
1997
1998         if (r < 0)
1999                 return -ENOMEM;
2000
2001         r = unit_add_cgroup_attribute(u, "blkio",
2002                                       streq(lvalue, "BlockIOReadBandwidth") ? "blkio.read_bps_device" : "blkio.write_bps_device",
2003                                       t, blkio_map);
2004         free(t);
2005
2006         if (r < 0) {
2007                 log_error("[%s:%u] Failed to add cgroup attribute value, ignoring: %s", filename, line, rvalue);
2008                 return 0;
2009         }
2010
2011         return 0;
2012 }
2013
2014
2015 #define FOLLOW_MAX 8
2016
2017 static int open_follow(char **filename, FILE **_f, Set *names, char **_final) {
2018         unsigned c = 0;
2019         int fd, r;
2020         FILE *f;
2021         char *id = NULL;
2022
2023         assert(filename);
2024         assert(*filename);
2025         assert(_f);
2026         assert(names);
2027
2028         /* This will update the filename pointer if the loaded file is
2029          * reached by a symlink. The old string will be freed. */
2030
2031         for (;;) {
2032                 char *target, *name;
2033
2034                 if (c++ >= FOLLOW_MAX)
2035                         return -ELOOP;
2036
2037                 path_kill_slashes(*filename);
2038
2039                 /* Add the file name we are currently looking at to
2040                  * the names of this unit, but only if it is a valid
2041                  * unit name. */
2042                 name = file_name_from_path(*filename);
2043
2044                 if (unit_name_is_valid(name, true)) {
2045
2046                         id = set_get(names, name);
2047                         if (!id) {
2048                                 id = strdup(name);
2049                                 if (!id)
2050                                         return -ENOMEM;
2051
2052                                 r = set_put(names, id);
2053                                 if (r < 0) {
2054                                         free(id);
2055                                         return r;
2056                                 }
2057                         }
2058                 }
2059
2060                 /* Try to open the file name, but don't if its a symlink */
2061                 if ((fd = open(*filename, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW)) >= 0)
2062                         break;
2063
2064                 if (errno != ELOOP)
2065                         return -errno;
2066
2067                 /* Hmm, so this is a symlink. Let's read the name, and follow it manually */
2068                 if ((r = readlink_and_make_absolute(*filename, &target)) < 0)
2069                         return r;
2070
2071                 free(*filename);
2072                 *filename = target;
2073         }
2074
2075         if (!(f = fdopen(fd, "re"))) {
2076                 r = -errno;
2077                 close_nointr_nofail(fd);
2078                 return r;
2079         }
2080
2081         *_f = f;
2082         *_final = id;
2083         return 0;
2084 }
2085
2086 static int merge_by_names(Unit **u, Set *names, const char *id) {
2087         char *k;
2088         int r;
2089
2090         assert(u);
2091         assert(*u);
2092         assert(names);
2093
2094         /* Let's try to add in all symlink names we found */
2095         while ((k = set_steal_first(names))) {
2096
2097                 /* First try to merge in the other name into our
2098                  * unit */
2099                 if ((r = unit_merge_by_name(*u, k)) < 0) {
2100                         Unit *other;
2101
2102                         /* Hmm, we couldn't merge the other unit into
2103                          * ours? Then let's try it the other way
2104                          * round */
2105
2106                         other = manager_get_unit((*u)->meta.manager, k);
2107                         free(k);
2108
2109                         if (other)
2110                                 if ((r = unit_merge(other, *u)) >= 0) {
2111                                         *u = other;
2112                                         return merge_by_names(u, names, NULL);
2113                                 }
2114
2115                         return r;
2116                 }
2117
2118                 if (id == k)
2119                         unit_choose_id(*u, id);
2120
2121                 free(k);
2122         }
2123
2124         return 0;
2125 }
2126
2127 static int load_from_path(Unit *u, const char *path) {
2128         int r;
2129         Set *symlink_names;
2130         FILE *f = NULL;
2131         char *filename = NULL, *id = NULL;
2132         Unit *merged;
2133         struct stat st;
2134
2135         assert(u);
2136         assert(path);
2137
2138         symlink_names = set_new(string_hash_func, string_compare_func);
2139         if (!symlink_names)
2140                 return -ENOMEM;
2141
2142         if (path_is_absolute(path)) {
2143
2144                 if (!(filename = strdup(path))) {
2145                         r = -ENOMEM;
2146                         goto finish;
2147                 }
2148
2149                 if ((r = open_follow(&filename, &f, symlink_names, &id)) < 0) {
2150                         free(filename);
2151                         filename = NULL;
2152
2153                         if (r != -ENOENT)
2154                                 goto finish;
2155                 }
2156
2157         } else  {
2158                 char **p;
2159
2160                 STRV_FOREACH(p, u->meta.manager->lookup_paths.unit_path) {
2161
2162                         /* Instead of opening the path right away, we manually
2163                          * follow all symlinks and add their name to our unit
2164                          * name set while doing so */
2165                         if (!(filename = path_make_absolute(path, *p))) {
2166                                 r = -ENOMEM;
2167                                 goto finish;
2168                         }
2169
2170                         if (u->meta.manager->unit_path_cache &&
2171                             !set_get(u->meta.manager->unit_path_cache, filename))
2172                                 r = -ENOENT;
2173                         else
2174                                 r = open_follow(&filename, &f, symlink_names, &id);
2175
2176                         if (r < 0) {
2177                                 char *sn;
2178
2179                                 free(filename);
2180                                 filename = NULL;
2181
2182                                 if (r != -ENOENT)
2183                                         goto finish;
2184
2185                                 /* Empty the symlink names for the next run */
2186                                 while ((sn = set_steal_first(symlink_names)))
2187                                         free(sn);
2188
2189                                 continue;
2190                         }
2191
2192                         break;
2193                 }
2194         }
2195
2196         if (!filename) {
2197                 /* Hmm, no suitable file found? */
2198                 r = 0;
2199                 goto finish;
2200         }
2201
2202         merged = u;
2203         if ((r = merge_by_names(&merged, symlink_names, id)) < 0)
2204                 goto finish;
2205
2206         if (merged != u) {
2207                 u->meta.load_state = UNIT_MERGED;
2208                 r = 0;
2209                 goto finish;
2210         }
2211
2212         zero(st);
2213         if (fstat(fileno(f), &st) < 0) {
2214                 r = -errno;
2215                 goto finish;
2216         }
2217
2218         if (null_or_empty(&st))
2219                 u->meta.load_state = UNIT_MASKED;
2220         else {
2221                 /* Now, parse the file contents */
2222                 r = config_parse(filename, f, UNIT_VTABLE(u)->sections, config_item_perf_lookup, (void*) load_fragment_gperf_lookup, false, u);
2223                 if (r < 0)
2224                         goto finish;
2225
2226                 u->meta.load_state = UNIT_LOADED;
2227         }
2228
2229         free(u->meta.fragment_path);
2230         u->meta.fragment_path = filename;
2231         filename = NULL;
2232
2233         u->meta.fragment_mtime = timespec_load(&st.st_mtim);
2234
2235         r = 0;
2236
2237 finish:
2238         set_free_free(symlink_names);
2239         free(filename);
2240
2241         if (f)
2242                 fclose(f);
2243
2244         return r;
2245 }
2246
2247 int unit_load_fragment(Unit *u) {
2248         int r;
2249         Iterator i;
2250         const char *t;
2251
2252         assert(u);
2253         assert(u->meta.load_state == UNIT_STUB);
2254         assert(u->meta.id);
2255
2256         /* First, try to find the unit under its id. We always look
2257          * for unit files in the default directories, to make it easy
2258          * to override things by placing things in /etc/systemd/system */
2259         if ((r = load_from_path(u, u->meta.id)) < 0)
2260                 return r;
2261
2262         /* Try to find an alias we can load this with */
2263         if (u->meta.load_state == UNIT_STUB)
2264                 SET_FOREACH(t, u->meta.names, i) {
2265
2266                         if (t == u->meta.id)
2267                                 continue;
2268
2269                         if ((r = load_from_path(u, t)) < 0)
2270                                 return r;
2271
2272                         if (u->meta.load_state != UNIT_STUB)
2273                                 break;
2274                 }
2275
2276         /* And now, try looking for it under the suggested (originally linked) path */
2277         if (u->meta.load_state == UNIT_STUB && u->meta.fragment_path) {
2278
2279                 if ((r = load_from_path(u, u->meta.fragment_path)) < 0)
2280                         return r;
2281
2282                 if (u->meta.load_state == UNIT_STUB) {
2283                         /* Hmm, this didn't work? Then let's get rid
2284                          * of the fragment path stored for us, so that
2285                          * we don't point to an invalid location. */
2286                         free(u->meta.fragment_path);
2287                         u->meta.fragment_path = NULL;
2288                 }
2289         }
2290
2291         /* Look for a template */
2292         if (u->meta.load_state == UNIT_STUB && u->meta.instance) {
2293                 char *k;
2294
2295                 if (!(k = unit_name_template(u->meta.id)))
2296                         return -ENOMEM;
2297
2298                 r = load_from_path(u, k);
2299                 free(k);
2300
2301                 if (r < 0)
2302                         return r;
2303
2304                 if (u->meta.load_state == UNIT_STUB)
2305                         SET_FOREACH(t, u->meta.names, i) {
2306
2307                                 if (t == u->meta.id)
2308                                         continue;
2309
2310                                 if (!(k = unit_name_template(t)))
2311                                         return -ENOMEM;
2312
2313                                 r = load_from_path(u, k);
2314                                 free(k);
2315
2316                                 if (r < 0)
2317                                         return r;
2318
2319                                 if (u->meta.load_state != UNIT_STUB)
2320                                         break;
2321                         }
2322         }
2323
2324         return 0;
2325 }
2326
2327 void unit_dump_config_items(FILE *f) {
2328         static const struct {
2329                 const ConfigParserCallback callback;
2330                 const char *rvalue;
2331         } table[] = {
2332                 { config_parse_int,                   "INTEGER" },
2333                 { config_parse_unsigned,              "UNSIGNED" },
2334                 { config_parse_size,                  "SIZE" },
2335                 { config_parse_bool,                  "BOOLEAN" },
2336                 { config_parse_string,                "STRING" },
2337                 { config_parse_path,                  "PATH" },
2338                 { config_parse_unit_path_printf,      "PATH" },
2339                 { config_parse_strv,                  "STRING [...]" },
2340                 { config_parse_exec_nice,             "NICE" },
2341                 { config_parse_exec_oom_score_adjust, "OOMSCOREADJUST" },
2342                 { config_parse_exec_io_class,         "IOCLASS" },
2343                 { config_parse_exec_io_priority,      "IOPRIORITY" },
2344                 { config_parse_exec_cpu_sched_policy, "CPUSCHEDPOLICY" },
2345                 { config_parse_exec_cpu_sched_prio,   "CPUSCHEDPRIO" },
2346                 { config_parse_exec_cpu_affinity,     "CPUAFFINITY" },
2347                 { config_parse_mode,                  "MODE" },
2348                 { config_parse_unit_env_file,         "FILE" },
2349                 { config_parse_output,                "OUTPUT" },
2350                 { config_parse_input,                 "INPUT" },
2351                 { config_parse_facility,              "FACILITY" },
2352                 { config_parse_level,                 "LEVEL" },
2353                 { config_parse_exec_capabilities,     "CAPABILITIES" },
2354                 { config_parse_exec_secure_bits,      "SECUREBITS" },
2355                 { config_parse_exec_bounding_set,     "BOUNDINGSET" },
2356                 { config_parse_exec_timer_slack_nsec, "TIMERSLACK" },
2357                 { config_parse_limit,                 "LIMIT" },
2358                 { config_parse_unit_cgroup,           "CGROUP [...]" },
2359                 { config_parse_unit_deps,             "UNIT [...]" },
2360                 { config_parse_unit_names,            "UNIT [...]" },
2361                 { config_parse_exec,                  "PATH [ARGUMENT [...]]" },
2362                 { config_parse_service_type,          "SERVICETYPE" },
2363                 { config_parse_service_restart,       "SERVICERESTART" },
2364 #ifdef HAVE_SYSV_COMPAT
2365                 { config_parse_sysv_priority,         "SYSVPRIORITY" },
2366 #else
2367                 { config_parse_warn_compat,           "NOTSUPPORTED" },
2368 #endif
2369                 { config_parse_kill_mode,             "KILLMODE" },
2370                 { config_parse_kill_signal,           "SIGNAL" },
2371                 { config_parse_socket_listen,         "SOCKET [...]" },
2372                 { config_parse_socket_bind,           "SOCKETBIND" },
2373                 { config_parse_socket_bindtodevice,   "NETWORKINTERFACE" },
2374                 { config_parse_usec,                  "SECONDS" },
2375                 { config_parse_path_strv,             "PATH [...]" },
2376                 { config_parse_exec_mount_flags,      "MOUNTFLAG [...]" },
2377                 { config_parse_unit_string_printf,    "STRING" },
2378                 { config_parse_timer,                 "TIMER" },
2379                 { config_parse_timer_unit,            "NAME" },
2380                 { config_parse_path_spec,             "PATH" },
2381                 { config_parse_path_unit,             "UNIT" },
2382                 { config_parse_notify_access,         "ACCESS" },
2383                 { config_parse_ip_tos,                "TOS" },
2384                 { config_parse_unit_condition_path,   "CONDITION" },
2385                 { config_parse_unit_condition_string, "CONDITION" },
2386                 { config_parse_unit_condition_null,   "CONDITION" },
2387         };
2388
2389         const char *prev = NULL;
2390         const char *i;
2391
2392         assert(f);
2393
2394         NULSTR_FOREACH(i, load_fragment_gperf_nulstr) {
2395                 const char *rvalue = "OTHER", *lvalue;
2396                 unsigned j;
2397                 size_t prefix_len;
2398                 const char *dot;
2399                 const ConfigPerfItem *p;
2400
2401                 assert_se(p = load_fragment_gperf_lookup(i, strlen(i)));
2402
2403                 dot = strchr(i, '.');
2404                 lvalue = dot ? dot + 1 : i;
2405                 prefix_len = dot-i;
2406
2407                 if (dot)
2408                         if (!prev || strncmp(prev, i, prefix_len+1) != 0) {
2409                                 if (prev)
2410                                         fputc('\n', f);
2411
2412                                 fprintf(f, "[%.*s]\n", (int) prefix_len, i);
2413                         }
2414
2415                 for (j = 0; j < ELEMENTSOF(table); j++)
2416                         if (p->parse == table[j].callback) {
2417                                 rvalue = table[j].rvalue;
2418                                 break;
2419                         }
2420
2421                 fprintf(f, "%s=%s\n", lvalue, rvalue);
2422                 prev = i;
2423         }
2424 }