blob: 6a71d422f4a25e3030be26dee95d900fc0fabff7 [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 Poettering398ef8b2010-07-08 02:43:18 +020042#include "bus-errors.h"
Lennart Poettering3efd4192009-11-19 23:13:20 +010043
Lennart Poetteringddb26e12010-06-18 06:06:24 +020044#define COMMENTS "#;\n"
Lennart Poetteringddb26e12010-06-18 06:06:24 +020045
Lennart Poettering42f4e3c2010-01-19 02:56:37 +010046static int config_parse_deps(
Lennart Poettering3efd4192009-11-19 23:13:20 +010047 const char *filename,
48 unsigned line,
49 const char *section,
50 const char *lvalue,
51 const char *rvalue,
52 void *data,
53 void *userdata) {
54
Lennart Poettering87f0e412010-01-26 21:39:06 +010055 UnitDependency d = PTR_TO_UINT(data);
56 Unit *u = userdata;
Lennart Poettering3efd4192009-11-19 23:13:20 +010057 char *w;
58 size_t l;
59 char *state;
60
61 assert(filename);
62 assert(lvalue);
63 assert(rvalue);
Lennart Poettering3efd4192009-11-19 23:13:20 +010064
Lennart Poetteringf60f22d2010-07-07 20:58:41 +020065 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
Lennart Poettering9e2f7c12010-04-15 03:11:11 +020066 char *t, *k;
Lennart Poettering3efd4192009-11-19 23:13:20 +010067 int r;
Lennart Poettering3efd4192009-11-19 23:13:20 +010068
69 if (!(t = strndup(w, l)))
70 return -ENOMEM;
71
Lennart Poettering9e2f7c12010-04-15 03:11:11 +020072 k = unit_name_printf(u, t);
Lennart Poettering3efd4192009-11-19 23:13:20 +010073 free(t);
74
Lennart Poettering9e2f7c12010-04-15 03:11:11 +020075 if (!k)
76 return -ENOMEM;
77
Lennart Poettering701cc382010-04-21 06:01:13 +020078 r = unit_add_dependency_by_name(u, d, k, NULL, true);
Lennart Poettering9e2f7c12010-04-15 03:11:11 +020079 free(k);
80
Lennart Poettering3efd4192009-11-19 23:13:20 +010081 if (r < 0)
82 return r;
Lennart Poettering3efd4192009-11-19 23:13:20 +010083 }
84
85 return 0;
86}
87
Lennart Poettering42f4e3c2010-01-19 02:56:37 +010088static int config_parse_names(
Lennart Poettering87d15152010-01-18 23:50:13 +010089 const char *filename,
90 unsigned line,
91 const char *section,
92 const char *lvalue,
93 const char *rvalue,
94 void *data,
95 void *userdata) {
96
Lennart Poettering87f0e412010-01-26 21:39:06 +010097 Unit *u = userdata;
Lennart Poettering87d15152010-01-18 23:50:13 +010098 char *w;
99 size_t l;
100 char *state;
101
102 assert(filename);
103 assert(lvalue);
104 assert(rvalue);
105 assert(data);
106
Lennart Poetteringf60f22d2010-07-07 20:58:41 +0200107 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
Lennart Poettering9e2f7c12010-04-15 03:11:11 +0200108 char *t, *k;
Lennart Poettering87d15152010-01-18 23:50:13 +0100109 int r;
Lennart Poettering87d15152010-01-18 23:50:13 +0100110
111 if (!(t = strndup(w, l)))
112 return -ENOMEM;
113
Lennart Poettering9e2f7c12010-04-15 03:11:11 +0200114 k = unit_name_printf(u, t);
Lennart Poettering87d15152010-01-18 23:50:13 +0100115 free(t);
Lennart Poettering23a177e2010-04-06 02:43:58 +0200116
Lennart Poettering9e2f7c12010-04-15 03:11:11 +0200117 if (!k)
118 return -ENOMEM;
119
120 r = unit_merge_by_name(u, k);
121 free(k);
122
Lennart Poettering23a177e2010-04-06 02:43:58 +0200123 if (r < 0)
124 return r;
Lennart Poettering87d15152010-01-18 23:50:13 +0100125 }
126
127 return 0;
128}
129
Lennart Poetteringf2d37692010-06-18 23:25:19 +0200130static int config_parse_string_printf(
Lennart Poettering932921b2010-04-24 03:11:01 +0200131 const char *filename,
132 unsigned line,
133 const char *section,
134 const char *lvalue,
135 const char *rvalue,
136 void *data,
137 void *userdata) {
138
139 Unit *u = userdata;
Lennart Poetteringf2d37692010-06-18 23:25:19 +0200140 char **s = data;
Lennart Poettering932921b2010-04-24 03:11:01 +0200141 char *k;
142
143 assert(filename);
144 assert(lvalue);
145 assert(rvalue);
Lennart Poetteringf2d37692010-06-18 23:25:19 +0200146 assert(s);
147 assert(u);
Lennart Poettering932921b2010-04-24 03:11:01 +0200148
149 if (!(k = unit_full_printf(u, rvalue)))
150 return -ENOMEM;
151
Lennart Poetteringf2d37692010-06-18 23:25:19 +0200152 free(*s);
Lennart Poettering932921b2010-04-24 03:11:01 +0200153 if (*k)
Lennart Poetteringf2d37692010-06-18 23:25:19 +0200154 *s = k;
Lennart Poettering932921b2010-04-24 03:11:01 +0200155 else {
156 free(k);
Lennart Poetteringf2d37692010-06-18 23:25:19 +0200157 *s = NULL;
Lennart Poettering932921b2010-04-24 03:11:01 +0200158 }
159
160 return 0;
161}
162
Lennart Poettering42f4e3c2010-01-19 02:56:37 +0100163static int config_parse_listen(
164 const char *filename,
165 unsigned line,
166 const char *section,
167 const char *lvalue,
168 const char *rvalue,
169 void *data,
170 void *userdata) {
171
Lennart Poettering16354ef2010-01-20 19:19:53 +0100172 int r;
Lennart Poettering542563b2010-01-23 03:35:54 +0100173 SocketPort *p;
174 Socket *s;
Lennart Poettering16354ef2010-01-20 19:19:53 +0100175
Lennart Poettering42f4e3c2010-01-19 02:56:37 +0100176 assert(filename);
177 assert(lvalue);
178 assert(rvalue);
179 assert(data);
180
Lennart Poettering542563b2010-01-23 03:35:54 +0100181 s = (Socket*) data;
182
183 if (!(p = new0(SocketPort, 1)))
184 return -ENOMEM;
185
186 if (streq(lvalue, "ListenFIFO")) {
187 p->type = SOCKET_FIFO;
188
189 if (!(p->path = strdup(rvalue))) {
190 free(p);
191 return -ENOMEM;
192 }
Lennart Poettering01f78472010-05-24 05:25:33 +0200193
194 path_kill_slashes(p->path);
Lennart Poettering542563b2010-01-23 03:35:54 +0100195 } else {
196 p->type = SOCKET_SOCKET;
197
198 if ((r = socket_address_parse(&p->address, rvalue)) < 0) {
199 log_error("[%s:%u] Failed to parse address value: %s", filename, line, rvalue);
200 free(p);
201 return r;
202 }
203
204 if (streq(lvalue, "ListenStream"))
205 p->address.type = SOCK_STREAM;
206 else if (streq(lvalue, "ListenDatagram"))
207 p->address.type = SOCK_DGRAM;
208 else {
209 assert(streq(lvalue, "ListenSequentialPacket"));
210 p->address.type = SOCK_SEQPACKET;
211 }
212
213 if (socket_address_family(&p->address) != AF_LOCAL && p->address.type == SOCK_SEQPACKET) {
214 free(p);
215 return -EPROTONOSUPPORT;
216 }
Lennart Poettering16354ef2010-01-20 19:19:53 +0100217 }
218
Lennart Poettering542563b2010-01-23 03:35:54 +0100219 p->fd = -1;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100220 LIST_PREPEND(SocketPort, port, s->ports, p);
Lennart Poettering542563b2010-01-23 03:35:54 +0100221
Lennart Poettering16354ef2010-01-20 19:19:53 +0100222 return 0;
Lennart Poettering42f4e3c2010-01-19 02:56:37 +0100223}
224
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100225static int config_parse_socket_bind(
Lennart Poettering42f4e3c2010-01-19 02:56:37 +0100226 const char *filename,
227 unsigned line,
228 const char *section,
229 const char *lvalue,
230 const char *rvalue,
231 void *data,
232 void *userdata) {
233
Lennart Poettering542563b2010-01-23 03:35:54 +0100234 Socket *s;
Lennart Poetteringc0120d92010-05-21 23:41:25 +0200235 SocketAddressBindIPv6Only b;
Lennart Poettering42f4e3c2010-01-19 02:56:37 +0100236
237 assert(filename);
238 assert(lvalue);
239 assert(rvalue);
240 assert(data);
241
Lennart Poettering542563b2010-01-23 03:35:54 +0100242 s = (Socket*) data;
243
Lennart Poetteringc0120d92010-05-21 23:41:25 +0200244 if ((b = socket_address_bind_ipv6_only_from_string(rvalue)) < 0) {
245 int r;
Lennart Poettering42f4e3c2010-01-19 02:56:37 +0100246
Lennart Poetteringc0120d92010-05-21 23:41:25 +0200247 if ((r = parse_boolean(rvalue)) < 0) {
248 log_error("[%s:%u] Failed to parse bind IPv6 only value: %s", filename, line, rvalue);
249 return -EBADMSG;
250 }
251
252 s->bind_ipv6_only = r ? SOCKET_ADDRESS_IPV6_ONLY : SOCKET_ADDRESS_BOTH;
253 } else
254 s->bind_ipv6_only = b;
Lennart Poettering542563b2010-01-23 03:35:54 +0100255
Lennart Poettering42f4e3c2010-01-19 02:56:37 +0100256 return 0;
257}
258
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100259static int config_parse_nice(
260 const char *filename,
261 unsigned line,
262 const char *section,
263 const char *lvalue,
264 const char *rvalue,
265 void *data,
266 void *userdata) {
267
Lennart Poetteringfb33a392010-01-28 02:53:56 +0100268 ExecContext *c = data;
269 int priority, r;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100270
271 assert(filename);
272 assert(lvalue);
273 assert(rvalue);
274 assert(data);
275
276 if ((r = safe_atoi(rvalue, &priority)) < 0) {
277 log_error("[%s:%u] Failed to parse nice priority: %s", filename, line, rvalue);
278 return r;
279 }
280
281 if (priority < PRIO_MIN || priority >= PRIO_MAX) {
282 log_error("[%s:%u] Nice priority out of range: %s", filename, line, rvalue);
283 return -ERANGE;
284 }
285
Lennart Poetteringfb33a392010-01-28 02:53:56 +0100286 c->nice = priority;
287 c->nice_set = false;
288
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100289 return 0;
290}
291
292static int config_parse_oom_adjust(
293 const char *filename,
294 unsigned line,
295 const char *section,
296 const char *lvalue,
297 const char *rvalue,
298 void *data,
299 void *userdata) {
300
Lennart Poetteringfb33a392010-01-28 02:53:56 +0100301 ExecContext *c = data;
302 int oa, r;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100303
304 assert(filename);
305 assert(lvalue);
306 assert(rvalue);
307 assert(data);
308
309 if ((r = safe_atoi(rvalue, &oa)) < 0) {
310 log_error("[%s:%u] Failed to parse OOM adjust value: %s", filename, line, rvalue);
311 return r;
312 }
313
314 if (oa < OOM_DISABLE || oa > OOM_ADJUST_MAX) {
315 log_error("[%s:%u] OOM adjust value out of range: %s", filename, line, rvalue);
316 return -ERANGE;
317 }
318
Lennart Poetteringfb33a392010-01-28 02:53:56 +0100319 c->oom_adjust = oa;
320 c->oom_adjust_set = true;
321
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100322 return 0;
323}
324
Lennart Poetteringb5a06992010-02-12 02:02:14 +0100325static int config_parse_mode(
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100326 const char *filename,
327 unsigned line,
328 const char *section,
329 const char *lvalue,
330 const char *rvalue,
331 void *data,
332 void *userdata) {
333
334 mode_t *m = data;
335 long l;
336 char *x = NULL;
337
338 assert(filename);
339 assert(lvalue);
340 assert(rvalue);
341 assert(data);
342
343 errno = 0;
344 l = strtol(rvalue, &x, 8);
345 if (!x || *x || errno) {
Lennart Poetteringb5a06992010-02-12 02:02:14 +0100346 log_error("[%s:%u] Failed to parse mode value: %s", filename, line, rvalue);
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100347 return errno ? -errno : -EINVAL;
348 }
349
Lennart Poetteringb5a06992010-02-12 02:02:14 +0100350 if (l < 0000 || l > 07777) {
351 log_error("[%s:%u] mode value out of range: %s", filename, line, rvalue);
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100352 return -ERANGE;
353 }
354
355 *m = (mode_t) l;
356 return 0;
357}
358
359static int config_parse_exec(
360 const char *filename,
361 unsigned line,
362 const char *section,
363 const char *lvalue,
364 const char *rvalue,
365 void *data,
366 void *userdata) {
367
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200368 ExecCommand **e = data, *nce;
369 char *path, **n;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100370 unsigned k;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100371
372 assert(filename);
373 assert(lvalue);
374 assert(rvalue);
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200375 assert(e);
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100376
Lennart Poettering6c666e22010-05-19 21:51:53 +0200377 /* We accept an absolute path as first argument, or
378 * alternatively an absolute prefixed with @ to allow
379 * overriding of argv[0]. */
380
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200381 for (;;) {
382 char *w;
383 size_t l;
384 char *state;
385 bool honour_argv0, write_to_path;
Lennart Poettering6c666e22010-05-19 21:51:53 +0200386
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200387 path = NULL;
388 nce = NULL;
389 n = NULL;
Lennart Poettering6c666e22010-05-19 21:51:53 +0200390
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200391 rvalue += strspn(rvalue, WHITESPACE);
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100392
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200393 if (rvalue[0] == 0)
394 break;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100395
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200396 honour_argv0 = rvalue[0] == '@';
397
398 if (rvalue[honour_argv0 ? 1 : 0] != '/') {
399 log_error("[%s:%u] Invalid executable path in command line: %s", filename, line, rvalue);
400 return -EINVAL;
Lennart Poettering6c666e22010-05-19 21:51:53 +0200401 }
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100402
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200403 k = 0;
404 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
405 if (strncmp(w, ";", l) == 0)
406 break;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100407
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200408 k++;
409 }
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100410
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200411 if (!(n = new(char*, k + (honour_argv0 ? 0 : 1))))
412 return -ENOMEM;
413
414 k = 0;
415 write_to_path = honour_argv0;
416 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
417 if (strncmp(w, ";", l) == 0)
418 break;
419
420 if (write_to_path) {
421 if (!(path = cunescape_length(w+1, l-1)))
422 goto fail;
423 write_to_path = false;
424 } else {
425 if (!(n[k++] = cunescape_length(w, l)))
426 goto fail;
427 }
428 }
429
430 n[k] = NULL;
431
432 if (!n[0]) {
433 log_error("[%s:%u] Invalid command line: %s", filename, line, rvalue);
434 strv_free(n);
435 return -EINVAL;
436 }
437
438 if (!path)
439 if (!(path = strdup(n[0])))
440 goto fail;
441
442 assert(path_is_absolute(path));
443
444 if (!(nce = new0(ExecCommand, 1)))
Lennart Poettering6c666e22010-05-19 21:51:53 +0200445 goto fail;
446
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200447 nce->argv = n;
448 nce->path = path;
Lennart Poettering6c666e22010-05-19 21:51:53 +0200449
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200450 path_kill_slashes(nce->path);
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100451
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200452 exec_command_append_list(e, nce);
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100453
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200454 rvalue = state;
455 }
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100456
457 return 0;
458
459fail:
Lennart Poettering6c666e22010-05-19 21:51:53 +0200460 n[k] = NULL;
461 strv_free(n);
462 free(path);
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100463 free(nce);
464
465 return -ENOMEM;
466}
467
468static int config_parse_usec(
469 const char *filename,
470 unsigned line,
471 const char *section,
472 const char *lvalue,
473 const char *rvalue,
474 void *data,
475 void *userdata) {
476
477 usec_t *usec = data;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100478 int r;
479
480 assert(filename);
481 assert(lvalue);
482 assert(rvalue);
483 assert(data);
484
Lennart Poettering24a6e4a2010-04-23 20:26:59 +0200485 if ((r = parse_usec(rvalue, usec)) < 0) {
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100486 log_error("[%s:%u] Failed to parse time value: %s", filename, line, rvalue);
487 return r;
488 }
489
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100490 return 0;
491}
492
Lennart Poettering487393e2010-07-07 01:10:27 +0200493static DEFINE_CONFIG_PARSE_ENUM(config_parse_service_type, service_type, ServiceType, "Failed to parse service type");
494static DEFINE_CONFIG_PARSE_ENUM(config_parse_service_restart, service_restart, ServiceRestart, "Failed to parse service restart specifier");
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100495
Lennart Poettering47be8702010-02-03 14:21:48 +0100496static int config_parse_bindtodevice(
Lennart Poetteringacbb0222010-01-27 04:31:52 +0100497 const char *filename,
498 unsigned line,
499 const char *section,
500 const char *lvalue,
501 const char *rvalue,
502 void *data,
503 void *userdata) {
504
505 Socket *s = data;
506 char *n;
507
508 assert(filename);
509 assert(lvalue);
510 assert(rvalue);
511 assert(data);
512
513 if (rvalue[0] && !streq(rvalue, "*")) {
514 if (!(n = strdup(rvalue)))
515 return -ENOMEM;
516 } else
517 n = NULL;
518
519 free(s->bind_to_device);
520 s->bind_to_device = n;
521
522 return 0;
523}
524
Lennart Poettering487393e2010-07-07 01:10:27 +0200525static DEFINE_CONFIG_PARSE_ENUM(config_parse_output, exec_output, ExecOutput, "Failed to parse output specifier");
526static DEFINE_CONFIG_PARSE_ENUM(config_parse_input, exec_input, ExecInput, "Failed to parse input specifier");
Lennart Poettering071830f2010-01-28 02:06:20 +0100527
Lennart Poettering47be8702010-02-03 14:21:48 +0100528static int config_parse_facility(
Lennart Poettering071830f2010-01-28 02:06:20 +0100529 const char *filename,
530 unsigned line,
531 const char *section,
532 const char *lvalue,
533 const char *rvalue,
534 void *data,
535 void *userdata) {
536
Lennart Poettering071830f2010-01-28 02:06:20 +0100537
Lennart Poettering94f04342010-01-30 01:55:42 +0100538 int *o = data, x;
Lennart Poettering071830f2010-01-28 02:06:20 +0100539
540 assert(filename);
541 assert(lvalue);
542 assert(rvalue);
543 assert(data);
544
Lennart Poettering0d87eb42010-04-13 02:22:41 +0200545 if ((x = log_facility_from_string(rvalue)) < 0) {
546 log_error("[%s:%u] Failed to parse log facility: %s", filename, line, rvalue);
547 return -EBADMSG;
548 }
Lennart Poettering94f04342010-01-30 01:55:42 +0100549
550 *o = LOG_MAKEPRI(x, LOG_PRI(*o));
Lennart Poettering071830f2010-01-28 02:06:20 +0100551
552 return 0;
553}
554
Lennart Poettering47be8702010-02-03 14:21:48 +0100555static int config_parse_level(
Lennart Poettering071830f2010-01-28 02:06:20 +0100556 const char *filename,
557 unsigned line,
558 const char *section,
559 const char *lvalue,
560 const char *rvalue,
561 void *data,
562 void *userdata) {
563
Lennart Poettering071830f2010-01-28 02:06:20 +0100564
Lennart Poettering94f04342010-01-30 01:55:42 +0100565 int *o = data, x;
Lennart Poettering071830f2010-01-28 02:06:20 +0100566
567 assert(filename);
568 assert(lvalue);
569 assert(rvalue);
570 assert(data);
571
Lennart Poettering0d87eb42010-04-13 02:22:41 +0200572 if ((x = log_level_from_string(rvalue)) < 0) {
573 log_error("[%s:%u] Failed to parse log level: %s", filename, line, rvalue);
574 return -EBADMSG;
575 }
Lennart Poettering071830f2010-01-28 02:06:20 +0100576
Lennart Poettering94f04342010-01-30 01:55:42 +0100577 *o = LOG_MAKEPRI(LOG_FAC(*o), x);
Lennart Poettering071830f2010-01-28 02:06:20 +0100578 return 0;
579}
580
Lennart Poettering47be8702010-02-03 14:21:48 +0100581static int config_parse_io_class(
Lennart Poettering9eba9da2010-01-29 20:46:22 +0100582 const char *filename,
583 unsigned line,
584 const char *section,
585 const char *lvalue,
586 const char *rvalue,
587 void *data,
588 void *userdata) {
589
Lennart Poettering9eba9da2010-01-29 20:46:22 +0100590 ExecContext *c = data;
Lennart Poettering94f04342010-01-30 01:55:42 +0100591 int x;
Lennart Poettering9eba9da2010-01-29 20:46:22 +0100592
593 assert(filename);
594 assert(lvalue);
595 assert(rvalue);
596 assert(data);
597
Lennart Poettering0d87eb42010-04-13 02:22:41 +0200598 if ((x = ioprio_class_from_string(rvalue)) < 0) {
599 log_error("[%s:%u] Failed to parse IO scheduling class: %s", filename, line, rvalue);
600 return -EBADMSG;
601 }
Lennart Poettering9eba9da2010-01-29 20:46:22 +0100602
Lennart Poettering94f04342010-01-30 01:55:42 +0100603 c->ioprio = IOPRIO_PRIO_VALUE(x, IOPRIO_PRIO_DATA(c->ioprio));
Lennart Poettering9eba9da2010-01-29 20:46:22 +0100604 c->ioprio_set = true;
605
606 return 0;
607}
608
Lennart Poettering47be8702010-02-03 14:21:48 +0100609static int config_parse_io_priority(
Lennart Poettering9eba9da2010-01-29 20:46:22 +0100610 const char *filename,
611 unsigned line,
612 const char *section,
613 const char *lvalue,
614 const char *rvalue,
615 void *data,
616 void *userdata) {
617
618 ExecContext *c = data;
619 int i;
620
621 assert(filename);
622 assert(lvalue);
623 assert(rvalue);
624 assert(data);
625
Lennart Poettering94f04342010-01-30 01:55:42 +0100626 if (safe_atoi(rvalue, &i) < 0 || i < 0 || i >= IOPRIO_BE_NR) {
Lennart Poettering9eba9da2010-01-29 20:46:22 +0100627 log_error("[%s:%u] Failed to parse io priority: %s", filename, line, rvalue);
628 return -EBADMSG;
629 }
630
Lennart Poettering94f04342010-01-30 01:55:42 +0100631 c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_PRIO_CLASS(c->ioprio), i);
Lennart Poettering9eba9da2010-01-29 20:46:22 +0100632 c->ioprio_set = true;
633
634 return 0;
635}
636
Lennart Poettering47be8702010-02-03 14:21:48 +0100637static int config_parse_cpu_sched_policy(
Lennart Poettering94f04342010-01-30 01:55:42 +0100638 const char *filename,
639 unsigned line,
640 const char *section,
641 const char *lvalue,
642 const char *rvalue,
643 void *data,
644 void *userdata) {
645
646
647 ExecContext *c = data;
648 int x;
649
650 assert(filename);
651 assert(lvalue);
652 assert(rvalue);
653 assert(data);
654
Lennart Poettering0d87eb42010-04-13 02:22:41 +0200655 if ((x = sched_policy_from_string(rvalue)) < 0) {
656 log_error("[%s:%u] Failed to parse CPU scheduling policy: %s", filename, line, rvalue);
657 return -EBADMSG;
658 }
Lennart Poettering94f04342010-01-30 01:55:42 +0100659
660 c->cpu_sched_policy = x;
661 c->cpu_sched_set = true;
662
663 return 0;
664}
665
Lennart Poettering47be8702010-02-03 14:21:48 +0100666static int config_parse_cpu_sched_prio(
Lennart Poettering94f04342010-01-30 01:55:42 +0100667 const char *filename,
668 unsigned line,
669 const char *section,
670 const char *lvalue,
671 const char *rvalue,
672 void *data,
673 void *userdata) {
674
675 ExecContext *c = data;
676 int i;
677
678 assert(filename);
679 assert(lvalue);
680 assert(rvalue);
681 assert(data);
682
683 /* On Linux RR/FIFO have the same range */
684 if (safe_atoi(rvalue, &i) < 0 || i < sched_get_priority_min(SCHED_RR) || i > sched_get_priority_max(SCHED_RR)) {
685 log_error("[%s:%u] Failed to parse CPU scheduling priority: %s", filename, line, rvalue);
686 return -EBADMSG;
687 }
688
689 c->cpu_sched_priority = i;
690 c->cpu_sched_set = true;
691
692 return 0;
693}
694
Lennart Poettering47be8702010-02-03 14:21:48 +0100695static int config_parse_cpu_affinity(
Lennart Poettering94f04342010-01-30 01:55:42 +0100696 const char *filename,
697 unsigned line,
698 const char *section,
699 const char *lvalue,
700 const char *rvalue,
701 void *data,
702 void *userdata) {
703
704 ExecContext *c = data;
705 char *w;
706 size_t l;
707 char *state;
708
709 assert(filename);
710 assert(lvalue);
711 assert(rvalue);
712 assert(data);
713
Lennart Poetteringf60f22d2010-07-07 20:58:41 +0200714 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
Lennart Poettering94f04342010-01-30 01:55:42 +0100715 char *t;
716 int r;
717 unsigned cpu;
718
719 if (!(t = strndup(w, l)))
720 return -ENOMEM;
721
Lennart Poettering487393e2010-07-07 01:10:27 +0200722 r = safe_atou(t, &cpu);
723 free(t);
724
Lennart Poettering82c121a2010-07-04 16:44:58 +0200725 if (!(c->cpuset))
726 if (!(c->cpuset = cpu_set_malloc(&c->cpuset_ncpus)))
727 return -ENOMEM;
728
Lennart Poettering82c121a2010-07-04 16:44:58 +0200729 if (r < 0 || cpu >= c->cpuset_ncpus) {
Lennart Poettering94f04342010-01-30 01:55:42 +0100730 log_error("[%s:%u] Failed to parse CPU affinity: %s", filename, line, rvalue);
731 return -EBADMSG;
732 }
733
Lennart Poettering82c121a2010-07-04 16:44:58 +0200734 CPU_SET_S(cpu, CPU_ALLOC_SIZE(c->cpuset_ncpus), c->cpuset);
Lennart Poettering94f04342010-01-30 01:55:42 +0100735 }
736
Lennart Poettering94f04342010-01-30 01:55:42 +0100737 return 0;
738}
739
Lennart Poettering47be8702010-02-03 14:21:48 +0100740static int config_parse_capabilities(
Lennart Poettering94f04342010-01-30 01:55:42 +0100741 const char *filename,
742 unsigned line,
743 const char *section,
744 const char *lvalue,
745 const char *rvalue,
746 void *data,
747 void *userdata) {
748
749 ExecContext *c = data;
750 cap_t cap;
751
752 assert(filename);
753 assert(lvalue);
754 assert(rvalue);
755 assert(data);
756
757 if (!(cap = cap_from_text(rvalue))) {
758 if (errno == ENOMEM)
759 return -ENOMEM;
760
761 log_error("[%s:%u] Failed to parse capabilities: %s", filename, line, rvalue);
762 return -EBADMSG;
763 }
764
765 if (c->capabilities)
766 cap_free(c->capabilities);
767 c->capabilities = cap;
768
769 return 0;
770}
771
Lennart Poettering47be8702010-02-03 14:21:48 +0100772static int config_parse_secure_bits(
Lennart Poettering94f04342010-01-30 01:55:42 +0100773 const char *filename,
774 unsigned line,
775 const char *section,
776 const char *lvalue,
777 const char *rvalue,
778 void *data,
779 void *userdata) {
780
781 ExecContext *c = data;
782 char *w;
783 size_t l;
784 char *state;
785
786 assert(filename);
787 assert(lvalue);
788 assert(rvalue);
789 assert(data);
790
Lennart Poetteringf60f22d2010-07-07 20:58:41 +0200791 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
Lennart Poettering94f04342010-01-30 01:55:42 +0100792 if (first_word(w, "keep-caps"))
793 c->secure_bits |= SECURE_KEEP_CAPS;
794 else if (first_word(w, "keep-caps-locked"))
795 c->secure_bits |= SECURE_KEEP_CAPS_LOCKED;
796 else if (first_word(w, "no-setuid-fixup"))
797 c->secure_bits |= SECURE_NO_SETUID_FIXUP;
798 else if (first_word(w, "no-setuid-fixup-locked"))
799 c->secure_bits |= SECURE_NO_SETUID_FIXUP_LOCKED;
800 else if (first_word(w, "noroot"))
801 c->secure_bits |= SECURE_NOROOT;
802 else if (first_word(w, "noroot-locked"))
803 c->secure_bits |= SECURE_NOROOT_LOCKED;
804 else {
805 log_error("[%s:%u] Failed to parse secure bits: %s", filename, line, rvalue);
806 return -EBADMSG;
807 }
808 }
809
810 return 0;
811}
812
Lennart Poettering47be8702010-02-03 14:21:48 +0100813static int config_parse_bounding_set(
Lennart Poettering94f04342010-01-30 01:55:42 +0100814 const char *filename,
815 unsigned line,
816 const char *section,
817 const char *lvalue,
818 const char *rvalue,
819 void *data,
820 void *userdata) {
821
822 ExecContext *c = data;
823 char *w;
824 size_t l;
825 char *state;
826
827 assert(filename);
828 assert(lvalue);
829 assert(rvalue);
830 assert(data);
831
Lennart Poetteringf60f22d2010-07-07 20:58:41 +0200832 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
Lennart Poettering94f04342010-01-30 01:55:42 +0100833 char *t;
834 int r;
835 cap_value_t cap;
836
837 if (!(t = strndup(w, l)))
838 return -ENOMEM;
839
840 r = cap_from_name(t, &cap);
841 free(t);
842
843 if (r < 0) {
844 log_error("[%s:%u] Failed to parse capability bounding set: %s", filename, line, rvalue);
845 return -EBADMSG;
846 }
847
848 c->capability_bounding_set_drop |= 1 << cap;
849 }
850
851 return 0;
852}
853
Lennart Poettering03fae012010-07-04 21:12:10 +0200854static int config_parse_timer_slack_nsec(
Lennart Poettering94f04342010-01-30 01:55:42 +0100855 const char *filename,
856 unsigned line,
857 const char *section,
858 const char *lvalue,
859 const char *rvalue,
860 void *data,
861 void *userdata) {
862
863 ExecContext *c = data;
864 unsigned long u;
865 int r;
866
867 assert(filename);
868 assert(lvalue);
869 assert(rvalue);
870 assert(data);
871
872 if ((r = safe_atolu(rvalue, &u)) < 0) {
873 log_error("[%s:%u] Failed to parse time slack value: %s", filename, line, rvalue);
874 return r;
875 }
876
Lennart Poettering03fae012010-07-04 21:12:10 +0200877 c->timer_slack_nsec = u;
Lennart Poettering94f04342010-01-30 01:55:42 +0100878
879 return 0;
880}
881
882static int config_parse_limit(
883 const char *filename,
884 unsigned line,
885 const char *section,
886 const char *lvalue,
887 const char *rvalue,
888 void *data,
889 void *userdata) {
890
891 struct rlimit **rl = data;
892 unsigned long long u;
893 int r;
894
895 assert(filename);
896 assert(lvalue);
897 assert(rvalue);
898 assert(data);
899
900 if ((r = safe_atollu(rvalue, &u)) < 0) {
901 log_error("[%s:%u] Failed to parse resource value: %s", filename, line, rvalue);
902 return r;
903 }
904
905 if (!*rl)
906 if (!(*rl = new(struct rlimit, 1)))
907 return -ENOMEM;
908
909 (*rl)->rlim_cur = (*rl)->rlim_max = (rlim_t) u;
910 return 0;
911}
912
Lennart Poettering8e274522010-03-31 16:29:55 +0200913static int config_parse_cgroup(
914 const char *filename,
915 unsigned line,
916 const char *section,
917 const char *lvalue,
918 const char *rvalue,
919 void *data,
920 void *userdata) {
921
922 Unit *u = userdata;
923 char *w;
924 size_t l;
925 char *state;
926
Lennart Poetteringf60f22d2010-07-07 20:58:41 +0200927 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
Lennart Poettering8e274522010-03-31 16:29:55 +0200928 char *t;
929 int r;
930
Lennart Poetteringf60f22d2010-07-07 20:58:41 +0200931 if (!(t = cunescape_length(w, l)))
Lennart Poettering8e274522010-03-31 16:29:55 +0200932 return -ENOMEM;
933
934 r = unit_add_cgroup_from_text(u, t);
935 free(t);
936
937 if (r < 0)
938 return r;
939 }
940
941 return 0;
942}
943
Lennart Poetteringa9a1e002010-04-06 02:40:10 +0200944static int config_parse_sysv_priority(
945 const char *filename,
946 unsigned line,
947 const char *section,
948 const char *lvalue,
949 const char *rvalue,
950 void *data,
951 void *userdata) {
952
953 int *priority = data;
954 int r, i;
955
956 assert(filename);
957 assert(lvalue);
958 assert(rvalue);
959 assert(data);
960
961 if ((r = safe_atoi(rvalue, &i)) < 0 || i < 0) {
962 log_error("[%s:%u] Failed to parse SysV start priority: %s", filename, line, rvalue);
963 return r;
964 }
965
966 *priority = (int) i;
967 return 0;
968}
969
Lennart Poettering487393e2010-07-07 01:10:27 +0200970static DEFINE_CONFIG_PARSE_ENUM(config_parse_kill_mode, kill_mode, KillMode, "Failed to parse kill mode");
Lennart Poettering50159e62010-04-08 00:52:14 +0200971
Lennart Poettering2e22afe2010-07-10 04:49:37 +0200972static int config_parse_kill_signal(
973 const char *filename,
974 unsigned line,
975 const char *section,
976 const char *lvalue,
977 const char *rvalue,
978 void *data,
979 void *userdata) {
980
981 int *sig = data;
982 int r;
983
984 assert(filename);
985 assert(lvalue);
986 assert(rvalue);
987 assert(sig);
988
989 if ((r = signal_from_string(rvalue)) <= 0)
990 if (startswith(rvalue, "SIG"))
991 r = signal_from_string(rvalue+3);
992
993 if (r <= 0) {
994 log_error("[%s:%u] Failed to parse kill signal: %s", filename, line, rvalue);
995 return -EINVAL;
996 }
997
998 *sig = r;
999 return 0;
1000}
1001
Lennart Poettering15ae4222010-04-21 22:15:06 +02001002static int config_parse_mount_flags(
1003 const char *filename,
1004 unsigned line,
1005 const char *section,
1006 const char *lvalue,
1007 const char *rvalue,
1008 void *data,
1009 void *userdata) {
1010
1011 ExecContext *c = data;
1012 char *w;
1013 size_t l;
1014 char *state;
1015 unsigned long flags = 0;
1016
1017 assert(filename);
1018 assert(lvalue);
1019 assert(rvalue);
1020 assert(data);
1021
Lennart Poetteringf60f22d2010-07-07 20:58:41 +02001022 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
Lennart Poettering15ae4222010-04-21 22:15:06 +02001023 if (strncmp(w, "shared", l) == 0)
1024 flags |= MS_SHARED;
1025 else if (strncmp(w, "slave", l) == 0)
1026 flags |= MS_SLAVE;
1027 else if (strncmp(w, "private", l) == 0)
1028 flags |= MS_PRIVATE;
1029 else {
1030 log_error("[%s:%u] Failed to parse mount flags: %s", filename, line, rvalue);
1031 return -EINVAL;
1032 }
1033 }
1034
1035 c->mount_flags = flags;
1036 return 0;
1037}
1038
Lennart Poettering871d7de2010-05-24 01:45:54 +02001039static int config_parse_timer(
1040 const char *filename,
1041 unsigned line,
1042 const char *section,
1043 const char *lvalue,
1044 const char *rvalue,
1045 void *data,
1046 void *userdata) {
1047
1048 Timer *t = data;
1049 usec_t u;
1050 int r;
1051 TimerValue *v;
1052 TimerBase b;
1053
1054 assert(filename);
1055 assert(lvalue);
1056 assert(rvalue);
1057 assert(data);
1058
1059 if ((b = timer_base_from_string(lvalue)) < 0) {
1060 log_error("[%s:%u] Failed to parse timer base: %s", filename, line, lvalue);
1061 return -EINVAL;
1062 }
1063
1064 if ((r = parse_usec(rvalue, &u)) < 0) {
1065 log_error("[%s:%u] Failed to parse timer value: %s", filename, line, rvalue);
1066 return r;
1067 }
1068
1069 if (!(v = new0(TimerValue, 1)))
1070 return -ENOMEM;
1071
1072 v->base = b;
1073 v->value = u;
1074
1075 LIST_PREPEND(TimerValue, value, t->values, v);
1076
1077 return 0;
1078}
1079
1080static int config_parse_timer_unit(
1081 const char *filename,
1082 unsigned line,
1083 const char *section,
1084 const char *lvalue,
1085 const char *rvalue,
1086 void *data,
1087 void *userdata) {
1088
1089 Timer *t = data;
1090 int r;
Lennart Poettering398ef8b2010-07-08 02:43:18 +02001091 DBusError error;
1092
1093 assert(filename);
1094 assert(lvalue);
1095 assert(rvalue);
1096 assert(data);
1097
1098 dbus_error_init(&error);
Lennart Poettering871d7de2010-05-24 01:45:54 +02001099
1100 if (endswith(rvalue, ".timer")) {
1101 log_error("[%s:%u] Unit cannot be of type timer: %s", filename, line, rvalue);
1102 return -EINVAL;
1103 }
1104
Lennart Poettering398ef8b2010-07-08 02:43:18 +02001105 if ((r = manager_load_unit(t->meta.manager, rvalue, NULL, NULL, &t->unit)) < 0) {
1106 log_error("[%s:%u] Failed to load unit %s: %s", filename, line, rvalue, bus_error(&error, r));
1107 dbus_error_free(&error);
Lennart Poettering871d7de2010-05-24 01:45:54 +02001108 return r;
1109 }
1110
1111 return 0;
1112}
1113
Lennart Poettering01f78472010-05-24 05:25:33 +02001114static int config_parse_path_spec(
1115 const char *filename,
1116 unsigned line,
1117 const char *section,
1118 const char *lvalue,
1119 const char *rvalue,
1120 void *data,
1121 void *userdata) {
1122
1123 Path *p = data;
1124 PathSpec *s;
1125 PathType b;
1126
1127 assert(filename);
1128 assert(lvalue);
1129 assert(rvalue);
1130 assert(data);
1131
1132 if ((b = path_type_from_string(lvalue)) < 0) {
1133 log_error("[%s:%u] Failed to parse path type: %s", filename, line, lvalue);
1134 return -EINVAL;
1135 }
1136
1137 if (!path_is_absolute(rvalue)) {
1138 log_error("[%s:%u] Path is not absolute: %s", filename, line, rvalue);
1139 return -EINVAL;
1140 }
1141
1142 if (!(s = new0(PathSpec, 1)))
1143 return -ENOMEM;
1144
1145 if (!(s->path = strdup(rvalue))) {
1146 free(s);
1147 return -ENOMEM;
1148 }
1149
1150 path_kill_slashes(s->path);
1151
1152 s->type = b;
1153 s->inotify_fd = -1;
1154
1155 LIST_PREPEND(PathSpec, spec, p->specs, s);
1156
1157 return 0;
1158}
1159
1160static int config_parse_path_unit(
1161 const char *filename,
1162 unsigned line,
1163 const char *section,
1164 const char *lvalue,
1165 const char *rvalue,
1166 void *data,
1167 void *userdata) {
1168
1169 Path *t = data;
1170 int r;
Lennart Poettering398ef8b2010-07-08 02:43:18 +02001171 DBusError error;
1172
1173 assert(filename);
1174 assert(lvalue);
1175 assert(rvalue);
1176 assert(data);
1177
1178 dbus_error_init(&error);
Lennart Poettering01f78472010-05-24 05:25:33 +02001179
1180 if (endswith(rvalue, ".path")) {
1181 log_error("[%s:%u] Unit cannot be of type path: %s", filename, line, rvalue);
1182 return -EINVAL;
1183 }
1184
Lennart Poettering398ef8b2010-07-08 02:43:18 +02001185 if ((r = manager_load_unit(t->meta.manager, rvalue, NULL, &error, &t->unit)) < 0) {
1186 log_error("[%s:%u] Failed to load unit %s: %s", filename, line, rvalue, bus_error(&error, r));
1187 dbus_error_free(&error);
Lennart Poettering01f78472010-05-24 05:25:33 +02001188 return r;
1189 }
1190
1191 return 0;
1192}
1193
Lennart Poetteringddb26e12010-06-18 06:06:24 +02001194static int config_parse_env_file(
1195 const char *filename,
1196 unsigned line,
1197 const char *section,
1198 const char *lvalue,
1199 const char *rvalue,
1200 void *data,
1201 void *userdata) {
1202
1203 FILE *f;
1204 int r;
1205 char ***env = data;
1206
1207 assert(filename);
1208 assert(lvalue);
1209 assert(rvalue);
1210 assert(data);
1211
1212 if (!(f = fopen(rvalue, "re"))) {
1213 log_error("[%s:%u] Failed to open environment file '%s': %m", filename, line, rvalue);
1214 return -errno;
1215 }
1216
1217 while (!feof(f)) {
1218 char l[LINE_MAX], *p;
1219 char **t;
1220
1221 if (!fgets(l, sizeof(l), f)) {
1222 if (feof(f))
1223 break;
1224
1225 r = -errno;
1226 log_error("[%s:%u] Failed to read environment file '%s': %m", filename, line, rvalue);
1227 goto finish;
1228 }
1229
1230 p = strstrip(l);
1231
1232 if (!*p)
1233 continue;
1234
1235 if (strchr(COMMENTS, *p))
1236 continue;
1237
1238 t = strv_env_set(*env, p);
1239 strv_free(*env);
1240 *env = t;
1241 }
1242
1243 r = 0;
1244
1245finish:
1246 if (f)
1247 fclose(f);
1248
1249 return r;
1250}
1251
Lennart Poettering4fd59482010-07-01 00:29:17 +02001252static int config_parse_ip_tos(
1253 const char *filename,
1254 unsigned line,
1255 const char *section,
1256 const char *lvalue,
1257 const char *rvalue,
1258 void *data,
1259 void *userdata) {
1260
1261 int *ip_tos = data, x;
1262 int r;
1263
1264 assert(filename);
1265 assert(lvalue);
1266 assert(rvalue);
1267 assert(data);
1268
1269 if ((x = ip_tos_from_string(rvalue)) < 0)
1270 if ((r = safe_atoi(rvalue, &x)) < 0) {
1271 log_error("[%s:%u] Failed to parse IP TOS value: %s", filename, line, rvalue);
1272 return r;
1273 }
1274
1275 *ip_tos = x;
1276 return 0;
1277}
1278
Lennart Poettering487393e2010-07-07 01:10:27 +02001279static DEFINE_CONFIG_PARSE_ENUM(config_parse_notify_access, notify_access, NotifyAccess, "Failed to parse notify access specifier");
Lennart Poetteringc952c6e2010-06-18 23:12:48 +02001280
Lennart Poettering071830f2010-01-28 02:06:20 +01001281#define FOLLOW_MAX 8
Lennart Poettering87f0e412010-01-26 21:39:06 +01001282
Lennart Poettering9e2f7c12010-04-15 03:11:11 +02001283static int open_follow(char **filename, FILE **_f, Set *names, char **_final) {
Lennart Poettering0301abf2010-01-27 00:15:56 +01001284 unsigned c = 0;
Lennart Poettering87f0e412010-01-26 21:39:06 +01001285 int fd, r;
1286 FILE *f;
Lennart Poettering0301abf2010-01-27 00:15:56 +01001287 char *id = NULL;
Lennart Poettering87f0e412010-01-26 21:39:06 +01001288
1289 assert(filename);
1290 assert(*filename);
1291 assert(_f);
1292 assert(names);
1293
Lennart Poettering0301abf2010-01-27 00:15:56 +01001294 /* This will update the filename pointer if the loaded file is
1295 * reached by a symlink. The old string will be freed. */
Lennart Poettering87f0e412010-01-26 21:39:06 +01001296
Lennart Poettering0301abf2010-01-27 00:15:56 +01001297 for (;;) {
Lennart Poettering2c7108c2010-06-16 01:56:00 +02001298 char *target, *name;
Lennart Poettering87f0e412010-01-26 21:39:06 +01001299
Lennart Poettering0301abf2010-01-27 00:15:56 +01001300 if (c++ >= FOLLOW_MAX)
1301 return -ELOOP;
1302
Lennart Poetteringb08d03f2010-01-29 02:07:41 +01001303 path_kill_slashes(*filename);
1304
Lennart Poettering87f0e412010-01-26 21:39:06 +01001305 /* Add the file name we are currently looking at to
1306 * the names of this unit */
Lennart Poettering0301abf2010-01-27 00:15:56 +01001307 name = file_name_from_path(*filename);
1308 if (!(id = set_get(names, name))) {
Lennart Poettering87f0e412010-01-26 21:39:06 +01001309
Lennart Poettering0301abf2010-01-27 00:15:56 +01001310 if (!(id = strdup(name)))
1311 return -ENOMEM;
1312
1313 if ((r = set_put(names, id)) < 0) {
1314 free(id);
1315 return r;
Lennart Poettering87f0e412010-01-26 21:39:06 +01001316 }
Lennart Poettering87f0e412010-01-26 21:39:06 +01001317 }
1318
Lennart Poettering0301abf2010-01-27 00:15:56 +01001319 /* Try to open the file name, but don't if its a symlink */
1320 if ((fd = open(*filename, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW)) >= 0)
Lennart Poettering87f0e412010-01-26 21:39:06 +01001321 break;
1322
Lennart Poettering0301abf2010-01-27 00:15:56 +01001323 if (errno != ELOOP)
1324 return -errno;
Lennart Poettering87f0e412010-01-26 21:39:06 +01001325
Lennart Poettering0301abf2010-01-27 00:15:56 +01001326 /* Hmm, so this is a symlink. Let's read the name, and follow it manually */
Lennart Poettering2c7108c2010-06-16 01:56:00 +02001327 if ((r = readlink_and_make_absolute(*filename, &target)) < 0)
Lennart Poettering0301abf2010-01-27 00:15:56 +01001328 return r;
1329
Lennart Poettering0301abf2010-01-27 00:15:56 +01001330 free(*filename);
Lennart Poettering2c7108c2010-06-16 01:56:00 +02001331 *filename = target;
Lennart Poettering87f0e412010-01-26 21:39:06 +01001332 }
1333
1334 if (!(f = fdopen(fd, "r"))) {
1335 r = -errno;
Lennart Poettering9e2f7c12010-04-15 03:11:11 +02001336 close_nointr_nofail(fd);
Lennart Poettering0301abf2010-01-27 00:15:56 +01001337 return r;
Lennart Poettering87f0e412010-01-26 21:39:06 +01001338 }
1339
1340 *_f = f;
Lennart Poettering9e2f7c12010-04-15 03:11:11 +02001341 *_final = id;
Lennart Poettering0301abf2010-01-27 00:15:56 +01001342 return 0;
Lennart Poettering87f0e412010-01-26 21:39:06 +01001343}
1344
Lennart Poettering23a177e2010-04-06 02:43:58 +02001345static int merge_by_names(Unit **u, Set *names, const char *id) {
1346 char *k;
1347 int r;
1348
1349 assert(u);
1350 assert(*u);
1351 assert(names);
1352
1353 /* Let's try to add in all symlink names we found */
1354 while ((k = set_steal_first(names))) {
1355
1356 /* First try to merge in the other name into our
1357 * unit */
1358 if ((r = unit_merge_by_name(*u, k)) < 0) {
1359 Unit *other;
1360
1361 /* Hmm, we couldn't merge the other unit into
1362 * ours? Then let's try it the other way
1363 * round */
1364
1365 other = manager_get_unit((*u)->meta.manager, k);
1366 free(k);
1367
1368 if (other)
1369 if ((r = unit_merge(other, *u)) >= 0) {
1370 *u = other;
1371 return merge_by_names(u, names, NULL);
1372 }
1373
1374 return r;
1375 }
1376
1377 if (id == k)
1378 unit_choose_id(*u, id);
1379
1380 free(k);
1381 }
1382
1383 return 0;
1384}
1385
Lennart Poetteringe5373522010-04-10 17:53:17 +02001386static void dump_items(FILE *f, const ConfigItem *items) {
1387 const ConfigItem *i;
1388 const char *prev_section = NULL;
1389 bool not_first = false;
1390
1391 struct {
1392 ConfigParserCallback callback;
1393 const char *rvalue;
1394 } table[] = {
1395 { config_parse_int, "INTEGER" },
1396 { config_parse_unsigned, "UNSIGNED" },
1397 { config_parse_size, "SIZE" },
1398 { config_parse_bool, "BOOLEAN" },
1399 { config_parse_string, "STRING" },
1400 { config_parse_path, "PATH" },
1401 { config_parse_strv, "STRING [...]" },
1402 { config_parse_nice, "NICE" },
1403 { config_parse_oom_adjust, "OOMADJUST" },
1404 { config_parse_io_class, "IOCLASS" },
1405 { config_parse_io_priority, "IOPRIORITY" },
1406 { config_parse_cpu_sched_policy, "CPUSCHEDPOLICY" },
1407 { config_parse_cpu_sched_prio, "CPUSCHEDPRIO" },
1408 { config_parse_cpu_affinity, "CPUAFFINITY" },
1409 { config_parse_mode, "MODE" },
Lennart Poetteringddb26e12010-06-18 06:06:24 +02001410 { config_parse_env_file, "FILE" },
Lennart Poetteringe5373522010-04-10 17:53:17 +02001411 { config_parse_output, "OUTPUT" },
1412 { config_parse_input, "INPUT" },
1413 { config_parse_facility, "FACILITY" },
1414 { config_parse_level, "LEVEL" },
1415 { config_parse_capabilities, "CAPABILITIES" },
1416 { config_parse_secure_bits, "SECUREBITS" },
1417 { config_parse_bounding_set, "BOUNDINGSET" },
Lennart Poettering03fae012010-07-04 21:12:10 +02001418 { config_parse_timer_slack_nsec, "TIMERSLACK" },
Lennart Poetteringe5373522010-04-10 17:53:17 +02001419 { config_parse_limit, "LIMIT" },
1420 { config_parse_cgroup, "CGROUP [...]" },
1421 { config_parse_deps, "UNIT [...]" },
1422 { config_parse_names, "UNIT [...]" },
1423 { config_parse_exec, "PATH [ARGUMENT [...]]" },
1424 { config_parse_service_type, "SERVICETYPE" },
1425 { config_parse_service_restart, "SERVICERESTART" },
1426 { config_parse_sysv_priority, "SYSVPRIORITY" },
1427 { config_parse_kill_mode, "KILLMODE" },
Lennart Poettering2e22afe2010-07-10 04:49:37 +02001428 { config_parse_kill_signal, "SIGNAL" },
Lennart Poetteringe5373522010-04-10 17:53:17 +02001429 { config_parse_listen, "SOCKET [...]" },
1430 { config_parse_socket_bind, "SOCKETBIND" },
Lennart Poettering93ef5e82010-04-23 20:27:47 +02001431 { config_parse_bindtodevice, "NETWORKINTERFACE" },
1432 { config_parse_usec, "SECONDS" },
1433 { config_parse_path_strv, "PATH [...]" },
Lennart Poettering932921b2010-04-24 03:11:01 +02001434 { config_parse_mount_flags, "MOUNTFLAG [...]" },
Lennart Poetteringf2d37692010-06-18 23:25:19 +02001435 { config_parse_string_printf, "STRING" },
Lennart Poettering871d7de2010-05-24 01:45:54 +02001436 { config_parse_timer, "TIMER" },
1437 { config_parse_timer_unit, "NAME" },
Lennart Poetteringc952c6e2010-06-18 23:12:48 +02001438 { config_parse_path_spec, "PATH" },
1439 { config_parse_path_unit, "UNIT" },
Lennart Poettering8b03dae2010-07-01 16:34:26 +02001440 { config_parse_notify_access, "ACCESS" },
1441 { config_parse_ip_tos, "TOS" },
Lennart Poetteringe5373522010-04-10 17:53:17 +02001442 };
1443
1444 assert(f);
1445 assert(items);
1446
1447 for (i = items; i->lvalue; i++) {
1448 unsigned j;
1449 const char *rvalue = "OTHER";
1450
1451 if (!streq_ptr(i->section, prev_section)) {
1452 if (!not_first)
1453 not_first = true;
1454 else
1455 fputc('\n', f);
1456
1457 fprintf(f, "[%s]\n", i->section);
1458 prev_section = i->section;
1459 }
1460
1461 for (j = 0; j < ELEMENTSOF(table); j++)
1462 if (i->parse == table[j].callback) {
1463 rvalue = table[j].rvalue;
1464 break;
1465 }
1466
1467 fprintf(f, "%s=%s\n", i->lvalue, rvalue);
1468 }
1469}
1470
1471static int load_from_path(Unit *u, const char *path) {
Lennart Poettering87f0e412010-01-26 21:39:06 +01001472
1473 static const char* const section_table[_UNIT_TYPE_MAX] = {
1474 [UNIT_SERVICE] = "Service",
1475 [UNIT_TIMER] = "Timer",
1476 [UNIT_SOCKET] = "Socket",
1477 [UNIT_TARGET] = "Target",
1478 [UNIT_DEVICE] = "Device",
1479 [UNIT_MOUNT] = "Mount",
1480 [UNIT_AUTOMOUNT] = "Automount",
Maarten Lankhorst07b0b132010-05-09 18:44:11 +02001481 [UNIT_SNAPSHOT] = "Snapshot",
Lennart Poettering01f78472010-05-24 05:25:33 +02001482 [UNIT_SWAP] = "Swap",
1483 [UNIT_PATH] = "Path"
Lennart Poettering42f4e3c2010-01-19 02:56:37 +01001484 };
1485
Lennart Poettering034c6ed2010-01-26 04:18:44 +01001486#define EXEC_CONTEXT_CONFIG_ITEMS(context, section) \
Lennart Poettering9eba9da2010-01-29 20:46:22 +01001487 { "WorkingDirectory", config_parse_path, &(context).working_directory, section }, \
1488 { "RootDirectory", config_parse_path, &(context).root_directory, section }, \
Lennart Poetteringf2d37692010-06-18 23:25:19 +02001489 { "User", config_parse_string_printf, &(context).user, section }, \
1490 { "Group", config_parse_string_printf, &(context).group, section }, \
Lennart Poettering1c01f822010-01-27 02:16:11 +01001491 { "SupplementaryGroups", config_parse_strv, &(context).supplementary_groups, section }, \
Lennart Poetteringfb33a392010-01-28 02:53:56 +01001492 { "Nice", config_parse_nice, &(context), section }, \
1493 { "OOMAdjust", config_parse_oom_adjust, &(context), section }, \
Lennart Poettering9eba9da2010-01-29 20:46:22 +01001494 { "IOSchedulingClass", config_parse_io_class, &(context), section }, \
Lennart Poettering94f04342010-01-30 01:55:42 +01001495 { "IOSchedulingPriority", config_parse_io_priority, &(context), section }, \
1496 { "CPUSchedulingPolicy", config_parse_cpu_sched_policy,&(context), section }, \
1497 { "CPUSchedulingPriority", config_parse_cpu_sched_prio, &(context), section }, \
Lennart Poettering38b48752010-02-02 12:50:04 +01001498 { "CPUSchedulingResetOnFork", config_parse_bool, &(context).cpu_sched_reset_on_fork, section }, \
Lennart Poettering94f04342010-01-30 01:55:42 +01001499 { "CPUAffinity", config_parse_cpu_affinity, &(context), section }, \
Lennart Poetteringb5a06992010-02-12 02:02:14 +01001500 { "UMask", config_parse_mode, &(context).umask, section }, \
Lennart Poettering071830f2010-01-28 02:06:20 +01001501 { "Environment", config_parse_strv, &(context).environment, section }, \
Lennart Poetteringddb26e12010-06-18 06:06:24 +02001502 { "EnvironmentFile", config_parse_env_file, &(context).environment, section }, \
Lennart Poettering80876c22010-04-13 02:06:27 +02001503 { "StandardInput", config_parse_input, &(context).std_input, section }, \
1504 { "StandardOutput", config_parse_output, &(context).std_output, section }, \
Lennart Poetteringa4ddf822010-06-03 03:37:12 +02001505 { "StandardError", config_parse_output, &(context).std_error, section }, \
Lennart Poettering80876c22010-04-13 02:06:27 +02001506 { "TTYPath", config_parse_path, &(context).tty_path, section }, \
Lennart Poetteringf2d37692010-06-18 23:25:19 +02001507 { "SyslogIdentifier", config_parse_string_printf, &(context).syslog_identifier, section }, \
Lennart Poettering071830f2010-01-28 02:06:20 +01001508 { "SyslogFacility", config_parse_facility, &(context).syslog_priority, section }, \
Lennart Poettering94f04342010-01-30 01:55:42 +01001509 { "SyslogLevel", config_parse_level, &(context).syslog_priority, section }, \
Lennart Poettering74922902010-07-05 01:08:13 +02001510 { "SyslogLevelPrefix", config_parse_bool, &(context).syslog_level_prefix, section }, \
Lennart Poettering94f04342010-01-30 01:55:42 +01001511 { "Capabilities", config_parse_capabilities, &(context), section }, \
1512 { "SecureBits", config_parse_secure_bits, &(context), section }, \
1513 { "CapabilityBoundingSetDrop", config_parse_bounding_set, &(context), section }, \
Lennart Poettering03fae012010-07-04 21:12:10 +02001514 { "TimerSlackNSec", config_parse_timer_slack_nsec,&(context), section }, \
Lennart Poettering94f04342010-01-30 01:55:42 +01001515 { "LimitCPU", config_parse_limit, &(context).rlimit[RLIMIT_CPU], section }, \
1516 { "LimitFSIZE", config_parse_limit, &(context).rlimit[RLIMIT_FSIZE], section }, \
1517 { "LimitDATA", config_parse_limit, &(context).rlimit[RLIMIT_DATA], section }, \
1518 { "LimitSTACK", config_parse_limit, &(context).rlimit[RLIMIT_STACK], section }, \
1519 { "LimitCORE", config_parse_limit, &(context).rlimit[RLIMIT_CORE], section }, \
1520 { "LimitRSS", config_parse_limit, &(context).rlimit[RLIMIT_RSS], section }, \
1521 { "LimitNOFILE", config_parse_limit, &(context).rlimit[RLIMIT_NOFILE], section }, \
1522 { "LimitAS", config_parse_limit, &(context).rlimit[RLIMIT_AS], section }, \
1523 { "LimitNPROC", config_parse_limit, &(context).rlimit[RLIMIT_NPROC], section }, \
1524 { "LimitMEMLOCK", config_parse_limit, &(context).rlimit[RLIMIT_MEMLOCK], section }, \
1525 { "LimitLOCKS", config_parse_limit, &(context).rlimit[RLIMIT_LOCKS], section }, \
1526 { "LimitSIGPENDING", config_parse_limit, &(context).rlimit[RLIMIT_SIGPENDING], section }, \
1527 { "LimitMSGQUEUE", config_parse_limit, &(context).rlimit[RLIMIT_MSGQUEUE], section }, \
1528 { "LimitNICE", config_parse_limit, &(context).rlimit[RLIMIT_NICE], section }, \
1529 { "LimitRTPRIO", config_parse_limit, &(context).rlimit[RLIMIT_RTPRIO], section }, \
Lennart Poettering451a0742010-02-12 02:00:18 +01001530 { "LimitRTTIME", config_parse_limit, &(context).rlimit[RLIMIT_RTTIME], section }, \
Lennart Poettering15ae4222010-04-21 22:15:06 +02001531 { "ControlGroup", config_parse_cgroup, u, section }, \
1532 { "ReadWriteDirectories", config_parse_path_strv, &(context).read_write_dirs, section }, \
1533 { "ReadOnlyDirectories", config_parse_path_strv, &(context).read_only_dirs, section }, \
1534 { "InaccessibleDirectories",config_parse_path_strv, &(context).inaccessible_dirs, section }, \
1535 { "PrivateTmp", config_parse_bool, &(context).private_tmp, section }, \
Lennart Poetteringdf1f0af2010-06-16 16:25:42 +02001536 { "MountFlags", config_parse_mount_flags, &(context), section }, \
Lennart Poetteringf2d37692010-06-18 23:25:19 +02001537 { "TCPWrapName", config_parse_string_printf, &(context).tcpwrap_name, section }, \
Lennart Poettering2e22afe2010-07-10 04:49:37 +02001538 { "PAMName", config_parse_string_printf, &(context).pam_name, section }, \
1539 { "KillMode", config_parse_kill_mode, &(context).kill_mode, section }, \
1540 { "KillSignal", config_parse_kill_signal, &(context).kill_signal, section }
Lennart Poettering034c6ed2010-01-26 04:18:44 +01001541
Lennart Poettering3efd4192009-11-19 23:13:20 +01001542 const ConfigItem items[] = {
Lennart Poettering09477262010-04-15 23:20:17 +02001543 { "Names", config_parse_names, u, "Unit" },
Lennart Poetteringf2d37692010-06-18 23:25:19 +02001544 { "Description", config_parse_string_printf, &u->meta.description, "Unit" },
Lennart Poettering09477262010-04-15 23:20:17 +02001545 { "Requires", config_parse_deps, UINT_TO_PTR(UNIT_REQUIRES), "Unit" },
1546 { "RequiresOverridable", config_parse_deps, UINT_TO_PTR(UNIT_REQUIRES_OVERRIDABLE), "Unit" },
1547 { "Requisite", config_parse_deps, UINT_TO_PTR(UNIT_REQUISITE), "Unit" },
1548 { "RequisiteOverridable", config_parse_deps, UINT_TO_PTR(UNIT_REQUISITE_OVERRIDABLE), "Unit" },
1549 { "Wants", config_parse_deps, UINT_TO_PTR(UNIT_WANTS), "Unit" },
1550 { "Conflicts", config_parse_deps, UINT_TO_PTR(UNIT_CONFLICTS), "Unit" },
1551 { "Before", config_parse_deps, UINT_TO_PTR(UNIT_BEFORE), "Unit" },
1552 { "After", config_parse_deps, UINT_TO_PTR(UNIT_AFTER), "Unit" },
1553 { "RecursiveStop", config_parse_bool, &u->meta.recursive_stop, "Unit" },
1554 { "StopWhenUnneeded", config_parse_bool, &u->meta.stop_when_unneeded, "Unit" },
Lennart Poetteringbc0f8772010-05-21 23:41:54 +02001555 { "OnlyByDependency", config_parse_bool, &u->meta.only_by_dependency, "Unit" },
Lennart Poetteringa40eb732010-07-03 19:48:33 +02001556 { "DefaultDependencies", config_parse_bool, &u->meta.default_dependencies, "Unit" },
Lennart Poettering034c6ed2010-01-26 04:18:44 +01001557
Lennart Poettering1c01f822010-01-27 02:16:11 +01001558 { "PIDFile", config_parse_path, &u->service.pid_file, "Service" },
1559 { "ExecStartPre", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START_PRE, "Service" },
1560 { "ExecStart", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START, "Service" },
1561 { "ExecStartPost", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START_POST, "Service" },
1562 { "ExecReload", config_parse_exec, u->service.exec_command+SERVICE_EXEC_RELOAD, "Service" },
1563 { "ExecStop", config_parse_exec, u->service.exec_command+SERVICE_EXEC_STOP, "Service" },
1564 { "ExecStopPost", config_parse_exec, u->service.exec_command+SERVICE_EXEC_STOP_POST, "Service" },
1565 { "RestartSec", config_parse_usec, &u->service.restart_usec, "Service" },
1566 { "TimeoutSec", config_parse_usec, &u->service.timeout_usec, "Service" },
Lennart Poettering0d87eb42010-04-13 02:22:41 +02001567 { "Type", config_parse_service_type, &u->service.type, "Service" },
1568 { "Restart", config_parse_service_restart, &u->service.restart, "Service" },
Lennart Poettering81a2b7c2010-02-14 22:43:08 +01001569 { "PermissionsStartOnly", config_parse_bool, &u->service.permissions_start_only, "Service" },
1570 { "RootDirectoryStartOnly", config_parse_bool, &u->service.root_directory_start_only, "Service" },
Lennart Poettering8e274522010-03-31 16:29:55 +02001571 { "ValidNoProcess", config_parse_bool, &u->service.valid_no_process, "Service" },
Lennart Poetteringa9a1e002010-04-06 02:40:10 +02001572 { "SysVStartPriority", config_parse_sysv_priority, &u->service.sysv_start_priority, "Service" },
Lennart Poetteringb778d552010-04-10 17:48:41 +02001573 { "NonBlocking", config_parse_bool, &u->service.exec_context.non_blocking, "Service" },
Lennart Poetteringf2d37692010-06-18 23:25:19 +02001574 { "BusName", config_parse_string_printf, &u->service.bus_name, "Service" },
Lennart Poetteringc952c6e2010-06-18 23:12:48 +02001575 { "NotifyAccess", config_parse_notify_access, &u->service.notify_access, "Service" },
Lennart Poettering87f0e412010-01-26 21:39:06 +01001576 EXEC_CONTEXT_CONFIG_ITEMS(u->service.exec_context, "Service"),
Lennart Poettering034c6ed2010-01-26 04:18:44 +01001577
Lennart Poettering1c01f822010-01-27 02:16:11 +01001578 { "ListenStream", config_parse_listen, &u->socket, "Socket" },
1579 { "ListenDatagram", config_parse_listen, &u->socket, "Socket" },
1580 { "ListenSequentialPacket", config_parse_listen, &u->socket, "Socket" },
1581 { "ListenFIFO", config_parse_listen, &u->socket, "Socket" },
1582 { "BindIPv6Only", config_parse_socket_bind, &u->socket, "Socket" },
1583 { "Backlog", config_parse_unsigned, &u->socket.backlog, "Socket" },
Lennart Poetteringacbb0222010-01-27 04:31:52 +01001584 { "BindToDevice", config_parse_bindtodevice, &u->socket, "Socket" },
Lennart Poettering1c01f822010-01-27 02:16:11 +01001585 { "ExecStartPre", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_START_PRE, "Socket" },
1586 { "ExecStartPost", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_START_POST, "Socket" },
1587 { "ExecStopPre", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_STOP_PRE, "Socket" },
1588 { "ExecStopPost", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_STOP_POST, "Socket" },
Lennart Poettering108736d2010-04-10 17:50:54 +02001589 { "TimeoutSec", config_parse_usec, &u->socket.timeout_usec, "Socket" },
Lennart Poetteringb5a06992010-02-12 02:02:14 +01001590 { "DirectoryMode", config_parse_mode, &u->socket.directory_mode, "Socket" },
1591 { "SocketMode", config_parse_mode, &u->socket.socket_mode, "Socket" },
Lennart Poettering4f2d5282010-04-15 06:19:54 +02001592 { "Accept", config_parse_bool, &u->socket.accept, "Socket" },
Lennart Poettering6cf6bbc2010-06-19 04:25:28 +02001593 { "MaxConnections", config_parse_unsigned, &u->socket.max_connections, "Socket" },
Lennart Poettering4fd59482010-07-01 00:29:17 +02001594 { "KeepAlive", config_parse_bool, &u->socket.keep_alive, "Socket" },
1595 { "Priority", config_parse_int, &u->socket.priority, "Socket" },
1596 { "ReceiveBuffer", config_parse_size, &u->socket.receive_buffer, "Socket" },
1597 { "SendBuffer", config_parse_size, &u->socket.send_buffer, "Socket" },
1598 { "IPTOS", config_parse_ip_tos, &u->socket.ip_tos, "Socket" },
1599 { "IPTTL", config_parse_int, &u->socket.ip_ttl, "Socket" },
1600 { "Mark", config_parse_int, &u->socket.mark, "Socket" },
1601 { "PipeSize", config_parse_size, &u->socket.pipe_size, "Socket" },
1602 { "FreeBind", config_parse_bool, &u->socket.free_bind, "Socket" },
Lennart Poettering87f0e412010-01-26 21:39:06 +01001603 EXEC_CONTEXT_CONFIG_ITEMS(u->socket.exec_context, "Socket"),
Lennart Poettering034c6ed2010-01-26 04:18:44 +01001604
Lennart Poetteringe5373522010-04-10 17:53:17 +02001605 { "What", config_parse_string, &u->mount.parameters_fragment.what, "Mount" },
1606 { "Where", config_parse_path, &u->mount.where, "Mount" },
1607 { "Options", config_parse_string, &u->mount.parameters_fragment.options, "Mount" },
1608 { "Type", config_parse_string, &u->mount.parameters_fragment.fstype, "Mount" },
1609 { "TimeoutSec", config_parse_usec, &u->mount.timeout_usec, "Mount" },
Lennart Poettering3e5235b2010-07-02 00:28:44 +02001610 { "DirectoryMode", config_parse_mode, &u->mount.directory_mode, "Mount" },
Lennart Poetteringe5373522010-04-10 17:53:17 +02001611 EXEC_CONTEXT_CONFIG_ITEMS(u->mount.exec_context, "Mount"),
Lennart Poettering034c6ed2010-01-26 04:18:44 +01001612
Lennart Poettering8d567582010-04-16 23:24:39 +02001613 { "Where", config_parse_path, &u->automount.where, "Automount" },
Lennart Poettering1cf18f22010-07-02 01:17:21 +02001614 { "DirectoryMode", config_parse_mode, &u->automount.directory_mode, "Automount" },
Lennart Poettering8d567582010-04-16 23:24:39 +02001615
Lennart Poettering01f78472010-05-24 05:25:33 +02001616 { "What", config_parse_path, &u->swap.parameters_fragment.what, "Swap" },
1617 { "Priority", config_parse_int, &u->swap.parameters_fragment.priority, "Swap" },
Maarten Lankhorst07b0b132010-05-09 18:44:11 +02001618
Lennart Poettering03fae012010-07-04 21:12:10 +02001619 { "OnActiveSec", config_parse_timer, &u->timer, "Timer" },
1620 { "OnBootSec", config_parse_timer, &u->timer, "Timer" },
1621 { "OnStartupSec", config_parse_timer, &u->timer, "Timer" },
1622 { "OnUnitActiveSec", config_parse_timer, &u->timer, "Timer" },
1623 { "OnUnitInactiveSec", config_parse_timer, &u->timer, "Timer" },
Lennart Poettering01f78472010-05-24 05:25:33 +02001624 { "Unit", config_parse_timer_unit, &u->timer, "Timer" },
1625
1626 { "PathExists", config_parse_path_spec, &u->path, "Path" },
1627 { "PathChanged", config_parse_path_spec, &u->path, "Path" },
1628 { "DirectoryNotEmpty", config_parse_path_spec, &u->path, "Path" },
1629 { "Unit", config_parse_path_unit, &u->path, "Path" },
Lennart Poettering871d7de2010-05-24 01:45:54 +02001630
Lennart Poettering10e87ee2010-06-16 01:58:50 +02001631 /* The [Install] section is ignored here. */
1632 { "Alias", NULL, NULL, "Install" },
1633 { "WantedBy", NULL, NULL, "Install" },
1634 { "Also", NULL, NULL, "Install" },
1635
Lennart Poettering3efd4192009-11-19 23:13:20 +01001636 { NULL, NULL, NULL, NULL }
1637 };
1638
Lennart Poettering034c6ed2010-01-26 04:18:44 +01001639#undef EXEC_CONTEXT_CONFIG_ITEMS
Lennart Poettering42f4e3c2010-01-19 02:56:37 +01001640
Lennart Poettering10e87ee2010-06-16 01:58:50 +02001641 const char *sections[4];
Lennart Poettering0301abf2010-01-27 00:15:56 +01001642 int r;
Lennart Poettering87f0e412010-01-26 21:39:06 +01001643 Set *symlink_names;
Lennart Poettering23a177e2010-04-06 02:43:58 +02001644 FILE *f = NULL;
1645 char *filename = NULL, *id = NULL;
1646 Unit *merged;
1647
Lennart Poetteringe5373522010-04-10 17:53:17 +02001648 if (!u) {
1649 /* Dirty dirty hack. */
1650 dump_items((FILE*) path, items);
1651 return 0;
1652 }
1653
Lennart Poettering23a177e2010-04-06 02:43:58 +02001654 assert(u);
Lennart Poetteringe5373522010-04-10 17:53:17 +02001655 assert(path);
Lennart Poettering3efd4192009-11-19 23:13:20 +01001656
Lennart Poettering09477262010-04-15 23:20:17 +02001657 sections[0] = "Unit";
Lennart Poettering87f0e412010-01-26 21:39:06 +01001658 sections[1] = section_table[u->meta.type];
Lennart Poettering10e87ee2010-06-16 01:58:50 +02001659 sections[2] = "Install";
1660 sections[3] = NULL;
Lennart Poettering42f4e3c2010-01-19 02:56:37 +01001661
Lennart Poettering87f0e412010-01-26 21:39:06 +01001662 if (!(symlink_names = set_new(string_hash_func, string_compare_func)))
1663 return -ENOMEM;
Lennart Poettering3efd4192009-11-19 23:13:20 +01001664
Lennart Poettering036643a2010-02-13 01:07:02 +01001665 if (path_is_absolute(path)) {
1666
1667 if (!(filename = strdup(path))) {
1668 r = -ENOMEM;
1669 goto finish;
1670 }
1671
1672 if ((r = open_follow(&filename, &f, symlink_names, &id)) < 0) {
1673 free(filename);
1674 filename = NULL;
1675
1676 if (r != -ENOENT)
1677 goto finish;
1678 }
1679
1680 } else {
1681 char **p;
1682
Lennart Poettering84e35432010-06-15 14:45:15 +02001683 STRV_FOREACH(p, u->meta.manager->lookup_paths.unit_path) {
Lennart Poettering036643a2010-02-13 01:07:02 +01001684
1685 /* Instead of opening the path right away, we manually
1686 * follow all symlinks and add their name to our unit
1687 * name set while doing so */
1688 if (!(filename = path_make_absolute(path, *p))) {
1689 r = -ENOMEM;
1690 goto finish;
1691 }
1692
1693 if ((r = open_follow(&filename, &f, symlink_names, &id)) < 0) {
1694 char *sn;
1695
1696 free(filename);
1697 filename = NULL;
1698
1699 if (r != -ENOENT)
1700 goto finish;
1701
1702 /* Empty the symlink names for the next run */
1703 while ((sn = set_steal_first(symlink_names)))
1704 free(sn);
1705
1706 continue;
1707 }
1708
1709 break;
1710 }
Lennart Poettering034c6ed2010-01-26 04:18:44 +01001711 }
1712
Lennart Poettering036643a2010-02-13 01:07:02 +01001713 if (!filename) {
Lennart Poettering23a177e2010-04-06 02:43:58 +02001714 r = 0;
1715 goto finish;
1716 }
1717
1718 merged = u;
1719 if ((r = merge_by_names(&merged, symlink_names, id)) < 0)
1720 goto finish;
1721
1722 if (merged != u) {
Lennart Poetteringe5373522010-04-10 17:53:17 +02001723 u->meta.load_state = UNIT_MERGED;
Lennart Poettering23a177e2010-04-06 02:43:58 +02001724 r = 0;
Lennart Poettering0301abf2010-01-27 00:15:56 +01001725 goto finish;
1726 }
1727
1728 /* Now, parse the file contents */
Lennart Poettering10e87ee2010-06-16 01:58:50 +02001729 if ((r = config_parse(filename, f, sections, items, false, u)) < 0)
Lennart Poettering0301abf2010-01-27 00:15:56 +01001730 goto finish;
1731
Lennart Poettering6be1e7d2010-02-14 01:01:10 +01001732 free(u->meta.fragment_path);
1733 u->meta.fragment_path = filename;
Lennart Poettering0301abf2010-01-27 00:15:56 +01001734 filename = NULL;
1735
Lennart Poetteringe5373522010-04-10 17:53:17 +02001736 u->meta.load_state = UNIT_LOADED;
Lennart Poettering23a177e2010-04-06 02:43:58 +02001737 r = 0;
Lennart Poettering87f0e412010-01-26 21:39:06 +01001738
1739finish:
Lennart Poettering53ec43c2010-06-15 02:45:26 +02001740 set_free_free(symlink_names);
Lennart Poettering0301abf2010-01-27 00:15:56 +01001741 free(filename);
1742
Lennart Poettering23a177e2010-04-06 02:43:58 +02001743 if (f)
1744 fclose(f);
1745
Lennart Poettering0301abf2010-01-27 00:15:56 +01001746 return r;
1747}
1748
Lennart Poetteringe5373522010-04-10 17:53:17 +02001749int unit_load_fragment(Unit *u) {
Lennart Poettering23a177e2010-04-06 02:43:58 +02001750 int r;
Lennart Poettering0301abf2010-01-27 00:15:56 +01001751
1752 assert(u);
Lennart Poettering0301abf2010-01-27 00:15:56 +01001753
Lennart Poettering23a177e2010-04-06 02:43:58 +02001754 if (u->meta.fragment_path) {
1755
Lennart Poetteringe5373522010-04-10 17:53:17 +02001756 if ((r = load_from_path(u, u->meta.fragment_path)) < 0)
Lennart Poettering23a177e2010-04-06 02:43:58 +02001757 return r;
1758
1759 } else {
Lennart Poettering0301abf2010-01-27 00:15:56 +01001760 Iterator i;
Lennart Poettering890f4342010-02-14 01:08:20 +01001761 const char *t;
Lennart Poettering0301abf2010-01-27 00:15:56 +01001762
Lennart Poettering890f4342010-02-14 01:08:20 +01001763 /* Try to find the unit under its id */
Lennart Poettering9e2f7c12010-04-15 03:11:11 +02001764 if ((r = load_from_path(u, u->meta.id)) < 0)
1765 return r;
Lennart Poettering890f4342010-02-14 01:08:20 +01001766
1767 /* Try to find an alias we can load this with */
Lennart Poetteringe5373522010-04-10 17:53:17 +02001768 if (u->meta.load_state == UNIT_STUB)
Lennart Poettering23a177e2010-04-06 02:43:58 +02001769 SET_FOREACH(t, u->meta.names, i) {
1770
Lennart Poettering9e2f7c12010-04-15 03:11:11 +02001771 if (t == u->meta.id)
Lennart Poettering23a177e2010-04-06 02:43:58 +02001772 continue;
1773
Lennart Poetteringe5373522010-04-10 17:53:17 +02001774 if ((r = load_from_path(u, t)) < 0)
Lennart Poettering23a177e2010-04-06 02:43:58 +02001775 return r;
1776
Lennart Poetteringe5373522010-04-10 17:53:17 +02001777 if (u->meta.load_state != UNIT_STUB)
Lennart Poettering890f4342010-02-14 01:08:20 +01001778 break;
Lennart Poettering23a177e2010-04-06 02:43:58 +02001779 }
Lennart Poettering9e2f7c12010-04-15 03:11:11 +02001780
1781 /* Now, follow the same logic, but look for a template */
1782 if (u->meta.load_state == UNIT_STUB && u->meta.instance) {
1783 char *k;
1784
1785 if (!(k = unit_name_template(u->meta.id)))
1786 return -ENOMEM;
1787
1788 r = load_from_path(u, k);
1789 free(k);
1790
1791 if (r < 0)
1792 return r;
1793
1794 if (u->meta.load_state == UNIT_STUB)
1795 SET_FOREACH(t, u->meta.names, i) {
1796
1797 if (t == u->meta.id)
1798 continue;
1799
1800 if (!(k = unit_name_template(t)))
1801 return -ENOMEM;
1802
1803 r = load_from_path(u, k);
1804 free(k);
1805
1806 if (r < 0)
1807 return r;
1808
1809 if (u->meta.load_state != UNIT_STUB)
1810 break;
1811 }
1812 }
Lennart Poettering0301abf2010-01-27 00:15:56 +01001813 }
Lennart Poettering87f0e412010-01-26 21:39:06 +01001814
Lennart Poettering23a177e2010-04-06 02:43:58 +02001815 return 0;
Lennart Poettering3efd4192009-11-19 23:13:20 +01001816}
Lennart Poetteringe5373522010-04-10 17:53:17 +02001817
1818void unit_dump_config_items(FILE *f) {
1819 /* OK, this wins a prize for extreme ugliness. */
1820
1821 load_from_path(NULL, (const void*) f);
1822}