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