]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
7e4249b9 | 2 | |
03a7b521 LP |
3 | #include <errno.h> |
4 | #include <fcntl.h> | |
7e4249b9 | 5 | #include <getopt.h> |
a9cdc94f | 6 | #include <locale.h> |
7e4249b9 | 7 | #include <stdbool.h> |
03a7b521 LP |
8 | #include <stddef.h> |
9 | #include <stdio.h> | |
7e4249b9 | 10 | #include <string.h> |
4c787df7 | 11 | #include <sys/mount.h> |
4bb2e9d4 | 12 | #include <sys/prctl.h> |
03a7b521 | 13 | #include <sys/reboot.h> |
f1c5860b | 14 | #include <sys/socket.h> |
03a7b521 | 15 | #include <unistd.h> |
81527be1 | 16 | |
03a7b521 | 17 | #include "sd-bus.h" |
f459b602 | 18 | #include "sd-daemon.h" |
adb6cd9b | 19 | #include "sd-event.h" |
f459b602 | 20 | #include "sd-login.h" |
03a7b521 | 21 | |
b5efdb8a | 22 | #include "alloc-util.h" |
4bb2e9d4 | 23 | #include "bootspec.h" |
03a7b521 LP |
24 | #include "bus-common-errors.h" |
25 | #include "bus-error.h" | |
26 | #include "bus-message.h" | |
25b1d72d | 27 | #include "bus-unit-procs.h" |
291d565a | 28 | #include "bus-unit-util.h" |
03a7b521 | 29 | #include "bus-util.h" |
e45c81b8 | 30 | #include "bus-wait-for-jobs.h" |
d81bc51f | 31 | #include "bus-wait-for-units.h" |
ab35fb1b | 32 | #include "cgroup-show.h" |
c6c18be3 | 33 | #include "cgroup-util.h" |
cda134ab | 34 | #include "copy.h" |
a047f4f1 | 35 | #include "cpu-set-util.h" |
ad2a0358 | 36 | #include "dropin.h" |
5bdf2243 | 37 | #include "efivars.h" |
03a7b521 | 38 | #include "env-util.h" |
804ee07c | 39 | #include "escape.h" |
b3d59367 | 40 | #include "exec-util.h" |
03a7b521 | 41 | #include "exit-status.h" |
3ffd4af2 | 42 | #include "fd-util.h" |
f97b34a6 | 43 | #include "format-util.h" |
f4f15635 | 44 | #include "fs-util.h" |
7d50b32a | 45 | #include "glob-util.h" |
08f3be7a | 46 | #include "hexdecoct.h" |
c52a937b | 47 | #include "hostname-util.h" |
0a970718 | 48 | #include "in-addr-util.h" |
03a7b521 LP |
49 | #include "initreq.h" |
50 | #include "install.h" | |
c004493c | 51 | #include "io-util.h" |
305a2850 | 52 | #include "journal-util.h" |
03a7b521 | 53 | #include "list.h" |
8752c575 | 54 | #include "locale-util.h" |
03a7b521 LP |
55 | #include "log.h" |
56 | #include "logs-show.h" | |
57 | #include "macro.h" | |
d1405af3 | 58 | #include "main-func.h" |
0a970718 | 59 | #include "memory-util.h" |
03a7b521 LP |
60 | #include "mkdir.h" |
61 | #include "pager.h" | |
6bedfcbb | 62 | #include "parse-util.h" |
03a7b521 LP |
63 | #include "path-lookup.h" |
64 | #include "path-util.h" | |
294bf0c3 | 65 | #include "pretty-print.h" |
01771226 | 66 | #include "proc-cmdline.h" |
03a7b521 | 67 | #include "process-util.h" |
e3631d1c | 68 | #include "reboot-util.h" |
78f22b97 | 69 | #include "rlimit-util.h" |
03a7b521 | 70 | #include "set.h" |
592705f2 | 71 | #include "sigbus.h" |
24882e06 | 72 | #include "signal-util.h" |
03a7b521 | 73 | #include "socket-util.h" |
760877e9 | 74 | #include "sort-util.h" |
03a7b521 LP |
75 | #include "spawn-ask-password-agent.h" |
76 | #include "spawn-polkit-agent.h" | |
77 | #include "special.h" | |
8fcde012 | 78 | #include "stat-util.h" |
8559e61d | 79 | #include "string-table.h" |
03a7b521 | 80 | #include "strv.h" |
63a3b3cb | 81 | #include "sysv-compat.h" |
03a7b521 | 82 | #include "terminal-util.h" |
e4de7287 | 83 | #include "tmpfile-util.h" |
89ada3ba | 84 | #include "unit-def.h" |
03a7b521 | 85 | #include "unit-name.h" |
b1d4f8e1 | 86 | #include "user-util.h" |
305a2850 | 87 | #include "utf8.h" |
03a7b521 | 88 | #include "utmp-wtmp.h" |
e449de87 | 89 | #include "verbs.h" |
7f4b3c5e | 90 | #include "virt.h" |
7e4249b9 | 91 | |
20b3f379 | 92 | static char **arg_types = NULL; |
9b9b3d36 | 93 | static char **arg_states = NULL; |
20b3f379 | 94 | static char **arg_properties = NULL; |
7e4249b9 | 95 | static bool arg_all = false; |
afba4199 ZJS |
96 | static enum dependency { |
97 | DEPENDENCY_FORWARD, | |
98 | DEPENDENCY_REVERSE, | |
99 | DEPENDENCY_AFTER, | |
100 | DEPENDENCY_BEFORE, | |
071066a5 | 101 | _DEPENDENCY_MAX |
afba4199 | 102 | } arg_dependency = DEPENDENCY_FORWARD; |
e67c3609 | 103 | static const char *arg_job_mode = "replace"; |
729e3769 | 104 | static UnitFileScope arg_scope = UNIT_FILE_SYSTEM; |
93a08841 | 105 | static bool arg_wait = false; |
ee5762e3 | 106 | static bool arg_no_block = false; |
ebed32bf | 107 | static bool arg_no_legend = false; |
0221d68a | 108 | static PagerFlags arg_pager_flags = 0; |
e4b61340 | 109 | static bool arg_no_wtmp = false; |
f3f054f0 | 110 | static bool arg_no_sync = false; |
514f4ef5 | 111 | static bool arg_no_wall = false; |
ee5762e3 | 112 | static bool arg_no_reload = false; |
4f9a9105 | 113 | static bool arg_value = false; |
991f2a39 | 114 | static bool arg_show_types = false; |
b37844d3 | 115 | static bool arg_ignore_inhibitors = false; |
1ae17672 | 116 | static bool arg_dry_run = false; |
0183528f | 117 | static bool arg_quiet = false; |
ee5762e3 | 118 | static bool arg_full = false; |
1238ee09 | 119 | static bool arg_recursive = false; |
85d9b598 | 120 | static bool arg_show_transaction = false; |
e606bb61 | 121 | static int arg_force = 0; |
16f017fa | 122 | static bool arg_ask_password = false; |
729e3769 | 123 | static bool arg_runtime = false; |
d309c1c3 | 124 | static UnitFilePresetMode arg_preset_mode = UNIT_FILE_PRESET_FULL; |
e4b61340 | 125 | static char **arg_wall = NULL; |
8a0867d6 | 126 | static const char *arg_kill_who = NULL; |
8a0867d6 | 127 | static int arg_signal = SIGTERM; |
0f03c2a4 | 128 | static char *arg_root = NULL; |
f6144808 | 129 | static usec_t arg_when = 0; |
4445a875 | 130 | static enum action { |
e4b61340 LP |
131 | ACTION_SYSTEMCTL, |
132 | ACTION_HALT, | |
133 | ACTION_POWEROFF, | |
134 | ACTION_REBOOT, | |
20b09ca7 LP |
135 | ACTION_KEXEC, |
136 | ACTION_EXIT, | |
6edd7d0a LP |
137 | ACTION_SUSPEND, |
138 | ACTION_HIBERNATE, | |
6524990f | 139 | ACTION_HYBRID_SLEEP, |
e68c79db | 140 | ACTION_SUSPEND_THEN_HIBERNATE, |
e4b61340 LP |
141 | ACTION_RUNLEVEL2, |
142 | ACTION_RUNLEVEL3, | |
143 | ACTION_RUNLEVEL4, | |
144 | ACTION_RUNLEVEL5, | |
145 | ACTION_RESCUE, | |
514f4ef5 LP |
146 | ACTION_EMERGENCY, |
147 | ACTION_DEFAULT, | |
e4b61340 LP |
148 | ACTION_RELOAD, |
149 | ACTION_REEXEC, | |
150 | ACTION_RUNLEVEL, | |
f6144808 | 151 | ACTION_CANCEL_SHUTDOWN, |
913c1916 AJ |
152 | _ACTION_MAX, |
153 | _ACTION_INVALID = -1 | |
e4b61340 | 154 | } arg_action = ACTION_SYSTEMCTL; |
f459b602 | 155 | static BusTransport arg_transport = BUS_TRANSPORT_LOCAL; |
fc2ffaf1 | 156 | static const char *arg_host = NULL; |
df50185b LP |
157 | static unsigned arg_lines = 10; |
158 | static OutputMode arg_output = OUTPUT_SHORT; | |
5d0c05e5 | 159 | static bool arg_plain = false; |
5bdf2243 | 160 | static bool arg_firmware_setup = false; |
97af80c5 LP |
161 | static usec_t arg_boot_loader_menu = USEC_INFINITY; |
162 | static const char *arg_boot_loader_entry = NULL; | |
57ab2eab | 163 | static bool arg_now = false; |
82948f6c LP |
164 | static bool arg_jobs_before = false; |
165 | static bool arg_jobs_after = false; | |
d81bc51f | 166 | static char **arg_clean_what = NULL; |
1238ee09 | 167 | |
61ff45db LP |
168 | STATIC_DESTRUCTOR_REGISTER(arg_wall, strv_freep); |
169 | STATIC_DESTRUCTOR_REGISTER(arg_root, freep); | |
170 | STATIC_DESTRUCTOR_REGISTER(arg_types, strv_freep); | |
171 | STATIC_DESTRUCTOR_REGISTER(arg_states, strv_freep); | |
172 | STATIC_DESTRUCTOR_REGISTER(arg_properties, strv_freep); | |
d81bc51f | 173 | STATIC_DESTRUCTOR_REGISTER(arg_clean_what, strv_freep); |
61ff45db | 174 | |
e449de87 | 175 | static int daemon_reload(int argc, char *argv[], void* userdata); |
2853b60a | 176 | static int trivial_method(int argc, char *argv[], void *userdata); |
477def80 | 177 | static int halt_now(enum action a); |
fa0d5878 | 178 | static int get_state_one_unit(sd_bus *bus, const char *name, UnitActiveState *active_state); |
dbc2c080 | 179 | |
4fbd7192 LP |
180 | static bool original_stdout_is_tty; |
181 | ||
182 | typedef enum BusFocus { | |
183 | BUS_FULL, /* The full bus indicated via --system or --user */ | |
184 | BUS_MANAGER, /* The manager itself, possibly directly, possibly via the bus */ | |
185 | _BUS_FOCUS_MAX | |
186 | } BusFocus; | |
187 | ||
5238e957 | 188 | static sd_bus *buses[_BUS_FOCUS_MAX] = {}; |
4fbd7192 | 189 | |
b3796dd8 JS |
190 | static UnitFileFlags args_to_flags(void) { |
191 | return (arg_runtime ? UNIT_FILE_RUNTIME : 0) | | |
192 | (arg_force ? UNIT_FILE_FORCE : 0); | |
193 | } | |
194 | ||
4fbd7192 LP |
195 | static int acquire_bus(BusFocus focus, sd_bus **ret) { |
196 | int r; | |
197 | ||
198 | assert(focus < _BUS_FOCUS_MAX); | |
199 | assert(ret); | |
200 | ||
201 | /* We only go directly to the manager, if we are using a local transport */ | |
202 | if (arg_transport != BUS_TRANSPORT_LOCAL) | |
203 | focus = BUS_FULL; | |
204 | ||
d4015567 LP |
205 | if (getenv_bool("SYSTEMCTL_FORCE_BUS") > 0) |
206 | focus = BUS_FULL; | |
207 | ||
5238e957 | 208 | if (!buses[focus]) { |
4fbd7192 LP |
209 | bool user; |
210 | ||
211 | user = arg_scope != UNIT_FILE_SYSTEM; | |
212 | ||
213 | if (focus == BUS_MANAGER) | |
5238e957 | 214 | r = bus_connect_transport_systemd(arg_transport, arg_host, user, &buses[focus]); |
4fbd7192 | 215 | else |
5238e957 | 216 | r = bus_connect_transport(arg_transport, arg_host, user, &buses[focus]); |
4fbd7192 LP |
217 | if (r < 0) |
218 | return log_error_errno(r, "Failed to connect to bus: %m"); | |
219 | ||
5238e957 | 220 | (void) sd_bus_set_allow_interactive_authorization(buses[focus], arg_ask_password); |
4fbd7192 LP |
221 | } |
222 | ||
5238e957 | 223 | *ret = buses[focus]; |
4fbd7192 LP |
224 | return 0; |
225 | } | |
226 | ||
227 | static void release_busses(void) { | |
228 | BusFocus w; | |
229 | ||
230 | for (w = 0; w < _BUS_FOCUS_MAX; w++) | |
5238e957 | 231 | buses[w] = sd_bus_flush_close_unref(buses[w]); |
4fbd7192 LP |
232 | } |
233 | ||
6bb92a16 | 234 | static void ask_password_agent_open_if_enabled(void) { |
729e3769 | 235 | /* Open the password agent as a child process if necessary */ |
501fc174 | 236 | |
1ae17672 ZJS |
237 | if (arg_dry_run) |
238 | return; | |
239 | ||
501fc174 LP |
240 | if (!arg_ask_password) |
241 | return; | |
715554e7 | 242 | |
729e3769 | 243 | if (arg_scope != UNIT_FILE_SYSTEM) |
501fc174 LP |
244 | return; |
245 | ||
cbc9fbd1 LP |
246 | if (arg_transport != BUS_TRANSPORT_LOCAL) |
247 | return; | |
248 | ||
6bb92a16 LP |
249 | ask_password_agent_open(); |
250 | } | |
251 | ||
8a4b13c5 | 252 | static void polkit_agent_open_maybe(void) { |
6bb92a16 LP |
253 | /* Open the polkit agent as a child process if necessary */ |
254 | ||
6bb92a16 LP |
255 | if (arg_scope != UNIT_FILE_SYSTEM) |
256 | return; | |
257 | ||
8a4b13c5 | 258 | polkit_agent_open_if_enabled(arg_transport, arg_ask_password); |
501fc174 LP |
259 | } |
260 | ||
3c756001 LP |
261 | static OutputFlags get_output_flags(void) { |
262 | return | |
263 | arg_all * OUTPUT_SHOW_ALL | | |
459b9f9f | 264 | (arg_full || !on_tty() || pager_have()) * OUTPUT_FULL_WIDTH | |
40c9fe4c | 265 | colors_enabled() * OUTPUT_COLOR | |
8b0cc9a3 | 266 | !arg_quiet * OUTPUT_WARN_CUTOFF; |
3c756001 LP |
267 | } |
268 | ||
f459b602 | 269 | static int translate_bus_error_to_exit_status(int r, const sd_bus_error *error) { |
22f4096c LP |
270 | assert(error); |
271 | ||
f459b602 | 272 | if (!sd_bus_error_is_set(error)) |
22f4096c LP |
273 | return r; |
274 | ||
f459b602 MAP |
275 | if (sd_bus_error_has_name(error, SD_BUS_ERROR_ACCESS_DENIED) || |
276 | sd_bus_error_has_name(error, BUS_ERROR_ONLY_BY_DEPENDENCY) || | |
277 | sd_bus_error_has_name(error, BUS_ERROR_NO_ISOLATION) || | |
278 | sd_bus_error_has_name(error, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE)) | |
22f4096c LP |
279 | return EXIT_NOPERMISSION; |
280 | ||
f459b602 | 281 | if (sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT)) |
22f4096c LP |
282 | return EXIT_NOTINSTALLED; |
283 | ||
f459b602 | 284 | if (sd_bus_error_has_name(error, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE) || |
718db961 | 285 | sd_bus_error_has_name(error, SD_BUS_ERROR_NOT_SUPPORTED)) |
22f4096c LP |
286 | return EXIT_NOTIMPLEMENTED; |
287 | ||
f459b602 | 288 | if (sd_bus_error_has_name(error, BUS_ERROR_LOAD_FAILED)) |
22f4096c LP |
289 | return EXIT_NOTCONFIGURED; |
290 | ||
291 | if (r != 0) | |
292 | return r; | |
293 | ||
294 | return EXIT_FAILURE; | |
295 | } | |
296 | ||
4fbd7192 | 297 | static bool install_client_side(void) { |
4fbd7192 LP |
298 | /* Decides when to execute enable/disable/... operations |
299 | * client-side rather than server-side. */ | |
4f16c1f4 | 300 | |
f38951a6 | 301 | if (running_in_chroot_or_offline()) |
729e3769 LP |
302 | return true; |
303 | ||
304 | if (sd_booted() <= 0) | |
305 | return true; | |
306 | ||
307 | if (!isempty(arg_root)) | |
308 | return true; | |
309 | ||
310 | if (arg_scope == UNIT_FILE_GLOBAL) | |
311 | return true; | |
312 | ||
b41b9d2a LP |
313 | /* Unsupported environment variable, mostly for debugging purposes */ |
314 | if (getenv_bool("SYSTEMCTL_INSTALL_CLIENT_SIDE") > 0) | |
315 | return true; | |
316 | ||
729e3769 LP |
317 | return false; |
318 | } | |
319 | ||
93bab288 | 320 | static int compare_unit_info(const UnitInfo *a, const UnitInfo *b) { |
36c32ba2 | 321 | const char *d1, *d2; |
1238ee09 LP |
322 | int r; |
323 | ||
324 | /* First, order by machine */ | |
93bab288 | 325 | if (!a->machine && b->machine) |
1238ee09 | 326 | return -1; |
93bab288 | 327 | if (a->machine && !b->machine) |
1238ee09 | 328 | return 1; |
93bab288 YW |
329 | if (a->machine && b->machine) { |
330 | r = strcasecmp(a->machine, b->machine); | |
1238ee09 LP |
331 | if (r != 0) |
332 | return r; | |
333 | } | |
36c32ba2 | 334 | |
1238ee09 | 335 | /* Second, order by unit type */ |
93bab288 YW |
336 | d1 = strrchr(a->id, '.'); |
337 | d2 = strrchr(b->id, '.'); | |
36c32ba2 | 338 | if (d1 && d2) { |
f84190d8 LP |
339 | r = strcasecmp(d1, d2); |
340 | if (r != 0) | |
36c32ba2 LP |
341 | return r; |
342 | } | |
343 | ||
1238ee09 | 344 | /* Third, order by name */ |
93bab288 | 345 | return strcasecmp(a->id, b->id); |
36c32ba2 LP |
346 | } |
347 | ||
16484a8a ZJS |
348 | static const char* unit_type_suffix(const char *name) { |
349 | const char *dot; | |
350 | ||
351 | dot = strrchr(name, '.'); | |
352 | if (!dot) | |
353 | return ""; | |
354 | ||
355 | return dot + 1; | |
356 | } | |
357 | ||
d8fba7c6 | 358 | static bool output_show_unit(const UnitInfo *u, char **patterns) { |
f8591ee1 LP |
359 | assert(u); |
360 | ||
2404701e | 361 | if (!strv_fnmatch_or_empty(patterns, u->id, FNM_NOESCAPE)) |
d8fba7c6 | 362 | return false; |
d8fba7c6 | 363 | |
16484a8a ZJS |
364 | if (arg_types && !strv_find(arg_types, unit_type_suffix(u->id))) |
365 | return false; | |
6c71341a ZJS |
366 | |
367 | if (arg_all) | |
368 | return true; | |
369 | ||
409472cb FB |
370 | /* Note that '--all' is not purely a state filter, but also a |
371 | * filter that hides units that "follow" other units (which is | |
372 | * used for device units that appear under different names). */ | |
373 | if (!isempty(u->following)) | |
374 | return false; | |
375 | ||
ebc96265 FB |
376 | if (!strv_isempty(arg_states)) |
377 | return true; | |
378 | ||
379 | /* By default show all units except the ones in inactive | |
380 | * state and with no pending job */ | |
6c71341a ZJS |
381 | if (u->job_id > 0) |
382 | return true; | |
383 | ||
409472cb | 384 | if (streq(u->active_state, "inactive")) |
6c71341a ZJS |
385 | return false; |
386 | ||
387 | return true; | |
33330222 ZJS |
388 | } |
389 | ||
1238ee09 | 390 | static int output_units_list(const UnitInfo *unit_infos, unsigned c) { |
b5d7f1bb | 391 | unsigned circle_len = 0, id_len, max_id_len, load_len, active_len, sub_len, job_len, desc_len, max_desc_len; |
f459b602 | 392 | const UnitInfo *u; |
a1074881 | 393 | unsigned n_shown = 0; |
b5d7f1bb | 394 | int job_count = 0; |
0221d68a | 395 | bool full = arg_full || FLAGS_SET(arg_pager_flags, PAGER_DISABLE); |
33330222 | 396 | |
fbd0b64f LP |
397 | max_id_len = STRLEN("UNIT"); |
398 | load_len = STRLEN("LOAD"); | |
399 | active_len = STRLEN("ACTIVE"); | |
400 | sub_len = STRLEN("SUB"); | |
401 | job_len = STRLEN("JOB"); | |
402 | max_desc_len = STRLEN("DESCRIPTION"); | |
b036fc00 LP |
403 | |
404 | for (u = unit_infos; u < unit_infos + c; u++) { | |
1238ee09 | 405 | max_id_len = MAX(max_id_len, strlen(u->id) + (u->machine ? strlen(u->machine)+1 : 0)); |
a1074881 | 406 | load_len = MAX(load_len, strlen(u->load_state)); |
b036fc00 LP |
407 | active_len = MAX(active_len, strlen(u->active_state)); |
408 | sub_len = MAX(sub_len, strlen(u->sub_state)); | |
b5d7f1bb | 409 | max_desc_len = MAX(max_desc_len, strlen(u->description)); |
f459b602 | 410 | |
ccd41387 | 411 | if (u->job_id != 0) { |
b036fc00 | 412 | job_len = MAX(job_len, strlen(u->job_type)); |
ccd41387 ZJS |
413 | job_count++; |
414 | } | |
90c3f79d LP |
415 | |
416 | if (!arg_no_legend && | |
417 | (streq(u->active_state, "failed") || | |
c4555ad8 | 418 | STR_IN_SET(u->load_state, "error", "not-found", "bad-setting", "masked"))) |
90c3f79d | 419 | circle_len = 2; |
33330222 ZJS |
420 | } |
421 | ||
184ecaf7 | 422 | if (!arg_full && original_stdout_is_tty) { |
4deb3b93 | 423 | unsigned basic_len; |
cbc9fbd1 | 424 | |
b5d7f1bb | 425 | id_len = MIN(max_id_len, 25u); /* as much as it needs, but at most 25 for now */ |
a9bd056a | 426 | basic_len = circle_len + 1 + id_len + 1 + load_len + 1 + active_len + 1 + sub_len + 1; |
cbc9fbd1 | 427 | |
ccd41387 ZJS |
428 | if (job_count) |
429 | basic_len += job_len + 1; | |
cbc9fbd1 | 430 | |
4deb3b93 MS |
431 | if (basic_len < (unsigned) columns()) { |
432 | unsigned extra_len, incr; | |
433 | extra_len = columns() - basic_len; | |
cbc9fbd1 | 434 | |
4deb3b93 MS |
435 | /* Either UNIT already got 25, or is fully satisfied. |
436 | * Grant up to 25 to DESC now. */ | |
9607d947 | 437 | incr = MIN(extra_len, 25u); |
b5d7f1bb | 438 | desc_len = incr; |
4deb3b93 | 439 | extra_len -= incr; |
cbc9fbd1 | 440 | |
b5d7f1bb LP |
441 | /* Of the remainder give as much as the ID needs to the ID, and give the rest to the |
442 | * description but not more than it needs. */ | |
4deb3b93 | 443 | if (extra_len > 0) { |
b5d7f1bb | 444 | incr = MIN(max_id_len - id_len, extra_len); |
4deb3b93 | 445 | id_len += incr; |
b5d7f1bb | 446 | desc_len += MIN(extra_len - incr, max_desc_len - desc_len); |
4deb3b93 | 447 | } |
43479f8d ZJS |
448 | } else |
449 | desc_len = 0; | |
b5d7f1bb | 450 | } else { |
4deb3b93 | 451 | id_len = max_id_len; |
b5d7f1bb LP |
452 | desc_len = max_desc_len; |
453 | } | |
4deb3b93 | 454 | |
b036fc00 | 455 | for (u = unit_infos; u < unit_infos + c; u++) { |
1238ee09 | 456 | _cleanup_free_ char *e = NULL, *j = NULL; |
16484a8a | 457 | const char *on_underline = "", *off_underline = ""; |
90c3f79d LP |
458 | const char *on_loaded = "", *off_loaded = ""; |
459 | const char *on_active = "", *off_active = ""; | |
460 | const char *on_circle = "", *off_circle = ""; | |
1238ee09 | 461 | const char *id; |
16484a8a | 462 | bool circle = false, underline = false; |
b036fc00 | 463 | |
ad94ad63 | 464 | if (!n_shown && !arg_no_legend) { |
90c3f79d LP |
465 | |
466 | if (circle_len > 0) | |
467 | fputs(" ", stdout); | |
468 | ||
16484a8a ZJS |
469 | printf("%s%-*s %-*s %-*s %-*s ", |
470 | ansi_underline(), | |
cbc9fbd1 LP |
471 | id_len, "UNIT", |
472 | load_len, "LOAD", | |
473 | active_len, "ACTIVE", | |
474 | sub_len, "SUB"); | |
475 | ||
ccd41387 ZJS |
476 | if (job_count) |
477 | printf("%-*s ", job_len, "JOB"); | |
cbc9fbd1 | 478 | |
b5d7f1bb LP |
479 | printf("%-*.*s%s\n", |
480 | desc_len, | |
0221d68a | 481 | full ? -1 : (int) desc_len, |
16484a8a ZJS |
482 | "DESCRIPTION", |
483 | ansi_normal()); | |
ad94ad63 ZJS |
484 | } |
485 | ||
688c6725 LP |
486 | n_shown++; |
487 | ||
16484a8a ZJS |
488 | if (u + 1 < unit_infos + c && |
489 | !streq(unit_type_suffix(u->id), unit_type_suffix((u + 1)->id))) { | |
490 | on_underline = ansi_underline(); | |
491 | off_underline = ansi_normal(); | |
492 | underline = true; | |
493 | } | |
494 | ||
c4555ad8 | 495 | if (STR_IN_SET(u->load_state, "error", "not-found", "bad-setting", "masked") && !arg_plain) { |
90c3f79d | 496 | on_circle = ansi_highlight_yellow(); |
16484a8a | 497 | off_circle = ansi_normal(); |
90c3f79d | 498 | circle = true; |
16484a8a | 499 | on_loaded = underline ? ansi_highlight_red_underline() : ansi_highlight_red(); |
835a19e0 | 500 | off_loaded = underline ? on_underline : ansi_normal(); |
250ba664 | 501 | } else if (streq(u->active_state, "failed") && !arg_plain) { |
16484a8a ZJS |
502 | on_circle = ansi_highlight_red(); |
503 | off_circle = ansi_normal(); | |
90c3f79d | 504 | circle = true; |
16484a8a | 505 | on_active = underline ? ansi_highlight_red_underline() : ansi_highlight_red(); |
835a19e0 | 506 | off_active = underline ? on_underline : ansi_normal(); |
90c3f79d | 507 | } |
eb68c413 | 508 | |
1238ee09 | 509 | if (u->machine) { |
605405c6 | 510 | j = strjoin(u->machine, ":", u->id); |
1238ee09 LP |
511 | if (!j) |
512 | return log_oom(); | |
513 | ||
514 | id = j; | |
515 | } else | |
516 | id = u->id; | |
517 | ||
518 | if (arg_full) { | |
519 | e = ellipsize(id, id_len, 33); | |
520 | if (!e) | |
521 | return log_oom(); | |
522 | ||
523 | id = e; | |
524 | } | |
eb68c413 | 525 | |
90c3f79d | 526 | if (circle_len > 0) |
9a6f746f | 527 | printf("%s%s%s ", on_circle, circle ? special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE) : " ", off_circle); |
90c3f79d | 528 | |
16484a8a ZJS |
529 | printf("%s%s%-*s%s %s%-*s%s %s%-*s %-*s%s %-*s", |
530 | on_underline, | |
90c3f79d | 531 | on_active, id_len, id, off_active, |
a1074881 | 532 | on_loaded, load_len, u->load_state, off_loaded, |
b036fc00 LP |
533 | on_active, active_len, u->active_state, |
534 | sub_len, u->sub_state, off_active, | |
ccd41387 | 535 | job_count ? job_len + 1 : 0, u->job_id ? u->job_type : ""); |
cbc9fbd1 | 536 | |
b5d7f1bb LP |
537 | printf("%-*.*s%s\n", |
538 | desc_len, | |
0221d68a | 539 | full ? -1 : (int) desc_len, |
16484a8a ZJS |
540 | u->description, |
541 | off_underline); | |
eb68c413 ZJS |
542 | } |
543 | ||
ebed32bf | 544 | if (!arg_no_legend) { |
57f7ae4f ZJS |
545 | const char *on, *off; |
546 | ||
547 | if (n_shown) { | |
90c3f79d LP |
548 | puts("\n" |
549 | "LOAD = Reflects whether the unit definition was properly loaded.\n" | |
e3e0314b ZJS |
550 | "ACTIVE = The high-level unit activation state, i.e. generalization of SUB.\n" |
551 | "SUB = The low-level unit activation state, values depend on unit type."); | |
552 | puts(job_count ? "JOB = Pending job for the unit.\n" : ""); | |
0b5a519c | 553 | on = ansi_highlight(); |
1fc464f6 | 554 | off = ansi_normal(); |
57f7ae4f | 555 | } else { |
0b5a519c | 556 | on = ansi_highlight_red(); |
1fc464f6 | 557 | off = ansi_normal(); |
57f7ae4f | 558 | } |
eb68c413 ZJS |
559 | |
560 | if (arg_all) | |
48c2826b | 561 | printf("%s%u loaded units listed.%s\n" |
57f7ae4f ZJS |
562 | "To show all installed unit files use 'systemctl list-unit-files'.\n", |
563 | on, n_shown, off); | |
eb68c413 | 564 | else |
48c2826b | 565 | printf("%s%u loaded units listed.%s Pass --all to see loaded but inactive units, too.\n" |
57f7ae4f ZJS |
566 | "To show all installed unit files use 'systemctl list-unit-files'.\n", |
567 | on, n_shown, off); | |
eb68c413 | 568 | } |
1238ee09 LP |
569 | |
570 | return 0; | |
eb68c413 ZJS |
571 | } |
572 | ||
a00963a2 | 573 | static int get_unit_list( |
f459b602 | 574 | sd_bus *bus, |
1238ee09 LP |
575 | const char *machine, |
576 | char **patterns, | |
577 | UnitInfo **unit_infos, | |
578 | int c, | |
579 | sd_bus_message **_reply) { | |
a00963a2 | 580 | |
4afd3348 LP |
581 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
582 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
583 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
ca2d3784 | 584 | size_t size = c; |
1238ee09 | 585 | int r; |
f459b602 | 586 | UnitInfo u; |
313fe66f | 587 | bool fallback = false; |
7e4249b9 | 588 | |
265a7a2a | 589 | assert(bus); |
1238ee09 | 590 | assert(unit_infos); |
f459b602 | 591 | assert(_reply); |
1238ee09 | 592 | |
cdc06ed7 | 593 | r = sd_bus_message_new_method_call( |
f22f08cd | 594 | bus, |
cdc06ed7 | 595 | &m, |
f22f08cd SP |
596 | "org.freedesktop.systemd1", |
597 | "/org/freedesktop/systemd1", | |
598 | "org.freedesktop.systemd1.Manager", | |
313fe66f | 599 | "ListUnitsByPatterns"); |
cdc06ed7 DS |
600 | if (r < 0) |
601 | return bus_log_create_error(r); | |
602 | ||
603 | r = sd_bus_message_append_strv(m, arg_states); | |
604 | if (r < 0) | |
605 | return bus_log_create_error(r); | |
606 | ||
313fe66f | 607 | r = sd_bus_message_append_strv(m, patterns); |
608 | if (r < 0) | |
609 | return bus_log_create_error(r); | |
610 | ||
cdc06ed7 | 611 | r = sd_bus_call(bus, m, 0, &error, &reply); |
33d52725 ZJS |
612 | if (r < 0 && (sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_METHOD) || |
613 | sd_bus_error_has_name(&error, SD_BUS_ERROR_ACCESS_DENIED))) { | |
313fe66f | 614 | /* Fallback to legacy ListUnitsFiltered method */ |
615 | fallback = true; | |
616 | log_debug_errno(r, "Failed to list units: %s Falling back to ListUnitsFiltered method.", bus_error_message(&error, r)); | |
617 | m = sd_bus_message_unref(m); | |
618 | sd_bus_error_free(&error); | |
619 | ||
620 | r = sd_bus_message_new_method_call( | |
621 | bus, | |
622 | &m, | |
623 | "org.freedesktop.systemd1", | |
624 | "/org/freedesktop/systemd1", | |
625 | "org.freedesktop.systemd1.Manager", | |
626 | "ListUnitsFiltered"); | |
627 | if (r < 0) | |
628 | return bus_log_create_error(r); | |
629 | ||
630 | r = sd_bus_message_append_strv(m, arg_states); | |
631 | if (r < 0) | |
632 | return bus_log_create_error(r); | |
633 | ||
634 | r = sd_bus_call(bus, m, 0, &error, &reply); | |
635 | } | |
4c3e8e39 LP |
636 | if (r < 0) |
637 | return log_error_errno(r, "Failed to list units: %s", bus_error_message(&error, r)); | |
7e4249b9 | 638 | |
f459b602 MAP |
639 | r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssssouso)"); |
640 | if (r < 0) | |
641 | return bus_log_parse_error(r); | |
7e4249b9 | 642 | |
f459b602 | 643 | while ((r = bus_parse_unit_info(reply, &u)) > 0) { |
1238ee09 LP |
644 | u.machine = machine; |
645 | ||
313fe66f | 646 | if (!output_show_unit(&u, fallback ? patterns : NULL)) |
8d5ba5a9 | 647 | continue; |
7e4249b9 | 648 | |
1238ee09 | 649 | if (!GREEDY_REALLOC(*unit_infos, size, c+1)) |
f459b602 | 650 | return log_oom(); |
36c32ba2 | 651 | |
1238ee09 | 652 | (*unit_infos)[c++] = u; |
991f2a39 | 653 | } |
f459b602 MAP |
654 | if (r < 0) |
655 | return bus_log_parse_error(r); | |
991f2a39 | 656 | |
f459b602 MAP |
657 | r = sd_bus_message_exit_container(reply); |
658 | if (r < 0) | |
659 | return bus_log_parse_error(r); | |
660 | ||
1cc6c93a | 661 | *_reply = TAKE_PTR(reply); |
f459b602 | 662 | |
1238ee09 LP |
663 | return c; |
664 | } | |
665 | ||
666 | static void message_set_freep(Set **set) { | |
224b0e7a | 667 | set_free_with_destructor(*set, sd_bus_message_unref); |
1238ee09 LP |
668 | } |
669 | ||
670 | static int get_unit_list_recursive( | |
671 | sd_bus *bus, | |
672 | char **patterns, | |
673 | UnitInfo **_unit_infos, | |
674 | Set **_replies, | |
675 | char ***_machines) { | |
676 | ||
677 | _cleanup_free_ UnitInfo *unit_infos = NULL; | |
678 | _cleanup_(message_set_freep) Set *replies; | |
679 | sd_bus_message *reply; | |
680 | int c, r; | |
681 | ||
682 | assert(bus); | |
683 | assert(_replies); | |
684 | assert(_unit_infos); | |
685 | assert(_machines); | |
686 | ||
d5099efc | 687 | replies = set_new(NULL); |
1238ee09 LP |
688 | if (!replies) |
689 | return log_oom(); | |
690 | ||
691 | c = get_unit_list(bus, NULL, patterns, &unit_infos, 0, &reply); | |
692 | if (c < 0) | |
693 | return c; | |
694 | ||
695 | r = set_put(replies, reply); | |
696 | if (r < 0) { | |
697 | sd_bus_message_unref(reply); | |
4c3e8e39 | 698 | return log_oom(); |
1238ee09 LP |
699 | } |
700 | ||
701 | if (arg_recursive) { | |
702 | _cleanup_strv_free_ char **machines = NULL; | |
703 | char **i; | |
704 | ||
705 | r = sd_get_machine_names(&machines); | |
706 | if (r < 0) | |
4c3e8e39 | 707 | return log_error_errno(r, "Failed to get machine names: %m"); |
1238ee09 LP |
708 | |
709 | STRV_FOREACH(i, machines) { | |
4afd3348 | 710 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *container = NULL; |
1238ee09 LP |
711 | int k; |
712 | ||
de33fc62 | 713 | r = sd_bus_open_system_machine(&container, *i); |
1238ee09 | 714 | if (r < 0) { |
4c3e8e39 | 715 | log_warning_errno(r, "Failed to connect to container %s, ignoring: %m", *i); |
1238ee09 LP |
716 | continue; |
717 | } | |
718 | ||
719 | k = get_unit_list(container, *i, patterns, &unit_infos, c, &reply); | |
720 | if (k < 0) | |
721 | return k; | |
722 | ||
723 | c = k; | |
724 | ||
725 | r = set_put(replies, reply); | |
726 | if (r < 0) { | |
727 | sd_bus_message_unref(reply); | |
4c3e8e39 | 728 | return log_oom(); |
1238ee09 LP |
729 | } |
730 | } | |
731 | ||
1cc6c93a | 732 | *_machines = TAKE_PTR(machines); |
1238ee09 LP |
733 | } else |
734 | *_machines = NULL; | |
735 | ||
1cc6c93a | 736 | *_unit_infos = TAKE_PTR(unit_infos); |
1cc6c93a | 737 | *_replies = TAKE_PTR(replies); |
1238ee09 | 738 | |
f459b602 | 739 | return c; |
991f2a39 ZJS |
740 | } |
741 | ||
e449de87 | 742 | static int list_units(int argc, char *argv[], void *userdata) { |
f459b602 | 743 | _cleanup_free_ UnitInfo *unit_infos = NULL; |
1238ee09 LP |
744 | _cleanup_(message_set_freep) Set *replies = NULL; |
745 | _cleanup_strv_free_ char **machines = NULL; | |
4fbd7192 | 746 | sd_bus *bus; |
991f2a39 ZJS |
747 | int r; |
748 | ||
4fbd7192 LP |
749 | r = acquire_bus(BUS_MANAGER, &bus); |
750 | if (r < 0) | |
751 | return r; | |
752 | ||
0221d68a | 753 | (void) pager_open(arg_pager_flags); |
d2ad7e1b | 754 | |
e449de87 | 755 | r = get_unit_list_recursive(bus, strv_skip(argv, 1), &unit_infos, &replies, &machines); |
991f2a39 ZJS |
756 | if (r < 0) |
757 | return r; | |
758 | ||
93bab288 | 759 | typesafe_qsort(unit_infos, r, compare_unit_info); |
1238ee09 | 760 | return output_units_list(unit_infos, r); |
991f2a39 ZJS |
761 | } |
762 | ||
a00963a2 | 763 | static int get_triggered_units( |
f459b602 MAP |
764 | sd_bus *bus, |
765 | const char* path, | |
766 | char*** ret) { | |
a00963a2 | 767 | |
4afd3348 | 768 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
991f2a39 ZJS |
769 | int r; |
770 | ||
4fbd7192 LP |
771 | assert(bus); |
772 | assert(path); | |
773 | assert(ret); | |
774 | ||
f459b602 MAP |
775 | r = sd_bus_get_property_strv( |
776 | bus, | |
777 | "org.freedesktop.systemd1", | |
778 | path, | |
779 | "org.freedesktop.systemd1.Unit", | |
780 | "Triggers", | |
781 | &error, | |
782 | ret); | |
f459b602 | 783 | if (r < 0) |
691395d8 | 784 | return log_error_errno(r, "Failed to determine triggers: %s", bus_error_message(&error, r)); |
991f2a39 ZJS |
785 | |
786 | return 0; | |
787 | } | |
788 | ||
f459b602 MAP |
789 | static int get_listening( |
790 | sd_bus *bus, | |
791 | const char* unit_path, | |
cbb76c29 | 792 | char*** listening) { |
f459b602 | 793 | |
4afd3348 LP |
794 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
795 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
f459b602 | 796 | const char *type, *path; |
cbb76c29 | 797 | int r, n = 0; |
991f2a39 | 798 | |
f459b602 MAP |
799 | r = sd_bus_get_property( |
800 | bus, | |
801 | "org.freedesktop.systemd1", | |
802 | unit_path, | |
803 | "org.freedesktop.systemd1.Socket", | |
804 | "Listen", | |
805 | &error, | |
806 | &reply, | |
807 | "a(ss)"); | |
691395d8 LP |
808 | if (r < 0) |
809 | return log_error_errno(r, "Failed to get list of listening sockets: %s", bus_error_message(&error, r)); | |
991f2a39 | 810 | |
f459b602 MAP |
811 | r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ss)"); |
812 | if (r < 0) | |
813 | return bus_log_parse_error(r); | |
991f2a39 | 814 | |
f459b602 | 815 | while ((r = sd_bus_message_read(reply, "(ss)", &type, &path)) > 0) { |
36c32ba2 | 816 | |
0d95178e | 817 | r = strv_extend(listening, type); |
f459b602 MAP |
818 | if (r < 0) |
819 | return log_oom(); | |
991f2a39 | 820 | |
0d95178e | 821 | r = strv_extend(listening, path); |
f459b602 MAP |
822 | if (r < 0) |
823 | return log_oom(); | |
7e4249b9 | 824 | |
cbb76c29 | 825 | n++; |
36c32ba2 | 826 | } |
f459b602 MAP |
827 | if (r < 0) |
828 | return bus_log_parse_error(r); | |
829 | ||
830 | r = sd_bus_message_exit_container(reply); | |
831 | if (r < 0) | |
832 | return bus_log_parse_error(r); | |
36c32ba2 | 833 | |
cbb76c29 | 834 | return n; |
991f2a39 ZJS |
835 | } |
836 | ||
837 | struct socket_info { | |
0cfc3525 | 838 | const char *machine; |
991f2a39 ZJS |
839 | const char* id; |
840 | ||
841 | char* type; | |
842 | char* path; | |
843 | ||
844 | /* Note: triggered is a list here, although it almost certainly | |
845 | * will always be one unit. Nevertheless, dbus API allows for multiple | |
f7340ab2 | 846 | * values, so let's follow that. */ |
991f2a39 ZJS |
847 | char** triggered; |
848 | ||
849 | /* The strv above is shared. free is set only in the first one. */ | |
850 | bool own_triggered; | |
851 | }; | |
852 | ||
cbb76c29 | 853 | static int socket_info_compare(const struct socket_info *a, const struct socket_info *b) { |
93bab288 | 854 | int r; |
cbc9fbd1 | 855 | |
cbb76c29 LP |
856 | assert(a); |
857 | assert(b); | |
858 | ||
0cfc3525 TA |
859 | if (!a->machine && b->machine) |
860 | return -1; | |
861 | if (a->machine && !b->machine) | |
862 | return 1; | |
863 | if (a->machine && b->machine) { | |
93bab288 YW |
864 | r = strcasecmp(a->machine, b->machine); |
865 | if (r != 0) | |
866 | return r; | |
0cfc3525 TA |
867 | } |
868 | ||
93bab288 YW |
869 | r = strcmp(a->path, b->path); |
870 | if (r == 0) | |
871 | r = strcmp(a->type, b->type); | |
cbc9fbd1 | 872 | |
93bab288 | 873 | return r; |
991f2a39 ZJS |
874 | } |
875 | ||
876 | static int output_sockets_list(struct socket_info *socket_infos, unsigned cs) { | |
877 | struct socket_info *s; | |
fbd0b64f LP |
878 | unsigned pathlen = STRLEN("LISTEN"), |
879 | typelen = STRLEN("TYPE") * arg_show_types, | |
880 | socklen = STRLEN("UNIT"), | |
881 | servlen = STRLEN("ACTIVATES"); | |
991f2a39 ZJS |
882 | const char *on, *off; |
883 | ||
884 | for (s = socket_infos; s < socket_infos + cs; s++) { | |
991f2a39 | 885 | unsigned tmp = 0; |
cbc9fbd1 | 886 | char **a; |
991f2a39 ZJS |
887 | |
888 | socklen = MAX(socklen, strlen(s->id)); | |
889 | if (arg_show_types) | |
890 | typelen = MAX(typelen, strlen(s->type)); | |
0cfc3525 | 891 | pathlen = MAX(pathlen, strlen(s->path) + (s->machine ? strlen(s->machine)+1 : 0)); |
991f2a39 ZJS |
892 | |
893 | STRV_FOREACH(a, s->triggered) | |
894 | tmp += strlen(*a) + 2*(a != s->triggered); | |
895 | servlen = MAX(servlen, tmp); | |
896 | } | |
897 | ||
898 | if (cs) { | |
571bfc6c MM |
899 | if (!arg_no_legend) |
900 | printf("%-*s %-*.*s%-*s %s\n", | |
901 | pathlen, "LISTEN", | |
902 | typelen + arg_show_types, typelen + arg_show_types, "TYPE ", | |
903 | socklen, "UNIT", | |
904 | "ACTIVATES"); | |
991f2a39 ZJS |
905 | |
906 | for (s = socket_infos; s < socket_infos + cs; s++) { | |
0cfc3525 TA |
907 | _cleanup_free_ char *j = NULL; |
908 | const char *path; | |
991f2a39 ZJS |
909 | char **a; |
910 | ||
0cfc3525 | 911 | if (s->machine) { |
605405c6 | 912 | j = strjoin(s->machine, ":", s->path); |
0cfc3525 TA |
913 | if (!j) |
914 | return log_oom(); | |
915 | path = j; | |
916 | } else | |
917 | path = s->path; | |
918 | ||
991f2a39 ZJS |
919 | if (arg_show_types) |
920 | printf("%-*s %-*s %-*s", | |
0cfc3525 | 921 | pathlen, path, typelen, s->type, socklen, s->id); |
991f2a39 ZJS |
922 | else |
923 | printf("%-*s %-*s", | |
0cfc3525 | 924 | pathlen, path, socklen, s->id); |
991f2a39 ZJS |
925 | STRV_FOREACH(a, s->triggered) |
926 | printf("%s %s", | |
927 | a == s->triggered ? "" : ",", *a); | |
928 | printf("\n"); | |
929 | } | |
930 | ||
0b5a519c | 931 | on = ansi_highlight(); |
1fc464f6 | 932 | off = ansi_normal(); |
571bfc6c MM |
933 | if (!arg_no_legend) |
934 | printf("\n"); | |
991f2a39 | 935 | } else { |
0b5a519c | 936 | on = ansi_highlight_red(); |
1fc464f6 | 937 | off = ansi_normal(); |
991f2a39 ZJS |
938 | } |
939 | ||
571bfc6c MM |
940 | if (!arg_no_legend) { |
941 | printf("%s%u sockets listed.%s\n", on, cs, off); | |
942 | if (!arg_all) | |
943 | printf("Pass --all to see loaded but inactive sockets, too.\n"); | |
944 | } | |
265a7a2a ZJS |
945 | |
946 | return 0; | |
947 | } | |
948 | ||
e449de87 | 949 | static int list_sockets(int argc, char *argv[], void *userdata) { |
0cfc3525 TA |
950 | _cleanup_(message_set_freep) Set *replies = NULL; |
951 | _cleanup_strv_free_ char **machines = NULL; | |
f459b602 | 952 | _cleanup_free_ UnitInfo *unit_infos = NULL; |
8d5ba5a9 | 953 | _cleanup_free_ struct socket_info *socket_infos = NULL; |
f459b602 | 954 | const UnitInfo *u; |
991f2a39 | 955 | struct socket_info *s; |
cbb76c29 | 956 | unsigned cs = 0; |
991f2a39 | 957 | size_t size = 0; |
ad83b4c4 | 958 | int r = 0, n; |
4fbd7192 | 959 | sd_bus *bus; |
265a7a2a | 960 | |
4fbd7192 LP |
961 | r = acquire_bus(BUS_MANAGER, &bus); |
962 | if (r < 0) | |
963 | return r; | |
964 | ||
0221d68a | 965 | (void) pager_open(arg_pager_flags); |
d2ad7e1b | 966 | |
e449de87 | 967 | n = get_unit_list_recursive(bus, strv_skip(argv, 1), &unit_infos, &replies, &machines); |
cbb76c29 LP |
968 | if (n < 0) |
969 | return n; | |
265a7a2a | 970 | |
cbb76c29 | 971 | for (u = unit_infos; u < unit_infos + n; u++) { |
0d95178e | 972 | _cleanup_strv_free_ char **listening = NULL, **triggered = NULL; |
cbb76c29 | 973 | int i, c; |
991f2a39 | 974 | |
cbc9fbd1 | 975 | if (!endswith(u->id, ".socket")) |
991f2a39 ZJS |
976 | continue; |
977 | ||
978 | r = get_triggered_units(bus, u->unit_path, &triggered); | |
979 | if (r < 0) | |
980 | goto cleanup; | |
981 | ||
cbb76c29 LP |
982 | c = get_listening(bus, u->unit_path, &listening); |
983 | if (c < 0) { | |
984 | r = c; | |
991f2a39 | 985 | goto cleanup; |
cbb76c29 | 986 | } |
991f2a39 ZJS |
987 | |
988 | if (!GREEDY_REALLOC(socket_infos, size, cs + c)) { | |
989 | r = log_oom(); | |
990 | goto cleanup; | |
991 | } | |
992 | ||
993 | for (i = 0; i < c; i++) | |
994 | socket_infos[cs + i] = (struct socket_info) { | |
0cfc3525 | 995 | .machine = u->machine, |
991f2a39 | 996 | .id = u->id, |
0d95178e KS |
997 | .type = listening[i*2], |
998 | .path = listening[i*2 + 1], | |
991f2a39 ZJS |
999 | .triggered = triggered, |
1000 | .own_triggered = i==0, | |
1001 | }; | |
1002 | ||
1003 | /* from this point on we will cleanup those socket_infos */ | |
1004 | cs += c; | |
0d95178e KS |
1005 | free(listening); |
1006 | listening = triggered = NULL; /* avoid cleanup */ | |
991f2a39 ZJS |
1007 | } |
1008 | ||
93bab288 | 1009 | typesafe_qsort(socket_infos, cs, socket_info_compare); |
991f2a39 ZJS |
1010 | |
1011 | output_sockets_list(socket_infos, cs); | |
1012 | ||
1013 | cleanup: | |
1014 | assert(cs == 0 || socket_infos); | |
1015 | for (s = socket_infos; s < socket_infos + cs; s++) { | |
1016 | free(s->type); | |
1017 | free(s->path); | |
1018 | if (s->own_triggered) | |
1019 | strv_free(s->triggered); | |
1020 | } | |
4445a875 | 1021 | |
872c8faa | 1022 | return r; |
4445a875 LP |
1023 | } |
1024 | ||
cbb76c29 LP |
1025 | static int get_next_elapse( |
1026 | sd_bus *bus, | |
1027 | const char *path, | |
1028 | dual_timestamp *next) { | |
1029 | ||
4afd3348 | 1030 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
cbb76c29 LP |
1031 | dual_timestamp t; |
1032 | int r; | |
1033 | ||
1034 | assert(bus); | |
1035 | assert(path); | |
1036 | assert(next); | |
1037 | ||
1038 | r = sd_bus_get_property_trivial( | |
1039 | bus, | |
1040 | "org.freedesktop.systemd1", | |
1041 | path, | |
1042 | "org.freedesktop.systemd1.Timer", | |
1043 | "NextElapseUSecMonotonic", | |
1044 | &error, | |
1045 | 't', | |
1046 | &t.monotonic); | |
691395d8 | 1047 | if (r < 0) |
021723c6 | 1048 | return log_error_errno(r, "Failed to get next elapse time: %s", bus_error_message(&error, r)); |
cbb76c29 LP |
1049 | |
1050 | r = sd_bus_get_property_trivial( | |
1051 | bus, | |
1052 | "org.freedesktop.systemd1", | |
1053 | path, | |
1054 | "org.freedesktop.systemd1.Timer", | |
1055 | "NextElapseUSecRealtime", | |
1056 | &error, | |
1057 | 't', | |
1058 | &t.realtime); | |
691395d8 | 1059 | if (r < 0) |
021723c6 | 1060 | return log_error_errno(r, "Failed to get next elapse time: %s", bus_error_message(&error, r)); |
cbb76c29 LP |
1061 | |
1062 | *next = t; | |
1063 | return 0; | |
1064 | } | |
1065 | ||
d784e2db LP |
1066 | static int get_last_trigger( |
1067 | sd_bus *bus, | |
1068 | const char *path, | |
1069 | usec_t *last) { | |
1070 | ||
4afd3348 | 1071 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
d784e2db LP |
1072 | int r; |
1073 | ||
1074 | assert(bus); | |
1075 | assert(path); | |
1076 | assert(last); | |
1077 | ||
1078 | r = sd_bus_get_property_trivial( | |
1079 | bus, | |
1080 | "org.freedesktop.systemd1", | |
1081 | path, | |
1082 | "org.freedesktop.systemd1.Timer", | |
dedabea4 | 1083 | "LastTriggerUSec", |
d784e2db LP |
1084 | &error, |
1085 | 't', | |
1086 | last); | |
691395d8 LP |
1087 | if (r < 0) |
1088 | return log_error_errno(r, "Failed to get last trigger time: %s", bus_error_message(&error, r)); | |
d784e2db LP |
1089 | |
1090 | return 0; | |
1091 | } | |
1092 | ||
cbb76c29 | 1093 | struct timer_info { |
806a37e7 | 1094 | const char* machine; |
cbb76c29 LP |
1095 | const char* id; |
1096 | usec_t next_elapse; | |
d784e2db | 1097 | usec_t last_trigger; |
cbb76c29 LP |
1098 | char** triggered; |
1099 | }; | |
1100 | ||
1101 | static int timer_info_compare(const struct timer_info *a, const struct timer_info *b) { | |
93bab288 | 1102 | int r; |
806a37e7 | 1103 | |
cbb76c29 LP |
1104 | assert(a); |
1105 | assert(b); | |
1106 | ||
806a37e7 TA |
1107 | if (!a->machine && b->machine) |
1108 | return -1; | |
1109 | if (a->machine && !b->machine) | |
1110 | return 1; | |
1111 | if (a->machine && b->machine) { | |
93bab288 YW |
1112 | r = strcasecmp(a->machine, b->machine); |
1113 | if (r != 0) | |
1114 | return r; | |
806a37e7 TA |
1115 | } |
1116 | ||
93bab288 YW |
1117 | r = CMP(a->next_elapse, b->next_elapse); |
1118 | if (r != 0) | |
1119 | return r; | |
cbb76c29 LP |
1120 | |
1121 | return strcmp(a->id, b->id); | |
1122 | } | |
1123 | ||
1124 | static int output_timers_list(struct timer_info *timer_infos, unsigned n) { | |
1125 | struct timer_info *t; | |
1126 | unsigned | |
fbd0b64f LP |
1127 | nextlen = STRLEN("NEXT"), |
1128 | leftlen = STRLEN("LEFT"), | |
1129 | lastlen = STRLEN("LAST"), | |
1130 | passedlen = STRLEN("PASSED"), | |
1131 | unitlen = STRLEN("UNIT"), | |
1132 | activatelen = STRLEN("ACTIVATES"); | |
cbb76c29 LP |
1133 | |
1134 | const char *on, *off; | |
1135 | ||
1136 | assert(timer_infos || n == 0); | |
1137 | ||
1138 | for (t = timer_infos; t < timer_infos + n; t++) { | |
1139 | unsigned ul = 0; | |
1140 | char **a; | |
1141 | ||
1142 | if (t->next_elapse > 0) { | |
1143 | char tstamp[FORMAT_TIMESTAMP_MAX] = "", trel[FORMAT_TIMESTAMP_RELATIVE_MAX] = ""; | |
1144 | ||
1145 | format_timestamp(tstamp, sizeof(tstamp), t->next_elapse); | |
1146 | nextlen = MAX(nextlen, strlen(tstamp) + 1); | |
1147 | ||
1148 | format_timestamp_relative(trel, sizeof(trel), t->next_elapse); | |
1149 | leftlen = MAX(leftlen, strlen(trel)); | |
1150 | } | |
1151 | ||
d784e2db LP |
1152 | if (t->last_trigger > 0) { |
1153 | char tstamp[FORMAT_TIMESTAMP_MAX] = "", trel[FORMAT_TIMESTAMP_RELATIVE_MAX] = ""; | |
1154 | ||
1155 | format_timestamp(tstamp, sizeof(tstamp), t->last_trigger); | |
1156 | lastlen = MAX(lastlen, strlen(tstamp) + 1); | |
1157 | ||
1158 | format_timestamp_relative(trel, sizeof(trel), t->last_trigger); | |
1159 | passedlen = MAX(passedlen, strlen(trel)); | |
1160 | } | |
1161 | ||
806a37e7 | 1162 | unitlen = MAX(unitlen, strlen(t->id) + (t->machine ? strlen(t->machine)+1 : 0)); |
cbb76c29 LP |
1163 | |
1164 | STRV_FOREACH(a, t->triggered) | |
1165 | ul += strlen(*a) + 2*(a != t->triggered); | |
d784e2db | 1166 | |
cbb76c29 LP |
1167 | activatelen = MAX(activatelen, ul); |
1168 | } | |
1169 | ||
1170 | if (n > 0) { | |
1171 | if (!arg_no_legend) | |
d784e2db LP |
1172 | printf("%-*s %-*s %-*s %-*s %-*s %s\n", |
1173 | nextlen, "NEXT", | |
1174 | leftlen, "LEFT", | |
1175 | lastlen, "LAST", | |
1176 | passedlen, "PASSED", | |
1177 | unitlen, "UNIT", | |
1178 | "ACTIVATES"); | |
cbb76c29 LP |
1179 | |
1180 | for (t = timer_infos; t < timer_infos + n; t++) { | |
806a37e7 TA |
1181 | _cleanup_free_ char *j = NULL; |
1182 | const char *unit; | |
d784e2db LP |
1183 | char tstamp1[FORMAT_TIMESTAMP_MAX] = "n/a", trel1[FORMAT_TIMESTAMP_RELATIVE_MAX] = "n/a"; |
1184 | char tstamp2[FORMAT_TIMESTAMP_MAX] = "n/a", trel2[FORMAT_TIMESTAMP_RELATIVE_MAX] = "n/a"; | |
cbb76c29 LP |
1185 | char **a; |
1186 | ||
d784e2db LP |
1187 | format_timestamp(tstamp1, sizeof(tstamp1), t->next_elapse); |
1188 | format_timestamp_relative(trel1, sizeof(trel1), t->next_elapse); | |
cbb76c29 | 1189 | |
d784e2db LP |
1190 | format_timestamp(tstamp2, sizeof(tstamp2), t->last_trigger); |
1191 | format_timestamp_relative(trel2, sizeof(trel2), t->last_trigger); | |
1192 | ||
806a37e7 | 1193 | if (t->machine) { |
605405c6 | 1194 | j = strjoin(t->machine, ":", t->id); |
806a37e7 TA |
1195 | if (!j) |
1196 | return log_oom(); | |
1197 | unit = j; | |
1198 | } else | |
1199 | unit = t->id; | |
1200 | ||
d784e2db | 1201 | printf("%-*s %-*s %-*s %-*s %-*s", |
806a37e7 | 1202 | nextlen, tstamp1, leftlen, trel1, lastlen, tstamp2, passedlen, trel2, unitlen, unit); |
cbb76c29 LP |
1203 | |
1204 | STRV_FOREACH(a, t->triggered) | |
1205 | printf("%s %s", | |
1206 | a == t->triggered ? "" : ",", *a); | |
1207 | printf("\n"); | |
1208 | } | |
1209 | ||
1210 | on = ansi_highlight(); | |
1fc464f6 | 1211 | off = ansi_normal(); |
cbb76c29 LP |
1212 | if (!arg_no_legend) |
1213 | printf("\n"); | |
1214 | } else { | |
1215 | on = ansi_highlight_red(); | |
1fc464f6 | 1216 | off = ansi_normal(); |
cbb76c29 LP |
1217 | } |
1218 | ||
1219 | if (!arg_no_legend) { | |
1220 | printf("%s%u timers listed.%s\n", on, n, off); | |
1221 | if (!arg_all) | |
1222 | printf("Pass --all to see loaded but inactive timers, too.\n"); | |
1223 | } | |
1224 | ||
1225 | return 0; | |
1226 | } | |
1227 | ||
f5080e73 DH |
1228 | static usec_t calc_next_elapse(dual_timestamp *nw, dual_timestamp *next) { |
1229 | usec_t next_elapse; | |
1230 | ||
1231 | assert(nw); | |
1232 | assert(next); | |
1233 | ||
1f65fd49 | 1234 | if (timestamp_is_set(next->monotonic)) { |
f5080e73 DH |
1235 | usec_t converted; |
1236 | ||
1237 | if (next->monotonic > nw->monotonic) | |
1238 | converted = nw->realtime + (next->monotonic - nw->monotonic); | |
1239 | else | |
1240 | converted = nw->realtime - (nw->monotonic - next->monotonic); | |
1241 | ||
1f65fd49 | 1242 | if (timestamp_is_set(next->realtime)) |
f5080e73 DH |
1243 | next_elapse = MIN(converted, next->realtime); |
1244 | else | |
1245 | next_elapse = converted; | |
1246 | ||
1247 | } else | |
1248 | next_elapse = next->realtime; | |
1249 | ||
1250 | return next_elapse; | |
1251 | } | |
1252 | ||
e449de87 | 1253 | static int list_timers(int argc, char *argv[], void *userdata) { |
806a37e7 TA |
1254 | _cleanup_(message_set_freep) Set *replies = NULL; |
1255 | _cleanup_strv_free_ char **machines = NULL; | |
cbb76c29 LP |
1256 | _cleanup_free_ struct timer_info *timer_infos = NULL; |
1257 | _cleanup_free_ UnitInfo *unit_infos = NULL; | |
1258 | struct timer_info *t; | |
1259 | const UnitInfo *u; | |
1260 | size_t size = 0; | |
1823b86e | 1261 | int n, c = 0; |
cbb76c29 | 1262 | dual_timestamp nw; |
4fbd7192 | 1263 | sd_bus *bus; |
1823b86e | 1264 | int r = 0; |
cbb76c29 | 1265 | |
4fbd7192 LP |
1266 | r = acquire_bus(BUS_MANAGER, &bus); |
1267 | if (r < 0) | |
1268 | return r; | |
1269 | ||
0221d68a | 1270 | (void) pager_open(arg_pager_flags); |
d2ad7e1b | 1271 | |
e449de87 | 1272 | n = get_unit_list_recursive(bus, strv_skip(argv, 1), &unit_infos, &replies, &machines); |
cbb76c29 LP |
1273 | if (n < 0) |
1274 | return n; | |
1275 | ||
1276 | dual_timestamp_get(&nw); | |
1277 | ||
1278 | for (u = unit_infos; u < unit_infos + n; u++) { | |
1279 | _cleanup_strv_free_ char **triggered = NULL; | |
5cb14b37 | 1280 | dual_timestamp next = DUAL_TIMESTAMP_NULL; |
d784e2db | 1281 | usec_t m, last = 0; |
cbb76c29 | 1282 | |
cbb76c29 LP |
1283 | if (!endswith(u->id, ".timer")) |
1284 | continue; | |
1285 | ||
1286 | r = get_triggered_units(bus, u->unit_path, &triggered); | |
1287 | if (r < 0) | |
1288 | goto cleanup; | |
1289 | ||
1290 | r = get_next_elapse(bus, u->unit_path, &next); | |
1291 | if (r < 0) | |
1292 | goto cleanup; | |
1293 | ||
d784e2db LP |
1294 | get_last_trigger(bus, u->unit_path, &last); |
1295 | ||
cbb76c29 LP |
1296 | if (!GREEDY_REALLOC(timer_infos, size, c+1)) { |
1297 | r = log_oom(); | |
1298 | goto cleanup; | |
1299 | } | |
1300 | ||
f5080e73 DH |
1301 | m = calc_next_elapse(&nw, &next); |
1302 | ||
cbb76c29 | 1303 | timer_infos[c++] = (struct timer_info) { |
806a37e7 | 1304 | .machine = u->machine, |
cbb76c29 LP |
1305 | .id = u->id, |
1306 | .next_elapse = m, | |
d784e2db | 1307 | .last_trigger = last, |
1cc6c93a | 1308 | .triggered = TAKE_PTR(triggered), |
cbb76c29 | 1309 | }; |
cbb76c29 LP |
1310 | } |
1311 | ||
93bab288 | 1312 | typesafe_qsort(timer_infos, c, timer_info_compare); |
cbb76c29 LP |
1313 | |
1314 | output_timers_list(timer_infos, c); | |
1315 | ||
1316 | cleanup: | |
1317 | for (t = timer_infos; t < timer_infos + c; t++) | |
1318 | strv_free(t->triggered); | |
1319 | ||
1320 | return r; | |
1321 | } | |
1322 | ||
93bab288 | 1323 | static int compare_unit_file_list(const UnitFileList *a, const UnitFileList *b) { |
729e3769 | 1324 | const char *d1, *d2; |
729e3769 | 1325 | |
93bab288 YW |
1326 | d1 = strrchr(a->path, '.'); |
1327 | d2 = strrchr(b->path, '.'); | |
729e3769 LP |
1328 | |
1329 | if (d1 && d2) { | |
1330 | int r; | |
1331 | ||
1332 | r = strcasecmp(d1, d2); | |
1333 | if (r != 0) | |
1334 | return r; | |
1335 | } | |
1336 | ||
93bab288 | 1337 | return strcasecmp(basename(a->path), basename(b->path)); |
729e3769 LP |
1338 | } |
1339 | ||
313fe66f | 1340 | static bool output_show_unit_file(const UnitFileList *u, char **states, char **patterns) { |
f8591ee1 LP |
1341 | assert(u); |
1342 | ||
2404701e | 1343 | if (!strv_fnmatch_or_empty(patterns, basename(u->path), FNM_NOESCAPE)) |
d8fba7c6 | 1344 | return false; |
d8fba7c6 | 1345 | |
6c71341a ZJS |
1346 | if (!strv_isempty(arg_types)) { |
1347 | const char *dot; | |
1348 | ||
1349 | dot = strrchr(u->path, '.'); | |
1350 | if (!dot) | |
1351 | return false; | |
1352 | ||
1353 | if (!strv_find(arg_types, dot+1)) | |
1354 | return false; | |
1355 | } | |
1356 | ||
313fe66f | 1357 | if (!strv_isempty(states) && |
1358 | !strv_find(states, unit_file_state_to_string(u->state))) | |
bceccd5e | 1359 | return false; |
fec1530e | 1360 | |
6c71341a | 1361 | return true; |
729e3769 LP |
1362 | } |
1363 | ||
8d5ba5a9 | 1364 | static void output_unit_file_list(const UnitFileList *units, unsigned c) { |
0d292f5e | 1365 | unsigned max_id_len, id_cols, state_cols; |
729e3769 LP |
1366 | const UnitFileList *u; |
1367 | ||
fbd0b64f LP |
1368 | max_id_len = STRLEN("UNIT FILE"); |
1369 | state_cols = STRLEN("STATE"); | |
cbc9fbd1 | 1370 | |
1c0a113f | 1371 | for (u = units; u < units + c; u++) { |
2b6bf07d | 1372 | max_id_len = MAX(max_id_len, strlen(basename(u->path))); |
1c0a113f ZJS |
1373 | state_cols = MAX(state_cols, strlen(unit_file_state_to_string(u->state))); |
1374 | } | |
1375 | ||
1376 | if (!arg_full) { | |
1377 | unsigned basic_cols; | |
cbc9fbd1 | 1378 | |
9607d947 | 1379 | id_cols = MIN(max_id_len, 25u); |
1c0a113f ZJS |
1380 | basic_cols = 1 + id_cols + state_cols; |
1381 | if (basic_cols < (unsigned) columns()) | |
1382 | id_cols += MIN(columns() - basic_cols, max_id_len - id_cols); | |
1383 | } else | |
1384 | id_cols = max_id_len; | |
1385 | ||
11690bcc | 1386 | if (!arg_no_legend && c > 0) |
6ae3e62a ZJS |
1387 | printf("%s%-*s %-*s%s\n", |
1388 | ansi_underline(), | |
cbc9fbd1 | 1389 | id_cols, "UNIT FILE", |
6ae3e62a ZJS |
1390 | state_cols, "STATE", |
1391 | ansi_normal()); | |
729e3769 LP |
1392 | |
1393 | for (u = units; u < units + c; u++) { | |
aaa6732d | 1394 | const char *on_underline = NULL, *on_color = NULL, *off = NULL, *id; |
7fd1b19b | 1395 | _cleanup_free_ char *e = NULL; |
aaa6732d LP |
1396 | bool underline; |
1397 | ||
1398 | underline = u + 1 < units + c && | |
1399 | !streq(unit_type_suffix(u->path), unit_type_suffix((u + 1)->path)); | |
6ae3e62a | 1400 | |
aaa6732d | 1401 | if (underline) |
6ae3e62a | 1402 | on_underline = ansi_underline(); |
729e3769 | 1403 | |
4c315c2c IS |
1404 | if (IN_SET(u->state, |
1405 | UNIT_FILE_MASKED, | |
1406 | UNIT_FILE_MASKED_RUNTIME, | |
1407 | UNIT_FILE_DISABLED, | |
6ae3e62a | 1408 | UNIT_FILE_BAD)) |
aaa6732d | 1409 | on_color = underline ? ansi_highlight_red_underline() : ansi_highlight_red(); |
6ae3e62a | 1410 | else if (u->state == UNIT_FILE_ENABLED) |
aaa6732d LP |
1411 | on_color = underline ? ansi_highlight_green_underline() : ansi_highlight_green(); |
1412 | ||
1413 | if (on_underline || on_color) | |
1414 | off = ansi_normal(); | |
729e3769 | 1415 | |
2b6bf07d | 1416 | id = basename(u->path); |
729e3769 | 1417 | |
1c0a113f | 1418 | e = arg_full ? NULL : ellipsize(id, id_cols, 33); |
729e3769 | 1419 | |
aaa6732d LP |
1420 | printf("%s%-*s %s%-*s%s\n", |
1421 | strempty(on_underline), | |
1c0a113f | 1422 | id_cols, e ? e : id, |
aaa6732d | 1423 | strempty(on_color), state_cols, unit_file_state_to_string(u->state), strempty(off)); |
729e3769 LP |
1424 | } |
1425 | ||
1c0a113f | 1426 | if (!arg_no_legend) |
0d292f5e | 1427 | printf("\n%u unit files listed.\n", c); |
729e3769 LP |
1428 | } |
1429 | ||
e449de87 | 1430 | static int list_unit_files(int argc, char *argv[], void *userdata) { |
4afd3348 | 1431 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
f84190d8 | 1432 | _cleanup_free_ UnitFileList *units = NULL; |
8d5ba5a9 ZJS |
1433 | UnitFileList *unit; |
1434 | size_t size = 0; | |
f459b602 MAP |
1435 | unsigned c = 0; |
1436 | const char *state; | |
1437 | char *path; | |
f84190d8 | 1438 | int r; |
313fe66f | 1439 | bool fallback = false; |
729e3769 | 1440 | |
4fbd7192 | 1441 | if (install_client_side()) { |
729e3769 LP |
1442 | Hashmap *h; |
1443 | UnitFileList *u; | |
1444 | Iterator i; | |
f459b602 | 1445 | unsigned n_units; |
729e3769 | 1446 | |
d5099efc | 1447 | h = hashmap_new(&string_hash_ops); |
0d0f0c50 SL |
1448 | if (!h) |
1449 | return log_oom(); | |
729e3769 | 1450 | |
313fe66f | 1451 | r = unit_file_get_list(arg_scope, arg_root, h, arg_states, strv_skip(argv, 1)); |
729e3769 | 1452 | if (r < 0) { |
8ea913b2 | 1453 | unit_file_list_free(h); |
691395d8 | 1454 | return log_error_errno(r, "Failed to get unit file list: %m"); |
729e3769 LP |
1455 | } |
1456 | ||
1457 | n_units = hashmap_size(h); | |
fdbdf6ec | 1458 | |
0da999fa ZJS |
1459 | units = new(UnitFileList, n_units ?: 1); /* avoid malloc(0) */ |
1460 | if (!units) { | |
729e3769 | 1461 | unit_file_list_free(h); |
0d0f0c50 | 1462 | return log_oom(); |
729e3769 LP |
1463 | } |
1464 | ||
1465 | HASHMAP_FOREACH(u, h, i) { | |
313fe66f | 1466 | if (!output_show_unit_file(u, NULL, NULL)) |
8d5ba5a9 ZJS |
1467 | continue; |
1468 | ||
1469 | units[c++] = *u; | |
729e3769 LP |
1470 | free(u); |
1471 | } | |
1472 | ||
8d5ba5a9 | 1473 | assert(c <= n_units); |
729e3769 | 1474 | hashmap_free(h); |
5f056378 CH |
1475 | |
1476 | r = 0; | |
729e3769 | 1477 | } else { |
313fe66f | 1478 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
4afd3348 | 1479 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
4fbd7192 LP |
1480 | sd_bus *bus; |
1481 | ||
1482 | r = acquire_bus(BUS_MANAGER, &bus); | |
1483 | if (r < 0) | |
1484 | return r; | |
6e646d22 | 1485 | |
313fe66f | 1486 | r = sd_bus_message_new_method_call( |
f22f08cd | 1487 | bus, |
313fe66f | 1488 | &m, |
729e3769 LP |
1489 | "org.freedesktop.systemd1", |
1490 | "/org/freedesktop/systemd1", | |
1491 | "org.freedesktop.systemd1.Manager", | |
313fe66f | 1492 | "ListUnitFilesByPatterns"); |
1493 | if (r < 0) | |
1494 | return bus_log_create_error(r); | |
1495 | ||
1496 | r = sd_bus_message_append_strv(m, arg_states); | |
1497 | if (r < 0) | |
1498 | return bus_log_create_error(r); | |
1499 | ||
1500 | r = sd_bus_message_append_strv(m, strv_skip(argv, 1)); | |
1501 | if (r < 0) | |
1502 | return bus_log_create_error(r); | |
1503 | ||
1504 | r = sd_bus_call(bus, m, 0, &error, &reply); | |
1505 | if (r < 0 && sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_METHOD)) { | |
1506 | /* Fallback to legacy ListUnitFiles method */ | |
1507 | fallback = true; | |
1508 | log_debug_errno(r, "Failed to list unit files: %s Falling back to ListUnitsFiles method.", bus_error_message(&error, r)); | |
1509 | m = sd_bus_message_unref(m); | |
1510 | sd_bus_error_free(&error); | |
1511 | ||
1512 | r = sd_bus_message_new_method_call( | |
1513 | bus, | |
1514 | &m, | |
1515 | "org.freedesktop.systemd1", | |
1516 | "/org/freedesktop/systemd1", | |
1517 | "org.freedesktop.systemd1.Manager", | |
1518 | "ListUnitFiles"); | |
1519 | if (r < 0) | |
1520 | return bus_log_create_error(r); | |
1521 | ||
1522 | r = sd_bus_call(bus, m, 0, &error, &reply); | |
1523 | } | |
691395d8 LP |
1524 | if (r < 0) |
1525 | return log_error_errno(r, "Failed to list unit files: %s", bus_error_message(&error, r)); | |
729e3769 | 1526 | |
f459b602 MAP |
1527 | r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ss)"); |
1528 | if (r < 0) | |
1529 | return bus_log_parse_error(r); | |
729e3769 | 1530 | |
f459b602 | 1531 | while ((r = sd_bus_message_read(reply, "(ss)", &path, &state)) > 0) { |
729e3769 | 1532 | |
f459b602 MAP |
1533 | if (!GREEDY_REALLOC(units, size, c + 1)) |
1534 | return log_oom(); | |
729e3769 | 1535 | |
8d5ba5a9 | 1536 | units[c] = (struct UnitFileList) { |
f459b602 MAP |
1537 | path, |
1538 | unit_file_state_from_string(state) | |
1539 | }; | |
8d5ba5a9 | 1540 | |
313fe66f | 1541 | if (output_show_unit_file(&units[c], |
1542 | fallback ? arg_states : NULL, | |
1543 | fallback ? strv_skip(argv, 1) : NULL)) | |
313cefa1 | 1544 | c++; |
8d5ba5a9 | 1545 | |
729e3769 | 1546 | } |
f459b602 MAP |
1547 | if (r < 0) |
1548 | return bus_log_parse_error(r); | |
1549 | ||
1550 | r = sd_bus_message_exit_container(reply); | |
1551 | if (r < 0) | |
1552 | return bus_log_parse_error(r); | |
729e3769 LP |
1553 | } |
1554 | ||
0221d68a | 1555 | (void) pager_open(arg_pager_flags); |
d2ad7e1b | 1556 | |
93bab288 | 1557 | typesafe_qsort(units, c, compare_unit_file_list); |
4fe1be9c | 1558 | output_unit_file_list(units, c); |
729e3769 | 1559 | |
0da999fa | 1560 | if (install_client_side()) |
8d5ba5a9 ZJS |
1561 | for (unit = units; unit < units + c; unit++) |
1562 | free(unit->path); | |
1563 | ||
f84190d8 | 1564 | return 0; |
729e3769 LP |
1565 | } |
1566 | ||
14cb109d | 1567 | static int list_dependencies_print(const char *name, int level, unsigned branches, bool last) { |
55c0b89c | 1568 | _cleanup_free_ char *n = NULL; |
9607d947 | 1569 | size_t max_len = MAX(columns(),20u); |
cbc9fbd1 LP |
1570 | size_t len = 0; |
1571 | int i; | |
55c0b89c | 1572 | |
5d0c05e5 | 1573 | if (!arg_plain) { |
cbc9fbd1 | 1574 | |
5d0c05e5 LN |
1575 | for (i = level - 1; i >= 0; i--) { |
1576 | len += 2; | |
f168c273 | 1577 | if (len > max_len - 3 && !arg_full) { |
5d0c05e5 LN |
1578 | printf("%s...\n",max_len % 2 ? "" : " "); |
1579 | return 0; | |
1580 | } | |
9a6f746f | 1581 | printf("%s", special_glyph(branches & (1 << i) ? SPECIAL_GLYPH_TREE_VERTICAL : SPECIAL_GLYPH_TREE_SPACE)); |
5d0c05e5 | 1582 | } |
55c0b89c | 1583 | len += 2; |
cbc9fbd1 | 1584 | |
f168c273 | 1585 | if (len > max_len - 3 && !arg_full) { |
55c0b89c LN |
1586 | printf("%s...\n",max_len % 2 ? "" : " "); |
1587 | return 0; | |
1588 | } | |
cbc9fbd1 | 1589 | |
9a6f746f | 1590 | printf("%s", special_glyph(last ? SPECIAL_GLYPH_TREE_RIGHT : SPECIAL_GLYPH_TREE_BRANCH)); |
55c0b89c | 1591 | } |
55c0b89c | 1592 | |
9ed794a3 | 1593 | if (arg_full) { |
55c0b89c LN |
1594 | printf("%s\n", name); |
1595 | return 0; | |
1596 | } | |
1597 | ||
1598 | n = ellipsize(name, max_len-len, 100); | |
f168c273 | 1599 | if (!n) |
55c0b89c LN |
1600 | return log_oom(); |
1601 | ||
1602 | printf("%s\n", n); | |
1603 | return 0; | |
1604 | } | |
1605 | ||
f459b602 | 1606 | static int list_dependencies_get_dependencies(sd_bus *bus, const char *name, char ***deps) { |
32b47279 YW |
1607 | struct DependencyStatusInfo { |
1608 | char **dep[5]; | |
1609 | } info = {}; | |
1610 | ||
1611 | static const struct bus_properties_map map[_DEPENDENCY_MAX][6] = { | |
1612 | [DEPENDENCY_FORWARD] = { | |
1613 | { "Requires", "as", NULL, offsetof(struct DependencyStatusInfo, dep[0]) }, | |
1614 | { "Requisite", "as", NULL, offsetof(struct DependencyStatusInfo, dep[1]) }, | |
1615 | { "Wants", "as", NULL, offsetof(struct DependencyStatusInfo, dep[2]) }, | |
1616 | { "ConsistsOf", "as", NULL, offsetof(struct DependencyStatusInfo, dep[3]) }, | |
1617 | { "BindsTo", "as", NULL, offsetof(struct DependencyStatusInfo, dep[4]) }, | |
1618 | {} | |
1619 | }, | |
1620 | [DEPENDENCY_REVERSE] = { | |
1621 | { "RequiredBy", "as", NULL, offsetof(struct DependencyStatusInfo, dep[0]) }, | |
1622 | { "RequisiteOf", "as", NULL, offsetof(struct DependencyStatusInfo, dep[1]) }, | |
1623 | { "WantedBy", "as", NULL, offsetof(struct DependencyStatusInfo, dep[2]) }, | |
1624 | { "PartOf", "as", NULL, offsetof(struct DependencyStatusInfo, dep[3]) }, | |
1625 | { "BoundBy", "as", NULL, offsetof(struct DependencyStatusInfo, dep[4]) }, | |
1626 | {} | |
1627 | }, | |
1628 | [DEPENDENCY_AFTER] = { | |
1629 | { "After", "as", NULL, offsetof(struct DependencyStatusInfo, dep[0]) }, | |
1630 | {} | |
1631 | }, | |
1632 | [DEPENDENCY_BEFORE] = { | |
1633 | { "Before", "as", NULL, offsetof(struct DependencyStatusInfo, dep[0]) }, | |
1634 | {} | |
1635 | }, | |
afba4199 | 1636 | }; |
55c0b89c | 1637 | |
4afd3348 | 1638 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
f459b602 MAP |
1639 | _cleanup_strv_free_ char **ret = NULL; |
1640 | _cleanup_free_ char *path = NULL; | |
32b47279 | 1641 | int i, r; |
55c0b89c LN |
1642 | |
1643 | assert(bus); | |
1644 | assert(name); | |
1645 | assert(deps); | |
1646 | ||
1647 | path = unit_dbus_path_from_name(name); | |
f459b602 MAP |
1648 | if (!path) |
1649 | return log_oom(); | |
55c0b89c | 1650 | |
32b47279 YW |
1651 | r = bus_map_all_properties(bus, |
1652 | "org.freedesktop.systemd1", | |
1653 | path, | |
1654 | map[arg_dependency], | |
f39cbb6e | 1655 | BUS_MAP_STRDUP, |
32b47279 | 1656 | &error, |
f37f8a61 | 1657 | NULL, |
32b47279 | 1658 | &info); |
691395d8 LP |
1659 | if (r < 0) |
1660 | return log_error_errno(r, "Failed to get properties of %s: %s", name, bus_error_message(&error, r)); | |
55c0b89c | 1661 | |
32b47279 YW |
1662 | if (IN_SET(arg_dependency, DEPENDENCY_AFTER, DEPENDENCY_BEFORE)) { |
1663 | *deps = info.dep[0]; | |
1664 | return 0; | |
1665 | } | |
55c0b89c | 1666 | |
32b47279 YW |
1667 | for (i = 0; i < 5; i++) { |
1668 | r = strv_extend_strv(&ret, info.dep[i], true); | |
f459b602 | 1669 | if (r < 0) |
32b47279 YW |
1670 | return log_oom(); |
1671 | info.dep[i] = strv_free(info.dep[i]); | |
f459b602 | 1672 | } |
540e7dbe | 1673 | |
1cc6c93a | 1674 | *deps = TAKE_PTR(ret); |
540e7dbe | 1675 | |
f459b602 | 1676 | return 0; |
55c0b89c LN |
1677 | } |
1678 | ||
93bab288 | 1679 | static int list_dependencies_compare(char * const *a, char * const *b) { |
55c0b89c LN |
1680 | if (unit_name_to_type(*a) == UNIT_TARGET && unit_name_to_type(*b) != UNIT_TARGET) |
1681 | return 1; | |
1682 | if (unit_name_to_type(*a) != UNIT_TARGET && unit_name_to_type(*b) == UNIT_TARGET) | |
1683 | return -1; | |
cbc9fbd1 | 1684 | |
55c0b89c LN |
1685 | return strcasecmp(*a, *b); |
1686 | } | |
1687 | ||
f459b602 MAP |
1688 | static int list_dependencies_one( |
1689 | sd_bus *bus, | |
1690 | const char *name, | |
1691 | int level, | |
1692 | char ***units, | |
14cb109d | 1693 | unsigned branches) { |
f459b602 | 1694 | |
e3e45d4f | 1695 | _cleanup_strv_free_ char **deps = NULL; |
55c0b89c | 1696 | char **c; |
55c0b89c LN |
1697 | int r = 0; |
1698 | ||
cbc9fbd1 LP |
1699 | assert(bus); |
1700 | assert(name); | |
1701 | assert(units); | |
1702 | ||
e3e45d4f SP |
1703 | r = strv_extend(units, name); |
1704 | if (r < 0) | |
55c0b89c LN |
1705 | return log_oom(); |
1706 | ||
1707 | r = list_dependencies_get_dependencies(bus, name, &deps); | |
1708 | if (r < 0) | |
cec7eda5 | 1709 | return r; |
55c0b89c | 1710 | |
93bab288 | 1711 | typesafe_qsort(deps, strv_length(deps), list_dependencies_compare); |
55c0b89c LN |
1712 | |
1713 | STRV_FOREACH(c, deps) { | |
e3e45d4f | 1714 | if (strv_contains(*units, *c)) { |
5d0c05e5 | 1715 | if (!arg_plain) { |
60705040 | 1716 | printf(" "); |
5d0c05e5 LN |
1717 | r = list_dependencies_print("...", level + 1, (branches << 1) | (c[1] == NULL ? 0 : 1), 1); |
1718 | if (r < 0) | |
1719 | return r; | |
1720 | } | |
55c0b89c LN |
1721 | continue; |
1722 | } | |
1723 | ||
250ba664 ZJS |
1724 | if (arg_plain) |
1725 | printf(" "); | |
1726 | else { | |
fa0d5878 | 1727 | UnitActiveState active_state = _UNIT_ACTIVE_STATE_INVALID; |
250ba664 ZJS |
1728 | const char *on; |
1729 | ||
fa0d5878 | 1730 | (void) get_state_one_unit(bus, *c, &active_state); |
baa9ecc1 | 1731 | |
fa0d5878 | 1732 | switch (active_state) { |
baa9ecc1 LP |
1733 | case UNIT_ACTIVE: |
1734 | case UNIT_RELOADING: | |
1735 | case UNIT_ACTIVATING: | |
1736 | on = ansi_highlight_green(); | |
1737 | break; | |
1738 | ||
1739 | case UNIT_INACTIVE: | |
1740 | case UNIT_DEACTIVATING: | |
1741 | on = ansi_normal(); | |
1742 | break; | |
1743 | ||
1744 | default: | |
1745 | on = ansi_highlight_red(); | |
1746 | break; | |
fa0d5878 BR |
1747 | } |
1748 | ||
9a6f746f | 1749 | printf("%s%s%s ", on, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE), ansi_normal()); |
250ba664 | 1750 | } |
dbc2c080 | 1751 | |
55c0b89c | 1752 | r = list_dependencies_print(*c, level, branches, c[1] == NULL); |
cec7eda5 ZJS |
1753 | if (r < 0) |
1754 | return r; | |
55c0b89c LN |
1755 | |
1756 | if (arg_all || unit_name_to_type(*c) == UNIT_TARGET) { | |
e3e45d4f | 1757 | r = list_dependencies_one(bus, *c, level + 1, units, (branches << 1) | (c[1] == NULL ? 0 : 1)); |
f168c273 | 1758 | if (r < 0) |
cec7eda5 | 1759 | return r; |
55c0b89c LN |
1760 | } |
1761 | } | |
f459b602 | 1762 | |
e3e45d4f SP |
1763 | if (!arg_plain) |
1764 | strv_remove(*units, name); | |
f459b602 | 1765 | |
cec7eda5 | 1766 | return 0; |
55c0b89c LN |
1767 | } |
1768 | ||
e449de87 | 1769 | static int list_dependencies(int argc, char *argv[], void *userdata) { |
5d0c05e5 | 1770 | _cleanup_strv_free_ char **units = NULL; |
f459b602 | 1771 | _cleanup_free_ char *unit = NULL; |
e31165b2 | 1772 | const char *u; |
4fbd7192 | 1773 | sd_bus *bus; |
7410616c | 1774 | int r; |
55c0b89c | 1775 | |
e449de87 | 1776 | if (argv[1]) { |
37cbc1d5 | 1777 | r = unit_name_mangle(argv[1], arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN, &unit); |
7410616c LP |
1778 | if (r < 0) |
1779 | return log_error_errno(r, "Failed to mangle unit name: %m"); | |
1780 | ||
e31165b2 LP |
1781 | u = unit; |
1782 | } else | |
1783 | u = SPECIAL_DEFAULT_TARGET; | |
55c0b89c | 1784 | |
4fbd7192 LP |
1785 | r = acquire_bus(BUS_MANAGER, &bus); |
1786 | if (r < 0) | |
1787 | return r; | |
1788 | ||
0221d68a | 1789 | (void) pager_open(arg_pager_flags); |
d2ad7e1b | 1790 | |
e31165b2 LP |
1791 | puts(u); |
1792 | ||
5d0c05e5 | 1793 | return list_dependencies_one(bus, u, 0, &units, 0); |
55c0b89c LN |
1794 | } |
1795 | ||
0d292f5e LP |
1796 | struct machine_info { |
1797 | bool is_host; | |
1798 | char *name; | |
0d292f5e | 1799 | char *state; |
8fcf784d LP |
1800 | char *control_group; |
1801 | uint32_t n_failed_units; | |
1802 | uint32_t n_jobs; | |
1803 | usec_t timestamp; | |
1804 | }; | |
1805 | ||
1806 | static const struct bus_properties_map machine_info_property_map[] = { | |
1807 | { "SystemState", "s", NULL, offsetof(struct machine_info, state) }, | |
1808 | { "NJobs", "u", NULL, offsetof(struct machine_info, n_jobs) }, | |
1809 | { "NFailedUnits", "u", NULL, offsetof(struct machine_info, n_failed_units) }, | |
1810 | { "ControlGroup", "s", NULL, offsetof(struct machine_info, control_group) }, | |
1811 | { "UserspaceTimestamp", "t", NULL, offsetof(struct machine_info, timestamp) }, | |
1812 | {} | |
0d292f5e LP |
1813 | }; |
1814 | ||
e7e55dbd | 1815 | static void machine_info_clear(struct machine_info *info) { |
f8654baa ZJS |
1816 | assert(info); |
1817 | ||
1818 | free(info->name); | |
1819 | free(info->state); | |
1820 | free(info->control_group); | |
1821 | zero(*info); | |
e7e55dbd DH |
1822 | } |
1823 | ||
0d292f5e LP |
1824 | static void free_machines_list(struct machine_info *machine_infos, int n) { |
1825 | int i; | |
1826 | ||
1827 | if (!machine_infos) | |
1828 | return; | |
1829 | ||
e7e55dbd DH |
1830 | for (i = 0; i < n; i++) |
1831 | machine_info_clear(&machine_infos[i]); | |
0d292f5e LP |
1832 | |
1833 | free(machine_infos); | |
1834 | } | |
1835 | ||
93bab288 YW |
1836 | static int compare_machine_info(const struct machine_info *a, const struct machine_info *b) { |
1837 | int r; | |
0d292f5e | 1838 | |
93bab288 YW |
1839 | r = CMP(b->is_host, a->is_host); |
1840 | if (r != 0) | |
1841 | return r; | |
0d292f5e | 1842 | |
93bab288 | 1843 | return strcasecmp(a->name, b->name); |
0d292f5e LP |
1844 | } |
1845 | ||
1846 | static int get_machine_properties(sd_bus *bus, struct machine_info *mi) { | |
4afd3348 | 1847 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *container = NULL; |
0d292f5e LP |
1848 | int r; |
1849 | ||
1850 | assert(mi); | |
1851 | ||
1852 | if (!bus) { | |
de33fc62 | 1853 | r = sd_bus_open_system_machine(&container, mi->name); |
0d292f5e LP |
1854 | if (r < 0) |
1855 | return r; | |
1856 | ||
1857 | bus = container; | |
1858 | } | |
1859 | ||
a7e4861c YW |
1860 | r = bus_map_all_properties( |
1861 | bus, | |
1862 | "org.freedesktop.systemd1", | |
1863 | "/org/freedesktop/systemd1", | |
1864 | machine_info_property_map, | |
1865 | BUS_MAP_STRDUP, | |
1866 | NULL, | |
1867 | NULL, | |
1868 | mi); | |
0d292f5e LP |
1869 | if (r < 0) |
1870 | return r; | |
1871 | ||
1872 | return 0; | |
1873 | } | |
1874 | ||
1875 | static bool output_show_machine(const char *name, char **patterns) { | |
2404701e | 1876 | return strv_fnmatch_or_empty(patterns, name, FNM_NOESCAPE); |
0d292f5e LP |
1877 | } |
1878 | ||
1879 | static int get_machine_list( | |
1880 | sd_bus *bus, | |
1881 | struct machine_info **_machine_infos, | |
1882 | char **patterns) { | |
1883 | ||
1884 | struct machine_info *machine_infos = NULL; | |
1885 | _cleanup_strv_free_ char **m = NULL; | |
1886 | _cleanup_free_ char *hn = NULL; | |
1887 | size_t sz = 0; | |
1888 | char **i; | |
4c3e8e39 | 1889 | int c = 0, r; |
0d292f5e LP |
1890 | |
1891 | hn = gethostname_malloc(); | |
1892 | if (!hn) | |
1893 | return log_oom(); | |
1894 | ||
1895 | if (output_show_machine(hn, patterns)) { | |
1896 | if (!GREEDY_REALLOC0(machine_infos, sz, c+1)) | |
1897 | return log_oom(); | |
1898 | ||
1899 | machine_infos[c].is_host = true; | |
ae2a15bc | 1900 | machine_infos[c].name = TAKE_PTR(hn); |
0d292f5e | 1901 | |
f9e0eefc | 1902 | (void) get_machine_properties(bus, &machine_infos[c]); |
0d292f5e LP |
1903 | c++; |
1904 | } | |
1905 | ||
4c3e8e39 LP |
1906 | r = sd_get_machine_names(&m); |
1907 | if (r < 0) | |
1908 | return log_error_errno(r, "Failed to get machine list: %m"); | |
1909 | ||
0d292f5e LP |
1910 | STRV_FOREACH(i, m) { |
1911 | _cleanup_free_ char *class = NULL; | |
1912 | ||
1913 | if (!output_show_machine(*i, patterns)) | |
1914 | continue; | |
1915 | ||
1916 | sd_machine_get_class(*i, &class); | |
1917 | if (!streq_ptr(class, "container")) | |
1918 | continue; | |
1919 | ||
1920 | if (!GREEDY_REALLOC0(machine_infos, sz, c+1)) { | |
1921 | free_machines_list(machine_infos, c); | |
1922 | return log_oom(); | |
1923 | } | |
1924 | ||
1925 | machine_infos[c].is_host = false; | |
1926 | machine_infos[c].name = strdup(*i); | |
1927 | if (!machine_infos[c].name) { | |
1928 | free_machines_list(machine_infos, c); | |
1929 | return log_oom(); | |
1930 | } | |
1931 | ||
f9e0eefc | 1932 | (void) get_machine_properties(NULL, &machine_infos[c]); |
0d292f5e LP |
1933 | c++; |
1934 | } | |
1935 | ||
1936 | *_machine_infos = machine_infos; | |
1937 | return c; | |
1938 | } | |
1939 | ||
1940 | static void output_machines_list(struct machine_info *machine_infos, unsigned n) { | |
1941 | struct machine_info *m; | |
1942 | unsigned | |
90c3f79d | 1943 | circle_len = 0, |
fbd0b64f LP |
1944 | namelen = STRLEN("NAME"), |
1945 | statelen = STRLEN("STATE"), | |
1946 | failedlen = STRLEN("FAILED"), | |
1947 | jobslen = STRLEN("JOBS"); | |
aa91d592 | 1948 | bool state_missing = false; |
0d292f5e LP |
1949 | |
1950 | assert(machine_infos || n == 0); | |
1951 | ||
1952 | for (m = machine_infos; m < machine_infos + n; m++) { | |
fbd0b64f LP |
1953 | namelen = MAX(namelen, |
1954 | strlen(m->name) + (m->is_host ? STRLEN(" (host)") : 0)); | |
7bf7ce28 | 1955 | statelen = MAX(statelen, strlen_ptr(m->state)); |
0d292f5e LP |
1956 | failedlen = MAX(failedlen, DECIMAL_STR_WIDTH(m->n_failed_units)); |
1957 | jobslen = MAX(jobslen, DECIMAL_STR_WIDTH(m->n_jobs)); | |
90c3f79d | 1958 | |
a004cb75 | 1959 | if (!arg_plain && m->state && !streq(m->state, "running")) |
90c3f79d | 1960 | circle_len = 2; |
0d292f5e LP |
1961 | } |
1962 | ||
90c3f79d LP |
1963 | if (!arg_no_legend) { |
1964 | if (circle_len > 0) | |
1965 | fputs(" ", stdout); | |
1966 | ||
0d292f5e LP |
1967 | printf("%-*s %-*s %-*s %-*s\n", |
1968 | namelen, "NAME", | |
1969 | statelen, "STATE", | |
1970 | failedlen, "FAILED", | |
1971 | jobslen, "JOBS"); | |
90c3f79d | 1972 | } |
0d292f5e LP |
1973 | |
1974 | for (m = machine_infos; m < machine_infos + n; m++) { | |
90c3f79d LP |
1975 | const char *on_state = "", *off_state = ""; |
1976 | const char *on_failed = "", *off_failed = ""; | |
1977 | bool circle = false; | |
0d292f5e LP |
1978 | |
1979 | if (streq_ptr(m->state, "degraded")) { | |
1980 | on_state = ansi_highlight_red(); | |
1fc464f6 | 1981 | off_state = ansi_normal(); |
90c3f79d | 1982 | circle = true; |
0d292f5e LP |
1983 | } else if (!streq_ptr(m->state, "running")) { |
1984 | on_state = ansi_highlight_yellow(); | |
1fc464f6 | 1985 | off_state = ansi_normal(); |
90c3f79d LP |
1986 | circle = true; |
1987 | } | |
0d292f5e LP |
1988 | |
1989 | if (m->n_failed_units > 0) { | |
1990 | on_failed = ansi_highlight_red(); | |
1fc464f6 | 1991 | off_failed = ansi_normal(); |
0d292f5e LP |
1992 | } else |
1993 | on_failed = off_failed = ""; | |
1994 | ||
90c3f79d | 1995 | if (circle_len > 0) |
9a6f746f | 1996 | printf("%s%s%s ", on_state, circle ? special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE) : " ", off_state); |
90c3f79d | 1997 | |
4bea2469 ZJS |
1998 | if (!m->state) |
1999 | state_missing = true; | |
2000 | ||
0d292f5e | 2001 | if (m->is_host) |
c6ba5b80 | 2002 | printf("%-*s (host) %s%-*s%s %s%*" PRIu32 "%s %*" PRIu32 "\n", |
4bea2469 | 2003 | (int) (namelen - strlen(" (host)")), |
fbd0b64f | 2004 | strna(m->name), |
0d292f5e LP |
2005 | on_state, statelen, strna(m->state), off_state, |
2006 | on_failed, failedlen, m->n_failed_units, off_failed, | |
2007 | jobslen, m->n_jobs); | |
2008 | else | |
c6ba5b80 | 2009 | printf("%-*s %s%-*s%s %s%*" PRIu32 "%s %*" PRIu32 "\n", |
0d292f5e LP |
2010 | namelen, strna(m->name), |
2011 | on_state, statelen, strna(m->state), off_state, | |
2012 | on_failed, failedlen, m->n_failed_units, off_failed, | |
2013 | jobslen, m->n_jobs); | |
2014 | } | |
2015 | ||
4bea2469 ZJS |
2016 | if (!arg_no_legend) { |
2017 | printf("\n"); | |
2018 | if (state_missing && geteuid() != 0) | |
2019 | printf("Notice: some information only available to privileged users was not shown.\n"); | |
2020 | printf("%u machines listed.\n", n); | |
2021 | } | |
0d292f5e LP |
2022 | } |
2023 | ||
e449de87 | 2024 | static int list_machines(int argc, char *argv[], void *userdata) { |
0d292f5e | 2025 | struct machine_info *machine_infos = NULL; |
4fbd7192 | 2026 | sd_bus *bus; |
0d292f5e LP |
2027 | int r; |
2028 | ||
4fbd7192 LP |
2029 | r = acquire_bus(BUS_MANAGER, &bus); |
2030 | if (r < 0) | |
2031 | return r; | |
2032 | ||
e449de87 | 2033 | r = get_machine_list(bus, &machine_infos, strv_skip(argv, 1)); |
0d292f5e LP |
2034 | if (r < 0) |
2035 | return r; | |
2036 | ||
0221d68a | 2037 | (void) pager_open(arg_pager_flags); |
d2ad7e1b | 2038 | |
93bab288 | 2039 | typesafe_qsort(machine_infos, r, compare_machine_info); |
0d292f5e LP |
2040 | output_machines_list(machine_infos, r); |
2041 | free_machines_list(machine_infos, r); | |
2042 | ||
2043 | return 0; | |
2044 | } | |
2045 | ||
e449de87 | 2046 | static int get_default(int argc, char *argv[], void *userdata) { |
4afd3348 | 2047 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
f459b602 MAP |
2048 | _cleanup_free_ char *_path = NULL; |
2049 | const char *path; | |
99504dd4 | 2050 | int r; |
99504dd4 | 2051 | |
4fbd7192 | 2052 | if (install_client_side()) { |
f459b602 | 2053 | r = unit_file_get_default(arg_scope, arg_root, &_path); |
f647962d MS |
2054 | if (r < 0) |
2055 | return log_error_errno(r, "Failed to get default target: %m"); | |
f459b602 | 2056 | path = _path; |
99504dd4 | 2057 | |
5f056378 | 2058 | r = 0; |
99504dd4 | 2059 | } else { |
4afd3348 | 2060 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
4fbd7192 LP |
2061 | sd_bus *bus; |
2062 | ||
2063 | r = acquire_bus(BUS_MANAGER, &bus); | |
2064 | if (r < 0) | |
2065 | return r; | |
6e646d22 | 2066 | |
f459b602 MAP |
2067 | r = sd_bus_call_method( |
2068 | bus, | |
2069 | "org.freedesktop.systemd1", | |
2070 | "/org/freedesktop/systemd1", | |
2071 | "org.freedesktop.systemd1.Manager", | |
2072 | "GetDefaultTarget", | |
2073 | &error, | |
2074 | &reply, | |
2075 | NULL); | |
691395d8 LP |
2076 | if (r < 0) |
2077 | return log_error_errno(r, "Failed to get default target: %s", bus_error_message(&error, r)); | |
99504dd4 | 2078 | |
f459b602 MAP |
2079 | r = sd_bus_message_read(reply, "s", &path); |
2080 | if (r < 0) | |
2081 | return bus_log_parse_error(r); | |
99504dd4 VP |
2082 | } |
2083 | ||
2084 | if (path) | |
2085 | printf("%s\n", path); | |
2086 | ||
f459b602 | 2087 | return 0; |
99504dd4 VP |
2088 | } |
2089 | ||
e449de87 | 2090 | static int set_default(int argc, char *argv[], void *userdata) { |
718db961 | 2091 | _cleanup_free_ char *unit = NULL; |
acc0269c | 2092 | UnitFileChange *changes = NULL; |
da6053d0 | 2093 | size_t n_changes = 0; |
718db961 LP |
2094 | int r; |
2095 | ||
e449de87 LP |
2096 | assert(argc >= 2); |
2097 | assert(argv); | |
2098 | ||
37cbc1d5 | 2099 | r = unit_name_mangle_with_suffix(argv[1], arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN, ".target", &unit); |
7410616c LP |
2100 | if (r < 0) |
2101 | return log_error_errno(r, "Failed to mangle unit name: %m"); | |
718db961 | 2102 | |
4fbd7192 | 2103 | if (install_client_side()) { |
b3796dd8 | 2104 | r = unit_file_set_default(arg_scope, UNIT_FILE_FORCE, arg_root, unit, &changes, &n_changes); |
af3d8113 | 2105 | unit_file_dump_changes(r, "set default", changes, n_changes, arg_quiet); |
5f056378 CH |
2106 | |
2107 | if (r > 0) | |
2108 | r = 0; | |
718db961 | 2109 | } else { |
4afd3348 LP |
2110 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
2111 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
4fbd7192 | 2112 | sd_bus *bus; |
718db961 | 2113 | |
8a4b13c5 | 2114 | polkit_agent_open_maybe(); |
6e646d22 | 2115 | |
4fbd7192 LP |
2116 | r = acquire_bus(BUS_MANAGER, &bus); |
2117 | if (r < 0) | |
2118 | return r; | |
2119 | ||
6e646d22 | 2120 | r = sd_bus_call_method( |
718db961 LP |
2121 | bus, |
2122 | "org.freedesktop.systemd1", | |
2123 | "/org/freedesktop/systemd1", | |
2124 | "org.freedesktop.systemd1.Manager", | |
6e646d22 LP |
2125 | "SetDefaultTarget", |
2126 | &error, | |
2127 | &reply, | |
2128 | "sb", unit, 1); | |
691395d8 LP |
2129 | if (r < 0) |
2130 | return log_error_errno(r, "Failed to set default target: %s", bus_error_message(&error, r)); | |
718db961 | 2131 | |
acc0269c | 2132 | r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet, &changes, &n_changes); |
718db961 | 2133 | if (r < 0) |
acc0269c | 2134 | goto finish; |
718db961 | 2135 | |
93c941e3 | 2136 | /* Try to reload if enabled */ |
718db961 | 2137 | if (!arg_no_reload) |
e449de87 | 2138 | r = daemon_reload(argc, argv, userdata); |
718db961 LP |
2139 | else |
2140 | r = 0; | |
2141 | } | |
2142 | ||
acc0269c CH |
2143 | finish: |
2144 | unit_file_changes_free(changes, n_changes); | |
2145 | ||
718db961 LP |
2146 | return r; |
2147 | } | |
2148 | ||
76d8ca22 | 2149 | static int output_waiting_jobs(sd_bus *bus, uint32_t id, const char *method, const char *prefix) { |
82948f6c LP |
2150 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
2151 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
31d99bd1 | 2152 | const char *name, *type; |
82948f6c LP |
2153 | uint32_t other_id; |
2154 | int r; | |
2155 | ||
2156 | assert(bus); | |
2157 | ||
2158 | r = sd_bus_call_method( | |
2159 | bus, | |
2160 | "org.freedesktop.systemd1", | |
2161 | "/org/freedesktop/systemd1", | |
2162 | "org.freedesktop.systemd1.Manager", | |
2163 | method, | |
2164 | &error, | |
2165 | &reply, | |
2166 | "u", id); | |
76d8ca22 ZJS |
2167 | if (r < 0) |
2168 | return log_debug_errno(r, "Failed to get waiting jobs for job %" PRIu32, id); | |
82948f6c LP |
2169 | |
2170 | r = sd_bus_message_enter_container(reply, 'a', "(usssoo)"); | |
76d8ca22 ZJS |
2171 | if (r < 0) |
2172 | return bus_log_parse_error(r); | |
82948f6c | 2173 | |
31d99bd1 | 2174 | while ((r = sd_bus_message_read(reply, "(usssoo)", &other_id, &name, &type, NULL, NULL, NULL)) > 0) |
bc357ce5 | 2175 | printf("%s %u (%s/%s)\n", prefix, other_id, name, type); |
76d8ca22 ZJS |
2176 | if (r < 0) |
2177 | return bus_log_parse_error(r); | |
82948f6c LP |
2178 | |
2179 | r = sd_bus_message_exit_container(reply); | |
76d8ca22 ZJS |
2180 | if (r < 0) |
2181 | return bus_log_parse_error(r); | |
2182 | ||
2183 | return 0; | |
82948f6c LP |
2184 | } |
2185 | ||
75add28a ZJS |
2186 | struct job_info { |
2187 | uint32_t id; | |
f459b602 | 2188 | const char *name, *type, *state; |
75add28a ZJS |
2189 | }; |
2190 | ||
82948f6c | 2191 | static void output_jobs_list(sd_bus *bus, const struct job_info* jobs, unsigned n, bool skipped) { |
f459b602 MAP |
2192 | unsigned id_len, unit_len, type_len, state_len; |
2193 | const struct job_info *j; | |
75add28a ZJS |
2194 | const char *on, *off; |
2195 | bool shorten = false; | |
2196 | ||
2197 | assert(n == 0 || jobs); | |
2198 | ||
2199 | if (n == 0) { | |
250ba664 ZJS |
2200 | if (!arg_no_legend) { |
2201 | on = ansi_highlight_green(); | |
1fc464f6 | 2202 | off = ansi_normal(); |
75add28a | 2203 | |
250ba664 ZJS |
2204 | printf("%sNo jobs %s.%s\n", on, skipped ? "listed" : "running", off); |
2205 | } | |
75add28a ZJS |
2206 | return; |
2207 | } | |
2208 | ||
0221d68a | 2209 | (void) pager_open(arg_pager_flags); |
75add28a | 2210 | |
fbd0b64f LP |
2211 | id_len = STRLEN("JOB"); |
2212 | unit_len = STRLEN("UNIT"); | |
2213 | type_len = STRLEN("TYPE"); | |
2214 | state_len = STRLEN("STATE"); | |
6d6d40c9 | 2215 | |
f459b602 MAP |
2216 | for (j = jobs; j < jobs + n; j++) { |
2217 | uint32_t id = j->id; | |
2218 | assert(j->name && j->type && j->state); | |
75add28a | 2219 | |
f459b602 MAP |
2220 | id_len = MAX(id_len, DECIMAL_STR_WIDTH(id)); |
2221 | unit_len = MAX(unit_len, strlen(j->name)); | |
2222 | type_len = MAX(type_len, strlen(j->type)); | |
2223 | state_len = MAX(state_len, strlen(j->state)); | |
2224 | } | |
75add28a | 2225 | |
f459b602 MAP |
2226 | if (!arg_full && id_len + 1 + unit_len + type_len + 1 + state_len > columns()) { |
2227 | unit_len = MAX(33u, columns() - id_len - type_len - state_len - 3); | |
2228 | shorten = true; | |
2229 | } | |
75add28a | 2230 | |
6ce774fd MM |
2231 | if (!arg_no_legend) |
2232 | printf("%*s %-*s %-*s %-*s\n", | |
2233 | id_len, "JOB", | |
2234 | unit_len, "UNIT", | |
2235 | type_len, "TYPE", | |
2236 | state_len, "STATE"); | |
75add28a | 2237 | |
f459b602 MAP |
2238 | for (j = jobs; j < jobs + n; j++) { |
2239 | _cleanup_free_ char *e = NULL; | |
75add28a | 2240 | |
f459b602 MAP |
2241 | if (streq(j->state, "running")) { |
2242 | on = ansi_highlight(); | |
1fc464f6 | 2243 | off = ansi_normal(); |
f459b602 MAP |
2244 | } else |
2245 | on = off = ""; | |
2246 | ||
2247 | e = shorten ? ellipsize(j->name, unit_len, 33) : NULL; | |
2248 | printf("%*u %s%-*s%s %-*s %s%-*s%s\n", | |
2249 | id_len, j->id, | |
2250 | on, unit_len, e ? e : j->name, off, | |
2251 | type_len, j->type, | |
2252 | on, state_len, j->state, off); | |
82948f6c LP |
2253 | |
2254 | if (arg_jobs_after) | |
bc357ce5 | 2255 | output_waiting_jobs(bus, j->id, "GetJobAfter", "\twaiting for job"); |
82948f6c | 2256 | if (arg_jobs_before) |
bc357ce5 | 2257 | output_waiting_jobs(bus, j->id, "GetJobBefore", "\tblocking job"); |
75add28a ZJS |
2258 | } |
2259 | ||
6ce774fd MM |
2260 | if (!arg_no_legend) { |
2261 | on = ansi_highlight(); | |
1fc464f6 | 2262 | off = ansi_normal(); |
75add28a | 2263 | |
6ce774fd MM |
2264 | printf("\n%s%u jobs listed%s.\n", on, n, off); |
2265 | } | |
75add28a ZJS |
2266 | } |
2267 | ||
d8fba7c6 | 2268 | static bool output_show_job(struct job_info *job, char **patterns) { |
2404701e | 2269 | return strv_fnmatch_or_empty(patterns, job->name, FNM_NOESCAPE); |
d8fba7c6 ZJS |
2270 | } |
2271 | ||
e449de87 | 2272 | static int list_jobs(int argc, char *argv[], void *userdata) { |
4afd3348 LP |
2273 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
2274 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
f459b602 | 2275 | _cleanup_free_ struct job_info *jobs = NULL; |
31d99bd1 YW |
2276 | const char *name, *type, *state; |
2277 | bool skipped = false; | |
f459b602 MAP |
2278 | size_t size = 0; |
2279 | unsigned c = 0; | |
4fbd7192 | 2280 | sd_bus *bus; |
f459b602 | 2281 | uint32_t id; |
f84190d8 | 2282 | int r; |
ec14911e | 2283 | |
4fbd7192 LP |
2284 | r = acquire_bus(BUS_MANAGER, &bus); |
2285 | if (r < 0) | |
2286 | return r; | |
2287 | ||
f459b602 | 2288 | r = sd_bus_call_method( |
f22f08cd SP |
2289 | bus, |
2290 | "org.freedesktop.systemd1", | |
2291 | "/org/freedesktop/systemd1", | |
2292 | "org.freedesktop.systemd1.Manager", | |
2293 | "ListJobs", | |
f459b602 | 2294 | &error, |
f22f08cd | 2295 | &reply, |
f459b602 | 2296 | NULL); |
691395d8 LP |
2297 | if (r < 0) |
2298 | return log_error_errno(r, "Failed to list jobs: %s", bus_error_message(&error, r)); | |
7e4249b9 | 2299 | |
f459b602 MAP |
2300 | r = sd_bus_message_enter_container(reply, 'a', "(usssoo)"); |
2301 | if (r < 0) | |
2302 | return bus_log_parse_error(r); | |
7e4249b9 | 2303 | |
31d99bd1 | 2304 | while ((r = sd_bus_message_read(reply, "(usssoo)", &id, &name, &type, &state, NULL, NULL)) > 0) { |
d8fba7c6 ZJS |
2305 | struct job_info job = { id, name, type, state }; |
2306 | ||
e449de87 | 2307 | if (!output_show_job(&job, strv_skip(argv, 1))) { |
d8fba7c6 ZJS |
2308 | skipped = true; |
2309 | continue; | |
2310 | } | |
8fe914ec | 2311 | |
f459b602 MAP |
2312 | if (!GREEDY_REALLOC(jobs, size, c + 1)) |
2313 | return log_oom(); | |
7e4249b9 | 2314 | |
d8fba7c6 | 2315 | jobs[c++] = job; |
7e4249b9 | 2316 | } |
f459b602 MAP |
2317 | if (r < 0) |
2318 | return bus_log_parse_error(r); | |
7e4249b9 | 2319 | |
f459b602 MAP |
2320 | r = sd_bus_message_exit_container(reply); |
2321 | if (r < 0) | |
2322 | return bus_log_parse_error(r); | |
f73e33d9 | 2323 | |
0221d68a | 2324 | (void) pager_open(arg_pager_flags); |
d2ad7e1b | 2325 | |
82948f6c | 2326 | output_jobs_list(bus, jobs, c, skipped); |
91d0f17e | 2327 | return 0; |
7e4249b9 LP |
2328 | } |
2329 | ||
e449de87 | 2330 | static int cancel_job(int argc, char *argv[], void *userdata) { |
4fbd7192 | 2331 | sd_bus *bus; |
729e3769 | 2332 | char **name; |
342641fb | 2333 | int r = 0; |
7e4249b9 | 2334 | |
e449de87 | 2335 | if (argc <= 1) |
2853b60a | 2336 | return trivial_method(argc, argv, userdata); |
ee5762e3 | 2337 | |
4fbd7192 LP |
2338 | r = acquire_bus(BUS_MANAGER, &bus); |
2339 | if (r < 0) | |
2340 | return r; | |
2341 | ||
8a4b13c5 | 2342 | polkit_agent_open_maybe(); |
d2ad7e1b | 2343 | |
e449de87 | 2344 | STRV_FOREACH(name, strv_skip(argv, 1)) { |
4afd3348 | 2345 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
5dd9014f | 2346 | uint32_t id; |
342641fb | 2347 | int q; |
7e4249b9 | 2348 | |
342641fb | 2349 | q = safe_atou32(*name, &id); |
f647962d MS |
2350 | if (q < 0) |
2351 | return log_error_errno(q, "Failed to parse job id \"%s\": %m", *name); | |
7e4249b9 | 2352 | |
6e646d22 | 2353 | q = sd_bus_call_method( |
f22f08cd SP |
2354 | bus, |
2355 | "org.freedesktop.systemd1", | |
2356 | "/org/freedesktop/systemd1", | |
2357 | "org.freedesktop.systemd1.Manager", | |
6e646d22 LP |
2358 | "CancelJob", |
2359 | &error, | |
2360 | NULL, | |
2361 | "u", id); | |
342641fb | 2362 | if (q < 0) { |
691395d8 | 2363 | log_error_errno(q, "Failed to cancel job %"PRIu32": %s", id, bus_error_message(&error, q)); |
342641fb LP |
2364 | if (r == 0) |
2365 | r = q; | |
f459b602 | 2366 | } |
7e4249b9 LP |
2367 | } |
2368 | ||
342641fb | 2369 | return r; |
7e4249b9 LP |
2370 | } |
2371 | ||
f459b602 | 2372 | static int need_daemon_reload(sd_bus *bus, const char *unit) { |
4afd3348 | 2373 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
f459b602 MAP |
2374 | const char *path; |
2375 | int b, r; | |
94c01aeb | 2376 | |
f459b602 MAP |
2377 | /* We ignore all errors here, since this is used to show a |
2378 | * warning only */ | |
45fb0699 | 2379 | |
f459b602 MAP |
2380 | /* We don't use unit_dbus_path_from_name() directly since we |
2381 | * don't want to load the unit if it isn't loaded. */ | |
f84190d8 | 2382 | |
f459b602 | 2383 | r = sd_bus_call_method( |
f22f08cd SP |
2384 | bus, |
2385 | "org.freedesktop.systemd1", | |
2386 | "/org/freedesktop/systemd1", | |
2387 | "org.freedesktop.systemd1.Manager", | |
2388 | "GetUnit", | |
f459b602 | 2389 | NULL, |
f22f08cd | 2390 | &reply, |
e3e0314b | 2391 | "s", unit); |
f84190d8 LP |
2392 | if (r < 0) |
2393 | return r; | |
45fb0699 | 2394 | |
f459b602 MAP |
2395 | r = sd_bus_message_read(reply, "o", &path); |
2396 | if (r < 0) | |
2397 | return r; | |
f84190d8 | 2398 | |
f459b602 | 2399 | r = sd_bus_get_property_trivial( |
f22f08cd | 2400 | bus, |
b0193f1c LP |
2401 | "org.freedesktop.systemd1", |
2402 | path, | |
f459b602 MAP |
2403 | "org.freedesktop.systemd1.Unit", |
2404 | "NeedDaemonReload", | |
2405 | NULL, | |
2406 | 'b', &b); | |
f84190d8 LP |
2407 | if (r < 0) |
2408 | return r; | |
45fb0699 | 2409 | |
45fb0699 LP |
2410 | return b; |
2411 | } | |
2412 | ||
3f36991e | 2413 | static void warn_unit_file_changed(const char *name) { |
87ec20ef LP |
2414 | assert(name); |
2415 | ||
e9f4aabd | 2416 | log_warning("%sWarning:%s The unit file, source configuration file or drop-ins of %s changed on disk. Run 'systemctl%s daemon-reload' to reload units.", |
3f36991e | 2417 | ansi_highlight_red(), |
1fc464f6 | 2418 | ansi_normal(), |
3f36991e ZJS |
2419 | name, |
2420 | arg_scope == UNIT_FILE_SYSTEM ? "" : " --user"); | |
2421 | } | |
2422 | ||
c45e7e0c | 2423 | static int unit_file_find_path(LookupPaths *lp, const char *unit_name, char **ret_unit_path) { |
33f6c497 ZJS |
2424 | char **p; |
2425 | ||
2426 | assert(lp); | |
2427 | assert(unit_name); | |
33f6c497 | 2428 | |
a3c4eb07 | 2429 | STRV_FOREACH(p, lp->search_path) { |
c6dd36b6 FS |
2430 | _cleanup_free_ char *path = NULL, *lpath = NULL; |
2431 | int r; | |
33f6c497 | 2432 | |
62a85ee0 | 2433 | path = path_join(*p, unit_name); |
33f6c497 ZJS |
2434 | if (!path) |
2435 | return log_oom(); | |
2436 | ||
c4f4fce7 | 2437 | r = chase_symlinks(path, arg_root, 0, &lpath); |
c6dd36b6 FS |
2438 | if (r == -ENOENT) |
2439 | continue; | |
2440 | if (r == -ENOMEM) | |
2441 | return log_oom(); | |
2442 | if (r < 0) | |
86f004fb | 2443 | return log_error_errno(r, "Failed to access path \"%s\": %m", path); |
c6dd36b6 | 2444 | |
c45e7e0c LP |
2445 | if (ret_unit_path) |
2446 | *ret_unit_path = TAKE_PTR(lpath); | |
ae2a15bc | 2447 | |
c6dd36b6 | 2448 | return 1; |
33f6c497 ZJS |
2449 | } |
2450 | ||
f67cb270 LP |
2451 | if (ret_unit_path) |
2452 | *ret_unit_path = NULL; | |
2453 | ||
33f6c497 ZJS |
2454 | return 0; |
2455 | } | |
2456 | ||
173471b7 ZJS |
2457 | static int unit_find_template_path( |
2458 | const char *unit_name, | |
2459 | LookupPaths *lp, | |
b03677e2 LP |
2460 | char **ret_fragment_path, |
2461 | char **ret_template) { | |
173471b7 | 2462 | |
b03677e2 | 2463 | _cleanup_free_ char *t = NULL, *f = NULL; |
173471b7 ZJS |
2464 | int r; |
2465 | ||
2466 | /* Returns 1 if a fragment was found, 0 if not found, negative on error. */ | |
2467 | ||
b03677e2 LP |
2468 | r = unit_file_find_path(lp, unit_name, &f); |
2469 | if (r < 0) | |
2470 | return r; | |
2471 | if (r > 0) { | |
2472 | if (ret_fragment_path) | |
2473 | *ret_fragment_path = TAKE_PTR(f); | |
2474 | if (ret_template) | |
2475 | *ret_template = NULL; | |
2476 | return r; /* found a real unit */ | |
2477 | } | |
2478 | ||
2479 | r = unit_name_template(unit_name, &t); | |
2480 | if (r == -EINVAL) { | |
2481 | if (ret_fragment_path) | |
2482 | *ret_fragment_path = NULL; | |
2483 | if (ret_template) | |
2484 | *ret_template = NULL; | |
173471b7 | 2485 | |
173471b7 | 2486 | return 0; /* not a template, does not exist */ |
b03677e2 | 2487 | } |
173471b7 ZJS |
2488 | if (r < 0) |
2489 | return log_error_errno(r, "Failed to determine template name: %m"); | |
2490 | ||
b03677e2 | 2491 | r = unit_file_find_path(lp, t, ret_fragment_path); |
173471b7 ZJS |
2492 | if (r < 0) |
2493 | return r; | |
2494 | ||
b03677e2 LP |
2495 | if (ret_template) |
2496 | *ret_template = r > 0 ? TAKE_PTR(t) : NULL; | |
ae2a15bc | 2497 | |
173471b7 ZJS |
2498 | return r; |
2499 | } | |
2500 | ||
6e646d22 LP |
2501 | static int unit_find_paths( |
2502 | sd_bus *bus, | |
2503 | const char *unit_name, | |
6e646d22 | 2504 | LookupPaths *lp, |
02a126a3 LP |
2505 | bool force_client_side, |
2506 | char **ret_fragment_path, | |
2507 | char ***ret_dropin_paths) { | |
dab2bce8 | 2508 | |
dab2bce8 | 2509 | _cleanup_strv_free_ char **dropins = NULL; |
02a126a3 | 2510 | _cleanup_free_ char *path = NULL; |
33f6c497 ZJS |
2511 | int r; |
2512 | ||
ad2a0358 | 2513 | /** |
e04df06f ZJS |
2514 | * Finds where the unit is defined on disk. Returns 0 if the unit is not found. Returns 1 if it is |
2515 | * found, and sets: | |
02a126a3 | 2516 | * - the path to the unit in *ret_frament_path, if it exists on disk, |
e04df06f ZJS |
2517 | * - and a strv of existing drop-ins in *ret_dropin_paths, if the arg is not NULL and any dropins |
2518 | * were found. | |
02a126a3 | 2519 | * |
e04df06f ZJS |
2520 | * Returns -ERFKILL if the unit is masked, and -EKEYREJECTED if the unit file could not be loaded for |
2521 | * some reason (the latter only applies if we are going through the service manager). | |
ad2a0358 ZJS |
2522 | */ |
2523 | ||
33f6c497 | 2524 | assert(unit_name); |
02a126a3 | 2525 | assert(ret_fragment_path); |
33f6c497 ZJS |
2526 | assert(lp); |
2527 | ||
02a126a3 LP |
2528 | /* Go via the bus to acquire the path, unless we are explicitly told not to, or when the unit name is a template */ |
2529 | if (!force_client_side && | |
2530 | !install_client_side() && | |
2531 | !unit_name_is_valid(unit_name, UNIT_NAME_TEMPLATE)) { | |
4afd3348 | 2532 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
02a126a3 | 2533 | _cleanup_free_ char *load_state = NULL, *unit = NULL; |
33f6c497 ZJS |
2534 | |
2535 | unit = unit_dbus_path_from_name(unit_name); | |
2536 | if (!unit) | |
2537 | return log_oom(); | |
2538 | ||
02a126a3 LP |
2539 | r = sd_bus_get_property_string( |
2540 | bus, | |
2541 | "org.freedesktop.systemd1", | |
2542 | unit, | |
2543 | "org.freedesktop.systemd1.Unit", | |
2544 | "LoadState", | |
2545 | &error, | |
2546 | &load_state); | |
2547 | if (r < 0) | |
2548 | return log_error_errno(r, "Failed to get LoadState: %s", bus_error_message(&error, r)); | |
2549 | ||
2550 | if (streq(load_state, "masked")) | |
2551 | return -ERFKILL; | |
2552 | if (streq(load_state, "not-found")) { | |
2553 | r = 0; | |
2554 | goto not_found; | |
2555 | } | |
e04df06f | 2556 | if (!STR_IN_SET(load_state, "loaded", "bad-setting")) |
02a126a3 LP |
2557 | return -EKEYREJECTED; |
2558 | ||
33f6c497 ZJS |
2559 | r = sd_bus_get_property_string( |
2560 | bus, | |
2561 | "org.freedesktop.systemd1", | |
2562 | unit, | |
2563 | "org.freedesktop.systemd1.Unit", | |
2564 | "FragmentPath", | |
2565 | &error, | |
ad2a0358 ZJS |
2566 | &path); |
2567 | if (r < 0) | |
2568 | return log_error_errno(r, "Failed to get FragmentPath: %s", bus_error_message(&error, r)); | |
2569 | ||
02a126a3 | 2570 | if (ret_dropin_paths) { |
dab2bce8 IS |
2571 | r = sd_bus_get_property_strv( |
2572 | bus, | |
2573 | "org.freedesktop.systemd1", | |
2574 | unit, | |
2575 | "org.freedesktop.systemd1.Unit", | |
2576 | "DropInPaths", | |
2577 | &error, | |
2578 | &dropins); | |
2579 | if (r < 0) | |
2580 | return log_error_errno(r, "Failed to get DropInPaths: %s", bus_error_message(&error, r)); | |
33f6c497 | 2581 | } |
ad2a0358 | 2582 | } else { |
173471b7 | 2583 | _cleanup_set_free_ Set *names = NULL; |
6483ad89 | 2584 | _cleanup_free_ char *template = NULL; |
33f6c497 | 2585 | |
ad2a0358 ZJS |
2586 | names = set_new(NULL); |
2587 | if (!names) | |
7410616c | 2588 | return log_oom(); |
33f6c497 | 2589 | |
173471b7 | 2590 | r = unit_find_template_path(unit_name, lp, &path, &template); |
ad2a0358 ZJS |
2591 | if (r < 0) |
2592 | return r; | |
86f004fb ZJS |
2593 | if (r > 0) { |
2594 | if (null_or_empty_path(path)) | |
2595 | /* The template is masked. Let's cut the process short. */ | |
2596 | return -ERFKILL; | |
2597 | ||
6483ad89 ZJS |
2598 | /* We found the unit file. If we followed symlinks, this name might be |
2599 | * different then the unit_name with started with. Look for dropins matching | |
2600 | * that "final" name. */ | |
2601 | r = set_put(names, basename(path)); | |
86f004fb | 2602 | } else if (!template) |
6483ad89 ZJS |
2603 | /* No unit file, let's look for dropins matching the original name. |
2604 | * systemd has fairly complicated rules (based on unit type and provenience), | |
2605 | * which units are allowed not to have the main unit file. We err on the | |
2606 | * side of including too many files, and always try to load dropins. */ | |
2607 | r = set_put(names, unit_name); | |
2608 | else | |
2609 | /* The cases where we allow a unit to exist without the main file are | |
2610 | * never valid for templates. Don't try to load dropins in this case. */ | |
2611 | goto not_found; | |
2612 | ||
c6dd36b6 FS |
2613 | if (r < 0) |
2614 | return log_error_errno(r, "Failed to add unit name: %m"); | |
2615 | ||
02a126a3 LP |
2616 | if (ret_dropin_paths) { |
2617 | r = unit_file_find_dropin_conf_paths(arg_root, lp->search_path, NULL, names, &dropins); | |
dab2bce8 IS |
2618 | if (r < 0) |
2619 | return r; | |
2620 | } | |
2621 | } | |
2622 | ||
02a126a3 | 2623 | r = 0; |
dab2bce8 IS |
2624 | |
2625 | if (!isempty(path)) { | |
02a126a3 | 2626 | *ret_fragment_path = TAKE_PTR(path); |
dab2bce8 | 2627 | r = 1; |
02a126a3 LP |
2628 | } else |
2629 | *ret_fragment_path = NULL; | |
dab2bce8 | 2630 | |
02a126a3 LP |
2631 | if (ret_dropin_paths) { |
2632 | if (!strv_isempty(dropins)) { | |
2633 | *ret_dropin_paths = TAKE_PTR(dropins); | |
2634 | r = 1; | |
2635 | } else | |
2636 | *ret_dropin_paths = NULL; | |
33f6c497 | 2637 | } |
02a126a3 | 2638 | |
6483ad89 | 2639 | not_found: |
39c38ce1 | 2640 | if (r == 0 && !arg_force) |
b5e6a600 IS |
2641 | log_error("No files found for %s.", unit_name); |
2642 | ||
ad2a0358 | 2643 | return r; |
33f6c497 ZJS |
2644 | } |
2645 | ||
fa0d5878 | 2646 | static int get_state_one_unit(sd_bus *bus, const char *name, UnitActiveState *active_state) { |
b430f85f | 2647 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
3d083b22 | 2648 | _cleanup_free_ char *buf = NULL, *path = NULL; |
fa0d5878 | 2649 | UnitActiveState state; |
f22f08cd | 2650 | int r; |
701cdcb9 | 2651 | |
31be1221 | 2652 | assert(name); |
fa0d5878 | 2653 | assert(active_state); |
701cdcb9 | 2654 | |
3d083b22 LP |
2655 | path = unit_dbus_path_from_name(name); |
2656 | if (!path) | |
2657 | return log_oom(); | |
2658 | ||
2659 | r = sd_bus_get_property_string( | |
f22f08cd SP |
2660 | bus, |
2661 | "org.freedesktop.systemd1", | |
3d083b22 LP |
2662 | path, |
2663 | "org.freedesktop.systemd1.Unit", | |
2664 | "ActiveState", | |
b430f85f | 2665 | &error, |
3d083b22 LP |
2666 | &buf); |
2667 | if (r < 0) | |
2668 | return log_error_errno(r, "Failed to retrieve unit state: %s", bus_error_message(&error, r)); | |
b430f85f | 2669 | |
3d083b22 | 2670 | state = unit_active_state_from_string(buf); |
2f50e59f LP |
2671 | if (state < 0) |
2672 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid unit state '%s' for: %s", buf, name); | |
701cdcb9 | 2673 | |
fa0d5878 BR |
2674 | *active_state = state; |
2675 | return 0; | |
701cdcb9 MS |
2676 | } |
2677 | ||
86f004fb | 2678 | static int unit_is_masked(sd_bus *bus, LookupPaths *lp, const char *name) { |
bd062910 | 2679 | _cleanup_free_ char *load_state = NULL; |
f22f08cd | 2680 | int r; |
701cdcb9 | 2681 | |
86f004fb ZJS |
2682 | if (unit_name_is_valid(name, UNIT_NAME_TEMPLATE)) { |
2683 | _cleanup_free_ char *path = NULL; | |
2684 | ||
2685 | /* A template cannot be loaded, but it can be still masked, so | |
2686 | * we need to use a different method. */ | |
2687 | ||
2688 | r = unit_file_find_path(lp, name, &path); | |
2689 | if (r < 0) | |
2690 | return r; | |
2691 | if (r == 0) | |
2692 | return false; | |
2693 | return null_or_empty_path(path); | |
2694 | } | |
2695 | ||
bd062910 | 2696 | r = unit_load_state(bus, name, &load_state); |
691395d8 | 2697 | if (r < 0) |
bd062910 | 2698 | return r; |
d0a5cdb2 | 2699 | |
9d9dd746 ZJS |
2700 | return streq(load_state, "masked"); |
2701 | } | |
2702 | ||
2703 | static int check_triggering_units(sd_bus *bus, const char *name) { | |
2704 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
86f004fb | 2705 | _cleanup_free_ char *n = NULL, *path = NULL, *load_state = NULL; |
9d9dd746 ZJS |
2706 | _cleanup_strv_free_ char **triggered_by = NULL; |
2707 | bool print_warning_label = true; | |
2708 | UnitActiveState active_state; | |
2709 | char **i; | |
2710 | int r; | |
2711 | ||
2712 | r = unit_name_mangle(name, 0, &n); | |
2713 | if (r < 0) | |
2714 | return log_error_errno(r, "Failed to mangle unit name: %m"); | |
2715 | ||
86f004fb ZJS |
2716 | r = unit_load_state(bus, name, &load_state); |
2717 | if (r < 0) | |
2718 | return r; | |
2719 | ||
2720 | if (streq(load_state, "masked")) | |
2721 | return 0; | |
9d9dd746 ZJS |
2722 | |
2723 | path = unit_dbus_path_from_name(n); | |
2724 | if (!path) | |
2725 | return log_oom(); | |
701cdcb9 | 2726 | |
f459b602 MAP |
2727 | r = sd_bus_get_property_strv( |
2728 | bus, | |
2729 | "org.freedesktop.systemd1", | |
2730 | path, | |
2731 | "org.freedesktop.systemd1.Unit", | |
2732 | "TriggeredBy", | |
2733 | &error, | |
2734 | &triggered_by); | |
691395d8 LP |
2735 | if (r < 0) |
2736 | return log_error_errno(r, "Failed to get triggered by array of %s: %s", n, bus_error_message(&error, r)); | |
701cdcb9 | 2737 | |
f459b602 | 2738 | STRV_FOREACH(i, triggered_by) { |
fa0d5878 | 2739 | r = get_state_one_unit(bus, *i, &active_state); |
f647962d | 2740 | if (r < 0) |
fa0d5878 | 2741 | return r; |
701cdcb9 | 2742 | |
fa0d5878 | 2743 | if (!IN_SET(active_state, UNIT_ACTIVE, UNIT_RELOADING)) |
f459b602 | 2744 | continue; |
60f9ba0b | 2745 | |
f459b602 MAP |
2746 | if (print_warning_label) { |
2747 | log_warning("Warning: Stopping %s, but it can still be activated by:", n); | |
2748 | print_warning_label = false; | |
701cdcb9 | 2749 | } |
1c291cf3 | 2750 | |
f459b602 | 2751 | log_warning(" %s", *i); |
701cdcb9 | 2752 | } |
f459b602 MAP |
2753 | |
2754 | return 0; | |
701cdcb9 MS |
2755 | } |
2756 | ||
2fc9a280 | 2757 | static const struct { |
85d9b598 LP |
2758 | const char *verb; /* systemctl verb */ |
2759 | const char *method; /* Name of the specific D-Bus method */ | |
2760 | const char *job_type; /* Job type when passing to the generic EnqueueUnitJob() method */ | |
2fc9a280 | 2761 | } unit_actions[] = { |
85d9b598 LP |
2762 | { "start", "StartUnit", "start" }, |
2763 | { "stop", "StopUnit", "stop" }, | |
2764 | { "condstop", "StopUnit", "stop" }, /* legacy alias */ | |
2765 | { "reload", "ReloadUnit", "reload" }, | |
2766 | { "restart", "RestartUnit", "restart" }, | |
2767 | { "try-restart", "TryRestartUnit", "try-restart" }, | |
2768 | { "condrestart", "TryRestartUnit", "try-restart" }, /* legacy alias */ | |
2769 | { "reload-or-restart", "ReloadOrRestartUnit", "reload-or-restart" }, | |
2770 | { "try-reload-or-restart", "ReloadOrTryRestartUnit", "reload-or-try-restart" }, | |
2771 | { "reload-or-try-restart", "ReloadOrTryRestartUnit", "reload-or-try-restart" }, /* legacy alias */ | |
2772 | { "condreload", "ReloadOrTryRestartUnit", "reload-or-try-restart" }, /* legacy alias */ | |
2773 | { "force-reload", "ReloadOrTryRestartUnit", "reload-or-try-restart" }, /* legacy alias */ | |
2fc9a280 LP |
2774 | }; |
2775 | ||
39602c39 | 2776 | static const char *verb_to_method(const char *verb) { |
85d9b598 | 2777 | size_t i; |
39602c39 TA |
2778 | |
2779 | for (i = 0; i < ELEMENTSOF(unit_actions); i++) | |
2780 | if (streq_ptr(unit_actions[i].verb, verb)) | |
2781 | return unit_actions[i].method; | |
2782 | ||
2783 | return "StartUnit"; | |
2784 | } | |
2785 | ||
85d9b598 LP |
2786 | static const char *verb_to_job_type(const char *verb) { |
2787 | size_t i; | |
39602c39 TA |
2788 | |
2789 | for (i = 0; i < ELEMENTSOF(unit_actions); i++) | |
85d9b598 LP |
2790 | if (streq_ptr(unit_actions[i].verb, verb)) |
2791 | return unit_actions[i].job_type; | |
39602c39 | 2792 | |
85d9b598 | 2793 | return "start"; |
39602c39 TA |
2794 | } |
2795 | ||
93a08841 MP |
2796 | typedef struct { |
2797 | sd_bus_slot *match; | |
2798 | sd_event *event; | |
2799 | Set *unit_paths; | |
2800 | bool any_failed; | |
2801 | } WaitContext; | |
2802 | ||
2803 | static void wait_context_free(WaitContext *c) { | |
2804 | c->match = sd_bus_slot_unref(c->match); | |
2805 | c->event = sd_event_unref(c->event); | |
6fa4160d | 2806 | c->unit_paths = set_free_free(c->unit_paths); |
93a08841 MP |
2807 | } |
2808 | ||
2809 | static int on_properties_changed(sd_bus_message *m, void *userdata, sd_bus_error *error) { | |
109b0cbf | 2810 | const char *path, *interface, *active_state = NULL, *job_path = NULL; |
93a08841 | 2811 | WaitContext *c = userdata; |
109b0cbf | 2812 | bool is_failed; |
93a08841 MP |
2813 | int r; |
2814 | ||
109b0cbf LP |
2815 | /* Called whenever we get a PropertiesChanged signal. Checks if ActiveState changed to inactive/failed. |
2816 | * | |
2817 | * Signal parameters: (s interface, a{sv} changed_properties, as invalidated_properties) */ | |
2818 | ||
93a08841 MP |
2819 | path = sd_bus_message_get_path(m); |
2820 | if (!set_contains(c->unit_paths, path)) | |
2821 | return 0; | |
2822 | ||
109b0cbf | 2823 | r = sd_bus_message_read(m, "s", &interface); |
93a08841 MP |
2824 | if (r < 0) |
2825 | return bus_log_parse_error(r); | |
6fa4160d | 2826 | |
109b0cbf LP |
2827 | if (!streq(interface, "org.freedesktop.systemd1.Unit")) /* ActiveState is on the Unit interface */ |
2828 | return 0; | |
2829 | ||
93a08841 MP |
2830 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "{sv}"); |
2831 | if (r < 0) | |
2832 | return bus_log_parse_error(r); | |
2833 | ||
109b0cbf | 2834 | for (;;) { |
93a08841 | 2835 | const char *s; |
93a08841 | 2836 | |
109b0cbf | 2837 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_DICT_ENTRY, "sv"); |
93a08841 MP |
2838 | if (r < 0) |
2839 | return bus_log_parse_error(r); | |
109b0cbf LP |
2840 | if (r == 0) /* end of array */ |
2841 | break; | |
6fa4160d | 2842 | |
109b0cbf LP |
2843 | r = sd_bus_message_read(m, "s", &s); /* Property name */ |
2844 | if (r < 0) | |
2845 | return bus_log_parse_error(r); | |
6fa4160d | 2846 | |
109b0cbf LP |
2847 | if (streq(s, "ActiveState")) { |
2848 | r = sd_bus_message_read(m, "v", "s", &active_state); | |
93a08841 MP |
2849 | if (r < 0) |
2850 | return bus_log_parse_error(r); | |
6fa4160d | 2851 | |
109b0cbf LP |
2852 | if (job_path) /* Found everything we need */ |
2853 | break; | |
2854 | ||
2855 | } else if (streq(s, "Job")) { | |
2856 | uint32_t job_id; | |
2857 | ||
2858 | r = sd_bus_message_read(m, "v", "(uo)", &job_id, &job_path); | |
93a08841 MP |
2859 | if (r < 0) |
2860 | return bus_log_parse_error(r); | |
6fa4160d | 2861 | |
109b0cbf LP |
2862 | /* There's still a job pending for this unit, let's ignore this for now, and return right-away. */ |
2863 | if (job_id != 0) | |
2864 | return 0; | |
2865 | ||
2866 | if (active_state) /* Found everything we need */ | |
2867 | break; | |
6fa4160d | 2868 | |
93a08841 | 2869 | } else { |
109b0cbf | 2870 | r = sd_bus_message_skip(m, "v"); /* Other property */ |
93a08841 MP |
2871 | if (r < 0) |
2872 | return bus_log_parse_error(r); | |
2873 | } | |
109b0cbf | 2874 | |
93a08841 MP |
2875 | r = sd_bus_message_exit_container(m); |
2876 | if (r < 0) | |
2877 | return bus_log_parse_error(r); | |
2878 | } | |
109b0cbf LP |
2879 | |
2880 | /* If this didn't contain the ActiveState property we can't do anything */ | |
2881 | if (!active_state) | |
2882 | return 0; | |
2883 | ||
2884 | is_failed = streq(active_state, "failed"); | |
2885 | if (streq(active_state, "inactive") || is_failed) { | |
2886 | log_debug("%s became %s, dropping from --wait tracking", path, active_state); | |
2887 | free(set_remove(c->unit_paths, path)); | |
2888 | c->any_failed = c->any_failed || is_failed; | |
2889 | } else | |
2890 | log_debug("ActiveState on %s changed to %s", path, active_state); | |
93a08841 MP |
2891 | |
2892 | if (set_isempty(c->unit_paths)) | |
2893 | sd_event_exit(c->event, EXIT_SUCCESS); | |
2894 | ||
2895 | return 0; | |
2896 | } | |
2897 | ||
9a199645 LP |
2898 | static int wait_context_watch( |
2899 | WaitContext *wait_context, | |
2900 | sd_bus *bus, | |
2901 | const char *name) { | |
2902 | ||
2903 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
2904 | _cleanup_free_ char *unit_path = NULL; | |
2905 | int r; | |
2906 | ||
2907 | assert(wait_context); | |
2908 | assert(name); | |
2909 | ||
2910 | log_debug("Watching for property changes of %s", name); | |
2911 | r = sd_bus_call_method( | |
2912 | bus, | |
2913 | "org.freedesktop.systemd1", | |
2914 | "/org/freedesktop/systemd1", | |
2915 | "org.freedesktop.systemd1.Manager", | |
2916 | "RefUnit", | |
2917 | &error, | |
2918 | NULL, | |
2919 | "s", name); | |
2920 | if (r < 0) | |
2921 | return log_error_errno(r, "Failed to add reference to unit %s: %s", name, bus_error_message(&error, r)); | |
2922 | ||
2923 | unit_path = unit_dbus_path_from_name(name); | |
2924 | if (!unit_path) | |
2925 | return log_oom(); | |
2926 | ||
2927 | r = set_ensure_allocated(&wait_context->unit_paths, &string_hash_ops); | |
2928 | if (r < 0) | |
2929 | return log_oom(); | |
2930 | ||
2931 | r = set_put_strdup(wait_context->unit_paths, unit_path); | |
2932 | if (r < 0) | |
2933 | return log_error_errno(r, "Failed to add unit path %s to set: %m", unit_path); | |
2934 | ||
2935 | r = sd_bus_match_signal_async(bus, | |
2936 | &wait_context->match, | |
2937 | NULL, | |
2938 | unit_path, | |
2939 | "org.freedesktop.DBus.Properties", | |
2940 | "PropertiesChanged", | |
2941 | on_properties_changed, NULL, wait_context); | |
2942 | if (r < 0) | |
2943 | return log_error_errno(r, "Failed to request match for PropertiesChanged signal: %m"); | |
2944 | ||
2945 | return 0; | |
2946 | } | |
2947 | ||
e4b61340 | 2948 | static int start_unit_one( |
f459b602 | 2949 | sd_bus *bus, |
85d9b598 LP |
2950 | const char *method, /* When using classic per-job bus methods */ |
2951 | const char *job_type, /* When using new-style EnqueueUnitJob() */ | |
e4b61340 LP |
2952 | const char *name, |
2953 | const char *mode, | |
f459b602 | 2954 | sd_bus_error *error, |
93a08841 MP |
2955 | BusWaitForJobs *w, |
2956 | WaitContext *wait_context) { | |
7e4249b9 | 2957 | |
4afd3348 | 2958 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
45fb0699 | 2959 | const char *path; |
85d9b598 | 2960 | bool done = false; |
7e4249b9 | 2961 | int r; |
7e4249b9 | 2962 | |
e4b61340 LP |
2963 | assert(method); |
2964 | assert(name); | |
2965 | assert(mode); | |
22f4096c | 2966 | assert(error); |
7e4249b9 | 2967 | |
93a08841 | 2968 | if (wait_context) { |
9a199645 | 2969 | r = wait_context_watch(wait_context, bus, name); |
93a08841 | 2970 | if (r < 0) |
9a199645 | 2971 | return r; |
93a08841 MP |
2972 | } |
2973 | ||
dbc9830c ZJS |
2974 | log_debug("%s dbus call org.freedesktop.systemd1.Manager %s(%s, %s)", |
2975 | arg_dry_run ? "Would execute" : "Executing", | |
1ae17672 | 2976 | method, name, mode); |
85d9b598 | 2977 | |
1ae17672 ZJS |
2978 | if (arg_dry_run) |
2979 | return 0; | |
342641fb | 2980 | |
85d9b598 LP |
2981 | if (arg_show_transaction) { |
2982 | _cleanup_(sd_bus_error_free) sd_bus_error enqueue_error = SD_BUS_ERROR_NULL; | |
39602c39 | 2983 | |
85d9b598 LP |
2984 | /* Use the new, fancy EnqueueUnitJob() API if the user wants us to print the transaction */ |
2985 | r = sd_bus_call_method( | |
2986 | bus, | |
2987 | "org.freedesktop.systemd1", | |
2988 | "/org/freedesktop/systemd1", | |
2989 | "org.freedesktop.systemd1.Manager", | |
2990 | "EnqueueUnitJob", | |
2991 | &enqueue_error, | |
2992 | &reply, | |
2993 | "sss", | |
2994 | name, job_type, mode); | |
2995 | if (r < 0) { | |
2996 | if (!sd_bus_error_has_name(&enqueue_error, SD_BUS_ERROR_UNKNOWN_METHOD)) { | |
2997 | (void) sd_bus_error_move(error, &enqueue_error); | |
2998 | goto fail; | |
2999 | } | |
67f3c402 | 3000 | |
85d9b598 LP |
3001 | /* Hmm, the API is not yet available. Let's use the classic API instead (see below). */ |
3002 | log_notice("--show-transaction not supported by this service manager, proceeding without."); | |
3003 | } else { | |
3004 | const char *u, *jt; | |
3005 | uint32_t id; | |
39602c39 | 3006 | |
85d9b598 LP |
3007 | r = sd_bus_message_read(reply, "uosos", &id, &path, &u, NULL, &jt); |
3008 | if (r < 0) | |
3009 | return bus_log_parse_error(r); | |
ee87525c | 3010 | |
85d9b598 | 3011 | log_info("Enqueued anchor job %" PRIu32 " %s/%s.", id, u, jt); |
ee87525c | 3012 | |
85d9b598 LP |
3013 | r = sd_bus_message_enter_container(reply, 'a', "(uosos)"); |
3014 | if (r < 0) | |
3015 | return bus_log_parse_error(r); | |
3016 | for (;;) { | |
3017 | r = sd_bus_message_read(reply, "(uosos)", &id, NULL, &u, NULL, &jt); | |
3018 | if (r < 0) | |
3019 | return bus_log_parse_error(r); | |
3020 | if (r == 0) | |
3021 | break; | |
3022 | ||
3023 | log_info("Enqueued auxiliary job %" PRIu32 " %s/%s.", id, u, jt); | |
3024 | } | |
3025 | ||
3026 | r = sd_bus_message_exit_container(reply); | |
3027 | if (r < 0) | |
3028 | return bus_log_parse_error(r); | |
3029 | ||
3030 | done = true; | |
3031 | } | |
7e4249b9 LP |
3032 | } |
3033 | ||
85d9b598 LP |
3034 | if (!done) { |
3035 | r = sd_bus_call_method( | |
3036 | bus, | |
3037 | "org.freedesktop.systemd1", | |
3038 | "/org/freedesktop/systemd1", | |
3039 | "org.freedesktop.systemd1.Manager", | |
3040 | method, | |
3041 | error, | |
3042 | &reply, | |
3043 | "ss", name, mode); | |
3044 | if (r < 0) | |
3045 | goto fail; | |
3046 | ||
3047 | r = sd_bus_message_read(reply, "o", &path); | |
3048 | if (r < 0) | |
3049 | return bus_log_parse_error(r); | |
3050 | } | |
45fb0699 | 3051 | |
e3e0314b | 3052 | if (need_daemon_reload(bus, name) > 0) |
3f36991e | 3053 | warn_unit_file_changed(name); |
45fb0699 | 3054 | |
ebd011d9 LP |
3055 | if (w) { |
3056 | log_debug("Adding %s to the set", path); | |
3057 | r = bus_wait_for_jobs_add(w, path); | |
cbc9fbd1 | 3058 | if (r < 0) |
d2e1b2fd | 3059 | return log_error_errno(r, "Failed to watch job for %s: %m", name); |
e4b61340 | 3060 | } |
7e4249b9 | 3061 | |
46eddbb5 | 3062 | return 0; |
85d9b598 LP |
3063 | |
3064 | fail: | |
3065 | /* There's always a fallback possible for legacy actions. */ | |
3066 | if (arg_action != ACTION_SYSTEMCTL) | |
3067 | return r; | |
3068 | ||
3069 | log_error_errno(r, "Failed to %s %s: %s", job_type, name, bus_error_message(error, r)); | |
3070 | ||
3071 | if (!sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT) && | |
3072 | !sd_bus_error_has_name(error, BUS_ERROR_UNIT_MASKED) && | |
3073 | !sd_bus_error_has_name(error, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE)) | |
3074 | log_error("See %s logs and 'systemctl%s status%s %s' for details.", | |
3075 | arg_scope == UNIT_FILE_SYSTEM ? "system" : "user", | |
3076 | arg_scope == UNIT_FILE_SYSTEM ? "" : " --user", | |
3077 | name[0] == '-' ? " --" : "", | |
3078 | name); | |
3079 | ||
3080 | return r; | |
7e4249b9 LP |
3081 | } |
3082 | ||
e3e0314b | 3083 | static int expand_names(sd_bus *bus, char **names, const char* suffix, char ***ret) { |
e3e0314b ZJS |
3084 | _cleanup_strv_free_ char **mangled = NULL, **globs = NULL; |
3085 | char **name; | |
7410616c | 3086 | int r, i; |
e3e0314b | 3087 | |
4fbd7192 LP |
3088 | assert(bus); |
3089 | assert(ret); | |
3090 | ||
e3e0314b ZJS |
3091 | STRV_FOREACH(name, names) { |
3092 | char *t; | |
37cbc1d5 | 3093 | UnitNameMangle options = UNIT_NAME_MANGLE_GLOB | (arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN); |
e3e0314b | 3094 | |
e80733be | 3095 | if (suffix) |
37cbc1d5 | 3096 | r = unit_name_mangle_with_suffix(*name, options, suffix, &t); |
e80733be | 3097 | else |
37cbc1d5 | 3098 | r = unit_name_mangle(*name, options, &t); |
7410616c LP |
3099 | if (r < 0) |
3100 | return log_error_errno(r, "Failed to mangle name: %m"); | |
e3e0314b ZJS |
3101 | |
3102 | if (string_is_glob(t)) | |
6e18964d | 3103 | r = strv_consume(&globs, t); |
e3e0314b | 3104 | else |
6e18964d ZJS |
3105 | r = strv_consume(&mangled, t); |
3106 | if (r < 0) | |
e3e0314b | 3107 | return log_oom(); |
e3e0314b ZJS |
3108 | } |
3109 | ||
3110 | /* Query the manager only if any of the names are a glob, since | |
3111 | * this is fairly expensive */ | |
3112 | if (!strv_isempty(globs)) { | |
4afd3348 | 3113 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
e3e0314b | 3114 | _cleanup_free_ UnitInfo *unit_infos = NULL; |
1b53f64b | 3115 | size_t allocated, n; |
e3e0314b | 3116 | |
1238ee09 | 3117 | r = get_unit_list(bus, NULL, globs, &unit_infos, 0, &reply); |
e3e0314b ZJS |
3118 | if (r < 0) |
3119 | return r; | |
3120 | ||
1b53f64b LP |
3121 | n = strv_length(mangled); |
3122 | allocated = n + 1; | |
3123 | ||
3124 | for (i = 0; i < r; i++) { | |
3125 | if (!GREEDY_REALLOC(mangled, allocated, n+2)) | |
3126 | return log_oom(); | |
3127 | ||
3128 | mangled[n] = strdup(unit_infos[i].id); | |
3129 | if (!mangled[n]) | |
e3e0314b | 3130 | return log_oom(); |
1b53f64b LP |
3131 | |
3132 | mangled[++n] = NULL; | |
3133 | } | |
e3e0314b ZJS |
3134 | } |
3135 | ||
1cc6c93a | 3136 | *ret = TAKE_PTR(mangled); |
e3e0314b ZJS |
3137 | return 0; |
3138 | } | |
3139 | ||
47a0eaa6 MS |
3140 | static const struct { |
3141 | const char *target; | |
3142 | const char *verb; | |
3143 | const char *mode; | |
3144 | } action_table[_ACTION_MAX] = { | |
5fd77930 LP |
3145 | [ACTION_HALT] = { SPECIAL_HALT_TARGET, "halt", "replace-irreversibly" }, |
3146 | [ACTION_POWEROFF] = { SPECIAL_POWEROFF_TARGET, "poweroff", "replace-irreversibly" }, | |
3147 | [ACTION_REBOOT] = { SPECIAL_REBOOT_TARGET, "reboot", "replace-irreversibly" }, | |
3148 | [ACTION_KEXEC] = { SPECIAL_KEXEC_TARGET, "kexec", "replace-irreversibly" }, | |
3149 | [ACTION_RUNLEVEL2] = { SPECIAL_MULTI_USER_TARGET, NULL, "isolate" }, | |
3150 | [ACTION_RUNLEVEL3] = { SPECIAL_MULTI_USER_TARGET, NULL, "isolate" }, | |
3151 | [ACTION_RUNLEVEL4] = { SPECIAL_MULTI_USER_TARGET, NULL, "isolate" }, | |
3152 | [ACTION_RUNLEVEL5] = { SPECIAL_GRAPHICAL_TARGET, NULL, "isolate" }, | |
3153 | [ACTION_RESCUE] = { SPECIAL_RESCUE_TARGET, "rescue", "isolate" }, | |
3154 | [ACTION_EMERGENCY] = { SPECIAL_EMERGENCY_TARGET, "emergency", "isolate" }, | |
3155 | [ACTION_DEFAULT] = { SPECIAL_DEFAULT_TARGET, "default", "isolate" }, | |
3156 | [ACTION_EXIT] = { SPECIAL_EXIT_TARGET, "exit", "replace-irreversibly" }, | |
3157 | [ACTION_SUSPEND] = { SPECIAL_SUSPEND_TARGET, "suspend", "replace-irreversibly" }, | |
3158 | [ACTION_HIBERNATE] = { SPECIAL_HIBERNATE_TARGET, "hibernate", "replace-irreversibly" }, | |
3159 | [ACTION_HYBRID_SLEEP] = { SPECIAL_HYBRID_SLEEP_TARGET, "hybrid-sleep", "replace-irreversibly" }, | |
e68c79db | 3160 | [ACTION_SUSPEND_THEN_HIBERNATE] = { SPECIAL_SUSPEND_THEN_HIBERNATE_TARGET, "suspend-then-hibernate", "replace-irreversibly" }, |
47a0eaa6 MS |
3161 | }; |
3162 | ||
514f4ef5 | 3163 | static enum action verb_to_action(const char *verb) { |
47a0eaa6 MS |
3164 | enum action i; |
3165 | ||
913c1916 | 3166 | for (i = 0; i < _ACTION_MAX; i++) |
f459b602 | 3167 | if (streq_ptr(action_table[i].verb, verb)) |
47a0eaa6 | 3168 | return i; |
514f4ef5 | 3169 | |
f459b602 MAP |
3170 | return _ACTION_INVALID; |
3171 | } | |
e4b61340 | 3172 | |
94369fc0 LP |
3173 | static const char** make_extra_args(const char *extra_args[static 4]) { |
3174 | size_t n = 0; | |
3175 | ||
3176 | if (arg_scope != UNIT_FILE_SYSTEM) | |
3177 | extra_args[n++] = "--user"; | |
3178 | ||
3179 | if (arg_transport == BUS_TRANSPORT_REMOTE) { | |
3180 | extra_args[n++] = "-H"; | |
3181 | extra_args[n++] = arg_host; | |
3182 | } else if (arg_transport == BUS_TRANSPORT_MACHINE) { | |
3183 | extra_args[n++] = "-M"; | |
3184 | extra_args[n++] = arg_host; | |
3185 | } else | |
3186 | assert(arg_transport == BUS_TRANSPORT_LOCAL); | |
3187 | ||
3188 | extra_args[n] = NULL; | |
3189 | return extra_args; | |
3190 | } | |
3191 | ||
e449de87 | 3192 | static int start_unit(int argc, char *argv[], void *userdata) { |
ebd011d9 | 3193 | _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL; |
0e8d9c0c | 3194 | _cleanup_(wait_context_free) WaitContext wait_context = {}; |
85d9b598 | 3195 | const char *method, *job_type, *mode, *one_name, *suffix = NULL; |
416b8801 | 3196 | _cleanup_free_ char **stopped_units = NULL; /* Do not use _cleanup_strv_free_ */ |
ebd011d9 | 3197 | _cleanup_strv_free_ char **names = NULL; |
0e8d9c0c | 3198 | int r, ret = EXIT_SUCCESS; |
4fbd7192 | 3199 | sd_bus *bus; |
729e3769 | 3200 | char **name; |
e4b61340 | 3201 | |
2f50e59f LP |
3202 | if (arg_wait && !STR_IN_SET(argv[0], "start", "restart")) |
3203 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
3204 | "--wait may only be used with the 'start' or 'restart' commands."); | |
93a08841 MP |
3205 | |
3206 | /* we cannot do sender tracking on the private bus, so we need the full | |
3207 | * one for RefUnit to implement --wait */ | |
3208 | r = acquire_bus(arg_wait ? BUS_FULL : BUS_MANAGER, &bus); | |
4fbd7192 LP |
3209 | if (r < 0) |
3210 | return r; | |
3211 | ||
d2ad7e1b | 3212 | ask_password_agent_open_if_enabled(); |
8a4b13c5 | 3213 | polkit_agent_open_maybe(); |
d2ad7e1b | 3214 | |
e4b61340 | 3215 | if (arg_action == ACTION_SYSTEMCTL) { |
47a0eaa6 | 3216 | enum action action; |
e4b61340 | 3217 | |
e449de87 LP |
3218 | action = verb_to_action(argv[0]); |
3219 | ||
913c1916 | 3220 | if (action != _ACTION_INVALID) { |
85d9b598 | 3221 | /* A command in style "systemctl reboot", "systemctl poweroff", … */ |
913c1916 | 3222 | method = "StartUnit"; |
85d9b598 | 3223 | job_type = "start"; |
913c1916 AJ |
3224 | mode = action_table[action].mode; |
3225 | one_name = action_table[action].target; | |
3226 | } else { | |
3227 | if (streq(argv[0], "isolate")) { | |
85d9b598 | 3228 | /* A "systemctl isolate <unit1> <unit2> …" command */ |
913c1916 | 3229 | method = "StartUnit"; |
85d9b598 | 3230 | job_type = "start"; |
913c1916 | 3231 | mode = "isolate"; |
913c1916 AJ |
3232 | suffix = ".target"; |
3233 | } else { | |
85d9b598 | 3234 | /* A command in style of "systemctl start <unit1> <unit2> …", "sysemctl stop <unit1> <unit2> …" and so on */ |
913c1916 | 3235 | method = verb_to_method(argv[0]); |
85d9b598 | 3236 | job_type = verb_to_job_type(argv[0]); |
913c1916 AJ |
3237 | mode = arg_job_mode; |
3238 | } | |
3239 | one_name = NULL; | |
3240 | } | |
e4b61340 | 3241 | } else { |
85d9b598 | 3242 | /* A SysV legacy command such as "halt", "reboot", "poweroff", … */ |
78ca9099 | 3243 | assert(arg_action >= 0 && arg_action < _ACTION_MAX); |
47a0eaa6 | 3244 | assert(action_table[arg_action].target); |
913c1916 | 3245 | assert(action_table[arg_action].mode); |
e4b61340 LP |
3246 | |
3247 | method = "StartUnit"; | |
85d9b598 | 3248 | job_type = "start"; |
47a0eaa6 | 3249 | mode = action_table[arg_action].mode; |
e3e0314b | 3250 | one_name = action_table[arg_action].target; |
514f4ef5 LP |
3251 | } |
3252 | ||
8b7ec7bb | 3253 | if (one_name) { |
bea1a013 | 3254 | names = strv_new(one_name); |
8b7ec7bb LP |
3255 | if (!names) |
3256 | return log_oom(); | |
3257 | } else { | |
e449de87 | 3258 | r = expand_names(bus, strv_skip(argv, 1), suffix, &names); |
e3e0314b | 3259 | if (r < 0) |
691395d8 | 3260 | return log_error_errno(r, "Failed to expand names: %m"); |
e3e0314b | 3261 | } |
b6520546 | 3262 | |
6e905d93 | 3263 | if (!arg_no_block) { |
ebd011d9 | 3264 | r = bus_wait_for_jobs_new(bus, &w); |
f647962d MS |
3265 | if (r < 0) |
3266 | return log_error_errno(r, "Could not watch jobs: %m"); | |
e4b61340 LP |
3267 | } |
3268 | ||
93a08841 | 3269 | if (arg_wait) { |
31b2cd5d | 3270 | r = sd_bus_call_method_async( |
93a08841 | 3271 | bus, |
31b2cd5d | 3272 | NULL, |
93a08841 MP |
3273 | "org.freedesktop.systemd1", |
3274 | "/org/freedesktop/systemd1", | |
3275 | "org.freedesktop.systemd1.Manager", | |
3276 | "Subscribe", | |
31b2cd5d LP |
3277 | NULL, NULL, |
3278 | NULL); | |
93a08841 | 3279 | if (r < 0) |
31b2cd5d | 3280 | return log_error_errno(r, "Failed to enable subscription: %m"); |
85d9b598 | 3281 | |
93a08841 MP |
3282 | r = sd_event_default(&wait_context.event); |
3283 | if (r < 0) | |
3284 | return log_error_errno(r, "Failed to allocate event loop: %m"); | |
85d9b598 | 3285 | |
93a08841 MP |
3286 | r = sd_bus_attach_event(bus, wait_context.event, 0); |
3287 | if (r < 0) | |
3288 | return log_error_errno(r, "Failed to attach bus to event loop: %m"); | |
3289 | } | |
3290 | ||
b6520546 | 3291 | STRV_FOREACH(name, names) { |
4afd3348 | 3292 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
f459b602 | 3293 | |
85d9b598 | 3294 | r = start_unit_one(bus, method, job_type, *name, mode, &error, w, arg_wait ? &wait_context : NULL); |
0e8d9c0c LP |
3295 | if (ret == EXIT_SUCCESS && r < 0) |
3296 | ret = translate_bus_error_to_exit_status(r, &error); | |
416b8801 YW |
3297 | |
3298 | if (r >= 0 && streq(method, "StopUnit")) { | |
3299 | r = strv_push(&stopped_units, *name); | |
3300 | if (r < 0) | |
3301 | return log_oom(); | |
3302 | } | |
e4b61340 LP |
3303 | } |
3304 | ||
67f3c402 | 3305 | if (!arg_no_block) { |
94369fc0 | 3306 | const char* extra_args[4]; |
f459b602 | 3307 | |
94369fc0 | 3308 | r = bus_wait_for_jobs(w, arg_quiet, make_extra_args(extra_args)); |
0e8d9c0c LP |
3309 | if (r < 0) |
3310 | return r; | |
49111a70 ZJS |
3311 | |
3312 | /* When stopping units, warn if they can still be triggered by | |
3313 | * another active unit (socket, path, timer) */ | |
416b8801 YW |
3314 | if (!arg_quiet) |
3315 | STRV_FOREACH(name, stopped_units) | |
86f004fb | 3316 | (void) check_triggering_units(bus, *name); |
67f3c402 | 3317 | } |
514f4ef5 | 3318 | |
0e8d9c0c LP |
3319 | if (ret == EXIT_SUCCESS && arg_wait && !set_isempty(wait_context.unit_paths)) { |
3320 | r = sd_event_loop(wait_context.event); | |
3321 | if (r < 0) | |
3322 | return log_error_errno(r, "Failed to run event loop: %m"); | |
93a08841 | 3323 | if (wait_context.any_failed) |
0e8d9c0c | 3324 | ret = EXIT_FAILURE; |
93a08841 MP |
3325 | } |
3326 | ||
0e8d9c0c | 3327 | return ret; |
e4b61340 LP |
3328 | } |
3329 | ||
349cc4a5 | 3330 | #if ENABLE_LOGIND |
4fbd7192 | 3331 | static int logind_set_wall_message(void) { |
4afd3348 | 3332 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
4fbd7192 | 3333 | sd_bus *bus; |
f2d11d35 LP |
3334 | _cleanup_free_ char *m = NULL; |
3335 | int r; | |
3336 | ||
4fbd7192 LP |
3337 | r = acquire_bus(BUS_FULL, &bus); |
3338 | if (r < 0) | |
3339 | return r; | |
f2d11d35 LP |
3340 | |
3341 | m = strv_join(arg_wall, " "); | |
3342 | if (!m) | |
3343 | return log_oom(); | |
3344 | ||
dbc9830c ZJS |
3345 | log_debug("%s wall message \"%s\".", arg_dry_run ? "Would set" : "Setting", m); |
3346 | if (arg_dry_run) | |
3347 | return 0; | |
3348 | ||
f2d11d35 LP |
3349 | r = sd_bus_call_method( |
3350 | bus, | |
3351 | "org.freedesktop.login1", | |
3352 | "/org/freedesktop/login1", | |
3353 | "org.freedesktop.login1.Manager", | |
3354 | "SetWallMessage", | |
3355 | &error, | |
3356 | NULL, | |
3357 | "sb", | |
3358 | m, | |
3359 | !arg_no_wall); | |
3360 | ||
3361 | if (r < 0) | |
3362 | return log_warning_errno(r, "Failed to set wall message, ignoring: %s", bus_error_message(&error, r)); | |
f2d11d35 LP |
3363 | return 0; |
3364 | } | |
79f1c8f6 | 3365 | #endif |
f2d11d35 | 3366 | |
c45e5fb8 | 3367 | /* Ask systemd-logind, which might grant access to unprivileged users through polkit */ |
4fbd7192 | 3368 | static int logind_reboot(enum action a) { |
349cc4a5 | 3369 | #if ENABLE_LOGIND |
c45e5fb8 LP |
3370 | static const struct { |
3371 | const char *method; | |
3372 | const char *description; | |
3373 | } actions[_ACTION_MAX] = { | |
3374 | [ACTION_POWEROFF] = { "PowerOff", "power off system" }, | |
3375 | [ACTION_REBOOT] = { "Reboot", "reboot system" }, | |
3376 | [ACTION_HALT] = { "Halt", "halt system" }, | |
3377 | [ACTION_SUSPEND] = { "Suspend", "suspend system" }, | |
3378 | [ACTION_HIBERNATE] = { "Hibernate", "hibernate system" }, | |
3379 | [ACTION_HYBRID_SLEEP] = { "HybridSleep", "put system into hybrid sleep" }, | |
3380 | [ACTION_SUSPEND_THEN_HIBERNATE] = { "SuspendThenHibernate", "suspend system, hibernate later" }, | |
3381 | }; | |
3382 | ||
4afd3348 | 3383 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
4fbd7192 | 3384 | sd_bus *bus; |
f459b602 | 3385 | int r; |
4c80c73c | 3386 | |
c45e5fb8 LP |
3387 | if (a < 0 || a >= _ACTION_MAX || !actions[a].method) |
3388 | return -EINVAL; | |
3389 | ||
4fbd7192 LP |
3390 | r = acquire_bus(BUS_FULL, &bus); |
3391 | if (r < 0) | |
3392 | return r; | |
f2d11d35 | 3393 | |
8a4b13c5 | 3394 | polkit_agent_open_maybe(); |
d2ad7e1b ZJS |
3395 | (void) logind_set_wall_message(); |
3396 | ||
c45e5fb8 LP |
3397 | log_debug("%s org.freedesktop.login1.Manager %s dbus call.", arg_dry_run ? "Would execute" : "Executing", actions[a].method); |
3398 | ||
dbc9830c ZJS |
3399 | if (arg_dry_run) |
3400 | return 0; | |
3401 | ||
f459b602 | 3402 | r = sd_bus_call_method( |
f22f08cd SP |
3403 | bus, |
3404 | "org.freedesktop.login1", | |
3405 | "/org/freedesktop/login1", | |
3406 | "org.freedesktop.login1.Manager", | |
c45e5fb8 | 3407 | actions[a].method, |
f459b602 | 3408 | &error, |
f22f08cd | 3409 | NULL, |
342641fb | 3410 | "b", arg_ask_password); |
f459b602 | 3411 | if (r < 0) |
c45e5fb8 | 3412 | return log_error_errno(r, "Failed to %s via logind: %s", actions[a].description, bus_error_message(&error, r)); |
f459b602 | 3413 | |
691395d8 | 3414 | return 0; |
4c80c73c KS |
3415 | #else |
3416 | return -ENOSYS; | |
3417 | #endif | |
3418 | } | |
3419 | ||
4fbd7192 | 3420 | static int logind_check_inhibitors(enum action a) { |
349cc4a5 | 3421 | #if ENABLE_LOGIND |
4afd3348 | 3422 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
59164be4 | 3423 | _cleanup_strv_free_ char **sessions = NULL; |
f459b602 MAP |
3424 | const char *what, *who, *why, *mode; |
3425 | uint32_t uid, pid; | |
4fbd7192 | 3426 | sd_bus *bus; |
f459b602 | 3427 | unsigned c = 0; |
59164be4 | 3428 | char **s; |
f459b602 | 3429 | int r; |
b37844d3 | 3430 | |
748ebafa LP |
3431 | if (arg_ignore_inhibitors || arg_force > 0) |
3432 | return 0; | |
3433 | ||
3434 | if (arg_when > 0) | |
3435 | return 0; | |
3436 | ||
3437 | if (geteuid() == 0) | |
b37844d3 LP |
3438 | return 0; |
3439 | ||
3440 | if (!on_tty()) | |
3441 | return 0; | |
e08ab379 LP |
3442 | |
3443 | if (arg_transport != BUS_TRANSPORT_LOCAL) | |
3444 | return 0; | |
b37844d3 | 3445 | |
4fbd7192 LP |
3446 | r = acquire_bus(BUS_FULL, &bus); |
3447 | if (r < 0) | |
3448 | return r; | |
3449 | ||
f459b602 | 3450 | r = sd_bus_call_method( |
b37844d3 LP |
3451 | bus, |
3452 | "org.freedesktop.login1", | |
3453 | "/org/freedesktop/login1", | |
3454 | "org.freedesktop.login1.Manager", | |
3455 | "ListInhibitors", | |
b37844d3 | 3456 | NULL, |
f459b602 MAP |
3457 | &reply, |
3458 | NULL); | |
b37844d3 LP |
3459 | if (r < 0) |
3460 | /* If logind is not around, then there are no inhibitors... */ | |
3461 | return 0; | |
3462 | ||
4aa2beac | 3463 | r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssuu)"); |
f459b602 MAP |
3464 | if (r < 0) |
3465 | return bus_log_parse_error(r); | |
b37844d3 | 3466 | |
4aa2beac | 3467 | while ((r = sd_bus_message_read(reply, "(ssssuu)", &what, &who, &why, &mode, &uid, &pid)) > 0) { |
59164be4 | 3468 | _cleanup_free_ char *comm = NULL, *user = NULL; |
f459b602 | 3469 | _cleanup_strv_free_ char **sv = NULL; |
b37844d3 LP |
3470 | |
3471 | if (!streq(mode, "block")) | |
f459b602 | 3472 | continue; |
b37844d3 LP |
3473 | |
3474 | sv = strv_split(what, ":"); | |
3475 | if (!sv) | |
3476 | return log_oom(); | |
3477 | ||
2f50e59f LP |
3478 | if (!pid_is_valid((pid_t) pid)) |
3479 | return log_error_errno(SYNTHETIC_ERRNO(ERANGE), "Invalid PID "PID_FMT".", (pid_t) pid); | |
d028e018 | 3480 | |
b37844d3 | 3481 | if (!strv_contains(sv, |
4c315c2c IS |
3482 | IN_SET(a, |
3483 | ACTION_HALT, | |
3484 | ACTION_POWEROFF, | |
3485 | ACTION_REBOOT, | |
3486 | ACTION_KEXEC) ? "shutdown" : "sleep")) | |
f459b602 | 3487 | continue; |
b37844d3 LP |
3488 | |
3489 | get_process_comm(pid, &comm); | |
59164be4 | 3490 | user = uid_to_name(uid); |
f459b602 | 3491 | |
de0671ee | 3492 | log_warning("Operation inhibited by \"%s\" (PID "PID_FMT" \"%s\", user %s), reason is \"%s\".", |
d028e018 | 3493 | who, (pid_t) pid, strna(comm), strna(user), why); |
b37844d3 | 3494 | |
f459b602 | 3495 | c++; |
b37844d3 | 3496 | } |
f459b602 MAP |
3497 | if (r < 0) |
3498 | return bus_log_parse_error(r); | |
b37844d3 | 3499 | |
f459b602 MAP |
3500 | r = sd_bus_message_exit_container(reply); |
3501 | if (r < 0) | |
3502 | return bus_log_parse_error(r); | |
b37844d3 | 3503 | |
59164be4 LP |
3504 | /* Check for current sessions */ |
3505 | sd_get_sessions(&sessions); | |
3506 | STRV_FOREACH(s, sessions) { | |
59164be4 LP |
3507 | _cleanup_free_ char *type = NULL, *tty = NULL, *seat = NULL, *user = NULL, *service = NULL, *class = NULL; |
3508 | ||
3509 | if (sd_session_get_uid(*s, &uid) < 0 || uid == getuid()) | |
3510 | continue; | |
3511 | ||
3512 | if (sd_session_get_class(*s, &class) < 0 || !streq(class, "user")) | |
3513 | continue; | |
3514 | ||
28abce5d | 3515 | if (sd_session_get_type(*s, &type) < 0 || !STR_IN_SET(type, "x11", "wayland", "tty", "mir")) |
59164be4 LP |
3516 | continue; |
3517 | ||
3518 | sd_session_get_tty(*s, &tty); | |
3519 | sd_session_get_seat(*s, &seat); | |
3520 | sd_session_get_service(*s, &service); | |
3521 | user = uid_to_name(uid); | |
3522 | ||
3523 | log_warning("User %s is logged in on %s.", strna(user), isempty(tty) ? (isempty(seat) ? strna(service) : seat) : tty); | |
3524 | c++; | |
3525 | } | |
3526 | ||
b37844d3 LP |
3527 | if (c <= 0) |
3528 | return 0; | |
3529 | ||
59164be4 | 3530 | log_error("Please retry operation after closing inhibitors and logging out other users.\nAlternatively, ignore inhibitors and users with 'systemctl %s -i'.", |
47a0eaa6 | 3531 | action_table[a].verb); |
b37844d3 LP |
3532 | |
3533 | return -EPERM; | |
3534 | #else | |
3535 | return 0; | |
3536 | #endif | |
3537 | } | |
3538 | ||
97af80c5 LP |
3539 | static int prepare_firmware_setup(void) { |
3540 | ||
3541 | if (!arg_firmware_setup) | |
3542 | return 0; | |
3543 | ||
349cc4a5 | 3544 | #if ENABLE_LOGIND |
4afd3348 | 3545 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
4fbd7192 | 3546 | sd_bus *bus; |
a4921cf4 JJ |
3547 | int r; |
3548 | ||
4fbd7192 LP |
3549 | r = acquire_bus(BUS_FULL, &bus); |
3550 | if (r < 0) | |
3551 | return r; | |
a4921cf4 | 3552 | |
a4921cf4 JJ |
3553 | r = sd_bus_call_method( |
3554 | bus, | |
3555 | "org.freedesktop.login1", | |
3556 | "/org/freedesktop/login1", | |
3557 | "org.freedesktop.login1.Manager", | |
3558 | "SetRebootToFirmwareSetup", | |
3559 | &error, | |
3560 | NULL, | |
3561 | "b", true); | |
4fbd7192 LP |
3562 | if (r < 0) |
3563 | return log_error_errno(r, "Cannot indicate to EFI to boot into setup mode: %s", bus_error_message(&error, r)); | |
a4921cf4 JJ |
3564 | |
3565 | return 0; | |
3566 | #else | |
2f50e59f LP |
3567 | return log_error_errno(SYNTHETIC_ERRNO(ENOSYS), |
3568 | "Booting into firmware setup not supported."); | |
a4921cf4 JJ |
3569 | #endif |
3570 | } | |
3571 | ||
97af80c5 LP |
3572 | static int prepare_boot_loader_menu(void) { |
3573 | ||
3574 | if (arg_boot_loader_menu == USEC_INFINITY) | |
3575 | return 0; | |
3576 | ||
3577 | #if ENABLE_LOGIND | |
3578 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
3579 | sd_bus *bus; | |
4fbd7192 LP |
3580 | int r; |
3581 | ||
97af80c5 LP |
3582 | r = acquire_bus(BUS_FULL, &bus); |
3583 | if (r < 0) | |
3584 | return r; | |
3585 | ||
3586 | r = sd_bus_call_method( | |
3587 | bus, | |
3588 | "org.freedesktop.login1", | |
3589 | "/org/freedesktop/login1", | |
3590 | "org.freedesktop.login1.Manager", | |
3591 | "SetRebootToBootLoaderMenu", | |
3592 | &error, | |
3593 | NULL, | |
3594 | "t", arg_boot_loader_menu); | |
3595 | if (r < 0) | |
3596 | return log_error_errno(r, "Cannot indicate to boot loader to enter boot loader entry menu: %s", bus_error_message(&error, r)); | |
3597 | ||
3598 | return 0; | |
3599 | #else | |
2f50e59f LP |
3600 | return log_error_errno(SYNTHETIC_ERRNO(ENOSYS), |
3601 | "Booting into boot loader menu not supported."); | |
97af80c5 LP |
3602 | #endif |
3603 | } | |
3604 | ||
3605 | static int prepare_boot_loader_entry(void) { | |
3606 | ||
3607 | if (!arg_boot_loader_entry) | |
4fbd7192 LP |
3608 | return 0; |
3609 | ||
97af80c5 LP |
3610 | #if ENABLE_LOGIND |
3611 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
3612 | sd_bus *bus; | |
3613 | int r; | |
4fbd7192 | 3614 | |
97af80c5 LP |
3615 | r = acquire_bus(BUS_FULL, &bus); |
3616 | if (r < 0) | |
3617 | return r; | |
3618 | ||
3619 | r = sd_bus_call_method( | |
3620 | bus, | |
3621 | "org.freedesktop.login1", | |
3622 | "/org/freedesktop/login1", | |
3623 | "org.freedesktop.login1.Manager", | |
3624 | "SetRebootToBootLoaderEntry", | |
3625 | &error, | |
3626 | NULL, | |
3627 | "s", arg_boot_loader_entry); | |
3628 | if (r < 0) | |
3629 | return log_error_errno(r, "Cannot set boot into loader entry '%s': %s", arg_boot_loader_entry, bus_error_message(&error, r)); | |
4fbd7192 | 3630 | |
97af80c5 LP |
3631 | return 0; |
3632 | #else | |
ee429649 LP |
3633 | return log_error_errno(SYNTHETIC_ERRNO(ENOSYS), |
3634 | "Booting into boot loader entry not supported."); | |
97af80c5 | 3635 | #endif |
4fbd7192 LP |
3636 | } |
3637 | ||
4bb2e9d4 ZJS |
3638 | static int load_kexec_kernel(void) { |
3639 | _cleanup_(boot_config_free) BootConfig config = {}; | |
43b736a8 | 3640 | _cleanup_free_ char *kernel = NULL, *initrd = NULL, *options = NULL; |
4bb2e9d4 ZJS |
3641 | const BootEntry *e; |
3642 | pid_t pid; | |
3643 | int r; | |
3644 | ||
3645 | if (kexec_loaded()) { | |
3646 | log_debug("Kexec kernel already loaded."); | |
3647 | return 0; | |
3648 | } | |
3649 | ||
c0d73214 ZJS |
3650 | if (access(KEXEC, X_OK) < 0) |
3651 | return log_error_errno(errno, KEXEC" is not available: %m"); | |
3652 | ||
38bd74d6 | 3653 | r = boot_entries_load_config_auto(NULL, NULL, &config); |
cc5957dc LP |
3654 | if (r == -ENOKEY) /* The call doesn't log about ENOKEY, let's do so here. */ |
3655 | return log_error_errno(r, "Cannot find the ESP partition mount point."); | |
4bb2e9d4 | 3656 | if (r < 0) |
1b20d889 | 3657 | return r; |
4bb2e9d4 | 3658 | |
38bd74d6 LP |
3659 | e = boot_config_default_entry(&config); |
3660 | if (!e) | |
3661 | return log_error_errno(SYNTHETIC_ERRNO(ENOENT), | |
3662 | "No boot loader entry suitable as default, refusing to guess."); | |
3663 | ||
3664 | log_debug("Found default boot loader entry in file \"%s\"", e->path); | |
3665 | ||
78c38fe7 LP |
3666 | if (!e->kernel) |
3667 | return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), | |
3668 | "Boot entry does not refer to Linux kernel, which is not supported currently."); | |
8a1ef3bf | 3669 | if (strv_length(e->initrd) > 1) |
0f88d829 | 3670 | return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), |
8a1ef3bf | 3671 | "Boot entry specifies multiple initrds, which is not supported currently."); |
4bb2e9d4 | 3672 | |
43b736a8 | 3673 | kernel = path_join(e->root, e->kernel); |
9fc32cd9 LP |
3674 | if (!kernel) |
3675 | return log_oom(); | |
3676 | ||
3677 | if (!strv_isempty(e->initrd)) { | |
0f88d829 | 3678 | initrd = path_join(e->root, e->initrd[0]); |
9fc32cd9 LP |
3679 | if (!initrd) |
3680 | return log_oom(); | |
3681 | } | |
3682 | ||
4bb2e9d4 ZJS |
3683 | options = strv_join(e->options, " "); |
3684 | if (!options) | |
3685 | return log_oom(); | |
3686 | ||
cd086a01 ZJS |
3687 | log_full(arg_quiet ? LOG_DEBUG : LOG_INFO, |
3688 | "%s "KEXEC" --load \"%s\" --append \"%s\"%s%s%s", | |
3689 | arg_dry_run ? "Would run" : "Running", | |
3690 | kernel, | |
3691 | options, | |
3692 | initrd ? " --initrd \"" : NULL, strempty(initrd), initrd ? "\"" : ""); | |
4bb2e9d4 ZJS |
3693 | if (arg_dry_run) |
3694 | return 0; | |
3695 | ||
c943e95f | 3696 | r = safe_fork("(kexec)", FORK_WAIT|FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG, &pid); |
4c253ed1 | 3697 | if (r < 0) |
b6e1fff1 | 3698 | return r; |
4c253ed1 | 3699 | if (r == 0) { |
4bb2e9d4 ZJS |
3700 | const char* const args[] = { |
3701 | KEXEC, | |
3702 | "--load", kernel, | |
3703 | "--append", options, | |
3704 | initrd ? "--initrd" : NULL, initrd, | |
c943e95f LP |
3705 | NULL |
3706 | }; | |
4bb2e9d4 ZJS |
3707 | |
3708 | /* Child */ | |
4bb2e9d4 ZJS |
3709 | execv(args[0], (char * const *) args); |
3710 | _exit(EXIT_FAILURE); | |
4c253ed1 LP |
3711 | } |
3712 | ||
df685d57 | 3713 | return 0; |
4bb2e9d4 ZJS |
3714 | } |
3715 | ||
57ab9006 | 3716 | static int set_exit_code(uint8_t code) { |
4afd3348 | 3717 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
57ab9006 LP |
3718 | sd_bus *bus; |
3719 | int r; | |
3720 | ||
3721 | r = acquire_bus(BUS_MANAGER, &bus); | |
3722 | if (r < 0) | |
3723 | return r; | |
3724 | ||
3725 | r = sd_bus_call_method( | |
3726 | bus, | |
3727 | "org.freedesktop.systemd1", | |
3728 | "/org/freedesktop/systemd1", | |
3729 | "org.freedesktop.systemd1.Manager", | |
3730 | "SetExitCode", | |
3731 | &error, | |
3732 | NULL, | |
3733 | "y", code); | |
3734 | if (r < 0) | |
af3d8113 | 3735 | return log_error_errno(r, "Failed to set exit code: %s", bus_error_message(&error, r)); |
57ab9006 LP |
3736 | |
3737 | return 0; | |
3738 | } | |
3739 | ||
e449de87 | 3740 | static int start_special(int argc, char *argv[], void *userdata) { |
4c80c73c | 3741 | enum action a; |
983d9c90 | 3742 | int r; |
2b0f4e6f ZJS |
3743 | bool termination_action; /* an action that terminates the manager, |
3744 | * can be performed also by signal. */ | |
983d9c90 | 3745 | |
e449de87 | 3746 | assert(argv); |
514f4ef5 | 3747 | |
e449de87 | 3748 | a = verb_to_action(argv[0]); |
4c80c73c | 3749 | |
4fbd7192 | 3750 | r = logind_check_inhibitors(a); |
748ebafa LP |
3751 | if (r < 0) |
3752 | return r; | |
3753 | ||
fba868fa LP |
3754 | if (arg_force >= 2) { |
3755 | r = must_be_root(); | |
3756 | if (r < 0) | |
3757 | return r; | |
c32b90de LP |
3758 | } |
3759 | ||
4fbd7192 | 3760 | r = prepare_firmware_setup(); |
a4921cf4 JJ |
3761 | if (r < 0) |
3762 | return r; | |
5bdf2243 | 3763 | |
97af80c5 LP |
3764 | r = prepare_boot_loader_menu(); |
3765 | if (r < 0) | |
3766 | return r; | |
3767 | ||
3768 | r = prepare_boot_loader_entry(); | |
3769 | if (r < 0) | |
3770 | return r; | |
3771 | ||
e449de87 | 3772 | if (a == ACTION_REBOOT && argc > 1) { |
77defcf5 | 3773 | r = update_reboot_parameter_and_warn(argv[1], false); |
b986229e SW |
3774 | if (r < 0) |
3775 | return r; | |
57ab9006 | 3776 | |
4bb2e9d4 ZJS |
3777 | } else if (a == ACTION_KEXEC) { |
3778 | r = load_kexec_kernel(); | |
d23b5ce2 ZJS |
3779 | if (r < 0 && arg_force >= 1) |
3780 | log_notice("Failed to load kexec kernel, continuing without."); | |
3781 | else if (r < 0) | |
4bb2e9d4 ZJS |
3782 | return r; |
3783 | ||
e449de87 | 3784 | } else if (a == ACTION_EXIT && argc > 1) { |
7bbb5359 | 3785 | uint8_t code; |
287419c1 | 3786 | |
57ab9006 LP |
3787 | /* If the exit code is not given on the command line, |
3788 | * don't reset it to zero: just keep it as it might | |
3789 | * have been set previously. */ | |
287419c1 | 3790 | |
e449de87 | 3791 | r = safe_atou8(argv[1], &code); |
4fbd7192 | 3792 | if (r < 0) |
57ab9006 | 3793 | return log_error_errno(r, "Invalid exit code."); |
4fbd7192 | 3794 | |
57ab9006 | 3795 | r = set_exit_code(code); |
691395d8 | 3796 | if (r < 0) |
57ab9006 | 3797 | return r; |
b986229e SW |
3798 | } |
3799 | ||
2b0f4e6f ZJS |
3800 | termination_action = IN_SET(a, |
3801 | ACTION_HALT, | |
3802 | ACTION_POWEROFF, | |
3803 | ACTION_REBOOT); | |
3804 | if (termination_action && arg_force >= 2) | |
477def80 | 3805 | return halt_now(a); |
e606bb61 | 3806 | |
7e59bfcb | 3807 | if (arg_force >= 1 && |
5955df2c | 3808 | (termination_action || IN_SET(a, ACTION_KEXEC, ACTION_EXIT))) |
2b0f4e6f ZJS |
3809 | r = trivial_method(argc, argv, userdata); |
3810 | else { | |
3811 | /* First try logind, to allow authentication with polkit */ | |
3812 | if (IN_SET(a, | |
3813 | ACTION_POWEROFF, | |
3814 | ACTION_REBOOT, | |
36b69c31 | 3815 | ACTION_HALT, |
2b0f4e6f ZJS |
3816 | ACTION_SUSPEND, |
3817 | ACTION_HIBERNATE, | |
c58493c0 | 3818 | ACTION_HYBRID_SLEEP, |
e68c79db | 3819 | ACTION_SUSPEND_THEN_HIBERNATE)) { |
2b0f4e6f ZJS |
3820 | |
3821 | r = logind_reboot(a); | |
3822 | if (r >= 0) | |
3823 | return r; | |
3824 | if (IN_SET(r, -EOPNOTSUPP, -EINPROGRESS)) | |
3825 | /* requested operation is not supported or already in progress */ | |
3826 | return r; | |
20b09ca7 | 3827 | |
130246d2 LP |
3828 | /* On all other errors, try low-level operation. In order to minimize the difference between |
3829 | * operation with and without logind, we explicitly enable non-blocking mode for this, as | |
3830 | * logind's shutdown operations are always non-blocking. */ | |
3831 | ||
3832 | arg_no_block = true; | |
3833 | ||
3834 | } else if (IN_SET(a, ACTION_EXIT, ACTION_KEXEC)) | |
3835 | /* Since exit/kexec are so close in behaviour to power-off/reboot, let's also make them | |
3836 | * asynchronous, in order to not confuse the user needlessly with unexpected behaviour. */ | |
3837 | arg_no_block = true; | |
7089051f | 3838 | |
2b0f4e6f | 3839 | r = start_unit(argc, argv, userdata); |
4c80c73c | 3840 | } |
983d9c90 | 3841 | |
2b0f4e6f ZJS |
3842 | if (termination_action && arg_force < 2 && |
3843 | IN_SET(r, -ENOENT, -ETIMEDOUT)) | |
cc7de2ba | 3844 | log_notice("It is possible to perform action directly, see discussion of --force --force in man:systemctl(1)."); |
2b0f4e6f ZJS |
3845 | |
3846 | return r; | |
514f4ef5 LP |
3847 | } |
3848 | ||
988b3b17 ZJS |
3849 | static int start_system_special(int argc, char *argv[], void *userdata) { |
3850 | /* Like start_special above, but raises an error when running in user mode */ | |
3851 | ||
baaa35ad ZJS |
3852 | if (arg_scope != UNIT_FILE_SYSTEM) |
3853 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
3854 | "Bad action for %s mode.", | |
3855 | arg_scope == UNIT_FILE_GLOBAL ? "--global" : "--user"); | |
988b3b17 ZJS |
3856 | |
3857 | return start_special(argc, argv, userdata); | |
3858 | } | |
3859 | ||
fa0d5878 | 3860 | static int check_unit_generic(int code, const UnitActiveState good_states[], int nb_states, char **args) { |
e3e0314b | 3861 | _cleanup_strv_free_ char **names = NULL; |
fa0d5878 | 3862 | UnitActiveState active_state; |
4fbd7192 | 3863 | sd_bus *bus; |
729e3769 | 3864 | char **name; |
fa0d5878 | 3865 | int r, i; |
d60f6ad0 | 3866 | bool found = false; |
0183528f | 3867 | |
4fbd7192 LP |
3868 | r = acquire_bus(BUS_MANAGER, &bus); |
3869 | if (r < 0) | |
3870 | return r; | |
3871 | ||
e3e0314b | 3872 | r = expand_names(bus, args, NULL, &names); |
f647962d MS |
3873 | if (r < 0) |
3874 | return log_error_errno(r, "Failed to expand names: %m"); | |
e3e0314b ZJS |
3875 | |
3876 | STRV_FOREACH(name, names) { | |
fa0d5878 BR |
3877 | r = get_state_one_unit(bus, *name, &active_state); |
3878 | if (r < 0) | |
3879 | return r; | |
3880 | ||
3881 | if (!arg_quiet) | |
3882 | puts(unit_active_state_to_string(active_state)); | |
60f9ba0b | 3883 | |
fa0d5878 BR |
3884 | for (i = 0; i < nb_states; ++i) |
3885 | if (good_states[i] == active_state) | |
3886 | found = true; | |
1a0fce45 TA |
3887 | } |
3888 | ||
d60f6ad0 LN |
3889 | /* use the given return code for the case that we won't find |
3890 | * any unit which matches the list */ | |
3891 | return found ? 0 : code; | |
1a0fce45 TA |
3892 | } |
3893 | ||
e449de87 | 3894 | static int check_unit_active(int argc, char *argv[], void *userdata) { |
9897b09b LP |
3895 | static const UnitActiveState states[] = { |
3896 | UNIT_ACTIVE, | |
3897 | UNIT_RELOADING, | |
3898 | }; | |
3899 | ||
e3e0314b | 3900 | /* According to LSB: 3, "program is not running" */ |
b613907e | 3901 | return check_unit_generic(EXIT_PROGRAM_NOT_RUNNING, states, ELEMENTSOF(states), strv_skip(argv, 1)); |
e3e0314b | 3902 | } |
0183528f | 3903 | |
e449de87 | 3904 | static int check_unit_failed(int argc, char *argv[], void *userdata) { |
9897b09b LP |
3905 | static const UnitActiveState states[] = { |
3906 | UNIT_FAILED, | |
3907 | }; | |
3908 | ||
b613907e | 3909 | return check_unit_generic(EXIT_PROGRAM_DEAD_AND_PID_EXISTS, states, ELEMENTSOF(states), strv_skip(argv, 1)); |
48220598 LP |
3910 | } |
3911 | ||
e449de87 | 3912 | static int kill_unit(int argc, char *argv[], void *userdata) { |
e3e0314b | 3913 | _cleanup_strv_free_ char **names = NULL; |
ac5e3a50 | 3914 | char *kill_who = NULL, **name; |
4fbd7192 | 3915 | sd_bus *bus; |
e3e0314b | 3916 | int r, q; |
8a0867d6 | 3917 | |
4fbd7192 LP |
3918 | r = acquire_bus(BUS_MANAGER, &bus); |
3919 | if (r < 0) | |
3920 | return r; | |
3921 | ||
8a4b13c5 | 3922 | polkit_agent_open_maybe(); |
d2ad7e1b | 3923 | |
8a0867d6 LP |
3924 | if (!arg_kill_who) |
3925 | arg_kill_who = "all"; | |
3926 | ||
ac5e3a50 JS |
3927 | /* --fail was specified */ |
3928 | if (streq(arg_job_mode, "fail")) | |
81d62103 | 3929 | kill_who = strjoina(arg_kill_who, "-fail"); |
ac5e3a50 | 3930 | |
e449de87 | 3931 | r = expand_names(bus, strv_skip(argv, 1), NULL, &names); |
e3e0314b | 3932 | if (r < 0) |
691395d8 | 3933 | return log_error_errno(r, "Failed to expand names: %m"); |
60f9ba0b | 3934 | |
e3e0314b | 3935 | STRV_FOREACH(name, names) { |
4afd3348 | 3936 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
342641fb | 3937 | |
6e646d22 | 3938 | q = sd_bus_call_method( |
f22f08cd | 3939 | bus, |
b0193f1c LP |
3940 | "org.freedesktop.systemd1", |
3941 | "/org/freedesktop/systemd1", | |
3942 | "org.freedesktop.systemd1.Manager", | |
6e646d22 LP |
3943 | "KillUnit", |
3944 | &error, | |
3945 | NULL, | |
169d7fb6 | 3946 | "ssi", *name, kill_who ? kill_who : arg_kill_who, arg_signal); |
e3e0314b | 3947 | if (q < 0) { |
169d7fb6 | 3948 | log_error_errno(q, "Failed to kill unit %s: %s", *name, bus_error_message(&error, q)); |
e3e0314b ZJS |
3949 | if (r == 0) |
3950 | r = q; | |
f459b602 | 3951 | } |
8a0867d6 | 3952 | } |
f459b602 | 3953 | |
e3e0314b | 3954 | return r; |
8a0867d6 LP |
3955 | } |
3956 | ||
d81bc51f LP |
3957 | static int clean_unit(int argc, char *argv[], void *userdata) { |
3958 | _cleanup_(bus_wait_for_units_freep) BusWaitForUnits *w = NULL; | |
3959 | _cleanup_strv_free_ char **names = NULL; | |
3960 | int r, ret = EXIT_SUCCESS; | |
3961 | char **name; | |
3962 | sd_bus *bus; | |
3963 | ||
3964 | r = acquire_bus(BUS_FULL, &bus); | |
3965 | if (r < 0) | |
3966 | return r; | |
3967 | ||
3968 | polkit_agent_open_maybe(); | |
3969 | ||
3970 | if (!arg_clean_what) { | |
3971 | arg_clean_what = strv_new("cache", "runtime"); | |
3972 | if (!arg_clean_what) | |
3973 | return log_oom(); | |
3974 | } | |
3975 | ||
3976 | r = expand_names(bus, strv_skip(argv, 1), NULL, &names); | |
3977 | if (r < 0) | |
3978 | return log_error_errno(r, "Failed to expand names: %m"); | |
3979 | ||
3980 | if (!arg_no_block) { | |
3981 | r = bus_wait_for_units_new(bus, &w); | |
3982 | if (r < 0) | |
3983 | return log_error_errno(r, "Failed to allocate unit waiter: %m"); | |
3984 | } | |
3985 | ||
3986 | STRV_FOREACH(name, names) { | |
3987 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
3988 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; | |
3989 | ||
3990 | if (w) { | |
3991 | /* If we shall wait for the cleaning to complete, let's add a ref on the unit first */ | |
3992 | r = sd_bus_call_method( | |
3993 | bus, | |
3994 | "org.freedesktop.systemd1", | |
3995 | "/org/freedesktop/systemd1", | |
3996 | "org.freedesktop.systemd1.Manager", | |
3997 | "RefUnit", | |
3998 | &error, | |
3999 | NULL, | |
4000 | "s", *name); | |
4001 | if (r < 0) { | |
4002 | log_error_errno(r, "Failed to add reference to unit %s: %s", *name, bus_error_message(&error, r)); | |
4003 | if (ret == EXIT_SUCCESS) | |
4004 | ret = r; | |
4005 | continue; | |
4006 | } | |
4007 | } | |
4008 | ||
4009 | r = sd_bus_message_new_method_call( | |
4010 | bus, | |
4011 | &m, | |
4012 | "org.freedesktop.systemd1", | |
4013 | "/org/freedesktop/systemd1", | |
4014 | "org.freedesktop.systemd1.Manager", | |
4015 | "CleanUnit"); | |
4016 | if (r < 0) | |
4017 | return bus_log_create_error(r); | |
4018 | ||
4019 | r = sd_bus_message_append(m, "s", *name); | |
4020 | if (r < 0) | |
4021 | return bus_log_create_error(r); | |
4022 | ||
4023 | r = sd_bus_message_append_strv(m, arg_clean_what); | |
4024 | if (r < 0) | |
4025 | return bus_log_create_error(r); | |
4026 | ||
4027 | r = sd_bus_call(bus, m, 0, &error, NULL); | |
4028 | if (r < 0) { | |
4029 | log_error_errno(r, "Failed to clean unit %s: %s", *name, bus_error_message(&error, r)); | |
4030 | if (ret == EXIT_SUCCESS) { | |
4031 | ret = r; | |
4032 | continue; | |
4033 | } | |
4034 | } | |
4035 | ||
4036 | if (w) { | |
4037 | r = bus_wait_for_units_add_unit(w, *name, BUS_WAIT_REFFED|BUS_WAIT_FOR_MAINTENANCE_END, NULL, NULL); | |
4038 | if (r < 0) | |
4039 | return log_error_errno(r, "Failed to watch unit %s: %m", *name); | |
4040 | } | |
4041 | } | |
4042 | ||
4043 | r = bus_wait_for_units_run(w); | |
4044 | if (r < 0) | |
4045 | return log_error_errno(r, "Failed to wait for units: %m"); | |
4046 | if (r == BUS_WAIT_FAILURE) | |
4047 | ret = EXIT_FAILURE; | |
4048 | ||
4049 | return ret; | |
4050 | } | |
4051 | ||
582a507f | 4052 | typedef struct ExecStatusInfo { |
0129173a LP |
4053 | char *name; |
4054 | ||
582a507f LP |
4055 | char *path; |
4056 | char **argv; | |
4057 | ||
b708e7ce LP |
4058 | bool ignore; |
4059 | ||
582a507f LP |
4060 | usec_t start_timestamp; |
4061 | usec_t exit_timestamp; | |
4062 | pid_t pid; | |
4063 | int code; | |
4064 | int status; | |
4065 | ||
b3d59367 AZ |
4066 | ExecCommandFlags flags; |
4067 | ||
582a507f LP |
4068 | LIST_FIELDS(struct ExecStatusInfo, exec); |
4069 | } ExecStatusInfo; | |
4070 | ||
4071 | static void exec_status_info_free(ExecStatusInfo *i) { | |
4072 | assert(i); | |
4073 | ||
0129173a | 4074 | free(i->name); |
582a507f LP |
4075 | free(i->path); |
4076 | strv_free(i->argv); | |
4077 | free(i); | |
4078 | } | |
4079 | ||
b3d59367 AZ |
4080 | static int exec_status_info_deserialize(sd_bus_message *m, ExecStatusInfo *i, bool is_ex_prop) { |
4081 | _cleanup_strv_free_ char **ex_opts = NULL; | |
b21a0ef8 | 4082 | uint64_t start_timestamp, exit_timestamp, start_timestamp_monotonic, exit_timestamp_monotonic; |
f459b602 | 4083 | const char *path; |
582a507f LP |
4084 | uint32_t pid; |
4085 | int32_t code, status; | |
f459b602 | 4086 | int ignore, r; |
582a507f | 4087 | |
f459b602 | 4088 | assert(m); |
582a507f | 4089 | assert(i); |
582a507f | 4090 | |
b3d59367 | 4091 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_STRUCT, is_ex_prop ? "sasasttttuii" : "sasbttttuii"); |
f459b602 MAP |
4092 | if (r < 0) |
4093 | return bus_log_parse_error(r); | |
4094 | else if (r == 0) | |
4095 | return 0; | |
582a507f | 4096 | |
f459b602 MAP |
4097 | r = sd_bus_message_read(m, "s", &path); |
4098 | if (r < 0) | |
4099 | return bus_log_parse_error(r); | |
582a507f | 4100 | |
f84190d8 LP |
4101 | i->path = strdup(path); |
4102 | if (!i->path) | |
f459b602 | 4103 | return log_oom(); |
582a507f | 4104 | |
f459b602 MAP |
4105 | r = sd_bus_message_read_strv(m, &i->argv); |
4106 | if (r < 0) | |
4107 | return bus_log_parse_error(r); | |
4108 | ||
b3d59367 AZ |
4109 | r = is_ex_prop ? sd_bus_message_read_strv(m, &ex_opts) : sd_bus_message_read(m, "b", &ignore); |
4110 | if (r < 0) | |
4111 | return bus_log_parse_error(r); | |
4112 | ||
f459b602 | 4113 | r = sd_bus_message_read(m, |
b3d59367 | 4114 | "ttttuii", |
f459b602 MAP |
4115 | &start_timestamp, &start_timestamp_monotonic, |
4116 | &exit_timestamp, &exit_timestamp_monotonic, | |
4117 | &pid, | |
4118 | &code, &status); | |
4119 | if (r < 0) | |
4120 | return bus_log_parse_error(r); | |
582a507f | 4121 | |
b3d59367 AZ |
4122 | if (is_ex_prop) { |
4123 | r = exec_command_flags_from_strv(ex_opts, &i->flags); | |
4124 | if (r < 0) | |
4125 | return log_error_errno(r, "Failed to convert strv to ExecCommandFlags: %m"); | |
4126 | ||
4127 | i->ignore = FLAGS_SET(i->flags, EXEC_COMMAND_IGNORE_FAILURE); | |
4128 | } else | |
4129 | i->ignore = ignore; | |
4130 | ||
582a507f LP |
4131 | i->start_timestamp = (usec_t) start_timestamp; |
4132 | i->exit_timestamp = (usec_t) exit_timestamp; | |
4133 | i->pid = (pid_t) pid; | |
4134 | i->code = code; | |
4135 | i->status = status; | |
4136 | ||
f459b602 MAP |
4137 | r = sd_bus_message_exit_container(m); |
4138 | if (r < 0) | |
4139 | return bus_log_parse_error(r); | |
4140 | ||
4141 | return 1; | |
582a507f LP |
4142 | } |
4143 | ||
d5db7fe6 WC |
4144 | typedef struct UnitCondition { |
4145 | char *name; | |
b1ed76ae | 4146 | char *param; |
d5db7fe6 WC |
4147 | bool trigger; |
4148 | bool negate; | |
d5db7fe6 WC |
4149 | int tristate; |
4150 | ||
b1ed76ae | 4151 | LIST_FIELDS(struct UnitCondition, conditions); |
d5db7fe6 WC |
4152 | } UnitCondition; |
4153 | ||
4154 | static void unit_condition_free(UnitCondition *c) { | |
9bb71940 ZJS |
4155 | if (!c) |
4156 | return; | |
d5db7fe6 WC |
4157 | |
4158 | free(c->name); | |
4159 | free(c->param); | |
4160 | free(c); | |
4161 | } | |
4162 | ||
9bb71940 ZJS |
4163 | DEFINE_TRIVIAL_CLEANUP_FUNC(UnitCondition*, unit_condition_free); |
4164 | ||
61cbdc4b LP |
4165 | typedef struct UnitStatusInfo { |
4166 | const char *id; | |
4167 | const char *load_state; | |
4168 | const char *active_state; | |
4169 | const char *sub_state; | |
a4375746 | 4170 | const char *unit_file_state; |
d2dc52db | 4171 | const char *unit_file_preset; |
61cbdc4b LP |
4172 | |
4173 | const char *description; | |
4a9e2fff | 4174 | const char *following; |
61cbdc4b | 4175 | |
49dbfa7b LP |
4176 | char **documentation; |
4177 | ||
1b64d026 LP |
4178 | const char *fragment_path; |
4179 | const char *source_path; | |
4ad49000 | 4180 | const char *control_group; |
61cbdc4b | 4181 | |
76d14b87 OS |
4182 | char **dropin_paths; |
4183 | ||
9f39404c | 4184 | const char *load_error; |
f42806df | 4185 | const char *result; |
9f39404c | 4186 | |
584be568 | 4187 | usec_t inactive_exit_timestamp; |
df50185b | 4188 | usec_t inactive_exit_timestamp_monotonic; |
584be568 LP |
4189 | usec_t active_enter_timestamp; |
4190 | usec_t active_exit_timestamp; | |
4191 | usec_t inactive_enter_timestamp; | |
4192 | ||
45fb0699 | 4193 | bool need_daemon_reload; |
055ef36b | 4194 | bool transient; |
45fb0699 | 4195 | |
61cbdc4b LP |
4196 | /* Service */ |
4197 | pid_t main_pid; | |
4198 | pid_t control_pid; | |
4199 | const char *status_text; | |
175728c4 | 4200 | const char *pid_file; |
d06dacd0 | 4201 | bool running:1; |
b4af5a80 | 4202 | int status_errno; |
61cbdc4b LP |
4203 | |
4204 | usec_t start_timestamp; | |
4205 | usec_t exit_timestamp; | |
4206 | ||
4207 | int exit_code, exit_status; | |
4208 | ||
90bbc946 LP |
4209 | usec_t condition_timestamp; |
4210 | bool condition_result; | |
b1ed76ae | 4211 | LIST_HEAD(UnitCondition, conditions); |
59fccdc5 LP |
4212 | |
4213 | usec_t assert_timestamp; | |
4214 | bool assert_result; | |
4215 | bool failed_assert_trigger; | |
4216 | bool failed_assert_negate; | |
4217 | const char *failed_assert; | |
4218 | const char *failed_assert_parameter; | |
601b0842 GS |
4219 | usec_t next_elapse_real; |
4220 | usec_t next_elapse_monotonic; | |
90bbc946 | 4221 | |
61cbdc4b LP |
4222 | /* Socket */ |
4223 | unsigned n_accepted; | |
4224 | unsigned n_connections; | |
a98f7575 | 4225 | unsigned n_refused; |
b8131a87 | 4226 | bool accept; |
61cbdc4b | 4227 | |
13160134 | 4228 | /* Pairs of type, path */ |
67419600 OS |
4229 | char **listen; |
4230 | ||
61cbdc4b LP |
4231 | /* Device */ |
4232 | const char *sysfs_path; | |
4233 | ||
4234 | /* Mount, Automount */ | |
4235 | const char *where; | |
4236 | ||
4237 | /* Swap */ | |
4238 | const char *what; | |
582a507f | 4239 | |
934277fe LP |
4240 | /* CGroup */ |
4241 | uint64_t memory_current; | |
48422635 | 4242 | uint64_t memory_min; |
da4d897e TH |
4243 | uint64_t memory_low; |
4244 | uint64_t memory_high; | |
4245 | uint64_t memory_max; | |
96e131ea | 4246 | uint64_t memory_swap_max; |
934277fe | 4247 | uint64_t memory_limit; |
5ad096b3 | 4248 | uint64_t cpu_usage_nsec; |
03a7b521 LP |
4249 | uint64_t tasks_current; |
4250 | uint64_t tasks_max; | |
0e97c93f DM |
4251 | uint64_t ip_ingress_bytes; |
4252 | uint64_t ip_egress_bytes; | |
7a5de3f9 LP |
4253 | uint64_t io_read_bytes; |
4254 | uint64_t io_write_bytes; | |
0e97c93f | 4255 | |
7ad5439e | 4256 | uint64_t default_memory_min; |
c52db42b CD |
4257 | uint64_t default_memory_low; |
4258 | ||
582a507f | 4259 | LIST_HEAD(ExecStatusInfo, exec); |
61cbdc4b LP |
4260 | } UnitStatusInfo; |
4261 | ||
a7335518 ZJS |
4262 | static void unit_status_info_free(UnitStatusInfo *info) { |
4263 | ExecStatusInfo *p; | |
4264 | UnitCondition *c; | |
4265 | ||
4266 | strv_free(info->documentation); | |
4267 | strv_free(info->dropin_paths); | |
4268 | strv_free(info->listen); | |
4269 | ||
4270 | while ((c = info->conditions)) { | |
4271 | LIST_REMOVE(conditions, info->conditions, c); | |
4272 | unit_condition_free(c); | |
4273 | } | |
4274 | ||
4275 | while ((p = info->exec)) { | |
4276 | LIST_REMOVE(exec, info->exec, p); | |
4277 | exec_status_info_free(p); | |
4278 | } | |
4279 | } | |
4280 | ||
f459b602 | 4281 | static void print_status_info( |
291d565a | 4282 | sd_bus *bus, |
f459b602 MAP |
4283 | UnitStatusInfo *i, |
4284 | bool *ellipsized) { | |
4285 | ||
4d9685be ZJS |
4286 | char since1[FORMAT_TIMESTAMP_RELATIVE_MAX], since2[FORMAT_TIMESTAMP_MAX]; |
4287 | const char *s1, *s2, *active_on, *active_off, *on, *off, *ss; | |
c7080257 LP |
4288 | _cleanup_free_ char *formatted_path = NULL; |
4289 | ExecStatusInfo *p; | |
584be568 | 4290 | usec_t timestamp; |
1b64d026 | 4291 | const char *path; |
13160134 | 4292 | char **t, **t2; |
291d565a | 4293 | int r; |
582a507f | 4294 | |
61cbdc4b LP |
4295 | assert(i); |
4296 | ||
4297 | /* This shows pretty information about a unit. See | |
4298 | * print_property() for a low-level property printer */ | |
4299 | ||
b0d14c69 LP |
4300 | if (streq_ptr(i->active_state, "failed")) { |
4301 | active_on = ansi_highlight_red(); | |
1fc464f6 | 4302 | active_off = ansi_normal(); |
1cf03a4f | 4303 | } else if (STRPTR_IN_SET(i->active_state, "active", "reloading")) { |
b0d14c69 | 4304 | active_on = ansi_highlight_green(); |
1fc464f6 | 4305 | active_off = ansi_normal(); |
b0d14c69 LP |
4306 | } else |
4307 | active_on = active_off = ""; | |
4308 | ||
9a6f746f | 4309 | printf("%s%s%s %s", active_on, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE), active_off, strna(i->id)); |
61cbdc4b LP |
4310 | |
4311 | if (i->description && !streq_ptr(i->id, i->description)) | |
4312 | printf(" - %s", i->description); | |
4313 | ||
4314 | printf("\n"); | |
4315 | ||
4a9e2fff | 4316 | if (i->following) |
856323c9 | 4317 | printf(" Follow: unit currently follows state of %s\n", i->following); |
4a9e2fff | 4318 | |
c4555ad8 | 4319 | if (STRPTR_IN_SET(i->load_state, "error", "not-found", "bad-setting")) { |
0b5a519c | 4320 | on = ansi_highlight_red(); |
1fc464f6 | 4321 | off = ansi_normal(); |
c31b4423 LP |
4322 | } else |
4323 | on = off = ""; | |
4324 | ||
c7080257 LP |
4325 | path = i->source_path ?: i->fragment_path; |
4326 | if (path && terminal_urlify_path(path, NULL, &formatted_path) >= 0) | |
4327 | path = formatted_path; | |
1b64d026 | 4328 | |
9a0abfa8 | 4329 | if (!isempty(i->load_error)) |
856323c9 ZJS |
4330 | printf(" Loaded: %s%s%s (Reason: %s)\n", |
4331 | on, strna(i->load_state), off, i->load_error); | |
15ce1647 LP |
4332 | else if (path && !isempty(i->unit_file_state) && !isempty(i->unit_file_preset) && |
4333 | !STR_IN_SET(i->unit_file_state, "generated", "transient")) | |
d2dc52db LP |
4334 | printf(" Loaded: %s%s%s (%s; %s; vendor preset: %s)\n", |
4335 | on, strna(i->load_state), off, path, i->unit_file_state, i->unit_file_preset); | |
4336 | else if (path && !isempty(i->unit_file_state)) | |
856323c9 ZJS |
4337 | printf(" Loaded: %s%s%s (%s; %s)\n", |
4338 | on, strna(i->load_state), off, path, i->unit_file_state); | |
1b64d026 | 4339 | else if (path) |
856323c9 ZJS |
4340 | printf(" Loaded: %s%s%s (%s)\n", |
4341 | on, strna(i->load_state), off, path); | |
61cbdc4b | 4342 | else |
856323c9 ZJS |
4343 | printf(" Loaded: %s%s%s\n", |
4344 | on, strna(i->load_state), off); | |
61cbdc4b | 4345 | |
055ef36b LP |
4346 | if (i->transient) |
4347 | printf("Transient: yes\n"); | |
4348 | ||
76d14b87 | 4349 | if (!strv_isempty(i->dropin_paths)) { |
f459b602 | 4350 | _cleanup_free_ char *dir = NULL; |
76d14b87 | 4351 | bool last = false; |
f459b602 | 4352 | char ** dropin; |
76d14b87 OS |
4353 | |
4354 | STRV_FOREACH(dropin, i->dropin_paths) { | |
c7080257 LP |
4355 | _cleanup_free_ char *dropin_formatted = NULL; |
4356 | const char *df; | |
4357 | ||
eb2c3192 LP |
4358 | if (!dir || last) { |
4359 | printf(dir ? " " : | |
4360 | " Drop-In: "); | |
76d14b87 | 4361 | |
97b11eed | 4362 | dir = mfree(dir); |
76d14b87 | 4363 | |
5f311f8c LP |
4364 | dir = dirname_malloc(*dropin); |
4365 | if (!dir) { | |
76d14b87 OS |
4366 | log_oom(); |
4367 | return; | |
4368 | } | |
4369 | ||
eb2c3192 LP |
4370 | printf("%s\n" |
4371 | " %s", dir, | |
9a6f746f | 4372 | special_glyph(SPECIAL_GLYPH_TREE_RIGHT)); |
76d14b87 OS |
4373 | } |
4374 | ||
4375 | last = ! (*(dropin + 1) && startswith(*(dropin + 1), dir)); | |
4376 | ||
c7080257 LP |
4377 | if (terminal_urlify_path(*dropin, basename(*dropin), &dropin_formatted) >= 0) |
4378 | df = dropin_formatted; | |
4379 | else | |
4380 | df = *dropin; | |
4381 | ||
4382 | printf("%s%s", df, last ? "\n" : ", "); | |
76d14b87 | 4383 | } |
76d14b87 OS |
4384 | } |
4385 | ||
2ee68f72 | 4386 | ss = streq_ptr(i->active_state, i->sub_state) ? NULL : i->sub_state; |
2ee68f72 | 4387 | if (ss) |
856323c9 | 4388 | printf(" Active: %s%s (%s)%s", |
b0d14c69 | 4389 | active_on, strna(i->active_state), ss, active_off); |
2ee68f72 | 4390 | else |
856323c9 | 4391 | printf(" Active: %s%s%s", |
b0d14c69 | 4392 | active_on, strna(i->active_state), active_off); |
61cbdc4b | 4393 | |
f42806df LP |
4394 | if (!isempty(i->result) && !streq(i->result, "success")) |
4395 | printf(" (Result: %s)", i->result); | |
4396 | ||
1cf03a4f ZJS |
4397 | timestamp = STRPTR_IN_SET(i->active_state, "active", "reloading") ? i->active_enter_timestamp : |
4398 | STRPTR_IN_SET(i->active_state, "inactive", "failed") ? i->inactive_enter_timestamp : | |
4399 | STRPTR_IN_SET(i->active_state, "activating") ? i->inactive_exit_timestamp : | |
4400 | i->active_exit_timestamp; | |
584be568 | 4401 | |
bbb8486e | 4402 | s1 = format_timestamp_relative(since1, sizeof(since1), timestamp); |
584be568 LP |
4403 | s2 = format_timestamp(since2, sizeof(since2), timestamp); |
4404 | ||
4405 | if (s1) | |
538da63d | 4406 | printf(" since %s; %s\n", s2, s1); |
584be568 | 4407 | else if (s2) |
538da63d | 4408 | printf(" since %s\n", s2); |
584be568 LP |
4409 | else |
4410 | printf("\n"); | |
4411 | ||
601b0842 GS |
4412 | if (endswith(i->id, ".timer")) { |
4413 | char tstamp1[FORMAT_TIMESTAMP_RELATIVE_MAX], | |
4414 | tstamp2[FORMAT_TIMESTAMP_MAX]; | |
4d9685be | 4415 | const char *next_rel_time, *next_time; |
601b0842 GS |
4416 | dual_timestamp nw, next = {i->next_elapse_real, |
4417 | i->next_elapse_monotonic}; | |
4418 | usec_t next_elapse; | |
4419 | ||
4420 | printf(" Trigger: "); | |
4421 | ||
4422 | dual_timestamp_get(&nw); | |
4423 | next_elapse = calc_next_elapse(&nw, &next); | |
4d9685be ZJS |
4424 | next_rel_time = format_timestamp_relative(tstamp1, sizeof tstamp1, next_elapse); |
4425 | next_time = format_timestamp(tstamp2, sizeof tstamp2, next_elapse); | |
601b0842 GS |
4426 | |
4427 | if (next_time && next_rel_time) | |
4428 | printf("%s; %s\n", next_time, next_rel_time); | |
4429 | else | |
4430 | printf("n/a\n"); | |
4431 | } | |
4432 | ||
90bbc946 | 4433 | if (!i->condition_result && i->condition_timestamp > 0) { |
d5db7fe6 WC |
4434 | UnitCondition *c; |
4435 | int n = 0; | |
4436 | ||
bbb8486e | 4437 | s1 = format_timestamp_relative(since1, sizeof(since1), i->condition_timestamp); |
90bbc946 LP |
4438 | s2 = format_timestamp(since2, sizeof(since2), i->condition_timestamp); |
4439 | ||
59fccdc5 | 4440 | printf("Condition: start %scondition failed%s at %s%s%s\n", |
1fc464f6 | 4441 | ansi_highlight_yellow(), ansi_normal(), |
5cfee414 | 4442 | s2, s1 ? "; " : "", strempty(s1)); |
d5db7fe6 | 4443 | |
b1ed76ae | 4444 | LIST_FOREACH(conditions, c, i->conditions) |
d5db7fe6 WC |
4445 | if (c->tristate < 0) |
4446 | n++; | |
d5db7fe6 | 4447 | |
b1ed76ae ZJS |
4448 | LIST_FOREACH(conditions, c, i->conditions) |
4449 | if (c->tristate < 0) | |
4450 | printf(" %s %s=%s%s%s was not met\n", | |
9a6f746f | 4451 | --n ? special_glyph(SPECIAL_GLYPH_TREE_BRANCH) : special_glyph(SPECIAL_GLYPH_TREE_RIGHT), |
b1ed76ae ZJS |
4452 | c->name, |
4453 | c->trigger ? "|" : "", | |
4454 | c->negate ? "!" : "", | |
4455 | c->param); | |
59fccdc5 LP |
4456 | } |
4457 | ||
4458 | if (!i->assert_result && i->assert_timestamp > 0) { | |
4459 | s1 = format_timestamp_relative(since1, sizeof(since1), i->assert_timestamp); | |
4460 | s2 = format_timestamp(since2, sizeof(since2), i->assert_timestamp); | |
4461 | ||
4462 | printf(" Assert: start %sassertion failed%s at %s%s%s\n", | |
1fc464f6 | 4463 | ansi_highlight_red(), ansi_normal(), |
5cfee414 | 4464 | s2, s1 ? "; " : "", strempty(s1)); |
59fccdc5 LP |
4465 | if (i->failed_assert_trigger) |
4466 | printf(" none of the trigger assertions were met\n"); | |
4467 | else if (i->failed_assert) | |
4468 | printf(" %s=%s%s was not met\n", | |
4469 | i->failed_assert, | |
4470 | i->failed_assert_negate ? "!" : "", | |
4471 | i->failed_assert_parameter); | |
90bbc946 LP |
4472 | } |
4473 | ||
61cbdc4b | 4474 | if (i->sysfs_path) |
856323c9 | 4475 | printf(" Device: %s\n", i->sysfs_path); |
9feeba4b | 4476 | if (i->where) |
856323c9 | 4477 | printf(" Where: %s\n", i->where); |
9feeba4b | 4478 | if (i->what) |
856323c9 | 4479 | printf(" What: %s\n", i->what); |
49dbfa7b | 4480 | |
78c7d20e LP |
4481 | STRV_FOREACH(t, i->documentation) { |
4482 | _cleanup_free_ char *formatted = NULL; | |
4483 | const char *q; | |
4484 | ||
4485 | if (terminal_urlify(*t, NULL, &formatted) >= 0) | |
4486 | q = formatted; | |
4487 | else | |
4488 | q = *t; | |
4489 | ||
4490 | printf(" %*s %s\n", 9, t == i->documentation ? "Docs:" : "", q); | |
4491 | } | |
49dbfa7b | 4492 | |
13160134 | 4493 | STRV_FOREACH_PAIR(t, t2, i->listen) |
856323c9 | 4494 | printf(" %*s %s (%s)\n", 9, t == i->listen ? "Listen:" : "", *t2, *t); |
67419600 | 4495 | |
a98f7575 MM |
4496 | if (i->accept) { |
4497 | printf(" Accepted: %u; Connected: %u;", i->n_accepted, i->n_connections); | |
4498 | if (i->n_refused) | |
4499 | printf(" Refused: %u", i->n_refused); | |
4500 | printf("\n"); | |
4501 | } | |
4502 | ||
582a507f | 4503 | LIST_FOREACH(exec, p, i->exec) { |
13160134 | 4504 | _cleanup_free_ char *argv = NULL; |
9a57c629 | 4505 | bool good; |
582a507f LP |
4506 | |
4507 | /* Only show exited processes here */ | |
4508 | if (p->code == 0) | |
4509 | continue; | |
4510 | ||
13160134 | 4511 | argv = strv_join(p->argv, " "); |
1fa2f38f | 4512 | printf(" Process: "PID_FMT" %s=%s ", p->pid, p->name, strna(argv)); |
582a507f | 4513 | |
1f0958f6 | 4514 | good = is_clean_exit(p->code, p->status, EXIT_CLEAN_DAEMON, NULL); |
9a57c629 | 4515 | if (!good) { |
0b5a519c | 4516 | on = ansi_highlight_red(); |
1fc464f6 | 4517 | off = ansi_normal(); |
9a57c629 LP |
4518 | } else |
4519 | on = off = ""; | |
4520 | ||
4521 | printf("%s(code=%s, ", on, sigchld_code_to_string(p->code)); | |
4522 | ||
d06dacd0 LP |
4523 | if (p->code == CLD_EXITED) { |
4524 | const char *c; | |
4525 | ||
582a507f | 4526 | printf("status=%i", p->status); |
d06dacd0 | 4527 | |
1b64d026 LP |
4528 | c = exit_status_to_string(p->status, EXIT_STATUS_SYSTEMD); |
4529 | if (c) | |
d06dacd0 LP |
4530 | printf("/%s", c); |
4531 | ||
4532 | } else | |
582a507f | 4533 | printf("signal=%s", signal_to_string(p->status)); |
9a57c629 LP |
4534 | |
4535 | printf(")%s\n", off); | |
4536 | ||
582a507f LP |
4537 | if (i->main_pid == p->pid && |
4538 | i->start_timestamp == p->start_timestamp && | |
4539 | i->exit_timestamp == p->start_timestamp) | |
4540 | /* Let's not show this twice */ | |
4541 | i->main_pid = 0; | |
4542 | ||
4543 | if (p->pid == i->control_pid) | |
4544 | i->control_pid = 0; | |
4545 | } | |
4546 | ||
61cbdc4b | 4547 | if (i->main_pid > 0 || i->control_pid > 0) { |
61cbdc4b | 4548 | if (i->main_pid > 0) { |
8c06592f | 4549 | printf(" Main PID: "PID_FMT, i->main_pid); |
61cbdc4b LP |
4550 | |
4551 | if (i->running) { | |
a081b9ce LP |
4552 | |
4553 | if (arg_transport == BUS_TRANSPORT_LOCAL) { | |
4554 | _cleanup_free_ char *comm = NULL; | |
4555 | ||
4556 | (void) get_process_comm(i->main_pid, &comm); | |
4557 | if (comm) | |
4558 | printf(" (%s)", comm); | |
4559 | } | |
4560 | ||
6d4fc029 | 4561 | } else if (i->exit_code > 0) { |
61cbdc4b LP |
4562 | printf(" (code=%s, ", sigchld_code_to_string(i->exit_code)); |
4563 | ||
d06dacd0 LP |
4564 | if (i->exit_code == CLD_EXITED) { |
4565 | const char *c; | |
4566 | ||
61cbdc4b | 4567 | printf("status=%i", i->exit_status); |
d06dacd0 | 4568 | |
1b64d026 LP |
4569 | c = exit_status_to_string(i->exit_status, EXIT_STATUS_SYSTEMD); |
4570 | if (c) | |
d06dacd0 LP |
4571 | printf("/%s", c); |
4572 | ||
4573 | } else | |
582a507f | 4574 | printf("signal=%s", signal_to_string(i->exit_status)); |
6d4fc029 LP |
4575 | printf(")"); |
4576 | } | |
13160134 | 4577 | } |
61cbdc4b LP |
4578 | |
4579 | if (i->control_pid > 0) { | |
13160134 | 4580 | _cleanup_free_ char *c = NULL; |
61cbdc4b | 4581 | |
1089dcd4 LP |
4582 | if (i->main_pid > 0) |
4583 | fputs("; Control PID: ", stdout); | |
4584 | else | |
4585 | fputs("Cntrl PID: ", stdout); /* if first in column, abbreviated so it fits alignment */ | |
4586 | ||
4587 | printf(PID_FMT, i->control_pid); | |
61cbdc4b | 4588 | |
a081b9ce LP |
4589 | if (arg_transport == BUS_TRANSPORT_LOCAL) { |
4590 | (void) get_process_comm(i->control_pid, &c); | |
4591 | if (c) | |
4592 | printf(" (%s)", c); | |
4593 | } | |
61cbdc4b LP |
4594 | } |
4595 | ||
4596 | printf("\n"); | |
4597 | } | |
4598 | ||
17bb7382 | 4599 | if (i->status_text) |
856323c9 | 4600 | printf(" Status: \"%s\"\n", i->status_text); |
b4af5a80 | 4601 | if (i->status_errno > 0) |
4bbccb02 | 4602 | printf(" Error: %i (%s)\n", i->status_errno, strerror_safe(i->status_errno)); |
17bb7382 | 4603 | |
0e97c93f DM |
4604 | if (i->ip_ingress_bytes != (uint64_t) -1 && i->ip_egress_bytes != (uint64_t) -1) { |
4605 | char buf_in[FORMAT_BYTES_MAX], buf_out[FORMAT_BYTES_MAX]; | |
4606 | ||
4607 | printf(" IP: %s in, %s out\n", | |
4608 | format_bytes(buf_in, sizeof(buf_in), i->ip_ingress_bytes), | |
4609 | format_bytes(buf_out, sizeof(buf_out), i->ip_egress_bytes)); | |
4610 | } | |
4611 | ||
7a5de3f9 LP |
4612 | if (i->io_read_bytes != UINT64_MAX && i->io_write_bytes != UINT64_MAX) { |
4613 | char buf_in[FORMAT_BYTES_MAX], buf_out[FORMAT_BYTES_MAX]; | |
4614 | ||
4615 | printf(" IO: %s read, %s written\n", | |
4616 | format_bytes(buf_in, sizeof(buf_in), i->io_read_bytes), | |
4617 | format_bytes(buf_out, sizeof(buf_out), i->io_write_bytes)); | |
4618 | } | |
4619 | ||
03a7b521 LP |
4620 | if (i->tasks_current != (uint64_t) -1) { |
4621 | printf(" Tasks: %" PRIu64, i->tasks_current); | |
4622 | ||
4623 | if (i->tasks_max != (uint64_t) -1) | |
2a6736dd | 4624 | printf(" (limit: %" PRIu64 ")\n", i->tasks_max); |
03a7b521 LP |
4625 | else |
4626 | printf("\n"); | |
4627 | } | |
4628 | ||
934277fe LP |
4629 | if (i->memory_current != (uint64_t) -1) { |
4630 | char buf[FORMAT_BYTES_MAX]; | |
4631 | ||
4632 | printf(" Memory: %s", format_bytes(buf, sizeof(buf), i->memory_current)); | |
4633 | ||
48422635 TH |
4634 | if (i->memory_min > 0 || i->memory_low > 0 || |
4635 | i->memory_high != CGROUP_LIMIT_MAX || i->memory_max != CGROUP_LIMIT_MAX || | |
4636 | i->memory_swap_max != CGROUP_LIMIT_MAX || | |
da4d897e TH |
4637 | i->memory_limit != CGROUP_LIMIT_MAX) { |
4638 | const char *prefix = ""; | |
4639 | ||
4640 | printf(" ("); | |
48422635 TH |
4641 | if (i->memory_min > 0) { |
4642 | printf("%smin: %s", prefix, format_bytes(buf, sizeof(buf), i->memory_min)); | |
4643 | prefix = " "; | |
4644 | } | |
da4d897e TH |
4645 | if (i->memory_low > 0) { |
4646 | printf("%slow: %s", prefix, format_bytes(buf, sizeof(buf), i->memory_low)); | |
4647 | prefix = " "; | |
4648 | } | |
4649 | if (i->memory_high != CGROUP_LIMIT_MAX) { | |
4650 | printf("%shigh: %s", prefix, format_bytes(buf, sizeof(buf), i->memory_high)); | |
4651 | prefix = " "; | |
4652 | } | |
4653 | if (i->memory_max != CGROUP_LIMIT_MAX) { | |
4654 | printf("%smax: %s", prefix, format_bytes(buf, sizeof(buf), i->memory_max)); | |
4655 | prefix = " "; | |
4656 | } | |
96e131ea WC |
4657 | if (i->memory_swap_max != CGROUP_LIMIT_MAX) { |
4658 | printf("%sswap max: %s", prefix, format_bytes(buf, sizeof(buf), i->memory_swap_max)); | |
4659 | prefix = " "; | |
4660 | } | |
da4d897e TH |
4661 | if (i->memory_limit != CGROUP_LIMIT_MAX) { |
4662 | printf("%slimit: %s", prefix, format_bytes(buf, sizeof(buf), i->memory_limit)); | |
4663 | prefix = " "; | |
4664 | } | |
4665 | printf(")"); | |
4666 | } | |
4667 | printf("\n"); | |
934277fe LP |
4668 | } |
4669 | ||
5ad096b3 LP |
4670 | if (i->cpu_usage_nsec != (uint64_t) -1) { |
4671 | char buf[FORMAT_TIMESPAN_MAX]; | |
4672 | printf(" CPU: %s\n", format_timespan(buf, sizeof(buf), i->cpu_usage_nsec / NSEC_PER_USEC, USEC_PER_MSEC)); | |
4673 | } | |
4674 | ||
51e22f88 | 4675 | if (i->control_group) { |
291d565a LP |
4676 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
4677 | static const char prefix[] = " "; | |
ab35fb1b LP |
4678 | unsigned c; |
4679 | ||
51e22f88 LP |
4680 | printf(" CGroup: %s\n", i->control_group); |
4681 | ||
291d565a LP |
4682 | c = columns(); |
4683 | if (c > sizeof(prefix) - 1) | |
4684 | c -= sizeof(prefix) - 1; | |
4685 | else | |
4686 | c = 0; | |
ab35fb1b | 4687 | |
291d565a LP |
4688 | r = unit_show_processes(bus, i->id, i->control_group, prefix, c, get_output_flags(), &error); |
4689 | if (r == -EBADR) { | |
b69d29ce LP |
4690 | unsigned k = 0; |
4691 | pid_t extra[2]; | |
4692 | ||
291d565a | 4693 | /* Fallback for older systemd versions where the GetUnitProcesses() call is not yet available */ |
ab35fb1b | 4694 | |
b69d29ce LP |
4695 | if (i->main_pid > 0) |
4696 | extra[k++] = i->main_pid; | |
4697 | ||
4698 | if (i->control_pid > 0) | |
4699 | extra[k++] = i->control_pid; | |
4700 | ||
0ff308c8 | 4701 | show_cgroup_and_extra(SYSTEMD_CGROUP_CONTROLLER, i->control_group, prefix, c, extra, k, get_output_flags()); |
291d565a | 4702 | } else if (r < 0) |
950d6889 | 4703 | log_warning_errno(r, "Failed to dump process list for '%s', ignoring: %s", i->id, bus_error_message(&error, r)); |
c59760ee | 4704 | } |
45fb0699 | 4705 | |
ece174c5 | 4706 | if (i->id && arg_transport == BUS_TRANSPORT_LOCAL) |
3c756001 LP |
4707 | show_journal_by_unit( |
4708 | stdout, | |
4709 | i->id, | |
4710 | arg_output, | |
4711 | 0, | |
4712 | i->inactive_exit_timestamp_monotonic, | |
4713 | arg_lines, | |
4714 | getuid(), | |
4715 | get_output_flags() | OUTPUT_BEGIN_NEWLINE, | |
4716 | SD_JOURNAL_LOCAL_ONLY, | |
4717 | arg_scope == UNIT_FILE_SYSTEM, | |
4718 | ellipsized); | |
86aa7ba4 | 4719 | |
45fb0699 | 4720 | if (i->need_daemon_reload) |
3f36991e | 4721 | warn_unit_file_changed(i->id); |
61cbdc4b LP |
4722 | } |
4723 | ||
b43f208f | 4724 | static void show_unit_help(UnitStatusInfo *i) { |
256425cc LP |
4725 | char **p; |
4726 | ||
4727 | assert(i); | |
4728 | ||
4729 | if (!i->documentation) { | |
4730 | log_info("Documentation for %s not known.", i->id); | |
4731 | return; | |
4732 | } | |
4733 | ||
78002a67 ZJS |
4734 | STRV_FOREACH(p, i->documentation) |
4735 | if (startswith(*p, "man:")) | |
4736 | show_man_page(*p + 4, false); | |
4737 | else | |
0315fe37 | 4738 | log_info("Can't show: %s", *p); |
256425cc LP |
4739 | } |
4740 | ||
4679a8c3 YW |
4741 | static int map_main_pid(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { |
4742 | UnitStatusInfo *i = userdata; | |
4743 | uint32_t u; | |
f459b602 | 4744 | int r; |
61cbdc4b | 4745 | |
4679a8c3 YW |
4746 | r = sd_bus_message_read(m, "u", &u); |
4747 | if (r < 0) | |
4748 | return r; | |
61cbdc4b | 4749 | |
4679a8c3 YW |
4750 | i->main_pid = (pid_t) u; |
4751 | i->running = u > 0; | |
b8131a87 | 4752 | |
4679a8c3 YW |
4753 | return 0; |
4754 | } | |
b8131a87 | 4755 | |
4679a8c3 | 4756 | static int map_load_error(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { |
31d99bd1 | 4757 | const char *message, **p = userdata; |
4679a8c3 | 4758 | int r; |
b8131a87 | 4759 | |
31d99bd1 | 4760 | r = sd_bus_message_read(m, "(ss)", NULL, &message); |
4679a8c3 YW |
4761 | if (r < 0) |
4762 | return r; | |
b8131a87 | 4763 | |
4679a8c3 YW |
4764 | if (!isempty(message)) |
4765 | *p = message; | |
61cbdc4b | 4766 | |
4679a8c3 YW |
4767 | return 0; |
4768 | } | |
61cbdc4b | 4769 | |
4679a8c3 YW |
4770 | static int map_listen(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { |
4771 | const char *type, *path; | |
4772 | char ***p = userdata; | |
4773 | int r; | |
61cbdc4b | 4774 | |
4679a8c3 YW |
4775 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(ss)"); |
4776 | if (r < 0) | |
4777 | return r; | |
61cbdc4b | 4778 | |
4679a8c3 | 4779 | while ((r = sd_bus_message_read(m, "(ss)", &type, &path)) > 0) { |
61cbdc4b | 4780 | |
4679a8c3 | 4781 | r = strv_extend(p, type); |
f459b602 | 4782 | if (r < 0) |
4679a8c3 | 4783 | return r; |
61cbdc4b | 4784 | |
4679a8c3 | 4785 | r = strv_extend(p, path); |
f459b602 | 4786 | if (r < 0) |
4679a8c3 | 4787 | return r; |
61cbdc4b | 4788 | } |
4679a8c3 YW |
4789 | if (r < 0) |
4790 | return r; | |
582a507f | 4791 | |
4679a8c3 YW |
4792 | r = sd_bus_message_exit_container(m); |
4793 | if (r < 0) | |
4794 | return r; | |
49dbfa7b | 4795 | |
4679a8c3 YW |
4796 | return 0; |
4797 | } | |
52990c2e | 4798 | |
4679a8c3 YW |
4799 | static int map_conditions(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { |
4800 | UnitStatusInfo *i = userdata; | |
4801 | const char *cond, *param; | |
4802 | int trigger, negate; | |
4803 | int32_t state; | |
4804 | int r; | |
52990c2e | 4805 | |
4679a8c3 YW |
4806 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(sbbsi)"); |
4807 | if (r < 0) | |
4808 | return r; | |
f459b602 | 4809 | |
4679a8c3 YW |
4810 | while ((r = sd_bus_message_read(m, "(sbbsi)", &cond, &trigger, &negate, ¶m, &state)) > 0) { |
4811 | _cleanup_(unit_condition_freep) UnitCondition *c = NULL; | |
d5db7fe6 | 4812 | |
3bfa8668 | 4813 | c = new(UnitCondition, 1); |
4679a8c3 YW |
4814 | if (!c) |
4815 | return -ENOMEM; | |
d5db7fe6 | 4816 | |
3bfa8668 LP |
4817 | *c = (UnitCondition) { |
4818 | .name = strdup(cond), | |
4819 | .param = strdup(param), | |
4820 | .trigger = trigger, | |
4821 | .negate = negate, | |
4822 | .tristate = state, | |
4823 | }; | |
4824 | ||
4679a8c3 YW |
4825 | if (!c->name || !c->param) |
4826 | return -ENOMEM; | |
d5db7fe6 | 4827 | |
d5db7fe6 | 4828 | |
3bfa8668 | 4829 | LIST_PREPEND(conditions, i->conditions, TAKE_PTR(c)); |
4679a8c3 YW |
4830 | } |
4831 | if (r < 0) | |
4832 | return r; | |
d5db7fe6 | 4833 | |
4679a8c3 YW |
4834 | r = sd_bus_message_exit_container(m); |
4835 | if (r < 0) | |
4836 | return r; | |
59fccdc5 | 4837 | |
4679a8c3 YW |
4838 | return 0; |
4839 | } | |
59fccdc5 | 4840 | |
4679a8c3 YW |
4841 | static int map_asserts(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { |
4842 | UnitStatusInfo *i = userdata; | |
4843 | const char *cond, *param; | |
4844 | int trigger, negate; | |
4845 | int32_t state; | |
4846 | int r; | |
59fccdc5 | 4847 | |
4679a8c3 YW |
4848 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(sbbsi)"); |
4849 | if (r < 0) | |
4850 | return r; | |
59fccdc5 | 4851 | |
4679a8c3 YW |
4852 | while ((r = sd_bus_message_read(m, "(sbbsi)", &cond, &trigger, &negate, ¶m, &state)) > 0) { |
4853 | if (state < 0 && (!trigger || !i->failed_assert)) { | |
4854 | i->failed_assert = cond; | |
4855 | i->failed_assert_trigger = trigger; | |
4856 | i->failed_assert_negate = negate; | |
4857 | i->failed_assert_parameter = param; | |
4858 | } | |
4859 | } | |
4860 | if (r < 0) | |
4861 | return r; | |
f459b602 | 4862 | |
4679a8c3 YW |
4863 | r = sd_bus_message_exit_container(m); |
4864 | if (r < 0) | |
4865 | return r; | |
f459b602 | 4866 | |
4679a8c3 YW |
4867 | return 0; |
4868 | } | |
582a507f | 4869 | |
4679a8c3 YW |
4870 | static int map_exec(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { |
4871 | _cleanup_free_ ExecStatusInfo *info = NULL; | |
3e14d36a | 4872 | ExecStatusInfo *last; |
4679a8c3 | 4873 | UnitStatusInfo *i = userdata; |
b3d59367 | 4874 | bool is_ex_prop = endswith(member, "Ex"); |
4679a8c3 | 4875 | int r; |
9f39404c | 4876 | |
b3d59367 | 4877 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, is_ex_prop ? "(sasasttttuii)" : "(sasbttttuii)"); |
4679a8c3 YW |
4878 | if (r < 0) |
4879 | return r; | |
9f39404c | 4880 | |
4679a8c3 YW |
4881 | info = new0(ExecStatusInfo, 1); |
4882 | if (!info) | |
4883 | return -ENOMEM; | |
9f39404c | 4884 | |
3e14d36a LP |
4885 | LIST_FIND_TAIL(exec, i->exec, last); |
4886 | ||
b3d59367 | 4887 | while ((r = exec_status_info_deserialize(m, info, is_ex_prop)) > 0) { |
9f39404c | 4888 | |
4679a8c3 YW |
4889 | info->name = strdup(member); |
4890 | if (!info->name) | |
4891 | return -ENOMEM; | |
9f39404c | 4892 | |
3e14d36a LP |
4893 | LIST_INSERT_AFTER(exec, i->exec, last, info); |
4894 | last = info; | |
f459b602 | 4895 | |
4679a8c3 YW |
4896 | info = new0(ExecStatusInfo, 1); |
4897 | if (!info) | |
4898 | return -ENOMEM; | |
9f39404c | 4899 | } |
4679a8c3 YW |
4900 | if (r < 0) |
4901 | return r; | |
f459b602 | 4902 | |
4679a8c3 | 4903 | r = sd_bus_message_exit_container(m); |
f459b602 | 4904 | if (r < 0) |
4679a8c3 | 4905 | return r; |
61cbdc4b LP |
4906 | |
4907 | return 0; | |
4908 | } | |
4909 | ||
eda19357 | 4910 | static int print_property(const char *name, const char *expected_value, sd_bus_message *m, bool value, bool all) { |
07636114 YW |
4911 | char bus_type; |
4912 | const char *contents; | |
f459b602 MAP |
4913 | int r; |
4914 | ||
48220598 | 4915 | assert(name); |
f459b602 | 4916 | assert(m); |
48220598 | 4917 | |
61cbdc4b LP |
4918 | /* This is a low-level property printer, see |
4919 | * print_status_info() for the nicer output */ | |
4920 | ||
07636114 YW |
4921 | r = sd_bus_message_peek_type(m, &bus_type, &contents); |
4922 | if (r < 0) | |
852c1b4d | 4923 | return r; |
48220598 | 4924 | |
07636114 | 4925 | switch (bus_type) { |
48220598 | 4926 | |
a3ddd457 YW |
4927 | case SD_BUS_TYPE_INT32: |
4928 | if (endswith(name, "ActionExitStatus")) { | |
4929 | int32_t i; | |
4930 | ||
4931 | r = sd_bus_message_read_basic(m, bus_type, &i); | |
4932 | if (r < 0) | |
4933 | return r; | |
4934 | ||
4935 | if (i >= 0 && i <= 255) | |
4936 | bus_print_property_valuef(name, expected_value, value, "%"PRIi32, i); | |
4937 | else if (all) | |
4938 | bus_print_property_value(name, expected_value, value, "[not set]"); | |
4939 | ||
b070c7c0 MS |
4940 | return 1; |
4941 | } else if (streq(name, "NUMAPolicy")) { | |
4942 | int32_t i; | |
4943 | ||
4944 | r = sd_bus_message_read_basic(m, bus_type, &i); | |
4945 | if (r < 0) | |
4946 | return r; | |
4947 | ||
4948 | bus_print_property_valuef(name, expected_value, value, "%s", strna(mpol_to_string(i))); | |
4949 | ||
a3ddd457 YW |
4950 | return 1; |
4951 | } | |
4952 | break; | |
4953 | ||
07636114 | 4954 | case SD_BUS_TYPE_STRUCT: |
48220598 | 4955 | |
07636114 | 4956 | if (contents[0] == SD_BUS_TYPE_UINT32 && streq(name, "Job")) { |
48220598 LP |
4957 | uint32_t u; |
4958 | ||
f459b602 MAP |
4959 | r = sd_bus_message_read(m, "(uo)", &u, NULL); |
4960 | if (r < 0) | |
4961 | return bus_log_parse_error(r); | |
48220598 | 4962 | |
f459b602 | 4963 | if (u > 0) |
102b0214 | 4964 | bus_print_property_valuef(name, expected_value, value, "%"PRIu32, u); |
07636114 | 4965 | else if (all) |
102b0214 | 4966 | bus_print_property_value(name, expected_value, value, ""); |
48220598 | 4967 | |
07636114 | 4968 | return 1; |
f459b602 | 4969 | |
07636114 | 4970 | } else if (contents[0] == SD_BUS_TYPE_STRING && streq(name, "Unit")) { |
48220598 LP |
4971 | const char *s; |
4972 | ||
f459b602 MAP |
4973 | r = sd_bus_message_read(m, "(so)", &s, NULL); |
4974 | if (r < 0) | |
4975 | return bus_log_parse_error(r); | |
48220598 | 4976 | |
07636114 | 4977 | if (all || !isempty(s)) |
102b0214 | 4978 | bus_print_property_value(name, expected_value, value, s); |
48220598 | 4979 | |
07636114 | 4980 | return 1; |
f459b602 | 4981 | |
07636114 | 4982 | } else if (contents[0] == SD_BUS_TYPE_STRING && streq(name, "LoadError")) { |
9f39404c LP |
4983 | const char *a = NULL, *b = NULL; |
4984 | ||
f459b602 MAP |
4985 | r = sd_bus_message_read(m, "(ss)", &a, &b); |
4986 | if (r < 0) | |
4987 | return bus_log_parse_error(r); | |
9f39404c | 4988 | |
48d3358d | 4989 | if (!isempty(a) || !isempty(b)) |
102b0214 | 4990 | bus_print_property_valuef(name, expected_value, value, "%s \"%s\"", strempty(a), strempty(b)); |
48d3358d YW |
4991 | else if (all) |
4992 | bus_print_property_value(name, expected_value, value, ""); | |
f786e80d | 4993 | |
07636114 | 4994 | return 1; |
04749e40 YW |
4995 | |
4996 | } else if (STR_IN_SET(name, "SystemCallFilter", "RestrictAddressFamilies")) { | |
57183d11 LP |
4997 | _cleanup_strv_free_ char **l = NULL; |
4998 | int whitelist; | |
4999 | ||
5000 | r = sd_bus_message_enter_container(m, 'r', "bas"); | |
5001 | if (r < 0) | |
5002 | return bus_log_parse_error(r); | |
5003 | ||
5004 | r = sd_bus_message_read(m, "b", &whitelist); | |
5005 | if (r < 0) | |
5006 | return bus_log_parse_error(r); | |
5007 | ||
5008 | r = sd_bus_message_read_strv(m, &l); | |
5009 | if (r < 0) | |
5010 | return bus_log_parse_error(r); | |
5011 | ||
5012 | r = sd_bus_message_exit_container(m); | |
5013 | if (r < 0) | |
5014 | return bus_log_parse_error(r); | |
5015 | ||
07636114 | 5016 | if (all || whitelist || !strv_isempty(l)) { |
57183d11 LP |
5017 | bool first = true; |
5018 | char **i; | |
5019 | ||
07636114 | 5020 | if (!value) { |
4f9a9105 ZJS |
5021 | fputs(name, stdout); |
5022 | fputc('=', stdout); | |
5023 | } | |
57183d11 LP |
5024 | |
5025 | if (!whitelist) | |
5026 | fputc('~', stdout); | |
5027 | ||
5028 | STRV_FOREACH(i, l) { | |
5029 | if (first) | |
5030 | first = false; | |
5031 | else | |
5032 | fputc(' ', stdout); | |
5033 | ||
5034 | fputs(*i, stdout); | |
5035 | } | |
5036 | fputc('\n', stdout); | |
5037 | } | |
5038 | ||
c7bb2fec | 5039 | return 1; |
102b0214 | 5040 | |
c7bb2fec IP |
5041 | } else if (STR_IN_SET(name, "SELinuxContext", "AppArmorProfile", "SmackProcessLabel")) { |
5042 | int ignore; | |
5043 | const char *s; | |
5044 | ||
5045 | r = sd_bus_message_read(m, "(bs)", &ignore, &s); | |
5046 | if (r < 0) | |
5047 | return bus_log_parse_error(r); | |
5048 | ||
5049 | if (!isempty(s)) | |
102b0214 | 5050 | bus_print_property_valuef(name, expected_value, value, "%s%s", ignore ? "-" : "", s); |
c7bb2fec | 5051 | else if (all) |
102b0214 | 5052 | bus_print_property_value(name, expected_value, value, ""); |
c7bb2fec | 5053 | |
07636114 | 5054 | return 1; |
4a220712 YW |
5055 | |
5056 | } else if (endswith(name, "ExitStatus") && streq(contents, "aiai")) { | |
5057 | const int32_t *status, *signal; | |
5058 | size_t sz_status, sz_signal, i; | |
5059 | ||
5060 | r = sd_bus_message_enter_container(m, 'r', "aiai"); | |
5061 | if (r < 0) | |
5062 | return bus_log_parse_error(r); | |
5063 | ||
5064 | r = sd_bus_message_read_array(m, 'i', (const void **) &status, &sz_status); | |
5065 | if (r < 0) | |
5066 | return bus_log_parse_error(r); | |
5067 | ||
5068 | r = sd_bus_message_read_array(m, 'i', (const void **) &signal, &sz_signal); | |
5069 | if (r < 0) | |
5070 | return bus_log_parse_error(r); | |
5071 | ||
5072 | r = sd_bus_message_exit_container(m); | |
5073 | if (r < 0) | |
5074 | return bus_log_parse_error(r); | |
5075 | ||
5076 | sz_status /= sizeof(int32_t); | |
5077 | sz_signal /= sizeof(int32_t); | |
5078 | ||
5079 | if (all || sz_status > 0 || sz_signal > 0) { | |
5080 | bool first = true; | |
5081 | ||
5082 | if (!value) { | |
5083 | fputs(name, stdout); | |
5084 | fputc('=', stdout); | |
5085 | } | |
5086 | ||
5087 | for (i = 0; i < sz_status; i++) { | |
5088 | if (status[i] < 0 || status[i] > 255) | |
5089 | continue; | |
5090 | ||
5091 | if (first) | |
5092 | first = false; | |
5093 | else | |
5094 | fputc(' ', stdout); | |
5095 | ||
5096 | printf("%"PRIi32, status[i]); | |
5097 | } | |
5098 | ||
5099 | for (i = 0; i < sz_signal; i++) { | |
5100 | const char *str; | |
5101 | ||
5102 | str = signal_to_string((int) signal[i]); | |
5103 | if (!str) | |
5104 | continue; | |
5105 | ||
5106 | if (first) | |
5107 | first = false; | |
5108 | else | |
5109 | fputc(' ', stdout); | |
5110 | ||
5111 | fputs(str, stdout); | |
5112 | } | |
5113 | ||
5114 | fputc('\n', stdout); | |
5115 | } | |
5116 | return 1; | |
48220598 LP |
5117 | } |
5118 | ||
5119 | break; | |
48220598 | 5120 | |
f459b602 | 5121 | case SD_BUS_TYPE_ARRAY: |
48220598 | 5122 | |
07636114 | 5123 | if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "EnvironmentFiles")) { |
f459b602 MAP |
5124 | const char *path; |
5125 | int ignore; | |
8c7be95e | 5126 | |
f459b602 MAP |
5127 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(sb)"); |
5128 | if (r < 0) | |
5129 | return bus_log_parse_error(r); | |
8c7be95e | 5130 | |
f459b602 | 5131 | while ((r = sd_bus_message_read(m, "(sb)", &path, &ignore)) > 0) |
102b0214 | 5132 | bus_print_property_valuef(name, expected_value, value, "%s (ignore_errors=%s)", path, yes_no(ignore)); |
8c7be95e | 5133 | |
f459b602 MAP |
5134 | if (r < 0) |
5135 | return bus_log_parse_error(r); | |
8c7be95e | 5136 | |
f459b602 MAP |
5137 | r = sd_bus_message_exit_container(m); |
5138 | if (r < 0) | |
5139 | return bus_log_parse_error(r); | |
8c7be95e | 5140 | |
07636114 | 5141 | return 1; |
8c7be95e | 5142 | |
07636114 | 5143 | } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "Paths")) { |
f459b602 | 5144 | const char *type, *path; |
67419600 | 5145 | |
f459b602 MAP |
5146 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(ss)"); |
5147 | if (r < 0) | |
5148 | return bus_log_parse_error(r); | |
ebf57b80 | 5149 | |
f459b602 | 5150 | while ((r = sd_bus_message_read(m, "(ss)", &type, &path)) > 0) |
102b0214 | 5151 | bus_print_property_valuef(name, expected_value, value, "%s (%s)", path, type); |
f459b602 MAP |
5152 | if (r < 0) |
5153 | return bus_log_parse_error(r); | |
ebf57b80 | 5154 | |
f459b602 MAP |
5155 | r = sd_bus_message_exit_container(m); |
5156 | if (r < 0) | |
5157 | return bus_log_parse_error(r); | |
ebf57b80 | 5158 | |
07636114 | 5159 | return 1; |
582a507f | 5160 | |
07636114 | 5161 | } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "Listen")) { |
f459b602 | 5162 | const char *type, *path; |
67419600 | 5163 | |
f459b602 MAP |
5164 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(ss)"); |
5165 | if (r < 0) | |
5166 | return bus_log_parse_error(r); | |
67419600 | 5167 | |
f459b602 | 5168 | while ((r = sd_bus_message_read(m, "(ss)", &type, &path)) > 0) |
102b0214 | 5169 | bus_print_property_valuef(name, expected_value, value, "%s (%s)", path, type); |
f459b602 MAP |
5170 | if (r < 0) |
5171 | return bus_log_parse_error(r); | |
67419600 | 5172 | |
f459b602 MAP |
5173 | r = sd_bus_message_exit_container(m); |
5174 | if (r < 0) | |
5175 | return bus_log_parse_error(r); | |
67419600 | 5176 | |
07636114 | 5177 | return 1; |
67419600 | 5178 | |
07636114 | 5179 | } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "TimersMonotonic")) { |
f459b602 | 5180 | const char *base; |
07636114 | 5181 | uint64_t v, next_elapse; |
707e5e52 | 5182 | |
f459b602 MAP |
5183 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(stt)"); |
5184 | if (r < 0) | |
5185 | return bus_log_parse_error(r); | |
552e4331 | 5186 | |
07636114 | 5187 | while ((r = sd_bus_message_read(m, "(stt)", &base, &v, &next_elapse)) > 0) { |
f459b602 | 5188 | char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX]; |
fe68089d | 5189 | |
102b0214 YW |
5190 | bus_print_property_valuef(name, expected_value, value, "{ %s=%s ; next_elapse=%s }", base, |
5191 | format_timespan(timespan1, sizeof(timespan1), v, 0), | |
5192 | format_timespan(timespan2, sizeof(timespan2), next_elapse, 0)); | |
fe68089d | 5193 | } |
f459b602 MAP |
5194 | if (r < 0) |
5195 | return bus_log_parse_error(r); | |
5196 | ||
5197 | r = sd_bus_message_exit_container(m); | |
5198 | if (r < 0) | |
5199 | return bus_log_parse_error(r); | |
fe68089d | 5200 | |
07636114 | 5201 | return 1; |
fe68089d | 5202 | |
07636114 | 5203 | } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "TimersCalendar")) { |
fa6dee52 YW |
5204 | const char *base, *spec; |
5205 | uint64_t next_elapse; | |
5206 | ||
5207 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(sst)"); | |
5208 | if (r < 0) | |
5209 | return bus_log_parse_error(r); | |
5210 | ||
5211 | while ((r = sd_bus_message_read(m, "(sst)", &base, &spec, &next_elapse)) > 0) { | |
5212 | char timestamp[FORMAT_TIMESTAMP_MAX]; | |
5213 | ||
102b0214 YW |
5214 | bus_print_property_valuef(name, expected_value, value, "{ %s=%s ; next_elapse=%s }", base, spec, |
5215 | format_timestamp(timestamp, sizeof(timestamp), next_elapse)); | |
fa6dee52 YW |
5216 | } |
5217 | if (r < 0) | |
5218 | return bus_log_parse_error(r); | |
5219 | ||
5220 | r = sd_bus_message_exit_container(m); | |
5221 | if (r < 0) | |
5222 | return bus_log_parse_error(r); | |
5223 | ||
07636114 | 5224 | return 1; |
fa6dee52 | 5225 | |
07636114 | 5226 | } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && startswith(name, "Exec")) { |
f459b602 | 5227 | ExecStatusInfo info = {}; |
b3d59367 | 5228 | bool is_ex_prop = endswith(name, "Ex"); |
f459b602 | 5229 | |
b3d59367 | 5230 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, is_ex_prop ? "(sasasttttuii)" : "(sasbttttuii)"); |
f459b602 MAP |
5231 | if (r < 0) |
5232 | return bus_log_parse_error(r); | |
5233 | ||
b3d59367 | 5234 | while ((r = exec_status_info_deserialize(m, &info, is_ex_prop)) > 0) { |
f459b602 | 5235 | char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX]; |
b3d59367 AZ |
5236 | _cleanup_strv_free_ char **optv = NULL; |
5237 | _cleanup_free_ char *tt, *o = NULL; | |
f459b602 MAP |
5238 | |
5239 | tt = strv_join(info.argv, " "); | |
5240 | ||
b3d59367 AZ |
5241 | if (is_ex_prop) { |
5242 | r = exec_command_flags_to_strv(info.flags, &optv); | |
5243 | if (r < 0) | |
5244 | return log_error_errno(r, "Failed to convert ExecCommandFlags to strv: %m"); | |
5245 | ||
5246 | o = strv_join(optv, " "); | |
5247 | ||
5248 | bus_print_property_valuef(name, expected_value, value, | |
5249 | "{ path=%s ; argv[]=%s ; flags=%s ; start_time=[%s] ; stop_time=[%s] ; pid="PID_FMT" ; code=%s ; status=%i%s%s }", | |
5250 | strna(info.path), | |
5251 | strna(tt), | |
5252 | strna(o), | |
5253 | strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)), | |
5254 | strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)), | |
5255 | info.pid, | |
5256 | sigchld_code_to_string(info.code), | |
5257 | info.status, | |
5258 | info.code == CLD_EXITED ? "" : "/", | |
5259 | strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status))); | |
5260 | } else | |
5261 | bus_print_property_valuef(name, expected_value, value, | |
5262 | "{ path=%s ; argv[]=%s ; ignore_errors=%s ; start_time=[%s] ; stop_time=[%s] ; pid="PID_FMT" ; code=%s ; status=%i%s%s }", | |
5263 | strna(info.path), | |
5264 | strna(tt), | |
5265 | yes_no(info.ignore), | |
5266 | strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)), | |
5267 | strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)), | |
5268 | info.pid, | |
5269 | sigchld_code_to_string(info.code), | |
5270 | info.status, | |
5271 | info.code == CLD_EXITED ? "" : "/", | |
5272 | strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status))); | |
fe68089d | 5273 | |
582a507f LP |
5274 | free(info.path); |
5275 | strv_free(info.argv); | |
f459b602 | 5276 | zero(info); |
707e5e52 LP |
5277 | } |
5278 | ||
f459b602 MAP |
5279 | r = sd_bus_message_exit_container(m); |
5280 | if (r < 0) | |
5281 | return bus_log_parse_error(r); | |
5282 | ||
07636114 | 5283 | return 1; |
4ad49000 | 5284 | |
07636114 | 5285 | } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "DeviceAllow")) { |
f459b602 | 5286 | const char *path, *rwm; |
4ad49000 | 5287 | |
f459b602 MAP |
5288 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(ss)"); |
5289 | if (r < 0) | |
5290 | return bus_log_parse_error(r); | |
4ad49000 | 5291 | |
f459b602 | 5292 | while ((r = sd_bus_message_read(m, "(ss)", &path, &rwm)) > 0) |
102b0214 | 5293 | bus_print_property_valuef(name, expected_value, value, "%s %s", strna(path), strna(rwm)); |
f459b602 MAP |
5294 | if (r < 0) |
5295 | return bus_log_parse_error(r); | |
4ad49000 | 5296 | |
f459b602 MAP |
5297 | r = sd_bus_message_exit_container(m); |
5298 | if (r < 0) | |
5299 | return bus_log_parse_error(r); | |
4ad49000 | 5300 | |
07636114 | 5301 | return 1; |
4ad49000 | 5302 | |
07636114 | 5303 | } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && |
72240b52 | 5304 | STR_IN_SET(name, "IODeviceWeight", "BlockIODeviceWeight")) { |
f459b602 MAP |
5305 | const char *path; |
5306 | uint64_t weight; | |
b8ab2dc6 | 5307 | |
f459b602 MAP |
5308 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(st)"); |
5309 | if (r < 0) | |
5310 | return bus_log_parse_error(r); | |
b8ab2dc6 | 5311 | |
f459b602 | 5312 | while ((r = sd_bus_message_read(m, "(st)", &path, &weight)) > 0) |
102b0214 | 5313 | bus_print_property_valuef(name, expected_value, value, "%s %"PRIu64, strna(path), weight); |
f459b602 MAP |
5314 | if (r < 0) |
5315 | return bus_log_parse_error(r); | |
b8ab2dc6 | 5316 | |
f459b602 MAP |
5317 | r = sd_bus_message_exit_container(m); |
5318 | if (r < 0) | |
5319 | return bus_log_parse_error(r); | |
b8ab2dc6 | 5320 | |
07636114 | 5321 | return 1; |
b8ab2dc6 | 5322 | |
07636114 | 5323 | } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && |
72240b52 ZJS |
5324 | (cgroup_io_limit_type_from_string(name) >= 0 || |
5325 | STR_IN_SET(name, "BlockIOReadBandwidth", "BlockIOWriteBandwidth"))) { | |
f459b602 MAP |
5326 | const char *path; |
5327 | uint64_t bandwidth; | |
4ad49000 | 5328 | |
f459b602 MAP |
5329 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(st)"); |
5330 | if (r < 0) | |
5331 | return bus_log_parse_error(r); | |
4ad49000 | 5332 | |
f459b602 | 5333 | while ((r = sd_bus_message_read(m, "(st)", &path, &bandwidth)) > 0) |
102b0214 | 5334 | bus_print_property_valuef(name, expected_value, value, "%s %"PRIu64, strna(path), bandwidth); |
f459b602 MAP |
5335 | if (r < 0) |
5336 | return bus_log_parse_error(r); | |
4ad49000 | 5337 | |
f459b602 MAP |
5338 | r = sd_bus_message_exit_container(m); |
5339 | if (r < 0) | |
5340 | return bus_log_parse_error(r); | |
4ad49000 | 5341 | |
07636114 | 5342 | return 1; |
6ae4283c TH |
5343 | |
5344 | } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && | |
5345 | streq(name, "IODeviceLatencyTargetUSec")) { | |
5346 | char ts[FORMAT_TIMESPAN_MAX]; | |
5347 | const char *path; | |
5348 | uint64_t target; | |
5349 | ||
5350 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(st)"); | |
5351 | if (r < 0) | |
5352 | return bus_log_parse_error(r); | |
5353 | ||
5354 | while ((r = sd_bus_message_read(m, "(st)", &path, &target)) > 0) | |
102b0214 YW |
5355 | bus_print_property_valuef(name, expected_value, value, "%s %s", strna(path), |
5356 | format_timespan(ts, sizeof(ts), target, 1)); | |
6ae4283c TH |
5357 | if (r < 0) |
5358 | return bus_log_parse_error(r); | |
5359 | ||
5360 | r = sd_bus_message_exit_container(m); | |
5361 | if (r < 0) | |
5362 | return bus_log_parse_error(r); | |
5363 | ||
5364 | return 1; | |
08f3be7a | 5365 | |
07636114 | 5366 | } else if (contents[0] == SD_BUS_TYPE_BYTE && streq(name, "StandardInputData")) { |
08f3be7a LP |
5367 | _cleanup_free_ char *h = NULL; |
5368 | const void *p; | |
5369 | size_t sz; | |
5370 | ssize_t n; | |
5371 | ||
5372 | r = sd_bus_message_read_array(m, 'y', &p, &sz); | |
5373 | if (r < 0) | |
5374 | return bus_log_parse_error(r); | |
5375 | ||
5376 | n = base64mem(p, sz, &h); | |
5377 | if (n < 0) | |
5378 | return log_oom(); | |
5379 | ||
102b0214 | 5380 | bus_print_property_value(name, expected_value, value, h); |
08f3be7a | 5381 | |
07636114 | 5382 | return 1; |
9c1f9465 YW |
5383 | |
5384 | } else if (STR_IN_SET(name, "IPAddressAllow", "IPAddressDeny")) { | |
5385 | _cleanup_free_ char *addresses = NULL; | |
5386 | ||
5387 | r = sd_bus_message_enter_container(m, 'a', "(iayu)"); | |
5388 | if (r < 0) | |
5389 | return bus_log_parse_error(r); | |
5390 | ||
5391 | for (;;) { | |
5392 | _cleanup_free_ char *str = NULL; | |
5393 | uint32_t prefixlen; | |
5394 | int32_t family; | |
5395 | const void *ap; | |
5396 | size_t an; | |
5397 | ||
5398 | r = sd_bus_message_enter_container(m, 'r', "iayu"); | |
5399 | if (r < 0) | |
5400 | return bus_log_parse_error(r); | |
5401 | if (r == 0) | |
5402 | break; | |
5403 | ||
5404 | r = sd_bus_message_read(m, "i", &family); | |
5405 | if (r < 0) | |
5406 | return bus_log_parse_error(r); | |
5407 | ||
5408 | r = sd_bus_message_read_array(m, 'y', &ap, &an); | |
5409 | if (r < 0) | |
5410 | return bus_log_parse_error(r); | |
5411 | ||
5412 | r = sd_bus_message_read(m, "u", &prefixlen); | |
5413 | if (r < 0) | |
5414 | return bus_log_parse_error(r); | |
5415 | ||
5416 | r = sd_bus_message_exit_container(m); | |
5417 | if (r < 0) | |
5418 | return bus_log_parse_error(r); | |
5419 | ||
5420 | if (!IN_SET(family, AF_INET, AF_INET6)) | |
5421 | continue; | |
5422 | ||
5423 | if (an != FAMILY_ADDRESS_SIZE(family)) | |
5424 | continue; | |
5425 | ||
5426 | if (prefixlen > FAMILY_ADDRESS_SIZE(family) * 8) | |
5427 | continue; | |
5428 | ||
5429 | if (in_addr_prefix_to_string(family, (union in_addr_union *) ap, prefixlen, &str) < 0) | |
5430 | continue; | |
5431 | ||
5432 | if (!strextend_with_separator(&addresses, " ", str, NULL)) | |
5433 | return log_oom(); | |
5434 | } | |
5435 | ||
5436 | r = sd_bus_message_exit_container(m); | |
5437 | if (r < 0) | |
5438 | return bus_log_parse_error(r); | |
5439 | ||
5440 | if (all || !isempty(addresses)) | |
5441 | bus_print_property_value(name, expected_value, value, strempty(addresses)); | |
5442 | ||
5443 | return 1; | |
4c787df7 YW |
5444 | |
5445 | } else if (STR_IN_SET(name, "BindPaths", "BindReadOnlyPaths")) { | |
5446 | _cleanup_free_ char *paths = NULL; | |
5447 | const char *source, *dest; | |
5448 | int ignore_enoent; | |
5449 | uint64_t rbind; | |
5450 | ||
5451 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(ssbt)"); | |
5452 | if (r < 0) | |
5453 | return bus_log_parse_error(r); | |
5454 | ||
5455 | while ((r = sd_bus_message_read(m, "(ssbt)", &source, &dest, &ignore_enoent, &rbind)) > 0) { | |
5456 | _cleanup_free_ char *str = NULL; | |
5457 | ||
5458 | if (isempty(source)) | |
5459 | continue; | |
5460 | ||
5461 | if (asprintf(&str, "%s%s%s%s%s", | |
5462 | ignore_enoent ? "-" : "", | |
5463 | source, | |
5464 | isempty(dest) ? "" : ":", | |
5465 | strempty(dest), | |
5466 | rbind == MS_REC ? ":rbind" : "") < 0) | |
5467 | return log_oom(); | |
5468 | ||
5469 | if (!strextend_with_separator(&paths, " ", str, NULL)) | |
5470 | return log_oom(); | |
5471 | } | |
5472 | if (r < 0) | |
5473 | return bus_log_parse_error(r); | |
5474 | ||
5475 | r = sd_bus_message_exit_container(m); | |
5476 | if (r < 0) | |
5477 | return bus_log_parse_error(r); | |
5478 | ||
5479 | if (all || !isempty(paths)) | |
5480 | bus_print_property_value(name, expected_value, value, strempty(paths)); | |
5481 | ||
5482 | return 1; | |
5483 | ||
5484 | } else if (streq(name, "TemporaryFileSystem")) { | |
5485 | _cleanup_free_ char *paths = NULL; | |
5486 | const char *target, *option; | |
5487 | ||
5488 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(ss)"); | |
5489 | if (r < 0) | |
5490 | return bus_log_parse_error(r); | |
5491 | ||
5492 | while ((r = sd_bus_message_read(m, "(ss)", &target, &option)) > 0) { | |
5493 | _cleanup_free_ char *str = NULL; | |
5494 | ||
5495 | if (isempty(target)) | |
5496 | continue; | |
5497 | ||
5498 | if (asprintf(&str, "%s%s%s", target, isempty(option) ? "" : ":", strempty(option)) < 0) | |
5499 | return log_oom(); | |
5500 | ||
5501 | if (!strextend_with_separator(&paths, " ", str, NULL)) | |
5502 | return log_oom(); | |
5503 | } | |
5504 | if (r < 0) | |
5505 | return bus_log_parse_error(r); | |
5506 | ||
5507 | r = sd_bus_message_exit_container(m); | |
5508 | if (r < 0) | |
5509 | return bus_log_parse_error(r); | |
5510 | ||
5511 | if (all || !isempty(paths)) | |
5512 | bus_print_property_value(name, expected_value, value, strempty(paths)); | |
5513 | ||
5514 | return 1; | |
305a2850 YW |
5515 | |
5516 | } else if (streq(name, "LogExtraFields")) { | |
5517 | _cleanup_free_ char *fields = NULL; | |
5518 | const void *p; | |
5519 | size_t sz; | |
5520 | ||
5521 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "ay"); | |
5522 | if (r < 0) | |
5523 | return bus_log_parse_error(r); | |
5524 | ||
5525 | while ((r = sd_bus_message_read_array(m, 'y', &p, &sz)) > 0) { | |
5526 | _cleanup_free_ char *str = NULL; | |
5527 | const char *eq; | |
5528 | ||
5529 | if (memchr(p, 0, sz)) | |
5530 | continue; | |
5531 | ||
5532 | eq = memchr(p, '=', sz); | |
5533 | if (!eq) | |
5534 | continue; | |
5535 | ||
5536 | if (!journal_field_valid(p, eq - (const char*) p, false)) | |
5537 | continue; | |
5538 | ||
5539 | str = malloc(sz + 1); | |
5540 | if (!str) | |
5541 | return log_oom(); | |
5542 | ||
5543 | memcpy(str, p, sz); | |
5544 | str[sz] = '\0'; | |
5545 | ||
5546 | if (!utf8_is_valid(str)) | |
5547 | continue; | |
5548 | ||
5549 | if (!strextend_with_separator(&fields, " ", str, NULL)) | |
5550 | return log_oom(); | |
5551 | } | |
5552 | if (r < 0) | |
5553 | return bus_log_parse_error(r); | |
5554 | ||
5555 | r = sd_bus_message_exit_container(m); | |
5556 | if (r < 0) | |
5557 | return bus_log_parse_error(r); | |
5558 | ||
5559 | if (all || !isempty(fields)) | |
5560 | bus_print_property_value(name, expected_value, value, strempty(fields)); | |
5561 | ||
a047f4f1 | 5562 | return 1; |
b070c7c0 | 5563 | } else if (contents[0] == SD_BUS_TYPE_BYTE && STR_IN_SET(name, "CPUAffinity", "NUMAMask")) { |
a047f4f1 MS |
5564 | _cleanup_free_ char *affinity = NULL; |
5565 | _cleanup_(cpu_set_reset) CPUSet set = {}; | |
5566 | const void *a; | |
5567 | size_t n; | |
5568 | ||
5569 | r = sd_bus_message_read_array(m, 'y', &a, &n); | |
5570 | if (r < 0) | |
5571 | return bus_log_parse_error(r); | |
5572 | ||
5573 | r = cpu_set_from_dbus(a, n, &set); | |
5574 | if (r < 0) | |
b070c7c0 | 5575 | return log_error_errno(r, "Failed to deserialize %s: %m", name); |
a047f4f1 MS |
5576 | |
5577 | affinity = cpu_set_to_range_string(&set); | |
5578 | if (!affinity) | |
5579 | return log_oom(); | |
5580 | ||
5581 | bus_print_property_value(name, expected_value, value, affinity); | |
5582 | ||
305a2850 | 5583 | return 1; |
48220598 LP |
5584 | } |
5585 | ||
5586 | break; | |
5587 | } | |
5588 | ||
48220598 LP |
5589 | return 0; |
5590 | } | |
5591 | ||
8559e61d YW |
5592 | typedef enum SystemctlShowMode{ |
5593 | SYSTEMCTL_SHOW_PROPERTIES, | |
5594 | SYSTEMCTL_SHOW_STATUS, | |
5595 | SYSTEMCTL_SHOW_HELP, | |
5596 | _SYSTEMCTL_SHOW_MODE_MAX, | |
5597 | _SYSTEMCTL_SHOW_MODE_INVALID = -1, | |
5598 | } SystemctlShowMode; | |
5599 | ||
aa8d423e | 5600 | static const char* const systemctl_show_mode_table[_SYSTEMCTL_SHOW_MODE_MAX] = { |
8559e61d YW |
5601 | [SYSTEMCTL_SHOW_PROPERTIES] = "show", |
5602 | [SYSTEMCTL_SHOW_STATUS] = "status", | |
5603 | [SYSTEMCTL_SHOW_HELP] = "help", | |
5604 | }; | |
5605 | ||
5606 | DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(systemctl_show_mode, SystemctlShowMode); | |
5607 | ||
f459b602 | 5608 | static int show_one( |
f459b602 MAP |
5609 | sd_bus *bus, |
5610 | const char *path, | |
3dced37b | 5611 | const char *unit, |
8559e61d | 5612 | SystemctlShowMode show_mode, |
f459b602 MAP |
5613 | bool *new_line, |
5614 | bool *ellipsized) { | |
5615 | ||
3dced37b | 5616 | static const struct bus_properties_map property_map[] = { |
b3d59367 AZ |
5617 | { "LoadState", "s", NULL, offsetof(UnitStatusInfo, load_state) }, |
5618 | { "ActiveState", "s", NULL, offsetof(UnitStatusInfo, active_state) }, | |
5619 | { "Documentation", "as", NULL, offsetof(UnitStatusInfo, documentation) }, | |
4679a8c3 YW |
5620 | {} |
5621 | }, status_map[] = { | |
b3d59367 AZ |
5622 | { "Id", "s", NULL, offsetof(UnitStatusInfo, id) }, |
5623 | { "LoadState", "s", NULL, offsetof(UnitStatusInfo, load_state) }, | |
5624 | { "ActiveState", "s", NULL, offsetof(UnitStatusInfo, active_state) }, | |
5625 | { "SubState", "s", NULL, offsetof(UnitStatusInfo, sub_state) }, | |
5626 | { "UnitFileState", "s", NULL, offsetof(UnitStatusInfo, unit_file_state) }, | |
5627 | { "UnitFilePreset", "s", NULL, offsetof(UnitStatusInfo, unit_file_preset) }, | |
5628 | { "Description", "s", NULL, offsetof(UnitStatusInfo, description) }, | |
5629 | { "Following", "s", NULL, offsetof(UnitStatusInfo, following) }, | |
5630 | { "Documentation", "as", NULL, offsetof(UnitStatusInfo, documentation) }, | |
5631 | { "FragmentPath", "s", NULL, offsetof(UnitStatusInfo, fragment_path) }, | |
5632 | { "SourcePath", "s", NULL, offsetof(UnitStatusInfo, source_path) }, | |
5633 | { "ControlGroup", "s", NULL, offsetof(UnitStatusInfo, control_group) }, | |
5634 | { "DropInPaths", "as", NULL, offsetof(UnitStatusInfo, dropin_paths) }, | |
5635 | { "LoadError", "(ss)", map_load_error, offsetof(UnitStatusInfo, load_error) }, | |
5636 | { "Result", "s", NULL, offsetof(UnitStatusInfo, result) }, | |
5637 | { "InactiveExitTimestamp", "t", NULL, offsetof(UnitStatusInfo, inactive_exit_timestamp) }, | |
5638 | { "InactiveExitTimestampMonotonic", "t", NULL, offsetof(UnitStatusInfo, inactive_exit_timestamp_monotonic) }, | |
5639 | { "ActiveEnterTimestamp", "t", NULL, offsetof(UnitStatusInfo, active_enter_timestamp) }, | |
5640 | { "ActiveExitTimestamp", "t", NULL, offsetof(UnitStatusInfo, active_exit_timestamp) }, | |
5641 | { "InactiveEnterTimestamp", "t", NULL, offsetof(UnitStatusInfo, inactive_enter_timestamp) }, | |
5642 | { "NeedDaemonReload", "b", NULL, offsetof(UnitStatusInfo, need_daemon_reload) }, | |
5643 | { "Transient", "b", NULL, offsetof(UnitStatusInfo, transient) }, | |
5644 | { "ExecMainPID", "u", NULL, offsetof(UnitStatusInfo, main_pid) }, | |
5645 | { "MainPID", "u", map_main_pid, 0 }, | |
5646 | { "ControlPID", "u", NULL, offsetof(UnitStatusInfo, control_pid) }, | |
5647 | { "StatusText", "s", NULL, offsetof(UnitStatusInfo, status_text) }, | |
5648 | { "PIDFile", "s", NULL, offsetof(UnitStatusInfo, pid_file) }, | |
5649 | { "StatusErrno", "i", NULL, offsetof(UnitStatusInfo, status_errno) }, | |
5650 | { "ExecMainStartTimestamp", "t", NULL, offsetof(UnitStatusInfo, start_timestamp) }, | |
5651 | { "ExecMainExitTimestamp", "t", NULL, offsetof(UnitStatusInfo, exit_timestamp) }, | |
5652 | { "ExecMainCode", "i", NULL, offsetof(UnitStatusInfo, exit_code) }, | |
5653 | { "ExecMainStatus", "i", NULL, offsetof(UnitStatusInfo, exit_status) }, | |
5654 | { "ConditionTimestamp", "t", NULL, offsetof(UnitStatusInfo, condition_timestamp) }, | |
5655 | { "ConditionResult", "b", NULL, offsetof(UnitStatusInfo, condition_result) }, | |
5656 | { "Conditions", "a(sbbsi)", map_conditions, 0 }, | |
5657 | { "AssertTimestamp", "t", NULL, offsetof(UnitStatusInfo, assert_timestamp) }, | |
5658 | { "AssertResult", "b", NULL, offsetof(UnitStatusInfo, assert_result) }, | |
5659 | { "Asserts", "a(sbbsi)", map_asserts, 0 }, | |
5660 | { "NextElapseUSecRealtime", "t", NULL, offsetof(UnitStatusInfo, next_elapse_real) }, | |
5661 | { "NextElapseUSecMonotonic", "t", NULL, offsetof(UnitStatusInfo, next_elapse_monotonic) }, | |
5662 | { "NAccepted", "u", NULL, offsetof(UnitStatusInfo, n_accepted) }, | |
5663 | { "NConnections", "u", NULL, offsetof(UnitStatusInfo, n_connections) }, | |
5664 | { "NRefused", "u", NULL, offsetof(UnitStatusInfo, n_refused) }, | |
5665 | { "Accept", "b", NULL, offsetof(UnitStatusInfo, accept) }, | |
5666 | { "Listen", "a(ss)", map_listen, offsetof(UnitStatusInfo, listen) }, | |
5667 | { "SysFSPath", "s", NULL, offsetof(UnitStatusInfo, sysfs_path) }, | |
5668 | { "Where", "s", NULL, offsetof(UnitStatusInfo, where) }, | |
5669 | { "What", "s", NULL, offsetof(UnitStatusInfo, what) }, | |
5670 | { "MemoryCurrent", "t", NULL, offsetof(UnitStatusInfo, memory_current) }, | |
5671 | { "DefaultMemoryMin", "t", NULL, offsetof(UnitStatusInfo, default_memory_min) }, | |
5672 | { "DefaultMemoryLow", "t", NULL, offsetof(UnitStatusInfo, default_memory_low) }, | |
5673 | { "MemoryMin", "t", NULL, offsetof(UnitStatusInfo, memory_min) }, | |
5674 | { "MemoryLow", "t", NULL, offsetof(UnitStatusInfo, memory_low) }, | |
5675 | { "MemoryHigh", "t", NULL, offsetof(UnitStatusInfo, memory_high) }, | |
5676 | { "MemoryMax", "t", NULL, offsetof(UnitStatusInfo, memory_max) }, | |
5677 | { "MemorySwapMax", "t", NULL, offsetof(UnitStatusInfo, memory_swap_max) }, | |
5678 | { "MemoryLimit", "t", NULL, offsetof(UnitStatusInfo, memory_limit) }, | |
5679 | { "CPUUsageNSec", "t", NULL, offsetof(UnitStatusInfo, cpu_usage_nsec) }, | |
5680 | { "TasksCurrent", "t", NULL, offsetof(UnitStatusInfo, tasks_current) }, | |
5681 | { "TasksMax", "t", NULL, offsetof(UnitStatusInfo, tasks_max) }, | |
5682 | { "IPIngressBytes", "t", NULL, offsetof(UnitStatusInfo, ip_ingress_bytes) }, | |
5683 | { "IPEgressBytes", "t", NULL, offsetof(UnitStatusInfo, ip_egress_bytes) }, | |
5684 | { "IOReadBytes", "t", NULL, offsetof(UnitStatusInfo, io_read_bytes) }, | |
5685 | { "IOWriteBytes", "t", NULL, offsetof(UnitStatusInfo, io_write_bytes) }, | |
5686 | { "ExecStartPre", "a(sasbttttuii)", map_exec, 0 }, | |
5687 | { "ExecStartPreEx", "a(sasasttttuii)", map_exec, 0 }, | |
5688 | { "ExecStart", "a(sasbttttuii)", map_exec, 0 }, | |
5689 | { "ExecStartEx", "a(sasasttttuii)", map_exec, 0 }, | |
5690 | { "ExecStartPost", "a(sasbttttuii)", map_exec, 0 }, | |
5691 | { "ExecStartPostEx", "a(sasasttttuii)", map_exec, 0 }, | |
5692 | { "ExecReload", "a(sasbttttuii)", map_exec, 0 }, | |
5693 | { "ExecStopPre", "a(sasbttttuii)", map_exec, 0 }, | |
5694 | { "ExecStop", "a(sasbttttuii)", map_exec, 0 }, | |
5695 | { "ExecStopPost", "a(sasbttttuii)", map_exec, 0 }, | |
3dced37b LP |
5696 | {} |
5697 | }; | |
5698 | ||
4afd3348 LP |
5699 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
5700 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
3dced37b | 5701 | _cleanup_set_free_ Set *found_properties = NULL; |
a7335518 | 5702 | _cleanup_(unit_status_info_free) UnitStatusInfo info = { |
934277fe | 5703 | .memory_current = (uint64_t) -1, |
da4d897e TH |
5704 | .memory_high = CGROUP_LIMIT_MAX, |
5705 | .memory_max = CGROUP_LIMIT_MAX, | |
96e131ea | 5706 | .memory_swap_max = CGROUP_LIMIT_MAX, |
934277fe | 5707 | .memory_limit = (uint64_t) -1, |
5ad096b3 | 5708 | .cpu_usage_nsec = (uint64_t) -1, |
03a7b521 LP |
5709 | .tasks_current = (uint64_t) -1, |
5710 | .tasks_max = (uint64_t) -1, | |
0e97c93f DM |
5711 | .ip_ingress_bytes = (uint64_t) -1, |
5712 | .ip_egress_bytes = (uint64_t) -1, | |
7a5de3f9 LP |
5713 | .io_read_bytes = UINT64_MAX, |
5714 | .io_write_bytes = UINT64_MAX, | |
934277fe | 5715 | }; |
4679a8c3 | 5716 | char **pp; |
f459b602 | 5717 | int r; |
48220598 | 5718 | |
48220598 | 5719 | assert(path); |
61cbdc4b | 5720 | assert(new_line); |
48220598 | 5721 | |
e3e0314b ZJS |
5722 | log_debug("Showing one %s", path); |
5723 | ||
4679a8c3 | 5724 | r = bus_map_all_properties( |
f22f08cd SP |
5725 | bus, |
5726 | "org.freedesktop.systemd1", | |
5727 | path, | |
4679a8c3 | 5728 | show_mode == SYSTEMCTL_SHOW_STATUS ? status_map : property_map, |
a7e4861c | 5729 | BUS_MAP_BOOLEAN_AS_BOOL, |
f459b602 | 5730 | &error, |
f22f08cd | 5731 | &reply, |
4679a8c3 | 5732 | &info); |
4c3e8e39 LP |
5733 | if (r < 0) |
5734 | return log_error_errno(r, "Failed to get properties: %s", bus_error_message(&error, r)); | |
48220598 | 5735 | |
4679a8c3 YW |
5736 | if (unit && streq_ptr(info.load_state, "not-found") && streq_ptr(info.active_state, "inactive")) { |
5737 | log_full(show_mode == SYSTEMCTL_SHOW_STATUS ? LOG_ERR : LOG_DEBUG, | |
5738 | "Unit %s could not be found.", unit); | |
e33a06a1 | 5739 | |
4679a8c3 YW |
5740 | if (show_mode == SYSTEMCTL_SHOW_STATUS) |
5741 | return EXIT_PROGRAM_OR_SERVICES_STATUS_UNKNOWN; | |
5742 | else if (show_mode == SYSTEMCTL_SHOW_HELP) | |
5743 | return -ENOENT; | |
5744 | } | |
e33a06a1 | 5745 | |
4679a8c3 YW |
5746 | if (*new_line) |
5747 | printf("\n"); | |
3dced37b | 5748 | |
4679a8c3 YW |
5749 | *new_line = true; |
5750 | ||
5751 | if (show_mode == SYSTEMCTL_SHOW_STATUS) { | |
5752 | print_status_info(bus, &info, ellipsized); | |
5753 | ||
5754 | if (info.active_state && !STR_IN_SET(info.active_state, "active", "reloading")) | |
5755 | return EXIT_PROGRAM_NOT_RUNNING; | |
5756 | ||
5757 | return EXIT_PROGRAM_RUNNING_OR_SERVICE_OK; | |
5758 | ||
5759 | } else if (show_mode == SYSTEMCTL_SHOW_HELP) { | |
5760 | show_unit_help(&info); | |
5761 | return 0; | |
3dced37b | 5762 | } |
e33a06a1 | 5763 | |
4679a8c3 YW |
5764 | r = sd_bus_message_rewind(reply, true); |
5765 | if (r < 0) | |
5766 | return log_error_errno(r, "Failed to rewind: %s", bus_error_message(&error, r)); | |
5767 | ||
07636114 | 5768 | r = bus_message_print_all_properties(reply, print_property, arg_properties, arg_value, arg_all, &found_properties); |
f459b602 MAP |
5769 | if (r < 0) |
5770 | return bus_log_parse_error(r); | |
48220598 | 5771 | |
4679a8c3 YW |
5772 | STRV_FOREACH(pp, arg_properties) |
5773 | if (!set_contains(found_properties, *pp)) | |
5774 | log_debug("Property %s does not exist.", *pp); | |
3dced37b | 5775 | |
4679a8c3 | 5776 | return 0; |
48220598 LP |
5777 | } |
5778 | ||
f74294c1 | 5779 | static int get_unit_dbus_path_by_pid( |
f459b602 MAP |
5780 | sd_bus *bus, |
5781 | uint32_t pid, | |
f74294c1 | 5782 | char **unit) { |
f459b602 | 5783 | |
4afd3348 LP |
5784 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
5785 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
373d32c9 | 5786 | char *u; |
a223b325 MS |
5787 | int r; |
5788 | ||
f459b602 | 5789 | r = sd_bus_call_method( |
f22f08cd SP |
5790 | bus, |
5791 | "org.freedesktop.systemd1", | |
5792 | "/org/freedesktop/systemd1", | |
5793 | "org.freedesktop.systemd1.Manager", | |
5794 | "GetUnitByPID", | |
f459b602 | 5795 | &error, |
f22f08cd | 5796 | &reply, |
f459b602 | 5797 | "u", pid); |
691395d8 LP |
5798 | if (r < 0) |
5799 | return log_error_errno(r, "Failed to get unit for PID %"PRIu32": %s", pid, bus_error_message(&error, r)); | |
a223b325 | 5800 | |
373d32c9 | 5801 | r = sd_bus_message_read(reply, "o", &u); |
f459b602 MAP |
5802 | if (r < 0) |
5803 | return bus_log_parse_error(r); | |
5804 | ||
373d32c9 LP |
5805 | u = strdup(u); |
5806 | if (!u) | |
5807 | return log_oom(); | |
5808 | ||
5809 | *unit = u; | |
f74294c1 | 5810 | return 0; |
a223b325 MS |
5811 | } |
5812 | ||
f459b602 | 5813 | static int show_all( |
f459b602 | 5814 | sd_bus *bus, |
f459b602 MAP |
5815 | bool *new_line, |
5816 | bool *ellipsized) { | |
5817 | ||
4afd3348 | 5818 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
f459b602 MAP |
5819 | _cleanup_free_ UnitInfo *unit_infos = NULL; |
5820 | const UnitInfo *u; | |
5821 | unsigned c; | |
5bb75bc7 | 5822 | int r, ret = 0; |
265a7a2a | 5823 | |
1238ee09 | 5824 | r = get_unit_list(bus, NULL, NULL, &unit_infos, 0, &reply); |
265a7a2a ZJS |
5825 | if (r < 0) |
5826 | return r; | |
5827 | ||
0221d68a | 5828 | (void) pager_open(arg_pager_flags); |
dbed408b | 5829 | |
f459b602 MAP |
5830 | c = (unsigned) r; |
5831 | ||
93bab288 | 5832 | typesafe_qsort(unit_infos, c, compare_unit_info); |
991f2a39 | 5833 | |
265a7a2a | 5834 | for (u = unit_infos; u < unit_infos + c; u++) { |
7fd1b19b | 5835 | _cleanup_free_ char *p = NULL; |
265a7a2a | 5836 | |
265a7a2a ZJS |
5837 | p = unit_dbus_path_from_name(u->id); |
5838 | if (!p) | |
5839 | return log_oom(); | |
5840 | ||
8559e61d | 5841 | r = show_one(bus, p, u->id, SYSTEMCTL_SHOW_STATUS, new_line, ellipsized); |
3df538da | 5842 | if (r < 0) |
265a7a2a | 5843 | return r; |
5bb75bc7 ZJS |
5844 | else if (r > 0 && ret == 0) |
5845 | ret = r; | |
265a7a2a ZJS |
5846 | } |
5847 | ||
5bb75bc7 | 5848 | return ret; |
265a7a2a ZJS |
5849 | } |
5850 | ||
8fcf784d LP |
5851 | static int show_system_status(sd_bus *bus) { |
5852 | char since1[FORMAT_TIMESTAMP_RELATIVE_MAX], since2[FORMAT_TIMESTAMP_MAX]; | |
f9e0eefc | 5853 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
e7e55dbd | 5854 | _cleanup_(machine_info_clear) struct machine_info mi = {}; |
f9e0eefc | 5855 | _cleanup_free_ char *hn = NULL; |
8fcf784d LP |
5856 | const char *on, *off; |
5857 | int r; | |
5858 | ||
5859 | hn = gethostname_malloc(); | |
5860 | if (!hn) | |
5861 | return log_oom(); | |
5862 | ||
a7e4861c YW |
5863 | r = bus_map_all_properties( |
5864 | bus, | |
5865 | "org.freedesktop.systemd1", | |
5866 | "/org/freedesktop/systemd1", | |
5867 | machine_info_property_map, | |
5868 | BUS_MAP_STRDUP, | |
5869 | &error, | |
5870 | NULL, | |
5871 | &mi); | |
f647962d | 5872 | if (r < 0) |
f9e0eefc | 5873 | return log_error_errno(r, "Failed to read server status: %s", bus_error_message(&error, r)); |
8fcf784d | 5874 | |
8fcf784d LP |
5875 | if (streq_ptr(mi.state, "degraded")) { |
5876 | on = ansi_highlight_red(); | |
1fc464f6 | 5877 | off = ansi_normal(); |
92f34a9c JR |
5878 | } else if (streq_ptr(mi.state, "running")) { |
5879 | on = ansi_highlight_green(); | |
5880 | off = ansi_normal(); | |
5881 | } else { | |
8fcf784d | 5882 | on = ansi_highlight_yellow(); |
1fc464f6 | 5883 | off = ansi_normal(); |
92f34a9c | 5884 | } |
8fcf784d | 5885 | |
9a6f746f | 5886 | printf("%s%s%s %s\n", on, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE), off, arg_host ? arg_host : hn); |
b0d14c69 | 5887 | |
8fcf784d LP |
5888 | printf(" State: %s%s%s\n", |
5889 | on, strna(mi.state), off); | |
5890 | ||
c6ba5b80 MP |
5891 | printf(" Jobs: %" PRIu32 " queued\n", mi.n_jobs); |
5892 | printf(" Failed: %" PRIu32 " units\n", mi.n_failed_units); | |
8fcf784d LP |
5893 | |
5894 | printf(" Since: %s; %s\n", | |
5895 | format_timestamp(since2, sizeof(since2), mi.timestamp), | |
5896 | format_timestamp_relative(since1, sizeof(since1), mi.timestamp)); | |
5897 | ||
5898 | printf(" CGroup: %s\n", mi.control_group ?: "/"); | |
4c315c2c IS |
5899 | if (IN_SET(arg_transport, |
5900 | BUS_TRANSPORT_LOCAL, | |
5901 | BUS_TRANSPORT_MACHINE)) { | |
8fcf784d LP |
5902 | static const char prefix[] = " "; |
5903 | unsigned c; | |
5904 | ||
5905 | c = columns(); | |
5906 | if (c > sizeof(prefix) - 1) | |
5907 | c -= sizeof(prefix) - 1; | |
5908 | else | |
5909 | c = 0; | |
5910 | ||
0ff308c8 | 5911 | show_cgroup(SYSTEMD_CGROUP_CONTROLLER, strempty(mi.control_group), prefix, c, get_output_flags()); |
8fcf784d LP |
5912 | } |
5913 | ||
8fcf784d LP |
5914 | return 0; |
5915 | } | |
5916 | ||
e449de87 | 5917 | static int show(int argc, char *argv[], void *userdata) { |
8559e61d YW |
5918 | bool new_line = false, ellipsized = false; |
5919 | SystemctlShowMode show_mode; | |
e3e0314b | 5920 | int r, ret = 0; |
4fbd7192 | 5921 | sd_bus *bus; |
48220598 | 5922 | |
e449de87 | 5923 | assert(argv); |
48220598 | 5924 | |
8559e61d | 5925 | show_mode = systemctl_show_mode_from_string(argv[0]); |
baaa35ad ZJS |
5926 | if (show_mode < 0) |
5927 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
5928 | "Invalid argument."); | |
e449de87 | 5929 | |
baaa35ad ZJS |
5930 | if (show_mode == SYSTEMCTL_SHOW_HELP && argc <= 1) |
5931 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
5932 | "This command expects one or more unit names. Did you mean --help?"); | |
61cbdc4b | 5933 | |
d2ad7e1b ZJS |
5934 | r = acquire_bus(BUS_MANAGER, &bus); |
5935 | if (r < 0) | |
5936 | return r; | |
5937 | ||
0221d68a | 5938 | (void) pager_open(arg_pager_flags); |
ec14911e | 5939 | |
4fbd7192 | 5940 | /* If no argument is specified inspect the manager itself */ |
8559e61d YW |
5941 | if (show_mode == SYSTEMCTL_SHOW_PROPERTIES && argc <= 1) |
5942 | return show_one(bus, "/org/freedesktop/systemd1", NULL, show_mode, &new_line, &ellipsized); | |
48220598 | 5943 | |
8559e61d | 5944 | if (show_mode == SYSTEMCTL_SHOW_STATUS && argc <= 1) { |
8fcf784d | 5945 | |
8fcf784d LP |
5946 | show_system_status(bus); |
5947 | new_line = true; | |
5948 | ||
5949 | if (arg_all) | |
8559e61d | 5950 | ret = show_all(bus, &new_line, &ellipsized); |
8fcf784d | 5951 | } else { |
e3e0314b ZJS |
5952 | _cleanup_free_ char **patterns = NULL; |
5953 | char **name; | |
5954 | ||
e449de87 | 5955 | STRV_FOREACH(name, strv_skip(argv, 1)) { |
3dced37b | 5956 | _cleanup_free_ char *path = NULL, *unit = NULL; |
94e0bd7d | 5957 | uint32_t id; |
48220598 | 5958 | |
94e0bd7d | 5959 | if (safe_atou32(*name, &id) < 0) { |
e3e0314b | 5960 | if (strv_push(&patterns, *name) < 0) |
94e0bd7d | 5961 | return log_oom(); |
48220598 | 5962 | |
e3e0314b | 5963 | continue; |
8559e61d | 5964 | } else if (show_mode == SYSTEMCTL_SHOW_PROPERTIES) { |
94e0bd7d | 5965 | /* Interpret as job id */ |
3dced37b | 5966 | if (asprintf(&path, "/org/freedesktop/systemd1/job/%u", id) < 0) |
94e0bd7d | 5967 | return log_oom(); |
48220598 | 5968 | |
94e0bd7d ZJS |
5969 | } else { |
5970 | /* Interpret as PID */ | |
3dced37b | 5971 | r = get_unit_dbus_path_by_pid(bus, id, &path); |
373d32c9 | 5972 | if (r < 0) { |
94e0bd7d | 5973 | ret = r; |
373d32c9 LP |
5974 | continue; |
5975 | } | |
3dced37b LP |
5976 | |
5977 | r = unit_name_from_dbus_path(path, &unit); | |
5978 | if (r < 0) | |
5979 | return log_oom(); | |
94e0bd7d | 5980 | } |
f74294c1 | 5981 | |
8559e61d | 5982 | r = show_one(bus, path, unit, show_mode, &new_line, &ellipsized); |
5bb75bc7 ZJS |
5983 | if (r < 0) |
5984 | return r; | |
5985 | else if (r > 0 && ret == 0) | |
5986 | ret = r; | |
48220598 | 5987 | } |
94e0bd7d | 5988 | |
e3e0314b ZJS |
5989 | if (!strv_isempty(patterns)) { |
5990 | _cleanup_strv_free_ char **names = NULL; | |
5991 | ||
5992 | r = expand_names(bus, patterns, NULL, &names); | |
5993 | if (r < 0) | |
691395d8 | 5994 | return log_error_errno(r, "Failed to expand names: %m"); |
e3e0314b ZJS |
5995 | |
5996 | STRV_FOREACH(name, names) { | |
3dced37b | 5997 | _cleanup_free_ char *path; |
e3e0314b | 5998 | |
3dced37b LP |
5999 | path = unit_dbus_path_from_name(*name); |
6000 | if (!path) | |
e3e0314b ZJS |
6001 | return log_oom(); |
6002 | ||
8559e61d | 6003 | r = show_one(bus, path, *name, show_mode, &new_line, &ellipsized); |
5bb75bc7 ZJS |
6004 | if (r < 0) |
6005 | return r; | |
3dced37b | 6006 | if (r > 0 && ret == 0) |
5bb75bc7 | 6007 | ret = r; |
e3e0314b ZJS |
6008 | } |
6009 | } | |
6010 | } | |
6011 | ||
94e0bd7d ZJS |
6012 | if (ellipsized && !arg_quiet) |
6013 | printf("Hint: Some lines were ellipsized, use -l to show in full.\n"); | |
48220598 | 6014 | |
22f4096c | 6015 | return ret; |
0183528f LP |
6016 | } |
6017 | ||
e449de87 | 6018 | static int cat(int argc, char *argv[], void *userdata) { |
8e766630 | 6019 | _cleanup_(lookup_paths_free) LookupPaths lp = {}; |
15ef1144 LP |
6020 | _cleanup_strv_free_ char **names = NULL; |
6021 | char **name; | |
4fbd7192 LP |
6022 | sd_bus *bus; |
6023 | bool first = true; | |
25586912 | 6024 | int r; |
15ef1144 | 6025 | |
4eb53120 FS |
6026 | /* Include all units by default - i.e. continue as if the --all |
6027 | * option was used */ | |
6028 | if (strv_isempty(arg_states)) | |
6029 | arg_all = true; | |
6030 | ||
2f50e59f LP |
6031 | if (arg_transport != BUS_TRANSPORT_LOCAL) |
6032 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot remotely cat units."); | |
3e495a66 | 6033 | |
4943d143 | 6034 | r = lookup_paths_init(&lp, arg_scope, 0, arg_root); |
ad2a0358 | 6035 | if (r < 0) |
c51932be | 6036 | return log_error_errno(r, "Failed to determine unit paths: %m"); |
ad2a0358 | 6037 | |
4fbd7192 | 6038 | r = acquire_bus(BUS_MANAGER, &bus); |
15ef1144 | 6039 | if (r < 0) |
4fbd7192 | 6040 | return r; |
15ef1144 | 6041 | |
e449de87 | 6042 | r = expand_names(bus, strv_skip(argv, 1), NULL, &names); |
4fbd7192 LP |
6043 | if (r < 0) |
6044 | return log_error_errno(r, "Failed to expand names: %m"); | |
ad2a0358 | 6045 | |
0221d68a | 6046 | (void) pager_open(arg_pager_flags); |
15ef1144 LP |
6047 | |
6048 | STRV_FOREACH(name, names) { | |
ad2a0358 | 6049 | _cleanup_free_ char *fragment_path = NULL; |
15ef1144 | 6050 | _cleanup_strv_free_ char **dropin_paths = NULL; |
15ef1144 | 6051 | |
02a126a3 | 6052 | r = unit_find_paths(bus, *name, &lp, false, &fragment_path, &dropin_paths); |
86f004fb | 6053 | if (r == -ERFKILL) { |
02a126a3 LP |
6054 | printf("%s# Unit %s is masked%s.\n", |
6055 | ansi_highlight_magenta(), | |
6056 | *name, | |
6057 | ansi_normal()); | |
6058 | continue; | |
6059 | } | |
6060 | if (r == -EKEYREJECTED) { | |
6061 | printf("%s# Unit %s could not be loaded.%s\n", | |
86f004fb ZJS |
6062 | ansi_highlight_magenta(), |
6063 | *name, | |
6064 | ansi_normal()); | |
6065 | continue; | |
6066 | } | |
ad2a0358 ZJS |
6067 | if (r < 0) |
6068 | return r; | |
02a126a3 | 6069 | if (r == 0) |
b5e6a600 | 6070 | return -ENOENT; |
15ef1144 LP |
6071 | |
6072 | if (first) | |
6073 | first = false; | |
6074 | else | |
6075 | puts(""); | |
6076 | ||
5b9635d1 | 6077 | if (need_daemon_reload(bus, *name) > 0) /* ignore errors (<0), this is informational output */ |
e100155d LW |
6078 | fprintf(stderr, |
6079 | "%s# Warning: %s changed on disk, the version systemd has loaded is outdated.\n" | |
6080 | "%s# This output shows the current version of the unit's original fragment and drop-in files.\n" | |
6081 | "%s# If fragments or drop-ins were added or removed, they are not properly reflected in this output.\n" | |
6082 | "%s# Run 'systemctl%s daemon-reload' to reload units.%s\n", | |
6083 | ansi_highlight_red(), | |
6084 | *name, | |
6085 | ansi_highlight_red(), | |
6086 | ansi_highlight_red(), | |
6087 | ansi_highlight_red(), | |
6088 | arg_scope == UNIT_FILE_SYSTEM ? "" : " --user", | |
6089 | ansi_normal()); | |
6090 | ||
854a42fb | 6091 | r = cat_files(fragment_path, dropin_paths, 0); |
81f5e513 ZJS |
6092 | if (r < 0) |
6093 | return r; | |
15ef1144 LP |
6094 | } |
6095 | ||
25586912 | 6096 | return 0; |
15ef1144 LP |
6097 | } |
6098 | ||
e449de87 | 6099 | static int set_property(int argc, char *argv[], void *userdata) { |
4afd3348 LP |
6100 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
6101 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
68372da6 | 6102 | _cleanup_free_ char *n = NULL; |
89ada3ba | 6103 | UnitType t; |
4fbd7192 | 6104 | sd_bus *bus; |
8e2af478 LP |
6105 | int r; |
6106 | ||
4fbd7192 LP |
6107 | r = acquire_bus(BUS_MANAGER, &bus); |
6108 | if (r < 0) | |
6109 | return r; | |
6110 | ||
8a4b13c5 | 6111 | polkit_agent_open_maybe(); |
d2ad7e1b | 6112 | |
f459b602 MAP |
6113 | r = sd_bus_message_new_method_call( |
6114 | bus, | |
151b9b96 | 6115 | &m, |
8e2af478 LP |
6116 | "org.freedesktop.systemd1", |
6117 | "/org/freedesktop/systemd1", | |
6118 | "org.freedesktop.systemd1.Manager", | |
151b9b96 | 6119 | "SetUnitProperties"); |
f459b602 MAP |
6120 | if (r < 0) |
6121 | return bus_log_create_error(r); | |
8e2af478 | 6122 | |
37cbc1d5 | 6123 | r = unit_name_mangle(argv[1], arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN, &n); |
7410616c LP |
6124 | if (r < 0) |
6125 | return log_error_errno(r, "Failed to mangle unit name: %m"); | |
68372da6 | 6126 | |
89ada3ba | 6127 | t = unit_name_to_type(n); |
2f50e59f LP |
6128 | if (t < 0) |
6129 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid unit type: %s", n); | |
89ada3ba | 6130 | |
f459b602 MAP |
6131 | r = sd_bus_message_append(m, "sb", n, arg_runtime); |
6132 | if (r < 0) | |
6133 | return bus_log_create_error(r); | |
8e2af478 | 6134 | |
f459b602 MAP |
6135 | r = sd_bus_message_open_container(m, SD_BUS_TYPE_ARRAY, "(sv)"); |
6136 | if (r < 0) | |
6137 | return bus_log_create_error(r); | |
8e2af478 | 6138 | |
89ada3ba | 6139 | r = bus_append_unit_property_assignment_many(m, t, strv_skip(argv, 2)); |
8673cf13 LP |
6140 | if (r < 0) |
6141 | return r; | |
8e2af478 | 6142 | |
f459b602 MAP |
6143 | r = sd_bus_message_close_container(m); |
6144 | if (r < 0) | |
6145 | return bus_log_create_error(r); | |
8e2af478 | 6146 | |
c49b30a2 | 6147 | r = sd_bus_call(bus, m, 0, &error, NULL); |
691395d8 LP |
6148 | if (r < 0) |
6149 | return log_error_errno(r, "Failed to set unit properties on %s: %s", n, bus_error_message(&error, r)); | |
8e2af478 LP |
6150 | |
6151 | return 0; | |
6152 | } | |
6153 | ||
e449de87 | 6154 | static int daemon_reload(int argc, char *argv[], void *userdata) { |
4afd3348 | 6155 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
2853b60a | 6156 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
7e4249b9 | 6157 | const char *method; |
4fbd7192 | 6158 | sd_bus *bus; |
f459b602 | 6159 | int r; |
7e4249b9 | 6160 | |
4fbd7192 LP |
6161 | r = acquire_bus(BUS_MANAGER, &bus); |
6162 | if (r < 0) | |
6163 | return r; | |
6164 | ||
8a4b13c5 | 6165 | polkit_agent_open_maybe(); |
d2ad7e1b | 6166 | |
2853b60a LP |
6167 | switch (arg_action) { |
6168 | ||
6169 | case ACTION_RELOAD: | |
e4b61340 | 6170 | method = "Reload"; |
2853b60a LP |
6171 | break; |
6172 | ||
6173 | case ACTION_REEXEC: | |
e4b61340 | 6174 | method = "Reexecute"; |
2853b60a LP |
6175 | break; |
6176 | ||
6177 | case ACTION_SYSTEMCTL: | |
6178 | method = streq(argv[0], "daemon-reexec") ? "Reexecute" : | |
6179 | /* "daemon-reload" */ "Reload"; | |
6180 | break; | |
e4b61340 | 6181 | |
2853b60a LP |
6182 | default: |
6183 | assert_not_reached("Unexpected action"); | |
e4b61340 | 6184 | } |
7e4249b9 | 6185 | |
2853b60a LP |
6186 | r = sd_bus_message_new_method_call( |
6187 | bus, | |
6188 | &m, | |
6189 | "org.freedesktop.systemd1", | |
6190 | "/org/freedesktop/systemd1", | |
6191 | "org.freedesktop.systemd1.Manager", | |
6192 | method); | |
6193 | if (r < 0) | |
6194 | return bus_log_create_error(r); | |
6195 | ||
6196 | /* Note we use an extra-long timeout here. This is because a reload or reexec means generators are rerun which | |
6197 | * are timed out after DEFAULT_TIMEOUT_USEC. Let's use twice that time here, so that the generators can have | |
6198 | * their timeout, and for everything else there's the same time budget in place. */ | |
6199 | ||
6200 | r = sd_bus_call(bus, m, DEFAULT_TIMEOUT_USEC * 2, &error, NULL); | |
6201 | ||
6202 | /* On reexecution, we expect a disconnect, not a reply */ | |
6203 | if (IN_SET(r, -ETIMEDOUT, -ECONNRESET) && streq(method, "Reexecute")) | |
6204 | r = 0; | |
6205 | ||
6206 | if (r < 0 && arg_action == ACTION_SYSTEMCTL) | |
6207 | return log_error_errno(r, "Failed to reload daemon: %s", bus_error_message(&error, r)); | |
6208 | ||
6209 | /* Note that for the legacy commands (i.e. those with action != ACTION_SYSTEMCTL) we support fallbacks to the | |
6210 | * old ways of doing things, hence don't log any error in that case here. */ | |
6211 | ||
6212 | return r < 0 ? r : 0; | |
6213 | } | |
6214 | ||
6215 | static int trivial_method(int argc, char *argv[], void *userdata) { | |
6216 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
6217 | const char *method; | |
6218 | sd_bus *bus; | |
6219 | int r; | |
6220 | ||
1ae17672 ZJS |
6221 | if (arg_dry_run) |
6222 | return 0; | |
6223 | ||
2853b60a LP |
6224 | r = acquire_bus(BUS_MANAGER, &bus); |
6225 | if (r < 0) | |
6226 | return r; | |
6227 | ||
8a4b13c5 | 6228 | polkit_agent_open_maybe(); |
d2ad7e1b | 6229 | |
2853b60a LP |
6230 | method = |
6231 | streq(argv[0], "clear-jobs") || | |
6232 | streq(argv[0], "cancel") ? "ClearJobs" : | |
6233 | streq(argv[0], "reset-failed") ? "ResetFailed" : | |
6234 | streq(argv[0], "halt") ? "Halt" : | |
6235 | streq(argv[0], "reboot") ? "Reboot" : | |
6236 | streq(argv[0], "kexec") ? "KExec" : | |
6237 | streq(argv[0], "exit") ? "Exit" : | |
6238 | /* poweroff */ "PowerOff"; | |
6239 | ||
6e646d22 | 6240 | r = sd_bus_call_method( |
f22f08cd SP |
6241 | bus, |
6242 | "org.freedesktop.systemd1", | |
6243 | "/org/freedesktop/systemd1", | |
6244 | "org.freedesktop.systemd1.Manager", | |
6e646d22 LP |
6245 | method, |
6246 | &error, | |
6247 | NULL, | |
6248 | NULL); | |
2853b60a LP |
6249 | if (r < 0 && arg_action == ACTION_SYSTEMCTL) |
6250 | return log_error_errno(r, "Failed to execute operation: %s", bus_error_message(&error, r)); | |
6251 | ||
6252 | /* Note that for the legacy commands (i.e. those with action != ACTION_SYSTEMCTL) we support fallbacks to the | |
6253 | * old ways of doing things, hence don't log any error in that case here. */ | |
7e4249b9 | 6254 | |
0a9776c2 | 6255 | return r < 0 ? r : 0; |
7e4249b9 LP |
6256 | } |
6257 | ||
e449de87 | 6258 | static int reset_failed(int argc, char *argv[], void *userdata) { |
e3e0314b | 6259 | _cleanup_strv_free_ char **names = NULL; |
4fbd7192 | 6260 | sd_bus *bus; |
f84190d8 | 6261 | char **name; |
e3e0314b | 6262 | int r, q; |
5632e374 | 6263 | |
e449de87 | 6264 | if (argc <= 1) |
2853b60a | 6265 | return trivial_method(argc, argv, userdata); |
5632e374 | 6266 | |
4fbd7192 LP |
6267 | r = acquire_bus(BUS_MANAGER, &bus); |
6268 | if (r < 0) | |
6269 | return r; | |
6270 | ||
8a4b13c5 | 6271 | polkit_agent_open_maybe(); |
d2ad7e1b | 6272 | |
e449de87 | 6273 | r = expand_names(bus, strv_skip(argv, 1), NULL, &names); |
e3e0314b | 6274 | if (r < 0) |
691395d8 | 6275 | return log_error_errno(r, "Failed to expand names: %m"); |
f84190d8 | 6276 | |
e3e0314b | 6277 | STRV_FOREACH(name, names) { |
4afd3348 | 6278 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
342641fb | 6279 | |
6e646d22 | 6280 | q = sd_bus_call_method( |
f22f08cd | 6281 | bus, |
b0193f1c LP |
6282 | "org.freedesktop.systemd1", |
6283 | "/org/freedesktop/systemd1", | |
6284 | "org.freedesktop.systemd1.Manager", | |
6e646d22 LP |
6285 | "ResetFailedUnit", |
6286 | &error, | |
6287 | NULL, | |
6288 | "s", *name); | |
e3e0314b | 6289 | if (q < 0) { |
691395d8 | 6290 | log_error_errno(q, "Failed to reset failed state of unit %s: %s", *name, bus_error_message(&error, q)); |
e3e0314b ZJS |
6291 | if (r == 0) |
6292 | r = q; | |
f459b602 | 6293 | } |
5632e374 LP |
6294 | } |
6295 | ||
e3e0314b | 6296 | return r; |
5632e374 LP |
6297 | } |
6298 | ||
804ee07c ZJS |
6299 | static int print_variable(const char *s) { |
6300 | const char *sep; | |
6301 | _cleanup_free_ char *esc = NULL; | |
6302 | ||
6303 | sep = strchr(s, '='); | |
baaa35ad ZJS |
6304 | if (!sep) |
6305 | return log_error_errno(SYNTHETIC_ERRNO(EUCLEAN), | |
6306 | "Invalid environment block"); | |
804ee07c ZJS |
6307 | |
6308 | esc = shell_maybe_quote(sep + 1, ESCAPE_POSIX); | |
6309 | if (!esc) | |
6310 | return log_oom(); | |
6311 | ||
6312 | printf("%.*s=%s\n", (int)(sep-s), s, esc); | |
6313 | return 0; | |
6314 | } | |
6315 | ||
e449de87 | 6316 | static int show_environment(int argc, char *argv[], void *userdata) { |
4afd3348 LP |
6317 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
6318 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
f459b602 | 6319 | const char *text; |
4fbd7192 | 6320 | sd_bus *bus; |
7e4249b9 | 6321 | int r; |
7e4249b9 | 6322 | |
4fbd7192 LP |
6323 | r = acquire_bus(BUS_MANAGER, &bus); |
6324 | if (r < 0) | |
6325 | return r; | |
6326 | ||
0221d68a | 6327 | (void) pager_open(arg_pager_flags); |
d2ad7e1b | 6328 | |
f459b602 | 6329 | r = sd_bus_get_property( |
f22f08cd SP |
6330 | bus, |
6331 | "org.freedesktop.systemd1", | |
6332 | "/org/freedesktop/systemd1", | |
f459b602 MAP |
6333 | "org.freedesktop.systemd1.Manager", |
6334 | "Environment", | |
6335 | &error, | |
f22f08cd | 6336 | &reply, |
f459b602 | 6337 | "as"); |
691395d8 LP |
6338 | if (r < 0) |
6339 | return log_error_errno(r, "Failed to get environment: %s", bus_error_message(&error, r)); | |
7e4249b9 | 6340 | |
f459b602 MAP |
6341 | r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "s"); |
6342 | if (r < 0) | |
6343 | return bus_log_parse_error(r); | |
7e4249b9 | 6344 | |
804ee07c ZJS |
6345 | while ((r = sd_bus_message_read_basic(reply, SD_BUS_TYPE_STRING, &text)) > 0) { |
6346 | r = print_variable(text); | |
6347 | if (r < 0) | |
6348 | return r; | |
6349 | } | |
f459b602 MAP |
6350 | if (r < 0) |
6351 | return bus_log_parse_error(r); | |
7e4249b9 | 6352 | |
f459b602 MAP |
6353 | r = sd_bus_message_exit_container(reply); |
6354 | if (r < 0) | |
6355 | return bus_log_parse_error(r); | |
7e4249b9 | 6356 | |
f84190d8 | 6357 | return 0; |
7e4249b9 LP |
6358 | } |
6359 | ||
e449de87 | 6360 | static int switch_root(int argc, char *argv[], void *userdata) { |
4afd3348 | 6361 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
f39d4a08 HH |
6362 | _cleanup_free_ char *cmdline_init = NULL; |
6363 | const char *root, *init; | |
4fbd7192 | 6364 | sd_bus *bus; |
f459b602 | 6365 | int r; |
957eb8ca | 6366 | |
2f50e59f LP |
6367 | if (arg_transport != BUS_TRANSPORT_LOCAL) |
6368 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot switch root remotely."); | |
4fbd7192 | 6369 | |
2f50e59f LP |
6370 | if (argc < 2 || argc > 3) |
6371 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Wrong number of arguments."); | |
957eb8ca | 6372 | |
e449de87 | 6373 | root = argv[1]; |
13068da8 | 6374 | |
e449de87 LP |
6375 | if (argc >= 3) |
6376 | init = argv[2]; | |
13068da8 | 6377 | else { |
01771226 | 6378 | r = proc_cmdline_get_key("init", 0, &cmdline_init); |
f39d4a08 | 6379 | if (r < 0) |
da927ba9 | 6380 | log_debug_errno(r, "Failed to parse /proc/cmdline: %m"); |
13068da8 | 6381 | |
f39d4a08 | 6382 | init = cmdline_init; |
13068da8 | 6383 | } |
f459b602 | 6384 | |
3c6f7c34 | 6385 | init = empty_to_null(init); |
f39d4a08 HH |
6386 | if (init) { |
6387 | const char *root_systemd_path = NULL, *root_init_path = NULL; | |
6388 | ||
270384b2 YW |
6389 | root_systemd_path = prefix_roota(root, "/" SYSTEMD_BINARY_PATH); |
6390 | root_init_path = prefix_roota(root, init); | |
f39d4a08 HH |
6391 | |
6392 | /* If the passed init is actually the same as the | |
6393 | * systemd binary, then let's suppress it. */ | |
e3f791a2 | 6394 | if (files_same(root_init_path, root_systemd_path, 0) > 0) |
f39d4a08 HH |
6395 | init = NULL; |
6396 | } | |
13068da8 | 6397 | |
d86e4c97 LP |
6398 | /* Instruct PID1 to exclude us from its killing spree applied during the transition. Otherwise we |
6399 | * would exit with a failure status even though the switch to the new root has succeed. */ | |
6400 | assert(saved_argv); | |
6401 | assert(saved_argv[0]); | |
6402 | saved_argv[0][0] = '@'; | |
acc28e2e | 6403 | |
4fbd7192 LP |
6404 | r = acquire_bus(BUS_MANAGER, &bus); |
6405 | if (r < 0) | |
6406 | return r; | |
6407 | ||
8a7a9cea ZJS |
6408 | /* If we are slow to exit after the root switch, the new systemd instance |
6409 | * will send us a signal to terminate. Just ignore it and exit normally. | |
6410 | * This way the unit does not end up as failed. | |
6411 | */ | |
6412 | r = ignore_signals(SIGTERM, -1); | |
6413 | if (r < 0) | |
6414 | log_warning_errno(r, "Failed to change disposition of SIGTERM to ignore: %m"); | |
6415 | ||
f39d4a08 | 6416 | log_debug("Switching root - root: %s; init: %s", root, strna(init)); |
957eb8ca | 6417 | |
f459b602 | 6418 | r = sd_bus_call_method( |
f22f08cd | 6419 | bus, |
957eb8ca LP |
6420 | "org.freedesktop.systemd1", |
6421 | "/org/freedesktop/systemd1", | |
6422 | "org.freedesktop.systemd1.Manager", | |
f22f08cd | 6423 | "SwitchRoot", |
f459b602 | 6424 | &error, |
f22f08cd | 6425 | NULL, |
f459b602 | 6426 | "ss", root, init); |
8a7a9cea ZJS |
6427 | if (r < 0) { |
6428 | (void) default_signals(SIGTERM, -1); | |
6429 | ||
691395d8 | 6430 | return log_error_errno(r, "Failed to switch root: %s", bus_error_message(&error, r)); |
8a7a9cea | 6431 | } |
f459b602 MAP |
6432 | |
6433 | return 0; | |
957eb8ca LP |
6434 | } |
6435 | ||
e449de87 | 6436 | static int set_environment(int argc, char *argv[], void *userdata) { |
4afd3348 LP |
6437 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
6438 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; | |
7e4249b9 | 6439 | const char *method; |
4fbd7192 | 6440 | sd_bus *bus; |
31e767f7 LP |
6441 | int r; |
6442 | ||
e449de87 LP |
6443 | assert(argc > 1); |
6444 | assert(argv); | |
7e4249b9 | 6445 | |
4fbd7192 LP |
6446 | r = acquire_bus(BUS_MANAGER, &bus); |
6447 | if (r < 0) | |
6448 | return r; | |
6449 | ||
8a4b13c5 | 6450 | polkit_agent_open_maybe(); |
d2ad7e1b | 6451 | |
e449de87 | 6452 | method = streq(argv[0], "set-environment") |
7e4249b9 LP |
6453 | ? "SetEnvironment" |
6454 | : "UnsetEnvironment"; | |
6455 | ||
f459b602 MAP |
6456 | r = sd_bus_message_new_method_call( |
6457 | bus, | |
151b9b96 | 6458 | &m, |
31e767f7 LP |
6459 | "org.freedesktop.systemd1", |
6460 | "/org/freedesktop/systemd1", | |
6461 | "org.freedesktop.systemd1.Manager", | |
151b9b96 | 6462 | method); |
f459b602 MAP |
6463 | if (r < 0) |
6464 | return bus_log_create_error(r); | |
7e4249b9 | 6465 | |
e449de87 | 6466 | r = sd_bus_message_append_strv(m, strv_skip(argv, 1)); |
31e767f7 | 6467 | if (r < 0) |
f459b602 | 6468 | return bus_log_create_error(r); |
7e4249b9 | 6469 | |
c49b30a2 | 6470 | r = sd_bus_call(bus, m, 0, &error, NULL); |
691395d8 LP |
6471 | if (r < 0) |
6472 | return log_error_errno(r, "Failed to set environment: %s", bus_error_message(&error, r)); | |
7e4249b9 | 6473 | |
f84190d8 | 6474 | return 0; |
7e4249b9 LP |
6475 | } |
6476 | ||
e449de87 | 6477 | static int import_environment(int argc, char *argv[], void *userdata) { |
4afd3348 LP |
6478 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
6479 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; | |
4fbd7192 | 6480 | sd_bus *bus; |
ac3efa8a LP |
6481 | int r; |
6482 | ||
4fbd7192 LP |
6483 | r = acquire_bus(BUS_MANAGER, &bus); |
6484 | if (r < 0) | |
6485 | return r; | |
6486 | ||
8a4b13c5 | 6487 | polkit_agent_open_maybe(); |
d2ad7e1b | 6488 | |
ac3efa8a LP |
6489 | r = sd_bus_message_new_method_call( |
6490 | bus, | |
151b9b96 | 6491 | &m, |
ac3efa8a LP |
6492 | "org.freedesktop.systemd1", |
6493 | "/org/freedesktop/systemd1", | |
6494 | "org.freedesktop.systemd1.Manager", | |
151b9b96 | 6495 | "SetEnvironment"); |
ac3efa8a LP |
6496 | if (r < 0) |
6497 | return bus_log_create_error(r); | |
6498 | ||
e449de87 | 6499 | if (argc < 2) |
ac3efa8a LP |
6500 | r = sd_bus_message_append_strv(m, environ); |
6501 | else { | |
6502 | char **a, **b; | |
6503 | ||
6504 | r = sd_bus_message_open_container(m, 'a', "s"); | |
6505 | if (r < 0) | |
6506 | return bus_log_create_error(r); | |
6507 | ||
e449de87 | 6508 | STRV_FOREACH(a, strv_skip(argv, 1)) { |
ac3efa8a | 6509 | |
2f50e59f LP |
6510 | if (!env_name_is_valid(*a)) |
6511 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Not a valid environment variable name: %s", *a); | |
ac3efa8a LP |
6512 | |
6513 | STRV_FOREACH(b, environ) { | |
6514 | const char *eq; | |
6515 | ||
6516 | eq = startswith(*b, *a); | |
6517 | if (eq && *eq == '=') { | |
6518 | ||
6519 | r = sd_bus_message_append(m, "s", *b); | |
6520 | if (r < 0) | |
6521 | return bus_log_create_error(r); | |
6522 | ||
6523 | break; | |
6524 | } | |
6525 | } | |
6526 | } | |
6527 | ||
6528 | r = sd_bus_message_close_container(m); | |
6529 | } | |
6530 | if (r < 0) | |
6531 | return bus_log_create_error(r); | |
6532 | ||
6533 | r = sd_bus_call(bus, m, 0, &error, NULL); | |
691395d8 LP |
6534 | if (r < 0) |
6535 | return log_error_errno(r, "Failed to import environment: %s", bus_error_message(&error, r)); | |
ac3efa8a LP |
6536 | |
6537 | return 0; | |
6538 | } | |
6539 | ||
cbb13b2a | 6540 | static int enable_sysv_units(const char *verb, char **args) { |
729e3769 | 6541 | int r = 0; |
ee5762e3 | 6542 | |
349cc4a5 | 6543 | #if HAVE_SYSV_COMPAT |
8e766630 | 6544 | _cleanup_(lookup_paths_free) LookupPaths paths = {}; |
e735decc LP |
6545 | unsigned f = 0; |
6546 | ||
6547 | /* Processes all SysV units, and reshuffles the array so that afterwards only the native units remain */ | |
ee5762e3 | 6548 | |
729e3769 LP |
6549 | if (arg_scope != UNIT_FILE_SYSTEM) |
6550 | return 0; | |
ee5762e3 | 6551 | |
b41b9d2a LP |
6552 | if (getenv_bool("SYSTEMCTL_SKIP_SYSV") > 0) |
6553 | return 0; | |
6554 | ||
4c315c2c IS |
6555 | if (!STR_IN_SET(verb, |
6556 | "enable", | |
6557 | "disable", | |
6558 | "is-enabled")) | |
729e3769 | 6559 | return 0; |
ee5762e3 | 6560 | |
4943d143 | 6561 | r = lookup_paths_init(&paths, arg_scope, LOOKUP_PATHS_EXCLUDE_GENERATED, arg_root); |
729e3769 LP |
6562 | if (r < 0) |
6563 | return r; | |
ee5762e3 | 6564 | |
729e3769 | 6565 | r = 0; |
a644abed | 6566 | while (args[f]) { |
e735decc LP |
6567 | |
6568 | const char *argv[] = { | |
6569 | ROOTLIBEXECDIR "/systemd-sysv-install", | |
3483460c LP |
6570 | NULL, /* --root= */ |
6571 | NULL, /* verb */ | |
6572 | NULL, /* service */ | |
e735decc LP |
6573 | NULL, |
6574 | }; | |
6575 | ||
8d1ee648 | 6576 | _cleanup_free_ char *p = NULL, *q = NULL, *l = NULL, *v = NULL; |
729e3769 | 6577 | bool found_native = false, found_sysv; |
e735decc | 6578 | const char *name; |
729e3769 | 6579 | unsigned c = 1; |
729e3769 | 6580 | pid_t pid; |
e735decc | 6581 | int j; |
ee5762e3 | 6582 | |
a644abed | 6583 | name = args[f++]; |
ee5762e3 | 6584 | |
729e3769 LP |
6585 | if (!endswith(name, ".service")) |
6586 | continue; | |
ee5762e3 | 6587 | |
729e3769 LP |
6588 | if (path_is_absolute(name)) |
6589 | continue; | |
ee5762e3 | 6590 | |
e735decc | 6591 | j = unit_file_exists(arg_scope, &paths, name); |
76ec966f | 6592 | if (j < 0 && !IN_SET(j, -ELOOP, -ERFKILL, -EADDRNOTAVAIL)) |
e735decc | 6593 | return log_error_errno(j, "Failed to lookup unit file state: %m"); |
3c6d8e57 | 6594 | found_native = j != 0; |
ee5762e3 | 6595 | |
e735decc LP |
6596 | /* If we have both a native unit and a SysV script, enable/disable them both (below); for is-enabled, |
6597 | * prefer the native unit */ | |
355ff449 | 6598 | if (found_native && streq(verb, "is-enabled")) |
729e3769 | 6599 | continue; |
ee5762e3 | 6600 | |
652ef298 | 6601 | p = path_join(arg_root, SYSTEM_SYSVINIT_PATH, name); |
0c6ea3a4 | 6602 | if (!p) |
60731f32 | 6603 | return log_oom(); |
ee5762e3 | 6604 | |
fbd0b64f | 6605 | p[strlen(p) - STRLEN(".service")] = 0; |
729e3769 | 6606 | found_sysv = access(p, F_OK) >= 0; |
4b6756a8 | 6607 | if (!found_sysv) |
729e3769 | 6608 | continue; |
71fad675 | 6609 | |
689e4e6a CR |
6610 | if (!arg_quiet) { |
6611 | if (found_native) | |
6612 | log_info("Synchronizing state of %s with SysV service script with %s.", name, argv[0]); | |
6613 | else | |
6614 | log_info("%s is not a native service, redirecting to systemd-sysv-install.", name); | |
6615 | } | |
ee5762e3 | 6616 | |
b1dffbb9 LP |
6617 | if (!isempty(arg_root)) { |
6618 | q = strappend("--root=", arg_root); | |
6619 | if (!q) | |
6620 | return log_oom(); | |
6621 | ||
6622 | argv[c++] = q; | |
6623 | } | |
ee5762e3 | 6624 | |
8d1ee648 LP |
6625 | /* Let's copy the verb, since it's still pointing directly into the original argv[] array we |
6626 | * got passed, but safe_fork() is likely going to rewrite that for the new child */ | |
6627 | v = strdup(verb); | |
6628 | if (!v) | |
6629 | return log_oom(); | |
6630 | ||
6631 | argv[c++] = v; | |
2b6bf07d | 6632 | argv[c++] = basename(p); |
729e3769 | 6633 | argv[c] = NULL; |
ee5762e3 | 6634 | |
729e3769 | 6635 | l = strv_join((char**)argv, " "); |
60731f32 ZJS |
6636 | if (!l) |
6637 | return log_oom(); | |
ee5762e3 | 6638 | |
8ecc68f4 LP |
6639 | if (!arg_quiet) |
6640 | log_info("Executing: %s", l); | |
ee5762e3 | 6641 | |
0672e2c6 | 6642 | j = safe_fork("(sysv-install)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG, &pid); |
4c253ed1 | 6643 | if (j < 0) |
b6e1fff1 | 6644 | return j; |
4c253ed1 | 6645 | if (j == 0) { |
729e3769 | 6646 | /* Child */ |
729e3769 | 6647 | execv(argv[0], (char**) argv); |
cc7cb0ca | 6648 | log_error_errno(errno, "Failed to execute %s: %m", argv[0]); |
729e3769 LP |
6649 | _exit(EXIT_FAILURE); |
6650 | } | |
ee5762e3 | 6651 | |
2e87a1fd | 6652 | j = wait_for_terminate_and_check("sysv-install", pid, WAIT_LOG_ABNORMAL); |
d710aaf7 | 6653 | if (j < 0) |
2e87a1fd LP |
6654 | return j; |
6655 | if (streq(verb, "is-enabled")) { | |
b4a34311 | 6656 | if (j == EXIT_SUCCESS) { |
2e87a1fd LP |
6657 | if (!arg_quiet) |
6658 | puts("enabled"); | |
6659 | r = 1; | |
6660 | } else { | |
6661 | if (!arg_quiet) | |
6662 | puts("disabled"); | |
6663 | } | |
ee5762e3 | 6664 | |
b4a34311 | 6665 | } else if (j != EXIT_SUCCESS) |
2e87a1fd | 6666 | return -EBADE; /* We don't warn here, under the assumption the script already showed an explanation */ |
ee5762e3 | 6667 | |
355ff449 MP |
6668 | if (found_native) |
6669 | continue; | |
6670 | ||
a644abed | 6671 | /* Remove this entry, so that we don't try enabling it as native unit */ |
aba84331 LP |
6672 | assert(f > 0); |
6673 | f--; | |
6674 | assert(args[f] == name); | |
290cb8e8 | 6675 | strv_remove(args + f, name); |
729e3769 | 6676 | } |
ee5762e3 | 6677 | |
729e3769 LP |
6678 | #endif |
6679 | return r; | |
6680 | } | |
ee5762e3 | 6681 | |
37370d0c | 6682 | static int mangle_names(char **original_names, char ***mangled_names) { |
a33fdebb | 6683 | char **i, **l, **name; |
7410616c | 6684 | int r; |
37370d0c | 6685 | |
7410616c | 6686 | l = i = new(char*, strv_length(original_names) + 1); |
a33fdebb | 6687 | if (!l) |
37370d0c VP |
6688 | return log_oom(); |
6689 | ||
37370d0c | 6690 | STRV_FOREACH(name, original_names) { |
44386fc1 LN |
6691 | |
6692 | /* When enabling units qualified path names are OK, | |
6693 | * too, hence allow them explicitly. */ | |
6694 | ||
7410616c | 6695 | if (is_path(*name)) { |
44386fc1 | 6696 | *i = strdup(*name); |
7410616c LP |
6697 | if (!*i) { |
6698 | strv_free(l); | |
6699 | return log_oom(); | |
6700 | } | |
6701 | } else { | |
37cbc1d5 | 6702 | r = unit_name_mangle(*name, arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN, i); |
7410616c | 6703 | if (r < 0) { |
523f8cde | 6704 | *i = NULL; |
7410616c LP |
6705 | strv_free(l); |
6706 | return log_error_errno(r, "Failed to mangle unit name: %m"); | |
6707 | } | |
a33fdebb LP |
6708 | } |
6709 | ||
6710 | i++; | |
37370d0c | 6711 | } |
a33fdebb LP |
6712 | |
6713 | *i = NULL; | |
6714 | *mangled_names = l; | |
37370d0c VP |
6715 | |
6716 | return 0; | |
6717 | } | |
6718 | ||
decd7982 JR |
6719 | static int normalize_filenames(char **names) { |
6720 | char **u; | |
6721 | int r; | |
6722 | ||
6723 | STRV_FOREACH(u, names) | |
6724 | if (!path_is_absolute(*u)) { | |
6725 | char* normalized_path; | |
6726 | ||
baaa35ad ZJS |
6727 | if (!isempty(arg_root)) |
6728 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
6729 | "Non-absolute paths are not allowed when --root is used: %s", | |
6730 | *u); | |
decd7982 | 6731 | |
baaa35ad ZJS |
6732 | if (!strchr(*u,'/')) |
6733 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
6734 | "Link argument does contain at least one directory separator: %s", | |
6735 | *u); | |
decd7982 JR |
6736 | |
6737 | r = path_make_absolute_cwd(*u, &normalized_path); | |
6738 | if (r < 0) | |
6739 | return r; | |
6740 | ||
6741 | free_and_replace(*u, normalized_path); | |
6742 | } | |
6743 | ||
6744 | return 0; | |
6745 | } | |
6746 | ||
1d3c86c0 MS |
6747 | static int normalize_names(char **names, bool warn_if_path) { |
6748 | char **u; | |
6749 | bool was_path = false; | |
6750 | ||
6751 | STRV_FOREACH(u, names) { | |
6752 | int r; | |
6753 | ||
6754 | if (!is_path(*u)) | |
6755 | continue; | |
6756 | ||
6757 | r = free_and_strdup(u, basename(*u)); | |
6758 | if (r < 0) | |
6759 | return log_error_errno(r, "Failed to normalize unit file path: %m"); | |
6760 | ||
6761 | was_path = true; | |
6762 | } | |
6763 | ||
6764 | if (warn_if_path && was_path) | |
6765 | log_warning("Warning: Can't execute disable on the unit file path. Proceeding with the unit name."); | |
6766 | ||
6767 | return 0; | |
6768 | } | |
6769 | ||
173471b7 | 6770 | static int unit_exists(LookupPaths *lp, const char *unit) { |
d6568222 | 6771 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
f37f8a61 | 6772 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
d6568222 SS |
6773 | _cleanup_free_ char *path = NULL; |
6774 | static const struct bus_properties_map property_map[] = { | |
f37f8a61 YW |
6775 | { "LoadState", "s", NULL, offsetof(UnitStatusInfo, load_state) }, |
6776 | { "ActiveState", "s", NULL, offsetof(UnitStatusInfo, active_state)}, | |
d6568222 SS |
6777 | {}, |
6778 | }; | |
6779 | UnitStatusInfo info = {}; | |
6780 | sd_bus *bus; | |
6781 | int r; | |
6782 | ||
173471b7 ZJS |
6783 | if (unit_name_is_valid(unit, UNIT_NAME_TEMPLATE)) |
6784 | return unit_find_template_path(unit, lp, NULL, NULL); | |
6785 | ||
d6568222 SS |
6786 | path = unit_dbus_path_from_name(unit); |
6787 | if (!path) | |
6788 | return log_oom(); | |
6789 | ||
6790 | r = acquire_bus(BUS_MANAGER, &bus); | |
6791 | if (r < 0) | |
6792 | return r; | |
6793 | ||
a7e4861c | 6794 | r = bus_map_all_properties(bus, "org.freedesktop.systemd1", path, property_map, 0, &error, &m, &info); |
d6568222 SS |
6795 | if (r < 0) |
6796 | return log_error_errno(r, "Failed to get properties: %s", bus_error_message(&error, r)); | |
6797 | ||
d6568222 SS |
6798 | return !streq_ptr(info.load_state, "not-found") || !streq_ptr(info.active_state, "inactive"); |
6799 | } | |
6800 | ||
e449de87 | 6801 | static int enable_unit(int argc, char *argv[], void *userdata) { |
e3e0314b | 6802 | _cleanup_strv_free_ char **names = NULL; |
e449de87 | 6803 | const char *verb = argv[0]; |
729e3769 | 6804 | UnitFileChange *changes = NULL; |
da6053d0 | 6805 | size_t n_changes = 0; |
729e3769 | 6806 | int carries_install_info = -1; |
35b132e8 | 6807 | bool ignore_carries_install_info = arg_quiet; |
729e3769 | 6808 | int r; |
ee5762e3 | 6809 | |
e449de87 | 6810 | if (!argv[1]) |
ab5919fa MS |
6811 | return 0; |
6812 | ||
e449de87 | 6813 | r = mangle_names(strv_skip(argv, 1), &names); |
3a05c0f9 | 6814 | if (r < 0) |
cbb13b2a VP |
6815 | return r; |
6816 | ||
e3e0314b | 6817 | r = enable_sysv_units(verb, names); |
cbb13b2a VP |
6818 | if (r < 0) |
6819 | return r; | |
3a05c0f9 | 6820 | |
c61b443d | 6821 | /* If the operation was fully executed by the SysV compat, let's finish early */ |
823e5fab FB |
6822 | if (strv_isempty(names)) { |
6823 | if (arg_no_reload || install_client_side()) | |
6824 | return 0; | |
6825 | return daemon_reload(argc, argv, userdata); | |
6826 | } | |
67d66210 | 6827 | |
1d3c86c0 MS |
6828 | if (streq(verb, "disable")) { |
6829 | r = normalize_names(names, true); | |
6830 | if (r < 0) | |
6831 | return r; | |
6832 | } | |
6833 | ||
decd7982 JR |
6834 | if (streq(verb, "link")) { |
6835 | r = normalize_filenames(names); | |
6836 | if (r < 0) | |
6837 | return r; | |
6838 | } | |
6839 | ||
4fbd7192 | 6840 | if (install_client_side()) { |
b3796dd8 JS |
6841 | UnitFileFlags flags; |
6842 | ||
6843 | flags = args_to_flags(); | |
729e3769 | 6844 | if (streq(verb, "enable")) { |
b3796dd8 | 6845 | r = unit_file_enable(arg_scope, flags, arg_root, names, &changes, &n_changes); |
729e3769 LP |
6846 | carries_install_info = r; |
6847 | } else if (streq(verb, "disable")) | |
b3796dd8 | 6848 | r = unit_file_disable(arg_scope, flags, arg_root, names, &changes, &n_changes); |
729e3769 | 6849 | else if (streq(verb, "reenable")) { |
b3796dd8 | 6850 | r = unit_file_reenable(arg_scope, flags, arg_root, names, &changes, &n_changes); |
729e3769 LP |
6851 | carries_install_info = r; |
6852 | } else if (streq(verb, "link")) | |
b3796dd8 | 6853 | r = unit_file_link(arg_scope, flags, arg_root, names, &changes, &n_changes); |
729e3769 | 6854 | else if (streq(verb, "preset")) { |
b3796dd8 | 6855 | r = unit_file_preset(arg_scope, flags, arg_root, names, arg_preset_mode, &changes, &n_changes); |
729e3769 | 6856 | } else if (streq(verb, "mask")) |
b3796dd8 | 6857 | r = unit_file_mask(arg_scope, flags, arg_root, names, &changes, &n_changes); |
729e3769 | 6858 | else if (streq(verb, "unmask")) |
b3796dd8 | 6859 | r = unit_file_unmask(arg_scope, flags, arg_root, names, &changes, &n_changes); |
344ca755 LP |
6860 | else if (streq(verb, "revert")) |
6861 | r = unit_file_revert(arg_scope, arg_root, names, &changes, &n_changes); | |
729e3769 LP |
6862 | else |
6863 | assert_not_reached("Unknown verb"); | |
ee5762e3 | 6864 | |
af3d8113 | 6865 | unit_file_dump_changes(r, verb, changes, n_changes, arg_quiet); |
d073dea0 | 6866 | if (r < 0) |
85b78539 | 6867 | goto finish; |
df77cdf0 | 6868 | r = 0; |
729e3769 | 6869 | } else { |
4afd3348 LP |
6870 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL, *m = NULL; |
6871 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
39207373 | 6872 | bool expect_carries_install_info = false; |
344ca755 | 6873 | bool send_runtime = true, send_force = true, send_preset_mode = false; |
718db961 | 6874 | const char *method; |
4fbd7192 | 6875 | sd_bus *bus; |
729e3769 | 6876 | |
d6568222 | 6877 | if (STR_IN_SET(verb, "mask", "unmask")) { |
f8d6cb48 | 6878 | char **name; |
8e766630 | 6879 | _cleanup_(lookup_paths_free) LookupPaths lp = {}; |
173471b7 ZJS |
6880 | |
6881 | r = lookup_paths_init(&lp, arg_scope, 0, arg_root); | |
d6568222 SS |
6882 | if (r < 0) |
6883 | return r; | |
f8d6cb48 ZJS |
6884 | |
6885 | STRV_FOREACH(name, names) { | |
173471b7 | 6886 | r = unit_exists(&lp, *name); |
f8d6cb48 ZJS |
6887 | if (r < 0) |
6888 | return r; | |
6889 | if (r == 0) | |
25c59b5d | 6890 | log_notice("Unit %s does not exist, proceeding anyway.", *name); |
f8d6cb48 | 6891 | } |
d6568222 SS |
6892 | } |
6893 | ||
4fbd7192 LP |
6894 | r = acquire_bus(BUS_MANAGER, &bus); |
6895 | if (r < 0) | |
6896 | return r; | |
6897 | ||
8a4b13c5 | 6898 | polkit_agent_open_maybe(); |
d2ad7e1b | 6899 | |
729e3769 LP |
6900 | if (streq(verb, "enable")) { |
6901 | method = "EnableUnitFiles"; | |
6902 | expect_carries_install_info = true; | |
6903 | } else if (streq(verb, "disable")) { | |
6904 | method = "DisableUnitFiles"; | |
6905 | send_force = false; | |
6906 | } else if (streq(verb, "reenable")) { | |
6907 | method = "ReenableUnitFiles"; | |
6908 | expect_carries_install_info = true; | |
6909 | } else if (streq(verb, "link")) | |
6910 | method = "LinkUnitFiles"; | |
6911 | else if (streq(verb, "preset")) { | |
d309c1c3 LP |
6912 | |
6913 | if (arg_preset_mode != UNIT_FILE_PRESET_FULL) { | |
6914 | method = "PresetUnitFilesWithMode"; | |
6915 | send_preset_mode = true; | |
6916 | } else | |
6917 | method = "PresetUnitFiles"; | |
6918 | ||
729e3769 | 6919 | expect_carries_install_info = true; |
39207373 | 6920 | ignore_carries_install_info = true; |
729e3769 LP |
6921 | } else if (streq(verb, "mask")) |
6922 | method = "MaskUnitFiles"; | |
6923 | else if (streq(verb, "unmask")) { | |
6924 | method = "UnmaskUnitFiles"; | |
6925 | send_force = false; | |
344ca755 LP |
6926 | } else if (streq(verb, "revert")) { |
6927 | method = "RevertUnitFiles"; | |
6928 | send_runtime = send_force = false; | |
729e3769 LP |
6929 | } else |
6930 | assert_not_reached("Unknown verb"); | |
6931 | ||
f459b602 MAP |
6932 | r = sd_bus_message_new_method_call( |
6933 | bus, | |
151b9b96 | 6934 | &m, |
729e3769 LP |
6935 | "org.freedesktop.systemd1", |
6936 | "/org/freedesktop/systemd1", | |
6937 | "org.freedesktop.systemd1.Manager", | |
151b9b96 | 6938 | method); |
f459b602 MAP |
6939 | if (r < 0) |
6940 | return bus_log_create_error(r); | |
ee5762e3 | 6941 | |
e3e0314b | 6942 | r = sd_bus_message_append_strv(m, names); |
f459b602 MAP |
6943 | if (r < 0) |
6944 | return bus_log_create_error(r); | |
ee5762e3 | 6945 | |
d309c1c3 LP |
6946 | if (send_preset_mode) { |
6947 | r = sd_bus_message_append(m, "s", unit_file_preset_mode_to_string(arg_preset_mode)); | |
6948 | if (r < 0) | |
6949 | return bus_log_create_error(r); | |
6950 | } | |
6951 | ||
344ca755 LP |
6952 | if (send_runtime) { |
6953 | r = sd_bus_message_append(m, "b", arg_runtime); | |
6954 | if (r < 0) | |
6955 | return bus_log_create_error(r); | |
6956 | } | |
ee5762e3 | 6957 | |
729e3769 | 6958 | if (send_force) { |
f459b602 MAP |
6959 | r = sd_bus_message_append(m, "b", arg_force); |
6960 | if (r < 0) | |
6961 | return bus_log_create_error(r); | |
ee5762e3 LP |
6962 | } |
6963 | ||
c49b30a2 | 6964 | r = sd_bus_call(bus, m, 0, &error, &reply); |
691395d8 | 6965 | if (r < 0) |
af3d8113 | 6966 | return log_error_errno(r, "Failed to %s unit: %s", verb, bus_error_message(&error, r)); |
be394c48 | 6967 | |
729e3769 | 6968 | if (expect_carries_install_info) { |
f459b602 MAP |
6969 | r = sd_bus_message_read(reply, "b", &carries_install_info); |
6970 | if (r < 0) | |
6971 | return bus_log_parse_error(r); | |
ee5762e3 LP |
6972 | } |
6973 | ||
57ab2eab | 6974 | r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet, &changes, &n_changes); |
f459b602 | 6975 | if (r < 0) |
85b78539 | 6976 | goto finish; |
b77398f7 | 6977 | |
93c941e3 | 6978 | /* Try to reload if enabled */ |
d6cb60c7 | 6979 | if (!arg_no_reload) |
e449de87 | 6980 | r = daemon_reload(argc, argv, userdata); |
f459b602 MAP |
6981 | else |
6982 | r = 0; | |
b647f10d | 6983 | } |
3d3961f2 | 6984 | |
39207373 | 6985 | if (carries_install_info == 0 && !ignore_carries_install_info) |
46efc978 LP |
6986 | log_notice("The unit files have no installation config (WantedBy=, RequiredBy=, Also=,\n" |
6987 | "Alias= settings in the [Install] section, and DefaultInstance= for template\n" | |
2811184a ZJS |
6988 | "units). This means they are not meant to be enabled using systemctl.\n" |
6989 | " \n" /* trick: the space is needed so that the line does not get stripped from output */ | |
46efc978 | 6990 | "Possible reasons for having this kind of units are:\n" |
18c93ee3 LP |
6991 | "%1$s A unit may be statically enabled by being symlinked from another unit's\n" |
6992 | " .wants/ or .requires/ directory.\n" | |
6993 | "%1$s A unit's purpose may be to act as a helper for some other unit which has\n" | |
6994 | " a requirement dependency on it.\n" | |
6995 | "%1$s A unit may be started when needed via activation (socket, path, timer,\n" | |
6996 | " D-Bus, udev, scripted systemctl call, ...).\n" | |
6997 | "%1$s In case of template units, the unit is meant to be enabled with some\n" | |
6998 | " instance name specified.", | |
9a6f746f | 6999 | special_glyph(SPECIAL_GLYPH_BULLET)); |
ee5762e3 | 7000 | |
6bc30691 | 7001 | if (arg_now && STR_IN_SET(argv[0], "enable", "disable", "mask")) { |
4fbd7192 | 7002 | sd_bus *bus; |
da6053d0 | 7003 | size_t len, i; |
57ab2eab | 7004 | |
4fbd7192 LP |
7005 | r = acquire_bus(BUS_MANAGER, &bus); |
7006 | if (r < 0) | |
d073dea0 | 7007 | goto finish; |
4fbd7192 | 7008 | |
6bc30691 JS |
7009 | len = strv_length(names); |
7010 | { | |
7011 | char *new_args[len + 2]; | |
57ab2eab | 7012 | |
6bc30691 JS |
7013 | new_args[0] = (char*) (streq(argv[0], "enable") ? "start" : "stop"); |
7014 | for (i = 0; i < len; i++) | |
7015 | new_args[i + 1] = basename(names[i]); | |
7016 | new_args[i + 1] = NULL; | |
7017 | ||
7018 | r = start_unit(len + 1, new_args, userdata); | |
7019 | } | |
57ab2eab JS |
7020 | } |
7021 | ||
729e3769 | 7022 | finish: |
729e3769 | 7023 | unit_file_changes_free(changes, n_changes); |
ee5762e3 | 7024 | |
729e3769 | 7025 | return r; |
ee5762e3 LP |
7026 | } |
7027 | ||
e449de87 | 7028 | static int add_dependency(int argc, char *argv[], void *userdata) { |
e94937df LN |
7029 | _cleanup_strv_free_ char **names = NULL; |
7030 | _cleanup_free_ char *target = NULL; | |
e449de87 | 7031 | const char *verb = argv[0]; |
acc0269c | 7032 | UnitFileChange *changes = NULL; |
da6053d0 | 7033 | size_t n_changes = 0; |
e94937df LN |
7034 | UnitDependency dep; |
7035 | int r = 0; | |
7036 | ||
e449de87 | 7037 | if (!argv[1]) |
e94937df LN |
7038 | return 0; |
7039 | ||
37cbc1d5 | 7040 | r = unit_name_mangle_with_suffix(argv[1], arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN, ".target", &target); |
7410616c LP |
7041 | if (r < 0) |
7042 | return log_error_errno(r, "Failed to mangle unit name: %m"); | |
e94937df | 7043 | |
e449de87 | 7044 | r = mangle_names(strv_skip(argv, 2), &names); |
e94937df LN |
7045 | if (r < 0) |
7046 | return r; | |
7047 | ||
7048 | if (streq(verb, "add-wants")) | |
7049 | dep = UNIT_WANTS; | |
7050 | else if (streq(verb, "add-requires")) | |
7051 | dep = UNIT_REQUIRES; | |
7052 | else | |
7053 | assert_not_reached("Unknown verb"); | |
7054 | ||
4fbd7192 | 7055 | if (install_client_side()) { |
b3796dd8 | 7056 | r = unit_file_add_dependency(arg_scope, args_to_flags(), arg_root, names, target, dep, &changes, &n_changes); |
af3d8113 | 7057 | unit_file_dump_changes(r, "add dependency on", changes, n_changes, arg_quiet); |
5f056378 CH |
7058 | |
7059 | if (r > 0) | |
7060 | r = 0; | |
e94937df | 7061 | } else { |
4afd3348 LP |
7062 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL, *m = NULL; |
7063 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
4fbd7192 | 7064 | sd_bus *bus; |
e94937df | 7065 | |
4fbd7192 LP |
7066 | r = acquire_bus(BUS_MANAGER, &bus); |
7067 | if (r < 0) | |
7068 | return r; | |
7069 | ||
8a4b13c5 | 7070 | polkit_agent_open_maybe(); |
d2ad7e1b | 7071 | |
e94937df LN |
7072 | r = sd_bus_message_new_method_call( |
7073 | bus, | |
7074 | &m, | |
7075 | "org.freedesktop.systemd1", | |
7076 | "/org/freedesktop/systemd1", | |
7077 | "org.freedesktop.systemd1.Manager", | |
7078 | "AddDependencyUnitFiles"); | |
7079 | if (r < 0) | |
7080 | return bus_log_create_error(r); | |
7081 | ||
342641fb | 7082 | r = sd_bus_message_append_strv(m, names); |
e94937df LN |
7083 | if (r < 0) |
7084 | return bus_log_create_error(r); | |
7085 | ||
342641fb | 7086 | r = sd_bus_message_append(m, "ssbb", target, unit_dependency_to_string(dep), arg_runtime, arg_force); |
e94937df LN |
7087 | if (r < 0) |
7088 | return bus_log_create_error(r); | |
7089 | ||
7090 | r = sd_bus_call(bus, m, 0, &error, &reply); | |
691395d8 | 7091 | if (r < 0) |
af3d8113 | 7092 | return log_error_errno(r, "Failed to add dependency: %s", bus_error_message(&error, r)); |
e94937df | 7093 | |
acc0269c | 7094 | r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet, &changes, &n_changes); |
e94937df | 7095 | if (r < 0) |
acc0269c | 7096 | goto finish; |
e94937df | 7097 | |
acc0269c CH |
7098 | if (arg_no_reload) { |
7099 | r = 0; | |
7100 | goto finish; | |
7101 | } | |
7102 | ||
7103 | r = daemon_reload(argc, argv, userdata); | |
e94937df | 7104 | } |
acc0269c CH |
7105 | |
7106 | finish: | |
7107 | unit_file_changes_free(changes, n_changes); | |
7108 | ||
7109 | return r; | |
e94937df LN |
7110 | } |
7111 | ||
e449de87 | 7112 | static int preset_all(int argc, char *argv[], void *userdata) { |
acc0269c | 7113 | UnitFileChange *changes = NULL; |
da6053d0 | 7114 | size_t n_changes = 0; |
d309c1c3 LP |
7115 | int r; |
7116 | ||
4fbd7192 | 7117 | if (install_client_side()) { |
b3796dd8 | 7118 | r = unit_file_preset_all(arg_scope, args_to_flags(), arg_root, arg_preset_mode, &changes, &n_changes); |
af3d8113 | 7119 | unit_file_dump_changes(r, "preset", changes, n_changes, arg_quiet); |
5f056378 CH |
7120 | |
7121 | if (r > 0) | |
7122 | r = 0; | |
d309c1c3 | 7123 | } else { |
4afd3348 LP |
7124 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
7125 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
4fbd7192 | 7126 | sd_bus *bus; |
d309c1c3 | 7127 | |
4fbd7192 LP |
7128 | r = acquire_bus(BUS_MANAGER, &bus); |
7129 | if (r < 0) | |
7130 | return r; | |
7131 | ||
8a4b13c5 | 7132 | polkit_agent_open_maybe(); |
d2ad7e1b | 7133 | |
6e646d22 | 7134 | r = sd_bus_call_method( |
d309c1c3 LP |
7135 | bus, |
7136 | "org.freedesktop.systemd1", | |
7137 | "/org/freedesktop/systemd1", | |
7138 | "org.freedesktop.systemd1.Manager", | |
6e646d22 LP |
7139 | "PresetAllUnitFiles", |
7140 | &error, | |
7141 | &reply, | |
d309c1c3 LP |
7142 | "sbb", |
7143 | unit_file_preset_mode_to_string(arg_preset_mode), | |
7144 | arg_runtime, | |
7145 | arg_force); | |
691395d8 | 7146 | if (r < 0) |
af3d8113 | 7147 | return log_error_errno(r, "Failed to preset all units: %s", bus_error_message(&error, r)); |
d309c1c3 | 7148 | |
acc0269c | 7149 | r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet, &changes, &n_changes); |
d309c1c3 | 7150 | if (r < 0) |
acc0269c | 7151 | goto finish; |
d309c1c3 | 7152 | |
acc0269c CH |
7153 | if (arg_no_reload) { |
7154 | r = 0; | |
7155 | goto finish; | |
7156 | } | |
7157 | ||
7158 | r = daemon_reload(argc, argv, userdata); | |
d309c1c3 | 7159 | } |
acc0269c CH |
7160 | |
7161 | finish: | |
7162 | unit_file_changes_free(changes, n_changes); | |
7163 | ||
7164 | return r; | |
d309c1c3 LP |
7165 | } |
7166 | ||
3b3557c4 JS |
7167 | static int show_installation_targets_client_side(const char *name) { |
7168 | UnitFileChange *changes = NULL; | |
da6053d0 | 7169 | size_t n_changes = 0, i; |
3b3557c4 JS |
7170 | UnitFileFlags flags; |
7171 | char **p; | |
7172 | int r; | |
7173 | ||
7174 | p = STRV_MAKE(name); | |
7175 | flags = UNIT_FILE_DRY_RUN | | |
7176 | (arg_runtime ? UNIT_FILE_RUNTIME : 0); | |
7177 | ||
7178 | r = unit_file_disable(UNIT_FILE_SYSTEM, flags, NULL, p, &changes, &n_changes); | |
7179 | if (r < 0) | |
7180 | return log_error_errno(r, "Failed to get file links for %s: %m", name); | |
7181 | ||
7182 | for (i = 0; i < n_changes; i++) | |
7183 | if (changes[i].type == UNIT_FILE_UNLINK) | |
7184 | printf(" %s\n", changes[i].path); | |
7185 | ||
7186 | return 0; | |
7187 | } | |
7188 | ||
7189 | static int show_installation_targets(sd_bus *bus, const char *name) { | |
7190 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
7191 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
7192 | const char *link; | |
7193 | int r; | |
7194 | ||
7195 | r = sd_bus_call_method( | |
7196 | bus, | |
7197 | "org.freedesktop.systemd1", | |
7198 | "/org/freedesktop/systemd1", | |
7199 | "org.freedesktop.systemd1.Manager", | |
7200 | "GetUnitFileLinks", | |
7201 | &error, | |
7202 | &reply, | |
7203 | "sb", name, arg_runtime); | |
7204 | if (r < 0) | |
7205 | return log_error_errno(r, "Failed to get unit file links for %s: %s", name, bus_error_message(&error, r)); | |
7206 | ||
7207 | r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "s"); | |
7208 | if (r < 0) | |
7209 | return bus_log_parse_error(r); | |
7210 | ||
7211 | while ((r = sd_bus_message_read(reply, "s", &link)) > 0) | |
7212 | printf(" %s\n", link); | |
7213 | ||
7214 | if (r < 0) | |
7215 | return bus_log_parse_error(r); | |
7216 | ||
7217 | r = sd_bus_message_exit_container(reply); | |
7218 | if (r < 0) | |
7219 | return bus_log_parse_error(r); | |
7220 | ||
7221 | return 0; | |
7222 | } | |
7223 | ||
e449de87 | 7224 | static int unit_is_enabled(int argc, char *argv[], void *userdata) { |
e3e0314b | 7225 | _cleanup_strv_free_ char **names = NULL; |
729e3769 LP |
7226 | bool enabled; |
7227 | char **name; | |
f459b602 | 7228 | int r; |
ee5762e3 | 7229 | |
e449de87 | 7230 | r = mangle_names(strv_skip(argv, 1), &names); |
cbb13b2a VP |
7231 | if (r < 0) |
7232 | return r; | |
7233 | ||
e449de87 | 7234 | r = enable_sysv_units(argv[0], names); |
729e3769 LP |
7235 | if (r < 0) |
7236 | return r; | |
ee5762e3 | 7237 | |
729e3769 | 7238 | enabled = r > 0; |
ee5762e3 | 7239 | |
4fbd7192 | 7240 | if (install_client_side()) { |
e3e0314b | 7241 | STRV_FOREACH(name, names) { |
729e3769 | 7242 | UnitFileState state; |
ee5762e3 | 7243 | |
0ec0deaa LP |
7244 | r = unit_file_get_state(arg_scope, arg_root, *name, &state); |
7245 | if (r < 0) | |
c5024cd0 | 7246 | return log_error_errno(r, "Failed to get unit file state for %s: %m", *name); |
ee5762e3 | 7247 | |
4c315c2c IS |
7248 | if (IN_SET(state, |
7249 | UNIT_FILE_ENABLED, | |
7250 | UNIT_FILE_ENABLED_RUNTIME, | |
7251 | UNIT_FILE_STATIC, | |
f4139308 LP |
7252 | UNIT_FILE_INDIRECT, |
7253 | UNIT_FILE_GENERATED)) | |
729e3769 LP |
7254 | enabled = true; |
7255 | ||
3b3557c4 | 7256 | if (!arg_quiet) { |
729e3769 | 7257 | puts(unit_file_state_to_string(state)); |
3b3557c4 JS |
7258 | if (arg_full) { |
7259 | r = show_installation_targets_client_side(*name); | |
7260 | if (r < 0) | |
7261 | return r; | |
7262 | } | |
7263 | } | |
71fad675 | 7264 | } |
ee5762e3 | 7265 | |
5f056378 | 7266 | r = 0; |
729e3769 | 7267 | } else { |
4afd3348 | 7268 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
4fbd7192 LP |
7269 | sd_bus *bus; |
7270 | ||
7271 | r = acquire_bus(BUS_MANAGER, &bus); | |
7272 | if (r < 0) | |
7273 | return r; | |
7274 | ||
e3e0314b | 7275 | STRV_FOREACH(name, names) { |
4afd3348 | 7276 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
729e3769 | 7277 | const char *s; |
63a723f3 | 7278 | |
f459b602 | 7279 | r = sd_bus_call_method( |
f22f08cd | 7280 | bus, |
729e3769 LP |
7281 | "org.freedesktop.systemd1", |
7282 | "/org/freedesktop/systemd1", | |
7283 | "org.freedesktop.systemd1.Manager", | |
f22f08cd | 7284 | "GetUnitFileState", |
f459b602 | 7285 | &error, |
f22f08cd | 7286 | &reply, |
04504f93 | 7287 | "s", *name); |
691395d8 LP |
7288 | if (r < 0) |
7289 | return log_error_errno(r, "Failed to get unit file state for %s: %s", *name, bus_error_message(&error, r)); | |
ee5762e3 | 7290 | |
f459b602 MAP |
7291 | r = sd_bus_message_read(reply, "s", &s); |
7292 | if (r < 0) | |
7293 | return bus_log_parse_error(r); | |
ee5762e3 | 7294 | |
f4139308 | 7295 | if (STR_IN_SET(s, "enabled", "enabled-runtime", "static", "indirect", "generated")) |
729e3769 LP |
7296 | enabled = true; |
7297 | ||
3b3557c4 | 7298 | if (!arg_quiet) { |
729e3769 | 7299 | puts(s); |
3b3557c4 JS |
7300 | if (arg_full) { |
7301 | r = show_installation_targets(bus, *name); | |
7302 | if (r < 0) | |
7303 | return r; | |
7304 | } | |
7305 | } | |
560d8f23 | 7306 | } |
ee5762e3 LP |
7307 | } |
7308 | ||
f4139308 | 7309 | return enabled ? EXIT_SUCCESS : EXIT_FAILURE; |
ee5762e3 LP |
7310 | } |
7311 | ||
adb6cd9b FB |
7312 | static int match_startup_finished(sd_bus_message *m, void *userdata, sd_bus_error *error) { |
7313 | char **state = userdata; | |
7314 | int r; | |
7315 | ||
7316 | assert(state); | |
7317 | ||
7318 | r = sd_bus_get_property_string( | |
7319 | sd_bus_message_get_bus(m), | |
7320 | "org.freedesktop.systemd1", | |
7321 | "/org/freedesktop/systemd1", | |
7322 | "org.freedesktop.systemd1.Manager", | |
7323 | "SystemState", | |
7324 | NULL, | |
7325 | state); | |
7326 | ||
7327 | sd_event_exit(sd_bus_get_event(sd_bus_message_get_bus(m)), r); | |
7328 | return 0; | |
7329 | } | |
7330 | ||
e449de87 | 7331 | static int is_system_running(int argc, char *argv[], void *userdata) { |
e686a616 | 7332 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
adb6cd9b FB |
7333 | _cleanup_(sd_bus_slot_unrefp) sd_bus_slot *slot_startup_finished = NULL; |
7334 | _cleanup_(sd_event_unrefp) sd_event* event = NULL; | |
99813a19 | 7335 | _cleanup_free_ char *state = NULL; |
4fbd7192 | 7336 | sd_bus *bus; |
99813a19 LP |
7337 | int r; |
7338 | ||
040524b4 | 7339 | if (running_in_chroot() > 0 || (arg_transport == BUS_TRANSPORT_LOCAL && !sd_booted())) { |
94f099d8 LP |
7340 | if (!arg_quiet) |
7341 | puts("offline"); | |
7342 | return EXIT_FAILURE; | |
7343 | } | |
7344 | ||
4fbd7192 LP |
7345 | r = acquire_bus(BUS_MANAGER, &bus); |
7346 | if (r < 0) | |
7347 | return r; | |
7348 | ||
adb6cd9b FB |
7349 | if (arg_wait) { |
7350 | r = sd_event_default(&event); | |
7351 | if (r >= 0) | |
7352 | r = sd_bus_attach_event(bus, event, 0); | |
7353 | if (r >= 0) | |
7354 | r = sd_bus_match_signal_async( | |
7355 | bus, | |
7356 | &slot_startup_finished, | |
7357 | "org.freedesktop.systemd1", | |
7358 | "/org/freedesktop/systemd1", | |
7359 | "org.freedesktop.systemd1.Manager", | |
7360 | "StartupFinished", | |
7361 | match_startup_finished, NULL, &state); | |
7362 | if (r < 0) { | |
7363 | log_warning_errno(r, "Failed to request match for StartupFinished: %m"); | |
7364 | arg_wait = false; | |
7365 | } | |
7366 | } | |
7367 | ||
99813a19 LP |
7368 | r = sd_bus_get_property_string( |
7369 | bus, | |
7370 | "org.freedesktop.systemd1", | |
7371 | "/org/freedesktop/systemd1", | |
7372 | "org.freedesktop.systemd1.Manager", | |
7373 | "SystemState", | |
e686a616 | 7374 | &error, |
99813a19 LP |
7375 | &state); |
7376 | if (r < 0) { | |
adb6cd9b | 7377 | log_warning_errno(r, "Failed to query system state: %s", bus_error_message(&error, r)); |
e686a616 | 7378 | |
99813a19 LP |
7379 | if (!arg_quiet) |
7380 | puts("unknown"); | |
bfeec178 | 7381 | return EXIT_FAILURE; |
99813a19 LP |
7382 | } |
7383 | ||
adb6cd9b FB |
7384 | if (arg_wait && STR_IN_SET(state, "initializing", "starting")) { |
7385 | r = sd_event_loop(event); | |
7386 | if (r < 0) { | |
7387 | log_warning_errno(r, "Failed to get property from event loop: %m"); | |
7388 | if (!arg_quiet) | |
7389 | puts("unknown"); | |
7390 | return EXIT_FAILURE; | |
7391 | } | |
7392 | } | |
7393 | ||
99813a19 LP |
7394 | if (!arg_quiet) |
7395 | puts(state); | |
7396 | ||
7397 | return streq(state, "running") ? EXIT_SUCCESS : EXIT_FAILURE; | |
7398 | } | |
7399 | ||
7d4fb3b1 | 7400 | static int create_edit_temp_file(const char *new_path, const char *original_path, char **ret_tmp_fn) { |
45519fd6 | 7401 | _cleanup_free_ char *t = NULL; |
ae6c3cc0 | 7402 | int r; |
7d4fb3b1 RC |
7403 | |
7404 | assert(new_path); | |
7405 | assert(original_path); | |
7406 | assert(ret_tmp_fn); | |
7407 | ||
14bcf25c | 7408 | r = tempfn_random(new_path, NULL, &t); |
ae6c3cc0 | 7409 | if (r < 0) |
029009d4 | 7410 | return log_error_errno(r, "Failed to determine temporary filename for \"%s\": %m", new_path); |
7d4fb3b1 RC |
7411 | |
7412 | r = mkdir_parents(new_path, 0755); | |
45519fd6 | 7413 | if (r < 0) |
691395d8 | 7414 | return log_error_errno(r, "Failed to create directories for \"%s\": %m", new_path); |
7d4fb3b1 | 7415 | |
8a016c74 | 7416 | r = copy_file(original_path, t, 0, 0644, 0, 0, COPY_REFLINK); |
7d4fb3b1 | 7417 | if (r == -ENOENT) { |
45519fd6 | 7418 | |
7d4fb3b1 | 7419 | r = touch(t); |
45519fd6 LP |
7420 | if (r < 0) |
7421 | return log_error_errno(r, "Failed to create temporary file \"%s\": %m", t); | |
7422 | ||
7423 | } else if (r < 0) | |
39c38ce1 | 7424 | return log_error_errno(r, "Failed to create temporary file for \"%s\": %m", new_path); |
7d4fb3b1 | 7425 | |
ae2a15bc | 7426 | *ret_tmp_fn = TAKE_PTR(t); |
7d4fb3b1 RC |
7427 | |
7428 | return 0; | |
7429 | } | |
7430 | ||
c51932be LP |
7431 | static int get_file_to_edit( |
7432 | const LookupPaths *paths, | |
7433 | const char *name, | |
7434 | char **ret_path) { | |
7435 | ||
7436 | _cleanup_free_ char *path = NULL, *run = NULL; | |
7d4fb3b1 | 7437 | |
45519fd6 LP |
7438 | assert(name); |
7439 | assert(ret_path); | |
7440 | ||
657ee2d8 | 7441 | path = path_join(paths->persistent_config, name); |
c51932be | 7442 | if (!path) |
7d4fb3b1 RC |
7443 | return log_oom(); |
7444 | ||
c51932be | 7445 | if (arg_runtime) { |
657ee2d8 | 7446 | run = path_join(paths->runtime_config, name); |
c51932be LP |
7447 | if (!run) |
7448 | return log_oom(); | |
7449 | } | |
7450 | ||
bc854dc7 | 7451 | if (arg_runtime) { |
baaa35ad ZJS |
7452 | if (access(path, F_OK) >= 0) |
7453 | return log_error_errno(SYNTHETIC_ERRNO(EEXIST), | |
7454 | "Refusing to create \"%s\" because it would be overridden by \"%s\" anyway.", | |
7455 | run, path); | |
691395d8 | 7456 | |
ae2a15bc LP |
7457 | *ret_path = TAKE_PTR(run); |
7458 | } else | |
7459 | *ret_path = TAKE_PTR(path); | |
7d4fb3b1 RC |
7460 | |
7461 | return 0; | |
7462 | } | |
7463 | ||
39c38ce1 | 7464 | static int unit_file_create_new( |
c51932be LP |
7465 | const LookupPaths *paths, |
7466 | const char *unit_name, | |
39c38ce1 | 7467 | const char *suffix, |
c51932be LP |
7468 | char **ret_new_path, |
7469 | char **ret_tmp_path) { | |
7470 | ||
919d2720 LP |
7471 | _cleanup_free_ char *new_path = NULL, *tmp_path = NULL; |
7472 | const char *ending; | |
7d4fb3b1 RC |
7473 | int r; |
7474 | ||
7475 | assert(unit_name); | |
7476 | assert(ret_new_path); | |
7477 | assert(ret_tmp_path); | |
7478 | ||
39c38ce1 | 7479 | ending = strjoina(unit_name, suffix); |
919d2720 | 7480 | r = get_file_to_edit(paths, ending, &new_path); |
7d4fb3b1 RC |
7481 | if (r < 0) |
7482 | return r; | |
7483 | ||
919d2720 LP |
7484 | r = create_edit_temp_file(new_path, new_path, &tmp_path); |
7485 | if (r < 0) | |
7d4fb3b1 | 7486 | return r; |
7d4fb3b1 | 7487 | |
919d2720 LP |
7488 | *ret_new_path = TAKE_PTR(new_path); |
7489 | *ret_tmp_path = TAKE_PTR(tmp_path); | |
7d4fb3b1 RC |
7490 | |
7491 | return 0; | |
7492 | } | |
7493 | ||
1cfa9a4c | 7494 | static int unit_file_create_copy( |
c51932be | 7495 | const LookupPaths *paths, |
1cfa9a4c LP |
7496 | const char *unit_name, |
7497 | const char *fragment_path, | |
1cfa9a4c LP |
7498 | char **ret_new_path, |
7499 | char **ret_tmp_path) { | |
7500 | ||
92d6b0bf | 7501 | _cleanup_free_ char *new_path = NULL, *tmp_path = NULL; |
7d4fb3b1 RC |
7502 | int r; |
7503 | ||
7504 | assert(fragment_path); | |
7505 | assert(unit_name); | |
7506 | assert(ret_new_path); | |
7507 | assert(ret_tmp_path); | |
7508 | ||
92d6b0bf | 7509 | r = get_file_to_edit(paths, unit_name, &new_path); |
7d4fb3b1 RC |
7510 | if (r < 0) |
7511 | return r; | |
7512 | ||
92d6b0bf | 7513 | if (!path_equal(fragment_path, new_path) && access(new_path, F_OK) >= 0) { |
7d4fb3b1 RC |
7514 | char response; |
7515 | ||
92d6b0bf LP |
7516 | r = ask_char(&response, "yn", "\"%s\" already exists. Overwrite with \"%s\"? [(y)es, (n)o] ", new_path, fragment_path); |
7517 | if (r < 0) | |
7d4fb3b1 | 7518 | return r; |
5daacba2 LP |
7519 | if (response != 'y') |
7520 | return log_warning_errno(SYNTHETIC_ERRNO(EKEYREJECTED), "%s skipped.", unit_name); | |
7d4fb3b1 RC |
7521 | } |
7522 | ||
92d6b0bf LP |
7523 | r = create_edit_temp_file(new_path, fragment_path, &tmp_path); |
7524 | if (r < 0) | |
7d4fb3b1 | 7525 | return r; |
7d4fb3b1 | 7526 | |
92d6b0bf LP |
7527 | *ret_new_path = TAKE_PTR(new_path); |
7528 | *ret_tmp_path = TAKE_PTR(tmp_path); | |
7d4fb3b1 RC |
7529 | |
7530 | return 0; | |
7531 | } | |
7532 | ||
7533 | static int run_editor(char **paths) { | |
7d4fb3b1 RC |
7534 | int r; |
7535 | ||
7536 | assert(paths); | |
7537 | ||
0672e2c6 | 7538 | r = safe_fork("(editor)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG|FORK_WAIT, NULL); |
4c253ed1 | 7539 | if (r < 0) |
b6e1fff1 | 7540 | return r; |
4c253ed1 | 7541 | if (r == 0) { |
b2238e38 | 7542 | char **editor_args = NULL, **tmp_path, **original_path; |
70d6e5bd | 7543 | size_t n_editor_args = 0, i = 1, argc; |
b2238e38 | 7544 | const char **args, *editor, *p; |
7d4fb3b1 RC |
7545 | |
7546 | argc = strv_length(paths)/2 + 1; | |
7d4fb3b1 RC |
7547 | |
7548 | /* SYSTEMD_EDITOR takes precedence over EDITOR which takes precedence over VISUAL | |
7549 | * If neither SYSTEMD_EDITOR nor EDITOR nor VISUAL are present, | |
7550 | * we try to execute well known editors | |
7551 | */ | |
7552 | editor = getenv("SYSTEMD_EDITOR"); | |
7553 | if (!editor) | |
7554 | editor = getenv("EDITOR"); | |
7555 | if (!editor) | |
7556 | editor = getenv("VISUAL"); | |
7557 | ||
7558 | if (!isempty(editor)) { | |
9ef5d8f2 JS |
7559 | editor_args = strv_split(editor, WHITESPACE); |
7560 | if (!editor_args) { | |
7561 | (void) log_oom(); | |
7562 | _exit(EXIT_FAILURE); | |
7563 | } | |
7564 | n_editor_args = strv_length(editor_args); | |
7565 | argc += n_editor_args - 1; | |
7566 | } | |
02a126a3 | 7567 | |
9ef5d8f2 JS |
7568 | args = newa(const char*, argc + 1); |
7569 | ||
7570 | if (n_editor_args > 0) { | |
7571 | args[0] = editor_args[0]; | |
7572 | for (; i < n_editor_args; i++) | |
7573 | args[i] = editor_args[i]; | |
7574 | } | |
7575 | ||
df79fdab LP |
7576 | STRV_FOREACH_PAIR(original_path, tmp_path, paths) |
7577 | args[i++] = *tmp_path; | |
9ef5d8f2 JS |
7578 | args[i] = NULL; |
7579 | ||
7580 | if (n_editor_args > 0) | |
7581 | execvp(args[0], (char* const*) args); | |
7d4fb3b1 | 7582 | |
9391a1c3 | 7583 | FOREACH_STRING(p, "editor", "nano", "vim", "vi") { |
1cfa9a4c LP |
7584 | args[0] = p; |
7585 | execvp(p, (char* const*) args); | |
7d4fb3b1 RC |
7586 | /* We do not fail if the editor doesn't exist |
7587 | * because we want to try each one of them before | |
7588 | * failing. | |
7589 | */ | |
7590 | if (errno != ENOENT) { | |
691395d8 | 7591 | log_error_errno(errno, "Failed to execute %s: %m", editor); |
7d4fb3b1 RC |
7592 | _exit(EXIT_FAILURE); |
7593 | } | |
7594 | } | |
7595 | ||
1cfa9a4c | 7596 | log_error("Cannot edit unit(s), no editor available. Please set either $SYSTEMD_EDITOR, $EDITOR or $VISUAL."); |
7d4fb3b1 RC |
7597 | _exit(EXIT_FAILURE); |
7598 | } | |
7599 | ||
45519fd6 | 7600 | return 0; |
7d4fb3b1 RC |
7601 | } |
7602 | ||
7603 | static int find_paths_to_edit(sd_bus *bus, char **names, char ***paths) { | |
8e766630 | 7604 | _cleanup_(lookup_paths_free) LookupPaths lp = {}; |
7d4fb3b1 RC |
7605 | char **name; |
7606 | int r; | |
7607 | ||
7608 | assert(names); | |
7609 | assert(paths); | |
7610 | ||
4943d143 | 7611 | r = lookup_paths_init(&lp, arg_scope, 0, arg_root); |
5b013a2f | 7612 | if (r < 0) |
8df18507 | 7613 | return r; |
7d4fb3b1 | 7614 | |
e9e310f8 | 7615 | STRV_FOREACH(name, names) { |
b74df547 ZJS |
7616 | _cleanup_free_ char *path = NULL, *new_path = NULL, *tmp_path = NULL, *tmp_name = NULL; |
7617 | const char *unit_name; | |
7d4fb3b1 | 7618 | |
02a126a3 LP |
7619 | r = unit_find_paths(bus, *name, &lp, false, &path, NULL); |
7620 | if (r == -EKEYREJECTED) { | |
7621 | /* If loading of the unit failed server side complete, then the server won't tell us the unit | |
7622 | * file path. In that case, find the file client side. */ | |
7623 | log_debug_errno(r, "Unit '%s' was not loaded correctly, retrying client-side.", *name); | |
7624 | r = unit_find_paths(bus, *name, &lp, true, &path, NULL); | |
7625 | } | |
7626 | if (r == -ERFKILL) | |
7627 | return log_error_errno(r, "Unit '%s' masked, cannot edit.", *name); | |
e9e310f8 RC |
7628 | if (r < 0) |
7629 | return r; | |
b3734d98 ZJS |
7630 | |
7631 | if (r == 0) { | |
7632 | assert(!path); | |
7633 | ||
7634 | if (!arg_force) { | |
85163756 LP |
7635 | log_info("Run 'systemctl edit%s --force --full %s' to create a new unit.", |
7636 | arg_scope == UNIT_FILE_GLOBAL ? " --global" : | |
7637 | arg_scope == UNIT_FILE_USER ? " --user" : "", | |
7638 | *name); | |
39c38ce1 DC |
7639 | return -ENOENT; |
7640 | } | |
39c38ce1 | 7641 | |
b3734d98 ZJS |
7642 | /* Create a new unit from scratch */ |
7643 | unit_name = *name; | |
7644 | r = unit_file_create_new(&lp, unit_name, | |
7645 | arg_full ? NULL : ".d/override.conf", | |
7646 | &new_path, &tmp_path); | |
7647 | } else { | |
7648 | assert(path); | |
b74df547 | 7649 | |
b3734d98 ZJS |
7650 | unit_name = basename(path); |
7651 | /* We follow unit aliases, but we need to propagate the instance */ | |
7652 | if (unit_name_is_valid(*name, UNIT_NAME_INSTANCE) && | |
7653 | unit_name_is_valid(unit_name, UNIT_NAME_TEMPLATE)) { | |
7654 | _cleanup_free_ char *instance = NULL; | |
b74df547 | 7655 | |
b3734d98 ZJS |
7656 | r = unit_name_to_instance(*name, &instance); |
7657 | if (r < 0) | |
7658 | return r; | |
b74df547 | 7659 | |
b3734d98 ZJS |
7660 | r = unit_name_replace_instance(unit_name, instance, &tmp_name); |
7661 | if (r < 0) | |
7662 | return r; | |
7663 | ||
7664 | unit_name = tmp_name; | |
7665 | } | |
b74df547 | 7666 | |
39c38ce1 | 7667 | if (arg_full) |
b74df547 | 7668 | r = unit_file_create_copy(&lp, unit_name, path, &new_path, &tmp_path); |
39c38ce1 | 7669 | else |
b74df547 | 7670 | r = unit_file_create_new(&lp, unit_name, ".d/override.conf", &new_path, &tmp_path); |
b3734d98 | 7671 | } |
e9e310f8 | 7672 | if (r < 0) |
ad2a0358 | 7673 | return r; |
7d4fb3b1 | 7674 | |
e9e310f8 RC |
7675 | r = strv_push_pair(paths, new_path, tmp_path); |
7676 | if (r < 0) | |
7677 | return log_oom(); | |
02a126a3 | 7678 | |
78df0edc | 7679 | new_path = tmp_path = NULL; |
7d4fb3b1 RC |
7680 | } |
7681 | ||
7682 | return 0; | |
7683 | } | |
7684 | ||
e449de87 | 7685 | static int edit(int argc, char *argv[], void *userdata) { |
86f004fb | 7686 | _cleanup_(lookup_paths_free) LookupPaths lp = {}; |
7d4fb3b1 RC |
7687 | _cleanup_strv_free_ char **names = NULL; |
7688 | _cleanup_strv_free_ char **paths = NULL; | |
7689 | char **original, **tmp; | |
4fbd7192 | 7690 | sd_bus *bus; |
7d4fb3b1 RC |
7691 | int r; |
7692 | ||
2f50e59f LP |
7693 | if (!on_tty()) |
7694 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot edit units if not on a tty."); | |
7d4fb3b1 | 7695 | |
2f50e59f LP |
7696 | if (arg_transport != BUS_TRANSPORT_LOCAL) |
7697 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot edit units remotely."); | |
7d4fb3b1 | 7698 | |
86f004fb ZJS |
7699 | r = lookup_paths_init(&lp, arg_scope, 0, arg_root); |
7700 | if (r < 0) | |
7701 | return log_error_errno(r, "Failed to determine unit paths: %m"); | |
7702 | ||
4fbd7192 LP |
7703 | r = acquire_bus(BUS_MANAGER, &bus); |
7704 | if (r < 0) | |
7705 | return r; | |
7706 | ||
e449de87 | 7707 | r = expand_names(bus, strv_skip(argv, 1), NULL, &names); |
7d4fb3b1 RC |
7708 | if (r < 0) |
7709 | return log_error_errno(r, "Failed to expand names: %m"); | |
7710 | ||
9d9dd746 | 7711 | STRV_FOREACH(tmp, names) { |
86f004fb | 7712 | r = unit_is_masked(bus, &lp, *tmp); |
9d9dd746 ZJS |
7713 | if (r < 0) |
7714 | return r; | |
2f50e59f LP |
7715 | if (r > 0) |
7716 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot edit %s: unit is masked.", *tmp); | |
9d9dd746 ZJS |
7717 | } |
7718 | ||
7d4fb3b1 RC |
7719 | r = find_paths_to_edit(bus, names, &paths); |
7720 | if (r < 0) | |
7721 | return r; | |
7722 | ||
b5e6a600 | 7723 | if (strv_isempty(paths)) |
7d4fb3b1 | 7724 | return -ENOENT; |
7d4fb3b1 RC |
7725 | |
7726 | r = run_editor(paths); | |
7727 | if (r < 0) | |
7728 | goto end; | |
7729 | ||
7730 | STRV_FOREACH_PAIR(original, tmp, paths) { | |
9d9dd746 ZJS |
7731 | /* If the temporary file is empty we ignore it. |
7732 | * This allows the user to cancel the modification. | |
7d4fb3b1 RC |
7733 | */ |
7734 | if (null_or_empty_path(*tmp)) { | |
45519fd6 | 7735 | log_warning("Editing \"%s\" canceled: temporary file is empty.", *original); |
7d4fb3b1 RC |
7736 | continue; |
7737 | } | |
45519fd6 | 7738 | |
7d4fb3b1 RC |
7739 | r = rename(*tmp, *original); |
7740 | if (r < 0) { | |
029009d4 | 7741 | r = log_error_errno(errno, "Failed to rename \"%s\" to \"%s\": %m", *tmp, *original); |
7d4fb3b1 RC |
7742 | goto end; |
7743 | } | |
7744 | } | |
7745 | ||
45519fd6 LP |
7746 | r = 0; |
7747 | ||
7748 | if (!arg_no_reload && !install_client_side()) | |
e449de87 | 7749 | r = daemon_reload(argc, argv, userdata); |
7d4fb3b1 RC |
7750 | |
7751 | end: | |
5f18271e | 7752 | STRV_FOREACH_PAIR(original, tmp, paths) { |
45519fd6 | 7753 | (void) unlink(*tmp); |
7d4fb3b1 | 7754 | |
5f18271e RC |
7755 | /* Removing empty dropin dirs */ |
7756 | if (!arg_full) { | |
043717f9 RC |
7757 | _cleanup_free_ char *dir; |
7758 | ||
7759 | dir = dirname_malloc(*original); | |
7760 | if (!dir) | |
7761 | return log_oom(); | |
7762 | ||
5f18271e RC |
7763 | /* no need to check if the dir is empty, rmdir |
7764 | * does nothing if it is not the case. | |
7765 | */ | |
7766 | (void) rmdir(dir); | |
7767 | } | |
7768 | } | |
7769 | ||
7d4fb3b1 RC |
7770 | return r; |
7771 | } | |
7772 | ||
37ec0fdd LP |
7773 | static int systemctl_help(void) { |
7774 | _cleanup_free_ char *link = NULL; | |
7775 | int r; | |
7776 | ||
0221d68a | 7777 | (void) pager_open(arg_pager_flags); |
729e3769 | 7778 | |
37ec0fdd LP |
7779 | r = terminal_urlify_man("systemctl", "1", &link); |
7780 | if (r < 0) | |
7781 | return log_oom(); | |
7782 | ||
8e543004 | 7783 | printf("%1$s [OPTIONS...] {COMMAND} ...\n\n" |
729e3769 | 7784 | "Query or send control commands to the systemd manager.\n\n" |
8a0867d6 LP |
7785 | " -h --help Show this help\n" |
7786 | " --version Show package version\n" | |
f459b602 MAP |
7787 | " --system Connect to system manager\n" |
7788 | " --user Connect to user service manager\n" | |
7789 | " -H --host=[USER@]HOST\n" | |
7790 | " Operate on remote host\n" | |
7791 | " -M --machine=CONTAINER\n" | |
7792 | " Operate on local container\n" | |
3fb90db2 ZJS |
7793 | " -t --type=TYPE List units of a particular type\n" |
7794 | " --state=STATE List units with particular LOAD or SUB or ACTIVE state\n" | |
1d52a931 | 7795 | " --failed Shorcut for --state=failed\n" |
8a0867d6 | 7796 | " -p --property=NAME Show only properties by this name\n" |
2cdbbc9a LP |
7797 | " -a --all Show all properties/all units currently in memory,\n" |
7798 | " including dead/empty ones. To list all units installed on\n" | |
7799 | " the system, use the 'list-unit-files' command instead.\n" | |
98a6e132 | 7800 | " -l --full Don't ellipsize unit names on output\n" |
1238ee09 | 7801 | " -r --recursive Show unit list of host and local containers\n" |
4dc5b821 LP |
7802 | " --reverse Show reverse dependencies with 'list-dependencies'\n" |
7803 | " --job-mode=MODE Specify how to deal with already queued jobs, when\n" | |
7804 | " queueing a new job\n" | |
85d9b598 LP |
7805 | " -T --show-transaction\n" |
7806 | " When enqueuing a unit job, show full transaction\n" | |
a521ae4a | 7807 | " --show-types When showing sockets, explicitly show their type\n" |
4f9a9105 | 7808 | " --value When showing properties, only print the value\n" |
b37844d3 LP |
7809 | " -i --ignore-inhibitors\n" |
7810 | " When shutting down or sleeping, ignore inhibitors\n" | |
a8f11321 LP |
7811 | " --kill-who=WHO Who to send signal to\n" |
7812 | " -s --signal=SIGNAL Which signal to send\n" | |
d81bc51f | 7813 | " --what=RESOURCES Which types of resources to remove\n" |
57ab2eab | 7814 | " --now Start or stop unit in addition to enabling or disabling it\n" |
1ae17672 | 7815 | " --dry-run Only print what would be done\n" |
8a0867d6 | 7816 | " -q --quiet Suppress output\n" |
93a08841 | 7817 | " --wait For (re)start, wait until service stopped again\n" |
adb6cd9b | 7818 | " For is-system-running, wait until startup is completed\n" |
8a0867d6 | 7819 | " --no-block Do not wait until operation finished\n" |
8a0867d6 | 7820 | " --no-wall Don't send wall message before halt/power-off/reboot\n" |
3fb90db2 | 7821 | " --no-reload Don't reload daemon after en-/dis-abling unit files\n" |
ebed32bf | 7822 | " --no-legend Do not print a legend (column headers and hints)\n" |
69fc152f | 7823 | " --no-pager Do not pipe output into a pager\n" |
501fc174 LP |
7824 | " --no-ask-password\n" |
7825 | " Do not ask for system passwords\n" | |
4e93d369 LR |
7826 | " --global Enable/disable/mask unit files globally\n" |
7827 | " --runtime Enable/disable/mask unit files temporarily until next\n" | |
7828 | " reboot\n" | |
8a0867d6 LP |
7829 | " -f --force When enabling unit files, override existing symlinks\n" |
7830 | " When shutting down, execute action immediately\n" | |
3fb90db2 | 7831 | " --preset-mode= Apply only enable, only disable, or all presets\n" |
4e93d369 LR |
7832 | " --root=PATH Enable/disable/mask unit files in the specified root\n" |
7833 | " directory\n" | |
76fdc966 | 7834 | " -n --lines=INTEGER Number of journal entries to show\n" |
584c6e70 | 7835 | " -o --output=STRING Change journal output mode (short, short-precise,\n" |
7e563bfc IW |
7836 | " short-iso, short-iso-precise, short-full,\n" |
7837 | " short-monotonic, short-unix,\n" | |
584c6e70 | 7838 | " verbose, export, json, json-pretty, json-sse, cat)\n" |
5bdf2243 | 7839 | " --firmware-setup Tell the firmware to show the setup menu on next boot\n" |
97af80c5 LP |
7840 | " --boot-loader-menu=TIME\n" |
7841 | " Boot into boot loader menu on next boot\n" | |
7842 | " --boot-loader-entry=NAME\n" | |
7843 | " Boot into a specific boot loader entry on next boot\n" | |
815ebc54 | 7844 | " --plain Print unit dependencies as a list instead of a tree\n\n" |
8e543004 | 7845 | "%3$sUnit Commands:%4$s\n" |
fbf3283b ZJS |
7846 | " list-units [PATTERN...] List units currently in memory\n" |
7847 | " list-sockets [PATTERN...] List socket units currently in memory,\n" | |
7848 | " ordered by address\n" | |
7849 | " list-timers [PATTERN...] List timer units currently in memory,\n" | |
7850 | " ordered by next elapse\n" | |
7851 | " start UNIT... Start (activate) one or more units\n" | |
7852 | " stop UNIT... Stop (deactivate) one or more units\n" | |
7853 | " reload UNIT... Reload one or more units\n" | |
7854 | " restart UNIT... Start or restart one or more units\n" | |
7855 | " try-restart UNIT... Restart one or more units if active\n" | |
7856 | " reload-or-restart UNIT... Reload one or more units if possible,\n" | |
7857 | " otherwise start or restart\n" | |
7858 | " try-reload-or-restart UNIT... If active, reload one or more units,\n" | |
7859 | " if supported, otherwise restart\n" | |
7860 | " isolate UNIT Start one unit and stop all others\n" | |
7861 | " kill UNIT... Send signal to processes of a unit\n" | |
d81bc51f LP |
7862 | " clean UNIT... Clean runtime, cache, state, logs or\n" |
7863 | " or configuration of unit\n" | |
fbf3283b ZJS |
7864 | " is-active PATTERN... Check whether units are active\n" |
7865 | " is-failed PATTERN... Check whether units are failed\n" | |
7866 | " status [PATTERN...|PID...] Show runtime status of one or more units\n" | |
7867 | " show [PATTERN...|JOB...] Show properties of one or more\n" | |
7868 | " units/jobs or the manager\n" | |
7869 | " cat PATTERN... Show files and drop-ins of specified units\n" | |
7870 | " set-property UNIT PROPERTY=VALUE... Sets one or more properties of a unit\n" | |
7871 | " help PATTERN...|PID... Show manual for one or more units\n" | |
7872 | " reset-failed [PATTERN...] Reset failed state for all, one, or more\n" | |
7873 | " units\n" | |
7874 | " list-dependencies [UNIT] Recursively show units which are required\n" | |
7875 | " or wanted by this unit or by which this\n" | |
7876 | " unit is required or wanted\n\n" | |
8e543004 | 7877 | "%3$sUnit File Commands:%4$s\n" |
fbf3283b ZJS |
7878 | " list-unit-files [PATTERN...] List installed unit files\n" |
7879 | " enable [UNIT...|PATH...] Enable one or more unit files\n" | |
7880 | " disable UNIT... Disable one or more unit files\n" | |
7881 | " reenable UNIT... Reenable one or more unit files\n" | |
7882 | " preset UNIT... Enable/disable one or more unit files\n" | |
7883 | " based on preset configuration\n" | |
7884 | " preset-all Enable/disable all unit files based on\n" | |
7885 | " preset configuration\n" | |
7886 | " is-enabled UNIT... Check whether unit files are enabled\n" | |
7887 | " mask UNIT... Mask one or more units\n" | |
7888 | " unmask UNIT... Unmask one or more units\n" | |
7889 | " link PATH... Link one or more units files into\n" | |
7890 | " the search path\n" | |
7891 | " revert UNIT... Revert one or more unit files to vendor\n" | |
7892 | " version\n" | |
7893 | " add-wants TARGET UNIT... Add 'Wants' dependency for the target\n" | |
7894 | " on specified one or more units\n" | |
7895 | " add-requires TARGET UNIT... Add 'Requires' dependency for the target\n" | |
7896 | " on specified one or more units\n" | |
7897 | " edit UNIT... Edit one or more unit files\n" | |
7898 | " get-default Get the name of the default target\n" | |
7899 | " set-default TARGET Set the default target\n\n" | |
8e543004 | 7900 | "%3$sMachine Commands:%4$s\n" |
fbf3283b | 7901 | " list-machines [PATTERN...] List local containers and host\n\n" |
8e543004 | 7902 | "%3$sJob Commands:%4$s\n" |
fbf3283b ZJS |
7903 | " list-jobs [PATTERN...] List jobs\n" |
7904 | " cancel [JOB...] Cancel all, one, or more jobs\n\n" | |
8e543004 | 7905 | "%3$sEnvironment Commands:%4$s\n" |
fbf3283b | 7906 | " show-environment Dump environment\n" |
135775c1 ZJS |
7907 | " set-environment VARIABLE=VALUE... Set one or more environment variables\n" |
7908 | " unset-environment VARIABLE... Unset one or more environment variables\n" | |
7909 | " import-environment [VARIABLE...] Import all or some environment variables\n\n" | |
8e543004 | 7910 | "%3$sManager Lifecycle Commands:%4$s\n" |
fbf3283b ZJS |
7911 | " daemon-reload Reload systemd manager configuration\n" |
7912 | " daemon-reexec Reexecute systemd manager\n\n" | |
8e543004 | 7913 | "%3$sSystem Commands:%4$s\n" |
fbf3283b ZJS |
7914 | " is-system-running Check whether system is fully running\n" |
7915 | " default Enter system default mode\n" | |
7916 | " rescue Enter system rescue mode\n" | |
7917 | " emergency Enter system emergency mode\n" | |
7918 | " halt Shut down and halt the system\n" | |
7919 | " poweroff Shut down and power-off the system\n" | |
7920 | " reboot [ARG] Shut down and reboot the system\n" | |
7921 | " kexec Shut down and reboot the system with kexec\n" | |
7922 | " exit [EXIT_CODE] Request user instance or container exit\n" | |
7923 | " switch-root ROOT [INIT] Change to a different root file system\n" | |
7924 | " suspend Suspend the system\n" | |
7925 | " hibernate Hibernate the system\n" | |
c58493c0 | 7926 | " hybrid-sleep Hibernate and suspend the system\n" |
e68c79db | 7927 | " suspend-then-hibernate Suspend the system, wake after a period of\n" |
37ec0fdd | 7928 | " time and put it into hibernate\n" |
8e543004 | 7929 | "\nSee the %2$s for details.\n" |
37ec0fdd LP |
7930 | , program_invocation_short_name |
7931 | , link | |
8e543004 | 7932 | , ansi_underline(), ansi_normal() |
37ec0fdd LP |
7933 | ); |
7934 | ||
7935 | return 0; | |
7e4249b9 LP |
7936 | } |
7937 | ||
37ec0fdd LP |
7938 | static int halt_help(void) { |
7939 | _cleanup_free_ char *link = NULL; | |
7940 | int r; | |
7941 | ||
7942 | r = terminal_urlify_man("halt", "8", &link); | |
7943 | if (r < 0) | |
7944 | return log_oom(); | |
7945 | ||
37185ec8 | 7946 | printf("%s [OPTIONS...]%s\n\n" |
e4b61340 LP |
7947 | "%s the system.\n\n" |
7948 | " --help Show this help\n" | |
7949 | " --halt Halt the machine\n" | |
7950 | " -p --poweroff Switch off the machine\n" | |
7951 | " --reboot Reboot the machine\n" | |
2e33c433 LP |
7952 | " -f --force Force immediate halt/power-off/reboot\n" |
7953 | " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n" | |
e4b61340 | 7954 | " -d --no-wtmp Don't write wtmp record\n" |
37ec0fdd LP |
7955 | " --no-wall Don't send wall message before halt/power-off/reboot\n" |
7956 | "\nSee the %s for details.\n" | |
7957 | , program_invocation_short_name | |
7958 | , arg_action == ACTION_REBOOT ? " [ARG]" : "", | |
7959 | arg_action == ACTION_REBOOT ? "Reboot" : | |
7960 | arg_action == ACTION_POWEROFF ? "Power off" : | |
7961 | "Halt" | |
7962 | , link | |
7963 | ); | |
7964 | ||
7965 | return 0; | |
e4b61340 LP |
7966 | } |
7967 | ||
37ec0fdd LP |
7968 | static int shutdown_help(void) { |
7969 | _cleanup_free_ char *link = NULL; | |
7970 | int r; | |
7971 | ||
7972 | r = terminal_urlify_man("shutdown", "8", &link); | |
7973 | if (r < 0) | |
7974 | return log_oom(); | |
7975 | ||
08e4b1c5 | 7976 | printf("%s [OPTIONS...] [TIME] [WALL...]\n\n" |
e4b61340 LP |
7977 | "Shut down the system.\n\n" |
7978 | " --help Show this help\n" | |
7979 | " -H --halt Halt the machine\n" | |
7980 | " -P --poweroff Power-off the machine\n" | |
7981 | " -r --reboot Reboot the machine\n" | |
386da858 | 7982 | " -h Equivalent to --poweroff, overridden by --halt\n" |
2e33c433 | 7983 | " -k Don't halt/power-off/reboot, just send warnings\n" |
f6144808 | 7984 | " --no-wall Don't send wall message before halt/power-off/reboot\n" |
37ec0fdd LP |
7985 | " -c Cancel a pending shutdown\n" |
7986 | "\nSee the %s for details.\n" | |
7987 | , program_invocation_short_name | |
7988 | , link | |
7989 | ); | |
7990 | ||
7991 | return 0; | |
e4b61340 LP |
7992 | } |
7993 | ||
37ec0fdd LP |
7994 | static int telinit_help(void) { |
7995 | _cleanup_free_ char *link = NULL; | |
7996 | int r; | |
7997 | ||
7998 | r = terminal_urlify_man("telinit", "8", &link); | |
7999 | if (r < 0) | |
8000 | return log_oom(); | |
8001 | ||
2e33c433 | 8002 | printf("%s [OPTIONS...] {COMMAND}\n\n" |
514f4ef5 LP |
8003 | "Send control commands to the init daemon.\n\n" |
8004 | " --help Show this help\n" | |
2e33c433 | 8005 | " --no-wall Don't send wall message before halt/power-off/reboot\n\n" |
e4b61340 LP |
8006 | "Commands:\n" |
8007 | " 0 Power-off the machine\n" | |
8008 | " 6 Reboot the machine\n" | |
514f4ef5 LP |
8009 | " 2, 3, 4, 5 Start runlevelX.target unit\n" |
8010 | " 1, s, S Enter rescue mode\n" | |
8011 | " q, Q Reload init daemon configuration\n" | |
37ec0fdd LP |
8012 | " u, U Reexecute init daemon\n" |
8013 | "\nSee the %s for details.\n" | |
8014 | , program_invocation_short_name | |
8015 | , link | |
8016 | ); | |
8017 | ||
8018 | return 0; | |
e4b61340 LP |
8019 | } |
8020 | ||
37ec0fdd LP |
8021 | static int runlevel_help(void) { |
8022 | _cleanup_free_ char *link = NULL; | |
8023 | int r; | |
8024 | ||
8025 | r = terminal_urlify_man("runlevel", "8", &link); | |
8026 | if (r < 0) | |
8027 | return log_oom(); | |
8028 | ||
2e33c433 | 8029 | printf("%s [OPTIONS...]\n\n" |
e4b61340 | 8030 | "Prints the previous and current runlevel of the init system.\n\n" |
37ec0fdd LP |
8031 | " --help Show this help\n" |
8032 | "\nSee the %s for details.\n" | |
8033 | , program_invocation_short_name | |
8034 | , link | |
8035 | ); | |
8036 | ||
8037 | return 0; | |
e4b61340 LP |
8038 | } |
8039 | ||
b93312f5 | 8040 | static void help_types(void) { |
b93312f5 ZJS |
8041 | if (!arg_no_legend) |
8042 | puts("Available unit types:"); | |
5c828e66 LP |
8043 | |
8044 | DUMP_STRING_TABLE(unit_type, UnitType, _UNIT_TYPE_MAX); | |
e16972e6 ZJS |
8045 | } |
8046 | ||
8047 | static void help_states(void) { | |
e16972e6 ZJS |
8048 | if (!arg_no_legend) |
8049 | puts("Available unit load states:"); | |
5c828e66 | 8050 | DUMP_STRING_TABLE(unit_load_state, UnitLoadState, _UNIT_LOAD_STATE_MAX); |
e16972e6 ZJS |
8051 | |
8052 | if (!arg_no_legend) | |
8053 | puts("\nAvailable unit active states:"); | |
5c828e66 | 8054 | DUMP_STRING_TABLE(unit_active_state, UnitActiveState, _UNIT_ACTIVE_STATE_MAX); |
7e55de3b ZJS |
8055 | |
8056 | if (!arg_no_legend) | |
8057 | puts("\nAvailable automount unit substates:"); | |
5c828e66 | 8058 | DUMP_STRING_TABLE(automount_state, AutomountState, _AUTOMOUNT_STATE_MAX); |
7e55de3b | 8059 | |
7e55de3b ZJS |
8060 | if (!arg_no_legend) |
8061 | puts("\nAvailable device unit substates:"); | |
5c828e66 | 8062 | DUMP_STRING_TABLE(device_state, DeviceState, _DEVICE_STATE_MAX); |
7e55de3b ZJS |
8063 | |
8064 | if (!arg_no_legend) | |
8065 | puts("\nAvailable mount unit substates:"); | |
5c828e66 | 8066 | DUMP_STRING_TABLE(mount_state, MountState, _MOUNT_STATE_MAX); |
7e55de3b ZJS |
8067 | |
8068 | if (!arg_no_legend) | |
8069 | puts("\nAvailable path unit substates:"); | |
5c828e66 | 8070 | DUMP_STRING_TABLE(path_state, PathState, _PATH_STATE_MAX); |
7e55de3b ZJS |
8071 | |
8072 | if (!arg_no_legend) | |
8073 | puts("\nAvailable scope unit substates:"); | |
5c828e66 | 8074 | DUMP_STRING_TABLE(scope_state, ScopeState, _SCOPE_STATE_MAX); |
7e55de3b ZJS |
8075 | |
8076 | if (!arg_no_legend) | |
8077 | puts("\nAvailable service unit substates:"); | |
5c828e66 | 8078 | DUMP_STRING_TABLE(service_state, ServiceState, _SERVICE_STATE_MAX); |
7e55de3b ZJS |
8079 | |
8080 | if (!arg_no_legend) | |
8081 | puts("\nAvailable slice unit substates:"); | |
5c828e66 | 8082 | DUMP_STRING_TABLE(slice_state, SliceState, _SLICE_STATE_MAX); |
7e55de3b | 8083 | |
7e55de3b ZJS |
8084 | if (!arg_no_legend) |
8085 | puts("\nAvailable socket unit substates:"); | |
5c828e66 | 8086 | DUMP_STRING_TABLE(socket_state, SocketState, _SOCKET_STATE_MAX); |
7e55de3b ZJS |
8087 | |
8088 | if (!arg_no_legend) | |
8089 | puts("\nAvailable swap unit substates:"); | |
5c828e66 | 8090 | DUMP_STRING_TABLE(swap_state, SwapState, _SWAP_STATE_MAX); |
7e55de3b ZJS |
8091 | |
8092 | if (!arg_no_legend) | |
8093 | puts("\nAvailable target unit substates:"); | |
5c828e66 | 8094 | DUMP_STRING_TABLE(target_state, TargetState, _TARGET_STATE_MAX); |
7e55de3b ZJS |
8095 | |
8096 | if (!arg_no_legend) | |
8097 | puts("\nAvailable timer unit substates:"); | |
5c828e66 | 8098 | DUMP_STRING_TABLE(timer_state, TimerState, _TIMER_STATE_MAX); |
45c0c61d ZJS |
8099 | } |
8100 | ||
97af80c5 LP |
8101 | static int help_boot_loader_entry(void) { |
8102 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
8103 | _cleanup_free_ char **l = NULL; | |
8104 | sd_bus *bus; | |
8105 | char **i; | |
8106 | int r; | |
8107 | ||
8108 | r = acquire_bus(BUS_FULL, &bus); | |
8109 | if (r < 0) | |
8110 | return r; | |
8111 | ||
8112 | r = sd_bus_get_property_strv( | |
8113 | bus, | |
8114 | "org.freedesktop.login1", | |
8115 | "/org/freedesktop/login1", | |
8116 | "org.freedesktop.login1.Manager", | |
8117 | "BootLoaderEntries", | |
8118 | &error, | |
8119 | &l); | |
8120 | if (r < 0) | |
8121 | return log_error_errno(r, "Failed to enumerate boot loader entries: %s", bus_error_message(&error, r)); | |
8122 | ||
2f50e59f LP |
8123 | if (strv_isempty(l)) |
8124 | return log_error_errno(SYNTHETIC_ERRNO(ENODATA), "No boot loader entries discovered."); | |
97af80c5 LP |
8125 | |
8126 | STRV_FOREACH(i, l) | |
8127 | puts(*i); | |
8128 | ||
8129 | return 0; | |
8130 | } | |
8131 | ||
e4b61340 | 8132 | static int systemctl_parse_argv(int argc, char *argv[]) { |
7e4249b9 | 8133 | enum { |
90d473a1 | 8134 | ARG_FAIL = 0x100, |
afba4199 ZJS |
8135 | ARG_REVERSE, |
8136 | ARG_AFTER, | |
8137 | ARG_BEFORE, | |
1ae17672 | 8138 | ARG_DRY_RUN, |
991f2a39 | 8139 | ARG_SHOW_TYPES, |
23ade460 | 8140 | ARG_IRREVERSIBLE, |
e67c3609 | 8141 | ARG_IGNORE_DEPENDENCIES, |
4f9a9105 | 8142 | ARG_VALUE, |
35df8f27 | 8143 | ARG_VERSION, |
af2d49f7 | 8144 | ARG_USER, |
7e4249b9 | 8145 | ARG_SYSTEM, |
ee5762e3 | 8146 | ARG_GLOBAL, |
6e905d93 | 8147 | ARG_NO_BLOCK, |
ebed32bf | 8148 | ARG_NO_LEGEND, |
611efaac | 8149 | ARG_NO_PAGER, |
4445a875 | 8150 | ARG_NO_WALL, |
be394c48 | 8151 | ARG_ROOT, |
ee5762e3 | 8152 | ARG_NO_RELOAD, |
501fc174 | 8153 | ARG_KILL_WHO, |
30732560 | 8154 | ARG_NO_ASK_PASSWORD, |
729e3769 | 8155 | ARG_FAILED, |
df50185b | 8156 | ARG_RUNTIME, |
9b9b3d36 | 8157 | ARG_PLAIN, |
4dc5b821 | 8158 | ARG_STATE, |
d309c1c3 LP |
8159 | ARG_JOB_MODE, |
8160 | ARG_PRESET_MODE, | |
5bdf2243 | 8161 | ARG_FIRMWARE_SETUP, |
97af80c5 LP |
8162 | ARG_BOOT_LOADER_MENU, |
8163 | ARG_BOOT_LOADER_ENTRY, | |
57ab2eab | 8164 | ARG_NOW, |
9ef15026 | 8165 | ARG_MESSAGE, |
93a08841 | 8166 | ARG_WAIT, |
d81bc51f | 8167 | ARG_WHAT, |
7e4249b9 LP |
8168 | }; |
8169 | ||
8170 | static const struct option options[] = { | |
9ea9d4cf LP |
8171 | { "help", no_argument, NULL, 'h' }, |
8172 | { "version", no_argument, NULL, ARG_VERSION }, | |
8173 | { "type", required_argument, NULL, 't' }, | |
8174 | { "property", required_argument, NULL, 'p' }, | |
8175 | { "all", no_argument, NULL, 'a' }, | |
8176 | { "reverse", no_argument, NULL, ARG_REVERSE }, | |
8177 | { "after", no_argument, NULL, ARG_AFTER }, | |
8178 | { "before", no_argument, NULL, ARG_BEFORE }, | |
8179 | { "show-types", no_argument, NULL, ARG_SHOW_TYPES }, | |
8180 | { "failed", no_argument, NULL, ARG_FAILED }, /* compatibility only */ | |
8181 | { "full", no_argument, NULL, 'l' }, | |
4dc5b821 LP |
8182 | { "job-mode", required_argument, NULL, ARG_JOB_MODE }, |
8183 | { "fail", no_argument, NULL, ARG_FAIL }, /* compatibility only */ | |
8184 | { "irreversible", no_argument, NULL, ARG_IRREVERSIBLE }, /* compatibility only */ | |
8185 | { "ignore-dependencies", no_argument, NULL, ARG_IGNORE_DEPENDENCIES }, /* compatibility only */ | |
9ea9d4cf | 8186 | { "ignore-inhibitors", no_argument, NULL, 'i' }, |
4f9a9105 | 8187 | { "value", no_argument, NULL, ARG_VALUE }, |
9ea9d4cf LP |
8188 | { "user", no_argument, NULL, ARG_USER }, |
8189 | { "system", no_argument, NULL, ARG_SYSTEM }, | |
8190 | { "global", no_argument, NULL, ARG_GLOBAL }, | |
93a08841 | 8191 | { "wait", no_argument, NULL, ARG_WAIT }, |
9ea9d4cf LP |
8192 | { "no-block", no_argument, NULL, ARG_NO_BLOCK }, |
8193 | { "no-legend", no_argument, NULL, ARG_NO_LEGEND }, | |
8194 | { "no-pager", no_argument, NULL, ARG_NO_PAGER }, | |
8195 | { "no-wall", no_argument, NULL, ARG_NO_WALL }, | |
1ae17672 | 8196 | { "dry-run", no_argument, NULL, ARG_DRY_RUN }, |
9ea9d4cf LP |
8197 | { "quiet", no_argument, NULL, 'q' }, |
8198 | { "root", required_argument, NULL, ARG_ROOT }, | |
68da321f | 8199 | { "force", no_argument, NULL, 'f' }, |
9ea9d4cf LP |
8200 | { "no-reload", no_argument, NULL, ARG_NO_RELOAD }, |
8201 | { "kill-who", required_argument, NULL, ARG_KILL_WHO }, | |
8202 | { "signal", required_argument, NULL, 's' }, | |
8203 | { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD }, | |
8204 | { "host", required_argument, NULL, 'H' }, | |
f459b602 | 8205 | { "machine", required_argument, NULL, 'M' }, |
9ea9d4cf LP |
8206 | { "runtime", no_argument, NULL, ARG_RUNTIME }, |
8207 | { "lines", required_argument, NULL, 'n' }, | |
8208 | { "output", required_argument, NULL, 'o' }, | |
8209 | { "plain", no_argument, NULL, ARG_PLAIN }, | |
8210 | { "state", required_argument, NULL, ARG_STATE }, | |
1238ee09 | 8211 | { "recursive", no_argument, NULL, 'r' }, |
d309c1c3 | 8212 | { "preset-mode", required_argument, NULL, ARG_PRESET_MODE }, |
5bdf2243 | 8213 | { "firmware-setup", no_argument, NULL, ARG_FIRMWARE_SETUP }, |
97af80c5 LP |
8214 | { "boot-loader-menu", required_argument, NULL, ARG_BOOT_LOADER_MENU }, |
8215 | { "boot-loader-entry", required_argument, NULL, ARG_BOOT_LOADER_ENTRY }, | |
57ab2eab | 8216 | { "now", no_argument, NULL, ARG_NOW }, |
9ef15026 | 8217 | { "message", required_argument, NULL, ARG_MESSAGE }, |
85d9b598 | 8218 | { "show-transaction", no_argument, NULL, 'T' }, |
d81bc51f | 8219 | { "what", required_argument, NULL, ARG_WHAT }, |
eb9da376 | 8220 | {} |
7e4249b9 LP |
8221 | }; |
8222 | ||
0f03c2a4 | 8223 | int c, r; |
7e4249b9 | 8224 | |
e4b61340 | 8225 | assert(argc >= 0); |
7e4249b9 LP |
8226 | assert(argv); |
8227 | ||
16f017fa IS |
8228 | /* we default to allowing interactive authorization only in systemctl (not in the legacy commands) */ |
8229 | arg_ask_password = true; | |
8230 | ||
85d9b598 | 8231 | while ((c = getopt_long(argc, argv, "ht:p:alqfs:H:M:n:o:iTr.::", options, NULL)) >= 0) |
7e4249b9 LP |
8232 | |
8233 | switch (c) { | |
8234 | ||
8235 | case 'h': | |
37ec0fdd | 8236 | return systemctl_help(); |
35df8f27 LP |
8237 | |
8238 | case ARG_VERSION: | |
3f6fd1ba | 8239 | return version(); |
7e4249b9 | 8240 | |
20b3f379 | 8241 | case 't': { |
fb1ac50e LP |
8242 | const char *p; |
8243 | ||
baaa35ad ZJS |
8244 | if (isempty(optarg)) |
8245 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8246 | "--type= requires arguments."); | |
45c0c61d | 8247 | |
c58bd76a | 8248 | for (p = optarg;;) { |
5ab22f33 | 8249 | _cleanup_free_ char *type = NULL; |
20b3f379 | 8250 | |
5ab22f33 SS |
8251 | r = extract_first_word(&p, &type, ",", 0); |
8252 | if (r < 0) | |
8253 | return log_error_errno(r, "Failed to parse type: %s", optarg); | |
5ab22f33 SS |
8254 | if (r == 0) |
8255 | break; | |
20b3f379 ZJS |
8256 | |
8257 | if (streq(type, "help")) { | |
8258 | help_types(); | |
8259 | return 0; | |
8260 | } | |
8261 | ||
8262 | if (unit_type_from_string(type) >= 0) { | |
96e895ef | 8263 | if (strv_consume(&arg_types, TAKE_PTR(type)) < 0) |
20b3f379 | 8264 | return log_oom(); |
20b3f379 ZJS |
8265 | continue; |
8266 | } | |
8267 | ||
9b9b3d36 MW |
8268 | /* It's much nicer to use --state= for |
8269 | * load states, but let's support this | |
8270 | * in --types= too for compatibility | |
8271 | * with old versions */ | |
7e9d36e0 | 8272 | if (unit_load_state_from_string(type) >= 0) { |
96e895ef | 8273 | if (strv_consume(&arg_states, TAKE_PTR(type)) < 0) |
20b3f379 | 8274 | return log_oom(); |
20b3f379 ZJS |
8275 | continue; |
8276 | } | |
8277 | ||
ab06eef8 | 8278 | log_error("Unknown unit type or load state '%s'.", type); |
baaa35ad ZJS |
8279 | return log_info_errno(SYNTHETIC_ERRNO(EINVAL), |
8280 | "Use -t help to see a list of allowed values."); | |
c147dc42 | 8281 | } |
20b3f379 ZJS |
8282 | |
8283 | break; | |
8284 | } | |
8285 | ||
aee7082f LP |
8286 | case 'p': |
8287 | /* Make sure that if the empty property list was specified, we won't show any | |
8288 | properties. */ | |
20b3f379 | 8289 | if (isempty(optarg) && !arg_properties) { |
cbc9fbd1 | 8290 | arg_properties = new0(char*, 1); |
20b3f379 ZJS |
8291 | if (!arg_properties) |
8292 | return log_oom(); | |
fb1ac50e LP |
8293 | } else { |
8294 | const char *p; | |
8295 | ||
c58bd76a | 8296 | for (p = optarg;;) { |
5ab22f33 | 8297 | _cleanup_free_ char *prop = NULL; |
033a842c | 8298 | |
5ab22f33 SS |
8299 | r = extract_first_word(&p, &prop, ",", 0); |
8300 | if (r < 0) | |
8301 | return log_error_errno(r, "Failed to parse property: %s", optarg); | |
5ab22f33 SS |
8302 | if (r == 0) |
8303 | break; | |
ea4a240d | 8304 | |
9dccc6d3 | 8305 | if (strv_consume(&arg_properties, TAKE_PTR(prop)) < 0) |
20b3f379 | 8306 | return log_oom(); |
20b3f379 | 8307 | } |
fb1ac50e | 8308 | } |
48220598 LP |
8309 | |
8310 | /* If the user asked for a particular | |
8311 | * property, show it to him, even if it is | |
8312 | * empty. */ | |
8313 | arg_all = true; | |
033a842c | 8314 | |
48220598 LP |
8315 | break; |
8316 | ||
7e4249b9 LP |
8317 | case 'a': |
8318 | arg_all = true; | |
8319 | break; | |
8320 | ||
afba4199 ZJS |
8321 | case ARG_REVERSE: |
8322 | arg_dependency = DEPENDENCY_REVERSE; | |
8323 | break; | |
8324 | ||
8325 | case ARG_AFTER: | |
8326 | arg_dependency = DEPENDENCY_AFTER; | |
82948f6c | 8327 | arg_jobs_after = true; |
afba4199 ZJS |
8328 | break; |
8329 | ||
8330 | case ARG_BEFORE: | |
8331 | arg_dependency = DEPENDENCY_BEFORE; | |
82948f6c | 8332 | arg_jobs_before = true; |
afba4199 ZJS |
8333 | break; |
8334 | ||
991f2a39 ZJS |
8335 | case ARG_SHOW_TYPES: |
8336 | arg_show_types = true; | |
8337 | break; | |
8338 | ||
4f9a9105 ZJS |
8339 | case ARG_VALUE: |
8340 | arg_value = true; | |
8341 | break; | |
8342 | ||
4dc5b821 LP |
8343 | case ARG_JOB_MODE: |
8344 | arg_job_mode = optarg; | |
8345 | break; | |
8346 | ||
90d473a1 | 8347 | case ARG_FAIL: |
e67c3609 LP |
8348 | arg_job_mode = "fail"; |
8349 | break; | |
8350 | ||
23ade460 MS |
8351 | case ARG_IRREVERSIBLE: |
8352 | arg_job_mode = "replace-irreversibly"; | |
8353 | break; | |
8354 | ||
e67c3609 LP |
8355 | case ARG_IGNORE_DEPENDENCIES: |
8356 | arg_job_mode = "ignore-dependencies"; | |
7e4249b9 LP |
8357 | break; |
8358 | ||
af2d49f7 | 8359 | case ARG_USER: |
729e3769 | 8360 | arg_scope = UNIT_FILE_USER; |
7e4249b9 LP |
8361 | break; |
8362 | ||
8363 | case ARG_SYSTEM: | |
729e3769 LP |
8364 | arg_scope = UNIT_FILE_SYSTEM; |
8365 | break; | |
8366 | ||
8367 | case ARG_GLOBAL: | |
8368 | arg_scope = UNIT_FILE_GLOBAL; | |
7e4249b9 LP |
8369 | break; |
8370 | ||
93a08841 MP |
8371 | case ARG_WAIT: |
8372 | arg_wait = true; | |
8373 | break; | |
8374 | ||
6e905d93 LP |
8375 | case ARG_NO_BLOCK: |
8376 | arg_no_block = true; | |
7e4249b9 LP |
8377 | break; |
8378 | ||
ebed32bf MS |
8379 | case ARG_NO_LEGEND: |
8380 | arg_no_legend = true; | |
8381 | break; | |
8382 | ||
611efaac | 8383 | case ARG_NO_PAGER: |
0221d68a | 8384 | arg_pager_flags |= PAGER_DISABLE; |
611efaac | 8385 | break; |
0736af98 | 8386 | |
514f4ef5 LP |
8387 | case ARG_NO_WALL: |
8388 | arg_no_wall = true; | |
8389 | break; | |
8390 | ||
be394c48 | 8391 | case ARG_ROOT: |
bac75eb3 | 8392 | r = parse_path_argument_and_warn(optarg, false, &arg_root); |
0f03c2a4 LP |
8393 | if (r < 0) |
8394 | return r; | |
be394c48 FC |
8395 | break; |
8396 | ||
98a6e132 | 8397 | case 'l': |
8fe914ec LP |
8398 | arg_full = true; |
8399 | break; | |
8400 | ||
30732560 | 8401 | case ARG_FAILED: |
9b9b3d36 MW |
8402 | if (strv_extend(&arg_states, "failed") < 0) |
8403 | return log_oom(); | |
8404 | ||
30732560 LP |
8405 | break; |
8406 | ||
1ae17672 ZJS |
8407 | case ARG_DRY_RUN: |
8408 | arg_dry_run = true; | |
8409 | break; | |
8410 | ||
0183528f LP |
8411 | case 'q': |
8412 | arg_quiet = true; | |
8413 | break; | |
8414 | ||
b4f27ccc | 8415 | case 'f': |
313cefa1 | 8416 | arg_force++; |
ee5762e3 LP |
8417 | break; |
8418 | ||
8419 | case ARG_NO_RELOAD: | |
8420 | arg_no_reload = true; | |
8421 | break; | |
8422 | ||
8a0867d6 LP |
8423 | case ARG_KILL_WHO: |
8424 | arg_kill_who = optarg; | |
8425 | break; | |
8426 | ||
8a0867d6 | 8427 | case 's': |
5c828e66 LP |
8428 | if (streq(optarg, "help")) { |
8429 | DUMP_STRING_TABLE(signal, int, _NSIG); | |
8430 | return 0; | |
8431 | } | |
8432 | ||
29a3db75 | 8433 | arg_signal = signal_from_string(optarg); |
baaa35ad ZJS |
8434 | if (arg_signal < 0) |
8435 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8436 | "Failed to parse signal string %s.", | |
8437 | optarg); | |
8a0867d6 LP |
8438 | break; |
8439 | ||
501fc174 LP |
8440 | case ARG_NO_ASK_PASSWORD: |
8441 | arg_ask_password = false; | |
8442 | break; | |
8443 | ||
f459b602 MAP |
8444 | case 'H': |
8445 | arg_transport = BUS_TRANSPORT_REMOTE; | |
8446 | arg_host = optarg; | |
a8f11321 LP |
8447 | break; |
8448 | ||
f459b602 | 8449 | case 'M': |
de33fc62 | 8450 | arg_transport = BUS_TRANSPORT_MACHINE; |
f459b602 | 8451 | arg_host = optarg; |
a8f11321 LP |
8452 | break; |
8453 | ||
729e3769 LP |
8454 | case ARG_RUNTIME: |
8455 | arg_runtime = true; | |
8456 | break; | |
8457 | ||
df50185b | 8458 | case 'n': |
baaa35ad ZJS |
8459 | if (safe_atou(optarg, &arg_lines) < 0) |
8460 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8461 | "Failed to parse lines '%s'", | |
8462 | optarg); | |
df50185b LP |
8463 | break; |
8464 | ||
df50185b | 8465 | case 'o': |
5c828e66 LP |
8466 | if (streq(optarg, "help")) { |
8467 | DUMP_STRING_TABLE(output_mode, OutputMode, _OUTPUT_MODE_MAX); | |
8468 | return 0; | |
8469 | } | |
8470 | ||
df50185b | 8471 | arg_output = output_mode_from_string(optarg); |
baaa35ad ZJS |
8472 | if (arg_output < 0) |
8473 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8474 | "Unknown output '%s'.", | |
8475 | optarg); | |
df50185b LP |
8476 | break; |
8477 | ||
b37844d3 LP |
8478 | case 'i': |
8479 | arg_ignore_inhibitors = true; | |
8480 | break; | |
8481 | ||
5d0c05e5 LN |
8482 | case ARG_PLAIN: |
8483 | arg_plain = true; | |
8484 | break; | |
8485 | ||
5bdf2243 JJ |
8486 | case ARG_FIRMWARE_SETUP: |
8487 | arg_firmware_setup = true; | |
8488 | break; | |
8489 | ||
97af80c5 LP |
8490 | case ARG_BOOT_LOADER_MENU: |
8491 | ||
8492 | r = parse_sec(optarg, &arg_boot_loader_menu); | |
8493 | if (r < 0) | |
8494 | return log_error_errno(r, "Failed to parse --boot-loader-menu= argument '%s': %m", optarg); | |
8495 | ||
8496 | break; | |
8497 | ||
8498 | case ARG_BOOT_LOADER_ENTRY: | |
8499 | ||
8500 | if (streq(optarg, "help")) { /* Yes, this means, "help" is not a valid boot loader entry name we can deal with */ | |
8501 | r = help_boot_loader_entry(); | |
8502 | if (r < 0) | |
8503 | return r; | |
8504 | ||
8505 | return 0; | |
8506 | } | |
8507 | ||
8508 | arg_boot_loader_entry = empty_to_null(optarg); | |
8509 | break; | |
8510 | ||
9b9b3d36 | 8511 | case ARG_STATE: { |
fb1ac50e LP |
8512 | const char *p; |
8513 | ||
baaa35ad ZJS |
8514 | if (isempty(optarg)) |
8515 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8516 | "--state= requires arguments."); | |
9b9b3d36 | 8517 | |
c58bd76a | 8518 | for (p = optarg;;) { |
bc6c18fe | 8519 | _cleanup_free_ char *s = NULL; |
9b9b3d36 | 8520 | |
5ab22f33 SS |
8521 | r = extract_first_word(&p, &s, ",", 0); |
8522 | if (r < 0) | |
6862111e | 8523 | return log_error_errno(r, "Failed to parse state: %s", optarg); |
5ab22f33 SS |
8524 | if (r == 0) |
8525 | break; | |
9b9b3d36 | 8526 | |
e16972e6 ZJS |
8527 | if (streq(s, "help")) { |
8528 | help_states(); | |
8529 | return 0; | |
8530 | } | |
8531 | ||
9dccc6d3 | 8532 | if (strv_consume(&arg_states, TAKE_PTR(s)) < 0) |
9b9b3d36 | 8533 | return log_oom(); |
9b9b3d36 MW |
8534 | } |
8535 | break; | |
8536 | } | |
8537 | ||
1238ee09 | 8538 | case 'r': |
baaa35ad ZJS |
8539 | if (geteuid() != 0) |
8540 | return log_error_errno(SYNTHETIC_ERRNO(EPERM), | |
8541 | "--recursive requires root privileges."); | |
1238ee09 LP |
8542 | |
8543 | arg_recursive = true; | |
8544 | break; | |
8545 | ||
d309c1c3 | 8546 | case ARG_PRESET_MODE: |
5c828e66 LP |
8547 | if (streq(optarg, "help")) { |
8548 | DUMP_STRING_TABLE(unit_file_preset_mode, UnitFilePresetMode, _UNIT_FILE_PRESET_MAX); | |
8549 | return 0; | |
8550 | } | |
d309c1c3 LP |
8551 | |
8552 | arg_preset_mode = unit_file_preset_mode_from_string(optarg); | |
baaa35ad ZJS |
8553 | if (arg_preset_mode < 0) |
8554 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8555 | "Failed to parse preset mode: %s.", optarg); | |
d309c1c3 LP |
8556 | |
8557 | break; | |
8558 | ||
57ab2eab JS |
8559 | case ARG_NOW: |
8560 | arg_now = true; | |
8561 | break; | |
8562 | ||
9ef15026 JS |
8563 | case ARG_MESSAGE: |
8564 | if (strv_extend(&arg_wall, optarg) < 0) | |
8565 | return log_oom(); | |
8566 | break; | |
8567 | ||
85d9b598 LP |
8568 | case 'T': |
8569 | arg_show_transaction = true; | |
8570 | break; | |
8571 | ||
d81bc51f LP |
8572 | case ARG_WHAT: { |
8573 | const char *p; | |
8574 | ||
8575 | if (isempty(optarg)) | |
8576 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--what= requires arguments."); | |
8577 | ||
8578 | for (p = optarg;;) { | |
8579 | _cleanup_free_ char *k = NULL; | |
8580 | ||
8581 | r = extract_first_word(&p, &k, ",", 0); | |
8582 | if (r < 0) | |
8583 | return log_error_errno(r, "Failed to parse directory type: %s", optarg); | |
8584 | if (r == 0) | |
8585 | break; | |
8586 | ||
8587 | if (streq(k, "help")) { | |
8588 | puts("runtime\n" | |
8589 | "state\n" | |
8590 | "cache\n" | |
8591 | "logs\n" | |
8592 | "configuration"); | |
8593 | return 0; | |
8594 | } | |
8595 | ||
8596 | r = strv_consume(&arg_clean_what, TAKE_PTR(k)); | |
8597 | if (r < 0) | |
8598 | return log_oom(); | |
8599 | } | |
8600 | ||
8601 | break; | |
8602 | } | |
8603 | ||
2e7e19ca ZJS |
8604 | case '.': |
8605 | /* Output an error mimicking getopt, and print a hint afterwards */ | |
8606 | log_error("%s: invalid option -- '.'", program_invocation_name); | |
8607 | log_notice("Hint: to specify units starting with a dash, use \"--\":\n" | |
8608 | " %s [OPTIONS...] {COMMAND} -- -.%s ...", | |
8609 | program_invocation_name, optarg ?: "mount"); | |
8610 | _fallthrough_; | |
8611 | ||
7e4249b9 LP |
8612 | case '?': |
8613 | return -EINVAL; | |
8614 | ||
8615 | default: | |
eb9da376 | 8616 | assert_not_reached("Unhandled option"); |
7e4249b9 | 8617 | } |
7e4249b9 | 8618 | |
baaa35ad ZJS |
8619 | if (arg_transport != BUS_TRANSPORT_LOCAL && arg_scope != UNIT_FILE_SYSTEM) |
8620 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8621 | "Cannot access user instance remotely."); | |
a8f11321 | 8622 | |
baaa35ad ZJS |
8623 | if (arg_wait && arg_no_block) |
8624 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8625 | "--wait may not be combined with --no-block."); | |
93a08841 | 8626 | |
7e4249b9 LP |
8627 | return 1; |
8628 | } | |
8629 | ||
e4b61340 | 8630 | static int halt_parse_argv(int argc, char *argv[]) { |
e4b61340 LP |
8631 | enum { |
8632 | ARG_HELP = 0x100, | |
8633 | ARG_HALT, | |
514f4ef5 LP |
8634 | ARG_REBOOT, |
8635 | ARG_NO_WALL | |
e4b61340 LP |
8636 | }; |
8637 | ||
8638 | static const struct option options[] = { | |
8639 | { "help", no_argument, NULL, ARG_HELP }, | |
8640 | { "halt", no_argument, NULL, ARG_HALT }, | |
8641 | { "poweroff", no_argument, NULL, 'p' }, | |
8642 | { "reboot", no_argument, NULL, ARG_REBOOT }, | |
8643 | { "force", no_argument, NULL, 'f' }, | |
8644 | { "wtmp-only", no_argument, NULL, 'w' }, | |
8645 | { "no-wtmp", no_argument, NULL, 'd' }, | |
f3f054f0 | 8646 | { "no-sync", no_argument, NULL, 'n' }, |
514f4ef5 | 8647 | { "no-wall", no_argument, NULL, ARG_NO_WALL }, |
eb9da376 | 8648 | {} |
e4b61340 LP |
8649 | }; |
8650 | ||
37185ec8 | 8651 | int c, r, runlevel; |
e4b61340 LP |
8652 | |
8653 | assert(argc >= 0); | |
8654 | assert(argv); | |
8655 | ||
8656 | if (utmp_get_runlevel(&runlevel, NULL) >= 0) | |
4c701096 | 8657 | if (IN_SET(runlevel, '0', '6')) |
65491fd8 | 8658 | arg_force = 2; |
e4b61340 | 8659 | |
601185b4 | 8660 | while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) |
e4b61340 LP |
8661 | switch (c) { |
8662 | ||
8663 | case ARG_HELP: | |
37ec0fdd | 8664 | return halt_help(); |
e4b61340 LP |
8665 | |
8666 | case ARG_HALT: | |
8667 | arg_action = ACTION_HALT; | |
8668 | break; | |
8669 | ||
8670 | case 'p': | |
a042efad MS |
8671 | if (arg_action != ACTION_REBOOT) |
8672 | arg_action = ACTION_POWEROFF; | |
e4b61340 LP |
8673 | break; |
8674 | ||
8675 | case ARG_REBOOT: | |
8676 | arg_action = ACTION_REBOOT; | |
8677 | break; | |
8678 | ||
8679 | case 'f': | |
65491fd8 | 8680 | arg_force = 2; |
e4b61340 LP |
8681 | break; |
8682 | ||
8683 | case 'w': | |
1ae17672 | 8684 | arg_dry_run = true; |
e4b61340 LP |
8685 | break; |
8686 | ||
8687 | case 'd': | |
8688 | arg_no_wtmp = true; | |
8689 | break; | |
8690 | ||
f3f054f0 TB |
8691 | case 'n': |
8692 | arg_no_sync = true; | |
8693 | break; | |
8694 | ||
514f4ef5 LP |
8695 | case ARG_NO_WALL: |
8696 | arg_no_wall = true; | |
8697 | break; | |
8698 | ||
e4b61340 LP |
8699 | case 'i': |
8700 | case 'h': | |
8701 | /* Compatibility nops */ | |
8702 | break; | |
8703 | ||
8704 | case '?': | |
8705 | return -EINVAL; | |
8706 | ||
8707 | default: | |
eb9da376 | 8708 | assert_not_reached("Unhandled option"); |
e4b61340 | 8709 | } |
e4b61340 | 8710 | |
c5220a94 | 8711 | if (arg_action == ACTION_REBOOT && (argc == optind || argc == optind + 1)) { |
77defcf5 | 8712 | r = update_reboot_parameter_and_warn(argc == optind + 1 ? argv[optind] : NULL, false); |
c5220a94 | 8713 | if (r < 0) |
37185ec8 | 8714 | return r; |
baaa35ad ZJS |
8715 | } else if (optind < argc) |
8716 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8717 | "Too many arguments."); | |
e4b61340 LP |
8718 | |
8719 | return 1; | |
8720 | } | |
8721 | ||
8722 | static int shutdown_parse_argv(int argc, char *argv[]) { | |
e4b61340 LP |
8723 | enum { |
8724 | ARG_HELP = 0x100, | |
514f4ef5 | 8725 | ARG_NO_WALL |
e4b61340 LP |
8726 | }; |
8727 | ||
8728 | static const struct option options[] = { | |
8729 | { "help", no_argument, NULL, ARG_HELP }, | |
8730 | { "halt", no_argument, NULL, 'H' }, | |
8731 | { "poweroff", no_argument, NULL, 'P' }, | |
8732 | { "reboot", no_argument, NULL, 'r' }, | |
04ebb595 | 8733 | { "kexec", no_argument, NULL, 'K' }, /* not documented extension */ |
514f4ef5 | 8734 | { "no-wall", no_argument, NULL, ARG_NO_WALL }, |
eb9da376 | 8735 | {} |
e4b61340 LP |
8736 | }; |
8737 | ||
172d7abf | 8738 | char **wall = NULL; |
f6144808 | 8739 | int c, r; |
e4b61340 LP |
8740 | |
8741 | assert(argc >= 0); | |
8742 | assert(argv); | |
8743 | ||
b068c6f5 | 8744 | while ((c = getopt_long(argc, argv, "HPrhkKat:fFc", options, NULL)) >= 0) |
e4b61340 LP |
8745 | switch (c) { |
8746 | ||
8747 | case ARG_HELP: | |
37ec0fdd | 8748 | return shutdown_help(); |
e4b61340 LP |
8749 | |
8750 | case 'H': | |
8751 | arg_action = ACTION_HALT; | |
8752 | break; | |
8753 | ||
8754 | case 'P': | |
8755 | arg_action = ACTION_POWEROFF; | |
8756 | break; | |
8757 | ||
8758 | case 'r': | |
5622dde3 KS |
8759 | if (kexec_loaded()) |
8760 | arg_action = ACTION_KEXEC; | |
8761 | else | |
8762 | arg_action = ACTION_REBOOT; | |
e4b61340 LP |
8763 | break; |
8764 | ||
04ebb595 LP |
8765 | case 'K': |
8766 | arg_action = ACTION_KEXEC; | |
8767 | break; | |
8768 | ||
e4b61340 LP |
8769 | case 'h': |
8770 | if (arg_action != ACTION_HALT) | |
8771 | arg_action = ACTION_POWEROFF; | |
8772 | break; | |
8773 | ||
8774 | case 'k': | |
1ae17672 | 8775 | arg_dry_run = true; |
e4b61340 LP |
8776 | break; |
8777 | ||
514f4ef5 LP |
8778 | case ARG_NO_WALL: |
8779 | arg_no_wall = true; | |
8780 | break; | |
8781 | ||
e4b61340 | 8782 | case 'a': |
b068c6f5 | 8783 | case 't': /* Note that we also ignore any passed argument to -t, not just the -t itself */ |
75836b9d JS |
8784 | case 'f': |
8785 | case 'F': | |
e4b61340 LP |
8786 | /* Compatibility nops */ |
8787 | break; | |
8788 | ||
f6144808 LP |
8789 | case 'c': |
8790 | arg_action = ACTION_CANCEL_SHUTDOWN; | |
8791 | break; | |
8792 | ||
e4b61340 LP |
8793 | case '?': |
8794 | return -EINVAL; | |
8795 | ||
8796 | default: | |
eb9da376 | 8797 | assert_not_reached("Unhandled option"); |
e4b61340 | 8798 | } |
e4b61340 | 8799 | |
dfcc5c33 | 8800 | if (argc > optind && arg_action != ACTION_CANCEL_SHUTDOWN) { |
2cc7b0a2 | 8801 | r = parse_shutdown_time_spec(argv[optind], &arg_when); |
7e59bfcb | 8802 | if (r < 0) { |
f6144808 LP |
8803 | log_error("Failed to parse time specification: %s", argv[optind]); |
8804 | return r; | |
8805 | } | |
6b5ad000 | 8806 | } else |
08e4b1c5 | 8807 | arg_when = now(CLOCK_REALTIME) + USEC_PER_MINUTE; |
442b9094 | 8808 | |
dfcc5c33 MS |
8809 | if (argc > optind && arg_action == ACTION_CANCEL_SHUTDOWN) |
8810 | /* No time argument for shutdown cancel */ | |
172d7abf | 8811 | wall = argv + optind; |
dfcc5c33 MS |
8812 | else if (argc > optind + 1) |
8813 | /* We skip the time argument */ | |
172d7abf LP |
8814 | wall = argv + optind + 1; |
8815 | ||
8816 | if (wall) { | |
8817 | arg_wall = strv_copy(wall); | |
8818 | if (!arg_wall) | |
8819 | return log_oom(); | |
8820 | } | |
e4b61340 LP |
8821 | |
8822 | optind = argc; | |
8823 | ||
8824 | return 1; | |
e4b61340 LP |
8825 | } |
8826 | ||
8827 | static int telinit_parse_argv(int argc, char *argv[]) { | |
e4b61340 LP |
8828 | enum { |
8829 | ARG_HELP = 0x100, | |
514f4ef5 | 8830 | ARG_NO_WALL |
e4b61340 LP |
8831 | }; |
8832 | ||
8833 | static const struct option options[] = { | |
8834 | { "help", no_argument, NULL, ARG_HELP }, | |
514f4ef5 | 8835 | { "no-wall", no_argument, NULL, ARG_NO_WALL }, |
eb9da376 | 8836 | {} |
e4b61340 LP |
8837 | }; |
8838 | ||
8839 | static const struct { | |
8840 | char from; | |
8841 | enum action to; | |
8842 | } table[] = { | |
8843 | { '0', ACTION_POWEROFF }, | |
8844 | { '6', ACTION_REBOOT }, | |
ef2f1067 | 8845 | { '1', ACTION_RESCUE }, |
e4b61340 LP |
8846 | { '2', ACTION_RUNLEVEL2 }, |
8847 | { '3', ACTION_RUNLEVEL3 }, | |
8848 | { '4', ACTION_RUNLEVEL4 }, | |
8849 | { '5', ACTION_RUNLEVEL5 }, | |
8850 | { 's', ACTION_RESCUE }, | |
8851 | { 'S', ACTION_RESCUE }, | |
8852 | { 'q', ACTION_RELOAD }, | |
8853 | { 'Q', ACTION_RELOAD }, | |
8854 | { 'u', ACTION_REEXEC }, | |
8855 | { 'U', ACTION_REEXEC } | |
8856 | }; | |
8857 | ||
8858 | unsigned i; | |
8859 | int c; | |
8860 | ||
8861 | assert(argc >= 0); | |
8862 | assert(argv); | |
8863 | ||
601185b4 | 8864 | while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) |
e4b61340 LP |
8865 | switch (c) { |
8866 | ||
8867 | case ARG_HELP: | |
37ec0fdd | 8868 | return telinit_help(); |
e4b61340 | 8869 | |
514f4ef5 LP |
8870 | case ARG_NO_WALL: |
8871 | arg_no_wall = true; | |
8872 | break; | |
8873 | ||
e4b61340 LP |
8874 | case '?': |
8875 | return -EINVAL; | |
8876 | ||
8877 | default: | |
eb9da376 | 8878 | assert_not_reached("Unhandled option"); |
e4b61340 | 8879 | } |
e4b61340 | 8880 | |
baaa35ad ZJS |
8881 | if (optind >= argc) |
8882 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8883 | "%s: required argument missing.", | |
8884 | program_invocation_short_name); | |
e4b61340 | 8885 | |
baaa35ad ZJS |
8886 | if (optind + 1 < argc) |
8887 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8888 | "Too many arguments."); | |
e4b61340 | 8889 | |
baaa35ad ZJS |
8890 | if (strlen(argv[optind]) != 1) |
8891 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8892 | "Expected single character argument."); | |
e4b61340 LP |
8893 | |
8894 | for (i = 0; i < ELEMENTSOF(table); i++) | |
8895 | if (table[i].from == argv[optind][0]) | |
8896 | break; | |
8897 | ||
baaa35ad ZJS |
8898 | if (i >= ELEMENTSOF(table)) |
8899 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8900 | "Unknown command '%s'.", argv[optind]); | |
e4b61340 LP |
8901 | |
8902 | arg_action = table[i].to; | |
8903 | ||
313cefa1 | 8904 | optind++; |
e4b61340 LP |
8905 | |
8906 | return 1; | |
8907 | } | |
8908 | ||
8909 | static int runlevel_parse_argv(int argc, char *argv[]) { | |
e4b61340 LP |
8910 | enum { |
8911 | ARG_HELP = 0x100, | |
8912 | }; | |
8913 | ||
8914 | static const struct option options[] = { | |
8915 | { "help", no_argument, NULL, ARG_HELP }, | |
eb9da376 | 8916 | {} |
e4b61340 LP |
8917 | }; |
8918 | ||
8919 | int c; | |
8920 | ||
8921 | assert(argc >= 0); | |
8922 | assert(argv); | |
8923 | ||
601185b4 | 8924 | while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) |
e4b61340 LP |
8925 | switch (c) { |
8926 | ||
8927 | case ARG_HELP: | |
37ec0fdd | 8928 | return runlevel_help(); |
e4b61340 LP |
8929 | |
8930 | case '?': | |
8931 | return -EINVAL; | |
8932 | ||
8933 | default: | |
eb9da376 | 8934 | assert_not_reached("Unhandled option"); |
e4b61340 | 8935 | } |
e4b61340 | 8936 | |
baaa35ad ZJS |
8937 | if (optind < argc) |
8938 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8939 | "Too many arguments."); | |
e4b61340 LP |
8940 | |
8941 | return 1; | |
8942 | } | |
8943 | ||
8944 | static int parse_argv(int argc, char *argv[]) { | |
8945 | assert(argc >= 0); | |
8946 | assert(argv); | |
8947 | ||
8948 | if (program_invocation_short_name) { | |
8949 | ||
8950 | if (strstr(program_invocation_short_name, "halt")) { | |
8951 | arg_action = ACTION_HALT; | |
8952 | return halt_parse_argv(argc, argv); | |
c54819ca | 8953 | |
e4b61340 LP |
8954 | } else if (strstr(program_invocation_short_name, "poweroff")) { |
8955 | arg_action = ACTION_POWEROFF; | |
8956 | return halt_parse_argv(argc, argv); | |
c54819ca | 8957 | |
e4b61340 | 8958 | } else if (strstr(program_invocation_short_name, "reboot")) { |
5622dde3 KS |
8959 | if (kexec_loaded()) |
8960 | arg_action = ACTION_KEXEC; | |
8961 | else | |
8962 | arg_action = ACTION_REBOOT; | |
e4b61340 | 8963 | return halt_parse_argv(argc, argv); |
c54819ca | 8964 | |
e4b61340 LP |
8965 | } else if (strstr(program_invocation_short_name, "shutdown")) { |
8966 | arg_action = ACTION_POWEROFF; | |
8967 | return shutdown_parse_argv(argc, argv); | |
c54819ca | 8968 | |
e4b61340 | 8969 | } else if (strstr(program_invocation_short_name, "init")) { |
d5ca5f11 | 8970 | |
c54819ca LP |
8971 | /* Matches invocations as "init" as well as "telinit", which are synonymous when run as PID != |
8972 | * 1 on SysV. | |
8973 | * | |
8974 | * On SysV "telinit" was the official command to communicate with PID 1, but "init" would | |
8975 | * redirect itself to "telinit" if called with PID != 1. We follow the same logic here still, | |
8976 | * though we add one level of indirection, as we implement "telinit" in "systemctl". Hence, for | |
8977 | * us if you invoke "init" you get "systemd", but it will execve() "systemctl" immediately with | |
8978 | * argv[] unmodified if PID is != 1. If you invoke "telinit" you directly get "systemctl". In | |
8979 | * both cases we shall do the same thing, which is why we do strstr(p_i_s_n, "init") here, as a | |
8980 | * quick way to match both. | |
8981 | * | |
8982 | * Also see redirect_telinit() in src/core/main.c. */ | |
8983 | ||
d5ca5f11 | 8984 | if (sd_booted() > 0) { |
f459b602 | 8985 | arg_action = _ACTION_INVALID; |
d5ca5f11 LP |
8986 | return telinit_parse_argv(argc, argv); |
8987 | } else { | |
c54819ca LP |
8988 | /* Hmm, so some other init system is running, we need to forward this request to |
8989 | * it. For now we simply guess that it is Upstart. */ | |
d5ca5f11 | 8990 | |
595225af | 8991 | (void) rlimit_nofile_safe(); |
4ad61fd1 | 8992 | execv(TELINIT, argv); |
d5ca5f11 | 8993 | |
baaa35ad ZJS |
8994 | return log_error_errno(SYNTHETIC_ERRNO(EIO), |
8995 | "Couldn't find an alternative telinit implementation to spawn."); | |
d5ca5f11 LP |
8996 | } |
8997 | ||
e4b61340 LP |
8998 | } else if (strstr(program_invocation_short_name, "runlevel")) { |
8999 | arg_action = ACTION_RUNLEVEL; | |
9000 | return runlevel_parse_argv(argc, argv); | |
9001 | } | |
9002 | } | |
9003 | ||
9004 | arg_action = ACTION_SYSTEMCTL; | |
9005 | return systemctl_parse_argv(argc, argv); | |
9006 | } | |
9007 | ||
349cc4a5 | 9008 | #if HAVE_SYSV_COMPAT |
44a6b1b6 | 9009 | _pure_ static int action_to_runlevel(void) { |
eb22ac37 LP |
9010 | static const char table[_ACTION_MAX] = { |
9011 | [ACTION_HALT] = '0', | |
9012 | [ACTION_POWEROFF] = '0', | |
9013 | [ACTION_REBOOT] = '6', | |
9014 | [ACTION_RUNLEVEL2] = '2', | |
9015 | [ACTION_RUNLEVEL3] = '3', | |
9016 | [ACTION_RUNLEVEL4] = '4', | |
9017 | [ACTION_RUNLEVEL5] = '5', | |
9018 | [ACTION_RESCUE] = '1' | |
9019 | }; | |
9020 | ||
78ca9099 | 9021 | assert(arg_action >= 0 && arg_action < _ACTION_MAX); |
d55ae9e6 LP |
9022 | return table[arg_action]; |
9023 | } | |
d2e79673 | 9024 | #endif |
d55ae9e6 | 9025 | |
e449de87 | 9026 | static int systemctl_main(int argc, char *argv[]) { |
e449de87 | 9027 | static const Verb verbs[] = { |
c56d1e2c CW |
9028 | { "list-units", VERB_ANY, VERB_ANY, VERB_DEFAULT|VERB_ONLINE_ONLY, list_units }, |
9029 | { "list-unit-files", VERB_ANY, VERB_ANY, 0, list_unit_files }, | |
9030 | { "list-sockets", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, list_sockets }, | |
9031 | { "list-timers", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, list_timers }, | |
9032 | { "list-jobs", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, list_jobs }, | |
a004cb75 | 9033 | { "list-machines", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, list_machines }, |
c56d1e2c CW |
9034 | { "clear-jobs", VERB_ANY, 1, VERB_ONLINE_ONLY, trivial_method }, |
9035 | { "cancel", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, cancel_job }, | |
9036 | { "start", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, | |
9037 | { "stop", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, | |
9038 | { "condstop", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, /* For compatibility with ALTLinux */ | |
9039 | { "reload", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, | |
9040 | { "restart", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, | |
9041 | { "try-restart", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, | |
9042 | { "reload-or-restart", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, | |
5238e957 | 9043 | { "reload-or-try-restart", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, /* For compatibility with old systemctl <= 228 */ |
c56d1e2c CW |
9044 | { "try-reload-or-restart", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, |
9045 | { "force-reload", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, /* For compatibility with SysV */ | |
9046 | { "condreload", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, /* For compatibility with ALTLinux */ | |
9047 | { "condrestart", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, /* For compatibility with RH */ | |
9048 | { "isolate", 2, 2, VERB_ONLINE_ONLY, start_unit }, | |
9049 | { "kill", 2, VERB_ANY, VERB_ONLINE_ONLY, kill_unit }, | |
d81bc51f | 9050 | { "clean", 2, VERB_ANY, VERB_ONLINE_ONLY, clean_unit }, |
c56d1e2c | 9051 | { "is-active", 2, VERB_ANY, VERB_ONLINE_ONLY, check_unit_active }, |
3e7e587d | 9052 | { "check", 2, VERB_ANY, VERB_ONLINE_ONLY, check_unit_active }, /* deprecated alias of is-active */ |
c56d1e2c CW |
9053 | { "is-failed", 2, VERB_ANY, VERB_ONLINE_ONLY, check_unit_failed }, |
9054 | { "show", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, show }, | |
9055 | { "cat", 2, VERB_ANY, VERB_ONLINE_ONLY, cat }, | |
9056 | { "status", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, show }, | |
9057 | { "help", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, show }, | |
9058 | { "daemon-reload", VERB_ANY, 1, VERB_ONLINE_ONLY, daemon_reload }, | |
9059 | { "daemon-reexec", VERB_ANY, 1, VERB_ONLINE_ONLY, daemon_reload }, | |
9060 | { "show-environment", VERB_ANY, 1, VERB_ONLINE_ONLY, show_environment }, | |
9061 | { "set-environment", 2, VERB_ANY, VERB_ONLINE_ONLY, set_environment }, | |
9062 | { "unset-environment", 2, VERB_ANY, VERB_ONLINE_ONLY, set_environment }, | |
9063 | { "import-environment", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, import_environment }, | |
9064 | { "halt", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, | |
9065 | { "poweroff", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, | |
9066 | { "reboot", VERB_ANY, 2, VERB_ONLINE_ONLY, start_system_special }, | |
9067 | { "kexec", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, | |
9068 | { "suspend", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, | |
9069 | { "hibernate", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, | |
9070 | { "hybrid-sleep", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, | |
e68c79db | 9071 | { "suspend-then-hibernate",VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, |
c56d1e2c CW |
9072 | { "default", VERB_ANY, 1, VERB_ONLINE_ONLY, start_special }, |
9073 | { "rescue", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, | |
9074 | { "emergency", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, | |
9075 | { "exit", VERB_ANY, 2, VERB_ONLINE_ONLY, start_special }, | |
9076 | { "reset-failed", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, reset_failed }, | |
9077 | { "enable", 2, VERB_ANY, 0, enable_unit }, | |
9078 | { "disable", 2, VERB_ANY, 0, enable_unit }, | |
9079 | { "is-enabled", 2, VERB_ANY, 0, unit_is_enabled }, | |
9080 | { "reenable", 2, VERB_ANY, 0, enable_unit }, | |
9081 | { "preset", 2, VERB_ANY, 0, enable_unit }, | |
9082 | { "preset-all", VERB_ANY, 1, 0, preset_all }, | |
9083 | { "mask", 2, VERB_ANY, 0, enable_unit }, | |
9084 | { "unmask", 2, VERB_ANY, 0, enable_unit }, | |
9085 | { "link", 2, VERB_ANY, 0, enable_unit }, | |
9086 | { "revert", 2, VERB_ANY, 0, enable_unit }, | |
9087 | { "switch-root", 2, VERB_ANY, VERB_ONLINE_ONLY, switch_root }, | |
9088 | { "list-dependencies", VERB_ANY, 2, VERB_ONLINE_ONLY, list_dependencies }, | |
9089 | { "set-default", 2, 2, 0, set_default }, | |
9090 | { "get-default", VERB_ANY, 1, 0, get_default }, | |
9091 | { "set-property", 3, VERB_ANY, VERB_ONLINE_ONLY, set_property }, | |
9092 | { "is-system-running", VERB_ANY, 1, 0, is_system_running }, | |
9093 | { "add-wants", 3, VERB_ANY, 0, add_dependency }, | |
9094 | { "add-requires", 3, VERB_ANY, 0, add_dependency }, | |
9095 | { "edit", 2, VERB_ANY, VERB_ONLINE_ONLY, edit }, | |
d08e75ed | 9096 | {} |
e449de87 | 9097 | }; |
7e4249b9 | 9098 | |
e449de87 | 9099 | return dispatch_verb(argc, argv, verbs, NULL); |
e4b61340 LP |
9100 | } |
9101 | ||
4fbd7192 | 9102 | static int reload_with_fallback(void) { |
4fbd7192 | 9103 | /* First, try systemd via D-Bus. */ |
e449de87 | 9104 | if (daemon_reload(0, NULL, NULL) >= 0) |
4fbd7192 | 9105 | return 0; |
e4b61340 LP |
9106 | |
9107 | /* Nothing else worked, so let's try signals */ | |
2853b60a | 9108 | assert(IN_SET(arg_action, ACTION_RELOAD, ACTION_REEXEC)); |
e4b61340 | 9109 | |
4a62c710 MS |
9110 | if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) |
9111 | return log_error_errno(errno, "kill() failed: %m"); | |
e4b61340 LP |
9112 | |
9113 | return 0; | |
9114 | } | |
9115 | ||
4fbd7192 | 9116 | static int start_with_fallback(void) { |
4fbd7192 | 9117 | /* First, try systemd via D-Bus. */ |
2e13c0c3 | 9118 | if (start_unit(0, NULL, NULL) == 0) |
4fbd7192 | 9119 | return 0; |
e4b61340 | 9120 | |
2771aaf5 | 9121 | #if HAVE_SYSV_COMPAT |
2853b60a | 9122 | /* Nothing else worked, so let's try /dev/initctl */ |
63a3b3cb | 9123 | if (talk_initctl(action_to_runlevel()) > 0) |
48ec22bc | 9124 | return 0; |
2771aaf5 | 9125 | #endif |
d55ae9e6 | 9126 | |
baaa35ad ZJS |
9127 | return log_error_errno(SYNTHETIC_ERRNO(EIO), |
9128 | "Failed to talk to init daemon."); | |
e4b61340 LP |
9129 | } |
9130 | ||
477def80 | 9131 | static int halt_now(enum action a) { |
c01dcddf | 9132 | /* The kernel will automatically flush ATA disks and suchlike on reboot(), but the file systems need to be |
f03ec2b1 | 9133 | * synced explicitly in advance. */ |
1ae17672 | 9134 | if (!arg_no_sync && !arg_dry_run) |
f3f054f0 | 9135 | (void) sync(); |
4a3ad399 | 9136 | |
1ae17672 ZJS |
9137 | /* Make sure C-A-D is handled by the kernel from this point on... */ |
9138 | if (!arg_dry_run) | |
9139 | (void) reboot(RB_ENABLE_CAD); | |
e606bb61 | 9140 | |
4c80c73c | 9141 | switch (a) { |
e606bb61 LP |
9142 | |
9143 | case ACTION_HALT: | |
60675884 LP |
9144 | if (!arg_quiet) |
9145 | log_info("Halting."); | |
1ae17672 ZJS |
9146 | if (arg_dry_run) |
9147 | return 0; | |
19578bb2 | 9148 | (void) reboot(RB_HALT_SYSTEM); |
477def80 | 9149 | return -errno; |
e606bb61 LP |
9150 | |
9151 | case ACTION_POWEROFF: | |
60675884 LP |
9152 | if (!arg_quiet) |
9153 | log_info("Powering off."); | |
1ae17672 ZJS |
9154 | if (arg_dry_run) |
9155 | return 0; | |
19578bb2 | 9156 | (void) reboot(RB_POWER_OFF); |
477def80 | 9157 | return -errno; |
e606bb61 | 9158 | |
98d52feb | 9159 | case ACTION_KEXEC: |
c01dcddf LP |
9160 | case ACTION_REBOOT: |
9161 | return reboot_with_parameter(REBOOT_FALLBACK | | |
9162 | (arg_quiet ? 0 : REBOOT_LOG) | | |
9163 | (arg_dry_run ? REBOOT_DRY_RUN : 0)); | |
e606bb61 | 9164 | |
477def80 LP |
9165 | default: |
9166 | assert_not_reached("Unknown action."); | |
9167 | } | |
e606bb61 LP |
9168 | } |
9169 | ||
56a730fa | 9170 | static int logind_schedule_shutdown(void) { |
7f56c3d4 | 9171 | |
cf9cda44 | 9172 | #if ENABLE_LOGIND |
4afd3348 | 9173 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
56a730fa LP |
9174 | char date[FORMAT_TIMESTAMP_MAX]; |
9175 | const char *action; | |
4fbd7192 | 9176 | sd_bus *bus; |
56a730fa LP |
9177 | int r; |
9178 | ||
4fbd7192 | 9179 | r = acquire_bus(BUS_FULL, &bus); |
56a730fa | 9180 | if (r < 0) |
4fbd7192 | 9181 | return r; |
56a730fa LP |
9182 | |
9183 | switch (arg_action) { | |
9184 | case ACTION_HALT: | |
9185 | action = "halt"; | |
9186 | break; | |
9187 | case ACTION_POWEROFF: | |
9188 | action = "poweroff"; | |
9189 | break; | |
9190 | case ACTION_KEXEC: | |
9191 | action = "kexec"; | |
9192 | break; | |
a4420f7b LP |
9193 | case ACTION_EXIT: |
9194 | action = "exit"; | |
9195 | break; | |
9196 | case ACTION_REBOOT: | |
56a730fa LP |
9197 | default: |
9198 | action = "reboot"; | |
9199 | break; | |
9200 | } | |
9201 | ||
1ae17672 | 9202 | if (arg_dry_run) |
56a730fa LP |
9203 | action = strjoina("dry-", action); |
9204 | ||
d2ad7e1b ZJS |
9205 | (void) logind_set_wall_message(); |
9206 | ||
56a730fa | 9207 | r = sd_bus_call_method( |
4fbd7192 | 9208 | bus, |
56a730fa LP |
9209 | "org.freedesktop.login1", |
9210 | "/org/freedesktop/login1", | |
9211 | "org.freedesktop.login1.Manager", | |
9212 | "ScheduleShutdown", | |
9213 | &error, | |
9214 | NULL, | |
9215 | "st", | |
9216 | action, | |
9217 | arg_when); | |
9218 | if (r < 0) | |
9219 | return log_warning_errno(r, "Failed to call ScheduleShutdown in logind, proceeding with immediate shutdown: %s", bus_error_message(&error, r)); | |
9220 | ||
60675884 LP |
9221 | if (!arg_quiet) |
9222 | log_info("Shutdown scheduled for %s, use 'shutdown -c' to cancel.", format_timestamp(date, sizeof(date), arg_when)); | |
56a730fa | 9223 | return 0; |
cf9cda44 | 9224 | #else |
2f50e59f LP |
9225 | return log_error_errno(SYNTHETIC_ERRNO(ENOSYS), |
9226 | "Cannot schedule shutdown without logind support, proceeding with immediate shutdown."); | |
807690fb | 9227 | #endif |
56a730fa LP |
9228 | } |
9229 | ||
4fbd7192 | 9230 | static int halt_main(void) { |
e4b61340 LP |
9231 | int r; |
9232 | ||
4fbd7192 | 9233 | r = logind_check_inhibitors(arg_action); |
748ebafa LP |
9234 | if (r < 0) |
9235 | return r; | |
b37844d3 | 9236 | |
940bec70 DJL |
9237 | /* Delayed shutdown requested, and was successful */ |
9238 | if (arg_when > 0 && logind_schedule_shutdown() == 0) | |
9239 | return 0; | |
9240 | /* no delay, or logind failed or is not at all available */ | |
7f96539d | 9241 | |
bc8c2f5c | 9242 | if (geteuid() != 0) { |
1ae17672 | 9243 | if (arg_dry_run || arg_force > 0) { |
fba868fa | 9244 | (void) must_be_root(); |
2ac3930f IS |
9245 | return -EPERM; |
9246 | } | |
9247 | ||
7e59bfcb | 9248 | /* Try logind if we are a normal user and no special |
d35f51ea | 9249 | * mode applies. Maybe polkit allows us to shutdown |
7e59bfcb | 9250 | * the machine. */ |
36b69c31 | 9251 | if (IN_SET(arg_action, ACTION_POWEROFF, ACTION_REBOOT, ACTION_HALT)) { |
4fbd7192 | 9252 | r = logind_reboot(arg_action); |
4c80c73c KS |
9253 | if (r >= 0) |
9254 | return r; | |
a9085ea3 | 9255 | if (IN_SET(r, -EOPNOTSUPP, -EINPROGRESS)) |
7f96539d LP |
9256 | /* requested operation is not |
9257 | * supported on the local system or | |
9258 | * already in progress */ | |
a9085ea3 IS |
9259 | return r; |
9260 | /* on all other errors, try low-level operation */ | |
4c80c73c | 9261 | } |
bc8c2f5c LP |
9262 | } |
9263 | ||
2955e0d4 GJ |
9264 | /* In order to minimize the difference between operation with and |
9265 | * without logind, we explicitly enable non-blocking mode for this, | |
9266 | * as logind's shutdown operations are always non-blocking. */ | |
9267 | arg_no_block = true; | |
9268 | ||
1ae17672 | 9269 | if (!arg_dry_run && !arg_force) |
4fbd7192 | 9270 | return start_with_fallback(); |
e4b61340 | 9271 | |
2ac3930f IS |
9272 | assert(geteuid() == 0); |
9273 | ||
d90e1a30 LP |
9274 | if (!arg_no_wtmp) { |
9275 | if (sd_booted() > 0) | |
9276 | log_debug("Not writing utmp record, assuming that systemd-update-utmp is used."); | |
7e59bfcb LP |
9277 | else { |
9278 | r = utmp_put_shutdown(); | |
9279 | if (r < 0) | |
da927ba9 | 9280 | log_warning_errno(r, "Failed to write utmp record: %m"); |
7e59bfcb | 9281 | } |
d90e1a30 | 9282 | } |
e4b61340 | 9283 | |
1ae17672 | 9284 | if (arg_dry_run) |
e4b61340 LP |
9285 | return 0; |
9286 | ||
477def80 | 9287 | r = halt_now(arg_action); |
691395d8 | 9288 | return log_error_errno(r, "Failed to reboot: %m"); |
e4b61340 LP |
9289 | } |
9290 | ||
9291 | static int runlevel_main(void) { | |
9292 | int r, runlevel, previous; | |
9293 | ||
729e3769 LP |
9294 | r = utmp_get_runlevel(&runlevel, &previous); |
9295 | if (r < 0) { | |
9296 | puts("unknown"); | |
e4b61340 LP |
9297 | return r; |
9298 | } | |
9299 | ||
9300 | printf("%c %c\n", | |
9301 | previous <= 0 ? 'N' : previous, | |
9302 | runlevel <= 0 ? 'N' : runlevel); | |
9303 | ||
9304 | return 0; | |
9305 | } | |
9306 | ||
2cf05793 | 9307 | static int logind_cancel_shutdown(void) { |
349cc4a5 | 9308 | #if ENABLE_LOGIND |
4afd3348 | 9309 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
4fbd7192 | 9310 | sd_bus *bus; |
949d9ce9 LP |
9311 | int r; |
9312 | ||
4fbd7192 | 9313 | r = acquire_bus(BUS_FULL, &bus); |
949d9ce9 | 9314 | if (r < 0) |
4fbd7192 | 9315 | return r; |
949d9ce9 | 9316 | |
4fbd7192 | 9317 | (void) logind_set_wall_message(); |
949d9ce9 LP |
9318 | |
9319 | r = sd_bus_call_method( | |
4fbd7192 | 9320 | bus, |
949d9ce9 LP |
9321 | "org.freedesktop.login1", |
9322 | "/org/freedesktop/login1", | |
9323 | "org.freedesktop.login1.Manager", | |
9324 | "CancelScheduledShutdown", | |
9325 | &error, | |
9326 | NULL, NULL); | |
9327 | if (r < 0) | |
9328 | return log_warning_errno(r, "Failed to talk to logind, shutdown hasn't been cancelled: %s", bus_error_message(&error, r)); | |
9329 | ||
9330 | return 0; | |
2cf05793 | 9331 | #else |
2f50e59f LP |
9332 | return log_error_errno(SYNTHETIC_ERRNO(ENOSYS), |
9333 | "Not compiled with logind support, cannot cancel scheduled shutdowns."); | |
2cf05793 | 9334 | #endif |
949d9ce9 LP |
9335 | } |
9336 | ||
f2a3de01 | 9337 | static int run(int argc, char *argv[]) { |
f459b602 | 9338 | int r; |
e4b61340 | 9339 | |
a9cdc94f | 9340 | setlocale(LC_ALL, ""); |
e4b61340 | 9341 | log_parse_environment(); |
2396fb04 | 9342 | log_open(); |
1abaf488 LP |
9343 | |
9344 | /* The journal merging logic potentially needs a lot of fds. */ | |
9345 | (void) rlimit_nofile_bump(HIGH_RLIMIT_NOFILE); | |
9346 | ||
592705f2 | 9347 | sigbus_install(); |
e4b61340 | 9348 | |
184ecaf7 DR |
9349 | /* Explicitly not on_tty() to avoid setting cached value. |
9350 | * This becomes relevant for piping output which might be | |
9351 | * ellipsized. */ | |
9352 | original_stdout_is_tty = isatty(STDOUT_FILENO); | |
9353 | ||
04ebb595 | 9354 | r = parse_argv(argc, argv); |
f459b602 | 9355 | if (r <= 0) |
e4b61340 | 9356 | goto finish; |
7e4249b9 | 9357 | |
040524b4 | 9358 | if (arg_action != ACTION_SYSTEMCTL && running_in_chroot() > 0) { |
60675884 LP |
9359 | if (!arg_quiet) |
9360 | log_info("Running in chroot, ignoring request."); | |
f459b602 | 9361 | r = 0; |
82e23ddd LP |
9362 | goto finish; |
9363 | } | |
9364 | ||
41dd15e4 LP |
9365 | /* systemctl_main() will print an error message for the bus |
9366 | * connection, but only if it needs to */ | |
e4b61340 LP |
9367 | |
9368 | switch (arg_action) { | |
9369 | ||
22f4096c | 9370 | case ACTION_SYSTEMCTL: |
e449de87 | 9371 | r = systemctl_main(argc, argv); |
e4b61340 | 9372 | break; |
e4b61340 | 9373 | |
081dc638 AJ |
9374 | /* Legacy command aliases set arg_action. They provide some fallbacks, |
9375 | * e.g. to tell sysvinit to reboot after you have installed systemd | |
9376 | * binaries. */ | |
9377 | ||
e4b61340 LP |
9378 | case ACTION_HALT: |
9379 | case ACTION_POWEROFF: | |
9380 | case ACTION_REBOOT: | |
5622dde3 | 9381 | case ACTION_KEXEC: |
4fbd7192 | 9382 | r = halt_main(); |
e4b61340 LP |
9383 | break; |
9384 | ||
e4b61340 LP |
9385 | case ACTION_RUNLEVEL2: |
9386 | case ACTION_RUNLEVEL3: | |
9387 | case ACTION_RUNLEVEL4: | |
9388 | case ACTION_RUNLEVEL5: | |
9389 | case ACTION_RESCUE: | |
4fbd7192 | 9390 | r = start_with_fallback(); |
e4b61340 | 9391 | break; |
7e4249b9 | 9392 | |
e4b61340 LP |
9393 | case ACTION_RELOAD: |
9394 | case ACTION_REEXEC: | |
4fbd7192 | 9395 | r = reload_with_fallback(); |
e4b61340 LP |
9396 | break; |
9397 | ||
949d9ce9 | 9398 | case ACTION_CANCEL_SHUTDOWN: |
2cf05793 | 9399 | r = logind_cancel_shutdown(); |
f6144808 LP |
9400 | break; |
9401 | ||
eb22ac37 | 9402 | case ACTION_RUNLEVEL: |
4f16c1f4 LP |
9403 | r = runlevel_main(); |
9404 | break; | |
9405 | ||
081dc638 AJ |
9406 | case ACTION_EXIT: |
9407 | case ACTION_SUSPEND: | |
9408 | case ACTION_HIBERNATE: | |
9409 | case ACTION_HYBRID_SLEEP: | |
e68c79db | 9410 | case ACTION_SUSPEND_THEN_HIBERNATE: |
081dc638 AJ |
9411 | case ACTION_EMERGENCY: |
9412 | case ACTION_DEFAULT: | |
9413 | /* systemctl verbs with no equivalent in the legacy commands. | |
9414 | * These cannot appear in arg_action. Fall through. */ | |
9415 | ||
f459b602 | 9416 | case _ACTION_INVALID: |
e4b61340 LP |
9417 | default: |
9418 | assert_not_reached("Unknown action"); | |
9419 | } | |
7e4249b9 LP |
9420 | |
9421 | finish: | |
cf647b69 LP |
9422 | release_busses(); |
9423 | ||
d1405af3 ZJS |
9424 | /* Note that we return r here, not 0, so that we can implement the LSB-like return codes */ |
9425 | return r; | |
7e4249b9 | 9426 | } |
d1405af3 ZJS |
9427 | |
9428 | DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run); |