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