]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/analyze/analyze-verify-util.c
tree-wide: use ASSERT_PTR more
[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 int r;
40 const char *name;
41 _cleanup_free_ char *abspath = NULL;
42 _cleanup_free_ char *dir = NULL;
43 _cleanup_free_ char *with_instance = NULL;
44 char *c;
45
46 assert(filename);
47 assert(ret);
48
49 r = path_make_absolute_cwd(filename, &abspath);
50 if (r < 0)
51 return r;
52
53 name = basename(abspath);
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 int verify_generate_path(char **ret, char **filenames) {
76 _cleanup_strv_free_ char **ans = NULL;
77 _cleanup_free_ char *joined = NULL;
78 const char *old;
79 int r;
80
81 STRV_FOREACH(filename, filenames) {
82 _cleanup_free_ char *a = NULL;
83 char *t;
84
85 r = path_make_absolute_cwd(*filename, &a);
86 if (r < 0)
87 return r;
88
89 r = path_extract_directory(a, &t);
90 if (r < 0)
91 return r;
92
93 r = strv_consume(&ans, t);
94 if (r < 0)
95 return r;
96 }
97
98 strv_uniq(ans);
99
100 /* First, prepend our directories. Second, if some path was specified, use that, and
101 * otherwise use the defaults. Any duplicates will be filtered out in path-lookup.c.
102 * Treat explicit empty path to mean that nothing should be appended.
103 */
104 old = getenv("SYSTEMD_UNIT_PATH");
105 if (!streq_ptr(old, "")) {
106 if (!old)
107 old = ":";
108
109 r = strv_extend(&ans, old);
110 if (r < 0)
111 return r;
112 }
113
114 joined = strv_join(ans, ":");
115 if (!joined)
116 return -ENOMEM;
117
118 *ret = TAKE_PTR(joined);
119 return 0;
120 }
121
122 static int verify_socket(Unit *u) {
123 Unit *service;
124 int r;
125
126 assert(u);
127
128 if (u->type != UNIT_SOCKET)
129 return 0;
130
131 r = socket_load_service_unit(SOCKET(u), -1, &service);
132 if (r < 0)
133 return log_unit_error_errno(u, r, "service unit for the socket cannot be loaded: %m");
134
135 if (service->load_state != UNIT_LOADED)
136 return log_unit_error_errno(u, SYNTHETIC_ERRNO(ENOENT),
137 "service %s not loaded, socket cannot be started.", service->id);
138
139 log_unit_debug(u, "using service unit %s.", service->id);
140 return 0;
141 }
142
143 int verify_executable(Unit *u, const ExecCommand *exec, const char *root) {
144 int r;
145
146 if (!exec)
147 return 0;
148
149 if (exec->flags & EXEC_COMMAND_IGNORE_FAILURE)
150 return 0;
151
152 r = find_executable_full(exec->path, root, NULL, false, NULL, NULL);
153 if (r < 0)
154 return log_unit_error_errno(u, r, "Command %s is not executable: %m", exec->path);
155
156 return 0;
157 }
158
159 static int verify_executables(Unit *u, const char *root) {
160 ExecCommand *exec;
161 int r = 0, k;
162 unsigned i;
163
164 assert(u);
165
166 exec = u->type == UNIT_SOCKET ? SOCKET(u)->control_command :
167 u->type == UNIT_MOUNT ? MOUNT(u)->control_command :
168 u->type == UNIT_SWAP ? SWAP(u)->control_command : NULL;
169 k = verify_executable(u, exec, root);
170 if (k < 0 && r == 0)
171 r = k;
172
173 if (u->type == UNIT_SERVICE)
174 for (i = 0; i < ELEMENTSOF(SERVICE(u)->exec_command); i++) {
175 k = verify_executable(u, SERVICE(u)->exec_command[i], root);
176 if (k < 0 && r == 0)
177 r = k;
178 }
179
180 if (u->type == UNIT_SOCKET)
181 for (i = 0; i < ELEMENTSOF(SOCKET(u)->exec_command); i++) {
182 k = verify_executable(u, SOCKET(u)->exec_command[i], root);
183 if (k < 0 && r == 0)
184 r = k;
185 }
186
187 return r;
188 }
189
190 static int verify_documentation(Unit *u, bool check_man) {
191 int r = 0, k;
192
193 STRV_FOREACH(p, u->documentation) {
194 log_unit_debug(u, "Found documentation item: %s", *p);
195
196 if (check_man && startswith(*p, "man:")) {
197 k = show_man_page(*p + 4, true);
198 if (k != 0) {
199 if (k < 0)
200 log_unit_error_errno(u, k, "Can't show %s: %m", *p + 4);
201 else {
202 log_unit_error(u, "Command 'man %s' failed with code %d", *p + 4, k);
203 k = -ENOEXEC;
204 }
205 if (r == 0)
206 r = k;
207 }
208 }
209 }
210
211 /* Check remote URLs? */
212
213 return r;
214 }
215
216 static int verify_unit(Unit *u, bool check_man, const char *root) {
217 _cleanup_(sd_bus_error_free) sd_bus_error err = SD_BUS_ERROR_NULL;
218 int r, k;
219
220 assert(u);
221
222 if (DEBUG_LOGGING)
223 unit_dump(u, stdout, "\t");
224
225 log_unit_debug(u, "Creating %s/start job", u->id);
226 r = manager_add_job(u->manager, JOB_START, u, JOB_REPLACE, NULL, &err, NULL);
227 if (r < 0)
228 log_unit_error_errno(u, r, "Failed to create %s/start: %s", u->id, bus_error_message(&err, r));
229
230 k = verify_socket(u);
231 if (k < 0 && r == 0)
232 r = k;
233
234 k = verify_executables(u, root);
235 if (k < 0 && r == 0)
236 r = k;
237
238 k = verify_documentation(u, check_man);
239 if (k < 0 && r == 0)
240 r = k;
241
242 return r;
243 }
244
245 static void set_destroy_ignore_pointer_max(Set** s) {
246 if (*s == POINTER_MAX)
247 return;
248 set_free_free(*s);
249 }
250
251 int verify_units(char **filenames, LookupScope scope, bool check_man, bool run_generators, RecursiveErrors recursive_errors, const char *root) {
252 const ManagerTestRunFlags flags =
253 MANAGER_TEST_RUN_MINIMAL |
254 MANAGER_TEST_RUN_ENV_GENERATORS |
255 (recursive_errors == RECURSIVE_ERRORS_NO) * MANAGER_TEST_RUN_IGNORE_DEPENDENCIES |
256 run_generators * MANAGER_TEST_RUN_GENERATORS;
257
258 _cleanup_(manager_freep) Manager *m = NULL;
259 _cleanup_(set_destroy_ignore_pointer_max) Set *s = NULL;
260 _unused_ _cleanup_(clear_log_syntax_callback) dummy_t dummy;
261 Unit *units[strv_length(filenames)];
262 _cleanup_free_ char *var = NULL;
263 int r, k, i, count = 0;
264
265 if (strv_isempty(filenames))
266 return 0;
267
268 /* Allow systemd-analyze to hook in a callback function so that it can get
269 * all the required log data from the function itself without having to rely
270 * on a global set variable for the same */
271 set_log_syntax_callback(log_syntax_callback, &s);
272
273 /* set the path */
274 r = verify_generate_path(&var, filenames);
275 if (r < 0)
276 return log_error_errno(r, "Failed to generate unit load path: %m");
277
278 assert_se(set_unit_path(var) >= 0);
279
280 r = manager_new(scope, flags, &m);
281 if (r < 0)
282 return log_error_errno(r, "Failed to initialize manager: %m");
283
284 log_debug("Starting manager...");
285
286 r = manager_startup(m, /* serialization= */ NULL, /* fds= */ NULL, root);
287 if (r < 0)
288 return r;
289
290 manager_clear_jobs(m);
291
292 log_debug("Loading remaining units from the command line...");
293
294 STRV_FOREACH(filename, filenames) {
295 _cleanup_free_ char *prepared = NULL;
296
297 log_debug("Handling %s...", *filename);
298
299 k = verify_prepare_filename(*filename, &prepared);
300 if (k < 0) {
301 log_error_errno(k, "Failed to prepare filename %s: %m", *filename);
302 if (r == 0)
303 r = k;
304 continue;
305 }
306
307 k = manager_load_startable_unit_or_warn(m, NULL, prepared, &units[count]);
308 if (k < 0) {
309 if (r == 0)
310 r = k;
311 continue;
312 }
313
314 count++;
315 }
316
317 for (i = 0; i < count; i++) {
318 k = verify_unit(units[i], check_man, root);
319 if (k < 0 && r == 0)
320 r = k;
321 }
322
323 if (s == POINTER_MAX)
324 return log_oom();
325
326 if (set_isempty(s) || r != 0)
327 return r;
328
329 /* If all previous verifications succeeded, then either the recursive parsing of all the
330 * associated dependencies with RECURSIVE_ERRORS_YES or the parsing of the specified unit file
331 * with RECURSIVE_ERRORS_NO must have yielded a syntax warning and hence, a non-empty set. */
332 if (IN_SET(recursive_errors, RECURSIVE_ERRORS_YES, RECURSIVE_ERRORS_NO))
333 return -ENOTRECOVERABLE;
334
335 /* If all previous verifications succeeded, then the non-empty set could have resulted from
336 * a syntax warning encountered during the recursive parsing of the specified unit file and
337 * its direct dependencies. Hence, search for any of the filenames in the set and if found,
338 * return a non-zero process exit status. */
339 if (recursive_errors == RECURSIVE_ERRORS_ONE)
340 STRV_FOREACH(filename, filenames)
341 if (set_contains(s, basename(*filename)))
342 return -ENOTRECOVERABLE;
343
344 return 0;
345 }
346
347 static const char* const recursive_errors_table[_RECURSIVE_ERRORS_MAX] = {
348 [RECURSIVE_ERRORS_NO] = "no",
349 [RECURSIVE_ERRORS_YES] = "yes",
350 [RECURSIVE_ERRORS_ONE] = "one",
351 };
352
353 DEFINE_STRING_TABLE_LOOKUP(recursive_errors, RecursiveErrors);