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(Unit *u, const char *path, UnitDependency dependency, char ***strv) {
35 _cleanup_closedir_ DIR *d = NULL;
41 /* The config directories are special, since the order of the
44 r = strv_extend(strv, path);
61 union dirent_storage buf;
62 _cleanup_free_ char *f = NULL;
65 k = readdir_r(d, &buf.de, &de);
67 log_error("Failed to read directory %s: %s", path, strerror(k));
74 if (ignore_file(de->d_name))
77 f = strjoin(path, "/", de->d_name, NULL);
81 r = unit_add_dependency_by_name(u, dependency, de->d_name, f, true);
83 log_error("Cannot add dependency %s to %s, ignoring: %s", de->d_name, u->id, strerror(-r));
89 static int process_dir(Unit *u, const char *unit_path, const char *name, const char *suffix, UnitDependency dependency, char ***strv) {
98 path = strjoin(unit_path, "/", name, suffix, NULL);
102 if (u->manager->unit_path_cache &&
103 !set_get(u->manager->unit_path_cache, path))
106 r = iterate_dir(u, path, dependency, strv);
114 /* Also try the template dir */
116 template = unit_name_template(name);
120 path = strjoin(unit_path, "/", template, suffix, NULL);
126 if (u->manager->unit_path_cache &&
127 !set_get(u->manager->unit_path_cache, path))
130 r = iterate_dir(u, path, dependency, strv);
140 int unit_load_dropin(Unit *u) {
143 _cleanup_strv_free_ char **strv = NULL;
149 /* Load dependencies from supplementary drop-in directories */
151 SET_FOREACH(t, u->names, i) {
154 STRV_FOREACH(p, u->manager->lookup_paths.unit_path) {
155 r = process_dir(u, *p, t, ".wants", UNIT_WANTS, NULL);
159 r = process_dir(u, *p, t, ".requires", UNIT_REQUIRES, NULL);
163 /* This loads the drop-in config snippets */
164 r = process_dir(u, *p, t, ".d", _UNIT_DEPENDENCY_INVALID, &strv);
170 if (!strv_isempty(strv)) {
171 _cleanup_strv_free_ char **files = NULL;
174 r = conf_files_list_strv(&files, ".conf", NULL, (const char**) strv);
176 log_error("Failed to get list of configuration files: %s", strerror(-r));
180 STRV_FOREACH(f, files) {
181 r = config_parse(*f, NULL, UNIT_VTABLE(u)->sections, config_item_perf_lookup, (void*) load_fragment_gperf_lookup, false, u);