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