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
, ":", 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
) {
205 u
->type
== UNIT_SOCKET
? SOCKET(u
)->control_command
:
206 u
->type
== UNIT_MOUNT
? MOUNT(u
)->control_command
:
207 u
->type
== UNIT_SWAP
? SWAP(u
)->control_command
: NULL
;
208 RET_GATHER(r
, verify_executable(u
, exec
, root
));
210 if (u
->type
== UNIT_SERVICE
)
211 FOREACH_ARRAY(i
, SERVICE(u
)->exec_command
, ELEMENTSOF(SERVICE(u
)->exec_command
))
212 RET_GATHER(r
, verify_executable(u
, *i
, root
));
214 if (u
->type
== UNIT_SOCKET
)
215 FOREACH_ARRAY(i
, SOCKET(u
)->exec_command
, ELEMENTSOF(SOCKET(u
)->exec_command
))
216 RET_GATHER(r
, verify_executable(u
, *i
, root
));
221 static int verify_documentation(Unit
*u
, bool check_man
) {
224 STRV_FOREACH(p
, u
->documentation
) {
225 log_unit_debug(u
, "Found documentation item: %s", *p
);
227 if (check_man
&& startswith(*p
, "man:")) {
228 k
= show_man_page(*p
+ 4, true);
231 log_unit_error_errno(u
, k
, "Can't show %s: %m", *p
+ 4);
233 log_unit_error(u
, "Command 'man %s' failed with code %d", *p
+ 4, k
);
242 /* Check remote URLs? */
247 static int verify_unit(Unit
*u
, bool check_man
, const char *root
) {
248 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
254 unit_dump(u
, stdout
, "\t");
256 log_unit_debug(u
, "Creating %s/start job", u
->id
);
257 r
= manager_add_job(u
->manager
, JOB_START
, u
, JOB_REPLACE
, NULL
, &error
, NULL
);
259 log_unit_error_errno(u
, r
, "Failed to create %s/start: %s", u
->id
, bus_error_message(&error
, r
));
261 RET_GATHER(r
, verify_socket(u
));
262 RET_GATHER(r
, verify_executables(u
, root
));
263 RET_GATHER(r
, verify_documentation(u
, check_man
));
268 static void set_destroy_ignore_pointer_max(Set
**s
) {
269 if (*s
== POINTER_MAX
)
279 RecursiveErrors recursive_errors
,
282 const ManagerTestRunFlags flags
=
283 MANAGER_TEST_RUN_MINIMAL
|
284 MANAGER_TEST_RUN_ENV_GENERATORS
|
285 MANAGER_TEST_DONT_OPEN_EXECUTOR
|
286 (recursive_errors
== RECURSIVE_ERRORS_NO
) * MANAGER_TEST_RUN_IGNORE_DEPENDENCIES
|
287 run_generators
* MANAGER_TEST_RUN_GENERATORS
;
289 _cleanup_(manager_freep
) Manager
*m
= NULL
;
290 _cleanup_(set_destroy_ignore_pointer_max
) Set
*s
= NULL
;
291 _unused_
_cleanup_(clear_log_syntax_callback
) dummy_t dummy
;
292 Unit
*units
[strv_length(filenames
)];
295 if (strv_isempty(filenames
))
298 /* Allow systemd-analyze to hook in a callback function so that it can get
299 * all the required log data from the function itself without having to rely
300 * on a global set variable for the same */
301 set_log_syntax_callback(log_syntax_callback
, &s
);
304 r
= verify_set_unit_path(filenames
);
306 return log_error_errno(r
, "Failed to set unit load path: %m");
308 r
= manager_new(scope
, flags
, &m
);
310 return log_error_errno(r
, "Failed to initialize manager: %m");
312 log_debug("Starting manager...");
314 r
= manager_startup(m
, /* serialization= */ NULL
, /* fds= */ NULL
, root
);
318 manager_clear_jobs(m
);
320 log_debug("Loading remaining units from the command line...");
322 STRV_FOREACH(filename
, filenames
) {
323 _cleanup_free_
char *prepared
= NULL
;
325 log_debug("Handling %s...", *filename
);
327 k
= verify_prepare_filename(*filename
, &prepared
);
329 log_error_errno(k
, "Failed to prepare filename %s: %m", *filename
);
334 k
= manager_load_startable_unit_or_warn(m
, NULL
, prepared
, &units
[count
]);
343 FOREACH_ARRAY(i
, units
, count
)
344 RET_GATHER(r
, verify_unit(*i
, check_man
, root
));
346 if (s
== POINTER_MAX
)
349 if (set_isempty(s
) || r
!= 0)
352 /* If all previous verifications succeeded, then either the recursive parsing of all the
353 * associated dependencies with RECURSIVE_ERRORS_YES or the parsing of the specified unit file
354 * with RECURSIVE_ERRORS_NO must have yielded a syntax warning and hence, a non-empty set. */
355 if (IN_SET(recursive_errors
, RECURSIVE_ERRORS_YES
, RECURSIVE_ERRORS_NO
))
356 return -ENOTRECOVERABLE
;
358 /* If all previous verifications succeeded, then the non-empty set could have resulted from
359 * a syntax warning encountered during the recursive parsing of the specified unit file and
360 * its direct dependencies. Hence, search for any of the filenames in the set and if found,
361 * return a non-zero process exit status. */
362 if (recursive_errors
== RECURSIVE_ERRORS_ONE
)
363 STRV_FOREACH(filename
, filenames
)
364 if (set_contains(s
, basename(*filename
)))
365 return -ENOTRECOVERABLE
;
370 static const char* const recursive_errors_table
[_RECURSIVE_ERRORS_MAX
] = {
371 [RECURSIVE_ERRORS_NO
] = "no",
372 [RECURSIVE_ERRORS_YES
] = "yes",
373 [RECURSIVE_ERRORS_ONE
] = "one",
376 DEFINE_STRING_TABLE_LOOKUP(recursive_errors
, RecursiveErrors
);