]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/load-fragment.c
unit: reduce heap usage for unit objects
[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 47#ifndef HAVE_SYSV_COMPAT
f975e971 48int config_parse_warn_compat(
07459bb6
FF
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
f975e971 63int config_parse_unit_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
f975e971 73 UnitDependency d = ltype;
87f0e418 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 86
57020a3a
LP
87 t = strndup(w, l);
88 if (!t)
3efd4195
LP
89 return -ENOMEM;
90
9e2f7c11 91 k = unit_name_printf(u, t);
3efd4195
LP
92 free(t);
93
9e2f7c11
LP
94 if (!k)
95 return -ENOMEM;
96
701cc384 97 r = unit_add_dependency_by_name(u, d, k, NULL, true);
57020a3a
LP
98 if (r < 0)
99 log_error("[%s:%u] Failed to add dependency on %s, ignoring: %s", filename, line, k, strerror(-r));
c0b34696
LP
100
101 free(k);
3efd4195
LP
102 }
103
104 return 0;
105}
106
f975e971 107int config_parse_unit_names(
87d1515d
LP
108 const char *filename,
109 unsigned line,
110 const char *section,
111 const char *lvalue,
2b583ce6 112 int ltype,
87d1515d
LP
113 const char *rvalue,
114 void *data,
115 void *userdata) {
116
87f0e418 117 Unit *u = userdata;
87d1515d
LP
118 char *w;
119 size_t l;
120 char *state;
121
122 assert(filename);
123 assert(lvalue);
124 assert(rvalue);
125 assert(data);
126
f60f22df 127 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
9e2f7c11 128 char *t, *k;
87d1515d 129 int r;
87d1515d
LP
130
131 if (!(t = strndup(w, l)))
132 return -ENOMEM;
133
9e2f7c11 134 k = unit_name_printf(u, t);
87d1515d 135 free(t);
23a177ef 136
9e2f7c11
LP
137 if (!k)
138 return -ENOMEM;
139
140 r = unit_merge_by_name(u, k);
9e2f7c11 141
c0b34696
LP
142 if (r < 0) {
143 log_error("Failed to add name %s, ignoring: %s", k, strerror(-r));
144 free(k);
145 return 0;
146 }
147
148 free(k);
87d1515d
LP
149 }
150
151 return 0;
152}
153
f975e971 154int config_parse_unit_string_printf(
932921b5
LP
155 const char *filename,
156 unsigned line,
157 const char *section,
158 const char *lvalue,
2b583ce6 159 int ltype,
932921b5
LP
160 const char *rvalue,
161 void *data,
162 void *userdata) {
163
164 Unit *u = userdata;
f2d3769a 165 char **s = data;
932921b5
LP
166 char *k;
167
168 assert(filename);
169 assert(lvalue);
170 assert(rvalue);
f2d3769a
LP
171 assert(s);
172 assert(u);
932921b5
LP
173
174 if (!(k = unit_full_printf(u, rvalue)))
175 return -ENOMEM;
176
f2d3769a 177 free(*s);
932921b5 178 if (*k)
f2d3769a 179 *s = k;
932921b5
LP
180 else {
181 free(k);
f2d3769a 182 *s = NULL;
932921b5
LP
183 }
184
185 return 0;
186}
187
f975e971 188int config_parse_unit_strv_printf(
8fef7659
LP
189 const char *filename,
190 unsigned line,
191 const char *section,
192 const char *lvalue,
193 int ltype,
194 const char *rvalue,
195 void *data,
196 void *userdata) {
197
198 Unit *u = userdata;
199 char *k;
200 int r;
201
202 assert(filename);
203 assert(lvalue);
204 assert(rvalue);
205 assert(u);
206
207 k = unit_full_printf(u, rvalue);
208 if (!k)
209 return -ENOMEM;
210
211 r = config_parse_strv(filename, line, section, lvalue, ltype, k, data, userdata);
212 free(k);
213
214 return r;
215}
216
f975e971 217int config_parse_unit_path_printf(
6ea832a2
LP
218 const char *filename,
219 unsigned line,
220 const char *section,
221 const char *lvalue,
222 int ltype,
223 const char *rvalue,
224 void *data,
225 void *userdata) {
226
227 Unit *u = userdata;
228 char **s = data;
229 char *k;
230
231 assert(filename);
232 assert(lvalue);
233 assert(rvalue);
234 assert(s);
235 assert(u);
236
237 if (!(k = unit_full_printf(u, rvalue)))
238 return -ENOMEM;
239
240 if (!path_is_absolute(k)) {
241 log_error("[%s:%u] Not an absolute path: %s", filename, line, k);
242 free(k);
243 return -EINVAL;
244 }
245
246 path_kill_slashes(k);
247
248 free(*s);
249 *s = k;
250
251 return 0;
252}
253
f975e971 254int config_parse_socket_listen(
42f4e3c4
LP
255 const char *filename,
256 unsigned line,
257 const char *section,
258 const char *lvalue,
2b583ce6 259 int ltype,
42f4e3c4
LP
260 const char *rvalue,
261 void *data,
262 void *userdata) {
263
49f91047 264 SocketPort *p, *tail;
542563ba 265 Socket *s;
16354eff 266
42f4e3c4
LP
267 assert(filename);
268 assert(lvalue);
269 assert(rvalue);
270 assert(data);
271
542563ba
LP
272 s = (Socket*) data;
273
274 if (!(p = new0(SocketPort, 1)))
275 return -ENOMEM;
276
277 if (streq(lvalue, "ListenFIFO")) {
278 p->type = SOCKET_FIFO;
279
1fd45a90 280 if (!(p->path = unit_full_printf(UNIT(s), rvalue))) {
542563ba
LP
281 free(p);
282 return -ENOMEM;
283 }
01f78473
LP
284
285 path_kill_slashes(p->path);
b0a3f2bc
LP
286
287 } else if (streq(lvalue, "ListenSpecial")) {
288 p->type = SOCKET_SPECIAL;
289
1fd45a90 290 if (!(p->path = unit_full_printf(UNIT(s), rvalue))) {
b0a3f2bc
LP
291 free(p);
292 return -ENOMEM;
293 }
294
295 path_kill_slashes(p->path);
296
916abb21
LP
297 } else if (streq(lvalue, "ListenMessageQueue")) {
298
299 p->type = SOCKET_MQUEUE;
300
1fd45a90 301 if (!(p->path = unit_full_printf(UNIT(s), rvalue))) {
916abb21
LP
302 free(p);
303 return -ENOMEM;
304 }
305
306 path_kill_slashes(p->path);
307
7a22745a 308 } else if (streq(lvalue, "ListenNetlink")) {
1fd45a90
LP
309 char *k;
310 int r;
311
7a22745a 312 p->type = SOCKET_SOCKET;
1fd45a90
LP
313 k = unit_full_printf(UNIT(s), rvalue);
314 r = socket_address_parse_netlink(&p->address, k);
315 free(k);
7a22745a 316
1fd45a90 317 if (r < 0) {
7a22745a
LP
318 log_error("[%s:%u] Failed to parse address value, ignoring: %s", filename, line, rvalue);
319 free(p);
320 return 0;
321 }
322
542563ba 323 } else {
1fd45a90
LP
324 char *k;
325 int r;
326
542563ba 327 p->type = SOCKET_SOCKET;
1fd45a90
LP
328 k = unit_full_printf(UNIT(s), rvalue);
329 r = socket_address_parse(&p->address, k);
330 free(k);
542563ba 331
1fd45a90 332 if (r < 0) {
c0b34696 333 log_error("[%s:%u] Failed to parse address value, ignoring: %s", filename, line, rvalue);
542563ba 334 free(p);
c0b34696 335 return 0;
542563ba
LP
336 }
337
338 if (streq(lvalue, "ListenStream"))
339 p->address.type = SOCK_STREAM;
340 else if (streq(lvalue, "ListenDatagram"))
341 p->address.type = SOCK_DGRAM;
342 else {
343 assert(streq(lvalue, "ListenSequentialPacket"));
344 p->address.type = SOCK_SEQPACKET;
345 }
346
347 if (socket_address_family(&p->address) != AF_LOCAL && p->address.type == SOCK_SEQPACKET) {
c0b34696 348 log_error("[%s:%u] Address family not supported, ignoring: %s", filename, line, rvalue);
542563ba 349 free(p);
c0b34696 350 return 0;
542563ba 351 }
16354eff
LP
352 }
353
542563ba 354 p->fd = -1;
49f91047
LP
355
356 if (s->ports) {
357 LIST_FIND_TAIL(SocketPort, port, s->ports, tail);
358 LIST_INSERT_AFTER(SocketPort, port, s->ports, tail, p);
359 } else
360 LIST_PREPEND(SocketPort, port, s->ports, p);
542563ba 361
16354eff 362 return 0;
42f4e3c4
LP
363}
364
f975e971 365int config_parse_socket_bind(
42f4e3c4
LP
366 const char *filename,
367 unsigned line,
368 const char *section,
369 const char *lvalue,
2b583ce6 370 int ltype,
42f4e3c4
LP
371 const char *rvalue,
372 void *data,
373 void *userdata) {
374
542563ba 375 Socket *s;
c0120d99 376 SocketAddressBindIPv6Only b;
42f4e3c4
LP
377
378 assert(filename);
379 assert(lvalue);
380 assert(rvalue);
381 assert(data);
382
542563ba
LP
383 s = (Socket*) data;
384
c0120d99
LP
385 if ((b = socket_address_bind_ipv6_only_from_string(rvalue)) < 0) {
386 int r;
387
388 if ((r = parse_boolean(rvalue)) < 0) {
c0b34696
LP
389 log_error("[%s:%u] Failed to parse bind IPv6 only value, ignoring: %s", filename, line, rvalue);
390 return 0;
c0120d99 391 }
42f4e3c4 392
c0120d99
LP
393 s->bind_ipv6_only = r ? SOCKET_ADDRESS_IPV6_ONLY : SOCKET_ADDRESS_BOTH;
394 } else
395 s->bind_ipv6_only = b;
542563ba 396
42f4e3c4
LP
397 return 0;
398}
399
f975e971 400int config_parse_exec_nice(
034c6ed7
LP
401 const char *filename,
402 unsigned line,
403 const char *section,
404 const char *lvalue,
2b583ce6 405 int ltype,
034c6ed7
LP
406 const char *rvalue,
407 void *data,
408 void *userdata) {
409
fb33a393 410 ExecContext *c = data;
bd40a2d8 411 int priority;
034c6ed7
LP
412
413 assert(filename);
414 assert(lvalue);
415 assert(rvalue);
416 assert(data);
417
bd40a2d8 418 if (safe_atoi(rvalue, &priority) < 0) {
c0b34696
LP
419 log_error("[%s:%u] Failed to parse nice priority, ignoring: %s. ", filename, line, rvalue);
420 return 0;
034c6ed7
LP
421 }
422
423 if (priority < PRIO_MIN || priority >= PRIO_MAX) {
c0b34696
LP
424 log_error("[%s:%u] Nice priority out of range, ignoring: %s", filename, line, rvalue);
425 return 0;
034c6ed7
LP
426 }
427
fb33a393 428 c->nice = priority;
71155933 429 c->nice_set = true;
fb33a393 430
034c6ed7
LP
431 return 0;
432}
433
f975e971 434int config_parse_exec_oom_score_adjust(
034c6ed7
LP
435 const char *filename,
436 unsigned line,
437 const char *section,
438 const char *lvalue,
2b583ce6 439 int ltype,
034c6ed7
LP
440 const char *rvalue,
441 void *data,
442 void *userdata) {
443
fb33a393 444 ExecContext *c = data;
bd40a2d8 445 int oa;
034c6ed7
LP
446
447 assert(filename);
448 assert(lvalue);
449 assert(rvalue);
450 assert(data);
451
bd40a2d8 452 if (safe_atoi(rvalue, &oa) < 0) {
dd6c17b1 453 log_error("[%s:%u] Failed to parse the OOM score adjust value, ignoring: %s", filename, line, rvalue);
c0b34696 454 return 0;
034c6ed7
LP
455 }
456
dd6c17b1
LP
457 if (oa < OOM_SCORE_ADJ_MIN || oa > OOM_SCORE_ADJ_MAX) {
458 log_error("[%s:%u] OOM score adjust value out of range, ignoring: %s", filename, line, rvalue);
c0b34696 459 return 0;
034c6ed7
LP
460 }
461
dd6c17b1
LP
462 c->oom_score_adjust = oa;
463 c->oom_score_adjust_set = true;
fb33a393 464
034c6ed7
LP
465 return 0;
466}
467
f975e971 468int config_parse_exec(
034c6ed7
LP
469 const char *filename,
470 unsigned line,
471 const char *section,
472 const char *lvalue,
2b583ce6 473 int ltype,
034c6ed7
LP
474 const char *rvalue,
475 void *data,
476 void *userdata) {
477
61e5d8ed
LP
478 ExecCommand **e = data, *nce;
479 char *path, **n;
034c6ed7 480 unsigned k;
034c6ed7
LP
481
482 assert(filename);
483 assert(lvalue);
484 assert(rvalue);
61e5d8ed 485 assert(e);
034c6ed7 486
6c666e26
LP
487 /* We accept an absolute path as first argument, or
488 * alternatively an absolute prefixed with @ to allow
489 * overriding of argv[0]. */
490
f975e971
LP
491 e += ltype;
492
61e5d8ed
LP
493 for (;;) {
494 char *w;
495 size_t l;
496 char *state;
b708e7ce 497 bool honour_argv0 = false, ignore = false;
6c666e26 498
61e5d8ed
LP
499 path = NULL;
500 nce = NULL;
501 n = NULL;
6c666e26 502
61e5d8ed 503 rvalue += strspn(rvalue, WHITESPACE);
034c6ed7 504
61e5d8ed
LP
505 if (rvalue[0] == 0)
506 break;
034c6ed7 507
b708e7ce
LP
508 if (rvalue[0] == '-') {
509 ignore = true;
510 rvalue ++;
511 }
512
513 if (rvalue[0] == '@') {
514 honour_argv0 = true;
515 rvalue ++;
516 }
61e5d8ed 517
b708e7ce 518 if (*rvalue != '/') {
c0b34696
LP
519 log_error("[%s:%u] Invalid executable path in command line, ignoring: %s", filename, line, rvalue);
520 return 0;
6c666e26 521 }
034c6ed7 522
61e5d8ed
LP
523 k = 0;
524 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
f90cf44c 525 if (strncmp(w, ";", MAX(l, 1U)) == 0)
61e5d8ed 526 break;
034c6ed7 527
61e5d8ed
LP
528 k++;
529 }
034c6ed7 530
b708e7ce 531 if (!(n = new(char*, k + !honour_argv0)))
61e5d8ed
LP
532 return -ENOMEM;
533
534 k = 0;
61e5d8ed 535 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
f90cf44c 536 if (strncmp(w, ";", MAX(l, 1U)) == 0)
61e5d8ed
LP
537 break;
538
b708e7ce
LP
539 if (honour_argv0 && w == rvalue) {
540 assert(!path);
541 if (!(path = cunescape_length(w, l)))
61e5d8ed 542 goto fail;
61e5d8ed
LP
543 } else {
544 if (!(n[k++] = cunescape_length(w, l)))
545 goto fail;
546 }
547 }
548
549 n[k] = NULL;
550
551 if (!n[0]) {
c0b34696 552 log_error("[%s:%u] Invalid command line, ignoring: %s", filename, line, rvalue);
61e5d8ed 553 strv_free(n);
8ea913b2 554 free(path);
c0b34696 555 return 0;
61e5d8ed
LP
556 }
557
558 if (!path)
559 if (!(path = strdup(n[0])))
560 goto fail;
6c666e26 561
61e5d8ed 562 assert(path_is_absolute(path));
6c666e26 563
61e5d8ed
LP
564 if (!(nce = new0(ExecCommand, 1)))
565 goto fail;
566
567 nce->argv = n;
568 nce->path = path;
b708e7ce 569 nce->ignore = ignore;
034c6ed7 570
61e5d8ed 571 path_kill_slashes(nce->path);
034c6ed7 572
61e5d8ed 573 exec_command_append_list(e, nce);
01f78473 574
61e5d8ed
LP
575 rvalue = state;
576 }
034c6ed7
LP
577
578 return 0;
579
580fail:
6c666e26
LP
581 n[k] = NULL;
582 strv_free(n);
583 free(path);
034c6ed7
LP
584 free(nce);
585
586 return -ENOMEM;
587}
588
f975e971
LP
589DEFINE_CONFIG_PARSE_ENUM(config_parse_service_type, service_type, ServiceType, "Failed to parse service type");
590DEFINE_CONFIG_PARSE_ENUM(config_parse_service_restart, service_restart, ServiceRestart, "Failed to parse service restart specifier");
034c6ed7 591
f975e971 592int config_parse_socket_bindtodevice(
acbb0225
LP
593 const char *filename,
594 unsigned line,
595 const char *section,
596 const char *lvalue,
2b583ce6 597 int ltype,
acbb0225
LP
598 const char *rvalue,
599 void *data,
600 void *userdata) {
601
602 Socket *s = data;
603 char *n;
604
605 assert(filename);
606 assert(lvalue);
607 assert(rvalue);
608 assert(data);
609
610 if (rvalue[0] && !streq(rvalue, "*")) {
611 if (!(n = strdup(rvalue)))
612 return -ENOMEM;
613 } else
614 n = NULL;
615
616 free(s->bind_to_device);
617 s->bind_to_device = n;
618
619 return 0;
620}
621
f975e971
LP
622DEFINE_CONFIG_PARSE_ENUM(config_parse_output, exec_output, ExecOutput, "Failed to parse output specifier");
623DEFINE_CONFIG_PARSE_ENUM(config_parse_input, exec_input, ExecInput, "Failed to parse input specifier");
87f0e418 624
f975e971 625int config_parse_facility(
071830ff
LP
626 const char *filename,
627 unsigned line,
628 const char *section,
629 const char *lvalue,
2b583ce6 630 int ltype,
071830ff
LP
631 const char *rvalue,
632 void *data,
633 void *userdata) {
634
071830ff 635
94f04347 636 int *o = data, x;
071830ff
LP
637
638 assert(filename);
639 assert(lvalue);
640 assert(rvalue);
641 assert(data);
642
7d76f312 643 if ((x = log_facility_unshifted_from_string(rvalue)) < 0) {
c0b34696
LP
644 log_error("[%s:%u] Failed to parse log facility, ignoring: %s", filename, line, rvalue);
645 return 0;
0d87eb42 646 }
94f04347 647
7d76f312 648 *o = (x << 3) | LOG_PRI(*o);
071830ff
LP
649
650 return 0;
651}
652
f975e971 653int config_parse_level(
071830ff
LP
654 const char *filename,
655 unsigned line,
656 const char *section,
657 const char *lvalue,
2b583ce6 658 int ltype,
071830ff
LP
659 const char *rvalue,
660 void *data,
661 void *userdata) {
662
071830ff 663
94f04347 664 int *o = data, x;
071830ff
LP
665
666 assert(filename);
667 assert(lvalue);
668 assert(rvalue);
669 assert(data);
670
0d87eb42 671 if ((x = log_level_from_string(rvalue)) < 0) {
c0b34696
LP
672 log_error("[%s:%u] Failed to parse log level, ignoring: %s", filename, line, rvalue);
673 return 0;
0d87eb42 674 }
071830ff 675
7d76f312 676 *o = (*o & LOG_FACMASK) | x;
94f04347
LP
677 return 0;
678}
679
f975e971 680int config_parse_exec_io_class(
94f04347
LP
681 const char *filename,
682 unsigned line,
683 const char *section,
684 const char *lvalue,
2b583ce6 685 int ltype,
94f04347
LP
686 const char *rvalue,
687 void *data,
688 void *userdata) {
689
690 ExecContext *c = data;
691 int x;
692
693 assert(filename);
694 assert(lvalue);
695 assert(rvalue);
696 assert(data);
697
0d87eb42 698 if ((x = ioprio_class_from_string(rvalue)) < 0) {
c0b34696
LP
699 log_error("[%s:%u] Failed to parse IO scheduling class, ignoring: %s", filename, line, rvalue);
700 return 0;
0d87eb42 701 }
94f04347
LP
702
703 c->ioprio = IOPRIO_PRIO_VALUE(x, IOPRIO_PRIO_DATA(c->ioprio));
704 c->ioprio_set = true;
705
706 return 0;
707}
708
f975e971 709int config_parse_exec_io_priority(
94f04347
LP
710 const char *filename,
711 unsigned line,
712 const char *section,
713 const char *lvalue,
2b583ce6 714 int ltype,
94f04347
LP
715 const char *rvalue,
716 void *data,
717 void *userdata) {
718
719 ExecContext *c = data;
720 int i;
721
722 assert(filename);
723 assert(lvalue);
724 assert(rvalue);
725 assert(data);
726
727 if (safe_atoi(rvalue, &i) < 0 || i < 0 || i >= IOPRIO_BE_NR) {
c0b34696
LP
728 log_error("[%s:%u] Failed to parse io priority, ignoring: %s", filename, line, rvalue);
729 return 0;
071830ff
LP
730 }
731
94f04347
LP
732 c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_PRIO_CLASS(c->ioprio), i);
733 c->ioprio_set = true;
734
071830ff
LP
735 return 0;
736}
737
f975e971 738int config_parse_exec_cpu_sched_policy(
9eba9da4
LP
739 const char *filename,
740 unsigned line,
741 const char *section,
742 const char *lvalue,
2b583ce6 743 int ltype,
9eba9da4
LP
744 const char *rvalue,
745 void *data,
746 void *userdata) {
747
94f04347
LP
748
749 ExecContext *c = data;
750 int x;
751
752 assert(filename);
753 assert(lvalue);
754 assert(rvalue);
755 assert(data);
756
0d87eb42 757 if ((x = sched_policy_from_string(rvalue)) < 0) {
c0b34696
LP
758 log_error("[%s:%u] Failed to parse CPU scheduling policy, ignoring: %s", filename, line, rvalue);
759 return 0;
0d87eb42 760 }
94f04347
LP
761
762 c->cpu_sched_policy = x;
763 c->cpu_sched_set = true;
764
765 return 0;
766}
767
f975e971 768int config_parse_exec_cpu_sched_prio(
94f04347
LP
769 const char *filename,
770 unsigned line,
771 const char *section,
772 const char *lvalue,
2b583ce6 773 int ltype,
94f04347
LP
774 const char *rvalue,
775 void *data,
776 void *userdata) {
9eba9da4
LP
777
778 ExecContext *c = data;
779 int i;
780
781 assert(filename);
782 assert(lvalue);
783 assert(rvalue);
784 assert(data);
785
94f04347
LP
786 /* On Linux RR/FIFO have the same range */
787 if (safe_atoi(rvalue, &i) < 0 || i < sched_get_priority_min(SCHED_RR) || i > sched_get_priority_max(SCHED_RR)) {
c0b34696
LP
788 log_error("[%s:%u] Failed to parse CPU scheduling priority, ignoring: %s", filename, line, rvalue);
789 return 0;
94f04347 790 }
9eba9da4 791
94f04347
LP
792 c->cpu_sched_priority = i;
793 c->cpu_sched_set = true;
794
795 return 0;
796}
797
f975e971 798int config_parse_exec_cpu_affinity(
94f04347
LP
799 const char *filename,
800 unsigned line,
801 const char *section,
802 const char *lvalue,
2b583ce6 803 int ltype,
94f04347
LP
804 const char *rvalue,
805 void *data,
806 void *userdata) {
807
808 ExecContext *c = data;
809 char *w;
810 size_t l;
811 char *state;
812
813 assert(filename);
814 assert(lvalue);
815 assert(rvalue);
816 assert(data);
817
f60f22df 818 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
94f04347
LP
819 char *t;
820 int r;
821 unsigned cpu;
822
823 if (!(t = strndup(w, l)))
824 return -ENOMEM;
825
487393e9
LP
826 r = safe_atou(t, &cpu);
827 free(t);
828
82c121a4
LP
829 if (!(c->cpuset))
830 if (!(c->cpuset = cpu_set_malloc(&c->cpuset_ncpus)))
831 return -ENOMEM;
832
82c121a4 833 if (r < 0 || cpu >= c->cpuset_ncpus) {
c0b34696
LP
834 log_error("[%s:%u] Failed to parse CPU affinity, ignoring: %s", filename, line, rvalue);
835 return 0;
9eba9da4 836 }
94f04347 837
82c121a4 838 CPU_SET_S(cpu, CPU_ALLOC_SIZE(c->cpuset_ncpus), c->cpuset);
9eba9da4
LP
839 }
840
94f04347
LP
841 return 0;
842}
843
f975e971 844int config_parse_exec_capabilities(
94f04347
LP
845 const char *filename,
846 unsigned line,
847 const char *section,
848 const char *lvalue,
2b583ce6 849 int ltype,
94f04347
LP
850 const char *rvalue,
851 void *data,
852 void *userdata) {
853
854 ExecContext *c = data;
855 cap_t cap;
856
857 assert(filename);
858 assert(lvalue);
859 assert(rvalue);
860 assert(data);
861
862 if (!(cap = cap_from_text(rvalue))) {
863 if (errno == ENOMEM)
864 return -ENOMEM;
865
c0b34696
LP
866 log_error("[%s:%u] Failed to parse capabilities, ignoring: %s", filename, line, rvalue);
867 return 0;
94f04347
LP
868 }
869
870 if (c->capabilities)
871 cap_free(c->capabilities);
872 c->capabilities = cap;
873
874 return 0;
875}
876
f975e971 877int config_parse_exec_secure_bits(
94f04347
LP
878 const char *filename,
879 unsigned line,
880 const char *section,
881 const char *lvalue,
2b583ce6 882 int ltype,
94f04347
LP
883 const char *rvalue,
884 void *data,
885 void *userdata) {
886
887 ExecContext *c = data;
888 char *w;
889 size_t l;
890 char *state;
891
892 assert(filename);
893 assert(lvalue);
894 assert(rvalue);
895 assert(data);
896
f60f22df 897 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
94f04347
LP
898 if (first_word(w, "keep-caps"))
899 c->secure_bits |= SECURE_KEEP_CAPS;
900 else if (first_word(w, "keep-caps-locked"))
901 c->secure_bits |= SECURE_KEEP_CAPS_LOCKED;
902 else if (first_word(w, "no-setuid-fixup"))
903 c->secure_bits |= SECURE_NO_SETUID_FIXUP;
904 else if (first_word(w, "no-setuid-fixup-locked"))
905 c->secure_bits |= SECURE_NO_SETUID_FIXUP_LOCKED;
906 else if (first_word(w, "noroot"))
907 c->secure_bits |= SECURE_NOROOT;
908 else if (first_word(w, "noroot-locked"))
909 c->secure_bits |= SECURE_NOROOT_LOCKED;
9eba9da4 910 else {
c0b34696
LP
911 log_error("[%s:%u] Failed to parse secure bits, ignoring: %s", filename, line, rvalue);
912 return 0;
9eba9da4
LP
913 }
914 }
915
94f04347
LP
916 return 0;
917}
918
f975e971 919int config_parse_exec_bounding_set(
94f04347
LP
920 const char *filename,
921 unsigned line,
922 const char *section,
923 const char *lvalue,
2b583ce6 924 int ltype,
94f04347
LP
925 const char *rvalue,
926 void *data,
927 void *userdata) {
928
929 ExecContext *c = data;
930 char *w;
931 size_t l;
932 char *state;
260abb78
LP
933 bool invert = false;
934 uint64_t sum = 0;
94f04347
LP
935
936 assert(filename);
937 assert(lvalue);
938 assert(rvalue);
939 assert(data);
940
260abb78
LP
941 if (rvalue[0] == '~') {
942 invert = true;
943 rvalue++;
944 }
945
946 /* Note that we store this inverted internally, since the
947 * kernel wants it like this. But we actually expose it
948 * non-inverted everywhere to have a fully normalized
949 * interface. */
950
f60f22df 951 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
94f04347
LP
952 char *t;
953 int r;
954 cap_value_t cap;
955
956 if (!(t = strndup(w, l)))
957 return -ENOMEM;
958
959 r = cap_from_name(t, &cap);
960 free(t);
961
962 if (r < 0) {
c0b34696
LP
963 log_error("[%s:%u] Failed to parse capability bounding set, ignoring: %s", filename, line, rvalue);
964 return 0;
94f04347
LP
965 }
966
260abb78 967 sum |= ((uint64_t) 1ULL) << (uint64_t) cap;
94f04347 968 }
9eba9da4 969
260abb78
LP
970 if (invert)
971 c->capability_bounding_set_drop |= sum;
972 else
973 c->capability_bounding_set_drop |= ~sum;
974
9eba9da4
LP
975 return 0;
976}
977
f975e971 978int config_parse_exec_timer_slack_nsec(
9eba9da4
LP
979 const char *filename,
980 unsigned line,
981 const char *section,
982 const char *lvalue,
2b583ce6 983 int ltype,
9eba9da4
LP
984 const char *rvalue,
985 void *data,
986 void *userdata) {
987
988 ExecContext *c = data;
94f04347 989 unsigned long u;
9eba9da4
LP
990
991 assert(filename);
992 assert(lvalue);
993 assert(rvalue);
994 assert(data);
995
bd40a2d8 996 if (safe_atolu(rvalue, &u) < 0) {
c0b34696
LP
997 log_error("[%s:%u] Failed to parse time slack value, ignoring: %s", filename, line, rvalue);
998 return 0;
9eba9da4
LP
999 }
1000
03fae018 1001 c->timer_slack_nsec = u;
94f04347
LP
1002
1003 return 0;
1004}
1005
f975e971 1006int config_parse_limit(
94f04347
LP
1007 const char *filename,
1008 unsigned line,
1009 const char *section,
1010 const char *lvalue,
2b583ce6 1011 int ltype,
94f04347
LP
1012 const char *rvalue,
1013 void *data,
1014 void *userdata) {
1015
1016 struct rlimit **rl = data;
1017 unsigned long long u;
94f04347
LP
1018
1019 assert(filename);
1020 assert(lvalue);
1021 assert(rvalue);
1022 assert(data);
1023
f975e971
LP
1024 rl += ltype;
1025
3d57c6ab
LP
1026 if (streq(rvalue, "infinity"))
1027 u = (unsigned long long) RLIM_INFINITY;
1028 else if (safe_atollu(rvalue, &u) < 0) {
c0b34696
LP
1029 log_error("[%s:%u] Failed to parse resource value, ignoring: %s", filename, line, rvalue);
1030 return 0;
94f04347
LP
1031 }
1032
1033 if (!*rl)
1034 if (!(*rl = new(struct rlimit, 1)))
1035 return -ENOMEM;
9eba9da4 1036
94f04347 1037 (*rl)->rlim_cur = (*rl)->rlim_max = (rlim_t) u;
9eba9da4
LP
1038 return 0;
1039}
1040
f975e971 1041int config_parse_unit_cgroup(
8e274523
LP
1042 const char *filename,
1043 unsigned line,
1044 const char *section,
1045 const char *lvalue,
2b583ce6 1046 int ltype,
8e274523
LP
1047 const char *rvalue,
1048 void *data,
1049 void *userdata) {
1050
1051 Unit *u = userdata;
1052 char *w;
1053 size_t l;
1054 char *state;
1055
f60f22df 1056 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
f284f69a 1057 char *t, *k;
8e274523
LP
1058 int r;
1059
f284f69a
LP
1060 t = strndup(w, l);
1061 if (!t)
1062 return -ENOMEM;
1063
1064 k = unit_full_printf(u, t);
1065 free(t);
1066
1067 if (!k)
1068 return -ENOMEM;
1069
1070 t = cunescape(k);
1071 free(k);
1072
1073 if (!t)
8e274523
LP
1074 return -ENOMEM;
1075
1076 r = unit_add_cgroup_from_text(u, t);
1077 free(t);
1078
c0b34696
LP
1079 if (r < 0) {
1080 log_error("[%s:%u] Failed to parse cgroup value, ignoring: %s", filename, line, rvalue);
1081 return 0;
1082 }
8e274523
LP
1083 }
1084
1085 return 0;
1086}
1087
07459bb6 1088#ifdef HAVE_SYSV_COMPAT
f975e971 1089int config_parse_sysv_priority(
a9a1e00a
LP
1090 const char *filename,
1091 unsigned line,
1092 const char *section,
1093 const char *lvalue,
2b583ce6 1094 int ltype,
a9a1e00a
LP
1095 const char *rvalue,
1096 void *data,
1097 void *userdata) {
1098
1099 int *priority = data;
bd40a2d8 1100 int i;
a9a1e00a
LP
1101
1102 assert(filename);
1103 assert(lvalue);
1104 assert(rvalue);
1105 assert(data);
1106
bd40a2d8 1107 if (safe_atoi(rvalue, &i) < 0 || i < 0) {
c0b34696
LP
1108 log_error("[%s:%u] Failed to parse SysV start priority, ignoring: %s", filename, line, rvalue);
1109 return 0;
a9a1e00a
LP
1110 }
1111
1112 *priority = (int) i;
1113 return 0;
1114}
07459bb6 1115#endif
a9a1e00a 1116
f975e971 1117int config_parse_fsck_passno(
2ba545f1
LP
1118 const char *filename,
1119 unsigned line,
1120 const char *section,
1121 const char *lvalue,
2b583ce6 1122 int ltype,
2ba545f1
LP
1123 const char *rvalue,
1124 void *data,
1125 void *userdata) {
1126
1127 int *passno = data;
bd40a2d8 1128 int i;
2ba545f1
LP
1129
1130 assert(filename);
1131 assert(lvalue);
1132 assert(rvalue);
1133 assert(data);
1134
bd40a2d8 1135 if (safe_atoi(rvalue, &i) || i < 0) {
2ba545f1
LP
1136 log_error("[%s:%u] Failed to parse fsck pass number, ignoring: %s", filename, line, rvalue);
1137 return 0;
1138 }
1139
1140 *passno = (int) i;
1141 return 0;
1142}
1143
f975e971 1144DEFINE_CONFIG_PARSE_ENUM(config_parse_kill_mode, kill_mode, KillMode, "Failed to parse kill mode");
50159e6a 1145
f975e971 1146int config_parse_kill_signal(
2e22afe9
LP
1147 const char *filename,
1148 unsigned line,
1149 const char *section,
1150 const char *lvalue,
2b583ce6 1151 int ltype,
2e22afe9
LP
1152 const char *rvalue,
1153 void *data,
1154 void *userdata) {
1155
1156 int *sig = data;
1157 int r;
1158
1159 assert(filename);
1160 assert(lvalue);
1161 assert(rvalue);
1162 assert(sig);
1163
8a0867d6 1164 if ((r = signal_from_string_try_harder(rvalue)) <= 0) {
c0b34696
LP
1165 log_error("[%s:%u] Failed to parse kill signal, ignoring: %s", filename, line, rvalue);
1166 return 0;
2e22afe9
LP
1167 }
1168
1169 *sig = r;
1170 return 0;
1171}
1172
f975e971 1173int config_parse_exec_mount_flags(
15ae422b
LP
1174 const char *filename,
1175 unsigned line,
1176 const char *section,
1177 const char *lvalue,
2b583ce6 1178 int ltype,
15ae422b
LP
1179 const char *rvalue,
1180 void *data,
1181 void *userdata) {
1182
1183 ExecContext *c = data;
1184 char *w;
1185 size_t l;
1186 char *state;
1187 unsigned long flags = 0;
1188
1189 assert(filename);
1190 assert(lvalue);
1191 assert(rvalue);
1192 assert(data);
1193
f60f22df 1194 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
f90cf44c 1195 if (strncmp(w, "shared", MAX(l, 6U)) == 0)
15ae422b 1196 flags |= MS_SHARED;
f90cf44c 1197 else if (strncmp(w, "slave", MAX(l, 5U)) == 0)
15ae422b 1198 flags |= MS_SLAVE;
f90cf44c 1199 else if (strncmp(w, "private", MAX(l, 7U)) == 0)
15ae422b
LP
1200 flags |= MS_PRIVATE;
1201 else {
c0b34696
LP
1202 log_error("[%s:%u] Failed to parse mount flags, ignoring: %s", filename, line, rvalue);
1203 return 0;
15ae422b
LP
1204 }
1205 }
1206
1207 c->mount_flags = flags;
1208 return 0;
1209}
1210
f975e971 1211int config_parse_timer(
871d7de4
LP
1212 const char *filename,
1213 unsigned line,
1214 const char *section,
1215 const char *lvalue,
2b583ce6 1216 int ltype,
871d7de4
LP
1217 const char *rvalue,
1218 void *data,
1219 void *userdata) {
1220
1221 Timer *t = data;
1222 usec_t u;
871d7de4
LP
1223 TimerValue *v;
1224 TimerBase b;
1225
1226 assert(filename);
1227 assert(lvalue);
1228 assert(rvalue);
1229 assert(data);
1230
1231 if ((b = timer_base_from_string(lvalue)) < 0) {
c0b34696
LP
1232 log_error("[%s:%u] Failed to parse timer base, ignoring: %s", filename, line, lvalue);
1233 return 0;
871d7de4
LP
1234 }
1235
bd40a2d8 1236 if (parse_usec(rvalue, &u) < 0) {
c0b34696
LP
1237 log_error("[%s:%u] Failed to parse timer value, ignoring: %s", filename, line, rvalue);
1238 return 0;
871d7de4
LP
1239 }
1240
1241 if (!(v = new0(TimerValue, 1)))
1242 return -ENOMEM;
1243
1244 v->base = b;
1245 v->value = u;
1246
1247 LIST_PREPEND(TimerValue, value, t->values, v);
1248
1249 return 0;
1250}
1251
f975e971 1252int config_parse_timer_unit(
871d7de4
LP
1253 const char *filename,
1254 unsigned line,
1255 const char *section,
1256 const char *lvalue,
2b583ce6 1257 int ltype,
871d7de4
LP
1258 const char *rvalue,
1259 void *data,
1260 void *userdata) {
1261
1262 Timer *t = data;
1263 int r;
398ef8ba 1264 DBusError error;
57020a3a 1265 Unit *u;
398ef8ba
LP
1266
1267 assert(filename);
1268 assert(lvalue);
1269 assert(rvalue);
1270 assert(data);
1271
1272 dbus_error_init(&error);
871d7de4
LP
1273
1274 if (endswith(rvalue, ".timer")) {
c0b34696
LP
1275 log_error("[%s:%u] Unit cannot be of type timer, ignoring: %s", filename, line, rvalue);
1276 return 0;
871d7de4
LP
1277 }
1278
57020a3a
LP
1279 r = manager_load_unit(t->meta.manager, rvalue, NULL, NULL, &u);
1280 if (r < 0) {
c0b34696 1281 log_error("[%s:%u] Failed to load unit %s, ignoring: %s", filename, line, rvalue, bus_error(&error, r));
398ef8ba 1282 dbus_error_free(&error);
c0b34696 1283 return 0;
871d7de4
LP
1284 }
1285
57020a3a
LP
1286 unit_ref_set(&t->unit, u);
1287
871d7de4
LP
1288 return 0;
1289}
1290
f975e971 1291int config_parse_path_spec(
01f78473
LP
1292 const char *filename,
1293 unsigned line,
1294 const char *section,
1295 const char *lvalue,
2b583ce6 1296 int ltype,
01f78473
LP
1297 const char *rvalue,
1298 void *data,
1299 void *userdata) {
1300
1301 Path *p = data;
1302 PathSpec *s;
1303 PathType b;
1304
1305 assert(filename);
1306 assert(lvalue);
1307 assert(rvalue);
1308 assert(data);
1309
1310 if ((b = path_type_from_string(lvalue)) < 0) {
c0b34696
LP
1311 log_error("[%s:%u] Failed to parse path type, ignoring: %s", filename, line, lvalue);
1312 return 0;
01f78473
LP
1313 }
1314
1315 if (!path_is_absolute(rvalue)) {
c0b34696
LP
1316 log_error("[%s:%u] Path is not absolute, ignoring: %s", filename, line, rvalue);
1317 return 0;
01f78473
LP
1318 }
1319
1320 if (!(s = new0(PathSpec, 1)))
1321 return -ENOMEM;
1322
1323 if (!(s->path = strdup(rvalue))) {
1324 free(s);
1325 return -ENOMEM;
1326 }
1327
1328 path_kill_slashes(s->path);
1329
1330 s->type = b;
1331 s->inotify_fd = -1;
1332
1333 LIST_PREPEND(PathSpec, spec, p->specs, s);
1334
1335 return 0;
1336}
1337
f975e971 1338int config_parse_path_unit(
01f78473
LP
1339 const char *filename,
1340 unsigned line,
1341 const char *section,
1342 const char *lvalue,
2b583ce6 1343 int ltype,
01f78473
LP
1344 const char *rvalue,
1345 void *data,
1346 void *userdata) {
1347
1348 Path *t = data;
1349 int r;
398ef8ba 1350 DBusError error;
57020a3a 1351 Unit *u;
398ef8ba
LP
1352
1353 assert(filename);
1354 assert(lvalue);
1355 assert(rvalue);
1356 assert(data);
1357
1358 dbus_error_init(&error);
01f78473
LP
1359
1360 if (endswith(rvalue, ".path")) {
c0b34696
LP
1361 log_error("[%s:%u] Unit cannot be of type path, ignoring: %s", filename, line, rvalue);
1362 return 0;
01f78473
LP
1363 }
1364
57020a3a 1365 if ((r = manager_load_unit(t->meta.manager, rvalue, NULL, &error, &u)) < 0) {
c0b34696 1366 log_error("[%s:%u] Failed to load unit %s, ignoring: %s", filename, line, rvalue, bus_error(&error, r));
398ef8ba 1367 dbus_error_free(&error);
c0b34696 1368 return 0;
01f78473
LP
1369 }
1370
57020a3a
LP
1371 unit_ref_set(&t->unit, u);
1372
01f78473
LP
1373 return 0;
1374}
1375
f975e971 1376int config_parse_socket_service(
d9ff321a
LP
1377 const char *filename,
1378 unsigned line,
1379 const char *section,
1380 const char *lvalue,
2b583ce6 1381 int ltype,
d9ff321a
LP
1382 const char *rvalue,
1383 void *data,
1384 void *userdata) {
1385
1386 Socket *s = data;
1387 int r;
1388 DBusError error;
4ff77f66 1389 Unit *x;
d9ff321a
LP
1390
1391 assert(filename);
1392 assert(lvalue);
1393 assert(rvalue);
1394 assert(data);
1395
1396 dbus_error_init(&error);
1397
f976f3f6
LP
1398 if (!endswith(rvalue, ".service")) {
1399 log_error("[%s:%u] Unit must be of type service, ignoring: %s", filename, line, rvalue);
d9ff321a
LP
1400 return 0;
1401 }
1402
4ff77f66
LP
1403 r = manager_load_unit(s->meta.manager, rvalue, NULL, &error, &x);
1404 if (r < 0) {
d9ff321a
LP
1405 log_error("[%s:%u] Failed to load unit %s, ignoring: %s", filename, line, rvalue, bus_error(&error, r));
1406 dbus_error_free(&error);
1407 return 0;
1408 }
1409
4ff77f66
LP
1410 unit_ref_set(&s->service, x);
1411
d9ff321a
LP
1412 return 0;
1413}
1414
f975e971 1415int config_parse_service_sockets(
f976f3f6
LP
1416 const char *filename,
1417 unsigned line,
1418 const char *section,
1419 const char *lvalue,
2b583ce6 1420 int ltype,
f976f3f6
LP
1421 const char *rvalue,
1422 void *data,
1423 void *userdata) {
1424
1425 Service *s = data;
1426 int r;
f976f3f6
LP
1427 char *state, *w;
1428 size_t l;
1429
1430 assert(filename);
1431 assert(lvalue);
1432 assert(rvalue);
1433 assert(data);
1434
f976f3f6 1435 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
57020a3a 1436 char *t, *k;
f976f3f6 1437
57020a3a
LP
1438 t = strndup(w, l);
1439 if (!t)
f976f3f6
LP
1440 return -ENOMEM;
1441
57020a3a 1442 k = unit_name_printf(UNIT(s), t);
f976f3f6
LP
1443 free(t);
1444
57020a3a
LP
1445 if (!k)
1446 return -ENOMEM;
1447
1448 if (!endswith(k, ".socket")) {
1449 log_error("[%s:%u] Unit must be of type socket, ignoring: %s", filename, line, rvalue);
1450 free(k);
f976f3f6
LP
1451 continue;
1452 }
1453
57020a3a
LP
1454 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_WANTS, UNIT_AFTER, k, NULL, true);
1455 if (r < 0)
1456 log_error("[%s:%u] Failed to add dependency on %s, ignoring: %s", filename, line, k, strerror(-r));
f976f3f6 1457
57020a3a
LP
1458 r = unit_add_dependency_by_name(UNIT(s), UNIT_TRIGGERED_BY, k, NULL, true);
1459 if (r < 0)
f976f3f6 1460 return r;
57020a3a
LP
1461
1462 free(k);
f976f3f6
LP
1463 }
1464
1465 return 0;
1466}
1467
f975e971 1468int config_parse_unit_env_file(
ddb26e18
LP
1469 const char *filename,
1470 unsigned line,
1471 const char *section,
1472 const char *lvalue,
2b583ce6 1473 int ltype,
ddb26e18
LP
1474 const char *rvalue,
1475 void *data,
1476 void *userdata) {
1477
8c7be95e 1478 char ***env = data, **k;
8fef7659
LP
1479 Unit *u = userdata;
1480 char *s;
ddb26e18
LP
1481
1482 assert(filename);
1483 assert(lvalue);
1484 assert(rvalue);
1485 assert(data);
1486
8fef7659
LP
1487 s = unit_full_printf(u, rvalue);
1488 if (!s)
1489 return -ENOMEM;
1490
1491 if (!path_is_absolute(s[0] == '-' ? s + 1 : s)) {
1492 log_error("[%s:%u] Path '%s' is not absolute, ignoring.", filename, line, s);
1493 free(s);
afe4bfe2
LP
1494 return 0;
1495 }
1496
8fef7659
LP
1497 k = strv_append(*env, s);
1498 free(s);
1499 if (!k)
8c7be95e 1500 return -ENOMEM;
ddb26e18 1501
8c7be95e
LP
1502 strv_free(*env);
1503 *env = k;
ddb26e18 1504
8c7be95e 1505 return 0;
ddb26e18
LP
1506}
1507
f975e971 1508int config_parse_ip_tos(
4fd5948e
LP
1509 const char *filename,
1510 unsigned line,
1511 const char *section,
1512 const char *lvalue,
2b583ce6 1513 int ltype,
4fd5948e
LP
1514 const char *rvalue,
1515 void *data,
1516 void *userdata) {
1517
1518 int *ip_tos = data, x;
4fd5948e
LP
1519
1520 assert(filename);
1521 assert(lvalue);
1522 assert(rvalue);
1523 assert(data);
1524
1525 if ((x = ip_tos_from_string(rvalue)) < 0)
bd40a2d8 1526 if (safe_atoi(rvalue, &x) < 0) {
c0b34696
LP
1527 log_error("[%s:%u] Failed to parse IP TOS value, ignoring: %s", filename, line, rvalue);
1528 return 0;
4fd5948e
LP
1529 }
1530
1531 *ip_tos = x;
1532 return 0;
1533}
1534
f975e971 1535int config_parse_unit_condition_path(
52661efd
LP
1536 const char *filename,
1537 unsigned line,
1538 const char *section,
1539 const char *lvalue,
2b583ce6 1540 int ltype,
52661efd
LP
1541 const char *rvalue,
1542 void *data,
1543 void *userdata) {
1544
2b583ce6 1545 ConditionType cond = ltype;
52661efd 1546 Unit *u = data;
267632f0 1547 bool trigger, negate;
52661efd
LP
1548 Condition *c;
1549
1550 assert(filename);
1551 assert(lvalue);
1552 assert(rvalue);
1553 assert(data);
1554
ab7f148f
LP
1555 trigger = rvalue[0] == '|';
1556 if (trigger)
267632f0
LP
1557 rvalue++;
1558
ab7f148f
LP
1559 negate = rvalue[0] == '!';
1560 if (negate)
52661efd
LP
1561 rvalue++;
1562
1563 if (!path_is_absolute(rvalue)) {
d257ddef 1564 log_error("[%s:%u] Path in condition not absolute, ignoring: %s", filename, line, rvalue);
52661efd
LP
1565 return 0;
1566 }
1567
ab7f148f
LP
1568 c = condition_new(cond, rvalue, trigger, negate);
1569 if (!c)
52661efd
LP
1570 return -ENOMEM;
1571
1572 LIST_PREPEND(Condition, conditions, u->meta.conditions, c);
1573 return 0;
1574}
1575
f975e971 1576int config_parse_unit_condition_string(
039655a4
LP
1577 const char *filename,
1578 unsigned line,
1579 const char *section,
1580 const char *lvalue,
2b583ce6 1581 int ltype,
039655a4
LP
1582 const char *rvalue,
1583 void *data,
1584 void *userdata) {
1585
41584525 1586 ConditionType cond = ltype;
039655a4 1587 Unit *u = data;
267632f0 1588 bool trigger, negate;
039655a4
LP
1589 Condition *c;
1590
1591 assert(filename);
1592 assert(lvalue);
1593 assert(rvalue);
1594 assert(data);
1595
267632f0
LP
1596 if ((trigger = rvalue[0] == '|'))
1597 rvalue++;
1598
039655a4
LP
1599 if ((negate = rvalue[0] == '!'))
1600 rvalue++;
1601
41584525 1602 if (!(c = condition_new(cond, rvalue, trigger, negate)))
039655a4
LP
1603 return -ENOMEM;
1604
1605 LIST_PREPEND(Condition, conditions, u->meta.conditions, c);
1606 return 0;
1607}
1608
f975e971 1609int config_parse_unit_condition_null(
d257ddef
LP
1610 const char *filename,
1611 unsigned line,
1612 const char *section,
1613 const char *lvalue,
2b583ce6 1614 int ltype,
d257ddef
LP
1615 const char *rvalue,
1616 void *data,
1617 void *userdata) {
1618
1619 Unit *u = data;
1620 Condition *c;
267632f0 1621 bool trigger, negate;
d257ddef
LP
1622 int b;
1623
1624 assert(filename);
1625 assert(lvalue);
1626 assert(rvalue);
1627 assert(data);
1628
267632f0
LP
1629 if ((trigger = rvalue[0] == '|'))
1630 rvalue++;
1631
d257ddef
LP
1632 if ((negate = rvalue[0] == '!'))
1633 rvalue++;
1634
1635 if ((b = parse_boolean(rvalue)) < 0) {
1636 log_error("[%s:%u] Failed to parse boolean value in condition, ignoring: %s", filename, line, rvalue);
1637 return 0;
1638 }
1639
1640 if (!b)
1641 negate = !negate;
1642
267632f0 1643 if (!(c = condition_new(CONDITION_NULL, NULL, trigger, negate)))
d257ddef
LP
1644 return -ENOMEM;
1645
1646 LIST_PREPEND(Condition, conditions, u->meta.conditions, c);
1647 return 0;
1648}
1649
f975e971 1650DEFINE_CONFIG_PARSE_ENUM(config_parse_notify_access, notify_access, NotifyAccess, "Failed to parse notify access specifier");
c952c6ec 1651
ab1f0633
LP
1652int config_parse_unit_cgroup_attr(
1653 const char *filename,
1654 unsigned line,
1655 const char *section,
1656 const char *lvalue,
1657 int ltype,
1658 const char *rvalue,
1659 void *data,
1660 void *userdata) {
1661
1662 Unit *u = data;
1663 char **l;
1664 int r;
1665
1666 assert(filename);
1667 assert(lvalue);
1668 assert(rvalue);
1669 assert(data);
1670
1671 l = strv_split_quoted(rvalue);
1672 if (!l)
1673 return -ENOMEM;
1674
1675 if (strv_length(l) != 2) {
1676 log_error("[%s:%u] Failed to parse cgroup attribute value, ignoring: %s", filename, line, rvalue);
1677 strv_free(l);
1678 return 0;
1679 }
1680
1681 r = unit_add_cgroup_attribute(u, NULL, l[0], l[1], NULL);
1682 strv_free(l);
1683
1684 if (r < 0) {
1685 log_error("[%s:%u] Failed to add cgroup attribute value, ignoring: %s", filename, line, rvalue);
1686 return 0;
1687 }
1688
1689 return 0;
1690}
1691
1692int config_parse_unit_cpu_shares(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata) {
1693 Unit *u = data;
1694 int r;
1695 unsigned long ul;
1696 char *t;
1697
1698 assert(filename);
1699 assert(lvalue);
1700 assert(rvalue);
1701 assert(data);
1702
1703 if (safe_atolu(rvalue, &ul) < 0 || ul < 1) {
1704 log_error("[%s:%u] Failed to parse CPU shares value, ignoring: %s", filename, line, rvalue);
1705 return 0;
1706 }
1707
1708 if (asprintf(&t, "%lu", ul) < 0)
1709 return -ENOMEM;
1710
1711 r = unit_add_cgroup_attribute(u, "cpu", "cpu.shares", t, NULL);
1712 free(t);
1713
1714 if (r < 0) {
1715 log_error("[%s:%u] Failed to add cgroup attribute value, ignoring: %s", filename, line, rvalue);
1716 return 0;
1717 }
1718
1719 return 0;
1720}
1721
1722int config_parse_unit_memory_limit(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata) {
1723 Unit *u = data;
1724 int r;
1725 off_t sz;
1726 char *t;
1727
1728 assert(filename);
1729 assert(lvalue);
1730 assert(rvalue);
1731 assert(data);
1732
1733 if (parse_bytes(rvalue, &sz) < 0 || sz <= 0) {
1734 log_error("[%s:%u] Failed to parse memory limit value, ignoring: %s", filename, line, rvalue);
1735 return 0;
1736 }
1737
1738 if (asprintf(&t, "%llu", (unsigned long long) sz) < 0)
1739 return -ENOMEM;
1740
1741 r = unit_add_cgroup_attribute(u,
1742 "memory",
1743 streq(lvalue, "MemorySoftLimit") ? "memory.soft_limit_in_bytes" : "memory.limit_in_bytes",
1744 t, NULL);
1745 free(t);
1746
1747 if (r < 0) {
1748 log_error("[%s:%u] Failed to add cgroup attribute value, ignoring: %s", filename, line, rvalue);
1749 return 0;
1750 }
1751
1752 return 0;
1753}
1754
1755static int device_map(const char *controller, const char *name, const char *value, char **ret) {
ab1f0633
LP
1756 char **l;
1757
9e372868
LP
1758 assert(controller);
1759 assert(name);
1760 assert(value);
1761 assert(ret);
1762
ab1f0633
LP
1763 l = strv_split_quoted(value);
1764 if (!l)
1765 return -ENOMEM;
1766
1767 assert(strv_length(l) >= 1);
1768
1769 if (streq(l[0], "*")) {
1770
1771 if (asprintf(ret, "a *:*%s%s",
1772 isempty(l[1]) ? "" : " ", strempty(l[1])) < 0) {
1773 strv_free(l);
1774 return -ENOMEM;
1775 }
1776
1777 } else {
9e372868
LP
1778 struct stat st;
1779
1780 if (stat(l[0], &st) < 0) {
ab1f0633
LP
1781 log_warning("Couldn't stat device %s", l[0]);
1782 strv_free(l);
1783 return -errno;
1784 }
1785
1786 if (!S_ISCHR(st.st_mode) && !S_ISBLK(st.st_mode)) {
1787 log_warning("%s is not a device.", l[0]);
1788 strv_free(l);
1789 return -ENODEV;
1790 }
1791
1792 if (asprintf(ret, "%c %u:%u%s%s",
1793 S_ISCHR(st.st_mode) ? 'c' : 'b',
1794 major(st.st_rdev), minor(st.st_rdev),
1795 isempty(l[1]) ? "" : " ", strempty(l[1])) < 0) {
1796
1797 strv_free(l);
1798 return -ENOMEM;
1799 }
1800 }
1801
1802 strv_free(l);
1803 return 0;
1804}
1805
1806int config_parse_unit_device_allow(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata) {
1807 Unit *u = data;
1808 char **l;
1809 int r;
1810 unsigned k;
1811
1812 assert(filename);
1813 assert(lvalue);
1814 assert(rvalue);
1815 assert(data);
1816
1817 l = strv_split_quoted(rvalue);
1818 if (!l)
1819 return -ENOMEM;
1820
1821 k = strv_length(l);
1822 if (k < 1 || k > 2) {
1823 log_error("[%s:%u] Failed to parse device value, ignoring: %s", filename, line, rvalue);
1824 strv_free(l);
1825 return 0;
1826 }
1827
1828 if (!streq(l[0], "*") && !path_startswith(l[0], "/dev")) {
1829 log_error("[%s:%u] Device node path not absolute, ignoring: %s", filename, line, rvalue);
1830 strv_free(l);
1831 return 0;
1832 }
1833
1834 if (!isempty(l[1]) && !in_charset(l[1], "rwm")) {
1835 log_error("[%s:%u] Device access string invalid, ignoring: %s", filename, line, rvalue);
1836 strv_free(l);
1837 return 0;
1838 }
1839 strv_free(l);
1840
1841 r = unit_add_cgroup_attribute(u, "devices",
1842 streq(lvalue, "DeviceAllow") ? "devices.allow" : "devices.deny",
1843 rvalue, device_map);
1844
1845 if (r < 0) {
1846 log_error("[%s:%u] Failed to add cgroup attribute value, ignoring: %s", filename, line, rvalue);
1847 return 0;
1848 }
1849
1850 return 0;
1851}
1852
9e372868
LP
1853static int blkio_map(const char *controller, const char *name, const char *value, char **ret) {
1854 struct stat st;
1855 char **l;
94959f0f 1856 dev_t d;
9e372868
LP
1857
1858 assert(controller);
1859 assert(name);
1860 assert(value);
1861 assert(ret);
1862
1863 l = strv_split_quoted(value);
1864 if (!l)
1865 return -ENOMEM;
1866
1867 assert(strv_length(l) == 2);
1868
1869 if (stat(l[0], &st) < 0) {
1870 log_warning("Couldn't stat device %s", l[0]);
1871 strv_free(l);
1872 return -errno;
1873 }
1874
94959f0f
LP
1875 if (S_ISBLK(st.st_mode))
1876 d = st.st_rdev;
1877 else if (major(st.st_dev) != 0) {
1878 /* If this is not a device node then find the block
1879 * device this file is stored on */
1880 d = st.st_dev;
1881
1882 /* If this is a partition, try to get the originating
1883 * block device */
1884 block_get_whole_disk(d, &d);
1885 } else {
1886 log_warning("%s is not a block device and file system block device cannot be determined or is not local.", l[0]);
9e372868
LP
1887 strv_free(l);
1888 return -ENODEV;
1889 }
1890
94959f0f 1891 if (asprintf(ret, "%u:%u %s", major(d), minor(d), l[1]) < 0) {
9e372868
LP
1892 strv_free(l);
1893 return -ENOMEM;
1894 }
1895
1896 strv_free(l);
1897 return 0;
1898}
1899
1900int config_parse_unit_blkio_weight(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata) {
1901 Unit *u = data;
1902 int r;
1903 unsigned long ul;
1904 const char *device = NULL, *weight;
1905 unsigned k;
1906 char *t, **l;
1907
1908 assert(filename);
1909 assert(lvalue);
1910 assert(rvalue);
1911 assert(data);
1912
1913 l = strv_split_quoted(rvalue);
1914 if (!l)
1915 return -ENOMEM;
1916
1917 k = strv_length(l);
1918 if (k < 1 || k > 2) {
1919 log_error("[%s:%u] Failed to parse weight value, ignoring: %s", filename, line, rvalue);
1920 strv_free(l);
1921 return 0;
1922 }
1923
1924 if (k == 1)
1925 weight = l[0];
1926 else {
1927 device = l[0];
1928 weight = l[1];
1929 }
1930
94959f0f 1931 if (device && !path_is_absolute(device)) {
9e372868
LP
1932 log_error("[%s:%u] Failed to parse block device node value, ignoring: %s", filename, line, rvalue);
1933 strv_free(l);
1934 return 0;
1935 }
1936
1937 if (safe_atolu(weight, &ul) < 0 || ul < 10 || ul > 1000) {
1938 log_error("[%s:%u] Failed to parse block IO weight value, ignoring: %s", filename, line, rvalue);
1939 strv_free(l);
1940 return 0;
1941 }
1942
1943 if (device)
1944 r = asprintf(&t, "%s %lu", device, ul);
1945 else
1946 r = asprintf(&t, "%lu", ul);
1947 strv_free(l);
1948
1949 if (r < 0)
1950 return -ENOMEM;
1951
1952 if (device)
1953 r = unit_add_cgroup_attribute(u, "blkio", "blkio.weight_device", t, blkio_map);
1954 else
1955 r = unit_add_cgroup_attribute(u, "blkio", "blkio.weight", t, NULL);
1956 free(t);
1957
1958 if (r < 0) {
1959 log_error("[%s:%u] Failed to add cgroup attribute value, ignoring: %s", filename, line, rvalue);
1960 return 0;
1961 }
1962
1963 return 0;
1964}
1965
1966int config_parse_unit_blkio_bandwidth(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata) {
1967 Unit *u = data;
1968 int r;
1969 off_t bytes;
1970 unsigned k;
1971 char *t, **l;
1972
1973 assert(filename);
1974 assert(lvalue);
1975 assert(rvalue);
1976 assert(data);
1977
1978 l = strv_split_quoted(rvalue);
1979 if (!l)
1980 return -ENOMEM;
1981
1982 k = strv_length(l);
1983 if (k != 2) {
1984 log_error("[%s:%u] Failed to parse bandwidth value, ignoring: %s", filename, line, rvalue);
1985 strv_free(l);
1986 return 0;
1987 }
1988
94959f0f 1989 if (!path_is_absolute(l[0])) {
9e372868
LP
1990 log_error("[%s:%u] Failed to parse block device node value, ignoring: %s", filename, line, rvalue);
1991 strv_free(l);
1992 return 0;
1993 }
1994
1995 if (parse_bytes(l[1], &bytes) < 0 || bytes <= 0) {
1996 log_error("[%s:%u] Failed to parse block IO bandwith value, ignoring: %s", filename, line, rvalue);
1997 strv_free(l);
1998 return 0;
1999 }
2000
2001 r = asprintf(&t, "%s %llu", l[0], (unsigned long long) bytes);
2002 strv_free(l);
2003
2004 if (r < 0)
2005 return -ENOMEM;
2006
2007 r = unit_add_cgroup_attribute(u, "blkio",
2008 streq(lvalue, "BlockIOReadBandwidth") ? "blkio.read_bps_device" : "blkio.write_bps_device",
2009 t, blkio_map);
2010 free(t);
2011
2012 if (r < 0) {
2013 log_error("[%s:%u] Failed to add cgroup attribute value, ignoring: %s", filename, line, rvalue);
2014 return 0;
2015 }
2016
2017 return 0;
2018}
2019
2020
071830ff 2021#define FOLLOW_MAX 8
87f0e418 2022
9e2f7c11 2023static int open_follow(char **filename, FILE **_f, Set *names, char **_final) {
0301abf4 2024 unsigned c = 0;
87f0e418
LP
2025 int fd, r;
2026 FILE *f;
0301abf4 2027 char *id = NULL;
87f0e418
LP
2028
2029 assert(filename);
2030 assert(*filename);
2031 assert(_f);
2032 assert(names);
2033
0301abf4
LP
2034 /* This will update the filename pointer if the loaded file is
2035 * reached by a symlink. The old string will be freed. */
87f0e418 2036
0301abf4 2037 for (;;) {
2c7108c4 2038 char *target, *name;
87f0e418 2039
0301abf4
LP
2040 if (c++ >= FOLLOW_MAX)
2041 return -ELOOP;
2042
b08d03ff
LP
2043 path_kill_slashes(*filename);
2044
87f0e418 2045 /* Add the file name we are currently looking at to
8f05424d
LP
2046 * the names of this unit, but only if it is a valid
2047 * unit name. */
0301abf4 2048 name = file_name_from_path(*filename);
87f0e418 2049
15e11d81 2050 if (unit_name_is_valid(name, true)) {
8f05424d 2051
15e11d81
LP
2052 id = set_get(names, name);
2053 if (!id) {
2054 id = strdup(name);
2055 if (!id)
8f05424d 2056 return -ENOMEM;
87f0e418 2057
15e11d81
LP
2058 r = set_put(names, id);
2059 if (r < 0) {
8f05424d
LP
2060 free(id);
2061 return r;
2062 }
87f0e418 2063 }
87f0e418
LP
2064 }
2065
0301abf4
LP
2066 /* Try to open the file name, but don't if its a symlink */
2067 if ((fd = open(*filename, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW)) >= 0)
87f0e418
LP
2068 break;
2069
0301abf4
LP
2070 if (errno != ELOOP)
2071 return -errno;
2072
87f0e418 2073 /* Hmm, so this is a symlink. Let's read the name, and follow it manually */
2c7108c4 2074 if ((r = readlink_and_make_absolute(*filename, &target)) < 0)
0301abf4 2075 return r;
87f0e418 2076
0301abf4 2077 free(*filename);
2c7108c4 2078 *filename = target;
87f0e418
LP
2079 }
2080
8f05424d 2081 if (!(f = fdopen(fd, "re"))) {
87f0e418 2082 r = -errno;
9e2f7c11 2083 close_nointr_nofail(fd);
0301abf4 2084 return r;
87f0e418
LP
2085 }
2086
2087 *_f = f;
9e2f7c11 2088 *_final = id;
0301abf4 2089 return 0;
87f0e418
LP
2090}
2091
23a177ef
LP
2092static int merge_by_names(Unit **u, Set *names, const char *id) {
2093 char *k;
2094 int r;
2095
2096 assert(u);
2097 assert(*u);
2098 assert(names);
2099
2100 /* Let's try to add in all symlink names we found */
2101 while ((k = set_steal_first(names))) {
2102
2103 /* First try to merge in the other name into our
2104 * unit */
2105 if ((r = unit_merge_by_name(*u, k)) < 0) {
2106 Unit *other;
2107
2108 /* Hmm, we couldn't merge the other unit into
2109 * ours? Then let's try it the other way
2110 * round */
2111
2112 other = manager_get_unit((*u)->meta.manager, k);
2113 free(k);
2114
2115 if (other)
2116 if ((r = unit_merge(other, *u)) >= 0) {
2117 *u = other;
2118 return merge_by_names(u, names, NULL);
2119 }
2120
2121 return r;
2122 }
2123
2124 if (id == k)
2125 unit_choose_id(*u, id);
2126
2127 free(k);
2128 }
2129
2130 return 0;
2131}
2132
e537352b 2133static int load_from_path(Unit *u, const char *path) {
0301abf4 2134 int r;
87f0e418 2135 Set *symlink_names;
23a177ef
LP
2136 FILE *f = NULL;
2137 char *filename = NULL, *id = NULL;
2138 Unit *merged;
45fb0699 2139 struct stat st;
23a177ef
LP
2140
2141 assert(u);
e537352b 2142 assert(path);
3efd4195 2143
f975e971
LP
2144 symlink_names = set_new(string_hash_func, string_compare_func);
2145 if (!symlink_names)
87f0e418 2146 return -ENOMEM;
3efd4195 2147
036643a2
LP
2148 if (path_is_absolute(path)) {
2149
2150 if (!(filename = strdup(path))) {
2151 r = -ENOMEM;
2152 goto finish;
2153 }
2154
2155 if ((r = open_follow(&filename, &f, symlink_names, &id)) < 0) {
2156 free(filename);
2157 filename = NULL;
2158
2159 if (r != -ENOENT)
2160 goto finish;
2161 }
2162
2163 } else {
2164 char **p;
2165
84e3543e 2166 STRV_FOREACH(p, u->meta.manager->lookup_paths.unit_path) {
036643a2
LP
2167
2168 /* Instead of opening the path right away, we manually
2169 * follow all symlinks and add their name to our unit
2170 * name set while doing so */
2171 if (!(filename = path_make_absolute(path, *p))) {
2172 r = -ENOMEM;
2173 goto finish;
2174 }
2175
fe51822e
LP
2176 if (u->meta.manager->unit_path_cache &&
2177 !set_get(u->meta.manager->unit_path_cache, filename))
2178 r = -ENOENT;
2179 else
2180 r = open_follow(&filename, &f, symlink_names, &id);
2181
2182 if (r < 0) {
036643a2
LP
2183 char *sn;
2184
2185 free(filename);
2186 filename = NULL;
2187
2188 if (r != -ENOENT)
2189 goto finish;
2190
2191 /* Empty the symlink names for the next run */
2192 while ((sn = set_steal_first(symlink_names)))
2193 free(sn);
3efd4195 2194
036643a2
LP
2195 continue;
2196 }
2197
2198 break;
2199 }
2200 }
034c6ed7 2201
036643a2 2202 if (!filename) {
8f05424d 2203 /* Hmm, no suitable file found? */
23a177ef 2204 r = 0;
0301abf4
LP
2205 goto finish;
2206 }
87f0e418 2207
23a177ef
LP
2208 merged = u;
2209 if ((r = merge_by_names(&merged, symlink_names, id)) < 0)
0301abf4 2210 goto finish;
87f0e418 2211
23a177ef 2212 if (merged != u) {
e537352b 2213 u->meta.load_state = UNIT_MERGED;
23a177ef
LP
2214 r = 0;
2215 goto finish;
034c6ed7
LP
2216 }
2217
45fb0699
LP
2218 zero(st);
2219 if (fstat(fileno(f), &st) < 0) {
2220 r = -errno;
2221 goto finish;
2222 }
2223
00dc5d76 2224 if (null_or_empty(&st))
6daf4f90 2225 u->meta.load_state = UNIT_MASKED;
00dc5d76
LP
2226 else {
2227 /* Now, parse the file contents */
f975e971
LP
2228 r = config_parse(filename, f, UNIT_VTABLE(u)->sections, config_item_perf_lookup, (void*) load_fragment_gperf_lookup, false, u);
2229 if (r < 0)
00dc5d76
LP
2230 goto finish;
2231
2232 u->meta.load_state = UNIT_LOADED;
2233 }
b08d03ff 2234
6be1e7d5
LP
2235 free(u->meta.fragment_path);
2236 u->meta.fragment_path = filename;
0301abf4 2237 filename = NULL;
87f0e418 2238
45fb0699
LP
2239 u->meta.fragment_mtime = timespec_load(&st.st_mtim);
2240
23a177ef 2241 r = 0;
87f0e418
LP
2242
2243finish:
53ec43c6 2244 set_free_free(symlink_names);
0301abf4
LP
2245 free(filename);
2246
23a177ef
LP
2247 if (f)
2248 fclose(f);
2249
0301abf4
LP
2250 return r;
2251}
2252
e537352b 2253int unit_load_fragment(Unit *u) {
23a177ef 2254 int r;
294d81f1
LP
2255 Iterator i;
2256 const char *t;
0301abf4
LP
2257
2258 assert(u);
294d81f1
LP
2259 assert(u->meta.load_state == UNIT_STUB);
2260 assert(u->meta.id);
23a177ef 2261
294d81f1
LP
2262 /* First, try to find the unit under its id. We always look
2263 * for unit files in the default directories, to make it easy
2264 * to override things by placing things in /etc/systemd/system */
2265 if ((r = load_from_path(u, u->meta.id)) < 0)
2266 return r;
2267
2268 /* Try to find an alias we can load this with */
2269 if (u->meta.load_state == UNIT_STUB)
2270 SET_FOREACH(t, u->meta.names, i) {
2271
2272 if (t == u->meta.id)
2273 continue;
2274
2275 if ((r = load_from_path(u, t)) < 0)
2276 return r;
2277
2278 if (u->meta.load_state != UNIT_STUB)
2279 break;
2280 }
23a177ef 2281
294d81f1 2282 /* And now, try looking for it under the suggested (originally linked) path */
6ccb1b44
LP
2283 if (u->meta.load_state == UNIT_STUB && u->meta.fragment_path) {
2284
e537352b 2285 if ((r = load_from_path(u, u->meta.fragment_path)) < 0)
23a177ef 2286 return r;
0301abf4 2287
6ccb1b44
LP
2288 if (u->meta.load_state == UNIT_STUB) {
2289 /* Hmm, this didn't work? Then let's get rid
2290 * of the fragment path stored for us, so that
2291 * we don't point to an invalid location. */
2292 free(u->meta.fragment_path);
2293 u->meta.fragment_path = NULL;
2294 }
2295 }
2296
294d81f1
LP
2297 /* Look for a template */
2298 if (u->meta.load_state == UNIT_STUB && u->meta.instance) {
2299 char *k;
2300
2301 if (!(k = unit_name_template(u->meta.id)))
2302 return -ENOMEM;
2303
2304 r = load_from_path(u, k);
2305 free(k);
0301abf4 2306
294d81f1 2307 if (r < 0)
9e2f7c11 2308 return r;
890f434c 2309
e537352b 2310 if (u->meta.load_state == UNIT_STUB)
23a177ef 2311 SET_FOREACH(t, u->meta.names, i) {
87f0e418 2312
9e2f7c11 2313 if (t == u->meta.id)
23a177ef 2314 continue;
071830ff 2315
294d81f1
LP
2316 if (!(k = unit_name_template(t)))
2317 return -ENOMEM;
2318
2319 r = load_from_path(u, k);
2320 free(k);
2321
2322 if (r < 0)
23a177ef 2323 return r;
890f434c 2324
e537352b 2325 if (u->meta.load_state != UNIT_STUB)
23a177ef
LP
2326 break;
2327 }
071830ff
LP
2328 }
2329
23a177ef 2330 return 0;
3efd4195 2331}
e537352b
LP
2332
2333void unit_dump_config_items(FILE *f) {
f975e971
LP
2334 static const struct {
2335 const ConfigParserCallback callback;
2336 const char *rvalue;
2337 } table[] = {
2338 { config_parse_int, "INTEGER" },
2339 { config_parse_unsigned, "UNSIGNED" },
2340 { config_parse_size, "SIZE" },
2341 { config_parse_bool, "BOOLEAN" },
2342 { config_parse_string, "STRING" },
2343 { config_parse_path, "PATH" },
2344 { config_parse_unit_path_printf, "PATH" },
2345 { config_parse_strv, "STRING [...]" },
2346 { config_parse_exec_nice, "NICE" },
2347 { config_parse_exec_oom_score_adjust, "OOMSCOREADJUST" },
2348 { config_parse_exec_io_class, "IOCLASS" },
2349 { config_parse_exec_io_priority, "IOPRIORITY" },
2350 { config_parse_exec_cpu_sched_policy, "CPUSCHEDPOLICY" },
2351 { config_parse_exec_cpu_sched_prio, "CPUSCHEDPRIO" },
2352 { config_parse_exec_cpu_affinity, "CPUAFFINITY" },
2353 { config_parse_mode, "MODE" },
2354 { config_parse_unit_env_file, "FILE" },
2355 { config_parse_output, "OUTPUT" },
2356 { config_parse_input, "INPUT" },
2357 { config_parse_facility, "FACILITY" },
2358 { config_parse_level, "LEVEL" },
2359 { config_parse_exec_capabilities, "CAPABILITIES" },
2360 { config_parse_exec_secure_bits, "SECUREBITS" },
2361 { config_parse_exec_bounding_set, "BOUNDINGSET" },
2362 { config_parse_exec_timer_slack_nsec, "TIMERSLACK" },
2363 { config_parse_limit, "LIMIT" },
2364 { config_parse_unit_cgroup, "CGROUP [...]" },
2365 { config_parse_unit_deps, "UNIT [...]" },
2366 { config_parse_unit_names, "UNIT [...]" },
2367 { config_parse_exec, "PATH [ARGUMENT [...]]" },
2368 { config_parse_service_type, "SERVICETYPE" },
2369 { config_parse_service_restart, "SERVICERESTART" },
2370#ifdef HAVE_SYSV_COMPAT
2371 { config_parse_sysv_priority, "SYSVPRIORITY" },
2372#else
2373 { config_parse_warn_compat, "NOTSUPPORTED" },
2374#endif
2375 { config_parse_kill_mode, "KILLMODE" },
2376 { config_parse_kill_signal, "SIGNAL" },
2377 { config_parse_socket_listen, "SOCKET [...]" },
2378 { config_parse_socket_bind, "SOCKETBIND" },
2379 { config_parse_socket_bindtodevice, "NETWORKINTERFACE" },
2380 { config_parse_usec, "SECONDS" },
2381 { config_parse_path_strv, "PATH [...]" },
2382 { config_parse_exec_mount_flags, "MOUNTFLAG [...]" },
2383 { config_parse_unit_string_printf, "STRING" },
2384 { config_parse_timer, "TIMER" },
2385 { config_parse_timer_unit, "NAME" },
2386 { config_parse_path_spec, "PATH" },
2387 { config_parse_path_unit, "UNIT" },
2388 { config_parse_notify_access, "ACCESS" },
2389 { config_parse_ip_tos, "TOS" },
2390 { config_parse_unit_condition_path, "CONDITION" },
2391 { config_parse_unit_condition_string, "CONDITION" },
2392 { config_parse_unit_condition_null, "CONDITION" },
2393 };
2394
2395 const char *prev = NULL;
2396 const char *i;
2397
2398 assert(f);
e537352b 2399
f975e971
LP
2400 NULSTR_FOREACH(i, load_fragment_gperf_nulstr) {
2401 const char *rvalue = "OTHER", *lvalue;
2402 unsigned j;
2403 size_t prefix_len;
2404 const char *dot;
2405 const ConfigPerfItem *p;
2406
2407 assert_se(p = load_fragment_gperf_lookup(i, strlen(i)));
2408
2409 dot = strchr(i, '.');
2410 lvalue = dot ? dot + 1 : i;
2411 prefix_len = dot-i;
2412
2413 if (dot)
2414 if (!prev || strncmp(prev, i, prefix_len+1) != 0) {
2415 if (prev)
2416 fputc('\n', f);
2417
2418 fprintf(f, "[%.*s]\n", (int) prefix_len, i);
2419 }
2420
2421 for (j = 0; j < ELEMENTSOF(table); j++)
2422 if (p->parse == table[j].callback) {
2423 rvalue = table[j].rvalue;
2424 break;
2425 }
2426
2427 fprintf(f, "%s=%s\n", lvalue, rvalue);
2428 prev = i;
2429 }
e537352b 2430}