]> git.ipfire.org Git - people/ms/systemd.git/blob - load-fragment.c
implement coldpluggin
[people/ms/systemd.git] / load-fragment.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
2
3 #include <linux/oom.h>
4 #include <assert.h>
5 #include <errno.h>
6 #include <string.h>
7 #include <unistd.h>
8 #include <fcntl.h>
9
10 #include "unit.h"
11 #include "strv.h"
12 #include "conf-parser.h"
13 #include "load-fragment.h"
14 #include "log.h"
15
16 static int config_parse_deps(
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
25 UnitDependency d = PTR_TO_UINT(data);
26 Unit *u = userdata;
27 char *w;
28 size_t l;
29 char *state;
30
31 assert(filename);
32 assert(lvalue);
33 assert(rvalue);
34
35 FOREACH_WORD(w, &l, rvalue, state) {
36 char *t;
37 int r;
38
39 if (!(t = strndup(w, l)))
40 return -ENOMEM;
41
42 r = unit_add_dependency_by_name(u, d, t);
43 free(t);
44
45 if (r < 0)
46 return r;
47 }
48
49 return 0;
50 }
51
52 static int config_parse_names(
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
61 Unit *u = userdata;
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;
74 Unit *other;
75
76 if (!(t = strndup(w, l)))
77 return -ENOMEM;
78
79 other = manager_get_unit(u->meta.manager, t);
80
81 if (other) {
82
83 if (other != u) {
84
85 if (other->meta.load_state != UNIT_STUB) {
86 free(t);
87 return -EEXIST;
88 }
89
90 if ((r = unit_merge(u, other)) < 0) {
91 free(t);
92 return r;
93 }
94 }
95
96 } else {
97 if ((r = unit_add_name(u, t)) < 0) {
98 free(t);
99 return r;
100 }
101 }
102
103 free(t);
104 }
105
106 return 0;
107 }
108
109 static 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
118 int r;
119 SocketPort *p;
120 Socket *s;
121
122 assert(filename);
123 assert(lvalue);
124 assert(rvalue);
125 assert(data);
126
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 }
161 }
162
163 p->fd = -1;
164 LIST_PREPEND(SocketPort, port, s->ports, p);
165
166 return 0;
167 }
168
169 static int config_parse_socket_bind(
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
178 int r;
179 Socket *s;
180
181 assert(filename);
182 assert(lvalue);
183 assert(rvalue);
184 assert(data);
185
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;
191 }
192
193 s->bind_ipv6_only = r ? SOCKET_ADDRESS_IPV6_ONLY : SOCKET_ADDRESS_BOTH;
194
195 return 0;
196 }
197
198 static 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
207 ExecContext *c = data;
208 int priority, r;
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
225 c->nice = priority;
226 c->nice_set = false;
227
228 return 0;
229 }
230
231 static 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
240 ExecContext *c = data;
241 int oa, r;
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
258 c->oom_adjust = oa;
259 c->oom_adjust_set = true;
260
261 return 0;
262 }
263
264 static 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
298 static 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
326 k = 0;
327 FOREACH_WORD_QUOTED(w, l, rvalue, state)
328 if (!(n[k++] = strndup(w, l)))
329 goto fail;
330
331 n[k] = NULL;
332
333 if (!n[0] || !path_is_absolute(n[0])) {
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
355 fail:
356 for (; k > 0; k--)
357 free(n[k-1]);
358 free(n);
359
360 free(nce);
361
362 return -ENOMEM;
363 }
364
365 static 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
397 static 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
425 static 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
455 int 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
484 int 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 }
515
516 int 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
578 int 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
629
630 static int open_follow(char **filename, FILE **_f, Set *names, char **_id) {
631 unsigned c = 0;
632 int fd, r;
633 FILE *f;
634 char *id = NULL;
635
636 assert(filename);
637 assert(*filename);
638 assert(_f);
639 assert(names);
640
641 /* This will update the filename pointer if the loaded file is
642 * reached by a symlink. The old string will be freed. */
643
644 for (;;) {
645 char *target, *k, *name;
646
647 if (c++ >= FOLLOW_MAX)
648 return -ELOOP;
649
650 path_kill_slashes(*filename);
651
652 /* Add the file name we are currently looking at to
653 * the names of this unit */
654 name = file_name_from_path(*filename);
655 if (!(id = set_get(names, name))) {
656
657 if (!(id = strdup(name)))
658 return -ENOMEM;
659
660 if ((r = set_put(names, id)) < 0) {
661 free(id);
662 return r;
663 }
664 }
665
666 /* Try to open the file name, but don't if its a symlink */
667 if ((fd = open(*filename, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW)) >= 0)
668 break;
669
670 if (errno != ELOOP)
671 return -errno;
672
673 /* Hmm, so this is a symlink. Let's read the name, and follow it manually */
674 if ((r = readlink_malloc(*filename, &target)) < 0)
675 return r;
676
677 k = file_in_same_dir(*filename, target);
678 free(target);
679
680 if (!k)
681 return -ENOMEM;
682
683 free(*filename);
684 *filename = k;
685 }
686
687 if (!(f = fdopen(fd, "r"))) {
688 r = -errno;
689 assert(close_nointr(fd) == 0);
690 return r;
691 }
692
693 *_f = f;
694 *_id = id;
695 return 0;
696 }
697
698 static int load_from_path(Unit *u, const char *path) {
699
700 static const char* const section_table[_UNIT_TYPE_MAX] = {
701 [UNIT_SERVICE] = "Service",
702 [UNIT_TIMER] = "Timer",
703 [UNIT_SOCKET] = "Socket",
704 [UNIT_TARGET] = "Target",
705 [UNIT_DEVICE] = "Device",
706 [UNIT_MOUNT] = "Mount",
707 [UNIT_AUTOMOUNT] = "Automount",
708 [UNIT_SNAPSHOT] = "Snapshot"
709 };
710
711 #define EXEC_CONTEXT_CONFIG_ITEMS(context, section) \
712 { "Directory", config_parse_path, &(context).directory, section }, \
713 { "User", config_parse_string, &(context).user, section }, \
714 { "Group", config_parse_string, &(context).group, section }, \
715 { "SupplementaryGroups", config_parse_strv, &(context).supplementary_groups, section }, \
716 { "Nice", config_parse_nice, &(context), section }, \
717 { "OOMAdjust", config_parse_oom_adjust, &(context), section }, \
718 { "UMask", config_parse_umask, &(context).umask, section }, \
719 { "Environment", config_parse_strv, &(context).environment, section }, \
720 { "Output", config_parse_output, &(context).output, section }, \
721 { "SyslogIdentifier", config_parse_string, &(context).syslog_identifier, section }, \
722 { "SyslogFacility", config_parse_facility, &(context).syslog_priority, section }, \
723 { "SyslogLevel", config_parse_level, &(context).syslog_priority, section }
724
725 const ConfigItem items[] = {
726 { "Names", config_parse_names, u, "Meta" },
727 { "Description", config_parse_string, &u->meta.description, "Meta" },
728 { "Requires", config_parse_deps, UINT_TO_PTR(UNIT_REQUIRES), "Meta" },
729 { "SoftRequires", config_parse_deps, UINT_TO_PTR(UNIT_SOFT_REQUIRES), "Meta" },
730 { "Wants", config_parse_deps, UINT_TO_PTR(UNIT_WANTS), "Meta" },
731 { "Requisite", config_parse_deps, UINT_TO_PTR(UNIT_REQUISITE), "Meta" },
732 { "SoftRequisite", config_parse_deps, UINT_TO_PTR(UNIT_SOFT_REQUISITE), "Meta" },
733 { "Conflicts", config_parse_deps, UINT_TO_PTR(UNIT_CONFLICTS), "Meta" },
734 { "Before", config_parse_deps, UINT_TO_PTR(UNIT_BEFORE), "Meta" },
735 { "After", config_parse_deps, UINT_TO_PTR(UNIT_AFTER), "Meta" },
736
737 { "PIDFile", config_parse_path, &u->service.pid_file, "Service" },
738 { "ExecStartPre", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START_PRE, "Service" },
739 { "ExecStart", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START, "Service" },
740 { "ExecStartPost", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START_POST, "Service" },
741 { "ExecReload", config_parse_exec, u->service.exec_command+SERVICE_EXEC_RELOAD, "Service" },
742 { "ExecStop", config_parse_exec, u->service.exec_command+SERVICE_EXEC_STOP, "Service" },
743 { "ExecStopPost", config_parse_exec, u->service.exec_command+SERVICE_EXEC_STOP_POST, "Service" },
744 { "RestartSec", config_parse_usec, &u->service.restart_usec, "Service" },
745 { "TimeoutSec", config_parse_usec, &u->service.timeout_usec, "Service" },
746 { "Type", config_parse_service_type, &u->service, "Service" },
747 { "Restart", config_parse_service_restart, &u->service, "Service" },
748 EXEC_CONTEXT_CONFIG_ITEMS(u->service.exec_context, "Service"),
749
750 { "ListenStream", config_parse_listen, &u->socket, "Socket" },
751 { "ListenDatagram", config_parse_listen, &u->socket, "Socket" },
752 { "ListenSequentialPacket", config_parse_listen, &u->socket, "Socket" },
753 { "ListenFIFO", config_parse_listen, &u->socket, "Socket" },
754 { "BindIPv6Only", config_parse_socket_bind, &u->socket, "Socket" },
755 { "Backlog", config_parse_unsigned, &u->socket.backlog, "Socket" },
756 { "BindToDevice", config_parse_bindtodevice, &u->socket, "Socket" },
757 { "ExecStartPre", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_START_PRE, "Socket" },
758 { "ExecStartPost", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_START_POST, "Socket" },
759 { "ExecStopPre", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_STOP_PRE, "Socket" },
760 { "ExecStopPost", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_STOP_POST, "Socket" },
761 EXEC_CONTEXT_CONFIG_ITEMS(u->socket.exec_context, "Socket"),
762
763 EXEC_CONTEXT_CONFIG_ITEMS(u->automount.exec_context, "Automount"),
764
765 { NULL, NULL, NULL, NULL }
766 };
767
768 #undef EXEC_CONTEXT_CONFIG_ITEMS
769
770 const char *sections[3];
771 char *k;
772 int r;
773 Set *symlink_names;
774 FILE *f;
775 char *filename, *id;
776
777 sections[0] = "Meta";
778 sections[1] = section_table[u->meta.type];
779 sections[2] = NULL;
780
781 if (!(symlink_names = set_new(string_hash_func, string_compare_func)))
782 return -ENOMEM;
783
784 /* Instead of opening the path right away, we manually
785 * follow all symlinks and add their name to our unit
786 * name set while doing so */
787 if (!(filename = path_make_absolute(path, unit_path()))) {
788 r = -ENOMEM;
789 goto finish;
790 }
791
792 if ((r = open_follow(&filename, &f, symlink_names, &id)) < 0) {
793 if (r == -ENOENT)
794 r = 0; /* returning 0 means: no suitable config file found */
795
796 goto finish;
797 }
798
799 /* Now, parse the file contents */
800 r = config_parse(filename, f, sections, items, u);
801 if (r < 0)
802 goto finish;
803
804 /* Let's try to add in all symlink names we found */
805 while ((k = set_steal_first(symlink_names))) {
806 if ((r = unit_add_name(u, k)) < 0)
807 goto finish;
808
809
810 if (id == k)
811 unit_choose_id(u, id);
812 free(k);
813 }
814
815
816 free(u->meta.load_path);
817 u->meta.load_path = filename;
818 filename = NULL;
819
820 r = 1; /* returning 1 means: suitable config file found and loaded */
821
822 finish:
823 while ((k = set_steal_first(symlink_names)))
824 free(k);
825 set_free(symlink_names);
826 free(filename);
827
828 return r;
829 }
830
831 int unit_load_fragment(Unit *u) {
832 int r = 0;
833 ExecContext *c;
834
835 assert(u);
836 assert(u->meta.load_state == UNIT_STUB);
837
838 if (u->meta.load_path)
839 r = load_from_path(u, u->meta.load_path);
840 else {
841 Iterator i;
842 char *t;
843
844 /* Try to find a name we can load this with */
845 SET_FOREACH(t, u->meta.names, i)
846 if ((r = load_from_path(u, t)) != 0)
847 return r;
848 }
849
850 if (u->meta.type == UNIT_SOCKET)
851 c = &u->socket.exec_context;
852 else if (u->meta.type == UNIT_SERVICE)
853 c = &u->service.exec_context;
854 else
855 c = NULL;
856
857 if (r >= 0 && c &&
858 (c->output == EXEC_KERNEL || c->output == EXEC_SYSLOG)) {
859 int k;
860
861 /* If syslog or kernel logging is requested, make sure
862 * our own logging daemon is run first. */
863
864 if ((k = unit_add_dependency_by_name(u, UNIT_AFTER, SPECIAL_LOGGER_SOCKET)) < 0)
865 return k;
866
867 if ((k = unit_add_dependency_by_name(u, UNIT_REQUIRES, SPECIAL_LOGGER_SOCKET)) < 0)
868 return k;
869 }
870
871 return r;
872 }