blob: a2974cbeaded5bb595c610c16fdbd259276f1823 [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 Poettering45fb0692010-07-17 00:57:51 +020032#include <sys/stat.h>
Lennart Poettering3efd4192009-11-19 23:13:20 +010033
Lennart Poettering87f0e412010-01-26 21:39:06 +010034#include "unit.h"
Lennart Poettering3efd4192009-11-19 23:13:20 +010035#include "strv.h"
36#include "conf-parser.h"
37#include "load-fragment.h"
Lennart Poettering16354ef2010-01-20 19:19:53 +010038#include "log.h"
Lennart Poettering9eba9da2010-01-29 20:46:22 +010039#include "ioprio.h"
Lennart Poettering94f04342010-01-30 01:55:42 +010040#include "securebits.h"
41#include "missing.h"
Lennart Poettering9e2f7c12010-04-15 03:11:11 +020042#include "unit-name.h"
Lennart Poettering398ef8b2010-07-08 02:43:18 +020043#include "bus-errors.h"
Lennart Poettering3efd4192009-11-19 23:13:20 +010044
Lennart Poetteringddb26e12010-06-18 06:06:24 +020045#define COMMENTS "#;\n"
Lennart Poetteringddb26e12010-06-18 06:06:24 +020046
Lennart Poettering42f4e3c2010-01-19 02:56:37 +010047static int config_parse_deps(
Lennart Poettering3efd4192009-11-19 23:13:20 +010048 const char *filename,
49 unsigned line,
50 const char *section,
51 const char *lvalue,
52 const char *rvalue,
53 void *data,
54 void *userdata) {
55
Lennart Poettering87f0e412010-01-26 21:39:06 +010056 UnitDependency d = PTR_TO_UINT(data);
57 Unit *u = userdata;
Lennart Poettering3efd4192009-11-19 23:13:20 +010058 char *w;
59 size_t l;
60 char *state;
61
62 assert(filename);
63 assert(lvalue);
64 assert(rvalue);
Lennart Poettering3efd4192009-11-19 23:13:20 +010065
Lennart Poetteringf60f22d2010-07-07 20:58:41 +020066 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
Lennart Poettering9e2f7c12010-04-15 03:11:11 +020067 char *t, *k;
Lennart Poettering3efd4192009-11-19 23:13:20 +010068 int r;
Lennart Poettering3efd4192009-11-19 23:13:20 +010069
70 if (!(t = strndup(w, l)))
71 return -ENOMEM;
72
Lennart Poettering9e2f7c12010-04-15 03:11:11 +020073 k = unit_name_printf(u, t);
Lennart Poettering3efd4192009-11-19 23:13:20 +010074 free(t);
75
Lennart Poettering9e2f7c12010-04-15 03:11:11 +020076 if (!k)
77 return -ENOMEM;
78
Lennart Poettering701cc382010-04-21 06:01:13 +020079 r = unit_add_dependency_by_name(u, d, k, NULL, true);
Lennart Poettering9e2f7c12010-04-15 03:11:11 +020080 free(k);
81
Lennart Poettering3efd4192009-11-19 23:13:20 +010082 if (r < 0)
83 return r;
Lennart Poettering3efd4192009-11-19 23:13:20 +010084 }
85
86 return 0;
87}
88
Lennart Poettering42f4e3c2010-01-19 02:56:37 +010089static int config_parse_names(
Lennart Poettering87d15152010-01-18 23:50:13 +010090 const char *filename,
91 unsigned line,
92 const char *section,
93 const char *lvalue,
94 const char *rvalue,
95 void *data,
96 void *userdata) {
97
Lennart Poettering87f0e412010-01-26 21:39:06 +010098 Unit *u = userdata;
Lennart Poettering87d15152010-01-18 23:50:13 +010099 char *w;
100 size_t l;
101 char *state;
102
103 assert(filename);
104 assert(lvalue);
105 assert(rvalue);
106 assert(data);
107
Lennart Poetteringf60f22d2010-07-07 20:58:41 +0200108 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
Lennart Poettering9e2f7c12010-04-15 03:11:11 +0200109 char *t, *k;
Lennart Poettering87d15152010-01-18 23:50:13 +0100110 int r;
Lennart Poettering87d15152010-01-18 23:50:13 +0100111
112 if (!(t = strndup(w, l)))
113 return -ENOMEM;
114
Lennart Poettering9e2f7c12010-04-15 03:11:11 +0200115 k = unit_name_printf(u, t);
Lennart Poettering87d15152010-01-18 23:50:13 +0100116 free(t);
Lennart Poettering23a177e2010-04-06 02:43:58 +0200117
Lennart Poettering9e2f7c12010-04-15 03:11:11 +0200118 if (!k)
119 return -ENOMEM;
120
121 r = unit_merge_by_name(u, k);
122 free(k);
123
Lennart Poettering23a177e2010-04-06 02:43:58 +0200124 if (r < 0)
125 return r;
Lennart Poettering87d15152010-01-18 23:50:13 +0100126 }
127
128 return 0;
129}
130
Lennart Poetteringf2d37692010-06-18 23:25:19 +0200131static int config_parse_string_printf(
Lennart Poettering932921b2010-04-24 03:11:01 +0200132 const char *filename,
133 unsigned line,
134 const char *section,
135 const char *lvalue,
136 const char *rvalue,
137 void *data,
138 void *userdata) {
139
140 Unit *u = userdata;
Lennart Poetteringf2d37692010-06-18 23:25:19 +0200141 char **s = data;
Lennart Poettering932921b2010-04-24 03:11:01 +0200142 char *k;
143
144 assert(filename);
145 assert(lvalue);
146 assert(rvalue);
Lennart Poetteringf2d37692010-06-18 23:25:19 +0200147 assert(s);
148 assert(u);
Lennart Poettering932921b2010-04-24 03:11:01 +0200149
150 if (!(k = unit_full_printf(u, rvalue)))
151 return -ENOMEM;
152
Lennart Poetteringf2d37692010-06-18 23:25:19 +0200153 free(*s);
Lennart Poettering932921b2010-04-24 03:11:01 +0200154 if (*k)
Lennart Poetteringf2d37692010-06-18 23:25:19 +0200155 *s = k;
Lennart Poettering932921b2010-04-24 03:11:01 +0200156 else {
157 free(k);
Lennart Poetteringf2d37692010-06-18 23:25:19 +0200158 *s = NULL;
Lennart Poettering932921b2010-04-24 03:11:01 +0200159 }
160
161 return 0;
162}
163
Lennart Poettering42f4e3c2010-01-19 02:56:37 +0100164static int config_parse_listen(
165 const char *filename,
166 unsigned line,
167 const char *section,
168 const char *lvalue,
169 const char *rvalue,
170 void *data,
171 void *userdata) {
172
Lennart Poettering16354ef2010-01-20 19:19:53 +0100173 int r;
Lennart Poettering542563b2010-01-23 03:35:54 +0100174 SocketPort *p;
175 Socket *s;
Lennart Poettering16354ef2010-01-20 19:19:53 +0100176
Lennart Poettering42f4e3c2010-01-19 02:56:37 +0100177 assert(filename);
178 assert(lvalue);
179 assert(rvalue);
180 assert(data);
181
Lennart Poettering542563b2010-01-23 03:35:54 +0100182 s = (Socket*) data;
183
184 if (!(p = new0(SocketPort, 1)))
185 return -ENOMEM;
186
187 if (streq(lvalue, "ListenFIFO")) {
188 p->type = SOCKET_FIFO;
189
190 if (!(p->path = strdup(rvalue))) {
191 free(p);
192 return -ENOMEM;
193 }
Lennart Poettering01f78472010-05-24 05:25:33 +0200194
195 path_kill_slashes(p->path);
Lennart Poettering542563b2010-01-23 03:35:54 +0100196 } else {
197 p->type = SOCKET_SOCKET;
198
199 if ((r = socket_address_parse(&p->address, rvalue)) < 0) {
200 log_error("[%s:%u] Failed to parse address value: %s", filename, line, rvalue);
201 free(p);
202 return r;
203 }
204
205 if (streq(lvalue, "ListenStream"))
206 p->address.type = SOCK_STREAM;
207 else if (streq(lvalue, "ListenDatagram"))
208 p->address.type = SOCK_DGRAM;
209 else {
210 assert(streq(lvalue, "ListenSequentialPacket"));
211 p->address.type = SOCK_SEQPACKET;
212 }
213
214 if (socket_address_family(&p->address) != AF_LOCAL && p->address.type == SOCK_SEQPACKET) {
215 free(p);
216 return -EPROTONOSUPPORT;
217 }
Lennart Poettering16354ef2010-01-20 19:19:53 +0100218 }
219
Lennart Poettering542563b2010-01-23 03:35:54 +0100220 p->fd = -1;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100221 LIST_PREPEND(SocketPort, port, s->ports, p);
Lennart Poettering542563b2010-01-23 03:35:54 +0100222
Lennart Poettering16354ef2010-01-20 19:19:53 +0100223 return 0;
Lennart Poettering42f4e3c2010-01-19 02:56:37 +0100224}
225
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100226static int config_parse_socket_bind(
Lennart Poettering42f4e3c2010-01-19 02:56:37 +0100227 const char *filename,
228 unsigned line,
229 const char *section,
230 const char *lvalue,
231 const char *rvalue,
232 void *data,
233 void *userdata) {
234
Lennart Poettering542563b2010-01-23 03:35:54 +0100235 Socket *s;
Lennart Poetteringc0120d92010-05-21 23:41:25 +0200236 SocketAddressBindIPv6Only b;
Lennart Poettering42f4e3c2010-01-19 02:56:37 +0100237
238 assert(filename);
239 assert(lvalue);
240 assert(rvalue);
241 assert(data);
242
Lennart Poettering542563b2010-01-23 03:35:54 +0100243 s = (Socket*) data;
244
Lennart Poetteringc0120d92010-05-21 23:41:25 +0200245 if ((b = socket_address_bind_ipv6_only_from_string(rvalue)) < 0) {
246 int r;
Lennart Poettering42f4e3c2010-01-19 02:56:37 +0100247
Lennart Poetteringc0120d92010-05-21 23:41:25 +0200248 if ((r = parse_boolean(rvalue)) < 0) {
249 log_error("[%s:%u] Failed to parse bind IPv6 only value: %s", filename, line, rvalue);
250 return -EBADMSG;
251 }
252
253 s->bind_ipv6_only = r ? SOCKET_ADDRESS_IPV6_ONLY : SOCKET_ADDRESS_BOTH;
254 } else
255 s->bind_ipv6_only = b;
Lennart Poettering542563b2010-01-23 03:35:54 +0100256
Lennart Poettering42f4e3c2010-01-19 02:56:37 +0100257 return 0;
258}
259
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100260static int config_parse_nice(
261 const char *filename,
262 unsigned line,
263 const char *section,
264 const char *lvalue,
265 const char *rvalue,
266 void *data,
267 void *userdata) {
268
Lennart Poetteringfb33a392010-01-28 02:53:56 +0100269 ExecContext *c = data;
270 int priority, r;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100271
272 assert(filename);
273 assert(lvalue);
274 assert(rvalue);
275 assert(data);
276
277 if ((r = safe_atoi(rvalue, &priority)) < 0) {
278 log_error("[%s:%u] Failed to parse nice priority: %s", filename, line, rvalue);
279 return r;
280 }
281
282 if (priority < PRIO_MIN || priority >= PRIO_MAX) {
283 log_error("[%s:%u] Nice priority out of range: %s", filename, line, rvalue);
284 return -ERANGE;
285 }
286
Lennart Poetteringfb33a392010-01-28 02:53:56 +0100287 c->nice = priority;
288 c->nice_set = false;
289
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100290 return 0;
291}
292
293static int config_parse_oom_adjust(
294 const char *filename,
295 unsigned line,
296 const char *section,
297 const char *lvalue,
298 const char *rvalue,
299 void *data,
300 void *userdata) {
301
Lennart Poetteringfb33a392010-01-28 02:53:56 +0100302 ExecContext *c = data;
303 int oa, r;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100304
305 assert(filename);
306 assert(lvalue);
307 assert(rvalue);
308 assert(data);
309
310 if ((r = safe_atoi(rvalue, &oa)) < 0) {
311 log_error("[%s:%u] Failed to parse OOM adjust value: %s", filename, line, rvalue);
312 return r;
313 }
314
315 if (oa < OOM_DISABLE || oa > OOM_ADJUST_MAX) {
316 log_error("[%s:%u] OOM adjust value out of range: %s", filename, line, rvalue);
317 return -ERANGE;
318 }
319
Lennart Poetteringfb33a392010-01-28 02:53:56 +0100320 c->oom_adjust = oa;
321 c->oom_adjust_set = true;
322
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100323 return 0;
324}
325
Lennart Poetteringb5a06992010-02-12 02:02:14 +0100326static int config_parse_mode(
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100327 const char *filename,
328 unsigned line,
329 const char *section,
330 const char *lvalue,
331 const char *rvalue,
332 void *data,
333 void *userdata) {
334
335 mode_t *m = data;
336 long l;
337 char *x = NULL;
338
339 assert(filename);
340 assert(lvalue);
341 assert(rvalue);
342 assert(data);
343
344 errno = 0;
345 l = strtol(rvalue, &x, 8);
346 if (!x || *x || errno) {
Lennart Poetteringb5a06992010-02-12 02:02:14 +0100347 log_error("[%s:%u] Failed to parse mode value: %s", filename, line, rvalue);
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100348 return errno ? -errno : -EINVAL;
349 }
350
Lennart Poetteringb5a06992010-02-12 02:02:14 +0100351 if (l < 0000 || l > 07777) {
352 log_error("[%s:%u] mode value out of range: %s", filename, line, rvalue);
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100353 return -ERANGE;
354 }
355
356 *m = (mode_t) l;
357 return 0;
358}
359
360static int config_parse_exec(
361 const char *filename,
362 unsigned line,
363 const char *section,
364 const char *lvalue,
365 const char *rvalue,
366 void *data,
367 void *userdata) {
368
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200369 ExecCommand **e = data, *nce;
370 char *path, **n;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100371 unsigned k;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100372
373 assert(filename);
374 assert(lvalue);
375 assert(rvalue);
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200376 assert(e);
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100377
Lennart Poettering6c666e22010-05-19 21:51:53 +0200378 /* We accept an absolute path as first argument, or
379 * alternatively an absolute prefixed with @ to allow
380 * overriding of argv[0]. */
381
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200382 for (;;) {
383 char *w;
384 size_t l;
385 char *state;
Lennart Poetteringb708e7c2010-07-12 02:25:42 +0200386 bool honour_argv0 = false, ignore = false;
Lennart Poettering6c666e22010-05-19 21:51:53 +0200387
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200388 path = NULL;
389 nce = NULL;
390 n = NULL;
Lennart Poettering6c666e22010-05-19 21:51:53 +0200391
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200392 rvalue += strspn(rvalue, WHITESPACE);
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100393
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200394 if (rvalue[0] == 0)
395 break;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100396
Lennart Poetteringb708e7c2010-07-12 02:25:42 +0200397 if (rvalue[0] == '-') {
398 ignore = true;
399 rvalue ++;
400 }
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200401
Lennart Poetteringb708e7c2010-07-12 02:25:42 +0200402 if (rvalue[0] == '@') {
403 honour_argv0 = true;
404 rvalue ++;
405 }
406
407 if (*rvalue != '/') {
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200408 log_error("[%s:%u] Invalid executable path in command line: %s", filename, line, rvalue);
409 return -EINVAL;
Lennart Poettering6c666e22010-05-19 21:51:53 +0200410 }
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100411
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200412 k = 0;
413 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
414 if (strncmp(w, ";", l) == 0)
415 break;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100416
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200417 k++;
418 }
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100419
Lennart Poetteringb708e7c2010-07-12 02:25:42 +0200420 if (!(n = new(char*, k + !honour_argv0)))
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200421 return -ENOMEM;
422
423 k = 0;
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200424 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
425 if (strncmp(w, ";", l) == 0)
426 break;
427
Lennart Poetteringb708e7c2010-07-12 02:25:42 +0200428 if (honour_argv0 && w == rvalue) {
429 assert(!path);
430 if (!(path = cunescape_length(w, l)))
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200431 goto fail;
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200432 } else {
433 if (!(n[k++] = cunescape_length(w, l)))
434 goto fail;
435 }
436 }
437
438 n[k] = NULL;
439
440 if (!n[0]) {
441 log_error("[%s:%u] Invalid command line: %s", filename, line, rvalue);
442 strv_free(n);
443 return -EINVAL;
444 }
445
446 if (!path)
447 if (!(path = strdup(n[0])))
448 goto fail;
449
450 assert(path_is_absolute(path));
451
452 if (!(nce = new0(ExecCommand, 1)))
Lennart Poettering6c666e22010-05-19 21:51:53 +0200453 goto fail;
454
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200455 nce->argv = n;
456 nce->path = path;
Lennart Poetteringb708e7c2010-07-12 02:25:42 +0200457 nce->ignore = ignore;
Lennart Poettering6c666e22010-05-19 21:51:53 +0200458
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200459 path_kill_slashes(nce->path);
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100460
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200461 exec_command_append_list(e, nce);
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100462
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200463 rvalue = state;
464 }
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100465
466 return 0;
467
468fail:
Lennart Poettering6c666e22010-05-19 21:51:53 +0200469 n[k] = NULL;
470 strv_free(n);
471 free(path);
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100472 free(nce);
473
474 return -ENOMEM;
475}
476
477static int config_parse_usec(
478 const char *filename,
479 unsigned line,
480 const char *section,
481 const char *lvalue,
482 const char *rvalue,
483 void *data,
484 void *userdata) {
485
486 usec_t *usec = data;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100487 int r;
488
489 assert(filename);
490 assert(lvalue);
491 assert(rvalue);
492 assert(data);
493
Lennart Poettering24a6e4a2010-04-23 20:26:59 +0200494 if ((r = parse_usec(rvalue, usec)) < 0) {
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100495 log_error("[%s:%u] Failed to parse time value: %s", filename, line, rvalue);
496 return r;
497 }
498
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100499 return 0;
500}
501
Lennart Poettering487393e2010-07-07 01:10:27 +0200502static DEFINE_CONFIG_PARSE_ENUM(config_parse_service_type, service_type, ServiceType, "Failed to parse service type");
503static DEFINE_CONFIG_PARSE_ENUM(config_parse_service_restart, service_restart, ServiceRestart, "Failed to parse service restart specifier");
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100504
Lennart Poettering47be8702010-02-03 14:21:48 +0100505static int config_parse_bindtodevice(
Lennart Poetteringacbb0222010-01-27 04:31:52 +0100506 const char *filename,
507 unsigned line,
508 const char *section,
509 const char *lvalue,
510 const char *rvalue,
511 void *data,
512 void *userdata) {
513
514 Socket *s = data;
515 char *n;
516
517 assert(filename);
518 assert(lvalue);
519 assert(rvalue);
520 assert(data);
521
522 if (rvalue[0] && !streq(rvalue, "*")) {
523 if (!(n = strdup(rvalue)))
524 return -ENOMEM;
525 } else
526 n = NULL;
527
528 free(s->bind_to_device);
529 s->bind_to_device = n;
530
531 return 0;
532}
533
Lennart Poettering487393e2010-07-07 01:10:27 +0200534static DEFINE_CONFIG_PARSE_ENUM(config_parse_output, exec_output, ExecOutput, "Failed to parse output specifier");
535static DEFINE_CONFIG_PARSE_ENUM(config_parse_input, exec_input, ExecInput, "Failed to parse input specifier");
Lennart Poettering071830f2010-01-28 02:06:20 +0100536
Lennart Poettering47be8702010-02-03 14:21:48 +0100537static int config_parse_facility(
Lennart Poettering071830f2010-01-28 02:06:20 +0100538 const char *filename,
539 unsigned line,
540 const char *section,
541 const char *lvalue,
542 const char *rvalue,
543 void *data,
544 void *userdata) {
545
Lennart Poettering071830f2010-01-28 02:06:20 +0100546
Lennart Poettering94f04342010-01-30 01:55:42 +0100547 int *o = data, x;
Lennart Poettering071830f2010-01-28 02:06:20 +0100548
549 assert(filename);
550 assert(lvalue);
551 assert(rvalue);
552 assert(data);
553
Lennart Poettering0d87eb42010-04-13 02:22:41 +0200554 if ((x = log_facility_from_string(rvalue)) < 0) {
555 log_error("[%s:%u] Failed to parse log facility: %s", filename, line, rvalue);
556 return -EBADMSG;
557 }
Lennart Poettering94f04342010-01-30 01:55:42 +0100558
559 *o = LOG_MAKEPRI(x, LOG_PRI(*o));
Lennart Poettering071830f2010-01-28 02:06:20 +0100560
561 return 0;
562}
563
Lennart Poettering47be8702010-02-03 14:21:48 +0100564static int config_parse_level(
Lennart Poettering071830f2010-01-28 02:06:20 +0100565 const char *filename,
566 unsigned line,
567 const char *section,
568 const char *lvalue,
569 const char *rvalue,
570 void *data,
571 void *userdata) {
572
Lennart Poettering071830f2010-01-28 02:06:20 +0100573
Lennart Poettering94f04342010-01-30 01:55:42 +0100574 int *o = data, x;
Lennart Poettering071830f2010-01-28 02:06:20 +0100575
576 assert(filename);
577 assert(lvalue);
578 assert(rvalue);
579 assert(data);
580
Lennart Poettering0d87eb42010-04-13 02:22:41 +0200581 if ((x = log_level_from_string(rvalue)) < 0) {
582 log_error("[%s:%u] Failed to parse log level: %s", filename, line, rvalue);
583 return -EBADMSG;
584 }
Lennart Poettering071830f2010-01-28 02:06:20 +0100585
Lennart Poettering94f04342010-01-30 01:55:42 +0100586 *o = LOG_MAKEPRI(LOG_FAC(*o), x);
Lennart Poettering071830f2010-01-28 02:06:20 +0100587 return 0;
588}
589
Lennart Poettering47be8702010-02-03 14:21:48 +0100590static int config_parse_io_class(
Lennart Poettering9eba9da2010-01-29 20:46:22 +0100591 const char *filename,
592 unsigned line,
593 const char *section,
594 const char *lvalue,
595 const char *rvalue,
596 void *data,
597 void *userdata) {
598
Lennart Poettering9eba9da2010-01-29 20:46:22 +0100599 ExecContext *c = data;
Lennart Poettering94f04342010-01-30 01:55:42 +0100600 int x;
Lennart Poettering9eba9da2010-01-29 20:46:22 +0100601
602 assert(filename);
603 assert(lvalue);
604 assert(rvalue);
605 assert(data);
606
Lennart Poettering0d87eb42010-04-13 02:22:41 +0200607 if ((x = ioprio_class_from_string(rvalue)) < 0) {
608 log_error("[%s:%u] Failed to parse IO scheduling class: %s", filename, line, rvalue);
609 return -EBADMSG;
610 }
Lennart Poettering9eba9da2010-01-29 20:46:22 +0100611
Lennart Poettering94f04342010-01-30 01:55:42 +0100612 c->ioprio = IOPRIO_PRIO_VALUE(x, IOPRIO_PRIO_DATA(c->ioprio));
Lennart Poettering9eba9da2010-01-29 20:46:22 +0100613 c->ioprio_set = true;
614
615 return 0;
616}
617
Lennart Poettering47be8702010-02-03 14:21:48 +0100618static int config_parse_io_priority(
Lennart Poettering9eba9da2010-01-29 20:46:22 +0100619 const char *filename,
620 unsigned line,
621 const char *section,
622 const char *lvalue,
623 const char *rvalue,
624 void *data,
625 void *userdata) {
626
627 ExecContext *c = data;
628 int i;
629
630 assert(filename);
631 assert(lvalue);
632 assert(rvalue);
633 assert(data);
634
Lennart Poettering94f04342010-01-30 01:55:42 +0100635 if (safe_atoi(rvalue, &i) < 0 || i < 0 || i >= IOPRIO_BE_NR) {
Lennart Poettering9eba9da2010-01-29 20:46:22 +0100636 log_error("[%s:%u] Failed to parse io priority: %s", filename, line, rvalue);
637 return -EBADMSG;
638 }
639
Lennart Poettering94f04342010-01-30 01:55:42 +0100640 c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_PRIO_CLASS(c->ioprio), i);
Lennart Poettering9eba9da2010-01-29 20:46:22 +0100641 c->ioprio_set = true;
642
643 return 0;
644}
645
Lennart Poettering47be8702010-02-03 14:21:48 +0100646static int config_parse_cpu_sched_policy(
Lennart Poettering94f04342010-01-30 01:55:42 +0100647 const char *filename,
648 unsigned line,
649 const char *section,
650 const char *lvalue,
651 const char *rvalue,
652 void *data,
653 void *userdata) {
654
655
656 ExecContext *c = data;
657 int x;
658
659 assert(filename);
660 assert(lvalue);
661 assert(rvalue);
662 assert(data);
663
Lennart Poettering0d87eb42010-04-13 02:22:41 +0200664 if ((x = sched_policy_from_string(rvalue)) < 0) {
665 log_error("[%s:%u] Failed to parse CPU scheduling policy: %s", filename, line, rvalue);
666 return -EBADMSG;
667 }
Lennart Poettering94f04342010-01-30 01:55:42 +0100668
669 c->cpu_sched_policy = x;
670 c->cpu_sched_set = true;
671
672 return 0;
673}
674
Lennart Poettering47be8702010-02-03 14:21:48 +0100675static int config_parse_cpu_sched_prio(
Lennart Poettering94f04342010-01-30 01:55:42 +0100676 const char *filename,
677 unsigned line,
678 const char *section,
679 const char *lvalue,
680 const char *rvalue,
681 void *data,
682 void *userdata) {
683
684 ExecContext *c = data;
685 int i;
686
687 assert(filename);
688 assert(lvalue);
689 assert(rvalue);
690 assert(data);
691
692 /* On Linux RR/FIFO have the same range */
693 if (safe_atoi(rvalue, &i) < 0 || i < sched_get_priority_min(SCHED_RR) || i > sched_get_priority_max(SCHED_RR)) {
694 log_error("[%s:%u] Failed to parse CPU scheduling priority: %s", filename, line, rvalue);
695 return -EBADMSG;
696 }
697
698 c->cpu_sched_priority = i;
699 c->cpu_sched_set = true;
700
701 return 0;
702}
703
Lennart Poettering47be8702010-02-03 14:21:48 +0100704static int config_parse_cpu_affinity(
Lennart Poettering94f04342010-01-30 01:55:42 +0100705 const char *filename,
706 unsigned line,
707 const char *section,
708 const char *lvalue,
709 const char *rvalue,
710 void *data,
711 void *userdata) {
712
713 ExecContext *c = data;
714 char *w;
715 size_t l;
716 char *state;
717
718 assert(filename);
719 assert(lvalue);
720 assert(rvalue);
721 assert(data);
722
Lennart Poetteringf60f22d2010-07-07 20:58:41 +0200723 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
Lennart Poettering94f04342010-01-30 01:55:42 +0100724 char *t;
725 int r;
726 unsigned cpu;
727
728 if (!(t = strndup(w, l)))
729 return -ENOMEM;
730
Lennart Poettering487393e2010-07-07 01:10:27 +0200731 r = safe_atou(t, &cpu);
732 free(t);
733
Lennart Poettering82c121a2010-07-04 16:44:58 +0200734 if (!(c->cpuset))
735 if (!(c->cpuset = cpu_set_malloc(&c->cpuset_ncpus)))
736 return -ENOMEM;
737
Lennart Poettering82c121a2010-07-04 16:44:58 +0200738 if (r < 0 || cpu >= c->cpuset_ncpus) {
Lennart Poettering94f04342010-01-30 01:55:42 +0100739 log_error("[%s:%u] Failed to parse CPU affinity: %s", filename, line, rvalue);
740 return -EBADMSG;
741 }
742
Lennart Poettering82c121a2010-07-04 16:44:58 +0200743 CPU_SET_S(cpu, CPU_ALLOC_SIZE(c->cpuset_ncpus), c->cpuset);
Lennart Poettering94f04342010-01-30 01:55:42 +0100744 }
745
Lennart Poettering94f04342010-01-30 01:55:42 +0100746 return 0;
747}
748
Lennart Poettering47be8702010-02-03 14:21:48 +0100749static int config_parse_capabilities(
Lennart Poettering94f04342010-01-30 01:55:42 +0100750 const char *filename,
751 unsigned line,
752 const char *section,
753 const char *lvalue,
754 const char *rvalue,
755 void *data,
756 void *userdata) {
757
758 ExecContext *c = data;
759 cap_t cap;
760
761 assert(filename);
762 assert(lvalue);
763 assert(rvalue);
764 assert(data);
765
766 if (!(cap = cap_from_text(rvalue))) {
767 if (errno == ENOMEM)
768 return -ENOMEM;
769
770 log_error("[%s:%u] Failed to parse capabilities: %s", filename, line, rvalue);
771 return -EBADMSG;
772 }
773
774 if (c->capabilities)
775 cap_free(c->capabilities);
776 c->capabilities = cap;
777
778 return 0;
779}
780
Lennart Poettering47be8702010-02-03 14:21:48 +0100781static int config_parse_secure_bits(
Lennart Poettering94f04342010-01-30 01:55:42 +0100782 const char *filename,
783 unsigned line,
784 const char *section,
785 const char *lvalue,
786 const char *rvalue,
787 void *data,
788 void *userdata) {
789
790 ExecContext *c = data;
791 char *w;
792 size_t l;
793 char *state;
794
795 assert(filename);
796 assert(lvalue);
797 assert(rvalue);
798 assert(data);
799
Lennart Poetteringf60f22d2010-07-07 20:58:41 +0200800 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
Lennart Poettering94f04342010-01-30 01:55:42 +0100801 if (first_word(w, "keep-caps"))
802 c->secure_bits |= SECURE_KEEP_CAPS;
803 else if (first_word(w, "keep-caps-locked"))
804 c->secure_bits |= SECURE_KEEP_CAPS_LOCKED;
805 else if (first_word(w, "no-setuid-fixup"))
806 c->secure_bits |= SECURE_NO_SETUID_FIXUP;
807 else if (first_word(w, "no-setuid-fixup-locked"))
808 c->secure_bits |= SECURE_NO_SETUID_FIXUP_LOCKED;
809 else if (first_word(w, "noroot"))
810 c->secure_bits |= SECURE_NOROOT;
811 else if (first_word(w, "noroot-locked"))
812 c->secure_bits |= SECURE_NOROOT_LOCKED;
813 else {
814 log_error("[%s:%u] Failed to parse secure bits: %s", filename, line, rvalue);
815 return -EBADMSG;
816 }
817 }
818
819 return 0;
820}
821
Lennart Poettering47be8702010-02-03 14:21:48 +0100822static int config_parse_bounding_set(
Lennart Poettering94f04342010-01-30 01:55:42 +0100823 const char *filename,
824 unsigned line,
825 const char *section,
826 const char *lvalue,
827 const char *rvalue,
828 void *data,
829 void *userdata) {
830
831 ExecContext *c = data;
832 char *w;
833 size_t l;
834 char *state;
835
836 assert(filename);
837 assert(lvalue);
838 assert(rvalue);
839 assert(data);
840
Lennart Poetteringf60f22d2010-07-07 20:58:41 +0200841 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
Lennart Poettering94f04342010-01-30 01:55:42 +0100842 char *t;
843 int r;
844 cap_value_t cap;
845
846 if (!(t = strndup(w, l)))
847 return -ENOMEM;
848
849 r = cap_from_name(t, &cap);
850 free(t);
851
852 if (r < 0) {
853 log_error("[%s:%u] Failed to parse capability bounding set: %s", filename, line, rvalue);
854 return -EBADMSG;
855 }
856
857 c->capability_bounding_set_drop |= 1 << cap;
858 }
859
860 return 0;
861}
862
Lennart Poettering03fae012010-07-04 21:12:10 +0200863static int config_parse_timer_slack_nsec(
Lennart Poettering94f04342010-01-30 01:55:42 +0100864 const char *filename,
865 unsigned line,
866 const char *section,
867 const char *lvalue,
868 const char *rvalue,
869 void *data,
870 void *userdata) {
871
872 ExecContext *c = data;
873 unsigned long u;
874 int r;
875
876 assert(filename);
877 assert(lvalue);
878 assert(rvalue);
879 assert(data);
880
881 if ((r = safe_atolu(rvalue, &u)) < 0) {
882 log_error("[%s:%u] Failed to parse time slack value: %s", filename, line, rvalue);
883 return r;
884 }
885
Lennart Poettering03fae012010-07-04 21:12:10 +0200886 c->timer_slack_nsec = u;
Lennart Poettering94f04342010-01-30 01:55:42 +0100887
888 return 0;
889}
890
891static int config_parse_limit(
892 const char *filename,
893 unsigned line,
894 const char *section,
895 const char *lvalue,
896 const char *rvalue,
897 void *data,
898 void *userdata) {
899
900 struct rlimit **rl = data;
901 unsigned long long u;
902 int r;
903
904 assert(filename);
905 assert(lvalue);
906 assert(rvalue);
907 assert(data);
908
909 if ((r = safe_atollu(rvalue, &u)) < 0) {
910 log_error("[%s:%u] Failed to parse resource value: %s", filename, line, rvalue);
911 return r;
912 }
913
914 if (!*rl)
915 if (!(*rl = new(struct rlimit, 1)))
916 return -ENOMEM;
917
918 (*rl)->rlim_cur = (*rl)->rlim_max = (rlim_t) u;
919 return 0;
920}
921
Lennart Poettering8e274522010-03-31 16:29:55 +0200922static int config_parse_cgroup(
923 const char *filename,
924 unsigned line,
925 const char *section,
926 const char *lvalue,
927 const char *rvalue,
928 void *data,
929 void *userdata) {
930
931 Unit *u = userdata;
932 char *w;
933 size_t l;
934 char *state;
935
Lennart Poetteringf60f22d2010-07-07 20:58:41 +0200936 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
Lennart Poettering8e274522010-03-31 16:29:55 +0200937 char *t;
938 int r;
939
Lennart Poetteringf60f22d2010-07-07 20:58:41 +0200940 if (!(t = cunescape_length(w, l)))
Lennart Poettering8e274522010-03-31 16:29:55 +0200941 return -ENOMEM;
942
943 r = unit_add_cgroup_from_text(u, t);
944 free(t);
945
946 if (r < 0)
947 return r;
948 }
949
950 return 0;
951}
952
Lennart Poetteringa9a1e002010-04-06 02:40:10 +0200953static int config_parse_sysv_priority(
954 const char *filename,
955 unsigned line,
956 const char *section,
957 const char *lvalue,
958 const char *rvalue,
959 void *data,
960 void *userdata) {
961
962 int *priority = data;
963 int r, i;
964
965 assert(filename);
966 assert(lvalue);
967 assert(rvalue);
968 assert(data);
969
970 if ((r = safe_atoi(rvalue, &i)) < 0 || i < 0) {
971 log_error("[%s:%u] Failed to parse SysV start priority: %s", filename, line, rvalue);
972 return r;
973 }
974
975 *priority = (int) i;
976 return 0;
977}
978
Lennart Poettering487393e2010-07-07 01:10:27 +0200979static DEFINE_CONFIG_PARSE_ENUM(config_parse_kill_mode, kill_mode, KillMode, "Failed to parse kill mode");
Lennart Poettering50159e62010-04-08 00:52:14 +0200980
Lennart Poettering2e22afe2010-07-10 04:49:37 +0200981static int config_parse_kill_signal(
982 const char *filename,
983 unsigned line,
984 const char *section,
985 const char *lvalue,
986 const char *rvalue,
987 void *data,
988 void *userdata) {
989
990 int *sig = data;
991 int r;
992
993 assert(filename);
994 assert(lvalue);
995 assert(rvalue);
996 assert(sig);
997
998 if ((r = signal_from_string(rvalue)) <= 0)
999 if (startswith(rvalue, "SIG"))
1000 r = signal_from_string(rvalue+3);
1001
1002 if (r <= 0) {
1003 log_error("[%s:%u] Failed to parse kill signal: %s", filename, line, rvalue);
1004 return -EINVAL;
1005 }
1006
1007 *sig = r;
1008 return 0;
1009}
1010
Lennart Poettering15ae4222010-04-21 22:15:06 +02001011static int config_parse_mount_flags(
1012 const char *filename,
1013 unsigned line,
1014 const char *section,
1015 const char *lvalue,
1016 const char *rvalue,
1017 void *data,
1018 void *userdata) {
1019
1020 ExecContext *c = data;
1021 char *w;
1022 size_t l;
1023 char *state;
1024 unsigned long flags = 0;
1025
1026 assert(filename);
1027 assert(lvalue);
1028 assert(rvalue);
1029 assert(data);
1030
Lennart Poetteringf60f22d2010-07-07 20:58:41 +02001031 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
Lennart Poettering15ae4222010-04-21 22:15:06 +02001032 if (strncmp(w, "shared", l) == 0)
1033 flags |= MS_SHARED;
1034 else if (strncmp(w, "slave", l) == 0)
1035 flags |= MS_SLAVE;
1036 else if (strncmp(w, "private", l) == 0)
1037 flags |= MS_PRIVATE;
1038 else {
1039 log_error("[%s:%u] Failed to parse mount flags: %s", filename, line, rvalue);
1040 return -EINVAL;
1041 }
1042 }
1043
1044 c->mount_flags = flags;
1045 return 0;
1046}
1047
Lennart Poettering871d7de2010-05-24 01:45:54 +02001048static int config_parse_timer(
1049 const char *filename,
1050 unsigned line,
1051 const char *section,
1052 const char *lvalue,
1053 const char *rvalue,
1054 void *data,
1055 void *userdata) {
1056
1057 Timer *t = data;
1058 usec_t u;
1059 int r;
1060 TimerValue *v;
1061 TimerBase b;
1062
1063 assert(filename);
1064 assert(lvalue);
1065 assert(rvalue);
1066 assert(data);
1067
1068 if ((b = timer_base_from_string(lvalue)) < 0) {
1069 log_error("[%s:%u] Failed to parse timer base: %s", filename, line, lvalue);
1070 return -EINVAL;
1071 }
1072
1073 if ((r = parse_usec(rvalue, &u)) < 0) {
1074 log_error("[%s:%u] Failed to parse timer value: %s", filename, line, rvalue);
1075 return r;
1076 }
1077
1078 if (!(v = new0(TimerValue, 1)))
1079 return -ENOMEM;
1080
1081 v->base = b;
1082 v->value = u;
1083
1084 LIST_PREPEND(TimerValue, value, t->values, v);
1085
1086 return 0;
1087}
1088
1089static int config_parse_timer_unit(
1090 const char *filename,
1091 unsigned line,
1092 const char *section,
1093 const char *lvalue,
1094 const char *rvalue,
1095 void *data,
1096 void *userdata) {
1097
1098 Timer *t = data;
1099 int r;
Lennart Poettering398ef8b2010-07-08 02:43:18 +02001100 DBusError error;
1101
1102 assert(filename);
1103 assert(lvalue);
1104 assert(rvalue);
1105 assert(data);
1106
1107 dbus_error_init(&error);
Lennart Poettering871d7de2010-05-24 01:45:54 +02001108
1109 if (endswith(rvalue, ".timer")) {
1110 log_error("[%s:%u] Unit cannot be of type timer: %s", filename, line, rvalue);
1111 return -EINVAL;
1112 }
1113
Lennart Poettering398ef8b2010-07-08 02:43:18 +02001114 if ((r = manager_load_unit(t->meta.manager, rvalue, NULL, NULL, &t->unit)) < 0) {
1115 log_error("[%s:%u] Failed to load unit %s: %s", filename, line, rvalue, bus_error(&error, r));
1116 dbus_error_free(&error);
Lennart Poettering871d7de2010-05-24 01:45:54 +02001117 return r;
1118 }
1119
1120 return 0;
1121}
1122
Lennart Poettering01f78472010-05-24 05:25:33 +02001123static int config_parse_path_spec(
1124 const char *filename,
1125 unsigned line,
1126 const char *section,
1127 const char *lvalue,
1128 const char *rvalue,
1129 void *data,
1130 void *userdata) {
1131
1132 Path *p = data;
1133 PathSpec *s;
1134 PathType b;
1135
1136 assert(filename);
1137 assert(lvalue);
1138 assert(rvalue);
1139 assert(data);
1140
1141 if ((b = path_type_from_string(lvalue)) < 0) {
1142 log_error("[%s:%u] Failed to parse path type: %s", filename, line, lvalue);
1143 return -EINVAL;
1144 }
1145
1146 if (!path_is_absolute(rvalue)) {
1147 log_error("[%s:%u] Path is not absolute: %s", filename, line, rvalue);
1148 return -EINVAL;
1149 }
1150
1151 if (!(s = new0(PathSpec, 1)))
1152 return -ENOMEM;
1153
1154 if (!(s->path = strdup(rvalue))) {
1155 free(s);
1156 return -ENOMEM;
1157 }
1158
1159 path_kill_slashes(s->path);
1160
1161 s->type = b;
1162 s->inotify_fd = -1;
1163
1164 LIST_PREPEND(PathSpec, spec, p->specs, s);
1165
1166 return 0;
1167}
1168
1169static int config_parse_path_unit(
1170 const char *filename,
1171 unsigned line,
1172 const char *section,
1173 const char *lvalue,
1174 const char *rvalue,
1175 void *data,
1176 void *userdata) {
1177
1178 Path *t = data;
1179 int r;
Lennart Poettering398ef8b2010-07-08 02:43:18 +02001180 DBusError error;
1181
1182 assert(filename);
1183 assert(lvalue);
1184 assert(rvalue);
1185 assert(data);
1186
1187 dbus_error_init(&error);
Lennart Poettering01f78472010-05-24 05:25:33 +02001188
1189 if (endswith(rvalue, ".path")) {
1190 log_error("[%s:%u] Unit cannot be of type path: %s", filename, line, rvalue);
1191 return -EINVAL;
1192 }
1193
Lennart Poettering398ef8b2010-07-08 02:43:18 +02001194 if ((r = manager_load_unit(t->meta.manager, rvalue, NULL, &error, &t->unit)) < 0) {
1195 log_error("[%s:%u] Failed to load unit %s: %s", filename, line, rvalue, bus_error(&error, r));
1196 dbus_error_free(&error);
Lennart Poettering01f78472010-05-24 05:25:33 +02001197 return r;
1198 }
1199
1200 return 0;
1201}
1202
Lennart Poetteringddb26e12010-06-18 06:06:24 +02001203static int config_parse_env_file(
1204 const char *filename,
1205 unsigned line,
1206 const char *section,
1207 const char *lvalue,
1208 const char *rvalue,
1209 void *data,
1210 void *userdata) {
1211
1212 FILE *f;
1213 int r;
1214 char ***env = data;
1215
1216 assert(filename);
1217 assert(lvalue);
1218 assert(rvalue);
1219 assert(data);
1220
1221 if (!(f = fopen(rvalue, "re"))) {
1222 log_error("[%s:%u] Failed to open environment file '%s': %m", filename, line, rvalue);
1223 return -errno;
1224 }
1225
1226 while (!feof(f)) {
1227 char l[LINE_MAX], *p;
1228 char **t;
1229
1230 if (!fgets(l, sizeof(l), f)) {
1231 if (feof(f))
1232 break;
1233
1234 r = -errno;
1235 log_error("[%s:%u] Failed to read environment file '%s': %m", filename, line, rvalue);
1236 goto finish;
1237 }
1238
1239 p = strstrip(l);
1240
1241 if (!*p)
1242 continue;
1243
1244 if (strchr(COMMENTS, *p))
1245 continue;
1246
1247 t = strv_env_set(*env, p);
1248 strv_free(*env);
1249 *env = t;
1250 }
1251
1252 r = 0;
1253
1254finish:
1255 if (f)
1256 fclose(f);
1257
1258 return r;
1259}
1260
Lennart Poettering4fd59482010-07-01 00:29:17 +02001261static int config_parse_ip_tos(
1262 const char *filename,
1263 unsigned line,
1264 const char *section,
1265 const char *lvalue,
1266 const char *rvalue,
1267 void *data,
1268 void *userdata) {
1269
1270 int *ip_tos = data, x;
1271 int r;
1272
1273 assert(filename);
1274 assert(lvalue);
1275 assert(rvalue);
1276 assert(data);
1277
1278 if ((x = ip_tos_from_string(rvalue)) < 0)
1279 if ((r = safe_atoi(rvalue, &x)) < 0) {
1280 log_error("[%s:%u] Failed to parse IP TOS value: %s", filename, line, rvalue);
1281 return r;
1282 }
1283
1284 *ip_tos = x;
1285 return 0;
1286}
1287
Lennart Poettering487393e2010-07-07 01:10:27 +02001288static DEFINE_CONFIG_PARSE_ENUM(config_parse_notify_access, notify_access, NotifyAccess, "Failed to parse notify access specifier");
Lennart Poetteringc952c6e2010-06-18 23:12:48 +02001289
Lennart Poettering071830f2010-01-28 02:06:20 +01001290#define FOLLOW_MAX 8
Lennart Poettering87f0e412010-01-26 21:39:06 +01001291
Lennart Poettering9e2f7c12010-04-15 03:11:11 +02001292static int open_follow(char **filename, FILE **_f, Set *names, char **_final) {
Lennart Poettering0301abf2010-01-27 00:15:56 +01001293 unsigned c = 0;
Lennart Poettering87f0e412010-01-26 21:39:06 +01001294 int fd, r;
1295 FILE *f;
Lennart Poettering0301abf2010-01-27 00:15:56 +01001296 char *id = NULL;
Lennart Poettering87f0e412010-01-26 21:39:06 +01001297
1298 assert(filename);
1299 assert(*filename);
1300 assert(_f);
1301 assert(names);
1302
Lennart Poettering0301abf2010-01-27 00:15:56 +01001303 /* This will update the filename pointer if the loaded file is
1304 * reached by a symlink. The old string will be freed. */
Lennart Poettering87f0e412010-01-26 21:39:06 +01001305
Lennart Poettering0301abf2010-01-27 00:15:56 +01001306 for (;;) {
Lennart Poettering2c7108c2010-06-16 01:56:00 +02001307 char *target, *name;
Lennart Poettering87f0e412010-01-26 21:39:06 +01001308
Lennart Poettering0301abf2010-01-27 00:15:56 +01001309 if (c++ >= FOLLOW_MAX)
1310 return -ELOOP;
1311
Lennart Poetteringb08d03f2010-01-29 02:07:41 +01001312 path_kill_slashes(*filename);
1313
Lennart Poettering87f0e412010-01-26 21:39:06 +01001314 /* Add the file name we are currently looking at to
1315 * the names of this unit */
Lennart Poettering0301abf2010-01-27 00:15:56 +01001316 name = file_name_from_path(*filename);
1317 if (!(id = set_get(names, name))) {
Lennart Poettering87f0e412010-01-26 21:39:06 +01001318
Lennart Poettering0301abf2010-01-27 00:15:56 +01001319 if (!(id = strdup(name)))
1320 return -ENOMEM;
1321
1322 if ((r = set_put(names, id)) < 0) {
1323 free(id);
1324 return r;
Lennart Poettering87f0e412010-01-26 21:39:06 +01001325 }
Lennart Poettering87f0e412010-01-26 21:39:06 +01001326 }
1327
Lennart Poettering0301abf2010-01-27 00:15:56 +01001328 /* Try to open the file name, but don't if its a symlink */
1329 if ((fd = open(*filename, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW)) >= 0)
Lennart Poettering87f0e412010-01-26 21:39:06 +01001330 break;
1331
Lennart Poettering0301abf2010-01-27 00:15:56 +01001332 if (errno != ELOOP)
1333 return -errno;
Lennart Poettering87f0e412010-01-26 21:39:06 +01001334
Lennart Poettering0301abf2010-01-27 00:15:56 +01001335 /* Hmm, so this is a symlink. Let's read the name, and follow it manually */
Lennart Poettering2c7108c2010-06-16 01:56:00 +02001336 if ((r = readlink_and_make_absolute(*filename, &target)) < 0)
Lennart Poettering0301abf2010-01-27 00:15:56 +01001337 return r;
1338
Lennart Poettering0301abf2010-01-27 00:15:56 +01001339 free(*filename);
Lennart Poettering2c7108c2010-06-16 01:56:00 +02001340 *filename = target;
Lennart Poettering87f0e412010-01-26 21:39:06 +01001341 }
1342
1343 if (!(f = fdopen(fd, "r"))) {
1344 r = -errno;
Lennart Poettering9e2f7c12010-04-15 03:11:11 +02001345 close_nointr_nofail(fd);
Lennart Poettering0301abf2010-01-27 00:15:56 +01001346 return r;
Lennart Poettering87f0e412010-01-26 21:39:06 +01001347 }
1348
1349 *_f = f;
Lennart Poettering9e2f7c12010-04-15 03:11:11 +02001350 *_final = id;
Lennart Poettering0301abf2010-01-27 00:15:56 +01001351 return 0;
Lennart Poettering87f0e412010-01-26 21:39:06 +01001352}
1353
Lennart Poettering23a177e2010-04-06 02:43:58 +02001354static int merge_by_names(Unit **u, Set *names, const char *id) {
1355 char *k;
1356 int r;
1357
1358 assert(u);
1359 assert(*u);
1360 assert(names);
1361
1362 /* Let's try to add in all symlink names we found */
1363 while ((k = set_steal_first(names))) {
1364
1365 /* First try to merge in the other name into our
1366 * unit */
1367 if ((r = unit_merge_by_name(*u, k)) < 0) {
1368 Unit *other;
1369
1370 /* Hmm, we couldn't merge the other unit into
1371 * ours? Then let's try it the other way
1372 * round */
1373
1374 other = manager_get_unit((*u)->meta.manager, k);
1375 free(k);
1376
1377 if (other)
1378 if ((r = unit_merge(other, *u)) >= 0) {
1379 *u = other;
1380 return merge_by_names(u, names, NULL);
1381 }
1382
1383 return r;
1384 }
1385
1386 if (id == k)
1387 unit_choose_id(*u, id);
1388
1389 free(k);
1390 }
1391
1392 return 0;
1393}
1394
Lennart Poetteringe5373522010-04-10 17:53:17 +02001395static void dump_items(FILE *f, const ConfigItem *items) {
1396 const ConfigItem *i;
1397 const char *prev_section = NULL;
1398 bool not_first = false;
1399
1400 struct {
1401 ConfigParserCallback callback;
1402 const char *rvalue;
1403 } table[] = {
1404 { config_parse_int, "INTEGER" },
1405 { config_parse_unsigned, "UNSIGNED" },
1406 { config_parse_size, "SIZE" },
1407 { config_parse_bool, "BOOLEAN" },
1408 { config_parse_string, "STRING" },
1409 { config_parse_path, "PATH" },
1410 { config_parse_strv, "STRING [...]" },
1411 { config_parse_nice, "NICE" },
1412 { config_parse_oom_adjust, "OOMADJUST" },
1413 { config_parse_io_class, "IOCLASS" },
1414 { config_parse_io_priority, "IOPRIORITY" },
1415 { config_parse_cpu_sched_policy, "CPUSCHEDPOLICY" },
1416 { config_parse_cpu_sched_prio, "CPUSCHEDPRIO" },
1417 { config_parse_cpu_affinity, "CPUAFFINITY" },
1418 { config_parse_mode, "MODE" },
Lennart Poetteringddb26e12010-06-18 06:06:24 +02001419 { config_parse_env_file, "FILE" },
Lennart Poetteringe5373522010-04-10 17:53:17 +02001420 { config_parse_output, "OUTPUT" },
1421 { config_parse_input, "INPUT" },
1422 { config_parse_facility, "FACILITY" },
1423 { config_parse_level, "LEVEL" },
1424 { config_parse_capabilities, "CAPABILITIES" },
1425 { config_parse_secure_bits, "SECUREBITS" },
1426 { config_parse_bounding_set, "BOUNDINGSET" },
Lennart Poettering03fae012010-07-04 21:12:10 +02001427 { config_parse_timer_slack_nsec, "TIMERSLACK" },
Lennart Poetteringe5373522010-04-10 17:53:17 +02001428 { config_parse_limit, "LIMIT" },
1429 { config_parse_cgroup, "CGROUP [...]" },
1430 { config_parse_deps, "UNIT [...]" },
1431 { config_parse_names, "UNIT [...]" },
1432 { config_parse_exec, "PATH [ARGUMENT [...]]" },
1433 { config_parse_service_type, "SERVICETYPE" },
1434 { config_parse_service_restart, "SERVICERESTART" },
1435 { config_parse_sysv_priority, "SYSVPRIORITY" },
1436 { config_parse_kill_mode, "KILLMODE" },
Lennart Poettering2e22afe2010-07-10 04:49:37 +02001437 { config_parse_kill_signal, "SIGNAL" },
Lennart Poetteringe5373522010-04-10 17:53:17 +02001438 { config_parse_listen, "SOCKET [...]" },
1439 { config_parse_socket_bind, "SOCKETBIND" },
Lennart Poettering93ef5e82010-04-23 20:27:47 +02001440 { config_parse_bindtodevice, "NETWORKINTERFACE" },
1441 { config_parse_usec, "SECONDS" },
1442 { config_parse_path_strv, "PATH [...]" },
Lennart Poettering932921b2010-04-24 03:11:01 +02001443 { config_parse_mount_flags, "MOUNTFLAG [...]" },
Lennart Poetteringf2d37692010-06-18 23:25:19 +02001444 { config_parse_string_printf, "STRING" },
Lennart Poettering871d7de2010-05-24 01:45:54 +02001445 { config_parse_timer, "TIMER" },
1446 { config_parse_timer_unit, "NAME" },
Lennart Poetteringc952c6e2010-06-18 23:12:48 +02001447 { config_parse_path_spec, "PATH" },
1448 { config_parse_path_unit, "UNIT" },
Lennart Poettering8b03dae2010-07-01 16:34:26 +02001449 { config_parse_notify_access, "ACCESS" },
1450 { config_parse_ip_tos, "TOS" },
Lennart Poetteringe5373522010-04-10 17:53:17 +02001451 };
1452
1453 assert(f);
1454 assert(items);
1455
1456 for (i = items; i->lvalue; i++) {
1457 unsigned j;
1458 const char *rvalue = "OTHER";
1459
1460 if (!streq_ptr(i->section, prev_section)) {
1461 if (!not_first)
1462 not_first = true;
1463 else
1464 fputc('\n', f);
1465
1466 fprintf(f, "[%s]\n", i->section);
1467 prev_section = i->section;
1468 }
1469
1470 for (j = 0; j < ELEMENTSOF(table); j++)
1471 if (i->parse == table[j].callback) {
1472 rvalue = table[j].rvalue;
1473 break;
1474 }
1475
1476 fprintf(f, "%s=%s\n", i->lvalue, rvalue);
1477 }
1478}
1479
1480static int load_from_path(Unit *u, const char *path) {
Lennart Poettering87f0e412010-01-26 21:39:06 +01001481
1482 static const char* const section_table[_UNIT_TYPE_MAX] = {
1483 [UNIT_SERVICE] = "Service",
1484 [UNIT_TIMER] = "Timer",
1485 [UNIT_SOCKET] = "Socket",
1486 [UNIT_TARGET] = "Target",
1487 [UNIT_DEVICE] = "Device",
1488 [UNIT_MOUNT] = "Mount",
1489 [UNIT_AUTOMOUNT] = "Automount",
Maarten Lankhorst07b0b132010-05-09 18:44:11 +02001490 [UNIT_SNAPSHOT] = "Snapshot",
Lennart Poettering01f78472010-05-24 05:25:33 +02001491 [UNIT_SWAP] = "Swap",
1492 [UNIT_PATH] = "Path"
Lennart Poettering42f4e3c2010-01-19 02:56:37 +01001493 };
1494
Lennart Poettering034c6ed2010-01-26 04:18:44 +01001495#define EXEC_CONTEXT_CONFIG_ITEMS(context, section) \
Lennart Poettering9eba9da2010-01-29 20:46:22 +01001496 { "WorkingDirectory", config_parse_path, &(context).working_directory, section }, \
1497 { "RootDirectory", config_parse_path, &(context).root_directory, section }, \
Lennart Poetteringf2d37692010-06-18 23:25:19 +02001498 { "User", config_parse_string_printf, &(context).user, section }, \
1499 { "Group", config_parse_string_printf, &(context).group, section }, \
Lennart Poettering1c01f822010-01-27 02:16:11 +01001500 { "SupplementaryGroups", config_parse_strv, &(context).supplementary_groups, section }, \
Lennart Poetteringfb33a392010-01-28 02:53:56 +01001501 { "Nice", config_parse_nice, &(context), section }, \
1502 { "OOMAdjust", config_parse_oom_adjust, &(context), section }, \
Lennart Poettering9eba9da2010-01-29 20:46:22 +01001503 { "IOSchedulingClass", config_parse_io_class, &(context), section }, \
Lennart Poettering94f04342010-01-30 01:55:42 +01001504 { "IOSchedulingPriority", config_parse_io_priority, &(context), section }, \
1505 { "CPUSchedulingPolicy", config_parse_cpu_sched_policy,&(context), section }, \
1506 { "CPUSchedulingPriority", config_parse_cpu_sched_prio, &(context), section }, \
Lennart Poettering38b48752010-02-02 12:50:04 +01001507 { "CPUSchedulingResetOnFork", config_parse_bool, &(context).cpu_sched_reset_on_fork, section }, \
Lennart Poettering94f04342010-01-30 01:55:42 +01001508 { "CPUAffinity", config_parse_cpu_affinity, &(context), section }, \
Lennart Poetteringb5a06992010-02-12 02:02:14 +01001509 { "UMask", config_parse_mode, &(context).umask, section }, \
Lennart Poettering071830f2010-01-28 02:06:20 +01001510 { "Environment", config_parse_strv, &(context).environment, section }, \
Lennart Poetteringddb26e12010-06-18 06:06:24 +02001511 { "EnvironmentFile", config_parse_env_file, &(context).environment, section }, \
Lennart Poettering80876c22010-04-13 02:06:27 +02001512 { "StandardInput", config_parse_input, &(context).std_input, section }, \
1513 { "StandardOutput", config_parse_output, &(context).std_output, section }, \
Lennart Poetteringa4ddf822010-06-03 03:37:12 +02001514 { "StandardError", config_parse_output, &(context).std_error, section }, \
Lennart Poettering80876c22010-04-13 02:06:27 +02001515 { "TTYPath", config_parse_path, &(context).tty_path, section }, \
Lennart Poetteringf2d37692010-06-18 23:25:19 +02001516 { "SyslogIdentifier", config_parse_string_printf, &(context).syslog_identifier, section }, \
Lennart Poettering071830f2010-01-28 02:06:20 +01001517 { "SyslogFacility", config_parse_facility, &(context).syslog_priority, section }, \
Lennart Poettering94f04342010-01-30 01:55:42 +01001518 { "SyslogLevel", config_parse_level, &(context).syslog_priority, section }, \
Lennart Poettering74922902010-07-05 01:08:13 +02001519 { "SyslogLevelPrefix", config_parse_bool, &(context).syslog_level_prefix, section }, \
Lennart Poettering94f04342010-01-30 01:55:42 +01001520 { "Capabilities", config_parse_capabilities, &(context), section }, \
1521 { "SecureBits", config_parse_secure_bits, &(context), section }, \
1522 { "CapabilityBoundingSetDrop", config_parse_bounding_set, &(context), section }, \
Lennart Poettering03fae012010-07-04 21:12:10 +02001523 { "TimerSlackNSec", config_parse_timer_slack_nsec,&(context), section }, \
Lennart Poettering94f04342010-01-30 01:55:42 +01001524 { "LimitCPU", config_parse_limit, &(context).rlimit[RLIMIT_CPU], section }, \
1525 { "LimitFSIZE", config_parse_limit, &(context).rlimit[RLIMIT_FSIZE], section }, \
1526 { "LimitDATA", config_parse_limit, &(context).rlimit[RLIMIT_DATA], section }, \
1527 { "LimitSTACK", config_parse_limit, &(context).rlimit[RLIMIT_STACK], section }, \
1528 { "LimitCORE", config_parse_limit, &(context).rlimit[RLIMIT_CORE], section }, \
1529 { "LimitRSS", config_parse_limit, &(context).rlimit[RLIMIT_RSS], section }, \
1530 { "LimitNOFILE", config_parse_limit, &(context).rlimit[RLIMIT_NOFILE], section }, \
1531 { "LimitAS", config_parse_limit, &(context).rlimit[RLIMIT_AS], section }, \
1532 { "LimitNPROC", config_parse_limit, &(context).rlimit[RLIMIT_NPROC], section }, \
1533 { "LimitMEMLOCK", config_parse_limit, &(context).rlimit[RLIMIT_MEMLOCK], section }, \
1534 { "LimitLOCKS", config_parse_limit, &(context).rlimit[RLIMIT_LOCKS], section }, \
1535 { "LimitSIGPENDING", config_parse_limit, &(context).rlimit[RLIMIT_SIGPENDING], section }, \
1536 { "LimitMSGQUEUE", config_parse_limit, &(context).rlimit[RLIMIT_MSGQUEUE], section }, \
1537 { "LimitNICE", config_parse_limit, &(context).rlimit[RLIMIT_NICE], section }, \
1538 { "LimitRTPRIO", config_parse_limit, &(context).rlimit[RLIMIT_RTPRIO], section }, \
Lennart Poettering451a0742010-02-12 02:00:18 +01001539 { "LimitRTTIME", config_parse_limit, &(context).rlimit[RLIMIT_RTTIME], section }, \
Lennart Poettering15ae4222010-04-21 22:15:06 +02001540 { "ControlGroup", config_parse_cgroup, u, section }, \
1541 { "ReadWriteDirectories", config_parse_path_strv, &(context).read_write_dirs, section }, \
1542 { "ReadOnlyDirectories", config_parse_path_strv, &(context).read_only_dirs, section }, \
1543 { "InaccessibleDirectories",config_parse_path_strv, &(context).inaccessible_dirs, section }, \
1544 { "PrivateTmp", config_parse_bool, &(context).private_tmp, section }, \
Lennart Poetteringdf1f0af2010-06-16 16:25:42 +02001545 { "MountFlags", config_parse_mount_flags, &(context), section }, \
Lennart Poetteringf2d37692010-06-18 23:25:19 +02001546 { "TCPWrapName", config_parse_string_printf, &(context).tcpwrap_name, section }, \
Lennart Poettering2e22afe2010-07-10 04:49:37 +02001547 { "PAMName", config_parse_string_printf, &(context).pam_name, section }, \
1548 { "KillMode", config_parse_kill_mode, &(context).kill_mode, section }, \
1549 { "KillSignal", config_parse_kill_signal, &(context).kill_signal, section }
Lennart Poettering034c6ed2010-01-26 04:18:44 +01001550
Lennart Poettering3efd4192009-11-19 23:13:20 +01001551 const ConfigItem items[] = {
Lennart Poettering09477262010-04-15 23:20:17 +02001552 { "Names", config_parse_names, u, "Unit" },
Lennart Poetteringf2d37692010-06-18 23:25:19 +02001553 { "Description", config_parse_string_printf, &u->meta.description, "Unit" },
Lennart Poettering09477262010-04-15 23:20:17 +02001554 { "Requires", config_parse_deps, UINT_TO_PTR(UNIT_REQUIRES), "Unit" },
1555 { "RequiresOverridable", config_parse_deps, UINT_TO_PTR(UNIT_REQUIRES_OVERRIDABLE), "Unit" },
1556 { "Requisite", config_parse_deps, UINT_TO_PTR(UNIT_REQUISITE), "Unit" },
1557 { "RequisiteOverridable", config_parse_deps, UINT_TO_PTR(UNIT_REQUISITE_OVERRIDABLE), "Unit" },
1558 { "Wants", config_parse_deps, UINT_TO_PTR(UNIT_WANTS), "Unit" },
1559 { "Conflicts", config_parse_deps, UINT_TO_PTR(UNIT_CONFLICTS), "Unit" },
1560 { "Before", config_parse_deps, UINT_TO_PTR(UNIT_BEFORE), "Unit" },
1561 { "After", config_parse_deps, UINT_TO_PTR(UNIT_AFTER), "Unit" },
Lennart Poettering45fb0692010-07-17 00:57:51 +02001562 { "OnFailure", config_parse_deps, UINT_TO_PTR(UNIT_ON_FAILURE), "Unit" },
Lennart Poettering09477262010-04-15 23:20:17 +02001563 { "RecursiveStop", config_parse_bool, &u->meta.recursive_stop, "Unit" },
1564 { "StopWhenUnneeded", config_parse_bool, &u->meta.stop_when_unneeded, "Unit" },
Lennart Poetteringbc0f8772010-05-21 23:41:54 +02001565 { "OnlyByDependency", config_parse_bool, &u->meta.only_by_dependency, "Unit" },
Lennart Poetteringa40eb732010-07-03 19:48:33 +02001566 { "DefaultDependencies", config_parse_bool, &u->meta.default_dependencies, "Unit" },
Lennart Poettering3b6fdb52010-07-12 02:56:17 +02001567 { "IgnoreDependencyFailure",config_parse_bool, &u->meta.ignore_dependency_failure, "Unit" },
Lennart Poettering034c6ed2010-01-26 04:18:44 +01001568
Lennart Poettering1c01f822010-01-27 02:16:11 +01001569 { "PIDFile", config_parse_path, &u->service.pid_file, "Service" },
1570 { "ExecStartPre", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START_PRE, "Service" },
1571 { "ExecStart", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START, "Service" },
1572 { "ExecStartPost", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START_POST, "Service" },
1573 { "ExecReload", config_parse_exec, u->service.exec_command+SERVICE_EXEC_RELOAD, "Service" },
1574 { "ExecStop", config_parse_exec, u->service.exec_command+SERVICE_EXEC_STOP, "Service" },
1575 { "ExecStopPost", config_parse_exec, u->service.exec_command+SERVICE_EXEC_STOP_POST, "Service" },
1576 { "RestartSec", config_parse_usec, &u->service.restart_usec, "Service" },
1577 { "TimeoutSec", config_parse_usec, &u->service.timeout_usec, "Service" },
Lennart Poettering0d87eb42010-04-13 02:22:41 +02001578 { "Type", config_parse_service_type, &u->service.type, "Service" },
1579 { "Restart", config_parse_service_restart, &u->service.restart, "Service" },
Lennart Poettering81a2b7c2010-02-14 22:43:08 +01001580 { "PermissionsStartOnly", config_parse_bool, &u->service.permissions_start_only, "Service" },
1581 { "RootDirectoryStartOnly", config_parse_bool, &u->service.root_directory_start_only, "Service" },
Lennart Poettering8e274522010-03-31 16:29:55 +02001582 { "ValidNoProcess", config_parse_bool, &u->service.valid_no_process, "Service" },
Lennart Poetteringa9a1e002010-04-06 02:40:10 +02001583 { "SysVStartPriority", config_parse_sysv_priority, &u->service.sysv_start_priority, "Service" },
Lennart Poetteringb778d552010-04-10 17:48:41 +02001584 { "NonBlocking", config_parse_bool, &u->service.exec_context.non_blocking, "Service" },
Lennart Poetteringf2d37692010-06-18 23:25:19 +02001585 { "BusName", config_parse_string_printf, &u->service.bus_name, "Service" },
Lennart Poetteringc952c6e2010-06-18 23:12:48 +02001586 { "NotifyAccess", config_parse_notify_access, &u->service.notify_access, "Service" },
Lennart Poettering87f0e412010-01-26 21:39:06 +01001587 EXEC_CONTEXT_CONFIG_ITEMS(u->service.exec_context, "Service"),
Lennart Poettering034c6ed2010-01-26 04:18:44 +01001588
Lennart Poettering1c01f822010-01-27 02:16:11 +01001589 { "ListenStream", config_parse_listen, &u->socket, "Socket" },
1590 { "ListenDatagram", config_parse_listen, &u->socket, "Socket" },
1591 { "ListenSequentialPacket", config_parse_listen, &u->socket, "Socket" },
1592 { "ListenFIFO", config_parse_listen, &u->socket, "Socket" },
1593 { "BindIPv6Only", config_parse_socket_bind, &u->socket, "Socket" },
1594 { "Backlog", config_parse_unsigned, &u->socket.backlog, "Socket" },
Lennart Poetteringacbb0222010-01-27 04:31:52 +01001595 { "BindToDevice", config_parse_bindtodevice, &u->socket, "Socket" },
Lennart Poettering1c01f822010-01-27 02:16:11 +01001596 { "ExecStartPre", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_START_PRE, "Socket" },
1597 { "ExecStartPost", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_START_POST, "Socket" },
1598 { "ExecStopPre", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_STOP_PRE, "Socket" },
1599 { "ExecStopPost", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_STOP_POST, "Socket" },
Lennart Poettering108736d2010-04-10 17:50:54 +02001600 { "TimeoutSec", config_parse_usec, &u->socket.timeout_usec, "Socket" },
Lennart Poetteringb5a06992010-02-12 02:02:14 +01001601 { "DirectoryMode", config_parse_mode, &u->socket.directory_mode, "Socket" },
1602 { "SocketMode", config_parse_mode, &u->socket.socket_mode, "Socket" },
Lennart Poettering4f2d5282010-04-15 06:19:54 +02001603 { "Accept", config_parse_bool, &u->socket.accept, "Socket" },
Lennart Poettering6cf6bbc2010-06-19 04:25:28 +02001604 { "MaxConnections", config_parse_unsigned, &u->socket.max_connections, "Socket" },
Lennart Poettering4fd59482010-07-01 00:29:17 +02001605 { "KeepAlive", config_parse_bool, &u->socket.keep_alive, "Socket" },
1606 { "Priority", config_parse_int, &u->socket.priority, "Socket" },
1607 { "ReceiveBuffer", config_parse_size, &u->socket.receive_buffer, "Socket" },
1608 { "SendBuffer", config_parse_size, &u->socket.send_buffer, "Socket" },
1609 { "IPTOS", config_parse_ip_tos, &u->socket.ip_tos, "Socket" },
1610 { "IPTTL", config_parse_int, &u->socket.ip_ttl, "Socket" },
1611 { "Mark", config_parse_int, &u->socket.mark, "Socket" },
1612 { "PipeSize", config_parse_size, &u->socket.pipe_size, "Socket" },
1613 { "FreeBind", config_parse_bool, &u->socket.free_bind, "Socket" },
Lennart Poettering87f0e412010-01-26 21:39:06 +01001614 EXEC_CONTEXT_CONFIG_ITEMS(u->socket.exec_context, "Socket"),
Lennart Poettering034c6ed2010-01-26 04:18:44 +01001615
Lennart Poetteringe5373522010-04-10 17:53:17 +02001616 { "What", config_parse_string, &u->mount.parameters_fragment.what, "Mount" },
1617 { "Where", config_parse_path, &u->mount.where, "Mount" },
1618 { "Options", config_parse_string, &u->mount.parameters_fragment.options, "Mount" },
1619 { "Type", config_parse_string, &u->mount.parameters_fragment.fstype, "Mount" },
1620 { "TimeoutSec", config_parse_usec, &u->mount.timeout_usec, "Mount" },
Lennart Poettering3e5235b2010-07-02 00:28:44 +02001621 { "DirectoryMode", config_parse_mode, &u->mount.directory_mode, "Mount" },
Lennart Poetteringe5373522010-04-10 17:53:17 +02001622 EXEC_CONTEXT_CONFIG_ITEMS(u->mount.exec_context, "Mount"),
Lennart Poettering034c6ed2010-01-26 04:18:44 +01001623
Lennart Poettering8d567582010-04-16 23:24:39 +02001624 { "Where", config_parse_path, &u->automount.where, "Automount" },
Lennart Poettering1cf18f22010-07-02 01:17:21 +02001625 { "DirectoryMode", config_parse_mode, &u->automount.directory_mode, "Automount" },
Lennart Poettering8d567582010-04-16 23:24:39 +02001626
Lennart Poettering01f78472010-05-24 05:25:33 +02001627 { "What", config_parse_path, &u->swap.parameters_fragment.what, "Swap" },
1628 { "Priority", config_parse_int, &u->swap.parameters_fragment.priority, "Swap" },
Maarten Lankhorst07b0b132010-05-09 18:44:11 +02001629
Lennart Poettering03fae012010-07-04 21:12:10 +02001630 { "OnActiveSec", config_parse_timer, &u->timer, "Timer" },
1631 { "OnBootSec", config_parse_timer, &u->timer, "Timer" },
1632 { "OnStartupSec", config_parse_timer, &u->timer, "Timer" },
1633 { "OnUnitActiveSec", config_parse_timer, &u->timer, "Timer" },
1634 { "OnUnitInactiveSec", config_parse_timer, &u->timer, "Timer" },
Lennart Poettering01f78472010-05-24 05:25:33 +02001635 { "Unit", config_parse_timer_unit, &u->timer, "Timer" },
1636
1637 { "PathExists", config_parse_path_spec, &u->path, "Path" },
1638 { "PathChanged", config_parse_path_spec, &u->path, "Path" },
1639 { "DirectoryNotEmpty", config_parse_path_spec, &u->path, "Path" },
1640 { "Unit", config_parse_path_unit, &u->path, "Path" },
Lennart Poettering871d7de2010-05-24 01:45:54 +02001641
Lennart Poettering10e87ee2010-06-16 01:58:50 +02001642 /* The [Install] section is ignored here. */
1643 { "Alias", NULL, NULL, "Install" },
1644 { "WantedBy", NULL, NULL, "Install" },
1645 { "Also", NULL, NULL, "Install" },
1646
Lennart Poettering3efd4192009-11-19 23:13:20 +01001647 { NULL, NULL, NULL, NULL }
1648 };
1649
Lennart Poettering034c6ed2010-01-26 04:18:44 +01001650#undef EXEC_CONTEXT_CONFIG_ITEMS
Lennart Poettering42f4e3c2010-01-19 02:56:37 +01001651
Lennart Poettering10e87ee2010-06-16 01:58:50 +02001652 const char *sections[4];
Lennart Poettering0301abf2010-01-27 00:15:56 +01001653 int r;
Lennart Poettering87f0e412010-01-26 21:39:06 +01001654 Set *symlink_names;
Lennart Poettering23a177e2010-04-06 02:43:58 +02001655 FILE *f = NULL;
1656 char *filename = NULL, *id = NULL;
1657 Unit *merged;
Lennart Poettering45fb0692010-07-17 00:57:51 +02001658 struct stat st;
Lennart Poettering23a177e2010-04-06 02:43:58 +02001659
Lennart Poetteringe5373522010-04-10 17:53:17 +02001660 if (!u) {
1661 /* Dirty dirty hack. */
1662 dump_items((FILE*) path, items);
1663 return 0;
1664 }
1665
Lennart Poettering23a177e2010-04-06 02:43:58 +02001666 assert(u);
Lennart Poetteringe5373522010-04-10 17:53:17 +02001667 assert(path);
Lennart Poettering3efd4192009-11-19 23:13:20 +01001668
Lennart Poettering09477262010-04-15 23:20:17 +02001669 sections[0] = "Unit";
Lennart Poettering87f0e412010-01-26 21:39:06 +01001670 sections[1] = section_table[u->meta.type];
Lennart Poettering10e87ee2010-06-16 01:58:50 +02001671 sections[2] = "Install";
1672 sections[3] = NULL;
Lennart Poettering42f4e3c2010-01-19 02:56:37 +01001673
Lennart Poettering87f0e412010-01-26 21:39:06 +01001674 if (!(symlink_names = set_new(string_hash_func, string_compare_func)))
1675 return -ENOMEM;
Lennart Poettering3efd4192009-11-19 23:13:20 +01001676
Lennart Poettering036643a2010-02-13 01:07:02 +01001677 if (path_is_absolute(path)) {
1678
1679 if (!(filename = strdup(path))) {
1680 r = -ENOMEM;
1681 goto finish;
1682 }
1683
1684 if ((r = open_follow(&filename, &f, symlink_names, &id)) < 0) {
1685 free(filename);
1686 filename = NULL;
1687
1688 if (r != -ENOENT)
1689 goto finish;
1690 }
1691
1692 } else {
1693 char **p;
1694
Lennart Poettering84e35432010-06-15 14:45:15 +02001695 STRV_FOREACH(p, u->meta.manager->lookup_paths.unit_path) {
Lennart Poettering036643a2010-02-13 01:07:02 +01001696
1697 /* Instead of opening the path right away, we manually
1698 * follow all symlinks and add their name to our unit
1699 * name set while doing so */
1700 if (!(filename = path_make_absolute(path, *p))) {
1701 r = -ENOMEM;
1702 goto finish;
1703 }
1704
Lennart Poetteringfe518222010-07-11 00:52:00 +02001705 if (u->meta.manager->unit_path_cache &&
1706 !set_get(u->meta.manager->unit_path_cache, filename))
1707 r = -ENOENT;
1708 else
1709 r = open_follow(&filename, &f, symlink_names, &id);
1710
1711 if (r < 0) {
Lennart Poettering036643a2010-02-13 01:07:02 +01001712 char *sn;
1713
1714 free(filename);
1715 filename = NULL;
1716
1717 if (r != -ENOENT)
1718 goto finish;
1719
1720 /* Empty the symlink names for the next run */
1721 while ((sn = set_steal_first(symlink_names)))
1722 free(sn);
1723
1724 continue;
1725 }
1726
1727 break;
1728 }
Lennart Poettering034c6ed2010-01-26 04:18:44 +01001729 }
1730
Lennart Poettering036643a2010-02-13 01:07:02 +01001731 if (!filename) {
Lennart Poettering23a177e2010-04-06 02:43:58 +02001732 r = 0;
1733 goto finish;
1734 }
1735
1736 merged = u;
1737 if ((r = merge_by_names(&merged, symlink_names, id)) < 0)
1738 goto finish;
1739
1740 if (merged != u) {
Lennart Poetteringe5373522010-04-10 17:53:17 +02001741 u->meta.load_state = UNIT_MERGED;
Lennart Poettering23a177e2010-04-06 02:43:58 +02001742 r = 0;
Lennart Poettering0301abf2010-01-27 00:15:56 +01001743 goto finish;
1744 }
1745
Lennart Poettering45fb0692010-07-17 00:57:51 +02001746 zero(st);
1747 if (fstat(fileno(f), &st) < 0) {
1748 r = -errno;
1749 goto finish;
1750 }
1751
1752 if (!S_ISREG(st.st_mode)) {
1753 r = -ENOENT;
1754 goto finish;
1755 }
1756
Lennart Poettering0301abf2010-01-27 00:15:56 +01001757 /* Now, parse the file contents */
Lennart Poettering10e87ee2010-06-16 01:58:50 +02001758 if ((r = config_parse(filename, f, sections, items, false, u)) < 0)
Lennart Poettering0301abf2010-01-27 00:15:56 +01001759 goto finish;
1760
Lennart Poettering6be1e7d2010-02-14 01:01:10 +01001761 free(u->meta.fragment_path);
1762 u->meta.fragment_path = filename;
Lennart Poettering0301abf2010-01-27 00:15:56 +01001763 filename = NULL;
1764
Lennart Poettering45fb0692010-07-17 00:57:51 +02001765 u->meta.fragment_mtime = timespec_load(&st.st_mtim);
1766
Lennart Poetteringe5373522010-04-10 17:53:17 +02001767 u->meta.load_state = UNIT_LOADED;
Lennart Poettering23a177e2010-04-06 02:43:58 +02001768 r = 0;
Lennart Poettering87f0e412010-01-26 21:39:06 +01001769
1770finish:
Lennart Poettering53ec43c2010-06-15 02:45:26 +02001771 set_free_free(symlink_names);
Lennart Poettering0301abf2010-01-27 00:15:56 +01001772 free(filename);
1773
Lennart Poettering23a177e2010-04-06 02:43:58 +02001774 if (f)
1775 fclose(f);
1776
Lennart Poettering0301abf2010-01-27 00:15:56 +01001777 return r;
1778}
1779
Lennart Poetteringe5373522010-04-10 17:53:17 +02001780int unit_load_fragment(Unit *u) {
Lennart Poettering23a177e2010-04-06 02:43:58 +02001781 int r;
Lennart Poettering0301abf2010-01-27 00:15:56 +01001782
1783 assert(u);
Lennart Poettering0301abf2010-01-27 00:15:56 +01001784
Lennart Poettering23a177e2010-04-06 02:43:58 +02001785 if (u->meta.fragment_path) {
1786
Lennart Poetteringe5373522010-04-10 17:53:17 +02001787 if ((r = load_from_path(u, u->meta.fragment_path)) < 0)
Lennart Poettering23a177e2010-04-06 02:43:58 +02001788 return r;
1789
1790 } else {
Lennart Poettering0301abf2010-01-27 00:15:56 +01001791 Iterator i;
Lennart Poettering890f4342010-02-14 01:08:20 +01001792 const char *t;
Lennart Poettering0301abf2010-01-27 00:15:56 +01001793
Lennart Poettering890f4342010-02-14 01:08:20 +01001794 /* Try to find the unit under its id */
Lennart Poettering9e2f7c12010-04-15 03:11:11 +02001795 if ((r = load_from_path(u, u->meta.id)) < 0)
1796 return r;
Lennart Poettering890f4342010-02-14 01:08:20 +01001797
1798 /* Try to find an alias we can load this with */
Lennart Poetteringe5373522010-04-10 17:53:17 +02001799 if (u->meta.load_state == UNIT_STUB)
Lennart Poettering23a177e2010-04-06 02:43:58 +02001800 SET_FOREACH(t, u->meta.names, i) {
1801
Lennart Poettering9e2f7c12010-04-15 03:11:11 +02001802 if (t == u->meta.id)
Lennart Poettering23a177e2010-04-06 02:43:58 +02001803 continue;
1804
Lennart Poetteringe5373522010-04-10 17:53:17 +02001805 if ((r = load_from_path(u, t)) < 0)
Lennart Poettering23a177e2010-04-06 02:43:58 +02001806 return r;
1807
Lennart Poetteringe5373522010-04-10 17:53:17 +02001808 if (u->meta.load_state != UNIT_STUB)
Lennart Poettering890f4342010-02-14 01:08:20 +01001809 break;
Lennart Poettering23a177e2010-04-06 02:43:58 +02001810 }
Lennart Poettering9e2f7c12010-04-15 03:11:11 +02001811
1812 /* Now, follow the same logic, but look for a template */
1813 if (u->meta.load_state == UNIT_STUB && u->meta.instance) {
1814 char *k;
1815
1816 if (!(k = unit_name_template(u->meta.id)))
1817 return -ENOMEM;
1818
1819 r = load_from_path(u, k);
1820 free(k);
1821
1822 if (r < 0)
1823 return r;
1824
1825 if (u->meta.load_state == UNIT_STUB)
1826 SET_FOREACH(t, u->meta.names, i) {
1827
1828 if (t == u->meta.id)
1829 continue;
1830
1831 if (!(k = unit_name_template(t)))
1832 return -ENOMEM;
1833
1834 r = load_from_path(u, k);
1835 free(k);
1836
1837 if (r < 0)
1838 return r;
1839
1840 if (u->meta.load_state != UNIT_STUB)
1841 break;
1842 }
1843 }
Lennart Poettering0301abf2010-01-27 00:15:56 +01001844 }
Lennart Poettering87f0e412010-01-26 21:39:06 +01001845
Lennart Poettering23a177e2010-04-06 02:43:58 +02001846 return 0;
Lennart Poettering3efd4192009-11-19 23:13:20 +01001847}
Lennart Poetteringe5373522010-04-10 17:53:17 +02001848
1849void unit_dump_config_items(FILE *f) {
1850 /* OK, this wins a prize for extreme ugliness. */
1851
1852 load_from_path(NULL, (const void*) f);
1853}