chiark / gitweb /
5e0637de65ed42120a76b80ce367cfae3ff7d74f
[elogind.git] / src / load-fragment.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
2
3 /***
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
22 #include <linux/oom.h>
23 #include <assert.h>
24 #include <errno.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <fcntl.h>
28 #include <sched.h>
29 #include <sys/prctl.h>
30 #include <sys/mount.h>
31 #include <linux/fs.h>
32
33 #include "unit.h"
34 #include "strv.h"
35 #include "conf-parser.h"
36 #include "load-fragment.h"
37 #include "log.h"
38 #include "ioprio.h"
39 #include "securebits.h"
40 #include "missing.h"
41 #include "unit-name.h"
42
43 #define DEFINE_CONFIG_PARSE_ENUM(function,name,type,msg)                \
44         static int function(                                            \
45                         const char *filename,                           \
46                         unsigned line,                                  \
47                         const char *section,                            \
48                         const char *lvalue,                             \
49                         const char *rvalue,                             \
50                         void *data,                                     \
51                         void *userdata) {                               \
52                                                                         \
53                 type *i = data, x;                                      \
54                                                                         \
55                 assert(filename);                                       \
56                 assert(lvalue);                                         \
57                 assert(rvalue);                                         \
58                 assert(data);                                           \
59                                                                         \
60                 if ((x = name##_from_string(rvalue)) < 0) {             \
61                         log_error("[%s:%u] " msg ": %s", filename, line, rvalue); \
62                         return -EBADMSG;                                \
63                 }                                                       \
64                                                                         \
65                 *i = x;                                                 \
66                                                                         \
67                 return 0;                                               \
68         }
69
70 static int config_parse_deps(
71                 const char *filename,
72                 unsigned line,
73                 const char *section,
74                 const char *lvalue,
75                 const char *rvalue,
76                 void *data,
77                 void *userdata) {
78
79         UnitDependency d = PTR_TO_UINT(data);
80         Unit *u = userdata;
81         char *w;
82         size_t l;
83         char *state;
84
85         assert(filename);
86         assert(lvalue);
87         assert(rvalue);
88
89         FOREACH_WORD(w, l, rvalue, state) {
90                 char *t, *k;
91                 int r;
92
93                 if (!(t = strndup(w, l)))
94                         return -ENOMEM;
95
96                 k = unit_name_printf(u, t);
97                 free(t);
98
99                 if (!k)
100                         return -ENOMEM;
101
102                 r = unit_add_dependency_by_name(u, d, k, NULL, true);
103                 free(k);
104
105                 if (r < 0)
106                         return r;
107         }
108
109         return 0;
110 }
111
112 static int config_parse_names(
113                 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
121         Unit *u = userdata;
122         char *w;
123         size_t l;
124         char *state;
125
126         assert(filename);
127         assert(lvalue);
128         assert(rvalue);
129         assert(data);
130
131         FOREACH_WORD(w, l, rvalue, state) {
132                 char *t, *k;
133                 int r;
134
135                 if (!(t = strndup(w, l)))
136                         return -ENOMEM;
137
138                 k = unit_name_printf(u, t);
139                 free(t);
140
141                 if (!k)
142                         return -ENOMEM;
143
144                 r = unit_merge_by_name(u, k);
145                 free(k);
146
147                 if (r < 0)
148                         return r;
149         }
150
151         return 0;
152 }
153
154 static int config_parse_description(
155                 const char *filename,
156                 unsigned line,
157                 const char *section,
158                 const char *lvalue,
159                 const char *rvalue,
160                 void *data,
161                 void *userdata) {
162
163         Unit *u = userdata;
164         char *k;
165
166         assert(filename);
167         assert(lvalue);
168         assert(rvalue);
169         assert(data);
170
171         if (!(k = unit_full_printf(u, rvalue)))
172                 return -ENOMEM;
173
174         free(u->meta.description);
175
176         if (*k)
177                 u->meta.description = k;
178         else {
179                 free(k);
180                 u->meta.description = NULL;
181         }
182
183         return 0;
184 }
185
186 static int config_parse_listen(
187                 const char *filename,
188                 unsigned line,
189                 const char *section,
190                 const char *lvalue,
191                 const char *rvalue,
192                 void *data,
193                 void *userdata) {
194
195         int r;
196         SocketPort *p;
197         Socket *s;
198
199         assert(filename);
200         assert(lvalue);
201         assert(rvalue);
202         assert(data);
203
204         s = (Socket*) data;
205
206         if (!(p = new0(SocketPort, 1)))
207                 return -ENOMEM;
208
209         if (streq(lvalue, "ListenFIFO")) {
210                 p->type = SOCKET_FIFO;
211
212                 if (!(p->path = strdup(rvalue))) {
213                         free(p);
214                         return -ENOMEM;
215                 }
216         } else {
217                 p->type = SOCKET_SOCKET;
218
219                 if ((r = socket_address_parse(&p->address, rvalue)) < 0) {
220                         log_error("[%s:%u] Failed to parse address value: %s", filename, line, rvalue);
221                         free(p);
222                         return r;
223                 }
224
225                 if (streq(lvalue, "ListenStream"))
226                         p->address.type = SOCK_STREAM;
227                 else if (streq(lvalue, "ListenDatagram"))
228                         p->address.type = SOCK_DGRAM;
229                 else {
230                         assert(streq(lvalue, "ListenSequentialPacket"));
231                         p->address.type = SOCK_SEQPACKET;
232                 }
233
234                 if (socket_address_family(&p->address) != AF_LOCAL && p->address.type == SOCK_SEQPACKET) {
235                         free(p);
236                         return -EPROTONOSUPPORT;
237                 }
238         }
239
240         p->fd = -1;
241         LIST_PREPEND(SocketPort, port, s->ports, p);
242
243         return 0;
244 }
245
246 static int config_parse_socket_bind(
247                 const char *filename,
248                 unsigned line,
249                 const char *section,
250                 const char *lvalue,
251                 const char *rvalue,
252                 void *data,
253                 void *userdata) {
254
255         int r;
256         Socket *s;
257
258         assert(filename);
259         assert(lvalue);
260         assert(rvalue);
261         assert(data);
262
263         s = (Socket*) data;
264
265         if ((r = parse_boolean(rvalue)) < 0) {
266                 log_error("[%s:%u] Failed to parse bind IPv6 only value: %s", filename, line, rvalue);
267                 return r;
268         }
269
270         s->bind_ipv6_only = r ? SOCKET_ADDRESS_IPV6_ONLY : SOCKET_ADDRESS_BOTH;
271
272         return 0;
273 }
274
275 static int config_parse_nice(
276                 const char *filename,
277                 unsigned line,
278                 const char *section,
279                 const char *lvalue,
280                 const char *rvalue,
281                 void *data,
282                 void *userdata) {
283
284         ExecContext *c = data;
285         int priority, r;
286
287         assert(filename);
288         assert(lvalue);
289         assert(rvalue);
290         assert(data);
291
292         if ((r = safe_atoi(rvalue, &priority)) < 0) {
293                 log_error("[%s:%u] Failed to parse nice priority: %s", filename, line, rvalue);
294                 return r;
295         }
296
297         if (priority < PRIO_MIN || priority >= PRIO_MAX) {
298                 log_error("[%s:%u] Nice priority out of range: %s", filename, line, rvalue);
299                 return -ERANGE;
300         }
301
302         c->nice = priority;
303         c->nice_set = false;
304
305         return 0;
306 }
307
308 static int config_parse_oom_adjust(
309                 const char *filename,
310                 unsigned line,
311                 const char *section,
312                 const char *lvalue,
313                 const char *rvalue,
314                 void *data,
315                 void *userdata) {
316
317         ExecContext *c = data;
318         int oa, r;
319
320         assert(filename);
321         assert(lvalue);
322         assert(rvalue);
323         assert(data);
324
325         if ((r = safe_atoi(rvalue, &oa)) < 0) {
326                 log_error("[%s:%u] Failed to parse OOM adjust value: %s", filename, line, rvalue);
327                 return r;
328         }
329
330         if (oa < OOM_DISABLE || oa > OOM_ADJUST_MAX) {
331                 log_error("[%s:%u] OOM adjust value out of range: %s", filename, line, rvalue);
332                 return -ERANGE;
333         }
334
335         c->oom_adjust = oa;
336         c->oom_adjust_set = true;
337
338         return 0;
339 }
340
341 static int config_parse_mode(
342                 const char *filename,
343                 unsigned line,
344                 const char *section,
345                 const char *lvalue,
346                 const char *rvalue,
347                 void *data,
348                 void *userdata) {
349
350         mode_t *m = data;
351         long l;
352         char *x = NULL;
353
354         assert(filename);
355         assert(lvalue);
356         assert(rvalue);
357         assert(data);
358
359         errno = 0;
360         l = strtol(rvalue, &x, 8);
361         if (!x || *x || errno) {
362                 log_error("[%s:%u] Failed to parse mode value: %s", filename, line, rvalue);
363                 return errno ? -errno : -EINVAL;
364         }
365
366         if (l < 0000 || l > 07777) {
367                 log_error("[%s:%u] mode value out of range: %s", filename, line, rvalue);
368                 return -ERANGE;
369         }
370
371         *m = (mode_t) l;
372         return 0;
373 }
374
375 static int config_parse_exec(
376                 const char *filename,
377                 unsigned line,
378                 const char *section,
379                 const char *lvalue,
380                 const char *rvalue,
381                 void *data,
382                 void *userdata) {
383
384         ExecCommand **e = data, *nce = NULL;
385         char **n;
386         char *w;
387         unsigned k;
388         size_t l;
389         char *state, *path = NULL;
390         bool honour_argv0, write_to_path;
391
392         assert(filename);
393         assert(lvalue);
394         assert(rvalue);
395         assert(data);
396
397         /* We accept an absolute path as first argument, or
398          * alternatively an absolute prefixed with @ to allow
399          * overriding of argv[0]. */
400
401         honour_argv0 = rvalue[0] == '@';
402
403         if (rvalue[honour_argv0 ? 1 : 0] != '/') {
404                 log_error("[%s:%u] Invalid executable path in command line: %s", filename, line, rvalue);
405                 return -EINVAL;
406         }
407
408         k = 0;
409         FOREACH_WORD_QUOTED(w, l, rvalue, state)
410                 k++;
411
412         if (!(n = new(char*, k + (honour_argv0 ? 0 : 1))))
413                 return -ENOMEM;
414
415         k = 0;
416         write_to_path = honour_argv0;
417         FOREACH_WORD_QUOTED(w, l, rvalue, state) {
418                 if (write_to_path) {
419                         if (!(path = strndup(w+1, l-1)))
420                                 goto fail;
421                         write_to_path = false;
422                 } else {
423                         if (!(n[k++] = strndup(w, l)))
424                                 goto fail;
425                 }
426         }
427
428         n[k] = NULL;
429
430         if (!n[0]) {
431                 log_error("[%s:%u] Invalid command line: %s", filename, line, rvalue);
432                 strv_free(n);
433                 return -EINVAL;
434         }
435
436         if (!path)
437                 if (!(path = strdup(n[0])))
438                         goto fail;
439
440         assert(path_is_absolute(path));
441
442         if (!(nce = new0(ExecCommand, 1)))
443                 goto fail;
444
445         nce->argv = n;
446         nce->path = path;
447
448         exec_command_append_list(e, nce);
449
450         return 0;
451
452 fail:
453         n[k] = NULL;
454         strv_free(n);
455         free(path);
456         free(nce);
457
458         return -ENOMEM;
459 }
460
461 static int config_parse_usec(
462                 const char *filename,
463                 unsigned line,
464                 const char *section,
465                 const char *lvalue,
466                 const char *rvalue,
467                 void *data,
468                 void *userdata) {
469
470         usec_t *usec = data;
471         int r;
472
473         assert(filename);
474         assert(lvalue);
475         assert(rvalue);
476         assert(data);
477
478         if ((r = parse_usec(rvalue, usec)) < 0) {
479                 log_error("[%s:%u] Failed to parse time value: %s", filename, line, rvalue);
480                 return r;
481         }
482
483         return 0;
484 }
485
486 DEFINE_CONFIG_PARSE_ENUM(config_parse_service_type, service_type, ServiceType, "Failed to parse service type");
487 DEFINE_CONFIG_PARSE_ENUM(config_parse_service_restart, service_restart, ServiceRestart, "Failed to parse service restart specifier");
488
489 static int config_parse_bindtodevice(
490                 const char *filename,
491                 unsigned line,
492                 const char *section,
493                 const char *lvalue,
494                 const char *rvalue,
495                 void *data,
496                 void *userdata) {
497
498         Socket *s = data;
499         char *n;
500
501         assert(filename);
502         assert(lvalue);
503         assert(rvalue);
504         assert(data);
505
506         if (rvalue[0] && !streq(rvalue, "*")) {
507                 if (!(n = strdup(rvalue)))
508                         return -ENOMEM;
509         } else
510                 n = NULL;
511
512         free(s->bind_to_device);
513         s->bind_to_device = n;
514
515         return 0;
516 }
517
518 DEFINE_CONFIG_PARSE_ENUM(config_parse_output, exec_output, ExecOutput, "Failed to parse output specifier");
519 DEFINE_CONFIG_PARSE_ENUM(config_parse_input, exec_input, ExecInput, "Failed to parse input specifier");
520
521 static int config_parse_facility(
522                 const char *filename,
523                 unsigned line,
524                 const char *section,
525                 const char *lvalue,
526                 const char *rvalue,
527                 void *data,
528                 void *userdata) {
529
530
531         int *o = data, x;
532
533         assert(filename);
534         assert(lvalue);
535         assert(rvalue);
536         assert(data);
537
538         if ((x = log_facility_from_string(rvalue)) < 0) {
539                 log_error("[%s:%u] Failed to parse log facility: %s", filename, line, rvalue);
540                 return -EBADMSG;
541         }
542
543         *o = LOG_MAKEPRI(x, LOG_PRI(*o));
544
545         return 0;
546 }
547
548 static int config_parse_level(
549                 const char *filename,
550                 unsigned line,
551                 const char *section,
552                 const char *lvalue,
553                 const char *rvalue,
554                 void *data,
555                 void *userdata) {
556
557
558         int *o = data, x;
559
560         assert(filename);
561         assert(lvalue);
562         assert(rvalue);
563         assert(data);
564
565         if ((x = log_level_from_string(rvalue)) < 0) {
566                 log_error("[%s:%u] Failed to parse log level: %s", filename, line, rvalue);
567                 return -EBADMSG;
568         }
569
570         *o = LOG_MAKEPRI(LOG_FAC(*o), x);
571         return 0;
572 }
573
574 static int config_parse_io_class(
575                 const char *filename,
576                 unsigned line,
577                 const char *section,
578                 const char *lvalue,
579                 const char *rvalue,
580                 void *data,
581                 void *userdata) {
582
583         ExecContext *c = data;
584         int x;
585
586         assert(filename);
587         assert(lvalue);
588         assert(rvalue);
589         assert(data);
590
591         if ((x = ioprio_class_from_string(rvalue)) < 0) {
592                 log_error("[%s:%u] Failed to parse IO scheduling class: %s", filename, line, rvalue);
593                 return -EBADMSG;
594         }
595
596         c->ioprio = IOPRIO_PRIO_VALUE(x, IOPRIO_PRIO_DATA(c->ioprio));
597         c->ioprio_set = true;
598
599         return 0;
600 }
601
602 static int config_parse_io_priority(
603                 const char *filename,
604                 unsigned line,
605                 const char *section,
606                 const char *lvalue,
607                 const char *rvalue,
608                 void *data,
609                 void *userdata) {
610
611         ExecContext *c = data;
612         int i;
613
614         assert(filename);
615         assert(lvalue);
616         assert(rvalue);
617         assert(data);
618
619         if (safe_atoi(rvalue, &i) < 0 || i < 0 || i >= IOPRIO_BE_NR) {
620                 log_error("[%s:%u] Failed to parse io priority: %s", filename, line, rvalue);
621                 return -EBADMSG;
622         }
623
624         c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_PRIO_CLASS(c->ioprio), i);
625         c->ioprio_set = true;
626
627         return 0;
628 }
629
630 static int config_parse_cpu_sched_policy(
631                 const char *filename,
632                 unsigned line,
633                 const char *section,
634                 const char *lvalue,
635                 const char *rvalue,
636                 void *data,
637                 void *userdata) {
638
639
640         ExecContext *c = data;
641         int x;
642
643         assert(filename);
644         assert(lvalue);
645         assert(rvalue);
646         assert(data);
647
648         if ((x = sched_policy_from_string(rvalue)) < 0) {
649                 log_error("[%s:%u] Failed to parse CPU scheduling policy: %s", filename, line, rvalue);
650                 return -EBADMSG;
651         }
652
653         c->cpu_sched_policy = x;
654         c->cpu_sched_set = true;
655
656         return 0;
657 }
658
659 static int config_parse_cpu_sched_prio(
660                 const char *filename,
661                 unsigned line,
662                 const char *section,
663                 const char *lvalue,
664                 const char *rvalue,
665                 void *data,
666                 void *userdata) {
667
668         ExecContext *c = data;
669         int i;
670
671         assert(filename);
672         assert(lvalue);
673         assert(rvalue);
674         assert(data);
675
676         /* On Linux RR/FIFO have the same range */
677         if (safe_atoi(rvalue, &i) < 0 || i < sched_get_priority_min(SCHED_RR) || i > sched_get_priority_max(SCHED_RR)) {
678                 log_error("[%s:%u] Failed to parse CPU scheduling priority: %s", filename, line, rvalue);
679                 return -EBADMSG;
680         }
681
682         c->cpu_sched_priority = i;
683         c->cpu_sched_set = true;
684
685         return 0;
686 }
687
688 static int config_parse_cpu_affinity(
689                 const char *filename,
690                 unsigned line,
691                 const char *section,
692                 const char *lvalue,
693                 const char *rvalue,
694                 void *data,
695                 void *userdata) {
696
697         ExecContext *c = data;
698         char *w;
699         size_t l;
700         char *state;
701
702         assert(filename);
703         assert(lvalue);
704         assert(rvalue);
705         assert(data);
706
707         FOREACH_WORD(w, l, rvalue, state) {
708                 char *t;
709                 int r;
710                 unsigned cpu;
711
712                 if (!(t = strndup(w, l)))
713                         return -ENOMEM;
714
715                 r = safe_atou(t, &cpu);
716                 free(t);
717
718                 if (r < 0 || cpu >= CPU_SETSIZE) {
719                         log_error("[%s:%u] Failed to parse CPU affinity: %s", filename, line, rvalue);
720                         return -EBADMSG;
721                 }
722
723                 CPU_SET(cpu, &c->cpu_affinity);
724         }
725
726         c->cpu_affinity_set = true;
727
728         return 0;
729 }
730
731 static int config_parse_capabilities(
732                 const char *filename,
733                 unsigned line,
734                 const char *section,
735                 const char *lvalue,
736                 const char *rvalue,
737                 void *data,
738                 void *userdata) {
739
740         ExecContext *c = data;
741         cap_t cap;
742
743         assert(filename);
744         assert(lvalue);
745         assert(rvalue);
746         assert(data);
747
748         if (!(cap = cap_from_text(rvalue))) {
749                 if (errno == ENOMEM)
750                         return -ENOMEM;
751
752                 log_error("[%s:%u] Failed to parse capabilities: %s", filename, line, rvalue);
753                 return -EBADMSG;
754         }
755
756         if (c->capabilities)
757                 cap_free(c->capabilities);
758         c->capabilities = cap;
759
760         return 0;
761 }
762
763 static int config_parse_secure_bits(
764                 const char *filename,
765                 unsigned line,
766                 const char *section,
767                 const char *lvalue,
768                 const char *rvalue,
769                 void *data,
770                 void *userdata) {
771
772         ExecContext *c = data;
773         char *w;
774         size_t l;
775         char *state;
776
777         assert(filename);
778         assert(lvalue);
779         assert(rvalue);
780         assert(data);
781
782         FOREACH_WORD(w, l, rvalue, state) {
783                 if (first_word(w, "keep-caps"))
784                         c->secure_bits |= SECURE_KEEP_CAPS;
785                 else if (first_word(w, "keep-caps-locked"))
786                         c->secure_bits |= SECURE_KEEP_CAPS_LOCKED;
787                 else if (first_word(w, "no-setuid-fixup"))
788                         c->secure_bits |= SECURE_NO_SETUID_FIXUP;
789                 else if (first_word(w, "no-setuid-fixup-locked"))
790                         c->secure_bits |= SECURE_NO_SETUID_FIXUP_LOCKED;
791                 else if (first_word(w, "noroot"))
792                         c->secure_bits |= SECURE_NOROOT;
793                 else if (first_word(w, "noroot-locked"))
794                         c->secure_bits |= SECURE_NOROOT_LOCKED;
795                 else {
796                         log_error("[%s:%u] Failed to parse secure bits: %s", filename, line, rvalue);
797                         return -EBADMSG;
798                 }
799         }
800
801         return 0;
802 }
803
804 static int config_parse_bounding_set(
805                 const char *filename,
806                 unsigned line,
807                 const char *section,
808                 const char *lvalue,
809                 const char *rvalue,
810                 void *data,
811                 void *userdata) {
812
813         ExecContext *c = data;
814         char *w;
815         size_t l;
816         char *state;
817
818         assert(filename);
819         assert(lvalue);
820         assert(rvalue);
821         assert(data);
822
823         FOREACH_WORD(w, l, rvalue, state) {
824                 char *t;
825                 int r;
826                 cap_value_t cap;
827
828                 if (!(t = strndup(w, l)))
829                         return -ENOMEM;
830
831                 r = cap_from_name(t, &cap);
832                 free(t);
833
834                 if (r < 0) {
835                         log_error("[%s:%u] Failed to parse capability bounding set: %s", filename, line, rvalue);
836                         return -EBADMSG;
837                 }
838
839                 c->capability_bounding_set_drop |= 1 << cap;
840         }
841
842         return 0;
843 }
844
845 static int config_parse_timer_slack_ns(
846                 const char *filename,
847                 unsigned line,
848                 const char *section,
849                 const char *lvalue,
850                 const char *rvalue,
851                 void *data,
852                 void *userdata) {
853
854         ExecContext *c = data;
855         unsigned long u;
856         int r;
857
858         assert(filename);
859         assert(lvalue);
860         assert(rvalue);
861         assert(data);
862
863         if ((r = safe_atolu(rvalue, &u)) < 0) {
864                 log_error("[%s:%u] Failed to parse time slack value: %s", filename, line, rvalue);
865                 return r;
866         }
867
868         c->timer_slack_ns = u;
869
870         return 0;
871 }
872
873 static int config_parse_limit(
874                 const char *filename,
875                 unsigned line,
876                 const char *section,
877                 const char *lvalue,
878                 const char *rvalue,
879                 void *data,
880                 void *userdata) {
881
882         struct rlimit **rl = data;
883         unsigned long long u;
884         int r;
885
886         assert(filename);
887         assert(lvalue);
888         assert(rvalue);
889         assert(data);
890
891         if ((r = safe_atollu(rvalue, &u)) < 0) {
892                 log_error("[%s:%u] Failed to parse resource value: %s", filename, line, rvalue);
893                 return r;
894         }
895
896         if (!*rl)
897                 if (!(*rl = new(struct rlimit, 1)))
898                         return -ENOMEM;
899
900         (*rl)->rlim_cur = (*rl)->rlim_max = (rlim_t) u;
901         return 0;
902 }
903
904 static int config_parse_cgroup(
905                 const char *filename,
906                 unsigned line,
907                 const char *section,
908                 const char *lvalue,
909                 const char *rvalue,
910                 void *data,
911                 void *userdata) {
912
913         Unit *u = userdata;
914         char *w;
915         size_t l;
916         char *state;
917
918         FOREACH_WORD(w, l, rvalue, state) {
919                 char *t;
920                 int r;
921
922                 if (!(t = strndup(w, l)))
923                         return -ENOMEM;
924
925                 r = unit_add_cgroup_from_text(u, t);
926                 free(t);
927
928                 if (r < 0)
929                         return r;
930         }
931
932         return 0;
933 }
934
935 static int config_parse_sysv_priority(
936                 const char *filename,
937                 unsigned line,
938                 const char *section,
939                 const char *lvalue,
940                 const char *rvalue,
941                 void *data,
942                 void *userdata) {
943
944         int *priority = data;
945         int r, i;
946
947         assert(filename);
948         assert(lvalue);
949         assert(rvalue);
950         assert(data);
951
952         if ((r = safe_atoi(rvalue, &i)) < 0 || i < 0) {
953                 log_error("[%s:%u] Failed to parse SysV start priority: %s", filename, line, rvalue);
954                 return r;
955         }
956
957         *priority = (int) i;
958         return 0;
959 }
960
961 DEFINE_CONFIG_PARSE_ENUM(config_parse_kill_mode, kill_mode, KillMode, "Failed to parse kill mode");
962
963 static int config_parse_mount_flags(
964                 const char *filename,
965                 unsigned line,
966                 const char *section,
967                 const char *lvalue,
968                 const char *rvalue,
969                 void *data,
970                 void *userdata) {
971
972         ExecContext *c = data;
973         char *w;
974         size_t l;
975         char *state;
976         unsigned long flags = 0;
977
978         assert(filename);
979         assert(lvalue);
980         assert(rvalue);
981         assert(data);
982
983         FOREACH_WORD(w, l, rvalue, state) {
984                 if (strncmp(w, "shared", l) == 0)
985                         flags |= MS_SHARED;
986                 else if (strncmp(w, "slave", l) == 0)
987                         flags |= MS_SLAVE;
988                 else if (strncmp(w, "private", l) == 0)
989                         flags |= MS_PRIVATE;
990                 else {
991                         log_error("[%s:%u] Failed to parse mount flags: %s", filename, line, rvalue);
992                         return -EINVAL;
993                 }
994         }
995
996         c->mount_flags = flags;
997         return 0;
998 }
999
1000 #define FOLLOW_MAX 8
1001
1002 static int open_follow(char **filename, FILE **_f, Set *names, char **_final) {
1003         unsigned c = 0;
1004         int fd, r;
1005         FILE *f;
1006         char *id = NULL;
1007
1008         assert(filename);
1009         assert(*filename);
1010         assert(_f);
1011         assert(names);
1012
1013         /* This will update the filename pointer if the loaded file is
1014          * reached by a symlink. The old string will be freed. */
1015
1016         for (;;) {
1017                 char *target, *k, *name;
1018
1019                 if (c++ >= FOLLOW_MAX)
1020                         return -ELOOP;
1021
1022                 path_kill_slashes(*filename);
1023
1024                 /* Add the file name we are currently looking at to
1025                  * the names of this unit */
1026                 name = file_name_from_path(*filename);
1027                 if (!(id = set_get(names, name))) {
1028
1029                         if (!(id = strdup(name)))
1030                                 return -ENOMEM;
1031
1032                         if ((r = set_put(names, id)) < 0) {
1033                                 free(id);
1034                                 return r;
1035                         }
1036                 }
1037
1038                 /* Try to open the file name, but don't if its a symlink */
1039                 if ((fd = open(*filename, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW)) >= 0)
1040                         break;
1041
1042                 if (errno != ELOOP)
1043                         return -errno;
1044
1045                 /* Hmm, so this is a symlink. Let's read the name, and follow it manually */
1046                 if ((r = readlink_malloc(*filename, &target)) < 0)
1047                         return r;
1048
1049                 k = file_in_same_dir(*filename, target);
1050                 free(target);
1051
1052                 if (!k)
1053                         return -ENOMEM;
1054
1055                 free(*filename);
1056                 *filename = k;
1057         }
1058
1059         if (!(f = fdopen(fd, "r"))) {
1060                 r = -errno;
1061                 close_nointr_nofail(fd);
1062                 return r;
1063         }
1064
1065         *_f = f;
1066         *_final = id;
1067         return 0;
1068 }
1069
1070 static int merge_by_names(Unit **u, Set *names, const char *id) {
1071         char *k;
1072         int r;
1073
1074         assert(u);
1075         assert(*u);
1076         assert(names);
1077
1078         /* Let's try to add in all symlink names we found */
1079         while ((k = set_steal_first(names))) {
1080
1081                 /* First try to merge in the other name into our
1082                  * unit */
1083                 if ((r = unit_merge_by_name(*u, k)) < 0) {
1084                         Unit *other;
1085
1086                         /* Hmm, we couldn't merge the other unit into
1087                          * ours? Then let's try it the other way
1088                          * round */
1089
1090                         other = manager_get_unit((*u)->meta.manager, k);
1091                         free(k);
1092
1093                         if (other)
1094                                 if ((r = unit_merge(other, *u)) >= 0) {
1095                                         *u = other;
1096                                         return merge_by_names(u, names, NULL);
1097                                 }
1098
1099                         return r;
1100                 }
1101
1102                 if (id == k)
1103                         unit_choose_id(*u, id);
1104
1105                 free(k);
1106         }
1107
1108         return 0;
1109 }
1110
1111 static void dump_items(FILE *f, const ConfigItem *items) {
1112         const ConfigItem *i;
1113         const char *prev_section = NULL;
1114         bool not_first = false;
1115
1116         struct {
1117                 ConfigParserCallback callback;
1118                 const char *rvalue;
1119         } table[] = {
1120                 { config_parse_int,              "INTEGER" },
1121                 { config_parse_unsigned,         "UNSIGNED" },
1122                 { config_parse_size,             "SIZE" },
1123                 { config_parse_bool,             "BOOLEAN" },
1124                 { config_parse_string,           "STRING" },
1125                 { config_parse_path,             "PATH" },
1126                 { config_parse_strv,             "STRING [...]" },
1127                 { config_parse_nice,             "NICE" },
1128                 { config_parse_oom_adjust,       "OOMADJUST" },
1129                 { config_parse_io_class,         "IOCLASS" },
1130                 { config_parse_io_priority,      "IOPRIORITY" },
1131                 { config_parse_cpu_sched_policy, "CPUSCHEDPOLICY" },
1132                 { config_parse_cpu_sched_prio,   "CPUSCHEDPRIO" },
1133                 { config_parse_cpu_affinity,     "CPUAFFINITY" },
1134                 { config_parse_mode,             "MODE" },
1135                 { config_parse_output,           "OUTPUT" },
1136                 { config_parse_input,            "INPUT" },
1137                 { config_parse_facility,         "FACILITY" },
1138                 { config_parse_level,            "LEVEL" },
1139                 { config_parse_capabilities,     "CAPABILITIES" },
1140                 { config_parse_secure_bits,      "SECUREBITS" },
1141                 { config_parse_bounding_set,     "BOUNDINGSET" },
1142                 { config_parse_timer_slack_ns,   "TIMERSLACK" },
1143                 { config_parse_limit,            "LIMIT" },
1144                 { config_parse_cgroup,           "CGROUP [...]" },
1145                 { config_parse_deps,             "UNIT [...]" },
1146                 { config_parse_names,            "UNIT [...]" },
1147                 { config_parse_exec,             "PATH [ARGUMENT [...]]" },
1148                 { config_parse_service_type,     "SERVICETYPE" },
1149                 { config_parse_service_restart,  "SERVICERESTART" },
1150                 { config_parse_sysv_priority,    "SYSVPRIORITY" },
1151                 { config_parse_kill_mode,        "KILLMODE" },
1152                 { config_parse_listen,           "SOCKET [...]" },
1153                 { config_parse_socket_bind,      "SOCKETBIND" },
1154                 { config_parse_bindtodevice,     "NETWORKINTERFACE" },
1155                 { config_parse_usec,             "SECONDS" },
1156                 { config_parse_path_strv,        "PATH [...]" },
1157                 { config_parse_mount_flags,      "MOUNTFLAG [...]" },
1158                 { config_parse_description,      "DESCRIPTION" },
1159         };
1160
1161         assert(f);
1162         assert(items);
1163
1164         for (i = items; i->lvalue; i++) {
1165                 unsigned j;
1166                 const char *rvalue = "OTHER";
1167
1168                 if (!streq_ptr(i->section, prev_section)) {
1169                         if (!not_first)
1170                                 not_first = true;
1171                         else
1172                                 fputc('\n', f);
1173
1174                         fprintf(f, "[%s]\n", i->section);
1175                         prev_section = i->section;
1176                 }
1177
1178                 for (j = 0; j < ELEMENTSOF(table); j++)
1179                         if (i->parse == table[j].callback) {
1180                                 rvalue = table[j].rvalue;
1181                                 break;
1182                         }
1183
1184                 fprintf(f, "%s=%s\n", i->lvalue, rvalue);
1185         }
1186 }
1187
1188 static int load_from_path(Unit *u, const char *path) {
1189
1190         static const char* const section_table[_UNIT_TYPE_MAX] = {
1191                 [UNIT_SERVICE]   = "Service",
1192                 [UNIT_TIMER]     = "Timer",
1193                 [UNIT_SOCKET]    = "Socket",
1194                 [UNIT_TARGET]    = "Target",
1195                 [UNIT_DEVICE]    = "Device",
1196                 [UNIT_MOUNT]     = "Mount",
1197                 [UNIT_AUTOMOUNT] = "Automount",
1198                 [UNIT_SNAPSHOT]  = "Snapshot",
1199                 [UNIT_SWAP]      = "Swap"
1200         };
1201
1202 #define EXEC_CONTEXT_CONFIG_ITEMS(context, section) \
1203                 { "WorkingDirectory",       config_parse_path,            &(context).working_directory,                    section   }, \
1204                 { "RootDirectory",          config_parse_path,            &(context).root_directory,                       section   }, \
1205                 { "User",                   config_parse_string,          &(context).user,                                 section   }, \
1206                 { "Group",                  config_parse_string,          &(context).group,                                section   }, \
1207                 { "SupplementaryGroups",    config_parse_strv,            &(context).supplementary_groups,                 section   }, \
1208                 { "Nice",                   config_parse_nice,            &(context),                                      section   }, \
1209                 { "OOMAdjust",              config_parse_oom_adjust,      &(context),                                      section   }, \
1210                 { "IOSchedulingClass",      config_parse_io_class,        &(context),                                      section   }, \
1211                 { "IOSchedulingPriority",   config_parse_io_priority,     &(context),                                      section   }, \
1212                 { "CPUSchedulingPolicy",    config_parse_cpu_sched_policy,&(context),                                      section   }, \
1213                 { "CPUSchedulingPriority",  config_parse_cpu_sched_prio,  &(context),                                      section   }, \
1214                 { "CPUSchedulingResetOnFork", config_parse_bool,          &(context).cpu_sched_reset_on_fork,              section   }, \
1215                 { "CPUAffinity",            config_parse_cpu_affinity,    &(context),                                      section   }, \
1216                 { "UMask",                  config_parse_mode,            &(context).umask,                                section   }, \
1217                 { "Environment",            config_parse_strv,            &(context).environment,                          section   }, \
1218                 { "StandardInput",          config_parse_input,           &(context).std_input,                            section   }, \
1219                 { "StandardOutput",         config_parse_output,          &(context).std_output,                           section   }, \
1220                 { "StandardError",          config_parse_output,          &(context).std_output,                           section   }, \
1221                 { "TTYPath",                config_parse_path,            &(context).tty_path,                             section   }, \
1222                 { "SyslogIdentifier",       config_parse_string,          &(context).syslog_identifier,                    section   }, \
1223                 { "SyslogFacility",         config_parse_facility,        &(context).syslog_priority,                      section   }, \
1224                 { "SyslogLevel",            config_parse_level,           &(context).syslog_priority,                      section   }, \
1225                 { "SyslogNoPrefix",         config_parse_bool,            &(context).syslog_no_prefix,                     section   }, \
1226                 { "Capabilities",           config_parse_capabilities,    &(context),                                      section   }, \
1227                 { "SecureBits",             config_parse_secure_bits,     &(context),                                      section   }, \
1228                 { "CapabilityBoundingSetDrop", config_parse_bounding_set, &(context),                                      section   }, \
1229                 { "TimerSlackNS",           config_parse_timer_slack_ns,  &(context),                                      section   }, \
1230                 { "LimitCPU",               config_parse_limit,           &(context).rlimit[RLIMIT_CPU],                   section   }, \
1231                 { "LimitFSIZE",             config_parse_limit,           &(context).rlimit[RLIMIT_FSIZE],                 section   }, \
1232                 { "LimitDATA",              config_parse_limit,           &(context).rlimit[RLIMIT_DATA],                  section   }, \
1233                 { "LimitSTACK",             config_parse_limit,           &(context).rlimit[RLIMIT_STACK],                 section   }, \
1234                 { "LimitCORE",              config_parse_limit,           &(context).rlimit[RLIMIT_CORE],                  section   }, \
1235                 { "LimitRSS",               config_parse_limit,           &(context).rlimit[RLIMIT_RSS],                   section   }, \
1236                 { "LimitNOFILE",            config_parse_limit,           &(context).rlimit[RLIMIT_NOFILE],                section   }, \
1237                 { "LimitAS",                config_parse_limit,           &(context).rlimit[RLIMIT_AS],                    section   }, \
1238                 { "LimitNPROC",             config_parse_limit,           &(context).rlimit[RLIMIT_NPROC],                 section   }, \
1239                 { "LimitMEMLOCK",           config_parse_limit,           &(context).rlimit[RLIMIT_MEMLOCK],               section   }, \
1240                 { "LimitLOCKS",             config_parse_limit,           &(context).rlimit[RLIMIT_LOCKS],                 section   }, \
1241                 { "LimitSIGPENDING",        config_parse_limit,           &(context).rlimit[RLIMIT_SIGPENDING],            section   }, \
1242                 { "LimitMSGQUEUE",          config_parse_limit,           &(context).rlimit[RLIMIT_MSGQUEUE],              section   }, \
1243                 { "LimitNICE",              config_parse_limit,           &(context).rlimit[RLIMIT_NICE],                  section   }, \
1244                 { "LimitRTPRIO",            config_parse_limit,           &(context).rlimit[RLIMIT_RTPRIO],                section   }, \
1245                 { "LimitRTTIME",            config_parse_limit,           &(context).rlimit[RLIMIT_RTTIME],                section   }, \
1246                 { "ControlGroup",           config_parse_cgroup,          u,                                               section   }, \
1247                 { "ReadWriteDirectories",   config_parse_path_strv,       &(context).read_write_dirs,                      section   }, \
1248                 { "ReadOnlyDirectories",    config_parse_path_strv,       &(context).read_only_dirs,                       section   }, \
1249                 { "InaccessibleDirectories",config_parse_path_strv,       &(context).inaccessible_dirs,                    section   }, \
1250                 { "PrivateTmp",             config_parse_bool,            &(context).private_tmp,                          section   }, \
1251                 { "MountFlags",             config_parse_mount_flags,     &(context),                                      section   }
1252
1253         const ConfigItem items[] = {
1254                 { "Names",                  config_parse_names,           u,                                               "Unit"    },
1255                 { "Description",            config_parse_description,     u,                                               "Unit"    },
1256                 { "Requires",               config_parse_deps,            UINT_TO_PTR(UNIT_REQUIRES),                      "Unit"    },
1257                 { "RequiresOverridable",    config_parse_deps,            UINT_TO_PTR(UNIT_REQUIRES_OVERRIDABLE),          "Unit"    },
1258                 { "Requisite",              config_parse_deps,            UINT_TO_PTR(UNIT_REQUISITE),                     "Unit"    },
1259                 { "RequisiteOverridable",   config_parse_deps,            UINT_TO_PTR(UNIT_REQUISITE_OVERRIDABLE),         "Unit"    },
1260                 { "Wants",                  config_parse_deps,            UINT_TO_PTR(UNIT_WANTS),                         "Unit"    },
1261                 { "Conflicts",              config_parse_deps,            UINT_TO_PTR(UNIT_CONFLICTS),                     "Unit"    },
1262                 { "Before",                 config_parse_deps,            UINT_TO_PTR(UNIT_BEFORE),                        "Unit"    },
1263                 { "After",                  config_parse_deps,            UINT_TO_PTR(UNIT_AFTER),                         "Unit"    },
1264                 { "RecursiveStop",          config_parse_bool,            &u->meta.recursive_stop,                         "Unit"    },
1265                 { "StopWhenUnneeded",       config_parse_bool,            &u->meta.stop_when_unneeded,                     "Unit"    },
1266
1267                 { "PIDFile",                config_parse_path,            &u->service.pid_file,                            "Service" },
1268                 { "ExecStartPre",           config_parse_exec,            u->service.exec_command+SERVICE_EXEC_START_PRE,  "Service" },
1269                 { "ExecStart",              config_parse_exec,            u->service.exec_command+SERVICE_EXEC_START,      "Service" },
1270                 { "ExecStartPost",          config_parse_exec,            u->service.exec_command+SERVICE_EXEC_START_POST, "Service" },
1271                 { "ExecReload",             config_parse_exec,            u->service.exec_command+SERVICE_EXEC_RELOAD,     "Service" },
1272                 { "ExecStop",               config_parse_exec,            u->service.exec_command+SERVICE_EXEC_STOP,       "Service" },
1273                 { "ExecStopPost",           config_parse_exec,            u->service.exec_command+SERVICE_EXEC_STOP_POST,  "Service" },
1274                 { "RestartSec",             config_parse_usec,            &u->service.restart_usec,                        "Service" },
1275                 { "TimeoutSec",             config_parse_usec,            &u->service.timeout_usec,                        "Service" },
1276                 { "Type",                   config_parse_service_type,    &u->service.type,                                "Service" },
1277                 { "Restart",                config_parse_service_restart, &u->service.restart,                             "Service" },
1278                 { "PermissionsStartOnly",   config_parse_bool,            &u->service.permissions_start_only,              "Service" },
1279                 { "RootDirectoryStartOnly", config_parse_bool,            &u->service.root_directory_start_only,           "Service" },
1280                 { "ValidNoProcess",         config_parse_bool,            &u->service.valid_no_process,                    "Service" },
1281                 { "SysVStartPriority",      config_parse_sysv_priority,   &u->service.sysv_start_priority,                 "Service" },
1282                 { "KillMode",               config_parse_kill_mode,       &u->service.kill_mode,                           "Service" },
1283                 { "NonBlocking",            config_parse_bool,            &u->service.exec_context.non_blocking,           "Service" },
1284                 { "BusName",                config_parse_string,          &u->service.bus_name,                            "Service" },
1285                 EXEC_CONTEXT_CONFIG_ITEMS(u->service.exec_context, "Service"),
1286
1287                 { "ListenStream",           config_parse_listen,          &u->socket,                                      "Socket"  },
1288                 { "ListenDatagram",         config_parse_listen,          &u->socket,                                      "Socket"  },
1289                 { "ListenSequentialPacket", config_parse_listen,          &u->socket,                                      "Socket"  },
1290                 { "ListenFIFO",             config_parse_listen,          &u->socket,                                      "Socket"  },
1291                 { "BindIPv6Only",           config_parse_socket_bind,     &u->socket,                                      "Socket"  },
1292                 { "Backlog",                config_parse_unsigned,        &u->socket.backlog,                              "Socket"  },
1293                 { "BindToDevice",           config_parse_bindtodevice,    &u->socket,                                      "Socket"  },
1294                 { "ExecStartPre",           config_parse_exec,            u->socket.exec_command+SOCKET_EXEC_START_PRE,    "Socket"  },
1295                 { "ExecStartPost",          config_parse_exec,            u->socket.exec_command+SOCKET_EXEC_START_POST,   "Socket"  },
1296                 { "ExecStopPre",            config_parse_exec,            u->socket.exec_command+SOCKET_EXEC_STOP_PRE,     "Socket"  },
1297                 { "ExecStopPost",           config_parse_exec,            u->socket.exec_command+SOCKET_EXEC_STOP_POST,    "Socket"  },
1298                 { "TimeoutSec",             config_parse_usec,            &u->socket.timeout_usec,                         "Socket"  },
1299                 { "DirectoryMode",          config_parse_mode,            &u->socket.directory_mode,                       "Socket"  },
1300                 { "SocketMode",             config_parse_mode,            &u->socket.socket_mode,                          "Socket"  },
1301                 { "KillMode",               config_parse_kill_mode,       &u->socket.kill_mode,                            "Socket"  },
1302                 { "Accept",                 config_parse_bool,            &u->socket.accept,                               "Socket"  },
1303                 EXEC_CONTEXT_CONFIG_ITEMS(u->socket.exec_context, "Socket"),
1304
1305                 { "What",                   config_parse_string,          &u->mount.parameters_fragment.what,              "Mount"   },
1306                 { "Where",                  config_parse_path,            &u->mount.where,                                 "Mount"   },
1307                 { "Options",                config_parse_string,          &u->mount.parameters_fragment.options,           "Mount"   },
1308                 { "Type",                   config_parse_string,          &u->mount.parameters_fragment.fstype,            "Mount"   },
1309                 { "TimeoutSec",             config_parse_usec,            &u->mount.timeout_usec,                          "Mount"   },
1310                 { "KillMode",               config_parse_kill_mode,       &u->mount.kill_mode,                             "Mount"   },
1311                 EXEC_CONTEXT_CONFIG_ITEMS(u->mount.exec_context, "Mount"),
1312
1313                 { "Where",                  config_parse_path,            &u->automount.where,                             "Automount" },
1314
1315                 { "What",                   config_parse_path,            &u->swap.parameters_fragment.what,               "Swap" },
1316                 { "Priority",               config_parse_int,             &u->swap.parameters_fragment.priority,           "Swap" },
1317
1318                 { NULL, NULL, NULL, NULL }
1319         };
1320
1321 #undef EXEC_CONTEXT_CONFIG_ITEMS
1322
1323         const char *sections[3];
1324         char *k;
1325         int r;
1326         Set *symlink_names;
1327         FILE *f = NULL;
1328         char *filename = NULL, *id = NULL;
1329         Unit *merged;
1330
1331         if (!u) {
1332                 /* Dirty dirty hack. */
1333                 dump_items((FILE*) path, items);
1334                 return 0;
1335         }
1336
1337         assert(u);
1338         assert(path);
1339
1340         sections[0] = "Unit";
1341         sections[1] = section_table[u->meta.type];
1342         sections[2] = NULL;
1343
1344         if (!(symlink_names = set_new(string_hash_func, string_compare_func)))
1345                 return -ENOMEM;
1346
1347         if (path_is_absolute(path)) {
1348
1349                 if (!(filename = strdup(path))) {
1350                         r = -ENOMEM;
1351                         goto finish;
1352                 }
1353
1354                 if ((r = open_follow(&filename, &f, symlink_names, &id)) < 0) {
1355                         free(filename);
1356                         filename = NULL;
1357
1358                         if (r != -ENOENT)
1359                                 goto finish;
1360                 }
1361
1362         } else  {
1363                 char **p;
1364
1365                 STRV_FOREACH(p, u->meta.manager->unit_path) {
1366
1367                         /* Instead of opening the path right away, we manually
1368                          * follow all symlinks and add their name to our unit
1369                          * name set while doing so */
1370                         if (!(filename = path_make_absolute(path, *p))) {
1371                                 r = -ENOMEM;
1372                                 goto finish;
1373                         }
1374
1375                         if ((r = open_follow(&filename, &f, symlink_names, &id)) < 0) {
1376                                 char *sn;
1377
1378                                 free(filename);
1379                                 filename = NULL;
1380
1381                                 if (r != -ENOENT)
1382                                         goto finish;
1383
1384                                 /* Empty the symlink names for the next run */
1385                                 while ((sn = set_steal_first(symlink_names)))
1386                                         free(sn);
1387
1388                                 continue;
1389                         }
1390
1391                         break;
1392                 }
1393         }
1394
1395         if (!filename) {
1396                 r = 0;
1397                 goto finish;
1398         }
1399
1400         merged = u;
1401         if ((r = merge_by_names(&merged, symlink_names, id)) < 0)
1402                 goto finish;
1403
1404         if (merged != u) {
1405                 u->meta.load_state = UNIT_MERGED;
1406                 r = 0;
1407                 goto finish;
1408         }
1409
1410         /* Now, parse the file contents */
1411         if ((r = config_parse(filename, f, sections, items, u)) < 0)
1412                 goto finish;
1413
1414         free(u->meta.fragment_path);
1415         u->meta.fragment_path = filename;
1416         filename = NULL;
1417
1418         u->meta.load_state = UNIT_LOADED;
1419         r = 0;
1420
1421 finish:
1422         while ((k = set_steal_first(symlink_names)))
1423                 free(k);
1424
1425         set_free(symlink_names);
1426         free(filename);
1427
1428         if (f)
1429                 fclose(f);
1430
1431         return r;
1432 }
1433
1434 int unit_load_fragment(Unit *u) {
1435         int r;
1436
1437         assert(u);
1438
1439         if (u->meta.fragment_path) {
1440
1441                 if ((r = load_from_path(u, u->meta.fragment_path)) < 0)
1442                         return r;
1443
1444         } else {
1445                 Iterator i;
1446                 const char *t;
1447
1448                 /* Try to find the unit under its id */
1449                 if ((r = load_from_path(u, u->meta.id)) < 0)
1450                         return r;
1451
1452                 /* Try to find an alias we can load this with */
1453                 if (u->meta.load_state == UNIT_STUB)
1454                         SET_FOREACH(t, u->meta.names, i) {
1455
1456                                 if (t == u->meta.id)
1457                                         continue;
1458
1459                                 if ((r = load_from_path(u, t)) < 0)
1460                                         return r;
1461
1462                                 if (u->meta.load_state != UNIT_STUB)
1463                                         break;
1464                         }
1465
1466                 /* Now, follow the same logic, but look for a template */
1467                 if (u->meta.load_state == UNIT_STUB && u->meta.instance) {
1468                         char *k;
1469
1470                         if (!(k = unit_name_template(u->meta.id)))
1471                                 return -ENOMEM;
1472
1473                         r = load_from_path(u, k);
1474                         free(k);
1475
1476                         if (r < 0)
1477                                 return r;
1478
1479                         if (u->meta.load_state == UNIT_STUB)
1480                                 SET_FOREACH(t, u->meta.names, i) {
1481
1482                                         if (t == u->meta.id)
1483                                                 continue;
1484
1485                                         if (!(k = unit_name_template(t)))
1486                                                 return -ENOMEM;
1487
1488                                         r = load_from_path(u, k);
1489                                         free(k);
1490
1491                                         if (r < 0)
1492                                                 return r;
1493
1494                                         if (u->meta.load_state != UNIT_STUB)
1495                                                 break;
1496                                 }
1497                 }
1498         }
1499
1500         return 0;
1501 }
1502
1503 void unit_dump_config_items(FILE *f) {
1504         /* OK, this wins a prize for extreme ugliness. */
1505
1506         load_from_path(NULL, (const void*) f);
1507 }