blob: 281863264e1ec212b6a0bc0eed68d14b27f6ab41 [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 Poettering16354ef2010-01-20 19:19:53 +0100194 int r;
Lennart Poettering542563b2010-01-23 03:35:54 +0100195 SocketPort *p;
196 Socket *s;
Lennart Poettering16354ef2010-01-20 19:19:53 +0100197
Lennart Poettering42f4e3c2010-01-19 02:56:37 +0100198 assert(filename);
199 assert(lvalue);
200 assert(rvalue);
201 assert(data);
202
Lennart Poettering542563b2010-01-23 03:35:54 +0100203 s = (Socket*) data;
204
205 if (!(p = new0(SocketPort, 1)))
206 return -ENOMEM;
207
208 if (streq(lvalue, "ListenFIFO")) {
209 p->type = SOCKET_FIFO;
210
211 if (!(p->path = strdup(rvalue))) {
212 free(p);
213 return -ENOMEM;
214 }
Lennart Poettering01f78472010-05-24 05:25:33 +0200215
216 path_kill_slashes(p->path);
Lennart Poettering542563b2010-01-23 03:35:54 +0100217 } else {
218 p->type = SOCKET_SOCKET;
219
220 if ((r = socket_address_parse(&p->address, rvalue)) < 0) {
Lennart Poetteringc0b34692010-08-17 03:30:53 +0200221 log_error("[%s:%u] Failed to parse address value, ignoring: %s", filename, line, rvalue);
Lennart Poettering542563b2010-01-23 03:35:54 +0100222 free(p);
Lennart Poetteringc0b34692010-08-17 03:30:53 +0200223 return 0;
Lennart Poettering542563b2010-01-23 03:35:54 +0100224 }
225
226 if (streq(lvalue, "ListenStream"))
227 p->address.type = SOCK_STREAM;
228 else if (streq(lvalue, "ListenDatagram"))
229 p->address.type = SOCK_DGRAM;
230 else {
231 assert(streq(lvalue, "ListenSequentialPacket"));
232 p->address.type = SOCK_SEQPACKET;
233 }
234
235 if (socket_address_family(&p->address) != AF_LOCAL && p->address.type == SOCK_SEQPACKET) {
Lennart Poetteringc0b34692010-08-17 03:30:53 +0200236 log_error("[%s:%u] Address family not supported, ignoring: %s", filename, line, rvalue);
Lennart Poettering542563b2010-01-23 03:35:54 +0100237 free(p);
Lennart Poetteringc0b34692010-08-17 03:30:53 +0200238 return 0;
Lennart Poettering542563b2010-01-23 03:35:54 +0100239 }
Lennart Poettering16354ef2010-01-20 19:19:53 +0100240 }
241
Lennart Poettering542563b2010-01-23 03:35:54 +0100242 p->fd = -1;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100243 LIST_PREPEND(SocketPort, port, s->ports, p);
Lennart Poettering542563b2010-01-23 03:35:54 +0100244
Lennart Poettering16354ef2010-01-20 19:19:53 +0100245 return 0;
Lennart Poettering42f4e3c2010-01-19 02:56:37 +0100246}
247
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100248static int config_parse_socket_bind(
Lennart Poettering42f4e3c2010-01-19 02:56:37 +0100249 const char *filename,
250 unsigned line,
251 const char *section,
252 const char *lvalue,
253 const char *rvalue,
254 void *data,
255 void *userdata) {
256
Lennart Poettering542563b2010-01-23 03:35:54 +0100257 Socket *s;
Lennart Poetteringc0120d92010-05-21 23:41:25 +0200258 SocketAddressBindIPv6Only b;
Lennart Poettering42f4e3c2010-01-19 02:56:37 +0100259
260 assert(filename);
261 assert(lvalue);
262 assert(rvalue);
263 assert(data);
264
Lennart Poettering542563b2010-01-23 03:35:54 +0100265 s = (Socket*) data;
266
Lennart Poetteringc0120d92010-05-21 23:41:25 +0200267 if ((b = socket_address_bind_ipv6_only_from_string(rvalue)) < 0) {
268 int r;
Lennart Poettering42f4e3c2010-01-19 02:56:37 +0100269
Lennart Poetteringc0120d92010-05-21 23:41:25 +0200270 if ((r = parse_boolean(rvalue)) < 0) {
Lennart Poetteringc0b34692010-08-17 03:30:53 +0200271 log_error("[%s:%u] Failed to parse bind IPv6 only value, ignoring: %s", filename, line, rvalue);
272 return 0;
Lennart Poetteringc0120d92010-05-21 23:41:25 +0200273 }
274
275 s->bind_ipv6_only = r ? SOCKET_ADDRESS_IPV6_ONLY : SOCKET_ADDRESS_BOTH;
276 } else
277 s->bind_ipv6_only = b;
Lennart Poettering542563b2010-01-23 03:35:54 +0100278
Lennart Poettering42f4e3c2010-01-19 02:56:37 +0100279 return 0;
280}
281
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100282static int config_parse_nice(
283 const char *filename,
284 unsigned line,
285 const char *section,
286 const char *lvalue,
287 const char *rvalue,
288 void *data,
289 void *userdata) {
290
Lennart Poetteringfb33a392010-01-28 02:53:56 +0100291 ExecContext *c = data;
292 int priority, r;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100293
294 assert(filename);
295 assert(lvalue);
296 assert(rvalue);
297 assert(data);
298
299 if ((r = safe_atoi(rvalue, &priority)) < 0) {
Lennart Poetteringc0b34692010-08-17 03:30:53 +0200300 log_error("[%s:%u] Failed to parse nice priority, ignoring: %s. ", filename, line, rvalue);
301 return 0;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100302 }
303
304 if (priority < PRIO_MIN || priority >= PRIO_MAX) {
Lennart Poetteringc0b34692010-08-17 03:30:53 +0200305 log_error("[%s:%u] Nice priority out of range, ignoring: %s", filename, line, rvalue);
306 return 0;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100307 }
308
Lennart Poetteringfb33a392010-01-28 02:53:56 +0100309 c->nice = priority;
310 c->nice_set = false;
311
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100312 return 0;
313}
314
Lennart Poetteringdd6c17b2010-08-31 01:33:39 +0200315static int config_parse_oom_score_adjust(
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100316 const char *filename,
317 unsigned line,
318 const char *section,
319 const char *lvalue,
320 const char *rvalue,
321 void *data,
322 void *userdata) {
323
Lennart Poetteringfb33a392010-01-28 02:53:56 +0100324 ExecContext *c = data;
325 int oa, r;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100326
327 assert(filename);
328 assert(lvalue);
329 assert(rvalue);
330 assert(data);
331
332 if ((r = safe_atoi(rvalue, &oa)) < 0) {
Lennart Poetteringdd6c17b2010-08-31 01:33:39 +0200333 log_error("[%s:%u] Failed to parse the OOM score adjust value, ignoring: %s", filename, line, rvalue);
Lennart Poetteringc0b34692010-08-17 03:30:53 +0200334 return 0;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100335 }
336
Lennart Poetteringdd6c17b2010-08-31 01:33:39 +0200337 if (oa < OOM_SCORE_ADJ_MIN || oa > OOM_SCORE_ADJ_MAX) {
338 log_error("[%s:%u] OOM score adjust value out of range, ignoring: %s", filename, line, rvalue);
Lennart Poetteringc0b34692010-08-17 03:30:53 +0200339 return 0;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100340 }
341
Lennart Poetteringdd6c17b2010-08-31 01:33:39 +0200342 c->oom_score_adjust = oa;
343 c->oom_score_adjust_set = true;
Lennart Poetteringfb33a392010-01-28 02:53:56 +0100344
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100345 return 0;
346}
347
Lennart Poetteringb5a06992010-02-12 02:02:14 +0100348static int config_parse_mode(
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100349 const char *filename,
350 unsigned line,
351 const char *section,
352 const char *lvalue,
353 const char *rvalue,
354 void *data,
355 void *userdata) {
356
357 mode_t *m = data;
358 long l;
359 char *x = NULL;
360
361 assert(filename);
362 assert(lvalue);
363 assert(rvalue);
364 assert(data);
365
366 errno = 0;
367 l = strtol(rvalue, &x, 8);
368 if (!x || *x || errno) {
Lennart Poetteringc0b34692010-08-17 03:30:53 +0200369 log_error("[%s:%u] Failed to parse mode value, ignoring: %s", filename, line, rvalue);
370 return 0;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100371 }
372
Lennart Poetteringb5a06992010-02-12 02:02:14 +0100373 if (l < 0000 || l > 07777) {
Lennart Poetteringc0b34692010-08-17 03:30:53 +0200374 log_error("[%s:%u] mode value out of range, ignoring: %s", filename, line, rvalue);
375 return 0;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100376 }
377
378 *m = (mode_t) l;
379 return 0;
380}
381
382static int config_parse_exec(
383 const char *filename,
384 unsigned line,
385 const char *section,
386 const char *lvalue,
387 const char *rvalue,
388 void *data,
389 void *userdata) {
390
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200391 ExecCommand **e = data, *nce;
392 char *path, **n;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100393 unsigned k;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100394
395 assert(filename);
396 assert(lvalue);
397 assert(rvalue);
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200398 assert(e);
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100399
Lennart Poettering6c666e22010-05-19 21:51:53 +0200400 /* We accept an absolute path as first argument, or
401 * alternatively an absolute prefixed with @ to allow
402 * overriding of argv[0]. */
403
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200404 for (;;) {
405 char *w;
406 size_t l;
407 char *state;
Lennart Poetteringb708e7c2010-07-12 02:25:42 +0200408 bool honour_argv0 = false, ignore = false;
Lennart Poettering6c666e22010-05-19 21:51:53 +0200409
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200410 path = NULL;
411 nce = NULL;
412 n = NULL;
Lennart Poettering6c666e22010-05-19 21:51:53 +0200413
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200414 rvalue += strspn(rvalue, WHITESPACE);
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100415
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200416 if (rvalue[0] == 0)
417 break;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100418
Lennart Poetteringb708e7c2010-07-12 02:25:42 +0200419 if (rvalue[0] == '-') {
420 ignore = true;
421 rvalue ++;
422 }
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200423
Lennart Poetteringb708e7c2010-07-12 02:25:42 +0200424 if (rvalue[0] == '@') {
425 honour_argv0 = true;
426 rvalue ++;
427 }
428
429 if (*rvalue != '/') {
Lennart Poetteringc0b34692010-08-17 03:30:53 +0200430 log_error("[%s:%u] Invalid executable path in command line, ignoring: %s", filename, line, rvalue);
431 return 0;
Lennart Poettering6c666e22010-05-19 21:51:53 +0200432 }
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100433
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200434 k = 0;
435 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
Lennart Poetteringf90cf442010-11-14 19:58:33 +0100436 if (strncmp(w, ";", MAX(l, 1U)) == 0)
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200437 break;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100438
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200439 k++;
440 }
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100441
Lennart Poetteringb708e7c2010-07-12 02:25:42 +0200442 if (!(n = new(char*, k + !honour_argv0)))
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200443 return -ENOMEM;
444
445 k = 0;
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200446 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
Lennart Poetteringf90cf442010-11-14 19:58:33 +0100447 if (strncmp(w, ";", MAX(l, 1U)) == 0)
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200448 break;
449
Lennart Poetteringb708e7c2010-07-12 02:25:42 +0200450 if (honour_argv0 && w == rvalue) {
451 assert(!path);
452 if (!(path = cunescape_length(w, l)))
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200453 goto fail;
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200454 } else {
455 if (!(n[k++] = cunescape_length(w, l)))
456 goto fail;
457 }
458 }
459
460 n[k] = NULL;
461
462 if (!n[0]) {
Lennart Poetteringc0b34692010-08-17 03:30:53 +0200463 log_error("[%s:%u] Invalid command line, ignoring: %s", filename, line, rvalue);
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200464 strv_free(n);
Lennart Poetteringc0b34692010-08-17 03:30:53 +0200465 return 0;
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200466 }
467
468 if (!path)
469 if (!(path = strdup(n[0])))
470 goto fail;
471
472 assert(path_is_absolute(path));
473
474 if (!(nce = new0(ExecCommand, 1)))
Lennart Poettering6c666e22010-05-19 21:51:53 +0200475 goto fail;
476
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200477 nce->argv = n;
478 nce->path = path;
Lennart Poetteringb708e7c2010-07-12 02:25:42 +0200479 nce->ignore = ignore;
Lennart Poettering6c666e22010-05-19 21:51:53 +0200480
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200481 path_kill_slashes(nce->path);
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100482
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200483 exec_command_append_list(e, nce);
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100484
Lennart Poettering61e5d8e2010-07-07 20:59:20 +0200485 rvalue = state;
486 }
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100487
488 return 0;
489
490fail:
Lennart Poettering6c666e22010-05-19 21:51:53 +0200491 n[k] = NULL;
492 strv_free(n);
493 free(path);
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100494 free(nce);
495
496 return -ENOMEM;
497}
498
499static int config_parse_usec(
500 const char *filename,
501 unsigned line,
502 const char *section,
503 const char *lvalue,
504 const char *rvalue,
505 void *data,
506 void *userdata) {
507
508 usec_t *usec = data;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100509 int r;
510
511 assert(filename);
512 assert(lvalue);
513 assert(rvalue);
514 assert(data);
515
Lennart Poettering24a6e4a2010-04-23 20:26:59 +0200516 if ((r = parse_usec(rvalue, usec)) < 0) {
Lennart Poetteringc0b34692010-08-17 03:30:53 +0200517 log_error("[%s:%u] Failed to parse time value, ignoring: %s", filename, line, rvalue);
518 return 0;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100519 }
520
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100521 return 0;
522}
523
Lennart Poettering487393e2010-07-07 01:10:27 +0200524static DEFINE_CONFIG_PARSE_ENUM(config_parse_service_type, service_type, ServiceType, "Failed to parse service type");
525static DEFINE_CONFIG_PARSE_ENUM(config_parse_service_restart, service_restart, ServiceRestart, "Failed to parse service restart specifier");
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100526
Lennart Poettering47be8702010-02-03 14:21:48 +0100527static int config_parse_bindtodevice(
Lennart Poetteringacbb0222010-01-27 04:31:52 +0100528 const char *filename,
529 unsigned line,
530 const char *section,
531 const char *lvalue,
532 const char *rvalue,
533 void *data,
534 void *userdata) {
535
536 Socket *s = data;
537 char *n;
538
539 assert(filename);
540 assert(lvalue);
541 assert(rvalue);
542 assert(data);
543
544 if (rvalue[0] && !streq(rvalue, "*")) {
545 if (!(n = strdup(rvalue)))
546 return -ENOMEM;
547 } else
548 n = NULL;
549
550 free(s->bind_to_device);
551 s->bind_to_device = n;
552
553 return 0;
554}
555
Lennart Poettering487393e2010-07-07 01:10:27 +0200556static DEFINE_CONFIG_PARSE_ENUM(config_parse_output, exec_output, ExecOutput, "Failed to parse output specifier");
557static DEFINE_CONFIG_PARSE_ENUM(config_parse_input, exec_input, ExecInput, "Failed to parse input specifier");
Lennart Poettering071830f2010-01-28 02:06:20 +0100558
Lennart Poettering47be8702010-02-03 14:21:48 +0100559static int config_parse_facility(
Lennart Poettering071830f2010-01-28 02:06:20 +0100560 const char *filename,
561 unsigned line,
562 const char *section,
563 const char *lvalue,
564 const char *rvalue,
565 void *data,
566 void *userdata) {
567
Lennart Poettering071830f2010-01-28 02:06:20 +0100568
Lennart Poettering94f04342010-01-30 01:55:42 +0100569 int *o = data, x;
Lennart Poettering071830f2010-01-28 02:06:20 +0100570
571 assert(filename);
572 assert(lvalue);
573 assert(rvalue);
574 assert(data);
575
Lennart Poettering0d87eb42010-04-13 02:22:41 +0200576 if ((x = log_facility_from_string(rvalue)) < 0) {
Lennart Poetteringc0b34692010-08-17 03:30:53 +0200577 log_error("[%s:%u] Failed to parse log facility, ignoring: %s", filename, line, rvalue);
578 return 0;
Lennart Poettering0d87eb42010-04-13 02:22:41 +0200579 }
Lennart Poettering94f04342010-01-30 01:55:42 +0100580
581 *o = LOG_MAKEPRI(x, LOG_PRI(*o));
Lennart Poettering071830f2010-01-28 02:06:20 +0100582
583 return 0;
584}
585
Lennart Poettering47be8702010-02-03 14:21:48 +0100586static int config_parse_level(
Lennart Poettering071830f2010-01-28 02:06:20 +0100587 const char *filename,
588 unsigned line,
589 const char *section,
590 const char *lvalue,
591 const char *rvalue,
592 void *data,
593 void *userdata) {
594
Lennart Poettering071830f2010-01-28 02:06:20 +0100595
Lennart Poettering94f04342010-01-30 01:55:42 +0100596 int *o = data, x;
Lennart Poettering071830f2010-01-28 02:06:20 +0100597
598 assert(filename);
599 assert(lvalue);
600 assert(rvalue);
601 assert(data);
602
Lennart Poettering0d87eb42010-04-13 02:22:41 +0200603 if ((x = log_level_from_string(rvalue)) < 0) {
Lennart Poetteringc0b34692010-08-17 03:30:53 +0200604 log_error("[%s:%u] Failed to parse log level, ignoring: %s", filename, line, rvalue);
605 return 0;
Lennart Poettering0d87eb42010-04-13 02:22:41 +0200606 }
Lennart Poettering071830f2010-01-28 02:06:20 +0100607
Lennart Poettering94f04342010-01-30 01:55:42 +0100608 *o = LOG_MAKEPRI(LOG_FAC(*o), x);
Lennart Poettering071830f2010-01-28 02:06:20 +0100609 return 0;
610}
611
Lennart Poettering47be8702010-02-03 14:21:48 +0100612static int config_parse_io_class(
Lennart Poettering9eba9da2010-01-29 20:46:22 +0100613 const char *filename,
614 unsigned line,
615 const char *section,
616 const char *lvalue,
617 const char *rvalue,
618 void *data,
619 void *userdata) {
620
Lennart Poettering9eba9da2010-01-29 20:46:22 +0100621 ExecContext *c = data;
Lennart Poettering94f04342010-01-30 01:55:42 +0100622 int x;
Lennart Poettering9eba9da2010-01-29 20:46:22 +0100623
624 assert(filename);
625 assert(lvalue);
626 assert(rvalue);
627 assert(data);
628
Lennart Poettering0d87eb42010-04-13 02:22:41 +0200629 if ((x = ioprio_class_from_string(rvalue)) < 0) {
Lennart Poetteringc0b34692010-08-17 03:30:53 +0200630 log_error("[%s:%u] Failed to parse IO scheduling class, ignoring: %s", filename, line, rvalue);
631 return 0;
Lennart Poettering0d87eb42010-04-13 02:22:41 +0200632 }
Lennart Poettering9eba9da2010-01-29 20:46:22 +0100633
Lennart Poettering94f04342010-01-30 01:55:42 +0100634 c->ioprio = IOPRIO_PRIO_VALUE(x, IOPRIO_PRIO_DATA(c->ioprio));
Lennart Poettering9eba9da2010-01-29 20:46:22 +0100635 c->ioprio_set = true;
636
637 return 0;
638}
639
Lennart Poettering47be8702010-02-03 14:21:48 +0100640static int config_parse_io_priority(
Lennart Poettering9eba9da2010-01-29 20:46:22 +0100641 const char *filename,
642 unsigned line,
643 const char *section,
644 const char *lvalue,
645 const char *rvalue,
646 void *data,
647 void *userdata) {
648
649 ExecContext *c = data;
650 int i;
651
652 assert(filename);
653 assert(lvalue);
654 assert(rvalue);
655 assert(data);
656
Lennart Poettering94f04342010-01-30 01:55:42 +0100657 if (safe_atoi(rvalue, &i) < 0 || i < 0 || i >= IOPRIO_BE_NR) {
Lennart Poetteringc0b34692010-08-17 03:30:53 +0200658 log_error("[%s:%u] Failed to parse io priority, ignoring: %s", filename, line, rvalue);
659 return 0;
Lennart Poettering9eba9da2010-01-29 20:46:22 +0100660 }
661
Lennart Poettering94f04342010-01-30 01:55:42 +0100662 c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_PRIO_CLASS(c->ioprio), i);
Lennart Poettering9eba9da2010-01-29 20:46:22 +0100663 c->ioprio_set = true;
664
665 return 0;
666}
667
Lennart Poettering47be8702010-02-03 14:21:48 +0100668static int config_parse_cpu_sched_policy(
Lennart Poettering94f04342010-01-30 01:55:42 +0100669 const char *filename,
670 unsigned line,
671 const char *section,
672 const char *lvalue,
673 const char *rvalue,
674 void *data,
675 void *userdata) {
676
677
678 ExecContext *c = data;
679 int x;
680
681 assert(filename);
682 assert(lvalue);
683 assert(rvalue);
684 assert(data);
685
Lennart Poettering0d87eb42010-04-13 02:22:41 +0200686 if ((x = sched_policy_from_string(rvalue)) < 0) {
Lennart Poetteringc0b34692010-08-17 03:30:53 +0200687 log_error("[%s:%u] Failed to parse CPU scheduling policy, ignoring: %s", filename, line, rvalue);
688 return 0;
Lennart Poettering0d87eb42010-04-13 02:22:41 +0200689 }
Lennart Poettering94f04342010-01-30 01:55:42 +0100690
691 c->cpu_sched_policy = x;
692 c->cpu_sched_set = true;
693
694 return 0;
695}
696
Lennart Poettering47be8702010-02-03 14:21:48 +0100697static int config_parse_cpu_sched_prio(
Lennart Poettering94f04342010-01-30 01:55:42 +0100698 const char *filename,
699 unsigned line,
700 const char *section,
701 const char *lvalue,
702 const char *rvalue,
703 void *data,
704 void *userdata) {
705
706 ExecContext *c = data;
707 int i;
708
709 assert(filename);
710 assert(lvalue);
711 assert(rvalue);
712 assert(data);
713
714 /* On Linux RR/FIFO have the same range */
715 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 +0200716 log_error("[%s:%u] Failed to parse CPU scheduling priority, ignoring: %s", filename, line, rvalue);
717 return 0;
Lennart Poettering94f04342010-01-30 01:55:42 +0100718 }
719
720 c->cpu_sched_priority = i;
721 c->cpu_sched_set = true;
722
723 return 0;
724}
725
Lennart Poettering47be8702010-02-03 14:21:48 +0100726static int config_parse_cpu_affinity(
Lennart Poettering94f04342010-01-30 01:55:42 +0100727 const char *filename,
728 unsigned line,
729 const char *section,
730 const char *lvalue,
731 const char *rvalue,
732 void *data,
733 void *userdata) {
734
735 ExecContext *c = data;
736 char *w;
737 size_t l;
738 char *state;
739
740 assert(filename);
741 assert(lvalue);
742 assert(rvalue);
743 assert(data);
744
Lennart Poetteringf60f22d2010-07-07 20:58:41 +0200745 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
Lennart Poettering94f04342010-01-30 01:55:42 +0100746 char *t;
747 int r;
748 unsigned cpu;
749
750 if (!(t = strndup(w, l)))
751 return -ENOMEM;
752
Lennart Poettering487393e2010-07-07 01:10:27 +0200753 r = safe_atou(t, &cpu);
754 free(t);
755
Lennart Poettering82c121a2010-07-04 16:44:58 +0200756 if (!(c->cpuset))
757 if (!(c->cpuset = cpu_set_malloc(&c->cpuset_ncpus)))
758 return -ENOMEM;
759
Lennart Poettering82c121a2010-07-04 16:44:58 +0200760 if (r < 0 || cpu >= c->cpuset_ncpus) {
Lennart Poetteringc0b34692010-08-17 03:30:53 +0200761 log_error("[%s:%u] Failed to parse CPU affinity, ignoring: %s", filename, line, rvalue);
762 return 0;
Lennart Poettering94f04342010-01-30 01:55:42 +0100763 }
764
Lennart Poettering82c121a2010-07-04 16:44:58 +0200765 CPU_SET_S(cpu, CPU_ALLOC_SIZE(c->cpuset_ncpus), c->cpuset);
Lennart Poettering94f04342010-01-30 01:55:42 +0100766 }
767
Lennart Poettering94f04342010-01-30 01:55:42 +0100768 return 0;
769}
770
Lennart Poettering47be8702010-02-03 14:21:48 +0100771static int config_parse_capabilities(
Lennart Poettering94f04342010-01-30 01:55:42 +0100772 const char *filename,
773 unsigned line,
774 const char *section,
775 const char *lvalue,
776 const char *rvalue,
777 void *data,
778 void *userdata) {
779
780 ExecContext *c = data;
781 cap_t cap;
782
783 assert(filename);
784 assert(lvalue);
785 assert(rvalue);
786 assert(data);
787
788 if (!(cap = cap_from_text(rvalue))) {
789 if (errno == ENOMEM)
790 return -ENOMEM;
791
Lennart Poetteringc0b34692010-08-17 03:30:53 +0200792 log_error("[%s:%u] Failed to parse capabilities, ignoring: %s", filename, line, rvalue);
793 return 0;
Lennart Poettering94f04342010-01-30 01:55:42 +0100794 }
795
796 if (c->capabilities)
797 cap_free(c->capabilities);
798 c->capabilities = cap;
799
800 return 0;
801}
802
Lennart Poettering47be8702010-02-03 14:21:48 +0100803static int config_parse_secure_bits(
Lennart Poettering94f04342010-01-30 01:55:42 +0100804 const char *filename,
805 unsigned line,
806 const char *section,
807 const char *lvalue,
808 const char *rvalue,
809 void *data,
810 void *userdata) {
811
812 ExecContext *c = data;
813 char *w;
814 size_t l;
815 char *state;
816
817 assert(filename);
818 assert(lvalue);
819 assert(rvalue);
820 assert(data);
821
Lennart Poetteringf60f22d2010-07-07 20:58:41 +0200822 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
Lennart Poettering94f04342010-01-30 01:55:42 +0100823 if (first_word(w, "keep-caps"))
824 c->secure_bits |= SECURE_KEEP_CAPS;
825 else if (first_word(w, "keep-caps-locked"))
826 c->secure_bits |= SECURE_KEEP_CAPS_LOCKED;
827 else if (first_word(w, "no-setuid-fixup"))
828 c->secure_bits |= SECURE_NO_SETUID_FIXUP;
829 else if (first_word(w, "no-setuid-fixup-locked"))
830 c->secure_bits |= SECURE_NO_SETUID_FIXUP_LOCKED;
831 else if (first_word(w, "noroot"))
832 c->secure_bits |= SECURE_NOROOT;
833 else if (first_word(w, "noroot-locked"))
834 c->secure_bits |= SECURE_NOROOT_LOCKED;
835 else {
Lennart Poetteringc0b34692010-08-17 03:30:53 +0200836 log_error("[%s:%u] Failed to parse secure bits, ignoring: %s", filename, line, rvalue);
837 return 0;
Lennart Poettering94f04342010-01-30 01:55:42 +0100838 }
839 }
840
841 return 0;
842}
843
Lennart Poettering47be8702010-02-03 14:21:48 +0100844static int config_parse_bounding_set(
Lennart Poettering94f04342010-01-30 01:55:42 +0100845 const char *filename,
846 unsigned line,
847 const char *section,
848 const char *lvalue,
849 const char *rvalue,
850 void *data,
851 void *userdata) {
852
853 ExecContext *c = data;
854 char *w;
855 size_t l;
856 char *state;
857
858 assert(filename);
859 assert(lvalue);
860 assert(rvalue);
861 assert(data);
862
Lennart Poetteringf60f22d2010-07-07 20:58:41 +0200863 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
Lennart Poettering94f04342010-01-30 01:55:42 +0100864 char *t;
865 int r;
866 cap_value_t cap;
867
868 if (!(t = strndup(w, l)))
869 return -ENOMEM;
870
871 r = cap_from_name(t, &cap);
872 free(t);
873
874 if (r < 0) {
Lennart Poetteringc0b34692010-08-17 03:30:53 +0200875 log_error("[%s:%u] Failed to parse capability bounding set, ignoring: %s", filename, line, rvalue);
876 return 0;
Lennart Poettering94f04342010-01-30 01:55:42 +0100877 }
878
879 c->capability_bounding_set_drop |= 1 << cap;
880 }
881
882 return 0;
883}
884
Lennart Poettering03fae012010-07-04 21:12:10 +0200885static int config_parse_timer_slack_nsec(
Lennart Poettering94f04342010-01-30 01:55:42 +0100886 const char *filename,
887 unsigned line,
888 const char *section,
889 const char *lvalue,
890 const char *rvalue,
891 void *data,
892 void *userdata) {
893
894 ExecContext *c = data;
895 unsigned long u;
896 int r;
897
898 assert(filename);
899 assert(lvalue);
900 assert(rvalue);
901 assert(data);
902
903 if ((r = safe_atolu(rvalue, &u)) < 0) {
Lennart Poetteringc0b34692010-08-17 03:30:53 +0200904 log_error("[%s:%u] Failed to parse time slack value, ignoring: %s", filename, line, rvalue);
905 return 0;
Lennart Poettering94f04342010-01-30 01:55:42 +0100906 }
907
Lennart Poettering03fae012010-07-04 21:12:10 +0200908 c->timer_slack_nsec = u;
Lennart Poettering94f04342010-01-30 01:55:42 +0100909
910 return 0;
911}
912
913static int config_parse_limit(
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 struct rlimit **rl = data;
923 unsigned long long u;
924 int r;
925
926 assert(filename);
927 assert(lvalue);
928 assert(rvalue);
929 assert(data);
930
931 if ((r = safe_atollu(rvalue, &u)) < 0) {
Lennart Poetteringc0b34692010-08-17 03:30:53 +0200932 log_error("[%s:%u] Failed to parse resource value, ignoring: %s", filename, line, rvalue);
933 return 0;
Lennart Poettering94f04342010-01-30 01:55:42 +0100934 }
935
936 if (!*rl)
937 if (!(*rl = new(struct rlimit, 1)))
938 return -ENOMEM;
939
940 (*rl)->rlim_cur = (*rl)->rlim_max = (rlim_t) u;
941 return 0;
942}
943
Lennart Poettering8e274522010-03-31 16:29:55 +0200944static int config_parse_cgroup(
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 Unit *u = userdata;
954 char *w;
955 size_t l;
956 char *state;
957
Lennart Poetteringf60f22d2010-07-07 20:58:41 +0200958 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
Lennart Poettering8e274522010-03-31 16:29:55 +0200959 char *t;
960 int r;
961
Lennart Poetteringf60f22d2010-07-07 20:58:41 +0200962 if (!(t = cunescape_length(w, l)))
Lennart Poettering8e274522010-03-31 16:29:55 +0200963 return -ENOMEM;
964
965 r = unit_add_cgroup_from_text(u, t);
966 free(t);
967
Lennart Poetteringc0b34692010-08-17 03:30:53 +0200968 if (r < 0) {
969 log_error("[%s:%u] Failed to parse cgroup value, ignoring: %s", filename, line, rvalue);
970 return 0;
971 }
Lennart Poettering8e274522010-03-31 16:29:55 +0200972 }
973
974 return 0;
975}
976
Fabiano Fidencio07459bb2010-09-21 00:23:12 -0300977#ifdef HAVE_SYSV_COMPAT
Lennart Poetteringa9a1e002010-04-06 02:40:10 +0200978static int config_parse_sysv_priority(
979 const char *filename,
980 unsigned line,
981 const char *section,
982 const char *lvalue,
983 const char *rvalue,
984 void *data,
985 void *userdata) {
986
987 int *priority = data;
988 int r, i;
989
990 assert(filename);
991 assert(lvalue);
992 assert(rvalue);
993 assert(data);
994
995 if ((r = safe_atoi(rvalue, &i)) < 0 || i < 0) {
Lennart Poetteringc0b34692010-08-17 03:30:53 +0200996 log_error("[%s:%u] Failed to parse SysV start priority, ignoring: %s", filename, line, rvalue);
997 return 0;
Lennart Poetteringa9a1e002010-04-06 02:40:10 +0200998 }
999
1000 *priority = (int) i;
1001 return 0;
1002}
Fabiano Fidencio07459bb2010-09-21 00:23:12 -03001003#endif
Lennart Poetteringa9a1e002010-04-06 02:40:10 +02001004
Lennart Poettering2ba545f2010-10-20 14:22:23 +02001005static int config_parse_fsck_passno(
1006 const char *filename,
1007 unsigned line,
1008 const char *section,
1009 const char *lvalue,
1010 const char *rvalue,
1011 void *data,
1012 void *userdata) {
1013
1014 int *passno = data;
1015 int r, i;
1016
1017 assert(filename);
1018 assert(lvalue);
1019 assert(rvalue);
1020 assert(data);
1021
1022 if ((r = safe_atoi(rvalue, &i)) < 0 || i < 0) {
1023 log_error("[%s:%u] Failed to parse fsck pass number, ignoring: %s", filename, line, rvalue);
1024 return 0;
1025 }
1026
1027 *passno = (int) i;
1028 return 0;
1029}
1030
Lennart Poettering487393e2010-07-07 01:10:27 +02001031static DEFINE_CONFIG_PARSE_ENUM(config_parse_kill_mode, kill_mode, KillMode, "Failed to parse kill mode");
Lennart Poettering50159e62010-04-08 00:52:14 +02001032
Lennart Poettering2e22afe2010-07-10 04:49:37 +02001033static int config_parse_kill_signal(
1034 const char *filename,
1035 unsigned line,
1036 const char *section,
1037 const char *lvalue,
1038 const char *rvalue,
1039 void *data,
1040 void *userdata) {
1041
1042 int *sig = data;
1043 int r;
1044
1045 assert(filename);
1046 assert(lvalue);
1047 assert(rvalue);
1048 assert(sig);
1049
Lennart Poettering8a0867d2010-10-22 16:11:50 +02001050 if ((r = signal_from_string_try_harder(rvalue)) <= 0) {
Lennart Poetteringc0b34692010-08-17 03:30:53 +02001051 log_error("[%s:%u] Failed to parse kill signal, ignoring: %s", filename, line, rvalue);
1052 return 0;
Lennart Poettering2e22afe2010-07-10 04:49:37 +02001053 }
1054
1055 *sig = r;
1056 return 0;
1057}
1058
Lennart Poettering15ae4222010-04-21 22:15:06 +02001059static int config_parse_mount_flags(
1060 const char *filename,
1061 unsigned line,
1062 const char *section,
1063 const char *lvalue,
1064 const char *rvalue,
1065 void *data,
1066 void *userdata) {
1067
1068 ExecContext *c = data;
1069 char *w;
1070 size_t l;
1071 char *state;
1072 unsigned long flags = 0;
1073
1074 assert(filename);
1075 assert(lvalue);
1076 assert(rvalue);
1077 assert(data);
1078
Lennart Poetteringf60f22d2010-07-07 20:58:41 +02001079 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
Lennart Poetteringf90cf442010-11-14 19:58:33 +01001080 if (strncmp(w, "shared", MAX(l, 6U)) == 0)
Lennart Poettering15ae4222010-04-21 22:15:06 +02001081 flags |= MS_SHARED;
Lennart Poetteringf90cf442010-11-14 19:58:33 +01001082 else if (strncmp(w, "slave", MAX(l, 5U)) == 0)
Lennart Poettering15ae4222010-04-21 22:15:06 +02001083 flags |= MS_SLAVE;
Lennart Poetteringf90cf442010-11-14 19:58:33 +01001084 else if (strncmp(w, "private", MAX(l, 7U)) == 0)
Lennart Poettering15ae4222010-04-21 22:15:06 +02001085 flags |= MS_PRIVATE;
1086 else {
Lennart Poetteringc0b34692010-08-17 03:30:53 +02001087 log_error("[%s:%u] Failed to parse mount flags, ignoring: %s", filename, line, rvalue);
1088 return 0;
Lennart Poettering15ae4222010-04-21 22:15:06 +02001089 }
1090 }
1091
1092 c->mount_flags = flags;
1093 return 0;
1094}
1095
Lennart Poettering871d7de2010-05-24 01:45:54 +02001096static int config_parse_timer(
1097 const char *filename,
1098 unsigned line,
1099 const char *section,
1100 const char *lvalue,
1101 const char *rvalue,
1102 void *data,
1103 void *userdata) {
1104
1105 Timer *t = data;
1106 usec_t u;
1107 int r;
1108 TimerValue *v;
1109 TimerBase b;
1110
1111 assert(filename);
1112 assert(lvalue);
1113 assert(rvalue);
1114 assert(data);
1115
1116 if ((b = timer_base_from_string(lvalue)) < 0) {
Lennart Poetteringc0b34692010-08-17 03:30:53 +02001117 log_error("[%s:%u] Failed to parse timer base, ignoring: %s", filename, line, lvalue);
1118 return 0;
Lennart Poettering871d7de2010-05-24 01:45:54 +02001119 }
1120
1121 if ((r = parse_usec(rvalue, &u)) < 0) {
Lennart Poetteringc0b34692010-08-17 03:30:53 +02001122 log_error("[%s:%u] Failed to parse timer value, ignoring: %s", filename, line, rvalue);
1123 return 0;
Lennart Poettering871d7de2010-05-24 01:45:54 +02001124 }
1125
1126 if (!(v = new0(TimerValue, 1)))
1127 return -ENOMEM;
1128
1129 v->base = b;
1130 v->value = u;
1131
1132 LIST_PREPEND(TimerValue, value, t->values, v);
1133
1134 return 0;
1135}
1136
1137static int config_parse_timer_unit(
1138 const char *filename,
1139 unsigned line,
1140 const char *section,
1141 const char *lvalue,
1142 const char *rvalue,
1143 void *data,
1144 void *userdata) {
1145
1146 Timer *t = data;
1147 int r;
Lennart Poettering398ef8b2010-07-08 02:43:18 +02001148 DBusError error;
1149
1150 assert(filename);
1151 assert(lvalue);
1152 assert(rvalue);
1153 assert(data);
1154
1155 dbus_error_init(&error);
Lennart Poettering871d7de2010-05-24 01:45:54 +02001156
1157 if (endswith(rvalue, ".timer")) {
Lennart Poetteringc0b34692010-08-17 03:30:53 +02001158 log_error("[%s:%u] Unit cannot be of type timer, ignoring: %s", filename, line, rvalue);
1159 return 0;
Lennart Poettering871d7de2010-05-24 01:45:54 +02001160 }
1161
Lennart Poettering398ef8b2010-07-08 02:43:18 +02001162 if ((r = manager_load_unit(t->meta.manager, rvalue, NULL, NULL, &t->unit)) < 0) {
Lennart Poetteringc0b34692010-08-17 03:30:53 +02001163 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 +02001164 dbus_error_free(&error);
Lennart Poetteringc0b34692010-08-17 03:30:53 +02001165 return 0;
Lennart Poettering871d7de2010-05-24 01:45:54 +02001166 }
1167
1168 return 0;
1169}
1170
Lennart Poettering01f78472010-05-24 05:25:33 +02001171static int config_parse_path_spec(
1172 const char *filename,
1173 unsigned line,
1174 const char *section,
1175 const char *lvalue,
1176 const char *rvalue,
1177 void *data,
1178 void *userdata) {
1179
1180 Path *p = data;
1181 PathSpec *s;
1182 PathType b;
1183
1184 assert(filename);
1185 assert(lvalue);
1186 assert(rvalue);
1187 assert(data);
1188
1189 if ((b = path_type_from_string(lvalue)) < 0) {
Lennart Poetteringc0b34692010-08-17 03:30:53 +02001190 log_error("[%s:%u] Failed to parse path type, ignoring: %s", filename, line, lvalue);
1191 return 0;
Lennart Poettering01f78472010-05-24 05:25:33 +02001192 }
1193
1194 if (!path_is_absolute(rvalue)) {
Lennart Poetteringc0b34692010-08-17 03:30:53 +02001195 log_error("[%s:%u] Path is not absolute, ignoring: %s", filename, line, rvalue);
1196 return 0;
Lennart Poettering01f78472010-05-24 05:25:33 +02001197 }
1198
1199 if (!(s = new0(PathSpec, 1)))
1200 return -ENOMEM;
1201
1202 if (!(s->path = strdup(rvalue))) {
1203 free(s);
1204 return -ENOMEM;
1205 }
1206
1207 path_kill_slashes(s->path);
1208
1209 s->type = b;
1210 s->inotify_fd = -1;
1211
1212 LIST_PREPEND(PathSpec, spec, p->specs, s);
1213
1214 return 0;
1215}
1216
1217static int config_parse_path_unit(
1218 const char *filename,
1219 unsigned line,
1220 const char *section,
1221 const char *lvalue,
1222 const char *rvalue,
1223 void *data,
1224 void *userdata) {
1225
1226 Path *t = data;
1227 int r;
Lennart Poettering398ef8b2010-07-08 02:43:18 +02001228 DBusError error;
1229
1230 assert(filename);
1231 assert(lvalue);
1232 assert(rvalue);
1233 assert(data);
1234
1235 dbus_error_init(&error);
Lennart Poettering01f78472010-05-24 05:25:33 +02001236
1237 if (endswith(rvalue, ".path")) {
Lennart Poetteringc0b34692010-08-17 03:30:53 +02001238 log_error("[%s:%u] Unit cannot be of type path, ignoring: %s", filename, line, rvalue);
1239 return 0;
Lennart Poettering01f78472010-05-24 05:25:33 +02001240 }
1241
Lennart Poettering398ef8b2010-07-08 02:43:18 +02001242 if ((r = manager_load_unit(t->meta.manager, rvalue, NULL, &error, &t->unit)) < 0) {
Lennart Poetteringc0b34692010-08-17 03:30:53 +02001243 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 +02001244 dbus_error_free(&error);
Lennart Poetteringc0b34692010-08-17 03:30:53 +02001245 return 0;
Lennart Poettering01f78472010-05-24 05:25:33 +02001246 }
1247
1248 return 0;
1249}
1250
Lennart Poetteringd9ff3212010-09-30 02:19:12 +02001251static int config_parse_socket_service(
1252 const char *filename,
1253 unsigned line,
1254 const char *section,
1255 const char *lvalue,
1256 const char *rvalue,
1257 void *data,
1258 void *userdata) {
1259
1260 Socket *s = data;
1261 int r;
1262 DBusError error;
1263
1264 assert(filename);
1265 assert(lvalue);
1266 assert(rvalue);
1267 assert(data);
1268
1269 dbus_error_init(&error);
1270
Lennart Poetteringf976f3f2010-10-05 19:49:15 +02001271 if (!endswith(rvalue, ".service")) {
1272 log_error("[%s:%u] Unit must be of type service, ignoring: %s", filename, line, rvalue);
Lennart Poetteringd9ff3212010-09-30 02:19:12 +02001273 return 0;
1274 }
1275
1276 if ((r = manager_load_unit(s->meta.manager, rvalue, NULL, &error, (Unit**) &s->service)) < 0) {
1277 log_error("[%s:%u] Failed to load unit %s, ignoring: %s", filename, line, rvalue, bus_error(&error, r));
1278 dbus_error_free(&error);
1279 return 0;
1280 }
1281
1282 return 0;
1283}
1284
Lennart Poetteringf976f3f2010-10-05 19:49:15 +02001285static int config_parse_service_sockets(
1286 const char *filename,
1287 unsigned line,
1288 const char *section,
1289 const char *lvalue,
1290 const char *rvalue,
1291 void *data,
1292 void *userdata) {
1293
1294 Service *s = data;
1295 int r;
1296 DBusError error;
1297 char *state, *w;
1298 size_t l;
1299
1300 assert(filename);
1301 assert(lvalue);
1302 assert(rvalue);
1303 assert(data);
1304
1305 dbus_error_init(&error);
1306
1307 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
1308 char *t;
1309 Unit *sock;
1310
1311 if (!(t = strndup(w, l)))
1312 return -ENOMEM;
1313
1314 if (!endswith(t, ".socket")) {
1315 log_error("[%s:%u] Unit must be of type socket, ignoring: %s", filename, line, rvalue);
1316 free(t);
1317 continue;
1318 }
1319
1320 r = manager_load_unit(s->meta.manager, t, NULL, &error, &sock);
1321 free(t);
1322
1323 if (r < 0) {
1324 log_error("[%s:%u] Failed to load unit %s, ignoring: %s", filename, line, rvalue, bus_error(&error, r));
1325 dbus_error_free(&error);
1326 continue;
1327 }
1328
1329 if ((r = set_ensure_allocated(&s->configured_sockets, trivial_hash_func, trivial_compare_func)) < 0)
1330 return r;
1331
1332 if ((r = set_put(s->configured_sockets, sock)) < 0)
1333 return r;
1334 }
1335
1336 return 0;
1337}
1338
Lennart Poetteringddb26e12010-06-18 06:06:24 +02001339static int config_parse_env_file(
1340 const char *filename,
1341 unsigned line,
1342 const char *section,
1343 const char *lvalue,
1344 const char *rvalue,
1345 void *data,
1346 void *userdata) {
1347
1348 FILE *f;
1349 int r;
1350 char ***env = data;
1351
1352 assert(filename);
1353 assert(lvalue);
1354 assert(rvalue);
1355 assert(data);
1356
1357 if (!(f = fopen(rvalue, "re"))) {
Lennart Poetteringc0b34692010-08-17 03:30:53 +02001358 log_error("[%s:%u] Failed to open environment file '%s', ignoring: %m", filename, line, rvalue);
1359 return 0;
Lennart Poetteringddb26e12010-06-18 06:06:24 +02001360 }
1361
1362 while (!feof(f)) {
Lennart Poettering5f7c4262011-01-05 16:06:35 +01001363 char l[LINE_MAX], *p, *u;
Lennart Poetteringddb26e12010-06-18 06:06:24 +02001364 char **t;
1365
1366 if (!fgets(l, sizeof(l), f)) {
1367 if (feof(f))
1368 break;
1369
1370 r = -errno;
Lennart Poetteringc0b34692010-08-17 03:30:53 +02001371 log_error("[%s:%u] Failed to read environment file '%s', ignoring: %m", filename, line, rvalue);
1372 r = 0;
Lennart Poetteringddb26e12010-06-18 06:06:24 +02001373 goto finish;
1374 }
1375
1376 p = strstrip(l);
1377
1378 if (!*p)
1379 continue;
1380
1381 if (strchr(COMMENTS, *p))
1382 continue;
1383
Lennart Poettering5f7c4262011-01-05 16:06:35 +01001384 if (!(u = normalize_env_assignment(p))) {
1385 log_error("Out of memory");
1386 r = -ENOMEM;
1387 goto finish;
1388 }
1389
1390 t = strv_env_set(*env, u);
1391 free(u);
1392
1393 if (!t) {
1394 log_error("Out of memory");
1395 r = -ENOMEM;
1396 goto finish;
1397 }
1398
Lennart Poetteringddb26e12010-06-18 06:06:24 +02001399 strv_free(*env);
1400 *env = t;
1401 }
1402
1403 r = 0;
1404
1405finish:
1406 if (f)
1407 fclose(f);
1408
1409 return r;
1410}
1411
Lennart Poettering4fd59482010-07-01 00:29:17 +02001412static int config_parse_ip_tos(
1413 const char *filename,
1414 unsigned line,
1415 const char *section,
1416 const char *lvalue,
1417 const char *rvalue,
1418 void *data,
1419 void *userdata) {
1420
1421 int *ip_tos = data, x;
1422 int r;
1423
1424 assert(filename);
1425 assert(lvalue);
1426 assert(rvalue);
1427 assert(data);
1428
1429 if ((x = ip_tos_from_string(rvalue)) < 0)
1430 if ((r = safe_atoi(rvalue, &x)) < 0) {
Lennart Poetteringc0b34692010-08-17 03:30:53 +02001431 log_error("[%s:%u] Failed to parse IP TOS value, ignoring: %s", filename, line, rvalue);
1432 return 0;
Lennart Poettering4fd59482010-07-01 00:29:17 +02001433 }
1434
1435 *ip_tos = x;
1436 return 0;
1437}
1438
Lennart Poettering52661ef2010-10-13 02:15:41 +02001439static int config_parse_condition_path(
1440 const char *filename,
1441 unsigned line,
1442 const char *section,
1443 const char *lvalue,
1444 const char *rvalue,
1445 void *data,
1446 void *userdata) {
1447
1448 Unit *u = data;
1449 bool negate;
1450 Condition *c;
1451
1452 assert(filename);
1453 assert(lvalue);
1454 assert(rvalue);
1455 assert(data);
1456
1457 if ((negate = rvalue[0] == '!'))
1458 rvalue++;
1459
1460 if (!path_is_absolute(rvalue)) {
Lennart Poetteringd257dde2010-11-10 22:28:19 +01001461 log_error("[%s:%u] Path in condition not absolute, ignoring: %s", filename, line, rvalue);
Lennart Poettering52661ef2010-10-13 02:15:41 +02001462 return 0;
1463 }
1464
Lennart Poettering36af55d2010-11-15 20:06:49 +01001465 if (!(c = condition_new(streq(lvalue, "ConditionPathExists") ? CONDITION_PATH_EXISTS : CONDITION_DIRECTORY_NOT_EMPTY,
1466 rvalue, negate)))
Lennart Poettering52661ef2010-10-13 02:15:41 +02001467 return -ENOMEM;
1468
1469 LIST_PREPEND(Condition, conditions, u->meta.conditions, c);
1470 return 0;
1471}
1472
1473static int config_parse_condition_kernel(
1474 const char *filename,
1475 unsigned line,
1476 const char *section,
1477 const char *lvalue,
1478 const char *rvalue,
1479 void *data,
1480 void *userdata) {
1481
1482 Unit *u = data;
1483 bool negate;
1484 Condition *c;
1485
1486 assert(filename);
1487 assert(lvalue);
1488 assert(rvalue);
1489 assert(data);
1490
1491 if ((negate = rvalue[0] == '!'))
1492 rvalue++;
1493
1494 if (!(c = condition_new(CONDITION_KERNEL_COMMAND_LINE, rvalue, negate)))
1495 return -ENOMEM;
1496
1497 LIST_PREPEND(Condition, conditions, u->meta.conditions, c);
1498 return 0;
1499}
1500
Lennart Poetteringd257dde2010-11-10 22:28:19 +01001501static int config_parse_condition_null(
1502 const char *filename,
1503 unsigned line,
1504 const char *section,
1505 const char *lvalue,
1506 const char *rvalue,
1507 void *data,
1508 void *userdata) {
1509
1510 Unit *u = data;
1511 Condition *c;
1512 bool negate;
1513 int b;
1514
1515 assert(filename);
1516 assert(lvalue);
1517 assert(rvalue);
1518 assert(data);
1519
1520 if ((negate = rvalue[0] == '!'))
1521 rvalue++;
1522
1523 if ((b = parse_boolean(rvalue)) < 0) {
1524 log_error("[%s:%u] Failed to parse boolean value in condition, ignoring: %s", filename, line, rvalue);
1525 return 0;
1526 }
1527
1528 if (!b)
1529 negate = !negate;
1530
1531 if (!(c = condition_new(CONDITION_NULL, NULL, negate)))
1532 return -ENOMEM;
1533
1534 LIST_PREPEND(Condition, conditions, u->meta.conditions, c);
1535 return 0;
1536}
1537
Lennart Poettering487393e2010-07-07 01:10:27 +02001538static DEFINE_CONFIG_PARSE_ENUM(config_parse_notify_access, notify_access, NotifyAccess, "Failed to parse notify access specifier");
Lennart Poetteringc952c6e2010-06-18 23:12:48 +02001539
Lennart Poettering071830f2010-01-28 02:06:20 +01001540#define FOLLOW_MAX 8
Lennart Poettering87f0e412010-01-26 21:39:06 +01001541
Lennart Poettering9e2f7c12010-04-15 03:11:11 +02001542static int open_follow(char **filename, FILE **_f, Set *names, char **_final) {
Lennart Poettering0301abf2010-01-27 00:15:56 +01001543 unsigned c = 0;
Lennart Poettering87f0e412010-01-26 21:39:06 +01001544 int fd, r;
1545 FILE *f;
Lennart Poettering0301abf2010-01-27 00:15:56 +01001546 char *id = NULL;
Lennart Poettering87f0e412010-01-26 21:39:06 +01001547
1548 assert(filename);
1549 assert(*filename);
1550 assert(_f);
1551 assert(names);
1552
Lennart Poettering0301abf2010-01-27 00:15:56 +01001553 /* This will update the filename pointer if the loaded file is
1554 * reached by a symlink. The old string will be freed. */
Lennart Poettering87f0e412010-01-26 21:39:06 +01001555
Lennart Poettering0301abf2010-01-27 00:15:56 +01001556 for (;;) {
Lennart Poettering2c7108c2010-06-16 01:56:00 +02001557 char *target, *name;
Lennart Poettering87f0e412010-01-26 21:39:06 +01001558
Lennart Poettering0301abf2010-01-27 00:15:56 +01001559 if (c++ >= FOLLOW_MAX)
1560 return -ELOOP;
1561
Lennart Poetteringb08d03f2010-01-29 02:07:41 +01001562 path_kill_slashes(*filename);
1563
Lennart Poettering87f0e412010-01-26 21:39:06 +01001564 /* Add the file name we are currently looking at to
Lennart Poettering8f054242010-07-21 03:13:15 +02001565 * the names of this unit, but only if it is a valid
1566 * unit name. */
Lennart Poettering0301abf2010-01-27 00:15:56 +01001567 name = file_name_from_path(*filename);
Lennart Poettering87f0e412010-01-26 21:39:06 +01001568
Lennart Poetteringb9c0d442010-10-08 03:09:25 +02001569 if (unit_name_is_valid(name, false)) {
Lennart Poettering8f054242010-07-21 03:13:15 +02001570 if (!(id = set_get(names, name))) {
Lennart Poettering0301abf2010-01-27 00:15:56 +01001571
Lennart Poettering8f054242010-07-21 03:13:15 +02001572 if (!(id = strdup(name)))
1573 return -ENOMEM;
1574
1575 if ((r = set_put(names, id)) < 0) {
1576 free(id);
1577 return r;
1578 }
Lennart Poettering87f0e412010-01-26 21:39:06 +01001579 }
Lennart Poettering87f0e412010-01-26 21:39:06 +01001580 }
1581
Lennart Poettering0301abf2010-01-27 00:15:56 +01001582 /* Try to open the file name, but don't if its a symlink */
1583 if ((fd = open(*filename, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW)) >= 0)
Lennart Poettering87f0e412010-01-26 21:39:06 +01001584 break;
1585
Lennart Poettering0301abf2010-01-27 00:15:56 +01001586 if (errno != ELOOP)
1587 return -errno;
Lennart Poettering87f0e412010-01-26 21:39:06 +01001588
Lennart Poettering0301abf2010-01-27 00:15:56 +01001589 /* Hmm, so this is a symlink. Let's read the name, and follow it manually */
Lennart Poettering2c7108c2010-06-16 01:56:00 +02001590 if ((r = readlink_and_make_absolute(*filename, &target)) < 0)
Lennart Poettering0301abf2010-01-27 00:15:56 +01001591 return r;
1592
Lennart Poettering0301abf2010-01-27 00:15:56 +01001593 free(*filename);
Lennart Poettering2c7108c2010-06-16 01:56:00 +02001594 *filename = target;
Lennart Poettering87f0e412010-01-26 21:39:06 +01001595 }
1596
Lennart Poettering8f054242010-07-21 03:13:15 +02001597 if (!(f = fdopen(fd, "re"))) {
Lennart Poettering87f0e412010-01-26 21:39:06 +01001598 r = -errno;
Lennart Poettering9e2f7c12010-04-15 03:11:11 +02001599 close_nointr_nofail(fd);
Lennart Poettering0301abf2010-01-27 00:15:56 +01001600 return r;
Lennart Poettering87f0e412010-01-26 21:39:06 +01001601 }
1602
1603 *_f = f;
Lennart Poettering9e2f7c12010-04-15 03:11:11 +02001604 *_final = id;
Lennart Poettering0301abf2010-01-27 00:15:56 +01001605 return 0;
Lennart Poettering87f0e412010-01-26 21:39:06 +01001606}
1607
Lennart Poettering23a177e2010-04-06 02:43:58 +02001608static int merge_by_names(Unit **u, Set *names, const char *id) {
1609 char *k;
1610 int r;
1611
1612 assert(u);
1613 assert(*u);
1614 assert(names);
1615
1616 /* Let's try to add in all symlink names we found */
1617 while ((k = set_steal_first(names))) {
1618
1619 /* First try to merge in the other name into our
1620 * unit */
1621 if ((r = unit_merge_by_name(*u, k)) < 0) {
1622 Unit *other;
1623
1624 /* Hmm, we couldn't merge the other unit into
1625 * ours? Then let's try it the other way
1626 * round */
1627
1628 other = manager_get_unit((*u)->meta.manager, k);
1629 free(k);
1630
1631 if (other)
1632 if ((r = unit_merge(other, *u)) >= 0) {
1633 *u = other;
1634 return merge_by_names(u, names, NULL);
1635 }
1636
1637 return r;
1638 }
1639
1640 if (id == k)
1641 unit_choose_id(*u, id);
1642
1643 free(k);
1644 }
1645
1646 return 0;
1647}
1648
Lennart Poetteringe5373522010-04-10 17:53:17 +02001649static void dump_items(FILE *f, const ConfigItem *items) {
1650 const ConfigItem *i;
1651 const char *prev_section = NULL;
1652 bool not_first = false;
1653
1654 struct {
1655 ConfigParserCallback callback;
1656 const char *rvalue;
1657 } table[] = {
1658 { config_parse_int, "INTEGER" },
1659 { config_parse_unsigned, "UNSIGNED" },
1660 { config_parse_size, "SIZE" },
1661 { config_parse_bool, "BOOLEAN" },
1662 { config_parse_string, "STRING" },
1663 { config_parse_path, "PATH" },
1664 { config_parse_strv, "STRING [...]" },
1665 { config_parse_nice, "NICE" },
Lennart Poetteringdd6c17b2010-08-31 01:33:39 +02001666 { config_parse_oom_score_adjust, "OOMSCOREADJUST" },
Lennart Poetteringe5373522010-04-10 17:53:17 +02001667 { config_parse_io_class, "IOCLASS" },
1668 { config_parse_io_priority, "IOPRIORITY" },
1669 { config_parse_cpu_sched_policy, "CPUSCHEDPOLICY" },
1670 { config_parse_cpu_sched_prio, "CPUSCHEDPRIO" },
1671 { config_parse_cpu_affinity, "CPUAFFINITY" },
1672 { config_parse_mode, "MODE" },
Lennart Poetteringddb26e12010-06-18 06:06:24 +02001673 { config_parse_env_file, "FILE" },
Lennart Poetteringe5373522010-04-10 17:53:17 +02001674 { config_parse_output, "OUTPUT" },
1675 { config_parse_input, "INPUT" },
1676 { config_parse_facility, "FACILITY" },
1677 { config_parse_level, "LEVEL" },
1678 { config_parse_capabilities, "CAPABILITIES" },
1679 { config_parse_secure_bits, "SECUREBITS" },
1680 { config_parse_bounding_set, "BOUNDINGSET" },
Lennart Poettering03fae012010-07-04 21:12:10 +02001681 { config_parse_timer_slack_nsec, "TIMERSLACK" },
Lennart Poetteringe5373522010-04-10 17:53:17 +02001682 { config_parse_limit, "LIMIT" },
1683 { config_parse_cgroup, "CGROUP [...]" },
1684 { config_parse_deps, "UNIT [...]" },
1685 { config_parse_names, "UNIT [...]" },
1686 { config_parse_exec, "PATH [ARGUMENT [...]]" },
1687 { config_parse_service_type, "SERVICETYPE" },
1688 { config_parse_service_restart, "SERVICERESTART" },
Fabiano Fidencio07459bb2010-09-21 00:23:12 -03001689#ifdef HAVE_SYSV_COMPAT
Lennart Poetteringe5373522010-04-10 17:53:17 +02001690 { config_parse_sysv_priority, "SYSVPRIORITY" },
Fabiano Fidencio07459bb2010-09-21 00:23:12 -03001691#else
1692 { config_parse_warn_compat, "NOTSUPPORTED" },
1693#endif
Lennart Poetteringe5373522010-04-10 17:53:17 +02001694 { config_parse_kill_mode, "KILLMODE" },
Lennart Poettering2e22afe2010-07-10 04:49:37 +02001695 { config_parse_kill_signal, "SIGNAL" },
Lennart Poetteringe5373522010-04-10 17:53:17 +02001696 { config_parse_listen, "SOCKET [...]" },
1697 { config_parse_socket_bind, "SOCKETBIND" },
Lennart Poettering93ef5e82010-04-23 20:27:47 +02001698 { config_parse_bindtodevice, "NETWORKINTERFACE" },
1699 { config_parse_usec, "SECONDS" },
1700 { config_parse_path_strv, "PATH [...]" },
Lennart Poettering932921b2010-04-24 03:11:01 +02001701 { config_parse_mount_flags, "MOUNTFLAG [...]" },
Lennart Poetteringf2d37692010-06-18 23:25:19 +02001702 { config_parse_string_printf, "STRING" },
Lennart Poettering871d7de2010-05-24 01:45:54 +02001703 { config_parse_timer, "TIMER" },
1704 { config_parse_timer_unit, "NAME" },
Lennart Poetteringc952c6e2010-06-18 23:12:48 +02001705 { config_parse_path_spec, "PATH" },
1706 { config_parse_path_unit, "UNIT" },
Lennart Poettering8b03dae2010-07-01 16:34:26 +02001707 { config_parse_notify_access, "ACCESS" },
1708 { config_parse_ip_tos, "TOS" },
Lennart Poettering52661ef2010-10-13 02:15:41 +02001709 { config_parse_condition_path, "CONDITION" },
1710 { config_parse_condition_kernel, "CONDITION" },
Lennart Poetteringd257dde2010-11-10 22:28:19 +01001711 { config_parse_condition_null, "CONDITION" },
Lennart Poetteringe5373522010-04-10 17:53:17 +02001712 };
1713
1714 assert(f);
1715 assert(items);
1716
1717 for (i = items; i->lvalue; i++) {
1718 unsigned j;
1719 const char *rvalue = "OTHER";
1720
1721 if (!streq_ptr(i->section, prev_section)) {
1722 if (!not_first)
1723 not_first = true;
1724 else
1725 fputc('\n', f);
1726
1727 fprintf(f, "[%s]\n", i->section);
1728 prev_section = i->section;
1729 }
1730
1731 for (j = 0; j < ELEMENTSOF(table); j++)
1732 if (i->parse == table[j].callback) {
1733 rvalue = table[j].rvalue;
1734 break;
1735 }
1736
1737 fprintf(f, "%s=%s\n", i->lvalue, rvalue);
1738 }
1739}
1740
1741static int load_from_path(Unit *u, const char *path) {
Lennart Poettering87f0e412010-01-26 21:39:06 +01001742
1743 static const char* const section_table[_UNIT_TYPE_MAX] = {
1744 [UNIT_SERVICE] = "Service",
1745 [UNIT_TIMER] = "Timer",
1746 [UNIT_SOCKET] = "Socket",
1747 [UNIT_TARGET] = "Target",
1748 [UNIT_DEVICE] = "Device",
1749 [UNIT_MOUNT] = "Mount",
1750 [UNIT_AUTOMOUNT] = "Automount",
Maarten Lankhorst07b0b132010-05-09 18:44:11 +02001751 [UNIT_SNAPSHOT] = "Snapshot",
Lennart Poettering01f78472010-05-24 05:25:33 +02001752 [UNIT_SWAP] = "Swap",
1753 [UNIT_PATH] = "Path"
Lennart Poettering42f4e3c2010-01-19 02:56:37 +01001754 };
1755
Lennart Poettering034c6ed2010-01-26 04:18:44 +01001756#define EXEC_CONTEXT_CONFIG_ITEMS(context, section) \
Lennart Poettering9eba9da2010-01-29 20:46:22 +01001757 { "WorkingDirectory", config_parse_path, &(context).working_directory, section }, \
1758 { "RootDirectory", config_parse_path, &(context).root_directory, section }, \
Lennart Poetteringf2d37692010-06-18 23:25:19 +02001759 { "User", config_parse_string_printf, &(context).user, section }, \
1760 { "Group", config_parse_string_printf, &(context).group, section }, \
Lennart Poettering1c01f822010-01-27 02:16:11 +01001761 { "SupplementaryGroups", config_parse_strv, &(context).supplementary_groups, section }, \
Lennart Poetteringfb33a392010-01-28 02:53:56 +01001762 { "Nice", config_parse_nice, &(context), section }, \
Lennart Poetteringdd6c17b2010-08-31 01:33:39 +02001763 { "OOMScoreAdjust", config_parse_oom_score_adjust,&(context), section }, \
Lennart Poettering9eba9da2010-01-29 20:46:22 +01001764 { "IOSchedulingClass", config_parse_io_class, &(context), section }, \
Lennart Poettering94f04342010-01-30 01:55:42 +01001765 { "IOSchedulingPriority", config_parse_io_priority, &(context), section }, \
1766 { "CPUSchedulingPolicy", config_parse_cpu_sched_policy,&(context), section }, \
1767 { "CPUSchedulingPriority", config_parse_cpu_sched_prio, &(context), section }, \
Lennart Poettering38b48752010-02-02 12:50:04 +01001768 { "CPUSchedulingResetOnFork", config_parse_bool, &(context).cpu_sched_reset_on_fork, section }, \
Lennart Poettering94f04342010-01-30 01:55:42 +01001769 { "CPUAffinity", config_parse_cpu_affinity, &(context), section }, \
Lennart Poetteringb5a06992010-02-12 02:02:14 +01001770 { "UMask", config_parse_mode, &(context).umask, section }, \
Lennart Poettering071830f2010-01-28 02:06:20 +01001771 { "Environment", config_parse_strv, &(context).environment, section }, \
Lennart Poetteringddb26e12010-06-18 06:06:24 +02001772 { "EnvironmentFile", config_parse_env_file, &(context).environment, section }, \
Lennart Poettering80876c22010-04-13 02:06:27 +02001773 { "StandardInput", config_parse_input, &(context).std_input, section }, \
1774 { "StandardOutput", config_parse_output, &(context).std_output, section }, \
Lennart Poetteringa4ddf822010-06-03 03:37:12 +02001775 { "StandardError", config_parse_output, &(context).std_error, section }, \
Lennart Poettering80876c22010-04-13 02:06:27 +02001776 { "TTYPath", config_parse_path, &(context).tty_path, section }, \
Lennart Poetteringf2d37692010-06-18 23:25:19 +02001777 { "SyslogIdentifier", config_parse_string_printf, &(context).syslog_identifier, section }, \
Lennart Poettering071830f2010-01-28 02:06:20 +01001778 { "SyslogFacility", config_parse_facility, &(context).syslog_priority, section }, \
Lennart Poettering94f04342010-01-30 01:55:42 +01001779 { "SyslogLevel", config_parse_level, &(context).syslog_priority, section }, \
Lennart Poettering74922902010-07-05 01:08:13 +02001780 { "SyslogLevelPrefix", config_parse_bool, &(context).syslog_level_prefix, section }, \
Lennart Poettering94f04342010-01-30 01:55:42 +01001781 { "Capabilities", config_parse_capabilities, &(context), section }, \
1782 { "SecureBits", config_parse_secure_bits, &(context), section }, \
1783 { "CapabilityBoundingSetDrop", config_parse_bounding_set, &(context), section }, \
Lennart Poettering03fae012010-07-04 21:12:10 +02001784 { "TimerSlackNSec", config_parse_timer_slack_nsec,&(context), section }, \
Lennart Poettering94f04342010-01-30 01:55:42 +01001785 { "LimitCPU", config_parse_limit, &(context).rlimit[RLIMIT_CPU], section }, \
1786 { "LimitFSIZE", config_parse_limit, &(context).rlimit[RLIMIT_FSIZE], section }, \
1787 { "LimitDATA", config_parse_limit, &(context).rlimit[RLIMIT_DATA], section }, \
1788 { "LimitSTACK", config_parse_limit, &(context).rlimit[RLIMIT_STACK], section }, \
1789 { "LimitCORE", config_parse_limit, &(context).rlimit[RLIMIT_CORE], section }, \
1790 { "LimitRSS", config_parse_limit, &(context).rlimit[RLIMIT_RSS], section }, \
1791 { "LimitNOFILE", config_parse_limit, &(context).rlimit[RLIMIT_NOFILE], section }, \
1792 { "LimitAS", config_parse_limit, &(context).rlimit[RLIMIT_AS], section }, \
1793 { "LimitNPROC", config_parse_limit, &(context).rlimit[RLIMIT_NPROC], section }, \
1794 { "LimitMEMLOCK", config_parse_limit, &(context).rlimit[RLIMIT_MEMLOCK], section }, \
1795 { "LimitLOCKS", config_parse_limit, &(context).rlimit[RLIMIT_LOCKS], section }, \
1796 { "LimitSIGPENDING", config_parse_limit, &(context).rlimit[RLIMIT_SIGPENDING], section }, \
1797 { "LimitMSGQUEUE", config_parse_limit, &(context).rlimit[RLIMIT_MSGQUEUE], section }, \
1798 { "LimitNICE", config_parse_limit, &(context).rlimit[RLIMIT_NICE], section }, \
1799 { "LimitRTPRIO", config_parse_limit, &(context).rlimit[RLIMIT_RTPRIO], section }, \
Lennart Poettering451a0742010-02-12 02:00:18 +01001800 { "LimitRTTIME", config_parse_limit, &(context).rlimit[RLIMIT_RTTIME], section }, \
Lennart Poettering15ae4222010-04-21 22:15:06 +02001801 { "ControlGroup", config_parse_cgroup, u, section }, \
1802 { "ReadWriteDirectories", config_parse_path_strv, &(context).read_write_dirs, section }, \
1803 { "ReadOnlyDirectories", config_parse_path_strv, &(context).read_only_dirs, section }, \
1804 { "InaccessibleDirectories",config_parse_path_strv, &(context).inaccessible_dirs, section }, \
1805 { "PrivateTmp", config_parse_bool, &(context).private_tmp, section }, \
Lennart Poetteringdf1f0af2010-06-16 16:25:42 +02001806 { "MountFlags", config_parse_mount_flags, &(context), section }, \
Lennart Poetteringf2d37692010-06-18 23:25:19 +02001807 { "TCPWrapName", config_parse_string_printf, &(context).tcpwrap_name, section }, \
Lennart Poettering2e22afe2010-07-10 04:49:37 +02001808 { "PAMName", config_parse_string_printf, &(context).pam_name, section }, \
1809 { "KillMode", config_parse_kill_mode, &(context).kill_mode, section }, \
Lennart Poettering169c1bd2010-10-08 16:06:23 +02001810 { "KillSignal", config_parse_kill_signal, &(context).kill_signal, section }, \
1811 { "UtmpIdentifier", config_parse_string_printf, &(context).utmp_id, section }
Lennart Poettering034c6ed2010-01-26 04:18:44 +01001812
Lennart Poettering3efd4192009-11-19 23:13:20 +01001813 const ConfigItem items[] = {
Lennart Poettering09477262010-04-15 23:20:17 +02001814 { "Names", config_parse_names, u, "Unit" },
Lennart Poetteringf2d37692010-06-18 23:25:19 +02001815 { "Description", config_parse_string_printf, &u->meta.description, "Unit" },
Lennart Poettering09477262010-04-15 23:20:17 +02001816 { "Requires", config_parse_deps, UINT_TO_PTR(UNIT_REQUIRES), "Unit" },
1817 { "RequiresOverridable", config_parse_deps, UINT_TO_PTR(UNIT_REQUIRES_OVERRIDABLE), "Unit" },
1818 { "Requisite", config_parse_deps, UINT_TO_PTR(UNIT_REQUISITE), "Unit" },
1819 { "RequisiteOverridable", config_parse_deps, UINT_TO_PTR(UNIT_REQUISITE_OVERRIDABLE), "Unit" },
1820 { "Wants", config_parse_deps, UINT_TO_PTR(UNIT_WANTS), "Unit" },
Lennart Poetteringb81884e2010-10-28 23:18:47 +02001821 { "BindTo", config_parse_deps, UINT_TO_PTR(UNIT_BIND_TO), "Unit" },
Lennart Poettering09477262010-04-15 23:20:17 +02001822 { "Conflicts", config_parse_deps, UINT_TO_PTR(UNIT_CONFLICTS), "Unit" },
1823 { "Before", config_parse_deps, UINT_TO_PTR(UNIT_BEFORE), "Unit" },
1824 { "After", config_parse_deps, UINT_TO_PTR(UNIT_AFTER), "Unit" },
Lennart Poettering45fb0692010-07-17 00:57:51 +02001825 { "OnFailure", config_parse_deps, UINT_TO_PTR(UNIT_ON_FAILURE), "Unit" },
Lennart Poettering09477262010-04-15 23:20:17 +02001826 { "StopWhenUnneeded", config_parse_bool, &u->meta.stop_when_unneeded, "Unit" },
Lennart Poetteringb5e9dba2010-08-10 20:57:21 +02001827 { "RefuseManualStart", config_parse_bool, &u->meta.refuse_manual_start, "Unit" },
1828 { "RefuseManualStop", config_parse_bool, &u->meta.refuse_manual_stop, "Unit" },
Lennart Poettering2528a7a2010-08-30 22:45:46 +02001829 { "AllowIsolate", config_parse_bool, &u->meta.allow_isolate, "Unit" },
Lennart Poetteringa40eb732010-07-03 19:48:33 +02001830 { "DefaultDependencies", config_parse_bool, &u->meta.default_dependencies, "Unit" },
Lennart Poetteringfaf919f2010-07-17 04:09:28 +02001831 { "JobTimeoutSec", config_parse_usec, &u->meta.job_timeout, "Unit" },
Lennart Poettering52661ef2010-10-13 02:15:41 +02001832 { "ConditionPathExists", config_parse_condition_path, u, "Unit" },
Lennart Poettering36af55d2010-11-15 20:06:49 +01001833 { "ConditionDirectoryNotEmpty", config_parse_condition_path, u, "Unit" },
Lennart Poettering52661ef2010-10-13 02:15:41 +02001834 { "ConditionKernelCommandLine", config_parse_condition_kernel, u, "Unit" },
Lennart Poetteringd257dde2010-11-10 22:28:19 +01001835 { "ConditionNull", config_parse_condition_null, u, "Unit" },
Lennart Poettering034c6ed2010-01-26 04:18:44 +01001836
Lennart Poettering1c01f822010-01-27 02:16:11 +01001837 { "PIDFile", config_parse_path, &u->service.pid_file, "Service" },
1838 { "ExecStartPre", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START_PRE, "Service" },
1839 { "ExecStart", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START, "Service" },
1840 { "ExecStartPost", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START_POST, "Service" },
1841 { "ExecReload", config_parse_exec, u->service.exec_command+SERVICE_EXEC_RELOAD, "Service" },
1842 { "ExecStop", config_parse_exec, u->service.exec_command+SERVICE_EXEC_STOP, "Service" },
1843 { "ExecStopPost", config_parse_exec, u->service.exec_command+SERVICE_EXEC_STOP_POST, "Service" },
1844 { "RestartSec", config_parse_usec, &u->service.restart_usec, "Service" },
1845 { "TimeoutSec", config_parse_usec, &u->service.timeout_usec, "Service" },
Lennart Poettering0d87eb42010-04-13 02:22:41 +02001846 { "Type", config_parse_service_type, &u->service.type, "Service" },
1847 { "Restart", config_parse_service_restart, &u->service.restart, "Service" },
Lennart Poettering81a2b7c2010-02-14 22:43:08 +01001848 { "PermissionsStartOnly", config_parse_bool, &u->service.permissions_start_only, "Service" },
1849 { "RootDirectoryStartOnly", config_parse_bool, &u->service.root_directory_start_only, "Service" },
Lennart Poettering02ee8652010-08-17 19:37:36 +02001850 { "RemainAfterExit", config_parse_bool, &u->service.remain_after_exit, "Service" },
Fabiano Fidencio07459bb2010-09-21 00:23:12 -03001851#ifdef HAVE_SYSV_COMPAT
Lennart Poetteringa9a1e002010-04-06 02:40:10 +02001852 { "SysVStartPriority", config_parse_sysv_priority, &u->service.sysv_start_priority, "Service" },
Fabiano Fidencio07459bb2010-09-21 00:23:12 -03001853#else
Lennart Poetteringf976f3f2010-10-05 19:49:15 +02001854 { "SysVStartPriority", config_parse_warn_compat, NULL, "Service" },
Fabiano Fidencio07459bb2010-09-21 00:23:12 -03001855#endif
Lennart Poetteringb778d552010-04-10 17:48:41 +02001856 { "NonBlocking", config_parse_bool, &u->service.exec_context.non_blocking, "Service" },
Lennart Poetteringf2d37692010-06-18 23:25:19 +02001857 { "BusName", config_parse_string_printf, &u->service.bus_name, "Service" },
Lennart Poetteringc952c6e2010-06-18 23:12:48 +02001858 { "NotifyAccess", config_parse_notify_access, &u->service.notify_access, "Service" },
Lennart Poetteringf976f3f2010-10-05 19:49:15 +02001859 { "Sockets", config_parse_service_sockets, &u->service, "Service" },
Lennart Poettering2ba545f2010-10-20 14:22:23 +02001860 { "FsckPassNo", config_parse_fsck_passno, &u->service.fsck_passno, "Service" },
Lennart Poettering87f0e412010-01-26 21:39:06 +01001861 EXEC_CONTEXT_CONFIG_ITEMS(u->service.exec_context, "Service"),
Lennart Poettering034c6ed2010-01-26 04:18:44 +01001862
Lennart Poettering1c01f822010-01-27 02:16:11 +01001863 { "ListenStream", config_parse_listen, &u->socket, "Socket" },
1864 { "ListenDatagram", config_parse_listen, &u->socket, "Socket" },
1865 { "ListenSequentialPacket", config_parse_listen, &u->socket, "Socket" },
1866 { "ListenFIFO", config_parse_listen, &u->socket, "Socket" },
1867 { "BindIPv6Only", config_parse_socket_bind, &u->socket, "Socket" },
1868 { "Backlog", config_parse_unsigned, &u->socket.backlog, "Socket" },
Lennart Poetteringacbb0222010-01-27 04:31:52 +01001869 { "BindToDevice", config_parse_bindtodevice, &u->socket, "Socket" },
Lennart Poettering1c01f822010-01-27 02:16:11 +01001870 { "ExecStartPre", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_START_PRE, "Socket" },
1871 { "ExecStartPost", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_START_POST, "Socket" },
1872 { "ExecStopPre", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_STOP_PRE, "Socket" },
1873 { "ExecStopPost", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_STOP_POST, "Socket" },
Lennart Poettering108736d2010-04-10 17:50:54 +02001874 { "TimeoutSec", config_parse_usec, &u->socket.timeout_usec, "Socket" },
Lennart Poetteringb5a06992010-02-12 02:02:14 +01001875 { "DirectoryMode", config_parse_mode, &u->socket.directory_mode, "Socket" },
1876 { "SocketMode", config_parse_mode, &u->socket.socket_mode, "Socket" },
Lennart Poettering4f2d5282010-04-15 06:19:54 +02001877 { "Accept", config_parse_bool, &u->socket.accept, "Socket" },
Lennart Poettering6cf6bbc2010-06-19 04:25:28 +02001878 { "MaxConnections", config_parse_unsigned, &u->socket.max_connections, "Socket" },
Lennart Poettering4fd59482010-07-01 00:29:17 +02001879 { "KeepAlive", config_parse_bool, &u->socket.keep_alive, "Socket" },
1880 { "Priority", config_parse_int, &u->socket.priority, "Socket" },
1881 { "ReceiveBuffer", config_parse_size, &u->socket.receive_buffer, "Socket" },
1882 { "SendBuffer", config_parse_size, &u->socket.send_buffer, "Socket" },
1883 { "IPTOS", config_parse_ip_tos, &u->socket.ip_tos, "Socket" },
1884 { "IPTTL", config_parse_int, &u->socket.ip_ttl, "Socket" },
1885 { "Mark", config_parse_int, &u->socket.mark, "Socket" },
1886 { "PipeSize", config_parse_size, &u->socket.pipe_size, "Socket" },
1887 { "FreeBind", config_parse_bool, &u->socket.free_bind, "Socket" },
Tomasz Torczcebf8b22010-08-03 13:33:40 +02001888 { "TCPCongestion", config_parse_string, &u->socket.tcp_congestion, "Socket" },
Lennart Poetteringd9ff3212010-09-30 02:19:12 +02001889 { "Service", config_parse_socket_service, &u->socket, "Socket" },
Lennart Poettering87f0e412010-01-26 21:39:06 +01001890 EXEC_CONTEXT_CONFIG_ITEMS(u->socket.exec_context, "Socket"),
Lennart Poettering034c6ed2010-01-26 04:18:44 +01001891
Lennart Poetteringe5373522010-04-10 17:53:17 +02001892 { "What", config_parse_string, &u->mount.parameters_fragment.what, "Mount" },
1893 { "Where", config_parse_path, &u->mount.where, "Mount" },
1894 { "Options", config_parse_string, &u->mount.parameters_fragment.options, "Mount" },
1895 { "Type", config_parse_string, &u->mount.parameters_fragment.fstype, "Mount" },
1896 { "TimeoutSec", config_parse_usec, &u->mount.timeout_usec, "Mount" },
Lennart Poettering3e5235b2010-07-02 00:28:44 +02001897 { "DirectoryMode", config_parse_mode, &u->mount.directory_mode, "Mount" },
Lennart Poetteringe5373522010-04-10 17:53:17 +02001898 EXEC_CONTEXT_CONFIG_ITEMS(u->mount.exec_context, "Mount"),
Lennart Poettering034c6ed2010-01-26 04:18:44 +01001899
Lennart Poettering8d567582010-04-16 23:24:39 +02001900 { "Where", config_parse_path, &u->automount.where, "Automount" },
Lennart Poettering1cf18f22010-07-02 01:17:21 +02001901 { "DirectoryMode", config_parse_mode, &u->automount.directory_mode, "Automount" },
Lennart Poettering8d567582010-04-16 23:24:39 +02001902
Lennart Poettering01f78472010-05-24 05:25:33 +02001903 { "What", config_parse_path, &u->swap.parameters_fragment.what, "Swap" },
1904 { "Priority", config_parse_int, &u->swap.parameters_fragment.priority, "Swap" },
Maarten Lankhorst07b0b132010-05-09 18:44:11 +02001905
Lennart Poettering03fae012010-07-04 21:12:10 +02001906 { "OnActiveSec", config_parse_timer, &u->timer, "Timer" },
1907 { "OnBootSec", config_parse_timer, &u->timer, "Timer" },
1908 { "OnStartupSec", config_parse_timer, &u->timer, "Timer" },
1909 { "OnUnitActiveSec", config_parse_timer, &u->timer, "Timer" },
1910 { "OnUnitInactiveSec", config_parse_timer, &u->timer, "Timer" },
Lennart Poettering01f78472010-05-24 05:25:33 +02001911 { "Unit", config_parse_timer_unit, &u->timer, "Timer" },
1912
1913 { "PathExists", config_parse_path_spec, &u->path, "Path" },
1914 { "PathChanged", config_parse_path_spec, &u->path, "Path" },
1915 { "DirectoryNotEmpty", config_parse_path_spec, &u->path, "Path" },
1916 { "Unit", config_parse_path_unit, &u->path, "Path" },
Lennart Poettering871d7de2010-05-24 01:45:54 +02001917
Lennart Poettering10e87ee2010-06-16 01:58:50 +02001918 /* The [Install] section is ignored here. */
1919 { "Alias", NULL, NULL, "Install" },
1920 { "WantedBy", NULL, NULL, "Install" },
1921 { "Also", NULL, NULL, "Install" },
1922
Lennart Poettering3efd4192009-11-19 23:13:20 +01001923 { NULL, NULL, NULL, NULL }
1924 };
1925
Lennart Poettering034c6ed2010-01-26 04:18:44 +01001926#undef EXEC_CONTEXT_CONFIG_ITEMS
Lennart Poettering42f4e3c2010-01-19 02:56:37 +01001927
Lennart Poettering10e87ee2010-06-16 01:58:50 +02001928 const char *sections[4];
Lennart Poettering0301abf2010-01-27 00:15:56 +01001929 int r;
Lennart Poettering87f0e412010-01-26 21:39:06 +01001930 Set *symlink_names;
Lennart Poettering23a177e2010-04-06 02:43:58 +02001931 FILE *f = NULL;
1932 char *filename = NULL, *id = NULL;
1933 Unit *merged;
Lennart Poettering45fb0692010-07-17 00:57:51 +02001934 struct stat st;
Lennart Poettering23a177e2010-04-06 02:43:58 +02001935
Lennart Poetteringe5373522010-04-10 17:53:17 +02001936 if (!u) {
1937 /* Dirty dirty hack. */
1938 dump_items((FILE*) path, items);
1939 return 0;
1940 }
1941
Lennart Poettering23a177e2010-04-06 02:43:58 +02001942 assert(u);
Lennart Poetteringe5373522010-04-10 17:53:17 +02001943 assert(path);
Lennart Poettering3efd4192009-11-19 23:13:20 +01001944
Lennart Poettering09477262010-04-15 23:20:17 +02001945 sections[0] = "Unit";
Lennart Poettering87f0e412010-01-26 21:39:06 +01001946 sections[1] = section_table[u->meta.type];
Lennart Poettering10e87ee2010-06-16 01:58:50 +02001947 sections[2] = "Install";
1948 sections[3] = NULL;
Lennart Poettering42f4e3c2010-01-19 02:56:37 +01001949
Lennart Poettering87f0e412010-01-26 21:39:06 +01001950 if (!(symlink_names = set_new(string_hash_func, string_compare_func)))
1951 return -ENOMEM;
Lennart Poettering3efd4192009-11-19 23:13:20 +01001952
Lennart Poettering036643a2010-02-13 01:07:02 +01001953 if (path_is_absolute(path)) {
1954
1955 if (!(filename = strdup(path))) {
1956 r = -ENOMEM;
1957 goto finish;
1958 }
1959
1960 if ((r = open_follow(&filename, &f, symlink_names, &id)) < 0) {
1961 free(filename);
1962 filename = NULL;
1963
1964 if (r != -ENOENT)
1965 goto finish;
1966 }
1967
1968 } else {
1969 char **p;
1970
Lennart Poettering84e35432010-06-15 14:45:15 +02001971 STRV_FOREACH(p, u->meta.manager->lookup_paths.unit_path) {
Lennart Poettering036643a2010-02-13 01:07:02 +01001972
1973 /* Instead of opening the path right away, we manually
1974 * follow all symlinks and add their name to our unit
1975 * name set while doing so */
1976 if (!(filename = path_make_absolute(path, *p))) {
1977 r = -ENOMEM;
1978 goto finish;
1979 }
1980
Lennart Poetteringfe518222010-07-11 00:52:00 +02001981 if (u->meta.manager->unit_path_cache &&
1982 !set_get(u->meta.manager->unit_path_cache, filename))
1983 r = -ENOENT;
1984 else
1985 r = open_follow(&filename, &f, symlink_names, &id);
1986
1987 if (r < 0) {
Lennart Poettering036643a2010-02-13 01:07:02 +01001988 char *sn;
1989
1990 free(filename);
1991 filename = NULL;
1992
1993 if (r != -ENOENT)
1994 goto finish;
1995
1996 /* Empty the symlink names for the next run */
1997 while ((sn = set_steal_first(symlink_names)))
1998 free(sn);
1999
2000 continue;
2001 }
2002
2003 break;
2004 }
Lennart Poettering034c6ed2010-01-26 04:18:44 +01002005 }
2006
Lennart Poettering036643a2010-02-13 01:07:02 +01002007 if (!filename) {
Lennart Poettering8f054242010-07-21 03:13:15 +02002008 /* Hmm, no suitable file found? */
Lennart Poettering23a177e2010-04-06 02:43:58 +02002009 r = 0;
2010 goto finish;
2011 }
2012
2013 merged = u;
2014 if ((r = merge_by_names(&merged, symlink_names, id)) < 0)
2015 goto finish;
2016
2017 if (merged != u) {
Lennart Poetteringe5373522010-04-10 17:53:17 +02002018 u->meta.load_state = UNIT_MERGED;
Lennart Poettering23a177e2010-04-06 02:43:58 +02002019 r = 0;
Lennart Poettering0301abf2010-01-27 00:15:56 +01002020 goto finish;
2021 }
2022
Lennart Poettering45fb0692010-07-17 00:57:51 +02002023 zero(st);
2024 if (fstat(fileno(f), &st) < 0) {
2025 r = -errno;
2026 goto finish;
2027 }
2028
Lennart Poettering00dc5d72010-10-08 02:31:36 +02002029 if (null_or_empty(&st))
Lennart Poettering6daf4f92010-10-08 18:21:52 +02002030 u->meta.load_state = UNIT_MASKED;
Lennart Poettering00dc5d72010-10-08 02:31:36 +02002031 else {
2032 /* Now, parse the file contents */
2033 if ((r = config_parse(filename, f, sections, items, false, u)) < 0)
2034 goto finish;
2035
2036 u->meta.load_state = UNIT_LOADED;
2037 }
Lennart Poettering0301abf2010-01-27 00:15:56 +01002038
Lennart Poettering6be1e7d2010-02-14 01:01:10 +01002039 free(u->meta.fragment_path);
2040 u->meta.fragment_path = filename;
Lennart Poettering0301abf2010-01-27 00:15:56 +01002041 filename = NULL;
2042
Lennart Poettering45fb0692010-07-17 00:57:51 +02002043 u->meta.fragment_mtime = timespec_load(&st.st_mtim);
2044
Lennart Poettering23a177e2010-04-06 02:43:58 +02002045 r = 0;
Lennart Poettering87f0e412010-01-26 21:39:06 +01002046
2047finish:
Lennart Poettering53ec43c2010-06-15 02:45:26 +02002048 set_free_free(symlink_names);
Lennart Poettering0301abf2010-01-27 00:15:56 +01002049 free(filename);
2050
Lennart Poettering23a177e2010-04-06 02:43:58 +02002051 if (f)
2052 fclose(f);
2053
Lennart Poettering0301abf2010-01-27 00:15:56 +01002054 return r;
2055}
2056
Lennart Poetteringe5373522010-04-10 17:53:17 +02002057int unit_load_fragment(Unit *u) {
Lennart Poettering23a177e2010-04-06 02:43:58 +02002058 int r;
Lennart Poettering294d81f2010-07-21 03:28:10 +02002059 Iterator i;
2060 const char *t;
Lennart Poettering0301abf2010-01-27 00:15:56 +01002061
2062 assert(u);
Lennart Poettering294d81f2010-07-21 03:28:10 +02002063 assert(u->meta.load_state == UNIT_STUB);
2064 assert(u->meta.id);
Lennart Poettering0301abf2010-01-27 00:15:56 +01002065
Lennart Poettering294d81f2010-07-21 03:28:10 +02002066 /* First, try to find the unit under its id. We always look
2067 * for unit files in the default directories, to make it easy
2068 * to override things by placing things in /etc/systemd/system */
2069 if ((r = load_from_path(u, u->meta.id)) < 0)
2070 return r;
Lennart Poettering23a177e2010-04-06 02:43:58 +02002071
Lennart Poettering294d81f2010-07-21 03:28:10 +02002072 /* Try to find an alias we can load this with */
2073 if (u->meta.load_state == UNIT_STUB)
2074 SET_FOREACH(t, u->meta.names, i) {
2075
2076 if (t == u->meta.id)
2077 continue;
2078
2079 if ((r = load_from_path(u, t)) < 0)
2080 return r;
2081
2082 if (u->meta.load_state != UNIT_STUB)
2083 break;
2084 }
2085
2086 /* And now, try looking for it under the suggested (originally linked) path */
Lennart Poettering6ccb1b42010-09-27 20:31:23 +02002087 if (u->meta.load_state == UNIT_STUB && u->meta.fragment_path) {
2088
Lennart Poetteringe5373522010-04-10 17:53:17 +02002089 if ((r = load_from_path(u, u->meta.fragment_path)) < 0)
Lennart Poettering23a177e2010-04-06 02:43:58 +02002090 return r;
2091
Lennart Poettering6ccb1b42010-09-27 20:31:23 +02002092 if (u->meta.load_state == UNIT_STUB) {
2093 /* Hmm, this didn't work? Then let's get rid
2094 * of the fragment path stored for us, so that
2095 * we don't point to an invalid location. */
2096 free(u->meta.fragment_path);
2097 u->meta.fragment_path = NULL;
2098 }
2099 }
2100
Lennart Poettering294d81f2010-07-21 03:28:10 +02002101 /* Look for a template */
2102 if (u->meta.load_state == UNIT_STUB && u->meta.instance) {
2103 char *k;
Lennart Poettering0301abf2010-01-27 00:15:56 +01002104
Lennart Poettering294d81f2010-07-21 03:28:10 +02002105 if (!(k = unit_name_template(u->meta.id)))
2106 return -ENOMEM;
2107
2108 r = load_from_path(u, k);
2109 free(k);
2110
2111 if (r < 0)
Lennart Poettering9e2f7c12010-04-15 03:11:11 +02002112 return r;
Lennart Poettering890f4342010-02-14 01:08:20 +01002113
Lennart Poetteringe5373522010-04-10 17:53:17 +02002114 if (u->meta.load_state == UNIT_STUB)
Lennart Poettering23a177e2010-04-06 02:43:58 +02002115 SET_FOREACH(t, u->meta.names, i) {
2116
Lennart Poettering9e2f7c12010-04-15 03:11:11 +02002117 if (t == u->meta.id)
Lennart Poettering23a177e2010-04-06 02:43:58 +02002118 continue;
2119
Lennart Poettering294d81f2010-07-21 03:28:10 +02002120 if (!(k = unit_name_template(t)))
2121 return -ENOMEM;
2122
2123 r = load_from_path(u, k);
2124 free(k);
2125
2126 if (r < 0)
Lennart Poettering23a177e2010-04-06 02:43:58 +02002127 return r;
2128
Lennart Poetteringe5373522010-04-10 17:53:17 +02002129 if (u->meta.load_state != UNIT_STUB)
Lennart Poettering890f4342010-02-14 01:08:20 +01002130 break;
Lennart Poettering23a177e2010-04-06 02:43:58 +02002131 }
Lennart Poettering0301abf2010-01-27 00:15:56 +01002132 }
Lennart Poettering87f0e412010-01-26 21:39:06 +01002133
Lennart Poettering23a177e2010-04-06 02:43:58 +02002134 return 0;
Lennart Poettering3efd4192009-11-19 23:13:20 +01002135}
Lennart Poetteringe5373522010-04-10 17:53:17 +02002136
2137void unit_dump_config_items(FILE *f) {
2138 /* OK, this wins a prize for extreme ugliness. */
2139
2140 load_from_path(NULL, (const void*) f);
2141}