]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/dropin.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2014 Zbigniew Jędrzejewski-Szmek
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 #include "alloc-util.h"
27 #include "conf-files.h"
28 #include "dirent-util.h"
32 #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 char prefix
[DECIMAL_STR_MAX(unsigned)];
48 _cleanup_free_
char *b
= NULL
;
56 sprintf(prefix
, "%u", level
);
58 b
= xescape(name
, "/.");
62 if (!filename_is_valid(b
))
65 p
= strjoin(dir
, "/", unit
, ".d");
69 q
= strjoin(p
, "/", prefix
, "-", b
, ".conf");
80 int write_drop_in(const char *dir
, const char *unit
, unsigned level
,
81 const char *name
, const char *data
) {
83 _cleanup_free_
char *p
= NULL
, *q
= NULL
;
91 r
= drop_in_file(dir
, unit
, level
, name
, &p
, &q
);
95 (void) mkdir_p(p
, 0755);
96 return write_string_file_atomic_label(q
, data
);
99 int write_drop_in_format(const char *dir
, const char *unit
, unsigned level
,
100 const char *name
, const char *format
, ...) {
101 _cleanup_free_
char *p
= NULL
;
110 va_start(ap
, format
);
111 r
= vasprintf(&p
, format
, ap
);
117 return write_drop_in(dir
, unit
, level
, name
, p
);
120 static int unit_file_find_dir(
121 const char *original_root
,
125 _cleanup_free_
char *chased
= NULL
;
130 r
= chase_symlinks(path
, original_root
, 0, &chased
);
131 if (r
== -ENOENT
) /* Ignore -ENOENT, after all most units won't have a drop-in dir */
134 return log_full_errno(LOG_WARNING
, r
, "Failed to canonicalize path %s: %m", path
);
136 r
= strv_push(dirs
, chased
);
144 static int unit_file_find_dirs(
145 const char *original_root
,
146 Set
*unit_path_cache
,
147 const char *unit_path
,
159 path
= strjoina(unit_path
, "/", name
, suffix
);
161 if (!unit_path_cache
|| set_get(unit_path_cache
, path
)) {
162 r
= unit_file_find_dir(original_root
, path
, dirs
);
167 if (unit_name_is_valid(name
, UNIT_NAME_INSTANCE
)) {
168 /* Also try the template dir */
170 _cleanup_free_
char *template = NULL
;
172 r
= unit_name_template(name
, &template);
174 return log_error_errno(r
, "Failed to generate template from unit name: %m");
176 return unit_file_find_dirs(original_root
, unit_path_cache
, unit_path
, template, suffix
, dirs
);
182 int unit_file_find_dropin_paths(
183 const char *original_root
,
185 Set
*unit_path_cache
,
186 const char *dir_suffix
,
187 const char *file_suffix
,
191 _cleanup_strv_free_
char **dirs
= NULL
;
198 SET_FOREACH(t
, names
, i
)
199 STRV_FOREACH(p
, lookup_path
)
200 unit_file_find_dirs(original_root
, unit_path_cache
, *p
, t
, dir_suffix
, &dirs
);
202 if (strv_isempty(dirs
)) {
207 r
= conf_files_list_strv(ret
, file_suffix
, NULL
, 0, (const char**) dirs
);
209 return log_warning_errno(r
, "Failed to create the list of configuration files: %m");