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