]> git.ipfire.org Git - people/ms/systemd.git/blob - load-fragment.c
config: implement search path logic
[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
31 #include "unit.h"
32 #include "strv.h"
33 #include "conf-parser.h"
34 #include "load-fragment.h"
35 #include "log.h"
36 #include "ioprio.h"
37 #include "securebits.h"
38 #include "missing.h"
39
40 static int config_parse_deps(
41 const char *filename,
42 unsigned line,
43 const char *section,
44 const char *lvalue,
45 const char *rvalue,
46 void *data,
47 void *userdata) {
48
49 UnitDependency d = PTR_TO_UINT(data);
50 Unit *u = userdata;
51 char *w;
52 size_t l;
53 char *state;
54
55 assert(filename);
56 assert(lvalue);
57 assert(rvalue);
58
59 FOREACH_WORD(w, &l, rvalue, state) {
60 char *t;
61 int r;
62
63 if (!(t = strndup(w, l)))
64 return -ENOMEM;
65
66 r = unit_add_dependency_by_name(u, d, t);
67 free(t);
68
69 if (r < 0)
70 return r;
71 }
72
73 return 0;
74 }
75
76 static int config_parse_names(
77 const char *filename,
78 unsigned line,
79 const char *section,
80 const char *lvalue,
81 const char *rvalue,
82 void *data,
83 void *userdata) {
84
85 Unit *u = userdata;
86 char *w;
87 size_t l;
88 char *state;
89
90 assert(filename);
91 assert(lvalue);
92 assert(rvalue);
93 assert(data);
94
95 FOREACH_WORD(w, &l, rvalue, state) {
96 char *t;
97 int r;
98 Unit *other;
99
100 if (!(t = strndup(w, l)))
101 return -ENOMEM;
102
103 other = manager_get_unit(u->meta.manager, t);
104
105 if (other) {
106
107 if (other != u) {
108
109 if (other->meta.load_state != UNIT_STUB) {
110 free(t);
111 return -EEXIST;
112 }
113
114 if ((r = unit_merge(u, other)) < 0) {
115 free(t);
116 return r;
117 }
118 }
119
120 } else {
121 if ((r = unit_add_name(u, t)) < 0) {
122 free(t);
123 return r;
124 }
125 }
126
127 free(t);
128 }
129
130 return 0;
131 }
132
133 static int config_parse_listen(
134 const char *filename,
135 unsigned line,
136 const char *section,
137 const char *lvalue,
138 const char *rvalue,
139 void *data,
140 void *userdata) {
141
142 int r;
143 SocketPort *p;
144 Socket *s;
145
146 assert(filename);
147 assert(lvalue);
148 assert(rvalue);
149 assert(data);
150
151 s = (Socket*) data;
152
153 if (!(p = new0(SocketPort, 1)))
154 return -ENOMEM;
155
156 if (streq(lvalue, "ListenFIFO")) {
157 p->type = SOCKET_FIFO;
158
159 if (!(p->path = strdup(rvalue))) {
160 free(p);
161 return -ENOMEM;
162 }
163 } else {
164 p->type = SOCKET_SOCKET;
165
166 if ((r = socket_address_parse(&p->address, rvalue)) < 0) {
167 log_error("[%s:%u] Failed to parse address value: %s", filename, line, rvalue);
168 free(p);
169 return r;
170 }
171
172 if (streq(lvalue, "ListenStream"))
173 p->address.type = SOCK_STREAM;
174 else if (streq(lvalue, "ListenDatagram"))
175 p->address.type = SOCK_DGRAM;
176 else {
177 assert(streq(lvalue, "ListenSequentialPacket"));
178 p->address.type = SOCK_SEQPACKET;
179 }
180
181 if (socket_address_family(&p->address) != AF_LOCAL && p->address.type == SOCK_SEQPACKET) {
182 free(p);
183 return -EPROTONOSUPPORT;
184 }
185 }
186
187 p->fd = -1;
188 LIST_PREPEND(SocketPort, port, s->ports, p);
189
190 return 0;
191 }
192
193 static int config_parse_socket_bind(
194 const char *filename,
195 unsigned line,
196 const char *section,
197 const char *lvalue,
198 const char *rvalue,
199 void *data,
200 void *userdata) {
201
202 int r;
203 Socket *s;
204
205 assert(filename);
206 assert(lvalue);
207 assert(rvalue);
208 assert(data);
209
210 s = (Socket*) data;
211
212 if ((r = parse_boolean(rvalue)) < 0) {
213 log_error("[%s:%u] Failed to parse bind IPv6 only value: %s", filename, line, rvalue);
214 return r;
215 }
216
217 s->bind_ipv6_only = r ? SOCKET_ADDRESS_IPV6_ONLY : SOCKET_ADDRESS_BOTH;
218
219 return 0;
220 }
221
222 static int config_parse_nice(
223 const char *filename,
224 unsigned line,
225 const char *section,
226 const char *lvalue,
227 const char *rvalue,
228 void *data,
229 void *userdata) {
230
231 ExecContext *c = data;
232 int priority, r;
233
234 assert(filename);
235 assert(lvalue);
236 assert(rvalue);
237 assert(data);
238
239 if ((r = safe_atoi(rvalue, &priority)) < 0) {
240 log_error("[%s:%u] Failed to parse nice priority: %s", filename, line, rvalue);
241 return r;
242 }
243
244 if (priority < PRIO_MIN || priority >= PRIO_MAX) {
245 log_error("[%s:%u] Nice priority out of range: %s", filename, line, rvalue);
246 return -ERANGE;
247 }
248
249 c->nice = priority;
250 c->nice_set = false;
251
252 return 0;
253 }
254
255 static int config_parse_oom_adjust(
256 const char *filename,
257 unsigned line,
258 const char *section,
259 const char *lvalue,
260 const char *rvalue,
261 void *data,
262 void *userdata) {
263
264 ExecContext *c = data;
265 int oa, r;
266
267 assert(filename);
268 assert(lvalue);
269 assert(rvalue);
270 assert(data);
271
272 if ((r = safe_atoi(rvalue, &oa)) < 0) {
273 log_error("[%s:%u] Failed to parse OOM adjust value: %s", filename, line, rvalue);
274 return r;
275 }
276
277 if (oa < OOM_DISABLE || oa > OOM_ADJUST_MAX) {
278 log_error("[%s:%u] OOM adjust value out of range: %s", filename, line, rvalue);
279 return -ERANGE;
280 }
281
282 c->oom_adjust = oa;
283 c->oom_adjust_set = true;
284
285 return 0;
286 }
287
288 static int config_parse_mode(
289 const char *filename,
290 unsigned line,
291 const char *section,
292 const char *lvalue,
293 const char *rvalue,
294 void *data,
295 void *userdata) {
296
297 mode_t *m = data;
298 long l;
299 char *x = NULL;
300
301 assert(filename);
302 assert(lvalue);
303 assert(rvalue);
304 assert(data);
305
306 errno = 0;
307 l = strtol(rvalue, &x, 8);
308 if (!x || *x || errno) {
309 log_error("[%s:%u] Failed to parse mode value: %s", filename, line, rvalue);
310 return errno ? -errno : -EINVAL;
311 }
312
313 if (l < 0000 || l > 07777) {
314 log_error("[%s:%u] mode value out of range: %s", filename, line, rvalue);
315 return -ERANGE;
316 }
317
318 *m = (mode_t) l;
319 return 0;
320 }
321
322 static int config_parse_exec(
323 const char *filename,
324 unsigned line,
325 const char *section,
326 const char *lvalue,
327 const char *rvalue,
328 void *data,
329 void *userdata) {
330
331 ExecCommand **e = data, *ee, *nce = NULL;
332 char **n;
333 char *w;
334 unsigned k;
335 size_t l;
336 char *state;
337
338 assert(filename);
339 assert(lvalue);
340 assert(rvalue);
341 assert(data);
342
343 k = 0;
344 FOREACH_WORD_QUOTED(w, l, rvalue, state)
345 k++;
346
347 if (!(n = new(char*, k+1)))
348 return -ENOMEM;
349
350 k = 0;
351 FOREACH_WORD_QUOTED(w, l, rvalue, state)
352 if (!(n[k++] = strndup(w, l)))
353 goto fail;
354
355 n[k] = NULL;
356
357 if (!n[0] || !path_is_absolute(n[0])) {
358 log_error("[%s:%u] Invalid executable path in command line: %s", filename, line, rvalue);
359 strv_free(n);
360 return -EINVAL;
361 }
362
363 if (!(nce = new0(ExecCommand, 1)))
364 goto fail;
365
366 nce->argv = n;
367 if (!(nce->path = strdup(n[0])))
368 goto fail;
369
370 if (*e) {
371 /* It's kinda important that we keep the order here */
372 LIST_FIND_TAIL(ExecCommand, command, *e, ee);
373 LIST_INSERT_AFTER(ExecCommand, command, *e, ee, nce);
374 } else
375 *e = nce;
376
377 return 0;
378
379 fail:
380 for (; k > 0; k--)
381 free(n[k-1]);
382 free(n);
383
384 free(nce);
385
386 return -ENOMEM;
387 }
388
389 static int config_parse_usec(
390 const char *filename,
391 unsigned line,
392 const char *section,
393 const char *lvalue,
394 const char *rvalue,
395 void *data,
396 void *userdata) {
397
398 usec_t *usec = data;
399 unsigned long long u;
400 int r;
401
402 assert(filename);
403 assert(lvalue);
404 assert(rvalue);
405 assert(data);
406
407 if ((r = safe_atollu(rvalue, &u)) < 0) {
408 log_error("[%s:%u] Failed to parse time value: %s", filename, line, rvalue);
409 return r;
410 }
411
412 /* We actually assume the user configures seconds. Later on we
413 * might choose to support suffixes for time values, to
414 * configure bigger or smaller units */
415
416 *usec = u * USEC_PER_SEC;
417
418 return 0;
419 }
420
421 static int config_parse_service_type(
422 const char *filename,
423 unsigned line,
424 const char *section,
425 const char *lvalue,
426 const char *rvalue,
427 void *data,
428 void *userdata) {
429
430 Service *s = data;
431 ServiceType x;
432
433 assert(filename);
434 assert(lvalue);
435 assert(rvalue);
436 assert(data);
437
438 if ((x = service_type_from_string(rvalue)) < 0) {
439 log_error("[%s:%u] Failed to parse service type: %s", filename, line, rvalue);
440 return -EBADMSG;
441 }
442
443 s->type = x;
444
445 return 0;
446 }
447
448 static int config_parse_service_restart(
449 const char *filename,
450 unsigned line,
451 const char *section,
452 const char *lvalue,
453 const char *rvalue,
454 void *data,
455 void *userdata) {
456
457 Service *s = data;
458 ServiceRestart x;
459
460 assert(filename);
461 assert(lvalue);
462 assert(rvalue);
463 assert(data);
464
465 if ((x = service_restart_from_string(rvalue)) < 0) {
466 log_error("[%s:%u] Failed to parse service restart specifier: %s", filename, line, rvalue);
467 return -EBADMSG;
468 }
469
470 s->restart = x;
471
472 return 0;
473 }
474
475 static int config_parse_bindtodevice(
476 const char *filename,
477 unsigned line,
478 const char *section,
479 const char *lvalue,
480 const char *rvalue,
481 void *data,
482 void *userdata) {
483
484 Socket *s = data;
485 char *n;
486
487 assert(filename);
488 assert(lvalue);
489 assert(rvalue);
490 assert(data);
491
492 if (rvalue[0] && !streq(rvalue, "*")) {
493 if (!(n = strdup(rvalue)))
494 return -ENOMEM;
495 } else
496 n = NULL;
497
498 free(s->bind_to_device);
499 s->bind_to_device = n;
500
501 return 0;
502 }
503
504 static int config_parse_output(
505 const char *filename,
506 unsigned line,
507 const char *section,
508 const char *lvalue,
509 const char *rvalue,
510 void *data,
511 void *userdata) {
512
513 ExecOutput *o = data, x;
514
515 assert(filename);
516 assert(lvalue);
517 assert(rvalue);
518 assert(data);
519
520 if ((x = exec_output_from_string(rvalue)) < 0) {
521 log_error("[%s:%u] Failed to parse output specifier: %s", filename, line, rvalue);
522 return -EBADMSG;
523 }
524
525 *o = x;
526
527 return 0;
528 }
529
530 static int config_parse_input(
531 const char *filename,
532 unsigned line,
533 const char *section,
534 const char *lvalue,
535 const char *rvalue,
536 void *data,
537 void *userdata) {
538
539 ExecInput *i = data, x;
540
541 assert(filename);
542 assert(lvalue);
543 assert(rvalue);
544 assert(data);
545
546 if ((x = exec_input_from_string(rvalue)) < 0) {
547 log_error("[%s:%u] Failed to parse input specifier: %s", filename, line, rvalue);
548 return -EBADMSG;
549 }
550
551 *i = x;
552
553 return 0;
554 }
555
556 static int config_parse_facility(
557 const char *filename,
558 unsigned line,
559 const char *section,
560 const char *lvalue,
561 const char *rvalue,
562 void *data,
563 void *userdata) {
564
565
566 int *o = data, x;
567
568 assert(filename);
569 assert(lvalue);
570 assert(rvalue);
571 assert(data);
572
573 if ((x = log_facility_from_string(rvalue)) < 0)
574
575 /* Second try, let's see if this is a number. */
576 if (safe_atoi(rvalue, &x) < 0 || !log_facility_to_string(x)) {
577 log_error("[%s:%u] Failed to parse log facility: %s", filename, line, rvalue);
578 return -EBADMSG;
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
605 /* Second try, let's see if this is a number. */
606 if (safe_atoi(rvalue, &x) < 0 || !log_level_to_string(x)) {
607 log_error("[%s:%u] Failed to parse log level: %s", filename, line, rvalue);
608 return -EBADMSG;
609 }
610
611 *o = LOG_MAKEPRI(LOG_FAC(*o), x);
612 return 0;
613 }
614
615 static int config_parse_io_class(
616 const char *filename,
617 unsigned line,
618 const char *section,
619 const char *lvalue,
620 const char *rvalue,
621 void *data,
622 void *userdata) {
623
624 ExecContext *c = data;
625 int x;
626
627 assert(filename);
628 assert(lvalue);
629 assert(rvalue);
630 assert(data);
631
632 if ((x = ioprio_class_from_string(rvalue)) < 0)
633
634 /* Second try, let's see if this is a number. */
635 if (safe_atoi(rvalue, &x) < 0 || !ioprio_class_to_string(x)) {
636 log_error("[%s:%u] Failed to parse IO scheduling class: %s", filename, line, rvalue);
637 return -EBADMSG;
638 }
639
640 c->ioprio = IOPRIO_PRIO_VALUE(x, IOPRIO_PRIO_DATA(c->ioprio));
641 c->ioprio_set = true;
642
643 return 0;
644 }
645
646 static int config_parse_io_priority(
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 ExecContext *c = data;
656 int i;
657
658 assert(filename);
659 assert(lvalue);
660 assert(rvalue);
661 assert(data);
662
663 if (safe_atoi(rvalue, &i) < 0 || i < 0 || i >= IOPRIO_BE_NR) {
664 log_error("[%s:%u] Failed to parse io priority: %s", filename, line, rvalue);
665 return -EBADMSG;
666 }
667
668 c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_PRIO_CLASS(c->ioprio), i);
669 c->ioprio_set = true;
670
671 return 0;
672 }
673
674 static int config_parse_cpu_sched_policy(
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
684 ExecContext *c = data;
685 int x;
686
687 assert(filename);
688 assert(lvalue);
689 assert(rvalue);
690 assert(data);
691
692 if ((x = sched_policy_from_string(rvalue)) < 0)
693
694 /* Second try, let's see if this is a number. */
695 if (safe_atoi(rvalue, &x) < 0 || !sched_policy_to_string(x)) {
696 log_error("[%s:%u] Failed to parse CPU scheduling policy: %s", filename, line, rvalue);
697 return -EBADMSG;
698 }
699
700 c->cpu_sched_policy = x;
701 c->cpu_sched_set = true;
702
703 return 0;
704 }
705
706 static int config_parse_cpu_sched_prio(
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 int i;
717
718 assert(filename);
719 assert(lvalue);
720 assert(rvalue);
721 assert(data);
722
723 /* On Linux RR/FIFO have the same range */
724 if (safe_atoi(rvalue, &i) < 0 || i < sched_get_priority_min(SCHED_RR) || i > sched_get_priority_max(SCHED_RR)) {
725 log_error("[%s:%u] Failed to parse CPU scheduling priority: %s", filename, line, rvalue);
726 return -EBADMSG;
727 }
728
729 c->cpu_sched_priority = i;
730 c->cpu_sched_set = true;
731
732 return 0;
733 }
734
735 static int config_parse_cpu_affinity(
736 const char *filename,
737 unsigned line,
738 const char *section,
739 const char *lvalue,
740 const char *rvalue,
741 void *data,
742 void *userdata) {
743
744 ExecContext *c = data;
745 char *w;
746 size_t l;
747 char *state;
748
749 assert(filename);
750 assert(lvalue);
751 assert(rvalue);
752 assert(data);
753
754 FOREACH_WORD(w, &l, rvalue, state) {
755 char *t;
756 int r;
757 unsigned cpu;
758
759 if (!(t = strndup(w, l)))
760 return -ENOMEM;
761
762 r = safe_atou(t, &cpu);
763 free(t);
764
765 if (r < 0 || cpu >= CPU_SETSIZE) {
766 log_error("[%s:%u] Failed to parse CPU affinity: %s", filename, line, rvalue);
767 return -EBADMSG;
768 }
769
770 CPU_SET(cpu, &c->cpu_affinity);
771 }
772
773 c->cpu_affinity_set = true;
774
775 return 0;
776 }
777
778 static int config_parse_capabilities(
779 const char *filename,
780 unsigned line,
781 const char *section,
782 const char *lvalue,
783 const char *rvalue,
784 void *data,
785 void *userdata) {
786
787 ExecContext *c = data;
788 cap_t cap;
789
790 assert(filename);
791 assert(lvalue);
792 assert(rvalue);
793 assert(data);
794
795 if (!(cap = cap_from_text(rvalue))) {
796 if (errno == ENOMEM)
797 return -ENOMEM;
798
799 log_error("[%s:%u] Failed to parse capabilities: %s", filename, line, rvalue);
800 return -EBADMSG;
801 }
802
803 if (c->capabilities)
804 cap_free(c->capabilities);
805 c->capabilities = cap;
806
807 return 0;
808 }
809
810 static int config_parse_secure_bits(
811 const char *filename,
812 unsigned line,
813 const char *section,
814 const char *lvalue,
815 const char *rvalue,
816 void *data,
817 void *userdata) {
818
819 ExecContext *c = data;
820 char *w;
821 size_t l;
822 char *state;
823
824 assert(filename);
825 assert(lvalue);
826 assert(rvalue);
827 assert(data);
828
829 FOREACH_WORD(w, &l, rvalue, state) {
830 if (first_word(w, "keep-caps"))
831 c->secure_bits |= SECURE_KEEP_CAPS;
832 else if (first_word(w, "keep-caps-locked"))
833 c->secure_bits |= SECURE_KEEP_CAPS_LOCKED;
834 else if (first_word(w, "no-setuid-fixup"))
835 c->secure_bits |= SECURE_NO_SETUID_FIXUP;
836 else if (first_word(w, "no-setuid-fixup-locked"))
837 c->secure_bits |= SECURE_NO_SETUID_FIXUP_LOCKED;
838 else if (first_word(w, "noroot"))
839 c->secure_bits |= SECURE_NOROOT;
840 else if (first_word(w, "noroot-locked"))
841 c->secure_bits |= SECURE_NOROOT_LOCKED;
842 else {
843 log_error("[%s:%u] Failed to parse secure bits: %s", filename, line, rvalue);
844 return -EBADMSG;
845 }
846 }
847
848 return 0;
849 }
850
851 static int config_parse_bounding_set(
852 const char *filename,
853 unsigned line,
854 const char *section,
855 const char *lvalue,
856 const char *rvalue,
857 void *data,
858 void *userdata) {
859
860 ExecContext *c = data;
861 char *w;
862 size_t l;
863 char *state;
864
865 assert(filename);
866 assert(lvalue);
867 assert(rvalue);
868 assert(data);
869
870 FOREACH_WORD(w, &l, rvalue, state) {
871 char *t;
872 int r;
873 cap_value_t cap;
874
875 if (!(t = strndup(w, l)))
876 return -ENOMEM;
877
878 r = cap_from_name(t, &cap);
879 free(t);
880
881 if (r < 0) {
882 log_error("[%s:%u] Failed to parse capability bounding set: %s", filename, line, rvalue);
883 return -EBADMSG;
884 }
885
886 c->capability_bounding_set_drop |= 1 << cap;
887 }
888
889 return 0;
890 }
891
892 static int config_parse_timer_slack_ns(
893 const char *filename,
894 unsigned line,
895 const char *section,
896 const char *lvalue,
897 const char *rvalue,
898 void *data,
899 void *userdata) {
900
901 ExecContext *c = data;
902 unsigned long u;
903 int r;
904
905 assert(filename);
906 assert(lvalue);
907 assert(rvalue);
908 assert(data);
909
910 if ((r = safe_atolu(rvalue, &u)) < 0) {
911 log_error("[%s:%u] Failed to parse time slack value: %s", filename, line, rvalue);
912 return r;
913 }
914
915 c->timer_slack_ns = u;
916
917 return 0;
918 }
919
920 static int config_parse_limit(
921 const char *filename,
922 unsigned line,
923 const char *section,
924 const char *lvalue,
925 const char *rvalue,
926 void *data,
927 void *userdata) {
928
929 struct rlimit **rl = data;
930 unsigned long long u;
931 int r;
932
933 assert(filename);
934 assert(lvalue);
935 assert(rvalue);
936 assert(data);
937
938 if ((r = safe_atollu(rvalue, &u)) < 0) {
939 log_error("[%s:%u] Failed to parse resource value: %s", filename, line, rvalue);
940 return r;
941 }
942
943 if (!*rl)
944 if (!(*rl = new(struct rlimit, 1)))
945 return -ENOMEM;
946
947 (*rl)->rlim_cur = (*rl)->rlim_max = (rlim_t) u;
948 return 0;
949 }
950
951 #define FOLLOW_MAX 8
952
953 static int open_follow(char **filename, FILE **_f, Set *names, char **_id) {
954 unsigned c = 0;
955 int fd, r;
956 FILE *f;
957 char *id = NULL;
958
959 assert(filename);
960 assert(*filename);
961 assert(_f);
962 assert(names);
963
964 /* This will update the filename pointer if the loaded file is
965 * reached by a symlink. The old string will be freed. */
966
967 for (;;) {
968 char *target, *k, *name;
969
970 if (c++ >= FOLLOW_MAX)
971 return -ELOOP;
972
973 path_kill_slashes(*filename);
974
975 /* Add the file name we are currently looking at to
976 * the names of this unit */
977 name = file_name_from_path(*filename);
978 if (!(id = set_get(names, name))) {
979
980 if (!(id = strdup(name)))
981 return -ENOMEM;
982
983 if ((r = set_put(names, id)) < 0) {
984 free(id);
985 return r;
986 }
987 }
988
989 /* Try to open the file name, but don't if its a symlink */
990 if ((fd = open(*filename, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW)) >= 0)
991 break;
992
993 if (errno != ELOOP)
994 return -errno;
995
996 /* Hmm, so this is a symlink. Let's read the name, and follow it manually */
997 if ((r = readlink_malloc(*filename, &target)) < 0)
998 return r;
999
1000 k = file_in_same_dir(*filename, target);
1001 free(target);
1002
1003 if (!k)
1004 return -ENOMEM;
1005
1006 free(*filename);
1007 *filename = k;
1008 }
1009
1010 if (!(f = fdopen(fd, "r"))) {
1011 r = -errno;
1012 assert(close_nointr(fd) == 0);
1013 return r;
1014 }
1015
1016 *_f = f;
1017 *_id = id;
1018 return 0;
1019 }
1020
1021 static int load_from_path(Unit *u, const char *path) {
1022
1023 static const char* const section_table[_UNIT_TYPE_MAX] = {
1024 [UNIT_SERVICE] = "Service",
1025 [UNIT_TIMER] = "Timer",
1026 [UNIT_SOCKET] = "Socket",
1027 [UNIT_TARGET] = "Target",
1028 [UNIT_DEVICE] = "Device",
1029 [UNIT_MOUNT] = "Mount",
1030 [UNIT_AUTOMOUNT] = "Automount",
1031 [UNIT_SNAPSHOT] = "Snapshot"
1032 };
1033
1034 #define EXEC_CONTEXT_CONFIG_ITEMS(context, section) \
1035 { "WorkingDirectory", config_parse_path, &(context).working_directory, section }, \
1036 { "RootDirectory", config_parse_path, &(context).root_directory, section }, \
1037 { "User", config_parse_string, &(context).user, section }, \
1038 { "Group", config_parse_string, &(context).group, section }, \
1039 { "SupplementaryGroups", config_parse_strv, &(context).supplementary_groups, section }, \
1040 { "Nice", config_parse_nice, &(context), section }, \
1041 { "OOMAdjust", config_parse_oom_adjust, &(context), section }, \
1042 { "IOSchedulingClass", config_parse_io_class, &(context), section }, \
1043 { "IOSchedulingPriority", config_parse_io_priority, &(context), section }, \
1044 { "CPUSchedulingPolicy", config_parse_cpu_sched_policy,&(context), section }, \
1045 { "CPUSchedulingPriority", config_parse_cpu_sched_prio, &(context), section }, \
1046 { "CPUSchedulingResetOnFork", config_parse_bool, &(context).cpu_sched_reset_on_fork, section }, \
1047 { "CPUAffinity", config_parse_cpu_affinity, &(context), section }, \
1048 { "UMask", config_parse_mode, &(context).umask, section }, \
1049 { "Environment", config_parse_strv, &(context).environment, section }, \
1050 { "Output", config_parse_output, &(context).output, section }, \
1051 { "Input", config_parse_input, &(context).input, section }, \
1052 { "SyslogIdentifier", config_parse_string, &(context).syslog_identifier, section }, \
1053 { "SyslogFacility", config_parse_facility, &(context).syslog_priority, section }, \
1054 { "SyslogLevel", config_parse_level, &(context).syslog_priority, section }, \
1055 { "Capabilities", config_parse_capabilities, &(context), section }, \
1056 { "SecureBits", config_parse_secure_bits, &(context), section }, \
1057 { "CapabilityBoundingSetDrop", config_parse_bounding_set, &(context), section }, \
1058 { "TimerSlackNS", config_parse_timer_slack_ns, &(context), section }, \
1059 { "LimitCPU", config_parse_limit, &(context).rlimit[RLIMIT_CPU], section }, \
1060 { "LimitFSIZE", config_parse_limit, &(context).rlimit[RLIMIT_FSIZE], section }, \
1061 { "LimitDATA", config_parse_limit, &(context).rlimit[RLIMIT_DATA], section }, \
1062 { "LimitSTACK", config_parse_limit, &(context).rlimit[RLIMIT_STACK], section }, \
1063 { "LimitCORE", config_parse_limit, &(context).rlimit[RLIMIT_CORE], section }, \
1064 { "LimitRSS", config_parse_limit, &(context).rlimit[RLIMIT_RSS], section }, \
1065 { "LimitNOFILE", config_parse_limit, &(context).rlimit[RLIMIT_NOFILE], section }, \
1066 { "LimitAS", config_parse_limit, &(context).rlimit[RLIMIT_AS], section }, \
1067 { "LimitNPROC", config_parse_limit, &(context).rlimit[RLIMIT_NPROC], section }, \
1068 { "LimitMEMLOCK", config_parse_limit, &(context).rlimit[RLIMIT_MEMLOCK], section }, \
1069 { "LimitLOCKS", config_parse_limit, &(context).rlimit[RLIMIT_LOCKS], section }, \
1070 { "LimitSIGPENDING", config_parse_limit, &(context).rlimit[RLIMIT_SIGPENDING], section }, \
1071 { "LimitMSGQUEUE", config_parse_limit, &(context).rlimit[RLIMIT_MSGQUEUE], section }, \
1072 { "LimitNICE", config_parse_limit, &(context).rlimit[RLIMIT_NICE], section }, \
1073 { "LimitRTPRIO", config_parse_limit, &(context).rlimit[RLIMIT_RTPRIO], section }, \
1074 { "LimitRTTIME", config_parse_limit, &(context).rlimit[RLIMIT_RTTIME], section }, \
1075 { "NonBlocking", config_parse_bool, &(context).non_blocking, section }
1076
1077 const ConfigItem items[] = {
1078 { "Names", config_parse_names, u, "Meta" },
1079 { "Description", config_parse_string, &u->meta.description, "Meta" },
1080 { "Requires", config_parse_deps, UINT_TO_PTR(UNIT_REQUIRES), "Meta" },
1081 { "SoftRequires", config_parse_deps, UINT_TO_PTR(UNIT_SOFT_REQUIRES), "Meta" },
1082 { "Wants", config_parse_deps, UINT_TO_PTR(UNIT_WANTS), "Meta" },
1083 { "Requisite", config_parse_deps, UINT_TO_PTR(UNIT_REQUISITE), "Meta" },
1084 { "SoftRequisite", config_parse_deps, UINT_TO_PTR(UNIT_SOFT_REQUISITE), "Meta" },
1085 { "Conflicts", config_parse_deps, UINT_TO_PTR(UNIT_CONFLICTS), "Meta" },
1086 { "Before", config_parse_deps, UINT_TO_PTR(UNIT_BEFORE), "Meta" },
1087 { "After", config_parse_deps, UINT_TO_PTR(UNIT_AFTER), "Meta" },
1088 { "RecursiveStop", config_parse_bool, &u->meta.recursive_stop, "Meta" },
1089 { "StopWhenUnneeded", config_parse_bool, &u->meta.stop_when_unneeded, "Meta" },
1090
1091 { "PIDFile", config_parse_path, &u->service.pid_file, "Service" },
1092 { "ExecStartPre", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START_PRE, "Service" },
1093 { "ExecStart", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START, "Service" },
1094 { "ExecStartPost", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START_POST, "Service" },
1095 { "ExecReload", config_parse_exec, u->service.exec_command+SERVICE_EXEC_RELOAD, "Service" },
1096 { "ExecStop", config_parse_exec, u->service.exec_command+SERVICE_EXEC_STOP, "Service" },
1097 { "ExecStopPost", config_parse_exec, u->service.exec_command+SERVICE_EXEC_STOP_POST, "Service" },
1098 { "RestartSec", config_parse_usec, &u->service.restart_usec, "Service" },
1099 { "TimeoutSec", config_parse_usec, &u->service.timeout_usec, "Service" },
1100 { "Type", config_parse_service_type, &u->service, "Service" },
1101 { "Restart", config_parse_service_restart, &u->service, "Service" },
1102 EXEC_CONTEXT_CONFIG_ITEMS(u->service.exec_context, "Service"),
1103
1104 { "ListenStream", config_parse_listen, &u->socket, "Socket" },
1105 { "ListenDatagram", config_parse_listen, &u->socket, "Socket" },
1106 { "ListenSequentialPacket", config_parse_listen, &u->socket, "Socket" },
1107 { "ListenFIFO", config_parse_listen, &u->socket, "Socket" },
1108 { "BindIPv6Only", config_parse_socket_bind, &u->socket, "Socket" },
1109 { "Backlog", config_parse_unsigned, &u->socket.backlog, "Socket" },
1110 { "BindToDevice", config_parse_bindtodevice, &u->socket, "Socket" },
1111 { "ExecStartPre", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_START_PRE, "Socket" },
1112 { "ExecStartPost", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_START_POST, "Socket" },
1113 { "ExecStopPre", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_STOP_PRE, "Socket" },
1114 { "ExecStopPost", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_STOP_POST, "Socket" },
1115 { "DirectoryMode", config_parse_mode, &u->socket.directory_mode, "Socket" },
1116 { "SocketMode", config_parse_mode, &u->socket.socket_mode, "Socket" },
1117 EXEC_CONTEXT_CONFIG_ITEMS(u->socket.exec_context, "Socket"),
1118
1119 EXEC_CONTEXT_CONFIG_ITEMS(u->automount.exec_context, "Automount"),
1120
1121 { NULL, NULL, NULL, NULL }
1122 };
1123
1124 #undef EXEC_CONTEXT_CONFIG_ITEMS
1125
1126 const char *sections[3];
1127 char *k;
1128 int r;
1129 Set *symlink_names;
1130 FILE *f;
1131 char *filename = NULL, *id;
1132
1133 sections[0] = "Meta";
1134 sections[1] = section_table[u->meta.type];
1135 sections[2] = NULL;
1136
1137 if (!(symlink_names = set_new(string_hash_func, string_compare_func)))
1138 return -ENOMEM;
1139
1140 if (path_is_absolute(path)) {
1141
1142 if (!(filename = strdup(path))) {
1143 r = -ENOMEM;
1144 goto finish;
1145 }
1146
1147 if ((r = open_follow(&filename, &f, symlink_names, &id)) < 0) {
1148 free(filename);
1149 filename = NULL;
1150
1151 if (r != -ENOENT)
1152 goto finish;
1153 }
1154
1155 } else {
1156 char **p;
1157
1158 STRV_FOREACH(p, u->meta.manager->unit_path) {
1159
1160 /* Instead of opening the path right away, we manually
1161 * follow all symlinks and add their name to our unit
1162 * name set while doing so */
1163 if (!(filename = path_make_absolute(path, *p))) {
1164 r = -ENOMEM;
1165 goto finish;
1166 }
1167
1168 if ((r = open_follow(&filename, &f, symlink_names, &id)) < 0) {
1169 char *sn;
1170
1171 free(filename);
1172 filename = NULL;
1173
1174 if (r != -ENOENT)
1175 goto finish;
1176
1177 /* Empty the symlink names for the next run */
1178 while ((sn = set_steal_first(symlink_names)))
1179 free(sn);
1180
1181 continue;
1182 }
1183
1184 break;
1185 }
1186 }
1187
1188 if (!filename) {
1189 r = 0; /* returning 0 means: no suitable config file found */
1190 goto finish;
1191 }
1192
1193 /* Now, parse the file contents */
1194 r = config_parse(filename, f, sections, items, u);
1195 if (r < 0)
1196 goto finish;
1197
1198 /* Let's try to add in all symlink names we found */
1199 while ((k = set_steal_first(symlink_names))) {
1200 if ((r = unit_add_name(u, k)) < 0)
1201 goto finish;
1202
1203
1204 if (id == k)
1205 unit_choose_id(u, id);
1206 free(k);
1207 }
1208
1209
1210 free(u->meta.load_path);
1211 u->meta.load_path = filename;
1212 filename = NULL;
1213
1214 r = 1; /* returning 1 means: suitable config file found and loaded */
1215
1216 finish:
1217 while ((k = set_steal_first(symlink_names)))
1218 free(k);
1219 set_free(symlink_names);
1220 free(filename);
1221
1222 return r;
1223 }
1224
1225 int unit_load_fragment(Unit *u) {
1226 int r = 0;
1227 ExecContext *c;
1228
1229 assert(u);
1230 assert(u->meta.load_state == UNIT_STUB);
1231
1232 if (u->meta.load_path)
1233 r = load_from_path(u, u->meta.load_path);
1234 else {
1235 Iterator i;
1236 char *t;
1237
1238 /* Try to find a name we can load this with */
1239 SET_FOREACH(t, u->meta.names, i)
1240 if ((r = load_from_path(u, t)) != 0)
1241 return r;
1242 }
1243
1244 if (u->meta.type == UNIT_SOCKET)
1245 c = &u->socket.exec_context;
1246 else if (u->meta.type == UNIT_SERVICE)
1247 c = &u->service.exec_context;
1248 else
1249 c = NULL;
1250
1251 if (r >= 0 && c &&
1252 (c->output == EXEC_OUTPUT_KERNEL || c->output == EXEC_OUTPUT_SYSLOG)) {
1253 int k;
1254
1255 /* If syslog or kernel logging is requested, make sure
1256 * our own logging daemon is run first. */
1257
1258 if ((k = unit_add_dependency_by_name(u, UNIT_AFTER, SPECIAL_LOGGER_SOCKET)) < 0)
1259 return k;
1260
1261 if ((k = unit_add_dependency_by_name(u, UNIT_REQUIRES, SPECIAL_LOGGER_SOCKET)) < 0)
1262 return k;
1263 }
1264
1265 return r;
1266 }