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