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 int verify_generate_path(char **ret
, char **filenames
) {
76 _cleanup_strv_free_
char **ans
= NULL
;
77 _cleanup_free_
char *joined
= NULL
;
81 STRV_FOREACH(filename
, filenames
) {
82 _cleanup_free_
char *a
= NULL
;
85 r
= path_make_absolute_cwd(*filename
, &a
);
89 r
= path_extract_directory(a
, &t
);
93 r
= strv_consume(&ans
, t
);
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.
104 old
= getenv("SYSTEMD_UNIT_PATH");
105 if (!streq_ptr(old
, "")) {
109 r
= strv_extend(&ans
, old
);
114 joined
= strv_join(ans
, ":");
118 *ret
= TAKE_PTR(joined
);
122 static int verify_socket(Unit
*u
) {
128 if (u
->type
!= UNIT_SOCKET
)
131 r
= socket_load_service_unit(SOCKET(u
), -1, &service
);
133 return log_unit_error_errno(u
, r
, "service unit for the socket cannot be loaded: %m");
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
);
139 log_unit_debug(u
, "using service unit %s.", service
->id
);
143 int verify_executable(Unit
*u
, const ExecCommand
*exec
, const char *root
) {
149 if (exec
->flags
& EXEC_COMMAND_IGNORE_FAILURE
)
152 r
= find_executable_full(exec
->path
, root
, NULL
, false, NULL
, NULL
);
154 return log_unit_error_errno(u
, r
, "Command %s is not executable: %m", exec
->path
);
159 static int verify_executables(Unit
*u
, const char *root
) {
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
));
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
));
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
));
181 static int verify_documentation(Unit
*u
, bool check_man
) {
184 STRV_FOREACH(p
, u
->documentation
) {
185 log_unit_debug(u
, "Found documentation item: %s", *p
);
187 if (check_man
&& startswith(*p
, "man:")) {
188 k
= show_man_page(*p
+ 4, true);
191 log_unit_error_errno(u
, k
, "Can't show %s: %m", *p
+ 4);
193 log_unit_error(u
, "Command 'man %s' failed with code %d", *p
+ 4, k
);
202 /* Check remote URLs? */
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
;
214 unit_dump(u
, stdout
, "\t");
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
);
219 log_unit_error_errno(u
, r
, "Failed to create %s/start: %s", u
->id
, bus_error_message(&error
, r
));
221 RET_GATHER(r
, verify_socket(u
));
222 RET_GATHER(r
, verify_executables(u
, root
));
223 RET_GATHER(r
, verify_documentation(u
, check_man
));
228 static void set_destroy_ignore_pointer_max(Set
** s
) {
229 if (*s
== POINTER_MAX
)
239 RecursiveErrors recursive_errors
,
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
;
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
;
255 if (strv_isempty(filenames
))
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
);
264 r
= verify_generate_path(&var
, filenames
);
266 return log_error_errno(r
, "Failed to generate unit load path: %m");
268 assert_se(set_unit_path(var
) >= 0);
270 r
= manager_new(scope
, flags
, &m
);
272 return log_error_errno(r
, "Failed to initialize manager: %m");
274 log_debug("Starting manager...");
276 r
= manager_startup(m
, /* serialization= */ NULL
, /* fds= */ NULL
, root
);
280 manager_clear_jobs(m
);
282 log_debug("Loading remaining units from the command line...");
284 STRV_FOREACH(filename
, filenames
) {
285 _cleanup_free_
char *prepared
= NULL
;
287 log_debug("Handling %s...", *filename
);
289 k
= verify_prepare_filename(*filename
, &prepared
);
291 log_error_errno(k
, "Failed to prepare filename %s: %m", *filename
);
296 k
= manager_load_startable_unit_or_warn(m
, NULL
, prepared
, &units
[count
]);
305 for (int i
= 0; i
< count
; i
++)
306 RET_GATHER(r
, verify_unit(units
[i
], check_man
, root
));
308 if (s
== POINTER_MAX
)
311 if (set_isempty(s
) || r
!= 0)
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
;
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
;
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",
338 DEFINE_STRING_TABLE_LOOKUP(recursive_errors
, RecursiveErrors
);