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