]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/analyze/analyze-verify.c
pkgconfig: define variables relative to ${prefix}/${rootprefix}/${sysconfdir}
[thirdparty/systemd.git] / src / analyze / analyze-verify.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <stdlib.h>
4
5 #include "alloc-util.h"
6 #include "all-units.h"
7 #include "analyze-verify.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 "strv.h"
15 #include "unit-name.h"
16
17 static 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 }
56
57 static int generate_path(char **var, char **filenames) {
58 const char *old;
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
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 }
91
92 *var = strv_join(ans, ":");
93 if (!*var)
94 return -ENOMEM;
95
96 return 0;
97 }
98
99 static 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));
111 if (r < 0)
112 return log_unit_error_errno(u, r, "Socket cannot be started, failed to create instance: %m");
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));
119 log_unit_debug(u, "Using %s", UNIT(service)->id);
120
121 if (UNIT(service)->load_state != UNIT_LOADED) {
122 log_unit_error(u, "Service %s not loaded, %s cannot be started.", UNIT(service)->id, u->id);
123 return -ENOENT;
124 }
125 }
126
127 return 0;
128 }
129
130 static int verify_executable(Unit *u, ExecCommand *exec) {
131 if (!exec)
132 return 0;
133
134 if (access(exec->path, X_OK) < 0)
135 return log_unit_error_errno(u, errno, "Command %s is not executable: %m", exec->path);
136
137 return 0;
138 }
139
140 static 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
171 static int verify_documentation(Unit *u, bool check_man) {
172 char **p;
173 int r = 0, k;
174
175 STRV_FOREACH(p, u->documentation) {
176 log_unit_debug(u, "Found documentation item: %s", *p);
177
178 if (check_man && startswith(*p, "man:")) {
179 k = show_man_page(*p + 4, true);
180 if (k != 0) {
181 if (k < 0)
182 log_unit_error_errno(u, k, "Can't show %s: %m", *p + 4);
183 else {
184 log_unit_error(u, "Command 'man %s' failed with code %d", *p + 4, k);
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
198 static int verify_unit(Unit *u, bool check_man) {
199 _cleanup_(sd_bus_error_free) sd_bus_error err = SD_BUS_ERROR_NULL;
200 int r, k;
201
202 assert(u);
203
204 if (DEBUG_LOGGING)
205 unit_dump(u, stdout, "\t");
206
207 log_unit_debug(u, "Creating %s/start job", u->id);
208 r = manager_add_job(u->manager, JOB_START, u, JOB_REPLACE, &err, NULL);
209 if (r < 0)
210 log_unit_error_errno(u, r, "Failed to create %s/start: %s", u->id, bus_error_message(&err, r));
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
220 k = verify_documentation(u, check_man);
221 if (k < 0 && r == 0)
222 r = k;
223
224 return r;
225 }
226
227 int verify_units(char **filenames, UnitFileScope scope, bool check_man, bool run_generators) {
228 const ManagerTestRunFlags flags =
229 MANAGER_TEST_RUN_BASIC |
230 MANAGER_TEST_RUN_ENV_GENERATORS |
231 run_generators * MANAGER_TEST_RUN_GENERATORS;
232
233 _cleanup_(manager_freep) Manager *m = NULL;
234 Unit *units[strv_length(filenames)];
235 _cleanup_free_ char *var = NULL;
236 int r = 0, k, i, count = 0;
237 char **filename;
238
239 if (strv_isempty(filenames))
240 return 0;
241
242 /* set the path */
243 r = generate_path(&var, filenames);
244 if (r < 0)
245 return log_error_errno(r, "Failed to generate unit load path: %m");
246
247 assert_se(set_unit_path(var) >= 0);
248
249 r = manager_new(scope, flags, &m);
250 if (r < 0)
251 return log_error_errno(r, "Failed to initialize manager: %m");
252
253 log_debug("Starting manager...");
254
255 r = manager_startup(m, NULL, NULL);
256 if (r < 0)
257 return r;
258
259 manager_clear_jobs(m);
260
261 log_debug("Loading remaining units from the command line...");
262
263 STRV_FOREACH(filename, filenames) {
264 _cleanup_free_ char *prepared = NULL;
265
266 log_debug("Handling %s...", *filename);
267
268 k = prepare_filename(*filename, &prepared);
269 if (k < 0) {
270 log_error_errno(k, "Failed to prepare filename %s: %m", *filename);
271 if (r == 0)
272 r = k;
273 continue;
274 }
275
276 k = manager_load_startable_unit_or_warn(m, NULL, prepared, &units[count]);
277 if (k < 0 && r == 0)
278 r = k;
279 else
280 count++;
281 }
282
283 for (i = 0; i < count; i++) {
284 k = verify_unit(units[i], check_man);
285 if (k < 0 && r == 0)
286 r = k;
287 }
288
289 return r;
290 }