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