]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
7e4249b9 | 2 | |
03a7b521 LP |
3 | #include <errno.h> |
4 | #include <fcntl.h> | |
7e4249b9 LP |
5 | #include <getopt.h> |
6 | #include <stdbool.h> | |
03a7b521 LP |
7 | #include <stddef.h> |
8 | #include <stdio.h> | |
4c787df7 | 9 | #include <sys/mount.h> |
4bb2e9d4 | 10 | #include <sys/prctl.h> |
03a7b521 | 11 | #include <sys/reboot.h> |
03a7b521 | 12 | #include <unistd.h> |
81527be1 | 13 | |
03a7b521 | 14 | #include "sd-bus.h" |
f459b602 | 15 | #include "sd-daemon.h" |
adb6cd9b | 16 | #include "sd-event.h" |
f459b602 | 17 | #include "sd-login.h" |
03a7b521 | 18 | |
b5efdb8a | 19 | #include "alloc-util.h" |
4bb2e9d4 | 20 | #include "bootspec.h" |
03a7b521 LP |
21 | #include "bus-common-errors.h" |
22 | #include "bus-error.h" | |
9b71e4ab | 23 | #include "bus-locator.h" |
807542be | 24 | #include "bus-map-properties.h" |
03a7b521 | 25 | #include "bus-message.h" |
9176326b | 26 | #include "bus-print-properties.h" |
25b1d72d | 27 | #include "bus-unit-procs.h" |
291d565a | 28 | #include "bus-unit-util.h" |
e45c81b8 | 29 | #include "bus-wait-for-jobs.h" |
d81bc51f | 30 | #include "bus-wait-for-units.h" |
ab35fb1b | 31 | #include "cgroup-show.h" |
c6c18be3 | 32 | #include "cgroup-util.h" |
cda134ab | 33 | #include "copy.h" |
a047f4f1 | 34 | #include "cpu-set-util.h" |
e8630e69 | 35 | #include "dirent-util.h" |
ad2a0358 | 36 | #include "dropin.h" |
2fec5854 | 37 | #include "efi-loader.h" |
5bdf2243 | 38 | #include "efivars.h" |
03a7b521 | 39 | #include "env-util.h" |
804ee07c | 40 | #include "escape.h" |
b3d59367 | 41 | #include "exec-util.h" |
03a7b521 | 42 | #include "exit-status.h" |
3ffd4af2 | 43 | #include "fd-util.h" |
de9a8fe1 | 44 | #include "format-table.h" |
f97b34a6 | 45 | #include "format-util.h" |
f4f15635 | 46 | #include "fs-util.h" |
7d50b32a | 47 | #include "glob-util.h" |
08f3be7a | 48 | #include "hexdecoct.h" |
c52a937b | 49 | #include "hostname-util.h" |
0a970718 | 50 | #include "in-addr-util.h" |
03a7b521 LP |
51 | #include "initreq.h" |
52 | #include "install.h" | |
c004493c | 53 | #include "io-util.h" |
305a2850 | 54 | #include "journal-util.h" |
03a7b521 | 55 | #include "list.h" |
8752c575 | 56 | #include "locale-util.h" |
03a7b521 LP |
57 | #include "log.h" |
58 | #include "logs-show.h" | |
59 | #include "macro.h" | |
d1405af3 | 60 | #include "main-func.h" |
0a970718 | 61 | #include "memory-util.h" |
03a7b521 | 62 | #include "mkdir.h" |
1808f768 | 63 | #include "numa-util.h" |
03a7b521 | 64 | #include "pager.h" |
6bedfcbb | 65 | #include "parse-util.h" |
03a7b521 LP |
66 | #include "path-lookup.h" |
67 | #include "path-util.h" | |
294bf0c3 | 68 | #include "pretty-print.h" |
01771226 | 69 | #include "proc-cmdline.h" |
03a7b521 | 70 | #include "process-util.h" |
e3631d1c | 71 | #include "reboot-util.h" |
78f22b97 | 72 | #include "rlimit-util.h" |
03a7b521 | 73 | #include "set.h" |
592705f2 | 74 | #include "sigbus.h" |
24882e06 | 75 | #include "signal-util.h" |
03a7b521 | 76 | #include "socket-util.h" |
760877e9 | 77 | #include "sort-util.h" |
03a7b521 LP |
78 | #include "spawn-ask-password-agent.h" |
79 | #include "spawn-polkit-agent.h" | |
80 | #include "special.h" | |
8fcde012 | 81 | #include "stat-util.h" |
8559e61d | 82 | #include "string-table.h" |
03a7b521 | 83 | #include "strv.h" |
63a3b3cb | 84 | #include "sysv-compat.h" |
03a7b521 | 85 | #include "terminal-util.h" |
e4de7287 | 86 | #include "tmpfile-util.h" |
89ada3ba | 87 | #include "unit-def.h" |
46ff70ca | 88 | #include "unit-file.h" |
03a7b521 | 89 | #include "unit-name.h" |
b1d4f8e1 | 90 | #include "user-util.h" |
305a2850 | 91 | #include "utf8.h" |
03a7b521 | 92 | #include "utmp-wtmp.h" |
e449de87 | 93 | #include "verbs.h" |
7f4b3c5e | 94 | #include "virt.h" |
7e4249b9 | 95 | |
20b3f379 | 96 | static char **arg_types = NULL; |
9b9b3d36 | 97 | static char **arg_states = NULL; |
20b3f379 | 98 | static char **arg_properties = NULL; |
7e4249b9 | 99 | static bool arg_all = false; |
afba4199 ZJS |
100 | static enum dependency { |
101 | DEPENDENCY_FORWARD, | |
102 | DEPENDENCY_REVERSE, | |
103 | DEPENDENCY_AFTER, | |
104 | DEPENDENCY_BEFORE, | |
071066a5 | 105 | _DEPENDENCY_MAX |
afba4199 | 106 | } arg_dependency = DEPENDENCY_FORWARD; |
e67c3609 | 107 | static const char *arg_job_mode = "replace"; |
729e3769 | 108 | static UnitFileScope arg_scope = UNIT_FILE_SYSTEM; |
93a08841 | 109 | static bool arg_wait = false; |
ee5762e3 | 110 | static bool arg_no_block = false; |
ebed32bf | 111 | static bool arg_no_legend = false; |
0221d68a | 112 | static PagerFlags arg_pager_flags = 0; |
e4b61340 | 113 | static bool arg_no_wtmp = false; |
f3f054f0 | 114 | static bool arg_no_sync = false; |
514f4ef5 | 115 | static bool arg_no_wall = false; |
ee5762e3 | 116 | static bool arg_no_reload = false; |
4f9a9105 | 117 | static bool arg_value = false; |
991f2a39 | 118 | static bool arg_show_types = false; |
b37844d3 | 119 | static bool arg_ignore_inhibitors = false; |
1ae17672 | 120 | static bool arg_dry_run = false; |
0183528f | 121 | static bool arg_quiet = false; |
ee5762e3 | 122 | static bool arg_full = false; |
1238ee09 | 123 | static bool arg_recursive = false; |
e9c387c8 | 124 | static bool arg_with_dependencies = false; |
85d9b598 | 125 | static bool arg_show_transaction = false; |
e606bb61 | 126 | static int arg_force = 0; |
16f017fa | 127 | static bool arg_ask_password = false; |
729e3769 | 128 | static bool arg_runtime = false; |
d309c1c3 | 129 | static UnitFilePresetMode arg_preset_mode = UNIT_FILE_PRESET_FULL; |
e4b61340 | 130 | static char **arg_wall = NULL; |
8a0867d6 | 131 | static const char *arg_kill_who = NULL; |
8a0867d6 | 132 | static int arg_signal = SIGTERM; |
0f03c2a4 | 133 | static char *arg_root = NULL; |
f6144808 | 134 | static usec_t arg_when = 0; |
dae710be | 135 | static const char *arg_reboot_argument = NULL; |
4445a875 | 136 | static enum action { |
e4b61340 LP |
137 | ACTION_SYSTEMCTL, |
138 | ACTION_HALT, | |
139 | ACTION_POWEROFF, | |
140 | ACTION_REBOOT, | |
20b09ca7 LP |
141 | ACTION_KEXEC, |
142 | ACTION_EXIT, | |
6edd7d0a LP |
143 | ACTION_SUSPEND, |
144 | ACTION_HIBERNATE, | |
6524990f | 145 | ACTION_HYBRID_SLEEP, |
e68c79db | 146 | ACTION_SUSPEND_THEN_HIBERNATE, |
e4b61340 LP |
147 | ACTION_RUNLEVEL2, |
148 | ACTION_RUNLEVEL3, | |
149 | ACTION_RUNLEVEL4, | |
150 | ACTION_RUNLEVEL5, | |
151 | ACTION_RESCUE, | |
514f4ef5 LP |
152 | ACTION_EMERGENCY, |
153 | ACTION_DEFAULT, | |
e4b61340 LP |
154 | ACTION_RELOAD, |
155 | ACTION_REEXEC, | |
156 | ACTION_RUNLEVEL, | |
f6144808 | 157 | ACTION_CANCEL_SHUTDOWN, |
913c1916 AJ |
158 | _ACTION_MAX, |
159 | _ACTION_INVALID = -1 | |
e4b61340 | 160 | } arg_action = ACTION_SYSTEMCTL; |
f459b602 | 161 | static BusTransport arg_transport = BUS_TRANSPORT_LOCAL; |
fc2ffaf1 | 162 | static const char *arg_host = NULL; |
df50185b LP |
163 | static unsigned arg_lines = 10; |
164 | static OutputMode arg_output = OUTPUT_SHORT; | |
5d0c05e5 | 165 | static bool arg_plain = false; |
5bdf2243 | 166 | static bool arg_firmware_setup = false; |
97af80c5 LP |
167 | static usec_t arg_boot_loader_menu = USEC_INFINITY; |
168 | static const char *arg_boot_loader_entry = NULL; | |
57ab2eab | 169 | static bool arg_now = false; |
82948f6c LP |
170 | static bool arg_jobs_before = false; |
171 | static bool arg_jobs_after = false; | |
d81bc51f | 172 | static char **arg_clean_what = NULL; |
1238ee09 | 173 | |
e8630e69 ZJS |
174 | /* This is a global cache that will be constructed on first use. */ |
175 | static Hashmap *cached_id_map = NULL; | |
176 | static Hashmap *cached_name_map = NULL; | |
177 | ||
61ff45db LP |
178 | STATIC_DESTRUCTOR_REGISTER(arg_wall, strv_freep); |
179 | STATIC_DESTRUCTOR_REGISTER(arg_root, freep); | |
180 | STATIC_DESTRUCTOR_REGISTER(arg_types, strv_freep); | |
181 | STATIC_DESTRUCTOR_REGISTER(arg_states, strv_freep); | |
182 | STATIC_DESTRUCTOR_REGISTER(arg_properties, strv_freep); | |
d81bc51f | 183 | STATIC_DESTRUCTOR_REGISTER(arg_clean_what, strv_freep); |
e8630e69 ZJS |
184 | STATIC_DESTRUCTOR_REGISTER(cached_id_map, hashmap_freep); |
185 | STATIC_DESTRUCTOR_REGISTER(cached_name_map, hashmap_freep); | |
61ff45db | 186 | |
e449de87 | 187 | static int daemon_reload(int argc, char *argv[], void* userdata); |
2853b60a | 188 | static int trivial_method(int argc, char *argv[], void *userdata); |
477def80 | 189 | static int halt_now(enum action a); |
fa0d5878 | 190 | static int get_state_one_unit(sd_bus *bus, const char *name, UnitActiveState *active_state); |
dbc2c080 | 191 | |
4fbd7192 LP |
192 | static bool original_stdout_is_tty; |
193 | ||
194 | typedef enum BusFocus { | |
195 | BUS_FULL, /* The full bus indicated via --system or --user */ | |
196 | BUS_MANAGER, /* The manager itself, possibly directly, possibly via the bus */ | |
197 | _BUS_FOCUS_MAX | |
198 | } BusFocus; | |
199 | ||
5238e957 | 200 | static sd_bus *buses[_BUS_FOCUS_MAX] = {}; |
4fbd7192 | 201 | |
b3796dd8 JS |
202 | static UnitFileFlags args_to_flags(void) { |
203 | return (arg_runtime ? UNIT_FILE_RUNTIME : 0) | | |
204 | (arg_force ? UNIT_FILE_FORCE : 0); | |
205 | } | |
206 | ||
4fbd7192 LP |
207 | static int acquire_bus(BusFocus focus, sd_bus **ret) { |
208 | int r; | |
209 | ||
210 | assert(focus < _BUS_FOCUS_MAX); | |
211 | assert(ret); | |
212 | ||
213 | /* We only go directly to the manager, if we are using a local transport */ | |
214 | if (arg_transport != BUS_TRANSPORT_LOCAL) | |
215 | focus = BUS_FULL; | |
216 | ||
d4015567 LP |
217 | if (getenv_bool("SYSTEMCTL_FORCE_BUS") > 0) |
218 | focus = BUS_FULL; | |
219 | ||
5238e957 | 220 | if (!buses[focus]) { |
4fbd7192 LP |
221 | bool user; |
222 | ||
223 | user = arg_scope != UNIT_FILE_SYSTEM; | |
224 | ||
225 | if (focus == BUS_MANAGER) | |
5238e957 | 226 | r = bus_connect_transport_systemd(arg_transport, arg_host, user, &buses[focus]); |
4fbd7192 | 227 | else |
5238e957 | 228 | r = bus_connect_transport(arg_transport, arg_host, user, &buses[focus]); |
4fbd7192 LP |
229 | if (r < 0) |
230 | return log_error_errno(r, "Failed to connect to bus: %m"); | |
231 | ||
5238e957 | 232 | (void) sd_bus_set_allow_interactive_authorization(buses[focus], arg_ask_password); |
4fbd7192 LP |
233 | } |
234 | ||
5238e957 | 235 | *ret = buses[focus]; |
4fbd7192 LP |
236 | return 0; |
237 | } | |
238 | ||
239 | static void release_busses(void) { | |
240 | BusFocus w; | |
241 | ||
242 | for (w = 0; w < _BUS_FOCUS_MAX; w++) | |
5238e957 | 243 | buses[w] = sd_bus_flush_close_unref(buses[w]); |
4fbd7192 LP |
244 | } |
245 | ||
c59e2ec6 | 246 | static void ask_password_agent_open_maybe(void) { |
729e3769 | 247 | /* Open the password agent as a child process if necessary */ |
501fc174 | 248 | |
1ae17672 ZJS |
249 | if (arg_dry_run) |
250 | return; | |
251 | ||
729e3769 | 252 | if (arg_scope != UNIT_FILE_SYSTEM) |
501fc174 LP |
253 | return; |
254 | ||
c59e2ec6 | 255 | ask_password_agent_open_if_enabled(arg_transport, arg_ask_password); |
6bb92a16 LP |
256 | } |
257 | ||
8a4b13c5 | 258 | static void polkit_agent_open_maybe(void) { |
6bb92a16 LP |
259 | /* Open the polkit agent as a child process if necessary */ |
260 | ||
6bb92a16 LP |
261 | if (arg_scope != UNIT_FILE_SYSTEM) |
262 | return; | |
263 | ||
8a4b13c5 | 264 | polkit_agent_open_if_enabled(arg_transport, arg_ask_password); |
501fc174 LP |
265 | } |
266 | ||
3c756001 LP |
267 | static OutputFlags get_output_flags(void) { |
268 | return | |
269 | arg_all * OUTPUT_SHOW_ALL | | |
459b9f9f | 270 | (arg_full || !on_tty() || pager_have()) * OUTPUT_FULL_WIDTH | |
40c9fe4c | 271 | colors_enabled() * OUTPUT_COLOR | |
8b0cc9a3 | 272 | !arg_quiet * OUTPUT_WARN_CUTOFF; |
3c756001 LP |
273 | } |
274 | ||
f459b602 | 275 | static int translate_bus_error_to_exit_status(int r, const sd_bus_error *error) { |
22f4096c LP |
276 | assert(error); |
277 | ||
f459b602 | 278 | if (!sd_bus_error_is_set(error)) |
22f4096c LP |
279 | return r; |
280 | ||
f459b602 MAP |
281 | if (sd_bus_error_has_name(error, SD_BUS_ERROR_ACCESS_DENIED) || |
282 | sd_bus_error_has_name(error, BUS_ERROR_ONLY_BY_DEPENDENCY) || | |
283 | sd_bus_error_has_name(error, BUS_ERROR_NO_ISOLATION) || | |
284 | sd_bus_error_has_name(error, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE)) | |
22f4096c LP |
285 | return EXIT_NOPERMISSION; |
286 | ||
f459b602 | 287 | if (sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT)) |
22f4096c LP |
288 | return EXIT_NOTINSTALLED; |
289 | ||
f459b602 | 290 | if (sd_bus_error_has_name(error, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE) || |
718db961 | 291 | sd_bus_error_has_name(error, SD_BUS_ERROR_NOT_SUPPORTED)) |
22f4096c LP |
292 | return EXIT_NOTIMPLEMENTED; |
293 | ||
f459b602 | 294 | if (sd_bus_error_has_name(error, BUS_ERROR_LOAD_FAILED)) |
22f4096c LP |
295 | return EXIT_NOTCONFIGURED; |
296 | ||
297 | if (r != 0) | |
298 | return r; | |
299 | ||
300 | return EXIT_FAILURE; | |
301 | } | |
302 | ||
4fbd7192 | 303 | static bool install_client_side(void) { |
4fbd7192 LP |
304 | /* Decides when to execute enable/disable/... operations |
305 | * client-side rather than server-side. */ | |
4f16c1f4 | 306 | |
f38951a6 | 307 | if (running_in_chroot_or_offline()) |
729e3769 LP |
308 | return true; |
309 | ||
310 | if (sd_booted() <= 0) | |
311 | return true; | |
312 | ||
313 | if (!isempty(arg_root)) | |
314 | return true; | |
315 | ||
316 | if (arg_scope == UNIT_FILE_GLOBAL) | |
317 | return true; | |
318 | ||
b41b9d2a LP |
319 | /* Unsupported environment variable, mostly for debugging purposes */ |
320 | if (getenv_bool("SYSTEMCTL_INSTALL_CLIENT_SIDE") > 0) | |
321 | return true; | |
322 | ||
729e3769 LP |
323 | return false; |
324 | } | |
325 | ||
93bab288 | 326 | static int compare_unit_info(const UnitInfo *a, const UnitInfo *b) { |
1238ee09 LP |
327 | int r; |
328 | ||
329 | /* First, order by machine */ | |
6cf3011c ZJS |
330 | r = strcasecmp_ptr(a->machine, b->machine); |
331 | if (r != 0) | |
332 | return r; | |
36c32ba2 | 333 | |
1238ee09 | 334 | /* Second, order by unit type */ |
6cf3011c ZJS |
335 | r = strcasecmp_ptr(strrchr(a->id, '.'), strrchr(b->id, '.')); |
336 | if (r != 0) | |
337 | return r; | |
36c32ba2 | 338 | |
1238ee09 | 339 | /* Third, order by name */ |
93bab288 | 340 | return strcasecmp(a->id, b->id); |
36c32ba2 LP |
341 | } |
342 | ||
16484a8a ZJS |
343 | static const char* unit_type_suffix(const char *name) { |
344 | const char *dot; | |
345 | ||
346 | dot = strrchr(name, '.'); | |
347 | if (!dot) | |
348 | return ""; | |
349 | ||
350 | return dot + 1; | |
351 | } | |
352 | ||
d8fba7c6 | 353 | static bool output_show_unit(const UnitInfo *u, char **patterns) { |
f8591ee1 LP |
354 | assert(u); |
355 | ||
2404701e | 356 | if (!strv_fnmatch_or_empty(patterns, u->id, FNM_NOESCAPE)) |
d8fba7c6 | 357 | return false; |
d8fba7c6 | 358 | |
16484a8a ZJS |
359 | if (arg_types && !strv_find(arg_types, unit_type_suffix(u->id))) |
360 | return false; | |
6c71341a ZJS |
361 | |
362 | if (arg_all) | |
363 | return true; | |
364 | ||
409472cb FB |
365 | /* Note that '--all' is not purely a state filter, but also a |
366 | * filter that hides units that "follow" other units (which is | |
367 | * used for device units that appear under different names). */ | |
368 | if (!isempty(u->following)) | |
369 | return false; | |
370 | ||
ebc96265 FB |
371 | if (!strv_isempty(arg_states)) |
372 | return true; | |
373 | ||
374 | /* By default show all units except the ones in inactive | |
375 | * state and with no pending job */ | |
6c71341a ZJS |
376 | if (u->job_id > 0) |
377 | return true; | |
378 | ||
409472cb | 379 | if (streq(u->active_state, "inactive")) |
6c71341a ZJS |
380 | return false; |
381 | ||
382 | return true; | |
33330222 ZJS |
383 | } |
384 | ||
49cd06fa LP |
385 | static int output_table(Table *table) { |
386 | int r; | |
387 | ||
388 | assert(table); | |
389 | ||
390 | if (OUTPUT_MODE_IS_JSON(arg_output)) | |
391 | r = table_print_json(table, NULL, output_mode_to_json_format_flags(arg_output) | JSON_FORMAT_COLOR_AUTO); | |
392 | else | |
393 | r = table_print(table, NULL); | |
394 | if (r < 0) | |
4b6607d9 | 395 | return table_log_print_error(r); |
49cd06fa LP |
396 | |
397 | return 0; | |
398 | } | |
399 | ||
1238ee09 | 400 | static int output_units_list(const UnitInfo *unit_infos, unsigned c) { |
de9a8fe1 | 401 | _cleanup_(table_unrefp) Table *table = NULL; |
de9a8fe1 | 402 | int r; |
cbc9fbd1 | 403 | |
de9a8fe1 FS |
404 | table = table_new("", "unit", "load", "active", "sub", "job", "description"); |
405 | if (!table) | |
406 | return log_oom(); | |
cbc9fbd1 | 407 | |
de9a8fe1 | 408 | table_set_header(table, !arg_no_legend); |
1cabd2d0 JW |
409 | if (arg_plain) { |
410 | /* Hide the 'glyph' column when --plain is requested */ | |
d171e679 FS |
411 | r = table_hide_column_from_display(table, 0); |
412 | if (r < 0) | |
413 | return log_error_errno(r, "Failed to hide column: %m"); | |
414 | } | |
de9a8fe1 FS |
415 | if (arg_full) |
416 | table_set_width(table, 0); | |
4deb3b93 | 417 | |
0773357a LP |
418 | (void) table_set_empty_string(table, "-"); |
419 | ||
c9d243cd ZJS |
420 | int job_count = 0; |
421 | for (const UnitInfo *u = unit_infos; unit_infos && u < unit_infos + c; u++) { | |
de9a8fe1 FS |
422 | _cleanup_free_ char *j = NULL; |
423 | const char *on_underline = "", *on_loaded = "", *on_active = ""; | |
424 | const char *on_circle = "", *id; | |
16484a8a | 425 | bool circle = false, underline = false; |
b036fc00 | 426 | |
16484a8a ZJS |
427 | if (u + 1 < unit_infos + c && |
428 | !streq(unit_type_suffix(u->id), unit_type_suffix((u + 1)->id))) { | |
429 | on_underline = ansi_underline(); | |
16484a8a ZJS |
430 | underline = true; |
431 | } | |
432 | ||
c4555ad8 | 433 | if (STR_IN_SET(u->load_state, "error", "not-found", "bad-setting", "masked") && !arg_plain) { |
1fab5797 | 434 | on_circle = underline ? ansi_highlight_yellow_underline() : ansi_highlight_yellow(); |
90c3f79d | 435 | circle = true; |
16484a8a | 436 | on_loaded = underline ? ansi_highlight_red_underline() : ansi_highlight_red(); |
250ba664 | 437 | } else if (streq(u->active_state, "failed") && !arg_plain) { |
1fab5797 | 438 | on_circle = underline ? ansi_highlight_red_underline() : ansi_highlight_red(); |
90c3f79d | 439 | circle = true; |
16484a8a | 440 | on_active = underline ? ansi_highlight_red_underline() : ansi_highlight_red(); |
de9a8fe1 | 441 | } else { |
1fab5797 | 442 | on_circle = on_underline; |
de9a8fe1 FS |
443 | on_active = on_underline; |
444 | on_loaded = on_underline; | |
90c3f79d | 445 | } |
eb68c413 | 446 | |
1238ee09 | 447 | if (u->machine) { |
605405c6 | 448 | j = strjoin(u->machine, ":", u->id); |
1238ee09 LP |
449 | if (!j) |
450 | return log_oom(); | |
451 | ||
452 | id = j; | |
453 | } else | |
454 | id = u->id; | |
455 | ||
de9a8fe1 FS |
456 | r = table_add_many(table, |
457 | TABLE_STRING, circle ? special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE) : " ", | |
1fab5797 | 458 | TABLE_SET_BOTH_COLORS, on_circle, |
de9a8fe1 | 459 | TABLE_STRING, id, |
1fab5797 | 460 | TABLE_SET_BOTH_COLORS, on_active, |
de9a8fe1 | 461 | TABLE_STRING, u->load_state, |
1fab5797 | 462 | TABLE_SET_BOTH_COLORS, on_loaded, |
de9a8fe1 | 463 | TABLE_STRING, u->active_state, |
1fab5797 | 464 | TABLE_SET_BOTH_COLORS, on_active, |
de9a8fe1 | 465 | TABLE_STRING, u->sub_state, |
1fab5797 | 466 | TABLE_SET_BOTH_COLORS, on_active, |
de9a8fe1 | 467 | TABLE_STRING, u->job_id ? u->job_type: "", |
1fab5797 | 468 | TABLE_SET_BOTH_COLORS, u->job_id ? on_underline : "", |
de9a8fe1 | 469 | TABLE_STRING, u->description, |
1fab5797 | 470 | TABLE_SET_BOTH_COLORS, on_underline); |
de9a8fe1 FS |
471 | if (r < 0) |
472 | return table_log_add_error(r); | |
1238ee09 | 473 | |
de9a8fe1 FS |
474 | if (u->job_id != 0) |
475 | job_count++; | |
476 | } | |
eb68c413 | 477 | |
de9a8fe1 FS |
478 | if (job_count == 0) { |
479 | /* There's no data in the JOB column, so let's hide it */ | |
d171e679 | 480 | r = table_hide_column_from_display(table, 5); |
de9a8fe1 | 481 | if (r < 0) |
d171e679 | 482 | return log_error_errno(r, "Failed to hide column: %m"); |
eb68c413 ZJS |
483 | } |
484 | ||
49cd06fa | 485 | r = output_table(table); |
de9a8fe1 | 486 | if (r < 0) |
49cd06fa | 487 | return r; |
de9a8fe1 | 488 | |
ebed32bf | 489 | if (!arg_no_legend) { |
57f7ae4f | 490 | const char *on, *off; |
de9a8fe1 | 491 | size_t records = table_get_rows(table) - 1; |
57f7ae4f | 492 | |
de9a8fe1 | 493 | if (records > 0) { |
90c3f79d LP |
494 | puts("\n" |
495 | "LOAD = Reflects whether the unit definition was properly loaded.\n" | |
e3e0314b ZJS |
496 | "ACTIVE = The high-level unit activation state, i.e. generalization of SUB.\n" |
497 | "SUB = The low-level unit activation state, values depend on unit type."); | |
498 | puts(job_count ? "JOB = Pending job for the unit.\n" : ""); | |
0b5a519c | 499 | on = ansi_highlight(); |
1fc464f6 | 500 | off = ansi_normal(); |
57f7ae4f | 501 | } else { |
0b5a519c | 502 | on = ansi_highlight_red(); |
1fc464f6 | 503 | off = ansi_normal(); |
57f7ae4f | 504 | } |
eb68c413 | 505 | |
8eb0cafe | 506 | if (arg_all || strv_contains(arg_states, "inactive")) |
de9a8fe1 | 507 | printf("%s%zu loaded units listed.%s\n" |
57f7ae4f | 508 | "To show all installed unit files use 'systemctl list-unit-files'.\n", |
de9a8fe1 | 509 | on, records, off); |
8eb0cafe | 510 | else if (!arg_states) |
de9a8fe1 | 511 | printf("%s%zu loaded units listed.%s Pass --all to see loaded but inactive units, too.\n" |
57f7ae4f | 512 | "To show all installed unit files use 'systemctl list-unit-files'.\n", |
de9a8fe1 | 513 | on, records, off); |
8eb0cafe | 514 | else |
de9a8fe1 | 515 | printf("%zu loaded units listed.\n", records); |
eb68c413 | 516 | } |
1238ee09 LP |
517 | |
518 | return 0; | |
eb68c413 ZJS |
519 | } |
520 | ||
a00963a2 | 521 | static int get_unit_list( |
f459b602 | 522 | sd_bus *bus, |
1238ee09 LP |
523 | const char *machine, |
524 | char **patterns, | |
525 | UnitInfo **unit_infos, | |
526 | int c, | |
527 | sd_bus_message **_reply) { | |
a00963a2 | 528 | |
4afd3348 LP |
529 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
530 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
531 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
ca2d3784 | 532 | size_t size = c; |
1238ee09 | 533 | int r; |
f459b602 | 534 | UnitInfo u; |
313fe66f | 535 | bool fallback = false; |
7e4249b9 | 536 | |
265a7a2a | 537 | assert(bus); |
1238ee09 | 538 | assert(unit_infos); |
f459b602 | 539 | assert(_reply); |
1238ee09 | 540 | |
8010c205 | 541 | r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "ListUnitsByPatterns"); |
cdc06ed7 DS |
542 | if (r < 0) |
543 | return bus_log_create_error(r); | |
544 | ||
545 | r = sd_bus_message_append_strv(m, arg_states); | |
546 | if (r < 0) | |
547 | return bus_log_create_error(r); | |
548 | ||
313fe66f | 549 | r = sd_bus_message_append_strv(m, patterns); |
550 | if (r < 0) | |
551 | return bus_log_create_error(r); | |
552 | ||
cdc06ed7 | 553 | r = sd_bus_call(bus, m, 0, &error, &reply); |
33d52725 ZJS |
554 | if (r < 0 && (sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_METHOD) || |
555 | sd_bus_error_has_name(&error, SD_BUS_ERROR_ACCESS_DENIED))) { | |
313fe66f | 556 | /* Fallback to legacy ListUnitsFiltered method */ |
557 | fallback = true; | |
558 | log_debug_errno(r, "Failed to list units: %s Falling back to ListUnitsFiltered method.", bus_error_message(&error, r)); | |
559 | m = sd_bus_message_unref(m); | |
560 | sd_bus_error_free(&error); | |
561 | ||
8010c205 | 562 | r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "ListUnitsFiltered"); |
313fe66f | 563 | if (r < 0) |
564 | return bus_log_create_error(r); | |
565 | ||
566 | r = sd_bus_message_append_strv(m, arg_states); | |
567 | if (r < 0) | |
568 | return bus_log_create_error(r); | |
569 | ||
570 | r = sd_bus_call(bus, m, 0, &error, &reply); | |
571 | } | |
4c3e8e39 LP |
572 | if (r < 0) |
573 | return log_error_errno(r, "Failed to list units: %s", bus_error_message(&error, r)); | |
7e4249b9 | 574 | |
f459b602 MAP |
575 | r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssssouso)"); |
576 | if (r < 0) | |
577 | return bus_log_parse_error(r); | |
7e4249b9 | 578 | |
f459b602 | 579 | while ((r = bus_parse_unit_info(reply, &u)) > 0) { |
1238ee09 LP |
580 | u.machine = machine; |
581 | ||
313fe66f | 582 | if (!output_show_unit(&u, fallback ? patterns : NULL)) |
8d5ba5a9 | 583 | continue; |
7e4249b9 | 584 | |
1238ee09 | 585 | if (!GREEDY_REALLOC(*unit_infos, size, c+1)) |
f459b602 | 586 | return log_oom(); |
36c32ba2 | 587 | |
1238ee09 | 588 | (*unit_infos)[c++] = u; |
991f2a39 | 589 | } |
f459b602 MAP |
590 | if (r < 0) |
591 | return bus_log_parse_error(r); | |
991f2a39 | 592 | |
f459b602 MAP |
593 | r = sd_bus_message_exit_container(reply); |
594 | if (r < 0) | |
595 | return bus_log_parse_error(r); | |
596 | ||
1cc6c93a | 597 | *_reply = TAKE_PTR(reply); |
f459b602 | 598 | |
1238ee09 LP |
599 | return c; |
600 | } | |
601 | ||
602 | static void message_set_freep(Set **set) { | |
224b0e7a | 603 | set_free_with_destructor(*set, sd_bus_message_unref); |
1238ee09 LP |
604 | } |
605 | ||
606 | static int get_unit_list_recursive( | |
607 | sd_bus *bus, | |
608 | char **patterns, | |
609 | UnitInfo **_unit_infos, | |
610 | Set **_replies, | |
611 | char ***_machines) { | |
612 | ||
613 | _cleanup_free_ UnitInfo *unit_infos = NULL; | |
614 | _cleanup_(message_set_freep) Set *replies; | |
615 | sd_bus_message *reply; | |
616 | int c, r; | |
617 | ||
618 | assert(bus); | |
619 | assert(_replies); | |
620 | assert(_unit_infos); | |
621 | assert(_machines); | |
622 | ||
d5099efc | 623 | replies = set_new(NULL); |
1238ee09 LP |
624 | if (!replies) |
625 | return log_oom(); | |
626 | ||
627 | c = get_unit_list(bus, NULL, patterns, &unit_infos, 0, &reply); | |
628 | if (c < 0) | |
629 | return c; | |
630 | ||
631 | r = set_put(replies, reply); | |
632 | if (r < 0) { | |
633 | sd_bus_message_unref(reply); | |
4c3e8e39 | 634 | return log_oom(); |
1238ee09 LP |
635 | } |
636 | ||
637 | if (arg_recursive) { | |
638 | _cleanup_strv_free_ char **machines = NULL; | |
639 | char **i; | |
640 | ||
641 | r = sd_get_machine_names(&machines); | |
642 | if (r < 0) | |
4c3e8e39 | 643 | return log_error_errno(r, "Failed to get machine names: %m"); |
1238ee09 LP |
644 | |
645 | STRV_FOREACH(i, machines) { | |
4afd3348 | 646 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *container = NULL; |
1238ee09 LP |
647 | int k; |
648 | ||
de33fc62 | 649 | r = sd_bus_open_system_machine(&container, *i); |
1238ee09 | 650 | if (r < 0) { |
4c3e8e39 | 651 | log_warning_errno(r, "Failed to connect to container %s, ignoring: %m", *i); |
1238ee09 LP |
652 | continue; |
653 | } | |
654 | ||
655 | k = get_unit_list(container, *i, patterns, &unit_infos, c, &reply); | |
656 | if (k < 0) | |
657 | return k; | |
658 | ||
659 | c = k; | |
660 | ||
661 | r = set_put(replies, reply); | |
662 | if (r < 0) { | |
663 | sd_bus_message_unref(reply); | |
4c3e8e39 | 664 | return log_oom(); |
1238ee09 LP |
665 | } |
666 | } | |
667 | ||
1cc6c93a | 668 | *_machines = TAKE_PTR(machines); |
1238ee09 LP |
669 | } else |
670 | *_machines = NULL; | |
671 | ||
1cc6c93a | 672 | *_unit_infos = TAKE_PTR(unit_infos); |
1cc6c93a | 673 | *_replies = TAKE_PTR(replies); |
1238ee09 | 674 | |
f459b602 | 675 | return c; |
991f2a39 ZJS |
676 | } |
677 | ||
1d56bc09 | 678 | static int expand_names(sd_bus *bus, char **names, const char* suffix, char ***ret, bool *ret_expanded) { |
3a77f9fb KK |
679 | _cleanup_strv_free_ char **mangled = NULL, **globs = NULL; |
680 | char **name; | |
681 | int r, i; | |
682 | ||
683 | assert(bus); | |
684 | assert(ret); | |
685 | ||
686 | STRV_FOREACH(name, names) { | |
687 | char *t; | |
688 | UnitNameMangle options = UNIT_NAME_MANGLE_GLOB | (arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN); | |
689 | ||
df7c4eb6 | 690 | r = unit_name_mangle_with_suffix(*name, NULL, options, suffix ?: ".service", &t); |
3a77f9fb KK |
691 | if (r < 0) |
692 | return log_error_errno(r, "Failed to mangle name: %m"); | |
693 | ||
694 | if (string_is_glob(t)) | |
695 | r = strv_consume(&globs, t); | |
696 | else | |
697 | r = strv_consume(&mangled, t); | |
698 | if (r < 0) | |
699 | return log_oom(); | |
700 | } | |
701 | ||
702 | /* Query the manager only if any of the names are a glob, since | |
703 | * this is fairly expensive */ | |
1d56bc09 ZJS |
704 | bool expanded = !strv_isempty(globs); |
705 | if (expanded) { | |
3a77f9fb KK |
706 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
707 | _cleanup_free_ UnitInfo *unit_infos = NULL; | |
708 | size_t allocated, n; | |
709 | ||
710 | r = get_unit_list(bus, NULL, globs, &unit_infos, 0, &reply); | |
711 | if (r < 0) | |
712 | return r; | |
713 | ||
714 | n = strv_length(mangled); | |
715 | allocated = n + 1; | |
716 | ||
717 | for (i = 0; i < r; i++) { | |
718 | if (!GREEDY_REALLOC(mangled, allocated, n+2)) | |
719 | return log_oom(); | |
720 | ||
721 | mangled[n] = strdup(unit_infos[i].id); | |
722 | if (!mangled[n]) | |
723 | return log_oom(); | |
724 | ||
725 | mangled[++n] = NULL; | |
726 | } | |
727 | } | |
728 | ||
1d56bc09 ZJS |
729 | if (ret_expanded) |
730 | *ret_expanded = expanded; | |
731 | ||
3a77f9fb KK |
732 | *ret = TAKE_PTR(mangled); |
733 | return 0; | |
734 | } | |
735 | ||
e9c387c8 KK |
736 | static int list_dependencies_get_dependencies(sd_bus *bus, const char *name, char ***ret) { |
737 | _cleanup_strv_free_ char **deps = NULL; | |
738 | ||
739 | static const struct bus_properties_map map[_DEPENDENCY_MAX][6] = { | |
740 | [DEPENDENCY_FORWARD] = { | |
741 | { "Requires", "as", NULL, 0 }, | |
742 | { "Requisite", "as", NULL, 0 }, | |
743 | { "Wants", "as", NULL, 0 }, | |
744 | { "ConsistsOf", "as", NULL, 0 }, | |
745 | { "BindsTo", "as", NULL, 0 }, | |
746 | {} | |
747 | }, | |
748 | [DEPENDENCY_REVERSE] = { | |
749 | { "RequiredBy", "as", NULL, 0 }, | |
750 | { "RequisiteOf", "as", NULL, 0 }, | |
751 | { "WantedBy", "as", NULL, 0 }, | |
752 | { "PartOf", "as", NULL, 0 }, | |
753 | { "BoundBy", "as", NULL, 0 }, | |
754 | {} | |
755 | }, | |
756 | [DEPENDENCY_AFTER] = { | |
757 | { "After", "as", NULL, 0 }, | |
758 | {} | |
759 | }, | |
760 | [DEPENDENCY_BEFORE] = { | |
761 | { "Before", "as", NULL, 0 }, | |
762 | {} | |
763 | }, | |
764 | }; | |
765 | ||
766 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
767 | _cleanup_free_ char *dbus_path = NULL; | |
768 | int r; | |
769 | ||
770 | assert(bus); | |
771 | assert(name); | |
772 | assert(ret); | |
773 | ||
774 | dbus_path = unit_dbus_path_from_name(name); | |
775 | if (!dbus_path) | |
776 | return log_oom(); | |
777 | ||
778 | r = bus_map_all_properties(bus, | |
779 | "org.freedesktop.systemd1", | |
780 | dbus_path, | |
781 | map[arg_dependency], | |
782 | BUS_MAP_STRDUP, | |
783 | &error, | |
784 | NULL, | |
785 | &deps); | |
786 | if (r < 0) | |
787 | return log_error_errno(r, "Failed to get properties of %s: %s", name, bus_error_message(&error, r)); | |
788 | ||
dbf2801f ZJS |
789 | strv_uniq(deps); /* Sometimes a unit might have multiple deps on the other unit, |
790 | * but we still want to show it just once. */ | |
e9c387c8 KK |
791 | *ret = TAKE_PTR(deps); |
792 | ||
793 | return 0; | |
794 | } | |
795 | ||
796 | static int append_unit_dependencies(sd_bus *bus, char **names, char ***ret) { | |
797 | _cleanup_strv_free_ char **with_deps = NULL; | |
798 | char **name; | |
799 | ||
800 | assert(bus); | |
801 | assert(ret); | |
802 | ||
803 | STRV_FOREACH(name, names) { | |
804 | _cleanup_strv_free_ char **deps = NULL; | |
805 | ||
806 | if (strv_extend(&with_deps, *name) < 0) | |
807 | return log_oom(); | |
808 | ||
809 | (void) list_dependencies_get_dependencies(bus, *name, &deps); | |
810 | ||
811 | if (strv_extend_strv(&with_deps, deps, true) < 0) | |
812 | return log_oom(); | |
813 | } | |
814 | ||
815 | *ret = TAKE_PTR(with_deps); | |
816 | ||
817 | return 0; | |
818 | } | |
819 | ||
820 | static int maybe_extend_with_unit_dependencies(sd_bus *bus, char ***list) { | |
821 | assert(bus); | |
822 | assert(list); | |
823 | ||
824 | if (arg_with_dependencies) { | |
825 | int r; | |
826 | _cleanup_strv_free_ char **list_with_deps = NULL; | |
827 | ||
828 | r = append_unit_dependencies(bus, *list, &list_with_deps); | |
829 | if (r < 0) | |
830 | return log_error_errno(r, "Failed to append unit dependencies: %m"); | |
831 | ||
832 | strv_free(*list); | |
833 | *list = TAKE_PTR(list_with_deps); | |
834 | } | |
835 | ||
836 | return 0; | |
837 | } | |
838 | ||
e449de87 | 839 | static int list_units(int argc, char *argv[], void *userdata) { |
f459b602 | 840 | _cleanup_free_ UnitInfo *unit_infos = NULL; |
1238ee09 LP |
841 | _cleanup_(message_set_freep) Set *replies = NULL; |
842 | _cleanup_strv_free_ char **machines = NULL; | |
4fbd7192 | 843 | sd_bus *bus; |
991f2a39 ZJS |
844 | int r; |
845 | ||
4fbd7192 LP |
846 | r = acquire_bus(BUS_MANAGER, &bus); |
847 | if (r < 0) | |
848 | return r; | |
849 | ||
0221d68a | 850 | (void) pager_open(arg_pager_flags); |
d2ad7e1b | 851 | |
e9c387c8 KK |
852 | if (arg_with_dependencies) { |
853 | _cleanup_strv_free_ char **names = NULL; | |
854 | ||
855 | r = append_unit_dependencies(bus, strv_skip(argv, 1), &names); | |
856 | if (r < 0) | |
857 | return r; | |
858 | ||
859 | r = get_unit_list_recursive(bus, names, &unit_infos, &replies, &machines); | |
860 | if (r < 0) | |
861 | return r; | |
862 | } else { | |
863 | r = get_unit_list_recursive(bus, strv_skip(argv, 1), &unit_infos, &replies, &machines); | |
864 | if (r < 0) | |
865 | return r; | |
866 | } | |
991f2a39 | 867 | |
93bab288 | 868 | typesafe_qsort(unit_infos, r, compare_unit_info); |
1238ee09 | 869 | return output_units_list(unit_infos, r); |
991f2a39 ZJS |
870 | } |
871 | ||
a00963a2 | 872 | static int get_triggered_units( |
f459b602 MAP |
873 | sd_bus *bus, |
874 | const char* path, | |
875 | char*** ret) { | |
a00963a2 | 876 | |
4afd3348 | 877 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
991f2a39 ZJS |
878 | int r; |
879 | ||
4fbd7192 LP |
880 | assert(bus); |
881 | assert(path); | |
882 | assert(ret); | |
883 | ||
f459b602 MAP |
884 | r = sd_bus_get_property_strv( |
885 | bus, | |
886 | "org.freedesktop.systemd1", | |
887 | path, | |
888 | "org.freedesktop.systemd1.Unit", | |
889 | "Triggers", | |
890 | &error, | |
891 | ret); | |
f459b602 | 892 | if (r < 0) |
691395d8 | 893 | return log_error_errno(r, "Failed to determine triggers: %s", bus_error_message(&error, r)); |
991f2a39 ZJS |
894 | |
895 | return 0; | |
896 | } | |
897 | ||
f459b602 MAP |
898 | static int get_listening( |
899 | sd_bus *bus, | |
900 | const char* unit_path, | |
cbb76c29 | 901 | char*** listening) { |
f459b602 | 902 | |
4afd3348 LP |
903 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
904 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
f459b602 | 905 | const char *type, *path; |
cbb76c29 | 906 | int r, n = 0; |
991f2a39 | 907 | |
f459b602 MAP |
908 | r = sd_bus_get_property( |
909 | bus, | |
910 | "org.freedesktop.systemd1", | |
911 | unit_path, | |
912 | "org.freedesktop.systemd1.Socket", | |
913 | "Listen", | |
914 | &error, | |
915 | &reply, | |
916 | "a(ss)"); | |
691395d8 LP |
917 | if (r < 0) |
918 | return log_error_errno(r, "Failed to get list of listening sockets: %s", bus_error_message(&error, r)); | |
991f2a39 | 919 | |
f459b602 MAP |
920 | r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ss)"); |
921 | if (r < 0) | |
922 | return bus_log_parse_error(r); | |
991f2a39 | 923 | |
f459b602 | 924 | while ((r = sd_bus_message_read(reply, "(ss)", &type, &path)) > 0) { |
36c32ba2 | 925 | |
0d95178e | 926 | r = strv_extend(listening, type); |
f459b602 MAP |
927 | if (r < 0) |
928 | return log_oom(); | |
991f2a39 | 929 | |
0d95178e | 930 | r = strv_extend(listening, path); |
f459b602 MAP |
931 | if (r < 0) |
932 | return log_oom(); | |
7e4249b9 | 933 | |
cbb76c29 | 934 | n++; |
36c32ba2 | 935 | } |
f459b602 MAP |
936 | if (r < 0) |
937 | return bus_log_parse_error(r); | |
938 | ||
939 | r = sd_bus_message_exit_container(reply); | |
940 | if (r < 0) | |
941 | return bus_log_parse_error(r); | |
36c32ba2 | 942 | |
cbb76c29 | 943 | return n; |
991f2a39 ZJS |
944 | } |
945 | ||
946 | struct socket_info { | |
0cfc3525 | 947 | const char *machine; |
991f2a39 ZJS |
948 | const char* id; |
949 | ||
950 | char* type; | |
951 | char* path; | |
952 | ||
953 | /* Note: triggered is a list here, although it almost certainly | |
954 | * will always be one unit. Nevertheless, dbus API allows for multiple | |
f7340ab2 | 955 | * values, so let's follow that. */ |
991f2a39 ZJS |
956 | char** triggered; |
957 | ||
958 | /* The strv above is shared. free is set only in the first one. */ | |
959 | bool own_triggered; | |
960 | }; | |
961 | ||
cbb76c29 | 962 | static int socket_info_compare(const struct socket_info *a, const struct socket_info *b) { |
93bab288 | 963 | int r; |
cbc9fbd1 | 964 | |
cbb76c29 LP |
965 | assert(a); |
966 | assert(b); | |
967 | ||
6cf3011c ZJS |
968 | r = strcasecmp_ptr(a->machine, b->machine); |
969 | if (r != 0) | |
970 | return r; | |
0cfc3525 | 971 | |
93bab288 | 972 | r = strcmp(a->path, b->path); |
6cf3011c ZJS |
973 | if (r != 0) |
974 | return r; | |
cbc9fbd1 | 975 | |
6cf3011c | 976 | return strcmp(a->type, b->type); |
991f2a39 ZJS |
977 | } |
978 | ||
979 | static int output_sockets_list(struct socket_info *socket_infos, unsigned cs) { | |
de9a8fe1 | 980 | _cleanup_(table_unrefp) Table *table = NULL; |
991f2a39 | 981 | struct socket_info *s; |
991f2a39 | 982 | const char *on, *off; |
de9a8fe1 | 983 | int r; |
991f2a39 | 984 | |
bc04bb0d | 985 | table = table_new("listen", "type", "unit", "activates"); |
de9a8fe1 FS |
986 | if (!table) |
987 | return log_oom(); | |
991f2a39 | 988 | |
de9a8fe1 FS |
989 | if (!arg_show_types) { |
990 | /* Hide the second (TYPE) column */ | |
ad5555b4 | 991 | r = table_set_display(table, (size_t) 0, (size_t) 2, (size_t) 3, (size_t) -1); |
de9a8fe1 FS |
992 | if (r < 0) |
993 | return log_error_errno(r, "Failed to set columns to display: %m"); | |
991f2a39 ZJS |
994 | } |
995 | ||
de9a8fe1 FS |
996 | table_set_header(table, !arg_no_legend); |
997 | if (arg_full) | |
998 | table_set_width(table, 0); | |
991f2a39 | 999 | |
0773357a LP |
1000 | (void) table_set_empty_string(table, "-"); |
1001 | ||
de9a8fe1 | 1002 | if (cs) { |
991f2a39 | 1003 | for (s = socket_infos; s < socket_infos + cs; s++) { |
50098d87 | 1004 | _cleanup_free_ char *j = NULL; |
0cfc3525 | 1005 | const char *path; |
991f2a39 | 1006 | |
0cfc3525 | 1007 | if (s->machine) { |
605405c6 | 1008 | j = strjoin(s->machine, ":", s->path); |
0cfc3525 TA |
1009 | if (!j) |
1010 | return log_oom(); | |
1011 | path = j; | |
1012 | } else | |
1013 | path = s->path; | |
1014 | ||
de9a8fe1 FS |
1015 | r = table_add_many(table, |
1016 | TABLE_STRING, path, | |
1017 | TABLE_STRING, s->type, | |
50098d87 LP |
1018 | TABLE_STRING, s->id); |
1019 | if (r < 0) | |
1020 | return table_log_add_error(r); | |
1021 | ||
1022 | if (strv_isempty(s->triggered)) | |
1023 | r = table_add_cell(table, NULL, TABLE_EMPTY, NULL); | |
1024 | else if (strv_length(s->triggered) == 1) | |
1025 | r = table_add_cell(table, NULL, TABLE_STRING, s->triggered[0]); | |
1026 | else | |
1027 | /* This should never happen, currently our socket units can only trigger a | |
1028 | * single unit. But let's handle this anyway, who knows what the future | |
1029 | * brings? */ | |
1030 | r = table_add_cell(table, NULL, TABLE_STRV, s->triggered); | |
de9a8fe1 FS |
1031 | if (r < 0) |
1032 | return table_log_add_error(r); | |
50098d87 | 1033 | |
991f2a39 ZJS |
1034 | } |
1035 | ||
0b5a519c | 1036 | on = ansi_highlight(); |
1fc464f6 | 1037 | off = ansi_normal(); |
991f2a39 | 1038 | } else { |
0b5a519c | 1039 | on = ansi_highlight_red(); |
1fc464f6 | 1040 | off = ansi_normal(); |
991f2a39 ZJS |
1041 | } |
1042 | ||
49cd06fa | 1043 | r = output_table(table); |
de9a8fe1 | 1044 | if (r < 0) |
49cd06fa | 1045 | return r; |
de9a8fe1 | 1046 | |
571bfc6c | 1047 | if (!arg_no_legend) { |
de9a8fe1 | 1048 | printf("\n%s%u sockets listed.%s\n", on, cs, off); |
571bfc6c MM |
1049 | if (!arg_all) |
1050 | printf("Pass --all to see loaded but inactive sockets, too.\n"); | |
1051 | } | |
265a7a2a ZJS |
1052 | |
1053 | return 0; | |
1054 | } | |
1055 | ||
e449de87 | 1056 | static int list_sockets(int argc, char *argv[], void *userdata) { |
0cfc3525 TA |
1057 | _cleanup_(message_set_freep) Set *replies = NULL; |
1058 | _cleanup_strv_free_ char **machines = NULL; | |
3a77f9fb | 1059 | _cleanup_strv_free_ char **sockets_with_suffix = NULL; |
f459b602 | 1060 | _cleanup_free_ UnitInfo *unit_infos = NULL; |
8d5ba5a9 | 1061 | _cleanup_free_ struct socket_info *socket_infos = NULL; |
f459b602 | 1062 | const UnitInfo *u; |
991f2a39 | 1063 | struct socket_info *s; |
cbb76c29 | 1064 | unsigned cs = 0; |
991f2a39 | 1065 | size_t size = 0; |
ad83b4c4 | 1066 | int r = 0, n; |
4fbd7192 | 1067 | sd_bus *bus; |
265a7a2a | 1068 | |
4fbd7192 LP |
1069 | r = acquire_bus(BUS_MANAGER, &bus); |
1070 | if (r < 0) | |
1071 | return r; | |
1072 | ||
0221d68a | 1073 | (void) pager_open(arg_pager_flags); |
d2ad7e1b | 1074 | |
1d56bc09 | 1075 | r = expand_names(bus, strv_skip(argv, 1), ".socket", &sockets_with_suffix, NULL); |
3a77f9fb KK |
1076 | if (r < 0) |
1077 | return r; | |
265a7a2a | 1078 | |
3a77f9fb KK |
1079 | if (argc == 1 || sockets_with_suffix) { |
1080 | n = get_unit_list_recursive(bus, sockets_with_suffix, &unit_infos, &replies, &machines); | |
1081 | if (n < 0) | |
1082 | return n; | |
991f2a39 | 1083 | |
3a77f9fb KK |
1084 | for (u = unit_infos; u < unit_infos + n; u++) { |
1085 | _cleanup_strv_free_ char **listening = NULL, **triggered = NULL; | |
1086 | int i, c; | |
991f2a39 | 1087 | |
3a77f9fb KK |
1088 | if (!endswith(u->id, ".socket")) |
1089 | continue; | |
991f2a39 | 1090 | |
3a77f9fb KK |
1091 | r = get_triggered_units(bus, u->unit_path, &triggered); |
1092 | if (r < 0) | |
1093 | goto cleanup; | |
991f2a39 | 1094 | |
3a77f9fb KK |
1095 | c = get_listening(bus, u->unit_path, &listening); |
1096 | if (c < 0) { | |
1097 | r = c; | |
1098 | goto cleanup; | |
1099 | } | |
991f2a39 | 1100 | |
3a77f9fb KK |
1101 | if (!GREEDY_REALLOC(socket_infos, size, cs + c)) { |
1102 | r = log_oom(); | |
1103 | goto cleanup; | |
1104 | } | |
991f2a39 | 1105 | |
3a77f9fb KK |
1106 | for (i = 0; i < c; i++) |
1107 | socket_infos[cs + i] = (struct socket_info) { | |
1108 | .machine = u->machine, | |
1109 | .id = u->id, | |
1110 | .type = listening[i*2], | |
1111 | .path = listening[i*2 + 1], | |
1112 | .triggered = triggered, | |
1113 | .own_triggered = i==0, | |
1114 | }; | |
1115 | ||
1116 | /* from this point on we will cleanup those socket_infos */ | |
1117 | cs += c; | |
1118 | free(listening); | |
1119 | listening = triggered = NULL; /* avoid cleanup */ | |
1120 | } | |
991f2a39 | 1121 | |
3a77f9fb KK |
1122 | typesafe_qsort(socket_infos, cs, socket_info_compare); |
1123 | } | |
991f2a39 ZJS |
1124 | |
1125 | output_sockets_list(socket_infos, cs); | |
1126 | ||
1127 | cleanup: | |
1128 | assert(cs == 0 || socket_infos); | |
1129 | for (s = socket_infos; s < socket_infos + cs; s++) { | |
1130 | free(s->type); | |
1131 | free(s->path); | |
1132 | if (s->own_triggered) | |
1133 | strv_free(s->triggered); | |
1134 | } | |
4445a875 | 1135 | |
872c8faa | 1136 | return r; |
4445a875 LP |
1137 | } |
1138 | ||
cbb76c29 LP |
1139 | static int get_next_elapse( |
1140 | sd_bus *bus, | |
1141 | const char *path, | |
1142 | dual_timestamp *next) { | |
1143 | ||
4afd3348 | 1144 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
cbb76c29 LP |
1145 | dual_timestamp t; |
1146 | int r; | |
1147 | ||
1148 | assert(bus); | |
1149 | assert(path); | |
1150 | assert(next); | |
1151 | ||
1152 | r = sd_bus_get_property_trivial( | |
1153 | bus, | |
1154 | "org.freedesktop.systemd1", | |
1155 | path, | |
1156 | "org.freedesktop.systemd1.Timer", | |
1157 | "NextElapseUSecMonotonic", | |
1158 | &error, | |
1159 | 't', | |
1160 | &t.monotonic); | |
691395d8 | 1161 | if (r < 0) |
021723c6 | 1162 | return log_error_errno(r, "Failed to get next elapse time: %s", bus_error_message(&error, r)); |
cbb76c29 LP |
1163 | |
1164 | r = sd_bus_get_property_trivial( | |
1165 | bus, | |
1166 | "org.freedesktop.systemd1", | |
1167 | path, | |
1168 | "org.freedesktop.systemd1.Timer", | |
1169 | "NextElapseUSecRealtime", | |
1170 | &error, | |
1171 | 't', | |
1172 | &t.realtime); | |
691395d8 | 1173 | if (r < 0) |
021723c6 | 1174 | return log_error_errno(r, "Failed to get next elapse time: %s", bus_error_message(&error, r)); |
cbb76c29 LP |
1175 | |
1176 | *next = t; | |
1177 | return 0; | |
1178 | } | |
1179 | ||
d784e2db LP |
1180 | static int get_last_trigger( |
1181 | sd_bus *bus, | |
1182 | const char *path, | |
1183 | usec_t *last) { | |
1184 | ||
4afd3348 | 1185 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
d784e2db LP |
1186 | int r; |
1187 | ||
1188 | assert(bus); | |
1189 | assert(path); | |
1190 | assert(last); | |
1191 | ||
1192 | r = sd_bus_get_property_trivial( | |
1193 | bus, | |
1194 | "org.freedesktop.systemd1", | |
1195 | path, | |
1196 | "org.freedesktop.systemd1.Timer", | |
dedabea4 | 1197 | "LastTriggerUSec", |
d784e2db LP |
1198 | &error, |
1199 | 't', | |
1200 | last); | |
691395d8 LP |
1201 | if (r < 0) |
1202 | return log_error_errno(r, "Failed to get last trigger time: %s", bus_error_message(&error, r)); | |
d784e2db LP |
1203 | |
1204 | return 0; | |
1205 | } | |
1206 | ||
cbb76c29 | 1207 | struct timer_info { |
806a37e7 | 1208 | const char* machine; |
cbb76c29 LP |
1209 | const char* id; |
1210 | usec_t next_elapse; | |
d784e2db | 1211 | usec_t last_trigger; |
cbb76c29 LP |
1212 | char** triggered; |
1213 | }; | |
1214 | ||
1215 | static int timer_info_compare(const struct timer_info *a, const struct timer_info *b) { | |
93bab288 | 1216 | int r; |
806a37e7 | 1217 | |
cbb76c29 LP |
1218 | assert(a); |
1219 | assert(b); | |
1220 | ||
6cf3011c ZJS |
1221 | r = strcasecmp_ptr(a->machine, b->machine); |
1222 | if (r != 0) | |
1223 | return r; | |
806a37e7 | 1224 | |
93bab288 YW |
1225 | r = CMP(a->next_elapse, b->next_elapse); |
1226 | if (r != 0) | |
1227 | return r; | |
cbb76c29 LP |
1228 | |
1229 | return strcmp(a->id, b->id); | |
1230 | } | |
1231 | ||
1232 | static int output_timers_list(struct timer_info *timer_infos, unsigned n) { | |
de9a8fe1 | 1233 | _cleanup_(table_unrefp) Table *table = NULL; |
cbb76c29 | 1234 | struct timer_info *t; |
cbb76c29 | 1235 | const char *on, *off; |
de9a8fe1 | 1236 | int r; |
cbb76c29 LP |
1237 | |
1238 | assert(timer_infos || n == 0); | |
1239 | ||
de9a8fe1 FS |
1240 | table = table_new("next", "left", "last", "passed", "unit", "activates"); |
1241 | if (!table) | |
1242 | return log_oom(); | |
d784e2db | 1243 | |
de9a8fe1 FS |
1244 | table_set_header(table, !arg_no_legend); |
1245 | if (arg_full) | |
1246 | table_set_width(table, 0); | |
cbb76c29 | 1247 | |
0773357a LP |
1248 | (void) table_set_empty_string(table, "-"); |
1249 | ||
cbb76c29 | 1250 | if (n > 0) { |
cbb76c29 | 1251 | for (t = timer_infos; t < timer_infos + n; t++) { |
de9a8fe1 | 1252 | _cleanup_free_ char *j = NULL, *activates = NULL; |
806a37e7 | 1253 | const char *unit; |
d784e2db | 1254 | |
806a37e7 | 1255 | if (t->machine) { |
605405c6 | 1256 | j = strjoin(t->machine, ":", t->id); |
806a37e7 TA |
1257 | if (!j) |
1258 | return log_oom(); | |
1259 | unit = j; | |
1260 | } else | |
1261 | unit = t->id; | |
1262 | ||
de9a8fe1 FS |
1263 | activates = strv_join(t->triggered, ", "); |
1264 | if (!activates) | |
1265 | return log_oom(); | |
cbb76c29 | 1266 | |
de9a8fe1 FS |
1267 | r = table_add_many(table, |
1268 | TABLE_TIMESTAMP, t->next_elapse, | |
1269 | TABLE_TIMESTAMP_RELATIVE, t->next_elapse, | |
1270 | TABLE_TIMESTAMP, t->last_trigger, | |
1271 | TABLE_TIMESTAMP_RELATIVE, t->last_trigger, | |
1272 | TABLE_STRING, unit, | |
1273 | TABLE_STRING, activates); | |
1274 | if (r < 0) | |
1275 | return table_log_add_error(r); | |
cbb76c29 LP |
1276 | } |
1277 | ||
1278 | on = ansi_highlight(); | |
1fc464f6 | 1279 | off = ansi_normal(); |
cbb76c29 LP |
1280 | } else { |
1281 | on = ansi_highlight_red(); | |
1fc464f6 | 1282 | off = ansi_normal(); |
cbb76c29 LP |
1283 | } |
1284 | ||
49cd06fa | 1285 | r = output_table(table); |
de9a8fe1 | 1286 | if (r < 0) |
49cd06fa | 1287 | return r; |
de9a8fe1 | 1288 | |
cbb76c29 | 1289 | if (!arg_no_legend) { |
de9a8fe1 | 1290 | printf("\n%s%u timers listed.%s\n", on, n, off); |
cbb76c29 LP |
1291 | if (!arg_all) |
1292 | printf("Pass --all to see loaded but inactive timers, too.\n"); | |
1293 | } | |
1294 | ||
1295 | return 0; | |
1296 | } | |
1297 | ||
f5080e73 DH |
1298 | static usec_t calc_next_elapse(dual_timestamp *nw, dual_timestamp *next) { |
1299 | usec_t next_elapse; | |
1300 | ||
1301 | assert(nw); | |
1302 | assert(next); | |
1303 | ||
1f65fd49 | 1304 | if (timestamp_is_set(next->monotonic)) { |
f5080e73 DH |
1305 | usec_t converted; |
1306 | ||
1307 | if (next->monotonic > nw->monotonic) | |
1308 | converted = nw->realtime + (next->monotonic - nw->monotonic); | |
1309 | else | |
1310 | converted = nw->realtime - (nw->monotonic - next->monotonic); | |
1311 | ||
1f65fd49 | 1312 | if (timestamp_is_set(next->realtime)) |
f5080e73 DH |
1313 | next_elapse = MIN(converted, next->realtime); |
1314 | else | |
1315 | next_elapse = converted; | |
1316 | ||
1317 | } else | |
1318 | next_elapse = next->realtime; | |
1319 | ||
1320 | return next_elapse; | |
1321 | } | |
1322 | ||
e449de87 | 1323 | static int list_timers(int argc, char *argv[], void *userdata) { |
806a37e7 TA |
1324 | _cleanup_(message_set_freep) Set *replies = NULL; |
1325 | _cleanup_strv_free_ char **machines = NULL; | |
3a77f9fb | 1326 | _cleanup_strv_free_ char **timers_with_suffix = NULL; |
cbb76c29 LP |
1327 | _cleanup_free_ struct timer_info *timer_infos = NULL; |
1328 | _cleanup_free_ UnitInfo *unit_infos = NULL; | |
1329 | struct timer_info *t; | |
1330 | const UnitInfo *u; | |
1331 | size_t size = 0; | |
1823b86e | 1332 | int n, c = 0; |
cbb76c29 | 1333 | dual_timestamp nw; |
4fbd7192 | 1334 | sd_bus *bus; |
1823b86e | 1335 | int r = 0; |
cbb76c29 | 1336 | |
4fbd7192 LP |
1337 | r = acquire_bus(BUS_MANAGER, &bus); |
1338 | if (r < 0) | |
1339 | return r; | |
1340 | ||
0221d68a | 1341 | (void) pager_open(arg_pager_flags); |
d2ad7e1b | 1342 | |
1d56bc09 | 1343 | r = expand_names(bus, strv_skip(argv, 1), ".timer", &timers_with_suffix, NULL); |
3a77f9fb KK |
1344 | if (r < 0) |
1345 | return r; | |
cbb76c29 | 1346 | |
3a77f9fb KK |
1347 | if (argc == 1 || timers_with_suffix) { |
1348 | n = get_unit_list_recursive(bus, timers_with_suffix, &unit_infos, &replies, &machines); | |
1349 | if (n < 0) | |
1350 | return n; | |
cbb76c29 | 1351 | |
3a77f9fb | 1352 | dual_timestamp_get(&nw); |
cbb76c29 | 1353 | |
3a77f9fb KK |
1354 | for (u = unit_infos; u < unit_infos + n; u++) { |
1355 | _cleanup_strv_free_ char **triggered = NULL; | |
1356 | dual_timestamp next = DUAL_TIMESTAMP_NULL; | |
1357 | usec_t m, last = 0; | |
cbb76c29 | 1358 | |
3a77f9fb KK |
1359 | if (!endswith(u->id, ".timer")) |
1360 | continue; | |
cbb76c29 | 1361 | |
3a77f9fb KK |
1362 | r = get_triggered_units(bus, u->unit_path, &triggered); |
1363 | if (r < 0) | |
1364 | goto cleanup; | |
cbb76c29 | 1365 | |
3a77f9fb KK |
1366 | r = get_next_elapse(bus, u->unit_path, &next); |
1367 | if (r < 0) | |
1368 | goto cleanup; | |
d784e2db | 1369 | |
3a77f9fb | 1370 | get_last_trigger(bus, u->unit_path, &last); |
cbb76c29 | 1371 | |
3a77f9fb KK |
1372 | if (!GREEDY_REALLOC(timer_infos, size, c+1)) { |
1373 | r = log_oom(); | |
1374 | goto cleanup; | |
1375 | } | |
f5080e73 | 1376 | |
3a77f9fb | 1377 | m = calc_next_elapse(&nw, &next); |
cbb76c29 | 1378 | |
3a77f9fb KK |
1379 | timer_infos[c++] = (struct timer_info) { |
1380 | .machine = u->machine, | |
1381 | .id = u->id, | |
1382 | .next_elapse = m, | |
1383 | .last_trigger = last, | |
1384 | .triggered = TAKE_PTR(triggered), | |
1385 | }; | |
1386 | } | |
1387 | ||
1388 | typesafe_qsort(timer_infos, c, timer_info_compare); | |
1389 | } | |
cbb76c29 LP |
1390 | |
1391 | output_timers_list(timer_infos, c); | |
1392 | ||
1393 | cleanup: | |
1394 | for (t = timer_infos; t < timer_infos + c; t++) | |
1395 | strv_free(t->triggered); | |
1396 | ||
1397 | return r; | |
1398 | } | |
1399 | ||
93bab288 | 1400 | static int compare_unit_file_list(const UnitFileList *a, const UnitFileList *b) { |
729e3769 | 1401 | const char *d1, *d2; |
729e3769 | 1402 | |
93bab288 YW |
1403 | d1 = strrchr(a->path, '.'); |
1404 | d2 = strrchr(b->path, '.'); | |
729e3769 LP |
1405 | |
1406 | if (d1 && d2) { | |
1407 | int r; | |
1408 | ||
1409 | r = strcasecmp(d1, d2); | |
1410 | if (r != 0) | |
1411 | return r; | |
1412 | } | |
1413 | ||
93bab288 | 1414 | return strcasecmp(basename(a->path), basename(b->path)); |
729e3769 LP |
1415 | } |
1416 | ||
313fe66f | 1417 | static bool output_show_unit_file(const UnitFileList *u, char **states, char **patterns) { |
f8591ee1 LP |
1418 | assert(u); |
1419 | ||
2404701e | 1420 | if (!strv_fnmatch_or_empty(patterns, basename(u->path), FNM_NOESCAPE)) |
d8fba7c6 | 1421 | return false; |
d8fba7c6 | 1422 | |
6c71341a ZJS |
1423 | if (!strv_isempty(arg_types)) { |
1424 | const char *dot; | |
1425 | ||
1426 | dot = strrchr(u->path, '.'); | |
1427 | if (!dot) | |
1428 | return false; | |
1429 | ||
1430 | if (!strv_find(arg_types, dot+1)) | |
1431 | return false; | |
1432 | } | |
1433 | ||
313fe66f | 1434 | if (!strv_isempty(states) && |
1435 | !strv_find(states, unit_file_state_to_string(u->state))) | |
bceccd5e | 1436 | return false; |
fec1530e | 1437 | |
6c71341a | 1438 | return true; |
729e3769 LP |
1439 | } |
1440 | ||
31b8895a ZJS |
1441 | static bool show_preset_for_state(UnitFileState state) { |
1442 | /* Don't show preset state in those unit file states, it'll only confuse users. */ | |
1443 | return !IN_SET(state, | |
1444 | UNIT_FILE_ALIAS, | |
1445 | UNIT_FILE_STATIC, | |
1446 | UNIT_FILE_GENERATED, | |
1447 | UNIT_FILE_TRANSIENT); | |
1448 | } | |
1449 | ||
de9a8fe1 FS |
1450 | static int output_unit_file_list(const UnitFileList *units, unsigned c) { |
1451 | _cleanup_(table_unrefp) Table *table = NULL; | |
8f7b2566 | 1452 | _cleanup_(unit_file_presets_freep) UnitFilePresets presets = {}; |
de9a8fe1 | 1453 | int r; |
729e3769 | 1454 | |
de9a8fe1 FS |
1455 | table = table_new("unit file", "state", "vendor preset"); |
1456 | if (!table) | |
1457 | return log_oom(); | |
1c0a113f | 1458 | |
de9a8fe1 FS |
1459 | table_set_header(table, !arg_no_legend); |
1460 | if (arg_full) | |
1461 | table_set_width(table, 0); | |
729e3769 | 1462 | |
0773357a LP |
1463 | (void) table_set_empty_string(table, "-"); |
1464 | ||
8f7b2566 | 1465 | for (const UnitFileList *u = units; u < units + c; u++) { |
b01c1f30 | 1466 | const char *on_underline = NULL, *on_unit_color = NULL, *id; |
aaa6732d LP |
1467 | bool underline; |
1468 | ||
1469 | underline = u + 1 < units + c && | |
1470 | !streq(unit_type_suffix(u->path), unit_type_suffix((u + 1)->path)); | |
6ae3e62a | 1471 | |
aaa6732d | 1472 | if (underline) |
6ae3e62a | 1473 | on_underline = ansi_underline(); |
729e3769 | 1474 | |
4c315c2c IS |
1475 | if (IN_SET(u->state, |
1476 | UNIT_FILE_MASKED, | |
1477 | UNIT_FILE_MASKED_RUNTIME, | |
1478 | UNIT_FILE_DISABLED, | |
6ae3e62a | 1479 | UNIT_FILE_BAD)) |
b01c1f30 | 1480 | on_unit_color = underline ? ansi_highlight_red_underline() : ansi_highlight_red(); |
15d7ab87 ZJS |
1481 | else if (IN_SET(u->state, |
1482 | UNIT_FILE_ENABLED, | |
1483 | UNIT_FILE_ALIAS)) | |
b01c1f30 | 1484 | on_unit_color = underline ? ansi_highlight_green_underline() : ansi_highlight_green(); |
de9a8fe1 FS |
1485 | else |
1486 | on_unit_color = on_underline; | |
729e3769 | 1487 | |
2b6bf07d | 1488 | id = basename(u->path); |
729e3769 | 1489 | |
de9a8fe1 FS |
1490 | r = table_add_many(table, |
1491 | TABLE_STRING, id, | |
1fab5797 | 1492 | TABLE_SET_BOTH_COLORS, strempty(on_underline), |
de9a8fe1 | 1493 | TABLE_STRING, unit_file_state_to_string(u->state), |
1fab5797 | 1494 | TABLE_SET_BOTH_COLORS, strempty(on_unit_color)); |
31b8895a ZJS |
1495 | if (r < 0) |
1496 | return table_log_add_error(r); | |
1497 | ||
1498 | if (show_preset_for_state(u->state)) { | |
1499 | const char *unit_preset_str, *on_preset_color; | |
1500 | ||
1501 | r = unit_file_query_preset(arg_scope, arg_root, id, &presets); | |
1502 | if (r < 0) { | |
1503 | unit_preset_str = "n/a"; | |
1504 | on_preset_color = underline ? on_underline : ansi_normal(); | |
1505 | } else if (r == 0) { | |
1506 | unit_preset_str = "disabled"; | |
1507 | on_preset_color = underline ? ansi_highlight_red_underline() : ansi_highlight_red(); | |
1508 | } else { | |
1509 | unit_preset_str = "enabled"; | |
1510 | on_preset_color = underline ? ansi_highlight_green_underline() : ansi_highlight_green(); | |
1511 | } | |
1512 | ||
1513 | r = table_add_many(table, | |
1514 | TABLE_STRING, unit_preset_str, | |
1fab5797 | 1515 | TABLE_SET_BOTH_COLORS, strempty(on_preset_color)); |
31b8895a | 1516 | } else |
45d82c3f LP |
1517 | r = table_add_many(table, |
1518 | TABLE_EMPTY, | |
1fab5797 | 1519 | TABLE_SET_BOTH_COLORS, underline ? ansi_grey_underline() : ansi_grey()); |
de9a8fe1 FS |
1520 | if (r < 0) |
1521 | return table_log_add_error(r); | |
729e3769 LP |
1522 | } |
1523 | ||
49cd06fa | 1524 | r = output_table(table); |
de9a8fe1 | 1525 | if (r < 0) |
49cd06fa | 1526 | return r; |
de9a8fe1 | 1527 | |
1c0a113f | 1528 | if (!arg_no_legend) |
0d292f5e | 1529 | printf("\n%u unit files listed.\n", c); |
de9a8fe1 FS |
1530 | |
1531 | return 0; | |
729e3769 LP |
1532 | } |
1533 | ||
e449de87 | 1534 | static int list_unit_files(int argc, char *argv[], void *userdata) { |
4afd3348 | 1535 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
f84190d8 | 1536 | _cleanup_free_ UnitFileList *units = NULL; |
8d5ba5a9 ZJS |
1537 | UnitFileList *unit; |
1538 | size_t size = 0; | |
f459b602 MAP |
1539 | unsigned c = 0; |
1540 | const char *state; | |
1541 | char *path; | |
f84190d8 | 1542 | int r; |
313fe66f | 1543 | bool fallback = false; |
729e3769 | 1544 | |
4fbd7192 | 1545 | if (install_client_side()) { |
729e3769 LP |
1546 | Hashmap *h; |
1547 | UnitFileList *u; | |
1548 | Iterator i; | |
f459b602 | 1549 | unsigned n_units; |
729e3769 | 1550 | |
d5099efc | 1551 | h = hashmap_new(&string_hash_ops); |
0d0f0c50 SL |
1552 | if (!h) |
1553 | return log_oom(); | |
729e3769 | 1554 | |
313fe66f | 1555 | r = unit_file_get_list(arg_scope, arg_root, h, arg_states, strv_skip(argv, 1)); |
729e3769 | 1556 | if (r < 0) { |
8ea913b2 | 1557 | unit_file_list_free(h); |
691395d8 | 1558 | return log_error_errno(r, "Failed to get unit file list: %m"); |
729e3769 LP |
1559 | } |
1560 | ||
1561 | n_units = hashmap_size(h); | |
fdbdf6ec | 1562 | |
0da999fa ZJS |
1563 | units = new(UnitFileList, n_units ?: 1); /* avoid malloc(0) */ |
1564 | if (!units) { | |
729e3769 | 1565 | unit_file_list_free(h); |
0d0f0c50 | 1566 | return log_oom(); |
729e3769 LP |
1567 | } |
1568 | ||
1569 | HASHMAP_FOREACH(u, h, i) { | |
313fe66f | 1570 | if (!output_show_unit_file(u, NULL, NULL)) |
8d5ba5a9 ZJS |
1571 | continue; |
1572 | ||
1573 | units[c++] = *u; | |
729e3769 LP |
1574 | free(u); |
1575 | } | |
1576 | ||
8d5ba5a9 | 1577 | assert(c <= n_units); |
729e3769 | 1578 | hashmap_free(h); |
5f056378 CH |
1579 | |
1580 | r = 0; | |
729e3769 | 1581 | } else { |
313fe66f | 1582 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
4afd3348 | 1583 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
4fbd7192 LP |
1584 | sd_bus *bus; |
1585 | ||
1586 | r = acquire_bus(BUS_MANAGER, &bus); | |
1587 | if (r < 0) | |
1588 | return r; | |
6e646d22 | 1589 | |
8010c205 | 1590 | r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "ListUnitFilesByPatterns"); |
313fe66f | 1591 | if (r < 0) |
1592 | return bus_log_create_error(r); | |
1593 | ||
1594 | r = sd_bus_message_append_strv(m, arg_states); | |
1595 | if (r < 0) | |
1596 | return bus_log_create_error(r); | |
1597 | ||
e9c387c8 KK |
1598 | if (arg_with_dependencies) { |
1599 | _cleanup_strv_free_ char **names_with_deps = NULL; | |
1600 | ||
1601 | r = append_unit_dependencies(bus, strv_skip(argv, 1), &names_with_deps); | |
1602 | if (r < 0) | |
1603 | return log_error_errno(r, "Failed to append unit dependencies: %m"); | |
1604 | ||
1605 | r = sd_bus_message_append_strv(m, names_with_deps); | |
1606 | if (r < 0) | |
1607 | return bus_log_create_error(r); | |
1608 | } else { | |
1609 | r = sd_bus_message_append_strv(m, strv_skip(argv, 1)); | |
1610 | if (r < 0) | |
1611 | return bus_log_create_error(r); | |
1612 | } | |
313fe66f | 1613 | |
1614 | r = sd_bus_call(bus, m, 0, &error, &reply); | |
1615 | if (r < 0 && sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_METHOD)) { | |
1616 | /* Fallback to legacy ListUnitFiles method */ | |
1617 | fallback = true; | |
1618 | log_debug_errno(r, "Failed to list unit files: %s Falling back to ListUnitsFiles method.", bus_error_message(&error, r)); | |
1619 | m = sd_bus_message_unref(m); | |
1620 | sd_bus_error_free(&error); | |
1621 | ||
8010c205 | 1622 | r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "ListUnitFiles"); |
313fe66f | 1623 | if (r < 0) |
1624 | return bus_log_create_error(r); | |
1625 | ||
1626 | r = sd_bus_call(bus, m, 0, &error, &reply); | |
1627 | } | |
691395d8 LP |
1628 | if (r < 0) |
1629 | return log_error_errno(r, "Failed to list unit files: %s", bus_error_message(&error, r)); | |
729e3769 | 1630 | |
f459b602 MAP |
1631 | r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ss)"); |
1632 | if (r < 0) | |
1633 | return bus_log_parse_error(r); | |
729e3769 | 1634 | |
f459b602 | 1635 | while ((r = sd_bus_message_read(reply, "(ss)", &path, &state)) > 0) { |
729e3769 | 1636 | |
f459b602 MAP |
1637 | if (!GREEDY_REALLOC(units, size, c + 1)) |
1638 | return log_oom(); | |
729e3769 | 1639 | |
8d5ba5a9 | 1640 | units[c] = (struct UnitFileList) { |
f459b602 MAP |
1641 | path, |
1642 | unit_file_state_from_string(state) | |
1643 | }; | |
8d5ba5a9 | 1644 | |
313fe66f | 1645 | if (output_show_unit_file(&units[c], |
1646 | fallback ? arg_states : NULL, | |
1647 | fallback ? strv_skip(argv, 1) : NULL)) | |
313cefa1 | 1648 | c++; |
8d5ba5a9 | 1649 | |
729e3769 | 1650 | } |
f459b602 MAP |
1651 | if (r < 0) |
1652 | return bus_log_parse_error(r); | |
1653 | ||
1654 | r = sd_bus_message_exit_container(reply); | |
1655 | if (r < 0) | |
1656 | return bus_log_parse_error(r); | |
729e3769 LP |
1657 | } |
1658 | ||
0221d68a | 1659 | (void) pager_open(arg_pager_flags); |
d2ad7e1b | 1660 | |
93bab288 | 1661 | typesafe_qsort(units, c, compare_unit_file_list); |
de9a8fe1 FS |
1662 | r = output_unit_file_list(units, c); |
1663 | if (r < 0) | |
1664 | return r; | |
729e3769 | 1665 | |
0da999fa | 1666 | if (install_client_side()) |
8d5ba5a9 ZJS |
1667 | for (unit = units; unit < units + c; unit++) |
1668 | free(unit->path); | |
1669 | ||
eeb1542b GS |
1670 | if (c == 0) |
1671 | return -ENOENT; | |
1672 | ||
f84190d8 | 1673 | return 0; |
729e3769 LP |
1674 | } |
1675 | ||
14cb109d | 1676 | static int list_dependencies_print(const char *name, int level, unsigned branches, bool last) { |
55c0b89c | 1677 | _cleanup_free_ char *n = NULL; |
9607d947 | 1678 | size_t max_len = MAX(columns(),20u); |
cbc9fbd1 LP |
1679 | size_t len = 0; |
1680 | int i; | |
55c0b89c | 1681 | |
5d0c05e5 | 1682 | if (!arg_plain) { |
cbc9fbd1 | 1683 | |
5d0c05e5 LN |
1684 | for (i = level - 1; i >= 0; i--) { |
1685 | len += 2; | |
f168c273 | 1686 | if (len > max_len - 3 && !arg_full) { |
5d0c05e5 LN |
1687 | printf("%s...\n",max_len % 2 ? "" : " "); |
1688 | return 0; | |
1689 | } | |
9a6f746f | 1690 | printf("%s", special_glyph(branches & (1 << i) ? SPECIAL_GLYPH_TREE_VERTICAL : SPECIAL_GLYPH_TREE_SPACE)); |
5d0c05e5 | 1691 | } |
55c0b89c | 1692 | len += 2; |
cbc9fbd1 | 1693 | |
f168c273 | 1694 | if (len > max_len - 3 && !arg_full) { |
55c0b89c LN |
1695 | printf("%s...\n",max_len % 2 ? "" : " "); |
1696 | return 0; | |
1697 | } | |
cbc9fbd1 | 1698 | |
9a6f746f | 1699 | printf("%s", special_glyph(last ? SPECIAL_GLYPH_TREE_RIGHT : SPECIAL_GLYPH_TREE_BRANCH)); |
55c0b89c | 1700 | } |
55c0b89c | 1701 | |
9ed794a3 | 1702 | if (arg_full) { |
55c0b89c LN |
1703 | printf("%s\n", name); |
1704 | return 0; | |
1705 | } | |
1706 | ||
1707 | n = ellipsize(name, max_len-len, 100); | |
f168c273 | 1708 | if (!n) |
55c0b89c LN |
1709 | return log_oom(); |
1710 | ||
1711 | printf("%s\n", n); | |
1712 | return 0; | |
1713 | } | |
1714 | ||
93bab288 | 1715 | static int list_dependencies_compare(char * const *a, char * const *b) { |
55c0b89c LN |
1716 | if (unit_name_to_type(*a) == UNIT_TARGET && unit_name_to_type(*b) != UNIT_TARGET) |
1717 | return 1; | |
1718 | if (unit_name_to_type(*a) != UNIT_TARGET && unit_name_to_type(*b) == UNIT_TARGET) | |
1719 | return -1; | |
cbc9fbd1 | 1720 | |
55c0b89c LN |
1721 | return strcasecmp(*a, *b); |
1722 | } | |
1723 | ||
f459b602 MAP |
1724 | static int list_dependencies_one( |
1725 | sd_bus *bus, | |
1726 | const char *name, | |
1727 | int level, | |
1728 | char ***units, | |
14cb109d | 1729 | unsigned branches) { |
f459b602 | 1730 | |
e3e45d4f | 1731 | _cleanup_strv_free_ char **deps = NULL; |
55c0b89c | 1732 | char **c; |
55c0b89c LN |
1733 | int r = 0; |
1734 | ||
cbc9fbd1 LP |
1735 | assert(bus); |
1736 | assert(name); | |
1737 | assert(units); | |
1738 | ||
e3e45d4f SP |
1739 | r = strv_extend(units, name); |
1740 | if (r < 0) | |
55c0b89c LN |
1741 | return log_oom(); |
1742 | ||
1743 | r = list_dependencies_get_dependencies(bus, name, &deps); | |
1744 | if (r < 0) | |
cec7eda5 | 1745 | return r; |
55c0b89c | 1746 | |
93bab288 | 1747 | typesafe_qsort(deps, strv_length(deps), list_dependencies_compare); |
55c0b89c LN |
1748 | |
1749 | STRV_FOREACH(c, deps) { | |
e3e45d4f | 1750 | if (strv_contains(*units, *c)) { |
5d0c05e5 | 1751 | if (!arg_plain) { |
60705040 | 1752 | printf(" "); |
5d0c05e5 LN |
1753 | r = list_dependencies_print("...", level + 1, (branches << 1) | (c[1] == NULL ? 0 : 1), 1); |
1754 | if (r < 0) | |
1755 | return r; | |
1756 | } | |
55c0b89c LN |
1757 | continue; |
1758 | } | |
1759 | ||
250ba664 ZJS |
1760 | if (arg_plain) |
1761 | printf(" "); | |
1762 | else { | |
fa0d5878 | 1763 | UnitActiveState active_state = _UNIT_ACTIVE_STATE_INVALID; |
250ba664 ZJS |
1764 | const char *on; |
1765 | ||
fa0d5878 | 1766 | (void) get_state_one_unit(bus, *c, &active_state); |
baa9ecc1 | 1767 | |
fa0d5878 | 1768 | switch (active_state) { |
baa9ecc1 LP |
1769 | case UNIT_ACTIVE: |
1770 | case UNIT_RELOADING: | |
1771 | case UNIT_ACTIVATING: | |
1772 | on = ansi_highlight_green(); | |
1773 | break; | |
1774 | ||
1775 | case UNIT_INACTIVE: | |
1776 | case UNIT_DEACTIVATING: | |
1777 | on = ansi_normal(); | |
1778 | break; | |
1779 | ||
1780 | default: | |
1781 | on = ansi_highlight_red(); | |
1782 | break; | |
fa0d5878 BR |
1783 | } |
1784 | ||
9a6f746f | 1785 | printf("%s%s%s ", on, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE), ansi_normal()); |
250ba664 | 1786 | } |
dbc2c080 | 1787 | |
55c0b89c | 1788 | r = list_dependencies_print(*c, level, branches, c[1] == NULL); |
cec7eda5 ZJS |
1789 | if (r < 0) |
1790 | return r; | |
55c0b89c LN |
1791 | |
1792 | if (arg_all || unit_name_to_type(*c) == UNIT_TARGET) { | |
e3e45d4f | 1793 | r = list_dependencies_one(bus, *c, level + 1, units, (branches << 1) | (c[1] == NULL ? 0 : 1)); |
f168c273 | 1794 | if (r < 0) |
cec7eda5 | 1795 | return r; |
55c0b89c LN |
1796 | } |
1797 | } | |
f459b602 | 1798 | |
e3e45d4f SP |
1799 | if (!arg_plain) |
1800 | strv_remove(*units, name); | |
f459b602 | 1801 | |
cec7eda5 | 1802 | return 0; |
55c0b89c LN |
1803 | } |
1804 | ||
e449de87 | 1805 | static int list_dependencies(int argc, char *argv[], void *userdata) { |
94c3a838 ZJS |
1806 | _cleanup_strv_free_ char **units = NULL, **done = NULL; |
1807 | char **u, **patterns; | |
4fbd7192 | 1808 | sd_bus *bus; |
7410616c | 1809 | int r; |
55c0b89c | 1810 | |
4fbd7192 LP |
1811 | r = acquire_bus(BUS_MANAGER, &bus); |
1812 | if (r < 0) | |
1813 | return r; | |
1814 | ||
94c3a838 ZJS |
1815 | patterns = strv_skip(argv, 1); |
1816 | if (strv_isempty(patterns)) { | |
1817 | units = strv_new(SPECIAL_DEFAULT_TARGET); | |
1818 | if (!units) | |
1819 | return log_oom(); | |
1820 | } else { | |
1821 | r = expand_names(bus, patterns, NULL, &units, NULL); | |
1822 | if (r < 0) | |
1823 | return log_error_errno(r, "Failed to expand names: %m"); | |
1824 | } | |
1825 | ||
0221d68a | 1826 | (void) pager_open(arg_pager_flags); |
d2ad7e1b | 1827 | |
94c3a838 ZJS |
1828 | STRV_FOREACH(u, units) { |
1829 | if (u != units) | |
1830 | puts(""); | |
1831 | ||
1832 | puts(*u); | |
1833 | r = list_dependencies_one(bus, *u, 0, &done, 0); | |
1834 | if (r < 0) | |
1835 | return r; | |
1836 | } | |
e31165b2 | 1837 | |
94c3a838 | 1838 | return 0; |
55c0b89c LN |
1839 | } |
1840 | ||
0d292f5e LP |
1841 | struct machine_info { |
1842 | bool is_host; | |
1843 | char *name; | |
0d292f5e | 1844 | char *state; |
8fcf784d LP |
1845 | char *control_group; |
1846 | uint32_t n_failed_units; | |
1847 | uint32_t n_jobs; | |
1848 | usec_t timestamp; | |
1849 | }; | |
1850 | ||
1851 | static const struct bus_properties_map machine_info_property_map[] = { | |
1852 | { "SystemState", "s", NULL, offsetof(struct machine_info, state) }, | |
1853 | { "NJobs", "u", NULL, offsetof(struct machine_info, n_jobs) }, | |
1854 | { "NFailedUnits", "u", NULL, offsetof(struct machine_info, n_failed_units) }, | |
1855 | { "ControlGroup", "s", NULL, offsetof(struct machine_info, control_group) }, | |
1856 | { "UserspaceTimestamp", "t", NULL, offsetof(struct machine_info, timestamp) }, | |
1857 | {} | |
0d292f5e LP |
1858 | }; |
1859 | ||
e7e55dbd | 1860 | static void machine_info_clear(struct machine_info *info) { |
f8654baa ZJS |
1861 | assert(info); |
1862 | ||
1863 | free(info->name); | |
1864 | free(info->state); | |
1865 | free(info->control_group); | |
1866 | zero(*info); | |
e7e55dbd DH |
1867 | } |
1868 | ||
0d292f5e LP |
1869 | static void free_machines_list(struct machine_info *machine_infos, int n) { |
1870 | int i; | |
1871 | ||
1872 | if (!machine_infos) | |
1873 | return; | |
1874 | ||
e7e55dbd DH |
1875 | for (i = 0; i < n; i++) |
1876 | machine_info_clear(&machine_infos[i]); | |
0d292f5e LP |
1877 | |
1878 | free(machine_infos); | |
1879 | } | |
1880 | ||
93bab288 YW |
1881 | static int compare_machine_info(const struct machine_info *a, const struct machine_info *b) { |
1882 | int r; | |
0d292f5e | 1883 | |
93bab288 YW |
1884 | r = CMP(b->is_host, a->is_host); |
1885 | if (r != 0) | |
1886 | return r; | |
0d292f5e | 1887 | |
93bab288 | 1888 | return strcasecmp(a->name, b->name); |
0d292f5e LP |
1889 | } |
1890 | ||
1891 | static int get_machine_properties(sd_bus *bus, struct machine_info *mi) { | |
4afd3348 | 1892 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *container = NULL; |
0d292f5e LP |
1893 | int r; |
1894 | ||
1895 | assert(mi); | |
1896 | ||
1897 | if (!bus) { | |
de33fc62 | 1898 | r = sd_bus_open_system_machine(&container, mi->name); |
0d292f5e LP |
1899 | if (r < 0) |
1900 | return r; | |
1901 | ||
1902 | bus = container; | |
1903 | } | |
1904 | ||
a7e4861c YW |
1905 | r = bus_map_all_properties( |
1906 | bus, | |
1907 | "org.freedesktop.systemd1", | |
1908 | "/org/freedesktop/systemd1", | |
1909 | machine_info_property_map, | |
1910 | BUS_MAP_STRDUP, | |
1911 | NULL, | |
1912 | NULL, | |
1913 | mi); | |
0d292f5e LP |
1914 | if (r < 0) |
1915 | return r; | |
1916 | ||
1917 | return 0; | |
1918 | } | |
1919 | ||
1920 | static bool output_show_machine(const char *name, char **patterns) { | |
2404701e | 1921 | return strv_fnmatch_or_empty(patterns, name, FNM_NOESCAPE); |
0d292f5e LP |
1922 | } |
1923 | ||
1924 | static int get_machine_list( | |
1925 | sd_bus *bus, | |
1926 | struct machine_info **_machine_infos, | |
1927 | char **patterns) { | |
1928 | ||
1929 | struct machine_info *machine_infos = NULL; | |
1930 | _cleanup_strv_free_ char **m = NULL; | |
1931 | _cleanup_free_ char *hn = NULL; | |
1932 | size_t sz = 0; | |
1933 | char **i; | |
4c3e8e39 | 1934 | int c = 0, r; |
0d292f5e LP |
1935 | |
1936 | hn = gethostname_malloc(); | |
1937 | if (!hn) | |
1938 | return log_oom(); | |
1939 | ||
1940 | if (output_show_machine(hn, patterns)) { | |
1941 | if (!GREEDY_REALLOC0(machine_infos, sz, c+1)) | |
1942 | return log_oom(); | |
1943 | ||
1944 | machine_infos[c].is_host = true; | |
ae2a15bc | 1945 | machine_infos[c].name = TAKE_PTR(hn); |
0d292f5e | 1946 | |
f9e0eefc | 1947 | (void) get_machine_properties(bus, &machine_infos[c]); |
0d292f5e LP |
1948 | c++; |
1949 | } | |
1950 | ||
4c3e8e39 LP |
1951 | r = sd_get_machine_names(&m); |
1952 | if (r < 0) | |
1953 | return log_error_errno(r, "Failed to get machine list: %m"); | |
1954 | ||
0d292f5e LP |
1955 | STRV_FOREACH(i, m) { |
1956 | _cleanup_free_ char *class = NULL; | |
1957 | ||
1958 | if (!output_show_machine(*i, patterns)) | |
1959 | continue; | |
1960 | ||
1961 | sd_machine_get_class(*i, &class); | |
1962 | if (!streq_ptr(class, "container")) | |
1963 | continue; | |
1964 | ||
1965 | if (!GREEDY_REALLOC0(machine_infos, sz, c+1)) { | |
1966 | free_machines_list(machine_infos, c); | |
1967 | return log_oom(); | |
1968 | } | |
1969 | ||
1970 | machine_infos[c].is_host = false; | |
1971 | machine_infos[c].name = strdup(*i); | |
1972 | if (!machine_infos[c].name) { | |
1973 | free_machines_list(machine_infos, c); | |
1974 | return log_oom(); | |
1975 | } | |
1976 | ||
f9e0eefc | 1977 | (void) get_machine_properties(NULL, &machine_infos[c]); |
0d292f5e LP |
1978 | c++; |
1979 | } | |
1980 | ||
1981 | *_machine_infos = machine_infos; | |
1982 | return c; | |
1983 | } | |
1984 | ||
de9a8fe1 FS |
1985 | static int output_machines_list(struct machine_info *machine_infos, unsigned n) { |
1986 | _cleanup_(table_unrefp) Table *table = NULL; | |
0d292f5e | 1987 | struct machine_info *m; |
aa91d592 | 1988 | bool state_missing = false; |
de9a8fe1 | 1989 | int r; |
0d292f5e LP |
1990 | |
1991 | assert(machine_infos || n == 0); | |
1992 | ||
de9a8fe1 FS |
1993 | table = table_new("", "name", "state", "failed", "jobs"); |
1994 | if (!table) | |
1995 | return log_oom(); | |
0d292f5e | 1996 | |
de9a8fe1 | 1997 | table_set_header(table, !arg_no_legend); |
1cabd2d0 JW |
1998 | if (arg_plain) { |
1999 | /* Hide the 'glyph' column when --plain is requested */ | |
d171e679 FS |
2000 | r = table_hide_column_from_display(table, 0); |
2001 | if (r < 0) | |
2002 | return log_error_errno(r, "Failed to hide column: %m"); | |
2003 | } | |
de9a8fe1 FS |
2004 | if (arg_full) |
2005 | table_set_width(table, 0); | |
0d292f5e | 2006 | |
0773357a LP |
2007 | (void) table_set_empty_string(table, "-"); |
2008 | ||
0d292f5e | 2009 | for (m = machine_infos; m < machine_infos + n; m++) { |
de9a8fe1 FS |
2010 | _cleanup_free_ char *mname = NULL; |
2011 | const char *on_state = "", *on_failed = ""; | |
90c3f79d | 2012 | bool circle = false; |
0d292f5e LP |
2013 | |
2014 | if (streq_ptr(m->state, "degraded")) { | |
2015 | on_state = ansi_highlight_red(); | |
90c3f79d | 2016 | circle = true; |
0d292f5e LP |
2017 | } else if (!streq_ptr(m->state, "running")) { |
2018 | on_state = ansi_highlight_yellow(); | |
90c3f79d LP |
2019 | circle = true; |
2020 | } | |
0d292f5e | 2021 | |
de9a8fe1 | 2022 | if (m->n_failed_units > 0) |
0d292f5e | 2023 | on_failed = ansi_highlight_red(); |
de9a8fe1 FS |
2024 | else |
2025 | on_failed = ""; | |
90c3f79d | 2026 | |
4bea2469 ZJS |
2027 | if (!m->state) |
2028 | state_missing = true; | |
2029 | ||
0d292f5e | 2030 | if (m->is_host) |
de9a8fe1 FS |
2031 | mname = strjoin(strna(m->name), " (host)"); |
2032 | ||
2033 | r = table_add_many(table, | |
2034 | TABLE_STRING, circle ? special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE) : " ", | |
2035 | TABLE_SET_COLOR, on_state, | |
2036 | TABLE_STRING, m->is_host ? mname : strna(m->name), | |
2037 | TABLE_STRING, strna(m->state), | |
2038 | TABLE_SET_COLOR, on_state, | |
2039 | TABLE_UINT32, m->n_failed_units, | |
2040 | TABLE_SET_COLOR, on_failed, | |
2041 | TABLE_UINT32, m->n_jobs); | |
2042 | if (r < 0) | |
2043 | return table_log_add_error(r); | |
0d292f5e LP |
2044 | } |
2045 | ||
49cd06fa | 2046 | r = output_table(table); |
de9a8fe1 | 2047 | if (r < 0) |
49cd06fa | 2048 | return r; |
de9a8fe1 | 2049 | |
4bea2469 ZJS |
2050 | if (!arg_no_legend) { |
2051 | printf("\n"); | |
2052 | if (state_missing && geteuid() != 0) | |
2053 | printf("Notice: some information only available to privileged users was not shown.\n"); | |
2054 | printf("%u machines listed.\n", n); | |
2055 | } | |
de9a8fe1 FS |
2056 | |
2057 | return 0; | |
0d292f5e LP |
2058 | } |
2059 | ||
e449de87 | 2060 | static int list_machines(int argc, char *argv[], void *userdata) { |
0d292f5e | 2061 | struct machine_info *machine_infos = NULL; |
4fbd7192 | 2062 | sd_bus *bus; |
de9a8fe1 | 2063 | int r, rc; |
0d292f5e | 2064 | |
4fbd7192 LP |
2065 | r = acquire_bus(BUS_MANAGER, &bus); |
2066 | if (r < 0) | |
2067 | return r; | |
2068 | ||
e449de87 | 2069 | r = get_machine_list(bus, &machine_infos, strv_skip(argv, 1)); |
0d292f5e LP |
2070 | if (r < 0) |
2071 | return r; | |
2072 | ||
0221d68a | 2073 | (void) pager_open(arg_pager_flags); |
d2ad7e1b | 2074 | |
93bab288 | 2075 | typesafe_qsort(machine_infos, r, compare_machine_info); |
de9a8fe1 | 2076 | rc = output_machines_list(machine_infos, r); |
0d292f5e LP |
2077 | free_machines_list(machine_infos, r); |
2078 | ||
de9a8fe1 | 2079 | return rc; |
0d292f5e LP |
2080 | } |
2081 | ||
5e59431c ZJS |
2082 | static int parse_proc_cmdline_item(const char *key, const char *value, void *data) { |
2083 | char **ret = data; | |
2084 | ||
2085 | if (streq(key, "systemd.unit")) { | |
2086 | if (proc_cmdline_value_missing(key, value)) | |
2087 | return 0; | |
2088 | if (!unit_name_is_valid(value, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE)) | |
2089 | return log_warning("Unit name specified on %s= is not valid, ignoring: %s", key, value); | |
2090 | ||
2091 | return free_and_strdup_warn(ret, key); | |
2092 | ||
2093 | } else if (!value) { | |
2094 | if (runlevel_to_target(key)) | |
2095 | return free_and_strdup_warn(ret, key); | |
2096 | } | |
2097 | ||
2098 | return 0; | |
2099 | } | |
2100 | ||
2101 | static void emit_cmdline_warning(void) { | |
2102 | if (arg_quiet || arg_root) | |
2103 | /* don't bother checking the commandline if we're operating on a container */ | |
2104 | return; | |
2105 | ||
2106 | _cleanup_free_ char *override = NULL; | |
2107 | int r; | |
2108 | ||
2109 | r = proc_cmdline_parse(parse_proc_cmdline_item, &override, 0); | |
2110 | if (r < 0) | |
2111 | log_debug_errno(r, "Failed to parse kernel command line, ignoring: %m"); | |
2112 | if (override) | |
2113 | log_notice("Note: found \"%s\" on the kernel commandline, which overrides the default unit.", | |
2114 | override); | |
2115 | } | |
2116 | ||
12877da2 | 2117 | static int determine_default(char **ret_name) { |
99504dd4 | 2118 | int r; |
99504dd4 | 2119 | |
4fbd7192 | 2120 | if (install_client_side()) { |
12877da2 | 2121 | r = unit_file_get_default(arg_scope, arg_root, ret_name); |
f647962d MS |
2122 | if (r < 0) |
2123 | return log_error_errno(r, "Failed to get default target: %m"); | |
12877da2 | 2124 | return 0; |
99504dd4 | 2125 | |
99504dd4 | 2126 | } else { |
4fbd7192 | 2127 | sd_bus *bus; |
12877da2 ZJS |
2128 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
2129 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
2130 | const char *name; | |
4fbd7192 LP |
2131 | |
2132 | r = acquire_bus(BUS_MANAGER, &bus); | |
2133 | if (r < 0) | |
2134 | return r; | |
6e646d22 | 2135 | |
8010c205 | 2136 | r = bus_call_method(bus, bus_systemd_mgr, "GetDefaultTarget", &error, &reply, NULL); |
691395d8 LP |
2137 | if (r < 0) |
2138 | return log_error_errno(r, "Failed to get default target: %s", bus_error_message(&error, r)); | |
99504dd4 | 2139 | |
12877da2 | 2140 | r = sd_bus_message_read(reply, "s", &name); |
f459b602 MAP |
2141 | if (r < 0) |
2142 | return bus_log_parse_error(r); | |
12877da2 ZJS |
2143 | |
2144 | return free_and_strdup_warn(ret_name, name); | |
99504dd4 | 2145 | } |
12877da2 ZJS |
2146 | } |
2147 | ||
2148 | static int get_default(int argc, char *argv[], void *userdata) { | |
2149 | _cleanup_free_ char *name = NULL; | |
2150 | int r; | |
99504dd4 | 2151 | |
12877da2 ZJS |
2152 | r = determine_default(&name); |
2153 | if (r < 0) | |
2154 | return r; | |
2155 | ||
2156 | printf("%s\n", name); | |
99504dd4 | 2157 | |
5e59431c ZJS |
2158 | emit_cmdline_warning(); |
2159 | ||
f459b602 | 2160 | return 0; |
99504dd4 VP |
2161 | } |
2162 | ||
e449de87 | 2163 | static int set_default(int argc, char *argv[], void *userdata) { |
718db961 | 2164 | _cleanup_free_ char *unit = NULL; |
acc0269c | 2165 | UnitFileChange *changes = NULL; |
da6053d0 | 2166 | size_t n_changes = 0; |
718db961 LP |
2167 | int r; |
2168 | ||
e449de87 LP |
2169 | assert(argc >= 2); |
2170 | assert(argv); | |
2171 | ||
df7c4eb6 ZJS |
2172 | r = unit_name_mangle_with_suffix(argv[1], "set-default", |
2173 | arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN, | |
2174 | ".target", &unit); | |
7410616c LP |
2175 | if (r < 0) |
2176 | return log_error_errno(r, "Failed to mangle unit name: %m"); | |
718db961 | 2177 | |
4fbd7192 | 2178 | if (install_client_side()) { |
b3796dd8 | 2179 | r = unit_file_set_default(arg_scope, UNIT_FILE_FORCE, arg_root, unit, &changes, &n_changes); |
af3d8113 | 2180 | unit_file_dump_changes(r, "set default", changes, n_changes, arg_quiet); |
5f056378 CH |
2181 | |
2182 | if (r > 0) | |
2183 | r = 0; | |
718db961 | 2184 | } else { |
4afd3348 LP |
2185 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
2186 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
4fbd7192 | 2187 | sd_bus *bus; |
718db961 | 2188 | |
8a4b13c5 | 2189 | polkit_agent_open_maybe(); |
6e646d22 | 2190 | |
4fbd7192 LP |
2191 | r = acquire_bus(BUS_MANAGER, &bus); |
2192 | if (r < 0) | |
2193 | return r; | |
2194 | ||
8010c205 | 2195 | r = bus_call_method(bus, bus_systemd_mgr, "SetDefaultTarget", &error, &reply, "sb", unit, 1); |
691395d8 LP |
2196 | if (r < 0) |
2197 | return log_error_errno(r, "Failed to set default target: %s", bus_error_message(&error, r)); | |
718db961 | 2198 | |
acc0269c | 2199 | r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet, &changes, &n_changes); |
718db961 | 2200 | if (r < 0) |
acc0269c | 2201 | goto finish; |
718db961 | 2202 | |
93c941e3 | 2203 | /* Try to reload if enabled */ |
718db961 | 2204 | if (!arg_no_reload) |
e449de87 | 2205 | r = daemon_reload(argc, argv, userdata); |
718db961 LP |
2206 | else |
2207 | r = 0; | |
2208 | } | |
2209 | ||
5e59431c ZJS |
2210 | emit_cmdline_warning(); |
2211 | ||
12877da2 ZJS |
2212 | if (!arg_quiet) { |
2213 | _cleanup_free_ char *final = NULL; | |
2214 | ||
2215 | r = determine_default(&final); | |
2216 | if (r < 0) | |
2217 | return r; | |
2218 | ||
2219 | if (!streq(final, unit)) | |
2220 | log_notice("Note: \"%s\" is the default unit (possibly a runtime override).", final); | |
2221 | } | |
2222 | ||
acc0269c CH |
2223 | finish: |
2224 | unit_file_changes_free(changes, n_changes); | |
2225 | ||
718db961 LP |
2226 | return r; |
2227 | } | |
2228 | ||
de9a8fe1 | 2229 | static int output_waiting_jobs(sd_bus *bus, Table *table, uint32_t id, const char *method, const char *prefix) { |
82948f6c LP |
2230 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
2231 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
31d99bd1 | 2232 | const char *name, *type; |
82948f6c LP |
2233 | uint32_t other_id; |
2234 | int r; | |
2235 | ||
2236 | assert(bus); | |
2237 | ||
8010c205 | 2238 | r = bus_call_method(bus, bus_systemd_mgr, method, &error, &reply, "u", id); |
76d8ca22 ZJS |
2239 | if (r < 0) |
2240 | return log_debug_errno(r, "Failed to get waiting jobs for job %" PRIu32, id); | |
82948f6c LP |
2241 | |
2242 | r = sd_bus_message_enter_container(reply, 'a', "(usssoo)"); | |
76d8ca22 ZJS |
2243 | if (r < 0) |
2244 | return bus_log_parse_error(r); | |
82948f6c | 2245 | |
de9a8fe1 FS |
2246 | while ((r = sd_bus_message_read(reply, "(usssoo)", &other_id, &name, &type, NULL, NULL, NULL)) > 0) { |
2247 | _cleanup_free_ char *row = NULL; | |
2248 | int rc; | |
2249 | ||
2250 | if (asprintf(&row, "%s %u (%s/%s)", prefix, other_id, name, type) < 0) | |
2251 | return log_oom(); | |
2252 | ||
2253 | rc = table_add_many(table, | |
2254 | TABLE_STRING, special_glyph(SPECIAL_GLYPH_TREE_RIGHT), | |
2255 | TABLE_STRING, row, | |
2256 | TABLE_EMPTY, | |
2257 | TABLE_EMPTY); | |
2258 | if (rc < 0) | |
2259 | return table_log_add_error(r); | |
2260 | } | |
2261 | ||
76d8ca22 ZJS |
2262 | if (r < 0) |
2263 | return bus_log_parse_error(r); | |
82948f6c LP |
2264 | |
2265 | r = sd_bus_message_exit_container(reply); | |
76d8ca22 ZJS |
2266 | if (r < 0) |
2267 | return bus_log_parse_error(r); | |
2268 | ||
2269 | return 0; | |
82948f6c LP |
2270 | } |
2271 | ||
75add28a ZJS |
2272 | struct job_info { |
2273 | uint32_t id; | |
f459b602 | 2274 | const char *name, *type, *state; |
75add28a ZJS |
2275 | }; |
2276 | ||
de9a8fe1 FS |
2277 | static int output_jobs_list(sd_bus *bus, const struct job_info* jobs, unsigned n, bool skipped) { |
2278 | _cleanup_(table_unrefp) Table *table = NULL; | |
f459b602 | 2279 | const struct job_info *j; |
75add28a | 2280 | const char *on, *off; |
de9a8fe1 | 2281 | int r; |
75add28a ZJS |
2282 | |
2283 | assert(n == 0 || jobs); | |
2284 | ||
2285 | if (n == 0) { | |
250ba664 ZJS |
2286 | if (!arg_no_legend) { |
2287 | on = ansi_highlight_green(); | |
1fc464f6 | 2288 | off = ansi_normal(); |
75add28a | 2289 | |
250ba664 ZJS |
2290 | printf("%sNo jobs %s.%s\n", on, skipped ? "listed" : "running", off); |
2291 | } | |
de9a8fe1 | 2292 | return 0; |
75add28a ZJS |
2293 | } |
2294 | ||
0221d68a | 2295 | (void) pager_open(arg_pager_flags); |
75add28a | 2296 | |
de9a8fe1 FS |
2297 | table = table_new("job", "unit", "type", "state"); |
2298 | if (!table) | |
2299 | return log_oom(); | |
75add28a | 2300 | |
de9a8fe1 FS |
2301 | table_set_header(table, !arg_no_legend); |
2302 | if (arg_full) | |
2303 | table_set_width(table, 0); | |
75add28a | 2304 | |
0773357a LP |
2305 | (void) table_set_empty_string(table, "-"); |
2306 | ||
f459b602 | 2307 | for (j = jobs; j < jobs + n; j++) { |
de9a8fe1 | 2308 | if (streq(j->state, "running")) |
f459b602 | 2309 | on = ansi_highlight(); |
de9a8fe1 FS |
2310 | else |
2311 | on = ""; | |
2312 | ||
de9a8fe1 FS |
2313 | r = table_add_many(table, |
2314 | TABLE_UINT, j->id, | |
2315 | TABLE_STRING, j->name, | |
2316 | TABLE_SET_COLOR, on, | |
2317 | TABLE_STRING, j->type, | |
2318 | TABLE_STRING, j->state, | |
2319 | TABLE_SET_COLOR, on); | |
2320 | if (r < 0) | |
2321 | return table_log_add_error(r); | |
82948f6c LP |
2322 | |
2323 | if (arg_jobs_after) | |
de9a8fe1 | 2324 | output_waiting_jobs(bus, table, j->id, "GetJobAfter", "\twaiting for job"); |
82948f6c | 2325 | if (arg_jobs_before) |
de9a8fe1 | 2326 | output_waiting_jobs(bus, table, j->id, "GetJobBefore", "\tblocking job"); |
75add28a ZJS |
2327 | } |
2328 | ||
de9a8fe1 FS |
2329 | r = table_print(table, NULL); |
2330 | if (r < 0) | |
2331 | return log_error_errno(r, "Failed to print the table: %m"); | |
2332 | ||
6ce774fd MM |
2333 | if (!arg_no_legend) { |
2334 | on = ansi_highlight(); | |
1fc464f6 | 2335 | off = ansi_normal(); |
75add28a | 2336 | |
6ce774fd MM |
2337 | printf("\n%s%u jobs listed%s.\n", on, n, off); |
2338 | } | |
de9a8fe1 FS |
2339 | |
2340 | return 0; | |
75add28a ZJS |
2341 | } |
2342 | ||
d8fba7c6 | 2343 | static bool output_show_job(struct job_info *job, char **patterns) { |
2404701e | 2344 | return strv_fnmatch_or_empty(patterns, job->name, FNM_NOESCAPE); |
d8fba7c6 ZJS |
2345 | } |
2346 | ||
e449de87 | 2347 | static int list_jobs(int argc, char *argv[], void *userdata) { |
4afd3348 LP |
2348 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
2349 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
f459b602 | 2350 | _cleanup_free_ struct job_info *jobs = NULL; |
31d99bd1 YW |
2351 | const char *name, *type, *state; |
2352 | bool skipped = false; | |
f459b602 MAP |
2353 | size_t size = 0; |
2354 | unsigned c = 0; | |
4fbd7192 | 2355 | sd_bus *bus; |
f459b602 | 2356 | uint32_t id; |
f84190d8 | 2357 | int r; |
ec14911e | 2358 | |
4fbd7192 LP |
2359 | r = acquire_bus(BUS_MANAGER, &bus); |
2360 | if (r < 0) | |
2361 | return r; | |
2362 | ||
8010c205 | 2363 | r = bus_call_method(bus, bus_systemd_mgr, "ListJobs", &error, &reply, NULL); |
691395d8 LP |
2364 | if (r < 0) |
2365 | return log_error_errno(r, "Failed to list jobs: %s", bus_error_message(&error, r)); | |
7e4249b9 | 2366 | |
f459b602 MAP |
2367 | r = sd_bus_message_enter_container(reply, 'a', "(usssoo)"); |
2368 | if (r < 0) | |
2369 | return bus_log_parse_error(r); | |
7e4249b9 | 2370 | |
31d99bd1 | 2371 | while ((r = sd_bus_message_read(reply, "(usssoo)", &id, &name, &type, &state, NULL, NULL)) > 0) { |
d8fba7c6 ZJS |
2372 | struct job_info job = { id, name, type, state }; |
2373 | ||
e449de87 | 2374 | if (!output_show_job(&job, strv_skip(argv, 1))) { |
d8fba7c6 ZJS |
2375 | skipped = true; |
2376 | continue; | |
2377 | } | |
8fe914ec | 2378 | |
f459b602 MAP |
2379 | if (!GREEDY_REALLOC(jobs, size, c + 1)) |
2380 | return log_oom(); | |
7e4249b9 | 2381 | |
d8fba7c6 | 2382 | jobs[c++] = job; |
7e4249b9 | 2383 | } |
f459b602 MAP |
2384 | if (r < 0) |
2385 | return bus_log_parse_error(r); | |
7e4249b9 | 2386 | |
f459b602 MAP |
2387 | r = sd_bus_message_exit_container(reply); |
2388 | if (r < 0) | |
2389 | return bus_log_parse_error(r); | |
f73e33d9 | 2390 | |
0221d68a | 2391 | (void) pager_open(arg_pager_flags); |
d2ad7e1b | 2392 | |
de9a8fe1 | 2393 | return output_jobs_list(bus, jobs, c, skipped); |
7e4249b9 LP |
2394 | } |
2395 | ||
e449de87 | 2396 | static int cancel_job(int argc, char *argv[], void *userdata) { |
4fbd7192 | 2397 | sd_bus *bus; |
729e3769 | 2398 | char **name; |
342641fb | 2399 | int r = 0; |
7e4249b9 | 2400 | |
e449de87 | 2401 | if (argc <= 1) |
2853b60a | 2402 | return trivial_method(argc, argv, userdata); |
ee5762e3 | 2403 | |
4fbd7192 LP |
2404 | r = acquire_bus(BUS_MANAGER, &bus); |
2405 | if (r < 0) | |
2406 | return r; | |
2407 | ||
8a4b13c5 | 2408 | polkit_agent_open_maybe(); |
d2ad7e1b | 2409 | |
e449de87 | 2410 | STRV_FOREACH(name, strv_skip(argv, 1)) { |
4afd3348 | 2411 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
5dd9014f | 2412 | uint32_t id; |
342641fb | 2413 | int q; |
7e4249b9 | 2414 | |
342641fb | 2415 | q = safe_atou32(*name, &id); |
f647962d MS |
2416 | if (q < 0) |
2417 | return log_error_errno(q, "Failed to parse job id \"%s\": %m", *name); | |
7e4249b9 | 2418 | |
8010c205 | 2419 | q = bus_call_method(bus, bus_systemd_mgr, "CancelJob", &error, NULL, "u", id); |
342641fb | 2420 | if (q < 0) { |
691395d8 | 2421 | log_error_errno(q, "Failed to cancel job %"PRIu32": %s", id, bus_error_message(&error, q)); |
342641fb LP |
2422 | if (r == 0) |
2423 | r = q; | |
f459b602 | 2424 | } |
7e4249b9 LP |
2425 | } |
2426 | ||
342641fb | 2427 | return r; |
7e4249b9 LP |
2428 | } |
2429 | ||
f459b602 | 2430 | static int need_daemon_reload(sd_bus *bus, const char *unit) { |
4afd3348 | 2431 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
f459b602 MAP |
2432 | const char *path; |
2433 | int b, r; | |
94c01aeb | 2434 | |
f459b602 MAP |
2435 | /* We ignore all errors here, since this is used to show a |
2436 | * warning only */ | |
45fb0699 | 2437 | |
f459b602 MAP |
2438 | /* We don't use unit_dbus_path_from_name() directly since we |
2439 | * don't want to load the unit if it isn't loaded. */ | |
f84190d8 | 2440 | |
8010c205 | 2441 | r = bus_call_method(bus, bus_systemd_mgr, "GetUnit", NULL, &reply, "s", unit); |
f84190d8 LP |
2442 | if (r < 0) |
2443 | return r; | |
45fb0699 | 2444 | |
f459b602 MAP |
2445 | r = sd_bus_message_read(reply, "o", &path); |
2446 | if (r < 0) | |
2447 | return r; | |
f84190d8 | 2448 | |
f459b602 | 2449 | r = sd_bus_get_property_trivial( |
f22f08cd | 2450 | bus, |
b0193f1c LP |
2451 | "org.freedesktop.systemd1", |
2452 | path, | |
f459b602 MAP |
2453 | "org.freedesktop.systemd1.Unit", |
2454 | "NeedDaemonReload", | |
2455 | NULL, | |
2456 | 'b', &b); | |
f84190d8 LP |
2457 | if (r < 0) |
2458 | return r; | |
45fb0699 | 2459 | |
45fb0699 LP |
2460 | return b; |
2461 | } | |
2462 | ||
3f36991e | 2463 | static void warn_unit_file_changed(const char *name) { |
87ec20ef LP |
2464 | assert(name); |
2465 | ||
e9f4aabd | 2466 | 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 | 2467 | ansi_highlight_red(), |
1fc464f6 | 2468 | ansi_normal(), |
3f36991e ZJS |
2469 | name, |
2470 | arg_scope == UNIT_FILE_SYSTEM ? "" : " --user"); | |
2471 | } | |
2472 | ||
c45e7e0c | 2473 | static int unit_file_find_path(LookupPaths *lp, const char *unit_name, char **ret_unit_path) { |
33f6c497 ZJS |
2474 | char **p; |
2475 | ||
2476 | assert(lp); | |
2477 | assert(unit_name); | |
33f6c497 | 2478 | |
a3c4eb07 | 2479 | STRV_FOREACH(p, lp->search_path) { |
c6dd36b6 FS |
2480 | _cleanup_free_ char *path = NULL, *lpath = NULL; |
2481 | int r; | |
33f6c497 | 2482 | |
62a85ee0 | 2483 | path = path_join(*p, unit_name); |
33f6c497 ZJS |
2484 | if (!path) |
2485 | return log_oom(); | |
2486 | ||
a5648b80 | 2487 | r = chase_symlinks(path, arg_root, 0, &lpath, NULL); |
c6dd36b6 FS |
2488 | if (r == -ENOENT) |
2489 | continue; | |
2490 | if (r == -ENOMEM) | |
2491 | return log_oom(); | |
2492 | if (r < 0) | |
86f004fb | 2493 | return log_error_errno(r, "Failed to access path \"%s\": %m", path); |
c6dd36b6 | 2494 | |
c45e7e0c LP |
2495 | if (ret_unit_path) |
2496 | *ret_unit_path = TAKE_PTR(lpath); | |
ae2a15bc | 2497 | |
c6dd36b6 | 2498 | return 1; |
33f6c497 ZJS |
2499 | } |
2500 | ||
f67cb270 LP |
2501 | if (ret_unit_path) |
2502 | *ret_unit_path = NULL; | |
2503 | ||
33f6c497 ZJS |
2504 | return 0; |
2505 | } | |
2506 | ||
173471b7 ZJS |
2507 | static int unit_find_template_path( |
2508 | const char *unit_name, | |
2509 | LookupPaths *lp, | |
b03677e2 LP |
2510 | char **ret_fragment_path, |
2511 | char **ret_template) { | |
173471b7 | 2512 | |
b03677e2 | 2513 | _cleanup_free_ char *t = NULL, *f = NULL; |
173471b7 ZJS |
2514 | int r; |
2515 | ||
2516 | /* Returns 1 if a fragment was found, 0 if not found, negative on error. */ | |
2517 | ||
b03677e2 LP |
2518 | r = unit_file_find_path(lp, unit_name, &f); |
2519 | if (r < 0) | |
2520 | return r; | |
2521 | if (r > 0) { | |
2522 | if (ret_fragment_path) | |
2523 | *ret_fragment_path = TAKE_PTR(f); | |
2524 | if (ret_template) | |
2525 | *ret_template = NULL; | |
2526 | return r; /* found a real unit */ | |
2527 | } | |
2528 | ||
2529 | r = unit_name_template(unit_name, &t); | |
2530 | if (r == -EINVAL) { | |
2531 | if (ret_fragment_path) | |
2532 | *ret_fragment_path = NULL; | |
2533 | if (ret_template) | |
2534 | *ret_template = NULL; | |
173471b7 | 2535 | |
173471b7 | 2536 | return 0; /* not a template, does not exist */ |
b03677e2 | 2537 | } |
173471b7 ZJS |
2538 | if (r < 0) |
2539 | return log_error_errno(r, "Failed to determine template name: %m"); | |
2540 | ||
b03677e2 | 2541 | r = unit_file_find_path(lp, t, ret_fragment_path); |
173471b7 ZJS |
2542 | if (r < 0) |
2543 | return r; | |
2544 | ||
b03677e2 LP |
2545 | if (ret_template) |
2546 | *ret_template = r > 0 ? TAKE_PTR(t) : NULL; | |
ae2a15bc | 2547 | |
173471b7 ZJS |
2548 | return r; |
2549 | } | |
2550 | ||
6e646d22 LP |
2551 | static int unit_find_paths( |
2552 | sd_bus *bus, | |
2553 | const char *unit_name, | |
6e646d22 | 2554 | LookupPaths *lp, |
02a126a3 LP |
2555 | bool force_client_side, |
2556 | char **ret_fragment_path, | |
2557 | char ***ret_dropin_paths) { | |
dab2bce8 | 2558 | |
dab2bce8 | 2559 | _cleanup_strv_free_ char **dropins = NULL; |
02a126a3 | 2560 | _cleanup_free_ char *path = NULL; |
33f6c497 ZJS |
2561 | int r; |
2562 | ||
ad2a0358 | 2563 | /** |
e04df06f ZJS |
2564 | * Finds where the unit is defined on disk. Returns 0 if the unit is not found. Returns 1 if it is |
2565 | * found, and sets: | |
02a126a3 | 2566 | * - the path to the unit in *ret_frament_path, if it exists on disk, |
e04df06f ZJS |
2567 | * - and a strv of existing drop-ins in *ret_dropin_paths, if the arg is not NULL and any dropins |
2568 | * were found. | |
02a126a3 | 2569 | * |
e04df06f ZJS |
2570 | * Returns -ERFKILL if the unit is masked, and -EKEYREJECTED if the unit file could not be loaded for |
2571 | * some reason (the latter only applies if we are going through the service manager). | |
ad2a0358 ZJS |
2572 | */ |
2573 | ||
33f6c497 | 2574 | assert(unit_name); |
02a126a3 | 2575 | assert(ret_fragment_path); |
33f6c497 ZJS |
2576 | assert(lp); |
2577 | ||
02a126a3 LP |
2578 | /* Go via the bus to acquire the path, unless we are explicitly told not to, or when the unit name is a template */ |
2579 | if (!force_client_side && | |
2580 | !install_client_side() && | |
2581 | !unit_name_is_valid(unit_name, UNIT_NAME_TEMPLATE)) { | |
4afd3348 | 2582 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
6176e89c | 2583 | _cleanup_free_ char *load_state = NULL, *dbus_path = NULL; |
33f6c497 | 2584 | |
6176e89c ZJS |
2585 | dbus_path = unit_dbus_path_from_name(unit_name); |
2586 | if (!dbus_path) | |
33f6c497 ZJS |
2587 | return log_oom(); |
2588 | ||
02a126a3 LP |
2589 | r = sd_bus_get_property_string( |
2590 | bus, | |
2591 | "org.freedesktop.systemd1", | |
6176e89c | 2592 | dbus_path, |
02a126a3 LP |
2593 | "org.freedesktop.systemd1.Unit", |
2594 | "LoadState", | |
2595 | &error, | |
2596 | &load_state); | |
2597 | if (r < 0) | |
2598 | return log_error_errno(r, "Failed to get LoadState: %s", bus_error_message(&error, r)); | |
2599 | ||
2600 | if (streq(load_state, "masked")) | |
2601 | return -ERFKILL; | |
2602 | if (streq(load_state, "not-found")) { | |
2603 | r = 0; | |
2604 | goto not_found; | |
2605 | } | |
e04df06f | 2606 | if (!STR_IN_SET(load_state, "loaded", "bad-setting")) |
02a126a3 LP |
2607 | return -EKEYREJECTED; |
2608 | ||
33f6c497 ZJS |
2609 | r = sd_bus_get_property_string( |
2610 | bus, | |
2611 | "org.freedesktop.systemd1", | |
6176e89c | 2612 | dbus_path, |
33f6c497 ZJS |
2613 | "org.freedesktop.systemd1.Unit", |
2614 | "FragmentPath", | |
2615 | &error, | |
ad2a0358 ZJS |
2616 | &path); |
2617 | if (r < 0) | |
2618 | return log_error_errno(r, "Failed to get FragmentPath: %s", bus_error_message(&error, r)); | |
2619 | ||
02a126a3 | 2620 | if (ret_dropin_paths) { |
dab2bce8 IS |
2621 | r = sd_bus_get_property_strv( |
2622 | bus, | |
2623 | "org.freedesktop.systemd1", | |
6176e89c | 2624 | dbus_path, |
dab2bce8 IS |
2625 | "org.freedesktop.systemd1.Unit", |
2626 | "DropInPaths", | |
2627 | &error, | |
2628 | &dropins); | |
2629 | if (r < 0) | |
2630 | return log_error_errno(r, "Failed to get DropInPaths: %s", bus_error_message(&error, r)); | |
33f6c497 | 2631 | } |
ad2a0358 | 2632 | } else { |
e8630e69 ZJS |
2633 | const char *_path; |
2634 | _cleanup_set_free_free_ Set *names = NULL; | |
33f6c497 | 2635 | |
e8630e69 | 2636 | if (!cached_name_map) { |
91e0ee5f | 2637 | r = unit_file_build_name_map(lp, NULL, &cached_id_map, &cached_name_map, NULL); |
e8630e69 ZJS |
2638 | if (r < 0) |
2639 | return r; | |
2640 | } | |
33f6c497 | 2641 | |
e8630e69 | 2642 | r = unit_file_find_fragment(cached_id_map, cached_name_map, unit_name, &_path, &names); |
ad2a0358 ZJS |
2643 | if (r < 0) |
2644 | return r; | |
6483ad89 | 2645 | |
e8630e69 ZJS |
2646 | if (_path) { |
2647 | path = strdup(_path); | |
2648 | if (!path) | |
2649 | return log_oom(); | |
2650 | } | |
c6dd36b6 | 2651 | |
02a126a3 | 2652 | if (ret_dropin_paths) { |
043fdc40 ZJS |
2653 | r = unit_file_find_dropin_paths(arg_root, lp->search_path, NULL, |
2654 | ".d", ".conf", | |
4562c355 | 2655 | NULL, names, &dropins); |
dab2bce8 IS |
2656 | if (r < 0) |
2657 | return r; | |
2658 | } | |
2659 | } | |
2660 | ||
6176e89c ZJS |
2661 | if (isempty(path)) { |
2662 | *ret_fragment_path = NULL; | |
02a126a3 | 2663 | r = 0; |
6176e89c | 2664 | } else { |
02a126a3 | 2665 | *ret_fragment_path = TAKE_PTR(path); |
dab2bce8 | 2666 | r = 1; |
6176e89c | 2667 | } |
dab2bce8 | 2668 | |
02a126a3 LP |
2669 | if (ret_dropin_paths) { |
2670 | if (!strv_isempty(dropins)) { | |
2671 | *ret_dropin_paths = TAKE_PTR(dropins); | |
2672 | r = 1; | |
2673 | } else | |
2674 | *ret_dropin_paths = NULL; | |
33f6c497 | 2675 | } |
02a126a3 | 2676 | |
6483ad89 | 2677 | not_found: |
39c38ce1 | 2678 | if (r == 0 && !arg_force) |
b5e6a600 IS |
2679 | log_error("No files found for %s.", unit_name); |
2680 | ||
ad2a0358 | 2681 | return r; |
33f6c497 ZJS |
2682 | } |
2683 | ||
fa0d5878 | 2684 | static int get_state_one_unit(sd_bus *bus, const char *name, UnitActiveState *active_state) { |
b430f85f | 2685 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
6176e89c | 2686 | _cleanup_free_ char *buf = NULL, *dbus_path = NULL; |
fa0d5878 | 2687 | UnitActiveState state; |
f22f08cd | 2688 | int r; |
701cdcb9 | 2689 | |
31be1221 | 2690 | assert(name); |
fa0d5878 | 2691 | assert(active_state); |
701cdcb9 | 2692 | |
6176e89c ZJS |
2693 | dbus_path = unit_dbus_path_from_name(name); |
2694 | if (!dbus_path) | |
3d083b22 LP |
2695 | return log_oom(); |
2696 | ||
2697 | r = sd_bus_get_property_string( | |
f22f08cd SP |
2698 | bus, |
2699 | "org.freedesktop.systemd1", | |
6176e89c | 2700 | dbus_path, |
3d083b22 LP |
2701 | "org.freedesktop.systemd1.Unit", |
2702 | "ActiveState", | |
b430f85f | 2703 | &error, |
3d083b22 LP |
2704 | &buf); |
2705 | if (r < 0) | |
2706 | return log_error_errno(r, "Failed to retrieve unit state: %s", bus_error_message(&error, r)); | |
b430f85f | 2707 | |
3d083b22 | 2708 | state = unit_active_state_from_string(buf); |
2f50e59f LP |
2709 | if (state < 0) |
2710 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid unit state '%s' for: %s", buf, name); | |
701cdcb9 | 2711 | |
fa0d5878 BR |
2712 | *active_state = state; |
2713 | return 0; | |
701cdcb9 MS |
2714 | } |
2715 | ||
86f004fb | 2716 | static int unit_is_masked(sd_bus *bus, LookupPaths *lp, const char *name) { |
bd062910 | 2717 | _cleanup_free_ char *load_state = NULL; |
f22f08cd | 2718 | int r; |
701cdcb9 | 2719 | |
86f004fb ZJS |
2720 | if (unit_name_is_valid(name, UNIT_NAME_TEMPLATE)) { |
2721 | _cleanup_free_ char *path = NULL; | |
2722 | ||
2723 | /* A template cannot be loaded, but it can be still masked, so | |
2724 | * we need to use a different method. */ | |
2725 | ||
2726 | r = unit_file_find_path(lp, name, &path); | |
2727 | if (r < 0) | |
2728 | return r; | |
2729 | if (r == 0) | |
2730 | return false; | |
2731 | return null_or_empty_path(path); | |
2732 | } | |
2733 | ||
bd062910 | 2734 | r = unit_load_state(bus, name, &load_state); |
691395d8 | 2735 | if (r < 0) |
bd062910 | 2736 | return r; |
d0a5cdb2 | 2737 | |
9d9dd746 ZJS |
2738 | return streq(load_state, "masked"); |
2739 | } | |
2740 | ||
2741 | static int check_triggering_units(sd_bus *bus, const char *name) { | |
2742 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
6176e89c | 2743 | _cleanup_free_ char *n = NULL, *dbus_path = NULL, *load_state = NULL; |
9d9dd746 ZJS |
2744 | _cleanup_strv_free_ char **triggered_by = NULL; |
2745 | bool print_warning_label = true; | |
2746 | UnitActiveState active_state; | |
2747 | char **i; | |
2748 | int r; | |
2749 | ||
2750 | r = unit_name_mangle(name, 0, &n); | |
2751 | if (r < 0) | |
2752 | return log_error_errno(r, "Failed to mangle unit name: %m"); | |
2753 | ||
86f004fb ZJS |
2754 | r = unit_load_state(bus, name, &load_state); |
2755 | if (r < 0) | |
2756 | return r; | |
2757 | ||
2758 | if (streq(load_state, "masked")) | |
2759 | return 0; | |
9d9dd746 | 2760 | |
6176e89c ZJS |
2761 | dbus_path = unit_dbus_path_from_name(n); |
2762 | if (!dbus_path) | |
9d9dd746 | 2763 | return log_oom(); |
701cdcb9 | 2764 | |
f459b602 MAP |
2765 | r = sd_bus_get_property_strv( |
2766 | bus, | |
2767 | "org.freedesktop.systemd1", | |
6176e89c | 2768 | dbus_path, |
f459b602 MAP |
2769 | "org.freedesktop.systemd1.Unit", |
2770 | "TriggeredBy", | |
2771 | &error, | |
2772 | &triggered_by); | |
691395d8 LP |
2773 | if (r < 0) |
2774 | return log_error_errno(r, "Failed to get triggered by array of %s: %s", n, bus_error_message(&error, r)); | |
701cdcb9 | 2775 | |
f459b602 | 2776 | STRV_FOREACH(i, triggered_by) { |
fa0d5878 | 2777 | r = get_state_one_unit(bus, *i, &active_state); |
f647962d | 2778 | if (r < 0) |
fa0d5878 | 2779 | return r; |
701cdcb9 | 2780 | |
fa0d5878 | 2781 | if (!IN_SET(active_state, UNIT_ACTIVE, UNIT_RELOADING)) |
f459b602 | 2782 | continue; |
60f9ba0b | 2783 | |
f459b602 MAP |
2784 | if (print_warning_label) { |
2785 | log_warning("Warning: Stopping %s, but it can still be activated by:", n); | |
2786 | print_warning_label = false; | |
701cdcb9 | 2787 | } |
1c291cf3 | 2788 | |
f459b602 | 2789 | log_warning(" %s", *i); |
701cdcb9 | 2790 | } |
f459b602 MAP |
2791 | |
2792 | return 0; | |
701cdcb9 MS |
2793 | } |
2794 | ||
2fc9a280 | 2795 | static const struct { |
85d9b598 LP |
2796 | const char *verb; /* systemctl verb */ |
2797 | const char *method; /* Name of the specific D-Bus method */ | |
2798 | const char *job_type; /* Job type when passing to the generic EnqueueUnitJob() method */ | |
2fc9a280 | 2799 | } unit_actions[] = { |
85d9b598 LP |
2800 | { "start", "StartUnit", "start" }, |
2801 | { "stop", "StopUnit", "stop" }, | |
2802 | { "condstop", "StopUnit", "stop" }, /* legacy alias */ | |
2803 | { "reload", "ReloadUnit", "reload" }, | |
2804 | { "restart", "RestartUnit", "restart" }, | |
2805 | { "try-restart", "TryRestartUnit", "try-restart" }, | |
2806 | { "condrestart", "TryRestartUnit", "try-restart" }, /* legacy alias */ | |
2807 | { "reload-or-restart", "ReloadOrRestartUnit", "reload-or-restart" }, | |
2808 | { "try-reload-or-restart", "ReloadOrTryRestartUnit", "reload-or-try-restart" }, | |
2809 | { "reload-or-try-restart", "ReloadOrTryRestartUnit", "reload-or-try-restart" }, /* legacy alias */ | |
2810 | { "condreload", "ReloadOrTryRestartUnit", "reload-or-try-restart" }, /* legacy alias */ | |
2811 | { "force-reload", "ReloadOrTryRestartUnit", "reload-or-try-restart" }, /* legacy alias */ | |
2fc9a280 LP |
2812 | }; |
2813 | ||
39602c39 | 2814 | static const char *verb_to_method(const char *verb) { |
85d9b598 | 2815 | size_t i; |
39602c39 TA |
2816 | |
2817 | for (i = 0; i < ELEMENTSOF(unit_actions); i++) | |
2818 | if (streq_ptr(unit_actions[i].verb, verb)) | |
2819 | return unit_actions[i].method; | |
2820 | ||
2821 | return "StartUnit"; | |
2822 | } | |
2823 | ||
85d9b598 LP |
2824 | static const char *verb_to_job_type(const char *verb) { |
2825 | size_t i; | |
39602c39 TA |
2826 | |
2827 | for (i = 0; i < ELEMENTSOF(unit_actions); i++) | |
85d9b598 LP |
2828 | if (streq_ptr(unit_actions[i].verb, verb)) |
2829 | return unit_actions[i].job_type; | |
39602c39 | 2830 | |
85d9b598 | 2831 | return "start"; |
39602c39 TA |
2832 | } |
2833 | ||
e4b61340 | 2834 | static int start_unit_one( |
f459b602 | 2835 | sd_bus *bus, |
85d9b598 LP |
2836 | const char *method, /* When using classic per-job bus methods */ |
2837 | const char *job_type, /* When using new-style EnqueueUnitJob() */ | |
e4b61340 LP |
2838 | const char *name, |
2839 | const char *mode, | |
f459b602 | 2840 | sd_bus_error *error, |
93a08841 | 2841 | BusWaitForJobs *w, |
a7056cde | 2842 | BusWaitForUnits *wu) { |
7e4249b9 | 2843 | |
4afd3348 | 2844 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
45fb0699 | 2845 | const char *path; |
85d9b598 | 2846 | bool done = false; |
7e4249b9 | 2847 | int r; |
7e4249b9 | 2848 | |
e4b61340 LP |
2849 | assert(method); |
2850 | assert(name); | |
2851 | assert(mode); | |
22f4096c | 2852 | assert(error); |
7e4249b9 | 2853 | |
dbc9830c ZJS |
2854 | log_debug("%s dbus call org.freedesktop.systemd1.Manager %s(%s, %s)", |
2855 | arg_dry_run ? "Would execute" : "Executing", | |
1ae17672 | 2856 | method, name, mode); |
85d9b598 | 2857 | |
1ae17672 ZJS |
2858 | if (arg_dry_run) |
2859 | return 0; | |
342641fb | 2860 | |
85d9b598 LP |
2861 | if (arg_show_transaction) { |
2862 | _cleanup_(sd_bus_error_free) sd_bus_error enqueue_error = SD_BUS_ERROR_NULL; | |
39602c39 | 2863 | |
85d9b598 | 2864 | /* Use the new, fancy EnqueueUnitJob() API if the user wants us to print the transaction */ |
8010c205 | 2865 | r = bus_call_method( |
85d9b598 | 2866 | bus, |
8010c205 | 2867 | bus_systemd_mgr, |
85d9b598 LP |
2868 | "EnqueueUnitJob", |
2869 | &enqueue_error, | |
2870 | &reply, | |
2871 | "sss", | |
2872 | name, job_type, mode); | |
2873 | if (r < 0) { | |
2874 | if (!sd_bus_error_has_name(&enqueue_error, SD_BUS_ERROR_UNKNOWN_METHOD)) { | |
2875 | (void) sd_bus_error_move(error, &enqueue_error); | |
2876 | goto fail; | |
2877 | } | |
67f3c402 | 2878 | |
85d9b598 LP |
2879 | /* Hmm, the API is not yet available. Let's use the classic API instead (see below). */ |
2880 | log_notice("--show-transaction not supported by this service manager, proceeding without."); | |
2881 | } else { | |
2882 | const char *u, *jt; | |
2883 | uint32_t id; | |
39602c39 | 2884 | |
85d9b598 LP |
2885 | r = sd_bus_message_read(reply, "uosos", &id, &path, &u, NULL, &jt); |
2886 | if (r < 0) | |
2887 | return bus_log_parse_error(r); | |
ee87525c | 2888 | |
85d9b598 | 2889 | log_info("Enqueued anchor job %" PRIu32 " %s/%s.", id, u, jt); |
ee87525c | 2890 | |
85d9b598 LP |
2891 | r = sd_bus_message_enter_container(reply, 'a', "(uosos)"); |
2892 | if (r < 0) | |
2893 | return bus_log_parse_error(r); | |
2894 | for (;;) { | |
2895 | r = sd_bus_message_read(reply, "(uosos)", &id, NULL, &u, NULL, &jt); | |
2896 | if (r < 0) | |
2897 | return bus_log_parse_error(r); | |
2898 | if (r == 0) | |
2899 | break; | |
2900 | ||
2901 | log_info("Enqueued auxiliary job %" PRIu32 " %s/%s.", id, u, jt); | |
2902 | } | |
2903 | ||
2904 | r = sd_bus_message_exit_container(reply); | |
2905 | if (r < 0) | |
2906 | return bus_log_parse_error(r); | |
2907 | ||
2908 | done = true; | |
2909 | } | |
7e4249b9 LP |
2910 | } |
2911 | ||
85d9b598 | 2912 | if (!done) { |
8010c205 | 2913 | r = bus_call_method(bus, bus_systemd_mgr, method, error, &reply, "ss", name, mode); |
85d9b598 LP |
2914 | if (r < 0) |
2915 | goto fail; | |
2916 | ||
2917 | r = sd_bus_message_read(reply, "o", &path); | |
2918 | if (r < 0) | |
2919 | return bus_log_parse_error(r); | |
2920 | } | |
45fb0699 | 2921 | |
e3e0314b | 2922 | if (need_daemon_reload(bus, name) > 0) |
3f36991e | 2923 | warn_unit_file_changed(name); |
45fb0699 | 2924 | |
ebd011d9 LP |
2925 | if (w) { |
2926 | log_debug("Adding %s to the set", path); | |
2927 | r = bus_wait_for_jobs_add(w, path); | |
cbc9fbd1 | 2928 | if (r < 0) |
d2e1b2fd | 2929 | return log_error_errno(r, "Failed to watch job for %s: %m", name); |
e4b61340 | 2930 | } |
7e4249b9 | 2931 | |
a7056cde LP |
2932 | if (wu) { |
2933 | r = bus_wait_for_units_add_unit(wu, name, BUS_WAIT_FOR_INACTIVE|BUS_WAIT_NO_JOB, NULL, NULL); | |
2934 | if (r < 0) | |
2935 | return log_error_errno(r, "Failed to watch unit %s: %m", name); | |
2936 | } | |
2937 | ||
46eddbb5 | 2938 | return 0; |
85d9b598 LP |
2939 | |
2940 | fail: | |
2941 | /* There's always a fallback possible for legacy actions. */ | |
2942 | if (arg_action != ACTION_SYSTEMCTL) | |
2943 | return r; | |
2944 | ||
2945 | log_error_errno(r, "Failed to %s %s: %s", job_type, name, bus_error_message(error, r)); | |
2946 | ||
2947 | if (!sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT) && | |
2948 | !sd_bus_error_has_name(error, BUS_ERROR_UNIT_MASKED) && | |
2949 | !sd_bus_error_has_name(error, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE)) | |
2950 | log_error("See %s logs and 'systemctl%s status%s %s' for details.", | |
2951 | arg_scope == UNIT_FILE_SYSTEM ? "system" : "user", | |
2952 | arg_scope == UNIT_FILE_SYSTEM ? "" : " --user", | |
2953 | name[0] == '-' ? " --" : "", | |
2954 | name); | |
2955 | ||
2956 | return r; | |
7e4249b9 LP |
2957 | } |
2958 | ||
47a0eaa6 MS |
2959 | static const struct { |
2960 | const char *target; | |
2961 | const char *verb; | |
2962 | const char *mode; | |
2963 | } action_table[_ACTION_MAX] = { | |
5fd77930 LP |
2964 | [ACTION_HALT] = { SPECIAL_HALT_TARGET, "halt", "replace-irreversibly" }, |
2965 | [ACTION_POWEROFF] = { SPECIAL_POWEROFF_TARGET, "poweroff", "replace-irreversibly" }, | |
2966 | [ACTION_REBOOT] = { SPECIAL_REBOOT_TARGET, "reboot", "replace-irreversibly" }, | |
2967 | [ACTION_KEXEC] = { SPECIAL_KEXEC_TARGET, "kexec", "replace-irreversibly" }, | |
2968 | [ACTION_RUNLEVEL2] = { SPECIAL_MULTI_USER_TARGET, NULL, "isolate" }, | |
2969 | [ACTION_RUNLEVEL3] = { SPECIAL_MULTI_USER_TARGET, NULL, "isolate" }, | |
2970 | [ACTION_RUNLEVEL4] = { SPECIAL_MULTI_USER_TARGET, NULL, "isolate" }, | |
2971 | [ACTION_RUNLEVEL5] = { SPECIAL_GRAPHICAL_TARGET, NULL, "isolate" }, | |
2972 | [ACTION_RESCUE] = { SPECIAL_RESCUE_TARGET, "rescue", "isolate" }, | |
2973 | [ACTION_EMERGENCY] = { SPECIAL_EMERGENCY_TARGET, "emergency", "isolate" }, | |
2974 | [ACTION_DEFAULT] = { SPECIAL_DEFAULT_TARGET, "default", "isolate" }, | |
2975 | [ACTION_EXIT] = { SPECIAL_EXIT_TARGET, "exit", "replace-irreversibly" }, | |
2976 | [ACTION_SUSPEND] = { SPECIAL_SUSPEND_TARGET, "suspend", "replace-irreversibly" }, | |
2977 | [ACTION_HIBERNATE] = { SPECIAL_HIBERNATE_TARGET, "hibernate", "replace-irreversibly" }, | |
2978 | [ACTION_HYBRID_SLEEP] = { SPECIAL_HYBRID_SLEEP_TARGET, "hybrid-sleep", "replace-irreversibly" }, | |
e68c79db | 2979 | [ACTION_SUSPEND_THEN_HIBERNATE] = { SPECIAL_SUSPEND_THEN_HIBERNATE_TARGET, "suspend-then-hibernate", "replace-irreversibly" }, |
47a0eaa6 MS |
2980 | }; |
2981 | ||
514f4ef5 | 2982 | static enum action verb_to_action(const char *verb) { |
47a0eaa6 MS |
2983 | enum action i; |
2984 | ||
913c1916 | 2985 | for (i = 0; i < _ACTION_MAX; i++) |
f459b602 | 2986 | if (streq_ptr(action_table[i].verb, verb)) |
47a0eaa6 | 2987 | return i; |
514f4ef5 | 2988 | |
f459b602 MAP |
2989 | return _ACTION_INVALID; |
2990 | } | |
e4b61340 | 2991 | |
94369fc0 LP |
2992 | static const char** make_extra_args(const char *extra_args[static 4]) { |
2993 | size_t n = 0; | |
2994 | ||
2caa38e9 LP |
2995 | assert(extra_args); |
2996 | ||
94369fc0 LP |
2997 | if (arg_scope != UNIT_FILE_SYSTEM) |
2998 | extra_args[n++] = "--user"; | |
2999 | ||
3000 | if (arg_transport == BUS_TRANSPORT_REMOTE) { | |
3001 | extra_args[n++] = "-H"; | |
3002 | extra_args[n++] = arg_host; | |
3003 | } else if (arg_transport == BUS_TRANSPORT_MACHINE) { | |
3004 | extra_args[n++] = "-M"; | |
3005 | extra_args[n++] = arg_host; | |
3006 | } else | |
3007 | assert(arg_transport == BUS_TRANSPORT_LOCAL); | |
3008 | ||
3009 | extra_args[n] = NULL; | |
3010 | return extra_args; | |
3011 | } | |
3012 | ||
e449de87 | 3013 | static int start_unit(int argc, char *argv[], void *userdata) { |
a7056cde | 3014 | _cleanup_(bus_wait_for_units_freep) BusWaitForUnits *wu = NULL; |
ebd011d9 | 3015 | _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL; |
85d9b598 | 3016 | const char *method, *job_type, *mode, *one_name, *suffix = NULL; |
416b8801 | 3017 | _cleanup_free_ char **stopped_units = NULL; /* Do not use _cleanup_strv_free_ */ |
ebd011d9 | 3018 | _cleanup_strv_free_ char **names = NULL; |
0e8d9c0c | 3019 | int r, ret = EXIT_SUCCESS; |
4fbd7192 | 3020 | sd_bus *bus; |
729e3769 | 3021 | char **name; |
e4b61340 | 3022 | |
2f50e59f LP |
3023 | if (arg_wait && !STR_IN_SET(argv[0], "start", "restart")) |
3024 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
3025 | "--wait may only be used with the 'start' or 'restart' commands."); | |
93a08841 MP |
3026 | |
3027 | /* we cannot do sender tracking on the private bus, so we need the full | |
3028 | * one for RefUnit to implement --wait */ | |
3029 | r = acquire_bus(arg_wait ? BUS_FULL : BUS_MANAGER, &bus); | |
4fbd7192 LP |
3030 | if (r < 0) |
3031 | return r; | |
3032 | ||
c59e2ec6 | 3033 | ask_password_agent_open_maybe(); |
8a4b13c5 | 3034 | polkit_agent_open_maybe(); |
d2ad7e1b | 3035 | |
e4b61340 | 3036 | if (arg_action == ACTION_SYSTEMCTL) { |
47a0eaa6 | 3037 | enum action action; |
e4b61340 | 3038 | |
e449de87 LP |
3039 | action = verb_to_action(argv[0]); |
3040 | ||
913c1916 | 3041 | if (action != _ACTION_INVALID) { |
85d9b598 | 3042 | /* A command in style "systemctl reboot", "systemctl poweroff", … */ |
913c1916 | 3043 | method = "StartUnit"; |
85d9b598 | 3044 | job_type = "start"; |
913c1916 AJ |
3045 | mode = action_table[action].mode; |
3046 | one_name = action_table[action].target; | |
3047 | } else { | |
3048 | if (streq(argv[0], "isolate")) { | |
85d9b598 | 3049 | /* A "systemctl isolate <unit1> <unit2> …" command */ |
913c1916 | 3050 | method = "StartUnit"; |
85d9b598 | 3051 | job_type = "start"; |
913c1916 | 3052 | mode = "isolate"; |
913c1916 AJ |
3053 | suffix = ".target"; |
3054 | } else { | |
85d9b598 | 3055 | /* A command in style of "systemctl start <unit1> <unit2> …", "sysemctl stop <unit1> <unit2> …" and so on */ |
913c1916 | 3056 | method = verb_to_method(argv[0]); |
85d9b598 | 3057 | job_type = verb_to_job_type(argv[0]); |
913c1916 AJ |
3058 | mode = arg_job_mode; |
3059 | } | |
3060 | one_name = NULL; | |
3061 | } | |
e4b61340 | 3062 | } else { |
85d9b598 | 3063 | /* A SysV legacy command such as "halt", "reboot", "poweroff", … */ |
78ca9099 | 3064 | assert(arg_action >= 0 && arg_action < _ACTION_MAX); |
47a0eaa6 | 3065 | assert(action_table[arg_action].target); |
913c1916 | 3066 | assert(action_table[arg_action].mode); |
e4b61340 LP |
3067 | |
3068 | method = "StartUnit"; | |
85d9b598 | 3069 | job_type = "start"; |
47a0eaa6 | 3070 | mode = action_table[arg_action].mode; |
e3e0314b | 3071 | one_name = action_table[arg_action].target; |
514f4ef5 LP |
3072 | } |
3073 | ||
8b7ec7bb | 3074 | if (one_name) { |
bea1a013 | 3075 | names = strv_new(one_name); |
8b7ec7bb LP |
3076 | if (!names) |
3077 | return log_oom(); | |
3078 | } else { | |
1d56bc09 ZJS |
3079 | bool expanded; |
3080 | ||
3081 | r = expand_names(bus, strv_skip(argv, 1), suffix, &names, &expanded); | |
e3e0314b | 3082 | if (r < 0) |
691395d8 | 3083 | return log_error_errno(r, "Failed to expand names: %m"); |
1d56bc09 ZJS |
3084 | |
3085 | if (!arg_all && expanded && streq(job_type, "start") && !arg_quiet) { | |
3086 | log_warning("Warning: %ssystemctl start called with a glob pattern.%s", | |
3087 | ansi_highlight_red(), | |
3088 | ansi_normal()); | |
3089 | log_notice("Hint: unit globs expand to loaded units, so start will usually have no effect.\n" | |
3090 | " Passing --all will also load units which are pulled in by other units.\n" | |
3091 | " See systemctl(1) for more details."); | |
3092 | } | |
e3e0314b | 3093 | } |
b6520546 | 3094 | |
6e905d93 | 3095 | if (!arg_no_block) { |
ebd011d9 | 3096 | r = bus_wait_for_jobs_new(bus, &w); |
f647962d MS |
3097 | if (r < 0) |
3098 | return log_error_errno(r, "Could not watch jobs: %m"); | |
e4b61340 LP |
3099 | } |
3100 | ||
93a08841 | 3101 | if (arg_wait) { |
8010c205 | 3102 | r = bus_call_method_async(bus, NULL, bus_systemd_mgr, "Subscribe", NULL, NULL, NULL); |
93a08841 | 3103 | if (r < 0) |
31b2cd5d | 3104 | return log_error_errno(r, "Failed to enable subscription: %m"); |
85d9b598 | 3105 | |
a7056cde | 3106 | r = bus_wait_for_units_new(bus, &wu); |
93a08841 | 3107 | if (r < 0) |
a7056cde | 3108 | return log_error_errno(r, "Failed to allocate unit watch context: %m"); |
93a08841 MP |
3109 | } |
3110 | ||
b6520546 | 3111 | STRV_FOREACH(name, names) { |
4afd3348 | 3112 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
f459b602 | 3113 | |
a7056cde | 3114 | r = start_unit_one(bus, method, job_type, *name, mode, &error, w, wu); |
0e8d9c0c LP |
3115 | if (ret == EXIT_SUCCESS && r < 0) |
3116 | ret = translate_bus_error_to_exit_status(r, &error); | |
416b8801 YW |
3117 | |
3118 | if (r >= 0 && streq(method, "StopUnit")) { | |
3119 | r = strv_push(&stopped_units, *name); | |
3120 | if (r < 0) | |
3121 | return log_oom(); | |
3122 | } | |
e4b61340 LP |
3123 | } |
3124 | ||
67f3c402 | 3125 | if (!arg_no_block) { |
94369fc0 | 3126 | const char* extra_args[4]; |
f459b602 | 3127 | |
94369fc0 | 3128 | r = bus_wait_for_jobs(w, arg_quiet, make_extra_args(extra_args)); |
0e8d9c0c LP |
3129 | if (r < 0) |
3130 | return r; | |
49111a70 ZJS |
3131 | |
3132 | /* When stopping units, warn if they can still be triggered by | |
3133 | * another active unit (socket, path, timer) */ | |
416b8801 YW |
3134 | if (!arg_quiet) |
3135 | STRV_FOREACH(name, stopped_units) | |
86f004fb | 3136 | (void) check_triggering_units(bus, *name); |
67f3c402 | 3137 | } |
514f4ef5 | 3138 | |
a7056cde LP |
3139 | if (arg_wait) { |
3140 | r = bus_wait_for_units_run(wu); | |
0e8d9c0c | 3141 | if (r < 0) |
a7056cde LP |
3142 | return log_error_errno(r, "Failed to wait for units: %m"); |
3143 | if (r == BUS_WAIT_FAILURE && ret == EXIT_SUCCESS) | |
0e8d9c0c | 3144 | ret = EXIT_FAILURE; |
93a08841 MP |
3145 | } |
3146 | ||
0e8d9c0c | 3147 | return ret; |
e4b61340 LP |
3148 | } |
3149 | ||
349cc4a5 | 3150 | #if ENABLE_LOGIND |
4fbd7192 | 3151 | static int logind_set_wall_message(void) { |
4afd3348 | 3152 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
4fbd7192 | 3153 | sd_bus *bus; |
f2d11d35 LP |
3154 | _cleanup_free_ char *m = NULL; |
3155 | int r; | |
3156 | ||
4fbd7192 LP |
3157 | r = acquire_bus(BUS_FULL, &bus); |
3158 | if (r < 0) | |
3159 | return r; | |
f2d11d35 LP |
3160 | |
3161 | m = strv_join(arg_wall, " "); | |
3162 | if (!m) | |
3163 | return log_oom(); | |
3164 | ||
dbc9830c ZJS |
3165 | log_debug("%s wall message \"%s\".", arg_dry_run ? "Would set" : "Setting", m); |
3166 | if (arg_dry_run) | |
3167 | return 0; | |
3168 | ||
8010c205 | 3169 | r = bus_call_method(bus, bus_login_mgr, "SetWallMessage", &error, NULL, "sb", m, !arg_no_wall); |
f2d11d35 LP |
3170 | if (r < 0) |
3171 | return log_warning_errno(r, "Failed to set wall message, ignoring: %s", bus_error_message(&error, r)); | |
f2d11d35 LP |
3172 | return 0; |
3173 | } | |
79f1c8f6 | 3174 | #endif |
f2d11d35 | 3175 | |
c45e5fb8 | 3176 | /* Ask systemd-logind, which might grant access to unprivileged users through polkit */ |
4fbd7192 | 3177 | static int logind_reboot(enum action a) { |
349cc4a5 | 3178 | #if ENABLE_LOGIND |
c45e5fb8 LP |
3179 | static const struct { |
3180 | const char *method; | |
3181 | const char *description; | |
3182 | } actions[_ACTION_MAX] = { | |
3183 | [ACTION_POWEROFF] = { "PowerOff", "power off system" }, | |
3184 | [ACTION_REBOOT] = { "Reboot", "reboot system" }, | |
3185 | [ACTION_HALT] = { "Halt", "halt system" }, | |
3186 | [ACTION_SUSPEND] = { "Suspend", "suspend system" }, | |
3187 | [ACTION_HIBERNATE] = { "Hibernate", "hibernate system" }, | |
3188 | [ACTION_HYBRID_SLEEP] = { "HybridSleep", "put system into hybrid sleep" }, | |
3189 | [ACTION_SUSPEND_THEN_HIBERNATE] = { "SuspendThenHibernate", "suspend system, hibernate later" }, | |
3190 | }; | |
3191 | ||
4afd3348 | 3192 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
4fbd7192 | 3193 | sd_bus *bus; |
f459b602 | 3194 | int r; |
4c80c73c | 3195 | |
c45e5fb8 LP |
3196 | if (a < 0 || a >= _ACTION_MAX || !actions[a].method) |
3197 | return -EINVAL; | |
3198 | ||
4fbd7192 LP |
3199 | r = acquire_bus(BUS_FULL, &bus); |
3200 | if (r < 0) | |
3201 | return r; | |
f2d11d35 | 3202 | |
8a4b13c5 | 3203 | polkit_agent_open_maybe(); |
d2ad7e1b ZJS |
3204 | (void) logind_set_wall_message(); |
3205 | ||
c45e5fb8 LP |
3206 | log_debug("%s org.freedesktop.login1.Manager %s dbus call.", arg_dry_run ? "Would execute" : "Executing", actions[a].method); |
3207 | ||
dbc9830c ZJS |
3208 | if (arg_dry_run) |
3209 | return 0; | |
3210 | ||
8010c205 | 3211 | r = bus_call_method(bus, bus_login_mgr, actions[a].method, &error, NULL, "b", arg_ask_password); |
f459b602 | 3212 | if (r < 0) |
c45e5fb8 | 3213 | return log_error_errno(r, "Failed to %s via logind: %s", actions[a].description, bus_error_message(&error, r)); |
f459b602 | 3214 | |
691395d8 | 3215 | return 0; |
4c80c73c KS |
3216 | #else |
3217 | return -ENOSYS; | |
3218 | #endif | |
3219 | } | |
3220 | ||
4fbd7192 | 3221 | static int logind_check_inhibitors(enum action a) { |
349cc4a5 | 3222 | #if ENABLE_LOGIND |
4afd3348 | 3223 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
59164be4 | 3224 | _cleanup_strv_free_ char **sessions = NULL; |
f459b602 MAP |
3225 | const char *what, *who, *why, *mode; |
3226 | uint32_t uid, pid; | |
4fbd7192 | 3227 | sd_bus *bus; |
f459b602 | 3228 | unsigned c = 0; |
59164be4 | 3229 | char **s; |
f459b602 | 3230 | int r; |
b37844d3 | 3231 | |
748ebafa LP |
3232 | if (arg_ignore_inhibitors || arg_force > 0) |
3233 | return 0; | |
3234 | ||
3235 | if (arg_when > 0) | |
3236 | return 0; | |
3237 | ||
3238 | if (geteuid() == 0) | |
b37844d3 LP |
3239 | return 0; |
3240 | ||
3241 | if (!on_tty()) | |
3242 | return 0; | |
e08ab379 LP |
3243 | |
3244 | if (arg_transport != BUS_TRANSPORT_LOCAL) | |
3245 | return 0; | |
b37844d3 | 3246 | |
4fbd7192 LP |
3247 | r = acquire_bus(BUS_FULL, &bus); |
3248 | if (r < 0) | |
3249 | return r; | |
3250 | ||
8010c205 | 3251 | r = bus_call_method(bus, bus_login_mgr, "ListInhibitors", NULL, &reply, NULL); |
b37844d3 LP |
3252 | if (r < 0) |
3253 | /* If logind is not around, then there are no inhibitors... */ | |
3254 | return 0; | |
3255 | ||
4aa2beac | 3256 | r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssuu)"); |
f459b602 MAP |
3257 | if (r < 0) |
3258 | return bus_log_parse_error(r); | |
b37844d3 | 3259 | |
4aa2beac | 3260 | while ((r = sd_bus_message_read(reply, "(ssssuu)", &what, &who, &why, &mode, &uid, &pid)) > 0) { |
59164be4 | 3261 | _cleanup_free_ char *comm = NULL, *user = NULL; |
f459b602 | 3262 | _cleanup_strv_free_ char **sv = NULL; |
b37844d3 LP |
3263 | |
3264 | if (!streq(mode, "block")) | |
f459b602 | 3265 | continue; |
b37844d3 LP |
3266 | |
3267 | sv = strv_split(what, ":"); | |
3268 | if (!sv) | |
3269 | return log_oom(); | |
3270 | ||
2f50e59f LP |
3271 | if (!pid_is_valid((pid_t) pid)) |
3272 | return log_error_errno(SYNTHETIC_ERRNO(ERANGE), "Invalid PID "PID_FMT".", (pid_t) pid); | |
d028e018 | 3273 | |
b37844d3 | 3274 | if (!strv_contains(sv, |
4c315c2c IS |
3275 | IN_SET(a, |
3276 | ACTION_HALT, | |
3277 | ACTION_POWEROFF, | |
3278 | ACTION_REBOOT, | |
3279 | ACTION_KEXEC) ? "shutdown" : "sleep")) | |
f459b602 | 3280 | continue; |
b37844d3 LP |
3281 | |
3282 | get_process_comm(pid, &comm); | |
59164be4 | 3283 | user = uid_to_name(uid); |
f459b602 | 3284 | |
de0671ee | 3285 | log_warning("Operation inhibited by \"%s\" (PID "PID_FMT" \"%s\", user %s), reason is \"%s\".", |
d028e018 | 3286 | who, (pid_t) pid, strna(comm), strna(user), why); |
b37844d3 | 3287 | |
f459b602 | 3288 | c++; |
b37844d3 | 3289 | } |
f459b602 MAP |
3290 | if (r < 0) |
3291 | return bus_log_parse_error(r); | |
b37844d3 | 3292 | |
f459b602 MAP |
3293 | r = sd_bus_message_exit_container(reply); |
3294 | if (r < 0) | |
3295 | return bus_log_parse_error(r); | |
b37844d3 | 3296 | |
59164be4 LP |
3297 | /* Check for current sessions */ |
3298 | sd_get_sessions(&sessions); | |
3299 | STRV_FOREACH(s, sessions) { | |
59164be4 LP |
3300 | _cleanup_free_ char *type = NULL, *tty = NULL, *seat = NULL, *user = NULL, *service = NULL, *class = NULL; |
3301 | ||
3302 | if (sd_session_get_uid(*s, &uid) < 0 || uid == getuid()) | |
3303 | continue; | |
3304 | ||
3305 | if (sd_session_get_class(*s, &class) < 0 || !streq(class, "user")) | |
3306 | continue; | |
3307 | ||
28abce5d | 3308 | if (sd_session_get_type(*s, &type) < 0 || !STR_IN_SET(type, "x11", "wayland", "tty", "mir")) |
59164be4 LP |
3309 | continue; |
3310 | ||
3311 | sd_session_get_tty(*s, &tty); | |
3312 | sd_session_get_seat(*s, &seat); | |
3313 | sd_session_get_service(*s, &service); | |
3314 | user = uid_to_name(uid); | |
3315 | ||
3316 | log_warning("User %s is logged in on %s.", strna(user), isempty(tty) ? (isempty(seat) ? strna(service) : seat) : tty); | |
3317 | c++; | |
3318 | } | |
3319 | ||
b37844d3 LP |
3320 | if (c <= 0) |
3321 | return 0; | |
3322 | ||
59164be4 | 3323 | log_error("Please retry operation after closing inhibitors and logging out other users.\nAlternatively, ignore inhibitors and users with 'systemctl %s -i'.", |
47a0eaa6 | 3324 | action_table[a].verb); |
b37844d3 LP |
3325 | |
3326 | return -EPERM; | |
3327 | #else | |
3328 | return 0; | |
3329 | #endif | |
3330 | } | |
3331 | ||
97af80c5 LP |
3332 | static int prepare_firmware_setup(void) { |
3333 | ||
3334 | if (!arg_firmware_setup) | |
3335 | return 0; | |
3336 | ||
349cc4a5 | 3337 | #if ENABLE_LOGIND |
4afd3348 | 3338 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
4fbd7192 | 3339 | sd_bus *bus; |
a4921cf4 JJ |
3340 | int r; |
3341 | ||
4fbd7192 LP |
3342 | r = acquire_bus(BUS_FULL, &bus); |
3343 | if (r < 0) | |
3344 | return r; | |
a4921cf4 | 3345 | |
8010c205 | 3346 | r = bus_call_method(bus, bus_login_mgr, "SetRebootToFirmwareSetup", &error, NULL, "b", true); |
4fbd7192 LP |
3347 | if (r < 0) |
3348 | return log_error_errno(r, "Cannot indicate to EFI to boot into setup mode: %s", bus_error_message(&error, r)); | |
a4921cf4 JJ |
3349 | |
3350 | return 0; | |
3351 | #else | |
2f50e59f LP |
3352 | return log_error_errno(SYNTHETIC_ERRNO(ENOSYS), |
3353 | "Booting into firmware setup not supported."); | |
a4921cf4 JJ |
3354 | #endif |
3355 | } | |
3356 | ||
97af80c5 LP |
3357 | static int prepare_boot_loader_menu(void) { |
3358 | ||
3359 | if (arg_boot_loader_menu == USEC_INFINITY) | |
3360 | return 0; | |
3361 | ||
3362 | #if ENABLE_LOGIND | |
3363 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
3364 | sd_bus *bus; | |
4fbd7192 LP |
3365 | int r; |
3366 | ||
97af80c5 LP |
3367 | r = acquire_bus(BUS_FULL, &bus); |
3368 | if (r < 0) | |
3369 | return r; | |
3370 | ||
8010c205 | 3371 | r = bus_call_method(bus, bus_login_mgr, "SetRebootToBootLoaderMenu", &error, NULL, "t", arg_boot_loader_menu); |
97af80c5 LP |
3372 | if (r < 0) |
3373 | return log_error_errno(r, "Cannot indicate to boot loader to enter boot loader entry menu: %s", bus_error_message(&error, r)); | |
3374 | ||
3375 | return 0; | |
3376 | #else | |
2f50e59f LP |
3377 | return log_error_errno(SYNTHETIC_ERRNO(ENOSYS), |
3378 | "Booting into boot loader menu not supported."); | |
97af80c5 LP |
3379 | #endif |
3380 | } | |
3381 | ||
3382 | static int prepare_boot_loader_entry(void) { | |
3383 | ||
3384 | if (!arg_boot_loader_entry) | |
4fbd7192 LP |
3385 | return 0; |
3386 | ||
97af80c5 LP |
3387 | #if ENABLE_LOGIND |
3388 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
3389 | sd_bus *bus; | |
3390 | int r; | |
4fbd7192 | 3391 | |
97af80c5 LP |
3392 | r = acquire_bus(BUS_FULL, &bus); |
3393 | if (r < 0) | |
3394 | return r; | |
3395 | ||
8010c205 | 3396 | r = bus_call_method(bus, bus_login_mgr, "SetRebootToBootLoaderEntry", &error, NULL, "s", arg_boot_loader_entry); |
97af80c5 LP |
3397 | if (r < 0) |
3398 | return log_error_errno(r, "Cannot set boot into loader entry '%s': %s", arg_boot_loader_entry, bus_error_message(&error, r)); | |
4fbd7192 | 3399 | |
97af80c5 LP |
3400 | return 0; |
3401 | #else | |
ee429649 LP |
3402 | return log_error_errno(SYNTHETIC_ERRNO(ENOSYS), |
3403 | "Booting into boot loader entry not supported."); | |
97af80c5 | 3404 | #endif |
4fbd7192 LP |
3405 | } |
3406 | ||
4bb2e9d4 ZJS |
3407 | static int load_kexec_kernel(void) { |
3408 | _cleanup_(boot_config_free) BootConfig config = {}; | |
43b736a8 | 3409 | _cleanup_free_ char *kernel = NULL, *initrd = NULL, *options = NULL; |
4bb2e9d4 ZJS |
3410 | const BootEntry *e; |
3411 | pid_t pid; | |
3412 | int r; | |
3413 | ||
3414 | if (kexec_loaded()) { | |
3415 | log_debug("Kexec kernel already loaded."); | |
3416 | return 0; | |
3417 | } | |
3418 | ||
c0d73214 ZJS |
3419 | if (access(KEXEC, X_OK) < 0) |
3420 | return log_error_errno(errno, KEXEC" is not available: %m"); | |
3421 | ||
38bd74d6 | 3422 | r = boot_entries_load_config_auto(NULL, NULL, &config); |
2fec5854 ZJS |
3423 | if (r == -ENOKEY) |
3424 | /* The call doesn't log about ENOKEY, let's do so here. */ | |
3425 | return log_error_errno(r, | |
3426 | "No kexec kernel loaded and autodetection failed.\n%s", | |
3427 | is_efi_boot() | |
3428 | ? "Cannot automatically load kernel: ESP partition mount point not found." | |
3429 | : "Automatic loading works only on systems booted with EFI."); | |
4bb2e9d4 | 3430 | if (r < 0) |
1b20d889 | 3431 | return r; |
4bb2e9d4 | 3432 | |
38bd74d6 LP |
3433 | e = boot_config_default_entry(&config); |
3434 | if (!e) | |
3435 | return log_error_errno(SYNTHETIC_ERRNO(ENOENT), | |
3436 | "No boot loader entry suitable as default, refusing to guess."); | |
3437 | ||
3438 | log_debug("Found default boot loader entry in file \"%s\"", e->path); | |
3439 | ||
78c38fe7 LP |
3440 | if (!e->kernel) |
3441 | return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), | |
3442 | "Boot entry does not refer to Linux kernel, which is not supported currently."); | |
8a1ef3bf | 3443 | if (strv_length(e->initrd) > 1) |
0f88d829 | 3444 | return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), |
8a1ef3bf | 3445 | "Boot entry specifies multiple initrds, which is not supported currently."); |
4bb2e9d4 | 3446 | |
43b736a8 | 3447 | kernel = path_join(e->root, e->kernel); |
9fc32cd9 LP |
3448 | if (!kernel) |
3449 | return log_oom(); | |
3450 | ||
3451 | if (!strv_isempty(e->initrd)) { | |
0f88d829 | 3452 | initrd = path_join(e->root, e->initrd[0]); |
9fc32cd9 LP |
3453 | if (!initrd) |
3454 | return log_oom(); | |
3455 | } | |
3456 | ||
4bb2e9d4 ZJS |
3457 | options = strv_join(e->options, " "); |
3458 | if (!options) | |
3459 | return log_oom(); | |
3460 | ||
cd086a01 ZJS |
3461 | log_full(arg_quiet ? LOG_DEBUG : LOG_INFO, |
3462 | "%s "KEXEC" --load \"%s\" --append \"%s\"%s%s%s", | |
3463 | arg_dry_run ? "Would run" : "Running", | |
3464 | kernel, | |
3465 | options, | |
3466 | initrd ? " --initrd \"" : NULL, strempty(initrd), initrd ? "\"" : ""); | |
4bb2e9d4 ZJS |
3467 | if (arg_dry_run) |
3468 | return 0; | |
3469 | ||
c943e95f | 3470 | r = safe_fork("(kexec)", FORK_WAIT|FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG, &pid); |
4c253ed1 | 3471 | if (r < 0) |
b6e1fff1 | 3472 | return r; |
4c253ed1 | 3473 | if (r == 0) { |
4bb2e9d4 ZJS |
3474 | const char* const args[] = { |
3475 | KEXEC, | |
3476 | "--load", kernel, | |
3477 | "--append", options, | |
3478 | initrd ? "--initrd" : NULL, initrd, | |
c943e95f LP |
3479 | NULL |
3480 | }; | |
4bb2e9d4 ZJS |
3481 | |
3482 | /* Child */ | |
4bb2e9d4 ZJS |
3483 | execv(args[0], (char * const *) args); |
3484 | _exit(EXIT_FAILURE); | |
4c253ed1 LP |
3485 | } |
3486 | ||
df685d57 | 3487 | return 0; |
4bb2e9d4 ZJS |
3488 | } |
3489 | ||
57ab9006 | 3490 | static int set_exit_code(uint8_t code) { |
4afd3348 | 3491 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
57ab9006 LP |
3492 | sd_bus *bus; |
3493 | int r; | |
3494 | ||
3495 | r = acquire_bus(BUS_MANAGER, &bus); | |
3496 | if (r < 0) | |
3497 | return r; | |
3498 | ||
8010c205 | 3499 | r = bus_call_method(bus, bus_systemd_mgr, "SetExitCode", &error, NULL, "y", code); |
57ab9006 | 3500 | if (r < 0) |
af3d8113 | 3501 | return log_error_errno(r, "Failed to set exit code: %s", bus_error_message(&error, r)); |
57ab9006 LP |
3502 | |
3503 | return 0; | |
3504 | } | |
3505 | ||
e449de87 | 3506 | static int start_special(int argc, char *argv[], void *userdata) { |
4c80c73c | 3507 | enum action a; |
983d9c90 | 3508 | int r; |
2b0f4e6f ZJS |
3509 | bool termination_action; /* an action that terminates the manager, |
3510 | * can be performed also by signal. */ | |
983d9c90 | 3511 | |
e449de87 | 3512 | assert(argv); |
514f4ef5 | 3513 | |
e449de87 | 3514 | a = verb_to_action(argv[0]); |
4c80c73c | 3515 | |
4fbd7192 | 3516 | r = logind_check_inhibitors(a); |
748ebafa LP |
3517 | if (r < 0) |
3518 | return r; | |
3519 | ||
fba868fa LP |
3520 | if (arg_force >= 2) { |
3521 | r = must_be_root(); | |
3522 | if (r < 0) | |
3523 | return r; | |
c32b90de LP |
3524 | } |
3525 | ||
4fbd7192 | 3526 | r = prepare_firmware_setup(); |
a4921cf4 JJ |
3527 | if (r < 0) |
3528 | return r; | |
5bdf2243 | 3529 | |
97af80c5 LP |
3530 | r = prepare_boot_loader_menu(); |
3531 | if (r < 0) | |
3532 | return r; | |
3533 | ||
3534 | r = prepare_boot_loader_entry(); | |
3535 | if (r < 0) | |
3536 | return r; | |
3537 | ||
dae710be | 3538 | if (a == ACTION_REBOOT) { |
3539 | const char *arg = NULL; | |
3540 | ||
3541 | if (argc > 1) { | |
3542 | if (arg_reboot_argument) | |
3543 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Both --reboot-argument= and positional argument passed to reboot command, refusing."); | |
3544 | ||
3545 | log_notice("Positional argument to reboot command is deprecated, please use --reboot-argument= instead. Accepting anyway."); | |
3546 | arg = argv[1]; | |
3547 | } else | |
3548 | arg = arg_reboot_argument; | |
3549 | ||
3550 | if (arg) { | |
3551 | r = update_reboot_parameter_and_warn(arg, false); | |
3552 | if (r < 0) | |
3553 | return r; | |
3554 | } | |
57ab9006 | 3555 | |
4bb2e9d4 ZJS |
3556 | } else if (a == ACTION_KEXEC) { |
3557 | r = load_kexec_kernel(); | |
d23b5ce2 ZJS |
3558 | if (r < 0 && arg_force >= 1) |
3559 | log_notice("Failed to load kexec kernel, continuing without."); | |
3560 | else if (r < 0) | |
4bb2e9d4 ZJS |
3561 | return r; |
3562 | ||
e449de87 | 3563 | } else if (a == ACTION_EXIT && argc > 1) { |
7bbb5359 | 3564 | uint8_t code; |
287419c1 | 3565 | |
57ab9006 LP |
3566 | /* If the exit code is not given on the command line, |
3567 | * don't reset it to zero: just keep it as it might | |
3568 | * have been set previously. */ | |
287419c1 | 3569 | |
e449de87 | 3570 | r = safe_atou8(argv[1], &code); |
4fbd7192 | 3571 | if (r < 0) |
57ab9006 | 3572 | return log_error_errno(r, "Invalid exit code."); |
4fbd7192 | 3573 | |
57ab9006 | 3574 | r = set_exit_code(code); |
691395d8 | 3575 | if (r < 0) |
57ab9006 | 3576 | return r; |
b986229e SW |
3577 | } |
3578 | ||
2b0f4e6f ZJS |
3579 | termination_action = IN_SET(a, |
3580 | ACTION_HALT, | |
3581 | ACTION_POWEROFF, | |
3582 | ACTION_REBOOT); | |
3583 | if (termination_action && arg_force >= 2) | |
477def80 | 3584 | return halt_now(a); |
e606bb61 | 3585 | |
7e59bfcb | 3586 | if (arg_force >= 1 && |
5955df2c | 3587 | (termination_action || IN_SET(a, ACTION_KEXEC, ACTION_EXIT))) |
2b0f4e6f ZJS |
3588 | r = trivial_method(argc, argv, userdata); |
3589 | else { | |
3590 | /* First try logind, to allow authentication with polkit */ | |
3591 | if (IN_SET(a, | |
3592 | ACTION_POWEROFF, | |
3593 | ACTION_REBOOT, | |
36b69c31 | 3594 | ACTION_HALT, |
2b0f4e6f ZJS |
3595 | ACTION_SUSPEND, |
3596 | ACTION_HIBERNATE, | |
c58493c0 | 3597 | ACTION_HYBRID_SLEEP, |
e68c79db | 3598 | ACTION_SUSPEND_THEN_HIBERNATE)) { |
2b0f4e6f ZJS |
3599 | |
3600 | r = logind_reboot(a); | |
3601 | if (r >= 0) | |
3602 | return r; | |
3603 | if (IN_SET(r, -EOPNOTSUPP, -EINPROGRESS)) | |
3604 | /* requested operation is not supported or already in progress */ | |
3605 | return r; | |
20b09ca7 | 3606 | |
130246d2 LP |
3607 | /* On all other errors, try low-level operation. In order to minimize the difference between |
3608 | * operation with and without logind, we explicitly enable non-blocking mode for this, as | |
3609 | * logind's shutdown operations are always non-blocking. */ | |
3610 | ||
3611 | arg_no_block = true; | |
3612 | ||
3613 | } else if (IN_SET(a, ACTION_EXIT, ACTION_KEXEC)) | |
3614 | /* Since exit/kexec are so close in behaviour to power-off/reboot, let's also make them | |
3615 | * asynchronous, in order to not confuse the user needlessly with unexpected behaviour. */ | |
3616 | arg_no_block = true; | |
7089051f | 3617 | |
2b0f4e6f | 3618 | r = start_unit(argc, argv, userdata); |
4c80c73c | 3619 | } |
983d9c90 | 3620 | |
2b0f4e6f ZJS |
3621 | if (termination_action && arg_force < 2 && |
3622 | IN_SET(r, -ENOENT, -ETIMEDOUT)) | |
cc7de2ba | 3623 | log_notice("It is possible to perform action directly, see discussion of --force --force in man:systemctl(1)."); |
2b0f4e6f ZJS |
3624 | |
3625 | return r; | |
514f4ef5 LP |
3626 | } |
3627 | ||
988b3b17 ZJS |
3628 | static int start_system_special(int argc, char *argv[], void *userdata) { |
3629 | /* Like start_special above, but raises an error when running in user mode */ | |
3630 | ||
baaa35ad ZJS |
3631 | if (arg_scope != UNIT_FILE_SYSTEM) |
3632 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
3633 | "Bad action for %s mode.", | |
3634 | arg_scope == UNIT_FILE_GLOBAL ? "--global" : "--user"); | |
988b3b17 ZJS |
3635 | |
3636 | return start_special(argc, argv, userdata); | |
3637 | } | |
3638 | ||
fa0d5878 | 3639 | static int check_unit_generic(int code, const UnitActiveState good_states[], int nb_states, char **args) { |
e3e0314b | 3640 | _cleanup_strv_free_ char **names = NULL; |
fa0d5878 | 3641 | UnitActiveState active_state; |
4fbd7192 | 3642 | sd_bus *bus; |
729e3769 | 3643 | char **name; |
fa0d5878 | 3644 | int r, i; |
d60f6ad0 | 3645 | bool found = false; |
0183528f | 3646 | |
4fbd7192 LP |
3647 | r = acquire_bus(BUS_MANAGER, &bus); |
3648 | if (r < 0) | |
3649 | return r; | |
3650 | ||
1d56bc09 | 3651 | r = expand_names(bus, args, NULL, &names, NULL); |
f647962d MS |
3652 | if (r < 0) |
3653 | return log_error_errno(r, "Failed to expand names: %m"); | |
e3e0314b ZJS |
3654 | |
3655 | STRV_FOREACH(name, names) { | |
fa0d5878 BR |
3656 | r = get_state_one_unit(bus, *name, &active_state); |
3657 | if (r < 0) | |
3658 | return r; | |
3659 | ||
3660 | if (!arg_quiet) | |
3661 | puts(unit_active_state_to_string(active_state)); | |
60f9ba0b | 3662 | |
fa0d5878 BR |
3663 | for (i = 0; i < nb_states; ++i) |
3664 | if (good_states[i] == active_state) | |
3665 | found = true; | |
1a0fce45 TA |
3666 | } |
3667 | ||
d60f6ad0 LN |
3668 | /* use the given return code for the case that we won't find |
3669 | * any unit which matches the list */ | |
3670 | return found ? 0 : code; | |
1a0fce45 TA |
3671 | } |
3672 | ||
e449de87 | 3673 | static int check_unit_active(int argc, char *argv[], void *userdata) { |
9897b09b LP |
3674 | static const UnitActiveState states[] = { |
3675 | UNIT_ACTIVE, | |
3676 | UNIT_RELOADING, | |
3677 | }; | |
3678 | ||
e3e0314b | 3679 | /* According to LSB: 3, "program is not running" */ |
b613907e | 3680 | return check_unit_generic(EXIT_PROGRAM_NOT_RUNNING, states, ELEMENTSOF(states), strv_skip(argv, 1)); |
e3e0314b | 3681 | } |
0183528f | 3682 | |
e449de87 | 3683 | static int check_unit_failed(int argc, char *argv[], void *userdata) { |
9897b09b LP |
3684 | static const UnitActiveState states[] = { |
3685 | UNIT_FAILED, | |
3686 | }; | |
3687 | ||
b613907e | 3688 | return check_unit_generic(EXIT_PROGRAM_DEAD_AND_PID_EXISTS, states, ELEMENTSOF(states), strv_skip(argv, 1)); |
48220598 LP |
3689 | } |
3690 | ||
e449de87 | 3691 | static int kill_unit(int argc, char *argv[], void *userdata) { |
e3e0314b | 3692 | _cleanup_strv_free_ char **names = NULL; |
ac5e3a50 | 3693 | char *kill_who = NULL, **name; |
4fbd7192 | 3694 | sd_bus *bus; |
e3e0314b | 3695 | int r, q; |
8a0867d6 | 3696 | |
4fbd7192 LP |
3697 | r = acquire_bus(BUS_MANAGER, &bus); |
3698 | if (r < 0) | |
3699 | return r; | |
3700 | ||
8a4b13c5 | 3701 | polkit_agent_open_maybe(); |
d2ad7e1b | 3702 | |
8a0867d6 LP |
3703 | if (!arg_kill_who) |
3704 | arg_kill_who = "all"; | |
3705 | ||
ac5e3a50 JS |
3706 | /* --fail was specified */ |
3707 | if (streq(arg_job_mode, "fail")) | |
81d62103 | 3708 | kill_who = strjoina(arg_kill_who, "-fail"); |
ac5e3a50 | 3709 | |
1d56bc09 | 3710 | r = expand_names(bus, strv_skip(argv, 1), NULL, &names, NULL); |
e3e0314b | 3711 | if (r < 0) |
691395d8 | 3712 | return log_error_errno(r, "Failed to expand names: %m"); |
60f9ba0b | 3713 | |
e3e0314b | 3714 | STRV_FOREACH(name, names) { |
4afd3348 | 3715 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
342641fb | 3716 | |
8010c205 | 3717 | q = bus_call_method( |
f22f08cd | 3718 | bus, |
8010c205 | 3719 | bus_systemd_mgr, |
6e646d22 LP |
3720 | "KillUnit", |
3721 | &error, | |
3722 | NULL, | |
169d7fb6 | 3723 | "ssi", *name, kill_who ? kill_who : arg_kill_who, arg_signal); |
e3e0314b | 3724 | if (q < 0) { |
169d7fb6 | 3725 | log_error_errno(q, "Failed to kill unit %s: %s", *name, bus_error_message(&error, q)); |
e3e0314b ZJS |
3726 | if (r == 0) |
3727 | r = q; | |
f459b602 | 3728 | } |
8a0867d6 | 3729 | } |
f459b602 | 3730 | |
e3e0314b | 3731 | return r; |
8a0867d6 LP |
3732 | } |
3733 | ||
d9e45bc3 | 3734 | static int clean_or_freeze_unit(int argc, char *argv[], void *userdata) { |
d81bc51f LP |
3735 | _cleanup_(bus_wait_for_units_freep) BusWaitForUnits *w = NULL; |
3736 | _cleanup_strv_free_ char **names = NULL; | |
3737 | int r, ret = EXIT_SUCCESS; | |
3738 | char **name; | |
d9e45bc3 | 3739 | const char *method; |
d81bc51f LP |
3740 | sd_bus *bus; |
3741 | ||
3742 | r = acquire_bus(BUS_FULL, &bus); | |
3743 | if (r < 0) | |
3744 | return r; | |
3745 | ||
3746 | polkit_agent_open_maybe(); | |
3747 | ||
3748 | if (!arg_clean_what) { | |
3749 | arg_clean_what = strv_new("cache", "runtime"); | |
3750 | if (!arg_clean_what) | |
3751 | return log_oom(); | |
3752 | } | |
3753 | ||
1d56bc09 | 3754 | r = expand_names(bus, strv_skip(argv, 1), NULL, &names, NULL); |
d81bc51f LP |
3755 | if (r < 0) |
3756 | return log_error_errno(r, "Failed to expand names: %m"); | |
3757 | ||
3758 | if (!arg_no_block) { | |
3759 | r = bus_wait_for_units_new(bus, &w); | |
3760 | if (r < 0) | |
3761 | return log_error_errno(r, "Failed to allocate unit waiter: %m"); | |
3762 | } | |
3763 | ||
d9e45bc3 MS |
3764 | if (streq(argv[0], "clean")) |
3765 | method = "CleanUnit"; | |
3766 | else if (streq(argv[0], "freeze")) | |
3767 | method = "FreezeUnit"; | |
3768 | else if (streq(argv[0], "thaw")) | |
3769 | method = "ThawUnit"; | |
fbb0b665 BR |
3770 | else |
3771 | assert_not_reached("Unhandled method"); | |
d9e45bc3 | 3772 | |
d81bc51f LP |
3773 | STRV_FOREACH(name, names) { |
3774 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
3775 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; | |
3776 | ||
3777 | if (w) { | |
3778 | /* If we shall wait for the cleaning to complete, let's add a ref on the unit first */ | |
8010c205 | 3779 | r = bus_call_method(bus, bus_systemd_mgr, "RefUnit", &error, NULL, "s", *name); |
d81bc51f LP |
3780 | if (r < 0) { |
3781 | log_error_errno(r, "Failed to add reference to unit %s: %s", *name, bus_error_message(&error, r)); | |
3782 | if (ret == EXIT_SUCCESS) | |
3783 | ret = r; | |
3784 | continue; | |
3785 | } | |
3786 | } | |
3787 | ||
8010c205 | 3788 | r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, method); |
d81bc51f LP |
3789 | if (r < 0) |
3790 | return bus_log_create_error(r); | |
3791 | ||
3792 | r = sd_bus_message_append(m, "s", *name); | |
3793 | if (r < 0) | |
3794 | return bus_log_create_error(r); | |
3795 | ||
d9e45bc3 MS |
3796 | if (streq(method, "CleanUnit")) { |
3797 | r = sd_bus_message_append_strv(m, arg_clean_what); | |
3798 | if (r < 0) | |
3799 | return bus_log_create_error(r); | |
3800 | } | |
d81bc51f LP |
3801 | |
3802 | r = sd_bus_call(bus, m, 0, &error, NULL); | |
3803 | if (r < 0) { | |
d9e45bc3 | 3804 | log_error_errno(r, "Failed to %s unit %s: %s", argv[0], *name, bus_error_message(&error, r)); |
d81bc51f LP |
3805 | if (ret == EXIT_SUCCESS) { |
3806 | ret = r; | |
3807 | continue; | |
3808 | } | |
3809 | } | |
3810 | ||
3811 | if (w) { | |
3812 | r = bus_wait_for_units_add_unit(w, *name, BUS_WAIT_REFFED|BUS_WAIT_FOR_MAINTENANCE_END, NULL, NULL); | |
3813 | if (r < 0) | |
3814 | return log_error_errno(r, "Failed to watch unit %s: %m", *name); | |
3815 | } | |
3816 | } | |
3817 | ||
3818 | r = bus_wait_for_units_run(w); | |
3819 | if (r < 0) | |
3820 | return log_error_errno(r, "Failed to wait for units: %m"); | |
3821 | if (r == BUS_WAIT_FAILURE) | |
3822 | ret = EXIT_FAILURE; | |
3823 | ||
3824 | return ret; | |
3825 | } | |
3826 | ||
582a507f | 3827 | typedef struct ExecStatusInfo { |
0129173a LP |
3828 | char *name; |
3829 | ||
582a507f LP |
3830 | char *path; |
3831 | char **argv; | |
3832 | ||
b708e7ce LP |
3833 | bool ignore; |
3834 | ||
582a507f LP |
3835 | usec_t start_timestamp; |
3836 | usec_t exit_timestamp; | |
3837 | pid_t pid; | |
3838 | int code; | |
3839 | int status; | |
3840 | ||
b3d59367 AZ |
3841 | ExecCommandFlags flags; |
3842 | ||
582a507f LP |
3843 | LIST_FIELDS(struct ExecStatusInfo, exec); |
3844 | } ExecStatusInfo; | |
3845 | ||
3846 | static void exec_status_info_free(ExecStatusInfo *i) { | |
3847 | assert(i); | |
3848 | ||
0129173a | 3849 | free(i->name); |
582a507f LP |
3850 | free(i->path); |
3851 | strv_free(i->argv); | |
3852 | free(i); | |
3853 | } | |
3854 | ||
b3d59367 AZ |
3855 | static int exec_status_info_deserialize(sd_bus_message *m, ExecStatusInfo *i, bool is_ex_prop) { |
3856 | _cleanup_strv_free_ char **ex_opts = NULL; | |
b21a0ef8 | 3857 | uint64_t start_timestamp, exit_timestamp, start_timestamp_monotonic, exit_timestamp_monotonic; |
f459b602 | 3858 | const char *path; |
582a507f LP |
3859 | uint32_t pid; |
3860 | int32_t code, status; | |
f459b602 | 3861 | int ignore, r; |
582a507f | 3862 | |
f459b602 | 3863 | assert(m); |
582a507f | 3864 | assert(i); |
582a507f | 3865 | |
b3d59367 | 3866 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_STRUCT, is_ex_prop ? "sasasttttuii" : "sasbttttuii"); |
f459b602 MAP |
3867 | if (r < 0) |
3868 | return bus_log_parse_error(r); | |
3869 | else if (r == 0) | |
3870 | return 0; | |
582a507f | 3871 | |
f459b602 MAP |
3872 | r = sd_bus_message_read(m, "s", &path); |
3873 | if (r < 0) | |
3874 | return bus_log_parse_error(r); | |
582a507f | 3875 | |
f84190d8 LP |
3876 | i->path = strdup(path); |
3877 | if (!i->path) | |
f459b602 | 3878 | return log_oom(); |
582a507f | 3879 | |
f459b602 MAP |
3880 | r = sd_bus_message_read_strv(m, &i->argv); |
3881 | if (r < 0) | |
3882 | return bus_log_parse_error(r); | |
3883 | ||
b3d59367 AZ |
3884 | r = is_ex_prop ? sd_bus_message_read_strv(m, &ex_opts) : sd_bus_message_read(m, "b", &ignore); |
3885 | if (r < 0) | |
3886 | return bus_log_parse_error(r); | |
3887 | ||
f459b602 | 3888 | r = sd_bus_message_read(m, |
b3d59367 | 3889 | "ttttuii", |
f459b602 MAP |
3890 | &start_timestamp, &start_timestamp_monotonic, |
3891 | &exit_timestamp, &exit_timestamp_monotonic, | |
3892 | &pid, | |
3893 | &code, &status); | |
3894 | if (r < 0) | |
3895 | return bus_log_parse_error(r); | |
582a507f | 3896 | |
b3d59367 AZ |
3897 | if (is_ex_prop) { |
3898 | r = exec_command_flags_from_strv(ex_opts, &i->flags); | |
3899 | if (r < 0) | |
3900 | return log_error_errno(r, "Failed to convert strv to ExecCommandFlags: %m"); | |
3901 | ||
3902 | i->ignore = FLAGS_SET(i->flags, EXEC_COMMAND_IGNORE_FAILURE); | |
3903 | } else | |
3904 | i->ignore = ignore; | |
3905 | ||
582a507f LP |
3906 | i->start_timestamp = (usec_t) start_timestamp; |
3907 | i->exit_timestamp = (usec_t) exit_timestamp; | |
3908 | i->pid = (pid_t) pid; | |
3909 | i->code = code; | |
3910 | i->status = status; | |
3911 | ||
f459b602 MAP |
3912 | r = sd_bus_message_exit_container(m); |
3913 | if (r < 0) | |
3914 | return bus_log_parse_error(r); | |
3915 | ||
3916 | return 1; | |
582a507f LP |
3917 | } |
3918 | ||
d5db7fe6 WC |
3919 | typedef struct UnitCondition { |
3920 | char *name; | |
b1ed76ae | 3921 | char *param; |
d5db7fe6 WC |
3922 | bool trigger; |
3923 | bool negate; | |
d5db7fe6 WC |
3924 | int tristate; |
3925 | ||
b1ed76ae | 3926 | LIST_FIELDS(struct UnitCondition, conditions); |
d5db7fe6 WC |
3927 | } UnitCondition; |
3928 | ||
3929 | static void unit_condition_free(UnitCondition *c) { | |
9bb71940 ZJS |
3930 | if (!c) |
3931 | return; | |
d5db7fe6 WC |
3932 | |
3933 | free(c->name); | |
3934 | free(c->param); | |
3935 | free(c); | |
3936 | } | |
3937 | ||
9bb71940 ZJS |
3938 | DEFINE_TRIVIAL_CLEANUP_FUNC(UnitCondition*, unit_condition_free); |
3939 | ||
61cbdc4b LP |
3940 | typedef struct UnitStatusInfo { |
3941 | const char *id; | |
3942 | const char *load_state; | |
3943 | const char *active_state; | |
d9e45bc3 | 3944 | const char *freezer_state; |
61cbdc4b | 3945 | const char *sub_state; |
a4375746 | 3946 | const char *unit_file_state; |
d2dc52db | 3947 | const char *unit_file_preset; |
61cbdc4b LP |
3948 | |
3949 | const char *description; | |
4a9e2fff | 3950 | const char *following; |
61cbdc4b | 3951 | |
49dbfa7b LP |
3952 | char **documentation; |
3953 | ||
1b64d026 LP |
3954 | const char *fragment_path; |
3955 | const char *source_path; | |
4ad49000 | 3956 | const char *control_group; |
61cbdc4b | 3957 | |
76d14b87 OS |
3958 | char **dropin_paths; |
3959 | ||
965c5d1d KK |
3960 | char **triggered_by; |
3961 | char **triggers; | |
3962 | ||
9f39404c | 3963 | const char *load_error; |
f42806df | 3964 | const char *result; |
9f39404c | 3965 | |
584be568 | 3966 | usec_t inactive_exit_timestamp; |
df50185b | 3967 | usec_t inactive_exit_timestamp_monotonic; |
584be568 LP |
3968 | usec_t active_enter_timestamp; |
3969 | usec_t active_exit_timestamp; | |
3970 | usec_t inactive_enter_timestamp; | |
3971 | ||
45fb0699 | 3972 | bool need_daemon_reload; |
055ef36b | 3973 | bool transient; |
45fb0699 | 3974 | |
61cbdc4b LP |
3975 | /* Service */ |
3976 | pid_t main_pid; | |
3977 | pid_t control_pid; | |
3978 | const char *status_text; | |
175728c4 | 3979 | const char *pid_file; |
d06dacd0 | 3980 | bool running:1; |
b4af5a80 | 3981 | int status_errno; |
61cbdc4b LP |
3982 | |
3983 | usec_t start_timestamp; | |
3984 | usec_t exit_timestamp; | |
3985 | ||
3986 | int exit_code, exit_status; | |
3987 | ||
d93dda3a LP |
3988 | const char *log_namespace; |
3989 | ||
90bbc946 LP |
3990 | usec_t condition_timestamp; |
3991 | bool condition_result; | |
b1ed76ae | 3992 | LIST_HEAD(UnitCondition, conditions); |
59fccdc5 LP |
3993 | |
3994 | usec_t assert_timestamp; | |
3995 | bool assert_result; | |
3996 | bool failed_assert_trigger; | |
3997 | bool failed_assert_negate; | |
3998 | const char *failed_assert; | |
3999 | const char *failed_assert_parameter; | |
601b0842 GS |
4000 | usec_t next_elapse_real; |
4001 | usec_t next_elapse_monotonic; | |
90bbc946 | 4002 | |
61cbdc4b LP |
4003 | /* Socket */ |
4004 | unsigned n_accepted; | |
4005 | unsigned n_connections; | |
a98f7575 | 4006 | unsigned n_refused; |
b8131a87 | 4007 | bool accept; |
61cbdc4b | 4008 | |
13160134 | 4009 | /* Pairs of type, path */ |
67419600 OS |
4010 | char **listen; |
4011 | ||
61cbdc4b LP |
4012 | /* Device */ |
4013 | const char *sysfs_path; | |
4014 | ||
4015 | /* Mount, Automount */ | |
4016 | const char *where; | |
4017 | ||
4018 | /* Swap */ | |
4019 | const char *what; | |
582a507f | 4020 | |
934277fe LP |
4021 | /* CGroup */ |
4022 | uint64_t memory_current; | |
48422635 | 4023 | uint64_t memory_min; |
da4d897e TH |
4024 | uint64_t memory_low; |
4025 | uint64_t memory_high; | |
4026 | uint64_t memory_max; | |
96e131ea | 4027 | uint64_t memory_swap_max; |
934277fe | 4028 | uint64_t memory_limit; |
5ad096b3 | 4029 | uint64_t cpu_usage_nsec; |
03a7b521 LP |
4030 | uint64_t tasks_current; |
4031 | uint64_t tasks_max; | |
0e97c93f DM |
4032 | uint64_t ip_ingress_bytes; |
4033 | uint64_t ip_egress_bytes; | |
7a5de3f9 LP |
4034 | uint64_t io_read_bytes; |
4035 | uint64_t io_write_bytes; | |
0e97c93f | 4036 | |
7ad5439e | 4037 | uint64_t default_memory_min; |
c52db42b CD |
4038 | uint64_t default_memory_low; |
4039 | ||
582a507f | 4040 | LIST_HEAD(ExecStatusInfo, exec); |
61cbdc4b LP |
4041 | } UnitStatusInfo; |
4042 | ||
a7335518 ZJS |
4043 | static void unit_status_info_free(UnitStatusInfo *info) { |
4044 | ExecStatusInfo *p; | |
4045 | UnitCondition *c; | |
4046 | ||
4047 | strv_free(info->documentation); | |
4048 | strv_free(info->dropin_paths); | |
965c5d1d KK |
4049 | strv_free(info->triggered_by); |
4050 | strv_free(info->triggers); | |
a7335518 ZJS |
4051 | strv_free(info->listen); |
4052 | ||
4053 | while ((c = info->conditions)) { | |
4054 | LIST_REMOVE(conditions, info->conditions, c); | |
4055 | unit_condition_free(c); | |
4056 | } | |
4057 | ||
4058 | while ((p = info->exec)) { | |
4059 | LIST_REMOVE(exec, info->exec, p); | |
4060 | exec_status_info_free(p); | |
4061 | } | |
4062 | } | |
4063 | ||
965c5d1d KK |
4064 | static void format_active_state(const char *active_state, const char **active_on, const char **active_off) { |
4065 | if (streq_ptr(active_state, "failed")) { | |
4066 | *active_on = ansi_highlight_red(); | |
4067 | *active_off = ansi_normal(); | |
4068 | } else if (STRPTR_IN_SET(active_state, "active", "reloading")) { | |
4069 | *active_on = ansi_highlight_green(); | |
4070 | *active_off = ansi_normal(); | |
4071 | } else | |
4072 | *active_on = *active_off = ""; | |
4073 | } | |
4074 | ||
f459b602 | 4075 | static void print_status_info( |
291d565a | 4076 | sd_bus *bus, |
f459b602 MAP |
4077 | UnitStatusInfo *i, |
4078 | bool *ellipsized) { | |
4079 | ||
4d9685be | 4080 | char since1[FORMAT_TIMESTAMP_RELATIVE_MAX], since2[FORMAT_TIMESTAMP_MAX]; |
d9e45bc3 | 4081 | const char *s1, *s2, *active_on, *active_off, *on, *off, *ss, *fs; |
c7080257 LP |
4082 | _cleanup_free_ char *formatted_path = NULL; |
4083 | ExecStatusInfo *p; | |
584be568 | 4084 | usec_t timestamp; |
1b64d026 | 4085 | const char *path; |
13160134 | 4086 | char **t, **t2; |
291d565a | 4087 | int r; |
582a507f | 4088 | |
61cbdc4b LP |
4089 | assert(i); |
4090 | ||
4091 | /* This shows pretty information about a unit. See | |
4092 | * print_property() for a low-level property printer */ | |
4093 | ||
965c5d1d | 4094 | format_active_state(i->active_state, &active_on, &active_off); |
b0d14c69 | 4095 | |
9a6f746f | 4096 | printf("%s%s%s %s", active_on, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE), active_off, strna(i->id)); |
61cbdc4b LP |
4097 | |
4098 | if (i->description && !streq_ptr(i->id, i->description)) | |
4099 | printf(" - %s", i->description); | |
4100 | ||
4101 | printf("\n"); | |
4102 | ||
4a9e2fff | 4103 | if (i->following) |
856323c9 | 4104 | printf(" Follow: unit currently follows state of %s\n", i->following); |
4a9e2fff | 4105 | |
c4555ad8 | 4106 | if (STRPTR_IN_SET(i->load_state, "error", "not-found", "bad-setting")) { |
0b5a519c | 4107 | on = ansi_highlight_red(); |
1fc464f6 | 4108 | off = ansi_normal(); |
c31b4423 LP |
4109 | } else |
4110 | on = off = ""; | |
4111 | ||
c7080257 LP |
4112 | path = i->source_path ?: i->fragment_path; |
4113 | if (path && terminal_urlify_path(path, NULL, &formatted_path) >= 0) | |
4114 | path = formatted_path; | |
1b64d026 | 4115 | |
9a0abfa8 | 4116 | if (!isempty(i->load_error)) |
0d588dea | 4117 | printf(" Loaded: %s%s%s (Reason: %s)\n", |
856323c9 | 4118 | on, strna(i->load_state), off, i->load_error); |
31b8895a ZJS |
4119 | else if (path && !isempty(i->unit_file_state)) { |
4120 | bool show_preset = !isempty(i->unit_file_preset) && | |
4121 | show_preset_for_state(unit_file_state_from_string(i->unit_file_state)); | |
4122 | ||
4123 | printf(" Loaded: %s%s%s (%s; %s%s%s)\n", | |
4124 | on, strna(i->load_state), off, | |
4125 | path, | |
4126 | i->unit_file_state, | |
4127 | show_preset ? "; vendor preset: " : "", | |
4128 | show_preset ? i->unit_file_preset : ""); | |
4129 | ||
4130 | } else if (path) | |
0d588dea | 4131 | printf(" Loaded: %s%s%s (%s)\n", |
856323c9 | 4132 | on, strna(i->load_state), off, path); |
61cbdc4b | 4133 | else |
0d588dea | 4134 | printf(" Loaded: %s%s%s\n", |
856323c9 | 4135 | on, strna(i->load_state), off); |
61cbdc4b | 4136 | |
055ef36b | 4137 | if (i->transient) |
0d588dea | 4138 | printf(" Transient: yes\n"); |
055ef36b | 4139 | |
76d14b87 | 4140 | if (!strv_isempty(i->dropin_paths)) { |
f459b602 | 4141 | _cleanup_free_ char *dir = NULL; |
76d14b87 | 4142 | bool last = false; |
f459b602 | 4143 | char ** dropin; |
76d14b87 OS |
4144 | |
4145 | STRV_FOREACH(dropin, i->dropin_paths) { | |
c7080257 LP |
4146 | _cleanup_free_ char *dropin_formatted = NULL; |
4147 | const char *df; | |
4148 | ||
eb2c3192 | 4149 | if (!dir || last) { |
0d588dea KK |
4150 | printf(dir ? " " : |
4151 | " Drop-In: "); | |
76d14b87 | 4152 | |
97b11eed | 4153 | dir = mfree(dir); |
76d14b87 | 4154 | |
5f311f8c LP |
4155 | dir = dirname_malloc(*dropin); |
4156 | if (!dir) { | |
76d14b87 OS |
4157 | log_oom(); |
4158 | return; | |
4159 | } | |
4160 | ||
eb2c3192 | 4161 | printf("%s\n" |
0d588dea | 4162 | " %s", dir, |
9a6f746f | 4163 | special_glyph(SPECIAL_GLYPH_TREE_RIGHT)); |
76d14b87 OS |
4164 | } |
4165 | ||
4166 | last = ! (*(dropin + 1) && startswith(*(dropin + 1), dir)); | |
4167 | ||
c7080257 LP |
4168 | if (terminal_urlify_path(*dropin, basename(*dropin), &dropin_formatted) >= 0) |
4169 | df = dropin_formatted; | |
4170 | else | |
4171 | df = *dropin; | |
4172 | ||
4173 | printf("%s%s", df, last ? "\n" : ", "); | |
76d14b87 | 4174 | } |
76d14b87 OS |
4175 | } |
4176 | ||
2ee68f72 | 4177 | ss = streq_ptr(i->active_state, i->sub_state) ? NULL : i->sub_state; |
2ee68f72 | 4178 | if (ss) |
266dd555 | 4179 | printf(" Active: %s%s (%s)%s", |
b0d14c69 | 4180 | active_on, strna(i->active_state), ss, active_off); |
2ee68f72 | 4181 | else |
0d588dea | 4182 | printf(" Active: %s%s%s", |
b0d14c69 | 4183 | active_on, strna(i->active_state), active_off); |
61cbdc4b | 4184 | |
d9e45bc3 MS |
4185 | fs = !isempty(i->freezer_state) && !streq(i->freezer_state, "running") ? i->freezer_state : NULL; |
4186 | if (fs) | |
38fee619 | 4187 | printf(" %s(%s)%s", ansi_highlight_yellow(), fs, ansi_normal()); |
d9e45bc3 | 4188 | |
f42806df LP |
4189 | if (!isempty(i->result) && !streq(i->result, "success")) |
4190 | printf(" (Result: %s)", i->result); | |
4191 | ||
1cf03a4f ZJS |
4192 | timestamp = STRPTR_IN_SET(i->active_state, "active", "reloading") ? i->active_enter_timestamp : |
4193 | STRPTR_IN_SET(i->active_state, "inactive", "failed") ? i->inactive_enter_timestamp : | |
4194 | STRPTR_IN_SET(i->active_state, "activating") ? i->inactive_exit_timestamp : | |
4195 | i->active_exit_timestamp; | |
584be568 | 4196 | |
bbb8486e | 4197 | s1 = format_timestamp_relative(since1, sizeof(since1), timestamp); |
584be568 LP |
4198 | s2 = format_timestamp(since2, sizeof(since2), timestamp); |
4199 | ||
4200 | if (s1) | |
538da63d | 4201 | printf(" since %s; %s\n", s2, s1); |
584be568 | 4202 | else if (s2) |
538da63d | 4203 | printf(" since %s\n", s2); |
584be568 LP |
4204 | else |
4205 | printf("\n"); | |
4206 | ||
bc9aa96b ZJS |
4207 | STRV_FOREACH(t, i->triggered_by) { |
4208 | UnitActiveState state = _UNIT_ACTIVE_STATE_INVALID; | |
965c5d1d | 4209 | |
bc9aa96b ZJS |
4210 | (void) get_state_one_unit(bus, *t, &state); |
4211 | format_active_state(unit_active_state_to_string(state), &on, &off); | |
965c5d1d | 4212 | |
bc9aa96b ZJS |
4213 | printf("%s %s%s%s %s\n", |
4214 | t == i->triggered_by ? "TriggeredBy:" : " ", | |
4215 | on, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE), off, | |
4216 | *t); | |
965c5d1d KK |
4217 | } |
4218 | ||
601b0842 GS |
4219 | if (endswith(i->id, ".timer")) { |
4220 | char tstamp1[FORMAT_TIMESTAMP_RELATIVE_MAX], | |
4221 | tstamp2[FORMAT_TIMESTAMP_MAX]; | |
4d9685be | 4222 | const char *next_rel_time, *next_time; |
601b0842 GS |
4223 | dual_timestamp nw, next = {i->next_elapse_real, |
4224 | i->next_elapse_monotonic}; | |
4225 | usec_t next_elapse; | |
4226 | ||
0d588dea | 4227 | printf(" Trigger: "); |
601b0842 GS |
4228 | |
4229 | dual_timestamp_get(&nw); | |
4230 | next_elapse = calc_next_elapse(&nw, &next); | |
4d9685be ZJS |
4231 | next_rel_time = format_timestamp_relative(tstamp1, sizeof tstamp1, next_elapse); |
4232 | next_time = format_timestamp(tstamp2, sizeof tstamp2, next_elapse); | |
601b0842 GS |
4233 | |
4234 | if (next_time && next_rel_time) | |
4235 | printf("%s; %s\n", next_time, next_rel_time); | |
4236 | else | |
4237 | printf("n/a\n"); | |
4238 | } | |
4239 | ||
bc9aa96b ZJS |
4240 | STRV_FOREACH(t, i->triggers) { |
4241 | UnitActiveState state = _UNIT_ACTIVE_STATE_INVALID; | |
965c5d1d | 4242 | |
bc9aa96b ZJS |
4243 | (void) get_state_one_unit(bus, *t, &state); |
4244 | format_active_state(unit_active_state_to_string(state), &on, &off); | |
965c5d1d | 4245 | |
bc9aa96b ZJS |
4246 | printf("%s %s%s%s %s\n", |
4247 | t == i->triggers ? " Triggers:" : " ", | |
4248 | on, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE), off, | |
4249 | *t); | |
965c5d1d KK |
4250 | } |
4251 | ||
90bbc946 | 4252 | if (!i->condition_result && i->condition_timestamp > 0) { |
d5db7fe6 WC |
4253 | UnitCondition *c; |
4254 | int n = 0; | |
4255 | ||
bbb8486e | 4256 | s1 = format_timestamp_relative(since1, sizeof(since1), i->condition_timestamp); |
90bbc946 LP |
4257 | s2 = format_timestamp(since2, sizeof(since2), i->condition_timestamp); |
4258 | ||
0d588dea | 4259 | printf(" Condition: start %scondition failed%s at %s%s%s\n", |
1fc464f6 | 4260 | ansi_highlight_yellow(), ansi_normal(), |
5cfee414 | 4261 | s2, s1 ? "; " : "", strempty(s1)); |
d5db7fe6 | 4262 | |
b1ed76ae | 4263 | LIST_FOREACH(conditions, c, i->conditions) |
d5db7fe6 WC |
4264 | if (c->tristate < 0) |
4265 | n++; | |
d5db7fe6 | 4266 | |
b1ed76ae ZJS |
4267 | LIST_FOREACH(conditions, c, i->conditions) |
4268 | if (c->tristate < 0) | |
0d588dea | 4269 | printf(" %s %s=%s%s%s was not met\n", |
9a6f746f | 4270 | --n ? special_glyph(SPECIAL_GLYPH_TREE_BRANCH) : special_glyph(SPECIAL_GLYPH_TREE_RIGHT), |
b1ed76ae ZJS |
4271 | c->name, |
4272 | c->trigger ? "|" : "", | |
4273 | c->negate ? "!" : "", | |
4274 | c->param); | |
59fccdc5 LP |
4275 | } |
4276 | ||
4277 | if (!i->assert_result && i->assert_timestamp > 0) { | |
4278 | s1 = format_timestamp_relative(since1, sizeof(since1), i->assert_timestamp); | |
4279 | s2 = format_timestamp(since2, sizeof(since2), i->assert_timestamp); | |
4280 | ||
0d588dea | 4281 | printf(" Assert: start %sassertion failed%s at %s%s%s\n", |
1fc464f6 | 4282 | ansi_highlight_red(), ansi_normal(), |
5cfee414 | 4283 | s2, s1 ? "; " : "", strempty(s1)); |
59fccdc5 | 4284 | if (i->failed_assert_trigger) |
0d588dea | 4285 | printf(" none of the trigger assertions were met\n"); |
59fccdc5 | 4286 | else if (i->failed_assert) |
0d588dea | 4287 | printf(" %s=%s%s was not met\n", |
59fccdc5 LP |
4288 | i->failed_assert, |
4289 | i->failed_assert_negate ? "!" : "", | |
4290 | i->failed_assert_parameter); | |
90bbc946 LP |
4291 | } |
4292 | ||
61cbdc4b | 4293 | if (i->sysfs_path) |
0d588dea | 4294 | printf(" Device: %s\n", i->sysfs_path); |
9feeba4b | 4295 | if (i->where) |
0d588dea | 4296 | printf(" Where: %s\n", i->where); |
9feeba4b | 4297 | if (i->what) |
0d588dea | 4298 | printf(" What: %s\n", i->what); |
49dbfa7b | 4299 | |
78c7d20e LP |
4300 | STRV_FOREACH(t, i->documentation) { |
4301 | _cleanup_free_ char *formatted = NULL; | |
4302 | const char *q; | |
4303 | ||
4304 | if (terminal_urlify(*t, NULL, &formatted) >= 0) | |
4305 | q = formatted; | |
4306 | else | |
4307 | q = *t; | |
4308 | ||
0d588dea | 4309 | printf(" %*s %s\n", 9, t == i->documentation ? "Docs:" : "", q); |
78c7d20e | 4310 | } |
49dbfa7b | 4311 | |
13160134 | 4312 | STRV_FOREACH_PAIR(t, t2, i->listen) |
0d588dea | 4313 | printf(" %*s %s (%s)\n", 9, t == i->listen ? "Listen:" : "", *t2, *t); |
67419600 | 4314 | |
a98f7575 | 4315 | if (i->accept) { |
0d588dea | 4316 | printf(" Accepted: %u; Connected: %u;", i->n_accepted, i->n_connections); |
a98f7575 | 4317 | if (i->n_refused) |
0d588dea | 4318 | printf(" Refused: %u", i->n_refused); |
a98f7575 MM |
4319 | printf("\n"); |
4320 | } | |
4321 | ||
582a507f | 4322 | LIST_FOREACH(exec, p, i->exec) { |
13160134 | 4323 | _cleanup_free_ char *argv = NULL; |
9a57c629 | 4324 | bool good; |
582a507f LP |
4325 | |
4326 | /* Only show exited processes here */ | |
4327 | if (p->code == 0) | |
4328 | continue; | |
4329 | ||
e5c85244 AZ |
4330 | /* Don't print ExecXYZEx= properties here since it will appear as a |
4331 | * duplicate of the non-Ex= variant. */ | |
4332 | if (endswith(p->name, "Ex")) | |
4333 | continue; | |
4334 | ||
13160134 | 4335 | argv = strv_join(p->argv, " "); |
0d588dea | 4336 | printf(" Process: "PID_FMT" %s=%s ", p->pid, p->name, strna(argv)); |
582a507f | 4337 | |
1f0958f6 | 4338 | good = is_clean_exit(p->code, p->status, EXIT_CLEAN_DAEMON, NULL); |
9a57c629 | 4339 | if (!good) { |
0b5a519c | 4340 | on = ansi_highlight_red(); |
1fc464f6 | 4341 | off = ansi_normal(); |
9a57c629 LP |
4342 | } else |
4343 | on = off = ""; | |
4344 | ||
4345 | printf("%s(code=%s, ", on, sigchld_code_to_string(p->code)); | |
4346 | ||
d06dacd0 LP |
4347 | if (p->code == CLD_EXITED) { |
4348 | const char *c; | |
4349 | ||
582a507f | 4350 | printf("status=%i", p->status); |
d06dacd0 | 4351 | |
e04ed6db | 4352 | c = exit_status_to_string(p->status, EXIT_STATUS_LIBC | EXIT_STATUS_SYSTEMD); |
1b64d026 | 4353 | if (c) |
d06dacd0 LP |
4354 | printf("/%s", c); |
4355 | ||
4356 | } else | |
582a507f | 4357 | printf("signal=%s", signal_to_string(p->status)); |
9a57c629 LP |
4358 | |
4359 | printf(")%s\n", off); | |
4360 | ||
582a507f LP |
4361 | if (i->main_pid == p->pid && |
4362 | i->start_timestamp == p->start_timestamp && | |
4363 | i->exit_timestamp == p->start_timestamp) | |
4364 | /* Let's not show this twice */ | |
4365 | i->main_pid = 0; | |
4366 | ||
4367 | if (p->pid == i->control_pid) | |
4368 | i->control_pid = 0; | |
4369 | } | |
4370 | ||
61cbdc4b | 4371 | if (i->main_pid > 0 || i->control_pid > 0) { |
61cbdc4b | 4372 | if (i->main_pid > 0) { |
0d588dea | 4373 | printf(" Main PID: "PID_FMT, i->main_pid); |
61cbdc4b LP |
4374 | |
4375 | if (i->running) { | |
a081b9ce LP |
4376 | |
4377 | if (arg_transport == BUS_TRANSPORT_LOCAL) { | |
4378 | _cleanup_free_ char *comm = NULL; | |
4379 | ||
4380 | (void) get_process_comm(i->main_pid, &comm); | |
4381 | if (comm) | |
4382 | printf(" (%s)", comm); | |
4383 | } | |
4384 | ||
6d4fc029 | 4385 | } else if (i->exit_code > 0) { |
61cbdc4b LP |
4386 | printf(" (code=%s, ", sigchld_code_to_string(i->exit_code)); |
4387 | ||
d06dacd0 LP |
4388 | if (i->exit_code == CLD_EXITED) { |
4389 | const char *c; | |
4390 | ||
61cbdc4b | 4391 | printf("status=%i", i->exit_status); |
d06dacd0 | 4392 | |
e1714f02 | 4393 | c = exit_status_to_string(i->exit_status, |
e04ed6db | 4394 | EXIT_STATUS_LIBC | EXIT_STATUS_SYSTEMD); |
1b64d026 | 4395 | if (c) |
d06dacd0 LP |
4396 | printf("/%s", c); |
4397 | ||
4398 | } else | |
582a507f | 4399 | printf("signal=%s", signal_to_string(i->exit_status)); |
6d4fc029 LP |
4400 | printf(")"); |
4401 | } | |
13160134 | 4402 | } |
61cbdc4b LP |
4403 | |
4404 | if (i->control_pid > 0) { | |
13160134 | 4405 | _cleanup_free_ char *c = NULL; |
61cbdc4b | 4406 | |
1089dcd4 LP |
4407 | if (i->main_pid > 0) |
4408 | fputs("; Control PID: ", stdout); | |
4409 | else | |
4410 | fputs("Cntrl PID: ", stdout); /* if first in column, abbreviated so it fits alignment */ | |
4411 | ||
4412 | printf(PID_FMT, i->control_pid); | |
61cbdc4b | 4413 | |
a081b9ce LP |
4414 | if (arg_transport == BUS_TRANSPORT_LOCAL) { |
4415 | (void) get_process_comm(i->control_pid, &c); | |
4416 | if (c) | |
4417 | printf(" (%s)", c); | |
4418 | } | |
61cbdc4b LP |
4419 | } |
4420 | ||
4421 | printf("\n"); | |
4422 | } | |
4423 | ||
17bb7382 | 4424 | if (i->status_text) |
0d588dea | 4425 | printf(" Status: \"%s\"\n", i->status_text); |
b4af5a80 | 4426 | if (i->status_errno > 0) |
0d588dea | 4427 | printf(" Error: %i (%s)\n", i->status_errno, strerror_safe(i->status_errno)); |
17bb7382 | 4428 | |
0e97c93f DM |
4429 | if (i->ip_ingress_bytes != (uint64_t) -1 && i->ip_egress_bytes != (uint64_t) -1) { |
4430 | char buf_in[FORMAT_BYTES_MAX], buf_out[FORMAT_BYTES_MAX]; | |
4431 | ||
0d588dea | 4432 | printf(" IP: %s in, %s out\n", |
0e97c93f DM |
4433 | format_bytes(buf_in, sizeof(buf_in), i->ip_ingress_bytes), |
4434 | format_bytes(buf_out, sizeof(buf_out), i->ip_egress_bytes)); | |
4435 | } | |
4436 | ||
7a5de3f9 LP |
4437 | if (i->io_read_bytes != UINT64_MAX && i->io_write_bytes != UINT64_MAX) { |
4438 | char buf_in[FORMAT_BYTES_MAX], buf_out[FORMAT_BYTES_MAX]; | |
4439 | ||
0d588dea | 4440 | printf(" IO: %s read, %s written\n", |
7a5de3f9 LP |
4441 | format_bytes(buf_in, sizeof(buf_in), i->io_read_bytes), |
4442 | format_bytes(buf_out, sizeof(buf_out), i->io_write_bytes)); | |
4443 | } | |
4444 | ||
03a7b521 | 4445 | if (i->tasks_current != (uint64_t) -1) { |
0d588dea | 4446 | printf(" Tasks: %" PRIu64, i->tasks_current); |
03a7b521 LP |
4447 | |
4448 | if (i->tasks_max != (uint64_t) -1) | |
2a6736dd | 4449 | printf(" (limit: %" PRIu64 ")\n", i->tasks_max); |
03a7b521 LP |
4450 | else |
4451 | printf("\n"); | |
4452 | } | |
4453 | ||
934277fe LP |
4454 | if (i->memory_current != (uint64_t) -1) { |
4455 | char buf[FORMAT_BYTES_MAX]; | |
4456 | ||
0d588dea | 4457 | printf(" Memory: %s", format_bytes(buf, sizeof(buf), i->memory_current)); |
934277fe | 4458 | |
48422635 TH |
4459 | if (i->memory_min > 0 || i->memory_low > 0 || |
4460 | i->memory_high != CGROUP_LIMIT_MAX || i->memory_max != CGROUP_LIMIT_MAX || | |
4461 | i->memory_swap_max != CGROUP_LIMIT_MAX || | |
da4d897e TH |
4462 | i->memory_limit != CGROUP_LIMIT_MAX) { |
4463 | const char *prefix = ""; | |
4464 | ||
4465 | printf(" ("); | |
48422635 | 4466 | if (i->memory_min > 0) { |
e0e2112f | 4467 | printf("%smin: %s", prefix, format_bytes_cgroup_protection(buf, sizeof(buf), i->memory_min)); |
48422635 TH |
4468 | prefix = " "; |
4469 | } | |
da4d897e | 4470 | if (i->memory_low > 0) { |
e0e2112f | 4471 | printf("%slow: %s", prefix, format_bytes_cgroup_protection(buf, sizeof(buf), i->memory_low)); |
da4d897e TH |
4472 | prefix = " "; |
4473 | } | |
4474 | if (i->memory_high != CGROUP_LIMIT_MAX) { | |
4475 | printf("%shigh: %s", prefix, format_bytes(buf, sizeof(buf), i->memory_high)); | |
4476 | prefix = " "; | |
4477 | } | |
4478 | if (i->memory_max != CGROUP_LIMIT_MAX) { | |
4479 | printf("%smax: %s", prefix, format_bytes(buf, sizeof(buf), i->memory_max)); | |
4480 | prefix = " "; | |
4481 | } | |
96e131ea WC |
4482 | if (i->memory_swap_max != CGROUP_LIMIT_MAX) { |
4483 | printf("%sswap max: %s", prefix, format_bytes(buf, sizeof(buf), i->memory_swap_max)); | |
4484 | prefix = " "; | |
4485 | } | |
da4d897e TH |
4486 | if (i->memory_limit != CGROUP_LIMIT_MAX) { |
4487 | printf("%slimit: %s", prefix, format_bytes(buf, sizeof(buf), i->memory_limit)); | |
4488 | prefix = " "; | |
4489 | } | |
4490 | printf(")"); | |
4491 | } | |
4492 | printf("\n"); | |
934277fe LP |
4493 | } |
4494 | ||
5ad096b3 LP |
4495 | if (i->cpu_usage_nsec != (uint64_t) -1) { |
4496 | char buf[FORMAT_TIMESPAN_MAX]; | |
0d588dea | 4497 | printf(" CPU: %s\n", format_timespan(buf, sizeof(buf), i->cpu_usage_nsec / NSEC_PER_USEC, USEC_PER_MSEC)); |
5ad096b3 LP |
4498 | } |
4499 | ||
51e22f88 | 4500 | if (i->control_group) { |
291d565a | 4501 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
a434236a | 4502 | static const char prefix[] = " "; |
ab35fb1b LP |
4503 | unsigned c; |
4504 | ||
0d588dea | 4505 | printf(" CGroup: %s\n", i->control_group); |
51e22f88 | 4506 | |
291d565a LP |
4507 | c = columns(); |
4508 | if (c > sizeof(prefix) - 1) | |
4509 | c -= sizeof(prefix) - 1; | |
4510 | else | |
4511 | c = 0; | |
ab35fb1b | 4512 | |
291d565a LP |
4513 | r = unit_show_processes(bus, i->id, i->control_group, prefix, c, get_output_flags(), &error); |
4514 | if (r == -EBADR) { | |
b69d29ce LP |
4515 | unsigned k = 0; |
4516 | pid_t extra[2]; | |
4517 | ||
291d565a | 4518 | /* Fallback for older systemd versions where the GetUnitProcesses() call is not yet available */ |
ab35fb1b | 4519 | |
b69d29ce LP |
4520 | if (i->main_pid > 0) |
4521 | extra[k++] = i->main_pid; | |
4522 | ||
4523 | if (i->control_pid > 0) | |
4524 | extra[k++] = i->control_pid; | |
4525 | ||
0ff308c8 | 4526 | show_cgroup_and_extra(SYSTEMD_CGROUP_CONTROLLER, i->control_group, prefix, c, extra, k, get_output_flags()); |
291d565a | 4527 | } else if (r < 0) |
107c2051 ZJS |
4528 | log_warning_errno(r, "Failed to dump process list for '%s', ignoring: %s", |
4529 | i->id, bus_error_message(&error, r)); | |
c59760ee | 4530 | } |
45fb0699 | 4531 | |
ece174c5 | 4532 | if (i->id && arg_transport == BUS_TRANSPORT_LOCAL) |
3c756001 LP |
4533 | show_journal_by_unit( |
4534 | stdout, | |
4535 | i->id, | |
d93dda3a | 4536 | i->log_namespace, |
3c756001 LP |
4537 | arg_output, |
4538 | 0, | |
4539 | i->inactive_exit_timestamp_monotonic, | |
4540 | arg_lines, | |
4541 | getuid(), | |
4542 | get_output_flags() | OUTPUT_BEGIN_NEWLINE, | |
4543 | SD_JOURNAL_LOCAL_ONLY, | |
4544 | arg_scope == UNIT_FILE_SYSTEM, | |
4545 | ellipsized); | |
86aa7ba4 | 4546 | |
45fb0699 | 4547 | if (i->need_daemon_reload) |
3f36991e | 4548 | warn_unit_file_changed(i->id); |
61cbdc4b LP |
4549 | } |
4550 | ||
b43f208f | 4551 | static void show_unit_help(UnitStatusInfo *i) { |
256425cc LP |
4552 | char **p; |
4553 | ||
4554 | assert(i); | |
4555 | ||
4556 | if (!i->documentation) { | |
4557 | log_info("Documentation for %s not known.", i->id); | |
4558 | return; | |
4559 | } | |
4560 | ||
78002a67 ZJS |
4561 | STRV_FOREACH(p, i->documentation) |
4562 | if (startswith(*p, "man:")) | |
4563 | show_man_page(*p + 4, false); | |
4564 | else | |
0315fe37 | 4565 | log_info("Can't show: %s", *p); |
256425cc LP |
4566 | } |
4567 | ||
4679a8c3 YW |
4568 | static int map_main_pid(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { |
4569 | UnitStatusInfo *i = userdata; | |
4570 | uint32_t u; | |
f459b602 | 4571 | int r; |
61cbdc4b | 4572 | |
4679a8c3 YW |
4573 | r = sd_bus_message_read(m, "u", &u); |
4574 | if (r < 0) | |
4575 | return r; | |
61cbdc4b | 4576 | |
4679a8c3 YW |
4577 | i->main_pid = (pid_t) u; |
4578 | i->running = u > 0; | |
b8131a87 | 4579 | |
4679a8c3 YW |
4580 | return 0; |
4581 | } | |
b8131a87 | 4582 | |
4679a8c3 | 4583 | static int map_load_error(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { |
31d99bd1 | 4584 | const char *message, **p = userdata; |
4679a8c3 | 4585 | int r; |
b8131a87 | 4586 | |
31d99bd1 | 4587 | r = sd_bus_message_read(m, "(ss)", NULL, &message); |
4679a8c3 YW |
4588 | if (r < 0) |
4589 | return r; | |
b8131a87 | 4590 | |
4679a8c3 YW |
4591 | if (!isempty(message)) |
4592 | *p = message; | |
61cbdc4b | 4593 | |
4679a8c3 YW |
4594 | return 0; |
4595 | } | |
61cbdc4b | 4596 | |
4679a8c3 YW |
4597 | static int map_listen(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { |
4598 | const char *type, *path; | |
4599 | char ***p = userdata; | |
4600 | int r; | |
61cbdc4b | 4601 | |
4679a8c3 YW |
4602 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(ss)"); |
4603 | if (r < 0) | |
4604 | return r; | |
61cbdc4b | 4605 | |
4679a8c3 | 4606 | while ((r = sd_bus_message_read(m, "(ss)", &type, &path)) > 0) { |
61cbdc4b | 4607 | |
4679a8c3 | 4608 | r = strv_extend(p, type); |
f459b602 | 4609 | if (r < 0) |
4679a8c3 | 4610 | return r; |
61cbdc4b | 4611 | |
4679a8c3 | 4612 | r = strv_extend(p, path); |
f459b602 | 4613 | if (r < 0) |
4679a8c3 | 4614 | return r; |
61cbdc4b | 4615 | } |
4679a8c3 YW |
4616 | if (r < 0) |
4617 | return r; | |
582a507f | 4618 | |
4679a8c3 YW |
4619 | r = sd_bus_message_exit_container(m); |
4620 | if (r < 0) | |
4621 | return r; | |
49dbfa7b | 4622 | |
4679a8c3 YW |
4623 | return 0; |
4624 | } | |
52990c2e | 4625 | |
4679a8c3 YW |
4626 | static int map_conditions(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { |
4627 | UnitStatusInfo *i = userdata; | |
4628 | const char *cond, *param; | |
4629 | int trigger, negate; | |
4630 | int32_t state; | |
4631 | int r; | |
52990c2e | 4632 | |
4679a8c3 YW |
4633 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(sbbsi)"); |
4634 | if (r < 0) | |
4635 | return r; | |
f459b602 | 4636 | |
4679a8c3 YW |
4637 | while ((r = sd_bus_message_read(m, "(sbbsi)", &cond, &trigger, &negate, ¶m, &state)) > 0) { |
4638 | _cleanup_(unit_condition_freep) UnitCondition *c = NULL; | |
d5db7fe6 | 4639 | |
3bfa8668 | 4640 | c = new(UnitCondition, 1); |
4679a8c3 YW |
4641 | if (!c) |
4642 | return -ENOMEM; | |
d5db7fe6 | 4643 | |
3bfa8668 LP |
4644 | *c = (UnitCondition) { |
4645 | .name = strdup(cond), | |
4646 | .param = strdup(param), | |
4647 | .trigger = trigger, | |
4648 | .negate = negate, | |
4649 | .tristate = state, | |
4650 | }; | |
4651 | ||
4679a8c3 YW |
4652 | if (!c->name || !c->param) |
4653 | return -ENOMEM; | |
d5db7fe6 | 4654 | |
3bfa8668 | 4655 | LIST_PREPEND(conditions, i->conditions, TAKE_PTR(c)); |
4679a8c3 YW |
4656 | } |
4657 | if (r < 0) | |
4658 | return r; | |
d5db7fe6 | 4659 | |
4679a8c3 YW |
4660 | r = sd_bus_message_exit_container(m); |
4661 | if (r < 0) | |
4662 | return r; | |
59fccdc5 | 4663 | |
4679a8c3 YW |
4664 | return 0; |
4665 | } | |
59fccdc5 | 4666 | |
4679a8c3 YW |
4667 | static int map_asserts(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { |
4668 | UnitStatusInfo *i = userdata; | |
4669 | const char *cond, *param; | |
4670 | int trigger, negate; | |
4671 | int32_t state; | |
4672 | int r; | |
59fccdc5 | 4673 | |
4679a8c3 YW |
4674 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(sbbsi)"); |
4675 | if (r < 0) | |
4676 | return r; | |
59fccdc5 | 4677 | |
4679a8c3 YW |
4678 | while ((r = sd_bus_message_read(m, "(sbbsi)", &cond, &trigger, &negate, ¶m, &state)) > 0) { |
4679 | if (state < 0 && (!trigger || !i->failed_assert)) { | |
4680 | i->failed_assert = cond; | |
4681 | i->failed_assert_trigger = trigger; | |
4682 | i->failed_assert_negate = negate; | |
4683 | i->failed_assert_parameter = param; | |
4684 | } | |
4685 | } | |
4686 | if (r < 0) | |
4687 | return r; | |
f459b602 | 4688 | |
4679a8c3 YW |
4689 | r = sd_bus_message_exit_container(m); |
4690 | if (r < 0) | |
4691 | return r; | |
f459b602 | 4692 | |
4679a8c3 YW |
4693 | return 0; |
4694 | } | |
582a507f | 4695 | |
4679a8c3 YW |
4696 | static int map_exec(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { |
4697 | _cleanup_free_ ExecStatusInfo *info = NULL; | |
3e14d36a | 4698 | ExecStatusInfo *last; |
4679a8c3 | 4699 | UnitStatusInfo *i = userdata; |
b3d59367 | 4700 | bool is_ex_prop = endswith(member, "Ex"); |
4679a8c3 | 4701 | int r; |
9f39404c | 4702 | |
b3d59367 | 4703 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, is_ex_prop ? "(sasasttttuii)" : "(sasbttttuii)"); |
4679a8c3 YW |
4704 | if (r < 0) |
4705 | return r; | |
9f39404c | 4706 | |
4679a8c3 YW |
4707 | info = new0(ExecStatusInfo, 1); |
4708 | if (!info) | |
4709 | return -ENOMEM; | |
9f39404c | 4710 | |
3e14d36a LP |
4711 | LIST_FIND_TAIL(exec, i->exec, last); |
4712 | ||
b3d59367 | 4713 | while ((r = exec_status_info_deserialize(m, info, is_ex_prop)) > 0) { |
9f39404c | 4714 | |
4679a8c3 YW |
4715 | info->name = strdup(member); |
4716 | if (!info->name) | |
4717 | return -ENOMEM; | |
9f39404c | 4718 | |
3e14d36a LP |
4719 | LIST_INSERT_AFTER(exec, i->exec, last, info); |
4720 | last = info; | |
f459b602 | 4721 | |
4679a8c3 YW |
4722 | info = new0(ExecStatusInfo, 1); |
4723 | if (!info) | |
4724 | return -ENOMEM; | |
9f39404c | 4725 | } |
4679a8c3 YW |
4726 | if (r < 0) |
4727 | return r; | |
f459b602 | 4728 | |
4679a8c3 | 4729 | r = sd_bus_message_exit_container(m); |
f459b602 | 4730 | if (r < 0) |
4679a8c3 | 4731 | return r; |
61cbdc4b LP |
4732 | |
4733 | return 0; | |
4734 | } | |
4735 | ||
eda19357 | 4736 | static int print_property(const char *name, const char *expected_value, sd_bus_message *m, bool value, bool all) { |
07636114 YW |
4737 | char bus_type; |
4738 | const char *contents; | |
f459b602 MAP |
4739 | int r; |
4740 | ||
48220598 | 4741 | assert(name); |
f459b602 | 4742 | assert(m); |
48220598 | 4743 | |
61cbdc4b LP |
4744 | /* This is a low-level property printer, see |
4745 | * print_status_info() for the nicer output */ | |
4746 | ||
07636114 YW |
4747 | r = sd_bus_message_peek_type(m, &bus_type, &contents); |
4748 | if (r < 0) | |
852c1b4d | 4749 | return r; |
48220598 | 4750 | |
07636114 | 4751 | switch (bus_type) { |
48220598 | 4752 | |
a3ddd457 YW |
4753 | case SD_BUS_TYPE_INT32: |
4754 | if (endswith(name, "ActionExitStatus")) { | |
4755 | int32_t i; | |
4756 | ||
4757 | r = sd_bus_message_read_basic(m, bus_type, &i); | |
4758 | if (r < 0) | |
4759 | return r; | |
4760 | ||
4761 | if (i >= 0 && i <= 255) | |
4762 | bus_print_property_valuef(name, expected_value, value, "%"PRIi32, i); | |
4763 | else if (all) | |
4764 | bus_print_property_value(name, expected_value, value, "[not set]"); | |
4765 | ||
b070c7c0 MS |
4766 | return 1; |
4767 | } else if (streq(name, "NUMAPolicy")) { | |
4768 | int32_t i; | |
4769 | ||
4770 | r = sd_bus_message_read_basic(m, bus_type, &i); | |
4771 | if (r < 0) | |
4772 | return r; | |
4773 | ||
4774 | bus_print_property_valuef(name, expected_value, value, "%s", strna(mpol_to_string(i))); | |
4775 | ||
a3ddd457 YW |
4776 | return 1; |
4777 | } | |
4778 | break; | |
4779 | ||
07636114 | 4780 | case SD_BUS_TYPE_STRUCT: |
48220598 | 4781 | |
07636114 | 4782 | if (contents[0] == SD_BUS_TYPE_UINT32 && streq(name, "Job")) { |
48220598 LP |
4783 | uint32_t u; |
4784 | ||
f459b602 MAP |
4785 | r = sd_bus_message_read(m, "(uo)", &u, NULL); |
4786 | if (r < 0) | |
4787 | return bus_log_parse_error(r); | |
48220598 | 4788 | |
f459b602 | 4789 | if (u > 0) |
102b0214 | 4790 | bus_print_property_valuef(name, expected_value, value, "%"PRIu32, u); |
07636114 | 4791 | else if (all) |
102b0214 | 4792 | bus_print_property_value(name, expected_value, value, ""); |
48220598 | 4793 | |
07636114 | 4794 | return 1; |
f459b602 | 4795 | |
07636114 | 4796 | } else if (contents[0] == SD_BUS_TYPE_STRING && streq(name, "Unit")) { |
48220598 LP |
4797 | const char *s; |
4798 | ||
f459b602 MAP |
4799 | r = sd_bus_message_read(m, "(so)", &s, NULL); |
4800 | if (r < 0) | |
4801 | return bus_log_parse_error(r); | |
48220598 | 4802 | |
07636114 | 4803 | if (all || !isempty(s)) |
102b0214 | 4804 | bus_print_property_value(name, expected_value, value, s); |
48220598 | 4805 | |
07636114 | 4806 | return 1; |
f459b602 | 4807 | |
07636114 | 4808 | } else if (contents[0] == SD_BUS_TYPE_STRING && streq(name, "LoadError")) { |
9f39404c LP |
4809 | const char *a = NULL, *b = NULL; |
4810 | ||
f459b602 MAP |
4811 | r = sd_bus_message_read(m, "(ss)", &a, &b); |
4812 | if (r < 0) | |
4813 | return bus_log_parse_error(r); | |
9f39404c | 4814 | |
48d3358d | 4815 | if (!isempty(a) || !isempty(b)) |
102b0214 | 4816 | bus_print_property_valuef(name, expected_value, value, "%s \"%s\"", strempty(a), strempty(b)); |
48d3358d YW |
4817 | else if (all) |
4818 | bus_print_property_value(name, expected_value, value, ""); | |
f786e80d | 4819 | |
07636114 | 4820 | return 1; |
04749e40 YW |
4821 | |
4822 | } else if (STR_IN_SET(name, "SystemCallFilter", "RestrictAddressFamilies")) { | |
57183d11 | 4823 | _cleanup_strv_free_ char **l = NULL; |
6b000af4 | 4824 | int allow_list; |
57183d11 LP |
4825 | |
4826 | r = sd_bus_message_enter_container(m, 'r', "bas"); | |
4827 | if (r < 0) | |
4828 | return bus_log_parse_error(r); | |
4829 | ||
6b000af4 | 4830 | r = sd_bus_message_read(m, "b", &allow_list); |
57183d11 LP |
4831 | if (r < 0) |
4832 | return bus_log_parse_error(r); | |
4833 | ||
4834 | r = sd_bus_message_read_strv(m, &l); | |
4835 | if (r < 0) | |
4836 | return bus_log_parse_error(r); | |
4837 | ||
4838 | r = sd_bus_message_exit_container(m); | |
4839 | if (r < 0) | |
4840 | return bus_log_parse_error(r); | |
4841 | ||
6b000af4 | 4842 | if (all || allow_list || !strv_isempty(l)) { |
57183d11 LP |
4843 | bool first = true; |
4844 | char **i; | |
4845 | ||
07636114 | 4846 | if (!value) { |
4f9a9105 ZJS |
4847 | fputs(name, stdout); |
4848 | fputc('=', stdout); | |
4849 | } | |
57183d11 | 4850 | |
6b000af4 | 4851 | if (!allow_list) |
57183d11 LP |
4852 | fputc('~', stdout); |
4853 | ||
4854 | STRV_FOREACH(i, l) { | |
4855 | if (first) | |
4856 | first = false; | |
4857 | else | |
4858 | fputc(' ', stdout); | |
4859 | ||
4860 | fputs(*i, stdout); | |
4861 | } | |
4862 | fputc('\n', stdout); | |
4863 | } | |
4864 | ||
c7bb2fec | 4865 | return 1; |
102b0214 | 4866 | |
c7bb2fec IP |
4867 | } else if (STR_IN_SET(name, "SELinuxContext", "AppArmorProfile", "SmackProcessLabel")) { |
4868 | int ignore; | |
4869 | const char *s; | |
4870 | ||
4871 | r = sd_bus_message_read(m, "(bs)", &ignore, &s); | |
4872 | if (r < 0) | |
4873 | return bus_log_parse_error(r); | |
4874 | ||
4875 | if (!isempty(s)) | |
102b0214 | 4876 | bus_print_property_valuef(name, expected_value, value, "%s%s", ignore ? "-" : "", s); |
c7bb2fec | 4877 | else if (all) |
102b0214 | 4878 | bus_print_property_value(name, expected_value, value, ""); |
c7bb2fec | 4879 | |
07636114 | 4880 | return 1; |
4a220712 YW |
4881 | |
4882 | } else if (endswith(name, "ExitStatus") && streq(contents, "aiai")) { | |
4883 | const int32_t *status, *signal; | |
62b21e2e | 4884 | size_t n_status, n_signal, i; |
4a220712 YW |
4885 | |
4886 | r = sd_bus_message_enter_container(m, 'r', "aiai"); | |
4887 | if (r < 0) | |
4888 | return bus_log_parse_error(r); | |
4889 | ||
62b21e2e | 4890 | r = sd_bus_message_read_array(m, 'i', (const void **) &status, &n_status); |
4a220712 YW |
4891 | if (r < 0) |
4892 | return bus_log_parse_error(r); | |
4893 | ||
62b21e2e | 4894 | r = sd_bus_message_read_array(m, 'i', (const void **) &signal, &n_signal); |
4a220712 YW |
4895 | if (r < 0) |
4896 | return bus_log_parse_error(r); | |
4897 | ||
4898 | r = sd_bus_message_exit_container(m); | |
4899 | if (r < 0) | |
4900 | return bus_log_parse_error(r); | |
4901 | ||
62b21e2e ZJS |
4902 | n_status /= sizeof(int32_t); |
4903 | n_signal /= sizeof(int32_t); | |
4a220712 | 4904 | |
62b21e2e | 4905 | if (all || n_status > 0 || n_signal > 0) { |
4a220712 YW |
4906 | bool first = true; |
4907 | ||
4908 | if (!value) { | |
4909 | fputs(name, stdout); | |
4910 | fputc('=', stdout); | |
4911 | } | |
4912 | ||
62b21e2e | 4913 | for (i = 0; i < n_status; i++) { |
4a220712 YW |
4914 | if (first) |
4915 | first = false; | |
4916 | else | |
4917 | fputc(' ', stdout); | |
4918 | ||
4919 | printf("%"PRIi32, status[i]); | |
4920 | } | |
4921 | ||
62b21e2e | 4922 | for (i = 0; i < n_signal; i++) { |
4a220712 YW |
4923 | const char *str; |
4924 | ||
4925 | str = signal_to_string((int) signal[i]); | |
4a220712 YW |
4926 | |
4927 | if (first) | |
4928 | first = false; | |
4929 | else | |
4930 | fputc(' ', stdout); | |
4931 | ||
148ffa2e ZJS |
4932 | if (str) |
4933 | fputs(str, stdout); | |
4934 | else | |
4935 | printf("%"PRIi32, status[i]); | |
4a220712 YW |
4936 | } |
4937 | ||
4938 | fputc('\n', stdout); | |
4939 | } | |
4940 | return 1; | |
48220598 LP |
4941 | } |
4942 | ||
4943 | break; | |
48220598 | 4944 | |
f459b602 | 4945 | case SD_BUS_TYPE_ARRAY: |
48220598 | 4946 | |
07636114 | 4947 | if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "EnvironmentFiles")) { |
f459b602 MAP |
4948 | const char *path; |
4949 | int ignore; | |
8c7be95e | 4950 | |
f459b602 MAP |
4951 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(sb)"); |
4952 | if (r < 0) | |
4953 | return bus_log_parse_error(r); | |
8c7be95e | 4954 | |
f459b602 | 4955 | while ((r = sd_bus_message_read(m, "(sb)", &path, &ignore)) > 0) |
102b0214 | 4956 | bus_print_property_valuef(name, expected_value, value, "%s (ignore_errors=%s)", path, yes_no(ignore)); |
8c7be95e | 4957 | |
f459b602 MAP |
4958 | if (r < 0) |
4959 | return bus_log_parse_error(r); | |
8c7be95e | 4960 | |
f459b602 MAP |
4961 | r = sd_bus_message_exit_container(m); |
4962 | if (r < 0) | |
4963 | return bus_log_parse_error(r); | |
8c7be95e | 4964 | |
07636114 | 4965 | return 1; |
8c7be95e | 4966 | |
07636114 | 4967 | } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "Paths")) { |
f459b602 | 4968 | const char *type, *path; |
67419600 | 4969 | |
f459b602 MAP |
4970 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(ss)"); |
4971 | if (r < 0) | |
4972 | return bus_log_parse_error(r); | |
ebf57b80 | 4973 | |
f459b602 | 4974 | while ((r = sd_bus_message_read(m, "(ss)", &type, &path)) > 0) |
102b0214 | 4975 | bus_print_property_valuef(name, expected_value, value, "%s (%s)", path, type); |
f459b602 MAP |
4976 | if (r < 0) |
4977 | return bus_log_parse_error(r); | |
ebf57b80 | 4978 | |
f459b602 MAP |
4979 | r = sd_bus_message_exit_container(m); |
4980 | if (r < 0) | |
4981 | return bus_log_parse_error(r); | |
ebf57b80 | 4982 | |
07636114 | 4983 | return 1; |
582a507f | 4984 | |
07636114 | 4985 | } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "Listen")) { |
f459b602 | 4986 | const char *type, *path; |
67419600 | 4987 | |
f459b602 MAP |
4988 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(ss)"); |
4989 | if (r < 0) | |
4990 | return bus_log_parse_error(r); | |
67419600 | 4991 | |
f459b602 | 4992 | while ((r = sd_bus_message_read(m, "(ss)", &type, &path)) > 0) |
102b0214 | 4993 | bus_print_property_valuef(name, expected_value, value, "%s (%s)", path, type); |
f459b602 MAP |
4994 | if (r < 0) |
4995 | return bus_log_parse_error(r); | |
67419600 | 4996 | |
f459b602 MAP |
4997 | r = sd_bus_message_exit_container(m); |
4998 | if (r < 0) | |
4999 | return bus_log_parse_error(r); | |
67419600 | 5000 | |
07636114 | 5001 | return 1; |
67419600 | 5002 | |
07636114 | 5003 | } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "TimersMonotonic")) { |
f459b602 | 5004 | const char *base; |
07636114 | 5005 | uint64_t v, next_elapse; |
707e5e52 | 5006 | |
f459b602 MAP |
5007 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(stt)"); |
5008 | if (r < 0) | |
5009 | return bus_log_parse_error(r); | |
552e4331 | 5010 | |
07636114 | 5011 | while ((r = sd_bus_message_read(m, "(stt)", &base, &v, &next_elapse)) > 0) { |
860f2be0 | 5012 | char timespan1[FORMAT_TIMESPAN_MAX] = "n/a", timespan2[FORMAT_TIMESPAN_MAX] = "n/a"; |
fe68089d | 5013 | |
860f2be0 ZJS |
5014 | (void) format_timespan(timespan1, sizeof timespan1, v, 0); |
5015 | (void) format_timespan(timespan2, sizeof timespan2, next_elapse, 0); | |
5016 | ||
5017 | bus_print_property_valuef(name, expected_value, value, | |
5018 | "{ %s=%s ; next_elapse=%s }", base, timespan1, timespan2); | |
fe68089d | 5019 | } |
f459b602 MAP |
5020 | if (r < 0) |
5021 | return bus_log_parse_error(r); | |
5022 | ||
5023 | r = sd_bus_message_exit_container(m); | |
5024 | if (r < 0) | |
5025 | return bus_log_parse_error(r); | |
fe68089d | 5026 | |
07636114 | 5027 | return 1; |
fe68089d | 5028 | |
07636114 | 5029 | } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "TimersCalendar")) { |
fa6dee52 YW |
5030 | const char *base, *spec; |
5031 | uint64_t next_elapse; | |
5032 | ||
5033 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(sst)"); | |
5034 | if (r < 0) | |
5035 | return bus_log_parse_error(r); | |
5036 | ||
5037 | while ((r = sd_bus_message_read(m, "(sst)", &base, &spec, &next_elapse)) > 0) { | |
860f2be0 | 5038 | char timestamp[FORMAT_TIMESTAMP_MAX] = "n/a"; |
fa6dee52 | 5039 | |
860f2be0 ZJS |
5040 | (void) format_timestamp(timestamp, sizeof(timestamp), next_elapse); |
5041 | bus_print_property_valuef(name, expected_value, value, | |
5042 | "{ %s=%s ; next_elapse=%s }", base, spec, timestamp); | |
fa6dee52 YW |
5043 | } |
5044 | if (r < 0) | |
5045 | return bus_log_parse_error(r); | |
5046 | ||
5047 | r = sd_bus_message_exit_container(m); | |
5048 | if (r < 0) | |
5049 | return bus_log_parse_error(r); | |
5050 | ||
07636114 | 5051 | return 1; |
fa6dee52 | 5052 | |
07636114 | 5053 | } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && startswith(name, "Exec")) { |
f459b602 | 5054 | ExecStatusInfo info = {}; |
b3d59367 | 5055 | bool is_ex_prop = endswith(name, "Ex"); |
f459b602 | 5056 | |
b3d59367 | 5057 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, is_ex_prop ? "(sasasttttuii)" : "(sasbttttuii)"); |
f459b602 MAP |
5058 | if (r < 0) |
5059 | return bus_log_parse_error(r); | |
5060 | ||
b3d59367 | 5061 | while ((r = exec_status_info_deserialize(m, &info, is_ex_prop)) > 0) { |
f459b602 | 5062 | char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX]; |
b3d59367 AZ |
5063 | _cleanup_strv_free_ char **optv = NULL; |
5064 | _cleanup_free_ char *tt, *o = NULL; | |
f459b602 MAP |
5065 | |
5066 | tt = strv_join(info.argv, " "); | |
5067 | ||
b3d59367 AZ |
5068 | if (is_ex_prop) { |
5069 | r = exec_command_flags_to_strv(info.flags, &optv); | |
5070 | if (r < 0) | |
5071 | return log_error_errno(r, "Failed to convert ExecCommandFlags to strv: %m"); | |
5072 | ||
5073 | o = strv_join(optv, " "); | |
5074 | ||
5075 | bus_print_property_valuef(name, expected_value, value, | |
5076 | "{ path=%s ; argv[]=%s ; flags=%s ; start_time=[%s] ; stop_time=[%s] ; pid="PID_FMT" ; code=%s ; status=%i%s%s }", | |
5077 | strna(info.path), | |
5078 | strna(tt), | |
5079 | strna(o), | |
5080 | strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)), | |
5081 | strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)), | |
5082 | info.pid, | |
5083 | sigchld_code_to_string(info.code), | |
5084 | info.status, | |
5085 | info.code == CLD_EXITED ? "" : "/", | |
5086 | strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status))); | |
5087 | } else | |
5088 | bus_print_property_valuef(name, expected_value, value, | |
5089 | "{ path=%s ; argv[]=%s ; ignore_errors=%s ; start_time=[%s] ; stop_time=[%s] ; pid="PID_FMT" ; code=%s ; status=%i%s%s }", | |
5090 | strna(info.path), | |
5091 | strna(tt), | |
5092 | yes_no(info.ignore), | |
5093 | strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)), | |
5094 | strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)), | |
5095 | info.pid, | |
5096 | sigchld_code_to_string(info.code), | |
5097 | info.status, | |
5098 | info.code == CLD_EXITED ? "" : "/", | |
5099 | strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status))); | |
fe68089d | 5100 | |
582a507f LP |
5101 | free(info.path); |
5102 | strv_free(info.argv); | |
f459b602 | 5103 | zero(info); |
707e5e52 LP |
5104 | } |
5105 | ||
f459b602 MAP |
5106 | r = sd_bus_message_exit_container(m); |
5107 | if (r < 0) | |
5108 | return bus_log_parse_error(r); | |
5109 | ||
07636114 | 5110 | return 1; |
4ad49000 | 5111 | |
07636114 | 5112 | } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "DeviceAllow")) { |
f459b602 | 5113 | const char *path, *rwm; |
4ad49000 | 5114 | |
f459b602 MAP |
5115 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(ss)"); |
5116 | if (r < 0) | |
5117 | return bus_log_parse_error(r); | |
4ad49000 | 5118 | |
f459b602 | 5119 | while ((r = sd_bus_message_read(m, "(ss)", &path, &rwm)) > 0) |
102b0214 | 5120 | bus_print_property_valuef(name, expected_value, value, "%s %s", strna(path), strna(rwm)); |
f459b602 MAP |
5121 | if (r < 0) |
5122 | return bus_log_parse_error(r); | |
4ad49000 | 5123 | |
f459b602 MAP |
5124 | r = sd_bus_message_exit_container(m); |
5125 | if (r < 0) | |
5126 | return bus_log_parse_error(r); | |
4ad49000 | 5127 | |
07636114 | 5128 | return 1; |
4ad49000 | 5129 | |
07636114 | 5130 | } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && |
72240b52 | 5131 | STR_IN_SET(name, "IODeviceWeight", "BlockIODeviceWeight")) { |
f459b602 MAP |
5132 | const char *path; |
5133 | uint64_t weight; | |
b8ab2dc6 | 5134 | |
f459b602 MAP |
5135 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(st)"); |
5136 | if (r < 0) | |
5137 | return bus_log_parse_error(r); | |
b8ab2dc6 | 5138 | |
f459b602 | 5139 | while ((r = sd_bus_message_read(m, "(st)", &path, &weight)) > 0) |
102b0214 | 5140 | bus_print_property_valuef(name, expected_value, value, "%s %"PRIu64, strna(path), weight); |
f459b602 MAP |
5141 | if (r < 0) |
5142 | return bus_log_parse_error(r); | |
b8ab2dc6 | 5143 | |
f459b602 MAP |
5144 | r = sd_bus_message_exit_container(m); |
5145 | if (r < 0) | |
5146 | return bus_log_parse_error(r); | |
b8ab2dc6 | 5147 | |
07636114 | 5148 | return 1; |
b8ab2dc6 | 5149 | |
07636114 | 5150 | } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && |
72240b52 ZJS |
5151 | (cgroup_io_limit_type_from_string(name) >= 0 || |
5152 | STR_IN_SET(name, "BlockIOReadBandwidth", "BlockIOWriteBandwidth"))) { | |
f459b602 MAP |
5153 | const char *path; |
5154 | uint64_t bandwidth; | |
4ad49000 | 5155 | |
f459b602 MAP |
5156 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(st)"); |
5157 | if (r < 0) | |
5158 | return bus_log_parse_error(r); | |
4ad49000 | 5159 | |
f459b602 | 5160 | while ((r = sd_bus_message_read(m, "(st)", &path, &bandwidth)) > 0) |
102b0214 | 5161 | bus_print_property_valuef(name, expected_value, value, "%s %"PRIu64, strna(path), bandwidth); |
f459b602 MAP |
5162 | if (r < 0) |
5163 | return bus_log_parse_error(r); | |
4ad49000 | 5164 | |
f459b602 MAP |
5165 | r = sd_bus_message_exit_container(m); |
5166 | if (r < 0) | |
5167 | return bus_log_parse_error(r); | |
4ad49000 | 5168 | |
07636114 | 5169 | return 1; |
6ae4283c TH |
5170 | |
5171 | } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && | |
5172 | streq(name, "IODeviceLatencyTargetUSec")) { | |
5173 | char ts[FORMAT_TIMESPAN_MAX]; | |
5174 | const char *path; | |
5175 | uint64_t target; | |
5176 | ||
5177 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(st)"); | |
5178 | if (r < 0) | |
5179 | return bus_log_parse_error(r); | |
5180 | ||
5181 | while ((r = sd_bus_message_read(m, "(st)", &path, &target)) > 0) | |
102b0214 YW |
5182 | bus_print_property_valuef(name, expected_value, value, "%s %s", strna(path), |
5183 | format_timespan(ts, sizeof(ts), target, 1)); | |
6ae4283c TH |
5184 | if (r < 0) |
5185 | return bus_log_parse_error(r); | |
5186 | ||
5187 | r = sd_bus_message_exit_container(m); | |
5188 | if (r < 0) | |
5189 | return bus_log_parse_error(r); | |
5190 | ||
5191 | return 1; | |
08f3be7a | 5192 | |
d4d55b0d | 5193 | } else if (contents[0] == SD_BUS_TYPE_BYTE && STR_IN_SET(name, "StandardInputData", "RootHashSignature")) { |
08f3be7a LP |
5194 | _cleanup_free_ char *h = NULL; |
5195 | const void *p; | |
5196 | size_t sz; | |
5197 | ssize_t n; | |
5198 | ||
5199 | r = sd_bus_message_read_array(m, 'y', &p, &sz); | |
5200 | if (r < 0) | |
5201 | return bus_log_parse_error(r); | |
5202 | ||
5203 | n = base64mem(p, sz, &h); | |
5204 | if (n < 0) | |
5205 | return log_oom(); | |
5206 | ||
102b0214 | 5207 | bus_print_property_value(name, expected_value, value, h); |
08f3be7a | 5208 | |
07636114 | 5209 | return 1; |
9c1f9465 YW |
5210 | |
5211 | } else if (STR_IN_SET(name, "IPAddressAllow", "IPAddressDeny")) { | |
5212 | _cleanup_free_ char *addresses = NULL; | |
5213 | ||
5214 | r = sd_bus_message_enter_container(m, 'a', "(iayu)"); | |
5215 | if (r < 0) | |
5216 | return bus_log_parse_error(r); | |
5217 | ||
5218 | for (;;) { | |
5219 | _cleanup_free_ char *str = NULL; | |
5220 | uint32_t prefixlen; | |
5221 | int32_t family; | |
5222 | const void *ap; | |
5223 | size_t an; | |
5224 | ||
5225 | r = sd_bus_message_enter_container(m, 'r', "iayu"); | |
5226 | if (r < 0) | |
5227 | return bus_log_parse_error(r); | |
5228 | if (r == 0) | |
5229 | break; | |
5230 | ||
5231 | r = sd_bus_message_read(m, "i", &family); | |
5232 | if (r < 0) | |
5233 | return bus_log_parse_error(r); | |
5234 | ||
5235 | r = sd_bus_message_read_array(m, 'y', &ap, &an); | |
5236 | if (r < 0) | |
5237 | return bus_log_parse_error(r); | |
5238 | ||
5239 | r = sd_bus_message_read(m, "u", &prefixlen); | |
5240 | if (r < 0) | |
5241 | return bus_log_parse_error(r); | |
5242 | ||
5243 | r = sd_bus_message_exit_container(m); | |
5244 | if (r < 0) | |
5245 | return bus_log_parse_error(r); | |
5246 | ||
5247 | if (!IN_SET(family, AF_INET, AF_INET6)) | |
5248 | continue; | |
5249 | ||
5250 | if (an != FAMILY_ADDRESS_SIZE(family)) | |
5251 | continue; | |
5252 | ||
5253 | if (prefixlen > FAMILY_ADDRESS_SIZE(family) * 8) | |
5254 | continue; | |
5255 | ||
5256 | if (in_addr_prefix_to_string(family, (union in_addr_union *) ap, prefixlen, &str) < 0) | |
5257 | continue; | |
5258 | ||
5259 | if (!strextend_with_separator(&addresses, " ", str, NULL)) | |
5260 | return log_oom(); | |
5261 | } | |
5262 | ||
5263 | r = sd_bus_message_exit_container(m); | |
5264 | if (r < 0) | |
5265 | return bus_log_parse_error(r); | |
5266 | ||
5267 | if (all || !isempty(addresses)) | |
5268 | bus_print_property_value(name, expected_value, value, strempty(addresses)); | |
5269 | ||
5270 | return 1; | |
4c787df7 YW |
5271 | |
5272 | } else if (STR_IN_SET(name, "BindPaths", "BindReadOnlyPaths")) { | |
5273 | _cleanup_free_ char *paths = NULL; | |
5274 | const char *source, *dest; | |
5275 | int ignore_enoent; | |
5276 | uint64_t rbind; | |
5277 | ||
5278 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(ssbt)"); | |
5279 | if (r < 0) | |
5280 | return bus_log_parse_error(r); | |
5281 | ||
5282 | while ((r = sd_bus_message_read(m, "(ssbt)", &source, &dest, &ignore_enoent, &rbind)) > 0) { | |
5283 | _cleanup_free_ char *str = NULL; | |
5284 | ||
5285 | if (isempty(source)) | |
5286 | continue; | |
5287 | ||
5288 | if (asprintf(&str, "%s%s%s%s%s", | |
5289 | ignore_enoent ? "-" : "", | |
5290 | source, | |
5291 | isempty(dest) ? "" : ":", | |
5292 | strempty(dest), | |
5293 | rbind == MS_REC ? ":rbind" : "") < 0) | |
5294 | return log_oom(); | |
5295 | ||
5296 | if (!strextend_with_separator(&paths, " ", str, NULL)) | |
5297 | return log_oom(); | |
5298 | } | |
5299 | if (r < 0) | |
5300 | return bus_log_parse_error(r); | |
5301 | ||
5302 | r = sd_bus_message_exit_container(m); | |
5303 | if (r < 0) | |
5304 | return bus_log_parse_error(r); | |
5305 | ||
5306 | if (all || !isempty(paths)) | |
5307 | bus_print_property_value(name, expected_value, value, strempty(paths)); | |
5308 | ||
5309 | return 1; | |
5310 | ||
5311 | } else if (streq(name, "TemporaryFileSystem")) { | |
5312 | _cleanup_free_ char *paths = NULL; | |
5313 | const char *target, *option; | |
5314 | ||
5315 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(ss)"); | |
5316 | if (r < 0) | |
5317 | return bus_log_parse_error(r); | |
5318 | ||
5319 | while ((r = sd_bus_message_read(m, "(ss)", &target, &option)) > 0) { | |
5320 | _cleanup_free_ char *str = NULL; | |
5321 | ||
5322 | if (isempty(target)) | |
5323 | continue; | |
5324 | ||
5325 | if (asprintf(&str, "%s%s%s", target, isempty(option) ? "" : ":", strempty(option)) < 0) | |
5326 | return log_oom(); | |
5327 | ||
5328 | if (!strextend_with_separator(&paths, " ", str, NULL)) | |
5329 | return log_oom(); | |
5330 | } | |
5331 | if (r < 0) | |
5332 | return bus_log_parse_error(r); | |
5333 | ||
5334 | r = sd_bus_message_exit_container(m); | |
5335 | if (r < 0) | |
5336 | return bus_log_parse_error(r); | |
5337 | ||
5338 | if (all || !isempty(paths)) | |
5339 | bus_print_property_value(name, expected_value, value, strempty(paths)); | |
5340 | ||
5341 | return 1; | |
305a2850 YW |
5342 | |
5343 | } else if (streq(name, "LogExtraFields")) { | |
5344 | _cleanup_free_ char *fields = NULL; | |
5345 | const void *p; | |
5346 | size_t sz; | |
5347 | ||
5348 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "ay"); | |
5349 | if (r < 0) | |
5350 | return bus_log_parse_error(r); | |
5351 | ||
5352 | while ((r = sd_bus_message_read_array(m, 'y', &p, &sz)) > 0) { | |
5353 | _cleanup_free_ char *str = NULL; | |
5354 | const char *eq; | |
5355 | ||
5356 | if (memchr(p, 0, sz)) | |
5357 | continue; | |
5358 | ||
5359 | eq = memchr(p, '=', sz); | |
5360 | if (!eq) | |
5361 | continue; | |
5362 | ||
5363 | if (!journal_field_valid(p, eq - (const char*) p, false)) | |
5364 | continue; | |
5365 | ||
5366 | str = malloc(sz + 1); | |
5367 | if (!str) | |
5368 | return log_oom(); | |
5369 | ||
5370 | memcpy(str, p, sz); | |
5371 | str[sz] = '\0'; | |
5372 | ||
5373 | if (!utf8_is_valid(str)) | |
5374 | continue; | |
5375 | ||
5376 | if (!strextend_with_separator(&fields, " ", str, NULL)) | |
5377 | return log_oom(); | |
5378 | } | |
5379 | if (r < 0) | |
5380 | return bus_log_parse_error(r); | |
5381 | ||
5382 | r = sd_bus_message_exit_container(m); | |
5383 | if (r < 0) | |
5384 | return bus_log_parse_error(r); | |
5385 | ||
5386 | if (all || !isempty(fields)) | |
5387 | bus_print_property_value(name, expected_value, value, strempty(fields)); | |
5388 | ||
a047f4f1 | 5389 | return 1; |
047f5d63 | 5390 | } else if (contents[0] == SD_BUS_TYPE_BYTE && STR_IN_SET(name, "CPUAffinity", "NUMAMask", "AllowedCPUs", "AllowedMemoryNodes", "EffectiveCPUs", "EffectiveMemoryNodes")) { |
a047f4f1 MS |
5391 | _cleanup_free_ char *affinity = NULL; |
5392 | _cleanup_(cpu_set_reset) CPUSet set = {}; | |
5393 | const void *a; | |
5394 | size_t n; | |
5395 | ||
5396 | r = sd_bus_message_read_array(m, 'y', &a, &n); | |
5397 | if (r < 0) | |
5398 | return bus_log_parse_error(r); | |
5399 | ||
5400 | r = cpu_set_from_dbus(a, n, &set); | |
5401 | if (r < 0) | |
b070c7c0 | 5402 | return log_error_errno(r, "Failed to deserialize %s: %m", name); |
a047f4f1 MS |
5403 | |
5404 | affinity = cpu_set_to_range_string(&set); | |
5405 | if (!affinity) | |
5406 | return log_oom(); | |
5407 | ||
5408 | bus_print_property_value(name, expected_value, value, affinity); | |
5409 | ||
305a2850 | 5410 | return 1; |
b3d13314 LB |
5411 | } else if (streq(name, "MountImages")) { |
5412 | _cleanup_free_ char *paths = NULL; | |
5413 | const char *source, *dest; | |
5414 | int ignore_enoent; | |
5415 | ||
5416 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(ssb)"); | |
5417 | if (r < 0) | |
5418 | return bus_log_parse_error(r); | |
5419 | ||
5420 | while ((r = sd_bus_message_read(m, "(ssb)", &source, &dest, &ignore_enoent)) > 0) { | |
5421 | _cleanup_free_ char *str = NULL; | |
5422 | ||
5423 | if (isempty(source)) | |
5424 | continue; | |
5425 | ||
5426 | if (asprintf(&str, "%s%s:%s", ignore_enoent ? "-" : "", source, dest) < 0) | |
5427 | return log_oom(); | |
5428 | ||
5429 | if (!strextend_with_separator(&paths, " ", str, NULL)) | |
5430 | return log_oom(); | |
5431 | } | |
5432 | if (r < 0) | |
5433 | return bus_log_parse_error(r); | |
5434 | ||
5435 | r = sd_bus_message_exit_container(m); | |
5436 | if (r < 0) | |
5437 | return bus_log_parse_error(r); | |
5438 | ||
5439 | if (all || !isempty(paths)) | |
5440 | bus_print_property_value(name, expected_value, value, strempty(paths)); | |
5441 | ||
5442 | return 1; | |
5443 | ||
48220598 LP |
5444 | } |
5445 | ||
5446 | break; | |
5447 | } | |
5448 | ||
48220598 LP |
5449 | return 0; |
5450 | } | |
5451 | ||
8559e61d YW |
5452 | typedef enum SystemctlShowMode{ |
5453 | SYSTEMCTL_SHOW_PROPERTIES, | |
5454 | SYSTEMCTL_SHOW_STATUS, | |
5455 | SYSTEMCTL_SHOW_HELP, | |
5456 | _SYSTEMCTL_SHOW_MODE_MAX, | |
5457 | _SYSTEMCTL_SHOW_MODE_INVALID = -1, | |
5458 | } SystemctlShowMode; | |
5459 | ||
aa8d423e | 5460 | static const char* const systemctl_show_mode_table[_SYSTEMCTL_SHOW_MODE_MAX] = { |
8559e61d YW |
5461 | [SYSTEMCTL_SHOW_PROPERTIES] = "show", |
5462 | [SYSTEMCTL_SHOW_STATUS] = "status", | |
5463 | [SYSTEMCTL_SHOW_HELP] = "help", | |
5464 | }; | |
5465 | ||
5466 | DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(systemctl_show_mode, SystemctlShowMode); | |
5467 | ||
f459b602 | 5468 | static int show_one( |
f459b602 MAP |
5469 | sd_bus *bus, |
5470 | const char *path, | |
3dced37b | 5471 | const char *unit, |
8559e61d | 5472 | SystemctlShowMode show_mode, |
f459b602 MAP |
5473 | bool *new_line, |
5474 | bool *ellipsized) { | |
5475 | ||
3dced37b | 5476 | static const struct bus_properties_map property_map[] = { |
b3d59367 AZ |
5477 | { "LoadState", "s", NULL, offsetof(UnitStatusInfo, load_state) }, |
5478 | { "ActiveState", "s", NULL, offsetof(UnitStatusInfo, active_state) }, | |
d9e45bc3 | 5479 | { "FreezerState", "s", NULL, offsetof(UnitStatusInfo, freezer_state) }, |
b3d59367 | 5480 | { "Documentation", "as", NULL, offsetof(UnitStatusInfo, documentation) }, |
4679a8c3 YW |
5481 | {} |
5482 | }, status_map[] = { | |
b3d59367 AZ |
5483 | { "Id", "s", NULL, offsetof(UnitStatusInfo, id) }, |
5484 | { "LoadState", "s", NULL, offsetof(UnitStatusInfo, load_state) }, | |
5485 | { "ActiveState", "s", NULL, offsetof(UnitStatusInfo, active_state) }, | |
d9e45bc3 | 5486 | { "FreezerState", "s", NULL, offsetof(UnitStatusInfo, freezer_state) }, |
b3d59367 AZ |
5487 | { "SubState", "s", NULL, offsetof(UnitStatusInfo, sub_state) }, |
5488 | { "UnitFileState", "s", NULL, offsetof(UnitStatusInfo, unit_file_state) }, | |
5489 | { "UnitFilePreset", "s", NULL, offsetof(UnitStatusInfo, unit_file_preset) }, | |
5490 | { "Description", "s", NULL, offsetof(UnitStatusInfo, description) }, | |
5491 | { "Following", "s", NULL, offsetof(UnitStatusInfo, following) }, | |
5492 | { "Documentation", "as", NULL, offsetof(UnitStatusInfo, documentation) }, | |
5493 | { "FragmentPath", "s", NULL, offsetof(UnitStatusInfo, fragment_path) }, | |
5494 | { "SourcePath", "s", NULL, offsetof(UnitStatusInfo, source_path) }, | |
5495 | { "ControlGroup", "s", NULL, offsetof(UnitStatusInfo, control_group) }, | |
5496 | { "DropInPaths", "as", NULL, offsetof(UnitStatusInfo, dropin_paths) }, | |
5497 | { "LoadError", "(ss)", map_load_error, offsetof(UnitStatusInfo, load_error) }, | |
5498 | { "Result", "s", NULL, offsetof(UnitStatusInfo, result) }, | |
965c5d1d KK |
5499 | { "TriggeredBy", "as", NULL, offsetof(UnitStatusInfo, triggered_by) }, |
5500 | { "Triggers", "as", NULL, offsetof(UnitStatusInfo, triggers) }, | |
b3d59367 AZ |
5501 | { "InactiveExitTimestamp", "t", NULL, offsetof(UnitStatusInfo, inactive_exit_timestamp) }, |
5502 | { "InactiveExitTimestampMonotonic", "t", NULL, offsetof(UnitStatusInfo, inactive_exit_timestamp_monotonic) }, | |
5503 | { "ActiveEnterTimestamp", "t", NULL, offsetof(UnitStatusInfo, active_enter_timestamp) }, | |
5504 | { "ActiveExitTimestamp", "t", NULL, offsetof(UnitStatusInfo, active_exit_timestamp) }, | |
5505 | { "InactiveEnterTimestamp", "t", NULL, offsetof(UnitStatusInfo, inactive_enter_timestamp) }, | |
5506 | { "NeedDaemonReload", "b", NULL, offsetof(UnitStatusInfo, need_daemon_reload) }, | |
5507 | { "Transient", "b", NULL, offsetof(UnitStatusInfo, transient) }, | |
5508 | { "ExecMainPID", "u", NULL, offsetof(UnitStatusInfo, main_pid) }, | |
5509 | { "MainPID", "u", map_main_pid, 0 }, | |
5510 | { "ControlPID", "u", NULL, offsetof(UnitStatusInfo, control_pid) }, | |
5511 | { "StatusText", "s", NULL, offsetof(UnitStatusInfo, status_text) }, | |
5512 | { "PIDFile", "s", NULL, offsetof(UnitStatusInfo, pid_file) }, | |
5513 | { "StatusErrno", "i", NULL, offsetof(UnitStatusInfo, status_errno) }, | |
5514 | { "ExecMainStartTimestamp", "t", NULL, offsetof(UnitStatusInfo, start_timestamp) }, | |
5515 | { "ExecMainExitTimestamp", "t", NULL, offsetof(UnitStatusInfo, exit_timestamp) }, | |
5516 | { "ExecMainCode", "i", NULL, offsetof(UnitStatusInfo, exit_code) }, | |
5517 | { "ExecMainStatus", "i", NULL, offsetof(UnitStatusInfo, exit_status) }, | |
d93dda3a | 5518 | { "LogNamespace", "s", NULL, offsetof(UnitStatusInfo, log_namespace) }, |
b3d59367 AZ |
5519 | { "ConditionTimestamp", "t", NULL, offsetof(UnitStatusInfo, condition_timestamp) }, |
5520 | { "ConditionResult", "b", NULL, offsetof(UnitStatusInfo, condition_result) }, | |
5521 | { "Conditions", "a(sbbsi)", map_conditions, 0 }, | |
5522 | { "AssertTimestamp", "t", NULL, offsetof(UnitStatusInfo, assert_timestamp) }, | |
5523 | { "AssertResult", "b", NULL, offsetof(UnitStatusInfo, assert_result) }, | |
5524 | { "Asserts", "a(sbbsi)", map_asserts, 0 }, | |
5525 | { "NextElapseUSecRealtime", "t", NULL, offsetof(UnitStatusInfo, next_elapse_real) }, | |
5526 | { "NextElapseUSecMonotonic", "t", NULL, offsetof(UnitStatusInfo, next_elapse_monotonic) }, | |
5527 | { "NAccepted", "u", NULL, offsetof(UnitStatusInfo, n_accepted) }, | |
5528 | { "NConnections", "u", NULL, offsetof(UnitStatusInfo, n_connections) }, | |
5529 | { "NRefused", "u", NULL, offsetof(UnitStatusInfo, n_refused) }, | |
5530 | { "Accept", "b", NULL, offsetof(UnitStatusInfo, accept) }, | |
5531 | { "Listen", "a(ss)", map_listen, offsetof(UnitStatusInfo, listen) }, | |
5532 | { "SysFSPath", "s", NULL, offsetof(UnitStatusInfo, sysfs_path) }, | |
5533 | { "Where", "s", NULL, offsetof(UnitStatusInfo, where) }, | |
5534 | { "What", "s", NULL, offsetof(UnitStatusInfo, what) }, | |
5535 | { "MemoryCurrent", "t", NULL, offsetof(UnitStatusInfo, memory_current) }, | |
5536 | { "DefaultMemoryMin", "t", NULL, offsetof(UnitStatusInfo, default_memory_min) }, | |
5537 | { "DefaultMemoryLow", "t", NULL, offsetof(UnitStatusInfo, default_memory_low) }, | |
5538 | { "MemoryMin", "t", NULL, offsetof(UnitStatusInfo, memory_min) }, | |
5539 | { "MemoryLow", "t", NULL, offsetof(UnitStatusInfo, memory_low) }, | |
5540 | { "MemoryHigh", "t", NULL, offsetof(UnitStatusInfo, memory_high) }, | |
5541 | { "MemoryMax", "t", NULL, offsetof(UnitStatusInfo, memory_max) }, | |
5542 | { "MemorySwapMax", "t", NULL, offsetof(UnitStatusInfo, memory_swap_max) }, | |
5543 | { "MemoryLimit", "t", NULL, offsetof(UnitStatusInfo, memory_limit) }, | |
5544 | { "CPUUsageNSec", "t", NULL, offsetof(UnitStatusInfo, cpu_usage_nsec) }, | |
5545 | { "TasksCurrent", "t", NULL, offsetof(UnitStatusInfo, tasks_current) }, | |
5546 | { "TasksMax", "t", NULL, offsetof(UnitStatusInfo, tasks_max) }, | |
5547 | { "IPIngressBytes", "t", NULL, offsetof(UnitStatusInfo, ip_ingress_bytes) }, | |
5548 | { "IPEgressBytes", "t", NULL, offsetof(UnitStatusInfo, ip_egress_bytes) }, | |
5549 | { "IOReadBytes", "t", NULL, offsetof(UnitStatusInfo, io_read_bytes) }, | |
5550 | { "IOWriteBytes", "t", NULL, offsetof(UnitStatusInfo, io_write_bytes) }, | |
898fc00e AZ |
5551 | { "ExecCondition", "a(sasbttttuii)", map_exec, 0 }, |
5552 | { "ExecConditionEx", "a(sasasttttuii)", map_exec, 0 }, | |
b3d59367 AZ |
5553 | { "ExecStartPre", "a(sasbttttuii)", map_exec, 0 }, |
5554 | { "ExecStartPreEx", "a(sasasttttuii)", map_exec, 0 }, | |
5555 | { "ExecStart", "a(sasbttttuii)", map_exec, 0 }, | |
5556 | { "ExecStartEx", "a(sasasttttuii)", map_exec, 0 }, | |
5557 | { "ExecStartPost", "a(sasbttttuii)", map_exec, 0 }, | |
5558 | { "ExecStartPostEx", "a(sasasttttuii)", map_exec, 0 }, | |
5559 | { "ExecReload", "a(sasbttttuii)", map_exec, 0 }, | |
898fc00e | 5560 | { "ExecReloadEx", "a(sasasttttuii)", map_exec, 0 }, |
b3d59367 AZ |
5561 | { "ExecStopPre", "a(sasbttttuii)", map_exec, 0 }, |
5562 | { "ExecStop", "a(sasbttttuii)", map_exec, 0 }, | |
898fc00e | 5563 | { "ExecStopEx", "a(sasasttttuii)", map_exec, 0 }, |
b3d59367 | 5564 | { "ExecStopPost", "a(sasbttttuii)", map_exec, 0 }, |
898fc00e | 5565 | { "ExecStopPostEx", "a(sasasttttuii)", map_exec, 0 }, |
3dced37b LP |
5566 | {} |
5567 | }; | |
5568 | ||
4afd3348 LP |
5569 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
5570 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
3dced37b | 5571 | _cleanup_set_free_ Set *found_properties = NULL; |
a7335518 | 5572 | _cleanup_(unit_status_info_free) UnitStatusInfo info = { |
934277fe | 5573 | .memory_current = (uint64_t) -1, |
da4d897e TH |
5574 | .memory_high = CGROUP_LIMIT_MAX, |
5575 | .memory_max = CGROUP_LIMIT_MAX, | |
96e131ea | 5576 | .memory_swap_max = CGROUP_LIMIT_MAX, |
934277fe | 5577 | .memory_limit = (uint64_t) -1, |
5ad096b3 | 5578 | .cpu_usage_nsec = (uint64_t) -1, |
03a7b521 LP |
5579 | .tasks_current = (uint64_t) -1, |
5580 | .tasks_max = (uint64_t) -1, | |
0e97c93f DM |
5581 | .ip_ingress_bytes = (uint64_t) -1, |
5582 | .ip_egress_bytes = (uint64_t) -1, | |
7a5de3f9 LP |
5583 | .io_read_bytes = UINT64_MAX, |
5584 | .io_write_bytes = UINT64_MAX, | |
934277fe | 5585 | }; |
4679a8c3 | 5586 | char **pp; |
f459b602 | 5587 | int r; |
48220598 | 5588 | |
48220598 | 5589 | assert(path); |
61cbdc4b | 5590 | assert(new_line); |
48220598 | 5591 | |
e3e0314b ZJS |
5592 | log_debug("Showing one %s", path); |
5593 | ||
4679a8c3 | 5594 | r = bus_map_all_properties( |
f22f08cd SP |
5595 | bus, |
5596 | "org.freedesktop.systemd1", | |
5597 | path, | |
4679a8c3 | 5598 | show_mode == SYSTEMCTL_SHOW_STATUS ? status_map : property_map, |
a7e4861c | 5599 | BUS_MAP_BOOLEAN_AS_BOOL, |
f459b602 | 5600 | &error, |
f22f08cd | 5601 | &reply, |
4679a8c3 | 5602 | &info); |
4c3e8e39 LP |
5603 | if (r < 0) |
5604 | return log_error_errno(r, "Failed to get properties: %s", bus_error_message(&error, r)); | |
48220598 | 5605 | |
4679a8c3 YW |
5606 | if (unit && streq_ptr(info.load_state, "not-found") && streq_ptr(info.active_state, "inactive")) { |
5607 | log_full(show_mode == SYSTEMCTL_SHOW_STATUS ? LOG_ERR : LOG_DEBUG, | |
5608 | "Unit %s could not be found.", unit); | |
e33a06a1 | 5609 | |
4679a8c3 YW |
5610 | if (show_mode == SYSTEMCTL_SHOW_STATUS) |
5611 | return EXIT_PROGRAM_OR_SERVICES_STATUS_UNKNOWN; | |
5612 | else if (show_mode == SYSTEMCTL_SHOW_HELP) | |
5613 | return -ENOENT; | |
5614 | } | |
e33a06a1 | 5615 | |
4679a8c3 YW |
5616 | if (*new_line) |
5617 | printf("\n"); | |
3dced37b | 5618 | |
4679a8c3 YW |
5619 | *new_line = true; |
5620 | ||
5621 | if (show_mode == SYSTEMCTL_SHOW_STATUS) { | |
5622 | print_status_info(bus, &info, ellipsized); | |
5623 | ||
5624 | if (info.active_state && !STR_IN_SET(info.active_state, "active", "reloading")) | |
5625 | return EXIT_PROGRAM_NOT_RUNNING; | |
5626 | ||
5627 | return EXIT_PROGRAM_RUNNING_OR_SERVICE_OK; | |
5628 | ||
5629 | } else if (show_mode == SYSTEMCTL_SHOW_HELP) { | |
5630 | show_unit_help(&info); | |
5631 | return 0; | |
3dced37b | 5632 | } |
e33a06a1 | 5633 | |
4679a8c3 YW |
5634 | r = sd_bus_message_rewind(reply, true); |
5635 | if (r < 0) | |
5636 | return log_error_errno(r, "Failed to rewind: %s", bus_error_message(&error, r)); | |
5637 | ||
07636114 | 5638 | r = bus_message_print_all_properties(reply, print_property, arg_properties, arg_value, arg_all, &found_properties); |
f459b602 MAP |
5639 | if (r < 0) |
5640 | return bus_log_parse_error(r); | |
48220598 | 5641 | |
4679a8c3 YW |
5642 | STRV_FOREACH(pp, arg_properties) |
5643 | if (!set_contains(found_properties, *pp)) | |
5644 | log_debug("Property %s does not exist.", *pp); | |
3dced37b | 5645 | |
4679a8c3 | 5646 | return 0; |
48220598 LP |
5647 | } |
5648 | ||
f74294c1 | 5649 | static int get_unit_dbus_path_by_pid( |
f459b602 MAP |
5650 | sd_bus *bus, |
5651 | uint32_t pid, | |
f74294c1 | 5652 | char **unit) { |
f459b602 | 5653 | |
4afd3348 LP |
5654 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
5655 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
373d32c9 | 5656 | char *u; |
a223b325 MS |
5657 | int r; |
5658 | ||
8010c205 | 5659 | r = bus_call_method(bus, bus_systemd_mgr, "GetUnitByPID", &error, &reply, "u", pid); |
691395d8 LP |
5660 | if (r < 0) |
5661 | return log_error_errno(r, "Failed to get unit for PID %"PRIu32": %s", pid, bus_error_message(&error, r)); | |
a223b325 | 5662 | |
373d32c9 | 5663 | r = sd_bus_message_read(reply, "o", &u); |
f459b602 MAP |
5664 | if (r < 0) |
5665 | return bus_log_parse_error(r); | |
5666 | ||
373d32c9 LP |
5667 | u = strdup(u); |
5668 | if (!u) | |
5669 | return log_oom(); | |
5670 | ||
5671 | *unit = u; | |
f74294c1 | 5672 | return 0; |
a223b325 MS |
5673 | } |
5674 | ||
f459b602 | 5675 | static int show_all( |
f459b602 | 5676 | sd_bus *bus, |
f459b602 MAP |
5677 | bool *new_line, |
5678 | bool *ellipsized) { | |
5679 | ||
4afd3348 | 5680 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
f459b602 MAP |
5681 | _cleanup_free_ UnitInfo *unit_infos = NULL; |
5682 | const UnitInfo *u; | |
5683 | unsigned c; | |
5bb75bc7 | 5684 | int r, ret = 0; |
265a7a2a | 5685 | |
1238ee09 | 5686 | r = get_unit_list(bus, NULL, NULL, &unit_infos, 0, &reply); |
265a7a2a ZJS |
5687 | if (r < 0) |
5688 | return r; | |
5689 | ||
0221d68a | 5690 | (void) pager_open(arg_pager_flags); |
dbed408b | 5691 | |
f459b602 MAP |
5692 | c = (unsigned) r; |
5693 | ||
93bab288 | 5694 | typesafe_qsort(unit_infos, c, compare_unit_info); |
991f2a39 | 5695 | |
265a7a2a | 5696 | for (u = unit_infos; u < unit_infos + c; u++) { |
7fd1b19b | 5697 | _cleanup_free_ char *p = NULL; |
265a7a2a | 5698 | |
265a7a2a ZJS |
5699 | p = unit_dbus_path_from_name(u->id); |
5700 | if (!p) | |
5701 | return log_oom(); | |
5702 | ||
8559e61d | 5703 | r = show_one(bus, p, u->id, SYSTEMCTL_SHOW_STATUS, new_line, ellipsized); |
3df538da | 5704 | if (r < 0) |
265a7a2a | 5705 | return r; |
5bb75bc7 ZJS |
5706 | else if (r > 0 && ret == 0) |
5707 | ret = r; | |
265a7a2a ZJS |
5708 | } |
5709 | ||
5bb75bc7 | 5710 | return ret; |
265a7a2a ZJS |
5711 | } |
5712 | ||
8fcf784d LP |
5713 | static int show_system_status(sd_bus *bus) { |
5714 | char since1[FORMAT_TIMESTAMP_RELATIVE_MAX], since2[FORMAT_TIMESTAMP_MAX]; | |
f9e0eefc | 5715 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
e7e55dbd | 5716 | _cleanup_(machine_info_clear) struct machine_info mi = {}; |
f9e0eefc | 5717 | _cleanup_free_ char *hn = NULL; |
8fcf784d LP |
5718 | const char *on, *off; |
5719 | int r; | |
5720 | ||
5721 | hn = gethostname_malloc(); | |
5722 | if (!hn) | |
5723 | return log_oom(); | |
5724 | ||
a7e4861c YW |
5725 | r = bus_map_all_properties( |
5726 | bus, | |
5727 | "org.freedesktop.systemd1", | |
5728 | "/org/freedesktop/systemd1", | |
5729 | machine_info_property_map, | |
5730 | BUS_MAP_STRDUP, | |
5731 | &error, | |
5732 | NULL, | |
5733 | &mi); | |
f647962d | 5734 | if (r < 0) |
f9e0eefc | 5735 | return log_error_errno(r, "Failed to read server status: %s", bus_error_message(&error, r)); |
8fcf784d | 5736 | |
8fcf784d LP |
5737 | if (streq_ptr(mi.state, "degraded")) { |
5738 | on = ansi_highlight_red(); | |
1fc464f6 | 5739 | off = ansi_normal(); |
92f34a9c JR |
5740 | } else if (streq_ptr(mi.state, "running")) { |
5741 | on = ansi_highlight_green(); | |
5742 | off = ansi_normal(); | |
5743 | } else { | |
8fcf784d | 5744 | on = ansi_highlight_yellow(); |
1fc464f6 | 5745 | off = ansi_normal(); |
92f34a9c | 5746 | } |
8fcf784d | 5747 | |
9a6f746f | 5748 | printf("%s%s%s %s\n", on, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE), off, arg_host ? arg_host : hn); |
b0d14c69 | 5749 | |
8fcf784d LP |
5750 | printf(" State: %s%s%s\n", |
5751 | on, strna(mi.state), off); | |
5752 | ||
c6ba5b80 MP |
5753 | printf(" Jobs: %" PRIu32 " queued\n", mi.n_jobs); |
5754 | printf(" Failed: %" PRIu32 " units\n", mi.n_failed_units); | |
8fcf784d LP |
5755 | |
5756 | printf(" Since: %s; %s\n", | |
5757 | format_timestamp(since2, sizeof(since2), mi.timestamp), | |
5758 | format_timestamp_relative(since1, sizeof(since1), mi.timestamp)); | |
5759 | ||
5760 | printf(" CGroup: %s\n", mi.control_group ?: "/"); | |
4c315c2c IS |
5761 | if (IN_SET(arg_transport, |
5762 | BUS_TRANSPORT_LOCAL, | |
5763 | BUS_TRANSPORT_MACHINE)) { | |
8fcf784d LP |
5764 | static const char prefix[] = " "; |
5765 | unsigned c; | |
5766 | ||
5767 | c = columns(); | |
5768 | if (c > sizeof(prefix) - 1) | |
5769 | c -= sizeof(prefix) - 1; | |
5770 | else | |
5771 | c = 0; | |
5772 | ||
0ff308c8 | 5773 | show_cgroup(SYSTEMD_CGROUP_CONTROLLER, strempty(mi.control_group), prefix, c, get_output_flags()); |
8fcf784d LP |
5774 | } |
5775 | ||
8fcf784d LP |
5776 | return 0; |
5777 | } | |
5778 | ||
e449de87 | 5779 | static int show(int argc, char *argv[], void *userdata) { |
8559e61d YW |
5780 | bool new_line = false, ellipsized = false; |
5781 | SystemctlShowMode show_mode; | |
e3e0314b | 5782 | int r, ret = 0; |
4fbd7192 | 5783 | sd_bus *bus; |
48220598 | 5784 | |
e449de87 | 5785 | assert(argv); |
48220598 | 5786 | |
8559e61d | 5787 | show_mode = systemctl_show_mode_from_string(argv[0]); |
baaa35ad ZJS |
5788 | if (show_mode < 0) |
5789 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
5790 | "Invalid argument."); | |
e449de87 | 5791 | |
baaa35ad ZJS |
5792 | if (show_mode == SYSTEMCTL_SHOW_HELP && argc <= 1) |
5793 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
77102db2 ZJS |
5794 | "'help' command expects one or more unit names.\n" |
5795 | "(Alternatively, help for systemctl itself may be shown with --help)"); | |
61cbdc4b | 5796 | |
d2ad7e1b ZJS |
5797 | r = acquire_bus(BUS_MANAGER, &bus); |
5798 | if (r < 0) | |
5799 | return r; | |
5800 | ||
0221d68a | 5801 | (void) pager_open(arg_pager_flags); |
ec14911e | 5802 | |
4fbd7192 | 5803 | /* If no argument is specified inspect the manager itself */ |
8559e61d YW |
5804 | if (show_mode == SYSTEMCTL_SHOW_PROPERTIES && argc <= 1) |
5805 | return show_one(bus, "/org/freedesktop/systemd1", NULL, show_mode, &new_line, &ellipsized); | |
48220598 | 5806 | |
8559e61d | 5807 | if (show_mode == SYSTEMCTL_SHOW_STATUS && argc <= 1) { |
8fcf784d | 5808 | |
8fcf784d LP |
5809 | show_system_status(bus); |
5810 | new_line = true; | |
5811 | ||
5812 | if (arg_all) | |
8559e61d | 5813 | ret = show_all(bus, &new_line, &ellipsized); |
8fcf784d | 5814 | } else { |
e3e0314b ZJS |
5815 | _cleanup_free_ char **patterns = NULL; |
5816 | char **name; | |
5817 | ||
e449de87 | 5818 | STRV_FOREACH(name, strv_skip(argv, 1)) { |
3dced37b | 5819 | _cleanup_free_ char *path = NULL, *unit = NULL; |
94e0bd7d | 5820 | uint32_t id; |
48220598 | 5821 | |
94e0bd7d | 5822 | if (safe_atou32(*name, &id) < 0) { |
e3e0314b | 5823 | if (strv_push(&patterns, *name) < 0) |
94e0bd7d | 5824 | return log_oom(); |
48220598 | 5825 | |
e3e0314b | 5826 | continue; |
8559e61d | 5827 | } else if (show_mode == SYSTEMCTL_SHOW_PROPERTIES) { |
94e0bd7d | 5828 | /* Interpret as job id */ |
3dced37b | 5829 | if (asprintf(&path, "/org/freedesktop/systemd1/job/%u", id) < 0) |
94e0bd7d | 5830 | return log_oom(); |
48220598 | 5831 | |
94e0bd7d ZJS |
5832 | } else { |
5833 | /* Interpret as PID */ | |
3dced37b | 5834 | r = get_unit_dbus_path_by_pid(bus, id, &path); |
373d32c9 | 5835 | if (r < 0) { |
94e0bd7d | 5836 | ret = r; |
373d32c9 LP |
5837 | continue; |
5838 | } | |
3dced37b LP |
5839 | |
5840 | r = unit_name_from_dbus_path(path, &unit); | |
5841 | if (r < 0) | |
5842 | return log_oom(); | |
94e0bd7d | 5843 | } |
f74294c1 | 5844 | |
8559e61d | 5845 | r = show_one(bus, path, unit, show_mode, &new_line, &ellipsized); |
5bb75bc7 ZJS |
5846 | if (r < 0) |
5847 | return r; | |
5848 | else if (r > 0 && ret == 0) | |
5849 | ret = r; | |
48220598 | 5850 | } |
94e0bd7d | 5851 | |
e3e0314b ZJS |
5852 | if (!strv_isempty(patterns)) { |
5853 | _cleanup_strv_free_ char **names = NULL; | |
5854 | ||
1d56bc09 | 5855 | r = expand_names(bus, patterns, NULL, &names, NULL); |
e3e0314b | 5856 | if (r < 0) |
691395d8 | 5857 | return log_error_errno(r, "Failed to expand names: %m"); |
e3e0314b | 5858 | |
e9c387c8 KK |
5859 | r = maybe_extend_with_unit_dependencies(bus, &names); |
5860 | if (r < 0) | |
5861 | return r; | |
5862 | ||
e3e0314b | 5863 | STRV_FOREACH(name, names) { |
3dced37b | 5864 | _cleanup_free_ char *path; |
e3e0314b | 5865 | |
3dced37b LP |
5866 | path = unit_dbus_path_from_name(*name); |
5867 | if (!path) | |
e3e0314b ZJS |
5868 | return log_oom(); |
5869 | ||
8559e61d | 5870 | r = show_one(bus, path, *name, show_mode, &new_line, &ellipsized); |
5bb75bc7 ZJS |
5871 | if (r < 0) |
5872 | return r; | |
3dced37b | 5873 | if (r > 0 && ret == 0) |
5bb75bc7 | 5874 | ret = r; |
e3e0314b ZJS |
5875 | } |
5876 | } | |
5877 | } | |
5878 | ||
94e0bd7d ZJS |
5879 | if (ellipsized && !arg_quiet) |
5880 | printf("Hint: Some lines were ellipsized, use -l to show in full.\n"); | |
48220598 | 5881 | |
22f4096c | 5882 | return ret; |
0183528f LP |
5883 | } |
5884 | ||
e449de87 | 5885 | static int cat(int argc, char *argv[], void *userdata) { |
8e766630 | 5886 | _cleanup_(lookup_paths_free) LookupPaths lp = {}; |
15ef1144 LP |
5887 | _cleanup_strv_free_ char **names = NULL; |
5888 | char **name; | |
4fbd7192 LP |
5889 | sd_bus *bus; |
5890 | bool first = true; | |
a25457f5 | 5891 | int r, rc = 0; |
15ef1144 | 5892 | |
4c70211c | 5893 | /* Include all units by default — i.e. continue as if the --all |
4eb53120 FS |
5894 | * option was used */ |
5895 | if (strv_isempty(arg_states)) | |
5896 | arg_all = true; | |
5897 | ||
2f50e59f LP |
5898 | if (arg_transport != BUS_TRANSPORT_LOCAL) |
5899 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot remotely cat units."); | |
3e495a66 | 5900 | |
4943d143 | 5901 | r = lookup_paths_init(&lp, arg_scope, 0, arg_root); |
ad2a0358 | 5902 | if (r < 0) |
c51932be | 5903 | return log_error_errno(r, "Failed to determine unit paths: %m"); |
ad2a0358 | 5904 | |
4fbd7192 | 5905 | r = acquire_bus(BUS_MANAGER, &bus); |
15ef1144 | 5906 | if (r < 0) |
4fbd7192 | 5907 | return r; |
15ef1144 | 5908 | |
1d56bc09 | 5909 | r = expand_names(bus, strv_skip(argv, 1), NULL, &names, NULL); |
4fbd7192 LP |
5910 | if (r < 0) |
5911 | return log_error_errno(r, "Failed to expand names: %m"); | |
ad2a0358 | 5912 | |
e9c387c8 KK |
5913 | r = maybe_extend_with_unit_dependencies(bus, &names); |
5914 | if (r < 0) | |
5915 | return r; | |
5916 | ||
0221d68a | 5917 | (void) pager_open(arg_pager_flags); |
15ef1144 LP |
5918 | |
5919 | STRV_FOREACH(name, names) { | |
ad2a0358 | 5920 | _cleanup_free_ char *fragment_path = NULL; |
15ef1144 | 5921 | _cleanup_strv_free_ char **dropin_paths = NULL; |
15ef1144 | 5922 | |
02a126a3 | 5923 | r = unit_find_paths(bus, *name, &lp, false, &fragment_path, &dropin_paths); |
86f004fb | 5924 | if (r == -ERFKILL) { |
02a126a3 LP |
5925 | printf("%s# Unit %s is masked%s.\n", |
5926 | ansi_highlight_magenta(), | |
5927 | *name, | |
5928 | ansi_normal()); | |
5929 | continue; | |
5930 | } | |
5931 | if (r == -EKEYREJECTED) { | |
5932 | printf("%s# Unit %s could not be loaded.%s\n", | |
86f004fb ZJS |
5933 | ansi_highlight_magenta(), |
5934 | *name, | |
5935 | ansi_normal()); | |
5936 | continue; | |
5937 | } | |
ad2a0358 ZJS |
5938 | if (r < 0) |
5939 | return r; | |
a25457f5 FS |
5940 | if (r == 0) { |
5941 | /* Skip units which have no on-disk counterpart, but | |
5942 | * propagate the error to the user */ | |
5943 | rc = -ENOENT; | |
5944 | continue; | |
5945 | } | |
15ef1144 LP |
5946 | |
5947 | if (first) | |
5948 | first = false; | |
5949 | else | |
5950 | puts(""); | |
5951 | ||
5b9635d1 | 5952 | if (need_daemon_reload(bus, *name) > 0) /* ignore errors (<0), this is informational output */ |
e100155d LW |
5953 | fprintf(stderr, |
5954 | "%s# Warning: %s changed on disk, the version systemd has loaded is outdated.\n" | |
5955 | "%s# This output shows the current version of the unit's original fragment and drop-in files.\n" | |
5956 | "%s# If fragments or drop-ins were added or removed, they are not properly reflected in this output.\n" | |
5957 | "%s# Run 'systemctl%s daemon-reload' to reload units.%s\n", | |
5958 | ansi_highlight_red(), | |
5959 | *name, | |
5960 | ansi_highlight_red(), | |
5961 | ansi_highlight_red(), | |
5962 | ansi_highlight_red(), | |
5963 | arg_scope == UNIT_FILE_SYSTEM ? "" : " --user", | |
5964 | ansi_normal()); | |
5965 | ||
854a42fb | 5966 | r = cat_files(fragment_path, dropin_paths, 0); |
81f5e513 ZJS |
5967 | if (r < 0) |
5968 | return r; | |
15ef1144 LP |
5969 | } |
5970 | ||
a25457f5 | 5971 | return rc; |
15ef1144 LP |
5972 | } |
5973 | ||
e449de87 | 5974 | static int set_property(int argc, char *argv[], void *userdata) { |
4afd3348 LP |
5975 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
5976 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
68372da6 | 5977 | _cleanup_free_ char *n = NULL; |
89ada3ba | 5978 | UnitType t; |
4fbd7192 | 5979 | sd_bus *bus; |
8e2af478 LP |
5980 | int r; |
5981 | ||
4fbd7192 LP |
5982 | r = acquire_bus(BUS_MANAGER, &bus); |
5983 | if (r < 0) | |
5984 | return r; | |
5985 | ||
8a4b13c5 | 5986 | polkit_agent_open_maybe(); |
d2ad7e1b | 5987 | |
8010c205 | 5988 | r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "SetUnitProperties"); |
f459b602 MAP |
5989 | if (r < 0) |
5990 | return bus_log_create_error(r); | |
8e2af478 | 5991 | |
37cbc1d5 | 5992 | r = unit_name_mangle(argv[1], arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN, &n); |
7410616c LP |
5993 | if (r < 0) |
5994 | return log_error_errno(r, "Failed to mangle unit name: %m"); | |
68372da6 | 5995 | |
89ada3ba | 5996 | t = unit_name_to_type(n); |
2f50e59f LP |
5997 | if (t < 0) |
5998 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid unit type: %s", n); | |
89ada3ba | 5999 | |
f459b602 MAP |
6000 | r = sd_bus_message_append(m, "sb", n, arg_runtime); |
6001 | if (r < 0) | |
6002 | return bus_log_create_error(r); | |
8e2af478 | 6003 | |
f459b602 MAP |
6004 | r = sd_bus_message_open_container(m, SD_BUS_TYPE_ARRAY, "(sv)"); |
6005 | if (r < 0) | |
6006 | return bus_log_create_error(r); | |
8e2af478 | 6007 | |
89ada3ba | 6008 | r = bus_append_unit_property_assignment_many(m, t, strv_skip(argv, 2)); |
8673cf13 LP |
6009 | if (r < 0) |
6010 | return r; | |
8e2af478 | 6011 | |
f459b602 MAP |
6012 | r = sd_bus_message_close_container(m); |
6013 | if (r < 0) | |
6014 | return bus_log_create_error(r); | |
8e2af478 | 6015 | |
c49b30a2 | 6016 | r = sd_bus_call(bus, m, 0, &error, NULL); |
691395d8 LP |
6017 | if (r < 0) |
6018 | return log_error_errno(r, "Failed to set unit properties on %s: %s", n, bus_error_message(&error, r)); | |
8e2af478 LP |
6019 | |
6020 | return 0; | |
6021 | } | |
6022 | ||
e449de87 | 6023 | static int daemon_reload(int argc, char *argv[], void *userdata) { |
4afd3348 | 6024 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
2853b60a | 6025 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
7e4249b9 | 6026 | const char *method; |
4fbd7192 | 6027 | sd_bus *bus; |
f459b602 | 6028 | int r; |
7e4249b9 | 6029 | |
4fbd7192 LP |
6030 | r = acquire_bus(BUS_MANAGER, &bus); |
6031 | if (r < 0) | |
6032 | return r; | |
6033 | ||
8a4b13c5 | 6034 | polkit_agent_open_maybe(); |
d2ad7e1b | 6035 | |
2853b60a LP |
6036 | switch (arg_action) { |
6037 | ||
6038 | case ACTION_RELOAD: | |
e4b61340 | 6039 | method = "Reload"; |
2853b60a LP |
6040 | break; |
6041 | ||
6042 | case ACTION_REEXEC: | |
e4b61340 | 6043 | method = "Reexecute"; |
2853b60a LP |
6044 | break; |
6045 | ||
6046 | case ACTION_SYSTEMCTL: | |
6047 | method = streq(argv[0], "daemon-reexec") ? "Reexecute" : | |
6048 | /* "daemon-reload" */ "Reload"; | |
6049 | break; | |
e4b61340 | 6050 | |
2853b60a LP |
6051 | default: |
6052 | assert_not_reached("Unexpected action"); | |
e4b61340 | 6053 | } |
7e4249b9 | 6054 | |
8010c205 | 6055 | r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, method); |
2853b60a LP |
6056 | if (r < 0) |
6057 | return bus_log_create_error(r); | |
6058 | ||
6059 | /* Note we use an extra-long timeout here. This is because a reload or reexec means generators are rerun which | |
6060 | * are timed out after DEFAULT_TIMEOUT_USEC. Let's use twice that time here, so that the generators can have | |
6061 | * their timeout, and for everything else there's the same time budget in place. */ | |
6062 | ||
6063 | r = sd_bus_call(bus, m, DEFAULT_TIMEOUT_USEC * 2, &error, NULL); | |
6064 | ||
6065 | /* On reexecution, we expect a disconnect, not a reply */ | |
6066 | if (IN_SET(r, -ETIMEDOUT, -ECONNRESET) && streq(method, "Reexecute")) | |
6067 | r = 0; | |
6068 | ||
6069 | if (r < 0 && arg_action == ACTION_SYSTEMCTL) | |
6070 | return log_error_errno(r, "Failed to reload daemon: %s", bus_error_message(&error, r)); | |
6071 | ||
6072 | /* Note that for the legacy commands (i.e. those with action != ACTION_SYSTEMCTL) we support fallbacks to the | |
6073 | * old ways of doing things, hence don't log any error in that case here. */ | |
6074 | ||
6075 | return r < 0 ? r : 0; | |
6076 | } | |
6077 | ||
6078 | static int trivial_method(int argc, char *argv[], void *userdata) { | |
6079 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
6080 | const char *method; | |
6081 | sd_bus *bus; | |
6082 | int r; | |
6083 | ||
1ae17672 ZJS |
6084 | if (arg_dry_run) |
6085 | return 0; | |
6086 | ||
2853b60a LP |
6087 | r = acquire_bus(BUS_MANAGER, &bus); |
6088 | if (r < 0) | |
6089 | return r; | |
6090 | ||
8a4b13c5 | 6091 | polkit_agent_open_maybe(); |
d2ad7e1b | 6092 | |
2853b60a LP |
6093 | method = |
6094 | streq(argv[0], "clear-jobs") || | |
6095 | streq(argv[0], "cancel") ? "ClearJobs" : | |
6096 | streq(argv[0], "reset-failed") ? "ResetFailed" : | |
6097 | streq(argv[0], "halt") ? "Halt" : | |
6098 | streq(argv[0], "reboot") ? "Reboot" : | |
6099 | streq(argv[0], "kexec") ? "KExec" : | |
6100 | streq(argv[0], "exit") ? "Exit" : | |
6101 | /* poweroff */ "PowerOff"; | |
6102 | ||
8010c205 | 6103 | r = bus_call_method(bus, bus_systemd_mgr, method, &error, NULL, NULL); |
2853b60a LP |
6104 | if (r < 0 && arg_action == ACTION_SYSTEMCTL) |
6105 | return log_error_errno(r, "Failed to execute operation: %s", bus_error_message(&error, r)); | |
6106 | ||
6107 | /* Note that for the legacy commands (i.e. those with action != ACTION_SYSTEMCTL) we support fallbacks to the | |
6108 | * old ways of doing things, hence don't log any error in that case here. */ | |
7e4249b9 | 6109 | |
0a9776c2 | 6110 | return r < 0 ? r : 0; |
7e4249b9 LP |
6111 | } |
6112 | ||
e449de87 | 6113 | static int reset_failed(int argc, char *argv[], void *userdata) { |
e3e0314b | 6114 | _cleanup_strv_free_ char **names = NULL; |
4fbd7192 | 6115 | sd_bus *bus; |
f84190d8 | 6116 | char **name; |
e3e0314b | 6117 | int r, q; |
5632e374 | 6118 | |
e449de87 | 6119 | if (argc <= 1) |
2853b60a | 6120 | return trivial_method(argc, argv, userdata); |
5632e374 | 6121 | |
4fbd7192 LP |
6122 | r = acquire_bus(BUS_MANAGER, &bus); |
6123 | if (r < 0) | |
6124 | return r; | |
6125 | ||
8a4b13c5 | 6126 | polkit_agent_open_maybe(); |
d2ad7e1b | 6127 | |
1d56bc09 | 6128 | r = expand_names(bus, strv_skip(argv, 1), NULL, &names, NULL); |
e3e0314b | 6129 | if (r < 0) |
691395d8 | 6130 | return log_error_errno(r, "Failed to expand names: %m"); |
f84190d8 | 6131 | |
e3e0314b | 6132 | STRV_FOREACH(name, names) { |
4afd3348 | 6133 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
342641fb | 6134 | |
8010c205 | 6135 | q = bus_call_method(bus, bus_systemd_mgr, "ResetFailedUnit", &error, NULL, "s", *name); |
e3e0314b | 6136 | if (q < 0) { |
691395d8 | 6137 | log_error_errno(q, "Failed to reset failed state of unit %s: %s", *name, bus_error_message(&error, q)); |
e3e0314b ZJS |
6138 | if (r == 0) |
6139 | r = q; | |
f459b602 | 6140 | } |
5632e374 LP |
6141 | } |
6142 | ||
e3e0314b | 6143 | return r; |
5632e374 LP |
6144 | } |
6145 | ||
804ee07c ZJS |
6146 | static int print_variable(const char *s) { |
6147 | const char *sep; | |
6148 | _cleanup_free_ char *esc = NULL; | |
6149 | ||
6150 | sep = strchr(s, '='); | |
baaa35ad ZJS |
6151 | if (!sep) |
6152 | return log_error_errno(SYNTHETIC_ERRNO(EUCLEAN), | |
6153 | "Invalid environment block"); | |
804ee07c ZJS |
6154 | |
6155 | esc = shell_maybe_quote(sep + 1, ESCAPE_POSIX); | |
6156 | if (!esc) | |
6157 | return log_oom(); | |
6158 | ||
6159 | printf("%.*s=%s\n", (int)(sep-s), s, esc); | |
6160 | return 0; | |
6161 | } | |
6162 | ||
e449de87 | 6163 | static int show_environment(int argc, char *argv[], void *userdata) { |
4afd3348 LP |
6164 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
6165 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
f459b602 | 6166 | const char *text; |
4fbd7192 | 6167 | sd_bus *bus; |
7e4249b9 | 6168 | int r; |
7e4249b9 | 6169 | |
4fbd7192 LP |
6170 | r = acquire_bus(BUS_MANAGER, &bus); |
6171 | if (r < 0) | |
6172 | return r; | |
6173 | ||
0221d68a | 6174 | (void) pager_open(arg_pager_flags); |
d2ad7e1b | 6175 | |
8010c205 | 6176 | r = bus_get_property(bus, bus_systemd_mgr, "Environment", &error, &reply, "as"); |
691395d8 LP |
6177 | if (r < 0) |
6178 | return log_error_errno(r, "Failed to get environment: %s", bus_error_message(&error, r)); | |
7e4249b9 | 6179 | |
f459b602 MAP |
6180 | r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "s"); |
6181 | if (r < 0) | |
6182 | return bus_log_parse_error(r); | |
7e4249b9 | 6183 | |
804ee07c ZJS |
6184 | while ((r = sd_bus_message_read_basic(reply, SD_BUS_TYPE_STRING, &text)) > 0) { |
6185 | r = print_variable(text); | |
6186 | if (r < 0) | |
6187 | return r; | |
6188 | } | |
f459b602 MAP |
6189 | if (r < 0) |
6190 | return bus_log_parse_error(r); | |
7e4249b9 | 6191 | |
f459b602 MAP |
6192 | r = sd_bus_message_exit_container(reply); |
6193 | if (r < 0) | |
6194 | return bus_log_parse_error(r); | |
7e4249b9 | 6195 | |
f84190d8 | 6196 | return 0; |
7e4249b9 LP |
6197 | } |
6198 | ||
e449de87 | 6199 | static int switch_root(int argc, char *argv[], void *userdata) { |
4afd3348 | 6200 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
f39d4a08 HH |
6201 | _cleanup_free_ char *cmdline_init = NULL; |
6202 | const char *root, *init; | |
4fbd7192 | 6203 | sd_bus *bus; |
f459b602 | 6204 | int r; |
957eb8ca | 6205 | |
2f50e59f LP |
6206 | if (arg_transport != BUS_TRANSPORT_LOCAL) |
6207 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot switch root remotely."); | |
4fbd7192 | 6208 | |
2f50e59f LP |
6209 | if (argc < 2 || argc > 3) |
6210 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Wrong number of arguments."); | |
957eb8ca | 6211 | |
e449de87 | 6212 | root = argv[1]; |
13068da8 | 6213 | |
e449de87 LP |
6214 | if (argc >= 3) |
6215 | init = argv[2]; | |
13068da8 | 6216 | else { |
01771226 | 6217 | r = proc_cmdline_get_key("init", 0, &cmdline_init); |
f39d4a08 | 6218 | if (r < 0) |
da927ba9 | 6219 | log_debug_errno(r, "Failed to parse /proc/cmdline: %m"); |
13068da8 | 6220 | |
f39d4a08 | 6221 | init = cmdline_init; |
13068da8 | 6222 | } |
f459b602 | 6223 | |
3c6f7c34 | 6224 | init = empty_to_null(init); |
f39d4a08 HH |
6225 | if (init) { |
6226 | const char *root_systemd_path = NULL, *root_init_path = NULL; | |
6227 | ||
270384b2 YW |
6228 | root_systemd_path = prefix_roota(root, "/" SYSTEMD_BINARY_PATH); |
6229 | root_init_path = prefix_roota(root, init); | |
f39d4a08 HH |
6230 | |
6231 | /* If the passed init is actually the same as the | |
6232 | * systemd binary, then let's suppress it. */ | |
e3f791a2 | 6233 | if (files_same(root_init_path, root_systemd_path, 0) > 0) |
f39d4a08 HH |
6234 | init = NULL; |
6235 | } | |
13068da8 | 6236 | |
d86e4c97 LP |
6237 | /* Instruct PID1 to exclude us from its killing spree applied during the transition. Otherwise we |
6238 | * would exit with a failure status even though the switch to the new root has succeed. */ | |
6239 | assert(saved_argv); | |
6240 | assert(saved_argv[0]); | |
6241 | saved_argv[0][0] = '@'; | |
acc28e2e | 6242 | |
4fbd7192 LP |
6243 | r = acquire_bus(BUS_MANAGER, &bus); |
6244 | if (r < 0) | |
6245 | return r; | |
6246 | ||
8a7a9cea ZJS |
6247 | /* If we are slow to exit after the root switch, the new systemd instance |
6248 | * will send us a signal to terminate. Just ignore it and exit normally. | |
6249 | * This way the unit does not end up as failed. | |
6250 | */ | |
6251 | r = ignore_signals(SIGTERM, -1); | |
6252 | if (r < 0) | |
6253 | log_warning_errno(r, "Failed to change disposition of SIGTERM to ignore: %m"); | |
6254 | ||
f39d4a08 | 6255 | log_debug("Switching root - root: %s; init: %s", root, strna(init)); |
957eb8ca | 6256 | |
8010c205 | 6257 | r = bus_call_method(bus, bus_systemd_mgr, "SwitchRoot", &error, NULL, "ss", root, init); |
8a7a9cea ZJS |
6258 | if (r < 0) { |
6259 | (void) default_signals(SIGTERM, -1); | |
6260 | ||
691395d8 | 6261 | return log_error_errno(r, "Failed to switch root: %s", bus_error_message(&error, r)); |
8a7a9cea | 6262 | } |
f459b602 MAP |
6263 | |
6264 | return 0; | |
957eb8ca LP |
6265 | } |
6266 | ||
38fcb7f7 ZJS |
6267 | static int log_level(int argc, char *argv[], void *userdata) { |
6268 | sd_bus *bus; | |
6269 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
6270 | int r; | |
6271 | ||
6272 | r = acquire_bus(BUS_MANAGER, &bus); | |
6273 | if (r < 0) | |
6274 | return r; | |
6275 | ||
6276 | if (argc == 1) { | |
6277 | _cleanup_free_ char *level = NULL; | |
6278 | ||
8010c205 | 6279 | r = bus_get_property_string(bus, bus_systemd_mgr, "LogLevel", &error, &level); |
38fcb7f7 ZJS |
6280 | if (r < 0) |
6281 | return log_error_errno(r, "Failed to get log level: %s", bus_error_message(&error, r)); | |
6282 | ||
6283 | puts(level); | |
6284 | ||
6285 | } else { | |
6286 | assert(argc == 2); | |
6287 | ||
8010c205 | 6288 | r = bus_set_property(bus, bus_systemd_mgr, "LogLevel", &error, "s", argv[1]); |
38fcb7f7 ZJS |
6289 | if (r < 0) |
6290 | return log_error_errno(r, "Failed to set log level: %s", bus_error_message(&error, r)); | |
6291 | } | |
6292 | ||
6293 | return 0; | |
6294 | } | |
6295 | ||
6296 | static int log_target(int argc, char *argv[], void *userdata) { | |
6297 | sd_bus *bus; | |
6298 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
6299 | int r; | |
6300 | ||
6301 | r = acquire_bus(BUS_MANAGER, &bus); | |
6302 | if (r < 0) | |
6303 | return r; | |
6304 | ||
6305 | if (argc == 1) { | |
6306 | _cleanup_free_ char *target = NULL; | |
6307 | ||
8010c205 | 6308 | r = bus_get_property_string(bus, bus_systemd_mgr, "LogTarget", &error, &target); |
38fcb7f7 ZJS |
6309 | if (r < 0) |
6310 | return log_error_errno(r, "Failed to get log target: %s", bus_error_message(&error, r)); | |
6311 | ||
6312 | puts(target); | |
6313 | ||
6314 | } else { | |
6315 | assert(argc == 2); | |
6316 | ||
8010c205 | 6317 | r = bus_set_property(bus, bus_systemd_mgr, "LogTarget", &error, "s", argv[1]); |
38fcb7f7 ZJS |
6318 | if (r < 0) |
6319 | return log_error_errno(r, "Failed to set log target: %s", bus_error_message(&error, r)); | |
6320 | } | |
6321 | ||
6322 | return 0; | |
6323 | } | |
6324 | ||
6ab86319 ZJS |
6325 | static int service_watchdogs(int argc, char *argv[], void *userdata) { |
6326 | sd_bus *bus; | |
6327 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
6328 | int b, r; | |
6329 | ||
6330 | assert(argv); | |
6331 | ||
6332 | r = acquire_bus(BUS_MANAGER, &bus); | |
6333 | if (r < 0) | |
6334 | return r; | |
6335 | ||
6336 | if (argc == 1) { | |
6337 | /* get ServiceWatchdogs */ | |
8010c205 | 6338 | r = bus_get_property_trivial(bus, bus_systemd_mgr, "ServiceWatchdogs", &error, 'b', &b); |
6ab86319 ZJS |
6339 | if (r < 0) |
6340 | return log_error_errno(r, "Failed to get service-watchdog state: %s", bus_error_message(&error, r)); | |
6341 | ||
6342 | printf("%s\n", yes_no(!!b)); | |
6343 | ||
6344 | } else { | |
6345 | /* set ServiceWatchdogs */ | |
6346 | assert(argc == 2); | |
6347 | ||
6348 | b = parse_boolean(argv[1]); | |
6349 | if (b < 0) | |
6350 | return log_error_errno(b, "Failed to parse service-watchdogs argument: %m"); | |
6351 | ||
8010c205 | 6352 | r = bus_set_property(bus, bus_systemd_mgr, "ServiceWatchdogs", &error, "b", b); |
6ab86319 ZJS |
6353 | if (r < 0) |
6354 | return log_error_errno(r, "Failed to set service-watchdog state: %s", bus_error_message(&error, r)); | |
6355 | } | |
6356 | ||
6357 | return 0; | |
6358 | } | |
6359 | ||
e449de87 | 6360 | static int set_environment(int argc, char *argv[], void *userdata) { |
4afd3348 LP |
6361 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
6362 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; | |
7e4249b9 | 6363 | const char *method; |
4fbd7192 | 6364 | sd_bus *bus; |
31e767f7 LP |
6365 | int r; |
6366 | ||
e449de87 LP |
6367 | assert(argc > 1); |
6368 | assert(argv); | |
7e4249b9 | 6369 | |
4fbd7192 LP |
6370 | r = acquire_bus(BUS_MANAGER, &bus); |
6371 | if (r < 0) | |
6372 | return r; | |
6373 | ||
8a4b13c5 | 6374 | polkit_agent_open_maybe(); |
d2ad7e1b | 6375 | |
e449de87 | 6376 | method = streq(argv[0], "set-environment") |
7e4249b9 LP |
6377 | ? "SetEnvironment" |
6378 | : "UnsetEnvironment"; | |
6379 | ||
8010c205 | 6380 | r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, method); |
f459b602 MAP |
6381 | if (r < 0) |
6382 | return bus_log_create_error(r); | |
7e4249b9 | 6383 | |
e449de87 | 6384 | r = sd_bus_message_append_strv(m, strv_skip(argv, 1)); |
31e767f7 | 6385 | if (r < 0) |
f459b602 | 6386 | return bus_log_create_error(r); |
7e4249b9 | 6387 | |
c49b30a2 | 6388 | r = sd_bus_call(bus, m, 0, &error, NULL); |
691395d8 LP |
6389 | if (r < 0) |
6390 | return log_error_errno(r, "Failed to set environment: %s", bus_error_message(&error, r)); | |
7e4249b9 | 6391 | |
f84190d8 | 6392 | return 0; |
7e4249b9 LP |
6393 | } |
6394 | ||
e449de87 | 6395 | static int import_environment(int argc, char *argv[], void *userdata) { |
4afd3348 LP |
6396 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
6397 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; | |
4fbd7192 | 6398 | sd_bus *bus; |
ac3efa8a LP |
6399 | int r; |
6400 | ||
4fbd7192 LP |
6401 | r = acquire_bus(BUS_MANAGER, &bus); |
6402 | if (r < 0) | |
6403 | return r; | |
6404 | ||
8a4b13c5 | 6405 | polkit_agent_open_maybe(); |
d2ad7e1b | 6406 | |
8010c205 | 6407 | r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "SetEnvironment"); |
ac3efa8a LP |
6408 | if (r < 0) |
6409 | return bus_log_create_error(r); | |
6410 | ||
e449de87 | 6411 | if (argc < 2) |
ac3efa8a LP |
6412 | r = sd_bus_message_append_strv(m, environ); |
6413 | else { | |
6414 | char **a, **b; | |
6415 | ||
6416 | r = sd_bus_message_open_container(m, 'a', "s"); | |
6417 | if (r < 0) | |
6418 | return bus_log_create_error(r); | |
6419 | ||
e449de87 | 6420 | STRV_FOREACH(a, strv_skip(argv, 1)) { |
ac3efa8a | 6421 | |
2f50e59f LP |
6422 | if (!env_name_is_valid(*a)) |
6423 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Not a valid environment variable name: %s", *a); | |
ac3efa8a LP |
6424 | |
6425 | STRV_FOREACH(b, environ) { | |
6426 | const char *eq; | |
6427 | ||
6428 | eq = startswith(*b, *a); | |
6429 | if (eq && *eq == '=') { | |
6430 | ||
6431 | r = sd_bus_message_append(m, "s", *b); | |
6432 | if (r < 0) | |
6433 | return bus_log_create_error(r); | |
6434 | ||
6435 | break; | |
6436 | } | |
6437 | } | |
6438 | } | |
6439 | ||
6440 | r = sd_bus_message_close_container(m); | |
6441 | } | |
6442 | if (r < 0) | |
6443 | return bus_log_create_error(r); | |
6444 | ||
6445 | r = sd_bus_call(bus, m, 0, &error, NULL); | |
691395d8 LP |
6446 | if (r < 0) |
6447 | return log_error_errno(r, "Failed to import environment: %s", bus_error_message(&error, r)); | |
ac3efa8a LP |
6448 | |
6449 | return 0; | |
6450 | } | |
6451 | ||
cbb13b2a | 6452 | static int enable_sysv_units(const char *verb, char **args) { |
729e3769 | 6453 | int r = 0; |
ee5762e3 | 6454 | |
349cc4a5 | 6455 | #if HAVE_SYSV_COMPAT |
8e766630 | 6456 | _cleanup_(lookup_paths_free) LookupPaths paths = {}; |
e735decc LP |
6457 | unsigned f = 0; |
6458 | ||
6459 | /* Processes all SysV units, and reshuffles the array so that afterwards only the native units remain */ | |
ee5762e3 | 6460 | |
729e3769 LP |
6461 | if (arg_scope != UNIT_FILE_SYSTEM) |
6462 | return 0; | |
ee5762e3 | 6463 | |
b41b9d2a LP |
6464 | if (getenv_bool("SYSTEMCTL_SKIP_SYSV") > 0) |
6465 | return 0; | |
6466 | ||
4c315c2c IS |
6467 | if (!STR_IN_SET(verb, |
6468 | "enable", | |
6469 | "disable", | |
6470 | "is-enabled")) | |
729e3769 | 6471 | return 0; |
ee5762e3 | 6472 | |
4943d143 | 6473 | r = lookup_paths_init(&paths, arg_scope, LOOKUP_PATHS_EXCLUDE_GENERATED, arg_root); |
729e3769 LP |
6474 | if (r < 0) |
6475 | return r; | |
ee5762e3 | 6476 | |
729e3769 | 6477 | r = 0; |
a644abed | 6478 | while (args[f]) { |
e735decc LP |
6479 | |
6480 | const char *argv[] = { | |
6481 | ROOTLIBEXECDIR "/systemd-sysv-install", | |
3483460c LP |
6482 | NULL, /* --root= */ |
6483 | NULL, /* verb */ | |
6484 | NULL, /* service */ | |
e735decc LP |
6485 | NULL, |
6486 | }; | |
6487 | ||
8d1ee648 | 6488 | _cleanup_free_ char *p = NULL, *q = NULL, *l = NULL, *v = NULL; |
729e3769 | 6489 | bool found_native = false, found_sysv; |
e735decc | 6490 | const char *name; |
729e3769 | 6491 | unsigned c = 1; |
729e3769 | 6492 | pid_t pid; |
e735decc | 6493 | int j; |
ee5762e3 | 6494 | |
a644abed | 6495 | name = args[f++]; |
ee5762e3 | 6496 | |
729e3769 LP |
6497 | if (!endswith(name, ".service")) |
6498 | continue; | |
ee5762e3 | 6499 | |
729e3769 LP |
6500 | if (path_is_absolute(name)) |
6501 | continue; | |
ee5762e3 | 6502 | |
e735decc | 6503 | j = unit_file_exists(arg_scope, &paths, name); |
76ec966f | 6504 | if (j < 0 && !IN_SET(j, -ELOOP, -ERFKILL, -EADDRNOTAVAIL)) |
105a1a36 | 6505 | return log_error_errno(j, "Failed to look up unit file state: %m"); |
3c6d8e57 | 6506 | found_native = j != 0; |
ee5762e3 | 6507 | |
e735decc LP |
6508 | /* If we have both a native unit and a SysV script, enable/disable them both (below); for is-enabled, |
6509 | * prefer the native unit */ | |
355ff449 | 6510 | if (found_native && streq(verb, "is-enabled")) |
729e3769 | 6511 | continue; |
ee5762e3 | 6512 | |
652ef298 | 6513 | p = path_join(arg_root, SYSTEM_SYSVINIT_PATH, name); |
0c6ea3a4 | 6514 | if (!p) |
60731f32 | 6515 | return log_oom(); |
ee5762e3 | 6516 | |
fbd0b64f | 6517 | p[strlen(p) - STRLEN(".service")] = 0; |
729e3769 | 6518 | found_sysv = access(p, F_OK) >= 0; |
4b6756a8 | 6519 | if (!found_sysv) |
729e3769 | 6520 | continue; |
71fad675 | 6521 | |
689e4e6a CR |
6522 | if (!arg_quiet) { |
6523 | if (found_native) | |
6524 | log_info("Synchronizing state of %s with SysV service script with %s.", name, argv[0]); | |
6525 | else | |
6526 | log_info("%s is not a native service, redirecting to systemd-sysv-install.", name); | |
6527 | } | |
ee5762e3 | 6528 | |
b1dffbb9 | 6529 | if (!isempty(arg_root)) { |
b910cc72 | 6530 | q = strjoin("--root=", arg_root); |
b1dffbb9 LP |
6531 | if (!q) |
6532 | return log_oom(); | |
6533 | ||
6534 | argv[c++] = q; | |
6535 | } | |
ee5762e3 | 6536 | |
8d1ee648 LP |
6537 | /* Let's copy the verb, since it's still pointing directly into the original argv[] array we |
6538 | * got passed, but safe_fork() is likely going to rewrite that for the new child */ | |
6539 | v = strdup(verb); | |
6540 | if (!v) | |
6541 | return log_oom(); | |
6542 | ||
6543 | argv[c++] = v; | |
2b6bf07d | 6544 | argv[c++] = basename(p); |
729e3769 | 6545 | argv[c] = NULL; |
ee5762e3 | 6546 | |
729e3769 | 6547 | l = strv_join((char**)argv, " "); |
60731f32 ZJS |
6548 | if (!l) |
6549 | return log_oom(); | |
ee5762e3 | 6550 | |
8ecc68f4 LP |
6551 | if (!arg_quiet) |
6552 | log_info("Executing: %s", l); | |
ee5762e3 | 6553 | |
0672e2c6 | 6554 | j = safe_fork("(sysv-install)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG, &pid); |
4c253ed1 | 6555 | if (j < 0) |
b6e1fff1 | 6556 | return j; |
4c253ed1 | 6557 | if (j == 0) { |
729e3769 | 6558 | /* Child */ |
729e3769 | 6559 | execv(argv[0], (char**) argv); |
cc7cb0ca | 6560 | log_error_errno(errno, "Failed to execute %s: %m", argv[0]); |
729e3769 LP |
6561 | _exit(EXIT_FAILURE); |
6562 | } | |
ee5762e3 | 6563 | |
2e87a1fd | 6564 | j = wait_for_terminate_and_check("sysv-install", pid, WAIT_LOG_ABNORMAL); |
d710aaf7 | 6565 | if (j < 0) |
2e87a1fd LP |
6566 | return j; |
6567 | if (streq(verb, "is-enabled")) { | |
b4a34311 | 6568 | if (j == EXIT_SUCCESS) { |
2e87a1fd LP |
6569 | if (!arg_quiet) |
6570 | puts("enabled"); | |
6571 | r = 1; | |
6572 | } else { | |
6573 | if (!arg_quiet) | |
6574 | puts("disabled"); | |
6575 | } | |
ee5762e3 | 6576 | |
b4a34311 | 6577 | } else if (j != EXIT_SUCCESS) |
2e87a1fd | 6578 | return -EBADE; /* We don't warn here, under the assumption the script already showed an explanation */ |
ee5762e3 | 6579 | |
355ff449 MP |
6580 | if (found_native) |
6581 | continue; | |
6582 | ||
a644abed | 6583 | /* Remove this entry, so that we don't try enabling it as native unit */ |
aba84331 LP |
6584 | assert(f > 0); |
6585 | f--; | |
6586 | assert(args[f] == name); | |
290cb8e8 | 6587 | strv_remove(args + f, name); |
729e3769 | 6588 | } |
ee5762e3 | 6589 | |
729e3769 LP |
6590 | #endif |
6591 | return r; | |
6592 | } | |
ee5762e3 | 6593 | |
df7c4eb6 | 6594 | static int mangle_names(const char *operation, char **original_names, char ***mangled_names) { |
a33fdebb | 6595 | char **i, **l, **name; |
7410616c | 6596 | int r; |
37370d0c | 6597 | |
7410616c | 6598 | l = i = new(char*, strv_length(original_names) + 1); |
a33fdebb | 6599 | if (!l) |
37370d0c VP |
6600 | return log_oom(); |
6601 | ||
37370d0c | 6602 | STRV_FOREACH(name, original_names) { |
44386fc1 LN |
6603 | |
6604 | /* When enabling units qualified path names are OK, | |
6605 | * too, hence allow them explicitly. */ | |
6606 | ||
7410616c | 6607 | if (is_path(*name)) { |
44386fc1 | 6608 | *i = strdup(*name); |
7410616c LP |
6609 | if (!*i) { |
6610 | strv_free(l); | |
6611 | return log_oom(); | |
6612 | } | |
6613 | } else { | |
df7c4eb6 ZJS |
6614 | r = unit_name_mangle_with_suffix(*name, operation, |
6615 | arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN, | |
6616 | ".service", i); | |
7410616c | 6617 | if (r < 0) { |
523f8cde | 6618 | *i = NULL; |
7410616c LP |
6619 | strv_free(l); |
6620 | return log_error_errno(r, "Failed to mangle unit name: %m"); | |
6621 | } | |
a33fdebb LP |
6622 | } |
6623 | ||
6624 | i++; | |
37370d0c | 6625 | } |
a33fdebb LP |
6626 | |
6627 | *i = NULL; | |
6628 | *mangled_names = l; | |
37370d0c VP |
6629 | |
6630 | return 0; | |
6631 | } | |
6632 | ||
decd7982 JR |
6633 | static int normalize_filenames(char **names) { |
6634 | char **u; | |
6635 | int r; | |
6636 | ||
6637 | STRV_FOREACH(u, names) | |
6638 | if (!path_is_absolute(*u)) { | |
6639 | char* normalized_path; | |
6640 | ||
baaa35ad ZJS |
6641 | if (!isempty(arg_root)) |
6642 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
6643 | "Non-absolute paths are not allowed when --root is used: %s", | |
6644 | *u); | |
decd7982 | 6645 | |
baaa35ad ZJS |
6646 | if (!strchr(*u,'/')) |
6647 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
6648 | "Link argument does contain at least one directory separator: %s", | |
6649 | *u); | |
decd7982 JR |
6650 | |
6651 | r = path_make_absolute_cwd(*u, &normalized_path); | |
6652 | if (r < 0) | |
6653 | return r; | |
6654 | ||
6655 | free_and_replace(*u, normalized_path); | |
6656 | } | |
6657 | ||
6658 | return 0; | |
6659 | } | |
6660 | ||
1d3c86c0 MS |
6661 | static int normalize_names(char **names, bool warn_if_path) { |
6662 | char **u; | |
6663 | bool was_path = false; | |
6664 | ||
6665 | STRV_FOREACH(u, names) { | |
6666 | int r; | |
6667 | ||
6668 | if (!is_path(*u)) | |
6669 | continue; | |
6670 | ||
6671 | r = free_and_strdup(u, basename(*u)); | |
6672 | if (r < 0) | |
6673 | return log_error_errno(r, "Failed to normalize unit file path: %m"); | |
6674 | ||
6675 | was_path = true; | |
6676 | } | |
6677 | ||
6678 | if (warn_if_path && was_path) | |
6679 | log_warning("Warning: Can't execute disable on the unit file path. Proceeding with the unit name."); | |
6680 | ||
6681 | return 0; | |
6682 | } | |
6683 | ||
173471b7 | 6684 | static int unit_exists(LookupPaths *lp, const char *unit) { |
d6568222 | 6685 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
f37f8a61 | 6686 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
d6568222 SS |
6687 | _cleanup_free_ char *path = NULL; |
6688 | static const struct bus_properties_map property_map[] = { | |
f37f8a61 YW |
6689 | { "LoadState", "s", NULL, offsetof(UnitStatusInfo, load_state) }, |
6690 | { "ActiveState", "s", NULL, offsetof(UnitStatusInfo, active_state)}, | |
d6568222 SS |
6691 | {}, |
6692 | }; | |
6693 | UnitStatusInfo info = {}; | |
6694 | sd_bus *bus; | |
6695 | int r; | |
6696 | ||
173471b7 ZJS |
6697 | if (unit_name_is_valid(unit, UNIT_NAME_TEMPLATE)) |
6698 | return unit_find_template_path(unit, lp, NULL, NULL); | |
6699 | ||
d6568222 SS |
6700 | path = unit_dbus_path_from_name(unit); |
6701 | if (!path) | |
6702 | return log_oom(); | |
6703 | ||
6704 | r = acquire_bus(BUS_MANAGER, &bus); | |
6705 | if (r < 0) | |
6706 | return r; | |
6707 | ||
a7e4861c | 6708 | r = bus_map_all_properties(bus, "org.freedesktop.systemd1", path, property_map, 0, &error, &m, &info); |
d6568222 SS |
6709 | if (r < 0) |
6710 | return log_error_errno(r, "Failed to get properties: %s", bus_error_message(&error, r)); | |
6711 | ||
d6568222 SS |
6712 | return !streq_ptr(info.load_state, "not-found") || !streq_ptr(info.active_state, "inactive"); |
6713 | } | |
6714 | ||
e449de87 | 6715 | static int enable_unit(int argc, char *argv[], void *userdata) { |
e3e0314b | 6716 | _cleanup_strv_free_ char **names = NULL; |
e449de87 | 6717 | const char *verb = argv[0]; |
729e3769 | 6718 | UnitFileChange *changes = NULL; |
da6053d0 | 6719 | size_t n_changes = 0; |
729e3769 | 6720 | int carries_install_info = -1; |
35b132e8 | 6721 | bool ignore_carries_install_info = arg_quiet; |
729e3769 | 6722 | int r; |
ee5762e3 | 6723 | |
e449de87 | 6724 | if (!argv[1]) |
ab5919fa MS |
6725 | return 0; |
6726 | ||
df7c4eb6 | 6727 | r = mangle_names("to enable", strv_skip(argv, 1), &names); |
3a05c0f9 | 6728 | if (r < 0) |
cbb13b2a VP |
6729 | return r; |
6730 | ||
e3e0314b | 6731 | r = enable_sysv_units(verb, names); |
cbb13b2a VP |
6732 | if (r < 0) |
6733 | return r; | |
3a05c0f9 | 6734 | |
c61b443d | 6735 | /* If the operation was fully executed by the SysV compat, let's finish early */ |
823e5fab FB |
6736 | if (strv_isempty(names)) { |
6737 | if (arg_no_reload || install_client_side()) | |
6738 | return 0; | |
6739 | return daemon_reload(argc, argv, userdata); | |
6740 | } | |
67d66210 | 6741 | |
1d3c86c0 MS |
6742 | if (streq(verb, "disable")) { |
6743 | r = normalize_names(names, true); | |
6744 | if (r < 0) | |
6745 | return r; | |
6746 | } | |
6747 | ||
decd7982 JR |
6748 | if (streq(verb, "link")) { |
6749 | r = normalize_filenames(names); | |
6750 | if (r < 0) | |
6751 | return r; | |
6752 | } | |
6753 | ||
4fbd7192 | 6754 | if (install_client_side()) { |
b3796dd8 JS |
6755 | UnitFileFlags flags; |
6756 | ||
6757 | flags = args_to_flags(); | |
729e3769 | 6758 | if (streq(verb, "enable")) { |
b3796dd8 | 6759 | r = unit_file_enable(arg_scope, flags, arg_root, names, &changes, &n_changes); |
729e3769 LP |
6760 | carries_install_info = r; |
6761 | } else if (streq(verb, "disable")) | |
b3796dd8 | 6762 | r = unit_file_disable(arg_scope, flags, arg_root, names, &changes, &n_changes); |
729e3769 | 6763 | else if (streq(verb, "reenable")) { |
b3796dd8 | 6764 | r = unit_file_reenable(arg_scope, flags, arg_root, names, &changes, &n_changes); |
729e3769 LP |
6765 | carries_install_info = r; |
6766 | } else if (streq(verb, "link")) | |
b3796dd8 | 6767 | r = unit_file_link(arg_scope, flags, arg_root, names, &changes, &n_changes); |
729e3769 | 6768 | else if (streq(verb, "preset")) { |
b3796dd8 | 6769 | r = unit_file_preset(arg_scope, flags, arg_root, names, arg_preset_mode, &changes, &n_changes); |
729e3769 | 6770 | } else if (streq(verb, "mask")) |
b3796dd8 | 6771 | r = unit_file_mask(arg_scope, flags, arg_root, names, &changes, &n_changes); |
729e3769 | 6772 | else if (streq(verb, "unmask")) |
b3796dd8 | 6773 | r = unit_file_unmask(arg_scope, flags, arg_root, names, &changes, &n_changes); |
344ca755 LP |
6774 | else if (streq(verb, "revert")) |
6775 | r = unit_file_revert(arg_scope, arg_root, names, &changes, &n_changes); | |
729e3769 LP |
6776 | else |
6777 | assert_not_reached("Unknown verb"); | |
ee5762e3 | 6778 | |
af3d8113 | 6779 | unit_file_dump_changes(r, verb, changes, n_changes, arg_quiet); |
d073dea0 | 6780 | if (r < 0) |
85b78539 | 6781 | goto finish; |
df77cdf0 | 6782 | r = 0; |
729e3769 | 6783 | } else { |
4afd3348 LP |
6784 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL, *m = NULL; |
6785 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
39207373 | 6786 | bool expect_carries_install_info = false; |
344ca755 | 6787 | bool send_runtime = true, send_force = true, send_preset_mode = false; |
718db961 | 6788 | const char *method; |
4fbd7192 | 6789 | sd_bus *bus; |
729e3769 | 6790 | |
d6568222 | 6791 | if (STR_IN_SET(verb, "mask", "unmask")) { |
f8d6cb48 | 6792 | char **name; |
8e766630 | 6793 | _cleanup_(lookup_paths_free) LookupPaths lp = {}; |
173471b7 ZJS |
6794 | |
6795 | r = lookup_paths_init(&lp, arg_scope, 0, arg_root); | |
d6568222 SS |
6796 | if (r < 0) |
6797 | return r; | |
f8d6cb48 ZJS |
6798 | |
6799 | STRV_FOREACH(name, names) { | |
173471b7 | 6800 | r = unit_exists(&lp, *name); |
f8d6cb48 ZJS |
6801 | if (r < 0) |
6802 | return r; | |
6803 | if (r == 0) | |
25c59b5d | 6804 | log_notice("Unit %s does not exist, proceeding anyway.", *name); |
f8d6cb48 | 6805 | } |
d6568222 SS |
6806 | } |
6807 | ||
4fbd7192 LP |
6808 | r = acquire_bus(BUS_MANAGER, &bus); |
6809 | if (r < 0) | |
6810 | return r; | |
6811 | ||
8a4b13c5 | 6812 | polkit_agent_open_maybe(); |
d2ad7e1b | 6813 | |
729e3769 LP |
6814 | if (streq(verb, "enable")) { |
6815 | method = "EnableUnitFiles"; | |
6816 | expect_carries_install_info = true; | |
6817 | } else if (streq(verb, "disable")) { | |
6818 | method = "DisableUnitFiles"; | |
6819 | send_force = false; | |
6820 | } else if (streq(verb, "reenable")) { | |
6821 | method = "ReenableUnitFiles"; | |
6822 | expect_carries_install_info = true; | |
6823 | } else if (streq(verb, "link")) | |
6824 | method = "LinkUnitFiles"; | |
6825 | else if (streq(verb, "preset")) { | |
d309c1c3 LP |
6826 | |
6827 | if (arg_preset_mode != UNIT_FILE_PRESET_FULL) { | |
6828 | method = "PresetUnitFilesWithMode"; | |
6829 | send_preset_mode = true; | |
6830 | } else | |
6831 | method = "PresetUnitFiles"; | |
6832 | ||
729e3769 | 6833 | expect_carries_install_info = true; |
39207373 | 6834 | ignore_carries_install_info = true; |
729e3769 LP |
6835 | } else if (streq(verb, "mask")) |
6836 | method = "MaskUnitFiles"; | |
6837 | else if (streq(verb, "unmask")) { | |
6838 | method = "UnmaskUnitFiles"; | |
6839 | send_force = false; | |
344ca755 LP |
6840 | } else if (streq(verb, "revert")) { |
6841 | method = "RevertUnitFiles"; | |
6842 | send_runtime = send_force = false; | |
729e3769 LP |
6843 | } else |
6844 | assert_not_reached("Unknown verb"); | |
6845 | ||
8010c205 | 6846 | r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, method); |
f459b602 MAP |
6847 | if (r < 0) |
6848 | return bus_log_create_error(r); | |
ee5762e3 | 6849 | |
e3e0314b | 6850 | r = sd_bus_message_append_strv(m, names); |
f459b602 MAP |
6851 | if (r < 0) |
6852 | return bus_log_create_error(r); | |
ee5762e3 | 6853 | |
d309c1c3 LP |
6854 | if (send_preset_mode) { |
6855 | r = sd_bus_message_append(m, "s", unit_file_preset_mode_to_string(arg_preset_mode)); | |
6856 | if (r < 0) | |
6857 | return bus_log_create_error(r); | |
6858 | } | |
6859 | ||
344ca755 LP |
6860 | if (send_runtime) { |
6861 | r = sd_bus_message_append(m, "b", arg_runtime); | |
6862 | if (r < 0) | |
6863 | return bus_log_create_error(r); | |
6864 | } | |
ee5762e3 | 6865 | |
729e3769 | 6866 | if (send_force) { |
f459b602 MAP |
6867 | r = sd_bus_message_append(m, "b", arg_force); |
6868 | if (r < 0) | |
6869 | return bus_log_create_error(r); | |
ee5762e3 LP |
6870 | } |
6871 | ||
c49b30a2 | 6872 | r = sd_bus_call(bus, m, 0, &error, &reply); |
691395d8 | 6873 | if (r < 0) |
af3d8113 | 6874 | return log_error_errno(r, "Failed to %s unit: %s", verb, bus_error_message(&error, r)); |
be394c48 | 6875 | |
729e3769 | 6876 | if (expect_carries_install_info) { |
f459b602 MAP |
6877 | r = sd_bus_message_read(reply, "b", &carries_install_info); |
6878 | if (r < 0) | |
6879 | return bus_log_parse_error(r); | |
ee5762e3 LP |
6880 | } |
6881 | ||
57ab2eab | 6882 | r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet, &changes, &n_changes); |
f459b602 | 6883 | if (r < 0) |
85b78539 | 6884 | goto finish; |
b77398f7 | 6885 | |
93c941e3 | 6886 | /* Try to reload if enabled */ |
d6cb60c7 | 6887 | if (!arg_no_reload) |
e449de87 | 6888 | r = daemon_reload(argc, argv, userdata); |
f459b602 MAP |
6889 | else |
6890 | r = 0; | |
b647f10d | 6891 | } |
3d3961f2 | 6892 | |
39207373 | 6893 | if (carries_install_info == 0 && !ignore_carries_install_info) |
46efc978 LP |
6894 | log_notice("The unit files have no installation config (WantedBy=, RequiredBy=, Also=,\n" |
6895 | "Alias= settings in the [Install] section, and DefaultInstance= for template\n" | |
2811184a ZJS |
6896 | "units). This means they are not meant to be enabled using systemctl.\n" |
6897 | " \n" /* trick: the space is needed so that the line does not get stripped from output */ | |
46efc978 | 6898 | "Possible reasons for having this kind of units are:\n" |
18c93ee3 LP |
6899 | "%1$s A unit may be statically enabled by being symlinked from another unit's\n" |
6900 | " .wants/ or .requires/ directory.\n" | |
6901 | "%1$s A unit's purpose may be to act as a helper for some other unit which has\n" | |
6902 | " a requirement dependency on it.\n" | |
6903 | "%1$s A unit may be started when needed via activation (socket, path, timer,\n" | |
6904 | " D-Bus, udev, scripted systemctl call, ...).\n" | |
6905 | "%1$s In case of template units, the unit is meant to be enabled with some\n" | |
6906 | " instance name specified.", | |
9a6f746f | 6907 | special_glyph(SPECIAL_GLYPH_BULLET)); |
ee5762e3 | 6908 | |
6bc30691 | 6909 | if (arg_now && STR_IN_SET(argv[0], "enable", "disable", "mask")) { |
4fbd7192 | 6910 | sd_bus *bus; |
da6053d0 | 6911 | size_t len, i; |
57ab2eab | 6912 | |
4fbd7192 LP |
6913 | r = acquire_bus(BUS_MANAGER, &bus); |
6914 | if (r < 0) | |
d073dea0 | 6915 | goto finish; |
4fbd7192 | 6916 | |
6bc30691 JS |
6917 | len = strv_length(names); |
6918 | { | |
6919 | char *new_args[len + 2]; | |
57ab2eab | 6920 | |
6bc30691 JS |
6921 | new_args[0] = (char*) (streq(argv[0], "enable") ? "start" : "stop"); |
6922 | for (i = 0; i < len; i++) | |
6923 | new_args[i + 1] = basename(names[i]); | |
6924 | new_args[i + 1] = NULL; | |
6925 | ||
6926 | r = start_unit(len + 1, new_args, userdata); | |
6927 | } | |
57ab2eab JS |
6928 | } |
6929 | ||
729e3769 | 6930 | finish: |
729e3769 | 6931 | unit_file_changes_free(changes, n_changes); |
ee5762e3 | 6932 | |
729e3769 | 6933 | return r; |
ee5762e3 LP |
6934 | } |
6935 | ||
e449de87 | 6936 | static int add_dependency(int argc, char *argv[], void *userdata) { |
e94937df LN |
6937 | _cleanup_strv_free_ char **names = NULL; |
6938 | _cleanup_free_ char *target = NULL; | |
e449de87 | 6939 | const char *verb = argv[0]; |
acc0269c | 6940 | UnitFileChange *changes = NULL; |
da6053d0 | 6941 | size_t n_changes = 0; |
e94937df LN |
6942 | UnitDependency dep; |
6943 | int r = 0; | |
6944 | ||
e449de87 | 6945 | if (!argv[1]) |
e94937df LN |
6946 | return 0; |
6947 | ||
df7c4eb6 ZJS |
6948 | r = unit_name_mangle_with_suffix(argv[1], "as target", |
6949 | arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN, | |
6950 | ".target", &target); | |
7410616c LP |
6951 | if (r < 0) |
6952 | return log_error_errno(r, "Failed to mangle unit name: %m"); | |
e94937df | 6953 | |
df7c4eb6 | 6954 | r = mangle_names("as dependency", strv_skip(argv, 2), &names); |
e94937df LN |
6955 | if (r < 0) |
6956 | return r; | |
6957 | ||
6958 | if (streq(verb, "add-wants")) | |
6959 | dep = UNIT_WANTS; | |
6960 | else if (streq(verb, "add-requires")) | |
6961 | dep = UNIT_REQUIRES; | |
6962 | else | |
6963 | assert_not_reached("Unknown verb"); | |
6964 | ||
4fbd7192 | 6965 | if (install_client_side()) { |
b3796dd8 | 6966 | r = unit_file_add_dependency(arg_scope, args_to_flags(), arg_root, names, target, dep, &changes, &n_changes); |
af3d8113 | 6967 | unit_file_dump_changes(r, "add dependency on", changes, n_changes, arg_quiet); |
5f056378 CH |
6968 | |
6969 | if (r > 0) | |
6970 | r = 0; | |
e94937df | 6971 | } else { |
4afd3348 LP |
6972 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL, *m = NULL; |
6973 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
4fbd7192 | 6974 | sd_bus *bus; |
e94937df | 6975 | |
4fbd7192 LP |
6976 | r = acquire_bus(BUS_MANAGER, &bus); |
6977 | if (r < 0) | |
6978 | return r; | |
6979 | ||
8a4b13c5 | 6980 | polkit_agent_open_maybe(); |
d2ad7e1b | 6981 | |
8010c205 | 6982 | r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "AddDependencyUnitFiles"); |
e94937df LN |
6983 | if (r < 0) |
6984 | return bus_log_create_error(r); | |
6985 | ||
342641fb | 6986 | r = sd_bus_message_append_strv(m, names); |
e94937df LN |
6987 | if (r < 0) |
6988 | return bus_log_create_error(r); | |
6989 | ||
342641fb | 6990 | r = sd_bus_message_append(m, "ssbb", target, unit_dependency_to_string(dep), arg_runtime, arg_force); |
e94937df LN |
6991 | if (r < 0) |
6992 | return bus_log_create_error(r); | |
6993 | ||
6994 | r = sd_bus_call(bus, m, 0, &error, &reply); | |
691395d8 | 6995 | if (r < 0) |
af3d8113 | 6996 | return log_error_errno(r, "Failed to add dependency: %s", bus_error_message(&error, r)); |
e94937df | 6997 | |
acc0269c | 6998 | r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet, &changes, &n_changes); |
e94937df | 6999 | if (r < 0) |
acc0269c | 7000 | goto finish; |
e94937df | 7001 | |
acc0269c CH |
7002 | if (arg_no_reload) { |
7003 | r = 0; | |
7004 | goto finish; | |
7005 | } | |
7006 | ||
7007 | r = daemon_reload(argc, argv, userdata); | |
e94937df | 7008 | } |
acc0269c CH |
7009 | |
7010 | finish: | |
7011 | unit_file_changes_free(changes, n_changes); | |
7012 | ||
7013 | return r; | |
e94937df LN |
7014 | } |
7015 | ||
e449de87 | 7016 | static int preset_all(int argc, char *argv[], void *userdata) { |
acc0269c | 7017 | UnitFileChange *changes = NULL; |
da6053d0 | 7018 | size_t n_changes = 0; |
d309c1c3 LP |
7019 | int r; |
7020 | ||
4fbd7192 | 7021 | if (install_client_side()) { |
b3796dd8 | 7022 | r = unit_file_preset_all(arg_scope, args_to_flags(), arg_root, arg_preset_mode, &changes, &n_changes); |
af3d8113 | 7023 | unit_file_dump_changes(r, "preset", changes, n_changes, arg_quiet); |
5f056378 CH |
7024 | |
7025 | if (r > 0) | |
7026 | r = 0; | |
d309c1c3 | 7027 | } else { |
4afd3348 LP |
7028 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
7029 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
4fbd7192 | 7030 | sd_bus *bus; |
d309c1c3 | 7031 | |
4fbd7192 LP |
7032 | r = acquire_bus(BUS_MANAGER, &bus); |
7033 | if (r < 0) | |
7034 | return r; | |
7035 | ||
8a4b13c5 | 7036 | polkit_agent_open_maybe(); |
d2ad7e1b | 7037 | |
8010c205 | 7038 | r = bus_call_method( |
d309c1c3 | 7039 | bus, |
8010c205 | 7040 | bus_systemd_mgr, |
6e646d22 LP |
7041 | "PresetAllUnitFiles", |
7042 | &error, | |
7043 | &reply, | |
d309c1c3 LP |
7044 | "sbb", |
7045 | unit_file_preset_mode_to_string(arg_preset_mode), | |
7046 | arg_runtime, | |
7047 | arg_force); | |
691395d8 | 7048 | if (r < 0) |
af3d8113 | 7049 | return log_error_errno(r, "Failed to preset all units: %s", bus_error_message(&error, r)); |
d309c1c3 | 7050 | |
acc0269c | 7051 | r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet, &changes, &n_changes); |
d309c1c3 | 7052 | if (r < 0) |
acc0269c | 7053 | goto finish; |
d309c1c3 | 7054 | |
acc0269c CH |
7055 | if (arg_no_reload) { |
7056 | r = 0; | |
7057 | goto finish; | |
7058 | } | |
7059 | ||
7060 | r = daemon_reload(argc, argv, userdata); | |
d309c1c3 | 7061 | } |
acc0269c CH |
7062 | |
7063 | finish: | |
7064 | unit_file_changes_free(changes, n_changes); | |
7065 | ||
7066 | return r; | |
d309c1c3 LP |
7067 | } |
7068 | ||
3b3557c4 JS |
7069 | static int show_installation_targets_client_side(const char *name) { |
7070 | UnitFileChange *changes = NULL; | |
da6053d0 | 7071 | size_t n_changes = 0, i; |
3b3557c4 JS |
7072 | UnitFileFlags flags; |
7073 | char **p; | |
7074 | int r; | |
7075 | ||
7076 | p = STRV_MAKE(name); | |
7077 | flags = UNIT_FILE_DRY_RUN | | |
7078 | (arg_runtime ? UNIT_FILE_RUNTIME : 0); | |
7079 | ||
7080 | r = unit_file_disable(UNIT_FILE_SYSTEM, flags, NULL, p, &changes, &n_changes); | |
7081 | if (r < 0) | |
7082 | return log_error_errno(r, "Failed to get file links for %s: %m", name); | |
7083 | ||
7084 | for (i = 0; i < n_changes; i++) | |
7085 | if (changes[i].type == UNIT_FILE_UNLINK) | |
7086 | printf(" %s\n", changes[i].path); | |
7087 | ||
7088 | return 0; | |
7089 | } | |
7090 | ||
7091 | static int show_installation_targets(sd_bus *bus, const char *name) { | |
7092 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
7093 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
7094 | const char *link; | |
7095 | int r; | |
7096 | ||
8010c205 | 7097 | r = bus_call_method(bus, bus_systemd_mgr, "GetUnitFileLinks", &error, &reply, "sb", name, arg_runtime); |
3b3557c4 JS |
7098 | if (r < 0) |
7099 | return log_error_errno(r, "Failed to get unit file links for %s: %s", name, bus_error_message(&error, r)); | |
7100 | ||
7101 | r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "s"); | |
7102 | if (r < 0) | |
7103 | return bus_log_parse_error(r); | |
7104 | ||
7105 | while ((r = sd_bus_message_read(reply, "s", &link)) > 0) | |
7106 | printf(" %s\n", link); | |
7107 | ||
7108 | if (r < 0) | |
7109 | return bus_log_parse_error(r); | |
7110 | ||
7111 | r = sd_bus_message_exit_container(reply); | |
7112 | if (r < 0) | |
7113 | return bus_log_parse_error(r); | |
7114 | ||
7115 | return 0; | |
7116 | } | |
7117 | ||
e449de87 | 7118 | static int unit_is_enabled(int argc, char *argv[], void *userdata) { |
e3e0314b | 7119 | _cleanup_strv_free_ char **names = NULL; |
729e3769 LP |
7120 | bool enabled; |
7121 | char **name; | |
f459b602 | 7122 | int r; |
ee5762e3 | 7123 | |
df7c4eb6 | 7124 | r = mangle_names("to check", strv_skip(argv, 1), &names); |
cbb13b2a VP |
7125 | if (r < 0) |
7126 | return r; | |
7127 | ||
e449de87 | 7128 | r = enable_sysv_units(argv[0], names); |
729e3769 LP |
7129 | if (r < 0) |
7130 | return r; | |
ee5762e3 | 7131 | |
729e3769 | 7132 | enabled = r > 0; |
ee5762e3 | 7133 | |
4fbd7192 | 7134 | if (install_client_side()) { |
e3e0314b | 7135 | STRV_FOREACH(name, names) { |
729e3769 | 7136 | UnitFileState state; |
ee5762e3 | 7137 | |
0ec0deaa LP |
7138 | r = unit_file_get_state(arg_scope, arg_root, *name, &state); |
7139 | if (r < 0) | |
c5024cd0 | 7140 | return log_error_errno(r, "Failed to get unit file state for %s: %m", *name); |
ee5762e3 | 7141 | |
4c315c2c IS |
7142 | if (IN_SET(state, |
7143 | UNIT_FILE_ENABLED, | |
7144 | UNIT_FILE_ENABLED_RUNTIME, | |
7145 | UNIT_FILE_STATIC, | |
15d7ab87 | 7146 | UNIT_FILE_ALIAS, |
f4139308 LP |
7147 | UNIT_FILE_INDIRECT, |
7148 | UNIT_FILE_GENERATED)) | |
729e3769 LP |
7149 | enabled = true; |
7150 | ||
3b3557c4 | 7151 | if (!arg_quiet) { |
729e3769 | 7152 | puts(unit_file_state_to_string(state)); |
3b3557c4 JS |
7153 | if (arg_full) { |
7154 | r = show_installation_targets_client_side(*name); | |
7155 | if (r < 0) | |
7156 | return r; | |
7157 | } | |
7158 | } | |
71fad675 | 7159 | } |
ee5762e3 | 7160 | |
5f056378 | 7161 | r = 0; |
729e3769 | 7162 | } else { |
4afd3348 | 7163 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
4fbd7192 LP |
7164 | sd_bus *bus; |
7165 | ||
7166 | r = acquire_bus(BUS_MANAGER, &bus); | |
7167 | if (r < 0) | |
7168 | return r; | |
7169 | ||
e3e0314b | 7170 | STRV_FOREACH(name, names) { |
4afd3348 | 7171 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
729e3769 | 7172 | const char *s; |
63a723f3 | 7173 | |
8010c205 | 7174 | r = bus_call_method(bus, bus_systemd_mgr, "GetUnitFileState", &error, &reply, "s", *name); |
691395d8 LP |
7175 | if (r < 0) |
7176 | return log_error_errno(r, "Failed to get unit file state for %s: %s", *name, bus_error_message(&error, r)); | |
ee5762e3 | 7177 | |
f459b602 MAP |
7178 | r = sd_bus_message_read(reply, "s", &s); |
7179 | if (r < 0) | |
7180 | return bus_log_parse_error(r); | |
ee5762e3 | 7181 | |
f4139308 | 7182 | if (STR_IN_SET(s, "enabled", "enabled-runtime", "static", "indirect", "generated")) |
729e3769 LP |
7183 | enabled = true; |
7184 | ||
3b3557c4 | 7185 | if (!arg_quiet) { |
729e3769 | 7186 | puts(s); |
3b3557c4 JS |
7187 | if (arg_full) { |
7188 | r = show_installation_targets(bus, *name); | |
7189 | if (r < 0) | |
7190 | return r; | |
7191 | } | |
7192 | } | |
560d8f23 | 7193 | } |
ee5762e3 LP |
7194 | } |
7195 | ||
f4139308 | 7196 | return enabled ? EXIT_SUCCESS : EXIT_FAILURE; |
ee5762e3 LP |
7197 | } |
7198 | ||
adb6cd9b FB |
7199 | static int match_startup_finished(sd_bus_message *m, void *userdata, sd_bus_error *error) { |
7200 | char **state = userdata; | |
7201 | int r; | |
7202 | ||
7203 | assert(state); | |
7204 | ||
8010c205 | 7205 | r = bus_get_property_string(sd_bus_message_get_bus(m), bus_systemd_mgr, "SystemState", NULL, state); |
adb6cd9b FB |
7206 | |
7207 | sd_event_exit(sd_bus_get_event(sd_bus_message_get_bus(m)), r); | |
7208 | return 0; | |
7209 | } | |
7210 | ||
e449de87 | 7211 | static int is_system_running(int argc, char *argv[], void *userdata) { |
e686a616 | 7212 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
adb6cd9b FB |
7213 | _cleanup_(sd_bus_slot_unrefp) sd_bus_slot *slot_startup_finished = NULL; |
7214 | _cleanup_(sd_event_unrefp) sd_event* event = NULL; | |
99813a19 | 7215 | _cleanup_free_ char *state = NULL; |
4fbd7192 | 7216 | sd_bus *bus; |
99813a19 LP |
7217 | int r; |
7218 | ||
040524b4 | 7219 | if (running_in_chroot() > 0 || (arg_transport == BUS_TRANSPORT_LOCAL && !sd_booted())) { |
94f099d8 LP |
7220 | if (!arg_quiet) |
7221 | puts("offline"); | |
7222 | return EXIT_FAILURE; | |
7223 | } | |
7224 | ||
4fbd7192 LP |
7225 | r = acquire_bus(BUS_MANAGER, &bus); |
7226 | if (r < 0) | |
7227 | return r; | |
7228 | ||
adb6cd9b FB |
7229 | if (arg_wait) { |
7230 | r = sd_event_default(&event); | |
7231 | if (r >= 0) | |
7232 | r = sd_bus_attach_event(bus, event, 0); | |
7233 | if (r >= 0) | |
8010c205 | 7234 | r = bus_match_signal_async( |
adb6cd9b FB |
7235 | bus, |
7236 | &slot_startup_finished, | |
8010c205 | 7237 | bus_systemd_mgr, |
adb6cd9b FB |
7238 | "StartupFinished", |
7239 | match_startup_finished, NULL, &state); | |
7240 | if (r < 0) { | |
7241 | log_warning_errno(r, "Failed to request match for StartupFinished: %m"); | |
7242 | arg_wait = false; | |
7243 | } | |
7244 | } | |
7245 | ||
8010c205 | 7246 | r = bus_get_property_string(bus, bus_systemd_mgr, "SystemState", &error, &state); |
99813a19 | 7247 | if (r < 0) { |
adb6cd9b | 7248 | log_warning_errno(r, "Failed to query system state: %s", bus_error_message(&error, r)); |
e686a616 | 7249 | |
99813a19 LP |
7250 | if (!arg_quiet) |
7251 | puts("unknown"); | |
bfeec178 | 7252 | return EXIT_FAILURE; |
99813a19 LP |
7253 | } |
7254 | ||
adb6cd9b FB |
7255 | if (arg_wait && STR_IN_SET(state, "initializing", "starting")) { |
7256 | r = sd_event_loop(event); | |
7257 | if (r < 0) { | |
7258 | log_warning_errno(r, "Failed to get property from event loop: %m"); | |
7259 | if (!arg_quiet) | |
7260 | puts("unknown"); | |
7261 | return EXIT_FAILURE; | |
7262 | } | |
7263 | } | |
7264 | ||
99813a19 LP |
7265 | if (!arg_quiet) |
7266 | puts(state); | |
7267 | ||
7268 | return streq(state, "running") ? EXIT_SUCCESS : EXIT_FAILURE; | |
7269 | } | |
7270 | ||
7d4fb3b1 | 7271 | static int create_edit_temp_file(const char *new_path, const char *original_path, char **ret_tmp_fn) { |
45519fd6 | 7272 | _cleanup_free_ char *t = NULL; |
ae6c3cc0 | 7273 | int r; |
7d4fb3b1 RC |
7274 | |
7275 | assert(new_path); | |
7276 | assert(original_path); | |
7277 | assert(ret_tmp_fn); | |
7278 | ||
14bcf25c | 7279 | r = tempfn_random(new_path, NULL, &t); |
ae6c3cc0 | 7280 | if (r < 0) |
029009d4 | 7281 | return log_error_errno(r, "Failed to determine temporary filename for \"%s\": %m", new_path); |
7d4fb3b1 RC |
7282 | |
7283 | r = mkdir_parents(new_path, 0755); | |
45519fd6 | 7284 | if (r < 0) |
691395d8 | 7285 | return log_error_errno(r, "Failed to create directories for \"%s\": %m", new_path); |
7d4fb3b1 | 7286 | |
8a016c74 | 7287 | r = copy_file(original_path, t, 0, 0644, 0, 0, COPY_REFLINK); |
7d4fb3b1 | 7288 | if (r == -ENOENT) { |
45519fd6 | 7289 | |
7d4fb3b1 | 7290 | r = touch(t); |
45519fd6 LP |
7291 | if (r < 0) |
7292 | return log_error_errno(r, "Failed to create temporary file \"%s\": %m", t); | |
7293 | ||
7294 | } else if (r < 0) | |
39c38ce1 | 7295 | return log_error_errno(r, "Failed to create temporary file for \"%s\": %m", new_path); |
7d4fb3b1 | 7296 | |
ae2a15bc | 7297 | *ret_tmp_fn = TAKE_PTR(t); |
7d4fb3b1 RC |
7298 | |
7299 | return 0; | |
7300 | } | |
7301 | ||
c51932be LP |
7302 | static int get_file_to_edit( |
7303 | const LookupPaths *paths, | |
7304 | const char *name, | |
7305 | char **ret_path) { | |
7306 | ||
7307 | _cleanup_free_ char *path = NULL, *run = NULL; | |
7d4fb3b1 | 7308 | |
45519fd6 LP |
7309 | assert(name); |
7310 | assert(ret_path); | |
7311 | ||
657ee2d8 | 7312 | path = path_join(paths->persistent_config, name); |
c51932be | 7313 | if (!path) |
7d4fb3b1 RC |
7314 | return log_oom(); |
7315 | ||
c51932be | 7316 | if (arg_runtime) { |
657ee2d8 | 7317 | run = path_join(paths->runtime_config, name); |
c51932be LP |
7318 | if (!run) |
7319 | return log_oom(); | |
7320 | } | |
7321 | ||
bc854dc7 | 7322 | if (arg_runtime) { |
baaa35ad ZJS |
7323 | if (access(path, F_OK) >= 0) |
7324 | return log_error_errno(SYNTHETIC_ERRNO(EEXIST), | |
7325 | "Refusing to create \"%s\" because it would be overridden by \"%s\" anyway.", | |
7326 | run, path); | |
691395d8 | 7327 | |
ae2a15bc LP |
7328 | *ret_path = TAKE_PTR(run); |
7329 | } else | |
7330 | *ret_path = TAKE_PTR(path); | |
7d4fb3b1 RC |
7331 | |
7332 | return 0; | |
7333 | } | |
7334 | ||
39c38ce1 | 7335 | static int unit_file_create_new( |
c51932be LP |
7336 | const LookupPaths *paths, |
7337 | const char *unit_name, | |
39c38ce1 | 7338 | const char *suffix, |
c51932be LP |
7339 | char **ret_new_path, |
7340 | char **ret_tmp_path) { | |
7341 | ||
919d2720 LP |
7342 | _cleanup_free_ char *new_path = NULL, *tmp_path = NULL; |
7343 | const char *ending; | |
7d4fb3b1 RC |
7344 | int r; |
7345 | ||
7346 | assert(unit_name); | |
7347 | assert(ret_new_path); | |
7348 | assert(ret_tmp_path); | |
7349 | ||
39c38ce1 | 7350 | ending = strjoina(unit_name, suffix); |
919d2720 | 7351 | r = get_file_to_edit(paths, ending, &new_path); |
7d4fb3b1 RC |
7352 | if (r < 0) |
7353 | return r; | |
7354 | ||
919d2720 LP |
7355 | r = create_edit_temp_file(new_path, new_path, &tmp_path); |
7356 | if (r < 0) | |
7d4fb3b1 | 7357 | return r; |
7d4fb3b1 | 7358 | |
919d2720 LP |
7359 | *ret_new_path = TAKE_PTR(new_path); |
7360 | *ret_tmp_path = TAKE_PTR(tmp_path); | |
7d4fb3b1 RC |
7361 | |
7362 | return 0; | |
7363 | } | |
7364 | ||
1cfa9a4c | 7365 | static int unit_file_create_copy( |
c51932be | 7366 | const LookupPaths *paths, |
1cfa9a4c LP |
7367 | const char *unit_name, |
7368 | const char *fragment_path, | |
1cfa9a4c LP |
7369 | char **ret_new_path, |
7370 | char **ret_tmp_path) { | |
7371 | ||
92d6b0bf | 7372 | _cleanup_free_ char *new_path = NULL, *tmp_path = NULL; |
7d4fb3b1 RC |
7373 | int r; |
7374 | ||
7375 | assert(fragment_path); | |
7376 | assert(unit_name); | |
7377 | assert(ret_new_path); | |
7378 | assert(ret_tmp_path); | |
7379 | ||
92d6b0bf | 7380 | r = get_file_to_edit(paths, unit_name, &new_path); |
7d4fb3b1 RC |
7381 | if (r < 0) |
7382 | return r; | |
7383 | ||
92d6b0bf | 7384 | if (!path_equal(fragment_path, new_path) && access(new_path, F_OK) >= 0) { |
7d4fb3b1 RC |
7385 | char response; |
7386 | ||
92d6b0bf LP |
7387 | r = ask_char(&response, "yn", "\"%s\" already exists. Overwrite with \"%s\"? [(y)es, (n)o] ", new_path, fragment_path); |
7388 | if (r < 0) | |
7d4fb3b1 | 7389 | return r; |
5daacba2 LP |
7390 | if (response != 'y') |
7391 | return log_warning_errno(SYNTHETIC_ERRNO(EKEYREJECTED), "%s skipped.", unit_name); | |
7d4fb3b1 RC |
7392 | } |
7393 | ||
92d6b0bf LP |
7394 | r = create_edit_temp_file(new_path, fragment_path, &tmp_path); |
7395 | if (r < 0) | |
7d4fb3b1 | 7396 | return r; |
7d4fb3b1 | 7397 | |
92d6b0bf LP |
7398 | *ret_new_path = TAKE_PTR(new_path); |
7399 | *ret_tmp_path = TAKE_PTR(tmp_path); | |
7d4fb3b1 RC |
7400 | |
7401 | return 0; | |
7402 | } | |
7403 | ||
7404 | static int run_editor(char **paths) { | |
7d4fb3b1 RC |
7405 | int r; |
7406 | ||
7407 | assert(paths); | |
7408 | ||
0672e2c6 | 7409 | r = safe_fork("(editor)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG|FORK_WAIT, NULL); |
4c253ed1 | 7410 | if (r < 0) |
b6e1fff1 | 7411 | return r; |
4c253ed1 | 7412 | if (r == 0) { |
b2238e38 | 7413 | char **editor_args = NULL, **tmp_path, **original_path; |
70d6e5bd | 7414 | size_t n_editor_args = 0, i = 1, argc; |
b2238e38 | 7415 | const char **args, *editor, *p; |
7d4fb3b1 RC |
7416 | |
7417 | argc = strv_length(paths)/2 + 1; | |
7d4fb3b1 RC |
7418 | |
7419 | /* SYSTEMD_EDITOR takes precedence over EDITOR which takes precedence over VISUAL | |
7420 | * If neither SYSTEMD_EDITOR nor EDITOR nor VISUAL are present, | |
7421 | * we try to execute well known editors | |
7422 | */ | |
7423 | editor = getenv("SYSTEMD_EDITOR"); | |
7424 | if (!editor) | |
7425 | editor = getenv("EDITOR"); | |
7426 | if (!editor) | |
7427 | editor = getenv("VISUAL"); | |
7428 | ||
7429 | if (!isempty(editor)) { | |
9ef5d8f2 JS |
7430 | editor_args = strv_split(editor, WHITESPACE); |
7431 | if (!editor_args) { | |
7432 | (void) log_oom(); | |
7433 | _exit(EXIT_FAILURE); | |
7434 | } | |
7435 | n_editor_args = strv_length(editor_args); | |
7436 | argc += n_editor_args - 1; | |
7437 | } | |
02a126a3 | 7438 | |
9ef5d8f2 JS |
7439 | args = newa(const char*, argc + 1); |
7440 | ||
7441 | if (n_editor_args > 0) { | |
7442 | args[0] = editor_args[0]; | |
7443 | for (; i < n_editor_args; i++) | |
7444 | args[i] = editor_args[i]; | |
7445 | } | |
7446 | ||
df79fdab LP |
7447 | STRV_FOREACH_PAIR(original_path, tmp_path, paths) |
7448 | args[i++] = *tmp_path; | |
9ef5d8f2 JS |
7449 | args[i] = NULL; |
7450 | ||
7451 | if (n_editor_args > 0) | |
7452 | execvp(args[0], (char* const*) args); | |
7d4fb3b1 | 7453 | |
9391a1c3 | 7454 | FOREACH_STRING(p, "editor", "nano", "vim", "vi") { |
1cfa9a4c LP |
7455 | args[0] = p; |
7456 | execvp(p, (char* const*) args); | |
7d4fb3b1 RC |
7457 | /* We do not fail if the editor doesn't exist |
7458 | * because we want to try each one of them before | |
7459 | * failing. | |
7460 | */ | |
7461 | if (errno != ENOENT) { | |
691395d8 | 7462 | log_error_errno(errno, "Failed to execute %s: %m", editor); |
7d4fb3b1 RC |
7463 | _exit(EXIT_FAILURE); |
7464 | } | |
7465 | } | |
7466 | ||
1cfa9a4c | 7467 | log_error("Cannot edit unit(s), no editor available. Please set either $SYSTEMD_EDITOR, $EDITOR or $VISUAL."); |
7d4fb3b1 RC |
7468 | _exit(EXIT_FAILURE); |
7469 | } | |
7470 | ||
45519fd6 | 7471 | return 0; |
7d4fb3b1 RC |
7472 | } |
7473 | ||
7474 | static int find_paths_to_edit(sd_bus *bus, char **names, char ***paths) { | |
8e766630 | 7475 | _cleanup_(lookup_paths_free) LookupPaths lp = {}; |
7d4fb3b1 RC |
7476 | char **name; |
7477 | int r; | |
7478 | ||
7479 | assert(names); | |
7480 | assert(paths); | |
7481 | ||
4943d143 | 7482 | r = lookup_paths_init(&lp, arg_scope, 0, arg_root); |
5b013a2f | 7483 | if (r < 0) |
8df18507 | 7484 | return r; |
7d4fb3b1 | 7485 | |
e9e310f8 | 7486 | STRV_FOREACH(name, names) { |
b74df547 ZJS |
7487 | _cleanup_free_ char *path = NULL, *new_path = NULL, *tmp_path = NULL, *tmp_name = NULL; |
7488 | const char *unit_name; | |
7d4fb3b1 | 7489 | |
02a126a3 LP |
7490 | r = unit_find_paths(bus, *name, &lp, false, &path, NULL); |
7491 | if (r == -EKEYREJECTED) { | |
7492 | /* If loading of the unit failed server side complete, then the server won't tell us the unit | |
7493 | * file path. In that case, find the file client side. */ | |
7494 | log_debug_errno(r, "Unit '%s' was not loaded correctly, retrying client-side.", *name); | |
7495 | r = unit_find_paths(bus, *name, &lp, true, &path, NULL); | |
7496 | } | |
7497 | if (r == -ERFKILL) | |
7498 | return log_error_errno(r, "Unit '%s' masked, cannot edit.", *name); | |
e9e310f8 RC |
7499 | if (r < 0) |
7500 | return r; | |
b3734d98 ZJS |
7501 | |
7502 | if (r == 0) { | |
7503 | assert(!path); | |
7504 | ||
7505 | if (!arg_force) { | |
85163756 LP |
7506 | log_info("Run 'systemctl edit%s --force --full %s' to create a new unit.", |
7507 | arg_scope == UNIT_FILE_GLOBAL ? " --global" : | |
7508 | arg_scope == UNIT_FILE_USER ? " --user" : "", | |
7509 | *name); | |
39c38ce1 DC |
7510 | return -ENOENT; |
7511 | } | |
39c38ce1 | 7512 | |
b3734d98 ZJS |
7513 | /* Create a new unit from scratch */ |
7514 | unit_name = *name; | |
7515 | r = unit_file_create_new(&lp, unit_name, | |
7516 | arg_full ? NULL : ".d/override.conf", | |
7517 | &new_path, &tmp_path); | |
7518 | } else { | |
7519 | assert(path); | |
b74df547 | 7520 | |
b3734d98 ZJS |
7521 | unit_name = basename(path); |
7522 | /* We follow unit aliases, but we need to propagate the instance */ | |
7523 | if (unit_name_is_valid(*name, UNIT_NAME_INSTANCE) && | |
7524 | unit_name_is_valid(unit_name, UNIT_NAME_TEMPLATE)) { | |
7525 | _cleanup_free_ char *instance = NULL; | |
b74df547 | 7526 | |
b3734d98 ZJS |
7527 | r = unit_name_to_instance(*name, &instance); |
7528 | if (r < 0) | |
7529 | return r; | |
b74df547 | 7530 | |
b3734d98 ZJS |
7531 | r = unit_name_replace_instance(unit_name, instance, &tmp_name); |
7532 | if (r < 0) | |
7533 | return r; | |
7534 | ||
7535 | unit_name = tmp_name; | |
7536 | } | |
b74df547 | 7537 | |
39c38ce1 | 7538 | if (arg_full) |
b74df547 | 7539 | r = unit_file_create_copy(&lp, unit_name, path, &new_path, &tmp_path); |
39c38ce1 | 7540 | else |
b74df547 | 7541 | r = unit_file_create_new(&lp, unit_name, ".d/override.conf", &new_path, &tmp_path); |
b3734d98 | 7542 | } |
e9e310f8 | 7543 | if (r < 0) |
ad2a0358 | 7544 | return r; |
7d4fb3b1 | 7545 | |
e9e310f8 RC |
7546 | r = strv_push_pair(paths, new_path, tmp_path); |
7547 | if (r < 0) | |
7548 | return log_oom(); | |
02a126a3 | 7549 | |
78df0edc | 7550 | new_path = tmp_path = NULL; |
7d4fb3b1 RC |
7551 | } |
7552 | ||
7553 | return 0; | |
7554 | } | |
7555 | ||
e449de87 | 7556 | static int edit(int argc, char *argv[], void *userdata) { |
86f004fb | 7557 | _cleanup_(lookup_paths_free) LookupPaths lp = {}; |
7d4fb3b1 RC |
7558 | _cleanup_strv_free_ char **names = NULL; |
7559 | _cleanup_strv_free_ char **paths = NULL; | |
7560 | char **original, **tmp; | |
4fbd7192 | 7561 | sd_bus *bus; |
7d4fb3b1 RC |
7562 | int r; |
7563 | ||
2f50e59f LP |
7564 | if (!on_tty()) |
7565 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot edit units if not on a tty."); | |
7d4fb3b1 | 7566 | |
2f50e59f LP |
7567 | if (arg_transport != BUS_TRANSPORT_LOCAL) |
7568 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot edit units remotely."); | |
7d4fb3b1 | 7569 | |
86f004fb ZJS |
7570 | r = lookup_paths_init(&lp, arg_scope, 0, arg_root); |
7571 | if (r < 0) | |
7572 | return log_error_errno(r, "Failed to determine unit paths: %m"); | |
7573 | ||
4fbd7192 LP |
7574 | r = acquire_bus(BUS_MANAGER, &bus); |
7575 | if (r < 0) | |
7576 | return r; | |
7577 | ||
1d56bc09 | 7578 | r = expand_names(bus, strv_skip(argv, 1), NULL, &names, NULL); |
7d4fb3b1 RC |
7579 | if (r < 0) |
7580 | return log_error_errno(r, "Failed to expand names: %m"); | |
7581 | ||
9d9dd746 | 7582 | STRV_FOREACH(tmp, names) { |
86f004fb | 7583 | r = unit_is_masked(bus, &lp, *tmp); |
9d9dd746 ZJS |
7584 | if (r < 0) |
7585 | return r; | |
2f50e59f LP |
7586 | if (r > 0) |
7587 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot edit %s: unit is masked.", *tmp); | |
9d9dd746 ZJS |
7588 | } |
7589 | ||
7d4fb3b1 RC |
7590 | r = find_paths_to_edit(bus, names, &paths); |
7591 | if (r < 0) | |
7592 | return r; | |
7593 | ||
b5e6a600 | 7594 | if (strv_isempty(paths)) |
7d4fb3b1 | 7595 | return -ENOENT; |
7d4fb3b1 RC |
7596 | |
7597 | r = run_editor(paths); | |
7598 | if (r < 0) | |
7599 | goto end; | |
7600 | ||
7601 | STRV_FOREACH_PAIR(original, tmp, paths) { | |
9d9dd746 ZJS |
7602 | /* If the temporary file is empty we ignore it. |
7603 | * This allows the user to cancel the modification. | |
7d4fb3b1 RC |
7604 | */ |
7605 | if (null_or_empty_path(*tmp)) { | |
45519fd6 | 7606 | log_warning("Editing \"%s\" canceled: temporary file is empty.", *original); |
7d4fb3b1 RC |
7607 | continue; |
7608 | } | |
45519fd6 | 7609 | |
7d4fb3b1 RC |
7610 | r = rename(*tmp, *original); |
7611 | if (r < 0) { | |
029009d4 | 7612 | r = log_error_errno(errno, "Failed to rename \"%s\" to \"%s\": %m", *tmp, *original); |
7d4fb3b1 RC |
7613 | goto end; |
7614 | } | |
7615 | } | |
7616 | ||
45519fd6 LP |
7617 | r = 0; |
7618 | ||
7619 | if (!arg_no_reload && !install_client_side()) | |
e449de87 | 7620 | r = daemon_reload(argc, argv, userdata); |
7d4fb3b1 RC |
7621 | |
7622 | end: | |
5f18271e | 7623 | STRV_FOREACH_PAIR(original, tmp, paths) { |
45519fd6 | 7624 | (void) unlink(*tmp); |
7d4fb3b1 | 7625 | |
5f18271e RC |
7626 | /* Removing empty dropin dirs */ |
7627 | if (!arg_full) { | |
043717f9 RC |
7628 | _cleanup_free_ char *dir; |
7629 | ||
7630 | dir = dirname_malloc(*original); | |
7631 | if (!dir) | |
7632 | return log_oom(); | |
7633 | ||
5f18271e RC |
7634 | /* no need to check if the dir is empty, rmdir |
7635 | * does nothing if it is not the case. | |
7636 | */ | |
7637 | (void) rmdir(dir); | |
7638 | } | |
7639 | } | |
7640 | ||
7d4fb3b1 RC |
7641 | return r; |
7642 | } | |
7643 | ||
37ec0fdd LP |
7644 | static int systemctl_help(void) { |
7645 | _cleanup_free_ char *link = NULL; | |
7646 | int r; | |
7647 | ||
0221d68a | 7648 | (void) pager_open(arg_pager_flags); |
729e3769 | 7649 | |
37ec0fdd LP |
7650 | r = terminal_urlify_man("systemctl", "1", &link); |
7651 | if (r < 0) | |
7652 | return log_oom(); | |
7653 | ||
353b2baa LP |
7654 | printf("%1$s [OPTIONS...] COMMAND ...\n\n" |
7655 | "%5$sQuery or send control commands to the system manager.%6$s\n" | |
7656 | "\n%3$sUnit Commands:%4$s\n" | |
fbf3283b ZJS |
7657 | " list-units [PATTERN...] List units currently in memory\n" |
7658 | " list-sockets [PATTERN...] List socket units currently in memory,\n" | |
7659 | " ordered by address\n" | |
7660 | " list-timers [PATTERN...] List timer units currently in memory,\n" | |
7661 | " ordered by next elapse\n" | |
7662 | " start UNIT... Start (activate) one or more units\n" | |
7663 | " stop UNIT... Stop (deactivate) one or more units\n" | |
7664 | " reload UNIT... Reload one or more units\n" | |
7665 | " restart UNIT... Start or restart one or more units\n" | |
7666 | " try-restart UNIT... Restart one or more units if active\n" | |
7667 | " reload-or-restart UNIT... Reload one or more units if possible,\n" | |
7668 | " otherwise start or restart\n" | |
7669 | " try-reload-or-restart UNIT... If active, reload one or more units,\n" | |
7670 | " if supported, otherwise restart\n" | |
7671 | " isolate UNIT Start one unit and stop all others\n" | |
7672 | " kill UNIT... Send signal to processes of a unit\n" | |
d81bc51f | 7673 | " clean UNIT... Clean runtime, cache, state, logs or\n" |
ab4fae0c | 7674 | " configuration of unit\n" |
d9e45bc3 MS |
7675 | " freeze PATTERN... Freeze execution of unit processes\n" |
7676 | " thaw PATTERN... Resume execution of a frozen unit\n" | |
fbf3283b ZJS |
7677 | " is-active PATTERN... Check whether units are active\n" |
7678 | " is-failed PATTERN... Check whether units are failed\n" | |
7679 | " status [PATTERN...|PID...] Show runtime status of one or more units\n" | |
7680 | " show [PATTERN...|JOB...] Show properties of one or more\n" | |
7681 | " units/jobs or the manager\n" | |
7682 | " cat PATTERN... Show files and drop-ins of specified units\n" | |
7683 | " set-property UNIT PROPERTY=VALUE... Sets one or more properties of a unit\n" | |
7684 | " help PATTERN...|PID... Show manual for one or more units\n" | |
7685 | " reset-failed [PATTERN...] Reset failed state for all, one, or more\n" | |
7686 | " units\n" | |
94c3a838 ZJS |
7687 | " list-dependencies [UNIT...] Recursively show units which are required\n" |
7688 | " or wanted by the units or by which those\n" | |
7689 | " units are required or wanted" | |
353b2baa | 7690 | "\n%3$sUnit File Commands:%4$s\n" |
fbf3283b ZJS |
7691 | " list-unit-files [PATTERN...] List installed unit files\n" |
7692 | " enable [UNIT...|PATH...] Enable one or more unit files\n" | |
7693 | " disable UNIT... Disable one or more unit files\n" | |
7694 | " reenable UNIT... Reenable one or more unit files\n" | |
7695 | " preset UNIT... Enable/disable one or more unit files\n" | |
7696 | " based on preset configuration\n" | |
7697 | " preset-all Enable/disable all unit files based on\n" | |
7698 | " preset configuration\n" | |
7699 | " is-enabled UNIT... Check whether unit files are enabled\n" | |
7700 | " mask UNIT... Mask one or more units\n" | |
7701 | " unmask UNIT... Unmask one or more units\n" | |
7702 | " link PATH... Link one or more units files into\n" | |
7703 | " the search path\n" | |
7704 | " revert UNIT... Revert one or more unit files to vendor\n" | |
7705 | " version\n" | |
7706 | " add-wants TARGET UNIT... Add 'Wants' dependency for the target\n" | |
7707 | " on specified one or more units\n" | |
7708 | " add-requires TARGET UNIT... Add 'Requires' dependency for the target\n" | |
7709 | " on specified one or more units\n" | |
7710 | " edit UNIT... Edit one or more unit files\n" | |
7711 | " get-default Get the name of the default target\n" | |
353b2baa LP |
7712 | " set-default TARGET Set the default target\n" |
7713 | "\n%3$sMachine Commands:%4$s\n" | |
4d985a31 | 7714 | " list-machines [PATTERN...] List local containers and host\n" |
353b2baa | 7715 | "\n%3$sJob Commands:%4$s\n" |
fbf3283b | 7716 | " list-jobs [PATTERN...] List jobs\n" |
353b2baa LP |
7717 | " cancel [JOB...] Cancel all, one, or more jobs\n" |
7718 | "\n%3$sEnvironment Commands:%4$s\n" | |
fbf3283b | 7719 | " show-environment Dump environment\n" |
135775c1 ZJS |
7720 | " set-environment VARIABLE=VALUE... Set one or more environment variables\n" |
7721 | " unset-environment VARIABLE... Unset one or more environment variables\n" | |
4d985a31 | 7722 | " import-environment [VARIABLE...] Import all or some environment variables\n" |
38fcb7f7 | 7723 | "\n%3$sManager State Commands:%4$s\n" |
fbf3283b | 7724 | " daemon-reload Reload systemd manager configuration\n" |
353b2baa | 7725 | " daemon-reexec Reexecute systemd manager\n" |
38fcb7f7 ZJS |
7726 | " log-level [LEVEL] Get/set logging threshold for manager\n" |
7727 | " log-target [TARGET] Get/set logging target for manager\n" | |
6ab86319 | 7728 | " service-watchdogs [BOOL] Get/set service watchdog state\n" |
353b2baa | 7729 | "\n%3$sSystem Commands:%4$s\n" |
fbf3283b ZJS |
7730 | " is-system-running Check whether system is fully running\n" |
7731 | " default Enter system default mode\n" | |
7732 | " rescue Enter system rescue mode\n" | |
7733 | " emergency Enter system emergency mode\n" | |
7734 | " halt Shut down and halt the system\n" | |
7735 | " poweroff Shut down and power-off the system\n" | |
dae710be | 7736 | " reboot Shut down and reboot the system\n" |
fbf3283b ZJS |
7737 | " kexec Shut down and reboot the system with kexec\n" |
7738 | " exit [EXIT_CODE] Request user instance or container exit\n" | |
7739 | " switch-root ROOT [INIT] Change to a different root file system\n" | |
7740 | " suspend Suspend the system\n" | |
7741 | " hibernate Hibernate the system\n" | |
c58493c0 | 7742 | " hybrid-sleep Hibernate and suspend the system\n" |
e68c79db | 7743 | " suspend-then-hibernate Suspend the system, wake after a period of\n" |
4d985a31 | 7744 | " time, and hibernate" |
353b2baa | 7745 | "\n%3$sOptions:%4$s\n" |
4d985a31 ZJS |
7746 | " -h --help Show this help\n" |
7747 | " --version Show package version\n" | |
7748 | " --system Connect to system manager\n" | |
7749 | " --user Connect to user service manager\n" | |
7750 | " -H --host=[USER@]HOST Operate on remote host\n" | |
7751 | " -M --machine=CONTAINER Operate on a local container\n" | |
7752 | " -t --type=TYPE List units of a particular type\n" | |
7753 | " --state=STATE List units with particular LOAD or SUB or ACTIVE state\n" | |
c1c28fe2 | 7754 | " --failed Shortcut for --state=failed\n" |
4d985a31 | 7755 | " -p --property=NAME Show only properties by this name\n" |
5292c240 | 7756 | " -P NAME Equivalent to --value --property=NAME\n" |
4d985a31 ZJS |
7757 | " -a --all Show all properties/all units currently in memory,\n" |
7758 | " including dead/empty ones. To list all units installed\n" | |
7759 | " on the system, use 'list-unit-files' instead.\n" | |
7760 | " -l --full Don't ellipsize unit names on output\n" | |
7761 | " -r --recursive Show unit list of host and local containers\n" | |
7762 | " --reverse Show reverse dependencies with 'list-dependencies'\n" | |
e9c387c8 KK |
7763 | " --with-dependencies Show unit dependencies with 'status', 'cat',\n" |
7764 | " 'list-units', and 'list-unit-files'.\n" | |
4d985a31 ZJS |
7765 | " --job-mode=MODE Specify how to deal with already queued jobs, when\n" |
7766 | " queueing a new job\n" | |
7767 | " -T --show-transaction When enqueuing a unit job, show full transaction\n" | |
7768 | " --show-types When showing sockets, explicitly show their type\n" | |
7769 | " --value When showing properties, only print the value\n" | |
7770 | " -i --ignore-inhibitors When shutting down or sleeping, ignore inhibitors\n" | |
7771 | " --kill-who=WHO Whom to send signal to\n" | |
7772 | " -s --signal=SIGNAL Which signal to send\n" | |
7773 | " --what=RESOURCES Which types of resources to remove\n" | |
7774 | " --now Start or stop unit after enabling or disabling it\n" | |
7775 | " --dry-run Only print what would be done\n" | |
4afb4a9c BW |
7776 | " Currently supported by verbs: halt, poweroff, reboot,\n" |
7777 | " kexec, suspend, hibernate, suspend-then-hibernate,\n" | |
7778 | " hybrid-sleep, default, rescue, emergency, and exit.\n" | |
4d985a31 ZJS |
7779 | " -q --quiet Suppress output\n" |
7780 | " --wait For (re)start, wait until service stopped again\n" | |
7781 | " For is-system-running, wait until startup is completed\n" | |
7782 | " --no-block Do not wait until operation finished\n" | |
7783 | " --no-wall Don't send wall message before halt/power-off/reboot\n" | |
7784 | " --no-reload Don't reload daemon after en-/dis-abling unit files\n" | |
7785 | " --no-legend Do not print a legend (column headers and hints)\n" | |
7786 | " --no-pager Do not pipe output into a pager\n" | |
7787 | " --no-ask-password Do not ask for system passwords\n" | |
7788 | " --global Enable/disable/mask unit files globally\n" | |
7789 | " --runtime Enable/disable/mask unit files temporarily until next\n" | |
7790 | " reboot\n" | |
7791 | " -f --force When enabling unit files, override existing symlinks\n" | |
7792 | " When shutting down, execute action immediately\n" | |
7793 | " --preset-mode= Apply only enable, only disable, or all presets\n" | |
7794 | " --root=PATH Enable/disable/mask unit files in the specified root\n" | |
7795 | " directory\n" | |
7796 | " -n --lines=INTEGER Number of journal entries to show\n" | |
7797 | " -o --output=STRING Change journal output mode (short, short-precise,\n" | |
e1fac8a6 ZJS |
7798 | " short-iso, short-iso-precise, short-full,\n" |
7799 | " short-monotonic, short-unix,\n" | |
7800 | " verbose, export, json, json-pretty, json-sse, cat)\n" | |
4d985a31 | 7801 | " --firmware-setup Tell the firmware to show the setup menu on next boot\n" |
e1fac8a6 | 7802 | " --boot-loader-menu=TIME\n" |
4d985a31 | 7803 | " Boot into boot loader menu on next boot\n" |
e1fac8a6 | 7804 | " --boot-loader-entry=NAME\n" |
4d985a31 ZJS |
7805 | " Boot into a specific boot loader entry on next boot\n" |
7806 | " --plain Print unit dependencies as a list instead of a tree\n" | |
8e543004 | 7807 | "\nSee the %2$s for details.\n" |
37ec0fdd LP |
7808 | , program_invocation_short_name |
7809 | , link | |
8e543004 | 7810 | , ansi_underline(), ansi_normal() |
ce2529b4 | 7811 | , ansi_highlight(), ansi_normal() |
37ec0fdd LP |
7812 | ); |
7813 | ||
7814 | return 0; | |
7e4249b9 LP |
7815 | } |
7816 | ||
37ec0fdd LP |
7817 | static int halt_help(void) { |
7818 | _cleanup_free_ char *link = NULL; | |
7819 | int r; | |
7820 | ||
7821 | r = terminal_urlify_man("halt", "8", &link); | |
7822 | if (r < 0) | |
7823 | return log_oom(); | |
7824 | ||
353b2baa LP |
7825 | printf("%s [OPTIONS...]%s\n" |
7826 | "\n%s%s the system.%s\n" | |
7827 | "\nOptions:\n" | |
e4b61340 LP |
7828 | " --help Show this help\n" |
7829 | " --halt Halt the machine\n" | |
7830 | " -p --poweroff Switch off the machine\n" | |
7831 | " --reboot Reboot the machine\n" | |
2e33c433 LP |
7832 | " -f --force Force immediate halt/power-off/reboot\n" |
7833 | " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n" | |
e4b61340 | 7834 | " -d --no-wtmp Don't write wtmp record\n" |
37ec0fdd LP |
7835 | " --no-wall Don't send wall message before halt/power-off/reboot\n" |
7836 | "\nSee the %s for details.\n" | |
7837 | , program_invocation_short_name | |
353b2baa LP |
7838 | , arg_action == ACTION_REBOOT ? " [ARG]" : "" |
7839 | , ansi_highlight() | |
7840 | , arg_action == ACTION_REBOOT ? "Reboot" : | |
37ec0fdd LP |
7841 | arg_action == ACTION_POWEROFF ? "Power off" : |
7842 | "Halt" | |
353b2baa | 7843 | , ansi_normal() |
37ec0fdd LP |
7844 | , link |
7845 | ); | |
7846 | ||
7847 | return 0; | |
e4b61340 LP |
7848 | } |
7849 | ||
37ec0fdd LP |
7850 | static int shutdown_help(void) { |
7851 | _cleanup_free_ char *link = NULL; | |
7852 | int r; | |
7853 | ||
7854 | r = terminal_urlify_man("shutdown", "8", &link); | |
7855 | if (r < 0) | |
7856 | return log_oom(); | |
7857 | ||
353b2baa LP |
7858 | printf("%s [OPTIONS...] [TIME] [WALL...]\n" |
7859 | "\n%sShut down the system.%s\n" | |
7860 | "\nOptions:\n" | |
e4b61340 LP |
7861 | " --help Show this help\n" |
7862 | " -H --halt Halt the machine\n" | |
7863 | " -P --poweroff Power-off the machine\n" | |
7864 | " -r --reboot Reboot the machine\n" | |
386da858 | 7865 | " -h Equivalent to --poweroff, overridden by --halt\n" |
2e33c433 | 7866 | " -k Don't halt/power-off/reboot, just send warnings\n" |
f6144808 | 7867 | " --no-wall Don't send wall message before halt/power-off/reboot\n" |
37ec0fdd LP |
7868 | " -c Cancel a pending shutdown\n" |
7869 | "\nSee the %s for details.\n" | |
7870 | , program_invocation_short_name | |
353b2baa | 7871 | , ansi_highlight(), ansi_normal() |
37ec0fdd LP |
7872 | , link |
7873 | ); | |
7874 | ||
7875 | return 0; | |
e4b61340 LP |
7876 | } |
7877 | ||
37ec0fdd LP |
7878 | static int telinit_help(void) { |
7879 | _cleanup_free_ char *link = NULL; | |
7880 | int r; | |
7881 | ||
7882 | r = terminal_urlify_man("telinit", "8", &link); | |
7883 | if (r < 0) | |
7884 | return log_oom(); | |
7885 | ||
353b2baa LP |
7886 | printf("%s [OPTIONS...] COMMAND\n\n" |
7887 | "%sSend control commands to the init daemon.%s\n" | |
7888 | "\nCommands:\n" | |
e4b61340 LP |
7889 | " 0 Power-off the machine\n" |
7890 | " 6 Reboot the machine\n" | |
514f4ef5 LP |
7891 | " 2, 3, 4, 5 Start runlevelX.target unit\n" |
7892 | " 1, s, S Enter rescue mode\n" | |
7893 | " q, Q Reload init daemon configuration\n" | |
37ec0fdd | 7894 | " u, U Reexecute init daemon\n" |
353b2baa LP |
7895 | "\nOptions:\n" |
7896 | " --help Show this help\n" | |
7897 | " --no-wall Don't send wall message before halt/power-off/reboot\n" | |
37ec0fdd LP |
7898 | "\nSee the %s for details.\n" |
7899 | , program_invocation_short_name | |
353b2baa | 7900 | , ansi_highlight(), ansi_normal() |
37ec0fdd LP |
7901 | , link |
7902 | ); | |
7903 | ||
7904 | return 0; | |
e4b61340 LP |
7905 | } |
7906 | ||
37ec0fdd LP |
7907 | static int runlevel_help(void) { |
7908 | _cleanup_free_ char *link = NULL; | |
7909 | int r; | |
7910 | ||
7911 | r = terminal_urlify_man("runlevel", "8", &link); | |
7912 | if (r < 0) | |
7913 | return log_oom(); | |
7914 | ||
353b2baa LP |
7915 | printf("%s [OPTIONS...]\n" |
7916 | "\n%sPrints the previous and current runlevel of the init system.%s\n" | |
7917 | "\nOptions:\n" | |
37ec0fdd LP |
7918 | " --help Show this help\n" |
7919 | "\nSee the %s for details.\n" | |
7920 | , program_invocation_short_name | |
353b2baa | 7921 | , ansi_highlight(), ansi_normal() |
37ec0fdd LP |
7922 | , link |
7923 | ); | |
7924 | ||
7925 | return 0; | |
e4b61340 LP |
7926 | } |
7927 | ||
b93312f5 | 7928 | static void help_types(void) { |
b93312f5 ZJS |
7929 | if (!arg_no_legend) |
7930 | puts("Available unit types:"); | |
5c828e66 LP |
7931 | |
7932 | DUMP_STRING_TABLE(unit_type, UnitType, _UNIT_TYPE_MAX); | |
e16972e6 ZJS |
7933 | } |
7934 | ||
7935 | static void help_states(void) { | |
e16972e6 ZJS |
7936 | if (!arg_no_legend) |
7937 | puts("Available unit load states:"); | |
5c828e66 | 7938 | DUMP_STRING_TABLE(unit_load_state, UnitLoadState, _UNIT_LOAD_STATE_MAX); |
e16972e6 ZJS |
7939 | |
7940 | if (!arg_no_legend) | |
7941 | puts("\nAvailable unit active states:"); | |
5c828e66 | 7942 | DUMP_STRING_TABLE(unit_active_state, UnitActiveState, _UNIT_ACTIVE_STATE_MAX); |
7e55de3b | 7943 | |
46ff70ca RP |
7944 | if (!arg_no_legend) |
7945 | puts("\nAvailable unit file states:"); | |
7946 | DUMP_STRING_TABLE(unit_file_state, UnitFileState, _UNIT_FILE_STATE_MAX); | |
7947 | ||
7e55de3b ZJS |
7948 | if (!arg_no_legend) |
7949 | puts("\nAvailable automount unit substates:"); | |
5c828e66 | 7950 | DUMP_STRING_TABLE(automount_state, AutomountState, _AUTOMOUNT_STATE_MAX); |
7e55de3b | 7951 | |
7e55de3b ZJS |
7952 | if (!arg_no_legend) |
7953 | puts("\nAvailable device unit substates:"); | |
5c828e66 | 7954 | DUMP_STRING_TABLE(device_state, DeviceState, _DEVICE_STATE_MAX); |
7e55de3b ZJS |
7955 | |
7956 | if (!arg_no_legend) | |
7957 | puts("\nAvailable mount unit substates:"); | |
5c828e66 | 7958 | DUMP_STRING_TABLE(mount_state, MountState, _MOUNT_STATE_MAX); |
7e55de3b ZJS |
7959 | |
7960 | if (!arg_no_legend) | |
7961 | puts("\nAvailable path unit substates:"); | |
5c828e66 | 7962 | DUMP_STRING_TABLE(path_state, PathState, _PATH_STATE_MAX); |
7e55de3b ZJS |
7963 | |
7964 | if (!arg_no_legend) | |
7965 | puts("\nAvailable scope unit substates:"); | |
5c828e66 | 7966 | DUMP_STRING_TABLE(scope_state, ScopeState, _SCOPE_STATE_MAX); |
7e55de3b ZJS |
7967 | |
7968 | if (!arg_no_legend) | |
7969 | puts("\nAvailable service unit substates:"); | |
5c828e66 | 7970 | DUMP_STRING_TABLE(service_state, ServiceState, _SERVICE_STATE_MAX); |
7e55de3b ZJS |
7971 | |
7972 | if (!arg_no_legend) | |
7973 | puts("\nAvailable slice unit substates:"); | |
5c828e66 | 7974 | DUMP_STRING_TABLE(slice_state, SliceState, _SLICE_STATE_MAX); |
7e55de3b | 7975 | |
7e55de3b ZJS |
7976 | if (!arg_no_legend) |
7977 | puts("\nAvailable socket unit substates:"); | |
5c828e66 | 7978 | DUMP_STRING_TABLE(socket_state, SocketState, _SOCKET_STATE_MAX); |
7e55de3b ZJS |
7979 | |
7980 | if (!arg_no_legend) | |
7981 | puts("\nAvailable swap unit substates:"); | |
5c828e66 | 7982 | DUMP_STRING_TABLE(swap_state, SwapState, _SWAP_STATE_MAX); |
7e55de3b ZJS |
7983 | |
7984 | if (!arg_no_legend) | |
7985 | puts("\nAvailable target unit substates:"); | |
5c828e66 | 7986 | DUMP_STRING_TABLE(target_state, TargetState, _TARGET_STATE_MAX); |
7e55de3b ZJS |
7987 | |
7988 | if (!arg_no_legend) | |
7989 | puts("\nAvailable timer unit substates:"); | |
5c828e66 | 7990 | DUMP_STRING_TABLE(timer_state, TimerState, _TIMER_STATE_MAX); |
45c0c61d ZJS |
7991 | } |
7992 | ||
97af80c5 LP |
7993 | static int help_boot_loader_entry(void) { |
7994 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
13f697b7 | 7995 | _cleanup_strv_free_ char **l = NULL; |
97af80c5 LP |
7996 | sd_bus *bus; |
7997 | char **i; | |
7998 | int r; | |
7999 | ||
8000 | r = acquire_bus(BUS_FULL, &bus); | |
8001 | if (r < 0) | |
8002 | return r; | |
8003 | ||
8010c205 | 8004 | r = bus_get_property_strv(bus, bus_login_mgr, "BootLoaderEntries", &error, &l); |
97af80c5 LP |
8005 | if (r < 0) |
8006 | return log_error_errno(r, "Failed to enumerate boot loader entries: %s", bus_error_message(&error, r)); | |
8007 | ||
2f50e59f LP |
8008 | if (strv_isempty(l)) |
8009 | return log_error_errno(SYNTHETIC_ERRNO(ENODATA), "No boot loader entries discovered."); | |
97af80c5 LP |
8010 | |
8011 | STRV_FOREACH(i, l) | |
8012 | puts(*i); | |
8013 | ||
8014 | return 0; | |
8015 | } | |
8016 | ||
e4b61340 | 8017 | static int systemctl_parse_argv(int argc, char *argv[]) { |
7e4249b9 | 8018 | enum { |
90d473a1 | 8019 | ARG_FAIL = 0x100, |
afba4199 ZJS |
8020 | ARG_REVERSE, |
8021 | ARG_AFTER, | |
8022 | ARG_BEFORE, | |
1ae17672 | 8023 | ARG_DRY_RUN, |
991f2a39 | 8024 | ARG_SHOW_TYPES, |
23ade460 | 8025 | ARG_IRREVERSIBLE, |
e67c3609 | 8026 | ARG_IGNORE_DEPENDENCIES, |
4f9a9105 | 8027 | ARG_VALUE, |
35df8f27 | 8028 | ARG_VERSION, |
af2d49f7 | 8029 | ARG_USER, |
7e4249b9 | 8030 | ARG_SYSTEM, |
ee5762e3 | 8031 | ARG_GLOBAL, |
6e905d93 | 8032 | ARG_NO_BLOCK, |
ebed32bf | 8033 | ARG_NO_LEGEND, |
611efaac | 8034 | ARG_NO_PAGER, |
4445a875 | 8035 | ARG_NO_WALL, |
be394c48 | 8036 | ARG_ROOT, |
ee5762e3 | 8037 | ARG_NO_RELOAD, |
501fc174 | 8038 | ARG_KILL_WHO, |
30732560 | 8039 | ARG_NO_ASK_PASSWORD, |
729e3769 | 8040 | ARG_FAILED, |
df50185b | 8041 | ARG_RUNTIME, |
9b9b3d36 | 8042 | ARG_PLAIN, |
4dc5b821 | 8043 | ARG_STATE, |
d309c1c3 LP |
8044 | ARG_JOB_MODE, |
8045 | ARG_PRESET_MODE, | |
5bdf2243 | 8046 | ARG_FIRMWARE_SETUP, |
97af80c5 LP |
8047 | ARG_BOOT_LOADER_MENU, |
8048 | ARG_BOOT_LOADER_ENTRY, | |
57ab2eab | 8049 | ARG_NOW, |
9ef15026 | 8050 | ARG_MESSAGE, |
e9c387c8 | 8051 | ARG_WITH_DEPENDENCIES, |
93a08841 | 8052 | ARG_WAIT, |
d81bc51f | 8053 | ARG_WHAT, |
dae710be | 8054 | ARG_REBOOT_ARG, |
7e4249b9 LP |
8055 | }; |
8056 | ||
8057 | static const struct option options[] = { | |
9ea9d4cf LP |
8058 | { "help", no_argument, NULL, 'h' }, |
8059 | { "version", no_argument, NULL, ARG_VERSION }, | |
8060 | { "type", required_argument, NULL, 't' }, | |
8061 | { "property", required_argument, NULL, 'p' }, | |
8062 | { "all", no_argument, NULL, 'a' }, | |
8063 | { "reverse", no_argument, NULL, ARG_REVERSE }, | |
8064 | { "after", no_argument, NULL, ARG_AFTER }, | |
8065 | { "before", no_argument, NULL, ARG_BEFORE }, | |
8066 | { "show-types", no_argument, NULL, ARG_SHOW_TYPES }, | |
8067 | { "failed", no_argument, NULL, ARG_FAILED }, /* compatibility only */ | |
8068 | { "full", no_argument, NULL, 'l' }, | |
4dc5b821 LP |
8069 | { "job-mode", required_argument, NULL, ARG_JOB_MODE }, |
8070 | { "fail", no_argument, NULL, ARG_FAIL }, /* compatibility only */ | |
8071 | { "irreversible", no_argument, NULL, ARG_IRREVERSIBLE }, /* compatibility only */ | |
8072 | { "ignore-dependencies", no_argument, NULL, ARG_IGNORE_DEPENDENCIES }, /* compatibility only */ | |
9ea9d4cf | 8073 | { "ignore-inhibitors", no_argument, NULL, 'i' }, |
4f9a9105 | 8074 | { "value", no_argument, NULL, ARG_VALUE }, |
9ea9d4cf LP |
8075 | { "user", no_argument, NULL, ARG_USER }, |
8076 | { "system", no_argument, NULL, ARG_SYSTEM }, | |
8077 | { "global", no_argument, NULL, ARG_GLOBAL }, | |
93a08841 | 8078 | { "wait", no_argument, NULL, ARG_WAIT }, |
9ea9d4cf LP |
8079 | { "no-block", no_argument, NULL, ARG_NO_BLOCK }, |
8080 | { "no-legend", no_argument, NULL, ARG_NO_LEGEND }, | |
8081 | { "no-pager", no_argument, NULL, ARG_NO_PAGER }, | |
8082 | { "no-wall", no_argument, NULL, ARG_NO_WALL }, | |
1ae17672 | 8083 | { "dry-run", no_argument, NULL, ARG_DRY_RUN }, |
9ea9d4cf LP |
8084 | { "quiet", no_argument, NULL, 'q' }, |
8085 | { "root", required_argument, NULL, ARG_ROOT }, | |
68da321f | 8086 | { "force", no_argument, NULL, 'f' }, |
9ea9d4cf LP |
8087 | { "no-reload", no_argument, NULL, ARG_NO_RELOAD }, |
8088 | { "kill-who", required_argument, NULL, ARG_KILL_WHO }, | |
8089 | { "signal", required_argument, NULL, 's' }, | |
8090 | { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD }, | |
8091 | { "host", required_argument, NULL, 'H' }, | |
f459b602 | 8092 | { "machine", required_argument, NULL, 'M' }, |
9ea9d4cf LP |
8093 | { "runtime", no_argument, NULL, ARG_RUNTIME }, |
8094 | { "lines", required_argument, NULL, 'n' }, | |
8095 | { "output", required_argument, NULL, 'o' }, | |
8096 | { "plain", no_argument, NULL, ARG_PLAIN }, | |
8097 | { "state", required_argument, NULL, ARG_STATE }, | |
1238ee09 | 8098 | { "recursive", no_argument, NULL, 'r' }, |
e9c387c8 | 8099 | { "with-dependencies", no_argument, NULL, ARG_WITH_DEPENDENCIES }, |
d309c1c3 | 8100 | { "preset-mode", required_argument, NULL, ARG_PRESET_MODE }, |
5bdf2243 | 8101 | { "firmware-setup", no_argument, NULL, ARG_FIRMWARE_SETUP }, |
97af80c5 LP |
8102 | { "boot-loader-menu", required_argument, NULL, ARG_BOOT_LOADER_MENU }, |
8103 | { "boot-loader-entry", required_argument, NULL, ARG_BOOT_LOADER_ENTRY }, | |
57ab2eab | 8104 | { "now", no_argument, NULL, ARG_NOW }, |
9ef15026 | 8105 | { "message", required_argument, NULL, ARG_MESSAGE }, |
85d9b598 | 8106 | { "show-transaction", no_argument, NULL, 'T' }, |
d81bc51f | 8107 | { "what", required_argument, NULL, ARG_WHAT }, |
dae710be | 8108 | { "reboot-argument", required_argument, NULL, ARG_REBOOT_ARG }, |
eb9da376 | 8109 | {} |
7e4249b9 LP |
8110 | }; |
8111 | ||
0f03c2a4 | 8112 | int c, r; |
7e4249b9 | 8113 | |
e4b61340 | 8114 | assert(argc >= 0); |
7e4249b9 LP |
8115 | assert(argv); |
8116 | ||
16f017fa IS |
8117 | /* we default to allowing interactive authorization only in systemctl (not in the legacy commands) */ |
8118 | arg_ask_password = true; | |
8119 | ||
5292c240 | 8120 | while ((c = getopt_long(argc, argv, "ht:p:P:alqfs:H:M:n:o:iTr.::", options, NULL)) >= 0) |
7e4249b9 LP |
8121 | |
8122 | switch (c) { | |
8123 | ||
8124 | case 'h': | |
37ec0fdd | 8125 | return systemctl_help(); |
35df8f27 LP |
8126 | |
8127 | case ARG_VERSION: | |
3f6fd1ba | 8128 | return version(); |
7e4249b9 | 8129 | |
20b3f379 | 8130 | case 't': { |
fb1ac50e LP |
8131 | const char *p; |
8132 | ||
baaa35ad ZJS |
8133 | if (isempty(optarg)) |
8134 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8135 | "--type= requires arguments."); | |
45c0c61d | 8136 | |
c58bd76a | 8137 | for (p = optarg;;) { |
5ab22f33 | 8138 | _cleanup_free_ char *type = NULL; |
20b3f379 | 8139 | |
5ab22f33 SS |
8140 | r = extract_first_word(&p, &type, ",", 0); |
8141 | if (r < 0) | |
8142 | return log_error_errno(r, "Failed to parse type: %s", optarg); | |
5ab22f33 SS |
8143 | if (r == 0) |
8144 | break; | |
20b3f379 ZJS |
8145 | |
8146 | if (streq(type, "help")) { | |
8147 | help_types(); | |
8148 | return 0; | |
8149 | } | |
8150 | ||
8151 | if (unit_type_from_string(type) >= 0) { | |
96e895ef | 8152 | if (strv_consume(&arg_types, TAKE_PTR(type)) < 0) |
20b3f379 | 8153 | return log_oom(); |
20b3f379 ZJS |
8154 | continue; |
8155 | } | |
8156 | ||
9b9b3d36 MW |
8157 | /* It's much nicer to use --state= for |
8158 | * load states, but let's support this | |
8159 | * in --types= too for compatibility | |
8160 | * with old versions */ | |
7e9d36e0 | 8161 | if (unit_load_state_from_string(type) >= 0) { |
96e895ef | 8162 | if (strv_consume(&arg_states, TAKE_PTR(type)) < 0) |
20b3f379 | 8163 | return log_oom(); |
20b3f379 ZJS |
8164 | continue; |
8165 | } | |
8166 | ||
ab06eef8 | 8167 | log_error("Unknown unit type or load state '%s'.", type); |
baaa35ad ZJS |
8168 | return log_info_errno(SYNTHETIC_ERRNO(EINVAL), |
8169 | "Use -t help to see a list of allowed values."); | |
c147dc42 | 8170 | } |
20b3f379 ZJS |
8171 | |
8172 | break; | |
8173 | } | |
8174 | ||
5292c240 ZJS |
8175 | case 'P': |
8176 | arg_value = true; | |
8177 | _fallthrough_; | |
8178 | ||
aee7082f LP |
8179 | case 'p': |
8180 | /* Make sure that if the empty property list was specified, we won't show any | |
8181 | properties. */ | |
20b3f379 | 8182 | if (isempty(optarg) && !arg_properties) { |
cbc9fbd1 | 8183 | arg_properties = new0(char*, 1); |
20b3f379 ZJS |
8184 | if (!arg_properties) |
8185 | return log_oom(); | |
fb1ac50e LP |
8186 | } else { |
8187 | const char *p; | |
8188 | ||
c58bd76a | 8189 | for (p = optarg;;) { |
5ab22f33 | 8190 | _cleanup_free_ char *prop = NULL; |
033a842c | 8191 | |
5ab22f33 SS |
8192 | r = extract_first_word(&p, &prop, ",", 0); |
8193 | if (r < 0) | |
8194 | return log_error_errno(r, "Failed to parse property: %s", optarg); | |
5ab22f33 SS |
8195 | if (r == 0) |
8196 | break; | |
ea4a240d | 8197 | |
9dccc6d3 | 8198 | if (strv_consume(&arg_properties, TAKE_PTR(prop)) < 0) |
20b3f379 | 8199 | return log_oom(); |
20b3f379 | 8200 | } |
fb1ac50e | 8201 | } |
48220598 | 8202 | |
5292c240 | 8203 | /* If the user asked for a particular property, show it, even if it is empty. */ |
48220598 | 8204 | arg_all = true; |
033a842c | 8205 | |
48220598 LP |
8206 | break; |
8207 | ||
7e4249b9 LP |
8208 | case 'a': |
8209 | arg_all = true; | |
8210 | break; | |
8211 | ||
afba4199 ZJS |
8212 | case ARG_REVERSE: |
8213 | arg_dependency = DEPENDENCY_REVERSE; | |
8214 | break; | |
8215 | ||
8216 | case ARG_AFTER: | |
8217 | arg_dependency = DEPENDENCY_AFTER; | |
82948f6c | 8218 | arg_jobs_after = true; |
afba4199 ZJS |
8219 | break; |
8220 | ||
8221 | case ARG_BEFORE: | |
8222 | arg_dependency = DEPENDENCY_BEFORE; | |
82948f6c | 8223 | arg_jobs_before = true; |
afba4199 ZJS |
8224 | break; |
8225 | ||
991f2a39 ZJS |
8226 | case ARG_SHOW_TYPES: |
8227 | arg_show_types = true; | |
8228 | break; | |
8229 | ||
4f9a9105 ZJS |
8230 | case ARG_VALUE: |
8231 | arg_value = true; | |
8232 | break; | |
8233 | ||
4dc5b821 LP |
8234 | case ARG_JOB_MODE: |
8235 | arg_job_mode = optarg; | |
8236 | break; | |
8237 | ||
90d473a1 | 8238 | case ARG_FAIL: |
e67c3609 LP |
8239 | arg_job_mode = "fail"; |
8240 | break; | |
8241 | ||
23ade460 MS |
8242 | case ARG_IRREVERSIBLE: |
8243 | arg_job_mode = "replace-irreversibly"; | |
8244 | break; | |
8245 | ||
e67c3609 LP |
8246 | case ARG_IGNORE_DEPENDENCIES: |
8247 | arg_job_mode = "ignore-dependencies"; | |
7e4249b9 LP |
8248 | break; |
8249 | ||
af2d49f7 | 8250 | case ARG_USER: |
729e3769 | 8251 | arg_scope = UNIT_FILE_USER; |
7e4249b9 LP |
8252 | break; |
8253 | ||
8254 | case ARG_SYSTEM: | |
729e3769 LP |
8255 | arg_scope = UNIT_FILE_SYSTEM; |
8256 | break; | |
8257 | ||
8258 | case ARG_GLOBAL: | |
8259 | arg_scope = UNIT_FILE_GLOBAL; | |
7e4249b9 LP |
8260 | break; |
8261 | ||
93a08841 MP |
8262 | case ARG_WAIT: |
8263 | arg_wait = true; | |
8264 | break; | |
8265 | ||
6e905d93 LP |
8266 | case ARG_NO_BLOCK: |
8267 | arg_no_block = true; | |
7e4249b9 LP |
8268 | break; |
8269 | ||
ebed32bf MS |
8270 | case ARG_NO_LEGEND: |
8271 | arg_no_legend = true; | |
8272 | break; | |
8273 | ||
611efaac | 8274 | case ARG_NO_PAGER: |
0221d68a | 8275 | arg_pager_flags |= PAGER_DISABLE; |
611efaac | 8276 | break; |
0736af98 | 8277 | |
514f4ef5 LP |
8278 | case ARG_NO_WALL: |
8279 | arg_no_wall = true; | |
8280 | break; | |
8281 | ||
be394c48 | 8282 | case ARG_ROOT: |
bac75eb3 | 8283 | r = parse_path_argument_and_warn(optarg, false, &arg_root); |
0f03c2a4 LP |
8284 | if (r < 0) |
8285 | return r; | |
be394c48 FC |
8286 | break; |
8287 | ||
98a6e132 | 8288 | case 'l': |
8fe914ec LP |
8289 | arg_full = true; |
8290 | break; | |
8291 | ||
30732560 | 8292 | case ARG_FAILED: |
9b9b3d36 MW |
8293 | if (strv_extend(&arg_states, "failed") < 0) |
8294 | return log_oom(); | |
8295 | ||
30732560 LP |
8296 | break; |
8297 | ||
1ae17672 ZJS |
8298 | case ARG_DRY_RUN: |
8299 | arg_dry_run = true; | |
8300 | break; | |
8301 | ||
0183528f LP |
8302 | case 'q': |
8303 | arg_quiet = true; | |
8304 | break; | |
8305 | ||
b4f27ccc | 8306 | case 'f': |
313cefa1 | 8307 | arg_force++; |
ee5762e3 LP |
8308 | break; |
8309 | ||
8310 | case ARG_NO_RELOAD: | |
8311 | arg_no_reload = true; | |
8312 | break; | |
8313 | ||
8a0867d6 LP |
8314 | case ARG_KILL_WHO: |
8315 | arg_kill_who = optarg; | |
8316 | break; | |
8317 | ||
8a0867d6 | 8318 | case 's': |
5c828e66 LP |
8319 | if (streq(optarg, "help")) { |
8320 | DUMP_STRING_TABLE(signal, int, _NSIG); | |
8321 | return 0; | |
8322 | } | |
8323 | ||
29a3db75 | 8324 | arg_signal = signal_from_string(optarg); |
baaa35ad ZJS |
8325 | if (arg_signal < 0) |
8326 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8327 | "Failed to parse signal string %s.", | |
8328 | optarg); | |
8a0867d6 LP |
8329 | break; |
8330 | ||
501fc174 LP |
8331 | case ARG_NO_ASK_PASSWORD: |
8332 | arg_ask_password = false; | |
8333 | break; | |
8334 | ||
f459b602 MAP |
8335 | case 'H': |
8336 | arg_transport = BUS_TRANSPORT_REMOTE; | |
8337 | arg_host = optarg; | |
a8f11321 LP |
8338 | break; |
8339 | ||
f459b602 | 8340 | case 'M': |
de33fc62 | 8341 | arg_transport = BUS_TRANSPORT_MACHINE; |
f459b602 | 8342 | arg_host = optarg; |
a8f11321 LP |
8343 | break; |
8344 | ||
729e3769 LP |
8345 | case ARG_RUNTIME: |
8346 | arg_runtime = true; | |
8347 | break; | |
8348 | ||
df50185b | 8349 | case 'n': |
baaa35ad ZJS |
8350 | if (safe_atou(optarg, &arg_lines) < 0) |
8351 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8352 | "Failed to parse lines '%s'", | |
8353 | optarg); | |
df50185b LP |
8354 | break; |
8355 | ||
df50185b | 8356 | case 'o': |
5c828e66 LP |
8357 | if (streq(optarg, "help")) { |
8358 | DUMP_STRING_TABLE(output_mode, OutputMode, _OUTPUT_MODE_MAX); | |
8359 | return 0; | |
8360 | } | |
8361 | ||
df50185b | 8362 | arg_output = output_mode_from_string(optarg); |
baaa35ad ZJS |
8363 | if (arg_output < 0) |
8364 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8365 | "Unknown output '%s'.", | |
8366 | optarg); | |
49cd06fa LP |
8367 | |
8368 | if (OUTPUT_MODE_IS_JSON(arg_output)) { | |
8369 | arg_no_legend = true; | |
8370 | arg_plain = true; | |
8371 | } | |
df50185b LP |
8372 | break; |
8373 | ||
b37844d3 LP |
8374 | case 'i': |
8375 | arg_ignore_inhibitors = true; | |
8376 | break; | |
8377 | ||
5d0c05e5 LN |
8378 | case ARG_PLAIN: |
8379 | arg_plain = true; | |
8380 | break; | |
8381 | ||
5bdf2243 JJ |
8382 | case ARG_FIRMWARE_SETUP: |
8383 | arg_firmware_setup = true; | |
8384 | break; | |
8385 | ||
97af80c5 LP |
8386 | case ARG_BOOT_LOADER_MENU: |
8387 | ||
8388 | r = parse_sec(optarg, &arg_boot_loader_menu); | |
8389 | if (r < 0) | |
8390 | return log_error_errno(r, "Failed to parse --boot-loader-menu= argument '%s': %m", optarg); | |
8391 | ||
8392 | break; | |
8393 | ||
8394 | case ARG_BOOT_LOADER_ENTRY: | |
8395 | ||
8396 | if (streq(optarg, "help")) { /* Yes, this means, "help" is not a valid boot loader entry name we can deal with */ | |
8397 | r = help_boot_loader_entry(); | |
8398 | if (r < 0) | |
8399 | return r; | |
8400 | ||
8401 | return 0; | |
8402 | } | |
8403 | ||
8404 | arg_boot_loader_entry = empty_to_null(optarg); | |
8405 | break; | |
8406 | ||
9b9b3d36 | 8407 | case ARG_STATE: { |
fb1ac50e LP |
8408 | const char *p; |
8409 | ||
baaa35ad ZJS |
8410 | if (isempty(optarg)) |
8411 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8412 | "--state= requires arguments."); | |
9b9b3d36 | 8413 | |
c58bd76a | 8414 | for (p = optarg;;) { |
bc6c18fe | 8415 | _cleanup_free_ char *s = NULL; |
9b9b3d36 | 8416 | |
5ab22f33 SS |
8417 | r = extract_first_word(&p, &s, ",", 0); |
8418 | if (r < 0) | |
6862111e | 8419 | return log_error_errno(r, "Failed to parse state: %s", optarg); |
5ab22f33 SS |
8420 | if (r == 0) |
8421 | break; | |
9b9b3d36 | 8422 | |
e16972e6 ZJS |
8423 | if (streq(s, "help")) { |
8424 | help_states(); | |
8425 | return 0; | |
8426 | } | |
8427 | ||
9dccc6d3 | 8428 | if (strv_consume(&arg_states, TAKE_PTR(s)) < 0) |
9b9b3d36 | 8429 | return log_oom(); |
9b9b3d36 MW |
8430 | } |
8431 | break; | |
8432 | } | |
8433 | ||
1238ee09 | 8434 | case 'r': |
baaa35ad ZJS |
8435 | if (geteuid() != 0) |
8436 | return log_error_errno(SYNTHETIC_ERRNO(EPERM), | |
8437 | "--recursive requires root privileges."); | |
1238ee09 LP |
8438 | |
8439 | arg_recursive = true; | |
8440 | break; | |
8441 | ||
d309c1c3 | 8442 | case ARG_PRESET_MODE: |
5c828e66 LP |
8443 | if (streq(optarg, "help")) { |
8444 | DUMP_STRING_TABLE(unit_file_preset_mode, UnitFilePresetMode, _UNIT_FILE_PRESET_MAX); | |
8445 | return 0; | |
8446 | } | |
d309c1c3 LP |
8447 | |
8448 | arg_preset_mode = unit_file_preset_mode_from_string(optarg); | |
baaa35ad ZJS |
8449 | if (arg_preset_mode < 0) |
8450 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8451 | "Failed to parse preset mode: %s.", optarg); | |
d309c1c3 LP |
8452 | |
8453 | break; | |
8454 | ||
57ab2eab JS |
8455 | case ARG_NOW: |
8456 | arg_now = true; | |
8457 | break; | |
8458 | ||
9ef15026 JS |
8459 | case ARG_MESSAGE: |
8460 | if (strv_extend(&arg_wall, optarg) < 0) | |
8461 | return log_oom(); | |
8462 | break; | |
8463 | ||
85d9b598 LP |
8464 | case 'T': |
8465 | arg_show_transaction = true; | |
8466 | break; | |
8467 | ||
e9c387c8 KK |
8468 | case ARG_WITH_DEPENDENCIES: |
8469 | arg_with_dependencies = true; | |
8470 | break; | |
8471 | ||
d81bc51f LP |
8472 | case ARG_WHAT: { |
8473 | const char *p; | |
8474 | ||
8475 | if (isempty(optarg)) | |
8476 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--what= requires arguments."); | |
8477 | ||
8478 | for (p = optarg;;) { | |
8479 | _cleanup_free_ char *k = NULL; | |
8480 | ||
8481 | r = extract_first_word(&p, &k, ",", 0); | |
8482 | if (r < 0) | |
8483 | return log_error_errno(r, "Failed to parse directory type: %s", optarg); | |
8484 | if (r == 0) | |
8485 | break; | |
8486 | ||
8487 | if (streq(k, "help")) { | |
8488 | puts("runtime\n" | |
8489 | "state\n" | |
8490 | "cache\n" | |
8491 | "logs\n" | |
8492 | "configuration"); | |
8493 | return 0; | |
8494 | } | |
8495 | ||
8496 | r = strv_consume(&arg_clean_what, TAKE_PTR(k)); | |
8497 | if (r < 0) | |
8498 | return log_oom(); | |
8499 | } | |
8500 | ||
8501 | break; | |
8502 | } | |
8503 | ||
dae710be | 8504 | case ARG_REBOOT_ARG: |
8505 | arg_reboot_argument = optarg; | |
8506 | break; | |
8507 | ||
2e7e19ca ZJS |
8508 | case '.': |
8509 | /* Output an error mimicking getopt, and print a hint afterwards */ | |
8510 | log_error("%s: invalid option -- '.'", program_invocation_name); | |
8511 | log_notice("Hint: to specify units starting with a dash, use \"--\":\n" | |
353b2baa | 8512 | " %s [OPTIONS...] COMMAND -- -.%s ...", |
2e7e19ca ZJS |
8513 | program_invocation_name, optarg ?: "mount"); |
8514 | _fallthrough_; | |
8515 | ||
7e4249b9 LP |
8516 | case '?': |
8517 | return -EINVAL; | |
8518 | ||
8519 | default: | |
eb9da376 | 8520 | assert_not_reached("Unhandled option"); |
7e4249b9 | 8521 | } |
7e4249b9 | 8522 | |
baaa35ad ZJS |
8523 | if (arg_transport != BUS_TRANSPORT_LOCAL && arg_scope != UNIT_FILE_SYSTEM) |
8524 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8525 | "Cannot access user instance remotely."); | |
a8f11321 | 8526 | |
baaa35ad ZJS |
8527 | if (arg_wait && arg_no_block) |
8528 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8529 | "--wait may not be combined with --no-block."); | |
93a08841 | 8530 | |
7e4249b9 LP |
8531 | return 1; |
8532 | } | |
8533 | ||
e4b61340 | 8534 | static int halt_parse_argv(int argc, char *argv[]) { |
e4b61340 LP |
8535 | enum { |
8536 | ARG_HELP = 0x100, | |
8537 | ARG_HALT, | |
514f4ef5 LP |
8538 | ARG_REBOOT, |
8539 | ARG_NO_WALL | |
e4b61340 LP |
8540 | }; |
8541 | ||
8542 | static const struct option options[] = { | |
8543 | { "help", no_argument, NULL, ARG_HELP }, | |
8544 | { "halt", no_argument, NULL, ARG_HALT }, | |
8545 | { "poweroff", no_argument, NULL, 'p' }, | |
8546 | { "reboot", no_argument, NULL, ARG_REBOOT }, | |
8547 | { "force", no_argument, NULL, 'f' }, | |
8548 | { "wtmp-only", no_argument, NULL, 'w' }, | |
8549 | { "no-wtmp", no_argument, NULL, 'd' }, | |
f3f054f0 | 8550 | { "no-sync", no_argument, NULL, 'n' }, |
514f4ef5 | 8551 | { "no-wall", no_argument, NULL, ARG_NO_WALL }, |
eb9da376 | 8552 | {} |
e4b61340 LP |
8553 | }; |
8554 | ||
37185ec8 | 8555 | int c, r, runlevel; |
e4b61340 LP |
8556 | |
8557 | assert(argc >= 0); | |
8558 | assert(argv); | |
8559 | ||
8560 | if (utmp_get_runlevel(&runlevel, NULL) >= 0) | |
4c701096 | 8561 | if (IN_SET(runlevel, '0', '6')) |
65491fd8 | 8562 | arg_force = 2; |
e4b61340 | 8563 | |
601185b4 | 8564 | while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) |
e4b61340 LP |
8565 | switch (c) { |
8566 | ||
8567 | case ARG_HELP: | |
37ec0fdd | 8568 | return halt_help(); |
e4b61340 LP |
8569 | |
8570 | case ARG_HALT: | |
8571 | arg_action = ACTION_HALT; | |
8572 | break; | |
8573 | ||
8574 | case 'p': | |
a042efad MS |
8575 | if (arg_action != ACTION_REBOOT) |
8576 | arg_action = ACTION_POWEROFF; | |
e4b61340 LP |
8577 | break; |
8578 | ||
8579 | case ARG_REBOOT: | |
8580 | arg_action = ACTION_REBOOT; | |
8581 | break; | |
8582 | ||
8583 | case 'f': | |
65491fd8 | 8584 | arg_force = 2; |
e4b61340 LP |
8585 | break; |
8586 | ||
8587 | case 'w': | |
1ae17672 | 8588 | arg_dry_run = true; |
e4b61340 LP |
8589 | break; |
8590 | ||
8591 | case 'd': | |
8592 | arg_no_wtmp = true; | |
8593 | break; | |
8594 | ||
f3f054f0 TB |
8595 | case 'n': |
8596 | arg_no_sync = true; | |
8597 | break; | |
8598 | ||
514f4ef5 LP |
8599 | case ARG_NO_WALL: |
8600 | arg_no_wall = true; | |
8601 | break; | |
8602 | ||
e4b61340 LP |
8603 | case 'i': |
8604 | case 'h': | |
8605 | /* Compatibility nops */ | |
8606 | break; | |
8607 | ||
8608 | case '?': | |
8609 | return -EINVAL; | |
8610 | ||
8611 | default: | |
eb9da376 | 8612 | assert_not_reached("Unhandled option"); |
e4b61340 | 8613 | } |
e4b61340 | 8614 | |
c5220a94 | 8615 | if (arg_action == ACTION_REBOOT && (argc == optind || argc == optind + 1)) { |
77defcf5 | 8616 | r = update_reboot_parameter_and_warn(argc == optind + 1 ? argv[optind] : NULL, false); |
c5220a94 | 8617 | if (r < 0) |
37185ec8 | 8618 | return r; |
baaa35ad ZJS |
8619 | } else if (optind < argc) |
8620 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8621 | "Too many arguments."); | |
e4b61340 LP |
8622 | |
8623 | return 1; | |
8624 | } | |
8625 | ||
8626 | static int shutdown_parse_argv(int argc, char *argv[]) { | |
e4b61340 LP |
8627 | enum { |
8628 | ARG_HELP = 0x100, | |
514f4ef5 | 8629 | ARG_NO_WALL |
e4b61340 LP |
8630 | }; |
8631 | ||
8632 | static const struct option options[] = { | |
8633 | { "help", no_argument, NULL, ARG_HELP }, | |
8634 | { "halt", no_argument, NULL, 'H' }, | |
8635 | { "poweroff", no_argument, NULL, 'P' }, | |
8636 | { "reboot", no_argument, NULL, 'r' }, | |
04ebb595 | 8637 | { "kexec", no_argument, NULL, 'K' }, /* not documented extension */ |
514f4ef5 | 8638 | { "no-wall", no_argument, NULL, ARG_NO_WALL }, |
eb9da376 | 8639 | {} |
e4b61340 LP |
8640 | }; |
8641 | ||
172d7abf | 8642 | char **wall = NULL; |
f6144808 | 8643 | int c, r; |
e4b61340 LP |
8644 | |
8645 | assert(argc >= 0); | |
8646 | assert(argv); | |
8647 | ||
b068c6f5 | 8648 | while ((c = getopt_long(argc, argv, "HPrhkKat:fFc", options, NULL)) >= 0) |
e4b61340 LP |
8649 | switch (c) { |
8650 | ||
8651 | case ARG_HELP: | |
37ec0fdd | 8652 | return shutdown_help(); |
e4b61340 LP |
8653 | |
8654 | case 'H': | |
8655 | arg_action = ACTION_HALT; | |
8656 | break; | |
8657 | ||
8658 | case 'P': | |
8659 | arg_action = ACTION_POWEROFF; | |
8660 | break; | |
8661 | ||
8662 | case 'r': | |
5622dde3 KS |
8663 | if (kexec_loaded()) |
8664 | arg_action = ACTION_KEXEC; | |
8665 | else | |
8666 | arg_action = ACTION_REBOOT; | |
e4b61340 LP |
8667 | break; |
8668 | ||
04ebb595 LP |
8669 | case 'K': |
8670 | arg_action = ACTION_KEXEC; | |
8671 | break; | |
8672 | ||
e4b61340 LP |
8673 | case 'h': |
8674 | if (arg_action != ACTION_HALT) | |
8675 | arg_action = ACTION_POWEROFF; | |
8676 | break; | |
8677 | ||
8678 | case 'k': | |
1ae17672 | 8679 | arg_dry_run = true; |
e4b61340 LP |
8680 | break; |
8681 | ||
514f4ef5 LP |
8682 | case ARG_NO_WALL: |
8683 | arg_no_wall = true; | |
8684 | break; | |
8685 | ||
e4b61340 | 8686 | case 'a': |
b068c6f5 | 8687 | case 't': /* Note that we also ignore any passed argument to -t, not just the -t itself */ |
75836b9d JS |
8688 | case 'f': |
8689 | case 'F': | |
e4b61340 LP |
8690 | /* Compatibility nops */ |
8691 | break; | |
8692 | ||
f6144808 LP |
8693 | case 'c': |
8694 | arg_action = ACTION_CANCEL_SHUTDOWN; | |
8695 | break; | |
8696 | ||
e4b61340 LP |
8697 | case '?': |
8698 | return -EINVAL; | |
8699 | ||
8700 | default: | |
eb9da376 | 8701 | assert_not_reached("Unhandled option"); |
e4b61340 | 8702 | } |
e4b61340 | 8703 | |
dfcc5c33 | 8704 | if (argc > optind && arg_action != ACTION_CANCEL_SHUTDOWN) { |
2cc7b0a2 | 8705 | r = parse_shutdown_time_spec(argv[optind], &arg_when); |
7e59bfcb | 8706 | if (r < 0) { |
f6144808 LP |
8707 | log_error("Failed to parse time specification: %s", argv[optind]); |
8708 | return r; | |
8709 | } | |
6b5ad000 | 8710 | } else |
08e4b1c5 | 8711 | arg_when = now(CLOCK_REALTIME) + USEC_PER_MINUTE; |
442b9094 | 8712 | |
dfcc5c33 MS |
8713 | if (argc > optind && arg_action == ACTION_CANCEL_SHUTDOWN) |
8714 | /* No time argument for shutdown cancel */ | |
172d7abf | 8715 | wall = argv + optind; |
dfcc5c33 MS |
8716 | else if (argc > optind + 1) |
8717 | /* We skip the time argument */ | |
172d7abf LP |
8718 | wall = argv + optind + 1; |
8719 | ||
8720 | if (wall) { | |
8721 | arg_wall = strv_copy(wall); | |
8722 | if (!arg_wall) | |
8723 | return log_oom(); | |
8724 | } | |
e4b61340 LP |
8725 | |
8726 | optind = argc; | |
8727 | ||
8728 | return 1; | |
e4b61340 LP |
8729 | } |
8730 | ||
8731 | static int telinit_parse_argv(int argc, char *argv[]) { | |
e4b61340 LP |
8732 | enum { |
8733 | ARG_HELP = 0x100, | |
514f4ef5 | 8734 | ARG_NO_WALL |
e4b61340 LP |
8735 | }; |
8736 | ||
8737 | static const struct option options[] = { | |
8738 | { "help", no_argument, NULL, ARG_HELP }, | |
514f4ef5 | 8739 | { "no-wall", no_argument, NULL, ARG_NO_WALL }, |
eb9da376 | 8740 | {} |
e4b61340 LP |
8741 | }; |
8742 | ||
8743 | static const struct { | |
8744 | char from; | |
8745 | enum action to; | |
8746 | } table[] = { | |
8747 | { '0', ACTION_POWEROFF }, | |
8748 | { '6', ACTION_REBOOT }, | |
ef2f1067 | 8749 | { '1', ACTION_RESCUE }, |
e4b61340 LP |
8750 | { '2', ACTION_RUNLEVEL2 }, |
8751 | { '3', ACTION_RUNLEVEL3 }, | |
8752 | { '4', ACTION_RUNLEVEL4 }, | |
8753 | { '5', ACTION_RUNLEVEL5 }, | |
8754 | { 's', ACTION_RESCUE }, | |
8755 | { 'S', ACTION_RESCUE }, | |
8756 | { 'q', ACTION_RELOAD }, | |
8757 | { 'Q', ACTION_RELOAD }, | |
8758 | { 'u', ACTION_REEXEC }, | |
8759 | { 'U', ACTION_REEXEC } | |
8760 | }; | |
8761 | ||
8762 | unsigned i; | |
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 telinit_help(); |
e4b61340 | 8773 | |
514f4ef5 LP |
8774 | case ARG_NO_WALL: |
8775 | arg_no_wall = true; | |
8776 | break; | |
8777 | ||
e4b61340 LP |
8778 | case '?': |
8779 | return -EINVAL; | |
8780 | ||
8781 | default: | |
eb9da376 | 8782 | assert_not_reached("Unhandled option"); |
e4b61340 | 8783 | } |
e4b61340 | 8784 | |
baaa35ad ZJS |
8785 | if (optind >= argc) |
8786 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8787 | "%s: required argument missing.", | |
8788 | program_invocation_short_name); | |
e4b61340 | 8789 | |
baaa35ad ZJS |
8790 | if (optind + 1 < argc) |
8791 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8792 | "Too many arguments."); | |
e4b61340 | 8793 | |
baaa35ad ZJS |
8794 | if (strlen(argv[optind]) != 1) |
8795 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8796 | "Expected single character argument."); | |
e4b61340 LP |
8797 | |
8798 | for (i = 0; i < ELEMENTSOF(table); i++) | |
8799 | if (table[i].from == argv[optind][0]) | |
8800 | break; | |
8801 | ||
baaa35ad ZJS |
8802 | if (i >= ELEMENTSOF(table)) |
8803 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8804 | "Unknown command '%s'.", argv[optind]); | |
e4b61340 LP |
8805 | |
8806 | arg_action = table[i].to; | |
8807 | ||
313cefa1 | 8808 | optind++; |
e4b61340 LP |
8809 | |
8810 | return 1; | |
8811 | } | |
8812 | ||
8813 | static int runlevel_parse_argv(int argc, char *argv[]) { | |
e4b61340 LP |
8814 | enum { |
8815 | ARG_HELP = 0x100, | |
8816 | }; | |
8817 | ||
8818 | static const struct option options[] = { | |
8819 | { "help", no_argument, NULL, ARG_HELP }, | |
eb9da376 | 8820 | {} |
e4b61340 LP |
8821 | }; |
8822 | ||
8823 | int c; | |
8824 | ||
8825 | assert(argc >= 0); | |
8826 | assert(argv); | |
8827 | ||
601185b4 | 8828 | while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) |
e4b61340 LP |
8829 | switch (c) { |
8830 | ||
8831 | case ARG_HELP: | |
37ec0fdd | 8832 | return runlevel_help(); |
e4b61340 LP |
8833 | |
8834 | case '?': | |
8835 | return -EINVAL; | |
8836 | ||
8837 | default: | |
eb9da376 | 8838 | assert_not_reached("Unhandled option"); |
e4b61340 | 8839 | } |
e4b61340 | 8840 | |
baaa35ad ZJS |
8841 | if (optind < argc) |
8842 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8843 | "Too many arguments."); | |
e4b61340 LP |
8844 | |
8845 | return 1; | |
8846 | } | |
8847 | ||
8848 | static int parse_argv(int argc, char *argv[]) { | |
8849 | assert(argc >= 0); | |
8850 | assert(argv); | |
8851 | ||
8852 | if (program_invocation_short_name) { | |
8853 | ||
8854 | if (strstr(program_invocation_short_name, "halt")) { | |
8855 | arg_action = ACTION_HALT; | |
8856 | return halt_parse_argv(argc, argv); | |
c54819ca | 8857 | |
e4b61340 LP |
8858 | } else if (strstr(program_invocation_short_name, "poweroff")) { |
8859 | arg_action = ACTION_POWEROFF; | |
8860 | return halt_parse_argv(argc, argv); | |
c54819ca | 8861 | |
e4b61340 | 8862 | } else if (strstr(program_invocation_short_name, "reboot")) { |
5622dde3 KS |
8863 | if (kexec_loaded()) |
8864 | arg_action = ACTION_KEXEC; | |
8865 | else | |
8866 | arg_action = ACTION_REBOOT; | |
e4b61340 | 8867 | return halt_parse_argv(argc, argv); |
c54819ca | 8868 | |
e4b61340 LP |
8869 | } else if (strstr(program_invocation_short_name, "shutdown")) { |
8870 | arg_action = ACTION_POWEROFF; | |
8871 | return shutdown_parse_argv(argc, argv); | |
c54819ca | 8872 | |
e4b61340 | 8873 | } else if (strstr(program_invocation_short_name, "init")) { |
d5ca5f11 | 8874 | |
c54819ca LP |
8875 | /* Matches invocations as "init" as well as "telinit", which are synonymous when run as PID != |
8876 | * 1 on SysV. | |
8877 | * | |
8878 | * On SysV "telinit" was the official command to communicate with PID 1, but "init" would | |
8879 | * redirect itself to "telinit" if called with PID != 1. We follow the same logic here still, | |
8880 | * though we add one level of indirection, as we implement "telinit" in "systemctl". Hence, for | |
8881 | * us if you invoke "init" you get "systemd", but it will execve() "systemctl" immediately with | |
8882 | * argv[] unmodified if PID is != 1. If you invoke "telinit" you directly get "systemctl". In | |
8883 | * both cases we shall do the same thing, which is why we do strstr(p_i_s_n, "init") here, as a | |
8884 | * quick way to match both. | |
8885 | * | |
8886 | * Also see redirect_telinit() in src/core/main.c. */ | |
8887 | ||
d5ca5f11 | 8888 | if (sd_booted() > 0) { |
f459b602 | 8889 | arg_action = _ACTION_INVALID; |
d5ca5f11 LP |
8890 | return telinit_parse_argv(argc, argv); |
8891 | } else { | |
c54819ca LP |
8892 | /* Hmm, so some other init system is running, we need to forward this request to |
8893 | * it. For now we simply guess that it is Upstart. */ | |
d5ca5f11 | 8894 | |
595225af | 8895 | (void) rlimit_nofile_safe(); |
4ad61fd1 | 8896 | execv(TELINIT, argv); |
d5ca5f11 | 8897 | |
baaa35ad ZJS |
8898 | return log_error_errno(SYNTHETIC_ERRNO(EIO), |
8899 | "Couldn't find an alternative telinit implementation to spawn."); | |
d5ca5f11 LP |
8900 | } |
8901 | ||
e4b61340 LP |
8902 | } else if (strstr(program_invocation_short_name, "runlevel")) { |
8903 | arg_action = ACTION_RUNLEVEL; | |
8904 | return runlevel_parse_argv(argc, argv); | |
8905 | } | |
8906 | } | |
8907 | ||
8908 | arg_action = ACTION_SYSTEMCTL; | |
8909 | return systemctl_parse_argv(argc, argv); | |
8910 | } | |
8911 | ||
349cc4a5 | 8912 | #if HAVE_SYSV_COMPAT |
44a6b1b6 | 8913 | _pure_ static int action_to_runlevel(void) { |
eb22ac37 LP |
8914 | static const char table[_ACTION_MAX] = { |
8915 | [ACTION_HALT] = '0', | |
8916 | [ACTION_POWEROFF] = '0', | |
8917 | [ACTION_REBOOT] = '6', | |
8918 | [ACTION_RUNLEVEL2] = '2', | |
8919 | [ACTION_RUNLEVEL3] = '3', | |
8920 | [ACTION_RUNLEVEL4] = '4', | |
8921 | [ACTION_RUNLEVEL5] = '5', | |
8922 | [ACTION_RESCUE] = '1' | |
8923 | }; | |
8924 | ||
78ca9099 | 8925 | assert(arg_action >= 0 && arg_action < _ACTION_MAX); |
d55ae9e6 LP |
8926 | return table[arg_action]; |
8927 | } | |
d2e79673 | 8928 | #endif |
d55ae9e6 | 8929 | |
e449de87 | 8930 | static int systemctl_main(int argc, char *argv[]) { |
e449de87 | 8931 | static const Verb verbs[] = { |
c56d1e2c | 8932 | { "list-units", VERB_ANY, VERB_ANY, VERB_DEFAULT|VERB_ONLINE_ONLY, list_units }, |
38fcb7f7 ZJS |
8933 | { "list-unit-files", VERB_ANY, VERB_ANY, 0, list_unit_files }, |
8934 | { "list-sockets", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, list_sockets }, | |
8935 | { "list-timers", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, list_timers }, | |
8936 | { "list-jobs", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, list_jobs }, | |
8937 | { "list-machines", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, list_machines }, | |
8938 | { "clear-jobs", VERB_ANY, 1, VERB_ONLINE_ONLY, trivial_method }, | |
8939 | { "cancel", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, cancel_job }, | |
8940 | { "start", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, | |
8941 | { "stop", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, | |
8942 | { "condstop", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, /* For compatibility with ALTLinux */ | |
8943 | { "reload", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, | |
8944 | { "restart", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, | |
8945 | { "try-restart", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, | |
8946 | { "reload-or-restart", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, | |
8947 | { "reload-or-try-restart", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, /* For compatibility with old systemctl <= 228 */ | |
8948 | { "try-reload-or-restart", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, | |
8949 | { "force-reload", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, /* For compatibility with SysV */ | |
8950 | { "condreload", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, /* For compatibility with ALTLinux */ | |
8951 | { "condrestart", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, /* For compatibility with RH */ | |
8952 | { "isolate", 2, 2, VERB_ONLINE_ONLY, start_unit }, | |
8953 | { "kill", 2, VERB_ANY, VERB_ONLINE_ONLY, kill_unit }, | |
d9e45bc3 MS |
8954 | { "clean", 2, VERB_ANY, VERB_ONLINE_ONLY, clean_or_freeze_unit }, |
8955 | { "freeze", 2, VERB_ANY, VERB_ONLINE_ONLY, clean_or_freeze_unit }, | |
8956 | { "thaw", 2, VERB_ANY, VERB_ONLINE_ONLY, clean_or_freeze_unit }, | |
38fcb7f7 ZJS |
8957 | { "is-active", 2, VERB_ANY, VERB_ONLINE_ONLY, check_unit_active }, |
8958 | { "check", 2, VERB_ANY, VERB_ONLINE_ONLY, check_unit_active }, /* deprecated alias of is-active */ | |
8959 | { "is-failed", 2, VERB_ANY, VERB_ONLINE_ONLY, check_unit_failed }, | |
8960 | { "show", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, show }, | |
8961 | { "cat", 2, VERB_ANY, VERB_ONLINE_ONLY, cat }, | |
8962 | { "status", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, show }, | |
8963 | { "help", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, show }, | |
8964 | { "daemon-reload", VERB_ANY, 1, VERB_ONLINE_ONLY, daemon_reload }, | |
8965 | { "daemon-reexec", VERB_ANY, 1, VERB_ONLINE_ONLY, daemon_reload }, | |
ae98f726 ZJS |
8966 | { "log-level", VERB_ANY, 2, VERB_ONLINE_ONLY, log_level }, |
8967 | { "log-target", VERB_ANY, 2, VERB_ONLINE_ONLY, log_target }, | |
8968 | { "service-watchdogs", VERB_ANY, 2, VERB_ONLINE_ONLY, service_watchdogs }, | |
38fcb7f7 ZJS |
8969 | { "show-environment", VERB_ANY, 1, VERB_ONLINE_ONLY, show_environment }, |
8970 | { "set-environment", 2, VERB_ANY, VERB_ONLINE_ONLY, set_environment }, | |
8971 | { "unset-environment", 2, VERB_ANY, VERB_ONLINE_ONLY, set_environment }, | |
8972 | { "import-environment", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, import_environment }, | |
8973 | { "halt", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, | |
8974 | { "poweroff", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, | |
8975 | { "reboot", VERB_ANY, 2, VERB_ONLINE_ONLY, start_system_special }, | |
8976 | { "kexec", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, | |
8977 | { "suspend", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, | |
8978 | { "hibernate", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, | |
8979 | { "hybrid-sleep", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, | |
8980 | { "suspend-then-hibernate",VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, | |
8981 | { "default", VERB_ANY, 1, VERB_ONLINE_ONLY, start_special }, | |
8982 | { "rescue", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, | |
8983 | { "emergency", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, | |
8984 | { "exit", VERB_ANY, 2, VERB_ONLINE_ONLY, start_special }, | |
8985 | { "reset-failed", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, reset_failed }, | |
8986 | { "enable", 2, VERB_ANY, 0, enable_unit }, | |
8987 | { "disable", 2, VERB_ANY, 0, enable_unit }, | |
8988 | { "is-enabled", 2, VERB_ANY, 0, unit_is_enabled }, | |
8989 | { "reenable", 2, VERB_ANY, 0, enable_unit }, | |
8990 | { "preset", 2, VERB_ANY, 0, enable_unit }, | |
8991 | { "preset-all", VERB_ANY, 1, 0, preset_all }, | |
8992 | { "mask", 2, VERB_ANY, 0, enable_unit }, | |
8993 | { "unmask", 2, VERB_ANY, 0, enable_unit }, | |
8994 | { "link", 2, VERB_ANY, 0, enable_unit }, | |
8995 | { "revert", 2, VERB_ANY, 0, enable_unit }, | |
8996 | { "switch-root", 2, VERB_ANY, VERB_ONLINE_ONLY, switch_root }, | |
94c3a838 | 8997 | { "list-dependencies", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, list_dependencies }, |
38fcb7f7 ZJS |
8998 | { "set-default", 2, 2, 0, set_default }, |
8999 | { "get-default", VERB_ANY, 1, 0, get_default }, | |
9000 | { "set-property", 3, VERB_ANY, VERB_ONLINE_ONLY, set_property }, | |
9001 | { "is-system-running", VERB_ANY, 1, 0, is_system_running }, | |
9002 | { "add-wants", 3, VERB_ANY, 0, add_dependency }, | |
9003 | { "add-requires", 3, VERB_ANY, 0, add_dependency }, | |
9004 | { "edit", 2, VERB_ANY, VERB_ONLINE_ONLY, edit }, | |
d08e75ed | 9005 | {} |
e449de87 | 9006 | }; |
7e4249b9 | 9007 | |
4cb1015e ZJS |
9008 | const Verb *verb = verbs_find_verb(argv[optind], verbs); |
9009 | if (verb && (verb->flags & VERB_ONLINE_ONLY) && arg_root) | |
9010 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
9011 | "Verb '%s' cannot be used with --root=.", | |
9012 | argv[optind] ?: verb->verb); | |
9013 | ||
e449de87 | 9014 | return dispatch_verb(argc, argv, verbs, NULL); |
e4b61340 LP |
9015 | } |
9016 | ||
4fbd7192 | 9017 | static int reload_with_fallback(void) { |
4fbd7192 | 9018 | /* First, try systemd via D-Bus. */ |
e449de87 | 9019 | if (daemon_reload(0, NULL, NULL) >= 0) |
4fbd7192 | 9020 | return 0; |
e4b61340 LP |
9021 | |
9022 | /* Nothing else worked, so let's try signals */ | |
2853b60a | 9023 | assert(IN_SET(arg_action, ACTION_RELOAD, ACTION_REEXEC)); |
e4b61340 | 9024 | |
4a62c710 MS |
9025 | if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) |
9026 | return log_error_errno(errno, "kill() failed: %m"); | |
e4b61340 LP |
9027 | |
9028 | return 0; | |
9029 | } | |
9030 | ||
4fbd7192 | 9031 | static int start_with_fallback(void) { |
4fbd7192 | 9032 | /* First, try systemd via D-Bus. */ |
2e13c0c3 | 9033 | if (start_unit(0, NULL, NULL) == 0) |
4fbd7192 | 9034 | return 0; |
e4b61340 | 9035 | |
2771aaf5 | 9036 | #if HAVE_SYSV_COMPAT |
2853b60a | 9037 | /* Nothing else worked, so let's try /dev/initctl */ |
63a3b3cb | 9038 | if (talk_initctl(action_to_runlevel()) > 0) |
48ec22bc | 9039 | return 0; |
2771aaf5 | 9040 | #endif |
d55ae9e6 | 9041 | |
baaa35ad ZJS |
9042 | return log_error_errno(SYNTHETIC_ERRNO(EIO), |
9043 | "Failed to talk to init daemon."); | |
e4b61340 LP |
9044 | } |
9045 | ||
477def80 | 9046 | static int halt_now(enum action a) { |
c01dcddf | 9047 | /* The kernel will automatically flush ATA disks and suchlike on reboot(), but the file systems need to be |
f03ec2b1 | 9048 | * synced explicitly in advance. */ |
1ae17672 | 9049 | if (!arg_no_sync && !arg_dry_run) |
f3f054f0 | 9050 | (void) sync(); |
4a3ad399 | 9051 | |
1ae17672 ZJS |
9052 | /* Make sure C-A-D is handled by the kernel from this point on... */ |
9053 | if (!arg_dry_run) | |
9054 | (void) reboot(RB_ENABLE_CAD); | |
e606bb61 | 9055 | |
4c80c73c | 9056 | switch (a) { |
e606bb61 LP |
9057 | |
9058 | case ACTION_HALT: | |
60675884 LP |
9059 | if (!arg_quiet) |
9060 | log_info("Halting."); | |
1ae17672 ZJS |
9061 | if (arg_dry_run) |
9062 | return 0; | |
19578bb2 | 9063 | (void) reboot(RB_HALT_SYSTEM); |
477def80 | 9064 | return -errno; |
e606bb61 LP |
9065 | |
9066 | case ACTION_POWEROFF: | |
60675884 LP |
9067 | if (!arg_quiet) |
9068 | log_info("Powering off."); | |
1ae17672 ZJS |
9069 | if (arg_dry_run) |
9070 | return 0; | |
19578bb2 | 9071 | (void) reboot(RB_POWER_OFF); |
477def80 | 9072 | return -errno; |
e606bb61 | 9073 | |
98d52feb | 9074 | case ACTION_KEXEC: |
c01dcddf LP |
9075 | case ACTION_REBOOT: |
9076 | return reboot_with_parameter(REBOOT_FALLBACK | | |
9077 | (arg_quiet ? 0 : REBOOT_LOG) | | |
9078 | (arg_dry_run ? REBOOT_DRY_RUN : 0)); | |
e606bb61 | 9079 | |
477def80 LP |
9080 | default: |
9081 | assert_not_reached("Unknown action."); | |
9082 | } | |
e606bb61 LP |
9083 | } |
9084 | ||
56a730fa | 9085 | static int logind_schedule_shutdown(void) { |
7f56c3d4 | 9086 | |
cf9cda44 | 9087 | #if ENABLE_LOGIND |
4afd3348 | 9088 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
56a730fa LP |
9089 | char date[FORMAT_TIMESTAMP_MAX]; |
9090 | const char *action; | |
6df8a6c7 | 9091 | const char *log_action; |
4fbd7192 | 9092 | sd_bus *bus; |
56a730fa LP |
9093 | int r; |
9094 | ||
4fbd7192 | 9095 | r = acquire_bus(BUS_FULL, &bus); |
56a730fa | 9096 | if (r < 0) |
4fbd7192 | 9097 | return r; |
56a730fa LP |
9098 | |
9099 | switch (arg_action) { | |
9100 | case ACTION_HALT: | |
9101 | action = "halt"; | |
6df8a6c7 | 9102 | log_action = "Shutdown"; |
56a730fa LP |
9103 | break; |
9104 | case ACTION_POWEROFF: | |
9105 | action = "poweroff"; | |
6df8a6c7 | 9106 | log_action = "Shutdown"; |
56a730fa LP |
9107 | break; |
9108 | case ACTION_KEXEC: | |
9109 | action = "kexec"; | |
6df8a6c7 | 9110 | log_action = "Reboot via kexec"; |
56a730fa | 9111 | break; |
a4420f7b LP |
9112 | case ACTION_EXIT: |
9113 | action = "exit"; | |
6df8a6c7 | 9114 | log_action = "Shutdown"; |
a4420f7b LP |
9115 | break; |
9116 | case ACTION_REBOOT: | |
56a730fa LP |
9117 | default: |
9118 | action = "reboot"; | |
6df8a6c7 | 9119 | log_action = "Reboot"; |
56a730fa LP |
9120 | break; |
9121 | } | |
9122 | ||
1ae17672 | 9123 | if (arg_dry_run) |
56a730fa LP |
9124 | action = strjoina("dry-", action); |
9125 | ||
d2ad7e1b ZJS |
9126 | (void) logind_set_wall_message(); |
9127 | ||
8010c205 | 9128 | r = bus_call_method(bus, bus_login_mgr, "ScheduleShutdown", &error, NULL, "st", action, arg_when); |
56a730fa LP |
9129 | if (r < 0) |
9130 | return log_warning_errno(r, "Failed to call ScheduleShutdown in logind, proceeding with immediate shutdown: %s", bus_error_message(&error, r)); | |
9131 | ||
60675884 | 9132 | if (!arg_quiet) |
6df8a6c7 | 9133 | log_info("%s scheduled for %s, use 'shutdown -c' to cancel.", log_action, format_timestamp(date, sizeof(date), arg_when)); |
56a730fa | 9134 | return 0; |
cf9cda44 | 9135 | #else |
2f50e59f LP |
9136 | return log_error_errno(SYNTHETIC_ERRNO(ENOSYS), |
9137 | "Cannot schedule shutdown without logind support, proceeding with immediate shutdown."); | |
807690fb | 9138 | #endif |
56a730fa LP |
9139 | } |
9140 | ||
4fbd7192 | 9141 | static int halt_main(void) { |
e4b61340 LP |
9142 | int r; |
9143 | ||
4fbd7192 | 9144 | r = logind_check_inhibitors(arg_action); |
748ebafa LP |
9145 | if (r < 0) |
9146 | return r; | |
b37844d3 | 9147 | |
940bec70 DJL |
9148 | /* Delayed shutdown requested, and was successful */ |
9149 | if (arg_when > 0 && logind_schedule_shutdown() == 0) | |
9150 | return 0; | |
9151 | /* no delay, or logind failed or is not at all available */ | |
7f96539d | 9152 | |
bc8c2f5c | 9153 | if (geteuid() != 0) { |
1ae17672 | 9154 | if (arg_dry_run || arg_force > 0) { |
fba868fa | 9155 | (void) must_be_root(); |
2ac3930f IS |
9156 | return -EPERM; |
9157 | } | |
9158 | ||
7e59bfcb | 9159 | /* Try logind if we are a normal user and no special |
d35f51ea | 9160 | * mode applies. Maybe polkit allows us to shutdown |
7e59bfcb | 9161 | * the machine. */ |
36b69c31 | 9162 | if (IN_SET(arg_action, ACTION_POWEROFF, ACTION_REBOOT, ACTION_HALT)) { |
4fbd7192 | 9163 | r = logind_reboot(arg_action); |
4c80c73c KS |
9164 | if (r >= 0) |
9165 | return r; | |
a9085ea3 | 9166 | if (IN_SET(r, -EOPNOTSUPP, -EINPROGRESS)) |
7f96539d LP |
9167 | /* requested operation is not |
9168 | * supported on the local system or | |
9169 | * already in progress */ | |
a9085ea3 IS |
9170 | return r; |
9171 | /* on all other errors, try low-level operation */ | |
4c80c73c | 9172 | } |
bc8c2f5c LP |
9173 | } |
9174 | ||
2955e0d4 GJ |
9175 | /* In order to minimize the difference between operation with and |
9176 | * without logind, we explicitly enable non-blocking mode for this, | |
9177 | * as logind's shutdown operations are always non-blocking. */ | |
9178 | arg_no_block = true; | |
9179 | ||
1ae17672 | 9180 | if (!arg_dry_run && !arg_force) |
4fbd7192 | 9181 | return start_with_fallback(); |
e4b61340 | 9182 | |
2ac3930f IS |
9183 | assert(geteuid() == 0); |
9184 | ||
d90e1a30 LP |
9185 | if (!arg_no_wtmp) { |
9186 | if (sd_booted() > 0) | |
9187 | log_debug("Not writing utmp record, assuming that systemd-update-utmp is used."); | |
7e59bfcb LP |
9188 | else { |
9189 | r = utmp_put_shutdown(); | |
9190 | if (r < 0) | |
da927ba9 | 9191 | log_warning_errno(r, "Failed to write utmp record: %m"); |
7e59bfcb | 9192 | } |
d90e1a30 | 9193 | } |
e4b61340 | 9194 | |
1ae17672 | 9195 | if (arg_dry_run) |
e4b61340 LP |
9196 | return 0; |
9197 | ||
477def80 | 9198 | r = halt_now(arg_action); |
691395d8 | 9199 | return log_error_errno(r, "Failed to reboot: %m"); |
e4b61340 LP |
9200 | } |
9201 | ||
9202 | static int runlevel_main(void) { | |
9203 | int r, runlevel, previous; | |
9204 | ||
729e3769 LP |
9205 | r = utmp_get_runlevel(&runlevel, &previous); |
9206 | if (r < 0) { | |
9207 | puts("unknown"); | |
e4b61340 LP |
9208 | return r; |
9209 | } | |
9210 | ||
9211 | printf("%c %c\n", | |
9212 | previous <= 0 ? 'N' : previous, | |
9213 | runlevel <= 0 ? 'N' : runlevel); | |
9214 | ||
9215 | return 0; | |
9216 | } | |
9217 | ||
2cf05793 | 9218 | static int logind_cancel_shutdown(void) { |
349cc4a5 | 9219 | #if ENABLE_LOGIND |
4afd3348 | 9220 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
4fbd7192 | 9221 | sd_bus *bus; |
949d9ce9 LP |
9222 | int r; |
9223 | ||
4fbd7192 | 9224 | r = acquire_bus(BUS_FULL, &bus); |
949d9ce9 | 9225 | if (r < 0) |
4fbd7192 | 9226 | return r; |
949d9ce9 | 9227 | |
4fbd7192 | 9228 | (void) logind_set_wall_message(); |
949d9ce9 | 9229 | |
8010c205 | 9230 | r = bus_call_method(bus, bus_login_mgr, "CancelScheduledShutdown", &error, NULL, NULL); |
949d9ce9 LP |
9231 | if (r < 0) |
9232 | return log_warning_errno(r, "Failed to talk to logind, shutdown hasn't been cancelled: %s", bus_error_message(&error, r)); | |
9233 | ||
9234 | return 0; | |
2cf05793 | 9235 | #else |
2f50e59f LP |
9236 | return log_error_errno(SYNTHETIC_ERRNO(ENOSYS), |
9237 | "Not compiled with logind support, cannot cancel scheduled shutdowns."); | |
2cf05793 | 9238 | #endif |
949d9ce9 LP |
9239 | } |
9240 | ||
f2a3de01 | 9241 | static int run(int argc, char *argv[]) { |
f459b602 | 9242 | int r; |
e4b61340 | 9243 | |
a9cdc94f | 9244 | setlocale(LC_ALL, ""); |
41d1f469 | 9245 | log_parse_environment_cli(); |
2396fb04 | 9246 | log_open(); |
1abaf488 LP |
9247 | |
9248 | /* The journal merging logic potentially needs a lot of fds. */ | |
9249 | (void) rlimit_nofile_bump(HIGH_RLIMIT_NOFILE); | |
9250 | ||
592705f2 | 9251 | sigbus_install(); |
e4b61340 | 9252 | |
184ecaf7 DR |
9253 | /* Explicitly not on_tty() to avoid setting cached value. |
9254 | * This becomes relevant for piping output which might be | |
9255 | * ellipsized. */ | |
9256 | original_stdout_is_tty = isatty(STDOUT_FILENO); | |
9257 | ||
04ebb595 | 9258 | r = parse_argv(argc, argv); |
f459b602 | 9259 | if (r <= 0) |
e4b61340 | 9260 | goto finish; |
7e4249b9 | 9261 | |
040524b4 | 9262 | if (arg_action != ACTION_SYSTEMCTL && running_in_chroot() > 0) { |
60675884 LP |
9263 | if (!arg_quiet) |
9264 | log_info("Running in chroot, ignoring request."); | |
f459b602 | 9265 | r = 0; |
82e23ddd LP |
9266 | goto finish; |
9267 | } | |
9268 | ||
41dd15e4 LP |
9269 | /* systemctl_main() will print an error message for the bus |
9270 | * connection, but only if it needs to */ | |
e4b61340 LP |
9271 | |
9272 | switch (arg_action) { | |
9273 | ||
22f4096c | 9274 | case ACTION_SYSTEMCTL: |
e449de87 | 9275 | r = systemctl_main(argc, argv); |
e4b61340 | 9276 | break; |
e4b61340 | 9277 | |
081dc638 AJ |
9278 | /* Legacy command aliases set arg_action. They provide some fallbacks, |
9279 | * e.g. to tell sysvinit to reboot after you have installed systemd | |
9280 | * binaries. */ | |
9281 | ||
e4b61340 LP |
9282 | case ACTION_HALT: |
9283 | case ACTION_POWEROFF: | |
9284 | case ACTION_REBOOT: | |
5622dde3 | 9285 | case ACTION_KEXEC: |
4fbd7192 | 9286 | r = halt_main(); |
e4b61340 LP |
9287 | break; |
9288 | ||
e4b61340 LP |
9289 | case ACTION_RUNLEVEL2: |
9290 | case ACTION_RUNLEVEL3: | |
9291 | case ACTION_RUNLEVEL4: | |
9292 | case ACTION_RUNLEVEL5: | |
9293 | case ACTION_RESCUE: | |
4fbd7192 | 9294 | r = start_with_fallback(); |
e4b61340 | 9295 | break; |
7e4249b9 | 9296 | |
e4b61340 LP |
9297 | case ACTION_RELOAD: |
9298 | case ACTION_REEXEC: | |
4fbd7192 | 9299 | r = reload_with_fallback(); |
e4b61340 LP |
9300 | break; |
9301 | ||
949d9ce9 | 9302 | case ACTION_CANCEL_SHUTDOWN: |
2cf05793 | 9303 | r = logind_cancel_shutdown(); |
f6144808 LP |
9304 | break; |
9305 | ||
eb22ac37 | 9306 | case ACTION_RUNLEVEL: |
4f16c1f4 LP |
9307 | r = runlevel_main(); |
9308 | break; | |
9309 | ||
081dc638 AJ |
9310 | case ACTION_EXIT: |
9311 | case ACTION_SUSPEND: | |
9312 | case ACTION_HIBERNATE: | |
9313 | case ACTION_HYBRID_SLEEP: | |
e68c79db | 9314 | case ACTION_SUSPEND_THEN_HIBERNATE: |
081dc638 AJ |
9315 | case ACTION_EMERGENCY: |
9316 | case ACTION_DEFAULT: | |
9317 | /* systemctl verbs with no equivalent in the legacy commands. | |
9318 | * These cannot appear in arg_action. Fall through. */ | |
9319 | ||
f459b602 | 9320 | case _ACTION_INVALID: |
e4b61340 LP |
9321 | default: |
9322 | assert_not_reached("Unknown action"); | |
9323 | } | |
7e4249b9 LP |
9324 | |
9325 | finish: | |
cf647b69 LP |
9326 | release_busses(); |
9327 | ||
d1405af3 ZJS |
9328 | /* Note that we return r here, not 0, so that we can implement the LSB-like return codes */ |
9329 | return r; | |
7e4249b9 | 9330 | } |
d1405af3 ZJS |
9331 | |
9332 | DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run); |