]>
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" |
ab35fb1b | 31 | #include "cgroup-show.h" |
c6c18be3 | 32 | #include "cgroup-util.h" |
cda134ab | 33 | #include "copy.h" |
ad2a0358 | 34 | #include "dropin.h" |
5bdf2243 | 35 | #include "efivars.h" |
03a7b521 | 36 | #include "env-util.h" |
804ee07c | 37 | #include "escape.h" |
03a7b521 | 38 | #include "exit-status.h" |
3ffd4af2 | 39 | #include "fd-util.h" |
f97b34a6 | 40 | #include "format-util.h" |
f4f15635 | 41 | #include "fs-util.h" |
7d50b32a | 42 | #include "glob-util.h" |
08f3be7a | 43 | #include "hexdecoct.h" |
c52a937b | 44 | #include "hostname-util.h" |
0a970718 | 45 | #include "in-addr-util.h" |
03a7b521 LP |
46 | #include "initreq.h" |
47 | #include "install.h" | |
c004493c | 48 | #include "io-util.h" |
305a2850 | 49 | #include "journal-util.h" |
03a7b521 | 50 | #include "list.h" |
8752c575 | 51 | #include "locale-util.h" |
03a7b521 LP |
52 | #include "log.h" |
53 | #include "logs-show.h" | |
54 | #include "macro.h" | |
d1405af3 | 55 | #include "main-func.h" |
0a970718 | 56 | #include "memory-util.h" |
03a7b521 LP |
57 | #include "mkdir.h" |
58 | #include "pager.h" | |
6bedfcbb | 59 | #include "parse-util.h" |
03a7b521 LP |
60 | #include "path-lookup.h" |
61 | #include "path-util.h" | |
294bf0c3 | 62 | #include "pretty-print.h" |
01771226 | 63 | #include "proc-cmdline.h" |
03a7b521 | 64 | #include "process-util.h" |
e3631d1c | 65 | #include "reboot-util.h" |
78f22b97 | 66 | #include "rlimit-util.h" |
03a7b521 | 67 | #include "set.h" |
592705f2 | 68 | #include "sigbus.h" |
24882e06 | 69 | #include "signal-util.h" |
03a7b521 | 70 | #include "socket-util.h" |
760877e9 | 71 | #include "sort-util.h" |
03a7b521 LP |
72 | #include "spawn-ask-password-agent.h" |
73 | #include "spawn-polkit-agent.h" | |
74 | #include "special.h" | |
8fcde012 | 75 | #include "stat-util.h" |
8559e61d | 76 | #include "string-table.h" |
03a7b521 LP |
77 | #include "strv.h" |
78 | #include "terminal-util.h" | |
e4de7287 | 79 | #include "tmpfile-util.h" |
89ada3ba | 80 | #include "unit-def.h" |
03a7b521 | 81 | #include "unit-name.h" |
b1d4f8e1 | 82 | #include "user-util.h" |
305a2850 | 83 | #include "utf8.h" |
03a7b521 | 84 | #include "utmp-wtmp.h" |
e449de87 | 85 | #include "verbs.h" |
7f4b3c5e | 86 | #include "virt.h" |
7e4249b9 | 87 | |
b613907e SS |
88 | /* The init script exit status codes |
89 | 0 program is running or service is OK | |
90 | 1 program is dead and /var/run pid file exists | |
91 | 2 program is dead and /var/lock lock file exists | |
92 | 3 program is not running | |
93 | 4 program or service status is unknown | |
94 | 5-99 reserved for future LSB use | |
95 | 100-149 reserved for distribution use | |
96 | 150-199 reserved for application use | |
97 | 200-254 reserved | |
98 | */ | |
99 | enum { | |
100 | EXIT_PROGRAM_RUNNING_OR_SERVICE_OK = 0, | |
101 | EXIT_PROGRAM_DEAD_AND_PID_EXISTS = 1, | |
102 | EXIT_PROGRAM_DEAD_AND_LOCK_FILE_EXISTS = 2, | |
103 | EXIT_PROGRAM_NOT_RUNNING = 3, | |
104 | EXIT_PROGRAM_OR_SERVICES_STATUS_UNKNOWN = 4, | |
105 | }; | |
106 | ||
20b3f379 | 107 | static char **arg_types = NULL; |
9b9b3d36 | 108 | static char **arg_states = NULL; |
20b3f379 | 109 | static char **arg_properties = NULL; |
7e4249b9 | 110 | static bool arg_all = false; |
afba4199 ZJS |
111 | static enum dependency { |
112 | DEPENDENCY_FORWARD, | |
113 | DEPENDENCY_REVERSE, | |
114 | DEPENDENCY_AFTER, | |
115 | DEPENDENCY_BEFORE, | |
071066a5 | 116 | _DEPENDENCY_MAX |
afba4199 | 117 | } arg_dependency = DEPENDENCY_FORWARD; |
e67c3609 | 118 | static const char *arg_job_mode = "replace"; |
729e3769 | 119 | static UnitFileScope arg_scope = UNIT_FILE_SYSTEM; |
93a08841 | 120 | static bool arg_wait = false; |
ee5762e3 | 121 | static bool arg_no_block = false; |
ebed32bf | 122 | static bool arg_no_legend = false; |
0221d68a | 123 | static PagerFlags arg_pager_flags = 0; |
e4b61340 | 124 | static bool arg_no_wtmp = false; |
f3f054f0 | 125 | static bool arg_no_sync = false; |
514f4ef5 | 126 | static bool arg_no_wall = false; |
ee5762e3 | 127 | static bool arg_no_reload = false; |
4f9a9105 | 128 | static bool arg_value = false; |
991f2a39 | 129 | static bool arg_show_types = false; |
b37844d3 | 130 | static bool arg_ignore_inhibitors = false; |
1ae17672 | 131 | static bool arg_dry_run = false; |
0183528f | 132 | static bool arg_quiet = false; |
ee5762e3 | 133 | static bool arg_full = false; |
1238ee09 | 134 | static bool arg_recursive = false; |
e606bb61 | 135 | static int arg_force = 0; |
16f017fa | 136 | static bool arg_ask_password = false; |
729e3769 | 137 | static bool arg_runtime = false; |
d309c1c3 | 138 | static UnitFilePresetMode arg_preset_mode = UNIT_FILE_PRESET_FULL; |
e4b61340 | 139 | static char **arg_wall = NULL; |
8a0867d6 | 140 | static const char *arg_kill_who = NULL; |
8a0867d6 | 141 | static int arg_signal = SIGTERM; |
0f03c2a4 | 142 | static char *arg_root = NULL; |
f6144808 | 143 | static usec_t arg_when = 0; |
acc28e2e | 144 | static char *argv_cmdline = NULL; |
4445a875 | 145 | static enum action { |
e4b61340 LP |
146 | ACTION_SYSTEMCTL, |
147 | ACTION_HALT, | |
148 | ACTION_POWEROFF, | |
149 | ACTION_REBOOT, | |
20b09ca7 LP |
150 | ACTION_KEXEC, |
151 | ACTION_EXIT, | |
6edd7d0a LP |
152 | ACTION_SUSPEND, |
153 | ACTION_HIBERNATE, | |
6524990f | 154 | ACTION_HYBRID_SLEEP, |
e68c79db | 155 | ACTION_SUSPEND_THEN_HIBERNATE, |
e4b61340 LP |
156 | ACTION_RUNLEVEL2, |
157 | ACTION_RUNLEVEL3, | |
158 | ACTION_RUNLEVEL4, | |
159 | ACTION_RUNLEVEL5, | |
160 | ACTION_RESCUE, | |
514f4ef5 LP |
161 | ACTION_EMERGENCY, |
162 | ACTION_DEFAULT, | |
e4b61340 LP |
163 | ACTION_RELOAD, |
164 | ACTION_REEXEC, | |
165 | ACTION_RUNLEVEL, | |
f6144808 | 166 | ACTION_CANCEL_SHUTDOWN, |
913c1916 AJ |
167 | _ACTION_MAX, |
168 | _ACTION_INVALID = -1 | |
e4b61340 | 169 | } arg_action = ACTION_SYSTEMCTL; |
f459b602 | 170 | static BusTransport arg_transport = BUS_TRANSPORT_LOCAL; |
fc2ffaf1 | 171 | static const char *arg_host = NULL; |
df50185b LP |
172 | static unsigned arg_lines = 10; |
173 | static OutputMode arg_output = OUTPUT_SHORT; | |
5d0c05e5 | 174 | static bool arg_plain = false; |
5bdf2243 | 175 | static bool arg_firmware_setup = false; |
97af80c5 LP |
176 | static usec_t arg_boot_loader_menu = USEC_INFINITY; |
177 | static const char *arg_boot_loader_entry = NULL; | |
57ab2eab | 178 | static bool arg_now = false; |
82948f6c LP |
179 | static bool arg_jobs_before = false; |
180 | static bool arg_jobs_after = false; | |
1238ee09 | 181 | |
61ff45db LP |
182 | STATIC_DESTRUCTOR_REGISTER(arg_wall, strv_freep); |
183 | STATIC_DESTRUCTOR_REGISTER(arg_root, freep); | |
184 | STATIC_DESTRUCTOR_REGISTER(arg_types, strv_freep); | |
185 | STATIC_DESTRUCTOR_REGISTER(arg_states, strv_freep); | |
186 | STATIC_DESTRUCTOR_REGISTER(arg_properties, strv_freep); | |
187 | ||
e449de87 | 188 | static int daemon_reload(int argc, char *argv[], void* userdata); |
2853b60a | 189 | static int trivial_method(int argc, char *argv[], void *userdata); |
477def80 | 190 | static int halt_now(enum action a); |
fa0d5878 | 191 | static int get_state_one_unit(sd_bus *bus, const char *name, UnitActiveState *active_state); |
dbc2c080 | 192 | |
4fbd7192 LP |
193 | static bool original_stdout_is_tty; |
194 | ||
195 | typedef enum BusFocus { | |
196 | BUS_FULL, /* The full bus indicated via --system or --user */ | |
197 | BUS_MANAGER, /* The manager itself, possibly directly, possibly via the bus */ | |
198 | _BUS_FOCUS_MAX | |
199 | } BusFocus; | |
200 | ||
201 | static sd_bus *busses[_BUS_FOCUS_MAX] = {}; | |
202 | ||
b3796dd8 JS |
203 | static UnitFileFlags args_to_flags(void) { |
204 | return (arg_runtime ? UNIT_FILE_RUNTIME : 0) | | |
205 | (arg_force ? UNIT_FILE_FORCE : 0); | |
206 | } | |
207 | ||
4fbd7192 LP |
208 | static int acquire_bus(BusFocus focus, sd_bus **ret) { |
209 | int r; | |
210 | ||
211 | assert(focus < _BUS_FOCUS_MAX); | |
212 | assert(ret); | |
213 | ||
214 | /* We only go directly to the manager, if we are using a local transport */ | |
215 | if (arg_transport != BUS_TRANSPORT_LOCAL) | |
216 | focus = BUS_FULL; | |
217 | ||
d4015567 LP |
218 | if (getenv_bool("SYSTEMCTL_FORCE_BUS") > 0) |
219 | focus = BUS_FULL; | |
220 | ||
4fbd7192 LP |
221 | if (!busses[focus]) { |
222 | bool user; | |
223 | ||
224 | user = arg_scope != UNIT_FILE_SYSTEM; | |
225 | ||
226 | if (focus == BUS_MANAGER) | |
227 | r = bus_connect_transport_systemd(arg_transport, arg_host, user, &busses[focus]); | |
228 | else | |
229 | r = bus_connect_transport(arg_transport, arg_host, user, &busses[focus]); | |
230 | if (r < 0) | |
231 | return log_error_errno(r, "Failed to connect to bus: %m"); | |
232 | ||
233 | (void) sd_bus_set_allow_interactive_authorization(busses[focus], arg_ask_password); | |
234 | } | |
235 | ||
236 | *ret = busses[focus]; | |
237 | return 0; | |
238 | } | |
239 | ||
240 | static void release_busses(void) { | |
241 | BusFocus w; | |
242 | ||
243 | for (w = 0; w < _BUS_FOCUS_MAX; w++) | |
244 | busses[w] = sd_bus_flush_close_unref(busses[w]); | |
245 | } | |
246 | ||
6bb92a16 | 247 | static void ask_password_agent_open_if_enabled(void) { |
729e3769 | 248 | /* Open the password agent as a child process if necessary */ |
501fc174 | 249 | |
1ae17672 ZJS |
250 | if (arg_dry_run) |
251 | return; | |
252 | ||
501fc174 LP |
253 | if (!arg_ask_password) |
254 | return; | |
715554e7 | 255 | |
729e3769 | 256 | if (arg_scope != UNIT_FILE_SYSTEM) |
501fc174 LP |
257 | return; |
258 | ||
cbc9fbd1 LP |
259 | if (arg_transport != BUS_TRANSPORT_LOCAL) |
260 | return; | |
261 | ||
6bb92a16 LP |
262 | ask_password_agent_open(); |
263 | } | |
264 | ||
8a4b13c5 | 265 | static void polkit_agent_open_maybe(void) { |
6bb92a16 LP |
266 | /* Open the polkit agent as a child process if necessary */ |
267 | ||
6bb92a16 LP |
268 | if (arg_scope != UNIT_FILE_SYSTEM) |
269 | return; | |
270 | ||
8a4b13c5 | 271 | polkit_agent_open_if_enabled(arg_transport, arg_ask_password); |
501fc174 LP |
272 | } |
273 | ||
3c756001 LP |
274 | static OutputFlags get_output_flags(void) { |
275 | return | |
276 | arg_all * OUTPUT_SHOW_ALL | | |
459b9f9f | 277 | (arg_full || !on_tty() || pager_have()) * OUTPUT_FULL_WIDTH | |
40c9fe4c | 278 | colors_enabled() * OUTPUT_COLOR | |
8b0cc9a3 | 279 | !arg_quiet * OUTPUT_WARN_CUTOFF; |
3c756001 LP |
280 | } |
281 | ||
f459b602 | 282 | static int translate_bus_error_to_exit_status(int r, const sd_bus_error *error) { |
22f4096c LP |
283 | assert(error); |
284 | ||
f459b602 | 285 | if (!sd_bus_error_is_set(error)) |
22f4096c LP |
286 | return r; |
287 | ||
f459b602 MAP |
288 | if (sd_bus_error_has_name(error, SD_BUS_ERROR_ACCESS_DENIED) || |
289 | sd_bus_error_has_name(error, BUS_ERROR_ONLY_BY_DEPENDENCY) || | |
290 | sd_bus_error_has_name(error, BUS_ERROR_NO_ISOLATION) || | |
291 | sd_bus_error_has_name(error, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE)) | |
22f4096c LP |
292 | return EXIT_NOPERMISSION; |
293 | ||
f459b602 | 294 | if (sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT)) |
22f4096c LP |
295 | return EXIT_NOTINSTALLED; |
296 | ||
f459b602 | 297 | if (sd_bus_error_has_name(error, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE) || |
718db961 | 298 | sd_bus_error_has_name(error, SD_BUS_ERROR_NOT_SUPPORTED)) |
22f4096c LP |
299 | return EXIT_NOTIMPLEMENTED; |
300 | ||
f459b602 | 301 | if (sd_bus_error_has_name(error, BUS_ERROR_LOAD_FAILED)) |
22f4096c LP |
302 | return EXIT_NOTCONFIGURED; |
303 | ||
304 | if (r != 0) | |
305 | return r; | |
306 | ||
307 | return EXIT_FAILURE; | |
308 | } | |
309 | ||
4fbd7192 | 310 | static bool install_client_side(void) { |
4fbd7192 LP |
311 | /* Decides when to execute enable/disable/... operations |
312 | * client-side rather than server-side. */ | |
4f16c1f4 | 313 | |
f38951a6 | 314 | if (running_in_chroot_or_offline()) |
729e3769 LP |
315 | return true; |
316 | ||
317 | if (sd_booted() <= 0) | |
318 | return true; | |
319 | ||
320 | if (!isempty(arg_root)) | |
321 | return true; | |
322 | ||
323 | if (arg_scope == UNIT_FILE_GLOBAL) | |
324 | return true; | |
325 | ||
b41b9d2a LP |
326 | /* Unsupported environment variable, mostly for debugging purposes */ |
327 | if (getenv_bool("SYSTEMCTL_INSTALL_CLIENT_SIDE") > 0) | |
328 | return true; | |
329 | ||
729e3769 LP |
330 | return false; |
331 | } | |
332 | ||
93bab288 | 333 | static int compare_unit_info(const UnitInfo *a, const UnitInfo *b) { |
36c32ba2 | 334 | const char *d1, *d2; |
1238ee09 LP |
335 | int r; |
336 | ||
337 | /* First, order by machine */ | |
93bab288 | 338 | if (!a->machine && b->machine) |
1238ee09 | 339 | return -1; |
93bab288 | 340 | if (a->machine && !b->machine) |
1238ee09 | 341 | return 1; |
93bab288 YW |
342 | if (a->machine && b->machine) { |
343 | r = strcasecmp(a->machine, b->machine); | |
1238ee09 LP |
344 | if (r != 0) |
345 | return r; | |
346 | } | |
36c32ba2 | 347 | |
1238ee09 | 348 | /* Second, order by unit type */ |
93bab288 YW |
349 | d1 = strrchr(a->id, '.'); |
350 | d2 = strrchr(b->id, '.'); | |
36c32ba2 | 351 | if (d1 && d2) { |
f84190d8 LP |
352 | r = strcasecmp(d1, d2); |
353 | if (r != 0) | |
36c32ba2 LP |
354 | return r; |
355 | } | |
356 | ||
1238ee09 | 357 | /* Third, order by name */ |
93bab288 | 358 | return strcasecmp(a->id, b->id); |
36c32ba2 LP |
359 | } |
360 | ||
16484a8a ZJS |
361 | static const char* unit_type_suffix(const char *name) { |
362 | const char *dot; | |
363 | ||
364 | dot = strrchr(name, '.'); | |
365 | if (!dot) | |
366 | return ""; | |
367 | ||
368 | return dot + 1; | |
369 | } | |
370 | ||
d8fba7c6 | 371 | static bool output_show_unit(const UnitInfo *u, char **patterns) { |
f8591ee1 LP |
372 | assert(u); |
373 | ||
2404701e | 374 | if (!strv_fnmatch_or_empty(patterns, u->id, FNM_NOESCAPE)) |
d8fba7c6 | 375 | return false; |
d8fba7c6 | 376 | |
16484a8a ZJS |
377 | if (arg_types && !strv_find(arg_types, unit_type_suffix(u->id))) |
378 | return false; | |
6c71341a ZJS |
379 | |
380 | if (arg_all) | |
381 | return true; | |
382 | ||
409472cb FB |
383 | /* Note that '--all' is not purely a state filter, but also a |
384 | * filter that hides units that "follow" other units (which is | |
385 | * used for device units that appear under different names). */ | |
386 | if (!isempty(u->following)) | |
387 | return false; | |
388 | ||
ebc96265 FB |
389 | if (!strv_isempty(arg_states)) |
390 | return true; | |
391 | ||
392 | /* By default show all units except the ones in inactive | |
393 | * state and with no pending job */ | |
6c71341a ZJS |
394 | if (u->job_id > 0) |
395 | return true; | |
396 | ||
409472cb | 397 | if (streq(u->active_state, "inactive")) |
6c71341a ZJS |
398 | return false; |
399 | ||
400 | return true; | |
33330222 ZJS |
401 | } |
402 | ||
1238ee09 | 403 | static int output_units_list(const UnitInfo *unit_infos, unsigned c) { |
b5d7f1bb | 404 | unsigned circle_len = 0, id_len, max_id_len, load_len, active_len, sub_len, job_len, desc_len, max_desc_len; |
f459b602 | 405 | const UnitInfo *u; |
a1074881 | 406 | unsigned n_shown = 0; |
b5d7f1bb | 407 | int job_count = 0; |
0221d68a | 408 | bool full = arg_full || FLAGS_SET(arg_pager_flags, PAGER_DISABLE); |
33330222 | 409 | |
fbd0b64f LP |
410 | max_id_len = STRLEN("UNIT"); |
411 | load_len = STRLEN("LOAD"); | |
412 | active_len = STRLEN("ACTIVE"); | |
413 | sub_len = STRLEN("SUB"); | |
414 | job_len = STRLEN("JOB"); | |
415 | max_desc_len = STRLEN("DESCRIPTION"); | |
b036fc00 LP |
416 | |
417 | for (u = unit_infos; u < unit_infos + c; u++) { | |
1238ee09 | 418 | max_id_len = MAX(max_id_len, strlen(u->id) + (u->machine ? strlen(u->machine)+1 : 0)); |
a1074881 | 419 | load_len = MAX(load_len, strlen(u->load_state)); |
b036fc00 LP |
420 | active_len = MAX(active_len, strlen(u->active_state)); |
421 | sub_len = MAX(sub_len, strlen(u->sub_state)); | |
b5d7f1bb | 422 | max_desc_len = MAX(max_desc_len, strlen(u->description)); |
f459b602 | 423 | |
ccd41387 | 424 | if (u->job_id != 0) { |
b036fc00 | 425 | job_len = MAX(job_len, strlen(u->job_type)); |
ccd41387 ZJS |
426 | job_count++; |
427 | } | |
90c3f79d LP |
428 | |
429 | if (!arg_no_legend && | |
430 | (streq(u->active_state, "failed") || | |
c4555ad8 | 431 | STR_IN_SET(u->load_state, "error", "not-found", "bad-setting", "masked"))) |
90c3f79d | 432 | circle_len = 2; |
33330222 ZJS |
433 | } |
434 | ||
184ecaf7 | 435 | if (!arg_full && original_stdout_is_tty) { |
4deb3b93 | 436 | unsigned basic_len; |
cbc9fbd1 | 437 | |
b5d7f1bb | 438 | id_len = MIN(max_id_len, 25u); /* as much as it needs, but at most 25 for now */ |
a9bd056a | 439 | basic_len = circle_len + 1 + id_len + 1 + load_len + 1 + active_len + 1 + sub_len + 1; |
cbc9fbd1 | 440 | |
ccd41387 ZJS |
441 | if (job_count) |
442 | basic_len += job_len + 1; | |
cbc9fbd1 | 443 | |
4deb3b93 MS |
444 | if (basic_len < (unsigned) columns()) { |
445 | unsigned extra_len, incr; | |
446 | extra_len = columns() - basic_len; | |
cbc9fbd1 | 447 | |
4deb3b93 MS |
448 | /* Either UNIT already got 25, or is fully satisfied. |
449 | * Grant up to 25 to DESC now. */ | |
9607d947 | 450 | incr = MIN(extra_len, 25u); |
b5d7f1bb | 451 | desc_len = incr; |
4deb3b93 | 452 | extra_len -= incr; |
cbc9fbd1 | 453 | |
b5d7f1bb LP |
454 | /* Of the remainder give as much as the ID needs to the ID, and give the rest to the |
455 | * description but not more than it needs. */ | |
4deb3b93 | 456 | if (extra_len > 0) { |
b5d7f1bb | 457 | incr = MIN(max_id_len - id_len, extra_len); |
4deb3b93 | 458 | id_len += incr; |
b5d7f1bb | 459 | desc_len += MIN(extra_len - incr, max_desc_len - desc_len); |
4deb3b93 | 460 | } |
43479f8d ZJS |
461 | } else |
462 | desc_len = 0; | |
b5d7f1bb | 463 | } else { |
4deb3b93 | 464 | id_len = max_id_len; |
b5d7f1bb LP |
465 | desc_len = max_desc_len; |
466 | } | |
4deb3b93 | 467 | |
b036fc00 | 468 | for (u = unit_infos; u < unit_infos + c; u++) { |
1238ee09 | 469 | _cleanup_free_ char *e = NULL, *j = NULL; |
16484a8a | 470 | const char *on_underline = "", *off_underline = ""; |
90c3f79d LP |
471 | const char *on_loaded = "", *off_loaded = ""; |
472 | const char *on_active = "", *off_active = ""; | |
473 | const char *on_circle = "", *off_circle = ""; | |
1238ee09 | 474 | const char *id; |
16484a8a | 475 | bool circle = false, underline = false; |
b036fc00 | 476 | |
ad94ad63 | 477 | if (!n_shown && !arg_no_legend) { |
90c3f79d LP |
478 | |
479 | if (circle_len > 0) | |
480 | fputs(" ", stdout); | |
481 | ||
16484a8a ZJS |
482 | printf("%s%-*s %-*s %-*s %-*s ", |
483 | ansi_underline(), | |
cbc9fbd1 LP |
484 | id_len, "UNIT", |
485 | load_len, "LOAD", | |
486 | active_len, "ACTIVE", | |
487 | sub_len, "SUB"); | |
488 | ||
ccd41387 ZJS |
489 | if (job_count) |
490 | printf("%-*s ", job_len, "JOB"); | |
cbc9fbd1 | 491 | |
b5d7f1bb LP |
492 | printf("%-*.*s%s\n", |
493 | desc_len, | |
0221d68a | 494 | full ? -1 : (int) desc_len, |
16484a8a ZJS |
495 | "DESCRIPTION", |
496 | ansi_normal()); | |
ad94ad63 ZJS |
497 | } |
498 | ||
688c6725 LP |
499 | n_shown++; |
500 | ||
16484a8a ZJS |
501 | if (u + 1 < unit_infos + c && |
502 | !streq(unit_type_suffix(u->id), unit_type_suffix((u + 1)->id))) { | |
503 | on_underline = ansi_underline(); | |
504 | off_underline = ansi_normal(); | |
505 | underline = true; | |
506 | } | |
507 | ||
c4555ad8 | 508 | if (STR_IN_SET(u->load_state, "error", "not-found", "bad-setting", "masked") && !arg_plain) { |
90c3f79d | 509 | on_circle = ansi_highlight_yellow(); |
16484a8a | 510 | off_circle = ansi_normal(); |
90c3f79d | 511 | circle = true; |
16484a8a | 512 | on_loaded = underline ? ansi_highlight_red_underline() : ansi_highlight_red(); |
835a19e0 | 513 | off_loaded = underline ? on_underline : ansi_normal(); |
250ba664 | 514 | } else if (streq(u->active_state, "failed") && !arg_plain) { |
16484a8a ZJS |
515 | on_circle = ansi_highlight_red(); |
516 | off_circle = ansi_normal(); | |
90c3f79d | 517 | circle = true; |
16484a8a | 518 | on_active = underline ? ansi_highlight_red_underline() : ansi_highlight_red(); |
835a19e0 | 519 | off_active = underline ? on_underline : ansi_normal(); |
90c3f79d | 520 | } |
eb68c413 | 521 | |
1238ee09 | 522 | if (u->machine) { |
605405c6 | 523 | j = strjoin(u->machine, ":", u->id); |
1238ee09 LP |
524 | if (!j) |
525 | return log_oom(); | |
526 | ||
527 | id = j; | |
528 | } else | |
529 | id = u->id; | |
530 | ||
531 | if (arg_full) { | |
532 | e = ellipsize(id, id_len, 33); | |
533 | if (!e) | |
534 | return log_oom(); | |
535 | ||
536 | id = e; | |
537 | } | |
eb68c413 | 538 | |
90c3f79d | 539 | if (circle_len > 0) |
9a6f746f | 540 | printf("%s%s%s ", on_circle, circle ? special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE) : " ", off_circle); |
90c3f79d | 541 | |
16484a8a ZJS |
542 | printf("%s%s%-*s%s %s%-*s%s %s%-*s %-*s%s %-*s", |
543 | on_underline, | |
90c3f79d | 544 | on_active, id_len, id, off_active, |
a1074881 | 545 | on_loaded, load_len, u->load_state, off_loaded, |
b036fc00 LP |
546 | on_active, active_len, u->active_state, |
547 | sub_len, u->sub_state, off_active, | |
ccd41387 | 548 | job_count ? job_len + 1 : 0, u->job_id ? u->job_type : ""); |
cbc9fbd1 | 549 | |
b5d7f1bb LP |
550 | printf("%-*.*s%s\n", |
551 | desc_len, | |
0221d68a | 552 | full ? -1 : (int) desc_len, |
16484a8a ZJS |
553 | u->description, |
554 | off_underline); | |
eb68c413 ZJS |
555 | } |
556 | ||
ebed32bf | 557 | if (!arg_no_legend) { |
57f7ae4f ZJS |
558 | const char *on, *off; |
559 | ||
560 | if (n_shown) { | |
90c3f79d LP |
561 | puts("\n" |
562 | "LOAD = Reflects whether the unit definition was properly loaded.\n" | |
e3e0314b ZJS |
563 | "ACTIVE = The high-level unit activation state, i.e. generalization of SUB.\n" |
564 | "SUB = The low-level unit activation state, values depend on unit type."); | |
565 | puts(job_count ? "JOB = Pending job for the unit.\n" : ""); | |
0b5a519c | 566 | on = ansi_highlight(); |
1fc464f6 | 567 | off = ansi_normal(); |
57f7ae4f | 568 | } else { |
0b5a519c | 569 | on = ansi_highlight_red(); |
1fc464f6 | 570 | off = ansi_normal(); |
57f7ae4f | 571 | } |
eb68c413 ZJS |
572 | |
573 | if (arg_all) | |
48c2826b | 574 | printf("%s%u loaded units listed.%s\n" |
57f7ae4f ZJS |
575 | "To show all installed unit files use 'systemctl list-unit-files'.\n", |
576 | on, n_shown, off); | |
eb68c413 | 577 | else |
48c2826b | 578 | printf("%s%u loaded units listed.%s Pass --all to see loaded but inactive units, too.\n" |
57f7ae4f ZJS |
579 | "To show all installed unit files use 'systemctl list-unit-files'.\n", |
580 | on, n_shown, off); | |
eb68c413 | 581 | } |
1238ee09 LP |
582 | |
583 | return 0; | |
eb68c413 ZJS |
584 | } |
585 | ||
a00963a2 | 586 | static int get_unit_list( |
f459b602 | 587 | sd_bus *bus, |
1238ee09 LP |
588 | const char *machine, |
589 | char **patterns, | |
590 | UnitInfo **unit_infos, | |
591 | int c, | |
592 | sd_bus_message **_reply) { | |
a00963a2 | 593 | |
4afd3348 LP |
594 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
595 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
596 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
ca2d3784 | 597 | size_t size = c; |
1238ee09 | 598 | int r; |
f459b602 | 599 | UnitInfo u; |
313fe66f | 600 | bool fallback = false; |
7e4249b9 | 601 | |
265a7a2a | 602 | assert(bus); |
1238ee09 | 603 | assert(unit_infos); |
f459b602 | 604 | assert(_reply); |
1238ee09 | 605 | |
cdc06ed7 | 606 | r = sd_bus_message_new_method_call( |
f22f08cd | 607 | bus, |
cdc06ed7 | 608 | &m, |
f22f08cd SP |
609 | "org.freedesktop.systemd1", |
610 | "/org/freedesktop/systemd1", | |
611 | "org.freedesktop.systemd1.Manager", | |
313fe66f | 612 | "ListUnitsByPatterns"); |
cdc06ed7 DS |
613 | if (r < 0) |
614 | return bus_log_create_error(r); | |
615 | ||
616 | r = sd_bus_message_append_strv(m, arg_states); | |
617 | if (r < 0) | |
618 | return bus_log_create_error(r); | |
619 | ||
313fe66f | 620 | r = sd_bus_message_append_strv(m, patterns); |
621 | if (r < 0) | |
622 | return bus_log_create_error(r); | |
623 | ||
cdc06ed7 | 624 | r = sd_bus_call(bus, m, 0, &error, &reply); |
33d52725 ZJS |
625 | if (r < 0 && (sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_METHOD) || |
626 | sd_bus_error_has_name(&error, SD_BUS_ERROR_ACCESS_DENIED))) { | |
313fe66f | 627 | /* Fallback to legacy ListUnitsFiltered method */ |
628 | fallback = true; | |
629 | log_debug_errno(r, "Failed to list units: %s Falling back to ListUnitsFiltered method.", bus_error_message(&error, r)); | |
630 | m = sd_bus_message_unref(m); | |
631 | sd_bus_error_free(&error); | |
632 | ||
633 | r = sd_bus_message_new_method_call( | |
634 | bus, | |
635 | &m, | |
636 | "org.freedesktop.systemd1", | |
637 | "/org/freedesktop/systemd1", | |
638 | "org.freedesktop.systemd1.Manager", | |
639 | "ListUnitsFiltered"); | |
640 | if (r < 0) | |
641 | return bus_log_create_error(r); | |
642 | ||
643 | r = sd_bus_message_append_strv(m, arg_states); | |
644 | if (r < 0) | |
645 | return bus_log_create_error(r); | |
646 | ||
647 | r = sd_bus_call(bus, m, 0, &error, &reply); | |
648 | } | |
4c3e8e39 LP |
649 | if (r < 0) |
650 | return log_error_errno(r, "Failed to list units: %s", bus_error_message(&error, r)); | |
7e4249b9 | 651 | |
f459b602 MAP |
652 | r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssssouso)"); |
653 | if (r < 0) | |
654 | return bus_log_parse_error(r); | |
7e4249b9 | 655 | |
f459b602 | 656 | while ((r = bus_parse_unit_info(reply, &u)) > 0) { |
1238ee09 LP |
657 | u.machine = machine; |
658 | ||
313fe66f | 659 | if (!output_show_unit(&u, fallback ? patterns : NULL)) |
8d5ba5a9 | 660 | continue; |
7e4249b9 | 661 | |
1238ee09 | 662 | if (!GREEDY_REALLOC(*unit_infos, size, c+1)) |
f459b602 | 663 | return log_oom(); |
36c32ba2 | 664 | |
1238ee09 | 665 | (*unit_infos)[c++] = u; |
991f2a39 | 666 | } |
f459b602 MAP |
667 | if (r < 0) |
668 | return bus_log_parse_error(r); | |
991f2a39 | 669 | |
f459b602 MAP |
670 | r = sd_bus_message_exit_container(reply); |
671 | if (r < 0) | |
672 | return bus_log_parse_error(r); | |
673 | ||
1cc6c93a | 674 | *_reply = TAKE_PTR(reply); |
f459b602 | 675 | |
1238ee09 LP |
676 | return c; |
677 | } | |
678 | ||
679 | static void message_set_freep(Set **set) { | |
224b0e7a | 680 | set_free_with_destructor(*set, sd_bus_message_unref); |
1238ee09 LP |
681 | } |
682 | ||
683 | static int get_unit_list_recursive( | |
684 | sd_bus *bus, | |
685 | char **patterns, | |
686 | UnitInfo **_unit_infos, | |
687 | Set **_replies, | |
688 | char ***_machines) { | |
689 | ||
690 | _cleanup_free_ UnitInfo *unit_infos = NULL; | |
691 | _cleanup_(message_set_freep) Set *replies; | |
692 | sd_bus_message *reply; | |
693 | int c, r; | |
694 | ||
695 | assert(bus); | |
696 | assert(_replies); | |
697 | assert(_unit_infos); | |
698 | assert(_machines); | |
699 | ||
d5099efc | 700 | replies = set_new(NULL); |
1238ee09 LP |
701 | if (!replies) |
702 | return log_oom(); | |
703 | ||
704 | c = get_unit_list(bus, NULL, patterns, &unit_infos, 0, &reply); | |
705 | if (c < 0) | |
706 | return c; | |
707 | ||
708 | r = set_put(replies, reply); | |
709 | if (r < 0) { | |
710 | sd_bus_message_unref(reply); | |
4c3e8e39 | 711 | return log_oom(); |
1238ee09 LP |
712 | } |
713 | ||
714 | if (arg_recursive) { | |
715 | _cleanup_strv_free_ char **machines = NULL; | |
716 | char **i; | |
717 | ||
718 | r = sd_get_machine_names(&machines); | |
719 | if (r < 0) | |
4c3e8e39 | 720 | return log_error_errno(r, "Failed to get machine names: %m"); |
1238ee09 LP |
721 | |
722 | STRV_FOREACH(i, machines) { | |
4afd3348 | 723 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *container = NULL; |
1238ee09 LP |
724 | int k; |
725 | ||
de33fc62 | 726 | r = sd_bus_open_system_machine(&container, *i); |
1238ee09 | 727 | if (r < 0) { |
4c3e8e39 | 728 | log_warning_errno(r, "Failed to connect to container %s, ignoring: %m", *i); |
1238ee09 LP |
729 | continue; |
730 | } | |
731 | ||
732 | k = get_unit_list(container, *i, patterns, &unit_infos, c, &reply); | |
733 | if (k < 0) | |
734 | return k; | |
735 | ||
736 | c = k; | |
737 | ||
738 | r = set_put(replies, reply); | |
739 | if (r < 0) { | |
740 | sd_bus_message_unref(reply); | |
4c3e8e39 | 741 | return log_oom(); |
1238ee09 LP |
742 | } |
743 | } | |
744 | ||
1cc6c93a | 745 | *_machines = TAKE_PTR(machines); |
1238ee09 LP |
746 | } else |
747 | *_machines = NULL; | |
748 | ||
1cc6c93a | 749 | *_unit_infos = TAKE_PTR(unit_infos); |
f459b602 | 750 | |
1cc6c93a | 751 | *_replies = TAKE_PTR(replies); |
1238ee09 | 752 | |
f459b602 | 753 | return c; |
991f2a39 ZJS |
754 | } |
755 | ||
e449de87 | 756 | static int list_units(int argc, char *argv[], void *userdata) { |
f459b602 | 757 | _cleanup_free_ UnitInfo *unit_infos = NULL; |
1238ee09 LP |
758 | _cleanup_(message_set_freep) Set *replies = NULL; |
759 | _cleanup_strv_free_ char **machines = NULL; | |
4fbd7192 | 760 | sd_bus *bus; |
991f2a39 ZJS |
761 | int r; |
762 | ||
4fbd7192 LP |
763 | r = acquire_bus(BUS_MANAGER, &bus); |
764 | if (r < 0) | |
765 | return r; | |
766 | ||
0221d68a | 767 | (void) pager_open(arg_pager_flags); |
d2ad7e1b | 768 | |
e449de87 | 769 | r = get_unit_list_recursive(bus, strv_skip(argv, 1), &unit_infos, &replies, &machines); |
991f2a39 ZJS |
770 | if (r < 0) |
771 | return r; | |
772 | ||
93bab288 | 773 | typesafe_qsort(unit_infos, r, compare_unit_info); |
1238ee09 | 774 | return output_units_list(unit_infos, r); |
991f2a39 ZJS |
775 | } |
776 | ||
a00963a2 | 777 | static int get_triggered_units( |
f459b602 MAP |
778 | sd_bus *bus, |
779 | const char* path, | |
780 | char*** ret) { | |
a00963a2 | 781 | |
4afd3348 | 782 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
991f2a39 ZJS |
783 | int r; |
784 | ||
4fbd7192 LP |
785 | assert(bus); |
786 | assert(path); | |
787 | assert(ret); | |
788 | ||
f459b602 MAP |
789 | r = sd_bus_get_property_strv( |
790 | bus, | |
791 | "org.freedesktop.systemd1", | |
792 | path, | |
793 | "org.freedesktop.systemd1.Unit", | |
794 | "Triggers", | |
795 | &error, | |
796 | ret); | |
f459b602 | 797 | if (r < 0) |
691395d8 | 798 | return log_error_errno(r, "Failed to determine triggers: %s", bus_error_message(&error, r)); |
991f2a39 ZJS |
799 | |
800 | return 0; | |
801 | } | |
802 | ||
f459b602 MAP |
803 | static int get_listening( |
804 | sd_bus *bus, | |
805 | const char* unit_path, | |
cbb76c29 | 806 | char*** listening) { |
f459b602 | 807 | |
4afd3348 LP |
808 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
809 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
f459b602 | 810 | const char *type, *path; |
cbb76c29 | 811 | int r, n = 0; |
991f2a39 | 812 | |
f459b602 MAP |
813 | r = sd_bus_get_property( |
814 | bus, | |
815 | "org.freedesktop.systemd1", | |
816 | unit_path, | |
817 | "org.freedesktop.systemd1.Socket", | |
818 | "Listen", | |
819 | &error, | |
820 | &reply, | |
821 | "a(ss)"); | |
691395d8 LP |
822 | if (r < 0) |
823 | return log_error_errno(r, "Failed to get list of listening sockets: %s", bus_error_message(&error, r)); | |
991f2a39 | 824 | |
f459b602 MAP |
825 | r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ss)"); |
826 | if (r < 0) | |
827 | return bus_log_parse_error(r); | |
991f2a39 | 828 | |
f459b602 | 829 | while ((r = sd_bus_message_read(reply, "(ss)", &type, &path)) > 0) { |
36c32ba2 | 830 | |
0d95178e | 831 | r = strv_extend(listening, type); |
f459b602 MAP |
832 | if (r < 0) |
833 | return log_oom(); | |
991f2a39 | 834 | |
0d95178e | 835 | r = strv_extend(listening, path); |
f459b602 MAP |
836 | if (r < 0) |
837 | return log_oom(); | |
7e4249b9 | 838 | |
cbb76c29 | 839 | n++; |
36c32ba2 | 840 | } |
f459b602 MAP |
841 | if (r < 0) |
842 | return bus_log_parse_error(r); | |
843 | ||
844 | r = sd_bus_message_exit_container(reply); | |
845 | if (r < 0) | |
846 | return bus_log_parse_error(r); | |
36c32ba2 | 847 | |
cbb76c29 | 848 | return n; |
991f2a39 ZJS |
849 | } |
850 | ||
851 | struct socket_info { | |
0cfc3525 | 852 | const char *machine; |
991f2a39 ZJS |
853 | const char* id; |
854 | ||
855 | char* type; | |
856 | char* path; | |
857 | ||
858 | /* Note: triggered is a list here, although it almost certainly | |
859 | * will always be one unit. Nevertheless, dbus API allows for multiple | |
f7340ab2 | 860 | * values, so let's follow that. */ |
991f2a39 ZJS |
861 | char** triggered; |
862 | ||
863 | /* The strv above is shared. free is set only in the first one. */ | |
864 | bool own_triggered; | |
865 | }; | |
866 | ||
cbb76c29 | 867 | static int socket_info_compare(const struct socket_info *a, const struct socket_info *b) { |
93bab288 | 868 | int r; |
cbc9fbd1 | 869 | |
cbb76c29 LP |
870 | assert(a); |
871 | assert(b); | |
872 | ||
0cfc3525 TA |
873 | if (!a->machine && b->machine) |
874 | return -1; | |
875 | if (a->machine && !b->machine) | |
876 | return 1; | |
877 | if (a->machine && b->machine) { | |
93bab288 YW |
878 | r = strcasecmp(a->machine, b->machine); |
879 | if (r != 0) | |
880 | return r; | |
0cfc3525 TA |
881 | } |
882 | ||
93bab288 YW |
883 | r = strcmp(a->path, b->path); |
884 | if (r == 0) | |
885 | r = strcmp(a->type, b->type); | |
cbc9fbd1 | 886 | |
93bab288 | 887 | return r; |
991f2a39 ZJS |
888 | } |
889 | ||
890 | static int output_sockets_list(struct socket_info *socket_infos, unsigned cs) { | |
891 | struct socket_info *s; | |
fbd0b64f LP |
892 | unsigned pathlen = STRLEN("LISTEN"), |
893 | typelen = STRLEN("TYPE") * arg_show_types, | |
894 | socklen = STRLEN("UNIT"), | |
895 | servlen = STRLEN("ACTIVATES"); | |
991f2a39 ZJS |
896 | const char *on, *off; |
897 | ||
898 | for (s = socket_infos; s < socket_infos + cs; s++) { | |
991f2a39 | 899 | unsigned tmp = 0; |
cbc9fbd1 | 900 | char **a; |
991f2a39 ZJS |
901 | |
902 | socklen = MAX(socklen, strlen(s->id)); | |
903 | if (arg_show_types) | |
904 | typelen = MAX(typelen, strlen(s->type)); | |
0cfc3525 | 905 | pathlen = MAX(pathlen, strlen(s->path) + (s->machine ? strlen(s->machine)+1 : 0)); |
991f2a39 ZJS |
906 | |
907 | STRV_FOREACH(a, s->triggered) | |
908 | tmp += strlen(*a) + 2*(a != s->triggered); | |
909 | servlen = MAX(servlen, tmp); | |
910 | } | |
911 | ||
912 | if (cs) { | |
571bfc6c MM |
913 | if (!arg_no_legend) |
914 | printf("%-*s %-*.*s%-*s %s\n", | |
915 | pathlen, "LISTEN", | |
916 | typelen + arg_show_types, typelen + arg_show_types, "TYPE ", | |
917 | socklen, "UNIT", | |
918 | "ACTIVATES"); | |
991f2a39 ZJS |
919 | |
920 | for (s = socket_infos; s < socket_infos + cs; s++) { | |
0cfc3525 TA |
921 | _cleanup_free_ char *j = NULL; |
922 | const char *path; | |
991f2a39 ZJS |
923 | char **a; |
924 | ||
0cfc3525 | 925 | if (s->machine) { |
605405c6 | 926 | j = strjoin(s->machine, ":", s->path); |
0cfc3525 TA |
927 | if (!j) |
928 | return log_oom(); | |
929 | path = j; | |
930 | } else | |
931 | path = s->path; | |
932 | ||
991f2a39 ZJS |
933 | if (arg_show_types) |
934 | printf("%-*s %-*s %-*s", | |
0cfc3525 | 935 | pathlen, path, typelen, s->type, socklen, s->id); |
991f2a39 ZJS |
936 | else |
937 | printf("%-*s %-*s", | |
0cfc3525 | 938 | pathlen, path, socklen, s->id); |
991f2a39 ZJS |
939 | STRV_FOREACH(a, s->triggered) |
940 | printf("%s %s", | |
941 | a == s->triggered ? "" : ",", *a); | |
942 | printf("\n"); | |
943 | } | |
944 | ||
0b5a519c | 945 | on = ansi_highlight(); |
1fc464f6 | 946 | off = ansi_normal(); |
571bfc6c MM |
947 | if (!arg_no_legend) |
948 | printf("\n"); | |
991f2a39 | 949 | } else { |
0b5a519c | 950 | on = ansi_highlight_red(); |
1fc464f6 | 951 | off = ansi_normal(); |
991f2a39 ZJS |
952 | } |
953 | ||
571bfc6c MM |
954 | if (!arg_no_legend) { |
955 | printf("%s%u sockets listed.%s\n", on, cs, off); | |
956 | if (!arg_all) | |
957 | printf("Pass --all to see loaded but inactive sockets, too.\n"); | |
958 | } | |
265a7a2a ZJS |
959 | |
960 | return 0; | |
961 | } | |
962 | ||
e449de87 | 963 | static int list_sockets(int argc, char *argv[], void *userdata) { |
0cfc3525 TA |
964 | _cleanup_(message_set_freep) Set *replies = NULL; |
965 | _cleanup_strv_free_ char **machines = NULL; | |
f459b602 | 966 | _cleanup_free_ UnitInfo *unit_infos = NULL; |
8d5ba5a9 | 967 | _cleanup_free_ struct socket_info *socket_infos = NULL; |
f459b602 | 968 | const UnitInfo *u; |
991f2a39 | 969 | struct socket_info *s; |
cbb76c29 | 970 | unsigned cs = 0; |
991f2a39 | 971 | size_t size = 0; |
ad83b4c4 | 972 | int r = 0, n; |
4fbd7192 | 973 | sd_bus *bus; |
265a7a2a | 974 | |
4fbd7192 LP |
975 | r = acquire_bus(BUS_MANAGER, &bus); |
976 | if (r < 0) | |
977 | return r; | |
978 | ||
0221d68a | 979 | (void) pager_open(arg_pager_flags); |
d2ad7e1b | 980 | |
e449de87 | 981 | n = get_unit_list_recursive(bus, strv_skip(argv, 1), &unit_infos, &replies, &machines); |
cbb76c29 LP |
982 | if (n < 0) |
983 | return n; | |
265a7a2a | 984 | |
cbb76c29 | 985 | for (u = unit_infos; u < unit_infos + n; u++) { |
0d95178e | 986 | _cleanup_strv_free_ char **listening = NULL, **triggered = NULL; |
cbb76c29 | 987 | int i, c; |
991f2a39 | 988 | |
cbc9fbd1 | 989 | if (!endswith(u->id, ".socket")) |
991f2a39 ZJS |
990 | continue; |
991 | ||
992 | r = get_triggered_units(bus, u->unit_path, &triggered); | |
993 | if (r < 0) | |
994 | goto cleanup; | |
995 | ||
cbb76c29 LP |
996 | c = get_listening(bus, u->unit_path, &listening); |
997 | if (c < 0) { | |
998 | r = c; | |
991f2a39 | 999 | goto cleanup; |
cbb76c29 | 1000 | } |
991f2a39 ZJS |
1001 | |
1002 | if (!GREEDY_REALLOC(socket_infos, size, cs + c)) { | |
1003 | r = log_oom(); | |
1004 | goto cleanup; | |
1005 | } | |
1006 | ||
1007 | for (i = 0; i < c; i++) | |
1008 | socket_infos[cs + i] = (struct socket_info) { | |
0cfc3525 | 1009 | .machine = u->machine, |
991f2a39 | 1010 | .id = u->id, |
0d95178e KS |
1011 | .type = listening[i*2], |
1012 | .path = listening[i*2 + 1], | |
991f2a39 ZJS |
1013 | .triggered = triggered, |
1014 | .own_triggered = i==0, | |
1015 | }; | |
1016 | ||
1017 | /* from this point on we will cleanup those socket_infos */ | |
1018 | cs += c; | |
0d95178e KS |
1019 | free(listening); |
1020 | listening = triggered = NULL; /* avoid cleanup */ | |
991f2a39 ZJS |
1021 | } |
1022 | ||
93bab288 | 1023 | typesafe_qsort(socket_infos, cs, socket_info_compare); |
991f2a39 ZJS |
1024 | |
1025 | output_sockets_list(socket_infos, cs); | |
1026 | ||
1027 | cleanup: | |
1028 | assert(cs == 0 || socket_infos); | |
1029 | for (s = socket_infos; s < socket_infos + cs; s++) { | |
1030 | free(s->type); | |
1031 | free(s->path); | |
1032 | if (s->own_triggered) | |
1033 | strv_free(s->triggered); | |
1034 | } | |
4445a875 | 1035 | |
872c8faa | 1036 | return r; |
4445a875 LP |
1037 | } |
1038 | ||
cbb76c29 LP |
1039 | static int get_next_elapse( |
1040 | sd_bus *bus, | |
1041 | const char *path, | |
1042 | dual_timestamp *next) { | |
1043 | ||
4afd3348 | 1044 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
cbb76c29 LP |
1045 | dual_timestamp t; |
1046 | int r; | |
1047 | ||
1048 | assert(bus); | |
1049 | assert(path); | |
1050 | assert(next); | |
1051 | ||
1052 | r = sd_bus_get_property_trivial( | |
1053 | bus, | |
1054 | "org.freedesktop.systemd1", | |
1055 | path, | |
1056 | "org.freedesktop.systemd1.Timer", | |
1057 | "NextElapseUSecMonotonic", | |
1058 | &error, | |
1059 | 't', | |
1060 | &t.monotonic); | |
691395d8 | 1061 | if (r < 0) |
021723c6 | 1062 | return log_error_errno(r, "Failed to get next elapse time: %s", bus_error_message(&error, r)); |
cbb76c29 LP |
1063 | |
1064 | r = sd_bus_get_property_trivial( | |
1065 | bus, | |
1066 | "org.freedesktop.systemd1", | |
1067 | path, | |
1068 | "org.freedesktop.systemd1.Timer", | |
1069 | "NextElapseUSecRealtime", | |
1070 | &error, | |
1071 | 't', | |
1072 | &t.realtime); | |
691395d8 | 1073 | if (r < 0) |
021723c6 | 1074 | return log_error_errno(r, "Failed to get next elapse time: %s", bus_error_message(&error, r)); |
cbb76c29 LP |
1075 | |
1076 | *next = t; | |
1077 | return 0; | |
1078 | } | |
1079 | ||
d784e2db LP |
1080 | static int get_last_trigger( |
1081 | sd_bus *bus, | |
1082 | const char *path, | |
1083 | usec_t *last) { | |
1084 | ||
4afd3348 | 1085 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
d784e2db LP |
1086 | int r; |
1087 | ||
1088 | assert(bus); | |
1089 | assert(path); | |
1090 | assert(last); | |
1091 | ||
1092 | r = sd_bus_get_property_trivial( | |
1093 | bus, | |
1094 | "org.freedesktop.systemd1", | |
1095 | path, | |
1096 | "org.freedesktop.systemd1.Timer", | |
dedabea4 | 1097 | "LastTriggerUSec", |
d784e2db LP |
1098 | &error, |
1099 | 't', | |
1100 | last); | |
691395d8 LP |
1101 | if (r < 0) |
1102 | return log_error_errno(r, "Failed to get last trigger time: %s", bus_error_message(&error, r)); | |
d784e2db LP |
1103 | |
1104 | return 0; | |
1105 | } | |
1106 | ||
cbb76c29 | 1107 | struct timer_info { |
806a37e7 | 1108 | const char* machine; |
cbb76c29 LP |
1109 | const char* id; |
1110 | usec_t next_elapse; | |
d784e2db | 1111 | usec_t last_trigger; |
cbb76c29 LP |
1112 | char** triggered; |
1113 | }; | |
1114 | ||
1115 | static int timer_info_compare(const struct timer_info *a, const struct timer_info *b) { | |
93bab288 | 1116 | int r; |
806a37e7 | 1117 | |
cbb76c29 LP |
1118 | assert(a); |
1119 | assert(b); | |
1120 | ||
806a37e7 TA |
1121 | if (!a->machine && b->machine) |
1122 | return -1; | |
1123 | if (a->machine && !b->machine) | |
1124 | return 1; | |
1125 | if (a->machine && b->machine) { | |
93bab288 YW |
1126 | r = strcasecmp(a->machine, b->machine); |
1127 | if (r != 0) | |
1128 | return r; | |
806a37e7 TA |
1129 | } |
1130 | ||
93bab288 YW |
1131 | r = CMP(a->next_elapse, b->next_elapse); |
1132 | if (r != 0) | |
1133 | return r; | |
cbb76c29 LP |
1134 | |
1135 | return strcmp(a->id, b->id); | |
1136 | } | |
1137 | ||
1138 | static int output_timers_list(struct timer_info *timer_infos, unsigned n) { | |
1139 | struct timer_info *t; | |
1140 | unsigned | |
fbd0b64f LP |
1141 | nextlen = STRLEN("NEXT"), |
1142 | leftlen = STRLEN("LEFT"), | |
1143 | lastlen = STRLEN("LAST"), | |
1144 | passedlen = STRLEN("PASSED"), | |
1145 | unitlen = STRLEN("UNIT"), | |
1146 | activatelen = STRLEN("ACTIVATES"); | |
cbb76c29 LP |
1147 | |
1148 | const char *on, *off; | |
1149 | ||
1150 | assert(timer_infos || n == 0); | |
1151 | ||
1152 | for (t = timer_infos; t < timer_infos + n; t++) { | |
1153 | unsigned ul = 0; | |
1154 | char **a; | |
1155 | ||
1156 | if (t->next_elapse > 0) { | |
1157 | char tstamp[FORMAT_TIMESTAMP_MAX] = "", trel[FORMAT_TIMESTAMP_RELATIVE_MAX] = ""; | |
1158 | ||
1159 | format_timestamp(tstamp, sizeof(tstamp), t->next_elapse); | |
1160 | nextlen = MAX(nextlen, strlen(tstamp) + 1); | |
1161 | ||
1162 | format_timestamp_relative(trel, sizeof(trel), t->next_elapse); | |
1163 | leftlen = MAX(leftlen, strlen(trel)); | |
1164 | } | |
1165 | ||
d784e2db LP |
1166 | if (t->last_trigger > 0) { |
1167 | char tstamp[FORMAT_TIMESTAMP_MAX] = "", trel[FORMAT_TIMESTAMP_RELATIVE_MAX] = ""; | |
1168 | ||
1169 | format_timestamp(tstamp, sizeof(tstamp), t->last_trigger); | |
1170 | lastlen = MAX(lastlen, strlen(tstamp) + 1); | |
1171 | ||
1172 | format_timestamp_relative(trel, sizeof(trel), t->last_trigger); | |
1173 | passedlen = MAX(passedlen, strlen(trel)); | |
1174 | } | |
1175 | ||
806a37e7 | 1176 | unitlen = MAX(unitlen, strlen(t->id) + (t->machine ? strlen(t->machine)+1 : 0)); |
cbb76c29 LP |
1177 | |
1178 | STRV_FOREACH(a, t->triggered) | |
1179 | ul += strlen(*a) + 2*(a != t->triggered); | |
d784e2db | 1180 | |
cbb76c29 LP |
1181 | activatelen = MAX(activatelen, ul); |
1182 | } | |
1183 | ||
1184 | if (n > 0) { | |
1185 | if (!arg_no_legend) | |
d784e2db LP |
1186 | printf("%-*s %-*s %-*s %-*s %-*s %s\n", |
1187 | nextlen, "NEXT", | |
1188 | leftlen, "LEFT", | |
1189 | lastlen, "LAST", | |
1190 | passedlen, "PASSED", | |
1191 | unitlen, "UNIT", | |
1192 | "ACTIVATES"); | |
cbb76c29 LP |
1193 | |
1194 | for (t = timer_infos; t < timer_infos + n; t++) { | |
806a37e7 TA |
1195 | _cleanup_free_ char *j = NULL; |
1196 | const char *unit; | |
d784e2db LP |
1197 | char tstamp1[FORMAT_TIMESTAMP_MAX] = "n/a", trel1[FORMAT_TIMESTAMP_RELATIVE_MAX] = "n/a"; |
1198 | char tstamp2[FORMAT_TIMESTAMP_MAX] = "n/a", trel2[FORMAT_TIMESTAMP_RELATIVE_MAX] = "n/a"; | |
cbb76c29 LP |
1199 | char **a; |
1200 | ||
d784e2db LP |
1201 | format_timestamp(tstamp1, sizeof(tstamp1), t->next_elapse); |
1202 | format_timestamp_relative(trel1, sizeof(trel1), t->next_elapse); | |
cbb76c29 | 1203 | |
d784e2db LP |
1204 | format_timestamp(tstamp2, sizeof(tstamp2), t->last_trigger); |
1205 | format_timestamp_relative(trel2, sizeof(trel2), t->last_trigger); | |
1206 | ||
806a37e7 | 1207 | if (t->machine) { |
605405c6 | 1208 | j = strjoin(t->machine, ":", t->id); |
806a37e7 TA |
1209 | if (!j) |
1210 | return log_oom(); | |
1211 | unit = j; | |
1212 | } else | |
1213 | unit = t->id; | |
1214 | ||
d784e2db | 1215 | printf("%-*s %-*s %-*s %-*s %-*s", |
806a37e7 | 1216 | nextlen, tstamp1, leftlen, trel1, lastlen, tstamp2, passedlen, trel2, unitlen, unit); |
cbb76c29 LP |
1217 | |
1218 | STRV_FOREACH(a, t->triggered) | |
1219 | printf("%s %s", | |
1220 | a == t->triggered ? "" : ",", *a); | |
1221 | printf("\n"); | |
1222 | } | |
1223 | ||
1224 | on = ansi_highlight(); | |
1fc464f6 | 1225 | off = ansi_normal(); |
cbb76c29 LP |
1226 | if (!arg_no_legend) |
1227 | printf("\n"); | |
1228 | } else { | |
1229 | on = ansi_highlight_red(); | |
1fc464f6 | 1230 | off = ansi_normal(); |
cbb76c29 LP |
1231 | } |
1232 | ||
1233 | if (!arg_no_legend) { | |
1234 | printf("%s%u timers listed.%s\n", on, n, off); | |
1235 | if (!arg_all) | |
1236 | printf("Pass --all to see loaded but inactive timers, too.\n"); | |
1237 | } | |
1238 | ||
1239 | return 0; | |
1240 | } | |
1241 | ||
f5080e73 DH |
1242 | static usec_t calc_next_elapse(dual_timestamp *nw, dual_timestamp *next) { |
1243 | usec_t next_elapse; | |
1244 | ||
1245 | assert(nw); | |
1246 | assert(next); | |
1247 | ||
3a43da28 | 1248 | if (next->monotonic != USEC_INFINITY && next->monotonic > 0) { |
f5080e73 DH |
1249 | usec_t converted; |
1250 | ||
1251 | if (next->monotonic > nw->monotonic) | |
1252 | converted = nw->realtime + (next->monotonic - nw->monotonic); | |
1253 | else | |
1254 | converted = nw->realtime - (nw->monotonic - next->monotonic); | |
1255 | ||
3a43da28 | 1256 | if (next->realtime != USEC_INFINITY && next->realtime > 0) |
f5080e73 DH |
1257 | next_elapse = MIN(converted, next->realtime); |
1258 | else | |
1259 | next_elapse = converted; | |
1260 | ||
1261 | } else | |
1262 | next_elapse = next->realtime; | |
1263 | ||
1264 | return next_elapse; | |
1265 | } | |
1266 | ||
e449de87 | 1267 | static int list_timers(int argc, char *argv[], void *userdata) { |
806a37e7 TA |
1268 | _cleanup_(message_set_freep) Set *replies = NULL; |
1269 | _cleanup_strv_free_ char **machines = NULL; | |
cbb76c29 LP |
1270 | _cleanup_free_ struct timer_info *timer_infos = NULL; |
1271 | _cleanup_free_ UnitInfo *unit_infos = NULL; | |
1272 | struct timer_info *t; | |
1273 | const UnitInfo *u; | |
1274 | size_t size = 0; | |
1823b86e | 1275 | int n, c = 0; |
cbb76c29 | 1276 | dual_timestamp nw; |
4fbd7192 | 1277 | sd_bus *bus; |
1823b86e | 1278 | int r = 0; |
cbb76c29 | 1279 | |
4fbd7192 LP |
1280 | r = acquire_bus(BUS_MANAGER, &bus); |
1281 | if (r < 0) | |
1282 | return r; | |
1283 | ||
0221d68a | 1284 | (void) pager_open(arg_pager_flags); |
d2ad7e1b | 1285 | |
e449de87 | 1286 | n = get_unit_list_recursive(bus, strv_skip(argv, 1), &unit_infos, &replies, &machines); |
cbb76c29 LP |
1287 | if (n < 0) |
1288 | return n; | |
1289 | ||
1290 | dual_timestamp_get(&nw); | |
1291 | ||
1292 | for (u = unit_infos; u < unit_infos + n; u++) { | |
1293 | _cleanup_strv_free_ char **triggered = NULL; | |
5cb14b37 | 1294 | dual_timestamp next = DUAL_TIMESTAMP_NULL; |
d784e2db | 1295 | usec_t m, last = 0; |
cbb76c29 | 1296 | |
cbb76c29 LP |
1297 | if (!endswith(u->id, ".timer")) |
1298 | continue; | |
1299 | ||
1300 | r = get_triggered_units(bus, u->unit_path, &triggered); | |
1301 | if (r < 0) | |
1302 | goto cleanup; | |
1303 | ||
1304 | r = get_next_elapse(bus, u->unit_path, &next); | |
1305 | if (r < 0) | |
1306 | goto cleanup; | |
1307 | ||
d784e2db LP |
1308 | get_last_trigger(bus, u->unit_path, &last); |
1309 | ||
cbb76c29 LP |
1310 | if (!GREEDY_REALLOC(timer_infos, size, c+1)) { |
1311 | r = log_oom(); | |
1312 | goto cleanup; | |
1313 | } | |
1314 | ||
f5080e73 DH |
1315 | m = calc_next_elapse(&nw, &next); |
1316 | ||
cbb76c29 | 1317 | timer_infos[c++] = (struct timer_info) { |
806a37e7 | 1318 | .machine = u->machine, |
cbb76c29 LP |
1319 | .id = u->id, |
1320 | .next_elapse = m, | |
d784e2db | 1321 | .last_trigger = last, |
1cc6c93a | 1322 | .triggered = TAKE_PTR(triggered), |
cbb76c29 | 1323 | }; |
cbb76c29 LP |
1324 | } |
1325 | ||
93bab288 | 1326 | typesafe_qsort(timer_infos, c, timer_info_compare); |
cbb76c29 LP |
1327 | |
1328 | output_timers_list(timer_infos, c); | |
1329 | ||
1330 | cleanup: | |
1331 | for (t = timer_infos; t < timer_infos + c; t++) | |
1332 | strv_free(t->triggered); | |
1333 | ||
1334 | return r; | |
1335 | } | |
1336 | ||
93bab288 | 1337 | static int compare_unit_file_list(const UnitFileList *a, const UnitFileList *b) { |
729e3769 | 1338 | const char *d1, *d2; |
729e3769 | 1339 | |
93bab288 YW |
1340 | d1 = strrchr(a->path, '.'); |
1341 | d2 = strrchr(b->path, '.'); | |
729e3769 LP |
1342 | |
1343 | if (d1 && d2) { | |
1344 | int r; | |
1345 | ||
1346 | r = strcasecmp(d1, d2); | |
1347 | if (r != 0) | |
1348 | return r; | |
1349 | } | |
1350 | ||
93bab288 | 1351 | return strcasecmp(basename(a->path), basename(b->path)); |
729e3769 LP |
1352 | } |
1353 | ||
313fe66f | 1354 | static bool output_show_unit_file(const UnitFileList *u, char **states, char **patterns) { |
f8591ee1 LP |
1355 | assert(u); |
1356 | ||
2404701e | 1357 | if (!strv_fnmatch_or_empty(patterns, basename(u->path), FNM_NOESCAPE)) |
d8fba7c6 | 1358 | return false; |
d8fba7c6 | 1359 | |
6c71341a ZJS |
1360 | if (!strv_isempty(arg_types)) { |
1361 | const char *dot; | |
1362 | ||
1363 | dot = strrchr(u->path, '.'); | |
1364 | if (!dot) | |
1365 | return false; | |
1366 | ||
1367 | if (!strv_find(arg_types, dot+1)) | |
1368 | return false; | |
1369 | } | |
1370 | ||
313fe66f | 1371 | if (!strv_isempty(states) && |
1372 | !strv_find(states, unit_file_state_to_string(u->state))) | |
bceccd5e | 1373 | return false; |
fec1530e | 1374 | |
6c71341a | 1375 | return true; |
729e3769 LP |
1376 | } |
1377 | ||
8d5ba5a9 | 1378 | static void output_unit_file_list(const UnitFileList *units, unsigned c) { |
0d292f5e | 1379 | unsigned max_id_len, id_cols, state_cols; |
729e3769 LP |
1380 | const UnitFileList *u; |
1381 | ||
fbd0b64f LP |
1382 | max_id_len = STRLEN("UNIT FILE"); |
1383 | state_cols = STRLEN("STATE"); | |
cbc9fbd1 | 1384 | |
1c0a113f | 1385 | for (u = units; u < units + c; u++) { |
2b6bf07d | 1386 | max_id_len = MAX(max_id_len, strlen(basename(u->path))); |
1c0a113f ZJS |
1387 | state_cols = MAX(state_cols, strlen(unit_file_state_to_string(u->state))); |
1388 | } | |
1389 | ||
1390 | if (!arg_full) { | |
1391 | unsigned basic_cols; | |
cbc9fbd1 | 1392 | |
9607d947 | 1393 | id_cols = MIN(max_id_len, 25u); |
1c0a113f ZJS |
1394 | basic_cols = 1 + id_cols + state_cols; |
1395 | if (basic_cols < (unsigned) columns()) | |
1396 | id_cols += MIN(columns() - basic_cols, max_id_len - id_cols); | |
1397 | } else | |
1398 | id_cols = max_id_len; | |
1399 | ||
11690bcc | 1400 | if (!arg_no_legend && c > 0) |
6ae3e62a ZJS |
1401 | printf("%s%-*s %-*s%s\n", |
1402 | ansi_underline(), | |
cbc9fbd1 | 1403 | id_cols, "UNIT FILE", |
6ae3e62a ZJS |
1404 | state_cols, "STATE", |
1405 | ansi_normal()); | |
729e3769 LP |
1406 | |
1407 | for (u = units; u < units + c; u++) { | |
aaa6732d | 1408 | const char *on_underline = NULL, *on_color = NULL, *off = NULL, *id; |
7fd1b19b | 1409 | _cleanup_free_ char *e = NULL; |
aaa6732d LP |
1410 | bool underline; |
1411 | ||
1412 | underline = u + 1 < units + c && | |
1413 | !streq(unit_type_suffix(u->path), unit_type_suffix((u + 1)->path)); | |
6ae3e62a | 1414 | |
aaa6732d | 1415 | if (underline) |
6ae3e62a | 1416 | on_underline = ansi_underline(); |
729e3769 | 1417 | |
4c315c2c IS |
1418 | if (IN_SET(u->state, |
1419 | UNIT_FILE_MASKED, | |
1420 | UNIT_FILE_MASKED_RUNTIME, | |
1421 | UNIT_FILE_DISABLED, | |
6ae3e62a | 1422 | UNIT_FILE_BAD)) |
aaa6732d | 1423 | on_color = underline ? ansi_highlight_red_underline() : ansi_highlight_red(); |
6ae3e62a | 1424 | else if (u->state == UNIT_FILE_ENABLED) |
aaa6732d LP |
1425 | on_color = underline ? ansi_highlight_green_underline() : ansi_highlight_green(); |
1426 | ||
1427 | if (on_underline || on_color) | |
1428 | off = ansi_normal(); | |
729e3769 | 1429 | |
2b6bf07d | 1430 | id = basename(u->path); |
729e3769 | 1431 | |
1c0a113f | 1432 | e = arg_full ? NULL : ellipsize(id, id_cols, 33); |
729e3769 | 1433 | |
aaa6732d LP |
1434 | printf("%s%-*s %s%-*s%s\n", |
1435 | strempty(on_underline), | |
1c0a113f | 1436 | id_cols, e ? e : id, |
aaa6732d | 1437 | strempty(on_color), state_cols, unit_file_state_to_string(u->state), strempty(off)); |
729e3769 LP |
1438 | } |
1439 | ||
1c0a113f | 1440 | if (!arg_no_legend) |
0d292f5e | 1441 | printf("\n%u unit files listed.\n", c); |
729e3769 LP |
1442 | } |
1443 | ||
e449de87 | 1444 | static int list_unit_files(int argc, char *argv[], void *userdata) { |
4afd3348 | 1445 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
f84190d8 | 1446 | _cleanup_free_ UnitFileList *units = NULL; |
8d5ba5a9 ZJS |
1447 | UnitFileList *unit; |
1448 | size_t size = 0; | |
f459b602 MAP |
1449 | unsigned c = 0; |
1450 | const char *state; | |
1451 | char *path; | |
f84190d8 | 1452 | int r; |
313fe66f | 1453 | bool fallback = false; |
729e3769 | 1454 | |
4fbd7192 | 1455 | if (install_client_side()) { |
729e3769 LP |
1456 | Hashmap *h; |
1457 | UnitFileList *u; | |
1458 | Iterator i; | |
f459b602 | 1459 | unsigned n_units; |
729e3769 | 1460 | |
d5099efc | 1461 | h = hashmap_new(&string_hash_ops); |
0d0f0c50 SL |
1462 | if (!h) |
1463 | return log_oom(); | |
729e3769 | 1464 | |
313fe66f | 1465 | r = unit_file_get_list(arg_scope, arg_root, h, arg_states, strv_skip(argv, 1)); |
729e3769 | 1466 | if (r < 0) { |
8ea913b2 | 1467 | unit_file_list_free(h); |
691395d8 | 1468 | return log_error_errno(r, "Failed to get unit file list: %m"); |
729e3769 LP |
1469 | } |
1470 | ||
1471 | n_units = hashmap_size(h); | |
fdbdf6ec | 1472 | |
0da999fa ZJS |
1473 | units = new(UnitFileList, n_units ?: 1); /* avoid malloc(0) */ |
1474 | if (!units) { | |
729e3769 | 1475 | unit_file_list_free(h); |
0d0f0c50 | 1476 | return log_oom(); |
729e3769 LP |
1477 | } |
1478 | ||
1479 | HASHMAP_FOREACH(u, h, i) { | |
313fe66f | 1480 | if (!output_show_unit_file(u, NULL, NULL)) |
8d5ba5a9 ZJS |
1481 | continue; |
1482 | ||
1483 | units[c++] = *u; | |
729e3769 LP |
1484 | free(u); |
1485 | } | |
1486 | ||
8d5ba5a9 | 1487 | assert(c <= n_units); |
729e3769 | 1488 | hashmap_free(h); |
5f056378 CH |
1489 | |
1490 | r = 0; | |
729e3769 | 1491 | } else { |
313fe66f | 1492 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
4afd3348 | 1493 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
4fbd7192 LP |
1494 | sd_bus *bus; |
1495 | ||
1496 | r = acquire_bus(BUS_MANAGER, &bus); | |
1497 | if (r < 0) | |
1498 | return r; | |
6e646d22 | 1499 | |
313fe66f | 1500 | r = sd_bus_message_new_method_call( |
f22f08cd | 1501 | bus, |
313fe66f | 1502 | &m, |
729e3769 LP |
1503 | "org.freedesktop.systemd1", |
1504 | "/org/freedesktop/systemd1", | |
1505 | "org.freedesktop.systemd1.Manager", | |
313fe66f | 1506 | "ListUnitFilesByPatterns"); |
1507 | if (r < 0) | |
1508 | return bus_log_create_error(r); | |
1509 | ||
1510 | r = sd_bus_message_append_strv(m, arg_states); | |
1511 | if (r < 0) | |
1512 | return bus_log_create_error(r); | |
1513 | ||
1514 | r = sd_bus_message_append_strv(m, strv_skip(argv, 1)); | |
1515 | if (r < 0) | |
1516 | return bus_log_create_error(r); | |
1517 | ||
1518 | r = sd_bus_call(bus, m, 0, &error, &reply); | |
1519 | if (r < 0 && sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_METHOD)) { | |
1520 | /* Fallback to legacy ListUnitFiles method */ | |
1521 | fallback = true; | |
1522 | log_debug_errno(r, "Failed to list unit files: %s Falling back to ListUnitsFiles method.", bus_error_message(&error, r)); | |
1523 | m = sd_bus_message_unref(m); | |
1524 | sd_bus_error_free(&error); | |
1525 | ||
1526 | r = sd_bus_message_new_method_call( | |
1527 | bus, | |
1528 | &m, | |
1529 | "org.freedesktop.systemd1", | |
1530 | "/org/freedesktop/systemd1", | |
1531 | "org.freedesktop.systemd1.Manager", | |
1532 | "ListUnitFiles"); | |
1533 | if (r < 0) | |
1534 | return bus_log_create_error(r); | |
1535 | ||
1536 | r = sd_bus_call(bus, m, 0, &error, &reply); | |
1537 | } | |
691395d8 LP |
1538 | if (r < 0) |
1539 | return log_error_errno(r, "Failed to list unit files: %s", bus_error_message(&error, r)); | |
729e3769 | 1540 | |
f459b602 MAP |
1541 | r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ss)"); |
1542 | if (r < 0) | |
1543 | return bus_log_parse_error(r); | |
729e3769 | 1544 | |
f459b602 | 1545 | while ((r = sd_bus_message_read(reply, "(ss)", &path, &state)) > 0) { |
729e3769 | 1546 | |
f459b602 MAP |
1547 | if (!GREEDY_REALLOC(units, size, c + 1)) |
1548 | return log_oom(); | |
729e3769 | 1549 | |
8d5ba5a9 | 1550 | units[c] = (struct UnitFileList) { |
f459b602 MAP |
1551 | path, |
1552 | unit_file_state_from_string(state) | |
1553 | }; | |
8d5ba5a9 | 1554 | |
313fe66f | 1555 | if (output_show_unit_file(&units[c], |
1556 | fallback ? arg_states : NULL, | |
1557 | fallback ? strv_skip(argv, 1) : NULL)) | |
313cefa1 | 1558 | c++; |
8d5ba5a9 | 1559 | |
729e3769 | 1560 | } |
f459b602 MAP |
1561 | if (r < 0) |
1562 | return bus_log_parse_error(r); | |
1563 | ||
1564 | r = sd_bus_message_exit_container(reply); | |
1565 | if (r < 0) | |
1566 | return bus_log_parse_error(r); | |
729e3769 LP |
1567 | } |
1568 | ||
0221d68a | 1569 | (void) pager_open(arg_pager_flags); |
d2ad7e1b | 1570 | |
93bab288 | 1571 | typesafe_qsort(units, c, compare_unit_file_list); |
4fe1be9c | 1572 | output_unit_file_list(units, c); |
729e3769 | 1573 | |
0da999fa | 1574 | if (install_client_side()) |
8d5ba5a9 ZJS |
1575 | for (unit = units; unit < units + c; unit++) |
1576 | free(unit->path); | |
1577 | ||
f84190d8 | 1578 | return 0; |
729e3769 LP |
1579 | } |
1580 | ||
14cb109d | 1581 | static int list_dependencies_print(const char *name, int level, unsigned branches, bool last) { |
55c0b89c | 1582 | _cleanup_free_ char *n = NULL; |
9607d947 | 1583 | size_t max_len = MAX(columns(),20u); |
cbc9fbd1 LP |
1584 | size_t len = 0; |
1585 | int i; | |
55c0b89c | 1586 | |
5d0c05e5 | 1587 | if (!arg_plain) { |
cbc9fbd1 | 1588 | |
5d0c05e5 LN |
1589 | for (i = level - 1; i >= 0; i--) { |
1590 | len += 2; | |
f168c273 | 1591 | if (len > max_len - 3 && !arg_full) { |
5d0c05e5 LN |
1592 | printf("%s...\n",max_len % 2 ? "" : " "); |
1593 | return 0; | |
1594 | } | |
9a6f746f | 1595 | printf("%s", special_glyph(branches & (1 << i) ? SPECIAL_GLYPH_TREE_VERTICAL : SPECIAL_GLYPH_TREE_SPACE)); |
5d0c05e5 | 1596 | } |
55c0b89c | 1597 | len += 2; |
cbc9fbd1 | 1598 | |
f168c273 | 1599 | if (len > max_len - 3 && !arg_full) { |
55c0b89c LN |
1600 | printf("%s...\n",max_len % 2 ? "" : " "); |
1601 | return 0; | |
1602 | } | |
cbc9fbd1 | 1603 | |
9a6f746f | 1604 | printf("%s", special_glyph(last ? SPECIAL_GLYPH_TREE_RIGHT : SPECIAL_GLYPH_TREE_BRANCH)); |
55c0b89c | 1605 | } |
55c0b89c | 1606 | |
9ed794a3 | 1607 | if (arg_full) { |
55c0b89c LN |
1608 | printf("%s\n", name); |
1609 | return 0; | |
1610 | } | |
1611 | ||
1612 | n = ellipsize(name, max_len-len, 100); | |
f168c273 | 1613 | if (!n) |
55c0b89c LN |
1614 | return log_oom(); |
1615 | ||
1616 | printf("%s\n", n); | |
1617 | return 0; | |
1618 | } | |
1619 | ||
f459b602 | 1620 | static int list_dependencies_get_dependencies(sd_bus *bus, const char *name, char ***deps) { |
32b47279 YW |
1621 | struct DependencyStatusInfo { |
1622 | char **dep[5]; | |
1623 | } info = {}; | |
1624 | ||
1625 | static const struct bus_properties_map map[_DEPENDENCY_MAX][6] = { | |
1626 | [DEPENDENCY_FORWARD] = { | |
1627 | { "Requires", "as", NULL, offsetof(struct DependencyStatusInfo, dep[0]) }, | |
1628 | { "Requisite", "as", NULL, offsetof(struct DependencyStatusInfo, dep[1]) }, | |
1629 | { "Wants", "as", NULL, offsetof(struct DependencyStatusInfo, dep[2]) }, | |
1630 | { "ConsistsOf", "as", NULL, offsetof(struct DependencyStatusInfo, dep[3]) }, | |
1631 | { "BindsTo", "as", NULL, offsetof(struct DependencyStatusInfo, dep[4]) }, | |
1632 | {} | |
1633 | }, | |
1634 | [DEPENDENCY_REVERSE] = { | |
1635 | { "RequiredBy", "as", NULL, offsetof(struct DependencyStatusInfo, dep[0]) }, | |
1636 | { "RequisiteOf", "as", NULL, offsetof(struct DependencyStatusInfo, dep[1]) }, | |
1637 | { "WantedBy", "as", NULL, offsetof(struct DependencyStatusInfo, dep[2]) }, | |
1638 | { "PartOf", "as", NULL, offsetof(struct DependencyStatusInfo, dep[3]) }, | |
1639 | { "BoundBy", "as", NULL, offsetof(struct DependencyStatusInfo, dep[4]) }, | |
1640 | {} | |
1641 | }, | |
1642 | [DEPENDENCY_AFTER] = { | |
1643 | { "After", "as", NULL, offsetof(struct DependencyStatusInfo, dep[0]) }, | |
1644 | {} | |
1645 | }, | |
1646 | [DEPENDENCY_BEFORE] = { | |
1647 | { "Before", "as", NULL, offsetof(struct DependencyStatusInfo, dep[0]) }, | |
1648 | {} | |
1649 | }, | |
afba4199 | 1650 | }; |
55c0b89c | 1651 | |
4afd3348 | 1652 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
f459b602 MAP |
1653 | _cleanup_strv_free_ char **ret = NULL; |
1654 | _cleanup_free_ char *path = NULL; | |
32b47279 | 1655 | int i, r; |
55c0b89c LN |
1656 | |
1657 | assert(bus); | |
1658 | assert(name); | |
1659 | assert(deps); | |
1660 | ||
1661 | path = unit_dbus_path_from_name(name); | |
f459b602 MAP |
1662 | if (!path) |
1663 | return log_oom(); | |
55c0b89c | 1664 | |
32b47279 YW |
1665 | r = bus_map_all_properties(bus, |
1666 | "org.freedesktop.systemd1", | |
1667 | path, | |
1668 | map[arg_dependency], | |
f39cbb6e | 1669 | BUS_MAP_STRDUP, |
32b47279 | 1670 | &error, |
f37f8a61 | 1671 | NULL, |
32b47279 | 1672 | &info); |
691395d8 LP |
1673 | if (r < 0) |
1674 | return log_error_errno(r, "Failed to get properties of %s: %s", name, bus_error_message(&error, r)); | |
55c0b89c | 1675 | |
32b47279 YW |
1676 | if (IN_SET(arg_dependency, DEPENDENCY_AFTER, DEPENDENCY_BEFORE)) { |
1677 | *deps = info.dep[0]; | |
1678 | return 0; | |
1679 | } | |
55c0b89c | 1680 | |
32b47279 YW |
1681 | for (i = 0; i < 5; i++) { |
1682 | r = strv_extend_strv(&ret, info.dep[i], true); | |
f459b602 | 1683 | if (r < 0) |
32b47279 YW |
1684 | return log_oom(); |
1685 | info.dep[i] = strv_free(info.dep[i]); | |
f459b602 | 1686 | } |
540e7dbe | 1687 | |
1cc6c93a | 1688 | *deps = TAKE_PTR(ret); |
540e7dbe | 1689 | |
f459b602 | 1690 | return 0; |
55c0b89c LN |
1691 | } |
1692 | ||
93bab288 | 1693 | static int list_dependencies_compare(char * const *a, char * const *b) { |
55c0b89c LN |
1694 | if (unit_name_to_type(*a) == UNIT_TARGET && unit_name_to_type(*b) != UNIT_TARGET) |
1695 | return 1; | |
1696 | if (unit_name_to_type(*a) != UNIT_TARGET && unit_name_to_type(*b) == UNIT_TARGET) | |
1697 | return -1; | |
cbc9fbd1 | 1698 | |
55c0b89c LN |
1699 | return strcasecmp(*a, *b); |
1700 | } | |
1701 | ||
f459b602 MAP |
1702 | static int list_dependencies_one( |
1703 | sd_bus *bus, | |
1704 | const char *name, | |
1705 | int level, | |
1706 | char ***units, | |
14cb109d | 1707 | unsigned branches) { |
f459b602 | 1708 | |
e3e45d4f | 1709 | _cleanup_strv_free_ char **deps = NULL; |
55c0b89c | 1710 | char **c; |
55c0b89c LN |
1711 | int r = 0; |
1712 | ||
cbc9fbd1 LP |
1713 | assert(bus); |
1714 | assert(name); | |
1715 | assert(units); | |
1716 | ||
e3e45d4f SP |
1717 | r = strv_extend(units, name); |
1718 | if (r < 0) | |
55c0b89c LN |
1719 | return log_oom(); |
1720 | ||
1721 | r = list_dependencies_get_dependencies(bus, name, &deps); | |
1722 | if (r < 0) | |
cec7eda5 | 1723 | return r; |
55c0b89c | 1724 | |
93bab288 | 1725 | typesafe_qsort(deps, strv_length(deps), list_dependencies_compare); |
55c0b89c LN |
1726 | |
1727 | STRV_FOREACH(c, deps) { | |
e3e45d4f | 1728 | if (strv_contains(*units, *c)) { |
5d0c05e5 | 1729 | if (!arg_plain) { |
60705040 | 1730 | printf(" "); |
5d0c05e5 LN |
1731 | r = list_dependencies_print("...", level + 1, (branches << 1) | (c[1] == NULL ? 0 : 1), 1); |
1732 | if (r < 0) | |
1733 | return r; | |
1734 | } | |
55c0b89c LN |
1735 | continue; |
1736 | } | |
1737 | ||
250ba664 ZJS |
1738 | if (arg_plain) |
1739 | printf(" "); | |
1740 | else { | |
fa0d5878 | 1741 | UnitActiveState active_state = _UNIT_ACTIVE_STATE_INVALID; |
250ba664 ZJS |
1742 | const char *on; |
1743 | ||
fa0d5878 | 1744 | (void) get_state_one_unit(bus, *c, &active_state); |
baa9ecc1 | 1745 | |
fa0d5878 | 1746 | switch (active_state) { |
baa9ecc1 LP |
1747 | case UNIT_ACTIVE: |
1748 | case UNIT_RELOADING: | |
1749 | case UNIT_ACTIVATING: | |
1750 | on = ansi_highlight_green(); | |
1751 | break; | |
1752 | ||
1753 | case UNIT_INACTIVE: | |
1754 | case UNIT_DEACTIVATING: | |
1755 | on = ansi_normal(); | |
1756 | break; | |
1757 | ||
1758 | default: | |
1759 | on = ansi_highlight_red(); | |
1760 | break; | |
fa0d5878 BR |
1761 | } |
1762 | ||
9a6f746f | 1763 | printf("%s%s%s ", on, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE), ansi_normal()); |
250ba664 | 1764 | } |
dbc2c080 | 1765 | |
55c0b89c | 1766 | r = list_dependencies_print(*c, level, branches, c[1] == NULL); |
cec7eda5 ZJS |
1767 | if (r < 0) |
1768 | return r; | |
55c0b89c LN |
1769 | |
1770 | if (arg_all || unit_name_to_type(*c) == UNIT_TARGET) { | |
e3e45d4f | 1771 | r = list_dependencies_one(bus, *c, level + 1, units, (branches << 1) | (c[1] == NULL ? 0 : 1)); |
f168c273 | 1772 | if (r < 0) |
cec7eda5 | 1773 | return r; |
55c0b89c LN |
1774 | } |
1775 | } | |
f459b602 | 1776 | |
e3e45d4f SP |
1777 | if (!arg_plain) |
1778 | strv_remove(*units, name); | |
f459b602 | 1779 | |
cec7eda5 | 1780 | return 0; |
55c0b89c LN |
1781 | } |
1782 | ||
e449de87 | 1783 | static int list_dependencies(int argc, char *argv[], void *userdata) { |
5d0c05e5 | 1784 | _cleanup_strv_free_ char **units = NULL; |
f459b602 | 1785 | _cleanup_free_ char *unit = NULL; |
e31165b2 | 1786 | const char *u; |
4fbd7192 | 1787 | sd_bus *bus; |
7410616c | 1788 | int r; |
55c0b89c | 1789 | |
e449de87 | 1790 | if (argv[1]) { |
37cbc1d5 | 1791 | r = unit_name_mangle(argv[1], arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN, &unit); |
7410616c LP |
1792 | if (r < 0) |
1793 | return log_error_errno(r, "Failed to mangle unit name: %m"); | |
1794 | ||
e31165b2 LP |
1795 | u = unit; |
1796 | } else | |
1797 | u = SPECIAL_DEFAULT_TARGET; | |
55c0b89c | 1798 | |
4fbd7192 LP |
1799 | r = acquire_bus(BUS_MANAGER, &bus); |
1800 | if (r < 0) | |
1801 | return r; | |
1802 | ||
0221d68a | 1803 | (void) pager_open(arg_pager_flags); |
d2ad7e1b | 1804 | |
e31165b2 LP |
1805 | puts(u); |
1806 | ||
5d0c05e5 | 1807 | return list_dependencies_one(bus, u, 0, &units, 0); |
55c0b89c LN |
1808 | } |
1809 | ||
0d292f5e LP |
1810 | struct machine_info { |
1811 | bool is_host; | |
1812 | char *name; | |
0d292f5e | 1813 | char *state; |
8fcf784d LP |
1814 | char *control_group; |
1815 | uint32_t n_failed_units; | |
1816 | uint32_t n_jobs; | |
1817 | usec_t timestamp; | |
1818 | }; | |
1819 | ||
1820 | static const struct bus_properties_map machine_info_property_map[] = { | |
1821 | { "SystemState", "s", NULL, offsetof(struct machine_info, state) }, | |
1822 | { "NJobs", "u", NULL, offsetof(struct machine_info, n_jobs) }, | |
1823 | { "NFailedUnits", "u", NULL, offsetof(struct machine_info, n_failed_units) }, | |
1824 | { "ControlGroup", "s", NULL, offsetof(struct machine_info, control_group) }, | |
1825 | { "UserspaceTimestamp", "t", NULL, offsetof(struct machine_info, timestamp) }, | |
1826 | {} | |
0d292f5e LP |
1827 | }; |
1828 | ||
e7e55dbd | 1829 | static void machine_info_clear(struct machine_info *info) { |
f8654baa ZJS |
1830 | assert(info); |
1831 | ||
1832 | free(info->name); | |
1833 | free(info->state); | |
1834 | free(info->control_group); | |
1835 | zero(*info); | |
e7e55dbd DH |
1836 | } |
1837 | ||
0d292f5e LP |
1838 | static void free_machines_list(struct machine_info *machine_infos, int n) { |
1839 | int i; | |
1840 | ||
1841 | if (!machine_infos) | |
1842 | return; | |
1843 | ||
e7e55dbd DH |
1844 | for (i = 0; i < n; i++) |
1845 | machine_info_clear(&machine_infos[i]); | |
0d292f5e LP |
1846 | |
1847 | free(machine_infos); | |
1848 | } | |
1849 | ||
93bab288 YW |
1850 | static int compare_machine_info(const struct machine_info *a, const struct machine_info *b) { |
1851 | int r; | |
0d292f5e | 1852 | |
93bab288 YW |
1853 | r = CMP(b->is_host, a->is_host); |
1854 | if (r != 0) | |
1855 | return r; | |
0d292f5e | 1856 | |
93bab288 | 1857 | return strcasecmp(a->name, b->name); |
0d292f5e LP |
1858 | } |
1859 | ||
1860 | static int get_machine_properties(sd_bus *bus, struct machine_info *mi) { | |
4afd3348 | 1861 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *container = NULL; |
0d292f5e LP |
1862 | int r; |
1863 | ||
1864 | assert(mi); | |
1865 | ||
1866 | if (!bus) { | |
de33fc62 | 1867 | r = sd_bus_open_system_machine(&container, mi->name); |
0d292f5e LP |
1868 | if (r < 0) |
1869 | return r; | |
1870 | ||
1871 | bus = container; | |
1872 | } | |
1873 | ||
a7e4861c YW |
1874 | r = bus_map_all_properties( |
1875 | bus, | |
1876 | "org.freedesktop.systemd1", | |
1877 | "/org/freedesktop/systemd1", | |
1878 | machine_info_property_map, | |
1879 | BUS_MAP_STRDUP, | |
1880 | NULL, | |
1881 | NULL, | |
1882 | mi); | |
0d292f5e LP |
1883 | if (r < 0) |
1884 | return r; | |
1885 | ||
1886 | return 0; | |
1887 | } | |
1888 | ||
1889 | static bool output_show_machine(const char *name, char **patterns) { | |
2404701e | 1890 | return strv_fnmatch_or_empty(patterns, name, FNM_NOESCAPE); |
0d292f5e LP |
1891 | } |
1892 | ||
1893 | static int get_machine_list( | |
1894 | sd_bus *bus, | |
1895 | struct machine_info **_machine_infos, | |
1896 | char **patterns) { | |
1897 | ||
1898 | struct machine_info *machine_infos = NULL; | |
1899 | _cleanup_strv_free_ char **m = NULL; | |
1900 | _cleanup_free_ char *hn = NULL; | |
1901 | size_t sz = 0; | |
1902 | char **i; | |
4c3e8e39 | 1903 | int c = 0, r; |
0d292f5e LP |
1904 | |
1905 | hn = gethostname_malloc(); | |
1906 | if (!hn) | |
1907 | return log_oom(); | |
1908 | ||
1909 | if (output_show_machine(hn, patterns)) { | |
1910 | if (!GREEDY_REALLOC0(machine_infos, sz, c+1)) | |
1911 | return log_oom(); | |
1912 | ||
1913 | machine_infos[c].is_host = true; | |
ae2a15bc | 1914 | machine_infos[c].name = TAKE_PTR(hn); |
0d292f5e | 1915 | |
f9e0eefc | 1916 | (void) get_machine_properties(bus, &machine_infos[c]); |
0d292f5e LP |
1917 | c++; |
1918 | } | |
1919 | ||
4c3e8e39 LP |
1920 | r = sd_get_machine_names(&m); |
1921 | if (r < 0) | |
1922 | return log_error_errno(r, "Failed to get machine list: %m"); | |
1923 | ||
0d292f5e LP |
1924 | STRV_FOREACH(i, m) { |
1925 | _cleanup_free_ char *class = NULL; | |
1926 | ||
1927 | if (!output_show_machine(*i, patterns)) | |
1928 | continue; | |
1929 | ||
1930 | sd_machine_get_class(*i, &class); | |
1931 | if (!streq_ptr(class, "container")) | |
1932 | continue; | |
1933 | ||
1934 | if (!GREEDY_REALLOC0(machine_infos, sz, c+1)) { | |
1935 | free_machines_list(machine_infos, c); | |
1936 | return log_oom(); | |
1937 | } | |
1938 | ||
1939 | machine_infos[c].is_host = false; | |
1940 | machine_infos[c].name = strdup(*i); | |
1941 | if (!machine_infos[c].name) { | |
1942 | free_machines_list(machine_infos, c); | |
1943 | return log_oom(); | |
1944 | } | |
1945 | ||
f9e0eefc | 1946 | (void) get_machine_properties(NULL, &machine_infos[c]); |
0d292f5e LP |
1947 | c++; |
1948 | } | |
1949 | ||
1950 | *_machine_infos = machine_infos; | |
1951 | return c; | |
1952 | } | |
1953 | ||
1954 | static void output_machines_list(struct machine_info *machine_infos, unsigned n) { | |
1955 | struct machine_info *m; | |
1956 | unsigned | |
90c3f79d | 1957 | circle_len = 0, |
fbd0b64f LP |
1958 | namelen = STRLEN("NAME"), |
1959 | statelen = STRLEN("STATE"), | |
1960 | failedlen = STRLEN("FAILED"), | |
1961 | jobslen = STRLEN("JOBS"); | |
0d292f5e LP |
1962 | |
1963 | assert(machine_infos || n == 0); | |
1964 | ||
1965 | for (m = machine_infos; m < machine_infos + n; m++) { | |
fbd0b64f LP |
1966 | namelen = MAX(namelen, |
1967 | strlen(m->name) + (m->is_host ? STRLEN(" (host)") : 0)); | |
7bf7ce28 | 1968 | statelen = MAX(statelen, strlen_ptr(m->state)); |
0d292f5e LP |
1969 | failedlen = MAX(failedlen, DECIMAL_STR_WIDTH(m->n_failed_units)); |
1970 | jobslen = MAX(jobslen, DECIMAL_STR_WIDTH(m->n_jobs)); | |
90c3f79d | 1971 | |
250ba664 | 1972 | if (!arg_plain && !streq_ptr(m->state, "running")) |
90c3f79d | 1973 | circle_len = 2; |
0d292f5e LP |
1974 | } |
1975 | ||
90c3f79d LP |
1976 | if (!arg_no_legend) { |
1977 | if (circle_len > 0) | |
1978 | fputs(" ", stdout); | |
1979 | ||
0d292f5e LP |
1980 | printf("%-*s %-*s %-*s %-*s\n", |
1981 | namelen, "NAME", | |
1982 | statelen, "STATE", | |
1983 | failedlen, "FAILED", | |
1984 | jobslen, "JOBS"); | |
90c3f79d | 1985 | } |
0d292f5e LP |
1986 | |
1987 | for (m = machine_infos; m < machine_infos + n; m++) { | |
90c3f79d LP |
1988 | const char *on_state = "", *off_state = ""; |
1989 | const char *on_failed = "", *off_failed = ""; | |
1990 | bool circle = false; | |
0d292f5e LP |
1991 | |
1992 | if (streq_ptr(m->state, "degraded")) { | |
1993 | on_state = ansi_highlight_red(); | |
1fc464f6 | 1994 | off_state = ansi_normal(); |
90c3f79d | 1995 | circle = true; |
0d292f5e LP |
1996 | } else if (!streq_ptr(m->state, "running")) { |
1997 | on_state = ansi_highlight_yellow(); | |
1fc464f6 | 1998 | off_state = ansi_normal(); |
90c3f79d LP |
1999 | circle = true; |
2000 | } | |
0d292f5e LP |
2001 | |
2002 | if (m->n_failed_units > 0) { | |
2003 | on_failed = ansi_highlight_red(); | |
1fc464f6 | 2004 | off_failed = ansi_normal(); |
0d292f5e LP |
2005 | } else |
2006 | on_failed = off_failed = ""; | |
2007 | ||
90c3f79d | 2008 | if (circle_len > 0) |
9a6f746f | 2009 | printf("%s%s%s ", on_state, circle ? special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE) : " ", off_state); |
90c3f79d | 2010 | |
0d292f5e | 2011 | if (m->is_host) |
c6ba5b80 | 2012 | printf("%-*s (host) %s%-*s%s %s%*" PRIu32 "%s %*" PRIu32 "\n", |
fbd0b64f LP |
2013 | (int) (namelen - (STRLEN(" (host)"))), |
2014 | strna(m->name), | |
0d292f5e LP |
2015 | on_state, statelen, strna(m->state), off_state, |
2016 | on_failed, failedlen, m->n_failed_units, off_failed, | |
2017 | jobslen, m->n_jobs); | |
2018 | else | |
c6ba5b80 | 2019 | printf("%-*s %s%-*s%s %s%*" PRIu32 "%s %*" PRIu32 "\n", |
0d292f5e LP |
2020 | namelen, strna(m->name), |
2021 | on_state, statelen, strna(m->state), off_state, | |
2022 | on_failed, failedlen, m->n_failed_units, off_failed, | |
2023 | jobslen, m->n_jobs); | |
2024 | } | |
2025 | ||
2026 | if (!arg_no_legend) | |
2027 | printf("\n%u machines listed.\n", n); | |
2028 | } | |
2029 | ||
e449de87 | 2030 | static int list_machines(int argc, char *argv[], void *userdata) { |
0d292f5e | 2031 | struct machine_info *machine_infos = NULL; |
4fbd7192 | 2032 | sd_bus *bus; |
0d292f5e LP |
2033 | int r; |
2034 | ||
4fbd7192 LP |
2035 | r = acquire_bus(BUS_MANAGER, &bus); |
2036 | if (r < 0) | |
2037 | return r; | |
2038 | ||
e449de87 | 2039 | r = get_machine_list(bus, &machine_infos, strv_skip(argv, 1)); |
0d292f5e LP |
2040 | if (r < 0) |
2041 | return r; | |
2042 | ||
0221d68a | 2043 | (void) pager_open(arg_pager_flags); |
d2ad7e1b | 2044 | |
93bab288 | 2045 | typesafe_qsort(machine_infos, r, compare_machine_info); |
0d292f5e LP |
2046 | output_machines_list(machine_infos, r); |
2047 | free_machines_list(machine_infos, r); | |
2048 | ||
2049 | return 0; | |
2050 | } | |
2051 | ||
e449de87 | 2052 | static int get_default(int argc, char *argv[], void *userdata) { |
4afd3348 | 2053 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
f459b602 MAP |
2054 | _cleanup_free_ char *_path = NULL; |
2055 | const char *path; | |
99504dd4 | 2056 | int r; |
99504dd4 | 2057 | |
4fbd7192 | 2058 | if (install_client_side()) { |
f459b602 | 2059 | r = unit_file_get_default(arg_scope, arg_root, &_path); |
f647962d MS |
2060 | if (r < 0) |
2061 | return log_error_errno(r, "Failed to get default target: %m"); | |
f459b602 | 2062 | path = _path; |
99504dd4 | 2063 | |
5f056378 | 2064 | r = 0; |
99504dd4 | 2065 | } else { |
4afd3348 | 2066 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
4fbd7192 LP |
2067 | sd_bus *bus; |
2068 | ||
2069 | r = acquire_bus(BUS_MANAGER, &bus); | |
2070 | if (r < 0) | |
2071 | return r; | |
6e646d22 | 2072 | |
f459b602 MAP |
2073 | r = sd_bus_call_method( |
2074 | bus, | |
2075 | "org.freedesktop.systemd1", | |
2076 | "/org/freedesktop/systemd1", | |
2077 | "org.freedesktop.systemd1.Manager", | |
2078 | "GetDefaultTarget", | |
2079 | &error, | |
2080 | &reply, | |
2081 | NULL); | |
691395d8 LP |
2082 | if (r < 0) |
2083 | return log_error_errno(r, "Failed to get default target: %s", bus_error_message(&error, r)); | |
99504dd4 | 2084 | |
f459b602 MAP |
2085 | r = sd_bus_message_read(reply, "s", &path); |
2086 | if (r < 0) | |
2087 | return bus_log_parse_error(r); | |
99504dd4 VP |
2088 | } |
2089 | ||
2090 | if (path) | |
2091 | printf("%s\n", path); | |
2092 | ||
f459b602 | 2093 | return 0; |
99504dd4 VP |
2094 | } |
2095 | ||
e449de87 | 2096 | static int set_default(int argc, char *argv[], void *userdata) { |
718db961 | 2097 | _cleanup_free_ char *unit = NULL; |
acc0269c | 2098 | UnitFileChange *changes = NULL; |
da6053d0 | 2099 | size_t n_changes = 0; |
718db961 LP |
2100 | int r; |
2101 | ||
e449de87 LP |
2102 | assert(argc >= 2); |
2103 | assert(argv); | |
2104 | ||
37cbc1d5 | 2105 | r = unit_name_mangle_with_suffix(argv[1], arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN, ".target", &unit); |
7410616c LP |
2106 | if (r < 0) |
2107 | return log_error_errno(r, "Failed to mangle unit name: %m"); | |
718db961 | 2108 | |
4fbd7192 | 2109 | if (install_client_side()) { |
b3796dd8 | 2110 | r = unit_file_set_default(arg_scope, UNIT_FILE_FORCE, arg_root, unit, &changes, &n_changes); |
af3d8113 | 2111 | unit_file_dump_changes(r, "set default", changes, n_changes, arg_quiet); |
5f056378 CH |
2112 | |
2113 | if (r > 0) | |
2114 | r = 0; | |
718db961 | 2115 | } else { |
4afd3348 LP |
2116 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
2117 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
4fbd7192 | 2118 | sd_bus *bus; |
718db961 | 2119 | |
8a4b13c5 | 2120 | polkit_agent_open_maybe(); |
6e646d22 | 2121 | |
4fbd7192 LP |
2122 | r = acquire_bus(BUS_MANAGER, &bus); |
2123 | if (r < 0) | |
2124 | return r; | |
2125 | ||
6e646d22 | 2126 | r = sd_bus_call_method( |
718db961 LP |
2127 | bus, |
2128 | "org.freedesktop.systemd1", | |
2129 | "/org/freedesktop/systemd1", | |
2130 | "org.freedesktop.systemd1.Manager", | |
6e646d22 LP |
2131 | "SetDefaultTarget", |
2132 | &error, | |
2133 | &reply, | |
2134 | "sb", unit, 1); | |
691395d8 LP |
2135 | if (r < 0) |
2136 | return log_error_errno(r, "Failed to set default target: %s", bus_error_message(&error, r)); | |
718db961 | 2137 | |
acc0269c | 2138 | r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet, &changes, &n_changes); |
718db961 | 2139 | if (r < 0) |
acc0269c | 2140 | goto finish; |
718db961 | 2141 | |
93c941e3 | 2142 | /* Try to reload if enabled */ |
718db961 | 2143 | if (!arg_no_reload) |
e449de87 | 2144 | r = daemon_reload(argc, argv, userdata); |
718db961 LP |
2145 | else |
2146 | r = 0; | |
2147 | } | |
2148 | ||
acc0269c CH |
2149 | finish: |
2150 | unit_file_changes_free(changes, n_changes); | |
2151 | ||
718db961 LP |
2152 | return r; |
2153 | } | |
2154 | ||
76d8ca22 | 2155 | static int output_waiting_jobs(sd_bus *bus, uint32_t id, const char *method, const char *prefix) { |
82948f6c LP |
2156 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
2157 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
31d99bd1 | 2158 | const char *name, *type; |
82948f6c LP |
2159 | uint32_t other_id; |
2160 | int r; | |
2161 | ||
2162 | assert(bus); | |
2163 | ||
2164 | r = sd_bus_call_method( | |
2165 | bus, | |
2166 | "org.freedesktop.systemd1", | |
2167 | "/org/freedesktop/systemd1", | |
2168 | "org.freedesktop.systemd1.Manager", | |
2169 | method, | |
2170 | &error, | |
2171 | &reply, | |
2172 | "u", id); | |
76d8ca22 ZJS |
2173 | if (r < 0) |
2174 | return log_debug_errno(r, "Failed to get waiting jobs for job %" PRIu32, id); | |
82948f6c LP |
2175 | |
2176 | r = sd_bus_message_enter_container(reply, 'a', "(usssoo)"); | |
76d8ca22 ZJS |
2177 | if (r < 0) |
2178 | return bus_log_parse_error(r); | |
82948f6c | 2179 | |
31d99bd1 | 2180 | while ((r = sd_bus_message_read(reply, "(usssoo)", &other_id, &name, &type, NULL, NULL, NULL)) > 0) |
bc357ce5 | 2181 | printf("%s %u (%s/%s)\n", prefix, other_id, name, type); |
76d8ca22 ZJS |
2182 | if (r < 0) |
2183 | return bus_log_parse_error(r); | |
82948f6c LP |
2184 | |
2185 | r = sd_bus_message_exit_container(reply); | |
76d8ca22 ZJS |
2186 | if (r < 0) |
2187 | return bus_log_parse_error(r); | |
2188 | ||
2189 | return 0; | |
82948f6c LP |
2190 | } |
2191 | ||
75add28a ZJS |
2192 | struct job_info { |
2193 | uint32_t id; | |
f459b602 | 2194 | const char *name, *type, *state; |
75add28a ZJS |
2195 | }; |
2196 | ||
82948f6c | 2197 | static void output_jobs_list(sd_bus *bus, const struct job_info* jobs, unsigned n, bool skipped) { |
f459b602 MAP |
2198 | unsigned id_len, unit_len, type_len, state_len; |
2199 | const struct job_info *j; | |
75add28a ZJS |
2200 | const char *on, *off; |
2201 | bool shorten = false; | |
2202 | ||
2203 | assert(n == 0 || jobs); | |
2204 | ||
2205 | if (n == 0) { | |
250ba664 ZJS |
2206 | if (!arg_no_legend) { |
2207 | on = ansi_highlight_green(); | |
1fc464f6 | 2208 | off = ansi_normal(); |
75add28a | 2209 | |
250ba664 ZJS |
2210 | printf("%sNo jobs %s.%s\n", on, skipped ? "listed" : "running", off); |
2211 | } | |
75add28a ZJS |
2212 | return; |
2213 | } | |
2214 | ||
0221d68a | 2215 | (void) pager_open(arg_pager_flags); |
75add28a | 2216 | |
fbd0b64f LP |
2217 | id_len = STRLEN("JOB"); |
2218 | unit_len = STRLEN("UNIT"); | |
2219 | type_len = STRLEN("TYPE"); | |
2220 | state_len = STRLEN("STATE"); | |
6d6d40c9 | 2221 | |
f459b602 MAP |
2222 | for (j = jobs; j < jobs + n; j++) { |
2223 | uint32_t id = j->id; | |
2224 | assert(j->name && j->type && j->state); | |
75add28a | 2225 | |
f459b602 MAP |
2226 | id_len = MAX(id_len, DECIMAL_STR_WIDTH(id)); |
2227 | unit_len = MAX(unit_len, strlen(j->name)); | |
2228 | type_len = MAX(type_len, strlen(j->type)); | |
2229 | state_len = MAX(state_len, strlen(j->state)); | |
2230 | } | |
75add28a | 2231 | |
f459b602 MAP |
2232 | if (!arg_full && id_len + 1 + unit_len + type_len + 1 + state_len > columns()) { |
2233 | unit_len = MAX(33u, columns() - id_len - type_len - state_len - 3); | |
2234 | shorten = true; | |
2235 | } | |
75add28a | 2236 | |
6ce774fd MM |
2237 | if (!arg_no_legend) |
2238 | printf("%*s %-*s %-*s %-*s\n", | |
2239 | id_len, "JOB", | |
2240 | unit_len, "UNIT", | |
2241 | type_len, "TYPE", | |
2242 | state_len, "STATE"); | |
75add28a | 2243 | |
f459b602 MAP |
2244 | for (j = jobs; j < jobs + n; j++) { |
2245 | _cleanup_free_ char *e = NULL; | |
75add28a | 2246 | |
f459b602 MAP |
2247 | if (streq(j->state, "running")) { |
2248 | on = ansi_highlight(); | |
1fc464f6 | 2249 | off = ansi_normal(); |
f459b602 MAP |
2250 | } else |
2251 | on = off = ""; | |
2252 | ||
2253 | e = shorten ? ellipsize(j->name, unit_len, 33) : NULL; | |
2254 | printf("%*u %s%-*s%s %-*s %s%-*s%s\n", | |
2255 | id_len, j->id, | |
2256 | on, unit_len, e ? e : j->name, off, | |
2257 | type_len, j->type, | |
2258 | on, state_len, j->state, off); | |
82948f6c LP |
2259 | |
2260 | if (arg_jobs_after) | |
bc357ce5 | 2261 | output_waiting_jobs(bus, j->id, "GetJobAfter", "\twaiting for job"); |
82948f6c | 2262 | if (arg_jobs_before) |
bc357ce5 | 2263 | output_waiting_jobs(bus, j->id, "GetJobBefore", "\tblocking job"); |
75add28a ZJS |
2264 | } |
2265 | ||
6ce774fd MM |
2266 | if (!arg_no_legend) { |
2267 | on = ansi_highlight(); | |
1fc464f6 | 2268 | off = ansi_normal(); |
75add28a | 2269 | |
6ce774fd MM |
2270 | printf("\n%s%u jobs listed%s.\n", on, n, off); |
2271 | } | |
75add28a ZJS |
2272 | } |
2273 | ||
d8fba7c6 | 2274 | static bool output_show_job(struct job_info *job, char **patterns) { |
2404701e | 2275 | return strv_fnmatch_or_empty(patterns, job->name, FNM_NOESCAPE); |
d8fba7c6 ZJS |
2276 | } |
2277 | ||
e449de87 | 2278 | static int list_jobs(int argc, char *argv[], void *userdata) { |
4afd3348 LP |
2279 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
2280 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
f459b602 | 2281 | _cleanup_free_ struct job_info *jobs = NULL; |
31d99bd1 YW |
2282 | const char *name, *type, *state; |
2283 | bool skipped = false; | |
f459b602 MAP |
2284 | size_t size = 0; |
2285 | unsigned c = 0; | |
4fbd7192 | 2286 | sd_bus *bus; |
f459b602 | 2287 | uint32_t id; |
f84190d8 | 2288 | int r; |
ec14911e | 2289 | |
4fbd7192 LP |
2290 | r = acquire_bus(BUS_MANAGER, &bus); |
2291 | if (r < 0) | |
2292 | return r; | |
2293 | ||
f459b602 | 2294 | r = sd_bus_call_method( |
f22f08cd SP |
2295 | bus, |
2296 | "org.freedesktop.systemd1", | |
2297 | "/org/freedesktop/systemd1", | |
2298 | "org.freedesktop.systemd1.Manager", | |
2299 | "ListJobs", | |
f459b602 | 2300 | &error, |
f22f08cd | 2301 | &reply, |
f459b602 | 2302 | NULL); |
691395d8 LP |
2303 | if (r < 0) |
2304 | return log_error_errno(r, "Failed to list jobs: %s", bus_error_message(&error, r)); | |
7e4249b9 | 2305 | |
f459b602 MAP |
2306 | r = sd_bus_message_enter_container(reply, 'a', "(usssoo)"); |
2307 | if (r < 0) | |
2308 | return bus_log_parse_error(r); | |
7e4249b9 | 2309 | |
31d99bd1 | 2310 | while ((r = sd_bus_message_read(reply, "(usssoo)", &id, &name, &type, &state, NULL, NULL)) > 0) { |
d8fba7c6 ZJS |
2311 | struct job_info job = { id, name, type, state }; |
2312 | ||
e449de87 | 2313 | if (!output_show_job(&job, strv_skip(argv, 1))) { |
d8fba7c6 ZJS |
2314 | skipped = true; |
2315 | continue; | |
2316 | } | |
8fe914ec | 2317 | |
f459b602 MAP |
2318 | if (!GREEDY_REALLOC(jobs, size, c + 1)) |
2319 | return log_oom(); | |
7e4249b9 | 2320 | |
d8fba7c6 | 2321 | jobs[c++] = job; |
7e4249b9 | 2322 | } |
f459b602 MAP |
2323 | if (r < 0) |
2324 | return bus_log_parse_error(r); | |
7e4249b9 | 2325 | |
f459b602 MAP |
2326 | r = sd_bus_message_exit_container(reply); |
2327 | if (r < 0) | |
2328 | return bus_log_parse_error(r); | |
f73e33d9 | 2329 | |
0221d68a | 2330 | (void) pager_open(arg_pager_flags); |
d2ad7e1b | 2331 | |
82948f6c | 2332 | output_jobs_list(bus, jobs, c, skipped); |
91d0f17e | 2333 | return 0; |
7e4249b9 LP |
2334 | } |
2335 | ||
e449de87 | 2336 | static int cancel_job(int argc, char *argv[], void *userdata) { |
4fbd7192 | 2337 | sd_bus *bus; |
729e3769 | 2338 | char **name; |
342641fb | 2339 | int r = 0; |
7e4249b9 | 2340 | |
e449de87 | 2341 | if (argc <= 1) |
2853b60a | 2342 | return trivial_method(argc, argv, userdata); |
ee5762e3 | 2343 | |
4fbd7192 LP |
2344 | r = acquire_bus(BUS_MANAGER, &bus); |
2345 | if (r < 0) | |
2346 | return r; | |
2347 | ||
8a4b13c5 | 2348 | polkit_agent_open_maybe(); |
d2ad7e1b | 2349 | |
e449de87 | 2350 | STRV_FOREACH(name, strv_skip(argv, 1)) { |
4afd3348 | 2351 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
5dd9014f | 2352 | uint32_t id; |
342641fb | 2353 | int q; |
7e4249b9 | 2354 | |
342641fb | 2355 | q = safe_atou32(*name, &id); |
f647962d MS |
2356 | if (q < 0) |
2357 | return log_error_errno(q, "Failed to parse job id \"%s\": %m", *name); | |
7e4249b9 | 2358 | |
6e646d22 | 2359 | q = sd_bus_call_method( |
f22f08cd SP |
2360 | bus, |
2361 | "org.freedesktop.systemd1", | |
2362 | "/org/freedesktop/systemd1", | |
2363 | "org.freedesktop.systemd1.Manager", | |
6e646d22 LP |
2364 | "CancelJob", |
2365 | &error, | |
2366 | NULL, | |
2367 | "u", id); | |
342641fb | 2368 | if (q < 0) { |
691395d8 | 2369 | log_error_errno(q, "Failed to cancel job %"PRIu32": %s", id, bus_error_message(&error, q)); |
342641fb LP |
2370 | if (r == 0) |
2371 | r = q; | |
f459b602 | 2372 | } |
7e4249b9 LP |
2373 | } |
2374 | ||
342641fb | 2375 | return r; |
7e4249b9 LP |
2376 | } |
2377 | ||
f459b602 | 2378 | static int need_daemon_reload(sd_bus *bus, const char *unit) { |
4afd3348 | 2379 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
f459b602 MAP |
2380 | const char *path; |
2381 | int b, r; | |
94c01aeb | 2382 | |
f459b602 MAP |
2383 | /* We ignore all errors here, since this is used to show a |
2384 | * warning only */ | |
45fb0699 | 2385 | |
f459b602 MAP |
2386 | /* We don't use unit_dbus_path_from_name() directly since we |
2387 | * don't want to load the unit if it isn't loaded. */ | |
f84190d8 | 2388 | |
f459b602 | 2389 | r = sd_bus_call_method( |
f22f08cd SP |
2390 | bus, |
2391 | "org.freedesktop.systemd1", | |
2392 | "/org/freedesktop/systemd1", | |
2393 | "org.freedesktop.systemd1.Manager", | |
2394 | "GetUnit", | |
f459b602 | 2395 | NULL, |
f22f08cd | 2396 | &reply, |
e3e0314b | 2397 | "s", unit); |
f84190d8 LP |
2398 | if (r < 0) |
2399 | return r; | |
45fb0699 | 2400 | |
f459b602 MAP |
2401 | r = sd_bus_message_read(reply, "o", &path); |
2402 | if (r < 0) | |
2403 | return r; | |
f84190d8 | 2404 | |
f459b602 | 2405 | r = sd_bus_get_property_trivial( |
f22f08cd | 2406 | bus, |
b0193f1c LP |
2407 | "org.freedesktop.systemd1", |
2408 | path, | |
f459b602 MAP |
2409 | "org.freedesktop.systemd1.Unit", |
2410 | "NeedDaemonReload", | |
2411 | NULL, | |
2412 | 'b', &b); | |
f84190d8 LP |
2413 | if (r < 0) |
2414 | return r; | |
45fb0699 | 2415 | |
45fb0699 LP |
2416 | return b; |
2417 | } | |
2418 | ||
3f36991e | 2419 | static void warn_unit_file_changed(const char *name) { |
87ec20ef LP |
2420 | assert(name); |
2421 | ||
e9f4aabd | 2422 | 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 | 2423 | ansi_highlight_red(), |
1fc464f6 | 2424 | ansi_normal(), |
3f36991e ZJS |
2425 | name, |
2426 | arg_scope == UNIT_FILE_SYSTEM ? "" : " --user"); | |
2427 | } | |
2428 | ||
c45e7e0c | 2429 | static int unit_file_find_path(LookupPaths *lp, const char *unit_name, char **ret_unit_path) { |
33f6c497 ZJS |
2430 | char **p; |
2431 | ||
2432 | assert(lp); | |
2433 | assert(unit_name); | |
33f6c497 | 2434 | |
a3c4eb07 | 2435 | STRV_FOREACH(p, lp->search_path) { |
c6dd36b6 FS |
2436 | _cleanup_free_ char *path = NULL, *lpath = NULL; |
2437 | int r; | |
33f6c497 | 2438 | |
62a85ee0 | 2439 | path = path_join(*p, unit_name); |
33f6c497 ZJS |
2440 | if (!path) |
2441 | return log_oom(); | |
2442 | ||
c4f4fce7 | 2443 | r = chase_symlinks(path, arg_root, 0, &lpath); |
c6dd36b6 FS |
2444 | if (r == -ENOENT) |
2445 | continue; | |
2446 | if (r == -ENOMEM) | |
2447 | return log_oom(); | |
2448 | if (r < 0) | |
86f004fb | 2449 | return log_error_errno(r, "Failed to access path \"%s\": %m", path); |
c6dd36b6 | 2450 | |
c45e7e0c LP |
2451 | if (ret_unit_path) |
2452 | *ret_unit_path = TAKE_PTR(lpath); | |
ae2a15bc | 2453 | |
c6dd36b6 | 2454 | return 1; |
33f6c497 ZJS |
2455 | } |
2456 | ||
f67cb270 LP |
2457 | if (ret_unit_path) |
2458 | *ret_unit_path = NULL; | |
2459 | ||
33f6c497 ZJS |
2460 | return 0; |
2461 | } | |
2462 | ||
173471b7 ZJS |
2463 | static int unit_find_template_path( |
2464 | const char *unit_name, | |
2465 | LookupPaths *lp, | |
b03677e2 LP |
2466 | char **ret_fragment_path, |
2467 | char **ret_template) { | |
173471b7 | 2468 | |
b03677e2 | 2469 | _cleanup_free_ char *t = NULL, *f = NULL; |
173471b7 ZJS |
2470 | int r; |
2471 | ||
2472 | /* Returns 1 if a fragment was found, 0 if not found, negative on error. */ | |
2473 | ||
b03677e2 LP |
2474 | r = unit_file_find_path(lp, unit_name, &f); |
2475 | if (r < 0) | |
2476 | return r; | |
2477 | if (r > 0) { | |
2478 | if (ret_fragment_path) | |
2479 | *ret_fragment_path = TAKE_PTR(f); | |
2480 | if (ret_template) | |
2481 | *ret_template = NULL; | |
2482 | return r; /* found a real unit */ | |
2483 | } | |
2484 | ||
2485 | r = unit_name_template(unit_name, &t); | |
2486 | if (r == -EINVAL) { | |
2487 | if (ret_fragment_path) | |
2488 | *ret_fragment_path = NULL; | |
2489 | if (ret_template) | |
2490 | *ret_template = NULL; | |
173471b7 | 2491 | |
173471b7 | 2492 | return 0; /* not a template, does not exist */ |
b03677e2 | 2493 | } |
173471b7 ZJS |
2494 | if (r < 0) |
2495 | return log_error_errno(r, "Failed to determine template name: %m"); | |
2496 | ||
b03677e2 | 2497 | r = unit_file_find_path(lp, t, ret_fragment_path); |
173471b7 ZJS |
2498 | if (r < 0) |
2499 | return r; | |
2500 | ||
b03677e2 LP |
2501 | if (ret_template) |
2502 | *ret_template = r > 0 ? TAKE_PTR(t) : NULL; | |
ae2a15bc | 2503 | |
173471b7 ZJS |
2504 | return r; |
2505 | } | |
2506 | ||
6e646d22 LP |
2507 | static int unit_find_paths( |
2508 | sd_bus *bus, | |
2509 | const char *unit_name, | |
6e646d22 | 2510 | LookupPaths *lp, |
02a126a3 LP |
2511 | bool force_client_side, |
2512 | char **ret_fragment_path, | |
2513 | char ***ret_dropin_paths) { | |
dab2bce8 | 2514 | |
dab2bce8 | 2515 | _cleanup_strv_free_ char **dropins = NULL; |
02a126a3 | 2516 | _cleanup_free_ char *path = NULL; |
33f6c497 ZJS |
2517 | int r; |
2518 | ||
ad2a0358 | 2519 | /** |
02a126a3 LP |
2520 | * Finds where the unit is defined on disk. Returns 0 if the unit is not found. Returns 1 if it is found, and |
2521 | * sets: | |
2522 | * - the path to the unit in *ret_frament_path, if it exists on disk, | |
2523 | * - and a strv of existing drop-ins in *ret_dropin_paths, if the arg is not NULL and any dropins were found. | |
2524 | * | |
2525 | * Returns -ERFKILL if the unit is masked, and -EKEYREJECTED if the unit file could not be loaded for some | |
2526 | * reason (the latter only applies if we are going through the service manager) | |
ad2a0358 ZJS |
2527 | */ |
2528 | ||
33f6c497 | 2529 | assert(unit_name); |
02a126a3 | 2530 | assert(ret_fragment_path); |
33f6c497 ZJS |
2531 | assert(lp); |
2532 | ||
02a126a3 LP |
2533 | /* Go via the bus to acquire the path, unless we are explicitly told not to, or when the unit name is a template */ |
2534 | if (!force_client_side && | |
2535 | !install_client_side() && | |
2536 | !unit_name_is_valid(unit_name, UNIT_NAME_TEMPLATE)) { | |
4afd3348 | 2537 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
02a126a3 | 2538 | _cleanup_free_ char *load_state = NULL, *unit = NULL; |
33f6c497 ZJS |
2539 | |
2540 | unit = unit_dbus_path_from_name(unit_name); | |
2541 | if (!unit) | |
2542 | return log_oom(); | |
2543 | ||
02a126a3 LP |
2544 | r = sd_bus_get_property_string( |
2545 | bus, | |
2546 | "org.freedesktop.systemd1", | |
2547 | unit, | |
2548 | "org.freedesktop.systemd1.Unit", | |
2549 | "LoadState", | |
2550 | &error, | |
2551 | &load_state); | |
2552 | if (r < 0) | |
2553 | return log_error_errno(r, "Failed to get LoadState: %s", bus_error_message(&error, r)); | |
2554 | ||
2555 | if (streq(load_state, "masked")) | |
2556 | return -ERFKILL; | |
2557 | if (streq(load_state, "not-found")) { | |
2558 | r = 0; | |
2559 | goto not_found; | |
2560 | } | |
2561 | if (!streq(load_state, "loaded")) | |
2562 | return -EKEYREJECTED; | |
2563 | ||
33f6c497 ZJS |
2564 | r = sd_bus_get_property_string( |
2565 | bus, | |
2566 | "org.freedesktop.systemd1", | |
2567 | unit, | |
2568 | "org.freedesktop.systemd1.Unit", | |
2569 | "FragmentPath", | |
2570 | &error, | |
ad2a0358 ZJS |
2571 | &path); |
2572 | if (r < 0) | |
2573 | return log_error_errno(r, "Failed to get FragmentPath: %s", bus_error_message(&error, r)); | |
2574 | ||
02a126a3 | 2575 | if (ret_dropin_paths) { |
dab2bce8 IS |
2576 | r = sd_bus_get_property_strv( |
2577 | bus, | |
2578 | "org.freedesktop.systemd1", | |
2579 | unit, | |
2580 | "org.freedesktop.systemd1.Unit", | |
2581 | "DropInPaths", | |
2582 | &error, | |
2583 | &dropins); | |
2584 | if (r < 0) | |
2585 | return log_error_errno(r, "Failed to get DropInPaths: %s", bus_error_message(&error, r)); | |
33f6c497 | 2586 | } |
ad2a0358 | 2587 | } else { |
173471b7 | 2588 | _cleanup_set_free_ Set *names = NULL; |
6483ad89 | 2589 | _cleanup_free_ char *template = NULL; |
33f6c497 | 2590 | |
ad2a0358 ZJS |
2591 | names = set_new(NULL); |
2592 | if (!names) | |
7410616c | 2593 | return log_oom(); |
33f6c497 | 2594 | |
173471b7 | 2595 | r = unit_find_template_path(unit_name, lp, &path, &template); |
ad2a0358 ZJS |
2596 | if (r < 0) |
2597 | return r; | |
86f004fb ZJS |
2598 | if (r > 0) { |
2599 | if (null_or_empty_path(path)) | |
2600 | /* The template is masked. Let's cut the process short. */ | |
2601 | return -ERFKILL; | |
2602 | ||
6483ad89 ZJS |
2603 | /* We found the unit file. If we followed symlinks, this name might be |
2604 | * different then the unit_name with started with. Look for dropins matching | |
2605 | * that "final" name. */ | |
2606 | r = set_put(names, basename(path)); | |
86f004fb | 2607 | } else if (!template) |
6483ad89 ZJS |
2608 | /* No unit file, let's look for dropins matching the original name. |
2609 | * systemd has fairly complicated rules (based on unit type and provenience), | |
2610 | * which units are allowed not to have the main unit file. We err on the | |
2611 | * side of including too many files, and always try to load dropins. */ | |
2612 | r = set_put(names, unit_name); | |
2613 | else | |
2614 | /* The cases where we allow a unit to exist without the main file are | |
2615 | * never valid for templates. Don't try to load dropins in this case. */ | |
2616 | goto not_found; | |
2617 | ||
c6dd36b6 FS |
2618 | if (r < 0) |
2619 | return log_error_errno(r, "Failed to add unit name: %m"); | |
2620 | ||
02a126a3 LP |
2621 | if (ret_dropin_paths) { |
2622 | r = unit_file_find_dropin_conf_paths(arg_root, lp->search_path, NULL, names, &dropins); | |
dab2bce8 IS |
2623 | if (r < 0) |
2624 | return r; | |
2625 | } | |
2626 | } | |
2627 | ||
02a126a3 | 2628 | r = 0; |
dab2bce8 IS |
2629 | |
2630 | if (!isempty(path)) { | |
02a126a3 | 2631 | *ret_fragment_path = TAKE_PTR(path); |
dab2bce8 | 2632 | r = 1; |
02a126a3 LP |
2633 | } else |
2634 | *ret_fragment_path = NULL; | |
dab2bce8 | 2635 | |
02a126a3 LP |
2636 | if (ret_dropin_paths) { |
2637 | if (!strv_isempty(dropins)) { | |
2638 | *ret_dropin_paths = TAKE_PTR(dropins); | |
2639 | r = 1; | |
2640 | } else | |
2641 | *ret_dropin_paths = NULL; | |
33f6c497 | 2642 | } |
02a126a3 | 2643 | |
6483ad89 | 2644 | not_found: |
39c38ce1 | 2645 | if (r == 0 && !arg_force) |
b5e6a600 IS |
2646 | log_error("No files found for %s.", unit_name); |
2647 | ||
ad2a0358 | 2648 | return r; |
33f6c497 ZJS |
2649 | } |
2650 | ||
fa0d5878 | 2651 | static int get_state_one_unit(sd_bus *bus, const char *name, UnitActiveState *active_state) { |
b430f85f | 2652 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
3d083b22 | 2653 | _cleanup_free_ char *buf = NULL, *path = NULL; |
fa0d5878 | 2654 | UnitActiveState state; |
f22f08cd | 2655 | int r; |
701cdcb9 | 2656 | |
31be1221 | 2657 | assert(name); |
fa0d5878 | 2658 | assert(active_state); |
701cdcb9 | 2659 | |
3d083b22 LP |
2660 | path = unit_dbus_path_from_name(name); |
2661 | if (!path) | |
2662 | return log_oom(); | |
2663 | ||
2664 | r = sd_bus_get_property_string( | |
f22f08cd SP |
2665 | bus, |
2666 | "org.freedesktop.systemd1", | |
3d083b22 LP |
2667 | path, |
2668 | "org.freedesktop.systemd1.Unit", | |
2669 | "ActiveState", | |
b430f85f | 2670 | &error, |
3d083b22 LP |
2671 | &buf); |
2672 | if (r < 0) | |
2673 | return log_error_errno(r, "Failed to retrieve unit state: %s", bus_error_message(&error, r)); | |
b430f85f | 2674 | |
3d083b22 LP |
2675 | state = unit_active_state_from_string(buf); |
2676 | if (state == _UNIT_ACTIVE_STATE_INVALID) { | |
2677 | log_error("Invalid unit state '%s' for: %s", buf, name); | |
2678 | return -EINVAL; | |
60f9ba0b | 2679 | } |
701cdcb9 | 2680 | |
fa0d5878 BR |
2681 | *active_state = state; |
2682 | return 0; | |
701cdcb9 MS |
2683 | } |
2684 | ||
86f004fb | 2685 | static int unit_is_masked(sd_bus *bus, LookupPaths *lp, const char *name) { |
bd062910 | 2686 | _cleanup_free_ char *load_state = NULL; |
f22f08cd | 2687 | int r; |
701cdcb9 | 2688 | |
86f004fb ZJS |
2689 | if (unit_name_is_valid(name, UNIT_NAME_TEMPLATE)) { |
2690 | _cleanup_free_ char *path = NULL; | |
2691 | ||
2692 | /* A template cannot be loaded, but it can be still masked, so | |
2693 | * we need to use a different method. */ | |
2694 | ||
2695 | r = unit_file_find_path(lp, name, &path); | |
2696 | if (r < 0) | |
2697 | return r; | |
2698 | if (r == 0) | |
2699 | return false; | |
2700 | return null_or_empty_path(path); | |
2701 | } | |
2702 | ||
bd062910 | 2703 | r = unit_load_state(bus, name, &load_state); |
691395d8 | 2704 | if (r < 0) |
bd062910 | 2705 | return r; |
d0a5cdb2 | 2706 | |
9d9dd746 ZJS |
2707 | return streq(load_state, "masked"); |
2708 | } | |
2709 | ||
2710 | static int check_triggering_units(sd_bus *bus, const char *name) { | |
2711 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
86f004fb | 2712 | _cleanup_free_ char *n = NULL, *path = NULL, *load_state = NULL; |
9d9dd746 ZJS |
2713 | _cleanup_strv_free_ char **triggered_by = NULL; |
2714 | bool print_warning_label = true; | |
2715 | UnitActiveState active_state; | |
2716 | char **i; | |
2717 | int r; | |
2718 | ||
2719 | r = unit_name_mangle(name, 0, &n); | |
2720 | if (r < 0) | |
2721 | return log_error_errno(r, "Failed to mangle unit name: %m"); | |
2722 | ||
86f004fb ZJS |
2723 | r = unit_load_state(bus, name, &load_state); |
2724 | if (r < 0) | |
2725 | return r; | |
2726 | ||
2727 | if (streq(load_state, "masked")) | |
2728 | return 0; | |
9d9dd746 ZJS |
2729 | |
2730 | path = unit_dbus_path_from_name(n); | |
2731 | if (!path) | |
2732 | return log_oom(); | |
701cdcb9 | 2733 | |
f459b602 MAP |
2734 | r = sd_bus_get_property_strv( |
2735 | bus, | |
2736 | "org.freedesktop.systemd1", | |
2737 | path, | |
2738 | "org.freedesktop.systemd1.Unit", | |
2739 | "TriggeredBy", | |
2740 | &error, | |
2741 | &triggered_by); | |
691395d8 LP |
2742 | if (r < 0) |
2743 | return log_error_errno(r, "Failed to get triggered by array of %s: %s", n, bus_error_message(&error, r)); | |
701cdcb9 | 2744 | |
f459b602 | 2745 | STRV_FOREACH(i, triggered_by) { |
fa0d5878 | 2746 | r = get_state_one_unit(bus, *i, &active_state); |
f647962d | 2747 | if (r < 0) |
fa0d5878 | 2748 | return r; |
701cdcb9 | 2749 | |
fa0d5878 | 2750 | if (!IN_SET(active_state, UNIT_ACTIVE, UNIT_RELOADING)) |
f459b602 | 2751 | continue; |
60f9ba0b | 2752 | |
f459b602 MAP |
2753 | if (print_warning_label) { |
2754 | log_warning("Warning: Stopping %s, but it can still be activated by:", n); | |
2755 | print_warning_label = false; | |
701cdcb9 | 2756 | } |
1c291cf3 | 2757 | |
f459b602 | 2758 | log_warning(" %s", *i); |
701cdcb9 | 2759 | } |
f459b602 MAP |
2760 | |
2761 | return 0; | |
701cdcb9 MS |
2762 | } |
2763 | ||
2fc9a280 LP |
2764 | static const struct { |
2765 | const char *verb; | |
2766 | const char *method; | |
2767 | } unit_actions[] = { | |
2768 | { "start", "StartUnit" }, | |
2769 | { "stop", "StopUnit" }, | |
2770 | { "condstop", "StopUnit" }, | |
2771 | { "reload", "ReloadUnit" }, | |
2772 | { "restart", "RestartUnit" }, | |
2773 | { "try-restart", "TryRestartUnit" }, | |
2774 | { "condrestart", "TryRestartUnit" }, | |
2775 | { "reload-or-restart", "ReloadOrRestartUnit" }, | |
aabf5d42 | 2776 | { "try-reload-or-restart", "ReloadOrTryRestartUnit" }, |
2fc9a280 LP |
2777 | { "reload-or-try-restart", "ReloadOrTryRestartUnit" }, |
2778 | { "condreload", "ReloadOrTryRestartUnit" }, | |
2779 | { "force-reload", "ReloadOrTryRestartUnit" } | |
2780 | }; | |
2781 | ||
39602c39 TA |
2782 | static const char *verb_to_method(const char *verb) { |
2783 | uint i; | |
2784 | ||
2785 | for (i = 0; i < ELEMENTSOF(unit_actions); i++) | |
2786 | if (streq_ptr(unit_actions[i].verb, verb)) | |
2787 | return unit_actions[i].method; | |
2788 | ||
2789 | return "StartUnit"; | |
2790 | } | |
2791 | ||
2792 | static const char *method_to_verb(const char *method) { | |
2793 | uint i; | |
2794 | ||
2795 | for (i = 0; i < ELEMENTSOF(unit_actions); i++) | |
2796 | if (streq_ptr(unit_actions[i].method, method)) | |
2797 | return unit_actions[i].verb; | |
2798 | ||
2799 | return "n/a"; | |
2800 | } | |
2801 | ||
93a08841 MP |
2802 | typedef struct { |
2803 | sd_bus_slot *match; | |
2804 | sd_event *event; | |
2805 | Set *unit_paths; | |
2806 | bool any_failed; | |
2807 | } WaitContext; | |
2808 | ||
2809 | static void wait_context_free(WaitContext *c) { | |
2810 | c->match = sd_bus_slot_unref(c->match); | |
2811 | c->event = sd_event_unref(c->event); | |
6fa4160d | 2812 | c->unit_paths = set_free_free(c->unit_paths); |
93a08841 MP |
2813 | } |
2814 | ||
2815 | static int on_properties_changed(sd_bus_message *m, void *userdata, sd_bus_error *error) { | |
109b0cbf | 2816 | const char *path, *interface, *active_state = NULL, *job_path = NULL; |
93a08841 | 2817 | WaitContext *c = userdata; |
109b0cbf | 2818 | bool is_failed; |
93a08841 MP |
2819 | int r; |
2820 | ||
109b0cbf LP |
2821 | /* Called whenever we get a PropertiesChanged signal. Checks if ActiveState changed to inactive/failed. |
2822 | * | |
2823 | * Signal parameters: (s interface, a{sv} changed_properties, as invalidated_properties) */ | |
2824 | ||
93a08841 MP |
2825 | path = sd_bus_message_get_path(m); |
2826 | if (!set_contains(c->unit_paths, path)) | |
2827 | return 0; | |
2828 | ||
109b0cbf | 2829 | r = sd_bus_message_read(m, "s", &interface); |
93a08841 MP |
2830 | if (r < 0) |
2831 | return bus_log_parse_error(r); | |
6fa4160d | 2832 | |
109b0cbf LP |
2833 | if (!streq(interface, "org.freedesktop.systemd1.Unit")) /* ActiveState is on the Unit interface */ |
2834 | return 0; | |
2835 | ||
93a08841 MP |
2836 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "{sv}"); |
2837 | if (r < 0) | |
2838 | return bus_log_parse_error(r); | |
2839 | ||
109b0cbf | 2840 | for (;;) { |
93a08841 | 2841 | const char *s; |
93a08841 | 2842 | |
109b0cbf | 2843 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_DICT_ENTRY, "sv"); |
93a08841 MP |
2844 | if (r < 0) |
2845 | return bus_log_parse_error(r); | |
109b0cbf LP |
2846 | if (r == 0) /* end of array */ |
2847 | break; | |
6fa4160d | 2848 | |
109b0cbf LP |
2849 | r = sd_bus_message_read(m, "s", &s); /* Property name */ |
2850 | if (r < 0) | |
2851 | return bus_log_parse_error(r); | |
6fa4160d | 2852 | |
109b0cbf LP |
2853 | if (streq(s, "ActiveState")) { |
2854 | r = sd_bus_message_read(m, "v", "s", &active_state); | |
93a08841 MP |
2855 | if (r < 0) |
2856 | return bus_log_parse_error(r); | |
6fa4160d | 2857 | |
109b0cbf LP |
2858 | if (job_path) /* Found everything we need */ |
2859 | break; | |
2860 | ||
2861 | } else if (streq(s, "Job")) { | |
2862 | uint32_t job_id; | |
2863 | ||
2864 | r = sd_bus_message_read(m, "v", "(uo)", &job_id, &job_path); | |
93a08841 MP |
2865 | if (r < 0) |
2866 | return bus_log_parse_error(r); | |
6fa4160d | 2867 | |
109b0cbf LP |
2868 | /* There's still a job pending for this unit, let's ignore this for now, and return right-away. */ |
2869 | if (job_id != 0) | |
2870 | return 0; | |
2871 | ||
2872 | if (active_state) /* Found everything we need */ | |
2873 | break; | |
6fa4160d | 2874 | |
93a08841 | 2875 | } else { |
109b0cbf | 2876 | r = sd_bus_message_skip(m, "v"); /* Other property */ |
93a08841 MP |
2877 | if (r < 0) |
2878 | return bus_log_parse_error(r); | |
2879 | } | |
109b0cbf | 2880 | |
93a08841 MP |
2881 | r = sd_bus_message_exit_container(m); |
2882 | if (r < 0) | |
2883 | return bus_log_parse_error(r); | |
2884 | } | |
109b0cbf LP |
2885 | |
2886 | /* If this didn't contain the ActiveState property we can't do anything */ | |
2887 | if (!active_state) | |
2888 | return 0; | |
2889 | ||
2890 | is_failed = streq(active_state, "failed"); | |
2891 | if (streq(active_state, "inactive") || is_failed) { | |
2892 | log_debug("%s became %s, dropping from --wait tracking", path, active_state); | |
2893 | free(set_remove(c->unit_paths, path)); | |
2894 | c->any_failed = c->any_failed || is_failed; | |
2895 | } else | |
2896 | log_debug("ActiveState on %s changed to %s", path, active_state); | |
93a08841 MP |
2897 | |
2898 | if (set_isempty(c->unit_paths)) | |
2899 | sd_event_exit(c->event, EXIT_SUCCESS); | |
2900 | ||
2901 | return 0; | |
2902 | } | |
2903 | ||
9a199645 LP |
2904 | static int wait_context_watch( |
2905 | WaitContext *wait_context, | |
2906 | sd_bus *bus, | |
2907 | const char *name) { | |
2908 | ||
2909 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
2910 | _cleanup_free_ char *unit_path = NULL; | |
2911 | int r; | |
2912 | ||
2913 | assert(wait_context); | |
2914 | assert(name); | |
2915 | ||
2916 | log_debug("Watching for property changes of %s", name); | |
2917 | r = sd_bus_call_method( | |
2918 | bus, | |
2919 | "org.freedesktop.systemd1", | |
2920 | "/org/freedesktop/systemd1", | |
2921 | "org.freedesktop.systemd1.Manager", | |
2922 | "RefUnit", | |
2923 | &error, | |
2924 | NULL, | |
2925 | "s", name); | |
2926 | if (r < 0) | |
2927 | return log_error_errno(r, "Failed to add reference to unit %s: %s", name, bus_error_message(&error, r)); | |
2928 | ||
2929 | unit_path = unit_dbus_path_from_name(name); | |
2930 | if (!unit_path) | |
2931 | return log_oom(); | |
2932 | ||
2933 | r = set_ensure_allocated(&wait_context->unit_paths, &string_hash_ops); | |
2934 | if (r < 0) | |
2935 | return log_oom(); | |
2936 | ||
2937 | r = set_put_strdup(wait_context->unit_paths, unit_path); | |
2938 | if (r < 0) | |
2939 | return log_error_errno(r, "Failed to add unit path %s to set: %m", unit_path); | |
2940 | ||
2941 | r = sd_bus_match_signal_async(bus, | |
2942 | &wait_context->match, | |
2943 | NULL, | |
2944 | unit_path, | |
2945 | "org.freedesktop.DBus.Properties", | |
2946 | "PropertiesChanged", | |
2947 | on_properties_changed, NULL, wait_context); | |
2948 | if (r < 0) | |
2949 | return log_error_errno(r, "Failed to request match for PropertiesChanged signal: %m"); | |
2950 | ||
2951 | return 0; | |
2952 | } | |
2953 | ||
e4b61340 | 2954 | static int start_unit_one( |
f459b602 | 2955 | sd_bus *bus, |
e4b61340 LP |
2956 | const char *method, |
2957 | const char *name, | |
2958 | const char *mode, | |
f459b602 | 2959 | sd_bus_error *error, |
93a08841 MP |
2960 | BusWaitForJobs *w, |
2961 | WaitContext *wait_context) { | |
7e4249b9 | 2962 | |
4afd3348 | 2963 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
45fb0699 | 2964 | const char *path; |
7e4249b9 | 2965 | int r; |
7e4249b9 | 2966 | |
e4b61340 LP |
2967 | assert(method); |
2968 | assert(name); | |
2969 | assert(mode); | |
22f4096c | 2970 | assert(error); |
7e4249b9 | 2971 | |
93a08841 | 2972 | if (wait_context) { |
9a199645 | 2973 | r = wait_context_watch(wait_context, bus, name); |
93a08841 | 2974 | if (r < 0) |
9a199645 | 2975 | return r; |
93a08841 MP |
2976 | } |
2977 | ||
dbc9830c ZJS |
2978 | log_debug("%s dbus call org.freedesktop.systemd1.Manager %s(%s, %s)", |
2979 | arg_dry_run ? "Would execute" : "Executing", | |
1ae17672 ZJS |
2980 | method, name, mode); |
2981 | if (arg_dry_run) | |
2982 | return 0; | |
342641fb | 2983 | |
6e646d22 | 2984 | r = sd_bus_call_method( |
f22f08cd | 2985 | bus, |
b0193f1c LP |
2986 | "org.freedesktop.systemd1", |
2987 | "/org/freedesktop/systemd1", | |
2988 | "org.freedesktop.systemd1.Manager", | |
6e646d22 LP |
2989 | method, |
2990 | error, | |
2991 | &reply, | |
2992 | "ss", name, mode); | |
f459b602 | 2993 | if (r < 0) { |
39602c39 TA |
2994 | const char *verb; |
2995 | ||
033f0ab8 LP |
2996 | /* There's always a fallback possible for legacy actions. */ |
2997 | if (arg_action != ACTION_SYSTEMCTL) | |
2998 | return r; | |
67f3c402 | 2999 | |
39602c39 TA |
3000 | verb = method_to_verb(method); |
3001 | ||
ee87525c FB |
3002 | log_error("Failed to %s %s: %s", verb, name, bus_error_message(error, r)); |
3003 | ||
3004 | if (!sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT) && | |
5b464a18 ZJS |
3005 | !sd_bus_error_has_name(error, BUS_ERROR_UNIT_MASKED) && |
3006 | !sd_bus_error_has_name(error, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE)) | |
a6405ca2 | 3007 | log_error("See %s logs and 'systemctl%s status%s %s' for details.", |
0b8505b7 RC |
3008 | arg_scope == UNIT_FILE_SYSTEM ? "system" : "user", |
3009 | arg_scope == UNIT_FILE_SYSTEM ? "" : " --user", | |
a6405ca2 | 3010 | name[0] == '-' ? " --" : "", |
0b8505b7 | 3011 | name); |
ee87525c FB |
3012 | |
3013 | return r; | |
7e4249b9 LP |
3014 | } |
3015 | ||
f459b602 MAP |
3016 | r = sd_bus_message_read(reply, "o", &path); |
3017 | if (r < 0) | |
3018 | return bus_log_parse_error(r); | |
45fb0699 | 3019 | |
e3e0314b | 3020 | if (need_daemon_reload(bus, name) > 0) |
3f36991e | 3021 | warn_unit_file_changed(name); |
45fb0699 | 3022 | |
ebd011d9 LP |
3023 | if (w) { |
3024 | log_debug("Adding %s to the set", path); | |
3025 | r = bus_wait_for_jobs_add(w, path); | |
cbc9fbd1 LP |
3026 | if (r < 0) |
3027 | return log_oom(); | |
e4b61340 | 3028 | } |
7e4249b9 | 3029 | |
46eddbb5 | 3030 | return 0; |
7e4249b9 LP |
3031 | } |
3032 | ||
e3e0314b | 3033 | static int expand_names(sd_bus *bus, char **names, const char* suffix, char ***ret) { |
e3e0314b ZJS |
3034 | _cleanup_strv_free_ char **mangled = NULL, **globs = NULL; |
3035 | char **name; | |
7410616c | 3036 | int r, i; |
e3e0314b | 3037 | |
4fbd7192 LP |
3038 | assert(bus); |
3039 | assert(ret); | |
3040 | ||
e3e0314b ZJS |
3041 | STRV_FOREACH(name, names) { |
3042 | char *t; | |
37cbc1d5 | 3043 | UnitNameMangle options = UNIT_NAME_MANGLE_GLOB | (arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN); |
e3e0314b | 3044 | |
e80733be | 3045 | if (suffix) |
37cbc1d5 | 3046 | r = unit_name_mangle_with_suffix(*name, options, suffix, &t); |
e80733be | 3047 | else |
37cbc1d5 | 3048 | r = unit_name_mangle(*name, options, &t); |
7410616c LP |
3049 | if (r < 0) |
3050 | return log_error_errno(r, "Failed to mangle name: %m"); | |
e3e0314b ZJS |
3051 | |
3052 | if (string_is_glob(t)) | |
6e18964d | 3053 | r = strv_consume(&globs, t); |
e3e0314b | 3054 | else |
6e18964d ZJS |
3055 | r = strv_consume(&mangled, t); |
3056 | if (r < 0) | |
e3e0314b | 3057 | return log_oom(); |
e3e0314b ZJS |
3058 | } |
3059 | ||
3060 | /* Query the manager only if any of the names are a glob, since | |
3061 | * this is fairly expensive */ | |
3062 | if (!strv_isempty(globs)) { | |
4afd3348 | 3063 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
e3e0314b | 3064 | _cleanup_free_ UnitInfo *unit_infos = NULL; |
1b53f64b | 3065 | size_t allocated, n; |
e3e0314b | 3066 | |
1238ee09 | 3067 | r = get_unit_list(bus, NULL, globs, &unit_infos, 0, &reply); |
e3e0314b ZJS |
3068 | if (r < 0) |
3069 | return r; | |
3070 | ||
1b53f64b LP |
3071 | n = strv_length(mangled); |
3072 | allocated = n + 1; | |
3073 | ||
3074 | for (i = 0; i < r; i++) { | |
3075 | if (!GREEDY_REALLOC(mangled, allocated, n+2)) | |
3076 | return log_oom(); | |
3077 | ||
3078 | mangled[n] = strdup(unit_infos[i].id); | |
3079 | if (!mangled[n]) | |
e3e0314b | 3080 | return log_oom(); |
1b53f64b LP |
3081 | |
3082 | mangled[++n] = NULL; | |
3083 | } | |
e3e0314b ZJS |
3084 | } |
3085 | ||
1cc6c93a | 3086 | *ret = TAKE_PTR(mangled); |
1238ee09 | 3087 | |
e3e0314b ZJS |
3088 | return 0; |
3089 | } | |
3090 | ||
47a0eaa6 MS |
3091 | static const struct { |
3092 | const char *target; | |
3093 | const char *verb; | |
3094 | const char *mode; | |
3095 | } action_table[_ACTION_MAX] = { | |
e68c79db ML |
3096 | [ACTION_HALT] = { SPECIAL_HALT_TARGET, "halt", "replace-irreversibly" }, |
3097 | [ACTION_POWEROFF] = { SPECIAL_POWEROFF_TARGET, "poweroff", "replace-irreversibly" }, | |
3098 | [ACTION_REBOOT] = { SPECIAL_REBOOT_TARGET, "reboot", "replace-irreversibly" }, | |
3099 | [ACTION_KEXEC] = { SPECIAL_KEXEC_TARGET, "kexec", "replace-irreversibly" }, | |
3100 | [ACTION_RUNLEVEL2] = { SPECIAL_MULTI_USER_TARGET, NULL, "isolate" }, | |
3101 | [ACTION_RUNLEVEL3] = { SPECIAL_MULTI_USER_TARGET, NULL, "isolate" }, | |
3102 | [ACTION_RUNLEVEL4] = { SPECIAL_MULTI_USER_TARGET, NULL, "isolate" }, | |
3103 | [ACTION_RUNLEVEL5] = { SPECIAL_GRAPHICAL_TARGET, NULL, "isolate" }, | |
3104 | [ACTION_RESCUE] = { SPECIAL_RESCUE_TARGET, "rescue", "isolate" }, | |
3105 | [ACTION_EMERGENCY] = { SPECIAL_EMERGENCY_TARGET, "emergency", "isolate" }, | |
3106 | [ACTION_DEFAULT] = { SPECIAL_DEFAULT_TARGET, "default", "isolate" }, | |
3107 | [ACTION_EXIT] = { SPECIAL_EXIT_TARGET, "exit", "replace-irreversibly" }, | |
3108 | [ACTION_SUSPEND] = { SPECIAL_SUSPEND_TARGET, "suspend", "replace-irreversibly" }, | |
3109 | [ACTION_HIBERNATE] = { SPECIAL_HIBERNATE_TARGET, "hibernate", "replace-irreversibly" }, | |
3110 | [ACTION_HYBRID_SLEEP] = { SPECIAL_HYBRID_SLEEP_TARGET, "hybrid-sleep", "replace-irreversibly" }, | |
3111 | [ACTION_SUSPEND_THEN_HIBERNATE] = { SPECIAL_SUSPEND_THEN_HIBERNATE_TARGET, "suspend-then-hibernate", "replace-irreversibly" }, | |
47a0eaa6 MS |
3112 | }; |
3113 | ||
514f4ef5 | 3114 | static enum action verb_to_action(const char *verb) { |
47a0eaa6 MS |
3115 | enum action i; |
3116 | ||
913c1916 | 3117 | for (i = 0; i < _ACTION_MAX; i++) |
f459b602 | 3118 | if (streq_ptr(action_table[i].verb, verb)) |
47a0eaa6 | 3119 | return i; |
514f4ef5 | 3120 | |
f459b602 MAP |
3121 | return _ACTION_INVALID; |
3122 | } | |
e4b61340 | 3123 | |
e449de87 | 3124 | static int start_unit(int argc, char *argv[], void *userdata) { |
ebd011d9 | 3125 | _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL; |
0e8d9c0c | 3126 | _cleanup_(wait_context_free) WaitContext wait_context = {}; |
08073121 | 3127 | const char *method, *mode, *one_name, *suffix = NULL; |
416b8801 | 3128 | _cleanup_free_ char **stopped_units = NULL; /* Do not use _cleanup_strv_free_ */ |
ebd011d9 | 3129 | _cleanup_strv_free_ char **names = NULL; |
0e8d9c0c | 3130 | int r, ret = EXIT_SUCCESS; |
4fbd7192 | 3131 | sd_bus *bus; |
729e3769 | 3132 | char **name; |
e4b61340 | 3133 | |
f886603b AJ |
3134 | if (arg_wait && !STR_IN_SET(argv[0], "start", "restart")) { |
3135 | log_error("--wait may only be used with the 'start' or 'restart' commands."); | |
93a08841 MP |
3136 | return -EINVAL; |
3137 | } | |
3138 | ||
3139 | /* we cannot do sender tracking on the private bus, so we need the full | |
3140 | * one for RefUnit to implement --wait */ | |
3141 | r = acquire_bus(arg_wait ? BUS_FULL : BUS_MANAGER, &bus); | |
4fbd7192 LP |
3142 | if (r < 0) |
3143 | return r; | |
3144 | ||
d2ad7e1b | 3145 | ask_password_agent_open_if_enabled(); |
8a4b13c5 | 3146 | polkit_agent_open_maybe(); |
d2ad7e1b | 3147 | |
e4b61340 | 3148 | if (arg_action == ACTION_SYSTEMCTL) { |
47a0eaa6 | 3149 | enum action action; |
e4b61340 | 3150 | |
e449de87 LP |
3151 | action = verb_to_action(argv[0]); |
3152 | ||
913c1916 AJ |
3153 | if (action != _ACTION_INVALID) { |
3154 | method = "StartUnit"; | |
3155 | mode = action_table[action].mode; | |
3156 | one_name = action_table[action].target; | |
3157 | } else { | |
3158 | if (streq(argv[0], "isolate")) { | |
3159 | method = "StartUnit"; | |
3160 | mode = "isolate"; | |
3161 | ||
3162 | suffix = ".target"; | |
3163 | } else { | |
3164 | method = verb_to_method(argv[0]); | |
3165 | mode = arg_job_mode; | |
3166 | } | |
3167 | one_name = NULL; | |
3168 | } | |
e4b61340 | 3169 | } else { |
78ca9099 | 3170 | assert(arg_action >= 0 && arg_action < _ACTION_MAX); |
47a0eaa6 | 3171 | assert(action_table[arg_action].target); |
913c1916 | 3172 | assert(action_table[arg_action].mode); |
e4b61340 LP |
3173 | |
3174 | method = "StartUnit"; | |
47a0eaa6 | 3175 | mode = action_table[arg_action].mode; |
e3e0314b | 3176 | one_name = action_table[arg_action].target; |
514f4ef5 LP |
3177 | } |
3178 | ||
8b7ec7bb | 3179 | if (one_name) { |
bea1a013 | 3180 | names = strv_new(one_name); |
8b7ec7bb LP |
3181 | if (!names) |
3182 | return log_oom(); | |
3183 | } else { | |
e449de87 | 3184 | r = expand_names(bus, strv_skip(argv, 1), suffix, &names); |
e3e0314b | 3185 | if (r < 0) |
691395d8 | 3186 | return log_error_errno(r, "Failed to expand names: %m"); |
e3e0314b | 3187 | } |
b6520546 | 3188 | |
6e905d93 | 3189 | if (!arg_no_block) { |
ebd011d9 | 3190 | r = bus_wait_for_jobs_new(bus, &w); |
f647962d MS |
3191 | if (r < 0) |
3192 | return log_error_errno(r, "Could not watch jobs: %m"); | |
e4b61340 LP |
3193 | } |
3194 | ||
93a08841 | 3195 | if (arg_wait) { |
31b2cd5d | 3196 | r = sd_bus_call_method_async( |
93a08841 | 3197 | bus, |
31b2cd5d | 3198 | NULL, |
93a08841 MP |
3199 | "org.freedesktop.systemd1", |
3200 | "/org/freedesktop/systemd1", | |
3201 | "org.freedesktop.systemd1.Manager", | |
3202 | "Subscribe", | |
31b2cd5d LP |
3203 | NULL, NULL, |
3204 | NULL); | |
93a08841 | 3205 | if (r < 0) |
31b2cd5d | 3206 | return log_error_errno(r, "Failed to enable subscription: %m"); |
93a08841 MP |
3207 | r = sd_event_default(&wait_context.event); |
3208 | if (r < 0) | |
3209 | return log_error_errno(r, "Failed to allocate event loop: %m"); | |
3210 | r = sd_bus_attach_event(bus, wait_context.event, 0); | |
3211 | if (r < 0) | |
3212 | return log_error_errno(r, "Failed to attach bus to event loop: %m"); | |
3213 | } | |
3214 | ||
b6520546 | 3215 | STRV_FOREACH(name, names) { |
4afd3348 | 3216 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
f459b602 | 3217 | |
0e8d9c0c LP |
3218 | r = start_unit_one(bus, method, *name, mode, &error, w, arg_wait ? &wait_context : NULL); |
3219 | if (ret == EXIT_SUCCESS && r < 0) | |
3220 | ret = translate_bus_error_to_exit_status(r, &error); | |
416b8801 YW |
3221 | |
3222 | if (r >= 0 && streq(method, "StopUnit")) { | |
3223 | r = strv_push(&stopped_units, *name); | |
3224 | if (r < 0) | |
3225 | return log_oom(); | |
3226 | } | |
e4b61340 LP |
3227 | } |
3228 | ||
67f3c402 | 3229 | if (!arg_no_block) { |
c11bda1e | 3230 | const char* extra_args[4] = {}; |
0e8d9c0c | 3231 | int arg_count = 0; |
4524439e | 3232 | |
4524439e ZJS |
3233 | if (arg_scope != UNIT_FILE_SYSTEM) |
3234 | extra_args[arg_count++] = "--user"; | |
3235 | ||
3236 | assert(IN_SET(arg_transport, BUS_TRANSPORT_LOCAL, BUS_TRANSPORT_REMOTE, BUS_TRANSPORT_MACHINE)); | |
3237 | if (arg_transport == BUS_TRANSPORT_REMOTE) { | |
3238 | extra_args[arg_count++] = "-H"; | |
3239 | extra_args[arg_count++] = arg_host; | |
3240 | } else if (arg_transport == BUS_TRANSPORT_MACHINE) { | |
3241 | extra_args[arg_count++] = "-M"; | |
3242 | extra_args[arg_count++] = arg_host; | |
3243 | } | |
f459b602 | 3244 | |
0e8d9c0c LP |
3245 | r = bus_wait_for_jobs(w, arg_quiet, extra_args); |
3246 | if (r < 0) | |
3247 | return r; | |
49111a70 ZJS |
3248 | |
3249 | /* When stopping units, warn if they can still be triggered by | |
3250 | * another active unit (socket, path, timer) */ | |
416b8801 YW |
3251 | if (!arg_quiet) |
3252 | STRV_FOREACH(name, stopped_units) | |
86f004fb | 3253 | (void) check_triggering_units(bus, *name); |
67f3c402 | 3254 | } |
514f4ef5 | 3255 | |
0e8d9c0c LP |
3256 | if (ret == EXIT_SUCCESS && arg_wait && !set_isempty(wait_context.unit_paths)) { |
3257 | r = sd_event_loop(wait_context.event); | |
3258 | if (r < 0) | |
3259 | return log_error_errno(r, "Failed to run event loop: %m"); | |
93a08841 | 3260 | if (wait_context.any_failed) |
0e8d9c0c | 3261 | ret = EXIT_FAILURE; |
93a08841 MP |
3262 | } |
3263 | ||
0e8d9c0c | 3264 | return ret; |
e4b61340 LP |
3265 | } |
3266 | ||
349cc4a5 | 3267 | #if ENABLE_LOGIND |
4fbd7192 | 3268 | static int logind_set_wall_message(void) { |
4afd3348 | 3269 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
4fbd7192 | 3270 | sd_bus *bus; |
f2d11d35 LP |
3271 | _cleanup_free_ char *m = NULL; |
3272 | int r; | |
3273 | ||
4fbd7192 LP |
3274 | r = acquire_bus(BUS_FULL, &bus); |
3275 | if (r < 0) | |
3276 | return r; | |
f2d11d35 LP |
3277 | |
3278 | m = strv_join(arg_wall, " "); | |
3279 | if (!m) | |
3280 | return log_oom(); | |
3281 | ||
dbc9830c ZJS |
3282 | log_debug("%s wall message \"%s\".", arg_dry_run ? "Would set" : "Setting", m); |
3283 | if (arg_dry_run) | |
3284 | return 0; | |
3285 | ||
f2d11d35 LP |
3286 | r = sd_bus_call_method( |
3287 | bus, | |
3288 | "org.freedesktop.login1", | |
3289 | "/org/freedesktop/login1", | |
3290 | "org.freedesktop.login1.Manager", | |
3291 | "SetWallMessage", | |
3292 | &error, | |
3293 | NULL, | |
3294 | "sb", | |
3295 | m, | |
3296 | !arg_no_wall); | |
3297 | ||
3298 | if (r < 0) | |
3299 | return log_warning_errno(r, "Failed to set wall message, ignoring: %s", bus_error_message(&error, r)); | |
f2d11d35 LP |
3300 | return 0; |
3301 | } | |
79f1c8f6 | 3302 | #endif |
f2d11d35 | 3303 | |
7e59bfcb | 3304 | /* Ask systemd-logind, which might grant access to unprivileged users |
d35f51ea | 3305 | * through polkit */ |
4fbd7192 | 3306 | static int logind_reboot(enum action a) { |
349cc4a5 | 3307 | #if ENABLE_LOGIND |
4afd3348 | 3308 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
58158dc7 | 3309 | const char *method, *description; |
4fbd7192 | 3310 | sd_bus *bus; |
f459b602 | 3311 | int r; |
4c80c73c | 3312 | |
4fbd7192 LP |
3313 | r = acquire_bus(BUS_FULL, &bus); |
3314 | if (r < 0) | |
3315 | return r; | |
f2d11d35 | 3316 | |
4c80c73c KS |
3317 | switch (a) { |
3318 | ||
36b69c31 LP |
3319 | case ACTION_POWEROFF: |
3320 | method = "PowerOff"; | |
3321 | description = "power off system"; | |
3322 | break; | |
3323 | ||
4c80c73c KS |
3324 | case ACTION_REBOOT: |
3325 | method = "Reboot"; | |
58158dc7 | 3326 | description = "reboot system"; |
4c80c73c KS |
3327 | break; |
3328 | ||
36b69c31 LP |
3329 | case ACTION_HALT: |
3330 | method = "Halt"; | |
3331 | description = "halt system"; | |
4c80c73c KS |
3332 | break; |
3333 | ||
d889a206 LP |
3334 | case ACTION_SUSPEND: |
3335 | method = "Suspend"; | |
58158dc7 | 3336 | description = "suspend system"; |
d889a206 LP |
3337 | break; |
3338 | ||
3339 | case ACTION_HIBERNATE: | |
3340 | method = "Hibernate"; | |
58158dc7 | 3341 | description = "hibernate system"; |
d889a206 LP |
3342 | break; |
3343 | ||
6524990f LP |
3344 | case ACTION_HYBRID_SLEEP: |
3345 | method = "HybridSleep"; | |
58158dc7 | 3346 | description = "put system into hybrid sleep"; |
6524990f LP |
3347 | break; |
3348 | ||
e68c79db ML |
3349 | case ACTION_SUSPEND_THEN_HIBERNATE: |
3350 | method = "SuspendThenHibernate"; | |
c58493c0 ML |
3351 | description = "put system into suspend followed by hibernate"; |
3352 | break; | |
3353 | ||
4c80c73c KS |
3354 | default: |
3355 | return -EINVAL; | |
3356 | } | |
3357 | ||
8a4b13c5 | 3358 | polkit_agent_open_maybe(); |
d2ad7e1b ZJS |
3359 | (void) logind_set_wall_message(); |
3360 | ||
dbc9830c ZJS |
3361 | log_debug("%s org.freedesktop.login1.Manager %s dbus call.", arg_dry_run ? "Would execute" : "Executing", method); |
3362 | if (arg_dry_run) | |
3363 | return 0; | |
3364 | ||
f459b602 | 3365 | r = sd_bus_call_method( |
f22f08cd SP |
3366 | bus, |
3367 | "org.freedesktop.login1", | |
3368 | "/org/freedesktop/login1", | |
3369 | "org.freedesktop.login1.Manager", | |
3370 | method, | |
f459b602 | 3371 | &error, |
f22f08cd | 3372 | NULL, |
342641fb | 3373 | "b", arg_ask_password); |
f459b602 | 3374 | if (r < 0) |
691395d8 | 3375 | return log_error_errno(r, "Failed to %s via logind: %s", description, bus_error_message(&error, r)); |
f459b602 | 3376 | |
691395d8 | 3377 | return 0; |
4c80c73c KS |
3378 | #else |
3379 | return -ENOSYS; | |
3380 | #endif | |
3381 | } | |
3382 | ||
4fbd7192 | 3383 | static int logind_check_inhibitors(enum action a) { |
349cc4a5 | 3384 | #if ENABLE_LOGIND |
4afd3348 | 3385 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
59164be4 | 3386 | _cleanup_strv_free_ char **sessions = NULL; |
f459b602 MAP |
3387 | const char *what, *who, *why, *mode; |
3388 | uint32_t uid, pid; | |
4fbd7192 | 3389 | sd_bus *bus; |
f459b602 | 3390 | unsigned c = 0; |
59164be4 | 3391 | char **s; |
f459b602 | 3392 | int r; |
b37844d3 | 3393 | |
748ebafa LP |
3394 | if (arg_ignore_inhibitors || arg_force > 0) |
3395 | return 0; | |
3396 | ||
3397 | if (arg_when > 0) | |
3398 | return 0; | |
3399 | ||
3400 | if (geteuid() == 0) | |
b37844d3 LP |
3401 | return 0; |
3402 | ||
3403 | if (!on_tty()) | |
3404 | return 0; | |
e08ab379 LP |
3405 | |
3406 | if (arg_transport != BUS_TRANSPORT_LOCAL) | |
3407 | return 0; | |
b37844d3 | 3408 | |
4fbd7192 LP |
3409 | r = acquire_bus(BUS_FULL, &bus); |
3410 | if (r < 0) | |
3411 | return r; | |
3412 | ||
f459b602 | 3413 | r = sd_bus_call_method( |
b37844d3 LP |
3414 | bus, |
3415 | "org.freedesktop.login1", | |
3416 | "/org/freedesktop/login1", | |
3417 | "org.freedesktop.login1.Manager", | |
3418 | "ListInhibitors", | |
b37844d3 | 3419 | NULL, |
f459b602 MAP |
3420 | &reply, |
3421 | NULL); | |
b37844d3 LP |
3422 | if (r < 0) |
3423 | /* If logind is not around, then there are no inhibitors... */ | |
3424 | return 0; | |
3425 | ||
4aa2beac | 3426 | r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssuu)"); |
f459b602 MAP |
3427 | if (r < 0) |
3428 | return bus_log_parse_error(r); | |
b37844d3 | 3429 | |
4aa2beac | 3430 | while ((r = sd_bus_message_read(reply, "(ssssuu)", &what, &who, &why, &mode, &uid, &pid)) > 0) { |
59164be4 | 3431 | _cleanup_free_ char *comm = NULL, *user = NULL; |
f459b602 | 3432 | _cleanup_strv_free_ char **sv = NULL; |
b37844d3 LP |
3433 | |
3434 | if (!streq(mode, "block")) | |
f459b602 | 3435 | continue; |
b37844d3 LP |
3436 | |
3437 | sv = strv_split(what, ":"); | |
3438 | if (!sv) | |
3439 | return log_oom(); | |
3440 | ||
ee043777 | 3441 | if (!pid_is_valid((pid_t) pid)) { |
72b3f82e | 3442 | log_error("Invalid PID "PID_FMT".", (pid_t) pid); |
ee043777 LP |
3443 | return -ERANGE; |
3444 | } | |
d028e018 | 3445 | |
b37844d3 | 3446 | if (!strv_contains(sv, |
4c315c2c IS |
3447 | IN_SET(a, |
3448 | ACTION_HALT, | |
3449 | ACTION_POWEROFF, | |
3450 | ACTION_REBOOT, | |
3451 | ACTION_KEXEC) ? "shutdown" : "sleep")) | |
f459b602 | 3452 | continue; |
b37844d3 LP |
3453 | |
3454 | get_process_comm(pid, &comm); | |
59164be4 | 3455 | user = uid_to_name(uid); |
f459b602 | 3456 | |
de0671ee | 3457 | log_warning("Operation inhibited by \"%s\" (PID "PID_FMT" \"%s\", user %s), reason is \"%s\".", |
d028e018 | 3458 | who, (pid_t) pid, strna(comm), strna(user), why); |
b37844d3 | 3459 | |
f459b602 | 3460 | c++; |
b37844d3 | 3461 | } |
f459b602 MAP |
3462 | if (r < 0) |
3463 | return bus_log_parse_error(r); | |
b37844d3 | 3464 | |
f459b602 MAP |
3465 | r = sd_bus_message_exit_container(reply); |
3466 | if (r < 0) | |
3467 | return bus_log_parse_error(r); | |
b37844d3 | 3468 | |
59164be4 LP |
3469 | /* Check for current sessions */ |
3470 | sd_get_sessions(&sessions); | |
3471 | STRV_FOREACH(s, sessions) { | |
59164be4 LP |
3472 | _cleanup_free_ char *type = NULL, *tty = NULL, *seat = NULL, *user = NULL, *service = NULL, *class = NULL; |
3473 | ||
3474 | if (sd_session_get_uid(*s, &uid) < 0 || uid == getuid()) | |
3475 | continue; | |
3476 | ||
3477 | if (sd_session_get_class(*s, &class) < 0 || !streq(class, "user")) | |
3478 | continue; | |
3479 | ||
28abce5d | 3480 | if (sd_session_get_type(*s, &type) < 0 || !STR_IN_SET(type, "x11", "wayland", "tty", "mir")) |
59164be4 LP |
3481 | continue; |
3482 | ||
3483 | sd_session_get_tty(*s, &tty); | |
3484 | sd_session_get_seat(*s, &seat); | |
3485 | sd_session_get_service(*s, &service); | |
3486 | user = uid_to_name(uid); | |
3487 | ||
3488 | log_warning("User %s is logged in on %s.", strna(user), isempty(tty) ? (isempty(seat) ? strna(service) : seat) : tty); | |
3489 | c++; | |
3490 | } | |
3491 | ||
b37844d3 LP |
3492 | if (c <= 0) |
3493 | return 0; | |
3494 | ||
59164be4 | 3495 | log_error("Please retry operation after closing inhibitors and logging out other users.\nAlternatively, ignore inhibitors and users with 'systemctl %s -i'.", |
47a0eaa6 | 3496 | action_table[a].verb); |
b37844d3 LP |
3497 | |
3498 | return -EPERM; | |
3499 | #else | |
3500 | return 0; | |
3501 | #endif | |
3502 | } | |
3503 | ||
97af80c5 LP |
3504 | static int prepare_firmware_setup(void) { |
3505 | ||
3506 | if (!arg_firmware_setup) | |
3507 | return 0; | |
3508 | ||
349cc4a5 | 3509 | #if ENABLE_LOGIND |
4afd3348 | 3510 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
4fbd7192 | 3511 | sd_bus *bus; |
a4921cf4 JJ |
3512 | int r; |
3513 | ||
4fbd7192 LP |
3514 | r = acquire_bus(BUS_FULL, &bus); |
3515 | if (r < 0) | |
3516 | return r; | |
a4921cf4 | 3517 | |
a4921cf4 JJ |
3518 | r = sd_bus_call_method( |
3519 | bus, | |
3520 | "org.freedesktop.login1", | |
3521 | "/org/freedesktop/login1", | |
3522 | "org.freedesktop.login1.Manager", | |
3523 | "SetRebootToFirmwareSetup", | |
3524 | &error, | |
3525 | NULL, | |
3526 | "b", true); | |
4fbd7192 LP |
3527 | if (r < 0) |
3528 | return log_error_errno(r, "Cannot indicate to EFI to boot into setup mode: %s", bus_error_message(&error, r)); | |
a4921cf4 JJ |
3529 | |
3530 | return 0; | |
3531 | #else | |
97af80c5 | 3532 | log_error("Booting into firmware setup not supported."); |
4fbd7192 | 3533 | return -ENOSYS; |
a4921cf4 JJ |
3534 | #endif |
3535 | } | |
3536 | ||
97af80c5 LP |
3537 | static int prepare_boot_loader_menu(void) { |
3538 | ||
3539 | if (arg_boot_loader_menu == USEC_INFINITY) | |
3540 | return 0; | |
3541 | ||
3542 | #if ENABLE_LOGIND | |
3543 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
3544 | sd_bus *bus; | |
4fbd7192 LP |
3545 | int r; |
3546 | ||
97af80c5 LP |
3547 | r = acquire_bus(BUS_FULL, &bus); |
3548 | if (r < 0) | |
3549 | return r; | |
3550 | ||
3551 | r = sd_bus_call_method( | |
3552 | bus, | |
3553 | "org.freedesktop.login1", | |
3554 | "/org/freedesktop/login1", | |
3555 | "org.freedesktop.login1.Manager", | |
3556 | "SetRebootToBootLoaderMenu", | |
3557 | &error, | |
3558 | NULL, | |
3559 | "t", arg_boot_loader_menu); | |
3560 | if (r < 0) | |
3561 | return log_error_errno(r, "Cannot indicate to boot loader to enter boot loader entry menu: %s", bus_error_message(&error, r)); | |
3562 | ||
3563 | return 0; | |
3564 | #else | |
3565 | log_error("Booting into boot loader menu not supported."); | |
3566 | return -ENOSYS; | |
3567 | #endif | |
3568 | } | |
3569 | ||
3570 | static int prepare_boot_loader_entry(void) { | |
3571 | ||
3572 | if (!arg_boot_loader_entry) | |
4fbd7192 LP |
3573 | return 0; |
3574 | ||
97af80c5 LP |
3575 | #if ENABLE_LOGIND |
3576 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
3577 | sd_bus *bus; | |
3578 | int r; | |
4fbd7192 | 3579 | |
97af80c5 LP |
3580 | r = acquire_bus(BUS_FULL, &bus); |
3581 | if (r < 0) | |
3582 | return r; | |
3583 | ||
3584 | r = sd_bus_call_method( | |
3585 | bus, | |
3586 | "org.freedesktop.login1", | |
3587 | "/org/freedesktop/login1", | |
3588 | "org.freedesktop.login1.Manager", | |
3589 | "SetRebootToBootLoaderEntry", | |
3590 | &error, | |
3591 | NULL, | |
3592 | "s", arg_boot_loader_entry); | |
3593 | if (r < 0) | |
3594 | return log_error_errno(r, "Cannot set boot into loader entry '%s': %s", arg_boot_loader_entry, bus_error_message(&error, r)); | |
4fbd7192 | 3595 | |
97af80c5 LP |
3596 | return 0; |
3597 | #else | |
3598 | log_error("Booting into boot loader entry not supported."); | |
3599 | return -ENOSYS; | |
3600 | #endif | |
4fbd7192 LP |
3601 | } |
3602 | ||
4bb2e9d4 ZJS |
3603 | static int load_kexec_kernel(void) { |
3604 | _cleanup_(boot_config_free) BootConfig config = {}; | |
43b736a8 | 3605 | _cleanup_free_ char *kernel = NULL, *initrd = NULL, *options = NULL; |
4bb2e9d4 ZJS |
3606 | const BootEntry *e; |
3607 | pid_t pid; | |
3608 | int r; | |
3609 | ||
3610 | if (kexec_loaded()) { | |
3611 | log_debug("Kexec kernel already loaded."); | |
3612 | return 0; | |
3613 | } | |
3614 | ||
c0d73214 ZJS |
3615 | if (access(KEXEC, X_OK) < 0) |
3616 | return log_error_errno(errno, KEXEC" is not available: %m"); | |
3617 | ||
38bd74d6 | 3618 | r = boot_entries_load_config_auto(NULL, NULL, &config); |
cc5957dc LP |
3619 | if (r == -ENOKEY) /* The call doesn't log about ENOKEY, let's do so here. */ |
3620 | return log_error_errno(r, "Cannot find the ESP partition mount point."); | |
4bb2e9d4 | 3621 | if (r < 0) |
1b20d889 | 3622 | return r; |
4bb2e9d4 | 3623 | |
38bd74d6 LP |
3624 | e = boot_config_default_entry(&config); |
3625 | if (!e) | |
3626 | return log_error_errno(SYNTHETIC_ERRNO(ENOENT), | |
3627 | "No boot loader entry suitable as default, refusing to guess."); | |
3628 | ||
3629 | log_debug("Found default boot loader entry in file \"%s\"", e->path); | |
3630 | ||
78c38fe7 LP |
3631 | if (!e->kernel) |
3632 | return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), | |
3633 | "Boot entry does not refer to Linux kernel, which is not supported currently."); | |
8a1ef3bf | 3634 | if (strv_length(e->initrd) > 1) |
0f88d829 | 3635 | return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), |
8a1ef3bf | 3636 | "Boot entry specifies multiple initrds, which is not supported currently."); |
4bb2e9d4 | 3637 | |
43b736a8 | 3638 | kernel = path_join(e->root, e->kernel); |
9fc32cd9 LP |
3639 | if (!kernel) |
3640 | return log_oom(); | |
3641 | ||
3642 | if (!strv_isempty(e->initrd)) { | |
0f88d829 | 3643 | initrd = path_join(e->root, e->initrd[0]); |
9fc32cd9 LP |
3644 | if (!initrd) |
3645 | return log_oom(); | |
3646 | } | |
3647 | ||
4bb2e9d4 ZJS |
3648 | options = strv_join(e->options, " "); |
3649 | if (!options) | |
3650 | return log_oom(); | |
3651 | ||
cd086a01 ZJS |
3652 | log_full(arg_quiet ? LOG_DEBUG : LOG_INFO, |
3653 | "%s "KEXEC" --load \"%s\" --append \"%s\"%s%s%s", | |
3654 | arg_dry_run ? "Would run" : "Running", | |
3655 | kernel, | |
3656 | options, | |
3657 | initrd ? " --initrd \"" : NULL, strempty(initrd), initrd ? "\"" : ""); | |
4bb2e9d4 ZJS |
3658 | if (arg_dry_run) |
3659 | return 0; | |
3660 | ||
c943e95f | 3661 | r = safe_fork("(kexec)", FORK_WAIT|FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG, &pid); |
4c253ed1 | 3662 | if (r < 0) |
b6e1fff1 | 3663 | return r; |
4c253ed1 | 3664 | if (r == 0) { |
4bb2e9d4 ZJS |
3665 | const char* const args[] = { |
3666 | KEXEC, | |
3667 | "--load", kernel, | |
3668 | "--append", options, | |
3669 | initrd ? "--initrd" : NULL, initrd, | |
c943e95f LP |
3670 | NULL |
3671 | }; | |
4bb2e9d4 ZJS |
3672 | |
3673 | /* Child */ | |
4bb2e9d4 ZJS |
3674 | execv(args[0], (char * const *) args); |
3675 | _exit(EXIT_FAILURE); | |
4c253ed1 LP |
3676 | } |
3677 | ||
df685d57 | 3678 | return 0; |
4bb2e9d4 ZJS |
3679 | } |
3680 | ||
57ab9006 | 3681 | static int set_exit_code(uint8_t code) { |
4afd3348 | 3682 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
57ab9006 LP |
3683 | sd_bus *bus; |
3684 | int r; | |
3685 | ||
3686 | r = acquire_bus(BUS_MANAGER, &bus); | |
3687 | if (r < 0) | |
3688 | return r; | |
3689 | ||
3690 | r = sd_bus_call_method( | |
3691 | bus, | |
3692 | "org.freedesktop.systemd1", | |
3693 | "/org/freedesktop/systemd1", | |
3694 | "org.freedesktop.systemd1.Manager", | |
3695 | "SetExitCode", | |
3696 | &error, | |
3697 | NULL, | |
3698 | "y", code); | |
3699 | if (r < 0) | |
af3d8113 | 3700 | return log_error_errno(r, "Failed to set exit code: %s", bus_error_message(&error, r)); |
57ab9006 LP |
3701 | |
3702 | return 0; | |
3703 | } | |
3704 | ||
e449de87 | 3705 | static int start_special(int argc, char *argv[], void *userdata) { |
4c80c73c | 3706 | enum action a; |
983d9c90 | 3707 | int r; |
2b0f4e6f ZJS |
3708 | bool termination_action; /* an action that terminates the manager, |
3709 | * can be performed also by signal. */ | |
983d9c90 | 3710 | |
e449de87 | 3711 | assert(argv); |
514f4ef5 | 3712 | |
e449de87 | 3713 | a = verb_to_action(argv[0]); |
4c80c73c | 3714 | |
4fbd7192 | 3715 | r = logind_check_inhibitors(a); |
748ebafa LP |
3716 | if (r < 0) |
3717 | return r; | |
3718 | ||
fba868fa LP |
3719 | if (arg_force >= 2) { |
3720 | r = must_be_root(); | |
3721 | if (r < 0) | |
3722 | return r; | |
c32b90de LP |
3723 | } |
3724 | ||
4fbd7192 | 3725 | r = prepare_firmware_setup(); |
a4921cf4 JJ |
3726 | if (r < 0) |
3727 | return r; | |
5bdf2243 | 3728 | |
97af80c5 LP |
3729 | r = prepare_boot_loader_menu(); |
3730 | if (r < 0) | |
3731 | return r; | |
3732 | ||
3733 | r = prepare_boot_loader_entry(); | |
3734 | if (r < 0) | |
3735 | return r; | |
3736 | ||
e449de87 | 3737 | if (a == ACTION_REBOOT && argc > 1) { |
27c06cb5 | 3738 | r = update_reboot_parameter_and_warn(argv[1]); |
b986229e SW |
3739 | if (r < 0) |
3740 | return r; | |
57ab9006 | 3741 | |
4bb2e9d4 ZJS |
3742 | } else if (a == ACTION_KEXEC) { |
3743 | r = load_kexec_kernel(); | |
d23b5ce2 ZJS |
3744 | if (r < 0 && arg_force >= 1) |
3745 | log_notice("Failed to load kexec kernel, continuing without."); | |
3746 | else if (r < 0) | |
4bb2e9d4 ZJS |
3747 | return r; |
3748 | ||
e449de87 | 3749 | } else if (a == ACTION_EXIT && argc > 1) { |
7bbb5359 | 3750 | uint8_t code; |
287419c1 | 3751 | |
57ab9006 LP |
3752 | /* If the exit code is not given on the command line, |
3753 | * don't reset it to zero: just keep it as it might | |
3754 | * have been set previously. */ | |
287419c1 | 3755 | |
e449de87 | 3756 | r = safe_atou8(argv[1], &code); |
4fbd7192 | 3757 | if (r < 0) |
57ab9006 | 3758 | return log_error_errno(r, "Invalid exit code."); |
4fbd7192 | 3759 | |
57ab9006 | 3760 | r = set_exit_code(code); |
691395d8 | 3761 | if (r < 0) |
57ab9006 | 3762 | return r; |
b986229e SW |
3763 | } |
3764 | ||
2b0f4e6f ZJS |
3765 | termination_action = IN_SET(a, |
3766 | ACTION_HALT, | |
3767 | ACTION_POWEROFF, | |
3768 | ACTION_REBOOT); | |
3769 | if (termination_action && arg_force >= 2) | |
477def80 | 3770 | return halt_now(a); |
e606bb61 | 3771 | |
7e59bfcb | 3772 | if (arg_force >= 1 && |
5955df2c | 3773 | (termination_action || IN_SET(a, ACTION_KEXEC, ACTION_EXIT))) |
2b0f4e6f ZJS |
3774 | r = trivial_method(argc, argv, userdata); |
3775 | else { | |
3776 | /* First try logind, to allow authentication with polkit */ | |
3777 | if (IN_SET(a, | |
3778 | ACTION_POWEROFF, | |
3779 | ACTION_REBOOT, | |
36b69c31 | 3780 | ACTION_HALT, |
2b0f4e6f ZJS |
3781 | ACTION_SUSPEND, |
3782 | ACTION_HIBERNATE, | |
c58493c0 | 3783 | ACTION_HYBRID_SLEEP, |
e68c79db | 3784 | ACTION_SUSPEND_THEN_HIBERNATE)) { |
2b0f4e6f ZJS |
3785 | |
3786 | r = logind_reboot(a); | |
3787 | if (r >= 0) | |
3788 | return r; | |
3789 | if (IN_SET(r, -EOPNOTSUPP, -EINPROGRESS)) | |
3790 | /* requested operation is not supported or already in progress */ | |
3791 | return r; | |
20b09ca7 | 3792 | |
130246d2 LP |
3793 | /* On all other errors, try low-level operation. In order to minimize the difference between |
3794 | * operation with and without logind, we explicitly enable non-blocking mode for this, as | |
3795 | * logind's shutdown operations are always non-blocking. */ | |
3796 | ||
3797 | arg_no_block = true; | |
3798 | ||
3799 | } else if (IN_SET(a, ACTION_EXIT, ACTION_KEXEC)) | |
3800 | /* Since exit/kexec are so close in behaviour to power-off/reboot, let's also make them | |
3801 | * asynchronous, in order to not confuse the user needlessly with unexpected behaviour. */ | |
3802 | arg_no_block = true; | |
7089051f | 3803 | |
2b0f4e6f | 3804 | r = start_unit(argc, argv, userdata); |
4c80c73c | 3805 | } |
983d9c90 | 3806 | |
2b0f4e6f ZJS |
3807 | if (termination_action && arg_force < 2 && |
3808 | IN_SET(r, -ENOENT, -ETIMEDOUT)) | |
cc7de2ba | 3809 | log_notice("It is possible to perform action directly, see discussion of --force --force in man:systemctl(1)."); |
2b0f4e6f ZJS |
3810 | |
3811 | return r; | |
514f4ef5 LP |
3812 | } |
3813 | ||
988b3b17 ZJS |
3814 | static int start_system_special(int argc, char *argv[], void *userdata) { |
3815 | /* Like start_special above, but raises an error when running in user mode */ | |
3816 | ||
baaa35ad ZJS |
3817 | if (arg_scope != UNIT_FILE_SYSTEM) |
3818 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
3819 | "Bad action for %s mode.", | |
3820 | arg_scope == UNIT_FILE_GLOBAL ? "--global" : "--user"); | |
988b3b17 ZJS |
3821 | |
3822 | return start_special(argc, argv, userdata); | |
3823 | } | |
3824 | ||
fa0d5878 | 3825 | static int check_unit_generic(int code, const UnitActiveState good_states[], int nb_states, char **args) { |
e3e0314b | 3826 | _cleanup_strv_free_ char **names = NULL; |
fa0d5878 | 3827 | UnitActiveState active_state; |
4fbd7192 | 3828 | sd_bus *bus; |
729e3769 | 3829 | char **name; |
fa0d5878 | 3830 | int r, i; |
d60f6ad0 | 3831 | bool found = false; |
0183528f | 3832 | |
4fbd7192 LP |
3833 | r = acquire_bus(BUS_MANAGER, &bus); |
3834 | if (r < 0) | |
3835 | return r; | |
3836 | ||
e3e0314b | 3837 | r = expand_names(bus, args, NULL, &names); |
f647962d MS |
3838 | if (r < 0) |
3839 | return log_error_errno(r, "Failed to expand names: %m"); | |
e3e0314b ZJS |
3840 | |
3841 | STRV_FOREACH(name, names) { | |
fa0d5878 BR |
3842 | r = get_state_one_unit(bus, *name, &active_state); |
3843 | if (r < 0) | |
3844 | return r; | |
3845 | ||
3846 | if (!arg_quiet) | |
3847 | puts(unit_active_state_to_string(active_state)); | |
60f9ba0b | 3848 | |
fa0d5878 BR |
3849 | for (i = 0; i < nb_states; ++i) |
3850 | if (good_states[i] == active_state) | |
3851 | found = true; | |
1a0fce45 TA |
3852 | } |
3853 | ||
d60f6ad0 LN |
3854 | /* use the given return code for the case that we won't find |
3855 | * any unit which matches the list */ | |
3856 | return found ? 0 : code; | |
1a0fce45 TA |
3857 | } |
3858 | ||
e449de87 | 3859 | static int check_unit_active(int argc, char *argv[], void *userdata) { |
9897b09b LP |
3860 | static const UnitActiveState states[] = { |
3861 | UNIT_ACTIVE, | |
3862 | UNIT_RELOADING, | |
3863 | }; | |
3864 | ||
e3e0314b | 3865 | /* According to LSB: 3, "program is not running" */ |
b613907e | 3866 | return check_unit_generic(EXIT_PROGRAM_NOT_RUNNING, states, ELEMENTSOF(states), strv_skip(argv, 1)); |
e3e0314b | 3867 | } |
0183528f | 3868 | |
e449de87 | 3869 | static int check_unit_failed(int argc, char *argv[], void *userdata) { |
9897b09b LP |
3870 | static const UnitActiveState states[] = { |
3871 | UNIT_FAILED, | |
3872 | }; | |
3873 | ||
b613907e | 3874 | return check_unit_generic(EXIT_PROGRAM_DEAD_AND_PID_EXISTS, states, ELEMENTSOF(states), strv_skip(argv, 1)); |
48220598 LP |
3875 | } |
3876 | ||
e449de87 | 3877 | static int kill_unit(int argc, char *argv[], void *userdata) { |
e3e0314b | 3878 | _cleanup_strv_free_ char **names = NULL; |
ac5e3a50 | 3879 | char *kill_who = NULL, **name; |
4fbd7192 | 3880 | sd_bus *bus; |
e3e0314b | 3881 | int r, q; |
8a0867d6 | 3882 | |
4fbd7192 LP |
3883 | r = acquire_bus(BUS_MANAGER, &bus); |
3884 | if (r < 0) | |
3885 | return r; | |
3886 | ||
8a4b13c5 | 3887 | polkit_agent_open_maybe(); |
d2ad7e1b | 3888 | |
8a0867d6 LP |
3889 | if (!arg_kill_who) |
3890 | arg_kill_who = "all"; | |
3891 | ||
ac5e3a50 JS |
3892 | /* --fail was specified */ |
3893 | if (streq(arg_job_mode, "fail")) | |
81d62103 | 3894 | kill_who = strjoina(arg_kill_who, "-fail"); |
ac5e3a50 | 3895 | |
e449de87 | 3896 | r = expand_names(bus, strv_skip(argv, 1), NULL, &names); |
e3e0314b | 3897 | if (r < 0) |
691395d8 | 3898 | return log_error_errno(r, "Failed to expand names: %m"); |
60f9ba0b | 3899 | |
e3e0314b | 3900 | STRV_FOREACH(name, names) { |
4afd3348 | 3901 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
342641fb | 3902 | |
6e646d22 | 3903 | q = sd_bus_call_method( |
f22f08cd | 3904 | bus, |
b0193f1c LP |
3905 | "org.freedesktop.systemd1", |
3906 | "/org/freedesktop/systemd1", | |
3907 | "org.freedesktop.systemd1.Manager", | |
6e646d22 LP |
3908 | "KillUnit", |
3909 | &error, | |
3910 | NULL, | |
169d7fb6 | 3911 | "ssi", *name, kill_who ? kill_who : arg_kill_who, arg_signal); |
e3e0314b | 3912 | if (q < 0) { |
169d7fb6 | 3913 | log_error_errno(q, "Failed to kill unit %s: %s", *name, bus_error_message(&error, q)); |
e3e0314b ZJS |
3914 | if (r == 0) |
3915 | r = q; | |
f459b602 | 3916 | } |
8a0867d6 | 3917 | } |
f459b602 | 3918 | |
e3e0314b | 3919 | return r; |
8a0867d6 LP |
3920 | } |
3921 | ||
582a507f | 3922 | typedef struct ExecStatusInfo { |
0129173a LP |
3923 | char *name; |
3924 | ||
582a507f LP |
3925 | char *path; |
3926 | char **argv; | |
3927 | ||
b708e7ce LP |
3928 | bool ignore; |
3929 | ||
582a507f LP |
3930 | usec_t start_timestamp; |
3931 | usec_t exit_timestamp; | |
3932 | pid_t pid; | |
3933 | int code; | |
3934 | int status; | |
3935 | ||
3936 | LIST_FIELDS(struct ExecStatusInfo, exec); | |
3937 | } ExecStatusInfo; | |
3938 | ||
3939 | static void exec_status_info_free(ExecStatusInfo *i) { | |
3940 | assert(i); | |
3941 | ||
0129173a | 3942 | free(i->name); |
582a507f LP |
3943 | free(i->path); |
3944 | strv_free(i->argv); | |
3945 | free(i); | |
3946 | } | |
3947 | ||
f459b602 | 3948 | static int exec_status_info_deserialize(sd_bus_message *m, ExecStatusInfo *i) { |
b21a0ef8 | 3949 | uint64_t start_timestamp, exit_timestamp, start_timestamp_monotonic, exit_timestamp_monotonic; |
f459b602 | 3950 | const char *path; |
582a507f LP |
3951 | uint32_t pid; |
3952 | int32_t code, status; | |
f459b602 | 3953 | int ignore, r; |
582a507f | 3954 | |
f459b602 | 3955 | assert(m); |
582a507f | 3956 | assert(i); |
582a507f | 3957 | |
f459b602 MAP |
3958 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_STRUCT, "sasbttttuii"); |
3959 | if (r < 0) | |
3960 | return bus_log_parse_error(r); | |
3961 | else if (r == 0) | |
3962 | return 0; | |
582a507f | 3963 | |
f459b602 MAP |
3964 | r = sd_bus_message_read(m, "s", &path); |
3965 | if (r < 0) | |
3966 | return bus_log_parse_error(r); | |
582a507f | 3967 | |
f84190d8 LP |
3968 | i->path = strdup(path); |
3969 | if (!i->path) | |
f459b602 | 3970 | return log_oom(); |
582a507f | 3971 | |
f459b602 MAP |
3972 | r = sd_bus_message_read_strv(m, &i->argv); |
3973 | if (r < 0) | |
3974 | return bus_log_parse_error(r); | |
3975 | ||
3976 | r = sd_bus_message_read(m, | |
3977 | "bttttuii", | |
3978 | &ignore, | |
3979 | &start_timestamp, &start_timestamp_monotonic, | |
3980 | &exit_timestamp, &exit_timestamp_monotonic, | |
3981 | &pid, | |
3982 | &code, &status); | |
3983 | if (r < 0) | |
3984 | return bus_log_parse_error(r); | |
582a507f | 3985 | |
b708e7ce | 3986 | i->ignore = ignore; |
582a507f LP |
3987 | i->start_timestamp = (usec_t) start_timestamp; |
3988 | i->exit_timestamp = (usec_t) exit_timestamp; | |
3989 | i->pid = (pid_t) pid; | |
3990 | i->code = code; | |
3991 | i->status = status; | |
3992 | ||
f459b602 MAP |
3993 | r = sd_bus_message_exit_container(m); |
3994 | if (r < 0) | |
3995 | return bus_log_parse_error(r); | |
3996 | ||
3997 | return 1; | |
582a507f LP |
3998 | } |
3999 | ||
d5db7fe6 WC |
4000 | typedef struct UnitCondition { |
4001 | char *name; | |
b1ed76ae | 4002 | char *param; |
d5db7fe6 WC |
4003 | bool trigger; |
4004 | bool negate; | |
d5db7fe6 WC |
4005 | int tristate; |
4006 | ||
b1ed76ae | 4007 | LIST_FIELDS(struct UnitCondition, conditions); |
d5db7fe6 WC |
4008 | } UnitCondition; |
4009 | ||
4010 | static void unit_condition_free(UnitCondition *c) { | |
9bb71940 ZJS |
4011 | if (!c) |
4012 | return; | |
d5db7fe6 WC |
4013 | |
4014 | free(c->name); | |
4015 | free(c->param); | |
4016 | free(c); | |
4017 | } | |
4018 | ||
9bb71940 ZJS |
4019 | DEFINE_TRIVIAL_CLEANUP_FUNC(UnitCondition*, unit_condition_free); |
4020 | ||
61cbdc4b LP |
4021 | typedef struct UnitStatusInfo { |
4022 | const char *id; | |
4023 | const char *load_state; | |
4024 | const char *active_state; | |
4025 | const char *sub_state; | |
a4375746 | 4026 | const char *unit_file_state; |
d2dc52db | 4027 | const char *unit_file_preset; |
61cbdc4b LP |
4028 | |
4029 | const char *description; | |
4a9e2fff | 4030 | const char *following; |
61cbdc4b | 4031 | |
49dbfa7b LP |
4032 | char **documentation; |
4033 | ||
1b64d026 LP |
4034 | const char *fragment_path; |
4035 | const char *source_path; | |
4ad49000 | 4036 | const char *control_group; |
61cbdc4b | 4037 | |
76d14b87 OS |
4038 | char **dropin_paths; |
4039 | ||
9f39404c | 4040 | const char *load_error; |
f42806df | 4041 | const char *result; |
9f39404c | 4042 | |
584be568 | 4043 | usec_t inactive_exit_timestamp; |
df50185b | 4044 | usec_t inactive_exit_timestamp_monotonic; |
584be568 LP |
4045 | usec_t active_enter_timestamp; |
4046 | usec_t active_exit_timestamp; | |
4047 | usec_t inactive_enter_timestamp; | |
4048 | ||
45fb0699 | 4049 | bool need_daemon_reload; |
055ef36b | 4050 | bool transient; |
45fb0699 | 4051 | |
61cbdc4b LP |
4052 | /* Service */ |
4053 | pid_t main_pid; | |
4054 | pid_t control_pid; | |
4055 | const char *status_text; | |
175728c4 | 4056 | const char *pid_file; |
d06dacd0 | 4057 | bool running:1; |
b4af5a80 | 4058 | int status_errno; |
61cbdc4b LP |
4059 | |
4060 | usec_t start_timestamp; | |
4061 | usec_t exit_timestamp; | |
4062 | ||
4063 | int exit_code, exit_status; | |
4064 | ||
90bbc946 LP |
4065 | usec_t condition_timestamp; |
4066 | bool condition_result; | |
b1ed76ae | 4067 | LIST_HEAD(UnitCondition, conditions); |
59fccdc5 LP |
4068 | |
4069 | usec_t assert_timestamp; | |
4070 | bool assert_result; | |
4071 | bool failed_assert_trigger; | |
4072 | bool failed_assert_negate; | |
4073 | const char *failed_assert; | |
4074 | const char *failed_assert_parameter; | |
601b0842 GS |
4075 | usec_t next_elapse_real; |
4076 | usec_t next_elapse_monotonic; | |
90bbc946 | 4077 | |
61cbdc4b LP |
4078 | /* Socket */ |
4079 | unsigned n_accepted; | |
4080 | unsigned n_connections; | |
a98f7575 | 4081 | unsigned n_refused; |
b8131a87 | 4082 | bool accept; |
61cbdc4b | 4083 | |
13160134 | 4084 | /* Pairs of type, path */ |
67419600 OS |
4085 | char **listen; |
4086 | ||
61cbdc4b LP |
4087 | /* Device */ |
4088 | const char *sysfs_path; | |
4089 | ||
4090 | /* Mount, Automount */ | |
4091 | const char *where; | |
4092 | ||
4093 | /* Swap */ | |
4094 | const char *what; | |
582a507f | 4095 | |
934277fe LP |
4096 | /* CGroup */ |
4097 | uint64_t memory_current; | |
48422635 | 4098 | uint64_t memory_min; |
da4d897e TH |
4099 | uint64_t memory_low; |
4100 | uint64_t memory_high; | |
4101 | uint64_t memory_max; | |
96e131ea | 4102 | uint64_t memory_swap_max; |
934277fe | 4103 | uint64_t memory_limit; |
5ad096b3 | 4104 | uint64_t cpu_usage_nsec; |
03a7b521 LP |
4105 | uint64_t tasks_current; |
4106 | uint64_t tasks_max; | |
934277fe | 4107 | |
0e97c93f DM |
4108 | uint64_t ip_ingress_bytes; |
4109 | uint64_t ip_egress_bytes; | |
4110 | ||
582a507f | 4111 | LIST_HEAD(ExecStatusInfo, exec); |
61cbdc4b LP |
4112 | } UnitStatusInfo; |
4113 | ||
a7335518 ZJS |
4114 | static void unit_status_info_free(UnitStatusInfo *info) { |
4115 | ExecStatusInfo *p; | |
4116 | UnitCondition *c; | |
4117 | ||
4118 | strv_free(info->documentation); | |
4119 | strv_free(info->dropin_paths); | |
4120 | strv_free(info->listen); | |
4121 | ||
4122 | while ((c = info->conditions)) { | |
4123 | LIST_REMOVE(conditions, info->conditions, c); | |
4124 | unit_condition_free(c); | |
4125 | } | |
4126 | ||
4127 | while ((p = info->exec)) { | |
4128 | LIST_REMOVE(exec, info->exec, p); | |
4129 | exec_status_info_free(p); | |
4130 | } | |
4131 | } | |
4132 | ||
f459b602 | 4133 | static void print_status_info( |
291d565a | 4134 | sd_bus *bus, |
f459b602 MAP |
4135 | UnitStatusInfo *i, |
4136 | bool *ellipsized) { | |
4137 | ||
4d9685be ZJS |
4138 | char since1[FORMAT_TIMESTAMP_RELATIVE_MAX], since2[FORMAT_TIMESTAMP_MAX]; |
4139 | const char *s1, *s2, *active_on, *active_off, *on, *off, *ss; | |
c7080257 LP |
4140 | _cleanup_free_ char *formatted_path = NULL; |
4141 | ExecStatusInfo *p; | |
584be568 | 4142 | usec_t timestamp; |
1b64d026 | 4143 | const char *path; |
13160134 | 4144 | char **t, **t2; |
291d565a | 4145 | int r; |
582a507f | 4146 | |
61cbdc4b LP |
4147 | assert(i); |
4148 | ||
4149 | /* This shows pretty information about a unit. See | |
4150 | * print_property() for a low-level property printer */ | |
4151 | ||
b0d14c69 LP |
4152 | if (streq_ptr(i->active_state, "failed")) { |
4153 | active_on = ansi_highlight_red(); | |
1fc464f6 | 4154 | active_off = ansi_normal(); |
1cf03a4f | 4155 | } else if (STRPTR_IN_SET(i->active_state, "active", "reloading")) { |
b0d14c69 | 4156 | active_on = ansi_highlight_green(); |
1fc464f6 | 4157 | active_off = ansi_normal(); |
b0d14c69 LP |
4158 | } else |
4159 | active_on = active_off = ""; | |
4160 | ||
9a6f746f | 4161 | printf("%s%s%s %s", active_on, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE), active_off, strna(i->id)); |
61cbdc4b LP |
4162 | |
4163 | if (i->description && !streq_ptr(i->id, i->description)) | |
4164 | printf(" - %s", i->description); | |
4165 | ||
4166 | printf("\n"); | |
4167 | ||
4a9e2fff | 4168 | if (i->following) |
856323c9 | 4169 | printf(" Follow: unit currently follows state of %s\n", i->following); |
4a9e2fff | 4170 | |
c4555ad8 | 4171 | if (STRPTR_IN_SET(i->load_state, "error", "not-found", "bad-setting")) { |
0b5a519c | 4172 | on = ansi_highlight_red(); |
1fc464f6 | 4173 | off = ansi_normal(); |
c31b4423 LP |
4174 | } else |
4175 | on = off = ""; | |
4176 | ||
c7080257 LP |
4177 | path = i->source_path ?: i->fragment_path; |
4178 | if (path && terminal_urlify_path(path, NULL, &formatted_path) >= 0) | |
4179 | path = formatted_path; | |
1b64d026 | 4180 | |
9a0abfa8 | 4181 | if (!isempty(i->load_error)) |
856323c9 ZJS |
4182 | printf(" Loaded: %s%s%s (Reason: %s)\n", |
4183 | on, strna(i->load_state), off, i->load_error); | |
15ce1647 LP |
4184 | else if (path && !isempty(i->unit_file_state) && !isempty(i->unit_file_preset) && |
4185 | !STR_IN_SET(i->unit_file_state, "generated", "transient")) | |
d2dc52db LP |
4186 | printf(" Loaded: %s%s%s (%s; %s; vendor preset: %s)\n", |
4187 | on, strna(i->load_state), off, path, i->unit_file_state, i->unit_file_preset); | |
4188 | else if (path && !isempty(i->unit_file_state)) | |
856323c9 ZJS |
4189 | printf(" Loaded: %s%s%s (%s; %s)\n", |
4190 | on, strna(i->load_state), off, path, i->unit_file_state); | |
1b64d026 | 4191 | else if (path) |
856323c9 ZJS |
4192 | printf(" Loaded: %s%s%s (%s)\n", |
4193 | on, strna(i->load_state), off, path); | |
61cbdc4b | 4194 | else |
856323c9 ZJS |
4195 | printf(" Loaded: %s%s%s\n", |
4196 | on, strna(i->load_state), off); | |
61cbdc4b | 4197 | |
055ef36b LP |
4198 | if (i->transient) |
4199 | printf("Transient: yes\n"); | |
4200 | ||
76d14b87 | 4201 | if (!strv_isempty(i->dropin_paths)) { |
f459b602 | 4202 | _cleanup_free_ char *dir = NULL; |
76d14b87 | 4203 | bool last = false; |
f459b602 | 4204 | char ** dropin; |
76d14b87 OS |
4205 | |
4206 | STRV_FOREACH(dropin, i->dropin_paths) { | |
c7080257 LP |
4207 | _cleanup_free_ char *dropin_formatted = NULL; |
4208 | const char *df; | |
4209 | ||
eb2c3192 LP |
4210 | if (!dir || last) { |
4211 | printf(dir ? " " : | |
4212 | " Drop-In: "); | |
76d14b87 | 4213 | |
97b11eed | 4214 | dir = mfree(dir); |
76d14b87 | 4215 | |
5f311f8c LP |
4216 | dir = dirname_malloc(*dropin); |
4217 | if (!dir) { | |
76d14b87 OS |
4218 | log_oom(); |
4219 | return; | |
4220 | } | |
4221 | ||
eb2c3192 LP |
4222 | printf("%s\n" |
4223 | " %s", dir, | |
9a6f746f | 4224 | special_glyph(SPECIAL_GLYPH_TREE_RIGHT)); |
76d14b87 OS |
4225 | } |
4226 | ||
4227 | last = ! (*(dropin + 1) && startswith(*(dropin + 1), dir)); | |
4228 | ||
c7080257 LP |
4229 | if (terminal_urlify_path(*dropin, basename(*dropin), &dropin_formatted) >= 0) |
4230 | df = dropin_formatted; | |
4231 | else | |
4232 | df = *dropin; | |
4233 | ||
4234 | printf("%s%s", df, last ? "\n" : ", "); | |
76d14b87 | 4235 | } |
76d14b87 OS |
4236 | } |
4237 | ||
2ee68f72 | 4238 | ss = streq_ptr(i->active_state, i->sub_state) ? NULL : i->sub_state; |
2ee68f72 | 4239 | if (ss) |
856323c9 | 4240 | printf(" Active: %s%s (%s)%s", |
b0d14c69 | 4241 | active_on, strna(i->active_state), ss, active_off); |
2ee68f72 | 4242 | else |
856323c9 | 4243 | printf(" Active: %s%s%s", |
b0d14c69 | 4244 | active_on, strna(i->active_state), active_off); |
61cbdc4b | 4245 | |
f42806df LP |
4246 | if (!isempty(i->result) && !streq(i->result, "success")) |
4247 | printf(" (Result: %s)", i->result); | |
4248 | ||
1cf03a4f ZJS |
4249 | timestamp = STRPTR_IN_SET(i->active_state, "active", "reloading") ? i->active_enter_timestamp : |
4250 | STRPTR_IN_SET(i->active_state, "inactive", "failed") ? i->inactive_enter_timestamp : | |
4251 | STRPTR_IN_SET(i->active_state, "activating") ? i->inactive_exit_timestamp : | |
4252 | i->active_exit_timestamp; | |
584be568 | 4253 | |
bbb8486e | 4254 | s1 = format_timestamp_relative(since1, sizeof(since1), timestamp); |
584be568 LP |
4255 | s2 = format_timestamp(since2, sizeof(since2), timestamp); |
4256 | ||
4257 | if (s1) | |
538da63d | 4258 | printf(" since %s; %s\n", s2, s1); |
584be568 | 4259 | else if (s2) |
538da63d | 4260 | printf(" since %s\n", s2); |
584be568 LP |
4261 | else |
4262 | printf("\n"); | |
4263 | ||
601b0842 GS |
4264 | if (endswith(i->id, ".timer")) { |
4265 | char tstamp1[FORMAT_TIMESTAMP_RELATIVE_MAX], | |
4266 | tstamp2[FORMAT_TIMESTAMP_MAX]; | |
4d9685be | 4267 | const char *next_rel_time, *next_time; |
601b0842 GS |
4268 | dual_timestamp nw, next = {i->next_elapse_real, |
4269 | i->next_elapse_monotonic}; | |
4270 | usec_t next_elapse; | |
4271 | ||
4272 | printf(" Trigger: "); | |
4273 | ||
4274 | dual_timestamp_get(&nw); | |
4275 | next_elapse = calc_next_elapse(&nw, &next); | |
4d9685be ZJS |
4276 | next_rel_time = format_timestamp_relative(tstamp1, sizeof tstamp1, next_elapse); |
4277 | next_time = format_timestamp(tstamp2, sizeof tstamp2, next_elapse); | |
601b0842 GS |
4278 | |
4279 | if (next_time && next_rel_time) | |
4280 | printf("%s; %s\n", next_time, next_rel_time); | |
4281 | else | |
4282 | printf("n/a\n"); | |
4283 | } | |
4284 | ||
90bbc946 | 4285 | if (!i->condition_result && i->condition_timestamp > 0) { |
d5db7fe6 WC |
4286 | UnitCondition *c; |
4287 | int n = 0; | |
4288 | ||
bbb8486e | 4289 | s1 = format_timestamp_relative(since1, sizeof(since1), i->condition_timestamp); |
90bbc946 LP |
4290 | s2 = format_timestamp(since2, sizeof(since2), i->condition_timestamp); |
4291 | ||
59fccdc5 | 4292 | printf("Condition: start %scondition failed%s at %s%s%s\n", |
1fc464f6 | 4293 | ansi_highlight_yellow(), ansi_normal(), |
5cfee414 | 4294 | s2, s1 ? "; " : "", strempty(s1)); |
d5db7fe6 | 4295 | |
b1ed76ae | 4296 | LIST_FOREACH(conditions, c, i->conditions) |
d5db7fe6 WC |
4297 | if (c->tristate < 0) |
4298 | n++; | |
d5db7fe6 | 4299 | |
b1ed76ae ZJS |
4300 | LIST_FOREACH(conditions, c, i->conditions) |
4301 | if (c->tristate < 0) | |
4302 | printf(" %s %s=%s%s%s was not met\n", | |
9a6f746f | 4303 | --n ? special_glyph(SPECIAL_GLYPH_TREE_BRANCH) : special_glyph(SPECIAL_GLYPH_TREE_RIGHT), |
b1ed76ae ZJS |
4304 | c->name, |
4305 | c->trigger ? "|" : "", | |
4306 | c->negate ? "!" : "", | |
4307 | c->param); | |
59fccdc5 LP |
4308 | } |
4309 | ||
4310 | if (!i->assert_result && i->assert_timestamp > 0) { | |
4311 | s1 = format_timestamp_relative(since1, sizeof(since1), i->assert_timestamp); | |
4312 | s2 = format_timestamp(since2, sizeof(since2), i->assert_timestamp); | |
4313 | ||
4314 | printf(" Assert: start %sassertion failed%s at %s%s%s\n", | |
1fc464f6 | 4315 | ansi_highlight_red(), ansi_normal(), |
5cfee414 | 4316 | s2, s1 ? "; " : "", strempty(s1)); |
59fccdc5 LP |
4317 | if (i->failed_assert_trigger) |
4318 | printf(" none of the trigger assertions were met\n"); | |
4319 | else if (i->failed_assert) | |
4320 | printf(" %s=%s%s was not met\n", | |
4321 | i->failed_assert, | |
4322 | i->failed_assert_negate ? "!" : "", | |
4323 | i->failed_assert_parameter); | |
90bbc946 LP |
4324 | } |
4325 | ||
61cbdc4b | 4326 | if (i->sysfs_path) |
856323c9 | 4327 | printf(" Device: %s\n", i->sysfs_path); |
9feeba4b | 4328 | if (i->where) |
856323c9 | 4329 | printf(" Where: %s\n", i->where); |
9feeba4b | 4330 | if (i->what) |
856323c9 | 4331 | printf(" What: %s\n", i->what); |
49dbfa7b | 4332 | |
78c7d20e LP |
4333 | STRV_FOREACH(t, i->documentation) { |
4334 | _cleanup_free_ char *formatted = NULL; | |
4335 | const char *q; | |
4336 | ||
4337 | if (terminal_urlify(*t, NULL, &formatted) >= 0) | |
4338 | q = formatted; | |
4339 | else | |
4340 | q = *t; | |
4341 | ||
4342 | printf(" %*s %s\n", 9, t == i->documentation ? "Docs:" : "", q); | |
4343 | } | |
49dbfa7b | 4344 | |
13160134 | 4345 | STRV_FOREACH_PAIR(t, t2, i->listen) |
856323c9 | 4346 | printf(" %*s %s (%s)\n", 9, t == i->listen ? "Listen:" : "", *t2, *t); |
67419600 | 4347 | |
a98f7575 MM |
4348 | if (i->accept) { |
4349 | printf(" Accepted: %u; Connected: %u;", i->n_accepted, i->n_connections); | |
4350 | if (i->n_refused) | |
4351 | printf(" Refused: %u", i->n_refused); | |
4352 | printf("\n"); | |
4353 | } | |
4354 | ||
582a507f | 4355 | LIST_FOREACH(exec, p, i->exec) { |
13160134 | 4356 | _cleanup_free_ char *argv = NULL; |
9a57c629 | 4357 | bool good; |
582a507f LP |
4358 | |
4359 | /* Only show exited processes here */ | |
4360 | if (p->code == 0) | |
4361 | continue; | |
4362 | ||
13160134 | 4363 | argv = strv_join(p->argv, " "); |
1fa2f38f | 4364 | printf(" Process: "PID_FMT" %s=%s ", p->pid, p->name, strna(argv)); |
582a507f | 4365 | |
1f0958f6 | 4366 | good = is_clean_exit(p->code, p->status, EXIT_CLEAN_DAEMON, NULL); |
9a57c629 | 4367 | if (!good) { |
0b5a519c | 4368 | on = ansi_highlight_red(); |
1fc464f6 | 4369 | off = ansi_normal(); |
9a57c629 LP |
4370 | } else |
4371 | on = off = ""; | |
4372 | ||
4373 | printf("%s(code=%s, ", on, sigchld_code_to_string(p->code)); | |
4374 | ||
d06dacd0 LP |
4375 | if (p->code == CLD_EXITED) { |
4376 | const char *c; | |
4377 | ||
582a507f | 4378 | printf("status=%i", p->status); |
d06dacd0 | 4379 | |
1b64d026 LP |
4380 | c = exit_status_to_string(p->status, EXIT_STATUS_SYSTEMD); |
4381 | if (c) | |
d06dacd0 LP |
4382 | printf("/%s", c); |
4383 | ||
4384 | } else | |
582a507f | 4385 | printf("signal=%s", signal_to_string(p->status)); |
9a57c629 LP |
4386 | |
4387 | printf(")%s\n", off); | |
4388 | ||
582a507f LP |
4389 | if (i->main_pid == p->pid && |
4390 | i->start_timestamp == p->start_timestamp && | |
4391 | i->exit_timestamp == p->start_timestamp) | |
4392 | /* Let's not show this twice */ | |
4393 | i->main_pid = 0; | |
4394 | ||
4395 | if (p->pid == i->control_pid) | |
4396 | i->control_pid = 0; | |
4397 | } | |
4398 | ||
61cbdc4b | 4399 | if (i->main_pid > 0 || i->control_pid > 0) { |
61cbdc4b | 4400 | if (i->main_pid > 0) { |
8c06592f | 4401 | printf(" Main PID: "PID_FMT, i->main_pid); |
61cbdc4b LP |
4402 | |
4403 | if (i->running) { | |
a081b9ce LP |
4404 | |
4405 | if (arg_transport == BUS_TRANSPORT_LOCAL) { | |
4406 | _cleanup_free_ char *comm = NULL; | |
4407 | ||
4408 | (void) get_process_comm(i->main_pid, &comm); | |
4409 | if (comm) | |
4410 | printf(" (%s)", comm); | |
4411 | } | |
4412 | ||
6d4fc029 | 4413 | } else if (i->exit_code > 0) { |
61cbdc4b LP |
4414 | printf(" (code=%s, ", sigchld_code_to_string(i->exit_code)); |
4415 | ||
d06dacd0 LP |
4416 | if (i->exit_code == CLD_EXITED) { |
4417 | const char *c; | |
4418 | ||
61cbdc4b | 4419 | printf("status=%i", i->exit_status); |
d06dacd0 | 4420 | |
1b64d026 LP |
4421 | c = exit_status_to_string(i->exit_status, EXIT_STATUS_SYSTEMD); |
4422 | if (c) | |
d06dacd0 LP |
4423 | printf("/%s", c); |
4424 | ||
4425 | } else | |
582a507f | 4426 | printf("signal=%s", signal_to_string(i->exit_status)); |
6d4fc029 LP |
4427 | printf(")"); |
4428 | } | |
13160134 | 4429 | } |
61cbdc4b LP |
4430 | |
4431 | if (i->control_pid > 0) { | |
13160134 | 4432 | _cleanup_free_ char *c = NULL; |
61cbdc4b | 4433 | |
1089dcd4 LP |
4434 | if (i->main_pid > 0) |
4435 | fputs("; Control PID: ", stdout); | |
4436 | else | |
4437 | fputs("Cntrl PID: ", stdout); /* if first in column, abbreviated so it fits alignment */ | |
4438 | ||
4439 | printf(PID_FMT, i->control_pid); | |
61cbdc4b | 4440 | |
a081b9ce LP |
4441 | if (arg_transport == BUS_TRANSPORT_LOCAL) { |
4442 | (void) get_process_comm(i->control_pid, &c); | |
4443 | if (c) | |
4444 | printf(" (%s)", c); | |
4445 | } | |
61cbdc4b LP |
4446 | } |
4447 | ||
4448 | printf("\n"); | |
4449 | } | |
4450 | ||
17bb7382 | 4451 | if (i->status_text) |
856323c9 | 4452 | printf(" Status: \"%s\"\n", i->status_text); |
b4af5a80 LP |
4453 | if (i->status_errno > 0) |
4454 | printf(" Error: %i (%s)\n", i->status_errno, strerror(i->status_errno)); | |
17bb7382 | 4455 | |
0e97c93f DM |
4456 | if (i->ip_ingress_bytes != (uint64_t) -1 && i->ip_egress_bytes != (uint64_t) -1) { |
4457 | char buf_in[FORMAT_BYTES_MAX], buf_out[FORMAT_BYTES_MAX]; | |
4458 | ||
4459 | printf(" IP: %s in, %s out\n", | |
4460 | format_bytes(buf_in, sizeof(buf_in), i->ip_ingress_bytes), | |
4461 | format_bytes(buf_out, sizeof(buf_out), i->ip_egress_bytes)); | |
4462 | } | |
4463 | ||
03a7b521 LP |
4464 | if (i->tasks_current != (uint64_t) -1) { |
4465 | printf(" Tasks: %" PRIu64, i->tasks_current); | |
4466 | ||
4467 | if (i->tasks_max != (uint64_t) -1) | |
2a6736dd | 4468 | printf(" (limit: %" PRIu64 ")\n", i->tasks_max); |
03a7b521 LP |
4469 | else |
4470 | printf("\n"); | |
4471 | } | |
4472 | ||
934277fe LP |
4473 | if (i->memory_current != (uint64_t) -1) { |
4474 | char buf[FORMAT_BYTES_MAX]; | |
4475 | ||
4476 | printf(" Memory: %s", format_bytes(buf, sizeof(buf), i->memory_current)); | |
4477 | ||
48422635 TH |
4478 | if (i->memory_min > 0 || i->memory_low > 0 || |
4479 | i->memory_high != CGROUP_LIMIT_MAX || i->memory_max != CGROUP_LIMIT_MAX || | |
4480 | i->memory_swap_max != CGROUP_LIMIT_MAX || | |
da4d897e TH |
4481 | i->memory_limit != CGROUP_LIMIT_MAX) { |
4482 | const char *prefix = ""; | |
4483 | ||
4484 | printf(" ("); | |
48422635 TH |
4485 | if (i->memory_min > 0) { |
4486 | printf("%smin: %s", prefix, format_bytes(buf, sizeof(buf), i->memory_min)); | |
4487 | prefix = " "; | |
4488 | } | |
da4d897e TH |
4489 | if (i->memory_low > 0) { |
4490 | printf("%slow: %s", prefix, format_bytes(buf, sizeof(buf), i->memory_low)); | |
4491 | prefix = " "; | |
4492 | } | |
4493 | if (i->memory_high != CGROUP_LIMIT_MAX) { | |
4494 | printf("%shigh: %s", prefix, format_bytes(buf, sizeof(buf), i->memory_high)); | |
4495 | prefix = " "; | |
4496 | } | |
4497 | if (i->memory_max != CGROUP_LIMIT_MAX) { | |
4498 | printf("%smax: %s", prefix, format_bytes(buf, sizeof(buf), i->memory_max)); | |
4499 | prefix = " "; | |
4500 | } | |
96e131ea WC |
4501 | if (i->memory_swap_max != CGROUP_LIMIT_MAX) { |
4502 | printf("%sswap max: %s", prefix, format_bytes(buf, sizeof(buf), i->memory_swap_max)); | |
4503 | prefix = " "; | |
4504 | } | |
da4d897e TH |
4505 | if (i->memory_limit != CGROUP_LIMIT_MAX) { |
4506 | printf("%slimit: %s", prefix, format_bytes(buf, sizeof(buf), i->memory_limit)); | |
4507 | prefix = " "; | |
4508 | } | |
4509 | printf(")"); | |
4510 | } | |
4511 | printf("\n"); | |
934277fe LP |
4512 | } |
4513 | ||
5ad096b3 LP |
4514 | if (i->cpu_usage_nsec != (uint64_t) -1) { |
4515 | char buf[FORMAT_TIMESPAN_MAX]; | |
4516 | printf(" CPU: %s\n", format_timespan(buf, sizeof(buf), i->cpu_usage_nsec / NSEC_PER_USEC, USEC_PER_MSEC)); | |
4517 | } | |
4518 | ||
51e22f88 | 4519 | if (i->control_group) { |
291d565a LP |
4520 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
4521 | static const char prefix[] = " "; | |
ab35fb1b LP |
4522 | unsigned c; |
4523 | ||
51e22f88 LP |
4524 | printf(" CGroup: %s\n", i->control_group); |
4525 | ||
291d565a LP |
4526 | c = columns(); |
4527 | if (c > sizeof(prefix) - 1) | |
4528 | c -= sizeof(prefix) - 1; | |
4529 | else | |
4530 | c = 0; | |
ab35fb1b | 4531 | |
291d565a LP |
4532 | r = unit_show_processes(bus, i->id, i->control_group, prefix, c, get_output_flags(), &error); |
4533 | if (r == -EBADR) { | |
b69d29ce LP |
4534 | unsigned k = 0; |
4535 | pid_t extra[2]; | |
4536 | ||
291d565a | 4537 | /* Fallback for older systemd versions where the GetUnitProcesses() call is not yet available */ |
ab35fb1b | 4538 | |
b69d29ce LP |
4539 | if (i->main_pid > 0) |
4540 | extra[k++] = i->main_pid; | |
4541 | ||
4542 | if (i->control_pid > 0) | |
4543 | extra[k++] = i->control_pid; | |
4544 | ||
0ff308c8 | 4545 | show_cgroup_and_extra(SYSTEMD_CGROUP_CONTROLLER, i->control_group, prefix, c, extra, k, get_output_flags()); |
291d565a | 4546 | } else if (r < 0) |
950d6889 | 4547 | log_warning_errno(r, "Failed to dump process list for '%s', ignoring: %s", i->id, bus_error_message(&error, r)); |
c59760ee | 4548 | } |
45fb0699 | 4549 | |
ece174c5 | 4550 | if (i->id && arg_transport == BUS_TRANSPORT_LOCAL) |
3c756001 LP |
4551 | show_journal_by_unit( |
4552 | stdout, | |
4553 | i->id, | |
4554 | arg_output, | |
4555 | 0, | |
4556 | i->inactive_exit_timestamp_monotonic, | |
4557 | arg_lines, | |
4558 | getuid(), | |
4559 | get_output_flags() | OUTPUT_BEGIN_NEWLINE, | |
4560 | SD_JOURNAL_LOCAL_ONLY, | |
4561 | arg_scope == UNIT_FILE_SYSTEM, | |
4562 | ellipsized); | |
86aa7ba4 | 4563 | |
45fb0699 | 4564 | if (i->need_daemon_reload) |
3f36991e | 4565 | warn_unit_file_changed(i->id); |
61cbdc4b LP |
4566 | } |
4567 | ||
b43f208f | 4568 | static void show_unit_help(UnitStatusInfo *i) { |
256425cc LP |
4569 | char **p; |
4570 | ||
4571 | assert(i); | |
4572 | ||
4573 | if (!i->documentation) { | |
4574 | log_info("Documentation for %s not known.", i->id); | |
4575 | return; | |
4576 | } | |
4577 | ||
78002a67 ZJS |
4578 | STRV_FOREACH(p, i->documentation) |
4579 | if (startswith(*p, "man:")) | |
4580 | show_man_page(*p + 4, false); | |
4581 | else | |
0315fe37 | 4582 | log_info("Can't show: %s", *p); |
256425cc LP |
4583 | } |
4584 | ||
4679a8c3 YW |
4585 | static int map_main_pid(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { |
4586 | UnitStatusInfo *i = userdata; | |
4587 | uint32_t u; | |
f459b602 | 4588 | int r; |
61cbdc4b | 4589 | |
4679a8c3 YW |
4590 | r = sd_bus_message_read(m, "u", &u); |
4591 | if (r < 0) | |
4592 | return r; | |
61cbdc4b | 4593 | |
4679a8c3 YW |
4594 | i->main_pid = (pid_t) u; |
4595 | i->running = u > 0; | |
b8131a87 | 4596 | |
4679a8c3 YW |
4597 | return 0; |
4598 | } | |
b8131a87 | 4599 | |
4679a8c3 | 4600 | static int map_load_error(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { |
31d99bd1 | 4601 | const char *message, **p = userdata; |
4679a8c3 | 4602 | int r; |
b8131a87 | 4603 | |
31d99bd1 | 4604 | r = sd_bus_message_read(m, "(ss)", NULL, &message); |
4679a8c3 YW |
4605 | if (r < 0) |
4606 | return r; | |
b8131a87 | 4607 | |
4679a8c3 YW |
4608 | if (!isempty(message)) |
4609 | *p = message; | |
61cbdc4b | 4610 | |
4679a8c3 YW |
4611 | return 0; |
4612 | } | |
61cbdc4b | 4613 | |
4679a8c3 YW |
4614 | static int map_listen(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { |
4615 | const char *type, *path; | |
4616 | char ***p = userdata; | |
4617 | int r; | |
61cbdc4b | 4618 | |
4679a8c3 YW |
4619 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(ss)"); |
4620 | if (r < 0) | |
4621 | return r; | |
61cbdc4b | 4622 | |
4679a8c3 | 4623 | while ((r = sd_bus_message_read(m, "(ss)", &type, &path)) > 0) { |
61cbdc4b | 4624 | |
4679a8c3 | 4625 | r = strv_extend(p, type); |
f459b602 | 4626 | if (r < 0) |
4679a8c3 | 4627 | return r; |
61cbdc4b | 4628 | |
4679a8c3 | 4629 | r = strv_extend(p, path); |
f459b602 | 4630 | if (r < 0) |
4679a8c3 | 4631 | return r; |
61cbdc4b | 4632 | } |
4679a8c3 YW |
4633 | if (r < 0) |
4634 | return r; | |
582a507f | 4635 | |
4679a8c3 YW |
4636 | r = sd_bus_message_exit_container(m); |
4637 | if (r < 0) | |
4638 | return r; | |
49dbfa7b | 4639 | |
4679a8c3 YW |
4640 | return 0; |
4641 | } | |
52990c2e | 4642 | |
4679a8c3 YW |
4643 | static int map_conditions(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { |
4644 | UnitStatusInfo *i = userdata; | |
4645 | const char *cond, *param; | |
4646 | int trigger, negate; | |
4647 | int32_t state; | |
4648 | int r; | |
52990c2e | 4649 | |
4679a8c3 YW |
4650 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(sbbsi)"); |
4651 | if (r < 0) | |
4652 | return r; | |
f459b602 | 4653 | |
4679a8c3 YW |
4654 | while ((r = sd_bus_message_read(m, "(sbbsi)", &cond, &trigger, &negate, ¶m, &state)) > 0) { |
4655 | _cleanup_(unit_condition_freep) UnitCondition *c = NULL; | |
d5db7fe6 | 4656 | |
4679a8c3 YW |
4657 | c = new0(UnitCondition, 1); |
4658 | if (!c) | |
4659 | return -ENOMEM; | |
d5db7fe6 | 4660 | |
4679a8c3 YW |
4661 | c->name = strdup(cond); |
4662 | c->param = strdup(param); | |
4663 | if (!c->name || !c->param) | |
4664 | return -ENOMEM; | |
d5db7fe6 | 4665 | |
4679a8c3 YW |
4666 | c->trigger = trigger; |
4667 | c->negate = negate; | |
4668 | c->tristate = state; | |
d5db7fe6 | 4669 | |
4679a8c3 YW |
4670 | LIST_PREPEND(conditions, i->conditions, c); |
4671 | c = NULL; | |
4672 | } | |
4673 | if (r < 0) | |
4674 | return r; | |
d5db7fe6 | 4675 | |
4679a8c3 YW |
4676 | r = sd_bus_message_exit_container(m); |
4677 | if (r < 0) | |
4678 | return r; | |
59fccdc5 | 4679 | |
4679a8c3 YW |
4680 | return 0; |
4681 | } | |
59fccdc5 | 4682 | |
4679a8c3 YW |
4683 | static int map_asserts(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { |
4684 | UnitStatusInfo *i = userdata; | |
4685 | const char *cond, *param; | |
4686 | int trigger, negate; | |
4687 | int32_t state; | |
4688 | int r; | |
59fccdc5 | 4689 | |
4679a8c3 YW |
4690 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(sbbsi)"); |
4691 | if (r < 0) | |
4692 | return r; | |
59fccdc5 | 4693 | |
4679a8c3 YW |
4694 | while ((r = sd_bus_message_read(m, "(sbbsi)", &cond, &trigger, &negate, ¶m, &state)) > 0) { |
4695 | if (state < 0 && (!trigger || !i->failed_assert)) { | |
4696 | i->failed_assert = cond; | |
4697 | i->failed_assert_trigger = trigger; | |
4698 | i->failed_assert_negate = negate; | |
4699 | i->failed_assert_parameter = param; | |
4700 | } | |
4701 | } | |
4702 | if (r < 0) | |
4703 | return r; | |
f459b602 | 4704 | |
4679a8c3 YW |
4705 | r = sd_bus_message_exit_container(m); |
4706 | if (r < 0) | |
4707 | return r; | |
f459b602 | 4708 | |
4679a8c3 YW |
4709 | return 0; |
4710 | } | |
582a507f | 4711 | |
4679a8c3 YW |
4712 | static int map_exec(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { |
4713 | _cleanup_free_ ExecStatusInfo *info = NULL; | |
3e14d36a | 4714 | ExecStatusInfo *last; |
4679a8c3 YW |
4715 | UnitStatusInfo *i = userdata; |
4716 | int r; | |
9f39404c | 4717 | |
4679a8c3 YW |
4718 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(sasbttttuii)"); |
4719 | if (r < 0) | |
4720 | return r; | |
9f39404c | 4721 | |
4679a8c3 YW |
4722 | info = new0(ExecStatusInfo, 1); |
4723 | if (!info) | |
4724 | return -ENOMEM; | |
9f39404c | 4725 | |
3e14d36a LP |
4726 | LIST_FIND_TAIL(exec, i->exec, last); |
4727 | ||
4679a8c3 | 4728 | while ((r = exec_status_info_deserialize(m, info)) > 0) { |
9f39404c | 4729 | |
4679a8c3 YW |
4730 | info->name = strdup(member); |
4731 | if (!info->name) | |
4732 | return -ENOMEM; | |
9f39404c | 4733 | |
3e14d36a LP |
4734 | LIST_INSERT_AFTER(exec, i->exec, last, info); |
4735 | last = info; | |
f459b602 | 4736 | |
4679a8c3 YW |
4737 | info = new0(ExecStatusInfo, 1); |
4738 | if (!info) | |
4739 | return -ENOMEM; | |
9f39404c | 4740 | } |
4679a8c3 YW |
4741 | if (r < 0) |
4742 | return r; | |
f459b602 | 4743 | |
4679a8c3 | 4744 | r = sd_bus_message_exit_container(m); |
f459b602 | 4745 | if (r < 0) |
4679a8c3 | 4746 | return r; |
61cbdc4b LP |
4747 | |
4748 | return 0; | |
4749 | } | |
4750 | ||
eda19357 | 4751 | static int print_property(const char *name, const char *expected_value, sd_bus_message *m, bool value, bool all) { |
07636114 YW |
4752 | char bus_type; |
4753 | const char *contents; | |
f459b602 MAP |
4754 | int r; |
4755 | ||
48220598 | 4756 | assert(name); |
f459b602 | 4757 | assert(m); |
48220598 | 4758 | |
61cbdc4b LP |
4759 | /* This is a low-level property printer, see |
4760 | * print_status_info() for the nicer output */ | |
4761 | ||
07636114 YW |
4762 | r = sd_bus_message_peek_type(m, &bus_type, &contents); |
4763 | if (r < 0) | |
852c1b4d | 4764 | return r; |
48220598 | 4765 | |
07636114 | 4766 | switch (bus_type) { |
48220598 | 4767 | |
a3ddd457 YW |
4768 | case SD_BUS_TYPE_INT32: |
4769 | if (endswith(name, "ActionExitStatus")) { | |
4770 | int32_t i; | |
4771 | ||
4772 | r = sd_bus_message_read_basic(m, bus_type, &i); | |
4773 | if (r < 0) | |
4774 | return r; | |
4775 | ||
4776 | if (i >= 0 && i <= 255) | |
4777 | bus_print_property_valuef(name, expected_value, value, "%"PRIi32, i); | |
4778 | else if (all) | |
4779 | bus_print_property_value(name, expected_value, value, "[not set]"); | |
4780 | ||
4781 | return 1; | |
4782 | } | |
4783 | break; | |
4784 | ||
07636114 | 4785 | case SD_BUS_TYPE_STRUCT: |
48220598 | 4786 | |
07636114 | 4787 | if (contents[0] == SD_BUS_TYPE_UINT32 && streq(name, "Job")) { |
48220598 LP |
4788 | uint32_t u; |
4789 | ||
f459b602 MAP |
4790 | r = sd_bus_message_read(m, "(uo)", &u, NULL); |
4791 | if (r < 0) | |
4792 | return bus_log_parse_error(r); | |
48220598 | 4793 | |
f459b602 | 4794 | if (u > 0) |
102b0214 | 4795 | bus_print_property_valuef(name, expected_value, value, "%"PRIu32, u); |
07636114 | 4796 | else if (all) |
102b0214 | 4797 | bus_print_property_value(name, expected_value, value, ""); |
48220598 | 4798 | |
07636114 | 4799 | return 1; |
f459b602 | 4800 | |
07636114 | 4801 | } else if (contents[0] == SD_BUS_TYPE_STRING && streq(name, "Unit")) { |
48220598 LP |
4802 | const char *s; |
4803 | ||
f459b602 MAP |
4804 | r = sd_bus_message_read(m, "(so)", &s, NULL); |
4805 | if (r < 0) | |
4806 | return bus_log_parse_error(r); | |
48220598 | 4807 | |
07636114 | 4808 | if (all || !isempty(s)) |
102b0214 | 4809 | bus_print_property_value(name, expected_value, value, s); |
48220598 | 4810 | |
07636114 | 4811 | return 1; |
f459b602 | 4812 | |
07636114 | 4813 | } else if (contents[0] == SD_BUS_TYPE_STRING && streq(name, "LoadError")) { |
9f39404c LP |
4814 | const char *a = NULL, *b = NULL; |
4815 | ||
f459b602 MAP |
4816 | r = sd_bus_message_read(m, "(ss)", &a, &b); |
4817 | if (r < 0) | |
4818 | return bus_log_parse_error(r); | |
9f39404c | 4819 | |
48d3358d | 4820 | if (!isempty(a) || !isempty(b)) |
102b0214 | 4821 | bus_print_property_valuef(name, expected_value, value, "%s \"%s\"", strempty(a), strempty(b)); |
48d3358d YW |
4822 | else if (all) |
4823 | bus_print_property_value(name, expected_value, value, ""); | |
f786e80d | 4824 | |
07636114 | 4825 | return 1; |
04749e40 YW |
4826 | |
4827 | } else if (STR_IN_SET(name, "SystemCallFilter", "RestrictAddressFamilies")) { | |
57183d11 LP |
4828 | _cleanup_strv_free_ char **l = NULL; |
4829 | int whitelist; | |
4830 | ||
4831 | r = sd_bus_message_enter_container(m, 'r', "bas"); | |
4832 | if (r < 0) | |
4833 | return bus_log_parse_error(r); | |
4834 | ||
4835 | r = sd_bus_message_read(m, "b", &whitelist); | |
4836 | if (r < 0) | |
4837 | return bus_log_parse_error(r); | |
4838 | ||
4839 | r = sd_bus_message_read_strv(m, &l); | |
4840 | if (r < 0) | |
4841 | return bus_log_parse_error(r); | |
4842 | ||
4843 | r = sd_bus_message_exit_container(m); | |
4844 | if (r < 0) | |
4845 | return bus_log_parse_error(r); | |
4846 | ||
07636114 | 4847 | if (all || whitelist || !strv_isempty(l)) { |
57183d11 LP |
4848 | bool first = true; |
4849 | char **i; | |
4850 | ||
07636114 | 4851 | if (!value) { |
4f9a9105 ZJS |
4852 | fputs(name, stdout); |
4853 | fputc('=', stdout); | |
4854 | } | |
57183d11 LP |
4855 | |
4856 | if (!whitelist) | |
4857 | fputc('~', stdout); | |
4858 | ||
4859 | STRV_FOREACH(i, l) { | |
4860 | if (first) | |
4861 | first = false; | |
4862 | else | |
4863 | fputc(' ', stdout); | |
4864 | ||
4865 | fputs(*i, stdout); | |
4866 | } | |
4867 | fputc('\n', stdout); | |
4868 | } | |
4869 | ||
c7bb2fec | 4870 | return 1; |
102b0214 | 4871 | |
c7bb2fec IP |
4872 | } else if (STR_IN_SET(name, "SELinuxContext", "AppArmorProfile", "SmackProcessLabel")) { |
4873 | int ignore; | |
4874 | const char *s; | |
4875 | ||
4876 | r = sd_bus_message_read(m, "(bs)", &ignore, &s); | |
4877 | if (r < 0) | |
4878 | return bus_log_parse_error(r); | |
4879 | ||
4880 | if (!isempty(s)) | |
102b0214 | 4881 | bus_print_property_valuef(name, expected_value, value, "%s%s", ignore ? "-" : "", s); |
c7bb2fec | 4882 | else if (all) |
102b0214 | 4883 | bus_print_property_value(name, expected_value, value, ""); |
c7bb2fec | 4884 | |
07636114 | 4885 | return 1; |
4a220712 YW |
4886 | |
4887 | } else if (endswith(name, "ExitStatus") && streq(contents, "aiai")) { | |
4888 | const int32_t *status, *signal; | |
4889 | size_t sz_status, sz_signal, i; | |
4890 | ||
4891 | r = sd_bus_message_enter_container(m, 'r', "aiai"); | |
4892 | if (r < 0) | |
4893 | return bus_log_parse_error(r); | |
4894 | ||
4895 | r = sd_bus_message_read_array(m, 'i', (const void **) &status, &sz_status); | |
4896 | if (r < 0) | |
4897 | return bus_log_parse_error(r); | |
4898 | ||
4899 | r = sd_bus_message_read_array(m, 'i', (const void **) &signal, &sz_signal); | |
4900 | if (r < 0) | |
4901 | return bus_log_parse_error(r); | |
4902 | ||
4903 | r = sd_bus_message_exit_container(m); | |
4904 | if (r < 0) | |
4905 | return bus_log_parse_error(r); | |
4906 | ||
4907 | sz_status /= sizeof(int32_t); | |
4908 | sz_signal /= sizeof(int32_t); | |
4909 | ||
4910 | if (all || sz_status > 0 || sz_signal > 0) { | |
4911 | bool first = true; | |
4912 | ||
4913 | if (!value) { | |
4914 | fputs(name, stdout); | |
4915 | fputc('=', stdout); | |
4916 | } | |
4917 | ||
4918 | for (i = 0; i < sz_status; i++) { | |
4919 | if (status[i] < 0 || status[i] > 255) | |
4920 | continue; | |
4921 | ||
4922 | if (first) | |
4923 | first = false; | |
4924 | else | |
4925 | fputc(' ', stdout); | |
4926 | ||
4927 | printf("%"PRIi32, status[i]); | |
4928 | } | |
4929 | ||
4930 | for (i = 0; i < sz_signal; i++) { | |
4931 | const char *str; | |
4932 | ||
4933 | str = signal_to_string((int) signal[i]); | |
4934 | if (!str) | |
4935 | continue; | |
4936 | ||
4937 | if (first) | |
4938 | first = false; | |
4939 | else | |
4940 | fputc(' ', stdout); | |
4941 | ||
4942 | fputs(str, stdout); | |
4943 | } | |
4944 | ||
4945 | fputc('\n', stdout); | |
4946 | } | |
4947 | return 1; | |
48220598 LP |
4948 | } |
4949 | ||
4950 | break; | |
48220598 | 4951 | |
f459b602 | 4952 | case SD_BUS_TYPE_ARRAY: |
48220598 | 4953 | |
07636114 | 4954 | if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "EnvironmentFiles")) { |
f459b602 MAP |
4955 | const char *path; |
4956 | int ignore; | |
8c7be95e | 4957 | |
f459b602 MAP |
4958 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(sb)"); |
4959 | if (r < 0) | |
4960 | return bus_log_parse_error(r); | |
8c7be95e | 4961 | |
f459b602 | 4962 | while ((r = sd_bus_message_read(m, "(sb)", &path, &ignore)) > 0) |
102b0214 | 4963 | bus_print_property_valuef(name, expected_value, value, "%s (ignore_errors=%s)", path, yes_no(ignore)); |
8c7be95e | 4964 | |
f459b602 MAP |
4965 | if (r < 0) |
4966 | return bus_log_parse_error(r); | |
8c7be95e | 4967 | |
f459b602 MAP |
4968 | r = sd_bus_message_exit_container(m); |
4969 | if (r < 0) | |
4970 | return bus_log_parse_error(r); | |
8c7be95e | 4971 | |
07636114 | 4972 | return 1; |
8c7be95e | 4973 | |
07636114 | 4974 | } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "Paths")) { |
f459b602 | 4975 | const char *type, *path; |
67419600 | 4976 | |
f459b602 MAP |
4977 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(ss)"); |
4978 | if (r < 0) | |
4979 | return bus_log_parse_error(r); | |
ebf57b80 | 4980 | |
f459b602 | 4981 | while ((r = sd_bus_message_read(m, "(ss)", &type, &path)) > 0) |
102b0214 | 4982 | bus_print_property_valuef(name, expected_value, value, "%s (%s)", path, type); |
f459b602 MAP |
4983 | if (r < 0) |
4984 | return bus_log_parse_error(r); | |
ebf57b80 | 4985 | |
f459b602 MAP |
4986 | r = sd_bus_message_exit_container(m); |
4987 | if (r < 0) | |
4988 | return bus_log_parse_error(r); | |
ebf57b80 | 4989 | |
07636114 | 4990 | return 1; |
582a507f | 4991 | |
07636114 | 4992 | } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "Listen")) { |
f459b602 | 4993 | const char *type, *path; |
67419600 | 4994 | |
f459b602 MAP |
4995 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(ss)"); |
4996 | if (r < 0) | |
4997 | return bus_log_parse_error(r); | |
67419600 | 4998 | |
f459b602 | 4999 | while ((r = sd_bus_message_read(m, "(ss)", &type, &path)) > 0) |
102b0214 | 5000 | bus_print_property_valuef(name, expected_value, value, "%s (%s)", path, type); |
f459b602 MAP |
5001 | if (r < 0) |
5002 | return bus_log_parse_error(r); | |
67419600 | 5003 | |
f459b602 MAP |
5004 | r = sd_bus_message_exit_container(m); |
5005 | if (r < 0) | |
5006 | return bus_log_parse_error(r); | |
67419600 | 5007 | |
07636114 | 5008 | return 1; |
67419600 | 5009 | |
07636114 | 5010 | } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "TimersMonotonic")) { |
f459b602 | 5011 | const char *base; |
07636114 | 5012 | uint64_t v, next_elapse; |
707e5e52 | 5013 | |
f459b602 MAP |
5014 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(stt)"); |
5015 | if (r < 0) | |
5016 | return bus_log_parse_error(r); | |
552e4331 | 5017 | |
07636114 | 5018 | while ((r = sd_bus_message_read(m, "(stt)", &base, &v, &next_elapse)) > 0) { |
f459b602 | 5019 | char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX]; |
fe68089d | 5020 | |
102b0214 YW |
5021 | bus_print_property_valuef(name, expected_value, value, "{ %s=%s ; next_elapse=%s }", base, |
5022 | format_timespan(timespan1, sizeof(timespan1), v, 0), | |
5023 | format_timespan(timespan2, sizeof(timespan2), next_elapse, 0)); | |
fe68089d | 5024 | } |
f459b602 MAP |
5025 | if (r < 0) |
5026 | return bus_log_parse_error(r); | |
5027 | ||
5028 | r = sd_bus_message_exit_container(m); | |
5029 | if (r < 0) | |
5030 | return bus_log_parse_error(r); | |
fe68089d | 5031 | |
07636114 | 5032 | return 1; |
fe68089d | 5033 | |
07636114 | 5034 | } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "TimersCalendar")) { |
fa6dee52 YW |
5035 | const char *base, *spec; |
5036 | uint64_t next_elapse; | |
5037 | ||
5038 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(sst)"); | |
5039 | if (r < 0) | |
5040 | return bus_log_parse_error(r); | |
5041 | ||
5042 | while ((r = sd_bus_message_read(m, "(sst)", &base, &spec, &next_elapse)) > 0) { | |
5043 | char timestamp[FORMAT_TIMESTAMP_MAX]; | |
5044 | ||
102b0214 YW |
5045 | bus_print_property_valuef(name, expected_value, value, "{ %s=%s ; next_elapse=%s }", base, spec, |
5046 | format_timestamp(timestamp, sizeof(timestamp), next_elapse)); | |
fa6dee52 YW |
5047 | } |
5048 | if (r < 0) | |
5049 | return bus_log_parse_error(r); | |
5050 | ||
5051 | r = sd_bus_message_exit_container(m); | |
5052 | if (r < 0) | |
5053 | return bus_log_parse_error(r); | |
5054 | ||
07636114 | 5055 | return 1; |
fa6dee52 | 5056 | |
07636114 | 5057 | } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && startswith(name, "Exec")) { |
f459b602 MAP |
5058 | ExecStatusInfo info = {}; |
5059 | ||
5060 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(sasbttttuii)"); | |
5061 | if (r < 0) | |
5062 | return bus_log_parse_error(r); | |
5063 | ||
5064 | while ((r = exec_status_info_deserialize(m, &info)) > 0) { | |
5065 | char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX]; | |
5066 | _cleanup_free_ char *tt; | |
5067 | ||
5068 | tt = strv_join(info.argv, " "); | |
5069 | ||
102b0214 YW |
5070 | bus_print_property_valuef(name, expected_value, value, |
5071 | "{ path=%s ; argv[]=%s ; ignore_errors=%s ; start_time=[%s] ; stop_time=[%s] ; pid="PID_FMT" ; code=%s ; status=%i%s%s }", | |
5072 | strna(info.path), | |
5073 | strna(tt), | |
5074 | yes_no(info.ignore), | |
5075 | strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)), | |
5076 | strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)), | |
5077 | info.pid, | |
5078 | sigchld_code_to_string(info.code), | |
5079 | info.status, | |
5080 | info.code == CLD_EXITED ? "" : "/", | |
5081 | strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status))); | |
fe68089d | 5082 | |
582a507f LP |
5083 | free(info.path); |
5084 | strv_free(info.argv); | |
f459b602 | 5085 | zero(info); |
707e5e52 LP |
5086 | } |
5087 | ||
f459b602 MAP |
5088 | r = sd_bus_message_exit_container(m); |
5089 | if (r < 0) | |
5090 | return bus_log_parse_error(r); | |
5091 | ||
07636114 | 5092 | return 1; |
4ad49000 | 5093 | |
07636114 | 5094 | } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "DeviceAllow")) { |
f459b602 | 5095 | const char *path, *rwm; |
4ad49000 | 5096 | |
f459b602 MAP |
5097 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(ss)"); |
5098 | if (r < 0) | |
5099 | return bus_log_parse_error(r); | |
4ad49000 | 5100 | |
f459b602 | 5101 | while ((r = sd_bus_message_read(m, "(ss)", &path, &rwm)) > 0) |
102b0214 | 5102 | bus_print_property_valuef(name, expected_value, value, "%s %s", strna(path), strna(rwm)); |
f459b602 MAP |
5103 | if (r < 0) |
5104 | return bus_log_parse_error(r); | |
4ad49000 | 5105 | |
f459b602 MAP |
5106 | r = sd_bus_message_exit_container(m); |
5107 | if (r < 0) | |
5108 | return bus_log_parse_error(r); | |
4ad49000 | 5109 | |
07636114 | 5110 | return 1; |
4ad49000 | 5111 | |
07636114 | 5112 | } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && |
72240b52 | 5113 | STR_IN_SET(name, "IODeviceWeight", "BlockIODeviceWeight")) { |
f459b602 MAP |
5114 | const char *path; |
5115 | uint64_t weight; | |
b8ab2dc6 | 5116 | |
f459b602 MAP |
5117 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(st)"); |
5118 | if (r < 0) | |
5119 | return bus_log_parse_error(r); | |
b8ab2dc6 | 5120 | |
f459b602 | 5121 | while ((r = sd_bus_message_read(m, "(st)", &path, &weight)) > 0) |
102b0214 | 5122 | bus_print_property_valuef(name, expected_value, value, "%s %"PRIu64, strna(path), weight); |
f459b602 MAP |
5123 | if (r < 0) |
5124 | return bus_log_parse_error(r); | |
b8ab2dc6 | 5125 | |
f459b602 MAP |
5126 | r = sd_bus_message_exit_container(m); |
5127 | if (r < 0) | |
5128 | return bus_log_parse_error(r); | |
b8ab2dc6 | 5129 | |
07636114 | 5130 | return 1; |
b8ab2dc6 | 5131 | |
07636114 | 5132 | } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && |
72240b52 ZJS |
5133 | (cgroup_io_limit_type_from_string(name) >= 0 || |
5134 | STR_IN_SET(name, "BlockIOReadBandwidth", "BlockIOWriteBandwidth"))) { | |
f459b602 MAP |
5135 | const char *path; |
5136 | uint64_t bandwidth; | |
4ad49000 | 5137 | |
f459b602 MAP |
5138 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(st)"); |
5139 | if (r < 0) | |
5140 | return bus_log_parse_error(r); | |
4ad49000 | 5141 | |
f459b602 | 5142 | while ((r = sd_bus_message_read(m, "(st)", &path, &bandwidth)) > 0) |
102b0214 | 5143 | bus_print_property_valuef(name, expected_value, value, "%s %"PRIu64, strna(path), bandwidth); |
f459b602 MAP |
5144 | if (r < 0) |
5145 | return bus_log_parse_error(r); | |
4ad49000 | 5146 | |
f459b602 MAP |
5147 | r = sd_bus_message_exit_container(m); |
5148 | if (r < 0) | |
5149 | return bus_log_parse_error(r); | |
4ad49000 | 5150 | |
07636114 | 5151 | return 1; |
6ae4283c TH |
5152 | |
5153 | } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && | |
5154 | streq(name, "IODeviceLatencyTargetUSec")) { | |
5155 | char ts[FORMAT_TIMESPAN_MAX]; | |
5156 | const char *path; | |
5157 | uint64_t target; | |
5158 | ||
5159 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(st)"); | |
5160 | if (r < 0) | |
5161 | return bus_log_parse_error(r); | |
5162 | ||
5163 | while ((r = sd_bus_message_read(m, "(st)", &path, &target)) > 0) | |
102b0214 YW |
5164 | bus_print_property_valuef(name, expected_value, value, "%s %s", strna(path), |
5165 | format_timespan(ts, sizeof(ts), target, 1)); | |
6ae4283c TH |
5166 | if (r < 0) |
5167 | return bus_log_parse_error(r); | |
5168 | ||
5169 | r = sd_bus_message_exit_container(m); | |
5170 | if (r < 0) | |
5171 | return bus_log_parse_error(r); | |
5172 | ||
5173 | return 1; | |
08f3be7a | 5174 | |
07636114 | 5175 | } else if (contents[0] == SD_BUS_TYPE_BYTE && streq(name, "StandardInputData")) { |
08f3be7a LP |
5176 | _cleanup_free_ char *h = NULL; |
5177 | const void *p; | |
5178 | size_t sz; | |
5179 | ssize_t n; | |
5180 | ||
5181 | r = sd_bus_message_read_array(m, 'y', &p, &sz); | |
5182 | if (r < 0) | |
5183 | return bus_log_parse_error(r); | |
5184 | ||
5185 | n = base64mem(p, sz, &h); | |
5186 | if (n < 0) | |
5187 | return log_oom(); | |
5188 | ||
102b0214 | 5189 | bus_print_property_value(name, expected_value, value, h); |
08f3be7a | 5190 | |
07636114 | 5191 | return 1; |
9c1f9465 YW |
5192 | |
5193 | } else if (STR_IN_SET(name, "IPAddressAllow", "IPAddressDeny")) { | |
5194 | _cleanup_free_ char *addresses = NULL; | |
5195 | ||
5196 | r = sd_bus_message_enter_container(m, 'a', "(iayu)"); | |
5197 | if (r < 0) | |
5198 | return bus_log_parse_error(r); | |
5199 | ||
5200 | for (;;) { | |
5201 | _cleanup_free_ char *str = NULL; | |
5202 | uint32_t prefixlen; | |
5203 | int32_t family; | |
5204 | const void *ap; | |
5205 | size_t an; | |
5206 | ||
5207 | r = sd_bus_message_enter_container(m, 'r', "iayu"); | |
5208 | if (r < 0) | |
5209 | return bus_log_parse_error(r); | |
5210 | if (r == 0) | |
5211 | break; | |
5212 | ||
5213 | r = sd_bus_message_read(m, "i", &family); | |
5214 | if (r < 0) | |
5215 | return bus_log_parse_error(r); | |
5216 | ||
5217 | r = sd_bus_message_read_array(m, 'y', &ap, &an); | |
5218 | if (r < 0) | |
5219 | return bus_log_parse_error(r); | |
5220 | ||
5221 | r = sd_bus_message_read(m, "u", &prefixlen); | |
5222 | if (r < 0) | |
5223 | return bus_log_parse_error(r); | |
5224 | ||
5225 | r = sd_bus_message_exit_container(m); | |
5226 | if (r < 0) | |
5227 | return bus_log_parse_error(r); | |
5228 | ||
5229 | if (!IN_SET(family, AF_INET, AF_INET6)) | |
5230 | continue; | |
5231 | ||
5232 | if (an != FAMILY_ADDRESS_SIZE(family)) | |
5233 | continue; | |
5234 | ||
5235 | if (prefixlen > FAMILY_ADDRESS_SIZE(family) * 8) | |
5236 | continue; | |
5237 | ||
5238 | if (in_addr_prefix_to_string(family, (union in_addr_union *) ap, prefixlen, &str) < 0) | |
5239 | continue; | |
5240 | ||
5241 | if (!strextend_with_separator(&addresses, " ", str, NULL)) | |
5242 | return log_oom(); | |
5243 | } | |
5244 | ||
5245 | r = sd_bus_message_exit_container(m); | |
5246 | if (r < 0) | |
5247 | return bus_log_parse_error(r); | |
5248 | ||
5249 | if (all || !isempty(addresses)) | |
5250 | bus_print_property_value(name, expected_value, value, strempty(addresses)); | |
5251 | ||
5252 | return 1; | |
4c787df7 YW |
5253 | |
5254 | } else if (STR_IN_SET(name, "BindPaths", "BindReadOnlyPaths")) { | |
5255 | _cleanup_free_ char *paths = NULL; | |
5256 | const char *source, *dest; | |
5257 | int ignore_enoent; | |
5258 | uint64_t rbind; | |
5259 | ||
5260 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(ssbt)"); | |
5261 | if (r < 0) | |
5262 | return bus_log_parse_error(r); | |
5263 | ||
5264 | while ((r = sd_bus_message_read(m, "(ssbt)", &source, &dest, &ignore_enoent, &rbind)) > 0) { | |
5265 | _cleanup_free_ char *str = NULL; | |
5266 | ||
5267 | if (isempty(source)) | |
5268 | continue; | |
5269 | ||
5270 | if (asprintf(&str, "%s%s%s%s%s", | |
5271 | ignore_enoent ? "-" : "", | |
5272 | source, | |
5273 | isempty(dest) ? "" : ":", | |
5274 | strempty(dest), | |
5275 | rbind == MS_REC ? ":rbind" : "") < 0) | |
5276 | return log_oom(); | |
5277 | ||
5278 | if (!strextend_with_separator(&paths, " ", str, NULL)) | |
5279 | return log_oom(); | |
5280 | } | |
5281 | if (r < 0) | |
5282 | return bus_log_parse_error(r); | |
5283 | ||
5284 | r = sd_bus_message_exit_container(m); | |
5285 | if (r < 0) | |
5286 | return bus_log_parse_error(r); | |
5287 | ||
5288 | if (all || !isempty(paths)) | |
5289 | bus_print_property_value(name, expected_value, value, strempty(paths)); | |
5290 | ||
5291 | return 1; | |
5292 | ||
5293 | } else if (streq(name, "TemporaryFileSystem")) { | |
5294 | _cleanup_free_ char *paths = NULL; | |
5295 | const char *target, *option; | |
5296 | ||
5297 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(ss)"); | |
5298 | if (r < 0) | |
5299 | return bus_log_parse_error(r); | |
5300 | ||
5301 | while ((r = sd_bus_message_read(m, "(ss)", &target, &option)) > 0) { | |
5302 | _cleanup_free_ char *str = NULL; | |
5303 | ||
5304 | if (isempty(target)) | |
5305 | continue; | |
5306 | ||
5307 | if (asprintf(&str, "%s%s%s", target, isempty(option) ? "" : ":", strempty(option)) < 0) | |
5308 | return log_oom(); | |
5309 | ||
5310 | if (!strextend_with_separator(&paths, " ", str, NULL)) | |
5311 | return log_oom(); | |
5312 | } | |
5313 | if (r < 0) | |
5314 | return bus_log_parse_error(r); | |
5315 | ||
5316 | r = sd_bus_message_exit_container(m); | |
5317 | if (r < 0) | |
5318 | return bus_log_parse_error(r); | |
5319 | ||
5320 | if (all || !isempty(paths)) | |
5321 | bus_print_property_value(name, expected_value, value, strempty(paths)); | |
5322 | ||
5323 | return 1; | |
305a2850 YW |
5324 | |
5325 | } else if (streq(name, "LogExtraFields")) { | |
5326 | _cleanup_free_ char *fields = NULL; | |
5327 | const void *p; | |
5328 | size_t sz; | |
5329 | ||
5330 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "ay"); | |
5331 | if (r < 0) | |
5332 | return bus_log_parse_error(r); | |
5333 | ||
5334 | while ((r = sd_bus_message_read_array(m, 'y', &p, &sz)) > 0) { | |
5335 | _cleanup_free_ char *str = NULL; | |
5336 | const char *eq; | |
5337 | ||
5338 | if (memchr(p, 0, sz)) | |
5339 | continue; | |
5340 | ||
5341 | eq = memchr(p, '=', sz); | |
5342 | if (!eq) | |
5343 | continue; | |
5344 | ||
5345 | if (!journal_field_valid(p, eq - (const char*) p, false)) | |
5346 | continue; | |
5347 | ||
5348 | str = malloc(sz + 1); | |
5349 | if (!str) | |
5350 | return log_oom(); | |
5351 | ||
5352 | memcpy(str, p, sz); | |
5353 | str[sz] = '\0'; | |
5354 | ||
5355 | if (!utf8_is_valid(str)) | |
5356 | continue; | |
5357 | ||
5358 | if (!strextend_with_separator(&fields, " ", str, NULL)) | |
5359 | return log_oom(); | |
5360 | } | |
5361 | if (r < 0) | |
5362 | return bus_log_parse_error(r); | |
5363 | ||
5364 | r = sd_bus_message_exit_container(m); | |
5365 | if (r < 0) | |
5366 | return bus_log_parse_error(r); | |
5367 | ||
5368 | if (all || !isempty(fields)) | |
5369 | bus_print_property_value(name, expected_value, value, strempty(fields)); | |
5370 | ||
5371 | return 1; | |
48220598 LP |
5372 | } |
5373 | ||
5374 | break; | |
5375 | } | |
5376 | ||
48220598 LP |
5377 | return 0; |
5378 | } | |
5379 | ||
8559e61d YW |
5380 | typedef enum SystemctlShowMode{ |
5381 | SYSTEMCTL_SHOW_PROPERTIES, | |
5382 | SYSTEMCTL_SHOW_STATUS, | |
5383 | SYSTEMCTL_SHOW_HELP, | |
5384 | _SYSTEMCTL_SHOW_MODE_MAX, | |
5385 | _SYSTEMCTL_SHOW_MODE_INVALID = -1, | |
5386 | } SystemctlShowMode; | |
5387 | ||
aa8d423e | 5388 | static const char* const systemctl_show_mode_table[_SYSTEMCTL_SHOW_MODE_MAX] = { |
8559e61d YW |
5389 | [SYSTEMCTL_SHOW_PROPERTIES] = "show", |
5390 | [SYSTEMCTL_SHOW_STATUS] = "status", | |
5391 | [SYSTEMCTL_SHOW_HELP] = "help", | |
5392 | }; | |
5393 | ||
5394 | DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(systemctl_show_mode, SystemctlShowMode); | |
5395 | ||
f459b602 | 5396 | static int show_one( |
f459b602 MAP |
5397 | sd_bus *bus, |
5398 | const char *path, | |
3dced37b | 5399 | const char *unit, |
8559e61d | 5400 | SystemctlShowMode show_mode, |
f459b602 MAP |
5401 | bool *new_line, |
5402 | bool *ellipsized) { | |
5403 | ||
3dced37b | 5404 | static const struct bus_properties_map property_map[] = { |
4679a8c3 YW |
5405 | { "LoadState", "s", NULL, offsetof(UnitStatusInfo, load_state) }, |
5406 | { "ActiveState", "s", NULL, offsetof(UnitStatusInfo, active_state) }, | |
5407 | { "Documentation", "as", NULL, offsetof(UnitStatusInfo, documentation) }, | |
5408 | {} | |
5409 | }, status_map[] = { | |
5410 | { "Id", "s", NULL, offsetof(UnitStatusInfo, id) }, | |
5411 | { "LoadState", "s", NULL, offsetof(UnitStatusInfo, load_state) }, | |
5412 | { "ActiveState", "s", NULL, offsetof(UnitStatusInfo, active_state) }, | |
5413 | { "SubState", "s", NULL, offsetof(UnitStatusInfo, sub_state) }, | |
5414 | { "UnitFileState", "s", NULL, offsetof(UnitStatusInfo, unit_file_state) }, | |
5415 | { "UnitFilePreset", "s", NULL, offsetof(UnitStatusInfo, unit_file_preset) }, | |
5416 | { "Description", "s", NULL, offsetof(UnitStatusInfo, description) }, | |
5417 | { "Following", "s", NULL, offsetof(UnitStatusInfo, following) }, | |
5418 | { "Documentation", "as", NULL, offsetof(UnitStatusInfo, documentation) }, | |
5419 | { "FragmentPath", "s", NULL, offsetof(UnitStatusInfo, fragment_path) }, | |
5420 | { "SourcePath", "s", NULL, offsetof(UnitStatusInfo, source_path) }, | |
5421 | { "ControlGroup", "s", NULL, offsetof(UnitStatusInfo, control_group) }, | |
5422 | { "DropInPaths", "as", NULL, offsetof(UnitStatusInfo, dropin_paths) }, | |
5423 | { "LoadError", "(ss)", map_load_error, offsetof(UnitStatusInfo, load_error) }, | |
5424 | { "Result", "s", NULL, offsetof(UnitStatusInfo, result) }, | |
5425 | { "InactiveExitTimestamp", "t", NULL, offsetof(UnitStatusInfo, inactive_exit_timestamp) }, | |
5426 | { "InactiveExitTimestampMonotonic", "t", NULL, offsetof(UnitStatusInfo, inactive_exit_timestamp_monotonic) }, | |
5427 | { "ActiveEnterTimestamp", "t", NULL, offsetof(UnitStatusInfo, active_enter_timestamp) }, | |
5428 | { "ActiveExitTimestamp", "t", NULL, offsetof(UnitStatusInfo, active_exit_timestamp) }, | |
5429 | { "InactiveEnterTimestamp", "t", NULL, offsetof(UnitStatusInfo, inactive_enter_timestamp) }, | |
5430 | { "NeedDaemonReload", "b", NULL, offsetof(UnitStatusInfo, need_daemon_reload) }, | |
5431 | { "Transient", "b", NULL, offsetof(UnitStatusInfo, transient) }, | |
5432 | { "ExecMainPID", "u", NULL, offsetof(UnitStatusInfo, main_pid) }, | |
5433 | { "MainPID", "u", map_main_pid, 0 }, | |
5434 | { "ControlPID", "u", NULL, offsetof(UnitStatusInfo, control_pid) }, | |
5435 | { "StatusText", "s", NULL, offsetof(UnitStatusInfo, status_text) }, | |
5436 | { "PIDFile", "s", NULL, offsetof(UnitStatusInfo, pid_file) }, | |
5437 | { "StatusErrno", "i", NULL, offsetof(UnitStatusInfo, status_errno) }, | |
5438 | { "ExecMainStartTimestamp", "t", NULL, offsetof(UnitStatusInfo, start_timestamp) }, | |
5439 | { "ExecMainExitTimestamp", "t", NULL, offsetof(UnitStatusInfo, exit_timestamp) }, | |
5440 | { "ExecMainCode", "i", NULL, offsetof(UnitStatusInfo, exit_code) }, | |
5441 | { "ExecMainStatus", "i", NULL, offsetof(UnitStatusInfo, exit_status) }, | |
5442 | { "ConditionTimestamp", "t", NULL, offsetof(UnitStatusInfo, condition_timestamp) }, | |
5443 | { "ConditionResult", "b", NULL, offsetof(UnitStatusInfo, condition_result) }, | |
5444 | { "Conditions", "a(sbbsi)", map_conditions, 0 }, | |
5445 | { "AssertTimestamp", "t", NULL, offsetof(UnitStatusInfo, assert_timestamp) }, | |
5446 | { "AssertResult", "b", NULL, offsetof(UnitStatusInfo, assert_result) }, | |
5447 | { "Asserts", "a(sbbsi)", map_asserts, 0 }, | |
5448 | { "NextElapseUSecRealtime", "t", NULL, offsetof(UnitStatusInfo, next_elapse_real) }, | |
5449 | { "NextElapseUSecMonotonic", "t", NULL, offsetof(UnitStatusInfo, next_elapse_monotonic) }, | |
5450 | { "NAccepted", "u", NULL, offsetof(UnitStatusInfo, n_accepted) }, | |
5451 | { "NConnections", "u", NULL, offsetof(UnitStatusInfo, n_connections) }, | |
a98f7575 | 5452 | { "NRefused", "u", NULL, offsetof(UnitStatusInfo, n_refused) }, |
4679a8c3 YW |
5453 | { "Accept", "b", NULL, offsetof(UnitStatusInfo, accept) }, |
5454 | { "Listen", "a(ss)", map_listen, offsetof(UnitStatusInfo, listen) }, | |
5455 | { "SysFSPath", "s", NULL, offsetof(UnitStatusInfo, sysfs_path) }, | |
5456 | { "Where", "s", NULL, offsetof(UnitStatusInfo, where) }, | |
5457 | { "What", "s", NULL, offsetof(UnitStatusInfo, what) }, | |
5458 | { "MemoryCurrent", "t", NULL, offsetof(UnitStatusInfo, memory_current) }, | |
48422635 | 5459 | { "MemoryMin", "t", NULL, offsetof(UnitStatusInfo, memory_min) }, |
4679a8c3 YW |
5460 | { "MemoryLow", "t", NULL, offsetof(UnitStatusInfo, memory_low) }, |
5461 | { "MemoryHigh", "t", NULL, offsetof(UnitStatusInfo, memory_high) }, | |
5462 | { "MemoryMax", "t", NULL, offsetof(UnitStatusInfo, memory_max) }, | |
5463 | { "MemorySwapMax", "t", NULL, offsetof(UnitStatusInfo, memory_swap_max) }, | |
5464 | { "MemoryLimit", "t", NULL, offsetof(UnitStatusInfo, memory_limit) }, | |
5465 | { "CPUUsageNSec", "t", NULL, offsetof(UnitStatusInfo, cpu_usage_nsec) }, | |
5466 | { "TasksCurrent", "t", NULL, offsetof(UnitStatusInfo, tasks_current) }, | |
5467 | { "TasksMax", "t", NULL, offsetof(UnitStatusInfo, tasks_max) }, | |
5468 | { "IPIngressBytes", "t", NULL, offsetof(UnitStatusInfo, ip_ingress_bytes) }, | |
5469 | { "IPEgressBytes", "t", NULL, offsetof(UnitStatusInfo, ip_egress_bytes) }, | |
5470 | { "ExecStartPre", "a(sasbttttuii)", map_exec, 0 }, | |
5471 | { "ExecStart", "a(sasbttttuii)", map_exec, 0 }, | |
5472 | { "ExecStartPost", "a(sasbttttuii)", map_exec, 0 }, | |
5473 | { "ExecReload", "a(sasbttttuii)", map_exec, 0 }, | |
5474 | { "ExecStopPre", "a(sasbttttuii)", map_exec, 0 }, | |
5475 | { "ExecStop", "a(sasbttttuii)", map_exec, 0 }, | |
5476 | { "ExecStopPost", "a(sasbttttuii)", map_exec, 0 }, | |
3dced37b LP |
5477 | {} |
5478 | }; | |
5479 | ||
4afd3348 LP |
5480 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
5481 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
3dced37b | 5482 | _cleanup_set_free_ Set *found_properties = NULL; |
a7335518 | 5483 | _cleanup_(unit_status_info_free) UnitStatusInfo info = { |
934277fe | 5484 | .memory_current = (uint64_t) -1, |
da4d897e TH |
5485 | .memory_high = CGROUP_LIMIT_MAX, |
5486 | .memory_max = CGROUP_LIMIT_MAX, | |
96e131ea | 5487 | .memory_swap_max = CGROUP_LIMIT_MAX, |
934277fe | 5488 | .memory_limit = (uint64_t) -1, |
5ad096b3 | 5489 | .cpu_usage_nsec = (uint64_t) -1, |
03a7b521 LP |
5490 | .tasks_current = (uint64_t) -1, |
5491 | .tasks_max = (uint64_t) -1, | |
0e97c93f DM |
5492 | .ip_ingress_bytes = (uint64_t) -1, |
5493 | .ip_egress_bytes = (uint64_t) -1, | |
934277fe | 5494 | }; |
4679a8c3 | 5495 | char **pp; |
f459b602 | 5496 | int r; |
48220598 | 5497 | |
48220598 | 5498 | assert(path); |
61cbdc4b | 5499 | assert(new_line); |
48220598 | 5500 | |
e3e0314b ZJS |
5501 | log_debug("Showing one %s", path); |
5502 | ||
4679a8c3 | 5503 | r = bus_map_all_properties( |
f22f08cd SP |
5504 | bus, |
5505 | "org.freedesktop.systemd1", | |
5506 | path, | |
4679a8c3 | 5507 | show_mode == SYSTEMCTL_SHOW_STATUS ? status_map : property_map, |
a7e4861c | 5508 | BUS_MAP_BOOLEAN_AS_BOOL, |
f459b602 | 5509 | &error, |
f22f08cd | 5510 | &reply, |
4679a8c3 | 5511 | &info); |
4c3e8e39 LP |
5512 | if (r < 0) |
5513 | return log_error_errno(r, "Failed to get properties: %s", bus_error_message(&error, r)); | |
48220598 | 5514 | |
4679a8c3 YW |
5515 | if (unit && streq_ptr(info.load_state, "not-found") && streq_ptr(info.active_state, "inactive")) { |
5516 | log_full(show_mode == SYSTEMCTL_SHOW_STATUS ? LOG_ERR : LOG_DEBUG, | |
5517 | "Unit %s could not be found.", unit); | |
e33a06a1 | 5518 | |
4679a8c3 YW |
5519 | if (show_mode == SYSTEMCTL_SHOW_STATUS) |
5520 | return EXIT_PROGRAM_OR_SERVICES_STATUS_UNKNOWN; | |
5521 | else if (show_mode == SYSTEMCTL_SHOW_HELP) | |
5522 | return -ENOENT; | |
5523 | } | |
e33a06a1 | 5524 | |
4679a8c3 YW |
5525 | if (*new_line) |
5526 | printf("\n"); | |
3dced37b | 5527 | |
4679a8c3 YW |
5528 | *new_line = true; |
5529 | ||
5530 | if (show_mode == SYSTEMCTL_SHOW_STATUS) { | |
5531 | print_status_info(bus, &info, ellipsized); | |
5532 | ||
5533 | if (info.active_state && !STR_IN_SET(info.active_state, "active", "reloading")) | |
5534 | return EXIT_PROGRAM_NOT_RUNNING; | |
5535 | ||
5536 | return EXIT_PROGRAM_RUNNING_OR_SERVICE_OK; | |
5537 | ||
5538 | } else if (show_mode == SYSTEMCTL_SHOW_HELP) { | |
5539 | show_unit_help(&info); | |
5540 | return 0; | |
3dced37b | 5541 | } |
e33a06a1 | 5542 | |
4679a8c3 YW |
5543 | r = sd_bus_message_rewind(reply, true); |
5544 | if (r < 0) | |
5545 | return log_error_errno(r, "Failed to rewind: %s", bus_error_message(&error, r)); | |
5546 | ||
07636114 | 5547 | r = bus_message_print_all_properties(reply, print_property, arg_properties, arg_value, arg_all, &found_properties); |
f459b602 MAP |
5548 | if (r < 0) |
5549 | return bus_log_parse_error(r); | |
48220598 | 5550 | |
4679a8c3 YW |
5551 | STRV_FOREACH(pp, arg_properties) |
5552 | if (!set_contains(found_properties, *pp)) | |
5553 | log_debug("Property %s does not exist.", *pp); | |
3dced37b | 5554 | |
4679a8c3 | 5555 | return 0; |
48220598 LP |
5556 | } |
5557 | ||
f74294c1 | 5558 | static int get_unit_dbus_path_by_pid( |
f459b602 MAP |
5559 | sd_bus *bus, |
5560 | uint32_t pid, | |
f74294c1 | 5561 | char **unit) { |
f459b602 | 5562 | |
4afd3348 LP |
5563 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
5564 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
373d32c9 | 5565 | char *u; |
a223b325 MS |
5566 | int r; |
5567 | ||
f459b602 | 5568 | r = sd_bus_call_method( |
f22f08cd SP |
5569 | bus, |
5570 | "org.freedesktop.systemd1", | |
5571 | "/org/freedesktop/systemd1", | |
5572 | "org.freedesktop.systemd1.Manager", | |
5573 | "GetUnitByPID", | |
f459b602 | 5574 | &error, |
f22f08cd | 5575 | &reply, |
f459b602 | 5576 | "u", pid); |
691395d8 LP |
5577 | if (r < 0) |
5578 | return log_error_errno(r, "Failed to get unit for PID %"PRIu32": %s", pid, bus_error_message(&error, r)); | |
a223b325 | 5579 | |
373d32c9 | 5580 | r = sd_bus_message_read(reply, "o", &u); |
f459b602 MAP |
5581 | if (r < 0) |
5582 | return bus_log_parse_error(r); | |
5583 | ||
373d32c9 LP |
5584 | u = strdup(u); |
5585 | if (!u) | |
5586 | return log_oom(); | |
5587 | ||
5588 | *unit = u; | |
f74294c1 | 5589 | return 0; |
a223b325 MS |
5590 | } |
5591 | ||
f459b602 | 5592 | static int show_all( |
f459b602 | 5593 | sd_bus *bus, |
f459b602 MAP |
5594 | bool *new_line, |
5595 | bool *ellipsized) { | |
5596 | ||
4afd3348 | 5597 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
f459b602 MAP |
5598 | _cleanup_free_ UnitInfo *unit_infos = NULL; |
5599 | const UnitInfo *u; | |
5600 | unsigned c; | |
5bb75bc7 | 5601 | int r, ret = 0; |
265a7a2a | 5602 | |
1238ee09 | 5603 | r = get_unit_list(bus, NULL, NULL, &unit_infos, 0, &reply); |
265a7a2a ZJS |
5604 | if (r < 0) |
5605 | return r; | |
5606 | ||
0221d68a | 5607 | (void) pager_open(arg_pager_flags); |
dbed408b | 5608 | |
f459b602 MAP |
5609 | c = (unsigned) r; |
5610 | ||
93bab288 | 5611 | typesafe_qsort(unit_infos, c, compare_unit_info); |
991f2a39 | 5612 | |
265a7a2a | 5613 | for (u = unit_infos; u < unit_infos + c; u++) { |
7fd1b19b | 5614 | _cleanup_free_ char *p = NULL; |
265a7a2a | 5615 | |
265a7a2a ZJS |
5616 | p = unit_dbus_path_from_name(u->id); |
5617 | if (!p) | |
5618 | return log_oom(); | |
5619 | ||
8559e61d | 5620 | r = show_one(bus, p, u->id, SYSTEMCTL_SHOW_STATUS, new_line, ellipsized); |
3df538da | 5621 | if (r < 0) |
265a7a2a | 5622 | return r; |
5bb75bc7 ZJS |
5623 | else if (r > 0 && ret == 0) |
5624 | ret = r; | |
265a7a2a ZJS |
5625 | } |
5626 | ||
5bb75bc7 | 5627 | return ret; |
265a7a2a ZJS |
5628 | } |
5629 | ||
8fcf784d LP |
5630 | static int show_system_status(sd_bus *bus) { |
5631 | char since1[FORMAT_TIMESTAMP_RELATIVE_MAX], since2[FORMAT_TIMESTAMP_MAX]; | |
f9e0eefc | 5632 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
e7e55dbd | 5633 | _cleanup_(machine_info_clear) struct machine_info mi = {}; |
f9e0eefc | 5634 | _cleanup_free_ char *hn = NULL; |
8fcf784d LP |
5635 | const char *on, *off; |
5636 | int r; | |
5637 | ||
5638 | hn = gethostname_malloc(); | |
5639 | if (!hn) | |
5640 | return log_oom(); | |
5641 | ||
a7e4861c YW |
5642 | r = bus_map_all_properties( |
5643 | bus, | |
5644 | "org.freedesktop.systemd1", | |
5645 | "/org/freedesktop/systemd1", | |
5646 | machine_info_property_map, | |
5647 | BUS_MAP_STRDUP, | |
5648 | &error, | |
5649 | NULL, | |
5650 | &mi); | |
f647962d | 5651 | if (r < 0) |
f9e0eefc | 5652 | return log_error_errno(r, "Failed to read server status: %s", bus_error_message(&error, r)); |
8fcf784d | 5653 | |
8fcf784d LP |
5654 | if (streq_ptr(mi.state, "degraded")) { |
5655 | on = ansi_highlight_red(); | |
1fc464f6 | 5656 | off = ansi_normal(); |
92f34a9c JR |
5657 | } else if (streq_ptr(mi.state, "running")) { |
5658 | on = ansi_highlight_green(); | |
5659 | off = ansi_normal(); | |
5660 | } else { | |
8fcf784d | 5661 | on = ansi_highlight_yellow(); |
1fc464f6 | 5662 | off = ansi_normal(); |
92f34a9c | 5663 | } |
8fcf784d | 5664 | |
9a6f746f | 5665 | printf("%s%s%s %s\n", on, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE), off, arg_host ? arg_host : hn); |
b0d14c69 | 5666 | |
8fcf784d LP |
5667 | printf(" State: %s%s%s\n", |
5668 | on, strna(mi.state), off); | |
5669 | ||
c6ba5b80 MP |
5670 | printf(" Jobs: %" PRIu32 " queued\n", mi.n_jobs); |
5671 | printf(" Failed: %" PRIu32 " units\n", mi.n_failed_units); | |
8fcf784d LP |
5672 | |
5673 | printf(" Since: %s; %s\n", | |
5674 | format_timestamp(since2, sizeof(since2), mi.timestamp), | |
5675 | format_timestamp_relative(since1, sizeof(since1), mi.timestamp)); | |
5676 | ||
5677 | printf(" CGroup: %s\n", mi.control_group ?: "/"); | |
4c315c2c IS |
5678 | if (IN_SET(arg_transport, |
5679 | BUS_TRANSPORT_LOCAL, | |
5680 | BUS_TRANSPORT_MACHINE)) { | |
8fcf784d LP |
5681 | static const char prefix[] = " "; |
5682 | unsigned c; | |
5683 | ||
5684 | c = columns(); | |
5685 | if (c > sizeof(prefix) - 1) | |
5686 | c -= sizeof(prefix) - 1; | |
5687 | else | |
5688 | c = 0; | |
5689 | ||
0ff308c8 | 5690 | show_cgroup(SYSTEMD_CGROUP_CONTROLLER, strempty(mi.control_group), prefix, c, get_output_flags()); |
8fcf784d LP |
5691 | } |
5692 | ||
8fcf784d LP |
5693 | return 0; |
5694 | } | |
5695 | ||
e449de87 | 5696 | static int show(int argc, char *argv[], void *userdata) { |
8559e61d YW |
5697 | bool new_line = false, ellipsized = false; |
5698 | SystemctlShowMode show_mode; | |
e3e0314b | 5699 | int r, ret = 0; |
4fbd7192 | 5700 | sd_bus *bus; |
48220598 | 5701 | |
e449de87 | 5702 | assert(argv); |
48220598 | 5703 | |
8559e61d | 5704 | show_mode = systemctl_show_mode_from_string(argv[0]); |
baaa35ad ZJS |
5705 | if (show_mode < 0) |
5706 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
5707 | "Invalid argument."); | |
e449de87 | 5708 | |
baaa35ad ZJS |
5709 | if (show_mode == SYSTEMCTL_SHOW_HELP && argc <= 1) |
5710 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
5711 | "This command expects one or more unit names. Did you mean --help?"); | |
61cbdc4b | 5712 | |
d2ad7e1b ZJS |
5713 | r = acquire_bus(BUS_MANAGER, &bus); |
5714 | if (r < 0) | |
5715 | return r; | |
5716 | ||
0221d68a | 5717 | (void) pager_open(arg_pager_flags); |
ec14911e | 5718 | |
4fbd7192 | 5719 | /* If no argument is specified inspect the manager itself */ |
8559e61d YW |
5720 | if (show_mode == SYSTEMCTL_SHOW_PROPERTIES && argc <= 1) |
5721 | return show_one(bus, "/org/freedesktop/systemd1", NULL, show_mode, &new_line, &ellipsized); | |
48220598 | 5722 | |
8559e61d | 5723 | if (show_mode == SYSTEMCTL_SHOW_STATUS && argc <= 1) { |
8fcf784d | 5724 | |
8fcf784d LP |
5725 | show_system_status(bus); |
5726 | new_line = true; | |
5727 | ||
5728 | if (arg_all) | |
8559e61d | 5729 | ret = show_all(bus, &new_line, &ellipsized); |
8fcf784d | 5730 | } else { |
e3e0314b ZJS |
5731 | _cleanup_free_ char **patterns = NULL; |
5732 | char **name; | |
5733 | ||
e449de87 | 5734 | STRV_FOREACH(name, strv_skip(argv, 1)) { |
3dced37b | 5735 | _cleanup_free_ char *path = NULL, *unit = NULL; |
94e0bd7d | 5736 | uint32_t id; |
48220598 | 5737 | |
94e0bd7d | 5738 | if (safe_atou32(*name, &id) < 0) { |
e3e0314b | 5739 | if (strv_push(&patterns, *name) < 0) |
94e0bd7d | 5740 | return log_oom(); |
48220598 | 5741 | |
e3e0314b | 5742 | continue; |
8559e61d | 5743 | } else if (show_mode == SYSTEMCTL_SHOW_PROPERTIES) { |
94e0bd7d | 5744 | /* Interpret as job id */ |
3dced37b | 5745 | if (asprintf(&path, "/org/freedesktop/systemd1/job/%u", id) < 0) |
94e0bd7d | 5746 | return log_oom(); |
48220598 | 5747 | |
94e0bd7d ZJS |
5748 | } else { |
5749 | /* Interpret as PID */ | |
3dced37b | 5750 | r = get_unit_dbus_path_by_pid(bus, id, &path); |
373d32c9 | 5751 | if (r < 0) { |
94e0bd7d | 5752 | ret = r; |
373d32c9 LP |
5753 | continue; |
5754 | } | |
3dced37b LP |
5755 | |
5756 | r = unit_name_from_dbus_path(path, &unit); | |
5757 | if (r < 0) | |
5758 | return log_oom(); | |
94e0bd7d | 5759 | } |
f74294c1 | 5760 | |
8559e61d | 5761 | r = show_one(bus, path, unit, show_mode, &new_line, &ellipsized); |
5bb75bc7 ZJS |
5762 | if (r < 0) |
5763 | return r; | |
5764 | else if (r > 0 && ret == 0) | |
5765 | ret = r; | |
48220598 | 5766 | } |
94e0bd7d | 5767 | |
e3e0314b ZJS |
5768 | if (!strv_isempty(patterns)) { |
5769 | _cleanup_strv_free_ char **names = NULL; | |
5770 | ||
5771 | r = expand_names(bus, patterns, NULL, &names); | |
5772 | if (r < 0) | |
691395d8 | 5773 | return log_error_errno(r, "Failed to expand names: %m"); |
e3e0314b ZJS |
5774 | |
5775 | STRV_FOREACH(name, names) { | |
3dced37b | 5776 | _cleanup_free_ char *path; |
e3e0314b | 5777 | |
3dced37b LP |
5778 | path = unit_dbus_path_from_name(*name); |
5779 | if (!path) | |
e3e0314b ZJS |
5780 | return log_oom(); |
5781 | ||
8559e61d | 5782 | r = show_one(bus, path, *name, show_mode, &new_line, &ellipsized); |
5bb75bc7 ZJS |
5783 | if (r < 0) |
5784 | return r; | |
3dced37b | 5785 | if (r > 0 && ret == 0) |
5bb75bc7 | 5786 | ret = r; |
e3e0314b ZJS |
5787 | } |
5788 | } | |
5789 | } | |
5790 | ||
94e0bd7d ZJS |
5791 | if (ellipsized && !arg_quiet) |
5792 | printf("Hint: Some lines were ellipsized, use -l to show in full.\n"); | |
48220598 | 5793 | |
22f4096c | 5794 | return ret; |
0183528f LP |
5795 | } |
5796 | ||
e449de87 | 5797 | static int cat(int argc, char *argv[], void *userdata) { |
8e766630 | 5798 | _cleanup_(lookup_paths_free) LookupPaths lp = {}; |
15ef1144 LP |
5799 | _cleanup_strv_free_ char **names = NULL; |
5800 | char **name; | |
4fbd7192 LP |
5801 | sd_bus *bus; |
5802 | bool first = true; | |
25586912 | 5803 | int r; |
15ef1144 | 5804 | |
3e7eed84 | 5805 | if (arg_transport != BUS_TRANSPORT_LOCAL) { |
4fbd7192 | 5806 | log_error("Cannot remotely cat units."); |
3e495a66 ZJS |
5807 | return -EINVAL; |
5808 | } | |
5809 | ||
4943d143 | 5810 | r = lookup_paths_init(&lp, arg_scope, 0, arg_root); |
ad2a0358 | 5811 | if (r < 0) |
c51932be | 5812 | return log_error_errno(r, "Failed to determine unit paths: %m"); |
ad2a0358 | 5813 | |
4fbd7192 | 5814 | r = acquire_bus(BUS_MANAGER, &bus); |
15ef1144 | 5815 | if (r < 0) |
4fbd7192 | 5816 | return r; |
15ef1144 | 5817 | |
e449de87 | 5818 | r = expand_names(bus, strv_skip(argv, 1), NULL, &names); |
4fbd7192 LP |
5819 | if (r < 0) |
5820 | return log_error_errno(r, "Failed to expand names: %m"); | |
ad2a0358 | 5821 | |
0221d68a | 5822 | (void) pager_open(arg_pager_flags); |
15ef1144 LP |
5823 | |
5824 | STRV_FOREACH(name, names) { | |
ad2a0358 | 5825 | _cleanup_free_ char *fragment_path = NULL; |
15ef1144 | 5826 | _cleanup_strv_free_ char **dropin_paths = NULL; |
15ef1144 | 5827 | |
02a126a3 | 5828 | r = unit_find_paths(bus, *name, &lp, false, &fragment_path, &dropin_paths); |
86f004fb | 5829 | if (r == -ERFKILL) { |
02a126a3 LP |
5830 | printf("%s# Unit %s is masked%s.\n", |
5831 | ansi_highlight_magenta(), | |
5832 | *name, | |
5833 | ansi_normal()); | |
5834 | continue; | |
5835 | } | |
5836 | if (r == -EKEYREJECTED) { | |
5837 | printf("%s# Unit %s could not be loaded.%s\n", | |
86f004fb ZJS |
5838 | ansi_highlight_magenta(), |
5839 | *name, | |
5840 | ansi_normal()); | |
5841 | continue; | |
5842 | } | |
ad2a0358 ZJS |
5843 | if (r < 0) |
5844 | return r; | |
02a126a3 | 5845 | if (r == 0) |
b5e6a600 | 5846 | return -ENOENT; |
15ef1144 LP |
5847 | |
5848 | if (first) | |
5849 | first = false; | |
5850 | else | |
5851 | puts(""); | |
5852 | ||
5b9635d1 | 5853 | if (need_daemon_reload(bus, *name) > 0) /* ignore errors (<0), this is informational output */ |
e100155d LW |
5854 | fprintf(stderr, |
5855 | "%s# Warning: %s changed on disk, the version systemd has loaded is outdated.\n" | |
5856 | "%s# This output shows the current version of the unit's original fragment and drop-in files.\n" | |
5857 | "%s# If fragments or drop-ins were added or removed, they are not properly reflected in this output.\n" | |
5858 | "%s# Run 'systemctl%s daemon-reload' to reload units.%s\n", | |
5859 | ansi_highlight_red(), | |
5860 | *name, | |
5861 | ansi_highlight_red(), | |
5862 | ansi_highlight_red(), | |
5863 | ansi_highlight_red(), | |
5864 | arg_scope == UNIT_FILE_SYSTEM ? "" : " --user", | |
5865 | ansi_normal()); | |
5866 | ||
854a42fb | 5867 | r = cat_files(fragment_path, dropin_paths, 0); |
81f5e513 ZJS |
5868 | if (r < 0) |
5869 | return r; | |
15ef1144 LP |
5870 | } |
5871 | ||
25586912 | 5872 | return 0; |
15ef1144 LP |
5873 | } |
5874 | ||
e449de87 | 5875 | static int set_property(int argc, char *argv[], void *userdata) { |
4afd3348 LP |
5876 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
5877 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
68372da6 | 5878 | _cleanup_free_ char *n = NULL; |
89ada3ba | 5879 | UnitType t; |
4fbd7192 | 5880 | sd_bus *bus; |
8e2af478 LP |
5881 | int r; |
5882 | ||
4fbd7192 LP |
5883 | r = acquire_bus(BUS_MANAGER, &bus); |
5884 | if (r < 0) | |
5885 | return r; | |
5886 | ||
8a4b13c5 | 5887 | polkit_agent_open_maybe(); |
d2ad7e1b | 5888 | |
f459b602 MAP |
5889 | r = sd_bus_message_new_method_call( |
5890 | bus, | |
151b9b96 | 5891 | &m, |
8e2af478 LP |
5892 | "org.freedesktop.systemd1", |
5893 | "/org/freedesktop/systemd1", | |
5894 | "org.freedesktop.systemd1.Manager", | |
151b9b96 | 5895 | "SetUnitProperties"); |
f459b602 MAP |
5896 | if (r < 0) |
5897 | return bus_log_create_error(r); | |
8e2af478 | 5898 | |
37cbc1d5 | 5899 | r = unit_name_mangle(argv[1], arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN, &n); |
7410616c LP |
5900 | if (r < 0) |
5901 | return log_error_errno(r, "Failed to mangle unit name: %m"); | |
68372da6 | 5902 | |
89ada3ba YW |
5903 | t = unit_name_to_type(n); |
5904 | if (t < 0) { | |
5905 | log_error("Invalid unit type: %s", n); | |
5906 | return -EINVAL; | |
5907 | } | |
5908 | ||
f459b602 MAP |
5909 | r = sd_bus_message_append(m, "sb", n, arg_runtime); |
5910 | if (r < 0) | |
5911 | return bus_log_create_error(r); | |
8e2af478 | 5912 | |
f459b602 MAP |
5913 | r = sd_bus_message_open_container(m, SD_BUS_TYPE_ARRAY, "(sv)"); |
5914 | if (r < 0) | |
5915 | return bus_log_create_error(r); | |
8e2af478 | 5916 | |
89ada3ba | 5917 | r = bus_append_unit_property_assignment_many(m, t, strv_skip(argv, 2)); |
8673cf13 LP |
5918 | if (r < 0) |
5919 | return r; | |
8e2af478 | 5920 | |
f459b602 MAP |
5921 | r = sd_bus_message_close_container(m); |
5922 | if (r < 0) | |
5923 | return bus_log_create_error(r); | |
8e2af478 | 5924 | |
c49b30a2 | 5925 | r = sd_bus_call(bus, m, 0, &error, NULL); |
691395d8 LP |
5926 | if (r < 0) |
5927 | return log_error_errno(r, "Failed to set unit properties on %s: %s", n, bus_error_message(&error, r)); | |
8e2af478 LP |
5928 | |
5929 | return 0; | |
5930 | } | |
5931 | ||
e449de87 | 5932 | static int daemon_reload(int argc, char *argv[], void *userdata) { |
4afd3348 | 5933 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
2853b60a | 5934 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
7e4249b9 | 5935 | const char *method; |
4fbd7192 | 5936 | sd_bus *bus; |
f459b602 | 5937 | int r; |
7e4249b9 | 5938 | |
4fbd7192 LP |
5939 | r = acquire_bus(BUS_MANAGER, &bus); |
5940 | if (r < 0) | |
5941 | return r; | |
5942 | ||
8a4b13c5 | 5943 | polkit_agent_open_maybe(); |
d2ad7e1b | 5944 | |
2853b60a LP |
5945 | switch (arg_action) { |
5946 | ||
5947 | case ACTION_RELOAD: | |
e4b61340 | 5948 | method = "Reload"; |
2853b60a LP |
5949 | break; |
5950 | ||
5951 | case ACTION_REEXEC: | |
e4b61340 | 5952 | method = "Reexecute"; |
2853b60a LP |
5953 | break; |
5954 | ||
5955 | case ACTION_SYSTEMCTL: | |
5956 | method = streq(argv[0], "daemon-reexec") ? "Reexecute" : | |
5957 | /* "daemon-reload" */ "Reload"; | |
5958 | break; | |
e4b61340 | 5959 | |
2853b60a LP |
5960 | default: |
5961 | assert_not_reached("Unexpected action"); | |
e4b61340 | 5962 | } |
7e4249b9 | 5963 | |
2853b60a LP |
5964 | r = sd_bus_message_new_method_call( |
5965 | bus, | |
5966 | &m, | |
5967 | "org.freedesktop.systemd1", | |
5968 | "/org/freedesktop/systemd1", | |
5969 | "org.freedesktop.systemd1.Manager", | |
5970 | method); | |
5971 | if (r < 0) | |
5972 | return bus_log_create_error(r); | |
5973 | ||
5974 | /* Note we use an extra-long timeout here. This is because a reload or reexec means generators are rerun which | |
5975 | * are timed out after DEFAULT_TIMEOUT_USEC. Let's use twice that time here, so that the generators can have | |
5976 | * their timeout, and for everything else there's the same time budget in place. */ | |
5977 | ||
5978 | r = sd_bus_call(bus, m, DEFAULT_TIMEOUT_USEC * 2, &error, NULL); | |
5979 | ||
5980 | /* On reexecution, we expect a disconnect, not a reply */ | |
5981 | if (IN_SET(r, -ETIMEDOUT, -ECONNRESET) && streq(method, "Reexecute")) | |
5982 | r = 0; | |
5983 | ||
5984 | if (r < 0 && arg_action == ACTION_SYSTEMCTL) | |
5985 | return log_error_errno(r, "Failed to reload daemon: %s", bus_error_message(&error, r)); | |
5986 | ||
5987 | /* Note that for the legacy commands (i.e. those with action != ACTION_SYSTEMCTL) we support fallbacks to the | |
5988 | * old ways of doing things, hence don't log any error in that case here. */ | |
5989 | ||
5990 | return r < 0 ? r : 0; | |
5991 | } | |
5992 | ||
5993 | static int trivial_method(int argc, char *argv[], void *userdata) { | |
5994 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
5995 | const char *method; | |
5996 | sd_bus *bus; | |
5997 | int r; | |
5998 | ||
1ae17672 ZJS |
5999 | if (arg_dry_run) |
6000 | return 0; | |
6001 | ||
2853b60a LP |
6002 | r = acquire_bus(BUS_MANAGER, &bus); |
6003 | if (r < 0) | |
6004 | return r; | |
6005 | ||
8a4b13c5 | 6006 | polkit_agent_open_maybe(); |
d2ad7e1b | 6007 | |
2853b60a LP |
6008 | method = |
6009 | streq(argv[0], "clear-jobs") || | |
6010 | streq(argv[0], "cancel") ? "ClearJobs" : | |
6011 | streq(argv[0], "reset-failed") ? "ResetFailed" : | |
6012 | streq(argv[0], "halt") ? "Halt" : | |
6013 | streq(argv[0], "reboot") ? "Reboot" : | |
6014 | streq(argv[0], "kexec") ? "KExec" : | |
6015 | streq(argv[0], "exit") ? "Exit" : | |
6016 | /* poweroff */ "PowerOff"; | |
6017 | ||
6e646d22 | 6018 | r = sd_bus_call_method( |
f22f08cd SP |
6019 | bus, |
6020 | "org.freedesktop.systemd1", | |
6021 | "/org/freedesktop/systemd1", | |
6022 | "org.freedesktop.systemd1.Manager", | |
6e646d22 LP |
6023 | method, |
6024 | &error, | |
6025 | NULL, | |
6026 | NULL); | |
2853b60a LP |
6027 | if (r < 0 && arg_action == ACTION_SYSTEMCTL) |
6028 | return log_error_errno(r, "Failed to execute operation: %s", bus_error_message(&error, r)); | |
6029 | ||
6030 | /* Note that for the legacy commands (i.e. those with action != ACTION_SYSTEMCTL) we support fallbacks to the | |
6031 | * old ways of doing things, hence don't log any error in that case here. */ | |
7e4249b9 | 6032 | |
0a9776c2 | 6033 | return r < 0 ? r : 0; |
7e4249b9 LP |
6034 | } |
6035 | ||
e449de87 | 6036 | static int reset_failed(int argc, char *argv[], void *userdata) { |
e3e0314b | 6037 | _cleanup_strv_free_ char **names = NULL; |
4fbd7192 | 6038 | sd_bus *bus; |
f84190d8 | 6039 | char **name; |
e3e0314b | 6040 | int r, q; |
5632e374 | 6041 | |
e449de87 | 6042 | if (argc <= 1) |
2853b60a | 6043 | return trivial_method(argc, argv, userdata); |
5632e374 | 6044 | |
4fbd7192 LP |
6045 | r = acquire_bus(BUS_MANAGER, &bus); |
6046 | if (r < 0) | |
6047 | return r; | |
6048 | ||
8a4b13c5 | 6049 | polkit_agent_open_maybe(); |
d2ad7e1b | 6050 | |
e449de87 | 6051 | r = expand_names(bus, strv_skip(argv, 1), NULL, &names); |
e3e0314b | 6052 | if (r < 0) |
691395d8 | 6053 | return log_error_errno(r, "Failed to expand names: %m"); |
f84190d8 | 6054 | |
e3e0314b | 6055 | STRV_FOREACH(name, names) { |
4afd3348 | 6056 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
342641fb | 6057 | |
6e646d22 | 6058 | q = sd_bus_call_method( |
f22f08cd | 6059 | bus, |
b0193f1c LP |
6060 | "org.freedesktop.systemd1", |
6061 | "/org/freedesktop/systemd1", | |
6062 | "org.freedesktop.systemd1.Manager", | |
6e646d22 LP |
6063 | "ResetFailedUnit", |
6064 | &error, | |
6065 | NULL, | |
6066 | "s", *name); | |
e3e0314b | 6067 | if (q < 0) { |
691395d8 | 6068 | log_error_errno(q, "Failed to reset failed state of unit %s: %s", *name, bus_error_message(&error, q)); |
e3e0314b ZJS |
6069 | if (r == 0) |
6070 | r = q; | |
f459b602 | 6071 | } |
5632e374 LP |
6072 | } |
6073 | ||
e3e0314b | 6074 | return r; |
5632e374 LP |
6075 | } |
6076 | ||
804ee07c ZJS |
6077 | static int print_variable(const char *s) { |
6078 | const char *sep; | |
6079 | _cleanup_free_ char *esc = NULL; | |
6080 | ||
6081 | sep = strchr(s, '='); | |
baaa35ad ZJS |
6082 | if (!sep) |
6083 | return log_error_errno(SYNTHETIC_ERRNO(EUCLEAN), | |
6084 | "Invalid environment block"); | |
804ee07c ZJS |
6085 | |
6086 | esc = shell_maybe_quote(sep + 1, ESCAPE_POSIX); | |
6087 | if (!esc) | |
6088 | return log_oom(); | |
6089 | ||
6090 | printf("%.*s=%s\n", (int)(sep-s), s, esc); | |
6091 | return 0; | |
6092 | } | |
6093 | ||
e449de87 | 6094 | static int show_environment(int argc, char *argv[], void *userdata) { |
4afd3348 LP |
6095 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
6096 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
f459b602 | 6097 | const char *text; |
4fbd7192 | 6098 | sd_bus *bus; |
7e4249b9 | 6099 | int r; |
7e4249b9 | 6100 | |
4fbd7192 LP |
6101 | r = acquire_bus(BUS_MANAGER, &bus); |
6102 | if (r < 0) | |
6103 | return r; | |
6104 | ||
0221d68a | 6105 | (void) pager_open(arg_pager_flags); |
d2ad7e1b | 6106 | |
f459b602 | 6107 | r = sd_bus_get_property( |
f22f08cd SP |
6108 | bus, |
6109 | "org.freedesktop.systemd1", | |
6110 | "/org/freedesktop/systemd1", | |
f459b602 MAP |
6111 | "org.freedesktop.systemd1.Manager", |
6112 | "Environment", | |
6113 | &error, | |
f22f08cd | 6114 | &reply, |
f459b602 | 6115 | "as"); |
691395d8 LP |
6116 | if (r < 0) |
6117 | return log_error_errno(r, "Failed to get environment: %s", bus_error_message(&error, r)); | |
7e4249b9 | 6118 | |
f459b602 MAP |
6119 | r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "s"); |
6120 | if (r < 0) | |
6121 | return bus_log_parse_error(r); | |
7e4249b9 | 6122 | |
804ee07c ZJS |
6123 | while ((r = sd_bus_message_read_basic(reply, SD_BUS_TYPE_STRING, &text)) > 0) { |
6124 | r = print_variable(text); | |
6125 | if (r < 0) | |
6126 | return r; | |
6127 | } | |
f459b602 MAP |
6128 | if (r < 0) |
6129 | return bus_log_parse_error(r); | |
7e4249b9 | 6130 | |
f459b602 MAP |
6131 | r = sd_bus_message_exit_container(reply); |
6132 | if (r < 0) | |
6133 | return bus_log_parse_error(r); | |
7e4249b9 | 6134 | |
f84190d8 | 6135 | return 0; |
7e4249b9 LP |
6136 | } |
6137 | ||
e449de87 | 6138 | static int switch_root(int argc, char *argv[], void *userdata) { |
4afd3348 | 6139 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
f39d4a08 HH |
6140 | _cleanup_free_ char *cmdline_init = NULL; |
6141 | const char *root, *init; | |
4fbd7192 | 6142 | sd_bus *bus; |
f459b602 | 6143 | int r; |
957eb8ca | 6144 | |
4fbd7192 LP |
6145 | if (arg_transport != BUS_TRANSPORT_LOCAL) { |
6146 | log_error("Cannot switch root remotely."); | |
6147 | return -EINVAL; | |
6148 | } | |
6149 | ||
e449de87 | 6150 | if (argc < 2 || argc > 3) { |
957eb8ca LP |
6151 | log_error("Wrong number of arguments."); |
6152 | return -EINVAL; | |
6153 | } | |
6154 | ||
e449de87 | 6155 | root = argv[1]; |
13068da8 | 6156 | |
e449de87 LP |
6157 | if (argc >= 3) |
6158 | init = argv[2]; | |
13068da8 | 6159 | else { |
01771226 | 6160 | r = proc_cmdline_get_key("init", 0, &cmdline_init); |
f39d4a08 | 6161 | if (r < 0) |
da927ba9 | 6162 | log_debug_errno(r, "Failed to parse /proc/cmdline: %m"); |
13068da8 | 6163 | |
f39d4a08 | 6164 | init = cmdline_init; |
13068da8 | 6165 | } |
f459b602 | 6166 | |
3c6f7c34 | 6167 | init = empty_to_null(init); |
f39d4a08 HH |
6168 | if (init) { |
6169 | const char *root_systemd_path = NULL, *root_init_path = NULL; | |
6170 | ||
63c372cb LP |
6171 | root_systemd_path = strjoina(root, "/" SYSTEMD_BINARY_PATH); |
6172 | root_init_path = strjoina(root, "/", init); | |
f39d4a08 HH |
6173 | |
6174 | /* If the passed init is actually the same as the | |
6175 | * systemd binary, then let's suppress it. */ | |
e3f791a2 | 6176 | if (files_same(root_init_path, root_systemd_path, 0) > 0) |
f39d4a08 HH |
6177 | init = NULL; |
6178 | } | |
13068da8 | 6179 | |
acc28e2e | 6180 | /* Instruct PID1 to exclude us from its killing spree applied during |
b3ad0ff4 ZJS |
6181 | * the transition. Otherwise we would exit with a failure status even |
6182 | * though the switch to the new root has succeed. */ | |
6183 | argv_cmdline[0] = '@'; | |
acc28e2e | 6184 | |
4fbd7192 LP |
6185 | r = acquire_bus(BUS_MANAGER, &bus); |
6186 | if (r < 0) | |
6187 | return r; | |
6188 | ||
8a7a9cea ZJS |
6189 | /* If we are slow to exit after the root switch, the new systemd instance |
6190 | * will send us a signal to terminate. Just ignore it and exit normally. | |
6191 | * This way the unit does not end up as failed. | |
6192 | */ | |
6193 | r = ignore_signals(SIGTERM, -1); | |
6194 | if (r < 0) | |
6195 | log_warning_errno(r, "Failed to change disposition of SIGTERM to ignore: %m"); | |
6196 | ||
f39d4a08 | 6197 | log_debug("Switching root - root: %s; init: %s", root, strna(init)); |
957eb8ca | 6198 | |
f459b602 | 6199 | r = sd_bus_call_method( |
f22f08cd | 6200 | bus, |
957eb8ca LP |
6201 | "org.freedesktop.systemd1", |
6202 | "/org/freedesktop/systemd1", | |
6203 | "org.freedesktop.systemd1.Manager", | |
f22f08cd | 6204 | "SwitchRoot", |
f459b602 | 6205 | &error, |
f22f08cd | 6206 | NULL, |
f459b602 | 6207 | "ss", root, init); |
8a7a9cea ZJS |
6208 | if (r < 0) { |
6209 | (void) default_signals(SIGTERM, -1); | |
6210 | ||
691395d8 | 6211 | return log_error_errno(r, "Failed to switch root: %s", bus_error_message(&error, r)); |
8a7a9cea | 6212 | } |
f459b602 MAP |
6213 | |
6214 | return 0; | |
957eb8ca LP |
6215 | } |
6216 | ||
e449de87 | 6217 | static int set_environment(int argc, char *argv[], void *userdata) { |
4afd3348 LP |
6218 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
6219 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; | |
7e4249b9 | 6220 | const char *method; |
4fbd7192 | 6221 | sd_bus *bus; |
31e767f7 LP |
6222 | int r; |
6223 | ||
e449de87 LP |
6224 | assert(argc > 1); |
6225 | assert(argv); | |
7e4249b9 | 6226 | |
4fbd7192 LP |
6227 | r = acquire_bus(BUS_MANAGER, &bus); |
6228 | if (r < 0) | |
6229 | return r; | |
6230 | ||
8a4b13c5 | 6231 | polkit_agent_open_maybe(); |
d2ad7e1b | 6232 | |
e449de87 | 6233 | method = streq(argv[0], "set-environment") |
7e4249b9 LP |
6234 | ? "SetEnvironment" |
6235 | : "UnsetEnvironment"; | |
6236 | ||
f459b602 MAP |
6237 | r = sd_bus_message_new_method_call( |
6238 | bus, | |
151b9b96 | 6239 | &m, |
31e767f7 LP |
6240 | "org.freedesktop.systemd1", |
6241 | "/org/freedesktop/systemd1", | |
6242 | "org.freedesktop.systemd1.Manager", | |
151b9b96 | 6243 | method); |
f459b602 MAP |
6244 | if (r < 0) |
6245 | return bus_log_create_error(r); | |
7e4249b9 | 6246 | |
e449de87 | 6247 | r = sd_bus_message_append_strv(m, strv_skip(argv, 1)); |
31e767f7 | 6248 | if (r < 0) |
f459b602 | 6249 | return bus_log_create_error(r); |
7e4249b9 | 6250 | |
c49b30a2 | 6251 | r = sd_bus_call(bus, m, 0, &error, NULL); |
691395d8 LP |
6252 | if (r < 0) |
6253 | return log_error_errno(r, "Failed to set environment: %s", bus_error_message(&error, r)); | |
7e4249b9 | 6254 | |
f84190d8 | 6255 | return 0; |
7e4249b9 LP |
6256 | } |
6257 | ||
e449de87 | 6258 | static int import_environment(int argc, char *argv[], void *userdata) { |
4afd3348 LP |
6259 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
6260 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; | |
4fbd7192 | 6261 | sd_bus *bus; |
ac3efa8a LP |
6262 | int r; |
6263 | ||
4fbd7192 LP |
6264 | r = acquire_bus(BUS_MANAGER, &bus); |
6265 | if (r < 0) | |
6266 | return r; | |
6267 | ||
8a4b13c5 | 6268 | polkit_agent_open_maybe(); |
d2ad7e1b | 6269 | |
ac3efa8a LP |
6270 | r = sd_bus_message_new_method_call( |
6271 | bus, | |
151b9b96 | 6272 | &m, |
ac3efa8a LP |
6273 | "org.freedesktop.systemd1", |
6274 | "/org/freedesktop/systemd1", | |
6275 | "org.freedesktop.systemd1.Manager", | |
151b9b96 | 6276 | "SetEnvironment"); |
ac3efa8a LP |
6277 | if (r < 0) |
6278 | return bus_log_create_error(r); | |
6279 | ||
e449de87 | 6280 | if (argc < 2) |
ac3efa8a LP |
6281 | r = sd_bus_message_append_strv(m, environ); |
6282 | else { | |
6283 | char **a, **b; | |
6284 | ||
6285 | r = sd_bus_message_open_container(m, 'a', "s"); | |
6286 | if (r < 0) | |
6287 | return bus_log_create_error(r); | |
6288 | ||
e449de87 | 6289 | STRV_FOREACH(a, strv_skip(argv, 1)) { |
ac3efa8a LP |
6290 | |
6291 | if (!env_name_is_valid(*a)) { | |
6292 | log_error("Not a valid environment variable name: %s", *a); | |
6293 | return -EINVAL; | |
6294 | } | |
6295 | ||
6296 | STRV_FOREACH(b, environ) { | |
6297 | const char *eq; | |
6298 | ||
6299 | eq = startswith(*b, *a); | |
6300 | if (eq && *eq == '=') { | |
6301 | ||
6302 | r = sd_bus_message_append(m, "s", *b); | |
6303 | if (r < 0) | |
6304 | return bus_log_create_error(r); | |
6305 | ||
6306 | break; | |
6307 | } | |
6308 | } | |
6309 | } | |
6310 | ||
6311 | r = sd_bus_message_close_container(m); | |
6312 | } | |
6313 | if (r < 0) | |
6314 | return bus_log_create_error(r); | |
6315 | ||
6316 | r = sd_bus_call(bus, m, 0, &error, NULL); | |
691395d8 LP |
6317 | if (r < 0) |
6318 | return log_error_errno(r, "Failed to import environment: %s", bus_error_message(&error, r)); | |
ac3efa8a LP |
6319 | |
6320 | return 0; | |
6321 | } | |
6322 | ||
cbb13b2a | 6323 | static int enable_sysv_units(const char *verb, char **args) { |
729e3769 | 6324 | int r = 0; |
ee5762e3 | 6325 | |
349cc4a5 | 6326 | #if HAVE_SYSV_COMPAT |
8e766630 | 6327 | _cleanup_(lookup_paths_free) LookupPaths paths = {}; |
e735decc LP |
6328 | unsigned f = 0; |
6329 | ||
6330 | /* Processes all SysV units, and reshuffles the array so that afterwards only the native units remain */ | |
ee5762e3 | 6331 | |
729e3769 LP |
6332 | if (arg_scope != UNIT_FILE_SYSTEM) |
6333 | return 0; | |
ee5762e3 | 6334 | |
b41b9d2a LP |
6335 | if (getenv_bool("SYSTEMCTL_SKIP_SYSV") > 0) |
6336 | return 0; | |
6337 | ||
4c315c2c IS |
6338 | if (!STR_IN_SET(verb, |
6339 | "enable", | |
6340 | "disable", | |
6341 | "is-enabled")) | |
729e3769 | 6342 | return 0; |
ee5762e3 | 6343 | |
4943d143 | 6344 | r = lookup_paths_init(&paths, arg_scope, LOOKUP_PATHS_EXCLUDE_GENERATED, arg_root); |
729e3769 LP |
6345 | if (r < 0) |
6346 | return r; | |
ee5762e3 | 6347 | |
729e3769 | 6348 | r = 0; |
a644abed | 6349 | while (args[f]) { |
e735decc LP |
6350 | |
6351 | const char *argv[] = { | |
6352 | ROOTLIBEXECDIR "/systemd-sysv-install", | |
6353 | NULL, | |
6354 | NULL, | |
6355 | NULL, | |
6356 | NULL, | |
6357 | }; | |
6358 | ||
05cae7f3 | 6359 | _cleanup_free_ char *p = NULL, *q = NULL, *l = NULL; |
729e3769 | 6360 | bool found_native = false, found_sysv; |
e735decc | 6361 | const char *name; |
729e3769 | 6362 | unsigned c = 1; |
729e3769 | 6363 | pid_t pid; |
e735decc | 6364 | int j; |
ee5762e3 | 6365 | |
a644abed | 6366 | name = args[f++]; |
ee5762e3 | 6367 | |
729e3769 LP |
6368 | if (!endswith(name, ".service")) |
6369 | continue; | |
ee5762e3 | 6370 | |
729e3769 LP |
6371 | if (path_is_absolute(name)) |
6372 | continue; | |
ee5762e3 | 6373 | |
e735decc | 6374 | j = unit_file_exists(arg_scope, &paths, name); |
76ec966f | 6375 | if (j < 0 && !IN_SET(j, -ELOOP, -ERFKILL, -EADDRNOTAVAIL)) |
e735decc | 6376 | return log_error_errno(j, "Failed to lookup unit file state: %m"); |
3c6d8e57 | 6377 | found_native = j != 0; |
ee5762e3 | 6378 | |
e735decc LP |
6379 | /* If we have both a native unit and a SysV script, enable/disable them both (below); for is-enabled, |
6380 | * prefer the native unit */ | |
355ff449 | 6381 | if (found_native && streq(verb, "is-enabled")) |
729e3769 | 6382 | continue; |
ee5762e3 | 6383 | |
652ef298 | 6384 | p = path_join(arg_root, SYSTEM_SYSVINIT_PATH, name); |
0c6ea3a4 | 6385 | if (!p) |
60731f32 | 6386 | return log_oom(); |
ee5762e3 | 6387 | |
fbd0b64f | 6388 | p[strlen(p) - STRLEN(".service")] = 0; |
729e3769 | 6389 | found_sysv = access(p, F_OK) >= 0; |
4b6756a8 | 6390 | if (!found_sysv) |
729e3769 | 6391 | continue; |
71fad675 | 6392 | |
689e4e6a CR |
6393 | if (!arg_quiet) { |
6394 | if (found_native) | |
6395 | log_info("Synchronizing state of %s with SysV service script with %s.", name, argv[0]); | |
6396 | else | |
6397 | log_info("%s is not a native service, redirecting to systemd-sysv-install.", name); | |
6398 | } | |
ee5762e3 | 6399 | |
729e3769 LP |
6400 | if (!isempty(arg_root)) |
6401 | argv[c++] = q = strappend("--root=", arg_root); | |
ee5762e3 | 6402 | |
0f0467e6 | 6403 | argv[c++] = verb; |
2b6bf07d | 6404 | argv[c++] = basename(p); |
729e3769 | 6405 | argv[c] = NULL; |
ee5762e3 | 6406 | |
729e3769 | 6407 | l = strv_join((char**)argv, " "); |
60731f32 ZJS |
6408 | if (!l) |
6409 | return log_oom(); | |
ee5762e3 | 6410 | |
8ecc68f4 LP |
6411 | if (!arg_quiet) |
6412 | log_info("Executing: %s", l); | |
ee5762e3 | 6413 | |
0672e2c6 | 6414 | j = safe_fork("(sysv-install)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG, &pid); |
4c253ed1 | 6415 | if (j < 0) |
b6e1fff1 | 6416 | return j; |
4c253ed1 | 6417 | if (j == 0) { |
729e3769 | 6418 | /* Child */ |
729e3769 | 6419 | execv(argv[0], (char**) argv); |
cc7cb0ca | 6420 | log_error_errno(errno, "Failed to execute %s: %m", argv[0]); |
729e3769 LP |
6421 | _exit(EXIT_FAILURE); |
6422 | } | |
ee5762e3 | 6423 | |
2e87a1fd | 6424 | j = wait_for_terminate_and_check("sysv-install", pid, WAIT_LOG_ABNORMAL); |
d710aaf7 | 6425 | if (j < 0) |
2e87a1fd LP |
6426 | return j; |
6427 | if (streq(verb, "is-enabled")) { | |
b4a34311 | 6428 | if (j == EXIT_SUCCESS) { |
2e87a1fd LP |
6429 | if (!arg_quiet) |
6430 | puts("enabled"); | |
6431 | r = 1; | |
6432 | } else { | |
6433 | if (!arg_quiet) | |
6434 | puts("disabled"); | |
6435 | } | |
ee5762e3 | 6436 | |
b4a34311 | 6437 | } else if (j != EXIT_SUCCESS) |
2e87a1fd | 6438 | return -EBADE; /* We don't warn here, under the assumption the script already showed an explanation */ |
ee5762e3 | 6439 | |
355ff449 MP |
6440 | if (found_native) |
6441 | continue; | |
6442 | ||
a644abed | 6443 | /* Remove this entry, so that we don't try enabling it as native unit */ |
aba84331 LP |
6444 | assert(f > 0); |
6445 | f--; | |
6446 | assert(args[f] == name); | |
6447 | strv_remove(args, name); | |
729e3769 | 6448 | } |
ee5762e3 | 6449 | |
729e3769 LP |
6450 | #endif |
6451 | return r; | |
6452 | } | |
ee5762e3 | 6453 | |
37370d0c | 6454 | static int mangle_names(char **original_names, char ***mangled_names) { |
a33fdebb | 6455 | char **i, **l, **name; |
7410616c | 6456 | int r; |
37370d0c | 6457 | |
7410616c | 6458 | l = i = new(char*, strv_length(original_names) + 1); |
a33fdebb | 6459 | if (!l) |
37370d0c VP |
6460 | return log_oom(); |
6461 | ||
37370d0c | 6462 | STRV_FOREACH(name, original_names) { |
44386fc1 LN |
6463 | |
6464 | /* When enabling units qualified path names are OK, | |
6465 | * too, hence allow them explicitly. */ | |
6466 | ||
7410616c | 6467 | if (is_path(*name)) { |
44386fc1 | 6468 | *i = strdup(*name); |
7410616c LP |
6469 | if (!*i) { |
6470 | strv_free(l); | |
6471 | return log_oom(); | |
6472 | } | |
6473 | } else { | |
37cbc1d5 | 6474 | r = unit_name_mangle(*name, arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN, i); |
7410616c | 6475 | if (r < 0) { |
523f8cde | 6476 | *i = NULL; |
7410616c LP |
6477 | strv_free(l); |
6478 | return log_error_errno(r, "Failed to mangle unit name: %m"); | |
6479 | } | |
a33fdebb LP |
6480 | } |
6481 | ||
6482 | i++; | |
37370d0c | 6483 | } |
a33fdebb LP |
6484 | |
6485 | *i = NULL; | |
6486 | *mangled_names = l; | |
37370d0c VP |
6487 | |
6488 | return 0; | |
6489 | } | |
6490 | ||
decd7982 JR |
6491 | static int normalize_filenames(char **names) { |
6492 | char **u; | |
6493 | int r; | |
6494 | ||
6495 | STRV_FOREACH(u, names) | |
6496 | if (!path_is_absolute(*u)) { | |
6497 | char* normalized_path; | |
6498 | ||
baaa35ad ZJS |
6499 | if (!isempty(arg_root)) |
6500 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
6501 | "Non-absolute paths are not allowed when --root is used: %s", | |
6502 | *u); | |
decd7982 | 6503 | |
baaa35ad ZJS |
6504 | if (!strchr(*u,'/')) |
6505 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
6506 | "Link argument does contain at least one directory separator: %s", | |
6507 | *u); | |
decd7982 JR |
6508 | |
6509 | r = path_make_absolute_cwd(*u, &normalized_path); | |
6510 | if (r < 0) | |
6511 | return r; | |
6512 | ||
6513 | free_and_replace(*u, normalized_path); | |
6514 | } | |
6515 | ||
6516 | return 0; | |
6517 | } | |
6518 | ||
1d3c86c0 MS |
6519 | static int normalize_names(char **names, bool warn_if_path) { |
6520 | char **u; | |
6521 | bool was_path = false; | |
6522 | ||
6523 | STRV_FOREACH(u, names) { | |
6524 | int r; | |
6525 | ||
6526 | if (!is_path(*u)) | |
6527 | continue; | |
6528 | ||
6529 | r = free_and_strdup(u, basename(*u)); | |
6530 | if (r < 0) | |
6531 | return log_error_errno(r, "Failed to normalize unit file path: %m"); | |
6532 | ||
6533 | was_path = true; | |
6534 | } | |
6535 | ||
6536 | if (warn_if_path && was_path) | |
6537 | log_warning("Warning: Can't execute disable on the unit file path. Proceeding with the unit name."); | |
6538 | ||
6539 | return 0; | |
6540 | } | |
6541 | ||
173471b7 | 6542 | static int unit_exists(LookupPaths *lp, const char *unit) { |
d6568222 | 6543 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
f37f8a61 | 6544 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
d6568222 SS |
6545 | _cleanup_free_ char *path = NULL; |
6546 | static const struct bus_properties_map property_map[] = { | |
f37f8a61 YW |
6547 | { "LoadState", "s", NULL, offsetof(UnitStatusInfo, load_state) }, |
6548 | { "ActiveState", "s", NULL, offsetof(UnitStatusInfo, active_state)}, | |
d6568222 SS |
6549 | {}, |
6550 | }; | |
6551 | UnitStatusInfo info = {}; | |
6552 | sd_bus *bus; | |
6553 | int r; | |
6554 | ||
173471b7 ZJS |
6555 | if (unit_name_is_valid(unit, UNIT_NAME_TEMPLATE)) |
6556 | return unit_find_template_path(unit, lp, NULL, NULL); | |
6557 | ||
d6568222 SS |
6558 | path = unit_dbus_path_from_name(unit); |
6559 | if (!path) | |
6560 | return log_oom(); | |
6561 | ||
6562 | r = acquire_bus(BUS_MANAGER, &bus); | |
6563 | if (r < 0) | |
6564 | return r; | |
6565 | ||
a7e4861c | 6566 | r = bus_map_all_properties(bus, "org.freedesktop.systemd1", path, property_map, 0, &error, &m, &info); |
d6568222 SS |
6567 | if (r < 0) |
6568 | return log_error_errno(r, "Failed to get properties: %s", bus_error_message(&error, r)); | |
6569 | ||
d6568222 SS |
6570 | return !streq_ptr(info.load_state, "not-found") || !streq_ptr(info.active_state, "inactive"); |
6571 | } | |
6572 | ||
e449de87 | 6573 | static int enable_unit(int argc, char *argv[], void *userdata) { |
e3e0314b | 6574 | _cleanup_strv_free_ char **names = NULL; |
e449de87 | 6575 | const char *verb = argv[0]; |
729e3769 | 6576 | UnitFileChange *changes = NULL; |
da6053d0 | 6577 | size_t n_changes = 0; |
729e3769 | 6578 | int carries_install_info = -1; |
35b132e8 | 6579 | bool ignore_carries_install_info = arg_quiet; |
729e3769 | 6580 | int r; |
ee5762e3 | 6581 | |
e449de87 | 6582 | if (!argv[1]) |
ab5919fa MS |
6583 | return 0; |
6584 | ||
e449de87 | 6585 | r = mangle_names(strv_skip(argv, 1), &names); |
3a05c0f9 | 6586 | if (r < 0) |
cbb13b2a VP |
6587 | return r; |
6588 | ||
e3e0314b | 6589 | r = enable_sysv_units(verb, names); |
cbb13b2a VP |
6590 | if (r < 0) |
6591 | return r; | |
3a05c0f9 | 6592 | |
c61b443d | 6593 | /* If the operation was fully executed by the SysV compat, let's finish early */ |
823e5fab FB |
6594 | if (strv_isempty(names)) { |
6595 | if (arg_no_reload || install_client_side()) | |
6596 | return 0; | |
6597 | return daemon_reload(argc, argv, userdata); | |
6598 | } | |
67d66210 | 6599 | |
1d3c86c0 MS |
6600 | if (streq(verb, "disable")) { |
6601 | r = normalize_names(names, true); | |
6602 | if (r < 0) | |
6603 | return r; | |
6604 | } | |
6605 | ||
decd7982 JR |
6606 | if (streq(verb, "link")) { |
6607 | r = normalize_filenames(names); | |
6608 | if (r < 0) | |
6609 | return r; | |
6610 | } | |
6611 | ||
4fbd7192 | 6612 | if (install_client_side()) { |
b3796dd8 JS |
6613 | UnitFileFlags flags; |
6614 | ||
6615 | flags = args_to_flags(); | |
729e3769 | 6616 | if (streq(verb, "enable")) { |
b3796dd8 | 6617 | r = unit_file_enable(arg_scope, flags, arg_root, names, &changes, &n_changes); |
729e3769 LP |
6618 | carries_install_info = r; |
6619 | } else if (streq(verb, "disable")) | |
b3796dd8 | 6620 | r = unit_file_disable(arg_scope, flags, arg_root, names, &changes, &n_changes); |
729e3769 | 6621 | else if (streq(verb, "reenable")) { |
b3796dd8 | 6622 | r = unit_file_reenable(arg_scope, flags, arg_root, names, &changes, &n_changes); |
729e3769 LP |
6623 | carries_install_info = r; |
6624 | } else if (streq(verb, "link")) | |
b3796dd8 | 6625 | r = unit_file_link(arg_scope, flags, arg_root, names, &changes, &n_changes); |
729e3769 | 6626 | else if (streq(verb, "preset")) { |
b3796dd8 | 6627 | r = unit_file_preset(arg_scope, flags, arg_root, names, arg_preset_mode, &changes, &n_changes); |
729e3769 | 6628 | } else if (streq(verb, "mask")) |
b3796dd8 | 6629 | r = unit_file_mask(arg_scope, flags, arg_root, names, &changes, &n_changes); |
729e3769 | 6630 | else if (streq(verb, "unmask")) |
b3796dd8 | 6631 | r = unit_file_unmask(arg_scope, flags, arg_root, names, &changes, &n_changes); |
344ca755 LP |
6632 | else if (streq(verb, "revert")) |
6633 | r = unit_file_revert(arg_scope, arg_root, names, &changes, &n_changes); | |
729e3769 LP |
6634 | else |
6635 | assert_not_reached("Unknown verb"); | |
ee5762e3 | 6636 | |
af3d8113 | 6637 | unit_file_dump_changes(r, verb, changes, n_changes, arg_quiet); |
d073dea0 | 6638 | if (r < 0) |
85b78539 | 6639 | goto finish; |
df77cdf0 | 6640 | r = 0; |
729e3769 | 6641 | } else { |
4afd3348 LP |
6642 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL, *m = NULL; |
6643 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
39207373 | 6644 | bool expect_carries_install_info = false; |
344ca755 | 6645 | bool send_runtime = true, send_force = true, send_preset_mode = false; |
718db961 | 6646 | const char *method; |
4fbd7192 | 6647 | sd_bus *bus; |
729e3769 | 6648 | |
d6568222 | 6649 | if (STR_IN_SET(verb, "mask", "unmask")) { |
f8d6cb48 | 6650 | char **name; |
8e766630 | 6651 | _cleanup_(lookup_paths_free) LookupPaths lp = {}; |
173471b7 ZJS |
6652 | |
6653 | r = lookup_paths_init(&lp, arg_scope, 0, arg_root); | |
d6568222 SS |
6654 | if (r < 0) |
6655 | return r; | |
f8d6cb48 ZJS |
6656 | |
6657 | STRV_FOREACH(name, names) { | |
173471b7 | 6658 | r = unit_exists(&lp, *name); |
f8d6cb48 ZJS |
6659 | if (r < 0) |
6660 | return r; | |
6661 | if (r == 0) | |
25c59b5d | 6662 | log_notice("Unit %s does not exist, proceeding anyway.", *name); |
f8d6cb48 | 6663 | } |
d6568222 SS |
6664 | } |
6665 | ||
4fbd7192 LP |
6666 | r = acquire_bus(BUS_MANAGER, &bus); |
6667 | if (r < 0) | |
6668 | return r; | |
6669 | ||
8a4b13c5 | 6670 | polkit_agent_open_maybe(); |
d2ad7e1b | 6671 | |
729e3769 LP |
6672 | if (streq(verb, "enable")) { |
6673 | method = "EnableUnitFiles"; | |
6674 | expect_carries_install_info = true; | |
6675 | } else if (streq(verb, "disable")) { | |
6676 | method = "DisableUnitFiles"; | |
6677 | send_force = false; | |
6678 | } else if (streq(verb, "reenable")) { | |
6679 | method = "ReenableUnitFiles"; | |
6680 | expect_carries_install_info = true; | |
6681 | } else if (streq(verb, "link")) | |
6682 | method = "LinkUnitFiles"; | |
6683 | else if (streq(verb, "preset")) { | |
d309c1c3 LP |
6684 | |
6685 | if (arg_preset_mode != UNIT_FILE_PRESET_FULL) { | |
6686 | method = "PresetUnitFilesWithMode"; | |
6687 | send_preset_mode = true; | |
6688 | } else | |
6689 | method = "PresetUnitFiles"; | |
6690 | ||
729e3769 | 6691 | expect_carries_install_info = true; |
39207373 | 6692 | ignore_carries_install_info = true; |
729e3769 LP |
6693 | } else if (streq(verb, "mask")) |
6694 | method = "MaskUnitFiles"; | |
6695 | else if (streq(verb, "unmask")) { | |
6696 | method = "UnmaskUnitFiles"; | |
6697 | send_force = false; | |
344ca755 LP |
6698 | } else if (streq(verb, "revert")) { |
6699 | method = "RevertUnitFiles"; | |
6700 | send_runtime = send_force = false; | |
729e3769 LP |
6701 | } else |
6702 | assert_not_reached("Unknown verb"); | |
6703 | ||
f459b602 MAP |
6704 | r = sd_bus_message_new_method_call( |
6705 | bus, | |
151b9b96 | 6706 | &m, |
729e3769 LP |
6707 | "org.freedesktop.systemd1", |
6708 | "/org/freedesktop/systemd1", | |
6709 | "org.freedesktop.systemd1.Manager", | |
151b9b96 | 6710 | method); |
f459b602 MAP |
6711 | if (r < 0) |
6712 | return bus_log_create_error(r); | |
ee5762e3 | 6713 | |
e3e0314b | 6714 | r = sd_bus_message_append_strv(m, names); |
f459b602 MAP |
6715 | if (r < 0) |
6716 | return bus_log_create_error(r); | |
ee5762e3 | 6717 | |
d309c1c3 LP |
6718 | if (send_preset_mode) { |
6719 | r = sd_bus_message_append(m, "s", unit_file_preset_mode_to_string(arg_preset_mode)); | |
6720 | if (r < 0) | |
6721 | return bus_log_create_error(r); | |
6722 | } | |
6723 | ||
344ca755 LP |
6724 | if (send_runtime) { |
6725 | r = sd_bus_message_append(m, "b", arg_runtime); | |
6726 | if (r < 0) | |
6727 | return bus_log_create_error(r); | |
6728 | } | |
ee5762e3 | 6729 | |
729e3769 | 6730 | if (send_force) { |
f459b602 MAP |
6731 | r = sd_bus_message_append(m, "b", arg_force); |
6732 | if (r < 0) | |
6733 | return bus_log_create_error(r); | |
ee5762e3 LP |
6734 | } |
6735 | ||
c49b30a2 | 6736 | r = sd_bus_call(bus, m, 0, &error, &reply); |
691395d8 | 6737 | if (r < 0) |
af3d8113 | 6738 | return log_error_errno(r, "Failed to %s unit: %s", verb, bus_error_message(&error, r)); |
be394c48 | 6739 | |
729e3769 | 6740 | if (expect_carries_install_info) { |
f459b602 MAP |
6741 | r = sd_bus_message_read(reply, "b", &carries_install_info); |
6742 | if (r < 0) | |
6743 | return bus_log_parse_error(r); | |
ee5762e3 LP |
6744 | } |
6745 | ||
57ab2eab | 6746 | r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet, &changes, &n_changes); |
f459b602 | 6747 | if (r < 0) |
85b78539 | 6748 | goto finish; |
b77398f7 | 6749 | |
93c941e3 | 6750 | /* Try to reload if enabled */ |
d6cb60c7 | 6751 | if (!arg_no_reload) |
e449de87 | 6752 | r = daemon_reload(argc, argv, userdata); |
f459b602 MAP |
6753 | else |
6754 | r = 0; | |
b647f10d | 6755 | } |
3d3961f2 | 6756 | |
39207373 | 6757 | if (carries_install_info == 0 && !ignore_carries_install_info) |
46efc978 LP |
6758 | log_notice("The unit files have no installation config (WantedBy=, RequiredBy=, Also=,\n" |
6759 | "Alias= settings in the [Install] section, and DefaultInstance= for template\n" | |
2811184a ZJS |
6760 | "units). This means they are not meant to be enabled using systemctl.\n" |
6761 | " \n" /* trick: the space is needed so that the line does not get stripped from output */ | |
46efc978 | 6762 | "Possible reasons for having this kind of units are:\n" |
18c93ee3 LP |
6763 | "%1$s A unit may be statically enabled by being symlinked from another unit's\n" |
6764 | " .wants/ or .requires/ directory.\n" | |
6765 | "%1$s A unit's purpose may be to act as a helper for some other unit which has\n" | |
6766 | " a requirement dependency on it.\n" | |
6767 | "%1$s A unit may be started when needed via activation (socket, path, timer,\n" | |
6768 | " D-Bus, udev, scripted systemctl call, ...).\n" | |
6769 | "%1$s In case of template units, the unit is meant to be enabled with some\n" | |
6770 | " instance name specified.", | |
9a6f746f | 6771 | special_glyph(SPECIAL_GLYPH_BULLET)); |
ee5762e3 | 6772 | |
6bc30691 | 6773 | if (arg_now && STR_IN_SET(argv[0], "enable", "disable", "mask")) { |
4fbd7192 | 6774 | sd_bus *bus; |
da6053d0 | 6775 | size_t len, i; |
57ab2eab | 6776 | |
4fbd7192 LP |
6777 | r = acquire_bus(BUS_MANAGER, &bus); |
6778 | if (r < 0) | |
d073dea0 | 6779 | goto finish; |
4fbd7192 | 6780 | |
6bc30691 JS |
6781 | len = strv_length(names); |
6782 | { | |
6783 | char *new_args[len + 2]; | |
57ab2eab | 6784 | |
6bc30691 JS |
6785 | new_args[0] = (char*) (streq(argv[0], "enable") ? "start" : "stop"); |
6786 | for (i = 0; i < len; i++) | |
6787 | new_args[i + 1] = basename(names[i]); | |
6788 | new_args[i + 1] = NULL; | |
6789 | ||
6790 | r = start_unit(len + 1, new_args, userdata); | |
6791 | } | |
57ab2eab JS |
6792 | } |
6793 | ||
729e3769 | 6794 | finish: |
729e3769 | 6795 | unit_file_changes_free(changes, n_changes); |
ee5762e3 | 6796 | |
729e3769 | 6797 | return r; |
ee5762e3 LP |
6798 | } |
6799 | ||
e449de87 | 6800 | static int add_dependency(int argc, char *argv[], void *userdata) { |
e94937df LN |
6801 | _cleanup_strv_free_ char **names = NULL; |
6802 | _cleanup_free_ char *target = NULL; | |
e449de87 | 6803 | const char *verb = argv[0]; |
acc0269c | 6804 | UnitFileChange *changes = NULL; |
da6053d0 | 6805 | size_t n_changes = 0; |
e94937df LN |
6806 | UnitDependency dep; |
6807 | int r = 0; | |
6808 | ||
e449de87 | 6809 | if (!argv[1]) |
e94937df LN |
6810 | return 0; |
6811 | ||
37cbc1d5 | 6812 | r = unit_name_mangle_with_suffix(argv[1], arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN, ".target", &target); |
7410616c LP |
6813 | if (r < 0) |
6814 | return log_error_errno(r, "Failed to mangle unit name: %m"); | |
e94937df | 6815 | |
e449de87 | 6816 | r = mangle_names(strv_skip(argv, 2), &names); |
e94937df LN |
6817 | if (r < 0) |
6818 | return r; | |
6819 | ||
6820 | if (streq(verb, "add-wants")) | |
6821 | dep = UNIT_WANTS; | |
6822 | else if (streq(verb, "add-requires")) | |
6823 | dep = UNIT_REQUIRES; | |
6824 | else | |
6825 | assert_not_reached("Unknown verb"); | |
6826 | ||
4fbd7192 | 6827 | if (install_client_side()) { |
b3796dd8 | 6828 | r = unit_file_add_dependency(arg_scope, args_to_flags(), arg_root, names, target, dep, &changes, &n_changes); |
af3d8113 | 6829 | unit_file_dump_changes(r, "add dependency on", changes, n_changes, arg_quiet); |
5f056378 CH |
6830 | |
6831 | if (r > 0) | |
6832 | r = 0; | |
e94937df | 6833 | } else { |
4afd3348 LP |
6834 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL, *m = NULL; |
6835 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
4fbd7192 | 6836 | sd_bus *bus; |
e94937df | 6837 | |
4fbd7192 LP |
6838 | r = acquire_bus(BUS_MANAGER, &bus); |
6839 | if (r < 0) | |
6840 | return r; | |
6841 | ||
8a4b13c5 | 6842 | polkit_agent_open_maybe(); |
d2ad7e1b | 6843 | |
e94937df LN |
6844 | r = sd_bus_message_new_method_call( |
6845 | bus, | |
6846 | &m, | |
6847 | "org.freedesktop.systemd1", | |
6848 | "/org/freedesktop/systemd1", | |
6849 | "org.freedesktop.systemd1.Manager", | |
6850 | "AddDependencyUnitFiles"); | |
6851 | if (r < 0) | |
6852 | return bus_log_create_error(r); | |
6853 | ||
342641fb | 6854 | r = sd_bus_message_append_strv(m, names); |
e94937df LN |
6855 | if (r < 0) |
6856 | return bus_log_create_error(r); | |
6857 | ||
342641fb | 6858 | r = sd_bus_message_append(m, "ssbb", target, unit_dependency_to_string(dep), arg_runtime, arg_force); |
e94937df LN |
6859 | if (r < 0) |
6860 | return bus_log_create_error(r); | |
6861 | ||
6862 | r = sd_bus_call(bus, m, 0, &error, &reply); | |
691395d8 | 6863 | if (r < 0) |
af3d8113 | 6864 | return log_error_errno(r, "Failed to add dependency: %s", bus_error_message(&error, r)); |
e94937df | 6865 | |
acc0269c | 6866 | r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet, &changes, &n_changes); |
e94937df | 6867 | if (r < 0) |
acc0269c | 6868 | goto finish; |
e94937df | 6869 | |
acc0269c CH |
6870 | if (arg_no_reload) { |
6871 | r = 0; | |
6872 | goto finish; | |
6873 | } | |
6874 | ||
6875 | r = daemon_reload(argc, argv, userdata); | |
e94937df | 6876 | } |
acc0269c CH |
6877 | |
6878 | finish: | |
6879 | unit_file_changes_free(changes, n_changes); | |
6880 | ||
6881 | return r; | |
e94937df LN |
6882 | } |
6883 | ||
e449de87 | 6884 | static int preset_all(int argc, char *argv[], void *userdata) { |
acc0269c | 6885 | UnitFileChange *changes = NULL; |
da6053d0 | 6886 | size_t n_changes = 0; |
d309c1c3 LP |
6887 | int r; |
6888 | ||
4fbd7192 | 6889 | if (install_client_side()) { |
b3796dd8 | 6890 | r = unit_file_preset_all(arg_scope, args_to_flags(), arg_root, arg_preset_mode, &changes, &n_changes); |
af3d8113 | 6891 | unit_file_dump_changes(r, "preset", changes, n_changes, arg_quiet); |
5f056378 CH |
6892 | |
6893 | if (r > 0) | |
6894 | r = 0; | |
d309c1c3 | 6895 | } else { |
4afd3348 LP |
6896 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
6897 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
4fbd7192 | 6898 | sd_bus *bus; |
d309c1c3 | 6899 | |
4fbd7192 LP |
6900 | r = acquire_bus(BUS_MANAGER, &bus); |
6901 | if (r < 0) | |
6902 | return r; | |
6903 | ||
8a4b13c5 | 6904 | polkit_agent_open_maybe(); |
d2ad7e1b | 6905 | |
6e646d22 | 6906 | r = sd_bus_call_method( |
d309c1c3 LP |
6907 | bus, |
6908 | "org.freedesktop.systemd1", | |
6909 | "/org/freedesktop/systemd1", | |
6910 | "org.freedesktop.systemd1.Manager", | |
6e646d22 LP |
6911 | "PresetAllUnitFiles", |
6912 | &error, | |
6913 | &reply, | |
d309c1c3 LP |
6914 | "sbb", |
6915 | unit_file_preset_mode_to_string(arg_preset_mode), | |
6916 | arg_runtime, | |
6917 | arg_force); | |
691395d8 | 6918 | if (r < 0) |
af3d8113 | 6919 | return log_error_errno(r, "Failed to preset all units: %s", bus_error_message(&error, r)); |
d309c1c3 | 6920 | |
acc0269c | 6921 | r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet, &changes, &n_changes); |
d309c1c3 | 6922 | if (r < 0) |
acc0269c | 6923 | goto finish; |
d309c1c3 | 6924 | |
acc0269c CH |
6925 | if (arg_no_reload) { |
6926 | r = 0; | |
6927 | goto finish; | |
6928 | } | |
6929 | ||
6930 | r = daemon_reload(argc, argv, userdata); | |
d309c1c3 | 6931 | } |
acc0269c CH |
6932 | |
6933 | finish: | |
6934 | unit_file_changes_free(changes, n_changes); | |
6935 | ||
6936 | return r; | |
d309c1c3 LP |
6937 | } |
6938 | ||
3b3557c4 JS |
6939 | static int show_installation_targets_client_side(const char *name) { |
6940 | UnitFileChange *changes = NULL; | |
da6053d0 | 6941 | size_t n_changes = 0, i; |
3b3557c4 JS |
6942 | UnitFileFlags flags; |
6943 | char **p; | |
6944 | int r; | |
6945 | ||
6946 | p = STRV_MAKE(name); | |
6947 | flags = UNIT_FILE_DRY_RUN | | |
6948 | (arg_runtime ? UNIT_FILE_RUNTIME : 0); | |
6949 | ||
6950 | r = unit_file_disable(UNIT_FILE_SYSTEM, flags, NULL, p, &changes, &n_changes); | |
6951 | if (r < 0) | |
6952 | return log_error_errno(r, "Failed to get file links for %s: %m", name); | |
6953 | ||
6954 | for (i = 0; i < n_changes; i++) | |
6955 | if (changes[i].type == UNIT_FILE_UNLINK) | |
6956 | printf(" %s\n", changes[i].path); | |
6957 | ||
6958 | return 0; | |
6959 | } | |
6960 | ||
6961 | static int show_installation_targets(sd_bus *bus, const char *name) { | |
6962 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
6963 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
6964 | const char *link; | |
6965 | int r; | |
6966 | ||
6967 | r = sd_bus_call_method( | |
6968 | bus, | |
6969 | "org.freedesktop.systemd1", | |
6970 | "/org/freedesktop/systemd1", | |
6971 | "org.freedesktop.systemd1.Manager", | |
6972 | "GetUnitFileLinks", | |
6973 | &error, | |
6974 | &reply, | |
6975 | "sb", name, arg_runtime); | |
6976 | if (r < 0) | |
6977 | return log_error_errno(r, "Failed to get unit file links for %s: %s", name, bus_error_message(&error, r)); | |
6978 | ||
6979 | r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "s"); | |
6980 | if (r < 0) | |
6981 | return bus_log_parse_error(r); | |
6982 | ||
6983 | while ((r = sd_bus_message_read(reply, "s", &link)) > 0) | |
6984 | printf(" %s\n", link); | |
6985 | ||
6986 | if (r < 0) | |
6987 | return bus_log_parse_error(r); | |
6988 | ||
6989 | r = sd_bus_message_exit_container(reply); | |
6990 | if (r < 0) | |
6991 | return bus_log_parse_error(r); | |
6992 | ||
6993 | return 0; | |
6994 | } | |
6995 | ||
e449de87 | 6996 | static int unit_is_enabled(int argc, char *argv[], void *userdata) { |
e3e0314b | 6997 | _cleanup_strv_free_ char **names = NULL; |
729e3769 LP |
6998 | bool enabled; |
6999 | char **name; | |
f459b602 | 7000 | int r; |
ee5762e3 | 7001 | |
e449de87 | 7002 | r = mangle_names(strv_skip(argv, 1), &names); |
cbb13b2a VP |
7003 | if (r < 0) |
7004 | return r; | |
7005 | ||
e449de87 | 7006 | r = enable_sysv_units(argv[0], names); |
729e3769 LP |
7007 | if (r < 0) |
7008 | return r; | |
ee5762e3 | 7009 | |
729e3769 | 7010 | enabled = r > 0; |
ee5762e3 | 7011 | |
4fbd7192 | 7012 | if (install_client_side()) { |
e3e0314b | 7013 | STRV_FOREACH(name, names) { |
729e3769 | 7014 | UnitFileState state; |
ee5762e3 | 7015 | |
0ec0deaa LP |
7016 | r = unit_file_get_state(arg_scope, arg_root, *name, &state); |
7017 | if (r < 0) | |
c5024cd0 | 7018 | return log_error_errno(r, "Failed to get unit file state for %s: %m", *name); |
ee5762e3 | 7019 | |
4c315c2c IS |
7020 | if (IN_SET(state, |
7021 | UNIT_FILE_ENABLED, | |
7022 | UNIT_FILE_ENABLED_RUNTIME, | |
7023 | UNIT_FILE_STATIC, | |
f4139308 LP |
7024 | UNIT_FILE_INDIRECT, |
7025 | UNIT_FILE_GENERATED)) | |
729e3769 LP |
7026 | enabled = true; |
7027 | ||
3b3557c4 | 7028 | if (!arg_quiet) { |
729e3769 | 7029 | puts(unit_file_state_to_string(state)); |
3b3557c4 JS |
7030 | if (arg_full) { |
7031 | r = show_installation_targets_client_side(*name); | |
7032 | if (r < 0) | |
7033 | return r; | |
7034 | } | |
7035 | } | |
71fad675 | 7036 | } |
ee5762e3 | 7037 | |
5f056378 | 7038 | r = 0; |
729e3769 | 7039 | } else { |
4afd3348 | 7040 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
4fbd7192 LP |
7041 | sd_bus *bus; |
7042 | ||
7043 | r = acquire_bus(BUS_MANAGER, &bus); | |
7044 | if (r < 0) | |
7045 | return r; | |
7046 | ||
e3e0314b | 7047 | STRV_FOREACH(name, names) { |
4afd3348 | 7048 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
729e3769 | 7049 | const char *s; |
63a723f3 | 7050 | |
f459b602 | 7051 | r = sd_bus_call_method( |
f22f08cd | 7052 | bus, |
729e3769 LP |
7053 | "org.freedesktop.systemd1", |
7054 | "/org/freedesktop/systemd1", | |
7055 | "org.freedesktop.systemd1.Manager", | |
f22f08cd | 7056 | "GetUnitFileState", |
f459b602 | 7057 | &error, |
f22f08cd | 7058 | &reply, |
04504f93 | 7059 | "s", *name); |
691395d8 LP |
7060 | if (r < 0) |
7061 | return log_error_errno(r, "Failed to get unit file state for %s: %s", *name, bus_error_message(&error, r)); | |
ee5762e3 | 7062 | |
f459b602 MAP |
7063 | r = sd_bus_message_read(reply, "s", &s); |
7064 | if (r < 0) | |
7065 | return bus_log_parse_error(r); | |
ee5762e3 | 7066 | |
f4139308 | 7067 | if (STR_IN_SET(s, "enabled", "enabled-runtime", "static", "indirect", "generated")) |
729e3769 LP |
7068 | enabled = true; |
7069 | ||
3b3557c4 | 7070 | if (!arg_quiet) { |
729e3769 | 7071 | puts(s); |
3b3557c4 JS |
7072 | if (arg_full) { |
7073 | r = show_installation_targets(bus, *name); | |
7074 | if (r < 0) | |
7075 | return r; | |
7076 | } | |
7077 | } | |
560d8f23 | 7078 | } |
ee5762e3 LP |
7079 | } |
7080 | ||
f4139308 | 7081 | return enabled ? EXIT_SUCCESS : EXIT_FAILURE; |
ee5762e3 LP |
7082 | } |
7083 | ||
adb6cd9b FB |
7084 | static int match_startup_finished(sd_bus_message *m, void *userdata, sd_bus_error *error) { |
7085 | char **state = userdata; | |
7086 | int r; | |
7087 | ||
7088 | assert(state); | |
7089 | ||
7090 | r = sd_bus_get_property_string( | |
7091 | sd_bus_message_get_bus(m), | |
7092 | "org.freedesktop.systemd1", | |
7093 | "/org/freedesktop/systemd1", | |
7094 | "org.freedesktop.systemd1.Manager", | |
7095 | "SystemState", | |
7096 | NULL, | |
7097 | state); | |
7098 | ||
7099 | sd_event_exit(sd_bus_get_event(sd_bus_message_get_bus(m)), r); | |
7100 | return 0; | |
7101 | } | |
7102 | ||
e449de87 | 7103 | static int is_system_running(int argc, char *argv[], void *userdata) { |
e686a616 | 7104 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
adb6cd9b FB |
7105 | _cleanup_(sd_bus_slot_unrefp) sd_bus_slot *slot_startup_finished = NULL; |
7106 | _cleanup_(sd_event_unrefp) sd_event* event = NULL; | |
99813a19 | 7107 | _cleanup_free_ char *state = NULL; |
4fbd7192 | 7108 | sd_bus *bus; |
99813a19 LP |
7109 | int r; |
7110 | ||
040524b4 | 7111 | if (running_in_chroot() > 0 || (arg_transport == BUS_TRANSPORT_LOCAL && !sd_booted())) { |
94f099d8 LP |
7112 | if (!arg_quiet) |
7113 | puts("offline"); | |
7114 | return EXIT_FAILURE; | |
7115 | } | |
7116 | ||
4fbd7192 LP |
7117 | r = acquire_bus(BUS_MANAGER, &bus); |
7118 | if (r < 0) | |
7119 | return r; | |
7120 | ||
adb6cd9b FB |
7121 | if (arg_wait) { |
7122 | r = sd_event_default(&event); | |
7123 | if (r >= 0) | |
7124 | r = sd_bus_attach_event(bus, event, 0); | |
7125 | if (r >= 0) | |
7126 | r = sd_bus_match_signal_async( | |
7127 | bus, | |
7128 | &slot_startup_finished, | |
7129 | "org.freedesktop.systemd1", | |
7130 | "/org/freedesktop/systemd1", | |
7131 | "org.freedesktop.systemd1.Manager", | |
7132 | "StartupFinished", | |
7133 | match_startup_finished, NULL, &state); | |
7134 | if (r < 0) { | |
7135 | log_warning_errno(r, "Failed to request match for StartupFinished: %m"); | |
7136 | arg_wait = false; | |
7137 | } | |
7138 | } | |
7139 | ||
99813a19 LP |
7140 | r = sd_bus_get_property_string( |
7141 | bus, | |
7142 | "org.freedesktop.systemd1", | |
7143 | "/org/freedesktop/systemd1", | |
7144 | "org.freedesktop.systemd1.Manager", | |
7145 | "SystemState", | |
e686a616 | 7146 | &error, |
99813a19 LP |
7147 | &state); |
7148 | if (r < 0) { | |
adb6cd9b | 7149 | log_warning_errno(r, "Failed to query system state: %s", bus_error_message(&error, r)); |
e686a616 | 7150 | |
99813a19 LP |
7151 | if (!arg_quiet) |
7152 | puts("unknown"); | |
bfeec178 | 7153 | return EXIT_FAILURE; |
99813a19 LP |
7154 | } |
7155 | ||
adb6cd9b FB |
7156 | if (arg_wait && STR_IN_SET(state, "initializing", "starting")) { |
7157 | r = sd_event_loop(event); | |
7158 | if (r < 0) { | |
7159 | log_warning_errno(r, "Failed to get property from event loop: %m"); | |
7160 | if (!arg_quiet) | |
7161 | puts("unknown"); | |
7162 | return EXIT_FAILURE; | |
7163 | } | |
7164 | } | |
7165 | ||
99813a19 LP |
7166 | if (!arg_quiet) |
7167 | puts(state); | |
7168 | ||
7169 | return streq(state, "running") ? EXIT_SUCCESS : EXIT_FAILURE; | |
7170 | } | |
7171 | ||
7d4fb3b1 | 7172 | static int create_edit_temp_file(const char *new_path, const char *original_path, char **ret_tmp_fn) { |
45519fd6 | 7173 | _cleanup_free_ char *t = NULL; |
ae6c3cc0 | 7174 | int r; |
7d4fb3b1 RC |
7175 | |
7176 | assert(new_path); | |
7177 | assert(original_path); | |
7178 | assert(ret_tmp_fn); | |
7179 | ||
14bcf25c | 7180 | r = tempfn_random(new_path, NULL, &t); |
ae6c3cc0 | 7181 | if (r < 0) |
029009d4 | 7182 | return log_error_errno(r, "Failed to determine temporary filename for \"%s\": %m", new_path); |
7d4fb3b1 RC |
7183 | |
7184 | r = mkdir_parents(new_path, 0755); | |
45519fd6 | 7185 | if (r < 0) |
691395d8 | 7186 | return log_error_errno(r, "Failed to create directories for \"%s\": %m", new_path); |
7d4fb3b1 | 7187 | |
1c876927 | 7188 | r = copy_file(original_path, t, 0, 0644, 0, COPY_REFLINK); |
7d4fb3b1 | 7189 | if (r == -ENOENT) { |
45519fd6 | 7190 | |
7d4fb3b1 | 7191 | r = touch(t); |
45519fd6 LP |
7192 | if (r < 0) |
7193 | return log_error_errno(r, "Failed to create temporary file \"%s\": %m", t); | |
7194 | ||
7195 | } else if (r < 0) | |
39c38ce1 | 7196 | return log_error_errno(r, "Failed to create temporary file for \"%s\": %m", new_path); |
7d4fb3b1 | 7197 | |
ae2a15bc | 7198 | *ret_tmp_fn = TAKE_PTR(t); |
7d4fb3b1 RC |
7199 | |
7200 | return 0; | |
7201 | } | |
7202 | ||
c51932be LP |
7203 | static int get_file_to_edit( |
7204 | const LookupPaths *paths, | |
7205 | const char *name, | |
7206 | char **ret_path) { | |
7207 | ||
7208 | _cleanup_free_ char *path = NULL, *run = NULL; | |
7d4fb3b1 | 7209 | |
45519fd6 LP |
7210 | assert(name); |
7211 | assert(ret_path); | |
7212 | ||
605405c6 | 7213 | path = strjoin(paths->persistent_config, "/", name); |
c51932be | 7214 | if (!path) |
7d4fb3b1 RC |
7215 | return log_oom(); |
7216 | ||
c51932be | 7217 | if (arg_runtime) { |
605405c6 | 7218 | run = strjoin(paths->runtime_config, "/", name); |
c51932be LP |
7219 | if (!run) |
7220 | return log_oom(); | |
7221 | } | |
7222 | ||
bc854dc7 | 7223 | if (arg_runtime) { |
baaa35ad ZJS |
7224 | if (access(path, F_OK) >= 0) |
7225 | return log_error_errno(SYNTHETIC_ERRNO(EEXIST), | |
7226 | "Refusing to create \"%s\" because it would be overridden by \"%s\" anyway.", | |
7227 | run, path); | |
691395d8 | 7228 | |
ae2a15bc LP |
7229 | *ret_path = TAKE_PTR(run); |
7230 | } else | |
7231 | *ret_path = TAKE_PTR(path); | |
7d4fb3b1 RC |
7232 | |
7233 | return 0; | |
7234 | } | |
7235 | ||
39c38ce1 | 7236 | static int unit_file_create_new( |
c51932be LP |
7237 | const LookupPaths *paths, |
7238 | const char *unit_name, | |
39c38ce1 | 7239 | const char *suffix, |
c51932be LP |
7240 | char **ret_new_path, |
7241 | char **ret_tmp_path) { | |
7242 | ||
919d2720 LP |
7243 | _cleanup_free_ char *new_path = NULL, *tmp_path = NULL; |
7244 | const char *ending; | |
7d4fb3b1 RC |
7245 | int r; |
7246 | ||
7247 | assert(unit_name); | |
7248 | assert(ret_new_path); | |
7249 | assert(ret_tmp_path); | |
7250 | ||
39c38ce1 | 7251 | ending = strjoina(unit_name, suffix); |
919d2720 | 7252 | r = get_file_to_edit(paths, ending, &new_path); |
7d4fb3b1 RC |
7253 | if (r < 0) |
7254 | return r; | |
7255 | ||
919d2720 LP |
7256 | r = create_edit_temp_file(new_path, new_path, &tmp_path); |
7257 | if (r < 0) | |
7d4fb3b1 | 7258 | return r; |
7d4fb3b1 | 7259 | |
919d2720 LP |
7260 | *ret_new_path = TAKE_PTR(new_path); |
7261 | *ret_tmp_path = TAKE_PTR(tmp_path); | |
7d4fb3b1 RC |
7262 | |
7263 | return 0; | |
7264 | } | |
7265 | ||
1cfa9a4c | 7266 | static int unit_file_create_copy( |
c51932be | 7267 | const LookupPaths *paths, |
1cfa9a4c LP |
7268 | const char *unit_name, |
7269 | const char *fragment_path, | |
1cfa9a4c LP |
7270 | char **ret_new_path, |
7271 | char **ret_tmp_path) { | |
7272 | ||
92d6b0bf | 7273 | _cleanup_free_ char *new_path = NULL, *tmp_path = NULL; |
7d4fb3b1 RC |
7274 | int r; |
7275 | ||
7276 | assert(fragment_path); | |
7277 | assert(unit_name); | |
7278 | assert(ret_new_path); | |
7279 | assert(ret_tmp_path); | |
7280 | ||
92d6b0bf | 7281 | r = get_file_to_edit(paths, unit_name, &new_path); |
7d4fb3b1 RC |
7282 | if (r < 0) |
7283 | return r; | |
7284 | ||
92d6b0bf | 7285 | if (!path_equal(fragment_path, new_path) && access(new_path, F_OK) >= 0) { |
7d4fb3b1 RC |
7286 | char response; |
7287 | ||
92d6b0bf LP |
7288 | r = ask_char(&response, "yn", "\"%s\" already exists. Overwrite with \"%s\"? [(y)es, (n)o] ", new_path, fragment_path); |
7289 | if (r < 0) | |
7d4fb3b1 | 7290 | return r; |
5daacba2 LP |
7291 | if (response != 'y') |
7292 | return log_warning_errno(SYNTHETIC_ERRNO(EKEYREJECTED), "%s skipped.", unit_name); | |
7d4fb3b1 RC |
7293 | } |
7294 | ||
92d6b0bf LP |
7295 | r = create_edit_temp_file(new_path, fragment_path, &tmp_path); |
7296 | if (r < 0) | |
7d4fb3b1 | 7297 | return r; |
7d4fb3b1 | 7298 | |
92d6b0bf LP |
7299 | *ret_new_path = TAKE_PTR(new_path); |
7300 | *ret_tmp_path = TAKE_PTR(tmp_path); | |
7d4fb3b1 RC |
7301 | |
7302 | return 0; | |
7303 | } | |
7304 | ||
7305 | static int run_editor(char **paths) { | |
7d4fb3b1 RC |
7306 | int r; |
7307 | ||
7308 | assert(paths); | |
7309 | ||
0672e2c6 | 7310 | r = safe_fork("(editor)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG|FORK_WAIT, NULL); |
4c253ed1 | 7311 | if (r < 0) |
b6e1fff1 | 7312 | return r; |
4c253ed1 | 7313 | if (r == 0) { |
b2238e38 | 7314 | char **editor_args = NULL, **tmp_path, **original_path; |
70d6e5bd | 7315 | size_t n_editor_args = 0, i = 1, argc; |
b2238e38 | 7316 | const char **args, *editor, *p; |
7d4fb3b1 RC |
7317 | |
7318 | argc = strv_length(paths)/2 + 1; | |
7d4fb3b1 RC |
7319 | |
7320 | /* SYSTEMD_EDITOR takes precedence over EDITOR which takes precedence over VISUAL | |
7321 | * If neither SYSTEMD_EDITOR nor EDITOR nor VISUAL are present, | |
7322 | * we try to execute well known editors | |
7323 | */ | |
7324 | editor = getenv("SYSTEMD_EDITOR"); | |
7325 | if (!editor) | |
7326 | editor = getenv("EDITOR"); | |
7327 | if (!editor) | |
7328 | editor = getenv("VISUAL"); | |
7329 | ||
7330 | if (!isempty(editor)) { | |
9ef5d8f2 JS |
7331 | editor_args = strv_split(editor, WHITESPACE); |
7332 | if (!editor_args) { | |
7333 | (void) log_oom(); | |
7334 | _exit(EXIT_FAILURE); | |
7335 | } | |
7336 | n_editor_args = strv_length(editor_args); | |
7337 | argc += n_editor_args - 1; | |
7338 | } | |
02a126a3 | 7339 | |
9ef5d8f2 JS |
7340 | args = newa(const char*, argc + 1); |
7341 | ||
7342 | if (n_editor_args > 0) { | |
7343 | args[0] = editor_args[0]; | |
7344 | for (; i < n_editor_args; i++) | |
7345 | args[i] = editor_args[i]; | |
7346 | } | |
7347 | ||
df79fdab LP |
7348 | STRV_FOREACH_PAIR(original_path, tmp_path, paths) |
7349 | args[i++] = *tmp_path; | |
9ef5d8f2 JS |
7350 | args[i] = NULL; |
7351 | ||
7352 | if (n_editor_args > 0) | |
7353 | execvp(args[0], (char* const*) args); | |
7d4fb3b1 | 7354 | |
9391a1c3 | 7355 | FOREACH_STRING(p, "editor", "nano", "vim", "vi") { |
1cfa9a4c LP |
7356 | args[0] = p; |
7357 | execvp(p, (char* const*) args); | |
7d4fb3b1 RC |
7358 | /* We do not fail if the editor doesn't exist |
7359 | * because we want to try each one of them before | |
7360 | * failing. | |
7361 | */ | |
7362 | if (errno != ENOENT) { | |
691395d8 | 7363 | log_error_errno(errno, "Failed to execute %s: %m", editor); |
7d4fb3b1 RC |
7364 | _exit(EXIT_FAILURE); |
7365 | } | |
7366 | } | |
7367 | ||
1cfa9a4c | 7368 | log_error("Cannot edit unit(s), no editor available. Please set either $SYSTEMD_EDITOR, $EDITOR or $VISUAL."); |
7d4fb3b1 RC |
7369 | _exit(EXIT_FAILURE); |
7370 | } | |
7371 | ||
45519fd6 | 7372 | return 0; |
7d4fb3b1 RC |
7373 | } |
7374 | ||
7375 | static int find_paths_to_edit(sd_bus *bus, char **names, char ***paths) { | |
8e766630 | 7376 | _cleanup_(lookup_paths_free) LookupPaths lp = {}; |
7d4fb3b1 RC |
7377 | char **name; |
7378 | int r; | |
7379 | ||
7380 | assert(names); | |
7381 | assert(paths); | |
7382 | ||
4943d143 | 7383 | r = lookup_paths_init(&lp, arg_scope, 0, arg_root); |
5b013a2f | 7384 | if (r < 0) |
8df18507 | 7385 | return r; |
7d4fb3b1 | 7386 | |
e9e310f8 | 7387 | STRV_FOREACH(name, names) { |
b74df547 ZJS |
7388 | _cleanup_free_ char *path = NULL, *new_path = NULL, *tmp_path = NULL, *tmp_name = NULL; |
7389 | const char *unit_name; | |
7d4fb3b1 | 7390 | |
02a126a3 LP |
7391 | r = unit_find_paths(bus, *name, &lp, false, &path, NULL); |
7392 | if (r == -EKEYREJECTED) { | |
7393 | /* If loading of the unit failed server side complete, then the server won't tell us the unit | |
7394 | * file path. In that case, find the file client side. */ | |
7395 | log_debug_errno(r, "Unit '%s' was not loaded correctly, retrying client-side.", *name); | |
7396 | r = unit_find_paths(bus, *name, &lp, true, &path, NULL); | |
7397 | } | |
7398 | if (r == -ERFKILL) | |
7399 | return log_error_errno(r, "Unit '%s' masked, cannot edit.", *name); | |
e9e310f8 RC |
7400 | if (r < 0) |
7401 | return r; | |
b3734d98 ZJS |
7402 | |
7403 | if (r == 0) { | |
7404 | assert(!path); | |
7405 | ||
7406 | if (!arg_force) { | |
85163756 LP |
7407 | log_info("Run 'systemctl edit%s --force --full %s' to create a new unit.", |
7408 | arg_scope == UNIT_FILE_GLOBAL ? " --global" : | |
7409 | arg_scope == UNIT_FILE_USER ? " --user" : "", | |
7410 | *name); | |
39c38ce1 DC |
7411 | return -ENOENT; |
7412 | } | |
39c38ce1 | 7413 | |
b3734d98 ZJS |
7414 | /* Create a new unit from scratch */ |
7415 | unit_name = *name; | |
7416 | r = unit_file_create_new(&lp, unit_name, | |
7417 | arg_full ? NULL : ".d/override.conf", | |
7418 | &new_path, &tmp_path); | |
7419 | } else { | |
7420 | assert(path); | |
b74df547 | 7421 | |
b3734d98 ZJS |
7422 | unit_name = basename(path); |
7423 | /* We follow unit aliases, but we need to propagate the instance */ | |
7424 | if (unit_name_is_valid(*name, UNIT_NAME_INSTANCE) && | |
7425 | unit_name_is_valid(unit_name, UNIT_NAME_TEMPLATE)) { | |
7426 | _cleanup_free_ char *instance = NULL; | |
b74df547 | 7427 | |
b3734d98 ZJS |
7428 | r = unit_name_to_instance(*name, &instance); |
7429 | if (r < 0) | |
7430 | return r; | |
b74df547 | 7431 | |
b3734d98 ZJS |
7432 | r = unit_name_replace_instance(unit_name, instance, &tmp_name); |
7433 | if (r < 0) | |
7434 | return r; | |
7435 | ||
7436 | unit_name = tmp_name; | |
7437 | } | |
b74df547 | 7438 | |
39c38ce1 | 7439 | if (arg_full) |
b74df547 | 7440 | r = unit_file_create_copy(&lp, unit_name, path, &new_path, &tmp_path); |
39c38ce1 | 7441 | else |
b74df547 | 7442 | r = unit_file_create_new(&lp, unit_name, ".d/override.conf", &new_path, &tmp_path); |
b3734d98 | 7443 | } |
e9e310f8 | 7444 | if (r < 0) |
ad2a0358 | 7445 | return r; |
7d4fb3b1 | 7446 | |
e9e310f8 RC |
7447 | r = strv_push_pair(paths, new_path, tmp_path); |
7448 | if (r < 0) | |
7449 | return log_oom(); | |
02a126a3 | 7450 | |
78df0edc | 7451 | new_path = tmp_path = NULL; |
7d4fb3b1 RC |
7452 | } |
7453 | ||
7454 | return 0; | |
7455 | } | |
7456 | ||
e449de87 | 7457 | static int edit(int argc, char *argv[], void *userdata) { |
86f004fb | 7458 | _cleanup_(lookup_paths_free) LookupPaths lp = {}; |
7d4fb3b1 RC |
7459 | _cleanup_strv_free_ char **names = NULL; |
7460 | _cleanup_strv_free_ char **paths = NULL; | |
7461 | char **original, **tmp; | |
4fbd7192 | 7462 | sd_bus *bus; |
7d4fb3b1 RC |
7463 | int r; |
7464 | ||
7d4fb3b1 | 7465 | if (!on_tty()) { |
4fbd7192 | 7466 | log_error("Cannot edit units if not on a tty."); |
7d4fb3b1 RC |
7467 | return -EINVAL; |
7468 | } | |
7469 | ||
7470 | if (arg_transport != BUS_TRANSPORT_LOCAL) { | |
4fbd7192 | 7471 | log_error("Cannot edit units remotely."); |
7d4fb3b1 RC |
7472 | return -EINVAL; |
7473 | } | |
7474 | ||
86f004fb ZJS |
7475 | r = lookup_paths_init(&lp, arg_scope, 0, arg_root); |
7476 | if (r < 0) | |
7477 | return log_error_errno(r, "Failed to determine unit paths: %m"); | |
7478 | ||
4fbd7192 LP |
7479 | r = acquire_bus(BUS_MANAGER, &bus); |
7480 | if (r < 0) | |
7481 | return r; | |
7482 | ||
e449de87 | 7483 | r = expand_names(bus, strv_skip(argv, 1), NULL, &names); |
7d4fb3b1 RC |
7484 | if (r < 0) |
7485 | return log_error_errno(r, "Failed to expand names: %m"); | |
7486 | ||
9d9dd746 | 7487 | STRV_FOREACH(tmp, names) { |
86f004fb | 7488 | r = unit_is_masked(bus, &lp, *tmp); |
9d9dd746 ZJS |
7489 | if (r < 0) |
7490 | return r; | |
9d9dd746 ZJS |
7491 | if (r > 0) { |
7492 | log_error("Cannot edit %s: unit is masked.", *tmp); | |
7493 | return -EINVAL; | |
7494 | } | |
7495 | } | |
7496 | ||
7d4fb3b1 RC |
7497 | r = find_paths_to_edit(bus, names, &paths); |
7498 | if (r < 0) | |
7499 | return r; | |
7500 | ||
b5e6a600 | 7501 | if (strv_isempty(paths)) |
7d4fb3b1 | 7502 | return -ENOENT; |
7d4fb3b1 RC |
7503 | |
7504 | r = run_editor(paths); | |
7505 | if (r < 0) | |
7506 | goto end; | |
7507 | ||
7508 | STRV_FOREACH_PAIR(original, tmp, paths) { | |
9d9dd746 ZJS |
7509 | /* If the temporary file is empty we ignore it. |
7510 | * This allows the user to cancel the modification. | |
7d4fb3b1 RC |
7511 | */ |
7512 | if (null_or_empty_path(*tmp)) { | |
45519fd6 | 7513 | log_warning("Editing \"%s\" canceled: temporary file is empty.", *original); |
7d4fb3b1 RC |
7514 | continue; |
7515 | } | |
45519fd6 | 7516 | |
7d4fb3b1 RC |
7517 | r = rename(*tmp, *original); |
7518 | if (r < 0) { | |
029009d4 | 7519 | r = log_error_errno(errno, "Failed to rename \"%s\" to \"%s\": %m", *tmp, *original); |
7d4fb3b1 RC |
7520 | goto end; |
7521 | } | |
7522 | } | |
7523 | ||
45519fd6 LP |
7524 | r = 0; |
7525 | ||
7526 | if (!arg_no_reload && !install_client_side()) | |
e449de87 | 7527 | r = daemon_reload(argc, argv, userdata); |
7d4fb3b1 RC |
7528 | |
7529 | end: | |
5f18271e | 7530 | STRV_FOREACH_PAIR(original, tmp, paths) { |
45519fd6 | 7531 | (void) unlink(*tmp); |
7d4fb3b1 | 7532 | |
5f18271e RC |
7533 | /* Removing empty dropin dirs */ |
7534 | if (!arg_full) { | |
043717f9 RC |
7535 | _cleanup_free_ char *dir; |
7536 | ||
7537 | dir = dirname_malloc(*original); | |
7538 | if (!dir) | |
7539 | return log_oom(); | |
7540 | ||
5f18271e RC |
7541 | /* no need to check if the dir is empty, rmdir |
7542 | * does nothing if it is not the case. | |
7543 | */ | |
7544 | (void) rmdir(dir); | |
7545 | } | |
7546 | } | |
7547 | ||
7d4fb3b1 RC |
7548 | return r; |
7549 | } | |
7550 | ||
37ec0fdd LP |
7551 | static int systemctl_help(void) { |
7552 | _cleanup_free_ char *link = NULL; | |
7553 | int r; | |
7554 | ||
0221d68a | 7555 | (void) pager_open(arg_pager_flags); |
729e3769 | 7556 | |
37ec0fdd LP |
7557 | r = terminal_urlify_man("systemctl", "1", &link); |
7558 | if (r < 0) | |
7559 | return log_oom(); | |
7560 | ||
2e33c433 | 7561 | printf("%s [OPTIONS...] {COMMAND} ...\n\n" |
729e3769 | 7562 | "Query or send control commands to the systemd manager.\n\n" |
8a0867d6 LP |
7563 | " -h --help Show this help\n" |
7564 | " --version Show package version\n" | |
f459b602 MAP |
7565 | " --system Connect to system manager\n" |
7566 | " --user Connect to user service manager\n" | |
7567 | " -H --host=[USER@]HOST\n" | |
7568 | " Operate on remote host\n" | |
7569 | " -M --machine=CONTAINER\n" | |
7570 | " Operate on local container\n" | |
3fb90db2 ZJS |
7571 | " -t --type=TYPE List units of a particular type\n" |
7572 | " --state=STATE List units with particular LOAD or SUB or ACTIVE state\n" | |
8a0867d6 | 7573 | " -p --property=NAME Show only properties by this name\n" |
2cdbbc9a LP |
7574 | " -a --all Show all properties/all units currently in memory,\n" |
7575 | " including dead/empty ones. To list all units installed on\n" | |
7576 | " the system, use the 'list-unit-files' command instead.\n" | |
bef19548 | 7577 | " --failed Same as --state=failed\n" |
98a6e132 | 7578 | " -l --full Don't ellipsize unit names on output\n" |
1238ee09 | 7579 | " -r --recursive Show unit list of host and local containers\n" |
4dc5b821 LP |
7580 | " --reverse Show reverse dependencies with 'list-dependencies'\n" |
7581 | " --job-mode=MODE Specify how to deal with already queued jobs, when\n" | |
7582 | " queueing a new job\n" | |
a521ae4a | 7583 | " --show-types When showing sockets, explicitly show their type\n" |
4f9a9105 | 7584 | " --value When showing properties, only print the value\n" |
b37844d3 LP |
7585 | " -i --ignore-inhibitors\n" |
7586 | " When shutting down or sleeping, ignore inhibitors\n" | |
a8f11321 LP |
7587 | " --kill-who=WHO Who to send signal to\n" |
7588 | " -s --signal=SIGNAL Which signal to send\n" | |
57ab2eab | 7589 | " --now Start or stop unit in addition to enabling or disabling it\n" |
1ae17672 | 7590 | " --dry-run Only print what would be done\n" |
8a0867d6 | 7591 | " -q --quiet Suppress output\n" |
93a08841 | 7592 | " --wait For (re)start, wait until service stopped again\n" |
adb6cd9b | 7593 | " For is-system-running, wait until startup is completed\n" |
8a0867d6 | 7594 | " --no-block Do not wait until operation finished\n" |
8a0867d6 | 7595 | " --no-wall Don't send wall message before halt/power-off/reboot\n" |
3fb90db2 | 7596 | " --no-reload Don't reload daemon after en-/dis-abling unit files\n" |
ebed32bf | 7597 | " --no-legend Do not print a legend (column headers and hints)\n" |
69fc152f | 7598 | " --no-pager Do not pipe output into a pager\n" |
501fc174 LP |
7599 | " --no-ask-password\n" |
7600 | " Do not ask for system passwords\n" | |
4e93d369 LR |
7601 | " --global Enable/disable/mask unit files globally\n" |
7602 | " --runtime Enable/disable/mask unit files temporarily until next\n" | |
7603 | " reboot\n" | |
8a0867d6 LP |
7604 | " -f --force When enabling unit files, override existing symlinks\n" |
7605 | " When shutting down, execute action immediately\n" | |
3fb90db2 | 7606 | " --preset-mode= Apply only enable, only disable, or all presets\n" |
4e93d369 LR |
7607 | " --root=PATH Enable/disable/mask unit files in the specified root\n" |
7608 | " directory\n" | |
76fdc966 | 7609 | " -n --lines=INTEGER Number of journal entries to show\n" |
584c6e70 | 7610 | " -o --output=STRING Change journal output mode (short, short-precise,\n" |
7e563bfc IW |
7611 | " short-iso, short-iso-precise, short-full,\n" |
7612 | " short-monotonic, short-unix,\n" | |
584c6e70 | 7613 | " verbose, export, json, json-pretty, json-sse, cat)\n" |
5bdf2243 | 7614 | " --firmware-setup Tell the firmware to show the setup menu on next boot\n" |
97af80c5 LP |
7615 | " --boot-loader-menu=TIME\n" |
7616 | " Boot into boot loader menu on next boot\n" | |
7617 | " --boot-loader-entry=NAME\n" | |
7618 | " Boot into a specific boot loader entry on next boot\n" | |
815ebc54 | 7619 | " --plain Print unit dependencies as a list instead of a tree\n\n" |
34c4b47b | 7620 | "Unit Commands:\n" |
fbf3283b ZJS |
7621 | " list-units [PATTERN...] List units currently in memory\n" |
7622 | " list-sockets [PATTERN...] List socket units currently in memory,\n" | |
7623 | " ordered by address\n" | |
7624 | " list-timers [PATTERN...] List timer units currently in memory,\n" | |
7625 | " ordered by next elapse\n" | |
7626 | " start UNIT... Start (activate) one or more units\n" | |
7627 | " stop UNIT... Stop (deactivate) one or more units\n" | |
7628 | " reload UNIT... Reload one or more units\n" | |
7629 | " restart UNIT... Start or restart one or more units\n" | |
7630 | " try-restart UNIT... Restart one or more units if active\n" | |
7631 | " reload-or-restart UNIT... Reload one or more units if possible,\n" | |
7632 | " otherwise start or restart\n" | |
7633 | " try-reload-or-restart UNIT... If active, reload one or more units,\n" | |
7634 | " if supported, otherwise restart\n" | |
7635 | " isolate UNIT Start one unit and stop all others\n" | |
7636 | " kill UNIT... Send signal to processes of a unit\n" | |
7637 | " is-active PATTERN... Check whether units are active\n" | |
7638 | " is-failed PATTERN... Check whether units are failed\n" | |
7639 | " status [PATTERN...|PID...] Show runtime status of one or more units\n" | |
7640 | " show [PATTERN...|JOB...] Show properties of one or more\n" | |
7641 | " units/jobs or the manager\n" | |
7642 | " cat PATTERN... Show files and drop-ins of specified units\n" | |
7643 | " set-property UNIT PROPERTY=VALUE... Sets one or more properties of a unit\n" | |
7644 | " help PATTERN...|PID... Show manual for one or more units\n" | |
7645 | " reset-failed [PATTERN...] Reset failed state for all, one, or more\n" | |
7646 | " units\n" | |
7647 | " list-dependencies [UNIT] Recursively show units which are required\n" | |
7648 | " or wanted by this unit or by which this\n" | |
7649 | " unit is required or wanted\n\n" | |
34c4b47b | 7650 | "Unit File Commands:\n" |
fbf3283b ZJS |
7651 | " list-unit-files [PATTERN...] List installed unit files\n" |
7652 | " enable [UNIT...|PATH...] Enable one or more unit files\n" | |
7653 | " disable UNIT... Disable one or more unit files\n" | |
7654 | " reenable UNIT... Reenable one or more unit files\n" | |
7655 | " preset UNIT... Enable/disable one or more unit files\n" | |
7656 | " based on preset configuration\n" | |
7657 | " preset-all Enable/disable all unit files based on\n" | |
7658 | " preset configuration\n" | |
7659 | " is-enabled UNIT... Check whether unit files are enabled\n" | |
7660 | " mask UNIT... Mask one or more units\n" | |
7661 | " unmask UNIT... Unmask one or more units\n" | |
7662 | " link PATH... Link one or more units files into\n" | |
7663 | " the search path\n" | |
7664 | " revert UNIT... Revert one or more unit files to vendor\n" | |
7665 | " version\n" | |
7666 | " add-wants TARGET UNIT... Add 'Wants' dependency for the target\n" | |
7667 | " on specified one or more units\n" | |
7668 | " add-requires TARGET UNIT... Add 'Requires' dependency for the target\n" | |
7669 | " on specified one or more units\n" | |
7670 | " edit UNIT... Edit one or more unit files\n" | |
7671 | " get-default Get the name of the default target\n" | |
7672 | " set-default TARGET Set the default target\n\n" | |
0d292f5e | 7673 | "Machine Commands:\n" |
fbf3283b | 7674 | " list-machines [PATTERN...] List local containers and host\n\n" |
34c4b47b | 7675 | "Job Commands:\n" |
fbf3283b ZJS |
7676 | " list-jobs [PATTERN...] List jobs\n" |
7677 | " cancel [JOB...] Cancel all, one, or more jobs\n\n" | |
34c4b47b | 7678 | "Environment Commands:\n" |
fbf3283b | 7679 | " show-environment Dump environment\n" |
135775c1 ZJS |
7680 | " set-environment VARIABLE=VALUE... Set one or more environment variables\n" |
7681 | " unset-environment VARIABLE... Unset one or more environment variables\n" | |
7682 | " import-environment [VARIABLE...] Import all or some environment variables\n\n" | |
34c4b47b | 7683 | "Manager Lifecycle Commands:\n" |
fbf3283b ZJS |
7684 | " daemon-reload Reload systemd manager configuration\n" |
7685 | " daemon-reexec Reexecute systemd manager\n\n" | |
34c4b47b | 7686 | "System Commands:\n" |
fbf3283b ZJS |
7687 | " is-system-running Check whether system is fully running\n" |
7688 | " default Enter system default mode\n" | |
7689 | " rescue Enter system rescue mode\n" | |
7690 | " emergency Enter system emergency mode\n" | |
7691 | " halt Shut down and halt the system\n" | |
7692 | " poweroff Shut down and power-off the system\n" | |
7693 | " reboot [ARG] Shut down and reboot the system\n" | |
7694 | " kexec Shut down and reboot the system with kexec\n" | |
7695 | " exit [EXIT_CODE] Request user instance or container exit\n" | |
7696 | " switch-root ROOT [INIT] Change to a different root file system\n" | |
7697 | " suspend Suspend the system\n" | |
7698 | " hibernate Hibernate the system\n" | |
c58493c0 | 7699 | " hybrid-sleep Hibernate and suspend the system\n" |
e68c79db | 7700 | " suspend-then-hibernate Suspend the system, wake after a period of\n" |
37ec0fdd LP |
7701 | " time and put it into hibernate\n" |
7702 | "\nSee the %s for details.\n" | |
7703 | , program_invocation_short_name | |
7704 | , link | |
7705 | ); | |
7706 | ||
7707 | return 0; | |
7e4249b9 LP |
7708 | } |
7709 | ||
37ec0fdd LP |
7710 | static int halt_help(void) { |
7711 | _cleanup_free_ char *link = NULL; | |
7712 | int r; | |
7713 | ||
7714 | r = terminal_urlify_man("halt", "8", &link); | |
7715 | if (r < 0) | |
7716 | return log_oom(); | |
7717 | ||
37185ec8 | 7718 | printf("%s [OPTIONS...]%s\n\n" |
e4b61340 LP |
7719 | "%s the system.\n\n" |
7720 | " --help Show this help\n" | |
7721 | " --halt Halt the machine\n" | |
7722 | " -p --poweroff Switch off the machine\n" | |
7723 | " --reboot Reboot the machine\n" | |
2e33c433 LP |
7724 | " -f --force Force immediate halt/power-off/reboot\n" |
7725 | " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n" | |
e4b61340 | 7726 | " -d --no-wtmp Don't write wtmp record\n" |
37ec0fdd LP |
7727 | " --no-wall Don't send wall message before halt/power-off/reboot\n" |
7728 | "\nSee the %s for details.\n" | |
7729 | , program_invocation_short_name | |
7730 | , arg_action == ACTION_REBOOT ? " [ARG]" : "", | |
7731 | arg_action == ACTION_REBOOT ? "Reboot" : | |
7732 | arg_action == ACTION_POWEROFF ? "Power off" : | |
7733 | "Halt" | |
7734 | , link | |
7735 | ); | |
7736 | ||
7737 | return 0; | |
e4b61340 LP |
7738 | } |
7739 | ||
37ec0fdd LP |
7740 | static int shutdown_help(void) { |
7741 | _cleanup_free_ char *link = NULL; | |
7742 | int r; | |
7743 | ||
7744 | r = terminal_urlify_man("shutdown", "8", &link); | |
7745 | if (r < 0) | |
7746 | return log_oom(); | |
7747 | ||
08e4b1c5 | 7748 | printf("%s [OPTIONS...] [TIME] [WALL...]\n\n" |
e4b61340 LP |
7749 | "Shut down the system.\n\n" |
7750 | " --help Show this help\n" | |
7751 | " -H --halt Halt the machine\n" | |
7752 | " -P --poweroff Power-off the machine\n" | |
7753 | " -r --reboot Reboot the machine\n" | |
386da858 | 7754 | " -h Equivalent to --poweroff, overridden by --halt\n" |
2e33c433 | 7755 | " -k Don't halt/power-off/reboot, just send warnings\n" |
f6144808 | 7756 | " --no-wall Don't send wall message before halt/power-off/reboot\n" |
37ec0fdd LP |
7757 | " -c Cancel a pending shutdown\n" |
7758 | "\nSee the %s for details.\n" | |
7759 | , program_invocation_short_name | |
7760 | , link | |
7761 | ); | |
7762 | ||
7763 | return 0; | |
e4b61340 LP |
7764 | } |
7765 | ||
37ec0fdd LP |
7766 | static int telinit_help(void) { |
7767 | _cleanup_free_ char *link = NULL; | |
7768 | int r; | |
7769 | ||
7770 | r = terminal_urlify_man("telinit", "8", &link); | |
7771 | if (r < 0) | |
7772 | return log_oom(); | |
7773 | ||
2e33c433 | 7774 | printf("%s [OPTIONS...] {COMMAND}\n\n" |
514f4ef5 LP |
7775 | "Send control commands to the init daemon.\n\n" |
7776 | " --help Show this help\n" | |
2e33c433 | 7777 | " --no-wall Don't send wall message before halt/power-off/reboot\n\n" |
e4b61340 LP |
7778 | "Commands:\n" |
7779 | " 0 Power-off the machine\n" | |
7780 | " 6 Reboot the machine\n" | |
514f4ef5 LP |
7781 | " 2, 3, 4, 5 Start runlevelX.target unit\n" |
7782 | " 1, s, S Enter rescue mode\n" | |
7783 | " q, Q Reload init daemon configuration\n" | |
37ec0fdd LP |
7784 | " u, U Reexecute init daemon\n" |
7785 | "\nSee the %s for details.\n" | |
7786 | , program_invocation_short_name | |
7787 | , link | |
7788 | ); | |
7789 | ||
7790 | return 0; | |
e4b61340 LP |
7791 | } |
7792 | ||
37ec0fdd LP |
7793 | static int runlevel_help(void) { |
7794 | _cleanup_free_ char *link = NULL; | |
7795 | int r; | |
7796 | ||
7797 | r = terminal_urlify_man("runlevel", "8", &link); | |
7798 | if (r < 0) | |
7799 | return log_oom(); | |
7800 | ||
2e33c433 | 7801 | printf("%s [OPTIONS...]\n\n" |
e4b61340 | 7802 | "Prints the previous and current runlevel of the init system.\n\n" |
37ec0fdd LP |
7803 | " --help Show this help\n" |
7804 | "\nSee the %s for details.\n" | |
7805 | , program_invocation_short_name | |
7806 | , link | |
7807 | ); | |
7808 | ||
7809 | return 0; | |
e4b61340 LP |
7810 | } |
7811 | ||
b93312f5 | 7812 | static void help_types(void) { |
b93312f5 ZJS |
7813 | if (!arg_no_legend) |
7814 | puts("Available unit types:"); | |
5c828e66 LP |
7815 | |
7816 | DUMP_STRING_TABLE(unit_type, UnitType, _UNIT_TYPE_MAX); | |
e16972e6 ZJS |
7817 | } |
7818 | ||
7819 | static void help_states(void) { | |
e16972e6 ZJS |
7820 | if (!arg_no_legend) |
7821 | puts("Available unit load states:"); | |
5c828e66 | 7822 | DUMP_STRING_TABLE(unit_load_state, UnitLoadState, _UNIT_LOAD_STATE_MAX); |
e16972e6 ZJS |
7823 | |
7824 | if (!arg_no_legend) | |
7825 | puts("\nAvailable unit active states:"); | |
5c828e66 | 7826 | DUMP_STRING_TABLE(unit_active_state, UnitActiveState, _UNIT_ACTIVE_STATE_MAX); |
7e55de3b ZJS |
7827 | |
7828 | if (!arg_no_legend) | |
7829 | puts("\nAvailable automount unit substates:"); | |
5c828e66 | 7830 | DUMP_STRING_TABLE(automount_state, AutomountState, _AUTOMOUNT_STATE_MAX); |
7e55de3b | 7831 | |
7e55de3b ZJS |
7832 | if (!arg_no_legend) |
7833 | puts("\nAvailable device unit substates:"); | |
5c828e66 | 7834 | DUMP_STRING_TABLE(device_state, DeviceState, _DEVICE_STATE_MAX); |
7e55de3b ZJS |
7835 | |
7836 | if (!arg_no_legend) | |
7837 | puts("\nAvailable mount unit substates:"); | |
5c828e66 | 7838 | DUMP_STRING_TABLE(mount_state, MountState, _MOUNT_STATE_MAX); |
7e55de3b ZJS |
7839 | |
7840 | if (!arg_no_legend) | |
7841 | puts("\nAvailable path unit substates:"); | |
5c828e66 | 7842 | DUMP_STRING_TABLE(path_state, PathState, _PATH_STATE_MAX); |
7e55de3b ZJS |
7843 | |
7844 | if (!arg_no_legend) | |
7845 | puts("\nAvailable scope unit substates:"); | |
5c828e66 | 7846 | DUMP_STRING_TABLE(scope_state, ScopeState, _SCOPE_STATE_MAX); |
7e55de3b ZJS |
7847 | |
7848 | if (!arg_no_legend) | |
7849 | puts("\nAvailable service unit substates:"); | |
5c828e66 | 7850 | DUMP_STRING_TABLE(service_state, ServiceState, _SERVICE_STATE_MAX); |
7e55de3b ZJS |
7851 | |
7852 | if (!arg_no_legend) | |
7853 | puts("\nAvailable slice unit substates:"); | |
5c828e66 | 7854 | DUMP_STRING_TABLE(slice_state, SliceState, _SLICE_STATE_MAX); |
7e55de3b | 7855 | |
7e55de3b ZJS |
7856 | if (!arg_no_legend) |
7857 | puts("\nAvailable socket unit substates:"); | |
5c828e66 | 7858 | DUMP_STRING_TABLE(socket_state, SocketState, _SOCKET_STATE_MAX); |
7e55de3b ZJS |
7859 | |
7860 | if (!arg_no_legend) | |
7861 | puts("\nAvailable swap unit substates:"); | |
5c828e66 | 7862 | DUMP_STRING_TABLE(swap_state, SwapState, _SWAP_STATE_MAX); |
7e55de3b ZJS |
7863 | |
7864 | if (!arg_no_legend) | |
7865 | puts("\nAvailable target unit substates:"); | |
5c828e66 | 7866 | DUMP_STRING_TABLE(target_state, TargetState, _TARGET_STATE_MAX); |
7e55de3b ZJS |
7867 | |
7868 | if (!arg_no_legend) | |
7869 | puts("\nAvailable timer unit substates:"); | |
5c828e66 | 7870 | DUMP_STRING_TABLE(timer_state, TimerState, _TIMER_STATE_MAX); |
45c0c61d ZJS |
7871 | } |
7872 | ||
97af80c5 LP |
7873 | static int help_boot_loader_entry(void) { |
7874 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
7875 | _cleanup_free_ char **l = NULL; | |
7876 | sd_bus *bus; | |
7877 | char **i; | |
7878 | int r; | |
7879 | ||
7880 | r = acquire_bus(BUS_FULL, &bus); | |
7881 | if (r < 0) | |
7882 | return r; | |
7883 | ||
7884 | r = sd_bus_get_property_strv( | |
7885 | bus, | |
7886 | "org.freedesktop.login1", | |
7887 | "/org/freedesktop/login1", | |
7888 | "org.freedesktop.login1.Manager", | |
7889 | "BootLoaderEntries", | |
7890 | &error, | |
7891 | &l); | |
7892 | if (r < 0) | |
7893 | return log_error_errno(r, "Failed to enumerate boot loader entries: %s", bus_error_message(&error, r)); | |
7894 | ||
7895 | if (strv_isempty(l)) { | |
7896 | log_error("No boot loader entries discovered."); | |
7897 | return -ENODATA; | |
7898 | } | |
7899 | ||
7900 | STRV_FOREACH(i, l) | |
7901 | puts(*i); | |
7902 | ||
7903 | return 0; | |
7904 | } | |
7905 | ||
e4b61340 | 7906 | static int systemctl_parse_argv(int argc, char *argv[]) { |
7e4249b9 | 7907 | enum { |
90d473a1 | 7908 | ARG_FAIL = 0x100, |
afba4199 ZJS |
7909 | ARG_REVERSE, |
7910 | ARG_AFTER, | |
7911 | ARG_BEFORE, | |
1ae17672 | 7912 | ARG_DRY_RUN, |
991f2a39 | 7913 | ARG_SHOW_TYPES, |
23ade460 | 7914 | ARG_IRREVERSIBLE, |
e67c3609 | 7915 | ARG_IGNORE_DEPENDENCIES, |
4f9a9105 | 7916 | ARG_VALUE, |
35df8f27 | 7917 | ARG_VERSION, |
af2d49f7 | 7918 | ARG_USER, |
7e4249b9 | 7919 | ARG_SYSTEM, |
ee5762e3 | 7920 | ARG_GLOBAL, |
6e905d93 | 7921 | ARG_NO_BLOCK, |
ebed32bf | 7922 | ARG_NO_LEGEND, |
611efaac | 7923 | ARG_NO_PAGER, |
4445a875 | 7924 | ARG_NO_WALL, |
be394c48 | 7925 | ARG_ROOT, |
ee5762e3 | 7926 | ARG_NO_RELOAD, |
501fc174 | 7927 | ARG_KILL_WHO, |
30732560 | 7928 | ARG_NO_ASK_PASSWORD, |
729e3769 | 7929 | ARG_FAILED, |
df50185b | 7930 | ARG_RUNTIME, |
9b9b3d36 | 7931 | ARG_PLAIN, |
4dc5b821 | 7932 | ARG_STATE, |
d309c1c3 LP |
7933 | ARG_JOB_MODE, |
7934 | ARG_PRESET_MODE, | |
5bdf2243 | 7935 | ARG_FIRMWARE_SETUP, |
97af80c5 LP |
7936 | ARG_BOOT_LOADER_MENU, |
7937 | ARG_BOOT_LOADER_ENTRY, | |
57ab2eab | 7938 | ARG_NOW, |
9ef15026 | 7939 | ARG_MESSAGE, |
93a08841 | 7940 | ARG_WAIT, |
7e4249b9 LP |
7941 | }; |
7942 | ||
7943 | static const struct option options[] = { | |
9ea9d4cf LP |
7944 | { "help", no_argument, NULL, 'h' }, |
7945 | { "version", no_argument, NULL, ARG_VERSION }, | |
7946 | { "type", required_argument, NULL, 't' }, | |
7947 | { "property", required_argument, NULL, 'p' }, | |
7948 | { "all", no_argument, NULL, 'a' }, | |
7949 | { "reverse", no_argument, NULL, ARG_REVERSE }, | |
7950 | { "after", no_argument, NULL, ARG_AFTER }, | |
7951 | { "before", no_argument, NULL, ARG_BEFORE }, | |
7952 | { "show-types", no_argument, NULL, ARG_SHOW_TYPES }, | |
7953 | { "failed", no_argument, NULL, ARG_FAILED }, /* compatibility only */ | |
7954 | { "full", no_argument, NULL, 'l' }, | |
4dc5b821 LP |
7955 | { "job-mode", required_argument, NULL, ARG_JOB_MODE }, |
7956 | { "fail", no_argument, NULL, ARG_FAIL }, /* compatibility only */ | |
7957 | { "irreversible", no_argument, NULL, ARG_IRREVERSIBLE }, /* compatibility only */ | |
7958 | { "ignore-dependencies", no_argument, NULL, ARG_IGNORE_DEPENDENCIES }, /* compatibility only */ | |
9ea9d4cf | 7959 | { "ignore-inhibitors", no_argument, NULL, 'i' }, |
4f9a9105 | 7960 | { "value", no_argument, NULL, ARG_VALUE }, |
9ea9d4cf LP |
7961 | { "user", no_argument, NULL, ARG_USER }, |
7962 | { "system", no_argument, NULL, ARG_SYSTEM }, | |
7963 | { "global", no_argument, NULL, ARG_GLOBAL }, | |
93a08841 | 7964 | { "wait", no_argument, NULL, ARG_WAIT }, |
9ea9d4cf LP |
7965 | { "no-block", no_argument, NULL, ARG_NO_BLOCK }, |
7966 | { "no-legend", no_argument, NULL, ARG_NO_LEGEND }, | |
7967 | { "no-pager", no_argument, NULL, ARG_NO_PAGER }, | |
7968 | { "no-wall", no_argument, NULL, ARG_NO_WALL }, | |
1ae17672 | 7969 | { "dry-run", no_argument, NULL, ARG_DRY_RUN }, |
9ea9d4cf LP |
7970 | { "quiet", no_argument, NULL, 'q' }, |
7971 | { "root", required_argument, NULL, ARG_ROOT }, | |
68da321f | 7972 | { "force", no_argument, NULL, 'f' }, |
9ea9d4cf LP |
7973 | { "no-reload", no_argument, NULL, ARG_NO_RELOAD }, |
7974 | { "kill-who", required_argument, NULL, ARG_KILL_WHO }, | |
7975 | { "signal", required_argument, NULL, 's' }, | |
7976 | { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD }, | |
7977 | { "host", required_argument, NULL, 'H' }, | |
f459b602 | 7978 | { "machine", required_argument, NULL, 'M' }, |
9ea9d4cf LP |
7979 | { "runtime", no_argument, NULL, ARG_RUNTIME }, |
7980 | { "lines", required_argument, NULL, 'n' }, | |
7981 | { "output", required_argument, NULL, 'o' }, | |
7982 | { "plain", no_argument, NULL, ARG_PLAIN }, | |
7983 | { "state", required_argument, NULL, ARG_STATE }, | |
1238ee09 | 7984 | { "recursive", no_argument, NULL, 'r' }, |
d309c1c3 | 7985 | { "preset-mode", required_argument, NULL, ARG_PRESET_MODE }, |
5bdf2243 | 7986 | { "firmware-setup", no_argument, NULL, ARG_FIRMWARE_SETUP }, |
97af80c5 LP |
7987 | { "boot-loader-menu", required_argument, NULL, ARG_BOOT_LOADER_MENU }, |
7988 | { "boot-loader-entry", required_argument, NULL, ARG_BOOT_LOADER_ENTRY }, | |
57ab2eab | 7989 | { "now", no_argument, NULL, ARG_NOW }, |
9ef15026 | 7990 | { "message", required_argument, NULL, ARG_MESSAGE }, |
eb9da376 | 7991 | {} |
7e4249b9 LP |
7992 | }; |
7993 | ||
0f03c2a4 | 7994 | int c, r; |
7e4249b9 | 7995 | |
e4b61340 | 7996 | assert(argc >= 0); |
7e4249b9 LP |
7997 | assert(argv); |
7998 | ||
16f017fa IS |
7999 | /* we default to allowing interactive authorization only in systemctl (not in the legacy commands) */ |
8000 | arg_ask_password = true; | |
8001 | ||
2e7e19ca | 8002 | while ((c = getopt_long(argc, argv, "ht:p:alqfs:H:M:n:o:ir.::", options, NULL)) >= 0) |
7e4249b9 LP |
8003 | |
8004 | switch (c) { | |
8005 | ||
8006 | case 'h': | |
37ec0fdd | 8007 | return systemctl_help(); |
35df8f27 LP |
8008 | |
8009 | case ARG_VERSION: | |
3f6fd1ba | 8010 | return version(); |
7e4249b9 | 8011 | |
20b3f379 | 8012 | case 't': { |
fb1ac50e LP |
8013 | const char *p; |
8014 | ||
baaa35ad ZJS |
8015 | if (isempty(optarg)) |
8016 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8017 | "--type= requires arguments."); | |
45c0c61d | 8018 | |
c58bd76a | 8019 | for (p = optarg;;) { |
5ab22f33 | 8020 | _cleanup_free_ char *type = NULL; |
20b3f379 | 8021 | |
5ab22f33 SS |
8022 | r = extract_first_word(&p, &type, ",", 0); |
8023 | if (r < 0) | |
8024 | return log_error_errno(r, "Failed to parse type: %s", optarg); | |
5ab22f33 SS |
8025 | if (r == 0) |
8026 | break; | |
20b3f379 ZJS |
8027 | |
8028 | if (streq(type, "help")) { | |
8029 | help_types(); | |
8030 | return 0; | |
8031 | } | |
8032 | ||
8033 | if (unit_type_from_string(type) >= 0) { | |
96e895ef | 8034 | if (strv_consume(&arg_types, TAKE_PTR(type)) < 0) |
20b3f379 | 8035 | return log_oom(); |
20b3f379 ZJS |
8036 | continue; |
8037 | } | |
8038 | ||
9b9b3d36 MW |
8039 | /* It's much nicer to use --state= for |
8040 | * load states, but let's support this | |
8041 | * in --types= too for compatibility | |
8042 | * with old versions */ | |
7e9d36e0 | 8043 | if (unit_load_state_from_string(type) >= 0) { |
96e895ef | 8044 | if (strv_consume(&arg_states, TAKE_PTR(type)) < 0) |
20b3f379 | 8045 | return log_oom(); |
20b3f379 ZJS |
8046 | continue; |
8047 | } | |
8048 | ||
ab06eef8 | 8049 | log_error("Unknown unit type or load state '%s'.", type); |
baaa35ad ZJS |
8050 | return log_info_errno(SYNTHETIC_ERRNO(EINVAL), |
8051 | "Use -t help to see a list of allowed values."); | |
c147dc42 | 8052 | } |
20b3f379 ZJS |
8053 | |
8054 | break; | |
8055 | } | |
8056 | ||
aee7082f LP |
8057 | case 'p': |
8058 | /* Make sure that if the empty property list was specified, we won't show any | |
8059 | properties. */ | |
20b3f379 | 8060 | if (isempty(optarg) && !arg_properties) { |
cbc9fbd1 | 8061 | arg_properties = new0(char*, 1); |
20b3f379 ZJS |
8062 | if (!arg_properties) |
8063 | return log_oom(); | |
fb1ac50e LP |
8064 | } else { |
8065 | const char *p; | |
8066 | ||
c58bd76a | 8067 | for (p = optarg;;) { |
5ab22f33 | 8068 | _cleanup_free_ char *prop = NULL; |
033a842c | 8069 | |
5ab22f33 SS |
8070 | r = extract_first_word(&p, &prop, ",", 0); |
8071 | if (r < 0) | |
8072 | return log_error_errno(r, "Failed to parse property: %s", optarg); | |
5ab22f33 SS |
8073 | if (r == 0) |
8074 | break; | |
ea4a240d | 8075 | |
9dccc6d3 | 8076 | if (strv_consume(&arg_properties, TAKE_PTR(prop)) < 0) |
20b3f379 | 8077 | return log_oom(); |
20b3f379 | 8078 | } |
fb1ac50e | 8079 | } |
48220598 LP |
8080 | |
8081 | /* If the user asked for a particular | |
8082 | * property, show it to him, even if it is | |
8083 | * empty. */ | |
8084 | arg_all = true; | |
033a842c | 8085 | |
48220598 LP |
8086 | break; |
8087 | ||
7e4249b9 LP |
8088 | case 'a': |
8089 | arg_all = true; | |
8090 | break; | |
8091 | ||
afba4199 ZJS |
8092 | case ARG_REVERSE: |
8093 | arg_dependency = DEPENDENCY_REVERSE; | |
8094 | break; | |
8095 | ||
8096 | case ARG_AFTER: | |
8097 | arg_dependency = DEPENDENCY_AFTER; | |
82948f6c | 8098 | arg_jobs_after = true; |
afba4199 ZJS |
8099 | break; |
8100 | ||
8101 | case ARG_BEFORE: | |
8102 | arg_dependency = DEPENDENCY_BEFORE; | |
82948f6c | 8103 | arg_jobs_before = true; |
afba4199 ZJS |
8104 | break; |
8105 | ||
991f2a39 ZJS |
8106 | case ARG_SHOW_TYPES: |
8107 | arg_show_types = true; | |
8108 | break; | |
8109 | ||
4f9a9105 ZJS |
8110 | case ARG_VALUE: |
8111 | arg_value = true; | |
8112 | break; | |
8113 | ||
4dc5b821 LP |
8114 | case ARG_JOB_MODE: |
8115 | arg_job_mode = optarg; | |
8116 | break; | |
8117 | ||
90d473a1 | 8118 | case ARG_FAIL: |
e67c3609 LP |
8119 | arg_job_mode = "fail"; |
8120 | break; | |
8121 | ||
23ade460 MS |
8122 | case ARG_IRREVERSIBLE: |
8123 | arg_job_mode = "replace-irreversibly"; | |
8124 | break; | |
8125 | ||
e67c3609 LP |
8126 | case ARG_IGNORE_DEPENDENCIES: |
8127 | arg_job_mode = "ignore-dependencies"; | |
7e4249b9 LP |
8128 | break; |
8129 | ||
af2d49f7 | 8130 | case ARG_USER: |
729e3769 | 8131 | arg_scope = UNIT_FILE_USER; |
7e4249b9 LP |
8132 | break; |
8133 | ||
8134 | case ARG_SYSTEM: | |
729e3769 LP |
8135 | arg_scope = UNIT_FILE_SYSTEM; |
8136 | break; | |
8137 | ||
8138 | case ARG_GLOBAL: | |
8139 | arg_scope = UNIT_FILE_GLOBAL; | |
7e4249b9 LP |
8140 | break; |
8141 | ||
93a08841 MP |
8142 | case ARG_WAIT: |
8143 | arg_wait = true; | |
8144 | break; | |
8145 | ||
6e905d93 LP |
8146 | case ARG_NO_BLOCK: |
8147 | arg_no_block = true; | |
7e4249b9 LP |
8148 | break; |
8149 | ||
ebed32bf MS |
8150 | case ARG_NO_LEGEND: |
8151 | arg_no_legend = true; | |
8152 | break; | |
8153 | ||
611efaac | 8154 | case ARG_NO_PAGER: |
0221d68a | 8155 | arg_pager_flags |= PAGER_DISABLE; |
611efaac | 8156 | break; |
0736af98 | 8157 | |
514f4ef5 LP |
8158 | case ARG_NO_WALL: |
8159 | arg_no_wall = true; | |
8160 | break; | |
8161 | ||
be394c48 | 8162 | case ARG_ROOT: |
bac75eb3 | 8163 | r = parse_path_argument_and_warn(optarg, false, &arg_root); |
0f03c2a4 LP |
8164 | if (r < 0) |
8165 | return r; | |
be394c48 FC |
8166 | break; |
8167 | ||
98a6e132 | 8168 | case 'l': |
8fe914ec LP |
8169 | arg_full = true; |
8170 | break; | |
8171 | ||
30732560 | 8172 | case ARG_FAILED: |
9b9b3d36 MW |
8173 | if (strv_extend(&arg_states, "failed") < 0) |
8174 | return log_oom(); | |
8175 | ||
30732560 LP |
8176 | break; |
8177 | ||
1ae17672 ZJS |
8178 | case ARG_DRY_RUN: |
8179 | arg_dry_run = true; | |
8180 | break; | |
8181 | ||
0183528f LP |
8182 | case 'q': |
8183 | arg_quiet = true; | |
8184 | break; | |
8185 | ||
b4f27ccc | 8186 | case 'f': |
313cefa1 | 8187 | arg_force++; |
ee5762e3 LP |
8188 | break; |
8189 | ||
8190 | case ARG_NO_RELOAD: | |
8191 | arg_no_reload = true; | |
8192 | break; | |
8193 | ||
8a0867d6 LP |
8194 | case ARG_KILL_WHO: |
8195 | arg_kill_who = optarg; | |
8196 | break; | |
8197 | ||
8a0867d6 | 8198 | case 's': |
5c828e66 LP |
8199 | if (streq(optarg, "help")) { |
8200 | DUMP_STRING_TABLE(signal, int, _NSIG); | |
8201 | return 0; | |
8202 | } | |
8203 | ||
29a3db75 | 8204 | arg_signal = signal_from_string(optarg); |
baaa35ad ZJS |
8205 | if (arg_signal < 0) |
8206 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8207 | "Failed to parse signal string %s.", | |
8208 | optarg); | |
8a0867d6 LP |
8209 | break; |
8210 | ||
501fc174 LP |
8211 | case ARG_NO_ASK_PASSWORD: |
8212 | arg_ask_password = false; | |
8213 | break; | |
8214 | ||
f459b602 MAP |
8215 | case 'H': |
8216 | arg_transport = BUS_TRANSPORT_REMOTE; | |
8217 | arg_host = optarg; | |
a8f11321 LP |
8218 | break; |
8219 | ||
f459b602 | 8220 | case 'M': |
de33fc62 | 8221 | arg_transport = BUS_TRANSPORT_MACHINE; |
f459b602 | 8222 | arg_host = optarg; |
a8f11321 LP |
8223 | break; |
8224 | ||
729e3769 LP |
8225 | case ARG_RUNTIME: |
8226 | arg_runtime = true; | |
8227 | break; | |
8228 | ||
df50185b | 8229 | case 'n': |
baaa35ad ZJS |
8230 | if (safe_atou(optarg, &arg_lines) < 0) |
8231 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8232 | "Failed to parse lines '%s'", | |
8233 | optarg); | |
df50185b LP |
8234 | break; |
8235 | ||
df50185b | 8236 | case 'o': |
5c828e66 LP |
8237 | if (streq(optarg, "help")) { |
8238 | DUMP_STRING_TABLE(output_mode, OutputMode, _OUTPUT_MODE_MAX); | |
8239 | return 0; | |
8240 | } | |
8241 | ||
df50185b | 8242 | arg_output = output_mode_from_string(optarg); |
baaa35ad ZJS |
8243 | if (arg_output < 0) |
8244 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8245 | "Unknown output '%s'.", | |
8246 | optarg); | |
df50185b LP |
8247 | break; |
8248 | ||
b37844d3 LP |
8249 | case 'i': |
8250 | arg_ignore_inhibitors = true; | |
8251 | break; | |
8252 | ||
5d0c05e5 LN |
8253 | case ARG_PLAIN: |
8254 | arg_plain = true; | |
8255 | break; | |
8256 | ||
5bdf2243 JJ |
8257 | case ARG_FIRMWARE_SETUP: |
8258 | arg_firmware_setup = true; | |
8259 | break; | |
8260 | ||
97af80c5 LP |
8261 | case ARG_BOOT_LOADER_MENU: |
8262 | ||
8263 | r = parse_sec(optarg, &arg_boot_loader_menu); | |
8264 | if (r < 0) | |
8265 | return log_error_errno(r, "Failed to parse --boot-loader-menu= argument '%s': %m", optarg); | |
8266 | ||
8267 | break; | |
8268 | ||
8269 | case ARG_BOOT_LOADER_ENTRY: | |
8270 | ||
8271 | if (streq(optarg, "help")) { /* Yes, this means, "help" is not a valid boot loader entry name we can deal with */ | |
8272 | r = help_boot_loader_entry(); | |
8273 | if (r < 0) | |
8274 | return r; | |
8275 | ||
8276 | return 0; | |
8277 | } | |
8278 | ||
8279 | arg_boot_loader_entry = empty_to_null(optarg); | |
8280 | break; | |
8281 | ||
9b9b3d36 | 8282 | case ARG_STATE: { |
fb1ac50e LP |
8283 | const char *p; |
8284 | ||
baaa35ad ZJS |
8285 | if (isempty(optarg)) |
8286 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8287 | "--state= requires arguments."); | |
9b9b3d36 | 8288 | |
c58bd76a | 8289 | for (p = optarg;;) { |
bc6c18fe | 8290 | _cleanup_free_ char *s = NULL; |
9b9b3d36 | 8291 | |
5ab22f33 SS |
8292 | r = extract_first_word(&p, &s, ",", 0); |
8293 | if (r < 0) | |
6862111e | 8294 | return log_error_errno(r, "Failed to parse state: %s", optarg); |
5ab22f33 SS |
8295 | if (r == 0) |
8296 | break; | |
9b9b3d36 | 8297 | |
e16972e6 ZJS |
8298 | if (streq(s, "help")) { |
8299 | help_states(); | |
8300 | return 0; | |
8301 | } | |
8302 | ||
9dccc6d3 | 8303 | if (strv_consume(&arg_states, TAKE_PTR(s)) < 0) |
9b9b3d36 | 8304 | return log_oom(); |
9b9b3d36 MW |
8305 | } |
8306 | break; | |
8307 | } | |
8308 | ||
1238ee09 | 8309 | case 'r': |
baaa35ad ZJS |
8310 | if (geteuid() != 0) |
8311 | return log_error_errno(SYNTHETIC_ERRNO(EPERM), | |
8312 | "--recursive requires root privileges."); | |
1238ee09 LP |
8313 | |
8314 | arg_recursive = true; | |
8315 | break; | |
8316 | ||
d309c1c3 | 8317 | case ARG_PRESET_MODE: |
5c828e66 LP |
8318 | if (streq(optarg, "help")) { |
8319 | DUMP_STRING_TABLE(unit_file_preset_mode, UnitFilePresetMode, _UNIT_FILE_PRESET_MAX); | |
8320 | return 0; | |
8321 | } | |
d309c1c3 LP |
8322 | |
8323 | arg_preset_mode = unit_file_preset_mode_from_string(optarg); | |
baaa35ad ZJS |
8324 | if (arg_preset_mode < 0) |
8325 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8326 | "Failed to parse preset mode: %s.", optarg); | |
d309c1c3 LP |
8327 | |
8328 | break; | |
8329 | ||
57ab2eab JS |
8330 | case ARG_NOW: |
8331 | arg_now = true; | |
8332 | break; | |
8333 | ||
9ef15026 JS |
8334 | case ARG_MESSAGE: |
8335 | if (strv_extend(&arg_wall, optarg) < 0) | |
8336 | return log_oom(); | |
8337 | break; | |
8338 | ||
2e7e19ca ZJS |
8339 | case '.': |
8340 | /* Output an error mimicking getopt, and print a hint afterwards */ | |
8341 | log_error("%s: invalid option -- '.'", program_invocation_name); | |
8342 | log_notice("Hint: to specify units starting with a dash, use \"--\":\n" | |
8343 | " %s [OPTIONS...] {COMMAND} -- -.%s ...", | |
8344 | program_invocation_name, optarg ?: "mount"); | |
8345 | _fallthrough_; | |
8346 | ||
7e4249b9 LP |
8347 | case '?': |
8348 | return -EINVAL; | |
8349 | ||
8350 | default: | |
eb9da376 | 8351 | assert_not_reached("Unhandled option"); |
7e4249b9 | 8352 | } |
7e4249b9 | 8353 | |
baaa35ad ZJS |
8354 | if (arg_transport != BUS_TRANSPORT_LOCAL && arg_scope != UNIT_FILE_SYSTEM) |
8355 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8356 | "Cannot access user instance remotely."); | |
a8f11321 | 8357 | |
baaa35ad ZJS |
8358 | if (arg_wait && arg_no_block) |
8359 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8360 | "--wait may not be combined with --no-block."); | |
93a08841 | 8361 | |
7e4249b9 LP |
8362 | return 1; |
8363 | } | |
8364 | ||
e4b61340 | 8365 | static int halt_parse_argv(int argc, char *argv[]) { |
e4b61340 LP |
8366 | enum { |
8367 | ARG_HELP = 0x100, | |
8368 | ARG_HALT, | |
514f4ef5 LP |
8369 | ARG_REBOOT, |
8370 | ARG_NO_WALL | |
e4b61340 LP |
8371 | }; |
8372 | ||
8373 | static const struct option options[] = { | |
8374 | { "help", no_argument, NULL, ARG_HELP }, | |
8375 | { "halt", no_argument, NULL, ARG_HALT }, | |
8376 | { "poweroff", no_argument, NULL, 'p' }, | |
8377 | { "reboot", no_argument, NULL, ARG_REBOOT }, | |
8378 | { "force", no_argument, NULL, 'f' }, | |
8379 | { "wtmp-only", no_argument, NULL, 'w' }, | |
8380 | { "no-wtmp", no_argument, NULL, 'd' }, | |
f3f054f0 | 8381 | { "no-sync", no_argument, NULL, 'n' }, |
514f4ef5 | 8382 | { "no-wall", no_argument, NULL, ARG_NO_WALL }, |
eb9da376 | 8383 | {} |
e4b61340 LP |
8384 | }; |
8385 | ||
37185ec8 | 8386 | int c, r, runlevel; |
e4b61340 LP |
8387 | |
8388 | assert(argc >= 0); | |
8389 | assert(argv); | |
8390 | ||
8391 | if (utmp_get_runlevel(&runlevel, NULL) >= 0) | |
4c701096 | 8392 | if (IN_SET(runlevel, '0', '6')) |
65491fd8 | 8393 | arg_force = 2; |
e4b61340 | 8394 | |
601185b4 | 8395 | while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) |
e4b61340 LP |
8396 | switch (c) { |
8397 | ||
8398 | case ARG_HELP: | |
37ec0fdd | 8399 | return halt_help(); |
e4b61340 LP |
8400 | |
8401 | case ARG_HALT: | |
8402 | arg_action = ACTION_HALT; | |
8403 | break; | |
8404 | ||
8405 | case 'p': | |
a042efad MS |
8406 | if (arg_action != ACTION_REBOOT) |
8407 | arg_action = ACTION_POWEROFF; | |
e4b61340 LP |
8408 | break; |
8409 | ||
8410 | case ARG_REBOOT: | |
8411 | arg_action = ACTION_REBOOT; | |
8412 | break; | |
8413 | ||
8414 | case 'f': | |
65491fd8 | 8415 | arg_force = 2; |
e4b61340 LP |
8416 | break; |
8417 | ||
8418 | case 'w': | |
1ae17672 | 8419 | arg_dry_run = true; |
e4b61340 LP |
8420 | break; |
8421 | ||
8422 | case 'd': | |
8423 | arg_no_wtmp = true; | |
8424 | break; | |
8425 | ||
f3f054f0 TB |
8426 | case 'n': |
8427 | arg_no_sync = true; | |
8428 | break; | |
8429 | ||
514f4ef5 LP |
8430 | case ARG_NO_WALL: |
8431 | arg_no_wall = true; | |
8432 | break; | |
8433 | ||
e4b61340 LP |
8434 | case 'i': |
8435 | case 'h': | |
8436 | /* Compatibility nops */ | |
8437 | break; | |
8438 | ||
8439 | case '?': | |
8440 | return -EINVAL; | |
8441 | ||
8442 | default: | |
eb9da376 | 8443 | assert_not_reached("Unhandled option"); |
e4b61340 | 8444 | } |
e4b61340 | 8445 | |
c5220a94 | 8446 | if (arg_action == ACTION_REBOOT && (argc == optind || argc == optind + 1)) { |
27c06cb5 | 8447 | r = update_reboot_parameter_and_warn(argc == optind + 1 ? argv[optind] : NULL); |
c5220a94 | 8448 | if (r < 0) |
37185ec8 | 8449 | return r; |
baaa35ad ZJS |
8450 | } else if (optind < argc) |
8451 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8452 | "Too many arguments."); | |
e4b61340 LP |
8453 | |
8454 | return 1; | |
8455 | } | |
8456 | ||
2cc7b0a2 | 8457 | static int parse_shutdown_time_spec(const char *t, usec_t *_u) { |
f6144808 LP |
8458 | assert(t); |
8459 | assert(_u); | |
8460 | ||
8461 | if (streq(t, "now")) | |
8462 | *_u = 0; | |
1a639877 | 8463 | else if (!strchr(t, ':')) { |
f6144808 LP |
8464 | uint64_t u; |
8465 | ||
1a639877 | 8466 | if (safe_atou64(t, &u) < 0) |
f6144808 LP |
8467 | return -EINVAL; |
8468 | ||
8469 | *_u = now(CLOCK_REALTIME) + USEC_PER_MINUTE * u; | |
8470 | } else { | |
8471 | char *e = NULL; | |
8472 | long hour, minute; | |
b92bea5d | 8473 | struct tm tm = {}; |
f6144808 LP |
8474 | time_t s; |
8475 | usec_t n; | |
8476 | ||
8477 | errno = 0; | |
8478 | hour = strtol(t, &e, 10); | |
8333c77e | 8479 | if (errno > 0 || *e != ':' || hour < 0 || hour > 23) |
f6144808 LP |
8480 | return -EINVAL; |
8481 | ||
8482 | minute = strtol(e+1, &e, 10); | |
8333c77e | 8483 | if (errno > 0 || *e != 0 || minute < 0 || minute > 59) |
f6144808 LP |
8484 | return -EINVAL; |
8485 | ||
8486 | n = now(CLOCK_REALTIME); | |
08e4b1c5 LP |
8487 | s = (time_t) (n / USEC_PER_SEC); |
8488 | ||
f6144808 LP |
8489 | assert_se(localtime_r(&s, &tm)); |
8490 | ||
8491 | tm.tm_hour = (int) hour; | |
8492 | tm.tm_min = (int) minute; | |
08e4b1c5 | 8493 | tm.tm_sec = 0; |
f6144808 | 8494 | |
855a86a3 ZJS |
8495 | s = mktime(&tm); |
8496 | assert(s >= 0); | |
f6144808 LP |
8497 | |
8498 | *_u = (usec_t) s * USEC_PER_SEC; | |
8499 | ||
8500 | while (*_u <= n) | |
8501 | *_u += USEC_PER_DAY; | |
8502 | } | |
8503 | ||
8504 | return 0; | |
8505 | } | |
8506 | ||
e4b61340 | 8507 | static int shutdown_parse_argv(int argc, char *argv[]) { |
e4b61340 LP |
8508 | enum { |
8509 | ARG_HELP = 0x100, | |
514f4ef5 | 8510 | ARG_NO_WALL |
e4b61340 LP |
8511 | }; |
8512 | ||
8513 | static const struct option options[] = { | |
8514 | { "help", no_argument, NULL, ARG_HELP }, | |
8515 | { "halt", no_argument, NULL, 'H' }, | |
8516 | { "poweroff", no_argument, NULL, 'P' }, | |
8517 | { "reboot", no_argument, NULL, 'r' }, | |
04ebb595 | 8518 | { "kexec", no_argument, NULL, 'K' }, /* not documented extension */ |
514f4ef5 | 8519 | { "no-wall", no_argument, NULL, ARG_NO_WALL }, |
eb9da376 | 8520 | {} |
e4b61340 LP |
8521 | }; |
8522 | ||
172d7abf | 8523 | char **wall = NULL; |
f6144808 | 8524 | int c, r; |
e4b61340 LP |
8525 | |
8526 | assert(argc >= 0); | |
8527 | assert(argv); | |
8528 | ||
b068c6f5 | 8529 | while ((c = getopt_long(argc, argv, "HPrhkKat:fFc", options, NULL)) >= 0) |
e4b61340 LP |
8530 | switch (c) { |
8531 | ||
8532 | case ARG_HELP: | |
37ec0fdd | 8533 | return shutdown_help(); |
e4b61340 LP |
8534 | |
8535 | case 'H': | |
8536 | arg_action = ACTION_HALT; | |
8537 | break; | |
8538 | ||
8539 | case 'P': | |
8540 | arg_action = ACTION_POWEROFF; | |
8541 | break; | |
8542 | ||
8543 | case 'r': | |
5622dde3 KS |
8544 | if (kexec_loaded()) |
8545 | arg_action = ACTION_KEXEC; | |
8546 | else | |
8547 | arg_action = ACTION_REBOOT; | |
e4b61340 LP |
8548 | break; |
8549 | ||
04ebb595 LP |
8550 | case 'K': |
8551 | arg_action = ACTION_KEXEC; | |
8552 | break; | |
8553 | ||
e4b61340 LP |
8554 | case 'h': |
8555 | if (arg_action != ACTION_HALT) | |
8556 | arg_action = ACTION_POWEROFF; | |
8557 | break; | |
8558 | ||
8559 | case 'k': | |
1ae17672 | 8560 | arg_dry_run = true; |
e4b61340 LP |
8561 | break; |
8562 | ||
514f4ef5 LP |
8563 | case ARG_NO_WALL: |
8564 | arg_no_wall = true; | |
8565 | break; | |
8566 | ||
e4b61340 | 8567 | case 'a': |
b068c6f5 | 8568 | case 't': /* Note that we also ignore any passed argument to -t, not just the -t itself */ |
75836b9d JS |
8569 | case 'f': |
8570 | case 'F': | |
e4b61340 LP |
8571 | /* Compatibility nops */ |
8572 | break; | |
8573 | ||
f6144808 LP |
8574 | case 'c': |
8575 | arg_action = ACTION_CANCEL_SHUTDOWN; | |
8576 | break; | |
8577 | ||
e4b61340 LP |
8578 | case '?': |
8579 | return -EINVAL; | |
8580 | ||
8581 | default: | |
eb9da376 | 8582 | assert_not_reached("Unhandled option"); |
e4b61340 | 8583 | } |
e4b61340 | 8584 | |
dfcc5c33 | 8585 | if (argc > optind && arg_action != ACTION_CANCEL_SHUTDOWN) { |
2cc7b0a2 | 8586 | r = parse_shutdown_time_spec(argv[optind], &arg_when); |
7e59bfcb | 8587 | if (r < 0) { |
f6144808 LP |
8588 | log_error("Failed to parse time specification: %s", argv[optind]); |
8589 | return r; | |
8590 | } | |
6b5ad000 | 8591 | } else |
08e4b1c5 | 8592 | arg_when = now(CLOCK_REALTIME) + USEC_PER_MINUTE; |
442b9094 | 8593 | |
dfcc5c33 MS |
8594 | if (argc > optind && arg_action == ACTION_CANCEL_SHUTDOWN) |
8595 | /* No time argument for shutdown cancel */ | |
172d7abf | 8596 | wall = argv + optind; |
dfcc5c33 MS |
8597 | else if (argc > optind + 1) |
8598 | /* We skip the time argument */ | |
172d7abf LP |
8599 | wall = argv + optind + 1; |
8600 | ||
8601 | if (wall) { | |
8602 | arg_wall = strv_copy(wall); | |
8603 | if (!arg_wall) | |
8604 | return log_oom(); | |
8605 | } | |
e4b61340 LP |
8606 | |
8607 | optind = argc; | |
8608 | ||
8609 | return 1; | |
e4b61340 LP |
8610 | } |
8611 | ||
8612 | static int telinit_parse_argv(int argc, char *argv[]) { | |
e4b61340 LP |
8613 | enum { |
8614 | ARG_HELP = 0x100, | |
514f4ef5 | 8615 | ARG_NO_WALL |
e4b61340 LP |
8616 | }; |
8617 | ||
8618 | static const struct option options[] = { | |
8619 | { "help", no_argument, NULL, ARG_HELP }, | |
514f4ef5 | 8620 | { "no-wall", no_argument, NULL, ARG_NO_WALL }, |
eb9da376 | 8621 | {} |
e4b61340 LP |
8622 | }; |
8623 | ||
8624 | static const struct { | |
8625 | char from; | |
8626 | enum action to; | |
8627 | } table[] = { | |
8628 | { '0', ACTION_POWEROFF }, | |
8629 | { '6', ACTION_REBOOT }, | |
ef2f1067 | 8630 | { '1', ACTION_RESCUE }, |
e4b61340 LP |
8631 | { '2', ACTION_RUNLEVEL2 }, |
8632 | { '3', ACTION_RUNLEVEL3 }, | |
8633 | { '4', ACTION_RUNLEVEL4 }, | |
8634 | { '5', ACTION_RUNLEVEL5 }, | |
8635 | { 's', ACTION_RESCUE }, | |
8636 | { 'S', ACTION_RESCUE }, | |
8637 | { 'q', ACTION_RELOAD }, | |
8638 | { 'Q', ACTION_RELOAD }, | |
8639 | { 'u', ACTION_REEXEC }, | |
8640 | { 'U', ACTION_REEXEC } | |
8641 | }; | |
8642 | ||
8643 | unsigned i; | |
8644 | int c; | |
8645 | ||
8646 | assert(argc >= 0); | |
8647 | assert(argv); | |
8648 | ||
601185b4 | 8649 | while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) |
e4b61340 LP |
8650 | switch (c) { |
8651 | ||
8652 | case ARG_HELP: | |
37ec0fdd | 8653 | return telinit_help(); |
e4b61340 | 8654 | |
514f4ef5 LP |
8655 | case ARG_NO_WALL: |
8656 | arg_no_wall = true; | |
8657 | break; | |
8658 | ||
e4b61340 LP |
8659 | case '?': |
8660 | return -EINVAL; | |
8661 | ||
8662 | default: | |
eb9da376 | 8663 | assert_not_reached("Unhandled option"); |
e4b61340 | 8664 | } |
e4b61340 | 8665 | |
baaa35ad ZJS |
8666 | if (optind >= argc) |
8667 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8668 | "%s: required argument missing.", | |
8669 | program_invocation_short_name); | |
e4b61340 | 8670 | |
baaa35ad ZJS |
8671 | if (optind + 1 < argc) |
8672 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8673 | "Too many arguments."); | |
e4b61340 | 8674 | |
baaa35ad ZJS |
8675 | if (strlen(argv[optind]) != 1) |
8676 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8677 | "Expected single character argument."); | |
e4b61340 LP |
8678 | |
8679 | for (i = 0; i < ELEMENTSOF(table); i++) | |
8680 | if (table[i].from == argv[optind][0]) | |
8681 | break; | |
8682 | ||
baaa35ad ZJS |
8683 | if (i >= ELEMENTSOF(table)) |
8684 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8685 | "Unknown command '%s'.", argv[optind]); | |
e4b61340 LP |
8686 | |
8687 | arg_action = table[i].to; | |
8688 | ||
313cefa1 | 8689 | optind++; |
e4b61340 LP |
8690 | |
8691 | return 1; | |
8692 | } | |
8693 | ||
8694 | static int runlevel_parse_argv(int argc, char *argv[]) { | |
e4b61340 LP |
8695 | enum { |
8696 | ARG_HELP = 0x100, | |
8697 | }; | |
8698 | ||
8699 | static const struct option options[] = { | |
8700 | { "help", no_argument, NULL, ARG_HELP }, | |
eb9da376 | 8701 | {} |
e4b61340 LP |
8702 | }; |
8703 | ||
8704 | int c; | |
8705 | ||
8706 | assert(argc >= 0); | |
8707 | assert(argv); | |
8708 | ||
601185b4 | 8709 | while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) |
e4b61340 LP |
8710 | switch (c) { |
8711 | ||
8712 | case ARG_HELP: | |
37ec0fdd | 8713 | return runlevel_help(); |
e4b61340 LP |
8714 | |
8715 | case '?': | |
8716 | return -EINVAL; | |
8717 | ||
8718 | default: | |
eb9da376 | 8719 | assert_not_reached("Unhandled option"); |
e4b61340 | 8720 | } |
e4b61340 | 8721 | |
baaa35ad ZJS |
8722 | if (optind < argc) |
8723 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8724 | "Too many arguments."); | |
e4b61340 LP |
8725 | |
8726 | return 1; | |
8727 | } | |
8728 | ||
8729 | static int parse_argv(int argc, char *argv[]) { | |
8730 | assert(argc >= 0); | |
8731 | assert(argv); | |
8732 | ||
8733 | if (program_invocation_short_name) { | |
8734 | ||
8735 | if (strstr(program_invocation_short_name, "halt")) { | |
8736 | arg_action = ACTION_HALT; | |
8737 | return halt_parse_argv(argc, argv); | |
c54819ca | 8738 | |
e4b61340 LP |
8739 | } else if (strstr(program_invocation_short_name, "poweroff")) { |
8740 | arg_action = ACTION_POWEROFF; | |
8741 | return halt_parse_argv(argc, argv); | |
c54819ca | 8742 | |
e4b61340 | 8743 | } else if (strstr(program_invocation_short_name, "reboot")) { |
5622dde3 KS |
8744 | if (kexec_loaded()) |
8745 | arg_action = ACTION_KEXEC; | |
8746 | else | |
8747 | arg_action = ACTION_REBOOT; | |
e4b61340 | 8748 | return halt_parse_argv(argc, argv); |
c54819ca | 8749 | |
e4b61340 LP |
8750 | } else if (strstr(program_invocation_short_name, "shutdown")) { |
8751 | arg_action = ACTION_POWEROFF; | |
8752 | return shutdown_parse_argv(argc, argv); | |
c54819ca | 8753 | |
e4b61340 | 8754 | } else if (strstr(program_invocation_short_name, "init")) { |
d5ca5f11 | 8755 | |
c54819ca LP |
8756 | /* Matches invocations as "init" as well as "telinit", which are synonymous when run as PID != |
8757 | * 1 on SysV. | |
8758 | * | |
8759 | * On SysV "telinit" was the official command to communicate with PID 1, but "init" would | |
8760 | * redirect itself to "telinit" if called with PID != 1. We follow the same logic here still, | |
8761 | * though we add one level of indirection, as we implement "telinit" in "systemctl". Hence, for | |
8762 | * us if you invoke "init" you get "systemd", but it will execve() "systemctl" immediately with | |
8763 | * argv[] unmodified if PID is != 1. If you invoke "telinit" you directly get "systemctl". In | |
8764 | * both cases we shall do the same thing, which is why we do strstr(p_i_s_n, "init") here, as a | |
8765 | * quick way to match both. | |
8766 | * | |
8767 | * Also see redirect_telinit() in src/core/main.c. */ | |
8768 | ||
d5ca5f11 | 8769 | if (sd_booted() > 0) { |
f459b602 | 8770 | arg_action = _ACTION_INVALID; |
d5ca5f11 LP |
8771 | return telinit_parse_argv(argc, argv); |
8772 | } else { | |
c54819ca LP |
8773 | /* Hmm, so some other init system is running, we need to forward this request to |
8774 | * it. For now we simply guess that it is Upstart. */ | |
d5ca5f11 | 8775 | |
595225af | 8776 | (void) rlimit_nofile_safe(); |
4ad61fd1 | 8777 | execv(TELINIT, argv); |
d5ca5f11 | 8778 | |
baaa35ad ZJS |
8779 | return log_error_errno(SYNTHETIC_ERRNO(EIO), |
8780 | "Couldn't find an alternative telinit implementation to spawn."); | |
d5ca5f11 LP |
8781 | } |
8782 | ||
e4b61340 LP |
8783 | } else if (strstr(program_invocation_short_name, "runlevel")) { |
8784 | arg_action = ACTION_RUNLEVEL; | |
8785 | return runlevel_parse_argv(argc, argv); | |
8786 | } | |
8787 | } | |
8788 | ||
8789 | arg_action = ACTION_SYSTEMCTL; | |
8790 | return systemctl_parse_argv(argc, argv); | |
8791 | } | |
8792 | ||
349cc4a5 | 8793 | #if HAVE_SYSV_COMPAT |
44a6b1b6 | 8794 | _pure_ static int action_to_runlevel(void) { |
eb22ac37 LP |
8795 | static const char table[_ACTION_MAX] = { |
8796 | [ACTION_HALT] = '0', | |
8797 | [ACTION_POWEROFF] = '0', | |
8798 | [ACTION_REBOOT] = '6', | |
8799 | [ACTION_RUNLEVEL2] = '2', | |
8800 | [ACTION_RUNLEVEL3] = '3', | |
8801 | [ACTION_RUNLEVEL4] = '4', | |
8802 | [ACTION_RUNLEVEL5] = '5', | |
8803 | [ACTION_RESCUE] = '1' | |
8804 | }; | |
8805 | ||
78ca9099 | 8806 | assert(arg_action >= 0 && arg_action < _ACTION_MAX); |
d55ae9e6 LP |
8807 | |
8808 | return table[arg_action]; | |
8809 | } | |
d2e79673 | 8810 | #endif |
d55ae9e6 | 8811 | |
d55ae9e6 | 8812 | static int talk_initctl(void) { |
349cc4a5 | 8813 | #if HAVE_SYSV_COMPAT |
cbc9fbd1 LP |
8814 | struct init_request request = { |
8815 | .magic = INIT_MAGIC, | |
8816 | .sleeptime = 0, | |
8817 | .cmd = INIT_CMD_RUNLVL | |
8818 | }; | |
8819 | ||
7fd1b19b | 8820 | _cleanup_close_ int fd = -1; |
d55ae9e6 | 8821 | char rl; |
cbc9fbd1 | 8822 | int r; |
83c76e8c | 8823 | const char *p; |
eb22ac37 | 8824 | |
427b47c4 ZJS |
8825 | rl = action_to_runlevel(); |
8826 | if (!rl) | |
eb22ac37 LP |
8827 | return 0; |
8828 | ||
d55ae9e6 LP |
8829 | request.runlevel = rl; |
8830 | ||
83c76e8c MG |
8831 | FOREACH_STRING(p, "/run/initctl", "/dev/initctl") { |
8832 | fd = open(p, O_WRONLY|O_NONBLOCK|O_CLOEXEC|O_NOCTTY); | |
8833 | if (fd >= 0 || errno != ENOENT) | |
8834 | break; | |
8835 | } | |
427b47c4 | 8836 | if (fd < 0) { |
d55ae9e6 LP |
8837 | if (errno == ENOENT) |
8838 | return 0; | |
eb22ac37 | 8839 | |
83c76e8c | 8840 | return log_error_errno(errno, "Failed to open initctl fifo: %m"); |
d55ae9e6 | 8841 | } |
eb22ac37 | 8842 | |
553acb7b ZJS |
8843 | r = loop_write(fd, &request, sizeof(request), false); |
8844 | if (r < 0) | |
83c76e8c | 8845 | return log_error_errno(r, "Failed to write to %s: %m", p); |
eb22ac37 LP |
8846 | |
8847 | return 1; | |
eca830be LP |
8848 | #else |
8849 | return 0; | |
8850 | #endif | |
e4b61340 LP |
8851 | } |
8852 | ||
e449de87 | 8853 | static int systemctl_main(int argc, char *argv[]) { |
e449de87 | 8854 | static const Verb verbs[] = { |
c56d1e2c CW |
8855 | { "list-units", VERB_ANY, VERB_ANY, VERB_DEFAULT|VERB_ONLINE_ONLY, list_units }, |
8856 | { "list-unit-files", VERB_ANY, VERB_ANY, 0, list_unit_files }, | |
8857 | { "list-sockets", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, list_sockets }, | |
8858 | { "list-timers", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, list_timers }, | |
8859 | { "list-jobs", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, list_jobs }, | |
8860 | { "list-machines", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY|VERB_MUST_BE_ROOT, list_machines }, | |
8861 | { "clear-jobs", VERB_ANY, 1, VERB_ONLINE_ONLY, trivial_method }, | |
8862 | { "cancel", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, cancel_job }, | |
8863 | { "start", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, | |
8864 | { "stop", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, | |
8865 | { "condstop", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, /* For compatibility with ALTLinux */ | |
8866 | { "reload", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, | |
8867 | { "restart", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, | |
8868 | { "try-restart", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, | |
8869 | { "reload-or-restart", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, | |
8870 | { "reload-or-try-restart", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, /* For compatbility with old systemctl <= 228 */ | |
8871 | { "try-reload-or-restart", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, | |
8872 | { "force-reload", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, /* For compatibility with SysV */ | |
8873 | { "condreload", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, /* For compatibility with ALTLinux */ | |
8874 | { "condrestart", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, /* For compatibility with RH */ | |
8875 | { "isolate", 2, 2, VERB_ONLINE_ONLY, start_unit }, | |
8876 | { "kill", 2, VERB_ANY, VERB_ONLINE_ONLY, kill_unit }, | |
8877 | { "is-active", 2, VERB_ANY, VERB_ONLINE_ONLY, check_unit_active }, | |
3e7e587d | 8878 | { "check", 2, VERB_ANY, VERB_ONLINE_ONLY, check_unit_active }, /* deprecated alias of is-active */ |
c56d1e2c CW |
8879 | { "is-failed", 2, VERB_ANY, VERB_ONLINE_ONLY, check_unit_failed }, |
8880 | { "show", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, show }, | |
8881 | { "cat", 2, VERB_ANY, VERB_ONLINE_ONLY, cat }, | |
8882 | { "status", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, show }, | |
8883 | { "help", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, show }, | |
8884 | { "daemon-reload", VERB_ANY, 1, VERB_ONLINE_ONLY, daemon_reload }, | |
8885 | { "daemon-reexec", VERB_ANY, 1, VERB_ONLINE_ONLY, daemon_reload }, | |
8886 | { "show-environment", VERB_ANY, 1, VERB_ONLINE_ONLY, show_environment }, | |
8887 | { "set-environment", 2, VERB_ANY, VERB_ONLINE_ONLY, set_environment }, | |
8888 | { "unset-environment", 2, VERB_ANY, VERB_ONLINE_ONLY, set_environment }, | |
8889 | { "import-environment", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, import_environment }, | |
8890 | { "halt", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, | |
8891 | { "poweroff", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, | |
8892 | { "reboot", VERB_ANY, 2, VERB_ONLINE_ONLY, start_system_special }, | |
8893 | { "kexec", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, | |
8894 | { "suspend", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, | |
8895 | { "hibernate", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, | |
8896 | { "hybrid-sleep", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, | |
e68c79db | 8897 | { "suspend-then-hibernate",VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, |
c56d1e2c CW |
8898 | { "default", VERB_ANY, 1, VERB_ONLINE_ONLY, start_special }, |
8899 | { "rescue", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, | |
8900 | { "emergency", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, | |
8901 | { "exit", VERB_ANY, 2, VERB_ONLINE_ONLY, start_special }, | |
8902 | { "reset-failed", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, reset_failed }, | |
8903 | { "enable", 2, VERB_ANY, 0, enable_unit }, | |
8904 | { "disable", 2, VERB_ANY, 0, enable_unit }, | |
8905 | { "is-enabled", 2, VERB_ANY, 0, unit_is_enabled }, | |
8906 | { "reenable", 2, VERB_ANY, 0, enable_unit }, | |
8907 | { "preset", 2, VERB_ANY, 0, enable_unit }, | |
8908 | { "preset-all", VERB_ANY, 1, 0, preset_all }, | |
8909 | { "mask", 2, VERB_ANY, 0, enable_unit }, | |
8910 | { "unmask", 2, VERB_ANY, 0, enable_unit }, | |
8911 | { "link", 2, VERB_ANY, 0, enable_unit }, | |
8912 | { "revert", 2, VERB_ANY, 0, enable_unit }, | |
8913 | { "switch-root", 2, VERB_ANY, VERB_ONLINE_ONLY, switch_root }, | |
8914 | { "list-dependencies", VERB_ANY, 2, VERB_ONLINE_ONLY, list_dependencies }, | |
8915 | { "set-default", 2, 2, 0, set_default }, | |
8916 | { "get-default", VERB_ANY, 1, 0, get_default }, | |
8917 | { "set-property", 3, VERB_ANY, VERB_ONLINE_ONLY, set_property }, | |
8918 | { "is-system-running", VERB_ANY, 1, 0, is_system_running }, | |
8919 | { "add-wants", 3, VERB_ANY, 0, add_dependency }, | |
8920 | { "add-requires", 3, VERB_ANY, 0, add_dependency }, | |
8921 | { "edit", 2, VERB_ANY, VERB_ONLINE_ONLY, edit }, | |
d08e75ed | 8922 | {} |
e449de87 | 8923 | }; |
7e4249b9 | 8924 | |
e449de87 | 8925 | return dispatch_verb(argc, argv, verbs, NULL); |
e4b61340 LP |
8926 | } |
8927 | ||
4fbd7192 | 8928 | static int reload_with_fallback(void) { |
4fbd7192 | 8929 | /* First, try systemd via D-Bus. */ |
e449de87 | 8930 | if (daemon_reload(0, NULL, NULL) >= 0) |
4fbd7192 | 8931 | return 0; |
e4b61340 LP |
8932 | |
8933 | /* Nothing else worked, so let's try signals */ | |
2853b60a | 8934 | assert(IN_SET(arg_action, ACTION_RELOAD, ACTION_REEXEC)); |
e4b61340 | 8935 | |
4a62c710 MS |
8936 | if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) |
8937 | return log_error_errno(errno, "kill() failed: %m"); | |
e4b61340 LP |
8938 | |
8939 | return 0; | |
8940 | } | |
8941 | ||
4fbd7192 | 8942 | static int start_with_fallback(void) { |
4fbd7192 | 8943 | /* First, try systemd via D-Bus. */ |
2e13c0c3 | 8944 | if (start_unit(0, NULL, NULL) == 0) |
4fbd7192 | 8945 | return 0; |
e4b61340 | 8946 | |
2853b60a | 8947 | /* Nothing else worked, so let's try /dev/initctl */ |
fbc43921 | 8948 | if (talk_initctl() > 0) |
48ec22bc | 8949 | return 0; |
d55ae9e6 | 8950 | |
baaa35ad ZJS |
8951 | return log_error_errno(SYNTHETIC_ERRNO(EIO), |
8952 | "Failed to talk to init daemon."); | |
e4b61340 LP |
8953 | } |
8954 | ||
477def80 | 8955 | static int halt_now(enum action a) { |
c01dcddf | 8956 | /* The kernel will automatically flush ATA disks and suchlike on reboot(), but the file systems need to be |
f03ec2b1 | 8957 | * synced explicitly in advance. */ |
1ae17672 | 8958 | if (!arg_no_sync && !arg_dry_run) |
f3f054f0 | 8959 | (void) sync(); |
4a3ad399 | 8960 | |
1ae17672 ZJS |
8961 | /* Make sure C-A-D is handled by the kernel from this point on... */ |
8962 | if (!arg_dry_run) | |
8963 | (void) reboot(RB_ENABLE_CAD); | |
e606bb61 | 8964 | |
4c80c73c | 8965 | switch (a) { |
e606bb61 LP |
8966 | |
8967 | case ACTION_HALT: | |
60675884 LP |
8968 | if (!arg_quiet) |
8969 | log_info("Halting."); | |
1ae17672 ZJS |
8970 | if (arg_dry_run) |
8971 | return 0; | |
19578bb2 | 8972 | (void) reboot(RB_HALT_SYSTEM); |
477def80 | 8973 | return -errno; |
e606bb61 LP |
8974 | |
8975 | case ACTION_POWEROFF: | |
60675884 LP |
8976 | if (!arg_quiet) |
8977 | log_info("Powering off."); | |
1ae17672 ZJS |
8978 | if (arg_dry_run) |
8979 | return 0; | |
19578bb2 | 8980 | (void) reboot(RB_POWER_OFF); |
477def80 | 8981 | return -errno; |
e606bb61 | 8982 | |
98d52feb | 8983 | case ACTION_KEXEC: |
c01dcddf LP |
8984 | case ACTION_REBOOT: |
8985 | return reboot_with_parameter(REBOOT_FALLBACK | | |
8986 | (arg_quiet ? 0 : REBOOT_LOG) | | |
8987 | (arg_dry_run ? REBOOT_DRY_RUN : 0)); | |
e606bb61 | 8988 | |
477def80 LP |
8989 | default: |
8990 | assert_not_reached("Unknown action."); | |
8991 | } | |
e606bb61 LP |
8992 | } |
8993 | ||
56a730fa | 8994 | static int logind_schedule_shutdown(void) { |
7f56c3d4 | 8995 | |
cf9cda44 | 8996 | #if ENABLE_LOGIND |
4afd3348 | 8997 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
56a730fa LP |
8998 | char date[FORMAT_TIMESTAMP_MAX]; |
8999 | const char *action; | |
4fbd7192 | 9000 | sd_bus *bus; |
56a730fa LP |
9001 | int r; |
9002 | ||
4fbd7192 | 9003 | r = acquire_bus(BUS_FULL, &bus); |
56a730fa | 9004 | if (r < 0) |
4fbd7192 | 9005 | return r; |
56a730fa LP |
9006 | |
9007 | switch (arg_action) { | |
9008 | case ACTION_HALT: | |
9009 | action = "halt"; | |
9010 | break; | |
9011 | case ACTION_POWEROFF: | |
9012 | action = "poweroff"; | |
9013 | break; | |
9014 | case ACTION_KEXEC: | |
9015 | action = "kexec"; | |
9016 | break; | |
a4420f7b LP |
9017 | case ACTION_EXIT: |
9018 | action = "exit"; | |
9019 | break; | |
9020 | case ACTION_REBOOT: | |
56a730fa LP |
9021 | default: |
9022 | action = "reboot"; | |
9023 | break; | |
9024 | } | |
9025 | ||
1ae17672 | 9026 | if (arg_dry_run) |
56a730fa LP |
9027 | action = strjoina("dry-", action); |
9028 | ||
d2ad7e1b ZJS |
9029 | (void) logind_set_wall_message(); |
9030 | ||
56a730fa | 9031 | r = sd_bus_call_method( |
4fbd7192 | 9032 | bus, |
56a730fa LP |
9033 | "org.freedesktop.login1", |
9034 | "/org/freedesktop/login1", | |
9035 | "org.freedesktop.login1.Manager", | |
9036 | "ScheduleShutdown", | |
9037 | &error, | |
9038 | NULL, | |
9039 | "st", | |
9040 | action, | |
9041 | arg_when); | |
9042 | if (r < 0) | |
9043 | return log_warning_errno(r, "Failed to call ScheduleShutdown in logind, proceeding with immediate shutdown: %s", bus_error_message(&error, r)); | |
9044 | ||
60675884 LP |
9045 | if (!arg_quiet) |
9046 | log_info("Shutdown scheduled for %s, use 'shutdown -c' to cancel.", format_timestamp(date, sizeof(date), arg_when)); | |
56a730fa | 9047 | return 0; |
cf9cda44 DJL |
9048 | #else |
9049 | log_error("Cannot schedule shutdown without logind support, proceeding with immediate shutdown."); | |
9050 | return -ENOSYS; | |
807690fb | 9051 | #endif |
56a730fa LP |
9052 | } |
9053 | ||
4fbd7192 | 9054 | static int halt_main(void) { |
e4b61340 LP |
9055 | int r; |
9056 | ||
4fbd7192 | 9057 | r = logind_check_inhibitors(arg_action); |
748ebafa LP |
9058 | if (r < 0) |
9059 | return r; | |
b37844d3 | 9060 | |
940bec70 DJL |
9061 | /* Delayed shutdown requested, and was successful */ |
9062 | if (arg_when > 0 && logind_schedule_shutdown() == 0) | |
9063 | return 0; | |
9064 | /* no delay, or logind failed or is not at all available */ | |
7f96539d | 9065 | |
bc8c2f5c | 9066 | if (geteuid() != 0) { |
1ae17672 | 9067 | if (arg_dry_run || arg_force > 0) { |
fba868fa | 9068 | (void) must_be_root(); |
2ac3930f IS |
9069 | return -EPERM; |
9070 | } | |
9071 | ||
7e59bfcb | 9072 | /* Try logind if we are a normal user and no special |
d35f51ea | 9073 | * mode applies. Maybe polkit allows us to shutdown |
7e59bfcb | 9074 | * the machine. */ |
36b69c31 | 9075 | if (IN_SET(arg_action, ACTION_POWEROFF, ACTION_REBOOT, ACTION_HALT)) { |
4fbd7192 | 9076 | r = logind_reboot(arg_action); |
4c80c73c KS |
9077 | if (r >= 0) |
9078 | return r; | |
a9085ea3 | 9079 | if (IN_SET(r, -EOPNOTSUPP, -EINPROGRESS)) |
7f96539d LP |
9080 | /* requested operation is not |
9081 | * supported on the local system or | |
9082 | * already in progress */ | |
a9085ea3 IS |
9083 | return r; |
9084 | /* on all other errors, try low-level operation */ | |
4c80c73c | 9085 | } |
bc8c2f5c LP |
9086 | } |
9087 | ||
2955e0d4 GJ |
9088 | /* In order to minimize the difference between operation with and |
9089 | * without logind, we explicitly enable non-blocking mode for this, | |
9090 | * as logind's shutdown operations are always non-blocking. */ | |
9091 | arg_no_block = true; | |
9092 | ||
1ae17672 | 9093 | if (!arg_dry_run && !arg_force) |
4fbd7192 | 9094 | return start_with_fallback(); |
e4b61340 | 9095 | |
2ac3930f IS |
9096 | assert(geteuid() == 0); |
9097 | ||
d90e1a30 LP |
9098 | if (!arg_no_wtmp) { |
9099 | if (sd_booted() > 0) | |
9100 | log_debug("Not writing utmp record, assuming that systemd-update-utmp is used."); | |
7e59bfcb LP |
9101 | else { |
9102 | r = utmp_put_shutdown(); | |
9103 | if (r < 0) | |
da927ba9 | 9104 | log_warning_errno(r, "Failed to write utmp record: %m"); |
7e59bfcb | 9105 | } |
d90e1a30 | 9106 | } |
e4b61340 | 9107 | |
1ae17672 | 9108 | if (arg_dry_run) |
e4b61340 LP |
9109 | return 0; |
9110 | ||
477def80 | 9111 | r = halt_now(arg_action); |
691395d8 | 9112 | return log_error_errno(r, "Failed to reboot: %m"); |
e4b61340 LP |
9113 | } |
9114 | ||
9115 | static int runlevel_main(void) { | |
9116 | int r, runlevel, previous; | |
9117 | ||
729e3769 LP |
9118 | r = utmp_get_runlevel(&runlevel, &previous); |
9119 | if (r < 0) { | |
9120 | puts("unknown"); | |
e4b61340 LP |
9121 | return r; |
9122 | } | |
9123 | ||
9124 | printf("%c %c\n", | |
9125 | previous <= 0 ? 'N' : previous, | |
9126 | runlevel <= 0 ? 'N' : runlevel); | |
9127 | ||
9128 | return 0; | |
9129 | } | |
9130 | ||
2cf05793 | 9131 | static int logind_cancel_shutdown(void) { |
349cc4a5 | 9132 | #if ENABLE_LOGIND |
4afd3348 | 9133 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
4fbd7192 | 9134 | sd_bus *bus; |
949d9ce9 LP |
9135 | int r; |
9136 | ||
4fbd7192 | 9137 | r = acquire_bus(BUS_FULL, &bus); |
949d9ce9 | 9138 | if (r < 0) |
4fbd7192 | 9139 | return r; |
949d9ce9 | 9140 | |
4fbd7192 | 9141 | (void) logind_set_wall_message(); |
949d9ce9 LP |
9142 | |
9143 | r = sd_bus_call_method( | |
4fbd7192 | 9144 | bus, |
949d9ce9 LP |
9145 | "org.freedesktop.login1", |
9146 | "/org/freedesktop/login1", | |
9147 | "org.freedesktop.login1.Manager", | |
9148 | "CancelScheduledShutdown", | |
9149 | &error, | |
9150 | NULL, NULL); | |
9151 | if (r < 0) | |
9152 | return log_warning_errno(r, "Failed to talk to logind, shutdown hasn't been cancelled: %s", bus_error_message(&error, r)); | |
9153 | ||
9154 | return 0; | |
2cf05793 LP |
9155 | #else |
9156 | log_error("Not compiled with logind support, cannot cancel scheduled shutdowns."); | |
9157 | return -ENOSYS; | |
9158 | #endif | |
949d9ce9 LP |
9159 | } |
9160 | ||
f2a3de01 | 9161 | static int run(int argc, char *argv[]) { |
f459b602 | 9162 | int r; |
e4b61340 | 9163 | |
acc28e2e FB |
9164 | argv_cmdline = argv[0]; |
9165 | ||
a9cdc94f | 9166 | setlocale(LC_ALL, ""); |
e4b61340 | 9167 | log_parse_environment(); |
2396fb04 | 9168 | log_open(); |
1abaf488 LP |
9169 | |
9170 | /* The journal merging logic potentially needs a lot of fds. */ | |
9171 | (void) rlimit_nofile_bump(HIGH_RLIMIT_NOFILE); | |
9172 | ||
592705f2 | 9173 | sigbus_install(); |
e4b61340 | 9174 | |
184ecaf7 DR |
9175 | /* Explicitly not on_tty() to avoid setting cached value. |
9176 | * This becomes relevant for piping output which might be | |
9177 | * ellipsized. */ | |
9178 | original_stdout_is_tty = isatty(STDOUT_FILENO); | |
9179 | ||
04ebb595 | 9180 | r = parse_argv(argc, argv); |
f459b602 | 9181 | if (r <= 0) |
e4b61340 | 9182 | goto finish; |
7e4249b9 | 9183 | |
040524b4 | 9184 | if (arg_action != ACTION_SYSTEMCTL && running_in_chroot() > 0) { |
60675884 LP |
9185 | if (!arg_quiet) |
9186 | log_info("Running in chroot, ignoring request."); | |
f459b602 | 9187 | r = 0; |
82e23ddd LP |
9188 | goto finish; |
9189 | } | |
9190 | ||
41dd15e4 LP |
9191 | /* systemctl_main() will print an error message for the bus |
9192 | * connection, but only if it needs to */ | |
e4b61340 LP |
9193 | |
9194 | switch (arg_action) { | |
9195 | ||
22f4096c | 9196 | case ACTION_SYSTEMCTL: |
e449de87 | 9197 | r = systemctl_main(argc, argv); |
e4b61340 | 9198 | break; |
e4b61340 | 9199 | |
081dc638 AJ |
9200 | /* Legacy command aliases set arg_action. They provide some fallbacks, |
9201 | * e.g. to tell sysvinit to reboot after you have installed systemd | |
9202 | * binaries. */ | |
9203 | ||
e4b61340 LP |
9204 | case ACTION_HALT: |
9205 | case ACTION_POWEROFF: | |
9206 | case ACTION_REBOOT: | |
5622dde3 | 9207 | case ACTION_KEXEC: |
4fbd7192 | 9208 | r = halt_main(); |
e4b61340 LP |
9209 | break; |
9210 | ||
e4b61340 LP |
9211 | case ACTION_RUNLEVEL2: |
9212 | case ACTION_RUNLEVEL3: | |
9213 | case ACTION_RUNLEVEL4: | |
9214 | case ACTION_RUNLEVEL5: | |
9215 | case ACTION_RESCUE: | |
4fbd7192 | 9216 | r = start_with_fallback(); |
e4b61340 | 9217 | break; |
7e4249b9 | 9218 | |
e4b61340 LP |
9219 | case ACTION_RELOAD: |
9220 | case ACTION_REEXEC: | |
4fbd7192 | 9221 | r = reload_with_fallback(); |
e4b61340 LP |
9222 | break; |
9223 | ||
949d9ce9 | 9224 | case ACTION_CANCEL_SHUTDOWN: |
2cf05793 | 9225 | r = logind_cancel_shutdown(); |
f6144808 LP |
9226 | break; |
9227 | ||
eb22ac37 | 9228 | case ACTION_RUNLEVEL: |
4f16c1f4 LP |
9229 | r = runlevel_main(); |
9230 | break; | |
9231 | ||
081dc638 AJ |
9232 | case ACTION_EXIT: |
9233 | case ACTION_SUSPEND: | |
9234 | case ACTION_HIBERNATE: | |
9235 | case ACTION_HYBRID_SLEEP: | |
e68c79db | 9236 | case ACTION_SUSPEND_THEN_HIBERNATE: |
081dc638 AJ |
9237 | case ACTION_EMERGENCY: |
9238 | case ACTION_DEFAULT: | |
9239 | /* systemctl verbs with no equivalent in the legacy commands. | |
9240 | * These cannot appear in arg_action. Fall through. */ | |
9241 | ||
f459b602 | 9242 | case _ACTION_INVALID: |
e4b61340 LP |
9243 | default: |
9244 | assert_not_reached("Unknown action"); | |
9245 | } | |
7e4249b9 LP |
9246 | |
9247 | finish: | |
cf647b69 LP |
9248 | release_busses(); |
9249 | ||
d1405af3 ZJS |
9250 | /* Note that we return r here, not 0, so that we can implement the LSB-like return codes */ |
9251 | return r; | |
7e4249b9 | 9252 | } |
d1405af3 ZJS |
9253 | |
9254 | DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run); |