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