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