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