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