1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2014 Zbigniew Jędrzejewski-Szmek
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 "fileio-label.h"
27 #include "conf-files.h"
29 int drop_in_file(const char *dir, const char *unit, unsigned level,
30 const char *name, char **_p, char **_q) {
32 _cleanup_free_ char *b = NULL;
35 char prefix[DECIMAL_STR_MAX(unsigned)];
42 sprintf(prefix, "%u", level);
44 b = xescape(name, "/.");
48 if (!filename_is_valid(b))
51 p = strjoin(dir, "/", unit, ".d", NULL);
55 q = strjoin(p, "/", prefix, "-", b, ".conf", NULL);
66 int write_drop_in(const char *dir, const char *unit, unsigned level,
67 const char *name, const char *data) {
69 _cleanup_free_ char *p = NULL, *q = NULL;
77 r = drop_in_file(dir, unit, level, name, &p, &q);
82 return write_string_file_atomic_label(q, data);
85 int write_drop_in_format(const char *dir, const char *unit, unsigned level,
86 const char *name, const char *format, ...) {
87 _cleanup_free_ char *p = NULL;
97 r = vasprintf(&p, format, ap);
103 return write_drop_in(dir, unit, level, name, p);
106 static int iterate_dir(
108 UnitDependency dependency,
109 dependency_consumer_t consumer,
113 _cleanup_closedir_ DIR *d = NULL;
118 /* The config directories are special, since the order of the
119 * drop-ins matters */
120 if (dependency < 0) {
121 r = strv_extend(strv, path);
135 log_error_errno(errno, "Failed to open directory %s: %m", path);
141 _cleanup_free_ char *f = NULL;
145 if (!de && errno != 0)
146 return log_error_errno(errno, "Failed to read directory %s: %m", path);
151 if (hidden_file(de->d_name))
154 f = strjoin(path, "/", de->d_name, NULL);
158 r = consumer(dependency, de->d_name, f, arg);
166 int unit_file_process_dir(
167 Set *unit_path_cache,
168 const char *unit_path,
171 UnitDependency dependency,
172 dependency_consumer_t consumer,
176 _cleanup_free_ char *path = NULL;
183 path = strjoin(unit_path, "/", name, suffix, NULL);
187 if (!unit_path_cache || set_get(unit_path_cache, path))
188 (void) iterate_dir(path, dependency, consumer, arg, strv);
190 if (unit_name_is_valid(name, UNIT_NAME_INSTANCE)) {
191 _cleanup_free_ char *template = NULL, *p = NULL;
192 /* Also try the template dir */
194 r = unit_name_template(name, &template);
196 return log_error_errno(r, "Failed to generate template from unit name: %m");
198 p = strjoin(unit_path, "/", template, suffix, NULL);
202 if (!unit_path_cache || set_get(unit_path_cache, p))
203 (void) iterate_dir(p, dependency, consumer, arg, strv);
209 int unit_file_find_dropin_paths(
211 Set *unit_path_cache,
215 _cleanup_strv_free_ char **strv = NULL, **ans = NULL;
222 SET_FOREACH(t, names, i) {
225 STRV_FOREACH(p, lookup_path)
226 unit_file_process_dir(unit_path_cache, *p, t, ".d", _UNIT_DEPENDENCY_INVALID, NULL, NULL, &strv);
229 if (strv_isempty(strv))
232 r = conf_files_list_strv(&ans, ".conf", NULL, (const char**) strv);
234 return log_warning_errno(r, "Failed to get list of configuration files: %m");