]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/load-dropin.c
systemctl: if we managed to reexec the init system via the bus don't retry via signal
[thirdparty/systemd.git] / src / load-dropin.c
CommitLineData
d6c9574f 1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
5cb5a6ff 2
a7334b09
LP
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
0301abf4
LP
22#include <dirent.h>
23#include <errno.h>
24
25#include "unit.h"
5cb5a6ff 26#include "load-dropin.h"
b952f2e1 27#include "log.h"
036643a2 28#include "strv.h"
9e2f7c11
LP
29#include "unit-name.h"
30
99f08d14 31static int iterate_dir(Unit *u, const char *path, UnitDependency dependency) {
9e2f7c11
LP
32 DIR *d;
33 struct dirent *de;
34 int r;
35
99f08d14
LP
36 assert(u);
37 assert(path);
38
9e2f7c11
LP
39 if (!(d = opendir(path))) {
40
41 if (errno == ENOENT)
42 return 0;
43
44 return -errno;
45 }
46
47 while ((de = readdir(d))) {
48 char *f;
49
50 if (ignore_file(de->d_name))
51 continue;
52
44d91056
LP
53 f = join(path, "/", de->d_name, NULL);
54 if (!f) {
9e2f7c11
LP
55 r = -ENOMEM;
56 goto finish;
57 }
58
99f08d14 59 r = unit_add_dependency_by_name(u, dependency, de->d_name, f, true);
9e2f7c11
LP
60 free(f);
61
62 if (r < 0)
63 goto finish;
64 }
65
66 r = 0;
67
68finish:
69 closedir(d);
70 return r;
71}
5cb5a6ff 72
99f08d14 73static int process_dir(Unit *u, const char *unit_path, const char *name, const char *suffix, UnitDependency dependency) {
0301abf4 74 int r;
99f08d14 75 char *path;
0301abf4 76
87f0e418 77 assert(u);
99f08d14
LP
78 assert(unit_path);
79 assert(name);
80 assert(suffix);
5cb5a6ff 81
911a4828
LP
82 path = join(unit_path, "/", name, suffix, NULL);
83 if (!path)
99f08d14 84 return -ENOMEM;
5cb5a6ff 85
99f08d14
LP
86 if (u->meta.manager->unit_path_cache &&
87 !set_get(u->meta.manager->unit_path_cache, path))
88 r = 0;
89 else
90 r = iterate_dir(u, path, dependency);
91 free(path);
0301abf4 92
99f08d14
LP
93 if (r < 0)
94 return r;
0301abf4 95
99f08d14
LP
96 if (u->meta.instance) {
97 char *template;
98 /* Also try the template dir */
0301abf4 99
99f08d14
LP
100 if (!(template = unit_name_template(name)))
101 return -ENOMEM;
0301abf4 102
cd0ed1db 103 path = join(unit_path, "/", template, suffix, NULL);
99f08d14 104 free(template);
0301abf4 105
cd0ed1db 106 if (!path)
99f08d14 107 return -ENOMEM;
0301abf4 108
99f08d14
LP
109 if (u->meta.manager->unit_path_cache &&
110 !set_get(u->meta.manager->unit_path_cache, path))
111 r = 0;
112 else
113 r = iterate_dir(u, path, dependency);
114 free(path);
0301abf4 115
99f08d14
LP
116 if (r < 0)
117 return r;
118 }
0301abf4 119
99f08d14
LP
120 return 0;
121}
0301abf4 122
99f08d14
LP
123int unit_load_dropin(Unit *u) {
124 Iterator i;
125 char *t;
126
127 assert(u);
128
129 /* Load dependencies from supplementary drop-in directories */
036643a2 130
99f08d14
LP
131 SET_FOREACH(t, u->meta.names, i) {
132 char **p;
133
134 STRV_FOREACH(p, u->meta.manager->lookup_paths.unit_path) {
135 int r;
0301abf4 136
99f08d14
LP
137 if ((r = process_dir(u, *p, t, ".wants", UNIT_WANTS)) < 0)
138 return r;
139
140 if ((r = process_dir(u, *p, t, ".requires", UNIT_REQUIRES)) < 0)
141 return r;
036643a2 142 }
0301abf4
LP
143 }
144
5cb5a6ff
LP
145 return 0;
146}