]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/dropin.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / shared / dropin.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2014 Zbigniew Jędrzejewski-Szmek
6
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.
11
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.
16
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/>.
19 ***/
20
21 #include <errno.h>
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25
26 #include "alloc-util.h"
27 #include "conf-files.h"
28 #include "dirent-util.h"
29 #include "dropin.h"
30 #include "escape.h"
31 #include "fd-util.h"
32 #include "fileio-label.h"
33 #include "fs-util.h"
34 #include "hashmap.h"
35 #include "log.h"
36 #include "macro.h"
37 #include "mkdir.h"
38 #include "path-util.h"
39 #include "set.h"
40 #include "string-util.h"
41 #include "strv.h"
42 #include "unit-name.h"
43
44 int drop_in_file(const char *dir, const char *unit, unsigned level,
45 const char *name, char **_p, char **_q) {
46
47 char prefix[DECIMAL_STR_MAX(unsigned)];
48 _cleanup_free_ char *b = NULL;
49 char *p, *q;
50
51 assert(unit);
52 assert(name);
53 assert(_p);
54 assert(_q);
55
56 sprintf(prefix, "%u", level);
57
58 b = xescape(name, "/.");
59 if (!b)
60 return -ENOMEM;
61
62 if (!filename_is_valid(b))
63 return -EINVAL;
64
65 p = strjoin(dir, "/", unit, ".d");
66 if (!p)
67 return -ENOMEM;
68
69 q = strjoin(p, "/", prefix, "-", b, ".conf");
70 if (!q) {
71 free(p);
72 return -ENOMEM;
73 }
74
75 *_p = p;
76 *_q = q;
77 return 0;
78 }
79
80 int write_drop_in(const char *dir, const char *unit, unsigned level,
81 const char *name, const char *data) {
82
83 _cleanup_free_ char *p = NULL, *q = NULL;
84 int r;
85
86 assert(dir);
87 assert(unit);
88 assert(name);
89 assert(data);
90
91 r = drop_in_file(dir, unit, level, name, &p, &q);
92 if (r < 0)
93 return r;
94
95 (void) mkdir_p(p, 0755);
96 return write_string_file_atomic_label(q, data);
97 }
98
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;
102 va_list ap;
103 int r;
104
105 assert(dir);
106 assert(unit);
107 assert(name);
108 assert(format);
109
110 va_start(ap, format);
111 r = vasprintf(&p, format, ap);
112 va_end(ap);
113
114 if (r < 0)
115 return -ENOMEM;
116
117 return write_drop_in(dir, unit, level, name, p);
118 }
119
120 static int unit_file_find_dir(
121 const char *original_root,
122 const char *path,
123 char ***dirs) {
124
125 _cleanup_free_ char *chased = NULL;
126 int r;
127
128 assert(path);
129
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 */
132 return 0;
133 if (r < 0)
134 return log_full_errno(LOG_WARNING, r, "Failed to canonicalize path %s: %m", path);
135
136 r = strv_push(dirs, chased);
137 if (r < 0)
138 return log_oom();
139
140 chased = NULL;
141 return 0;
142 }
143
144 static int unit_file_find_dirs(
145 const char *original_root,
146 Set *unit_path_cache,
147 const char *unit_path,
148 const char *name,
149 const char *suffix,
150 char ***dirs) {
151
152 char *path;
153 int r;
154
155 assert(unit_path);
156 assert(name);
157 assert(suffix);
158
159 path = strjoina(unit_path, "/", name, suffix);
160
161 if (!unit_path_cache || set_get(unit_path_cache, path)) {
162 r = unit_file_find_dir(original_root, path, dirs);
163 if (r < 0)
164 return r;
165 }
166
167 if (unit_name_is_valid(name, UNIT_NAME_INSTANCE)) {
168 /* Also try the template dir */
169
170 _cleanup_free_ char *template = NULL;
171
172 r = unit_name_template(name, &template);
173 if (r < 0)
174 return log_error_errno(r, "Failed to generate template from unit name: %m");
175
176 return unit_file_find_dirs(original_root, unit_path_cache, unit_path, template, suffix, dirs);
177 }
178
179 return 0;
180 }
181
182 int unit_file_find_dropin_paths(
183 const char *original_root,
184 char **lookup_path,
185 Set *unit_path_cache,
186 const char *dir_suffix,
187 const char *file_suffix,
188 Set *names,
189 char ***ret) {
190
191 _cleanup_strv_free_ char **dirs = NULL;
192 Iterator i;
193 char *t, **p;
194 int r;
195
196 assert(ret);
197
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);
201
202 if (strv_isempty(dirs)) {
203 *ret = NULL;
204 return 0;
205 }
206
207 r = conf_files_list_strv(ret, file_suffix, NULL, 0, (const char**) dirs);
208 if (r < 0)
209 return log_warning_errno(r, "Failed to create the list of configuration files: %m");
210
211 return 1;
212 }