blob: 49b577fb2f51d1b894864ca9ba829c2b66dd87df [file] [log] [blame]
Lennart Poettering3efd4192009-11-19 23:13:20 +01001/*-*- Mode: C; c-basic-offset: 8 -*-*/
2
Lennart Poetteringa7334b02010-02-03 13:03:47 +01003/***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
Lennart Poettering87f0e412010-01-26 21:39:06 +010022#include <linux/oom.h>
Lennart Poettering3efd4192009-11-19 23:13:20 +010023#include <assert.h>
24#include <errno.h>
25#include <string.h>
Lennart Poettering87f0e412010-01-26 21:39:06 +010026#include <unistd.h>
27#include <fcntl.h>
Lennart Poettering94f04342010-01-30 01:55:42 +010028#include <sched.h>
29#include <sys/prctl.h>
Lennart Poettering15ae4222010-04-21 22:15:06 +020030#include <sys/mount.h>
Kay Sievers25e870b2010-04-24 05:05:01 +020031#include <linux/fs.h>
Lennart Poettering3efd4192009-11-19 23:13:20 +010032
Lennart Poettering87f0e412010-01-26 21:39:06 +010033#include "unit.h"
Lennart Poettering3efd4192009-11-19 23:13:20 +010034#include "strv.h"
35#include "conf-parser.h"
36#include "load-fragment.h"
Lennart Poettering16354ef2010-01-20 19:19:53 +010037#include "log.h"
Lennart Poettering9eba9da2010-01-29 20:46:22 +010038#include "ioprio.h"
Lennart Poettering94f04342010-01-30 01:55:42 +010039#include "securebits.h"
40#include "missing.h"
Lennart Poettering9e2f7c12010-04-15 03:11:11 +020041#include "unit-name.h"
Lennart Poettering3efd4192009-11-19 23:13:20 +010042
Lennart Poetteringddb26e12010-06-18 06:06:24 +020043#define COMMENTS "#;\n"
44#define LINE_MAX 4096
45
Lennart Poettering0d87eb42010-04-13 02:22:41 +020046#define DEFINE_CONFIG_PARSE_ENUM(function,name,type,msg) \
47 static int function( \
48 const char *filename, \
49 unsigned line, \
50 const char *section, \
51 const char *lvalue, \
52 const char *rvalue, \
53 void *data, \
54 void *userdata) { \
55 \
56 type *i = data, x; \
57 \
58 assert(filename); \
59 assert(lvalue); \
60 assert(rvalue); \
61 assert(data); \
62 \
63 if ((x = name##_from_string(rvalue)) < 0) { \
64 log_error("[%s:%u] " msg ": %s", filename, line, rvalue); \
65 return -EBADMSG; \
66 } \
67 \
68 *i = x; \
69 \
70 return 0; \
71 }
72
Lennart Poettering42f4e3c2010-01-19 02:56:37 +010073static int config_parse_deps(
Lennart Poettering3efd4192009-11-19 23:13:20 +010074 const char *filename,
75 unsigned line,
76 const char *section,
77 const char *lvalue,
78 const char *rvalue,
79 void *data,
80 void *userdata) {
81
Lennart Poettering87f0e412010-01-26 21:39:06 +010082 UnitDependency d = PTR_TO_UINT(data);
83 Unit *u = userdata;
Lennart Poettering3efd4192009-11-19 23:13:20 +010084 char *w;
85 size_t l;
86 char *state;
87
88 assert(filename);
89 assert(lvalue);
90 assert(rvalue);
Lennart Poettering3efd4192009-11-19 23:13:20 +010091
Lennart Poetteringf62c0e42010-02-14 01:07:36 +010092 FOREACH_WORD(w, l, rvalue, state) {
Lennart Poettering9e2f7c12010-04-15 03:11:11 +020093 char *t, *k;
Lennart Poettering3efd4192009-11-19 23:13:20 +010094 int r;
Lennart Poettering3efd4192009-11-19 23:13:20 +010095
96 if (!(t = strndup(w, l)))
97 return -ENOMEM;
98
Lennart Poettering9e2f7c12010-04-15 03:11:11 +020099 k = unit_name_printf(u, t);
Lennart Poettering3efd4192009-11-19 23:13:20 +0100100 free(t);
101
Lennart Poettering9e2f7c12010-04-15 03:11:11 +0200102 if (!k)
103 return -ENOMEM;
104
Lennart Poettering701cc382010-04-21 06:01:13 +0200105 r = unit_add_dependency_by_name(u, d, k, NULL, true);
Lennart Poettering9e2f7c12010-04-15 03:11:11 +0200106 free(k);
107
Lennart Poettering3efd4192009-11-19 23:13:20 +0100108 if (r < 0)
109 return r;
Lennart Poettering3efd4192009-11-19 23:13:20 +0100110 }
111
112 return 0;
113}
114
Lennart Poettering42f4e3c2010-01-19 02:56:37 +0100115static int config_parse_names(
Lennart Poettering87d15152010-01-18 23:50:13 +0100116 const char *filename,
117 unsigned line,
118 const char *section,
119 const char *lvalue,
120 const char *rvalue,
121 void *data,
122 void *userdata) {
123
Lennart Poettering87f0e412010-01-26 21:39:06 +0100124 Unit *u = userdata;
Lennart Poettering87d15152010-01-18 23:50:13 +0100125 char *w;
126 size_t l;
127 char *state;
128
129 assert(filename);
130 assert(lvalue);
131 assert(rvalue);
132 assert(data);
133
Lennart Poetteringf62c0e42010-02-14 01:07:36 +0100134 FOREACH_WORD(w, l, rvalue, state) {
Lennart Poettering9e2f7c12010-04-15 03:11:11 +0200135 char *t, *k;
Lennart Poettering87d15152010-01-18 23:50:13 +0100136 int r;
Lennart Poettering87d15152010-01-18 23:50:13 +0100137
138 if (!(t = strndup(w, l)))
139 return -ENOMEM;
140
Lennart Poettering9e2f7c12010-04-15 03:11:11 +0200141 k = unit_name_printf(u, t);
Lennart Poettering87d15152010-01-18 23:50:13 +0100142 free(t);
Lennart Poettering23a177e2010-04-06 02:43:58 +0200143
Lennart Poettering9e2f7c12010-04-15 03:11:11 +0200144 if (!k)
145 return -ENOMEM;
146
147 r = unit_merge_by_name(u, k);
148 free(k);
149
Lennart Poettering23a177e2010-04-06 02:43:58 +0200150 if (r < 0)
151 return r;
Lennart Poettering87d15152010-01-18 23:50:13 +0100152 }
153
154 return 0;
155}
156
Lennart Poetteringf2d37692010-06-18 23:25:19 +0200157static int config_parse_string_printf(
Lennart Poettering932921b2010-04-24 03:11:01 +0200158 const char *filename,
159 unsigned line,
160 const char *section,
161 const char *lvalue,
162 const char *rvalue,
163 void *data,
164 void *userdata) {
165
166 Unit *u = userdata;
Lennart Poetteringf2d37692010-06-18 23:25:19 +0200167 char **s = data;
Lennart Poettering932921b2010-04-24 03:11:01 +0200168 char *k;
169
170 assert(filename);
171 assert(lvalue);
172 assert(rvalue);
Lennart Poetteringf2d37692010-06-18 23:25:19 +0200173 assert(s);
174 assert(u);
Lennart Poettering932921b2010-04-24 03:11:01 +0200175
176 if (!(k = unit_full_printf(u, rvalue)))
177 return -ENOMEM;
178
Lennart Poetteringf2d37692010-06-18 23:25:19 +0200179 free(*s);
Lennart Poettering932921b2010-04-24 03:11:01 +0200180 if (*k)
Lennart Poetteringf2d37692010-06-18 23:25:19 +0200181 *s = k;
Lennart Poettering932921b2010-04-24 03:11:01 +0200182 else {
183 free(k);
Lennart Poetteringf2d37692010-06-18 23:25:19 +0200184 *s = NULL;
Lennart Poettering932921b2010-04-24 03:11:01 +0200185 }
186
187 return 0;
188}
189
Lennart Poettering42f4e3c2010-01-19 02:56:37 +0100190static int config_parse_listen(
191 const char *filename,
192 unsigned line,
193 const char *section,
194 const char *lvalue,
195 const char *rvalue,
196 void *data,
197 void *userdata) {
198
Lennart Poettering16354ef2010-01-20 19:19:53 +0100199 int r;
Lennart Poettering542563b2010-01-23 03:35:54 +0100200 SocketPort *p;
201 Socket *s;
Lennart Poettering16354ef2010-01-20 19:19:53 +0100202
Lennart Poettering42f4e3c2010-01-19 02:56:37 +0100203 assert(filename);
204 assert(lvalue);
205 assert(rvalue);
206 assert(data);
207
Lennart Poettering542563b2010-01-23 03:35:54 +0100208 s = (Socket*) data;
209
210 if (!(p = new0(SocketPort, 1)))
211 return -ENOMEM;
212
213 if (streq(lvalue, "ListenFIFO")) {
214 p->type = SOCKET_FIFO;
215
216 if (!(p->path = strdup(rvalue))) {
217 free(p);
218 return -ENOMEM;
219 }
Lennart Poettering01f78472010-05-24 05:25:33 +0200220
221 path_kill_slashes(p->path);
Lennart Poettering542563b2010-01-23 03:35:54 +0100222 } else {
223 p->type = SOCKET_SOCKET;
224
225 if ((r = socket_address_parse(&p->address, rvalue)) < 0) {
226 log_error("[%s:%u] Failed to parse address value: %s", filename, line, rvalue);
227 free(p);
228 return r;
229 }
230
231 if (streq(lvalue, "ListenStream"))
232 p->address.type = SOCK_STREAM;
233 else if (streq(lvalue, "ListenDatagram"))
234 p->address.type = SOCK_DGRAM;
235 else {
236 assert(streq(lvalue, "ListenSequentialPacket"));
237 p->address.type = SOCK_SEQPACKET;
238 }
239
240 if (socket_address_family(&p->address) != AF_LOCAL && p->address.type == SOCK_SEQPACKET) {
241 free(p);
242 return -EPROTONOSUPPORT;
243 }
Lennart Poettering16354ef2010-01-20 19:19:53 +0100244 }
245
Lennart Poettering542563b2010-01-23 03:35:54 +0100246 p->fd = -1;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100247 LIST_PREPEND(SocketPort, port, s->ports, p);
Lennart Poettering542563b2010-01-23 03:35:54 +0100248
Lennart Poettering16354ef2010-01-20 19:19:53 +0100249 return 0;
Lennart Poettering42f4e3c2010-01-19 02:56:37 +0100250}
251
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100252static int config_parse_socket_bind(
Lennart Poettering42f4e3c2010-01-19 02:56:37 +0100253 const char *filename,
254 unsigned line,
255 const char *section,
256 const char *lvalue,
257 const char *rvalue,
258 void *data,
259 void *userdata) {
260
Lennart Poettering542563b2010-01-23 03:35:54 +0100261 Socket *s;
Lennart Poetteringc0120d92010-05-21 23:41:25 +0200262 SocketAddressBindIPv6Only b;
Lennart Poettering42f4e3c2010-01-19 02:56:37 +0100263
264 assert(filename);
265 assert(lvalue);
266 assert(rvalue);
267 assert(data);
268
Lennart Poettering542563b2010-01-23 03:35:54 +0100269 s = (Socket*) data;
270
Lennart Poetteringc0120d92010-05-21 23:41:25 +0200271 if ((b = socket_address_bind_ipv6_only_from_string(rvalue)) < 0) {
272 int r;
Lennart Poettering42f4e3c2010-01-19 02:56:37 +0100273
Lennart Poetteringc0120d92010-05-21 23:41:25 +0200274 if ((r = parse_boolean(rvalue)) < 0) {
275 log_error("[%s:%u] Failed to parse bind IPv6 only value: %s", filename, line, rvalue);
276 return -EBADMSG;
277 }
278
279 s->bind_ipv6_only = r ? SOCKET_ADDRESS_IPV6_ONLY : SOCKET_ADDRESS_BOTH;
280 } else
281 s->bind_ipv6_only = b;
Lennart Poettering542563b2010-01-23 03:35:54 +0100282
Lennart Poettering42f4e3c2010-01-19 02:56:37 +0100283 return 0;
284}
285
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100286static int config_parse_nice(
287 const char *filename,
288 unsigned line,
289 const char *section,
290 const char *lvalue,
291 const char *rvalue,
292 void *data,
293 void *userdata) {
294
Lennart Poetteringfb33a392010-01-28 02:53:56 +0100295 ExecContext *c = data;
296 int priority, r;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100297
298 assert(filename);
299 assert(lvalue);
300 assert(rvalue);
301 assert(data);
302
303 if ((r = safe_atoi(rvalue, &priority)) < 0) {
304 log_error("[%s:%u] Failed to parse nice priority: %s", filename, line, rvalue);
305 return r;
306 }
307
308 if (priority < PRIO_MIN || priority >= PRIO_MAX) {
309 log_error("[%s:%u] Nice priority out of range: %s", filename, line, rvalue);
310 return -ERANGE;
311 }
312
Lennart Poetteringfb33a392010-01-28 02:53:56 +0100313 c->nice = priority;
314 c->nice_set = false;
315
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100316 return 0;
317}
318
319static int config_parse_oom_adjust(
320 const char *filename,
321 unsigned line,
322 const char *section,
323 const char *lvalue,
324 const char *rvalue,
325 void *data,
326 void *userdata) {
327
Lennart Poetteringfb33a392010-01-28 02:53:56 +0100328 ExecContext *c = data;
329 int oa, r;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100330
331 assert(filename);
332 assert(lvalue);
333 assert(rvalue);
334 assert(data);
335
336 if ((r = safe_atoi(rvalue, &oa)) < 0) {
337 log_error("[%s:%u] Failed to parse OOM adjust value: %s", filename, line, rvalue);
338 return r;
339 }
340
341 if (oa < OOM_DISABLE || oa > OOM_ADJUST_MAX) {
342 log_error("[%s:%u] OOM adjust value out of range: %s", filename, line, rvalue);
343 return -ERANGE;
344 }
345
Lennart Poetteringfb33a392010-01-28 02:53:56 +0100346 c->oom_adjust = oa;
347 c->oom_adjust_set = true;
348
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100349 return 0;
350}
351
Lennart Poetteringb5a06992010-02-12 02:02:14 +0100352static int config_parse_mode(
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100353 const char *filename,
354 unsigned line,
355 const char *section,
356 const char *lvalue,
357 const char *rvalue,
358 void *data,
359 void *userdata) {
360
361 mode_t *m = data;
362 long l;
363 char *x = NULL;
364
365 assert(filename);
366 assert(lvalue);
367 assert(rvalue);
368 assert(data);
369
370 errno = 0;
371 l = strtol(rvalue, &x, 8);
372 if (!x || *x || errno) {
Lennart Poetteringb5a06992010-02-12 02:02:14 +0100373 log_error("[%s:%u] Failed to parse mode value: %s", filename, line, rvalue);
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100374 return errno ? -errno : -EINVAL;
375 }
376
Lennart Poetteringb5a06992010-02-12 02:02:14 +0100377 if (l < 0000 || l > 07777) {
378 log_error("[%s:%u] mode value out of range: %s", filename, line, rvalue);
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100379 return -ERANGE;
380 }
381
382 *m = (mode_t) l;
383 return 0;
384}
385
386static int config_parse_exec(
387 const char *filename,
388 unsigned line,
389 const char *section,
390 const char *lvalue,
391 const char *rvalue,
392 void *data,
393 void *userdata) {
394
Lennart Poetteringa6a80b42010-02-14 01:05:55 +0100395 ExecCommand **e = data, *nce = NULL;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100396 char **n;
397 char *w;
398 unsigned k;
399 size_t l;
Lennart Poettering6c666e22010-05-19 21:51:53 +0200400 char *state, *path = NULL;
401 bool honour_argv0, write_to_path;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100402
403 assert(filename);
404 assert(lvalue);
405 assert(rvalue);
406 assert(data);
407
Lennart Poettering6c666e22010-05-19 21:51:53 +0200408 /* We accept an absolute path as first argument, or
409 * alternatively an absolute prefixed with @ to allow
410 * overriding of argv[0]. */
411
412 honour_argv0 = rvalue[0] == '@';
413
414 if (rvalue[honour_argv0 ? 1 : 0] != '/') {
415 log_error("[%s:%u] Invalid executable path in command line: %s", filename, line, rvalue);
416 return -EINVAL;
417 }
418
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100419 k = 0;
420 FOREACH_WORD_QUOTED(w, l, rvalue, state)
421 k++;
422
Lennart Poettering6c666e22010-05-19 21:51:53 +0200423 if (!(n = new(char*, k + (honour_argv0 ? 0 : 1))))
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100424 return -ENOMEM;
425
Lennart Poettering44d8db92010-01-26 07:02:51 +0100426 k = 0;
Lennart Poettering6c666e22010-05-19 21:51:53 +0200427 write_to_path = honour_argv0;
428 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
429 if (write_to_path) {
430 if (!(path = strndup(w+1, l-1)))
431 goto fail;
432 write_to_path = false;
433 } else {
434 if (!(n[k++] = strndup(w, l)))
435 goto fail;
436 }
437 }
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100438
439 n[k] = NULL;
440
Lennart Poettering6c666e22010-05-19 21:51:53 +0200441 if (!n[0]) {
442 log_error("[%s:%u] Invalid command line: %s", filename, line, rvalue);
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100443 strv_free(n);
444 return -EINVAL;
445 }
446
Lennart Poettering6c666e22010-05-19 21:51:53 +0200447 if (!path)
448 if (!(path = strdup(n[0])))
449 goto fail;
450
451 assert(path_is_absolute(path));
452
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100453 if (!(nce = new0(ExecCommand, 1)))
454 goto fail;
455
456 nce->argv = n;
Lennart Poettering6c666e22010-05-19 21:51:53 +0200457 nce->path = path;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100458
Lennart Poettering01f78472010-05-24 05:25:33 +0200459 path_kill_slashes(nce->path);
460
Lennart Poetteringa6a80b42010-02-14 01:05:55 +0100461 exec_command_append_list(e, nce);
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100462
463 return 0;
464
465fail:
Lennart Poettering6c666e22010-05-19 21:51:53 +0200466 n[k] = NULL;
467 strv_free(n);
468 free(path);
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100469 free(nce);
470
471 return -ENOMEM;
472}
473
474static int config_parse_usec(
475 const char *filename,
476 unsigned line,
477 const char *section,
478 const char *lvalue,
479 const char *rvalue,
480 void *data,
481 void *userdata) {
482
483 usec_t *usec = data;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100484 int r;
485
486 assert(filename);
487 assert(lvalue);
488 assert(rvalue);
489 assert(data);
490
Lennart Poettering24a6e4a2010-04-23 20:26:59 +0200491 if ((r = parse_usec(rvalue, usec)) < 0) {
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100492 log_error("[%s:%u] Failed to parse time value: %s", filename, line, rvalue);
493 return r;
494 }
495
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100496 return 0;
497}
498
Lennart Poettering0d87eb42010-04-13 02:22:41 +0200499DEFINE_CONFIG_PARSE_ENUM(config_parse_service_type, service_type, ServiceType, "Failed to parse service type");
500DEFINE_CONFIG_PARSE_ENUM(config_parse_service_restart, service_restart, ServiceRestart, "Failed to parse service restart specifier");
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100501
Lennart Poettering47be8702010-02-03 14:21:48 +0100502static int config_parse_bindtodevice(
Lennart Poetteringacbb0222010-01-27 04:31:52 +0100503 const char *filename,
504 unsigned line,
505 const char *section,
506 const char *lvalue,
507 const char *rvalue,
508 void *data,
509 void *userdata) {
510
511 Socket *s = data;
512 char *n;
513
514 assert(filename);
515 assert(lvalue);
516 assert(rvalue);
517 assert(data);
518
519 if (rvalue[0] && !streq(rvalue, "*")) {
520 if (!(n = strdup(rvalue)))
521 return -ENOMEM;
522 } else
523 n = NULL;
524
525 free(s->bind_to_device);
526 s->bind_to_device = n;
527
528 return 0;
529}
530
Lennart Poettering0d87eb42010-04-13 02:22:41 +0200531DEFINE_CONFIG_PARSE_ENUM(config_parse_output, exec_output, ExecOutput, "Failed to parse output specifier");
532DEFINE_CONFIG_PARSE_ENUM(config_parse_input, exec_input, ExecInput, "Failed to parse input specifier");
Lennart Poettering071830f2010-01-28 02:06:20 +0100533
Lennart Poettering47be8702010-02-03 14:21:48 +0100534static int config_parse_facility(
Lennart Poettering071830f2010-01-28 02:06:20 +0100535 const char *filename,
536 unsigned line,
537 const char *section,
538 const char *lvalue,
539 const char *rvalue,
540 void *data,
541 void *userdata) {
542
Lennart Poettering071830f2010-01-28 02:06:20 +0100543
Lennart Poettering94f04342010-01-30 01:55:42 +0100544 int *o = data, x;
Lennart Poettering071830f2010-01-28 02:06:20 +0100545
546 assert(filename);
547 assert(lvalue);
548 assert(rvalue);
549 assert(data);
550
Lennart Poettering0d87eb42010-04-13 02:22:41 +0200551 if ((x = log_facility_from_string(rvalue)) < 0) {
552 log_error("[%s:%u] Failed to parse log facility: %s", filename, line, rvalue);
553 return -EBADMSG;
554 }
Lennart Poettering94f04342010-01-30 01:55:42 +0100555
556 *o = LOG_MAKEPRI(x, LOG_PRI(*o));
Lennart Poettering071830f2010-01-28 02:06:20 +0100557
558 return 0;
559}
560
Lennart Poettering47be8702010-02-03 14:21:48 +0100561static int config_parse_level(
Lennart Poettering071830f2010-01-28 02:06:20 +0100562 const char *filename,
563 unsigned line,
564 const char *section,
565 const char *lvalue,
566 const char *rvalue,
567 void *data,
568 void *userdata) {
569
Lennart Poettering071830f2010-01-28 02:06:20 +0100570
Lennart Poettering94f04342010-01-30 01:55:42 +0100571 int *o = data, x;
Lennart Poettering071830f2010-01-28 02:06:20 +0100572
573 assert(filename);
574 assert(lvalue);
575 assert(rvalue);
576 assert(data);
577
Lennart Poettering0d87eb42010-04-13 02:22:41 +0200578 if ((x = log_level_from_string(rvalue)) < 0) {
579 log_error("[%s:%u] Failed to parse log level: %s", filename, line, rvalue);
580 return -EBADMSG;
581 }
Lennart Poettering071830f2010-01-28 02:06:20 +0100582
Lennart Poettering94f04342010-01-30 01:55:42 +0100583 *o = LOG_MAKEPRI(LOG_FAC(*o), x);
Lennart Poettering071830f2010-01-28 02:06:20 +0100584 return 0;
585}
586
Lennart Poettering47be8702010-02-03 14:21:48 +0100587static int config_parse_io_class(
Lennart Poettering9eba9da2010-01-29 20:46:22 +0100588 const char *filename,
589 unsigned line,
590 const char *section,
591 const char *lvalue,
592 const char *rvalue,
593 void *data,
594 void *userdata) {
595
Lennart Poettering9eba9da2010-01-29 20:46:22 +0100596 ExecContext *c = data;
Lennart Poettering94f04342010-01-30 01:55:42 +0100597 int x;
Lennart Poettering9eba9da2010-01-29 20:46:22 +0100598
599 assert(filename);
600 assert(lvalue);
601 assert(rvalue);
602 assert(data);
603
Lennart Poettering0d87eb42010-04-13 02:22:41 +0200604 if ((x = ioprio_class_from_string(rvalue)) < 0) {
605 log_error("[%s:%u] Failed to parse IO scheduling class: %s", filename, line, rvalue);
606 return -EBADMSG;
607 }
Lennart Poettering9eba9da2010-01-29 20:46:22 +0100608
Lennart Poettering94f04342010-01-30 01:55:42 +0100609 c->ioprio = IOPRIO_PRIO_VALUE(x, IOPRIO_PRIO_DATA(c->ioprio));
Lennart Poettering9eba9da2010-01-29 20:46:22 +0100610 c->ioprio_set = true;
611
612 return 0;
613}
614
Lennart Poettering47be8702010-02-03 14:21:48 +0100615static int config_parse_io_priority(
Lennart Poettering9eba9da2010-01-29 20:46:22 +0100616 const char *filename,
617 unsigned line,
618 const char *section,
619 const char *lvalue,
620 const char *rvalue,
621 void *data,
622 void *userdata) {
623
624 ExecContext *c = data;
625 int i;
626
627 assert(filename);
628 assert(lvalue);
629 assert(rvalue);
630 assert(data);
631
Lennart Poettering94f04342010-01-30 01:55:42 +0100632 if (safe_atoi(rvalue, &i) < 0 || i < 0 || i >= IOPRIO_BE_NR) {
Lennart Poettering9eba9da2010-01-29 20:46:22 +0100633 log_error("[%s:%u] Failed to parse io priority: %s", filename, line, rvalue);
634 return -EBADMSG;
635 }
636
Lennart Poettering94f04342010-01-30 01:55:42 +0100637 c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_PRIO_CLASS(c->ioprio), i);
Lennart Poettering9eba9da2010-01-29 20:46:22 +0100638 c->ioprio_set = true;
639
640 return 0;
641}
642
Lennart Poettering47be8702010-02-03 14:21:48 +0100643static int config_parse_cpu_sched_policy(
Lennart Poettering94f04342010-01-30 01:55:42 +0100644 const char *filename,
645 unsigned line,
646 const char *section,
647 const char *lvalue,
648 const char *rvalue,
649 void *data,
650 void *userdata) {
651
652
653 ExecContext *c = data;
654 int x;
655
656 assert(filename);
657 assert(lvalue);
658 assert(rvalue);
659 assert(data);
660
Lennart Poettering0d87eb42010-04-13 02:22:41 +0200661 if ((x = sched_policy_from_string(rvalue)) < 0) {
662 log_error("[%s:%u] Failed to parse CPU scheduling policy: %s", filename, line, rvalue);
663 return -EBADMSG;
664 }
Lennart Poettering94f04342010-01-30 01:55:42 +0100665
666 c->cpu_sched_policy = x;
667 c->cpu_sched_set = true;
668
669 return 0;
670}
671
Lennart Poettering47be8702010-02-03 14:21:48 +0100672static int config_parse_cpu_sched_prio(
Lennart Poettering94f04342010-01-30 01:55:42 +0100673 const char *filename,
674 unsigned line,
675 const char *section,
676 const char *lvalue,
677 const char *rvalue,
678 void *data,
679 void *userdata) {
680
681 ExecContext *c = data;
682 int i;
683
684 assert(filename);
685 assert(lvalue);
686 assert(rvalue);
687 assert(data);
688
689 /* On Linux RR/FIFO have the same range */
690 if (safe_atoi(rvalue, &i) < 0 || i < sched_get_priority_min(SCHED_RR) || i > sched_get_priority_max(SCHED_RR)) {
691 log_error("[%s:%u] Failed to parse CPU scheduling priority: %s", filename, line, rvalue);
692 return -EBADMSG;
693 }
694
695 c->cpu_sched_priority = i;
696 c->cpu_sched_set = true;
697
698 return 0;
699}
700
Lennart Poettering47be8702010-02-03 14:21:48 +0100701static int config_parse_cpu_affinity(
Lennart Poettering94f04342010-01-30 01:55:42 +0100702 const char *filename,
703 unsigned line,
704 const char *section,
705 const char *lvalue,
706 const char *rvalue,
707 void *data,
708 void *userdata) {
709
710 ExecContext *c = data;
711 char *w;
712 size_t l;
713 char *state;
714
715 assert(filename);
716 assert(lvalue);
717 assert(rvalue);
718 assert(data);
719
Lennart Poetteringf62c0e42010-02-14 01:07:36 +0100720 FOREACH_WORD(w, l, rvalue, state) {
Lennart Poettering94f04342010-01-30 01:55:42 +0100721 char *t;
722 int r;
723 unsigned cpu;
724
725 if (!(t = strndup(w, l)))
726 return -ENOMEM;
727
728 r = safe_atou(t, &cpu);
729 free(t);
730
731 if (r < 0 || cpu >= CPU_SETSIZE) {
732 log_error("[%s:%u] Failed to parse CPU affinity: %s", filename, line, rvalue);
733 return -EBADMSG;
734 }
735
736 CPU_SET(cpu, &c->cpu_affinity);
737 }
738
739 c->cpu_affinity_set = true;
740
741 return 0;
742}
743
Lennart Poettering47be8702010-02-03 14:21:48 +0100744static int config_parse_capabilities(
Lennart Poettering94f04342010-01-30 01:55:42 +0100745 const char *filename,
746 unsigned line,
747 const char *section,
748 const char *lvalue,
749 const char *rvalue,
750 void *data,
751 void *userdata) {
752
753 ExecContext *c = data;
754 cap_t cap;
755
756 assert(filename);
757 assert(lvalue);
758 assert(rvalue);
759 assert(data);
760
761 if (!(cap = cap_from_text(rvalue))) {
762 if (errno == ENOMEM)
763 return -ENOMEM;
764
765 log_error("[%s:%u] Failed to parse capabilities: %s", filename, line, rvalue);
766 return -EBADMSG;
767 }
768
769 if (c->capabilities)
770 cap_free(c->capabilities);
771 c->capabilities = cap;
772
773 return 0;
774}
775
Lennart Poettering47be8702010-02-03 14:21:48 +0100776static int config_parse_secure_bits(
Lennart Poettering94f04342010-01-30 01:55:42 +0100777 const char *filename,
778 unsigned line,
779 const char *section,
780 const char *lvalue,
781 const char *rvalue,
782 void *data,
783 void *userdata) {
784
785 ExecContext *c = data;
786 char *w;
787 size_t l;
788 char *state;
789
790 assert(filename);
791 assert(lvalue);
792 assert(rvalue);
793 assert(data);
794
Lennart Poetteringf62c0e42010-02-14 01:07:36 +0100795 FOREACH_WORD(w, l, rvalue, state) {
Lennart Poettering94f04342010-01-30 01:55:42 +0100796 if (first_word(w, "keep-caps"))
797 c->secure_bits |= SECURE_KEEP_CAPS;
798 else if (first_word(w, "keep-caps-locked"))
799 c->secure_bits |= SECURE_KEEP_CAPS_LOCKED;
800 else if (first_word(w, "no-setuid-fixup"))
801 c->secure_bits |= SECURE_NO_SETUID_FIXUP;
802 else if (first_word(w, "no-setuid-fixup-locked"))
803 c->secure_bits |= SECURE_NO_SETUID_FIXUP_LOCKED;
804 else if (first_word(w, "noroot"))
805 c->secure_bits |= SECURE_NOROOT;
806 else if (first_word(w, "noroot-locked"))
807 c->secure_bits |= SECURE_NOROOT_LOCKED;
808 else {
809 log_error("[%s:%u] Failed to parse secure bits: %s", filename, line, rvalue);
810 return -EBADMSG;
811 }
812 }
813
814 return 0;
815}
816
Lennart Poettering47be8702010-02-03 14:21:48 +0100817static int config_parse_bounding_set(
Lennart Poettering94f04342010-01-30 01:55:42 +0100818 const char *filename,
819 unsigned line,
820 const char *section,
821 const char *lvalue,
822 const char *rvalue,
823 void *data,
824 void *userdata) {
825
826 ExecContext *c = data;
827 char *w;
828 size_t l;
829 char *state;
830
831 assert(filename);
832 assert(lvalue);
833 assert(rvalue);
834 assert(data);
835
Lennart Poetteringf62c0e42010-02-14 01:07:36 +0100836 FOREACH_WORD(w, l, rvalue, state) {
Lennart Poettering94f04342010-01-30 01:55:42 +0100837 char *t;
838 int r;
839 cap_value_t cap;
840
841 if (!(t = strndup(w, l)))
842 return -ENOMEM;
843
844 r = cap_from_name(t, &cap);
845 free(t);
846
847 if (r < 0) {
848 log_error("[%s:%u] Failed to parse capability bounding set: %s", filename, line, rvalue);
849 return -EBADMSG;
850 }
851
852 c->capability_bounding_set_drop |= 1 << cap;
853 }
854
855 return 0;
856}
857
858static int config_parse_timer_slack_ns(
859 const char *filename,
860 unsigned line,
861 const char *section,
862 const char *lvalue,
863 const char *rvalue,
864 void *data,
865 void *userdata) {
866
867 ExecContext *c = data;
868 unsigned long u;
869 int r;
870
871 assert(filename);
872 assert(lvalue);
873 assert(rvalue);
874 assert(data);
875
876 if ((r = safe_atolu(rvalue, &u)) < 0) {
877 log_error("[%s:%u] Failed to parse time slack value: %s", filename, line, rvalue);
878 return r;
879 }
880
881 c->timer_slack_ns = u;
882
883 return 0;
884}
885
886static int config_parse_limit(
887 const char *filename,
888 unsigned line,
889 const char *section,
890 const char *lvalue,
891 const char *rvalue,
892 void *data,
893 void *userdata) {
894
895 struct rlimit **rl = data;
896 unsigned long long u;
897 int r;
898
899 assert(filename);
900 assert(lvalue);
901 assert(rvalue);
902 assert(data);
903
904 if ((r = safe_atollu(rvalue, &u)) < 0) {
905 log_error("[%s:%u] Failed to parse resource value: %s", filename, line, rvalue);
906 return r;
907 }
908
909 if (!*rl)
910 if (!(*rl = new(struct rlimit, 1)))
911 return -ENOMEM;
912
913 (*rl)->rlim_cur = (*rl)->rlim_max = (rlim_t) u;
914 return 0;
915}
916
Lennart Poettering8e274522010-03-31 16:29:55 +0200917static int config_parse_cgroup(
918 const char *filename,
919 unsigned line,
920 const char *section,
921 const char *lvalue,
922 const char *rvalue,
923 void *data,
924 void *userdata) {
925
926 Unit *u = userdata;
927 char *w;
928 size_t l;
929 char *state;
930
931 FOREACH_WORD(w, l, rvalue, state) {
932 char *t;
933 int r;
934
935 if (!(t = strndup(w, l)))
936 return -ENOMEM;
937
938 r = unit_add_cgroup_from_text(u, t);
939 free(t);
940
941 if (r < 0)
942 return r;
943 }
944
945 return 0;
946}
947
Lennart Poetteringa9a1e002010-04-06 02:40:10 +0200948static int config_parse_sysv_priority(
949 const char *filename,
950 unsigned line,
951 const char *section,
952 const char *lvalue,
953 const char *rvalue,
954 void *data,
955 void *userdata) {
956
957 int *priority = data;
958 int r, i;
959
960 assert(filename);
961 assert(lvalue);
962 assert(rvalue);
963 assert(data);
964
965 if ((r = safe_atoi(rvalue, &i)) < 0 || i < 0) {
966 log_error("[%s:%u] Failed to parse SysV start priority: %s", filename, line, rvalue);
967 return r;
968 }
969
970 *priority = (int) i;
971 return 0;
972}
973
Lennart Poettering0d87eb42010-04-13 02:22:41 +0200974DEFINE_CONFIG_PARSE_ENUM(config_parse_kill_mode, kill_mode, KillMode, "Failed to parse kill mode");
Lennart Poettering50159e62010-04-08 00:52:14 +0200975
Lennart Poettering15ae4222010-04-21 22:15:06 +0200976static int config_parse_mount_flags(
977 const char *filename,
978 unsigned line,
979 const char *section,
980 const char *lvalue,
981 const char *rvalue,
982 void *data,
983 void *userdata) {
984
985 ExecContext *c = data;
986 char *w;
987 size_t l;
988 char *state;
989 unsigned long flags = 0;
990
991 assert(filename);
992 assert(lvalue);
993 assert(rvalue);
994 assert(data);
995
996 FOREACH_WORD(w, l, rvalue, state) {
997 if (strncmp(w, "shared", l) == 0)
998 flags |= MS_SHARED;
999 else if (strncmp(w, "slave", l) == 0)
1000 flags |= MS_SLAVE;
1001 else if (strncmp(w, "private", l) == 0)
1002 flags |= MS_PRIVATE;
1003 else {
1004 log_error("[%s:%u] Failed to parse mount flags: %s", filename, line, rvalue);
1005 return -EINVAL;
1006 }
1007 }
1008
1009 c->mount_flags = flags;
1010 return 0;
1011}
1012
Lennart Poettering871d7de2010-05-24 01:45:54 +02001013static int config_parse_timer(
1014 const char *filename,
1015 unsigned line,
1016 const char *section,
1017 const char *lvalue,
1018 const char *rvalue,
1019 void *data,
1020 void *userdata) {
1021
1022 Timer *t = data;
1023 usec_t u;
1024 int r;
1025 TimerValue *v;
1026 TimerBase b;
1027
1028 assert(filename);
1029 assert(lvalue);
1030 assert(rvalue);
1031 assert(data);
1032
1033 if ((b = timer_base_from_string(lvalue)) < 0) {
1034 log_error("[%s:%u] Failed to parse timer base: %s", filename, line, lvalue);
1035 return -EINVAL;
1036 }
1037
1038 if ((r = parse_usec(rvalue, &u)) < 0) {
1039 log_error("[%s:%u] Failed to parse timer value: %s", filename, line, rvalue);
1040 return r;
1041 }
1042
1043 if (!(v = new0(TimerValue, 1)))
1044 return -ENOMEM;
1045
1046 v->base = b;
1047 v->value = u;
1048
1049 LIST_PREPEND(TimerValue, value, t->values, v);
1050
1051 return 0;
1052}
1053
1054static int config_parse_timer_unit(
1055 const char *filename,
1056 unsigned line,
1057 const char *section,
1058 const char *lvalue,
1059 const char *rvalue,
1060 void *data,
1061 void *userdata) {
1062
1063 Timer *t = data;
1064 int r;
1065
1066 if (endswith(rvalue, ".timer")) {
1067 log_error("[%s:%u] Unit cannot be of type timer: %s", filename, line, rvalue);
1068 return -EINVAL;
1069 }
1070
1071 if ((r = manager_load_unit(t->meta.manager, rvalue, NULL, &t->unit)) < 0) {
1072 log_error("[%s:%u] Failed to load unit: %s", filename, line, rvalue);
1073 return r;
1074 }
1075
1076 return 0;
1077}
1078
Lennart Poettering01f78472010-05-24 05:25:33 +02001079static int config_parse_path_spec(
1080 const char *filename,
1081 unsigned line,
1082 const char *section,
1083 const char *lvalue,
1084 const char *rvalue,
1085 void *data,
1086 void *userdata) {
1087
1088 Path *p = data;
1089 PathSpec *s;
1090 PathType b;
1091
1092 assert(filename);
1093 assert(lvalue);
1094 assert(rvalue);
1095 assert(data);
1096
1097 if ((b = path_type_from_string(lvalue)) < 0) {
1098 log_error("[%s:%u] Failed to parse path type: %s", filename, line, lvalue);
1099 return -EINVAL;
1100 }
1101
1102 if (!path_is_absolute(rvalue)) {
1103 log_error("[%s:%u] Path is not absolute: %s", filename, line, rvalue);
1104 return -EINVAL;
1105 }
1106
1107 if (!(s = new0(PathSpec, 1)))
1108 return -ENOMEM;
1109
1110 if (!(s->path = strdup(rvalue))) {
1111 free(s);
1112 return -ENOMEM;
1113 }
1114
1115 path_kill_slashes(s->path);
1116
1117 s->type = b;
1118 s->inotify_fd = -1;
1119
1120 LIST_PREPEND(PathSpec, spec, p->specs, s);
1121
1122 return 0;
1123}
1124
1125static int config_parse_path_unit(
1126 const char *filename,
1127 unsigned line,
1128 const char *section,
1129 const char *lvalue,
1130 const char *rvalue,
1131 void *data,
1132 void *userdata) {
1133
1134 Path *t = data;
1135 int r;
1136
1137 if (endswith(rvalue, ".path")) {
1138 log_error("[%s:%u] Unit cannot be of type path: %s", filename, line, rvalue);
1139 return -EINVAL;
1140 }
1141
1142 if ((r = manager_load_unit(t->meta.manager, rvalue, NULL, &t->unit)) < 0) {
1143 log_error("[%s:%u] Failed to load unit: %s", filename, line, rvalue);
1144 return r;
1145 }
1146
1147 return 0;
1148}
1149
Lennart Poetteringddb26e12010-06-18 06:06:24 +02001150static int config_parse_env_file(
1151 const char *filename,
1152 unsigned line,
1153 const char *section,
1154 const char *lvalue,
1155 const char *rvalue,
1156 void *data,
1157 void *userdata) {
1158
1159 FILE *f;
1160 int r;
1161 char ***env = data;
1162
1163 assert(filename);
1164 assert(lvalue);
1165 assert(rvalue);
1166 assert(data);
1167
1168 if (!(f = fopen(rvalue, "re"))) {
1169 log_error("[%s:%u] Failed to open environment file '%s': %m", filename, line, rvalue);
1170 return -errno;
1171 }
1172
1173 while (!feof(f)) {
1174 char l[LINE_MAX], *p;
1175 char **t;
1176
1177 if (!fgets(l, sizeof(l), f)) {
1178 if (feof(f))
1179 break;
1180
1181 r = -errno;
1182 log_error("[%s:%u] Failed to read environment file '%s': %m", filename, line, rvalue);
1183 goto finish;
1184 }
1185
1186 p = strstrip(l);
1187
1188 if (!*p)
1189 continue;
1190
1191 if (strchr(COMMENTS, *p))
1192 continue;
1193
1194 t = strv_env_set(*env, p);
1195 strv_free(*env);
1196 *env = t;
1197 }
1198
1199 r = 0;
1200
1201finish:
1202 if (f)
1203 fclose(f);
1204
1205 return r;
1206}
1207
Lennart Poettering4fd59482010-07-01 00:29:17 +02001208static int config_parse_ip_tos(
1209 const char *filename,
1210 unsigned line,
1211 const char *section,
1212 const char *lvalue,
1213 const char *rvalue,
1214 void *data,
1215 void *userdata) {
1216
1217 int *ip_tos = data, x;
1218 int r;
1219
1220 assert(filename);
1221 assert(lvalue);
1222 assert(rvalue);
1223 assert(data);
1224
1225 if ((x = ip_tos_from_string(rvalue)) < 0)
1226 if ((r = safe_atoi(rvalue, &x)) < 0) {
1227 log_error("[%s:%u] Failed to parse IP TOS value: %s", filename, line, rvalue);
1228 return r;
1229 }
1230
1231 *ip_tos = x;
1232 return 0;
1233}
1234
Lennart Poetteringc952c6e2010-06-18 23:12:48 +02001235DEFINE_CONFIG_PARSE_ENUM(config_parse_notify_access, notify_access, NotifyAccess, "Failed to parse notify access specifier");
1236
Lennart Poettering071830f2010-01-28 02:06:20 +01001237#define FOLLOW_MAX 8
Lennart Poettering87f0e412010-01-26 21:39:06 +01001238
Lennart Poettering9e2f7c12010-04-15 03:11:11 +02001239static int open_follow(char **filename, FILE **_f, Set *names, char **_final) {
Lennart Poettering0301abf2010-01-27 00:15:56 +01001240 unsigned c = 0;
Lennart Poettering87f0e412010-01-26 21:39:06 +01001241 int fd, r;
1242 FILE *f;
Lennart Poettering0301abf2010-01-27 00:15:56 +01001243 char *id = NULL;
Lennart Poettering87f0e412010-01-26 21:39:06 +01001244
1245 assert(filename);
1246 assert(*filename);
1247 assert(_f);
1248 assert(names);
1249
Lennart Poettering0301abf2010-01-27 00:15:56 +01001250 /* This will update the filename pointer if the loaded file is
1251 * reached by a symlink. The old string will be freed. */
Lennart Poettering87f0e412010-01-26 21:39:06 +01001252
Lennart Poettering0301abf2010-01-27 00:15:56 +01001253 for (;;) {
Lennart Poettering2c7108c2010-06-16 01:56:00 +02001254 char *target, *name;
Lennart Poettering87f0e412010-01-26 21:39:06 +01001255
Lennart Poettering0301abf2010-01-27 00:15:56 +01001256 if (c++ >= FOLLOW_MAX)
1257 return -ELOOP;
1258
Lennart Poetteringb08d03f2010-01-29 02:07:41 +01001259 path_kill_slashes(*filename);
1260
Lennart Poettering87f0e412010-01-26 21:39:06 +01001261 /* Add the file name we are currently looking at to
1262 * the names of this unit */
Lennart Poettering0301abf2010-01-27 00:15:56 +01001263 name = file_name_from_path(*filename);
1264 if (!(id = set_get(names, name))) {
Lennart Poettering87f0e412010-01-26 21:39:06 +01001265
Lennart Poettering0301abf2010-01-27 00:15:56 +01001266 if (!(id = strdup(name)))
1267 return -ENOMEM;
1268
1269 if ((r = set_put(names, id)) < 0) {
1270 free(id);
1271 return r;
Lennart Poettering87f0e412010-01-26 21:39:06 +01001272 }
Lennart Poettering87f0e412010-01-26 21:39:06 +01001273 }
1274
Lennart Poettering0301abf2010-01-27 00:15:56 +01001275 /* Try to open the file name, but don't if its a symlink */
1276 if ((fd = open(*filename, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW)) >= 0)
Lennart Poettering87f0e412010-01-26 21:39:06 +01001277 break;
1278
Lennart Poettering0301abf2010-01-27 00:15:56 +01001279 if (errno != ELOOP)
1280 return -errno;
Lennart Poettering87f0e412010-01-26 21:39:06 +01001281
Lennart Poettering0301abf2010-01-27 00:15:56 +01001282 /* Hmm, so this is a symlink. Let's read the name, and follow it manually */
Lennart Poettering2c7108c2010-06-16 01:56:00 +02001283 if ((r = readlink_and_make_absolute(*filename, &target)) < 0)
Lennart Poettering0301abf2010-01-27 00:15:56 +01001284 return r;
1285
Lennart Poettering0301abf2010-01-27 00:15:56 +01001286 free(*filename);
Lennart Poettering2c7108c2010-06-16 01:56:00 +02001287 *filename = target;
Lennart Poettering87f0e412010-01-26 21:39:06 +01001288 }
1289
1290 if (!(f = fdopen(fd, "r"))) {
1291 r = -errno;
Lennart Poettering9e2f7c12010-04-15 03:11:11 +02001292 close_nointr_nofail(fd);
Lennart Poettering0301abf2010-01-27 00:15:56 +01001293 return r;
Lennart Poettering87f0e412010-01-26 21:39:06 +01001294 }
1295
1296 *_f = f;
Lennart Poettering9e2f7c12010-04-15 03:11:11 +02001297 *_final = id;
Lennart Poettering0301abf2010-01-27 00:15:56 +01001298 return 0;
Lennart Poettering87f0e412010-01-26 21:39:06 +01001299}
1300
Lennart Poettering23a177e2010-04-06 02:43:58 +02001301static int merge_by_names(Unit **u, Set *names, const char *id) {
1302 char *k;
1303 int r;
1304
1305 assert(u);
1306 assert(*u);
1307 assert(names);
1308
1309 /* Let's try to add in all symlink names we found */
1310 while ((k = set_steal_first(names))) {
1311
1312 /* First try to merge in the other name into our
1313 * unit */
1314 if ((r = unit_merge_by_name(*u, k)) < 0) {
1315 Unit *other;
1316
1317 /* Hmm, we couldn't merge the other unit into
1318 * ours? Then let's try it the other way
1319 * round */
1320
1321 other = manager_get_unit((*u)->meta.manager, k);
1322 free(k);
1323
1324 if (other)
1325 if ((r = unit_merge(other, *u)) >= 0) {
1326 *u = other;
1327 return merge_by_names(u, names, NULL);
1328 }
1329
1330 return r;
1331 }
1332
1333 if (id == k)
1334 unit_choose_id(*u, id);
1335
1336 free(k);
1337 }
1338
1339 return 0;
1340}
1341
Lennart Poetteringe5373522010-04-10 17:53:17 +02001342static void dump_items(FILE *f, const ConfigItem *items) {
1343 const ConfigItem *i;
1344 const char *prev_section = NULL;
1345 bool not_first = false;
1346
1347 struct {
1348 ConfigParserCallback callback;
1349 const char *rvalue;
1350 } table[] = {
1351 { config_parse_int, "INTEGER" },
1352 { config_parse_unsigned, "UNSIGNED" },
1353 { config_parse_size, "SIZE" },
1354 { config_parse_bool, "BOOLEAN" },
1355 { config_parse_string, "STRING" },
1356 { config_parse_path, "PATH" },
1357 { config_parse_strv, "STRING [...]" },
1358 { config_parse_nice, "NICE" },
1359 { config_parse_oom_adjust, "OOMADJUST" },
1360 { config_parse_io_class, "IOCLASS" },
1361 { config_parse_io_priority, "IOPRIORITY" },
1362 { config_parse_cpu_sched_policy, "CPUSCHEDPOLICY" },
1363 { config_parse_cpu_sched_prio, "CPUSCHEDPRIO" },
1364 { config_parse_cpu_affinity, "CPUAFFINITY" },
1365 { config_parse_mode, "MODE" },
Lennart Poetteringddb26e12010-06-18 06:06:24 +02001366 { config_parse_env_file, "FILE" },
Lennart Poetteringe5373522010-04-10 17:53:17 +02001367 { config_parse_output, "OUTPUT" },
1368 { config_parse_input, "INPUT" },
1369 { config_parse_facility, "FACILITY" },
1370 { config_parse_level, "LEVEL" },
1371 { config_parse_capabilities, "CAPABILITIES" },
1372 { config_parse_secure_bits, "SECUREBITS" },
1373 { config_parse_bounding_set, "BOUNDINGSET" },
1374 { config_parse_timer_slack_ns, "TIMERSLACK" },
1375 { config_parse_limit, "LIMIT" },
1376 { config_parse_cgroup, "CGROUP [...]" },
1377 { config_parse_deps, "UNIT [...]" },
1378 { config_parse_names, "UNIT [...]" },
1379 { config_parse_exec, "PATH [ARGUMENT [...]]" },
1380 { config_parse_service_type, "SERVICETYPE" },
1381 { config_parse_service_restart, "SERVICERESTART" },
1382 { config_parse_sysv_priority, "SYSVPRIORITY" },
1383 { config_parse_kill_mode, "KILLMODE" },
1384 { config_parse_listen, "SOCKET [...]" },
1385 { config_parse_socket_bind, "SOCKETBIND" },
Lennart Poettering93ef5e82010-04-23 20:27:47 +02001386 { config_parse_bindtodevice, "NETWORKINTERFACE" },
1387 { config_parse_usec, "SECONDS" },
1388 { config_parse_path_strv, "PATH [...]" },
Lennart Poettering932921b2010-04-24 03:11:01 +02001389 { config_parse_mount_flags, "MOUNTFLAG [...]" },
Lennart Poetteringf2d37692010-06-18 23:25:19 +02001390 { config_parse_string_printf, "STRING" },
Lennart Poettering871d7de2010-05-24 01:45:54 +02001391 { config_parse_timer, "TIMER" },
1392 { config_parse_timer_unit, "NAME" },
Lennart Poetteringc952c6e2010-06-18 23:12:48 +02001393 { config_parse_path_spec, "PATH" },
1394 { config_parse_path_unit, "UNIT" },
Lennart Poettering8b03dae2010-07-01 16:34:26 +02001395 { config_parse_notify_access, "ACCESS" },
1396 { config_parse_ip_tos, "TOS" },
Lennart Poetteringe5373522010-04-10 17:53:17 +02001397 };
1398
1399 assert(f);
1400 assert(items);
1401
1402 for (i = items; i->lvalue; i++) {
1403 unsigned j;
1404 const char *rvalue = "OTHER";
1405
1406 if (!streq_ptr(i->section, prev_section)) {
1407 if (!not_first)
1408 not_first = true;
1409 else
1410 fputc('\n', f);
1411
1412 fprintf(f, "[%s]\n", i->section);
1413 prev_section = i->section;
1414 }
1415
1416 for (j = 0; j < ELEMENTSOF(table); j++)
1417 if (i->parse == table[j].callback) {
1418 rvalue = table[j].rvalue;
1419 break;
1420 }
1421
1422 fprintf(f, "%s=%s\n", i->lvalue, rvalue);
1423 }
1424}
1425
1426static int load_from_path(Unit *u, const char *path) {
Lennart Poettering87f0e412010-01-26 21:39:06 +01001427
1428 static const char* const section_table[_UNIT_TYPE_MAX] = {
1429 [UNIT_SERVICE] = "Service",
1430 [UNIT_TIMER] = "Timer",
1431 [UNIT_SOCKET] = "Socket",
1432 [UNIT_TARGET] = "Target",
1433 [UNIT_DEVICE] = "Device",
1434 [UNIT_MOUNT] = "Mount",
1435 [UNIT_AUTOMOUNT] = "Automount",
Maarten Lankhorst07b0b132010-05-09 18:44:11 +02001436 [UNIT_SNAPSHOT] = "Snapshot",
Lennart Poettering01f78472010-05-24 05:25:33 +02001437 [UNIT_SWAP] = "Swap",
1438 [UNIT_PATH] = "Path"
Lennart Poettering42f4e3c2010-01-19 02:56:37 +01001439 };
1440
Lennart Poettering034c6ed2010-01-26 04:18:44 +01001441#define EXEC_CONTEXT_CONFIG_ITEMS(context, section) \
Lennart Poettering9eba9da2010-01-29 20:46:22 +01001442 { "WorkingDirectory", config_parse_path, &(context).working_directory, section }, \
1443 { "RootDirectory", config_parse_path, &(context).root_directory, section }, \
Lennart Poetteringf2d37692010-06-18 23:25:19 +02001444 { "User", config_parse_string_printf, &(context).user, section }, \
1445 { "Group", config_parse_string_printf, &(context).group, section }, \
Lennart Poettering1c01f822010-01-27 02:16:11 +01001446 { "SupplementaryGroups", config_parse_strv, &(context).supplementary_groups, section }, \
Lennart Poetteringfb33a392010-01-28 02:53:56 +01001447 { "Nice", config_parse_nice, &(context), section }, \
1448 { "OOMAdjust", config_parse_oom_adjust, &(context), section }, \
Lennart Poettering9eba9da2010-01-29 20:46:22 +01001449 { "IOSchedulingClass", config_parse_io_class, &(context), section }, \
Lennart Poettering94f04342010-01-30 01:55:42 +01001450 { "IOSchedulingPriority", config_parse_io_priority, &(context), section }, \
1451 { "CPUSchedulingPolicy", config_parse_cpu_sched_policy,&(context), section }, \
1452 { "CPUSchedulingPriority", config_parse_cpu_sched_prio, &(context), section }, \
Lennart Poettering38b48752010-02-02 12:50:04 +01001453 { "CPUSchedulingResetOnFork", config_parse_bool, &(context).cpu_sched_reset_on_fork, section }, \
Lennart Poettering94f04342010-01-30 01:55:42 +01001454 { "CPUAffinity", config_parse_cpu_affinity, &(context), section }, \
Lennart Poetteringb5a06992010-02-12 02:02:14 +01001455 { "UMask", config_parse_mode, &(context).umask, section }, \
Lennart Poettering071830f2010-01-28 02:06:20 +01001456 { "Environment", config_parse_strv, &(context).environment, section }, \
Lennart Poetteringddb26e12010-06-18 06:06:24 +02001457 { "EnvironmentFile", config_parse_env_file, &(context).environment, section }, \
Lennart Poettering80876c22010-04-13 02:06:27 +02001458 { "StandardInput", config_parse_input, &(context).std_input, section }, \
1459 { "StandardOutput", config_parse_output, &(context).std_output, section }, \
Lennart Poetteringa4ddf822010-06-03 03:37:12 +02001460 { "StandardError", config_parse_output, &(context).std_error, section }, \
Lennart Poettering80876c22010-04-13 02:06:27 +02001461 { "TTYPath", config_parse_path, &(context).tty_path, section }, \
Lennart Poetteringf2d37692010-06-18 23:25:19 +02001462 { "SyslogIdentifier", config_parse_string_printf, &(context).syslog_identifier, section }, \
Lennart Poettering071830f2010-01-28 02:06:20 +01001463 { "SyslogFacility", config_parse_facility, &(context).syslog_priority, section }, \
Lennart Poettering94f04342010-01-30 01:55:42 +01001464 { "SyslogLevel", config_parse_level, &(context).syslog_priority, section }, \
Lennart Poettering4f4a1db2010-05-16 01:46:35 +02001465 { "SyslogNoPrefix", config_parse_bool, &(context).syslog_no_prefix, section }, \
Lennart Poettering94f04342010-01-30 01:55:42 +01001466 { "Capabilities", config_parse_capabilities, &(context), section }, \
1467 { "SecureBits", config_parse_secure_bits, &(context), section }, \
1468 { "CapabilityBoundingSetDrop", config_parse_bounding_set, &(context), section }, \
1469 { "TimerSlackNS", config_parse_timer_slack_ns, &(context), section }, \
1470 { "LimitCPU", config_parse_limit, &(context).rlimit[RLIMIT_CPU], section }, \
1471 { "LimitFSIZE", config_parse_limit, &(context).rlimit[RLIMIT_FSIZE], section }, \
1472 { "LimitDATA", config_parse_limit, &(context).rlimit[RLIMIT_DATA], section }, \
1473 { "LimitSTACK", config_parse_limit, &(context).rlimit[RLIMIT_STACK], section }, \
1474 { "LimitCORE", config_parse_limit, &(context).rlimit[RLIMIT_CORE], section }, \
1475 { "LimitRSS", config_parse_limit, &(context).rlimit[RLIMIT_RSS], section }, \
1476 { "LimitNOFILE", config_parse_limit, &(context).rlimit[RLIMIT_NOFILE], section }, \
1477 { "LimitAS", config_parse_limit, &(context).rlimit[RLIMIT_AS], section }, \
1478 { "LimitNPROC", config_parse_limit, &(context).rlimit[RLIMIT_NPROC], section }, \
1479 { "LimitMEMLOCK", config_parse_limit, &(context).rlimit[RLIMIT_MEMLOCK], section }, \
1480 { "LimitLOCKS", config_parse_limit, &(context).rlimit[RLIMIT_LOCKS], section }, \
1481 { "LimitSIGPENDING", config_parse_limit, &(context).rlimit[RLIMIT_SIGPENDING], section }, \
1482 { "LimitMSGQUEUE", config_parse_limit, &(context).rlimit[RLIMIT_MSGQUEUE], section }, \
1483 { "LimitNICE", config_parse_limit, &(context).rlimit[RLIMIT_NICE], section }, \
1484 { "LimitRTPRIO", config_parse_limit, &(context).rlimit[RLIMIT_RTPRIO], section }, \
Lennart Poettering451a0742010-02-12 02:00:18 +01001485 { "LimitRTTIME", config_parse_limit, &(context).rlimit[RLIMIT_RTTIME], section }, \
Lennart Poettering15ae4222010-04-21 22:15:06 +02001486 { "ControlGroup", config_parse_cgroup, u, section }, \
1487 { "ReadWriteDirectories", config_parse_path_strv, &(context).read_write_dirs, section }, \
1488 { "ReadOnlyDirectories", config_parse_path_strv, &(context).read_only_dirs, section }, \
1489 { "InaccessibleDirectories",config_parse_path_strv, &(context).inaccessible_dirs, section }, \
1490 { "PrivateTmp", config_parse_bool, &(context).private_tmp, section }, \
Lennart Poetteringdf1f0af2010-06-16 16:25:42 +02001491 { "MountFlags", config_parse_mount_flags, &(context), section }, \
Lennart Poetteringf2d37692010-06-18 23:25:19 +02001492 { "TCPWrapName", config_parse_string_printf, &(context).tcpwrap_name, section }, \
1493 { "PAMName", config_parse_string_printf, &(context).pam_name, section }
Lennart Poettering034c6ed2010-01-26 04:18:44 +01001494
Lennart Poettering3efd4192009-11-19 23:13:20 +01001495 const ConfigItem items[] = {
Lennart Poettering09477262010-04-15 23:20:17 +02001496 { "Names", config_parse_names, u, "Unit" },
Lennart Poetteringf2d37692010-06-18 23:25:19 +02001497 { "Description", config_parse_string_printf, &u->meta.description, "Unit" },
Lennart Poettering09477262010-04-15 23:20:17 +02001498 { "Requires", config_parse_deps, UINT_TO_PTR(UNIT_REQUIRES), "Unit" },
1499 { "RequiresOverridable", config_parse_deps, UINT_TO_PTR(UNIT_REQUIRES_OVERRIDABLE), "Unit" },
1500 { "Requisite", config_parse_deps, UINT_TO_PTR(UNIT_REQUISITE), "Unit" },
1501 { "RequisiteOverridable", config_parse_deps, UINT_TO_PTR(UNIT_REQUISITE_OVERRIDABLE), "Unit" },
1502 { "Wants", config_parse_deps, UINT_TO_PTR(UNIT_WANTS), "Unit" },
1503 { "Conflicts", config_parse_deps, UINT_TO_PTR(UNIT_CONFLICTS), "Unit" },
1504 { "Before", config_parse_deps, UINT_TO_PTR(UNIT_BEFORE), "Unit" },
1505 { "After", config_parse_deps, UINT_TO_PTR(UNIT_AFTER), "Unit" },
1506 { "RecursiveStop", config_parse_bool, &u->meta.recursive_stop, "Unit" },
1507 { "StopWhenUnneeded", config_parse_bool, &u->meta.stop_when_unneeded, "Unit" },
Lennart Poetteringbc0f8772010-05-21 23:41:54 +02001508 { "OnlyByDependency", config_parse_bool, &u->meta.only_by_dependency, "Unit" },
Lennart Poettering034c6ed2010-01-26 04:18:44 +01001509
Lennart Poettering1c01f822010-01-27 02:16:11 +01001510 { "PIDFile", config_parse_path, &u->service.pid_file, "Service" },
1511 { "ExecStartPre", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START_PRE, "Service" },
1512 { "ExecStart", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START, "Service" },
1513 { "ExecStartPost", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START_POST, "Service" },
1514 { "ExecReload", config_parse_exec, u->service.exec_command+SERVICE_EXEC_RELOAD, "Service" },
1515 { "ExecStop", config_parse_exec, u->service.exec_command+SERVICE_EXEC_STOP, "Service" },
1516 { "ExecStopPost", config_parse_exec, u->service.exec_command+SERVICE_EXEC_STOP_POST, "Service" },
1517 { "RestartSec", config_parse_usec, &u->service.restart_usec, "Service" },
1518 { "TimeoutSec", config_parse_usec, &u->service.timeout_usec, "Service" },
Lennart Poettering0d87eb42010-04-13 02:22:41 +02001519 { "Type", config_parse_service_type, &u->service.type, "Service" },
1520 { "Restart", config_parse_service_restart, &u->service.restart, "Service" },
Lennart Poettering81a2b7c2010-02-14 22:43:08 +01001521 { "PermissionsStartOnly", config_parse_bool, &u->service.permissions_start_only, "Service" },
1522 { "RootDirectoryStartOnly", config_parse_bool, &u->service.root_directory_start_only, "Service" },
Lennart Poettering8e274522010-03-31 16:29:55 +02001523 { "ValidNoProcess", config_parse_bool, &u->service.valid_no_process, "Service" },
Lennart Poetteringa9a1e002010-04-06 02:40:10 +02001524 { "SysVStartPriority", config_parse_sysv_priority, &u->service.sysv_start_priority, "Service" },
Lennart Poettering50159e62010-04-08 00:52:14 +02001525 { "KillMode", config_parse_kill_mode, &u->service.kill_mode, "Service" },
Lennart Poetteringb778d552010-04-10 17:48:41 +02001526 { "NonBlocking", config_parse_bool, &u->service.exec_context.non_blocking, "Service" },
Lennart Poetteringf2d37692010-06-18 23:25:19 +02001527 { "BusName", config_parse_string_printf, &u->service.bus_name, "Service" },
Lennart Poetteringc952c6e2010-06-18 23:12:48 +02001528 { "NotifyAccess", config_parse_notify_access, &u->service.notify_access, "Service" },
Lennart Poettering87f0e412010-01-26 21:39:06 +01001529 EXEC_CONTEXT_CONFIG_ITEMS(u->service.exec_context, "Service"),
Lennart Poettering034c6ed2010-01-26 04:18:44 +01001530
Lennart Poettering1c01f822010-01-27 02:16:11 +01001531 { "ListenStream", config_parse_listen, &u->socket, "Socket" },
1532 { "ListenDatagram", config_parse_listen, &u->socket, "Socket" },
1533 { "ListenSequentialPacket", config_parse_listen, &u->socket, "Socket" },
1534 { "ListenFIFO", config_parse_listen, &u->socket, "Socket" },
1535 { "BindIPv6Only", config_parse_socket_bind, &u->socket, "Socket" },
1536 { "Backlog", config_parse_unsigned, &u->socket.backlog, "Socket" },
Lennart Poetteringacbb0222010-01-27 04:31:52 +01001537 { "BindToDevice", config_parse_bindtodevice, &u->socket, "Socket" },
Lennart Poettering1c01f822010-01-27 02:16:11 +01001538 { "ExecStartPre", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_START_PRE, "Socket" },
1539 { "ExecStartPost", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_START_POST, "Socket" },
1540 { "ExecStopPre", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_STOP_PRE, "Socket" },
1541 { "ExecStopPost", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_STOP_POST, "Socket" },
Lennart Poettering108736d2010-04-10 17:50:54 +02001542 { "TimeoutSec", config_parse_usec, &u->socket.timeout_usec, "Socket" },
Lennart Poetteringb5a06992010-02-12 02:02:14 +01001543 { "DirectoryMode", config_parse_mode, &u->socket.directory_mode, "Socket" },
1544 { "SocketMode", config_parse_mode, &u->socket.socket_mode, "Socket" },
Lennart Poettering50159e62010-04-08 00:52:14 +02001545 { "KillMode", config_parse_kill_mode, &u->socket.kill_mode, "Socket" },
Lennart Poettering4f2d5282010-04-15 06:19:54 +02001546 { "Accept", config_parse_bool, &u->socket.accept, "Socket" },
Lennart Poettering6cf6bbc2010-06-19 04:25:28 +02001547 { "MaxConnections", config_parse_unsigned, &u->socket.max_connections, "Socket" },
Lennart Poettering4fd59482010-07-01 00:29:17 +02001548 { "KeepAlive", config_parse_bool, &u->socket.keep_alive, "Socket" },
1549 { "Priority", config_parse_int, &u->socket.priority, "Socket" },
1550 { "ReceiveBuffer", config_parse_size, &u->socket.receive_buffer, "Socket" },
1551 { "SendBuffer", config_parse_size, &u->socket.send_buffer, "Socket" },
1552 { "IPTOS", config_parse_ip_tos, &u->socket.ip_tos, "Socket" },
1553 { "IPTTL", config_parse_int, &u->socket.ip_ttl, "Socket" },
1554 { "Mark", config_parse_int, &u->socket.mark, "Socket" },
1555 { "PipeSize", config_parse_size, &u->socket.pipe_size, "Socket" },
1556 { "FreeBind", config_parse_bool, &u->socket.free_bind, "Socket" },
Lennart Poettering87f0e412010-01-26 21:39:06 +01001557 EXEC_CONTEXT_CONFIG_ITEMS(u->socket.exec_context, "Socket"),
Lennart Poettering034c6ed2010-01-26 04:18:44 +01001558
Lennart Poetteringe5373522010-04-10 17:53:17 +02001559 { "What", config_parse_string, &u->mount.parameters_fragment.what, "Mount" },
1560 { "Where", config_parse_path, &u->mount.where, "Mount" },
1561 { "Options", config_parse_string, &u->mount.parameters_fragment.options, "Mount" },
1562 { "Type", config_parse_string, &u->mount.parameters_fragment.fstype, "Mount" },
1563 { "TimeoutSec", config_parse_usec, &u->mount.timeout_usec, "Mount" },
1564 { "KillMode", config_parse_kill_mode, &u->mount.kill_mode, "Mount" },
Lennart Poettering3e5235b2010-07-02 00:28:44 +02001565 { "DirectoryMode", config_parse_mode, &u->mount.directory_mode, "Mount" },
Lennart Poetteringe5373522010-04-10 17:53:17 +02001566 EXEC_CONTEXT_CONFIG_ITEMS(u->mount.exec_context, "Mount"),
Lennart Poettering034c6ed2010-01-26 04:18:44 +01001567
Lennart Poettering8d567582010-04-16 23:24:39 +02001568 { "Where", config_parse_path, &u->automount.where, "Automount" },
Lennart Poettering1cf18f22010-07-02 01:17:21 +02001569 { "DirectoryMode", config_parse_mode, &u->automount.directory_mode, "Automount" },
Lennart Poettering8d567582010-04-16 23:24:39 +02001570
Lennart Poettering01f78472010-05-24 05:25:33 +02001571 { "What", config_parse_path, &u->swap.parameters_fragment.what, "Swap" },
1572 { "Priority", config_parse_int, &u->swap.parameters_fragment.priority, "Swap" },
Maarten Lankhorst07b0b132010-05-09 18:44:11 +02001573
Lennart Poettering01f78472010-05-24 05:25:33 +02001574 { "OnActive", config_parse_timer, &u->timer, "Timer" },
1575 { "OnBoot", config_parse_timer, &u->timer, "Timer" },
1576 { "OnStartup", config_parse_timer, &u->timer, "Timer" },
1577 { "OnUnitActive", config_parse_timer, &u->timer, "Timer" },
1578 { "OnUnitInactive", config_parse_timer, &u->timer, "Timer" },
1579 { "Unit", config_parse_timer_unit, &u->timer, "Timer" },
1580
1581 { "PathExists", config_parse_path_spec, &u->path, "Path" },
1582 { "PathChanged", config_parse_path_spec, &u->path, "Path" },
1583 { "DirectoryNotEmpty", config_parse_path_spec, &u->path, "Path" },
1584 { "Unit", config_parse_path_unit, &u->path, "Path" },
Lennart Poettering871d7de2010-05-24 01:45:54 +02001585
Lennart Poettering10e87ee2010-06-16 01:58:50 +02001586 /* The [Install] section is ignored here. */
1587 { "Alias", NULL, NULL, "Install" },
1588 { "WantedBy", NULL, NULL, "Install" },
1589 { "Also", NULL, NULL, "Install" },
1590
Lennart Poettering3efd4192009-11-19 23:13:20 +01001591 { NULL, NULL, NULL, NULL }
1592 };
1593
Lennart Poettering034c6ed2010-01-26 04:18:44 +01001594#undef EXEC_CONTEXT_CONFIG_ITEMS
Lennart Poettering42f4e3c2010-01-19 02:56:37 +01001595
Lennart Poettering10e87ee2010-06-16 01:58:50 +02001596 const char *sections[4];
Lennart Poettering0301abf2010-01-27 00:15:56 +01001597 int r;
Lennart Poettering87f0e412010-01-26 21:39:06 +01001598 Set *symlink_names;
Lennart Poettering23a177e2010-04-06 02:43:58 +02001599 FILE *f = NULL;
1600 char *filename = NULL, *id = NULL;
1601 Unit *merged;
1602
Lennart Poetteringe5373522010-04-10 17:53:17 +02001603 if (!u) {
1604 /* Dirty dirty hack. */
1605 dump_items((FILE*) path, items);
1606 return 0;
1607 }
1608
Lennart Poettering23a177e2010-04-06 02:43:58 +02001609 assert(u);
Lennart Poetteringe5373522010-04-10 17:53:17 +02001610 assert(path);
Lennart Poettering3efd4192009-11-19 23:13:20 +01001611
Lennart Poettering09477262010-04-15 23:20:17 +02001612 sections[0] = "Unit";
Lennart Poettering87f0e412010-01-26 21:39:06 +01001613 sections[1] = section_table[u->meta.type];
Lennart Poettering10e87ee2010-06-16 01:58:50 +02001614 sections[2] = "Install";
1615 sections[3] = NULL;
Lennart Poettering42f4e3c2010-01-19 02:56:37 +01001616
Lennart Poettering87f0e412010-01-26 21:39:06 +01001617 if (!(symlink_names = set_new(string_hash_func, string_compare_func)))
1618 return -ENOMEM;
Lennart Poettering3efd4192009-11-19 23:13:20 +01001619
Lennart Poettering036643a2010-02-13 01:07:02 +01001620 if (path_is_absolute(path)) {
1621
1622 if (!(filename = strdup(path))) {
1623 r = -ENOMEM;
1624 goto finish;
1625 }
1626
1627 if ((r = open_follow(&filename, &f, symlink_names, &id)) < 0) {
1628 free(filename);
1629 filename = NULL;
1630
1631 if (r != -ENOENT)
1632 goto finish;
1633 }
1634
1635 } else {
1636 char **p;
1637
Lennart Poettering84e35432010-06-15 14:45:15 +02001638 STRV_FOREACH(p, u->meta.manager->lookup_paths.unit_path) {
Lennart Poettering036643a2010-02-13 01:07:02 +01001639
1640 /* Instead of opening the path right away, we manually
1641 * follow all symlinks and add their name to our unit
1642 * name set while doing so */
1643 if (!(filename = path_make_absolute(path, *p))) {
1644 r = -ENOMEM;
1645 goto finish;
1646 }
1647
1648 if ((r = open_follow(&filename, &f, symlink_names, &id)) < 0) {
1649 char *sn;
1650
1651 free(filename);
1652 filename = NULL;
1653
1654 if (r != -ENOENT)
1655 goto finish;
1656
1657 /* Empty the symlink names for the next run */
1658 while ((sn = set_steal_first(symlink_names)))
1659 free(sn);
1660
1661 continue;
1662 }
1663
1664 break;
1665 }
Lennart Poettering034c6ed2010-01-26 04:18:44 +01001666 }
1667
Lennart Poettering036643a2010-02-13 01:07:02 +01001668 if (!filename) {
Lennart Poettering23a177e2010-04-06 02:43:58 +02001669 r = 0;
1670 goto finish;
1671 }
1672
1673 merged = u;
1674 if ((r = merge_by_names(&merged, symlink_names, id)) < 0)
1675 goto finish;
1676
1677 if (merged != u) {
Lennart Poetteringe5373522010-04-10 17:53:17 +02001678 u->meta.load_state = UNIT_MERGED;
Lennart Poettering23a177e2010-04-06 02:43:58 +02001679 r = 0;
Lennart Poettering0301abf2010-01-27 00:15:56 +01001680 goto finish;
1681 }
1682
1683 /* Now, parse the file contents */
Lennart Poettering10e87ee2010-06-16 01:58:50 +02001684 if ((r = config_parse(filename, f, sections, items, false, u)) < 0)
Lennart Poettering0301abf2010-01-27 00:15:56 +01001685 goto finish;
1686
Lennart Poettering6be1e7d2010-02-14 01:01:10 +01001687 free(u->meta.fragment_path);
1688 u->meta.fragment_path = filename;
Lennart Poettering0301abf2010-01-27 00:15:56 +01001689 filename = NULL;
1690
Lennart Poetteringe5373522010-04-10 17:53:17 +02001691 u->meta.load_state = UNIT_LOADED;
Lennart Poettering23a177e2010-04-06 02:43:58 +02001692 r = 0;
Lennart Poettering87f0e412010-01-26 21:39:06 +01001693
1694finish:
Lennart Poettering53ec43c2010-06-15 02:45:26 +02001695 set_free_free(symlink_names);
Lennart Poettering0301abf2010-01-27 00:15:56 +01001696 free(filename);
1697
Lennart Poettering23a177e2010-04-06 02:43:58 +02001698 if (f)
1699 fclose(f);
1700
Lennart Poettering0301abf2010-01-27 00:15:56 +01001701 return r;
1702}
1703
Lennart Poetteringe5373522010-04-10 17:53:17 +02001704int unit_load_fragment(Unit *u) {
Lennart Poettering23a177e2010-04-06 02:43:58 +02001705 int r;
Lennart Poettering0301abf2010-01-27 00:15:56 +01001706
1707 assert(u);
Lennart Poettering0301abf2010-01-27 00:15:56 +01001708
Lennart Poettering23a177e2010-04-06 02:43:58 +02001709 if (u->meta.fragment_path) {
1710
Lennart Poetteringe5373522010-04-10 17:53:17 +02001711 if ((r = load_from_path(u, u->meta.fragment_path)) < 0)
Lennart Poettering23a177e2010-04-06 02:43:58 +02001712 return r;
1713
1714 } else {
Lennart Poettering0301abf2010-01-27 00:15:56 +01001715 Iterator i;
Lennart Poettering890f4342010-02-14 01:08:20 +01001716 const char *t;
Lennart Poettering0301abf2010-01-27 00:15:56 +01001717
Lennart Poettering890f4342010-02-14 01:08:20 +01001718 /* Try to find the unit under its id */
Lennart Poettering9e2f7c12010-04-15 03:11:11 +02001719 if ((r = load_from_path(u, u->meta.id)) < 0)
1720 return r;
Lennart Poettering890f4342010-02-14 01:08:20 +01001721
1722 /* Try to find an alias we can load this with */
Lennart Poetteringe5373522010-04-10 17:53:17 +02001723 if (u->meta.load_state == UNIT_STUB)
Lennart Poettering23a177e2010-04-06 02:43:58 +02001724 SET_FOREACH(t, u->meta.names, i) {
1725
Lennart Poettering9e2f7c12010-04-15 03:11:11 +02001726 if (t == u->meta.id)
Lennart Poettering23a177e2010-04-06 02:43:58 +02001727 continue;
1728
Lennart Poetteringe5373522010-04-10 17:53:17 +02001729 if ((r = load_from_path(u, t)) < 0)
Lennart Poettering23a177e2010-04-06 02:43:58 +02001730 return r;
1731
Lennart Poetteringe5373522010-04-10 17:53:17 +02001732 if (u->meta.load_state != UNIT_STUB)
Lennart Poettering890f4342010-02-14 01:08:20 +01001733 break;
Lennart Poettering23a177e2010-04-06 02:43:58 +02001734 }
Lennart Poettering9e2f7c12010-04-15 03:11:11 +02001735
1736 /* Now, follow the same logic, but look for a template */
1737 if (u->meta.load_state == UNIT_STUB && u->meta.instance) {
1738 char *k;
1739
1740 if (!(k = unit_name_template(u->meta.id)))
1741 return -ENOMEM;
1742
1743 r = load_from_path(u, k);
1744 free(k);
1745
1746 if (r < 0)
1747 return r;
1748
1749 if (u->meta.load_state == UNIT_STUB)
1750 SET_FOREACH(t, u->meta.names, i) {
1751
1752 if (t == u->meta.id)
1753 continue;
1754
1755 if (!(k = unit_name_template(t)))
1756 return -ENOMEM;
1757
1758 r = load_from_path(u, k);
1759 free(k);
1760
1761 if (r < 0)
1762 return r;
1763
1764 if (u->meta.load_state != UNIT_STUB)
1765 break;
1766 }
1767 }
Lennart Poettering0301abf2010-01-27 00:15:56 +01001768 }
Lennart Poettering87f0e412010-01-26 21:39:06 +01001769
Lennart Poettering23a177e2010-04-06 02:43:58 +02001770 return 0;
Lennart Poettering3efd4192009-11-19 23:13:20 +01001771}
Lennart Poetteringe5373522010-04-10 17:53:17 +02001772
1773void unit_dump_config_items(FILE *f) {
1774 /* OK, this wins a prize for extreme ugliness. */
1775
1776 load_from_path(NULL, (const void*) f);
1777}