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