]>
Commit | Line | Data |
---|---|---|
3efd4195 LP |
1 | /*-*- Mode: C; c-basic-offset: 8 -*-*/ |
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> |
3efd4195 | 32 | |
87f0e418 | 33 | #include "unit.h" |
3efd4195 LP |
34 | #include "strv.h" |
35 | #include "conf-parser.h" | |
36 | #include "load-fragment.h" | |
16354eff | 37 | #include "log.h" |
9eba9da4 | 38 | #include "ioprio.h" |
94f04347 LP |
39 | #include "securebits.h" |
40 | #include "missing.h" | |
9e2f7c11 | 41 | #include "unit-name.h" |
3efd4195 | 42 | |
0d87eb42 LP |
43 | #define DEFINE_CONFIG_PARSE_ENUM(function,name,type,msg) \ |
44 | static int function( \ | |
45 | const char *filename, \ | |
46 | unsigned line, \ | |
47 | const char *section, \ | |
48 | const char *lvalue, \ | |
49 | const char *rvalue, \ | |
50 | void *data, \ | |
51 | void *userdata) { \ | |
52 | \ | |
53 | type *i = data, x; \ | |
54 | \ | |
55 | assert(filename); \ | |
56 | assert(lvalue); \ | |
57 | assert(rvalue); \ | |
58 | assert(data); \ | |
59 | \ | |
60 | if ((x = name##_from_string(rvalue)) < 0) { \ | |
61 | log_error("[%s:%u] " msg ": %s", filename, line, rvalue); \ | |
62 | return -EBADMSG; \ | |
63 | } \ | |
64 | \ | |
65 | *i = x; \ | |
66 | \ | |
67 | return 0; \ | |
68 | } | |
69 | ||
42f4e3c4 | 70 | static int config_parse_deps( |
3efd4195 LP |
71 | const char *filename, |
72 | unsigned line, | |
73 | const char *section, | |
74 | const char *lvalue, | |
75 | const char *rvalue, | |
76 | void *data, | |
77 | void *userdata) { | |
78 | ||
87f0e418 LP |
79 | UnitDependency d = PTR_TO_UINT(data); |
80 | Unit *u = userdata; | |
3efd4195 LP |
81 | char *w; |
82 | size_t l; | |
83 | char *state; | |
84 | ||
85 | assert(filename); | |
86 | assert(lvalue); | |
87 | assert(rvalue); | |
3efd4195 | 88 | |
f62c0e4f | 89 | FOREACH_WORD(w, l, rvalue, state) { |
9e2f7c11 | 90 | char *t, *k; |
3efd4195 | 91 | int r; |
3efd4195 LP |
92 | |
93 | if (!(t = strndup(w, l))) | |
94 | return -ENOMEM; | |
95 | ||
9e2f7c11 | 96 | k = unit_name_printf(u, t); |
3efd4195 LP |
97 | free(t); |
98 | ||
9e2f7c11 LP |
99 | if (!k) |
100 | return -ENOMEM; | |
101 | ||
701cc384 | 102 | r = unit_add_dependency_by_name(u, d, k, NULL, true); |
9e2f7c11 LP |
103 | free(k); |
104 | ||
3efd4195 LP |
105 | if (r < 0) |
106 | return r; | |
3efd4195 LP |
107 | } |
108 | ||
109 | return 0; | |
110 | } | |
111 | ||
42f4e3c4 | 112 | static int config_parse_names( |
87d1515d LP |
113 | const char *filename, |
114 | unsigned line, | |
115 | const char *section, | |
116 | const char *lvalue, | |
117 | const char *rvalue, | |
118 | void *data, | |
119 | void *userdata) { | |
120 | ||
87f0e418 | 121 | Unit *u = userdata; |
87d1515d LP |
122 | char *w; |
123 | size_t l; | |
124 | char *state; | |
125 | ||
126 | assert(filename); | |
127 | assert(lvalue); | |
128 | assert(rvalue); | |
129 | assert(data); | |
130 | ||
f62c0e4f | 131 | FOREACH_WORD(w, l, rvalue, state) { |
9e2f7c11 | 132 | char *t, *k; |
87d1515d | 133 | int r; |
87d1515d LP |
134 | |
135 | if (!(t = strndup(w, l))) | |
136 | return -ENOMEM; | |
137 | ||
9e2f7c11 | 138 | k = unit_name_printf(u, t); |
87d1515d | 139 | free(t); |
23a177ef | 140 | |
9e2f7c11 LP |
141 | if (!k) |
142 | return -ENOMEM; | |
143 | ||
144 | r = unit_merge_by_name(u, k); | |
145 | free(k); | |
146 | ||
23a177ef LP |
147 | if (r < 0) |
148 | return r; | |
87d1515d LP |
149 | } |
150 | ||
151 | return 0; | |
152 | } | |
153 | ||
932921b5 LP |
154 | static int config_parse_description( |
155 | const char *filename, | |
156 | unsigned line, | |
157 | const char *section, | |
158 | const char *lvalue, | |
159 | const char *rvalue, | |
160 | void *data, | |
161 | void *userdata) { | |
162 | ||
163 | Unit *u = userdata; | |
164 | char *k; | |
165 | ||
166 | assert(filename); | |
167 | assert(lvalue); | |
168 | assert(rvalue); | |
169 | assert(data); | |
170 | ||
171 | if (!(k = unit_full_printf(u, rvalue))) | |
172 | return -ENOMEM; | |
173 | ||
174 | free(u->meta.description); | |
175 | ||
176 | if (*k) | |
177 | u->meta.description = k; | |
178 | else { | |
179 | free(k); | |
180 | u->meta.description = NULL; | |
181 | } | |
182 | ||
183 | return 0; | |
184 | } | |
185 | ||
42f4e3c4 LP |
186 | static int config_parse_listen( |
187 | const char *filename, | |
188 | unsigned line, | |
189 | const char *section, | |
190 | const char *lvalue, | |
191 | const char *rvalue, | |
192 | void *data, | |
193 | void *userdata) { | |
194 | ||
16354eff | 195 | int r; |
542563ba LP |
196 | SocketPort *p; |
197 | Socket *s; | |
16354eff | 198 | |
42f4e3c4 LP |
199 | assert(filename); |
200 | assert(lvalue); | |
201 | assert(rvalue); | |
202 | assert(data); | |
203 | ||
542563ba LP |
204 | s = (Socket*) data; |
205 | ||
206 | if (!(p = new0(SocketPort, 1))) | |
207 | return -ENOMEM; | |
208 | ||
209 | if (streq(lvalue, "ListenFIFO")) { | |
210 | p->type = SOCKET_FIFO; | |
211 | ||
212 | if (!(p->path = strdup(rvalue))) { | |
213 | free(p); | |
214 | return -ENOMEM; | |
215 | } | |
216 | } else { | |
217 | p->type = SOCKET_SOCKET; | |
218 | ||
219 | if ((r = socket_address_parse(&p->address, rvalue)) < 0) { | |
220 | log_error("[%s:%u] Failed to parse address value: %s", filename, line, rvalue); | |
221 | free(p); | |
222 | return r; | |
223 | } | |
224 | ||
225 | if (streq(lvalue, "ListenStream")) | |
226 | p->address.type = SOCK_STREAM; | |
227 | else if (streq(lvalue, "ListenDatagram")) | |
228 | p->address.type = SOCK_DGRAM; | |
229 | else { | |
230 | assert(streq(lvalue, "ListenSequentialPacket")); | |
231 | p->address.type = SOCK_SEQPACKET; | |
232 | } | |
233 | ||
234 | if (socket_address_family(&p->address) != AF_LOCAL && p->address.type == SOCK_SEQPACKET) { | |
235 | free(p); | |
236 | return -EPROTONOSUPPORT; | |
237 | } | |
16354eff LP |
238 | } |
239 | ||
542563ba | 240 | p->fd = -1; |
034c6ed7 | 241 | LIST_PREPEND(SocketPort, port, s->ports, p); |
542563ba | 242 | |
16354eff | 243 | return 0; |
42f4e3c4 LP |
244 | } |
245 | ||
034c6ed7 | 246 | static int config_parse_socket_bind( |
42f4e3c4 LP |
247 | const char *filename, |
248 | unsigned line, | |
249 | const char *section, | |
250 | const char *lvalue, | |
251 | const char *rvalue, | |
252 | void *data, | |
253 | void *userdata) { | |
254 | ||
542563ba LP |
255 | int r; |
256 | Socket *s; | |
42f4e3c4 LP |
257 | |
258 | assert(filename); | |
259 | assert(lvalue); | |
260 | assert(rvalue); | |
261 | assert(data); | |
262 | ||
542563ba LP |
263 | s = (Socket*) data; |
264 | ||
265 | if ((r = parse_boolean(rvalue)) < 0) { | |
266 | log_error("[%s:%u] Failed to parse bind IPv6 only value: %s", filename, line, rvalue); | |
267 | return r; | |
16354eff | 268 | } |
42f4e3c4 | 269 | |
542563ba LP |
270 | s->bind_ipv6_only = r ? SOCKET_ADDRESS_IPV6_ONLY : SOCKET_ADDRESS_BOTH; |
271 | ||
42f4e3c4 LP |
272 | return 0; |
273 | } | |
274 | ||
034c6ed7 LP |
275 | static int config_parse_nice( |
276 | const char *filename, | |
277 | unsigned line, | |
278 | const char *section, | |
279 | const char *lvalue, | |
280 | const char *rvalue, | |
281 | void *data, | |
282 | void *userdata) { | |
283 | ||
fb33a393 LP |
284 | ExecContext *c = data; |
285 | int priority, r; | |
034c6ed7 LP |
286 | |
287 | assert(filename); | |
288 | assert(lvalue); | |
289 | assert(rvalue); | |
290 | assert(data); | |
291 | ||
292 | if ((r = safe_atoi(rvalue, &priority)) < 0) { | |
293 | log_error("[%s:%u] Failed to parse nice priority: %s", filename, line, rvalue); | |
294 | return r; | |
295 | } | |
296 | ||
297 | if (priority < PRIO_MIN || priority >= PRIO_MAX) { | |
298 | log_error("[%s:%u] Nice priority out of range: %s", filename, line, rvalue); | |
299 | return -ERANGE; | |
300 | } | |
301 | ||
fb33a393 LP |
302 | c->nice = priority; |
303 | c->nice_set = false; | |
304 | ||
034c6ed7 LP |
305 | return 0; |
306 | } | |
307 | ||
308 | static int config_parse_oom_adjust( | |
309 | const char *filename, | |
310 | unsigned line, | |
311 | const char *section, | |
312 | const char *lvalue, | |
313 | const char *rvalue, | |
314 | void *data, | |
315 | void *userdata) { | |
316 | ||
fb33a393 LP |
317 | ExecContext *c = data; |
318 | int oa, r; | |
034c6ed7 LP |
319 | |
320 | assert(filename); | |
321 | assert(lvalue); | |
322 | assert(rvalue); | |
323 | assert(data); | |
324 | ||
325 | if ((r = safe_atoi(rvalue, &oa)) < 0) { | |
326 | log_error("[%s:%u] Failed to parse OOM adjust value: %s", filename, line, rvalue); | |
327 | return r; | |
328 | } | |
329 | ||
330 | if (oa < OOM_DISABLE || oa > OOM_ADJUST_MAX) { | |
331 | log_error("[%s:%u] OOM adjust value out of range: %s", filename, line, rvalue); | |
332 | return -ERANGE; | |
333 | } | |
334 | ||
fb33a393 LP |
335 | c->oom_adjust = oa; |
336 | c->oom_adjust_set = true; | |
337 | ||
034c6ed7 LP |
338 | return 0; |
339 | } | |
340 | ||
b5a0699f | 341 | static int config_parse_mode( |
034c6ed7 LP |
342 | const char *filename, |
343 | unsigned line, | |
344 | const char *section, | |
345 | const char *lvalue, | |
346 | const char *rvalue, | |
347 | void *data, | |
348 | void *userdata) { | |
349 | ||
350 | mode_t *m = data; | |
351 | long l; | |
352 | char *x = NULL; | |
353 | ||
354 | assert(filename); | |
355 | assert(lvalue); | |
356 | assert(rvalue); | |
357 | assert(data); | |
358 | ||
359 | errno = 0; | |
360 | l = strtol(rvalue, &x, 8); | |
361 | if (!x || *x || errno) { | |
b5a0699f | 362 | log_error("[%s:%u] Failed to parse mode value: %s", filename, line, rvalue); |
034c6ed7 LP |
363 | return errno ? -errno : -EINVAL; |
364 | } | |
365 | ||
b5a0699f LP |
366 | if (l < 0000 || l > 07777) { |
367 | log_error("[%s:%u] mode value out of range: %s", filename, line, rvalue); | |
034c6ed7 LP |
368 | return -ERANGE; |
369 | } | |
370 | ||
371 | *m = (mode_t) l; | |
372 | return 0; | |
373 | } | |
374 | ||
375 | static int config_parse_exec( | |
376 | const char *filename, | |
377 | unsigned line, | |
378 | const char *section, | |
379 | const char *lvalue, | |
380 | const char *rvalue, | |
381 | void *data, | |
382 | void *userdata) { | |
383 | ||
a6a80b4f | 384 | ExecCommand **e = data, *nce = NULL; |
034c6ed7 LP |
385 | char **n; |
386 | char *w; | |
387 | unsigned k; | |
388 | size_t l; | |
389 | char *state; | |
390 | ||
391 | assert(filename); | |
392 | assert(lvalue); | |
393 | assert(rvalue); | |
394 | assert(data); | |
395 | ||
396 | k = 0; | |
397 | FOREACH_WORD_QUOTED(w, l, rvalue, state) | |
398 | k++; | |
399 | ||
400 | if (!(n = new(char*, k+1))) | |
401 | return -ENOMEM; | |
402 | ||
44d8db9e | 403 | k = 0; |
034c6ed7 LP |
404 | FOREACH_WORD_QUOTED(w, l, rvalue, state) |
405 | if (!(n[k++] = strndup(w, l))) | |
406 | goto fail; | |
407 | ||
408 | n[k] = NULL; | |
409 | ||
0301abf4 | 410 | if (!n[0] || !path_is_absolute(n[0])) { |
034c6ed7 LP |
411 | log_error("[%s:%u] Invalid executable path in command line: %s", filename, line, rvalue); |
412 | strv_free(n); | |
413 | return -EINVAL; | |
414 | } | |
415 | ||
416 | if (!(nce = new0(ExecCommand, 1))) | |
417 | goto fail; | |
418 | ||
419 | nce->argv = n; | |
420 | if (!(nce->path = strdup(n[0]))) | |
421 | goto fail; | |
422 | ||
a6a80b4f | 423 | exec_command_append_list(e, nce); |
034c6ed7 LP |
424 | |
425 | return 0; | |
426 | ||
427 | fail: | |
428 | for (; k > 0; k--) | |
429 | free(n[k-1]); | |
430 | free(n); | |
431 | ||
432 | free(nce); | |
433 | ||
434 | return -ENOMEM; | |
435 | } | |
436 | ||
437 | static int config_parse_usec( | |
438 | const char *filename, | |
439 | unsigned line, | |
440 | const char *section, | |
441 | const char *lvalue, | |
442 | const char *rvalue, | |
443 | void *data, | |
444 | void *userdata) { | |
445 | ||
446 | usec_t *usec = data; | |
034c6ed7 LP |
447 | int r; |
448 | ||
449 | assert(filename); | |
450 | assert(lvalue); | |
451 | assert(rvalue); | |
452 | assert(data); | |
453 | ||
24a6e4a4 | 454 | if ((r = parse_usec(rvalue, usec)) < 0) { |
034c6ed7 LP |
455 | log_error("[%s:%u] Failed to parse time value: %s", filename, line, rvalue); |
456 | return r; | |
457 | } | |
458 | ||
034c6ed7 LP |
459 | return 0; |
460 | } | |
461 | ||
0d87eb42 LP |
462 | DEFINE_CONFIG_PARSE_ENUM(config_parse_service_type, service_type, ServiceType, "Failed to parse service type"); |
463 | DEFINE_CONFIG_PARSE_ENUM(config_parse_service_restart, service_restart, ServiceRestart, "Failed to parse service restart specifier"); | |
034c6ed7 | 464 | |
47be870b | 465 | static int config_parse_bindtodevice( |
acbb0225 LP |
466 | const char *filename, |
467 | unsigned line, | |
468 | const char *section, | |
469 | const char *lvalue, | |
470 | const char *rvalue, | |
471 | void *data, | |
472 | void *userdata) { | |
473 | ||
474 | Socket *s = data; | |
475 | char *n; | |
476 | ||
477 | assert(filename); | |
478 | assert(lvalue); | |
479 | assert(rvalue); | |
480 | assert(data); | |
481 | ||
482 | if (rvalue[0] && !streq(rvalue, "*")) { | |
483 | if (!(n = strdup(rvalue))) | |
484 | return -ENOMEM; | |
485 | } else | |
486 | n = NULL; | |
487 | ||
488 | free(s->bind_to_device); | |
489 | s->bind_to_device = n; | |
490 | ||
491 | return 0; | |
492 | } | |
493 | ||
0d87eb42 LP |
494 | DEFINE_CONFIG_PARSE_ENUM(config_parse_output, exec_output, ExecOutput, "Failed to parse output specifier"); |
495 | DEFINE_CONFIG_PARSE_ENUM(config_parse_input, exec_input, ExecInput, "Failed to parse input specifier"); | |
87f0e418 | 496 | |
47be870b | 497 | static int config_parse_facility( |
071830ff LP |
498 | const char *filename, |
499 | unsigned line, | |
500 | const char *section, | |
501 | const char *lvalue, | |
502 | const char *rvalue, | |
503 | void *data, | |
504 | void *userdata) { | |
505 | ||
071830ff | 506 | |
94f04347 | 507 | int *o = data, x; |
071830ff LP |
508 | |
509 | assert(filename); | |
510 | assert(lvalue); | |
511 | assert(rvalue); | |
512 | assert(data); | |
513 | ||
0d87eb42 LP |
514 | if ((x = log_facility_from_string(rvalue)) < 0) { |
515 | log_error("[%s:%u] Failed to parse log facility: %s", filename, line, rvalue); | |
516 | return -EBADMSG; | |
517 | } | |
94f04347 LP |
518 | |
519 | *o = LOG_MAKEPRI(x, LOG_PRI(*o)); | |
071830ff LP |
520 | |
521 | return 0; | |
522 | } | |
523 | ||
47be870b | 524 | static int config_parse_level( |
071830ff LP |
525 | const char *filename, |
526 | unsigned line, | |
527 | const char *section, | |
528 | const char *lvalue, | |
529 | const char *rvalue, | |
530 | void *data, | |
531 | void *userdata) { | |
532 | ||
071830ff | 533 | |
94f04347 | 534 | int *o = data, x; |
071830ff LP |
535 | |
536 | assert(filename); | |
537 | assert(lvalue); | |
538 | assert(rvalue); | |
539 | assert(data); | |
540 | ||
0d87eb42 LP |
541 | if ((x = log_level_from_string(rvalue)) < 0) { |
542 | log_error("[%s:%u] Failed to parse log level: %s", filename, line, rvalue); | |
543 | return -EBADMSG; | |
544 | } | |
071830ff | 545 | |
94f04347 LP |
546 | *o = LOG_MAKEPRI(LOG_FAC(*o), x); |
547 | return 0; | |
548 | } | |
549 | ||
47be870b | 550 | static int config_parse_io_class( |
94f04347 LP |
551 | const char *filename, |
552 | unsigned line, | |
553 | const char *section, | |
554 | const char *lvalue, | |
555 | const char *rvalue, | |
556 | void *data, | |
557 | void *userdata) { | |
558 | ||
559 | ExecContext *c = data; | |
560 | int x; | |
561 | ||
562 | assert(filename); | |
563 | assert(lvalue); | |
564 | assert(rvalue); | |
565 | assert(data); | |
566 | ||
0d87eb42 LP |
567 | if ((x = ioprio_class_from_string(rvalue)) < 0) { |
568 | log_error("[%s:%u] Failed to parse IO scheduling class: %s", filename, line, rvalue); | |
569 | return -EBADMSG; | |
570 | } | |
94f04347 LP |
571 | |
572 | c->ioprio = IOPRIO_PRIO_VALUE(x, IOPRIO_PRIO_DATA(c->ioprio)); | |
573 | c->ioprio_set = true; | |
574 | ||
575 | return 0; | |
576 | } | |
577 | ||
47be870b | 578 | static int config_parse_io_priority( |
94f04347 LP |
579 | const char *filename, |
580 | unsigned line, | |
581 | const char *section, | |
582 | const char *lvalue, | |
583 | const char *rvalue, | |
584 | void *data, | |
585 | void *userdata) { | |
586 | ||
587 | ExecContext *c = data; | |
588 | int i; | |
589 | ||
590 | assert(filename); | |
591 | assert(lvalue); | |
592 | assert(rvalue); | |
593 | assert(data); | |
594 | ||
595 | if (safe_atoi(rvalue, &i) < 0 || i < 0 || i >= IOPRIO_BE_NR) { | |
596 | log_error("[%s:%u] Failed to parse io priority: %s", filename, line, rvalue); | |
597 | return -EBADMSG; | |
071830ff LP |
598 | } |
599 | ||
94f04347 LP |
600 | c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_PRIO_CLASS(c->ioprio), i); |
601 | c->ioprio_set = true; | |
602 | ||
071830ff LP |
603 | return 0; |
604 | } | |
605 | ||
47be870b | 606 | static int config_parse_cpu_sched_policy( |
9eba9da4 LP |
607 | const char *filename, |
608 | unsigned line, | |
609 | const char *section, | |
610 | const char *lvalue, | |
611 | const char *rvalue, | |
612 | void *data, | |
613 | void *userdata) { | |
614 | ||
94f04347 LP |
615 | |
616 | ExecContext *c = data; | |
617 | int x; | |
618 | ||
619 | assert(filename); | |
620 | assert(lvalue); | |
621 | assert(rvalue); | |
622 | assert(data); | |
623 | ||
0d87eb42 LP |
624 | if ((x = sched_policy_from_string(rvalue)) < 0) { |
625 | log_error("[%s:%u] Failed to parse CPU scheduling policy: %s", filename, line, rvalue); | |
626 | return -EBADMSG; | |
627 | } | |
94f04347 LP |
628 | |
629 | c->cpu_sched_policy = x; | |
630 | c->cpu_sched_set = true; | |
631 | ||
632 | return 0; | |
633 | } | |
634 | ||
47be870b | 635 | static int config_parse_cpu_sched_prio( |
94f04347 LP |
636 | const char *filename, |
637 | unsigned line, | |
638 | const char *section, | |
639 | const char *lvalue, | |
640 | const char *rvalue, | |
641 | void *data, | |
642 | void *userdata) { | |
9eba9da4 LP |
643 | |
644 | ExecContext *c = data; | |
645 | int i; | |
646 | ||
647 | assert(filename); | |
648 | assert(lvalue); | |
649 | assert(rvalue); | |
650 | assert(data); | |
651 | ||
94f04347 LP |
652 | /* On Linux RR/FIFO have the same range */ |
653 | if (safe_atoi(rvalue, &i) < 0 || i < sched_get_priority_min(SCHED_RR) || i > sched_get_priority_max(SCHED_RR)) { | |
654 | log_error("[%s:%u] Failed to parse CPU scheduling priority: %s", filename, line, rvalue); | |
655 | return -EBADMSG; | |
656 | } | |
9eba9da4 | 657 | |
94f04347 LP |
658 | c->cpu_sched_priority = i; |
659 | c->cpu_sched_set = true; | |
660 | ||
661 | return 0; | |
662 | } | |
663 | ||
47be870b | 664 | static int config_parse_cpu_affinity( |
94f04347 LP |
665 | const char *filename, |
666 | unsigned line, | |
667 | const char *section, | |
668 | const char *lvalue, | |
669 | const char *rvalue, | |
670 | void *data, | |
671 | void *userdata) { | |
672 | ||
673 | ExecContext *c = data; | |
674 | char *w; | |
675 | size_t l; | |
676 | char *state; | |
677 | ||
678 | assert(filename); | |
679 | assert(lvalue); | |
680 | assert(rvalue); | |
681 | assert(data); | |
682 | ||
f62c0e4f | 683 | FOREACH_WORD(w, l, rvalue, state) { |
94f04347 LP |
684 | char *t; |
685 | int r; | |
686 | unsigned cpu; | |
687 | ||
688 | if (!(t = strndup(w, l))) | |
689 | return -ENOMEM; | |
690 | ||
691 | r = safe_atou(t, &cpu); | |
692 | free(t); | |
693 | ||
694 | if (r < 0 || cpu >= CPU_SETSIZE) { | |
695 | log_error("[%s:%u] Failed to parse CPU affinity: %s", filename, line, rvalue); | |
696 | return -EBADMSG; | |
9eba9da4 | 697 | } |
94f04347 LP |
698 | |
699 | CPU_SET(cpu, &c->cpu_affinity); | |
9eba9da4 LP |
700 | } |
701 | ||
94f04347 | 702 | c->cpu_affinity_set = true; |
9eba9da4 | 703 | |
94f04347 LP |
704 | return 0; |
705 | } | |
706 | ||
47be870b | 707 | static int config_parse_capabilities( |
94f04347 LP |
708 | const char *filename, |
709 | unsigned line, | |
710 | const char *section, | |
711 | const char *lvalue, | |
712 | const char *rvalue, | |
713 | void *data, | |
714 | void *userdata) { | |
715 | ||
716 | ExecContext *c = data; | |
717 | cap_t cap; | |
718 | ||
719 | assert(filename); | |
720 | assert(lvalue); | |
721 | assert(rvalue); | |
722 | assert(data); | |
723 | ||
724 | if (!(cap = cap_from_text(rvalue))) { | |
725 | if (errno == ENOMEM) | |
726 | return -ENOMEM; | |
727 | ||
728 | log_error("[%s:%u] Failed to parse capabilities: %s", filename, line, rvalue); | |
729 | return -EBADMSG; | |
730 | } | |
731 | ||
732 | if (c->capabilities) | |
733 | cap_free(c->capabilities); | |
734 | c->capabilities = cap; | |
735 | ||
736 | return 0; | |
737 | } | |
738 | ||
47be870b | 739 | static int config_parse_secure_bits( |
94f04347 LP |
740 | const char *filename, |
741 | unsigned line, | |
742 | const char *section, | |
743 | const char *lvalue, | |
744 | const char *rvalue, | |
745 | void *data, | |
746 | void *userdata) { | |
747 | ||
748 | ExecContext *c = data; | |
749 | char *w; | |
750 | size_t l; | |
751 | char *state; | |
752 | ||
753 | assert(filename); | |
754 | assert(lvalue); | |
755 | assert(rvalue); | |
756 | assert(data); | |
757 | ||
f62c0e4f | 758 | FOREACH_WORD(w, l, rvalue, state) { |
94f04347 LP |
759 | if (first_word(w, "keep-caps")) |
760 | c->secure_bits |= SECURE_KEEP_CAPS; | |
761 | else if (first_word(w, "keep-caps-locked")) | |
762 | c->secure_bits |= SECURE_KEEP_CAPS_LOCKED; | |
763 | else if (first_word(w, "no-setuid-fixup")) | |
764 | c->secure_bits |= SECURE_NO_SETUID_FIXUP; | |
765 | else if (first_word(w, "no-setuid-fixup-locked")) | |
766 | c->secure_bits |= SECURE_NO_SETUID_FIXUP_LOCKED; | |
767 | else if (first_word(w, "noroot")) | |
768 | c->secure_bits |= SECURE_NOROOT; | |
769 | else if (first_word(w, "noroot-locked")) | |
770 | c->secure_bits |= SECURE_NOROOT_LOCKED; | |
9eba9da4 | 771 | else { |
94f04347 | 772 | log_error("[%s:%u] Failed to parse secure bits: %s", filename, line, rvalue); |
9eba9da4 LP |
773 | return -EBADMSG; |
774 | } | |
775 | } | |
776 | ||
94f04347 LP |
777 | return 0; |
778 | } | |
779 | ||
47be870b | 780 | static int config_parse_bounding_set( |
94f04347 LP |
781 | const char *filename, |
782 | unsigned line, | |
783 | const char *section, | |
784 | const char *lvalue, | |
785 | const char *rvalue, | |
786 | void *data, | |
787 | void *userdata) { | |
788 | ||
789 | ExecContext *c = data; | |
790 | char *w; | |
791 | size_t l; | |
792 | char *state; | |
793 | ||
794 | assert(filename); | |
795 | assert(lvalue); | |
796 | assert(rvalue); | |
797 | assert(data); | |
798 | ||
f62c0e4f | 799 | FOREACH_WORD(w, l, rvalue, state) { |
94f04347 LP |
800 | char *t; |
801 | int r; | |
802 | cap_value_t cap; | |
803 | ||
804 | if (!(t = strndup(w, l))) | |
805 | return -ENOMEM; | |
806 | ||
807 | r = cap_from_name(t, &cap); | |
808 | free(t); | |
809 | ||
810 | if (r < 0) { | |
811 | log_error("[%s:%u] Failed to parse capability bounding set: %s", filename, line, rvalue); | |
812 | return -EBADMSG; | |
813 | } | |
814 | ||
815 | c->capability_bounding_set_drop |= 1 << cap; | |
816 | } | |
9eba9da4 LP |
817 | |
818 | return 0; | |
819 | } | |
820 | ||
94f04347 | 821 | static int config_parse_timer_slack_ns( |
9eba9da4 LP |
822 | const char *filename, |
823 | unsigned line, | |
824 | const char *section, | |
825 | const char *lvalue, | |
826 | const char *rvalue, | |
827 | void *data, | |
828 | void *userdata) { | |
829 | ||
830 | ExecContext *c = data; | |
94f04347 LP |
831 | unsigned long u; |
832 | int r; | |
9eba9da4 LP |
833 | |
834 | assert(filename); | |
835 | assert(lvalue); | |
836 | assert(rvalue); | |
837 | assert(data); | |
838 | ||
94f04347 LP |
839 | if ((r = safe_atolu(rvalue, &u)) < 0) { |
840 | log_error("[%s:%u] Failed to parse time slack value: %s", filename, line, rvalue); | |
841 | return r; | |
9eba9da4 LP |
842 | } |
843 | ||
94f04347 LP |
844 | c->timer_slack_ns = u; |
845 | ||
846 | return 0; | |
847 | } | |
848 | ||
849 | static int config_parse_limit( | |
850 | const char *filename, | |
851 | unsigned line, | |
852 | const char *section, | |
853 | const char *lvalue, | |
854 | const char *rvalue, | |
855 | void *data, | |
856 | void *userdata) { | |
857 | ||
858 | struct rlimit **rl = data; | |
859 | unsigned long long u; | |
860 | int r; | |
861 | ||
862 | assert(filename); | |
863 | assert(lvalue); | |
864 | assert(rvalue); | |
865 | assert(data); | |
866 | ||
867 | if ((r = safe_atollu(rvalue, &u)) < 0) { | |
868 | log_error("[%s:%u] Failed to parse resource value: %s", filename, line, rvalue); | |
869 | return r; | |
870 | } | |
871 | ||
872 | if (!*rl) | |
873 | if (!(*rl = new(struct rlimit, 1))) | |
874 | return -ENOMEM; | |
9eba9da4 | 875 | |
94f04347 | 876 | (*rl)->rlim_cur = (*rl)->rlim_max = (rlim_t) u; |
9eba9da4 LP |
877 | return 0; |
878 | } | |
879 | ||
8e274523 LP |
880 | static int config_parse_cgroup( |
881 | const char *filename, | |
882 | unsigned line, | |
883 | const char *section, | |
884 | const char *lvalue, | |
885 | const char *rvalue, | |
886 | void *data, | |
887 | void *userdata) { | |
888 | ||
889 | Unit *u = userdata; | |
890 | char *w; | |
891 | size_t l; | |
892 | char *state; | |
893 | ||
894 | FOREACH_WORD(w, l, rvalue, state) { | |
895 | char *t; | |
896 | int r; | |
897 | ||
898 | if (!(t = strndup(w, l))) | |
899 | return -ENOMEM; | |
900 | ||
901 | r = unit_add_cgroup_from_text(u, t); | |
902 | free(t); | |
903 | ||
904 | if (r < 0) | |
905 | return r; | |
906 | } | |
907 | ||
908 | return 0; | |
909 | } | |
910 | ||
a9a1e00a LP |
911 | static int config_parse_sysv_priority( |
912 | const char *filename, | |
913 | unsigned line, | |
914 | const char *section, | |
915 | const char *lvalue, | |
916 | const char *rvalue, | |
917 | void *data, | |
918 | void *userdata) { | |
919 | ||
920 | int *priority = data; | |
921 | int r, i; | |
922 | ||
923 | assert(filename); | |
924 | assert(lvalue); | |
925 | assert(rvalue); | |
926 | assert(data); | |
927 | ||
928 | if ((r = safe_atoi(rvalue, &i)) < 0 || i < 0) { | |
929 | log_error("[%s:%u] Failed to parse SysV start priority: %s", filename, line, rvalue); | |
930 | return r; | |
931 | } | |
932 | ||
933 | *priority = (int) i; | |
934 | return 0; | |
935 | } | |
936 | ||
0d87eb42 | 937 | DEFINE_CONFIG_PARSE_ENUM(config_parse_kill_mode, kill_mode, KillMode, "Failed to parse kill mode"); |
50159e6a | 938 | |
15ae422b LP |
939 | static int config_parse_mount_flags( |
940 | const char *filename, | |
941 | unsigned line, | |
942 | const char *section, | |
943 | const char *lvalue, | |
944 | const char *rvalue, | |
945 | void *data, | |
946 | void *userdata) { | |
947 | ||
948 | ExecContext *c = data; | |
949 | char *w; | |
950 | size_t l; | |
951 | char *state; | |
952 | unsigned long flags = 0; | |
953 | ||
954 | assert(filename); | |
955 | assert(lvalue); | |
956 | assert(rvalue); | |
957 | assert(data); | |
958 | ||
959 | FOREACH_WORD(w, l, rvalue, state) { | |
960 | if (strncmp(w, "shared", l) == 0) | |
961 | flags |= MS_SHARED; | |
962 | else if (strncmp(w, "slave", l) == 0) | |
963 | flags |= MS_SLAVE; | |
964 | else if (strncmp(w, "private", l) == 0) | |
965 | flags |= MS_PRIVATE; | |
966 | else { | |
967 | log_error("[%s:%u] Failed to parse mount flags: %s", filename, line, rvalue); | |
968 | return -EINVAL; | |
969 | } | |
970 | } | |
971 | ||
972 | c->mount_flags = flags; | |
973 | return 0; | |
974 | } | |
975 | ||
071830ff | 976 | #define FOLLOW_MAX 8 |
87f0e418 | 977 | |
9e2f7c11 | 978 | static int open_follow(char **filename, FILE **_f, Set *names, char **_final) { |
0301abf4 | 979 | unsigned c = 0; |
87f0e418 LP |
980 | int fd, r; |
981 | FILE *f; | |
0301abf4 | 982 | char *id = NULL; |
87f0e418 LP |
983 | |
984 | assert(filename); | |
985 | assert(*filename); | |
986 | assert(_f); | |
987 | assert(names); | |
988 | ||
0301abf4 LP |
989 | /* This will update the filename pointer if the loaded file is |
990 | * reached by a symlink. The old string will be freed. */ | |
87f0e418 | 991 | |
0301abf4 | 992 | for (;;) { |
87f0e418 LP |
993 | char *target, *k, *name; |
994 | ||
0301abf4 LP |
995 | if (c++ >= FOLLOW_MAX) |
996 | return -ELOOP; | |
997 | ||
b08d03ff LP |
998 | path_kill_slashes(*filename); |
999 | ||
87f0e418 LP |
1000 | /* Add the file name we are currently looking at to |
1001 | * the names of this unit */ | |
0301abf4 LP |
1002 | name = file_name_from_path(*filename); |
1003 | if (!(id = set_get(names, name))) { | |
87f0e418 | 1004 | |
0301abf4 LP |
1005 | if (!(id = strdup(name))) |
1006 | return -ENOMEM; | |
87f0e418 | 1007 | |
0301abf4 LP |
1008 | if ((r = set_put(names, id)) < 0) { |
1009 | free(id); | |
1010 | return r; | |
87f0e418 | 1011 | } |
87f0e418 LP |
1012 | } |
1013 | ||
0301abf4 LP |
1014 | /* Try to open the file name, but don't if its a symlink */ |
1015 | if ((fd = open(*filename, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW)) >= 0) | |
87f0e418 LP |
1016 | break; |
1017 | ||
0301abf4 LP |
1018 | if (errno != ELOOP) |
1019 | return -errno; | |
1020 | ||
87f0e418 | 1021 | /* Hmm, so this is a symlink. Let's read the name, and follow it manually */ |
0301abf4 LP |
1022 | if ((r = readlink_malloc(*filename, &target)) < 0) |
1023 | return r; | |
87f0e418 | 1024 | |
c25fb0ed | 1025 | k = file_in_same_dir(*filename, target); |
87f0e418 LP |
1026 | free(target); |
1027 | ||
0301abf4 LP |
1028 | if (!k) |
1029 | return -ENOMEM; | |
87f0e418 | 1030 | |
0301abf4 LP |
1031 | free(*filename); |
1032 | *filename = k; | |
87f0e418 LP |
1033 | } |
1034 | ||
1035 | if (!(f = fdopen(fd, "r"))) { | |
1036 | r = -errno; | |
9e2f7c11 | 1037 | close_nointr_nofail(fd); |
0301abf4 | 1038 | return r; |
87f0e418 LP |
1039 | } |
1040 | ||
1041 | *_f = f; | |
9e2f7c11 | 1042 | *_final = id; |
0301abf4 | 1043 | return 0; |
87f0e418 LP |
1044 | } |
1045 | ||
23a177ef LP |
1046 | static int merge_by_names(Unit **u, Set *names, const char *id) { |
1047 | char *k; | |
1048 | int r; | |
1049 | ||
1050 | assert(u); | |
1051 | assert(*u); | |
1052 | assert(names); | |
1053 | ||
1054 | /* Let's try to add in all symlink names we found */ | |
1055 | while ((k = set_steal_first(names))) { | |
1056 | ||
1057 | /* First try to merge in the other name into our | |
1058 | * unit */ | |
1059 | if ((r = unit_merge_by_name(*u, k)) < 0) { | |
1060 | Unit *other; | |
1061 | ||
1062 | /* Hmm, we couldn't merge the other unit into | |
1063 | * ours? Then let's try it the other way | |
1064 | * round */ | |
1065 | ||
1066 | other = manager_get_unit((*u)->meta.manager, k); | |
1067 | free(k); | |
1068 | ||
1069 | if (other) | |
1070 | if ((r = unit_merge(other, *u)) >= 0) { | |
1071 | *u = other; | |
1072 | return merge_by_names(u, names, NULL); | |
1073 | } | |
1074 | ||
1075 | return r; | |
1076 | } | |
1077 | ||
1078 | if (id == k) | |
1079 | unit_choose_id(*u, id); | |
1080 | ||
1081 | free(k); | |
1082 | } | |
1083 | ||
1084 | return 0; | |
1085 | } | |
1086 | ||
e537352b LP |
1087 | static void dump_items(FILE *f, const ConfigItem *items) { |
1088 | const ConfigItem *i; | |
1089 | const char *prev_section = NULL; | |
1090 | bool not_first = false; | |
1091 | ||
1092 | struct { | |
1093 | ConfigParserCallback callback; | |
1094 | const char *rvalue; | |
1095 | } table[] = { | |
1096 | { config_parse_int, "INTEGER" }, | |
1097 | { config_parse_unsigned, "UNSIGNED" }, | |
1098 | { config_parse_size, "SIZE" }, | |
1099 | { config_parse_bool, "BOOLEAN" }, | |
1100 | { config_parse_string, "STRING" }, | |
1101 | { config_parse_path, "PATH" }, | |
1102 | { config_parse_strv, "STRING [...]" }, | |
1103 | { config_parse_nice, "NICE" }, | |
1104 | { config_parse_oom_adjust, "OOMADJUST" }, | |
1105 | { config_parse_io_class, "IOCLASS" }, | |
1106 | { config_parse_io_priority, "IOPRIORITY" }, | |
1107 | { config_parse_cpu_sched_policy, "CPUSCHEDPOLICY" }, | |
1108 | { config_parse_cpu_sched_prio, "CPUSCHEDPRIO" }, | |
1109 | { config_parse_cpu_affinity, "CPUAFFINITY" }, | |
1110 | { config_parse_mode, "MODE" }, | |
1111 | { config_parse_output, "OUTPUT" }, | |
1112 | { config_parse_input, "INPUT" }, | |
1113 | { config_parse_facility, "FACILITY" }, | |
1114 | { config_parse_level, "LEVEL" }, | |
1115 | { config_parse_capabilities, "CAPABILITIES" }, | |
1116 | { config_parse_secure_bits, "SECUREBITS" }, | |
1117 | { config_parse_bounding_set, "BOUNDINGSET" }, | |
1118 | { config_parse_timer_slack_ns, "TIMERSLACK" }, | |
1119 | { config_parse_limit, "LIMIT" }, | |
1120 | { config_parse_cgroup, "CGROUP [...]" }, | |
1121 | { config_parse_deps, "UNIT [...]" }, | |
1122 | { config_parse_names, "UNIT [...]" }, | |
1123 | { config_parse_exec, "PATH [ARGUMENT [...]]" }, | |
1124 | { config_parse_service_type, "SERVICETYPE" }, | |
1125 | { config_parse_service_restart, "SERVICERESTART" }, | |
1126 | { config_parse_sysv_priority, "SYSVPRIORITY" }, | |
1127 | { config_parse_kill_mode, "KILLMODE" }, | |
1128 | { config_parse_listen, "SOCKET [...]" }, | |
1129 | { config_parse_socket_bind, "SOCKETBIND" }, | |
93ef5e80 LP |
1130 | { config_parse_bindtodevice, "NETWORKINTERFACE" }, |
1131 | { config_parse_usec, "SECONDS" }, | |
1132 | { config_parse_path_strv, "PATH [...]" }, | |
932921b5 LP |
1133 | { config_parse_mount_flags, "MOUNTFLAG [...]" }, |
1134 | { config_parse_description, "DESCRIPTION" }, | |
e537352b LP |
1135 | }; |
1136 | ||
1137 | assert(f); | |
1138 | assert(items); | |
1139 | ||
1140 | for (i = items; i->lvalue; i++) { | |
1141 | unsigned j; | |
1142 | const char *rvalue = "OTHER"; | |
1143 | ||
1144 | if (!streq_ptr(i->section, prev_section)) { | |
1145 | if (!not_first) | |
1146 | not_first = true; | |
1147 | else | |
1148 | fputc('\n', f); | |
1149 | ||
1150 | fprintf(f, "[%s]\n", i->section); | |
1151 | prev_section = i->section; | |
1152 | } | |
1153 | ||
1154 | for (j = 0; j < ELEMENTSOF(table); j++) | |
1155 | if (i->parse == table[j].callback) { | |
1156 | rvalue = table[j].rvalue; | |
1157 | break; | |
1158 | } | |
1159 | ||
1160 | fprintf(f, "%s=%s\n", i->lvalue, rvalue); | |
1161 | } | |
1162 | } | |
1163 | ||
1164 | static int load_from_path(Unit *u, const char *path) { | |
87f0e418 LP |
1165 | |
1166 | static const char* const section_table[_UNIT_TYPE_MAX] = { | |
1167 | [UNIT_SERVICE] = "Service", | |
1168 | [UNIT_TIMER] = "Timer", | |
1169 | [UNIT_SOCKET] = "Socket", | |
1170 | [UNIT_TARGET] = "Target", | |
1171 | [UNIT_DEVICE] = "Device", | |
1172 | [UNIT_MOUNT] = "Mount", | |
1173 | [UNIT_AUTOMOUNT] = "Automount", | |
07b0b134 ML |
1174 | [UNIT_SNAPSHOT] = "Snapshot", |
1175 | [UNIT_SWAP] = "Swap" | |
42f4e3c4 LP |
1176 | }; |
1177 | ||
034c6ed7 | 1178 | #define EXEC_CONTEXT_CONFIG_ITEMS(context, section) \ |
9eba9da4 LP |
1179 | { "WorkingDirectory", config_parse_path, &(context).working_directory, section }, \ |
1180 | { "RootDirectory", config_parse_path, &(context).root_directory, section }, \ | |
1c01f82b LP |
1181 | { "User", config_parse_string, &(context).user, section }, \ |
1182 | { "Group", config_parse_string, &(context).group, section }, \ | |
1183 | { "SupplementaryGroups", config_parse_strv, &(context).supplementary_groups, section }, \ | |
fb33a393 LP |
1184 | { "Nice", config_parse_nice, &(context), section }, \ |
1185 | { "OOMAdjust", config_parse_oom_adjust, &(context), section }, \ | |
9eba9da4 | 1186 | { "IOSchedulingClass", config_parse_io_class, &(context), section }, \ |
94f04347 LP |
1187 | { "IOSchedulingPriority", config_parse_io_priority, &(context), section }, \ |
1188 | { "CPUSchedulingPolicy", config_parse_cpu_sched_policy,&(context), section }, \ | |
1189 | { "CPUSchedulingPriority", config_parse_cpu_sched_prio, &(context), section }, \ | |
38b48754 | 1190 | { "CPUSchedulingResetOnFork", config_parse_bool, &(context).cpu_sched_reset_on_fork, section }, \ |
94f04347 | 1191 | { "CPUAffinity", config_parse_cpu_affinity, &(context), section }, \ |
b5a0699f | 1192 | { "UMask", config_parse_mode, &(context).umask, section }, \ |
071830ff | 1193 | { "Environment", config_parse_strv, &(context).environment, section }, \ |
80876c20 LP |
1194 | { "StandardInput", config_parse_input, &(context).std_input, section }, \ |
1195 | { "StandardOutput", config_parse_output, &(context).std_output, section }, \ | |
1196 | { "StandardError", config_parse_output, &(context).std_output, section }, \ | |
1197 | { "TTYPath", config_parse_path, &(context).tty_path, section }, \ | |
071830ff LP |
1198 | { "SyslogIdentifier", config_parse_string, &(context).syslog_identifier, section }, \ |
1199 | { "SyslogFacility", config_parse_facility, &(context).syslog_priority, section }, \ | |
94f04347 LP |
1200 | { "SyslogLevel", config_parse_level, &(context).syslog_priority, section }, \ |
1201 | { "Capabilities", config_parse_capabilities, &(context), section }, \ | |
1202 | { "SecureBits", config_parse_secure_bits, &(context), section }, \ | |
1203 | { "CapabilityBoundingSetDrop", config_parse_bounding_set, &(context), section }, \ | |
1204 | { "TimerSlackNS", config_parse_timer_slack_ns, &(context), section }, \ | |
1205 | { "LimitCPU", config_parse_limit, &(context).rlimit[RLIMIT_CPU], section }, \ | |
1206 | { "LimitFSIZE", config_parse_limit, &(context).rlimit[RLIMIT_FSIZE], section }, \ | |
1207 | { "LimitDATA", config_parse_limit, &(context).rlimit[RLIMIT_DATA], section }, \ | |
1208 | { "LimitSTACK", config_parse_limit, &(context).rlimit[RLIMIT_STACK], section }, \ | |
1209 | { "LimitCORE", config_parse_limit, &(context).rlimit[RLIMIT_CORE], section }, \ | |
1210 | { "LimitRSS", config_parse_limit, &(context).rlimit[RLIMIT_RSS], section }, \ | |
1211 | { "LimitNOFILE", config_parse_limit, &(context).rlimit[RLIMIT_NOFILE], section }, \ | |
1212 | { "LimitAS", config_parse_limit, &(context).rlimit[RLIMIT_AS], section }, \ | |
1213 | { "LimitNPROC", config_parse_limit, &(context).rlimit[RLIMIT_NPROC], section }, \ | |
1214 | { "LimitMEMLOCK", config_parse_limit, &(context).rlimit[RLIMIT_MEMLOCK], section }, \ | |
1215 | { "LimitLOCKS", config_parse_limit, &(context).rlimit[RLIMIT_LOCKS], section }, \ | |
1216 | { "LimitSIGPENDING", config_parse_limit, &(context).rlimit[RLIMIT_SIGPENDING], section }, \ | |
1217 | { "LimitMSGQUEUE", config_parse_limit, &(context).rlimit[RLIMIT_MSGQUEUE], section }, \ | |
1218 | { "LimitNICE", config_parse_limit, &(context).rlimit[RLIMIT_NICE], section }, \ | |
1219 | { "LimitRTPRIO", config_parse_limit, &(context).rlimit[RLIMIT_RTPRIO], section }, \ | |
451a074f | 1220 | { "LimitRTTIME", config_parse_limit, &(context).rlimit[RLIMIT_RTTIME], section }, \ |
15ae422b LP |
1221 | { "ControlGroup", config_parse_cgroup, u, section }, \ |
1222 | { "ReadWriteDirectories", config_parse_path_strv, &(context).read_write_dirs, section }, \ | |
1223 | { "ReadOnlyDirectories", config_parse_path_strv, &(context).read_only_dirs, section }, \ | |
1224 | { "InaccessibleDirectories",config_parse_path_strv, &(context).inaccessible_dirs, section }, \ | |
1225 | { "PrivateTmp", config_parse_bool, &(context).private_tmp, section }, \ | |
1226 | { "MountFlags", config_parse_mount_flags, &(context), section } | |
034c6ed7 | 1227 | |
3efd4195 | 1228 | const ConfigItem items[] = { |
09477267 | 1229 | { "Names", config_parse_names, u, "Unit" }, |
932921b5 | 1230 | { "Description", config_parse_description, u, "Unit" }, |
09477267 LP |
1231 | { "Requires", config_parse_deps, UINT_TO_PTR(UNIT_REQUIRES), "Unit" }, |
1232 | { "RequiresOverridable", config_parse_deps, UINT_TO_PTR(UNIT_REQUIRES_OVERRIDABLE), "Unit" }, | |
1233 | { "Requisite", config_parse_deps, UINT_TO_PTR(UNIT_REQUISITE), "Unit" }, | |
1234 | { "RequisiteOverridable", config_parse_deps, UINT_TO_PTR(UNIT_REQUISITE_OVERRIDABLE), "Unit" }, | |
1235 | { "Wants", config_parse_deps, UINT_TO_PTR(UNIT_WANTS), "Unit" }, | |
1236 | { "Conflicts", config_parse_deps, UINT_TO_PTR(UNIT_CONFLICTS), "Unit" }, | |
1237 | { "Before", config_parse_deps, UINT_TO_PTR(UNIT_BEFORE), "Unit" }, | |
1238 | { "After", config_parse_deps, UINT_TO_PTR(UNIT_AFTER), "Unit" }, | |
1239 | { "RecursiveStop", config_parse_bool, &u->meta.recursive_stop, "Unit" }, | |
1240 | { "StopWhenUnneeded", config_parse_bool, &u->meta.stop_when_unneeded, "Unit" }, | |
1c01f82b LP |
1241 | |
1242 | { "PIDFile", config_parse_path, &u->service.pid_file, "Service" }, | |
1243 | { "ExecStartPre", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START_PRE, "Service" }, | |
1244 | { "ExecStart", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START, "Service" }, | |
1245 | { "ExecStartPost", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START_POST, "Service" }, | |
1246 | { "ExecReload", config_parse_exec, u->service.exec_command+SERVICE_EXEC_RELOAD, "Service" }, | |
1247 | { "ExecStop", config_parse_exec, u->service.exec_command+SERVICE_EXEC_STOP, "Service" }, | |
1248 | { "ExecStopPost", config_parse_exec, u->service.exec_command+SERVICE_EXEC_STOP_POST, "Service" }, | |
1249 | { "RestartSec", config_parse_usec, &u->service.restart_usec, "Service" }, | |
1250 | { "TimeoutSec", config_parse_usec, &u->service.timeout_usec, "Service" }, | |
0d87eb42 LP |
1251 | { "Type", config_parse_service_type, &u->service.type, "Service" }, |
1252 | { "Restart", config_parse_service_restart, &u->service.restart, "Service" }, | |
81a2b7ce LP |
1253 | { "PermissionsStartOnly", config_parse_bool, &u->service.permissions_start_only, "Service" }, |
1254 | { "RootDirectoryStartOnly", config_parse_bool, &u->service.root_directory_start_only, "Service" }, | |
8e274523 | 1255 | { "ValidNoProcess", config_parse_bool, &u->service.valid_no_process, "Service" }, |
a9a1e00a | 1256 | { "SysVStartPriority", config_parse_sysv_priority, &u->service.sysv_start_priority, "Service" }, |
50159e6a | 1257 | { "KillMode", config_parse_kill_mode, &u->service.kill_mode, "Service" }, |
b778d555 | 1258 | { "NonBlocking", config_parse_bool, &u->service.exec_context.non_blocking, "Service" }, |
05e343b7 | 1259 | { "BusName", config_parse_string, &u->service.bus_name, "Service" }, |
87f0e418 LP |
1260 | EXEC_CONTEXT_CONFIG_ITEMS(u->service.exec_context, "Service"), |
1261 | ||
1c01f82b LP |
1262 | { "ListenStream", config_parse_listen, &u->socket, "Socket" }, |
1263 | { "ListenDatagram", config_parse_listen, &u->socket, "Socket" }, | |
1264 | { "ListenSequentialPacket", config_parse_listen, &u->socket, "Socket" }, | |
1265 | { "ListenFIFO", config_parse_listen, &u->socket, "Socket" }, | |
1266 | { "BindIPv6Only", config_parse_socket_bind, &u->socket, "Socket" }, | |
1267 | { "Backlog", config_parse_unsigned, &u->socket.backlog, "Socket" }, | |
acbb0225 | 1268 | { "BindToDevice", config_parse_bindtodevice, &u->socket, "Socket" }, |
1c01f82b LP |
1269 | { "ExecStartPre", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_START_PRE, "Socket" }, |
1270 | { "ExecStartPost", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_START_POST, "Socket" }, | |
1271 | { "ExecStopPre", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_STOP_PRE, "Socket" }, | |
1272 | { "ExecStopPost", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_STOP_POST, "Socket" }, | |
108736d0 | 1273 | { "TimeoutSec", config_parse_usec, &u->socket.timeout_usec, "Socket" }, |
b5a0699f LP |
1274 | { "DirectoryMode", config_parse_mode, &u->socket.directory_mode, "Socket" }, |
1275 | { "SocketMode", config_parse_mode, &u->socket.socket_mode, "Socket" }, | |
50159e6a | 1276 | { "KillMode", config_parse_kill_mode, &u->socket.kill_mode, "Socket" }, |
4f2d528d | 1277 | { "Accept", config_parse_bool, &u->socket.accept, "Socket" }, |
87f0e418 LP |
1278 | EXEC_CONTEXT_CONFIG_ITEMS(u->socket.exec_context, "Socket"), |
1279 | ||
e537352b LP |
1280 | { "What", config_parse_string, &u->mount.parameters_fragment.what, "Mount" }, |
1281 | { "Where", config_parse_path, &u->mount.where, "Mount" }, | |
1282 | { "Options", config_parse_string, &u->mount.parameters_fragment.options, "Mount" }, | |
1283 | { "Type", config_parse_string, &u->mount.parameters_fragment.fstype, "Mount" }, | |
1284 | { "TimeoutSec", config_parse_usec, &u->mount.timeout_usec, "Mount" }, | |
1285 | { "KillMode", config_parse_kill_mode, &u->mount.kill_mode, "Mount" }, | |
1286 | EXEC_CONTEXT_CONFIG_ITEMS(u->mount.exec_context, "Mount"), | |
034c6ed7 | 1287 | |
8d567588 LP |
1288 | { "Where", config_parse_path, &u->automount.where, "Automount" }, |
1289 | ||
07b0b134 ML |
1290 | { "What", config_parse_path, &u->swap.what, "Swap" }, |
1291 | { "Priority", config_parse_unsigned, &u->swap.priority, "Swap" }, | |
1292 | { "NoAuto", config_parse_bool, &u->swap.no_auto, "Swap" }, | |
1293 | ||
3efd4195 LP |
1294 | { NULL, NULL, NULL, NULL } |
1295 | }; | |
1296 | ||
034c6ed7 | 1297 | #undef EXEC_CONTEXT_CONFIG_ITEMS |
42f4e3c4 | 1298 | |
42f4e3c4 | 1299 | const char *sections[3]; |
0301abf4 LP |
1300 | char *k; |
1301 | int r; | |
87f0e418 | 1302 | Set *symlink_names; |
23a177ef LP |
1303 | FILE *f = NULL; |
1304 | char *filename = NULL, *id = NULL; | |
1305 | Unit *merged; | |
1306 | ||
e537352b LP |
1307 | if (!u) { |
1308 | /* Dirty dirty hack. */ | |
1309 | dump_items((FILE*) path, items); | |
1310 | return 0; | |
1311 | } | |
1312 | ||
23a177ef | 1313 | assert(u); |
e537352b | 1314 | assert(path); |
3efd4195 | 1315 | |
09477267 | 1316 | sections[0] = "Unit"; |
87f0e418 | 1317 | sections[1] = section_table[u->meta.type]; |
42f4e3c4 LP |
1318 | sections[2] = NULL; |
1319 | ||
87f0e418 LP |
1320 | if (!(symlink_names = set_new(string_hash_func, string_compare_func))) |
1321 | return -ENOMEM; | |
3efd4195 | 1322 | |
036643a2 LP |
1323 | if (path_is_absolute(path)) { |
1324 | ||
1325 | if (!(filename = strdup(path))) { | |
1326 | r = -ENOMEM; | |
1327 | goto finish; | |
1328 | } | |
1329 | ||
1330 | if ((r = open_follow(&filename, &f, symlink_names, &id)) < 0) { | |
1331 | free(filename); | |
1332 | filename = NULL; | |
1333 | ||
1334 | if (r != -ENOENT) | |
1335 | goto finish; | |
1336 | } | |
1337 | ||
1338 | } else { | |
1339 | char **p; | |
1340 | ||
1341 | STRV_FOREACH(p, u->meta.manager->unit_path) { | |
1342 | ||
1343 | /* Instead of opening the path right away, we manually | |
1344 | * follow all symlinks and add their name to our unit | |
1345 | * name set while doing so */ | |
1346 | if (!(filename = path_make_absolute(path, *p))) { | |
1347 | r = -ENOMEM; | |
1348 | goto finish; | |
1349 | } | |
1350 | ||
1351 | if ((r = open_follow(&filename, &f, symlink_names, &id)) < 0) { | |
1352 | char *sn; | |
1353 | ||
1354 | free(filename); | |
1355 | filename = NULL; | |
1356 | ||
1357 | if (r != -ENOENT) | |
1358 | goto finish; | |
1359 | ||
1360 | /* Empty the symlink names for the next run */ | |
1361 | while ((sn = set_steal_first(symlink_names))) | |
1362 | free(sn); | |
3efd4195 | 1363 | |
036643a2 LP |
1364 | continue; |
1365 | } | |
1366 | ||
1367 | break; | |
1368 | } | |
1369 | } | |
034c6ed7 | 1370 | |
036643a2 | 1371 | if (!filename) { |
23a177ef | 1372 | r = 0; |
0301abf4 LP |
1373 | goto finish; |
1374 | } | |
87f0e418 | 1375 | |
23a177ef LP |
1376 | merged = u; |
1377 | if ((r = merge_by_names(&merged, symlink_names, id)) < 0) | |
0301abf4 | 1378 | goto finish; |
87f0e418 | 1379 | |
23a177ef | 1380 | if (merged != u) { |
e537352b | 1381 | u->meta.load_state = UNIT_MERGED; |
23a177ef LP |
1382 | r = 0; |
1383 | goto finish; | |
034c6ed7 LP |
1384 | } |
1385 | ||
23a177ef LP |
1386 | /* Now, parse the file contents */ |
1387 | if ((r = config_parse(filename, f, sections, items, u)) < 0) | |
1388 | goto finish; | |
b08d03ff | 1389 | |
6be1e7d5 LP |
1390 | free(u->meta.fragment_path); |
1391 | u->meta.fragment_path = filename; | |
0301abf4 | 1392 | filename = NULL; |
87f0e418 | 1393 | |
e537352b | 1394 | u->meta.load_state = UNIT_LOADED; |
23a177ef | 1395 | r = 0; |
87f0e418 LP |
1396 | |
1397 | finish: | |
1398 | while ((k = set_steal_first(symlink_names))) | |
1399 | free(k); | |
23a177ef | 1400 | |
87f0e418 | 1401 | set_free(symlink_names); |
0301abf4 LP |
1402 | free(filename); |
1403 | ||
23a177ef LP |
1404 | if (f) |
1405 | fclose(f); | |
1406 | ||
0301abf4 LP |
1407 | return r; |
1408 | } | |
1409 | ||
e537352b | 1410 | int unit_load_fragment(Unit *u) { |
23a177ef | 1411 | int r; |
0301abf4 LP |
1412 | |
1413 | assert(u); | |
23a177ef LP |
1414 | |
1415 | if (u->meta.fragment_path) { | |
1416 | ||
e537352b | 1417 | if ((r = load_from_path(u, u->meta.fragment_path)) < 0) |
23a177ef | 1418 | return r; |
0301abf4 | 1419 | |
23a177ef | 1420 | } else { |
0301abf4 | 1421 | Iterator i; |
890f434c | 1422 | const char *t; |
0301abf4 | 1423 | |
890f434c | 1424 | /* Try to find the unit under its id */ |
9e2f7c11 LP |
1425 | if ((r = load_from_path(u, u->meta.id)) < 0) |
1426 | return r; | |
890f434c LP |
1427 | |
1428 | /* Try to find an alias we can load this with */ | |
e537352b | 1429 | if (u->meta.load_state == UNIT_STUB) |
23a177ef | 1430 | SET_FOREACH(t, u->meta.names, i) { |
87f0e418 | 1431 | |
9e2f7c11 | 1432 | if (t == u->meta.id) |
23a177ef | 1433 | continue; |
071830ff | 1434 | |
e537352b | 1435 | if ((r = load_from_path(u, t)) < 0) |
23a177ef | 1436 | return r; |
890f434c | 1437 | |
e537352b | 1438 | if (u->meta.load_state != UNIT_STUB) |
23a177ef LP |
1439 | break; |
1440 | } | |
9e2f7c11 LP |
1441 | |
1442 | /* Now, follow the same logic, but look for a template */ | |
1443 | if (u->meta.load_state == UNIT_STUB && u->meta.instance) { | |
1444 | char *k; | |
1445 | ||
1446 | if (!(k = unit_name_template(u->meta.id))) | |
1447 | return -ENOMEM; | |
1448 | ||
1449 | r = load_from_path(u, k); | |
1450 | free(k); | |
1451 | ||
1452 | if (r < 0) | |
1453 | return r; | |
1454 | ||
1455 | if (u->meta.load_state == UNIT_STUB) | |
1456 | SET_FOREACH(t, u->meta.names, i) { | |
1457 | ||
1458 | if (t == u->meta.id) | |
1459 | continue; | |
1460 | ||
1461 | if (!(k = unit_name_template(t))) | |
1462 | return -ENOMEM; | |
1463 | ||
1464 | r = load_from_path(u, k); | |
1465 | free(k); | |
1466 | ||
1467 | if (r < 0) | |
1468 | return r; | |
1469 | ||
1470 | if (u->meta.load_state != UNIT_STUB) | |
1471 | break; | |
1472 | } | |
1473 | } | |
071830ff LP |
1474 | } |
1475 | ||
23a177ef | 1476 | return 0; |
3efd4195 | 1477 | } |
e537352b LP |
1478 | |
1479 | void unit_dump_config_items(FILE *f) { | |
1480 | /* OK, this wins a prize for extreme ugliness. */ | |
1481 | ||
1482 | load_from_path(NULL, (const void*) f); | |
1483 | } |