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