chiark / gitweb /
execute: when running in session mode, still enforce proper ordering of logger socket
[elogind.git] / 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
31 #include "unit.h"
32 #include "strv.h"
33 #include "conf-parser.h"
34 #include "load-fragment.h"
35 #include "log.h"
36 #include "ioprio.h"
37 #include "securebits.h"
38 #include "missing.h"
39
40 static int config_parse_deps(
41                 const char *filename,
42                 unsigned line,
43                 const char *section,
44                 const char *lvalue,
45                 const char *rvalue,
46                 void *data,
47                 void *userdata) {
48
49         UnitDependency d = PTR_TO_UINT(data);
50         Unit *u = userdata;
51         char *w;
52         size_t l;
53         char *state;
54
55         assert(filename);
56         assert(lvalue);
57         assert(rvalue);
58
59         FOREACH_WORD(w, l, rvalue, state) {
60                 char *t;
61                 int r;
62
63                 if (!(t = strndup(w, l)))
64                         return -ENOMEM;
65
66                 r = unit_add_dependency_by_name(u, d, t);
67                 free(t);
68
69                 if (r < 0)
70                         return r;
71         }
72
73         return 0;
74 }
75
76 static int config_parse_names(
77                 const char *filename,
78                 unsigned line,
79                 const char *section,
80                 const char *lvalue,
81                 const char *rvalue,
82                 void *data,
83                 void *userdata) {
84
85         Unit *u = userdata;
86         char *w;
87         size_t l;
88         char *state;
89
90         assert(filename);
91         assert(lvalue);
92         assert(rvalue);
93         assert(data);
94
95         FOREACH_WORD(w, l, rvalue, state) {
96                 char *t;
97                 int r;
98                 Unit *other;
99
100                 if (!(t = strndup(w, l)))
101                         return -ENOMEM;
102
103                 other = manager_get_unit(u->meta.manager, t);
104
105                 if (other) {
106
107                         if (other != u) {
108
109                                 if (other->meta.load_state != UNIT_STUB) {
110                                         free(t);
111                                         return -EEXIST;
112                                 }
113
114                                 if ((r = unit_merge(u, other)) < 0) {
115                                         free(t);
116                                         return r;
117                                 }
118                         }
119
120                 } else {
121                         if ((r = unit_add_name(u, t)) < 0) {
122                                 free(t);
123                                 return r;
124                         }
125                 }
126
127                 free(t);
128         }
129
130         return 0;
131 }
132
133 static int config_parse_listen(
134                 const char *filename,
135                 unsigned line,
136                 const char *section,
137                 const char *lvalue,
138                 const char *rvalue,
139                 void *data,
140                 void *userdata) {
141
142         int r;
143         SocketPort *p;
144         Socket *s;
145
146         assert(filename);
147         assert(lvalue);
148         assert(rvalue);
149         assert(data);
150
151         s = (Socket*) data;
152
153         if (!(p = new0(SocketPort, 1)))
154                 return -ENOMEM;
155
156         if (streq(lvalue, "ListenFIFO")) {
157                 p->type = SOCKET_FIFO;
158
159                 if (!(p->path = strdup(rvalue))) {
160                         free(p);
161                         return -ENOMEM;
162                 }
163         } else {
164                 p->type = SOCKET_SOCKET;
165
166                 if ((r = socket_address_parse(&p->address, rvalue)) < 0) {
167                         log_error("[%s:%u] Failed to parse address value: %s", filename, line, rvalue);
168                         free(p);
169                         return r;
170                 }
171
172                 if (streq(lvalue, "ListenStream"))
173                         p->address.type = SOCK_STREAM;
174                 else if (streq(lvalue, "ListenDatagram"))
175                         p->address.type = SOCK_DGRAM;
176                 else {
177                         assert(streq(lvalue, "ListenSequentialPacket"));
178                         p->address.type = SOCK_SEQPACKET;
179                 }
180
181                 if (socket_address_family(&p->address) != AF_LOCAL && p->address.type == SOCK_SEQPACKET) {
182                         free(p);
183                         return -EPROTONOSUPPORT;
184                 }
185         }
186
187         p->fd = -1;
188         LIST_PREPEND(SocketPort, port, s->ports, p);
189
190         return 0;
191 }
192
193 static int config_parse_socket_bind(
194                 const char *filename,
195                 unsigned line,
196                 const char *section,
197                 const char *lvalue,
198                 const char *rvalue,
199                 void *data,
200                 void *userdata) {
201
202         int r;
203         Socket *s;
204
205         assert(filename);
206         assert(lvalue);
207         assert(rvalue);
208         assert(data);
209
210         s = (Socket*) data;
211
212         if ((r = parse_boolean(rvalue)) < 0) {
213                 log_error("[%s:%u] Failed to parse bind IPv6 only value: %s", filename, line, rvalue);
214                 return r;
215         }
216
217         s->bind_ipv6_only = r ? SOCKET_ADDRESS_IPV6_ONLY : SOCKET_ADDRESS_BOTH;
218
219         return 0;
220 }
221
222 static int config_parse_nice(
223                 const char *filename,
224                 unsigned line,
225                 const char *section,
226                 const char *lvalue,
227                 const char *rvalue,
228                 void *data,
229                 void *userdata) {
230
231         ExecContext *c = data;
232         int priority, r;
233
234         assert(filename);
235         assert(lvalue);
236         assert(rvalue);
237         assert(data);
238
239         if ((r = safe_atoi(rvalue, &priority)) < 0) {
240                 log_error("[%s:%u] Failed to parse nice priority: %s", filename, line, rvalue);
241                 return r;
242         }
243
244         if (priority < PRIO_MIN || priority >= PRIO_MAX) {
245                 log_error("[%s:%u] Nice priority out of range: %s", filename, line, rvalue);
246                 return -ERANGE;
247         }
248
249         c->nice = priority;
250         c->nice_set = false;
251
252         return 0;
253 }
254
255 static int config_parse_oom_adjust(
256                 const char *filename,
257                 unsigned line,
258                 const char *section,
259                 const char *lvalue,
260                 const char *rvalue,
261                 void *data,
262                 void *userdata) {
263
264         ExecContext *c = data;
265         int oa, r;
266
267         assert(filename);
268         assert(lvalue);
269         assert(rvalue);
270         assert(data);
271
272         if ((r = safe_atoi(rvalue, &oa)) < 0) {
273                 log_error("[%s:%u] Failed to parse OOM adjust value: %s", filename, line, rvalue);
274                 return r;
275         }
276
277         if (oa < OOM_DISABLE || oa > OOM_ADJUST_MAX) {
278                 log_error("[%s:%u] OOM adjust value out of range: %s", filename, line, rvalue);
279                 return -ERANGE;
280         }
281
282         c->oom_adjust = oa;
283         c->oom_adjust_set = true;
284
285         return 0;
286 }
287
288 static int config_parse_mode(
289                 const char *filename,
290                 unsigned line,
291                 const char *section,
292                 const char *lvalue,
293                 const char *rvalue,
294                 void *data,
295                 void *userdata) {
296
297         mode_t *m = data;
298         long l;
299         char *x = NULL;
300
301         assert(filename);
302         assert(lvalue);
303         assert(rvalue);
304         assert(data);
305
306         errno = 0;
307         l = strtol(rvalue, &x, 8);
308         if (!x || *x || errno) {
309                 log_error("[%s:%u] Failed to parse mode value: %s", filename, line, rvalue);
310                 return errno ? -errno : -EINVAL;
311         }
312
313         if (l < 0000 || l > 07777) {
314                 log_error("[%s:%u] mode value out of range: %s", filename, line, rvalue);
315                 return -ERANGE;
316         }
317
318         *m = (mode_t) l;
319         return 0;
320 }
321
322 static int config_parse_exec(
323                 const char *filename,
324                 unsigned line,
325                 const char *section,
326                 const char *lvalue,
327                 const char *rvalue,
328                 void *data,
329                 void *userdata) {
330
331         ExecCommand **e = data, *nce = NULL;
332         char **n;
333         char *w;
334         unsigned k;
335         size_t l;
336         char *state;
337
338         assert(filename);
339         assert(lvalue);
340         assert(rvalue);
341         assert(data);
342
343         k = 0;
344         FOREACH_WORD_QUOTED(w, l, rvalue, state)
345                 k++;
346
347         if (!(n = new(char*, k+1)))
348                 return -ENOMEM;
349
350         k = 0;
351         FOREACH_WORD_QUOTED(w, l, rvalue, state)
352                 if (!(n[k++] = strndup(w, l)))
353                         goto fail;
354
355         n[k] = NULL;
356
357         if (!n[0] || !path_is_absolute(n[0])) {
358                 log_error("[%s:%u] Invalid executable path in command line: %s", filename, line, rvalue);
359                 strv_free(n);
360                 return -EINVAL;
361         }
362
363         if (!(nce = new0(ExecCommand, 1)))
364                 goto fail;
365
366         nce->argv = n;
367         if (!(nce->path = strdup(n[0])))
368                 goto fail;
369
370         exec_command_append_list(e, nce);
371
372         return 0;
373
374 fail:
375         for (; k > 0; k--)
376                 free(n[k-1]);
377         free(n);
378
379         free(nce);
380
381         return -ENOMEM;
382 }
383
384 static int config_parse_usec(
385                 const char *filename,
386                 unsigned line,
387                 const char *section,
388                 const char *lvalue,
389                 const char *rvalue,
390                 void *data,
391                 void *userdata) {
392
393         usec_t *usec = data;
394         unsigned long long u;
395         int r;
396
397         assert(filename);
398         assert(lvalue);
399         assert(rvalue);
400         assert(data);
401
402         if ((r = safe_atollu(rvalue, &u)) < 0) {
403                 log_error("[%s:%u] Failed to parse time value: %s", filename, line, rvalue);
404                 return r;
405         }
406
407         /* We actually assume the user configures seconds. Later on we
408          * might choose to support suffixes for time values, to
409          * configure bigger or smaller units */
410
411         *usec = u * USEC_PER_SEC;
412
413         return 0;
414 }
415
416 static int config_parse_service_type(
417                 const char *filename,
418                 unsigned line,
419                 const char *section,
420                 const char *lvalue,
421                 const char *rvalue,
422                 void *data,
423                 void *userdata) {
424
425         Service *s = data;
426         ServiceType x;
427
428         assert(filename);
429         assert(lvalue);
430         assert(rvalue);
431         assert(data);
432
433         if ((x = service_type_from_string(rvalue)) < 0) {
434                 log_error("[%s:%u] Failed to parse service type: %s", filename, line, rvalue);
435                 return -EBADMSG;
436         }
437
438         s->type = x;
439
440         return 0;
441 }
442
443 static int config_parse_service_restart(
444                 const char *filename,
445                 unsigned line,
446                 const char *section,
447                 const char *lvalue,
448                 const char *rvalue,
449                 void *data,
450                 void *userdata) {
451
452         Service *s = data;
453         ServiceRestart x;
454
455         assert(filename);
456         assert(lvalue);
457         assert(rvalue);
458         assert(data);
459
460         if ((x = service_restart_from_string(rvalue)) < 0) {
461                 log_error("[%s:%u] Failed to parse service restart specifier: %s", filename, line, rvalue);
462                 return -EBADMSG;
463         }
464
465         s->restart = x;
466
467         return 0;
468 }
469
470 static int config_parse_bindtodevice(
471                 const char *filename,
472                 unsigned line,
473                 const char *section,
474                 const char *lvalue,
475                 const char *rvalue,
476                 void *data,
477                 void *userdata) {
478
479         Socket *s = data;
480         char *n;
481
482         assert(filename);
483         assert(lvalue);
484         assert(rvalue);
485         assert(data);
486
487         if (rvalue[0] && !streq(rvalue, "*")) {
488                 if (!(n = strdup(rvalue)))
489                         return -ENOMEM;
490         } else
491                 n = NULL;
492
493         free(s->bind_to_device);
494         s->bind_to_device = n;
495
496         return 0;
497 }
498
499 static int config_parse_output(
500                 const char *filename,
501                 unsigned line,
502                 const char *section,
503                 const char *lvalue,
504                 const char *rvalue,
505                 void *data,
506                 void *userdata) {
507
508         ExecOutput *o = data, x;
509
510         assert(filename);
511         assert(lvalue);
512         assert(rvalue);
513         assert(data);
514
515         if ((x = exec_output_from_string(rvalue)) < 0) {
516                 log_error("[%s:%u] Failed to parse output specifier: %s", filename, line, rvalue);
517                 return -EBADMSG;
518         }
519
520         *o = x;
521
522         return 0;
523 }
524
525 static int config_parse_input(
526                 const char *filename,
527                 unsigned line,
528                 const char *section,
529                 const char *lvalue,
530                 const char *rvalue,
531                 void *data,
532                 void *userdata) {
533
534         ExecInput *i = data, x;
535
536         assert(filename);
537         assert(lvalue);
538         assert(rvalue);
539         assert(data);
540
541         if ((x = exec_input_from_string(rvalue)) < 0) {
542                 log_error("[%s:%u] Failed to parse input specifier: %s", filename, line, rvalue);
543                 return -EBADMSG;
544         }
545
546         *i = x;
547
548         return 0;
549 }
550
551 static int config_parse_facility(
552                 const char *filename,
553                 unsigned line,
554                 const char *section,
555                 const char *lvalue,
556                 const char *rvalue,
557                 void *data,
558                 void *userdata) {
559
560
561         int *o = data, x;
562
563         assert(filename);
564         assert(lvalue);
565         assert(rvalue);
566         assert(data);
567
568         if ((x = log_facility_from_string(rvalue)) < 0)
569
570                 /* Second try, let's see if this is a number. */
571                 if (safe_atoi(rvalue, &x) < 0 || !log_facility_to_string(x)) {
572                         log_error("[%s:%u] Failed to parse log facility: %s", filename, line, rvalue);
573                         return -EBADMSG;
574                 }
575
576         *o = LOG_MAKEPRI(x, LOG_PRI(*o));
577
578         return 0;
579 }
580
581 static int config_parse_level(
582                 const char *filename,
583                 unsigned line,
584                 const char *section,
585                 const char *lvalue,
586                 const char *rvalue,
587                 void *data,
588                 void *userdata) {
589
590
591         int *o = data, x;
592
593         assert(filename);
594         assert(lvalue);
595         assert(rvalue);
596         assert(data);
597
598         if ((x = log_level_from_string(rvalue)) < 0)
599
600                 /* Second try, let's see if this is a number. */
601                 if (safe_atoi(rvalue, &x) < 0 || !log_level_to_string(x)) {
602                         log_error("[%s:%u] Failed to parse log level: %s", filename, line, rvalue);
603                         return -EBADMSG;
604                 }
605
606         *o = LOG_MAKEPRI(LOG_FAC(*o), x);
607         return 0;
608 }
609
610 static int config_parse_io_class(
611                 const char *filename,
612                 unsigned line,
613                 const char *section,
614                 const char *lvalue,
615                 const char *rvalue,
616                 void *data,
617                 void *userdata) {
618
619         ExecContext *c = data;
620         int x;
621
622         assert(filename);
623         assert(lvalue);
624         assert(rvalue);
625         assert(data);
626
627         if ((x = ioprio_class_from_string(rvalue)) < 0)
628
629                 /* Second try, let's see if this is a number. */
630                 if (safe_atoi(rvalue, &x) < 0 || !ioprio_class_to_string(x)) {
631                         log_error("[%s:%u] Failed to parse IO scheduling class: %s", filename, line, rvalue);
632                         return -EBADMSG;
633                 }
634
635         c->ioprio = IOPRIO_PRIO_VALUE(x, IOPRIO_PRIO_DATA(c->ioprio));
636         c->ioprio_set = true;
637
638         return 0;
639 }
640
641 static int config_parse_io_priority(
642                 const char *filename,
643                 unsigned line,
644                 const char *section,
645                 const char *lvalue,
646                 const char *rvalue,
647                 void *data,
648                 void *userdata) {
649
650         ExecContext *c = data;
651         int i;
652
653         assert(filename);
654         assert(lvalue);
655         assert(rvalue);
656         assert(data);
657
658         if (safe_atoi(rvalue, &i) < 0 || i < 0 || i >= IOPRIO_BE_NR) {
659                 log_error("[%s:%u] Failed to parse io priority: %s", filename, line, rvalue);
660                 return -EBADMSG;
661         }
662
663         c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_PRIO_CLASS(c->ioprio), i);
664         c->ioprio_set = true;
665
666         return 0;
667 }
668
669 static int config_parse_cpu_sched_policy(
670                 const char *filename,
671                 unsigned line,
672                 const char *section,
673                 const char *lvalue,
674                 const char *rvalue,
675                 void *data,
676                 void *userdata) {
677
678
679         ExecContext *c = data;
680         int x;
681
682         assert(filename);
683         assert(lvalue);
684         assert(rvalue);
685         assert(data);
686
687         if ((x = sched_policy_from_string(rvalue)) < 0)
688
689                 /* Second try, let's see if this is a number. */
690                 if (safe_atoi(rvalue, &x) < 0 || !sched_policy_to_string(x)) {
691                         log_error("[%s:%u] Failed to parse CPU scheduling policy: %s", filename, line, rvalue);
692                         return -EBADMSG;
693                 }
694
695         c->cpu_sched_policy = x;
696         c->cpu_sched_set = true;
697
698         return 0;
699 }
700
701 static int config_parse_cpu_sched_prio(
702                 const char *filename,
703                 unsigned line,
704                 const char *section,
705                 const char *lvalue,
706                 const char *rvalue,
707                 void *data,
708                 void *userdata) {
709
710         ExecContext *c = data;
711         int i;
712
713         assert(filename);
714         assert(lvalue);
715         assert(rvalue);
716         assert(data);
717
718         /* On Linux RR/FIFO have the same range */
719         if (safe_atoi(rvalue, &i) < 0 || i < sched_get_priority_min(SCHED_RR) || i > sched_get_priority_max(SCHED_RR)) {
720                 log_error("[%s:%u] Failed to parse CPU scheduling priority: %s", filename, line, rvalue);
721                 return -EBADMSG;
722         }
723
724         c->cpu_sched_priority = i;
725         c->cpu_sched_set = true;
726
727         return 0;
728 }
729
730 static int config_parse_cpu_affinity(
731                 const char *filename,
732                 unsigned line,
733                 const char *section,
734                 const char *lvalue,
735                 const char *rvalue,
736                 void *data,
737                 void *userdata) {
738
739         ExecContext *c = data;
740         char *w;
741         size_t l;
742         char *state;
743
744         assert(filename);
745         assert(lvalue);
746         assert(rvalue);
747         assert(data);
748
749         FOREACH_WORD(w, l, rvalue, state) {
750                 char *t;
751                 int r;
752                 unsigned cpu;
753
754                 if (!(t = strndup(w, l)))
755                         return -ENOMEM;
756
757                 r = safe_atou(t, &cpu);
758                 free(t);
759
760                 if (r < 0 || cpu >= CPU_SETSIZE) {
761                         log_error("[%s:%u] Failed to parse CPU affinity: %s", filename, line, rvalue);
762                         return -EBADMSG;
763                 }
764
765                 CPU_SET(cpu, &c->cpu_affinity);
766         }
767
768         c->cpu_affinity_set = true;
769
770         return 0;
771 }
772
773 static int config_parse_capabilities(
774                 const char *filename,
775                 unsigned line,
776                 const char *section,
777                 const char *lvalue,
778                 const char *rvalue,
779                 void *data,
780                 void *userdata) {
781
782         ExecContext *c = data;
783         cap_t cap;
784
785         assert(filename);
786         assert(lvalue);
787         assert(rvalue);
788         assert(data);
789
790         if (!(cap = cap_from_text(rvalue))) {
791                 if (errno == ENOMEM)
792                         return -ENOMEM;
793
794                 log_error("[%s:%u] Failed to parse capabilities: %s", filename, line, rvalue);
795                 return -EBADMSG;
796         }
797
798         if (c->capabilities)
799                 cap_free(c->capabilities);
800         c->capabilities = cap;
801
802         return 0;
803 }
804
805 static int config_parse_secure_bits(
806                 const char *filename,
807                 unsigned line,
808                 const char *section,
809                 const char *lvalue,
810                 const char *rvalue,
811                 void *data,
812                 void *userdata) {
813
814         ExecContext *c = data;
815         char *w;
816         size_t l;
817         char *state;
818
819         assert(filename);
820         assert(lvalue);
821         assert(rvalue);
822         assert(data);
823
824         FOREACH_WORD(w, l, rvalue, state) {
825                 if (first_word(w, "keep-caps"))
826                         c->secure_bits |= SECURE_KEEP_CAPS;
827                 else if (first_word(w, "keep-caps-locked"))
828                         c->secure_bits |= SECURE_KEEP_CAPS_LOCKED;
829                 else if (first_word(w, "no-setuid-fixup"))
830                         c->secure_bits |= SECURE_NO_SETUID_FIXUP;
831                 else if (first_word(w, "no-setuid-fixup-locked"))
832                         c->secure_bits |= SECURE_NO_SETUID_FIXUP_LOCKED;
833                 else if (first_word(w, "noroot"))
834                         c->secure_bits |= SECURE_NOROOT;
835                 else if (first_word(w, "noroot-locked"))
836                         c->secure_bits |= SECURE_NOROOT_LOCKED;
837                 else {
838                         log_error("[%s:%u] Failed to parse secure bits: %s", filename, line, rvalue);
839                         return -EBADMSG;
840                 }
841         }
842
843         return 0;
844 }
845
846 static int config_parse_bounding_set(
847                 const char *filename,
848                 unsigned line,
849                 const char *section,
850                 const char *lvalue,
851                 const char *rvalue,
852                 void *data,
853                 void *userdata) {
854
855         ExecContext *c = data;
856         char *w;
857         size_t l;
858         char *state;
859
860         assert(filename);
861         assert(lvalue);
862         assert(rvalue);
863         assert(data);
864
865         FOREACH_WORD(w, l, rvalue, state) {
866                 char *t;
867                 int r;
868                 cap_value_t cap;
869
870                 if (!(t = strndup(w, l)))
871                         return -ENOMEM;
872
873                 r = cap_from_name(t, &cap);
874                 free(t);
875
876                 if (r < 0) {
877                         log_error("[%s:%u] Failed to parse capability bounding set: %s", filename, line, rvalue);
878                         return -EBADMSG;
879                 }
880
881                 c->capability_bounding_set_drop |= 1 << cap;
882         }
883
884         return 0;
885 }
886
887 static int config_parse_timer_slack_ns(
888                 const char *filename,
889                 unsigned line,
890                 const char *section,
891                 const char *lvalue,
892                 const char *rvalue,
893                 void *data,
894                 void *userdata) {
895
896         ExecContext *c = data;
897         unsigned long u;
898         int r;
899
900         assert(filename);
901         assert(lvalue);
902         assert(rvalue);
903         assert(data);
904
905         if ((r = safe_atolu(rvalue, &u)) < 0) {
906                 log_error("[%s:%u] Failed to parse time slack value: %s", filename, line, rvalue);
907                 return r;
908         }
909
910         c->timer_slack_ns = u;
911
912         return 0;
913 }
914
915 static int config_parse_limit(
916                 const char *filename,
917                 unsigned line,
918                 const char *section,
919                 const char *lvalue,
920                 const char *rvalue,
921                 void *data,
922                 void *userdata) {
923
924         struct rlimit **rl = data;
925         unsigned long long u;
926         int r;
927
928         assert(filename);
929         assert(lvalue);
930         assert(rvalue);
931         assert(data);
932
933         if ((r = safe_atollu(rvalue, &u)) < 0) {
934                 log_error("[%s:%u] Failed to parse resource value: %s", filename, line, rvalue);
935                 return r;
936         }
937
938         if (!*rl)
939                 if (!(*rl = new(struct rlimit, 1)))
940                         return -ENOMEM;
941
942         (*rl)->rlim_cur = (*rl)->rlim_max = (rlim_t) u;
943         return 0;
944 }
945
946 #define FOLLOW_MAX 8
947
948 static int open_follow(char **filename, FILE **_f, Set *names, char **_id) {
949         unsigned c = 0;
950         int fd, r;
951         FILE *f;
952         char *id = NULL;
953
954         assert(filename);
955         assert(*filename);
956         assert(_f);
957         assert(names);
958
959         /* This will update the filename pointer if the loaded file is
960          * reached by a symlink. The old string will be freed. */
961
962         for (;;) {
963                 char *target, *k, *name;
964
965                 if (c++ >= FOLLOW_MAX)
966                         return -ELOOP;
967
968                 path_kill_slashes(*filename);
969
970                 /* Add the file name we are currently looking at to
971                  * the names of this unit */
972                 name = file_name_from_path(*filename);
973                 if (!(id = set_get(names, name))) {
974
975                         if (!(id = strdup(name)))
976                                 return -ENOMEM;
977
978                         if ((r = set_put(names, id)) < 0) {
979                                 free(id);
980                                 return r;
981                         }
982                 }
983
984                 /* Try to open the file name, but don't if its a symlink */
985                 if ((fd = open(*filename, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW)) >= 0)
986                         break;
987
988                 if (errno != ELOOP)
989                         return -errno;
990
991                 /* Hmm, so this is a symlink. Let's read the name, and follow it manually */
992                 if ((r = readlink_malloc(*filename, &target)) < 0)
993                         return r;
994
995                 k = file_in_same_dir(*filename, target);
996                 free(target);
997
998                 if (!k)
999                         return -ENOMEM;
1000
1001                 free(*filename);
1002                 *filename = k;
1003         }
1004
1005         if (!(f = fdopen(fd, "r"))) {
1006                 r = -errno;
1007                 assert(close_nointr(fd) == 0);
1008                 return r;
1009         }
1010
1011         *_f = f;
1012         *_id = id;
1013         return 0;
1014 }
1015
1016 static int load_from_path(Unit *u, const char *path) {
1017
1018         static const char* const section_table[_UNIT_TYPE_MAX] = {
1019                 [UNIT_SERVICE]   = "Service",
1020                 [UNIT_TIMER]     = "Timer",
1021                 [UNIT_SOCKET]    = "Socket",
1022                 [UNIT_TARGET]    = "Target",
1023                 [UNIT_DEVICE]    = "Device",
1024                 [UNIT_MOUNT]     = "Mount",
1025                 [UNIT_AUTOMOUNT] = "Automount",
1026                 [UNIT_SNAPSHOT]  = "Snapshot"
1027         };
1028
1029 #define EXEC_CONTEXT_CONFIG_ITEMS(context, section) \
1030                 { "WorkingDirectory",       config_parse_path,            &(context).working_directory,                    section   }, \
1031                 { "RootDirectory",          config_parse_path,            &(context).root_directory,                       section   }, \
1032                 { "User",                   config_parse_string,          &(context).user,                                 section   }, \
1033                 { "Group",                  config_parse_string,          &(context).group,                                section   }, \
1034                 { "SupplementaryGroups",    config_parse_strv,            &(context).supplementary_groups,                 section   }, \
1035                 { "Nice",                   config_parse_nice,            &(context),                                      section   }, \
1036                 { "OOMAdjust",              config_parse_oom_adjust,      &(context),                                      section   }, \
1037                 { "IOSchedulingClass",      config_parse_io_class,        &(context),                                      section   }, \
1038                 { "IOSchedulingPriority",   config_parse_io_priority,     &(context),                                      section   }, \
1039                 { "CPUSchedulingPolicy",    config_parse_cpu_sched_policy,&(context),                                      section   }, \
1040                 { "CPUSchedulingPriority",  config_parse_cpu_sched_prio,  &(context),                                      section   }, \
1041                 { "CPUSchedulingResetOnFork", config_parse_bool,          &(context).cpu_sched_reset_on_fork,              section   }, \
1042                 { "CPUAffinity",            config_parse_cpu_affinity,    &(context),                                      section   }, \
1043                 { "UMask",                  config_parse_mode,            &(context).umask,                                section   }, \
1044                 { "Environment",            config_parse_strv,            &(context).environment,                          section   }, \
1045                 { "Output",                 config_parse_output,          &(context).output,                               section   }, \
1046                 { "Input",                  config_parse_input,           &(context).input,                                section   }, \
1047                 { "SyslogIdentifier",       config_parse_string,          &(context).syslog_identifier,                    section   }, \
1048                 { "SyslogFacility",         config_parse_facility,        &(context).syslog_priority,                      section   }, \
1049                 { "SyslogLevel",            config_parse_level,           &(context).syslog_priority,                      section   }, \
1050                 { "Capabilities",           config_parse_capabilities,    &(context),                                      section   }, \
1051                 { "SecureBits",             config_parse_secure_bits,     &(context),                                      section   }, \
1052                 { "CapabilityBoundingSetDrop", config_parse_bounding_set, &(context),                                      section   }, \
1053                 { "TimerSlackNS",           config_parse_timer_slack_ns,  &(context),                                      section   }, \
1054                 { "LimitCPU",               config_parse_limit,           &(context).rlimit[RLIMIT_CPU],                   section   }, \
1055                 { "LimitFSIZE",             config_parse_limit,           &(context).rlimit[RLIMIT_FSIZE],                 section   }, \
1056                 { "LimitDATA",              config_parse_limit,           &(context).rlimit[RLIMIT_DATA],                  section   }, \
1057                 { "LimitSTACK",             config_parse_limit,           &(context).rlimit[RLIMIT_STACK],                 section   }, \
1058                 { "LimitCORE",              config_parse_limit,           &(context).rlimit[RLIMIT_CORE],                  section   }, \
1059                 { "LimitRSS",               config_parse_limit,           &(context).rlimit[RLIMIT_RSS],                   section   }, \
1060                 { "LimitNOFILE",            config_parse_limit,           &(context).rlimit[RLIMIT_NOFILE],                section   }, \
1061                 { "LimitAS",                config_parse_limit,           &(context).rlimit[RLIMIT_AS],                    section   }, \
1062                 { "LimitNPROC",             config_parse_limit,           &(context).rlimit[RLIMIT_NPROC],                 section   }, \
1063                 { "LimitMEMLOCK",           config_parse_limit,           &(context).rlimit[RLIMIT_MEMLOCK],               section   }, \
1064                 { "LimitLOCKS",             config_parse_limit,           &(context).rlimit[RLIMIT_LOCKS],                 section   }, \
1065                 { "LimitSIGPENDING",        config_parse_limit,           &(context).rlimit[RLIMIT_SIGPENDING],            section   }, \
1066                 { "LimitMSGQUEUE",          config_parse_limit,           &(context).rlimit[RLIMIT_MSGQUEUE],              section   }, \
1067                 { "LimitNICE",              config_parse_limit,           &(context).rlimit[RLIMIT_NICE],                  section   }, \
1068                 { "LimitRTPRIO",            config_parse_limit,           &(context).rlimit[RLIMIT_RTPRIO],                section   }, \
1069                 { "LimitRTTIME",            config_parse_limit,           &(context).rlimit[RLIMIT_RTTIME],                section   }, \
1070                 { "NonBlocking",            config_parse_bool,            &(context).non_blocking,                         section   }
1071
1072         const ConfigItem items[] = {
1073                 { "Names",                  config_parse_names,           u,                                               "Meta"    },
1074                 { "Description",            config_parse_string,          &u->meta.description,                            "Meta"    },
1075                 { "Requires",               config_parse_deps,            UINT_TO_PTR(UNIT_REQUIRES),                      "Meta"    },
1076                 { "SoftRequires",           config_parse_deps,            UINT_TO_PTR(UNIT_SOFT_REQUIRES),                 "Meta"    },
1077                 { "Wants",                  config_parse_deps,            UINT_TO_PTR(UNIT_WANTS),                         "Meta"    },
1078                 { "Requisite",              config_parse_deps,            UINT_TO_PTR(UNIT_REQUISITE),                     "Meta"    },
1079                 { "SoftRequisite",          config_parse_deps,            UINT_TO_PTR(UNIT_SOFT_REQUISITE),                "Meta"    },
1080                 { "Conflicts",              config_parse_deps,            UINT_TO_PTR(UNIT_CONFLICTS),                     "Meta"    },
1081                 { "Before",                 config_parse_deps,            UINT_TO_PTR(UNIT_BEFORE),                        "Meta"    },
1082                 { "After",                  config_parse_deps,            UINT_TO_PTR(UNIT_AFTER),                         "Meta"    },
1083                 { "RecursiveStop",          config_parse_bool,            &u->meta.recursive_stop,                         "Meta"    },
1084                 { "StopWhenUnneeded",       config_parse_bool,            &u->meta.stop_when_unneeded,                     "Meta"    },
1085
1086                 { "PIDFile",                config_parse_path,            &u->service.pid_file,                            "Service" },
1087                 { "ExecStartPre",           config_parse_exec,            u->service.exec_command+SERVICE_EXEC_START_PRE,  "Service" },
1088                 { "ExecStart",              config_parse_exec,            u->service.exec_command+SERVICE_EXEC_START,      "Service" },
1089                 { "ExecStartPost",          config_parse_exec,            u->service.exec_command+SERVICE_EXEC_START_POST, "Service" },
1090                 { "ExecReload",             config_parse_exec,            u->service.exec_command+SERVICE_EXEC_RELOAD,     "Service" },
1091                 { "ExecStop",               config_parse_exec,            u->service.exec_command+SERVICE_EXEC_STOP,       "Service" },
1092                 { "ExecStopPost",           config_parse_exec,            u->service.exec_command+SERVICE_EXEC_STOP_POST,  "Service" },
1093                 { "RestartSec",             config_parse_usec,            &u->service.restart_usec,                        "Service" },
1094                 { "TimeoutSec",             config_parse_usec,            &u->service.timeout_usec,                        "Service" },
1095                 { "Type",                   config_parse_service_type,    &u->service,                                     "Service" },
1096                 { "Restart",                config_parse_service_restart, &u->service,                                     "Service" },
1097                 { "PermissionsStartOnly",   config_parse_bool,            &u->service.permissions_start_only,              "Service" },
1098                 { "RootDirectoryStartOnly", config_parse_bool,            &u->service.root_directory_start_only,           "Service" },
1099                 EXEC_CONTEXT_CONFIG_ITEMS(u->service.exec_context, "Service"),
1100
1101                 { "ListenStream",           config_parse_listen,          &u->socket,                                      "Socket"  },
1102                 { "ListenDatagram",         config_parse_listen,          &u->socket,                                      "Socket"  },
1103                 { "ListenSequentialPacket", config_parse_listen,          &u->socket,                                      "Socket"  },
1104                 { "ListenFIFO",             config_parse_listen,          &u->socket,                                      "Socket"  },
1105                 { "BindIPv6Only",           config_parse_socket_bind,     &u->socket,                                      "Socket"  },
1106                 { "Backlog",                config_parse_unsigned,        &u->socket.backlog,                              "Socket"  },
1107                 { "BindToDevice",           config_parse_bindtodevice,    &u->socket,                                      "Socket"  },
1108                 { "ExecStartPre",           config_parse_exec,            u->socket.exec_command+SOCKET_EXEC_START_PRE,    "Socket"  },
1109                 { "ExecStartPost",          config_parse_exec,            u->socket.exec_command+SOCKET_EXEC_START_POST,   "Socket"  },
1110                 { "ExecStopPre",            config_parse_exec,            u->socket.exec_command+SOCKET_EXEC_STOP_PRE,     "Socket"  },
1111                 { "ExecStopPost",           config_parse_exec,            u->socket.exec_command+SOCKET_EXEC_STOP_POST,    "Socket"  },
1112                 { "DirectoryMode",          config_parse_mode,            &u->socket.directory_mode,                       "Socket"  },
1113                 { "SocketMode",             config_parse_mode,            &u->socket.socket_mode,                          "Socket"  },
1114                 EXEC_CONTEXT_CONFIG_ITEMS(u->socket.exec_context, "Socket"),
1115
1116                 EXEC_CONTEXT_CONFIG_ITEMS(u->automount.exec_context, "Automount"),
1117
1118                 { NULL, NULL, NULL, NULL }
1119         };
1120
1121 #undef EXEC_CONTEXT_CONFIG_ITEMS
1122
1123         const char *sections[3];
1124         char *k;
1125         int r;
1126         Set *symlink_names;
1127         FILE *f;
1128         char *filename = NULL, *id;
1129
1130         sections[0] = "Meta";
1131         sections[1] = section_table[u->meta.type];
1132         sections[2] = NULL;
1133
1134         if (!(symlink_names = set_new(string_hash_func, string_compare_func)))
1135                 return -ENOMEM;
1136
1137         if (path_is_absolute(path)) {
1138
1139                 if (!(filename = strdup(path))) {
1140                         r = -ENOMEM;
1141                         goto finish;
1142                 }
1143
1144                 if ((r = open_follow(&filename, &f, symlink_names, &id)) < 0) {
1145                         free(filename);
1146                         filename = NULL;
1147
1148                         if (r != -ENOENT)
1149                                 goto finish;
1150                 }
1151
1152         } else  {
1153                 char **p;
1154
1155                 STRV_FOREACH(p, u->meta.manager->unit_path) {
1156
1157                         /* Instead of opening the path right away, we manually
1158                          * follow all symlinks and add their name to our unit
1159                          * name set while doing so */
1160                         if (!(filename = path_make_absolute(path, *p))) {
1161                                 r = -ENOMEM;
1162                                 goto finish;
1163                         }
1164
1165                         if ((r = open_follow(&filename, &f, symlink_names, &id)) < 0) {
1166                                 char *sn;
1167
1168                                 free(filename);
1169                                 filename = NULL;
1170
1171                                 if (r != -ENOENT)
1172                                         goto finish;
1173
1174                                 /* Empty the symlink names for the next run */
1175                                 while ((sn = set_steal_first(symlink_names)))
1176                                         free(sn);
1177
1178                                 continue;
1179                         }
1180
1181                         break;
1182                 }
1183         }
1184
1185         if (!filename) {
1186                 r = 0; /* returning 0 means: no suitable config file found */
1187                 goto finish;
1188         }
1189
1190         /* Now, parse the file contents */
1191         r = config_parse(filename, f, sections, items, u);
1192         if (r < 0)
1193                 goto finish;
1194
1195         /* Let's try to add in all symlink names we found */
1196         while ((k = set_steal_first(symlink_names))) {
1197                 if ((r = unit_add_name(u, k)) < 0)
1198                         goto finish;
1199
1200
1201                 if (id == k)
1202                         unit_choose_id(u, id);
1203                 free(k);
1204         }
1205
1206
1207         free(u->meta.fragment_path);
1208         u->meta.fragment_path = filename;
1209         filename = NULL;
1210
1211         r = 1; /* returning 1 means: suitable config file found and loaded */
1212
1213 finish:
1214         while ((k = set_steal_first(symlink_names)))
1215                 free(k);
1216         set_free(symlink_names);
1217         free(filename);
1218
1219         return r;
1220 }
1221
1222 int unit_load_fragment(Unit *u) {
1223         int r = 0;
1224
1225         assert(u);
1226         assert(u->meta.load_state == UNIT_STUB);
1227
1228         if (u->meta.fragment_path)
1229                 r = load_from_path(u, u->meta.fragment_path);
1230         else {
1231                 Iterator i;
1232                 const char *t;
1233
1234                 /* Try to find the unit under its id */
1235                 if ((t = unit_id(u)))
1236                         r = load_from_path(u, t);
1237
1238                 /* Try to find an alias we can load this with */
1239                 if (r == 0)
1240                         SET_FOREACH(t, u->meta.names, i)
1241                                 if ((r = load_from_path(u, t)) != 0)
1242                                         break;
1243         }
1244
1245         if (r >= 0) {
1246                 ExecContext *c;
1247
1248                 if (u->meta.type == UNIT_SOCKET)
1249                         c = &u->socket.exec_context;
1250                 else if (u->meta.type == UNIT_SERVICE)
1251                         c = &u->service.exec_context;
1252                 else
1253                         c = NULL;
1254
1255                 if (c &&
1256                     (c->output == EXEC_OUTPUT_KERNEL || c->output == EXEC_OUTPUT_SYSLOG)) {
1257                         int k;
1258
1259                         /* If syslog or kernel logging is requested, make sure
1260                          * our own logging daemon is run first. */
1261
1262                         if ((k = unit_add_dependency_by_name(u, UNIT_AFTER, SPECIAL_LOGGER_SOCKET)) < 0)
1263                                 return k;
1264
1265                         if (u->meta.manager->running_as != MANAGER_SESSION)
1266                                 if ((k = unit_add_dependency_by_name(u, UNIT_REQUIRES, SPECIAL_LOGGER_SOCKET)) < 0)
1267                                         return k;
1268                 }
1269         }
1270
1271         return r;
1272 }