1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
6 #include "alloc-util.h"
7 #include "analyze-verify-util.h"
13 #include "path-util.h"
14 #include "string-table.h"
16 #include "unit-name.h"
17 #include "unit-serialize.h"
19 static void log_syntax_callback(const char *unit
, int level
, void *userdata
) {
20 Set
**s
= ASSERT_PTR(userdata
);
25 if (level
> LOG_WARNING
)
28 if (*s
== POINTER_MAX
)
31 r
= set_put_strdup(s
, unit
);
38 int verify_prepare_filename(const char *filename
, char **ret
) {
39 _cleanup_free_
char *abspath
= NULL
, *name
= NULL
, *dir
= NULL
, *with_instance
= NULL
;
46 r
= path_make_absolute_cwd(filename
, &abspath
);
50 r
= path_extract_filename(abspath
, &name
);
54 if (!unit_name_is_valid(name
, UNIT_NAME_ANY
))
57 if (unit_name_is_valid(name
, UNIT_NAME_TEMPLATE
)) {
58 r
= unit_name_replace_instance(name
, "i", &with_instance
);
63 r
= path_extract_directory(abspath
, &dir
);
67 c
= path_join(dir
, with_instance
?: name
);
75 static int find_unit_directory(const char *p
, char **ret
) {
76 _cleanup_free_
char *a
= NULL
, *u
= NULL
, *t
= NULL
, *d
= NULL
;
82 r
= path_make_absolute_cwd(p
, &a
);
86 if (access(a
, F_OK
) >= 0) {
87 r
= path_extract_directory(a
, &d
);
95 r
= path_extract_filename(a
, &u
);
99 if (!unit_name_is_valid(u
, UNIT_NAME_INSTANCE
))
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
);
107 r
= path_extract_directory(a
, &d
);
116 if (access(a
, F_OK
) < 0)
123 int verify_set_unit_path(char **filenames
) {
124 _cleanup_strv_free_
char **ans
= NULL
;
125 _cleanup_free_
char *joined
= NULL
;
129 STRV_FOREACH(filename
, filenames
) {
130 _cleanup_free_
char *t
= NULL
;
132 r
= find_unit_directory(*filename
, &t
);
138 r
= strv_consume(&ans
, TAKE_PTR(t
));
143 if (strv_isempty(ans
))
146 joined
= strv_join(strv_uniq(ans
), ":");
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
)))
158 assert_se(set_unit_path(joined
) >= 0);
162 static int verify_socket(Unit
*u
) {
168 if (u
->type
!= UNIT_SOCKET
)
171 r
= socket_load_service_unit(SOCKET(u
), -1, &service
);
173 return log_unit_error_errno(u
, r
, "service unit for the socket cannot be loaded: %m");
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
);
179 log_unit_debug(u
, "using service unit %s.", service
->id
);
183 int verify_executable(Unit
*u
, const ExecCommand
*exec
, const char *root
) {
189 if (exec
->flags
& EXEC_COMMAND_IGNORE_FAILURE
)
192 r
= find_executable_full(exec
->path
, root
, NULL
, false, NULL
, NULL
);
194 return log_unit_error_errno(u
, r
, "Command %s is not executable: %m", exec
->path
);
199 static int verify_executables(Unit
*u
, const char *root
) {
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
));
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
));
217 static int verify_documentation(Unit
*u
, bool check_man
) {
220 STRV_FOREACH(p
, u
->documentation
) {
221 log_unit_debug(u
, "Found documentation item: %s", *p
);
223 if (check_man
&& startswith(*p
, "man:")) {
224 k
= show_man_page(*p
+ 4, true);
227 log_unit_error_errno(u
, k
, "Can't show %s: %m", *p
+ 4);
229 log_unit_error(u
, "Command 'man %s' failed with code %d", *p
+ 4, k
);
238 /* Check remote URLs? */
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
;
250 unit_dump(u
, stdout
, "\t");
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
);
255 log_unit_error_errno(u
, r
, "Failed to create %s/start: %s", u
->id
, bus_error_message(&error
, r
));
257 RET_GATHER(r
, verify_socket(u
));
258 RET_GATHER(r
, verify_executables(u
, root
));
259 RET_GATHER(r
, verify_documentation(u
, check_man
));
264 static void set_destroy_ignore_pointer_max(Set
**s
) {
265 if (*s
== POINTER_MAX
)
275 RecursiveErrors recursive_errors
,
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
;
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
)];
291 if (strv_isempty(filenames
))
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
);
300 r
= verify_set_unit_path(filenames
);
302 return log_error_errno(r
, "Failed to set unit load path: %m");
304 r
= manager_new(scope
, flags
, &m
);
306 return log_error_errno(r
, "Failed to initialize manager: %m");
308 log_debug("Starting manager...");
310 r
= manager_startup(m
, /* serialization= */ NULL
, /* fds= */ NULL
, root
);
314 manager_clear_jobs(m
);
316 log_debug("Loading remaining units from the command line...");
318 STRV_FOREACH(filename
, filenames
) {
319 _cleanup_free_
char *prepared
= NULL
;
321 log_debug("Handling %s...", *filename
);
323 k
= verify_prepare_filename(*filename
, &prepared
);
325 log_error_errno(k
, "Failed to prepare filename %s: %m", *filename
);
330 k
= manager_load_startable_unit_or_warn(m
, NULL
, prepared
, &units
[count
]);
339 FOREACH_ARRAY(i
, units
, count
)
340 RET_GATHER(r
, verify_unit(*i
, check_man
, root
));
342 if (s
== POINTER_MAX
)
345 if (set_isempty(s
) || r
!= 0)
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
;
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
;
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",
372 DEFINE_STRING_TABLE_LOOKUP(recursive_errors
, RecursiveErrors
);