]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
7e4249b9 | 2 | |
03a7b521 LP |
3 | #include <errno.h> |
4 | #include <fcntl.h> | |
7e4249b9 | 5 | #include <getopt.h> |
a9cdc94f | 6 | #include <locale.h> |
7e4249b9 | 7 | #include <stdbool.h> |
03a7b521 LP |
8 | #include <stddef.h> |
9 | #include <stdio.h> | |
7e4249b9 | 10 | #include <string.h> |
4c787df7 | 11 | #include <sys/mount.h> |
4bb2e9d4 | 12 | #include <sys/prctl.h> |
03a7b521 | 13 | #include <sys/reboot.h> |
f1c5860b | 14 | #include <sys/socket.h> |
03a7b521 | 15 | #include <unistd.h> |
81527be1 | 16 | |
03a7b521 | 17 | #include "sd-bus.h" |
f459b602 | 18 | #include "sd-daemon.h" |
adb6cd9b | 19 | #include "sd-event.h" |
f459b602 | 20 | #include "sd-login.h" |
03a7b521 | 21 | |
b5efdb8a | 22 | #include "alloc-util.h" |
4bb2e9d4 | 23 | #include "bootspec.h" |
03a7b521 LP |
24 | #include "bus-common-errors.h" |
25 | #include "bus-error.h" | |
26 | #include "bus-message.h" | |
25b1d72d | 27 | #include "bus-unit-procs.h" |
291d565a | 28 | #include "bus-unit-util.h" |
03a7b521 | 29 | #include "bus-util.h" |
e45c81b8 | 30 | #include "bus-wait-for-jobs.h" |
d81bc51f | 31 | #include "bus-wait-for-units.h" |
ab35fb1b | 32 | #include "cgroup-show.h" |
c6c18be3 | 33 | #include "cgroup-util.h" |
cda134ab | 34 | #include "copy.h" |
a047f4f1 | 35 | #include "cpu-set-util.h" |
ad2a0358 | 36 | #include "dropin.h" |
5bdf2243 | 37 | #include "efivars.h" |
03a7b521 | 38 | #include "env-util.h" |
804ee07c | 39 | #include "escape.h" |
b3d59367 | 40 | #include "exec-util.h" |
03a7b521 | 41 | #include "exit-status.h" |
3ffd4af2 | 42 | #include "fd-util.h" |
f97b34a6 | 43 | #include "format-util.h" |
f4f15635 | 44 | #include "fs-util.h" |
7d50b32a | 45 | #include "glob-util.h" |
08f3be7a | 46 | #include "hexdecoct.h" |
c52a937b | 47 | #include "hostname-util.h" |
0a970718 | 48 | #include "in-addr-util.h" |
03a7b521 LP |
49 | #include "initreq.h" |
50 | #include "install.h" | |
c004493c | 51 | #include "io-util.h" |
305a2850 | 52 | #include "journal-util.h" |
03a7b521 | 53 | #include "list.h" |
8752c575 | 54 | #include "locale-util.h" |
03a7b521 LP |
55 | #include "log.h" |
56 | #include "logs-show.h" | |
57 | #include "macro.h" | |
d1405af3 | 58 | #include "main-func.h" |
0a970718 | 59 | #include "memory-util.h" |
03a7b521 LP |
60 | #include "mkdir.h" |
61 | #include "pager.h" | |
6bedfcbb | 62 | #include "parse-util.h" |
03a7b521 LP |
63 | #include "path-lookup.h" |
64 | #include "path-util.h" | |
294bf0c3 | 65 | #include "pretty-print.h" |
01771226 | 66 | #include "proc-cmdline.h" |
03a7b521 | 67 | #include "process-util.h" |
e3631d1c | 68 | #include "reboot-util.h" |
78f22b97 | 69 | #include "rlimit-util.h" |
03a7b521 | 70 | #include "set.h" |
592705f2 | 71 | #include "sigbus.h" |
24882e06 | 72 | #include "signal-util.h" |
03a7b521 | 73 | #include "socket-util.h" |
760877e9 | 74 | #include "sort-util.h" |
03a7b521 LP |
75 | #include "spawn-ask-password-agent.h" |
76 | #include "spawn-polkit-agent.h" | |
77 | #include "special.h" | |
8fcde012 | 78 | #include "stat-util.h" |
8559e61d | 79 | #include "string-table.h" |
03a7b521 | 80 | #include "strv.h" |
63a3b3cb | 81 | #include "sysv-compat.h" |
03a7b521 | 82 | #include "terminal-util.h" |
e4de7287 | 83 | #include "tmpfile-util.h" |
89ada3ba | 84 | #include "unit-def.h" |
03a7b521 | 85 | #include "unit-name.h" |
b1d4f8e1 | 86 | #include "user-util.h" |
305a2850 | 87 | #include "utf8.h" |
03a7b521 | 88 | #include "utmp-wtmp.h" |
e449de87 | 89 | #include "verbs.h" |
7f4b3c5e | 90 | #include "virt.h" |
7e4249b9 | 91 | |
20b3f379 | 92 | static char **arg_types = NULL; |
9b9b3d36 | 93 | static char **arg_states = NULL; |
20b3f379 | 94 | static char **arg_properties = NULL; |
7e4249b9 | 95 | static bool arg_all = false; |
afba4199 ZJS |
96 | static enum dependency { |
97 | DEPENDENCY_FORWARD, | |
98 | DEPENDENCY_REVERSE, | |
99 | DEPENDENCY_AFTER, | |
100 | DEPENDENCY_BEFORE, | |
071066a5 | 101 | _DEPENDENCY_MAX |
afba4199 | 102 | } arg_dependency = DEPENDENCY_FORWARD; |
e67c3609 | 103 | static const char *arg_job_mode = "replace"; |
729e3769 | 104 | static UnitFileScope arg_scope = UNIT_FILE_SYSTEM; |
93a08841 | 105 | static bool arg_wait = false; |
ee5762e3 | 106 | static bool arg_no_block = false; |
ebed32bf | 107 | static bool arg_no_legend = false; |
0221d68a | 108 | static PagerFlags arg_pager_flags = 0; |
e4b61340 | 109 | static bool arg_no_wtmp = false; |
f3f054f0 | 110 | static bool arg_no_sync = false; |
514f4ef5 | 111 | static bool arg_no_wall = false; |
ee5762e3 | 112 | static bool arg_no_reload = false; |
4f9a9105 | 113 | static bool arg_value = false; |
991f2a39 | 114 | static bool arg_show_types = false; |
b37844d3 | 115 | static bool arg_ignore_inhibitors = false; |
1ae17672 | 116 | static bool arg_dry_run = false; |
0183528f | 117 | static bool arg_quiet = false; |
ee5762e3 | 118 | static bool arg_full = false; |
1238ee09 | 119 | static bool arg_recursive = false; |
85d9b598 | 120 | static bool arg_show_transaction = false; |
e606bb61 | 121 | static int arg_force = 0; |
16f017fa | 122 | static bool arg_ask_password = false; |
729e3769 | 123 | static bool arg_runtime = false; |
d309c1c3 | 124 | static UnitFilePresetMode arg_preset_mode = UNIT_FILE_PRESET_FULL; |
e4b61340 | 125 | static char **arg_wall = NULL; |
8a0867d6 | 126 | static const char *arg_kill_who = NULL; |
8a0867d6 | 127 | static int arg_signal = SIGTERM; |
0f03c2a4 | 128 | static char *arg_root = NULL; |
f6144808 | 129 | static usec_t arg_when = 0; |
4445a875 | 130 | static enum action { |
e4b61340 LP |
131 | ACTION_SYSTEMCTL, |
132 | ACTION_HALT, | |
133 | ACTION_POWEROFF, | |
134 | ACTION_REBOOT, | |
20b09ca7 LP |
135 | ACTION_KEXEC, |
136 | ACTION_EXIT, | |
6edd7d0a LP |
137 | ACTION_SUSPEND, |
138 | ACTION_HIBERNATE, | |
6524990f | 139 | ACTION_HYBRID_SLEEP, |
e68c79db | 140 | ACTION_SUSPEND_THEN_HIBERNATE, |
e4b61340 LP |
141 | ACTION_RUNLEVEL2, |
142 | ACTION_RUNLEVEL3, | |
143 | ACTION_RUNLEVEL4, | |
144 | ACTION_RUNLEVEL5, | |
145 | ACTION_RESCUE, | |
514f4ef5 LP |
146 | ACTION_EMERGENCY, |
147 | ACTION_DEFAULT, | |
e4b61340 LP |
148 | ACTION_RELOAD, |
149 | ACTION_REEXEC, | |
150 | ACTION_RUNLEVEL, | |
f6144808 | 151 | ACTION_CANCEL_SHUTDOWN, |
913c1916 AJ |
152 | _ACTION_MAX, |
153 | _ACTION_INVALID = -1 | |
e4b61340 | 154 | } arg_action = ACTION_SYSTEMCTL; |
f459b602 | 155 | static BusTransport arg_transport = BUS_TRANSPORT_LOCAL; |
fc2ffaf1 | 156 | static const char *arg_host = NULL; |
df50185b LP |
157 | static unsigned arg_lines = 10; |
158 | static OutputMode arg_output = OUTPUT_SHORT; | |
5d0c05e5 | 159 | static bool arg_plain = false; |
5bdf2243 | 160 | static bool arg_firmware_setup = false; |
97af80c5 LP |
161 | static usec_t arg_boot_loader_menu = USEC_INFINITY; |
162 | static const char *arg_boot_loader_entry = NULL; | |
57ab2eab | 163 | static bool arg_now = false; |
82948f6c LP |
164 | static bool arg_jobs_before = false; |
165 | static bool arg_jobs_after = false; | |
d81bc51f | 166 | static char **arg_clean_what = NULL; |
1238ee09 | 167 | |
61ff45db LP |
168 | STATIC_DESTRUCTOR_REGISTER(arg_wall, strv_freep); |
169 | STATIC_DESTRUCTOR_REGISTER(arg_root, freep); | |
170 | STATIC_DESTRUCTOR_REGISTER(arg_types, strv_freep); | |
171 | STATIC_DESTRUCTOR_REGISTER(arg_states, strv_freep); | |
172 | STATIC_DESTRUCTOR_REGISTER(arg_properties, strv_freep); | |
d81bc51f | 173 | STATIC_DESTRUCTOR_REGISTER(arg_clean_what, strv_freep); |
61ff45db | 174 | |
e449de87 | 175 | static int daemon_reload(int argc, char *argv[], void* userdata); |
2853b60a | 176 | static int trivial_method(int argc, char *argv[], void *userdata); |
477def80 | 177 | static int halt_now(enum action a); |
fa0d5878 | 178 | static int get_state_one_unit(sd_bus *bus, const char *name, UnitActiveState *active_state); |
dbc2c080 | 179 | |
4fbd7192 LP |
180 | static bool original_stdout_is_tty; |
181 | ||
182 | typedef enum BusFocus { | |
183 | BUS_FULL, /* The full bus indicated via --system or --user */ | |
184 | BUS_MANAGER, /* The manager itself, possibly directly, possibly via the bus */ | |
185 | _BUS_FOCUS_MAX | |
186 | } BusFocus; | |
187 | ||
5238e957 | 188 | static sd_bus *buses[_BUS_FOCUS_MAX] = {}; |
4fbd7192 | 189 | |
b3796dd8 JS |
190 | static UnitFileFlags args_to_flags(void) { |
191 | return (arg_runtime ? UNIT_FILE_RUNTIME : 0) | | |
192 | (arg_force ? UNIT_FILE_FORCE : 0); | |
193 | } | |
194 | ||
4fbd7192 LP |
195 | static int acquire_bus(BusFocus focus, sd_bus **ret) { |
196 | int r; | |
197 | ||
198 | assert(focus < _BUS_FOCUS_MAX); | |
199 | assert(ret); | |
200 | ||
201 | /* We only go directly to the manager, if we are using a local transport */ | |
202 | if (arg_transport != BUS_TRANSPORT_LOCAL) | |
203 | focus = BUS_FULL; | |
204 | ||
d4015567 LP |
205 | if (getenv_bool("SYSTEMCTL_FORCE_BUS") > 0) |
206 | focus = BUS_FULL; | |
207 | ||
5238e957 | 208 | if (!buses[focus]) { |
4fbd7192 LP |
209 | bool user; |
210 | ||
211 | user = arg_scope != UNIT_FILE_SYSTEM; | |
212 | ||
213 | if (focus == BUS_MANAGER) | |
5238e957 | 214 | r = bus_connect_transport_systemd(arg_transport, arg_host, user, &buses[focus]); |
4fbd7192 | 215 | else |
5238e957 | 216 | r = bus_connect_transport(arg_transport, arg_host, user, &buses[focus]); |
4fbd7192 LP |
217 | if (r < 0) |
218 | return log_error_errno(r, "Failed to connect to bus: %m"); | |
219 | ||
5238e957 | 220 | (void) sd_bus_set_allow_interactive_authorization(buses[focus], arg_ask_password); |
4fbd7192 LP |
221 | } |
222 | ||
5238e957 | 223 | *ret = buses[focus]; |
4fbd7192 LP |
224 | return 0; |
225 | } | |
226 | ||
227 | static void release_busses(void) { | |
228 | BusFocus w; | |
229 | ||
230 | for (w = 0; w < _BUS_FOCUS_MAX; w++) | |
5238e957 | 231 | buses[w] = sd_bus_flush_close_unref(buses[w]); |
4fbd7192 LP |
232 | } |
233 | ||
6bb92a16 | 234 | static void ask_password_agent_open_if_enabled(void) { |
729e3769 | 235 | /* Open the password agent as a child process if necessary */ |
501fc174 | 236 | |
1ae17672 ZJS |
237 | if (arg_dry_run) |
238 | return; | |
239 | ||
501fc174 LP |
240 | if (!arg_ask_password) |
241 | return; | |
715554e7 | 242 | |
729e3769 | 243 | if (arg_scope != UNIT_FILE_SYSTEM) |
501fc174 LP |
244 | return; |
245 | ||
cbc9fbd1 LP |
246 | if (arg_transport != BUS_TRANSPORT_LOCAL) |
247 | return; | |
248 | ||
6bb92a16 LP |
249 | ask_password_agent_open(); |
250 | } | |
251 | ||
8a4b13c5 | 252 | static void polkit_agent_open_maybe(void) { |
6bb92a16 LP |
253 | /* Open the polkit agent as a child process if necessary */ |
254 | ||
6bb92a16 LP |
255 | if (arg_scope != UNIT_FILE_SYSTEM) |
256 | return; | |
257 | ||
8a4b13c5 | 258 | polkit_agent_open_if_enabled(arg_transport, arg_ask_password); |
501fc174 LP |
259 | } |
260 | ||
3c756001 LP |
261 | static OutputFlags get_output_flags(void) { |
262 | return | |
263 | arg_all * OUTPUT_SHOW_ALL | | |
459b9f9f | 264 | (arg_full || !on_tty() || pager_have()) * OUTPUT_FULL_WIDTH | |
40c9fe4c | 265 | colors_enabled() * OUTPUT_COLOR | |
8b0cc9a3 | 266 | !arg_quiet * OUTPUT_WARN_CUTOFF; |
3c756001 LP |
267 | } |
268 | ||
f459b602 | 269 | static int translate_bus_error_to_exit_status(int r, const sd_bus_error *error) { |
22f4096c LP |
270 | assert(error); |
271 | ||
f459b602 | 272 | if (!sd_bus_error_is_set(error)) |
22f4096c LP |
273 | return r; |
274 | ||
f459b602 MAP |
275 | if (sd_bus_error_has_name(error, SD_BUS_ERROR_ACCESS_DENIED) || |
276 | sd_bus_error_has_name(error, BUS_ERROR_ONLY_BY_DEPENDENCY) || | |
277 | sd_bus_error_has_name(error, BUS_ERROR_NO_ISOLATION) || | |
278 | sd_bus_error_has_name(error, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE)) | |
22f4096c LP |
279 | return EXIT_NOPERMISSION; |
280 | ||
f459b602 | 281 | if (sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT)) |
22f4096c LP |
282 | return EXIT_NOTINSTALLED; |
283 | ||
f459b602 | 284 | if (sd_bus_error_has_name(error, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE) || |
718db961 | 285 | sd_bus_error_has_name(error, SD_BUS_ERROR_NOT_SUPPORTED)) |
22f4096c LP |
286 | return EXIT_NOTIMPLEMENTED; |
287 | ||
f459b602 | 288 | if (sd_bus_error_has_name(error, BUS_ERROR_LOAD_FAILED)) |
22f4096c LP |
289 | return EXIT_NOTCONFIGURED; |
290 | ||
291 | if (r != 0) | |
292 | return r; | |
293 | ||
294 | return EXIT_FAILURE; | |
295 | } | |
296 | ||
4fbd7192 | 297 | static bool install_client_side(void) { |
4fbd7192 LP |
298 | /* Decides when to execute enable/disable/... operations |
299 | * client-side rather than server-side. */ | |
4f16c1f4 | 300 | |
f38951a6 | 301 | if (running_in_chroot_or_offline()) |
729e3769 LP |
302 | return true; |
303 | ||
304 | if (sd_booted() <= 0) | |
305 | return true; | |
306 | ||
307 | if (!isempty(arg_root)) | |
308 | return true; | |
309 | ||
310 | if (arg_scope == UNIT_FILE_GLOBAL) | |
311 | return true; | |
312 | ||
b41b9d2a LP |
313 | /* Unsupported environment variable, mostly for debugging purposes */ |
314 | if (getenv_bool("SYSTEMCTL_INSTALL_CLIENT_SIDE") > 0) | |
315 | return true; | |
316 | ||
729e3769 LP |
317 | return false; |
318 | } | |
319 | ||
93bab288 | 320 | static int compare_unit_info(const UnitInfo *a, const UnitInfo *b) { |
36c32ba2 | 321 | const char *d1, *d2; |
1238ee09 LP |
322 | int r; |
323 | ||
324 | /* First, order by machine */ | |
93bab288 | 325 | if (!a->machine && b->machine) |
1238ee09 | 326 | return -1; |
93bab288 | 327 | if (a->machine && !b->machine) |
1238ee09 | 328 | return 1; |
93bab288 YW |
329 | if (a->machine && b->machine) { |
330 | r = strcasecmp(a->machine, b->machine); | |
1238ee09 LP |
331 | if (r != 0) |
332 | return r; | |
333 | } | |
36c32ba2 | 334 | |
1238ee09 | 335 | /* Second, order by unit type */ |
93bab288 YW |
336 | d1 = strrchr(a->id, '.'); |
337 | d2 = strrchr(b->id, '.'); | |
36c32ba2 | 338 | if (d1 && d2) { |
f84190d8 LP |
339 | r = strcasecmp(d1, d2); |
340 | if (r != 0) | |
36c32ba2 LP |
341 | return r; |
342 | } | |
343 | ||
1238ee09 | 344 | /* Third, order by name */ |
93bab288 | 345 | return strcasecmp(a->id, b->id); |
36c32ba2 LP |
346 | } |
347 | ||
16484a8a ZJS |
348 | static const char* unit_type_suffix(const char *name) { |
349 | const char *dot; | |
350 | ||
351 | dot = strrchr(name, '.'); | |
352 | if (!dot) | |
353 | return ""; | |
354 | ||
355 | return dot + 1; | |
356 | } | |
357 | ||
d8fba7c6 | 358 | static bool output_show_unit(const UnitInfo *u, char **patterns) { |
f8591ee1 LP |
359 | assert(u); |
360 | ||
2404701e | 361 | if (!strv_fnmatch_or_empty(patterns, u->id, FNM_NOESCAPE)) |
d8fba7c6 | 362 | return false; |
d8fba7c6 | 363 | |
16484a8a ZJS |
364 | if (arg_types && !strv_find(arg_types, unit_type_suffix(u->id))) |
365 | return false; | |
6c71341a ZJS |
366 | |
367 | if (arg_all) | |
368 | return true; | |
369 | ||
409472cb FB |
370 | /* Note that '--all' is not purely a state filter, but also a |
371 | * filter that hides units that "follow" other units (which is | |
372 | * used for device units that appear under different names). */ | |
373 | if (!isempty(u->following)) | |
374 | return false; | |
375 | ||
ebc96265 FB |
376 | if (!strv_isempty(arg_states)) |
377 | return true; | |
378 | ||
379 | /* By default show all units except the ones in inactive | |
380 | * state and with no pending job */ | |
6c71341a ZJS |
381 | if (u->job_id > 0) |
382 | return true; | |
383 | ||
409472cb | 384 | if (streq(u->active_state, "inactive")) |
6c71341a ZJS |
385 | return false; |
386 | ||
387 | return true; | |
33330222 ZJS |
388 | } |
389 | ||
1238ee09 | 390 | static int output_units_list(const UnitInfo *unit_infos, unsigned c) { |
b5d7f1bb | 391 | unsigned circle_len = 0, id_len, max_id_len, load_len, active_len, sub_len, job_len, desc_len, max_desc_len; |
f459b602 | 392 | const UnitInfo *u; |
a1074881 | 393 | unsigned n_shown = 0; |
b5d7f1bb | 394 | int job_count = 0; |
0221d68a | 395 | bool full = arg_full || FLAGS_SET(arg_pager_flags, PAGER_DISABLE); |
33330222 | 396 | |
fbd0b64f LP |
397 | max_id_len = STRLEN("UNIT"); |
398 | load_len = STRLEN("LOAD"); | |
399 | active_len = STRLEN("ACTIVE"); | |
400 | sub_len = STRLEN("SUB"); | |
401 | job_len = STRLEN("JOB"); | |
402 | max_desc_len = STRLEN("DESCRIPTION"); | |
b036fc00 LP |
403 | |
404 | for (u = unit_infos; u < unit_infos + c; u++) { | |
1238ee09 | 405 | max_id_len = MAX(max_id_len, strlen(u->id) + (u->machine ? strlen(u->machine)+1 : 0)); |
a1074881 | 406 | load_len = MAX(load_len, strlen(u->load_state)); |
b036fc00 LP |
407 | active_len = MAX(active_len, strlen(u->active_state)); |
408 | sub_len = MAX(sub_len, strlen(u->sub_state)); | |
b5d7f1bb | 409 | max_desc_len = MAX(max_desc_len, strlen(u->description)); |
f459b602 | 410 | |
ccd41387 | 411 | if (u->job_id != 0) { |
b036fc00 | 412 | job_len = MAX(job_len, strlen(u->job_type)); |
ccd41387 ZJS |
413 | job_count++; |
414 | } | |
90c3f79d LP |
415 | |
416 | if (!arg_no_legend && | |
417 | (streq(u->active_state, "failed") || | |
c4555ad8 | 418 | STR_IN_SET(u->load_state, "error", "not-found", "bad-setting", "masked"))) |
90c3f79d | 419 | circle_len = 2; |
33330222 ZJS |
420 | } |
421 | ||
184ecaf7 | 422 | if (!arg_full && original_stdout_is_tty) { |
4deb3b93 | 423 | unsigned basic_len; |
cbc9fbd1 | 424 | |
b5d7f1bb | 425 | id_len = MIN(max_id_len, 25u); /* as much as it needs, but at most 25 for now */ |
a9bd056a | 426 | basic_len = circle_len + 1 + id_len + 1 + load_len + 1 + active_len + 1 + sub_len + 1; |
cbc9fbd1 | 427 | |
ccd41387 ZJS |
428 | if (job_count) |
429 | basic_len += job_len + 1; | |
cbc9fbd1 | 430 | |
4deb3b93 MS |
431 | if (basic_len < (unsigned) columns()) { |
432 | unsigned extra_len, incr; | |
433 | extra_len = columns() - basic_len; | |
cbc9fbd1 | 434 | |
4deb3b93 MS |
435 | /* Either UNIT already got 25, or is fully satisfied. |
436 | * Grant up to 25 to DESC now. */ | |
9607d947 | 437 | incr = MIN(extra_len, 25u); |
b5d7f1bb | 438 | desc_len = incr; |
4deb3b93 | 439 | extra_len -= incr; |
cbc9fbd1 | 440 | |
b5d7f1bb LP |
441 | /* Of the remainder give as much as the ID needs to the ID, and give the rest to the |
442 | * description but not more than it needs. */ | |
4deb3b93 | 443 | if (extra_len > 0) { |
b5d7f1bb | 444 | incr = MIN(max_id_len - id_len, extra_len); |
4deb3b93 | 445 | id_len += incr; |
b5d7f1bb | 446 | desc_len += MIN(extra_len - incr, max_desc_len - desc_len); |
4deb3b93 | 447 | } |
43479f8d ZJS |
448 | } else |
449 | desc_len = 0; | |
b5d7f1bb | 450 | } else { |
4deb3b93 | 451 | id_len = max_id_len; |
b5d7f1bb LP |
452 | desc_len = max_desc_len; |
453 | } | |
4deb3b93 | 454 | |
b036fc00 | 455 | for (u = unit_infos; u < unit_infos + c; u++) { |
1238ee09 | 456 | _cleanup_free_ char *e = NULL, *j = NULL; |
16484a8a | 457 | const char *on_underline = "", *off_underline = ""; |
90c3f79d LP |
458 | const char *on_loaded = "", *off_loaded = ""; |
459 | const char *on_active = "", *off_active = ""; | |
460 | const char *on_circle = "", *off_circle = ""; | |
1238ee09 | 461 | const char *id; |
16484a8a | 462 | bool circle = false, underline = false; |
b036fc00 | 463 | |
ad94ad63 | 464 | if (!n_shown && !arg_no_legend) { |
90c3f79d LP |
465 | |
466 | if (circle_len > 0) | |
467 | fputs(" ", stdout); | |
468 | ||
16484a8a ZJS |
469 | printf("%s%-*s %-*s %-*s %-*s ", |
470 | ansi_underline(), | |
cbc9fbd1 LP |
471 | id_len, "UNIT", |
472 | load_len, "LOAD", | |
473 | active_len, "ACTIVE", | |
474 | sub_len, "SUB"); | |
475 | ||
ccd41387 ZJS |
476 | if (job_count) |
477 | printf("%-*s ", job_len, "JOB"); | |
cbc9fbd1 | 478 | |
b5d7f1bb LP |
479 | printf("%-*.*s%s\n", |
480 | desc_len, | |
0221d68a | 481 | full ? -1 : (int) desc_len, |
16484a8a ZJS |
482 | "DESCRIPTION", |
483 | ansi_normal()); | |
ad94ad63 ZJS |
484 | } |
485 | ||
688c6725 LP |
486 | n_shown++; |
487 | ||
16484a8a ZJS |
488 | if (u + 1 < unit_infos + c && |
489 | !streq(unit_type_suffix(u->id), unit_type_suffix((u + 1)->id))) { | |
490 | on_underline = ansi_underline(); | |
491 | off_underline = ansi_normal(); | |
492 | underline = true; | |
493 | } | |
494 | ||
c4555ad8 | 495 | if (STR_IN_SET(u->load_state, "error", "not-found", "bad-setting", "masked") && !arg_plain) { |
90c3f79d | 496 | on_circle = ansi_highlight_yellow(); |
16484a8a | 497 | off_circle = ansi_normal(); |
90c3f79d | 498 | circle = true; |
16484a8a | 499 | on_loaded = underline ? ansi_highlight_red_underline() : ansi_highlight_red(); |
835a19e0 | 500 | off_loaded = underline ? on_underline : ansi_normal(); |
250ba664 | 501 | } else if (streq(u->active_state, "failed") && !arg_plain) { |
16484a8a ZJS |
502 | on_circle = ansi_highlight_red(); |
503 | off_circle = ansi_normal(); | |
90c3f79d | 504 | circle = true; |
16484a8a | 505 | on_active = underline ? ansi_highlight_red_underline() : ansi_highlight_red(); |
835a19e0 | 506 | off_active = underline ? on_underline : ansi_normal(); |
90c3f79d | 507 | } |
eb68c413 | 508 | |
1238ee09 | 509 | if (u->machine) { |
605405c6 | 510 | j = strjoin(u->machine, ":", u->id); |
1238ee09 LP |
511 | if (!j) |
512 | return log_oom(); | |
513 | ||
514 | id = j; | |
515 | } else | |
516 | id = u->id; | |
517 | ||
518 | if (arg_full) { | |
519 | e = ellipsize(id, id_len, 33); | |
520 | if (!e) | |
521 | return log_oom(); | |
522 | ||
523 | id = e; | |
524 | } | |
eb68c413 | 525 | |
90c3f79d | 526 | if (circle_len > 0) |
9a6f746f | 527 | printf("%s%s%s ", on_circle, circle ? special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE) : " ", off_circle); |
90c3f79d | 528 | |
16484a8a ZJS |
529 | printf("%s%s%-*s%s %s%-*s%s %s%-*s %-*s%s %-*s", |
530 | on_underline, | |
90c3f79d | 531 | on_active, id_len, id, off_active, |
a1074881 | 532 | on_loaded, load_len, u->load_state, off_loaded, |
b036fc00 LP |
533 | on_active, active_len, u->active_state, |
534 | sub_len, u->sub_state, off_active, | |
ccd41387 | 535 | job_count ? job_len + 1 : 0, u->job_id ? u->job_type : ""); |
cbc9fbd1 | 536 | |
b5d7f1bb LP |
537 | printf("%-*.*s%s\n", |
538 | desc_len, | |
0221d68a | 539 | full ? -1 : (int) desc_len, |
16484a8a ZJS |
540 | u->description, |
541 | off_underline); | |
eb68c413 ZJS |
542 | } |
543 | ||
ebed32bf | 544 | if (!arg_no_legend) { |
57f7ae4f ZJS |
545 | const char *on, *off; |
546 | ||
547 | if (n_shown) { | |
90c3f79d LP |
548 | puts("\n" |
549 | "LOAD = Reflects whether the unit definition was properly loaded.\n" | |
e3e0314b ZJS |
550 | "ACTIVE = The high-level unit activation state, i.e. generalization of SUB.\n" |
551 | "SUB = The low-level unit activation state, values depend on unit type."); | |
552 | puts(job_count ? "JOB = Pending job for the unit.\n" : ""); | |
0b5a519c | 553 | on = ansi_highlight(); |
1fc464f6 | 554 | off = ansi_normal(); |
57f7ae4f | 555 | } else { |
0b5a519c | 556 | on = ansi_highlight_red(); |
1fc464f6 | 557 | off = ansi_normal(); |
57f7ae4f | 558 | } |
eb68c413 ZJS |
559 | |
560 | if (arg_all) | |
48c2826b | 561 | printf("%s%u loaded units listed.%s\n" |
57f7ae4f ZJS |
562 | "To show all installed unit files use 'systemctl list-unit-files'.\n", |
563 | on, n_shown, off); | |
eb68c413 | 564 | else |
48c2826b | 565 | printf("%s%u loaded units listed.%s Pass --all to see loaded but inactive units, too.\n" |
57f7ae4f ZJS |
566 | "To show all installed unit files use 'systemctl list-unit-files'.\n", |
567 | on, n_shown, off); | |
eb68c413 | 568 | } |
1238ee09 LP |
569 | |
570 | return 0; | |
eb68c413 ZJS |
571 | } |
572 | ||
a00963a2 | 573 | static int get_unit_list( |
f459b602 | 574 | sd_bus *bus, |
1238ee09 LP |
575 | const char *machine, |
576 | char **patterns, | |
577 | UnitInfo **unit_infos, | |
578 | int c, | |
579 | sd_bus_message **_reply) { | |
a00963a2 | 580 | |
4afd3348 LP |
581 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
582 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
583 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
ca2d3784 | 584 | size_t size = c; |
1238ee09 | 585 | int r; |
f459b602 | 586 | UnitInfo u; |
313fe66f | 587 | bool fallback = false; |
7e4249b9 | 588 | |
265a7a2a | 589 | assert(bus); |
1238ee09 | 590 | assert(unit_infos); |
f459b602 | 591 | assert(_reply); |
1238ee09 | 592 | |
cdc06ed7 | 593 | r = sd_bus_message_new_method_call( |
f22f08cd | 594 | bus, |
cdc06ed7 | 595 | &m, |
f22f08cd SP |
596 | "org.freedesktop.systemd1", |
597 | "/org/freedesktop/systemd1", | |
598 | "org.freedesktop.systemd1.Manager", | |
313fe66f | 599 | "ListUnitsByPatterns"); |
cdc06ed7 DS |
600 | if (r < 0) |
601 | return bus_log_create_error(r); | |
602 | ||
603 | r = sd_bus_message_append_strv(m, arg_states); | |
604 | if (r < 0) | |
605 | return bus_log_create_error(r); | |
606 | ||
313fe66f | 607 | r = sd_bus_message_append_strv(m, patterns); |
608 | if (r < 0) | |
609 | return bus_log_create_error(r); | |
610 | ||
cdc06ed7 | 611 | r = sd_bus_call(bus, m, 0, &error, &reply); |
33d52725 ZJS |
612 | if (r < 0 && (sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_METHOD) || |
613 | sd_bus_error_has_name(&error, SD_BUS_ERROR_ACCESS_DENIED))) { | |
313fe66f | 614 | /* Fallback to legacy ListUnitsFiltered method */ |
615 | fallback = true; | |
616 | log_debug_errno(r, "Failed to list units: %s Falling back to ListUnitsFiltered method.", bus_error_message(&error, r)); | |
617 | m = sd_bus_message_unref(m); | |
618 | sd_bus_error_free(&error); | |
619 | ||
620 | r = sd_bus_message_new_method_call( | |
621 | bus, | |
622 | &m, | |
623 | "org.freedesktop.systemd1", | |
624 | "/org/freedesktop/systemd1", | |
625 | "org.freedesktop.systemd1.Manager", | |
626 | "ListUnitsFiltered"); | |
627 | if (r < 0) | |
628 | return bus_log_create_error(r); | |
629 | ||
630 | r = sd_bus_message_append_strv(m, arg_states); | |
631 | if (r < 0) | |
632 | return bus_log_create_error(r); | |
633 | ||
634 | r = sd_bus_call(bus, m, 0, &error, &reply); | |
635 | } | |
4c3e8e39 LP |
636 | if (r < 0) |
637 | return log_error_errno(r, "Failed to list units: %s", bus_error_message(&error, r)); | |
7e4249b9 | 638 | |
f459b602 MAP |
639 | r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssssouso)"); |
640 | if (r < 0) | |
641 | return bus_log_parse_error(r); | |
7e4249b9 | 642 | |
f459b602 | 643 | while ((r = bus_parse_unit_info(reply, &u)) > 0) { |
1238ee09 LP |
644 | u.machine = machine; |
645 | ||
313fe66f | 646 | if (!output_show_unit(&u, fallback ? patterns : NULL)) |
8d5ba5a9 | 647 | continue; |
7e4249b9 | 648 | |
1238ee09 | 649 | if (!GREEDY_REALLOC(*unit_infos, size, c+1)) |
f459b602 | 650 | return log_oom(); |
36c32ba2 | 651 | |
1238ee09 | 652 | (*unit_infos)[c++] = u; |
991f2a39 | 653 | } |
f459b602 MAP |
654 | if (r < 0) |
655 | return bus_log_parse_error(r); | |
991f2a39 | 656 | |
f459b602 MAP |
657 | r = sd_bus_message_exit_container(reply); |
658 | if (r < 0) | |
659 | return bus_log_parse_error(r); | |
660 | ||
1cc6c93a | 661 | *_reply = TAKE_PTR(reply); |
f459b602 | 662 | |
1238ee09 LP |
663 | return c; |
664 | } | |
665 | ||
666 | static void message_set_freep(Set **set) { | |
224b0e7a | 667 | set_free_with_destructor(*set, sd_bus_message_unref); |
1238ee09 LP |
668 | } |
669 | ||
670 | static int get_unit_list_recursive( | |
671 | sd_bus *bus, | |
672 | char **patterns, | |
673 | UnitInfo **_unit_infos, | |
674 | Set **_replies, | |
675 | char ***_machines) { | |
676 | ||
677 | _cleanup_free_ UnitInfo *unit_infos = NULL; | |
678 | _cleanup_(message_set_freep) Set *replies; | |
679 | sd_bus_message *reply; | |
680 | int c, r; | |
681 | ||
682 | assert(bus); | |
683 | assert(_replies); | |
684 | assert(_unit_infos); | |
685 | assert(_machines); | |
686 | ||
d5099efc | 687 | replies = set_new(NULL); |
1238ee09 LP |
688 | if (!replies) |
689 | return log_oom(); | |
690 | ||
691 | c = get_unit_list(bus, NULL, patterns, &unit_infos, 0, &reply); | |
692 | if (c < 0) | |
693 | return c; | |
694 | ||
695 | r = set_put(replies, reply); | |
696 | if (r < 0) { | |
697 | sd_bus_message_unref(reply); | |
4c3e8e39 | 698 | return log_oom(); |
1238ee09 LP |
699 | } |
700 | ||
701 | if (arg_recursive) { | |
702 | _cleanup_strv_free_ char **machines = NULL; | |
703 | char **i; | |
704 | ||
705 | r = sd_get_machine_names(&machines); | |
706 | if (r < 0) | |
4c3e8e39 | 707 | return log_error_errno(r, "Failed to get machine names: %m"); |
1238ee09 LP |
708 | |
709 | STRV_FOREACH(i, machines) { | |
4afd3348 | 710 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *container = NULL; |
1238ee09 LP |
711 | int k; |
712 | ||
de33fc62 | 713 | r = sd_bus_open_system_machine(&container, *i); |
1238ee09 | 714 | if (r < 0) { |
4c3e8e39 | 715 | log_warning_errno(r, "Failed to connect to container %s, ignoring: %m", *i); |
1238ee09 LP |
716 | continue; |
717 | } | |
718 | ||
719 | k = get_unit_list(container, *i, patterns, &unit_infos, c, &reply); | |
720 | if (k < 0) | |
721 | return k; | |
722 | ||
723 | c = k; | |
724 | ||
725 | r = set_put(replies, reply); | |
726 | if (r < 0) { | |
727 | sd_bus_message_unref(reply); | |
4c3e8e39 | 728 | return log_oom(); |
1238ee09 LP |
729 | } |
730 | } | |
731 | ||
1cc6c93a | 732 | *_machines = TAKE_PTR(machines); |
1238ee09 LP |
733 | } else |
734 | *_machines = NULL; | |
735 | ||
1cc6c93a | 736 | *_unit_infos = TAKE_PTR(unit_infos); |
1cc6c93a | 737 | *_replies = TAKE_PTR(replies); |
1238ee09 | 738 | |
f459b602 | 739 | return c; |
991f2a39 ZJS |
740 | } |
741 | ||
e449de87 | 742 | static int list_units(int argc, char *argv[], void *userdata) { |
f459b602 | 743 | _cleanup_free_ UnitInfo *unit_infos = NULL; |
1238ee09 LP |
744 | _cleanup_(message_set_freep) Set *replies = NULL; |
745 | _cleanup_strv_free_ char **machines = NULL; | |
4fbd7192 | 746 | sd_bus *bus; |
991f2a39 ZJS |
747 | int r; |
748 | ||
4fbd7192 LP |
749 | r = acquire_bus(BUS_MANAGER, &bus); |
750 | if (r < 0) | |
751 | return r; | |
752 | ||
0221d68a | 753 | (void) pager_open(arg_pager_flags); |
d2ad7e1b | 754 | |
e449de87 | 755 | r = get_unit_list_recursive(bus, strv_skip(argv, 1), &unit_infos, &replies, &machines); |
991f2a39 ZJS |
756 | if (r < 0) |
757 | return r; | |
758 | ||
93bab288 | 759 | typesafe_qsort(unit_infos, r, compare_unit_info); |
1238ee09 | 760 | return output_units_list(unit_infos, r); |
991f2a39 ZJS |
761 | } |
762 | ||
a00963a2 | 763 | static int get_triggered_units( |
f459b602 MAP |
764 | sd_bus *bus, |
765 | const char* path, | |
766 | char*** ret) { | |
a00963a2 | 767 | |
4afd3348 | 768 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
991f2a39 ZJS |
769 | int r; |
770 | ||
4fbd7192 LP |
771 | assert(bus); |
772 | assert(path); | |
773 | assert(ret); | |
774 | ||
f459b602 MAP |
775 | r = sd_bus_get_property_strv( |
776 | bus, | |
777 | "org.freedesktop.systemd1", | |
778 | path, | |
779 | "org.freedesktop.systemd1.Unit", | |
780 | "Triggers", | |
781 | &error, | |
782 | ret); | |
f459b602 | 783 | if (r < 0) |
691395d8 | 784 | return log_error_errno(r, "Failed to determine triggers: %s", bus_error_message(&error, r)); |
991f2a39 ZJS |
785 | |
786 | return 0; | |
787 | } | |
788 | ||
f459b602 MAP |
789 | static int get_listening( |
790 | sd_bus *bus, | |
791 | const char* unit_path, | |
cbb76c29 | 792 | char*** listening) { |
f459b602 | 793 | |
4afd3348 LP |
794 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
795 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
f459b602 | 796 | const char *type, *path; |
cbb76c29 | 797 | int r, n = 0; |
991f2a39 | 798 | |
f459b602 MAP |
799 | r = sd_bus_get_property( |
800 | bus, | |
801 | "org.freedesktop.systemd1", | |
802 | unit_path, | |
803 | "org.freedesktop.systemd1.Socket", | |
804 | "Listen", | |
805 | &error, | |
806 | &reply, | |
807 | "a(ss)"); | |
691395d8 LP |
808 | if (r < 0) |
809 | return log_error_errno(r, "Failed to get list of listening sockets: %s", bus_error_message(&error, r)); | |
991f2a39 | 810 | |
f459b602 MAP |
811 | r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ss)"); |
812 | if (r < 0) | |
813 | return bus_log_parse_error(r); | |
991f2a39 | 814 | |
f459b602 | 815 | while ((r = sd_bus_message_read(reply, "(ss)", &type, &path)) > 0) { |
36c32ba2 | 816 | |
0d95178e | 817 | r = strv_extend(listening, type); |
f459b602 MAP |
818 | if (r < 0) |
819 | return log_oom(); | |
991f2a39 | 820 | |
0d95178e | 821 | r = strv_extend(listening, path); |
f459b602 MAP |
822 | if (r < 0) |
823 | return log_oom(); | |
7e4249b9 | 824 | |
cbb76c29 | 825 | n++; |
36c32ba2 | 826 | } |
f459b602 MAP |
827 | if (r < 0) |
828 | return bus_log_parse_error(r); | |
829 | ||
830 | r = sd_bus_message_exit_container(reply); | |
831 | if (r < 0) | |
832 | return bus_log_parse_error(r); | |
36c32ba2 | 833 | |
cbb76c29 | 834 | return n; |
991f2a39 ZJS |
835 | } |
836 | ||
837 | struct socket_info { | |
0cfc3525 | 838 | const char *machine; |
991f2a39 ZJS |
839 | const char* id; |
840 | ||
841 | char* type; | |
842 | char* path; | |
843 | ||
844 | /* Note: triggered is a list here, although it almost certainly | |
845 | * will always be one unit. Nevertheless, dbus API allows for multiple | |
f7340ab2 | 846 | * values, so let's follow that. */ |
991f2a39 ZJS |
847 | char** triggered; |
848 | ||
849 | /* The strv above is shared. free is set only in the first one. */ | |
850 | bool own_triggered; | |
851 | }; | |
852 | ||
cbb76c29 | 853 | static int socket_info_compare(const struct socket_info *a, const struct socket_info *b) { |
93bab288 | 854 | int r; |
cbc9fbd1 | 855 | |
cbb76c29 LP |
856 | assert(a); |
857 | assert(b); | |
858 | ||
0cfc3525 TA |
859 | if (!a->machine && b->machine) |
860 | return -1; | |
861 | if (a->machine && !b->machine) | |
862 | return 1; | |
863 | if (a->machine && b->machine) { | |
93bab288 YW |
864 | r = strcasecmp(a->machine, b->machine); |
865 | if (r != 0) | |
866 | return r; | |
0cfc3525 TA |
867 | } |
868 | ||
93bab288 YW |
869 | r = strcmp(a->path, b->path); |
870 | if (r == 0) | |
871 | r = strcmp(a->type, b->type); | |
cbc9fbd1 | 872 | |
93bab288 | 873 | return r; |
991f2a39 ZJS |
874 | } |
875 | ||
876 | static int output_sockets_list(struct socket_info *socket_infos, unsigned cs) { | |
877 | struct socket_info *s; | |
fbd0b64f LP |
878 | unsigned pathlen = STRLEN("LISTEN"), |
879 | typelen = STRLEN("TYPE") * arg_show_types, | |
880 | socklen = STRLEN("UNIT"), | |
881 | servlen = STRLEN("ACTIVATES"); | |
991f2a39 ZJS |
882 | const char *on, *off; |
883 | ||
884 | for (s = socket_infos; s < socket_infos + cs; s++) { | |
991f2a39 | 885 | unsigned tmp = 0; |
cbc9fbd1 | 886 | char **a; |
991f2a39 ZJS |
887 | |
888 | socklen = MAX(socklen, strlen(s->id)); | |
889 | if (arg_show_types) | |
890 | typelen = MAX(typelen, strlen(s->type)); | |
0cfc3525 | 891 | pathlen = MAX(pathlen, strlen(s->path) + (s->machine ? strlen(s->machine)+1 : 0)); |
991f2a39 ZJS |
892 | |
893 | STRV_FOREACH(a, s->triggered) | |
894 | tmp += strlen(*a) + 2*(a != s->triggered); | |
895 | servlen = MAX(servlen, tmp); | |
896 | } | |
897 | ||
898 | if (cs) { | |
571bfc6c MM |
899 | if (!arg_no_legend) |
900 | printf("%-*s %-*.*s%-*s %s\n", | |
901 | pathlen, "LISTEN", | |
902 | typelen + arg_show_types, typelen + arg_show_types, "TYPE ", | |
903 | socklen, "UNIT", | |
904 | "ACTIVATES"); | |
991f2a39 ZJS |
905 | |
906 | for (s = socket_infos; s < socket_infos + cs; s++) { | |
0cfc3525 TA |
907 | _cleanup_free_ char *j = NULL; |
908 | const char *path; | |
991f2a39 ZJS |
909 | char **a; |
910 | ||
0cfc3525 | 911 | if (s->machine) { |
605405c6 | 912 | j = strjoin(s->machine, ":", s->path); |
0cfc3525 TA |
913 | if (!j) |
914 | return log_oom(); | |
915 | path = j; | |
916 | } else | |
917 | path = s->path; | |
918 | ||
991f2a39 ZJS |
919 | if (arg_show_types) |
920 | printf("%-*s %-*s %-*s", | |
0cfc3525 | 921 | pathlen, path, typelen, s->type, socklen, s->id); |
991f2a39 ZJS |
922 | else |
923 | printf("%-*s %-*s", | |
0cfc3525 | 924 | pathlen, path, socklen, s->id); |
991f2a39 ZJS |
925 | STRV_FOREACH(a, s->triggered) |
926 | printf("%s %s", | |
927 | a == s->triggered ? "" : ",", *a); | |
928 | printf("\n"); | |
929 | } | |
930 | ||
0b5a519c | 931 | on = ansi_highlight(); |
1fc464f6 | 932 | off = ansi_normal(); |
571bfc6c MM |
933 | if (!arg_no_legend) |
934 | printf("\n"); | |
991f2a39 | 935 | } else { |
0b5a519c | 936 | on = ansi_highlight_red(); |
1fc464f6 | 937 | off = ansi_normal(); |
991f2a39 ZJS |
938 | } |
939 | ||
571bfc6c MM |
940 | if (!arg_no_legend) { |
941 | printf("%s%u sockets listed.%s\n", on, cs, off); | |
942 | if (!arg_all) | |
943 | printf("Pass --all to see loaded but inactive sockets, too.\n"); | |
944 | } | |
265a7a2a ZJS |
945 | |
946 | return 0; | |
947 | } | |
948 | ||
e449de87 | 949 | static int list_sockets(int argc, char *argv[], void *userdata) { |
0cfc3525 TA |
950 | _cleanup_(message_set_freep) Set *replies = NULL; |
951 | _cleanup_strv_free_ char **machines = NULL; | |
f459b602 | 952 | _cleanup_free_ UnitInfo *unit_infos = NULL; |
8d5ba5a9 | 953 | _cleanup_free_ struct socket_info *socket_infos = NULL; |
f459b602 | 954 | const UnitInfo *u; |
991f2a39 | 955 | struct socket_info *s; |
cbb76c29 | 956 | unsigned cs = 0; |
991f2a39 | 957 | size_t size = 0; |
ad83b4c4 | 958 | int r = 0, n; |
4fbd7192 | 959 | sd_bus *bus; |
265a7a2a | 960 | |
4fbd7192 LP |
961 | r = acquire_bus(BUS_MANAGER, &bus); |
962 | if (r < 0) | |
963 | return r; | |
964 | ||
0221d68a | 965 | (void) pager_open(arg_pager_flags); |
d2ad7e1b | 966 | |
e449de87 | 967 | n = get_unit_list_recursive(bus, strv_skip(argv, 1), &unit_infos, &replies, &machines); |
cbb76c29 LP |
968 | if (n < 0) |
969 | return n; | |
265a7a2a | 970 | |
cbb76c29 | 971 | for (u = unit_infos; u < unit_infos + n; u++) { |
0d95178e | 972 | _cleanup_strv_free_ char **listening = NULL, **triggered = NULL; |
cbb76c29 | 973 | int i, c; |
991f2a39 | 974 | |
cbc9fbd1 | 975 | if (!endswith(u->id, ".socket")) |
991f2a39 ZJS |
976 | continue; |
977 | ||
978 | r = get_triggered_units(bus, u->unit_path, &triggered); | |
979 | if (r < 0) | |
980 | goto cleanup; | |
981 | ||
cbb76c29 LP |
982 | c = get_listening(bus, u->unit_path, &listening); |
983 | if (c < 0) { | |
984 | r = c; | |
991f2a39 | 985 | goto cleanup; |
cbb76c29 | 986 | } |
991f2a39 ZJS |
987 | |
988 | if (!GREEDY_REALLOC(socket_infos, size, cs + c)) { | |
989 | r = log_oom(); | |
990 | goto cleanup; | |
991 | } | |
992 | ||
993 | for (i = 0; i < c; i++) | |
994 | socket_infos[cs + i] = (struct socket_info) { | |
0cfc3525 | 995 | .machine = u->machine, |
991f2a39 | 996 | .id = u->id, |
0d95178e KS |
997 | .type = listening[i*2], |
998 | .path = listening[i*2 + 1], | |
991f2a39 ZJS |
999 | .triggered = triggered, |
1000 | .own_triggered = i==0, | |
1001 | }; | |
1002 | ||
1003 | /* from this point on we will cleanup those socket_infos */ | |
1004 | cs += c; | |
0d95178e KS |
1005 | free(listening); |
1006 | listening = triggered = NULL; /* avoid cleanup */ | |
991f2a39 ZJS |
1007 | } |
1008 | ||
93bab288 | 1009 | typesafe_qsort(socket_infos, cs, socket_info_compare); |
991f2a39 ZJS |
1010 | |
1011 | output_sockets_list(socket_infos, cs); | |
1012 | ||
1013 | cleanup: | |
1014 | assert(cs == 0 || socket_infos); | |
1015 | for (s = socket_infos; s < socket_infos + cs; s++) { | |
1016 | free(s->type); | |
1017 | free(s->path); | |
1018 | if (s->own_triggered) | |
1019 | strv_free(s->triggered); | |
1020 | } | |
4445a875 | 1021 | |
872c8faa | 1022 | return r; |
4445a875 LP |
1023 | } |
1024 | ||
cbb76c29 LP |
1025 | static int get_next_elapse( |
1026 | sd_bus *bus, | |
1027 | const char *path, | |
1028 | dual_timestamp *next) { | |
1029 | ||
4afd3348 | 1030 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
cbb76c29 LP |
1031 | dual_timestamp t; |
1032 | int r; | |
1033 | ||
1034 | assert(bus); | |
1035 | assert(path); | |
1036 | assert(next); | |
1037 | ||
1038 | r = sd_bus_get_property_trivial( | |
1039 | bus, | |
1040 | "org.freedesktop.systemd1", | |
1041 | path, | |
1042 | "org.freedesktop.systemd1.Timer", | |
1043 | "NextElapseUSecMonotonic", | |
1044 | &error, | |
1045 | 't', | |
1046 | &t.monotonic); | |
691395d8 | 1047 | if (r < 0) |
021723c6 | 1048 | return log_error_errno(r, "Failed to get next elapse time: %s", bus_error_message(&error, r)); |
cbb76c29 LP |
1049 | |
1050 | r = sd_bus_get_property_trivial( | |
1051 | bus, | |
1052 | "org.freedesktop.systemd1", | |
1053 | path, | |
1054 | "org.freedesktop.systemd1.Timer", | |
1055 | "NextElapseUSecRealtime", | |
1056 | &error, | |
1057 | 't', | |
1058 | &t.realtime); | |
691395d8 | 1059 | if (r < 0) |
021723c6 | 1060 | return log_error_errno(r, "Failed to get next elapse time: %s", bus_error_message(&error, r)); |
cbb76c29 LP |
1061 | |
1062 | *next = t; | |
1063 | return 0; | |
1064 | } | |
1065 | ||
d784e2db LP |
1066 | static int get_last_trigger( |
1067 | sd_bus *bus, | |
1068 | const char *path, | |
1069 | usec_t *last) { | |
1070 | ||
4afd3348 | 1071 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
d784e2db LP |
1072 | int r; |
1073 | ||
1074 | assert(bus); | |
1075 | assert(path); | |
1076 | assert(last); | |
1077 | ||
1078 | r = sd_bus_get_property_trivial( | |
1079 | bus, | |
1080 | "org.freedesktop.systemd1", | |
1081 | path, | |
1082 | "org.freedesktop.systemd1.Timer", | |
dedabea4 | 1083 | "LastTriggerUSec", |
d784e2db LP |
1084 | &error, |
1085 | 't', | |
1086 | last); | |
691395d8 LP |
1087 | if (r < 0) |
1088 | return log_error_errno(r, "Failed to get last trigger time: %s", bus_error_message(&error, r)); | |
d784e2db LP |
1089 | |
1090 | return 0; | |
1091 | } | |
1092 | ||
cbb76c29 | 1093 | struct timer_info { |
806a37e7 | 1094 | const char* machine; |
cbb76c29 LP |
1095 | const char* id; |
1096 | usec_t next_elapse; | |
d784e2db | 1097 | usec_t last_trigger; |
cbb76c29 LP |
1098 | char** triggered; |
1099 | }; | |
1100 | ||
1101 | static int timer_info_compare(const struct timer_info *a, const struct timer_info *b) { | |
93bab288 | 1102 | int r; |
806a37e7 | 1103 | |
cbb76c29 LP |
1104 | assert(a); |
1105 | assert(b); | |
1106 | ||
806a37e7 TA |
1107 | if (!a->machine && b->machine) |
1108 | return -1; | |
1109 | if (a->machine && !b->machine) | |
1110 | return 1; | |
1111 | if (a->machine && b->machine) { | |
93bab288 YW |
1112 | r = strcasecmp(a->machine, b->machine); |
1113 | if (r != 0) | |
1114 | return r; | |
806a37e7 TA |
1115 | } |
1116 | ||
93bab288 YW |
1117 | r = CMP(a->next_elapse, b->next_elapse); |
1118 | if (r != 0) | |
1119 | return r; | |
cbb76c29 LP |
1120 | |
1121 | return strcmp(a->id, b->id); | |
1122 | } | |
1123 | ||
1124 | static int output_timers_list(struct timer_info *timer_infos, unsigned n) { | |
1125 | struct timer_info *t; | |
1126 | unsigned | |
fbd0b64f LP |
1127 | nextlen = STRLEN("NEXT"), |
1128 | leftlen = STRLEN("LEFT"), | |
1129 | lastlen = STRLEN("LAST"), | |
1130 | passedlen = STRLEN("PASSED"), | |
1131 | unitlen = STRLEN("UNIT"), | |
1132 | activatelen = STRLEN("ACTIVATES"); | |
cbb76c29 LP |
1133 | |
1134 | const char *on, *off; | |
1135 | ||
1136 | assert(timer_infos || n == 0); | |
1137 | ||
1138 | for (t = timer_infos; t < timer_infos + n; t++) { | |
1139 | unsigned ul = 0; | |
1140 | char **a; | |
1141 | ||
1142 | if (t->next_elapse > 0) { | |
1143 | char tstamp[FORMAT_TIMESTAMP_MAX] = "", trel[FORMAT_TIMESTAMP_RELATIVE_MAX] = ""; | |
1144 | ||
1145 | format_timestamp(tstamp, sizeof(tstamp), t->next_elapse); | |
1146 | nextlen = MAX(nextlen, strlen(tstamp) + 1); | |
1147 | ||
1148 | format_timestamp_relative(trel, sizeof(trel), t->next_elapse); | |
1149 | leftlen = MAX(leftlen, strlen(trel)); | |
1150 | } | |
1151 | ||
d784e2db LP |
1152 | if (t->last_trigger > 0) { |
1153 | char tstamp[FORMAT_TIMESTAMP_MAX] = "", trel[FORMAT_TIMESTAMP_RELATIVE_MAX] = ""; | |
1154 | ||
1155 | format_timestamp(tstamp, sizeof(tstamp), t->last_trigger); | |
1156 | lastlen = MAX(lastlen, strlen(tstamp) + 1); | |
1157 | ||
1158 | format_timestamp_relative(trel, sizeof(trel), t->last_trigger); | |
1159 | passedlen = MAX(passedlen, strlen(trel)); | |
1160 | } | |
1161 | ||
806a37e7 | 1162 | unitlen = MAX(unitlen, strlen(t->id) + (t->machine ? strlen(t->machine)+1 : 0)); |
cbb76c29 LP |
1163 | |
1164 | STRV_FOREACH(a, t->triggered) | |
1165 | ul += strlen(*a) + 2*(a != t->triggered); | |
d784e2db | 1166 | |
cbb76c29 LP |
1167 | activatelen = MAX(activatelen, ul); |
1168 | } | |
1169 | ||
1170 | if (n > 0) { | |
1171 | if (!arg_no_legend) | |
d784e2db LP |
1172 | printf("%-*s %-*s %-*s %-*s %-*s %s\n", |
1173 | nextlen, "NEXT", | |
1174 | leftlen, "LEFT", | |
1175 | lastlen, "LAST", | |
1176 | passedlen, "PASSED", | |
1177 | unitlen, "UNIT", | |
1178 | "ACTIVATES"); | |
cbb76c29 LP |
1179 | |
1180 | for (t = timer_infos; t < timer_infos + n; t++) { | |
806a37e7 TA |
1181 | _cleanup_free_ char *j = NULL; |
1182 | const char *unit; | |
d784e2db LP |
1183 | char tstamp1[FORMAT_TIMESTAMP_MAX] = "n/a", trel1[FORMAT_TIMESTAMP_RELATIVE_MAX] = "n/a"; |
1184 | char tstamp2[FORMAT_TIMESTAMP_MAX] = "n/a", trel2[FORMAT_TIMESTAMP_RELATIVE_MAX] = "n/a"; | |
cbb76c29 LP |
1185 | char **a; |
1186 | ||
d784e2db LP |
1187 | format_timestamp(tstamp1, sizeof(tstamp1), t->next_elapse); |
1188 | format_timestamp_relative(trel1, sizeof(trel1), t->next_elapse); | |
cbb76c29 | 1189 | |
d784e2db LP |
1190 | format_timestamp(tstamp2, sizeof(tstamp2), t->last_trigger); |
1191 | format_timestamp_relative(trel2, sizeof(trel2), t->last_trigger); | |
1192 | ||
806a37e7 | 1193 | if (t->machine) { |
605405c6 | 1194 | j = strjoin(t->machine, ":", t->id); |
806a37e7 TA |
1195 | if (!j) |
1196 | return log_oom(); | |
1197 | unit = j; | |
1198 | } else | |
1199 | unit = t->id; | |
1200 | ||
d784e2db | 1201 | printf("%-*s %-*s %-*s %-*s %-*s", |
806a37e7 | 1202 | nextlen, tstamp1, leftlen, trel1, lastlen, tstamp2, passedlen, trel2, unitlen, unit); |
cbb76c29 LP |
1203 | |
1204 | STRV_FOREACH(a, t->triggered) | |
1205 | printf("%s %s", | |
1206 | a == t->triggered ? "" : ",", *a); | |
1207 | printf("\n"); | |
1208 | } | |
1209 | ||
1210 | on = ansi_highlight(); | |
1fc464f6 | 1211 | off = ansi_normal(); |
cbb76c29 LP |
1212 | if (!arg_no_legend) |
1213 | printf("\n"); | |
1214 | } else { | |
1215 | on = ansi_highlight_red(); | |
1fc464f6 | 1216 | off = ansi_normal(); |
cbb76c29 LP |
1217 | } |
1218 | ||
1219 | if (!arg_no_legend) { | |
1220 | printf("%s%u timers listed.%s\n", on, n, off); | |
1221 | if (!arg_all) | |
1222 | printf("Pass --all to see loaded but inactive timers, too.\n"); | |
1223 | } | |
1224 | ||
1225 | return 0; | |
1226 | } | |
1227 | ||
f5080e73 DH |
1228 | static usec_t calc_next_elapse(dual_timestamp *nw, dual_timestamp *next) { |
1229 | usec_t next_elapse; | |
1230 | ||
1231 | assert(nw); | |
1232 | assert(next); | |
1233 | ||
1f65fd49 | 1234 | if (timestamp_is_set(next->monotonic)) { |
f5080e73 DH |
1235 | usec_t converted; |
1236 | ||
1237 | if (next->monotonic > nw->monotonic) | |
1238 | converted = nw->realtime + (next->monotonic - nw->monotonic); | |
1239 | else | |
1240 | converted = nw->realtime - (nw->monotonic - next->monotonic); | |
1241 | ||
1f65fd49 | 1242 | if (timestamp_is_set(next->realtime)) |
f5080e73 DH |
1243 | next_elapse = MIN(converted, next->realtime); |
1244 | else | |
1245 | next_elapse = converted; | |
1246 | ||
1247 | } else | |
1248 | next_elapse = next->realtime; | |
1249 | ||
1250 | return next_elapse; | |
1251 | } | |
1252 | ||
e449de87 | 1253 | static int list_timers(int argc, char *argv[], void *userdata) { |
806a37e7 TA |
1254 | _cleanup_(message_set_freep) Set *replies = NULL; |
1255 | _cleanup_strv_free_ char **machines = NULL; | |
cbb76c29 LP |
1256 | _cleanup_free_ struct timer_info *timer_infos = NULL; |
1257 | _cleanup_free_ UnitInfo *unit_infos = NULL; | |
1258 | struct timer_info *t; | |
1259 | const UnitInfo *u; | |
1260 | size_t size = 0; | |
1823b86e | 1261 | int n, c = 0; |
cbb76c29 | 1262 | dual_timestamp nw; |
4fbd7192 | 1263 | sd_bus *bus; |
1823b86e | 1264 | int r = 0; |
cbb76c29 | 1265 | |
4fbd7192 LP |
1266 | r = acquire_bus(BUS_MANAGER, &bus); |
1267 | if (r < 0) | |
1268 | return r; | |
1269 | ||
0221d68a | 1270 | (void) pager_open(arg_pager_flags); |
d2ad7e1b | 1271 | |
e449de87 | 1272 | n = get_unit_list_recursive(bus, strv_skip(argv, 1), &unit_infos, &replies, &machines); |
cbb76c29 LP |
1273 | if (n < 0) |
1274 | return n; | |
1275 | ||
1276 | dual_timestamp_get(&nw); | |
1277 | ||
1278 | for (u = unit_infos; u < unit_infos + n; u++) { | |
1279 | _cleanup_strv_free_ char **triggered = NULL; | |
5cb14b37 | 1280 | dual_timestamp next = DUAL_TIMESTAMP_NULL; |
d784e2db | 1281 | usec_t m, last = 0; |
cbb76c29 | 1282 | |
cbb76c29 LP |
1283 | if (!endswith(u->id, ".timer")) |
1284 | continue; | |
1285 | ||
1286 | r = get_triggered_units(bus, u->unit_path, &triggered); | |
1287 | if (r < 0) | |
1288 | goto cleanup; | |
1289 | ||
1290 | r = get_next_elapse(bus, u->unit_path, &next); | |
1291 | if (r < 0) | |
1292 | goto cleanup; | |
1293 | ||
d784e2db LP |
1294 | get_last_trigger(bus, u->unit_path, &last); |
1295 | ||
cbb76c29 LP |
1296 | if (!GREEDY_REALLOC(timer_infos, size, c+1)) { |
1297 | r = log_oom(); | |
1298 | goto cleanup; | |
1299 | } | |
1300 | ||
f5080e73 DH |
1301 | m = calc_next_elapse(&nw, &next); |
1302 | ||
cbb76c29 | 1303 | timer_infos[c++] = (struct timer_info) { |
806a37e7 | 1304 | .machine = u->machine, |
cbb76c29 LP |
1305 | .id = u->id, |
1306 | .next_elapse = m, | |
d784e2db | 1307 | .last_trigger = last, |
1cc6c93a | 1308 | .triggered = TAKE_PTR(triggered), |
cbb76c29 | 1309 | }; |
cbb76c29 LP |
1310 | } |
1311 | ||
93bab288 | 1312 | typesafe_qsort(timer_infos, c, timer_info_compare); |
cbb76c29 LP |
1313 | |
1314 | output_timers_list(timer_infos, c); | |
1315 | ||
1316 | cleanup: | |
1317 | for (t = timer_infos; t < timer_infos + c; t++) | |
1318 | strv_free(t->triggered); | |
1319 | ||
1320 | return r; | |
1321 | } | |
1322 | ||
93bab288 | 1323 | static int compare_unit_file_list(const UnitFileList *a, const UnitFileList *b) { |
729e3769 | 1324 | const char *d1, *d2; |
729e3769 | 1325 | |
93bab288 YW |
1326 | d1 = strrchr(a->path, '.'); |
1327 | d2 = strrchr(b->path, '.'); | |
729e3769 LP |
1328 | |
1329 | if (d1 && d2) { | |
1330 | int r; | |
1331 | ||
1332 | r = strcasecmp(d1, d2); | |
1333 | if (r != 0) | |
1334 | return r; | |
1335 | } | |
1336 | ||
93bab288 | 1337 | return strcasecmp(basename(a->path), basename(b->path)); |
729e3769 LP |
1338 | } |
1339 | ||
313fe66f | 1340 | static bool output_show_unit_file(const UnitFileList *u, char **states, char **patterns) { |
f8591ee1 LP |
1341 | assert(u); |
1342 | ||
2404701e | 1343 | if (!strv_fnmatch_or_empty(patterns, basename(u->path), FNM_NOESCAPE)) |
d8fba7c6 | 1344 | return false; |
d8fba7c6 | 1345 | |
6c71341a ZJS |
1346 | if (!strv_isempty(arg_types)) { |
1347 | const char *dot; | |
1348 | ||
1349 | dot = strrchr(u->path, '.'); | |
1350 | if (!dot) | |
1351 | return false; | |
1352 | ||
1353 | if (!strv_find(arg_types, dot+1)) | |
1354 | return false; | |
1355 | } | |
1356 | ||
313fe66f | 1357 | if (!strv_isempty(states) && |
1358 | !strv_find(states, unit_file_state_to_string(u->state))) | |
bceccd5e | 1359 | return false; |
fec1530e | 1360 | |
6c71341a | 1361 | return true; |
729e3769 LP |
1362 | } |
1363 | ||
8d5ba5a9 | 1364 | static void output_unit_file_list(const UnitFileList *units, unsigned c) { |
0d292f5e | 1365 | unsigned max_id_len, id_cols, state_cols; |
729e3769 LP |
1366 | const UnitFileList *u; |
1367 | ||
fbd0b64f LP |
1368 | max_id_len = STRLEN("UNIT FILE"); |
1369 | state_cols = STRLEN("STATE"); | |
cbc9fbd1 | 1370 | |
1c0a113f | 1371 | for (u = units; u < units + c; u++) { |
2b6bf07d | 1372 | max_id_len = MAX(max_id_len, strlen(basename(u->path))); |
1c0a113f ZJS |
1373 | state_cols = MAX(state_cols, strlen(unit_file_state_to_string(u->state))); |
1374 | } | |
1375 | ||
1376 | if (!arg_full) { | |
1377 | unsigned basic_cols; | |
cbc9fbd1 | 1378 | |
9607d947 | 1379 | id_cols = MIN(max_id_len, 25u); |
1c0a113f ZJS |
1380 | basic_cols = 1 + id_cols + state_cols; |
1381 | if (basic_cols < (unsigned) columns()) | |
1382 | id_cols += MIN(columns() - basic_cols, max_id_len - id_cols); | |
1383 | } else | |
1384 | id_cols = max_id_len; | |
1385 | ||
11690bcc | 1386 | if (!arg_no_legend && c > 0) |
6ae3e62a ZJS |
1387 | printf("%s%-*s %-*s%s\n", |
1388 | ansi_underline(), | |
cbc9fbd1 | 1389 | id_cols, "UNIT FILE", |
6ae3e62a ZJS |
1390 | state_cols, "STATE", |
1391 | ansi_normal()); | |
729e3769 LP |
1392 | |
1393 | for (u = units; u < units + c; u++) { | |
aaa6732d | 1394 | const char *on_underline = NULL, *on_color = NULL, *off = NULL, *id; |
7fd1b19b | 1395 | _cleanup_free_ char *e = NULL; |
aaa6732d LP |
1396 | bool underline; |
1397 | ||
1398 | underline = u + 1 < units + c && | |
1399 | !streq(unit_type_suffix(u->path), unit_type_suffix((u + 1)->path)); | |
6ae3e62a | 1400 | |
aaa6732d | 1401 | if (underline) |
6ae3e62a | 1402 | on_underline = ansi_underline(); |
729e3769 | 1403 | |
4c315c2c IS |
1404 | if (IN_SET(u->state, |
1405 | UNIT_FILE_MASKED, | |
1406 | UNIT_FILE_MASKED_RUNTIME, | |
1407 | UNIT_FILE_DISABLED, | |
6ae3e62a | 1408 | UNIT_FILE_BAD)) |
aaa6732d | 1409 | on_color = underline ? ansi_highlight_red_underline() : ansi_highlight_red(); |
6ae3e62a | 1410 | else if (u->state == UNIT_FILE_ENABLED) |
aaa6732d LP |
1411 | on_color = underline ? ansi_highlight_green_underline() : ansi_highlight_green(); |
1412 | ||
1413 | if (on_underline || on_color) | |
1414 | off = ansi_normal(); | |
729e3769 | 1415 | |
2b6bf07d | 1416 | id = basename(u->path); |
729e3769 | 1417 | |
1c0a113f | 1418 | e = arg_full ? NULL : ellipsize(id, id_cols, 33); |
729e3769 | 1419 | |
aaa6732d LP |
1420 | printf("%s%-*s %s%-*s%s\n", |
1421 | strempty(on_underline), | |
1c0a113f | 1422 | id_cols, e ? e : id, |
aaa6732d | 1423 | strempty(on_color), state_cols, unit_file_state_to_string(u->state), strempty(off)); |
729e3769 LP |
1424 | } |
1425 | ||
1c0a113f | 1426 | if (!arg_no_legend) |
0d292f5e | 1427 | printf("\n%u unit files listed.\n", c); |
729e3769 LP |
1428 | } |
1429 | ||
e449de87 | 1430 | static int list_unit_files(int argc, char *argv[], void *userdata) { |
4afd3348 | 1431 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
f84190d8 | 1432 | _cleanup_free_ UnitFileList *units = NULL; |
8d5ba5a9 ZJS |
1433 | UnitFileList *unit; |
1434 | size_t size = 0; | |
f459b602 MAP |
1435 | unsigned c = 0; |
1436 | const char *state; | |
1437 | char *path; | |
f84190d8 | 1438 | int r; |
313fe66f | 1439 | bool fallback = false; |
729e3769 | 1440 | |
4fbd7192 | 1441 | if (install_client_side()) { |
729e3769 LP |
1442 | Hashmap *h; |
1443 | UnitFileList *u; | |
1444 | Iterator i; | |
f459b602 | 1445 | unsigned n_units; |
729e3769 | 1446 | |
d5099efc | 1447 | h = hashmap_new(&string_hash_ops); |
0d0f0c50 SL |
1448 | if (!h) |
1449 | return log_oom(); | |
729e3769 | 1450 | |
313fe66f | 1451 | r = unit_file_get_list(arg_scope, arg_root, h, arg_states, strv_skip(argv, 1)); |
729e3769 | 1452 | if (r < 0) { |
8ea913b2 | 1453 | unit_file_list_free(h); |
691395d8 | 1454 | return log_error_errno(r, "Failed to get unit file list: %m"); |
729e3769 LP |
1455 | } |
1456 | ||
1457 | n_units = hashmap_size(h); | |
fdbdf6ec | 1458 | |
0da999fa ZJS |
1459 | units = new(UnitFileList, n_units ?: 1); /* avoid malloc(0) */ |
1460 | if (!units) { | |
729e3769 | 1461 | unit_file_list_free(h); |
0d0f0c50 | 1462 | return log_oom(); |
729e3769 LP |
1463 | } |
1464 | ||
1465 | HASHMAP_FOREACH(u, h, i) { | |
313fe66f | 1466 | if (!output_show_unit_file(u, NULL, NULL)) |
8d5ba5a9 ZJS |
1467 | continue; |
1468 | ||
1469 | units[c++] = *u; | |
729e3769 LP |
1470 | free(u); |
1471 | } | |
1472 | ||
8d5ba5a9 | 1473 | assert(c <= n_units); |
729e3769 | 1474 | hashmap_free(h); |
5f056378 CH |
1475 | |
1476 | r = 0; | |
729e3769 | 1477 | } else { |
313fe66f | 1478 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
4afd3348 | 1479 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
4fbd7192 LP |
1480 | sd_bus *bus; |
1481 | ||
1482 | r = acquire_bus(BUS_MANAGER, &bus); | |
1483 | if (r < 0) | |
1484 | return r; | |
6e646d22 | 1485 | |
313fe66f | 1486 | r = sd_bus_message_new_method_call( |
f22f08cd | 1487 | bus, |
313fe66f | 1488 | &m, |
729e3769 LP |
1489 | "org.freedesktop.systemd1", |
1490 | "/org/freedesktop/systemd1", | |
1491 | "org.freedesktop.systemd1.Manager", | |
313fe66f | 1492 | "ListUnitFilesByPatterns"); |
1493 | if (r < 0) | |
1494 | return bus_log_create_error(r); | |
1495 | ||
1496 | r = sd_bus_message_append_strv(m, arg_states); | |
1497 | if (r < 0) | |
1498 | return bus_log_create_error(r); | |
1499 | ||
1500 | r = sd_bus_message_append_strv(m, strv_skip(argv, 1)); | |
1501 | if (r < 0) | |
1502 | return bus_log_create_error(r); | |
1503 | ||
1504 | r = sd_bus_call(bus, m, 0, &error, &reply); | |
1505 | if (r < 0 && sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_METHOD)) { | |
1506 | /* Fallback to legacy ListUnitFiles method */ | |
1507 | fallback = true; | |
1508 | log_debug_errno(r, "Failed to list unit files: %s Falling back to ListUnitsFiles method.", bus_error_message(&error, r)); | |
1509 | m = sd_bus_message_unref(m); | |
1510 | sd_bus_error_free(&error); | |
1511 | ||
1512 | r = sd_bus_message_new_method_call( | |
1513 | bus, | |
1514 | &m, | |
1515 | "org.freedesktop.systemd1", | |
1516 | "/org/freedesktop/systemd1", | |
1517 | "org.freedesktop.systemd1.Manager", | |
1518 | "ListUnitFiles"); | |
1519 | if (r < 0) | |
1520 | return bus_log_create_error(r); | |
1521 | ||
1522 | r = sd_bus_call(bus, m, 0, &error, &reply); | |
1523 | } | |
691395d8 LP |
1524 | if (r < 0) |
1525 | return log_error_errno(r, "Failed to list unit files: %s", bus_error_message(&error, r)); | |
729e3769 | 1526 | |
f459b602 MAP |
1527 | r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ss)"); |
1528 | if (r < 0) | |
1529 | return bus_log_parse_error(r); | |
729e3769 | 1530 | |
f459b602 | 1531 | while ((r = sd_bus_message_read(reply, "(ss)", &path, &state)) > 0) { |
729e3769 | 1532 | |
f459b602 MAP |
1533 | if (!GREEDY_REALLOC(units, size, c + 1)) |
1534 | return log_oom(); | |
729e3769 | 1535 | |
8d5ba5a9 | 1536 | units[c] = (struct UnitFileList) { |
f459b602 MAP |
1537 | path, |
1538 | unit_file_state_from_string(state) | |
1539 | }; | |
8d5ba5a9 | 1540 | |
313fe66f | 1541 | if (output_show_unit_file(&units[c], |
1542 | fallback ? arg_states : NULL, | |
1543 | fallback ? strv_skip(argv, 1) : NULL)) | |
313cefa1 | 1544 | c++; |
8d5ba5a9 | 1545 | |
729e3769 | 1546 | } |
f459b602 MAP |
1547 | if (r < 0) |
1548 | return bus_log_parse_error(r); | |
1549 | ||
1550 | r = sd_bus_message_exit_container(reply); | |
1551 | if (r < 0) | |
1552 | return bus_log_parse_error(r); | |
729e3769 LP |
1553 | } |
1554 | ||
0221d68a | 1555 | (void) pager_open(arg_pager_flags); |
d2ad7e1b | 1556 | |
93bab288 | 1557 | typesafe_qsort(units, c, compare_unit_file_list); |
4fe1be9c | 1558 | output_unit_file_list(units, c); |
729e3769 | 1559 | |
0da999fa | 1560 | if (install_client_side()) |
8d5ba5a9 ZJS |
1561 | for (unit = units; unit < units + c; unit++) |
1562 | free(unit->path); | |
1563 | ||
f84190d8 | 1564 | return 0; |
729e3769 LP |
1565 | } |
1566 | ||
14cb109d | 1567 | static int list_dependencies_print(const char *name, int level, unsigned branches, bool last) { |
55c0b89c | 1568 | _cleanup_free_ char *n = NULL; |
9607d947 | 1569 | size_t max_len = MAX(columns(),20u); |
cbc9fbd1 LP |
1570 | size_t len = 0; |
1571 | int i; | |
55c0b89c | 1572 | |
5d0c05e5 | 1573 | if (!arg_plain) { |
cbc9fbd1 | 1574 | |
5d0c05e5 LN |
1575 | for (i = level - 1; i >= 0; i--) { |
1576 | len += 2; | |
f168c273 | 1577 | if (len > max_len - 3 && !arg_full) { |
5d0c05e5 LN |
1578 | printf("%s...\n",max_len % 2 ? "" : " "); |
1579 | return 0; | |
1580 | } | |
9a6f746f | 1581 | printf("%s", special_glyph(branches & (1 << i) ? SPECIAL_GLYPH_TREE_VERTICAL : SPECIAL_GLYPH_TREE_SPACE)); |
5d0c05e5 | 1582 | } |
55c0b89c | 1583 | len += 2; |
cbc9fbd1 | 1584 | |
f168c273 | 1585 | if (len > max_len - 3 && !arg_full) { |
55c0b89c LN |
1586 | printf("%s...\n",max_len % 2 ? "" : " "); |
1587 | return 0; | |
1588 | } | |
cbc9fbd1 | 1589 | |
9a6f746f | 1590 | printf("%s", special_glyph(last ? SPECIAL_GLYPH_TREE_RIGHT : SPECIAL_GLYPH_TREE_BRANCH)); |
55c0b89c | 1591 | } |
55c0b89c | 1592 | |
9ed794a3 | 1593 | if (arg_full) { |
55c0b89c LN |
1594 | printf("%s\n", name); |
1595 | return 0; | |
1596 | } | |
1597 | ||
1598 | n = ellipsize(name, max_len-len, 100); | |
f168c273 | 1599 | if (!n) |
55c0b89c LN |
1600 | return log_oom(); |
1601 | ||
1602 | printf("%s\n", n); | |
1603 | return 0; | |
1604 | } | |
1605 | ||
f459b602 | 1606 | static int list_dependencies_get_dependencies(sd_bus *bus, const char *name, char ***deps) { |
32b47279 YW |
1607 | struct DependencyStatusInfo { |
1608 | char **dep[5]; | |
1609 | } info = {}; | |
1610 | ||
1611 | static const struct bus_properties_map map[_DEPENDENCY_MAX][6] = { | |
1612 | [DEPENDENCY_FORWARD] = { | |
1613 | { "Requires", "as", NULL, offsetof(struct DependencyStatusInfo, dep[0]) }, | |
1614 | { "Requisite", "as", NULL, offsetof(struct DependencyStatusInfo, dep[1]) }, | |
1615 | { "Wants", "as", NULL, offsetof(struct DependencyStatusInfo, dep[2]) }, | |
1616 | { "ConsistsOf", "as", NULL, offsetof(struct DependencyStatusInfo, dep[3]) }, | |
1617 | { "BindsTo", "as", NULL, offsetof(struct DependencyStatusInfo, dep[4]) }, | |
1618 | {} | |
1619 | }, | |
1620 | [DEPENDENCY_REVERSE] = { | |
1621 | { "RequiredBy", "as", NULL, offsetof(struct DependencyStatusInfo, dep[0]) }, | |
1622 | { "RequisiteOf", "as", NULL, offsetof(struct DependencyStatusInfo, dep[1]) }, | |
1623 | { "WantedBy", "as", NULL, offsetof(struct DependencyStatusInfo, dep[2]) }, | |
1624 | { "PartOf", "as", NULL, offsetof(struct DependencyStatusInfo, dep[3]) }, | |
1625 | { "BoundBy", "as", NULL, offsetof(struct DependencyStatusInfo, dep[4]) }, | |
1626 | {} | |
1627 | }, | |
1628 | [DEPENDENCY_AFTER] = { | |
1629 | { "After", "as", NULL, offsetof(struct DependencyStatusInfo, dep[0]) }, | |
1630 | {} | |
1631 | }, | |
1632 | [DEPENDENCY_BEFORE] = { | |
1633 | { "Before", "as", NULL, offsetof(struct DependencyStatusInfo, dep[0]) }, | |
1634 | {} | |
1635 | }, | |
afba4199 | 1636 | }; |
55c0b89c | 1637 | |
4afd3348 | 1638 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
f459b602 MAP |
1639 | _cleanup_strv_free_ char **ret = NULL; |
1640 | _cleanup_free_ char *path = NULL; | |
32b47279 | 1641 | int i, r; |
55c0b89c LN |
1642 | |
1643 | assert(bus); | |
1644 | assert(name); | |
1645 | assert(deps); | |
1646 | ||
1647 | path = unit_dbus_path_from_name(name); | |
f459b602 MAP |
1648 | if (!path) |
1649 | return log_oom(); | |
55c0b89c | 1650 | |
32b47279 YW |
1651 | r = bus_map_all_properties(bus, |
1652 | "org.freedesktop.systemd1", | |
1653 | path, | |
1654 | map[arg_dependency], | |
f39cbb6e | 1655 | BUS_MAP_STRDUP, |
32b47279 | 1656 | &error, |
f37f8a61 | 1657 | NULL, |
32b47279 | 1658 | &info); |
691395d8 LP |
1659 | if (r < 0) |
1660 | return log_error_errno(r, "Failed to get properties of %s: %s", name, bus_error_message(&error, r)); | |
55c0b89c | 1661 | |
32b47279 YW |
1662 | if (IN_SET(arg_dependency, DEPENDENCY_AFTER, DEPENDENCY_BEFORE)) { |
1663 | *deps = info.dep[0]; | |
1664 | return 0; | |
1665 | } | |
55c0b89c | 1666 | |
32b47279 YW |
1667 | for (i = 0; i < 5; i++) { |
1668 | r = strv_extend_strv(&ret, info.dep[i], true); | |
f459b602 | 1669 | if (r < 0) |
32b47279 YW |
1670 | return log_oom(); |
1671 | info.dep[i] = strv_free(info.dep[i]); | |
f459b602 | 1672 | } |
540e7dbe | 1673 | |
1cc6c93a | 1674 | *deps = TAKE_PTR(ret); |
540e7dbe | 1675 | |
f459b602 | 1676 | return 0; |
55c0b89c LN |
1677 | } |
1678 | ||
93bab288 | 1679 | static int list_dependencies_compare(char * const *a, char * const *b) { |
55c0b89c LN |
1680 | if (unit_name_to_type(*a) == UNIT_TARGET && unit_name_to_type(*b) != UNIT_TARGET) |
1681 | return 1; | |
1682 | if (unit_name_to_type(*a) != UNIT_TARGET && unit_name_to_type(*b) == UNIT_TARGET) | |
1683 | return -1; | |
cbc9fbd1 | 1684 | |
55c0b89c LN |
1685 | return strcasecmp(*a, *b); |
1686 | } | |
1687 | ||
f459b602 MAP |
1688 | static int list_dependencies_one( |
1689 | sd_bus *bus, | |
1690 | const char *name, | |
1691 | int level, | |
1692 | char ***units, | |
14cb109d | 1693 | unsigned branches) { |
f459b602 | 1694 | |
e3e45d4f | 1695 | _cleanup_strv_free_ char **deps = NULL; |
55c0b89c | 1696 | char **c; |
55c0b89c LN |
1697 | int r = 0; |
1698 | ||
cbc9fbd1 LP |
1699 | assert(bus); |
1700 | assert(name); | |
1701 | assert(units); | |
1702 | ||
e3e45d4f SP |
1703 | r = strv_extend(units, name); |
1704 | if (r < 0) | |
55c0b89c LN |
1705 | return log_oom(); |
1706 | ||
1707 | r = list_dependencies_get_dependencies(bus, name, &deps); | |
1708 | if (r < 0) | |
cec7eda5 | 1709 | return r; |
55c0b89c | 1710 | |
93bab288 | 1711 | typesafe_qsort(deps, strv_length(deps), list_dependencies_compare); |
55c0b89c LN |
1712 | |
1713 | STRV_FOREACH(c, deps) { | |
e3e45d4f | 1714 | if (strv_contains(*units, *c)) { |
5d0c05e5 | 1715 | if (!arg_plain) { |
60705040 | 1716 | printf(" "); |
5d0c05e5 LN |
1717 | r = list_dependencies_print("...", level + 1, (branches << 1) | (c[1] == NULL ? 0 : 1), 1); |
1718 | if (r < 0) | |
1719 | return r; | |
1720 | } | |
55c0b89c LN |
1721 | continue; |
1722 | } | |
1723 | ||
250ba664 ZJS |
1724 | if (arg_plain) |
1725 | printf(" "); | |
1726 | else { | |
fa0d5878 | 1727 | UnitActiveState active_state = _UNIT_ACTIVE_STATE_INVALID; |
250ba664 ZJS |
1728 | const char *on; |
1729 | ||
fa0d5878 | 1730 | (void) get_state_one_unit(bus, *c, &active_state); |
baa9ecc1 | 1731 | |
fa0d5878 | 1732 | switch (active_state) { |
baa9ecc1 LP |
1733 | case UNIT_ACTIVE: |
1734 | case UNIT_RELOADING: | |
1735 | case UNIT_ACTIVATING: | |
1736 | on = ansi_highlight_green(); | |
1737 | break; | |
1738 | ||
1739 | case UNIT_INACTIVE: | |
1740 | case UNIT_DEACTIVATING: | |
1741 | on = ansi_normal(); | |
1742 | break; | |
1743 | ||
1744 | default: | |
1745 | on = ansi_highlight_red(); | |
1746 | break; | |
fa0d5878 BR |
1747 | } |
1748 | ||
9a6f746f | 1749 | printf("%s%s%s ", on, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE), ansi_normal()); |
250ba664 | 1750 | } |
dbc2c080 | 1751 | |
55c0b89c | 1752 | r = list_dependencies_print(*c, level, branches, c[1] == NULL); |
cec7eda5 ZJS |
1753 | if (r < 0) |
1754 | return r; | |
55c0b89c LN |
1755 | |
1756 | if (arg_all || unit_name_to_type(*c) == UNIT_TARGET) { | |
e3e45d4f | 1757 | r = list_dependencies_one(bus, *c, level + 1, units, (branches << 1) | (c[1] == NULL ? 0 : 1)); |
f168c273 | 1758 | if (r < 0) |
cec7eda5 | 1759 | return r; |
55c0b89c LN |
1760 | } |
1761 | } | |
f459b602 | 1762 | |
e3e45d4f SP |
1763 | if (!arg_plain) |
1764 | strv_remove(*units, name); | |
f459b602 | 1765 | |
cec7eda5 | 1766 | return 0; |
55c0b89c LN |
1767 | } |
1768 | ||
e449de87 | 1769 | static int list_dependencies(int argc, char *argv[], void *userdata) { |
5d0c05e5 | 1770 | _cleanup_strv_free_ char **units = NULL; |
f459b602 | 1771 | _cleanup_free_ char *unit = NULL; |
e31165b2 | 1772 | const char *u; |
4fbd7192 | 1773 | sd_bus *bus; |
7410616c | 1774 | int r; |
55c0b89c | 1775 | |
e449de87 | 1776 | if (argv[1]) { |
37cbc1d5 | 1777 | r = unit_name_mangle(argv[1], arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN, &unit); |
7410616c LP |
1778 | if (r < 0) |
1779 | return log_error_errno(r, "Failed to mangle unit name: %m"); | |
1780 | ||
e31165b2 LP |
1781 | u = unit; |
1782 | } else | |
1783 | u = SPECIAL_DEFAULT_TARGET; | |
55c0b89c | 1784 | |
4fbd7192 LP |
1785 | r = acquire_bus(BUS_MANAGER, &bus); |
1786 | if (r < 0) | |
1787 | return r; | |
1788 | ||
0221d68a | 1789 | (void) pager_open(arg_pager_flags); |
d2ad7e1b | 1790 | |
e31165b2 LP |
1791 | puts(u); |
1792 | ||
5d0c05e5 | 1793 | return list_dependencies_one(bus, u, 0, &units, 0); |
55c0b89c LN |
1794 | } |
1795 | ||
0d292f5e LP |
1796 | struct machine_info { |
1797 | bool is_host; | |
1798 | char *name; | |
0d292f5e | 1799 | char *state; |
8fcf784d LP |
1800 | char *control_group; |
1801 | uint32_t n_failed_units; | |
1802 | uint32_t n_jobs; | |
1803 | usec_t timestamp; | |
1804 | }; | |
1805 | ||
1806 | static const struct bus_properties_map machine_info_property_map[] = { | |
1807 | { "SystemState", "s", NULL, offsetof(struct machine_info, state) }, | |
1808 | { "NJobs", "u", NULL, offsetof(struct machine_info, n_jobs) }, | |
1809 | { "NFailedUnits", "u", NULL, offsetof(struct machine_info, n_failed_units) }, | |
1810 | { "ControlGroup", "s", NULL, offsetof(struct machine_info, control_group) }, | |
1811 | { "UserspaceTimestamp", "t", NULL, offsetof(struct machine_info, timestamp) }, | |
1812 | {} | |
0d292f5e LP |
1813 | }; |
1814 | ||
e7e55dbd | 1815 | static void machine_info_clear(struct machine_info *info) { |
f8654baa ZJS |
1816 | assert(info); |
1817 | ||
1818 | free(info->name); | |
1819 | free(info->state); | |
1820 | free(info->control_group); | |
1821 | zero(*info); | |
e7e55dbd DH |
1822 | } |
1823 | ||
0d292f5e LP |
1824 | static void free_machines_list(struct machine_info *machine_infos, int n) { |
1825 | int i; | |
1826 | ||
1827 | if (!machine_infos) | |
1828 | return; | |
1829 | ||
e7e55dbd DH |
1830 | for (i = 0; i < n; i++) |
1831 | machine_info_clear(&machine_infos[i]); | |
0d292f5e LP |
1832 | |
1833 | free(machine_infos); | |
1834 | } | |
1835 | ||
93bab288 YW |
1836 | static int compare_machine_info(const struct machine_info *a, const struct machine_info *b) { |
1837 | int r; | |
0d292f5e | 1838 | |
93bab288 YW |
1839 | r = CMP(b->is_host, a->is_host); |
1840 | if (r != 0) | |
1841 | return r; | |
0d292f5e | 1842 | |
93bab288 | 1843 | return strcasecmp(a->name, b->name); |
0d292f5e LP |
1844 | } |
1845 | ||
1846 | static int get_machine_properties(sd_bus *bus, struct machine_info *mi) { | |
4afd3348 | 1847 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *container = NULL; |
0d292f5e LP |
1848 | int r; |
1849 | ||
1850 | assert(mi); | |
1851 | ||
1852 | if (!bus) { | |
de33fc62 | 1853 | r = sd_bus_open_system_machine(&container, mi->name); |
0d292f5e LP |
1854 | if (r < 0) |
1855 | return r; | |
1856 | ||
1857 | bus = container; | |
1858 | } | |
1859 | ||
a7e4861c YW |
1860 | r = bus_map_all_properties( |
1861 | bus, | |
1862 | "org.freedesktop.systemd1", | |
1863 | "/org/freedesktop/systemd1", | |
1864 | machine_info_property_map, | |
1865 | BUS_MAP_STRDUP, | |
1866 | NULL, | |
1867 | NULL, | |
1868 | mi); | |
0d292f5e LP |
1869 | if (r < 0) |
1870 | return r; | |
1871 | ||
1872 | return 0; | |
1873 | } | |
1874 | ||
1875 | static bool output_show_machine(const char *name, char **patterns) { | |
2404701e | 1876 | return strv_fnmatch_or_empty(patterns, name, FNM_NOESCAPE); |
0d292f5e LP |
1877 | } |
1878 | ||
1879 | static int get_machine_list( | |
1880 | sd_bus *bus, | |
1881 | struct machine_info **_machine_infos, | |
1882 | char **patterns) { | |
1883 | ||
1884 | struct machine_info *machine_infos = NULL; | |
1885 | _cleanup_strv_free_ char **m = NULL; | |
1886 | _cleanup_free_ char *hn = NULL; | |
1887 | size_t sz = 0; | |
1888 | char **i; | |
4c3e8e39 | 1889 | int c = 0, r; |
0d292f5e LP |
1890 | |
1891 | hn = gethostname_malloc(); | |
1892 | if (!hn) | |
1893 | return log_oom(); | |
1894 | ||
1895 | if (output_show_machine(hn, patterns)) { | |
1896 | if (!GREEDY_REALLOC0(machine_infos, sz, c+1)) | |
1897 | return log_oom(); | |
1898 | ||
1899 | machine_infos[c].is_host = true; | |
ae2a15bc | 1900 | machine_infos[c].name = TAKE_PTR(hn); |
0d292f5e | 1901 | |
f9e0eefc | 1902 | (void) get_machine_properties(bus, &machine_infos[c]); |
0d292f5e LP |
1903 | c++; |
1904 | } | |
1905 | ||
4c3e8e39 LP |
1906 | r = sd_get_machine_names(&m); |
1907 | if (r < 0) | |
1908 | return log_error_errno(r, "Failed to get machine list: %m"); | |
1909 | ||
0d292f5e LP |
1910 | STRV_FOREACH(i, m) { |
1911 | _cleanup_free_ char *class = NULL; | |
1912 | ||
1913 | if (!output_show_machine(*i, patterns)) | |
1914 | continue; | |
1915 | ||
1916 | sd_machine_get_class(*i, &class); | |
1917 | if (!streq_ptr(class, "container")) | |
1918 | continue; | |
1919 | ||
1920 | if (!GREEDY_REALLOC0(machine_infos, sz, c+1)) { | |
1921 | free_machines_list(machine_infos, c); | |
1922 | return log_oom(); | |
1923 | } | |
1924 | ||
1925 | machine_infos[c].is_host = false; | |
1926 | machine_infos[c].name = strdup(*i); | |
1927 | if (!machine_infos[c].name) { | |
1928 | free_machines_list(machine_infos, c); | |
1929 | return log_oom(); | |
1930 | } | |
1931 | ||
f9e0eefc | 1932 | (void) get_machine_properties(NULL, &machine_infos[c]); |
0d292f5e LP |
1933 | c++; |
1934 | } | |
1935 | ||
1936 | *_machine_infos = machine_infos; | |
1937 | return c; | |
1938 | } | |
1939 | ||
1940 | static void output_machines_list(struct machine_info *machine_infos, unsigned n) { | |
1941 | struct machine_info *m; | |
1942 | unsigned | |
90c3f79d | 1943 | circle_len = 0, |
fbd0b64f LP |
1944 | namelen = STRLEN("NAME"), |
1945 | statelen = STRLEN("STATE"), | |
1946 | failedlen = STRLEN("FAILED"), | |
1947 | jobslen = STRLEN("JOBS"); | |
aa91d592 | 1948 | bool state_missing = false; |
0d292f5e LP |
1949 | |
1950 | assert(machine_infos || n == 0); | |
1951 | ||
1952 | for (m = machine_infos; m < machine_infos + n; m++) { | |
fbd0b64f LP |
1953 | namelen = MAX(namelen, |
1954 | strlen(m->name) + (m->is_host ? STRLEN(" (host)") : 0)); | |
7bf7ce28 | 1955 | statelen = MAX(statelen, strlen_ptr(m->state)); |
0d292f5e LP |
1956 | failedlen = MAX(failedlen, DECIMAL_STR_WIDTH(m->n_failed_units)); |
1957 | jobslen = MAX(jobslen, DECIMAL_STR_WIDTH(m->n_jobs)); | |
90c3f79d | 1958 | |
a004cb75 | 1959 | if (!arg_plain && m->state && !streq(m->state, "running")) |
90c3f79d | 1960 | circle_len = 2; |
0d292f5e LP |
1961 | } |
1962 | ||
90c3f79d LP |
1963 | if (!arg_no_legend) { |
1964 | if (circle_len > 0) | |
1965 | fputs(" ", stdout); | |
1966 | ||
0d292f5e LP |
1967 | printf("%-*s %-*s %-*s %-*s\n", |
1968 | namelen, "NAME", | |
1969 | statelen, "STATE", | |
1970 | failedlen, "FAILED", | |
1971 | jobslen, "JOBS"); | |
90c3f79d | 1972 | } |
0d292f5e LP |
1973 | |
1974 | for (m = machine_infos; m < machine_infos + n; m++) { | |
90c3f79d LP |
1975 | const char *on_state = "", *off_state = ""; |
1976 | const char *on_failed = "", *off_failed = ""; | |
1977 | bool circle = false; | |
0d292f5e LP |
1978 | |
1979 | if (streq_ptr(m->state, "degraded")) { | |
1980 | on_state = ansi_highlight_red(); | |
1fc464f6 | 1981 | off_state = ansi_normal(); |
90c3f79d | 1982 | circle = true; |
0d292f5e LP |
1983 | } else if (!streq_ptr(m->state, "running")) { |
1984 | on_state = ansi_highlight_yellow(); | |
1fc464f6 | 1985 | off_state = ansi_normal(); |
90c3f79d LP |
1986 | circle = true; |
1987 | } | |
0d292f5e LP |
1988 | |
1989 | if (m->n_failed_units > 0) { | |
1990 | on_failed = ansi_highlight_red(); | |
1fc464f6 | 1991 | off_failed = ansi_normal(); |
0d292f5e LP |
1992 | } else |
1993 | on_failed = off_failed = ""; | |
1994 | ||
90c3f79d | 1995 | if (circle_len > 0) |
9a6f746f | 1996 | printf("%s%s%s ", on_state, circle ? special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE) : " ", off_state); |
90c3f79d | 1997 | |
4bea2469 ZJS |
1998 | if (!m->state) |
1999 | state_missing = true; | |
2000 | ||
0d292f5e | 2001 | if (m->is_host) |
c6ba5b80 | 2002 | printf("%-*s (host) %s%-*s%s %s%*" PRIu32 "%s %*" PRIu32 "\n", |
4bea2469 | 2003 | (int) (namelen - strlen(" (host)")), |
fbd0b64f | 2004 | strna(m->name), |
0d292f5e LP |
2005 | on_state, statelen, strna(m->state), off_state, |
2006 | on_failed, failedlen, m->n_failed_units, off_failed, | |
2007 | jobslen, m->n_jobs); | |
2008 | else | |
c6ba5b80 | 2009 | printf("%-*s %s%-*s%s %s%*" PRIu32 "%s %*" PRIu32 "\n", |
0d292f5e LP |
2010 | namelen, strna(m->name), |
2011 | on_state, statelen, strna(m->state), off_state, | |
2012 | on_failed, failedlen, m->n_failed_units, off_failed, | |
2013 | jobslen, m->n_jobs); | |
2014 | } | |
2015 | ||
4bea2469 ZJS |
2016 | if (!arg_no_legend) { |
2017 | printf("\n"); | |
2018 | if (state_missing && geteuid() != 0) | |
2019 | printf("Notice: some information only available to privileged users was not shown.\n"); | |
2020 | printf("%u machines listed.\n", n); | |
2021 | } | |
0d292f5e LP |
2022 | } |
2023 | ||
e449de87 | 2024 | static int list_machines(int argc, char *argv[], void *userdata) { |
0d292f5e | 2025 | struct machine_info *machine_infos = NULL; |
4fbd7192 | 2026 | sd_bus *bus; |
0d292f5e LP |
2027 | int r; |
2028 | ||
4fbd7192 LP |
2029 | r = acquire_bus(BUS_MANAGER, &bus); |
2030 | if (r < 0) | |
2031 | return r; | |
2032 | ||
e449de87 | 2033 | r = get_machine_list(bus, &machine_infos, strv_skip(argv, 1)); |
0d292f5e LP |
2034 | if (r < 0) |
2035 | return r; | |
2036 | ||
0221d68a | 2037 | (void) pager_open(arg_pager_flags); |
d2ad7e1b | 2038 | |
93bab288 | 2039 | typesafe_qsort(machine_infos, r, compare_machine_info); |
0d292f5e LP |
2040 | output_machines_list(machine_infos, r); |
2041 | free_machines_list(machine_infos, r); | |
2042 | ||
2043 | return 0; | |
2044 | } | |
2045 | ||
e449de87 | 2046 | static int get_default(int argc, char *argv[], void *userdata) { |
4afd3348 | 2047 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
f459b602 MAP |
2048 | _cleanup_free_ char *_path = NULL; |
2049 | const char *path; | |
99504dd4 | 2050 | int r; |
99504dd4 | 2051 | |
4fbd7192 | 2052 | if (install_client_side()) { |
f459b602 | 2053 | r = unit_file_get_default(arg_scope, arg_root, &_path); |
f647962d MS |
2054 | if (r < 0) |
2055 | return log_error_errno(r, "Failed to get default target: %m"); | |
f459b602 | 2056 | path = _path; |
99504dd4 | 2057 | |
5f056378 | 2058 | r = 0; |
99504dd4 | 2059 | } else { |
4afd3348 | 2060 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
4fbd7192 LP |
2061 | sd_bus *bus; |
2062 | ||
2063 | r = acquire_bus(BUS_MANAGER, &bus); | |
2064 | if (r < 0) | |
2065 | return r; | |
6e646d22 | 2066 | |
f459b602 MAP |
2067 | r = sd_bus_call_method( |
2068 | bus, | |
2069 | "org.freedesktop.systemd1", | |
2070 | "/org/freedesktop/systemd1", | |
2071 | "org.freedesktop.systemd1.Manager", | |
2072 | "GetDefaultTarget", | |
2073 | &error, | |
2074 | &reply, | |
2075 | NULL); | |
691395d8 LP |
2076 | if (r < 0) |
2077 | return log_error_errno(r, "Failed to get default target: %s", bus_error_message(&error, r)); | |
99504dd4 | 2078 | |
f459b602 MAP |
2079 | r = sd_bus_message_read(reply, "s", &path); |
2080 | if (r < 0) | |
2081 | return bus_log_parse_error(r); | |
99504dd4 VP |
2082 | } |
2083 | ||
2084 | if (path) | |
2085 | printf("%s\n", path); | |
2086 | ||
f459b602 | 2087 | return 0; |
99504dd4 VP |
2088 | } |
2089 | ||
e449de87 | 2090 | static int set_default(int argc, char *argv[], void *userdata) { |
718db961 | 2091 | _cleanup_free_ char *unit = NULL; |
acc0269c | 2092 | UnitFileChange *changes = NULL; |
da6053d0 | 2093 | size_t n_changes = 0; |
718db961 LP |
2094 | int r; |
2095 | ||
e449de87 LP |
2096 | assert(argc >= 2); |
2097 | assert(argv); | |
2098 | ||
37cbc1d5 | 2099 | r = unit_name_mangle_with_suffix(argv[1], arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN, ".target", &unit); |
7410616c LP |
2100 | if (r < 0) |
2101 | return log_error_errno(r, "Failed to mangle unit name: %m"); | |
718db961 | 2102 | |
4fbd7192 | 2103 | if (install_client_side()) { |
b3796dd8 | 2104 | r = unit_file_set_default(arg_scope, UNIT_FILE_FORCE, arg_root, unit, &changes, &n_changes); |
af3d8113 | 2105 | unit_file_dump_changes(r, "set default", changes, n_changes, arg_quiet); |
5f056378 CH |
2106 | |
2107 | if (r > 0) | |
2108 | r = 0; | |
718db961 | 2109 | } else { |
4afd3348 LP |
2110 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
2111 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
4fbd7192 | 2112 | sd_bus *bus; |
718db961 | 2113 | |
8a4b13c5 | 2114 | polkit_agent_open_maybe(); |
6e646d22 | 2115 | |
4fbd7192 LP |
2116 | r = acquire_bus(BUS_MANAGER, &bus); |
2117 | if (r < 0) | |
2118 | return r; | |
2119 | ||
6e646d22 | 2120 | r = sd_bus_call_method( |
718db961 LP |
2121 | bus, |
2122 | "org.freedesktop.systemd1", | |
2123 | "/org/freedesktop/systemd1", | |
2124 | "org.freedesktop.systemd1.Manager", | |
6e646d22 LP |
2125 | "SetDefaultTarget", |
2126 | &error, | |
2127 | &reply, | |
2128 | "sb", unit, 1); | |
691395d8 LP |
2129 | if (r < 0) |
2130 | return log_error_errno(r, "Failed to set default target: %s", bus_error_message(&error, r)); | |
718db961 | 2131 | |
acc0269c | 2132 | r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet, &changes, &n_changes); |
718db961 | 2133 | if (r < 0) |
acc0269c | 2134 | goto finish; |
718db961 | 2135 | |
93c941e3 | 2136 | /* Try to reload if enabled */ |
718db961 | 2137 | if (!arg_no_reload) |
e449de87 | 2138 | r = daemon_reload(argc, argv, userdata); |
718db961 LP |
2139 | else |
2140 | r = 0; | |
2141 | } | |
2142 | ||
acc0269c CH |
2143 | finish: |
2144 | unit_file_changes_free(changes, n_changes); | |
2145 | ||
718db961 LP |
2146 | return r; |
2147 | } | |
2148 | ||
76d8ca22 | 2149 | static int output_waiting_jobs(sd_bus *bus, uint32_t id, const char *method, const char *prefix) { |
82948f6c LP |
2150 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
2151 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
31d99bd1 | 2152 | const char *name, *type; |
82948f6c LP |
2153 | uint32_t other_id; |
2154 | int r; | |
2155 | ||
2156 | assert(bus); | |
2157 | ||
2158 | r = sd_bus_call_method( | |
2159 | bus, | |
2160 | "org.freedesktop.systemd1", | |
2161 | "/org/freedesktop/systemd1", | |
2162 | "org.freedesktop.systemd1.Manager", | |
2163 | method, | |
2164 | &error, | |
2165 | &reply, | |
2166 | "u", id); | |
76d8ca22 ZJS |
2167 | if (r < 0) |
2168 | return log_debug_errno(r, "Failed to get waiting jobs for job %" PRIu32, id); | |
82948f6c LP |
2169 | |
2170 | r = sd_bus_message_enter_container(reply, 'a', "(usssoo)"); | |
76d8ca22 ZJS |
2171 | if (r < 0) |
2172 | return bus_log_parse_error(r); | |
82948f6c | 2173 | |
31d99bd1 | 2174 | while ((r = sd_bus_message_read(reply, "(usssoo)", &other_id, &name, &type, NULL, NULL, NULL)) > 0) |
bc357ce5 | 2175 | printf("%s %u (%s/%s)\n", prefix, other_id, name, type); |
76d8ca22 ZJS |
2176 | if (r < 0) |
2177 | return bus_log_parse_error(r); | |
82948f6c LP |
2178 | |
2179 | r = sd_bus_message_exit_container(reply); | |
76d8ca22 ZJS |
2180 | if (r < 0) |
2181 | return bus_log_parse_error(r); | |
2182 | ||
2183 | return 0; | |
82948f6c LP |
2184 | } |
2185 | ||
75add28a ZJS |
2186 | struct job_info { |
2187 | uint32_t id; | |
f459b602 | 2188 | const char *name, *type, *state; |
75add28a ZJS |
2189 | }; |
2190 | ||
82948f6c | 2191 | static void output_jobs_list(sd_bus *bus, const struct job_info* jobs, unsigned n, bool skipped) { |
f459b602 MAP |
2192 | unsigned id_len, unit_len, type_len, state_len; |
2193 | const struct job_info *j; | |
75add28a ZJS |
2194 | const char *on, *off; |
2195 | bool shorten = false; | |
2196 | ||
2197 | assert(n == 0 || jobs); | |
2198 | ||
2199 | if (n == 0) { | |
250ba664 ZJS |
2200 | if (!arg_no_legend) { |
2201 | on = ansi_highlight_green(); | |
1fc464f6 | 2202 | off = ansi_normal(); |
75add28a | 2203 | |
250ba664 ZJS |
2204 | printf("%sNo jobs %s.%s\n", on, skipped ? "listed" : "running", off); |
2205 | } | |
75add28a ZJS |
2206 | return; |
2207 | } | |
2208 | ||
0221d68a | 2209 | (void) pager_open(arg_pager_flags); |
75add28a | 2210 | |
fbd0b64f LP |
2211 | id_len = STRLEN("JOB"); |
2212 | unit_len = STRLEN("UNIT"); | |
2213 | type_len = STRLEN("TYPE"); | |
2214 | state_len = STRLEN("STATE"); | |
6d6d40c9 | 2215 | |
f459b602 MAP |
2216 | for (j = jobs; j < jobs + n; j++) { |
2217 | uint32_t id = j->id; | |
2218 | assert(j->name && j->type && j->state); | |
75add28a | 2219 | |
f459b602 MAP |
2220 | id_len = MAX(id_len, DECIMAL_STR_WIDTH(id)); |
2221 | unit_len = MAX(unit_len, strlen(j->name)); | |
2222 | type_len = MAX(type_len, strlen(j->type)); | |
2223 | state_len = MAX(state_len, strlen(j->state)); | |
2224 | } | |
75add28a | 2225 | |
f459b602 MAP |
2226 | if (!arg_full && id_len + 1 + unit_len + type_len + 1 + state_len > columns()) { |
2227 | unit_len = MAX(33u, columns() - id_len - type_len - state_len - 3); | |
2228 | shorten = true; | |
2229 | } | |
75add28a | 2230 | |
6ce774fd MM |
2231 | if (!arg_no_legend) |
2232 | printf("%*s %-*s %-*s %-*s\n", | |
2233 | id_len, "JOB", | |
2234 | unit_len, "UNIT", | |
2235 | type_len, "TYPE", | |
2236 | state_len, "STATE"); | |
75add28a | 2237 | |
f459b602 MAP |
2238 | for (j = jobs; j < jobs + n; j++) { |
2239 | _cleanup_free_ char *e = NULL; | |
75add28a | 2240 | |
f459b602 MAP |
2241 | if (streq(j->state, "running")) { |
2242 | on = ansi_highlight(); | |
1fc464f6 | 2243 | off = ansi_normal(); |
f459b602 MAP |
2244 | } else |
2245 | on = off = ""; | |
2246 | ||
2247 | e = shorten ? ellipsize(j->name, unit_len, 33) : NULL; | |
2248 | printf("%*u %s%-*s%s %-*s %s%-*s%s\n", | |
2249 | id_len, j->id, | |
2250 | on, unit_len, e ? e : j->name, off, | |
2251 | type_len, j->type, | |
2252 | on, state_len, j->state, off); | |
82948f6c LP |
2253 | |
2254 | if (arg_jobs_after) | |
bc357ce5 | 2255 | output_waiting_jobs(bus, j->id, "GetJobAfter", "\twaiting for job"); |
82948f6c | 2256 | if (arg_jobs_before) |
bc357ce5 | 2257 | output_waiting_jobs(bus, j->id, "GetJobBefore", "\tblocking job"); |
75add28a ZJS |
2258 | } |
2259 | ||
6ce774fd MM |
2260 | if (!arg_no_legend) { |
2261 | on = ansi_highlight(); | |
1fc464f6 | 2262 | off = ansi_normal(); |
75add28a | 2263 | |
6ce774fd MM |
2264 | printf("\n%s%u jobs listed%s.\n", on, n, off); |
2265 | } | |
75add28a ZJS |
2266 | } |
2267 | ||
d8fba7c6 | 2268 | static bool output_show_job(struct job_info *job, char **patterns) { |
2404701e | 2269 | return strv_fnmatch_or_empty(patterns, job->name, FNM_NOESCAPE); |
d8fba7c6 ZJS |
2270 | } |
2271 | ||
e449de87 | 2272 | static int list_jobs(int argc, char *argv[], void *userdata) { |
4afd3348 LP |
2273 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
2274 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
f459b602 | 2275 | _cleanup_free_ struct job_info *jobs = NULL; |
31d99bd1 YW |
2276 | const char *name, *type, *state; |
2277 | bool skipped = false; | |
f459b602 MAP |
2278 | size_t size = 0; |
2279 | unsigned c = 0; | |
4fbd7192 | 2280 | sd_bus *bus; |
f459b602 | 2281 | uint32_t id; |
f84190d8 | 2282 | int r; |
ec14911e | 2283 | |
4fbd7192 LP |
2284 | r = acquire_bus(BUS_MANAGER, &bus); |
2285 | if (r < 0) | |
2286 | return r; | |
2287 | ||
f459b602 | 2288 | r = sd_bus_call_method( |
f22f08cd SP |
2289 | bus, |
2290 | "org.freedesktop.systemd1", | |
2291 | "/org/freedesktop/systemd1", | |
2292 | "org.freedesktop.systemd1.Manager", | |
2293 | "ListJobs", | |
f459b602 | 2294 | &error, |
f22f08cd | 2295 | &reply, |
f459b602 | 2296 | NULL); |
691395d8 LP |
2297 | if (r < 0) |
2298 | return log_error_errno(r, "Failed to list jobs: %s", bus_error_message(&error, r)); | |
7e4249b9 | 2299 | |
f459b602 MAP |
2300 | r = sd_bus_message_enter_container(reply, 'a', "(usssoo)"); |
2301 | if (r < 0) | |
2302 | return bus_log_parse_error(r); | |
7e4249b9 | 2303 | |
31d99bd1 | 2304 | while ((r = sd_bus_message_read(reply, "(usssoo)", &id, &name, &type, &state, NULL, NULL)) > 0) { |
d8fba7c6 ZJS |
2305 | struct job_info job = { id, name, type, state }; |
2306 | ||
e449de87 | 2307 | if (!output_show_job(&job, strv_skip(argv, 1))) { |
d8fba7c6 ZJS |
2308 | skipped = true; |
2309 | continue; | |
2310 | } | |
8fe914ec | 2311 | |
f459b602 MAP |
2312 | if (!GREEDY_REALLOC(jobs, size, c + 1)) |
2313 | return log_oom(); | |
7e4249b9 | 2314 | |
d8fba7c6 | 2315 | jobs[c++] = job; |
7e4249b9 | 2316 | } |
f459b602 MAP |
2317 | if (r < 0) |
2318 | return bus_log_parse_error(r); | |
7e4249b9 | 2319 | |
f459b602 MAP |
2320 | r = sd_bus_message_exit_container(reply); |
2321 | if (r < 0) | |
2322 | return bus_log_parse_error(r); | |
f73e33d9 | 2323 | |
0221d68a | 2324 | (void) pager_open(arg_pager_flags); |
d2ad7e1b | 2325 | |
82948f6c | 2326 | output_jobs_list(bus, jobs, c, skipped); |
91d0f17e | 2327 | return 0; |
7e4249b9 LP |
2328 | } |
2329 | ||
e449de87 | 2330 | static int cancel_job(int argc, char *argv[], void *userdata) { |
4fbd7192 | 2331 | sd_bus *bus; |
729e3769 | 2332 | char **name; |
342641fb | 2333 | int r = 0; |
7e4249b9 | 2334 | |
e449de87 | 2335 | if (argc <= 1) |
2853b60a | 2336 | return trivial_method(argc, argv, userdata); |
ee5762e3 | 2337 | |
4fbd7192 LP |
2338 | r = acquire_bus(BUS_MANAGER, &bus); |
2339 | if (r < 0) | |
2340 | return r; | |
2341 | ||
8a4b13c5 | 2342 | polkit_agent_open_maybe(); |
d2ad7e1b | 2343 | |
e449de87 | 2344 | STRV_FOREACH(name, strv_skip(argv, 1)) { |
4afd3348 | 2345 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
5dd9014f | 2346 | uint32_t id; |
342641fb | 2347 | int q; |
7e4249b9 | 2348 | |
342641fb | 2349 | q = safe_atou32(*name, &id); |
f647962d MS |
2350 | if (q < 0) |
2351 | return log_error_errno(q, "Failed to parse job id \"%s\": %m", *name); | |
7e4249b9 | 2352 | |
6e646d22 | 2353 | q = sd_bus_call_method( |
f22f08cd SP |
2354 | bus, |
2355 | "org.freedesktop.systemd1", | |
2356 | "/org/freedesktop/systemd1", | |
2357 | "org.freedesktop.systemd1.Manager", | |
6e646d22 LP |
2358 | "CancelJob", |
2359 | &error, | |
2360 | NULL, | |
2361 | "u", id); | |
342641fb | 2362 | if (q < 0) { |
691395d8 | 2363 | log_error_errno(q, "Failed to cancel job %"PRIu32": %s", id, bus_error_message(&error, q)); |
342641fb LP |
2364 | if (r == 0) |
2365 | r = q; | |
f459b602 | 2366 | } |
7e4249b9 LP |
2367 | } |
2368 | ||
342641fb | 2369 | return r; |
7e4249b9 LP |
2370 | } |
2371 | ||
f459b602 | 2372 | static int need_daemon_reload(sd_bus *bus, const char *unit) { |
4afd3348 | 2373 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
f459b602 MAP |
2374 | const char *path; |
2375 | int b, r; | |
94c01aeb | 2376 | |
f459b602 MAP |
2377 | /* We ignore all errors here, since this is used to show a |
2378 | * warning only */ | |
45fb0699 | 2379 | |
f459b602 MAP |
2380 | /* We don't use unit_dbus_path_from_name() directly since we |
2381 | * don't want to load the unit if it isn't loaded. */ | |
f84190d8 | 2382 | |
f459b602 | 2383 | r = sd_bus_call_method( |
f22f08cd SP |
2384 | bus, |
2385 | "org.freedesktop.systemd1", | |
2386 | "/org/freedesktop/systemd1", | |
2387 | "org.freedesktop.systemd1.Manager", | |
2388 | "GetUnit", | |
f459b602 | 2389 | NULL, |
f22f08cd | 2390 | &reply, |
e3e0314b | 2391 | "s", unit); |
f84190d8 LP |
2392 | if (r < 0) |
2393 | return r; | |
45fb0699 | 2394 | |
f459b602 MAP |
2395 | r = sd_bus_message_read(reply, "o", &path); |
2396 | if (r < 0) | |
2397 | return r; | |
f84190d8 | 2398 | |
f459b602 | 2399 | r = sd_bus_get_property_trivial( |
f22f08cd | 2400 | bus, |
b0193f1c LP |
2401 | "org.freedesktop.systemd1", |
2402 | path, | |
f459b602 MAP |
2403 | "org.freedesktop.systemd1.Unit", |
2404 | "NeedDaemonReload", | |
2405 | NULL, | |
2406 | 'b', &b); | |
f84190d8 LP |
2407 | if (r < 0) |
2408 | return r; | |
45fb0699 | 2409 | |
45fb0699 LP |
2410 | return b; |
2411 | } | |
2412 | ||
3f36991e | 2413 | static void warn_unit_file_changed(const char *name) { |
87ec20ef LP |
2414 | assert(name); |
2415 | ||
e9f4aabd | 2416 | log_warning("%sWarning:%s The unit file, source configuration file or drop-ins of %s changed on disk. Run 'systemctl%s daemon-reload' to reload units.", |
3f36991e | 2417 | ansi_highlight_red(), |
1fc464f6 | 2418 | ansi_normal(), |
3f36991e ZJS |
2419 | name, |
2420 | arg_scope == UNIT_FILE_SYSTEM ? "" : " --user"); | |
2421 | } | |
2422 | ||
c45e7e0c | 2423 | static int unit_file_find_path(LookupPaths *lp, const char *unit_name, char **ret_unit_path) { |
33f6c497 ZJS |
2424 | char **p; |
2425 | ||
2426 | assert(lp); | |
2427 | assert(unit_name); | |
33f6c497 | 2428 | |
a3c4eb07 | 2429 | STRV_FOREACH(p, lp->search_path) { |
c6dd36b6 FS |
2430 | _cleanup_free_ char *path = NULL, *lpath = NULL; |
2431 | int r; | |
33f6c497 | 2432 | |
62a85ee0 | 2433 | path = path_join(*p, unit_name); |
33f6c497 ZJS |
2434 | if (!path) |
2435 | return log_oom(); | |
2436 | ||
c4f4fce7 | 2437 | r = chase_symlinks(path, arg_root, 0, &lpath); |
c6dd36b6 FS |
2438 | if (r == -ENOENT) |
2439 | continue; | |
2440 | if (r == -ENOMEM) | |
2441 | return log_oom(); | |
2442 | if (r < 0) | |
86f004fb | 2443 | return log_error_errno(r, "Failed to access path \"%s\": %m", path); |
c6dd36b6 | 2444 | |
c45e7e0c LP |
2445 | if (ret_unit_path) |
2446 | *ret_unit_path = TAKE_PTR(lpath); | |
ae2a15bc | 2447 | |
c6dd36b6 | 2448 | return 1; |
33f6c497 ZJS |
2449 | } |
2450 | ||
f67cb270 LP |
2451 | if (ret_unit_path) |
2452 | *ret_unit_path = NULL; | |
2453 | ||
33f6c497 ZJS |
2454 | return 0; |
2455 | } | |
2456 | ||
173471b7 ZJS |
2457 | static int unit_find_template_path( |
2458 | const char *unit_name, | |
2459 | LookupPaths *lp, | |
b03677e2 LP |
2460 | char **ret_fragment_path, |
2461 | char **ret_template) { | |
173471b7 | 2462 | |
b03677e2 | 2463 | _cleanup_free_ char *t = NULL, *f = NULL; |
173471b7 ZJS |
2464 | int r; |
2465 | ||
2466 | /* Returns 1 if a fragment was found, 0 if not found, negative on error. */ | |
2467 | ||
b03677e2 LP |
2468 | r = unit_file_find_path(lp, unit_name, &f); |
2469 | if (r < 0) | |
2470 | return r; | |
2471 | if (r > 0) { | |
2472 | if (ret_fragment_path) | |
2473 | *ret_fragment_path = TAKE_PTR(f); | |
2474 | if (ret_template) | |
2475 | *ret_template = NULL; | |
2476 | return r; /* found a real unit */ | |
2477 | } | |
2478 | ||
2479 | r = unit_name_template(unit_name, &t); | |
2480 | if (r == -EINVAL) { | |
2481 | if (ret_fragment_path) | |
2482 | *ret_fragment_path = NULL; | |
2483 | if (ret_template) | |
2484 | *ret_template = NULL; | |
173471b7 | 2485 | |
173471b7 | 2486 | return 0; /* not a template, does not exist */ |
b03677e2 | 2487 | } |
173471b7 ZJS |
2488 | if (r < 0) |
2489 | return log_error_errno(r, "Failed to determine template name: %m"); | |
2490 | ||
b03677e2 | 2491 | r = unit_file_find_path(lp, t, ret_fragment_path); |
173471b7 ZJS |
2492 | if (r < 0) |
2493 | return r; | |
2494 | ||
b03677e2 LP |
2495 | if (ret_template) |
2496 | *ret_template = r > 0 ? TAKE_PTR(t) : NULL; | |
ae2a15bc | 2497 | |
173471b7 ZJS |
2498 | return r; |
2499 | } | |
2500 | ||
6e646d22 LP |
2501 | static int unit_find_paths( |
2502 | sd_bus *bus, | |
2503 | const char *unit_name, | |
6e646d22 | 2504 | LookupPaths *lp, |
02a126a3 LP |
2505 | bool force_client_side, |
2506 | char **ret_fragment_path, | |
2507 | char ***ret_dropin_paths) { | |
dab2bce8 | 2508 | |
dab2bce8 | 2509 | _cleanup_strv_free_ char **dropins = NULL; |
02a126a3 | 2510 | _cleanup_free_ char *path = NULL; |
33f6c497 ZJS |
2511 | int r; |
2512 | ||
ad2a0358 | 2513 | /** |
e04df06f ZJS |
2514 | * Finds where the unit is defined on disk. Returns 0 if the unit is not found. Returns 1 if it is |
2515 | * found, and sets: | |
02a126a3 | 2516 | * - the path to the unit in *ret_frament_path, if it exists on disk, |
e04df06f ZJS |
2517 | * - and a strv of existing drop-ins in *ret_dropin_paths, if the arg is not NULL and any dropins |
2518 | * were found. | |
02a126a3 | 2519 | * |
e04df06f ZJS |
2520 | * Returns -ERFKILL if the unit is masked, and -EKEYREJECTED if the unit file could not be loaded for |
2521 | * some reason (the latter only applies if we are going through the service manager). | |
ad2a0358 ZJS |
2522 | */ |
2523 | ||
33f6c497 | 2524 | assert(unit_name); |
02a126a3 | 2525 | assert(ret_fragment_path); |
33f6c497 ZJS |
2526 | assert(lp); |
2527 | ||
02a126a3 LP |
2528 | /* Go via the bus to acquire the path, unless we are explicitly told not to, or when the unit name is a template */ |
2529 | if (!force_client_side && | |
2530 | !install_client_side() && | |
2531 | !unit_name_is_valid(unit_name, UNIT_NAME_TEMPLATE)) { | |
4afd3348 | 2532 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
02a126a3 | 2533 | _cleanup_free_ char *load_state = NULL, *unit = NULL; |
33f6c497 ZJS |
2534 | |
2535 | unit = unit_dbus_path_from_name(unit_name); | |
2536 | if (!unit) | |
2537 | return log_oom(); | |
2538 | ||
02a126a3 LP |
2539 | r = sd_bus_get_property_string( |
2540 | bus, | |
2541 | "org.freedesktop.systemd1", | |
2542 | unit, | |
2543 | "org.freedesktop.systemd1.Unit", | |
2544 | "LoadState", | |
2545 | &error, | |
2546 | &load_state); | |
2547 | if (r < 0) | |
2548 | return log_error_errno(r, "Failed to get LoadState: %s", bus_error_message(&error, r)); | |
2549 | ||
2550 | if (streq(load_state, "masked")) | |
2551 | return -ERFKILL; | |
2552 | if (streq(load_state, "not-found")) { | |
2553 | r = 0; | |
2554 | goto not_found; | |
2555 | } | |
e04df06f | 2556 | if (!STR_IN_SET(load_state, "loaded", "bad-setting")) |
02a126a3 LP |
2557 | return -EKEYREJECTED; |
2558 | ||
33f6c497 ZJS |
2559 | r = sd_bus_get_property_string( |
2560 | bus, | |
2561 | "org.freedesktop.systemd1", | |
2562 | unit, | |
2563 | "org.freedesktop.systemd1.Unit", | |
2564 | "FragmentPath", | |
2565 | &error, | |
ad2a0358 ZJS |
2566 | &path); |
2567 | if (r < 0) | |
2568 | return log_error_errno(r, "Failed to get FragmentPath: %s", bus_error_message(&error, r)); | |
2569 | ||
02a126a3 | 2570 | if (ret_dropin_paths) { |
dab2bce8 IS |
2571 | r = sd_bus_get_property_strv( |
2572 | bus, | |
2573 | "org.freedesktop.systemd1", | |
2574 | unit, | |
2575 | "org.freedesktop.systemd1.Unit", | |
2576 | "DropInPaths", | |
2577 | &error, | |
2578 | &dropins); | |
2579 | if (r < 0) | |
2580 | return log_error_errno(r, "Failed to get DropInPaths: %s", bus_error_message(&error, r)); | |
33f6c497 | 2581 | } |
ad2a0358 | 2582 | } else { |
173471b7 | 2583 | _cleanup_set_free_ Set *names = NULL; |
6483ad89 | 2584 | _cleanup_free_ char *template = NULL; |
33f6c497 | 2585 | |
ad2a0358 ZJS |
2586 | names = set_new(NULL); |
2587 | if (!names) | |
7410616c | 2588 | return log_oom(); |
33f6c497 | 2589 | |
173471b7 | 2590 | r = unit_find_template_path(unit_name, lp, &path, &template); |
ad2a0358 ZJS |
2591 | if (r < 0) |
2592 | return r; | |
86f004fb ZJS |
2593 | if (r > 0) { |
2594 | if (null_or_empty_path(path)) | |
2595 | /* The template is masked. Let's cut the process short. */ | |
2596 | return -ERFKILL; | |
2597 | ||
6483ad89 ZJS |
2598 | /* We found the unit file. If we followed symlinks, this name might be |
2599 | * different then the unit_name with started with. Look for dropins matching | |
2600 | * that "final" name. */ | |
2601 | r = set_put(names, basename(path)); | |
86f004fb | 2602 | } else if (!template) |
6483ad89 ZJS |
2603 | /* No unit file, let's look for dropins matching the original name. |
2604 | * systemd has fairly complicated rules (based on unit type and provenience), | |
2605 | * which units are allowed not to have the main unit file. We err on the | |
2606 | * side of including too many files, and always try to load dropins. */ | |
2607 | r = set_put(names, unit_name); | |
2608 | else | |
2609 | /* The cases where we allow a unit to exist without the main file are | |
2610 | * never valid for templates. Don't try to load dropins in this case. */ | |
2611 | goto not_found; | |
2612 | ||
c6dd36b6 FS |
2613 | if (r < 0) |
2614 | return log_error_errno(r, "Failed to add unit name: %m"); | |
2615 | ||
02a126a3 LP |
2616 | if (ret_dropin_paths) { |
2617 | r = unit_file_find_dropin_conf_paths(arg_root, lp->search_path, NULL, names, &dropins); | |
dab2bce8 IS |
2618 | if (r < 0) |
2619 | return r; | |
2620 | } | |
2621 | } | |
2622 | ||
02a126a3 | 2623 | r = 0; |
dab2bce8 IS |
2624 | |
2625 | if (!isempty(path)) { | |
02a126a3 | 2626 | *ret_fragment_path = TAKE_PTR(path); |
dab2bce8 | 2627 | r = 1; |
02a126a3 LP |
2628 | } else |
2629 | *ret_fragment_path = NULL; | |
dab2bce8 | 2630 | |
02a126a3 LP |
2631 | if (ret_dropin_paths) { |
2632 | if (!strv_isempty(dropins)) { | |
2633 | *ret_dropin_paths = TAKE_PTR(dropins); | |
2634 | r = 1; | |
2635 | } else | |
2636 | *ret_dropin_paths = NULL; | |
33f6c497 | 2637 | } |
02a126a3 | 2638 | |
6483ad89 | 2639 | not_found: |
39c38ce1 | 2640 | if (r == 0 && !arg_force) |
b5e6a600 IS |
2641 | log_error("No files found for %s.", unit_name); |
2642 | ||
ad2a0358 | 2643 | return r; |
33f6c497 ZJS |
2644 | } |
2645 | ||
fa0d5878 | 2646 | static int get_state_one_unit(sd_bus *bus, const char *name, UnitActiveState *active_state) { |
b430f85f | 2647 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
3d083b22 | 2648 | _cleanup_free_ char *buf = NULL, *path = NULL; |
fa0d5878 | 2649 | UnitActiveState state; |
f22f08cd | 2650 | int r; |
701cdcb9 | 2651 | |
31be1221 | 2652 | assert(name); |
fa0d5878 | 2653 | assert(active_state); |
701cdcb9 | 2654 | |
3d083b22 LP |
2655 | path = unit_dbus_path_from_name(name); |
2656 | if (!path) | |
2657 | return log_oom(); | |
2658 | ||
2659 | r = sd_bus_get_property_string( | |
f22f08cd SP |
2660 | bus, |
2661 | "org.freedesktop.systemd1", | |
3d083b22 LP |
2662 | path, |
2663 | "org.freedesktop.systemd1.Unit", | |
2664 | "ActiveState", | |
b430f85f | 2665 | &error, |
3d083b22 LP |
2666 | &buf); |
2667 | if (r < 0) | |
2668 | return log_error_errno(r, "Failed to retrieve unit state: %s", bus_error_message(&error, r)); | |
b430f85f | 2669 | |
3d083b22 | 2670 | state = unit_active_state_from_string(buf); |
2f50e59f LP |
2671 | if (state < 0) |
2672 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid unit state '%s' for: %s", buf, name); | |
701cdcb9 | 2673 | |
fa0d5878 BR |
2674 | *active_state = state; |
2675 | return 0; | |
701cdcb9 MS |
2676 | } |
2677 | ||
86f004fb | 2678 | static int unit_is_masked(sd_bus *bus, LookupPaths *lp, const char *name) { |
bd062910 | 2679 | _cleanup_free_ char *load_state = NULL; |
f22f08cd | 2680 | int r; |
701cdcb9 | 2681 | |
86f004fb ZJS |
2682 | if (unit_name_is_valid(name, UNIT_NAME_TEMPLATE)) { |
2683 | _cleanup_free_ char *path = NULL; | |
2684 | ||
2685 | /* A template cannot be loaded, but it can be still masked, so | |
2686 | * we need to use a different method. */ | |
2687 | ||
2688 | r = unit_file_find_path(lp, name, &path); | |
2689 | if (r < 0) | |
2690 | return r; | |
2691 | if (r == 0) | |
2692 | return false; | |
2693 | return null_or_empty_path(path); | |
2694 | } | |
2695 | ||
bd062910 | 2696 | r = unit_load_state(bus, name, &load_state); |
691395d8 | 2697 | if (r < 0) |
bd062910 | 2698 | return r; |
d0a5cdb2 | 2699 | |
9d9dd746 ZJS |
2700 | return streq(load_state, "masked"); |
2701 | } | |
2702 | ||
2703 | static int check_triggering_units(sd_bus *bus, const char *name) { | |
2704 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
86f004fb | 2705 | _cleanup_free_ char *n = NULL, *path = NULL, *load_state = NULL; |
9d9dd746 ZJS |
2706 | _cleanup_strv_free_ char **triggered_by = NULL; |
2707 | bool print_warning_label = true; | |
2708 | UnitActiveState active_state; | |
2709 | char **i; | |
2710 | int r; | |
2711 | ||
2712 | r = unit_name_mangle(name, 0, &n); | |
2713 | if (r < 0) | |
2714 | return log_error_errno(r, "Failed to mangle unit name: %m"); | |
2715 | ||
86f004fb ZJS |
2716 | r = unit_load_state(bus, name, &load_state); |
2717 | if (r < 0) | |
2718 | return r; | |
2719 | ||
2720 | if (streq(load_state, "masked")) | |
2721 | return 0; | |
9d9dd746 ZJS |
2722 | |
2723 | path = unit_dbus_path_from_name(n); | |
2724 | if (!path) | |
2725 | return log_oom(); | |
701cdcb9 | 2726 | |
f459b602 MAP |
2727 | r = sd_bus_get_property_strv( |
2728 | bus, | |
2729 | "org.freedesktop.systemd1", | |
2730 | path, | |
2731 | "org.freedesktop.systemd1.Unit", | |
2732 | "TriggeredBy", | |
2733 | &error, | |
2734 | &triggered_by); | |
691395d8 LP |
2735 | if (r < 0) |
2736 | return log_error_errno(r, "Failed to get triggered by array of %s: %s", n, bus_error_message(&error, r)); | |
701cdcb9 | 2737 | |
f459b602 | 2738 | STRV_FOREACH(i, triggered_by) { |
fa0d5878 | 2739 | r = get_state_one_unit(bus, *i, &active_state); |
f647962d | 2740 | if (r < 0) |
fa0d5878 | 2741 | return r; |
701cdcb9 | 2742 | |
fa0d5878 | 2743 | if (!IN_SET(active_state, UNIT_ACTIVE, UNIT_RELOADING)) |
f459b602 | 2744 | continue; |
60f9ba0b | 2745 | |
f459b602 MAP |
2746 | if (print_warning_label) { |
2747 | log_warning("Warning: Stopping %s, but it can still be activated by:", n); | |
2748 | print_warning_label = false; | |
701cdcb9 | 2749 | } |
1c291cf3 | 2750 | |
f459b602 | 2751 | log_warning(" %s", *i); |
701cdcb9 | 2752 | } |
f459b602 MAP |
2753 | |
2754 | return 0; | |
701cdcb9 MS |
2755 | } |
2756 | ||
2fc9a280 | 2757 | static const struct { |
85d9b598 LP |
2758 | const char *verb; /* systemctl verb */ |
2759 | const char *method; /* Name of the specific D-Bus method */ | |
2760 | const char *job_type; /* Job type when passing to the generic EnqueueUnitJob() method */ | |
2fc9a280 | 2761 | } unit_actions[] = { |
85d9b598 LP |
2762 | { "start", "StartUnit", "start" }, |
2763 | { "stop", "StopUnit", "stop" }, | |
2764 | { "condstop", "StopUnit", "stop" }, /* legacy alias */ | |
2765 | { "reload", "ReloadUnit", "reload" }, | |
2766 | { "restart", "RestartUnit", "restart" }, | |
2767 | { "try-restart", "TryRestartUnit", "try-restart" }, | |
2768 | { "condrestart", "TryRestartUnit", "try-restart" }, /* legacy alias */ | |
2769 | { "reload-or-restart", "ReloadOrRestartUnit", "reload-or-restart" }, | |
2770 | { "try-reload-or-restart", "ReloadOrTryRestartUnit", "reload-or-try-restart" }, | |
2771 | { "reload-or-try-restart", "ReloadOrTryRestartUnit", "reload-or-try-restart" }, /* legacy alias */ | |
2772 | { "condreload", "ReloadOrTryRestartUnit", "reload-or-try-restart" }, /* legacy alias */ | |
2773 | { "force-reload", "ReloadOrTryRestartUnit", "reload-or-try-restart" }, /* legacy alias */ | |
2fc9a280 LP |
2774 | }; |
2775 | ||
39602c39 | 2776 | static const char *verb_to_method(const char *verb) { |
85d9b598 | 2777 | size_t i; |
39602c39 TA |
2778 | |
2779 | for (i = 0; i < ELEMENTSOF(unit_actions); i++) | |
2780 | if (streq_ptr(unit_actions[i].verb, verb)) | |
2781 | return unit_actions[i].method; | |
2782 | ||
2783 | return "StartUnit"; | |
2784 | } | |
2785 | ||
85d9b598 LP |
2786 | static const char *verb_to_job_type(const char *verb) { |
2787 | size_t i; | |
39602c39 TA |
2788 | |
2789 | for (i = 0; i < ELEMENTSOF(unit_actions); i++) | |
85d9b598 LP |
2790 | if (streq_ptr(unit_actions[i].verb, verb)) |
2791 | return unit_actions[i].job_type; | |
39602c39 | 2792 | |
85d9b598 | 2793 | return "start"; |
39602c39 TA |
2794 | } |
2795 | ||
e4b61340 | 2796 | static int start_unit_one( |
f459b602 | 2797 | sd_bus *bus, |
85d9b598 LP |
2798 | const char *method, /* When using classic per-job bus methods */ |
2799 | const char *job_type, /* When using new-style EnqueueUnitJob() */ | |
e4b61340 LP |
2800 | const char *name, |
2801 | const char *mode, | |
f459b602 | 2802 | sd_bus_error *error, |
93a08841 | 2803 | BusWaitForJobs *w, |
a7056cde | 2804 | BusWaitForUnits *wu) { |
7e4249b9 | 2805 | |
4afd3348 | 2806 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
45fb0699 | 2807 | const char *path; |
85d9b598 | 2808 | bool done = false; |
7e4249b9 | 2809 | int r; |
7e4249b9 | 2810 | |
e4b61340 LP |
2811 | assert(method); |
2812 | assert(name); | |
2813 | assert(mode); | |
22f4096c | 2814 | assert(error); |
7e4249b9 | 2815 | |
dbc9830c ZJS |
2816 | log_debug("%s dbus call org.freedesktop.systemd1.Manager %s(%s, %s)", |
2817 | arg_dry_run ? "Would execute" : "Executing", | |
1ae17672 | 2818 | method, name, mode); |
85d9b598 | 2819 | |
1ae17672 ZJS |
2820 | if (arg_dry_run) |
2821 | return 0; | |
342641fb | 2822 | |
85d9b598 LP |
2823 | if (arg_show_transaction) { |
2824 | _cleanup_(sd_bus_error_free) sd_bus_error enqueue_error = SD_BUS_ERROR_NULL; | |
39602c39 | 2825 | |
85d9b598 LP |
2826 | /* Use the new, fancy EnqueueUnitJob() API if the user wants us to print the transaction */ |
2827 | r = sd_bus_call_method( | |
2828 | bus, | |
2829 | "org.freedesktop.systemd1", | |
2830 | "/org/freedesktop/systemd1", | |
2831 | "org.freedesktop.systemd1.Manager", | |
2832 | "EnqueueUnitJob", | |
2833 | &enqueue_error, | |
2834 | &reply, | |
2835 | "sss", | |
2836 | name, job_type, mode); | |
2837 | if (r < 0) { | |
2838 | if (!sd_bus_error_has_name(&enqueue_error, SD_BUS_ERROR_UNKNOWN_METHOD)) { | |
2839 | (void) sd_bus_error_move(error, &enqueue_error); | |
2840 | goto fail; | |
2841 | } | |
67f3c402 | 2842 | |
85d9b598 LP |
2843 | /* Hmm, the API is not yet available. Let's use the classic API instead (see below). */ |
2844 | log_notice("--show-transaction not supported by this service manager, proceeding without."); | |
2845 | } else { | |
2846 | const char *u, *jt; | |
2847 | uint32_t id; | |
39602c39 | 2848 | |
85d9b598 LP |
2849 | r = sd_bus_message_read(reply, "uosos", &id, &path, &u, NULL, &jt); |
2850 | if (r < 0) | |
2851 | return bus_log_parse_error(r); | |
ee87525c | 2852 | |
85d9b598 | 2853 | log_info("Enqueued anchor job %" PRIu32 " %s/%s.", id, u, jt); |
ee87525c | 2854 | |
85d9b598 LP |
2855 | r = sd_bus_message_enter_container(reply, 'a', "(uosos)"); |
2856 | if (r < 0) | |
2857 | return bus_log_parse_error(r); | |
2858 | for (;;) { | |
2859 | r = sd_bus_message_read(reply, "(uosos)", &id, NULL, &u, NULL, &jt); | |
2860 | if (r < 0) | |
2861 | return bus_log_parse_error(r); | |
2862 | if (r == 0) | |
2863 | break; | |
2864 | ||
2865 | log_info("Enqueued auxiliary job %" PRIu32 " %s/%s.", id, u, jt); | |
2866 | } | |
2867 | ||
2868 | r = sd_bus_message_exit_container(reply); | |
2869 | if (r < 0) | |
2870 | return bus_log_parse_error(r); | |
2871 | ||
2872 | done = true; | |
2873 | } | |
7e4249b9 LP |
2874 | } |
2875 | ||
85d9b598 LP |
2876 | if (!done) { |
2877 | r = sd_bus_call_method( | |
2878 | bus, | |
2879 | "org.freedesktop.systemd1", | |
2880 | "/org/freedesktop/systemd1", | |
2881 | "org.freedesktop.systemd1.Manager", | |
2882 | method, | |
2883 | error, | |
2884 | &reply, | |
2885 | "ss", name, mode); | |
2886 | if (r < 0) | |
2887 | goto fail; | |
2888 | ||
2889 | r = sd_bus_message_read(reply, "o", &path); | |
2890 | if (r < 0) | |
2891 | return bus_log_parse_error(r); | |
2892 | } | |
45fb0699 | 2893 | |
e3e0314b | 2894 | if (need_daemon_reload(bus, name) > 0) |
3f36991e | 2895 | warn_unit_file_changed(name); |
45fb0699 | 2896 | |
ebd011d9 LP |
2897 | if (w) { |
2898 | log_debug("Adding %s to the set", path); | |
2899 | r = bus_wait_for_jobs_add(w, path); | |
cbc9fbd1 | 2900 | if (r < 0) |
d2e1b2fd | 2901 | return log_error_errno(r, "Failed to watch job for %s: %m", name); |
e4b61340 | 2902 | } |
7e4249b9 | 2903 | |
a7056cde LP |
2904 | if (wu) { |
2905 | r = bus_wait_for_units_add_unit(wu, name, BUS_WAIT_FOR_INACTIVE|BUS_WAIT_NO_JOB, NULL, NULL); | |
2906 | if (r < 0) | |
2907 | return log_error_errno(r, "Failed to watch unit %s: %m", name); | |
2908 | } | |
2909 | ||
46eddbb5 | 2910 | return 0; |
85d9b598 LP |
2911 | |
2912 | fail: | |
2913 | /* There's always a fallback possible for legacy actions. */ | |
2914 | if (arg_action != ACTION_SYSTEMCTL) | |
2915 | return r; | |
2916 | ||
2917 | log_error_errno(r, "Failed to %s %s: %s", job_type, name, bus_error_message(error, r)); | |
2918 | ||
2919 | if (!sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT) && | |
2920 | !sd_bus_error_has_name(error, BUS_ERROR_UNIT_MASKED) && | |
2921 | !sd_bus_error_has_name(error, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE)) | |
2922 | log_error("See %s logs and 'systemctl%s status%s %s' for details.", | |
2923 | arg_scope == UNIT_FILE_SYSTEM ? "system" : "user", | |
2924 | arg_scope == UNIT_FILE_SYSTEM ? "" : " --user", | |
2925 | name[0] == '-' ? " --" : "", | |
2926 | name); | |
2927 | ||
2928 | return r; | |
7e4249b9 LP |
2929 | } |
2930 | ||
e3e0314b | 2931 | static int expand_names(sd_bus *bus, char **names, const char* suffix, char ***ret) { |
e3e0314b ZJS |
2932 | _cleanup_strv_free_ char **mangled = NULL, **globs = NULL; |
2933 | char **name; | |
7410616c | 2934 | int r, i; |
e3e0314b | 2935 | |
4fbd7192 LP |
2936 | assert(bus); |
2937 | assert(ret); | |
2938 | ||
e3e0314b ZJS |
2939 | STRV_FOREACH(name, names) { |
2940 | char *t; | |
37cbc1d5 | 2941 | UnitNameMangle options = UNIT_NAME_MANGLE_GLOB | (arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN); |
e3e0314b | 2942 | |
e80733be | 2943 | if (suffix) |
37cbc1d5 | 2944 | r = unit_name_mangle_with_suffix(*name, options, suffix, &t); |
e80733be | 2945 | else |
37cbc1d5 | 2946 | r = unit_name_mangle(*name, options, &t); |
7410616c LP |
2947 | if (r < 0) |
2948 | return log_error_errno(r, "Failed to mangle name: %m"); | |
e3e0314b ZJS |
2949 | |
2950 | if (string_is_glob(t)) | |
6e18964d | 2951 | r = strv_consume(&globs, t); |
e3e0314b | 2952 | else |
6e18964d ZJS |
2953 | r = strv_consume(&mangled, t); |
2954 | if (r < 0) | |
e3e0314b | 2955 | return log_oom(); |
e3e0314b ZJS |
2956 | } |
2957 | ||
2958 | /* Query the manager only if any of the names are a glob, since | |
2959 | * this is fairly expensive */ | |
2960 | if (!strv_isempty(globs)) { | |
4afd3348 | 2961 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
e3e0314b | 2962 | _cleanup_free_ UnitInfo *unit_infos = NULL; |
1b53f64b | 2963 | size_t allocated, n; |
e3e0314b | 2964 | |
1238ee09 | 2965 | r = get_unit_list(bus, NULL, globs, &unit_infos, 0, &reply); |
e3e0314b ZJS |
2966 | if (r < 0) |
2967 | return r; | |
2968 | ||
1b53f64b LP |
2969 | n = strv_length(mangled); |
2970 | allocated = n + 1; | |
2971 | ||
2972 | for (i = 0; i < r; i++) { | |
2973 | if (!GREEDY_REALLOC(mangled, allocated, n+2)) | |
2974 | return log_oom(); | |
2975 | ||
2976 | mangled[n] = strdup(unit_infos[i].id); | |
2977 | if (!mangled[n]) | |
e3e0314b | 2978 | return log_oom(); |
1b53f64b LP |
2979 | |
2980 | mangled[++n] = NULL; | |
2981 | } | |
e3e0314b ZJS |
2982 | } |
2983 | ||
1cc6c93a | 2984 | *ret = TAKE_PTR(mangled); |
e3e0314b ZJS |
2985 | return 0; |
2986 | } | |
2987 | ||
47a0eaa6 MS |
2988 | static const struct { |
2989 | const char *target; | |
2990 | const char *verb; | |
2991 | const char *mode; | |
2992 | } action_table[_ACTION_MAX] = { | |
5fd77930 LP |
2993 | [ACTION_HALT] = { SPECIAL_HALT_TARGET, "halt", "replace-irreversibly" }, |
2994 | [ACTION_POWEROFF] = { SPECIAL_POWEROFF_TARGET, "poweroff", "replace-irreversibly" }, | |
2995 | [ACTION_REBOOT] = { SPECIAL_REBOOT_TARGET, "reboot", "replace-irreversibly" }, | |
2996 | [ACTION_KEXEC] = { SPECIAL_KEXEC_TARGET, "kexec", "replace-irreversibly" }, | |
2997 | [ACTION_RUNLEVEL2] = { SPECIAL_MULTI_USER_TARGET, NULL, "isolate" }, | |
2998 | [ACTION_RUNLEVEL3] = { SPECIAL_MULTI_USER_TARGET, NULL, "isolate" }, | |
2999 | [ACTION_RUNLEVEL4] = { SPECIAL_MULTI_USER_TARGET, NULL, "isolate" }, | |
3000 | [ACTION_RUNLEVEL5] = { SPECIAL_GRAPHICAL_TARGET, NULL, "isolate" }, | |
3001 | [ACTION_RESCUE] = { SPECIAL_RESCUE_TARGET, "rescue", "isolate" }, | |
3002 | [ACTION_EMERGENCY] = { SPECIAL_EMERGENCY_TARGET, "emergency", "isolate" }, | |
3003 | [ACTION_DEFAULT] = { SPECIAL_DEFAULT_TARGET, "default", "isolate" }, | |
3004 | [ACTION_EXIT] = { SPECIAL_EXIT_TARGET, "exit", "replace-irreversibly" }, | |
3005 | [ACTION_SUSPEND] = { SPECIAL_SUSPEND_TARGET, "suspend", "replace-irreversibly" }, | |
3006 | [ACTION_HIBERNATE] = { SPECIAL_HIBERNATE_TARGET, "hibernate", "replace-irreversibly" }, | |
3007 | [ACTION_HYBRID_SLEEP] = { SPECIAL_HYBRID_SLEEP_TARGET, "hybrid-sleep", "replace-irreversibly" }, | |
e68c79db | 3008 | [ACTION_SUSPEND_THEN_HIBERNATE] = { SPECIAL_SUSPEND_THEN_HIBERNATE_TARGET, "suspend-then-hibernate", "replace-irreversibly" }, |
47a0eaa6 MS |
3009 | }; |
3010 | ||
514f4ef5 | 3011 | static enum action verb_to_action(const char *verb) { |
47a0eaa6 MS |
3012 | enum action i; |
3013 | ||
913c1916 | 3014 | for (i = 0; i < _ACTION_MAX; i++) |
f459b602 | 3015 | if (streq_ptr(action_table[i].verb, verb)) |
47a0eaa6 | 3016 | return i; |
514f4ef5 | 3017 | |
f459b602 MAP |
3018 | return _ACTION_INVALID; |
3019 | } | |
e4b61340 | 3020 | |
94369fc0 LP |
3021 | static const char** make_extra_args(const char *extra_args[static 4]) { |
3022 | size_t n = 0; | |
3023 | ||
3024 | if (arg_scope != UNIT_FILE_SYSTEM) | |
3025 | extra_args[n++] = "--user"; | |
3026 | ||
3027 | if (arg_transport == BUS_TRANSPORT_REMOTE) { | |
3028 | extra_args[n++] = "-H"; | |
3029 | extra_args[n++] = arg_host; | |
3030 | } else if (arg_transport == BUS_TRANSPORT_MACHINE) { | |
3031 | extra_args[n++] = "-M"; | |
3032 | extra_args[n++] = arg_host; | |
3033 | } else | |
3034 | assert(arg_transport == BUS_TRANSPORT_LOCAL); | |
3035 | ||
3036 | extra_args[n] = NULL; | |
3037 | return extra_args; | |
3038 | } | |
3039 | ||
e449de87 | 3040 | static int start_unit(int argc, char *argv[], void *userdata) { |
a7056cde | 3041 | _cleanup_(bus_wait_for_units_freep) BusWaitForUnits *wu = NULL; |
ebd011d9 | 3042 | _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL; |
85d9b598 | 3043 | const char *method, *job_type, *mode, *one_name, *suffix = NULL; |
416b8801 | 3044 | _cleanup_free_ char **stopped_units = NULL; /* Do not use _cleanup_strv_free_ */ |
ebd011d9 | 3045 | _cleanup_strv_free_ char **names = NULL; |
0e8d9c0c | 3046 | int r, ret = EXIT_SUCCESS; |
4fbd7192 | 3047 | sd_bus *bus; |
729e3769 | 3048 | char **name; |
e4b61340 | 3049 | |
2f50e59f LP |
3050 | if (arg_wait && !STR_IN_SET(argv[0], "start", "restart")) |
3051 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
3052 | "--wait may only be used with the 'start' or 'restart' commands."); | |
93a08841 MP |
3053 | |
3054 | /* we cannot do sender tracking on the private bus, so we need the full | |
3055 | * one for RefUnit to implement --wait */ | |
3056 | r = acquire_bus(arg_wait ? BUS_FULL : BUS_MANAGER, &bus); | |
4fbd7192 LP |
3057 | if (r < 0) |
3058 | return r; | |
3059 | ||
d2ad7e1b | 3060 | ask_password_agent_open_if_enabled(); |
8a4b13c5 | 3061 | polkit_agent_open_maybe(); |
d2ad7e1b | 3062 | |
e4b61340 | 3063 | if (arg_action == ACTION_SYSTEMCTL) { |
47a0eaa6 | 3064 | enum action action; |
e4b61340 | 3065 | |
e449de87 LP |
3066 | action = verb_to_action(argv[0]); |
3067 | ||
913c1916 | 3068 | if (action != _ACTION_INVALID) { |
85d9b598 | 3069 | /* A command in style "systemctl reboot", "systemctl poweroff", … */ |
913c1916 | 3070 | method = "StartUnit"; |
85d9b598 | 3071 | job_type = "start"; |
913c1916 AJ |
3072 | mode = action_table[action].mode; |
3073 | one_name = action_table[action].target; | |
3074 | } else { | |
3075 | if (streq(argv[0], "isolate")) { | |
85d9b598 | 3076 | /* A "systemctl isolate <unit1> <unit2> …" command */ |
913c1916 | 3077 | method = "StartUnit"; |
85d9b598 | 3078 | job_type = "start"; |
913c1916 | 3079 | mode = "isolate"; |
913c1916 AJ |
3080 | suffix = ".target"; |
3081 | } else { | |
85d9b598 | 3082 | /* A command in style of "systemctl start <unit1> <unit2> …", "sysemctl stop <unit1> <unit2> …" and so on */ |
913c1916 | 3083 | method = verb_to_method(argv[0]); |
85d9b598 | 3084 | job_type = verb_to_job_type(argv[0]); |
913c1916 AJ |
3085 | mode = arg_job_mode; |
3086 | } | |
3087 | one_name = NULL; | |
3088 | } | |
e4b61340 | 3089 | } else { |
85d9b598 | 3090 | /* A SysV legacy command such as "halt", "reboot", "poweroff", … */ |
78ca9099 | 3091 | assert(arg_action >= 0 && arg_action < _ACTION_MAX); |
47a0eaa6 | 3092 | assert(action_table[arg_action].target); |
913c1916 | 3093 | assert(action_table[arg_action].mode); |
e4b61340 LP |
3094 | |
3095 | method = "StartUnit"; | |
85d9b598 | 3096 | job_type = "start"; |
47a0eaa6 | 3097 | mode = action_table[arg_action].mode; |
e3e0314b | 3098 | one_name = action_table[arg_action].target; |
514f4ef5 LP |
3099 | } |
3100 | ||
8b7ec7bb | 3101 | if (one_name) { |
bea1a013 | 3102 | names = strv_new(one_name); |
8b7ec7bb LP |
3103 | if (!names) |
3104 | return log_oom(); | |
3105 | } else { | |
e449de87 | 3106 | r = expand_names(bus, strv_skip(argv, 1), suffix, &names); |
e3e0314b | 3107 | if (r < 0) |
691395d8 | 3108 | return log_error_errno(r, "Failed to expand names: %m"); |
e3e0314b | 3109 | } |
b6520546 | 3110 | |
6e905d93 | 3111 | if (!arg_no_block) { |
ebd011d9 | 3112 | r = bus_wait_for_jobs_new(bus, &w); |
f647962d MS |
3113 | if (r < 0) |
3114 | return log_error_errno(r, "Could not watch jobs: %m"); | |
e4b61340 LP |
3115 | } |
3116 | ||
93a08841 | 3117 | if (arg_wait) { |
31b2cd5d | 3118 | r = sd_bus_call_method_async( |
93a08841 | 3119 | bus, |
31b2cd5d | 3120 | NULL, |
93a08841 MP |
3121 | "org.freedesktop.systemd1", |
3122 | "/org/freedesktop/systemd1", | |
3123 | "org.freedesktop.systemd1.Manager", | |
3124 | "Subscribe", | |
31b2cd5d LP |
3125 | NULL, NULL, |
3126 | NULL); | |
93a08841 | 3127 | if (r < 0) |
31b2cd5d | 3128 | return log_error_errno(r, "Failed to enable subscription: %m"); |
85d9b598 | 3129 | |
a7056cde | 3130 | r = bus_wait_for_units_new(bus, &wu); |
93a08841 | 3131 | if (r < 0) |
a7056cde | 3132 | return log_error_errno(r, "Failed to allocate unit watch context: %m"); |
93a08841 MP |
3133 | } |
3134 | ||
b6520546 | 3135 | STRV_FOREACH(name, names) { |
4afd3348 | 3136 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
f459b602 | 3137 | |
a7056cde | 3138 | r = start_unit_one(bus, method, job_type, *name, mode, &error, w, wu); |
0e8d9c0c LP |
3139 | if (ret == EXIT_SUCCESS && r < 0) |
3140 | ret = translate_bus_error_to_exit_status(r, &error); | |
416b8801 YW |
3141 | |
3142 | if (r >= 0 && streq(method, "StopUnit")) { | |
3143 | r = strv_push(&stopped_units, *name); | |
3144 | if (r < 0) | |
3145 | return log_oom(); | |
3146 | } | |
e4b61340 LP |
3147 | } |
3148 | ||
67f3c402 | 3149 | if (!arg_no_block) { |
94369fc0 | 3150 | const char* extra_args[4]; |
f459b602 | 3151 | |
94369fc0 | 3152 | r = bus_wait_for_jobs(w, arg_quiet, make_extra_args(extra_args)); |
0e8d9c0c LP |
3153 | if (r < 0) |
3154 | return r; | |
49111a70 ZJS |
3155 | |
3156 | /* When stopping units, warn if they can still be triggered by | |
3157 | * another active unit (socket, path, timer) */ | |
416b8801 YW |
3158 | if (!arg_quiet) |
3159 | STRV_FOREACH(name, stopped_units) | |
86f004fb | 3160 | (void) check_triggering_units(bus, *name); |
67f3c402 | 3161 | } |
514f4ef5 | 3162 | |
a7056cde LP |
3163 | if (arg_wait) { |
3164 | r = bus_wait_for_units_run(wu); | |
0e8d9c0c | 3165 | if (r < 0) |
a7056cde LP |
3166 | return log_error_errno(r, "Failed to wait for units: %m"); |
3167 | if (r == BUS_WAIT_FAILURE && ret == EXIT_SUCCESS) | |
0e8d9c0c | 3168 | ret = EXIT_FAILURE; |
93a08841 MP |
3169 | } |
3170 | ||
0e8d9c0c | 3171 | return ret; |
e4b61340 LP |
3172 | } |
3173 | ||
349cc4a5 | 3174 | #if ENABLE_LOGIND |
4fbd7192 | 3175 | static int logind_set_wall_message(void) { |
4afd3348 | 3176 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
4fbd7192 | 3177 | sd_bus *bus; |
f2d11d35 LP |
3178 | _cleanup_free_ char *m = NULL; |
3179 | int r; | |
3180 | ||
4fbd7192 LP |
3181 | r = acquire_bus(BUS_FULL, &bus); |
3182 | if (r < 0) | |
3183 | return r; | |
f2d11d35 LP |
3184 | |
3185 | m = strv_join(arg_wall, " "); | |
3186 | if (!m) | |
3187 | return log_oom(); | |
3188 | ||
dbc9830c ZJS |
3189 | log_debug("%s wall message \"%s\".", arg_dry_run ? "Would set" : "Setting", m); |
3190 | if (arg_dry_run) | |
3191 | return 0; | |
3192 | ||
f2d11d35 LP |
3193 | r = sd_bus_call_method( |
3194 | bus, | |
3195 | "org.freedesktop.login1", | |
3196 | "/org/freedesktop/login1", | |
3197 | "org.freedesktop.login1.Manager", | |
3198 | "SetWallMessage", | |
3199 | &error, | |
3200 | NULL, | |
3201 | "sb", | |
3202 | m, | |
3203 | !arg_no_wall); | |
3204 | ||
3205 | if (r < 0) | |
3206 | return log_warning_errno(r, "Failed to set wall message, ignoring: %s", bus_error_message(&error, r)); | |
f2d11d35 LP |
3207 | return 0; |
3208 | } | |
79f1c8f6 | 3209 | #endif |
f2d11d35 | 3210 | |
c45e5fb8 | 3211 | /* Ask systemd-logind, which might grant access to unprivileged users through polkit */ |
4fbd7192 | 3212 | static int logind_reboot(enum action a) { |
349cc4a5 | 3213 | #if ENABLE_LOGIND |
c45e5fb8 LP |
3214 | static const struct { |
3215 | const char *method; | |
3216 | const char *description; | |
3217 | } actions[_ACTION_MAX] = { | |
3218 | [ACTION_POWEROFF] = { "PowerOff", "power off system" }, | |
3219 | [ACTION_REBOOT] = { "Reboot", "reboot system" }, | |
3220 | [ACTION_HALT] = { "Halt", "halt system" }, | |
3221 | [ACTION_SUSPEND] = { "Suspend", "suspend system" }, | |
3222 | [ACTION_HIBERNATE] = { "Hibernate", "hibernate system" }, | |
3223 | [ACTION_HYBRID_SLEEP] = { "HybridSleep", "put system into hybrid sleep" }, | |
3224 | [ACTION_SUSPEND_THEN_HIBERNATE] = { "SuspendThenHibernate", "suspend system, hibernate later" }, | |
3225 | }; | |
3226 | ||
4afd3348 | 3227 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
4fbd7192 | 3228 | sd_bus *bus; |
f459b602 | 3229 | int r; |
4c80c73c | 3230 | |
c45e5fb8 LP |
3231 | if (a < 0 || a >= _ACTION_MAX || !actions[a].method) |
3232 | return -EINVAL; | |
3233 | ||
4fbd7192 LP |
3234 | r = acquire_bus(BUS_FULL, &bus); |
3235 | if (r < 0) | |
3236 | return r; | |
f2d11d35 | 3237 | |
8a4b13c5 | 3238 | polkit_agent_open_maybe(); |
d2ad7e1b ZJS |
3239 | (void) logind_set_wall_message(); |
3240 | ||
c45e5fb8 LP |
3241 | log_debug("%s org.freedesktop.login1.Manager %s dbus call.", arg_dry_run ? "Would execute" : "Executing", actions[a].method); |
3242 | ||
dbc9830c ZJS |
3243 | if (arg_dry_run) |
3244 | return 0; | |
3245 | ||
f459b602 | 3246 | r = sd_bus_call_method( |
f22f08cd SP |
3247 | bus, |
3248 | "org.freedesktop.login1", | |
3249 | "/org/freedesktop/login1", | |
3250 | "org.freedesktop.login1.Manager", | |
c45e5fb8 | 3251 | actions[a].method, |
f459b602 | 3252 | &error, |
f22f08cd | 3253 | NULL, |
342641fb | 3254 | "b", arg_ask_password); |
f459b602 | 3255 | if (r < 0) |
c45e5fb8 | 3256 | return log_error_errno(r, "Failed to %s via logind: %s", actions[a].description, bus_error_message(&error, r)); |
f459b602 | 3257 | |
691395d8 | 3258 | return 0; |
4c80c73c KS |
3259 | #else |
3260 | return -ENOSYS; | |
3261 | #endif | |
3262 | } | |
3263 | ||
4fbd7192 | 3264 | static int logind_check_inhibitors(enum action a) { |
349cc4a5 | 3265 | #if ENABLE_LOGIND |
4afd3348 | 3266 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
59164be4 | 3267 | _cleanup_strv_free_ char **sessions = NULL; |
f459b602 MAP |
3268 | const char *what, *who, *why, *mode; |
3269 | uint32_t uid, pid; | |
4fbd7192 | 3270 | sd_bus *bus; |
f459b602 | 3271 | unsigned c = 0; |
59164be4 | 3272 | char **s; |
f459b602 | 3273 | int r; |
b37844d3 | 3274 | |
748ebafa LP |
3275 | if (arg_ignore_inhibitors || arg_force > 0) |
3276 | return 0; | |
3277 | ||
3278 | if (arg_when > 0) | |
3279 | return 0; | |
3280 | ||
3281 | if (geteuid() == 0) | |
b37844d3 LP |
3282 | return 0; |
3283 | ||
3284 | if (!on_tty()) | |
3285 | return 0; | |
e08ab379 LP |
3286 | |
3287 | if (arg_transport != BUS_TRANSPORT_LOCAL) | |
3288 | return 0; | |
b37844d3 | 3289 | |
4fbd7192 LP |
3290 | r = acquire_bus(BUS_FULL, &bus); |
3291 | if (r < 0) | |
3292 | return r; | |
3293 | ||
f459b602 | 3294 | r = sd_bus_call_method( |
b37844d3 LP |
3295 | bus, |
3296 | "org.freedesktop.login1", | |
3297 | "/org/freedesktop/login1", | |
3298 | "org.freedesktop.login1.Manager", | |
3299 | "ListInhibitors", | |
b37844d3 | 3300 | NULL, |
f459b602 MAP |
3301 | &reply, |
3302 | NULL); | |
b37844d3 LP |
3303 | if (r < 0) |
3304 | /* If logind is not around, then there are no inhibitors... */ | |
3305 | return 0; | |
3306 | ||
4aa2beac | 3307 | r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssuu)"); |
f459b602 MAP |
3308 | if (r < 0) |
3309 | return bus_log_parse_error(r); | |
b37844d3 | 3310 | |
4aa2beac | 3311 | while ((r = sd_bus_message_read(reply, "(ssssuu)", &what, &who, &why, &mode, &uid, &pid)) > 0) { |
59164be4 | 3312 | _cleanup_free_ char *comm = NULL, *user = NULL; |
f459b602 | 3313 | _cleanup_strv_free_ char **sv = NULL; |
b37844d3 LP |
3314 | |
3315 | if (!streq(mode, "block")) | |
f459b602 | 3316 | continue; |
b37844d3 LP |
3317 | |
3318 | sv = strv_split(what, ":"); | |
3319 | if (!sv) | |
3320 | return log_oom(); | |
3321 | ||
2f50e59f LP |
3322 | if (!pid_is_valid((pid_t) pid)) |
3323 | return log_error_errno(SYNTHETIC_ERRNO(ERANGE), "Invalid PID "PID_FMT".", (pid_t) pid); | |
d028e018 | 3324 | |
b37844d3 | 3325 | if (!strv_contains(sv, |
4c315c2c IS |
3326 | IN_SET(a, |
3327 | ACTION_HALT, | |
3328 | ACTION_POWEROFF, | |
3329 | ACTION_REBOOT, | |
3330 | ACTION_KEXEC) ? "shutdown" : "sleep")) | |
f459b602 | 3331 | continue; |
b37844d3 LP |
3332 | |
3333 | get_process_comm(pid, &comm); | |
59164be4 | 3334 | user = uid_to_name(uid); |
f459b602 | 3335 | |
de0671ee | 3336 | log_warning("Operation inhibited by \"%s\" (PID "PID_FMT" \"%s\", user %s), reason is \"%s\".", |
d028e018 | 3337 | who, (pid_t) pid, strna(comm), strna(user), why); |
b37844d3 | 3338 | |
f459b602 | 3339 | c++; |
b37844d3 | 3340 | } |
f459b602 MAP |
3341 | if (r < 0) |
3342 | return bus_log_parse_error(r); | |
b37844d3 | 3343 | |
f459b602 MAP |
3344 | r = sd_bus_message_exit_container(reply); |
3345 | if (r < 0) | |
3346 | return bus_log_parse_error(r); | |
b37844d3 | 3347 | |
59164be4 LP |
3348 | /* Check for current sessions */ |
3349 | sd_get_sessions(&sessions); | |
3350 | STRV_FOREACH(s, sessions) { | |
59164be4 LP |
3351 | _cleanup_free_ char *type = NULL, *tty = NULL, *seat = NULL, *user = NULL, *service = NULL, *class = NULL; |
3352 | ||
3353 | if (sd_session_get_uid(*s, &uid) < 0 || uid == getuid()) | |
3354 | continue; | |
3355 | ||
3356 | if (sd_session_get_class(*s, &class) < 0 || !streq(class, "user")) | |
3357 | continue; | |
3358 | ||
28abce5d | 3359 | if (sd_session_get_type(*s, &type) < 0 || !STR_IN_SET(type, "x11", "wayland", "tty", "mir")) |
59164be4 LP |
3360 | continue; |
3361 | ||
3362 | sd_session_get_tty(*s, &tty); | |
3363 | sd_session_get_seat(*s, &seat); | |
3364 | sd_session_get_service(*s, &service); | |
3365 | user = uid_to_name(uid); | |
3366 | ||
3367 | log_warning("User %s is logged in on %s.", strna(user), isempty(tty) ? (isempty(seat) ? strna(service) : seat) : tty); | |
3368 | c++; | |
3369 | } | |
3370 | ||
b37844d3 LP |
3371 | if (c <= 0) |
3372 | return 0; | |
3373 | ||
59164be4 | 3374 | log_error("Please retry operation after closing inhibitors and logging out other users.\nAlternatively, ignore inhibitors and users with 'systemctl %s -i'.", |
47a0eaa6 | 3375 | action_table[a].verb); |
b37844d3 LP |
3376 | |
3377 | return -EPERM; | |
3378 | #else | |
3379 | return 0; | |
3380 | #endif | |
3381 | } | |
3382 | ||
97af80c5 LP |
3383 | static int prepare_firmware_setup(void) { |
3384 | ||
3385 | if (!arg_firmware_setup) | |
3386 | return 0; | |
3387 | ||
349cc4a5 | 3388 | #if ENABLE_LOGIND |
4afd3348 | 3389 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
4fbd7192 | 3390 | sd_bus *bus; |
a4921cf4 JJ |
3391 | int r; |
3392 | ||
4fbd7192 LP |
3393 | r = acquire_bus(BUS_FULL, &bus); |
3394 | if (r < 0) | |
3395 | return r; | |
a4921cf4 | 3396 | |
a4921cf4 JJ |
3397 | r = sd_bus_call_method( |
3398 | bus, | |
3399 | "org.freedesktop.login1", | |
3400 | "/org/freedesktop/login1", | |
3401 | "org.freedesktop.login1.Manager", | |
3402 | "SetRebootToFirmwareSetup", | |
3403 | &error, | |
3404 | NULL, | |
3405 | "b", true); | |
4fbd7192 LP |
3406 | if (r < 0) |
3407 | return log_error_errno(r, "Cannot indicate to EFI to boot into setup mode: %s", bus_error_message(&error, r)); | |
a4921cf4 JJ |
3408 | |
3409 | return 0; | |
3410 | #else | |
2f50e59f LP |
3411 | return log_error_errno(SYNTHETIC_ERRNO(ENOSYS), |
3412 | "Booting into firmware setup not supported."); | |
a4921cf4 JJ |
3413 | #endif |
3414 | } | |
3415 | ||
97af80c5 LP |
3416 | static int prepare_boot_loader_menu(void) { |
3417 | ||
3418 | if (arg_boot_loader_menu == USEC_INFINITY) | |
3419 | return 0; | |
3420 | ||
3421 | #if ENABLE_LOGIND | |
3422 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
3423 | sd_bus *bus; | |
4fbd7192 LP |
3424 | int r; |
3425 | ||
97af80c5 LP |
3426 | r = acquire_bus(BUS_FULL, &bus); |
3427 | if (r < 0) | |
3428 | return r; | |
3429 | ||
3430 | r = sd_bus_call_method( | |
3431 | bus, | |
3432 | "org.freedesktop.login1", | |
3433 | "/org/freedesktop/login1", | |
3434 | "org.freedesktop.login1.Manager", | |
3435 | "SetRebootToBootLoaderMenu", | |
3436 | &error, | |
3437 | NULL, | |
3438 | "t", arg_boot_loader_menu); | |
3439 | if (r < 0) | |
3440 | return log_error_errno(r, "Cannot indicate to boot loader to enter boot loader entry menu: %s", bus_error_message(&error, r)); | |
3441 | ||
3442 | return 0; | |
3443 | #else | |
2f50e59f LP |
3444 | return log_error_errno(SYNTHETIC_ERRNO(ENOSYS), |
3445 | "Booting into boot loader menu not supported."); | |
97af80c5 LP |
3446 | #endif |
3447 | } | |
3448 | ||
3449 | static int prepare_boot_loader_entry(void) { | |
3450 | ||
3451 | if (!arg_boot_loader_entry) | |
4fbd7192 LP |
3452 | return 0; |
3453 | ||
97af80c5 LP |
3454 | #if ENABLE_LOGIND |
3455 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
3456 | sd_bus *bus; | |
3457 | int r; | |
4fbd7192 | 3458 | |
97af80c5 LP |
3459 | r = acquire_bus(BUS_FULL, &bus); |
3460 | if (r < 0) | |
3461 | return r; | |
3462 | ||
3463 | r = sd_bus_call_method( | |
3464 | bus, | |
3465 | "org.freedesktop.login1", | |
3466 | "/org/freedesktop/login1", | |
3467 | "org.freedesktop.login1.Manager", | |
3468 | "SetRebootToBootLoaderEntry", | |
3469 | &error, | |
3470 | NULL, | |
3471 | "s", arg_boot_loader_entry); | |
3472 | if (r < 0) | |
3473 | return log_error_errno(r, "Cannot set boot into loader entry '%s': %s", arg_boot_loader_entry, bus_error_message(&error, r)); | |
4fbd7192 | 3474 | |
97af80c5 LP |
3475 | return 0; |
3476 | #else | |
ee429649 LP |
3477 | return log_error_errno(SYNTHETIC_ERRNO(ENOSYS), |
3478 | "Booting into boot loader entry not supported."); | |
97af80c5 | 3479 | #endif |
4fbd7192 LP |
3480 | } |
3481 | ||
4bb2e9d4 ZJS |
3482 | static int load_kexec_kernel(void) { |
3483 | _cleanup_(boot_config_free) BootConfig config = {}; | |
43b736a8 | 3484 | _cleanup_free_ char *kernel = NULL, *initrd = NULL, *options = NULL; |
4bb2e9d4 ZJS |
3485 | const BootEntry *e; |
3486 | pid_t pid; | |
3487 | int r; | |
3488 | ||
3489 | if (kexec_loaded()) { | |
3490 | log_debug("Kexec kernel already loaded."); | |
3491 | return 0; | |
3492 | } | |
3493 | ||
c0d73214 ZJS |
3494 | if (access(KEXEC, X_OK) < 0) |
3495 | return log_error_errno(errno, KEXEC" is not available: %m"); | |
3496 | ||
38bd74d6 | 3497 | r = boot_entries_load_config_auto(NULL, NULL, &config); |
cc5957dc LP |
3498 | if (r == -ENOKEY) /* The call doesn't log about ENOKEY, let's do so here. */ |
3499 | return log_error_errno(r, "Cannot find the ESP partition mount point."); | |
4bb2e9d4 | 3500 | if (r < 0) |
1b20d889 | 3501 | return r; |
4bb2e9d4 | 3502 | |
38bd74d6 LP |
3503 | e = boot_config_default_entry(&config); |
3504 | if (!e) | |
3505 | return log_error_errno(SYNTHETIC_ERRNO(ENOENT), | |
3506 | "No boot loader entry suitable as default, refusing to guess."); | |
3507 | ||
3508 | log_debug("Found default boot loader entry in file \"%s\"", e->path); | |
3509 | ||
78c38fe7 LP |
3510 | if (!e->kernel) |
3511 | return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), | |
3512 | "Boot entry does not refer to Linux kernel, which is not supported currently."); | |
8a1ef3bf | 3513 | if (strv_length(e->initrd) > 1) |
0f88d829 | 3514 | return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), |
8a1ef3bf | 3515 | "Boot entry specifies multiple initrds, which is not supported currently."); |
4bb2e9d4 | 3516 | |
43b736a8 | 3517 | kernel = path_join(e->root, e->kernel); |
9fc32cd9 LP |
3518 | if (!kernel) |
3519 | return log_oom(); | |
3520 | ||
3521 | if (!strv_isempty(e->initrd)) { | |
0f88d829 | 3522 | initrd = path_join(e->root, e->initrd[0]); |
9fc32cd9 LP |
3523 | if (!initrd) |
3524 | return log_oom(); | |
3525 | } | |
3526 | ||
4bb2e9d4 ZJS |
3527 | options = strv_join(e->options, " "); |
3528 | if (!options) | |
3529 | return log_oom(); | |
3530 | ||
cd086a01 ZJS |
3531 | log_full(arg_quiet ? LOG_DEBUG : LOG_INFO, |
3532 | "%s "KEXEC" --load \"%s\" --append \"%s\"%s%s%s", | |
3533 | arg_dry_run ? "Would run" : "Running", | |
3534 | kernel, | |
3535 | options, | |
3536 | initrd ? " --initrd \"" : NULL, strempty(initrd), initrd ? "\"" : ""); | |
4bb2e9d4 ZJS |
3537 | if (arg_dry_run) |
3538 | return 0; | |
3539 | ||
c943e95f | 3540 | r = safe_fork("(kexec)", FORK_WAIT|FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG, &pid); |
4c253ed1 | 3541 | if (r < 0) |
b6e1fff1 | 3542 | return r; |
4c253ed1 | 3543 | if (r == 0) { |
4bb2e9d4 ZJS |
3544 | const char* const args[] = { |
3545 | KEXEC, | |
3546 | "--load", kernel, | |
3547 | "--append", options, | |
3548 | initrd ? "--initrd" : NULL, initrd, | |
c943e95f LP |
3549 | NULL |
3550 | }; | |
4bb2e9d4 ZJS |
3551 | |
3552 | /* Child */ | |
4bb2e9d4 ZJS |
3553 | execv(args[0], (char * const *) args); |
3554 | _exit(EXIT_FAILURE); | |
4c253ed1 LP |
3555 | } |
3556 | ||
df685d57 | 3557 | return 0; |
4bb2e9d4 ZJS |
3558 | } |
3559 | ||
57ab9006 | 3560 | static int set_exit_code(uint8_t code) { |
4afd3348 | 3561 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
57ab9006 LP |
3562 | sd_bus *bus; |
3563 | int r; | |
3564 | ||
3565 | r = acquire_bus(BUS_MANAGER, &bus); | |
3566 | if (r < 0) | |
3567 | return r; | |
3568 | ||
3569 | r = sd_bus_call_method( | |
3570 | bus, | |
3571 | "org.freedesktop.systemd1", | |
3572 | "/org/freedesktop/systemd1", | |
3573 | "org.freedesktop.systemd1.Manager", | |
3574 | "SetExitCode", | |
3575 | &error, | |
3576 | NULL, | |
3577 | "y", code); | |
3578 | if (r < 0) | |
af3d8113 | 3579 | return log_error_errno(r, "Failed to set exit code: %s", bus_error_message(&error, r)); |
57ab9006 LP |
3580 | |
3581 | return 0; | |
3582 | } | |
3583 | ||
e449de87 | 3584 | static int start_special(int argc, char *argv[], void *userdata) { |
4c80c73c | 3585 | enum action a; |
983d9c90 | 3586 | int r; |
2b0f4e6f ZJS |
3587 | bool termination_action; /* an action that terminates the manager, |
3588 | * can be performed also by signal. */ | |
983d9c90 | 3589 | |
e449de87 | 3590 | assert(argv); |
514f4ef5 | 3591 | |
e449de87 | 3592 | a = verb_to_action(argv[0]); |
4c80c73c | 3593 | |
4fbd7192 | 3594 | r = logind_check_inhibitors(a); |
748ebafa LP |
3595 | if (r < 0) |
3596 | return r; | |
3597 | ||
fba868fa LP |
3598 | if (arg_force >= 2) { |
3599 | r = must_be_root(); | |
3600 | if (r < 0) | |
3601 | return r; | |
c32b90de LP |
3602 | } |
3603 | ||
4fbd7192 | 3604 | r = prepare_firmware_setup(); |
a4921cf4 JJ |
3605 | if (r < 0) |
3606 | return r; | |
5bdf2243 | 3607 | |
97af80c5 LP |
3608 | r = prepare_boot_loader_menu(); |
3609 | if (r < 0) | |
3610 | return r; | |
3611 | ||
3612 | r = prepare_boot_loader_entry(); | |
3613 | if (r < 0) | |
3614 | return r; | |
3615 | ||
e449de87 | 3616 | if (a == ACTION_REBOOT && argc > 1) { |
77defcf5 | 3617 | r = update_reboot_parameter_and_warn(argv[1], false); |
b986229e SW |
3618 | if (r < 0) |
3619 | return r; | |
57ab9006 | 3620 | |
4bb2e9d4 ZJS |
3621 | } else if (a == ACTION_KEXEC) { |
3622 | r = load_kexec_kernel(); | |
d23b5ce2 ZJS |
3623 | if (r < 0 && arg_force >= 1) |
3624 | log_notice("Failed to load kexec kernel, continuing without."); | |
3625 | else if (r < 0) | |
4bb2e9d4 ZJS |
3626 | return r; |
3627 | ||
e449de87 | 3628 | } else if (a == ACTION_EXIT && argc > 1) { |
7bbb5359 | 3629 | uint8_t code; |
287419c1 | 3630 | |
57ab9006 LP |
3631 | /* If the exit code is not given on the command line, |
3632 | * don't reset it to zero: just keep it as it might | |
3633 | * have been set previously. */ | |
287419c1 | 3634 | |
e449de87 | 3635 | r = safe_atou8(argv[1], &code); |
4fbd7192 | 3636 | if (r < 0) |
57ab9006 | 3637 | return log_error_errno(r, "Invalid exit code."); |
4fbd7192 | 3638 | |
57ab9006 | 3639 | r = set_exit_code(code); |
691395d8 | 3640 | if (r < 0) |
57ab9006 | 3641 | return r; |
b986229e SW |
3642 | } |
3643 | ||
2b0f4e6f ZJS |
3644 | termination_action = IN_SET(a, |
3645 | ACTION_HALT, | |
3646 | ACTION_POWEROFF, | |
3647 | ACTION_REBOOT); | |
3648 | if (termination_action && arg_force >= 2) | |
477def80 | 3649 | return halt_now(a); |
e606bb61 | 3650 | |
7e59bfcb | 3651 | if (arg_force >= 1 && |
5955df2c | 3652 | (termination_action || IN_SET(a, ACTION_KEXEC, ACTION_EXIT))) |
2b0f4e6f ZJS |
3653 | r = trivial_method(argc, argv, userdata); |
3654 | else { | |
3655 | /* First try logind, to allow authentication with polkit */ | |
3656 | if (IN_SET(a, | |
3657 | ACTION_POWEROFF, | |
3658 | ACTION_REBOOT, | |
36b69c31 | 3659 | ACTION_HALT, |
2b0f4e6f ZJS |
3660 | ACTION_SUSPEND, |
3661 | ACTION_HIBERNATE, | |
c58493c0 | 3662 | ACTION_HYBRID_SLEEP, |
e68c79db | 3663 | ACTION_SUSPEND_THEN_HIBERNATE)) { |
2b0f4e6f ZJS |
3664 | |
3665 | r = logind_reboot(a); | |
3666 | if (r >= 0) | |
3667 | return r; | |
3668 | if (IN_SET(r, -EOPNOTSUPP, -EINPROGRESS)) | |
3669 | /* requested operation is not supported or already in progress */ | |
3670 | return r; | |
20b09ca7 | 3671 | |
130246d2 LP |
3672 | /* On all other errors, try low-level operation. In order to minimize the difference between |
3673 | * operation with and without logind, we explicitly enable non-blocking mode for this, as | |
3674 | * logind's shutdown operations are always non-blocking. */ | |
3675 | ||
3676 | arg_no_block = true; | |
3677 | ||
3678 | } else if (IN_SET(a, ACTION_EXIT, ACTION_KEXEC)) | |
3679 | /* Since exit/kexec are so close in behaviour to power-off/reboot, let's also make them | |
3680 | * asynchronous, in order to not confuse the user needlessly with unexpected behaviour. */ | |
3681 | arg_no_block = true; | |
7089051f | 3682 | |
2b0f4e6f | 3683 | r = start_unit(argc, argv, userdata); |
4c80c73c | 3684 | } |
983d9c90 | 3685 | |
2b0f4e6f ZJS |
3686 | if (termination_action && arg_force < 2 && |
3687 | IN_SET(r, -ENOENT, -ETIMEDOUT)) | |
cc7de2ba | 3688 | log_notice("It is possible to perform action directly, see discussion of --force --force in man:systemctl(1)."); |
2b0f4e6f ZJS |
3689 | |
3690 | return r; | |
514f4ef5 LP |
3691 | } |
3692 | ||
988b3b17 ZJS |
3693 | static int start_system_special(int argc, char *argv[], void *userdata) { |
3694 | /* Like start_special above, but raises an error when running in user mode */ | |
3695 | ||
baaa35ad ZJS |
3696 | if (arg_scope != UNIT_FILE_SYSTEM) |
3697 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
3698 | "Bad action for %s mode.", | |
3699 | arg_scope == UNIT_FILE_GLOBAL ? "--global" : "--user"); | |
988b3b17 ZJS |
3700 | |
3701 | return start_special(argc, argv, userdata); | |
3702 | } | |
3703 | ||
fa0d5878 | 3704 | static int check_unit_generic(int code, const UnitActiveState good_states[], int nb_states, char **args) { |
e3e0314b | 3705 | _cleanup_strv_free_ char **names = NULL; |
fa0d5878 | 3706 | UnitActiveState active_state; |
4fbd7192 | 3707 | sd_bus *bus; |
729e3769 | 3708 | char **name; |
fa0d5878 | 3709 | int r, i; |
d60f6ad0 | 3710 | bool found = false; |
0183528f | 3711 | |
4fbd7192 LP |
3712 | r = acquire_bus(BUS_MANAGER, &bus); |
3713 | if (r < 0) | |
3714 | return r; | |
3715 | ||
e3e0314b | 3716 | r = expand_names(bus, args, NULL, &names); |
f647962d MS |
3717 | if (r < 0) |
3718 | return log_error_errno(r, "Failed to expand names: %m"); | |
e3e0314b ZJS |
3719 | |
3720 | STRV_FOREACH(name, names) { | |
fa0d5878 BR |
3721 | r = get_state_one_unit(bus, *name, &active_state); |
3722 | if (r < 0) | |
3723 | return r; | |
3724 | ||
3725 | if (!arg_quiet) | |
3726 | puts(unit_active_state_to_string(active_state)); | |
60f9ba0b | 3727 | |
fa0d5878 BR |
3728 | for (i = 0; i < nb_states; ++i) |
3729 | if (good_states[i] == active_state) | |
3730 | found = true; | |
1a0fce45 TA |
3731 | } |
3732 | ||
d60f6ad0 LN |
3733 | /* use the given return code for the case that we won't find |
3734 | * any unit which matches the list */ | |
3735 | return found ? 0 : code; | |
1a0fce45 TA |
3736 | } |
3737 | ||
e449de87 | 3738 | static int check_unit_active(int argc, char *argv[], void *userdata) { |
9897b09b LP |
3739 | static const UnitActiveState states[] = { |
3740 | UNIT_ACTIVE, | |
3741 | UNIT_RELOADING, | |
3742 | }; | |
3743 | ||
e3e0314b | 3744 | /* According to LSB: 3, "program is not running" */ |
b613907e | 3745 | return check_unit_generic(EXIT_PROGRAM_NOT_RUNNING, states, ELEMENTSOF(states), strv_skip(argv, 1)); |
e3e0314b | 3746 | } |
0183528f | 3747 | |
e449de87 | 3748 | static int check_unit_failed(int argc, char *argv[], void *userdata) { |
9897b09b LP |
3749 | static const UnitActiveState states[] = { |
3750 | UNIT_FAILED, | |
3751 | }; | |
3752 | ||
b613907e | 3753 | return check_unit_generic(EXIT_PROGRAM_DEAD_AND_PID_EXISTS, states, ELEMENTSOF(states), strv_skip(argv, 1)); |
48220598 LP |
3754 | } |
3755 | ||
e449de87 | 3756 | static int kill_unit(int argc, char *argv[], void *userdata) { |
e3e0314b | 3757 | _cleanup_strv_free_ char **names = NULL; |
ac5e3a50 | 3758 | char *kill_who = NULL, **name; |
4fbd7192 | 3759 | sd_bus *bus; |
e3e0314b | 3760 | int r, q; |
8a0867d6 | 3761 | |
4fbd7192 LP |
3762 | r = acquire_bus(BUS_MANAGER, &bus); |
3763 | if (r < 0) | |
3764 | return r; | |
3765 | ||
8a4b13c5 | 3766 | polkit_agent_open_maybe(); |
d2ad7e1b | 3767 | |
8a0867d6 LP |
3768 | if (!arg_kill_who) |
3769 | arg_kill_who = "all"; | |
3770 | ||
ac5e3a50 JS |
3771 | /* --fail was specified */ |
3772 | if (streq(arg_job_mode, "fail")) | |
81d62103 | 3773 | kill_who = strjoina(arg_kill_who, "-fail"); |
ac5e3a50 | 3774 | |
e449de87 | 3775 | r = expand_names(bus, strv_skip(argv, 1), NULL, &names); |
e3e0314b | 3776 | if (r < 0) |
691395d8 | 3777 | return log_error_errno(r, "Failed to expand names: %m"); |
60f9ba0b | 3778 | |
e3e0314b | 3779 | STRV_FOREACH(name, names) { |
4afd3348 | 3780 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
342641fb | 3781 | |
6e646d22 | 3782 | q = sd_bus_call_method( |
f22f08cd | 3783 | bus, |
b0193f1c LP |
3784 | "org.freedesktop.systemd1", |
3785 | "/org/freedesktop/systemd1", | |
3786 | "org.freedesktop.systemd1.Manager", | |
6e646d22 LP |
3787 | "KillUnit", |
3788 | &error, | |
3789 | NULL, | |
169d7fb6 | 3790 | "ssi", *name, kill_who ? kill_who : arg_kill_who, arg_signal); |
e3e0314b | 3791 | if (q < 0) { |
169d7fb6 | 3792 | log_error_errno(q, "Failed to kill unit %s: %s", *name, bus_error_message(&error, q)); |
e3e0314b ZJS |
3793 | if (r == 0) |
3794 | r = q; | |
f459b602 | 3795 | } |
8a0867d6 | 3796 | } |
f459b602 | 3797 | |
e3e0314b | 3798 | return r; |
8a0867d6 LP |
3799 | } |
3800 | ||
d81bc51f LP |
3801 | static int clean_unit(int argc, char *argv[], void *userdata) { |
3802 | _cleanup_(bus_wait_for_units_freep) BusWaitForUnits *w = NULL; | |
3803 | _cleanup_strv_free_ char **names = NULL; | |
3804 | int r, ret = EXIT_SUCCESS; | |
3805 | char **name; | |
3806 | sd_bus *bus; | |
3807 | ||
3808 | r = acquire_bus(BUS_FULL, &bus); | |
3809 | if (r < 0) | |
3810 | return r; | |
3811 | ||
3812 | polkit_agent_open_maybe(); | |
3813 | ||
3814 | if (!arg_clean_what) { | |
3815 | arg_clean_what = strv_new("cache", "runtime"); | |
3816 | if (!arg_clean_what) | |
3817 | return log_oom(); | |
3818 | } | |
3819 | ||
3820 | r = expand_names(bus, strv_skip(argv, 1), NULL, &names); | |
3821 | if (r < 0) | |
3822 | return log_error_errno(r, "Failed to expand names: %m"); | |
3823 | ||
3824 | if (!arg_no_block) { | |
3825 | r = bus_wait_for_units_new(bus, &w); | |
3826 | if (r < 0) | |
3827 | return log_error_errno(r, "Failed to allocate unit waiter: %m"); | |
3828 | } | |
3829 | ||
3830 | STRV_FOREACH(name, names) { | |
3831 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
3832 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; | |
3833 | ||
3834 | if (w) { | |
3835 | /* If we shall wait for the cleaning to complete, let's add a ref on the unit first */ | |
3836 | r = sd_bus_call_method( | |
3837 | bus, | |
3838 | "org.freedesktop.systemd1", | |
3839 | "/org/freedesktop/systemd1", | |
3840 | "org.freedesktop.systemd1.Manager", | |
3841 | "RefUnit", | |
3842 | &error, | |
3843 | NULL, | |
3844 | "s", *name); | |
3845 | if (r < 0) { | |
3846 | log_error_errno(r, "Failed to add reference to unit %s: %s", *name, bus_error_message(&error, r)); | |
3847 | if (ret == EXIT_SUCCESS) | |
3848 | ret = r; | |
3849 | continue; | |
3850 | } | |
3851 | } | |
3852 | ||
3853 | r = sd_bus_message_new_method_call( | |
3854 | bus, | |
3855 | &m, | |
3856 | "org.freedesktop.systemd1", | |
3857 | "/org/freedesktop/systemd1", | |
3858 | "org.freedesktop.systemd1.Manager", | |
3859 | "CleanUnit"); | |
3860 | if (r < 0) | |
3861 | return bus_log_create_error(r); | |
3862 | ||
3863 | r = sd_bus_message_append(m, "s", *name); | |
3864 | if (r < 0) | |
3865 | return bus_log_create_error(r); | |
3866 | ||
3867 | r = sd_bus_message_append_strv(m, arg_clean_what); | |
3868 | if (r < 0) | |
3869 | return bus_log_create_error(r); | |
3870 | ||
3871 | r = sd_bus_call(bus, m, 0, &error, NULL); | |
3872 | if (r < 0) { | |
3873 | log_error_errno(r, "Failed to clean unit %s: %s", *name, bus_error_message(&error, r)); | |
3874 | if (ret == EXIT_SUCCESS) { | |
3875 | ret = r; | |
3876 | continue; | |
3877 | } | |
3878 | } | |
3879 | ||
3880 | if (w) { | |
3881 | r = bus_wait_for_units_add_unit(w, *name, BUS_WAIT_REFFED|BUS_WAIT_FOR_MAINTENANCE_END, NULL, NULL); | |
3882 | if (r < 0) | |
3883 | return log_error_errno(r, "Failed to watch unit %s: %m", *name); | |
3884 | } | |
3885 | } | |
3886 | ||
3887 | r = bus_wait_for_units_run(w); | |
3888 | if (r < 0) | |
3889 | return log_error_errno(r, "Failed to wait for units: %m"); | |
3890 | if (r == BUS_WAIT_FAILURE) | |
3891 | ret = EXIT_FAILURE; | |
3892 | ||
3893 | return ret; | |
3894 | } | |
3895 | ||
582a507f | 3896 | typedef struct ExecStatusInfo { |
0129173a LP |
3897 | char *name; |
3898 | ||
582a507f LP |
3899 | char *path; |
3900 | char **argv; | |
3901 | ||
b708e7ce LP |
3902 | bool ignore; |
3903 | ||
582a507f LP |
3904 | usec_t start_timestamp; |
3905 | usec_t exit_timestamp; | |
3906 | pid_t pid; | |
3907 | int code; | |
3908 | int status; | |
3909 | ||
b3d59367 AZ |
3910 | ExecCommandFlags flags; |
3911 | ||
582a507f LP |
3912 | LIST_FIELDS(struct ExecStatusInfo, exec); |
3913 | } ExecStatusInfo; | |
3914 | ||
3915 | static void exec_status_info_free(ExecStatusInfo *i) { | |
3916 | assert(i); | |
3917 | ||
0129173a | 3918 | free(i->name); |
582a507f LP |
3919 | free(i->path); |
3920 | strv_free(i->argv); | |
3921 | free(i); | |
3922 | } | |
3923 | ||
b3d59367 AZ |
3924 | static int exec_status_info_deserialize(sd_bus_message *m, ExecStatusInfo *i, bool is_ex_prop) { |
3925 | _cleanup_strv_free_ char **ex_opts = NULL; | |
b21a0ef8 | 3926 | uint64_t start_timestamp, exit_timestamp, start_timestamp_monotonic, exit_timestamp_monotonic; |
f459b602 | 3927 | const char *path; |
582a507f LP |
3928 | uint32_t pid; |
3929 | int32_t code, status; | |
f459b602 | 3930 | int ignore, r; |
582a507f | 3931 | |
f459b602 | 3932 | assert(m); |
582a507f | 3933 | assert(i); |
582a507f | 3934 | |
b3d59367 | 3935 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_STRUCT, is_ex_prop ? "sasasttttuii" : "sasbttttuii"); |
f459b602 MAP |
3936 | if (r < 0) |
3937 | return bus_log_parse_error(r); | |
3938 | else if (r == 0) | |
3939 | return 0; | |
582a507f | 3940 | |
f459b602 MAP |
3941 | r = sd_bus_message_read(m, "s", &path); |
3942 | if (r < 0) | |
3943 | return bus_log_parse_error(r); | |
582a507f | 3944 | |
f84190d8 LP |
3945 | i->path = strdup(path); |
3946 | if (!i->path) | |
f459b602 | 3947 | return log_oom(); |
582a507f | 3948 | |
f459b602 MAP |
3949 | r = sd_bus_message_read_strv(m, &i->argv); |
3950 | if (r < 0) | |
3951 | return bus_log_parse_error(r); | |
3952 | ||
b3d59367 AZ |
3953 | r = is_ex_prop ? sd_bus_message_read_strv(m, &ex_opts) : sd_bus_message_read(m, "b", &ignore); |
3954 | if (r < 0) | |
3955 | return bus_log_parse_error(r); | |
3956 | ||
f459b602 | 3957 | r = sd_bus_message_read(m, |
b3d59367 | 3958 | "ttttuii", |
f459b602 MAP |
3959 | &start_timestamp, &start_timestamp_monotonic, |
3960 | &exit_timestamp, &exit_timestamp_monotonic, | |
3961 | &pid, | |
3962 | &code, &status); | |
3963 | if (r < 0) | |
3964 | return bus_log_parse_error(r); | |
582a507f | 3965 | |
b3d59367 AZ |
3966 | if (is_ex_prop) { |
3967 | r = exec_command_flags_from_strv(ex_opts, &i->flags); | |
3968 | if (r < 0) | |
3969 | return log_error_errno(r, "Failed to convert strv to ExecCommandFlags: %m"); | |
3970 | ||
3971 | i->ignore = FLAGS_SET(i->flags, EXEC_COMMAND_IGNORE_FAILURE); | |
3972 | } else | |
3973 | i->ignore = ignore; | |
3974 | ||
582a507f LP |
3975 | i->start_timestamp = (usec_t) start_timestamp; |
3976 | i->exit_timestamp = (usec_t) exit_timestamp; | |
3977 | i->pid = (pid_t) pid; | |
3978 | i->code = code; | |
3979 | i->status = status; | |
3980 | ||
f459b602 MAP |
3981 | r = sd_bus_message_exit_container(m); |
3982 | if (r < 0) | |
3983 | return bus_log_parse_error(r); | |
3984 | ||
3985 | return 1; | |
582a507f LP |
3986 | } |
3987 | ||
d5db7fe6 WC |
3988 | typedef struct UnitCondition { |
3989 | char *name; | |
b1ed76ae | 3990 | char *param; |
d5db7fe6 WC |
3991 | bool trigger; |
3992 | bool negate; | |
d5db7fe6 WC |
3993 | int tristate; |
3994 | ||
b1ed76ae | 3995 | LIST_FIELDS(struct UnitCondition, conditions); |
d5db7fe6 WC |
3996 | } UnitCondition; |
3997 | ||
3998 | static void unit_condition_free(UnitCondition *c) { | |
9bb71940 ZJS |
3999 | if (!c) |
4000 | return; | |
d5db7fe6 WC |
4001 | |
4002 | free(c->name); | |
4003 | free(c->param); | |
4004 | free(c); | |
4005 | } | |
4006 | ||
9bb71940 ZJS |
4007 | DEFINE_TRIVIAL_CLEANUP_FUNC(UnitCondition*, unit_condition_free); |
4008 | ||
61cbdc4b LP |
4009 | typedef struct UnitStatusInfo { |
4010 | const char *id; | |
4011 | const char *load_state; | |
4012 | const char *active_state; | |
4013 | const char *sub_state; | |
a4375746 | 4014 | const char *unit_file_state; |
d2dc52db | 4015 | const char *unit_file_preset; |
61cbdc4b LP |
4016 | |
4017 | const char *description; | |
4a9e2fff | 4018 | const char *following; |
61cbdc4b | 4019 | |
49dbfa7b LP |
4020 | char **documentation; |
4021 | ||
1b64d026 LP |
4022 | const char *fragment_path; |
4023 | const char *source_path; | |
4ad49000 | 4024 | const char *control_group; |
61cbdc4b | 4025 | |
76d14b87 OS |
4026 | char **dropin_paths; |
4027 | ||
9f39404c | 4028 | const char *load_error; |
f42806df | 4029 | const char *result; |
9f39404c | 4030 | |
584be568 | 4031 | usec_t inactive_exit_timestamp; |
df50185b | 4032 | usec_t inactive_exit_timestamp_monotonic; |
584be568 LP |
4033 | usec_t active_enter_timestamp; |
4034 | usec_t active_exit_timestamp; | |
4035 | usec_t inactive_enter_timestamp; | |
4036 | ||
45fb0699 | 4037 | bool need_daemon_reload; |
055ef36b | 4038 | bool transient; |
45fb0699 | 4039 | |
61cbdc4b LP |
4040 | /* Service */ |
4041 | pid_t main_pid; | |
4042 | pid_t control_pid; | |
4043 | const char *status_text; | |
175728c4 | 4044 | const char *pid_file; |
d06dacd0 | 4045 | bool running:1; |
b4af5a80 | 4046 | int status_errno; |
61cbdc4b LP |
4047 | |
4048 | usec_t start_timestamp; | |
4049 | usec_t exit_timestamp; | |
4050 | ||
4051 | int exit_code, exit_status; | |
4052 | ||
90bbc946 LP |
4053 | usec_t condition_timestamp; |
4054 | bool condition_result; | |
b1ed76ae | 4055 | LIST_HEAD(UnitCondition, conditions); |
59fccdc5 LP |
4056 | |
4057 | usec_t assert_timestamp; | |
4058 | bool assert_result; | |
4059 | bool failed_assert_trigger; | |
4060 | bool failed_assert_negate; | |
4061 | const char *failed_assert; | |
4062 | const char *failed_assert_parameter; | |
601b0842 GS |
4063 | usec_t next_elapse_real; |
4064 | usec_t next_elapse_monotonic; | |
90bbc946 | 4065 | |
61cbdc4b LP |
4066 | /* Socket */ |
4067 | unsigned n_accepted; | |
4068 | unsigned n_connections; | |
a98f7575 | 4069 | unsigned n_refused; |
b8131a87 | 4070 | bool accept; |
61cbdc4b | 4071 | |
13160134 | 4072 | /* Pairs of type, path */ |
67419600 OS |
4073 | char **listen; |
4074 | ||
61cbdc4b LP |
4075 | /* Device */ |
4076 | const char *sysfs_path; | |
4077 | ||
4078 | /* Mount, Automount */ | |
4079 | const char *where; | |
4080 | ||
4081 | /* Swap */ | |
4082 | const char *what; | |
582a507f | 4083 | |
934277fe LP |
4084 | /* CGroup */ |
4085 | uint64_t memory_current; | |
48422635 | 4086 | uint64_t memory_min; |
da4d897e TH |
4087 | uint64_t memory_low; |
4088 | uint64_t memory_high; | |
4089 | uint64_t memory_max; | |
96e131ea | 4090 | uint64_t memory_swap_max; |
934277fe | 4091 | uint64_t memory_limit; |
5ad096b3 | 4092 | uint64_t cpu_usage_nsec; |
03a7b521 LP |
4093 | uint64_t tasks_current; |
4094 | uint64_t tasks_max; | |
0e97c93f DM |
4095 | uint64_t ip_ingress_bytes; |
4096 | uint64_t ip_egress_bytes; | |
7a5de3f9 LP |
4097 | uint64_t io_read_bytes; |
4098 | uint64_t io_write_bytes; | |
0e97c93f | 4099 | |
7ad5439e | 4100 | uint64_t default_memory_min; |
c52db42b CD |
4101 | uint64_t default_memory_low; |
4102 | ||
582a507f | 4103 | LIST_HEAD(ExecStatusInfo, exec); |
61cbdc4b LP |
4104 | } UnitStatusInfo; |
4105 | ||
a7335518 ZJS |
4106 | static void unit_status_info_free(UnitStatusInfo *info) { |
4107 | ExecStatusInfo *p; | |
4108 | UnitCondition *c; | |
4109 | ||
4110 | strv_free(info->documentation); | |
4111 | strv_free(info->dropin_paths); | |
4112 | strv_free(info->listen); | |
4113 | ||
4114 | while ((c = info->conditions)) { | |
4115 | LIST_REMOVE(conditions, info->conditions, c); | |
4116 | unit_condition_free(c); | |
4117 | } | |
4118 | ||
4119 | while ((p = info->exec)) { | |
4120 | LIST_REMOVE(exec, info->exec, p); | |
4121 | exec_status_info_free(p); | |
4122 | } | |
4123 | } | |
4124 | ||
f459b602 | 4125 | static void print_status_info( |
291d565a | 4126 | sd_bus *bus, |
f459b602 MAP |
4127 | UnitStatusInfo *i, |
4128 | bool *ellipsized) { | |
4129 | ||
4d9685be ZJS |
4130 | char since1[FORMAT_TIMESTAMP_RELATIVE_MAX], since2[FORMAT_TIMESTAMP_MAX]; |
4131 | const char *s1, *s2, *active_on, *active_off, *on, *off, *ss; | |
c7080257 LP |
4132 | _cleanup_free_ char *formatted_path = NULL; |
4133 | ExecStatusInfo *p; | |
584be568 | 4134 | usec_t timestamp; |
1b64d026 | 4135 | const char *path; |
13160134 | 4136 | char **t, **t2; |
291d565a | 4137 | int r; |
582a507f | 4138 | |
61cbdc4b LP |
4139 | assert(i); |
4140 | ||
4141 | /* This shows pretty information about a unit. See | |
4142 | * print_property() for a low-level property printer */ | |
4143 | ||
b0d14c69 LP |
4144 | if (streq_ptr(i->active_state, "failed")) { |
4145 | active_on = ansi_highlight_red(); | |
1fc464f6 | 4146 | active_off = ansi_normal(); |
1cf03a4f | 4147 | } else if (STRPTR_IN_SET(i->active_state, "active", "reloading")) { |
b0d14c69 | 4148 | active_on = ansi_highlight_green(); |
1fc464f6 | 4149 | active_off = ansi_normal(); |
b0d14c69 LP |
4150 | } else |
4151 | active_on = active_off = ""; | |
4152 | ||
9a6f746f | 4153 | printf("%s%s%s %s", active_on, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE), active_off, strna(i->id)); |
61cbdc4b LP |
4154 | |
4155 | if (i->description && !streq_ptr(i->id, i->description)) | |
4156 | printf(" - %s", i->description); | |
4157 | ||
4158 | printf("\n"); | |
4159 | ||
4a9e2fff | 4160 | if (i->following) |
856323c9 | 4161 | printf(" Follow: unit currently follows state of %s\n", i->following); |
4a9e2fff | 4162 | |
c4555ad8 | 4163 | if (STRPTR_IN_SET(i->load_state, "error", "not-found", "bad-setting")) { |
0b5a519c | 4164 | on = ansi_highlight_red(); |
1fc464f6 | 4165 | off = ansi_normal(); |
c31b4423 LP |
4166 | } else |
4167 | on = off = ""; | |
4168 | ||
c7080257 LP |
4169 | path = i->source_path ?: i->fragment_path; |
4170 | if (path && terminal_urlify_path(path, NULL, &formatted_path) >= 0) | |
4171 | path = formatted_path; | |
1b64d026 | 4172 | |
9a0abfa8 | 4173 | if (!isempty(i->load_error)) |
856323c9 ZJS |
4174 | printf(" Loaded: %s%s%s (Reason: %s)\n", |
4175 | on, strna(i->load_state), off, i->load_error); | |
15ce1647 LP |
4176 | else if (path && !isempty(i->unit_file_state) && !isempty(i->unit_file_preset) && |
4177 | !STR_IN_SET(i->unit_file_state, "generated", "transient")) | |
d2dc52db LP |
4178 | printf(" Loaded: %s%s%s (%s; %s; vendor preset: %s)\n", |
4179 | on, strna(i->load_state), off, path, i->unit_file_state, i->unit_file_preset); | |
4180 | else if (path && !isempty(i->unit_file_state)) | |
856323c9 ZJS |
4181 | printf(" Loaded: %s%s%s (%s; %s)\n", |
4182 | on, strna(i->load_state), off, path, i->unit_file_state); | |
1b64d026 | 4183 | else if (path) |
856323c9 ZJS |
4184 | printf(" Loaded: %s%s%s (%s)\n", |
4185 | on, strna(i->load_state), off, path); | |
61cbdc4b | 4186 | else |
856323c9 ZJS |
4187 | printf(" Loaded: %s%s%s\n", |
4188 | on, strna(i->load_state), off); | |
61cbdc4b | 4189 | |
055ef36b LP |
4190 | if (i->transient) |
4191 | printf("Transient: yes\n"); | |
4192 | ||
76d14b87 | 4193 | if (!strv_isempty(i->dropin_paths)) { |
f459b602 | 4194 | _cleanup_free_ char *dir = NULL; |
76d14b87 | 4195 | bool last = false; |
f459b602 | 4196 | char ** dropin; |
76d14b87 OS |
4197 | |
4198 | STRV_FOREACH(dropin, i->dropin_paths) { | |
c7080257 LP |
4199 | _cleanup_free_ char *dropin_formatted = NULL; |
4200 | const char *df; | |
4201 | ||
eb2c3192 LP |
4202 | if (!dir || last) { |
4203 | printf(dir ? " " : | |
4204 | " Drop-In: "); | |
76d14b87 | 4205 | |
97b11eed | 4206 | dir = mfree(dir); |
76d14b87 | 4207 | |
5f311f8c LP |
4208 | dir = dirname_malloc(*dropin); |
4209 | if (!dir) { | |
76d14b87 OS |
4210 | log_oom(); |
4211 | return; | |
4212 | } | |
4213 | ||
eb2c3192 LP |
4214 | printf("%s\n" |
4215 | " %s", dir, | |
9a6f746f | 4216 | special_glyph(SPECIAL_GLYPH_TREE_RIGHT)); |
76d14b87 OS |
4217 | } |
4218 | ||
4219 | last = ! (*(dropin + 1) && startswith(*(dropin + 1), dir)); | |
4220 | ||
c7080257 LP |
4221 | if (terminal_urlify_path(*dropin, basename(*dropin), &dropin_formatted) >= 0) |
4222 | df = dropin_formatted; | |
4223 | else | |
4224 | df = *dropin; | |
4225 | ||
4226 | printf("%s%s", df, last ? "\n" : ", "); | |
76d14b87 | 4227 | } |
76d14b87 OS |
4228 | } |
4229 | ||
2ee68f72 | 4230 | ss = streq_ptr(i->active_state, i->sub_state) ? NULL : i->sub_state; |
2ee68f72 | 4231 | if (ss) |
856323c9 | 4232 | printf(" Active: %s%s (%s)%s", |
b0d14c69 | 4233 | active_on, strna(i->active_state), ss, active_off); |
2ee68f72 | 4234 | else |
856323c9 | 4235 | printf(" Active: %s%s%s", |
b0d14c69 | 4236 | active_on, strna(i->active_state), active_off); |
61cbdc4b | 4237 | |
f42806df LP |
4238 | if (!isempty(i->result) && !streq(i->result, "success")) |
4239 | printf(" (Result: %s)", i->result); | |
4240 | ||
1cf03a4f ZJS |
4241 | timestamp = STRPTR_IN_SET(i->active_state, "active", "reloading") ? i->active_enter_timestamp : |
4242 | STRPTR_IN_SET(i->active_state, "inactive", "failed") ? i->inactive_enter_timestamp : | |
4243 | STRPTR_IN_SET(i->active_state, "activating") ? i->inactive_exit_timestamp : | |
4244 | i->active_exit_timestamp; | |
584be568 | 4245 | |
bbb8486e | 4246 | s1 = format_timestamp_relative(since1, sizeof(since1), timestamp); |
584be568 LP |
4247 | s2 = format_timestamp(since2, sizeof(since2), timestamp); |
4248 | ||
4249 | if (s1) | |
538da63d | 4250 | printf(" since %s; %s\n", s2, s1); |
584be568 | 4251 | else if (s2) |
538da63d | 4252 | printf(" since %s\n", s2); |
584be568 LP |
4253 | else |
4254 | printf("\n"); | |
4255 | ||
601b0842 GS |
4256 | if (endswith(i->id, ".timer")) { |
4257 | char tstamp1[FORMAT_TIMESTAMP_RELATIVE_MAX], | |
4258 | tstamp2[FORMAT_TIMESTAMP_MAX]; | |
4d9685be | 4259 | const char *next_rel_time, *next_time; |
601b0842 GS |
4260 | dual_timestamp nw, next = {i->next_elapse_real, |
4261 | i->next_elapse_monotonic}; | |
4262 | usec_t next_elapse; | |
4263 | ||
4264 | printf(" Trigger: "); | |
4265 | ||
4266 | dual_timestamp_get(&nw); | |
4267 | next_elapse = calc_next_elapse(&nw, &next); | |
4d9685be ZJS |
4268 | next_rel_time = format_timestamp_relative(tstamp1, sizeof tstamp1, next_elapse); |
4269 | next_time = format_timestamp(tstamp2, sizeof tstamp2, next_elapse); | |
601b0842 GS |
4270 | |
4271 | if (next_time && next_rel_time) | |
4272 | printf("%s; %s\n", next_time, next_rel_time); | |
4273 | else | |
4274 | printf("n/a\n"); | |
4275 | } | |
4276 | ||
90bbc946 | 4277 | if (!i->condition_result && i->condition_timestamp > 0) { |
d5db7fe6 WC |
4278 | UnitCondition *c; |
4279 | int n = 0; | |
4280 | ||
bbb8486e | 4281 | s1 = format_timestamp_relative(since1, sizeof(since1), i->condition_timestamp); |
90bbc946 LP |
4282 | s2 = format_timestamp(since2, sizeof(since2), i->condition_timestamp); |
4283 | ||
59fccdc5 | 4284 | printf("Condition: start %scondition failed%s at %s%s%s\n", |
1fc464f6 | 4285 | ansi_highlight_yellow(), ansi_normal(), |
5cfee414 | 4286 | s2, s1 ? "; " : "", strempty(s1)); |
d5db7fe6 | 4287 | |
b1ed76ae | 4288 | LIST_FOREACH(conditions, c, i->conditions) |
d5db7fe6 WC |
4289 | if (c->tristate < 0) |
4290 | n++; | |
d5db7fe6 | 4291 | |
b1ed76ae ZJS |
4292 | LIST_FOREACH(conditions, c, i->conditions) |
4293 | if (c->tristate < 0) | |
4294 | printf(" %s %s=%s%s%s was not met\n", | |
9a6f746f | 4295 | --n ? special_glyph(SPECIAL_GLYPH_TREE_BRANCH) : special_glyph(SPECIAL_GLYPH_TREE_RIGHT), |
b1ed76ae ZJS |
4296 | c->name, |
4297 | c->trigger ? "|" : "", | |
4298 | c->negate ? "!" : "", | |
4299 | c->param); | |
59fccdc5 LP |
4300 | } |
4301 | ||
4302 | if (!i->assert_result && i->assert_timestamp > 0) { | |
4303 | s1 = format_timestamp_relative(since1, sizeof(since1), i->assert_timestamp); | |
4304 | s2 = format_timestamp(since2, sizeof(since2), i->assert_timestamp); | |
4305 | ||
4306 | printf(" Assert: start %sassertion failed%s at %s%s%s\n", | |
1fc464f6 | 4307 | ansi_highlight_red(), ansi_normal(), |
5cfee414 | 4308 | s2, s1 ? "; " : "", strempty(s1)); |
59fccdc5 LP |
4309 | if (i->failed_assert_trigger) |
4310 | printf(" none of the trigger assertions were met\n"); | |
4311 | else if (i->failed_assert) | |
4312 | printf(" %s=%s%s was not met\n", | |
4313 | i->failed_assert, | |
4314 | i->failed_assert_negate ? "!" : "", | |
4315 | i->failed_assert_parameter); | |
90bbc946 LP |
4316 | } |
4317 | ||
61cbdc4b | 4318 | if (i->sysfs_path) |
856323c9 | 4319 | printf(" Device: %s\n", i->sysfs_path); |
9feeba4b | 4320 | if (i->where) |
856323c9 | 4321 | printf(" Where: %s\n", i->where); |
9feeba4b | 4322 | if (i->what) |
856323c9 | 4323 | printf(" What: %s\n", i->what); |
49dbfa7b | 4324 | |
78c7d20e LP |
4325 | STRV_FOREACH(t, i->documentation) { |
4326 | _cleanup_free_ char *formatted = NULL; | |
4327 | const char *q; | |
4328 | ||
4329 | if (terminal_urlify(*t, NULL, &formatted) >= 0) | |
4330 | q = formatted; | |
4331 | else | |
4332 | q = *t; | |
4333 | ||
4334 | printf(" %*s %s\n", 9, t == i->documentation ? "Docs:" : "", q); | |
4335 | } | |
49dbfa7b | 4336 | |
13160134 | 4337 | STRV_FOREACH_PAIR(t, t2, i->listen) |
856323c9 | 4338 | printf(" %*s %s (%s)\n", 9, t == i->listen ? "Listen:" : "", *t2, *t); |
67419600 | 4339 | |
a98f7575 MM |
4340 | if (i->accept) { |
4341 | printf(" Accepted: %u; Connected: %u;", i->n_accepted, i->n_connections); | |
4342 | if (i->n_refused) | |
4343 | printf(" Refused: %u", i->n_refused); | |
4344 | printf("\n"); | |
4345 | } | |
4346 | ||
582a507f | 4347 | LIST_FOREACH(exec, p, i->exec) { |
13160134 | 4348 | _cleanup_free_ char *argv = NULL; |
9a57c629 | 4349 | bool good; |
582a507f LP |
4350 | |
4351 | /* Only show exited processes here */ | |
4352 | if (p->code == 0) | |
4353 | continue; | |
4354 | ||
13160134 | 4355 | argv = strv_join(p->argv, " "); |
1fa2f38f | 4356 | printf(" Process: "PID_FMT" %s=%s ", p->pid, p->name, strna(argv)); |
582a507f | 4357 | |
1f0958f6 | 4358 | good = is_clean_exit(p->code, p->status, EXIT_CLEAN_DAEMON, NULL); |
9a57c629 | 4359 | if (!good) { |
0b5a519c | 4360 | on = ansi_highlight_red(); |
1fc464f6 | 4361 | off = ansi_normal(); |
9a57c629 LP |
4362 | } else |
4363 | on = off = ""; | |
4364 | ||
4365 | printf("%s(code=%s, ", on, sigchld_code_to_string(p->code)); | |
4366 | ||
d06dacd0 LP |
4367 | if (p->code == CLD_EXITED) { |
4368 | const char *c; | |
4369 | ||
582a507f | 4370 | printf("status=%i", p->status); |
d06dacd0 | 4371 | |
1b64d026 LP |
4372 | c = exit_status_to_string(p->status, EXIT_STATUS_SYSTEMD); |
4373 | if (c) | |
d06dacd0 LP |
4374 | printf("/%s", c); |
4375 | ||
4376 | } else | |
582a507f | 4377 | printf("signal=%s", signal_to_string(p->status)); |
9a57c629 LP |
4378 | |
4379 | printf(")%s\n", off); | |
4380 | ||
582a507f LP |
4381 | if (i->main_pid == p->pid && |
4382 | i->start_timestamp == p->start_timestamp && | |
4383 | i->exit_timestamp == p->start_timestamp) | |
4384 | /* Let's not show this twice */ | |
4385 | i->main_pid = 0; | |
4386 | ||
4387 | if (p->pid == i->control_pid) | |
4388 | i->control_pid = 0; | |
4389 | } | |
4390 | ||
61cbdc4b | 4391 | if (i->main_pid > 0 || i->control_pid > 0) { |
61cbdc4b | 4392 | if (i->main_pid > 0) { |
8c06592f | 4393 | printf(" Main PID: "PID_FMT, i->main_pid); |
61cbdc4b LP |
4394 | |
4395 | if (i->running) { | |
a081b9ce LP |
4396 | |
4397 | if (arg_transport == BUS_TRANSPORT_LOCAL) { | |
4398 | _cleanup_free_ char *comm = NULL; | |
4399 | ||
4400 | (void) get_process_comm(i->main_pid, &comm); | |
4401 | if (comm) | |
4402 | printf(" (%s)", comm); | |
4403 | } | |
4404 | ||
6d4fc029 | 4405 | } else if (i->exit_code > 0) { |
61cbdc4b LP |
4406 | printf(" (code=%s, ", sigchld_code_to_string(i->exit_code)); |
4407 | ||
d06dacd0 LP |
4408 | if (i->exit_code == CLD_EXITED) { |
4409 | const char *c; | |
4410 | ||
61cbdc4b | 4411 | printf("status=%i", i->exit_status); |
d06dacd0 | 4412 | |
1b64d026 LP |
4413 | c = exit_status_to_string(i->exit_status, EXIT_STATUS_SYSTEMD); |
4414 | if (c) | |
d06dacd0 LP |
4415 | printf("/%s", c); |
4416 | ||
4417 | } else | |
582a507f | 4418 | printf("signal=%s", signal_to_string(i->exit_status)); |
6d4fc029 LP |
4419 | printf(")"); |
4420 | } | |
13160134 | 4421 | } |
61cbdc4b LP |
4422 | |
4423 | if (i->control_pid > 0) { | |
13160134 | 4424 | _cleanup_free_ char *c = NULL; |
61cbdc4b | 4425 | |
1089dcd4 LP |
4426 | if (i->main_pid > 0) |
4427 | fputs("; Control PID: ", stdout); | |
4428 | else | |
4429 | fputs("Cntrl PID: ", stdout); /* if first in column, abbreviated so it fits alignment */ | |
4430 | ||
4431 | printf(PID_FMT, i->control_pid); | |
61cbdc4b | 4432 | |
a081b9ce LP |
4433 | if (arg_transport == BUS_TRANSPORT_LOCAL) { |
4434 | (void) get_process_comm(i->control_pid, &c); | |
4435 | if (c) | |
4436 | printf(" (%s)", c); | |
4437 | } | |
61cbdc4b LP |
4438 | } |
4439 | ||
4440 | printf("\n"); | |
4441 | } | |
4442 | ||
17bb7382 | 4443 | if (i->status_text) |
856323c9 | 4444 | printf(" Status: \"%s\"\n", i->status_text); |
b4af5a80 | 4445 | if (i->status_errno > 0) |
4bbccb02 | 4446 | printf(" Error: %i (%s)\n", i->status_errno, strerror_safe(i->status_errno)); |
17bb7382 | 4447 | |
0e97c93f DM |
4448 | if (i->ip_ingress_bytes != (uint64_t) -1 && i->ip_egress_bytes != (uint64_t) -1) { |
4449 | char buf_in[FORMAT_BYTES_MAX], buf_out[FORMAT_BYTES_MAX]; | |
4450 | ||
4451 | printf(" IP: %s in, %s out\n", | |
4452 | format_bytes(buf_in, sizeof(buf_in), i->ip_ingress_bytes), | |
4453 | format_bytes(buf_out, sizeof(buf_out), i->ip_egress_bytes)); | |
4454 | } | |
4455 | ||
7a5de3f9 LP |
4456 | if (i->io_read_bytes != UINT64_MAX && i->io_write_bytes != UINT64_MAX) { |
4457 | char buf_in[FORMAT_BYTES_MAX], buf_out[FORMAT_BYTES_MAX]; | |
4458 | ||
4459 | printf(" IO: %s read, %s written\n", | |
4460 | format_bytes(buf_in, sizeof(buf_in), i->io_read_bytes), | |
4461 | format_bytes(buf_out, sizeof(buf_out), i->io_write_bytes)); | |
4462 | } | |
4463 | ||
03a7b521 LP |
4464 | if (i->tasks_current != (uint64_t) -1) { |
4465 | printf(" Tasks: %" PRIu64, i->tasks_current); | |
4466 | ||
4467 | if (i->tasks_max != (uint64_t) -1) | |
2a6736dd | 4468 | printf(" (limit: %" PRIu64 ")\n", i->tasks_max); |
03a7b521 LP |
4469 | else |
4470 | printf("\n"); | |
4471 | } | |
4472 | ||
934277fe LP |
4473 | if (i->memory_current != (uint64_t) -1) { |
4474 | char buf[FORMAT_BYTES_MAX]; | |
4475 | ||
4476 | printf(" Memory: %s", format_bytes(buf, sizeof(buf), i->memory_current)); | |
4477 | ||
48422635 TH |
4478 | if (i->memory_min > 0 || i->memory_low > 0 || |
4479 | i->memory_high != CGROUP_LIMIT_MAX || i->memory_max != CGROUP_LIMIT_MAX || | |
4480 | i->memory_swap_max != CGROUP_LIMIT_MAX || | |
da4d897e TH |
4481 | i->memory_limit != CGROUP_LIMIT_MAX) { |
4482 | const char *prefix = ""; | |
4483 | ||
4484 | printf(" ("); | |
48422635 TH |
4485 | if (i->memory_min > 0) { |
4486 | printf("%smin: %s", prefix, format_bytes(buf, sizeof(buf), i->memory_min)); | |
4487 | prefix = " "; | |
4488 | } | |
da4d897e TH |
4489 | if (i->memory_low > 0) { |
4490 | printf("%slow: %s", prefix, format_bytes(buf, sizeof(buf), i->memory_low)); | |
4491 | prefix = " "; | |
4492 | } | |
4493 | if (i->memory_high != CGROUP_LIMIT_MAX) { | |
4494 | printf("%shigh: %s", prefix, format_bytes(buf, sizeof(buf), i->memory_high)); | |
4495 | prefix = " "; | |
4496 | } | |
4497 | if (i->memory_max != CGROUP_LIMIT_MAX) { | |
4498 | printf("%smax: %s", prefix, format_bytes(buf, sizeof(buf), i->memory_max)); | |
4499 | prefix = " "; | |
4500 | } | |
96e131ea WC |
4501 | if (i->memory_swap_max != CGROUP_LIMIT_MAX) { |
4502 | printf("%sswap max: %s", prefix, format_bytes(buf, sizeof(buf), i->memory_swap_max)); | |
4503 | prefix = " "; | |
4504 | } | |
da4d897e TH |
4505 | if (i->memory_limit != CGROUP_LIMIT_MAX) { |
4506 | printf("%slimit: %s", prefix, format_bytes(buf, sizeof(buf), i->memory_limit)); | |
4507 | prefix = " "; | |
4508 | } | |
4509 | printf(")"); | |
4510 | } | |
4511 | printf("\n"); | |
934277fe LP |
4512 | } |
4513 | ||
5ad096b3 LP |
4514 | if (i->cpu_usage_nsec != (uint64_t) -1) { |
4515 | char buf[FORMAT_TIMESPAN_MAX]; | |
4516 | printf(" CPU: %s\n", format_timespan(buf, sizeof(buf), i->cpu_usage_nsec / NSEC_PER_USEC, USEC_PER_MSEC)); | |
4517 | } | |
4518 | ||
51e22f88 | 4519 | if (i->control_group) { |
291d565a LP |
4520 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
4521 | static const char prefix[] = " "; | |
ab35fb1b LP |
4522 | unsigned c; |
4523 | ||
51e22f88 LP |
4524 | printf(" CGroup: %s\n", i->control_group); |
4525 | ||
291d565a LP |
4526 | c = columns(); |
4527 | if (c > sizeof(prefix) - 1) | |
4528 | c -= sizeof(prefix) - 1; | |
4529 | else | |
4530 | c = 0; | |
ab35fb1b | 4531 | |
291d565a LP |
4532 | r = unit_show_processes(bus, i->id, i->control_group, prefix, c, get_output_flags(), &error); |
4533 | if (r == -EBADR) { | |
b69d29ce LP |
4534 | unsigned k = 0; |
4535 | pid_t extra[2]; | |
4536 | ||
291d565a | 4537 | /* Fallback for older systemd versions where the GetUnitProcesses() call is not yet available */ |
ab35fb1b | 4538 | |
b69d29ce LP |
4539 | if (i->main_pid > 0) |
4540 | extra[k++] = i->main_pid; | |
4541 | ||
4542 | if (i->control_pid > 0) | |
4543 | extra[k++] = i->control_pid; | |
4544 | ||
0ff308c8 | 4545 | show_cgroup_and_extra(SYSTEMD_CGROUP_CONTROLLER, i->control_group, prefix, c, extra, k, get_output_flags()); |
291d565a | 4546 | } else if (r < 0) |
950d6889 | 4547 | log_warning_errno(r, "Failed to dump process list for '%s', ignoring: %s", i->id, bus_error_message(&error, r)); |
c59760ee | 4548 | } |
45fb0699 | 4549 | |
ece174c5 | 4550 | if (i->id && arg_transport == BUS_TRANSPORT_LOCAL) |
3c756001 LP |
4551 | show_journal_by_unit( |
4552 | stdout, | |
4553 | i->id, | |
4554 | arg_output, | |
4555 | 0, | |
4556 | i->inactive_exit_timestamp_monotonic, | |
4557 | arg_lines, | |
4558 | getuid(), | |
4559 | get_output_flags() | OUTPUT_BEGIN_NEWLINE, | |
4560 | SD_JOURNAL_LOCAL_ONLY, | |
4561 | arg_scope == UNIT_FILE_SYSTEM, | |
4562 | ellipsized); | |
86aa7ba4 | 4563 | |
45fb0699 | 4564 | if (i->need_daemon_reload) |
3f36991e | 4565 | warn_unit_file_changed(i->id); |
61cbdc4b LP |
4566 | } |
4567 | ||
b43f208f | 4568 | static void show_unit_help(UnitStatusInfo *i) { |
256425cc LP |
4569 | char **p; |
4570 | ||
4571 | assert(i); | |
4572 | ||
4573 | if (!i->documentation) { | |
4574 | log_info("Documentation for %s not known.", i->id); | |
4575 | return; | |
4576 | } | |
4577 | ||
78002a67 ZJS |
4578 | STRV_FOREACH(p, i->documentation) |
4579 | if (startswith(*p, "man:")) | |
4580 | show_man_page(*p + 4, false); | |
4581 | else | |
0315fe37 | 4582 | log_info("Can't show: %s", *p); |
256425cc LP |
4583 | } |
4584 | ||
4679a8c3 YW |
4585 | static int map_main_pid(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { |
4586 | UnitStatusInfo *i = userdata; | |
4587 | uint32_t u; | |
f459b602 | 4588 | int r; |
61cbdc4b | 4589 | |
4679a8c3 YW |
4590 | r = sd_bus_message_read(m, "u", &u); |
4591 | if (r < 0) | |
4592 | return r; | |
61cbdc4b | 4593 | |
4679a8c3 YW |
4594 | i->main_pid = (pid_t) u; |
4595 | i->running = u > 0; | |
b8131a87 | 4596 | |
4679a8c3 YW |
4597 | return 0; |
4598 | } | |
b8131a87 | 4599 | |
4679a8c3 | 4600 | static int map_load_error(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { |
31d99bd1 | 4601 | const char *message, **p = userdata; |
4679a8c3 | 4602 | int r; |
b8131a87 | 4603 | |
31d99bd1 | 4604 | r = sd_bus_message_read(m, "(ss)", NULL, &message); |
4679a8c3 YW |
4605 | if (r < 0) |
4606 | return r; | |
b8131a87 | 4607 | |
4679a8c3 YW |
4608 | if (!isempty(message)) |
4609 | *p = message; | |
61cbdc4b | 4610 | |
4679a8c3 YW |
4611 | return 0; |
4612 | } | |
61cbdc4b | 4613 | |
4679a8c3 YW |
4614 | static int map_listen(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { |
4615 | const char *type, *path; | |
4616 | char ***p = userdata; | |
4617 | int r; | |
61cbdc4b | 4618 | |
4679a8c3 YW |
4619 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(ss)"); |
4620 | if (r < 0) | |
4621 | return r; | |
61cbdc4b | 4622 | |
4679a8c3 | 4623 | while ((r = sd_bus_message_read(m, "(ss)", &type, &path)) > 0) { |
61cbdc4b | 4624 | |
4679a8c3 | 4625 | r = strv_extend(p, type); |
f459b602 | 4626 | if (r < 0) |
4679a8c3 | 4627 | return r; |
61cbdc4b | 4628 | |
4679a8c3 | 4629 | r = strv_extend(p, path); |
f459b602 | 4630 | if (r < 0) |
4679a8c3 | 4631 | return r; |
61cbdc4b | 4632 | } |
4679a8c3 YW |
4633 | if (r < 0) |
4634 | return r; | |
582a507f | 4635 | |
4679a8c3 YW |
4636 | r = sd_bus_message_exit_container(m); |
4637 | if (r < 0) | |
4638 | return r; | |
49dbfa7b | 4639 | |
4679a8c3 YW |
4640 | return 0; |
4641 | } | |
52990c2e | 4642 | |
4679a8c3 YW |
4643 | static int map_conditions(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { |
4644 | UnitStatusInfo *i = userdata; | |
4645 | const char *cond, *param; | |
4646 | int trigger, negate; | |
4647 | int32_t state; | |
4648 | int r; | |
52990c2e | 4649 | |
4679a8c3 YW |
4650 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(sbbsi)"); |
4651 | if (r < 0) | |
4652 | return r; | |
f459b602 | 4653 | |
4679a8c3 YW |
4654 | while ((r = sd_bus_message_read(m, "(sbbsi)", &cond, &trigger, &negate, ¶m, &state)) > 0) { |
4655 | _cleanup_(unit_condition_freep) UnitCondition *c = NULL; | |
d5db7fe6 | 4656 | |
3bfa8668 | 4657 | c = new(UnitCondition, 1); |
4679a8c3 YW |
4658 | if (!c) |
4659 | return -ENOMEM; | |
d5db7fe6 | 4660 | |
3bfa8668 LP |
4661 | *c = (UnitCondition) { |
4662 | .name = strdup(cond), | |
4663 | .param = strdup(param), | |
4664 | .trigger = trigger, | |
4665 | .negate = negate, | |
4666 | .tristate = state, | |
4667 | }; | |
4668 | ||
4679a8c3 YW |
4669 | if (!c->name || !c->param) |
4670 | return -ENOMEM; | |
d5db7fe6 | 4671 | |
d5db7fe6 | 4672 | |
3bfa8668 | 4673 | LIST_PREPEND(conditions, i->conditions, TAKE_PTR(c)); |
4679a8c3 YW |
4674 | } |
4675 | if (r < 0) | |
4676 | return r; | |
d5db7fe6 | 4677 | |
4679a8c3 YW |
4678 | r = sd_bus_message_exit_container(m); |
4679 | if (r < 0) | |
4680 | return r; | |
59fccdc5 | 4681 | |
4679a8c3 YW |
4682 | return 0; |
4683 | } | |
59fccdc5 | 4684 | |
4679a8c3 YW |
4685 | static int map_asserts(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { |
4686 | UnitStatusInfo *i = userdata; | |
4687 | const char *cond, *param; | |
4688 | int trigger, negate; | |
4689 | int32_t state; | |
4690 | int r; | |
59fccdc5 | 4691 | |
4679a8c3 YW |
4692 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(sbbsi)"); |
4693 | if (r < 0) | |
4694 | return r; | |
59fccdc5 | 4695 | |
4679a8c3 YW |
4696 | while ((r = sd_bus_message_read(m, "(sbbsi)", &cond, &trigger, &negate, ¶m, &state)) > 0) { |
4697 | if (state < 0 && (!trigger || !i->failed_assert)) { | |
4698 | i->failed_assert = cond; | |
4699 | i->failed_assert_trigger = trigger; | |
4700 | i->failed_assert_negate = negate; | |
4701 | i->failed_assert_parameter = param; | |
4702 | } | |
4703 | } | |
4704 | if (r < 0) | |
4705 | return r; | |
f459b602 | 4706 | |
4679a8c3 YW |
4707 | r = sd_bus_message_exit_container(m); |
4708 | if (r < 0) | |
4709 | return r; | |
f459b602 | 4710 | |
4679a8c3 YW |
4711 | return 0; |
4712 | } | |
582a507f | 4713 | |
4679a8c3 YW |
4714 | static int map_exec(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { |
4715 | _cleanup_free_ ExecStatusInfo *info = NULL; | |
3e14d36a | 4716 | ExecStatusInfo *last; |
4679a8c3 | 4717 | UnitStatusInfo *i = userdata; |
b3d59367 | 4718 | bool is_ex_prop = endswith(member, "Ex"); |
4679a8c3 | 4719 | int r; |
9f39404c | 4720 | |
b3d59367 | 4721 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, is_ex_prop ? "(sasasttttuii)" : "(sasbttttuii)"); |
4679a8c3 YW |
4722 | if (r < 0) |
4723 | return r; | |
9f39404c | 4724 | |
4679a8c3 YW |
4725 | info = new0(ExecStatusInfo, 1); |
4726 | if (!info) | |
4727 | return -ENOMEM; | |
9f39404c | 4728 | |
3e14d36a LP |
4729 | LIST_FIND_TAIL(exec, i->exec, last); |
4730 | ||
b3d59367 | 4731 | while ((r = exec_status_info_deserialize(m, info, is_ex_prop)) > 0) { |
9f39404c | 4732 | |
4679a8c3 YW |
4733 | info->name = strdup(member); |
4734 | if (!info->name) | |
4735 | return -ENOMEM; | |
9f39404c | 4736 | |
3e14d36a LP |
4737 | LIST_INSERT_AFTER(exec, i->exec, last, info); |
4738 | last = info; | |
f459b602 | 4739 | |
4679a8c3 YW |
4740 | info = new0(ExecStatusInfo, 1); |
4741 | if (!info) | |
4742 | return -ENOMEM; | |
9f39404c | 4743 | } |
4679a8c3 YW |
4744 | if (r < 0) |
4745 | return r; | |
f459b602 | 4746 | |
4679a8c3 | 4747 | r = sd_bus_message_exit_container(m); |
f459b602 | 4748 | if (r < 0) |
4679a8c3 | 4749 | return r; |
61cbdc4b LP |
4750 | |
4751 | return 0; | |
4752 | } | |
4753 | ||
eda19357 | 4754 | static int print_property(const char *name, const char *expected_value, sd_bus_message *m, bool value, bool all) { |
07636114 YW |
4755 | char bus_type; |
4756 | const char *contents; | |
f459b602 MAP |
4757 | int r; |
4758 | ||
48220598 | 4759 | assert(name); |
f459b602 | 4760 | assert(m); |
48220598 | 4761 | |
61cbdc4b LP |
4762 | /* This is a low-level property printer, see |
4763 | * print_status_info() for the nicer output */ | |
4764 | ||
07636114 YW |
4765 | r = sd_bus_message_peek_type(m, &bus_type, &contents); |
4766 | if (r < 0) | |
852c1b4d | 4767 | return r; |
48220598 | 4768 | |
07636114 | 4769 | switch (bus_type) { |
48220598 | 4770 | |
a3ddd457 YW |
4771 | case SD_BUS_TYPE_INT32: |
4772 | if (endswith(name, "ActionExitStatus")) { | |
4773 | int32_t i; | |
4774 | ||
4775 | r = sd_bus_message_read_basic(m, bus_type, &i); | |
4776 | if (r < 0) | |
4777 | return r; | |
4778 | ||
4779 | if (i >= 0 && i <= 255) | |
4780 | bus_print_property_valuef(name, expected_value, value, "%"PRIi32, i); | |
4781 | else if (all) | |
4782 | bus_print_property_value(name, expected_value, value, "[not set]"); | |
4783 | ||
b070c7c0 MS |
4784 | return 1; |
4785 | } else if (streq(name, "NUMAPolicy")) { | |
4786 | int32_t i; | |
4787 | ||
4788 | r = sd_bus_message_read_basic(m, bus_type, &i); | |
4789 | if (r < 0) | |
4790 | return r; | |
4791 | ||
4792 | bus_print_property_valuef(name, expected_value, value, "%s", strna(mpol_to_string(i))); | |
4793 | ||
a3ddd457 YW |
4794 | return 1; |
4795 | } | |
4796 | break; | |
4797 | ||
07636114 | 4798 | case SD_BUS_TYPE_STRUCT: |
48220598 | 4799 | |
07636114 | 4800 | if (contents[0] == SD_BUS_TYPE_UINT32 && streq(name, "Job")) { |
48220598 LP |
4801 | uint32_t u; |
4802 | ||
f459b602 MAP |
4803 | r = sd_bus_message_read(m, "(uo)", &u, NULL); |
4804 | if (r < 0) | |
4805 | return bus_log_parse_error(r); | |
48220598 | 4806 | |
f459b602 | 4807 | if (u > 0) |
102b0214 | 4808 | bus_print_property_valuef(name, expected_value, value, "%"PRIu32, u); |
07636114 | 4809 | else if (all) |
102b0214 | 4810 | bus_print_property_value(name, expected_value, value, ""); |
48220598 | 4811 | |
07636114 | 4812 | return 1; |
f459b602 | 4813 | |
07636114 | 4814 | } else if (contents[0] == SD_BUS_TYPE_STRING && streq(name, "Unit")) { |
48220598 LP |
4815 | const char *s; |
4816 | ||
f459b602 MAP |
4817 | r = sd_bus_message_read(m, "(so)", &s, NULL); |
4818 | if (r < 0) | |
4819 | return bus_log_parse_error(r); | |
48220598 | 4820 | |
07636114 | 4821 | if (all || !isempty(s)) |
102b0214 | 4822 | bus_print_property_value(name, expected_value, value, s); |
48220598 | 4823 | |
07636114 | 4824 | return 1; |
f459b602 | 4825 | |
07636114 | 4826 | } else if (contents[0] == SD_BUS_TYPE_STRING && streq(name, "LoadError")) { |
9f39404c LP |
4827 | const char *a = NULL, *b = NULL; |
4828 | ||
f459b602 MAP |
4829 | r = sd_bus_message_read(m, "(ss)", &a, &b); |
4830 | if (r < 0) | |
4831 | return bus_log_parse_error(r); | |
9f39404c | 4832 | |
48d3358d | 4833 | if (!isempty(a) || !isempty(b)) |
102b0214 | 4834 | bus_print_property_valuef(name, expected_value, value, "%s \"%s\"", strempty(a), strempty(b)); |
48d3358d YW |
4835 | else if (all) |
4836 | bus_print_property_value(name, expected_value, value, ""); | |
f786e80d | 4837 | |
07636114 | 4838 | return 1; |
04749e40 YW |
4839 | |
4840 | } else if (STR_IN_SET(name, "SystemCallFilter", "RestrictAddressFamilies")) { | |
57183d11 LP |
4841 | _cleanup_strv_free_ char **l = NULL; |
4842 | int whitelist; | |
4843 | ||
4844 | r = sd_bus_message_enter_container(m, 'r', "bas"); | |
4845 | if (r < 0) | |
4846 | return bus_log_parse_error(r); | |
4847 | ||
4848 | r = sd_bus_message_read(m, "b", &whitelist); | |
4849 | if (r < 0) | |
4850 | return bus_log_parse_error(r); | |
4851 | ||
4852 | r = sd_bus_message_read_strv(m, &l); | |
4853 | if (r < 0) | |
4854 | return bus_log_parse_error(r); | |
4855 | ||
4856 | r = sd_bus_message_exit_container(m); | |
4857 | if (r < 0) | |
4858 | return bus_log_parse_error(r); | |
4859 | ||
07636114 | 4860 | if (all || whitelist || !strv_isempty(l)) { |
57183d11 LP |
4861 | bool first = true; |
4862 | char **i; | |
4863 | ||
07636114 | 4864 | if (!value) { |
4f9a9105 ZJS |
4865 | fputs(name, stdout); |
4866 | fputc('=', stdout); | |
4867 | } | |
57183d11 LP |
4868 | |
4869 | if (!whitelist) | |
4870 | fputc('~', stdout); | |
4871 | ||
4872 | STRV_FOREACH(i, l) { | |
4873 | if (first) | |
4874 | first = false; | |
4875 | else | |
4876 | fputc(' ', stdout); | |
4877 | ||
4878 | fputs(*i, stdout); | |
4879 | } | |
4880 | fputc('\n', stdout); | |
4881 | } | |
4882 | ||
c7bb2fec | 4883 | return 1; |
102b0214 | 4884 | |
c7bb2fec IP |
4885 | } else if (STR_IN_SET(name, "SELinuxContext", "AppArmorProfile", "SmackProcessLabel")) { |
4886 | int ignore; | |
4887 | const char *s; | |
4888 | ||
4889 | r = sd_bus_message_read(m, "(bs)", &ignore, &s); | |
4890 | if (r < 0) | |
4891 | return bus_log_parse_error(r); | |
4892 | ||
4893 | if (!isempty(s)) | |
102b0214 | 4894 | bus_print_property_valuef(name, expected_value, value, "%s%s", ignore ? "-" : "", s); |
c7bb2fec | 4895 | else if (all) |
102b0214 | 4896 | bus_print_property_value(name, expected_value, value, ""); |
c7bb2fec | 4897 | |
07636114 | 4898 | return 1; |
4a220712 YW |
4899 | |
4900 | } else if (endswith(name, "ExitStatus") && streq(contents, "aiai")) { | |
4901 | const int32_t *status, *signal; | |
4902 | size_t sz_status, sz_signal, i; | |
4903 | ||
4904 | r = sd_bus_message_enter_container(m, 'r', "aiai"); | |
4905 | if (r < 0) | |
4906 | return bus_log_parse_error(r); | |
4907 | ||
4908 | r = sd_bus_message_read_array(m, 'i', (const void **) &status, &sz_status); | |
4909 | if (r < 0) | |
4910 | return bus_log_parse_error(r); | |
4911 | ||
4912 | r = sd_bus_message_read_array(m, 'i', (const void **) &signal, &sz_signal); | |
4913 | if (r < 0) | |
4914 | return bus_log_parse_error(r); | |
4915 | ||
4916 | r = sd_bus_message_exit_container(m); | |
4917 | if (r < 0) | |
4918 | return bus_log_parse_error(r); | |
4919 | ||
4920 | sz_status /= sizeof(int32_t); | |
4921 | sz_signal /= sizeof(int32_t); | |
4922 | ||
4923 | if (all || sz_status > 0 || sz_signal > 0) { | |
4924 | bool first = true; | |
4925 | ||
4926 | if (!value) { | |
4927 | fputs(name, stdout); | |
4928 | fputc('=', stdout); | |
4929 | } | |
4930 | ||
4931 | for (i = 0; i < sz_status; i++) { | |
4932 | if (status[i] < 0 || status[i] > 255) | |
4933 | continue; | |
4934 | ||
4935 | if (first) | |
4936 | first = false; | |
4937 | else | |
4938 | fputc(' ', stdout); | |
4939 | ||
4940 | printf("%"PRIi32, status[i]); | |
4941 | } | |
4942 | ||
4943 | for (i = 0; i < sz_signal; i++) { | |
4944 | const char *str; | |
4945 | ||
4946 | str = signal_to_string((int) signal[i]); | |
4947 | if (!str) | |
4948 | continue; | |
4949 | ||
4950 | if (first) | |
4951 | first = false; | |
4952 | else | |
4953 | fputc(' ', stdout); | |
4954 | ||
4955 | fputs(str, stdout); | |
4956 | } | |
4957 | ||
4958 | fputc('\n', stdout); | |
4959 | } | |
4960 | return 1; | |
48220598 LP |
4961 | } |
4962 | ||
4963 | break; | |
48220598 | 4964 | |
f459b602 | 4965 | case SD_BUS_TYPE_ARRAY: |
48220598 | 4966 | |
07636114 | 4967 | if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "EnvironmentFiles")) { |
f459b602 MAP |
4968 | const char *path; |
4969 | int ignore; | |
8c7be95e | 4970 | |
f459b602 MAP |
4971 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(sb)"); |
4972 | if (r < 0) | |
4973 | return bus_log_parse_error(r); | |
8c7be95e | 4974 | |
f459b602 | 4975 | while ((r = sd_bus_message_read(m, "(sb)", &path, &ignore)) > 0) |
102b0214 | 4976 | bus_print_property_valuef(name, expected_value, value, "%s (ignore_errors=%s)", path, yes_no(ignore)); |
8c7be95e | 4977 | |
f459b602 MAP |
4978 | if (r < 0) |
4979 | return bus_log_parse_error(r); | |
8c7be95e | 4980 | |
f459b602 MAP |
4981 | r = sd_bus_message_exit_container(m); |
4982 | if (r < 0) | |
4983 | return bus_log_parse_error(r); | |
8c7be95e | 4984 | |
07636114 | 4985 | return 1; |
8c7be95e | 4986 | |
07636114 | 4987 | } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "Paths")) { |
f459b602 | 4988 | const char *type, *path; |
67419600 | 4989 | |
f459b602 MAP |
4990 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(ss)"); |
4991 | if (r < 0) | |
4992 | return bus_log_parse_error(r); | |
ebf57b80 | 4993 | |
f459b602 | 4994 | while ((r = sd_bus_message_read(m, "(ss)", &type, &path)) > 0) |
102b0214 | 4995 | bus_print_property_valuef(name, expected_value, value, "%s (%s)", path, type); |
f459b602 MAP |
4996 | if (r < 0) |
4997 | return bus_log_parse_error(r); | |
ebf57b80 | 4998 | |
f459b602 MAP |
4999 | r = sd_bus_message_exit_container(m); |
5000 | if (r < 0) | |
5001 | return bus_log_parse_error(r); | |
ebf57b80 | 5002 | |
07636114 | 5003 | return 1; |
582a507f | 5004 | |
07636114 | 5005 | } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "Listen")) { |
f459b602 | 5006 | const char *type, *path; |
67419600 | 5007 | |
f459b602 MAP |
5008 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(ss)"); |
5009 | if (r < 0) | |
5010 | return bus_log_parse_error(r); | |
67419600 | 5011 | |
f459b602 | 5012 | while ((r = sd_bus_message_read(m, "(ss)", &type, &path)) > 0) |
102b0214 | 5013 | bus_print_property_valuef(name, expected_value, value, "%s (%s)", path, type); |
f459b602 MAP |
5014 | if (r < 0) |
5015 | return bus_log_parse_error(r); | |
67419600 | 5016 | |
f459b602 MAP |
5017 | r = sd_bus_message_exit_container(m); |
5018 | if (r < 0) | |
5019 | return bus_log_parse_error(r); | |
67419600 | 5020 | |
07636114 | 5021 | return 1; |
67419600 | 5022 | |
07636114 | 5023 | } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "TimersMonotonic")) { |
f459b602 | 5024 | const char *base; |
07636114 | 5025 | uint64_t v, next_elapse; |
707e5e52 | 5026 | |
f459b602 MAP |
5027 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(stt)"); |
5028 | if (r < 0) | |
5029 | return bus_log_parse_error(r); | |
552e4331 | 5030 | |
07636114 | 5031 | while ((r = sd_bus_message_read(m, "(stt)", &base, &v, &next_elapse)) > 0) { |
f459b602 | 5032 | char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX]; |
fe68089d | 5033 | |
102b0214 YW |
5034 | bus_print_property_valuef(name, expected_value, value, "{ %s=%s ; next_elapse=%s }", base, |
5035 | format_timespan(timespan1, sizeof(timespan1), v, 0), | |
5036 | format_timespan(timespan2, sizeof(timespan2), next_elapse, 0)); | |
fe68089d | 5037 | } |
f459b602 MAP |
5038 | if (r < 0) |
5039 | return bus_log_parse_error(r); | |
5040 | ||
5041 | r = sd_bus_message_exit_container(m); | |
5042 | if (r < 0) | |
5043 | return bus_log_parse_error(r); | |
fe68089d | 5044 | |
07636114 | 5045 | return 1; |
fe68089d | 5046 | |
07636114 | 5047 | } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "TimersCalendar")) { |
fa6dee52 YW |
5048 | const char *base, *spec; |
5049 | uint64_t next_elapse; | |
5050 | ||
5051 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(sst)"); | |
5052 | if (r < 0) | |
5053 | return bus_log_parse_error(r); | |
5054 | ||
5055 | while ((r = sd_bus_message_read(m, "(sst)", &base, &spec, &next_elapse)) > 0) { | |
5056 | char timestamp[FORMAT_TIMESTAMP_MAX]; | |
5057 | ||
102b0214 YW |
5058 | bus_print_property_valuef(name, expected_value, value, "{ %s=%s ; next_elapse=%s }", base, spec, |
5059 | format_timestamp(timestamp, sizeof(timestamp), next_elapse)); | |
fa6dee52 YW |
5060 | } |
5061 | if (r < 0) | |
5062 | return bus_log_parse_error(r); | |
5063 | ||
5064 | r = sd_bus_message_exit_container(m); | |
5065 | if (r < 0) | |
5066 | return bus_log_parse_error(r); | |
5067 | ||
07636114 | 5068 | return 1; |
fa6dee52 | 5069 | |
07636114 | 5070 | } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && startswith(name, "Exec")) { |
f459b602 | 5071 | ExecStatusInfo info = {}; |
b3d59367 | 5072 | bool is_ex_prop = endswith(name, "Ex"); |
f459b602 | 5073 | |
b3d59367 | 5074 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, is_ex_prop ? "(sasasttttuii)" : "(sasbttttuii)"); |
f459b602 MAP |
5075 | if (r < 0) |
5076 | return bus_log_parse_error(r); | |
5077 | ||
b3d59367 | 5078 | while ((r = exec_status_info_deserialize(m, &info, is_ex_prop)) > 0) { |
f459b602 | 5079 | char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX]; |
b3d59367 AZ |
5080 | _cleanup_strv_free_ char **optv = NULL; |
5081 | _cleanup_free_ char *tt, *o = NULL; | |
f459b602 MAP |
5082 | |
5083 | tt = strv_join(info.argv, " "); | |
5084 | ||
b3d59367 AZ |
5085 | if (is_ex_prop) { |
5086 | r = exec_command_flags_to_strv(info.flags, &optv); | |
5087 | if (r < 0) | |
5088 | return log_error_errno(r, "Failed to convert ExecCommandFlags to strv: %m"); | |
5089 | ||
5090 | o = strv_join(optv, " "); | |
5091 | ||
5092 | bus_print_property_valuef(name, expected_value, value, | |
5093 | "{ path=%s ; argv[]=%s ; flags=%s ; start_time=[%s] ; stop_time=[%s] ; pid="PID_FMT" ; code=%s ; status=%i%s%s }", | |
5094 | strna(info.path), | |
5095 | strna(tt), | |
5096 | strna(o), | |
5097 | strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)), | |
5098 | strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)), | |
5099 | info.pid, | |
5100 | sigchld_code_to_string(info.code), | |
5101 | info.status, | |
5102 | info.code == CLD_EXITED ? "" : "/", | |
5103 | strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status))); | |
5104 | } else | |
5105 | bus_print_property_valuef(name, expected_value, value, | |
5106 | "{ path=%s ; argv[]=%s ; ignore_errors=%s ; start_time=[%s] ; stop_time=[%s] ; pid="PID_FMT" ; code=%s ; status=%i%s%s }", | |
5107 | strna(info.path), | |
5108 | strna(tt), | |
5109 | yes_no(info.ignore), | |
5110 | strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)), | |
5111 | strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)), | |
5112 | info.pid, | |
5113 | sigchld_code_to_string(info.code), | |
5114 | info.status, | |
5115 | info.code == CLD_EXITED ? "" : "/", | |
5116 | strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status))); | |
fe68089d | 5117 | |
582a507f LP |
5118 | free(info.path); |
5119 | strv_free(info.argv); | |
f459b602 | 5120 | zero(info); |
707e5e52 LP |
5121 | } |
5122 | ||
f459b602 MAP |
5123 | r = sd_bus_message_exit_container(m); |
5124 | if (r < 0) | |
5125 | return bus_log_parse_error(r); | |
5126 | ||
07636114 | 5127 | return 1; |
4ad49000 | 5128 | |
07636114 | 5129 | } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "DeviceAllow")) { |
f459b602 | 5130 | const char *path, *rwm; |
4ad49000 | 5131 | |
f459b602 MAP |
5132 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(ss)"); |
5133 | if (r < 0) | |
5134 | return bus_log_parse_error(r); | |
4ad49000 | 5135 | |
f459b602 | 5136 | while ((r = sd_bus_message_read(m, "(ss)", &path, &rwm)) > 0) |
102b0214 | 5137 | bus_print_property_valuef(name, expected_value, value, "%s %s", strna(path), strna(rwm)); |
f459b602 MAP |
5138 | if (r < 0) |
5139 | return bus_log_parse_error(r); | |
4ad49000 | 5140 | |
f459b602 MAP |
5141 | r = sd_bus_message_exit_container(m); |
5142 | if (r < 0) | |
5143 | return bus_log_parse_error(r); | |
4ad49000 | 5144 | |
07636114 | 5145 | return 1; |
4ad49000 | 5146 | |
07636114 | 5147 | } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && |
72240b52 | 5148 | STR_IN_SET(name, "IODeviceWeight", "BlockIODeviceWeight")) { |
f459b602 MAP |
5149 | const char *path; |
5150 | uint64_t weight; | |
b8ab2dc6 | 5151 | |
f459b602 MAP |
5152 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(st)"); |
5153 | if (r < 0) | |
5154 | return bus_log_parse_error(r); | |
b8ab2dc6 | 5155 | |
f459b602 | 5156 | while ((r = sd_bus_message_read(m, "(st)", &path, &weight)) > 0) |
102b0214 | 5157 | bus_print_property_valuef(name, expected_value, value, "%s %"PRIu64, strna(path), weight); |
f459b602 MAP |
5158 | if (r < 0) |
5159 | return bus_log_parse_error(r); | |
b8ab2dc6 | 5160 | |
f459b602 MAP |
5161 | r = sd_bus_message_exit_container(m); |
5162 | if (r < 0) | |
5163 | return bus_log_parse_error(r); | |
b8ab2dc6 | 5164 | |
07636114 | 5165 | return 1; |
b8ab2dc6 | 5166 | |
07636114 | 5167 | } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && |
72240b52 ZJS |
5168 | (cgroup_io_limit_type_from_string(name) >= 0 || |
5169 | STR_IN_SET(name, "BlockIOReadBandwidth", "BlockIOWriteBandwidth"))) { | |
f459b602 MAP |
5170 | const char *path; |
5171 | uint64_t bandwidth; | |
4ad49000 | 5172 | |
f459b602 MAP |
5173 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(st)"); |
5174 | if (r < 0) | |
5175 | return bus_log_parse_error(r); | |
4ad49000 | 5176 | |
f459b602 | 5177 | while ((r = sd_bus_message_read(m, "(st)", &path, &bandwidth)) > 0) |
102b0214 | 5178 | bus_print_property_valuef(name, expected_value, value, "%s %"PRIu64, strna(path), bandwidth); |
f459b602 MAP |
5179 | if (r < 0) |
5180 | return bus_log_parse_error(r); | |
4ad49000 | 5181 | |
f459b602 MAP |
5182 | r = sd_bus_message_exit_container(m); |
5183 | if (r < 0) | |
5184 | return bus_log_parse_error(r); | |
4ad49000 | 5185 | |
07636114 | 5186 | return 1; |
6ae4283c TH |
5187 | |
5188 | } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && | |
5189 | streq(name, "IODeviceLatencyTargetUSec")) { | |
5190 | char ts[FORMAT_TIMESPAN_MAX]; | |
5191 | const char *path; | |
5192 | uint64_t target; | |
5193 | ||
5194 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(st)"); | |
5195 | if (r < 0) | |
5196 | return bus_log_parse_error(r); | |
5197 | ||
5198 | while ((r = sd_bus_message_read(m, "(st)", &path, &target)) > 0) | |
102b0214 YW |
5199 | bus_print_property_valuef(name, expected_value, value, "%s %s", strna(path), |
5200 | format_timespan(ts, sizeof(ts), target, 1)); | |
6ae4283c TH |
5201 | if (r < 0) |
5202 | return bus_log_parse_error(r); | |
5203 | ||
5204 | r = sd_bus_message_exit_container(m); | |
5205 | if (r < 0) | |
5206 | return bus_log_parse_error(r); | |
5207 | ||
5208 | return 1; | |
08f3be7a | 5209 | |
07636114 | 5210 | } else if (contents[0] == SD_BUS_TYPE_BYTE && streq(name, "StandardInputData")) { |
08f3be7a LP |
5211 | _cleanup_free_ char *h = NULL; |
5212 | const void *p; | |
5213 | size_t sz; | |
5214 | ssize_t n; | |
5215 | ||
5216 | r = sd_bus_message_read_array(m, 'y', &p, &sz); | |
5217 | if (r < 0) | |
5218 | return bus_log_parse_error(r); | |
5219 | ||
5220 | n = base64mem(p, sz, &h); | |
5221 | if (n < 0) | |
5222 | return log_oom(); | |
5223 | ||
102b0214 | 5224 | bus_print_property_value(name, expected_value, value, h); |
08f3be7a | 5225 | |
07636114 | 5226 | return 1; |
9c1f9465 YW |
5227 | |
5228 | } else if (STR_IN_SET(name, "IPAddressAllow", "IPAddressDeny")) { | |
5229 | _cleanup_free_ char *addresses = NULL; | |
5230 | ||
5231 | r = sd_bus_message_enter_container(m, 'a', "(iayu)"); | |
5232 | if (r < 0) | |
5233 | return bus_log_parse_error(r); | |
5234 | ||
5235 | for (;;) { | |
5236 | _cleanup_free_ char *str = NULL; | |
5237 | uint32_t prefixlen; | |
5238 | int32_t family; | |
5239 | const void *ap; | |
5240 | size_t an; | |
5241 | ||
5242 | r = sd_bus_message_enter_container(m, 'r', "iayu"); | |
5243 | if (r < 0) | |
5244 | return bus_log_parse_error(r); | |
5245 | if (r == 0) | |
5246 | break; | |
5247 | ||
5248 | r = sd_bus_message_read(m, "i", &family); | |
5249 | if (r < 0) | |
5250 | return bus_log_parse_error(r); | |
5251 | ||
5252 | r = sd_bus_message_read_array(m, 'y', &ap, &an); | |
5253 | if (r < 0) | |
5254 | return bus_log_parse_error(r); | |
5255 | ||
5256 | r = sd_bus_message_read(m, "u", &prefixlen); | |
5257 | if (r < 0) | |
5258 | return bus_log_parse_error(r); | |
5259 | ||
5260 | r = sd_bus_message_exit_container(m); | |
5261 | if (r < 0) | |
5262 | return bus_log_parse_error(r); | |
5263 | ||
5264 | if (!IN_SET(family, AF_INET, AF_INET6)) | |
5265 | continue; | |
5266 | ||
5267 | if (an != FAMILY_ADDRESS_SIZE(family)) | |
5268 | continue; | |
5269 | ||
5270 | if (prefixlen > FAMILY_ADDRESS_SIZE(family) * 8) | |
5271 | continue; | |
5272 | ||
5273 | if (in_addr_prefix_to_string(family, (union in_addr_union *) ap, prefixlen, &str) < 0) | |
5274 | continue; | |
5275 | ||
5276 | if (!strextend_with_separator(&addresses, " ", str, NULL)) | |
5277 | return log_oom(); | |
5278 | } | |
5279 | ||
5280 | r = sd_bus_message_exit_container(m); | |
5281 | if (r < 0) | |
5282 | return bus_log_parse_error(r); | |
5283 | ||
5284 | if (all || !isempty(addresses)) | |
5285 | bus_print_property_value(name, expected_value, value, strempty(addresses)); | |
5286 | ||
5287 | return 1; | |
4c787df7 YW |
5288 | |
5289 | } else if (STR_IN_SET(name, "BindPaths", "BindReadOnlyPaths")) { | |
5290 | _cleanup_free_ char *paths = NULL; | |
5291 | const char *source, *dest; | |
5292 | int ignore_enoent; | |
5293 | uint64_t rbind; | |
5294 | ||
5295 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(ssbt)"); | |
5296 | if (r < 0) | |
5297 | return bus_log_parse_error(r); | |
5298 | ||
5299 | while ((r = sd_bus_message_read(m, "(ssbt)", &source, &dest, &ignore_enoent, &rbind)) > 0) { | |
5300 | _cleanup_free_ char *str = NULL; | |
5301 | ||
5302 | if (isempty(source)) | |
5303 | continue; | |
5304 | ||
5305 | if (asprintf(&str, "%s%s%s%s%s", | |
5306 | ignore_enoent ? "-" : "", | |
5307 | source, | |
5308 | isempty(dest) ? "" : ":", | |
5309 | strempty(dest), | |
5310 | rbind == MS_REC ? ":rbind" : "") < 0) | |
5311 | return log_oom(); | |
5312 | ||
5313 | if (!strextend_with_separator(&paths, " ", str, NULL)) | |
5314 | return log_oom(); | |
5315 | } | |
5316 | if (r < 0) | |
5317 | return bus_log_parse_error(r); | |
5318 | ||
5319 | r = sd_bus_message_exit_container(m); | |
5320 | if (r < 0) | |
5321 | return bus_log_parse_error(r); | |
5322 | ||
5323 | if (all || !isempty(paths)) | |
5324 | bus_print_property_value(name, expected_value, value, strempty(paths)); | |
5325 | ||
5326 | return 1; | |
5327 | ||
5328 | } else if (streq(name, "TemporaryFileSystem")) { | |
5329 | _cleanup_free_ char *paths = NULL; | |
5330 | const char *target, *option; | |
5331 | ||
5332 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(ss)"); | |
5333 | if (r < 0) | |
5334 | return bus_log_parse_error(r); | |
5335 | ||
5336 | while ((r = sd_bus_message_read(m, "(ss)", &target, &option)) > 0) { | |
5337 | _cleanup_free_ char *str = NULL; | |
5338 | ||
5339 | if (isempty(target)) | |
5340 | continue; | |
5341 | ||
5342 | if (asprintf(&str, "%s%s%s", target, isempty(option) ? "" : ":", strempty(option)) < 0) | |
5343 | return log_oom(); | |
5344 | ||
5345 | if (!strextend_with_separator(&paths, " ", str, NULL)) | |
5346 | return log_oom(); | |
5347 | } | |
5348 | if (r < 0) | |
5349 | return bus_log_parse_error(r); | |
5350 | ||
5351 | r = sd_bus_message_exit_container(m); | |
5352 | if (r < 0) | |
5353 | return bus_log_parse_error(r); | |
5354 | ||
5355 | if (all || !isempty(paths)) | |
5356 | bus_print_property_value(name, expected_value, value, strempty(paths)); | |
5357 | ||
5358 | return 1; | |
305a2850 YW |
5359 | |
5360 | } else if (streq(name, "LogExtraFields")) { | |
5361 | _cleanup_free_ char *fields = NULL; | |
5362 | const void *p; | |
5363 | size_t sz; | |
5364 | ||
5365 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "ay"); | |
5366 | if (r < 0) | |
5367 | return bus_log_parse_error(r); | |
5368 | ||
5369 | while ((r = sd_bus_message_read_array(m, 'y', &p, &sz)) > 0) { | |
5370 | _cleanup_free_ char *str = NULL; | |
5371 | const char *eq; | |
5372 | ||
5373 | if (memchr(p, 0, sz)) | |
5374 | continue; | |
5375 | ||
5376 | eq = memchr(p, '=', sz); | |
5377 | if (!eq) | |
5378 | continue; | |
5379 | ||
5380 | if (!journal_field_valid(p, eq - (const char*) p, false)) | |
5381 | continue; | |
5382 | ||
5383 | str = malloc(sz + 1); | |
5384 | if (!str) | |
5385 | return log_oom(); | |
5386 | ||
5387 | memcpy(str, p, sz); | |
5388 | str[sz] = '\0'; | |
5389 | ||
5390 | if (!utf8_is_valid(str)) | |
5391 | continue; | |
5392 | ||
5393 | if (!strextend_with_separator(&fields, " ", str, NULL)) | |
5394 | return log_oom(); | |
5395 | } | |
5396 | if (r < 0) | |
5397 | return bus_log_parse_error(r); | |
5398 | ||
5399 | r = sd_bus_message_exit_container(m); | |
5400 | if (r < 0) | |
5401 | return bus_log_parse_error(r); | |
5402 | ||
5403 | if (all || !isempty(fields)) | |
5404 | bus_print_property_value(name, expected_value, value, strempty(fields)); | |
5405 | ||
a047f4f1 | 5406 | return 1; |
b070c7c0 | 5407 | } else if (contents[0] == SD_BUS_TYPE_BYTE && STR_IN_SET(name, "CPUAffinity", "NUMAMask")) { |
a047f4f1 MS |
5408 | _cleanup_free_ char *affinity = NULL; |
5409 | _cleanup_(cpu_set_reset) CPUSet set = {}; | |
5410 | const void *a; | |
5411 | size_t n; | |
5412 | ||
5413 | r = sd_bus_message_read_array(m, 'y', &a, &n); | |
5414 | if (r < 0) | |
5415 | return bus_log_parse_error(r); | |
5416 | ||
5417 | r = cpu_set_from_dbus(a, n, &set); | |
5418 | if (r < 0) | |
b070c7c0 | 5419 | return log_error_errno(r, "Failed to deserialize %s: %m", name); |
a047f4f1 MS |
5420 | |
5421 | affinity = cpu_set_to_range_string(&set); | |
5422 | if (!affinity) | |
5423 | return log_oom(); | |
5424 | ||
5425 | bus_print_property_value(name, expected_value, value, affinity); | |
5426 | ||
305a2850 | 5427 | return 1; |
48220598 LP |
5428 | } |
5429 | ||
5430 | break; | |
5431 | } | |
5432 | ||
48220598 LP |
5433 | return 0; |
5434 | } | |
5435 | ||
8559e61d YW |
5436 | typedef enum SystemctlShowMode{ |
5437 | SYSTEMCTL_SHOW_PROPERTIES, | |
5438 | SYSTEMCTL_SHOW_STATUS, | |
5439 | SYSTEMCTL_SHOW_HELP, | |
5440 | _SYSTEMCTL_SHOW_MODE_MAX, | |
5441 | _SYSTEMCTL_SHOW_MODE_INVALID = -1, | |
5442 | } SystemctlShowMode; | |
5443 | ||
aa8d423e | 5444 | static const char* const systemctl_show_mode_table[_SYSTEMCTL_SHOW_MODE_MAX] = { |
8559e61d YW |
5445 | [SYSTEMCTL_SHOW_PROPERTIES] = "show", |
5446 | [SYSTEMCTL_SHOW_STATUS] = "status", | |
5447 | [SYSTEMCTL_SHOW_HELP] = "help", | |
5448 | }; | |
5449 | ||
5450 | DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(systemctl_show_mode, SystemctlShowMode); | |
5451 | ||
f459b602 | 5452 | static int show_one( |
f459b602 MAP |
5453 | sd_bus *bus, |
5454 | const char *path, | |
3dced37b | 5455 | const char *unit, |
8559e61d | 5456 | SystemctlShowMode show_mode, |
f459b602 MAP |
5457 | bool *new_line, |
5458 | bool *ellipsized) { | |
5459 | ||
3dced37b | 5460 | static const struct bus_properties_map property_map[] = { |
b3d59367 AZ |
5461 | { "LoadState", "s", NULL, offsetof(UnitStatusInfo, load_state) }, |
5462 | { "ActiveState", "s", NULL, offsetof(UnitStatusInfo, active_state) }, | |
5463 | { "Documentation", "as", NULL, offsetof(UnitStatusInfo, documentation) }, | |
4679a8c3 YW |
5464 | {} |
5465 | }, status_map[] = { | |
b3d59367 AZ |
5466 | { "Id", "s", NULL, offsetof(UnitStatusInfo, id) }, |
5467 | { "LoadState", "s", NULL, offsetof(UnitStatusInfo, load_state) }, | |
5468 | { "ActiveState", "s", NULL, offsetof(UnitStatusInfo, active_state) }, | |
5469 | { "SubState", "s", NULL, offsetof(UnitStatusInfo, sub_state) }, | |
5470 | { "UnitFileState", "s", NULL, offsetof(UnitStatusInfo, unit_file_state) }, | |
5471 | { "UnitFilePreset", "s", NULL, offsetof(UnitStatusInfo, unit_file_preset) }, | |
5472 | { "Description", "s", NULL, offsetof(UnitStatusInfo, description) }, | |
5473 | { "Following", "s", NULL, offsetof(UnitStatusInfo, following) }, | |
5474 | { "Documentation", "as", NULL, offsetof(UnitStatusInfo, documentation) }, | |
5475 | { "FragmentPath", "s", NULL, offsetof(UnitStatusInfo, fragment_path) }, | |
5476 | { "SourcePath", "s", NULL, offsetof(UnitStatusInfo, source_path) }, | |
5477 | { "ControlGroup", "s", NULL, offsetof(UnitStatusInfo, control_group) }, | |
5478 | { "DropInPaths", "as", NULL, offsetof(UnitStatusInfo, dropin_paths) }, | |
5479 | { "LoadError", "(ss)", map_load_error, offsetof(UnitStatusInfo, load_error) }, | |
5480 | { "Result", "s", NULL, offsetof(UnitStatusInfo, result) }, | |
5481 | { "InactiveExitTimestamp", "t", NULL, offsetof(UnitStatusInfo, inactive_exit_timestamp) }, | |
5482 | { "InactiveExitTimestampMonotonic", "t", NULL, offsetof(UnitStatusInfo, inactive_exit_timestamp_monotonic) }, | |
5483 | { "ActiveEnterTimestamp", "t", NULL, offsetof(UnitStatusInfo, active_enter_timestamp) }, | |
5484 | { "ActiveExitTimestamp", "t", NULL, offsetof(UnitStatusInfo, active_exit_timestamp) }, | |
5485 | { "InactiveEnterTimestamp", "t", NULL, offsetof(UnitStatusInfo, inactive_enter_timestamp) }, | |
5486 | { "NeedDaemonReload", "b", NULL, offsetof(UnitStatusInfo, need_daemon_reload) }, | |
5487 | { "Transient", "b", NULL, offsetof(UnitStatusInfo, transient) }, | |
5488 | { "ExecMainPID", "u", NULL, offsetof(UnitStatusInfo, main_pid) }, | |
5489 | { "MainPID", "u", map_main_pid, 0 }, | |
5490 | { "ControlPID", "u", NULL, offsetof(UnitStatusInfo, control_pid) }, | |
5491 | { "StatusText", "s", NULL, offsetof(UnitStatusInfo, status_text) }, | |
5492 | { "PIDFile", "s", NULL, offsetof(UnitStatusInfo, pid_file) }, | |
5493 | { "StatusErrno", "i", NULL, offsetof(UnitStatusInfo, status_errno) }, | |
5494 | { "ExecMainStartTimestamp", "t", NULL, offsetof(UnitStatusInfo, start_timestamp) }, | |
5495 | { "ExecMainExitTimestamp", "t", NULL, offsetof(UnitStatusInfo, exit_timestamp) }, | |
5496 | { "ExecMainCode", "i", NULL, offsetof(UnitStatusInfo, exit_code) }, | |
5497 | { "ExecMainStatus", "i", NULL, offsetof(UnitStatusInfo, exit_status) }, | |
5498 | { "ConditionTimestamp", "t", NULL, offsetof(UnitStatusInfo, condition_timestamp) }, | |
5499 | { "ConditionResult", "b", NULL, offsetof(UnitStatusInfo, condition_result) }, | |
5500 | { "Conditions", "a(sbbsi)", map_conditions, 0 }, | |
5501 | { "AssertTimestamp", "t", NULL, offsetof(UnitStatusInfo, assert_timestamp) }, | |
5502 | { "AssertResult", "b", NULL, offsetof(UnitStatusInfo, assert_result) }, | |
5503 | { "Asserts", "a(sbbsi)", map_asserts, 0 }, | |
5504 | { "NextElapseUSecRealtime", "t", NULL, offsetof(UnitStatusInfo, next_elapse_real) }, | |
5505 | { "NextElapseUSecMonotonic", "t", NULL, offsetof(UnitStatusInfo, next_elapse_monotonic) }, | |
5506 | { "NAccepted", "u", NULL, offsetof(UnitStatusInfo, n_accepted) }, | |
5507 | { "NConnections", "u", NULL, offsetof(UnitStatusInfo, n_connections) }, | |
5508 | { "NRefused", "u", NULL, offsetof(UnitStatusInfo, n_refused) }, | |
5509 | { "Accept", "b", NULL, offsetof(UnitStatusInfo, accept) }, | |
5510 | { "Listen", "a(ss)", map_listen, offsetof(UnitStatusInfo, listen) }, | |
5511 | { "SysFSPath", "s", NULL, offsetof(UnitStatusInfo, sysfs_path) }, | |
5512 | { "Where", "s", NULL, offsetof(UnitStatusInfo, where) }, | |
5513 | { "What", "s", NULL, offsetof(UnitStatusInfo, what) }, | |
5514 | { "MemoryCurrent", "t", NULL, offsetof(UnitStatusInfo, memory_current) }, | |
5515 | { "DefaultMemoryMin", "t", NULL, offsetof(UnitStatusInfo, default_memory_min) }, | |
5516 | { "DefaultMemoryLow", "t", NULL, offsetof(UnitStatusInfo, default_memory_low) }, | |
5517 | { "MemoryMin", "t", NULL, offsetof(UnitStatusInfo, memory_min) }, | |
5518 | { "MemoryLow", "t", NULL, offsetof(UnitStatusInfo, memory_low) }, | |
5519 | { "MemoryHigh", "t", NULL, offsetof(UnitStatusInfo, memory_high) }, | |
5520 | { "MemoryMax", "t", NULL, offsetof(UnitStatusInfo, memory_max) }, | |
5521 | { "MemorySwapMax", "t", NULL, offsetof(UnitStatusInfo, memory_swap_max) }, | |
5522 | { "MemoryLimit", "t", NULL, offsetof(UnitStatusInfo, memory_limit) }, | |
5523 | { "CPUUsageNSec", "t", NULL, offsetof(UnitStatusInfo, cpu_usage_nsec) }, | |
5524 | { "TasksCurrent", "t", NULL, offsetof(UnitStatusInfo, tasks_current) }, | |
5525 | { "TasksMax", "t", NULL, offsetof(UnitStatusInfo, tasks_max) }, | |
5526 | { "IPIngressBytes", "t", NULL, offsetof(UnitStatusInfo, ip_ingress_bytes) }, | |
5527 | { "IPEgressBytes", "t", NULL, offsetof(UnitStatusInfo, ip_egress_bytes) }, | |
5528 | { "IOReadBytes", "t", NULL, offsetof(UnitStatusInfo, io_read_bytes) }, | |
5529 | { "IOWriteBytes", "t", NULL, offsetof(UnitStatusInfo, io_write_bytes) }, | |
5530 | { "ExecStartPre", "a(sasbttttuii)", map_exec, 0 }, | |
5531 | { "ExecStartPreEx", "a(sasasttttuii)", map_exec, 0 }, | |
5532 | { "ExecStart", "a(sasbttttuii)", map_exec, 0 }, | |
5533 | { "ExecStartEx", "a(sasasttttuii)", map_exec, 0 }, | |
5534 | { "ExecStartPost", "a(sasbttttuii)", map_exec, 0 }, | |
5535 | { "ExecStartPostEx", "a(sasasttttuii)", map_exec, 0 }, | |
5536 | { "ExecReload", "a(sasbttttuii)", map_exec, 0 }, | |
5537 | { "ExecStopPre", "a(sasbttttuii)", map_exec, 0 }, | |
5538 | { "ExecStop", "a(sasbttttuii)", map_exec, 0 }, | |
5539 | { "ExecStopPost", "a(sasbttttuii)", map_exec, 0 }, | |
3dced37b LP |
5540 | {} |
5541 | }; | |
5542 | ||
4afd3348 LP |
5543 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
5544 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
3dced37b | 5545 | _cleanup_set_free_ Set *found_properties = NULL; |
a7335518 | 5546 | _cleanup_(unit_status_info_free) UnitStatusInfo info = { |
934277fe | 5547 | .memory_current = (uint64_t) -1, |
da4d897e TH |
5548 | .memory_high = CGROUP_LIMIT_MAX, |
5549 | .memory_max = CGROUP_LIMIT_MAX, | |
96e131ea | 5550 | .memory_swap_max = CGROUP_LIMIT_MAX, |
934277fe | 5551 | .memory_limit = (uint64_t) -1, |
5ad096b3 | 5552 | .cpu_usage_nsec = (uint64_t) -1, |
03a7b521 LP |
5553 | .tasks_current = (uint64_t) -1, |
5554 | .tasks_max = (uint64_t) -1, | |
0e97c93f DM |
5555 | .ip_ingress_bytes = (uint64_t) -1, |
5556 | .ip_egress_bytes = (uint64_t) -1, | |
7a5de3f9 LP |
5557 | .io_read_bytes = UINT64_MAX, |
5558 | .io_write_bytes = UINT64_MAX, | |
934277fe | 5559 | }; |
4679a8c3 | 5560 | char **pp; |
f459b602 | 5561 | int r; |
48220598 | 5562 | |
48220598 | 5563 | assert(path); |
61cbdc4b | 5564 | assert(new_line); |
48220598 | 5565 | |
e3e0314b ZJS |
5566 | log_debug("Showing one %s", path); |
5567 | ||
4679a8c3 | 5568 | r = bus_map_all_properties( |
f22f08cd SP |
5569 | bus, |
5570 | "org.freedesktop.systemd1", | |
5571 | path, | |
4679a8c3 | 5572 | show_mode == SYSTEMCTL_SHOW_STATUS ? status_map : property_map, |
a7e4861c | 5573 | BUS_MAP_BOOLEAN_AS_BOOL, |
f459b602 | 5574 | &error, |
f22f08cd | 5575 | &reply, |
4679a8c3 | 5576 | &info); |
4c3e8e39 LP |
5577 | if (r < 0) |
5578 | return log_error_errno(r, "Failed to get properties: %s", bus_error_message(&error, r)); | |
48220598 | 5579 | |
4679a8c3 YW |
5580 | if (unit && streq_ptr(info.load_state, "not-found") && streq_ptr(info.active_state, "inactive")) { |
5581 | log_full(show_mode == SYSTEMCTL_SHOW_STATUS ? LOG_ERR : LOG_DEBUG, | |
5582 | "Unit %s could not be found.", unit); | |
e33a06a1 | 5583 | |
4679a8c3 YW |
5584 | if (show_mode == SYSTEMCTL_SHOW_STATUS) |
5585 | return EXIT_PROGRAM_OR_SERVICES_STATUS_UNKNOWN; | |
5586 | else if (show_mode == SYSTEMCTL_SHOW_HELP) | |
5587 | return -ENOENT; | |
5588 | } | |
e33a06a1 | 5589 | |
4679a8c3 YW |
5590 | if (*new_line) |
5591 | printf("\n"); | |
3dced37b | 5592 | |
4679a8c3 YW |
5593 | *new_line = true; |
5594 | ||
5595 | if (show_mode == SYSTEMCTL_SHOW_STATUS) { | |
5596 | print_status_info(bus, &info, ellipsized); | |
5597 | ||
5598 | if (info.active_state && !STR_IN_SET(info.active_state, "active", "reloading")) | |
5599 | return EXIT_PROGRAM_NOT_RUNNING; | |
5600 | ||
5601 | return EXIT_PROGRAM_RUNNING_OR_SERVICE_OK; | |
5602 | ||
5603 | } else if (show_mode == SYSTEMCTL_SHOW_HELP) { | |
5604 | show_unit_help(&info); | |
5605 | return 0; | |
3dced37b | 5606 | } |
e33a06a1 | 5607 | |
4679a8c3 YW |
5608 | r = sd_bus_message_rewind(reply, true); |
5609 | if (r < 0) | |
5610 | return log_error_errno(r, "Failed to rewind: %s", bus_error_message(&error, r)); | |
5611 | ||
07636114 | 5612 | r = bus_message_print_all_properties(reply, print_property, arg_properties, arg_value, arg_all, &found_properties); |
f459b602 MAP |
5613 | if (r < 0) |
5614 | return bus_log_parse_error(r); | |
48220598 | 5615 | |
4679a8c3 YW |
5616 | STRV_FOREACH(pp, arg_properties) |
5617 | if (!set_contains(found_properties, *pp)) | |
5618 | log_debug("Property %s does not exist.", *pp); | |
3dced37b | 5619 | |
4679a8c3 | 5620 | return 0; |
48220598 LP |
5621 | } |
5622 | ||
f74294c1 | 5623 | static int get_unit_dbus_path_by_pid( |
f459b602 MAP |
5624 | sd_bus *bus, |
5625 | uint32_t pid, | |
f74294c1 | 5626 | char **unit) { |
f459b602 | 5627 | |
4afd3348 LP |
5628 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
5629 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
373d32c9 | 5630 | char *u; |
a223b325 MS |
5631 | int r; |
5632 | ||
f459b602 | 5633 | r = sd_bus_call_method( |
f22f08cd SP |
5634 | bus, |
5635 | "org.freedesktop.systemd1", | |
5636 | "/org/freedesktop/systemd1", | |
5637 | "org.freedesktop.systemd1.Manager", | |
5638 | "GetUnitByPID", | |
f459b602 | 5639 | &error, |
f22f08cd | 5640 | &reply, |
f459b602 | 5641 | "u", pid); |
691395d8 LP |
5642 | if (r < 0) |
5643 | return log_error_errno(r, "Failed to get unit for PID %"PRIu32": %s", pid, bus_error_message(&error, r)); | |
a223b325 | 5644 | |
373d32c9 | 5645 | r = sd_bus_message_read(reply, "o", &u); |
f459b602 MAP |
5646 | if (r < 0) |
5647 | return bus_log_parse_error(r); | |
5648 | ||
373d32c9 LP |
5649 | u = strdup(u); |
5650 | if (!u) | |
5651 | return log_oom(); | |
5652 | ||
5653 | *unit = u; | |
f74294c1 | 5654 | return 0; |
a223b325 MS |
5655 | } |
5656 | ||
f459b602 | 5657 | static int show_all( |
f459b602 | 5658 | sd_bus *bus, |
f459b602 MAP |
5659 | bool *new_line, |
5660 | bool *ellipsized) { | |
5661 | ||
4afd3348 | 5662 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
f459b602 MAP |
5663 | _cleanup_free_ UnitInfo *unit_infos = NULL; |
5664 | const UnitInfo *u; | |
5665 | unsigned c; | |
5bb75bc7 | 5666 | int r, ret = 0; |
265a7a2a | 5667 | |
1238ee09 | 5668 | r = get_unit_list(bus, NULL, NULL, &unit_infos, 0, &reply); |
265a7a2a ZJS |
5669 | if (r < 0) |
5670 | return r; | |
5671 | ||
0221d68a | 5672 | (void) pager_open(arg_pager_flags); |
dbed408b | 5673 | |
f459b602 MAP |
5674 | c = (unsigned) r; |
5675 | ||
93bab288 | 5676 | typesafe_qsort(unit_infos, c, compare_unit_info); |
991f2a39 | 5677 | |
265a7a2a | 5678 | for (u = unit_infos; u < unit_infos + c; u++) { |
7fd1b19b | 5679 | _cleanup_free_ char *p = NULL; |
265a7a2a | 5680 | |
265a7a2a ZJS |
5681 | p = unit_dbus_path_from_name(u->id); |
5682 | if (!p) | |
5683 | return log_oom(); | |
5684 | ||
8559e61d | 5685 | r = show_one(bus, p, u->id, SYSTEMCTL_SHOW_STATUS, new_line, ellipsized); |
3df538da | 5686 | if (r < 0) |
265a7a2a | 5687 | return r; |
5bb75bc7 ZJS |
5688 | else if (r > 0 && ret == 0) |
5689 | ret = r; | |
265a7a2a ZJS |
5690 | } |
5691 | ||
5bb75bc7 | 5692 | return ret; |
265a7a2a ZJS |
5693 | } |
5694 | ||
8fcf784d LP |
5695 | static int show_system_status(sd_bus *bus) { |
5696 | char since1[FORMAT_TIMESTAMP_RELATIVE_MAX], since2[FORMAT_TIMESTAMP_MAX]; | |
f9e0eefc | 5697 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
e7e55dbd | 5698 | _cleanup_(machine_info_clear) struct machine_info mi = {}; |
f9e0eefc | 5699 | _cleanup_free_ char *hn = NULL; |
8fcf784d LP |
5700 | const char *on, *off; |
5701 | int r; | |
5702 | ||
5703 | hn = gethostname_malloc(); | |
5704 | if (!hn) | |
5705 | return log_oom(); | |
5706 | ||
a7e4861c YW |
5707 | r = bus_map_all_properties( |
5708 | bus, | |
5709 | "org.freedesktop.systemd1", | |
5710 | "/org/freedesktop/systemd1", | |
5711 | machine_info_property_map, | |
5712 | BUS_MAP_STRDUP, | |
5713 | &error, | |
5714 | NULL, | |
5715 | &mi); | |
f647962d | 5716 | if (r < 0) |
f9e0eefc | 5717 | return log_error_errno(r, "Failed to read server status: %s", bus_error_message(&error, r)); |
8fcf784d | 5718 | |
8fcf784d LP |
5719 | if (streq_ptr(mi.state, "degraded")) { |
5720 | on = ansi_highlight_red(); | |
1fc464f6 | 5721 | off = ansi_normal(); |
92f34a9c JR |
5722 | } else if (streq_ptr(mi.state, "running")) { |
5723 | on = ansi_highlight_green(); | |
5724 | off = ansi_normal(); | |
5725 | } else { | |
8fcf784d | 5726 | on = ansi_highlight_yellow(); |
1fc464f6 | 5727 | off = ansi_normal(); |
92f34a9c | 5728 | } |
8fcf784d | 5729 | |
9a6f746f | 5730 | printf("%s%s%s %s\n", on, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE), off, arg_host ? arg_host : hn); |
b0d14c69 | 5731 | |
8fcf784d LP |
5732 | printf(" State: %s%s%s\n", |
5733 | on, strna(mi.state), off); | |
5734 | ||
c6ba5b80 MP |
5735 | printf(" Jobs: %" PRIu32 " queued\n", mi.n_jobs); |
5736 | printf(" Failed: %" PRIu32 " units\n", mi.n_failed_units); | |
8fcf784d LP |
5737 | |
5738 | printf(" Since: %s; %s\n", | |
5739 | format_timestamp(since2, sizeof(since2), mi.timestamp), | |
5740 | format_timestamp_relative(since1, sizeof(since1), mi.timestamp)); | |
5741 | ||
5742 | printf(" CGroup: %s\n", mi.control_group ?: "/"); | |
4c315c2c IS |
5743 | if (IN_SET(arg_transport, |
5744 | BUS_TRANSPORT_LOCAL, | |
5745 | BUS_TRANSPORT_MACHINE)) { | |
8fcf784d LP |
5746 | static const char prefix[] = " "; |
5747 | unsigned c; | |
5748 | ||
5749 | c = columns(); | |
5750 | if (c > sizeof(prefix) - 1) | |
5751 | c -= sizeof(prefix) - 1; | |
5752 | else | |
5753 | c = 0; | |
5754 | ||
0ff308c8 | 5755 | show_cgroup(SYSTEMD_CGROUP_CONTROLLER, strempty(mi.control_group), prefix, c, get_output_flags()); |
8fcf784d LP |
5756 | } |
5757 | ||
8fcf784d LP |
5758 | return 0; |
5759 | } | |
5760 | ||
e449de87 | 5761 | static int show(int argc, char *argv[], void *userdata) { |
8559e61d YW |
5762 | bool new_line = false, ellipsized = false; |
5763 | SystemctlShowMode show_mode; | |
e3e0314b | 5764 | int r, ret = 0; |
4fbd7192 | 5765 | sd_bus *bus; |
48220598 | 5766 | |
e449de87 | 5767 | assert(argv); |
48220598 | 5768 | |
8559e61d | 5769 | show_mode = systemctl_show_mode_from_string(argv[0]); |
baaa35ad ZJS |
5770 | if (show_mode < 0) |
5771 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
5772 | "Invalid argument."); | |
e449de87 | 5773 | |
baaa35ad ZJS |
5774 | if (show_mode == SYSTEMCTL_SHOW_HELP && argc <= 1) |
5775 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
5776 | "This command expects one or more unit names. Did you mean --help?"); | |
61cbdc4b | 5777 | |
d2ad7e1b ZJS |
5778 | r = acquire_bus(BUS_MANAGER, &bus); |
5779 | if (r < 0) | |
5780 | return r; | |
5781 | ||
0221d68a | 5782 | (void) pager_open(arg_pager_flags); |
ec14911e | 5783 | |
4fbd7192 | 5784 | /* If no argument is specified inspect the manager itself */ |
8559e61d YW |
5785 | if (show_mode == SYSTEMCTL_SHOW_PROPERTIES && argc <= 1) |
5786 | return show_one(bus, "/org/freedesktop/systemd1", NULL, show_mode, &new_line, &ellipsized); | |
48220598 | 5787 | |
8559e61d | 5788 | if (show_mode == SYSTEMCTL_SHOW_STATUS && argc <= 1) { |
8fcf784d | 5789 | |
8fcf784d LP |
5790 | show_system_status(bus); |
5791 | new_line = true; | |
5792 | ||
5793 | if (arg_all) | |
8559e61d | 5794 | ret = show_all(bus, &new_line, &ellipsized); |
8fcf784d | 5795 | } else { |
e3e0314b ZJS |
5796 | _cleanup_free_ char **patterns = NULL; |
5797 | char **name; | |
5798 | ||
e449de87 | 5799 | STRV_FOREACH(name, strv_skip(argv, 1)) { |
3dced37b | 5800 | _cleanup_free_ char *path = NULL, *unit = NULL; |
94e0bd7d | 5801 | uint32_t id; |
48220598 | 5802 | |
94e0bd7d | 5803 | if (safe_atou32(*name, &id) < 0) { |
e3e0314b | 5804 | if (strv_push(&patterns, *name) < 0) |
94e0bd7d | 5805 | return log_oom(); |
48220598 | 5806 | |
e3e0314b | 5807 | continue; |
8559e61d | 5808 | } else if (show_mode == SYSTEMCTL_SHOW_PROPERTIES) { |
94e0bd7d | 5809 | /* Interpret as job id */ |
3dced37b | 5810 | if (asprintf(&path, "/org/freedesktop/systemd1/job/%u", id) < 0) |
94e0bd7d | 5811 | return log_oom(); |
48220598 | 5812 | |
94e0bd7d ZJS |
5813 | } else { |
5814 | /* Interpret as PID */ | |
3dced37b | 5815 | r = get_unit_dbus_path_by_pid(bus, id, &path); |
373d32c9 | 5816 | if (r < 0) { |
94e0bd7d | 5817 | ret = r; |
373d32c9 LP |
5818 | continue; |
5819 | } | |
3dced37b LP |
5820 | |
5821 | r = unit_name_from_dbus_path(path, &unit); | |
5822 | if (r < 0) | |
5823 | return log_oom(); | |
94e0bd7d | 5824 | } |
f74294c1 | 5825 | |
8559e61d | 5826 | r = show_one(bus, path, unit, show_mode, &new_line, &ellipsized); |
5bb75bc7 ZJS |
5827 | if (r < 0) |
5828 | return r; | |
5829 | else if (r > 0 && ret == 0) | |
5830 | ret = r; | |
48220598 | 5831 | } |
94e0bd7d | 5832 | |
e3e0314b ZJS |
5833 | if (!strv_isempty(patterns)) { |
5834 | _cleanup_strv_free_ char **names = NULL; | |
5835 | ||
5836 | r = expand_names(bus, patterns, NULL, &names); | |
5837 | if (r < 0) | |
691395d8 | 5838 | return log_error_errno(r, "Failed to expand names: %m"); |
e3e0314b ZJS |
5839 | |
5840 | STRV_FOREACH(name, names) { | |
3dced37b | 5841 | _cleanup_free_ char *path; |
e3e0314b | 5842 | |
3dced37b LP |
5843 | path = unit_dbus_path_from_name(*name); |
5844 | if (!path) | |
e3e0314b ZJS |
5845 | return log_oom(); |
5846 | ||
8559e61d | 5847 | r = show_one(bus, path, *name, show_mode, &new_line, &ellipsized); |
5bb75bc7 ZJS |
5848 | if (r < 0) |
5849 | return r; | |
3dced37b | 5850 | if (r > 0 && ret == 0) |
5bb75bc7 | 5851 | ret = r; |
e3e0314b ZJS |
5852 | } |
5853 | } | |
5854 | } | |
5855 | ||
94e0bd7d ZJS |
5856 | if (ellipsized && !arg_quiet) |
5857 | printf("Hint: Some lines were ellipsized, use -l to show in full.\n"); | |
48220598 | 5858 | |
22f4096c | 5859 | return ret; |
0183528f LP |
5860 | } |
5861 | ||
e449de87 | 5862 | static int cat(int argc, char *argv[], void *userdata) { |
8e766630 | 5863 | _cleanup_(lookup_paths_free) LookupPaths lp = {}; |
15ef1144 LP |
5864 | _cleanup_strv_free_ char **names = NULL; |
5865 | char **name; | |
4fbd7192 LP |
5866 | sd_bus *bus; |
5867 | bool first = true; | |
25586912 | 5868 | int r; |
15ef1144 | 5869 | |
4eb53120 FS |
5870 | /* Include all units by default - i.e. continue as if the --all |
5871 | * option was used */ | |
5872 | if (strv_isempty(arg_states)) | |
5873 | arg_all = true; | |
5874 | ||
2f50e59f LP |
5875 | if (arg_transport != BUS_TRANSPORT_LOCAL) |
5876 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot remotely cat units."); | |
3e495a66 | 5877 | |
4943d143 | 5878 | r = lookup_paths_init(&lp, arg_scope, 0, arg_root); |
ad2a0358 | 5879 | if (r < 0) |
c51932be | 5880 | return log_error_errno(r, "Failed to determine unit paths: %m"); |
ad2a0358 | 5881 | |
4fbd7192 | 5882 | r = acquire_bus(BUS_MANAGER, &bus); |
15ef1144 | 5883 | if (r < 0) |
4fbd7192 | 5884 | return r; |
15ef1144 | 5885 | |
e449de87 | 5886 | r = expand_names(bus, strv_skip(argv, 1), NULL, &names); |
4fbd7192 LP |
5887 | if (r < 0) |
5888 | return log_error_errno(r, "Failed to expand names: %m"); | |
ad2a0358 | 5889 | |
0221d68a | 5890 | (void) pager_open(arg_pager_flags); |
15ef1144 LP |
5891 | |
5892 | STRV_FOREACH(name, names) { | |
ad2a0358 | 5893 | _cleanup_free_ char *fragment_path = NULL; |
15ef1144 | 5894 | _cleanup_strv_free_ char **dropin_paths = NULL; |
15ef1144 | 5895 | |
02a126a3 | 5896 | r = unit_find_paths(bus, *name, &lp, false, &fragment_path, &dropin_paths); |
86f004fb | 5897 | if (r == -ERFKILL) { |
02a126a3 LP |
5898 | printf("%s# Unit %s is masked%s.\n", |
5899 | ansi_highlight_magenta(), | |
5900 | *name, | |
5901 | ansi_normal()); | |
5902 | continue; | |
5903 | } | |
5904 | if (r == -EKEYREJECTED) { | |
5905 | printf("%s# Unit %s could not be loaded.%s\n", | |
86f004fb ZJS |
5906 | ansi_highlight_magenta(), |
5907 | *name, | |
5908 | ansi_normal()); | |
5909 | continue; | |
5910 | } | |
ad2a0358 ZJS |
5911 | if (r < 0) |
5912 | return r; | |
02a126a3 | 5913 | if (r == 0) |
b5e6a600 | 5914 | return -ENOENT; |
15ef1144 LP |
5915 | |
5916 | if (first) | |
5917 | first = false; | |
5918 | else | |
5919 | puts(""); | |
5920 | ||
5b9635d1 | 5921 | if (need_daemon_reload(bus, *name) > 0) /* ignore errors (<0), this is informational output */ |
e100155d LW |
5922 | fprintf(stderr, |
5923 | "%s# Warning: %s changed on disk, the version systemd has loaded is outdated.\n" | |
5924 | "%s# This output shows the current version of the unit's original fragment and drop-in files.\n" | |
5925 | "%s# If fragments or drop-ins were added or removed, they are not properly reflected in this output.\n" | |
5926 | "%s# Run 'systemctl%s daemon-reload' to reload units.%s\n", | |
5927 | ansi_highlight_red(), | |
5928 | *name, | |
5929 | ansi_highlight_red(), | |
5930 | ansi_highlight_red(), | |
5931 | ansi_highlight_red(), | |
5932 | arg_scope == UNIT_FILE_SYSTEM ? "" : " --user", | |
5933 | ansi_normal()); | |
5934 | ||
854a42fb | 5935 | r = cat_files(fragment_path, dropin_paths, 0); |
81f5e513 ZJS |
5936 | if (r < 0) |
5937 | return r; | |
15ef1144 LP |
5938 | } |
5939 | ||
25586912 | 5940 | return 0; |
15ef1144 LP |
5941 | } |
5942 | ||
e449de87 | 5943 | static int set_property(int argc, char *argv[], void *userdata) { |
4afd3348 LP |
5944 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
5945 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
68372da6 | 5946 | _cleanup_free_ char *n = NULL; |
89ada3ba | 5947 | UnitType t; |
4fbd7192 | 5948 | sd_bus *bus; |
8e2af478 LP |
5949 | int r; |
5950 | ||
4fbd7192 LP |
5951 | r = acquire_bus(BUS_MANAGER, &bus); |
5952 | if (r < 0) | |
5953 | return r; | |
5954 | ||
8a4b13c5 | 5955 | polkit_agent_open_maybe(); |
d2ad7e1b | 5956 | |
f459b602 MAP |
5957 | r = sd_bus_message_new_method_call( |
5958 | bus, | |
151b9b96 | 5959 | &m, |
8e2af478 LP |
5960 | "org.freedesktop.systemd1", |
5961 | "/org/freedesktop/systemd1", | |
5962 | "org.freedesktop.systemd1.Manager", | |
151b9b96 | 5963 | "SetUnitProperties"); |
f459b602 MAP |
5964 | if (r < 0) |
5965 | return bus_log_create_error(r); | |
8e2af478 | 5966 | |
37cbc1d5 | 5967 | r = unit_name_mangle(argv[1], arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN, &n); |
7410616c LP |
5968 | if (r < 0) |
5969 | return log_error_errno(r, "Failed to mangle unit name: %m"); | |
68372da6 | 5970 | |
89ada3ba | 5971 | t = unit_name_to_type(n); |
2f50e59f LP |
5972 | if (t < 0) |
5973 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid unit type: %s", n); | |
89ada3ba | 5974 | |
f459b602 MAP |
5975 | r = sd_bus_message_append(m, "sb", n, arg_runtime); |
5976 | if (r < 0) | |
5977 | return bus_log_create_error(r); | |
8e2af478 | 5978 | |
f459b602 MAP |
5979 | r = sd_bus_message_open_container(m, SD_BUS_TYPE_ARRAY, "(sv)"); |
5980 | if (r < 0) | |
5981 | return bus_log_create_error(r); | |
8e2af478 | 5982 | |
89ada3ba | 5983 | r = bus_append_unit_property_assignment_many(m, t, strv_skip(argv, 2)); |
8673cf13 LP |
5984 | if (r < 0) |
5985 | return r; | |
8e2af478 | 5986 | |
f459b602 MAP |
5987 | r = sd_bus_message_close_container(m); |
5988 | if (r < 0) | |
5989 | return bus_log_create_error(r); | |
8e2af478 | 5990 | |
c49b30a2 | 5991 | r = sd_bus_call(bus, m, 0, &error, NULL); |
691395d8 LP |
5992 | if (r < 0) |
5993 | return log_error_errno(r, "Failed to set unit properties on %s: %s", n, bus_error_message(&error, r)); | |
8e2af478 LP |
5994 | |
5995 | return 0; | |
5996 | } | |
5997 | ||
e449de87 | 5998 | static int daemon_reload(int argc, char *argv[], void *userdata) { |
4afd3348 | 5999 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
2853b60a | 6000 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
7e4249b9 | 6001 | const char *method; |
4fbd7192 | 6002 | sd_bus *bus; |
f459b602 | 6003 | int r; |
7e4249b9 | 6004 | |
4fbd7192 LP |
6005 | r = acquire_bus(BUS_MANAGER, &bus); |
6006 | if (r < 0) | |
6007 | return r; | |
6008 | ||
8a4b13c5 | 6009 | polkit_agent_open_maybe(); |
d2ad7e1b | 6010 | |
2853b60a LP |
6011 | switch (arg_action) { |
6012 | ||
6013 | case ACTION_RELOAD: | |
e4b61340 | 6014 | method = "Reload"; |
2853b60a LP |
6015 | break; |
6016 | ||
6017 | case ACTION_REEXEC: | |
e4b61340 | 6018 | method = "Reexecute"; |
2853b60a LP |
6019 | break; |
6020 | ||
6021 | case ACTION_SYSTEMCTL: | |
6022 | method = streq(argv[0], "daemon-reexec") ? "Reexecute" : | |
6023 | /* "daemon-reload" */ "Reload"; | |
6024 | break; | |
e4b61340 | 6025 | |
2853b60a LP |
6026 | default: |
6027 | assert_not_reached("Unexpected action"); | |
e4b61340 | 6028 | } |
7e4249b9 | 6029 | |
2853b60a LP |
6030 | r = sd_bus_message_new_method_call( |
6031 | bus, | |
6032 | &m, | |
6033 | "org.freedesktop.systemd1", | |
6034 | "/org/freedesktop/systemd1", | |
6035 | "org.freedesktop.systemd1.Manager", | |
6036 | method); | |
6037 | if (r < 0) | |
6038 | return bus_log_create_error(r); | |
6039 | ||
6040 | /* Note we use an extra-long timeout here. This is because a reload or reexec means generators are rerun which | |
6041 | * are timed out after DEFAULT_TIMEOUT_USEC. Let's use twice that time here, so that the generators can have | |
6042 | * their timeout, and for everything else there's the same time budget in place. */ | |
6043 | ||
6044 | r = sd_bus_call(bus, m, DEFAULT_TIMEOUT_USEC * 2, &error, NULL); | |
6045 | ||
6046 | /* On reexecution, we expect a disconnect, not a reply */ | |
6047 | if (IN_SET(r, -ETIMEDOUT, -ECONNRESET) && streq(method, "Reexecute")) | |
6048 | r = 0; | |
6049 | ||
6050 | if (r < 0 && arg_action == ACTION_SYSTEMCTL) | |
6051 | return log_error_errno(r, "Failed to reload daemon: %s", bus_error_message(&error, r)); | |
6052 | ||
6053 | /* Note that for the legacy commands (i.e. those with action != ACTION_SYSTEMCTL) we support fallbacks to the | |
6054 | * old ways of doing things, hence don't log any error in that case here. */ | |
6055 | ||
6056 | return r < 0 ? r : 0; | |
6057 | } | |
6058 | ||
6059 | static int trivial_method(int argc, char *argv[], void *userdata) { | |
6060 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
6061 | const char *method; | |
6062 | sd_bus *bus; | |
6063 | int r; | |
6064 | ||
1ae17672 ZJS |
6065 | if (arg_dry_run) |
6066 | return 0; | |
6067 | ||
2853b60a LP |
6068 | r = acquire_bus(BUS_MANAGER, &bus); |
6069 | if (r < 0) | |
6070 | return r; | |
6071 | ||
8a4b13c5 | 6072 | polkit_agent_open_maybe(); |
d2ad7e1b | 6073 | |
2853b60a LP |
6074 | method = |
6075 | streq(argv[0], "clear-jobs") || | |
6076 | streq(argv[0], "cancel") ? "ClearJobs" : | |
6077 | streq(argv[0], "reset-failed") ? "ResetFailed" : | |
6078 | streq(argv[0], "halt") ? "Halt" : | |
6079 | streq(argv[0], "reboot") ? "Reboot" : | |
6080 | streq(argv[0], "kexec") ? "KExec" : | |
6081 | streq(argv[0], "exit") ? "Exit" : | |
6082 | /* poweroff */ "PowerOff"; | |
6083 | ||
6e646d22 | 6084 | r = sd_bus_call_method( |
f22f08cd SP |
6085 | bus, |
6086 | "org.freedesktop.systemd1", | |
6087 | "/org/freedesktop/systemd1", | |
6088 | "org.freedesktop.systemd1.Manager", | |
6e646d22 LP |
6089 | method, |
6090 | &error, | |
6091 | NULL, | |
6092 | NULL); | |
2853b60a LP |
6093 | if (r < 0 && arg_action == ACTION_SYSTEMCTL) |
6094 | return log_error_errno(r, "Failed to execute operation: %s", bus_error_message(&error, r)); | |
6095 | ||
6096 | /* Note that for the legacy commands (i.e. those with action != ACTION_SYSTEMCTL) we support fallbacks to the | |
6097 | * old ways of doing things, hence don't log any error in that case here. */ | |
7e4249b9 | 6098 | |
0a9776c2 | 6099 | return r < 0 ? r : 0; |
7e4249b9 LP |
6100 | } |
6101 | ||
e449de87 | 6102 | static int reset_failed(int argc, char *argv[], void *userdata) { |
e3e0314b | 6103 | _cleanup_strv_free_ char **names = NULL; |
4fbd7192 | 6104 | sd_bus *bus; |
f84190d8 | 6105 | char **name; |
e3e0314b | 6106 | int r, q; |
5632e374 | 6107 | |
e449de87 | 6108 | if (argc <= 1) |
2853b60a | 6109 | return trivial_method(argc, argv, userdata); |
5632e374 | 6110 | |
4fbd7192 LP |
6111 | r = acquire_bus(BUS_MANAGER, &bus); |
6112 | if (r < 0) | |
6113 | return r; | |
6114 | ||
8a4b13c5 | 6115 | polkit_agent_open_maybe(); |
d2ad7e1b | 6116 | |
e449de87 | 6117 | r = expand_names(bus, strv_skip(argv, 1), NULL, &names); |
e3e0314b | 6118 | if (r < 0) |
691395d8 | 6119 | return log_error_errno(r, "Failed to expand names: %m"); |
f84190d8 | 6120 | |
e3e0314b | 6121 | STRV_FOREACH(name, names) { |
4afd3348 | 6122 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
342641fb | 6123 | |
6e646d22 | 6124 | q = sd_bus_call_method( |
f22f08cd | 6125 | bus, |
b0193f1c LP |
6126 | "org.freedesktop.systemd1", |
6127 | "/org/freedesktop/systemd1", | |
6128 | "org.freedesktop.systemd1.Manager", | |
6e646d22 LP |
6129 | "ResetFailedUnit", |
6130 | &error, | |
6131 | NULL, | |
6132 | "s", *name); | |
e3e0314b | 6133 | if (q < 0) { |
691395d8 | 6134 | log_error_errno(q, "Failed to reset failed state of unit %s: %s", *name, bus_error_message(&error, q)); |
e3e0314b ZJS |
6135 | if (r == 0) |
6136 | r = q; | |
f459b602 | 6137 | } |
5632e374 LP |
6138 | } |
6139 | ||
e3e0314b | 6140 | return r; |
5632e374 LP |
6141 | } |
6142 | ||
804ee07c ZJS |
6143 | static int print_variable(const char *s) { |
6144 | const char *sep; | |
6145 | _cleanup_free_ char *esc = NULL; | |
6146 | ||
6147 | sep = strchr(s, '='); | |
baaa35ad ZJS |
6148 | if (!sep) |
6149 | return log_error_errno(SYNTHETIC_ERRNO(EUCLEAN), | |
6150 | "Invalid environment block"); | |
804ee07c ZJS |
6151 | |
6152 | esc = shell_maybe_quote(sep + 1, ESCAPE_POSIX); | |
6153 | if (!esc) | |
6154 | return log_oom(); | |
6155 | ||
6156 | printf("%.*s=%s\n", (int)(sep-s), s, esc); | |
6157 | return 0; | |
6158 | } | |
6159 | ||
e449de87 | 6160 | static int show_environment(int argc, char *argv[], void *userdata) { |
4afd3348 LP |
6161 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
6162 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
f459b602 | 6163 | const char *text; |
4fbd7192 | 6164 | sd_bus *bus; |
7e4249b9 | 6165 | int r; |
7e4249b9 | 6166 | |
4fbd7192 LP |
6167 | r = acquire_bus(BUS_MANAGER, &bus); |
6168 | if (r < 0) | |
6169 | return r; | |
6170 | ||
0221d68a | 6171 | (void) pager_open(arg_pager_flags); |
d2ad7e1b | 6172 | |
f459b602 | 6173 | r = sd_bus_get_property( |
f22f08cd SP |
6174 | bus, |
6175 | "org.freedesktop.systemd1", | |
6176 | "/org/freedesktop/systemd1", | |
f459b602 MAP |
6177 | "org.freedesktop.systemd1.Manager", |
6178 | "Environment", | |
6179 | &error, | |
f22f08cd | 6180 | &reply, |
f459b602 | 6181 | "as"); |
691395d8 LP |
6182 | if (r < 0) |
6183 | return log_error_errno(r, "Failed to get environment: %s", bus_error_message(&error, r)); | |
7e4249b9 | 6184 | |
f459b602 MAP |
6185 | r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "s"); |
6186 | if (r < 0) | |
6187 | return bus_log_parse_error(r); | |
7e4249b9 | 6188 | |
804ee07c ZJS |
6189 | while ((r = sd_bus_message_read_basic(reply, SD_BUS_TYPE_STRING, &text)) > 0) { |
6190 | r = print_variable(text); | |
6191 | if (r < 0) | |
6192 | return r; | |
6193 | } | |
f459b602 MAP |
6194 | if (r < 0) |
6195 | return bus_log_parse_error(r); | |
7e4249b9 | 6196 | |
f459b602 MAP |
6197 | r = sd_bus_message_exit_container(reply); |
6198 | if (r < 0) | |
6199 | return bus_log_parse_error(r); | |
7e4249b9 | 6200 | |
f84190d8 | 6201 | return 0; |
7e4249b9 LP |
6202 | } |
6203 | ||
e449de87 | 6204 | static int switch_root(int argc, char *argv[], void *userdata) { |
4afd3348 | 6205 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
f39d4a08 HH |
6206 | _cleanup_free_ char *cmdline_init = NULL; |
6207 | const char *root, *init; | |
4fbd7192 | 6208 | sd_bus *bus; |
f459b602 | 6209 | int r; |
957eb8ca | 6210 | |
2f50e59f LP |
6211 | if (arg_transport != BUS_TRANSPORT_LOCAL) |
6212 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot switch root remotely."); | |
4fbd7192 | 6213 | |
2f50e59f LP |
6214 | if (argc < 2 || argc > 3) |
6215 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Wrong number of arguments."); | |
957eb8ca | 6216 | |
e449de87 | 6217 | root = argv[1]; |
13068da8 | 6218 | |
e449de87 LP |
6219 | if (argc >= 3) |
6220 | init = argv[2]; | |
13068da8 | 6221 | else { |
01771226 | 6222 | r = proc_cmdline_get_key("init", 0, &cmdline_init); |
f39d4a08 | 6223 | if (r < 0) |
da927ba9 | 6224 | log_debug_errno(r, "Failed to parse /proc/cmdline: %m"); |
13068da8 | 6225 | |
f39d4a08 | 6226 | init = cmdline_init; |
13068da8 | 6227 | } |
f459b602 | 6228 | |
3c6f7c34 | 6229 | init = empty_to_null(init); |
f39d4a08 HH |
6230 | if (init) { |
6231 | const char *root_systemd_path = NULL, *root_init_path = NULL; | |
6232 | ||
270384b2 YW |
6233 | root_systemd_path = prefix_roota(root, "/" SYSTEMD_BINARY_PATH); |
6234 | root_init_path = prefix_roota(root, init); | |
f39d4a08 HH |
6235 | |
6236 | /* If the passed init is actually the same as the | |
6237 | * systemd binary, then let's suppress it. */ | |
e3f791a2 | 6238 | if (files_same(root_init_path, root_systemd_path, 0) > 0) |
f39d4a08 HH |
6239 | init = NULL; |
6240 | } | |
13068da8 | 6241 | |
d86e4c97 LP |
6242 | /* Instruct PID1 to exclude us from its killing spree applied during the transition. Otherwise we |
6243 | * would exit with a failure status even though the switch to the new root has succeed. */ | |
6244 | assert(saved_argv); | |
6245 | assert(saved_argv[0]); | |
6246 | saved_argv[0][0] = '@'; | |
acc28e2e | 6247 | |
4fbd7192 LP |
6248 | r = acquire_bus(BUS_MANAGER, &bus); |
6249 | if (r < 0) | |
6250 | return r; | |
6251 | ||
8a7a9cea ZJS |
6252 | /* If we are slow to exit after the root switch, the new systemd instance |
6253 | * will send us a signal to terminate. Just ignore it and exit normally. | |
6254 | * This way the unit does not end up as failed. | |
6255 | */ | |
6256 | r = ignore_signals(SIGTERM, -1); | |
6257 | if (r < 0) | |
6258 | log_warning_errno(r, "Failed to change disposition of SIGTERM to ignore: %m"); | |
6259 | ||
f39d4a08 | 6260 | log_debug("Switching root - root: %s; init: %s", root, strna(init)); |
957eb8ca | 6261 | |
f459b602 | 6262 | r = sd_bus_call_method( |
f22f08cd | 6263 | bus, |
957eb8ca LP |
6264 | "org.freedesktop.systemd1", |
6265 | "/org/freedesktop/systemd1", | |
6266 | "org.freedesktop.systemd1.Manager", | |
f22f08cd | 6267 | "SwitchRoot", |
f459b602 | 6268 | &error, |
f22f08cd | 6269 | NULL, |
f459b602 | 6270 | "ss", root, init); |
8a7a9cea ZJS |
6271 | if (r < 0) { |
6272 | (void) default_signals(SIGTERM, -1); | |
6273 | ||
691395d8 | 6274 | return log_error_errno(r, "Failed to switch root: %s", bus_error_message(&error, r)); |
8a7a9cea | 6275 | } |
f459b602 MAP |
6276 | |
6277 | return 0; | |
957eb8ca LP |
6278 | } |
6279 | ||
e449de87 | 6280 | static int set_environment(int argc, char *argv[], void *userdata) { |
4afd3348 LP |
6281 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
6282 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; | |
7e4249b9 | 6283 | const char *method; |
4fbd7192 | 6284 | sd_bus *bus; |
31e767f7 LP |
6285 | int r; |
6286 | ||
e449de87 LP |
6287 | assert(argc > 1); |
6288 | assert(argv); | |
7e4249b9 | 6289 | |
4fbd7192 LP |
6290 | r = acquire_bus(BUS_MANAGER, &bus); |
6291 | if (r < 0) | |
6292 | return r; | |
6293 | ||
8a4b13c5 | 6294 | polkit_agent_open_maybe(); |
d2ad7e1b | 6295 | |
e449de87 | 6296 | method = streq(argv[0], "set-environment") |
7e4249b9 LP |
6297 | ? "SetEnvironment" |
6298 | : "UnsetEnvironment"; | |
6299 | ||
f459b602 MAP |
6300 | r = sd_bus_message_new_method_call( |
6301 | bus, | |
151b9b96 | 6302 | &m, |
31e767f7 LP |
6303 | "org.freedesktop.systemd1", |
6304 | "/org/freedesktop/systemd1", | |
6305 | "org.freedesktop.systemd1.Manager", | |
151b9b96 | 6306 | method); |
f459b602 MAP |
6307 | if (r < 0) |
6308 | return bus_log_create_error(r); | |
7e4249b9 | 6309 | |
e449de87 | 6310 | r = sd_bus_message_append_strv(m, strv_skip(argv, 1)); |
31e767f7 | 6311 | if (r < 0) |
f459b602 | 6312 | return bus_log_create_error(r); |
7e4249b9 | 6313 | |
c49b30a2 | 6314 | r = sd_bus_call(bus, m, 0, &error, NULL); |
691395d8 LP |
6315 | if (r < 0) |
6316 | return log_error_errno(r, "Failed to set environment: %s", bus_error_message(&error, r)); | |
7e4249b9 | 6317 | |
f84190d8 | 6318 | return 0; |
7e4249b9 LP |
6319 | } |
6320 | ||
e449de87 | 6321 | static int import_environment(int argc, char *argv[], void *userdata) { |
4afd3348 LP |
6322 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
6323 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; | |
4fbd7192 | 6324 | sd_bus *bus; |
ac3efa8a LP |
6325 | int r; |
6326 | ||
4fbd7192 LP |
6327 | r = acquire_bus(BUS_MANAGER, &bus); |
6328 | if (r < 0) | |
6329 | return r; | |
6330 | ||
8a4b13c5 | 6331 | polkit_agent_open_maybe(); |
d2ad7e1b | 6332 | |
ac3efa8a LP |
6333 | r = sd_bus_message_new_method_call( |
6334 | bus, | |
151b9b96 | 6335 | &m, |
ac3efa8a LP |
6336 | "org.freedesktop.systemd1", |
6337 | "/org/freedesktop/systemd1", | |
6338 | "org.freedesktop.systemd1.Manager", | |
151b9b96 | 6339 | "SetEnvironment"); |
ac3efa8a LP |
6340 | if (r < 0) |
6341 | return bus_log_create_error(r); | |
6342 | ||
e449de87 | 6343 | if (argc < 2) |
ac3efa8a LP |
6344 | r = sd_bus_message_append_strv(m, environ); |
6345 | else { | |
6346 | char **a, **b; | |
6347 | ||
6348 | r = sd_bus_message_open_container(m, 'a', "s"); | |
6349 | if (r < 0) | |
6350 | return bus_log_create_error(r); | |
6351 | ||
e449de87 | 6352 | STRV_FOREACH(a, strv_skip(argv, 1)) { |
ac3efa8a | 6353 | |
2f50e59f LP |
6354 | if (!env_name_is_valid(*a)) |
6355 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Not a valid environment variable name: %s", *a); | |
ac3efa8a LP |
6356 | |
6357 | STRV_FOREACH(b, environ) { | |
6358 | const char *eq; | |
6359 | ||
6360 | eq = startswith(*b, *a); | |
6361 | if (eq && *eq == '=') { | |
6362 | ||
6363 | r = sd_bus_message_append(m, "s", *b); | |
6364 | if (r < 0) | |
6365 | return bus_log_create_error(r); | |
6366 | ||
6367 | break; | |
6368 | } | |
6369 | } | |
6370 | } | |
6371 | ||
6372 | r = sd_bus_message_close_container(m); | |
6373 | } | |
6374 | if (r < 0) | |
6375 | return bus_log_create_error(r); | |
6376 | ||
6377 | r = sd_bus_call(bus, m, 0, &error, NULL); | |
691395d8 LP |
6378 | if (r < 0) |
6379 | return log_error_errno(r, "Failed to import environment: %s", bus_error_message(&error, r)); | |
ac3efa8a LP |
6380 | |
6381 | return 0; | |
6382 | } | |
6383 | ||
cbb13b2a | 6384 | static int enable_sysv_units(const char *verb, char **args) { |
729e3769 | 6385 | int r = 0; |
ee5762e3 | 6386 | |
349cc4a5 | 6387 | #if HAVE_SYSV_COMPAT |
8e766630 | 6388 | _cleanup_(lookup_paths_free) LookupPaths paths = {}; |
e735decc LP |
6389 | unsigned f = 0; |
6390 | ||
6391 | /* Processes all SysV units, and reshuffles the array so that afterwards only the native units remain */ | |
ee5762e3 | 6392 | |
729e3769 LP |
6393 | if (arg_scope != UNIT_FILE_SYSTEM) |
6394 | return 0; | |
ee5762e3 | 6395 | |
b41b9d2a LP |
6396 | if (getenv_bool("SYSTEMCTL_SKIP_SYSV") > 0) |
6397 | return 0; | |
6398 | ||
4c315c2c IS |
6399 | if (!STR_IN_SET(verb, |
6400 | "enable", | |
6401 | "disable", | |
6402 | "is-enabled")) | |
729e3769 | 6403 | return 0; |
ee5762e3 | 6404 | |
4943d143 | 6405 | r = lookup_paths_init(&paths, arg_scope, LOOKUP_PATHS_EXCLUDE_GENERATED, arg_root); |
729e3769 LP |
6406 | if (r < 0) |
6407 | return r; | |
ee5762e3 | 6408 | |
729e3769 | 6409 | r = 0; |
a644abed | 6410 | while (args[f]) { |
e735decc LP |
6411 | |
6412 | const char *argv[] = { | |
6413 | ROOTLIBEXECDIR "/systemd-sysv-install", | |
3483460c LP |
6414 | NULL, /* --root= */ |
6415 | NULL, /* verb */ | |
6416 | NULL, /* service */ | |
e735decc LP |
6417 | NULL, |
6418 | }; | |
6419 | ||
8d1ee648 | 6420 | _cleanup_free_ char *p = NULL, *q = NULL, *l = NULL, *v = NULL; |
729e3769 | 6421 | bool found_native = false, found_sysv; |
e735decc | 6422 | const char *name; |
729e3769 | 6423 | unsigned c = 1; |
729e3769 | 6424 | pid_t pid; |
e735decc | 6425 | int j; |
ee5762e3 | 6426 | |
a644abed | 6427 | name = args[f++]; |
ee5762e3 | 6428 | |
729e3769 LP |
6429 | if (!endswith(name, ".service")) |
6430 | continue; | |
ee5762e3 | 6431 | |
729e3769 LP |
6432 | if (path_is_absolute(name)) |
6433 | continue; | |
ee5762e3 | 6434 | |
e735decc | 6435 | j = unit_file_exists(arg_scope, &paths, name); |
76ec966f | 6436 | if (j < 0 && !IN_SET(j, -ELOOP, -ERFKILL, -EADDRNOTAVAIL)) |
e735decc | 6437 | return log_error_errno(j, "Failed to lookup unit file state: %m"); |
3c6d8e57 | 6438 | found_native = j != 0; |
ee5762e3 | 6439 | |
e735decc LP |
6440 | /* If we have both a native unit and a SysV script, enable/disable them both (below); for is-enabled, |
6441 | * prefer the native unit */ | |
355ff449 | 6442 | if (found_native && streq(verb, "is-enabled")) |
729e3769 | 6443 | continue; |
ee5762e3 | 6444 | |
652ef298 | 6445 | p = path_join(arg_root, SYSTEM_SYSVINIT_PATH, name); |
0c6ea3a4 | 6446 | if (!p) |
60731f32 | 6447 | return log_oom(); |
ee5762e3 | 6448 | |
fbd0b64f | 6449 | p[strlen(p) - STRLEN(".service")] = 0; |
729e3769 | 6450 | found_sysv = access(p, F_OK) >= 0; |
4b6756a8 | 6451 | if (!found_sysv) |
729e3769 | 6452 | continue; |
71fad675 | 6453 | |
689e4e6a CR |
6454 | if (!arg_quiet) { |
6455 | if (found_native) | |
6456 | log_info("Synchronizing state of %s with SysV service script with %s.", name, argv[0]); | |
6457 | else | |
6458 | log_info("%s is not a native service, redirecting to systemd-sysv-install.", name); | |
6459 | } | |
ee5762e3 | 6460 | |
b1dffbb9 | 6461 | if (!isempty(arg_root)) { |
b910cc72 | 6462 | q = strjoin("--root=", arg_root); |
b1dffbb9 LP |
6463 | if (!q) |
6464 | return log_oom(); | |
6465 | ||
6466 | argv[c++] = q; | |
6467 | } | |
ee5762e3 | 6468 | |
8d1ee648 LP |
6469 | /* Let's copy the verb, since it's still pointing directly into the original argv[] array we |
6470 | * got passed, but safe_fork() is likely going to rewrite that for the new child */ | |
6471 | v = strdup(verb); | |
6472 | if (!v) | |
6473 | return log_oom(); | |
6474 | ||
6475 | argv[c++] = v; | |
2b6bf07d | 6476 | argv[c++] = basename(p); |
729e3769 | 6477 | argv[c] = NULL; |
ee5762e3 | 6478 | |
729e3769 | 6479 | l = strv_join((char**)argv, " "); |
60731f32 ZJS |
6480 | if (!l) |
6481 | return log_oom(); | |
ee5762e3 | 6482 | |
8ecc68f4 LP |
6483 | if (!arg_quiet) |
6484 | log_info("Executing: %s", l); | |
ee5762e3 | 6485 | |
0672e2c6 | 6486 | j = safe_fork("(sysv-install)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG, &pid); |
4c253ed1 | 6487 | if (j < 0) |
b6e1fff1 | 6488 | return j; |
4c253ed1 | 6489 | if (j == 0) { |
729e3769 | 6490 | /* Child */ |
729e3769 | 6491 | execv(argv[0], (char**) argv); |
cc7cb0ca | 6492 | log_error_errno(errno, "Failed to execute %s: %m", argv[0]); |
729e3769 LP |
6493 | _exit(EXIT_FAILURE); |
6494 | } | |
ee5762e3 | 6495 | |
2e87a1fd | 6496 | j = wait_for_terminate_and_check("sysv-install", pid, WAIT_LOG_ABNORMAL); |
d710aaf7 | 6497 | if (j < 0) |
2e87a1fd LP |
6498 | return j; |
6499 | if (streq(verb, "is-enabled")) { | |
b4a34311 | 6500 | if (j == EXIT_SUCCESS) { |
2e87a1fd LP |
6501 | if (!arg_quiet) |
6502 | puts("enabled"); | |
6503 | r = 1; | |
6504 | } else { | |
6505 | if (!arg_quiet) | |
6506 | puts("disabled"); | |
6507 | } | |
ee5762e3 | 6508 | |
b4a34311 | 6509 | } else if (j != EXIT_SUCCESS) |
2e87a1fd | 6510 | return -EBADE; /* We don't warn here, under the assumption the script already showed an explanation */ |
ee5762e3 | 6511 | |
355ff449 MP |
6512 | if (found_native) |
6513 | continue; | |
6514 | ||
a644abed | 6515 | /* Remove this entry, so that we don't try enabling it as native unit */ |
aba84331 LP |
6516 | assert(f > 0); |
6517 | f--; | |
6518 | assert(args[f] == name); | |
290cb8e8 | 6519 | strv_remove(args + f, name); |
729e3769 | 6520 | } |
ee5762e3 | 6521 | |
729e3769 LP |
6522 | #endif |
6523 | return r; | |
6524 | } | |
ee5762e3 | 6525 | |
37370d0c | 6526 | static int mangle_names(char **original_names, char ***mangled_names) { |
a33fdebb | 6527 | char **i, **l, **name; |
7410616c | 6528 | int r; |
37370d0c | 6529 | |
7410616c | 6530 | l = i = new(char*, strv_length(original_names) + 1); |
a33fdebb | 6531 | if (!l) |
37370d0c VP |
6532 | return log_oom(); |
6533 | ||
37370d0c | 6534 | STRV_FOREACH(name, original_names) { |
44386fc1 LN |
6535 | |
6536 | /* When enabling units qualified path names are OK, | |
6537 | * too, hence allow them explicitly. */ | |
6538 | ||
7410616c | 6539 | if (is_path(*name)) { |
44386fc1 | 6540 | *i = strdup(*name); |
7410616c LP |
6541 | if (!*i) { |
6542 | strv_free(l); | |
6543 | return log_oom(); | |
6544 | } | |
6545 | } else { | |
37cbc1d5 | 6546 | r = unit_name_mangle(*name, arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN, i); |
7410616c | 6547 | if (r < 0) { |
523f8cde | 6548 | *i = NULL; |
7410616c LP |
6549 | strv_free(l); |
6550 | return log_error_errno(r, "Failed to mangle unit name: %m"); | |
6551 | } | |
a33fdebb LP |
6552 | } |
6553 | ||
6554 | i++; | |
37370d0c | 6555 | } |
a33fdebb LP |
6556 | |
6557 | *i = NULL; | |
6558 | *mangled_names = l; | |
37370d0c VP |
6559 | |
6560 | return 0; | |
6561 | } | |
6562 | ||
decd7982 JR |
6563 | static int normalize_filenames(char **names) { |
6564 | char **u; | |
6565 | int r; | |
6566 | ||
6567 | STRV_FOREACH(u, names) | |
6568 | if (!path_is_absolute(*u)) { | |
6569 | char* normalized_path; | |
6570 | ||
baaa35ad ZJS |
6571 | if (!isempty(arg_root)) |
6572 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
6573 | "Non-absolute paths are not allowed when --root is used: %s", | |
6574 | *u); | |
decd7982 | 6575 | |
baaa35ad ZJS |
6576 | if (!strchr(*u,'/')) |
6577 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
6578 | "Link argument does contain at least one directory separator: %s", | |
6579 | *u); | |
decd7982 JR |
6580 | |
6581 | r = path_make_absolute_cwd(*u, &normalized_path); | |
6582 | if (r < 0) | |
6583 | return r; | |
6584 | ||
6585 | free_and_replace(*u, normalized_path); | |
6586 | } | |
6587 | ||
6588 | return 0; | |
6589 | } | |
6590 | ||
1d3c86c0 MS |
6591 | static int normalize_names(char **names, bool warn_if_path) { |
6592 | char **u; | |
6593 | bool was_path = false; | |
6594 | ||
6595 | STRV_FOREACH(u, names) { | |
6596 | int r; | |
6597 | ||
6598 | if (!is_path(*u)) | |
6599 | continue; | |
6600 | ||
6601 | r = free_and_strdup(u, basename(*u)); | |
6602 | if (r < 0) | |
6603 | return log_error_errno(r, "Failed to normalize unit file path: %m"); | |
6604 | ||
6605 | was_path = true; | |
6606 | } | |
6607 | ||
6608 | if (warn_if_path && was_path) | |
6609 | log_warning("Warning: Can't execute disable on the unit file path. Proceeding with the unit name."); | |
6610 | ||
6611 | return 0; | |
6612 | } | |
6613 | ||
173471b7 | 6614 | static int unit_exists(LookupPaths *lp, const char *unit) { |
d6568222 | 6615 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
f37f8a61 | 6616 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
d6568222 SS |
6617 | _cleanup_free_ char *path = NULL; |
6618 | static const struct bus_properties_map property_map[] = { | |
f37f8a61 YW |
6619 | { "LoadState", "s", NULL, offsetof(UnitStatusInfo, load_state) }, |
6620 | { "ActiveState", "s", NULL, offsetof(UnitStatusInfo, active_state)}, | |
d6568222 SS |
6621 | {}, |
6622 | }; | |
6623 | UnitStatusInfo info = {}; | |
6624 | sd_bus *bus; | |
6625 | int r; | |
6626 | ||
173471b7 ZJS |
6627 | if (unit_name_is_valid(unit, UNIT_NAME_TEMPLATE)) |
6628 | return unit_find_template_path(unit, lp, NULL, NULL); | |
6629 | ||
d6568222 SS |
6630 | path = unit_dbus_path_from_name(unit); |
6631 | if (!path) | |
6632 | return log_oom(); | |
6633 | ||
6634 | r = acquire_bus(BUS_MANAGER, &bus); | |
6635 | if (r < 0) | |
6636 | return r; | |
6637 | ||
a7e4861c | 6638 | r = bus_map_all_properties(bus, "org.freedesktop.systemd1", path, property_map, 0, &error, &m, &info); |
d6568222 SS |
6639 | if (r < 0) |
6640 | return log_error_errno(r, "Failed to get properties: %s", bus_error_message(&error, r)); | |
6641 | ||
d6568222 SS |
6642 | return !streq_ptr(info.load_state, "not-found") || !streq_ptr(info.active_state, "inactive"); |
6643 | } | |
6644 | ||
e449de87 | 6645 | static int enable_unit(int argc, char *argv[], void *userdata) { |
e3e0314b | 6646 | _cleanup_strv_free_ char **names = NULL; |
e449de87 | 6647 | const char *verb = argv[0]; |
729e3769 | 6648 | UnitFileChange *changes = NULL; |
da6053d0 | 6649 | size_t n_changes = 0; |
729e3769 | 6650 | int carries_install_info = -1; |
35b132e8 | 6651 | bool ignore_carries_install_info = arg_quiet; |
729e3769 | 6652 | int r; |
ee5762e3 | 6653 | |
e449de87 | 6654 | if (!argv[1]) |
ab5919fa MS |
6655 | return 0; |
6656 | ||
e449de87 | 6657 | r = mangle_names(strv_skip(argv, 1), &names); |
3a05c0f9 | 6658 | if (r < 0) |
cbb13b2a VP |
6659 | return r; |
6660 | ||
e3e0314b | 6661 | r = enable_sysv_units(verb, names); |
cbb13b2a VP |
6662 | if (r < 0) |
6663 | return r; | |
3a05c0f9 | 6664 | |
c61b443d | 6665 | /* If the operation was fully executed by the SysV compat, let's finish early */ |
823e5fab FB |
6666 | if (strv_isempty(names)) { |
6667 | if (arg_no_reload || install_client_side()) | |
6668 | return 0; | |
6669 | return daemon_reload(argc, argv, userdata); | |
6670 | } | |
67d66210 | 6671 | |
1d3c86c0 MS |
6672 | if (streq(verb, "disable")) { |
6673 | r = normalize_names(names, true); | |
6674 | if (r < 0) | |
6675 | return r; | |
6676 | } | |
6677 | ||
decd7982 JR |
6678 | if (streq(verb, "link")) { |
6679 | r = normalize_filenames(names); | |
6680 | if (r < 0) | |
6681 | return r; | |
6682 | } | |
6683 | ||
4fbd7192 | 6684 | if (install_client_side()) { |
b3796dd8 JS |
6685 | UnitFileFlags flags; |
6686 | ||
6687 | flags = args_to_flags(); | |
729e3769 | 6688 | if (streq(verb, "enable")) { |
b3796dd8 | 6689 | r = unit_file_enable(arg_scope, flags, arg_root, names, &changes, &n_changes); |
729e3769 LP |
6690 | carries_install_info = r; |
6691 | } else if (streq(verb, "disable")) | |
b3796dd8 | 6692 | r = unit_file_disable(arg_scope, flags, arg_root, names, &changes, &n_changes); |
729e3769 | 6693 | else if (streq(verb, "reenable")) { |
b3796dd8 | 6694 | r = unit_file_reenable(arg_scope, flags, arg_root, names, &changes, &n_changes); |
729e3769 LP |
6695 | carries_install_info = r; |
6696 | } else if (streq(verb, "link")) | |
b3796dd8 | 6697 | r = unit_file_link(arg_scope, flags, arg_root, names, &changes, &n_changes); |
729e3769 | 6698 | else if (streq(verb, "preset")) { |
b3796dd8 | 6699 | r = unit_file_preset(arg_scope, flags, arg_root, names, arg_preset_mode, &changes, &n_changes); |
729e3769 | 6700 | } else if (streq(verb, "mask")) |
b3796dd8 | 6701 | r = unit_file_mask(arg_scope, flags, arg_root, names, &changes, &n_changes); |
729e3769 | 6702 | else if (streq(verb, "unmask")) |
b3796dd8 | 6703 | r = unit_file_unmask(arg_scope, flags, arg_root, names, &changes, &n_changes); |
344ca755 LP |
6704 | else if (streq(verb, "revert")) |
6705 | r = unit_file_revert(arg_scope, arg_root, names, &changes, &n_changes); | |
729e3769 LP |
6706 | else |
6707 | assert_not_reached("Unknown verb"); | |
ee5762e3 | 6708 | |
af3d8113 | 6709 | unit_file_dump_changes(r, verb, changes, n_changes, arg_quiet); |
d073dea0 | 6710 | if (r < 0) |
85b78539 | 6711 | goto finish; |
df77cdf0 | 6712 | r = 0; |
729e3769 | 6713 | } else { |
4afd3348 LP |
6714 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL, *m = NULL; |
6715 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
39207373 | 6716 | bool expect_carries_install_info = false; |
344ca755 | 6717 | bool send_runtime = true, send_force = true, send_preset_mode = false; |
718db961 | 6718 | const char *method; |
4fbd7192 | 6719 | sd_bus *bus; |
729e3769 | 6720 | |
d6568222 | 6721 | if (STR_IN_SET(verb, "mask", "unmask")) { |
f8d6cb48 | 6722 | char **name; |
8e766630 | 6723 | _cleanup_(lookup_paths_free) LookupPaths lp = {}; |
173471b7 ZJS |
6724 | |
6725 | r = lookup_paths_init(&lp, arg_scope, 0, arg_root); | |
d6568222 SS |
6726 | if (r < 0) |
6727 | return r; | |
f8d6cb48 ZJS |
6728 | |
6729 | STRV_FOREACH(name, names) { | |
173471b7 | 6730 | r = unit_exists(&lp, *name); |
f8d6cb48 ZJS |
6731 | if (r < 0) |
6732 | return r; | |
6733 | if (r == 0) | |
25c59b5d | 6734 | log_notice("Unit %s does not exist, proceeding anyway.", *name); |
f8d6cb48 | 6735 | } |
d6568222 SS |
6736 | } |
6737 | ||
4fbd7192 LP |
6738 | r = acquire_bus(BUS_MANAGER, &bus); |
6739 | if (r < 0) | |
6740 | return r; | |
6741 | ||
8a4b13c5 | 6742 | polkit_agent_open_maybe(); |
d2ad7e1b | 6743 | |
729e3769 LP |
6744 | if (streq(verb, "enable")) { |
6745 | method = "EnableUnitFiles"; | |
6746 | expect_carries_install_info = true; | |
6747 | } else if (streq(verb, "disable")) { | |
6748 | method = "DisableUnitFiles"; | |
6749 | send_force = false; | |
6750 | } else if (streq(verb, "reenable")) { | |
6751 | method = "ReenableUnitFiles"; | |
6752 | expect_carries_install_info = true; | |
6753 | } else if (streq(verb, "link")) | |
6754 | method = "LinkUnitFiles"; | |
6755 | else if (streq(verb, "preset")) { | |
d309c1c3 LP |
6756 | |
6757 | if (arg_preset_mode != UNIT_FILE_PRESET_FULL) { | |
6758 | method = "PresetUnitFilesWithMode"; | |
6759 | send_preset_mode = true; | |
6760 | } else | |
6761 | method = "PresetUnitFiles"; | |
6762 | ||
729e3769 | 6763 | expect_carries_install_info = true; |
39207373 | 6764 | ignore_carries_install_info = true; |
729e3769 LP |
6765 | } else if (streq(verb, "mask")) |
6766 | method = "MaskUnitFiles"; | |
6767 | else if (streq(verb, "unmask")) { | |
6768 | method = "UnmaskUnitFiles"; | |
6769 | send_force = false; | |
344ca755 LP |
6770 | } else if (streq(verb, "revert")) { |
6771 | method = "RevertUnitFiles"; | |
6772 | send_runtime = send_force = false; | |
729e3769 LP |
6773 | } else |
6774 | assert_not_reached("Unknown verb"); | |
6775 | ||
f459b602 MAP |
6776 | r = sd_bus_message_new_method_call( |
6777 | bus, | |
151b9b96 | 6778 | &m, |
729e3769 LP |
6779 | "org.freedesktop.systemd1", |
6780 | "/org/freedesktop/systemd1", | |
6781 | "org.freedesktop.systemd1.Manager", | |
151b9b96 | 6782 | method); |
f459b602 MAP |
6783 | if (r < 0) |
6784 | return bus_log_create_error(r); | |
ee5762e3 | 6785 | |
e3e0314b | 6786 | r = sd_bus_message_append_strv(m, names); |
f459b602 MAP |
6787 | if (r < 0) |
6788 | return bus_log_create_error(r); | |
ee5762e3 | 6789 | |
d309c1c3 LP |
6790 | if (send_preset_mode) { |
6791 | r = sd_bus_message_append(m, "s", unit_file_preset_mode_to_string(arg_preset_mode)); | |
6792 | if (r < 0) | |
6793 | return bus_log_create_error(r); | |
6794 | } | |
6795 | ||
344ca755 LP |
6796 | if (send_runtime) { |
6797 | r = sd_bus_message_append(m, "b", arg_runtime); | |
6798 | if (r < 0) | |
6799 | return bus_log_create_error(r); | |
6800 | } | |
ee5762e3 | 6801 | |
729e3769 | 6802 | if (send_force) { |
f459b602 MAP |
6803 | r = sd_bus_message_append(m, "b", arg_force); |
6804 | if (r < 0) | |
6805 | return bus_log_create_error(r); | |
ee5762e3 LP |
6806 | } |
6807 | ||
c49b30a2 | 6808 | r = sd_bus_call(bus, m, 0, &error, &reply); |
691395d8 | 6809 | if (r < 0) |
af3d8113 | 6810 | return log_error_errno(r, "Failed to %s unit: %s", verb, bus_error_message(&error, r)); |
be394c48 | 6811 | |
729e3769 | 6812 | if (expect_carries_install_info) { |
f459b602 MAP |
6813 | r = sd_bus_message_read(reply, "b", &carries_install_info); |
6814 | if (r < 0) | |
6815 | return bus_log_parse_error(r); | |
ee5762e3 LP |
6816 | } |
6817 | ||
57ab2eab | 6818 | r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet, &changes, &n_changes); |
f459b602 | 6819 | if (r < 0) |
85b78539 | 6820 | goto finish; |
b77398f7 | 6821 | |
93c941e3 | 6822 | /* Try to reload if enabled */ |
d6cb60c7 | 6823 | if (!arg_no_reload) |
e449de87 | 6824 | r = daemon_reload(argc, argv, userdata); |
f459b602 MAP |
6825 | else |
6826 | r = 0; | |
b647f10d | 6827 | } |
3d3961f2 | 6828 | |
39207373 | 6829 | if (carries_install_info == 0 && !ignore_carries_install_info) |
46efc978 LP |
6830 | log_notice("The unit files have no installation config (WantedBy=, RequiredBy=, Also=,\n" |
6831 | "Alias= settings in the [Install] section, and DefaultInstance= for template\n" | |
2811184a ZJS |
6832 | "units). This means they are not meant to be enabled using systemctl.\n" |
6833 | " \n" /* trick: the space is needed so that the line does not get stripped from output */ | |
46efc978 | 6834 | "Possible reasons for having this kind of units are:\n" |
18c93ee3 LP |
6835 | "%1$s A unit may be statically enabled by being symlinked from another unit's\n" |
6836 | " .wants/ or .requires/ directory.\n" | |
6837 | "%1$s A unit's purpose may be to act as a helper for some other unit which has\n" | |
6838 | " a requirement dependency on it.\n" | |
6839 | "%1$s A unit may be started when needed via activation (socket, path, timer,\n" | |
6840 | " D-Bus, udev, scripted systemctl call, ...).\n" | |
6841 | "%1$s In case of template units, the unit is meant to be enabled with some\n" | |
6842 | " instance name specified.", | |
9a6f746f | 6843 | special_glyph(SPECIAL_GLYPH_BULLET)); |
ee5762e3 | 6844 | |
6bc30691 | 6845 | if (arg_now && STR_IN_SET(argv[0], "enable", "disable", "mask")) { |
4fbd7192 | 6846 | sd_bus *bus; |
da6053d0 | 6847 | size_t len, i; |
57ab2eab | 6848 | |
4fbd7192 LP |
6849 | r = acquire_bus(BUS_MANAGER, &bus); |
6850 | if (r < 0) | |
d073dea0 | 6851 | goto finish; |
4fbd7192 | 6852 | |
6bc30691 JS |
6853 | len = strv_length(names); |
6854 | { | |
6855 | char *new_args[len + 2]; | |
57ab2eab | 6856 | |
6bc30691 JS |
6857 | new_args[0] = (char*) (streq(argv[0], "enable") ? "start" : "stop"); |
6858 | for (i = 0; i < len; i++) | |
6859 | new_args[i + 1] = basename(names[i]); | |
6860 | new_args[i + 1] = NULL; | |
6861 | ||
6862 | r = start_unit(len + 1, new_args, userdata); | |
6863 | } | |
57ab2eab JS |
6864 | } |
6865 | ||
729e3769 | 6866 | finish: |
729e3769 | 6867 | unit_file_changes_free(changes, n_changes); |
ee5762e3 | 6868 | |
729e3769 | 6869 | return r; |
ee5762e3 LP |
6870 | } |
6871 | ||
e449de87 | 6872 | static int add_dependency(int argc, char *argv[], void *userdata) { |
e94937df LN |
6873 | _cleanup_strv_free_ char **names = NULL; |
6874 | _cleanup_free_ char *target = NULL; | |
e449de87 | 6875 | const char *verb = argv[0]; |
acc0269c | 6876 | UnitFileChange *changes = NULL; |
da6053d0 | 6877 | size_t n_changes = 0; |
e94937df LN |
6878 | UnitDependency dep; |
6879 | int r = 0; | |
6880 | ||
e449de87 | 6881 | if (!argv[1]) |
e94937df LN |
6882 | return 0; |
6883 | ||
37cbc1d5 | 6884 | r = unit_name_mangle_with_suffix(argv[1], arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN, ".target", &target); |
7410616c LP |
6885 | if (r < 0) |
6886 | return log_error_errno(r, "Failed to mangle unit name: %m"); | |
e94937df | 6887 | |
e449de87 | 6888 | r = mangle_names(strv_skip(argv, 2), &names); |
e94937df LN |
6889 | if (r < 0) |
6890 | return r; | |
6891 | ||
6892 | if (streq(verb, "add-wants")) | |
6893 | dep = UNIT_WANTS; | |
6894 | else if (streq(verb, "add-requires")) | |
6895 | dep = UNIT_REQUIRES; | |
6896 | else | |
6897 | assert_not_reached("Unknown verb"); | |
6898 | ||
4fbd7192 | 6899 | if (install_client_side()) { |
b3796dd8 | 6900 | r = unit_file_add_dependency(arg_scope, args_to_flags(), arg_root, names, target, dep, &changes, &n_changes); |
af3d8113 | 6901 | unit_file_dump_changes(r, "add dependency on", changes, n_changes, arg_quiet); |
5f056378 CH |
6902 | |
6903 | if (r > 0) | |
6904 | r = 0; | |
e94937df | 6905 | } else { |
4afd3348 LP |
6906 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL, *m = NULL; |
6907 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
4fbd7192 | 6908 | sd_bus *bus; |
e94937df | 6909 | |
4fbd7192 LP |
6910 | r = acquire_bus(BUS_MANAGER, &bus); |
6911 | if (r < 0) | |
6912 | return r; | |
6913 | ||
8a4b13c5 | 6914 | polkit_agent_open_maybe(); |
d2ad7e1b | 6915 | |
e94937df LN |
6916 | r = sd_bus_message_new_method_call( |
6917 | bus, | |
6918 | &m, | |
6919 | "org.freedesktop.systemd1", | |
6920 | "/org/freedesktop/systemd1", | |
6921 | "org.freedesktop.systemd1.Manager", | |
6922 | "AddDependencyUnitFiles"); | |
6923 | if (r < 0) | |
6924 | return bus_log_create_error(r); | |
6925 | ||
342641fb | 6926 | r = sd_bus_message_append_strv(m, names); |
e94937df LN |
6927 | if (r < 0) |
6928 | return bus_log_create_error(r); | |
6929 | ||
342641fb | 6930 | r = sd_bus_message_append(m, "ssbb", target, unit_dependency_to_string(dep), arg_runtime, arg_force); |
e94937df LN |
6931 | if (r < 0) |
6932 | return bus_log_create_error(r); | |
6933 | ||
6934 | r = sd_bus_call(bus, m, 0, &error, &reply); | |
691395d8 | 6935 | if (r < 0) |
af3d8113 | 6936 | return log_error_errno(r, "Failed to add dependency: %s", bus_error_message(&error, r)); |
e94937df | 6937 | |
acc0269c | 6938 | r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet, &changes, &n_changes); |
e94937df | 6939 | if (r < 0) |
acc0269c | 6940 | goto finish; |
e94937df | 6941 | |
acc0269c CH |
6942 | if (arg_no_reload) { |
6943 | r = 0; | |
6944 | goto finish; | |
6945 | } | |
6946 | ||
6947 | r = daemon_reload(argc, argv, userdata); | |
e94937df | 6948 | } |
acc0269c CH |
6949 | |
6950 | finish: | |
6951 | unit_file_changes_free(changes, n_changes); | |
6952 | ||
6953 | return r; | |
e94937df LN |
6954 | } |
6955 | ||
e449de87 | 6956 | static int preset_all(int argc, char *argv[], void *userdata) { |
acc0269c | 6957 | UnitFileChange *changes = NULL; |
da6053d0 | 6958 | size_t n_changes = 0; |
d309c1c3 LP |
6959 | int r; |
6960 | ||
4fbd7192 | 6961 | if (install_client_side()) { |
b3796dd8 | 6962 | r = unit_file_preset_all(arg_scope, args_to_flags(), arg_root, arg_preset_mode, &changes, &n_changes); |
af3d8113 | 6963 | unit_file_dump_changes(r, "preset", changes, n_changes, arg_quiet); |
5f056378 CH |
6964 | |
6965 | if (r > 0) | |
6966 | r = 0; | |
d309c1c3 | 6967 | } else { |
4afd3348 LP |
6968 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
6969 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
4fbd7192 | 6970 | sd_bus *bus; |
d309c1c3 | 6971 | |
4fbd7192 LP |
6972 | r = acquire_bus(BUS_MANAGER, &bus); |
6973 | if (r < 0) | |
6974 | return r; | |
6975 | ||
8a4b13c5 | 6976 | polkit_agent_open_maybe(); |
d2ad7e1b | 6977 | |
6e646d22 | 6978 | r = sd_bus_call_method( |
d309c1c3 LP |
6979 | bus, |
6980 | "org.freedesktop.systemd1", | |
6981 | "/org/freedesktop/systemd1", | |
6982 | "org.freedesktop.systemd1.Manager", | |
6e646d22 LP |
6983 | "PresetAllUnitFiles", |
6984 | &error, | |
6985 | &reply, | |
d309c1c3 LP |
6986 | "sbb", |
6987 | unit_file_preset_mode_to_string(arg_preset_mode), | |
6988 | arg_runtime, | |
6989 | arg_force); | |
691395d8 | 6990 | if (r < 0) |
af3d8113 | 6991 | return log_error_errno(r, "Failed to preset all units: %s", bus_error_message(&error, r)); |
d309c1c3 | 6992 | |
acc0269c | 6993 | r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet, &changes, &n_changes); |
d309c1c3 | 6994 | if (r < 0) |
acc0269c | 6995 | goto finish; |
d309c1c3 | 6996 | |
acc0269c CH |
6997 | if (arg_no_reload) { |
6998 | r = 0; | |
6999 | goto finish; | |
7000 | } | |
7001 | ||
7002 | r = daemon_reload(argc, argv, userdata); | |
d309c1c3 | 7003 | } |
acc0269c CH |
7004 | |
7005 | finish: | |
7006 | unit_file_changes_free(changes, n_changes); | |
7007 | ||
7008 | return r; | |
d309c1c3 LP |
7009 | } |
7010 | ||
3b3557c4 JS |
7011 | static int show_installation_targets_client_side(const char *name) { |
7012 | UnitFileChange *changes = NULL; | |
da6053d0 | 7013 | size_t n_changes = 0, i; |
3b3557c4 JS |
7014 | UnitFileFlags flags; |
7015 | char **p; | |
7016 | int r; | |
7017 | ||
7018 | p = STRV_MAKE(name); | |
7019 | flags = UNIT_FILE_DRY_RUN | | |
7020 | (arg_runtime ? UNIT_FILE_RUNTIME : 0); | |
7021 | ||
7022 | r = unit_file_disable(UNIT_FILE_SYSTEM, flags, NULL, p, &changes, &n_changes); | |
7023 | if (r < 0) | |
7024 | return log_error_errno(r, "Failed to get file links for %s: %m", name); | |
7025 | ||
7026 | for (i = 0; i < n_changes; i++) | |
7027 | if (changes[i].type == UNIT_FILE_UNLINK) | |
7028 | printf(" %s\n", changes[i].path); | |
7029 | ||
7030 | return 0; | |
7031 | } | |
7032 | ||
7033 | static int show_installation_targets(sd_bus *bus, const char *name) { | |
7034 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
7035 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
7036 | const char *link; | |
7037 | int r; | |
7038 | ||
7039 | r = sd_bus_call_method( | |
7040 | bus, | |
7041 | "org.freedesktop.systemd1", | |
7042 | "/org/freedesktop/systemd1", | |
7043 | "org.freedesktop.systemd1.Manager", | |
7044 | "GetUnitFileLinks", | |
7045 | &error, | |
7046 | &reply, | |
7047 | "sb", name, arg_runtime); | |
7048 | if (r < 0) | |
7049 | return log_error_errno(r, "Failed to get unit file links for %s: %s", name, bus_error_message(&error, r)); | |
7050 | ||
7051 | r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "s"); | |
7052 | if (r < 0) | |
7053 | return bus_log_parse_error(r); | |
7054 | ||
7055 | while ((r = sd_bus_message_read(reply, "s", &link)) > 0) | |
7056 | printf(" %s\n", link); | |
7057 | ||
7058 | if (r < 0) | |
7059 | return bus_log_parse_error(r); | |
7060 | ||
7061 | r = sd_bus_message_exit_container(reply); | |
7062 | if (r < 0) | |
7063 | return bus_log_parse_error(r); | |
7064 | ||
7065 | return 0; | |
7066 | } | |
7067 | ||
e449de87 | 7068 | static int unit_is_enabled(int argc, char *argv[], void *userdata) { |
e3e0314b | 7069 | _cleanup_strv_free_ char **names = NULL; |
729e3769 LP |
7070 | bool enabled; |
7071 | char **name; | |
f459b602 | 7072 | int r; |
ee5762e3 | 7073 | |
e449de87 | 7074 | r = mangle_names(strv_skip(argv, 1), &names); |
cbb13b2a VP |
7075 | if (r < 0) |
7076 | return r; | |
7077 | ||
e449de87 | 7078 | r = enable_sysv_units(argv[0], names); |
729e3769 LP |
7079 | if (r < 0) |
7080 | return r; | |
ee5762e3 | 7081 | |
729e3769 | 7082 | enabled = r > 0; |
ee5762e3 | 7083 | |
4fbd7192 | 7084 | if (install_client_side()) { |
e3e0314b | 7085 | STRV_FOREACH(name, names) { |
729e3769 | 7086 | UnitFileState state; |
ee5762e3 | 7087 | |
0ec0deaa LP |
7088 | r = unit_file_get_state(arg_scope, arg_root, *name, &state); |
7089 | if (r < 0) | |
c5024cd0 | 7090 | return log_error_errno(r, "Failed to get unit file state for %s: %m", *name); |
ee5762e3 | 7091 | |
4c315c2c IS |
7092 | if (IN_SET(state, |
7093 | UNIT_FILE_ENABLED, | |
7094 | UNIT_FILE_ENABLED_RUNTIME, | |
7095 | UNIT_FILE_STATIC, | |
f4139308 LP |
7096 | UNIT_FILE_INDIRECT, |
7097 | UNIT_FILE_GENERATED)) | |
729e3769 LP |
7098 | enabled = true; |
7099 | ||
3b3557c4 | 7100 | if (!arg_quiet) { |
729e3769 | 7101 | puts(unit_file_state_to_string(state)); |
3b3557c4 JS |
7102 | if (arg_full) { |
7103 | r = show_installation_targets_client_side(*name); | |
7104 | if (r < 0) | |
7105 | return r; | |
7106 | } | |
7107 | } | |
71fad675 | 7108 | } |
ee5762e3 | 7109 | |
5f056378 | 7110 | r = 0; |
729e3769 | 7111 | } else { |
4afd3348 | 7112 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
4fbd7192 LP |
7113 | sd_bus *bus; |
7114 | ||
7115 | r = acquire_bus(BUS_MANAGER, &bus); | |
7116 | if (r < 0) | |
7117 | return r; | |
7118 | ||
e3e0314b | 7119 | STRV_FOREACH(name, names) { |
4afd3348 | 7120 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
729e3769 | 7121 | const char *s; |
63a723f3 | 7122 | |
f459b602 | 7123 | r = sd_bus_call_method( |
f22f08cd | 7124 | bus, |
729e3769 LP |
7125 | "org.freedesktop.systemd1", |
7126 | "/org/freedesktop/systemd1", | |
7127 | "org.freedesktop.systemd1.Manager", | |
f22f08cd | 7128 | "GetUnitFileState", |
f459b602 | 7129 | &error, |
f22f08cd | 7130 | &reply, |
04504f93 | 7131 | "s", *name); |
691395d8 LP |
7132 | if (r < 0) |
7133 | return log_error_errno(r, "Failed to get unit file state for %s: %s", *name, bus_error_message(&error, r)); | |
ee5762e3 | 7134 | |
f459b602 MAP |
7135 | r = sd_bus_message_read(reply, "s", &s); |
7136 | if (r < 0) | |
7137 | return bus_log_parse_error(r); | |
ee5762e3 | 7138 | |
f4139308 | 7139 | if (STR_IN_SET(s, "enabled", "enabled-runtime", "static", "indirect", "generated")) |
729e3769 LP |
7140 | enabled = true; |
7141 | ||
3b3557c4 | 7142 | if (!arg_quiet) { |
729e3769 | 7143 | puts(s); |
3b3557c4 JS |
7144 | if (arg_full) { |
7145 | r = show_installation_targets(bus, *name); | |
7146 | if (r < 0) | |
7147 | return r; | |
7148 | } | |
7149 | } | |
560d8f23 | 7150 | } |
ee5762e3 LP |
7151 | } |
7152 | ||
f4139308 | 7153 | return enabled ? EXIT_SUCCESS : EXIT_FAILURE; |
ee5762e3 LP |
7154 | } |
7155 | ||
adb6cd9b FB |
7156 | static int match_startup_finished(sd_bus_message *m, void *userdata, sd_bus_error *error) { |
7157 | char **state = userdata; | |
7158 | int r; | |
7159 | ||
7160 | assert(state); | |
7161 | ||
7162 | r = sd_bus_get_property_string( | |
7163 | sd_bus_message_get_bus(m), | |
7164 | "org.freedesktop.systemd1", | |
7165 | "/org/freedesktop/systemd1", | |
7166 | "org.freedesktop.systemd1.Manager", | |
7167 | "SystemState", | |
7168 | NULL, | |
7169 | state); | |
7170 | ||
7171 | sd_event_exit(sd_bus_get_event(sd_bus_message_get_bus(m)), r); | |
7172 | return 0; | |
7173 | } | |
7174 | ||
e449de87 | 7175 | static int is_system_running(int argc, char *argv[], void *userdata) { |
e686a616 | 7176 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
adb6cd9b FB |
7177 | _cleanup_(sd_bus_slot_unrefp) sd_bus_slot *slot_startup_finished = NULL; |
7178 | _cleanup_(sd_event_unrefp) sd_event* event = NULL; | |
99813a19 | 7179 | _cleanup_free_ char *state = NULL; |
4fbd7192 | 7180 | sd_bus *bus; |
99813a19 LP |
7181 | int r; |
7182 | ||
040524b4 | 7183 | if (running_in_chroot() > 0 || (arg_transport == BUS_TRANSPORT_LOCAL && !sd_booted())) { |
94f099d8 LP |
7184 | if (!arg_quiet) |
7185 | puts("offline"); | |
7186 | return EXIT_FAILURE; | |
7187 | } | |
7188 | ||
4fbd7192 LP |
7189 | r = acquire_bus(BUS_MANAGER, &bus); |
7190 | if (r < 0) | |
7191 | return r; | |
7192 | ||
adb6cd9b FB |
7193 | if (arg_wait) { |
7194 | r = sd_event_default(&event); | |
7195 | if (r >= 0) | |
7196 | r = sd_bus_attach_event(bus, event, 0); | |
7197 | if (r >= 0) | |
7198 | r = sd_bus_match_signal_async( | |
7199 | bus, | |
7200 | &slot_startup_finished, | |
7201 | "org.freedesktop.systemd1", | |
7202 | "/org/freedesktop/systemd1", | |
7203 | "org.freedesktop.systemd1.Manager", | |
7204 | "StartupFinished", | |
7205 | match_startup_finished, NULL, &state); | |
7206 | if (r < 0) { | |
7207 | log_warning_errno(r, "Failed to request match for StartupFinished: %m"); | |
7208 | arg_wait = false; | |
7209 | } | |
7210 | } | |
7211 | ||
99813a19 LP |
7212 | r = sd_bus_get_property_string( |
7213 | bus, | |
7214 | "org.freedesktop.systemd1", | |
7215 | "/org/freedesktop/systemd1", | |
7216 | "org.freedesktop.systemd1.Manager", | |
7217 | "SystemState", | |
e686a616 | 7218 | &error, |
99813a19 LP |
7219 | &state); |
7220 | if (r < 0) { | |
adb6cd9b | 7221 | log_warning_errno(r, "Failed to query system state: %s", bus_error_message(&error, r)); |
e686a616 | 7222 | |
99813a19 LP |
7223 | if (!arg_quiet) |
7224 | puts("unknown"); | |
bfeec178 | 7225 | return EXIT_FAILURE; |
99813a19 LP |
7226 | } |
7227 | ||
adb6cd9b FB |
7228 | if (arg_wait && STR_IN_SET(state, "initializing", "starting")) { |
7229 | r = sd_event_loop(event); | |
7230 | if (r < 0) { | |
7231 | log_warning_errno(r, "Failed to get property from event loop: %m"); | |
7232 | if (!arg_quiet) | |
7233 | puts("unknown"); | |
7234 | return EXIT_FAILURE; | |
7235 | } | |
7236 | } | |
7237 | ||
99813a19 LP |
7238 | if (!arg_quiet) |
7239 | puts(state); | |
7240 | ||
7241 | return streq(state, "running") ? EXIT_SUCCESS : EXIT_FAILURE; | |
7242 | } | |
7243 | ||
7d4fb3b1 | 7244 | static int create_edit_temp_file(const char *new_path, const char *original_path, char **ret_tmp_fn) { |
45519fd6 | 7245 | _cleanup_free_ char *t = NULL; |
ae6c3cc0 | 7246 | int r; |
7d4fb3b1 RC |
7247 | |
7248 | assert(new_path); | |
7249 | assert(original_path); | |
7250 | assert(ret_tmp_fn); | |
7251 | ||
14bcf25c | 7252 | r = tempfn_random(new_path, NULL, &t); |
ae6c3cc0 | 7253 | if (r < 0) |
029009d4 | 7254 | return log_error_errno(r, "Failed to determine temporary filename for \"%s\": %m", new_path); |
7d4fb3b1 RC |
7255 | |
7256 | r = mkdir_parents(new_path, 0755); | |
45519fd6 | 7257 | if (r < 0) |
691395d8 | 7258 | return log_error_errno(r, "Failed to create directories for \"%s\": %m", new_path); |
7d4fb3b1 | 7259 | |
8a016c74 | 7260 | r = copy_file(original_path, t, 0, 0644, 0, 0, COPY_REFLINK); |
7d4fb3b1 | 7261 | if (r == -ENOENT) { |
45519fd6 | 7262 | |
7d4fb3b1 | 7263 | r = touch(t); |
45519fd6 LP |
7264 | if (r < 0) |
7265 | return log_error_errno(r, "Failed to create temporary file \"%s\": %m", t); | |
7266 | ||
7267 | } else if (r < 0) | |
39c38ce1 | 7268 | return log_error_errno(r, "Failed to create temporary file for \"%s\": %m", new_path); |
7d4fb3b1 | 7269 | |
ae2a15bc | 7270 | *ret_tmp_fn = TAKE_PTR(t); |
7d4fb3b1 RC |
7271 | |
7272 | return 0; | |
7273 | } | |
7274 | ||
c51932be LP |
7275 | static int get_file_to_edit( |
7276 | const LookupPaths *paths, | |
7277 | const char *name, | |
7278 | char **ret_path) { | |
7279 | ||
7280 | _cleanup_free_ char *path = NULL, *run = NULL; | |
7d4fb3b1 | 7281 | |
45519fd6 LP |
7282 | assert(name); |
7283 | assert(ret_path); | |
7284 | ||
657ee2d8 | 7285 | path = path_join(paths->persistent_config, name); |
c51932be | 7286 | if (!path) |
7d4fb3b1 RC |
7287 | return log_oom(); |
7288 | ||
c51932be | 7289 | if (arg_runtime) { |
657ee2d8 | 7290 | run = path_join(paths->runtime_config, name); |
c51932be LP |
7291 | if (!run) |
7292 | return log_oom(); | |
7293 | } | |
7294 | ||
bc854dc7 | 7295 | if (arg_runtime) { |
baaa35ad ZJS |
7296 | if (access(path, F_OK) >= 0) |
7297 | return log_error_errno(SYNTHETIC_ERRNO(EEXIST), | |
7298 | "Refusing to create \"%s\" because it would be overridden by \"%s\" anyway.", | |
7299 | run, path); | |
691395d8 | 7300 | |
ae2a15bc LP |
7301 | *ret_path = TAKE_PTR(run); |
7302 | } else | |
7303 | *ret_path = TAKE_PTR(path); | |
7d4fb3b1 RC |
7304 | |
7305 | return 0; | |
7306 | } | |
7307 | ||
39c38ce1 | 7308 | static int unit_file_create_new( |
c51932be LP |
7309 | const LookupPaths *paths, |
7310 | const char *unit_name, | |
39c38ce1 | 7311 | const char *suffix, |
c51932be LP |
7312 | char **ret_new_path, |
7313 | char **ret_tmp_path) { | |
7314 | ||
919d2720 LP |
7315 | _cleanup_free_ char *new_path = NULL, *tmp_path = NULL; |
7316 | const char *ending; | |
7d4fb3b1 RC |
7317 | int r; |
7318 | ||
7319 | assert(unit_name); | |
7320 | assert(ret_new_path); | |
7321 | assert(ret_tmp_path); | |
7322 | ||
39c38ce1 | 7323 | ending = strjoina(unit_name, suffix); |
919d2720 | 7324 | r = get_file_to_edit(paths, ending, &new_path); |
7d4fb3b1 RC |
7325 | if (r < 0) |
7326 | return r; | |
7327 | ||
919d2720 LP |
7328 | r = create_edit_temp_file(new_path, new_path, &tmp_path); |
7329 | if (r < 0) | |
7d4fb3b1 | 7330 | return r; |
7d4fb3b1 | 7331 | |
919d2720 LP |
7332 | *ret_new_path = TAKE_PTR(new_path); |
7333 | *ret_tmp_path = TAKE_PTR(tmp_path); | |
7d4fb3b1 RC |
7334 | |
7335 | return 0; | |
7336 | } | |
7337 | ||
1cfa9a4c | 7338 | static int unit_file_create_copy( |
c51932be | 7339 | const LookupPaths *paths, |
1cfa9a4c LP |
7340 | const char *unit_name, |
7341 | const char *fragment_path, | |
1cfa9a4c LP |
7342 | char **ret_new_path, |
7343 | char **ret_tmp_path) { | |
7344 | ||
92d6b0bf | 7345 | _cleanup_free_ char *new_path = NULL, *tmp_path = NULL; |
7d4fb3b1 RC |
7346 | int r; |
7347 | ||
7348 | assert(fragment_path); | |
7349 | assert(unit_name); | |
7350 | assert(ret_new_path); | |
7351 | assert(ret_tmp_path); | |
7352 | ||
92d6b0bf | 7353 | r = get_file_to_edit(paths, unit_name, &new_path); |
7d4fb3b1 RC |
7354 | if (r < 0) |
7355 | return r; | |
7356 | ||
92d6b0bf | 7357 | if (!path_equal(fragment_path, new_path) && access(new_path, F_OK) >= 0) { |
7d4fb3b1 RC |
7358 | char response; |
7359 | ||
92d6b0bf LP |
7360 | r = ask_char(&response, "yn", "\"%s\" already exists. Overwrite with \"%s\"? [(y)es, (n)o] ", new_path, fragment_path); |
7361 | if (r < 0) | |
7d4fb3b1 | 7362 | return r; |
5daacba2 LP |
7363 | if (response != 'y') |
7364 | return log_warning_errno(SYNTHETIC_ERRNO(EKEYREJECTED), "%s skipped.", unit_name); | |
7d4fb3b1 RC |
7365 | } |
7366 | ||
92d6b0bf LP |
7367 | r = create_edit_temp_file(new_path, fragment_path, &tmp_path); |
7368 | if (r < 0) | |
7d4fb3b1 | 7369 | return r; |
7d4fb3b1 | 7370 | |
92d6b0bf LP |
7371 | *ret_new_path = TAKE_PTR(new_path); |
7372 | *ret_tmp_path = TAKE_PTR(tmp_path); | |
7d4fb3b1 RC |
7373 | |
7374 | return 0; | |
7375 | } | |
7376 | ||
7377 | static int run_editor(char **paths) { | |
7d4fb3b1 RC |
7378 | int r; |
7379 | ||
7380 | assert(paths); | |
7381 | ||
0672e2c6 | 7382 | r = safe_fork("(editor)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG|FORK_WAIT, NULL); |
4c253ed1 | 7383 | if (r < 0) |
b6e1fff1 | 7384 | return r; |
4c253ed1 | 7385 | if (r == 0) { |
b2238e38 | 7386 | char **editor_args = NULL, **tmp_path, **original_path; |
70d6e5bd | 7387 | size_t n_editor_args = 0, i = 1, argc; |
b2238e38 | 7388 | const char **args, *editor, *p; |
7d4fb3b1 RC |
7389 | |
7390 | argc = strv_length(paths)/2 + 1; | |
7d4fb3b1 RC |
7391 | |
7392 | /* SYSTEMD_EDITOR takes precedence over EDITOR which takes precedence over VISUAL | |
7393 | * If neither SYSTEMD_EDITOR nor EDITOR nor VISUAL are present, | |
7394 | * we try to execute well known editors | |
7395 | */ | |
7396 | editor = getenv("SYSTEMD_EDITOR"); | |
7397 | if (!editor) | |
7398 | editor = getenv("EDITOR"); | |
7399 | if (!editor) | |
7400 | editor = getenv("VISUAL"); | |
7401 | ||
7402 | if (!isempty(editor)) { | |
9ef5d8f2 JS |
7403 | editor_args = strv_split(editor, WHITESPACE); |
7404 | if (!editor_args) { | |
7405 | (void) log_oom(); | |
7406 | _exit(EXIT_FAILURE); | |
7407 | } | |
7408 | n_editor_args = strv_length(editor_args); | |
7409 | argc += n_editor_args - 1; | |
7410 | } | |
02a126a3 | 7411 | |
9ef5d8f2 JS |
7412 | args = newa(const char*, argc + 1); |
7413 | ||
7414 | if (n_editor_args > 0) { | |
7415 | args[0] = editor_args[0]; | |
7416 | for (; i < n_editor_args; i++) | |
7417 | args[i] = editor_args[i]; | |
7418 | } | |
7419 | ||
df79fdab LP |
7420 | STRV_FOREACH_PAIR(original_path, tmp_path, paths) |
7421 | args[i++] = *tmp_path; | |
9ef5d8f2 JS |
7422 | args[i] = NULL; |
7423 | ||
7424 | if (n_editor_args > 0) | |
7425 | execvp(args[0], (char* const*) args); | |
7d4fb3b1 | 7426 | |
9391a1c3 | 7427 | FOREACH_STRING(p, "editor", "nano", "vim", "vi") { |
1cfa9a4c LP |
7428 | args[0] = p; |
7429 | execvp(p, (char* const*) args); | |
7d4fb3b1 RC |
7430 | /* We do not fail if the editor doesn't exist |
7431 | * because we want to try each one of them before | |
7432 | * failing. | |
7433 | */ | |
7434 | if (errno != ENOENT) { | |
691395d8 | 7435 | log_error_errno(errno, "Failed to execute %s: %m", editor); |
7d4fb3b1 RC |
7436 | _exit(EXIT_FAILURE); |
7437 | } | |
7438 | } | |
7439 | ||
1cfa9a4c | 7440 | log_error("Cannot edit unit(s), no editor available. Please set either $SYSTEMD_EDITOR, $EDITOR or $VISUAL."); |
7d4fb3b1 RC |
7441 | _exit(EXIT_FAILURE); |
7442 | } | |
7443 | ||
45519fd6 | 7444 | return 0; |
7d4fb3b1 RC |
7445 | } |
7446 | ||
7447 | static int find_paths_to_edit(sd_bus *bus, char **names, char ***paths) { | |
8e766630 | 7448 | _cleanup_(lookup_paths_free) LookupPaths lp = {}; |
7d4fb3b1 RC |
7449 | char **name; |
7450 | int r; | |
7451 | ||
7452 | assert(names); | |
7453 | assert(paths); | |
7454 | ||
4943d143 | 7455 | r = lookup_paths_init(&lp, arg_scope, 0, arg_root); |
5b013a2f | 7456 | if (r < 0) |
8df18507 | 7457 | return r; |
7d4fb3b1 | 7458 | |
e9e310f8 | 7459 | STRV_FOREACH(name, names) { |
b74df547 ZJS |
7460 | _cleanup_free_ char *path = NULL, *new_path = NULL, *tmp_path = NULL, *tmp_name = NULL; |
7461 | const char *unit_name; | |
7d4fb3b1 | 7462 | |
02a126a3 LP |
7463 | r = unit_find_paths(bus, *name, &lp, false, &path, NULL); |
7464 | if (r == -EKEYREJECTED) { | |
7465 | /* If loading of the unit failed server side complete, then the server won't tell us the unit | |
7466 | * file path. In that case, find the file client side. */ | |
7467 | log_debug_errno(r, "Unit '%s' was not loaded correctly, retrying client-side.", *name); | |
7468 | r = unit_find_paths(bus, *name, &lp, true, &path, NULL); | |
7469 | } | |
7470 | if (r == -ERFKILL) | |
7471 | return log_error_errno(r, "Unit '%s' masked, cannot edit.", *name); | |
e9e310f8 RC |
7472 | if (r < 0) |
7473 | return r; | |
b3734d98 ZJS |
7474 | |
7475 | if (r == 0) { | |
7476 | assert(!path); | |
7477 | ||
7478 | if (!arg_force) { | |
85163756 LP |
7479 | log_info("Run 'systemctl edit%s --force --full %s' to create a new unit.", |
7480 | arg_scope == UNIT_FILE_GLOBAL ? " --global" : | |
7481 | arg_scope == UNIT_FILE_USER ? " --user" : "", | |
7482 | *name); | |
39c38ce1 DC |
7483 | return -ENOENT; |
7484 | } | |
39c38ce1 | 7485 | |
b3734d98 ZJS |
7486 | /* Create a new unit from scratch */ |
7487 | unit_name = *name; | |
7488 | r = unit_file_create_new(&lp, unit_name, | |
7489 | arg_full ? NULL : ".d/override.conf", | |
7490 | &new_path, &tmp_path); | |
7491 | } else { | |
7492 | assert(path); | |
b74df547 | 7493 | |
b3734d98 ZJS |
7494 | unit_name = basename(path); |
7495 | /* We follow unit aliases, but we need to propagate the instance */ | |
7496 | if (unit_name_is_valid(*name, UNIT_NAME_INSTANCE) && | |
7497 | unit_name_is_valid(unit_name, UNIT_NAME_TEMPLATE)) { | |
7498 | _cleanup_free_ char *instance = NULL; | |
b74df547 | 7499 | |
b3734d98 ZJS |
7500 | r = unit_name_to_instance(*name, &instance); |
7501 | if (r < 0) | |
7502 | return r; | |
b74df547 | 7503 | |
b3734d98 ZJS |
7504 | r = unit_name_replace_instance(unit_name, instance, &tmp_name); |
7505 | if (r < 0) | |
7506 | return r; | |
7507 | ||
7508 | unit_name = tmp_name; | |
7509 | } | |
b74df547 | 7510 | |
39c38ce1 | 7511 | if (arg_full) |
b74df547 | 7512 | r = unit_file_create_copy(&lp, unit_name, path, &new_path, &tmp_path); |
39c38ce1 | 7513 | else |
b74df547 | 7514 | r = unit_file_create_new(&lp, unit_name, ".d/override.conf", &new_path, &tmp_path); |
b3734d98 | 7515 | } |
e9e310f8 | 7516 | if (r < 0) |
ad2a0358 | 7517 | return r; |
7d4fb3b1 | 7518 | |
e9e310f8 RC |
7519 | r = strv_push_pair(paths, new_path, tmp_path); |
7520 | if (r < 0) | |
7521 | return log_oom(); | |
02a126a3 | 7522 | |
78df0edc | 7523 | new_path = tmp_path = NULL; |
7d4fb3b1 RC |
7524 | } |
7525 | ||
7526 | return 0; | |
7527 | } | |
7528 | ||
e449de87 | 7529 | static int edit(int argc, char *argv[], void *userdata) { |
86f004fb | 7530 | _cleanup_(lookup_paths_free) LookupPaths lp = {}; |
7d4fb3b1 RC |
7531 | _cleanup_strv_free_ char **names = NULL; |
7532 | _cleanup_strv_free_ char **paths = NULL; | |
7533 | char **original, **tmp; | |
4fbd7192 | 7534 | sd_bus *bus; |
7d4fb3b1 RC |
7535 | int r; |
7536 | ||
2f50e59f LP |
7537 | if (!on_tty()) |
7538 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot edit units if not on a tty."); | |
7d4fb3b1 | 7539 | |
2f50e59f LP |
7540 | if (arg_transport != BUS_TRANSPORT_LOCAL) |
7541 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot edit units remotely."); | |
7d4fb3b1 | 7542 | |
86f004fb ZJS |
7543 | r = lookup_paths_init(&lp, arg_scope, 0, arg_root); |
7544 | if (r < 0) | |
7545 | return log_error_errno(r, "Failed to determine unit paths: %m"); | |
7546 | ||
4fbd7192 LP |
7547 | r = acquire_bus(BUS_MANAGER, &bus); |
7548 | if (r < 0) | |
7549 | return r; | |
7550 | ||
e449de87 | 7551 | r = expand_names(bus, strv_skip(argv, 1), NULL, &names); |
7d4fb3b1 RC |
7552 | if (r < 0) |
7553 | return log_error_errno(r, "Failed to expand names: %m"); | |
7554 | ||
9d9dd746 | 7555 | STRV_FOREACH(tmp, names) { |
86f004fb | 7556 | r = unit_is_masked(bus, &lp, *tmp); |
9d9dd746 ZJS |
7557 | if (r < 0) |
7558 | return r; | |
2f50e59f LP |
7559 | if (r > 0) |
7560 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot edit %s: unit is masked.", *tmp); | |
9d9dd746 ZJS |
7561 | } |
7562 | ||
7d4fb3b1 RC |
7563 | r = find_paths_to_edit(bus, names, &paths); |
7564 | if (r < 0) | |
7565 | return r; | |
7566 | ||
b5e6a600 | 7567 | if (strv_isempty(paths)) |
7d4fb3b1 | 7568 | return -ENOENT; |
7d4fb3b1 RC |
7569 | |
7570 | r = run_editor(paths); | |
7571 | if (r < 0) | |
7572 | goto end; | |
7573 | ||
7574 | STRV_FOREACH_PAIR(original, tmp, paths) { | |
9d9dd746 ZJS |
7575 | /* If the temporary file is empty we ignore it. |
7576 | * This allows the user to cancel the modification. | |
7d4fb3b1 RC |
7577 | */ |
7578 | if (null_or_empty_path(*tmp)) { | |
45519fd6 | 7579 | log_warning("Editing \"%s\" canceled: temporary file is empty.", *original); |
7d4fb3b1 RC |
7580 | continue; |
7581 | } | |
45519fd6 | 7582 | |
7d4fb3b1 RC |
7583 | r = rename(*tmp, *original); |
7584 | if (r < 0) { | |
029009d4 | 7585 | r = log_error_errno(errno, "Failed to rename \"%s\" to \"%s\": %m", *tmp, *original); |
7d4fb3b1 RC |
7586 | goto end; |
7587 | } | |
7588 | } | |
7589 | ||
45519fd6 LP |
7590 | r = 0; |
7591 | ||
7592 | if (!arg_no_reload && !install_client_side()) | |
e449de87 | 7593 | r = daemon_reload(argc, argv, userdata); |
7d4fb3b1 RC |
7594 | |
7595 | end: | |
5f18271e | 7596 | STRV_FOREACH_PAIR(original, tmp, paths) { |
45519fd6 | 7597 | (void) unlink(*tmp); |
7d4fb3b1 | 7598 | |
5f18271e RC |
7599 | /* Removing empty dropin dirs */ |
7600 | if (!arg_full) { | |
043717f9 RC |
7601 | _cleanup_free_ char *dir; |
7602 | ||
7603 | dir = dirname_malloc(*original); | |
7604 | if (!dir) | |
7605 | return log_oom(); | |
7606 | ||
5f18271e RC |
7607 | /* no need to check if the dir is empty, rmdir |
7608 | * does nothing if it is not the case. | |
7609 | */ | |
7610 | (void) rmdir(dir); | |
7611 | } | |
7612 | } | |
7613 | ||
7d4fb3b1 RC |
7614 | return r; |
7615 | } | |
7616 | ||
37ec0fdd LP |
7617 | static int systemctl_help(void) { |
7618 | _cleanup_free_ char *link = NULL; | |
7619 | int r; | |
7620 | ||
0221d68a | 7621 | (void) pager_open(arg_pager_flags); |
729e3769 | 7622 | |
37ec0fdd LP |
7623 | r = terminal_urlify_man("systemctl", "1", &link); |
7624 | if (r < 0) | |
7625 | return log_oom(); | |
7626 | ||
8e543004 | 7627 | printf("%1$s [OPTIONS...] {COMMAND} ...\n\n" |
729e3769 | 7628 | "Query or send control commands to the systemd manager.\n\n" |
8a0867d6 LP |
7629 | " -h --help Show this help\n" |
7630 | " --version Show package version\n" | |
f459b602 MAP |
7631 | " --system Connect to system manager\n" |
7632 | " --user Connect to user service manager\n" | |
7633 | " -H --host=[USER@]HOST\n" | |
7634 | " Operate on remote host\n" | |
7635 | " -M --machine=CONTAINER\n" | |
7636 | " Operate on local container\n" | |
3fb90db2 ZJS |
7637 | " -t --type=TYPE List units of a particular type\n" |
7638 | " --state=STATE List units with particular LOAD or SUB or ACTIVE state\n" | |
1d52a931 | 7639 | " --failed Shorcut for --state=failed\n" |
8a0867d6 | 7640 | " -p --property=NAME Show only properties by this name\n" |
2cdbbc9a LP |
7641 | " -a --all Show all properties/all units currently in memory,\n" |
7642 | " including dead/empty ones. To list all units installed on\n" | |
7643 | " the system, use the 'list-unit-files' command instead.\n" | |
98a6e132 | 7644 | " -l --full Don't ellipsize unit names on output\n" |
1238ee09 | 7645 | " -r --recursive Show unit list of host and local containers\n" |
4dc5b821 LP |
7646 | " --reverse Show reverse dependencies with 'list-dependencies'\n" |
7647 | " --job-mode=MODE Specify how to deal with already queued jobs, when\n" | |
7648 | " queueing a new job\n" | |
85d9b598 LP |
7649 | " -T --show-transaction\n" |
7650 | " When enqueuing a unit job, show full transaction\n" | |
a521ae4a | 7651 | " --show-types When showing sockets, explicitly show their type\n" |
4f9a9105 | 7652 | " --value When showing properties, only print the value\n" |
b37844d3 LP |
7653 | " -i --ignore-inhibitors\n" |
7654 | " When shutting down or sleeping, ignore inhibitors\n" | |
a8f11321 LP |
7655 | " --kill-who=WHO Who to send signal to\n" |
7656 | " -s --signal=SIGNAL Which signal to send\n" | |
d81bc51f | 7657 | " --what=RESOURCES Which types of resources to remove\n" |
57ab2eab | 7658 | " --now Start or stop unit in addition to enabling or disabling it\n" |
1ae17672 | 7659 | " --dry-run Only print what would be done\n" |
8a0867d6 | 7660 | " -q --quiet Suppress output\n" |
93a08841 | 7661 | " --wait For (re)start, wait until service stopped again\n" |
adb6cd9b | 7662 | " For is-system-running, wait until startup is completed\n" |
8a0867d6 | 7663 | " --no-block Do not wait until operation finished\n" |
8a0867d6 | 7664 | " --no-wall Don't send wall message before halt/power-off/reboot\n" |
3fb90db2 | 7665 | " --no-reload Don't reload daemon after en-/dis-abling unit files\n" |
ebed32bf | 7666 | " --no-legend Do not print a legend (column headers and hints)\n" |
69fc152f | 7667 | " --no-pager Do not pipe output into a pager\n" |
501fc174 LP |
7668 | " --no-ask-password\n" |
7669 | " Do not ask for system passwords\n" | |
4e93d369 LR |
7670 | " --global Enable/disable/mask unit files globally\n" |
7671 | " --runtime Enable/disable/mask unit files temporarily until next\n" | |
7672 | " reboot\n" | |
8a0867d6 LP |
7673 | " -f --force When enabling unit files, override existing symlinks\n" |
7674 | " When shutting down, execute action immediately\n" | |
3fb90db2 | 7675 | " --preset-mode= Apply only enable, only disable, or all presets\n" |
4e93d369 LR |
7676 | " --root=PATH Enable/disable/mask unit files in the specified root\n" |
7677 | " directory\n" | |
76fdc966 | 7678 | " -n --lines=INTEGER Number of journal entries to show\n" |
584c6e70 | 7679 | " -o --output=STRING Change journal output mode (short, short-precise,\n" |
7e563bfc IW |
7680 | " short-iso, short-iso-precise, short-full,\n" |
7681 | " short-monotonic, short-unix,\n" | |
584c6e70 | 7682 | " verbose, export, json, json-pretty, json-sse, cat)\n" |
5bdf2243 | 7683 | " --firmware-setup Tell the firmware to show the setup menu on next boot\n" |
97af80c5 LP |
7684 | " --boot-loader-menu=TIME\n" |
7685 | " Boot into boot loader menu on next boot\n" | |
7686 | " --boot-loader-entry=NAME\n" | |
7687 | " Boot into a specific boot loader entry on next boot\n" | |
815ebc54 | 7688 | " --plain Print unit dependencies as a list instead of a tree\n\n" |
8e543004 | 7689 | "%3$sUnit Commands:%4$s\n" |
fbf3283b ZJS |
7690 | " list-units [PATTERN...] List units currently in memory\n" |
7691 | " list-sockets [PATTERN...] List socket units currently in memory,\n" | |
7692 | " ordered by address\n" | |
7693 | " list-timers [PATTERN...] List timer units currently in memory,\n" | |
7694 | " ordered by next elapse\n" | |
7695 | " start UNIT... Start (activate) one or more units\n" | |
7696 | " stop UNIT... Stop (deactivate) one or more units\n" | |
7697 | " reload UNIT... Reload one or more units\n" | |
7698 | " restart UNIT... Start or restart one or more units\n" | |
7699 | " try-restart UNIT... Restart one or more units if active\n" | |
7700 | " reload-or-restart UNIT... Reload one or more units if possible,\n" | |
7701 | " otherwise start or restart\n" | |
7702 | " try-reload-or-restart UNIT... If active, reload one or more units,\n" | |
7703 | " if supported, otherwise restart\n" | |
7704 | " isolate UNIT Start one unit and stop all others\n" | |
7705 | " kill UNIT... Send signal to processes of a unit\n" | |
d81bc51f LP |
7706 | " clean UNIT... Clean runtime, cache, state, logs or\n" |
7707 | " or configuration of unit\n" | |
fbf3283b ZJS |
7708 | " is-active PATTERN... Check whether units are active\n" |
7709 | " is-failed PATTERN... Check whether units are failed\n" | |
7710 | " status [PATTERN...|PID...] Show runtime status of one or more units\n" | |
7711 | " show [PATTERN...|JOB...] Show properties of one or more\n" | |
7712 | " units/jobs or the manager\n" | |
7713 | " cat PATTERN... Show files and drop-ins of specified units\n" | |
7714 | " set-property UNIT PROPERTY=VALUE... Sets one or more properties of a unit\n" | |
7715 | " help PATTERN...|PID... Show manual for one or more units\n" | |
7716 | " reset-failed [PATTERN...] Reset failed state for all, one, or more\n" | |
7717 | " units\n" | |
7718 | " list-dependencies [UNIT] Recursively show units which are required\n" | |
7719 | " or wanted by this unit or by which this\n" | |
7720 | " unit is required or wanted\n\n" | |
8e543004 | 7721 | "%3$sUnit File Commands:%4$s\n" |
fbf3283b ZJS |
7722 | " list-unit-files [PATTERN...] List installed unit files\n" |
7723 | " enable [UNIT...|PATH...] Enable one or more unit files\n" | |
7724 | " disable UNIT... Disable one or more unit files\n" | |
7725 | " reenable UNIT... Reenable one or more unit files\n" | |
7726 | " preset UNIT... Enable/disable one or more unit files\n" | |
7727 | " based on preset configuration\n" | |
7728 | " preset-all Enable/disable all unit files based on\n" | |
7729 | " preset configuration\n" | |
7730 | " is-enabled UNIT... Check whether unit files are enabled\n" | |
7731 | " mask UNIT... Mask one or more units\n" | |
7732 | " unmask UNIT... Unmask one or more units\n" | |
7733 | " link PATH... Link one or more units files into\n" | |
7734 | " the search path\n" | |
7735 | " revert UNIT... Revert one or more unit files to vendor\n" | |
7736 | " version\n" | |
7737 | " add-wants TARGET UNIT... Add 'Wants' dependency for the target\n" | |
7738 | " on specified one or more units\n" | |
7739 | " add-requires TARGET UNIT... Add 'Requires' dependency for the target\n" | |
7740 | " on specified one or more units\n" | |
7741 | " edit UNIT... Edit one or more unit files\n" | |
7742 | " get-default Get the name of the default target\n" | |
7743 | " set-default TARGET Set the default target\n\n" | |
8e543004 | 7744 | "%3$sMachine Commands:%4$s\n" |
fbf3283b | 7745 | " list-machines [PATTERN...] List local containers and host\n\n" |
8e543004 | 7746 | "%3$sJob Commands:%4$s\n" |
fbf3283b ZJS |
7747 | " list-jobs [PATTERN...] List jobs\n" |
7748 | " cancel [JOB...] Cancel all, one, or more jobs\n\n" | |
8e543004 | 7749 | "%3$sEnvironment Commands:%4$s\n" |
fbf3283b | 7750 | " show-environment Dump environment\n" |
135775c1 ZJS |
7751 | " set-environment VARIABLE=VALUE... Set one or more environment variables\n" |
7752 | " unset-environment VARIABLE... Unset one or more environment variables\n" | |
7753 | " import-environment [VARIABLE...] Import all or some environment variables\n\n" | |
8e543004 | 7754 | "%3$sManager Lifecycle Commands:%4$s\n" |
fbf3283b ZJS |
7755 | " daemon-reload Reload systemd manager configuration\n" |
7756 | " daemon-reexec Reexecute systemd manager\n\n" | |
8e543004 | 7757 | "%3$sSystem Commands:%4$s\n" |
fbf3283b ZJS |
7758 | " is-system-running Check whether system is fully running\n" |
7759 | " default Enter system default mode\n" | |
7760 | " rescue Enter system rescue mode\n" | |
7761 | " emergency Enter system emergency mode\n" | |
7762 | " halt Shut down and halt the system\n" | |
7763 | " poweroff Shut down and power-off the system\n" | |
7764 | " reboot [ARG] Shut down and reboot the system\n" | |
7765 | " kexec Shut down and reboot the system with kexec\n" | |
7766 | " exit [EXIT_CODE] Request user instance or container exit\n" | |
7767 | " switch-root ROOT [INIT] Change to a different root file system\n" | |
7768 | " suspend Suspend the system\n" | |
7769 | " hibernate Hibernate the system\n" | |
c58493c0 | 7770 | " hybrid-sleep Hibernate and suspend the system\n" |
e68c79db | 7771 | " suspend-then-hibernate Suspend the system, wake after a period of\n" |
37ec0fdd | 7772 | " time and put it into hibernate\n" |
8e543004 | 7773 | "\nSee the %2$s for details.\n" |
37ec0fdd LP |
7774 | , program_invocation_short_name |
7775 | , link | |
8e543004 | 7776 | , ansi_underline(), ansi_normal() |
37ec0fdd LP |
7777 | ); |
7778 | ||
7779 | return 0; | |
7e4249b9 LP |
7780 | } |
7781 | ||
37ec0fdd LP |
7782 | static int halt_help(void) { |
7783 | _cleanup_free_ char *link = NULL; | |
7784 | int r; | |
7785 | ||
7786 | r = terminal_urlify_man("halt", "8", &link); | |
7787 | if (r < 0) | |
7788 | return log_oom(); | |
7789 | ||
37185ec8 | 7790 | printf("%s [OPTIONS...]%s\n\n" |
e4b61340 LP |
7791 | "%s the system.\n\n" |
7792 | " --help Show this help\n" | |
7793 | " --halt Halt the machine\n" | |
7794 | " -p --poweroff Switch off the machine\n" | |
7795 | " --reboot Reboot the machine\n" | |
2e33c433 LP |
7796 | " -f --force Force immediate halt/power-off/reboot\n" |
7797 | " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n" | |
e4b61340 | 7798 | " -d --no-wtmp Don't write wtmp record\n" |
37ec0fdd LP |
7799 | " --no-wall Don't send wall message before halt/power-off/reboot\n" |
7800 | "\nSee the %s for details.\n" | |
7801 | , program_invocation_short_name | |
7802 | , arg_action == ACTION_REBOOT ? " [ARG]" : "", | |
7803 | arg_action == ACTION_REBOOT ? "Reboot" : | |
7804 | arg_action == ACTION_POWEROFF ? "Power off" : | |
7805 | "Halt" | |
7806 | , link | |
7807 | ); | |
7808 | ||
7809 | return 0; | |
e4b61340 LP |
7810 | } |
7811 | ||
37ec0fdd LP |
7812 | static int shutdown_help(void) { |
7813 | _cleanup_free_ char *link = NULL; | |
7814 | int r; | |
7815 | ||
7816 | r = terminal_urlify_man("shutdown", "8", &link); | |
7817 | if (r < 0) | |
7818 | return log_oom(); | |
7819 | ||
08e4b1c5 | 7820 | printf("%s [OPTIONS...] [TIME] [WALL...]\n\n" |
e4b61340 LP |
7821 | "Shut down the system.\n\n" |
7822 | " --help Show this help\n" | |
7823 | " -H --halt Halt the machine\n" | |
7824 | " -P --poweroff Power-off the machine\n" | |
7825 | " -r --reboot Reboot the machine\n" | |
386da858 | 7826 | " -h Equivalent to --poweroff, overridden by --halt\n" |
2e33c433 | 7827 | " -k Don't halt/power-off/reboot, just send warnings\n" |
f6144808 | 7828 | " --no-wall Don't send wall message before halt/power-off/reboot\n" |
37ec0fdd LP |
7829 | " -c Cancel a pending shutdown\n" |
7830 | "\nSee the %s for details.\n" | |
7831 | , program_invocation_short_name | |
7832 | , link | |
7833 | ); | |
7834 | ||
7835 | return 0; | |
e4b61340 LP |
7836 | } |
7837 | ||
37ec0fdd LP |
7838 | static int telinit_help(void) { |
7839 | _cleanup_free_ char *link = NULL; | |
7840 | int r; | |
7841 | ||
7842 | r = terminal_urlify_man("telinit", "8", &link); | |
7843 | if (r < 0) | |
7844 | return log_oom(); | |
7845 | ||
2e33c433 | 7846 | printf("%s [OPTIONS...] {COMMAND}\n\n" |
514f4ef5 LP |
7847 | "Send control commands to the init daemon.\n\n" |
7848 | " --help Show this help\n" | |
2e33c433 | 7849 | " --no-wall Don't send wall message before halt/power-off/reboot\n\n" |
e4b61340 LP |
7850 | "Commands:\n" |
7851 | " 0 Power-off the machine\n" | |
7852 | " 6 Reboot the machine\n" | |
514f4ef5 LP |
7853 | " 2, 3, 4, 5 Start runlevelX.target unit\n" |
7854 | " 1, s, S Enter rescue mode\n" | |
7855 | " q, Q Reload init daemon configuration\n" | |
37ec0fdd LP |
7856 | " u, U Reexecute init daemon\n" |
7857 | "\nSee the %s for details.\n" | |
7858 | , program_invocation_short_name | |
7859 | , link | |
7860 | ); | |
7861 | ||
7862 | return 0; | |
e4b61340 LP |
7863 | } |
7864 | ||
37ec0fdd LP |
7865 | static int runlevel_help(void) { |
7866 | _cleanup_free_ char *link = NULL; | |
7867 | int r; | |
7868 | ||
7869 | r = terminal_urlify_man("runlevel", "8", &link); | |
7870 | if (r < 0) | |
7871 | return log_oom(); | |
7872 | ||
2e33c433 | 7873 | printf("%s [OPTIONS...]\n\n" |
e4b61340 | 7874 | "Prints the previous and current runlevel of the init system.\n\n" |
37ec0fdd LP |
7875 | " --help Show this help\n" |
7876 | "\nSee the %s for details.\n" | |
7877 | , program_invocation_short_name | |
7878 | , link | |
7879 | ); | |
7880 | ||
7881 | return 0; | |
e4b61340 LP |
7882 | } |
7883 | ||
b93312f5 | 7884 | static void help_types(void) { |
b93312f5 ZJS |
7885 | if (!arg_no_legend) |
7886 | puts("Available unit types:"); | |
5c828e66 LP |
7887 | |
7888 | DUMP_STRING_TABLE(unit_type, UnitType, _UNIT_TYPE_MAX); | |
e16972e6 ZJS |
7889 | } |
7890 | ||
7891 | static void help_states(void) { | |
e16972e6 ZJS |
7892 | if (!arg_no_legend) |
7893 | puts("Available unit load states:"); | |
5c828e66 | 7894 | DUMP_STRING_TABLE(unit_load_state, UnitLoadState, _UNIT_LOAD_STATE_MAX); |
e16972e6 ZJS |
7895 | |
7896 | if (!arg_no_legend) | |
7897 | puts("\nAvailable unit active states:"); | |
5c828e66 | 7898 | DUMP_STRING_TABLE(unit_active_state, UnitActiveState, _UNIT_ACTIVE_STATE_MAX); |
7e55de3b ZJS |
7899 | |
7900 | if (!arg_no_legend) | |
7901 | puts("\nAvailable automount unit substates:"); | |
5c828e66 | 7902 | DUMP_STRING_TABLE(automount_state, AutomountState, _AUTOMOUNT_STATE_MAX); |
7e55de3b | 7903 | |
7e55de3b ZJS |
7904 | if (!arg_no_legend) |
7905 | puts("\nAvailable device unit substates:"); | |
5c828e66 | 7906 | DUMP_STRING_TABLE(device_state, DeviceState, _DEVICE_STATE_MAX); |
7e55de3b ZJS |
7907 | |
7908 | if (!arg_no_legend) | |
7909 | puts("\nAvailable mount unit substates:"); | |
5c828e66 | 7910 | DUMP_STRING_TABLE(mount_state, MountState, _MOUNT_STATE_MAX); |
7e55de3b ZJS |
7911 | |
7912 | if (!arg_no_legend) | |
7913 | puts("\nAvailable path unit substates:"); | |
5c828e66 | 7914 | DUMP_STRING_TABLE(path_state, PathState, _PATH_STATE_MAX); |
7e55de3b ZJS |
7915 | |
7916 | if (!arg_no_legend) | |
7917 | puts("\nAvailable scope unit substates:"); | |
5c828e66 | 7918 | DUMP_STRING_TABLE(scope_state, ScopeState, _SCOPE_STATE_MAX); |
7e55de3b ZJS |
7919 | |
7920 | if (!arg_no_legend) | |
7921 | puts("\nAvailable service unit substates:"); | |
5c828e66 | 7922 | DUMP_STRING_TABLE(service_state, ServiceState, _SERVICE_STATE_MAX); |
7e55de3b ZJS |
7923 | |
7924 | if (!arg_no_legend) | |
7925 | puts("\nAvailable slice unit substates:"); | |
5c828e66 | 7926 | DUMP_STRING_TABLE(slice_state, SliceState, _SLICE_STATE_MAX); |
7e55de3b | 7927 | |
7e55de3b ZJS |
7928 | if (!arg_no_legend) |
7929 | puts("\nAvailable socket unit substates:"); | |
5c828e66 | 7930 | DUMP_STRING_TABLE(socket_state, SocketState, _SOCKET_STATE_MAX); |
7e55de3b ZJS |
7931 | |
7932 | if (!arg_no_legend) | |
7933 | puts("\nAvailable swap unit substates:"); | |
5c828e66 | 7934 | DUMP_STRING_TABLE(swap_state, SwapState, _SWAP_STATE_MAX); |
7e55de3b ZJS |
7935 | |
7936 | if (!arg_no_legend) | |
7937 | puts("\nAvailable target unit substates:"); | |
5c828e66 | 7938 | DUMP_STRING_TABLE(target_state, TargetState, _TARGET_STATE_MAX); |
7e55de3b ZJS |
7939 | |
7940 | if (!arg_no_legend) | |
7941 | puts("\nAvailable timer unit substates:"); | |
5c828e66 | 7942 | DUMP_STRING_TABLE(timer_state, TimerState, _TIMER_STATE_MAX); |
45c0c61d ZJS |
7943 | } |
7944 | ||
97af80c5 LP |
7945 | static int help_boot_loader_entry(void) { |
7946 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
7947 | _cleanup_free_ char **l = NULL; | |
7948 | sd_bus *bus; | |
7949 | char **i; | |
7950 | int r; | |
7951 | ||
7952 | r = acquire_bus(BUS_FULL, &bus); | |
7953 | if (r < 0) | |
7954 | return r; | |
7955 | ||
7956 | r = sd_bus_get_property_strv( | |
7957 | bus, | |
7958 | "org.freedesktop.login1", | |
7959 | "/org/freedesktop/login1", | |
7960 | "org.freedesktop.login1.Manager", | |
7961 | "BootLoaderEntries", | |
7962 | &error, | |
7963 | &l); | |
7964 | if (r < 0) | |
7965 | return log_error_errno(r, "Failed to enumerate boot loader entries: %s", bus_error_message(&error, r)); | |
7966 | ||
2f50e59f LP |
7967 | if (strv_isempty(l)) |
7968 | return log_error_errno(SYNTHETIC_ERRNO(ENODATA), "No boot loader entries discovered."); | |
97af80c5 LP |
7969 | |
7970 | STRV_FOREACH(i, l) | |
7971 | puts(*i); | |
7972 | ||
7973 | return 0; | |
7974 | } | |
7975 | ||
e4b61340 | 7976 | static int systemctl_parse_argv(int argc, char *argv[]) { |
7e4249b9 | 7977 | enum { |
90d473a1 | 7978 | ARG_FAIL = 0x100, |
afba4199 ZJS |
7979 | ARG_REVERSE, |
7980 | ARG_AFTER, | |
7981 | ARG_BEFORE, | |
1ae17672 | 7982 | ARG_DRY_RUN, |
991f2a39 | 7983 | ARG_SHOW_TYPES, |
23ade460 | 7984 | ARG_IRREVERSIBLE, |
e67c3609 | 7985 | ARG_IGNORE_DEPENDENCIES, |
4f9a9105 | 7986 | ARG_VALUE, |
35df8f27 | 7987 | ARG_VERSION, |
af2d49f7 | 7988 | ARG_USER, |
7e4249b9 | 7989 | ARG_SYSTEM, |
ee5762e3 | 7990 | ARG_GLOBAL, |
6e905d93 | 7991 | ARG_NO_BLOCK, |
ebed32bf | 7992 | ARG_NO_LEGEND, |
611efaac | 7993 | ARG_NO_PAGER, |
4445a875 | 7994 | ARG_NO_WALL, |
be394c48 | 7995 | ARG_ROOT, |
ee5762e3 | 7996 | ARG_NO_RELOAD, |
501fc174 | 7997 | ARG_KILL_WHO, |
30732560 | 7998 | ARG_NO_ASK_PASSWORD, |
729e3769 | 7999 | ARG_FAILED, |
df50185b | 8000 | ARG_RUNTIME, |
9b9b3d36 | 8001 | ARG_PLAIN, |
4dc5b821 | 8002 | ARG_STATE, |
d309c1c3 LP |
8003 | ARG_JOB_MODE, |
8004 | ARG_PRESET_MODE, | |
5bdf2243 | 8005 | ARG_FIRMWARE_SETUP, |
97af80c5 LP |
8006 | ARG_BOOT_LOADER_MENU, |
8007 | ARG_BOOT_LOADER_ENTRY, | |
57ab2eab | 8008 | ARG_NOW, |
9ef15026 | 8009 | ARG_MESSAGE, |
93a08841 | 8010 | ARG_WAIT, |
d81bc51f | 8011 | ARG_WHAT, |
7e4249b9 LP |
8012 | }; |
8013 | ||
8014 | static const struct option options[] = { | |
9ea9d4cf LP |
8015 | { "help", no_argument, NULL, 'h' }, |
8016 | { "version", no_argument, NULL, ARG_VERSION }, | |
8017 | { "type", required_argument, NULL, 't' }, | |
8018 | { "property", required_argument, NULL, 'p' }, | |
8019 | { "all", no_argument, NULL, 'a' }, | |
8020 | { "reverse", no_argument, NULL, ARG_REVERSE }, | |
8021 | { "after", no_argument, NULL, ARG_AFTER }, | |
8022 | { "before", no_argument, NULL, ARG_BEFORE }, | |
8023 | { "show-types", no_argument, NULL, ARG_SHOW_TYPES }, | |
8024 | { "failed", no_argument, NULL, ARG_FAILED }, /* compatibility only */ | |
8025 | { "full", no_argument, NULL, 'l' }, | |
4dc5b821 LP |
8026 | { "job-mode", required_argument, NULL, ARG_JOB_MODE }, |
8027 | { "fail", no_argument, NULL, ARG_FAIL }, /* compatibility only */ | |
8028 | { "irreversible", no_argument, NULL, ARG_IRREVERSIBLE }, /* compatibility only */ | |
8029 | { "ignore-dependencies", no_argument, NULL, ARG_IGNORE_DEPENDENCIES }, /* compatibility only */ | |
9ea9d4cf | 8030 | { "ignore-inhibitors", no_argument, NULL, 'i' }, |
4f9a9105 | 8031 | { "value", no_argument, NULL, ARG_VALUE }, |
9ea9d4cf LP |
8032 | { "user", no_argument, NULL, ARG_USER }, |
8033 | { "system", no_argument, NULL, ARG_SYSTEM }, | |
8034 | { "global", no_argument, NULL, ARG_GLOBAL }, | |
93a08841 | 8035 | { "wait", no_argument, NULL, ARG_WAIT }, |
9ea9d4cf LP |
8036 | { "no-block", no_argument, NULL, ARG_NO_BLOCK }, |
8037 | { "no-legend", no_argument, NULL, ARG_NO_LEGEND }, | |
8038 | { "no-pager", no_argument, NULL, ARG_NO_PAGER }, | |
8039 | { "no-wall", no_argument, NULL, ARG_NO_WALL }, | |
1ae17672 | 8040 | { "dry-run", no_argument, NULL, ARG_DRY_RUN }, |
9ea9d4cf LP |
8041 | { "quiet", no_argument, NULL, 'q' }, |
8042 | { "root", required_argument, NULL, ARG_ROOT }, | |
68da321f | 8043 | { "force", no_argument, NULL, 'f' }, |
9ea9d4cf LP |
8044 | { "no-reload", no_argument, NULL, ARG_NO_RELOAD }, |
8045 | { "kill-who", required_argument, NULL, ARG_KILL_WHO }, | |
8046 | { "signal", required_argument, NULL, 's' }, | |
8047 | { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD }, | |
8048 | { "host", required_argument, NULL, 'H' }, | |
f459b602 | 8049 | { "machine", required_argument, NULL, 'M' }, |
9ea9d4cf LP |
8050 | { "runtime", no_argument, NULL, ARG_RUNTIME }, |
8051 | { "lines", required_argument, NULL, 'n' }, | |
8052 | { "output", required_argument, NULL, 'o' }, | |
8053 | { "plain", no_argument, NULL, ARG_PLAIN }, | |
8054 | { "state", required_argument, NULL, ARG_STATE }, | |
1238ee09 | 8055 | { "recursive", no_argument, NULL, 'r' }, |
d309c1c3 | 8056 | { "preset-mode", required_argument, NULL, ARG_PRESET_MODE }, |
5bdf2243 | 8057 | { "firmware-setup", no_argument, NULL, ARG_FIRMWARE_SETUP }, |
97af80c5 LP |
8058 | { "boot-loader-menu", required_argument, NULL, ARG_BOOT_LOADER_MENU }, |
8059 | { "boot-loader-entry", required_argument, NULL, ARG_BOOT_LOADER_ENTRY }, | |
57ab2eab | 8060 | { "now", no_argument, NULL, ARG_NOW }, |
9ef15026 | 8061 | { "message", required_argument, NULL, ARG_MESSAGE }, |
85d9b598 | 8062 | { "show-transaction", no_argument, NULL, 'T' }, |
d81bc51f | 8063 | { "what", required_argument, NULL, ARG_WHAT }, |
eb9da376 | 8064 | {} |
7e4249b9 LP |
8065 | }; |
8066 | ||
0f03c2a4 | 8067 | int c, r; |
7e4249b9 | 8068 | |
e4b61340 | 8069 | assert(argc >= 0); |
7e4249b9 LP |
8070 | assert(argv); |
8071 | ||
16f017fa IS |
8072 | /* we default to allowing interactive authorization only in systemctl (not in the legacy commands) */ |
8073 | arg_ask_password = true; | |
8074 | ||
85d9b598 | 8075 | while ((c = getopt_long(argc, argv, "ht:p:alqfs:H:M:n:o:iTr.::", options, NULL)) >= 0) |
7e4249b9 LP |
8076 | |
8077 | switch (c) { | |
8078 | ||
8079 | case 'h': | |
37ec0fdd | 8080 | return systemctl_help(); |
35df8f27 LP |
8081 | |
8082 | case ARG_VERSION: | |
3f6fd1ba | 8083 | return version(); |
7e4249b9 | 8084 | |
20b3f379 | 8085 | case 't': { |
fb1ac50e LP |
8086 | const char *p; |
8087 | ||
baaa35ad ZJS |
8088 | if (isempty(optarg)) |
8089 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8090 | "--type= requires arguments."); | |
45c0c61d | 8091 | |
c58bd76a | 8092 | for (p = optarg;;) { |
5ab22f33 | 8093 | _cleanup_free_ char *type = NULL; |
20b3f379 | 8094 | |
5ab22f33 SS |
8095 | r = extract_first_word(&p, &type, ",", 0); |
8096 | if (r < 0) | |
8097 | return log_error_errno(r, "Failed to parse type: %s", optarg); | |
5ab22f33 SS |
8098 | if (r == 0) |
8099 | break; | |
20b3f379 ZJS |
8100 | |
8101 | if (streq(type, "help")) { | |
8102 | help_types(); | |
8103 | return 0; | |
8104 | } | |
8105 | ||
8106 | if (unit_type_from_string(type) >= 0) { | |
96e895ef | 8107 | if (strv_consume(&arg_types, TAKE_PTR(type)) < 0) |
20b3f379 | 8108 | return log_oom(); |
20b3f379 ZJS |
8109 | continue; |
8110 | } | |
8111 | ||
9b9b3d36 MW |
8112 | /* It's much nicer to use --state= for |
8113 | * load states, but let's support this | |
8114 | * in --types= too for compatibility | |
8115 | * with old versions */ | |
7e9d36e0 | 8116 | if (unit_load_state_from_string(type) >= 0) { |
96e895ef | 8117 | if (strv_consume(&arg_states, TAKE_PTR(type)) < 0) |
20b3f379 | 8118 | return log_oom(); |
20b3f379 ZJS |
8119 | continue; |
8120 | } | |
8121 | ||
ab06eef8 | 8122 | log_error("Unknown unit type or load state '%s'.", type); |
baaa35ad ZJS |
8123 | return log_info_errno(SYNTHETIC_ERRNO(EINVAL), |
8124 | "Use -t help to see a list of allowed values."); | |
c147dc42 | 8125 | } |
20b3f379 ZJS |
8126 | |
8127 | break; | |
8128 | } | |
8129 | ||
aee7082f LP |
8130 | case 'p': |
8131 | /* Make sure that if the empty property list was specified, we won't show any | |
8132 | properties. */ | |
20b3f379 | 8133 | if (isempty(optarg) && !arg_properties) { |
cbc9fbd1 | 8134 | arg_properties = new0(char*, 1); |
20b3f379 ZJS |
8135 | if (!arg_properties) |
8136 | return log_oom(); | |
fb1ac50e LP |
8137 | } else { |
8138 | const char *p; | |
8139 | ||
c58bd76a | 8140 | for (p = optarg;;) { |
5ab22f33 | 8141 | _cleanup_free_ char *prop = NULL; |
033a842c | 8142 | |
5ab22f33 SS |
8143 | r = extract_first_word(&p, &prop, ",", 0); |
8144 | if (r < 0) | |
8145 | return log_error_errno(r, "Failed to parse property: %s", optarg); | |
5ab22f33 SS |
8146 | if (r == 0) |
8147 | break; | |
ea4a240d | 8148 | |
9dccc6d3 | 8149 | if (strv_consume(&arg_properties, TAKE_PTR(prop)) < 0) |
20b3f379 | 8150 | return log_oom(); |
20b3f379 | 8151 | } |
fb1ac50e | 8152 | } |
48220598 LP |
8153 | |
8154 | /* If the user asked for a particular | |
8155 | * property, show it to him, even if it is | |
8156 | * empty. */ | |
8157 | arg_all = true; | |
033a842c | 8158 | |
48220598 LP |
8159 | break; |
8160 | ||
7e4249b9 LP |
8161 | case 'a': |
8162 | arg_all = true; | |
8163 | break; | |
8164 | ||
afba4199 ZJS |
8165 | case ARG_REVERSE: |
8166 | arg_dependency = DEPENDENCY_REVERSE; | |
8167 | break; | |
8168 | ||
8169 | case ARG_AFTER: | |
8170 | arg_dependency = DEPENDENCY_AFTER; | |
82948f6c | 8171 | arg_jobs_after = true; |
afba4199 ZJS |
8172 | break; |
8173 | ||
8174 | case ARG_BEFORE: | |
8175 | arg_dependency = DEPENDENCY_BEFORE; | |
82948f6c | 8176 | arg_jobs_before = true; |
afba4199 ZJS |
8177 | break; |
8178 | ||
991f2a39 ZJS |
8179 | case ARG_SHOW_TYPES: |
8180 | arg_show_types = true; | |
8181 | break; | |
8182 | ||
4f9a9105 ZJS |
8183 | case ARG_VALUE: |
8184 | arg_value = true; | |
8185 | break; | |
8186 | ||
4dc5b821 LP |
8187 | case ARG_JOB_MODE: |
8188 | arg_job_mode = optarg; | |
8189 | break; | |
8190 | ||
90d473a1 | 8191 | case ARG_FAIL: |
e67c3609 LP |
8192 | arg_job_mode = "fail"; |
8193 | break; | |
8194 | ||
23ade460 MS |
8195 | case ARG_IRREVERSIBLE: |
8196 | arg_job_mode = "replace-irreversibly"; | |
8197 | break; | |
8198 | ||
e67c3609 LP |
8199 | case ARG_IGNORE_DEPENDENCIES: |
8200 | arg_job_mode = "ignore-dependencies"; | |
7e4249b9 LP |
8201 | break; |
8202 | ||
af2d49f7 | 8203 | case ARG_USER: |
729e3769 | 8204 | arg_scope = UNIT_FILE_USER; |
7e4249b9 LP |
8205 | break; |
8206 | ||
8207 | case ARG_SYSTEM: | |
729e3769 LP |
8208 | arg_scope = UNIT_FILE_SYSTEM; |
8209 | break; | |
8210 | ||
8211 | case ARG_GLOBAL: | |
8212 | arg_scope = UNIT_FILE_GLOBAL; | |
7e4249b9 LP |
8213 | break; |
8214 | ||
93a08841 MP |
8215 | case ARG_WAIT: |
8216 | arg_wait = true; | |
8217 | break; | |
8218 | ||
6e905d93 LP |
8219 | case ARG_NO_BLOCK: |
8220 | arg_no_block = true; | |
7e4249b9 LP |
8221 | break; |
8222 | ||
ebed32bf MS |
8223 | case ARG_NO_LEGEND: |
8224 | arg_no_legend = true; | |
8225 | break; | |
8226 | ||
611efaac | 8227 | case ARG_NO_PAGER: |
0221d68a | 8228 | arg_pager_flags |= PAGER_DISABLE; |
611efaac | 8229 | break; |
0736af98 | 8230 | |
514f4ef5 LP |
8231 | case ARG_NO_WALL: |
8232 | arg_no_wall = true; | |
8233 | break; | |
8234 | ||
be394c48 | 8235 | case ARG_ROOT: |
bac75eb3 | 8236 | r = parse_path_argument_and_warn(optarg, false, &arg_root); |
0f03c2a4 LP |
8237 | if (r < 0) |
8238 | return r; | |
be394c48 FC |
8239 | break; |
8240 | ||
98a6e132 | 8241 | case 'l': |
8fe914ec LP |
8242 | arg_full = true; |
8243 | break; | |
8244 | ||
30732560 | 8245 | case ARG_FAILED: |
9b9b3d36 MW |
8246 | if (strv_extend(&arg_states, "failed") < 0) |
8247 | return log_oom(); | |
8248 | ||
30732560 LP |
8249 | break; |
8250 | ||
1ae17672 ZJS |
8251 | case ARG_DRY_RUN: |
8252 | arg_dry_run = true; | |
8253 | break; | |
8254 | ||
0183528f LP |
8255 | case 'q': |
8256 | arg_quiet = true; | |
8257 | break; | |
8258 | ||
b4f27ccc | 8259 | case 'f': |
313cefa1 | 8260 | arg_force++; |
ee5762e3 LP |
8261 | break; |
8262 | ||
8263 | case ARG_NO_RELOAD: | |
8264 | arg_no_reload = true; | |
8265 | break; | |
8266 | ||
8a0867d6 LP |
8267 | case ARG_KILL_WHO: |
8268 | arg_kill_who = optarg; | |
8269 | break; | |
8270 | ||
8a0867d6 | 8271 | case 's': |
5c828e66 LP |
8272 | if (streq(optarg, "help")) { |
8273 | DUMP_STRING_TABLE(signal, int, _NSIG); | |
8274 | return 0; | |
8275 | } | |
8276 | ||
29a3db75 | 8277 | arg_signal = signal_from_string(optarg); |
baaa35ad ZJS |
8278 | if (arg_signal < 0) |
8279 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8280 | "Failed to parse signal string %s.", | |
8281 | optarg); | |
8a0867d6 LP |
8282 | break; |
8283 | ||
501fc174 LP |
8284 | case ARG_NO_ASK_PASSWORD: |
8285 | arg_ask_password = false; | |
8286 | break; | |
8287 | ||
f459b602 MAP |
8288 | case 'H': |
8289 | arg_transport = BUS_TRANSPORT_REMOTE; | |
8290 | arg_host = optarg; | |
a8f11321 LP |
8291 | break; |
8292 | ||
f459b602 | 8293 | case 'M': |
de33fc62 | 8294 | arg_transport = BUS_TRANSPORT_MACHINE; |
f459b602 | 8295 | arg_host = optarg; |
a8f11321 LP |
8296 | break; |
8297 | ||
729e3769 LP |
8298 | case ARG_RUNTIME: |
8299 | arg_runtime = true; | |
8300 | break; | |
8301 | ||
df50185b | 8302 | case 'n': |
baaa35ad ZJS |
8303 | if (safe_atou(optarg, &arg_lines) < 0) |
8304 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8305 | "Failed to parse lines '%s'", | |
8306 | optarg); | |
df50185b LP |
8307 | break; |
8308 | ||
df50185b | 8309 | case 'o': |
5c828e66 LP |
8310 | if (streq(optarg, "help")) { |
8311 | DUMP_STRING_TABLE(output_mode, OutputMode, _OUTPUT_MODE_MAX); | |
8312 | return 0; | |
8313 | } | |
8314 | ||
df50185b | 8315 | arg_output = output_mode_from_string(optarg); |
baaa35ad ZJS |
8316 | if (arg_output < 0) |
8317 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8318 | "Unknown output '%s'.", | |
8319 | optarg); | |
df50185b LP |
8320 | break; |
8321 | ||
b37844d3 LP |
8322 | case 'i': |
8323 | arg_ignore_inhibitors = true; | |
8324 | break; | |
8325 | ||
5d0c05e5 LN |
8326 | case ARG_PLAIN: |
8327 | arg_plain = true; | |
8328 | break; | |
8329 | ||
5bdf2243 JJ |
8330 | case ARG_FIRMWARE_SETUP: |
8331 | arg_firmware_setup = true; | |
8332 | break; | |
8333 | ||
97af80c5 LP |
8334 | case ARG_BOOT_LOADER_MENU: |
8335 | ||
8336 | r = parse_sec(optarg, &arg_boot_loader_menu); | |
8337 | if (r < 0) | |
8338 | return log_error_errno(r, "Failed to parse --boot-loader-menu= argument '%s': %m", optarg); | |
8339 | ||
8340 | break; | |
8341 | ||
8342 | case ARG_BOOT_LOADER_ENTRY: | |
8343 | ||
8344 | if (streq(optarg, "help")) { /* Yes, this means, "help" is not a valid boot loader entry name we can deal with */ | |
8345 | r = help_boot_loader_entry(); | |
8346 | if (r < 0) | |
8347 | return r; | |
8348 | ||
8349 | return 0; | |
8350 | } | |
8351 | ||
8352 | arg_boot_loader_entry = empty_to_null(optarg); | |
8353 | break; | |
8354 | ||
9b9b3d36 | 8355 | case ARG_STATE: { |
fb1ac50e LP |
8356 | const char *p; |
8357 | ||
baaa35ad ZJS |
8358 | if (isempty(optarg)) |
8359 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8360 | "--state= requires arguments."); | |
9b9b3d36 | 8361 | |
c58bd76a | 8362 | for (p = optarg;;) { |
bc6c18fe | 8363 | _cleanup_free_ char *s = NULL; |
9b9b3d36 | 8364 | |
5ab22f33 SS |
8365 | r = extract_first_word(&p, &s, ",", 0); |
8366 | if (r < 0) | |
6862111e | 8367 | return log_error_errno(r, "Failed to parse state: %s", optarg); |
5ab22f33 SS |
8368 | if (r == 0) |
8369 | break; | |
9b9b3d36 | 8370 | |
e16972e6 ZJS |
8371 | if (streq(s, "help")) { |
8372 | help_states(); | |
8373 | return 0; | |
8374 | } | |
8375 | ||
9dccc6d3 | 8376 | if (strv_consume(&arg_states, TAKE_PTR(s)) < 0) |
9b9b3d36 | 8377 | return log_oom(); |
9b9b3d36 MW |
8378 | } |
8379 | break; | |
8380 | } | |
8381 | ||
1238ee09 | 8382 | case 'r': |
baaa35ad ZJS |
8383 | if (geteuid() != 0) |
8384 | return log_error_errno(SYNTHETIC_ERRNO(EPERM), | |
8385 | "--recursive requires root privileges."); | |
1238ee09 LP |
8386 | |
8387 | arg_recursive = true; | |
8388 | break; | |
8389 | ||
d309c1c3 | 8390 | case ARG_PRESET_MODE: |
5c828e66 LP |
8391 | if (streq(optarg, "help")) { |
8392 | DUMP_STRING_TABLE(unit_file_preset_mode, UnitFilePresetMode, _UNIT_FILE_PRESET_MAX); | |
8393 | return 0; | |
8394 | } | |
d309c1c3 LP |
8395 | |
8396 | arg_preset_mode = unit_file_preset_mode_from_string(optarg); | |
baaa35ad ZJS |
8397 | if (arg_preset_mode < 0) |
8398 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8399 | "Failed to parse preset mode: %s.", optarg); | |
d309c1c3 LP |
8400 | |
8401 | break; | |
8402 | ||
57ab2eab JS |
8403 | case ARG_NOW: |
8404 | arg_now = true; | |
8405 | break; | |
8406 | ||
9ef15026 JS |
8407 | case ARG_MESSAGE: |
8408 | if (strv_extend(&arg_wall, optarg) < 0) | |
8409 | return log_oom(); | |
8410 | break; | |
8411 | ||
85d9b598 LP |
8412 | case 'T': |
8413 | arg_show_transaction = true; | |
8414 | break; | |
8415 | ||
d81bc51f LP |
8416 | case ARG_WHAT: { |
8417 | const char *p; | |
8418 | ||
8419 | if (isempty(optarg)) | |
8420 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--what= requires arguments."); | |
8421 | ||
8422 | for (p = optarg;;) { | |
8423 | _cleanup_free_ char *k = NULL; | |
8424 | ||
8425 | r = extract_first_word(&p, &k, ",", 0); | |
8426 | if (r < 0) | |
8427 | return log_error_errno(r, "Failed to parse directory type: %s", optarg); | |
8428 | if (r == 0) | |
8429 | break; | |
8430 | ||
8431 | if (streq(k, "help")) { | |
8432 | puts("runtime\n" | |
8433 | "state\n" | |
8434 | "cache\n" | |
8435 | "logs\n" | |
8436 | "configuration"); | |
8437 | return 0; | |
8438 | } | |
8439 | ||
8440 | r = strv_consume(&arg_clean_what, TAKE_PTR(k)); | |
8441 | if (r < 0) | |
8442 | return log_oom(); | |
8443 | } | |
8444 | ||
8445 | break; | |
8446 | } | |
8447 | ||
2e7e19ca ZJS |
8448 | case '.': |
8449 | /* Output an error mimicking getopt, and print a hint afterwards */ | |
8450 | log_error("%s: invalid option -- '.'", program_invocation_name); | |
8451 | log_notice("Hint: to specify units starting with a dash, use \"--\":\n" | |
8452 | " %s [OPTIONS...] {COMMAND} -- -.%s ...", | |
8453 | program_invocation_name, optarg ?: "mount"); | |
8454 | _fallthrough_; | |
8455 | ||
7e4249b9 LP |
8456 | case '?': |
8457 | return -EINVAL; | |
8458 | ||
8459 | default: | |
eb9da376 | 8460 | assert_not_reached("Unhandled option"); |
7e4249b9 | 8461 | } |
7e4249b9 | 8462 | |
baaa35ad ZJS |
8463 | if (arg_transport != BUS_TRANSPORT_LOCAL && arg_scope != UNIT_FILE_SYSTEM) |
8464 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8465 | "Cannot access user instance remotely."); | |
a8f11321 | 8466 | |
baaa35ad ZJS |
8467 | if (arg_wait && arg_no_block) |
8468 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8469 | "--wait may not be combined with --no-block."); | |
93a08841 | 8470 | |
7e4249b9 LP |
8471 | return 1; |
8472 | } | |
8473 | ||
e4b61340 | 8474 | static int halt_parse_argv(int argc, char *argv[]) { |
e4b61340 LP |
8475 | enum { |
8476 | ARG_HELP = 0x100, | |
8477 | ARG_HALT, | |
514f4ef5 LP |
8478 | ARG_REBOOT, |
8479 | ARG_NO_WALL | |
e4b61340 LP |
8480 | }; |
8481 | ||
8482 | static const struct option options[] = { | |
8483 | { "help", no_argument, NULL, ARG_HELP }, | |
8484 | { "halt", no_argument, NULL, ARG_HALT }, | |
8485 | { "poweroff", no_argument, NULL, 'p' }, | |
8486 | { "reboot", no_argument, NULL, ARG_REBOOT }, | |
8487 | { "force", no_argument, NULL, 'f' }, | |
8488 | { "wtmp-only", no_argument, NULL, 'w' }, | |
8489 | { "no-wtmp", no_argument, NULL, 'd' }, | |
f3f054f0 | 8490 | { "no-sync", no_argument, NULL, 'n' }, |
514f4ef5 | 8491 | { "no-wall", no_argument, NULL, ARG_NO_WALL }, |
eb9da376 | 8492 | {} |
e4b61340 LP |
8493 | }; |
8494 | ||
37185ec8 | 8495 | int c, r, runlevel; |
e4b61340 LP |
8496 | |
8497 | assert(argc >= 0); | |
8498 | assert(argv); | |
8499 | ||
8500 | if (utmp_get_runlevel(&runlevel, NULL) >= 0) | |
4c701096 | 8501 | if (IN_SET(runlevel, '0', '6')) |
65491fd8 | 8502 | arg_force = 2; |
e4b61340 | 8503 | |
601185b4 | 8504 | while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) |
e4b61340 LP |
8505 | switch (c) { |
8506 | ||
8507 | case ARG_HELP: | |
37ec0fdd | 8508 | return halt_help(); |
e4b61340 LP |
8509 | |
8510 | case ARG_HALT: | |
8511 | arg_action = ACTION_HALT; | |
8512 | break; | |
8513 | ||
8514 | case 'p': | |
a042efad MS |
8515 | if (arg_action != ACTION_REBOOT) |
8516 | arg_action = ACTION_POWEROFF; | |
e4b61340 LP |
8517 | break; |
8518 | ||
8519 | case ARG_REBOOT: | |
8520 | arg_action = ACTION_REBOOT; | |
8521 | break; | |
8522 | ||
8523 | case 'f': | |
65491fd8 | 8524 | arg_force = 2; |
e4b61340 LP |
8525 | break; |
8526 | ||
8527 | case 'w': | |
1ae17672 | 8528 | arg_dry_run = true; |
e4b61340 LP |
8529 | break; |
8530 | ||
8531 | case 'd': | |
8532 | arg_no_wtmp = true; | |
8533 | break; | |
8534 | ||
f3f054f0 TB |
8535 | case 'n': |
8536 | arg_no_sync = true; | |
8537 | break; | |
8538 | ||
514f4ef5 LP |
8539 | case ARG_NO_WALL: |
8540 | arg_no_wall = true; | |
8541 | break; | |
8542 | ||
e4b61340 LP |
8543 | case 'i': |
8544 | case 'h': | |
8545 | /* Compatibility nops */ | |
8546 | break; | |
8547 | ||
8548 | case '?': | |
8549 | return -EINVAL; | |
8550 | ||
8551 | default: | |
eb9da376 | 8552 | assert_not_reached("Unhandled option"); |
e4b61340 | 8553 | } |
e4b61340 | 8554 | |
c5220a94 | 8555 | if (arg_action == ACTION_REBOOT && (argc == optind || argc == optind + 1)) { |
77defcf5 | 8556 | r = update_reboot_parameter_and_warn(argc == optind + 1 ? argv[optind] : NULL, false); |
c5220a94 | 8557 | if (r < 0) |
37185ec8 | 8558 | return r; |
baaa35ad ZJS |
8559 | } else if (optind < argc) |
8560 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8561 | "Too many arguments."); | |
e4b61340 LP |
8562 | |
8563 | return 1; | |
8564 | } | |
8565 | ||
8566 | static int shutdown_parse_argv(int argc, char *argv[]) { | |
e4b61340 LP |
8567 | enum { |
8568 | ARG_HELP = 0x100, | |
514f4ef5 | 8569 | ARG_NO_WALL |
e4b61340 LP |
8570 | }; |
8571 | ||
8572 | static const struct option options[] = { | |
8573 | { "help", no_argument, NULL, ARG_HELP }, | |
8574 | { "halt", no_argument, NULL, 'H' }, | |
8575 | { "poweroff", no_argument, NULL, 'P' }, | |
8576 | { "reboot", no_argument, NULL, 'r' }, | |
04ebb595 | 8577 | { "kexec", no_argument, NULL, 'K' }, /* not documented extension */ |
514f4ef5 | 8578 | { "no-wall", no_argument, NULL, ARG_NO_WALL }, |
eb9da376 | 8579 | {} |
e4b61340 LP |
8580 | }; |
8581 | ||
172d7abf | 8582 | char **wall = NULL; |
f6144808 | 8583 | int c, r; |
e4b61340 LP |
8584 | |
8585 | assert(argc >= 0); | |
8586 | assert(argv); | |
8587 | ||
b068c6f5 | 8588 | while ((c = getopt_long(argc, argv, "HPrhkKat:fFc", options, NULL)) >= 0) |
e4b61340 LP |
8589 | switch (c) { |
8590 | ||
8591 | case ARG_HELP: | |
37ec0fdd | 8592 | return shutdown_help(); |
e4b61340 LP |
8593 | |
8594 | case 'H': | |
8595 | arg_action = ACTION_HALT; | |
8596 | break; | |
8597 | ||
8598 | case 'P': | |
8599 | arg_action = ACTION_POWEROFF; | |
8600 | break; | |
8601 | ||
8602 | case 'r': | |
5622dde3 KS |
8603 | if (kexec_loaded()) |
8604 | arg_action = ACTION_KEXEC; | |
8605 | else | |
8606 | arg_action = ACTION_REBOOT; | |
e4b61340 LP |
8607 | break; |
8608 | ||
04ebb595 LP |
8609 | case 'K': |
8610 | arg_action = ACTION_KEXEC; | |
8611 | break; | |
8612 | ||
e4b61340 LP |
8613 | case 'h': |
8614 | if (arg_action != ACTION_HALT) | |
8615 | arg_action = ACTION_POWEROFF; | |
8616 | break; | |
8617 | ||
8618 | case 'k': | |
1ae17672 | 8619 | arg_dry_run = true; |
e4b61340 LP |
8620 | break; |
8621 | ||
514f4ef5 LP |
8622 | case ARG_NO_WALL: |
8623 | arg_no_wall = true; | |
8624 | break; | |
8625 | ||
e4b61340 | 8626 | case 'a': |
b068c6f5 | 8627 | case 't': /* Note that we also ignore any passed argument to -t, not just the -t itself */ |
75836b9d JS |
8628 | case 'f': |
8629 | case 'F': | |
e4b61340 LP |
8630 | /* Compatibility nops */ |
8631 | break; | |
8632 | ||
f6144808 LP |
8633 | case 'c': |
8634 | arg_action = ACTION_CANCEL_SHUTDOWN; | |
8635 | break; | |
8636 | ||
e4b61340 LP |
8637 | case '?': |
8638 | return -EINVAL; | |
8639 | ||
8640 | default: | |
eb9da376 | 8641 | assert_not_reached("Unhandled option"); |
e4b61340 | 8642 | } |
e4b61340 | 8643 | |
dfcc5c33 | 8644 | if (argc > optind && arg_action != ACTION_CANCEL_SHUTDOWN) { |
2cc7b0a2 | 8645 | r = parse_shutdown_time_spec(argv[optind], &arg_when); |
7e59bfcb | 8646 | if (r < 0) { |
f6144808 LP |
8647 | log_error("Failed to parse time specification: %s", argv[optind]); |
8648 | return r; | |
8649 | } | |
6b5ad000 | 8650 | } else |
08e4b1c5 | 8651 | arg_when = now(CLOCK_REALTIME) + USEC_PER_MINUTE; |
442b9094 | 8652 | |
dfcc5c33 MS |
8653 | if (argc > optind && arg_action == ACTION_CANCEL_SHUTDOWN) |
8654 | /* No time argument for shutdown cancel */ | |
172d7abf | 8655 | wall = argv + optind; |
dfcc5c33 MS |
8656 | else if (argc > optind + 1) |
8657 | /* We skip the time argument */ | |
172d7abf LP |
8658 | wall = argv + optind + 1; |
8659 | ||
8660 | if (wall) { | |
8661 | arg_wall = strv_copy(wall); | |
8662 | if (!arg_wall) | |
8663 | return log_oom(); | |
8664 | } | |
e4b61340 LP |
8665 | |
8666 | optind = argc; | |
8667 | ||
8668 | return 1; | |
e4b61340 LP |
8669 | } |
8670 | ||
8671 | static int telinit_parse_argv(int argc, char *argv[]) { | |
e4b61340 LP |
8672 | enum { |
8673 | ARG_HELP = 0x100, | |
514f4ef5 | 8674 | ARG_NO_WALL |
e4b61340 LP |
8675 | }; |
8676 | ||
8677 | static const struct option options[] = { | |
8678 | { "help", no_argument, NULL, ARG_HELP }, | |
514f4ef5 | 8679 | { "no-wall", no_argument, NULL, ARG_NO_WALL }, |
eb9da376 | 8680 | {} |
e4b61340 LP |
8681 | }; |
8682 | ||
8683 | static const struct { | |
8684 | char from; | |
8685 | enum action to; | |
8686 | } table[] = { | |
8687 | { '0', ACTION_POWEROFF }, | |
8688 | { '6', ACTION_REBOOT }, | |
ef2f1067 | 8689 | { '1', ACTION_RESCUE }, |
e4b61340 LP |
8690 | { '2', ACTION_RUNLEVEL2 }, |
8691 | { '3', ACTION_RUNLEVEL3 }, | |
8692 | { '4', ACTION_RUNLEVEL4 }, | |
8693 | { '5', ACTION_RUNLEVEL5 }, | |
8694 | { 's', ACTION_RESCUE }, | |
8695 | { 'S', ACTION_RESCUE }, | |
8696 | { 'q', ACTION_RELOAD }, | |
8697 | { 'Q', ACTION_RELOAD }, | |
8698 | { 'u', ACTION_REEXEC }, | |
8699 | { 'U', ACTION_REEXEC } | |
8700 | }; | |
8701 | ||
8702 | unsigned i; | |
8703 | int c; | |
8704 | ||
8705 | assert(argc >= 0); | |
8706 | assert(argv); | |
8707 | ||
601185b4 | 8708 | while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) |
e4b61340 LP |
8709 | switch (c) { |
8710 | ||
8711 | case ARG_HELP: | |
37ec0fdd | 8712 | return telinit_help(); |
e4b61340 | 8713 | |
514f4ef5 LP |
8714 | case ARG_NO_WALL: |
8715 | arg_no_wall = true; | |
8716 | break; | |
8717 | ||
e4b61340 LP |
8718 | case '?': |
8719 | return -EINVAL; | |
8720 | ||
8721 | default: | |
eb9da376 | 8722 | assert_not_reached("Unhandled option"); |
e4b61340 | 8723 | } |
e4b61340 | 8724 | |
baaa35ad ZJS |
8725 | if (optind >= argc) |
8726 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8727 | "%s: required argument missing.", | |
8728 | program_invocation_short_name); | |
e4b61340 | 8729 | |
baaa35ad ZJS |
8730 | if (optind + 1 < argc) |
8731 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8732 | "Too many arguments."); | |
e4b61340 | 8733 | |
baaa35ad ZJS |
8734 | if (strlen(argv[optind]) != 1) |
8735 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8736 | "Expected single character argument."); | |
e4b61340 LP |
8737 | |
8738 | for (i = 0; i < ELEMENTSOF(table); i++) | |
8739 | if (table[i].from == argv[optind][0]) | |
8740 | break; | |
8741 | ||
baaa35ad ZJS |
8742 | if (i >= ELEMENTSOF(table)) |
8743 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8744 | "Unknown command '%s'.", argv[optind]); | |
e4b61340 LP |
8745 | |
8746 | arg_action = table[i].to; | |
8747 | ||
313cefa1 | 8748 | optind++; |
e4b61340 LP |
8749 | |
8750 | return 1; | |
8751 | } | |
8752 | ||
8753 | static int runlevel_parse_argv(int argc, char *argv[]) { | |
e4b61340 LP |
8754 | enum { |
8755 | ARG_HELP = 0x100, | |
8756 | }; | |
8757 | ||
8758 | static const struct option options[] = { | |
8759 | { "help", no_argument, NULL, ARG_HELP }, | |
eb9da376 | 8760 | {} |
e4b61340 LP |
8761 | }; |
8762 | ||
8763 | int c; | |
8764 | ||
8765 | assert(argc >= 0); | |
8766 | assert(argv); | |
8767 | ||
601185b4 | 8768 | while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) |
e4b61340 LP |
8769 | switch (c) { |
8770 | ||
8771 | case ARG_HELP: | |
37ec0fdd | 8772 | return runlevel_help(); |
e4b61340 LP |
8773 | |
8774 | case '?': | |
8775 | return -EINVAL; | |
8776 | ||
8777 | default: | |
eb9da376 | 8778 | assert_not_reached("Unhandled option"); |
e4b61340 | 8779 | } |
e4b61340 | 8780 | |
baaa35ad ZJS |
8781 | if (optind < argc) |
8782 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8783 | "Too many arguments."); | |
e4b61340 LP |
8784 | |
8785 | return 1; | |
8786 | } | |
8787 | ||
8788 | static int parse_argv(int argc, char *argv[]) { | |
8789 | assert(argc >= 0); | |
8790 | assert(argv); | |
8791 | ||
8792 | if (program_invocation_short_name) { | |
8793 | ||
8794 | if (strstr(program_invocation_short_name, "halt")) { | |
8795 | arg_action = ACTION_HALT; | |
8796 | return halt_parse_argv(argc, argv); | |
c54819ca | 8797 | |
e4b61340 LP |
8798 | } else if (strstr(program_invocation_short_name, "poweroff")) { |
8799 | arg_action = ACTION_POWEROFF; | |
8800 | return halt_parse_argv(argc, argv); | |
c54819ca | 8801 | |
e4b61340 | 8802 | } else if (strstr(program_invocation_short_name, "reboot")) { |
5622dde3 KS |
8803 | if (kexec_loaded()) |
8804 | arg_action = ACTION_KEXEC; | |
8805 | else | |
8806 | arg_action = ACTION_REBOOT; | |
e4b61340 | 8807 | return halt_parse_argv(argc, argv); |
c54819ca | 8808 | |
e4b61340 LP |
8809 | } else if (strstr(program_invocation_short_name, "shutdown")) { |
8810 | arg_action = ACTION_POWEROFF; | |
8811 | return shutdown_parse_argv(argc, argv); | |
c54819ca | 8812 | |
e4b61340 | 8813 | } else if (strstr(program_invocation_short_name, "init")) { |
d5ca5f11 | 8814 | |
c54819ca LP |
8815 | /* Matches invocations as "init" as well as "telinit", which are synonymous when run as PID != |
8816 | * 1 on SysV. | |
8817 | * | |
8818 | * On SysV "telinit" was the official command to communicate with PID 1, but "init" would | |
8819 | * redirect itself to "telinit" if called with PID != 1. We follow the same logic here still, | |
8820 | * though we add one level of indirection, as we implement "telinit" in "systemctl". Hence, for | |
8821 | * us if you invoke "init" you get "systemd", but it will execve() "systemctl" immediately with | |
8822 | * argv[] unmodified if PID is != 1. If you invoke "telinit" you directly get "systemctl". In | |
8823 | * both cases we shall do the same thing, which is why we do strstr(p_i_s_n, "init") here, as a | |
8824 | * quick way to match both. | |
8825 | * | |
8826 | * Also see redirect_telinit() in src/core/main.c. */ | |
8827 | ||
d5ca5f11 | 8828 | if (sd_booted() > 0) { |
f459b602 | 8829 | arg_action = _ACTION_INVALID; |
d5ca5f11 LP |
8830 | return telinit_parse_argv(argc, argv); |
8831 | } else { | |
c54819ca LP |
8832 | /* Hmm, so some other init system is running, we need to forward this request to |
8833 | * it. For now we simply guess that it is Upstart. */ | |
d5ca5f11 | 8834 | |
595225af | 8835 | (void) rlimit_nofile_safe(); |
4ad61fd1 | 8836 | execv(TELINIT, argv); |
d5ca5f11 | 8837 | |
baaa35ad ZJS |
8838 | return log_error_errno(SYNTHETIC_ERRNO(EIO), |
8839 | "Couldn't find an alternative telinit implementation to spawn."); | |
d5ca5f11 LP |
8840 | } |
8841 | ||
e4b61340 LP |
8842 | } else if (strstr(program_invocation_short_name, "runlevel")) { |
8843 | arg_action = ACTION_RUNLEVEL; | |
8844 | return runlevel_parse_argv(argc, argv); | |
8845 | } | |
8846 | } | |
8847 | ||
8848 | arg_action = ACTION_SYSTEMCTL; | |
8849 | return systemctl_parse_argv(argc, argv); | |
8850 | } | |
8851 | ||
349cc4a5 | 8852 | #if HAVE_SYSV_COMPAT |
44a6b1b6 | 8853 | _pure_ static int action_to_runlevel(void) { |
eb22ac37 LP |
8854 | static const char table[_ACTION_MAX] = { |
8855 | [ACTION_HALT] = '0', | |
8856 | [ACTION_POWEROFF] = '0', | |
8857 | [ACTION_REBOOT] = '6', | |
8858 | [ACTION_RUNLEVEL2] = '2', | |
8859 | [ACTION_RUNLEVEL3] = '3', | |
8860 | [ACTION_RUNLEVEL4] = '4', | |
8861 | [ACTION_RUNLEVEL5] = '5', | |
8862 | [ACTION_RESCUE] = '1' | |
8863 | }; | |
8864 | ||
78ca9099 | 8865 | assert(arg_action >= 0 && arg_action < _ACTION_MAX); |
d55ae9e6 LP |
8866 | return table[arg_action]; |
8867 | } | |
d2e79673 | 8868 | #endif |
d55ae9e6 | 8869 | |
e449de87 | 8870 | static int systemctl_main(int argc, char *argv[]) { |
e449de87 | 8871 | static const Verb verbs[] = { |
c56d1e2c CW |
8872 | { "list-units", VERB_ANY, VERB_ANY, VERB_DEFAULT|VERB_ONLINE_ONLY, list_units }, |
8873 | { "list-unit-files", VERB_ANY, VERB_ANY, 0, list_unit_files }, | |
8874 | { "list-sockets", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, list_sockets }, | |
8875 | { "list-timers", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, list_timers }, | |
8876 | { "list-jobs", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, list_jobs }, | |
a004cb75 | 8877 | { "list-machines", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, list_machines }, |
c56d1e2c CW |
8878 | { "clear-jobs", VERB_ANY, 1, VERB_ONLINE_ONLY, trivial_method }, |
8879 | { "cancel", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, cancel_job }, | |
8880 | { "start", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, | |
8881 | { "stop", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, | |
8882 | { "condstop", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, /* For compatibility with ALTLinux */ | |
8883 | { "reload", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, | |
8884 | { "restart", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, | |
8885 | { "try-restart", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, | |
8886 | { "reload-or-restart", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, | |
5238e957 | 8887 | { "reload-or-try-restart", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, /* For compatibility with old systemctl <= 228 */ |
c56d1e2c CW |
8888 | { "try-reload-or-restart", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, |
8889 | { "force-reload", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, /* For compatibility with SysV */ | |
8890 | { "condreload", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, /* For compatibility with ALTLinux */ | |
8891 | { "condrestart", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, /* For compatibility with RH */ | |
8892 | { "isolate", 2, 2, VERB_ONLINE_ONLY, start_unit }, | |
8893 | { "kill", 2, VERB_ANY, VERB_ONLINE_ONLY, kill_unit }, | |
d81bc51f | 8894 | { "clean", 2, VERB_ANY, VERB_ONLINE_ONLY, clean_unit }, |
c56d1e2c | 8895 | { "is-active", 2, VERB_ANY, VERB_ONLINE_ONLY, check_unit_active }, |
3e7e587d | 8896 | { "check", 2, VERB_ANY, VERB_ONLINE_ONLY, check_unit_active }, /* deprecated alias of is-active */ |
c56d1e2c CW |
8897 | { "is-failed", 2, VERB_ANY, VERB_ONLINE_ONLY, check_unit_failed }, |
8898 | { "show", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, show }, | |
8899 | { "cat", 2, VERB_ANY, VERB_ONLINE_ONLY, cat }, | |
8900 | { "status", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, show }, | |
8901 | { "help", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, show }, | |
8902 | { "daemon-reload", VERB_ANY, 1, VERB_ONLINE_ONLY, daemon_reload }, | |
8903 | { "daemon-reexec", VERB_ANY, 1, VERB_ONLINE_ONLY, daemon_reload }, | |
8904 | { "show-environment", VERB_ANY, 1, VERB_ONLINE_ONLY, show_environment }, | |
8905 | { "set-environment", 2, VERB_ANY, VERB_ONLINE_ONLY, set_environment }, | |
8906 | { "unset-environment", 2, VERB_ANY, VERB_ONLINE_ONLY, set_environment }, | |
8907 | { "import-environment", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, import_environment }, | |
8908 | { "halt", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, | |
8909 | { "poweroff", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, | |
8910 | { "reboot", VERB_ANY, 2, VERB_ONLINE_ONLY, start_system_special }, | |
8911 | { "kexec", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, | |
8912 | { "suspend", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, | |
8913 | { "hibernate", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, | |
8914 | { "hybrid-sleep", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, | |
e68c79db | 8915 | { "suspend-then-hibernate",VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, |
c56d1e2c CW |
8916 | { "default", VERB_ANY, 1, VERB_ONLINE_ONLY, start_special }, |
8917 | { "rescue", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, | |
8918 | { "emergency", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, | |
8919 | { "exit", VERB_ANY, 2, VERB_ONLINE_ONLY, start_special }, | |
8920 | { "reset-failed", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, reset_failed }, | |
8921 | { "enable", 2, VERB_ANY, 0, enable_unit }, | |
8922 | { "disable", 2, VERB_ANY, 0, enable_unit }, | |
8923 | { "is-enabled", 2, VERB_ANY, 0, unit_is_enabled }, | |
8924 | { "reenable", 2, VERB_ANY, 0, enable_unit }, | |
8925 | { "preset", 2, VERB_ANY, 0, enable_unit }, | |
8926 | { "preset-all", VERB_ANY, 1, 0, preset_all }, | |
8927 | { "mask", 2, VERB_ANY, 0, enable_unit }, | |
8928 | { "unmask", 2, VERB_ANY, 0, enable_unit }, | |
8929 | { "link", 2, VERB_ANY, 0, enable_unit }, | |
8930 | { "revert", 2, VERB_ANY, 0, enable_unit }, | |
8931 | { "switch-root", 2, VERB_ANY, VERB_ONLINE_ONLY, switch_root }, | |
8932 | { "list-dependencies", VERB_ANY, 2, VERB_ONLINE_ONLY, list_dependencies }, | |
8933 | { "set-default", 2, 2, 0, set_default }, | |
8934 | { "get-default", VERB_ANY, 1, 0, get_default }, | |
8935 | { "set-property", 3, VERB_ANY, VERB_ONLINE_ONLY, set_property }, | |
8936 | { "is-system-running", VERB_ANY, 1, 0, is_system_running }, | |
8937 | { "add-wants", 3, VERB_ANY, 0, add_dependency }, | |
8938 | { "add-requires", 3, VERB_ANY, 0, add_dependency }, | |
8939 | { "edit", 2, VERB_ANY, VERB_ONLINE_ONLY, edit }, | |
d08e75ed | 8940 | {} |
e449de87 | 8941 | }; |
7e4249b9 | 8942 | |
e449de87 | 8943 | return dispatch_verb(argc, argv, verbs, NULL); |
e4b61340 LP |
8944 | } |
8945 | ||
4fbd7192 | 8946 | static int reload_with_fallback(void) { |
4fbd7192 | 8947 | /* First, try systemd via D-Bus. */ |
e449de87 | 8948 | if (daemon_reload(0, NULL, NULL) >= 0) |
4fbd7192 | 8949 | return 0; |
e4b61340 LP |
8950 | |
8951 | /* Nothing else worked, so let's try signals */ | |
2853b60a | 8952 | assert(IN_SET(arg_action, ACTION_RELOAD, ACTION_REEXEC)); |
e4b61340 | 8953 | |
4a62c710 MS |
8954 | if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) |
8955 | return log_error_errno(errno, "kill() failed: %m"); | |
e4b61340 LP |
8956 | |
8957 | return 0; | |
8958 | } | |
8959 | ||
4fbd7192 | 8960 | static int start_with_fallback(void) { |
4fbd7192 | 8961 | /* First, try systemd via D-Bus. */ |
2e13c0c3 | 8962 | if (start_unit(0, NULL, NULL) == 0) |
4fbd7192 | 8963 | return 0; |
e4b61340 | 8964 | |
2771aaf5 | 8965 | #if HAVE_SYSV_COMPAT |
2853b60a | 8966 | /* Nothing else worked, so let's try /dev/initctl */ |
63a3b3cb | 8967 | if (talk_initctl(action_to_runlevel()) > 0) |
48ec22bc | 8968 | return 0; |
2771aaf5 | 8969 | #endif |
d55ae9e6 | 8970 | |
baaa35ad ZJS |
8971 | return log_error_errno(SYNTHETIC_ERRNO(EIO), |
8972 | "Failed to talk to init daemon."); | |
e4b61340 LP |
8973 | } |
8974 | ||
477def80 | 8975 | static int halt_now(enum action a) { |
c01dcddf | 8976 | /* The kernel will automatically flush ATA disks and suchlike on reboot(), but the file systems need to be |
f03ec2b1 | 8977 | * synced explicitly in advance. */ |
1ae17672 | 8978 | if (!arg_no_sync && !arg_dry_run) |
f3f054f0 | 8979 | (void) sync(); |
4a3ad399 | 8980 | |
1ae17672 ZJS |
8981 | /* Make sure C-A-D is handled by the kernel from this point on... */ |
8982 | if (!arg_dry_run) | |
8983 | (void) reboot(RB_ENABLE_CAD); | |
e606bb61 | 8984 | |
4c80c73c | 8985 | switch (a) { |
e606bb61 LP |
8986 | |
8987 | case ACTION_HALT: | |
60675884 LP |
8988 | if (!arg_quiet) |
8989 | log_info("Halting."); | |
1ae17672 ZJS |
8990 | if (arg_dry_run) |
8991 | return 0; | |
19578bb2 | 8992 | (void) reboot(RB_HALT_SYSTEM); |
477def80 | 8993 | return -errno; |
e606bb61 LP |
8994 | |
8995 | case ACTION_POWEROFF: | |
60675884 LP |
8996 | if (!arg_quiet) |
8997 | log_info("Powering off."); | |
1ae17672 ZJS |
8998 | if (arg_dry_run) |
8999 | return 0; | |
19578bb2 | 9000 | (void) reboot(RB_POWER_OFF); |
477def80 | 9001 | return -errno; |
e606bb61 | 9002 | |
98d52feb | 9003 | case ACTION_KEXEC: |
c01dcddf LP |
9004 | case ACTION_REBOOT: |
9005 | return reboot_with_parameter(REBOOT_FALLBACK | | |
9006 | (arg_quiet ? 0 : REBOOT_LOG) | | |
9007 | (arg_dry_run ? REBOOT_DRY_RUN : 0)); | |
e606bb61 | 9008 | |
477def80 LP |
9009 | default: |
9010 | assert_not_reached("Unknown action."); | |
9011 | } | |
e606bb61 LP |
9012 | } |
9013 | ||
56a730fa | 9014 | static int logind_schedule_shutdown(void) { |
7f56c3d4 | 9015 | |
cf9cda44 | 9016 | #if ENABLE_LOGIND |
4afd3348 | 9017 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
56a730fa LP |
9018 | char date[FORMAT_TIMESTAMP_MAX]; |
9019 | const char *action; | |
4fbd7192 | 9020 | sd_bus *bus; |
56a730fa LP |
9021 | int r; |
9022 | ||
4fbd7192 | 9023 | r = acquire_bus(BUS_FULL, &bus); |
56a730fa | 9024 | if (r < 0) |
4fbd7192 | 9025 | return r; |
56a730fa LP |
9026 | |
9027 | switch (arg_action) { | |
9028 | case ACTION_HALT: | |
9029 | action = "halt"; | |
9030 | break; | |
9031 | case ACTION_POWEROFF: | |
9032 | action = "poweroff"; | |
9033 | break; | |
9034 | case ACTION_KEXEC: | |
9035 | action = "kexec"; | |
9036 | break; | |
a4420f7b LP |
9037 | case ACTION_EXIT: |
9038 | action = "exit"; | |
9039 | break; | |
9040 | case ACTION_REBOOT: | |
56a730fa LP |
9041 | default: |
9042 | action = "reboot"; | |
9043 | break; | |
9044 | } | |
9045 | ||
1ae17672 | 9046 | if (arg_dry_run) |
56a730fa LP |
9047 | action = strjoina("dry-", action); |
9048 | ||
d2ad7e1b ZJS |
9049 | (void) logind_set_wall_message(); |
9050 | ||
56a730fa | 9051 | r = sd_bus_call_method( |
4fbd7192 | 9052 | bus, |
56a730fa LP |
9053 | "org.freedesktop.login1", |
9054 | "/org/freedesktop/login1", | |
9055 | "org.freedesktop.login1.Manager", | |
9056 | "ScheduleShutdown", | |
9057 | &error, | |
9058 | NULL, | |
9059 | "st", | |
9060 | action, | |
9061 | arg_when); | |
9062 | if (r < 0) | |
9063 | return log_warning_errno(r, "Failed to call ScheduleShutdown in logind, proceeding with immediate shutdown: %s", bus_error_message(&error, r)); | |
9064 | ||
60675884 LP |
9065 | if (!arg_quiet) |
9066 | log_info("Shutdown scheduled for %s, use 'shutdown -c' to cancel.", format_timestamp(date, sizeof(date), arg_when)); | |
56a730fa | 9067 | return 0; |
cf9cda44 | 9068 | #else |
2f50e59f LP |
9069 | return log_error_errno(SYNTHETIC_ERRNO(ENOSYS), |
9070 | "Cannot schedule shutdown without logind support, proceeding with immediate shutdown."); | |
807690fb | 9071 | #endif |
56a730fa LP |
9072 | } |
9073 | ||
4fbd7192 | 9074 | static int halt_main(void) { |
e4b61340 LP |
9075 | int r; |
9076 | ||
4fbd7192 | 9077 | r = logind_check_inhibitors(arg_action); |
748ebafa LP |
9078 | if (r < 0) |
9079 | return r; | |
b37844d3 | 9080 | |
940bec70 DJL |
9081 | /* Delayed shutdown requested, and was successful */ |
9082 | if (arg_when > 0 && logind_schedule_shutdown() == 0) | |
9083 | return 0; | |
9084 | /* no delay, or logind failed or is not at all available */ | |
7f96539d | 9085 | |
bc8c2f5c | 9086 | if (geteuid() != 0) { |
1ae17672 | 9087 | if (arg_dry_run || arg_force > 0) { |
fba868fa | 9088 | (void) must_be_root(); |
2ac3930f IS |
9089 | return -EPERM; |
9090 | } | |
9091 | ||
7e59bfcb | 9092 | /* Try logind if we are a normal user and no special |
d35f51ea | 9093 | * mode applies. Maybe polkit allows us to shutdown |
7e59bfcb | 9094 | * the machine. */ |
36b69c31 | 9095 | if (IN_SET(arg_action, ACTION_POWEROFF, ACTION_REBOOT, ACTION_HALT)) { |
4fbd7192 | 9096 | r = logind_reboot(arg_action); |
4c80c73c KS |
9097 | if (r >= 0) |
9098 | return r; | |
a9085ea3 | 9099 | if (IN_SET(r, -EOPNOTSUPP, -EINPROGRESS)) |
7f96539d LP |
9100 | /* requested operation is not |
9101 | * supported on the local system or | |
9102 | * already in progress */ | |
a9085ea3 IS |
9103 | return r; |
9104 | /* on all other errors, try low-level operation */ | |
4c80c73c | 9105 | } |
bc8c2f5c LP |
9106 | } |
9107 | ||
2955e0d4 GJ |
9108 | /* In order to minimize the difference between operation with and |
9109 | * without logind, we explicitly enable non-blocking mode for this, | |
9110 | * as logind's shutdown operations are always non-blocking. */ | |
9111 | arg_no_block = true; | |
9112 | ||
1ae17672 | 9113 | if (!arg_dry_run && !arg_force) |
4fbd7192 | 9114 | return start_with_fallback(); |
e4b61340 | 9115 | |
2ac3930f IS |
9116 | assert(geteuid() == 0); |
9117 | ||
d90e1a30 LP |
9118 | if (!arg_no_wtmp) { |
9119 | if (sd_booted() > 0) | |
9120 | log_debug("Not writing utmp record, assuming that systemd-update-utmp is used."); | |
7e59bfcb LP |
9121 | else { |
9122 | r = utmp_put_shutdown(); | |
9123 | if (r < 0) | |
da927ba9 | 9124 | log_warning_errno(r, "Failed to write utmp record: %m"); |
7e59bfcb | 9125 | } |
d90e1a30 | 9126 | } |
e4b61340 | 9127 | |
1ae17672 | 9128 | if (arg_dry_run) |
e4b61340 LP |
9129 | return 0; |
9130 | ||
477def80 | 9131 | r = halt_now(arg_action); |
691395d8 | 9132 | return log_error_errno(r, "Failed to reboot: %m"); |
e4b61340 LP |
9133 | } |
9134 | ||
9135 | static int runlevel_main(void) { | |
9136 | int r, runlevel, previous; | |
9137 | ||
729e3769 LP |
9138 | r = utmp_get_runlevel(&runlevel, &previous); |
9139 | if (r < 0) { | |
9140 | puts("unknown"); | |
e4b61340 LP |
9141 | return r; |
9142 | } | |
9143 | ||
9144 | printf("%c %c\n", | |
9145 | previous <= 0 ? 'N' : previous, | |
9146 | runlevel <= 0 ? 'N' : runlevel); | |
9147 | ||
9148 | return 0; | |
9149 | } | |
9150 | ||
2cf05793 | 9151 | static int logind_cancel_shutdown(void) { |
349cc4a5 | 9152 | #if ENABLE_LOGIND |
4afd3348 | 9153 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
4fbd7192 | 9154 | sd_bus *bus; |
949d9ce9 LP |
9155 | int r; |
9156 | ||
4fbd7192 | 9157 | r = acquire_bus(BUS_FULL, &bus); |
949d9ce9 | 9158 | if (r < 0) |
4fbd7192 | 9159 | return r; |
949d9ce9 | 9160 | |
4fbd7192 | 9161 | (void) logind_set_wall_message(); |
949d9ce9 LP |
9162 | |
9163 | r = sd_bus_call_method( | |
4fbd7192 | 9164 | bus, |
949d9ce9 LP |
9165 | "org.freedesktop.login1", |
9166 | "/org/freedesktop/login1", | |
9167 | "org.freedesktop.login1.Manager", | |
9168 | "CancelScheduledShutdown", | |
9169 | &error, | |
9170 | NULL, NULL); | |
9171 | if (r < 0) | |
9172 | return log_warning_errno(r, "Failed to talk to logind, shutdown hasn't been cancelled: %s", bus_error_message(&error, r)); | |
9173 | ||
9174 | return 0; | |
2cf05793 | 9175 | #else |
2f50e59f LP |
9176 | return log_error_errno(SYNTHETIC_ERRNO(ENOSYS), |
9177 | "Not compiled with logind support, cannot cancel scheduled shutdowns."); | |
2cf05793 | 9178 | #endif |
949d9ce9 LP |
9179 | } |
9180 | ||
f2a3de01 | 9181 | static int run(int argc, char *argv[]) { |
f459b602 | 9182 | int r; |
e4b61340 | 9183 | |
a9cdc94f | 9184 | setlocale(LC_ALL, ""); |
e4b61340 | 9185 | log_parse_environment(); |
2396fb04 | 9186 | log_open(); |
1abaf488 LP |
9187 | |
9188 | /* The journal merging logic potentially needs a lot of fds. */ | |
9189 | (void) rlimit_nofile_bump(HIGH_RLIMIT_NOFILE); | |
9190 | ||
592705f2 | 9191 | sigbus_install(); |
e4b61340 | 9192 | |
184ecaf7 DR |
9193 | /* Explicitly not on_tty() to avoid setting cached value. |
9194 | * This becomes relevant for piping output which might be | |
9195 | * ellipsized. */ | |
9196 | original_stdout_is_tty = isatty(STDOUT_FILENO); | |
9197 | ||
04ebb595 | 9198 | r = parse_argv(argc, argv); |
f459b602 | 9199 | if (r <= 0) |
e4b61340 | 9200 | goto finish; |
7e4249b9 | 9201 | |
040524b4 | 9202 | if (arg_action != ACTION_SYSTEMCTL && running_in_chroot() > 0) { |
60675884 LP |
9203 | if (!arg_quiet) |
9204 | log_info("Running in chroot, ignoring request."); | |
f459b602 | 9205 | r = 0; |
82e23ddd LP |
9206 | goto finish; |
9207 | } | |
9208 | ||
41dd15e4 LP |
9209 | /* systemctl_main() will print an error message for the bus |
9210 | * connection, but only if it needs to */ | |
e4b61340 LP |
9211 | |
9212 | switch (arg_action) { | |
9213 | ||
22f4096c | 9214 | case ACTION_SYSTEMCTL: |
e449de87 | 9215 | r = systemctl_main(argc, argv); |
e4b61340 | 9216 | break; |
e4b61340 | 9217 | |
081dc638 AJ |
9218 | /* Legacy command aliases set arg_action. They provide some fallbacks, |
9219 | * e.g. to tell sysvinit to reboot after you have installed systemd | |
9220 | * binaries. */ | |
9221 | ||
e4b61340 LP |
9222 | case ACTION_HALT: |
9223 | case ACTION_POWEROFF: | |
9224 | case ACTION_REBOOT: | |
5622dde3 | 9225 | case ACTION_KEXEC: |
4fbd7192 | 9226 | r = halt_main(); |
e4b61340 LP |
9227 | break; |
9228 | ||
e4b61340 LP |
9229 | case ACTION_RUNLEVEL2: |
9230 | case ACTION_RUNLEVEL3: | |
9231 | case ACTION_RUNLEVEL4: | |
9232 | case ACTION_RUNLEVEL5: | |
9233 | case ACTION_RESCUE: | |
4fbd7192 | 9234 | r = start_with_fallback(); |
e4b61340 | 9235 | break; |
7e4249b9 | 9236 | |
e4b61340 LP |
9237 | case ACTION_RELOAD: |
9238 | case ACTION_REEXEC: | |
4fbd7192 | 9239 | r = reload_with_fallback(); |
e4b61340 LP |
9240 | break; |
9241 | ||
949d9ce9 | 9242 | case ACTION_CANCEL_SHUTDOWN: |
2cf05793 | 9243 | r = logind_cancel_shutdown(); |
f6144808 LP |
9244 | break; |
9245 | ||
eb22ac37 | 9246 | case ACTION_RUNLEVEL: |
4f16c1f4 LP |
9247 | r = runlevel_main(); |
9248 | break; | |
9249 | ||
081dc638 AJ |
9250 | case ACTION_EXIT: |
9251 | case ACTION_SUSPEND: | |
9252 | case ACTION_HIBERNATE: | |
9253 | case ACTION_HYBRID_SLEEP: | |
e68c79db | 9254 | case ACTION_SUSPEND_THEN_HIBERNATE: |
081dc638 AJ |
9255 | case ACTION_EMERGENCY: |
9256 | case ACTION_DEFAULT: | |
9257 | /* systemctl verbs with no equivalent in the legacy commands. | |
9258 | * These cannot appear in arg_action. Fall through. */ | |
9259 | ||
f459b602 | 9260 | case _ACTION_INVALID: |
e4b61340 LP |
9261 | default: |
9262 | assert_not_reached("Unknown action"); | |
9263 | } | |
7e4249b9 LP |
9264 | |
9265 | finish: | |
cf647b69 LP |
9266 | release_busses(); |
9267 | ||
d1405af3 ZJS |
9268 | /* Note that we return r here, not 0, so that we can implement the LSB-like return codes */ |
9269 | return r; | |
7e4249b9 | 9270 | } |
d1405af3 ZJS |
9271 | |
9272 | DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run); |