blob: 47cfcd676c45a391fce4b1c678bebe44b2541ffc [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 Poettering3efd4192009-11-19 23:13:20 +010030
Lennart Poettering87f0e412010-01-26 21:39:06 +010031#include "unit.h"
Lennart Poettering3efd4192009-11-19 23:13:20 +010032#include "strv.h"
33#include "conf-parser.h"
34#include "load-fragment.h"
Lennart Poettering16354ef2010-01-20 19:19:53 +010035#include "log.h"
Lennart Poettering9eba9da2010-01-29 20:46:22 +010036#include "ioprio.h"
Lennart Poettering94f04342010-01-30 01:55:42 +010037#include "securebits.h"
38#include "missing.h"
Lennart Poettering3efd4192009-11-19 23:13:20 +010039
Lennart Poettering0d87eb42010-04-13 02:22:41 +020040#define DEFINE_CONFIG_PARSE_ENUM(function,name,type,msg) \
41 static int function( \
42 const char *filename, \
43 unsigned line, \
44 const char *section, \
45 const char *lvalue, \
46 const char *rvalue, \
47 void *data, \
48 void *userdata) { \
49 \
50 type *i = data, x; \
51 \
52 assert(filename); \
53 assert(lvalue); \
54 assert(rvalue); \
55 assert(data); \
56 \
57 if ((x = name##_from_string(rvalue)) < 0) { \
58 log_error("[%s:%u] " msg ": %s", filename, line, rvalue); \
59 return -EBADMSG; \
60 } \
61 \
62 *i = x; \
63 \
64 return 0; \
65 }
66
Lennart Poettering42f4e3c2010-01-19 02:56:37 +010067static int config_parse_deps(
Lennart Poettering3efd4192009-11-19 23:13:20 +010068 const char *filename,
69 unsigned line,
70 const char *section,
71 const char *lvalue,
72 const char *rvalue,
73 void *data,
74 void *userdata) {
75
Lennart Poettering87f0e412010-01-26 21:39:06 +010076 UnitDependency d = PTR_TO_UINT(data);
77 Unit *u = userdata;
Lennart Poettering3efd4192009-11-19 23:13:20 +010078 char *w;
79 size_t l;
80 char *state;
81
82 assert(filename);
83 assert(lvalue);
84 assert(rvalue);
Lennart Poettering3efd4192009-11-19 23:13:20 +010085
Lennart Poetteringe5373522010-04-10 17:53:17 +020086 if (UNIT_VTABLE(u)->refuse_requires &&
87 (d == UNIT_REQUIRES ||
88 d == UNIT_SOFT_REQUIRES ||
89 d == UNIT_REQUISITE ||
90 d == UNIT_SOFT_REQUISITE)) {
91 log_error("[%s:%u] Dependency of type %s not acceptable for this unit type.", filename, line, lvalue);
92 return -EBADMSG;
93 }
94
Lennart Poetteringf62c0e42010-02-14 01:07:36 +010095 FOREACH_WORD(w, l, rvalue, state) {
Lennart Poettering3efd4192009-11-19 23:13:20 +010096 char *t;
97 int r;
Lennart Poettering3efd4192009-11-19 23:13:20 +010098
99 if (!(t = strndup(w, l)))
100 return -ENOMEM;
101
Lennart Poetteringb19e7dc2010-01-28 06:43:49 +0100102 r = unit_add_dependency_by_name(u, d, t);
Lennart Poettering3efd4192009-11-19 23:13:20 +0100103 free(t);
104
105 if (r < 0)
106 return r;
Lennart Poettering3efd4192009-11-19 23:13:20 +0100107 }
108
109 return 0;
110}
111
Lennart Poettering42f4e3c2010-01-19 02:56:37 +0100112static int config_parse_names(
Lennart Poettering87d15152010-01-18 23:50:13 +0100113 const char *filename,
114 unsigned line,
115 const char *section,
116 const char *lvalue,
117 const char *rvalue,
118 void *data,
119 void *userdata) {
120
Lennart Poettering87f0e412010-01-26 21:39:06 +0100121 Unit *u = userdata;
Lennart Poettering87d15152010-01-18 23:50:13 +0100122 char *w;
123 size_t l;
124 char *state;
125
126 assert(filename);
127 assert(lvalue);
128 assert(rvalue);
129 assert(data);
130
Lennart Poetteringf62c0e42010-02-14 01:07:36 +0100131 FOREACH_WORD(w, l, rvalue, state) {
Lennart Poettering87d15152010-01-18 23:50:13 +0100132 char *t;
133 int r;
Lennart Poettering87d15152010-01-18 23:50:13 +0100134
135 if (!(t = strndup(w, l)))
136 return -ENOMEM;
137
Lennart Poettering23a177e2010-04-06 02:43:58 +0200138 r = unit_merge_by_name(u, t);
Lennart Poettering87d15152010-01-18 23:50:13 +0100139 free(t);
Lennart Poettering23a177e2010-04-06 02:43:58 +0200140
141 if (r < 0)
142 return r;
Lennart Poettering87d15152010-01-18 23:50:13 +0100143 }
144
145 return 0;
146}
147
Lennart Poettering42f4e3c2010-01-19 02:56:37 +0100148static int config_parse_listen(
149 const char *filename,
150 unsigned line,
151 const char *section,
152 const char *lvalue,
153 const char *rvalue,
154 void *data,
155 void *userdata) {
156
Lennart Poettering16354ef2010-01-20 19:19:53 +0100157 int r;
Lennart Poettering542563b2010-01-23 03:35:54 +0100158 SocketPort *p;
159 Socket *s;
Lennart Poettering16354ef2010-01-20 19:19:53 +0100160
Lennart Poettering42f4e3c2010-01-19 02:56:37 +0100161 assert(filename);
162 assert(lvalue);
163 assert(rvalue);
164 assert(data);
165
Lennart Poettering542563b2010-01-23 03:35:54 +0100166 s = (Socket*) data;
167
168 if (!(p = new0(SocketPort, 1)))
169 return -ENOMEM;
170
171 if (streq(lvalue, "ListenFIFO")) {
172 p->type = SOCKET_FIFO;
173
174 if (!(p->path = strdup(rvalue))) {
175 free(p);
176 return -ENOMEM;
177 }
178 } else {
179 p->type = SOCKET_SOCKET;
180
181 if ((r = socket_address_parse(&p->address, rvalue)) < 0) {
182 log_error("[%s:%u] Failed to parse address value: %s", filename, line, rvalue);
183 free(p);
184 return r;
185 }
186
187 if (streq(lvalue, "ListenStream"))
188 p->address.type = SOCK_STREAM;
189 else if (streq(lvalue, "ListenDatagram"))
190 p->address.type = SOCK_DGRAM;
191 else {
192 assert(streq(lvalue, "ListenSequentialPacket"));
193 p->address.type = SOCK_SEQPACKET;
194 }
195
196 if (socket_address_family(&p->address) != AF_LOCAL && p->address.type == SOCK_SEQPACKET) {
197 free(p);
198 return -EPROTONOSUPPORT;
199 }
Lennart Poettering16354ef2010-01-20 19:19:53 +0100200 }
201
Lennart Poettering542563b2010-01-23 03:35:54 +0100202 p->fd = -1;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100203 LIST_PREPEND(SocketPort, port, s->ports, p);
Lennart Poettering542563b2010-01-23 03:35:54 +0100204
Lennart Poettering16354ef2010-01-20 19:19:53 +0100205 return 0;
Lennart Poettering42f4e3c2010-01-19 02:56:37 +0100206}
207
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100208static int config_parse_socket_bind(
Lennart Poettering42f4e3c2010-01-19 02:56:37 +0100209 const char *filename,
210 unsigned line,
211 const char *section,
212 const char *lvalue,
213 const char *rvalue,
214 void *data,
215 void *userdata) {
216
Lennart Poettering542563b2010-01-23 03:35:54 +0100217 int r;
218 Socket *s;
Lennart Poettering42f4e3c2010-01-19 02:56:37 +0100219
220 assert(filename);
221 assert(lvalue);
222 assert(rvalue);
223 assert(data);
224
Lennart Poettering542563b2010-01-23 03:35:54 +0100225 s = (Socket*) data;
226
227 if ((r = parse_boolean(rvalue)) < 0) {
228 log_error("[%s:%u] Failed to parse bind IPv6 only value: %s", filename, line, rvalue);
229 return r;
Lennart Poettering16354ef2010-01-20 19:19:53 +0100230 }
Lennart Poettering42f4e3c2010-01-19 02:56:37 +0100231
Lennart Poettering542563b2010-01-23 03:35:54 +0100232 s->bind_ipv6_only = r ? SOCKET_ADDRESS_IPV6_ONLY : SOCKET_ADDRESS_BOTH;
233
Lennart Poettering42f4e3c2010-01-19 02:56:37 +0100234 return 0;
235}
236
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100237static int config_parse_nice(
238 const char *filename,
239 unsigned line,
240 const char *section,
241 const char *lvalue,
242 const char *rvalue,
243 void *data,
244 void *userdata) {
245
Lennart Poetteringfb33a392010-01-28 02:53:56 +0100246 ExecContext *c = data;
247 int priority, r;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100248
249 assert(filename);
250 assert(lvalue);
251 assert(rvalue);
252 assert(data);
253
254 if ((r = safe_atoi(rvalue, &priority)) < 0) {
255 log_error("[%s:%u] Failed to parse nice priority: %s", filename, line, rvalue);
256 return r;
257 }
258
259 if (priority < PRIO_MIN || priority >= PRIO_MAX) {
260 log_error("[%s:%u] Nice priority out of range: %s", filename, line, rvalue);
261 return -ERANGE;
262 }
263
Lennart Poetteringfb33a392010-01-28 02:53:56 +0100264 c->nice = priority;
265 c->nice_set = false;
266
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100267 return 0;
268}
269
270static int config_parse_oom_adjust(
271 const char *filename,
272 unsigned line,
273 const char *section,
274 const char *lvalue,
275 const char *rvalue,
276 void *data,
277 void *userdata) {
278
Lennart Poetteringfb33a392010-01-28 02:53:56 +0100279 ExecContext *c = data;
280 int oa, r;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100281
282 assert(filename);
283 assert(lvalue);
284 assert(rvalue);
285 assert(data);
286
287 if ((r = safe_atoi(rvalue, &oa)) < 0) {
288 log_error("[%s:%u] Failed to parse OOM adjust value: %s", filename, line, rvalue);
289 return r;
290 }
291
292 if (oa < OOM_DISABLE || oa > OOM_ADJUST_MAX) {
293 log_error("[%s:%u] OOM adjust value out of range: %s", filename, line, rvalue);
294 return -ERANGE;
295 }
296
Lennart Poetteringfb33a392010-01-28 02:53:56 +0100297 c->oom_adjust = oa;
298 c->oom_adjust_set = true;
299
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100300 return 0;
301}
302
Lennart Poetteringb5a06992010-02-12 02:02:14 +0100303static int config_parse_mode(
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100304 const char *filename,
305 unsigned line,
306 const char *section,
307 const char *lvalue,
308 const char *rvalue,
309 void *data,
310 void *userdata) {
311
312 mode_t *m = data;
313 long l;
314 char *x = NULL;
315
316 assert(filename);
317 assert(lvalue);
318 assert(rvalue);
319 assert(data);
320
321 errno = 0;
322 l = strtol(rvalue, &x, 8);
323 if (!x || *x || errno) {
Lennart Poetteringb5a06992010-02-12 02:02:14 +0100324 log_error("[%s:%u] Failed to parse mode value: %s", filename, line, rvalue);
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100325 return errno ? -errno : -EINVAL;
326 }
327
Lennart Poetteringb5a06992010-02-12 02:02:14 +0100328 if (l < 0000 || l > 07777) {
329 log_error("[%s:%u] mode value out of range: %s", filename, line, rvalue);
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100330 return -ERANGE;
331 }
332
333 *m = (mode_t) l;
334 return 0;
335}
336
337static int config_parse_exec(
338 const char *filename,
339 unsigned line,
340 const char *section,
341 const char *lvalue,
342 const char *rvalue,
343 void *data,
344 void *userdata) {
345
Lennart Poetteringa6a80b42010-02-14 01:05:55 +0100346 ExecCommand **e = data, *nce = NULL;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100347 char **n;
348 char *w;
349 unsigned k;
350 size_t l;
351 char *state;
352
353 assert(filename);
354 assert(lvalue);
355 assert(rvalue);
356 assert(data);
357
358 k = 0;
359 FOREACH_WORD_QUOTED(w, l, rvalue, state)
360 k++;
361
362 if (!(n = new(char*, k+1)))
363 return -ENOMEM;
364
Lennart Poettering44d8db92010-01-26 07:02:51 +0100365 k = 0;
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100366 FOREACH_WORD_QUOTED(w, l, rvalue, state)
367 if (!(n[k++] = strndup(w, l)))
368 goto fail;
369
370 n[k] = NULL;
371
Lennart Poettering0301abf2010-01-27 00:15:56 +0100372 if (!n[0] || !path_is_absolute(n[0])) {
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100373 log_error("[%s:%u] Invalid executable path in command line: %s", filename, line, rvalue);
374 strv_free(n);
375 return -EINVAL;
376 }
377
378 if (!(nce = new0(ExecCommand, 1)))
379 goto fail;
380
381 nce->argv = n;
382 if (!(nce->path = strdup(n[0])))
383 goto fail;
384
Lennart Poetteringa6a80b42010-02-14 01:05:55 +0100385 exec_command_append_list(e, nce);
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100386
387 return 0;
388
389fail:
390 for (; k > 0; k--)
391 free(n[k-1]);
392 free(n);
393
394 free(nce);
395
396 return -ENOMEM;
397}
398
399static int config_parse_usec(
400 const char *filename,
401 unsigned line,
402 const char *section,
403 const char *lvalue,
404 const char *rvalue,
405 void *data,
406 void *userdata) {
407
408 usec_t *usec = data;
409 unsigned long long u;
410 int r;
411
412 assert(filename);
413 assert(lvalue);
414 assert(rvalue);
415 assert(data);
416
417 if ((r = safe_atollu(rvalue, &u)) < 0) {
418 log_error("[%s:%u] Failed to parse time value: %s", filename, line, rvalue);
419 return r;
420 }
421
422 /* We actually assume the user configures seconds. Later on we
423 * might choose to support suffixes for time values, to
424 * configure bigger or smaller units */
425
426 *usec = u * USEC_PER_SEC;
427
428 return 0;
429}
430
Lennart Poettering0d87eb42010-04-13 02:22:41 +0200431DEFINE_CONFIG_PARSE_ENUM(config_parse_service_type, service_type, ServiceType, "Failed to parse service type");
432DEFINE_CONFIG_PARSE_ENUM(config_parse_service_restart, service_restart, ServiceRestart, "Failed to parse service restart specifier");
Lennart Poettering034c6ed2010-01-26 04:18:44 +0100433
Lennart Poettering47be8702010-02-03 14:21:48 +0100434static int config_parse_bindtodevice(
Lennart Poetteringacbb0222010-01-27 04:31:52 +0100435 const char *filename,
436 unsigned line,
437 const char *section,
438 const char *lvalue,
439 const char *rvalue,
440 void *data,
441 void *userdata) {
442
443 Socket *s = data;
444 char *n;
445
446 assert(filename);
447 assert(lvalue);
448 assert(rvalue);
449 assert(data);
450
451 if (rvalue[0] && !streq(rvalue, "*")) {
452 if (!(n = strdup(rvalue)))
453 return -ENOMEM;
454 } else
455 n = NULL;
456
457 free(s->bind_to_device);
458 s->bind_to_device = n;
459
460 return 0;
461}
462
Lennart Poettering0d87eb42010-04-13 02:22:41 +0200463DEFINE_CONFIG_PARSE_ENUM(config_parse_output, exec_output, ExecOutput, "Failed to parse output specifier");
464DEFINE_CONFIG_PARSE_ENUM(config_parse_input, exec_input, ExecInput, "Failed to parse input specifier");
Lennart Poettering071830f2010-01-28 02:06:20 +0100465
Lennart Poettering47be8702010-02-03 14:21:48 +0100466static int config_parse_facility(
Lennart Poettering071830f2010-01-28 02:06:20 +0100467 const char *filename,
468 unsigned line,
469 const char *section,
470 const char *lvalue,
471 const char *rvalue,
472 void *data,
473 void *userdata) {
474
Lennart Poettering071830f2010-01-28 02:06:20 +0100475
Lennart Poettering94f04342010-01-30 01:55:42 +0100476 int *o = data, x;
Lennart Poettering071830f2010-01-28 02:06:20 +0100477
478 assert(filename);
479 assert(lvalue);
480 assert(rvalue);
481 assert(data);
482
Lennart Poettering0d87eb42010-04-13 02:22:41 +0200483 if ((x = log_facility_from_string(rvalue)) < 0) {
484 log_error("[%s:%u] Failed to parse log facility: %s", filename, line, rvalue);
485 return -EBADMSG;
486 }
Lennart Poettering94f04342010-01-30 01:55:42 +0100487
488 *o = LOG_MAKEPRI(x, LOG_PRI(*o));
Lennart Poettering071830f2010-01-28 02:06:20 +0100489
490 return 0;
491}
492
Lennart Poettering47be8702010-02-03 14:21:48 +0100493static int config_parse_level(
Lennart Poettering071830f2010-01-28 02:06:20 +0100494 const char *filename,
495 unsigned line,
496 const char *section,
497 const char *lvalue,
498 const char *rvalue,
499 void *data,
500 void *userdata) {
501
Lennart Poettering071830f2010-01-28 02:06:20 +0100502
Lennart Poettering94f04342010-01-30 01:55:42 +0100503 int *o = data, x;
Lennart Poettering071830f2010-01-28 02:06:20 +0100504
505 assert(filename);
506 assert(lvalue);
507 assert(rvalue);
508 assert(data);
509
Lennart Poettering0d87eb42010-04-13 02:22:41 +0200510 if ((x = log_level_from_string(rvalue)) < 0) {
511 log_error("[%s:%u] Failed to parse log level: %s", filename, line, rvalue);
512 return -EBADMSG;
513 }
Lennart Poettering071830f2010-01-28 02:06:20 +0100514
Lennart Poettering94f04342010-01-30 01:55:42 +0100515 *o = LOG_MAKEPRI(LOG_FAC(*o), x);
Lennart Poettering071830f2010-01-28 02:06:20 +0100516 return 0;
517}
518
Lennart Poettering47be8702010-02-03 14:21:48 +0100519static int config_parse_io_class(
Lennart Poettering9eba9da2010-01-29 20:46:22 +0100520 const char *filename,
521 unsigned line,
522 const char *section,
523 const char *lvalue,
524 const char *rvalue,
525 void *data,
526 void *userdata) {
527
Lennart Poettering9eba9da2010-01-29 20:46:22 +0100528 ExecContext *c = data;
Lennart Poettering94f04342010-01-30 01:55:42 +0100529 int x;
Lennart Poettering9eba9da2010-01-29 20:46:22 +0100530
531 assert(filename);
532 assert(lvalue);
533 assert(rvalue);
534 assert(data);
535
Lennart Poettering0d87eb42010-04-13 02:22:41 +0200536 if ((x = ioprio_class_from_string(rvalue)) < 0) {
537 log_error("[%s:%u] Failed to parse IO scheduling class: %s", filename, line, rvalue);
538 return -EBADMSG;
539 }
Lennart Poettering9eba9da2010-01-29 20:46:22 +0100540
Lennart Poettering94f04342010-01-30 01:55:42 +0100541 c->ioprio = IOPRIO_PRIO_VALUE(x, IOPRIO_PRIO_DATA(c->ioprio));
Lennart Poettering9eba9da2010-01-29 20:46:22 +0100542 c->ioprio_set = true;
543
544 return 0;
545}
546
Lennart Poettering47be8702010-02-03 14:21:48 +0100547static int config_parse_io_priority(
Lennart Poettering9eba9da2010-01-29 20:46:22 +0100548 const char *filename,
549 unsigned line,
550 const char *section,
551 const char *lvalue,
552 const char *rvalue,
553 void *data,
554 void *userdata) {
555
556 ExecContext *c = data;
557 int i;
558
559 assert(filename);
560 assert(lvalue);
561 assert(rvalue);
562 assert(data);
563
Lennart Poettering94f04342010-01-30 01:55:42 +0100564 if (safe_atoi(rvalue, &i) < 0 || i < 0 || i >= IOPRIO_BE_NR) {
Lennart Poettering9eba9da2010-01-29 20:46:22 +0100565 log_error("[%s:%u] Failed to parse io priority: %s", filename, line, rvalue);
566 return -EBADMSG;
567 }
568
Lennart Poettering94f04342010-01-30 01:55:42 +0100569 c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_PRIO_CLASS(c->ioprio), i);
Lennart Poettering9eba9da2010-01-29 20:46:22 +0100570 c->ioprio_set = true;
571
572 return 0;
573}
574
Lennart Poettering47be8702010-02-03 14:21:48 +0100575static int config_parse_cpu_sched_policy(
Lennart Poettering94f04342010-01-30 01:55:42 +0100576 const char *filename,
577 unsigned line,
578 const char *section,
579 const char *lvalue,
580 const char *rvalue,
581 void *data,
582 void *userdata) {
583
584
585 ExecContext *c = data;
586 int x;
587
588 assert(filename);
589 assert(lvalue);
590 assert(rvalue);
591 assert(data);
592
Lennart Poettering0d87eb42010-04-13 02:22:41 +0200593 if ((x = sched_policy_from_string(rvalue)) < 0) {
594 log_error("[%s:%u] Failed to parse CPU scheduling policy: %s", filename, line, rvalue);
595 return -EBADMSG;
596 }
Lennart Poettering94f04342010-01-30 01:55:42 +0100597
598 c->cpu_sched_policy = x;
599 c->cpu_sched_set = true;
600
601 return 0;
602}
603
Lennart Poettering47be8702010-02-03 14:21:48 +0100604static int config_parse_cpu_sched_prio(
Lennart Poettering94f04342010-01-30 01:55:42 +0100605 const char *filename,
606 unsigned line,
607 const char *section,
608 const char *lvalue,
609 const char *rvalue,
610 void *data,
611 void *userdata) {
612
613 ExecContext *c = data;
614 int i;
615
616 assert(filename);
617 assert(lvalue);
618 assert(rvalue);
619 assert(data);
620
621 /* On Linux RR/FIFO have the same range */
622 if (safe_atoi(rvalue, &i) < 0 || i < sched_get_priority_min(SCHED_RR) || i > sched_get_priority_max(SCHED_RR)) {
623 log_error("[%s:%u] Failed to parse CPU scheduling priority: %s", filename, line, rvalue);
624 return -EBADMSG;
625 }
626
627 c->cpu_sched_priority = i;
628 c->cpu_sched_set = true;
629
630 return 0;
631}
632
Lennart Poettering47be8702010-02-03 14:21:48 +0100633static int config_parse_cpu_affinity(
Lennart Poettering94f04342010-01-30 01:55:42 +0100634 const char *filename,
635 unsigned line,
636 const char *section,
637 const char *lvalue,
638 const char *rvalue,
639 void *data,
640 void *userdata) {
641
642 ExecContext *c = data;
643 char *w;
644 size_t l;
645 char *state;
646
647 assert(filename);
648 assert(lvalue);
649 assert(rvalue);
650 assert(data);
651
Lennart Poetteringf62c0e42010-02-14 01:07:36 +0100652 FOREACH_WORD(w, l, rvalue, state) {
Lennart Poettering94f04342010-01-30 01:55:42 +0100653 char *t;
654 int r;
655 unsigned cpu;
656
657 if (!(t = strndup(w, l)))
658 return -ENOMEM;
659
660 r = safe_atou(t, &cpu);
661 free(t);
662
663 if (r < 0 || cpu >= CPU_SETSIZE) {
664 log_error("[%s:%u] Failed to parse CPU affinity: %s", filename, line, rvalue);
665 return -EBADMSG;
666 }
667
668 CPU_SET(cpu, &c->cpu_affinity);
669 }
670
671 c->cpu_affinity_set = true;
672
673 return 0;
674}
675
Lennart Poettering47be8702010-02-03 14:21:48 +0100676static int config_parse_capabilities(
Lennart Poettering94f04342010-01-30 01:55:42 +0100677 const char *filename,
678 unsigned line,
679 const char *section,
680 const char *lvalue,
681 const char *rvalue,
682 void *data,
683 void *userdata) {
684
685 ExecContext *c = data;
686 cap_t cap;
687
688 assert(filename);
689 assert(lvalue);
690 assert(rvalue);
691 assert(data);
692
693 if (!(cap = cap_from_text(rvalue))) {
694 if (errno == ENOMEM)
695 return -ENOMEM;
696
697 log_error("[%s:%u] Failed to parse capabilities: %s", filename, line, rvalue);
698 return -EBADMSG;
699 }
700
701 if (c->capabilities)
702 cap_free(c->capabilities);
703 c->capabilities = cap;
704
705 return 0;
706}
707
Lennart Poettering47be8702010-02-03 14:21:48 +0100708static int config_parse_secure_bits(
Lennart Poettering94f04342010-01-30 01:55:42 +0100709 const char *filename,
710 unsigned line,
711 const char *section,
712 const char *lvalue,
713 const char *rvalue,
714 void *data,
715 void *userdata) {
716
717 ExecContext *c = data;
718 char *w;
719 size_t l;
720 char *state;
721
722 assert(filename);
723 assert(lvalue);
724 assert(rvalue);
725 assert(data);
726
Lennart Poetteringf62c0e42010-02-14 01:07:36 +0100727 FOREACH_WORD(w, l, rvalue, state) {
Lennart Poettering94f04342010-01-30 01:55:42 +0100728 if (first_word(w, "keep-caps"))
729 c->secure_bits |= SECURE_KEEP_CAPS;
730 else if (first_word(w, "keep-caps-locked"))
731 c->secure_bits |= SECURE_KEEP_CAPS_LOCKED;
732 else if (first_word(w, "no-setuid-fixup"))
733 c->secure_bits |= SECURE_NO_SETUID_FIXUP;
734 else if (first_word(w, "no-setuid-fixup-locked"))
735 c->secure_bits |= SECURE_NO_SETUID_FIXUP_LOCKED;
736 else if (first_word(w, "noroot"))
737 c->secure_bits |= SECURE_NOROOT;
738 else if (first_word(w, "noroot-locked"))
739 c->secure_bits |= SECURE_NOROOT_LOCKED;
740 else {
741 log_error("[%s:%u] Failed to parse secure bits: %s", filename, line, rvalue);
742 return -EBADMSG;
743 }
744 }
745
746 return 0;
747}
748
Lennart Poettering47be8702010-02-03 14:21:48 +0100749static int config_parse_bounding_set(
Lennart Poettering94f04342010-01-30 01:55:42 +0100750 const char *filename,
751 unsigned line,
752 const char *section,
753 const char *lvalue,
754 const char *rvalue,
755 void *data,
756 void *userdata) {
757
758 ExecContext *c = data;
759 char *w;
760 size_t l;
761 char *state;
762
763 assert(filename);
764 assert(lvalue);
765 assert(rvalue);
766 assert(data);
767
Lennart Poetteringf62c0e42010-02-14 01:07:36 +0100768 FOREACH_WORD(w, l, rvalue, state) {
Lennart Poettering94f04342010-01-30 01:55:42 +0100769 char *t;
770 int r;
771 cap_value_t cap;
772
773 if (!(t = strndup(w, l)))
774 return -ENOMEM;
775
776 r = cap_from_name(t, &cap);
777 free(t);
778
779 if (r < 0) {
780 log_error("[%s:%u] Failed to parse capability bounding set: %s", filename, line, rvalue);
781 return -EBADMSG;
782 }
783
784 c->capability_bounding_set_drop |= 1 << cap;
785 }
786
787 return 0;
788}
789
790static int config_parse_timer_slack_ns(
791 const char *filename,
792 unsigned line,
793 const char *section,
794 const char *lvalue,
795 const char *rvalue,
796 void *data,
797 void *userdata) {
798
799 ExecContext *c = data;
800 unsigned long u;
801 int r;
802
803 assert(filename);
804 assert(lvalue);
805 assert(rvalue);
806 assert(data);
807
808 if ((r = safe_atolu(rvalue, &u)) < 0) {
809 log_error("[%s:%u] Failed to parse time slack value: %s", filename, line, rvalue);
810 return r;
811 }
812
813 c->timer_slack_ns = u;
814
815 return 0;
816}
817
818static int config_parse_limit(
819 const char *filename,
820 unsigned line,
821 const char *section,
822 const char *lvalue,
823 const char *rvalue,
824 void *data,
825 void *userdata) {
826
827 struct rlimit **rl = data;
828 unsigned long long u;
829 int r;
830
831 assert(filename);
832 assert(lvalue);
833 assert(rvalue);
834 assert(data);
835
836 if ((r = safe_atollu(rvalue, &u)) < 0) {
837 log_error("[%s:%u] Failed to parse resource value: %s", filename, line, rvalue);
838 return r;
839 }
840
841 if (!*rl)
842 if (!(*rl = new(struct rlimit, 1)))
843 return -ENOMEM;
844
845 (*rl)->rlim_cur = (*rl)->rlim_max = (rlim_t) u;
846 return 0;
847}
848
Lennart Poettering8e274522010-03-31 16:29:55 +0200849static int config_parse_cgroup(
850 const char *filename,
851 unsigned line,
852 const char *section,
853 const char *lvalue,
854 const char *rvalue,
855 void *data,
856 void *userdata) {
857
858 Unit *u = userdata;
859 char *w;
860 size_t l;
861 char *state;
862
863 FOREACH_WORD(w, l, rvalue, state) {
864 char *t;
865 int r;
866
867 if (!(t = strndup(w, l)))
868 return -ENOMEM;
869
870 r = unit_add_cgroup_from_text(u, t);
871 free(t);
872
873 if (r < 0)
874 return r;
875 }
876
877 return 0;
878}
879
Lennart Poetteringa9a1e002010-04-06 02:40:10 +0200880static int config_parse_sysv_priority(
881 const char *filename,
882 unsigned line,
883 const char *section,
884 const char *lvalue,
885 const char *rvalue,
886 void *data,
887 void *userdata) {
888
889 int *priority = data;
890 int r, i;
891
892 assert(filename);
893 assert(lvalue);
894 assert(rvalue);
895 assert(data);
896
897 if ((r = safe_atoi(rvalue, &i)) < 0 || i < 0) {
898 log_error("[%s:%u] Failed to parse SysV start priority: %s", filename, line, rvalue);
899 return r;
900 }
901
902 *priority = (int) i;
903 return 0;
904}
905
Lennart Poettering0d87eb42010-04-13 02:22:41 +0200906DEFINE_CONFIG_PARSE_ENUM(config_parse_kill_mode, kill_mode, KillMode, "Failed to parse kill mode");
Lennart Poettering50159e62010-04-08 00:52:14 +0200907
Lennart Poettering071830f2010-01-28 02:06:20 +0100908#define FOLLOW_MAX 8
Lennart Poettering87f0e412010-01-26 21:39:06 +0100909
Lennart Poettering0301abf2010-01-27 00:15:56 +0100910static int open_follow(char **filename, FILE **_f, Set *names, char **_id) {
911 unsigned c = 0;
Lennart Poettering87f0e412010-01-26 21:39:06 +0100912 int fd, r;
913 FILE *f;
Lennart Poettering0301abf2010-01-27 00:15:56 +0100914 char *id = NULL;
Lennart Poettering87f0e412010-01-26 21:39:06 +0100915
916 assert(filename);
917 assert(*filename);
918 assert(_f);
919 assert(names);
920
Lennart Poettering0301abf2010-01-27 00:15:56 +0100921 /* This will update the filename pointer if the loaded file is
922 * reached by a symlink. The old string will be freed. */
Lennart Poettering87f0e412010-01-26 21:39:06 +0100923
Lennart Poettering0301abf2010-01-27 00:15:56 +0100924 for (;;) {
Lennart Poettering87f0e412010-01-26 21:39:06 +0100925 char *target, *k, *name;
926
Lennart Poettering0301abf2010-01-27 00:15:56 +0100927 if (c++ >= FOLLOW_MAX)
928 return -ELOOP;
929
Lennart Poetteringb08d03f2010-01-29 02:07:41 +0100930 path_kill_slashes(*filename);
931
Lennart Poettering87f0e412010-01-26 21:39:06 +0100932 /* Add the file name we are currently looking at to
933 * the names of this unit */
Lennart Poettering0301abf2010-01-27 00:15:56 +0100934 name = file_name_from_path(*filename);
935 if (!(id = set_get(names, name))) {
Lennart Poettering87f0e412010-01-26 21:39:06 +0100936
Lennart Poettering0301abf2010-01-27 00:15:56 +0100937 if (!(id = strdup(name)))
938 return -ENOMEM;
939
940 if ((r = set_put(names, id)) < 0) {
941 free(id);
942 return r;
Lennart Poettering87f0e412010-01-26 21:39:06 +0100943 }
Lennart Poettering87f0e412010-01-26 21:39:06 +0100944 }
945
Lennart Poettering0301abf2010-01-27 00:15:56 +0100946 /* Try to open the file name, but don't if its a symlink */
947 if ((fd = open(*filename, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW)) >= 0)
Lennart Poettering87f0e412010-01-26 21:39:06 +0100948 break;
949
Lennart Poettering0301abf2010-01-27 00:15:56 +0100950 if (errno != ELOOP)
951 return -errno;
Lennart Poettering87f0e412010-01-26 21:39:06 +0100952
Lennart Poettering0301abf2010-01-27 00:15:56 +0100953 /* Hmm, so this is a symlink. Let's read the name, and follow it manually */
954 if ((r = readlink_malloc(*filename, &target)) < 0)
955 return r;
956
Lennart Poetteringc25fb0e2010-01-27 22:39:10 +0100957 k = file_in_same_dir(*filename, target);
Lennart Poettering87f0e412010-01-26 21:39:06 +0100958 free(target);
959
Lennart Poettering0301abf2010-01-27 00:15:56 +0100960 if (!k)
961 return -ENOMEM;
Lennart Poettering87f0e412010-01-26 21:39:06 +0100962
Lennart Poettering0301abf2010-01-27 00:15:56 +0100963 free(*filename);
964 *filename = k;
Lennart Poettering87f0e412010-01-26 21:39:06 +0100965 }
966
967 if (!(f = fdopen(fd, "r"))) {
968 r = -errno;
969 assert(close_nointr(fd) == 0);
Lennart Poettering0301abf2010-01-27 00:15:56 +0100970 return r;
Lennart Poettering87f0e412010-01-26 21:39:06 +0100971 }
972
973 *_f = f;
Lennart Poettering0301abf2010-01-27 00:15:56 +0100974 *_id = id;
975 return 0;
Lennart Poettering87f0e412010-01-26 21:39:06 +0100976}
977
Lennart Poettering23a177e2010-04-06 02:43:58 +0200978static int merge_by_names(Unit **u, Set *names, const char *id) {
979 char *k;
980 int r;
981
982 assert(u);
983 assert(*u);
984 assert(names);
985
986 /* Let's try to add in all symlink names we found */
987 while ((k = set_steal_first(names))) {
988
989 /* First try to merge in the other name into our
990 * unit */
991 if ((r = unit_merge_by_name(*u, k)) < 0) {
992 Unit *other;
993
994 /* Hmm, we couldn't merge the other unit into
995 * ours? Then let's try it the other way
996 * round */
997
998 other = manager_get_unit((*u)->meta.manager, k);
999 free(k);
1000
1001 if (other)
1002 if ((r = unit_merge(other, *u)) >= 0) {
1003 *u = other;
1004 return merge_by_names(u, names, NULL);
1005 }
1006
1007 return r;
1008 }
1009
1010 if (id == k)
1011 unit_choose_id(*u, id);
1012
1013 free(k);
1014 }
1015
1016 return 0;
1017}
1018
Lennart Poetteringe5373522010-04-10 17:53:17 +02001019static void dump_items(FILE *f, const ConfigItem *items) {
1020 const ConfigItem *i;
1021 const char *prev_section = NULL;
1022 bool not_first = false;
1023
1024 struct {
1025 ConfigParserCallback callback;
1026 const char *rvalue;
1027 } table[] = {
1028 { config_parse_int, "INTEGER" },
1029 { config_parse_unsigned, "UNSIGNED" },
1030 { config_parse_size, "SIZE" },
1031 { config_parse_bool, "BOOLEAN" },
1032 { config_parse_string, "STRING" },
1033 { config_parse_path, "PATH" },
1034 { config_parse_strv, "STRING [...]" },
1035 { config_parse_nice, "NICE" },
1036 { config_parse_oom_adjust, "OOMADJUST" },
1037 { config_parse_io_class, "IOCLASS" },
1038 { config_parse_io_priority, "IOPRIORITY" },
1039 { config_parse_cpu_sched_policy, "CPUSCHEDPOLICY" },
1040 { config_parse_cpu_sched_prio, "CPUSCHEDPRIO" },
1041 { config_parse_cpu_affinity, "CPUAFFINITY" },
1042 { config_parse_mode, "MODE" },
1043 { config_parse_output, "OUTPUT" },
1044 { config_parse_input, "INPUT" },
1045 { config_parse_facility, "FACILITY" },
1046 { config_parse_level, "LEVEL" },
1047 { config_parse_capabilities, "CAPABILITIES" },
1048 { config_parse_secure_bits, "SECUREBITS" },
1049 { config_parse_bounding_set, "BOUNDINGSET" },
1050 { config_parse_timer_slack_ns, "TIMERSLACK" },
1051 { config_parse_limit, "LIMIT" },
1052 { config_parse_cgroup, "CGROUP [...]" },
1053 { config_parse_deps, "UNIT [...]" },
1054 { config_parse_names, "UNIT [...]" },
1055 { config_parse_exec, "PATH [ARGUMENT [...]]" },
1056 { config_parse_service_type, "SERVICETYPE" },
1057 { config_parse_service_restart, "SERVICERESTART" },
1058 { config_parse_sysv_priority, "SYSVPRIORITY" },
1059 { config_parse_kill_mode, "KILLMODE" },
1060 { config_parse_listen, "SOCKET [...]" },
1061 { config_parse_socket_bind, "SOCKETBIND" },
1062 { config_parse_bindtodevice, "NETWORKINTERFACE" }
1063 };
1064
1065 assert(f);
1066 assert(items);
1067
1068 for (i = items; i->lvalue; i++) {
1069 unsigned j;
1070 const char *rvalue = "OTHER";
1071
1072 if (!streq_ptr(i->section, prev_section)) {
1073 if (!not_first)
1074 not_first = true;
1075 else
1076 fputc('\n', f);
1077
1078 fprintf(f, "[%s]\n", i->section);
1079 prev_section = i->section;
1080 }
1081
1082 for (j = 0; j < ELEMENTSOF(table); j++)
1083 if (i->parse == table[j].callback) {
1084 rvalue = table[j].rvalue;
1085 break;
1086 }
1087
1088 fprintf(f, "%s=%s\n", i->lvalue, rvalue);
1089 }
1090}
1091
1092static int load_from_path(Unit *u, const char *path) {
Lennart Poettering87f0e412010-01-26 21:39:06 +01001093
1094 static const char* const section_table[_UNIT_TYPE_MAX] = {
1095 [UNIT_SERVICE] = "Service",
1096 [UNIT_TIMER] = "Timer",
1097 [UNIT_SOCKET] = "Socket",
1098 [UNIT_TARGET] = "Target",
1099 [UNIT_DEVICE] = "Device",
1100 [UNIT_MOUNT] = "Mount",
1101 [UNIT_AUTOMOUNT] = "Automount",
1102 [UNIT_SNAPSHOT] = "Snapshot"
Lennart Poettering42f4e3c2010-01-19 02:56:37 +01001103 };
1104
Lennart Poettering034c6ed2010-01-26 04:18:44 +01001105#define EXEC_CONTEXT_CONFIG_ITEMS(context, section) \
Lennart Poettering9eba9da2010-01-29 20:46:22 +01001106 { "WorkingDirectory", config_parse_path, &(context).working_directory, section }, \
1107 { "RootDirectory", config_parse_path, &(context).root_directory, section }, \
Lennart Poettering1c01f822010-01-27 02:16:11 +01001108 { "User", config_parse_string, &(context).user, section }, \
1109 { "Group", config_parse_string, &(context).group, section }, \
1110 { "SupplementaryGroups", config_parse_strv, &(context).supplementary_groups, section }, \
Lennart Poetteringfb33a392010-01-28 02:53:56 +01001111 { "Nice", config_parse_nice, &(context), section }, \
1112 { "OOMAdjust", config_parse_oom_adjust, &(context), section }, \
Lennart Poettering9eba9da2010-01-29 20:46:22 +01001113 { "IOSchedulingClass", config_parse_io_class, &(context), section }, \
Lennart Poettering94f04342010-01-30 01:55:42 +01001114 { "IOSchedulingPriority", config_parse_io_priority, &(context), section }, \
1115 { "CPUSchedulingPolicy", config_parse_cpu_sched_policy,&(context), section }, \
1116 { "CPUSchedulingPriority", config_parse_cpu_sched_prio, &(context), section }, \
Lennart Poettering38b48752010-02-02 12:50:04 +01001117 { "CPUSchedulingResetOnFork", config_parse_bool, &(context).cpu_sched_reset_on_fork, section }, \
Lennart Poettering94f04342010-01-30 01:55:42 +01001118 { "CPUAffinity", config_parse_cpu_affinity, &(context), section }, \
Lennart Poetteringb5a06992010-02-12 02:02:14 +01001119 { "UMask", config_parse_mode, &(context).umask, section }, \
Lennart Poettering071830f2010-01-28 02:06:20 +01001120 { "Environment", config_parse_strv, &(context).environment, section }, \
Lennart Poettering80876c22010-04-13 02:06:27 +02001121 { "StandardInput", config_parse_input, &(context).std_input, section }, \
1122 { "StandardOutput", config_parse_output, &(context).std_output, section }, \
1123 { "StandardError", config_parse_output, &(context).std_output, section }, \
1124 { "TTYPath", config_parse_path, &(context).tty_path, section }, \
Lennart Poettering071830f2010-01-28 02:06:20 +01001125 { "SyslogIdentifier", config_parse_string, &(context).syslog_identifier, section }, \
1126 { "SyslogFacility", config_parse_facility, &(context).syslog_priority, section }, \
Lennart Poettering94f04342010-01-30 01:55:42 +01001127 { "SyslogLevel", config_parse_level, &(context).syslog_priority, section }, \
1128 { "Capabilities", config_parse_capabilities, &(context), section }, \
1129 { "SecureBits", config_parse_secure_bits, &(context), section }, \
1130 { "CapabilityBoundingSetDrop", config_parse_bounding_set, &(context), section }, \
1131 { "TimerSlackNS", config_parse_timer_slack_ns, &(context), section }, \
1132 { "LimitCPU", config_parse_limit, &(context).rlimit[RLIMIT_CPU], section }, \
1133 { "LimitFSIZE", config_parse_limit, &(context).rlimit[RLIMIT_FSIZE], section }, \
1134 { "LimitDATA", config_parse_limit, &(context).rlimit[RLIMIT_DATA], section }, \
1135 { "LimitSTACK", config_parse_limit, &(context).rlimit[RLIMIT_STACK], section }, \
1136 { "LimitCORE", config_parse_limit, &(context).rlimit[RLIMIT_CORE], section }, \
1137 { "LimitRSS", config_parse_limit, &(context).rlimit[RLIMIT_RSS], section }, \
1138 { "LimitNOFILE", config_parse_limit, &(context).rlimit[RLIMIT_NOFILE], section }, \
1139 { "LimitAS", config_parse_limit, &(context).rlimit[RLIMIT_AS], section }, \
1140 { "LimitNPROC", config_parse_limit, &(context).rlimit[RLIMIT_NPROC], section }, \
1141 { "LimitMEMLOCK", config_parse_limit, &(context).rlimit[RLIMIT_MEMLOCK], section }, \
1142 { "LimitLOCKS", config_parse_limit, &(context).rlimit[RLIMIT_LOCKS], section }, \
1143 { "LimitSIGPENDING", config_parse_limit, &(context).rlimit[RLIMIT_SIGPENDING], section }, \
1144 { "LimitMSGQUEUE", config_parse_limit, &(context).rlimit[RLIMIT_MSGQUEUE], section }, \
1145 { "LimitNICE", config_parse_limit, &(context).rlimit[RLIMIT_NICE], section }, \
1146 { "LimitRTPRIO", config_parse_limit, &(context).rlimit[RLIMIT_RTPRIO], section }, \
Lennart Poettering451a0742010-02-12 02:00:18 +01001147 { "LimitRTTIME", config_parse_limit, &(context).rlimit[RLIMIT_RTTIME], section }, \
Lennart Poettering80876c22010-04-13 02:06:27 +02001148 { "ControlGroup", config_parse_cgroup, u, section }
Lennart Poettering034c6ed2010-01-26 04:18:44 +01001149
Lennart Poettering3efd4192009-11-19 23:13:20 +01001150 const ConfigItem items[] = {
Lennart Poettering1c01f822010-01-27 02:16:11 +01001151 { "Names", config_parse_names, u, "Meta" },
1152 { "Description", config_parse_string, &u->meta.description, "Meta" },
1153 { "Requires", config_parse_deps, UINT_TO_PTR(UNIT_REQUIRES), "Meta" },
1154 { "SoftRequires", config_parse_deps, UINT_TO_PTR(UNIT_SOFT_REQUIRES), "Meta" },
1155 { "Wants", config_parse_deps, UINT_TO_PTR(UNIT_WANTS), "Meta" },
1156 { "Requisite", config_parse_deps, UINT_TO_PTR(UNIT_REQUISITE), "Meta" },
1157 { "SoftRequisite", config_parse_deps, UINT_TO_PTR(UNIT_SOFT_REQUISITE), "Meta" },
1158 { "Conflicts", config_parse_deps, UINT_TO_PTR(UNIT_CONFLICTS), "Meta" },
1159 { "Before", config_parse_deps, UINT_TO_PTR(UNIT_BEFORE), "Meta" },
1160 { "After", config_parse_deps, UINT_TO_PTR(UNIT_AFTER), "Meta" },
Lennart Poetteringf3bff0e2010-01-29 20:47:09 +01001161 { "RecursiveStop", config_parse_bool, &u->meta.recursive_stop, "Meta" },
1162 { "StopWhenUnneeded", config_parse_bool, &u->meta.stop_when_unneeded, "Meta" },
Lennart Poettering034c6ed2010-01-26 04:18:44 +01001163
Lennart Poettering1c01f822010-01-27 02:16:11 +01001164 { "PIDFile", config_parse_path, &u->service.pid_file, "Service" },
1165 { "ExecStartPre", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START_PRE, "Service" },
1166 { "ExecStart", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START, "Service" },
1167 { "ExecStartPost", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START_POST, "Service" },
1168 { "ExecReload", config_parse_exec, u->service.exec_command+SERVICE_EXEC_RELOAD, "Service" },
1169 { "ExecStop", config_parse_exec, u->service.exec_command+SERVICE_EXEC_STOP, "Service" },
1170 { "ExecStopPost", config_parse_exec, u->service.exec_command+SERVICE_EXEC_STOP_POST, "Service" },
1171 { "RestartSec", config_parse_usec, &u->service.restart_usec, "Service" },
1172 { "TimeoutSec", config_parse_usec, &u->service.timeout_usec, "Service" },
Lennart Poettering0d87eb42010-04-13 02:22:41 +02001173 { "Type", config_parse_service_type, &u->service.type, "Service" },
1174 { "Restart", config_parse_service_restart, &u->service.restart, "Service" },
Lennart Poettering81a2b7c2010-02-14 22:43:08 +01001175 { "PermissionsStartOnly", config_parse_bool, &u->service.permissions_start_only, "Service" },
1176 { "RootDirectoryStartOnly", config_parse_bool, &u->service.root_directory_start_only, "Service" },
Lennart Poettering8e274522010-03-31 16:29:55 +02001177 { "ValidNoProcess", config_parse_bool, &u->service.valid_no_process, "Service" },
Lennart Poetteringa9a1e002010-04-06 02:40:10 +02001178 { "SysVStartPriority", config_parse_sysv_priority, &u->service.sysv_start_priority, "Service" },
Lennart Poettering50159e62010-04-08 00:52:14 +02001179 { "KillMode", config_parse_kill_mode, &u->service.kill_mode, "Service" },
Lennart Poetteringb778d552010-04-10 17:48:41 +02001180 { "NonBlocking", config_parse_bool, &u->service.exec_context.non_blocking, "Service" },
Lennart Poettering87f0e412010-01-26 21:39:06 +01001181 EXEC_CONTEXT_CONFIG_ITEMS(u->service.exec_context, "Service"),
Lennart Poettering034c6ed2010-01-26 04:18:44 +01001182
Lennart Poettering1c01f822010-01-27 02:16:11 +01001183 { "ListenStream", config_parse_listen, &u->socket, "Socket" },
1184 { "ListenDatagram", config_parse_listen, &u->socket, "Socket" },
1185 { "ListenSequentialPacket", config_parse_listen, &u->socket, "Socket" },
1186 { "ListenFIFO", config_parse_listen, &u->socket, "Socket" },
1187 { "BindIPv6Only", config_parse_socket_bind, &u->socket, "Socket" },
1188 { "Backlog", config_parse_unsigned, &u->socket.backlog, "Socket" },
Lennart Poetteringacbb0222010-01-27 04:31:52 +01001189 { "BindToDevice", config_parse_bindtodevice, &u->socket, "Socket" },
Lennart Poettering1c01f822010-01-27 02:16:11 +01001190 { "ExecStartPre", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_START_PRE, "Socket" },
1191 { "ExecStartPost", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_START_POST, "Socket" },
1192 { "ExecStopPre", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_STOP_PRE, "Socket" },
1193 { "ExecStopPost", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_STOP_POST, "Socket" },
Lennart Poettering108736d2010-04-10 17:50:54 +02001194 { "TimeoutSec", config_parse_usec, &u->socket.timeout_usec, "Socket" },
Lennart Poetteringb5a06992010-02-12 02:02:14 +01001195 { "DirectoryMode", config_parse_mode, &u->socket.directory_mode, "Socket" },
1196 { "SocketMode", config_parse_mode, &u->socket.socket_mode, "Socket" },
Lennart Poettering50159e62010-04-08 00:52:14 +02001197 { "KillMode", config_parse_kill_mode, &u->socket.kill_mode, "Socket" },
Lennart Poettering87f0e412010-01-26 21:39:06 +01001198 EXEC_CONTEXT_CONFIG_ITEMS(u->socket.exec_context, "Socket"),
Lennart Poettering034c6ed2010-01-26 04:18:44 +01001199
Lennart Poetteringe5373522010-04-10 17:53:17 +02001200 { "What", config_parse_string, &u->mount.parameters_fragment.what, "Mount" },
1201 { "Where", config_parse_path, &u->mount.where, "Mount" },
1202 { "Options", config_parse_string, &u->mount.parameters_fragment.options, "Mount" },
1203 { "Type", config_parse_string, &u->mount.parameters_fragment.fstype, "Mount" },
1204 { "TimeoutSec", config_parse_usec, &u->mount.timeout_usec, "Mount" },
1205 { "KillMode", config_parse_kill_mode, &u->mount.kill_mode, "Mount" },
1206 EXEC_CONTEXT_CONFIG_ITEMS(u->mount.exec_context, "Mount"),
Lennart Poettering034c6ed2010-01-26 04:18:44 +01001207
Lennart Poettering3efd4192009-11-19 23:13:20 +01001208 { NULL, NULL, NULL, NULL }
1209 };
1210
Lennart Poettering034c6ed2010-01-26 04:18:44 +01001211#undef EXEC_CONTEXT_CONFIG_ITEMS
Lennart Poettering42f4e3c2010-01-19 02:56:37 +01001212
Lennart Poettering42f4e3c2010-01-19 02:56:37 +01001213 const char *sections[3];
Lennart Poettering0301abf2010-01-27 00:15:56 +01001214 char *k;
1215 int r;
Lennart Poettering87f0e412010-01-26 21:39:06 +01001216 Set *symlink_names;
Lennart Poettering23a177e2010-04-06 02:43:58 +02001217 FILE *f = NULL;
1218 char *filename = NULL, *id = NULL;
1219 Unit *merged;
1220
Lennart Poetteringe5373522010-04-10 17:53:17 +02001221 if (!u) {
1222 /* Dirty dirty hack. */
1223 dump_items((FILE*) path, items);
1224 return 0;
1225 }
1226
Lennart Poettering23a177e2010-04-06 02:43:58 +02001227 assert(u);
Lennart Poetteringe5373522010-04-10 17:53:17 +02001228 assert(path);
Lennart Poettering3efd4192009-11-19 23:13:20 +01001229
Lennart Poettering42f4e3c2010-01-19 02:56:37 +01001230 sections[0] = "Meta";
Lennart Poettering87f0e412010-01-26 21:39:06 +01001231 sections[1] = section_table[u->meta.type];
Lennart Poettering42f4e3c2010-01-19 02:56:37 +01001232 sections[2] = NULL;
1233
Lennart Poettering87f0e412010-01-26 21:39:06 +01001234 if (!(symlink_names = set_new(string_hash_func, string_compare_func)))
1235 return -ENOMEM;
Lennart Poettering3efd4192009-11-19 23:13:20 +01001236
Lennart Poettering036643a2010-02-13 01:07:02 +01001237 if (path_is_absolute(path)) {
1238
1239 if (!(filename = strdup(path))) {
1240 r = -ENOMEM;
1241 goto finish;
1242 }
1243
1244 if ((r = open_follow(&filename, &f, symlink_names, &id)) < 0) {
1245 free(filename);
1246 filename = NULL;
1247
1248 if (r != -ENOENT)
1249 goto finish;
1250 }
1251
1252 } else {
1253 char **p;
1254
1255 STRV_FOREACH(p, u->meta.manager->unit_path) {
1256
1257 /* Instead of opening the path right away, we manually
1258 * follow all symlinks and add their name to our unit
1259 * name set while doing so */
1260 if (!(filename = path_make_absolute(path, *p))) {
1261 r = -ENOMEM;
1262 goto finish;
1263 }
1264
1265 if ((r = open_follow(&filename, &f, symlink_names, &id)) < 0) {
1266 char *sn;
1267
1268 free(filename);
1269 filename = NULL;
1270
1271 if (r != -ENOENT)
1272 goto finish;
1273
1274 /* Empty the symlink names for the next run */
1275 while ((sn = set_steal_first(symlink_names)))
1276 free(sn);
1277
1278 continue;
1279 }
1280
1281 break;
1282 }
Lennart Poettering034c6ed2010-01-26 04:18:44 +01001283 }
1284
Lennart Poettering036643a2010-02-13 01:07:02 +01001285 if (!filename) {
Lennart Poettering23a177e2010-04-06 02:43:58 +02001286 r = 0;
1287 goto finish;
1288 }
1289
1290 merged = u;
1291 if ((r = merge_by_names(&merged, symlink_names, id)) < 0)
1292 goto finish;
1293
1294 if (merged != u) {
Lennart Poetteringe5373522010-04-10 17:53:17 +02001295 u->meta.load_state = UNIT_MERGED;
Lennart Poettering23a177e2010-04-06 02:43:58 +02001296 r = 0;
Lennart Poettering0301abf2010-01-27 00:15:56 +01001297 goto finish;
1298 }
1299
1300 /* Now, parse the file contents */
Lennart Poettering23a177e2010-04-06 02:43:58 +02001301 if ((r = config_parse(filename, f, sections, items, u)) < 0)
Lennart Poettering0301abf2010-01-27 00:15:56 +01001302 goto finish;
1303
Lennart Poettering6be1e7d2010-02-14 01:01:10 +01001304 free(u->meta.fragment_path);
1305 u->meta.fragment_path = filename;
Lennart Poettering0301abf2010-01-27 00:15:56 +01001306 filename = NULL;
1307
Lennart Poetteringe5373522010-04-10 17:53:17 +02001308 u->meta.load_state = UNIT_LOADED;
Lennart Poettering23a177e2010-04-06 02:43:58 +02001309 r = 0;
Lennart Poettering87f0e412010-01-26 21:39:06 +01001310
1311finish:
1312 while ((k = set_steal_first(symlink_names)))
1313 free(k);
Lennart Poettering23a177e2010-04-06 02:43:58 +02001314
Lennart Poettering87f0e412010-01-26 21:39:06 +01001315 set_free(symlink_names);
Lennart Poettering0301abf2010-01-27 00:15:56 +01001316 free(filename);
1317
Lennart Poettering23a177e2010-04-06 02:43:58 +02001318 if (f)
1319 fclose(f);
1320
Lennart Poettering0301abf2010-01-27 00:15:56 +01001321 return r;
1322}
1323
Lennart Poetteringe5373522010-04-10 17:53:17 +02001324int unit_load_fragment(Unit *u) {
Lennart Poettering23a177e2010-04-06 02:43:58 +02001325 int r;
Lennart Poettering0301abf2010-01-27 00:15:56 +01001326
1327 assert(u);
Lennart Poettering0301abf2010-01-27 00:15:56 +01001328
Lennart Poettering23a177e2010-04-06 02:43:58 +02001329 if (u->meta.fragment_path) {
1330
Lennart Poetteringe5373522010-04-10 17:53:17 +02001331 if ((r = load_from_path(u, u->meta.fragment_path)) < 0)
Lennart Poettering23a177e2010-04-06 02:43:58 +02001332 return r;
1333
1334 } else {
Lennart Poettering0301abf2010-01-27 00:15:56 +01001335 Iterator i;
Lennart Poettering890f4342010-02-14 01:08:20 +01001336 const char *t;
Lennart Poettering0301abf2010-01-27 00:15:56 +01001337
Lennart Poettering890f4342010-02-14 01:08:20 +01001338 /* Try to find the unit under its id */
1339 if ((t = unit_id(u)))
Lennart Poetteringe5373522010-04-10 17:53:17 +02001340 if ((r = load_from_path(u, t)) < 0)
Lennart Poettering23a177e2010-04-06 02:43:58 +02001341 return r;
Lennart Poettering890f4342010-02-14 01:08:20 +01001342
1343 /* Try to find an alias we can load this with */
Lennart Poetteringe5373522010-04-10 17:53:17 +02001344 if (u->meta.load_state == UNIT_STUB)
Lennart Poettering23a177e2010-04-06 02:43:58 +02001345 SET_FOREACH(t, u->meta.names, i) {
1346
1347 if (unit_id(u) == t)
1348 continue;
1349
Lennart Poetteringe5373522010-04-10 17:53:17 +02001350 if ((r = load_from_path(u, t)) < 0)
Lennart Poettering23a177e2010-04-06 02:43:58 +02001351 return r;
1352
Lennart Poetteringe5373522010-04-10 17:53:17 +02001353 if (u->meta.load_state != UNIT_STUB)
Lennart Poettering890f4342010-02-14 01:08:20 +01001354 break;
Lennart Poettering23a177e2010-04-06 02:43:58 +02001355 }
Lennart Poettering0301abf2010-01-27 00:15:56 +01001356 }
Lennart Poettering87f0e412010-01-26 21:39:06 +01001357
Lennart Poettering23a177e2010-04-06 02:43:58 +02001358 return 0;
Lennart Poettering3efd4192009-11-19 23:13:20 +01001359}
Lennart Poetteringe5373522010-04-10 17:53:17 +02001360
1361void unit_dump_config_items(FILE *f) {
1362 /* OK, this wins a prize for extreme ugliness. */
1363
1364 load_from_path(NULL, (const void*) f);
1365}