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/>.
22 #include "conf-files.h"
24 #include "fileio-label.h"
26 #include "string-util.h"
31 int drop_in_file(const char *dir
, const char *unit
, unsigned level
,
32 const char *name
, char **_p
, char **_q
) {
34 _cleanup_free_
char *b
= NULL
;
37 char prefix
[DECIMAL_STR_MAX(unsigned)];
44 sprintf(prefix
, "%u", level
);
46 b
= xescape(name
, "/.");
50 if (!filename_is_valid(b
))
53 p
= strjoin(dir
, "/", unit
, ".d", NULL
);
57 q
= strjoin(p
, "/", prefix
, "-", b
, ".conf", NULL
);
68 int write_drop_in(const char *dir
, const char *unit
, unsigned level
,
69 const char *name
, const char *data
) {
71 _cleanup_free_
char *p
= NULL
, *q
= NULL
;
79 r
= drop_in_file(dir
, unit
, level
, name
, &p
, &q
);
83 (void) mkdir_p(p
, 0755);
84 return write_string_file_atomic_label(q
, data
);
87 int write_drop_in_format(const char *dir
, const char *unit
, unsigned level
,
88 const char *name
, const char *format
, ...) {
89 _cleanup_free_
char *p
= NULL
;
99 r
= vasprintf(&p
, format
, ap
);
105 return write_drop_in(dir
, unit
, level
, name
, p
);
108 static int iterate_dir(
110 UnitDependency dependency
,
111 dependency_consumer_t consumer
,
115 _cleanup_closedir_
DIR *d
= NULL
;
120 /* The config directories are special, since the order of the
121 * drop-ins matters */
122 if (dependency
< 0) {
123 r
= strv_extend(strv
, path
);
137 return log_error_errno(errno
, "Failed to open directory %s: %m", path
);
142 _cleanup_free_
char *f
= NULL
;
146 if (!de
&& errno
!= 0)
147 return log_error_errno(errno
, "Failed to read directory %s: %m", path
);
152 if (hidden_file(de
->d_name
))
155 f
= strjoin(path
, "/", de
->d_name
, NULL
);
159 r
= consumer(dependency
, de
->d_name
, f
, arg
);
167 int unit_file_process_dir(
168 Set
*unit_path_cache
,
169 const char *unit_path
,
172 UnitDependency dependency
,
173 dependency_consumer_t consumer
,
177 _cleanup_free_
char *path
= NULL
;
184 path
= strjoin(unit_path
, "/", name
, suffix
, NULL
);
188 if (!unit_path_cache
|| set_get(unit_path_cache
, path
))
189 (void) iterate_dir(path
, dependency
, consumer
, arg
, strv
);
191 if (unit_name_is_valid(name
, UNIT_NAME_INSTANCE
)) {
192 _cleanup_free_
char *template = NULL
, *p
= NULL
;
193 /* Also try the template dir */
195 r
= unit_name_template(name
, &template);
197 return log_error_errno(r
, "Failed to generate template from unit name: %m");
199 p
= strjoin(unit_path
, "/", template, suffix
, NULL
);
203 if (!unit_path_cache
|| set_get(unit_path_cache
, p
))
204 (void) iterate_dir(p
, dependency
, consumer
, arg
, strv
);
210 int unit_file_find_dropin_paths(
212 Set
*unit_path_cache
,
216 _cleanup_strv_free_
char **strv
= NULL
, **ans
= NULL
;
223 SET_FOREACH(t
, names
, i
) {
226 STRV_FOREACH(p
, lookup_path
)
227 unit_file_process_dir(unit_path_cache
, *p
, t
, ".d", _UNIT_DEPENDENCY_INVALID
, NULL
, NULL
, &strv
);
230 if (strv_isempty(strv
))
233 r
= conf_files_list_strv(&ans
, ".conf", NULL
, (const char**) strv
);
235 return log_warning_errno(r
, "Failed to get list of configuration files: %m");