]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/dropin.c
tree-wide: drop 'This file is part of systemd' blurb
[thirdparty/systemd.git] / src / shared / dropin.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
29686440 2/***
29686440 3 Copyright 2014 Zbigniew Jędrzejewski-Szmek
29686440
ZJS
4***/
5
a8fbdf54
TA
6#include <errno.h>
7#include <stdarg.h>
8#include <stdio.h>
9#include <stdlib.h>
10
b5efdb8a 11#include "alloc-util.h"
1a7f1b38 12#include "conf-files.h"
8fb3f009 13#include "dirent-util.h"
3ffd4af2 14#include "dropin.h"
4f5dd394 15#include "escape.h"
3ffd4af2 16#include "fd-util.h"
4f5dd394 17#include "fileio-label.h"
17e78d18 18#include "fs-util.h"
a8fbdf54
TA
19#include "hashmap.h"
20#include "log.h"
21#include "macro.h"
4f5dd394 22#include "mkdir.h"
bb15fafe 23#include "path-util.h"
a8fbdf54 24#include "set.h"
07630cea 25#include "string-util.h"
4f5dd394 26#include "strv.h"
a8fbdf54 27#include "unit-name.h"
29686440 28
8eea8687 29int drop_in_file(const char *dir, const char *unit, unsigned level,
29686440
ZJS
30 const char *name, char **_p, char **_q) {
31
09c62487 32 char prefix[DECIMAL_STR_MAX(unsigned)];
29686440
ZJS
33 _cleanup_free_ char *b = NULL;
34 char *p, *q;
35
36 assert(unit);
37 assert(name);
38 assert(_p);
39 assert(_q);
40
8eea8687
ZJS
41 sprintf(prefix, "%u", level);
42
29686440
ZJS
43 b = xescape(name, "/.");
44 if (!b)
45 return -ENOMEM;
46
ae6c3cc0 47 if (!filename_is_valid(b))
29686440
ZJS
48 return -EINVAL;
49
605405c6 50 p = strjoin(dir, "/", unit, ".d");
29686440
ZJS
51 if (!p)
52 return -ENOMEM;
53
605405c6 54 q = strjoin(p, "/", prefix, "-", b, ".conf");
29686440
ZJS
55 if (!q) {
56 free(p);
57 return -ENOMEM;
58 }
59
60 *_p = p;
61 *_q = q;
62 return 0;
63}
64
8eea8687 65int write_drop_in(const char *dir, const char *unit, unsigned level,
29686440
ZJS
66 const char *name, const char *data) {
67
68 _cleanup_free_ char *p = NULL, *q = NULL;
69 int r;
70
71 assert(dir);
72 assert(unit);
73 assert(name);
74 assert(data);
75
8eea8687 76 r = drop_in_file(dir, unit, level, name, &p, &q);
29686440
ZJS
77 if (r < 0)
78 return r;
79
45519fd6 80 (void) mkdir_p(p, 0755);
29686440
ZJS
81 return write_string_file_atomic_label(q, data);
82}
83
8eea8687 84int write_drop_in_format(const char *dir, const char *unit, unsigned level,
29686440
ZJS
85 const char *name, const char *format, ...) {
86 _cleanup_free_ char *p = NULL;
87 va_list ap;
88 int r;
89
90 assert(dir);
91 assert(unit);
92 assert(name);
93 assert(format);
94
95 va_start(ap, format);
96 r = vasprintf(&p, format, ap);
97 va_end(ap);
98
99 if (r < 0)
100 return -ENOMEM;
101
8eea8687 102 return write_drop_in(dir, unit, level, name, p);
29686440 103}
1a7f1b38 104
dcc4f30e 105static int unit_file_find_dir(
17e78d18 106 const char *original_root,
dcc4f30e
ZJS
107 const char *path,
108 char ***dirs) {
1a7f1b38 109
17e78d18 110 _cleanup_free_ char *chased = NULL;
1a7f1b38
ZJS
111 int r;
112
113 assert(path);
114
17e78d18 115 r = chase_symlinks(path, original_root, 0, &chased);
952713b0 116 if (r == -ENOENT) /* Ignore -ENOENT, after all most units won't have a drop-in dir. */
09c62487 117 return 0;
952713b0
LP
118 if (r == -ENAMETOOLONG) {
119 /* Also, ignore -ENAMETOOLONG but log about it. After all, users are not even able to create the
120 * drop-in dir in such case. This mostly happens for device units with an overly long /sys path. */
121 log_debug_errno(r, "Path '%s' too long, couldn't canonicalize, ignoring.", path);
122 return 0;
123 }
17e78d18 124 if (r < 0)
952713b0 125 return log_warning_errno(r, "Failed to canonicalize path '%s': %m", path);
17e78d18 126
dcc4f30e
ZJS
127 r = strv_push(dirs, chased);
128 if (r < 0)
129 return log_oom();
1a7f1b38 130
dcc4f30e 131 chased = NULL;
1a7f1b38
ZJS
132 return 0;
133}
134
dcc4f30e 135static int unit_file_find_dirs(
17e78d18 136 const char *original_root,
7410616c 137 Set *unit_path_cache,
1a7f1b38
ZJS
138 const char *unit_path,
139 const char *name,
140 const char *suffix,
dcc4f30e 141 char ***dirs) {
1a7f1b38 142
53966245
LP
143 _cleanup_free_ char *prefix = NULL, *instance = NULL, *built = NULL;
144 bool is_instance, chopped;
145 const char *dash;
146 UnitType type;
96bb2fd8 147 char *path;
53966245 148 size_t n;
7410616c 149 int r;
1a7f1b38
ZJS
150
151 assert(unit_path);
152 assert(name);
153 assert(suffix);
154
96bb2fd8 155 path = strjoina(unit_path, "/", name, suffix);
dcc4f30e
ZJS
156 if (!unit_path_cache || set_get(unit_path_cache, path)) {
157 r = unit_file_find_dir(original_root, path, dirs);
158 if (r < 0)
159 return r;
160 }
1a7f1b38 161
53966245
LP
162 is_instance = unit_name_is_valid(name, UNIT_NAME_INSTANCE);
163 if (is_instance) { /* Also try the template dir */
a09d3eaf
LP
164 _cleanup_free_ char *template = NULL;
165
7410616c
LP
166 r = unit_name_template(name, &template);
167 if (r < 0)
168 return log_error_errno(r, "Failed to generate template from unit name: %m");
1a7f1b38 169
53966245
LP
170 r = unit_file_find_dirs(original_root, unit_path_cache, unit_path, template, suffix, dirs);
171 if (r < 0)
172 return r;
1a7f1b38
ZJS
173 }
174
53966245
LP
175 /* Let's see if there's a "-" prefix for this unit name. If so, let's invoke ourselves for it. This will then
176 * recursively do the same for all our prefixes. i.e. this means given "foo-bar-waldo.service" we'll also
177 * search "foo-bar-.service" and "foo-.service".
178 *
179 * Note the order in which we do it: we traverse up adding drop-ins on each step. This means the more specific
180 * drop-ins may override the more generic drop-ins, which is the intended behaviour. */
181
182 r = unit_name_to_prefix(name, &prefix);
183 if (r < 0)
184 return log_error_errno(r, "Failed to derive unit name prefix from unit name: %m");
185
186 chopped = false;
187 for (;;) {
188 dash = strrchr(prefix, '-');
189 if (!dash) /* No dash? if so we are done */
190 return 0;
191
192 n = (size_t) (dash - prefix);
193 if (n == 0) /* Leading dash? If so, we are done */
194 return 0;
195
196 if (prefix[n+1] != 0 || chopped) {
197 prefix[n+1] = 0;
198 break;
199 }
200
201 /* Trailing dash? If so, chop it off and try again, but not more than once. */
202 prefix[n] = 0;
203 chopped = true;
204 }
205
206 if (!unit_prefix_is_valid(prefix))
207 return 0;
208
209 type = unit_name_to_type(name);
210 if (type < 0) {
b8b846d7 211 log_error("Failed to to derive unit type from unit name: %s", name);
53966245
LP
212 return -EINVAL;
213 }
214
215 if (is_instance) {
216 r = unit_name_to_instance(name, &instance);
217 if (r < 0)
218 return log_error_errno(r, "Failed to derive unit name instance from unit name: %m");
219 }
220
221 r = unit_name_build_from_type(prefix, instance, type, &built);
222 if (r < 0)
223 return log_error_errno(r, "Failed to build prefix unit name: %m");
224
225 return unit_file_find_dirs(original_root, unit_path_cache, unit_path, built, suffix, dirs);
1a7f1b38
ZJS
226}
227
228int unit_file_find_dropin_paths(
17e78d18 229 const char *original_root,
1a7f1b38
ZJS
230 char **lookup_path,
231 Set *unit_path_cache,
95778782
ZJS
232 const char *dir_suffix,
233 const char *file_suffix,
1a7f1b38 234 Set *names,
058db925 235 char ***ret) {
1a7f1b38 236
3f6de63b 237 _cleanup_strv_free_ char **dirs = NULL;
dcc4f30e 238 char *t, **p;
53966245 239 Iterator i;
1a7f1b38
ZJS
240 int r;
241
058db925 242 assert(ret);
1a7f1b38 243
dcc4f30e 244 SET_FOREACH(t, names, i)
1a7f1b38 245 STRV_FOREACH(p, lookup_path)
53966245 246 (void) unit_file_find_dirs(original_root, unit_path_cache, *p, t, dir_suffix, &dirs);
1a7f1b38 247
058db925
LP
248 if (strv_isempty(dirs)) {
249 *ret = NULL;
1a7f1b38 250 return 0;
058db925 251 }
1a7f1b38 252
b5084605 253 r = conf_files_list_strv(ret, file_suffix, NULL, 0, (const char**) dirs);
1a7f1b38 254 if (r < 0)
3f6de63b 255 return log_warning_errno(r, "Failed to create the list of configuration files: %m");
058db925 256
1a7f1b38
ZJS
257 return 1;
258}