]> git.ipfire.org Git - people/ms/systemd.git/blame - load-fragment.c
add api for choose the id name for a service
[people/ms/systemd.git] / load-fragment.c
CommitLineData
3efd4195
LP
1/*-*- Mode: C; c-basic-offset: 8 -*-*/
2
87f0e418 3#include <linux/oom.h>
3efd4195
LP
4#include <assert.h>
5#include <errno.h>
6#include <string.h>
87f0e418
LP
7#include <unistd.h>
8#include <fcntl.h>
3efd4195 9
87f0e418 10#include "unit.h"
3efd4195
LP
11#include "strv.h"
12#include "conf-parser.h"
13#include "load-fragment.h"
16354eff 14#include "log.h"
3efd4195 15
42f4e3c4 16static int config_parse_deps(
3efd4195
LP
17 const char *filename,
18 unsigned line,
19 const char *section,
20 const char *lvalue,
21 const char *rvalue,
22 void *data,
23 void *userdata) {
24
87f0e418
LP
25 UnitDependency d = PTR_TO_UINT(data);
26 Unit *u = userdata;
3efd4195
LP
27 char *w;
28 size_t l;
29 char *state;
30
31 assert(filename);
32 assert(lvalue);
33 assert(rvalue);
3efd4195
LP
34
35 FOREACH_WORD(w, &l, rvalue, state) {
36 char *t;
37 int r;
3efd4195
LP
38
39 if (!(t = strndup(w, l)))
40 return -ENOMEM;
41
b19e7dc0 42 r = unit_add_dependency_by_name(u, d, t);
3efd4195
LP
43 free(t);
44
45 if (r < 0)
46 return r;
3efd4195
LP
47 }
48
49 return 0;
50}
51
42f4e3c4 52static int config_parse_names(
87d1515d
LP
53 const char *filename,
54 unsigned line,
55 const char *section,
56 const char *lvalue,
57 const char *rvalue,
58 void *data,
59 void *userdata) {
60
87f0e418 61 Unit *u = userdata;
87d1515d
LP
62 char *w;
63 size_t l;
64 char *state;
65
66 assert(filename);
67 assert(lvalue);
68 assert(rvalue);
69 assert(data);
70
71 FOREACH_WORD(w, &l, rvalue, state) {
72 char *t;
73 int r;
87f0e418 74 Unit *other;
87d1515d
LP
75
76 if (!(t = strndup(w, l)))
77 return -ENOMEM;
78
87f0e418 79 other = manager_get_unit(u->meta.manager, t);
87d1515d
LP
80
81 if (other) {
82
87f0e418 83 if (other != u) {
87d1515d 84
87f0e418 85 if (other->meta.load_state != UNIT_STUB) {
87d1515d
LP
86 free(t);
87 return -EEXIST;
88 }
89
87f0e418 90 if ((r = unit_merge(u, other)) < 0) {
87d1515d
LP
91 free(t);
92 return r;
93 }
94 }
95
96 } else {
87f0e418 97 if ((r = unit_add_name(u, t)) < 0) {
87d1515d
LP
98 free(t);
99 return r;
100 }
101 }
102
103 free(t);
104 }
105
106 return 0;
107}
108
42f4e3c4
LP
109static int config_parse_listen(
110 const char *filename,
111 unsigned line,
112 const char *section,
113 const char *lvalue,
114 const char *rvalue,
115 void *data,
116 void *userdata) {
117
16354eff 118 int r;
542563ba
LP
119 SocketPort *p;
120 Socket *s;
16354eff 121
42f4e3c4
LP
122 assert(filename);
123 assert(lvalue);
124 assert(rvalue);
125 assert(data);
126
542563ba
LP
127 s = (Socket*) data;
128
129 if (!(p = new0(SocketPort, 1)))
130 return -ENOMEM;
131
132 if (streq(lvalue, "ListenFIFO")) {
133 p->type = SOCKET_FIFO;
134
135 if (!(p->path = strdup(rvalue))) {
136 free(p);
137 return -ENOMEM;
138 }
139 } else {
140 p->type = SOCKET_SOCKET;
141
142 if ((r = socket_address_parse(&p->address, rvalue)) < 0) {
143 log_error("[%s:%u] Failed to parse address value: %s", filename, line, rvalue);
144 free(p);
145 return r;
146 }
147
148 if (streq(lvalue, "ListenStream"))
149 p->address.type = SOCK_STREAM;
150 else if (streq(lvalue, "ListenDatagram"))
151 p->address.type = SOCK_DGRAM;
152 else {
153 assert(streq(lvalue, "ListenSequentialPacket"));
154 p->address.type = SOCK_SEQPACKET;
155 }
156
157 if (socket_address_family(&p->address) != AF_LOCAL && p->address.type == SOCK_SEQPACKET) {
158 free(p);
159 return -EPROTONOSUPPORT;
160 }
16354eff
LP
161 }
162
542563ba 163 p->fd = -1;
034c6ed7 164 LIST_PREPEND(SocketPort, port, s->ports, p);
542563ba 165
16354eff 166 return 0;
42f4e3c4
LP
167}
168
034c6ed7 169static int config_parse_socket_bind(
42f4e3c4
LP
170 const char *filename,
171 unsigned line,
172 const char *section,
173 const char *lvalue,
174 const char *rvalue,
175 void *data,
176 void *userdata) {
177
542563ba
LP
178 int r;
179 Socket *s;
42f4e3c4
LP
180
181 assert(filename);
182 assert(lvalue);
183 assert(rvalue);
184 assert(data);
185
542563ba
LP
186 s = (Socket*) data;
187
188 if ((r = parse_boolean(rvalue)) < 0) {
189 log_error("[%s:%u] Failed to parse bind IPv6 only value: %s", filename, line, rvalue);
190 return r;
16354eff 191 }
42f4e3c4 192
542563ba
LP
193 s->bind_ipv6_only = r ? SOCKET_ADDRESS_IPV6_ONLY : SOCKET_ADDRESS_BOTH;
194
42f4e3c4
LP
195 return 0;
196}
197
034c6ed7
LP
198static int config_parse_nice(
199 const char *filename,
200 unsigned line,
201 const char *section,
202 const char *lvalue,
203 const char *rvalue,
204 void *data,
205 void *userdata) {
206
fb33a393
LP
207 ExecContext *c = data;
208 int priority, r;
034c6ed7
LP
209
210 assert(filename);
211 assert(lvalue);
212 assert(rvalue);
213 assert(data);
214
215 if ((r = safe_atoi(rvalue, &priority)) < 0) {
216 log_error("[%s:%u] Failed to parse nice priority: %s", filename, line, rvalue);
217 return r;
218 }
219
220 if (priority < PRIO_MIN || priority >= PRIO_MAX) {
221 log_error("[%s:%u] Nice priority out of range: %s", filename, line, rvalue);
222 return -ERANGE;
223 }
224
fb33a393
LP
225 c->nice = priority;
226 c->nice_set = false;
227
034c6ed7
LP
228 return 0;
229}
230
231static int config_parse_oom_adjust(
232 const char *filename,
233 unsigned line,
234 const char *section,
235 const char *lvalue,
236 const char *rvalue,
237 void *data,
238 void *userdata) {
239
fb33a393
LP
240 ExecContext *c = data;
241 int oa, r;
034c6ed7
LP
242
243 assert(filename);
244 assert(lvalue);
245 assert(rvalue);
246 assert(data);
247
248 if ((r = safe_atoi(rvalue, &oa)) < 0) {
249 log_error("[%s:%u] Failed to parse OOM adjust value: %s", filename, line, rvalue);
250 return r;
251 }
252
253 if (oa < OOM_DISABLE || oa > OOM_ADJUST_MAX) {
254 log_error("[%s:%u] OOM adjust value out of range: %s", filename, line, rvalue);
255 return -ERANGE;
256 }
257
fb33a393
LP
258 c->oom_adjust = oa;
259 c->oom_adjust_set = true;
260
034c6ed7
LP
261 return 0;
262}
263
264static int config_parse_umask(
265 const char *filename,
266 unsigned line,
267 const char *section,
268 const char *lvalue,
269 const char *rvalue,
270 void *data,
271 void *userdata) {
272
273 mode_t *m = data;
274 long l;
275 char *x = NULL;
276
277 assert(filename);
278 assert(lvalue);
279 assert(rvalue);
280 assert(data);
281
282 errno = 0;
283 l = strtol(rvalue, &x, 8);
284 if (!x || *x || errno) {
285 log_error("[%s:%u] Failed to parse umask value: %s", filename, line, rvalue);
286 return errno ? -errno : -EINVAL;
287 }
288
289 if (l < 0000 || l > 0777) {
290 log_error("[%s:%u] umask value out of range: %s", filename, line, rvalue);
291 return -ERANGE;
292 }
293
294 *m = (mode_t) l;
295 return 0;
296}
297
298static int config_parse_exec(
299 const char *filename,
300 unsigned line,
301 const char *section,
302 const char *lvalue,
303 const char *rvalue,
304 void *data,
305 void *userdata) {
306
307 ExecCommand **e = data, *ee, *nce = NULL;
308 char **n;
309 char *w;
310 unsigned k;
311 size_t l;
312 char *state;
313
314 assert(filename);
315 assert(lvalue);
316 assert(rvalue);
317 assert(data);
318
319 k = 0;
320 FOREACH_WORD_QUOTED(w, l, rvalue, state)
321 k++;
322
323 if (!(n = new(char*, k+1)))
324 return -ENOMEM;
325
44d8db9e 326 k = 0;
034c6ed7
LP
327 FOREACH_WORD_QUOTED(w, l, rvalue, state)
328 if (!(n[k++] = strndup(w, l)))
329 goto fail;
330
331 n[k] = NULL;
332
0301abf4 333 if (!n[0] || !path_is_absolute(n[0])) {
034c6ed7
LP
334 log_error("[%s:%u] Invalid executable path in command line: %s", filename, line, rvalue);
335 strv_free(n);
336 return -EINVAL;
337 }
338
339 if (!(nce = new0(ExecCommand, 1)))
340 goto fail;
341
342 nce->argv = n;
343 if (!(nce->path = strdup(n[0])))
344 goto fail;
345
346 if (*e) {
347 /* It's kinda important that we keep the order here */
348 LIST_FIND_TAIL(ExecCommand, command, *e, ee);
349 LIST_INSERT_AFTER(ExecCommand, command, *e, ee, nce);
350 } else
351 *e = nce;
352
353 return 0;
354
355fail:
356 for (; k > 0; k--)
357 free(n[k-1]);
358 free(n);
359
360 free(nce);
361
362 return -ENOMEM;
363}
364
365static int config_parse_usec(
366 const char *filename,
367 unsigned line,
368 const char *section,
369 const char *lvalue,
370 const char *rvalue,
371 void *data,
372 void *userdata) {
373
374 usec_t *usec = data;
375 unsigned long long u;
376 int r;
377
378 assert(filename);
379 assert(lvalue);
380 assert(rvalue);
381 assert(data);
382
383 if ((r = safe_atollu(rvalue, &u)) < 0) {
384 log_error("[%s:%u] Failed to parse time value: %s", filename, line, rvalue);
385 return r;
386 }
387
388 /* We actually assume the user configures seconds. Later on we
389 * might choose to support suffixes for time values, to
390 * configure bigger or smaller units */
391
392 *usec = u * USEC_PER_SEC;
393
394 return 0;
395}
396
397static int config_parse_service_type(
398 const char *filename,
399 unsigned line,
400 const char *section,
401 const char *lvalue,
402 const char *rvalue,
403 void *data,
404 void *userdata) {
405
406 Service *s = data;
407
408 assert(filename);
409 assert(lvalue);
410 assert(rvalue);
411 assert(data);
412
413 if (streq(rvalue, "forking"))
414 s->type = SERVICE_FORKING;
415 else if (streq(rvalue, "simple"))
416 s->type = SERVICE_SIMPLE;
417 else {
418 log_error("[%s:%u] Failed to parse service type: %s", filename, line, rvalue);
419 return -EBADMSG;
420 }
421
422 return 0;
423}
424
425static int config_parse_service_restart(
426 const char *filename,
427 unsigned line,
428 const char *section,
429 const char *lvalue,
430 const char *rvalue,
431 void *data,
432 void *userdata) {
433
434 Service *s = data;
435
436 assert(filename);
437 assert(lvalue);
438 assert(rvalue);
439 assert(data);
440
441 if (streq(rvalue, "once"))
442 s->restart = SERVICE_ONCE;
443 else if (streq(rvalue, "on-success"))
444 s->type = SERVICE_RESTART_ON_SUCCESS;
445 else if (streq(rvalue, "always"))
446 s->type = SERVICE_RESTART_ALWAYS;
447 else {
448 log_error("[%s:%u] Failed to parse service type: %s", filename, line, rvalue);
449 return -EBADMSG;
450 }
451
452 return 0;
453}
454
acbb0225
LP
455int config_parse_bindtodevice(
456 const char *filename,
457 unsigned line,
458 const char *section,
459 const char *lvalue,
460 const char *rvalue,
461 void *data,
462 void *userdata) {
463
464 Socket *s = data;
465 char *n;
466
467 assert(filename);
468 assert(lvalue);
469 assert(rvalue);
470 assert(data);
471
472 if (rvalue[0] && !streq(rvalue, "*")) {
473 if (!(n = strdup(rvalue)))
474 return -ENOMEM;
475 } else
476 n = NULL;
477
478 free(s->bind_to_device);
479 s->bind_to_device = n;
480
481 return 0;
482}
483
071830ff
LP
484int config_parse_output(
485 const char *filename,
486 unsigned line,
487 const char *section,
488 const char *lvalue,
489 const char *rvalue,
490 void *data,
491 void *userdata) {
492
493 ExecOutput *o = data;
494
495 assert(filename);
496 assert(lvalue);
497 assert(rvalue);
498 assert(data);
499
500 if (streq(rvalue, "syslog"))
501 *o = EXEC_SYSLOG;
502 else if (streq(rvalue, "null"))
503 *o = EXEC_NULL;
504 else if (streq(rvalue, "syslog"))
505 *o = EXEC_SYSLOG;
506 else if (streq(rvalue, "kernel"))
507 *o = EXEC_KERNEL;
508 else {
509 log_error("[%s:%u] Failed to parse log output: %s", filename, line, rvalue);
510 return -EBADMSG;
511 }
512
513 return 0;
514}
87f0e418 515
071830ff
LP
516int config_parse_facility(
517 const char *filename,
518 unsigned line,
519 const char *section,
520 const char *lvalue,
521 const char *rvalue,
522 void *data,
523 void *userdata) {
524
525 static const char * const table[LOG_NFACILITIES] = {
526 [LOG_FAC(LOG_KERN)] = "kern",
527 [LOG_FAC(LOG_USER)] = "user",
528 [LOG_FAC(LOG_MAIL)] = "mail",
529 [LOG_FAC(LOG_DAEMON)] = "daemon",
530 [LOG_FAC(LOG_AUTH)] = "auth",
531 [LOG_FAC(LOG_SYSLOG)] = "syslog",
532 [LOG_FAC(LOG_LPR)] = "lpr",
533 [LOG_FAC(LOG_NEWS)] = "news",
534 [LOG_FAC(LOG_UUCP)] = "uucp",
535 [LOG_FAC(LOG_CRON)] = "cron",
536 [LOG_FAC(LOG_AUTHPRIV)] = "authpriv",
537 [LOG_FAC(LOG_FTP)] = "ftp",
538 [LOG_FAC(LOG_LOCAL0)] = "local0",
539 [LOG_FAC(LOG_LOCAL1)] = "local1",
540 [LOG_FAC(LOG_LOCAL2)] = "local2",
541 [LOG_FAC(LOG_LOCAL3)] = "local3",
542 [LOG_FAC(LOG_LOCAL4)] = "local4",
543 [LOG_FAC(LOG_LOCAL5)] = "local5",
544 [LOG_FAC(LOG_LOCAL6)] = "local6",
545 [LOG_FAC(LOG_LOCAL7)] = "local7"
546 };
547
548 ExecOutput *o = data;
549 int i;
550
551 assert(filename);
552 assert(lvalue);
553 assert(rvalue);
554 assert(data);
555
556 for (i = 0; i < (int) ELEMENTSOF(table); i++)
557 if (streq(rvalue, table[i])) {
558 *o = LOG_MAKEPRI(i, LOG_PRI(*o));
559 break;
560 }
561
562 if (i >= (int) ELEMENTSOF(table)) {
563
564 /* Second try, let's see if this is a number. */
565 if (safe_atoi(rvalue, &i) >= 0 &&
566 i >= 0 &&
567 i < (int) ELEMENTSOF(table))
568 *o = LOG_MAKEPRI(i, LOG_PRI(*o));
569 else {
570 log_error("[%s:%u] Failed to parse log output: %s", filename, line, rvalue);
571 return -EBADMSG;
572 }
573 }
574
575 return 0;
576}
577
578int config_parse_level(
579 const char *filename,
580 unsigned line,
581 const char *section,
582 const char *lvalue,
583 const char *rvalue,
584 void *data,
585 void *userdata) {
586
587 static const char * const table[LOG_DEBUG+1] = {
588 [LOG_EMERG] = "emerg",
589 [LOG_ALERT] = "alert",
590 [LOG_CRIT] = "crit",
591 [LOG_ERR] = "err",
592 [LOG_WARNING] = "warning",
593 [LOG_NOTICE] = "notice",
594 [LOG_INFO] = "info",
595 [LOG_DEBUG] = "debug"
596 };
597
598 ExecOutput *o = data;
599 int i;
600
601 assert(filename);
602 assert(lvalue);
603 assert(rvalue);
604 assert(data);
605
606 for (i = 0; i < (int) ELEMENTSOF(table); i++)
607 if (streq(rvalue, table[i])) {
608 *o = LOG_MAKEPRI(LOG_FAC(*o), i);
609 break;
610 }
611
612 if (i >= LOG_NFACILITIES) {
613
614 /* Second try, let's see if this is a number. */
615 if (safe_atoi(rvalue, &i) >= 0 &&
616 i >= 0 &&
617 i < (int) ELEMENTSOF(table))
618 *o = LOG_MAKEPRI(LOG_FAC(*o), i);
619 else {
620 log_error("[%s:%u] Failed to parse log output: %s", filename, line, rvalue);
621 return -EBADMSG;
622 }
623 }
624
625 return 0;
626}
627
628#define FOLLOW_MAX 8
87f0e418 629
0301abf4
LP
630static int open_follow(char **filename, FILE **_f, Set *names, char **_id) {
631 unsigned c = 0;
87f0e418
LP
632 int fd, r;
633 FILE *f;
0301abf4 634 char *id = NULL;
87f0e418
LP
635
636 assert(filename);
637 assert(*filename);
638 assert(_f);
639 assert(names);
640
0301abf4
LP
641 /* This will update the filename pointer if the loaded file is
642 * reached by a symlink. The old string will be freed. */
87f0e418 643
0301abf4 644 for (;;) {
87f0e418
LP
645 char *target, *k, *name;
646
0301abf4
LP
647 if (c++ >= FOLLOW_MAX)
648 return -ELOOP;
649
87f0e418
LP
650 /* Add the file name we are currently looking at to
651 * the names of this unit */
0301abf4
LP
652 name = file_name_from_path(*filename);
653 if (!(id = set_get(names, name))) {
87f0e418 654
0301abf4
LP
655 if (!(id = strdup(name)))
656 return -ENOMEM;
87f0e418 657
0301abf4
LP
658 if ((r = set_put(names, id)) < 0) {
659 free(id);
660 return r;
87f0e418 661 }
87f0e418
LP
662 }
663
0301abf4
LP
664 /* Try to open the file name, but don't if its a symlink */
665 if ((fd = open(*filename, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW)) >= 0)
87f0e418
LP
666 break;
667
0301abf4
LP
668 if (errno != ELOOP)
669 return -errno;
670
87f0e418 671 /* Hmm, so this is a symlink. Let's read the name, and follow it manually */
0301abf4
LP
672 if ((r = readlink_malloc(*filename, &target)) < 0)
673 return r;
87f0e418 674
c25fb0ed 675 k = file_in_same_dir(*filename, target);
87f0e418
LP
676 free(target);
677
0301abf4
LP
678 if (!k)
679 return -ENOMEM;
87f0e418 680
0301abf4
LP
681 free(*filename);
682 *filename = k;
87f0e418
LP
683 }
684
685 if (!(f = fdopen(fd, "r"))) {
686 r = -errno;
687 assert(close_nointr(fd) == 0);
0301abf4 688 return r;
87f0e418
LP
689 }
690
691 *_f = f;
0301abf4
LP
692 *_id = id;
693 return 0;
87f0e418
LP
694}
695
0301abf4 696static int load_from_path(Unit *u, const char *path) {
87f0e418
LP
697
698 static const char* const section_table[_UNIT_TYPE_MAX] = {
699 [UNIT_SERVICE] = "Service",
700 [UNIT_TIMER] = "Timer",
701 [UNIT_SOCKET] = "Socket",
702 [UNIT_TARGET] = "Target",
703 [UNIT_DEVICE] = "Device",
704 [UNIT_MOUNT] = "Mount",
705 [UNIT_AUTOMOUNT] = "Automount",
706 [UNIT_SNAPSHOT] = "Snapshot"
42f4e3c4
LP
707 };
708
034c6ed7 709#define EXEC_CONTEXT_CONFIG_ITEMS(context, section) \
1c01f82b
LP
710 { "Directory", config_parse_path, &(context).directory, section }, \
711 { "User", config_parse_string, &(context).user, section }, \
712 { "Group", config_parse_string, &(context).group, section }, \
713 { "SupplementaryGroups", config_parse_strv, &(context).supplementary_groups, section }, \
fb33a393
LP
714 { "Nice", config_parse_nice, &(context), section }, \
715 { "OOMAdjust", config_parse_oom_adjust, &(context), section }, \
1c01f82b 716 { "UMask", config_parse_umask, &(context).umask, section }, \
071830ff
LP
717 { "Environment", config_parse_strv, &(context).environment, section }, \
718 { "Output", config_parse_output, &(context).output, section }, \
719 { "SyslogIdentifier", config_parse_string, &(context).syslog_identifier, section }, \
720 { "SyslogFacility", config_parse_facility, &(context).syslog_priority, section }, \
721 { "SyslogLevel", config_parse_level, &(context).syslog_priority, section }
034c6ed7 722
3efd4195 723 const ConfigItem items[] = {
1c01f82b
LP
724 { "Names", config_parse_names, u, "Meta" },
725 { "Description", config_parse_string, &u->meta.description, "Meta" },
726 { "Requires", config_parse_deps, UINT_TO_PTR(UNIT_REQUIRES), "Meta" },
727 { "SoftRequires", config_parse_deps, UINT_TO_PTR(UNIT_SOFT_REQUIRES), "Meta" },
728 { "Wants", config_parse_deps, UINT_TO_PTR(UNIT_WANTS), "Meta" },
729 { "Requisite", config_parse_deps, UINT_TO_PTR(UNIT_REQUISITE), "Meta" },
730 { "SoftRequisite", config_parse_deps, UINT_TO_PTR(UNIT_SOFT_REQUISITE), "Meta" },
731 { "Conflicts", config_parse_deps, UINT_TO_PTR(UNIT_CONFLICTS), "Meta" },
732 { "Before", config_parse_deps, UINT_TO_PTR(UNIT_BEFORE), "Meta" },
733 { "After", config_parse_deps, UINT_TO_PTR(UNIT_AFTER), "Meta" },
734
735 { "PIDFile", config_parse_path, &u->service.pid_file, "Service" },
736 { "ExecStartPre", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START_PRE, "Service" },
737 { "ExecStart", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START, "Service" },
738 { "ExecStartPost", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START_POST, "Service" },
739 { "ExecReload", config_parse_exec, u->service.exec_command+SERVICE_EXEC_RELOAD, "Service" },
740 { "ExecStop", config_parse_exec, u->service.exec_command+SERVICE_EXEC_STOP, "Service" },
741 { "ExecStopPost", config_parse_exec, u->service.exec_command+SERVICE_EXEC_STOP_POST, "Service" },
742 { "RestartSec", config_parse_usec, &u->service.restart_usec, "Service" },
743 { "TimeoutSec", config_parse_usec, &u->service.timeout_usec, "Service" },
744 { "Type", config_parse_service_type, &u->service, "Service" },
745 { "Restart", config_parse_service_restart, &u->service, "Service" },
87f0e418
LP
746 EXEC_CONTEXT_CONFIG_ITEMS(u->service.exec_context, "Service"),
747
1c01f82b
LP
748 { "ListenStream", config_parse_listen, &u->socket, "Socket" },
749 { "ListenDatagram", config_parse_listen, &u->socket, "Socket" },
750 { "ListenSequentialPacket", config_parse_listen, &u->socket, "Socket" },
751 { "ListenFIFO", config_parse_listen, &u->socket, "Socket" },
752 { "BindIPv6Only", config_parse_socket_bind, &u->socket, "Socket" },
753 { "Backlog", config_parse_unsigned, &u->socket.backlog, "Socket" },
acbb0225 754 { "BindToDevice", config_parse_bindtodevice, &u->socket, "Socket" },
1c01f82b
LP
755 { "ExecStartPre", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_START_PRE, "Socket" },
756 { "ExecStartPost", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_START_POST, "Socket" },
757 { "ExecStopPre", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_STOP_PRE, "Socket" },
758 { "ExecStopPost", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_STOP_POST, "Socket" },
87f0e418
LP
759 EXEC_CONTEXT_CONFIG_ITEMS(u->socket.exec_context, "Socket"),
760
761 EXEC_CONTEXT_CONFIG_ITEMS(u->automount.exec_context, "Automount"),
034c6ed7 762
3efd4195
LP
763 { NULL, NULL, NULL, NULL }
764 };
765
034c6ed7 766#undef EXEC_CONTEXT_CONFIG_ITEMS
42f4e3c4 767
42f4e3c4 768 const char *sections[3];
0301abf4
LP
769 char *k;
770 int r;
87f0e418 771 Set *symlink_names;
0301abf4
LP
772 FILE *f;
773 char *filename, *id;
3efd4195 774
42f4e3c4 775 sections[0] = "Meta";
87f0e418 776 sections[1] = section_table[u->meta.type];
42f4e3c4
LP
777 sections[2] = NULL;
778
87f0e418
LP
779 if (!(symlink_names = set_new(string_hash_func, string_compare_func)))
780 return -ENOMEM;
3efd4195 781
0301abf4
LP
782 /* Instead of opening the path right away, we manually
783 * follow all symlinks and add their name to our unit
784 * name set while doing so */
785 if (!(filename = path_make_absolute(path, unit_path()))) {
786 r = -ENOMEM;
787 goto finish;
788 }
3efd4195 789
0301abf4
LP
790 if ((r = open_follow(&filename, &f, symlink_names, &id)) < 0) {
791 if (r == -ENOENT)
792 r = 0; /* returning 0 means: no suitable config file found */
034c6ed7 793
0301abf4
LP
794 goto finish;
795 }
87f0e418 796
0301abf4
LP
797 /* Now, parse the file contents */
798 r = config_parse(filename, f, sections, items, u);
799 if (r < 0)
800 goto finish;
87f0e418 801
0301abf4
LP
802 /* Let's try to add in all symlink names we found */
803 while ((k = set_steal_first(symlink_names))) {
804 if ((r = unit_add_name(u, k)) < 0)
87f0e418
LP
805 goto finish;
806
0301abf4
LP
807 if (id == k)
808 assert_se(u->meta.id = set_get(u->meta.names, k));
87f0e418 809
0301abf4 810 free(k);
034c6ed7
LP
811 }
812
0301abf4
LP
813 free(u->meta.load_path);
814 u->meta.load_path = filename;
815 filename = NULL;
87f0e418 816
0301abf4 817 r = 1; /* returning 1 means: suitable config file found and loaded */
87f0e418
LP
818
819finish:
820 while ((k = set_steal_first(symlink_names)))
821 free(k);
87f0e418 822 set_free(symlink_names);
0301abf4
LP
823 free(filename);
824
825 return r;
826}
827
828int unit_load_fragment(Unit *u) {
d46de8a1 829 int r = 0;
071830ff 830 ExecContext *c;
0301abf4
LP
831
832 assert(u);
833 assert(u->meta.load_state == UNIT_STUB);
834
835 if (u->meta.load_path)
836 r = load_from_path(u, u->meta.load_path);
837 else {
838 Iterator i;
839 char *t;
840
841 /* Try to find a name we can load this with */
842 SET_FOREACH(t, u->meta.names, i)
843 if ((r = load_from_path(u, t)) != 0)
844 return r;
845 }
87f0e418 846
071830ff
LP
847 if (u->meta.type == UNIT_SOCKET)
848 c = &u->socket.exec_context;
849 else if (u->meta.type == UNIT_SERVICE)
850 c = &u->service.exec_context;
851 else
852 c = NULL;
853
854 if (r >= 0 && c &&
855 (c->output == EXEC_KERNEL || c->output == EXEC_SYSLOG)) {
d46de8a1
LP
856 int k;
857
071830ff
LP
858 /* If syslog or kernel logging is requested, make sure
859 * our own logging daemon is run first. */
860
d46de8a1
LP
861 if ((k = unit_add_dependency(u, UNIT_AFTER, u->meta.manager->special_units[SPECIAL_LOGGER_SOCKET])) < 0)
862 return k;
071830ff 863
d46de8a1
LP
864 if ((k = unit_add_dependency(u, UNIT_REQUIRES, u->meta.manager->special_units[SPECIAL_LOGGER_SOCKET])) < 0)
865 return k;
071830ff
LP
866 }
867
87f0e418 868 return r;
3efd4195 869}