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