1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
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 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 #include "load-dropin.h"
29 #include "unit-name.h"
30 #include "conf-parser.h"
31 #include "load-fragment.h"
32 #include "conf-files.h"
34 static int iterate_dir(
37 UnitDependency dependency,
40 _cleanup_closedir_ DIR *d = NULL;
46 /* The config directories are special, since the order of the
49 r = strv_extend(strv, path);
61 log_error("Failed to open directory %s: %m", path);
67 _cleanup_free_ char *f = NULL;
72 if (!de && errno != 0) {
74 log_error("Failed to read directory %s: %s", path, strerror(k));
81 if (ignore_file(de->d_name))
84 f = strjoin(path, "/", de->d_name, NULL);
88 r = unit_add_dependency_by_name(u, dependency, de->d_name, f, true);
90 log_error("Cannot add dependency %s to %s, ignoring: %s", de->d_name, u->id, strerror(-r));
96 static int process_dir(
98 const char *unit_path,
101 UnitDependency dependency,
104 _cleanup_free_ char *path = NULL;
111 path = strjoin(unit_path, "/", name, suffix, NULL);
115 if (!u->manager->unit_path_cache || set_get(u->manager->unit_path_cache, path))
116 iterate_dir(u, path, dependency, strv);
119 _cleanup_free_ char *template = NULL, *p = NULL;
120 /* Also try the template dir */
122 template = unit_name_template(name);
126 p = strjoin(unit_path, "/", template, suffix, NULL);
130 if (!u->manager->unit_path_cache || set_get(u->manager->unit_path_cache, p))
131 iterate_dir(u, p, dependency, strv);
137 char **unit_find_dropin_paths(Unit *u) {
138 _cleanup_strv_free_ char **strv = NULL;
139 char **configs = NULL;
146 SET_FOREACH(t, u->names, i) {
149 STRV_FOREACH(p, u->manager->lookup_paths.unit_path)
150 process_dir(u, *p, t, ".d", _UNIT_DEPENDENCY_INVALID, &strv);
153 if (strv_isempty(strv))
156 r = conf_files_list_strv(&configs, ".conf", NULL, (const char**) strv);
158 log_error("Failed to get list of configuration files: %s", strerror(-r));
166 int unit_load_dropin(Unit *u) {
172 /* Load dependencies from supplementary drop-in directories */
174 SET_FOREACH(t, u->names, i) {
177 STRV_FOREACH(p, u->manager->lookup_paths.unit_path) {
178 process_dir(u, *p, t, ".wants", UNIT_WANTS, NULL);
179 process_dir(u, *p, t, ".requires", UNIT_REQUIRES, NULL);
183 u->dropin_paths = unit_find_dropin_paths(u);
184 if (!u->dropin_paths)
187 STRV_FOREACH(f, u->dropin_paths) {
188 config_parse(u->id, *f, NULL,
189 UNIT_VTABLE(u)->sections,
190 config_item_perf_lookup, load_fragment_gperf_lookup,
191 false, false, false, u);
194 u->dropin_mtime = now(CLOCK_REALTIME);