Debian Codegraph @

src/analyze/analyze-verify-util.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <stdlib.h>
4 #include <unistd.h>
5
6 #include "sd-bus.h"
7
8 #include "all-units.h"
9 #include "alloc-util.h"
10 #include "analyze.h"
11 #include "analyze-verify-util.h"
12 #include "bus-error.h"
13 #include "errno-util.h"
14 #include "log.h"
15 #include "manager.h"
16 #include "pager.h"
17 #include "path-util.h"
18 #include "set.h"
19 #include "string-table.h"
20 #include "string-util.h"
21 #include "strv.h"
22 #include "unit-name.h"
23 #include "unit-serialize.h"
24
25 static void log_syntax_callback(const char *unit, int level, void *userdata) {
26 Set **s = ASSERT_PTR(userdata);
27 int r;
28
29 assert(unit);
30
31 if (level > LOG_WARNING)
32 return;
33
34 if (*s == POINTER_MAX)
35 return;
36
37 r = set_put_strdup(s, unit);
38 if (r < 0) {
39 set_free(*s);
40 *s = POINTER_MAX;
41 }
42 }
43
44 int verify_prepare_filename(const char *filename, char **ret) {
45 _cleanup_free_ char *abspath = NULL, *name = NULL, *dir = NULL, *with_instance = NULL;
46 char *c;
47 int r;
48
49 assert(filename);
50 assert(ret);
51
52 r = path_make_absolute_cwd(filename, &abspath);
53 if (r < 0)
54 return r;
55
56 r = path_extract_filename(abspath, &name);
57 if (r < 0)
58 return r;
59
60 if (!unit_name_is_valid(name, UNIT_NAME_ANY))
61 return -EINVAL;
62
63 if (unit_name_is_valid(name, UNIT_NAME_TEMPLATE)) {
64 r = unit_name_replace_instance(name, arg_instance, &with_instance);
65 if (r < 0)
66 return r;
67 }
68
69 r = path_extract_directory(abspath, &dir);
70 if (r < 0)
71 return r;
72
73 c = path_join(dir, with_instance ?: name);
74 if (!c)
75 return -ENOMEM;
76
77 *ret = c;
78 return 0;
79 }
80
81 static int find_unit_directory(const char *p, char **ret) {
82 _cleanup_free_ char *a = NULL, *u = NULL, *t = NULL, *d = NULL;
83 int r;
84
85 assert(p);
86 assert(ret);
87
88 r = path_make_absolute_cwd(p, &a);
89 if (r < 0)
90 return r;
91
92 if (access(a, F_OK) >= 0) {
93 r = path_extract_directory(a, &d);
94 if (r < 0)
95 return r;
96
97 *ret = TAKE_PTR(d);
98 return 0;
99 }
100
101 r = path_extract_filename(a, &u);
102 if (r < 0)
103 return r;
104
105 if (!unit_name_is_valid(u, UNIT_NAME_INSTANCE))
106 return -ENOENT;
107
108 /* If the specified unit is an instance of a template unit, then let's try to find the template unit. */
109 r = unit_name_template(u, &t);
110 if (r < 0)
111 return r;
112
113 r = path_extract_directory(a, &d);
114 if (r < 0)
115 return r;
116
117 free(a);
118 a = path_join(d, t);
119 if (!a)
120 return -ENOMEM;
121
122 if (access(a, F_OK) < 0)
123 return -errno;
124
125 *ret = TAKE_PTR(d);
126 return 0;
127 }
128
129 int verify_set_unit_path(char **filenames) {
130 _cleanup_strv_free_ char **ans = NULL;
131 _cleanup_free_ char *joined = NULL;
132 const char *old;
133 int r;
134
135 STRV_FOREACH(filename, filenames) {
136 _cleanup_free_ char *t = NULL;
137
138 r = find_unit_directory(*filename, &t);
139 if (r == -ENOMEM)
140 return r;
141 if (r < 0)
142 continue;
143
144 r = strv_consume(&ans, TAKE_PTR(t));
145 if (r < 0)
146 return r;
147 }
148
149 if (strv_isempty(ans))
150 return 0;
151
152 joined = strv_join(strv_uniq(ans), ":");
153 if (!joined)
154 return -ENOMEM;
155
156 /* First, prepend our directories. Second, if some path was specified, use that, and
157 * otherwise use the defaults. Any duplicates will be filtered out in path-lookup.c.
158 * Treat explicit empty path to mean that nothing should be appended. */
159 old = getenv("SYSTEMD_UNIT_PATH");
160 if (!streq_ptr(old, "") &&
161 !strextend_with_separator(&joined, ":", strempty(old)))
162 return -ENOMEM;
163
164 assert_se(setenv_unit_path(joined) >= 0);
165 return 0;
166 }
167
168 static int verify_socket(Unit *u) {
169 Unit *service;
170 int r;
171
172 assert(u);
173
174 if (u->type != UNIT_SOCKET)
175 return 0;
176
177 r = socket_load_service_unit(SOCKET(u), -1, &service);
178 if (r < 0)
179 return log_unit_error_errno(u, r, "service unit for the socket cannot be loaded: %m");
180
181 if (service->load_state != UNIT_LOADED)
182 return log_unit_error_errno(u, SYNTHETIC_ERRNO(ENOENT),
183 "service %s not loaded, socket cannot be started.", service->id);
184
185 log_unit_debug(u, "using service unit %s.", service->id);
186 return 0;
187 }
188
189 int verify_executable(Unit *u, const ExecCommand *exec, const char *root) {
190 int r;
191
192 if (!exec)
193 return 0;
194
195 if (exec->flags & EXEC_COMMAND_IGNORE_FAILURE)
196 return 0;
197
198 r = find_executable_full(exec->path, root, NULL, false, NULL, NULL);
199 if (r < 0)
200 return log_unit_error_errno(u, r, "Command %s is not executable: %m", exec->path);
201
202 return 0;
203 }
204
205 static int verify_executables(Unit *u, const char *root) {
206 int r = 0;
207
208 assert(u);
209
210 if (u->type == UNIT_SERVICE)
211 FOREACH_ELEMENT(i, SERVICE(u)->exec_command)
212 LIST_FOREACH(command, j, *i)
213 RET_GATHER(r, verify_executable(u, j, root));
214
215 if (u->type == UNIT_SOCKET)
216 FOREACH_ELEMENT(i, SOCKET(u)->exec_command)
217 LIST_FOREACH(command, j, *i)
218 RET_GATHER(r, verify_executable(u, j, root));
219
220 return r;
221 }
222
223 static int verify_documentation(Unit *u, bool check_man) {
224 int r = 0, k;
225
226 STRV_FOREACH(p, u->documentation) {
227 log_unit_debug(u, "Found documentation item: %s", *p);
228
229 if (check_man && startswith(*p, "man:")) {
230 k = show_man_page(*p + 4, true);
231 if (k != 0) {
232 if (k < 0)
233 log_unit_error_errno(u, k, "Can't show %s: %m", *p + 4);
234 else {
235 log_unit_error(u, "Command 'man %s' failed with code %d", *p + 4, k);
236 k = -ENOEXEC;
237 }
238 if (r == 0)
239 r = k;
240 }
241 }
242 }
243
244 /* Check remote URLs? */
245
246 return r;
247 }
248
249 static int verify_unit(Unit *u, bool check_man, const char *root) {
250 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
251 int r;
252
253 assert(u);
254
255 if (DEBUG_LOGGING)
256 unit_dump(u, stdout, "\t");
257
258 log_unit_debug(u, "Creating %s/start job", u->id);
259 r = manager_add_job(u->manager, JOB_START, u, JOB_REPLACE, &error, /* ret= */ NULL);
260 if (r < 0)
261 log_unit_error_errno(u, r, "Failed to create %s/start: %s", u->id, bus_error_message(&error, r));
262
263 RET_GATHER(r, verify_socket(u));
264 RET_GATHER(r, verify_executables(u, root));
265 RET_GATHER(r, verify_documentation(u, check_man));
266
267 return r;
268 }
269
270 static void set_destroy_ignore_pointer_max(Set **s) {
271 assert(s);
272
273 if (*s == POINTER_MAX)
274 return;
275 set_free(*s);
276 }
277
278 int verify_units(
279 char **filenames,
280 RuntimeScope scope,
281 bool check_man,
282 bool run_generators,
283 RecursiveErrors recursive_errors,
284 const char *root) {
285
286 const ManagerTestRunFlags flags =
287 MANAGER_TEST_RUN_MINIMAL |
288 MANAGER_TEST_RUN_ENV_GENERATORS |
289 MANAGER_TEST_DONT_OPEN_EXECUTOR |
290 (recursive_errors == RECURSIVE_ERRORS_NO) * MANAGER_TEST_RUN_IGNORE_DEPENDENCIES |
291 run_generators * MANAGER_TEST_RUN_GENERATORS;
292
293 _cleanup_(manager_freep) Manager *m = NULL;
294 _cleanup_(set_destroy_ignore_pointer_max) Set *s = NULL;
295 _unused_ _cleanup_(clear_log_syntax_callback) dummy_t dummy;
296 Unit *units[strv_length(filenames)];
297 int r, k, count = 0;
298
299 if (strv_isempty(filenames))
300 return 0;
301
302 /* Allow systemd-analyze to hook in a callback function so that it can get
303 * all the required log data from the function itself without having to rely
304 * on a global set variable for the same */
305 set_log_syntax_callback(log_syntax_callback, &s);
306
307 /* set the path */
308 r = verify_set_unit_path(filenames);
309 if (r < 0)
310 return log_error_errno(r, "Failed to set unit load path: %m");
311
312 r = manager_new(scope, flags, &m);
313 if (r < 0)
314 return log_error_errno(r, "Failed to initialize manager: %m");
315
316 log_debug("Starting manager...");
317
318 r = manager_startup(m, /* serialization= */ NULL, /* fds= */ NULL, /* named_listen_fds= */ NULL, root);
319 if (r < 0)
320 return r;
321
322 manager_clear_jobs(m);
323
324 log_debug("Loading remaining units from the command line...");
325
326 STRV_FOREACH(filename, filenames) {
327 _cleanup_free_ char *prepared = NULL;
328
329 log_debug("Handling %s...", *filename);
330
331 k = verify_prepare_filename(*filename, &prepared);
332 if (k < 0) {
333 log_error_errno(k, "Failed to prepare filename %s: %m", *filename);
334 RET_GATHER(r, k);
335 continue;
336 }
337
338 k = manager_load_startable_unit_or_warn(m, NULL, prepared, &units[count]);
339 if (k < 0) {
340 RET_GATHER(r, k);
341 continue;
342 }
343
344 count++;
345 }
346
347 FOREACH_ARRAY(i, units, count)
348 RET_GATHER(r, verify_unit(*i, check_man, root));
349
350 if (s == POINTER_MAX)
351 return log_oom();
352
353 if (set_isempty(s) || r != 0)
354 return r;
355
356 /* If all previous verifications succeeded, then either the recursive parsing of all the
357 * associated dependencies with RECURSIVE_ERRORS_YES or the parsing of the specified unit file
358 * with RECURSIVE_ERRORS_NO must have yielded a syntax warning and hence, a non-empty set. */
359 if (IN_SET(recursive_errors, RECURSIVE_ERRORS_YES, RECURSIVE_ERRORS_NO))
360 return -ENOTRECOVERABLE;
361
362 /* If all previous verifications succeeded, then the non-empty set could have resulted from
363 * a syntax warning encountered during the recursive parsing of the specified unit file and
364 * its direct dependencies. Hence, search for any of the filenames in the set and if found,
365 * return a non-zero process exit status. */
366 if (recursive_errors == RECURSIVE_ERRORS_ONE)
367 STRV_FOREACH(filename, filenames) {
368 _cleanup_free_ char *unit_file = NULL;
369
370 r = path_extract_filename(*filename, &unit_file);
371 if (r < 0)
372 return log_error_errno(r, "Failed to extract file name from '%s': %m", *filename);
373
374 if (set_contains(s, unit_file))
375 return -ENOTRECOVERABLE;
376 }
377
378 return 0;
379 }
380
381 static const char* const recursive_errors_table[_RECURSIVE_ERRORS_MAX] = {
382 [RECURSIVE_ERRORS_NO] = "no",
383 [RECURSIVE_ERRORS_YES] = "yes",
384 [RECURSIVE_ERRORS_ONE] = "one",
385 };
386
387 DEFINE_STRING_TABLE_LOOKUP(recursive_errors, RecursiveErrors);