]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/analyze/analyze-verify.c
analyze: use log_unit_error_errno()'s return value where we can
[thirdparty/systemd.git] / src / analyze / analyze-verify.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
8b835fcc
ZJS
2
3#include <stdlib.h>
8b835fcc 4
b5efdb8a 5#include "alloc-util.h"
57b7a260 6#include "all-units.h"
5f311f8c 7#include "analyze-verify.h"
4bd29fe5 8#include "bus-error.h"
8b835fcc
ZJS
9#include "bus-util.h"
10#include "log.h"
5f311f8c 11#include "manager.h"
78002a67 12#include "pager.h"
5f311f8c
LP
13#include "path-util.h"
14#include "strv.h"
25f17e47
EV
15#include "unit-name.h"
16
17static int prepare_filename(const char *filename, char **ret) {
18 int r;
19 const char *name;
20 _cleanup_free_ char *abspath = NULL;
21 _cleanup_free_ char *dir = NULL;
22 _cleanup_free_ char *with_instance = NULL;
23 char *c;
24
25 assert(filename);
26 assert(ret);
27
28 r = path_make_absolute_cwd(filename, &abspath);
29 if (r < 0)
30 return r;
31
32 name = basename(abspath);
33 if (!unit_name_is_valid(name, UNIT_NAME_ANY))
34 return -EINVAL;
35
36 if (unit_name_is_valid(name, UNIT_NAME_TEMPLATE)) {
37 r = unit_name_replace_instance(name, "i", &with_instance);
38 if (r < 0)
39 return r;
40 }
41
42 dir = dirname_malloc(abspath);
43 if (!dir)
44 return -ENOMEM;
45
46 if (with_instance)
47 c = path_join(NULL, dir, with_instance);
48 else
49 c = path_join(NULL, dir, name);
50 if (!c)
51 return -ENOMEM;
52
53 *ret = c;
54 return 0;
55}
8b835fcc
ZJS
56
57static int generate_path(char **var, char **filenames) {
d941ea22 58 const char *old;
8b835fcc
ZJS
59 char **filename;
60
61 _cleanup_strv_free_ char **ans = NULL;
62 int r;
63
64 STRV_FOREACH(filename, filenames) {
65 char *t;
66
67 t = dirname_malloc(*filename);
68 if (!t)
69 return -ENOMEM;
70
71 r = strv_consume(&ans, t);
72 if (r < 0)
73 return r;
74 }
75
76 assert_se(strv_uniq(ans));
77
d941ea22
ZJS
78 /* First, prepend our directories. Second, if some path was specified, use that, and
79 * otherwise use the defaults. Any duplicates will be filtered out in path-lookup.c.
80 * Treat explicit empty path to mean that nothing should be appended.
81 */
82 old = getenv("SYSTEMD_UNIT_PATH");
83 if (!streq_ptr(old, "")) {
84 if (!old)
85 old = ":";
86
87 r = strv_extend(&ans, old);
88 if (r < 0)
89 return r;
90 }
8b835fcc
ZJS
91
92 *var = strv_join(ans, ":");
93 if (!*var)
94 return -ENOMEM;
95
96 return 0;
97}
98
99static int verify_socket(Unit *u) {
100 int r;
101
102 assert(u);
103
104 if (u->type != UNIT_SOCKET)
105 return 0;
106
107 /* Cannot run this without the service being around */
108
109 /* This makes sure instance is created if necessary. */
110 r = socket_instantiate_service(SOCKET(u));
4d2a7ffd
LP
111 if (r < 0)
112 return log_unit_error_errno(u, r, "Socket cannot be started, failed to create instance: %m");
8b835fcc
ZJS
113
114 /* This checks both type of sockets */
115 if (UNIT_ISSET(SOCKET(u)->service)) {
116 Service *service;
117
118 service = SERVICE(UNIT_DEREF(SOCKET(u)->service));
f2341e0a 119 log_unit_debug(u, "Using %s", UNIT(service)->id);
8b835fcc
ZJS
120
121 if (UNIT(service)->load_state != UNIT_LOADED) {
f2341e0a 122 log_unit_error(u, "Service %s not loaded, %s cannot be started.", UNIT(service)->id, u->id);
8b835fcc
ZJS
123 return -ENOENT;
124 }
125 }
126
127 return 0;
128}
129
130static int verify_executable(Unit *u, ExecCommand *exec) {
234519ae 131 if (!exec)
8b835fcc
ZJS
132 return 0;
133
f2341e0a
LP
134 if (access(exec->path, X_OK) < 0)
135 return log_unit_error_errno(u, errno, "Command %s is not executable: %m", exec->path);
8b835fcc
ZJS
136
137 return 0;
138}
139
140static int verify_executables(Unit *u) {
141 ExecCommand *exec;
142 int r = 0, k;
143 unsigned i;
144
145 assert(u);
146
147 exec = u->type == UNIT_SOCKET ? SOCKET(u)->control_command :
148 u->type == UNIT_MOUNT ? MOUNT(u)->control_command :
149 u->type == UNIT_SWAP ? SWAP(u)->control_command : NULL;
150 k = verify_executable(u, exec);
151 if (k < 0 && r == 0)
152 r = k;
153
154 if (u->type == UNIT_SERVICE)
155 for (i = 0; i < ELEMENTSOF(SERVICE(u)->exec_command); i++) {
156 k = verify_executable(u, SERVICE(u)->exec_command[i]);
157 if (k < 0 && r == 0)
158 r = k;
159 }
160
161 if (u->type == UNIT_SOCKET)
162 for (i = 0; i < ELEMENTSOF(SOCKET(u)->exec_command); i++) {
163 k = verify_executable(u, SOCKET(u)->exec_command[i]);
164 if (k < 0 && r == 0)
165 r = k;
166 }
167
168 return r;
169}
170
1d3bc017 171static int verify_documentation(Unit *u, bool check_man) {
78002a67
ZJS
172 char **p;
173 int r = 0, k;
174
78002a67 175 STRV_FOREACH(p, u->documentation) {
f2341e0a
LP
176 log_unit_debug(u, "Found documentation item: %s", *p);
177
1d3bc017 178 if (check_man && startswith(*p, "man:")) {
78002a67
ZJS
179 k = show_man_page(*p + 4, true);
180 if (k != 0) {
181 if (k < 0)
f2341e0a 182 log_unit_error_errno(u, r, "Can't show %s: %m", *p);
78002a67 183 else {
f2341e0a 184 log_unit_error_errno(u, r, "man %s command failed with code %d", *p + 4, k);
78002a67
ZJS
185 k = -ENOEXEC;
186 }
187 if (r == 0)
188 r = k;
189 }
190 }
191 }
192
193 /* Check remote URLs? */
194
195 return r;
196}
197
1d3bc017 198static int verify_unit(Unit *u, bool check_man) {
4afd3348 199 _cleanup_(sd_bus_error_free) sd_bus_error err = SD_BUS_ERROR_NULL;
8b835fcc
ZJS
200 int r, k;
201
202 assert(u);
203
f1d34068 204 if (DEBUG_LOGGING)
8b835fcc
ZJS
205 unit_dump(u, stdout, "\t");
206
f2341e0a 207 log_unit_debug(u, "Creating %s/start job", u->id);
4bd29fe5 208 r = manager_add_job(u->manager, JOB_START, u, JOB_REPLACE, &err, NULL);
8b835fcc 209 if (r < 0)
4bd29fe5 210 log_unit_error_errno(u, r, "Failed to create %s/start: %s", u->id, bus_error_message(&err, r));
8b835fcc
ZJS
211
212 k = verify_socket(u);
213 if (k < 0 && r == 0)
214 r = k;
215
216 k = verify_executables(u);
217 if (k < 0 && r == 0)
218 r = k;
219
1d3bc017 220 k = verify_documentation(u, check_man);
78002a67
ZJS
221 if (k < 0 && r == 0)
222 r = k;
223
8b835fcc
ZJS
224 return r;
225}
226
641c0fd1 227int verify_units(char **filenames, UnitFileScope scope, bool check_man, bool run_generators) {
463d0d15 228 _cleanup_free_ char *var = NULL;
8b835fcc 229 Manager *m = NULL;
8b835fcc
ZJS
230 char **filename;
231 int r = 0, k;
232
233 Unit *units[strv_length(filenames)];
234 int i, count = 0;
e8112e67
ZJS
235 const uint8_t flags = MANAGER_TEST_RUN_BASIC |
236 MANAGER_TEST_RUN_ENV_GENERATORS |
641c0fd1 237 run_generators * MANAGER_TEST_RUN_GENERATORS;
8b835fcc 238
1d3bc017
ZJS
239 if (strv_isempty(filenames))
240 return 0;
241
8b835fcc
ZJS
242 /* set the path */
243 r = generate_path(&var, filenames);
23bbb0de
MS
244 if (r < 0)
245 return log_error_errno(r, "Failed to generate unit load path: %m");
8b835fcc
ZJS
246
247 assert_se(set_unit_path(var) >= 0);
248
641c0fd1 249 r = manager_new(scope, flags, &m);
23bbb0de 250 if (r < 0)
ff9b60f3 251 return log_error_errno(r, "Failed to initialize manager: %m");
8b835fcc
ZJS
252
253 log_debug("Starting manager...");
254
76752516 255 r = manager_startup(m, NULL, NULL);
8b835fcc 256 if (r < 0) {
da927ba9 257 log_error_errno(r, "Failed to start manager: %m");
8b835fcc
ZJS
258 goto finish;
259 }
260
261 manager_clear_jobs(m);
262
263 log_debug("Loading remaining units from the command line...");
264
265 STRV_FOREACH(filename, filenames) {
25f17e47 266 _cleanup_free_ char *prepared = NULL;
1d3bc017 267
8b835fcc
ZJS
268 log_debug("Handling %s...", *filename);
269
25f17e47
EV
270 k = prepare_filename(*filename, &prepared);
271 if (k < 0) {
272 log_error_errno(k, "Failed to prepare filename %s: %m", *filename);
273 if (r == 0)
274 r = k;
275 continue;
276 }
277
f79cd1a9
ZJS
278 k = manager_load_startable_unit_or_warn(m, NULL, prepared, &units[count]);
279 if (k < 0 && r == 0)
280 r = k;
281 else
313cefa1 282 count++;
8b835fcc
ZJS
283 }
284
285 for (i = 0; i < count; i++) {
1d3bc017 286 k = verify_unit(units[i], check_man);
8b835fcc
ZJS
287 if (k < 0 && r == 0)
288 r = k;
289 }
290
291finish:
292 manager_free(m);
293
294 return r;
295}