blob: 334bc713be8f691bcb65ba7ebf93d2ef31e5aab5 [file] [log] [blame]
Lennart Poetteringd6c95742010-08-14 19:59:25 +02001/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
Lennart Poettering3efd4192009-11-19 23:13:20 +01002
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
Fabiano Fidencio07459bb2010-09-21 00:23:12 -030045#ifndef HAVE_SYSV_COMPAT
46static int config_parse_warn_compat(
47 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
55 log_debug("[%s:%u] Support for option %s= has been disabled at compile time and is ignored", filename, line, lvalue);
56 return 0;
57}
58#endif
59
Lennart Poettering42f4e3c2010-01-19 02:56:37 +010060static int config_parse_deps(
Lennart Poettering3efd4192009-11-19 23:13:20 +010061 const char *filename,
62 unsigned line,
63 const char *section,
64 const char *lvalue,
65 const char *rvalue,
66 void *data,
67 void *userdata) {
68
Lennart Poettering87f0e412010-01-26 21:39:06 +010069 UnitDependency d = PTR_TO_UINT(data);
70 Unit *u = userdata;
Lennart Poettering3efd4192009-11-19 23:13:20 +010071 char *w;
72 size_t l;
73 char *state;
74
75 assert(filename);
76 assert(lvalue);
77 assert(rvalue);
Lennart Poettering3efd4192009-11-19 23:13:20 +010078
Lennart Poetteringf60f22d2010-07-07 20:58:41 +020079 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
Lennart Poettering9e2f7c12010-04-15 03:11:11 +020080 char *t, *k;
Lennart Poettering3efd4192009-11-19 23:13:20 +010081 int r;
Lennart Poettering3efd4192009-11-19 23:13:20 +010082
83 if (!(t = strndup(w, l)))
84 return -ENOMEM;
85
Lennart Poettering9e2f7c12010-04-15 03:11:11 +020086 k = unit_name_printf(u, t);
Lennart Poettering3efd4192009-11-19 23:13:20 +010087 free(t);
88
Lennart Poettering9e2f7c12010-04-15 03:11:11 +020089 if (!k)
90 return -ENOMEM;
91
Lennart Poettering701cc382010-04-21 06:01:13 +020092 r = unit_add_dependency_by_name(u, d, k, NULL, true);
Lennart Poettering9e2f7c12010-04-15 03:11:11 +020093
Lennart Poetteringc0b34692010-08-17 03:30:53 +020094 if (r < 0) {
95 log_error("Failed to add dependency on %s, ignoring: %s", k, strerror(-r));
96 free(k);
97 return 0;
98 }
99
100 free(k);
Lennart Poettering3efd4192009-11-19 23:13:20 +0100101 }
102
103 return 0;
104}
105
Lennart Poettering42f4e3c2010-01-19 02:56:37 +0100106static int config_parse_names(
Lennart Poettering87d15152010-01-18 23:50:13 +0100107 const char *filename,
108 unsigned line,
109 const char *section,
110 const char *lvalue,
111 const char *rvalue,
112 void *data,
113 void *userdata) {
114
Lennart Poettering87f0e412010-01-26 21:39:06 +0100115 Unit *u = userdata;
Lennart Poettering87d15152010-01-18 23:50:13 +0100116 char *w;
117 size_t l;
118 char *state;
119
120 assert(filename);
121 assert(lvalue);
122 assert(rvalue);
123 assert(data);
124
Lennart Poetteringf60f22d2010-07-07 20:58:41 +0200125 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
Lennart Poettering9e2f7c12010-04-15 03:11:11 +0200126 char *t, *k;
Lennart Poettering87d15152010-01-18 23:50:13 +0100127 int r;
Lennart Poettering87d15152010-01-18 23:50:13 +0100128
129 if (!(t = strndup(w, l)))
130 return -ENOMEM;
131
Lennart Poettering9e2f7c12010-04-15 03:11:11 +0200132 k = unit_name_printf(u, t);
Lennart Poettering87d15152010-01-18 23:50:13 +0100133 free(t);
Lennart Poettering23a177e2010-04-06 02:43:58 +0200134
Lennart Poettering9e2f7c12010-04-15 03:11:11 +0200135 if (!k)
136 return -ENOMEM;
137
138 r = unit_merge_by_name(u, k);
Lennart Poettering9e2f7c12010-04-15 03:11:11 +0200139
Lennart Poetteringc0b34692010-08-17 03:30:53 +0200140 if (r < 0) {
141 log_error("Failed to add name %s, ignoring: %s", k, strerror(-r));
142 free(k);
143 return 0;
144 }
145
146 free(k);
Lennart Poettering87d15152010-01-18 23:50:13 +0100147 }
148
149 return 0;
150}
151
Lennart Poetteringf2d37692010-06-18 23:25:19 +0200152static int config_parse_string_printf(
Lennart Poettering932921b2010-04-24 03:11:01 +0200153 const char *filename,
154 unsigned line,
155 const char *section,
156 const char *lvalue,
157 const char *rvalue,
158 void *data,
159 void *userdata) {
160
161 Unit *u = userdata;
Lennart Poetteringf2d37692010-06-18 23:25:19 +0200162 char **s = data;
Lennart Poettering932921b2010-04-24 03:11:01 +0200163 char *k;
164
165 assert(filename);
166 assert(lvalue);
167 assert(rvalue);
Lennart Poetteringf2d37692010-06-18 23:25:19 +0200168 assert(s);
169 assert(u);
Lennart Poettering932921b2010-04-24 03:11:01 +0200170
171 if (!(k = unit_full_printf(u, rvalue)))
172 return -ENOMEM;
173
Lennart Poetteringf2d37692010-06-18 23:25:19 +0200174 free(*s);
Lennart Poettering932921b2010-04-24 03:11:01 +0200175 if (*k)
Lennart Poetteringf2d37692010-06-18 23:25:19 +0200176 *s = k;
Lennart Poettering932921b2010-04-24 03:11:01 +0200177 else {
178 free(k);
Lennart Poetteringf2d37692010-06-18 23:25:19 +0200179 *s = NULL;
Lennart Poettering932921b2010-04-24 03:11:01 +0200180 }
181
182 return 0;
183}
184
Lennart Poettering42f4e3c2010-01-19 02:56:37 +0100185static int config_parse_listen(
186 const char *filename,
187 unsigned line,
188 const char *section,
189 const char *lvalue,
190 const char *rvalue,
191 void *data,
192 void *userdata) {
193
Lennart Poettering542563b2010-01-23 03:35:54 +0100194 SocketPort *p;
195 Socket *s;
Lennart Poettering16354ef2010-01-20 19:19:53 +0100196
Lennart Poettering42f4e3c2010-01-19 02:56:37 +0100197 assert(filename);
198 assert(lvalue);
199 assert(rvalue);
200 assert(data);
201
Lennart Poettering542563b2010-01-23 03:35:54 +0100202 s = (Socket*) data;
203
204 if (!(p = new0(SocketPort, 1)))
205 return -ENOMEM;
206
207 if (streq(lvalue, "ListenFIFO")) {
208 p->type = SOCKET_FIFO;
209
210 if (!(p->path = strdup(rvalue))) {
211 free(p);
212 return -ENOMEM;
213 }
Lennart Poettering01f78472010-05-24 05:25:33 +0200214
215 path_kill_slashes(p->path);
Lennart Poettering542563b2010-01-23 03:35:54 +0100216 } else {
217 p->type = SOCKET_SOCKET;
218
Lennart Poetteringbd40a2d2011-01-22 02:18:59 +0100219 if (socket_address_parse(&p->address, rvalue) < 0) {
Lennart Poetteringc0b34692010-08-17 03:30:53 +0200220 log_error("[%s:%u] Failed to parse address value, ignoring: %s", filename, line, rvalue);
Lennart Poettering542563b2010-01-23 03:35:54 +0100221 free(p);
Lennart Poetteringc0b34692010-08-17 03:30:53 +0200222 return 0;
Lennart Poettering542563b2010-01-23 03:35:54 +0100223 }
224
225 if (streq(lvalue, "ListenStream"))
226 p->address.type = SOCK_STREAM;
227 else if (streq(lvalue, "ListenDatagram"))
228 p->address.type = SOCK_DGRAM;
229 else {
230 assert(streq(lvalue, "ListenSequentialPacket"));
231 p->address.type = SOCK_SEQPACKET;
232 }
233
234 if (socket_address_family(&p->address) != AF_LOCAL && p->address.type == SOCK_SEQPACKET) {
Lennart Poetteringc0b34692010-08-17 03:30:53 +0200235 log_error("[%s:%u] Address family not supported, ignoring: %s", filename, line, rvalue);
Lennart Poettering542563b2010-01-23 03:35:54 +0100236 free(p);
Lennart Poetteringc0b34692010-08-17 03:30:53 +0200237 return 0;
Lennart Poettering542563b2010-01-23 03:35:54 +0100238 }
Lennart Poettering16354ef2010-01-20 19:19:53 +0100239 }
240
Lennart Poettering542563b2010-01-23 03:35:54 +0100241 p->fd = -1;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100242 LIST_PREPEND(SocketPort, port, s->ports, p);
Lennart Poettering542563b2010-01-23 03:35:54 +0100243
Lennart Poettering16354ef2010-01-20 19:19:53 +0100244 return 0;
Lennart Poettering42f4e3c2010-01-19 02:56:37 +0100245}
246
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100247static int config_parse_socket_bind(
Lennart Poettering42f4e3c2010-01-19 02:56:37 +0100248 const char *filename,
249 unsigned line,
250 const char *section,
251 const char *lvalue,
252 const char *rvalue,
253 void *data,
254 void *userdata) {
255
Lennart Poettering542563b2010-01-23 03:35:54 +0100256 Socket *s;
Lennart Poetteringc0120d92010-05-21 23:41:25 +0200257 SocketAddressBindIPv6Only b;
Lennart Poettering42f4e3c2010-01-19 02:56:37 +0100258
259 assert(filename);
260 assert(lvalue);
261 assert(rvalue);
262 assert(data);
263
Lennart Poettering542563b2010-01-23 03:35:54 +0100264 s = (Socket*) data;
265
Lennart Poetteringc0120d92010-05-21 23:41:25 +0200266 if ((b = socket_address_bind_ipv6_only_from_string(rvalue)) < 0) {
267 int r;
Lennart Poettering42f4e3c2010-01-19 02:56:37 +0100268
Lennart Poetteringc0120d92010-05-21 23:41:25 +0200269 if ((r = parse_boolean(rvalue)) < 0) {
Lennart Poetteringc0b34692010-08-17 03:30:53 +0200270 log_error("[%s:%u] Failed to parse bind IPv6 only value, ignoring: %s", filename, line, rvalue);
271 return 0;
Lennart Poetteringc0120d92010-05-21 23:41:25 +0200272 }
273
274 s->bind_ipv6_only = r ? SOCKET_ADDRESS_IPV6_ONLY : SOCKET_ADDRESS_BOTH;
275 } else
276 s->bind_ipv6_only = b;
Lennart Poettering542563b2010-01-23 03:35:54 +0100277
Lennart Poettering42f4e3c2010-01-19 02:56:37 +0100278 return 0;
279}
280
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100281static int config_parse_nice(
282 const char *filename,
283 unsigned line,
284 const char *section,
285 const char *lvalue,
286 const char *rvalue,
287 void *data,
288 void *userdata) {
289
Lennart Poetteringfb33a392010-01-28 02:53:56 +0100290 ExecContext *c = data;
Lennart Poetteringbd40a2d2011-01-22 02:18:59 +0100291 int priority;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100292
293 assert(filename);
294 assert(lvalue);
295 assert(rvalue);
296 assert(data);
297
Lennart Poetteringbd40a2d2011-01-22 02:18:59 +0100298 if (safe_atoi(rvalue, &priority) < 0) {
Lennart Poetteringc0b34692010-08-17 03:30:53 +0200299 log_error("[%s:%u] Failed to parse nice priority, ignoring: %s. ", filename, line, rvalue);
300 return 0;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100301 }
302
303 if (priority < PRIO_MIN || priority >= PRIO_MAX) {
Lennart Poetteringc0b34692010-08-17 03:30:53 +0200304 log_error("[%s:%u] Nice priority out of range, ignoring: %s", filename, line, rvalue);
305 return 0;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100306 }
307
Lennart Poetteringfb33a392010-01-28 02:53:56 +0100308 c->nice = priority;
Adrian Spinu71155932011-02-02 15:57:52 +0200309 c->nice_set = true;
Lennart Poetteringfb33a392010-01-28 02:53:56 +0100310
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100311 return 0;
312}
313
Lennart Poetteringdd6c17b2010-08-31 01:33:39 +0200314static int config_parse_oom_score_adjust(
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100315 const char *filename,
316 unsigned line,
317 const char *section,
318 const char *lvalue,
319 const char *rvalue,
320 void *data,
321 void *userdata) {
322
Lennart Poetteringfb33a392010-01-28 02:53:56 +0100323 ExecContext *c = data;
Lennart Poetteringbd40a2d2011-01-22 02:18:59 +0100324 int oa;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100325
326 assert(filename);
327 assert(lvalue);
328 assert(rvalue);
329 assert(data);
330
Lennart Poetteringbd40a2d2011-01-22 02:18:59 +0100331 if (safe_atoi(rvalue, &oa) < 0) {
Lennart Poetteringdd6c17b2010-08-31 01:33:39 +0200332 log_error("[%s:%u] Failed to parse the OOM score adjust value, ignoring: %s", filename, line, rvalue);
Lennart Poetteringc0b34692010-08-17 03:30:53 +0200333 return 0;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100334 }
335
Lennart Poetteringdd6c17b2010-08-31 01:33:39 +0200336 if (oa < OOM_SCORE_ADJ_MIN || oa > OOM_SCORE_ADJ_MAX) {
337 log_error("[%s:%u] OOM score adjust value out of range, ignoring: %s", filename, line, rvalue);
Lennart Poetteringc0b34692010-08-17 03:30:53 +0200338 return 0;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100339 }
340
Lennart Poetteringdd6c17b2010-08-31 01:33:39 +0200341 c->oom_score_adjust = oa;
342 c->oom_score_adjust_set = true;
Lennart Poetteringfb33a392010-01-28 02:53:56 +0100343
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100344 return 0;
345}
346
Lennart Poetteringb5a06992010-02-12 02:02:14 +0100347static int config_parse_mode(
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100348 const char *filename,
349 unsigned line,
350 const char *section,
351 const char *lvalue,
352 const char *rvalue,
353 void *data,
354 void *userdata) {
355
356 mode_t *m = data;
357 long l;
358 char *x = NULL;
359
360 assert(filename);
361 assert(lvalue);
362 assert(rvalue);
363 assert(data);
364
365 errno = 0;
366 l = strtol(rvalue, &x, 8);
367 if (!x || *x || errno) {
Lennart Poetteringc0b34692010-08-17 03:30:53 +0200368 log_error("[%s:%u] Failed to parse mode value, ignoring: %s", filename, line, rvalue);
369 return 0;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100370 }
371
Lennart Poetteringb5a06992010-02-12 02:02:14 +0100372 if (l < 0000 || l > 07777) {
Lennart Poetteringc0b34692010-08-17 03:30:53 +0200373 log_error("[%s:%u] mode value out of range, ignoring: %s", filename, line, rvalue);
374 return 0;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100375 }
376
377 *m = (mode_t) l;
378 return 0;
379}
380
381static int config_parse_exec(
382 const char *filename,
383 unsigned line,
384 const char *section,
385 const char *lvalue,
386 const char *rvalue,
387 void *data,
388 void *userdata) {
389
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200390 ExecCommand **e = data, *nce;
391 char *path, **n;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100392 unsigned k;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100393
394 assert(filename);
395 assert(lvalue);
396 assert(rvalue);
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200397 assert(e);
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100398
Lennart Poettering6c666e22010-05-19 21:51:53 +0200399 /* We accept an absolute path as first argument, or
400 * alternatively an absolute prefixed with @ to allow
401 * overriding of argv[0]. */
402
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200403 for (;;) {
404 char *w;
405 size_t l;
406 char *state;
Lennart Poetteringb708e7c2010-07-12 02:25:42 +0200407 bool honour_argv0 = false, ignore = false;
Lennart Poettering6c666e22010-05-19 21:51:53 +0200408
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200409 path = NULL;
410 nce = NULL;
411 n = NULL;
Lennart Poettering6c666e22010-05-19 21:51:53 +0200412
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200413 rvalue += strspn(rvalue, WHITESPACE);
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100414
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200415 if (rvalue[0] == 0)
416 break;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100417
Lennart Poetteringb708e7c2010-07-12 02:25:42 +0200418 if (rvalue[0] == '-') {
419 ignore = true;
420 rvalue ++;
421 }
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200422
Lennart Poetteringb708e7c2010-07-12 02:25:42 +0200423 if (rvalue[0] == '@') {
424 honour_argv0 = true;
425 rvalue ++;
426 }
427
428 if (*rvalue != '/') {
Lennart Poetteringc0b34692010-08-17 03:30:53 +0200429 log_error("[%s:%u] Invalid executable path in command line, ignoring: %s", filename, line, rvalue);
430 return 0;
Lennart Poettering6c666e22010-05-19 21:51:53 +0200431 }
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100432
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200433 k = 0;
434 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
Lennart Poetteringf90cf442010-11-14 19:58:33 +0100435 if (strncmp(w, ";", MAX(l, 1U)) == 0)
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200436 break;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100437
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200438 k++;
439 }
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100440
Lennart Poetteringb708e7c2010-07-12 02:25:42 +0200441 if (!(n = new(char*, k + !honour_argv0)))
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200442 return -ENOMEM;
443
444 k = 0;
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200445 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
Lennart Poetteringf90cf442010-11-14 19:58:33 +0100446 if (strncmp(w, ";", MAX(l, 1U)) == 0)
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200447 break;
448
Lennart Poetteringb708e7c2010-07-12 02:25:42 +0200449 if (honour_argv0 && w == rvalue) {
450 assert(!path);
451 if (!(path = cunescape_length(w, l)))
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200452 goto fail;
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200453 } else {
454 if (!(n[k++] = cunescape_length(w, l)))
455 goto fail;
456 }
457 }
458
459 n[k] = NULL;
460
461 if (!n[0]) {
Lennart Poetteringc0b34692010-08-17 03:30:53 +0200462 log_error("[%s:%u] Invalid command line, ignoring: %s", filename, line, rvalue);
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200463 strv_free(n);
Lennart Poetteringc0b34692010-08-17 03:30:53 +0200464 return 0;
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200465 }
466
467 if (!path)
468 if (!(path = strdup(n[0])))
469 goto fail;
470
471 assert(path_is_absolute(path));
472
473 if (!(nce = new0(ExecCommand, 1)))
Lennart Poettering6c666e22010-05-19 21:51:53 +0200474 goto fail;
475
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200476 nce->argv = n;
477 nce->path = path;
Lennart Poetteringb708e7c2010-07-12 02:25:42 +0200478 nce->ignore = ignore;
Lennart Poettering6c666e22010-05-19 21:51:53 +0200479
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200480 path_kill_slashes(nce->path);
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100481
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200482 exec_command_append_list(e, nce);
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100483
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200484 rvalue = state;
485 }
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100486
487 return 0;
488
489fail:
Lennart Poettering6c666e22010-05-19 21:51:53 +0200490 n[k] = NULL;
491 strv_free(n);
492 free(path);
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100493 free(nce);
494
495 return -ENOMEM;
496}
497
498static int config_parse_usec(
499 const char *filename,
500 unsigned line,
501 const char *section,
502 const char *lvalue,
503 const char *rvalue,
504 void *data,
505 void *userdata) {
506
507 usec_t *usec = data;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100508
509 assert(filename);
510 assert(lvalue);
511 assert(rvalue);
512 assert(data);
513
Lennart Poetteringbd40a2d2011-01-22 02:18:59 +0100514 if (parse_usec(rvalue, usec) < 0) {
Lennart Poetteringc0b34692010-08-17 03:30:53 +0200515 log_error("[%s:%u] Failed to parse time value, ignoring: %s", filename, line, rvalue);
516 return 0;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100517 }
518
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100519 return 0;
520}
521
Lennart Poettering487393e2010-07-07 01:10:27 +0200522static DEFINE_CONFIG_PARSE_ENUM(config_parse_service_type, service_type, ServiceType, "Failed to parse service type");
523static DEFINE_CONFIG_PARSE_ENUM(config_parse_service_restart, service_restart, ServiceRestart, "Failed to parse service restart specifier");
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100524
Lennart Poettering47be8702010-02-03 14:21:48 +0100525static int config_parse_bindtodevice(
Lennart Poetteringacbb0222010-01-27 04:31:52 +0100526 const char *filename,
527 unsigned line,
528 const char *section,
529 const char *lvalue,
530 const char *rvalue,
531 void *data,
532 void *userdata) {
533
534 Socket *s = data;
535 char *n;
536
537 assert(filename);
538 assert(lvalue);
539 assert(rvalue);
540 assert(data);
541
542 if (rvalue[0] && !streq(rvalue, "*")) {
543 if (!(n = strdup(rvalue)))
544 return -ENOMEM;
545 } else
546 n = NULL;
547
548 free(s->bind_to_device);
549 s->bind_to_device = n;
550
551 return 0;
552}
553
Lennart Poettering487393e2010-07-07 01:10:27 +0200554static DEFINE_CONFIG_PARSE_ENUM(config_parse_output, exec_output, ExecOutput, "Failed to parse output specifier");
555static DEFINE_CONFIG_PARSE_ENUM(config_parse_input, exec_input, ExecInput, "Failed to parse input specifier");
Lennart Poettering071830f2010-01-28 02:06:20 +0100556
Lennart Poettering47be8702010-02-03 14:21:48 +0100557static int config_parse_facility(
Lennart Poettering071830f2010-01-28 02:06:20 +0100558 const char *filename,
559 unsigned line,
560 const char *section,
561 const char *lvalue,
562 const char *rvalue,
563 void *data,
564 void *userdata) {
565
Lennart Poettering071830f2010-01-28 02:06:20 +0100566
Lennart Poettering94f04342010-01-30 01:55:42 +0100567 int *o = data, x;
Lennart Poettering071830f2010-01-28 02:06:20 +0100568
569 assert(filename);
570 assert(lvalue);
571 assert(rvalue);
572 assert(data);
573
Lennart Poettering0d87eb42010-04-13 02:22:41 +0200574 if ((x = log_facility_from_string(rvalue)) < 0) {
Lennart Poetteringc0b34692010-08-17 03:30:53 +0200575 log_error("[%s:%u] Failed to parse log facility, ignoring: %s", filename, line, rvalue);
576 return 0;
Lennart Poettering0d87eb42010-04-13 02:22:41 +0200577 }
Lennart Poettering94f04342010-01-30 01:55:42 +0100578
579 *o = LOG_MAKEPRI(x, LOG_PRI(*o));
Lennart Poettering071830f2010-01-28 02:06:20 +0100580
581 return 0;
582}
583
Lennart Poettering47be8702010-02-03 14:21:48 +0100584static int config_parse_level(
Lennart Poettering071830f2010-01-28 02:06:20 +0100585 const char *filename,
586 unsigned line,
587 const char *section,
588 const char *lvalue,
589 const char *rvalue,
590 void *data,
591 void *userdata) {
592
Lennart Poettering071830f2010-01-28 02:06:20 +0100593
Lennart Poettering94f04342010-01-30 01:55:42 +0100594 int *o = data, x;
Lennart Poettering071830f2010-01-28 02:06:20 +0100595
596 assert(filename);
597 assert(lvalue);
598 assert(rvalue);
599 assert(data);
600
Lennart Poettering0d87eb42010-04-13 02:22:41 +0200601 if ((x = log_level_from_string(rvalue)) < 0) {
Lennart Poetteringc0b34692010-08-17 03:30:53 +0200602 log_error("[%s:%u] Failed to parse log level, ignoring: %s", filename, line, rvalue);
603 return 0;
Lennart Poettering0d87eb42010-04-13 02:22:41 +0200604 }
Lennart Poettering071830f2010-01-28 02:06:20 +0100605
Lennart Poettering94f04342010-01-30 01:55:42 +0100606 *o = LOG_MAKEPRI(LOG_FAC(*o), x);
Lennart Poettering071830f2010-01-28 02:06:20 +0100607 return 0;
608}
609
Lennart Poettering47be8702010-02-03 14:21:48 +0100610static int config_parse_io_class(
Lennart Poettering9eba9da2010-01-29 20:46:22 +0100611 const char *filename,
612 unsigned line,
613 const char *section,
614 const char *lvalue,
615 const char *rvalue,
616 void *data,
617 void *userdata) {
618
Lennart Poettering9eba9da2010-01-29 20:46:22 +0100619 ExecContext *c = data;
Lennart Poettering94f04342010-01-30 01:55:42 +0100620 int x;
Lennart Poettering9eba9da2010-01-29 20:46:22 +0100621
622 assert(filename);
623 assert(lvalue);
624 assert(rvalue);
625 assert(data);
626
Lennart Poettering0d87eb42010-04-13 02:22:41 +0200627 if ((x = ioprio_class_from_string(rvalue)) < 0) {
Lennart Poetteringc0b34692010-08-17 03:30:53 +0200628 log_error("[%s:%u] Failed to parse IO scheduling class, ignoring: %s", filename, line, rvalue);
629 return 0;
Lennart Poettering0d87eb42010-04-13 02:22:41 +0200630 }
Lennart Poettering9eba9da2010-01-29 20:46:22 +0100631
Lennart Poettering94f04342010-01-30 01:55:42 +0100632 c->ioprio = IOPRIO_PRIO_VALUE(x, IOPRIO_PRIO_DATA(c->ioprio));
Lennart Poettering9eba9da2010-01-29 20:46:22 +0100633 c->ioprio_set = true;
634
635 return 0;
636}
637
Lennart Poettering47be8702010-02-03 14:21:48 +0100638static int config_parse_io_priority(
Lennart Poettering9eba9da2010-01-29 20:46:22 +0100639 const char *filename,
640 unsigned line,
641 const char *section,
642 const char *lvalue,
643 const char *rvalue,
644 void *data,
645 void *userdata) {
646
647 ExecContext *c = data;
648 int i;
649
650 assert(filename);
651 assert(lvalue);
652 assert(rvalue);
653 assert(data);
654
Lennart Poettering94f04342010-01-30 01:55:42 +0100655 if (safe_atoi(rvalue, &i) < 0 || i < 0 || i >= IOPRIO_BE_NR) {
Lennart Poetteringc0b34692010-08-17 03:30:53 +0200656 log_error("[%s:%u] Failed to parse io priority, ignoring: %s", filename, line, rvalue);
657 return 0;
Lennart Poettering9eba9da2010-01-29 20:46:22 +0100658 }
659
Lennart Poettering94f04342010-01-30 01:55:42 +0100660 c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_PRIO_CLASS(c->ioprio), i);
Lennart Poettering9eba9da2010-01-29 20:46:22 +0100661 c->ioprio_set = true;
662
663 return 0;
664}
665
Lennart Poettering47be8702010-02-03 14:21:48 +0100666static int config_parse_cpu_sched_policy(
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
676 ExecContext *c = data;
677 int x;
678
679 assert(filename);
680 assert(lvalue);
681 assert(rvalue);
682 assert(data);
683
Lennart Poettering0d87eb42010-04-13 02:22:41 +0200684 if ((x = sched_policy_from_string(rvalue)) < 0) {
Lennart Poetteringc0b34692010-08-17 03:30:53 +0200685 log_error("[%s:%u] Failed to parse CPU scheduling policy, ignoring: %s", filename, line, rvalue);
686 return 0;
Lennart Poettering0d87eb42010-04-13 02:22:41 +0200687 }
Lennart Poettering94f04342010-01-30 01:55:42 +0100688
689 c->cpu_sched_policy = x;
690 c->cpu_sched_set = true;
691
692 return 0;
693}
694
Lennart Poettering47be8702010-02-03 14:21:48 +0100695static int config_parse_cpu_sched_prio(
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 int i;
706
707 assert(filename);
708 assert(lvalue);
709 assert(rvalue);
710 assert(data);
711
712 /* On Linux RR/FIFO have the same range */
713 if (safe_atoi(rvalue, &i) < 0 || i < sched_get_priority_min(SCHED_RR) || i > sched_get_priority_max(SCHED_RR)) {
Lennart Poetteringc0b34692010-08-17 03:30:53 +0200714 log_error("[%s:%u] Failed to parse CPU scheduling priority, ignoring: %s", filename, line, rvalue);
715 return 0;
Lennart Poettering94f04342010-01-30 01:55:42 +0100716 }
717
718 c->cpu_sched_priority = i;
719 c->cpu_sched_set = true;
720
721 return 0;
722}
723
Lennart Poettering47be8702010-02-03 14:21:48 +0100724static int config_parse_cpu_affinity(
Lennart Poettering94f04342010-01-30 01:55:42 +0100725 const char *filename,
726 unsigned line,
727 const char *section,
728 const char *lvalue,
729 const char *rvalue,
730 void *data,
731 void *userdata) {
732
733 ExecContext *c = data;
734 char *w;
735 size_t l;
736 char *state;
737
738 assert(filename);
739 assert(lvalue);
740 assert(rvalue);
741 assert(data);
742
Lennart Poetteringf60f22d2010-07-07 20:58:41 +0200743 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
Lennart Poettering94f04342010-01-30 01:55:42 +0100744 char *t;
745 int r;
746 unsigned cpu;
747
748 if (!(t = strndup(w, l)))
749 return -ENOMEM;
750
Lennart Poettering487393e2010-07-07 01:10:27 +0200751 r = safe_atou(t, &cpu);
752 free(t);
753
Lennart Poettering82c121a2010-07-04 16:44:58 +0200754 if (!(c->cpuset))
755 if (!(c->cpuset = cpu_set_malloc(&c->cpuset_ncpus)))
756 return -ENOMEM;
757
Lennart Poettering82c121a2010-07-04 16:44:58 +0200758 if (r < 0 || cpu >= c->cpuset_ncpus) {
Lennart Poetteringc0b34692010-08-17 03:30:53 +0200759 log_error("[%s:%u] Failed to parse CPU affinity, ignoring: %s", filename, line, rvalue);
760 return 0;
Lennart Poettering94f04342010-01-30 01:55:42 +0100761 }
762
Lennart Poettering82c121a2010-07-04 16:44:58 +0200763 CPU_SET_S(cpu, CPU_ALLOC_SIZE(c->cpuset_ncpus), c->cpuset);
Lennart Poettering94f04342010-01-30 01:55:42 +0100764 }
765
Lennart Poettering94f04342010-01-30 01:55:42 +0100766 return 0;
767}
768
Lennart Poettering47be8702010-02-03 14:21:48 +0100769static int config_parse_capabilities(
Lennart Poettering94f04342010-01-30 01:55:42 +0100770 const char *filename,
771 unsigned line,
772 const char *section,
773 const char *lvalue,
774 const char *rvalue,
775 void *data,
776 void *userdata) {
777
778 ExecContext *c = data;
779 cap_t cap;
780
781 assert(filename);
782 assert(lvalue);
783 assert(rvalue);
784 assert(data);
785
786 if (!(cap = cap_from_text(rvalue))) {
787 if (errno == ENOMEM)
788 return -ENOMEM;
789
Lennart Poetteringc0b34692010-08-17 03:30:53 +0200790 log_error("[%s:%u] Failed to parse capabilities, ignoring: %s", filename, line, rvalue);
791 return 0;
Lennart Poettering94f04342010-01-30 01:55:42 +0100792 }
793
794 if (c->capabilities)
795 cap_free(c->capabilities);
796 c->capabilities = cap;
797
798 return 0;
799}
800
Lennart Poettering47be8702010-02-03 14:21:48 +0100801static int config_parse_secure_bits(
Lennart Poettering94f04342010-01-30 01:55:42 +0100802 const char *filename,
803 unsigned line,
804 const char *section,
805 const char *lvalue,
806 const char *rvalue,
807 void *data,
808 void *userdata) {
809
810 ExecContext *c = data;
811 char *w;
812 size_t l;
813 char *state;
814
815 assert(filename);
816 assert(lvalue);
817 assert(rvalue);
818 assert(data);
819
Lennart Poetteringf60f22d2010-07-07 20:58:41 +0200820 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
Lennart Poettering94f04342010-01-30 01:55:42 +0100821 if (first_word(w, "keep-caps"))
822 c->secure_bits |= SECURE_KEEP_CAPS;
823 else if (first_word(w, "keep-caps-locked"))
824 c->secure_bits |= SECURE_KEEP_CAPS_LOCKED;
825 else if (first_word(w, "no-setuid-fixup"))
826 c->secure_bits |= SECURE_NO_SETUID_FIXUP;
827 else if (first_word(w, "no-setuid-fixup-locked"))
828 c->secure_bits |= SECURE_NO_SETUID_FIXUP_LOCKED;
829 else if (first_word(w, "noroot"))
830 c->secure_bits |= SECURE_NOROOT;
831 else if (first_word(w, "noroot-locked"))
832 c->secure_bits |= SECURE_NOROOT_LOCKED;
833 else {
Lennart Poetteringc0b34692010-08-17 03:30:53 +0200834 log_error("[%s:%u] Failed to parse secure bits, ignoring: %s", filename, line, rvalue);
835 return 0;
Lennart Poettering94f04342010-01-30 01:55:42 +0100836 }
837 }
838
839 return 0;
840}
841
Lennart Poettering47be8702010-02-03 14:21:48 +0100842static int config_parse_bounding_set(
Lennart Poettering94f04342010-01-30 01:55:42 +0100843 const char *filename,
844 unsigned line,
845 const char *section,
846 const char *lvalue,
847 const char *rvalue,
848 void *data,
849 void *userdata) {
850
851 ExecContext *c = data;
852 char *w;
853 size_t l;
854 char *state;
855
856 assert(filename);
857 assert(lvalue);
858 assert(rvalue);
859 assert(data);
860
Lennart Poetteringf60f22d2010-07-07 20:58:41 +0200861 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
Lennart Poettering94f04342010-01-30 01:55:42 +0100862 char *t;
863 int r;
864 cap_value_t cap;
865
866 if (!(t = strndup(w, l)))
867 return -ENOMEM;
868
869 r = cap_from_name(t, &cap);
870 free(t);
871
872 if (r < 0) {
Lennart Poetteringc0b34692010-08-17 03:30:53 +0200873 log_error("[%s:%u] Failed to parse capability bounding set, ignoring: %s", filename, line, rvalue);
874 return 0;
Lennart Poettering94f04342010-01-30 01:55:42 +0100875 }
876
877 c->capability_bounding_set_drop |= 1 << cap;
878 }
879
880 return 0;
881}
882
Lennart Poettering03fae012010-07-04 21:12:10 +0200883static int config_parse_timer_slack_nsec(
Lennart Poettering94f04342010-01-30 01:55:42 +0100884 const char *filename,
885 unsigned line,
886 const char *section,
887 const char *lvalue,
888 const char *rvalue,
889 void *data,
890 void *userdata) {
891
892 ExecContext *c = data;
893 unsigned long u;
Lennart Poettering94f04342010-01-30 01:55:42 +0100894
895 assert(filename);
896 assert(lvalue);
897 assert(rvalue);
898 assert(data);
899
Lennart Poetteringbd40a2d2011-01-22 02:18:59 +0100900 if (safe_atolu(rvalue, &u) < 0) {
Lennart Poetteringc0b34692010-08-17 03:30:53 +0200901 log_error("[%s:%u] Failed to parse time slack value, ignoring: %s", filename, line, rvalue);
902 return 0;
Lennart Poettering94f04342010-01-30 01:55:42 +0100903 }
904
Lennart Poettering03fae012010-07-04 21:12:10 +0200905 c->timer_slack_nsec = u;
Lennart Poettering94f04342010-01-30 01:55:42 +0100906
907 return 0;
908}
909
910static int config_parse_limit(
911 const char *filename,
912 unsigned line,
913 const char *section,
914 const char *lvalue,
915 const char *rvalue,
916 void *data,
917 void *userdata) {
918
919 struct rlimit **rl = data;
920 unsigned long long u;
Lennart Poettering94f04342010-01-30 01:55:42 +0100921
922 assert(filename);
923 assert(lvalue);
924 assert(rvalue);
925 assert(data);
926
Lennart Poetteringbd40a2d2011-01-22 02:18:59 +0100927 if (safe_atollu(rvalue, &u) < 0) {
Lennart Poetteringc0b34692010-08-17 03:30:53 +0200928 log_error("[%s:%u] Failed to parse resource value, ignoring: %s", filename, line, rvalue);
929 return 0;
Lennart Poettering94f04342010-01-30 01:55:42 +0100930 }
931
932 if (!*rl)
933 if (!(*rl = new(struct rlimit, 1)))
934 return -ENOMEM;
935
936 (*rl)->rlim_cur = (*rl)->rlim_max = (rlim_t) u;
937 return 0;
938}
939
Lennart Poettering8e274522010-03-31 16:29:55 +0200940static int config_parse_cgroup(
941 const char *filename,
942 unsigned line,
943 const char *section,
944 const char *lvalue,
945 const char *rvalue,
946 void *data,
947 void *userdata) {
948
949 Unit *u = userdata;
950 char *w;
951 size_t l;
952 char *state;
953
Lennart Poetteringf60f22d2010-07-07 20:58:41 +0200954 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
Lennart Poettering8e274522010-03-31 16:29:55 +0200955 char *t;
956 int r;
957
Lennart Poetteringf60f22d2010-07-07 20:58:41 +0200958 if (!(t = cunescape_length(w, l)))
Lennart Poettering8e274522010-03-31 16:29:55 +0200959 return -ENOMEM;
960
961 r = unit_add_cgroup_from_text(u, t);
962 free(t);
963
Lennart Poetteringc0b34692010-08-17 03:30:53 +0200964 if (r < 0) {
965 log_error("[%s:%u] Failed to parse cgroup value, ignoring: %s", filename, line, rvalue);
966 return 0;
967 }
Lennart Poettering8e274522010-03-31 16:29:55 +0200968 }
969
970 return 0;
971}
972
Fabiano Fidencio07459bb2010-09-21 00:23:12 -0300973#ifdef HAVE_SYSV_COMPAT
Lennart Poetteringa9a1e002010-04-06 02:40:10 +0200974static int config_parse_sysv_priority(
975 const char *filename,
976 unsigned line,
977 const char *section,
978 const char *lvalue,
979 const char *rvalue,
980 void *data,
981 void *userdata) {
982
983 int *priority = data;
Lennart Poetteringbd40a2d2011-01-22 02:18:59 +0100984 int i;
Lennart Poetteringa9a1e002010-04-06 02:40:10 +0200985
986 assert(filename);
987 assert(lvalue);
988 assert(rvalue);
989 assert(data);
990
Lennart Poetteringbd40a2d2011-01-22 02:18:59 +0100991 if (safe_atoi(rvalue, &i) < 0 || i < 0) {
Lennart Poetteringc0b34692010-08-17 03:30:53 +0200992 log_error("[%s:%u] Failed to parse SysV start priority, ignoring: %s", filename, line, rvalue);
993 return 0;
Lennart Poetteringa9a1e002010-04-06 02:40:10 +0200994 }
995
996 *priority = (int) i;
997 return 0;
998}
Fabiano Fidencio07459bb2010-09-21 00:23:12 -0300999#endif
Lennart Poetteringa9a1e002010-04-06 02:40:10 +02001000
Lennart Poettering2ba545f2010-10-20 14:22:23 +02001001static int config_parse_fsck_passno(
1002 const char *filename,
1003 unsigned line,
1004 const char *section,
1005 const char *lvalue,
1006 const char *rvalue,
1007 void *data,
1008 void *userdata) {
1009
1010 int *passno = data;
Lennart Poetteringbd40a2d2011-01-22 02:18:59 +01001011 int i;
Lennart Poettering2ba545f2010-10-20 14:22:23 +02001012
1013 assert(filename);
1014 assert(lvalue);
1015 assert(rvalue);
1016 assert(data);
1017
Lennart Poetteringbd40a2d2011-01-22 02:18:59 +01001018 if (safe_atoi(rvalue, &i) || i < 0) {
Lennart Poettering2ba545f2010-10-20 14:22:23 +02001019 log_error("[%s:%u] Failed to parse fsck pass number, ignoring: %s", filename, line, rvalue);
1020 return 0;
1021 }
1022
1023 *passno = (int) i;
1024 return 0;
1025}
1026
Lennart Poettering487393e2010-07-07 01:10:27 +02001027static DEFINE_CONFIG_PARSE_ENUM(config_parse_kill_mode, kill_mode, KillMode, "Failed to parse kill mode");
Lennart Poettering50159e62010-04-08 00:52:14 +02001028
Lennart Poettering2e22afe2010-07-10 04:49:37 +02001029static int config_parse_kill_signal(
1030 const char *filename,
1031 unsigned line,
1032 const char *section,
1033 const char *lvalue,
1034 const char *rvalue,
1035 void *data,
1036 void *userdata) {
1037
1038 int *sig = data;
1039 int r;
1040
1041 assert(filename);
1042 assert(lvalue);
1043 assert(rvalue);
1044 assert(sig);
1045
Lennart Poettering8a0867d2010-10-22 16:11:50 +02001046 if ((r = signal_from_string_try_harder(rvalue)) <= 0) {
Lennart Poetteringc0b34692010-08-17 03:30:53 +02001047 log_error("[%s:%u] Failed to parse kill signal, ignoring: %s", filename, line, rvalue);
1048 return 0;
Lennart Poettering2e22afe2010-07-10 04:49:37 +02001049 }
1050
1051 *sig = r;
1052 return 0;
1053}
1054
Lennart Poettering15ae4222010-04-21 22:15:06 +02001055static int config_parse_mount_flags(
1056 const char *filename,
1057 unsigned line,
1058 const char *section,
1059 const char *lvalue,
1060 const char *rvalue,
1061 void *data,
1062 void *userdata) {
1063
1064 ExecContext *c = data;
1065 char *w;
1066 size_t l;
1067 char *state;
1068 unsigned long flags = 0;
1069
1070 assert(filename);
1071 assert(lvalue);
1072 assert(rvalue);
1073 assert(data);
1074
Lennart Poetteringf60f22d2010-07-07 20:58:41 +02001075 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
Lennart Poetteringf90cf442010-11-14 19:58:33 +01001076 if (strncmp(w, "shared", MAX(l, 6U)) == 0)
Lennart Poettering15ae4222010-04-21 22:15:06 +02001077 flags |= MS_SHARED;
Lennart Poetteringf90cf442010-11-14 19:58:33 +01001078 else if (strncmp(w, "slave", MAX(l, 5U)) == 0)
Lennart Poettering15ae4222010-04-21 22:15:06 +02001079 flags |= MS_SLAVE;
Lennart Poetteringf90cf442010-11-14 19:58:33 +01001080 else if (strncmp(w, "private", MAX(l, 7U)) == 0)
Lennart Poettering15ae4222010-04-21 22:15:06 +02001081 flags |= MS_PRIVATE;
1082 else {
Lennart Poetteringc0b34692010-08-17 03:30:53 +02001083 log_error("[%s:%u] Failed to parse mount flags, ignoring: %s", filename, line, rvalue);
1084 return 0;
Lennart Poettering15ae4222010-04-21 22:15:06 +02001085 }
1086 }
1087
1088 c->mount_flags = flags;
1089 return 0;
1090}
1091
Lennart Poettering871d7de2010-05-24 01:45:54 +02001092static int config_parse_timer(
1093 const char *filename,
1094 unsigned line,
1095 const char *section,
1096 const char *lvalue,
1097 const char *rvalue,
1098 void *data,
1099 void *userdata) {
1100
1101 Timer *t = data;
1102 usec_t u;
Lennart Poettering871d7de2010-05-24 01:45:54 +02001103 TimerValue *v;
1104 TimerBase b;
1105
1106 assert(filename);
1107 assert(lvalue);
1108 assert(rvalue);
1109 assert(data);
1110
1111 if ((b = timer_base_from_string(lvalue)) < 0) {
Lennart Poetteringc0b34692010-08-17 03:30:53 +02001112 log_error("[%s:%u] Failed to parse timer base, ignoring: %s", filename, line, lvalue);
1113 return 0;
Lennart Poettering871d7de2010-05-24 01:45:54 +02001114 }
1115
Lennart Poetteringbd40a2d2011-01-22 02:18:59 +01001116 if (parse_usec(rvalue, &u) < 0) {
Lennart Poetteringc0b34692010-08-17 03:30:53 +02001117 log_error("[%s:%u] Failed to parse timer value, ignoring: %s", filename, line, rvalue);
1118 return 0;
Lennart Poettering871d7de2010-05-24 01:45:54 +02001119 }
1120
1121 if (!(v = new0(TimerValue, 1)))
1122 return -ENOMEM;
1123
1124 v->base = b;
1125 v->value = u;
1126
1127 LIST_PREPEND(TimerValue, value, t->values, v);
1128
1129 return 0;
1130}
1131
1132static int config_parse_timer_unit(
1133 const char *filename,
1134 unsigned line,
1135 const char *section,
1136 const char *lvalue,
1137 const char *rvalue,
1138 void *data,
1139 void *userdata) {
1140
1141 Timer *t = data;
1142 int r;
Lennart Poettering398ef8b2010-07-08 02:43:18 +02001143 DBusError error;
1144
1145 assert(filename);
1146 assert(lvalue);
1147 assert(rvalue);
1148 assert(data);
1149
1150 dbus_error_init(&error);
Lennart Poettering871d7de2010-05-24 01:45:54 +02001151
1152 if (endswith(rvalue, ".timer")) {
Lennart Poetteringc0b34692010-08-17 03:30:53 +02001153 log_error("[%s:%u] Unit cannot be of type timer, ignoring: %s", filename, line, rvalue);
1154 return 0;
Lennart Poettering871d7de2010-05-24 01:45:54 +02001155 }
1156
Lennart Poettering398ef8b2010-07-08 02:43:18 +02001157 if ((r = manager_load_unit(t->meta.manager, rvalue, NULL, NULL, &t->unit)) < 0) {
Lennart Poetteringc0b34692010-08-17 03:30:53 +02001158 log_error("[%s:%u] Failed to load unit %s, ignoring: %s", filename, line, rvalue, bus_error(&error, r));
Lennart Poettering398ef8b2010-07-08 02:43:18 +02001159 dbus_error_free(&error);
Lennart Poetteringc0b34692010-08-17 03:30:53 +02001160 return 0;
Lennart Poettering871d7de2010-05-24 01:45:54 +02001161 }
1162
1163 return 0;
1164}
1165
Lennart Poettering01f78472010-05-24 05:25:33 +02001166static int config_parse_path_spec(
1167 const char *filename,
1168 unsigned line,
1169 const char *section,
1170 const char *lvalue,
1171 const char *rvalue,
1172 void *data,
1173 void *userdata) {
1174
1175 Path *p = data;
1176 PathSpec *s;
1177 PathType b;
1178
1179 assert(filename);
1180 assert(lvalue);
1181 assert(rvalue);
1182 assert(data);
1183
1184 if ((b = path_type_from_string(lvalue)) < 0) {
Lennart Poetteringc0b34692010-08-17 03:30:53 +02001185 log_error("[%s:%u] Failed to parse path type, ignoring: %s", filename, line, lvalue);
1186 return 0;
Lennart Poettering01f78472010-05-24 05:25:33 +02001187 }
1188
1189 if (!path_is_absolute(rvalue)) {
Lennart Poetteringc0b34692010-08-17 03:30:53 +02001190 log_error("[%s:%u] Path is not absolute, ignoring: %s", filename, line, rvalue);
1191 return 0;
Lennart Poettering01f78472010-05-24 05:25:33 +02001192 }
1193
1194 if (!(s = new0(PathSpec, 1)))
1195 return -ENOMEM;
1196
1197 if (!(s->path = strdup(rvalue))) {
1198 free(s);
1199 return -ENOMEM;
1200 }
1201
1202 path_kill_slashes(s->path);
1203
1204 s->type = b;
1205 s->inotify_fd = -1;
1206
1207 LIST_PREPEND(PathSpec, spec, p->specs, s);
1208
1209 return 0;
1210}
1211
1212static int config_parse_path_unit(
1213 const char *filename,
1214 unsigned line,
1215 const char *section,
1216 const char *lvalue,
1217 const char *rvalue,
1218 void *data,
1219 void *userdata) {
1220
1221 Path *t = data;
1222 int r;
Lennart Poettering398ef8b2010-07-08 02:43:18 +02001223 DBusError error;
1224
1225 assert(filename);
1226 assert(lvalue);
1227 assert(rvalue);
1228 assert(data);
1229
1230 dbus_error_init(&error);
Lennart Poettering01f78472010-05-24 05:25:33 +02001231
1232 if (endswith(rvalue, ".path")) {
Lennart Poetteringc0b34692010-08-17 03:30:53 +02001233 log_error("[%s:%u] Unit cannot be of type path, ignoring: %s", filename, line, rvalue);
1234 return 0;
Lennart Poettering01f78472010-05-24 05:25:33 +02001235 }
1236
Lennart Poettering398ef8b2010-07-08 02:43:18 +02001237 if ((r = manager_load_unit(t->meta.manager, rvalue, NULL, &error, &t->unit)) < 0) {
Lennart Poetteringc0b34692010-08-17 03:30:53 +02001238 log_error("[%s:%u] Failed to load unit %s, ignoring: %s", filename, line, rvalue, bus_error(&error, r));
Lennart Poettering398ef8b2010-07-08 02:43:18 +02001239 dbus_error_free(&error);
Lennart Poetteringc0b34692010-08-17 03:30:53 +02001240 return 0;
Lennart Poettering01f78472010-05-24 05:25:33 +02001241 }
1242
1243 return 0;
1244}
1245
Lennart Poetteringd9ff3212010-09-30 02:19:12 +02001246static int config_parse_socket_service(
1247 const char *filename,
1248 unsigned line,
1249 const char *section,
1250 const char *lvalue,
1251 const char *rvalue,
1252 void *data,
1253 void *userdata) {
1254
1255 Socket *s = data;
1256 int r;
1257 DBusError error;
1258
1259 assert(filename);
1260 assert(lvalue);
1261 assert(rvalue);
1262 assert(data);
1263
1264 dbus_error_init(&error);
1265
Lennart Poetteringf976f3f2010-10-05 19:49:15 +02001266 if (!endswith(rvalue, ".service")) {
1267 log_error("[%s:%u] Unit must be of type service, ignoring: %s", filename, line, rvalue);
Lennart Poetteringd9ff3212010-09-30 02:19:12 +02001268 return 0;
1269 }
1270
1271 if ((r = manager_load_unit(s->meta.manager, rvalue, NULL, &error, (Unit**) &s->service)) < 0) {
1272 log_error("[%s:%u] Failed to load unit %s, ignoring: %s", filename, line, rvalue, bus_error(&error, r));
1273 dbus_error_free(&error);
1274 return 0;
1275 }
1276
1277 return 0;
1278}
1279
Lennart Poetteringf976f3f2010-10-05 19:49:15 +02001280static int config_parse_service_sockets(
1281 const char *filename,
1282 unsigned line,
1283 const char *section,
1284 const char *lvalue,
1285 const char *rvalue,
1286 void *data,
1287 void *userdata) {
1288
1289 Service *s = data;
1290 int r;
1291 DBusError error;
1292 char *state, *w;
1293 size_t l;
1294
1295 assert(filename);
1296 assert(lvalue);
1297 assert(rvalue);
1298 assert(data);
1299
1300 dbus_error_init(&error);
1301
1302 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
1303 char *t;
1304 Unit *sock;
1305
1306 if (!(t = strndup(w, l)))
1307 return -ENOMEM;
1308
1309 if (!endswith(t, ".socket")) {
1310 log_error("[%s:%u] Unit must be of type socket, ignoring: %s", filename, line, rvalue);
1311 free(t);
1312 continue;
1313 }
1314
1315 r = manager_load_unit(s->meta.manager, t, NULL, &error, &sock);
1316 free(t);
1317
1318 if (r < 0) {
1319 log_error("[%s:%u] Failed to load unit %s, ignoring: %s", filename, line, rvalue, bus_error(&error, r));
1320 dbus_error_free(&error);
1321 continue;
1322 }
1323
1324 if ((r = set_ensure_allocated(&s->configured_sockets, trivial_hash_func, trivial_compare_func)) < 0)
1325 return r;
1326
1327 if ((r = set_put(s->configured_sockets, sock)) < 0)
1328 return r;
1329 }
1330
1331 return 0;
1332}
1333
Lennart Poetteringddb26e12010-06-18 06:06:24 +02001334static int config_parse_env_file(
1335 const char *filename,
1336 unsigned line,
1337 const char *section,
1338 const char *lvalue,
1339 const char *rvalue,
1340 void *data,
1341 void *userdata) {
1342
Lennart Poettering8c7be952011-03-04 03:44:43 +01001343 char ***env = data, **k;
Lennart Poetteringddb26e12010-06-18 06:06:24 +02001344
1345 assert(filename);
1346 assert(lvalue);
1347 assert(rvalue);
1348 assert(data);
1349
Lennart Poettering8c7be952011-03-04 03:44:43 +01001350 if (!path_is_absolute(rvalue[0] == '-' ? rvalue + 1 : rvalue)) {
Lennart Poetteringafe4bfe2011-01-06 01:39:08 +01001351 log_error("[%s:%u] Path '%s' is not absolute, ignoring.", filename, line, rvalue);
1352 return 0;
1353 }
1354
Lennart Poettering8c7be952011-03-04 03:44:43 +01001355 if (!(k = strv_append(*env, rvalue)))
1356 return -ENOMEM;
Lennart Poetteringddb26e12010-06-18 06:06:24 +02001357
Lennart Poettering8c7be952011-03-04 03:44:43 +01001358 strv_free(*env);
1359 *env = k;
Lennart Poetteringddb26e12010-06-18 06:06:24 +02001360
Lennart Poettering8c7be952011-03-04 03:44:43 +01001361 return 0;
Lennart Poetteringddb26e12010-06-18 06:06:24 +02001362}
1363
Lennart Poettering4fd59482010-07-01 00:29:17 +02001364static int config_parse_ip_tos(
1365 const char *filename,
1366 unsigned line,
1367 const char *section,
1368 const char *lvalue,
1369 const char *rvalue,
1370 void *data,
1371 void *userdata) {
1372
1373 int *ip_tos = data, x;
Lennart Poettering4fd59482010-07-01 00:29:17 +02001374
1375 assert(filename);
1376 assert(lvalue);
1377 assert(rvalue);
1378 assert(data);
1379
1380 if ((x = ip_tos_from_string(rvalue)) < 0)
Lennart Poetteringbd40a2d2011-01-22 02:18:59 +01001381 if (safe_atoi(rvalue, &x) < 0) {
Lennart Poetteringc0b34692010-08-17 03:30:53 +02001382 log_error("[%s:%u] Failed to parse IP TOS value, ignoring: %s", filename, line, rvalue);
1383 return 0;
Lennart Poettering4fd59482010-07-01 00:29:17 +02001384 }
1385
1386 *ip_tos = x;
1387 return 0;
1388}
1389
Lennart Poettering52661ef2010-10-13 02:15:41 +02001390static int config_parse_condition_path(
1391 const char *filename,
1392 unsigned line,
1393 const char *section,
1394 const char *lvalue,
1395 const char *rvalue,
1396 void *data,
1397 void *userdata) {
1398
1399 Unit *u = data;
Lennart Poettering267632f2011-03-08 03:04:47 +01001400 bool trigger, negate;
Lennart Poettering52661ef2010-10-13 02:15:41 +02001401 Condition *c;
1402
1403 assert(filename);
1404 assert(lvalue);
1405 assert(rvalue);
1406 assert(data);
1407
Lennart Poettering267632f2011-03-08 03:04:47 +01001408 if ((trigger = rvalue[0] == '|'))
1409 rvalue++;
1410
Lennart Poettering52661ef2010-10-13 02:15:41 +02001411 if ((negate = rvalue[0] == '!'))
1412 rvalue++;
1413
1414 if (!path_is_absolute(rvalue)) {
Lennart Poetteringd257dde2010-11-10 22:28:19 +01001415 log_error("[%s:%u] Path in condition not absolute, ignoring: %s", filename, line, rvalue);
Lennart Poettering52661ef2010-10-13 02:15:41 +02001416 return 0;
1417 }
1418
Lennart Poettering36af55d2010-11-15 20:06:49 +01001419 if (!(c = condition_new(streq(lvalue, "ConditionPathExists") ? CONDITION_PATH_EXISTS : CONDITION_DIRECTORY_NOT_EMPTY,
Lennart Poettering267632f2011-03-08 03:04:47 +01001420 rvalue, trigger, negate)))
Lennart Poettering52661ef2010-10-13 02:15:41 +02001421 return -ENOMEM;
1422
1423 LIST_PREPEND(Condition, conditions, u->meta.conditions, c);
1424 return 0;
1425}
1426
1427static int config_parse_condition_kernel(
1428 const char *filename,
1429 unsigned line,
1430 const char *section,
1431 const char *lvalue,
1432 const char *rvalue,
1433 void *data,
1434 void *userdata) {
1435
1436 Unit *u = data;
Lennart Poettering267632f2011-03-08 03:04:47 +01001437 bool trigger, negate;
Lennart Poettering52661ef2010-10-13 02:15:41 +02001438 Condition *c;
1439
1440 assert(filename);
1441 assert(lvalue);
1442 assert(rvalue);
1443 assert(data);
1444
Lennart Poettering267632f2011-03-08 03:04:47 +01001445 if ((trigger = rvalue[0] == '|'))
1446 rvalue++;
1447
Lennart Poettering52661ef2010-10-13 02:15:41 +02001448 if ((negate = rvalue[0] == '!'))
1449 rvalue++;
1450
Lennart Poettering267632f2011-03-08 03:04:47 +01001451 if (!(c = condition_new(CONDITION_KERNEL_COMMAND_LINE, rvalue, trigger, negate)))
Lennart Poettering52661ef2010-10-13 02:15:41 +02001452 return -ENOMEM;
1453
1454 LIST_PREPEND(Condition, conditions, u->meta.conditions, c);
1455 return 0;
1456}
1457
Lennart Poettering039655a2011-02-21 22:07:55 +01001458static int config_parse_condition_virt(
1459 const char *filename,
1460 unsigned line,
1461 const char *section,
1462 const char *lvalue,
1463 const char *rvalue,
1464 void *data,
1465 void *userdata) {
1466
1467 Unit *u = data;
Lennart Poettering267632f2011-03-08 03:04:47 +01001468 bool trigger, negate;
Lennart Poettering039655a2011-02-21 22:07:55 +01001469 Condition *c;
1470
1471 assert(filename);
1472 assert(lvalue);
1473 assert(rvalue);
1474 assert(data);
1475
Lennart Poettering267632f2011-03-08 03:04:47 +01001476 if ((trigger = rvalue[0] == '|'))
1477 rvalue++;
1478
Lennart Poettering039655a2011-02-21 22:07:55 +01001479 if ((negate = rvalue[0] == '!'))
1480 rvalue++;
1481
Lennart Poettering267632f2011-03-08 03:04:47 +01001482 if (!(c = condition_new(CONDITION_VIRTUALIZATION, rvalue, trigger, negate)))
Lennart Poettering039655a2011-02-21 22:07:55 +01001483 return -ENOMEM;
1484
1485 LIST_PREPEND(Condition, conditions, u->meta.conditions, c);
1486 return 0;
1487}
1488
Lennart Poetteringd257dde2010-11-10 22:28:19 +01001489static int config_parse_condition_null(
1490 const char *filename,
1491 unsigned line,
1492 const char *section,
1493 const char *lvalue,
1494 const char *rvalue,
1495 void *data,
1496 void *userdata) {
1497
1498 Unit *u = data;
1499 Condition *c;
Lennart Poettering267632f2011-03-08 03:04:47 +01001500 bool trigger, negate;
Lennart Poetteringd257dde2010-11-10 22:28:19 +01001501 int b;
1502
1503 assert(filename);
1504 assert(lvalue);
1505 assert(rvalue);
1506 assert(data);
1507
Lennart Poettering267632f2011-03-08 03:04:47 +01001508 if ((trigger = rvalue[0] == '|'))
1509 rvalue++;
1510
Lennart Poetteringd257dde2010-11-10 22:28:19 +01001511 if ((negate = rvalue[0] == '!'))
1512 rvalue++;
1513
1514 if ((b = parse_boolean(rvalue)) < 0) {
1515 log_error("[%s:%u] Failed to parse boolean value in condition, ignoring: %s", filename, line, rvalue);
1516 return 0;
1517 }
1518
1519 if (!b)
1520 negate = !negate;
1521
Lennart Poettering267632f2011-03-08 03:04:47 +01001522 if (!(c = condition_new(CONDITION_NULL, NULL, trigger, negate)))
Lennart Poetteringd257dde2010-11-10 22:28:19 +01001523 return -ENOMEM;
1524
1525 LIST_PREPEND(Condition, conditions, u->meta.conditions, c);
1526 return 0;
1527}
1528
Lennart Poettering487393e2010-07-07 01:10:27 +02001529static DEFINE_CONFIG_PARSE_ENUM(config_parse_notify_access, notify_access, NotifyAccess, "Failed to parse notify access specifier");
Lennart Poetteringc952c6e2010-06-18 23:12:48 +02001530
Lennart Poettering071830f2010-01-28 02:06:20 +01001531#define FOLLOW_MAX 8
Lennart Poettering87f0e412010-01-26 21:39:06 +01001532
Lennart Poettering9e2f7c12010-04-15 03:11:11 +02001533static int open_follow(char **filename, FILE **_f, Set *names, char **_final) {
Lennart Poettering0301abf2010-01-27 00:15:56 +01001534 unsigned c = 0;
Lennart Poettering87f0e412010-01-26 21:39:06 +01001535 int fd, r;
1536 FILE *f;
Lennart Poettering0301abf2010-01-27 00:15:56 +01001537 char *id = NULL;
Lennart Poettering87f0e412010-01-26 21:39:06 +01001538
1539 assert(filename);
1540 assert(*filename);
1541 assert(_f);
1542 assert(names);
1543
Lennart Poettering0301abf2010-01-27 00:15:56 +01001544 /* This will update the filename pointer if the loaded file is
1545 * reached by a symlink. The old string will be freed. */
Lennart Poettering87f0e412010-01-26 21:39:06 +01001546
Lennart Poettering0301abf2010-01-27 00:15:56 +01001547 for (;;) {
Lennart Poettering2c7108c2010-06-16 01:56:00 +02001548 char *target, *name;
Lennart Poettering87f0e412010-01-26 21:39:06 +01001549
Lennart Poettering0301abf2010-01-27 00:15:56 +01001550 if (c++ >= FOLLOW_MAX)
1551 return -ELOOP;
1552
Lennart Poetteringb08d03f2010-01-29 02:07:41 +01001553 path_kill_slashes(*filename);
1554
Lennart Poettering87f0e412010-01-26 21:39:06 +01001555 /* Add the file name we are currently looking at to
Lennart Poettering8f054242010-07-21 03:13:15 +02001556 * the names of this unit, but only if it is a valid
1557 * unit name. */
Lennart Poettering0301abf2010-01-27 00:15:56 +01001558 name = file_name_from_path(*filename);
Lennart Poettering87f0e412010-01-26 21:39:06 +01001559
Lennart Poetteringb9c0d442010-10-08 03:09:25 +02001560 if (unit_name_is_valid(name, false)) {
Lennart Poettering8f054242010-07-21 03:13:15 +02001561 if (!(id = set_get(names, name))) {
Lennart Poettering0301abf2010-01-27 00:15:56 +01001562
Lennart Poettering8f054242010-07-21 03:13:15 +02001563 if (!(id = strdup(name)))
1564 return -ENOMEM;
1565
1566 if ((r = set_put(names, id)) < 0) {
1567 free(id);
1568 return r;
1569 }
Lennart Poettering87f0e412010-01-26 21:39:06 +01001570 }
Lennart Poettering87f0e412010-01-26 21:39:06 +01001571 }
1572
Lennart Poettering0301abf2010-01-27 00:15:56 +01001573 /* Try to open the file name, but don't if its a symlink */
1574 if ((fd = open(*filename, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW)) >= 0)
Lennart Poettering87f0e412010-01-26 21:39:06 +01001575 break;
1576
Lennart Poettering0301abf2010-01-27 00:15:56 +01001577 if (errno != ELOOP)
1578 return -errno;
Lennart Poettering87f0e412010-01-26 21:39:06 +01001579
Lennart Poettering0301abf2010-01-27 00:15:56 +01001580 /* Hmm, so this is a symlink. Let's read the name, and follow it manually */
Lennart Poettering2c7108c2010-06-16 01:56:00 +02001581 if ((r = readlink_and_make_absolute(*filename, &target)) < 0)
Lennart Poettering0301abf2010-01-27 00:15:56 +01001582 return r;
1583
Lennart Poettering0301abf2010-01-27 00:15:56 +01001584 free(*filename);
Lennart Poettering2c7108c2010-06-16 01:56:00 +02001585 *filename = target;
Lennart Poettering87f0e412010-01-26 21:39:06 +01001586 }
1587
Lennart Poettering8f054242010-07-21 03:13:15 +02001588 if (!(f = fdopen(fd, "re"))) {
Lennart Poettering87f0e412010-01-26 21:39:06 +01001589 r = -errno;
Lennart Poettering9e2f7c12010-04-15 03:11:11 +02001590 close_nointr_nofail(fd);
Lennart Poettering0301abf2010-01-27 00:15:56 +01001591 return r;
Lennart Poettering87f0e412010-01-26 21:39:06 +01001592 }
1593
1594 *_f = f;
Lennart Poettering9e2f7c12010-04-15 03:11:11 +02001595 *_final = id;
Lennart Poettering0301abf2010-01-27 00:15:56 +01001596 return 0;
Lennart Poettering87f0e412010-01-26 21:39:06 +01001597}
1598
Lennart Poettering23a177e2010-04-06 02:43:58 +02001599static int merge_by_names(Unit **u, Set *names, const char *id) {
1600 char *k;
1601 int r;
1602
1603 assert(u);
1604 assert(*u);
1605 assert(names);
1606
1607 /* Let's try to add in all symlink names we found */
1608 while ((k = set_steal_first(names))) {
1609
1610 /* First try to merge in the other name into our
1611 * unit */
1612 if ((r = unit_merge_by_name(*u, k)) < 0) {
1613 Unit *other;
1614
1615 /* Hmm, we couldn't merge the other unit into
1616 * ours? Then let's try it the other way
1617 * round */
1618
1619 other = manager_get_unit((*u)->meta.manager, k);
1620 free(k);
1621
1622 if (other)
1623 if ((r = unit_merge(other, *u)) >= 0) {
1624 *u = other;
1625 return merge_by_names(u, names, NULL);
1626 }
1627
1628 return r;
1629 }
1630
1631 if (id == k)
1632 unit_choose_id(*u, id);
1633
1634 free(k);
1635 }
1636
1637 return 0;
1638}
1639
Lennart Poetteringe5373522010-04-10 17:53:17 +02001640static void dump_items(FILE *f, const ConfigItem *items) {
1641 const ConfigItem *i;
1642 const char *prev_section = NULL;
1643 bool not_first = false;
1644
1645 struct {
1646 ConfigParserCallback callback;
1647 const char *rvalue;
1648 } table[] = {
1649 { config_parse_int, "INTEGER" },
1650 { config_parse_unsigned, "UNSIGNED" },
1651 { config_parse_size, "SIZE" },
1652 { config_parse_bool, "BOOLEAN" },
1653 { config_parse_string, "STRING" },
1654 { config_parse_path, "PATH" },
1655 { config_parse_strv, "STRING [...]" },
1656 { config_parse_nice, "NICE" },
Lennart Poetteringdd6c17b2010-08-31 01:33:39 +02001657 { config_parse_oom_score_adjust, "OOMSCOREADJUST" },
Lennart Poetteringe5373522010-04-10 17:53:17 +02001658 { config_parse_io_class, "IOCLASS" },
1659 { config_parse_io_priority, "IOPRIORITY" },
1660 { config_parse_cpu_sched_policy, "CPUSCHEDPOLICY" },
1661 { config_parse_cpu_sched_prio, "CPUSCHEDPRIO" },
1662 { config_parse_cpu_affinity, "CPUAFFINITY" },
1663 { config_parse_mode, "MODE" },
Lennart Poetteringddb26e12010-06-18 06:06:24 +02001664 { config_parse_env_file, "FILE" },
Lennart Poetteringe5373522010-04-10 17:53:17 +02001665 { config_parse_output, "OUTPUT" },
1666 { config_parse_input, "INPUT" },
1667 { config_parse_facility, "FACILITY" },
1668 { config_parse_level, "LEVEL" },
1669 { config_parse_capabilities, "CAPABILITIES" },
1670 { config_parse_secure_bits, "SECUREBITS" },
1671 { config_parse_bounding_set, "BOUNDINGSET" },
Lennart Poettering03fae012010-07-04 21:12:10 +02001672 { config_parse_timer_slack_nsec, "TIMERSLACK" },
Lennart Poetteringe5373522010-04-10 17:53:17 +02001673 { config_parse_limit, "LIMIT" },
1674 { config_parse_cgroup, "CGROUP [...]" },
1675 { config_parse_deps, "UNIT [...]" },
1676 { config_parse_names, "UNIT [...]" },
1677 { config_parse_exec, "PATH [ARGUMENT [...]]" },
1678 { config_parse_service_type, "SERVICETYPE" },
1679 { config_parse_service_restart, "SERVICERESTART" },
Fabiano Fidencio07459bb2010-09-21 00:23:12 -03001680#ifdef HAVE_SYSV_COMPAT
Lennart Poetteringe5373522010-04-10 17:53:17 +02001681 { config_parse_sysv_priority, "SYSVPRIORITY" },
Fabiano Fidencio07459bb2010-09-21 00:23:12 -03001682#else
1683 { config_parse_warn_compat, "NOTSUPPORTED" },
1684#endif
Lennart Poetteringe5373522010-04-10 17:53:17 +02001685 { config_parse_kill_mode, "KILLMODE" },
Lennart Poettering2e22afe2010-07-10 04:49:37 +02001686 { config_parse_kill_signal, "SIGNAL" },
Lennart Poetteringe5373522010-04-10 17:53:17 +02001687 { config_parse_listen, "SOCKET [...]" },
1688 { config_parse_socket_bind, "SOCKETBIND" },
Lennart Poettering93ef5e82010-04-23 20:27:47 +02001689 { config_parse_bindtodevice, "NETWORKINTERFACE" },
1690 { config_parse_usec, "SECONDS" },
1691 { config_parse_path_strv, "PATH [...]" },
Lennart Poettering932921b2010-04-24 03:11:01 +02001692 { config_parse_mount_flags, "MOUNTFLAG [...]" },
Lennart Poetteringf2d37692010-06-18 23:25:19 +02001693 { config_parse_string_printf, "STRING" },
Lennart Poettering871d7de2010-05-24 01:45:54 +02001694 { config_parse_timer, "TIMER" },
1695 { config_parse_timer_unit, "NAME" },
Lennart Poetteringc952c6e2010-06-18 23:12:48 +02001696 { config_parse_path_spec, "PATH" },
1697 { config_parse_path_unit, "UNIT" },
Lennart Poettering8b03dae2010-07-01 16:34:26 +02001698 { config_parse_notify_access, "ACCESS" },
1699 { config_parse_ip_tos, "TOS" },
Lennart Poettering52661ef2010-10-13 02:15:41 +02001700 { config_parse_condition_path, "CONDITION" },
1701 { config_parse_condition_kernel, "CONDITION" },
Lennart Poetteringd257dde2010-11-10 22:28:19 +01001702 { config_parse_condition_null, "CONDITION" },
Lennart Poettering039655a2011-02-21 22:07:55 +01001703 { config_parse_condition_virt, "CONDITION" },
Lennart Poetteringe5373522010-04-10 17:53:17 +02001704 };
1705
1706 assert(f);
1707 assert(items);
1708
1709 for (i = items; i->lvalue; i++) {
1710 unsigned j;
1711 const char *rvalue = "OTHER";
1712
1713 if (!streq_ptr(i->section, prev_section)) {
1714 if (!not_first)
1715 not_first = true;
1716 else
1717 fputc('\n', f);
1718
1719 fprintf(f, "[%s]\n", i->section);
1720 prev_section = i->section;
1721 }
1722
1723 for (j = 0; j < ELEMENTSOF(table); j++)
1724 if (i->parse == table[j].callback) {
1725 rvalue = table[j].rvalue;
1726 break;
1727 }
1728
1729 fprintf(f, "%s=%s\n", i->lvalue, rvalue);
1730 }
1731}
1732
1733static int load_from_path(Unit *u, const char *path) {
Lennart Poettering87f0e412010-01-26 21:39:06 +01001734
1735 static const char* const section_table[_UNIT_TYPE_MAX] = {
1736 [UNIT_SERVICE] = "Service",
1737 [UNIT_TIMER] = "Timer",
1738 [UNIT_SOCKET] = "Socket",
1739 [UNIT_TARGET] = "Target",
1740 [UNIT_DEVICE] = "Device",
1741 [UNIT_MOUNT] = "Mount",
1742 [UNIT_AUTOMOUNT] = "Automount",
Maarten Lankhorst07b0b132010-05-09 18:44:11 +02001743 [UNIT_SNAPSHOT] = "Snapshot",
Lennart Poettering01f78472010-05-24 05:25:33 +02001744 [UNIT_SWAP] = "Swap",
1745 [UNIT_PATH] = "Path"
Lennart Poettering42f4e3c2010-01-19 02:56:37 +01001746 };
1747
Lennart Poettering034c6ed2010-01-26 04:18:44 +01001748#define EXEC_CONTEXT_CONFIG_ITEMS(context, section) \
Lennart Poettering9eba9da2010-01-29 20:46:22 +01001749 { "WorkingDirectory", config_parse_path, &(context).working_directory, section }, \
1750 { "RootDirectory", config_parse_path, &(context).root_directory, section }, \
Lennart Poetteringf2d37692010-06-18 23:25:19 +02001751 { "User", config_parse_string_printf, &(context).user, section }, \
1752 { "Group", config_parse_string_printf, &(context).group, section }, \
Lennart Poettering1c01f822010-01-27 02:16:11 +01001753 { "SupplementaryGroups", config_parse_strv, &(context).supplementary_groups, section }, \
Lennart Poetteringfb33a392010-01-28 02:53:56 +01001754 { "Nice", config_parse_nice, &(context), section }, \
Lennart Poetteringdd6c17b2010-08-31 01:33:39 +02001755 { "OOMScoreAdjust", config_parse_oom_score_adjust,&(context), section }, \
Lennart Poettering9eba9da2010-01-29 20:46:22 +01001756 { "IOSchedulingClass", config_parse_io_class, &(context), section }, \
Lennart Poettering94f04342010-01-30 01:55:42 +01001757 { "IOSchedulingPriority", config_parse_io_priority, &(context), section }, \
1758 { "CPUSchedulingPolicy", config_parse_cpu_sched_policy,&(context), section }, \
1759 { "CPUSchedulingPriority", config_parse_cpu_sched_prio, &(context), section }, \
Lennart Poettering38b48752010-02-02 12:50:04 +01001760 { "CPUSchedulingResetOnFork", config_parse_bool, &(context).cpu_sched_reset_on_fork, section }, \
Lennart Poettering94f04342010-01-30 01:55:42 +01001761 { "CPUAffinity", config_parse_cpu_affinity, &(context), section }, \
Lennart Poetteringb5a06992010-02-12 02:02:14 +01001762 { "UMask", config_parse_mode, &(context).umask, section }, \
Lennart Poettering071830f2010-01-28 02:06:20 +01001763 { "Environment", config_parse_strv, &(context).environment, section }, \
Lennart Poettering8c7be952011-03-04 03:44:43 +01001764 { "EnvironmentFile", config_parse_env_file, &(context).environment_files, section }, \
Lennart Poettering80876c22010-04-13 02:06:27 +02001765 { "StandardInput", config_parse_input, &(context).std_input, section }, \
1766 { "StandardOutput", config_parse_output, &(context).std_output, section }, \
Lennart Poetteringa4ddf822010-06-03 03:37:12 +02001767 { "StandardError", config_parse_output, &(context).std_error, section }, \
Lennart Poettering80876c22010-04-13 02:06:27 +02001768 { "TTYPath", config_parse_path, &(context).tty_path, section }, \
Lennart Poetteringf2d37692010-06-18 23:25:19 +02001769 { "SyslogIdentifier", config_parse_string_printf, &(context).syslog_identifier, section }, \
Lennart Poettering071830f2010-01-28 02:06:20 +01001770 { "SyslogFacility", config_parse_facility, &(context).syslog_priority, section }, \
Lennart Poettering94f04342010-01-30 01:55:42 +01001771 { "SyslogLevel", config_parse_level, &(context).syslog_priority, section }, \
Lennart Poettering74922902010-07-05 01:08:13 +02001772 { "SyslogLevelPrefix", config_parse_bool, &(context).syslog_level_prefix, section }, \
Lennart Poettering94f04342010-01-30 01:55:42 +01001773 { "Capabilities", config_parse_capabilities, &(context), section }, \
1774 { "SecureBits", config_parse_secure_bits, &(context), section }, \
1775 { "CapabilityBoundingSetDrop", config_parse_bounding_set, &(context), section }, \
Lennart Poettering03fae012010-07-04 21:12:10 +02001776 { "TimerSlackNSec", config_parse_timer_slack_nsec,&(context), section }, \
Lennart Poettering94f04342010-01-30 01:55:42 +01001777 { "LimitCPU", config_parse_limit, &(context).rlimit[RLIMIT_CPU], section }, \
1778 { "LimitFSIZE", config_parse_limit, &(context).rlimit[RLIMIT_FSIZE], section }, \
1779 { "LimitDATA", config_parse_limit, &(context).rlimit[RLIMIT_DATA], section }, \
1780 { "LimitSTACK", config_parse_limit, &(context).rlimit[RLIMIT_STACK], section }, \
1781 { "LimitCORE", config_parse_limit, &(context).rlimit[RLIMIT_CORE], section }, \
1782 { "LimitRSS", config_parse_limit, &(context).rlimit[RLIMIT_RSS], section }, \
1783 { "LimitNOFILE", config_parse_limit, &(context).rlimit[RLIMIT_NOFILE], section }, \
1784 { "LimitAS", config_parse_limit, &(context).rlimit[RLIMIT_AS], section }, \
1785 { "LimitNPROC", config_parse_limit, &(context).rlimit[RLIMIT_NPROC], section }, \
1786 { "LimitMEMLOCK", config_parse_limit, &(context).rlimit[RLIMIT_MEMLOCK], section }, \
1787 { "LimitLOCKS", config_parse_limit, &(context).rlimit[RLIMIT_LOCKS], section }, \
1788 { "LimitSIGPENDING", config_parse_limit, &(context).rlimit[RLIMIT_SIGPENDING], section }, \
1789 { "LimitMSGQUEUE", config_parse_limit, &(context).rlimit[RLIMIT_MSGQUEUE], section }, \
1790 { "LimitNICE", config_parse_limit, &(context).rlimit[RLIMIT_NICE], section }, \
1791 { "LimitRTPRIO", config_parse_limit, &(context).rlimit[RLIMIT_RTPRIO], section }, \
Lennart Poettering451a0742010-02-12 02:00:18 +01001792 { "LimitRTTIME", config_parse_limit, &(context).rlimit[RLIMIT_RTTIME], section }, \
Lennart Poettering15ae4222010-04-21 22:15:06 +02001793 { "ControlGroup", config_parse_cgroup, u, section }, \
1794 { "ReadWriteDirectories", config_parse_path_strv, &(context).read_write_dirs, section }, \
1795 { "ReadOnlyDirectories", config_parse_path_strv, &(context).read_only_dirs, section }, \
1796 { "InaccessibleDirectories",config_parse_path_strv, &(context).inaccessible_dirs, section }, \
1797 { "PrivateTmp", config_parse_bool, &(context).private_tmp, section }, \
Lennart Poetteringdf1f0af2010-06-16 16:25:42 +02001798 { "MountFlags", config_parse_mount_flags, &(context), section }, \
Lennart Poetteringf2d37692010-06-18 23:25:19 +02001799 { "TCPWrapName", config_parse_string_printf, &(context).tcpwrap_name, section }, \
Lennart Poettering2e22afe2010-07-10 04:49:37 +02001800 { "PAMName", config_parse_string_printf, &(context).pam_name, section }, \
1801 { "KillMode", config_parse_kill_mode, &(context).kill_mode, section }, \
Lennart Poettering169c1bd2010-10-08 16:06:23 +02001802 { "KillSignal", config_parse_kill_signal, &(context).kill_signal, section }, \
Lennart Poetteringba035df2011-01-18 22:55:54 +01001803 { "SendSIGKILL", config_parse_bool, &(context).send_sigkill, section }, \
Lennart Poettering169c1bd2010-10-08 16:06:23 +02001804 { "UtmpIdentifier", config_parse_string_printf, &(context).utmp_id, section }
Lennart Poettering034c6ed2010-01-26 04:18:44 +01001805
Lennart Poettering3efd4192009-11-19 23:13:20 +01001806 const ConfigItem items[] = {
Lennart Poettering09477262010-04-15 23:20:17 +02001807 { "Names", config_parse_names, u, "Unit" },
Lennart Poetteringf2d37692010-06-18 23:25:19 +02001808 { "Description", config_parse_string_printf, &u->meta.description, "Unit" },
Lennart Poettering09477262010-04-15 23:20:17 +02001809 { "Requires", config_parse_deps, UINT_TO_PTR(UNIT_REQUIRES), "Unit" },
1810 { "RequiresOverridable", config_parse_deps, UINT_TO_PTR(UNIT_REQUIRES_OVERRIDABLE), "Unit" },
1811 { "Requisite", config_parse_deps, UINT_TO_PTR(UNIT_REQUISITE), "Unit" },
1812 { "RequisiteOverridable", config_parse_deps, UINT_TO_PTR(UNIT_REQUISITE_OVERRIDABLE), "Unit" },
1813 { "Wants", config_parse_deps, UINT_TO_PTR(UNIT_WANTS), "Unit" },
Lennart Poetteringb81884e2010-10-28 23:18:47 +02001814 { "BindTo", config_parse_deps, UINT_TO_PTR(UNIT_BIND_TO), "Unit" },
Lennart Poettering09477262010-04-15 23:20:17 +02001815 { "Conflicts", config_parse_deps, UINT_TO_PTR(UNIT_CONFLICTS), "Unit" },
1816 { "Before", config_parse_deps, UINT_TO_PTR(UNIT_BEFORE), "Unit" },
1817 { "After", config_parse_deps, UINT_TO_PTR(UNIT_AFTER), "Unit" },
Lennart Poettering45fb0692010-07-17 00:57:51 +02001818 { "OnFailure", config_parse_deps, UINT_TO_PTR(UNIT_ON_FAILURE), "Unit" },
Lennart Poettering09477262010-04-15 23:20:17 +02001819 { "StopWhenUnneeded", config_parse_bool, &u->meta.stop_when_unneeded, "Unit" },
Lennart Poetteringb5e9dba2010-08-10 20:57:21 +02001820 { "RefuseManualStart", config_parse_bool, &u->meta.refuse_manual_start, "Unit" },
1821 { "RefuseManualStop", config_parse_bool, &u->meta.refuse_manual_stop, "Unit" },
Lennart Poettering2528a7a2010-08-30 22:45:46 +02001822 { "AllowIsolate", config_parse_bool, &u->meta.allow_isolate, "Unit" },
Lennart Poetteringa40eb732010-07-03 19:48:33 +02001823 { "DefaultDependencies", config_parse_bool, &u->meta.default_dependencies, "Unit" },
Lennart Poetteringfaf919f2010-07-17 04:09:28 +02001824 { "JobTimeoutSec", config_parse_usec, &u->meta.job_timeout, "Unit" },
Lennart Poettering52661ef2010-10-13 02:15:41 +02001825 { "ConditionPathExists", config_parse_condition_path, u, "Unit" },
Lennart Poettering36af55d2010-11-15 20:06:49 +01001826 { "ConditionDirectoryNotEmpty", config_parse_condition_path, u, "Unit" },
Lennart Poettering52661ef2010-10-13 02:15:41 +02001827 { "ConditionKernelCommandLine", config_parse_condition_kernel, u, "Unit" },
Lennart Poettering039655a2011-02-21 22:07:55 +01001828 { "ConditionVirtualization",config_parse_condition_virt, u, "Unit" },
Lennart Poetteringd257dde2010-11-10 22:28:19 +01001829 { "ConditionNull", config_parse_condition_null, u, "Unit" },
Lennart Poettering034c6ed2010-01-26 04:18:44 +01001830
Lennart Poettering1c01f822010-01-27 02:16:11 +01001831 { "PIDFile", config_parse_path, &u->service.pid_file, "Service" },
1832 { "ExecStartPre", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START_PRE, "Service" },
1833 { "ExecStart", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START, "Service" },
1834 { "ExecStartPost", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START_POST, "Service" },
1835 { "ExecReload", config_parse_exec, u->service.exec_command+SERVICE_EXEC_RELOAD, "Service" },
1836 { "ExecStop", config_parse_exec, u->service.exec_command+SERVICE_EXEC_STOP, "Service" },
1837 { "ExecStopPost", config_parse_exec, u->service.exec_command+SERVICE_EXEC_STOP_POST, "Service" },
1838 { "RestartSec", config_parse_usec, &u->service.restart_usec, "Service" },
1839 { "TimeoutSec", config_parse_usec, &u->service.timeout_usec, "Service" },
Lennart Poettering0d87eb42010-04-13 02:22:41 +02001840 { "Type", config_parse_service_type, &u->service.type, "Service" },
1841 { "Restart", config_parse_service_restart, &u->service.restart, "Service" },
Lennart Poettering81a2b7c2010-02-14 22:43:08 +01001842 { "PermissionsStartOnly", config_parse_bool, &u->service.permissions_start_only, "Service" },
1843 { "RootDirectoryStartOnly", config_parse_bool, &u->service.root_directory_start_only, "Service" },
Lennart Poettering02ee8652010-08-17 19:37:36 +02001844 { "RemainAfterExit", config_parse_bool, &u->service.remain_after_exit, "Service" },
Lennart Poettering3185a362011-02-13 18:51:30 +01001845 { "GuessMainPID", config_parse_bool, &u->service.guess_main_pid, "Service" },
Fabiano Fidencio07459bb2010-09-21 00:23:12 -03001846#ifdef HAVE_SYSV_COMPAT
Lennart Poetteringa9a1e002010-04-06 02:40:10 +02001847 { "SysVStartPriority", config_parse_sysv_priority, &u->service.sysv_start_priority, "Service" },
Fabiano Fidencio07459bb2010-09-21 00:23:12 -03001848#else
Lennart Poetteringf976f3f2010-10-05 19:49:15 +02001849 { "SysVStartPriority", config_parse_warn_compat, NULL, "Service" },
Fabiano Fidencio07459bb2010-09-21 00:23:12 -03001850#endif
Lennart Poetteringb778d552010-04-10 17:48:41 +02001851 { "NonBlocking", config_parse_bool, &u->service.exec_context.non_blocking, "Service" },
Lennart Poetteringf2d37692010-06-18 23:25:19 +02001852 { "BusName", config_parse_string_printf, &u->service.bus_name, "Service" },
Lennart Poetteringc952c6e2010-06-18 23:12:48 +02001853 { "NotifyAccess", config_parse_notify_access, &u->service.notify_access, "Service" },
Lennart Poetteringf976f3f2010-10-05 19:49:15 +02001854 { "Sockets", config_parse_service_sockets, &u->service, "Service" },
Lennart Poettering2ba545f2010-10-20 14:22:23 +02001855 { "FsckPassNo", config_parse_fsck_passno, &u->service.fsck_passno, "Service" },
Lennart Poettering87f0e412010-01-26 21:39:06 +01001856 EXEC_CONTEXT_CONFIG_ITEMS(u->service.exec_context, "Service"),
Lennart Poettering034c6ed2010-01-26 04:18:44 +01001857
Lennart Poettering1c01f822010-01-27 02:16:11 +01001858 { "ListenStream", config_parse_listen, &u->socket, "Socket" },
1859 { "ListenDatagram", config_parse_listen, &u->socket, "Socket" },
1860 { "ListenSequentialPacket", config_parse_listen, &u->socket, "Socket" },
1861 { "ListenFIFO", config_parse_listen, &u->socket, "Socket" },
1862 { "BindIPv6Only", config_parse_socket_bind, &u->socket, "Socket" },
1863 { "Backlog", config_parse_unsigned, &u->socket.backlog, "Socket" },
Lennart Poetteringacbb0222010-01-27 04:31:52 +01001864 { "BindToDevice", config_parse_bindtodevice, &u->socket, "Socket" },
Lennart Poettering1c01f822010-01-27 02:16:11 +01001865 { "ExecStartPre", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_START_PRE, "Socket" },
1866 { "ExecStartPost", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_START_POST, "Socket" },
1867 { "ExecStopPre", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_STOP_PRE, "Socket" },
1868 { "ExecStopPost", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_STOP_POST, "Socket" },
Lennart Poettering108736d2010-04-10 17:50:54 +02001869 { "TimeoutSec", config_parse_usec, &u->socket.timeout_usec, "Socket" },
Lennart Poetteringb5a06992010-02-12 02:02:14 +01001870 { "DirectoryMode", config_parse_mode, &u->socket.directory_mode, "Socket" },
1871 { "SocketMode", config_parse_mode, &u->socket.socket_mode, "Socket" },
Lennart Poettering4f2d5282010-04-15 06:19:54 +02001872 { "Accept", config_parse_bool, &u->socket.accept, "Socket" },
Lennart Poettering6cf6bbc2010-06-19 04:25:28 +02001873 { "MaxConnections", config_parse_unsigned, &u->socket.max_connections, "Socket" },
Lennart Poettering4fd59482010-07-01 00:29:17 +02001874 { "KeepAlive", config_parse_bool, &u->socket.keep_alive, "Socket" },
1875 { "Priority", config_parse_int, &u->socket.priority, "Socket" },
1876 { "ReceiveBuffer", config_parse_size, &u->socket.receive_buffer, "Socket" },
1877 { "SendBuffer", config_parse_size, &u->socket.send_buffer, "Socket" },
1878 { "IPTOS", config_parse_ip_tos, &u->socket.ip_tos, "Socket" },
1879 { "IPTTL", config_parse_int, &u->socket.ip_ttl, "Socket" },
1880 { "Mark", config_parse_int, &u->socket.mark, "Socket" },
1881 { "PipeSize", config_parse_size, &u->socket.pipe_size, "Socket" },
1882 { "FreeBind", config_parse_bool, &u->socket.free_bind, "Socket" },
Tomasz Torczcebf8b22010-08-03 13:33:40 +02001883 { "TCPCongestion", config_parse_string, &u->socket.tcp_congestion, "Socket" },
Lennart Poetteringd9ff3212010-09-30 02:19:12 +02001884 { "Service", config_parse_socket_service, &u->socket, "Socket" },
Lennart Poettering87f0e412010-01-26 21:39:06 +01001885 EXEC_CONTEXT_CONFIG_ITEMS(u->socket.exec_context, "Socket"),
Lennart Poettering034c6ed2010-01-26 04:18:44 +01001886
Lennart Poetteringe5373522010-04-10 17:53:17 +02001887 { "What", config_parse_string, &u->mount.parameters_fragment.what, "Mount" },
1888 { "Where", config_parse_path, &u->mount.where, "Mount" },
1889 { "Options", config_parse_string, &u->mount.parameters_fragment.options, "Mount" },
1890 { "Type", config_parse_string, &u->mount.parameters_fragment.fstype, "Mount" },
1891 { "TimeoutSec", config_parse_usec, &u->mount.timeout_usec, "Mount" },
Lennart Poettering3e5235b2010-07-02 00:28:44 +02001892 { "DirectoryMode", config_parse_mode, &u->mount.directory_mode, "Mount" },
Lennart Poetteringe5373522010-04-10 17:53:17 +02001893 EXEC_CONTEXT_CONFIG_ITEMS(u->mount.exec_context, "Mount"),
Lennart Poettering034c6ed2010-01-26 04:18:44 +01001894
Lennart Poettering8d567582010-04-16 23:24:39 +02001895 { "Where", config_parse_path, &u->automount.where, "Automount" },
Lennart Poettering1cf18f22010-07-02 01:17:21 +02001896 { "DirectoryMode", config_parse_mode, &u->automount.directory_mode, "Automount" },
Lennart Poettering8d567582010-04-16 23:24:39 +02001897
Lennart Poettering01f78472010-05-24 05:25:33 +02001898 { "What", config_parse_path, &u->swap.parameters_fragment.what, "Swap" },
1899 { "Priority", config_parse_int, &u->swap.parameters_fragment.priority, "Swap" },
Lennart Poettering22927072011-01-18 00:40:10 +01001900 { "TimeoutSec", config_parse_usec, &u->swap.timeout_usec, "Swap" },
1901 EXEC_CONTEXT_CONFIG_ITEMS(u->swap.exec_context, "Swap"),
Maarten Lankhorst07b0b132010-05-09 18:44:11 +02001902
Lennart Poettering03fae012010-07-04 21:12:10 +02001903 { "OnActiveSec", config_parse_timer, &u->timer, "Timer" },
1904 { "OnBootSec", config_parse_timer, &u->timer, "Timer" },
1905 { "OnStartupSec", config_parse_timer, &u->timer, "Timer" },
1906 { "OnUnitActiveSec", config_parse_timer, &u->timer, "Timer" },
1907 { "OnUnitInactiveSec", config_parse_timer, &u->timer, "Timer" },
Lennart Poettering01f78472010-05-24 05:25:33 +02001908 { "Unit", config_parse_timer_unit, &u->timer, "Timer" },
1909
1910 { "PathExists", config_parse_path_spec, &u->path, "Path" },
1911 { "PathChanged", config_parse_path_spec, &u->path, "Path" },
1912 { "DirectoryNotEmpty", config_parse_path_spec, &u->path, "Path" },
1913 { "Unit", config_parse_path_unit, &u->path, "Path" },
Lennart Poettering871d7de2010-05-24 01:45:54 +02001914
Lennart Poettering10e87ee2010-06-16 01:58:50 +02001915 /* The [Install] section is ignored here. */
1916 { "Alias", NULL, NULL, "Install" },
1917 { "WantedBy", NULL, NULL, "Install" },
1918 { "Also", NULL, NULL, "Install" },
1919
Lennart Poettering3efd4192009-11-19 23:13:20 +01001920 { NULL, NULL, NULL, NULL }
1921 };
1922
Lennart Poettering034c6ed2010-01-26 04:18:44 +01001923#undef EXEC_CONTEXT_CONFIG_ITEMS
Lennart Poettering42f4e3c2010-01-19 02:56:37 +01001924
Lennart Poettering10e87ee2010-06-16 01:58:50 +02001925 const char *sections[4];
Lennart Poettering0301abf2010-01-27 00:15:56 +01001926 int r;
Lennart Poettering87f0e412010-01-26 21:39:06 +01001927 Set *symlink_names;
Lennart Poettering23a177e2010-04-06 02:43:58 +02001928 FILE *f = NULL;
1929 char *filename = NULL, *id = NULL;
1930 Unit *merged;
Lennart Poettering45fb0692010-07-17 00:57:51 +02001931 struct stat st;
Lennart Poettering23a177e2010-04-06 02:43:58 +02001932
Lennart Poetteringe5373522010-04-10 17:53:17 +02001933 if (!u) {
1934 /* Dirty dirty hack. */
1935 dump_items((FILE*) path, items);
1936 return 0;
1937 }
1938
Lennart Poettering23a177e2010-04-06 02:43:58 +02001939 assert(u);
Lennart Poetteringe5373522010-04-10 17:53:17 +02001940 assert(path);
Lennart Poettering3efd4192009-11-19 23:13:20 +01001941
Lennart Poettering09477262010-04-15 23:20:17 +02001942 sections[0] = "Unit";
Lennart Poettering87f0e412010-01-26 21:39:06 +01001943 sections[1] = section_table[u->meta.type];
Lennart Poettering10e87ee2010-06-16 01:58:50 +02001944 sections[2] = "Install";
1945 sections[3] = NULL;
Lennart Poettering42f4e3c2010-01-19 02:56:37 +01001946
Lennart Poettering87f0e412010-01-26 21:39:06 +01001947 if (!(symlink_names = set_new(string_hash_func, string_compare_func)))
1948 return -ENOMEM;
Lennart Poettering3efd4192009-11-19 23:13:20 +01001949
Lennart Poettering036643a2010-02-13 01:07:02 +01001950 if (path_is_absolute(path)) {
1951
1952 if (!(filename = strdup(path))) {
1953 r = -ENOMEM;
1954 goto finish;
1955 }
1956
1957 if ((r = open_follow(&filename, &f, symlink_names, &id)) < 0) {
1958 free(filename);
1959 filename = NULL;
1960
1961 if (r != -ENOENT)
1962 goto finish;
1963 }
1964
1965 } else {
1966 char **p;
1967
Lennart Poettering84e35432010-06-15 14:45:15 +02001968 STRV_FOREACH(p, u->meta.manager->lookup_paths.unit_path) {
Lennart Poettering036643a2010-02-13 01:07:02 +01001969
1970 /* Instead of opening the path right away, we manually
1971 * follow all symlinks and add their name to our unit
1972 * name set while doing so */
1973 if (!(filename = path_make_absolute(path, *p))) {
1974 r = -ENOMEM;
1975 goto finish;
1976 }
1977
Lennart Poetteringfe518222010-07-11 00:52:00 +02001978 if (u->meta.manager->unit_path_cache &&
1979 !set_get(u->meta.manager->unit_path_cache, filename))
1980 r = -ENOENT;
1981 else
1982 r = open_follow(&filename, &f, symlink_names, &id);
1983
1984 if (r < 0) {
Lennart Poettering036643a2010-02-13 01:07:02 +01001985 char *sn;
1986
1987 free(filename);
1988 filename = NULL;
1989
1990 if (r != -ENOENT)
1991 goto finish;
1992
1993 /* Empty the symlink names for the next run */
1994 while ((sn = set_steal_first(symlink_names)))
1995 free(sn);
1996
1997 continue;
1998 }
1999
2000 break;
2001 }
Lennart Poettering034c6ed2010-01-26 04:18:44 +01002002 }
2003
Lennart Poettering036643a2010-02-13 01:07:02 +01002004 if (!filename) {
Lennart Poettering8f054242010-07-21 03:13:15 +02002005 /* Hmm, no suitable file found? */
Lennart Poettering23a177e2010-04-06 02:43:58 +02002006 r = 0;
2007 goto finish;
2008 }
2009
2010 merged = u;
2011 if ((r = merge_by_names(&merged, symlink_names, id)) < 0)
2012 goto finish;
2013
2014 if (merged != u) {
Lennart Poetteringe5373522010-04-10 17:53:17 +02002015 u->meta.load_state = UNIT_MERGED;
Lennart Poettering23a177e2010-04-06 02:43:58 +02002016 r = 0;
Lennart Poettering0301abf2010-01-27 00:15:56 +01002017 goto finish;
2018 }
2019
Lennart Poettering45fb0692010-07-17 00:57:51 +02002020 zero(st);
2021 if (fstat(fileno(f), &st) < 0) {
2022 r = -errno;
2023 goto finish;
2024 }
2025
Lennart Poettering00dc5d72010-10-08 02:31:36 +02002026 if (null_or_empty(&st))
Lennart Poettering6daf4f92010-10-08 18:21:52 +02002027 u->meta.load_state = UNIT_MASKED;
Lennart Poettering00dc5d72010-10-08 02:31:36 +02002028 else {
2029 /* Now, parse the file contents */
2030 if ((r = config_parse(filename, f, sections, items, false, u)) < 0)
2031 goto finish;
2032
2033 u->meta.load_state = UNIT_LOADED;
2034 }
Lennart Poettering0301abf2010-01-27 00:15:56 +01002035
Lennart Poettering6be1e7d2010-02-14 01:01:10 +01002036 free(u->meta.fragment_path);
2037 u->meta.fragment_path = filename;
Lennart Poettering0301abf2010-01-27 00:15:56 +01002038 filename = NULL;
2039
Lennart Poettering45fb0692010-07-17 00:57:51 +02002040 u->meta.fragment_mtime = timespec_load(&st.st_mtim);
2041
Lennart Poettering23a177e2010-04-06 02:43:58 +02002042 r = 0;
Lennart Poettering87f0e412010-01-26 21:39:06 +01002043
2044finish:
Lennart Poettering53ec43c2010-06-15 02:45:26 +02002045 set_free_free(symlink_names);
Lennart Poettering0301abf2010-01-27 00:15:56 +01002046 free(filename);
2047
Lennart Poettering23a177e2010-04-06 02:43:58 +02002048 if (f)
2049 fclose(f);
2050
Lennart Poettering0301abf2010-01-27 00:15:56 +01002051 return r;
2052}
2053
Lennart Poetteringe5373522010-04-10 17:53:17 +02002054int unit_load_fragment(Unit *u) {
Lennart Poettering23a177e2010-04-06 02:43:58 +02002055 int r;
Lennart Poettering294d81f2010-07-21 03:28:10 +02002056 Iterator i;
2057 const char *t;
Lennart Poettering0301abf2010-01-27 00:15:56 +01002058
2059 assert(u);
Lennart Poettering294d81f2010-07-21 03:28:10 +02002060 assert(u->meta.load_state == UNIT_STUB);
2061 assert(u->meta.id);
Lennart Poettering0301abf2010-01-27 00:15:56 +01002062
Lennart Poettering294d81f2010-07-21 03:28:10 +02002063 /* First, try to find the unit under its id. We always look
2064 * for unit files in the default directories, to make it easy
2065 * to override things by placing things in /etc/systemd/system */
2066 if ((r = load_from_path(u, u->meta.id)) < 0)
2067 return r;
Lennart Poettering23a177e2010-04-06 02:43:58 +02002068
Lennart Poettering294d81f2010-07-21 03:28:10 +02002069 /* Try to find an alias we can load this with */
2070 if (u->meta.load_state == UNIT_STUB)
2071 SET_FOREACH(t, u->meta.names, i) {
2072
2073 if (t == u->meta.id)
2074 continue;
2075
2076 if ((r = load_from_path(u, t)) < 0)
2077 return r;
2078
2079 if (u->meta.load_state != UNIT_STUB)
2080 break;
2081 }
2082
2083 /* And now, try looking for it under the suggested (originally linked) path */
Lennart Poettering6ccb1b42010-09-27 20:31:23 +02002084 if (u->meta.load_state == UNIT_STUB && u->meta.fragment_path) {
2085
Lennart Poetteringe5373522010-04-10 17:53:17 +02002086 if ((r = load_from_path(u, u->meta.fragment_path)) < 0)
Lennart Poettering23a177e2010-04-06 02:43:58 +02002087 return r;
2088
Lennart Poettering6ccb1b42010-09-27 20:31:23 +02002089 if (u->meta.load_state == UNIT_STUB) {
2090 /* Hmm, this didn't work? Then let's get rid
2091 * of the fragment path stored for us, so that
2092 * we don't point to an invalid location. */
2093 free(u->meta.fragment_path);
2094 u->meta.fragment_path = NULL;
2095 }
2096 }
2097
Lennart Poettering294d81f2010-07-21 03:28:10 +02002098 /* Look for a template */
2099 if (u->meta.load_state == UNIT_STUB && u->meta.instance) {
2100 char *k;
Lennart Poettering0301abf2010-01-27 00:15:56 +01002101
Lennart Poettering294d81f2010-07-21 03:28:10 +02002102 if (!(k = unit_name_template(u->meta.id)))
2103 return -ENOMEM;
2104
2105 r = load_from_path(u, k);
2106 free(k);
2107
2108 if (r < 0)
Lennart Poettering9e2f7c12010-04-15 03:11:11 +02002109 return r;
Lennart Poettering890f4342010-02-14 01:08:20 +01002110
Lennart Poetteringe5373522010-04-10 17:53:17 +02002111 if (u->meta.load_state == UNIT_STUB)
Lennart Poettering23a177e2010-04-06 02:43:58 +02002112 SET_FOREACH(t, u->meta.names, i) {
2113
Lennart Poettering9e2f7c12010-04-15 03:11:11 +02002114 if (t == u->meta.id)
Lennart Poettering23a177e2010-04-06 02:43:58 +02002115 continue;
2116
Lennart Poettering294d81f2010-07-21 03:28:10 +02002117 if (!(k = unit_name_template(t)))
2118 return -ENOMEM;
2119
2120 r = load_from_path(u, k);
2121 free(k);
2122
2123 if (r < 0)
Lennart Poettering23a177e2010-04-06 02:43:58 +02002124 return r;
2125
Lennart Poetteringe5373522010-04-10 17:53:17 +02002126 if (u->meta.load_state != UNIT_STUB)
Lennart Poettering890f4342010-02-14 01:08:20 +01002127 break;
Lennart Poettering23a177e2010-04-06 02:43:58 +02002128 }
Lennart Poettering0301abf2010-01-27 00:15:56 +01002129 }
Lennart Poettering87f0e412010-01-26 21:39:06 +01002130
Lennart Poettering23a177e2010-04-06 02:43:58 +02002131 return 0;
Lennart Poettering3efd4192009-11-19 23:13:20 +01002132}
Lennart Poetteringe5373522010-04-10 17:53:17 +02002133
2134void unit_dump_config_items(FILE *f) {
2135 /* OK, this wins a prize for extreme ugliness. */
2136
2137 load_from_path(NULL, (const void*) f);
2138}