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/>.
28 #include "alloc-util.h"
29 #include "conf-files.h"
33 #include "fileio-label.h"
38 #include "path-util.h"
40 #include "string-util.h"
42 #include "unit-name.h"
44 int drop_in_file(const char *dir
, const char *unit
, unsigned level
,
45 const char *name
, char **_p
, char **_q
) {
47 _cleanup_free_
char *b
= NULL
;
50 char prefix
[DECIMAL_STR_MAX(unsigned)];
57 sprintf(prefix
, "%u", level
);
59 b
= xescape(name
, "/.");
63 if (!filename_is_valid(b
))
66 p
= strjoin(dir
, "/", unit
, ".d", NULL
);
70 q
= strjoin(p
, "/", prefix
, "-", b
, ".conf", NULL
);
81 int write_drop_in(const char *dir
, const char *unit
, unsigned level
,
82 const char *name
, const char *data
) {
84 _cleanup_free_
char *p
= NULL
, *q
= NULL
;
92 r
= drop_in_file(dir
, unit
, level
, name
, &p
, &q
);
96 (void) mkdir_p(p
, 0755);
97 return write_string_file_atomic_label(q
, data
);
100 int write_drop_in_format(const char *dir
, const char *unit
, unsigned level
,
101 const char *name
, const char *format
, ...) {
102 _cleanup_free_
char *p
= NULL
;
111 va_start(ap
, format
);
112 r
= vasprintf(&p
, format
, ap
);
118 return write_drop_in(dir
, unit
, level
, name
, p
);
121 static int iterate_dir(
123 UnitDependency dependency
,
124 dependency_consumer_t consumer
,
128 _cleanup_closedir_
DIR *d
= NULL
;
133 /* The config directories are special, since the order of the
134 * drop-ins matters */
135 if (dependency
< 0) {
136 r
= strv_extend(strv
, path
);
150 return log_error_errno(errno
, "Failed to open directory %s: %m", path
);
155 _cleanup_free_
char *f
= NULL
;
159 if (!de
&& errno
> 0)
160 return log_error_errno(errno
, "Failed to read directory %s: %m", path
);
165 if (hidden_file(de
->d_name
))
168 f
= strjoin(path
, "/", de
->d_name
, NULL
);
172 r
= consumer(dependency
, de
->d_name
, f
, arg
);
180 int unit_file_process_dir(
181 Set
*unit_path_cache
,
182 const char *unit_path
,
185 UnitDependency dependency
,
186 dependency_consumer_t consumer
,
190 _cleanup_free_
char *path
= NULL
;
197 path
= strjoin(unit_path
, "/", name
, suffix
, NULL
);
201 if (!unit_path_cache
|| set_get(unit_path_cache
, path
))
202 (void) iterate_dir(path
, dependency
, consumer
, arg
, strv
);
204 if (unit_name_is_valid(name
, UNIT_NAME_INSTANCE
)) {
205 _cleanup_free_
char *template = NULL
, *p
= NULL
;
206 /* Also try the template dir */
208 r
= unit_name_template(name
, &template);
210 return log_error_errno(r
, "Failed to generate template from unit name: %m");
212 p
= strjoin(unit_path
, "/", template, suffix
, NULL
);
216 if (!unit_path_cache
|| set_get(unit_path_cache
, p
))
217 (void) iterate_dir(p
, dependency
, consumer
, arg
, strv
);
223 int unit_file_find_dropin_paths(
225 Set
*unit_path_cache
,
229 _cleanup_strv_free_
char **strv
= NULL
, **ans
= NULL
;
236 SET_FOREACH(t
, names
, i
) {
239 STRV_FOREACH(p
, lookup_path
)
240 unit_file_process_dir(unit_path_cache
, *p
, t
, ".d", _UNIT_DEPENDENCY_INVALID
, NULL
, NULL
, &strv
);
243 if (strv_isempty(strv
))
246 r
= conf_files_list_strv(&ans
, ".conf", NULL
, (const char**) strv
);
248 return log_warning_errno(r
, "Failed to get list of configuration files: %m");