1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2014 Zbigniew Jędrzejewski-Szmek
10 #include "alloc-util.h"
11 #include "analyze-verify.h"
12 #include "bus-error.h"
17 #include "path-util.h"
19 #include "unit-name.h"
21 static int prepare_filename(const char *filename
, char **ret
) {
24 _cleanup_free_
char *abspath
= NULL
;
25 _cleanup_free_
char *dir
= NULL
;
26 _cleanup_free_
char *with_instance
= NULL
;
32 r
= path_make_absolute_cwd(filename
, &abspath
);
36 name
= basename(abspath
);
37 if (!unit_name_is_valid(name
, UNIT_NAME_ANY
))
40 if (unit_name_is_valid(name
, UNIT_NAME_TEMPLATE
)) {
41 r
= unit_name_replace_instance(name
, "i", &with_instance
);
46 dir
= dirname_malloc(abspath
);
51 c
= path_join(NULL
, dir
, with_instance
);
53 c
= path_join(NULL
, dir
, name
);
61 static int generate_path(char **var
, char **filenames
) {
65 _cleanup_strv_free_
char **ans
= NULL
;
68 STRV_FOREACH(filename
, filenames
) {
71 t
= dirname_malloc(*filename
);
75 r
= strv_consume(&ans
, t
);
80 assert_se(strv_uniq(ans
));
82 /* First, prepend our directories. Second, if some path was specified, use that, and
83 * otherwise use the defaults. Any duplicates will be filtered out in path-lookup.c.
84 * Treat explicit empty path to mean that nothing should be appended.
86 old
= getenv("SYSTEMD_UNIT_PATH");
87 if (!streq_ptr(old
, "")) {
91 r
= strv_extend(&ans
, old
);
96 *var
= strv_join(ans
, ":");
103 static int verify_socket(Unit
*u
) {
108 if (u
->type
!= UNIT_SOCKET
)
111 /* Cannot run this without the service being around */
113 /* This makes sure instance is created if necessary. */
114 r
= socket_instantiate_service(SOCKET(u
));
116 log_unit_error_errno(u
, r
, "Socket cannot be started, failed to create instance: %m");
120 /* This checks both type of sockets */
121 if (UNIT_ISSET(SOCKET(u
)->service
)) {
124 service
= SERVICE(UNIT_DEREF(SOCKET(u
)->service
));
125 log_unit_debug(u
, "Using %s", UNIT(service
)->id
);
127 if (UNIT(service
)->load_state
!= UNIT_LOADED
) {
128 log_unit_error(u
, "Service %s not loaded, %s cannot be started.", UNIT(service
)->id
, u
->id
);
136 static int verify_executable(Unit
*u
, ExecCommand
*exec
) {
140 if (access(exec
->path
, X_OK
) < 0)
141 return log_unit_error_errno(u
, errno
, "Command %s is not executable: %m", exec
->path
);
146 static int verify_executables(Unit
*u
) {
153 exec
= u
->type
== UNIT_SOCKET
? SOCKET(u
)->control_command
:
154 u
->type
== UNIT_MOUNT
? MOUNT(u
)->control_command
:
155 u
->type
== UNIT_SWAP
? SWAP(u
)->control_command
: NULL
;
156 k
= verify_executable(u
, exec
);
160 if (u
->type
== UNIT_SERVICE
)
161 for (i
= 0; i
< ELEMENTSOF(SERVICE(u
)->exec_command
); i
++) {
162 k
= verify_executable(u
, SERVICE(u
)->exec_command
[i
]);
167 if (u
->type
== UNIT_SOCKET
)
168 for (i
= 0; i
< ELEMENTSOF(SOCKET(u
)->exec_command
); i
++) {
169 k
= verify_executable(u
, SOCKET(u
)->exec_command
[i
]);
177 static int verify_documentation(Unit
*u
, bool check_man
) {
181 STRV_FOREACH(p
, u
->documentation
) {
182 log_unit_debug(u
, "Found documentation item: %s", *p
);
184 if (check_man
&& startswith(*p
, "man:")) {
185 k
= show_man_page(*p
+ 4, true);
188 log_unit_error_errno(u
, r
, "Can't show %s: %m", *p
);
190 log_unit_error_errno(u
, r
, "man %s command failed with code %d", *p
+ 4, k
);
199 /* Check remote URLs? */
204 static int verify_unit(Unit
*u
, bool check_man
) {
205 _cleanup_(sd_bus_error_free
) sd_bus_error err
= SD_BUS_ERROR_NULL
;
211 unit_dump(u
, stdout
, "\t");
213 log_unit_debug(u
, "Creating %s/start job", u
->id
);
214 r
= manager_add_job(u
->manager
, JOB_START
, u
, JOB_REPLACE
, &err
, NULL
);
216 log_unit_error_errno(u
, r
, "Failed to create %s/start: %s", u
->id
, bus_error_message(&err
, r
));
218 k
= verify_socket(u
);
222 k
= verify_executables(u
);
226 k
= verify_documentation(u
, check_man
);
233 int verify_units(char **filenames
, UnitFileScope scope
, bool check_man
, bool run_generators
) {
234 _cleanup_free_
char *var
= NULL
;
241 Unit
*units
[strv_length(filenames
)];
243 const uint8_t flags
= MANAGER_TEST_RUN_BASIC
|
244 MANAGER_TEST_RUN_ENV_GENERATORS
|
245 run_generators
* MANAGER_TEST_RUN_GENERATORS
;
247 if (strv_isempty(filenames
))
251 r
= generate_path(&var
, filenames
);
253 return log_error_errno(r
, "Failed to generate unit load path: %m");
255 assert_se(set_unit_path(var
) >= 0);
257 r
= manager_new(scope
, flags
, &m
);
259 return log_error_errno(r
, "Failed to initialize manager: %m");
261 log_debug("Starting manager...");
263 r
= manager_startup(m
, serial
, fdset
);
265 log_error_errno(r
, "Failed to start manager: %m");
269 manager_clear_jobs(m
);
271 log_debug("Loading remaining units from the command line...");
273 STRV_FOREACH(filename
, filenames
) {
274 _cleanup_free_
char *prepared
= NULL
;
276 log_debug("Handling %s...", *filename
);
278 k
= prepare_filename(*filename
, &prepared
);
280 log_error_errno(k
, "Failed to prepare filename %s: %m", *filename
);
286 k
= manager_load_startable_unit_or_warn(m
, NULL
, prepared
, &units
[count
]);
293 for (i
= 0; i
< count
; i
++) {
294 k
= verify_unit(units
[i
], check_man
);