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 "alloc-util.h"
23 #include "conf-files.h"
27 #include "fileio-label.h"
29 #include "path-util.h"
30 #include "string-util.h"
34 int drop_in_file(const char *dir
, const char *unit
, unsigned level
,
35 const char *name
, char **_p
, char **_q
) {
37 _cleanup_free_
char *b
= NULL
;
40 char prefix
[DECIMAL_STR_MAX(unsigned)];
47 sprintf(prefix
, "%u", level
);
49 b
= xescape(name
, "/.");
53 if (!filename_is_valid(b
))
56 p
= strjoin(dir
, "/", unit
, ".d", NULL
);
60 q
= strjoin(p
, "/", prefix
, "-", b
, ".conf", NULL
);
71 int write_drop_in(const char *dir
, const char *unit
, unsigned level
,
72 const char *name
, const char *data
) {
74 _cleanup_free_
char *p
= NULL
, *q
= NULL
;
82 r
= drop_in_file(dir
, unit
, level
, name
, &p
, &q
);
86 (void) mkdir_p(p
, 0755);
87 return write_string_file_atomic_label(q
, data
);
90 int write_drop_in_format(const char *dir
, const char *unit
, unsigned level
,
91 const char *name
, const char *format
, ...) {
92 _cleanup_free_
char *p
= NULL
;
101 va_start(ap
, format
);
102 r
= vasprintf(&p
, format
, ap
);
108 return write_drop_in(dir
, unit
, level
, name
, p
);
111 static int iterate_dir(
113 UnitDependency dependency
,
114 dependency_consumer_t consumer
,
118 _cleanup_closedir_
DIR *d
= NULL
;
123 /* The config directories are special, since the order of the
124 * drop-ins matters */
125 if (dependency
< 0) {
126 r
= strv_extend(strv
, path
);
140 return log_error_errno(errno
, "Failed to open directory %s: %m", path
);
145 _cleanup_free_
char *f
= NULL
;
149 if (!de
&& errno
!= 0)
150 return log_error_errno(errno
, "Failed to read directory %s: %m", path
);
155 if (hidden_file(de
->d_name
))
158 f
= strjoin(path
, "/", de
->d_name
, NULL
);
162 r
= consumer(dependency
, de
->d_name
, f
, arg
);
170 int unit_file_process_dir(
171 Set
*unit_path_cache
,
172 const char *unit_path
,
175 UnitDependency dependency
,
176 dependency_consumer_t consumer
,
180 _cleanup_free_
char *path
= NULL
;
187 path
= strjoin(unit_path
, "/", name
, suffix
, NULL
);
191 if (!unit_path_cache
|| set_get(unit_path_cache
, path
))
192 (void) iterate_dir(path
, dependency
, consumer
, arg
, strv
);
194 if (unit_name_is_valid(name
, UNIT_NAME_INSTANCE
)) {
195 _cleanup_free_
char *template = NULL
, *p
= NULL
;
196 /* Also try the template dir */
198 r
= unit_name_template(name
, &template);
200 return log_error_errno(r
, "Failed to generate template from unit name: %m");
202 p
= strjoin(unit_path
, "/", template, suffix
, NULL
);
206 if (!unit_path_cache
|| set_get(unit_path_cache
, p
))
207 (void) iterate_dir(p
, dependency
, consumer
, arg
, strv
);
213 int unit_file_find_dropin_paths(
215 Set
*unit_path_cache
,
219 _cleanup_strv_free_
char **strv
= NULL
, **ans
= NULL
;
226 SET_FOREACH(t
, names
, i
) {
229 STRV_FOREACH(p
, lookup_path
)
230 unit_file_process_dir(unit_path_cache
, *p
, t
, ".d", _UNIT_DEPENDENCY_INVALID
, NULL
, NULL
, &strv
);
233 if (strv_isempty(strv
))
236 r
= conf_files_list_strv(&ans
, ".conf", NULL
, (const char**) strv
);
238 return log_warning_errno(r
, "Failed to get list of configuration files: %m");