]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/analyze/analyze-verify-util.c
various: use RET_GATHER
[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 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 int r = 0;
161
162 assert(u);
163
164 ExecCommand *exec =
165 u->type == UNIT_SOCKET ? SOCKET(u)->control_command :
166 u->type == UNIT_MOUNT ? MOUNT(u)->control_command :
167 u->type == UNIT_SWAP ? SWAP(u)->control_command : NULL;
168 RET_GATHER(r, verify_executable(u, exec, root));
169
170 if (u->type == UNIT_SERVICE)
171 for (unsigned i = 0; i < ELEMENTSOF(SERVICE(u)->exec_command); i++)
172 RET_GATHER(r, verify_executable(u, SERVICE(u)->exec_command[i], root));
173
174 if (u->type == UNIT_SOCKET)
175 for (unsigned i = 0; i < ELEMENTSOF(SOCKET(u)->exec_command); i++)
176 RET_GATHER(r, verify_executable(u, SOCKET(u)->exec_command[i], root));
177
178 return r;
179 }
180
181 static int verify_documentation(Unit *u, bool check_man) {
182 int r = 0, k;
183
184 STRV_FOREACH(p, u->documentation) {
185 log_unit_debug(u, "Found documentation item: %s", *p);
186
187 if (check_man && startswith(*p, "man:")) {
188 k = show_man_page(*p + 4, true);
189 if (k != 0) {
190 if (k < 0)
191 log_unit_error_errno(u, k, "Can't show %s: %m", *p + 4);
192 else {
193 log_unit_error(u, "Command 'man %s' failed with code %d", *p + 4, k);
194 k = -ENOEXEC;
195 }
196 if (r == 0)
197 r = k;
198 }
199 }
200 }
201
202 /* Check remote URLs? */
203
204 return r;
205 }
206
207 static int verify_unit(Unit *u, bool check_man, const char *root) {
208 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
209 int r;
210
211 assert(u);
212
213 if (DEBUG_LOGGING)
214 unit_dump(u, stdout, "\t");
215
216 log_unit_debug(u, "Creating %s/start job", u->id);
217 r = manager_add_job(u->manager, JOB_START, u, JOB_REPLACE, NULL, &error, NULL);
218 if (r < 0)
219 log_unit_error_errno(u, r, "Failed to create %s/start: %s", u->id, bus_error_message(&error, r));
220
221 RET_GATHER(r, verify_socket(u));
222 RET_GATHER(r, verify_executables(u, root));
223 RET_GATHER(r, verify_documentation(u, check_man));
224
225 return r;
226 }
227
228 static void set_destroy_ignore_pointer_max(Set** s) {
229 if (*s == POINTER_MAX)
230 return;
231 set_free_free(*s);
232 }
233
234 int verify_units(
235 char **filenames,
236 RuntimeScope scope,
237 bool check_man,
238 bool run_generators,
239 RecursiveErrors recursive_errors,
240 const char *root) {
241
242 const ManagerTestRunFlags flags =
243 MANAGER_TEST_RUN_MINIMAL |
244 MANAGER_TEST_RUN_ENV_GENERATORS |
245 (recursive_errors == RECURSIVE_ERRORS_NO) * MANAGER_TEST_RUN_IGNORE_DEPENDENCIES |
246 run_generators * MANAGER_TEST_RUN_GENERATORS;
247
248 _cleanup_(manager_freep) Manager *m = NULL;
249 _cleanup_(set_destroy_ignore_pointer_max) Set *s = NULL;
250 _unused_ _cleanup_(clear_log_syntax_callback) dummy_t dummy;
251 Unit *units[strv_length(filenames)];
252 _cleanup_free_ char *var = NULL;
253 int r, k, count = 0;
254
255 if (strv_isempty(filenames))
256 return 0;
257
258 /* Allow systemd-analyze to hook in a callback function so that it can get
259 * all the required log data from the function itself without having to rely
260 * on a global set variable for the same */
261 set_log_syntax_callback(log_syntax_callback, &s);
262
263 /* set the path */
264 r = verify_generate_path(&var, filenames);
265 if (r < 0)
266 return log_error_errno(r, "Failed to generate unit load path: %m");
267
268 assert_se(set_unit_path(var) >= 0);
269
270 r = manager_new(scope, flags, &m);
271 if (r < 0)
272 return log_error_errno(r, "Failed to initialize manager: %m");
273
274 log_debug("Starting manager...");
275
276 r = manager_startup(m, /* serialization= */ NULL, /* fds= */ NULL, root);
277 if (r < 0)
278 return r;
279
280 manager_clear_jobs(m);
281
282 log_debug("Loading remaining units from the command line...");
283
284 STRV_FOREACH(filename, filenames) {
285 _cleanup_free_ char *prepared = NULL;
286
287 log_debug("Handling %s...", *filename);
288
289 k = verify_prepare_filename(*filename, &prepared);
290 if (k < 0) {
291 log_error_errno(k, "Failed to prepare filename %s: %m", *filename);
292 RET_GATHER(r, k);
293 continue;
294 }
295
296 k = manager_load_startable_unit_or_warn(m, NULL, prepared, &units[count]);
297 if (k < 0) {
298 RET_GATHER(r, k);
299 continue;
300 }
301
302 count++;
303 }
304
305 for (int i = 0; i < count; i++)
306 RET_GATHER(r, verify_unit(units[i], check_man, root));
307
308 if (s == POINTER_MAX)
309 return log_oom();
310
311 if (set_isempty(s) || r != 0)
312 return r;
313
314 /* If all previous verifications succeeded, then either the recursive parsing of all the
315 * associated dependencies with RECURSIVE_ERRORS_YES or the parsing of the specified unit file
316 * with RECURSIVE_ERRORS_NO must have yielded a syntax warning and hence, a non-empty set. */
317 if (IN_SET(recursive_errors, RECURSIVE_ERRORS_YES, RECURSIVE_ERRORS_NO))
318 return -ENOTRECOVERABLE;
319
320 /* If all previous verifications succeeded, then the non-empty set could have resulted from
321 * a syntax warning encountered during the recursive parsing of the specified unit file and
322 * its direct dependencies. Hence, search for any of the filenames in the set and if found,
323 * return a non-zero process exit status. */
324 if (recursive_errors == RECURSIVE_ERRORS_ONE)
325 STRV_FOREACH(filename, filenames)
326 if (set_contains(s, basename(*filename)))
327 return -ENOTRECOVERABLE;
328
329 return 0;
330 }
331
332 static const char* const recursive_errors_table[_RECURSIVE_ERRORS_MAX] = {
333 [RECURSIVE_ERRORS_NO] = "no",
334 [RECURSIVE_ERRORS_YES] = "yes",
335 [RECURSIVE_ERRORS_ONE] = "one",
336 };
337
338 DEFINE_STRING_TABLE_LOOKUP(recursive_errors, RecursiveErrors);