blob: 5e0637de65ed42120a76b80ce367cfae3ff7d74f [file] [log] [blame]
Lennart Poettering3efd4192009-11-19 23:13:20 +01001/*-*- Mode: C; c-basic-offset: 8 -*-*/
2
Lennart Poetteringa7334b02010-02-03 13:03:47 +01003/***
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
Lennart Poettering87f0e412010-01-26 21:39:06 +010022#include <linux/oom.h>
Lennart Poettering3efd4192009-11-19 23:13:20 +010023#include <assert.h>
24#include <errno.h>
25#include <string.h>
Lennart Poettering87f0e412010-01-26 21:39:06 +010026#include <unistd.h>
27#include <fcntl.h>
Lennart Poettering94f04342010-01-30 01:55:42 +010028#include <sched.h>
29#include <sys/prctl.h>
Lennart Poettering15ae4222010-04-21 22:15:06 +020030#include <sys/mount.h>
Kay Sievers25e870b2010-04-24 05:05:01 +020031#include <linux/fs.h>
Lennart Poettering3efd4192009-11-19 23:13:20 +010032
Lennart Poettering87f0e412010-01-26 21:39:06 +010033#include "unit.h"
Lennart Poettering3efd4192009-11-19 23:13:20 +010034#include "strv.h"
35#include "conf-parser.h"
36#include "load-fragment.h"
Lennart Poettering16354ef2010-01-20 19:19:53 +010037#include "log.h"
Lennart Poettering9eba9da2010-01-29 20:46:22 +010038#include "ioprio.h"
Lennart Poettering94f04342010-01-30 01:55:42 +010039#include "securebits.h"
40#include "missing.h"
Lennart Poettering9e2f7c12010-04-15 03:11:11 +020041#include "unit-name.h"
Lennart Poettering3efd4192009-11-19 23:13:20 +010042
Lennart Poettering0d87eb42010-04-13 02:22:41 +020043#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
Lennart Poettering42f4e3c2010-01-19 02:56:37 +010070static int config_parse_deps(
Lennart Poettering3efd4192009-11-19 23:13:20 +010071 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
Lennart Poettering87f0e412010-01-26 21:39:06 +010079 UnitDependency d = PTR_TO_UINT(data);
80 Unit *u = userdata;
Lennart Poettering3efd4192009-11-19 23:13:20 +010081 char *w;
82 size_t l;
83 char *state;
84
85 assert(filename);
86 assert(lvalue);
87 assert(rvalue);
Lennart Poettering3efd4192009-11-19 23:13:20 +010088
Lennart Poetteringf62c0e42010-02-14 01:07:36 +010089 FOREACH_WORD(w, l, rvalue, state) {
Lennart Poettering9e2f7c12010-04-15 03:11:11 +020090 char *t, *k;
Lennart Poettering3efd4192009-11-19 23:13:20 +010091 int r;
Lennart Poettering3efd4192009-11-19 23:13:20 +010092
93 if (!(t = strndup(w, l)))
94 return -ENOMEM;
95
Lennart Poettering9e2f7c12010-04-15 03:11:11 +020096 k = unit_name_printf(u, t);
Lennart Poettering3efd4192009-11-19 23:13:20 +010097 free(t);
98
Lennart Poettering9e2f7c12010-04-15 03:11:11 +020099 if (!k)
100 return -ENOMEM;
101
Lennart Poettering701cc382010-04-21 06:01:13 +0200102 r = unit_add_dependency_by_name(u, d, k, NULL, true);
Lennart Poettering9e2f7c12010-04-15 03:11:11 +0200103 free(k);
104
Lennart Poettering3efd4192009-11-19 23:13:20 +0100105 if (r < 0)
106 return r;
Lennart Poettering3efd4192009-11-19 23:13:20 +0100107 }
108
109 return 0;
110}
111
Lennart Poettering42f4e3c2010-01-19 02:56:37 +0100112static int config_parse_names(
Lennart Poettering87d15152010-01-18 23:50:13 +0100113 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
Lennart Poettering87f0e412010-01-26 21:39:06 +0100121 Unit *u = userdata;
Lennart Poettering87d15152010-01-18 23:50:13 +0100122 char *w;
123 size_t l;
124 char *state;
125
126 assert(filename);
127 assert(lvalue);
128 assert(rvalue);
129 assert(data);
130
Lennart Poetteringf62c0e42010-02-14 01:07:36 +0100131 FOREACH_WORD(w, l, rvalue, state) {
Lennart Poettering9e2f7c12010-04-15 03:11:11 +0200132 char *t, *k;
Lennart Poettering87d15152010-01-18 23:50:13 +0100133 int r;
Lennart Poettering87d15152010-01-18 23:50:13 +0100134
135 if (!(t = strndup(w, l)))
136 return -ENOMEM;
137
Lennart Poettering9e2f7c12010-04-15 03:11:11 +0200138 k = unit_name_printf(u, t);
Lennart Poettering87d15152010-01-18 23:50:13 +0100139 free(t);
Lennart Poettering23a177e2010-04-06 02:43:58 +0200140
Lennart Poettering9e2f7c12010-04-15 03:11:11 +0200141 if (!k)
142 return -ENOMEM;
143
144 r = unit_merge_by_name(u, k);
145 free(k);
146
Lennart Poettering23a177e2010-04-06 02:43:58 +0200147 if (r < 0)
148 return r;
Lennart Poettering87d15152010-01-18 23:50:13 +0100149 }
150
151 return 0;
152}
153
Lennart Poettering932921b2010-04-24 03:11:01 +0200154static 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
Lennart Poettering42f4e3c2010-01-19 02:56:37 +0100186static 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
Lennart Poettering16354ef2010-01-20 19:19:53 +0100195 int r;
Lennart Poettering542563b2010-01-23 03:35:54 +0100196 SocketPort *p;
197 Socket *s;
Lennart Poettering16354ef2010-01-20 19:19:53 +0100198
Lennart Poettering42f4e3c2010-01-19 02:56:37 +0100199 assert(filename);
200 assert(lvalue);
201 assert(rvalue);
202 assert(data);
203
Lennart Poettering542563b2010-01-23 03:35:54 +0100204 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 }
Lennart Poettering16354ef2010-01-20 19:19:53 +0100238 }
239
Lennart Poettering542563b2010-01-23 03:35:54 +0100240 p->fd = -1;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100241 LIST_PREPEND(SocketPort, port, s->ports, p);
Lennart Poettering542563b2010-01-23 03:35:54 +0100242
Lennart Poettering16354ef2010-01-20 19:19:53 +0100243 return 0;
Lennart Poettering42f4e3c2010-01-19 02:56:37 +0100244}
245
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100246static int config_parse_socket_bind(
Lennart Poettering42f4e3c2010-01-19 02:56:37 +0100247 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
Lennart Poettering542563b2010-01-23 03:35:54 +0100255 int r;
256 Socket *s;
Lennart Poettering42f4e3c2010-01-19 02:56:37 +0100257
258 assert(filename);
259 assert(lvalue);
260 assert(rvalue);
261 assert(data);
262
Lennart Poettering542563b2010-01-23 03:35:54 +0100263 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;
Lennart Poettering16354ef2010-01-20 19:19:53 +0100268 }
Lennart Poettering42f4e3c2010-01-19 02:56:37 +0100269
Lennart Poettering542563b2010-01-23 03:35:54 +0100270 s->bind_ipv6_only = r ? SOCKET_ADDRESS_IPV6_ONLY : SOCKET_ADDRESS_BOTH;
271
Lennart Poettering42f4e3c2010-01-19 02:56:37 +0100272 return 0;
273}
274
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100275static 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
Lennart Poetteringfb33a392010-01-28 02:53:56 +0100284 ExecContext *c = data;
285 int priority, r;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100286
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
Lennart Poetteringfb33a392010-01-28 02:53:56 +0100302 c->nice = priority;
303 c->nice_set = false;
304
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100305 return 0;
306}
307
308static 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
Lennart Poetteringfb33a392010-01-28 02:53:56 +0100317 ExecContext *c = data;
318 int oa, r;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100319
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
Lennart Poetteringfb33a392010-01-28 02:53:56 +0100335 c->oom_adjust = oa;
336 c->oom_adjust_set = true;
337
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100338 return 0;
339}
340
Lennart Poetteringb5a06992010-02-12 02:02:14 +0100341static int config_parse_mode(
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100342 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) {
Lennart Poetteringb5a06992010-02-12 02:02:14 +0100362 log_error("[%s:%u] Failed to parse mode value: %s", filename, line, rvalue);
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100363 return errno ? -errno : -EINVAL;
364 }
365
Lennart Poetteringb5a06992010-02-12 02:02:14 +0100366 if (l < 0000 || l > 07777) {
367 log_error("[%s:%u] mode value out of range: %s", filename, line, rvalue);
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100368 return -ERANGE;
369 }
370
371 *m = (mode_t) l;
372 return 0;
373}
374
375static 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
Lennart Poetteringa6a80b42010-02-14 01:05:55 +0100384 ExecCommand **e = data, *nce = NULL;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100385 char **n;
386 char *w;
387 unsigned k;
388 size_t l;
Lennart Poettering6c666e22010-05-19 21:51:53 +0200389 char *state, *path = NULL;
390 bool honour_argv0, write_to_path;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100391
392 assert(filename);
393 assert(lvalue);
394 assert(rvalue);
395 assert(data);
396
Lennart Poettering6c666e22010-05-19 21:51:53 +0200397 /* We accept an absolute path as first argument, or
398 * alternatively an absolute prefixed with @ to allow
399 * overriding of argv[0]. */
400
401 honour_argv0 = rvalue[0] == '@';
402
403 if (rvalue[honour_argv0 ? 1 : 0] != '/') {
404 log_error("[%s:%u] Invalid executable path in command line: %s", filename, line, rvalue);
405 return -EINVAL;
406 }
407
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100408 k = 0;
409 FOREACH_WORD_QUOTED(w, l, rvalue, state)
410 k++;
411
Lennart Poettering6c666e22010-05-19 21:51:53 +0200412 if (!(n = new(char*, k + (honour_argv0 ? 0 : 1))))
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100413 return -ENOMEM;
414
Lennart Poettering44d8db92010-01-26 07:02:51 +0100415 k = 0;
Lennart Poettering6c666e22010-05-19 21:51:53 +0200416 write_to_path = honour_argv0;
417 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
418 if (write_to_path) {
419 if (!(path = strndup(w+1, l-1)))
420 goto fail;
421 write_to_path = false;
422 } else {
423 if (!(n[k++] = strndup(w, l)))
424 goto fail;
425 }
426 }
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100427
428 n[k] = NULL;
429
Lennart Poettering6c666e22010-05-19 21:51:53 +0200430 if (!n[0]) {
431 log_error("[%s:%u] Invalid command line: %s", filename, line, rvalue);
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100432 strv_free(n);
433 return -EINVAL;
434 }
435
Lennart Poettering6c666e22010-05-19 21:51:53 +0200436 if (!path)
437 if (!(path = strdup(n[0])))
438 goto fail;
439
440 assert(path_is_absolute(path));
441
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100442 if (!(nce = new0(ExecCommand, 1)))
443 goto fail;
444
445 nce->argv = n;
Lennart Poettering6c666e22010-05-19 21:51:53 +0200446 nce->path = path;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100447
Lennart Poetteringa6a80b42010-02-14 01:05:55 +0100448 exec_command_append_list(e, nce);
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100449
450 return 0;
451
452fail:
Lennart Poettering6c666e22010-05-19 21:51:53 +0200453 n[k] = NULL;
454 strv_free(n);
455 free(path);
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100456 free(nce);
457
458 return -ENOMEM;
459}
460
461static int config_parse_usec(
462 const char *filename,
463 unsigned line,
464 const char *section,
465 const char *lvalue,
466 const char *rvalue,
467 void *data,
468 void *userdata) {
469
470 usec_t *usec = data;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100471 int r;
472
473 assert(filename);
474 assert(lvalue);
475 assert(rvalue);
476 assert(data);
477
Lennart Poettering24a6e4a2010-04-23 20:26:59 +0200478 if ((r = parse_usec(rvalue, usec)) < 0) {
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100479 log_error("[%s:%u] Failed to parse time value: %s", filename, line, rvalue);
480 return r;
481 }
482
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100483 return 0;
484}
485
Lennart Poettering0d87eb42010-04-13 02:22:41 +0200486DEFINE_CONFIG_PARSE_ENUM(config_parse_service_type, service_type, ServiceType, "Failed to parse service type");
487DEFINE_CONFIG_PARSE_ENUM(config_parse_service_restart, service_restart, ServiceRestart, "Failed to parse service restart specifier");
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100488
Lennart Poettering47be8702010-02-03 14:21:48 +0100489static int config_parse_bindtodevice(
Lennart Poetteringacbb0222010-01-27 04:31:52 +0100490 const char *filename,
491 unsigned line,
492 const char *section,
493 const char *lvalue,
494 const char *rvalue,
495 void *data,
496 void *userdata) {
497
498 Socket *s = data;
499 char *n;
500
501 assert(filename);
502 assert(lvalue);
503 assert(rvalue);
504 assert(data);
505
506 if (rvalue[0] && !streq(rvalue, "*")) {
507 if (!(n = strdup(rvalue)))
508 return -ENOMEM;
509 } else
510 n = NULL;
511
512 free(s->bind_to_device);
513 s->bind_to_device = n;
514
515 return 0;
516}
517
Lennart Poettering0d87eb42010-04-13 02:22:41 +0200518DEFINE_CONFIG_PARSE_ENUM(config_parse_output, exec_output, ExecOutput, "Failed to parse output specifier");
519DEFINE_CONFIG_PARSE_ENUM(config_parse_input, exec_input, ExecInput, "Failed to parse input specifier");
Lennart Poettering071830f2010-01-28 02:06:20 +0100520
Lennart Poettering47be8702010-02-03 14:21:48 +0100521static int config_parse_facility(
Lennart Poettering071830f2010-01-28 02:06:20 +0100522 const char *filename,
523 unsigned line,
524 const char *section,
525 const char *lvalue,
526 const char *rvalue,
527 void *data,
528 void *userdata) {
529
Lennart Poettering071830f2010-01-28 02:06:20 +0100530
Lennart Poettering94f04342010-01-30 01:55:42 +0100531 int *o = data, x;
Lennart Poettering071830f2010-01-28 02:06:20 +0100532
533 assert(filename);
534 assert(lvalue);
535 assert(rvalue);
536 assert(data);
537
Lennart Poettering0d87eb42010-04-13 02:22:41 +0200538 if ((x = log_facility_from_string(rvalue)) < 0) {
539 log_error("[%s:%u] Failed to parse log facility: %s", filename, line, rvalue);
540 return -EBADMSG;
541 }
Lennart Poettering94f04342010-01-30 01:55:42 +0100542
543 *o = LOG_MAKEPRI(x, LOG_PRI(*o));
Lennart Poettering071830f2010-01-28 02:06:20 +0100544
545 return 0;
546}
547
Lennart Poettering47be8702010-02-03 14:21:48 +0100548static int config_parse_level(
Lennart Poettering071830f2010-01-28 02:06:20 +0100549 const char *filename,
550 unsigned line,
551 const char *section,
552 const char *lvalue,
553 const char *rvalue,
554 void *data,
555 void *userdata) {
556
Lennart Poettering071830f2010-01-28 02:06:20 +0100557
Lennart Poettering94f04342010-01-30 01:55:42 +0100558 int *o = data, x;
Lennart Poettering071830f2010-01-28 02:06:20 +0100559
560 assert(filename);
561 assert(lvalue);
562 assert(rvalue);
563 assert(data);
564
Lennart Poettering0d87eb42010-04-13 02:22:41 +0200565 if ((x = log_level_from_string(rvalue)) < 0) {
566 log_error("[%s:%u] Failed to parse log level: %s", filename, line, rvalue);
567 return -EBADMSG;
568 }
Lennart Poettering071830f2010-01-28 02:06:20 +0100569
Lennart Poettering94f04342010-01-30 01:55:42 +0100570 *o = LOG_MAKEPRI(LOG_FAC(*o), x);
Lennart Poettering071830f2010-01-28 02:06:20 +0100571 return 0;
572}
573
Lennart Poettering47be8702010-02-03 14:21:48 +0100574static int config_parse_io_class(
Lennart Poettering9eba9da2010-01-29 20:46:22 +0100575 const char *filename,
576 unsigned line,
577 const char *section,
578 const char *lvalue,
579 const char *rvalue,
580 void *data,
581 void *userdata) {
582
Lennart Poettering9eba9da2010-01-29 20:46:22 +0100583 ExecContext *c = data;
Lennart Poettering94f04342010-01-30 01:55:42 +0100584 int x;
Lennart Poettering9eba9da2010-01-29 20:46:22 +0100585
586 assert(filename);
587 assert(lvalue);
588 assert(rvalue);
589 assert(data);
590
Lennart Poettering0d87eb42010-04-13 02:22:41 +0200591 if ((x = ioprio_class_from_string(rvalue)) < 0) {
592 log_error("[%s:%u] Failed to parse IO scheduling class: %s", filename, line, rvalue);
593 return -EBADMSG;
594 }
Lennart Poettering9eba9da2010-01-29 20:46:22 +0100595
Lennart Poettering94f04342010-01-30 01:55:42 +0100596 c->ioprio = IOPRIO_PRIO_VALUE(x, IOPRIO_PRIO_DATA(c->ioprio));
Lennart Poettering9eba9da2010-01-29 20:46:22 +0100597 c->ioprio_set = true;
598
599 return 0;
600}
601
Lennart Poettering47be8702010-02-03 14:21:48 +0100602static int config_parse_io_priority(
Lennart Poettering9eba9da2010-01-29 20:46:22 +0100603 const char *filename,
604 unsigned line,
605 const char *section,
606 const char *lvalue,
607 const char *rvalue,
608 void *data,
609 void *userdata) {
610
611 ExecContext *c = data;
612 int i;
613
614 assert(filename);
615 assert(lvalue);
616 assert(rvalue);
617 assert(data);
618
Lennart Poettering94f04342010-01-30 01:55:42 +0100619 if (safe_atoi(rvalue, &i) < 0 || i < 0 || i >= IOPRIO_BE_NR) {
Lennart Poettering9eba9da2010-01-29 20:46:22 +0100620 log_error("[%s:%u] Failed to parse io priority: %s", filename, line, rvalue);
621 return -EBADMSG;
622 }
623
Lennart Poettering94f04342010-01-30 01:55:42 +0100624 c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_PRIO_CLASS(c->ioprio), i);
Lennart Poettering9eba9da2010-01-29 20:46:22 +0100625 c->ioprio_set = true;
626
627 return 0;
628}
629
Lennart Poettering47be8702010-02-03 14:21:48 +0100630static int config_parse_cpu_sched_policy(
Lennart Poettering94f04342010-01-30 01:55:42 +0100631 const char *filename,
632 unsigned line,
633 const char *section,
634 const char *lvalue,
635 const char *rvalue,
636 void *data,
637 void *userdata) {
638
639
640 ExecContext *c = data;
641 int x;
642
643 assert(filename);
644 assert(lvalue);
645 assert(rvalue);
646 assert(data);
647
Lennart Poettering0d87eb42010-04-13 02:22:41 +0200648 if ((x = sched_policy_from_string(rvalue)) < 0) {
649 log_error("[%s:%u] Failed to parse CPU scheduling policy: %s", filename, line, rvalue);
650 return -EBADMSG;
651 }
Lennart Poettering94f04342010-01-30 01:55:42 +0100652
653 c->cpu_sched_policy = x;
654 c->cpu_sched_set = true;
655
656 return 0;
657}
658
Lennart Poettering47be8702010-02-03 14:21:48 +0100659static int config_parse_cpu_sched_prio(
Lennart Poettering94f04342010-01-30 01:55:42 +0100660 const char *filename,
661 unsigned line,
662 const char *section,
663 const char *lvalue,
664 const char *rvalue,
665 void *data,
666 void *userdata) {
667
668 ExecContext *c = data;
669 int i;
670
671 assert(filename);
672 assert(lvalue);
673 assert(rvalue);
674 assert(data);
675
676 /* On Linux RR/FIFO have the same range */
677 if (safe_atoi(rvalue, &i) < 0 || i < sched_get_priority_min(SCHED_RR) || i > sched_get_priority_max(SCHED_RR)) {
678 log_error("[%s:%u] Failed to parse CPU scheduling priority: %s", filename, line, rvalue);
679 return -EBADMSG;
680 }
681
682 c->cpu_sched_priority = i;
683 c->cpu_sched_set = true;
684
685 return 0;
686}
687
Lennart Poettering47be8702010-02-03 14:21:48 +0100688static int config_parse_cpu_affinity(
Lennart Poettering94f04342010-01-30 01:55:42 +0100689 const char *filename,
690 unsigned line,
691 const char *section,
692 const char *lvalue,
693 const char *rvalue,
694 void *data,
695 void *userdata) {
696
697 ExecContext *c = data;
698 char *w;
699 size_t l;
700 char *state;
701
702 assert(filename);
703 assert(lvalue);
704 assert(rvalue);
705 assert(data);
706
Lennart Poetteringf62c0e42010-02-14 01:07:36 +0100707 FOREACH_WORD(w, l, rvalue, state) {
Lennart Poettering94f04342010-01-30 01:55:42 +0100708 char *t;
709 int r;
710 unsigned cpu;
711
712 if (!(t = strndup(w, l)))
713 return -ENOMEM;
714
715 r = safe_atou(t, &cpu);
716 free(t);
717
718 if (r < 0 || cpu >= CPU_SETSIZE) {
719 log_error("[%s:%u] Failed to parse CPU affinity: %s", filename, line, rvalue);
720 return -EBADMSG;
721 }
722
723 CPU_SET(cpu, &c->cpu_affinity);
724 }
725
726 c->cpu_affinity_set = true;
727
728 return 0;
729}
730
Lennart Poettering47be8702010-02-03 14:21:48 +0100731static int config_parse_capabilities(
Lennart Poettering94f04342010-01-30 01:55:42 +0100732 const char *filename,
733 unsigned line,
734 const char *section,
735 const char *lvalue,
736 const char *rvalue,
737 void *data,
738 void *userdata) {
739
740 ExecContext *c = data;
741 cap_t cap;
742
743 assert(filename);
744 assert(lvalue);
745 assert(rvalue);
746 assert(data);
747
748 if (!(cap = cap_from_text(rvalue))) {
749 if (errno == ENOMEM)
750 return -ENOMEM;
751
752 log_error("[%s:%u] Failed to parse capabilities: %s", filename, line, rvalue);
753 return -EBADMSG;
754 }
755
756 if (c->capabilities)
757 cap_free(c->capabilities);
758 c->capabilities = cap;
759
760 return 0;
761}
762
Lennart Poettering47be8702010-02-03 14:21:48 +0100763static int config_parse_secure_bits(
Lennart Poettering94f04342010-01-30 01:55:42 +0100764 const char *filename,
765 unsigned line,
766 const char *section,
767 const char *lvalue,
768 const char *rvalue,
769 void *data,
770 void *userdata) {
771
772 ExecContext *c = data;
773 char *w;
774 size_t l;
775 char *state;
776
777 assert(filename);
778 assert(lvalue);
779 assert(rvalue);
780 assert(data);
781
Lennart Poetteringf62c0e42010-02-14 01:07:36 +0100782 FOREACH_WORD(w, l, rvalue, state) {
Lennart Poettering94f04342010-01-30 01:55:42 +0100783 if (first_word(w, "keep-caps"))
784 c->secure_bits |= SECURE_KEEP_CAPS;
785 else if (first_word(w, "keep-caps-locked"))
786 c->secure_bits |= SECURE_KEEP_CAPS_LOCKED;
787 else if (first_word(w, "no-setuid-fixup"))
788 c->secure_bits |= SECURE_NO_SETUID_FIXUP;
789 else if (first_word(w, "no-setuid-fixup-locked"))
790 c->secure_bits |= SECURE_NO_SETUID_FIXUP_LOCKED;
791 else if (first_word(w, "noroot"))
792 c->secure_bits |= SECURE_NOROOT;
793 else if (first_word(w, "noroot-locked"))
794 c->secure_bits |= SECURE_NOROOT_LOCKED;
795 else {
796 log_error("[%s:%u] Failed to parse secure bits: %s", filename, line, rvalue);
797 return -EBADMSG;
798 }
799 }
800
801 return 0;
802}
803
Lennart Poettering47be8702010-02-03 14:21:48 +0100804static int config_parse_bounding_set(
Lennart Poettering94f04342010-01-30 01:55:42 +0100805 const char *filename,
806 unsigned line,
807 const char *section,
808 const char *lvalue,
809 const char *rvalue,
810 void *data,
811 void *userdata) {
812
813 ExecContext *c = data;
814 char *w;
815 size_t l;
816 char *state;
817
818 assert(filename);
819 assert(lvalue);
820 assert(rvalue);
821 assert(data);
822
Lennart Poetteringf62c0e42010-02-14 01:07:36 +0100823 FOREACH_WORD(w, l, rvalue, state) {
Lennart Poettering94f04342010-01-30 01:55:42 +0100824 char *t;
825 int r;
826 cap_value_t cap;
827
828 if (!(t = strndup(w, l)))
829 return -ENOMEM;
830
831 r = cap_from_name(t, &cap);
832 free(t);
833
834 if (r < 0) {
835 log_error("[%s:%u] Failed to parse capability bounding set: %s", filename, line, rvalue);
836 return -EBADMSG;
837 }
838
839 c->capability_bounding_set_drop |= 1 << cap;
840 }
841
842 return 0;
843}
844
845static int config_parse_timer_slack_ns(
846 const char *filename,
847 unsigned line,
848 const char *section,
849 const char *lvalue,
850 const char *rvalue,
851 void *data,
852 void *userdata) {
853
854 ExecContext *c = data;
855 unsigned long u;
856 int r;
857
858 assert(filename);
859 assert(lvalue);
860 assert(rvalue);
861 assert(data);
862
863 if ((r = safe_atolu(rvalue, &u)) < 0) {
864 log_error("[%s:%u] Failed to parse time slack value: %s", filename, line, rvalue);
865 return r;
866 }
867
868 c->timer_slack_ns = u;
869
870 return 0;
871}
872
873static int config_parse_limit(
874 const char *filename,
875 unsigned line,
876 const char *section,
877 const char *lvalue,
878 const char *rvalue,
879 void *data,
880 void *userdata) {
881
882 struct rlimit **rl = data;
883 unsigned long long u;
884 int r;
885
886 assert(filename);
887 assert(lvalue);
888 assert(rvalue);
889 assert(data);
890
891 if ((r = safe_atollu(rvalue, &u)) < 0) {
892 log_error("[%s:%u] Failed to parse resource value: %s", filename, line, rvalue);
893 return r;
894 }
895
896 if (!*rl)
897 if (!(*rl = new(struct rlimit, 1)))
898 return -ENOMEM;
899
900 (*rl)->rlim_cur = (*rl)->rlim_max = (rlim_t) u;
901 return 0;
902}
903
Lennart Poettering8e274522010-03-31 16:29:55 +0200904static int config_parse_cgroup(
905 const char *filename,
906 unsigned line,
907 const char *section,
908 const char *lvalue,
909 const char *rvalue,
910 void *data,
911 void *userdata) {
912
913 Unit *u = userdata;
914 char *w;
915 size_t l;
916 char *state;
917
918 FOREACH_WORD(w, l, rvalue, state) {
919 char *t;
920 int r;
921
922 if (!(t = strndup(w, l)))
923 return -ENOMEM;
924
925 r = unit_add_cgroup_from_text(u, t);
926 free(t);
927
928 if (r < 0)
929 return r;
930 }
931
932 return 0;
933}
934
Lennart Poetteringa9a1e002010-04-06 02:40:10 +0200935static int config_parse_sysv_priority(
936 const char *filename,
937 unsigned line,
938 const char *section,
939 const char *lvalue,
940 const char *rvalue,
941 void *data,
942 void *userdata) {
943
944 int *priority = data;
945 int r, i;
946
947 assert(filename);
948 assert(lvalue);
949 assert(rvalue);
950 assert(data);
951
952 if ((r = safe_atoi(rvalue, &i)) < 0 || i < 0) {
953 log_error("[%s:%u] Failed to parse SysV start priority: %s", filename, line, rvalue);
954 return r;
955 }
956
957 *priority = (int) i;
958 return 0;
959}
960
Lennart Poettering0d87eb42010-04-13 02:22:41 +0200961DEFINE_CONFIG_PARSE_ENUM(config_parse_kill_mode, kill_mode, KillMode, "Failed to parse kill mode");
Lennart Poettering50159e62010-04-08 00:52:14 +0200962
Lennart Poettering15ae4222010-04-21 22:15:06 +0200963static int config_parse_mount_flags(
964 const char *filename,
965 unsigned line,
966 const char *section,
967 const char *lvalue,
968 const char *rvalue,
969 void *data,
970 void *userdata) {
971
972 ExecContext *c = data;
973 char *w;
974 size_t l;
975 char *state;
976 unsigned long flags = 0;
977
978 assert(filename);
979 assert(lvalue);
980 assert(rvalue);
981 assert(data);
982
983 FOREACH_WORD(w, l, rvalue, state) {
984 if (strncmp(w, "shared", l) == 0)
985 flags |= MS_SHARED;
986 else if (strncmp(w, "slave", l) == 0)
987 flags |= MS_SLAVE;
988 else if (strncmp(w, "private", l) == 0)
989 flags |= MS_PRIVATE;
990 else {
991 log_error("[%s:%u] Failed to parse mount flags: %s", filename, line, rvalue);
992 return -EINVAL;
993 }
994 }
995
996 c->mount_flags = flags;
997 return 0;
998}
999
Lennart Poettering071830f2010-01-28 02:06:20 +01001000#define FOLLOW_MAX 8
Lennart Poettering87f0e412010-01-26 21:39:06 +01001001
Lennart Poettering9e2f7c12010-04-15 03:11:11 +02001002static int open_follow(char **filename, FILE **_f, Set *names, char **_final) {
Lennart Poettering0301abf2010-01-27 00:15:56 +01001003 unsigned c = 0;
Lennart Poettering87f0e412010-01-26 21:39:06 +01001004 int fd, r;
1005 FILE *f;
Lennart Poettering0301abf2010-01-27 00:15:56 +01001006 char *id = NULL;
Lennart Poettering87f0e412010-01-26 21:39:06 +01001007
1008 assert(filename);
1009 assert(*filename);
1010 assert(_f);
1011 assert(names);
1012
Lennart Poettering0301abf2010-01-27 00:15:56 +01001013 /* This will update the filename pointer if the loaded file is
1014 * reached by a symlink. The old string will be freed. */
Lennart Poettering87f0e412010-01-26 21:39:06 +01001015
Lennart Poettering0301abf2010-01-27 00:15:56 +01001016 for (;;) {
Lennart Poettering87f0e412010-01-26 21:39:06 +01001017 char *target, *k, *name;
1018
Lennart Poettering0301abf2010-01-27 00:15:56 +01001019 if (c++ >= FOLLOW_MAX)
1020 return -ELOOP;
1021
Lennart Poetteringb08d03f2010-01-29 02:07:41 +01001022 path_kill_slashes(*filename);
1023
Lennart Poettering87f0e412010-01-26 21:39:06 +01001024 /* Add the file name we are currently looking at to
1025 * the names of this unit */
Lennart Poettering0301abf2010-01-27 00:15:56 +01001026 name = file_name_from_path(*filename);
1027 if (!(id = set_get(names, name))) {
Lennart Poettering87f0e412010-01-26 21:39:06 +01001028
Lennart Poettering0301abf2010-01-27 00:15:56 +01001029 if (!(id = strdup(name)))
1030 return -ENOMEM;
1031
1032 if ((r = set_put(names, id)) < 0) {
1033 free(id);
1034 return r;
Lennart Poettering87f0e412010-01-26 21:39:06 +01001035 }
Lennart Poettering87f0e412010-01-26 21:39:06 +01001036 }
1037
Lennart Poettering0301abf2010-01-27 00:15:56 +01001038 /* Try to open the file name, but don't if its a symlink */
1039 if ((fd = open(*filename, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW)) >= 0)
Lennart Poettering87f0e412010-01-26 21:39:06 +01001040 break;
1041
Lennart Poettering0301abf2010-01-27 00:15:56 +01001042 if (errno != ELOOP)
1043 return -errno;
Lennart Poettering87f0e412010-01-26 21:39:06 +01001044
Lennart Poettering0301abf2010-01-27 00:15:56 +01001045 /* Hmm, so this is a symlink. Let's read the name, and follow it manually */
1046 if ((r = readlink_malloc(*filename, &target)) < 0)
1047 return r;
1048
Lennart Poetteringc25fb0e2010-01-27 22:39:10 +01001049 k = file_in_same_dir(*filename, target);
Lennart Poettering87f0e412010-01-26 21:39:06 +01001050 free(target);
1051
Lennart Poettering0301abf2010-01-27 00:15:56 +01001052 if (!k)
1053 return -ENOMEM;
Lennart Poettering87f0e412010-01-26 21:39:06 +01001054
Lennart Poettering0301abf2010-01-27 00:15:56 +01001055 free(*filename);
1056 *filename = k;
Lennart Poettering87f0e412010-01-26 21:39:06 +01001057 }
1058
1059 if (!(f = fdopen(fd, "r"))) {
1060 r = -errno;
Lennart Poettering9e2f7c12010-04-15 03:11:11 +02001061 close_nointr_nofail(fd);
Lennart Poettering0301abf2010-01-27 00:15:56 +01001062 return r;
Lennart Poettering87f0e412010-01-26 21:39:06 +01001063 }
1064
1065 *_f = f;
Lennart Poettering9e2f7c12010-04-15 03:11:11 +02001066 *_final = id;
Lennart Poettering0301abf2010-01-27 00:15:56 +01001067 return 0;
Lennart Poettering87f0e412010-01-26 21:39:06 +01001068}
1069
Lennart Poettering23a177e2010-04-06 02:43:58 +02001070static int merge_by_names(Unit **u, Set *names, const char *id) {
1071 char *k;
1072 int r;
1073
1074 assert(u);
1075 assert(*u);
1076 assert(names);
1077
1078 /* Let's try to add in all symlink names we found */
1079 while ((k = set_steal_first(names))) {
1080
1081 /* First try to merge in the other name into our
1082 * unit */
1083 if ((r = unit_merge_by_name(*u, k)) < 0) {
1084 Unit *other;
1085
1086 /* Hmm, we couldn't merge the other unit into
1087 * ours? Then let's try it the other way
1088 * round */
1089
1090 other = manager_get_unit((*u)->meta.manager, k);
1091 free(k);
1092
1093 if (other)
1094 if ((r = unit_merge(other, *u)) >= 0) {
1095 *u = other;
1096 return merge_by_names(u, names, NULL);
1097 }
1098
1099 return r;
1100 }
1101
1102 if (id == k)
1103 unit_choose_id(*u, id);
1104
1105 free(k);
1106 }
1107
1108 return 0;
1109}
1110
Lennart Poetteringe5373522010-04-10 17:53:17 +02001111static void dump_items(FILE *f, const ConfigItem *items) {
1112 const ConfigItem *i;
1113 const char *prev_section = NULL;
1114 bool not_first = false;
1115
1116 struct {
1117 ConfigParserCallback callback;
1118 const char *rvalue;
1119 } table[] = {
1120 { config_parse_int, "INTEGER" },
1121 { config_parse_unsigned, "UNSIGNED" },
1122 { config_parse_size, "SIZE" },
1123 { config_parse_bool, "BOOLEAN" },
1124 { config_parse_string, "STRING" },
1125 { config_parse_path, "PATH" },
1126 { config_parse_strv, "STRING [...]" },
1127 { config_parse_nice, "NICE" },
1128 { config_parse_oom_adjust, "OOMADJUST" },
1129 { config_parse_io_class, "IOCLASS" },
1130 { config_parse_io_priority, "IOPRIORITY" },
1131 { config_parse_cpu_sched_policy, "CPUSCHEDPOLICY" },
1132 { config_parse_cpu_sched_prio, "CPUSCHEDPRIO" },
1133 { config_parse_cpu_affinity, "CPUAFFINITY" },
1134 { config_parse_mode, "MODE" },
1135 { config_parse_output, "OUTPUT" },
1136 { config_parse_input, "INPUT" },
1137 { config_parse_facility, "FACILITY" },
1138 { config_parse_level, "LEVEL" },
1139 { config_parse_capabilities, "CAPABILITIES" },
1140 { config_parse_secure_bits, "SECUREBITS" },
1141 { config_parse_bounding_set, "BOUNDINGSET" },
1142 { config_parse_timer_slack_ns, "TIMERSLACK" },
1143 { config_parse_limit, "LIMIT" },
1144 { config_parse_cgroup, "CGROUP [...]" },
1145 { config_parse_deps, "UNIT [...]" },
1146 { config_parse_names, "UNIT [...]" },
1147 { config_parse_exec, "PATH [ARGUMENT [...]]" },
1148 { config_parse_service_type, "SERVICETYPE" },
1149 { config_parse_service_restart, "SERVICERESTART" },
1150 { config_parse_sysv_priority, "SYSVPRIORITY" },
1151 { config_parse_kill_mode, "KILLMODE" },
1152 { config_parse_listen, "SOCKET [...]" },
1153 { config_parse_socket_bind, "SOCKETBIND" },
Lennart Poettering93ef5e82010-04-23 20:27:47 +02001154 { config_parse_bindtodevice, "NETWORKINTERFACE" },
1155 { config_parse_usec, "SECONDS" },
1156 { config_parse_path_strv, "PATH [...]" },
Lennart Poettering932921b2010-04-24 03:11:01 +02001157 { config_parse_mount_flags, "MOUNTFLAG [...]" },
1158 { config_parse_description, "DESCRIPTION" },
Lennart Poetteringe5373522010-04-10 17:53:17 +02001159 };
1160
1161 assert(f);
1162 assert(items);
1163
1164 for (i = items; i->lvalue; i++) {
1165 unsigned j;
1166 const char *rvalue = "OTHER";
1167
1168 if (!streq_ptr(i->section, prev_section)) {
1169 if (!not_first)
1170 not_first = true;
1171 else
1172 fputc('\n', f);
1173
1174 fprintf(f, "[%s]\n", i->section);
1175 prev_section = i->section;
1176 }
1177
1178 for (j = 0; j < ELEMENTSOF(table); j++)
1179 if (i->parse == table[j].callback) {
1180 rvalue = table[j].rvalue;
1181 break;
1182 }
1183
1184 fprintf(f, "%s=%s\n", i->lvalue, rvalue);
1185 }
1186}
1187
1188static int load_from_path(Unit *u, const char *path) {
Lennart Poettering87f0e412010-01-26 21:39:06 +01001189
1190 static const char* const section_table[_UNIT_TYPE_MAX] = {
1191 [UNIT_SERVICE] = "Service",
1192 [UNIT_TIMER] = "Timer",
1193 [UNIT_SOCKET] = "Socket",
1194 [UNIT_TARGET] = "Target",
1195 [UNIT_DEVICE] = "Device",
1196 [UNIT_MOUNT] = "Mount",
1197 [UNIT_AUTOMOUNT] = "Automount",
Maarten Lankhorst07b0b132010-05-09 18:44:11 +02001198 [UNIT_SNAPSHOT] = "Snapshot",
1199 [UNIT_SWAP] = "Swap"
Lennart Poettering42f4e3c2010-01-19 02:56:37 +01001200 };
1201
Lennart Poettering034c6ed2010-01-26 04:18:44 +01001202#define EXEC_CONTEXT_CONFIG_ITEMS(context, section) \
Lennart Poettering9eba9da2010-01-29 20:46:22 +01001203 { "WorkingDirectory", config_parse_path, &(context).working_directory, section }, \
1204 { "RootDirectory", config_parse_path, &(context).root_directory, section }, \
Lennart Poettering1c01f822010-01-27 02:16:11 +01001205 { "User", config_parse_string, &(context).user, section }, \
1206 { "Group", config_parse_string, &(context).group, section }, \
1207 { "SupplementaryGroups", config_parse_strv, &(context).supplementary_groups, section }, \
Lennart Poetteringfb33a392010-01-28 02:53:56 +01001208 { "Nice", config_parse_nice, &(context), section }, \
1209 { "OOMAdjust", config_parse_oom_adjust, &(context), section }, \
Lennart Poettering9eba9da2010-01-29 20:46:22 +01001210 { "IOSchedulingClass", config_parse_io_class, &(context), section }, \
Lennart Poettering94f04342010-01-30 01:55:42 +01001211 { "IOSchedulingPriority", config_parse_io_priority, &(context), section }, \
1212 { "CPUSchedulingPolicy", config_parse_cpu_sched_policy,&(context), section }, \
1213 { "CPUSchedulingPriority", config_parse_cpu_sched_prio, &(context), section }, \
Lennart Poettering38b48752010-02-02 12:50:04 +01001214 { "CPUSchedulingResetOnFork", config_parse_bool, &(context).cpu_sched_reset_on_fork, section }, \
Lennart Poettering94f04342010-01-30 01:55:42 +01001215 { "CPUAffinity", config_parse_cpu_affinity, &(context), section }, \
Lennart Poetteringb5a06992010-02-12 02:02:14 +01001216 { "UMask", config_parse_mode, &(context).umask, section }, \
Lennart Poettering071830f2010-01-28 02:06:20 +01001217 { "Environment", config_parse_strv, &(context).environment, section }, \
Lennart Poettering80876c22010-04-13 02:06:27 +02001218 { "StandardInput", config_parse_input, &(context).std_input, section }, \
1219 { "StandardOutput", config_parse_output, &(context).std_output, section }, \
1220 { "StandardError", config_parse_output, &(context).std_output, section }, \
1221 { "TTYPath", config_parse_path, &(context).tty_path, section }, \
Lennart Poettering071830f2010-01-28 02:06:20 +01001222 { "SyslogIdentifier", config_parse_string, &(context).syslog_identifier, section }, \
1223 { "SyslogFacility", config_parse_facility, &(context).syslog_priority, section }, \
Lennart Poettering94f04342010-01-30 01:55:42 +01001224 { "SyslogLevel", config_parse_level, &(context).syslog_priority, section }, \
Lennart Poettering4f4a1db2010-05-16 01:46:35 +02001225 { "SyslogNoPrefix", config_parse_bool, &(context).syslog_no_prefix, section }, \
Lennart Poettering94f04342010-01-30 01:55:42 +01001226 { "Capabilities", config_parse_capabilities, &(context), section }, \
1227 { "SecureBits", config_parse_secure_bits, &(context), section }, \
1228 { "CapabilityBoundingSetDrop", config_parse_bounding_set, &(context), section }, \
1229 { "TimerSlackNS", config_parse_timer_slack_ns, &(context), section }, \
1230 { "LimitCPU", config_parse_limit, &(context).rlimit[RLIMIT_CPU], section }, \
1231 { "LimitFSIZE", config_parse_limit, &(context).rlimit[RLIMIT_FSIZE], section }, \
1232 { "LimitDATA", config_parse_limit, &(context).rlimit[RLIMIT_DATA], section }, \
1233 { "LimitSTACK", config_parse_limit, &(context).rlimit[RLIMIT_STACK], section }, \
1234 { "LimitCORE", config_parse_limit, &(context).rlimit[RLIMIT_CORE], section }, \
1235 { "LimitRSS", config_parse_limit, &(context).rlimit[RLIMIT_RSS], section }, \
1236 { "LimitNOFILE", config_parse_limit, &(context).rlimit[RLIMIT_NOFILE], section }, \
1237 { "LimitAS", config_parse_limit, &(context).rlimit[RLIMIT_AS], section }, \
1238 { "LimitNPROC", config_parse_limit, &(context).rlimit[RLIMIT_NPROC], section }, \
1239 { "LimitMEMLOCK", config_parse_limit, &(context).rlimit[RLIMIT_MEMLOCK], section }, \
1240 { "LimitLOCKS", config_parse_limit, &(context).rlimit[RLIMIT_LOCKS], section }, \
1241 { "LimitSIGPENDING", config_parse_limit, &(context).rlimit[RLIMIT_SIGPENDING], section }, \
1242 { "LimitMSGQUEUE", config_parse_limit, &(context).rlimit[RLIMIT_MSGQUEUE], section }, \
1243 { "LimitNICE", config_parse_limit, &(context).rlimit[RLIMIT_NICE], section }, \
1244 { "LimitRTPRIO", config_parse_limit, &(context).rlimit[RLIMIT_RTPRIO], section }, \
Lennart Poettering451a0742010-02-12 02:00:18 +01001245 { "LimitRTTIME", config_parse_limit, &(context).rlimit[RLIMIT_RTTIME], section }, \
Lennart Poettering15ae4222010-04-21 22:15:06 +02001246 { "ControlGroup", config_parse_cgroup, u, section }, \
1247 { "ReadWriteDirectories", config_parse_path_strv, &(context).read_write_dirs, section }, \
1248 { "ReadOnlyDirectories", config_parse_path_strv, &(context).read_only_dirs, section }, \
1249 { "InaccessibleDirectories",config_parse_path_strv, &(context).inaccessible_dirs, section }, \
1250 { "PrivateTmp", config_parse_bool, &(context).private_tmp, section }, \
1251 { "MountFlags", config_parse_mount_flags, &(context), section }
Lennart Poettering034c6ed2010-01-26 04:18:44 +01001252
Lennart Poettering3efd4192009-11-19 23:13:20 +01001253 const ConfigItem items[] = {
Lennart Poettering09477262010-04-15 23:20:17 +02001254 { "Names", config_parse_names, u, "Unit" },
Lennart Poettering932921b2010-04-24 03:11:01 +02001255 { "Description", config_parse_description, u, "Unit" },
Lennart Poettering09477262010-04-15 23:20:17 +02001256 { "Requires", config_parse_deps, UINT_TO_PTR(UNIT_REQUIRES), "Unit" },
1257 { "RequiresOverridable", config_parse_deps, UINT_TO_PTR(UNIT_REQUIRES_OVERRIDABLE), "Unit" },
1258 { "Requisite", config_parse_deps, UINT_TO_PTR(UNIT_REQUISITE), "Unit" },
1259 { "RequisiteOverridable", config_parse_deps, UINT_TO_PTR(UNIT_REQUISITE_OVERRIDABLE), "Unit" },
1260 { "Wants", config_parse_deps, UINT_TO_PTR(UNIT_WANTS), "Unit" },
1261 { "Conflicts", config_parse_deps, UINT_TO_PTR(UNIT_CONFLICTS), "Unit" },
1262 { "Before", config_parse_deps, UINT_TO_PTR(UNIT_BEFORE), "Unit" },
1263 { "After", config_parse_deps, UINT_TO_PTR(UNIT_AFTER), "Unit" },
1264 { "RecursiveStop", config_parse_bool, &u->meta.recursive_stop, "Unit" },
1265 { "StopWhenUnneeded", config_parse_bool, &u->meta.stop_when_unneeded, "Unit" },
Lennart Poettering034c6ed2010-01-26 04:18:44 +01001266
Lennart Poettering1c01f822010-01-27 02:16:11 +01001267 { "PIDFile", config_parse_path, &u->service.pid_file, "Service" },
1268 { "ExecStartPre", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START_PRE, "Service" },
1269 { "ExecStart", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START, "Service" },
1270 { "ExecStartPost", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START_POST, "Service" },
1271 { "ExecReload", config_parse_exec, u->service.exec_command+SERVICE_EXEC_RELOAD, "Service" },
1272 { "ExecStop", config_parse_exec, u->service.exec_command+SERVICE_EXEC_STOP, "Service" },
1273 { "ExecStopPost", config_parse_exec, u->service.exec_command+SERVICE_EXEC_STOP_POST, "Service" },
1274 { "RestartSec", config_parse_usec, &u->service.restart_usec, "Service" },
1275 { "TimeoutSec", config_parse_usec, &u->service.timeout_usec, "Service" },
Lennart Poettering0d87eb42010-04-13 02:22:41 +02001276 { "Type", config_parse_service_type, &u->service.type, "Service" },
1277 { "Restart", config_parse_service_restart, &u->service.restart, "Service" },
Lennart Poettering81a2b7c2010-02-14 22:43:08 +01001278 { "PermissionsStartOnly", config_parse_bool, &u->service.permissions_start_only, "Service" },
1279 { "RootDirectoryStartOnly", config_parse_bool, &u->service.root_directory_start_only, "Service" },
Lennart Poettering8e274522010-03-31 16:29:55 +02001280 { "ValidNoProcess", config_parse_bool, &u->service.valid_no_process, "Service" },
Lennart Poetteringa9a1e002010-04-06 02:40:10 +02001281 { "SysVStartPriority", config_parse_sysv_priority, &u->service.sysv_start_priority, "Service" },
Lennart Poettering50159e62010-04-08 00:52:14 +02001282 { "KillMode", config_parse_kill_mode, &u->service.kill_mode, "Service" },
Lennart Poetteringb778d552010-04-10 17:48:41 +02001283 { "NonBlocking", config_parse_bool, &u->service.exec_context.non_blocking, "Service" },
Lennart Poettering05e343b2010-04-15 23:16:16 +02001284 { "BusName", config_parse_string, &u->service.bus_name, "Service" },
Lennart Poettering87f0e412010-01-26 21:39:06 +01001285 EXEC_CONTEXT_CONFIG_ITEMS(u->service.exec_context, "Service"),
Lennart Poettering034c6ed2010-01-26 04:18:44 +01001286
Lennart Poettering1c01f822010-01-27 02:16:11 +01001287 { "ListenStream", config_parse_listen, &u->socket, "Socket" },
1288 { "ListenDatagram", config_parse_listen, &u->socket, "Socket" },
1289 { "ListenSequentialPacket", config_parse_listen, &u->socket, "Socket" },
1290 { "ListenFIFO", config_parse_listen, &u->socket, "Socket" },
1291 { "BindIPv6Only", config_parse_socket_bind, &u->socket, "Socket" },
1292 { "Backlog", config_parse_unsigned, &u->socket.backlog, "Socket" },
Lennart Poetteringacbb0222010-01-27 04:31:52 +01001293 { "BindToDevice", config_parse_bindtodevice, &u->socket, "Socket" },
Lennart Poettering1c01f822010-01-27 02:16:11 +01001294 { "ExecStartPre", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_START_PRE, "Socket" },
1295 { "ExecStartPost", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_START_POST, "Socket" },
1296 { "ExecStopPre", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_STOP_PRE, "Socket" },
1297 { "ExecStopPost", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_STOP_POST, "Socket" },
Lennart Poettering108736d2010-04-10 17:50:54 +02001298 { "TimeoutSec", config_parse_usec, &u->socket.timeout_usec, "Socket" },
Lennart Poetteringb5a06992010-02-12 02:02:14 +01001299 { "DirectoryMode", config_parse_mode, &u->socket.directory_mode, "Socket" },
1300 { "SocketMode", config_parse_mode, &u->socket.socket_mode, "Socket" },
Lennart Poettering50159e62010-04-08 00:52:14 +02001301 { "KillMode", config_parse_kill_mode, &u->socket.kill_mode, "Socket" },
Lennart Poettering4f2d5282010-04-15 06:19:54 +02001302 { "Accept", config_parse_bool, &u->socket.accept, "Socket" },
Lennart Poettering87f0e412010-01-26 21:39:06 +01001303 EXEC_CONTEXT_CONFIG_ITEMS(u->socket.exec_context, "Socket"),
Lennart Poettering034c6ed2010-01-26 04:18:44 +01001304
Lennart Poetteringe5373522010-04-10 17:53:17 +02001305 { "What", config_parse_string, &u->mount.parameters_fragment.what, "Mount" },
1306 { "Where", config_parse_path, &u->mount.where, "Mount" },
1307 { "Options", config_parse_string, &u->mount.parameters_fragment.options, "Mount" },
1308 { "Type", config_parse_string, &u->mount.parameters_fragment.fstype, "Mount" },
1309 { "TimeoutSec", config_parse_usec, &u->mount.timeout_usec, "Mount" },
1310 { "KillMode", config_parse_kill_mode, &u->mount.kill_mode, "Mount" },
1311 EXEC_CONTEXT_CONFIG_ITEMS(u->mount.exec_context, "Mount"),
Lennart Poettering034c6ed2010-01-26 04:18:44 +01001312
Lennart Poettering8d567582010-04-16 23:24:39 +02001313 { "Where", config_parse_path, &u->automount.where, "Automount" },
1314
Lennart Poettering4e85aff2010-05-14 02:29:45 +02001315 { "What", config_parse_path, &u->swap.parameters_fragment.what, "Swap" },
1316 { "Priority", config_parse_int, &u->swap.parameters_fragment.priority, "Swap" },
Maarten Lankhorst07b0b132010-05-09 18:44:11 +02001317
Lennart Poettering3efd4192009-11-19 23:13:20 +01001318 { NULL, NULL, NULL, NULL }
1319 };
1320
Lennart Poettering034c6ed2010-01-26 04:18:44 +01001321#undef EXEC_CONTEXT_CONFIG_ITEMS
Lennart Poettering42f4e3c2010-01-19 02:56:37 +01001322
Lennart Poettering42f4e3c2010-01-19 02:56:37 +01001323 const char *sections[3];
Lennart Poettering0301abf2010-01-27 00:15:56 +01001324 char *k;
1325 int r;
Lennart Poettering87f0e412010-01-26 21:39:06 +01001326 Set *symlink_names;
Lennart Poettering23a177e2010-04-06 02:43:58 +02001327 FILE *f = NULL;
1328 char *filename = NULL, *id = NULL;
1329 Unit *merged;
1330
Lennart Poetteringe5373522010-04-10 17:53:17 +02001331 if (!u) {
1332 /* Dirty dirty hack. */
1333 dump_items((FILE*) path, items);
1334 return 0;
1335 }
1336
Lennart Poettering23a177e2010-04-06 02:43:58 +02001337 assert(u);
Lennart Poetteringe5373522010-04-10 17:53:17 +02001338 assert(path);
Lennart Poettering3efd4192009-11-19 23:13:20 +01001339
Lennart Poettering09477262010-04-15 23:20:17 +02001340 sections[0] = "Unit";
Lennart Poettering87f0e412010-01-26 21:39:06 +01001341 sections[1] = section_table[u->meta.type];
Lennart Poettering42f4e3c2010-01-19 02:56:37 +01001342 sections[2] = NULL;
1343
Lennart Poettering87f0e412010-01-26 21:39:06 +01001344 if (!(symlink_names = set_new(string_hash_func, string_compare_func)))
1345 return -ENOMEM;
Lennart Poettering3efd4192009-11-19 23:13:20 +01001346
Lennart Poettering036643a2010-02-13 01:07:02 +01001347 if (path_is_absolute(path)) {
1348
1349 if (!(filename = strdup(path))) {
1350 r = -ENOMEM;
1351 goto finish;
1352 }
1353
1354 if ((r = open_follow(&filename, &f, symlink_names, &id)) < 0) {
1355 free(filename);
1356 filename = NULL;
1357
1358 if (r != -ENOENT)
1359 goto finish;
1360 }
1361
1362 } else {
1363 char **p;
1364
1365 STRV_FOREACH(p, u->meta.manager->unit_path) {
1366
1367 /* Instead of opening the path right away, we manually
1368 * follow all symlinks and add their name to our unit
1369 * name set while doing so */
1370 if (!(filename = path_make_absolute(path, *p))) {
1371 r = -ENOMEM;
1372 goto finish;
1373 }
1374
1375 if ((r = open_follow(&filename, &f, symlink_names, &id)) < 0) {
1376 char *sn;
1377
1378 free(filename);
1379 filename = NULL;
1380
1381 if (r != -ENOENT)
1382 goto finish;
1383
1384 /* Empty the symlink names for the next run */
1385 while ((sn = set_steal_first(symlink_names)))
1386 free(sn);
1387
1388 continue;
1389 }
1390
1391 break;
1392 }
Lennart Poettering034c6ed2010-01-26 04:18:44 +01001393 }
1394
Lennart Poettering036643a2010-02-13 01:07:02 +01001395 if (!filename) {
Lennart Poettering23a177e2010-04-06 02:43:58 +02001396 r = 0;
1397 goto finish;
1398 }
1399
1400 merged = u;
1401 if ((r = merge_by_names(&merged, symlink_names, id)) < 0)
1402 goto finish;
1403
1404 if (merged != u) {
Lennart Poetteringe5373522010-04-10 17:53:17 +02001405 u->meta.load_state = UNIT_MERGED;
Lennart Poettering23a177e2010-04-06 02:43:58 +02001406 r = 0;
Lennart Poettering0301abf2010-01-27 00:15:56 +01001407 goto finish;
1408 }
1409
1410 /* Now, parse the file contents */
Lennart Poettering23a177e2010-04-06 02:43:58 +02001411 if ((r = config_parse(filename, f, sections, items, u)) < 0)
Lennart Poettering0301abf2010-01-27 00:15:56 +01001412 goto finish;
1413
Lennart Poettering6be1e7d2010-02-14 01:01:10 +01001414 free(u->meta.fragment_path);
1415 u->meta.fragment_path = filename;
Lennart Poettering0301abf2010-01-27 00:15:56 +01001416 filename = NULL;
1417
Lennart Poetteringe5373522010-04-10 17:53:17 +02001418 u->meta.load_state = UNIT_LOADED;
Lennart Poettering23a177e2010-04-06 02:43:58 +02001419 r = 0;
Lennart Poettering87f0e412010-01-26 21:39:06 +01001420
1421finish:
1422 while ((k = set_steal_first(symlink_names)))
1423 free(k);
Lennart Poettering23a177e2010-04-06 02:43:58 +02001424
Lennart Poettering87f0e412010-01-26 21:39:06 +01001425 set_free(symlink_names);
Lennart Poettering0301abf2010-01-27 00:15:56 +01001426 free(filename);
1427
Lennart Poettering23a177e2010-04-06 02:43:58 +02001428 if (f)
1429 fclose(f);
1430
Lennart Poettering0301abf2010-01-27 00:15:56 +01001431 return r;
1432}
1433
Lennart Poetteringe5373522010-04-10 17:53:17 +02001434int unit_load_fragment(Unit *u) {
Lennart Poettering23a177e2010-04-06 02:43:58 +02001435 int r;
Lennart Poettering0301abf2010-01-27 00:15:56 +01001436
1437 assert(u);
Lennart Poettering0301abf2010-01-27 00:15:56 +01001438
Lennart Poettering23a177e2010-04-06 02:43:58 +02001439 if (u->meta.fragment_path) {
1440
Lennart Poetteringe5373522010-04-10 17:53:17 +02001441 if ((r = load_from_path(u, u->meta.fragment_path)) < 0)
Lennart Poettering23a177e2010-04-06 02:43:58 +02001442 return r;
1443
1444 } else {
Lennart Poettering0301abf2010-01-27 00:15:56 +01001445 Iterator i;
Lennart Poettering890f4342010-02-14 01:08:20 +01001446 const char *t;
Lennart Poettering0301abf2010-01-27 00:15:56 +01001447
Lennart Poettering890f4342010-02-14 01:08:20 +01001448 /* Try to find the unit under its id */
Lennart Poettering9e2f7c12010-04-15 03:11:11 +02001449 if ((r = load_from_path(u, u->meta.id)) < 0)
1450 return r;
Lennart Poettering890f4342010-02-14 01:08:20 +01001451
1452 /* Try to find an alias we can load this with */
Lennart Poetteringe5373522010-04-10 17:53:17 +02001453 if (u->meta.load_state == UNIT_STUB)
Lennart Poettering23a177e2010-04-06 02:43:58 +02001454 SET_FOREACH(t, u->meta.names, i) {
1455
Lennart Poettering9e2f7c12010-04-15 03:11:11 +02001456 if (t == u->meta.id)
Lennart Poettering23a177e2010-04-06 02:43:58 +02001457 continue;
1458
Lennart Poetteringe5373522010-04-10 17:53:17 +02001459 if ((r = load_from_path(u, t)) < 0)
Lennart Poettering23a177e2010-04-06 02:43:58 +02001460 return r;
1461
Lennart Poetteringe5373522010-04-10 17:53:17 +02001462 if (u->meta.load_state != UNIT_STUB)
Lennart Poettering890f4342010-02-14 01:08:20 +01001463 break;
Lennart Poettering23a177e2010-04-06 02:43:58 +02001464 }
Lennart Poettering9e2f7c12010-04-15 03:11:11 +02001465
1466 /* Now, follow the same logic, but look for a template */
1467 if (u->meta.load_state == UNIT_STUB && u->meta.instance) {
1468 char *k;
1469
1470 if (!(k = unit_name_template(u->meta.id)))
1471 return -ENOMEM;
1472
1473 r = load_from_path(u, k);
1474 free(k);
1475
1476 if (r < 0)
1477 return r;
1478
1479 if (u->meta.load_state == UNIT_STUB)
1480 SET_FOREACH(t, u->meta.names, i) {
1481
1482 if (t == u->meta.id)
1483 continue;
1484
1485 if (!(k = unit_name_template(t)))
1486 return -ENOMEM;
1487
1488 r = load_from_path(u, k);
1489 free(k);
1490
1491 if (r < 0)
1492 return r;
1493
1494 if (u->meta.load_state != UNIT_STUB)
1495 break;
1496 }
1497 }
Lennart Poettering0301abf2010-01-27 00:15:56 +01001498 }
Lennart Poettering87f0e412010-01-26 21:39:06 +01001499
Lennart Poettering23a177e2010-04-06 02:43:58 +02001500 return 0;
Lennart Poettering3efd4192009-11-19 23:13:20 +01001501}
Lennart Poetteringe5373522010-04-10 17:53:17 +02001502
1503void unit_dump_config_items(FILE *f) {
1504 /* OK, this wins a prize for extreme ugliness. */
1505
1506 load_from_path(NULL, (const void*) f);
1507}