]> git.ipfire.org Git - people/ms/systemd.git/blob - load-fragment.c
socket: fix bitfields in fd watches
[people/ms/systemd.git] / 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
32 #include "unit.h"
33 #include "strv.h"
34 #include "conf-parser.h"
35 #include "load-fragment.h"
36 #include "log.h"
37 #include "ioprio.h"
38 #include "securebits.h"
39 #include "missing.h"
40 #include "unit-name.h"
41
42 #define DEFINE_CONFIG_PARSE_ENUM(function,name,type,msg) \
43 static int function( \
44 const char *filename, \
45 unsigned line, \
46 const char *section, \
47 const char *lvalue, \
48 const char *rvalue, \
49 void *data, \
50 void *userdata) { \
51 \
52 type *i = data, x; \
53 \
54 assert(filename); \
55 assert(lvalue); \
56 assert(rvalue); \
57 assert(data); \
58 \
59 if ((x = name##_from_string(rvalue)) < 0) { \
60 log_error("[%s:%u] " msg ": %s", filename, line, rvalue); \
61 return -EBADMSG; \
62 } \
63 \
64 *i = x; \
65 \
66 return 0; \
67 }
68
69 static int config_parse_deps(
70 const char *filename,
71 unsigned line,
72 const char *section,
73 const char *lvalue,
74 const char *rvalue,
75 void *data,
76 void *userdata) {
77
78 UnitDependency d = PTR_TO_UINT(data);
79 Unit *u = userdata;
80 char *w;
81 size_t l;
82 char *state;
83
84 assert(filename);
85 assert(lvalue);
86 assert(rvalue);
87
88 FOREACH_WORD(w, l, rvalue, state) {
89 char *t, *k;
90 int r;
91
92 if (!(t = strndup(w, l)))
93 return -ENOMEM;
94
95 k = unit_name_printf(u, t);
96 free(t);
97
98 if (!k)
99 return -ENOMEM;
100
101 r = unit_add_dependency_by_name(u, d, k, NULL, true);
102 free(k);
103
104 if (r < 0)
105 return r;
106 }
107
108 return 0;
109 }
110
111 static int config_parse_names(
112 const char *filename,
113 unsigned line,
114 const char *section,
115 const char *lvalue,
116 const char *rvalue,
117 void *data,
118 void *userdata) {
119
120 Unit *u = userdata;
121 char *w;
122 size_t l;
123 char *state;
124
125 assert(filename);
126 assert(lvalue);
127 assert(rvalue);
128 assert(data);
129
130 FOREACH_WORD(w, l, rvalue, state) {
131 char *t, *k;
132 int r;
133
134 if (!(t = strndup(w, l)))
135 return -ENOMEM;
136
137 k = unit_name_printf(u, t);
138 free(t);
139
140 if (!k)
141 return -ENOMEM;
142
143 r = unit_merge_by_name(u, k);
144 free(k);
145
146 if (r < 0)
147 return r;
148 }
149
150 return 0;
151 }
152
153 static int config_parse_listen(
154 const char *filename,
155 unsigned line,
156 const char *section,
157 const char *lvalue,
158 const char *rvalue,
159 void *data,
160 void *userdata) {
161
162 int r;
163 SocketPort *p;
164 Socket *s;
165
166 assert(filename);
167 assert(lvalue);
168 assert(rvalue);
169 assert(data);
170
171 s = (Socket*) data;
172
173 if (!(p = new0(SocketPort, 1)))
174 return -ENOMEM;
175
176 if (streq(lvalue, "ListenFIFO")) {
177 p->type = SOCKET_FIFO;
178
179 if (!(p->path = strdup(rvalue))) {
180 free(p);
181 return -ENOMEM;
182 }
183 } else {
184 p->type = SOCKET_SOCKET;
185
186 if ((r = socket_address_parse(&p->address, rvalue)) < 0) {
187 log_error("[%s:%u] Failed to parse address value: %s", filename, line, rvalue);
188 free(p);
189 return r;
190 }
191
192 if (streq(lvalue, "ListenStream"))
193 p->address.type = SOCK_STREAM;
194 else if (streq(lvalue, "ListenDatagram"))
195 p->address.type = SOCK_DGRAM;
196 else {
197 assert(streq(lvalue, "ListenSequentialPacket"));
198 p->address.type = SOCK_SEQPACKET;
199 }
200
201 if (socket_address_family(&p->address) != AF_LOCAL && p->address.type == SOCK_SEQPACKET) {
202 free(p);
203 return -EPROTONOSUPPORT;
204 }
205 }
206
207 p->fd = -1;
208 LIST_PREPEND(SocketPort, port, s->ports, p);
209
210 return 0;
211 }
212
213 static int config_parse_socket_bind(
214 const char *filename,
215 unsigned line,
216 const char *section,
217 const char *lvalue,
218 const char *rvalue,
219 void *data,
220 void *userdata) {
221
222 int r;
223 Socket *s;
224
225 assert(filename);
226 assert(lvalue);
227 assert(rvalue);
228 assert(data);
229
230 s = (Socket*) data;
231
232 if ((r = parse_boolean(rvalue)) < 0) {
233 log_error("[%s:%u] Failed to parse bind IPv6 only value: %s", filename, line, rvalue);
234 return r;
235 }
236
237 s->bind_ipv6_only = r ? SOCKET_ADDRESS_IPV6_ONLY : SOCKET_ADDRESS_BOTH;
238
239 return 0;
240 }
241
242 static int config_parse_nice(
243 const char *filename,
244 unsigned line,
245 const char *section,
246 const char *lvalue,
247 const char *rvalue,
248 void *data,
249 void *userdata) {
250
251 ExecContext *c = data;
252 int priority, r;
253
254 assert(filename);
255 assert(lvalue);
256 assert(rvalue);
257 assert(data);
258
259 if ((r = safe_atoi(rvalue, &priority)) < 0) {
260 log_error("[%s:%u] Failed to parse nice priority: %s", filename, line, rvalue);
261 return r;
262 }
263
264 if (priority < PRIO_MIN || priority >= PRIO_MAX) {
265 log_error("[%s:%u] Nice priority out of range: %s", filename, line, rvalue);
266 return -ERANGE;
267 }
268
269 c->nice = priority;
270 c->nice_set = false;
271
272 return 0;
273 }
274
275 static int config_parse_oom_adjust(
276 const char *filename,
277 unsigned line,
278 const char *section,
279 const char *lvalue,
280 const char *rvalue,
281 void *data,
282 void *userdata) {
283
284 ExecContext *c = data;
285 int oa, r;
286
287 assert(filename);
288 assert(lvalue);
289 assert(rvalue);
290 assert(data);
291
292 if ((r = safe_atoi(rvalue, &oa)) < 0) {
293 log_error("[%s:%u] Failed to parse OOM adjust value: %s", filename, line, rvalue);
294 return r;
295 }
296
297 if (oa < OOM_DISABLE || oa > OOM_ADJUST_MAX) {
298 log_error("[%s:%u] OOM adjust value out of range: %s", filename, line, rvalue);
299 return -ERANGE;
300 }
301
302 c->oom_adjust = oa;
303 c->oom_adjust_set = true;
304
305 return 0;
306 }
307
308 static int config_parse_mode(
309 const char *filename,
310 unsigned line,
311 const char *section,
312 const char *lvalue,
313 const char *rvalue,
314 void *data,
315 void *userdata) {
316
317 mode_t *m = data;
318 long l;
319 char *x = NULL;
320
321 assert(filename);
322 assert(lvalue);
323 assert(rvalue);
324 assert(data);
325
326 errno = 0;
327 l = strtol(rvalue, &x, 8);
328 if (!x || *x || errno) {
329 log_error("[%s:%u] Failed to parse mode value: %s", filename, line, rvalue);
330 return errno ? -errno : -EINVAL;
331 }
332
333 if (l < 0000 || l > 07777) {
334 log_error("[%s:%u] mode value out of range: %s", filename, line, rvalue);
335 return -ERANGE;
336 }
337
338 *m = (mode_t) l;
339 return 0;
340 }
341
342 static int config_parse_exec(
343 const char *filename,
344 unsigned line,
345 const char *section,
346 const char *lvalue,
347 const char *rvalue,
348 void *data,
349 void *userdata) {
350
351 ExecCommand **e = data, *nce = NULL;
352 char **n;
353 char *w;
354 unsigned k;
355 size_t l;
356 char *state;
357
358 assert(filename);
359 assert(lvalue);
360 assert(rvalue);
361 assert(data);
362
363 k = 0;
364 FOREACH_WORD_QUOTED(w, l, rvalue, state)
365 k++;
366
367 if (!(n = new(char*, k+1)))
368 return -ENOMEM;
369
370 k = 0;
371 FOREACH_WORD_QUOTED(w, l, rvalue, state)
372 if (!(n[k++] = strndup(w, l)))
373 goto fail;
374
375 n[k] = NULL;
376
377 if (!n[0] || !path_is_absolute(n[0])) {
378 log_error("[%s:%u] Invalid executable path in command line: %s", filename, line, rvalue);
379 strv_free(n);
380 return -EINVAL;
381 }
382
383 if (!(nce = new0(ExecCommand, 1)))
384 goto fail;
385
386 nce->argv = n;
387 if (!(nce->path = strdup(n[0])))
388 goto fail;
389
390 exec_command_append_list(e, nce);
391
392 return 0;
393
394 fail:
395 for (; k > 0; k--)
396 free(n[k-1]);
397 free(n);
398
399 free(nce);
400
401 return -ENOMEM;
402 }
403
404 static int config_parse_usec(
405 const char *filename,
406 unsigned line,
407 const char *section,
408 const char *lvalue,
409 const char *rvalue,
410 void *data,
411 void *userdata) {
412
413 usec_t *usec = data;
414 int r;
415
416 assert(filename);
417 assert(lvalue);
418 assert(rvalue);
419 assert(data);
420
421 if ((r = parse_usec(rvalue, usec)) < 0) {
422 log_error("[%s:%u] Failed to parse time value: %s", filename, line, rvalue);
423 return r;
424 }
425
426 return 0;
427 }
428
429 DEFINE_CONFIG_PARSE_ENUM(config_parse_service_type, service_type, ServiceType, "Failed to parse service type");
430 DEFINE_CONFIG_PARSE_ENUM(config_parse_service_restart, service_restart, ServiceRestart, "Failed to parse service restart specifier");
431
432 static int config_parse_bindtodevice(
433 const char *filename,
434 unsigned line,
435 const char *section,
436 const char *lvalue,
437 const char *rvalue,
438 void *data,
439 void *userdata) {
440
441 Socket *s = data;
442 char *n;
443
444 assert(filename);
445 assert(lvalue);
446 assert(rvalue);
447 assert(data);
448
449 if (rvalue[0] && !streq(rvalue, "*")) {
450 if (!(n = strdup(rvalue)))
451 return -ENOMEM;
452 } else
453 n = NULL;
454
455 free(s->bind_to_device);
456 s->bind_to_device = n;
457
458 return 0;
459 }
460
461 DEFINE_CONFIG_PARSE_ENUM(config_parse_output, exec_output, ExecOutput, "Failed to parse output specifier");
462 DEFINE_CONFIG_PARSE_ENUM(config_parse_input, exec_input, ExecInput, "Failed to parse input specifier");
463
464 static int config_parse_facility(
465 const char *filename,
466 unsigned line,
467 const char *section,
468 const char *lvalue,
469 const char *rvalue,
470 void *data,
471 void *userdata) {
472
473
474 int *o = data, x;
475
476 assert(filename);
477 assert(lvalue);
478 assert(rvalue);
479 assert(data);
480
481 if ((x = log_facility_from_string(rvalue)) < 0) {
482 log_error("[%s:%u] Failed to parse log facility: %s", filename, line, rvalue);
483 return -EBADMSG;
484 }
485
486 *o = LOG_MAKEPRI(x, LOG_PRI(*o));
487
488 return 0;
489 }
490
491 static int config_parse_level(
492 const char *filename,
493 unsigned line,
494 const char *section,
495 const char *lvalue,
496 const char *rvalue,
497 void *data,
498 void *userdata) {
499
500
501 int *o = data, x;
502
503 assert(filename);
504 assert(lvalue);
505 assert(rvalue);
506 assert(data);
507
508 if ((x = log_level_from_string(rvalue)) < 0) {
509 log_error("[%s:%u] Failed to parse log level: %s", filename, line, rvalue);
510 return -EBADMSG;
511 }
512
513 *o = LOG_MAKEPRI(LOG_FAC(*o), x);
514 return 0;
515 }
516
517 static int config_parse_io_class(
518 const char *filename,
519 unsigned line,
520 const char *section,
521 const char *lvalue,
522 const char *rvalue,
523 void *data,
524 void *userdata) {
525
526 ExecContext *c = data;
527 int x;
528
529 assert(filename);
530 assert(lvalue);
531 assert(rvalue);
532 assert(data);
533
534 if ((x = ioprio_class_from_string(rvalue)) < 0) {
535 log_error("[%s:%u] Failed to parse IO scheduling class: %s", filename, line, rvalue);
536 return -EBADMSG;
537 }
538
539 c->ioprio = IOPRIO_PRIO_VALUE(x, IOPRIO_PRIO_DATA(c->ioprio));
540 c->ioprio_set = true;
541
542 return 0;
543 }
544
545 static int config_parse_io_priority(
546 const char *filename,
547 unsigned line,
548 const char *section,
549 const char *lvalue,
550 const char *rvalue,
551 void *data,
552 void *userdata) {
553
554 ExecContext *c = data;
555 int i;
556
557 assert(filename);
558 assert(lvalue);
559 assert(rvalue);
560 assert(data);
561
562 if (safe_atoi(rvalue, &i) < 0 || i < 0 || i >= IOPRIO_BE_NR) {
563 log_error("[%s:%u] Failed to parse io priority: %s", filename, line, rvalue);
564 return -EBADMSG;
565 }
566
567 c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_PRIO_CLASS(c->ioprio), i);
568 c->ioprio_set = true;
569
570 return 0;
571 }
572
573 static int config_parse_cpu_sched_policy(
574 const char *filename,
575 unsigned line,
576 const char *section,
577 const char *lvalue,
578 const char *rvalue,
579 void *data,
580 void *userdata) {
581
582
583 ExecContext *c = data;
584 int x;
585
586 assert(filename);
587 assert(lvalue);
588 assert(rvalue);
589 assert(data);
590
591 if ((x = sched_policy_from_string(rvalue)) < 0) {
592 log_error("[%s:%u] Failed to parse CPU scheduling policy: %s", filename, line, rvalue);
593 return -EBADMSG;
594 }
595
596 c->cpu_sched_policy = x;
597 c->cpu_sched_set = true;
598
599 return 0;
600 }
601
602 static int config_parse_cpu_sched_prio(
603 const char *filename,
604 unsigned line,
605 const char *section,
606 const char *lvalue,
607 const char *rvalue,
608 void *data,
609 void *userdata) {
610
611 ExecContext *c = data;
612 int i;
613
614 assert(filename);
615 assert(lvalue);
616 assert(rvalue);
617 assert(data);
618
619 /* On Linux RR/FIFO have the same range */
620 if (safe_atoi(rvalue, &i) < 0 || i < sched_get_priority_min(SCHED_RR) || i > sched_get_priority_max(SCHED_RR)) {
621 log_error("[%s:%u] Failed to parse CPU scheduling priority: %s", filename, line, rvalue);
622 return -EBADMSG;
623 }
624
625 c->cpu_sched_priority = i;
626 c->cpu_sched_set = true;
627
628 return 0;
629 }
630
631 static int config_parse_cpu_affinity(
632 const char *filename,
633 unsigned line,
634 const char *section,
635 const char *lvalue,
636 const char *rvalue,
637 void *data,
638 void *userdata) {
639
640 ExecContext *c = data;
641 char *w;
642 size_t l;
643 char *state;
644
645 assert(filename);
646 assert(lvalue);
647 assert(rvalue);
648 assert(data);
649
650 FOREACH_WORD(w, l, rvalue, state) {
651 char *t;
652 int r;
653 unsigned cpu;
654
655 if (!(t = strndup(w, l)))
656 return -ENOMEM;
657
658 r = safe_atou(t, &cpu);
659 free(t);
660
661 if (r < 0 || cpu >= CPU_SETSIZE) {
662 log_error("[%s:%u] Failed to parse CPU affinity: %s", filename, line, rvalue);
663 return -EBADMSG;
664 }
665
666 CPU_SET(cpu, &c->cpu_affinity);
667 }
668
669 c->cpu_affinity_set = true;
670
671 return 0;
672 }
673
674 static int config_parse_capabilities(
675 const char *filename,
676 unsigned line,
677 const char *section,
678 const char *lvalue,
679 const char *rvalue,
680 void *data,
681 void *userdata) {
682
683 ExecContext *c = data;
684 cap_t cap;
685
686 assert(filename);
687 assert(lvalue);
688 assert(rvalue);
689 assert(data);
690
691 if (!(cap = cap_from_text(rvalue))) {
692 if (errno == ENOMEM)
693 return -ENOMEM;
694
695 log_error("[%s:%u] Failed to parse capabilities: %s", filename, line, rvalue);
696 return -EBADMSG;
697 }
698
699 if (c->capabilities)
700 cap_free(c->capabilities);
701 c->capabilities = cap;
702
703 return 0;
704 }
705
706 static int config_parse_secure_bits(
707 const char *filename,
708 unsigned line,
709 const char *section,
710 const char *lvalue,
711 const char *rvalue,
712 void *data,
713 void *userdata) {
714
715 ExecContext *c = data;
716 char *w;
717 size_t l;
718 char *state;
719
720 assert(filename);
721 assert(lvalue);
722 assert(rvalue);
723 assert(data);
724
725 FOREACH_WORD(w, l, rvalue, state) {
726 if (first_word(w, "keep-caps"))
727 c->secure_bits |= SECURE_KEEP_CAPS;
728 else if (first_word(w, "keep-caps-locked"))
729 c->secure_bits |= SECURE_KEEP_CAPS_LOCKED;
730 else if (first_word(w, "no-setuid-fixup"))
731 c->secure_bits |= SECURE_NO_SETUID_FIXUP;
732 else if (first_word(w, "no-setuid-fixup-locked"))
733 c->secure_bits |= SECURE_NO_SETUID_FIXUP_LOCKED;
734 else if (first_word(w, "noroot"))
735 c->secure_bits |= SECURE_NOROOT;
736 else if (first_word(w, "noroot-locked"))
737 c->secure_bits |= SECURE_NOROOT_LOCKED;
738 else {
739 log_error("[%s:%u] Failed to parse secure bits: %s", filename, line, rvalue);
740 return -EBADMSG;
741 }
742 }
743
744 return 0;
745 }
746
747 static int config_parse_bounding_set(
748 const char *filename,
749 unsigned line,
750 const char *section,
751 const char *lvalue,
752 const char *rvalue,
753 void *data,
754 void *userdata) {
755
756 ExecContext *c = data;
757 char *w;
758 size_t l;
759 char *state;
760
761 assert(filename);
762 assert(lvalue);
763 assert(rvalue);
764 assert(data);
765
766 FOREACH_WORD(w, l, rvalue, state) {
767 char *t;
768 int r;
769 cap_value_t cap;
770
771 if (!(t = strndup(w, l)))
772 return -ENOMEM;
773
774 r = cap_from_name(t, &cap);
775 free(t);
776
777 if (r < 0) {
778 log_error("[%s:%u] Failed to parse capability bounding set: %s", filename, line, rvalue);
779 return -EBADMSG;
780 }
781
782 c->capability_bounding_set_drop |= 1 << cap;
783 }
784
785 return 0;
786 }
787
788 static int config_parse_timer_slack_ns(
789 const char *filename,
790 unsigned line,
791 const char *section,
792 const char *lvalue,
793 const char *rvalue,
794 void *data,
795 void *userdata) {
796
797 ExecContext *c = data;
798 unsigned long u;
799 int r;
800
801 assert(filename);
802 assert(lvalue);
803 assert(rvalue);
804 assert(data);
805
806 if ((r = safe_atolu(rvalue, &u)) < 0) {
807 log_error("[%s:%u] Failed to parse time slack value: %s", filename, line, rvalue);
808 return r;
809 }
810
811 c->timer_slack_ns = u;
812
813 return 0;
814 }
815
816 static int config_parse_limit(
817 const char *filename,
818 unsigned line,
819 const char *section,
820 const char *lvalue,
821 const char *rvalue,
822 void *data,
823 void *userdata) {
824
825 struct rlimit **rl = data;
826 unsigned long long u;
827 int r;
828
829 assert(filename);
830 assert(lvalue);
831 assert(rvalue);
832 assert(data);
833
834 if ((r = safe_atollu(rvalue, &u)) < 0) {
835 log_error("[%s:%u] Failed to parse resource value: %s", filename, line, rvalue);
836 return r;
837 }
838
839 if (!*rl)
840 if (!(*rl = new(struct rlimit, 1)))
841 return -ENOMEM;
842
843 (*rl)->rlim_cur = (*rl)->rlim_max = (rlim_t) u;
844 return 0;
845 }
846
847 static int config_parse_cgroup(
848 const char *filename,
849 unsigned line,
850 const char *section,
851 const char *lvalue,
852 const char *rvalue,
853 void *data,
854 void *userdata) {
855
856 Unit *u = userdata;
857 char *w;
858 size_t l;
859 char *state;
860
861 FOREACH_WORD(w, l, rvalue, state) {
862 char *t;
863 int r;
864
865 if (!(t = strndup(w, l)))
866 return -ENOMEM;
867
868 r = unit_add_cgroup_from_text(u, t);
869 free(t);
870
871 if (r < 0)
872 return r;
873 }
874
875 return 0;
876 }
877
878 static int config_parse_sysv_priority(
879 const char *filename,
880 unsigned line,
881 const char *section,
882 const char *lvalue,
883 const char *rvalue,
884 void *data,
885 void *userdata) {
886
887 int *priority = data;
888 int r, i;
889
890 assert(filename);
891 assert(lvalue);
892 assert(rvalue);
893 assert(data);
894
895 if ((r = safe_atoi(rvalue, &i)) < 0 || i < 0) {
896 log_error("[%s:%u] Failed to parse SysV start priority: %s", filename, line, rvalue);
897 return r;
898 }
899
900 *priority = (int) i;
901 return 0;
902 }
903
904 DEFINE_CONFIG_PARSE_ENUM(config_parse_kill_mode, kill_mode, KillMode, "Failed to parse kill mode");
905
906 static int config_parse_mount_flags(
907 const char *filename,
908 unsigned line,
909 const char *section,
910 const char *lvalue,
911 const char *rvalue,
912 void *data,
913 void *userdata) {
914
915 ExecContext *c = data;
916 char *w;
917 size_t l;
918 char *state;
919 unsigned long flags = 0;
920
921 assert(filename);
922 assert(lvalue);
923 assert(rvalue);
924 assert(data);
925
926 FOREACH_WORD(w, l, rvalue, state) {
927 if (strncmp(w, "shared", l) == 0)
928 flags |= MS_SHARED;
929 else if (strncmp(w, "slave", l) == 0)
930 flags |= MS_SLAVE;
931 else if (strncmp(w, "private", l) == 0)
932 flags |= MS_PRIVATE;
933 else {
934 log_error("[%s:%u] Failed to parse mount flags: %s", filename, line, rvalue);
935 return -EINVAL;
936 }
937 }
938
939 c->mount_flags = flags;
940 return 0;
941 }
942
943 #define FOLLOW_MAX 8
944
945 static int open_follow(char **filename, FILE **_f, Set *names, char **_final) {
946 unsigned c = 0;
947 int fd, r;
948 FILE *f;
949 char *id = NULL;
950
951 assert(filename);
952 assert(*filename);
953 assert(_f);
954 assert(names);
955
956 /* This will update the filename pointer if the loaded file is
957 * reached by a symlink. The old string will be freed. */
958
959 for (;;) {
960 char *target, *k, *name;
961
962 if (c++ >= FOLLOW_MAX)
963 return -ELOOP;
964
965 path_kill_slashes(*filename);
966
967 /* Add the file name we are currently looking at to
968 * the names of this unit */
969 name = file_name_from_path(*filename);
970 if (!(id = set_get(names, name))) {
971
972 if (!(id = strdup(name)))
973 return -ENOMEM;
974
975 if ((r = set_put(names, id)) < 0) {
976 free(id);
977 return r;
978 }
979 }
980
981 /* Try to open the file name, but don't if its a symlink */
982 if ((fd = open(*filename, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW)) >= 0)
983 break;
984
985 if (errno != ELOOP)
986 return -errno;
987
988 /* Hmm, so this is a symlink. Let's read the name, and follow it manually */
989 if ((r = readlink_malloc(*filename, &target)) < 0)
990 return r;
991
992 k = file_in_same_dir(*filename, target);
993 free(target);
994
995 if (!k)
996 return -ENOMEM;
997
998 free(*filename);
999 *filename = k;
1000 }
1001
1002 if (!(f = fdopen(fd, "r"))) {
1003 r = -errno;
1004 close_nointr_nofail(fd);
1005 return r;
1006 }
1007
1008 *_f = f;
1009 *_final = id;
1010 return 0;
1011 }
1012
1013 static int merge_by_names(Unit **u, Set *names, const char *id) {
1014 char *k;
1015 int r;
1016
1017 assert(u);
1018 assert(*u);
1019 assert(names);
1020
1021 /* Let's try to add in all symlink names we found */
1022 while ((k = set_steal_first(names))) {
1023
1024 /* First try to merge in the other name into our
1025 * unit */
1026 if ((r = unit_merge_by_name(*u, k)) < 0) {
1027 Unit *other;
1028
1029 /* Hmm, we couldn't merge the other unit into
1030 * ours? Then let's try it the other way
1031 * round */
1032
1033 other = manager_get_unit((*u)->meta.manager, k);
1034 free(k);
1035
1036 if (other)
1037 if ((r = unit_merge(other, *u)) >= 0) {
1038 *u = other;
1039 return merge_by_names(u, names, NULL);
1040 }
1041
1042 return r;
1043 }
1044
1045 if (id == k)
1046 unit_choose_id(*u, id);
1047
1048 free(k);
1049 }
1050
1051 return 0;
1052 }
1053
1054 static void dump_items(FILE *f, const ConfigItem *items) {
1055 const ConfigItem *i;
1056 const char *prev_section = NULL;
1057 bool not_first = false;
1058
1059 struct {
1060 ConfigParserCallback callback;
1061 const char *rvalue;
1062 } table[] = {
1063 { config_parse_int, "INTEGER" },
1064 { config_parse_unsigned, "UNSIGNED" },
1065 { config_parse_size, "SIZE" },
1066 { config_parse_bool, "BOOLEAN" },
1067 { config_parse_string, "STRING" },
1068 { config_parse_path, "PATH" },
1069 { config_parse_strv, "STRING [...]" },
1070 { config_parse_nice, "NICE" },
1071 { config_parse_oom_adjust, "OOMADJUST" },
1072 { config_parse_io_class, "IOCLASS" },
1073 { config_parse_io_priority, "IOPRIORITY" },
1074 { config_parse_cpu_sched_policy, "CPUSCHEDPOLICY" },
1075 { config_parse_cpu_sched_prio, "CPUSCHEDPRIO" },
1076 { config_parse_cpu_affinity, "CPUAFFINITY" },
1077 { config_parse_mode, "MODE" },
1078 { config_parse_output, "OUTPUT" },
1079 { config_parse_input, "INPUT" },
1080 { config_parse_facility, "FACILITY" },
1081 { config_parse_level, "LEVEL" },
1082 { config_parse_capabilities, "CAPABILITIES" },
1083 { config_parse_secure_bits, "SECUREBITS" },
1084 { config_parse_bounding_set, "BOUNDINGSET" },
1085 { config_parse_timer_slack_ns, "TIMERSLACK" },
1086 { config_parse_limit, "LIMIT" },
1087 { config_parse_cgroup, "CGROUP [...]" },
1088 { config_parse_deps, "UNIT [...]" },
1089 { config_parse_names, "UNIT [...]" },
1090 { config_parse_exec, "PATH [ARGUMENT [...]]" },
1091 { config_parse_service_type, "SERVICETYPE" },
1092 { config_parse_service_restart, "SERVICERESTART" },
1093 { config_parse_sysv_priority, "SYSVPRIORITY" },
1094 { config_parse_kill_mode, "KILLMODE" },
1095 { config_parse_listen, "SOCKET [...]" },
1096 { config_parse_socket_bind, "SOCKETBIND" },
1097 { config_parse_bindtodevice, "NETWORKINTERFACE" },
1098 { config_parse_usec, "SECONDS" },
1099 { config_parse_path_strv, "PATH [...]" },
1100 { config_parse_mount_flags, "MOUNTFLAG [...]" }
1101 };
1102
1103 assert(f);
1104 assert(items);
1105
1106 for (i = items; i->lvalue; i++) {
1107 unsigned j;
1108 const char *rvalue = "OTHER";
1109
1110 if (!streq_ptr(i->section, prev_section)) {
1111 if (!not_first)
1112 not_first = true;
1113 else
1114 fputc('\n', f);
1115
1116 fprintf(f, "[%s]\n", i->section);
1117 prev_section = i->section;
1118 }
1119
1120 for (j = 0; j < ELEMENTSOF(table); j++)
1121 if (i->parse == table[j].callback) {
1122 rvalue = table[j].rvalue;
1123 break;
1124 }
1125
1126 fprintf(f, "%s=%s\n", i->lvalue, rvalue);
1127 }
1128 }
1129
1130 static int load_from_path(Unit *u, const char *path) {
1131
1132 static const char* const section_table[_UNIT_TYPE_MAX] = {
1133 [UNIT_SERVICE] = "Service",
1134 [UNIT_TIMER] = "Timer",
1135 [UNIT_SOCKET] = "Socket",
1136 [UNIT_TARGET] = "Target",
1137 [UNIT_DEVICE] = "Device",
1138 [UNIT_MOUNT] = "Mount",
1139 [UNIT_AUTOMOUNT] = "Automount",
1140 [UNIT_SNAPSHOT] = "Snapshot"
1141 };
1142
1143 #define EXEC_CONTEXT_CONFIG_ITEMS(context, section) \
1144 { "WorkingDirectory", config_parse_path, &(context).working_directory, section }, \
1145 { "RootDirectory", config_parse_path, &(context).root_directory, section }, \
1146 { "User", config_parse_string, &(context).user, section }, \
1147 { "Group", config_parse_string, &(context).group, section }, \
1148 { "SupplementaryGroups", config_parse_strv, &(context).supplementary_groups, section }, \
1149 { "Nice", config_parse_nice, &(context), section }, \
1150 { "OOMAdjust", config_parse_oom_adjust, &(context), section }, \
1151 { "IOSchedulingClass", config_parse_io_class, &(context), section }, \
1152 { "IOSchedulingPriority", config_parse_io_priority, &(context), section }, \
1153 { "CPUSchedulingPolicy", config_parse_cpu_sched_policy,&(context), section }, \
1154 { "CPUSchedulingPriority", config_parse_cpu_sched_prio, &(context), section }, \
1155 { "CPUSchedulingResetOnFork", config_parse_bool, &(context).cpu_sched_reset_on_fork, section }, \
1156 { "CPUAffinity", config_parse_cpu_affinity, &(context), section }, \
1157 { "UMask", config_parse_mode, &(context).umask, section }, \
1158 { "Environment", config_parse_strv, &(context).environment, section }, \
1159 { "StandardInput", config_parse_input, &(context).std_input, section }, \
1160 { "StandardOutput", config_parse_output, &(context).std_output, section }, \
1161 { "StandardError", config_parse_output, &(context).std_output, section }, \
1162 { "TTYPath", config_parse_path, &(context).tty_path, section }, \
1163 { "SyslogIdentifier", config_parse_string, &(context).syslog_identifier, section }, \
1164 { "SyslogFacility", config_parse_facility, &(context).syslog_priority, section }, \
1165 { "SyslogLevel", config_parse_level, &(context).syslog_priority, section }, \
1166 { "Capabilities", config_parse_capabilities, &(context), section }, \
1167 { "SecureBits", config_parse_secure_bits, &(context), section }, \
1168 { "CapabilityBoundingSetDrop", config_parse_bounding_set, &(context), section }, \
1169 { "TimerSlackNS", config_parse_timer_slack_ns, &(context), section }, \
1170 { "LimitCPU", config_parse_limit, &(context).rlimit[RLIMIT_CPU], section }, \
1171 { "LimitFSIZE", config_parse_limit, &(context).rlimit[RLIMIT_FSIZE], section }, \
1172 { "LimitDATA", config_parse_limit, &(context).rlimit[RLIMIT_DATA], section }, \
1173 { "LimitSTACK", config_parse_limit, &(context).rlimit[RLIMIT_STACK], section }, \
1174 { "LimitCORE", config_parse_limit, &(context).rlimit[RLIMIT_CORE], section }, \
1175 { "LimitRSS", config_parse_limit, &(context).rlimit[RLIMIT_RSS], section }, \
1176 { "LimitNOFILE", config_parse_limit, &(context).rlimit[RLIMIT_NOFILE], section }, \
1177 { "LimitAS", config_parse_limit, &(context).rlimit[RLIMIT_AS], section }, \
1178 { "LimitNPROC", config_parse_limit, &(context).rlimit[RLIMIT_NPROC], section }, \
1179 { "LimitMEMLOCK", config_parse_limit, &(context).rlimit[RLIMIT_MEMLOCK], section }, \
1180 { "LimitLOCKS", config_parse_limit, &(context).rlimit[RLIMIT_LOCKS], section }, \
1181 { "LimitSIGPENDING", config_parse_limit, &(context).rlimit[RLIMIT_SIGPENDING], section }, \
1182 { "LimitMSGQUEUE", config_parse_limit, &(context).rlimit[RLIMIT_MSGQUEUE], section }, \
1183 { "LimitNICE", config_parse_limit, &(context).rlimit[RLIMIT_NICE], section }, \
1184 { "LimitRTPRIO", config_parse_limit, &(context).rlimit[RLIMIT_RTPRIO], section }, \
1185 { "LimitRTTIME", config_parse_limit, &(context).rlimit[RLIMIT_RTTIME], section }, \
1186 { "ControlGroup", config_parse_cgroup, u, section }, \
1187 { "ReadWriteDirectories", config_parse_path_strv, &(context).read_write_dirs, section }, \
1188 { "ReadOnlyDirectories", config_parse_path_strv, &(context).read_only_dirs, section }, \
1189 { "InaccessibleDirectories",config_parse_path_strv, &(context).inaccessible_dirs, section }, \
1190 { "PrivateTmp", config_parse_bool, &(context).private_tmp, section }, \
1191 { "MountFlags", config_parse_mount_flags, &(context), section }
1192
1193 const ConfigItem items[] = {
1194 { "Names", config_parse_names, u, "Unit" },
1195 { "Description", config_parse_string, &u->meta.description, "Unit" },
1196 { "Requires", config_parse_deps, UINT_TO_PTR(UNIT_REQUIRES), "Unit" },
1197 { "RequiresOverridable", config_parse_deps, UINT_TO_PTR(UNIT_REQUIRES_OVERRIDABLE), "Unit" },
1198 { "Requisite", config_parse_deps, UINT_TO_PTR(UNIT_REQUISITE), "Unit" },
1199 { "RequisiteOverridable", config_parse_deps, UINT_TO_PTR(UNIT_REQUISITE_OVERRIDABLE), "Unit" },
1200 { "Wants", config_parse_deps, UINT_TO_PTR(UNIT_WANTS), "Unit" },
1201 { "Conflicts", config_parse_deps, UINT_TO_PTR(UNIT_CONFLICTS), "Unit" },
1202 { "Before", config_parse_deps, UINT_TO_PTR(UNIT_BEFORE), "Unit" },
1203 { "After", config_parse_deps, UINT_TO_PTR(UNIT_AFTER), "Unit" },
1204 { "RecursiveStop", config_parse_bool, &u->meta.recursive_stop, "Unit" },
1205 { "StopWhenUnneeded", config_parse_bool, &u->meta.stop_when_unneeded, "Unit" },
1206
1207 { "PIDFile", config_parse_path, &u->service.pid_file, "Service" },
1208 { "ExecStartPre", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START_PRE, "Service" },
1209 { "ExecStart", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START, "Service" },
1210 { "ExecStartPost", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START_POST, "Service" },
1211 { "ExecReload", config_parse_exec, u->service.exec_command+SERVICE_EXEC_RELOAD, "Service" },
1212 { "ExecStop", config_parse_exec, u->service.exec_command+SERVICE_EXEC_STOP, "Service" },
1213 { "ExecStopPost", config_parse_exec, u->service.exec_command+SERVICE_EXEC_STOP_POST, "Service" },
1214 { "RestartSec", config_parse_usec, &u->service.restart_usec, "Service" },
1215 { "TimeoutSec", config_parse_usec, &u->service.timeout_usec, "Service" },
1216 { "Type", config_parse_service_type, &u->service.type, "Service" },
1217 { "Restart", config_parse_service_restart, &u->service.restart, "Service" },
1218 { "PermissionsStartOnly", config_parse_bool, &u->service.permissions_start_only, "Service" },
1219 { "RootDirectoryStartOnly", config_parse_bool, &u->service.root_directory_start_only, "Service" },
1220 { "ValidNoProcess", config_parse_bool, &u->service.valid_no_process, "Service" },
1221 { "SysVStartPriority", config_parse_sysv_priority, &u->service.sysv_start_priority, "Service" },
1222 { "KillMode", config_parse_kill_mode, &u->service.kill_mode, "Service" },
1223 { "NonBlocking", config_parse_bool, &u->service.exec_context.non_blocking, "Service" },
1224 { "BusName", config_parse_string, &u->service.bus_name, "Service" },
1225 EXEC_CONTEXT_CONFIG_ITEMS(u->service.exec_context, "Service"),
1226
1227 { "ListenStream", config_parse_listen, &u->socket, "Socket" },
1228 { "ListenDatagram", config_parse_listen, &u->socket, "Socket" },
1229 { "ListenSequentialPacket", config_parse_listen, &u->socket, "Socket" },
1230 { "ListenFIFO", config_parse_listen, &u->socket, "Socket" },
1231 { "BindIPv6Only", config_parse_socket_bind, &u->socket, "Socket" },
1232 { "Backlog", config_parse_unsigned, &u->socket.backlog, "Socket" },
1233 { "BindToDevice", config_parse_bindtodevice, &u->socket, "Socket" },
1234 { "ExecStartPre", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_START_PRE, "Socket" },
1235 { "ExecStartPost", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_START_POST, "Socket" },
1236 { "ExecStopPre", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_STOP_PRE, "Socket" },
1237 { "ExecStopPost", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_STOP_POST, "Socket" },
1238 { "TimeoutSec", config_parse_usec, &u->socket.timeout_usec, "Socket" },
1239 { "DirectoryMode", config_parse_mode, &u->socket.directory_mode, "Socket" },
1240 { "SocketMode", config_parse_mode, &u->socket.socket_mode, "Socket" },
1241 { "KillMode", config_parse_kill_mode, &u->socket.kill_mode, "Socket" },
1242 { "Accept", config_parse_bool, &u->socket.accept, "Socket" },
1243 EXEC_CONTEXT_CONFIG_ITEMS(u->socket.exec_context, "Socket"),
1244
1245 { "What", config_parse_string, &u->mount.parameters_fragment.what, "Mount" },
1246 { "Where", config_parse_path, &u->mount.where, "Mount" },
1247 { "Options", config_parse_string, &u->mount.parameters_fragment.options, "Mount" },
1248 { "Type", config_parse_string, &u->mount.parameters_fragment.fstype, "Mount" },
1249 { "TimeoutSec", config_parse_usec, &u->mount.timeout_usec, "Mount" },
1250 { "KillMode", config_parse_kill_mode, &u->mount.kill_mode, "Mount" },
1251 EXEC_CONTEXT_CONFIG_ITEMS(u->mount.exec_context, "Mount"),
1252
1253 { "Where", config_parse_path, &u->automount.where, "Automount" },
1254
1255 { NULL, NULL, NULL, NULL }
1256 };
1257
1258 #undef EXEC_CONTEXT_CONFIG_ITEMS
1259
1260 const char *sections[3];
1261 char *k;
1262 int r;
1263 Set *symlink_names;
1264 FILE *f = NULL;
1265 char *filename = NULL, *id = NULL;
1266 Unit *merged;
1267
1268 if (!u) {
1269 /* Dirty dirty hack. */
1270 dump_items((FILE*) path, items);
1271 return 0;
1272 }
1273
1274 assert(u);
1275 assert(path);
1276
1277 sections[0] = "Unit";
1278 sections[1] = section_table[u->meta.type];
1279 sections[2] = NULL;
1280
1281 if (!(symlink_names = set_new(string_hash_func, string_compare_func)))
1282 return -ENOMEM;
1283
1284 if (path_is_absolute(path)) {
1285
1286 if (!(filename = strdup(path))) {
1287 r = -ENOMEM;
1288 goto finish;
1289 }
1290
1291 if ((r = open_follow(&filename, &f, symlink_names, &id)) < 0) {
1292 free(filename);
1293 filename = NULL;
1294
1295 if (r != -ENOENT)
1296 goto finish;
1297 }
1298
1299 } else {
1300 char **p;
1301
1302 STRV_FOREACH(p, u->meta.manager->unit_path) {
1303
1304 /* Instead of opening the path right away, we manually
1305 * follow all symlinks and add their name to our unit
1306 * name set while doing so */
1307 if (!(filename = path_make_absolute(path, *p))) {
1308 r = -ENOMEM;
1309 goto finish;
1310 }
1311
1312 if ((r = open_follow(&filename, &f, symlink_names, &id)) < 0) {
1313 char *sn;
1314
1315 free(filename);
1316 filename = NULL;
1317
1318 if (r != -ENOENT)
1319 goto finish;
1320
1321 /* Empty the symlink names for the next run */
1322 while ((sn = set_steal_first(symlink_names)))
1323 free(sn);
1324
1325 continue;
1326 }
1327
1328 break;
1329 }
1330 }
1331
1332 if (!filename) {
1333 r = 0;
1334 goto finish;
1335 }
1336
1337 merged = u;
1338 if ((r = merge_by_names(&merged, symlink_names, id)) < 0)
1339 goto finish;
1340
1341 if (merged != u) {
1342 u->meta.load_state = UNIT_MERGED;
1343 r = 0;
1344 goto finish;
1345 }
1346
1347 /* Now, parse the file contents */
1348 if ((r = config_parse(filename, f, sections, items, u)) < 0)
1349 goto finish;
1350
1351 free(u->meta.fragment_path);
1352 u->meta.fragment_path = filename;
1353 filename = NULL;
1354
1355 u->meta.load_state = UNIT_LOADED;
1356 r = 0;
1357
1358 finish:
1359 while ((k = set_steal_first(symlink_names)))
1360 free(k);
1361
1362 set_free(symlink_names);
1363 free(filename);
1364
1365 if (f)
1366 fclose(f);
1367
1368 return r;
1369 }
1370
1371 int unit_load_fragment(Unit *u) {
1372 int r;
1373
1374 assert(u);
1375
1376 if (u->meta.fragment_path) {
1377
1378 if ((r = load_from_path(u, u->meta.fragment_path)) < 0)
1379 return r;
1380
1381 } else {
1382 Iterator i;
1383 const char *t;
1384
1385 /* Try to find the unit under its id */
1386 if ((r = load_from_path(u, u->meta.id)) < 0)
1387 return r;
1388
1389 /* Try to find an alias we can load this with */
1390 if (u->meta.load_state == UNIT_STUB)
1391 SET_FOREACH(t, u->meta.names, i) {
1392
1393 if (t == u->meta.id)
1394 continue;
1395
1396 if ((r = load_from_path(u, t)) < 0)
1397 return r;
1398
1399 if (u->meta.load_state != UNIT_STUB)
1400 break;
1401 }
1402
1403 /* Now, follow the same logic, but look for a template */
1404 if (u->meta.load_state == UNIT_STUB && u->meta.instance) {
1405 char *k;
1406
1407 if (!(k = unit_name_template(u->meta.id)))
1408 return -ENOMEM;
1409
1410 r = load_from_path(u, k);
1411 free(k);
1412
1413 if (r < 0)
1414 return r;
1415
1416 if (u->meta.load_state == UNIT_STUB)
1417 SET_FOREACH(t, u->meta.names, i) {
1418
1419 if (t == u->meta.id)
1420 continue;
1421
1422 if (!(k = unit_name_template(t)))
1423 return -ENOMEM;
1424
1425 r = load_from_path(u, k);
1426 free(k);
1427
1428 if (r < 0)
1429 return r;
1430
1431 if (u->meta.load_state != UNIT_STUB)
1432 break;
1433 }
1434 }
1435 }
1436
1437 return 0;
1438 }
1439
1440 void unit_dump_config_items(FILE *f) {
1441 /* OK, this wins a prize for extreme ugliness. */
1442
1443 load_from_path(NULL, (const void*) f);
1444 }