]>
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" | |
23 | #include "bus-message.h" | |
25b1d72d | 24 | #include "bus-unit-procs.h" |
291d565a | 25 | #include "bus-unit-util.h" |
03a7b521 | 26 | #include "bus-util.h" |
e45c81b8 | 27 | #include "bus-wait-for-jobs.h" |
d81bc51f | 28 | #include "bus-wait-for-units.h" |
ab35fb1b | 29 | #include "cgroup-show.h" |
c6c18be3 | 30 | #include "cgroup-util.h" |
cda134ab | 31 | #include "copy.h" |
a047f4f1 | 32 | #include "cpu-set-util.h" |
e8630e69 | 33 | #include "dirent-util.h" |
ad2a0358 | 34 | #include "dropin.h" |
2fec5854 | 35 | #include "efi-loader.h" |
5bdf2243 | 36 | #include "efivars.h" |
03a7b521 | 37 | #include "env-util.h" |
804ee07c | 38 | #include "escape.h" |
b3d59367 | 39 | #include "exec-util.h" |
03a7b521 | 40 | #include "exit-status.h" |
3ffd4af2 | 41 | #include "fd-util.h" |
de9a8fe1 | 42 | #include "format-table.h" |
f97b34a6 | 43 | #include "format-util.h" |
f4f15635 | 44 | #include "fs-util.h" |
7d50b32a | 45 | #include "glob-util.h" |
08f3be7a | 46 | #include "hexdecoct.h" |
c52a937b | 47 | #include "hostname-util.h" |
0a970718 | 48 | #include "in-addr-util.h" |
03a7b521 LP |
49 | #include "initreq.h" |
50 | #include "install.h" | |
c004493c | 51 | #include "io-util.h" |
305a2850 | 52 | #include "journal-util.h" |
03a7b521 | 53 | #include "list.h" |
8752c575 | 54 | #include "locale-util.h" |
03a7b521 LP |
55 | #include "log.h" |
56 | #include "logs-show.h" | |
57 | #include "macro.h" | |
d1405af3 | 58 | #include "main-func.h" |
0a970718 | 59 | #include "memory-util.h" |
03a7b521 | 60 | #include "mkdir.h" |
1808f768 | 61 | #include "numa-util.h" |
03a7b521 | 62 | #include "pager.h" |
6bedfcbb | 63 | #include "parse-util.h" |
03a7b521 LP |
64 | #include "path-lookup.h" |
65 | #include "path-util.h" | |
294bf0c3 | 66 | #include "pretty-print.h" |
01771226 | 67 | #include "proc-cmdline.h" |
03a7b521 | 68 | #include "process-util.h" |
e3631d1c | 69 | #include "reboot-util.h" |
78f22b97 | 70 | #include "rlimit-util.h" |
03a7b521 | 71 | #include "set.h" |
592705f2 | 72 | #include "sigbus.h" |
24882e06 | 73 | #include "signal-util.h" |
03a7b521 | 74 | #include "socket-util.h" |
760877e9 | 75 | #include "sort-util.h" |
03a7b521 LP |
76 | #include "spawn-ask-password-agent.h" |
77 | #include "spawn-polkit-agent.h" | |
78 | #include "special.h" | |
8fcde012 | 79 | #include "stat-util.h" |
8559e61d | 80 | #include "string-table.h" |
03a7b521 | 81 | #include "strv.h" |
63a3b3cb | 82 | #include "sysv-compat.h" |
03a7b521 | 83 | #include "terminal-util.h" |
e4de7287 | 84 | #include "tmpfile-util.h" |
89ada3ba | 85 | #include "unit-def.h" |
46ff70ca | 86 | #include "unit-file.h" |
03a7b521 | 87 | #include "unit-name.h" |
b1d4f8e1 | 88 | #include "user-util.h" |
305a2850 | 89 | #include "utf8.h" |
03a7b521 | 90 | #include "utmp-wtmp.h" |
e449de87 | 91 | #include "verbs.h" |
7f4b3c5e | 92 | #include "virt.h" |
7e4249b9 | 93 | |
20b3f379 | 94 | static char **arg_types = NULL; |
9b9b3d36 | 95 | static char **arg_states = NULL; |
20b3f379 | 96 | static char **arg_properties = NULL; |
7e4249b9 | 97 | static bool arg_all = false; |
afba4199 ZJS |
98 | static enum dependency { |
99 | DEPENDENCY_FORWARD, | |
100 | DEPENDENCY_REVERSE, | |
101 | DEPENDENCY_AFTER, | |
102 | DEPENDENCY_BEFORE, | |
071066a5 | 103 | _DEPENDENCY_MAX |
afba4199 | 104 | } arg_dependency = DEPENDENCY_FORWARD; |
e67c3609 | 105 | static const char *arg_job_mode = "replace"; |
729e3769 | 106 | static UnitFileScope arg_scope = UNIT_FILE_SYSTEM; |
93a08841 | 107 | static bool arg_wait = false; |
ee5762e3 | 108 | static bool arg_no_block = false; |
ebed32bf | 109 | static bool arg_no_legend = false; |
0221d68a | 110 | static PagerFlags arg_pager_flags = 0; |
e4b61340 | 111 | static bool arg_no_wtmp = false; |
f3f054f0 | 112 | static bool arg_no_sync = false; |
514f4ef5 | 113 | static bool arg_no_wall = false; |
ee5762e3 | 114 | static bool arg_no_reload = false; |
4f9a9105 | 115 | static bool arg_value = false; |
991f2a39 | 116 | static bool arg_show_types = false; |
b37844d3 | 117 | static bool arg_ignore_inhibitors = false; |
1ae17672 | 118 | static bool arg_dry_run = false; |
0183528f | 119 | static bool arg_quiet = false; |
ee5762e3 | 120 | static bool arg_full = false; |
1238ee09 | 121 | static bool arg_recursive = false; |
e9c387c8 | 122 | static bool arg_with_dependencies = false; |
85d9b598 | 123 | static bool arg_show_transaction = false; |
e606bb61 | 124 | static int arg_force = 0; |
16f017fa | 125 | static bool arg_ask_password = false; |
729e3769 | 126 | static bool arg_runtime = false; |
d309c1c3 | 127 | static UnitFilePresetMode arg_preset_mode = UNIT_FILE_PRESET_FULL; |
e4b61340 | 128 | static char **arg_wall = NULL; |
8a0867d6 | 129 | static const char *arg_kill_who = NULL; |
8a0867d6 | 130 | static int arg_signal = SIGTERM; |
0f03c2a4 | 131 | static char *arg_root = NULL; |
f6144808 | 132 | static usec_t arg_when = 0; |
dae710be | 133 | static const char *arg_reboot_argument = NULL; |
4445a875 | 134 | static enum action { |
e4b61340 LP |
135 | ACTION_SYSTEMCTL, |
136 | ACTION_HALT, | |
137 | ACTION_POWEROFF, | |
138 | ACTION_REBOOT, | |
20b09ca7 LP |
139 | ACTION_KEXEC, |
140 | ACTION_EXIT, | |
6edd7d0a LP |
141 | ACTION_SUSPEND, |
142 | ACTION_HIBERNATE, | |
6524990f | 143 | ACTION_HYBRID_SLEEP, |
e68c79db | 144 | ACTION_SUSPEND_THEN_HIBERNATE, |
e4b61340 LP |
145 | ACTION_RUNLEVEL2, |
146 | ACTION_RUNLEVEL3, | |
147 | ACTION_RUNLEVEL4, | |
148 | ACTION_RUNLEVEL5, | |
149 | ACTION_RESCUE, | |
514f4ef5 LP |
150 | ACTION_EMERGENCY, |
151 | ACTION_DEFAULT, | |
e4b61340 LP |
152 | ACTION_RELOAD, |
153 | ACTION_REEXEC, | |
154 | ACTION_RUNLEVEL, | |
f6144808 | 155 | ACTION_CANCEL_SHUTDOWN, |
913c1916 AJ |
156 | _ACTION_MAX, |
157 | _ACTION_INVALID = -1 | |
e4b61340 | 158 | } arg_action = ACTION_SYSTEMCTL; |
f459b602 | 159 | static BusTransport arg_transport = BUS_TRANSPORT_LOCAL; |
fc2ffaf1 | 160 | static const char *arg_host = NULL; |
df50185b LP |
161 | static unsigned arg_lines = 10; |
162 | static OutputMode arg_output = OUTPUT_SHORT; | |
5d0c05e5 | 163 | static bool arg_plain = false; |
5bdf2243 | 164 | static bool arg_firmware_setup = false; |
97af80c5 LP |
165 | static usec_t arg_boot_loader_menu = USEC_INFINITY; |
166 | static const char *arg_boot_loader_entry = NULL; | |
57ab2eab | 167 | static bool arg_now = false; |
82948f6c LP |
168 | static bool arg_jobs_before = false; |
169 | static bool arg_jobs_after = false; | |
d81bc51f | 170 | static char **arg_clean_what = NULL; |
1238ee09 | 171 | |
e8630e69 ZJS |
172 | /* This is a global cache that will be constructed on first use. */ |
173 | static Hashmap *cached_id_map = NULL; | |
174 | static Hashmap *cached_name_map = NULL; | |
175 | ||
61ff45db LP |
176 | STATIC_DESTRUCTOR_REGISTER(arg_wall, strv_freep); |
177 | STATIC_DESTRUCTOR_REGISTER(arg_root, freep); | |
178 | STATIC_DESTRUCTOR_REGISTER(arg_types, strv_freep); | |
179 | STATIC_DESTRUCTOR_REGISTER(arg_states, strv_freep); | |
180 | STATIC_DESTRUCTOR_REGISTER(arg_properties, strv_freep); | |
d81bc51f | 181 | STATIC_DESTRUCTOR_REGISTER(arg_clean_what, strv_freep); |
e8630e69 ZJS |
182 | STATIC_DESTRUCTOR_REGISTER(cached_id_map, hashmap_freep); |
183 | STATIC_DESTRUCTOR_REGISTER(cached_name_map, hashmap_freep); | |
61ff45db | 184 | |
e449de87 | 185 | static int daemon_reload(int argc, char *argv[], void* userdata); |
2853b60a | 186 | static int trivial_method(int argc, char *argv[], void *userdata); |
477def80 | 187 | static int halt_now(enum action a); |
fa0d5878 | 188 | static int get_state_one_unit(sd_bus *bus, const char *name, UnitActiveState *active_state); |
dbc2c080 | 189 | |
4fbd7192 LP |
190 | static bool original_stdout_is_tty; |
191 | ||
192 | typedef enum BusFocus { | |
193 | BUS_FULL, /* The full bus indicated via --system or --user */ | |
194 | BUS_MANAGER, /* The manager itself, possibly directly, possibly via the bus */ | |
195 | _BUS_FOCUS_MAX | |
196 | } BusFocus; | |
197 | ||
5238e957 | 198 | static sd_bus *buses[_BUS_FOCUS_MAX] = {}; |
4fbd7192 | 199 | |
b3796dd8 JS |
200 | static UnitFileFlags args_to_flags(void) { |
201 | return (arg_runtime ? UNIT_FILE_RUNTIME : 0) | | |
202 | (arg_force ? UNIT_FILE_FORCE : 0); | |
203 | } | |
204 | ||
4fbd7192 LP |
205 | static int acquire_bus(BusFocus focus, sd_bus **ret) { |
206 | int r; | |
207 | ||
208 | assert(focus < _BUS_FOCUS_MAX); | |
209 | assert(ret); | |
210 | ||
211 | /* We only go directly to the manager, if we are using a local transport */ | |
212 | if (arg_transport != BUS_TRANSPORT_LOCAL) | |
213 | focus = BUS_FULL; | |
214 | ||
d4015567 LP |
215 | if (getenv_bool("SYSTEMCTL_FORCE_BUS") > 0) |
216 | focus = BUS_FULL; | |
217 | ||
5238e957 | 218 | if (!buses[focus]) { |
4fbd7192 LP |
219 | bool user; |
220 | ||
221 | user = arg_scope != UNIT_FILE_SYSTEM; | |
222 | ||
223 | if (focus == BUS_MANAGER) | |
5238e957 | 224 | r = bus_connect_transport_systemd(arg_transport, arg_host, user, &buses[focus]); |
4fbd7192 | 225 | else |
5238e957 | 226 | r = bus_connect_transport(arg_transport, arg_host, user, &buses[focus]); |
4fbd7192 LP |
227 | if (r < 0) |
228 | return log_error_errno(r, "Failed to connect to bus: %m"); | |
229 | ||
5238e957 | 230 | (void) sd_bus_set_allow_interactive_authorization(buses[focus], arg_ask_password); |
4fbd7192 LP |
231 | } |
232 | ||
5238e957 | 233 | *ret = buses[focus]; |
4fbd7192 LP |
234 | return 0; |
235 | } | |
236 | ||
237 | static void release_busses(void) { | |
238 | BusFocus w; | |
239 | ||
240 | for (w = 0; w < _BUS_FOCUS_MAX; w++) | |
5238e957 | 241 | buses[w] = sd_bus_flush_close_unref(buses[w]); |
4fbd7192 LP |
242 | } |
243 | ||
c59e2ec6 | 244 | static void ask_password_agent_open_maybe(void) { |
729e3769 | 245 | /* Open the password agent as a child process if necessary */ |
501fc174 | 246 | |
1ae17672 ZJS |
247 | if (arg_dry_run) |
248 | return; | |
249 | ||
729e3769 | 250 | if (arg_scope != UNIT_FILE_SYSTEM) |
501fc174 LP |
251 | return; |
252 | ||
c59e2ec6 | 253 | ask_password_agent_open_if_enabled(arg_transport, arg_ask_password); |
6bb92a16 LP |
254 | } |
255 | ||
8a4b13c5 | 256 | static void polkit_agent_open_maybe(void) { |
6bb92a16 LP |
257 | /* Open the polkit agent as a child process if necessary */ |
258 | ||
6bb92a16 LP |
259 | if (arg_scope != UNIT_FILE_SYSTEM) |
260 | return; | |
261 | ||
8a4b13c5 | 262 | polkit_agent_open_if_enabled(arg_transport, arg_ask_password); |
501fc174 LP |
263 | } |
264 | ||
3c756001 LP |
265 | static OutputFlags get_output_flags(void) { |
266 | return | |
267 | arg_all * OUTPUT_SHOW_ALL | | |
459b9f9f | 268 | (arg_full || !on_tty() || pager_have()) * OUTPUT_FULL_WIDTH | |
40c9fe4c | 269 | colors_enabled() * OUTPUT_COLOR | |
8b0cc9a3 | 270 | !arg_quiet * OUTPUT_WARN_CUTOFF; |
3c756001 LP |
271 | } |
272 | ||
f459b602 | 273 | static int translate_bus_error_to_exit_status(int r, const sd_bus_error *error) { |
22f4096c LP |
274 | assert(error); |
275 | ||
f459b602 | 276 | if (!sd_bus_error_is_set(error)) |
22f4096c LP |
277 | return r; |
278 | ||
f459b602 MAP |
279 | if (sd_bus_error_has_name(error, SD_BUS_ERROR_ACCESS_DENIED) || |
280 | sd_bus_error_has_name(error, BUS_ERROR_ONLY_BY_DEPENDENCY) || | |
281 | sd_bus_error_has_name(error, BUS_ERROR_NO_ISOLATION) || | |
282 | sd_bus_error_has_name(error, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE)) | |
22f4096c LP |
283 | return EXIT_NOPERMISSION; |
284 | ||
f459b602 | 285 | if (sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT)) |
22f4096c LP |
286 | return EXIT_NOTINSTALLED; |
287 | ||
f459b602 | 288 | if (sd_bus_error_has_name(error, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE) || |
718db961 | 289 | sd_bus_error_has_name(error, SD_BUS_ERROR_NOT_SUPPORTED)) |
22f4096c LP |
290 | return EXIT_NOTIMPLEMENTED; |
291 | ||
f459b602 | 292 | if (sd_bus_error_has_name(error, BUS_ERROR_LOAD_FAILED)) |
22f4096c LP |
293 | return EXIT_NOTCONFIGURED; |
294 | ||
295 | if (r != 0) | |
296 | return r; | |
297 | ||
298 | return EXIT_FAILURE; | |
299 | } | |
300 | ||
4fbd7192 | 301 | static bool install_client_side(void) { |
4fbd7192 LP |
302 | /* Decides when to execute enable/disable/... operations |
303 | * client-side rather than server-side. */ | |
4f16c1f4 | 304 | |
f38951a6 | 305 | if (running_in_chroot_or_offline()) |
729e3769 LP |
306 | return true; |
307 | ||
308 | if (sd_booted() <= 0) | |
309 | return true; | |
310 | ||
311 | if (!isempty(arg_root)) | |
312 | return true; | |
313 | ||
314 | if (arg_scope == UNIT_FILE_GLOBAL) | |
315 | return true; | |
316 | ||
b41b9d2a LP |
317 | /* Unsupported environment variable, mostly for debugging purposes */ |
318 | if (getenv_bool("SYSTEMCTL_INSTALL_CLIENT_SIDE") > 0) | |
319 | return true; | |
320 | ||
729e3769 LP |
321 | return false; |
322 | } | |
323 | ||
93bab288 | 324 | static int compare_unit_info(const UnitInfo *a, const UnitInfo *b) { |
1238ee09 LP |
325 | int r; |
326 | ||
327 | /* First, order by machine */ | |
6cf3011c ZJS |
328 | r = strcasecmp_ptr(a->machine, b->machine); |
329 | if (r != 0) | |
330 | return r; | |
36c32ba2 | 331 | |
1238ee09 | 332 | /* Second, order by unit type */ |
6cf3011c ZJS |
333 | r = strcasecmp_ptr(strrchr(a->id, '.'), strrchr(b->id, '.')); |
334 | if (r != 0) | |
335 | return r; | |
36c32ba2 | 336 | |
1238ee09 | 337 | /* Third, order by name */ |
93bab288 | 338 | return strcasecmp(a->id, b->id); |
36c32ba2 LP |
339 | } |
340 | ||
16484a8a ZJS |
341 | static const char* unit_type_suffix(const char *name) { |
342 | const char *dot; | |
343 | ||
344 | dot = strrchr(name, '.'); | |
345 | if (!dot) | |
346 | return ""; | |
347 | ||
348 | return dot + 1; | |
349 | } | |
350 | ||
d8fba7c6 | 351 | static bool output_show_unit(const UnitInfo *u, char **patterns) { |
f8591ee1 LP |
352 | assert(u); |
353 | ||
2404701e | 354 | if (!strv_fnmatch_or_empty(patterns, u->id, FNM_NOESCAPE)) |
d8fba7c6 | 355 | return false; |
d8fba7c6 | 356 | |
16484a8a ZJS |
357 | if (arg_types && !strv_find(arg_types, unit_type_suffix(u->id))) |
358 | return false; | |
6c71341a ZJS |
359 | |
360 | if (arg_all) | |
361 | return true; | |
362 | ||
409472cb FB |
363 | /* Note that '--all' is not purely a state filter, but also a |
364 | * filter that hides units that "follow" other units (which is | |
365 | * used for device units that appear under different names). */ | |
366 | if (!isempty(u->following)) | |
367 | return false; | |
368 | ||
ebc96265 FB |
369 | if (!strv_isempty(arg_states)) |
370 | return true; | |
371 | ||
372 | /* By default show all units except the ones in inactive | |
373 | * state and with no pending job */ | |
6c71341a ZJS |
374 | if (u->job_id > 0) |
375 | return true; | |
376 | ||
409472cb | 377 | if (streq(u->active_state, "inactive")) |
6c71341a ZJS |
378 | return false; |
379 | ||
380 | return true; | |
33330222 ZJS |
381 | } |
382 | ||
49cd06fa LP |
383 | static int output_table(Table *table) { |
384 | int r; | |
385 | ||
386 | assert(table); | |
387 | ||
388 | if (OUTPUT_MODE_IS_JSON(arg_output)) | |
389 | r = table_print_json(table, NULL, output_mode_to_json_format_flags(arg_output) | JSON_FORMAT_COLOR_AUTO); | |
390 | else | |
391 | r = table_print(table, NULL); | |
392 | if (r < 0) | |
393 | return log_error_errno(r, "Failed to show table: %m"); | |
394 | ||
395 | return 0; | |
396 | } | |
397 | ||
1238ee09 | 398 | static int output_units_list(const UnitInfo *unit_infos, unsigned c) { |
de9a8fe1 | 399 | _cleanup_(table_unrefp) Table *table = NULL; |
de9a8fe1 | 400 | int r; |
cbc9fbd1 | 401 | |
de9a8fe1 FS |
402 | table = table_new("", "unit", "load", "active", "sub", "job", "description"); |
403 | if (!table) | |
404 | return log_oom(); | |
cbc9fbd1 | 405 | |
de9a8fe1 | 406 | table_set_header(table, !arg_no_legend); |
1cabd2d0 JW |
407 | if (arg_plain) { |
408 | /* Hide the 'glyph' column when --plain is requested */ | |
d171e679 FS |
409 | r = table_hide_column_from_display(table, 0); |
410 | if (r < 0) | |
411 | return log_error_errno(r, "Failed to hide column: %m"); | |
412 | } | |
de9a8fe1 FS |
413 | if (arg_full) |
414 | table_set_width(table, 0); | |
4deb3b93 | 415 | |
0773357a LP |
416 | (void) table_set_empty_string(table, "-"); |
417 | ||
c9d243cd ZJS |
418 | int job_count = 0; |
419 | for (const UnitInfo *u = unit_infos; unit_infos && u < unit_infos + c; u++) { | |
de9a8fe1 FS |
420 | _cleanup_free_ char *j = NULL; |
421 | const char *on_underline = "", *on_loaded = "", *on_active = ""; | |
422 | const char *on_circle = "", *id; | |
16484a8a | 423 | bool circle = false, underline = false; |
b036fc00 | 424 | |
16484a8a ZJS |
425 | if (u + 1 < unit_infos + c && |
426 | !streq(unit_type_suffix(u->id), unit_type_suffix((u + 1)->id))) { | |
427 | on_underline = ansi_underline(); | |
16484a8a ZJS |
428 | underline = true; |
429 | } | |
430 | ||
c4555ad8 | 431 | if (STR_IN_SET(u->load_state, "error", "not-found", "bad-setting", "masked") && !arg_plain) { |
1fab5797 | 432 | on_circle = underline ? ansi_highlight_yellow_underline() : ansi_highlight_yellow(); |
90c3f79d | 433 | circle = true; |
16484a8a | 434 | on_loaded = underline ? ansi_highlight_red_underline() : ansi_highlight_red(); |
250ba664 | 435 | } else if (streq(u->active_state, "failed") && !arg_plain) { |
1fab5797 | 436 | on_circle = underline ? ansi_highlight_red_underline() : ansi_highlight_red(); |
90c3f79d | 437 | circle = true; |
16484a8a | 438 | on_active = underline ? ansi_highlight_red_underline() : ansi_highlight_red(); |
de9a8fe1 | 439 | } else { |
1fab5797 | 440 | on_circle = on_underline; |
de9a8fe1 FS |
441 | on_active = on_underline; |
442 | on_loaded = on_underline; | |
90c3f79d | 443 | } |
eb68c413 | 444 | |
1238ee09 | 445 | if (u->machine) { |
605405c6 | 446 | j = strjoin(u->machine, ":", u->id); |
1238ee09 LP |
447 | if (!j) |
448 | return log_oom(); | |
449 | ||
450 | id = j; | |
451 | } else | |
452 | id = u->id; | |
453 | ||
de9a8fe1 FS |
454 | r = table_add_many(table, |
455 | TABLE_STRING, circle ? special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE) : " ", | |
1fab5797 | 456 | TABLE_SET_BOTH_COLORS, on_circle, |
de9a8fe1 | 457 | TABLE_STRING, id, |
1fab5797 | 458 | TABLE_SET_BOTH_COLORS, on_active, |
de9a8fe1 | 459 | TABLE_STRING, u->load_state, |
1fab5797 | 460 | TABLE_SET_BOTH_COLORS, on_loaded, |
de9a8fe1 | 461 | TABLE_STRING, u->active_state, |
1fab5797 | 462 | TABLE_SET_BOTH_COLORS, on_active, |
de9a8fe1 | 463 | TABLE_STRING, u->sub_state, |
1fab5797 | 464 | TABLE_SET_BOTH_COLORS, on_active, |
de9a8fe1 | 465 | TABLE_STRING, u->job_id ? u->job_type: "", |
1fab5797 | 466 | TABLE_SET_BOTH_COLORS, u->job_id ? on_underline : "", |
de9a8fe1 | 467 | TABLE_STRING, u->description, |
1fab5797 | 468 | TABLE_SET_BOTH_COLORS, on_underline); |
de9a8fe1 FS |
469 | if (r < 0) |
470 | return table_log_add_error(r); | |
1238ee09 | 471 | |
de9a8fe1 FS |
472 | if (u->job_id != 0) |
473 | job_count++; | |
474 | } | |
eb68c413 | 475 | |
de9a8fe1 FS |
476 | if (job_count == 0) { |
477 | /* There's no data in the JOB column, so let's hide it */ | |
d171e679 | 478 | r = table_hide_column_from_display(table, 5); |
de9a8fe1 | 479 | if (r < 0) |
d171e679 | 480 | return log_error_errno(r, "Failed to hide column: %m"); |
eb68c413 ZJS |
481 | } |
482 | ||
49cd06fa | 483 | r = output_table(table); |
de9a8fe1 | 484 | if (r < 0) |
49cd06fa | 485 | return r; |
de9a8fe1 | 486 | |
ebed32bf | 487 | if (!arg_no_legend) { |
57f7ae4f | 488 | const char *on, *off; |
de9a8fe1 | 489 | size_t records = table_get_rows(table) - 1; |
57f7ae4f | 490 | |
de9a8fe1 | 491 | if (records > 0) { |
90c3f79d LP |
492 | puts("\n" |
493 | "LOAD = Reflects whether the unit definition was properly loaded.\n" | |
e3e0314b ZJS |
494 | "ACTIVE = The high-level unit activation state, i.e. generalization of SUB.\n" |
495 | "SUB = The low-level unit activation state, values depend on unit type."); | |
496 | puts(job_count ? "JOB = Pending job for the unit.\n" : ""); | |
0b5a519c | 497 | on = ansi_highlight(); |
1fc464f6 | 498 | off = ansi_normal(); |
57f7ae4f | 499 | } else { |
0b5a519c | 500 | on = ansi_highlight_red(); |
1fc464f6 | 501 | off = ansi_normal(); |
57f7ae4f | 502 | } |
eb68c413 | 503 | |
8eb0cafe | 504 | if (arg_all || strv_contains(arg_states, "inactive")) |
de9a8fe1 | 505 | printf("%s%zu loaded units listed.%s\n" |
57f7ae4f | 506 | "To show all installed unit files use 'systemctl list-unit-files'.\n", |
de9a8fe1 | 507 | on, records, off); |
8eb0cafe | 508 | else if (!arg_states) |
de9a8fe1 | 509 | printf("%s%zu loaded units listed.%s Pass --all to see loaded but inactive units, too.\n" |
57f7ae4f | 510 | "To show all installed unit files use 'systemctl list-unit-files'.\n", |
de9a8fe1 | 511 | on, records, off); |
8eb0cafe | 512 | else |
de9a8fe1 | 513 | printf("%zu loaded units listed.\n", records); |
eb68c413 | 514 | } |
1238ee09 LP |
515 | |
516 | return 0; | |
eb68c413 ZJS |
517 | } |
518 | ||
a00963a2 | 519 | static int get_unit_list( |
f459b602 | 520 | sd_bus *bus, |
1238ee09 LP |
521 | const char *machine, |
522 | char **patterns, | |
523 | UnitInfo **unit_infos, | |
524 | int c, | |
525 | sd_bus_message **_reply) { | |
a00963a2 | 526 | |
4afd3348 LP |
527 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
528 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
529 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
ca2d3784 | 530 | size_t size = c; |
1238ee09 | 531 | int r; |
f459b602 | 532 | UnitInfo u; |
313fe66f | 533 | bool fallback = false; |
7e4249b9 | 534 | |
265a7a2a | 535 | assert(bus); |
1238ee09 | 536 | assert(unit_infos); |
f459b602 | 537 | assert(_reply); |
1238ee09 | 538 | |
8010c205 | 539 | r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "ListUnitsByPatterns"); |
cdc06ed7 DS |
540 | if (r < 0) |
541 | return bus_log_create_error(r); | |
542 | ||
543 | r = sd_bus_message_append_strv(m, arg_states); | |
544 | if (r < 0) | |
545 | return bus_log_create_error(r); | |
546 | ||
313fe66f | 547 | r = sd_bus_message_append_strv(m, patterns); |
548 | if (r < 0) | |
549 | return bus_log_create_error(r); | |
550 | ||
cdc06ed7 | 551 | r = sd_bus_call(bus, m, 0, &error, &reply); |
33d52725 ZJS |
552 | if (r < 0 && (sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_METHOD) || |
553 | sd_bus_error_has_name(&error, SD_BUS_ERROR_ACCESS_DENIED))) { | |
313fe66f | 554 | /* Fallback to legacy ListUnitsFiltered method */ |
555 | fallback = true; | |
556 | log_debug_errno(r, "Failed to list units: %s Falling back to ListUnitsFiltered method.", bus_error_message(&error, r)); | |
557 | m = sd_bus_message_unref(m); | |
558 | sd_bus_error_free(&error); | |
559 | ||
8010c205 | 560 | r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "ListUnitsFiltered"); |
313fe66f | 561 | if (r < 0) |
562 | return bus_log_create_error(r); | |
563 | ||
564 | r = sd_bus_message_append_strv(m, arg_states); | |
565 | if (r < 0) | |
566 | return bus_log_create_error(r); | |
567 | ||
568 | r = sd_bus_call(bus, m, 0, &error, &reply); | |
569 | } | |
4c3e8e39 LP |
570 | if (r < 0) |
571 | return log_error_errno(r, "Failed to list units: %s", bus_error_message(&error, r)); | |
7e4249b9 | 572 | |
f459b602 MAP |
573 | r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssssouso)"); |
574 | if (r < 0) | |
575 | return bus_log_parse_error(r); | |
7e4249b9 | 576 | |
f459b602 | 577 | while ((r = bus_parse_unit_info(reply, &u)) > 0) { |
1238ee09 LP |
578 | u.machine = machine; |
579 | ||
313fe66f | 580 | if (!output_show_unit(&u, fallback ? patterns : NULL)) |
8d5ba5a9 | 581 | continue; |
7e4249b9 | 582 | |
1238ee09 | 583 | if (!GREEDY_REALLOC(*unit_infos, size, c+1)) |
f459b602 | 584 | return log_oom(); |
36c32ba2 | 585 | |
1238ee09 | 586 | (*unit_infos)[c++] = u; |
991f2a39 | 587 | } |
f459b602 MAP |
588 | if (r < 0) |
589 | return bus_log_parse_error(r); | |
991f2a39 | 590 | |
f459b602 MAP |
591 | r = sd_bus_message_exit_container(reply); |
592 | if (r < 0) | |
593 | return bus_log_parse_error(r); | |
594 | ||
1cc6c93a | 595 | *_reply = TAKE_PTR(reply); |
f459b602 | 596 | |
1238ee09 LP |
597 | return c; |
598 | } | |
599 | ||
600 | static void message_set_freep(Set **set) { | |
224b0e7a | 601 | set_free_with_destructor(*set, sd_bus_message_unref); |
1238ee09 LP |
602 | } |
603 | ||
604 | static int get_unit_list_recursive( | |
605 | sd_bus *bus, | |
606 | char **patterns, | |
607 | UnitInfo **_unit_infos, | |
608 | Set **_replies, | |
609 | char ***_machines) { | |
610 | ||
611 | _cleanup_free_ UnitInfo *unit_infos = NULL; | |
612 | _cleanup_(message_set_freep) Set *replies; | |
613 | sd_bus_message *reply; | |
614 | int c, r; | |
615 | ||
616 | assert(bus); | |
617 | assert(_replies); | |
618 | assert(_unit_infos); | |
619 | assert(_machines); | |
620 | ||
d5099efc | 621 | replies = set_new(NULL); |
1238ee09 LP |
622 | if (!replies) |
623 | return log_oom(); | |
624 | ||
625 | c = get_unit_list(bus, NULL, patterns, &unit_infos, 0, &reply); | |
626 | if (c < 0) | |
627 | return c; | |
628 | ||
629 | r = set_put(replies, reply); | |
630 | if (r < 0) { | |
631 | sd_bus_message_unref(reply); | |
4c3e8e39 | 632 | return log_oom(); |
1238ee09 LP |
633 | } |
634 | ||
635 | if (arg_recursive) { | |
636 | _cleanup_strv_free_ char **machines = NULL; | |
637 | char **i; | |
638 | ||
639 | r = sd_get_machine_names(&machines); | |
640 | if (r < 0) | |
4c3e8e39 | 641 | return log_error_errno(r, "Failed to get machine names: %m"); |
1238ee09 LP |
642 | |
643 | STRV_FOREACH(i, machines) { | |
4afd3348 | 644 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *container = NULL; |
1238ee09 LP |
645 | int k; |
646 | ||
de33fc62 | 647 | r = sd_bus_open_system_machine(&container, *i); |
1238ee09 | 648 | if (r < 0) { |
4c3e8e39 | 649 | log_warning_errno(r, "Failed to connect to container %s, ignoring: %m", *i); |
1238ee09 LP |
650 | continue; |
651 | } | |
652 | ||
653 | k = get_unit_list(container, *i, patterns, &unit_infos, c, &reply); | |
654 | if (k < 0) | |
655 | return k; | |
656 | ||
657 | c = k; | |
658 | ||
659 | r = set_put(replies, reply); | |
660 | if (r < 0) { | |
661 | sd_bus_message_unref(reply); | |
4c3e8e39 | 662 | return log_oom(); |
1238ee09 LP |
663 | } |
664 | } | |
665 | ||
1cc6c93a | 666 | *_machines = TAKE_PTR(machines); |
1238ee09 LP |
667 | } else |
668 | *_machines = NULL; | |
669 | ||
1cc6c93a | 670 | *_unit_infos = TAKE_PTR(unit_infos); |
1cc6c93a | 671 | *_replies = TAKE_PTR(replies); |
1238ee09 | 672 | |
f459b602 | 673 | return c; |
991f2a39 ZJS |
674 | } |
675 | ||
1d56bc09 | 676 | static int expand_names(sd_bus *bus, char **names, const char* suffix, char ***ret, bool *ret_expanded) { |
3a77f9fb KK |
677 | _cleanup_strv_free_ char **mangled = NULL, **globs = NULL; |
678 | char **name; | |
679 | int r, i; | |
680 | ||
681 | assert(bus); | |
682 | assert(ret); | |
683 | ||
684 | STRV_FOREACH(name, names) { | |
685 | char *t; | |
686 | UnitNameMangle options = UNIT_NAME_MANGLE_GLOB | (arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN); | |
687 | ||
df7c4eb6 | 688 | r = unit_name_mangle_with_suffix(*name, NULL, options, suffix ?: ".service", &t); |
3a77f9fb KK |
689 | if (r < 0) |
690 | return log_error_errno(r, "Failed to mangle name: %m"); | |
691 | ||
692 | if (string_is_glob(t)) | |
693 | r = strv_consume(&globs, t); | |
694 | else | |
695 | r = strv_consume(&mangled, t); | |
696 | if (r < 0) | |
697 | return log_oom(); | |
698 | } | |
699 | ||
700 | /* Query the manager only if any of the names are a glob, since | |
701 | * this is fairly expensive */ | |
1d56bc09 ZJS |
702 | bool expanded = !strv_isempty(globs); |
703 | if (expanded) { | |
3a77f9fb KK |
704 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
705 | _cleanup_free_ UnitInfo *unit_infos = NULL; | |
706 | size_t allocated, n; | |
707 | ||
708 | r = get_unit_list(bus, NULL, globs, &unit_infos, 0, &reply); | |
709 | if (r < 0) | |
710 | return r; | |
711 | ||
712 | n = strv_length(mangled); | |
713 | allocated = n + 1; | |
714 | ||
715 | for (i = 0; i < r; i++) { | |
716 | if (!GREEDY_REALLOC(mangled, allocated, n+2)) | |
717 | return log_oom(); | |
718 | ||
719 | mangled[n] = strdup(unit_infos[i].id); | |
720 | if (!mangled[n]) | |
721 | return log_oom(); | |
722 | ||
723 | mangled[++n] = NULL; | |
724 | } | |
725 | } | |
726 | ||
1d56bc09 ZJS |
727 | if (ret_expanded) |
728 | *ret_expanded = expanded; | |
729 | ||
3a77f9fb KK |
730 | *ret = TAKE_PTR(mangled); |
731 | return 0; | |
732 | } | |
733 | ||
e9c387c8 KK |
734 | static int list_dependencies_get_dependencies(sd_bus *bus, const char *name, char ***ret) { |
735 | _cleanup_strv_free_ char **deps = NULL; | |
736 | ||
737 | static const struct bus_properties_map map[_DEPENDENCY_MAX][6] = { | |
738 | [DEPENDENCY_FORWARD] = { | |
739 | { "Requires", "as", NULL, 0 }, | |
740 | { "Requisite", "as", NULL, 0 }, | |
741 | { "Wants", "as", NULL, 0 }, | |
742 | { "ConsistsOf", "as", NULL, 0 }, | |
743 | { "BindsTo", "as", NULL, 0 }, | |
744 | {} | |
745 | }, | |
746 | [DEPENDENCY_REVERSE] = { | |
747 | { "RequiredBy", "as", NULL, 0 }, | |
748 | { "RequisiteOf", "as", NULL, 0 }, | |
749 | { "WantedBy", "as", NULL, 0 }, | |
750 | { "PartOf", "as", NULL, 0 }, | |
751 | { "BoundBy", "as", NULL, 0 }, | |
752 | {} | |
753 | }, | |
754 | [DEPENDENCY_AFTER] = { | |
755 | { "After", "as", NULL, 0 }, | |
756 | {} | |
757 | }, | |
758 | [DEPENDENCY_BEFORE] = { | |
759 | { "Before", "as", NULL, 0 }, | |
760 | {} | |
761 | }, | |
762 | }; | |
763 | ||
764 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
765 | _cleanup_free_ char *dbus_path = NULL; | |
766 | int r; | |
767 | ||
768 | assert(bus); | |
769 | assert(name); | |
770 | assert(ret); | |
771 | ||
772 | dbus_path = unit_dbus_path_from_name(name); | |
773 | if (!dbus_path) | |
774 | return log_oom(); | |
775 | ||
776 | r = bus_map_all_properties(bus, | |
777 | "org.freedesktop.systemd1", | |
778 | dbus_path, | |
779 | map[arg_dependency], | |
780 | BUS_MAP_STRDUP, | |
781 | &error, | |
782 | NULL, | |
783 | &deps); | |
784 | if (r < 0) | |
785 | return log_error_errno(r, "Failed to get properties of %s: %s", name, bus_error_message(&error, r)); | |
786 | ||
dbf2801f ZJS |
787 | strv_uniq(deps); /* Sometimes a unit might have multiple deps on the other unit, |
788 | * but we still want to show it just once. */ | |
e9c387c8 KK |
789 | *ret = TAKE_PTR(deps); |
790 | ||
791 | return 0; | |
792 | } | |
793 | ||
794 | static int append_unit_dependencies(sd_bus *bus, char **names, char ***ret) { | |
795 | _cleanup_strv_free_ char **with_deps = NULL; | |
796 | char **name; | |
797 | ||
798 | assert(bus); | |
799 | assert(ret); | |
800 | ||
801 | STRV_FOREACH(name, names) { | |
802 | _cleanup_strv_free_ char **deps = NULL; | |
803 | ||
804 | if (strv_extend(&with_deps, *name) < 0) | |
805 | return log_oom(); | |
806 | ||
807 | (void) list_dependencies_get_dependencies(bus, *name, &deps); | |
808 | ||
809 | if (strv_extend_strv(&with_deps, deps, true) < 0) | |
810 | return log_oom(); | |
811 | } | |
812 | ||
813 | *ret = TAKE_PTR(with_deps); | |
814 | ||
815 | return 0; | |
816 | } | |
817 | ||
818 | static int maybe_extend_with_unit_dependencies(sd_bus *bus, char ***list) { | |
819 | assert(bus); | |
820 | assert(list); | |
821 | ||
822 | if (arg_with_dependencies) { | |
823 | int r; | |
824 | _cleanup_strv_free_ char **list_with_deps = NULL; | |
825 | ||
826 | r = append_unit_dependencies(bus, *list, &list_with_deps); | |
827 | if (r < 0) | |
828 | return log_error_errno(r, "Failed to append unit dependencies: %m"); | |
829 | ||
830 | strv_free(*list); | |
831 | *list = TAKE_PTR(list_with_deps); | |
832 | } | |
833 | ||
834 | return 0; | |
835 | } | |
836 | ||
e449de87 | 837 | static int list_units(int argc, char *argv[], void *userdata) { |
f459b602 | 838 | _cleanup_free_ UnitInfo *unit_infos = NULL; |
1238ee09 LP |
839 | _cleanup_(message_set_freep) Set *replies = NULL; |
840 | _cleanup_strv_free_ char **machines = NULL; | |
4fbd7192 | 841 | sd_bus *bus; |
991f2a39 ZJS |
842 | int r; |
843 | ||
4fbd7192 LP |
844 | r = acquire_bus(BUS_MANAGER, &bus); |
845 | if (r < 0) | |
846 | return r; | |
847 | ||
0221d68a | 848 | (void) pager_open(arg_pager_flags); |
d2ad7e1b | 849 | |
e9c387c8 KK |
850 | if (arg_with_dependencies) { |
851 | _cleanup_strv_free_ char **names = NULL; | |
852 | ||
853 | r = append_unit_dependencies(bus, strv_skip(argv, 1), &names); | |
854 | if (r < 0) | |
855 | return r; | |
856 | ||
857 | r = get_unit_list_recursive(bus, names, &unit_infos, &replies, &machines); | |
858 | if (r < 0) | |
859 | return r; | |
860 | } else { | |
861 | r = get_unit_list_recursive(bus, strv_skip(argv, 1), &unit_infos, &replies, &machines); | |
862 | if (r < 0) | |
863 | return r; | |
864 | } | |
991f2a39 | 865 | |
93bab288 | 866 | typesafe_qsort(unit_infos, r, compare_unit_info); |
1238ee09 | 867 | return output_units_list(unit_infos, r); |
991f2a39 ZJS |
868 | } |
869 | ||
a00963a2 | 870 | static int get_triggered_units( |
f459b602 MAP |
871 | sd_bus *bus, |
872 | const char* path, | |
873 | char*** ret) { | |
a00963a2 | 874 | |
4afd3348 | 875 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
991f2a39 ZJS |
876 | int r; |
877 | ||
4fbd7192 LP |
878 | assert(bus); |
879 | assert(path); | |
880 | assert(ret); | |
881 | ||
f459b602 MAP |
882 | r = sd_bus_get_property_strv( |
883 | bus, | |
884 | "org.freedesktop.systemd1", | |
885 | path, | |
886 | "org.freedesktop.systemd1.Unit", | |
887 | "Triggers", | |
888 | &error, | |
889 | ret); | |
f459b602 | 890 | if (r < 0) |
691395d8 | 891 | return log_error_errno(r, "Failed to determine triggers: %s", bus_error_message(&error, r)); |
991f2a39 ZJS |
892 | |
893 | return 0; | |
894 | } | |
895 | ||
f459b602 MAP |
896 | static int get_listening( |
897 | sd_bus *bus, | |
898 | const char* unit_path, | |
cbb76c29 | 899 | char*** listening) { |
f459b602 | 900 | |
4afd3348 LP |
901 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
902 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
f459b602 | 903 | const char *type, *path; |
cbb76c29 | 904 | int r, n = 0; |
991f2a39 | 905 | |
f459b602 MAP |
906 | r = sd_bus_get_property( |
907 | bus, | |
908 | "org.freedesktop.systemd1", | |
909 | unit_path, | |
910 | "org.freedesktop.systemd1.Socket", | |
911 | "Listen", | |
912 | &error, | |
913 | &reply, | |
914 | "a(ss)"); | |
691395d8 LP |
915 | if (r < 0) |
916 | return log_error_errno(r, "Failed to get list of listening sockets: %s", bus_error_message(&error, r)); | |
991f2a39 | 917 | |
f459b602 MAP |
918 | r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ss)"); |
919 | if (r < 0) | |
920 | return bus_log_parse_error(r); | |
991f2a39 | 921 | |
f459b602 | 922 | while ((r = sd_bus_message_read(reply, "(ss)", &type, &path)) > 0) { |
36c32ba2 | 923 | |
0d95178e | 924 | r = strv_extend(listening, type); |
f459b602 MAP |
925 | if (r < 0) |
926 | return log_oom(); | |
991f2a39 | 927 | |
0d95178e | 928 | r = strv_extend(listening, path); |
f459b602 MAP |
929 | if (r < 0) |
930 | return log_oom(); | |
7e4249b9 | 931 | |
cbb76c29 | 932 | n++; |
36c32ba2 | 933 | } |
f459b602 MAP |
934 | if (r < 0) |
935 | return bus_log_parse_error(r); | |
936 | ||
937 | r = sd_bus_message_exit_container(reply); | |
938 | if (r < 0) | |
939 | return bus_log_parse_error(r); | |
36c32ba2 | 940 | |
cbb76c29 | 941 | return n; |
991f2a39 ZJS |
942 | } |
943 | ||
944 | struct socket_info { | |
0cfc3525 | 945 | const char *machine; |
991f2a39 ZJS |
946 | const char* id; |
947 | ||
948 | char* type; | |
949 | char* path; | |
950 | ||
951 | /* Note: triggered is a list here, although it almost certainly | |
952 | * will always be one unit. Nevertheless, dbus API allows for multiple | |
f7340ab2 | 953 | * values, so let's follow that. */ |
991f2a39 ZJS |
954 | char** triggered; |
955 | ||
956 | /* The strv above is shared. free is set only in the first one. */ | |
957 | bool own_triggered; | |
958 | }; | |
959 | ||
cbb76c29 | 960 | static int socket_info_compare(const struct socket_info *a, const struct socket_info *b) { |
93bab288 | 961 | int r; |
cbc9fbd1 | 962 | |
cbb76c29 LP |
963 | assert(a); |
964 | assert(b); | |
965 | ||
6cf3011c ZJS |
966 | r = strcasecmp_ptr(a->machine, b->machine); |
967 | if (r != 0) | |
968 | return r; | |
0cfc3525 | 969 | |
93bab288 | 970 | r = strcmp(a->path, b->path); |
6cf3011c ZJS |
971 | if (r != 0) |
972 | return r; | |
cbc9fbd1 | 973 | |
6cf3011c | 974 | return strcmp(a->type, b->type); |
991f2a39 ZJS |
975 | } |
976 | ||
977 | static int output_sockets_list(struct socket_info *socket_infos, unsigned cs) { | |
de9a8fe1 | 978 | _cleanup_(table_unrefp) Table *table = NULL; |
991f2a39 | 979 | struct socket_info *s; |
991f2a39 | 980 | const char *on, *off; |
de9a8fe1 | 981 | int r; |
991f2a39 | 982 | |
bc04bb0d | 983 | table = table_new("listen", "type", "unit", "activates"); |
de9a8fe1 FS |
984 | if (!table) |
985 | return log_oom(); | |
991f2a39 | 986 | |
de9a8fe1 FS |
987 | if (!arg_show_types) { |
988 | /* Hide the second (TYPE) column */ | |
ad5555b4 | 989 | r = table_set_display(table, (size_t) 0, (size_t) 2, (size_t) 3, (size_t) -1); |
de9a8fe1 FS |
990 | if (r < 0) |
991 | return log_error_errno(r, "Failed to set columns to display: %m"); | |
991f2a39 ZJS |
992 | } |
993 | ||
de9a8fe1 FS |
994 | table_set_header(table, !arg_no_legend); |
995 | if (arg_full) | |
996 | table_set_width(table, 0); | |
991f2a39 | 997 | |
0773357a LP |
998 | (void) table_set_empty_string(table, "-"); |
999 | ||
de9a8fe1 | 1000 | if (cs) { |
991f2a39 | 1001 | for (s = socket_infos; s < socket_infos + cs; s++) { |
50098d87 | 1002 | _cleanup_free_ char *j = NULL; |
0cfc3525 | 1003 | const char *path; |
991f2a39 | 1004 | |
0cfc3525 | 1005 | if (s->machine) { |
605405c6 | 1006 | j = strjoin(s->machine, ":", s->path); |
0cfc3525 TA |
1007 | if (!j) |
1008 | return log_oom(); | |
1009 | path = j; | |
1010 | } else | |
1011 | path = s->path; | |
1012 | ||
de9a8fe1 FS |
1013 | r = table_add_many(table, |
1014 | TABLE_STRING, path, | |
1015 | TABLE_STRING, s->type, | |
50098d87 LP |
1016 | TABLE_STRING, s->id); |
1017 | if (r < 0) | |
1018 | return table_log_add_error(r); | |
1019 | ||
1020 | if (strv_isempty(s->triggered)) | |
1021 | r = table_add_cell(table, NULL, TABLE_EMPTY, NULL); | |
1022 | else if (strv_length(s->triggered) == 1) | |
1023 | r = table_add_cell(table, NULL, TABLE_STRING, s->triggered[0]); | |
1024 | else | |
1025 | /* This should never happen, currently our socket units can only trigger a | |
1026 | * single unit. But let's handle this anyway, who knows what the future | |
1027 | * brings? */ | |
1028 | r = table_add_cell(table, NULL, TABLE_STRV, s->triggered); | |
de9a8fe1 FS |
1029 | if (r < 0) |
1030 | return table_log_add_error(r); | |
50098d87 | 1031 | |
991f2a39 ZJS |
1032 | } |
1033 | ||
0b5a519c | 1034 | on = ansi_highlight(); |
1fc464f6 | 1035 | off = ansi_normal(); |
991f2a39 | 1036 | } else { |
0b5a519c | 1037 | on = ansi_highlight_red(); |
1fc464f6 | 1038 | off = ansi_normal(); |
991f2a39 ZJS |
1039 | } |
1040 | ||
49cd06fa | 1041 | r = output_table(table); |
de9a8fe1 | 1042 | if (r < 0) |
49cd06fa | 1043 | return r; |
de9a8fe1 | 1044 | |
571bfc6c | 1045 | if (!arg_no_legend) { |
de9a8fe1 | 1046 | printf("\n%s%u sockets listed.%s\n", on, cs, off); |
571bfc6c MM |
1047 | if (!arg_all) |
1048 | printf("Pass --all to see loaded but inactive sockets, too.\n"); | |
1049 | } | |
265a7a2a ZJS |
1050 | |
1051 | return 0; | |
1052 | } | |
1053 | ||
e449de87 | 1054 | static int list_sockets(int argc, char *argv[], void *userdata) { |
0cfc3525 TA |
1055 | _cleanup_(message_set_freep) Set *replies = NULL; |
1056 | _cleanup_strv_free_ char **machines = NULL; | |
3a77f9fb | 1057 | _cleanup_strv_free_ char **sockets_with_suffix = NULL; |
f459b602 | 1058 | _cleanup_free_ UnitInfo *unit_infos = NULL; |
8d5ba5a9 | 1059 | _cleanup_free_ struct socket_info *socket_infos = NULL; |
f459b602 | 1060 | const UnitInfo *u; |
991f2a39 | 1061 | struct socket_info *s; |
cbb76c29 | 1062 | unsigned cs = 0; |
991f2a39 | 1063 | size_t size = 0; |
ad83b4c4 | 1064 | int r = 0, n; |
4fbd7192 | 1065 | sd_bus *bus; |
265a7a2a | 1066 | |
4fbd7192 LP |
1067 | r = acquire_bus(BUS_MANAGER, &bus); |
1068 | if (r < 0) | |
1069 | return r; | |
1070 | ||
0221d68a | 1071 | (void) pager_open(arg_pager_flags); |
d2ad7e1b | 1072 | |
1d56bc09 | 1073 | r = expand_names(bus, strv_skip(argv, 1), ".socket", &sockets_with_suffix, NULL); |
3a77f9fb KK |
1074 | if (r < 0) |
1075 | return r; | |
265a7a2a | 1076 | |
3a77f9fb KK |
1077 | if (argc == 1 || sockets_with_suffix) { |
1078 | n = get_unit_list_recursive(bus, sockets_with_suffix, &unit_infos, &replies, &machines); | |
1079 | if (n < 0) | |
1080 | return n; | |
991f2a39 | 1081 | |
3a77f9fb KK |
1082 | for (u = unit_infos; u < unit_infos + n; u++) { |
1083 | _cleanup_strv_free_ char **listening = NULL, **triggered = NULL; | |
1084 | int i, c; | |
991f2a39 | 1085 | |
3a77f9fb KK |
1086 | if (!endswith(u->id, ".socket")) |
1087 | continue; | |
991f2a39 | 1088 | |
3a77f9fb KK |
1089 | r = get_triggered_units(bus, u->unit_path, &triggered); |
1090 | if (r < 0) | |
1091 | goto cleanup; | |
991f2a39 | 1092 | |
3a77f9fb KK |
1093 | c = get_listening(bus, u->unit_path, &listening); |
1094 | if (c < 0) { | |
1095 | r = c; | |
1096 | goto cleanup; | |
1097 | } | |
991f2a39 | 1098 | |
3a77f9fb KK |
1099 | if (!GREEDY_REALLOC(socket_infos, size, cs + c)) { |
1100 | r = log_oom(); | |
1101 | goto cleanup; | |
1102 | } | |
991f2a39 | 1103 | |
3a77f9fb KK |
1104 | for (i = 0; i < c; i++) |
1105 | socket_infos[cs + i] = (struct socket_info) { | |
1106 | .machine = u->machine, | |
1107 | .id = u->id, | |
1108 | .type = listening[i*2], | |
1109 | .path = listening[i*2 + 1], | |
1110 | .triggered = triggered, | |
1111 | .own_triggered = i==0, | |
1112 | }; | |
1113 | ||
1114 | /* from this point on we will cleanup those socket_infos */ | |
1115 | cs += c; | |
1116 | free(listening); | |
1117 | listening = triggered = NULL; /* avoid cleanup */ | |
1118 | } | |
991f2a39 | 1119 | |
3a77f9fb KK |
1120 | typesafe_qsort(socket_infos, cs, socket_info_compare); |
1121 | } | |
991f2a39 ZJS |
1122 | |
1123 | output_sockets_list(socket_infos, cs); | |
1124 | ||
1125 | cleanup: | |
1126 | assert(cs == 0 || socket_infos); | |
1127 | for (s = socket_infos; s < socket_infos + cs; s++) { | |
1128 | free(s->type); | |
1129 | free(s->path); | |
1130 | if (s->own_triggered) | |
1131 | strv_free(s->triggered); | |
1132 | } | |
4445a875 | 1133 | |
872c8faa | 1134 | return r; |
4445a875 LP |
1135 | } |
1136 | ||
cbb76c29 LP |
1137 | static int get_next_elapse( |
1138 | sd_bus *bus, | |
1139 | const char *path, | |
1140 | dual_timestamp *next) { | |
1141 | ||
4afd3348 | 1142 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
cbb76c29 LP |
1143 | dual_timestamp t; |
1144 | int r; | |
1145 | ||
1146 | assert(bus); | |
1147 | assert(path); | |
1148 | assert(next); | |
1149 | ||
1150 | r = sd_bus_get_property_trivial( | |
1151 | bus, | |
1152 | "org.freedesktop.systemd1", | |
1153 | path, | |
1154 | "org.freedesktop.systemd1.Timer", | |
1155 | "NextElapseUSecMonotonic", | |
1156 | &error, | |
1157 | 't', | |
1158 | &t.monotonic); | |
691395d8 | 1159 | if (r < 0) |
021723c6 | 1160 | return log_error_errno(r, "Failed to get next elapse time: %s", bus_error_message(&error, r)); |
cbb76c29 LP |
1161 | |
1162 | r = sd_bus_get_property_trivial( | |
1163 | bus, | |
1164 | "org.freedesktop.systemd1", | |
1165 | path, | |
1166 | "org.freedesktop.systemd1.Timer", | |
1167 | "NextElapseUSecRealtime", | |
1168 | &error, | |
1169 | 't', | |
1170 | &t.realtime); | |
691395d8 | 1171 | if (r < 0) |
021723c6 | 1172 | return log_error_errno(r, "Failed to get next elapse time: %s", bus_error_message(&error, r)); |
cbb76c29 LP |
1173 | |
1174 | *next = t; | |
1175 | return 0; | |
1176 | } | |
1177 | ||
d784e2db LP |
1178 | static int get_last_trigger( |
1179 | sd_bus *bus, | |
1180 | const char *path, | |
1181 | usec_t *last) { | |
1182 | ||
4afd3348 | 1183 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
d784e2db LP |
1184 | int r; |
1185 | ||
1186 | assert(bus); | |
1187 | assert(path); | |
1188 | assert(last); | |
1189 | ||
1190 | r = sd_bus_get_property_trivial( | |
1191 | bus, | |
1192 | "org.freedesktop.systemd1", | |
1193 | path, | |
1194 | "org.freedesktop.systemd1.Timer", | |
dedabea4 | 1195 | "LastTriggerUSec", |
d784e2db LP |
1196 | &error, |
1197 | 't', | |
1198 | last); | |
691395d8 LP |
1199 | if (r < 0) |
1200 | return log_error_errno(r, "Failed to get last trigger time: %s", bus_error_message(&error, r)); | |
d784e2db LP |
1201 | |
1202 | return 0; | |
1203 | } | |
1204 | ||
cbb76c29 | 1205 | struct timer_info { |
806a37e7 | 1206 | const char* machine; |
cbb76c29 LP |
1207 | const char* id; |
1208 | usec_t next_elapse; | |
d784e2db | 1209 | usec_t last_trigger; |
cbb76c29 LP |
1210 | char** triggered; |
1211 | }; | |
1212 | ||
1213 | static int timer_info_compare(const struct timer_info *a, const struct timer_info *b) { | |
93bab288 | 1214 | int r; |
806a37e7 | 1215 | |
cbb76c29 LP |
1216 | assert(a); |
1217 | assert(b); | |
1218 | ||
6cf3011c ZJS |
1219 | r = strcasecmp_ptr(a->machine, b->machine); |
1220 | if (r != 0) | |
1221 | return r; | |
806a37e7 | 1222 | |
93bab288 YW |
1223 | r = CMP(a->next_elapse, b->next_elapse); |
1224 | if (r != 0) | |
1225 | return r; | |
cbb76c29 LP |
1226 | |
1227 | return strcmp(a->id, b->id); | |
1228 | } | |
1229 | ||
1230 | static int output_timers_list(struct timer_info *timer_infos, unsigned n) { | |
de9a8fe1 | 1231 | _cleanup_(table_unrefp) Table *table = NULL; |
cbb76c29 | 1232 | struct timer_info *t; |
cbb76c29 | 1233 | const char *on, *off; |
de9a8fe1 | 1234 | int r; |
cbb76c29 LP |
1235 | |
1236 | assert(timer_infos || n == 0); | |
1237 | ||
de9a8fe1 FS |
1238 | table = table_new("next", "left", "last", "passed", "unit", "activates"); |
1239 | if (!table) | |
1240 | return log_oom(); | |
d784e2db | 1241 | |
de9a8fe1 FS |
1242 | table_set_header(table, !arg_no_legend); |
1243 | if (arg_full) | |
1244 | table_set_width(table, 0); | |
cbb76c29 | 1245 | |
0773357a LP |
1246 | (void) table_set_empty_string(table, "-"); |
1247 | ||
cbb76c29 | 1248 | if (n > 0) { |
cbb76c29 | 1249 | for (t = timer_infos; t < timer_infos + n; t++) { |
de9a8fe1 | 1250 | _cleanup_free_ char *j = NULL, *activates = NULL; |
806a37e7 | 1251 | const char *unit; |
d784e2db | 1252 | |
806a37e7 | 1253 | if (t->machine) { |
605405c6 | 1254 | j = strjoin(t->machine, ":", t->id); |
806a37e7 TA |
1255 | if (!j) |
1256 | return log_oom(); | |
1257 | unit = j; | |
1258 | } else | |
1259 | unit = t->id; | |
1260 | ||
de9a8fe1 FS |
1261 | activates = strv_join(t->triggered, ", "); |
1262 | if (!activates) | |
1263 | return log_oom(); | |
cbb76c29 | 1264 | |
de9a8fe1 FS |
1265 | r = table_add_many(table, |
1266 | TABLE_TIMESTAMP, t->next_elapse, | |
1267 | TABLE_TIMESTAMP_RELATIVE, t->next_elapse, | |
1268 | TABLE_TIMESTAMP, t->last_trigger, | |
1269 | TABLE_TIMESTAMP_RELATIVE, t->last_trigger, | |
1270 | TABLE_STRING, unit, | |
1271 | TABLE_STRING, activates); | |
1272 | if (r < 0) | |
1273 | return table_log_add_error(r); | |
cbb76c29 LP |
1274 | } |
1275 | ||
1276 | on = ansi_highlight(); | |
1fc464f6 | 1277 | off = ansi_normal(); |
cbb76c29 LP |
1278 | } else { |
1279 | on = ansi_highlight_red(); | |
1fc464f6 | 1280 | off = ansi_normal(); |
cbb76c29 LP |
1281 | } |
1282 | ||
49cd06fa | 1283 | r = output_table(table); |
de9a8fe1 | 1284 | if (r < 0) |
49cd06fa | 1285 | return r; |
de9a8fe1 | 1286 | |
cbb76c29 | 1287 | if (!arg_no_legend) { |
de9a8fe1 | 1288 | printf("\n%s%u timers listed.%s\n", on, n, off); |
cbb76c29 LP |
1289 | if (!arg_all) |
1290 | printf("Pass --all to see loaded but inactive timers, too.\n"); | |
1291 | } | |
1292 | ||
1293 | return 0; | |
1294 | } | |
1295 | ||
f5080e73 DH |
1296 | static usec_t calc_next_elapse(dual_timestamp *nw, dual_timestamp *next) { |
1297 | usec_t next_elapse; | |
1298 | ||
1299 | assert(nw); | |
1300 | assert(next); | |
1301 | ||
1f65fd49 | 1302 | if (timestamp_is_set(next->monotonic)) { |
f5080e73 DH |
1303 | usec_t converted; |
1304 | ||
1305 | if (next->monotonic > nw->monotonic) | |
1306 | converted = nw->realtime + (next->monotonic - nw->monotonic); | |
1307 | else | |
1308 | converted = nw->realtime - (nw->monotonic - next->monotonic); | |
1309 | ||
1f65fd49 | 1310 | if (timestamp_is_set(next->realtime)) |
f5080e73 DH |
1311 | next_elapse = MIN(converted, next->realtime); |
1312 | else | |
1313 | next_elapse = converted; | |
1314 | ||
1315 | } else | |
1316 | next_elapse = next->realtime; | |
1317 | ||
1318 | return next_elapse; | |
1319 | } | |
1320 | ||
e449de87 | 1321 | static int list_timers(int argc, char *argv[], void *userdata) { |
806a37e7 TA |
1322 | _cleanup_(message_set_freep) Set *replies = NULL; |
1323 | _cleanup_strv_free_ char **machines = NULL; | |
3a77f9fb | 1324 | _cleanup_strv_free_ char **timers_with_suffix = NULL; |
cbb76c29 LP |
1325 | _cleanup_free_ struct timer_info *timer_infos = NULL; |
1326 | _cleanup_free_ UnitInfo *unit_infos = NULL; | |
1327 | struct timer_info *t; | |
1328 | const UnitInfo *u; | |
1329 | size_t size = 0; | |
1823b86e | 1330 | int n, c = 0; |
cbb76c29 | 1331 | dual_timestamp nw; |
4fbd7192 | 1332 | sd_bus *bus; |
1823b86e | 1333 | int r = 0; |
cbb76c29 | 1334 | |
4fbd7192 LP |
1335 | r = acquire_bus(BUS_MANAGER, &bus); |
1336 | if (r < 0) | |
1337 | return r; | |
1338 | ||
0221d68a | 1339 | (void) pager_open(arg_pager_flags); |
d2ad7e1b | 1340 | |
1d56bc09 | 1341 | r = expand_names(bus, strv_skip(argv, 1), ".timer", &timers_with_suffix, NULL); |
3a77f9fb KK |
1342 | if (r < 0) |
1343 | return r; | |
cbb76c29 | 1344 | |
3a77f9fb KK |
1345 | if (argc == 1 || timers_with_suffix) { |
1346 | n = get_unit_list_recursive(bus, timers_with_suffix, &unit_infos, &replies, &machines); | |
1347 | if (n < 0) | |
1348 | return n; | |
cbb76c29 | 1349 | |
3a77f9fb | 1350 | dual_timestamp_get(&nw); |
cbb76c29 | 1351 | |
3a77f9fb KK |
1352 | for (u = unit_infos; u < unit_infos + n; u++) { |
1353 | _cleanup_strv_free_ char **triggered = NULL; | |
1354 | dual_timestamp next = DUAL_TIMESTAMP_NULL; | |
1355 | usec_t m, last = 0; | |
cbb76c29 | 1356 | |
3a77f9fb KK |
1357 | if (!endswith(u->id, ".timer")) |
1358 | continue; | |
cbb76c29 | 1359 | |
3a77f9fb KK |
1360 | r = get_triggered_units(bus, u->unit_path, &triggered); |
1361 | if (r < 0) | |
1362 | goto cleanup; | |
cbb76c29 | 1363 | |
3a77f9fb KK |
1364 | r = get_next_elapse(bus, u->unit_path, &next); |
1365 | if (r < 0) | |
1366 | goto cleanup; | |
d784e2db | 1367 | |
3a77f9fb | 1368 | get_last_trigger(bus, u->unit_path, &last); |
cbb76c29 | 1369 | |
3a77f9fb KK |
1370 | if (!GREEDY_REALLOC(timer_infos, size, c+1)) { |
1371 | r = log_oom(); | |
1372 | goto cleanup; | |
1373 | } | |
f5080e73 | 1374 | |
3a77f9fb | 1375 | m = calc_next_elapse(&nw, &next); |
cbb76c29 | 1376 | |
3a77f9fb KK |
1377 | timer_infos[c++] = (struct timer_info) { |
1378 | .machine = u->machine, | |
1379 | .id = u->id, | |
1380 | .next_elapse = m, | |
1381 | .last_trigger = last, | |
1382 | .triggered = TAKE_PTR(triggered), | |
1383 | }; | |
1384 | } | |
1385 | ||
1386 | typesafe_qsort(timer_infos, c, timer_info_compare); | |
1387 | } | |
cbb76c29 LP |
1388 | |
1389 | output_timers_list(timer_infos, c); | |
1390 | ||
1391 | cleanup: | |
1392 | for (t = timer_infos; t < timer_infos + c; t++) | |
1393 | strv_free(t->triggered); | |
1394 | ||
1395 | return r; | |
1396 | } | |
1397 | ||
93bab288 | 1398 | static int compare_unit_file_list(const UnitFileList *a, const UnitFileList *b) { |
729e3769 | 1399 | const char *d1, *d2; |
729e3769 | 1400 | |
93bab288 YW |
1401 | d1 = strrchr(a->path, '.'); |
1402 | d2 = strrchr(b->path, '.'); | |
729e3769 LP |
1403 | |
1404 | if (d1 && d2) { | |
1405 | int r; | |
1406 | ||
1407 | r = strcasecmp(d1, d2); | |
1408 | if (r != 0) | |
1409 | return r; | |
1410 | } | |
1411 | ||
93bab288 | 1412 | return strcasecmp(basename(a->path), basename(b->path)); |
729e3769 LP |
1413 | } |
1414 | ||
313fe66f | 1415 | static bool output_show_unit_file(const UnitFileList *u, char **states, char **patterns) { |
f8591ee1 LP |
1416 | assert(u); |
1417 | ||
2404701e | 1418 | if (!strv_fnmatch_or_empty(patterns, basename(u->path), FNM_NOESCAPE)) |
d8fba7c6 | 1419 | return false; |
d8fba7c6 | 1420 | |
6c71341a ZJS |
1421 | if (!strv_isempty(arg_types)) { |
1422 | const char *dot; | |
1423 | ||
1424 | dot = strrchr(u->path, '.'); | |
1425 | if (!dot) | |
1426 | return false; | |
1427 | ||
1428 | if (!strv_find(arg_types, dot+1)) | |
1429 | return false; | |
1430 | } | |
1431 | ||
313fe66f | 1432 | if (!strv_isempty(states) && |
1433 | !strv_find(states, unit_file_state_to_string(u->state))) | |
bceccd5e | 1434 | return false; |
fec1530e | 1435 | |
6c71341a | 1436 | return true; |
729e3769 LP |
1437 | } |
1438 | ||
31b8895a ZJS |
1439 | static bool show_preset_for_state(UnitFileState state) { |
1440 | /* Don't show preset state in those unit file states, it'll only confuse users. */ | |
1441 | return !IN_SET(state, | |
1442 | UNIT_FILE_ALIAS, | |
1443 | UNIT_FILE_STATIC, | |
1444 | UNIT_FILE_GENERATED, | |
1445 | UNIT_FILE_TRANSIENT); | |
1446 | } | |
1447 | ||
de9a8fe1 FS |
1448 | static int output_unit_file_list(const UnitFileList *units, unsigned c) { |
1449 | _cleanup_(table_unrefp) Table *table = NULL; | |
8f7b2566 | 1450 | _cleanup_(unit_file_presets_freep) UnitFilePresets presets = {}; |
de9a8fe1 | 1451 | int r; |
729e3769 | 1452 | |
de9a8fe1 FS |
1453 | table = table_new("unit file", "state", "vendor preset"); |
1454 | if (!table) | |
1455 | return log_oom(); | |
1c0a113f | 1456 | |
de9a8fe1 FS |
1457 | table_set_header(table, !arg_no_legend); |
1458 | if (arg_full) | |
1459 | table_set_width(table, 0); | |
729e3769 | 1460 | |
0773357a LP |
1461 | (void) table_set_empty_string(table, "-"); |
1462 | ||
8f7b2566 | 1463 | for (const UnitFileList *u = units; u < units + c; u++) { |
b01c1f30 | 1464 | const char *on_underline = NULL, *on_unit_color = NULL, *id; |
aaa6732d LP |
1465 | bool underline; |
1466 | ||
1467 | underline = u + 1 < units + c && | |
1468 | !streq(unit_type_suffix(u->path), unit_type_suffix((u + 1)->path)); | |
6ae3e62a | 1469 | |
aaa6732d | 1470 | if (underline) |
6ae3e62a | 1471 | on_underline = ansi_underline(); |
729e3769 | 1472 | |
4c315c2c IS |
1473 | if (IN_SET(u->state, |
1474 | UNIT_FILE_MASKED, | |
1475 | UNIT_FILE_MASKED_RUNTIME, | |
1476 | UNIT_FILE_DISABLED, | |
6ae3e62a | 1477 | UNIT_FILE_BAD)) |
b01c1f30 | 1478 | on_unit_color = underline ? ansi_highlight_red_underline() : ansi_highlight_red(); |
15d7ab87 ZJS |
1479 | else if (IN_SET(u->state, |
1480 | UNIT_FILE_ENABLED, | |
1481 | UNIT_FILE_ALIAS)) | |
b01c1f30 | 1482 | on_unit_color = underline ? ansi_highlight_green_underline() : ansi_highlight_green(); |
de9a8fe1 FS |
1483 | else |
1484 | on_unit_color = on_underline; | |
729e3769 | 1485 | |
2b6bf07d | 1486 | id = basename(u->path); |
729e3769 | 1487 | |
de9a8fe1 FS |
1488 | r = table_add_many(table, |
1489 | TABLE_STRING, id, | |
1fab5797 | 1490 | TABLE_SET_BOTH_COLORS, strempty(on_underline), |
de9a8fe1 | 1491 | TABLE_STRING, unit_file_state_to_string(u->state), |
1fab5797 | 1492 | TABLE_SET_BOTH_COLORS, strempty(on_unit_color)); |
31b8895a ZJS |
1493 | if (r < 0) |
1494 | return table_log_add_error(r); | |
1495 | ||
1496 | if (show_preset_for_state(u->state)) { | |
1497 | const char *unit_preset_str, *on_preset_color; | |
1498 | ||
1499 | r = unit_file_query_preset(arg_scope, arg_root, id, &presets); | |
1500 | if (r < 0) { | |
1501 | unit_preset_str = "n/a"; | |
1502 | on_preset_color = underline ? on_underline : ansi_normal(); | |
1503 | } else if (r == 0) { | |
1504 | unit_preset_str = "disabled"; | |
1505 | on_preset_color = underline ? ansi_highlight_red_underline() : ansi_highlight_red(); | |
1506 | } else { | |
1507 | unit_preset_str = "enabled"; | |
1508 | on_preset_color = underline ? ansi_highlight_green_underline() : ansi_highlight_green(); | |
1509 | } | |
1510 | ||
1511 | r = table_add_many(table, | |
1512 | TABLE_STRING, unit_preset_str, | |
1fab5797 | 1513 | TABLE_SET_BOTH_COLORS, strempty(on_preset_color)); |
31b8895a | 1514 | } else |
45d82c3f LP |
1515 | r = table_add_many(table, |
1516 | TABLE_EMPTY, | |
1fab5797 | 1517 | TABLE_SET_BOTH_COLORS, underline ? ansi_grey_underline() : ansi_grey()); |
de9a8fe1 FS |
1518 | if (r < 0) |
1519 | return table_log_add_error(r); | |
729e3769 LP |
1520 | } |
1521 | ||
49cd06fa | 1522 | r = output_table(table); |
de9a8fe1 | 1523 | if (r < 0) |
49cd06fa | 1524 | return r; |
de9a8fe1 | 1525 | |
1c0a113f | 1526 | if (!arg_no_legend) |
0d292f5e | 1527 | printf("\n%u unit files listed.\n", c); |
de9a8fe1 FS |
1528 | |
1529 | return 0; | |
729e3769 LP |
1530 | } |
1531 | ||
e449de87 | 1532 | static int list_unit_files(int argc, char *argv[], void *userdata) { |
4afd3348 | 1533 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
f84190d8 | 1534 | _cleanup_free_ UnitFileList *units = NULL; |
8d5ba5a9 ZJS |
1535 | UnitFileList *unit; |
1536 | size_t size = 0; | |
f459b602 MAP |
1537 | unsigned c = 0; |
1538 | const char *state; | |
1539 | char *path; | |
f84190d8 | 1540 | int r; |
313fe66f | 1541 | bool fallback = false; |
729e3769 | 1542 | |
4fbd7192 | 1543 | if (install_client_side()) { |
729e3769 LP |
1544 | Hashmap *h; |
1545 | UnitFileList *u; | |
1546 | Iterator i; | |
f459b602 | 1547 | unsigned n_units; |
729e3769 | 1548 | |
d5099efc | 1549 | h = hashmap_new(&string_hash_ops); |
0d0f0c50 SL |
1550 | if (!h) |
1551 | return log_oom(); | |
729e3769 | 1552 | |
313fe66f | 1553 | r = unit_file_get_list(arg_scope, arg_root, h, arg_states, strv_skip(argv, 1)); |
729e3769 | 1554 | if (r < 0) { |
8ea913b2 | 1555 | unit_file_list_free(h); |
691395d8 | 1556 | return log_error_errno(r, "Failed to get unit file list: %m"); |
729e3769 LP |
1557 | } |
1558 | ||
1559 | n_units = hashmap_size(h); | |
fdbdf6ec | 1560 | |
0da999fa ZJS |
1561 | units = new(UnitFileList, n_units ?: 1); /* avoid malloc(0) */ |
1562 | if (!units) { | |
729e3769 | 1563 | unit_file_list_free(h); |
0d0f0c50 | 1564 | return log_oom(); |
729e3769 LP |
1565 | } |
1566 | ||
1567 | HASHMAP_FOREACH(u, h, i) { | |
313fe66f | 1568 | if (!output_show_unit_file(u, NULL, NULL)) |
8d5ba5a9 ZJS |
1569 | continue; |
1570 | ||
1571 | units[c++] = *u; | |
729e3769 LP |
1572 | free(u); |
1573 | } | |
1574 | ||
8d5ba5a9 | 1575 | assert(c <= n_units); |
729e3769 | 1576 | hashmap_free(h); |
5f056378 CH |
1577 | |
1578 | r = 0; | |
729e3769 | 1579 | } else { |
313fe66f | 1580 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
4afd3348 | 1581 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
4fbd7192 LP |
1582 | sd_bus *bus; |
1583 | ||
1584 | r = acquire_bus(BUS_MANAGER, &bus); | |
1585 | if (r < 0) | |
1586 | return r; | |
6e646d22 | 1587 | |
8010c205 | 1588 | r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "ListUnitFilesByPatterns"); |
313fe66f | 1589 | if (r < 0) |
1590 | return bus_log_create_error(r); | |
1591 | ||
1592 | r = sd_bus_message_append_strv(m, arg_states); | |
1593 | if (r < 0) | |
1594 | return bus_log_create_error(r); | |
1595 | ||
e9c387c8 KK |
1596 | if (arg_with_dependencies) { |
1597 | _cleanup_strv_free_ char **names_with_deps = NULL; | |
1598 | ||
1599 | r = append_unit_dependencies(bus, strv_skip(argv, 1), &names_with_deps); | |
1600 | if (r < 0) | |
1601 | return log_error_errno(r, "Failed to append unit dependencies: %m"); | |
1602 | ||
1603 | r = sd_bus_message_append_strv(m, names_with_deps); | |
1604 | if (r < 0) | |
1605 | return bus_log_create_error(r); | |
1606 | } else { | |
1607 | r = sd_bus_message_append_strv(m, strv_skip(argv, 1)); | |
1608 | if (r < 0) | |
1609 | return bus_log_create_error(r); | |
1610 | } | |
313fe66f | 1611 | |
1612 | r = sd_bus_call(bus, m, 0, &error, &reply); | |
1613 | if (r < 0 && sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_METHOD)) { | |
1614 | /* Fallback to legacy ListUnitFiles method */ | |
1615 | fallback = true; | |
1616 | log_debug_errno(r, "Failed to list unit files: %s Falling back to ListUnitsFiles method.", bus_error_message(&error, r)); | |
1617 | m = sd_bus_message_unref(m); | |
1618 | sd_bus_error_free(&error); | |
1619 | ||
8010c205 | 1620 | r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "ListUnitFiles"); |
313fe66f | 1621 | if (r < 0) |
1622 | return bus_log_create_error(r); | |
1623 | ||
1624 | r = sd_bus_call(bus, m, 0, &error, &reply); | |
1625 | } | |
691395d8 LP |
1626 | if (r < 0) |
1627 | return log_error_errno(r, "Failed to list unit files: %s", bus_error_message(&error, r)); | |
729e3769 | 1628 | |
f459b602 MAP |
1629 | r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ss)"); |
1630 | if (r < 0) | |
1631 | return bus_log_parse_error(r); | |
729e3769 | 1632 | |
f459b602 | 1633 | while ((r = sd_bus_message_read(reply, "(ss)", &path, &state)) > 0) { |
729e3769 | 1634 | |
f459b602 MAP |
1635 | if (!GREEDY_REALLOC(units, size, c + 1)) |
1636 | return log_oom(); | |
729e3769 | 1637 | |
8d5ba5a9 | 1638 | units[c] = (struct UnitFileList) { |
f459b602 MAP |
1639 | path, |
1640 | unit_file_state_from_string(state) | |
1641 | }; | |
8d5ba5a9 | 1642 | |
313fe66f | 1643 | if (output_show_unit_file(&units[c], |
1644 | fallback ? arg_states : NULL, | |
1645 | fallback ? strv_skip(argv, 1) : NULL)) | |
313cefa1 | 1646 | c++; |
8d5ba5a9 | 1647 | |
729e3769 | 1648 | } |
f459b602 MAP |
1649 | if (r < 0) |
1650 | return bus_log_parse_error(r); | |
1651 | ||
1652 | r = sd_bus_message_exit_container(reply); | |
1653 | if (r < 0) | |
1654 | return bus_log_parse_error(r); | |
729e3769 LP |
1655 | } |
1656 | ||
0221d68a | 1657 | (void) pager_open(arg_pager_flags); |
d2ad7e1b | 1658 | |
93bab288 | 1659 | typesafe_qsort(units, c, compare_unit_file_list); |
de9a8fe1 FS |
1660 | r = output_unit_file_list(units, c); |
1661 | if (r < 0) | |
1662 | return r; | |
729e3769 | 1663 | |
0da999fa | 1664 | if (install_client_side()) |
8d5ba5a9 ZJS |
1665 | for (unit = units; unit < units + c; unit++) |
1666 | free(unit->path); | |
1667 | ||
eeb1542b GS |
1668 | if (c == 0) |
1669 | return -ENOENT; | |
1670 | ||
f84190d8 | 1671 | return 0; |
729e3769 LP |
1672 | } |
1673 | ||
14cb109d | 1674 | static int list_dependencies_print(const char *name, int level, unsigned branches, bool last) { |
55c0b89c | 1675 | _cleanup_free_ char *n = NULL; |
9607d947 | 1676 | size_t max_len = MAX(columns(),20u); |
cbc9fbd1 LP |
1677 | size_t len = 0; |
1678 | int i; | |
55c0b89c | 1679 | |
5d0c05e5 | 1680 | if (!arg_plain) { |
cbc9fbd1 | 1681 | |
5d0c05e5 LN |
1682 | for (i = level - 1; i >= 0; i--) { |
1683 | len += 2; | |
f168c273 | 1684 | if (len > max_len - 3 && !arg_full) { |
5d0c05e5 LN |
1685 | printf("%s...\n",max_len % 2 ? "" : " "); |
1686 | return 0; | |
1687 | } | |
9a6f746f | 1688 | printf("%s", special_glyph(branches & (1 << i) ? SPECIAL_GLYPH_TREE_VERTICAL : SPECIAL_GLYPH_TREE_SPACE)); |
5d0c05e5 | 1689 | } |
55c0b89c | 1690 | len += 2; |
cbc9fbd1 | 1691 | |
f168c273 | 1692 | if (len > max_len - 3 && !arg_full) { |
55c0b89c LN |
1693 | printf("%s...\n",max_len % 2 ? "" : " "); |
1694 | return 0; | |
1695 | } | |
cbc9fbd1 | 1696 | |
9a6f746f | 1697 | printf("%s", special_glyph(last ? SPECIAL_GLYPH_TREE_RIGHT : SPECIAL_GLYPH_TREE_BRANCH)); |
55c0b89c | 1698 | } |
55c0b89c | 1699 | |
9ed794a3 | 1700 | if (arg_full) { |
55c0b89c LN |
1701 | printf("%s\n", name); |
1702 | return 0; | |
1703 | } | |
1704 | ||
1705 | n = ellipsize(name, max_len-len, 100); | |
f168c273 | 1706 | if (!n) |
55c0b89c LN |
1707 | return log_oom(); |
1708 | ||
1709 | printf("%s\n", n); | |
1710 | return 0; | |
1711 | } | |
1712 | ||
93bab288 | 1713 | static int list_dependencies_compare(char * const *a, char * const *b) { |
55c0b89c LN |
1714 | if (unit_name_to_type(*a) == UNIT_TARGET && unit_name_to_type(*b) != UNIT_TARGET) |
1715 | return 1; | |
1716 | if (unit_name_to_type(*a) != UNIT_TARGET && unit_name_to_type(*b) == UNIT_TARGET) | |
1717 | return -1; | |
cbc9fbd1 | 1718 | |
55c0b89c LN |
1719 | return strcasecmp(*a, *b); |
1720 | } | |
1721 | ||
f459b602 MAP |
1722 | static int list_dependencies_one( |
1723 | sd_bus *bus, | |
1724 | const char *name, | |
1725 | int level, | |
1726 | char ***units, | |
14cb109d | 1727 | unsigned branches) { |
f459b602 | 1728 | |
e3e45d4f | 1729 | _cleanup_strv_free_ char **deps = NULL; |
55c0b89c | 1730 | char **c; |
55c0b89c LN |
1731 | int r = 0; |
1732 | ||
cbc9fbd1 LP |
1733 | assert(bus); |
1734 | assert(name); | |
1735 | assert(units); | |
1736 | ||
e3e45d4f SP |
1737 | r = strv_extend(units, name); |
1738 | if (r < 0) | |
55c0b89c LN |
1739 | return log_oom(); |
1740 | ||
1741 | r = list_dependencies_get_dependencies(bus, name, &deps); | |
1742 | if (r < 0) | |
cec7eda5 | 1743 | return r; |
55c0b89c | 1744 | |
93bab288 | 1745 | typesafe_qsort(deps, strv_length(deps), list_dependencies_compare); |
55c0b89c LN |
1746 | |
1747 | STRV_FOREACH(c, deps) { | |
e3e45d4f | 1748 | if (strv_contains(*units, *c)) { |
5d0c05e5 | 1749 | if (!arg_plain) { |
60705040 | 1750 | printf(" "); |
5d0c05e5 LN |
1751 | r = list_dependencies_print("...", level + 1, (branches << 1) | (c[1] == NULL ? 0 : 1), 1); |
1752 | if (r < 0) | |
1753 | return r; | |
1754 | } | |
55c0b89c LN |
1755 | continue; |
1756 | } | |
1757 | ||
250ba664 ZJS |
1758 | if (arg_plain) |
1759 | printf(" "); | |
1760 | else { | |
fa0d5878 | 1761 | UnitActiveState active_state = _UNIT_ACTIVE_STATE_INVALID; |
250ba664 ZJS |
1762 | const char *on; |
1763 | ||
fa0d5878 | 1764 | (void) get_state_one_unit(bus, *c, &active_state); |
baa9ecc1 | 1765 | |
fa0d5878 | 1766 | switch (active_state) { |
baa9ecc1 LP |
1767 | case UNIT_ACTIVE: |
1768 | case UNIT_RELOADING: | |
1769 | case UNIT_ACTIVATING: | |
1770 | on = ansi_highlight_green(); | |
1771 | break; | |
1772 | ||
1773 | case UNIT_INACTIVE: | |
1774 | case UNIT_DEACTIVATING: | |
1775 | on = ansi_normal(); | |
1776 | break; | |
1777 | ||
1778 | default: | |
1779 | on = ansi_highlight_red(); | |
1780 | break; | |
fa0d5878 BR |
1781 | } |
1782 | ||
9a6f746f | 1783 | printf("%s%s%s ", on, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE), ansi_normal()); |
250ba664 | 1784 | } |
dbc2c080 | 1785 | |
55c0b89c | 1786 | r = list_dependencies_print(*c, level, branches, c[1] == NULL); |
cec7eda5 ZJS |
1787 | if (r < 0) |
1788 | return r; | |
55c0b89c LN |
1789 | |
1790 | if (arg_all || unit_name_to_type(*c) == UNIT_TARGET) { | |
e3e45d4f | 1791 | r = list_dependencies_one(bus, *c, level + 1, units, (branches << 1) | (c[1] == NULL ? 0 : 1)); |
f168c273 | 1792 | if (r < 0) |
cec7eda5 | 1793 | return r; |
55c0b89c LN |
1794 | } |
1795 | } | |
f459b602 | 1796 | |
e3e45d4f SP |
1797 | if (!arg_plain) |
1798 | strv_remove(*units, name); | |
f459b602 | 1799 | |
cec7eda5 | 1800 | return 0; |
55c0b89c LN |
1801 | } |
1802 | ||
e449de87 | 1803 | static int list_dependencies(int argc, char *argv[], void *userdata) { |
94c3a838 ZJS |
1804 | _cleanup_strv_free_ char **units = NULL, **done = NULL; |
1805 | char **u, **patterns; | |
4fbd7192 | 1806 | sd_bus *bus; |
7410616c | 1807 | int r; |
55c0b89c | 1808 | |
4fbd7192 LP |
1809 | r = acquire_bus(BUS_MANAGER, &bus); |
1810 | if (r < 0) | |
1811 | return r; | |
1812 | ||
94c3a838 ZJS |
1813 | patterns = strv_skip(argv, 1); |
1814 | if (strv_isempty(patterns)) { | |
1815 | units = strv_new(SPECIAL_DEFAULT_TARGET); | |
1816 | if (!units) | |
1817 | return log_oom(); | |
1818 | } else { | |
1819 | r = expand_names(bus, patterns, NULL, &units, NULL); | |
1820 | if (r < 0) | |
1821 | return log_error_errno(r, "Failed to expand names: %m"); | |
1822 | } | |
1823 | ||
0221d68a | 1824 | (void) pager_open(arg_pager_flags); |
d2ad7e1b | 1825 | |
94c3a838 ZJS |
1826 | STRV_FOREACH(u, units) { |
1827 | if (u != units) | |
1828 | puts(""); | |
1829 | ||
1830 | puts(*u); | |
1831 | r = list_dependencies_one(bus, *u, 0, &done, 0); | |
1832 | if (r < 0) | |
1833 | return r; | |
1834 | } | |
e31165b2 | 1835 | |
94c3a838 | 1836 | return 0; |
55c0b89c LN |
1837 | } |
1838 | ||
0d292f5e LP |
1839 | struct machine_info { |
1840 | bool is_host; | |
1841 | char *name; | |
0d292f5e | 1842 | char *state; |
8fcf784d LP |
1843 | char *control_group; |
1844 | uint32_t n_failed_units; | |
1845 | uint32_t n_jobs; | |
1846 | usec_t timestamp; | |
1847 | }; | |
1848 | ||
1849 | static const struct bus_properties_map machine_info_property_map[] = { | |
1850 | { "SystemState", "s", NULL, offsetof(struct machine_info, state) }, | |
1851 | { "NJobs", "u", NULL, offsetof(struct machine_info, n_jobs) }, | |
1852 | { "NFailedUnits", "u", NULL, offsetof(struct machine_info, n_failed_units) }, | |
1853 | { "ControlGroup", "s", NULL, offsetof(struct machine_info, control_group) }, | |
1854 | { "UserspaceTimestamp", "t", NULL, offsetof(struct machine_info, timestamp) }, | |
1855 | {} | |
0d292f5e LP |
1856 | }; |
1857 | ||
e7e55dbd | 1858 | static void machine_info_clear(struct machine_info *info) { |
f8654baa ZJS |
1859 | assert(info); |
1860 | ||
1861 | free(info->name); | |
1862 | free(info->state); | |
1863 | free(info->control_group); | |
1864 | zero(*info); | |
e7e55dbd DH |
1865 | } |
1866 | ||
0d292f5e LP |
1867 | static void free_machines_list(struct machine_info *machine_infos, int n) { |
1868 | int i; | |
1869 | ||
1870 | if (!machine_infos) | |
1871 | return; | |
1872 | ||
e7e55dbd DH |
1873 | for (i = 0; i < n; i++) |
1874 | machine_info_clear(&machine_infos[i]); | |
0d292f5e LP |
1875 | |
1876 | free(machine_infos); | |
1877 | } | |
1878 | ||
93bab288 YW |
1879 | static int compare_machine_info(const struct machine_info *a, const struct machine_info *b) { |
1880 | int r; | |
0d292f5e | 1881 | |
93bab288 YW |
1882 | r = CMP(b->is_host, a->is_host); |
1883 | if (r != 0) | |
1884 | return r; | |
0d292f5e | 1885 | |
93bab288 | 1886 | return strcasecmp(a->name, b->name); |
0d292f5e LP |
1887 | } |
1888 | ||
1889 | static int get_machine_properties(sd_bus *bus, struct machine_info *mi) { | |
4afd3348 | 1890 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *container = NULL; |
0d292f5e LP |
1891 | int r; |
1892 | ||
1893 | assert(mi); | |
1894 | ||
1895 | if (!bus) { | |
de33fc62 | 1896 | r = sd_bus_open_system_machine(&container, mi->name); |
0d292f5e LP |
1897 | if (r < 0) |
1898 | return r; | |
1899 | ||
1900 | bus = container; | |
1901 | } | |
1902 | ||
a7e4861c YW |
1903 | r = bus_map_all_properties( |
1904 | bus, | |
1905 | "org.freedesktop.systemd1", | |
1906 | "/org/freedesktop/systemd1", | |
1907 | machine_info_property_map, | |
1908 | BUS_MAP_STRDUP, | |
1909 | NULL, | |
1910 | NULL, | |
1911 | mi); | |
0d292f5e LP |
1912 | if (r < 0) |
1913 | return r; | |
1914 | ||
1915 | return 0; | |
1916 | } | |
1917 | ||
1918 | static bool output_show_machine(const char *name, char **patterns) { | |
2404701e | 1919 | return strv_fnmatch_or_empty(patterns, name, FNM_NOESCAPE); |
0d292f5e LP |
1920 | } |
1921 | ||
1922 | static int get_machine_list( | |
1923 | sd_bus *bus, | |
1924 | struct machine_info **_machine_infos, | |
1925 | char **patterns) { | |
1926 | ||
1927 | struct machine_info *machine_infos = NULL; | |
1928 | _cleanup_strv_free_ char **m = NULL; | |
1929 | _cleanup_free_ char *hn = NULL; | |
1930 | size_t sz = 0; | |
1931 | char **i; | |
4c3e8e39 | 1932 | int c = 0, r; |
0d292f5e LP |
1933 | |
1934 | hn = gethostname_malloc(); | |
1935 | if (!hn) | |
1936 | return log_oom(); | |
1937 | ||
1938 | if (output_show_machine(hn, patterns)) { | |
1939 | if (!GREEDY_REALLOC0(machine_infos, sz, c+1)) | |
1940 | return log_oom(); | |
1941 | ||
1942 | machine_infos[c].is_host = true; | |
ae2a15bc | 1943 | machine_infos[c].name = TAKE_PTR(hn); |
0d292f5e | 1944 | |
f9e0eefc | 1945 | (void) get_machine_properties(bus, &machine_infos[c]); |
0d292f5e LP |
1946 | c++; |
1947 | } | |
1948 | ||
4c3e8e39 LP |
1949 | r = sd_get_machine_names(&m); |
1950 | if (r < 0) | |
1951 | return log_error_errno(r, "Failed to get machine list: %m"); | |
1952 | ||
0d292f5e LP |
1953 | STRV_FOREACH(i, m) { |
1954 | _cleanup_free_ char *class = NULL; | |
1955 | ||
1956 | if (!output_show_machine(*i, patterns)) | |
1957 | continue; | |
1958 | ||
1959 | sd_machine_get_class(*i, &class); | |
1960 | if (!streq_ptr(class, "container")) | |
1961 | continue; | |
1962 | ||
1963 | if (!GREEDY_REALLOC0(machine_infos, sz, c+1)) { | |
1964 | free_machines_list(machine_infos, c); | |
1965 | return log_oom(); | |
1966 | } | |
1967 | ||
1968 | machine_infos[c].is_host = false; | |
1969 | machine_infos[c].name = strdup(*i); | |
1970 | if (!machine_infos[c].name) { | |
1971 | free_machines_list(machine_infos, c); | |
1972 | return log_oom(); | |
1973 | } | |
1974 | ||
f9e0eefc | 1975 | (void) get_machine_properties(NULL, &machine_infos[c]); |
0d292f5e LP |
1976 | c++; |
1977 | } | |
1978 | ||
1979 | *_machine_infos = machine_infos; | |
1980 | return c; | |
1981 | } | |
1982 | ||
de9a8fe1 FS |
1983 | static int output_machines_list(struct machine_info *machine_infos, unsigned n) { |
1984 | _cleanup_(table_unrefp) Table *table = NULL; | |
0d292f5e | 1985 | struct machine_info *m; |
aa91d592 | 1986 | bool state_missing = false; |
de9a8fe1 | 1987 | int r; |
0d292f5e LP |
1988 | |
1989 | assert(machine_infos || n == 0); | |
1990 | ||
de9a8fe1 FS |
1991 | table = table_new("", "name", "state", "failed", "jobs"); |
1992 | if (!table) | |
1993 | return log_oom(); | |
0d292f5e | 1994 | |
de9a8fe1 | 1995 | table_set_header(table, !arg_no_legend); |
1cabd2d0 JW |
1996 | if (arg_plain) { |
1997 | /* Hide the 'glyph' column when --plain is requested */ | |
d171e679 FS |
1998 | r = table_hide_column_from_display(table, 0); |
1999 | if (r < 0) | |
2000 | return log_error_errno(r, "Failed to hide column: %m"); | |
2001 | } | |
de9a8fe1 FS |
2002 | if (arg_full) |
2003 | table_set_width(table, 0); | |
0d292f5e | 2004 | |
0773357a LP |
2005 | (void) table_set_empty_string(table, "-"); |
2006 | ||
0d292f5e | 2007 | for (m = machine_infos; m < machine_infos + n; m++) { |
de9a8fe1 FS |
2008 | _cleanup_free_ char *mname = NULL; |
2009 | const char *on_state = "", *on_failed = ""; | |
90c3f79d | 2010 | bool circle = false; |
0d292f5e LP |
2011 | |
2012 | if (streq_ptr(m->state, "degraded")) { | |
2013 | on_state = ansi_highlight_red(); | |
90c3f79d | 2014 | circle = true; |
0d292f5e LP |
2015 | } else if (!streq_ptr(m->state, "running")) { |
2016 | on_state = ansi_highlight_yellow(); | |
90c3f79d LP |
2017 | circle = true; |
2018 | } | |
0d292f5e | 2019 | |
de9a8fe1 | 2020 | if (m->n_failed_units > 0) |
0d292f5e | 2021 | on_failed = ansi_highlight_red(); |
de9a8fe1 FS |
2022 | else |
2023 | on_failed = ""; | |
90c3f79d | 2024 | |
4bea2469 ZJS |
2025 | if (!m->state) |
2026 | state_missing = true; | |
2027 | ||
0d292f5e | 2028 | if (m->is_host) |
de9a8fe1 FS |
2029 | mname = strjoin(strna(m->name), " (host)"); |
2030 | ||
2031 | r = table_add_many(table, | |
2032 | TABLE_STRING, circle ? special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE) : " ", | |
2033 | TABLE_SET_COLOR, on_state, | |
2034 | TABLE_STRING, m->is_host ? mname : strna(m->name), | |
2035 | TABLE_STRING, strna(m->state), | |
2036 | TABLE_SET_COLOR, on_state, | |
2037 | TABLE_UINT32, m->n_failed_units, | |
2038 | TABLE_SET_COLOR, on_failed, | |
2039 | TABLE_UINT32, m->n_jobs); | |
2040 | if (r < 0) | |
2041 | return table_log_add_error(r); | |
0d292f5e LP |
2042 | } |
2043 | ||
49cd06fa | 2044 | r = output_table(table); |
de9a8fe1 | 2045 | if (r < 0) |
49cd06fa | 2046 | return r; |
de9a8fe1 | 2047 | |
4bea2469 ZJS |
2048 | if (!arg_no_legend) { |
2049 | printf("\n"); | |
2050 | if (state_missing && geteuid() != 0) | |
2051 | printf("Notice: some information only available to privileged users was not shown.\n"); | |
2052 | printf("%u machines listed.\n", n); | |
2053 | } | |
de9a8fe1 FS |
2054 | |
2055 | return 0; | |
0d292f5e LP |
2056 | } |
2057 | ||
e449de87 | 2058 | static int list_machines(int argc, char *argv[], void *userdata) { |
0d292f5e | 2059 | struct machine_info *machine_infos = NULL; |
4fbd7192 | 2060 | sd_bus *bus; |
de9a8fe1 | 2061 | int r, rc; |
0d292f5e | 2062 | |
4fbd7192 LP |
2063 | r = acquire_bus(BUS_MANAGER, &bus); |
2064 | if (r < 0) | |
2065 | return r; | |
2066 | ||
e449de87 | 2067 | r = get_machine_list(bus, &machine_infos, strv_skip(argv, 1)); |
0d292f5e LP |
2068 | if (r < 0) |
2069 | return r; | |
2070 | ||
0221d68a | 2071 | (void) pager_open(arg_pager_flags); |
d2ad7e1b | 2072 | |
93bab288 | 2073 | typesafe_qsort(machine_infos, r, compare_machine_info); |
de9a8fe1 | 2074 | rc = output_machines_list(machine_infos, r); |
0d292f5e LP |
2075 | free_machines_list(machine_infos, r); |
2076 | ||
de9a8fe1 | 2077 | return rc; |
0d292f5e LP |
2078 | } |
2079 | ||
5e59431c ZJS |
2080 | static int parse_proc_cmdline_item(const char *key, const char *value, void *data) { |
2081 | char **ret = data; | |
2082 | ||
2083 | if (streq(key, "systemd.unit")) { | |
2084 | if (proc_cmdline_value_missing(key, value)) | |
2085 | return 0; | |
2086 | if (!unit_name_is_valid(value, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE)) | |
2087 | return log_warning("Unit name specified on %s= is not valid, ignoring: %s", key, value); | |
2088 | ||
2089 | return free_and_strdup_warn(ret, key); | |
2090 | ||
2091 | } else if (!value) { | |
2092 | if (runlevel_to_target(key)) | |
2093 | return free_and_strdup_warn(ret, key); | |
2094 | } | |
2095 | ||
2096 | return 0; | |
2097 | } | |
2098 | ||
2099 | static void emit_cmdline_warning(void) { | |
2100 | if (arg_quiet || arg_root) | |
2101 | /* don't bother checking the commandline if we're operating on a container */ | |
2102 | return; | |
2103 | ||
2104 | _cleanup_free_ char *override = NULL; | |
2105 | int r; | |
2106 | ||
2107 | r = proc_cmdline_parse(parse_proc_cmdline_item, &override, 0); | |
2108 | if (r < 0) | |
2109 | log_debug_errno(r, "Failed to parse kernel command line, ignoring: %m"); | |
2110 | if (override) | |
2111 | log_notice("Note: found \"%s\" on the kernel commandline, which overrides the default unit.", | |
2112 | override); | |
2113 | } | |
2114 | ||
12877da2 | 2115 | static int determine_default(char **ret_name) { |
99504dd4 | 2116 | int r; |
99504dd4 | 2117 | |
4fbd7192 | 2118 | if (install_client_side()) { |
12877da2 | 2119 | r = unit_file_get_default(arg_scope, arg_root, ret_name); |
f647962d MS |
2120 | if (r < 0) |
2121 | return log_error_errno(r, "Failed to get default target: %m"); | |
12877da2 | 2122 | return 0; |
99504dd4 | 2123 | |
99504dd4 | 2124 | } else { |
4fbd7192 | 2125 | sd_bus *bus; |
12877da2 ZJS |
2126 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
2127 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
2128 | const char *name; | |
4fbd7192 LP |
2129 | |
2130 | r = acquire_bus(BUS_MANAGER, &bus); | |
2131 | if (r < 0) | |
2132 | return r; | |
6e646d22 | 2133 | |
8010c205 | 2134 | r = bus_call_method(bus, bus_systemd_mgr, "GetDefaultTarget", &error, &reply, NULL); |
691395d8 LP |
2135 | if (r < 0) |
2136 | return log_error_errno(r, "Failed to get default target: %s", bus_error_message(&error, r)); | |
99504dd4 | 2137 | |
12877da2 | 2138 | r = sd_bus_message_read(reply, "s", &name); |
f459b602 MAP |
2139 | if (r < 0) |
2140 | return bus_log_parse_error(r); | |
12877da2 ZJS |
2141 | |
2142 | return free_and_strdup_warn(ret_name, name); | |
99504dd4 | 2143 | } |
12877da2 ZJS |
2144 | } |
2145 | ||
2146 | static int get_default(int argc, char *argv[], void *userdata) { | |
2147 | _cleanup_free_ char *name = NULL; | |
2148 | int r; | |
99504dd4 | 2149 | |
12877da2 ZJS |
2150 | r = determine_default(&name); |
2151 | if (r < 0) | |
2152 | return r; | |
2153 | ||
2154 | printf("%s\n", name); | |
99504dd4 | 2155 | |
5e59431c ZJS |
2156 | emit_cmdline_warning(); |
2157 | ||
f459b602 | 2158 | return 0; |
99504dd4 VP |
2159 | } |
2160 | ||
e449de87 | 2161 | static int set_default(int argc, char *argv[], void *userdata) { |
718db961 | 2162 | _cleanup_free_ char *unit = NULL; |
acc0269c | 2163 | UnitFileChange *changes = NULL; |
da6053d0 | 2164 | size_t n_changes = 0; |
718db961 LP |
2165 | int r; |
2166 | ||
e449de87 LP |
2167 | assert(argc >= 2); |
2168 | assert(argv); | |
2169 | ||
df7c4eb6 ZJS |
2170 | r = unit_name_mangle_with_suffix(argv[1], "set-default", |
2171 | arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN, | |
2172 | ".target", &unit); | |
7410616c LP |
2173 | if (r < 0) |
2174 | return log_error_errno(r, "Failed to mangle unit name: %m"); | |
718db961 | 2175 | |
4fbd7192 | 2176 | if (install_client_side()) { |
b3796dd8 | 2177 | r = unit_file_set_default(arg_scope, UNIT_FILE_FORCE, arg_root, unit, &changes, &n_changes); |
af3d8113 | 2178 | unit_file_dump_changes(r, "set default", changes, n_changes, arg_quiet); |
5f056378 CH |
2179 | |
2180 | if (r > 0) | |
2181 | r = 0; | |
718db961 | 2182 | } else { |
4afd3348 LP |
2183 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
2184 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
4fbd7192 | 2185 | sd_bus *bus; |
718db961 | 2186 | |
8a4b13c5 | 2187 | polkit_agent_open_maybe(); |
6e646d22 | 2188 | |
4fbd7192 LP |
2189 | r = acquire_bus(BUS_MANAGER, &bus); |
2190 | if (r < 0) | |
2191 | return r; | |
2192 | ||
8010c205 | 2193 | r = bus_call_method(bus, bus_systemd_mgr, "SetDefaultTarget", &error, &reply, "sb", unit, 1); |
691395d8 LP |
2194 | if (r < 0) |
2195 | return log_error_errno(r, "Failed to set default target: %s", bus_error_message(&error, r)); | |
718db961 | 2196 | |
acc0269c | 2197 | r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet, &changes, &n_changes); |
718db961 | 2198 | if (r < 0) |
acc0269c | 2199 | goto finish; |
718db961 | 2200 | |
93c941e3 | 2201 | /* Try to reload if enabled */ |
718db961 | 2202 | if (!arg_no_reload) |
e449de87 | 2203 | r = daemon_reload(argc, argv, userdata); |
718db961 LP |
2204 | else |
2205 | r = 0; | |
2206 | } | |
2207 | ||
5e59431c ZJS |
2208 | emit_cmdline_warning(); |
2209 | ||
12877da2 ZJS |
2210 | if (!arg_quiet) { |
2211 | _cleanup_free_ char *final = NULL; | |
2212 | ||
2213 | r = determine_default(&final); | |
2214 | if (r < 0) | |
2215 | return r; | |
2216 | ||
2217 | if (!streq(final, unit)) | |
2218 | log_notice("Note: \"%s\" is the default unit (possibly a runtime override).", final); | |
2219 | } | |
2220 | ||
acc0269c CH |
2221 | finish: |
2222 | unit_file_changes_free(changes, n_changes); | |
2223 | ||
718db961 LP |
2224 | return r; |
2225 | } | |
2226 | ||
de9a8fe1 | 2227 | static int output_waiting_jobs(sd_bus *bus, Table *table, uint32_t id, const char *method, const char *prefix) { |
82948f6c LP |
2228 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
2229 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
31d99bd1 | 2230 | const char *name, *type; |
82948f6c LP |
2231 | uint32_t other_id; |
2232 | int r; | |
2233 | ||
2234 | assert(bus); | |
2235 | ||
8010c205 | 2236 | r = bus_call_method(bus, bus_systemd_mgr, method, &error, &reply, "u", id); |
76d8ca22 ZJS |
2237 | if (r < 0) |
2238 | return log_debug_errno(r, "Failed to get waiting jobs for job %" PRIu32, id); | |
82948f6c LP |
2239 | |
2240 | r = sd_bus_message_enter_container(reply, 'a', "(usssoo)"); | |
76d8ca22 ZJS |
2241 | if (r < 0) |
2242 | return bus_log_parse_error(r); | |
82948f6c | 2243 | |
de9a8fe1 FS |
2244 | while ((r = sd_bus_message_read(reply, "(usssoo)", &other_id, &name, &type, NULL, NULL, NULL)) > 0) { |
2245 | _cleanup_free_ char *row = NULL; | |
2246 | int rc; | |
2247 | ||
2248 | if (asprintf(&row, "%s %u (%s/%s)", prefix, other_id, name, type) < 0) | |
2249 | return log_oom(); | |
2250 | ||
2251 | rc = table_add_many(table, | |
2252 | TABLE_STRING, special_glyph(SPECIAL_GLYPH_TREE_RIGHT), | |
2253 | TABLE_STRING, row, | |
2254 | TABLE_EMPTY, | |
2255 | TABLE_EMPTY); | |
2256 | if (rc < 0) | |
2257 | return table_log_add_error(r); | |
2258 | } | |
2259 | ||
76d8ca22 ZJS |
2260 | if (r < 0) |
2261 | return bus_log_parse_error(r); | |
82948f6c LP |
2262 | |
2263 | r = sd_bus_message_exit_container(reply); | |
76d8ca22 ZJS |
2264 | if (r < 0) |
2265 | return bus_log_parse_error(r); | |
2266 | ||
2267 | return 0; | |
82948f6c LP |
2268 | } |
2269 | ||
75add28a ZJS |
2270 | struct job_info { |
2271 | uint32_t id; | |
f459b602 | 2272 | const char *name, *type, *state; |
75add28a ZJS |
2273 | }; |
2274 | ||
de9a8fe1 FS |
2275 | static int output_jobs_list(sd_bus *bus, const struct job_info* jobs, unsigned n, bool skipped) { |
2276 | _cleanup_(table_unrefp) Table *table = NULL; | |
f459b602 | 2277 | const struct job_info *j; |
75add28a | 2278 | const char *on, *off; |
de9a8fe1 | 2279 | int r; |
75add28a ZJS |
2280 | |
2281 | assert(n == 0 || jobs); | |
2282 | ||
2283 | if (n == 0) { | |
250ba664 ZJS |
2284 | if (!arg_no_legend) { |
2285 | on = ansi_highlight_green(); | |
1fc464f6 | 2286 | off = ansi_normal(); |
75add28a | 2287 | |
250ba664 ZJS |
2288 | printf("%sNo jobs %s.%s\n", on, skipped ? "listed" : "running", off); |
2289 | } | |
de9a8fe1 | 2290 | return 0; |
75add28a ZJS |
2291 | } |
2292 | ||
0221d68a | 2293 | (void) pager_open(arg_pager_flags); |
75add28a | 2294 | |
de9a8fe1 FS |
2295 | table = table_new("job", "unit", "type", "state"); |
2296 | if (!table) | |
2297 | return log_oom(); | |
75add28a | 2298 | |
de9a8fe1 FS |
2299 | table_set_header(table, !arg_no_legend); |
2300 | if (arg_full) | |
2301 | table_set_width(table, 0); | |
75add28a | 2302 | |
0773357a LP |
2303 | (void) table_set_empty_string(table, "-"); |
2304 | ||
f459b602 | 2305 | for (j = jobs; j < jobs + n; j++) { |
de9a8fe1 | 2306 | if (streq(j->state, "running")) |
f459b602 | 2307 | on = ansi_highlight(); |
de9a8fe1 FS |
2308 | else |
2309 | on = ""; | |
2310 | ||
de9a8fe1 FS |
2311 | r = table_add_many(table, |
2312 | TABLE_UINT, j->id, | |
2313 | TABLE_STRING, j->name, | |
2314 | TABLE_SET_COLOR, on, | |
2315 | TABLE_STRING, j->type, | |
2316 | TABLE_STRING, j->state, | |
2317 | TABLE_SET_COLOR, on); | |
2318 | if (r < 0) | |
2319 | return table_log_add_error(r); | |
82948f6c LP |
2320 | |
2321 | if (arg_jobs_after) | |
de9a8fe1 | 2322 | output_waiting_jobs(bus, table, j->id, "GetJobAfter", "\twaiting for job"); |
82948f6c | 2323 | if (arg_jobs_before) |
de9a8fe1 | 2324 | output_waiting_jobs(bus, table, j->id, "GetJobBefore", "\tblocking job"); |
75add28a ZJS |
2325 | } |
2326 | ||
de9a8fe1 FS |
2327 | r = table_print(table, NULL); |
2328 | if (r < 0) | |
2329 | return log_error_errno(r, "Failed to print the table: %m"); | |
2330 | ||
6ce774fd MM |
2331 | if (!arg_no_legend) { |
2332 | on = ansi_highlight(); | |
1fc464f6 | 2333 | off = ansi_normal(); |
75add28a | 2334 | |
6ce774fd MM |
2335 | printf("\n%s%u jobs listed%s.\n", on, n, off); |
2336 | } | |
de9a8fe1 FS |
2337 | |
2338 | return 0; | |
75add28a ZJS |
2339 | } |
2340 | ||
d8fba7c6 | 2341 | static bool output_show_job(struct job_info *job, char **patterns) { |
2404701e | 2342 | return strv_fnmatch_or_empty(patterns, job->name, FNM_NOESCAPE); |
d8fba7c6 ZJS |
2343 | } |
2344 | ||
e449de87 | 2345 | static int list_jobs(int argc, char *argv[], void *userdata) { |
4afd3348 LP |
2346 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
2347 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
f459b602 | 2348 | _cleanup_free_ struct job_info *jobs = NULL; |
31d99bd1 YW |
2349 | const char *name, *type, *state; |
2350 | bool skipped = false; | |
f459b602 MAP |
2351 | size_t size = 0; |
2352 | unsigned c = 0; | |
4fbd7192 | 2353 | sd_bus *bus; |
f459b602 | 2354 | uint32_t id; |
f84190d8 | 2355 | int r; |
ec14911e | 2356 | |
4fbd7192 LP |
2357 | r = acquire_bus(BUS_MANAGER, &bus); |
2358 | if (r < 0) | |
2359 | return r; | |
2360 | ||
8010c205 | 2361 | r = bus_call_method(bus, bus_systemd_mgr, "ListJobs", &error, &reply, NULL); |
691395d8 LP |
2362 | if (r < 0) |
2363 | return log_error_errno(r, "Failed to list jobs: %s", bus_error_message(&error, r)); | |
7e4249b9 | 2364 | |
f459b602 MAP |
2365 | r = sd_bus_message_enter_container(reply, 'a', "(usssoo)"); |
2366 | if (r < 0) | |
2367 | return bus_log_parse_error(r); | |
7e4249b9 | 2368 | |
31d99bd1 | 2369 | while ((r = sd_bus_message_read(reply, "(usssoo)", &id, &name, &type, &state, NULL, NULL)) > 0) { |
d8fba7c6 ZJS |
2370 | struct job_info job = { id, name, type, state }; |
2371 | ||
e449de87 | 2372 | if (!output_show_job(&job, strv_skip(argv, 1))) { |
d8fba7c6 ZJS |
2373 | skipped = true; |
2374 | continue; | |
2375 | } | |
8fe914ec | 2376 | |
f459b602 MAP |
2377 | if (!GREEDY_REALLOC(jobs, size, c + 1)) |
2378 | return log_oom(); | |
7e4249b9 | 2379 | |
d8fba7c6 | 2380 | jobs[c++] = job; |
7e4249b9 | 2381 | } |
f459b602 MAP |
2382 | if (r < 0) |
2383 | return bus_log_parse_error(r); | |
7e4249b9 | 2384 | |
f459b602 MAP |
2385 | r = sd_bus_message_exit_container(reply); |
2386 | if (r < 0) | |
2387 | return bus_log_parse_error(r); | |
f73e33d9 | 2388 | |
0221d68a | 2389 | (void) pager_open(arg_pager_flags); |
d2ad7e1b | 2390 | |
de9a8fe1 | 2391 | return output_jobs_list(bus, jobs, c, skipped); |
7e4249b9 LP |
2392 | } |
2393 | ||
e449de87 | 2394 | static int cancel_job(int argc, char *argv[], void *userdata) { |
4fbd7192 | 2395 | sd_bus *bus; |
729e3769 | 2396 | char **name; |
342641fb | 2397 | int r = 0; |
7e4249b9 | 2398 | |
e449de87 | 2399 | if (argc <= 1) |
2853b60a | 2400 | return trivial_method(argc, argv, userdata); |
ee5762e3 | 2401 | |
4fbd7192 LP |
2402 | r = acquire_bus(BUS_MANAGER, &bus); |
2403 | if (r < 0) | |
2404 | return r; | |
2405 | ||
8a4b13c5 | 2406 | polkit_agent_open_maybe(); |
d2ad7e1b | 2407 | |
e449de87 | 2408 | STRV_FOREACH(name, strv_skip(argv, 1)) { |
4afd3348 | 2409 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
5dd9014f | 2410 | uint32_t id; |
342641fb | 2411 | int q; |
7e4249b9 | 2412 | |
342641fb | 2413 | q = safe_atou32(*name, &id); |
f647962d MS |
2414 | if (q < 0) |
2415 | return log_error_errno(q, "Failed to parse job id \"%s\": %m", *name); | |
7e4249b9 | 2416 | |
8010c205 | 2417 | q = bus_call_method(bus, bus_systemd_mgr, "CancelJob", &error, NULL, "u", id); |
342641fb | 2418 | if (q < 0) { |
691395d8 | 2419 | log_error_errno(q, "Failed to cancel job %"PRIu32": %s", id, bus_error_message(&error, q)); |
342641fb LP |
2420 | if (r == 0) |
2421 | r = q; | |
f459b602 | 2422 | } |
7e4249b9 LP |
2423 | } |
2424 | ||
342641fb | 2425 | return r; |
7e4249b9 LP |
2426 | } |
2427 | ||
f459b602 | 2428 | static int need_daemon_reload(sd_bus *bus, const char *unit) { |
4afd3348 | 2429 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
f459b602 MAP |
2430 | const char *path; |
2431 | int b, r; | |
94c01aeb | 2432 | |
f459b602 MAP |
2433 | /* We ignore all errors here, since this is used to show a |
2434 | * warning only */ | |
45fb0699 | 2435 | |
f459b602 MAP |
2436 | /* We don't use unit_dbus_path_from_name() directly since we |
2437 | * don't want to load the unit if it isn't loaded. */ | |
f84190d8 | 2438 | |
8010c205 | 2439 | r = bus_call_method(bus, bus_systemd_mgr, "GetUnit", NULL, &reply, "s", unit); |
f84190d8 LP |
2440 | if (r < 0) |
2441 | return r; | |
45fb0699 | 2442 | |
f459b602 MAP |
2443 | r = sd_bus_message_read(reply, "o", &path); |
2444 | if (r < 0) | |
2445 | return r; | |
f84190d8 | 2446 | |
f459b602 | 2447 | r = sd_bus_get_property_trivial( |
f22f08cd | 2448 | bus, |
b0193f1c LP |
2449 | "org.freedesktop.systemd1", |
2450 | path, | |
f459b602 MAP |
2451 | "org.freedesktop.systemd1.Unit", |
2452 | "NeedDaemonReload", | |
2453 | NULL, | |
2454 | 'b', &b); | |
f84190d8 LP |
2455 | if (r < 0) |
2456 | return r; | |
45fb0699 | 2457 | |
45fb0699 LP |
2458 | return b; |
2459 | } | |
2460 | ||
3f36991e | 2461 | static void warn_unit_file_changed(const char *name) { |
87ec20ef LP |
2462 | assert(name); |
2463 | ||
e9f4aabd | 2464 | 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 | 2465 | ansi_highlight_red(), |
1fc464f6 | 2466 | ansi_normal(), |
3f36991e ZJS |
2467 | name, |
2468 | arg_scope == UNIT_FILE_SYSTEM ? "" : " --user"); | |
2469 | } | |
2470 | ||
c45e7e0c | 2471 | static int unit_file_find_path(LookupPaths *lp, const char *unit_name, char **ret_unit_path) { |
33f6c497 ZJS |
2472 | char **p; |
2473 | ||
2474 | assert(lp); | |
2475 | assert(unit_name); | |
33f6c497 | 2476 | |
a3c4eb07 | 2477 | STRV_FOREACH(p, lp->search_path) { |
c6dd36b6 FS |
2478 | _cleanup_free_ char *path = NULL, *lpath = NULL; |
2479 | int r; | |
33f6c497 | 2480 | |
62a85ee0 | 2481 | path = path_join(*p, unit_name); |
33f6c497 ZJS |
2482 | if (!path) |
2483 | return log_oom(); | |
2484 | ||
a5648b80 | 2485 | r = chase_symlinks(path, arg_root, 0, &lpath, NULL); |
c6dd36b6 FS |
2486 | if (r == -ENOENT) |
2487 | continue; | |
2488 | if (r == -ENOMEM) | |
2489 | return log_oom(); | |
2490 | if (r < 0) | |
86f004fb | 2491 | return log_error_errno(r, "Failed to access path \"%s\": %m", path); |
c6dd36b6 | 2492 | |
c45e7e0c LP |
2493 | if (ret_unit_path) |
2494 | *ret_unit_path = TAKE_PTR(lpath); | |
ae2a15bc | 2495 | |
c6dd36b6 | 2496 | return 1; |
33f6c497 ZJS |
2497 | } |
2498 | ||
f67cb270 LP |
2499 | if (ret_unit_path) |
2500 | *ret_unit_path = NULL; | |
2501 | ||
33f6c497 ZJS |
2502 | return 0; |
2503 | } | |
2504 | ||
173471b7 ZJS |
2505 | static int unit_find_template_path( |
2506 | const char *unit_name, | |
2507 | LookupPaths *lp, | |
b03677e2 LP |
2508 | char **ret_fragment_path, |
2509 | char **ret_template) { | |
173471b7 | 2510 | |
b03677e2 | 2511 | _cleanup_free_ char *t = NULL, *f = NULL; |
173471b7 ZJS |
2512 | int r; |
2513 | ||
2514 | /* Returns 1 if a fragment was found, 0 if not found, negative on error. */ | |
2515 | ||
b03677e2 LP |
2516 | r = unit_file_find_path(lp, unit_name, &f); |
2517 | if (r < 0) | |
2518 | return r; | |
2519 | if (r > 0) { | |
2520 | if (ret_fragment_path) | |
2521 | *ret_fragment_path = TAKE_PTR(f); | |
2522 | if (ret_template) | |
2523 | *ret_template = NULL; | |
2524 | return r; /* found a real unit */ | |
2525 | } | |
2526 | ||
2527 | r = unit_name_template(unit_name, &t); | |
2528 | if (r == -EINVAL) { | |
2529 | if (ret_fragment_path) | |
2530 | *ret_fragment_path = NULL; | |
2531 | if (ret_template) | |
2532 | *ret_template = NULL; | |
173471b7 | 2533 | |
173471b7 | 2534 | return 0; /* not a template, does not exist */ |
b03677e2 | 2535 | } |
173471b7 ZJS |
2536 | if (r < 0) |
2537 | return log_error_errno(r, "Failed to determine template name: %m"); | |
2538 | ||
b03677e2 | 2539 | r = unit_file_find_path(lp, t, ret_fragment_path); |
173471b7 ZJS |
2540 | if (r < 0) |
2541 | return r; | |
2542 | ||
b03677e2 LP |
2543 | if (ret_template) |
2544 | *ret_template = r > 0 ? TAKE_PTR(t) : NULL; | |
ae2a15bc | 2545 | |
173471b7 ZJS |
2546 | return r; |
2547 | } | |
2548 | ||
6e646d22 LP |
2549 | static int unit_find_paths( |
2550 | sd_bus *bus, | |
2551 | const char *unit_name, | |
6e646d22 | 2552 | LookupPaths *lp, |
02a126a3 LP |
2553 | bool force_client_side, |
2554 | char **ret_fragment_path, | |
2555 | char ***ret_dropin_paths) { | |
dab2bce8 | 2556 | |
dab2bce8 | 2557 | _cleanup_strv_free_ char **dropins = NULL; |
02a126a3 | 2558 | _cleanup_free_ char *path = NULL; |
33f6c497 ZJS |
2559 | int r; |
2560 | ||
ad2a0358 | 2561 | /** |
e04df06f ZJS |
2562 | * Finds where the unit is defined on disk. Returns 0 if the unit is not found. Returns 1 if it is |
2563 | * found, and sets: | |
02a126a3 | 2564 | * - the path to the unit in *ret_frament_path, if it exists on disk, |
e04df06f ZJS |
2565 | * - and a strv of existing drop-ins in *ret_dropin_paths, if the arg is not NULL and any dropins |
2566 | * were found. | |
02a126a3 | 2567 | * |
e04df06f ZJS |
2568 | * Returns -ERFKILL if the unit is masked, and -EKEYREJECTED if the unit file could not be loaded for |
2569 | * some reason (the latter only applies if we are going through the service manager). | |
ad2a0358 ZJS |
2570 | */ |
2571 | ||
33f6c497 | 2572 | assert(unit_name); |
02a126a3 | 2573 | assert(ret_fragment_path); |
33f6c497 ZJS |
2574 | assert(lp); |
2575 | ||
02a126a3 LP |
2576 | /* Go via the bus to acquire the path, unless we are explicitly told not to, or when the unit name is a template */ |
2577 | if (!force_client_side && | |
2578 | !install_client_side() && | |
2579 | !unit_name_is_valid(unit_name, UNIT_NAME_TEMPLATE)) { | |
4afd3348 | 2580 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
6176e89c | 2581 | _cleanup_free_ char *load_state = NULL, *dbus_path = NULL; |
33f6c497 | 2582 | |
6176e89c ZJS |
2583 | dbus_path = unit_dbus_path_from_name(unit_name); |
2584 | if (!dbus_path) | |
33f6c497 ZJS |
2585 | return log_oom(); |
2586 | ||
02a126a3 LP |
2587 | r = sd_bus_get_property_string( |
2588 | bus, | |
2589 | "org.freedesktop.systemd1", | |
6176e89c | 2590 | dbus_path, |
02a126a3 LP |
2591 | "org.freedesktop.systemd1.Unit", |
2592 | "LoadState", | |
2593 | &error, | |
2594 | &load_state); | |
2595 | if (r < 0) | |
2596 | return log_error_errno(r, "Failed to get LoadState: %s", bus_error_message(&error, r)); | |
2597 | ||
2598 | if (streq(load_state, "masked")) | |
2599 | return -ERFKILL; | |
2600 | if (streq(load_state, "not-found")) { | |
2601 | r = 0; | |
2602 | goto not_found; | |
2603 | } | |
e04df06f | 2604 | if (!STR_IN_SET(load_state, "loaded", "bad-setting")) |
02a126a3 LP |
2605 | return -EKEYREJECTED; |
2606 | ||
33f6c497 ZJS |
2607 | r = sd_bus_get_property_string( |
2608 | bus, | |
2609 | "org.freedesktop.systemd1", | |
6176e89c | 2610 | dbus_path, |
33f6c497 ZJS |
2611 | "org.freedesktop.systemd1.Unit", |
2612 | "FragmentPath", | |
2613 | &error, | |
ad2a0358 ZJS |
2614 | &path); |
2615 | if (r < 0) | |
2616 | return log_error_errno(r, "Failed to get FragmentPath: %s", bus_error_message(&error, r)); | |
2617 | ||
02a126a3 | 2618 | if (ret_dropin_paths) { |
dab2bce8 IS |
2619 | r = sd_bus_get_property_strv( |
2620 | bus, | |
2621 | "org.freedesktop.systemd1", | |
6176e89c | 2622 | dbus_path, |
dab2bce8 IS |
2623 | "org.freedesktop.systemd1.Unit", |
2624 | "DropInPaths", | |
2625 | &error, | |
2626 | &dropins); | |
2627 | if (r < 0) | |
2628 | return log_error_errno(r, "Failed to get DropInPaths: %s", bus_error_message(&error, r)); | |
33f6c497 | 2629 | } |
ad2a0358 | 2630 | } else { |
e8630e69 ZJS |
2631 | const char *_path; |
2632 | _cleanup_set_free_free_ Set *names = NULL; | |
33f6c497 | 2633 | |
e8630e69 | 2634 | if (!cached_name_map) { |
91e0ee5f | 2635 | r = unit_file_build_name_map(lp, NULL, &cached_id_map, &cached_name_map, NULL); |
e8630e69 ZJS |
2636 | if (r < 0) |
2637 | return r; | |
2638 | } | |
33f6c497 | 2639 | |
e8630e69 | 2640 | r = unit_file_find_fragment(cached_id_map, cached_name_map, unit_name, &_path, &names); |
ad2a0358 ZJS |
2641 | if (r < 0) |
2642 | return r; | |
6483ad89 | 2643 | |
e8630e69 ZJS |
2644 | if (_path) { |
2645 | path = strdup(_path); | |
2646 | if (!path) | |
2647 | return log_oom(); | |
2648 | } | |
c6dd36b6 | 2649 | |
02a126a3 | 2650 | if (ret_dropin_paths) { |
043fdc40 ZJS |
2651 | r = unit_file_find_dropin_paths(arg_root, lp->search_path, NULL, |
2652 | ".d", ".conf", | |
4562c355 | 2653 | NULL, names, &dropins); |
dab2bce8 IS |
2654 | if (r < 0) |
2655 | return r; | |
2656 | } | |
2657 | } | |
2658 | ||
6176e89c ZJS |
2659 | if (isempty(path)) { |
2660 | *ret_fragment_path = NULL; | |
02a126a3 | 2661 | r = 0; |
6176e89c | 2662 | } else { |
02a126a3 | 2663 | *ret_fragment_path = TAKE_PTR(path); |
dab2bce8 | 2664 | r = 1; |
6176e89c | 2665 | } |
dab2bce8 | 2666 | |
02a126a3 LP |
2667 | if (ret_dropin_paths) { |
2668 | if (!strv_isempty(dropins)) { | |
2669 | *ret_dropin_paths = TAKE_PTR(dropins); | |
2670 | r = 1; | |
2671 | } else | |
2672 | *ret_dropin_paths = NULL; | |
33f6c497 | 2673 | } |
02a126a3 | 2674 | |
6483ad89 | 2675 | not_found: |
39c38ce1 | 2676 | if (r == 0 && !arg_force) |
b5e6a600 IS |
2677 | log_error("No files found for %s.", unit_name); |
2678 | ||
ad2a0358 | 2679 | return r; |
33f6c497 ZJS |
2680 | } |
2681 | ||
fa0d5878 | 2682 | static int get_state_one_unit(sd_bus *bus, const char *name, UnitActiveState *active_state) { |
b430f85f | 2683 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
6176e89c | 2684 | _cleanup_free_ char *buf = NULL, *dbus_path = NULL; |
fa0d5878 | 2685 | UnitActiveState state; |
f22f08cd | 2686 | int r; |
701cdcb9 | 2687 | |
31be1221 | 2688 | assert(name); |
fa0d5878 | 2689 | assert(active_state); |
701cdcb9 | 2690 | |
6176e89c ZJS |
2691 | dbus_path = unit_dbus_path_from_name(name); |
2692 | if (!dbus_path) | |
3d083b22 LP |
2693 | return log_oom(); |
2694 | ||
2695 | r = sd_bus_get_property_string( | |
f22f08cd SP |
2696 | bus, |
2697 | "org.freedesktop.systemd1", | |
6176e89c | 2698 | dbus_path, |
3d083b22 LP |
2699 | "org.freedesktop.systemd1.Unit", |
2700 | "ActiveState", | |
b430f85f | 2701 | &error, |
3d083b22 LP |
2702 | &buf); |
2703 | if (r < 0) | |
2704 | return log_error_errno(r, "Failed to retrieve unit state: %s", bus_error_message(&error, r)); | |
b430f85f | 2705 | |
3d083b22 | 2706 | state = unit_active_state_from_string(buf); |
2f50e59f LP |
2707 | if (state < 0) |
2708 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid unit state '%s' for: %s", buf, name); | |
701cdcb9 | 2709 | |
fa0d5878 BR |
2710 | *active_state = state; |
2711 | return 0; | |
701cdcb9 MS |
2712 | } |
2713 | ||
86f004fb | 2714 | static int unit_is_masked(sd_bus *bus, LookupPaths *lp, const char *name) { |
bd062910 | 2715 | _cleanup_free_ char *load_state = NULL; |
f22f08cd | 2716 | int r; |
701cdcb9 | 2717 | |
86f004fb ZJS |
2718 | if (unit_name_is_valid(name, UNIT_NAME_TEMPLATE)) { |
2719 | _cleanup_free_ char *path = NULL; | |
2720 | ||
2721 | /* A template cannot be loaded, but it can be still masked, so | |
2722 | * we need to use a different method. */ | |
2723 | ||
2724 | r = unit_file_find_path(lp, name, &path); | |
2725 | if (r < 0) | |
2726 | return r; | |
2727 | if (r == 0) | |
2728 | return false; | |
2729 | return null_or_empty_path(path); | |
2730 | } | |
2731 | ||
bd062910 | 2732 | r = unit_load_state(bus, name, &load_state); |
691395d8 | 2733 | if (r < 0) |
bd062910 | 2734 | return r; |
d0a5cdb2 | 2735 | |
9d9dd746 ZJS |
2736 | return streq(load_state, "masked"); |
2737 | } | |
2738 | ||
2739 | static int check_triggering_units(sd_bus *bus, const char *name) { | |
2740 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
6176e89c | 2741 | _cleanup_free_ char *n = NULL, *dbus_path = NULL, *load_state = NULL; |
9d9dd746 ZJS |
2742 | _cleanup_strv_free_ char **triggered_by = NULL; |
2743 | bool print_warning_label = true; | |
2744 | UnitActiveState active_state; | |
2745 | char **i; | |
2746 | int r; | |
2747 | ||
2748 | r = unit_name_mangle(name, 0, &n); | |
2749 | if (r < 0) | |
2750 | return log_error_errno(r, "Failed to mangle unit name: %m"); | |
2751 | ||
86f004fb ZJS |
2752 | r = unit_load_state(bus, name, &load_state); |
2753 | if (r < 0) | |
2754 | return r; | |
2755 | ||
2756 | if (streq(load_state, "masked")) | |
2757 | return 0; | |
9d9dd746 | 2758 | |
6176e89c ZJS |
2759 | dbus_path = unit_dbus_path_from_name(n); |
2760 | if (!dbus_path) | |
9d9dd746 | 2761 | return log_oom(); |
701cdcb9 | 2762 | |
f459b602 MAP |
2763 | r = sd_bus_get_property_strv( |
2764 | bus, | |
2765 | "org.freedesktop.systemd1", | |
6176e89c | 2766 | dbus_path, |
f459b602 MAP |
2767 | "org.freedesktop.systemd1.Unit", |
2768 | "TriggeredBy", | |
2769 | &error, | |
2770 | &triggered_by); | |
691395d8 LP |
2771 | if (r < 0) |
2772 | return log_error_errno(r, "Failed to get triggered by array of %s: %s", n, bus_error_message(&error, r)); | |
701cdcb9 | 2773 | |
f459b602 | 2774 | STRV_FOREACH(i, triggered_by) { |
fa0d5878 | 2775 | r = get_state_one_unit(bus, *i, &active_state); |
f647962d | 2776 | if (r < 0) |
fa0d5878 | 2777 | return r; |
701cdcb9 | 2778 | |
fa0d5878 | 2779 | if (!IN_SET(active_state, UNIT_ACTIVE, UNIT_RELOADING)) |
f459b602 | 2780 | continue; |
60f9ba0b | 2781 | |
f459b602 MAP |
2782 | if (print_warning_label) { |
2783 | log_warning("Warning: Stopping %s, but it can still be activated by:", n); | |
2784 | print_warning_label = false; | |
701cdcb9 | 2785 | } |
1c291cf3 | 2786 | |
f459b602 | 2787 | log_warning(" %s", *i); |
701cdcb9 | 2788 | } |
f459b602 MAP |
2789 | |
2790 | return 0; | |
701cdcb9 MS |
2791 | } |
2792 | ||
2fc9a280 | 2793 | static const struct { |
85d9b598 LP |
2794 | const char *verb; /* systemctl verb */ |
2795 | const char *method; /* Name of the specific D-Bus method */ | |
2796 | const char *job_type; /* Job type when passing to the generic EnqueueUnitJob() method */ | |
2fc9a280 | 2797 | } unit_actions[] = { |
85d9b598 LP |
2798 | { "start", "StartUnit", "start" }, |
2799 | { "stop", "StopUnit", "stop" }, | |
2800 | { "condstop", "StopUnit", "stop" }, /* legacy alias */ | |
2801 | { "reload", "ReloadUnit", "reload" }, | |
2802 | { "restart", "RestartUnit", "restart" }, | |
2803 | { "try-restart", "TryRestartUnit", "try-restart" }, | |
2804 | { "condrestart", "TryRestartUnit", "try-restart" }, /* legacy alias */ | |
2805 | { "reload-or-restart", "ReloadOrRestartUnit", "reload-or-restart" }, | |
2806 | { "try-reload-or-restart", "ReloadOrTryRestartUnit", "reload-or-try-restart" }, | |
2807 | { "reload-or-try-restart", "ReloadOrTryRestartUnit", "reload-or-try-restart" }, /* legacy alias */ | |
2808 | { "condreload", "ReloadOrTryRestartUnit", "reload-or-try-restart" }, /* legacy alias */ | |
2809 | { "force-reload", "ReloadOrTryRestartUnit", "reload-or-try-restart" }, /* legacy alias */ | |
2fc9a280 LP |
2810 | }; |
2811 | ||
39602c39 | 2812 | static const char *verb_to_method(const char *verb) { |
85d9b598 | 2813 | size_t i; |
39602c39 TA |
2814 | |
2815 | for (i = 0; i < ELEMENTSOF(unit_actions); i++) | |
2816 | if (streq_ptr(unit_actions[i].verb, verb)) | |
2817 | return unit_actions[i].method; | |
2818 | ||
2819 | return "StartUnit"; | |
2820 | } | |
2821 | ||
85d9b598 LP |
2822 | static const char *verb_to_job_type(const char *verb) { |
2823 | size_t i; | |
39602c39 TA |
2824 | |
2825 | for (i = 0; i < ELEMENTSOF(unit_actions); i++) | |
85d9b598 LP |
2826 | if (streq_ptr(unit_actions[i].verb, verb)) |
2827 | return unit_actions[i].job_type; | |
39602c39 | 2828 | |
85d9b598 | 2829 | return "start"; |
39602c39 TA |
2830 | } |
2831 | ||
e4b61340 | 2832 | static int start_unit_one( |
f459b602 | 2833 | sd_bus *bus, |
85d9b598 LP |
2834 | const char *method, /* When using classic per-job bus methods */ |
2835 | const char *job_type, /* When using new-style EnqueueUnitJob() */ | |
e4b61340 LP |
2836 | const char *name, |
2837 | const char *mode, | |
f459b602 | 2838 | sd_bus_error *error, |
93a08841 | 2839 | BusWaitForJobs *w, |
a7056cde | 2840 | BusWaitForUnits *wu) { |
7e4249b9 | 2841 | |
4afd3348 | 2842 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
45fb0699 | 2843 | const char *path; |
85d9b598 | 2844 | bool done = false; |
7e4249b9 | 2845 | int r; |
7e4249b9 | 2846 | |
e4b61340 LP |
2847 | assert(method); |
2848 | assert(name); | |
2849 | assert(mode); | |
22f4096c | 2850 | assert(error); |
7e4249b9 | 2851 | |
dbc9830c ZJS |
2852 | log_debug("%s dbus call org.freedesktop.systemd1.Manager %s(%s, %s)", |
2853 | arg_dry_run ? "Would execute" : "Executing", | |
1ae17672 | 2854 | method, name, mode); |
85d9b598 | 2855 | |
1ae17672 ZJS |
2856 | if (arg_dry_run) |
2857 | return 0; | |
342641fb | 2858 | |
85d9b598 LP |
2859 | if (arg_show_transaction) { |
2860 | _cleanup_(sd_bus_error_free) sd_bus_error enqueue_error = SD_BUS_ERROR_NULL; | |
39602c39 | 2861 | |
85d9b598 | 2862 | /* Use the new, fancy EnqueueUnitJob() API if the user wants us to print the transaction */ |
8010c205 | 2863 | r = bus_call_method( |
85d9b598 | 2864 | bus, |
8010c205 | 2865 | bus_systemd_mgr, |
85d9b598 LP |
2866 | "EnqueueUnitJob", |
2867 | &enqueue_error, | |
2868 | &reply, | |
2869 | "sss", | |
2870 | name, job_type, mode); | |
2871 | if (r < 0) { | |
2872 | if (!sd_bus_error_has_name(&enqueue_error, SD_BUS_ERROR_UNKNOWN_METHOD)) { | |
2873 | (void) sd_bus_error_move(error, &enqueue_error); | |
2874 | goto fail; | |
2875 | } | |
67f3c402 | 2876 | |
85d9b598 LP |
2877 | /* Hmm, the API is not yet available. Let's use the classic API instead (see below). */ |
2878 | log_notice("--show-transaction not supported by this service manager, proceeding without."); | |
2879 | } else { | |
2880 | const char *u, *jt; | |
2881 | uint32_t id; | |
39602c39 | 2882 | |
85d9b598 LP |
2883 | r = sd_bus_message_read(reply, "uosos", &id, &path, &u, NULL, &jt); |
2884 | if (r < 0) | |
2885 | return bus_log_parse_error(r); | |
ee87525c | 2886 | |
85d9b598 | 2887 | log_info("Enqueued anchor job %" PRIu32 " %s/%s.", id, u, jt); |
ee87525c | 2888 | |
85d9b598 LP |
2889 | r = sd_bus_message_enter_container(reply, 'a', "(uosos)"); |
2890 | if (r < 0) | |
2891 | return bus_log_parse_error(r); | |
2892 | for (;;) { | |
2893 | r = sd_bus_message_read(reply, "(uosos)", &id, NULL, &u, NULL, &jt); | |
2894 | if (r < 0) | |
2895 | return bus_log_parse_error(r); | |
2896 | if (r == 0) | |
2897 | break; | |
2898 | ||
2899 | log_info("Enqueued auxiliary job %" PRIu32 " %s/%s.", id, u, jt); | |
2900 | } | |
2901 | ||
2902 | r = sd_bus_message_exit_container(reply); | |
2903 | if (r < 0) | |
2904 | return bus_log_parse_error(r); | |
2905 | ||
2906 | done = true; | |
2907 | } | |
7e4249b9 LP |
2908 | } |
2909 | ||
85d9b598 | 2910 | if (!done) { |
8010c205 | 2911 | r = bus_call_method(bus, bus_systemd_mgr, method, error, &reply, "ss", name, mode); |
85d9b598 LP |
2912 | if (r < 0) |
2913 | goto fail; | |
2914 | ||
2915 | r = sd_bus_message_read(reply, "o", &path); | |
2916 | if (r < 0) | |
2917 | return bus_log_parse_error(r); | |
2918 | } | |
45fb0699 | 2919 | |
e3e0314b | 2920 | if (need_daemon_reload(bus, name) > 0) |
3f36991e | 2921 | warn_unit_file_changed(name); |
45fb0699 | 2922 | |
ebd011d9 LP |
2923 | if (w) { |
2924 | log_debug("Adding %s to the set", path); | |
2925 | r = bus_wait_for_jobs_add(w, path); | |
cbc9fbd1 | 2926 | if (r < 0) |
d2e1b2fd | 2927 | return log_error_errno(r, "Failed to watch job for %s: %m", name); |
e4b61340 | 2928 | } |
7e4249b9 | 2929 | |
a7056cde LP |
2930 | if (wu) { |
2931 | r = bus_wait_for_units_add_unit(wu, name, BUS_WAIT_FOR_INACTIVE|BUS_WAIT_NO_JOB, NULL, NULL); | |
2932 | if (r < 0) | |
2933 | return log_error_errno(r, "Failed to watch unit %s: %m", name); | |
2934 | } | |
2935 | ||
46eddbb5 | 2936 | return 0; |
85d9b598 LP |
2937 | |
2938 | fail: | |
2939 | /* There's always a fallback possible for legacy actions. */ | |
2940 | if (arg_action != ACTION_SYSTEMCTL) | |
2941 | return r; | |
2942 | ||
2943 | log_error_errno(r, "Failed to %s %s: %s", job_type, name, bus_error_message(error, r)); | |
2944 | ||
2945 | if (!sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT) && | |
2946 | !sd_bus_error_has_name(error, BUS_ERROR_UNIT_MASKED) && | |
2947 | !sd_bus_error_has_name(error, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE)) | |
2948 | log_error("See %s logs and 'systemctl%s status%s %s' for details.", | |
2949 | arg_scope == UNIT_FILE_SYSTEM ? "system" : "user", | |
2950 | arg_scope == UNIT_FILE_SYSTEM ? "" : " --user", | |
2951 | name[0] == '-' ? " --" : "", | |
2952 | name); | |
2953 | ||
2954 | return r; | |
7e4249b9 LP |
2955 | } |
2956 | ||
47a0eaa6 MS |
2957 | static const struct { |
2958 | const char *target; | |
2959 | const char *verb; | |
2960 | const char *mode; | |
2961 | } action_table[_ACTION_MAX] = { | |
5fd77930 LP |
2962 | [ACTION_HALT] = { SPECIAL_HALT_TARGET, "halt", "replace-irreversibly" }, |
2963 | [ACTION_POWEROFF] = { SPECIAL_POWEROFF_TARGET, "poweroff", "replace-irreversibly" }, | |
2964 | [ACTION_REBOOT] = { SPECIAL_REBOOT_TARGET, "reboot", "replace-irreversibly" }, | |
2965 | [ACTION_KEXEC] = { SPECIAL_KEXEC_TARGET, "kexec", "replace-irreversibly" }, | |
2966 | [ACTION_RUNLEVEL2] = { SPECIAL_MULTI_USER_TARGET, NULL, "isolate" }, | |
2967 | [ACTION_RUNLEVEL3] = { SPECIAL_MULTI_USER_TARGET, NULL, "isolate" }, | |
2968 | [ACTION_RUNLEVEL4] = { SPECIAL_MULTI_USER_TARGET, NULL, "isolate" }, | |
2969 | [ACTION_RUNLEVEL5] = { SPECIAL_GRAPHICAL_TARGET, NULL, "isolate" }, | |
2970 | [ACTION_RESCUE] = { SPECIAL_RESCUE_TARGET, "rescue", "isolate" }, | |
2971 | [ACTION_EMERGENCY] = { SPECIAL_EMERGENCY_TARGET, "emergency", "isolate" }, | |
2972 | [ACTION_DEFAULT] = { SPECIAL_DEFAULT_TARGET, "default", "isolate" }, | |
2973 | [ACTION_EXIT] = { SPECIAL_EXIT_TARGET, "exit", "replace-irreversibly" }, | |
2974 | [ACTION_SUSPEND] = { SPECIAL_SUSPEND_TARGET, "suspend", "replace-irreversibly" }, | |
2975 | [ACTION_HIBERNATE] = { SPECIAL_HIBERNATE_TARGET, "hibernate", "replace-irreversibly" }, | |
2976 | [ACTION_HYBRID_SLEEP] = { SPECIAL_HYBRID_SLEEP_TARGET, "hybrid-sleep", "replace-irreversibly" }, | |
e68c79db | 2977 | [ACTION_SUSPEND_THEN_HIBERNATE] = { SPECIAL_SUSPEND_THEN_HIBERNATE_TARGET, "suspend-then-hibernate", "replace-irreversibly" }, |
47a0eaa6 MS |
2978 | }; |
2979 | ||
514f4ef5 | 2980 | static enum action verb_to_action(const char *verb) { |
47a0eaa6 MS |
2981 | enum action i; |
2982 | ||
913c1916 | 2983 | for (i = 0; i < _ACTION_MAX; i++) |
f459b602 | 2984 | if (streq_ptr(action_table[i].verb, verb)) |
47a0eaa6 | 2985 | return i; |
514f4ef5 | 2986 | |
f459b602 MAP |
2987 | return _ACTION_INVALID; |
2988 | } | |
e4b61340 | 2989 | |
94369fc0 LP |
2990 | static const char** make_extra_args(const char *extra_args[static 4]) { |
2991 | size_t n = 0; | |
2992 | ||
2caa38e9 LP |
2993 | assert(extra_args); |
2994 | ||
94369fc0 LP |
2995 | if (arg_scope != UNIT_FILE_SYSTEM) |
2996 | extra_args[n++] = "--user"; | |
2997 | ||
2998 | if (arg_transport == BUS_TRANSPORT_REMOTE) { | |
2999 | extra_args[n++] = "-H"; | |
3000 | extra_args[n++] = arg_host; | |
3001 | } else if (arg_transport == BUS_TRANSPORT_MACHINE) { | |
3002 | extra_args[n++] = "-M"; | |
3003 | extra_args[n++] = arg_host; | |
3004 | } else | |
3005 | assert(arg_transport == BUS_TRANSPORT_LOCAL); | |
3006 | ||
3007 | extra_args[n] = NULL; | |
3008 | return extra_args; | |
3009 | } | |
3010 | ||
e449de87 | 3011 | static int start_unit(int argc, char *argv[], void *userdata) { |
a7056cde | 3012 | _cleanup_(bus_wait_for_units_freep) BusWaitForUnits *wu = NULL; |
ebd011d9 | 3013 | _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL; |
85d9b598 | 3014 | const char *method, *job_type, *mode, *one_name, *suffix = NULL; |
416b8801 | 3015 | _cleanup_free_ char **stopped_units = NULL; /* Do not use _cleanup_strv_free_ */ |
ebd011d9 | 3016 | _cleanup_strv_free_ char **names = NULL; |
0e8d9c0c | 3017 | int r, ret = EXIT_SUCCESS; |
4fbd7192 | 3018 | sd_bus *bus; |
729e3769 | 3019 | char **name; |
e4b61340 | 3020 | |
2f50e59f LP |
3021 | if (arg_wait && !STR_IN_SET(argv[0], "start", "restart")) |
3022 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
3023 | "--wait may only be used with the 'start' or 'restart' commands."); | |
93a08841 MP |
3024 | |
3025 | /* we cannot do sender tracking on the private bus, so we need the full | |
3026 | * one for RefUnit to implement --wait */ | |
3027 | r = acquire_bus(arg_wait ? BUS_FULL : BUS_MANAGER, &bus); | |
4fbd7192 LP |
3028 | if (r < 0) |
3029 | return r; | |
3030 | ||
c59e2ec6 | 3031 | ask_password_agent_open_maybe(); |
8a4b13c5 | 3032 | polkit_agent_open_maybe(); |
d2ad7e1b | 3033 | |
e4b61340 | 3034 | if (arg_action == ACTION_SYSTEMCTL) { |
47a0eaa6 | 3035 | enum action action; |
e4b61340 | 3036 | |
e449de87 LP |
3037 | action = verb_to_action(argv[0]); |
3038 | ||
913c1916 | 3039 | if (action != _ACTION_INVALID) { |
85d9b598 | 3040 | /* A command in style "systemctl reboot", "systemctl poweroff", … */ |
913c1916 | 3041 | method = "StartUnit"; |
85d9b598 | 3042 | job_type = "start"; |
913c1916 AJ |
3043 | mode = action_table[action].mode; |
3044 | one_name = action_table[action].target; | |
3045 | } else { | |
3046 | if (streq(argv[0], "isolate")) { | |
85d9b598 | 3047 | /* A "systemctl isolate <unit1> <unit2> …" command */ |
913c1916 | 3048 | method = "StartUnit"; |
85d9b598 | 3049 | job_type = "start"; |
913c1916 | 3050 | mode = "isolate"; |
913c1916 AJ |
3051 | suffix = ".target"; |
3052 | } else { | |
85d9b598 | 3053 | /* A command in style of "systemctl start <unit1> <unit2> …", "sysemctl stop <unit1> <unit2> …" and so on */ |
913c1916 | 3054 | method = verb_to_method(argv[0]); |
85d9b598 | 3055 | job_type = verb_to_job_type(argv[0]); |
913c1916 AJ |
3056 | mode = arg_job_mode; |
3057 | } | |
3058 | one_name = NULL; | |
3059 | } | |
e4b61340 | 3060 | } else { |
85d9b598 | 3061 | /* A SysV legacy command such as "halt", "reboot", "poweroff", … */ |
78ca9099 | 3062 | assert(arg_action >= 0 && arg_action < _ACTION_MAX); |
47a0eaa6 | 3063 | assert(action_table[arg_action].target); |
913c1916 | 3064 | assert(action_table[arg_action].mode); |
e4b61340 LP |
3065 | |
3066 | method = "StartUnit"; | |
85d9b598 | 3067 | job_type = "start"; |
47a0eaa6 | 3068 | mode = action_table[arg_action].mode; |
e3e0314b | 3069 | one_name = action_table[arg_action].target; |
514f4ef5 LP |
3070 | } |
3071 | ||
8b7ec7bb | 3072 | if (one_name) { |
bea1a013 | 3073 | names = strv_new(one_name); |
8b7ec7bb LP |
3074 | if (!names) |
3075 | return log_oom(); | |
3076 | } else { | |
1d56bc09 ZJS |
3077 | bool expanded; |
3078 | ||
3079 | r = expand_names(bus, strv_skip(argv, 1), suffix, &names, &expanded); | |
e3e0314b | 3080 | if (r < 0) |
691395d8 | 3081 | return log_error_errno(r, "Failed to expand names: %m"); |
1d56bc09 ZJS |
3082 | |
3083 | if (!arg_all && expanded && streq(job_type, "start") && !arg_quiet) { | |
3084 | log_warning("Warning: %ssystemctl start called with a glob pattern.%s", | |
3085 | ansi_highlight_red(), | |
3086 | ansi_normal()); | |
3087 | log_notice("Hint: unit globs expand to loaded units, so start will usually have no effect.\n" | |
3088 | " Passing --all will also load units which are pulled in by other units.\n" | |
3089 | " See systemctl(1) for more details."); | |
3090 | } | |
e3e0314b | 3091 | } |
b6520546 | 3092 | |
6e905d93 | 3093 | if (!arg_no_block) { |
ebd011d9 | 3094 | r = bus_wait_for_jobs_new(bus, &w); |
f647962d MS |
3095 | if (r < 0) |
3096 | return log_error_errno(r, "Could not watch jobs: %m"); | |
e4b61340 LP |
3097 | } |
3098 | ||
93a08841 | 3099 | if (arg_wait) { |
8010c205 | 3100 | r = bus_call_method_async(bus, NULL, bus_systemd_mgr, "Subscribe", NULL, NULL, NULL); |
93a08841 | 3101 | if (r < 0) |
31b2cd5d | 3102 | return log_error_errno(r, "Failed to enable subscription: %m"); |
85d9b598 | 3103 | |
a7056cde | 3104 | r = bus_wait_for_units_new(bus, &wu); |
93a08841 | 3105 | if (r < 0) |
a7056cde | 3106 | return log_error_errno(r, "Failed to allocate unit watch context: %m"); |
93a08841 MP |
3107 | } |
3108 | ||
b6520546 | 3109 | STRV_FOREACH(name, names) { |
4afd3348 | 3110 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
f459b602 | 3111 | |
a7056cde | 3112 | r = start_unit_one(bus, method, job_type, *name, mode, &error, w, wu); |
0e8d9c0c LP |
3113 | if (ret == EXIT_SUCCESS && r < 0) |
3114 | ret = translate_bus_error_to_exit_status(r, &error); | |
416b8801 YW |
3115 | |
3116 | if (r >= 0 && streq(method, "StopUnit")) { | |
3117 | r = strv_push(&stopped_units, *name); | |
3118 | if (r < 0) | |
3119 | return log_oom(); | |
3120 | } | |
e4b61340 LP |
3121 | } |
3122 | ||
67f3c402 | 3123 | if (!arg_no_block) { |
94369fc0 | 3124 | const char* extra_args[4]; |
f459b602 | 3125 | |
94369fc0 | 3126 | r = bus_wait_for_jobs(w, arg_quiet, make_extra_args(extra_args)); |
0e8d9c0c LP |
3127 | if (r < 0) |
3128 | return r; | |
49111a70 ZJS |
3129 | |
3130 | /* When stopping units, warn if they can still be triggered by | |
3131 | * another active unit (socket, path, timer) */ | |
416b8801 YW |
3132 | if (!arg_quiet) |
3133 | STRV_FOREACH(name, stopped_units) | |
86f004fb | 3134 | (void) check_triggering_units(bus, *name); |
67f3c402 | 3135 | } |
514f4ef5 | 3136 | |
a7056cde LP |
3137 | if (arg_wait) { |
3138 | r = bus_wait_for_units_run(wu); | |
0e8d9c0c | 3139 | if (r < 0) |
a7056cde LP |
3140 | return log_error_errno(r, "Failed to wait for units: %m"); |
3141 | if (r == BUS_WAIT_FAILURE && ret == EXIT_SUCCESS) | |
0e8d9c0c | 3142 | ret = EXIT_FAILURE; |
93a08841 MP |
3143 | } |
3144 | ||
0e8d9c0c | 3145 | return ret; |
e4b61340 LP |
3146 | } |
3147 | ||
349cc4a5 | 3148 | #if ENABLE_LOGIND |
4fbd7192 | 3149 | static int logind_set_wall_message(void) { |
4afd3348 | 3150 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
4fbd7192 | 3151 | sd_bus *bus; |
f2d11d35 LP |
3152 | _cleanup_free_ char *m = NULL; |
3153 | int r; | |
3154 | ||
4fbd7192 LP |
3155 | r = acquire_bus(BUS_FULL, &bus); |
3156 | if (r < 0) | |
3157 | return r; | |
f2d11d35 LP |
3158 | |
3159 | m = strv_join(arg_wall, " "); | |
3160 | if (!m) | |
3161 | return log_oom(); | |
3162 | ||
dbc9830c ZJS |
3163 | log_debug("%s wall message \"%s\".", arg_dry_run ? "Would set" : "Setting", m); |
3164 | if (arg_dry_run) | |
3165 | return 0; | |
3166 | ||
8010c205 | 3167 | r = bus_call_method(bus, bus_login_mgr, "SetWallMessage", &error, NULL, "sb", m, !arg_no_wall); |
f2d11d35 LP |
3168 | if (r < 0) |
3169 | return log_warning_errno(r, "Failed to set wall message, ignoring: %s", bus_error_message(&error, r)); | |
f2d11d35 LP |
3170 | return 0; |
3171 | } | |
79f1c8f6 | 3172 | #endif |
f2d11d35 | 3173 | |
c45e5fb8 | 3174 | /* Ask systemd-logind, which might grant access to unprivileged users through polkit */ |
4fbd7192 | 3175 | static int logind_reboot(enum action a) { |
349cc4a5 | 3176 | #if ENABLE_LOGIND |
c45e5fb8 LP |
3177 | static const struct { |
3178 | const char *method; | |
3179 | const char *description; | |
3180 | } actions[_ACTION_MAX] = { | |
3181 | [ACTION_POWEROFF] = { "PowerOff", "power off system" }, | |
3182 | [ACTION_REBOOT] = { "Reboot", "reboot system" }, | |
3183 | [ACTION_HALT] = { "Halt", "halt system" }, | |
3184 | [ACTION_SUSPEND] = { "Suspend", "suspend system" }, | |
3185 | [ACTION_HIBERNATE] = { "Hibernate", "hibernate system" }, | |
3186 | [ACTION_HYBRID_SLEEP] = { "HybridSleep", "put system into hybrid sleep" }, | |
3187 | [ACTION_SUSPEND_THEN_HIBERNATE] = { "SuspendThenHibernate", "suspend system, hibernate later" }, | |
3188 | }; | |
3189 | ||
4afd3348 | 3190 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
4fbd7192 | 3191 | sd_bus *bus; |
f459b602 | 3192 | int r; |
4c80c73c | 3193 | |
c45e5fb8 LP |
3194 | if (a < 0 || a >= _ACTION_MAX || !actions[a].method) |
3195 | return -EINVAL; | |
3196 | ||
4fbd7192 LP |
3197 | r = acquire_bus(BUS_FULL, &bus); |
3198 | if (r < 0) | |
3199 | return r; | |
f2d11d35 | 3200 | |
8a4b13c5 | 3201 | polkit_agent_open_maybe(); |
d2ad7e1b ZJS |
3202 | (void) logind_set_wall_message(); |
3203 | ||
c45e5fb8 LP |
3204 | log_debug("%s org.freedesktop.login1.Manager %s dbus call.", arg_dry_run ? "Would execute" : "Executing", actions[a].method); |
3205 | ||
dbc9830c ZJS |
3206 | if (arg_dry_run) |
3207 | return 0; | |
3208 | ||
8010c205 | 3209 | r = bus_call_method(bus, bus_login_mgr, actions[a].method, &error, NULL, "b", arg_ask_password); |
f459b602 | 3210 | if (r < 0) |
c45e5fb8 | 3211 | return log_error_errno(r, "Failed to %s via logind: %s", actions[a].description, bus_error_message(&error, r)); |
f459b602 | 3212 | |
691395d8 | 3213 | return 0; |
4c80c73c KS |
3214 | #else |
3215 | return -ENOSYS; | |
3216 | #endif | |
3217 | } | |
3218 | ||
4fbd7192 | 3219 | static int logind_check_inhibitors(enum action a) { |
349cc4a5 | 3220 | #if ENABLE_LOGIND |
4afd3348 | 3221 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
59164be4 | 3222 | _cleanup_strv_free_ char **sessions = NULL; |
f459b602 MAP |
3223 | const char *what, *who, *why, *mode; |
3224 | uint32_t uid, pid; | |
4fbd7192 | 3225 | sd_bus *bus; |
f459b602 | 3226 | unsigned c = 0; |
59164be4 | 3227 | char **s; |
f459b602 | 3228 | int r; |
b37844d3 | 3229 | |
748ebafa LP |
3230 | if (arg_ignore_inhibitors || arg_force > 0) |
3231 | return 0; | |
3232 | ||
3233 | if (arg_when > 0) | |
3234 | return 0; | |
3235 | ||
3236 | if (geteuid() == 0) | |
b37844d3 LP |
3237 | return 0; |
3238 | ||
3239 | if (!on_tty()) | |
3240 | return 0; | |
e08ab379 LP |
3241 | |
3242 | if (arg_transport != BUS_TRANSPORT_LOCAL) | |
3243 | return 0; | |
b37844d3 | 3244 | |
4fbd7192 LP |
3245 | r = acquire_bus(BUS_FULL, &bus); |
3246 | if (r < 0) | |
3247 | return r; | |
3248 | ||
8010c205 | 3249 | r = bus_call_method(bus, bus_login_mgr, "ListInhibitors", NULL, &reply, NULL); |
b37844d3 LP |
3250 | if (r < 0) |
3251 | /* If logind is not around, then there are no inhibitors... */ | |
3252 | return 0; | |
3253 | ||
4aa2beac | 3254 | r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssuu)"); |
f459b602 MAP |
3255 | if (r < 0) |
3256 | return bus_log_parse_error(r); | |
b37844d3 | 3257 | |
4aa2beac | 3258 | while ((r = sd_bus_message_read(reply, "(ssssuu)", &what, &who, &why, &mode, &uid, &pid)) > 0) { |
59164be4 | 3259 | _cleanup_free_ char *comm = NULL, *user = NULL; |
f459b602 | 3260 | _cleanup_strv_free_ char **sv = NULL; |
b37844d3 LP |
3261 | |
3262 | if (!streq(mode, "block")) | |
f459b602 | 3263 | continue; |
b37844d3 LP |
3264 | |
3265 | sv = strv_split(what, ":"); | |
3266 | if (!sv) | |
3267 | return log_oom(); | |
3268 | ||
2f50e59f LP |
3269 | if (!pid_is_valid((pid_t) pid)) |
3270 | return log_error_errno(SYNTHETIC_ERRNO(ERANGE), "Invalid PID "PID_FMT".", (pid_t) pid); | |
d028e018 | 3271 | |
b37844d3 | 3272 | if (!strv_contains(sv, |
4c315c2c IS |
3273 | IN_SET(a, |
3274 | ACTION_HALT, | |
3275 | ACTION_POWEROFF, | |
3276 | ACTION_REBOOT, | |
3277 | ACTION_KEXEC) ? "shutdown" : "sleep")) | |
f459b602 | 3278 | continue; |
b37844d3 LP |
3279 | |
3280 | get_process_comm(pid, &comm); | |
59164be4 | 3281 | user = uid_to_name(uid); |
f459b602 | 3282 | |
de0671ee | 3283 | log_warning("Operation inhibited by \"%s\" (PID "PID_FMT" \"%s\", user %s), reason is \"%s\".", |
d028e018 | 3284 | who, (pid_t) pid, strna(comm), strna(user), why); |
b37844d3 | 3285 | |
f459b602 | 3286 | c++; |
b37844d3 | 3287 | } |
f459b602 MAP |
3288 | if (r < 0) |
3289 | return bus_log_parse_error(r); | |
b37844d3 | 3290 | |
f459b602 MAP |
3291 | r = sd_bus_message_exit_container(reply); |
3292 | if (r < 0) | |
3293 | return bus_log_parse_error(r); | |
b37844d3 | 3294 | |
59164be4 LP |
3295 | /* Check for current sessions */ |
3296 | sd_get_sessions(&sessions); | |
3297 | STRV_FOREACH(s, sessions) { | |
59164be4 LP |
3298 | _cleanup_free_ char *type = NULL, *tty = NULL, *seat = NULL, *user = NULL, *service = NULL, *class = NULL; |
3299 | ||
3300 | if (sd_session_get_uid(*s, &uid) < 0 || uid == getuid()) | |
3301 | continue; | |
3302 | ||
3303 | if (sd_session_get_class(*s, &class) < 0 || !streq(class, "user")) | |
3304 | continue; | |
3305 | ||
28abce5d | 3306 | if (sd_session_get_type(*s, &type) < 0 || !STR_IN_SET(type, "x11", "wayland", "tty", "mir")) |
59164be4 LP |
3307 | continue; |
3308 | ||
3309 | sd_session_get_tty(*s, &tty); | |
3310 | sd_session_get_seat(*s, &seat); | |
3311 | sd_session_get_service(*s, &service); | |
3312 | user = uid_to_name(uid); | |
3313 | ||
3314 | log_warning("User %s is logged in on %s.", strna(user), isempty(tty) ? (isempty(seat) ? strna(service) : seat) : tty); | |
3315 | c++; | |
3316 | } | |
3317 | ||
b37844d3 LP |
3318 | if (c <= 0) |
3319 | return 0; | |
3320 | ||
59164be4 | 3321 | log_error("Please retry operation after closing inhibitors and logging out other users.\nAlternatively, ignore inhibitors and users with 'systemctl %s -i'.", |
47a0eaa6 | 3322 | action_table[a].verb); |
b37844d3 LP |
3323 | |
3324 | return -EPERM; | |
3325 | #else | |
3326 | return 0; | |
3327 | #endif | |
3328 | } | |
3329 | ||
97af80c5 LP |
3330 | static int prepare_firmware_setup(void) { |
3331 | ||
3332 | if (!arg_firmware_setup) | |
3333 | return 0; | |
3334 | ||
349cc4a5 | 3335 | #if ENABLE_LOGIND |
4afd3348 | 3336 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
4fbd7192 | 3337 | sd_bus *bus; |
a4921cf4 JJ |
3338 | int r; |
3339 | ||
4fbd7192 LP |
3340 | r = acquire_bus(BUS_FULL, &bus); |
3341 | if (r < 0) | |
3342 | return r; | |
a4921cf4 | 3343 | |
8010c205 | 3344 | r = bus_call_method(bus, bus_login_mgr, "SetRebootToFirmwareSetup", &error, NULL, "b", true); |
4fbd7192 LP |
3345 | if (r < 0) |
3346 | return log_error_errno(r, "Cannot indicate to EFI to boot into setup mode: %s", bus_error_message(&error, r)); | |
a4921cf4 JJ |
3347 | |
3348 | return 0; | |
3349 | #else | |
2f50e59f LP |
3350 | return log_error_errno(SYNTHETIC_ERRNO(ENOSYS), |
3351 | "Booting into firmware setup not supported."); | |
a4921cf4 JJ |
3352 | #endif |
3353 | } | |
3354 | ||
97af80c5 LP |
3355 | static int prepare_boot_loader_menu(void) { |
3356 | ||
3357 | if (arg_boot_loader_menu == USEC_INFINITY) | |
3358 | return 0; | |
3359 | ||
3360 | #if ENABLE_LOGIND | |
3361 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
3362 | sd_bus *bus; | |
4fbd7192 LP |
3363 | int r; |
3364 | ||
97af80c5 LP |
3365 | r = acquire_bus(BUS_FULL, &bus); |
3366 | if (r < 0) | |
3367 | return r; | |
3368 | ||
8010c205 | 3369 | r = bus_call_method(bus, bus_login_mgr, "SetRebootToBootLoaderMenu", &error, NULL, "t", arg_boot_loader_menu); |
97af80c5 LP |
3370 | if (r < 0) |
3371 | return log_error_errno(r, "Cannot indicate to boot loader to enter boot loader entry menu: %s", bus_error_message(&error, r)); | |
3372 | ||
3373 | return 0; | |
3374 | #else | |
2f50e59f LP |
3375 | return log_error_errno(SYNTHETIC_ERRNO(ENOSYS), |
3376 | "Booting into boot loader menu not supported."); | |
97af80c5 LP |
3377 | #endif |
3378 | } | |
3379 | ||
3380 | static int prepare_boot_loader_entry(void) { | |
3381 | ||
3382 | if (!arg_boot_loader_entry) | |
4fbd7192 LP |
3383 | return 0; |
3384 | ||
97af80c5 LP |
3385 | #if ENABLE_LOGIND |
3386 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
3387 | sd_bus *bus; | |
3388 | int r; | |
4fbd7192 | 3389 | |
97af80c5 LP |
3390 | r = acquire_bus(BUS_FULL, &bus); |
3391 | if (r < 0) | |
3392 | return r; | |
3393 | ||
8010c205 | 3394 | r = bus_call_method(bus, bus_login_mgr, "SetRebootToBootLoaderEntry", &error, NULL, "s", arg_boot_loader_entry); |
97af80c5 LP |
3395 | if (r < 0) |
3396 | return log_error_errno(r, "Cannot set boot into loader entry '%s': %s", arg_boot_loader_entry, bus_error_message(&error, r)); | |
4fbd7192 | 3397 | |
97af80c5 LP |
3398 | return 0; |
3399 | #else | |
ee429649 LP |
3400 | return log_error_errno(SYNTHETIC_ERRNO(ENOSYS), |
3401 | "Booting into boot loader entry not supported."); | |
97af80c5 | 3402 | #endif |
4fbd7192 LP |
3403 | } |
3404 | ||
4bb2e9d4 ZJS |
3405 | static int load_kexec_kernel(void) { |
3406 | _cleanup_(boot_config_free) BootConfig config = {}; | |
43b736a8 | 3407 | _cleanup_free_ char *kernel = NULL, *initrd = NULL, *options = NULL; |
4bb2e9d4 ZJS |
3408 | const BootEntry *e; |
3409 | pid_t pid; | |
3410 | int r; | |
3411 | ||
3412 | if (kexec_loaded()) { | |
3413 | log_debug("Kexec kernel already loaded."); | |
3414 | return 0; | |
3415 | } | |
3416 | ||
c0d73214 ZJS |
3417 | if (access(KEXEC, X_OK) < 0) |
3418 | return log_error_errno(errno, KEXEC" is not available: %m"); | |
3419 | ||
38bd74d6 | 3420 | r = boot_entries_load_config_auto(NULL, NULL, &config); |
2fec5854 ZJS |
3421 | if (r == -ENOKEY) |
3422 | /* The call doesn't log about ENOKEY, let's do so here. */ | |
3423 | return log_error_errno(r, | |
3424 | "No kexec kernel loaded and autodetection failed.\n%s", | |
3425 | is_efi_boot() | |
3426 | ? "Cannot automatically load kernel: ESP partition mount point not found." | |
3427 | : "Automatic loading works only on systems booted with EFI."); | |
4bb2e9d4 | 3428 | if (r < 0) |
1b20d889 | 3429 | return r; |
4bb2e9d4 | 3430 | |
38bd74d6 LP |
3431 | e = boot_config_default_entry(&config); |
3432 | if (!e) | |
3433 | return log_error_errno(SYNTHETIC_ERRNO(ENOENT), | |
3434 | "No boot loader entry suitable as default, refusing to guess."); | |
3435 | ||
3436 | log_debug("Found default boot loader entry in file \"%s\"", e->path); | |
3437 | ||
78c38fe7 LP |
3438 | if (!e->kernel) |
3439 | return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), | |
3440 | "Boot entry does not refer to Linux kernel, which is not supported currently."); | |
8a1ef3bf | 3441 | if (strv_length(e->initrd) > 1) |
0f88d829 | 3442 | return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), |
8a1ef3bf | 3443 | "Boot entry specifies multiple initrds, which is not supported currently."); |
4bb2e9d4 | 3444 | |
43b736a8 | 3445 | kernel = path_join(e->root, e->kernel); |
9fc32cd9 LP |
3446 | if (!kernel) |
3447 | return log_oom(); | |
3448 | ||
3449 | if (!strv_isempty(e->initrd)) { | |
0f88d829 | 3450 | initrd = path_join(e->root, e->initrd[0]); |
9fc32cd9 LP |
3451 | if (!initrd) |
3452 | return log_oom(); | |
3453 | } | |
3454 | ||
4bb2e9d4 ZJS |
3455 | options = strv_join(e->options, " "); |
3456 | if (!options) | |
3457 | return log_oom(); | |
3458 | ||
cd086a01 ZJS |
3459 | log_full(arg_quiet ? LOG_DEBUG : LOG_INFO, |
3460 | "%s "KEXEC" --load \"%s\" --append \"%s\"%s%s%s", | |
3461 | arg_dry_run ? "Would run" : "Running", | |
3462 | kernel, | |
3463 | options, | |
3464 | initrd ? " --initrd \"" : NULL, strempty(initrd), initrd ? "\"" : ""); | |
4bb2e9d4 ZJS |
3465 | if (arg_dry_run) |
3466 | return 0; | |
3467 | ||
c943e95f | 3468 | r = safe_fork("(kexec)", FORK_WAIT|FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG, &pid); |
4c253ed1 | 3469 | if (r < 0) |
b6e1fff1 | 3470 | return r; |
4c253ed1 | 3471 | if (r == 0) { |
4bb2e9d4 ZJS |
3472 | const char* const args[] = { |
3473 | KEXEC, | |
3474 | "--load", kernel, | |
3475 | "--append", options, | |
3476 | initrd ? "--initrd" : NULL, initrd, | |
c943e95f LP |
3477 | NULL |
3478 | }; | |
4bb2e9d4 ZJS |
3479 | |
3480 | /* Child */ | |
4bb2e9d4 ZJS |
3481 | execv(args[0], (char * const *) args); |
3482 | _exit(EXIT_FAILURE); | |
4c253ed1 LP |
3483 | } |
3484 | ||
df685d57 | 3485 | return 0; |
4bb2e9d4 ZJS |
3486 | } |
3487 | ||
57ab9006 | 3488 | static int set_exit_code(uint8_t code) { |
4afd3348 | 3489 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
57ab9006 LP |
3490 | sd_bus *bus; |
3491 | int r; | |
3492 | ||
3493 | r = acquire_bus(BUS_MANAGER, &bus); | |
3494 | if (r < 0) | |
3495 | return r; | |
3496 | ||
8010c205 | 3497 | r = bus_call_method(bus, bus_systemd_mgr, "SetExitCode", &error, NULL, "y", code); |
57ab9006 | 3498 | if (r < 0) |
af3d8113 | 3499 | return log_error_errno(r, "Failed to set exit code: %s", bus_error_message(&error, r)); |
57ab9006 LP |
3500 | |
3501 | return 0; | |
3502 | } | |
3503 | ||
e449de87 | 3504 | static int start_special(int argc, char *argv[], void *userdata) { |
4c80c73c | 3505 | enum action a; |
983d9c90 | 3506 | int r; |
2b0f4e6f ZJS |
3507 | bool termination_action; /* an action that terminates the manager, |
3508 | * can be performed also by signal. */ | |
983d9c90 | 3509 | |
e449de87 | 3510 | assert(argv); |
514f4ef5 | 3511 | |
e449de87 | 3512 | a = verb_to_action(argv[0]); |
4c80c73c | 3513 | |
4fbd7192 | 3514 | r = logind_check_inhibitors(a); |
748ebafa LP |
3515 | if (r < 0) |
3516 | return r; | |
3517 | ||
fba868fa LP |
3518 | if (arg_force >= 2) { |
3519 | r = must_be_root(); | |
3520 | if (r < 0) | |
3521 | return r; | |
c32b90de LP |
3522 | } |
3523 | ||
4fbd7192 | 3524 | r = prepare_firmware_setup(); |
a4921cf4 JJ |
3525 | if (r < 0) |
3526 | return r; | |
5bdf2243 | 3527 | |
97af80c5 LP |
3528 | r = prepare_boot_loader_menu(); |
3529 | if (r < 0) | |
3530 | return r; | |
3531 | ||
3532 | r = prepare_boot_loader_entry(); | |
3533 | if (r < 0) | |
3534 | return r; | |
3535 | ||
dae710be | 3536 | if (a == ACTION_REBOOT) { |
3537 | const char *arg = NULL; | |
3538 | ||
3539 | if (argc > 1) { | |
3540 | if (arg_reboot_argument) | |
3541 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Both --reboot-argument= and positional argument passed to reboot command, refusing."); | |
3542 | ||
3543 | log_notice("Positional argument to reboot command is deprecated, please use --reboot-argument= instead. Accepting anyway."); | |
3544 | arg = argv[1]; | |
3545 | } else | |
3546 | arg = arg_reboot_argument; | |
3547 | ||
3548 | if (arg) { | |
3549 | r = update_reboot_parameter_and_warn(arg, false); | |
3550 | if (r < 0) | |
3551 | return r; | |
3552 | } | |
57ab9006 | 3553 | |
4bb2e9d4 ZJS |
3554 | } else if (a == ACTION_KEXEC) { |
3555 | r = load_kexec_kernel(); | |
d23b5ce2 ZJS |
3556 | if (r < 0 && arg_force >= 1) |
3557 | log_notice("Failed to load kexec kernel, continuing without."); | |
3558 | else if (r < 0) | |
4bb2e9d4 ZJS |
3559 | return r; |
3560 | ||
e449de87 | 3561 | } else if (a == ACTION_EXIT && argc > 1) { |
7bbb5359 | 3562 | uint8_t code; |
287419c1 | 3563 | |
57ab9006 LP |
3564 | /* If the exit code is not given on the command line, |
3565 | * don't reset it to zero: just keep it as it might | |
3566 | * have been set previously. */ | |
287419c1 | 3567 | |
e449de87 | 3568 | r = safe_atou8(argv[1], &code); |
4fbd7192 | 3569 | if (r < 0) |
57ab9006 | 3570 | return log_error_errno(r, "Invalid exit code."); |
4fbd7192 | 3571 | |
57ab9006 | 3572 | r = set_exit_code(code); |
691395d8 | 3573 | if (r < 0) |
57ab9006 | 3574 | return r; |
b986229e SW |
3575 | } |
3576 | ||
2b0f4e6f ZJS |
3577 | termination_action = IN_SET(a, |
3578 | ACTION_HALT, | |
3579 | ACTION_POWEROFF, | |
3580 | ACTION_REBOOT); | |
3581 | if (termination_action && arg_force >= 2) | |
477def80 | 3582 | return halt_now(a); |
e606bb61 | 3583 | |
7e59bfcb | 3584 | if (arg_force >= 1 && |
5955df2c | 3585 | (termination_action || IN_SET(a, ACTION_KEXEC, ACTION_EXIT))) |
2b0f4e6f ZJS |
3586 | r = trivial_method(argc, argv, userdata); |
3587 | else { | |
3588 | /* First try logind, to allow authentication with polkit */ | |
3589 | if (IN_SET(a, | |
3590 | ACTION_POWEROFF, | |
3591 | ACTION_REBOOT, | |
36b69c31 | 3592 | ACTION_HALT, |
2b0f4e6f ZJS |
3593 | ACTION_SUSPEND, |
3594 | ACTION_HIBERNATE, | |
c58493c0 | 3595 | ACTION_HYBRID_SLEEP, |
e68c79db | 3596 | ACTION_SUSPEND_THEN_HIBERNATE)) { |
2b0f4e6f ZJS |
3597 | |
3598 | r = logind_reboot(a); | |
3599 | if (r >= 0) | |
3600 | return r; | |
3601 | if (IN_SET(r, -EOPNOTSUPP, -EINPROGRESS)) | |
3602 | /* requested operation is not supported or already in progress */ | |
3603 | return r; | |
20b09ca7 | 3604 | |
130246d2 LP |
3605 | /* On all other errors, try low-level operation. In order to minimize the difference between |
3606 | * operation with and without logind, we explicitly enable non-blocking mode for this, as | |
3607 | * logind's shutdown operations are always non-blocking. */ | |
3608 | ||
3609 | arg_no_block = true; | |
3610 | ||
3611 | } else if (IN_SET(a, ACTION_EXIT, ACTION_KEXEC)) | |
3612 | /* Since exit/kexec are so close in behaviour to power-off/reboot, let's also make them | |
3613 | * asynchronous, in order to not confuse the user needlessly with unexpected behaviour. */ | |
3614 | arg_no_block = true; | |
7089051f | 3615 | |
2b0f4e6f | 3616 | r = start_unit(argc, argv, userdata); |
4c80c73c | 3617 | } |
983d9c90 | 3618 | |
2b0f4e6f ZJS |
3619 | if (termination_action && arg_force < 2 && |
3620 | IN_SET(r, -ENOENT, -ETIMEDOUT)) | |
cc7de2ba | 3621 | log_notice("It is possible to perform action directly, see discussion of --force --force in man:systemctl(1)."); |
2b0f4e6f ZJS |
3622 | |
3623 | return r; | |
514f4ef5 LP |
3624 | } |
3625 | ||
988b3b17 ZJS |
3626 | static int start_system_special(int argc, char *argv[], void *userdata) { |
3627 | /* Like start_special above, but raises an error when running in user mode */ | |
3628 | ||
baaa35ad ZJS |
3629 | if (arg_scope != UNIT_FILE_SYSTEM) |
3630 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
3631 | "Bad action for %s mode.", | |
3632 | arg_scope == UNIT_FILE_GLOBAL ? "--global" : "--user"); | |
988b3b17 ZJS |
3633 | |
3634 | return start_special(argc, argv, userdata); | |
3635 | } | |
3636 | ||
fa0d5878 | 3637 | static int check_unit_generic(int code, const UnitActiveState good_states[], int nb_states, char **args) { |
e3e0314b | 3638 | _cleanup_strv_free_ char **names = NULL; |
fa0d5878 | 3639 | UnitActiveState active_state; |
4fbd7192 | 3640 | sd_bus *bus; |
729e3769 | 3641 | char **name; |
fa0d5878 | 3642 | int r, i; |
d60f6ad0 | 3643 | bool found = false; |
0183528f | 3644 | |
4fbd7192 LP |
3645 | r = acquire_bus(BUS_MANAGER, &bus); |
3646 | if (r < 0) | |
3647 | return r; | |
3648 | ||
1d56bc09 | 3649 | r = expand_names(bus, args, NULL, &names, NULL); |
f647962d MS |
3650 | if (r < 0) |
3651 | return log_error_errno(r, "Failed to expand names: %m"); | |
e3e0314b ZJS |
3652 | |
3653 | STRV_FOREACH(name, names) { | |
fa0d5878 BR |
3654 | r = get_state_one_unit(bus, *name, &active_state); |
3655 | if (r < 0) | |
3656 | return r; | |
3657 | ||
3658 | if (!arg_quiet) | |
3659 | puts(unit_active_state_to_string(active_state)); | |
60f9ba0b | 3660 | |
fa0d5878 BR |
3661 | for (i = 0; i < nb_states; ++i) |
3662 | if (good_states[i] == active_state) | |
3663 | found = true; | |
1a0fce45 TA |
3664 | } |
3665 | ||
d60f6ad0 LN |
3666 | /* use the given return code for the case that we won't find |
3667 | * any unit which matches the list */ | |
3668 | return found ? 0 : code; | |
1a0fce45 TA |
3669 | } |
3670 | ||
e449de87 | 3671 | static int check_unit_active(int argc, char *argv[], void *userdata) { |
9897b09b LP |
3672 | static const UnitActiveState states[] = { |
3673 | UNIT_ACTIVE, | |
3674 | UNIT_RELOADING, | |
3675 | }; | |
3676 | ||
e3e0314b | 3677 | /* According to LSB: 3, "program is not running" */ |
b613907e | 3678 | return check_unit_generic(EXIT_PROGRAM_NOT_RUNNING, states, ELEMENTSOF(states), strv_skip(argv, 1)); |
e3e0314b | 3679 | } |
0183528f | 3680 | |
e449de87 | 3681 | static int check_unit_failed(int argc, char *argv[], void *userdata) { |
9897b09b LP |
3682 | static const UnitActiveState states[] = { |
3683 | UNIT_FAILED, | |
3684 | }; | |
3685 | ||
b613907e | 3686 | return check_unit_generic(EXIT_PROGRAM_DEAD_AND_PID_EXISTS, states, ELEMENTSOF(states), strv_skip(argv, 1)); |
48220598 LP |
3687 | } |
3688 | ||
e449de87 | 3689 | static int kill_unit(int argc, char *argv[], void *userdata) { |
e3e0314b | 3690 | _cleanup_strv_free_ char **names = NULL; |
ac5e3a50 | 3691 | char *kill_who = NULL, **name; |
4fbd7192 | 3692 | sd_bus *bus; |
e3e0314b | 3693 | int r, q; |
8a0867d6 | 3694 | |
4fbd7192 LP |
3695 | r = acquire_bus(BUS_MANAGER, &bus); |
3696 | if (r < 0) | |
3697 | return r; | |
3698 | ||
8a4b13c5 | 3699 | polkit_agent_open_maybe(); |
d2ad7e1b | 3700 | |
8a0867d6 LP |
3701 | if (!arg_kill_who) |
3702 | arg_kill_who = "all"; | |
3703 | ||
ac5e3a50 JS |
3704 | /* --fail was specified */ |
3705 | if (streq(arg_job_mode, "fail")) | |
81d62103 | 3706 | kill_who = strjoina(arg_kill_who, "-fail"); |
ac5e3a50 | 3707 | |
1d56bc09 | 3708 | r = expand_names(bus, strv_skip(argv, 1), NULL, &names, NULL); |
e3e0314b | 3709 | if (r < 0) |
691395d8 | 3710 | return log_error_errno(r, "Failed to expand names: %m"); |
60f9ba0b | 3711 | |
e3e0314b | 3712 | STRV_FOREACH(name, names) { |
4afd3348 | 3713 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
342641fb | 3714 | |
8010c205 | 3715 | q = bus_call_method( |
f22f08cd | 3716 | bus, |
8010c205 | 3717 | bus_systemd_mgr, |
6e646d22 LP |
3718 | "KillUnit", |
3719 | &error, | |
3720 | NULL, | |
169d7fb6 | 3721 | "ssi", *name, kill_who ? kill_who : arg_kill_who, arg_signal); |
e3e0314b | 3722 | if (q < 0) { |
169d7fb6 | 3723 | log_error_errno(q, "Failed to kill unit %s: %s", *name, bus_error_message(&error, q)); |
e3e0314b ZJS |
3724 | if (r == 0) |
3725 | r = q; | |
f459b602 | 3726 | } |
8a0867d6 | 3727 | } |
f459b602 | 3728 | |
e3e0314b | 3729 | return r; |
8a0867d6 LP |
3730 | } |
3731 | ||
d9e45bc3 | 3732 | static int clean_or_freeze_unit(int argc, char *argv[], void *userdata) { |
d81bc51f LP |
3733 | _cleanup_(bus_wait_for_units_freep) BusWaitForUnits *w = NULL; |
3734 | _cleanup_strv_free_ char **names = NULL; | |
3735 | int r, ret = EXIT_SUCCESS; | |
3736 | char **name; | |
d9e45bc3 | 3737 | const char *method; |
d81bc51f LP |
3738 | sd_bus *bus; |
3739 | ||
3740 | r = acquire_bus(BUS_FULL, &bus); | |
3741 | if (r < 0) | |
3742 | return r; | |
3743 | ||
3744 | polkit_agent_open_maybe(); | |
3745 | ||
3746 | if (!arg_clean_what) { | |
3747 | arg_clean_what = strv_new("cache", "runtime"); | |
3748 | if (!arg_clean_what) | |
3749 | return log_oom(); | |
3750 | } | |
3751 | ||
1d56bc09 | 3752 | r = expand_names(bus, strv_skip(argv, 1), NULL, &names, NULL); |
d81bc51f LP |
3753 | if (r < 0) |
3754 | return log_error_errno(r, "Failed to expand names: %m"); | |
3755 | ||
3756 | if (!arg_no_block) { | |
3757 | r = bus_wait_for_units_new(bus, &w); | |
3758 | if (r < 0) | |
3759 | return log_error_errno(r, "Failed to allocate unit waiter: %m"); | |
3760 | } | |
3761 | ||
d9e45bc3 MS |
3762 | if (streq(argv[0], "clean")) |
3763 | method = "CleanUnit"; | |
3764 | else if (streq(argv[0], "freeze")) | |
3765 | method = "FreezeUnit"; | |
3766 | else if (streq(argv[0], "thaw")) | |
3767 | method = "ThawUnit"; | |
fbb0b665 BR |
3768 | else |
3769 | assert_not_reached("Unhandled method"); | |
d9e45bc3 | 3770 | |
d81bc51f LP |
3771 | STRV_FOREACH(name, names) { |
3772 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
3773 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; | |
3774 | ||
3775 | if (w) { | |
3776 | /* If we shall wait for the cleaning to complete, let's add a ref on the unit first */ | |
8010c205 | 3777 | r = bus_call_method(bus, bus_systemd_mgr, "RefUnit", &error, NULL, "s", *name); |
d81bc51f LP |
3778 | if (r < 0) { |
3779 | log_error_errno(r, "Failed to add reference to unit %s: %s", *name, bus_error_message(&error, r)); | |
3780 | if (ret == EXIT_SUCCESS) | |
3781 | ret = r; | |
3782 | continue; | |
3783 | } | |
3784 | } | |
3785 | ||
8010c205 | 3786 | r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, method); |
d81bc51f LP |
3787 | if (r < 0) |
3788 | return bus_log_create_error(r); | |
3789 | ||
3790 | r = sd_bus_message_append(m, "s", *name); | |
3791 | if (r < 0) | |
3792 | return bus_log_create_error(r); | |
3793 | ||
d9e45bc3 MS |
3794 | if (streq(method, "CleanUnit")) { |
3795 | r = sd_bus_message_append_strv(m, arg_clean_what); | |
3796 | if (r < 0) | |
3797 | return bus_log_create_error(r); | |
3798 | } | |
d81bc51f LP |
3799 | |
3800 | r = sd_bus_call(bus, m, 0, &error, NULL); | |
3801 | if (r < 0) { | |
d9e45bc3 | 3802 | log_error_errno(r, "Failed to %s unit %s: %s", argv[0], *name, bus_error_message(&error, r)); |
d81bc51f LP |
3803 | if (ret == EXIT_SUCCESS) { |
3804 | ret = r; | |
3805 | continue; | |
3806 | } | |
3807 | } | |
3808 | ||
3809 | if (w) { | |
3810 | r = bus_wait_for_units_add_unit(w, *name, BUS_WAIT_REFFED|BUS_WAIT_FOR_MAINTENANCE_END, NULL, NULL); | |
3811 | if (r < 0) | |
3812 | return log_error_errno(r, "Failed to watch unit %s: %m", *name); | |
3813 | } | |
3814 | } | |
3815 | ||
3816 | r = bus_wait_for_units_run(w); | |
3817 | if (r < 0) | |
3818 | return log_error_errno(r, "Failed to wait for units: %m"); | |
3819 | if (r == BUS_WAIT_FAILURE) | |
3820 | ret = EXIT_FAILURE; | |
3821 | ||
3822 | return ret; | |
3823 | } | |
3824 | ||
582a507f | 3825 | typedef struct ExecStatusInfo { |
0129173a LP |
3826 | char *name; |
3827 | ||
582a507f LP |
3828 | char *path; |
3829 | char **argv; | |
3830 | ||
b708e7ce LP |
3831 | bool ignore; |
3832 | ||
582a507f LP |
3833 | usec_t start_timestamp; |
3834 | usec_t exit_timestamp; | |
3835 | pid_t pid; | |
3836 | int code; | |
3837 | int status; | |
3838 | ||
b3d59367 AZ |
3839 | ExecCommandFlags flags; |
3840 | ||
582a507f LP |
3841 | LIST_FIELDS(struct ExecStatusInfo, exec); |
3842 | } ExecStatusInfo; | |
3843 | ||
3844 | static void exec_status_info_free(ExecStatusInfo *i) { | |
3845 | assert(i); | |
3846 | ||
0129173a | 3847 | free(i->name); |
582a507f LP |
3848 | free(i->path); |
3849 | strv_free(i->argv); | |
3850 | free(i); | |
3851 | } | |
3852 | ||
b3d59367 AZ |
3853 | static int exec_status_info_deserialize(sd_bus_message *m, ExecStatusInfo *i, bool is_ex_prop) { |
3854 | _cleanup_strv_free_ char **ex_opts = NULL; | |
b21a0ef8 | 3855 | uint64_t start_timestamp, exit_timestamp, start_timestamp_monotonic, exit_timestamp_monotonic; |
f459b602 | 3856 | const char *path; |
582a507f LP |
3857 | uint32_t pid; |
3858 | int32_t code, status; | |
f459b602 | 3859 | int ignore, r; |
582a507f | 3860 | |
f459b602 | 3861 | assert(m); |
582a507f | 3862 | assert(i); |
582a507f | 3863 | |
b3d59367 | 3864 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_STRUCT, is_ex_prop ? "sasasttttuii" : "sasbttttuii"); |
f459b602 MAP |
3865 | if (r < 0) |
3866 | return bus_log_parse_error(r); | |
3867 | else if (r == 0) | |
3868 | return 0; | |
582a507f | 3869 | |
f459b602 MAP |
3870 | r = sd_bus_message_read(m, "s", &path); |
3871 | if (r < 0) | |
3872 | return bus_log_parse_error(r); | |
582a507f | 3873 | |
f84190d8 LP |
3874 | i->path = strdup(path); |
3875 | if (!i->path) | |
f459b602 | 3876 | return log_oom(); |
582a507f | 3877 | |
f459b602 MAP |
3878 | r = sd_bus_message_read_strv(m, &i->argv); |
3879 | if (r < 0) | |
3880 | return bus_log_parse_error(r); | |
3881 | ||
b3d59367 AZ |
3882 | r = is_ex_prop ? sd_bus_message_read_strv(m, &ex_opts) : sd_bus_message_read(m, "b", &ignore); |
3883 | if (r < 0) | |
3884 | return bus_log_parse_error(r); | |
3885 | ||
f459b602 | 3886 | r = sd_bus_message_read(m, |
b3d59367 | 3887 | "ttttuii", |
f459b602 MAP |
3888 | &start_timestamp, &start_timestamp_monotonic, |
3889 | &exit_timestamp, &exit_timestamp_monotonic, | |
3890 | &pid, | |
3891 | &code, &status); | |
3892 | if (r < 0) | |
3893 | return bus_log_parse_error(r); | |
582a507f | 3894 | |
b3d59367 AZ |
3895 | if (is_ex_prop) { |
3896 | r = exec_command_flags_from_strv(ex_opts, &i->flags); | |
3897 | if (r < 0) | |
3898 | return log_error_errno(r, "Failed to convert strv to ExecCommandFlags: %m"); | |
3899 | ||
3900 | i->ignore = FLAGS_SET(i->flags, EXEC_COMMAND_IGNORE_FAILURE); | |
3901 | } else | |
3902 | i->ignore = ignore; | |
3903 | ||
582a507f LP |
3904 | i->start_timestamp = (usec_t) start_timestamp; |
3905 | i->exit_timestamp = (usec_t) exit_timestamp; | |
3906 | i->pid = (pid_t) pid; | |
3907 | i->code = code; | |
3908 | i->status = status; | |
3909 | ||
f459b602 MAP |
3910 | r = sd_bus_message_exit_container(m); |
3911 | if (r < 0) | |
3912 | return bus_log_parse_error(r); | |
3913 | ||
3914 | return 1; | |
582a507f LP |
3915 | } |
3916 | ||
d5db7fe6 WC |
3917 | typedef struct UnitCondition { |
3918 | char *name; | |
b1ed76ae | 3919 | char *param; |
d5db7fe6 WC |
3920 | bool trigger; |
3921 | bool negate; | |
d5db7fe6 WC |
3922 | int tristate; |
3923 | ||
b1ed76ae | 3924 | LIST_FIELDS(struct UnitCondition, conditions); |
d5db7fe6 WC |
3925 | } UnitCondition; |
3926 | ||
3927 | static void unit_condition_free(UnitCondition *c) { | |
9bb71940 ZJS |
3928 | if (!c) |
3929 | return; | |
d5db7fe6 WC |
3930 | |
3931 | free(c->name); | |
3932 | free(c->param); | |
3933 | free(c); | |
3934 | } | |
3935 | ||
9bb71940 ZJS |
3936 | DEFINE_TRIVIAL_CLEANUP_FUNC(UnitCondition*, unit_condition_free); |
3937 | ||
61cbdc4b LP |
3938 | typedef struct UnitStatusInfo { |
3939 | const char *id; | |
3940 | const char *load_state; | |
3941 | const char *active_state; | |
d9e45bc3 | 3942 | const char *freezer_state; |
61cbdc4b | 3943 | const char *sub_state; |
a4375746 | 3944 | const char *unit_file_state; |
d2dc52db | 3945 | const char *unit_file_preset; |
61cbdc4b LP |
3946 | |
3947 | const char *description; | |
4a9e2fff | 3948 | const char *following; |
61cbdc4b | 3949 | |
49dbfa7b LP |
3950 | char **documentation; |
3951 | ||
1b64d026 LP |
3952 | const char *fragment_path; |
3953 | const char *source_path; | |
4ad49000 | 3954 | const char *control_group; |
61cbdc4b | 3955 | |
76d14b87 OS |
3956 | char **dropin_paths; |
3957 | ||
965c5d1d KK |
3958 | char **triggered_by; |
3959 | char **triggers; | |
3960 | ||
9f39404c | 3961 | const char *load_error; |
f42806df | 3962 | const char *result; |
9f39404c | 3963 | |
584be568 | 3964 | usec_t inactive_exit_timestamp; |
df50185b | 3965 | usec_t inactive_exit_timestamp_monotonic; |
584be568 LP |
3966 | usec_t active_enter_timestamp; |
3967 | usec_t active_exit_timestamp; | |
3968 | usec_t inactive_enter_timestamp; | |
3969 | ||
45fb0699 | 3970 | bool need_daemon_reload; |
055ef36b | 3971 | bool transient; |
45fb0699 | 3972 | |
61cbdc4b LP |
3973 | /* Service */ |
3974 | pid_t main_pid; | |
3975 | pid_t control_pid; | |
3976 | const char *status_text; | |
175728c4 | 3977 | const char *pid_file; |
d06dacd0 | 3978 | bool running:1; |
b4af5a80 | 3979 | int status_errno; |
61cbdc4b LP |
3980 | |
3981 | usec_t start_timestamp; | |
3982 | usec_t exit_timestamp; | |
3983 | ||
3984 | int exit_code, exit_status; | |
3985 | ||
d93dda3a LP |
3986 | const char *log_namespace; |
3987 | ||
90bbc946 LP |
3988 | usec_t condition_timestamp; |
3989 | bool condition_result; | |
b1ed76ae | 3990 | LIST_HEAD(UnitCondition, conditions); |
59fccdc5 LP |
3991 | |
3992 | usec_t assert_timestamp; | |
3993 | bool assert_result; | |
3994 | bool failed_assert_trigger; | |
3995 | bool failed_assert_negate; | |
3996 | const char *failed_assert; | |
3997 | const char *failed_assert_parameter; | |
601b0842 GS |
3998 | usec_t next_elapse_real; |
3999 | usec_t next_elapse_monotonic; | |
90bbc946 | 4000 | |
61cbdc4b LP |
4001 | /* Socket */ |
4002 | unsigned n_accepted; | |
4003 | unsigned n_connections; | |
a98f7575 | 4004 | unsigned n_refused; |
b8131a87 | 4005 | bool accept; |
61cbdc4b | 4006 | |
13160134 | 4007 | /* Pairs of type, path */ |
67419600 OS |
4008 | char **listen; |
4009 | ||
61cbdc4b LP |
4010 | /* Device */ |
4011 | const char *sysfs_path; | |
4012 | ||
4013 | /* Mount, Automount */ | |
4014 | const char *where; | |
4015 | ||
4016 | /* Swap */ | |
4017 | const char *what; | |
582a507f | 4018 | |
934277fe LP |
4019 | /* CGroup */ |
4020 | uint64_t memory_current; | |
48422635 | 4021 | uint64_t memory_min; |
da4d897e TH |
4022 | uint64_t memory_low; |
4023 | uint64_t memory_high; | |
4024 | uint64_t memory_max; | |
96e131ea | 4025 | uint64_t memory_swap_max; |
934277fe | 4026 | uint64_t memory_limit; |
5ad096b3 | 4027 | uint64_t cpu_usage_nsec; |
03a7b521 LP |
4028 | uint64_t tasks_current; |
4029 | uint64_t tasks_max; | |
0e97c93f DM |
4030 | uint64_t ip_ingress_bytes; |
4031 | uint64_t ip_egress_bytes; | |
7a5de3f9 LP |
4032 | uint64_t io_read_bytes; |
4033 | uint64_t io_write_bytes; | |
0e97c93f | 4034 | |
7ad5439e | 4035 | uint64_t default_memory_min; |
c52db42b CD |
4036 | uint64_t default_memory_low; |
4037 | ||
582a507f | 4038 | LIST_HEAD(ExecStatusInfo, exec); |
61cbdc4b LP |
4039 | } UnitStatusInfo; |
4040 | ||
a7335518 ZJS |
4041 | static void unit_status_info_free(UnitStatusInfo *info) { |
4042 | ExecStatusInfo *p; | |
4043 | UnitCondition *c; | |
4044 | ||
4045 | strv_free(info->documentation); | |
4046 | strv_free(info->dropin_paths); | |
965c5d1d KK |
4047 | strv_free(info->triggered_by); |
4048 | strv_free(info->triggers); | |
a7335518 ZJS |
4049 | strv_free(info->listen); |
4050 | ||
4051 | while ((c = info->conditions)) { | |
4052 | LIST_REMOVE(conditions, info->conditions, c); | |
4053 | unit_condition_free(c); | |
4054 | } | |
4055 | ||
4056 | while ((p = info->exec)) { | |
4057 | LIST_REMOVE(exec, info->exec, p); | |
4058 | exec_status_info_free(p); | |
4059 | } | |
4060 | } | |
4061 | ||
965c5d1d KK |
4062 | static void format_active_state(const char *active_state, const char **active_on, const char **active_off) { |
4063 | if (streq_ptr(active_state, "failed")) { | |
4064 | *active_on = ansi_highlight_red(); | |
4065 | *active_off = ansi_normal(); | |
4066 | } else if (STRPTR_IN_SET(active_state, "active", "reloading")) { | |
4067 | *active_on = ansi_highlight_green(); | |
4068 | *active_off = ansi_normal(); | |
4069 | } else | |
4070 | *active_on = *active_off = ""; | |
4071 | } | |
4072 | ||
f459b602 | 4073 | static void print_status_info( |
291d565a | 4074 | sd_bus *bus, |
f459b602 MAP |
4075 | UnitStatusInfo *i, |
4076 | bool *ellipsized) { | |
4077 | ||
4d9685be | 4078 | char since1[FORMAT_TIMESTAMP_RELATIVE_MAX], since2[FORMAT_TIMESTAMP_MAX]; |
d9e45bc3 | 4079 | const char *s1, *s2, *active_on, *active_off, *on, *off, *ss, *fs; |
c7080257 LP |
4080 | _cleanup_free_ char *formatted_path = NULL; |
4081 | ExecStatusInfo *p; | |
584be568 | 4082 | usec_t timestamp; |
1b64d026 | 4083 | const char *path; |
13160134 | 4084 | char **t, **t2; |
291d565a | 4085 | int r; |
582a507f | 4086 | |
61cbdc4b LP |
4087 | assert(i); |
4088 | ||
4089 | /* This shows pretty information about a unit. See | |
4090 | * print_property() for a low-level property printer */ | |
4091 | ||
965c5d1d | 4092 | format_active_state(i->active_state, &active_on, &active_off); |
b0d14c69 | 4093 | |
9a6f746f | 4094 | printf("%s%s%s %s", active_on, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE), active_off, strna(i->id)); |
61cbdc4b LP |
4095 | |
4096 | if (i->description && !streq_ptr(i->id, i->description)) | |
4097 | printf(" - %s", i->description); | |
4098 | ||
4099 | printf("\n"); | |
4100 | ||
4a9e2fff | 4101 | if (i->following) |
856323c9 | 4102 | printf(" Follow: unit currently follows state of %s\n", i->following); |
4a9e2fff | 4103 | |
c4555ad8 | 4104 | if (STRPTR_IN_SET(i->load_state, "error", "not-found", "bad-setting")) { |
0b5a519c | 4105 | on = ansi_highlight_red(); |
1fc464f6 | 4106 | off = ansi_normal(); |
c31b4423 LP |
4107 | } else |
4108 | on = off = ""; | |
4109 | ||
c7080257 LP |
4110 | path = i->source_path ?: i->fragment_path; |
4111 | if (path && terminal_urlify_path(path, NULL, &formatted_path) >= 0) | |
4112 | path = formatted_path; | |
1b64d026 | 4113 | |
9a0abfa8 | 4114 | if (!isempty(i->load_error)) |
0d588dea | 4115 | printf(" Loaded: %s%s%s (Reason: %s)\n", |
856323c9 | 4116 | on, strna(i->load_state), off, i->load_error); |
31b8895a ZJS |
4117 | else if (path && !isempty(i->unit_file_state)) { |
4118 | bool show_preset = !isempty(i->unit_file_preset) && | |
4119 | show_preset_for_state(unit_file_state_from_string(i->unit_file_state)); | |
4120 | ||
4121 | printf(" Loaded: %s%s%s (%s; %s%s%s)\n", | |
4122 | on, strna(i->load_state), off, | |
4123 | path, | |
4124 | i->unit_file_state, | |
4125 | show_preset ? "; vendor preset: " : "", | |
4126 | show_preset ? i->unit_file_preset : ""); | |
4127 | ||
4128 | } else if (path) | |
0d588dea | 4129 | printf(" Loaded: %s%s%s (%s)\n", |
856323c9 | 4130 | on, strna(i->load_state), off, path); |
61cbdc4b | 4131 | else |
0d588dea | 4132 | printf(" Loaded: %s%s%s\n", |
856323c9 | 4133 | on, strna(i->load_state), off); |
61cbdc4b | 4134 | |
055ef36b | 4135 | if (i->transient) |
0d588dea | 4136 | printf(" Transient: yes\n"); |
055ef36b | 4137 | |
76d14b87 | 4138 | if (!strv_isempty(i->dropin_paths)) { |
f459b602 | 4139 | _cleanup_free_ char *dir = NULL; |
76d14b87 | 4140 | bool last = false; |
f459b602 | 4141 | char ** dropin; |
76d14b87 OS |
4142 | |
4143 | STRV_FOREACH(dropin, i->dropin_paths) { | |
c7080257 LP |
4144 | _cleanup_free_ char *dropin_formatted = NULL; |
4145 | const char *df; | |
4146 | ||
eb2c3192 | 4147 | if (!dir || last) { |
0d588dea KK |
4148 | printf(dir ? " " : |
4149 | " Drop-In: "); | |
76d14b87 | 4150 | |
97b11eed | 4151 | dir = mfree(dir); |
76d14b87 | 4152 | |
5f311f8c LP |
4153 | dir = dirname_malloc(*dropin); |
4154 | if (!dir) { | |
76d14b87 OS |
4155 | log_oom(); |
4156 | return; | |
4157 | } | |
4158 | ||
eb2c3192 | 4159 | printf("%s\n" |
0d588dea | 4160 | " %s", dir, |
9a6f746f | 4161 | special_glyph(SPECIAL_GLYPH_TREE_RIGHT)); |
76d14b87 OS |
4162 | } |
4163 | ||
4164 | last = ! (*(dropin + 1) && startswith(*(dropin + 1), dir)); | |
4165 | ||
c7080257 LP |
4166 | if (terminal_urlify_path(*dropin, basename(*dropin), &dropin_formatted) >= 0) |
4167 | df = dropin_formatted; | |
4168 | else | |
4169 | df = *dropin; | |
4170 | ||
4171 | printf("%s%s", df, last ? "\n" : ", "); | |
76d14b87 | 4172 | } |
76d14b87 OS |
4173 | } |
4174 | ||
2ee68f72 | 4175 | ss = streq_ptr(i->active_state, i->sub_state) ? NULL : i->sub_state; |
2ee68f72 | 4176 | if (ss) |
266dd555 | 4177 | printf(" Active: %s%s (%s)%s", |
b0d14c69 | 4178 | active_on, strna(i->active_state), ss, active_off); |
2ee68f72 | 4179 | else |
0d588dea | 4180 | printf(" Active: %s%s%s", |
b0d14c69 | 4181 | active_on, strna(i->active_state), active_off); |
61cbdc4b | 4182 | |
d9e45bc3 MS |
4183 | fs = !isempty(i->freezer_state) && !streq(i->freezer_state, "running") ? i->freezer_state : NULL; |
4184 | if (fs) | |
38fee619 | 4185 | printf(" %s(%s)%s", ansi_highlight_yellow(), fs, ansi_normal()); |
d9e45bc3 | 4186 | |
f42806df LP |
4187 | if (!isempty(i->result) && !streq(i->result, "success")) |
4188 | printf(" (Result: %s)", i->result); | |
4189 | ||
1cf03a4f ZJS |
4190 | timestamp = STRPTR_IN_SET(i->active_state, "active", "reloading") ? i->active_enter_timestamp : |
4191 | STRPTR_IN_SET(i->active_state, "inactive", "failed") ? i->inactive_enter_timestamp : | |
4192 | STRPTR_IN_SET(i->active_state, "activating") ? i->inactive_exit_timestamp : | |
4193 | i->active_exit_timestamp; | |
584be568 | 4194 | |
bbb8486e | 4195 | s1 = format_timestamp_relative(since1, sizeof(since1), timestamp); |
584be568 LP |
4196 | s2 = format_timestamp(since2, sizeof(since2), timestamp); |
4197 | ||
4198 | if (s1) | |
538da63d | 4199 | printf(" since %s; %s\n", s2, s1); |
584be568 | 4200 | else if (s2) |
538da63d | 4201 | printf(" since %s\n", s2); |
584be568 LP |
4202 | else |
4203 | printf("\n"); | |
4204 | ||
bc9aa96b ZJS |
4205 | STRV_FOREACH(t, i->triggered_by) { |
4206 | UnitActiveState state = _UNIT_ACTIVE_STATE_INVALID; | |
965c5d1d | 4207 | |
bc9aa96b ZJS |
4208 | (void) get_state_one_unit(bus, *t, &state); |
4209 | format_active_state(unit_active_state_to_string(state), &on, &off); | |
965c5d1d | 4210 | |
bc9aa96b ZJS |
4211 | printf("%s %s%s%s %s\n", |
4212 | t == i->triggered_by ? "TriggeredBy:" : " ", | |
4213 | on, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE), off, | |
4214 | *t); | |
965c5d1d KK |
4215 | } |
4216 | ||
601b0842 GS |
4217 | if (endswith(i->id, ".timer")) { |
4218 | char tstamp1[FORMAT_TIMESTAMP_RELATIVE_MAX], | |
4219 | tstamp2[FORMAT_TIMESTAMP_MAX]; | |
4d9685be | 4220 | const char *next_rel_time, *next_time; |
601b0842 GS |
4221 | dual_timestamp nw, next = {i->next_elapse_real, |
4222 | i->next_elapse_monotonic}; | |
4223 | usec_t next_elapse; | |
4224 | ||
0d588dea | 4225 | printf(" Trigger: "); |
601b0842 GS |
4226 | |
4227 | dual_timestamp_get(&nw); | |
4228 | next_elapse = calc_next_elapse(&nw, &next); | |
4d9685be ZJS |
4229 | next_rel_time = format_timestamp_relative(tstamp1, sizeof tstamp1, next_elapse); |
4230 | next_time = format_timestamp(tstamp2, sizeof tstamp2, next_elapse); | |
601b0842 GS |
4231 | |
4232 | if (next_time && next_rel_time) | |
4233 | printf("%s; %s\n", next_time, next_rel_time); | |
4234 | else | |
4235 | printf("n/a\n"); | |
4236 | } | |
4237 | ||
bc9aa96b ZJS |
4238 | STRV_FOREACH(t, i->triggers) { |
4239 | UnitActiveState state = _UNIT_ACTIVE_STATE_INVALID; | |
965c5d1d | 4240 | |
bc9aa96b ZJS |
4241 | (void) get_state_one_unit(bus, *t, &state); |
4242 | format_active_state(unit_active_state_to_string(state), &on, &off); | |
965c5d1d | 4243 | |
bc9aa96b ZJS |
4244 | printf("%s %s%s%s %s\n", |
4245 | t == i->triggers ? " Triggers:" : " ", | |
4246 | on, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE), off, | |
4247 | *t); | |
965c5d1d KK |
4248 | } |
4249 | ||
90bbc946 | 4250 | if (!i->condition_result && i->condition_timestamp > 0) { |
d5db7fe6 WC |
4251 | UnitCondition *c; |
4252 | int n = 0; | |
4253 | ||
bbb8486e | 4254 | s1 = format_timestamp_relative(since1, sizeof(since1), i->condition_timestamp); |
90bbc946 LP |
4255 | s2 = format_timestamp(since2, sizeof(since2), i->condition_timestamp); |
4256 | ||
0d588dea | 4257 | printf(" Condition: start %scondition failed%s at %s%s%s\n", |
1fc464f6 | 4258 | ansi_highlight_yellow(), ansi_normal(), |
5cfee414 | 4259 | s2, s1 ? "; " : "", strempty(s1)); |
d5db7fe6 | 4260 | |
b1ed76ae | 4261 | LIST_FOREACH(conditions, c, i->conditions) |
d5db7fe6 WC |
4262 | if (c->tristate < 0) |
4263 | n++; | |
d5db7fe6 | 4264 | |
b1ed76ae ZJS |
4265 | LIST_FOREACH(conditions, c, i->conditions) |
4266 | if (c->tristate < 0) | |
0d588dea | 4267 | printf(" %s %s=%s%s%s was not met\n", |
9a6f746f | 4268 | --n ? special_glyph(SPECIAL_GLYPH_TREE_BRANCH) : special_glyph(SPECIAL_GLYPH_TREE_RIGHT), |
b1ed76ae ZJS |
4269 | c->name, |
4270 | c->trigger ? "|" : "", | |
4271 | c->negate ? "!" : "", | |
4272 | c->param); | |
59fccdc5 LP |
4273 | } |
4274 | ||
4275 | if (!i->assert_result && i->assert_timestamp > 0) { | |
4276 | s1 = format_timestamp_relative(since1, sizeof(since1), i->assert_timestamp); | |
4277 | s2 = format_timestamp(since2, sizeof(since2), i->assert_timestamp); | |
4278 | ||
0d588dea | 4279 | printf(" Assert: start %sassertion failed%s at %s%s%s\n", |
1fc464f6 | 4280 | ansi_highlight_red(), ansi_normal(), |
5cfee414 | 4281 | s2, s1 ? "; " : "", strempty(s1)); |
59fccdc5 | 4282 | if (i->failed_assert_trigger) |
0d588dea | 4283 | printf(" none of the trigger assertions were met\n"); |
59fccdc5 | 4284 | else if (i->failed_assert) |
0d588dea | 4285 | printf(" %s=%s%s was not met\n", |
59fccdc5 LP |
4286 | i->failed_assert, |
4287 | i->failed_assert_negate ? "!" : "", | |
4288 | i->failed_assert_parameter); | |
90bbc946 LP |
4289 | } |
4290 | ||
61cbdc4b | 4291 | if (i->sysfs_path) |
0d588dea | 4292 | printf(" Device: %s\n", i->sysfs_path); |
9feeba4b | 4293 | if (i->where) |
0d588dea | 4294 | printf(" Where: %s\n", i->where); |
9feeba4b | 4295 | if (i->what) |
0d588dea | 4296 | printf(" What: %s\n", i->what); |
49dbfa7b | 4297 | |
78c7d20e LP |
4298 | STRV_FOREACH(t, i->documentation) { |
4299 | _cleanup_free_ char *formatted = NULL; | |
4300 | const char *q; | |
4301 | ||
4302 | if (terminal_urlify(*t, NULL, &formatted) >= 0) | |
4303 | q = formatted; | |
4304 | else | |
4305 | q = *t; | |
4306 | ||
0d588dea | 4307 | printf(" %*s %s\n", 9, t == i->documentation ? "Docs:" : "", q); |
78c7d20e | 4308 | } |
49dbfa7b | 4309 | |
13160134 | 4310 | STRV_FOREACH_PAIR(t, t2, i->listen) |
0d588dea | 4311 | printf(" %*s %s (%s)\n", 9, t == i->listen ? "Listen:" : "", *t2, *t); |
67419600 | 4312 | |
a98f7575 | 4313 | if (i->accept) { |
0d588dea | 4314 | printf(" Accepted: %u; Connected: %u;", i->n_accepted, i->n_connections); |
a98f7575 | 4315 | if (i->n_refused) |
0d588dea | 4316 | printf(" Refused: %u", i->n_refused); |
a98f7575 MM |
4317 | printf("\n"); |
4318 | } | |
4319 | ||
582a507f | 4320 | LIST_FOREACH(exec, p, i->exec) { |
13160134 | 4321 | _cleanup_free_ char *argv = NULL; |
9a57c629 | 4322 | bool good; |
582a507f LP |
4323 | |
4324 | /* Only show exited processes here */ | |
4325 | if (p->code == 0) | |
4326 | continue; | |
4327 | ||
e5c85244 AZ |
4328 | /* Don't print ExecXYZEx= properties here since it will appear as a |
4329 | * duplicate of the non-Ex= variant. */ | |
4330 | if (endswith(p->name, "Ex")) | |
4331 | continue; | |
4332 | ||
13160134 | 4333 | argv = strv_join(p->argv, " "); |
0d588dea | 4334 | printf(" Process: "PID_FMT" %s=%s ", p->pid, p->name, strna(argv)); |
582a507f | 4335 | |
1f0958f6 | 4336 | good = is_clean_exit(p->code, p->status, EXIT_CLEAN_DAEMON, NULL); |
9a57c629 | 4337 | if (!good) { |
0b5a519c | 4338 | on = ansi_highlight_red(); |
1fc464f6 | 4339 | off = ansi_normal(); |
9a57c629 LP |
4340 | } else |
4341 | on = off = ""; | |
4342 | ||
4343 | printf("%s(code=%s, ", on, sigchld_code_to_string(p->code)); | |
4344 | ||
d06dacd0 LP |
4345 | if (p->code == CLD_EXITED) { |
4346 | const char *c; | |
4347 | ||
582a507f | 4348 | printf("status=%i", p->status); |
d06dacd0 | 4349 | |
e04ed6db | 4350 | c = exit_status_to_string(p->status, EXIT_STATUS_LIBC | EXIT_STATUS_SYSTEMD); |
1b64d026 | 4351 | if (c) |
d06dacd0 LP |
4352 | printf("/%s", c); |
4353 | ||
4354 | } else | |
582a507f | 4355 | printf("signal=%s", signal_to_string(p->status)); |
9a57c629 LP |
4356 | |
4357 | printf(")%s\n", off); | |
4358 | ||
582a507f LP |
4359 | if (i->main_pid == p->pid && |
4360 | i->start_timestamp == p->start_timestamp && | |
4361 | i->exit_timestamp == p->start_timestamp) | |
4362 | /* Let's not show this twice */ | |
4363 | i->main_pid = 0; | |
4364 | ||
4365 | if (p->pid == i->control_pid) | |
4366 | i->control_pid = 0; | |
4367 | } | |
4368 | ||
61cbdc4b | 4369 | if (i->main_pid > 0 || i->control_pid > 0) { |
61cbdc4b | 4370 | if (i->main_pid > 0) { |
0d588dea | 4371 | printf(" Main PID: "PID_FMT, i->main_pid); |
61cbdc4b LP |
4372 | |
4373 | if (i->running) { | |
a081b9ce LP |
4374 | |
4375 | if (arg_transport == BUS_TRANSPORT_LOCAL) { | |
4376 | _cleanup_free_ char *comm = NULL; | |
4377 | ||
4378 | (void) get_process_comm(i->main_pid, &comm); | |
4379 | if (comm) | |
4380 | printf(" (%s)", comm); | |
4381 | } | |
4382 | ||
6d4fc029 | 4383 | } else if (i->exit_code > 0) { |
61cbdc4b LP |
4384 | printf(" (code=%s, ", sigchld_code_to_string(i->exit_code)); |
4385 | ||
d06dacd0 LP |
4386 | if (i->exit_code == CLD_EXITED) { |
4387 | const char *c; | |
4388 | ||
61cbdc4b | 4389 | printf("status=%i", i->exit_status); |
d06dacd0 | 4390 | |
e1714f02 | 4391 | c = exit_status_to_string(i->exit_status, |
e04ed6db | 4392 | EXIT_STATUS_LIBC | EXIT_STATUS_SYSTEMD); |
1b64d026 | 4393 | if (c) |
d06dacd0 LP |
4394 | printf("/%s", c); |
4395 | ||
4396 | } else | |
582a507f | 4397 | printf("signal=%s", signal_to_string(i->exit_status)); |
6d4fc029 LP |
4398 | printf(")"); |
4399 | } | |
13160134 | 4400 | } |
61cbdc4b LP |
4401 | |
4402 | if (i->control_pid > 0) { | |
13160134 | 4403 | _cleanup_free_ char *c = NULL; |
61cbdc4b | 4404 | |
1089dcd4 LP |
4405 | if (i->main_pid > 0) |
4406 | fputs("; Control PID: ", stdout); | |
4407 | else | |
4408 | fputs("Cntrl PID: ", stdout); /* if first in column, abbreviated so it fits alignment */ | |
4409 | ||
4410 | printf(PID_FMT, i->control_pid); | |
61cbdc4b | 4411 | |
a081b9ce LP |
4412 | if (arg_transport == BUS_TRANSPORT_LOCAL) { |
4413 | (void) get_process_comm(i->control_pid, &c); | |
4414 | if (c) | |
4415 | printf(" (%s)", c); | |
4416 | } | |
61cbdc4b LP |
4417 | } |
4418 | ||
4419 | printf("\n"); | |
4420 | } | |
4421 | ||
17bb7382 | 4422 | if (i->status_text) |
0d588dea | 4423 | printf(" Status: \"%s\"\n", i->status_text); |
b4af5a80 | 4424 | if (i->status_errno > 0) |
0d588dea | 4425 | printf(" Error: %i (%s)\n", i->status_errno, strerror_safe(i->status_errno)); |
17bb7382 | 4426 | |
0e97c93f DM |
4427 | if (i->ip_ingress_bytes != (uint64_t) -1 && i->ip_egress_bytes != (uint64_t) -1) { |
4428 | char buf_in[FORMAT_BYTES_MAX], buf_out[FORMAT_BYTES_MAX]; | |
4429 | ||
0d588dea | 4430 | printf(" IP: %s in, %s out\n", |
0e97c93f DM |
4431 | format_bytes(buf_in, sizeof(buf_in), i->ip_ingress_bytes), |
4432 | format_bytes(buf_out, sizeof(buf_out), i->ip_egress_bytes)); | |
4433 | } | |
4434 | ||
7a5de3f9 LP |
4435 | if (i->io_read_bytes != UINT64_MAX && i->io_write_bytes != UINT64_MAX) { |
4436 | char buf_in[FORMAT_BYTES_MAX], buf_out[FORMAT_BYTES_MAX]; | |
4437 | ||
0d588dea | 4438 | printf(" IO: %s read, %s written\n", |
7a5de3f9 LP |
4439 | format_bytes(buf_in, sizeof(buf_in), i->io_read_bytes), |
4440 | format_bytes(buf_out, sizeof(buf_out), i->io_write_bytes)); | |
4441 | } | |
4442 | ||
03a7b521 | 4443 | if (i->tasks_current != (uint64_t) -1) { |
0d588dea | 4444 | printf(" Tasks: %" PRIu64, i->tasks_current); |
03a7b521 LP |
4445 | |
4446 | if (i->tasks_max != (uint64_t) -1) | |
2a6736dd | 4447 | printf(" (limit: %" PRIu64 ")\n", i->tasks_max); |
03a7b521 LP |
4448 | else |
4449 | printf("\n"); | |
4450 | } | |
4451 | ||
934277fe LP |
4452 | if (i->memory_current != (uint64_t) -1) { |
4453 | char buf[FORMAT_BYTES_MAX]; | |
4454 | ||
0d588dea | 4455 | printf(" Memory: %s", format_bytes(buf, sizeof(buf), i->memory_current)); |
934277fe | 4456 | |
48422635 TH |
4457 | if (i->memory_min > 0 || i->memory_low > 0 || |
4458 | i->memory_high != CGROUP_LIMIT_MAX || i->memory_max != CGROUP_LIMIT_MAX || | |
4459 | i->memory_swap_max != CGROUP_LIMIT_MAX || | |
da4d897e TH |
4460 | i->memory_limit != CGROUP_LIMIT_MAX) { |
4461 | const char *prefix = ""; | |
4462 | ||
4463 | printf(" ("); | |
48422635 | 4464 | if (i->memory_min > 0) { |
e0e2112f | 4465 | printf("%smin: %s", prefix, format_bytes_cgroup_protection(buf, sizeof(buf), i->memory_min)); |
48422635 TH |
4466 | prefix = " "; |
4467 | } | |
da4d897e | 4468 | if (i->memory_low > 0) { |
e0e2112f | 4469 | printf("%slow: %s", prefix, format_bytes_cgroup_protection(buf, sizeof(buf), i->memory_low)); |
da4d897e TH |
4470 | prefix = " "; |
4471 | } | |
4472 | if (i->memory_high != CGROUP_LIMIT_MAX) { | |
4473 | printf("%shigh: %s", prefix, format_bytes(buf, sizeof(buf), i->memory_high)); | |
4474 | prefix = " "; | |
4475 | } | |
4476 | if (i->memory_max != CGROUP_LIMIT_MAX) { | |
4477 | printf("%smax: %s", prefix, format_bytes(buf, sizeof(buf), i->memory_max)); | |
4478 | prefix = " "; | |
4479 | } | |
96e131ea WC |
4480 | if (i->memory_swap_max != CGROUP_LIMIT_MAX) { |
4481 | printf("%sswap max: %s", prefix, format_bytes(buf, sizeof(buf), i->memory_swap_max)); | |
4482 | prefix = " "; | |
4483 | } | |
da4d897e TH |
4484 | if (i->memory_limit != CGROUP_LIMIT_MAX) { |
4485 | printf("%slimit: %s", prefix, format_bytes(buf, sizeof(buf), i->memory_limit)); | |
4486 | prefix = " "; | |
4487 | } | |
4488 | printf(")"); | |
4489 | } | |
4490 | printf("\n"); | |
934277fe LP |
4491 | } |
4492 | ||
5ad096b3 LP |
4493 | if (i->cpu_usage_nsec != (uint64_t) -1) { |
4494 | char buf[FORMAT_TIMESPAN_MAX]; | |
0d588dea | 4495 | printf(" CPU: %s\n", format_timespan(buf, sizeof(buf), i->cpu_usage_nsec / NSEC_PER_USEC, USEC_PER_MSEC)); |
5ad096b3 LP |
4496 | } |
4497 | ||
51e22f88 | 4498 | if (i->control_group) { |
291d565a | 4499 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
a434236a | 4500 | static const char prefix[] = " "; |
ab35fb1b LP |
4501 | unsigned c; |
4502 | ||
0d588dea | 4503 | printf(" CGroup: %s\n", i->control_group); |
51e22f88 | 4504 | |
291d565a LP |
4505 | c = columns(); |
4506 | if (c > sizeof(prefix) - 1) | |
4507 | c -= sizeof(prefix) - 1; | |
4508 | else | |
4509 | c = 0; | |
ab35fb1b | 4510 | |
291d565a LP |
4511 | r = unit_show_processes(bus, i->id, i->control_group, prefix, c, get_output_flags(), &error); |
4512 | if (r == -EBADR) { | |
b69d29ce LP |
4513 | unsigned k = 0; |
4514 | pid_t extra[2]; | |
4515 | ||
291d565a | 4516 | /* Fallback for older systemd versions where the GetUnitProcesses() call is not yet available */ |
ab35fb1b | 4517 | |
b69d29ce LP |
4518 | if (i->main_pid > 0) |
4519 | extra[k++] = i->main_pid; | |
4520 | ||
4521 | if (i->control_pid > 0) | |
4522 | extra[k++] = i->control_pid; | |
4523 | ||
0ff308c8 | 4524 | show_cgroup_and_extra(SYSTEMD_CGROUP_CONTROLLER, i->control_group, prefix, c, extra, k, get_output_flags()); |
291d565a | 4525 | } else if (r < 0) |
107c2051 ZJS |
4526 | log_warning_errno(r, "Failed to dump process list for '%s', ignoring: %s", |
4527 | i->id, bus_error_message(&error, r)); | |
c59760ee | 4528 | } |
45fb0699 | 4529 | |
ece174c5 | 4530 | if (i->id && arg_transport == BUS_TRANSPORT_LOCAL) |
3c756001 LP |
4531 | show_journal_by_unit( |
4532 | stdout, | |
4533 | i->id, | |
d93dda3a | 4534 | i->log_namespace, |
3c756001 LP |
4535 | arg_output, |
4536 | 0, | |
4537 | i->inactive_exit_timestamp_monotonic, | |
4538 | arg_lines, | |
4539 | getuid(), | |
4540 | get_output_flags() | OUTPUT_BEGIN_NEWLINE, | |
4541 | SD_JOURNAL_LOCAL_ONLY, | |
4542 | arg_scope == UNIT_FILE_SYSTEM, | |
4543 | ellipsized); | |
86aa7ba4 | 4544 | |
45fb0699 | 4545 | if (i->need_daemon_reload) |
3f36991e | 4546 | warn_unit_file_changed(i->id); |
61cbdc4b LP |
4547 | } |
4548 | ||
b43f208f | 4549 | static void show_unit_help(UnitStatusInfo *i) { |
256425cc LP |
4550 | char **p; |
4551 | ||
4552 | assert(i); | |
4553 | ||
4554 | if (!i->documentation) { | |
4555 | log_info("Documentation for %s not known.", i->id); | |
4556 | return; | |
4557 | } | |
4558 | ||
78002a67 ZJS |
4559 | STRV_FOREACH(p, i->documentation) |
4560 | if (startswith(*p, "man:")) | |
4561 | show_man_page(*p + 4, false); | |
4562 | else | |
0315fe37 | 4563 | log_info("Can't show: %s", *p); |
256425cc LP |
4564 | } |
4565 | ||
4679a8c3 YW |
4566 | static int map_main_pid(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { |
4567 | UnitStatusInfo *i = userdata; | |
4568 | uint32_t u; | |
f459b602 | 4569 | int r; |
61cbdc4b | 4570 | |
4679a8c3 YW |
4571 | r = sd_bus_message_read(m, "u", &u); |
4572 | if (r < 0) | |
4573 | return r; | |
61cbdc4b | 4574 | |
4679a8c3 YW |
4575 | i->main_pid = (pid_t) u; |
4576 | i->running = u > 0; | |
b8131a87 | 4577 | |
4679a8c3 YW |
4578 | return 0; |
4579 | } | |
b8131a87 | 4580 | |
4679a8c3 | 4581 | static int map_load_error(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { |
31d99bd1 | 4582 | const char *message, **p = userdata; |
4679a8c3 | 4583 | int r; |
b8131a87 | 4584 | |
31d99bd1 | 4585 | r = sd_bus_message_read(m, "(ss)", NULL, &message); |
4679a8c3 YW |
4586 | if (r < 0) |
4587 | return r; | |
b8131a87 | 4588 | |
4679a8c3 YW |
4589 | if (!isempty(message)) |
4590 | *p = message; | |
61cbdc4b | 4591 | |
4679a8c3 YW |
4592 | return 0; |
4593 | } | |
61cbdc4b | 4594 | |
4679a8c3 YW |
4595 | static int map_listen(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { |
4596 | const char *type, *path; | |
4597 | char ***p = userdata; | |
4598 | int r; | |
61cbdc4b | 4599 | |
4679a8c3 YW |
4600 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(ss)"); |
4601 | if (r < 0) | |
4602 | return r; | |
61cbdc4b | 4603 | |
4679a8c3 | 4604 | while ((r = sd_bus_message_read(m, "(ss)", &type, &path)) > 0) { |
61cbdc4b | 4605 | |
4679a8c3 | 4606 | r = strv_extend(p, type); |
f459b602 | 4607 | if (r < 0) |
4679a8c3 | 4608 | return r; |
61cbdc4b | 4609 | |
4679a8c3 | 4610 | r = strv_extend(p, path); |
f459b602 | 4611 | if (r < 0) |
4679a8c3 | 4612 | return r; |
61cbdc4b | 4613 | } |
4679a8c3 YW |
4614 | if (r < 0) |
4615 | return r; | |
582a507f | 4616 | |
4679a8c3 YW |
4617 | r = sd_bus_message_exit_container(m); |
4618 | if (r < 0) | |
4619 | return r; | |
49dbfa7b | 4620 | |
4679a8c3 YW |
4621 | return 0; |
4622 | } | |
52990c2e | 4623 | |
4679a8c3 YW |
4624 | static int map_conditions(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { |
4625 | UnitStatusInfo *i = userdata; | |
4626 | const char *cond, *param; | |
4627 | int trigger, negate; | |
4628 | int32_t state; | |
4629 | int r; | |
52990c2e | 4630 | |
4679a8c3 YW |
4631 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(sbbsi)"); |
4632 | if (r < 0) | |
4633 | return r; | |
f459b602 | 4634 | |
4679a8c3 YW |
4635 | while ((r = sd_bus_message_read(m, "(sbbsi)", &cond, &trigger, &negate, ¶m, &state)) > 0) { |
4636 | _cleanup_(unit_condition_freep) UnitCondition *c = NULL; | |
d5db7fe6 | 4637 | |
3bfa8668 | 4638 | c = new(UnitCondition, 1); |
4679a8c3 YW |
4639 | if (!c) |
4640 | return -ENOMEM; | |
d5db7fe6 | 4641 | |
3bfa8668 LP |
4642 | *c = (UnitCondition) { |
4643 | .name = strdup(cond), | |
4644 | .param = strdup(param), | |
4645 | .trigger = trigger, | |
4646 | .negate = negate, | |
4647 | .tristate = state, | |
4648 | }; | |
4649 | ||
4679a8c3 YW |
4650 | if (!c->name || !c->param) |
4651 | return -ENOMEM; | |
d5db7fe6 | 4652 | |
3bfa8668 | 4653 | LIST_PREPEND(conditions, i->conditions, TAKE_PTR(c)); |
4679a8c3 YW |
4654 | } |
4655 | if (r < 0) | |
4656 | return r; | |
d5db7fe6 | 4657 | |
4679a8c3 YW |
4658 | r = sd_bus_message_exit_container(m); |
4659 | if (r < 0) | |
4660 | return r; | |
59fccdc5 | 4661 | |
4679a8c3 YW |
4662 | return 0; |
4663 | } | |
59fccdc5 | 4664 | |
4679a8c3 YW |
4665 | static int map_asserts(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { |
4666 | UnitStatusInfo *i = userdata; | |
4667 | const char *cond, *param; | |
4668 | int trigger, negate; | |
4669 | int32_t state; | |
4670 | int r; | |
59fccdc5 | 4671 | |
4679a8c3 YW |
4672 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(sbbsi)"); |
4673 | if (r < 0) | |
4674 | return r; | |
59fccdc5 | 4675 | |
4679a8c3 YW |
4676 | while ((r = sd_bus_message_read(m, "(sbbsi)", &cond, &trigger, &negate, ¶m, &state)) > 0) { |
4677 | if (state < 0 && (!trigger || !i->failed_assert)) { | |
4678 | i->failed_assert = cond; | |
4679 | i->failed_assert_trigger = trigger; | |
4680 | i->failed_assert_negate = negate; | |
4681 | i->failed_assert_parameter = param; | |
4682 | } | |
4683 | } | |
4684 | if (r < 0) | |
4685 | return r; | |
f459b602 | 4686 | |
4679a8c3 YW |
4687 | r = sd_bus_message_exit_container(m); |
4688 | if (r < 0) | |
4689 | return r; | |
f459b602 | 4690 | |
4679a8c3 YW |
4691 | return 0; |
4692 | } | |
582a507f | 4693 | |
4679a8c3 YW |
4694 | static int map_exec(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { |
4695 | _cleanup_free_ ExecStatusInfo *info = NULL; | |
3e14d36a | 4696 | ExecStatusInfo *last; |
4679a8c3 | 4697 | UnitStatusInfo *i = userdata; |
b3d59367 | 4698 | bool is_ex_prop = endswith(member, "Ex"); |
4679a8c3 | 4699 | int r; |
9f39404c | 4700 | |
b3d59367 | 4701 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, is_ex_prop ? "(sasasttttuii)" : "(sasbttttuii)"); |
4679a8c3 YW |
4702 | if (r < 0) |
4703 | return r; | |
9f39404c | 4704 | |
4679a8c3 YW |
4705 | info = new0(ExecStatusInfo, 1); |
4706 | if (!info) | |
4707 | return -ENOMEM; | |
9f39404c | 4708 | |
3e14d36a LP |
4709 | LIST_FIND_TAIL(exec, i->exec, last); |
4710 | ||
b3d59367 | 4711 | while ((r = exec_status_info_deserialize(m, info, is_ex_prop)) > 0) { |
9f39404c | 4712 | |
4679a8c3 YW |
4713 | info->name = strdup(member); |
4714 | if (!info->name) | |
4715 | return -ENOMEM; | |
9f39404c | 4716 | |
3e14d36a LP |
4717 | LIST_INSERT_AFTER(exec, i->exec, last, info); |
4718 | last = info; | |
f459b602 | 4719 | |
4679a8c3 YW |
4720 | info = new0(ExecStatusInfo, 1); |
4721 | if (!info) | |
4722 | return -ENOMEM; | |
9f39404c | 4723 | } |
4679a8c3 YW |
4724 | if (r < 0) |
4725 | return r; | |
f459b602 | 4726 | |
4679a8c3 | 4727 | r = sd_bus_message_exit_container(m); |
f459b602 | 4728 | if (r < 0) |
4679a8c3 | 4729 | return r; |
61cbdc4b LP |
4730 | |
4731 | return 0; | |
4732 | } | |
4733 | ||
eda19357 | 4734 | static int print_property(const char *name, const char *expected_value, sd_bus_message *m, bool value, bool all) { |
07636114 YW |
4735 | char bus_type; |
4736 | const char *contents; | |
f459b602 MAP |
4737 | int r; |
4738 | ||
48220598 | 4739 | assert(name); |
f459b602 | 4740 | assert(m); |
48220598 | 4741 | |
61cbdc4b LP |
4742 | /* This is a low-level property printer, see |
4743 | * print_status_info() for the nicer output */ | |
4744 | ||
07636114 YW |
4745 | r = sd_bus_message_peek_type(m, &bus_type, &contents); |
4746 | if (r < 0) | |
852c1b4d | 4747 | return r; |
48220598 | 4748 | |
07636114 | 4749 | switch (bus_type) { |
48220598 | 4750 | |
a3ddd457 YW |
4751 | case SD_BUS_TYPE_INT32: |
4752 | if (endswith(name, "ActionExitStatus")) { | |
4753 | int32_t i; | |
4754 | ||
4755 | r = sd_bus_message_read_basic(m, bus_type, &i); | |
4756 | if (r < 0) | |
4757 | return r; | |
4758 | ||
4759 | if (i >= 0 && i <= 255) | |
4760 | bus_print_property_valuef(name, expected_value, value, "%"PRIi32, i); | |
4761 | else if (all) | |
4762 | bus_print_property_value(name, expected_value, value, "[not set]"); | |
4763 | ||
b070c7c0 MS |
4764 | return 1; |
4765 | } else if (streq(name, "NUMAPolicy")) { | |
4766 | int32_t i; | |
4767 | ||
4768 | r = sd_bus_message_read_basic(m, bus_type, &i); | |
4769 | if (r < 0) | |
4770 | return r; | |
4771 | ||
4772 | bus_print_property_valuef(name, expected_value, value, "%s", strna(mpol_to_string(i))); | |
4773 | ||
a3ddd457 YW |
4774 | return 1; |
4775 | } | |
4776 | break; | |
4777 | ||
07636114 | 4778 | case SD_BUS_TYPE_STRUCT: |
48220598 | 4779 | |
07636114 | 4780 | if (contents[0] == SD_BUS_TYPE_UINT32 && streq(name, "Job")) { |
48220598 LP |
4781 | uint32_t u; |
4782 | ||
f459b602 MAP |
4783 | r = sd_bus_message_read(m, "(uo)", &u, NULL); |
4784 | if (r < 0) | |
4785 | return bus_log_parse_error(r); | |
48220598 | 4786 | |
f459b602 | 4787 | if (u > 0) |
102b0214 | 4788 | bus_print_property_valuef(name, expected_value, value, "%"PRIu32, u); |
07636114 | 4789 | else if (all) |
102b0214 | 4790 | bus_print_property_value(name, expected_value, value, ""); |
48220598 | 4791 | |
07636114 | 4792 | return 1; |
f459b602 | 4793 | |
07636114 | 4794 | } else if (contents[0] == SD_BUS_TYPE_STRING && streq(name, "Unit")) { |
48220598 LP |
4795 | const char *s; |
4796 | ||
f459b602 MAP |
4797 | r = sd_bus_message_read(m, "(so)", &s, NULL); |
4798 | if (r < 0) | |
4799 | return bus_log_parse_error(r); | |
48220598 | 4800 | |
07636114 | 4801 | if (all || !isempty(s)) |
102b0214 | 4802 | bus_print_property_value(name, expected_value, value, s); |
48220598 | 4803 | |
07636114 | 4804 | return 1; |
f459b602 | 4805 | |
07636114 | 4806 | } else if (contents[0] == SD_BUS_TYPE_STRING && streq(name, "LoadError")) { |
9f39404c LP |
4807 | const char *a = NULL, *b = NULL; |
4808 | ||
f459b602 MAP |
4809 | r = sd_bus_message_read(m, "(ss)", &a, &b); |
4810 | if (r < 0) | |
4811 | return bus_log_parse_error(r); | |
9f39404c | 4812 | |
48d3358d | 4813 | if (!isempty(a) || !isempty(b)) |
102b0214 | 4814 | bus_print_property_valuef(name, expected_value, value, "%s \"%s\"", strempty(a), strempty(b)); |
48d3358d YW |
4815 | else if (all) |
4816 | bus_print_property_value(name, expected_value, value, ""); | |
f786e80d | 4817 | |
07636114 | 4818 | return 1; |
04749e40 YW |
4819 | |
4820 | } else if (STR_IN_SET(name, "SystemCallFilter", "RestrictAddressFamilies")) { | |
57183d11 | 4821 | _cleanup_strv_free_ char **l = NULL; |
6b000af4 | 4822 | int allow_list; |
57183d11 LP |
4823 | |
4824 | r = sd_bus_message_enter_container(m, 'r', "bas"); | |
4825 | if (r < 0) | |
4826 | return bus_log_parse_error(r); | |
4827 | ||
6b000af4 | 4828 | r = sd_bus_message_read(m, "b", &allow_list); |
57183d11 LP |
4829 | if (r < 0) |
4830 | return bus_log_parse_error(r); | |
4831 | ||
4832 | r = sd_bus_message_read_strv(m, &l); | |
4833 | if (r < 0) | |
4834 | return bus_log_parse_error(r); | |
4835 | ||
4836 | r = sd_bus_message_exit_container(m); | |
4837 | if (r < 0) | |
4838 | return bus_log_parse_error(r); | |
4839 | ||
6b000af4 | 4840 | if (all || allow_list || !strv_isempty(l)) { |
57183d11 LP |
4841 | bool first = true; |
4842 | char **i; | |
4843 | ||
07636114 | 4844 | if (!value) { |
4f9a9105 ZJS |
4845 | fputs(name, stdout); |
4846 | fputc('=', stdout); | |
4847 | } | |
57183d11 | 4848 | |
6b000af4 | 4849 | if (!allow_list) |
57183d11 LP |
4850 | fputc('~', stdout); |
4851 | ||
4852 | STRV_FOREACH(i, l) { | |
4853 | if (first) | |
4854 | first = false; | |
4855 | else | |
4856 | fputc(' ', stdout); | |
4857 | ||
4858 | fputs(*i, stdout); | |
4859 | } | |
4860 | fputc('\n', stdout); | |
4861 | } | |
4862 | ||
c7bb2fec | 4863 | return 1; |
102b0214 | 4864 | |
c7bb2fec IP |
4865 | } else if (STR_IN_SET(name, "SELinuxContext", "AppArmorProfile", "SmackProcessLabel")) { |
4866 | int ignore; | |
4867 | const char *s; | |
4868 | ||
4869 | r = sd_bus_message_read(m, "(bs)", &ignore, &s); | |
4870 | if (r < 0) | |
4871 | return bus_log_parse_error(r); | |
4872 | ||
4873 | if (!isempty(s)) | |
102b0214 | 4874 | bus_print_property_valuef(name, expected_value, value, "%s%s", ignore ? "-" : "", s); |
c7bb2fec | 4875 | else if (all) |
102b0214 | 4876 | bus_print_property_value(name, expected_value, value, ""); |
c7bb2fec | 4877 | |
07636114 | 4878 | return 1; |
4a220712 YW |
4879 | |
4880 | } else if (endswith(name, "ExitStatus") && streq(contents, "aiai")) { | |
4881 | const int32_t *status, *signal; | |
62b21e2e | 4882 | size_t n_status, n_signal, i; |
4a220712 YW |
4883 | |
4884 | r = sd_bus_message_enter_container(m, 'r', "aiai"); | |
4885 | if (r < 0) | |
4886 | return bus_log_parse_error(r); | |
4887 | ||
62b21e2e | 4888 | r = sd_bus_message_read_array(m, 'i', (const void **) &status, &n_status); |
4a220712 YW |
4889 | if (r < 0) |
4890 | return bus_log_parse_error(r); | |
4891 | ||
62b21e2e | 4892 | r = sd_bus_message_read_array(m, 'i', (const void **) &signal, &n_signal); |
4a220712 YW |
4893 | if (r < 0) |
4894 | return bus_log_parse_error(r); | |
4895 | ||
4896 | r = sd_bus_message_exit_container(m); | |
4897 | if (r < 0) | |
4898 | return bus_log_parse_error(r); | |
4899 | ||
62b21e2e ZJS |
4900 | n_status /= sizeof(int32_t); |
4901 | n_signal /= sizeof(int32_t); | |
4a220712 | 4902 | |
62b21e2e | 4903 | if (all || n_status > 0 || n_signal > 0) { |
4a220712 YW |
4904 | bool first = true; |
4905 | ||
4906 | if (!value) { | |
4907 | fputs(name, stdout); | |
4908 | fputc('=', stdout); | |
4909 | } | |
4910 | ||
62b21e2e | 4911 | for (i = 0; i < n_status; i++) { |
4a220712 YW |
4912 | if (first) |
4913 | first = false; | |
4914 | else | |
4915 | fputc(' ', stdout); | |
4916 | ||
4917 | printf("%"PRIi32, status[i]); | |
4918 | } | |
4919 | ||
62b21e2e | 4920 | for (i = 0; i < n_signal; i++) { |
4a220712 YW |
4921 | const char *str; |
4922 | ||
4923 | str = signal_to_string((int) signal[i]); | |
4a220712 YW |
4924 | |
4925 | if (first) | |
4926 | first = false; | |
4927 | else | |
4928 | fputc(' ', stdout); | |
4929 | ||
148ffa2e ZJS |
4930 | if (str) |
4931 | fputs(str, stdout); | |
4932 | else | |
4933 | printf("%"PRIi32, status[i]); | |
4a220712 YW |
4934 | } |
4935 | ||
4936 | fputc('\n', stdout); | |
4937 | } | |
4938 | return 1; | |
48220598 LP |
4939 | } |
4940 | ||
4941 | break; | |
48220598 | 4942 | |
f459b602 | 4943 | case SD_BUS_TYPE_ARRAY: |
48220598 | 4944 | |
07636114 | 4945 | if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "EnvironmentFiles")) { |
f459b602 MAP |
4946 | const char *path; |
4947 | int ignore; | |
8c7be95e | 4948 | |
f459b602 MAP |
4949 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(sb)"); |
4950 | if (r < 0) | |
4951 | return bus_log_parse_error(r); | |
8c7be95e | 4952 | |
f459b602 | 4953 | while ((r = sd_bus_message_read(m, "(sb)", &path, &ignore)) > 0) |
102b0214 | 4954 | bus_print_property_valuef(name, expected_value, value, "%s (ignore_errors=%s)", path, yes_no(ignore)); |
8c7be95e | 4955 | |
f459b602 MAP |
4956 | if (r < 0) |
4957 | return bus_log_parse_error(r); | |
8c7be95e | 4958 | |
f459b602 MAP |
4959 | r = sd_bus_message_exit_container(m); |
4960 | if (r < 0) | |
4961 | return bus_log_parse_error(r); | |
8c7be95e | 4962 | |
07636114 | 4963 | return 1; |
8c7be95e | 4964 | |
07636114 | 4965 | } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "Paths")) { |
f459b602 | 4966 | const char *type, *path; |
67419600 | 4967 | |
f459b602 MAP |
4968 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(ss)"); |
4969 | if (r < 0) | |
4970 | return bus_log_parse_error(r); | |
ebf57b80 | 4971 | |
f459b602 | 4972 | while ((r = sd_bus_message_read(m, "(ss)", &type, &path)) > 0) |
102b0214 | 4973 | bus_print_property_valuef(name, expected_value, value, "%s (%s)", path, type); |
f459b602 MAP |
4974 | if (r < 0) |
4975 | return bus_log_parse_error(r); | |
ebf57b80 | 4976 | |
f459b602 MAP |
4977 | r = sd_bus_message_exit_container(m); |
4978 | if (r < 0) | |
4979 | return bus_log_parse_error(r); | |
ebf57b80 | 4980 | |
07636114 | 4981 | return 1; |
582a507f | 4982 | |
07636114 | 4983 | } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "Listen")) { |
f459b602 | 4984 | const char *type, *path; |
67419600 | 4985 | |
f459b602 MAP |
4986 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(ss)"); |
4987 | if (r < 0) | |
4988 | return bus_log_parse_error(r); | |
67419600 | 4989 | |
f459b602 | 4990 | while ((r = sd_bus_message_read(m, "(ss)", &type, &path)) > 0) |
102b0214 | 4991 | bus_print_property_valuef(name, expected_value, value, "%s (%s)", path, type); |
f459b602 MAP |
4992 | if (r < 0) |
4993 | return bus_log_parse_error(r); | |
67419600 | 4994 | |
f459b602 MAP |
4995 | r = sd_bus_message_exit_container(m); |
4996 | if (r < 0) | |
4997 | return bus_log_parse_error(r); | |
67419600 | 4998 | |
07636114 | 4999 | return 1; |
67419600 | 5000 | |
07636114 | 5001 | } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "TimersMonotonic")) { |
f459b602 | 5002 | const char *base; |
07636114 | 5003 | uint64_t v, next_elapse; |
707e5e52 | 5004 | |
f459b602 MAP |
5005 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(stt)"); |
5006 | if (r < 0) | |
5007 | return bus_log_parse_error(r); | |
552e4331 | 5008 | |
07636114 | 5009 | while ((r = sd_bus_message_read(m, "(stt)", &base, &v, &next_elapse)) > 0) { |
860f2be0 | 5010 | char timespan1[FORMAT_TIMESPAN_MAX] = "n/a", timespan2[FORMAT_TIMESPAN_MAX] = "n/a"; |
fe68089d | 5011 | |
860f2be0 ZJS |
5012 | (void) format_timespan(timespan1, sizeof timespan1, v, 0); |
5013 | (void) format_timespan(timespan2, sizeof timespan2, next_elapse, 0); | |
5014 | ||
5015 | bus_print_property_valuef(name, expected_value, value, | |
5016 | "{ %s=%s ; next_elapse=%s }", base, timespan1, timespan2); | |
fe68089d | 5017 | } |
f459b602 MAP |
5018 | if (r < 0) |
5019 | return bus_log_parse_error(r); | |
5020 | ||
5021 | r = sd_bus_message_exit_container(m); | |
5022 | if (r < 0) | |
5023 | return bus_log_parse_error(r); | |
fe68089d | 5024 | |
07636114 | 5025 | return 1; |
fe68089d | 5026 | |
07636114 | 5027 | } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "TimersCalendar")) { |
fa6dee52 YW |
5028 | const char *base, *spec; |
5029 | uint64_t next_elapse; | |
5030 | ||
5031 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(sst)"); | |
5032 | if (r < 0) | |
5033 | return bus_log_parse_error(r); | |
5034 | ||
5035 | while ((r = sd_bus_message_read(m, "(sst)", &base, &spec, &next_elapse)) > 0) { | |
860f2be0 | 5036 | char timestamp[FORMAT_TIMESTAMP_MAX] = "n/a"; |
fa6dee52 | 5037 | |
860f2be0 ZJS |
5038 | (void) format_timestamp(timestamp, sizeof(timestamp), next_elapse); |
5039 | bus_print_property_valuef(name, expected_value, value, | |
5040 | "{ %s=%s ; next_elapse=%s }", base, spec, timestamp); | |
fa6dee52 YW |
5041 | } |
5042 | if (r < 0) | |
5043 | return bus_log_parse_error(r); | |
5044 | ||
5045 | r = sd_bus_message_exit_container(m); | |
5046 | if (r < 0) | |
5047 | return bus_log_parse_error(r); | |
5048 | ||
07636114 | 5049 | return 1; |
fa6dee52 | 5050 | |
07636114 | 5051 | } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && startswith(name, "Exec")) { |
f459b602 | 5052 | ExecStatusInfo info = {}; |
b3d59367 | 5053 | bool is_ex_prop = endswith(name, "Ex"); |
f459b602 | 5054 | |
b3d59367 | 5055 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, is_ex_prop ? "(sasasttttuii)" : "(sasbttttuii)"); |
f459b602 MAP |
5056 | if (r < 0) |
5057 | return bus_log_parse_error(r); | |
5058 | ||
b3d59367 | 5059 | while ((r = exec_status_info_deserialize(m, &info, is_ex_prop)) > 0) { |
f459b602 | 5060 | char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX]; |
b3d59367 AZ |
5061 | _cleanup_strv_free_ char **optv = NULL; |
5062 | _cleanup_free_ char *tt, *o = NULL; | |
f459b602 MAP |
5063 | |
5064 | tt = strv_join(info.argv, " "); | |
5065 | ||
b3d59367 AZ |
5066 | if (is_ex_prop) { |
5067 | r = exec_command_flags_to_strv(info.flags, &optv); | |
5068 | if (r < 0) | |
5069 | return log_error_errno(r, "Failed to convert ExecCommandFlags to strv: %m"); | |
5070 | ||
5071 | o = strv_join(optv, " "); | |
5072 | ||
5073 | bus_print_property_valuef(name, expected_value, value, | |
5074 | "{ path=%s ; argv[]=%s ; flags=%s ; start_time=[%s] ; stop_time=[%s] ; pid="PID_FMT" ; code=%s ; status=%i%s%s }", | |
5075 | strna(info.path), | |
5076 | strna(tt), | |
5077 | strna(o), | |
5078 | strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)), | |
5079 | strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)), | |
5080 | info.pid, | |
5081 | sigchld_code_to_string(info.code), | |
5082 | info.status, | |
5083 | info.code == CLD_EXITED ? "" : "/", | |
5084 | strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status))); | |
5085 | } else | |
5086 | bus_print_property_valuef(name, expected_value, value, | |
5087 | "{ path=%s ; argv[]=%s ; ignore_errors=%s ; start_time=[%s] ; stop_time=[%s] ; pid="PID_FMT" ; code=%s ; status=%i%s%s }", | |
5088 | strna(info.path), | |
5089 | strna(tt), | |
5090 | yes_no(info.ignore), | |
5091 | strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)), | |
5092 | strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)), | |
5093 | info.pid, | |
5094 | sigchld_code_to_string(info.code), | |
5095 | info.status, | |
5096 | info.code == CLD_EXITED ? "" : "/", | |
5097 | strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status))); | |
fe68089d | 5098 | |
582a507f LP |
5099 | free(info.path); |
5100 | strv_free(info.argv); | |
f459b602 | 5101 | zero(info); |
707e5e52 LP |
5102 | } |
5103 | ||
f459b602 MAP |
5104 | r = sd_bus_message_exit_container(m); |
5105 | if (r < 0) | |
5106 | return bus_log_parse_error(r); | |
5107 | ||
07636114 | 5108 | return 1; |
4ad49000 | 5109 | |
07636114 | 5110 | } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "DeviceAllow")) { |
f459b602 | 5111 | const char *path, *rwm; |
4ad49000 | 5112 | |
f459b602 MAP |
5113 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(ss)"); |
5114 | if (r < 0) | |
5115 | return bus_log_parse_error(r); | |
4ad49000 | 5116 | |
f459b602 | 5117 | while ((r = sd_bus_message_read(m, "(ss)", &path, &rwm)) > 0) |
102b0214 | 5118 | bus_print_property_valuef(name, expected_value, value, "%s %s", strna(path), strna(rwm)); |
f459b602 MAP |
5119 | if (r < 0) |
5120 | return bus_log_parse_error(r); | |
4ad49000 | 5121 | |
f459b602 MAP |
5122 | r = sd_bus_message_exit_container(m); |
5123 | if (r < 0) | |
5124 | return bus_log_parse_error(r); | |
4ad49000 | 5125 | |
07636114 | 5126 | return 1; |
4ad49000 | 5127 | |
07636114 | 5128 | } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && |
72240b52 | 5129 | STR_IN_SET(name, "IODeviceWeight", "BlockIODeviceWeight")) { |
f459b602 MAP |
5130 | const char *path; |
5131 | uint64_t weight; | |
b8ab2dc6 | 5132 | |
f459b602 MAP |
5133 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(st)"); |
5134 | if (r < 0) | |
5135 | return bus_log_parse_error(r); | |
b8ab2dc6 | 5136 | |
f459b602 | 5137 | while ((r = sd_bus_message_read(m, "(st)", &path, &weight)) > 0) |
102b0214 | 5138 | bus_print_property_valuef(name, expected_value, value, "%s %"PRIu64, strna(path), weight); |
f459b602 MAP |
5139 | if (r < 0) |
5140 | return bus_log_parse_error(r); | |
b8ab2dc6 | 5141 | |
f459b602 MAP |
5142 | r = sd_bus_message_exit_container(m); |
5143 | if (r < 0) | |
5144 | return bus_log_parse_error(r); | |
b8ab2dc6 | 5145 | |
07636114 | 5146 | return 1; |
b8ab2dc6 | 5147 | |
07636114 | 5148 | } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && |
72240b52 ZJS |
5149 | (cgroup_io_limit_type_from_string(name) >= 0 || |
5150 | STR_IN_SET(name, "BlockIOReadBandwidth", "BlockIOWriteBandwidth"))) { | |
f459b602 MAP |
5151 | const char *path; |
5152 | uint64_t bandwidth; | |
4ad49000 | 5153 | |
f459b602 MAP |
5154 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(st)"); |
5155 | if (r < 0) | |
5156 | return bus_log_parse_error(r); | |
4ad49000 | 5157 | |
f459b602 | 5158 | while ((r = sd_bus_message_read(m, "(st)", &path, &bandwidth)) > 0) |
102b0214 | 5159 | bus_print_property_valuef(name, expected_value, value, "%s %"PRIu64, strna(path), bandwidth); |
f459b602 MAP |
5160 | if (r < 0) |
5161 | return bus_log_parse_error(r); | |
4ad49000 | 5162 | |
f459b602 MAP |
5163 | r = sd_bus_message_exit_container(m); |
5164 | if (r < 0) | |
5165 | return bus_log_parse_error(r); | |
4ad49000 | 5166 | |
07636114 | 5167 | return 1; |
6ae4283c TH |
5168 | |
5169 | } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && | |
5170 | streq(name, "IODeviceLatencyTargetUSec")) { | |
5171 | char ts[FORMAT_TIMESPAN_MAX]; | |
5172 | const char *path; | |
5173 | uint64_t target; | |
5174 | ||
5175 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(st)"); | |
5176 | if (r < 0) | |
5177 | return bus_log_parse_error(r); | |
5178 | ||
5179 | while ((r = sd_bus_message_read(m, "(st)", &path, &target)) > 0) | |
102b0214 YW |
5180 | bus_print_property_valuef(name, expected_value, value, "%s %s", strna(path), |
5181 | format_timespan(ts, sizeof(ts), target, 1)); | |
6ae4283c TH |
5182 | if (r < 0) |
5183 | return bus_log_parse_error(r); | |
5184 | ||
5185 | r = sd_bus_message_exit_container(m); | |
5186 | if (r < 0) | |
5187 | return bus_log_parse_error(r); | |
5188 | ||
5189 | return 1; | |
08f3be7a | 5190 | |
d4d55b0d | 5191 | } else if (contents[0] == SD_BUS_TYPE_BYTE && STR_IN_SET(name, "StandardInputData", "RootHashSignature")) { |
08f3be7a LP |
5192 | _cleanup_free_ char *h = NULL; |
5193 | const void *p; | |
5194 | size_t sz; | |
5195 | ssize_t n; | |
5196 | ||
5197 | r = sd_bus_message_read_array(m, 'y', &p, &sz); | |
5198 | if (r < 0) | |
5199 | return bus_log_parse_error(r); | |
5200 | ||
5201 | n = base64mem(p, sz, &h); | |
5202 | if (n < 0) | |
5203 | return log_oom(); | |
5204 | ||
102b0214 | 5205 | bus_print_property_value(name, expected_value, value, h); |
08f3be7a | 5206 | |
07636114 | 5207 | return 1; |
9c1f9465 YW |
5208 | |
5209 | } else if (STR_IN_SET(name, "IPAddressAllow", "IPAddressDeny")) { | |
5210 | _cleanup_free_ char *addresses = NULL; | |
5211 | ||
5212 | r = sd_bus_message_enter_container(m, 'a', "(iayu)"); | |
5213 | if (r < 0) | |
5214 | return bus_log_parse_error(r); | |
5215 | ||
5216 | for (;;) { | |
5217 | _cleanup_free_ char *str = NULL; | |
5218 | uint32_t prefixlen; | |
5219 | int32_t family; | |
5220 | const void *ap; | |
5221 | size_t an; | |
5222 | ||
5223 | r = sd_bus_message_enter_container(m, 'r', "iayu"); | |
5224 | if (r < 0) | |
5225 | return bus_log_parse_error(r); | |
5226 | if (r == 0) | |
5227 | break; | |
5228 | ||
5229 | r = sd_bus_message_read(m, "i", &family); | |
5230 | if (r < 0) | |
5231 | return bus_log_parse_error(r); | |
5232 | ||
5233 | r = sd_bus_message_read_array(m, 'y', &ap, &an); | |
5234 | if (r < 0) | |
5235 | return bus_log_parse_error(r); | |
5236 | ||
5237 | r = sd_bus_message_read(m, "u", &prefixlen); | |
5238 | if (r < 0) | |
5239 | return bus_log_parse_error(r); | |
5240 | ||
5241 | r = sd_bus_message_exit_container(m); | |
5242 | if (r < 0) | |
5243 | return bus_log_parse_error(r); | |
5244 | ||
5245 | if (!IN_SET(family, AF_INET, AF_INET6)) | |
5246 | continue; | |
5247 | ||
5248 | if (an != FAMILY_ADDRESS_SIZE(family)) | |
5249 | continue; | |
5250 | ||
5251 | if (prefixlen > FAMILY_ADDRESS_SIZE(family) * 8) | |
5252 | continue; | |
5253 | ||
5254 | if (in_addr_prefix_to_string(family, (union in_addr_union *) ap, prefixlen, &str) < 0) | |
5255 | continue; | |
5256 | ||
5257 | if (!strextend_with_separator(&addresses, " ", str, NULL)) | |
5258 | return log_oom(); | |
5259 | } | |
5260 | ||
5261 | r = sd_bus_message_exit_container(m); | |
5262 | if (r < 0) | |
5263 | return bus_log_parse_error(r); | |
5264 | ||
5265 | if (all || !isempty(addresses)) | |
5266 | bus_print_property_value(name, expected_value, value, strempty(addresses)); | |
5267 | ||
5268 | return 1; | |
4c787df7 YW |
5269 | |
5270 | } else if (STR_IN_SET(name, "BindPaths", "BindReadOnlyPaths")) { | |
5271 | _cleanup_free_ char *paths = NULL; | |
5272 | const char *source, *dest; | |
5273 | int ignore_enoent; | |
5274 | uint64_t rbind; | |
5275 | ||
5276 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(ssbt)"); | |
5277 | if (r < 0) | |
5278 | return bus_log_parse_error(r); | |
5279 | ||
5280 | while ((r = sd_bus_message_read(m, "(ssbt)", &source, &dest, &ignore_enoent, &rbind)) > 0) { | |
5281 | _cleanup_free_ char *str = NULL; | |
5282 | ||
5283 | if (isempty(source)) | |
5284 | continue; | |
5285 | ||
5286 | if (asprintf(&str, "%s%s%s%s%s", | |
5287 | ignore_enoent ? "-" : "", | |
5288 | source, | |
5289 | isempty(dest) ? "" : ":", | |
5290 | strempty(dest), | |
5291 | rbind == MS_REC ? ":rbind" : "") < 0) | |
5292 | return log_oom(); | |
5293 | ||
5294 | if (!strextend_with_separator(&paths, " ", str, NULL)) | |
5295 | return log_oom(); | |
5296 | } | |
5297 | if (r < 0) | |
5298 | return bus_log_parse_error(r); | |
5299 | ||
5300 | r = sd_bus_message_exit_container(m); | |
5301 | if (r < 0) | |
5302 | return bus_log_parse_error(r); | |
5303 | ||
5304 | if (all || !isempty(paths)) | |
5305 | bus_print_property_value(name, expected_value, value, strempty(paths)); | |
5306 | ||
5307 | return 1; | |
5308 | ||
5309 | } else if (streq(name, "TemporaryFileSystem")) { | |
5310 | _cleanup_free_ char *paths = NULL; | |
5311 | const char *target, *option; | |
5312 | ||
5313 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(ss)"); | |
5314 | if (r < 0) | |
5315 | return bus_log_parse_error(r); | |
5316 | ||
5317 | while ((r = sd_bus_message_read(m, "(ss)", &target, &option)) > 0) { | |
5318 | _cleanup_free_ char *str = NULL; | |
5319 | ||
5320 | if (isempty(target)) | |
5321 | continue; | |
5322 | ||
5323 | if (asprintf(&str, "%s%s%s", target, isempty(option) ? "" : ":", strempty(option)) < 0) | |
5324 | return log_oom(); | |
5325 | ||
5326 | if (!strextend_with_separator(&paths, " ", str, NULL)) | |
5327 | return log_oom(); | |
5328 | } | |
5329 | if (r < 0) | |
5330 | return bus_log_parse_error(r); | |
5331 | ||
5332 | r = sd_bus_message_exit_container(m); | |
5333 | if (r < 0) | |
5334 | return bus_log_parse_error(r); | |
5335 | ||
5336 | if (all || !isempty(paths)) | |
5337 | bus_print_property_value(name, expected_value, value, strempty(paths)); | |
5338 | ||
5339 | return 1; | |
305a2850 YW |
5340 | |
5341 | } else if (streq(name, "LogExtraFields")) { | |
5342 | _cleanup_free_ char *fields = NULL; | |
5343 | const void *p; | |
5344 | size_t sz; | |
5345 | ||
5346 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "ay"); | |
5347 | if (r < 0) | |
5348 | return bus_log_parse_error(r); | |
5349 | ||
5350 | while ((r = sd_bus_message_read_array(m, 'y', &p, &sz)) > 0) { | |
5351 | _cleanup_free_ char *str = NULL; | |
5352 | const char *eq; | |
5353 | ||
5354 | if (memchr(p, 0, sz)) | |
5355 | continue; | |
5356 | ||
5357 | eq = memchr(p, '=', sz); | |
5358 | if (!eq) | |
5359 | continue; | |
5360 | ||
5361 | if (!journal_field_valid(p, eq - (const char*) p, false)) | |
5362 | continue; | |
5363 | ||
5364 | str = malloc(sz + 1); | |
5365 | if (!str) | |
5366 | return log_oom(); | |
5367 | ||
5368 | memcpy(str, p, sz); | |
5369 | str[sz] = '\0'; | |
5370 | ||
5371 | if (!utf8_is_valid(str)) | |
5372 | continue; | |
5373 | ||
5374 | if (!strextend_with_separator(&fields, " ", str, NULL)) | |
5375 | return log_oom(); | |
5376 | } | |
5377 | if (r < 0) | |
5378 | return bus_log_parse_error(r); | |
5379 | ||
5380 | r = sd_bus_message_exit_container(m); | |
5381 | if (r < 0) | |
5382 | return bus_log_parse_error(r); | |
5383 | ||
5384 | if (all || !isempty(fields)) | |
5385 | bus_print_property_value(name, expected_value, value, strempty(fields)); | |
5386 | ||
a047f4f1 | 5387 | return 1; |
047f5d63 | 5388 | } else if (contents[0] == SD_BUS_TYPE_BYTE && STR_IN_SET(name, "CPUAffinity", "NUMAMask", "AllowedCPUs", "AllowedMemoryNodes", "EffectiveCPUs", "EffectiveMemoryNodes")) { |
a047f4f1 MS |
5389 | _cleanup_free_ char *affinity = NULL; |
5390 | _cleanup_(cpu_set_reset) CPUSet set = {}; | |
5391 | const void *a; | |
5392 | size_t n; | |
5393 | ||
5394 | r = sd_bus_message_read_array(m, 'y', &a, &n); | |
5395 | if (r < 0) | |
5396 | return bus_log_parse_error(r); | |
5397 | ||
5398 | r = cpu_set_from_dbus(a, n, &set); | |
5399 | if (r < 0) | |
b070c7c0 | 5400 | return log_error_errno(r, "Failed to deserialize %s: %m", name); |
a047f4f1 MS |
5401 | |
5402 | affinity = cpu_set_to_range_string(&set); | |
5403 | if (!affinity) | |
5404 | return log_oom(); | |
5405 | ||
5406 | bus_print_property_value(name, expected_value, value, affinity); | |
5407 | ||
305a2850 | 5408 | return 1; |
48220598 LP |
5409 | } |
5410 | ||
5411 | break; | |
5412 | } | |
5413 | ||
48220598 LP |
5414 | return 0; |
5415 | } | |
5416 | ||
8559e61d YW |
5417 | typedef enum SystemctlShowMode{ |
5418 | SYSTEMCTL_SHOW_PROPERTIES, | |
5419 | SYSTEMCTL_SHOW_STATUS, | |
5420 | SYSTEMCTL_SHOW_HELP, | |
5421 | _SYSTEMCTL_SHOW_MODE_MAX, | |
5422 | _SYSTEMCTL_SHOW_MODE_INVALID = -1, | |
5423 | } SystemctlShowMode; | |
5424 | ||
aa8d423e | 5425 | static const char* const systemctl_show_mode_table[_SYSTEMCTL_SHOW_MODE_MAX] = { |
8559e61d YW |
5426 | [SYSTEMCTL_SHOW_PROPERTIES] = "show", |
5427 | [SYSTEMCTL_SHOW_STATUS] = "status", | |
5428 | [SYSTEMCTL_SHOW_HELP] = "help", | |
5429 | }; | |
5430 | ||
5431 | DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(systemctl_show_mode, SystemctlShowMode); | |
5432 | ||
f459b602 | 5433 | static int show_one( |
f459b602 MAP |
5434 | sd_bus *bus, |
5435 | const char *path, | |
3dced37b | 5436 | const char *unit, |
8559e61d | 5437 | SystemctlShowMode show_mode, |
f459b602 MAP |
5438 | bool *new_line, |
5439 | bool *ellipsized) { | |
5440 | ||
3dced37b | 5441 | static const struct bus_properties_map property_map[] = { |
b3d59367 AZ |
5442 | { "LoadState", "s", NULL, offsetof(UnitStatusInfo, load_state) }, |
5443 | { "ActiveState", "s", NULL, offsetof(UnitStatusInfo, active_state) }, | |
d9e45bc3 | 5444 | { "FreezerState", "s", NULL, offsetof(UnitStatusInfo, freezer_state) }, |
b3d59367 | 5445 | { "Documentation", "as", NULL, offsetof(UnitStatusInfo, documentation) }, |
4679a8c3 YW |
5446 | {} |
5447 | }, status_map[] = { | |
b3d59367 AZ |
5448 | { "Id", "s", NULL, offsetof(UnitStatusInfo, id) }, |
5449 | { "LoadState", "s", NULL, offsetof(UnitStatusInfo, load_state) }, | |
5450 | { "ActiveState", "s", NULL, offsetof(UnitStatusInfo, active_state) }, | |
d9e45bc3 | 5451 | { "FreezerState", "s", NULL, offsetof(UnitStatusInfo, freezer_state) }, |
b3d59367 AZ |
5452 | { "SubState", "s", NULL, offsetof(UnitStatusInfo, sub_state) }, |
5453 | { "UnitFileState", "s", NULL, offsetof(UnitStatusInfo, unit_file_state) }, | |
5454 | { "UnitFilePreset", "s", NULL, offsetof(UnitStatusInfo, unit_file_preset) }, | |
5455 | { "Description", "s", NULL, offsetof(UnitStatusInfo, description) }, | |
5456 | { "Following", "s", NULL, offsetof(UnitStatusInfo, following) }, | |
5457 | { "Documentation", "as", NULL, offsetof(UnitStatusInfo, documentation) }, | |
5458 | { "FragmentPath", "s", NULL, offsetof(UnitStatusInfo, fragment_path) }, | |
5459 | { "SourcePath", "s", NULL, offsetof(UnitStatusInfo, source_path) }, | |
5460 | { "ControlGroup", "s", NULL, offsetof(UnitStatusInfo, control_group) }, | |
5461 | { "DropInPaths", "as", NULL, offsetof(UnitStatusInfo, dropin_paths) }, | |
5462 | { "LoadError", "(ss)", map_load_error, offsetof(UnitStatusInfo, load_error) }, | |
5463 | { "Result", "s", NULL, offsetof(UnitStatusInfo, result) }, | |
965c5d1d KK |
5464 | { "TriggeredBy", "as", NULL, offsetof(UnitStatusInfo, triggered_by) }, |
5465 | { "Triggers", "as", NULL, offsetof(UnitStatusInfo, triggers) }, | |
b3d59367 AZ |
5466 | { "InactiveExitTimestamp", "t", NULL, offsetof(UnitStatusInfo, inactive_exit_timestamp) }, |
5467 | { "InactiveExitTimestampMonotonic", "t", NULL, offsetof(UnitStatusInfo, inactive_exit_timestamp_monotonic) }, | |
5468 | { "ActiveEnterTimestamp", "t", NULL, offsetof(UnitStatusInfo, active_enter_timestamp) }, | |
5469 | { "ActiveExitTimestamp", "t", NULL, offsetof(UnitStatusInfo, active_exit_timestamp) }, | |
5470 | { "InactiveEnterTimestamp", "t", NULL, offsetof(UnitStatusInfo, inactive_enter_timestamp) }, | |
5471 | { "NeedDaemonReload", "b", NULL, offsetof(UnitStatusInfo, need_daemon_reload) }, | |
5472 | { "Transient", "b", NULL, offsetof(UnitStatusInfo, transient) }, | |
5473 | { "ExecMainPID", "u", NULL, offsetof(UnitStatusInfo, main_pid) }, | |
5474 | { "MainPID", "u", map_main_pid, 0 }, | |
5475 | { "ControlPID", "u", NULL, offsetof(UnitStatusInfo, control_pid) }, | |
5476 | { "StatusText", "s", NULL, offsetof(UnitStatusInfo, status_text) }, | |
5477 | { "PIDFile", "s", NULL, offsetof(UnitStatusInfo, pid_file) }, | |
5478 | { "StatusErrno", "i", NULL, offsetof(UnitStatusInfo, status_errno) }, | |
5479 | { "ExecMainStartTimestamp", "t", NULL, offsetof(UnitStatusInfo, start_timestamp) }, | |
5480 | { "ExecMainExitTimestamp", "t", NULL, offsetof(UnitStatusInfo, exit_timestamp) }, | |
5481 | { "ExecMainCode", "i", NULL, offsetof(UnitStatusInfo, exit_code) }, | |
5482 | { "ExecMainStatus", "i", NULL, offsetof(UnitStatusInfo, exit_status) }, | |
d93dda3a | 5483 | { "LogNamespace", "s", NULL, offsetof(UnitStatusInfo, log_namespace) }, |
b3d59367 AZ |
5484 | { "ConditionTimestamp", "t", NULL, offsetof(UnitStatusInfo, condition_timestamp) }, |
5485 | { "ConditionResult", "b", NULL, offsetof(UnitStatusInfo, condition_result) }, | |
5486 | { "Conditions", "a(sbbsi)", map_conditions, 0 }, | |
5487 | { "AssertTimestamp", "t", NULL, offsetof(UnitStatusInfo, assert_timestamp) }, | |
5488 | { "AssertResult", "b", NULL, offsetof(UnitStatusInfo, assert_result) }, | |
5489 | { "Asserts", "a(sbbsi)", map_asserts, 0 }, | |
5490 | { "NextElapseUSecRealtime", "t", NULL, offsetof(UnitStatusInfo, next_elapse_real) }, | |
5491 | { "NextElapseUSecMonotonic", "t", NULL, offsetof(UnitStatusInfo, next_elapse_monotonic) }, | |
5492 | { "NAccepted", "u", NULL, offsetof(UnitStatusInfo, n_accepted) }, | |
5493 | { "NConnections", "u", NULL, offsetof(UnitStatusInfo, n_connections) }, | |
5494 | { "NRefused", "u", NULL, offsetof(UnitStatusInfo, n_refused) }, | |
5495 | { "Accept", "b", NULL, offsetof(UnitStatusInfo, accept) }, | |
5496 | { "Listen", "a(ss)", map_listen, offsetof(UnitStatusInfo, listen) }, | |
5497 | { "SysFSPath", "s", NULL, offsetof(UnitStatusInfo, sysfs_path) }, | |
5498 | { "Where", "s", NULL, offsetof(UnitStatusInfo, where) }, | |
5499 | { "What", "s", NULL, offsetof(UnitStatusInfo, what) }, | |
5500 | { "MemoryCurrent", "t", NULL, offsetof(UnitStatusInfo, memory_current) }, | |
5501 | { "DefaultMemoryMin", "t", NULL, offsetof(UnitStatusInfo, default_memory_min) }, | |
5502 | { "DefaultMemoryLow", "t", NULL, offsetof(UnitStatusInfo, default_memory_low) }, | |
5503 | { "MemoryMin", "t", NULL, offsetof(UnitStatusInfo, memory_min) }, | |
5504 | { "MemoryLow", "t", NULL, offsetof(UnitStatusInfo, memory_low) }, | |
5505 | { "MemoryHigh", "t", NULL, offsetof(UnitStatusInfo, memory_high) }, | |
5506 | { "MemoryMax", "t", NULL, offsetof(UnitStatusInfo, memory_max) }, | |
5507 | { "MemorySwapMax", "t", NULL, offsetof(UnitStatusInfo, memory_swap_max) }, | |
5508 | { "MemoryLimit", "t", NULL, offsetof(UnitStatusInfo, memory_limit) }, | |
5509 | { "CPUUsageNSec", "t", NULL, offsetof(UnitStatusInfo, cpu_usage_nsec) }, | |
5510 | { "TasksCurrent", "t", NULL, offsetof(UnitStatusInfo, tasks_current) }, | |
5511 | { "TasksMax", "t", NULL, offsetof(UnitStatusInfo, tasks_max) }, | |
5512 | { "IPIngressBytes", "t", NULL, offsetof(UnitStatusInfo, ip_ingress_bytes) }, | |
5513 | { "IPEgressBytes", "t", NULL, offsetof(UnitStatusInfo, ip_egress_bytes) }, | |
5514 | { "IOReadBytes", "t", NULL, offsetof(UnitStatusInfo, io_read_bytes) }, | |
5515 | { "IOWriteBytes", "t", NULL, offsetof(UnitStatusInfo, io_write_bytes) }, | |
898fc00e AZ |
5516 | { "ExecCondition", "a(sasbttttuii)", map_exec, 0 }, |
5517 | { "ExecConditionEx", "a(sasasttttuii)", map_exec, 0 }, | |
b3d59367 AZ |
5518 | { "ExecStartPre", "a(sasbttttuii)", map_exec, 0 }, |
5519 | { "ExecStartPreEx", "a(sasasttttuii)", map_exec, 0 }, | |
5520 | { "ExecStart", "a(sasbttttuii)", map_exec, 0 }, | |
5521 | { "ExecStartEx", "a(sasasttttuii)", map_exec, 0 }, | |
5522 | { "ExecStartPost", "a(sasbttttuii)", map_exec, 0 }, | |
5523 | { "ExecStartPostEx", "a(sasasttttuii)", map_exec, 0 }, | |
5524 | { "ExecReload", "a(sasbttttuii)", map_exec, 0 }, | |
898fc00e | 5525 | { "ExecReloadEx", "a(sasasttttuii)", map_exec, 0 }, |
b3d59367 AZ |
5526 | { "ExecStopPre", "a(sasbttttuii)", map_exec, 0 }, |
5527 | { "ExecStop", "a(sasbttttuii)", map_exec, 0 }, | |
898fc00e | 5528 | { "ExecStopEx", "a(sasasttttuii)", map_exec, 0 }, |
b3d59367 | 5529 | { "ExecStopPost", "a(sasbttttuii)", map_exec, 0 }, |
898fc00e | 5530 | { "ExecStopPostEx", "a(sasasttttuii)", map_exec, 0 }, |
3dced37b LP |
5531 | {} |
5532 | }; | |
5533 | ||
4afd3348 LP |
5534 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
5535 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
3dced37b | 5536 | _cleanup_set_free_ Set *found_properties = NULL; |
a7335518 | 5537 | _cleanup_(unit_status_info_free) UnitStatusInfo info = { |
934277fe | 5538 | .memory_current = (uint64_t) -1, |
da4d897e TH |
5539 | .memory_high = CGROUP_LIMIT_MAX, |
5540 | .memory_max = CGROUP_LIMIT_MAX, | |
96e131ea | 5541 | .memory_swap_max = CGROUP_LIMIT_MAX, |
934277fe | 5542 | .memory_limit = (uint64_t) -1, |
5ad096b3 | 5543 | .cpu_usage_nsec = (uint64_t) -1, |
03a7b521 LP |
5544 | .tasks_current = (uint64_t) -1, |
5545 | .tasks_max = (uint64_t) -1, | |
0e97c93f DM |
5546 | .ip_ingress_bytes = (uint64_t) -1, |
5547 | .ip_egress_bytes = (uint64_t) -1, | |
7a5de3f9 LP |
5548 | .io_read_bytes = UINT64_MAX, |
5549 | .io_write_bytes = UINT64_MAX, | |
934277fe | 5550 | }; |
4679a8c3 | 5551 | char **pp; |
f459b602 | 5552 | int r; |
48220598 | 5553 | |
48220598 | 5554 | assert(path); |
61cbdc4b | 5555 | assert(new_line); |
48220598 | 5556 | |
e3e0314b ZJS |
5557 | log_debug("Showing one %s", path); |
5558 | ||
4679a8c3 | 5559 | r = bus_map_all_properties( |
f22f08cd SP |
5560 | bus, |
5561 | "org.freedesktop.systemd1", | |
5562 | path, | |
4679a8c3 | 5563 | show_mode == SYSTEMCTL_SHOW_STATUS ? status_map : property_map, |
a7e4861c | 5564 | BUS_MAP_BOOLEAN_AS_BOOL, |
f459b602 | 5565 | &error, |
f22f08cd | 5566 | &reply, |
4679a8c3 | 5567 | &info); |
4c3e8e39 LP |
5568 | if (r < 0) |
5569 | return log_error_errno(r, "Failed to get properties: %s", bus_error_message(&error, r)); | |
48220598 | 5570 | |
4679a8c3 YW |
5571 | if (unit && streq_ptr(info.load_state, "not-found") && streq_ptr(info.active_state, "inactive")) { |
5572 | log_full(show_mode == SYSTEMCTL_SHOW_STATUS ? LOG_ERR : LOG_DEBUG, | |
5573 | "Unit %s could not be found.", unit); | |
e33a06a1 | 5574 | |
4679a8c3 YW |
5575 | if (show_mode == SYSTEMCTL_SHOW_STATUS) |
5576 | return EXIT_PROGRAM_OR_SERVICES_STATUS_UNKNOWN; | |
5577 | else if (show_mode == SYSTEMCTL_SHOW_HELP) | |
5578 | return -ENOENT; | |
5579 | } | |
e33a06a1 | 5580 | |
4679a8c3 YW |
5581 | if (*new_line) |
5582 | printf("\n"); | |
3dced37b | 5583 | |
4679a8c3 YW |
5584 | *new_line = true; |
5585 | ||
5586 | if (show_mode == SYSTEMCTL_SHOW_STATUS) { | |
5587 | print_status_info(bus, &info, ellipsized); | |
5588 | ||
5589 | if (info.active_state && !STR_IN_SET(info.active_state, "active", "reloading")) | |
5590 | return EXIT_PROGRAM_NOT_RUNNING; | |
5591 | ||
5592 | return EXIT_PROGRAM_RUNNING_OR_SERVICE_OK; | |
5593 | ||
5594 | } else if (show_mode == SYSTEMCTL_SHOW_HELP) { | |
5595 | show_unit_help(&info); | |
5596 | return 0; | |
3dced37b | 5597 | } |
e33a06a1 | 5598 | |
4679a8c3 YW |
5599 | r = sd_bus_message_rewind(reply, true); |
5600 | if (r < 0) | |
5601 | return log_error_errno(r, "Failed to rewind: %s", bus_error_message(&error, r)); | |
5602 | ||
07636114 | 5603 | r = bus_message_print_all_properties(reply, print_property, arg_properties, arg_value, arg_all, &found_properties); |
f459b602 MAP |
5604 | if (r < 0) |
5605 | return bus_log_parse_error(r); | |
48220598 | 5606 | |
4679a8c3 YW |
5607 | STRV_FOREACH(pp, arg_properties) |
5608 | if (!set_contains(found_properties, *pp)) | |
5609 | log_debug("Property %s does not exist.", *pp); | |
3dced37b | 5610 | |
4679a8c3 | 5611 | return 0; |
48220598 LP |
5612 | } |
5613 | ||
f74294c1 | 5614 | static int get_unit_dbus_path_by_pid( |
f459b602 MAP |
5615 | sd_bus *bus, |
5616 | uint32_t pid, | |
f74294c1 | 5617 | char **unit) { |
f459b602 | 5618 | |
4afd3348 LP |
5619 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
5620 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
373d32c9 | 5621 | char *u; |
a223b325 MS |
5622 | int r; |
5623 | ||
8010c205 | 5624 | r = bus_call_method(bus, bus_systemd_mgr, "GetUnitByPID", &error, &reply, "u", pid); |
691395d8 LP |
5625 | if (r < 0) |
5626 | return log_error_errno(r, "Failed to get unit for PID %"PRIu32": %s", pid, bus_error_message(&error, r)); | |
a223b325 | 5627 | |
373d32c9 | 5628 | r = sd_bus_message_read(reply, "o", &u); |
f459b602 MAP |
5629 | if (r < 0) |
5630 | return bus_log_parse_error(r); | |
5631 | ||
373d32c9 LP |
5632 | u = strdup(u); |
5633 | if (!u) | |
5634 | return log_oom(); | |
5635 | ||
5636 | *unit = u; | |
f74294c1 | 5637 | return 0; |
a223b325 MS |
5638 | } |
5639 | ||
f459b602 | 5640 | static int show_all( |
f459b602 | 5641 | sd_bus *bus, |
f459b602 MAP |
5642 | bool *new_line, |
5643 | bool *ellipsized) { | |
5644 | ||
4afd3348 | 5645 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
f459b602 MAP |
5646 | _cleanup_free_ UnitInfo *unit_infos = NULL; |
5647 | const UnitInfo *u; | |
5648 | unsigned c; | |
5bb75bc7 | 5649 | int r, ret = 0; |
265a7a2a | 5650 | |
1238ee09 | 5651 | r = get_unit_list(bus, NULL, NULL, &unit_infos, 0, &reply); |
265a7a2a ZJS |
5652 | if (r < 0) |
5653 | return r; | |
5654 | ||
0221d68a | 5655 | (void) pager_open(arg_pager_flags); |
dbed408b | 5656 | |
f459b602 MAP |
5657 | c = (unsigned) r; |
5658 | ||
93bab288 | 5659 | typesafe_qsort(unit_infos, c, compare_unit_info); |
991f2a39 | 5660 | |
265a7a2a | 5661 | for (u = unit_infos; u < unit_infos + c; u++) { |
7fd1b19b | 5662 | _cleanup_free_ char *p = NULL; |
265a7a2a | 5663 | |
265a7a2a ZJS |
5664 | p = unit_dbus_path_from_name(u->id); |
5665 | if (!p) | |
5666 | return log_oom(); | |
5667 | ||
8559e61d | 5668 | r = show_one(bus, p, u->id, SYSTEMCTL_SHOW_STATUS, new_line, ellipsized); |
3df538da | 5669 | if (r < 0) |
265a7a2a | 5670 | return r; |
5bb75bc7 ZJS |
5671 | else if (r > 0 && ret == 0) |
5672 | ret = r; | |
265a7a2a ZJS |
5673 | } |
5674 | ||
5bb75bc7 | 5675 | return ret; |
265a7a2a ZJS |
5676 | } |
5677 | ||
8fcf784d LP |
5678 | static int show_system_status(sd_bus *bus) { |
5679 | char since1[FORMAT_TIMESTAMP_RELATIVE_MAX], since2[FORMAT_TIMESTAMP_MAX]; | |
f9e0eefc | 5680 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
e7e55dbd | 5681 | _cleanup_(machine_info_clear) struct machine_info mi = {}; |
f9e0eefc | 5682 | _cleanup_free_ char *hn = NULL; |
8fcf784d LP |
5683 | const char *on, *off; |
5684 | int r; | |
5685 | ||
5686 | hn = gethostname_malloc(); | |
5687 | if (!hn) | |
5688 | return log_oom(); | |
5689 | ||
a7e4861c YW |
5690 | r = bus_map_all_properties( |
5691 | bus, | |
5692 | "org.freedesktop.systemd1", | |
5693 | "/org/freedesktop/systemd1", | |
5694 | machine_info_property_map, | |
5695 | BUS_MAP_STRDUP, | |
5696 | &error, | |
5697 | NULL, | |
5698 | &mi); | |
f647962d | 5699 | if (r < 0) |
f9e0eefc | 5700 | return log_error_errno(r, "Failed to read server status: %s", bus_error_message(&error, r)); |
8fcf784d | 5701 | |
8fcf784d LP |
5702 | if (streq_ptr(mi.state, "degraded")) { |
5703 | on = ansi_highlight_red(); | |
1fc464f6 | 5704 | off = ansi_normal(); |
92f34a9c JR |
5705 | } else if (streq_ptr(mi.state, "running")) { |
5706 | on = ansi_highlight_green(); | |
5707 | off = ansi_normal(); | |
5708 | } else { | |
8fcf784d | 5709 | on = ansi_highlight_yellow(); |
1fc464f6 | 5710 | off = ansi_normal(); |
92f34a9c | 5711 | } |
8fcf784d | 5712 | |
9a6f746f | 5713 | printf("%s%s%s %s\n", on, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE), off, arg_host ? arg_host : hn); |
b0d14c69 | 5714 | |
8fcf784d LP |
5715 | printf(" State: %s%s%s\n", |
5716 | on, strna(mi.state), off); | |
5717 | ||
c6ba5b80 MP |
5718 | printf(" Jobs: %" PRIu32 " queued\n", mi.n_jobs); |
5719 | printf(" Failed: %" PRIu32 " units\n", mi.n_failed_units); | |
8fcf784d LP |
5720 | |
5721 | printf(" Since: %s; %s\n", | |
5722 | format_timestamp(since2, sizeof(since2), mi.timestamp), | |
5723 | format_timestamp_relative(since1, sizeof(since1), mi.timestamp)); | |
5724 | ||
5725 | printf(" CGroup: %s\n", mi.control_group ?: "/"); | |
4c315c2c IS |
5726 | if (IN_SET(arg_transport, |
5727 | BUS_TRANSPORT_LOCAL, | |
5728 | BUS_TRANSPORT_MACHINE)) { | |
8fcf784d LP |
5729 | static const char prefix[] = " "; |
5730 | unsigned c; | |
5731 | ||
5732 | c = columns(); | |
5733 | if (c > sizeof(prefix) - 1) | |
5734 | c -= sizeof(prefix) - 1; | |
5735 | else | |
5736 | c = 0; | |
5737 | ||
0ff308c8 | 5738 | show_cgroup(SYSTEMD_CGROUP_CONTROLLER, strempty(mi.control_group), prefix, c, get_output_flags()); |
8fcf784d LP |
5739 | } |
5740 | ||
8fcf784d LP |
5741 | return 0; |
5742 | } | |
5743 | ||
e449de87 | 5744 | static int show(int argc, char *argv[], void *userdata) { |
8559e61d YW |
5745 | bool new_line = false, ellipsized = false; |
5746 | SystemctlShowMode show_mode; | |
e3e0314b | 5747 | int r, ret = 0; |
4fbd7192 | 5748 | sd_bus *bus; |
48220598 | 5749 | |
e449de87 | 5750 | assert(argv); |
48220598 | 5751 | |
8559e61d | 5752 | show_mode = systemctl_show_mode_from_string(argv[0]); |
baaa35ad ZJS |
5753 | if (show_mode < 0) |
5754 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
5755 | "Invalid argument."); | |
e449de87 | 5756 | |
baaa35ad ZJS |
5757 | if (show_mode == SYSTEMCTL_SHOW_HELP && argc <= 1) |
5758 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
77102db2 ZJS |
5759 | "'help' command expects one or more unit names.\n" |
5760 | "(Alternatively, help for systemctl itself may be shown with --help)"); | |
61cbdc4b | 5761 | |
d2ad7e1b ZJS |
5762 | r = acquire_bus(BUS_MANAGER, &bus); |
5763 | if (r < 0) | |
5764 | return r; | |
5765 | ||
0221d68a | 5766 | (void) pager_open(arg_pager_flags); |
ec14911e | 5767 | |
4fbd7192 | 5768 | /* If no argument is specified inspect the manager itself */ |
8559e61d YW |
5769 | if (show_mode == SYSTEMCTL_SHOW_PROPERTIES && argc <= 1) |
5770 | return show_one(bus, "/org/freedesktop/systemd1", NULL, show_mode, &new_line, &ellipsized); | |
48220598 | 5771 | |
8559e61d | 5772 | if (show_mode == SYSTEMCTL_SHOW_STATUS && argc <= 1) { |
8fcf784d | 5773 | |
8fcf784d LP |
5774 | show_system_status(bus); |
5775 | new_line = true; | |
5776 | ||
5777 | if (arg_all) | |
8559e61d | 5778 | ret = show_all(bus, &new_line, &ellipsized); |
8fcf784d | 5779 | } else { |
e3e0314b ZJS |
5780 | _cleanup_free_ char **patterns = NULL; |
5781 | char **name; | |
5782 | ||
e449de87 | 5783 | STRV_FOREACH(name, strv_skip(argv, 1)) { |
3dced37b | 5784 | _cleanup_free_ char *path = NULL, *unit = NULL; |
94e0bd7d | 5785 | uint32_t id; |
48220598 | 5786 | |
94e0bd7d | 5787 | if (safe_atou32(*name, &id) < 0) { |
e3e0314b | 5788 | if (strv_push(&patterns, *name) < 0) |
94e0bd7d | 5789 | return log_oom(); |
48220598 | 5790 | |
e3e0314b | 5791 | continue; |
8559e61d | 5792 | } else if (show_mode == SYSTEMCTL_SHOW_PROPERTIES) { |
94e0bd7d | 5793 | /* Interpret as job id */ |
3dced37b | 5794 | if (asprintf(&path, "/org/freedesktop/systemd1/job/%u", id) < 0) |
94e0bd7d | 5795 | return log_oom(); |
48220598 | 5796 | |
94e0bd7d ZJS |
5797 | } else { |
5798 | /* Interpret as PID */ | |
3dced37b | 5799 | r = get_unit_dbus_path_by_pid(bus, id, &path); |
373d32c9 | 5800 | if (r < 0) { |
94e0bd7d | 5801 | ret = r; |
373d32c9 LP |
5802 | continue; |
5803 | } | |
3dced37b LP |
5804 | |
5805 | r = unit_name_from_dbus_path(path, &unit); | |
5806 | if (r < 0) | |
5807 | return log_oom(); | |
94e0bd7d | 5808 | } |
f74294c1 | 5809 | |
8559e61d | 5810 | r = show_one(bus, path, unit, show_mode, &new_line, &ellipsized); |
5bb75bc7 ZJS |
5811 | if (r < 0) |
5812 | return r; | |
5813 | else if (r > 0 && ret == 0) | |
5814 | ret = r; | |
48220598 | 5815 | } |
94e0bd7d | 5816 | |
e3e0314b ZJS |
5817 | if (!strv_isempty(patterns)) { |
5818 | _cleanup_strv_free_ char **names = NULL; | |
5819 | ||
1d56bc09 | 5820 | r = expand_names(bus, patterns, NULL, &names, NULL); |
e3e0314b | 5821 | if (r < 0) |
691395d8 | 5822 | return log_error_errno(r, "Failed to expand names: %m"); |
e3e0314b | 5823 | |
e9c387c8 KK |
5824 | r = maybe_extend_with_unit_dependencies(bus, &names); |
5825 | if (r < 0) | |
5826 | return r; | |
5827 | ||
e3e0314b | 5828 | STRV_FOREACH(name, names) { |
3dced37b | 5829 | _cleanup_free_ char *path; |
e3e0314b | 5830 | |
3dced37b LP |
5831 | path = unit_dbus_path_from_name(*name); |
5832 | if (!path) | |
e3e0314b ZJS |
5833 | return log_oom(); |
5834 | ||
8559e61d | 5835 | r = show_one(bus, path, *name, show_mode, &new_line, &ellipsized); |
5bb75bc7 ZJS |
5836 | if (r < 0) |
5837 | return r; | |
3dced37b | 5838 | if (r > 0 && ret == 0) |
5bb75bc7 | 5839 | ret = r; |
e3e0314b ZJS |
5840 | } |
5841 | } | |
5842 | } | |
5843 | ||
94e0bd7d ZJS |
5844 | if (ellipsized && !arg_quiet) |
5845 | printf("Hint: Some lines were ellipsized, use -l to show in full.\n"); | |
48220598 | 5846 | |
22f4096c | 5847 | return ret; |
0183528f LP |
5848 | } |
5849 | ||
e449de87 | 5850 | static int cat(int argc, char *argv[], void *userdata) { |
8e766630 | 5851 | _cleanup_(lookup_paths_free) LookupPaths lp = {}; |
15ef1144 LP |
5852 | _cleanup_strv_free_ char **names = NULL; |
5853 | char **name; | |
4fbd7192 LP |
5854 | sd_bus *bus; |
5855 | bool first = true; | |
a25457f5 | 5856 | int r, rc = 0; |
15ef1144 | 5857 | |
4c70211c | 5858 | /* Include all units by default — i.e. continue as if the --all |
4eb53120 FS |
5859 | * option was used */ |
5860 | if (strv_isempty(arg_states)) | |
5861 | arg_all = true; | |
5862 | ||
2f50e59f LP |
5863 | if (arg_transport != BUS_TRANSPORT_LOCAL) |
5864 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot remotely cat units."); | |
3e495a66 | 5865 | |
4943d143 | 5866 | r = lookup_paths_init(&lp, arg_scope, 0, arg_root); |
ad2a0358 | 5867 | if (r < 0) |
c51932be | 5868 | return log_error_errno(r, "Failed to determine unit paths: %m"); |
ad2a0358 | 5869 | |
4fbd7192 | 5870 | r = acquire_bus(BUS_MANAGER, &bus); |
15ef1144 | 5871 | if (r < 0) |
4fbd7192 | 5872 | return r; |
15ef1144 | 5873 | |
1d56bc09 | 5874 | r = expand_names(bus, strv_skip(argv, 1), NULL, &names, NULL); |
4fbd7192 LP |
5875 | if (r < 0) |
5876 | return log_error_errno(r, "Failed to expand names: %m"); | |
ad2a0358 | 5877 | |
e9c387c8 KK |
5878 | r = maybe_extend_with_unit_dependencies(bus, &names); |
5879 | if (r < 0) | |
5880 | return r; | |
5881 | ||
0221d68a | 5882 | (void) pager_open(arg_pager_flags); |
15ef1144 LP |
5883 | |
5884 | STRV_FOREACH(name, names) { | |
ad2a0358 | 5885 | _cleanup_free_ char *fragment_path = NULL; |
15ef1144 | 5886 | _cleanup_strv_free_ char **dropin_paths = NULL; |
15ef1144 | 5887 | |
02a126a3 | 5888 | r = unit_find_paths(bus, *name, &lp, false, &fragment_path, &dropin_paths); |
86f004fb | 5889 | if (r == -ERFKILL) { |
02a126a3 LP |
5890 | printf("%s# Unit %s is masked%s.\n", |
5891 | ansi_highlight_magenta(), | |
5892 | *name, | |
5893 | ansi_normal()); | |
5894 | continue; | |
5895 | } | |
5896 | if (r == -EKEYREJECTED) { | |
5897 | printf("%s# Unit %s could not be loaded.%s\n", | |
86f004fb ZJS |
5898 | ansi_highlight_magenta(), |
5899 | *name, | |
5900 | ansi_normal()); | |
5901 | continue; | |
5902 | } | |
ad2a0358 ZJS |
5903 | if (r < 0) |
5904 | return r; | |
a25457f5 FS |
5905 | if (r == 0) { |
5906 | /* Skip units which have no on-disk counterpart, but | |
5907 | * propagate the error to the user */ | |
5908 | rc = -ENOENT; | |
5909 | continue; | |
5910 | } | |
15ef1144 LP |
5911 | |
5912 | if (first) | |
5913 | first = false; | |
5914 | else | |
5915 | puts(""); | |
5916 | ||
5b9635d1 | 5917 | if (need_daemon_reload(bus, *name) > 0) /* ignore errors (<0), this is informational output */ |
e100155d LW |
5918 | fprintf(stderr, |
5919 | "%s# Warning: %s changed on disk, the version systemd has loaded is outdated.\n" | |
5920 | "%s# This output shows the current version of the unit's original fragment and drop-in files.\n" | |
5921 | "%s# If fragments or drop-ins were added or removed, they are not properly reflected in this output.\n" | |
5922 | "%s# Run 'systemctl%s daemon-reload' to reload units.%s\n", | |
5923 | ansi_highlight_red(), | |
5924 | *name, | |
5925 | ansi_highlight_red(), | |
5926 | ansi_highlight_red(), | |
5927 | ansi_highlight_red(), | |
5928 | arg_scope == UNIT_FILE_SYSTEM ? "" : " --user", | |
5929 | ansi_normal()); | |
5930 | ||
854a42fb | 5931 | r = cat_files(fragment_path, dropin_paths, 0); |
81f5e513 ZJS |
5932 | if (r < 0) |
5933 | return r; | |
15ef1144 LP |
5934 | } |
5935 | ||
a25457f5 | 5936 | return rc; |
15ef1144 LP |
5937 | } |
5938 | ||
e449de87 | 5939 | static int set_property(int argc, char *argv[], void *userdata) { |
4afd3348 LP |
5940 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
5941 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
68372da6 | 5942 | _cleanup_free_ char *n = NULL; |
89ada3ba | 5943 | UnitType t; |
4fbd7192 | 5944 | sd_bus *bus; |
8e2af478 LP |
5945 | int r; |
5946 | ||
4fbd7192 LP |
5947 | r = acquire_bus(BUS_MANAGER, &bus); |
5948 | if (r < 0) | |
5949 | return r; | |
5950 | ||
8a4b13c5 | 5951 | polkit_agent_open_maybe(); |
d2ad7e1b | 5952 | |
8010c205 | 5953 | r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "SetUnitProperties"); |
f459b602 MAP |
5954 | if (r < 0) |
5955 | return bus_log_create_error(r); | |
8e2af478 | 5956 | |
37cbc1d5 | 5957 | r = unit_name_mangle(argv[1], arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN, &n); |
7410616c LP |
5958 | if (r < 0) |
5959 | return log_error_errno(r, "Failed to mangle unit name: %m"); | |
68372da6 | 5960 | |
89ada3ba | 5961 | t = unit_name_to_type(n); |
2f50e59f LP |
5962 | if (t < 0) |
5963 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid unit type: %s", n); | |
89ada3ba | 5964 | |
f459b602 MAP |
5965 | r = sd_bus_message_append(m, "sb", n, arg_runtime); |
5966 | if (r < 0) | |
5967 | return bus_log_create_error(r); | |
8e2af478 | 5968 | |
f459b602 MAP |
5969 | r = sd_bus_message_open_container(m, SD_BUS_TYPE_ARRAY, "(sv)"); |
5970 | if (r < 0) | |
5971 | return bus_log_create_error(r); | |
8e2af478 | 5972 | |
89ada3ba | 5973 | r = bus_append_unit_property_assignment_many(m, t, strv_skip(argv, 2)); |
8673cf13 LP |
5974 | if (r < 0) |
5975 | return r; | |
8e2af478 | 5976 | |
f459b602 MAP |
5977 | r = sd_bus_message_close_container(m); |
5978 | if (r < 0) | |
5979 | return bus_log_create_error(r); | |
8e2af478 | 5980 | |
c49b30a2 | 5981 | r = sd_bus_call(bus, m, 0, &error, NULL); |
691395d8 LP |
5982 | if (r < 0) |
5983 | return log_error_errno(r, "Failed to set unit properties on %s: %s", n, bus_error_message(&error, r)); | |
8e2af478 LP |
5984 | |
5985 | return 0; | |
5986 | } | |
5987 | ||
e449de87 | 5988 | static int daemon_reload(int argc, char *argv[], void *userdata) { |
4afd3348 | 5989 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
2853b60a | 5990 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
7e4249b9 | 5991 | const char *method; |
4fbd7192 | 5992 | sd_bus *bus; |
f459b602 | 5993 | int r; |
7e4249b9 | 5994 | |
4fbd7192 LP |
5995 | r = acquire_bus(BUS_MANAGER, &bus); |
5996 | if (r < 0) | |
5997 | return r; | |
5998 | ||
8a4b13c5 | 5999 | polkit_agent_open_maybe(); |
d2ad7e1b | 6000 | |
2853b60a LP |
6001 | switch (arg_action) { |
6002 | ||
6003 | case ACTION_RELOAD: | |
e4b61340 | 6004 | method = "Reload"; |
2853b60a LP |
6005 | break; |
6006 | ||
6007 | case ACTION_REEXEC: | |
e4b61340 | 6008 | method = "Reexecute"; |
2853b60a LP |
6009 | break; |
6010 | ||
6011 | case ACTION_SYSTEMCTL: | |
6012 | method = streq(argv[0], "daemon-reexec") ? "Reexecute" : | |
6013 | /* "daemon-reload" */ "Reload"; | |
6014 | break; | |
e4b61340 | 6015 | |
2853b60a LP |
6016 | default: |
6017 | assert_not_reached("Unexpected action"); | |
e4b61340 | 6018 | } |
7e4249b9 | 6019 | |
8010c205 | 6020 | r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, method); |
2853b60a LP |
6021 | if (r < 0) |
6022 | return bus_log_create_error(r); | |
6023 | ||
6024 | /* Note we use an extra-long timeout here. This is because a reload or reexec means generators are rerun which | |
6025 | * are timed out after DEFAULT_TIMEOUT_USEC. Let's use twice that time here, so that the generators can have | |
6026 | * their timeout, and for everything else there's the same time budget in place. */ | |
6027 | ||
6028 | r = sd_bus_call(bus, m, DEFAULT_TIMEOUT_USEC * 2, &error, NULL); | |
6029 | ||
6030 | /* On reexecution, we expect a disconnect, not a reply */ | |
6031 | if (IN_SET(r, -ETIMEDOUT, -ECONNRESET) && streq(method, "Reexecute")) | |
6032 | r = 0; | |
6033 | ||
6034 | if (r < 0 && arg_action == ACTION_SYSTEMCTL) | |
6035 | return log_error_errno(r, "Failed to reload daemon: %s", bus_error_message(&error, r)); | |
6036 | ||
6037 | /* Note that for the legacy commands (i.e. those with action != ACTION_SYSTEMCTL) we support fallbacks to the | |
6038 | * old ways of doing things, hence don't log any error in that case here. */ | |
6039 | ||
6040 | return r < 0 ? r : 0; | |
6041 | } | |
6042 | ||
6043 | static int trivial_method(int argc, char *argv[], void *userdata) { | |
6044 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
6045 | const char *method; | |
6046 | sd_bus *bus; | |
6047 | int r; | |
6048 | ||
1ae17672 ZJS |
6049 | if (arg_dry_run) |
6050 | return 0; | |
6051 | ||
2853b60a LP |
6052 | r = acquire_bus(BUS_MANAGER, &bus); |
6053 | if (r < 0) | |
6054 | return r; | |
6055 | ||
8a4b13c5 | 6056 | polkit_agent_open_maybe(); |
d2ad7e1b | 6057 | |
2853b60a LP |
6058 | method = |
6059 | streq(argv[0], "clear-jobs") || | |
6060 | streq(argv[0], "cancel") ? "ClearJobs" : | |
6061 | streq(argv[0], "reset-failed") ? "ResetFailed" : | |
6062 | streq(argv[0], "halt") ? "Halt" : | |
6063 | streq(argv[0], "reboot") ? "Reboot" : | |
6064 | streq(argv[0], "kexec") ? "KExec" : | |
6065 | streq(argv[0], "exit") ? "Exit" : | |
6066 | /* poweroff */ "PowerOff"; | |
6067 | ||
8010c205 | 6068 | r = bus_call_method(bus, bus_systemd_mgr, method, &error, NULL, NULL); |
2853b60a LP |
6069 | if (r < 0 && arg_action == ACTION_SYSTEMCTL) |
6070 | return log_error_errno(r, "Failed to execute operation: %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. */ | |
7e4249b9 | 6074 | |
0a9776c2 | 6075 | return r < 0 ? r : 0; |
7e4249b9 LP |
6076 | } |
6077 | ||
e449de87 | 6078 | static int reset_failed(int argc, char *argv[], void *userdata) { |
e3e0314b | 6079 | _cleanup_strv_free_ char **names = NULL; |
4fbd7192 | 6080 | sd_bus *bus; |
f84190d8 | 6081 | char **name; |
e3e0314b | 6082 | int r, q; |
5632e374 | 6083 | |
e449de87 | 6084 | if (argc <= 1) |
2853b60a | 6085 | return trivial_method(argc, argv, userdata); |
5632e374 | 6086 | |
4fbd7192 LP |
6087 | r = acquire_bus(BUS_MANAGER, &bus); |
6088 | if (r < 0) | |
6089 | return r; | |
6090 | ||
8a4b13c5 | 6091 | polkit_agent_open_maybe(); |
d2ad7e1b | 6092 | |
1d56bc09 | 6093 | r = expand_names(bus, strv_skip(argv, 1), NULL, &names, NULL); |
e3e0314b | 6094 | if (r < 0) |
691395d8 | 6095 | return log_error_errno(r, "Failed to expand names: %m"); |
f84190d8 | 6096 | |
e3e0314b | 6097 | STRV_FOREACH(name, names) { |
4afd3348 | 6098 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
342641fb | 6099 | |
8010c205 | 6100 | q = bus_call_method(bus, bus_systemd_mgr, "ResetFailedUnit", &error, NULL, "s", *name); |
e3e0314b | 6101 | if (q < 0) { |
691395d8 | 6102 | log_error_errno(q, "Failed to reset failed state of unit %s: %s", *name, bus_error_message(&error, q)); |
e3e0314b ZJS |
6103 | if (r == 0) |
6104 | r = q; | |
f459b602 | 6105 | } |
5632e374 LP |
6106 | } |
6107 | ||
e3e0314b | 6108 | return r; |
5632e374 LP |
6109 | } |
6110 | ||
804ee07c ZJS |
6111 | static int print_variable(const char *s) { |
6112 | const char *sep; | |
6113 | _cleanup_free_ char *esc = NULL; | |
6114 | ||
6115 | sep = strchr(s, '='); | |
baaa35ad ZJS |
6116 | if (!sep) |
6117 | return log_error_errno(SYNTHETIC_ERRNO(EUCLEAN), | |
6118 | "Invalid environment block"); | |
804ee07c ZJS |
6119 | |
6120 | esc = shell_maybe_quote(sep + 1, ESCAPE_POSIX); | |
6121 | if (!esc) | |
6122 | return log_oom(); | |
6123 | ||
6124 | printf("%.*s=%s\n", (int)(sep-s), s, esc); | |
6125 | return 0; | |
6126 | } | |
6127 | ||
e449de87 | 6128 | static int show_environment(int argc, char *argv[], void *userdata) { |
4afd3348 LP |
6129 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
6130 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
f459b602 | 6131 | const char *text; |
4fbd7192 | 6132 | sd_bus *bus; |
7e4249b9 | 6133 | int r; |
7e4249b9 | 6134 | |
4fbd7192 LP |
6135 | r = acquire_bus(BUS_MANAGER, &bus); |
6136 | if (r < 0) | |
6137 | return r; | |
6138 | ||
0221d68a | 6139 | (void) pager_open(arg_pager_flags); |
d2ad7e1b | 6140 | |
8010c205 | 6141 | r = bus_get_property(bus, bus_systemd_mgr, "Environment", &error, &reply, "as"); |
691395d8 LP |
6142 | if (r < 0) |
6143 | return log_error_errno(r, "Failed to get environment: %s", bus_error_message(&error, r)); | |
7e4249b9 | 6144 | |
f459b602 MAP |
6145 | r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "s"); |
6146 | if (r < 0) | |
6147 | return bus_log_parse_error(r); | |
7e4249b9 | 6148 | |
804ee07c ZJS |
6149 | while ((r = sd_bus_message_read_basic(reply, SD_BUS_TYPE_STRING, &text)) > 0) { |
6150 | r = print_variable(text); | |
6151 | if (r < 0) | |
6152 | return r; | |
6153 | } | |
f459b602 MAP |
6154 | if (r < 0) |
6155 | return bus_log_parse_error(r); | |
7e4249b9 | 6156 | |
f459b602 MAP |
6157 | r = sd_bus_message_exit_container(reply); |
6158 | if (r < 0) | |
6159 | return bus_log_parse_error(r); | |
7e4249b9 | 6160 | |
f84190d8 | 6161 | return 0; |
7e4249b9 LP |
6162 | } |
6163 | ||
e449de87 | 6164 | static int switch_root(int argc, char *argv[], void *userdata) { |
4afd3348 | 6165 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
f39d4a08 HH |
6166 | _cleanup_free_ char *cmdline_init = NULL; |
6167 | const char *root, *init; | |
4fbd7192 | 6168 | sd_bus *bus; |
f459b602 | 6169 | int r; |
957eb8ca | 6170 | |
2f50e59f LP |
6171 | if (arg_transport != BUS_TRANSPORT_LOCAL) |
6172 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot switch root remotely."); | |
4fbd7192 | 6173 | |
2f50e59f LP |
6174 | if (argc < 2 || argc > 3) |
6175 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Wrong number of arguments."); | |
957eb8ca | 6176 | |
e449de87 | 6177 | root = argv[1]; |
13068da8 | 6178 | |
e449de87 LP |
6179 | if (argc >= 3) |
6180 | init = argv[2]; | |
13068da8 | 6181 | else { |
01771226 | 6182 | r = proc_cmdline_get_key("init", 0, &cmdline_init); |
f39d4a08 | 6183 | if (r < 0) |
da927ba9 | 6184 | log_debug_errno(r, "Failed to parse /proc/cmdline: %m"); |
13068da8 | 6185 | |
f39d4a08 | 6186 | init = cmdline_init; |
13068da8 | 6187 | } |
f459b602 | 6188 | |
3c6f7c34 | 6189 | init = empty_to_null(init); |
f39d4a08 HH |
6190 | if (init) { |
6191 | const char *root_systemd_path = NULL, *root_init_path = NULL; | |
6192 | ||
270384b2 YW |
6193 | root_systemd_path = prefix_roota(root, "/" SYSTEMD_BINARY_PATH); |
6194 | root_init_path = prefix_roota(root, init); | |
f39d4a08 HH |
6195 | |
6196 | /* If the passed init is actually the same as the | |
6197 | * systemd binary, then let's suppress it. */ | |
e3f791a2 | 6198 | if (files_same(root_init_path, root_systemd_path, 0) > 0) |
f39d4a08 HH |
6199 | init = NULL; |
6200 | } | |
13068da8 | 6201 | |
d86e4c97 LP |
6202 | /* Instruct PID1 to exclude us from its killing spree applied during the transition. Otherwise we |
6203 | * would exit with a failure status even though the switch to the new root has succeed. */ | |
6204 | assert(saved_argv); | |
6205 | assert(saved_argv[0]); | |
6206 | saved_argv[0][0] = '@'; | |
acc28e2e | 6207 | |
4fbd7192 LP |
6208 | r = acquire_bus(BUS_MANAGER, &bus); |
6209 | if (r < 0) | |
6210 | return r; | |
6211 | ||
8a7a9cea ZJS |
6212 | /* If we are slow to exit after the root switch, the new systemd instance |
6213 | * will send us a signal to terminate. Just ignore it and exit normally. | |
6214 | * This way the unit does not end up as failed. | |
6215 | */ | |
6216 | r = ignore_signals(SIGTERM, -1); | |
6217 | if (r < 0) | |
6218 | log_warning_errno(r, "Failed to change disposition of SIGTERM to ignore: %m"); | |
6219 | ||
f39d4a08 | 6220 | log_debug("Switching root - root: %s; init: %s", root, strna(init)); |
957eb8ca | 6221 | |
8010c205 | 6222 | r = bus_call_method(bus, bus_systemd_mgr, "SwitchRoot", &error, NULL, "ss", root, init); |
8a7a9cea ZJS |
6223 | if (r < 0) { |
6224 | (void) default_signals(SIGTERM, -1); | |
6225 | ||
691395d8 | 6226 | return log_error_errno(r, "Failed to switch root: %s", bus_error_message(&error, r)); |
8a7a9cea | 6227 | } |
f459b602 MAP |
6228 | |
6229 | return 0; | |
957eb8ca LP |
6230 | } |
6231 | ||
38fcb7f7 ZJS |
6232 | static int log_level(int argc, char *argv[], void *userdata) { |
6233 | sd_bus *bus; | |
6234 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
6235 | int r; | |
6236 | ||
6237 | r = acquire_bus(BUS_MANAGER, &bus); | |
6238 | if (r < 0) | |
6239 | return r; | |
6240 | ||
6241 | if (argc == 1) { | |
6242 | _cleanup_free_ char *level = NULL; | |
6243 | ||
8010c205 | 6244 | r = bus_get_property_string(bus, bus_systemd_mgr, "LogLevel", &error, &level); |
38fcb7f7 ZJS |
6245 | if (r < 0) |
6246 | return log_error_errno(r, "Failed to get log level: %s", bus_error_message(&error, r)); | |
6247 | ||
6248 | puts(level); | |
6249 | ||
6250 | } else { | |
6251 | assert(argc == 2); | |
6252 | ||
8010c205 | 6253 | r = bus_set_property(bus, bus_systemd_mgr, "LogLevel", &error, "s", argv[1]); |
38fcb7f7 ZJS |
6254 | if (r < 0) |
6255 | return log_error_errno(r, "Failed to set log level: %s", bus_error_message(&error, r)); | |
6256 | } | |
6257 | ||
6258 | return 0; | |
6259 | } | |
6260 | ||
6261 | static int log_target(int argc, char *argv[], void *userdata) { | |
6262 | sd_bus *bus; | |
6263 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
6264 | int r; | |
6265 | ||
6266 | r = acquire_bus(BUS_MANAGER, &bus); | |
6267 | if (r < 0) | |
6268 | return r; | |
6269 | ||
6270 | if (argc == 1) { | |
6271 | _cleanup_free_ char *target = NULL; | |
6272 | ||
8010c205 | 6273 | r = bus_get_property_string(bus, bus_systemd_mgr, "LogTarget", &error, &target); |
38fcb7f7 ZJS |
6274 | if (r < 0) |
6275 | return log_error_errno(r, "Failed to get log target: %s", bus_error_message(&error, r)); | |
6276 | ||
6277 | puts(target); | |
6278 | ||
6279 | } else { | |
6280 | assert(argc == 2); | |
6281 | ||
8010c205 | 6282 | r = bus_set_property(bus, bus_systemd_mgr, "LogTarget", &error, "s", argv[1]); |
38fcb7f7 ZJS |
6283 | if (r < 0) |
6284 | return log_error_errno(r, "Failed to set log target: %s", bus_error_message(&error, r)); | |
6285 | } | |
6286 | ||
6287 | return 0; | |
6288 | } | |
6289 | ||
6ab86319 ZJS |
6290 | static int service_watchdogs(int argc, char *argv[], void *userdata) { |
6291 | sd_bus *bus; | |
6292 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
6293 | int b, r; | |
6294 | ||
6295 | assert(argv); | |
6296 | ||
6297 | r = acquire_bus(BUS_MANAGER, &bus); | |
6298 | if (r < 0) | |
6299 | return r; | |
6300 | ||
6301 | if (argc == 1) { | |
6302 | /* get ServiceWatchdogs */ | |
8010c205 | 6303 | r = bus_get_property_trivial(bus, bus_systemd_mgr, "ServiceWatchdogs", &error, 'b', &b); |
6ab86319 ZJS |
6304 | if (r < 0) |
6305 | return log_error_errno(r, "Failed to get service-watchdog state: %s", bus_error_message(&error, r)); | |
6306 | ||
6307 | printf("%s\n", yes_no(!!b)); | |
6308 | ||
6309 | } else { | |
6310 | /* set ServiceWatchdogs */ | |
6311 | assert(argc == 2); | |
6312 | ||
6313 | b = parse_boolean(argv[1]); | |
6314 | if (b < 0) | |
6315 | return log_error_errno(b, "Failed to parse service-watchdogs argument: %m"); | |
6316 | ||
8010c205 | 6317 | r = bus_set_property(bus, bus_systemd_mgr, "ServiceWatchdogs", &error, "b", b); |
6ab86319 ZJS |
6318 | if (r < 0) |
6319 | return log_error_errno(r, "Failed to set service-watchdog state: %s", bus_error_message(&error, r)); | |
6320 | } | |
6321 | ||
6322 | return 0; | |
6323 | } | |
6324 | ||
e449de87 | 6325 | static int set_environment(int argc, char *argv[], void *userdata) { |
4afd3348 LP |
6326 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
6327 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; | |
7e4249b9 | 6328 | const char *method; |
4fbd7192 | 6329 | sd_bus *bus; |
31e767f7 LP |
6330 | int r; |
6331 | ||
e449de87 LP |
6332 | assert(argc > 1); |
6333 | assert(argv); | |
7e4249b9 | 6334 | |
4fbd7192 LP |
6335 | r = acquire_bus(BUS_MANAGER, &bus); |
6336 | if (r < 0) | |
6337 | return r; | |
6338 | ||
8a4b13c5 | 6339 | polkit_agent_open_maybe(); |
d2ad7e1b | 6340 | |
e449de87 | 6341 | method = streq(argv[0], "set-environment") |
7e4249b9 LP |
6342 | ? "SetEnvironment" |
6343 | : "UnsetEnvironment"; | |
6344 | ||
8010c205 | 6345 | r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, method); |
f459b602 MAP |
6346 | if (r < 0) |
6347 | return bus_log_create_error(r); | |
7e4249b9 | 6348 | |
e449de87 | 6349 | r = sd_bus_message_append_strv(m, strv_skip(argv, 1)); |
31e767f7 | 6350 | if (r < 0) |
f459b602 | 6351 | return bus_log_create_error(r); |
7e4249b9 | 6352 | |
c49b30a2 | 6353 | r = sd_bus_call(bus, m, 0, &error, NULL); |
691395d8 LP |
6354 | if (r < 0) |
6355 | return log_error_errno(r, "Failed to set environment: %s", bus_error_message(&error, r)); | |
7e4249b9 | 6356 | |
f84190d8 | 6357 | return 0; |
7e4249b9 LP |
6358 | } |
6359 | ||
e449de87 | 6360 | static int import_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; | |
4fbd7192 | 6363 | sd_bus *bus; |
ac3efa8a LP |
6364 | int r; |
6365 | ||
4fbd7192 LP |
6366 | r = acquire_bus(BUS_MANAGER, &bus); |
6367 | if (r < 0) | |
6368 | return r; | |
6369 | ||
8a4b13c5 | 6370 | polkit_agent_open_maybe(); |
d2ad7e1b | 6371 | |
8010c205 | 6372 | r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "SetEnvironment"); |
ac3efa8a LP |
6373 | if (r < 0) |
6374 | return bus_log_create_error(r); | |
6375 | ||
e449de87 | 6376 | if (argc < 2) |
ac3efa8a LP |
6377 | r = sd_bus_message_append_strv(m, environ); |
6378 | else { | |
6379 | char **a, **b; | |
6380 | ||
6381 | r = sd_bus_message_open_container(m, 'a', "s"); | |
6382 | if (r < 0) | |
6383 | return bus_log_create_error(r); | |
6384 | ||
e449de87 | 6385 | STRV_FOREACH(a, strv_skip(argv, 1)) { |
ac3efa8a | 6386 | |
2f50e59f LP |
6387 | if (!env_name_is_valid(*a)) |
6388 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Not a valid environment variable name: %s", *a); | |
ac3efa8a LP |
6389 | |
6390 | STRV_FOREACH(b, environ) { | |
6391 | const char *eq; | |
6392 | ||
6393 | eq = startswith(*b, *a); | |
6394 | if (eq && *eq == '=') { | |
6395 | ||
6396 | r = sd_bus_message_append(m, "s", *b); | |
6397 | if (r < 0) | |
6398 | return bus_log_create_error(r); | |
6399 | ||
6400 | break; | |
6401 | } | |
6402 | } | |
6403 | } | |
6404 | ||
6405 | r = sd_bus_message_close_container(m); | |
6406 | } | |
6407 | if (r < 0) | |
6408 | return bus_log_create_error(r); | |
6409 | ||
6410 | r = sd_bus_call(bus, m, 0, &error, NULL); | |
691395d8 LP |
6411 | if (r < 0) |
6412 | return log_error_errno(r, "Failed to import environment: %s", bus_error_message(&error, r)); | |
ac3efa8a LP |
6413 | |
6414 | return 0; | |
6415 | } | |
6416 | ||
cbb13b2a | 6417 | static int enable_sysv_units(const char *verb, char **args) { |
729e3769 | 6418 | int r = 0; |
ee5762e3 | 6419 | |
349cc4a5 | 6420 | #if HAVE_SYSV_COMPAT |
8e766630 | 6421 | _cleanup_(lookup_paths_free) LookupPaths paths = {}; |
e735decc LP |
6422 | unsigned f = 0; |
6423 | ||
6424 | /* Processes all SysV units, and reshuffles the array so that afterwards only the native units remain */ | |
ee5762e3 | 6425 | |
729e3769 LP |
6426 | if (arg_scope != UNIT_FILE_SYSTEM) |
6427 | return 0; | |
ee5762e3 | 6428 | |
b41b9d2a LP |
6429 | if (getenv_bool("SYSTEMCTL_SKIP_SYSV") > 0) |
6430 | return 0; | |
6431 | ||
4c315c2c IS |
6432 | if (!STR_IN_SET(verb, |
6433 | "enable", | |
6434 | "disable", | |
6435 | "is-enabled")) | |
729e3769 | 6436 | return 0; |
ee5762e3 | 6437 | |
4943d143 | 6438 | r = lookup_paths_init(&paths, arg_scope, LOOKUP_PATHS_EXCLUDE_GENERATED, arg_root); |
729e3769 LP |
6439 | if (r < 0) |
6440 | return r; | |
ee5762e3 | 6441 | |
729e3769 | 6442 | r = 0; |
a644abed | 6443 | while (args[f]) { |
e735decc LP |
6444 | |
6445 | const char *argv[] = { | |
6446 | ROOTLIBEXECDIR "/systemd-sysv-install", | |
3483460c LP |
6447 | NULL, /* --root= */ |
6448 | NULL, /* verb */ | |
6449 | NULL, /* service */ | |
e735decc LP |
6450 | NULL, |
6451 | }; | |
6452 | ||
8d1ee648 | 6453 | _cleanup_free_ char *p = NULL, *q = NULL, *l = NULL, *v = NULL; |
729e3769 | 6454 | bool found_native = false, found_sysv; |
e735decc | 6455 | const char *name; |
729e3769 | 6456 | unsigned c = 1; |
729e3769 | 6457 | pid_t pid; |
e735decc | 6458 | int j; |
ee5762e3 | 6459 | |
a644abed | 6460 | name = args[f++]; |
ee5762e3 | 6461 | |
729e3769 LP |
6462 | if (!endswith(name, ".service")) |
6463 | continue; | |
ee5762e3 | 6464 | |
729e3769 LP |
6465 | if (path_is_absolute(name)) |
6466 | continue; | |
ee5762e3 | 6467 | |
e735decc | 6468 | j = unit_file_exists(arg_scope, &paths, name); |
76ec966f | 6469 | if (j < 0 && !IN_SET(j, -ELOOP, -ERFKILL, -EADDRNOTAVAIL)) |
105a1a36 | 6470 | return log_error_errno(j, "Failed to look up unit file state: %m"); |
3c6d8e57 | 6471 | found_native = j != 0; |
ee5762e3 | 6472 | |
e735decc LP |
6473 | /* If we have both a native unit and a SysV script, enable/disable them both (below); for is-enabled, |
6474 | * prefer the native unit */ | |
355ff449 | 6475 | if (found_native && streq(verb, "is-enabled")) |
729e3769 | 6476 | continue; |
ee5762e3 | 6477 | |
652ef298 | 6478 | p = path_join(arg_root, SYSTEM_SYSVINIT_PATH, name); |
0c6ea3a4 | 6479 | if (!p) |
60731f32 | 6480 | return log_oom(); |
ee5762e3 | 6481 | |
fbd0b64f | 6482 | p[strlen(p) - STRLEN(".service")] = 0; |
729e3769 | 6483 | found_sysv = access(p, F_OK) >= 0; |
4b6756a8 | 6484 | if (!found_sysv) |
729e3769 | 6485 | continue; |
71fad675 | 6486 | |
689e4e6a CR |
6487 | if (!arg_quiet) { |
6488 | if (found_native) | |
6489 | log_info("Synchronizing state of %s with SysV service script with %s.", name, argv[0]); | |
6490 | else | |
6491 | log_info("%s is not a native service, redirecting to systemd-sysv-install.", name); | |
6492 | } | |
ee5762e3 | 6493 | |
b1dffbb9 | 6494 | if (!isempty(arg_root)) { |
b910cc72 | 6495 | q = strjoin("--root=", arg_root); |
b1dffbb9 LP |
6496 | if (!q) |
6497 | return log_oom(); | |
6498 | ||
6499 | argv[c++] = q; | |
6500 | } | |
ee5762e3 | 6501 | |
8d1ee648 LP |
6502 | /* Let's copy the verb, since it's still pointing directly into the original argv[] array we |
6503 | * got passed, but safe_fork() is likely going to rewrite that for the new child */ | |
6504 | v = strdup(verb); | |
6505 | if (!v) | |
6506 | return log_oom(); | |
6507 | ||
6508 | argv[c++] = v; | |
2b6bf07d | 6509 | argv[c++] = basename(p); |
729e3769 | 6510 | argv[c] = NULL; |
ee5762e3 | 6511 | |
729e3769 | 6512 | l = strv_join((char**)argv, " "); |
60731f32 ZJS |
6513 | if (!l) |
6514 | return log_oom(); | |
ee5762e3 | 6515 | |
8ecc68f4 LP |
6516 | if (!arg_quiet) |
6517 | log_info("Executing: %s", l); | |
ee5762e3 | 6518 | |
0672e2c6 | 6519 | j = safe_fork("(sysv-install)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG, &pid); |
4c253ed1 | 6520 | if (j < 0) |
b6e1fff1 | 6521 | return j; |
4c253ed1 | 6522 | if (j == 0) { |
729e3769 | 6523 | /* Child */ |
729e3769 | 6524 | execv(argv[0], (char**) argv); |
cc7cb0ca | 6525 | log_error_errno(errno, "Failed to execute %s: %m", argv[0]); |
729e3769 LP |
6526 | _exit(EXIT_FAILURE); |
6527 | } | |
ee5762e3 | 6528 | |
2e87a1fd | 6529 | j = wait_for_terminate_and_check("sysv-install", pid, WAIT_LOG_ABNORMAL); |
d710aaf7 | 6530 | if (j < 0) |
2e87a1fd LP |
6531 | return j; |
6532 | if (streq(verb, "is-enabled")) { | |
b4a34311 | 6533 | if (j == EXIT_SUCCESS) { |
2e87a1fd LP |
6534 | if (!arg_quiet) |
6535 | puts("enabled"); | |
6536 | r = 1; | |
6537 | } else { | |
6538 | if (!arg_quiet) | |
6539 | puts("disabled"); | |
6540 | } | |
ee5762e3 | 6541 | |
b4a34311 | 6542 | } else if (j != EXIT_SUCCESS) |
2e87a1fd | 6543 | return -EBADE; /* We don't warn here, under the assumption the script already showed an explanation */ |
ee5762e3 | 6544 | |
355ff449 MP |
6545 | if (found_native) |
6546 | continue; | |
6547 | ||
a644abed | 6548 | /* Remove this entry, so that we don't try enabling it as native unit */ |
aba84331 LP |
6549 | assert(f > 0); |
6550 | f--; | |
6551 | assert(args[f] == name); | |
290cb8e8 | 6552 | strv_remove(args + f, name); |
729e3769 | 6553 | } |
ee5762e3 | 6554 | |
729e3769 LP |
6555 | #endif |
6556 | return r; | |
6557 | } | |
ee5762e3 | 6558 | |
df7c4eb6 | 6559 | static int mangle_names(const char *operation, char **original_names, char ***mangled_names) { |
a33fdebb | 6560 | char **i, **l, **name; |
7410616c | 6561 | int r; |
37370d0c | 6562 | |
7410616c | 6563 | l = i = new(char*, strv_length(original_names) + 1); |
a33fdebb | 6564 | if (!l) |
37370d0c VP |
6565 | return log_oom(); |
6566 | ||
37370d0c | 6567 | STRV_FOREACH(name, original_names) { |
44386fc1 LN |
6568 | |
6569 | /* When enabling units qualified path names are OK, | |
6570 | * too, hence allow them explicitly. */ | |
6571 | ||
7410616c | 6572 | if (is_path(*name)) { |
44386fc1 | 6573 | *i = strdup(*name); |
7410616c LP |
6574 | if (!*i) { |
6575 | strv_free(l); | |
6576 | return log_oom(); | |
6577 | } | |
6578 | } else { | |
df7c4eb6 ZJS |
6579 | r = unit_name_mangle_with_suffix(*name, operation, |
6580 | arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN, | |
6581 | ".service", i); | |
7410616c | 6582 | if (r < 0) { |
523f8cde | 6583 | *i = NULL; |
7410616c LP |
6584 | strv_free(l); |
6585 | return log_error_errno(r, "Failed to mangle unit name: %m"); | |
6586 | } | |
a33fdebb LP |
6587 | } |
6588 | ||
6589 | i++; | |
37370d0c | 6590 | } |
a33fdebb LP |
6591 | |
6592 | *i = NULL; | |
6593 | *mangled_names = l; | |
37370d0c VP |
6594 | |
6595 | return 0; | |
6596 | } | |
6597 | ||
decd7982 JR |
6598 | static int normalize_filenames(char **names) { |
6599 | char **u; | |
6600 | int r; | |
6601 | ||
6602 | STRV_FOREACH(u, names) | |
6603 | if (!path_is_absolute(*u)) { | |
6604 | char* normalized_path; | |
6605 | ||
baaa35ad ZJS |
6606 | if (!isempty(arg_root)) |
6607 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
6608 | "Non-absolute paths are not allowed when --root is used: %s", | |
6609 | *u); | |
decd7982 | 6610 | |
baaa35ad ZJS |
6611 | if (!strchr(*u,'/')) |
6612 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
6613 | "Link argument does contain at least one directory separator: %s", | |
6614 | *u); | |
decd7982 JR |
6615 | |
6616 | r = path_make_absolute_cwd(*u, &normalized_path); | |
6617 | if (r < 0) | |
6618 | return r; | |
6619 | ||
6620 | free_and_replace(*u, normalized_path); | |
6621 | } | |
6622 | ||
6623 | return 0; | |
6624 | } | |
6625 | ||
1d3c86c0 MS |
6626 | static int normalize_names(char **names, bool warn_if_path) { |
6627 | char **u; | |
6628 | bool was_path = false; | |
6629 | ||
6630 | STRV_FOREACH(u, names) { | |
6631 | int r; | |
6632 | ||
6633 | if (!is_path(*u)) | |
6634 | continue; | |
6635 | ||
6636 | r = free_and_strdup(u, basename(*u)); | |
6637 | if (r < 0) | |
6638 | return log_error_errno(r, "Failed to normalize unit file path: %m"); | |
6639 | ||
6640 | was_path = true; | |
6641 | } | |
6642 | ||
6643 | if (warn_if_path && was_path) | |
6644 | log_warning("Warning: Can't execute disable on the unit file path. Proceeding with the unit name."); | |
6645 | ||
6646 | return 0; | |
6647 | } | |
6648 | ||
173471b7 | 6649 | static int unit_exists(LookupPaths *lp, const char *unit) { |
d6568222 | 6650 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
f37f8a61 | 6651 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
d6568222 SS |
6652 | _cleanup_free_ char *path = NULL; |
6653 | static const struct bus_properties_map property_map[] = { | |
f37f8a61 YW |
6654 | { "LoadState", "s", NULL, offsetof(UnitStatusInfo, load_state) }, |
6655 | { "ActiveState", "s", NULL, offsetof(UnitStatusInfo, active_state)}, | |
d6568222 SS |
6656 | {}, |
6657 | }; | |
6658 | UnitStatusInfo info = {}; | |
6659 | sd_bus *bus; | |
6660 | int r; | |
6661 | ||
173471b7 ZJS |
6662 | if (unit_name_is_valid(unit, UNIT_NAME_TEMPLATE)) |
6663 | return unit_find_template_path(unit, lp, NULL, NULL); | |
6664 | ||
d6568222 SS |
6665 | path = unit_dbus_path_from_name(unit); |
6666 | if (!path) | |
6667 | return log_oom(); | |
6668 | ||
6669 | r = acquire_bus(BUS_MANAGER, &bus); | |
6670 | if (r < 0) | |
6671 | return r; | |
6672 | ||
a7e4861c | 6673 | r = bus_map_all_properties(bus, "org.freedesktop.systemd1", path, property_map, 0, &error, &m, &info); |
d6568222 SS |
6674 | if (r < 0) |
6675 | return log_error_errno(r, "Failed to get properties: %s", bus_error_message(&error, r)); | |
6676 | ||
d6568222 SS |
6677 | return !streq_ptr(info.load_state, "not-found") || !streq_ptr(info.active_state, "inactive"); |
6678 | } | |
6679 | ||
e449de87 | 6680 | static int enable_unit(int argc, char *argv[], void *userdata) { |
e3e0314b | 6681 | _cleanup_strv_free_ char **names = NULL; |
e449de87 | 6682 | const char *verb = argv[0]; |
729e3769 | 6683 | UnitFileChange *changes = NULL; |
da6053d0 | 6684 | size_t n_changes = 0; |
729e3769 | 6685 | int carries_install_info = -1; |
35b132e8 | 6686 | bool ignore_carries_install_info = arg_quiet; |
729e3769 | 6687 | int r; |
ee5762e3 | 6688 | |
e449de87 | 6689 | if (!argv[1]) |
ab5919fa MS |
6690 | return 0; |
6691 | ||
df7c4eb6 | 6692 | r = mangle_names("to enable", strv_skip(argv, 1), &names); |
3a05c0f9 | 6693 | if (r < 0) |
cbb13b2a VP |
6694 | return r; |
6695 | ||
e3e0314b | 6696 | r = enable_sysv_units(verb, names); |
cbb13b2a VP |
6697 | if (r < 0) |
6698 | return r; | |
3a05c0f9 | 6699 | |
c61b443d | 6700 | /* If the operation was fully executed by the SysV compat, let's finish early */ |
823e5fab FB |
6701 | if (strv_isempty(names)) { |
6702 | if (arg_no_reload || install_client_side()) | |
6703 | return 0; | |
6704 | return daemon_reload(argc, argv, userdata); | |
6705 | } | |
67d66210 | 6706 | |
1d3c86c0 MS |
6707 | if (streq(verb, "disable")) { |
6708 | r = normalize_names(names, true); | |
6709 | if (r < 0) | |
6710 | return r; | |
6711 | } | |
6712 | ||
decd7982 JR |
6713 | if (streq(verb, "link")) { |
6714 | r = normalize_filenames(names); | |
6715 | if (r < 0) | |
6716 | return r; | |
6717 | } | |
6718 | ||
4fbd7192 | 6719 | if (install_client_side()) { |
b3796dd8 JS |
6720 | UnitFileFlags flags; |
6721 | ||
6722 | flags = args_to_flags(); | |
729e3769 | 6723 | if (streq(verb, "enable")) { |
b3796dd8 | 6724 | r = unit_file_enable(arg_scope, flags, arg_root, names, &changes, &n_changes); |
729e3769 LP |
6725 | carries_install_info = r; |
6726 | } else if (streq(verb, "disable")) | |
b3796dd8 | 6727 | r = unit_file_disable(arg_scope, flags, arg_root, names, &changes, &n_changes); |
729e3769 | 6728 | else if (streq(verb, "reenable")) { |
b3796dd8 | 6729 | r = unit_file_reenable(arg_scope, flags, arg_root, names, &changes, &n_changes); |
729e3769 LP |
6730 | carries_install_info = r; |
6731 | } else if (streq(verb, "link")) | |
b3796dd8 | 6732 | r = unit_file_link(arg_scope, flags, arg_root, names, &changes, &n_changes); |
729e3769 | 6733 | else if (streq(verb, "preset")) { |
b3796dd8 | 6734 | r = unit_file_preset(arg_scope, flags, arg_root, names, arg_preset_mode, &changes, &n_changes); |
729e3769 | 6735 | } else if (streq(verb, "mask")) |
b3796dd8 | 6736 | r = unit_file_mask(arg_scope, flags, arg_root, names, &changes, &n_changes); |
729e3769 | 6737 | else if (streq(verb, "unmask")) |
b3796dd8 | 6738 | r = unit_file_unmask(arg_scope, flags, arg_root, names, &changes, &n_changes); |
344ca755 LP |
6739 | else if (streq(verb, "revert")) |
6740 | r = unit_file_revert(arg_scope, arg_root, names, &changes, &n_changes); | |
729e3769 LP |
6741 | else |
6742 | assert_not_reached("Unknown verb"); | |
ee5762e3 | 6743 | |
af3d8113 | 6744 | unit_file_dump_changes(r, verb, changes, n_changes, arg_quiet); |
d073dea0 | 6745 | if (r < 0) |
85b78539 | 6746 | goto finish; |
df77cdf0 | 6747 | r = 0; |
729e3769 | 6748 | } else { |
4afd3348 LP |
6749 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL, *m = NULL; |
6750 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
39207373 | 6751 | bool expect_carries_install_info = false; |
344ca755 | 6752 | bool send_runtime = true, send_force = true, send_preset_mode = false; |
718db961 | 6753 | const char *method; |
4fbd7192 | 6754 | sd_bus *bus; |
729e3769 | 6755 | |
d6568222 | 6756 | if (STR_IN_SET(verb, "mask", "unmask")) { |
f8d6cb48 | 6757 | char **name; |
8e766630 | 6758 | _cleanup_(lookup_paths_free) LookupPaths lp = {}; |
173471b7 ZJS |
6759 | |
6760 | r = lookup_paths_init(&lp, arg_scope, 0, arg_root); | |
d6568222 SS |
6761 | if (r < 0) |
6762 | return r; | |
f8d6cb48 ZJS |
6763 | |
6764 | STRV_FOREACH(name, names) { | |
173471b7 | 6765 | r = unit_exists(&lp, *name); |
f8d6cb48 ZJS |
6766 | if (r < 0) |
6767 | return r; | |
6768 | if (r == 0) | |
25c59b5d | 6769 | log_notice("Unit %s does not exist, proceeding anyway.", *name); |
f8d6cb48 | 6770 | } |
d6568222 SS |
6771 | } |
6772 | ||
4fbd7192 LP |
6773 | r = acquire_bus(BUS_MANAGER, &bus); |
6774 | if (r < 0) | |
6775 | return r; | |
6776 | ||
8a4b13c5 | 6777 | polkit_agent_open_maybe(); |
d2ad7e1b | 6778 | |
729e3769 LP |
6779 | if (streq(verb, "enable")) { |
6780 | method = "EnableUnitFiles"; | |
6781 | expect_carries_install_info = true; | |
6782 | } else if (streq(verb, "disable")) { | |
6783 | method = "DisableUnitFiles"; | |
6784 | send_force = false; | |
6785 | } else if (streq(verb, "reenable")) { | |
6786 | method = "ReenableUnitFiles"; | |
6787 | expect_carries_install_info = true; | |
6788 | } else if (streq(verb, "link")) | |
6789 | method = "LinkUnitFiles"; | |
6790 | else if (streq(verb, "preset")) { | |
d309c1c3 LP |
6791 | |
6792 | if (arg_preset_mode != UNIT_FILE_PRESET_FULL) { | |
6793 | method = "PresetUnitFilesWithMode"; | |
6794 | send_preset_mode = true; | |
6795 | } else | |
6796 | method = "PresetUnitFiles"; | |
6797 | ||
729e3769 | 6798 | expect_carries_install_info = true; |
39207373 | 6799 | ignore_carries_install_info = true; |
729e3769 LP |
6800 | } else if (streq(verb, "mask")) |
6801 | method = "MaskUnitFiles"; | |
6802 | else if (streq(verb, "unmask")) { | |
6803 | method = "UnmaskUnitFiles"; | |
6804 | send_force = false; | |
344ca755 LP |
6805 | } else if (streq(verb, "revert")) { |
6806 | method = "RevertUnitFiles"; | |
6807 | send_runtime = send_force = false; | |
729e3769 LP |
6808 | } else |
6809 | assert_not_reached("Unknown verb"); | |
6810 | ||
8010c205 | 6811 | r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, method); |
f459b602 MAP |
6812 | if (r < 0) |
6813 | return bus_log_create_error(r); | |
ee5762e3 | 6814 | |
e3e0314b | 6815 | r = sd_bus_message_append_strv(m, names); |
f459b602 MAP |
6816 | if (r < 0) |
6817 | return bus_log_create_error(r); | |
ee5762e3 | 6818 | |
d309c1c3 LP |
6819 | if (send_preset_mode) { |
6820 | r = sd_bus_message_append(m, "s", unit_file_preset_mode_to_string(arg_preset_mode)); | |
6821 | if (r < 0) | |
6822 | return bus_log_create_error(r); | |
6823 | } | |
6824 | ||
344ca755 LP |
6825 | if (send_runtime) { |
6826 | r = sd_bus_message_append(m, "b", arg_runtime); | |
6827 | if (r < 0) | |
6828 | return bus_log_create_error(r); | |
6829 | } | |
ee5762e3 | 6830 | |
729e3769 | 6831 | if (send_force) { |
f459b602 MAP |
6832 | r = sd_bus_message_append(m, "b", arg_force); |
6833 | if (r < 0) | |
6834 | return bus_log_create_error(r); | |
ee5762e3 LP |
6835 | } |
6836 | ||
c49b30a2 | 6837 | r = sd_bus_call(bus, m, 0, &error, &reply); |
691395d8 | 6838 | if (r < 0) |
af3d8113 | 6839 | return log_error_errno(r, "Failed to %s unit: %s", verb, bus_error_message(&error, r)); |
be394c48 | 6840 | |
729e3769 | 6841 | if (expect_carries_install_info) { |
f459b602 MAP |
6842 | r = sd_bus_message_read(reply, "b", &carries_install_info); |
6843 | if (r < 0) | |
6844 | return bus_log_parse_error(r); | |
ee5762e3 LP |
6845 | } |
6846 | ||
57ab2eab | 6847 | r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet, &changes, &n_changes); |
f459b602 | 6848 | if (r < 0) |
85b78539 | 6849 | goto finish; |
b77398f7 | 6850 | |
93c941e3 | 6851 | /* Try to reload if enabled */ |
d6cb60c7 | 6852 | if (!arg_no_reload) |
e449de87 | 6853 | r = daemon_reload(argc, argv, userdata); |
f459b602 MAP |
6854 | else |
6855 | r = 0; | |
b647f10d | 6856 | } |
3d3961f2 | 6857 | |
39207373 | 6858 | if (carries_install_info == 0 && !ignore_carries_install_info) |
46efc978 LP |
6859 | log_notice("The unit files have no installation config (WantedBy=, RequiredBy=, Also=,\n" |
6860 | "Alias= settings in the [Install] section, and DefaultInstance= for template\n" | |
2811184a ZJS |
6861 | "units). This means they are not meant to be enabled using systemctl.\n" |
6862 | " \n" /* trick: the space is needed so that the line does not get stripped from output */ | |
46efc978 | 6863 | "Possible reasons for having this kind of units are:\n" |
18c93ee3 LP |
6864 | "%1$s A unit may be statically enabled by being symlinked from another unit's\n" |
6865 | " .wants/ or .requires/ directory.\n" | |
6866 | "%1$s A unit's purpose may be to act as a helper for some other unit which has\n" | |
6867 | " a requirement dependency on it.\n" | |
6868 | "%1$s A unit may be started when needed via activation (socket, path, timer,\n" | |
6869 | " D-Bus, udev, scripted systemctl call, ...).\n" | |
6870 | "%1$s In case of template units, the unit is meant to be enabled with some\n" | |
6871 | " instance name specified.", | |
9a6f746f | 6872 | special_glyph(SPECIAL_GLYPH_BULLET)); |
ee5762e3 | 6873 | |
6bc30691 | 6874 | if (arg_now && STR_IN_SET(argv[0], "enable", "disable", "mask")) { |
4fbd7192 | 6875 | sd_bus *bus; |
da6053d0 | 6876 | size_t len, i; |
57ab2eab | 6877 | |
4fbd7192 LP |
6878 | r = acquire_bus(BUS_MANAGER, &bus); |
6879 | if (r < 0) | |
d073dea0 | 6880 | goto finish; |
4fbd7192 | 6881 | |
6bc30691 JS |
6882 | len = strv_length(names); |
6883 | { | |
6884 | char *new_args[len + 2]; | |
57ab2eab | 6885 | |
6bc30691 JS |
6886 | new_args[0] = (char*) (streq(argv[0], "enable") ? "start" : "stop"); |
6887 | for (i = 0; i < len; i++) | |
6888 | new_args[i + 1] = basename(names[i]); | |
6889 | new_args[i + 1] = NULL; | |
6890 | ||
6891 | r = start_unit(len + 1, new_args, userdata); | |
6892 | } | |
57ab2eab JS |
6893 | } |
6894 | ||
729e3769 | 6895 | finish: |
729e3769 | 6896 | unit_file_changes_free(changes, n_changes); |
ee5762e3 | 6897 | |
729e3769 | 6898 | return r; |
ee5762e3 LP |
6899 | } |
6900 | ||
e449de87 | 6901 | static int add_dependency(int argc, char *argv[], void *userdata) { |
e94937df LN |
6902 | _cleanup_strv_free_ char **names = NULL; |
6903 | _cleanup_free_ char *target = NULL; | |
e449de87 | 6904 | const char *verb = argv[0]; |
acc0269c | 6905 | UnitFileChange *changes = NULL; |
da6053d0 | 6906 | size_t n_changes = 0; |
e94937df LN |
6907 | UnitDependency dep; |
6908 | int r = 0; | |
6909 | ||
e449de87 | 6910 | if (!argv[1]) |
e94937df LN |
6911 | return 0; |
6912 | ||
df7c4eb6 ZJS |
6913 | r = unit_name_mangle_with_suffix(argv[1], "as target", |
6914 | arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN, | |
6915 | ".target", &target); | |
7410616c LP |
6916 | if (r < 0) |
6917 | return log_error_errno(r, "Failed to mangle unit name: %m"); | |
e94937df | 6918 | |
df7c4eb6 | 6919 | r = mangle_names("as dependency", strv_skip(argv, 2), &names); |
e94937df LN |
6920 | if (r < 0) |
6921 | return r; | |
6922 | ||
6923 | if (streq(verb, "add-wants")) | |
6924 | dep = UNIT_WANTS; | |
6925 | else if (streq(verb, "add-requires")) | |
6926 | dep = UNIT_REQUIRES; | |
6927 | else | |
6928 | assert_not_reached("Unknown verb"); | |
6929 | ||
4fbd7192 | 6930 | if (install_client_side()) { |
b3796dd8 | 6931 | r = unit_file_add_dependency(arg_scope, args_to_flags(), arg_root, names, target, dep, &changes, &n_changes); |
af3d8113 | 6932 | unit_file_dump_changes(r, "add dependency on", changes, n_changes, arg_quiet); |
5f056378 CH |
6933 | |
6934 | if (r > 0) | |
6935 | r = 0; | |
e94937df | 6936 | } else { |
4afd3348 LP |
6937 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL, *m = NULL; |
6938 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
4fbd7192 | 6939 | sd_bus *bus; |
e94937df | 6940 | |
4fbd7192 LP |
6941 | r = acquire_bus(BUS_MANAGER, &bus); |
6942 | if (r < 0) | |
6943 | return r; | |
6944 | ||
8a4b13c5 | 6945 | polkit_agent_open_maybe(); |
d2ad7e1b | 6946 | |
8010c205 | 6947 | r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "AddDependencyUnitFiles"); |
e94937df LN |
6948 | if (r < 0) |
6949 | return bus_log_create_error(r); | |
6950 | ||
342641fb | 6951 | r = sd_bus_message_append_strv(m, names); |
e94937df LN |
6952 | if (r < 0) |
6953 | return bus_log_create_error(r); | |
6954 | ||
342641fb | 6955 | r = sd_bus_message_append(m, "ssbb", target, unit_dependency_to_string(dep), arg_runtime, arg_force); |
e94937df LN |
6956 | if (r < 0) |
6957 | return bus_log_create_error(r); | |
6958 | ||
6959 | r = sd_bus_call(bus, m, 0, &error, &reply); | |
691395d8 | 6960 | if (r < 0) |
af3d8113 | 6961 | return log_error_errno(r, "Failed to add dependency: %s", bus_error_message(&error, r)); |
e94937df | 6962 | |
acc0269c | 6963 | r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet, &changes, &n_changes); |
e94937df | 6964 | if (r < 0) |
acc0269c | 6965 | goto finish; |
e94937df | 6966 | |
acc0269c CH |
6967 | if (arg_no_reload) { |
6968 | r = 0; | |
6969 | goto finish; | |
6970 | } | |
6971 | ||
6972 | r = daemon_reload(argc, argv, userdata); | |
e94937df | 6973 | } |
acc0269c CH |
6974 | |
6975 | finish: | |
6976 | unit_file_changes_free(changes, n_changes); | |
6977 | ||
6978 | return r; | |
e94937df LN |
6979 | } |
6980 | ||
e449de87 | 6981 | static int preset_all(int argc, char *argv[], void *userdata) { |
acc0269c | 6982 | UnitFileChange *changes = NULL; |
da6053d0 | 6983 | size_t n_changes = 0; |
d309c1c3 LP |
6984 | int r; |
6985 | ||
4fbd7192 | 6986 | if (install_client_side()) { |
b3796dd8 | 6987 | r = unit_file_preset_all(arg_scope, args_to_flags(), arg_root, arg_preset_mode, &changes, &n_changes); |
af3d8113 | 6988 | unit_file_dump_changes(r, "preset", changes, n_changes, arg_quiet); |
5f056378 CH |
6989 | |
6990 | if (r > 0) | |
6991 | r = 0; | |
d309c1c3 | 6992 | } else { |
4afd3348 LP |
6993 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
6994 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
4fbd7192 | 6995 | sd_bus *bus; |
d309c1c3 | 6996 | |
4fbd7192 LP |
6997 | r = acquire_bus(BUS_MANAGER, &bus); |
6998 | if (r < 0) | |
6999 | return r; | |
7000 | ||
8a4b13c5 | 7001 | polkit_agent_open_maybe(); |
d2ad7e1b | 7002 | |
8010c205 | 7003 | r = bus_call_method( |
d309c1c3 | 7004 | bus, |
8010c205 | 7005 | bus_systemd_mgr, |
6e646d22 LP |
7006 | "PresetAllUnitFiles", |
7007 | &error, | |
7008 | &reply, | |
d309c1c3 LP |
7009 | "sbb", |
7010 | unit_file_preset_mode_to_string(arg_preset_mode), | |
7011 | arg_runtime, | |
7012 | arg_force); | |
691395d8 | 7013 | if (r < 0) |
af3d8113 | 7014 | return log_error_errno(r, "Failed to preset all units: %s", bus_error_message(&error, r)); |
d309c1c3 | 7015 | |
acc0269c | 7016 | r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet, &changes, &n_changes); |
d309c1c3 | 7017 | if (r < 0) |
acc0269c | 7018 | goto finish; |
d309c1c3 | 7019 | |
acc0269c CH |
7020 | if (arg_no_reload) { |
7021 | r = 0; | |
7022 | goto finish; | |
7023 | } | |
7024 | ||
7025 | r = daemon_reload(argc, argv, userdata); | |
d309c1c3 | 7026 | } |
acc0269c CH |
7027 | |
7028 | finish: | |
7029 | unit_file_changes_free(changes, n_changes); | |
7030 | ||
7031 | return r; | |
d309c1c3 LP |
7032 | } |
7033 | ||
3b3557c4 JS |
7034 | static int show_installation_targets_client_side(const char *name) { |
7035 | UnitFileChange *changes = NULL; | |
da6053d0 | 7036 | size_t n_changes = 0, i; |
3b3557c4 JS |
7037 | UnitFileFlags flags; |
7038 | char **p; | |
7039 | int r; | |
7040 | ||
7041 | p = STRV_MAKE(name); | |
7042 | flags = UNIT_FILE_DRY_RUN | | |
7043 | (arg_runtime ? UNIT_FILE_RUNTIME : 0); | |
7044 | ||
7045 | r = unit_file_disable(UNIT_FILE_SYSTEM, flags, NULL, p, &changes, &n_changes); | |
7046 | if (r < 0) | |
7047 | return log_error_errno(r, "Failed to get file links for %s: %m", name); | |
7048 | ||
7049 | for (i = 0; i < n_changes; i++) | |
7050 | if (changes[i].type == UNIT_FILE_UNLINK) | |
7051 | printf(" %s\n", changes[i].path); | |
7052 | ||
7053 | return 0; | |
7054 | } | |
7055 | ||
7056 | static int show_installation_targets(sd_bus *bus, const char *name) { | |
7057 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
7058 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
7059 | const char *link; | |
7060 | int r; | |
7061 | ||
8010c205 | 7062 | r = bus_call_method(bus, bus_systemd_mgr, "GetUnitFileLinks", &error, &reply, "sb", name, arg_runtime); |
3b3557c4 JS |
7063 | if (r < 0) |
7064 | return log_error_errno(r, "Failed to get unit file links for %s: %s", name, bus_error_message(&error, r)); | |
7065 | ||
7066 | r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "s"); | |
7067 | if (r < 0) | |
7068 | return bus_log_parse_error(r); | |
7069 | ||
7070 | while ((r = sd_bus_message_read(reply, "s", &link)) > 0) | |
7071 | printf(" %s\n", link); | |
7072 | ||
7073 | if (r < 0) | |
7074 | return bus_log_parse_error(r); | |
7075 | ||
7076 | r = sd_bus_message_exit_container(reply); | |
7077 | if (r < 0) | |
7078 | return bus_log_parse_error(r); | |
7079 | ||
7080 | return 0; | |
7081 | } | |
7082 | ||
e449de87 | 7083 | static int unit_is_enabled(int argc, char *argv[], void *userdata) { |
e3e0314b | 7084 | _cleanup_strv_free_ char **names = NULL; |
729e3769 LP |
7085 | bool enabled; |
7086 | char **name; | |
f459b602 | 7087 | int r; |
ee5762e3 | 7088 | |
df7c4eb6 | 7089 | r = mangle_names("to check", strv_skip(argv, 1), &names); |
cbb13b2a VP |
7090 | if (r < 0) |
7091 | return r; | |
7092 | ||
e449de87 | 7093 | r = enable_sysv_units(argv[0], names); |
729e3769 LP |
7094 | if (r < 0) |
7095 | return r; | |
ee5762e3 | 7096 | |
729e3769 | 7097 | enabled = r > 0; |
ee5762e3 | 7098 | |
4fbd7192 | 7099 | if (install_client_side()) { |
e3e0314b | 7100 | STRV_FOREACH(name, names) { |
729e3769 | 7101 | UnitFileState state; |
ee5762e3 | 7102 | |
0ec0deaa LP |
7103 | r = unit_file_get_state(arg_scope, arg_root, *name, &state); |
7104 | if (r < 0) | |
c5024cd0 | 7105 | return log_error_errno(r, "Failed to get unit file state for %s: %m", *name); |
ee5762e3 | 7106 | |
4c315c2c IS |
7107 | if (IN_SET(state, |
7108 | UNIT_FILE_ENABLED, | |
7109 | UNIT_FILE_ENABLED_RUNTIME, | |
7110 | UNIT_FILE_STATIC, | |
15d7ab87 | 7111 | UNIT_FILE_ALIAS, |
f4139308 LP |
7112 | UNIT_FILE_INDIRECT, |
7113 | UNIT_FILE_GENERATED)) | |
729e3769 LP |
7114 | enabled = true; |
7115 | ||
3b3557c4 | 7116 | if (!arg_quiet) { |
729e3769 | 7117 | puts(unit_file_state_to_string(state)); |
3b3557c4 JS |
7118 | if (arg_full) { |
7119 | r = show_installation_targets_client_side(*name); | |
7120 | if (r < 0) | |
7121 | return r; | |
7122 | } | |
7123 | } | |
71fad675 | 7124 | } |
ee5762e3 | 7125 | |
5f056378 | 7126 | r = 0; |
729e3769 | 7127 | } else { |
4afd3348 | 7128 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
4fbd7192 LP |
7129 | sd_bus *bus; |
7130 | ||
7131 | r = acquire_bus(BUS_MANAGER, &bus); | |
7132 | if (r < 0) | |
7133 | return r; | |
7134 | ||
e3e0314b | 7135 | STRV_FOREACH(name, names) { |
4afd3348 | 7136 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
729e3769 | 7137 | const char *s; |
63a723f3 | 7138 | |
8010c205 | 7139 | r = bus_call_method(bus, bus_systemd_mgr, "GetUnitFileState", &error, &reply, "s", *name); |
691395d8 LP |
7140 | if (r < 0) |
7141 | return log_error_errno(r, "Failed to get unit file state for %s: %s", *name, bus_error_message(&error, r)); | |
ee5762e3 | 7142 | |
f459b602 MAP |
7143 | r = sd_bus_message_read(reply, "s", &s); |
7144 | if (r < 0) | |
7145 | return bus_log_parse_error(r); | |
ee5762e3 | 7146 | |
f4139308 | 7147 | if (STR_IN_SET(s, "enabled", "enabled-runtime", "static", "indirect", "generated")) |
729e3769 LP |
7148 | enabled = true; |
7149 | ||
3b3557c4 | 7150 | if (!arg_quiet) { |
729e3769 | 7151 | puts(s); |
3b3557c4 JS |
7152 | if (arg_full) { |
7153 | r = show_installation_targets(bus, *name); | |
7154 | if (r < 0) | |
7155 | return r; | |
7156 | } | |
7157 | } | |
560d8f23 | 7158 | } |
ee5762e3 LP |
7159 | } |
7160 | ||
f4139308 | 7161 | return enabled ? EXIT_SUCCESS : EXIT_FAILURE; |
ee5762e3 LP |
7162 | } |
7163 | ||
adb6cd9b FB |
7164 | static int match_startup_finished(sd_bus_message *m, void *userdata, sd_bus_error *error) { |
7165 | char **state = userdata; | |
7166 | int r; | |
7167 | ||
7168 | assert(state); | |
7169 | ||
8010c205 | 7170 | r = bus_get_property_string(sd_bus_message_get_bus(m), bus_systemd_mgr, "SystemState", NULL, state); |
adb6cd9b FB |
7171 | |
7172 | sd_event_exit(sd_bus_get_event(sd_bus_message_get_bus(m)), r); | |
7173 | return 0; | |
7174 | } | |
7175 | ||
e449de87 | 7176 | static int is_system_running(int argc, char *argv[], void *userdata) { |
e686a616 | 7177 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
adb6cd9b FB |
7178 | _cleanup_(sd_bus_slot_unrefp) sd_bus_slot *slot_startup_finished = NULL; |
7179 | _cleanup_(sd_event_unrefp) sd_event* event = NULL; | |
99813a19 | 7180 | _cleanup_free_ char *state = NULL; |
4fbd7192 | 7181 | sd_bus *bus; |
99813a19 LP |
7182 | int r; |
7183 | ||
040524b4 | 7184 | if (running_in_chroot() > 0 || (arg_transport == BUS_TRANSPORT_LOCAL && !sd_booted())) { |
94f099d8 LP |
7185 | if (!arg_quiet) |
7186 | puts("offline"); | |
7187 | return EXIT_FAILURE; | |
7188 | } | |
7189 | ||
4fbd7192 LP |
7190 | r = acquire_bus(BUS_MANAGER, &bus); |
7191 | if (r < 0) | |
7192 | return r; | |
7193 | ||
adb6cd9b FB |
7194 | if (arg_wait) { |
7195 | r = sd_event_default(&event); | |
7196 | if (r >= 0) | |
7197 | r = sd_bus_attach_event(bus, event, 0); | |
7198 | if (r >= 0) | |
8010c205 | 7199 | r = bus_match_signal_async( |
adb6cd9b FB |
7200 | bus, |
7201 | &slot_startup_finished, | |
8010c205 | 7202 | bus_systemd_mgr, |
adb6cd9b FB |
7203 | "StartupFinished", |
7204 | match_startup_finished, NULL, &state); | |
7205 | if (r < 0) { | |
7206 | log_warning_errno(r, "Failed to request match for StartupFinished: %m"); | |
7207 | arg_wait = false; | |
7208 | } | |
7209 | } | |
7210 | ||
8010c205 | 7211 | r = bus_get_property_string(bus, bus_systemd_mgr, "SystemState", &error, &state); |
99813a19 | 7212 | if (r < 0) { |
adb6cd9b | 7213 | log_warning_errno(r, "Failed to query system state: %s", bus_error_message(&error, r)); |
e686a616 | 7214 | |
99813a19 LP |
7215 | if (!arg_quiet) |
7216 | puts("unknown"); | |
bfeec178 | 7217 | return EXIT_FAILURE; |
99813a19 LP |
7218 | } |
7219 | ||
adb6cd9b FB |
7220 | if (arg_wait && STR_IN_SET(state, "initializing", "starting")) { |
7221 | r = sd_event_loop(event); | |
7222 | if (r < 0) { | |
7223 | log_warning_errno(r, "Failed to get property from event loop: %m"); | |
7224 | if (!arg_quiet) | |
7225 | puts("unknown"); | |
7226 | return EXIT_FAILURE; | |
7227 | } | |
7228 | } | |
7229 | ||
99813a19 LP |
7230 | if (!arg_quiet) |
7231 | puts(state); | |
7232 | ||
7233 | return streq(state, "running") ? EXIT_SUCCESS : EXIT_FAILURE; | |
7234 | } | |
7235 | ||
7d4fb3b1 | 7236 | static int create_edit_temp_file(const char *new_path, const char *original_path, char **ret_tmp_fn) { |
45519fd6 | 7237 | _cleanup_free_ char *t = NULL; |
ae6c3cc0 | 7238 | int r; |
7d4fb3b1 RC |
7239 | |
7240 | assert(new_path); | |
7241 | assert(original_path); | |
7242 | assert(ret_tmp_fn); | |
7243 | ||
14bcf25c | 7244 | r = tempfn_random(new_path, NULL, &t); |
ae6c3cc0 | 7245 | if (r < 0) |
029009d4 | 7246 | return log_error_errno(r, "Failed to determine temporary filename for \"%s\": %m", new_path); |
7d4fb3b1 RC |
7247 | |
7248 | r = mkdir_parents(new_path, 0755); | |
45519fd6 | 7249 | if (r < 0) |
691395d8 | 7250 | return log_error_errno(r, "Failed to create directories for \"%s\": %m", new_path); |
7d4fb3b1 | 7251 | |
8a016c74 | 7252 | r = copy_file(original_path, t, 0, 0644, 0, 0, COPY_REFLINK); |
7d4fb3b1 | 7253 | if (r == -ENOENT) { |
45519fd6 | 7254 | |
7d4fb3b1 | 7255 | r = touch(t); |
45519fd6 LP |
7256 | if (r < 0) |
7257 | return log_error_errno(r, "Failed to create temporary file \"%s\": %m", t); | |
7258 | ||
7259 | } else if (r < 0) | |
39c38ce1 | 7260 | return log_error_errno(r, "Failed to create temporary file for \"%s\": %m", new_path); |
7d4fb3b1 | 7261 | |
ae2a15bc | 7262 | *ret_tmp_fn = TAKE_PTR(t); |
7d4fb3b1 RC |
7263 | |
7264 | return 0; | |
7265 | } | |
7266 | ||
c51932be LP |
7267 | static int get_file_to_edit( |
7268 | const LookupPaths *paths, | |
7269 | const char *name, | |
7270 | char **ret_path) { | |
7271 | ||
7272 | _cleanup_free_ char *path = NULL, *run = NULL; | |
7d4fb3b1 | 7273 | |
45519fd6 LP |
7274 | assert(name); |
7275 | assert(ret_path); | |
7276 | ||
657ee2d8 | 7277 | path = path_join(paths->persistent_config, name); |
c51932be | 7278 | if (!path) |
7d4fb3b1 RC |
7279 | return log_oom(); |
7280 | ||
c51932be | 7281 | if (arg_runtime) { |
657ee2d8 | 7282 | run = path_join(paths->runtime_config, name); |
c51932be LP |
7283 | if (!run) |
7284 | return log_oom(); | |
7285 | } | |
7286 | ||
bc854dc7 | 7287 | if (arg_runtime) { |
baaa35ad ZJS |
7288 | if (access(path, F_OK) >= 0) |
7289 | return log_error_errno(SYNTHETIC_ERRNO(EEXIST), | |
7290 | "Refusing to create \"%s\" because it would be overridden by \"%s\" anyway.", | |
7291 | run, path); | |
691395d8 | 7292 | |
ae2a15bc LP |
7293 | *ret_path = TAKE_PTR(run); |
7294 | } else | |
7295 | *ret_path = TAKE_PTR(path); | |
7d4fb3b1 RC |
7296 | |
7297 | return 0; | |
7298 | } | |
7299 | ||
39c38ce1 | 7300 | static int unit_file_create_new( |
c51932be LP |
7301 | const LookupPaths *paths, |
7302 | const char *unit_name, | |
39c38ce1 | 7303 | const char *suffix, |
c51932be LP |
7304 | char **ret_new_path, |
7305 | char **ret_tmp_path) { | |
7306 | ||
919d2720 LP |
7307 | _cleanup_free_ char *new_path = NULL, *tmp_path = NULL; |
7308 | const char *ending; | |
7d4fb3b1 RC |
7309 | int r; |
7310 | ||
7311 | assert(unit_name); | |
7312 | assert(ret_new_path); | |
7313 | assert(ret_tmp_path); | |
7314 | ||
39c38ce1 | 7315 | ending = strjoina(unit_name, suffix); |
919d2720 | 7316 | r = get_file_to_edit(paths, ending, &new_path); |
7d4fb3b1 RC |
7317 | if (r < 0) |
7318 | return r; | |
7319 | ||
919d2720 LP |
7320 | r = create_edit_temp_file(new_path, new_path, &tmp_path); |
7321 | if (r < 0) | |
7d4fb3b1 | 7322 | return r; |
7d4fb3b1 | 7323 | |
919d2720 LP |
7324 | *ret_new_path = TAKE_PTR(new_path); |
7325 | *ret_tmp_path = TAKE_PTR(tmp_path); | |
7d4fb3b1 RC |
7326 | |
7327 | return 0; | |
7328 | } | |
7329 | ||
1cfa9a4c | 7330 | static int unit_file_create_copy( |
c51932be | 7331 | const LookupPaths *paths, |
1cfa9a4c LP |
7332 | const char *unit_name, |
7333 | const char *fragment_path, | |
1cfa9a4c LP |
7334 | char **ret_new_path, |
7335 | char **ret_tmp_path) { | |
7336 | ||
92d6b0bf | 7337 | _cleanup_free_ char *new_path = NULL, *tmp_path = NULL; |
7d4fb3b1 RC |
7338 | int r; |
7339 | ||
7340 | assert(fragment_path); | |
7341 | assert(unit_name); | |
7342 | assert(ret_new_path); | |
7343 | assert(ret_tmp_path); | |
7344 | ||
92d6b0bf | 7345 | r = get_file_to_edit(paths, unit_name, &new_path); |
7d4fb3b1 RC |
7346 | if (r < 0) |
7347 | return r; | |
7348 | ||
92d6b0bf | 7349 | if (!path_equal(fragment_path, new_path) && access(new_path, F_OK) >= 0) { |
7d4fb3b1 RC |
7350 | char response; |
7351 | ||
92d6b0bf LP |
7352 | r = ask_char(&response, "yn", "\"%s\" already exists. Overwrite with \"%s\"? [(y)es, (n)o] ", new_path, fragment_path); |
7353 | if (r < 0) | |
7d4fb3b1 | 7354 | return r; |
5daacba2 LP |
7355 | if (response != 'y') |
7356 | return log_warning_errno(SYNTHETIC_ERRNO(EKEYREJECTED), "%s skipped.", unit_name); | |
7d4fb3b1 RC |
7357 | } |
7358 | ||
92d6b0bf LP |
7359 | r = create_edit_temp_file(new_path, fragment_path, &tmp_path); |
7360 | if (r < 0) | |
7d4fb3b1 | 7361 | return r; |
7d4fb3b1 | 7362 | |
92d6b0bf LP |
7363 | *ret_new_path = TAKE_PTR(new_path); |
7364 | *ret_tmp_path = TAKE_PTR(tmp_path); | |
7d4fb3b1 RC |
7365 | |
7366 | return 0; | |
7367 | } | |
7368 | ||
7369 | static int run_editor(char **paths) { | |
7d4fb3b1 RC |
7370 | int r; |
7371 | ||
7372 | assert(paths); | |
7373 | ||
0672e2c6 | 7374 | r = safe_fork("(editor)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG|FORK_WAIT, NULL); |
4c253ed1 | 7375 | if (r < 0) |
b6e1fff1 | 7376 | return r; |
4c253ed1 | 7377 | if (r == 0) { |
b2238e38 | 7378 | char **editor_args = NULL, **tmp_path, **original_path; |
70d6e5bd | 7379 | size_t n_editor_args = 0, i = 1, argc; |
b2238e38 | 7380 | const char **args, *editor, *p; |
7d4fb3b1 RC |
7381 | |
7382 | argc = strv_length(paths)/2 + 1; | |
7d4fb3b1 RC |
7383 | |
7384 | /* SYSTEMD_EDITOR takes precedence over EDITOR which takes precedence over VISUAL | |
7385 | * If neither SYSTEMD_EDITOR nor EDITOR nor VISUAL are present, | |
7386 | * we try to execute well known editors | |
7387 | */ | |
7388 | editor = getenv("SYSTEMD_EDITOR"); | |
7389 | if (!editor) | |
7390 | editor = getenv("EDITOR"); | |
7391 | if (!editor) | |
7392 | editor = getenv("VISUAL"); | |
7393 | ||
7394 | if (!isempty(editor)) { | |
9ef5d8f2 JS |
7395 | editor_args = strv_split(editor, WHITESPACE); |
7396 | if (!editor_args) { | |
7397 | (void) log_oom(); | |
7398 | _exit(EXIT_FAILURE); | |
7399 | } | |
7400 | n_editor_args = strv_length(editor_args); | |
7401 | argc += n_editor_args - 1; | |
7402 | } | |
02a126a3 | 7403 | |
9ef5d8f2 JS |
7404 | args = newa(const char*, argc + 1); |
7405 | ||
7406 | if (n_editor_args > 0) { | |
7407 | args[0] = editor_args[0]; | |
7408 | for (; i < n_editor_args; i++) | |
7409 | args[i] = editor_args[i]; | |
7410 | } | |
7411 | ||
df79fdab LP |
7412 | STRV_FOREACH_PAIR(original_path, tmp_path, paths) |
7413 | args[i++] = *tmp_path; | |
9ef5d8f2 JS |
7414 | args[i] = NULL; |
7415 | ||
7416 | if (n_editor_args > 0) | |
7417 | execvp(args[0], (char* const*) args); | |
7d4fb3b1 | 7418 | |
9391a1c3 | 7419 | FOREACH_STRING(p, "editor", "nano", "vim", "vi") { |
1cfa9a4c LP |
7420 | args[0] = p; |
7421 | execvp(p, (char* const*) args); | |
7d4fb3b1 RC |
7422 | /* We do not fail if the editor doesn't exist |
7423 | * because we want to try each one of them before | |
7424 | * failing. | |
7425 | */ | |
7426 | if (errno != ENOENT) { | |
691395d8 | 7427 | log_error_errno(errno, "Failed to execute %s: %m", editor); |
7d4fb3b1 RC |
7428 | _exit(EXIT_FAILURE); |
7429 | } | |
7430 | } | |
7431 | ||
1cfa9a4c | 7432 | log_error("Cannot edit unit(s), no editor available. Please set either $SYSTEMD_EDITOR, $EDITOR or $VISUAL."); |
7d4fb3b1 RC |
7433 | _exit(EXIT_FAILURE); |
7434 | } | |
7435 | ||
45519fd6 | 7436 | return 0; |
7d4fb3b1 RC |
7437 | } |
7438 | ||
7439 | static int find_paths_to_edit(sd_bus *bus, char **names, char ***paths) { | |
8e766630 | 7440 | _cleanup_(lookup_paths_free) LookupPaths lp = {}; |
7d4fb3b1 RC |
7441 | char **name; |
7442 | int r; | |
7443 | ||
7444 | assert(names); | |
7445 | assert(paths); | |
7446 | ||
4943d143 | 7447 | r = lookup_paths_init(&lp, arg_scope, 0, arg_root); |
5b013a2f | 7448 | if (r < 0) |
8df18507 | 7449 | return r; |
7d4fb3b1 | 7450 | |
e9e310f8 | 7451 | STRV_FOREACH(name, names) { |
b74df547 ZJS |
7452 | _cleanup_free_ char *path = NULL, *new_path = NULL, *tmp_path = NULL, *tmp_name = NULL; |
7453 | const char *unit_name; | |
7d4fb3b1 | 7454 | |
02a126a3 LP |
7455 | r = unit_find_paths(bus, *name, &lp, false, &path, NULL); |
7456 | if (r == -EKEYREJECTED) { | |
7457 | /* If loading of the unit failed server side complete, then the server won't tell us the unit | |
7458 | * file path. In that case, find the file client side. */ | |
7459 | log_debug_errno(r, "Unit '%s' was not loaded correctly, retrying client-side.", *name); | |
7460 | r = unit_find_paths(bus, *name, &lp, true, &path, NULL); | |
7461 | } | |
7462 | if (r == -ERFKILL) | |
7463 | return log_error_errno(r, "Unit '%s' masked, cannot edit.", *name); | |
e9e310f8 RC |
7464 | if (r < 0) |
7465 | return r; | |
b3734d98 ZJS |
7466 | |
7467 | if (r == 0) { | |
7468 | assert(!path); | |
7469 | ||
7470 | if (!arg_force) { | |
85163756 LP |
7471 | log_info("Run 'systemctl edit%s --force --full %s' to create a new unit.", |
7472 | arg_scope == UNIT_FILE_GLOBAL ? " --global" : | |
7473 | arg_scope == UNIT_FILE_USER ? " --user" : "", | |
7474 | *name); | |
39c38ce1 DC |
7475 | return -ENOENT; |
7476 | } | |
39c38ce1 | 7477 | |
b3734d98 ZJS |
7478 | /* Create a new unit from scratch */ |
7479 | unit_name = *name; | |
7480 | r = unit_file_create_new(&lp, unit_name, | |
7481 | arg_full ? NULL : ".d/override.conf", | |
7482 | &new_path, &tmp_path); | |
7483 | } else { | |
7484 | assert(path); | |
b74df547 | 7485 | |
b3734d98 ZJS |
7486 | unit_name = basename(path); |
7487 | /* We follow unit aliases, but we need to propagate the instance */ | |
7488 | if (unit_name_is_valid(*name, UNIT_NAME_INSTANCE) && | |
7489 | unit_name_is_valid(unit_name, UNIT_NAME_TEMPLATE)) { | |
7490 | _cleanup_free_ char *instance = NULL; | |
b74df547 | 7491 | |
b3734d98 ZJS |
7492 | r = unit_name_to_instance(*name, &instance); |
7493 | if (r < 0) | |
7494 | return r; | |
b74df547 | 7495 | |
b3734d98 ZJS |
7496 | r = unit_name_replace_instance(unit_name, instance, &tmp_name); |
7497 | if (r < 0) | |
7498 | return r; | |
7499 | ||
7500 | unit_name = tmp_name; | |
7501 | } | |
b74df547 | 7502 | |
39c38ce1 | 7503 | if (arg_full) |
b74df547 | 7504 | r = unit_file_create_copy(&lp, unit_name, path, &new_path, &tmp_path); |
39c38ce1 | 7505 | else |
b74df547 | 7506 | r = unit_file_create_new(&lp, unit_name, ".d/override.conf", &new_path, &tmp_path); |
b3734d98 | 7507 | } |
e9e310f8 | 7508 | if (r < 0) |
ad2a0358 | 7509 | return r; |
7d4fb3b1 | 7510 | |
e9e310f8 RC |
7511 | r = strv_push_pair(paths, new_path, tmp_path); |
7512 | if (r < 0) | |
7513 | return log_oom(); | |
02a126a3 | 7514 | |
78df0edc | 7515 | new_path = tmp_path = NULL; |
7d4fb3b1 RC |
7516 | } |
7517 | ||
7518 | return 0; | |
7519 | } | |
7520 | ||
e449de87 | 7521 | static int edit(int argc, char *argv[], void *userdata) { |
86f004fb | 7522 | _cleanup_(lookup_paths_free) LookupPaths lp = {}; |
7d4fb3b1 RC |
7523 | _cleanup_strv_free_ char **names = NULL; |
7524 | _cleanup_strv_free_ char **paths = NULL; | |
7525 | char **original, **tmp; | |
4fbd7192 | 7526 | sd_bus *bus; |
7d4fb3b1 RC |
7527 | int r; |
7528 | ||
2f50e59f LP |
7529 | if (!on_tty()) |
7530 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot edit units if not on a tty."); | |
7d4fb3b1 | 7531 | |
2f50e59f LP |
7532 | if (arg_transport != BUS_TRANSPORT_LOCAL) |
7533 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot edit units remotely."); | |
7d4fb3b1 | 7534 | |
86f004fb ZJS |
7535 | r = lookup_paths_init(&lp, arg_scope, 0, arg_root); |
7536 | if (r < 0) | |
7537 | return log_error_errno(r, "Failed to determine unit paths: %m"); | |
7538 | ||
4fbd7192 LP |
7539 | r = acquire_bus(BUS_MANAGER, &bus); |
7540 | if (r < 0) | |
7541 | return r; | |
7542 | ||
1d56bc09 | 7543 | r = expand_names(bus, strv_skip(argv, 1), NULL, &names, NULL); |
7d4fb3b1 RC |
7544 | if (r < 0) |
7545 | return log_error_errno(r, "Failed to expand names: %m"); | |
7546 | ||
9d9dd746 | 7547 | STRV_FOREACH(tmp, names) { |
86f004fb | 7548 | r = unit_is_masked(bus, &lp, *tmp); |
9d9dd746 ZJS |
7549 | if (r < 0) |
7550 | return r; | |
2f50e59f LP |
7551 | if (r > 0) |
7552 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot edit %s: unit is masked.", *tmp); | |
9d9dd746 ZJS |
7553 | } |
7554 | ||
7d4fb3b1 RC |
7555 | r = find_paths_to_edit(bus, names, &paths); |
7556 | if (r < 0) | |
7557 | return r; | |
7558 | ||
b5e6a600 | 7559 | if (strv_isempty(paths)) |
7d4fb3b1 | 7560 | return -ENOENT; |
7d4fb3b1 RC |
7561 | |
7562 | r = run_editor(paths); | |
7563 | if (r < 0) | |
7564 | goto end; | |
7565 | ||
7566 | STRV_FOREACH_PAIR(original, tmp, paths) { | |
9d9dd746 ZJS |
7567 | /* If the temporary file is empty we ignore it. |
7568 | * This allows the user to cancel the modification. | |
7d4fb3b1 RC |
7569 | */ |
7570 | if (null_or_empty_path(*tmp)) { | |
45519fd6 | 7571 | log_warning("Editing \"%s\" canceled: temporary file is empty.", *original); |
7d4fb3b1 RC |
7572 | continue; |
7573 | } | |
45519fd6 | 7574 | |
7d4fb3b1 RC |
7575 | r = rename(*tmp, *original); |
7576 | if (r < 0) { | |
029009d4 | 7577 | r = log_error_errno(errno, "Failed to rename \"%s\" to \"%s\": %m", *tmp, *original); |
7d4fb3b1 RC |
7578 | goto end; |
7579 | } | |
7580 | } | |
7581 | ||
45519fd6 LP |
7582 | r = 0; |
7583 | ||
7584 | if (!arg_no_reload && !install_client_side()) | |
e449de87 | 7585 | r = daemon_reload(argc, argv, userdata); |
7d4fb3b1 RC |
7586 | |
7587 | end: | |
5f18271e | 7588 | STRV_FOREACH_PAIR(original, tmp, paths) { |
45519fd6 | 7589 | (void) unlink(*tmp); |
7d4fb3b1 | 7590 | |
5f18271e RC |
7591 | /* Removing empty dropin dirs */ |
7592 | if (!arg_full) { | |
043717f9 RC |
7593 | _cleanup_free_ char *dir; |
7594 | ||
7595 | dir = dirname_malloc(*original); | |
7596 | if (!dir) | |
7597 | return log_oom(); | |
7598 | ||
5f18271e RC |
7599 | /* no need to check if the dir is empty, rmdir |
7600 | * does nothing if it is not the case. | |
7601 | */ | |
7602 | (void) rmdir(dir); | |
7603 | } | |
7604 | } | |
7605 | ||
7d4fb3b1 RC |
7606 | return r; |
7607 | } | |
7608 | ||
37ec0fdd LP |
7609 | static int systemctl_help(void) { |
7610 | _cleanup_free_ char *link = NULL; | |
7611 | int r; | |
7612 | ||
0221d68a | 7613 | (void) pager_open(arg_pager_flags); |
729e3769 | 7614 | |
37ec0fdd LP |
7615 | r = terminal_urlify_man("systemctl", "1", &link); |
7616 | if (r < 0) | |
7617 | return log_oom(); | |
7618 | ||
353b2baa LP |
7619 | printf("%1$s [OPTIONS...] COMMAND ...\n\n" |
7620 | "%5$sQuery or send control commands to the system manager.%6$s\n" | |
7621 | "\n%3$sUnit Commands:%4$s\n" | |
fbf3283b ZJS |
7622 | " list-units [PATTERN...] List units currently in memory\n" |
7623 | " list-sockets [PATTERN...] List socket units currently in memory,\n" | |
7624 | " ordered by address\n" | |
7625 | " list-timers [PATTERN...] List timer units currently in memory,\n" | |
7626 | " ordered by next elapse\n" | |
7627 | " start UNIT... Start (activate) one or more units\n" | |
7628 | " stop UNIT... Stop (deactivate) one or more units\n" | |
7629 | " reload UNIT... Reload one or more units\n" | |
7630 | " restart UNIT... Start or restart one or more units\n" | |
7631 | " try-restart UNIT... Restart one or more units if active\n" | |
7632 | " reload-or-restart UNIT... Reload one or more units if possible,\n" | |
7633 | " otherwise start or restart\n" | |
7634 | " try-reload-or-restart UNIT... If active, reload one or more units,\n" | |
7635 | " if supported, otherwise restart\n" | |
7636 | " isolate UNIT Start one unit and stop all others\n" | |
7637 | " kill UNIT... Send signal to processes of a unit\n" | |
d81bc51f | 7638 | " clean UNIT... Clean runtime, cache, state, logs or\n" |
ab4fae0c | 7639 | " configuration of unit\n" |
d9e45bc3 MS |
7640 | " freeze PATTERN... Freeze execution of unit processes\n" |
7641 | " thaw PATTERN... Resume execution of a frozen unit\n" | |
fbf3283b ZJS |
7642 | " is-active PATTERN... Check whether units are active\n" |
7643 | " is-failed PATTERN... Check whether units are failed\n" | |
7644 | " status [PATTERN...|PID...] Show runtime status of one or more units\n" | |
7645 | " show [PATTERN...|JOB...] Show properties of one or more\n" | |
7646 | " units/jobs or the manager\n" | |
7647 | " cat PATTERN... Show files and drop-ins of specified units\n" | |
7648 | " set-property UNIT PROPERTY=VALUE... Sets one or more properties of a unit\n" | |
7649 | " help PATTERN...|PID... Show manual for one or more units\n" | |
7650 | " reset-failed [PATTERN...] Reset failed state for all, one, or more\n" | |
7651 | " units\n" | |
94c3a838 ZJS |
7652 | " list-dependencies [UNIT...] Recursively show units which are required\n" |
7653 | " or wanted by the units or by which those\n" | |
7654 | " units are required or wanted" | |
353b2baa | 7655 | "\n%3$sUnit File Commands:%4$s\n" |
fbf3283b ZJS |
7656 | " list-unit-files [PATTERN...] List installed unit files\n" |
7657 | " enable [UNIT...|PATH...] Enable one or more unit files\n" | |
7658 | " disable UNIT... Disable one or more unit files\n" | |
7659 | " reenable UNIT... Reenable one or more unit files\n" | |
7660 | " preset UNIT... Enable/disable one or more unit files\n" | |
7661 | " based on preset configuration\n" | |
7662 | " preset-all Enable/disable all unit files based on\n" | |
7663 | " preset configuration\n" | |
7664 | " is-enabled UNIT... Check whether unit files are enabled\n" | |
7665 | " mask UNIT... Mask one or more units\n" | |
7666 | " unmask UNIT... Unmask one or more units\n" | |
7667 | " link PATH... Link one or more units files into\n" | |
7668 | " the search path\n" | |
7669 | " revert UNIT... Revert one or more unit files to vendor\n" | |
7670 | " version\n" | |
7671 | " add-wants TARGET UNIT... Add 'Wants' dependency for the target\n" | |
7672 | " on specified one or more units\n" | |
7673 | " add-requires TARGET UNIT... Add 'Requires' dependency for the target\n" | |
7674 | " on specified one or more units\n" | |
7675 | " edit UNIT... Edit one or more unit files\n" | |
7676 | " get-default Get the name of the default target\n" | |
353b2baa LP |
7677 | " set-default TARGET Set the default target\n" |
7678 | "\n%3$sMachine Commands:%4$s\n" | |
4d985a31 | 7679 | " list-machines [PATTERN...] List local containers and host\n" |
353b2baa | 7680 | "\n%3$sJob Commands:%4$s\n" |
fbf3283b | 7681 | " list-jobs [PATTERN...] List jobs\n" |
353b2baa LP |
7682 | " cancel [JOB...] Cancel all, one, or more jobs\n" |
7683 | "\n%3$sEnvironment Commands:%4$s\n" | |
fbf3283b | 7684 | " show-environment Dump environment\n" |
135775c1 ZJS |
7685 | " set-environment VARIABLE=VALUE... Set one or more environment variables\n" |
7686 | " unset-environment VARIABLE... Unset one or more environment variables\n" | |
4d985a31 | 7687 | " import-environment [VARIABLE...] Import all or some environment variables\n" |
38fcb7f7 | 7688 | "\n%3$sManager State Commands:%4$s\n" |
fbf3283b | 7689 | " daemon-reload Reload systemd manager configuration\n" |
353b2baa | 7690 | " daemon-reexec Reexecute systemd manager\n" |
38fcb7f7 ZJS |
7691 | " log-level [LEVEL] Get/set logging threshold for manager\n" |
7692 | " log-target [TARGET] Get/set logging target for manager\n" | |
6ab86319 | 7693 | " service-watchdogs [BOOL] Get/set service watchdog state\n" |
353b2baa | 7694 | "\n%3$sSystem Commands:%4$s\n" |
fbf3283b ZJS |
7695 | " is-system-running Check whether system is fully running\n" |
7696 | " default Enter system default mode\n" | |
7697 | " rescue Enter system rescue mode\n" | |
7698 | " emergency Enter system emergency mode\n" | |
7699 | " halt Shut down and halt the system\n" | |
7700 | " poweroff Shut down and power-off the system\n" | |
dae710be | 7701 | " reboot Shut down and reboot the system\n" |
fbf3283b ZJS |
7702 | " kexec Shut down and reboot the system with kexec\n" |
7703 | " exit [EXIT_CODE] Request user instance or container exit\n" | |
7704 | " switch-root ROOT [INIT] Change to a different root file system\n" | |
7705 | " suspend Suspend the system\n" | |
7706 | " hibernate Hibernate the system\n" | |
c58493c0 | 7707 | " hybrid-sleep Hibernate and suspend the system\n" |
e68c79db | 7708 | " suspend-then-hibernate Suspend the system, wake after a period of\n" |
4d985a31 | 7709 | " time, and hibernate" |
353b2baa | 7710 | "\n%3$sOptions:%4$s\n" |
4d985a31 ZJS |
7711 | " -h --help Show this help\n" |
7712 | " --version Show package version\n" | |
7713 | " --system Connect to system manager\n" | |
7714 | " --user Connect to user service manager\n" | |
7715 | " -H --host=[USER@]HOST Operate on remote host\n" | |
7716 | " -M --machine=CONTAINER Operate on a local container\n" | |
7717 | " -t --type=TYPE List units of a particular type\n" | |
7718 | " --state=STATE List units with particular LOAD or SUB or ACTIVE state\n" | |
7719 | " --failed Shorcut for --state=failed\n" | |
7720 | " -p --property=NAME Show only properties by this name\n" | |
5292c240 | 7721 | " -P NAME Equivalent to --value --property=NAME\n" |
4d985a31 ZJS |
7722 | " -a --all Show all properties/all units currently in memory,\n" |
7723 | " including dead/empty ones. To list all units installed\n" | |
7724 | " on the system, use 'list-unit-files' instead.\n" | |
7725 | " -l --full Don't ellipsize unit names on output\n" | |
7726 | " -r --recursive Show unit list of host and local containers\n" | |
7727 | " --reverse Show reverse dependencies with 'list-dependencies'\n" | |
e9c387c8 KK |
7728 | " --with-dependencies Show unit dependencies with 'status', 'cat',\n" |
7729 | " 'list-units', and 'list-unit-files'.\n" | |
4d985a31 ZJS |
7730 | " --job-mode=MODE Specify how to deal with already queued jobs, when\n" |
7731 | " queueing a new job\n" | |
7732 | " -T --show-transaction When enqueuing a unit job, show full transaction\n" | |
7733 | " --show-types When showing sockets, explicitly show their type\n" | |
7734 | " --value When showing properties, only print the value\n" | |
7735 | " -i --ignore-inhibitors When shutting down or sleeping, ignore inhibitors\n" | |
7736 | " --kill-who=WHO Whom to send signal to\n" | |
7737 | " -s --signal=SIGNAL Which signal to send\n" | |
7738 | " --what=RESOURCES Which types of resources to remove\n" | |
7739 | " --now Start or stop unit after enabling or disabling it\n" | |
7740 | " --dry-run Only print what would be done\n" | |
4afb4a9c BW |
7741 | " Currently supported by verbs: halt, poweroff, reboot,\n" |
7742 | " kexec, suspend, hibernate, suspend-then-hibernate,\n" | |
7743 | " hybrid-sleep, default, rescue, emergency, and exit.\n" | |
4d985a31 ZJS |
7744 | " -q --quiet Suppress output\n" |
7745 | " --wait For (re)start, wait until service stopped again\n" | |
7746 | " For is-system-running, wait until startup is completed\n" | |
7747 | " --no-block Do not wait until operation finished\n" | |
7748 | " --no-wall Don't send wall message before halt/power-off/reboot\n" | |
7749 | " --no-reload Don't reload daemon after en-/dis-abling unit files\n" | |
7750 | " --no-legend Do not print a legend (column headers and hints)\n" | |
7751 | " --no-pager Do not pipe output into a pager\n" | |
7752 | " --no-ask-password Do not ask for system passwords\n" | |
7753 | " --global Enable/disable/mask unit files globally\n" | |
7754 | " --runtime Enable/disable/mask unit files temporarily until next\n" | |
7755 | " reboot\n" | |
7756 | " -f --force When enabling unit files, override existing symlinks\n" | |
7757 | " When shutting down, execute action immediately\n" | |
7758 | " --preset-mode= Apply only enable, only disable, or all presets\n" | |
7759 | " --root=PATH Enable/disable/mask unit files in the specified root\n" | |
7760 | " directory\n" | |
7761 | " -n --lines=INTEGER Number of journal entries to show\n" | |
7762 | " -o --output=STRING Change journal output mode (short, short-precise,\n" | |
e1fac8a6 ZJS |
7763 | " short-iso, short-iso-precise, short-full,\n" |
7764 | " short-monotonic, short-unix,\n" | |
7765 | " verbose, export, json, json-pretty, json-sse, cat)\n" | |
4d985a31 | 7766 | " --firmware-setup Tell the firmware to show the setup menu on next boot\n" |
e1fac8a6 | 7767 | " --boot-loader-menu=TIME\n" |
4d985a31 | 7768 | " Boot into boot loader menu on next boot\n" |
e1fac8a6 | 7769 | " --boot-loader-entry=NAME\n" |
4d985a31 ZJS |
7770 | " Boot into a specific boot loader entry on next boot\n" |
7771 | " --plain Print unit dependencies as a list instead of a tree\n" | |
8e543004 | 7772 | "\nSee the %2$s for details.\n" |
37ec0fdd LP |
7773 | , program_invocation_short_name |
7774 | , link | |
8e543004 | 7775 | , ansi_underline(), ansi_normal() |
ce2529b4 | 7776 | , ansi_highlight(), ansi_normal() |
37ec0fdd LP |
7777 | ); |
7778 | ||
7779 | return 0; | |
7e4249b9 LP |
7780 | } |
7781 | ||
37ec0fdd LP |
7782 | static int halt_help(void) { |
7783 | _cleanup_free_ char *link = NULL; | |
7784 | int r; | |
7785 | ||
7786 | r = terminal_urlify_man("halt", "8", &link); | |
7787 | if (r < 0) | |
7788 | return log_oom(); | |
7789 | ||
353b2baa LP |
7790 | printf("%s [OPTIONS...]%s\n" |
7791 | "\n%s%s the system.%s\n" | |
7792 | "\nOptions:\n" | |
e4b61340 LP |
7793 | " --help Show this help\n" |
7794 | " --halt Halt the machine\n" | |
7795 | " -p --poweroff Switch off the machine\n" | |
7796 | " --reboot Reboot the machine\n" | |
2e33c433 LP |
7797 | " -f --force Force immediate halt/power-off/reboot\n" |
7798 | " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n" | |
e4b61340 | 7799 | " -d --no-wtmp Don't write wtmp record\n" |
37ec0fdd LP |
7800 | " --no-wall Don't send wall message before halt/power-off/reboot\n" |
7801 | "\nSee the %s for details.\n" | |
7802 | , program_invocation_short_name | |
353b2baa LP |
7803 | , arg_action == ACTION_REBOOT ? " [ARG]" : "" |
7804 | , ansi_highlight() | |
7805 | , arg_action == ACTION_REBOOT ? "Reboot" : | |
37ec0fdd LP |
7806 | arg_action == ACTION_POWEROFF ? "Power off" : |
7807 | "Halt" | |
353b2baa | 7808 | , ansi_normal() |
37ec0fdd LP |
7809 | , link |
7810 | ); | |
7811 | ||
7812 | return 0; | |
e4b61340 LP |
7813 | } |
7814 | ||
37ec0fdd LP |
7815 | static int shutdown_help(void) { |
7816 | _cleanup_free_ char *link = NULL; | |
7817 | int r; | |
7818 | ||
7819 | r = terminal_urlify_man("shutdown", "8", &link); | |
7820 | if (r < 0) | |
7821 | return log_oom(); | |
7822 | ||
353b2baa LP |
7823 | printf("%s [OPTIONS...] [TIME] [WALL...]\n" |
7824 | "\n%sShut down the system.%s\n" | |
7825 | "\nOptions:\n" | |
e4b61340 LP |
7826 | " --help Show this help\n" |
7827 | " -H --halt Halt the machine\n" | |
7828 | " -P --poweroff Power-off the machine\n" | |
7829 | " -r --reboot Reboot the machine\n" | |
386da858 | 7830 | " -h Equivalent to --poweroff, overridden by --halt\n" |
2e33c433 | 7831 | " -k Don't halt/power-off/reboot, just send warnings\n" |
f6144808 | 7832 | " --no-wall Don't send wall message before halt/power-off/reboot\n" |
37ec0fdd LP |
7833 | " -c Cancel a pending shutdown\n" |
7834 | "\nSee the %s for details.\n" | |
7835 | , program_invocation_short_name | |
353b2baa | 7836 | , ansi_highlight(), ansi_normal() |
37ec0fdd LP |
7837 | , link |
7838 | ); | |
7839 | ||
7840 | return 0; | |
e4b61340 LP |
7841 | } |
7842 | ||
37ec0fdd LP |
7843 | static int telinit_help(void) { |
7844 | _cleanup_free_ char *link = NULL; | |
7845 | int r; | |
7846 | ||
7847 | r = terminal_urlify_man("telinit", "8", &link); | |
7848 | if (r < 0) | |
7849 | return log_oom(); | |
7850 | ||
353b2baa LP |
7851 | printf("%s [OPTIONS...] COMMAND\n\n" |
7852 | "%sSend control commands to the init daemon.%s\n" | |
7853 | "\nCommands:\n" | |
e4b61340 LP |
7854 | " 0 Power-off the machine\n" |
7855 | " 6 Reboot the machine\n" | |
514f4ef5 LP |
7856 | " 2, 3, 4, 5 Start runlevelX.target unit\n" |
7857 | " 1, s, S Enter rescue mode\n" | |
7858 | " q, Q Reload init daemon configuration\n" | |
37ec0fdd | 7859 | " u, U Reexecute init daemon\n" |
353b2baa LP |
7860 | "\nOptions:\n" |
7861 | " --help Show this help\n" | |
7862 | " --no-wall Don't send wall message before halt/power-off/reboot\n" | |
37ec0fdd LP |
7863 | "\nSee the %s for details.\n" |
7864 | , program_invocation_short_name | |
353b2baa | 7865 | , ansi_highlight(), ansi_normal() |
37ec0fdd LP |
7866 | , link |
7867 | ); | |
7868 | ||
7869 | return 0; | |
e4b61340 LP |
7870 | } |
7871 | ||
37ec0fdd LP |
7872 | static int runlevel_help(void) { |
7873 | _cleanup_free_ char *link = NULL; | |
7874 | int r; | |
7875 | ||
7876 | r = terminal_urlify_man("runlevel", "8", &link); | |
7877 | if (r < 0) | |
7878 | return log_oom(); | |
7879 | ||
353b2baa LP |
7880 | printf("%s [OPTIONS...]\n" |
7881 | "\n%sPrints the previous and current runlevel of the init system.%s\n" | |
7882 | "\nOptions:\n" | |
37ec0fdd LP |
7883 | " --help Show this help\n" |
7884 | "\nSee the %s for details.\n" | |
7885 | , program_invocation_short_name | |
353b2baa | 7886 | , ansi_highlight(), ansi_normal() |
37ec0fdd LP |
7887 | , link |
7888 | ); | |
7889 | ||
7890 | return 0; | |
e4b61340 LP |
7891 | } |
7892 | ||
b93312f5 | 7893 | static void help_types(void) { |
b93312f5 ZJS |
7894 | if (!arg_no_legend) |
7895 | puts("Available unit types:"); | |
5c828e66 LP |
7896 | |
7897 | DUMP_STRING_TABLE(unit_type, UnitType, _UNIT_TYPE_MAX); | |
e16972e6 ZJS |
7898 | } |
7899 | ||
7900 | static void help_states(void) { | |
e16972e6 ZJS |
7901 | if (!arg_no_legend) |
7902 | puts("Available unit load states:"); | |
5c828e66 | 7903 | DUMP_STRING_TABLE(unit_load_state, UnitLoadState, _UNIT_LOAD_STATE_MAX); |
e16972e6 ZJS |
7904 | |
7905 | if (!arg_no_legend) | |
7906 | puts("\nAvailable unit active states:"); | |
5c828e66 | 7907 | DUMP_STRING_TABLE(unit_active_state, UnitActiveState, _UNIT_ACTIVE_STATE_MAX); |
7e55de3b | 7908 | |
46ff70ca RP |
7909 | if (!arg_no_legend) |
7910 | puts("\nAvailable unit file states:"); | |
7911 | DUMP_STRING_TABLE(unit_file_state, UnitFileState, _UNIT_FILE_STATE_MAX); | |
7912 | ||
7e55de3b ZJS |
7913 | if (!arg_no_legend) |
7914 | puts("\nAvailable automount unit substates:"); | |
5c828e66 | 7915 | DUMP_STRING_TABLE(automount_state, AutomountState, _AUTOMOUNT_STATE_MAX); |
7e55de3b | 7916 | |
7e55de3b ZJS |
7917 | if (!arg_no_legend) |
7918 | puts("\nAvailable device unit substates:"); | |
5c828e66 | 7919 | DUMP_STRING_TABLE(device_state, DeviceState, _DEVICE_STATE_MAX); |
7e55de3b ZJS |
7920 | |
7921 | if (!arg_no_legend) | |
7922 | puts("\nAvailable mount unit substates:"); | |
5c828e66 | 7923 | DUMP_STRING_TABLE(mount_state, MountState, _MOUNT_STATE_MAX); |
7e55de3b ZJS |
7924 | |
7925 | if (!arg_no_legend) | |
7926 | puts("\nAvailable path unit substates:"); | |
5c828e66 | 7927 | DUMP_STRING_TABLE(path_state, PathState, _PATH_STATE_MAX); |
7e55de3b ZJS |
7928 | |
7929 | if (!arg_no_legend) | |
7930 | puts("\nAvailable scope unit substates:"); | |
5c828e66 | 7931 | DUMP_STRING_TABLE(scope_state, ScopeState, _SCOPE_STATE_MAX); |
7e55de3b ZJS |
7932 | |
7933 | if (!arg_no_legend) | |
7934 | puts("\nAvailable service unit substates:"); | |
5c828e66 | 7935 | DUMP_STRING_TABLE(service_state, ServiceState, _SERVICE_STATE_MAX); |
7e55de3b ZJS |
7936 | |
7937 | if (!arg_no_legend) | |
7938 | puts("\nAvailable slice unit substates:"); | |
5c828e66 | 7939 | DUMP_STRING_TABLE(slice_state, SliceState, _SLICE_STATE_MAX); |
7e55de3b | 7940 | |
7e55de3b ZJS |
7941 | if (!arg_no_legend) |
7942 | puts("\nAvailable socket unit substates:"); | |
5c828e66 | 7943 | DUMP_STRING_TABLE(socket_state, SocketState, _SOCKET_STATE_MAX); |
7e55de3b ZJS |
7944 | |
7945 | if (!arg_no_legend) | |
7946 | puts("\nAvailable swap unit substates:"); | |
5c828e66 | 7947 | DUMP_STRING_TABLE(swap_state, SwapState, _SWAP_STATE_MAX); |
7e55de3b ZJS |
7948 | |
7949 | if (!arg_no_legend) | |
7950 | puts("\nAvailable target unit substates:"); | |
5c828e66 | 7951 | DUMP_STRING_TABLE(target_state, TargetState, _TARGET_STATE_MAX); |
7e55de3b ZJS |
7952 | |
7953 | if (!arg_no_legend) | |
7954 | puts("\nAvailable timer unit substates:"); | |
5c828e66 | 7955 | DUMP_STRING_TABLE(timer_state, TimerState, _TIMER_STATE_MAX); |
45c0c61d ZJS |
7956 | } |
7957 | ||
97af80c5 LP |
7958 | static int help_boot_loader_entry(void) { |
7959 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
13f697b7 | 7960 | _cleanup_strv_free_ char **l = NULL; |
97af80c5 LP |
7961 | sd_bus *bus; |
7962 | char **i; | |
7963 | int r; | |
7964 | ||
7965 | r = acquire_bus(BUS_FULL, &bus); | |
7966 | if (r < 0) | |
7967 | return r; | |
7968 | ||
8010c205 | 7969 | r = bus_get_property_strv(bus, bus_login_mgr, "BootLoaderEntries", &error, &l); |
97af80c5 LP |
7970 | if (r < 0) |
7971 | return log_error_errno(r, "Failed to enumerate boot loader entries: %s", bus_error_message(&error, r)); | |
7972 | ||
2f50e59f LP |
7973 | if (strv_isempty(l)) |
7974 | return log_error_errno(SYNTHETIC_ERRNO(ENODATA), "No boot loader entries discovered."); | |
97af80c5 LP |
7975 | |
7976 | STRV_FOREACH(i, l) | |
7977 | puts(*i); | |
7978 | ||
7979 | return 0; | |
7980 | } | |
7981 | ||
e4b61340 | 7982 | static int systemctl_parse_argv(int argc, char *argv[]) { |
7e4249b9 | 7983 | enum { |
90d473a1 | 7984 | ARG_FAIL = 0x100, |
afba4199 ZJS |
7985 | ARG_REVERSE, |
7986 | ARG_AFTER, | |
7987 | ARG_BEFORE, | |
1ae17672 | 7988 | ARG_DRY_RUN, |
991f2a39 | 7989 | ARG_SHOW_TYPES, |
23ade460 | 7990 | ARG_IRREVERSIBLE, |
e67c3609 | 7991 | ARG_IGNORE_DEPENDENCIES, |
4f9a9105 | 7992 | ARG_VALUE, |
35df8f27 | 7993 | ARG_VERSION, |
af2d49f7 | 7994 | ARG_USER, |
7e4249b9 | 7995 | ARG_SYSTEM, |
ee5762e3 | 7996 | ARG_GLOBAL, |
6e905d93 | 7997 | ARG_NO_BLOCK, |
ebed32bf | 7998 | ARG_NO_LEGEND, |
611efaac | 7999 | ARG_NO_PAGER, |
4445a875 | 8000 | ARG_NO_WALL, |
be394c48 | 8001 | ARG_ROOT, |
ee5762e3 | 8002 | ARG_NO_RELOAD, |
501fc174 | 8003 | ARG_KILL_WHO, |
30732560 | 8004 | ARG_NO_ASK_PASSWORD, |
729e3769 | 8005 | ARG_FAILED, |
df50185b | 8006 | ARG_RUNTIME, |
9b9b3d36 | 8007 | ARG_PLAIN, |
4dc5b821 | 8008 | ARG_STATE, |
d309c1c3 LP |
8009 | ARG_JOB_MODE, |
8010 | ARG_PRESET_MODE, | |
5bdf2243 | 8011 | ARG_FIRMWARE_SETUP, |
97af80c5 LP |
8012 | ARG_BOOT_LOADER_MENU, |
8013 | ARG_BOOT_LOADER_ENTRY, | |
57ab2eab | 8014 | ARG_NOW, |
9ef15026 | 8015 | ARG_MESSAGE, |
e9c387c8 | 8016 | ARG_WITH_DEPENDENCIES, |
93a08841 | 8017 | ARG_WAIT, |
d81bc51f | 8018 | ARG_WHAT, |
dae710be | 8019 | ARG_REBOOT_ARG, |
7e4249b9 LP |
8020 | }; |
8021 | ||
8022 | static const struct option options[] = { | |
9ea9d4cf LP |
8023 | { "help", no_argument, NULL, 'h' }, |
8024 | { "version", no_argument, NULL, ARG_VERSION }, | |
8025 | { "type", required_argument, NULL, 't' }, | |
8026 | { "property", required_argument, NULL, 'p' }, | |
8027 | { "all", no_argument, NULL, 'a' }, | |
8028 | { "reverse", no_argument, NULL, ARG_REVERSE }, | |
8029 | { "after", no_argument, NULL, ARG_AFTER }, | |
8030 | { "before", no_argument, NULL, ARG_BEFORE }, | |
8031 | { "show-types", no_argument, NULL, ARG_SHOW_TYPES }, | |
8032 | { "failed", no_argument, NULL, ARG_FAILED }, /* compatibility only */ | |
8033 | { "full", no_argument, NULL, 'l' }, | |
4dc5b821 LP |
8034 | { "job-mode", required_argument, NULL, ARG_JOB_MODE }, |
8035 | { "fail", no_argument, NULL, ARG_FAIL }, /* compatibility only */ | |
8036 | { "irreversible", no_argument, NULL, ARG_IRREVERSIBLE }, /* compatibility only */ | |
8037 | { "ignore-dependencies", no_argument, NULL, ARG_IGNORE_DEPENDENCIES }, /* compatibility only */ | |
9ea9d4cf | 8038 | { "ignore-inhibitors", no_argument, NULL, 'i' }, |
4f9a9105 | 8039 | { "value", no_argument, NULL, ARG_VALUE }, |
9ea9d4cf LP |
8040 | { "user", no_argument, NULL, ARG_USER }, |
8041 | { "system", no_argument, NULL, ARG_SYSTEM }, | |
8042 | { "global", no_argument, NULL, ARG_GLOBAL }, | |
93a08841 | 8043 | { "wait", no_argument, NULL, ARG_WAIT }, |
9ea9d4cf LP |
8044 | { "no-block", no_argument, NULL, ARG_NO_BLOCK }, |
8045 | { "no-legend", no_argument, NULL, ARG_NO_LEGEND }, | |
8046 | { "no-pager", no_argument, NULL, ARG_NO_PAGER }, | |
8047 | { "no-wall", no_argument, NULL, ARG_NO_WALL }, | |
1ae17672 | 8048 | { "dry-run", no_argument, NULL, ARG_DRY_RUN }, |
9ea9d4cf LP |
8049 | { "quiet", no_argument, NULL, 'q' }, |
8050 | { "root", required_argument, NULL, ARG_ROOT }, | |
68da321f | 8051 | { "force", no_argument, NULL, 'f' }, |
9ea9d4cf LP |
8052 | { "no-reload", no_argument, NULL, ARG_NO_RELOAD }, |
8053 | { "kill-who", required_argument, NULL, ARG_KILL_WHO }, | |
8054 | { "signal", required_argument, NULL, 's' }, | |
8055 | { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD }, | |
8056 | { "host", required_argument, NULL, 'H' }, | |
f459b602 | 8057 | { "machine", required_argument, NULL, 'M' }, |
9ea9d4cf LP |
8058 | { "runtime", no_argument, NULL, ARG_RUNTIME }, |
8059 | { "lines", required_argument, NULL, 'n' }, | |
8060 | { "output", required_argument, NULL, 'o' }, | |
8061 | { "plain", no_argument, NULL, ARG_PLAIN }, | |
8062 | { "state", required_argument, NULL, ARG_STATE }, | |
1238ee09 | 8063 | { "recursive", no_argument, NULL, 'r' }, |
e9c387c8 | 8064 | { "with-dependencies", no_argument, NULL, ARG_WITH_DEPENDENCIES }, |
d309c1c3 | 8065 | { "preset-mode", required_argument, NULL, ARG_PRESET_MODE }, |
5bdf2243 | 8066 | { "firmware-setup", no_argument, NULL, ARG_FIRMWARE_SETUP }, |
97af80c5 LP |
8067 | { "boot-loader-menu", required_argument, NULL, ARG_BOOT_LOADER_MENU }, |
8068 | { "boot-loader-entry", required_argument, NULL, ARG_BOOT_LOADER_ENTRY }, | |
57ab2eab | 8069 | { "now", no_argument, NULL, ARG_NOW }, |
9ef15026 | 8070 | { "message", required_argument, NULL, ARG_MESSAGE }, |
85d9b598 | 8071 | { "show-transaction", no_argument, NULL, 'T' }, |
d81bc51f | 8072 | { "what", required_argument, NULL, ARG_WHAT }, |
dae710be | 8073 | { "reboot-argument", required_argument, NULL, ARG_REBOOT_ARG }, |
eb9da376 | 8074 | {} |
7e4249b9 LP |
8075 | }; |
8076 | ||
0f03c2a4 | 8077 | int c, r; |
7e4249b9 | 8078 | |
e4b61340 | 8079 | assert(argc >= 0); |
7e4249b9 LP |
8080 | assert(argv); |
8081 | ||
16f017fa IS |
8082 | /* we default to allowing interactive authorization only in systemctl (not in the legacy commands) */ |
8083 | arg_ask_password = true; | |
8084 | ||
5292c240 | 8085 | while ((c = getopt_long(argc, argv, "ht:p:P:alqfs:H:M:n:o:iTr.::", options, NULL)) >= 0) |
7e4249b9 LP |
8086 | |
8087 | switch (c) { | |
8088 | ||
8089 | case 'h': | |
37ec0fdd | 8090 | return systemctl_help(); |
35df8f27 LP |
8091 | |
8092 | case ARG_VERSION: | |
3f6fd1ba | 8093 | return version(); |
7e4249b9 | 8094 | |
20b3f379 | 8095 | case 't': { |
fb1ac50e LP |
8096 | const char *p; |
8097 | ||
baaa35ad ZJS |
8098 | if (isempty(optarg)) |
8099 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8100 | "--type= requires arguments."); | |
45c0c61d | 8101 | |
c58bd76a | 8102 | for (p = optarg;;) { |
5ab22f33 | 8103 | _cleanup_free_ char *type = NULL; |
20b3f379 | 8104 | |
5ab22f33 SS |
8105 | r = extract_first_word(&p, &type, ",", 0); |
8106 | if (r < 0) | |
8107 | return log_error_errno(r, "Failed to parse type: %s", optarg); | |
5ab22f33 SS |
8108 | if (r == 0) |
8109 | break; | |
20b3f379 ZJS |
8110 | |
8111 | if (streq(type, "help")) { | |
8112 | help_types(); | |
8113 | return 0; | |
8114 | } | |
8115 | ||
8116 | if (unit_type_from_string(type) >= 0) { | |
96e895ef | 8117 | if (strv_consume(&arg_types, TAKE_PTR(type)) < 0) |
20b3f379 | 8118 | return log_oom(); |
20b3f379 ZJS |
8119 | continue; |
8120 | } | |
8121 | ||
9b9b3d36 MW |
8122 | /* It's much nicer to use --state= for |
8123 | * load states, but let's support this | |
8124 | * in --types= too for compatibility | |
8125 | * with old versions */ | |
7e9d36e0 | 8126 | if (unit_load_state_from_string(type) >= 0) { |
96e895ef | 8127 | if (strv_consume(&arg_states, TAKE_PTR(type)) < 0) |
20b3f379 | 8128 | return log_oom(); |
20b3f379 ZJS |
8129 | continue; |
8130 | } | |
8131 | ||
ab06eef8 | 8132 | log_error("Unknown unit type or load state '%s'.", type); |
baaa35ad ZJS |
8133 | return log_info_errno(SYNTHETIC_ERRNO(EINVAL), |
8134 | "Use -t help to see a list of allowed values."); | |
c147dc42 | 8135 | } |
20b3f379 ZJS |
8136 | |
8137 | break; | |
8138 | } | |
8139 | ||
5292c240 ZJS |
8140 | case 'P': |
8141 | arg_value = true; | |
8142 | _fallthrough_; | |
8143 | ||
aee7082f LP |
8144 | case 'p': |
8145 | /* Make sure that if the empty property list was specified, we won't show any | |
8146 | properties. */ | |
20b3f379 | 8147 | if (isempty(optarg) && !arg_properties) { |
cbc9fbd1 | 8148 | arg_properties = new0(char*, 1); |
20b3f379 ZJS |
8149 | if (!arg_properties) |
8150 | return log_oom(); | |
fb1ac50e LP |
8151 | } else { |
8152 | const char *p; | |
8153 | ||
c58bd76a | 8154 | for (p = optarg;;) { |
5ab22f33 | 8155 | _cleanup_free_ char *prop = NULL; |
033a842c | 8156 | |
5ab22f33 SS |
8157 | r = extract_first_word(&p, &prop, ",", 0); |
8158 | if (r < 0) | |
8159 | return log_error_errno(r, "Failed to parse property: %s", optarg); | |
5ab22f33 SS |
8160 | if (r == 0) |
8161 | break; | |
ea4a240d | 8162 | |
9dccc6d3 | 8163 | if (strv_consume(&arg_properties, TAKE_PTR(prop)) < 0) |
20b3f379 | 8164 | return log_oom(); |
20b3f379 | 8165 | } |
fb1ac50e | 8166 | } |
48220598 | 8167 | |
5292c240 | 8168 | /* If the user asked for a particular property, show it, even if it is empty. */ |
48220598 | 8169 | arg_all = true; |
033a842c | 8170 | |
48220598 LP |
8171 | break; |
8172 | ||
7e4249b9 LP |
8173 | case 'a': |
8174 | arg_all = true; | |
8175 | break; | |
8176 | ||
afba4199 ZJS |
8177 | case ARG_REVERSE: |
8178 | arg_dependency = DEPENDENCY_REVERSE; | |
8179 | break; | |
8180 | ||
8181 | case ARG_AFTER: | |
8182 | arg_dependency = DEPENDENCY_AFTER; | |
82948f6c | 8183 | arg_jobs_after = true; |
afba4199 ZJS |
8184 | break; |
8185 | ||
8186 | case ARG_BEFORE: | |
8187 | arg_dependency = DEPENDENCY_BEFORE; | |
82948f6c | 8188 | arg_jobs_before = true; |
afba4199 ZJS |
8189 | break; |
8190 | ||
991f2a39 ZJS |
8191 | case ARG_SHOW_TYPES: |
8192 | arg_show_types = true; | |
8193 | break; | |
8194 | ||
4f9a9105 ZJS |
8195 | case ARG_VALUE: |
8196 | arg_value = true; | |
8197 | break; | |
8198 | ||
4dc5b821 LP |
8199 | case ARG_JOB_MODE: |
8200 | arg_job_mode = optarg; | |
8201 | break; | |
8202 | ||
90d473a1 | 8203 | case ARG_FAIL: |
e67c3609 LP |
8204 | arg_job_mode = "fail"; |
8205 | break; | |
8206 | ||
23ade460 MS |
8207 | case ARG_IRREVERSIBLE: |
8208 | arg_job_mode = "replace-irreversibly"; | |
8209 | break; | |
8210 | ||
e67c3609 LP |
8211 | case ARG_IGNORE_DEPENDENCIES: |
8212 | arg_job_mode = "ignore-dependencies"; | |
7e4249b9 LP |
8213 | break; |
8214 | ||
af2d49f7 | 8215 | case ARG_USER: |
729e3769 | 8216 | arg_scope = UNIT_FILE_USER; |
7e4249b9 LP |
8217 | break; |
8218 | ||
8219 | case ARG_SYSTEM: | |
729e3769 LP |
8220 | arg_scope = UNIT_FILE_SYSTEM; |
8221 | break; | |
8222 | ||
8223 | case ARG_GLOBAL: | |
8224 | arg_scope = UNIT_FILE_GLOBAL; | |
7e4249b9 LP |
8225 | break; |
8226 | ||
93a08841 MP |
8227 | case ARG_WAIT: |
8228 | arg_wait = true; | |
8229 | break; | |
8230 | ||
6e905d93 LP |
8231 | case ARG_NO_BLOCK: |
8232 | arg_no_block = true; | |
7e4249b9 LP |
8233 | break; |
8234 | ||
ebed32bf MS |
8235 | case ARG_NO_LEGEND: |
8236 | arg_no_legend = true; | |
8237 | break; | |
8238 | ||
611efaac | 8239 | case ARG_NO_PAGER: |
0221d68a | 8240 | arg_pager_flags |= PAGER_DISABLE; |
611efaac | 8241 | break; |
0736af98 | 8242 | |
514f4ef5 LP |
8243 | case ARG_NO_WALL: |
8244 | arg_no_wall = true; | |
8245 | break; | |
8246 | ||
be394c48 | 8247 | case ARG_ROOT: |
bac75eb3 | 8248 | r = parse_path_argument_and_warn(optarg, false, &arg_root); |
0f03c2a4 LP |
8249 | if (r < 0) |
8250 | return r; | |
be394c48 FC |
8251 | break; |
8252 | ||
98a6e132 | 8253 | case 'l': |
8fe914ec LP |
8254 | arg_full = true; |
8255 | break; | |
8256 | ||
30732560 | 8257 | case ARG_FAILED: |
9b9b3d36 MW |
8258 | if (strv_extend(&arg_states, "failed") < 0) |
8259 | return log_oom(); | |
8260 | ||
30732560 LP |
8261 | break; |
8262 | ||
1ae17672 ZJS |
8263 | case ARG_DRY_RUN: |
8264 | arg_dry_run = true; | |
8265 | break; | |
8266 | ||
0183528f LP |
8267 | case 'q': |
8268 | arg_quiet = true; | |
8269 | break; | |
8270 | ||
b4f27ccc | 8271 | case 'f': |
313cefa1 | 8272 | arg_force++; |
ee5762e3 LP |
8273 | break; |
8274 | ||
8275 | case ARG_NO_RELOAD: | |
8276 | arg_no_reload = true; | |
8277 | break; | |
8278 | ||
8a0867d6 LP |
8279 | case ARG_KILL_WHO: |
8280 | arg_kill_who = optarg; | |
8281 | break; | |
8282 | ||
8a0867d6 | 8283 | case 's': |
5c828e66 LP |
8284 | if (streq(optarg, "help")) { |
8285 | DUMP_STRING_TABLE(signal, int, _NSIG); | |
8286 | return 0; | |
8287 | } | |
8288 | ||
29a3db75 | 8289 | arg_signal = signal_from_string(optarg); |
baaa35ad ZJS |
8290 | if (arg_signal < 0) |
8291 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8292 | "Failed to parse signal string %s.", | |
8293 | optarg); | |
8a0867d6 LP |
8294 | break; |
8295 | ||
501fc174 LP |
8296 | case ARG_NO_ASK_PASSWORD: |
8297 | arg_ask_password = false; | |
8298 | break; | |
8299 | ||
f459b602 MAP |
8300 | case 'H': |
8301 | arg_transport = BUS_TRANSPORT_REMOTE; | |
8302 | arg_host = optarg; | |
a8f11321 LP |
8303 | break; |
8304 | ||
f459b602 | 8305 | case 'M': |
de33fc62 | 8306 | arg_transport = BUS_TRANSPORT_MACHINE; |
f459b602 | 8307 | arg_host = optarg; |
a8f11321 LP |
8308 | break; |
8309 | ||
729e3769 LP |
8310 | case ARG_RUNTIME: |
8311 | arg_runtime = true; | |
8312 | break; | |
8313 | ||
df50185b | 8314 | case 'n': |
baaa35ad ZJS |
8315 | if (safe_atou(optarg, &arg_lines) < 0) |
8316 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8317 | "Failed to parse lines '%s'", | |
8318 | optarg); | |
df50185b LP |
8319 | break; |
8320 | ||
df50185b | 8321 | case 'o': |
5c828e66 LP |
8322 | if (streq(optarg, "help")) { |
8323 | DUMP_STRING_TABLE(output_mode, OutputMode, _OUTPUT_MODE_MAX); | |
8324 | return 0; | |
8325 | } | |
8326 | ||
df50185b | 8327 | arg_output = output_mode_from_string(optarg); |
baaa35ad ZJS |
8328 | if (arg_output < 0) |
8329 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8330 | "Unknown output '%s'.", | |
8331 | optarg); | |
49cd06fa LP |
8332 | |
8333 | if (OUTPUT_MODE_IS_JSON(arg_output)) { | |
8334 | arg_no_legend = true; | |
8335 | arg_plain = true; | |
8336 | } | |
df50185b LP |
8337 | break; |
8338 | ||
b37844d3 LP |
8339 | case 'i': |
8340 | arg_ignore_inhibitors = true; | |
8341 | break; | |
8342 | ||
5d0c05e5 LN |
8343 | case ARG_PLAIN: |
8344 | arg_plain = true; | |
8345 | break; | |
8346 | ||
5bdf2243 JJ |
8347 | case ARG_FIRMWARE_SETUP: |
8348 | arg_firmware_setup = true; | |
8349 | break; | |
8350 | ||
97af80c5 LP |
8351 | case ARG_BOOT_LOADER_MENU: |
8352 | ||
8353 | r = parse_sec(optarg, &arg_boot_loader_menu); | |
8354 | if (r < 0) | |
8355 | return log_error_errno(r, "Failed to parse --boot-loader-menu= argument '%s': %m", optarg); | |
8356 | ||
8357 | break; | |
8358 | ||
8359 | case ARG_BOOT_LOADER_ENTRY: | |
8360 | ||
8361 | if (streq(optarg, "help")) { /* Yes, this means, "help" is not a valid boot loader entry name we can deal with */ | |
8362 | r = help_boot_loader_entry(); | |
8363 | if (r < 0) | |
8364 | return r; | |
8365 | ||
8366 | return 0; | |
8367 | } | |
8368 | ||
8369 | arg_boot_loader_entry = empty_to_null(optarg); | |
8370 | break; | |
8371 | ||
9b9b3d36 | 8372 | case ARG_STATE: { |
fb1ac50e LP |
8373 | const char *p; |
8374 | ||
baaa35ad ZJS |
8375 | if (isempty(optarg)) |
8376 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8377 | "--state= requires arguments."); | |
9b9b3d36 | 8378 | |
c58bd76a | 8379 | for (p = optarg;;) { |
bc6c18fe | 8380 | _cleanup_free_ char *s = NULL; |
9b9b3d36 | 8381 | |
5ab22f33 SS |
8382 | r = extract_first_word(&p, &s, ",", 0); |
8383 | if (r < 0) | |
6862111e | 8384 | return log_error_errno(r, "Failed to parse state: %s", optarg); |
5ab22f33 SS |
8385 | if (r == 0) |
8386 | break; | |
9b9b3d36 | 8387 | |
e16972e6 ZJS |
8388 | if (streq(s, "help")) { |
8389 | help_states(); | |
8390 | return 0; | |
8391 | } | |
8392 | ||
9dccc6d3 | 8393 | if (strv_consume(&arg_states, TAKE_PTR(s)) < 0) |
9b9b3d36 | 8394 | return log_oom(); |
9b9b3d36 MW |
8395 | } |
8396 | break; | |
8397 | } | |
8398 | ||
1238ee09 | 8399 | case 'r': |
baaa35ad ZJS |
8400 | if (geteuid() != 0) |
8401 | return log_error_errno(SYNTHETIC_ERRNO(EPERM), | |
8402 | "--recursive requires root privileges."); | |
1238ee09 LP |
8403 | |
8404 | arg_recursive = true; | |
8405 | break; | |
8406 | ||
d309c1c3 | 8407 | case ARG_PRESET_MODE: |
5c828e66 LP |
8408 | if (streq(optarg, "help")) { |
8409 | DUMP_STRING_TABLE(unit_file_preset_mode, UnitFilePresetMode, _UNIT_FILE_PRESET_MAX); | |
8410 | return 0; | |
8411 | } | |
d309c1c3 LP |
8412 | |
8413 | arg_preset_mode = unit_file_preset_mode_from_string(optarg); | |
baaa35ad ZJS |
8414 | if (arg_preset_mode < 0) |
8415 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8416 | "Failed to parse preset mode: %s.", optarg); | |
d309c1c3 LP |
8417 | |
8418 | break; | |
8419 | ||
57ab2eab JS |
8420 | case ARG_NOW: |
8421 | arg_now = true; | |
8422 | break; | |
8423 | ||
9ef15026 JS |
8424 | case ARG_MESSAGE: |
8425 | if (strv_extend(&arg_wall, optarg) < 0) | |
8426 | return log_oom(); | |
8427 | break; | |
8428 | ||
85d9b598 LP |
8429 | case 'T': |
8430 | arg_show_transaction = true; | |
8431 | break; | |
8432 | ||
e9c387c8 KK |
8433 | case ARG_WITH_DEPENDENCIES: |
8434 | arg_with_dependencies = true; | |
8435 | break; | |
8436 | ||
d81bc51f LP |
8437 | case ARG_WHAT: { |
8438 | const char *p; | |
8439 | ||
8440 | if (isempty(optarg)) | |
8441 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--what= requires arguments."); | |
8442 | ||
8443 | for (p = optarg;;) { | |
8444 | _cleanup_free_ char *k = NULL; | |
8445 | ||
8446 | r = extract_first_word(&p, &k, ",", 0); | |
8447 | if (r < 0) | |
8448 | return log_error_errno(r, "Failed to parse directory type: %s", optarg); | |
8449 | if (r == 0) | |
8450 | break; | |
8451 | ||
8452 | if (streq(k, "help")) { | |
8453 | puts("runtime\n" | |
8454 | "state\n" | |
8455 | "cache\n" | |
8456 | "logs\n" | |
8457 | "configuration"); | |
8458 | return 0; | |
8459 | } | |
8460 | ||
8461 | r = strv_consume(&arg_clean_what, TAKE_PTR(k)); | |
8462 | if (r < 0) | |
8463 | return log_oom(); | |
8464 | } | |
8465 | ||
8466 | break; | |
8467 | } | |
8468 | ||
dae710be | 8469 | case ARG_REBOOT_ARG: |
8470 | arg_reboot_argument = optarg; | |
8471 | break; | |
8472 | ||
2e7e19ca ZJS |
8473 | case '.': |
8474 | /* Output an error mimicking getopt, and print a hint afterwards */ | |
8475 | log_error("%s: invalid option -- '.'", program_invocation_name); | |
8476 | log_notice("Hint: to specify units starting with a dash, use \"--\":\n" | |
353b2baa | 8477 | " %s [OPTIONS...] COMMAND -- -.%s ...", |
2e7e19ca ZJS |
8478 | program_invocation_name, optarg ?: "mount"); |
8479 | _fallthrough_; | |
8480 | ||
7e4249b9 LP |
8481 | case '?': |
8482 | return -EINVAL; | |
8483 | ||
8484 | default: | |
eb9da376 | 8485 | assert_not_reached("Unhandled option"); |
7e4249b9 | 8486 | } |
7e4249b9 | 8487 | |
baaa35ad ZJS |
8488 | if (arg_transport != BUS_TRANSPORT_LOCAL && arg_scope != UNIT_FILE_SYSTEM) |
8489 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8490 | "Cannot access user instance remotely."); | |
a8f11321 | 8491 | |
baaa35ad ZJS |
8492 | if (arg_wait && arg_no_block) |
8493 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8494 | "--wait may not be combined with --no-block."); | |
93a08841 | 8495 | |
7e4249b9 LP |
8496 | return 1; |
8497 | } | |
8498 | ||
e4b61340 | 8499 | static int halt_parse_argv(int argc, char *argv[]) { |
e4b61340 LP |
8500 | enum { |
8501 | ARG_HELP = 0x100, | |
8502 | ARG_HALT, | |
514f4ef5 LP |
8503 | ARG_REBOOT, |
8504 | ARG_NO_WALL | |
e4b61340 LP |
8505 | }; |
8506 | ||
8507 | static const struct option options[] = { | |
8508 | { "help", no_argument, NULL, ARG_HELP }, | |
8509 | { "halt", no_argument, NULL, ARG_HALT }, | |
8510 | { "poweroff", no_argument, NULL, 'p' }, | |
8511 | { "reboot", no_argument, NULL, ARG_REBOOT }, | |
8512 | { "force", no_argument, NULL, 'f' }, | |
8513 | { "wtmp-only", no_argument, NULL, 'w' }, | |
8514 | { "no-wtmp", no_argument, NULL, 'd' }, | |
f3f054f0 | 8515 | { "no-sync", no_argument, NULL, 'n' }, |
514f4ef5 | 8516 | { "no-wall", no_argument, NULL, ARG_NO_WALL }, |
eb9da376 | 8517 | {} |
e4b61340 LP |
8518 | }; |
8519 | ||
37185ec8 | 8520 | int c, r, runlevel; |
e4b61340 LP |
8521 | |
8522 | assert(argc >= 0); | |
8523 | assert(argv); | |
8524 | ||
8525 | if (utmp_get_runlevel(&runlevel, NULL) >= 0) | |
4c701096 | 8526 | if (IN_SET(runlevel, '0', '6')) |
65491fd8 | 8527 | arg_force = 2; |
e4b61340 | 8528 | |
601185b4 | 8529 | while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) |
e4b61340 LP |
8530 | switch (c) { |
8531 | ||
8532 | case ARG_HELP: | |
37ec0fdd | 8533 | return halt_help(); |
e4b61340 LP |
8534 | |
8535 | case ARG_HALT: | |
8536 | arg_action = ACTION_HALT; | |
8537 | break; | |
8538 | ||
8539 | case 'p': | |
a042efad MS |
8540 | if (arg_action != ACTION_REBOOT) |
8541 | arg_action = ACTION_POWEROFF; | |
e4b61340 LP |
8542 | break; |
8543 | ||
8544 | case ARG_REBOOT: | |
8545 | arg_action = ACTION_REBOOT; | |
8546 | break; | |
8547 | ||
8548 | case 'f': | |
65491fd8 | 8549 | arg_force = 2; |
e4b61340 LP |
8550 | break; |
8551 | ||
8552 | case 'w': | |
1ae17672 | 8553 | arg_dry_run = true; |
e4b61340 LP |
8554 | break; |
8555 | ||
8556 | case 'd': | |
8557 | arg_no_wtmp = true; | |
8558 | break; | |
8559 | ||
f3f054f0 TB |
8560 | case 'n': |
8561 | arg_no_sync = true; | |
8562 | break; | |
8563 | ||
514f4ef5 LP |
8564 | case ARG_NO_WALL: |
8565 | arg_no_wall = true; | |
8566 | break; | |
8567 | ||
e4b61340 LP |
8568 | case 'i': |
8569 | case 'h': | |
8570 | /* Compatibility nops */ | |
8571 | break; | |
8572 | ||
8573 | case '?': | |
8574 | return -EINVAL; | |
8575 | ||
8576 | default: | |
eb9da376 | 8577 | assert_not_reached("Unhandled option"); |
e4b61340 | 8578 | } |
e4b61340 | 8579 | |
c5220a94 | 8580 | if (arg_action == ACTION_REBOOT && (argc == optind || argc == optind + 1)) { |
77defcf5 | 8581 | r = update_reboot_parameter_and_warn(argc == optind + 1 ? argv[optind] : NULL, false); |
c5220a94 | 8582 | if (r < 0) |
37185ec8 | 8583 | return r; |
baaa35ad ZJS |
8584 | } else if (optind < argc) |
8585 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8586 | "Too many arguments."); | |
e4b61340 LP |
8587 | |
8588 | return 1; | |
8589 | } | |
8590 | ||
8591 | static int shutdown_parse_argv(int argc, char *argv[]) { | |
e4b61340 LP |
8592 | enum { |
8593 | ARG_HELP = 0x100, | |
514f4ef5 | 8594 | ARG_NO_WALL |
e4b61340 LP |
8595 | }; |
8596 | ||
8597 | static const struct option options[] = { | |
8598 | { "help", no_argument, NULL, ARG_HELP }, | |
8599 | { "halt", no_argument, NULL, 'H' }, | |
8600 | { "poweroff", no_argument, NULL, 'P' }, | |
8601 | { "reboot", no_argument, NULL, 'r' }, | |
04ebb595 | 8602 | { "kexec", no_argument, NULL, 'K' }, /* not documented extension */ |
514f4ef5 | 8603 | { "no-wall", no_argument, NULL, ARG_NO_WALL }, |
eb9da376 | 8604 | {} |
e4b61340 LP |
8605 | }; |
8606 | ||
172d7abf | 8607 | char **wall = NULL; |
f6144808 | 8608 | int c, r; |
e4b61340 LP |
8609 | |
8610 | assert(argc >= 0); | |
8611 | assert(argv); | |
8612 | ||
b068c6f5 | 8613 | while ((c = getopt_long(argc, argv, "HPrhkKat:fFc", options, NULL)) >= 0) |
e4b61340 LP |
8614 | switch (c) { |
8615 | ||
8616 | case ARG_HELP: | |
37ec0fdd | 8617 | return shutdown_help(); |
e4b61340 LP |
8618 | |
8619 | case 'H': | |
8620 | arg_action = ACTION_HALT; | |
8621 | break; | |
8622 | ||
8623 | case 'P': | |
8624 | arg_action = ACTION_POWEROFF; | |
8625 | break; | |
8626 | ||
8627 | case 'r': | |
5622dde3 KS |
8628 | if (kexec_loaded()) |
8629 | arg_action = ACTION_KEXEC; | |
8630 | else | |
8631 | arg_action = ACTION_REBOOT; | |
e4b61340 LP |
8632 | break; |
8633 | ||
04ebb595 LP |
8634 | case 'K': |
8635 | arg_action = ACTION_KEXEC; | |
8636 | break; | |
8637 | ||
e4b61340 LP |
8638 | case 'h': |
8639 | if (arg_action != ACTION_HALT) | |
8640 | arg_action = ACTION_POWEROFF; | |
8641 | break; | |
8642 | ||
8643 | case 'k': | |
1ae17672 | 8644 | arg_dry_run = true; |
e4b61340 LP |
8645 | break; |
8646 | ||
514f4ef5 LP |
8647 | case ARG_NO_WALL: |
8648 | arg_no_wall = true; | |
8649 | break; | |
8650 | ||
e4b61340 | 8651 | case 'a': |
b068c6f5 | 8652 | case 't': /* Note that we also ignore any passed argument to -t, not just the -t itself */ |
75836b9d JS |
8653 | case 'f': |
8654 | case 'F': | |
e4b61340 LP |
8655 | /* Compatibility nops */ |
8656 | break; | |
8657 | ||
f6144808 LP |
8658 | case 'c': |
8659 | arg_action = ACTION_CANCEL_SHUTDOWN; | |
8660 | break; | |
8661 | ||
e4b61340 LP |
8662 | case '?': |
8663 | return -EINVAL; | |
8664 | ||
8665 | default: | |
eb9da376 | 8666 | assert_not_reached("Unhandled option"); |
e4b61340 | 8667 | } |
e4b61340 | 8668 | |
dfcc5c33 | 8669 | if (argc > optind && arg_action != ACTION_CANCEL_SHUTDOWN) { |
2cc7b0a2 | 8670 | r = parse_shutdown_time_spec(argv[optind], &arg_when); |
7e59bfcb | 8671 | if (r < 0) { |
f6144808 LP |
8672 | log_error("Failed to parse time specification: %s", argv[optind]); |
8673 | return r; | |
8674 | } | |
6b5ad000 | 8675 | } else |
08e4b1c5 | 8676 | arg_when = now(CLOCK_REALTIME) + USEC_PER_MINUTE; |
442b9094 | 8677 | |
dfcc5c33 MS |
8678 | if (argc > optind && arg_action == ACTION_CANCEL_SHUTDOWN) |
8679 | /* No time argument for shutdown cancel */ | |
172d7abf | 8680 | wall = argv + optind; |
dfcc5c33 MS |
8681 | else if (argc > optind + 1) |
8682 | /* We skip the time argument */ | |
172d7abf LP |
8683 | wall = argv + optind + 1; |
8684 | ||
8685 | if (wall) { | |
8686 | arg_wall = strv_copy(wall); | |
8687 | if (!arg_wall) | |
8688 | return log_oom(); | |
8689 | } | |
e4b61340 LP |
8690 | |
8691 | optind = argc; | |
8692 | ||
8693 | return 1; | |
e4b61340 LP |
8694 | } |
8695 | ||
8696 | static int telinit_parse_argv(int argc, char *argv[]) { | |
e4b61340 LP |
8697 | enum { |
8698 | ARG_HELP = 0x100, | |
514f4ef5 | 8699 | ARG_NO_WALL |
e4b61340 LP |
8700 | }; |
8701 | ||
8702 | static const struct option options[] = { | |
8703 | { "help", no_argument, NULL, ARG_HELP }, | |
514f4ef5 | 8704 | { "no-wall", no_argument, NULL, ARG_NO_WALL }, |
eb9da376 | 8705 | {} |
e4b61340 LP |
8706 | }; |
8707 | ||
8708 | static const struct { | |
8709 | char from; | |
8710 | enum action to; | |
8711 | } table[] = { | |
8712 | { '0', ACTION_POWEROFF }, | |
8713 | { '6', ACTION_REBOOT }, | |
ef2f1067 | 8714 | { '1', ACTION_RESCUE }, |
e4b61340 LP |
8715 | { '2', ACTION_RUNLEVEL2 }, |
8716 | { '3', ACTION_RUNLEVEL3 }, | |
8717 | { '4', ACTION_RUNLEVEL4 }, | |
8718 | { '5', ACTION_RUNLEVEL5 }, | |
8719 | { 's', ACTION_RESCUE }, | |
8720 | { 'S', ACTION_RESCUE }, | |
8721 | { 'q', ACTION_RELOAD }, | |
8722 | { 'Q', ACTION_RELOAD }, | |
8723 | { 'u', ACTION_REEXEC }, | |
8724 | { 'U', ACTION_REEXEC } | |
8725 | }; | |
8726 | ||
8727 | unsigned i; | |
8728 | int c; | |
8729 | ||
8730 | assert(argc >= 0); | |
8731 | assert(argv); | |
8732 | ||
601185b4 | 8733 | while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) |
e4b61340 LP |
8734 | switch (c) { |
8735 | ||
8736 | case ARG_HELP: | |
37ec0fdd | 8737 | return telinit_help(); |
e4b61340 | 8738 | |
514f4ef5 LP |
8739 | case ARG_NO_WALL: |
8740 | arg_no_wall = true; | |
8741 | break; | |
8742 | ||
e4b61340 LP |
8743 | case '?': |
8744 | return -EINVAL; | |
8745 | ||
8746 | default: | |
eb9da376 | 8747 | assert_not_reached("Unhandled option"); |
e4b61340 | 8748 | } |
e4b61340 | 8749 | |
baaa35ad ZJS |
8750 | if (optind >= argc) |
8751 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8752 | "%s: required argument missing.", | |
8753 | program_invocation_short_name); | |
e4b61340 | 8754 | |
baaa35ad ZJS |
8755 | if (optind + 1 < argc) |
8756 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8757 | "Too many arguments."); | |
e4b61340 | 8758 | |
baaa35ad ZJS |
8759 | if (strlen(argv[optind]) != 1) |
8760 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8761 | "Expected single character argument."); | |
e4b61340 LP |
8762 | |
8763 | for (i = 0; i < ELEMENTSOF(table); i++) | |
8764 | if (table[i].from == argv[optind][0]) | |
8765 | break; | |
8766 | ||
baaa35ad ZJS |
8767 | if (i >= ELEMENTSOF(table)) |
8768 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8769 | "Unknown command '%s'.", argv[optind]); | |
e4b61340 LP |
8770 | |
8771 | arg_action = table[i].to; | |
8772 | ||
313cefa1 | 8773 | optind++; |
e4b61340 LP |
8774 | |
8775 | return 1; | |
8776 | } | |
8777 | ||
8778 | static int runlevel_parse_argv(int argc, char *argv[]) { | |
e4b61340 LP |
8779 | enum { |
8780 | ARG_HELP = 0x100, | |
8781 | }; | |
8782 | ||
8783 | static const struct option options[] = { | |
8784 | { "help", no_argument, NULL, ARG_HELP }, | |
eb9da376 | 8785 | {} |
e4b61340 LP |
8786 | }; |
8787 | ||
8788 | int c; | |
8789 | ||
8790 | assert(argc >= 0); | |
8791 | assert(argv); | |
8792 | ||
601185b4 | 8793 | while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) |
e4b61340 LP |
8794 | switch (c) { |
8795 | ||
8796 | case ARG_HELP: | |
37ec0fdd | 8797 | return runlevel_help(); |
e4b61340 LP |
8798 | |
8799 | case '?': | |
8800 | return -EINVAL; | |
8801 | ||
8802 | default: | |
eb9da376 | 8803 | assert_not_reached("Unhandled option"); |
e4b61340 | 8804 | } |
e4b61340 | 8805 | |
baaa35ad ZJS |
8806 | if (optind < argc) |
8807 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8808 | "Too many arguments."); | |
e4b61340 LP |
8809 | |
8810 | return 1; | |
8811 | } | |
8812 | ||
8813 | static int parse_argv(int argc, char *argv[]) { | |
8814 | assert(argc >= 0); | |
8815 | assert(argv); | |
8816 | ||
8817 | if (program_invocation_short_name) { | |
8818 | ||
8819 | if (strstr(program_invocation_short_name, "halt")) { | |
8820 | arg_action = ACTION_HALT; | |
8821 | return halt_parse_argv(argc, argv); | |
c54819ca | 8822 | |
e4b61340 LP |
8823 | } else if (strstr(program_invocation_short_name, "poweroff")) { |
8824 | arg_action = ACTION_POWEROFF; | |
8825 | return halt_parse_argv(argc, argv); | |
c54819ca | 8826 | |
e4b61340 | 8827 | } else if (strstr(program_invocation_short_name, "reboot")) { |
5622dde3 KS |
8828 | if (kexec_loaded()) |
8829 | arg_action = ACTION_KEXEC; | |
8830 | else | |
8831 | arg_action = ACTION_REBOOT; | |
e4b61340 | 8832 | return halt_parse_argv(argc, argv); |
c54819ca | 8833 | |
e4b61340 LP |
8834 | } else if (strstr(program_invocation_short_name, "shutdown")) { |
8835 | arg_action = ACTION_POWEROFF; | |
8836 | return shutdown_parse_argv(argc, argv); | |
c54819ca | 8837 | |
e4b61340 | 8838 | } else if (strstr(program_invocation_short_name, "init")) { |
d5ca5f11 | 8839 | |
c54819ca LP |
8840 | /* Matches invocations as "init" as well as "telinit", which are synonymous when run as PID != |
8841 | * 1 on SysV. | |
8842 | * | |
8843 | * On SysV "telinit" was the official command to communicate with PID 1, but "init" would | |
8844 | * redirect itself to "telinit" if called with PID != 1. We follow the same logic here still, | |
8845 | * though we add one level of indirection, as we implement "telinit" in "systemctl". Hence, for | |
8846 | * us if you invoke "init" you get "systemd", but it will execve() "systemctl" immediately with | |
8847 | * argv[] unmodified if PID is != 1. If you invoke "telinit" you directly get "systemctl". In | |
8848 | * both cases we shall do the same thing, which is why we do strstr(p_i_s_n, "init") here, as a | |
8849 | * quick way to match both. | |
8850 | * | |
8851 | * Also see redirect_telinit() in src/core/main.c. */ | |
8852 | ||
d5ca5f11 | 8853 | if (sd_booted() > 0) { |
f459b602 | 8854 | arg_action = _ACTION_INVALID; |
d5ca5f11 LP |
8855 | return telinit_parse_argv(argc, argv); |
8856 | } else { | |
c54819ca LP |
8857 | /* Hmm, so some other init system is running, we need to forward this request to |
8858 | * it. For now we simply guess that it is Upstart. */ | |
d5ca5f11 | 8859 | |
595225af | 8860 | (void) rlimit_nofile_safe(); |
4ad61fd1 | 8861 | execv(TELINIT, argv); |
d5ca5f11 | 8862 | |
baaa35ad ZJS |
8863 | return log_error_errno(SYNTHETIC_ERRNO(EIO), |
8864 | "Couldn't find an alternative telinit implementation to spawn."); | |
d5ca5f11 LP |
8865 | } |
8866 | ||
e4b61340 LP |
8867 | } else if (strstr(program_invocation_short_name, "runlevel")) { |
8868 | arg_action = ACTION_RUNLEVEL; | |
8869 | return runlevel_parse_argv(argc, argv); | |
8870 | } | |
8871 | } | |
8872 | ||
8873 | arg_action = ACTION_SYSTEMCTL; | |
8874 | return systemctl_parse_argv(argc, argv); | |
8875 | } | |
8876 | ||
349cc4a5 | 8877 | #if HAVE_SYSV_COMPAT |
44a6b1b6 | 8878 | _pure_ static int action_to_runlevel(void) { |
eb22ac37 LP |
8879 | static const char table[_ACTION_MAX] = { |
8880 | [ACTION_HALT] = '0', | |
8881 | [ACTION_POWEROFF] = '0', | |
8882 | [ACTION_REBOOT] = '6', | |
8883 | [ACTION_RUNLEVEL2] = '2', | |
8884 | [ACTION_RUNLEVEL3] = '3', | |
8885 | [ACTION_RUNLEVEL4] = '4', | |
8886 | [ACTION_RUNLEVEL5] = '5', | |
8887 | [ACTION_RESCUE] = '1' | |
8888 | }; | |
8889 | ||
78ca9099 | 8890 | assert(arg_action >= 0 && arg_action < _ACTION_MAX); |
d55ae9e6 LP |
8891 | return table[arg_action]; |
8892 | } | |
d2e79673 | 8893 | #endif |
d55ae9e6 | 8894 | |
e449de87 | 8895 | static int systemctl_main(int argc, char *argv[]) { |
e449de87 | 8896 | static const Verb verbs[] = { |
c56d1e2c | 8897 | { "list-units", VERB_ANY, VERB_ANY, VERB_DEFAULT|VERB_ONLINE_ONLY, list_units }, |
38fcb7f7 ZJS |
8898 | { "list-unit-files", VERB_ANY, VERB_ANY, 0, list_unit_files }, |
8899 | { "list-sockets", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, list_sockets }, | |
8900 | { "list-timers", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, list_timers }, | |
8901 | { "list-jobs", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, list_jobs }, | |
8902 | { "list-machines", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, list_machines }, | |
8903 | { "clear-jobs", VERB_ANY, 1, VERB_ONLINE_ONLY, trivial_method }, | |
8904 | { "cancel", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, cancel_job }, | |
8905 | { "start", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, | |
8906 | { "stop", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, | |
8907 | { "condstop", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, /* For compatibility with ALTLinux */ | |
8908 | { "reload", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, | |
8909 | { "restart", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, | |
8910 | { "try-restart", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, | |
8911 | { "reload-or-restart", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, | |
8912 | { "reload-or-try-restart", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, /* For compatibility with old systemctl <= 228 */ | |
8913 | { "try-reload-or-restart", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, | |
8914 | { "force-reload", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, /* For compatibility with SysV */ | |
8915 | { "condreload", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, /* For compatibility with ALTLinux */ | |
8916 | { "condrestart", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, /* For compatibility with RH */ | |
8917 | { "isolate", 2, 2, VERB_ONLINE_ONLY, start_unit }, | |
8918 | { "kill", 2, VERB_ANY, VERB_ONLINE_ONLY, kill_unit }, | |
d9e45bc3 MS |
8919 | { "clean", 2, VERB_ANY, VERB_ONLINE_ONLY, clean_or_freeze_unit }, |
8920 | { "freeze", 2, VERB_ANY, VERB_ONLINE_ONLY, clean_or_freeze_unit }, | |
8921 | { "thaw", 2, VERB_ANY, VERB_ONLINE_ONLY, clean_or_freeze_unit }, | |
38fcb7f7 ZJS |
8922 | { "is-active", 2, VERB_ANY, VERB_ONLINE_ONLY, check_unit_active }, |
8923 | { "check", 2, VERB_ANY, VERB_ONLINE_ONLY, check_unit_active }, /* deprecated alias of is-active */ | |
8924 | { "is-failed", 2, VERB_ANY, VERB_ONLINE_ONLY, check_unit_failed }, | |
8925 | { "show", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, show }, | |
8926 | { "cat", 2, VERB_ANY, VERB_ONLINE_ONLY, cat }, | |
8927 | { "status", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, show }, | |
8928 | { "help", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, show }, | |
8929 | { "daemon-reload", VERB_ANY, 1, VERB_ONLINE_ONLY, daemon_reload }, | |
8930 | { "daemon-reexec", VERB_ANY, 1, VERB_ONLINE_ONLY, daemon_reload }, | |
ae98f726 ZJS |
8931 | { "log-level", VERB_ANY, 2, VERB_ONLINE_ONLY, log_level }, |
8932 | { "log-target", VERB_ANY, 2, VERB_ONLINE_ONLY, log_target }, | |
8933 | { "service-watchdogs", VERB_ANY, 2, VERB_ONLINE_ONLY, service_watchdogs }, | |
38fcb7f7 ZJS |
8934 | { "show-environment", VERB_ANY, 1, VERB_ONLINE_ONLY, show_environment }, |
8935 | { "set-environment", 2, VERB_ANY, VERB_ONLINE_ONLY, set_environment }, | |
8936 | { "unset-environment", 2, VERB_ANY, VERB_ONLINE_ONLY, set_environment }, | |
8937 | { "import-environment", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, import_environment }, | |
8938 | { "halt", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, | |
8939 | { "poweroff", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, | |
8940 | { "reboot", VERB_ANY, 2, VERB_ONLINE_ONLY, start_system_special }, | |
8941 | { "kexec", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, | |
8942 | { "suspend", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, | |
8943 | { "hibernate", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, | |
8944 | { "hybrid-sleep", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, | |
8945 | { "suspend-then-hibernate",VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, | |
8946 | { "default", VERB_ANY, 1, VERB_ONLINE_ONLY, start_special }, | |
8947 | { "rescue", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, | |
8948 | { "emergency", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, | |
8949 | { "exit", VERB_ANY, 2, VERB_ONLINE_ONLY, start_special }, | |
8950 | { "reset-failed", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, reset_failed }, | |
8951 | { "enable", 2, VERB_ANY, 0, enable_unit }, | |
8952 | { "disable", 2, VERB_ANY, 0, enable_unit }, | |
8953 | { "is-enabled", 2, VERB_ANY, 0, unit_is_enabled }, | |
8954 | { "reenable", 2, VERB_ANY, 0, enable_unit }, | |
8955 | { "preset", 2, VERB_ANY, 0, enable_unit }, | |
8956 | { "preset-all", VERB_ANY, 1, 0, preset_all }, | |
8957 | { "mask", 2, VERB_ANY, 0, enable_unit }, | |
8958 | { "unmask", 2, VERB_ANY, 0, enable_unit }, | |
8959 | { "link", 2, VERB_ANY, 0, enable_unit }, | |
8960 | { "revert", 2, VERB_ANY, 0, enable_unit }, | |
8961 | { "switch-root", 2, VERB_ANY, VERB_ONLINE_ONLY, switch_root }, | |
94c3a838 | 8962 | { "list-dependencies", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, list_dependencies }, |
38fcb7f7 ZJS |
8963 | { "set-default", 2, 2, 0, set_default }, |
8964 | { "get-default", VERB_ANY, 1, 0, get_default }, | |
8965 | { "set-property", 3, VERB_ANY, VERB_ONLINE_ONLY, set_property }, | |
8966 | { "is-system-running", VERB_ANY, 1, 0, is_system_running }, | |
8967 | { "add-wants", 3, VERB_ANY, 0, add_dependency }, | |
8968 | { "add-requires", 3, VERB_ANY, 0, add_dependency }, | |
8969 | { "edit", 2, VERB_ANY, VERB_ONLINE_ONLY, edit }, | |
d08e75ed | 8970 | {} |
e449de87 | 8971 | }; |
7e4249b9 | 8972 | |
4cb1015e ZJS |
8973 | const Verb *verb = verbs_find_verb(argv[optind], verbs); |
8974 | if (verb && (verb->flags & VERB_ONLINE_ONLY) && arg_root) | |
8975 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8976 | "Verb '%s' cannot be used with --root=.", | |
8977 | argv[optind] ?: verb->verb); | |
8978 | ||
e449de87 | 8979 | return dispatch_verb(argc, argv, verbs, NULL); |
e4b61340 LP |
8980 | } |
8981 | ||
4fbd7192 | 8982 | static int reload_with_fallback(void) { |
4fbd7192 | 8983 | /* First, try systemd via D-Bus. */ |
e449de87 | 8984 | if (daemon_reload(0, NULL, NULL) >= 0) |
4fbd7192 | 8985 | return 0; |
e4b61340 LP |
8986 | |
8987 | /* Nothing else worked, so let's try signals */ | |
2853b60a | 8988 | assert(IN_SET(arg_action, ACTION_RELOAD, ACTION_REEXEC)); |
e4b61340 | 8989 | |
4a62c710 MS |
8990 | if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) |
8991 | return log_error_errno(errno, "kill() failed: %m"); | |
e4b61340 LP |
8992 | |
8993 | return 0; | |
8994 | } | |
8995 | ||
4fbd7192 | 8996 | static int start_with_fallback(void) { |
4fbd7192 | 8997 | /* First, try systemd via D-Bus. */ |
2e13c0c3 | 8998 | if (start_unit(0, NULL, NULL) == 0) |
4fbd7192 | 8999 | return 0; |
e4b61340 | 9000 | |
2771aaf5 | 9001 | #if HAVE_SYSV_COMPAT |
2853b60a | 9002 | /* Nothing else worked, so let's try /dev/initctl */ |
63a3b3cb | 9003 | if (talk_initctl(action_to_runlevel()) > 0) |
48ec22bc | 9004 | return 0; |
2771aaf5 | 9005 | #endif |
d55ae9e6 | 9006 | |
baaa35ad ZJS |
9007 | return log_error_errno(SYNTHETIC_ERRNO(EIO), |
9008 | "Failed to talk to init daemon."); | |
e4b61340 LP |
9009 | } |
9010 | ||
477def80 | 9011 | static int halt_now(enum action a) { |
c01dcddf | 9012 | /* The kernel will automatically flush ATA disks and suchlike on reboot(), but the file systems need to be |
f03ec2b1 | 9013 | * synced explicitly in advance. */ |
1ae17672 | 9014 | if (!arg_no_sync && !arg_dry_run) |
f3f054f0 | 9015 | (void) sync(); |
4a3ad399 | 9016 | |
1ae17672 ZJS |
9017 | /* Make sure C-A-D is handled by the kernel from this point on... */ |
9018 | if (!arg_dry_run) | |
9019 | (void) reboot(RB_ENABLE_CAD); | |
e606bb61 | 9020 | |
4c80c73c | 9021 | switch (a) { |
e606bb61 LP |
9022 | |
9023 | case ACTION_HALT: | |
60675884 LP |
9024 | if (!arg_quiet) |
9025 | log_info("Halting."); | |
1ae17672 ZJS |
9026 | if (arg_dry_run) |
9027 | return 0; | |
19578bb2 | 9028 | (void) reboot(RB_HALT_SYSTEM); |
477def80 | 9029 | return -errno; |
e606bb61 LP |
9030 | |
9031 | case ACTION_POWEROFF: | |
60675884 LP |
9032 | if (!arg_quiet) |
9033 | log_info("Powering off."); | |
1ae17672 ZJS |
9034 | if (arg_dry_run) |
9035 | return 0; | |
19578bb2 | 9036 | (void) reboot(RB_POWER_OFF); |
477def80 | 9037 | return -errno; |
e606bb61 | 9038 | |
98d52feb | 9039 | case ACTION_KEXEC: |
c01dcddf LP |
9040 | case ACTION_REBOOT: |
9041 | return reboot_with_parameter(REBOOT_FALLBACK | | |
9042 | (arg_quiet ? 0 : REBOOT_LOG) | | |
9043 | (arg_dry_run ? REBOOT_DRY_RUN : 0)); | |
e606bb61 | 9044 | |
477def80 LP |
9045 | default: |
9046 | assert_not_reached("Unknown action."); | |
9047 | } | |
e606bb61 LP |
9048 | } |
9049 | ||
56a730fa | 9050 | static int logind_schedule_shutdown(void) { |
7f56c3d4 | 9051 | |
cf9cda44 | 9052 | #if ENABLE_LOGIND |
4afd3348 | 9053 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
56a730fa LP |
9054 | char date[FORMAT_TIMESTAMP_MAX]; |
9055 | const char *action; | |
6df8a6c7 | 9056 | const char *log_action; |
4fbd7192 | 9057 | sd_bus *bus; |
56a730fa LP |
9058 | int r; |
9059 | ||
4fbd7192 | 9060 | r = acquire_bus(BUS_FULL, &bus); |
56a730fa | 9061 | if (r < 0) |
4fbd7192 | 9062 | return r; |
56a730fa LP |
9063 | |
9064 | switch (arg_action) { | |
9065 | case ACTION_HALT: | |
9066 | action = "halt"; | |
6df8a6c7 | 9067 | log_action = "Shutdown"; |
56a730fa LP |
9068 | break; |
9069 | case ACTION_POWEROFF: | |
9070 | action = "poweroff"; | |
6df8a6c7 | 9071 | log_action = "Shutdown"; |
56a730fa LP |
9072 | break; |
9073 | case ACTION_KEXEC: | |
9074 | action = "kexec"; | |
6df8a6c7 | 9075 | log_action = "Reboot via kexec"; |
56a730fa | 9076 | break; |
a4420f7b LP |
9077 | case ACTION_EXIT: |
9078 | action = "exit"; | |
6df8a6c7 | 9079 | log_action = "Shutdown"; |
a4420f7b LP |
9080 | break; |
9081 | case ACTION_REBOOT: | |
56a730fa LP |
9082 | default: |
9083 | action = "reboot"; | |
6df8a6c7 | 9084 | log_action = "Reboot"; |
56a730fa LP |
9085 | break; |
9086 | } | |
9087 | ||
1ae17672 | 9088 | if (arg_dry_run) |
56a730fa LP |
9089 | action = strjoina("dry-", action); |
9090 | ||
d2ad7e1b ZJS |
9091 | (void) logind_set_wall_message(); |
9092 | ||
8010c205 | 9093 | r = bus_call_method(bus, bus_login_mgr, "ScheduleShutdown", &error, NULL, "st", action, arg_when); |
56a730fa LP |
9094 | if (r < 0) |
9095 | return log_warning_errno(r, "Failed to call ScheduleShutdown in logind, proceeding with immediate shutdown: %s", bus_error_message(&error, r)); | |
9096 | ||
60675884 | 9097 | if (!arg_quiet) |
6df8a6c7 | 9098 | log_info("%s scheduled for %s, use 'shutdown -c' to cancel.", log_action, format_timestamp(date, sizeof(date), arg_when)); |
56a730fa | 9099 | return 0; |
cf9cda44 | 9100 | #else |
2f50e59f LP |
9101 | return log_error_errno(SYNTHETIC_ERRNO(ENOSYS), |
9102 | "Cannot schedule shutdown without logind support, proceeding with immediate shutdown."); | |
807690fb | 9103 | #endif |
56a730fa LP |
9104 | } |
9105 | ||
4fbd7192 | 9106 | static int halt_main(void) { |
e4b61340 LP |
9107 | int r; |
9108 | ||
4fbd7192 | 9109 | r = logind_check_inhibitors(arg_action); |
748ebafa LP |
9110 | if (r < 0) |
9111 | return r; | |
b37844d3 | 9112 | |
940bec70 DJL |
9113 | /* Delayed shutdown requested, and was successful */ |
9114 | if (arg_when > 0 && logind_schedule_shutdown() == 0) | |
9115 | return 0; | |
9116 | /* no delay, or logind failed or is not at all available */ | |
7f96539d | 9117 | |
bc8c2f5c | 9118 | if (geteuid() != 0) { |
1ae17672 | 9119 | if (arg_dry_run || arg_force > 0) { |
fba868fa | 9120 | (void) must_be_root(); |
2ac3930f IS |
9121 | return -EPERM; |
9122 | } | |
9123 | ||
7e59bfcb | 9124 | /* Try logind if we are a normal user and no special |
d35f51ea | 9125 | * mode applies. Maybe polkit allows us to shutdown |
7e59bfcb | 9126 | * the machine. */ |
36b69c31 | 9127 | if (IN_SET(arg_action, ACTION_POWEROFF, ACTION_REBOOT, ACTION_HALT)) { |
4fbd7192 | 9128 | r = logind_reboot(arg_action); |
4c80c73c KS |
9129 | if (r >= 0) |
9130 | return r; | |
a9085ea3 | 9131 | if (IN_SET(r, -EOPNOTSUPP, -EINPROGRESS)) |
7f96539d LP |
9132 | /* requested operation is not |
9133 | * supported on the local system or | |
9134 | * already in progress */ | |
a9085ea3 IS |
9135 | return r; |
9136 | /* on all other errors, try low-level operation */ | |
4c80c73c | 9137 | } |
bc8c2f5c LP |
9138 | } |
9139 | ||
2955e0d4 GJ |
9140 | /* In order to minimize the difference between operation with and |
9141 | * without logind, we explicitly enable non-blocking mode for this, | |
9142 | * as logind's shutdown operations are always non-blocking. */ | |
9143 | arg_no_block = true; | |
9144 | ||
1ae17672 | 9145 | if (!arg_dry_run && !arg_force) |
4fbd7192 | 9146 | return start_with_fallback(); |
e4b61340 | 9147 | |
2ac3930f IS |
9148 | assert(geteuid() == 0); |
9149 | ||
d90e1a30 LP |
9150 | if (!arg_no_wtmp) { |
9151 | if (sd_booted() > 0) | |
9152 | log_debug("Not writing utmp record, assuming that systemd-update-utmp is used."); | |
7e59bfcb LP |
9153 | else { |
9154 | r = utmp_put_shutdown(); | |
9155 | if (r < 0) | |
da927ba9 | 9156 | log_warning_errno(r, "Failed to write utmp record: %m"); |
7e59bfcb | 9157 | } |
d90e1a30 | 9158 | } |
e4b61340 | 9159 | |
1ae17672 | 9160 | if (arg_dry_run) |
e4b61340 LP |
9161 | return 0; |
9162 | ||
477def80 | 9163 | r = halt_now(arg_action); |
691395d8 | 9164 | return log_error_errno(r, "Failed to reboot: %m"); |
e4b61340 LP |
9165 | } |
9166 | ||
9167 | static int runlevel_main(void) { | |
9168 | int r, runlevel, previous; | |
9169 | ||
729e3769 LP |
9170 | r = utmp_get_runlevel(&runlevel, &previous); |
9171 | if (r < 0) { | |
9172 | puts("unknown"); | |
e4b61340 LP |
9173 | return r; |
9174 | } | |
9175 | ||
9176 | printf("%c %c\n", | |
9177 | previous <= 0 ? 'N' : previous, | |
9178 | runlevel <= 0 ? 'N' : runlevel); | |
9179 | ||
9180 | return 0; | |
9181 | } | |
9182 | ||
2cf05793 | 9183 | static int logind_cancel_shutdown(void) { |
349cc4a5 | 9184 | #if ENABLE_LOGIND |
4afd3348 | 9185 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
4fbd7192 | 9186 | sd_bus *bus; |
949d9ce9 LP |
9187 | int r; |
9188 | ||
4fbd7192 | 9189 | r = acquire_bus(BUS_FULL, &bus); |
949d9ce9 | 9190 | if (r < 0) |
4fbd7192 | 9191 | return r; |
949d9ce9 | 9192 | |
4fbd7192 | 9193 | (void) logind_set_wall_message(); |
949d9ce9 | 9194 | |
8010c205 | 9195 | r = bus_call_method(bus, bus_login_mgr, "CancelScheduledShutdown", &error, NULL, NULL); |
949d9ce9 LP |
9196 | if (r < 0) |
9197 | return log_warning_errno(r, "Failed to talk to logind, shutdown hasn't been cancelled: %s", bus_error_message(&error, r)); | |
9198 | ||
9199 | return 0; | |
2cf05793 | 9200 | #else |
2f50e59f LP |
9201 | return log_error_errno(SYNTHETIC_ERRNO(ENOSYS), |
9202 | "Not compiled with logind support, cannot cancel scheduled shutdowns."); | |
2cf05793 | 9203 | #endif |
949d9ce9 LP |
9204 | } |
9205 | ||
f2a3de01 | 9206 | static int run(int argc, char *argv[]) { |
f459b602 | 9207 | int r; |
e4b61340 | 9208 | |
a9cdc94f | 9209 | setlocale(LC_ALL, ""); |
41d1f469 | 9210 | log_parse_environment_cli(); |
2396fb04 | 9211 | log_open(); |
1abaf488 LP |
9212 | |
9213 | /* The journal merging logic potentially needs a lot of fds. */ | |
9214 | (void) rlimit_nofile_bump(HIGH_RLIMIT_NOFILE); | |
9215 | ||
592705f2 | 9216 | sigbus_install(); |
e4b61340 | 9217 | |
184ecaf7 DR |
9218 | /* Explicitly not on_tty() to avoid setting cached value. |
9219 | * This becomes relevant for piping output which might be | |
9220 | * ellipsized. */ | |
9221 | original_stdout_is_tty = isatty(STDOUT_FILENO); | |
9222 | ||
04ebb595 | 9223 | r = parse_argv(argc, argv); |
f459b602 | 9224 | if (r <= 0) |
e4b61340 | 9225 | goto finish; |
7e4249b9 | 9226 | |
040524b4 | 9227 | if (arg_action != ACTION_SYSTEMCTL && running_in_chroot() > 0) { |
60675884 LP |
9228 | if (!arg_quiet) |
9229 | log_info("Running in chroot, ignoring request."); | |
f459b602 | 9230 | r = 0; |
82e23ddd LP |
9231 | goto finish; |
9232 | } | |
9233 | ||
41dd15e4 LP |
9234 | /* systemctl_main() will print an error message for the bus |
9235 | * connection, but only if it needs to */ | |
e4b61340 LP |
9236 | |
9237 | switch (arg_action) { | |
9238 | ||
22f4096c | 9239 | case ACTION_SYSTEMCTL: |
e449de87 | 9240 | r = systemctl_main(argc, argv); |
e4b61340 | 9241 | break; |
e4b61340 | 9242 | |
081dc638 AJ |
9243 | /* Legacy command aliases set arg_action. They provide some fallbacks, |
9244 | * e.g. to tell sysvinit to reboot after you have installed systemd | |
9245 | * binaries. */ | |
9246 | ||
e4b61340 LP |
9247 | case ACTION_HALT: |
9248 | case ACTION_POWEROFF: | |
9249 | case ACTION_REBOOT: | |
5622dde3 | 9250 | case ACTION_KEXEC: |
4fbd7192 | 9251 | r = halt_main(); |
e4b61340 LP |
9252 | break; |
9253 | ||
e4b61340 LP |
9254 | case ACTION_RUNLEVEL2: |
9255 | case ACTION_RUNLEVEL3: | |
9256 | case ACTION_RUNLEVEL4: | |
9257 | case ACTION_RUNLEVEL5: | |
9258 | case ACTION_RESCUE: | |
4fbd7192 | 9259 | r = start_with_fallback(); |
e4b61340 | 9260 | break; |
7e4249b9 | 9261 | |
e4b61340 LP |
9262 | case ACTION_RELOAD: |
9263 | case ACTION_REEXEC: | |
4fbd7192 | 9264 | r = reload_with_fallback(); |
e4b61340 LP |
9265 | break; |
9266 | ||
949d9ce9 | 9267 | case ACTION_CANCEL_SHUTDOWN: |
2cf05793 | 9268 | r = logind_cancel_shutdown(); |
f6144808 LP |
9269 | break; |
9270 | ||
eb22ac37 | 9271 | case ACTION_RUNLEVEL: |
4f16c1f4 LP |
9272 | r = runlevel_main(); |
9273 | break; | |
9274 | ||
081dc638 AJ |
9275 | case ACTION_EXIT: |
9276 | case ACTION_SUSPEND: | |
9277 | case ACTION_HIBERNATE: | |
9278 | case ACTION_HYBRID_SLEEP: | |
e68c79db | 9279 | case ACTION_SUSPEND_THEN_HIBERNATE: |
081dc638 AJ |
9280 | case ACTION_EMERGENCY: |
9281 | case ACTION_DEFAULT: | |
9282 | /* systemctl verbs with no equivalent in the legacy commands. | |
9283 | * These cannot appear in arg_action. Fall through. */ | |
9284 | ||
f459b602 | 9285 | case _ACTION_INVALID: |
e4b61340 LP |
9286 | default: |
9287 | assert_not_reached("Unknown action"); | |
9288 | } | |
7e4249b9 LP |
9289 | |
9290 | finish: | |
cf647b69 LP |
9291 | release_busses(); |
9292 | ||
d1405af3 ZJS |
9293 | /* Note that we return r here, not 0, so that we can implement the LSB-like return codes */ |
9294 | return r; | |
7e4249b9 | 9295 | } |
d1405af3 ZJS |
9296 | |
9297 | DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run); |