]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
8b835fcc ZJS |
2 | |
3 | #include <stdlib.h> | |
8b835fcc | 4 | |
b5efdb8a | 5 | #include "alloc-util.h" |
57b7a260 | 6 | #include "all-units.h" |
5f311f8c | 7 | #include "analyze-verify.h" |
4bd29fe5 | 8 | #include "bus-error.h" |
8b835fcc ZJS |
9 | #include "bus-util.h" |
10 | #include "log.h" | |
5f311f8c | 11 | #include "manager.h" |
78002a67 | 12 | #include "pager.h" |
5f311f8c LP |
13 | #include "path-util.h" |
14 | #include "strv.h" | |
25f17e47 EV |
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 | ||
62a85ee0 | 46 | c = path_join(dir, with_instance ?: name); |
25f17e47 EV |
47 | if (!c) |
48 | return -ENOMEM; | |
49 | ||
50 | *ret = c; | |
51 | return 0; | |
52 | } | |
8b835fcc ZJS |
53 | |
54 | static int generate_path(char **var, char **filenames) { | |
d941ea22 | 55 | const char *old; |
8b835fcc ZJS |
56 | char **filename; |
57 | ||
58 | _cleanup_strv_free_ char **ans = NULL; | |
59 | int r; | |
60 | ||
61 | STRV_FOREACH(filename, filenames) { | |
62 | char *t; | |
63 | ||
64 | t = dirname_malloc(*filename); | |
65 | if (!t) | |
66 | return -ENOMEM; | |
67 | ||
68 | r = strv_consume(&ans, t); | |
69 | if (r < 0) | |
70 | return r; | |
71 | } | |
72 | ||
73 | assert_se(strv_uniq(ans)); | |
74 | ||
d941ea22 ZJS |
75 | /* First, prepend our directories. Second, if some path was specified, use that, and |
76 | * otherwise use the defaults. Any duplicates will be filtered out in path-lookup.c. | |
77 | * Treat explicit empty path to mean that nothing should be appended. | |
78 | */ | |
79 | old = getenv("SYSTEMD_UNIT_PATH"); | |
80 | if (!streq_ptr(old, "")) { | |
81 | if (!old) | |
82 | old = ":"; | |
83 | ||
84 | r = strv_extend(&ans, old); | |
85 | if (r < 0) | |
86 | return r; | |
87 | } | |
8b835fcc ZJS |
88 | |
89 | *var = strv_join(ans, ":"); | |
90 | if (!*var) | |
91 | return -ENOMEM; | |
92 | ||
93 | return 0; | |
94 | } | |
95 | ||
96 | static int verify_socket(Unit *u) { | |
934ef6a5 | 97 | Unit *service; |
8b835fcc ZJS |
98 | int r; |
99 | ||
100 | assert(u); | |
101 | ||
102 | if (u->type != UNIT_SOCKET) | |
103 | return 0; | |
104 | ||
934ef6a5 | 105 | r = socket_load_service_unit(SOCKET(u), -1, &service); |
4d2a7ffd | 106 | if (r < 0) |
934ef6a5 | 107 | return log_unit_error_errno(u, r, "service unit for the socket cannot be loaded: %m"); |
8b835fcc | 108 | |
934ef6a5 ZJS |
109 | if (service->load_state != UNIT_LOADED) |
110 | return log_unit_error_errno(u, SYNTHETIC_ERRNO(ENOENT), | |
111 | "service %s not loaded, socket cannot be started.", service->id); | |
8b835fcc | 112 | |
934ef6a5 | 113 | log_unit_debug(u, "using service unit %s.", service->id); |
8b835fcc ZJS |
114 | return 0; |
115 | } | |
116 | ||
c90c597e | 117 | int verify_executable(Unit *u, const ExecCommand *exec) { |
234519ae | 118 | if (!exec) |
8b835fcc ZJS |
119 | return 0; |
120 | ||
c90c597e GS |
121 | if (exec->flags & EXEC_COMMAND_IGNORE_FAILURE) |
122 | return 0; | |
123 | ||
f2341e0a LP |
124 | if (access(exec->path, X_OK) < 0) |
125 | return log_unit_error_errno(u, errno, "Command %s is not executable: %m", exec->path); | |
8b835fcc ZJS |
126 | |
127 | return 0; | |
128 | } | |
129 | ||
130 | static int verify_executables(Unit *u) { | |
131 | ExecCommand *exec; | |
132 | int r = 0, k; | |
133 | unsigned i; | |
134 | ||
135 | assert(u); | |
136 | ||
137 | exec = u->type == UNIT_SOCKET ? SOCKET(u)->control_command : | |
138 | u->type == UNIT_MOUNT ? MOUNT(u)->control_command : | |
139 | u->type == UNIT_SWAP ? SWAP(u)->control_command : NULL; | |
140 | k = verify_executable(u, exec); | |
141 | if (k < 0 && r == 0) | |
142 | r = k; | |
143 | ||
144 | if (u->type == UNIT_SERVICE) | |
145 | for (i = 0; i < ELEMENTSOF(SERVICE(u)->exec_command); i++) { | |
146 | k = verify_executable(u, SERVICE(u)->exec_command[i]); | |
147 | if (k < 0 && r == 0) | |
148 | r = k; | |
149 | } | |
150 | ||
151 | if (u->type == UNIT_SOCKET) | |
152 | for (i = 0; i < ELEMENTSOF(SOCKET(u)->exec_command); i++) { | |
153 | k = verify_executable(u, SOCKET(u)->exec_command[i]); | |
154 | if (k < 0 && r == 0) | |
155 | r = k; | |
156 | } | |
157 | ||
158 | return r; | |
159 | } | |
160 | ||
1d3bc017 | 161 | static int verify_documentation(Unit *u, bool check_man) { |
78002a67 ZJS |
162 | char **p; |
163 | int r = 0, k; | |
164 | ||
78002a67 | 165 | STRV_FOREACH(p, u->documentation) { |
f2341e0a LP |
166 | log_unit_debug(u, "Found documentation item: %s", *p); |
167 | ||
1d3bc017 | 168 | if (check_man && startswith(*p, "man:")) { |
78002a67 ZJS |
169 | k = show_man_page(*p + 4, true); |
170 | if (k != 0) { | |
171 | if (k < 0) | |
e95a86c7 | 172 | log_unit_error_errno(u, k, "Can't show %s: %m", *p + 4); |
78002a67 | 173 | else { |
e95a86c7 | 174 | log_unit_error(u, "Command 'man %s' failed with code %d", *p + 4, k); |
78002a67 ZJS |
175 | k = -ENOEXEC; |
176 | } | |
177 | if (r == 0) | |
178 | r = k; | |
179 | } | |
180 | } | |
181 | } | |
182 | ||
183 | /* Check remote URLs? */ | |
184 | ||
185 | return r; | |
186 | } | |
187 | ||
1d3bc017 | 188 | static int verify_unit(Unit *u, bool check_man) { |
4afd3348 | 189 | _cleanup_(sd_bus_error_free) sd_bus_error err = SD_BUS_ERROR_NULL; |
8b835fcc ZJS |
190 | int r, k; |
191 | ||
192 | assert(u); | |
193 | ||
f1d34068 | 194 | if (DEBUG_LOGGING) |
8b835fcc ZJS |
195 | unit_dump(u, stdout, "\t"); |
196 | ||
f2341e0a | 197 | log_unit_debug(u, "Creating %s/start job", u->id); |
50cbaba4 | 198 | r = manager_add_job(u->manager, JOB_START, u, JOB_REPLACE, NULL, &err, NULL); |
8b835fcc | 199 | if (r < 0) |
4bd29fe5 | 200 | log_unit_error_errno(u, r, "Failed to create %s/start: %s", u->id, bus_error_message(&err, r)); |
8b835fcc ZJS |
201 | |
202 | k = verify_socket(u); | |
203 | if (k < 0 && r == 0) | |
204 | r = k; | |
205 | ||
206 | k = verify_executables(u); | |
207 | if (k < 0 && r == 0) | |
208 | r = k; | |
209 | ||
1d3bc017 | 210 | k = verify_documentation(u, check_man); |
78002a67 ZJS |
211 | if (k < 0 && r == 0) |
212 | r = k; | |
213 | ||
8b835fcc ZJS |
214 | return r; |
215 | } | |
216 | ||
641c0fd1 | 217 | int verify_units(char **filenames, UnitFileScope scope, bool check_man, bool run_generators) { |
638cece4 LP |
218 | const ManagerTestRunFlags flags = |
219 | MANAGER_TEST_RUN_BASIC | | |
220 | MANAGER_TEST_RUN_ENV_GENERATORS | | |
221 | run_generators * MANAGER_TEST_RUN_GENERATORS; | |
8b835fcc | 222 | |
b5c9144c LP |
223 | _cleanup_(manager_freep) Manager *m = NULL; |
224 | Unit *units[strv_length(filenames)]; | |
225 | _cleanup_free_ char *var = NULL; | |
6f32f8e0 | 226 | int r, k, i, count = 0; |
b5c9144c LP |
227 | char **filename; |
228 | ||
1d3bc017 ZJS |
229 | if (strv_isempty(filenames)) |
230 | return 0; | |
231 | ||
8b835fcc ZJS |
232 | /* set the path */ |
233 | r = generate_path(&var, filenames); | |
23bbb0de MS |
234 | if (r < 0) |
235 | return log_error_errno(r, "Failed to generate unit load path: %m"); | |
8b835fcc ZJS |
236 | |
237 | assert_se(set_unit_path(var) >= 0); | |
238 | ||
641c0fd1 | 239 | r = manager_new(scope, flags, &m); |
23bbb0de | 240 | if (r < 0) |
ff9b60f3 | 241 | return log_error_errno(r, "Failed to initialize manager: %m"); |
8b835fcc ZJS |
242 | |
243 | log_debug("Starting manager..."); | |
244 | ||
76752516 | 245 | r = manager_startup(m, NULL, NULL); |
b5c9144c | 246 | if (r < 0) |
572986ca | 247 | return r; |
8b835fcc ZJS |
248 | |
249 | manager_clear_jobs(m); | |
250 | ||
251 | log_debug("Loading remaining units from the command line..."); | |
252 | ||
253 | STRV_FOREACH(filename, filenames) { | |
25f17e47 | 254 | _cleanup_free_ char *prepared = NULL; |
1d3bc017 | 255 | |
8b835fcc ZJS |
256 | log_debug("Handling %s...", *filename); |
257 | ||
25f17e47 EV |
258 | k = prepare_filename(*filename, &prepared); |
259 | if (k < 0) { | |
260 | log_error_errno(k, "Failed to prepare filename %s: %m", *filename); | |
261 | if (r == 0) | |
262 | r = k; | |
263 | continue; | |
264 | } | |
265 | ||
f79cd1a9 | 266 | k = manager_load_startable_unit_or_warn(m, NULL, prepared, &units[count]); |
fc9de36a HD |
267 | if (k < 0) { |
268 | if (r == 0) | |
269 | r = k; | |
270 | continue; | |
271 | } | |
272 | ||
273 | count++; | |
8b835fcc ZJS |
274 | } |
275 | ||
276 | for (i = 0; i < count; i++) { | |
1d3bc017 | 277 | k = verify_unit(units[i], check_man); |
8b835fcc ZJS |
278 | if (k < 0 && r == 0) |
279 | r = k; | |
280 | } | |
281 | ||
8b835fcc ZJS |
282 | return r; |
283 | } |