]>
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; |
4445a875 | 144 | static enum action { |
e4b61340 LP |
145 | ACTION_SYSTEMCTL, |
146 | ACTION_HALT, | |
147 | ACTION_POWEROFF, | |
148 | ACTION_REBOOT, | |
20b09ca7 LP |
149 | ACTION_KEXEC, |
150 | ACTION_EXIT, | |
6edd7d0a LP |
151 | ACTION_SUSPEND, |
152 | ACTION_HIBERNATE, | |
6524990f | 153 | ACTION_HYBRID_SLEEP, |
e68c79db | 154 | ACTION_SUSPEND_THEN_HIBERNATE, |
e4b61340 LP |
155 | ACTION_RUNLEVEL2, |
156 | ACTION_RUNLEVEL3, | |
157 | ACTION_RUNLEVEL4, | |
158 | ACTION_RUNLEVEL5, | |
159 | ACTION_RESCUE, | |
514f4ef5 LP |
160 | ACTION_EMERGENCY, |
161 | ACTION_DEFAULT, | |
e4b61340 LP |
162 | ACTION_RELOAD, |
163 | ACTION_REEXEC, | |
164 | ACTION_RUNLEVEL, | |
f6144808 | 165 | ACTION_CANCEL_SHUTDOWN, |
913c1916 AJ |
166 | _ACTION_MAX, |
167 | _ACTION_INVALID = -1 | |
e4b61340 | 168 | } arg_action = ACTION_SYSTEMCTL; |
f459b602 | 169 | static BusTransport arg_transport = BUS_TRANSPORT_LOCAL; |
fc2ffaf1 | 170 | static const char *arg_host = NULL; |
df50185b LP |
171 | static unsigned arg_lines = 10; |
172 | static OutputMode arg_output = OUTPUT_SHORT; | |
5d0c05e5 | 173 | static bool arg_plain = false; |
5bdf2243 | 174 | static bool arg_firmware_setup = false; |
97af80c5 LP |
175 | static usec_t arg_boot_loader_menu = USEC_INFINITY; |
176 | static const char *arg_boot_loader_entry = NULL; | |
57ab2eab | 177 | static bool arg_now = false; |
82948f6c LP |
178 | static bool arg_jobs_before = false; |
179 | static bool arg_jobs_after = false; | |
1238ee09 | 180 | |
61ff45db LP |
181 | STATIC_DESTRUCTOR_REGISTER(arg_wall, strv_freep); |
182 | STATIC_DESTRUCTOR_REGISTER(arg_root, freep); | |
183 | STATIC_DESTRUCTOR_REGISTER(arg_types, strv_freep); | |
184 | STATIC_DESTRUCTOR_REGISTER(arg_states, strv_freep); | |
185 | STATIC_DESTRUCTOR_REGISTER(arg_properties, strv_freep); | |
186 | ||
e449de87 | 187 | static int daemon_reload(int argc, char *argv[], void* userdata); |
2853b60a | 188 | static int trivial_method(int argc, char *argv[], void *userdata); |
477def80 | 189 | static int halt_now(enum action a); |
fa0d5878 | 190 | static int get_state_one_unit(sd_bus *bus, const char *name, UnitActiveState *active_state); |
dbc2c080 | 191 | |
4fbd7192 LP |
192 | static bool original_stdout_is_tty; |
193 | ||
194 | typedef enum BusFocus { | |
195 | BUS_FULL, /* The full bus indicated via --system or --user */ | |
196 | BUS_MANAGER, /* The manager itself, possibly directly, possibly via the bus */ | |
197 | _BUS_FOCUS_MAX | |
198 | } BusFocus; | |
199 | ||
200 | static sd_bus *busses[_BUS_FOCUS_MAX] = {}; | |
201 | ||
b3796dd8 JS |
202 | static UnitFileFlags args_to_flags(void) { |
203 | return (arg_runtime ? UNIT_FILE_RUNTIME : 0) | | |
204 | (arg_force ? UNIT_FILE_FORCE : 0); | |
205 | } | |
206 | ||
4fbd7192 LP |
207 | static int acquire_bus(BusFocus focus, sd_bus **ret) { |
208 | int r; | |
209 | ||
210 | assert(focus < _BUS_FOCUS_MAX); | |
211 | assert(ret); | |
212 | ||
213 | /* We only go directly to the manager, if we are using a local transport */ | |
214 | if (arg_transport != BUS_TRANSPORT_LOCAL) | |
215 | focus = BUS_FULL; | |
216 | ||
d4015567 LP |
217 | if (getenv_bool("SYSTEMCTL_FORCE_BUS") > 0) |
218 | focus = BUS_FULL; | |
219 | ||
4fbd7192 LP |
220 | if (!busses[focus]) { |
221 | bool user; | |
222 | ||
223 | user = arg_scope != UNIT_FILE_SYSTEM; | |
224 | ||
225 | if (focus == BUS_MANAGER) | |
226 | r = bus_connect_transport_systemd(arg_transport, arg_host, user, &busses[focus]); | |
227 | else | |
228 | r = bus_connect_transport(arg_transport, arg_host, user, &busses[focus]); | |
229 | if (r < 0) | |
230 | return log_error_errno(r, "Failed to connect to bus: %m"); | |
231 | ||
232 | (void) sd_bus_set_allow_interactive_authorization(busses[focus], arg_ask_password); | |
233 | } | |
234 | ||
235 | *ret = busses[focus]; | |
236 | return 0; | |
237 | } | |
238 | ||
239 | static void release_busses(void) { | |
240 | BusFocus w; | |
241 | ||
242 | for (w = 0; w < _BUS_FOCUS_MAX; w++) | |
243 | busses[w] = sd_bus_flush_close_unref(busses[w]); | |
244 | } | |
245 | ||
6bb92a16 | 246 | static void ask_password_agent_open_if_enabled(void) { |
729e3769 | 247 | /* Open the password agent as a child process if necessary */ |
501fc174 | 248 | |
1ae17672 ZJS |
249 | if (arg_dry_run) |
250 | return; | |
251 | ||
501fc174 LP |
252 | if (!arg_ask_password) |
253 | return; | |
715554e7 | 254 | |
729e3769 | 255 | if (arg_scope != UNIT_FILE_SYSTEM) |
501fc174 LP |
256 | return; |
257 | ||
cbc9fbd1 LP |
258 | if (arg_transport != BUS_TRANSPORT_LOCAL) |
259 | return; | |
260 | ||
6bb92a16 LP |
261 | ask_password_agent_open(); |
262 | } | |
263 | ||
8a4b13c5 | 264 | static void polkit_agent_open_maybe(void) { |
6bb92a16 LP |
265 | /* Open the polkit agent as a child process if necessary */ |
266 | ||
6bb92a16 LP |
267 | if (arg_scope != UNIT_FILE_SYSTEM) |
268 | return; | |
269 | ||
8a4b13c5 | 270 | polkit_agent_open_if_enabled(arg_transport, arg_ask_password); |
501fc174 LP |
271 | } |
272 | ||
3c756001 LP |
273 | static OutputFlags get_output_flags(void) { |
274 | return | |
275 | arg_all * OUTPUT_SHOW_ALL | | |
459b9f9f | 276 | (arg_full || !on_tty() || pager_have()) * OUTPUT_FULL_WIDTH | |
40c9fe4c | 277 | colors_enabled() * OUTPUT_COLOR | |
8b0cc9a3 | 278 | !arg_quiet * OUTPUT_WARN_CUTOFF; |
3c756001 LP |
279 | } |
280 | ||
f459b602 | 281 | static int translate_bus_error_to_exit_status(int r, const sd_bus_error *error) { |
22f4096c LP |
282 | assert(error); |
283 | ||
f459b602 | 284 | if (!sd_bus_error_is_set(error)) |
22f4096c LP |
285 | return r; |
286 | ||
f459b602 MAP |
287 | if (sd_bus_error_has_name(error, SD_BUS_ERROR_ACCESS_DENIED) || |
288 | sd_bus_error_has_name(error, BUS_ERROR_ONLY_BY_DEPENDENCY) || | |
289 | sd_bus_error_has_name(error, BUS_ERROR_NO_ISOLATION) || | |
290 | sd_bus_error_has_name(error, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE)) | |
22f4096c LP |
291 | return EXIT_NOPERMISSION; |
292 | ||
f459b602 | 293 | if (sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT)) |
22f4096c LP |
294 | return EXIT_NOTINSTALLED; |
295 | ||
f459b602 | 296 | if (sd_bus_error_has_name(error, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE) || |
718db961 | 297 | sd_bus_error_has_name(error, SD_BUS_ERROR_NOT_SUPPORTED)) |
22f4096c LP |
298 | return EXIT_NOTIMPLEMENTED; |
299 | ||
f459b602 | 300 | if (sd_bus_error_has_name(error, BUS_ERROR_LOAD_FAILED)) |
22f4096c LP |
301 | return EXIT_NOTCONFIGURED; |
302 | ||
303 | if (r != 0) | |
304 | return r; | |
305 | ||
306 | return EXIT_FAILURE; | |
307 | } | |
308 | ||
4fbd7192 | 309 | static bool install_client_side(void) { |
4fbd7192 LP |
310 | /* Decides when to execute enable/disable/... operations |
311 | * client-side rather than server-side. */ | |
4f16c1f4 | 312 | |
f38951a6 | 313 | if (running_in_chroot_or_offline()) |
729e3769 LP |
314 | return true; |
315 | ||
316 | if (sd_booted() <= 0) | |
317 | return true; | |
318 | ||
319 | if (!isempty(arg_root)) | |
320 | return true; | |
321 | ||
322 | if (arg_scope == UNIT_FILE_GLOBAL) | |
323 | return true; | |
324 | ||
b41b9d2a LP |
325 | /* Unsupported environment variable, mostly for debugging purposes */ |
326 | if (getenv_bool("SYSTEMCTL_INSTALL_CLIENT_SIDE") > 0) | |
327 | return true; | |
328 | ||
729e3769 LP |
329 | return false; |
330 | } | |
331 | ||
93bab288 | 332 | static int compare_unit_info(const UnitInfo *a, const UnitInfo *b) { |
36c32ba2 | 333 | const char *d1, *d2; |
1238ee09 LP |
334 | int r; |
335 | ||
336 | /* First, order by machine */ | |
93bab288 | 337 | if (!a->machine && b->machine) |
1238ee09 | 338 | return -1; |
93bab288 | 339 | if (a->machine && !b->machine) |
1238ee09 | 340 | return 1; |
93bab288 YW |
341 | if (a->machine && b->machine) { |
342 | r = strcasecmp(a->machine, b->machine); | |
1238ee09 LP |
343 | if (r != 0) |
344 | return r; | |
345 | } | |
36c32ba2 | 346 | |
1238ee09 | 347 | /* Second, order by unit type */ |
93bab288 YW |
348 | d1 = strrchr(a->id, '.'); |
349 | d2 = strrchr(b->id, '.'); | |
36c32ba2 | 350 | if (d1 && d2) { |
f84190d8 LP |
351 | r = strcasecmp(d1, d2); |
352 | if (r != 0) | |
36c32ba2 LP |
353 | return r; |
354 | } | |
355 | ||
1238ee09 | 356 | /* Third, order by name */ |
93bab288 | 357 | return strcasecmp(a->id, b->id); |
36c32ba2 LP |
358 | } |
359 | ||
16484a8a ZJS |
360 | static const char* unit_type_suffix(const char *name) { |
361 | const char *dot; | |
362 | ||
363 | dot = strrchr(name, '.'); | |
364 | if (!dot) | |
365 | return ""; | |
366 | ||
367 | return dot + 1; | |
368 | } | |
369 | ||
d8fba7c6 | 370 | static bool output_show_unit(const UnitInfo *u, char **patterns) { |
f8591ee1 LP |
371 | assert(u); |
372 | ||
2404701e | 373 | if (!strv_fnmatch_or_empty(patterns, u->id, FNM_NOESCAPE)) |
d8fba7c6 | 374 | return false; |
d8fba7c6 | 375 | |
16484a8a ZJS |
376 | if (arg_types && !strv_find(arg_types, unit_type_suffix(u->id))) |
377 | return false; | |
6c71341a ZJS |
378 | |
379 | if (arg_all) | |
380 | return true; | |
381 | ||
409472cb FB |
382 | /* Note that '--all' is not purely a state filter, but also a |
383 | * filter that hides units that "follow" other units (which is | |
384 | * used for device units that appear under different names). */ | |
385 | if (!isempty(u->following)) | |
386 | return false; | |
387 | ||
ebc96265 FB |
388 | if (!strv_isempty(arg_states)) |
389 | return true; | |
390 | ||
391 | /* By default show all units except the ones in inactive | |
392 | * state and with no pending job */ | |
6c71341a ZJS |
393 | if (u->job_id > 0) |
394 | return true; | |
395 | ||
409472cb | 396 | if (streq(u->active_state, "inactive")) |
6c71341a ZJS |
397 | return false; |
398 | ||
399 | return true; | |
33330222 ZJS |
400 | } |
401 | ||
1238ee09 | 402 | static int output_units_list(const UnitInfo *unit_infos, unsigned c) { |
b5d7f1bb | 403 | unsigned circle_len = 0, id_len, max_id_len, load_len, active_len, sub_len, job_len, desc_len, max_desc_len; |
f459b602 | 404 | const UnitInfo *u; |
a1074881 | 405 | unsigned n_shown = 0; |
b5d7f1bb | 406 | int job_count = 0; |
0221d68a | 407 | bool full = arg_full || FLAGS_SET(arg_pager_flags, PAGER_DISABLE); |
33330222 | 408 | |
fbd0b64f LP |
409 | max_id_len = STRLEN("UNIT"); |
410 | load_len = STRLEN("LOAD"); | |
411 | active_len = STRLEN("ACTIVE"); | |
412 | sub_len = STRLEN("SUB"); | |
413 | job_len = STRLEN("JOB"); | |
414 | max_desc_len = STRLEN("DESCRIPTION"); | |
b036fc00 LP |
415 | |
416 | for (u = unit_infos; u < unit_infos + c; u++) { | |
1238ee09 | 417 | max_id_len = MAX(max_id_len, strlen(u->id) + (u->machine ? strlen(u->machine)+1 : 0)); |
a1074881 | 418 | load_len = MAX(load_len, strlen(u->load_state)); |
b036fc00 LP |
419 | active_len = MAX(active_len, strlen(u->active_state)); |
420 | sub_len = MAX(sub_len, strlen(u->sub_state)); | |
b5d7f1bb | 421 | max_desc_len = MAX(max_desc_len, strlen(u->description)); |
f459b602 | 422 | |
ccd41387 | 423 | if (u->job_id != 0) { |
b036fc00 | 424 | job_len = MAX(job_len, strlen(u->job_type)); |
ccd41387 ZJS |
425 | job_count++; |
426 | } | |
90c3f79d LP |
427 | |
428 | if (!arg_no_legend && | |
429 | (streq(u->active_state, "failed") || | |
c4555ad8 | 430 | STR_IN_SET(u->load_state, "error", "not-found", "bad-setting", "masked"))) |
90c3f79d | 431 | circle_len = 2; |
33330222 ZJS |
432 | } |
433 | ||
184ecaf7 | 434 | if (!arg_full && original_stdout_is_tty) { |
4deb3b93 | 435 | unsigned basic_len; |
cbc9fbd1 | 436 | |
b5d7f1bb | 437 | id_len = MIN(max_id_len, 25u); /* as much as it needs, but at most 25 for now */ |
a9bd056a | 438 | basic_len = circle_len + 1 + id_len + 1 + load_len + 1 + active_len + 1 + sub_len + 1; |
cbc9fbd1 | 439 | |
ccd41387 ZJS |
440 | if (job_count) |
441 | basic_len += job_len + 1; | |
cbc9fbd1 | 442 | |
4deb3b93 MS |
443 | if (basic_len < (unsigned) columns()) { |
444 | unsigned extra_len, incr; | |
445 | extra_len = columns() - basic_len; | |
cbc9fbd1 | 446 | |
4deb3b93 MS |
447 | /* Either UNIT already got 25, or is fully satisfied. |
448 | * Grant up to 25 to DESC now. */ | |
9607d947 | 449 | incr = MIN(extra_len, 25u); |
b5d7f1bb | 450 | desc_len = incr; |
4deb3b93 | 451 | extra_len -= incr; |
cbc9fbd1 | 452 | |
b5d7f1bb LP |
453 | /* Of the remainder give as much as the ID needs to the ID, and give the rest to the |
454 | * description but not more than it needs. */ | |
4deb3b93 | 455 | if (extra_len > 0) { |
b5d7f1bb | 456 | incr = MIN(max_id_len - id_len, extra_len); |
4deb3b93 | 457 | id_len += incr; |
b5d7f1bb | 458 | desc_len += MIN(extra_len - incr, max_desc_len - desc_len); |
4deb3b93 | 459 | } |
43479f8d ZJS |
460 | } else |
461 | desc_len = 0; | |
b5d7f1bb | 462 | } else { |
4deb3b93 | 463 | id_len = max_id_len; |
b5d7f1bb LP |
464 | desc_len = max_desc_len; |
465 | } | |
4deb3b93 | 466 | |
b036fc00 | 467 | for (u = unit_infos; u < unit_infos + c; u++) { |
1238ee09 | 468 | _cleanup_free_ char *e = NULL, *j = NULL; |
16484a8a | 469 | const char *on_underline = "", *off_underline = ""; |
90c3f79d LP |
470 | const char *on_loaded = "", *off_loaded = ""; |
471 | const char *on_active = "", *off_active = ""; | |
472 | const char *on_circle = "", *off_circle = ""; | |
1238ee09 | 473 | const char *id; |
16484a8a | 474 | bool circle = false, underline = false; |
b036fc00 | 475 | |
ad94ad63 | 476 | if (!n_shown && !arg_no_legend) { |
90c3f79d LP |
477 | |
478 | if (circle_len > 0) | |
479 | fputs(" ", stdout); | |
480 | ||
16484a8a ZJS |
481 | printf("%s%-*s %-*s %-*s %-*s ", |
482 | ansi_underline(), | |
cbc9fbd1 LP |
483 | id_len, "UNIT", |
484 | load_len, "LOAD", | |
485 | active_len, "ACTIVE", | |
486 | sub_len, "SUB"); | |
487 | ||
ccd41387 ZJS |
488 | if (job_count) |
489 | printf("%-*s ", job_len, "JOB"); | |
cbc9fbd1 | 490 | |
b5d7f1bb LP |
491 | printf("%-*.*s%s\n", |
492 | desc_len, | |
0221d68a | 493 | full ? -1 : (int) desc_len, |
16484a8a ZJS |
494 | "DESCRIPTION", |
495 | ansi_normal()); | |
ad94ad63 ZJS |
496 | } |
497 | ||
688c6725 LP |
498 | n_shown++; |
499 | ||
16484a8a ZJS |
500 | if (u + 1 < unit_infos + c && |
501 | !streq(unit_type_suffix(u->id), unit_type_suffix((u + 1)->id))) { | |
502 | on_underline = ansi_underline(); | |
503 | off_underline = ansi_normal(); | |
504 | underline = true; | |
505 | } | |
506 | ||
c4555ad8 | 507 | if (STR_IN_SET(u->load_state, "error", "not-found", "bad-setting", "masked") && !arg_plain) { |
90c3f79d | 508 | on_circle = ansi_highlight_yellow(); |
16484a8a | 509 | off_circle = ansi_normal(); |
90c3f79d | 510 | circle = true; |
16484a8a | 511 | on_loaded = underline ? ansi_highlight_red_underline() : ansi_highlight_red(); |
835a19e0 | 512 | off_loaded = underline ? on_underline : ansi_normal(); |
250ba664 | 513 | } else if (streq(u->active_state, "failed") && !arg_plain) { |
16484a8a ZJS |
514 | on_circle = ansi_highlight_red(); |
515 | off_circle = ansi_normal(); | |
90c3f79d | 516 | circle = true; |
16484a8a | 517 | on_active = underline ? ansi_highlight_red_underline() : ansi_highlight_red(); |
835a19e0 | 518 | off_active = underline ? on_underline : ansi_normal(); |
90c3f79d | 519 | } |
eb68c413 | 520 | |
1238ee09 | 521 | if (u->machine) { |
605405c6 | 522 | j = strjoin(u->machine, ":", u->id); |
1238ee09 LP |
523 | if (!j) |
524 | return log_oom(); | |
525 | ||
526 | id = j; | |
527 | } else | |
528 | id = u->id; | |
529 | ||
530 | if (arg_full) { | |
531 | e = ellipsize(id, id_len, 33); | |
532 | if (!e) | |
533 | return log_oom(); | |
534 | ||
535 | id = e; | |
536 | } | |
eb68c413 | 537 | |
90c3f79d | 538 | if (circle_len > 0) |
9a6f746f | 539 | printf("%s%s%s ", on_circle, circle ? special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE) : " ", off_circle); |
90c3f79d | 540 | |
16484a8a ZJS |
541 | printf("%s%s%-*s%s %s%-*s%s %s%-*s %-*s%s %-*s", |
542 | on_underline, | |
90c3f79d | 543 | on_active, id_len, id, off_active, |
a1074881 | 544 | on_loaded, load_len, u->load_state, off_loaded, |
b036fc00 LP |
545 | on_active, active_len, u->active_state, |
546 | sub_len, u->sub_state, off_active, | |
ccd41387 | 547 | job_count ? job_len + 1 : 0, u->job_id ? u->job_type : ""); |
cbc9fbd1 | 548 | |
b5d7f1bb LP |
549 | printf("%-*.*s%s\n", |
550 | desc_len, | |
0221d68a | 551 | full ? -1 : (int) desc_len, |
16484a8a ZJS |
552 | u->description, |
553 | off_underline); | |
eb68c413 ZJS |
554 | } |
555 | ||
ebed32bf | 556 | if (!arg_no_legend) { |
57f7ae4f ZJS |
557 | const char *on, *off; |
558 | ||
559 | if (n_shown) { | |
90c3f79d LP |
560 | puts("\n" |
561 | "LOAD = Reflects whether the unit definition was properly loaded.\n" | |
e3e0314b ZJS |
562 | "ACTIVE = The high-level unit activation state, i.e. generalization of SUB.\n" |
563 | "SUB = The low-level unit activation state, values depend on unit type."); | |
564 | puts(job_count ? "JOB = Pending job for the unit.\n" : ""); | |
0b5a519c | 565 | on = ansi_highlight(); |
1fc464f6 | 566 | off = ansi_normal(); |
57f7ae4f | 567 | } else { |
0b5a519c | 568 | on = ansi_highlight_red(); |
1fc464f6 | 569 | off = ansi_normal(); |
57f7ae4f | 570 | } |
eb68c413 ZJS |
571 | |
572 | if (arg_all) | |
48c2826b | 573 | printf("%s%u loaded units listed.%s\n" |
57f7ae4f ZJS |
574 | "To show all installed unit files use 'systemctl list-unit-files'.\n", |
575 | on, n_shown, off); | |
eb68c413 | 576 | else |
48c2826b | 577 | printf("%s%u loaded units listed.%s Pass --all to see loaded but inactive units, too.\n" |
57f7ae4f ZJS |
578 | "To show all installed unit files use 'systemctl list-unit-files'.\n", |
579 | on, n_shown, off); | |
eb68c413 | 580 | } |
1238ee09 LP |
581 | |
582 | return 0; | |
eb68c413 ZJS |
583 | } |
584 | ||
a00963a2 | 585 | static int get_unit_list( |
f459b602 | 586 | sd_bus *bus, |
1238ee09 LP |
587 | const char *machine, |
588 | char **patterns, | |
589 | UnitInfo **unit_infos, | |
590 | int c, | |
591 | sd_bus_message **_reply) { | |
a00963a2 | 592 | |
4afd3348 LP |
593 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
594 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
595 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
ca2d3784 | 596 | size_t size = c; |
1238ee09 | 597 | int r; |
f459b602 | 598 | UnitInfo u; |
313fe66f | 599 | bool fallback = false; |
7e4249b9 | 600 | |
265a7a2a | 601 | assert(bus); |
1238ee09 | 602 | assert(unit_infos); |
f459b602 | 603 | assert(_reply); |
1238ee09 | 604 | |
cdc06ed7 | 605 | r = sd_bus_message_new_method_call( |
f22f08cd | 606 | bus, |
cdc06ed7 | 607 | &m, |
f22f08cd SP |
608 | "org.freedesktop.systemd1", |
609 | "/org/freedesktop/systemd1", | |
610 | "org.freedesktop.systemd1.Manager", | |
313fe66f | 611 | "ListUnitsByPatterns"); |
cdc06ed7 DS |
612 | if (r < 0) |
613 | return bus_log_create_error(r); | |
614 | ||
615 | r = sd_bus_message_append_strv(m, arg_states); | |
616 | if (r < 0) | |
617 | return bus_log_create_error(r); | |
618 | ||
313fe66f | 619 | r = sd_bus_message_append_strv(m, patterns); |
620 | if (r < 0) | |
621 | return bus_log_create_error(r); | |
622 | ||
cdc06ed7 | 623 | r = sd_bus_call(bus, m, 0, &error, &reply); |
33d52725 ZJS |
624 | if (r < 0 && (sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_METHOD) || |
625 | sd_bus_error_has_name(&error, SD_BUS_ERROR_ACCESS_DENIED))) { | |
313fe66f | 626 | /* Fallback to legacy ListUnitsFiltered method */ |
627 | fallback = true; | |
628 | log_debug_errno(r, "Failed to list units: %s Falling back to ListUnitsFiltered method.", bus_error_message(&error, r)); | |
629 | m = sd_bus_message_unref(m); | |
630 | sd_bus_error_free(&error); | |
631 | ||
632 | r = sd_bus_message_new_method_call( | |
633 | bus, | |
634 | &m, | |
635 | "org.freedesktop.systemd1", | |
636 | "/org/freedesktop/systemd1", | |
637 | "org.freedesktop.systemd1.Manager", | |
638 | "ListUnitsFiltered"); | |
639 | if (r < 0) | |
640 | return bus_log_create_error(r); | |
641 | ||
642 | r = sd_bus_message_append_strv(m, arg_states); | |
643 | if (r < 0) | |
644 | return bus_log_create_error(r); | |
645 | ||
646 | r = sd_bus_call(bus, m, 0, &error, &reply); | |
647 | } | |
4c3e8e39 LP |
648 | if (r < 0) |
649 | return log_error_errno(r, "Failed to list units: %s", bus_error_message(&error, r)); | |
7e4249b9 | 650 | |
f459b602 MAP |
651 | r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssssouso)"); |
652 | if (r < 0) | |
653 | return bus_log_parse_error(r); | |
7e4249b9 | 654 | |
f459b602 | 655 | while ((r = bus_parse_unit_info(reply, &u)) > 0) { |
1238ee09 LP |
656 | u.machine = machine; |
657 | ||
313fe66f | 658 | if (!output_show_unit(&u, fallback ? patterns : NULL)) |
8d5ba5a9 | 659 | continue; |
7e4249b9 | 660 | |
1238ee09 | 661 | if (!GREEDY_REALLOC(*unit_infos, size, c+1)) |
f459b602 | 662 | return log_oom(); |
36c32ba2 | 663 | |
1238ee09 | 664 | (*unit_infos)[c++] = u; |
991f2a39 | 665 | } |
f459b602 MAP |
666 | if (r < 0) |
667 | return bus_log_parse_error(r); | |
991f2a39 | 668 | |
f459b602 MAP |
669 | r = sd_bus_message_exit_container(reply); |
670 | if (r < 0) | |
671 | return bus_log_parse_error(r); | |
672 | ||
1cc6c93a | 673 | *_reply = TAKE_PTR(reply); |
f459b602 | 674 | |
1238ee09 LP |
675 | return c; |
676 | } | |
677 | ||
678 | static void message_set_freep(Set **set) { | |
224b0e7a | 679 | set_free_with_destructor(*set, sd_bus_message_unref); |
1238ee09 LP |
680 | } |
681 | ||
682 | static int get_unit_list_recursive( | |
683 | sd_bus *bus, | |
684 | char **patterns, | |
685 | UnitInfo **_unit_infos, | |
686 | Set **_replies, | |
687 | char ***_machines) { | |
688 | ||
689 | _cleanup_free_ UnitInfo *unit_infos = NULL; | |
690 | _cleanup_(message_set_freep) Set *replies; | |
691 | sd_bus_message *reply; | |
692 | int c, r; | |
693 | ||
694 | assert(bus); | |
695 | assert(_replies); | |
696 | assert(_unit_infos); | |
697 | assert(_machines); | |
698 | ||
d5099efc | 699 | replies = set_new(NULL); |
1238ee09 LP |
700 | if (!replies) |
701 | return log_oom(); | |
702 | ||
703 | c = get_unit_list(bus, NULL, patterns, &unit_infos, 0, &reply); | |
704 | if (c < 0) | |
705 | return c; | |
706 | ||
707 | r = set_put(replies, reply); | |
708 | if (r < 0) { | |
709 | sd_bus_message_unref(reply); | |
4c3e8e39 | 710 | return log_oom(); |
1238ee09 LP |
711 | } |
712 | ||
713 | if (arg_recursive) { | |
714 | _cleanup_strv_free_ char **machines = NULL; | |
715 | char **i; | |
716 | ||
717 | r = sd_get_machine_names(&machines); | |
718 | if (r < 0) | |
4c3e8e39 | 719 | return log_error_errno(r, "Failed to get machine names: %m"); |
1238ee09 LP |
720 | |
721 | STRV_FOREACH(i, machines) { | |
4afd3348 | 722 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *container = NULL; |
1238ee09 LP |
723 | int k; |
724 | ||
de33fc62 | 725 | r = sd_bus_open_system_machine(&container, *i); |
1238ee09 | 726 | if (r < 0) { |
4c3e8e39 | 727 | log_warning_errno(r, "Failed to connect to container %s, ignoring: %m", *i); |
1238ee09 LP |
728 | continue; |
729 | } | |
730 | ||
731 | k = get_unit_list(container, *i, patterns, &unit_infos, c, &reply); | |
732 | if (k < 0) | |
733 | return k; | |
734 | ||
735 | c = k; | |
736 | ||
737 | r = set_put(replies, reply); | |
738 | if (r < 0) { | |
739 | sd_bus_message_unref(reply); | |
4c3e8e39 | 740 | return log_oom(); |
1238ee09 LP |
741 | } |
742 | } | |
743 | ||
1cc6c93a | 744 | *_machines = TAKE_PTR(machines); |
1238ee09 LP |
745 | } else |
746 | *_machines = NULL; | |
747 | ||
1cc6c93a | 748 | *_unit_infos = TAKE_PTR(unit_infos); |
f459b602 | 749 | |
1cc6c93a | 750 | *_replies = TAKE_PTR(replies); |
1238ee09 | 751 | |
f459b602 | 752 | return c; |
991f2a39 ZJS |
753 | } |
754 | ||
e449de87 | 755 | static int list_units(int argc, char *argv[], void *userdata) { |
f459b602 | 756 | _cleanup_free_ UnitInfo *unit_infos = NULL; |
1238ee09 LP |
757 | _cleanup_(message_set_freep) Set *replies = NULL; |
758 | _cleanup_strv_free_ char **machines = NULL; | |
4fbd7192 | 759 | sd_bus *bus; |
991f2a39 ZJS |
760 | int r; |
761 | ||
4fbd7192 LP |
762 | r = acquire_bus(BUS_MANAGER, &bus); |
763 | if (r < 0) | |
764 | return r; | |
765 | ||
0221d68a | 766 | (void) pager_open(arg_pager_flags); |
d2ad7e1b | 767 | |
e449de87 | 768 | r = get_unit_list_recursive(bus, strv_skip(argv, 1), &unit_infos, &replies, &machines); |
991f2a39 ZJS |
769 | if (r < 0) |
770 | return r; | |
771 | ||
93bab288 | 772 | typesafe_qsort(unit_infos, r, compare_unit_info); |
1238ee09 | 773 | return output_units_list(unit_infos, r); |
991f2a39 ZJS |
774 | } |
775 | ||
a00963a2 | 776 | static int get_triggered_units( |
f459b602 MAP |
777 | sd_bus *bus, |
778 | const char* path, | |
779 | char*** ret) { | |
a00963a2 | 780 | |
4afd3348 | 781 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
991f2a39 ZJS |
782 | int r; |
783 | ||
4fbd7192 LP |
784 | assert(bus); |
785 | assert(path); | |
786 | assert(ret); | |
787 | ||
f459b602 MAP |
788 | r = sd_bus_get_property_strv( |
789 | bus, | |
790 | "org.freedesktop.systemd1", | |
791 | path, | |
792 | "org.freedesktop.systemd1.Unit", | |
793 | "Triggers", | |
794 | &error, | |
795 | ret); | |
f459b602 | 796 | if (r < 0) |
691395d8 | 797 | return log_error_errno(r, "Failed to determine triggers: %s", bus_error_message(&error, r)); |
991f2a39 ZJS |
798 | |
799 | return 0; | |
800 | } | |
801 | ||
f459b602 MAP |
802 | static int get_listening( |
803 | sd_bus *bus, | |
804 | const char* unit_path, | |
cbb76c29 | 805 | char*** listening) { |
f459b602 | 806 | |
4afd3348 LP |
807 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
808 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
f459b602 | 809 | const char *type, *path; |
cbb76c29 | 810 | int r, n = 0; |
991f2a39 | 811 | |
f459b602 MAP |
812 | r = sd_bus_get_property( |
813 | bus, | |
814 | "org.freedesktop.systemd1", | |
815 | unit_path, | |
816 | "org.freedesktop.systemd1.Socket", | |
817 | "Listen", | |
818 | &error, | |
819 | &reply, | |
820 | "a(ss)"); | |
691395d8 LP |
821 | if (r < 0) |
822 | return log_error_errno(r, "Failed to get list of listening sockets: %s", bus_error_message(&error, r)); | |
991f2a39 | 823 | |
f459b602 MAP |
824 | r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ss)"); |
825 | if (r < 0) | |
826 | return bus_log_parse_error(r); | |
991f2a39 | 827 | |
f459b602 | 828 | while ((r = sd_bus_message_read(reply, "(ss)", &type, &path)) > 0) { |
36c32ba2 | 829 | |
0d95178e | 830 | r = strv_extend(listening, type); |
f459b602 MAP |
831 | if (r < 0) |
832 | return log_oom(); | |
991f2a39 | 833 | |
0d95178e | 834 | r = strv_extend(listening, path); |
f459b602 MAP |
835 | if (r < 0) |
836 | return log_oom(); | |
7e4249b9 | 837 | |
cbb76c29 | 838 | n++; |
36c32ba2 | 839 | } |
f459b602 MAP |
840 | if (r < 0) |
841 | return bus_log_parse_error(r); | |
842 | ||
843 | r = sd_bus_message_exit_container(reply); | |
844 | if (r < 0) | |
845 | return bus_log_parse_error(r); | |
36c32ba2 | 846 | |
cbb76c29 | 847 | return n; |
991f2a39 ZJS |
848 | } |
849 | ||
850 | struct socket_info { | |
0cfc3525 | 851 | const char *machine; |
991f2a39 ZJS |
852 | const char* id; |
853 | ||
854 | char* type; | |
855 | char* path; | |
856 | ||
857 | /* Note: triggered is a list here, although it almost certainly | |
858 | * will always be one unit. Nevertheless, dbus API allows for multiple | |
f7340ab2 | 859 | * values, so let's follow that. */ |
991f2a39 ZJS |
860 | char** triggered; |
861 | ||
862 | /* The strv above is shared. free is set only in the first one. */ | |
863 | bool own_triggered; | |
864 | }; | |
865 | ||
cbb76c29 | 866 | static int socket_info_compare(const struct socket_info *a, const struct socket_info *b) { |
93bab288 | 867 | int r; |
cbc9fbd1 | 868 | |
cbb76c29 LP |
869 | assert(a); |
870 | assert(b); | |
871 | ||
0cfc3525 TA |
872 | if (!a->machine && b->machine) |
873 | return -1; | |
874 | if (a->machine && !b->machine) | |
875 | return 1; | |
876 | if (a->machine && b->machine) { | |
93bab288 YW |
877 | r = strcasecmp(a->machine, b->machine); |
878 | if (r != 0) | |
879 | return r; | |
0cfc3525 TA |
880 | } |
881 | ||
93bab288 YW |
882 | r = strcmp(a->path, b->path); |
883 | if (r == 0) | |
884 | r = strcmp(a->type, b->type); | |
cbc9fbd1 | 885 | |
93bab288 | 886 | return r; |
991f2a39 ZJS |
887 | } |
888 | ||
889 | static int output_sockets_list(struct socket_info *socket_infos, unsigned cs) { | |
890 | struct socket_info *s; | |
fbd0b64f LP |
891 | unsigned pathlen = STRLEN("LISTEN"), |
892 | typelen = STRLEN("TYPE") * arg_show_types, | |
893 | socklen = STRLEN("UNIT"), | |
894 | servlen = STRLEN("ACTIVATES"); | |
991f2a39 ZJS |
895 | const char *on, *off; |
896 | ||
897 | for (s = socket_infos; s < socket_infos + cs; s++) { | |
991f2a39 | 898 | unsigned tmp = 0; |
cbc9fbd1 | 899 | char **a; |
991f2a39 ZJS |
900 | |
901 | socklen = MAX(socklen, strlen(s->id)); | |
902 | if (arg_show_types) | |
903 | typelen = MAX(typelen, strlen(s->type)); | |
0cfc3525 | 904 | pathlen = MAX(pathlen, strlen(s->path) + (s->machine ? strlen(s->machine)+1 : 0)); |
991f2a39 ZJS |
905 | |
906 | STRV_FOREACH(a, s->triggered) | |
907 | tmp += strlen(*a) + 2*(a != s->triggered); | |
908 | servlen = MAX(servlen, tmp); | |
909 | } | |
910 | ||
911 | if (cs) { | |
571bfc6c MM |
912 | if (!arg_no_legend) |
913 | printf("%-*s %-*.*s%-*s %s\n", | |
914 | pathlen, "LISTEN", | |
915 | typelen + arg_show_types, typelen + arg_show_types, "TYPE ", | |
916 | socklen, "UNIT", | |
917 | "ACTIVATES"); | |
991f2a39 ZJS |
918 | |
919 | for (s = socket_infos; s < socket_infos + cs; s++) { | |
0cfc3525 TA |
920 | _cleanup_free_ char *j = NULL; |
921 | const char *path; | |
991f2a39 ZJS |
922 | char **a; |
923 | ||
0cfc3525 | 924 | if (s->machine) { |
605405c6 | 925 | j = strjoin(s->machine, ":", s->path); |
0cfc3525 TA |
926 | if (!j) |
927 | return log_oom(); | |
928 | path = j; | |
929 | } else | |
930 | path = s->path; | |
931 | ||
991f2a39 ZJS |
932 | if (arg_show_types) |
933 | printf("%-*s %-*s %-*s", | |
0cfc3525 | 934 | pathlen, path, typelen, s->type, socklen, s->id); |
991f2a39 ZJS |
935 | else |
936 | printf("%-*s %-*s", | |
0cfc3525 | 937 | pathlen, path, socklen, s->id); |
991f2a39 ZJS |
938 | STRV_FOREACH(a, s->triggered) |
939 | printf("%s %s", | |
940 | a == s->triggered ? "" : ",", *a); | |
941 | printf("\n"); | |
942 | } | |
943 | ||
0b5a519c | 944 | on = ansi_highlight(); |
1fc464f6 | 945 | off = ansi_normal(); |
571bfc6c MM |
946 | if (!arg_no_legend) |
947 | printf("\n"); | |
991f2a39 | 948 | } else { |
0b5a519c | 949 | on = ansi_highlight_red(); |
1fc464f6 | 950 | off = ansi_normal(); |
991f2a39 ZJS |
951 | } |
952 | ||
571bfc6c MM |
953 | if (!arg_no_legend) { |
954 | printf("%s%u sockets listed.%s\n", on, cs, off); | |
955 | if (!arg_all) | |
956 | printf("Pass --all to see loaded but inactive sockets, too.\n"); | |
957 | } | |
265a7a2a ZJS |
958 | |
959 | return 0; | |
960 | } | |
961 | ||
e449de87 | 962 | static int list_sockets(int argc, char *argv[], void *userdata) { |
0cfc3525 TA |
963 | _cleanup_(message_set_freep) Set *replies = NULL; |
964 | _cleanup_strv_free_ char **machines = NULL; | |
f459b602 | 965 | _cleanup_free_ UnitInfo *unit_infos = NULL; |
8d5ba5a9 | 966 | _cleanup_free_ struct socket_info *socket_infos = NULL; |
f459b602 | 967 | const UnitInfo *u; |
991f2a39 | 968 | struct socket_info *s; |
cbb76c29 | 969 | unsigned cs = 0; |
991f2a39 | 970 | size_t size = 0; |
ad83b4c4 | 971 | int r = 0, n; |
4fbd7192 | 972 | sd_bus *bus; |
265a7a2a | 973 | |
4fbd7192 LP |
974 | r = acquire_bus(BUS_MANAGER, &bus); |
975 | if (r < 0) | |
976 | return r; | |
977 | ||
0221d68a | 978 | (void) pager_open(arg_pager_flags); |
d2ad7e1b | 979 | |
e449de87 | 980 | n = get_unit_list_recursive(bus, strv_skip(argv, 1), &unit_infos, &replies, &machines); |
cbb76c29 LP |
981 | if (n < 0) |
982 | return n; | |
265a7a2a | 983 | |
cbb76c29 | 984 | for (u = unit_infos; u < unit_infos + n; u++) { |
0d95178e | 985 | _cleanup_strv_free_ char **listening = NULL, **triggered = NULL; |
cbb76c29 | 986 | int i, c; |
991f2a39 | 987 | |
cbc9fbd1 | 988 | if (!endswith(u->id, ".socket")) |
991f2a39 ZJS |
989 | continue; |
990 | ||
991 | r = get_triggered_units(bus, u->unit_path, &triggered); | |
992 | if (r < 0) | |
993 | goto cleanup; | |
994 | ||
cbb76c29 LP |
995 | c = get_listening(bus, u->unit_path, &listening); |
996 | if (c < 0) { | |
997 | r = c; | |
991f2a39 | 998 | goto cleanup; |
cbb76c29 | 999 | } |
991f2a39 ZJS |
1000 | |
1001 | if (!GREEDY_REALLOC(socket_infos, size, cs + c)) { | |
1002 | r = log_oom(); | |
1003 | goto cleanup; | |
1004 | } | |
1005 | ||
1006 | for (i = 0; i < c; i++) | |
1007 | socket_infos[cs + i] = (struct socket_info) { | |
0cfc3525 | 1008 | .machine = u->machine, |
991f2a39 | 1009 | .id = u->id, |
0d95178e KS |
1010 | .type = listening[i*2], |
1011 | .path = listening[i*2 + 1], | |
991f2a39 ZJS |
1012 | .triggered = triggered, |
1013 | .own_triggered = i==0, | |
1014 | }; | |
1015 | ||
1016 | /* from this point on we will cleanup those socket_infos */ | |
1017 | cs += c; | |
0d95178e KS |
1018 | free(listening); |
1019 | listening = triggered = NULL; /* avoid cleanup */ | |
991f2a39 ZJS |
1020 | } |
1021 | ||
93bab288 | 1022 | typesafe_qsort(socket_infos, cs, socket_info_compare); |
991f2a39 ZJS |
1023 | |
1024 | output_sockets_list(socket_infos, cs); | |
1025 | ||
1026 | cleanup: | |
1027 | assert(cs == 0 || socket_infos); | |
1028 | for (s = socket_infos; s < socket_infos + cs; s++) { | |
1029 | free(s->type); | |
1030 | free(s->path); | |
1031 | if (s->own_triggered) | |
1032 | strv_free(s->triggered); | |
1033 | } | |
4445a875 | 1034 | |
872c8faa | 1035 | return r; |
4445a875 LP |
1036 | } |
1037 | ||
cbb76c29 LP |
1038 | static int get_next_elapse( |
1039 | sd_bus *bus, | |
1040 | const char *path, | |
1041 | dual_timestamp *next) { | |
1042 | ||
4afd3348 | 1043 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
cbb76c29 LP |
1044 | dual_timestamp t; |
1045 | int r; | |
1046 | ||
1047 | assert(bus); | |
1048 | assert(path); | |
1049 | assert(next); | |
1050 | ||
1051 | r = sd_bus_get_property_trivial( | |
1052 | bus, | |
1053 | "org.freedesktop.systemd1", | |
1054 | path, | |
1055 | "org.freedesktop.systemd1.Timer", | |
1056 | "NextElapseUSecMonotonic", | |
1057 | &error, | |
1058 | 't', | |
1059 | &t.monotonic); | |
691395d8 | 1060 | if (r < 0) |
021723c6 | 1061 | return log_error_errno(r, "Failed to get next elapse time: %s", bus_error_message(&error, r)); |
cbb76c29 LP |
1062 | |
1063 | r = sd_bus_get_property_trivial( | |
1064 | bus, | |
1065 | "org.freedesktop.systemd1", | |
1066 | path, | |
1067 | "org.freedesktop.systemd1.Timer", | |
1068 | "NextElapseUSecRealtime", | |
1069 | &error, | |
1070 | 't', | |
1071 | &t.realtime); | |
691395d8 | 1072 | if (r < 0) |
021723c6 | 1073 | return log_error_errno(r, "Failed to get next elapse time: %s", bus_error_message(&error, r)); |
cbb76c29 LP |
1074 | |
1075 | *next = t; | |
1076 | return 0; | |
1077 | } | |
1078 | ||
d784e2db LP |
1079 | static int get_last_trigger( |
1080 | sd_bus *bus, | |
1081 | const char *path, | |
1082 | usec_t *last) { | |
1083 | ||
4afd3348 | 1084 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
d784e2db LP |
1085 | int r; |
1086 | ||
1087 | assert(bus); | |
1088 | assert(path); | |
1089 | assert(last); | |
1090 | ||
1091 | r = sd_bus_get_property_trivial( | |
1092 | bus, | |
1093 | "org.freedesktop.systemd1", | |
1094 | path, | |
1095 | "org.freedesktop.systemd1.Timer", | |
dedabea4 | 1096 | "LastTriggerUSec", |
d784e2db LP |
1097 | &error, |
1098 | 't', | |
1099 | last); | |
691395d8 LP |
1100 | if (r < 0) |
1101 | return log_error_errno(r, "Failed to get last trigger time: %s", bus_error_message(&error, r)); | |
d784e2db LP |
1102 | |
1103 | return 0; | |
1104 | } | |
1105 | ||
cbb76c29 | 1106 | struct timer_info { |
806a37e7 | 1107 | const char* machine; |
cbb76c29 LP |
1108 | const char* id; |
1109 | usec_t next_elapse; | |
d784e2db | 1110 | usec_t last_trigger; |
cbb76c29 LP |
1111 | char** triggered; |
1112 | }; | |
1113 | ||
1114 | static int timer_info_compare(const struct timer_info *a, const struct timer_info *b) { | |
93bab288 | 1115 | int r; |
806a37e7 | 1116 | |
cbb76c29 LP |
1117 | assert(a); |
1118 | assert(b); | |
1119 | ||
806a37e7 TA |
1120 | if (!a->machine && b->machine) |
1121 | return -1; | |
1122 | if (a->machine && !b->machine) | |
1123 | return 1; | |
1124 | if (a->machine && b->machine) { | |
93bab288 YW |
1125 | r = strcasecmp(a->machine, b->machine); |
1126 | if (r != 0) | |
1127 | return r; | |
806a37e7 TA |
1128 | } |
1129 | ||
93bab288 YW |
1130 | r = CMP(a->next_elapse, b->next_elapse); |
1131 | if (r != 0) | |
1132 | return r; | |
cbb76c29 LP |
1133 | |
1134 | return strcmp(a->id, b->id); | |
1135 | } | |
1136 | ||
1137 | static int output_timers_list(struct timer_info *timer_infos, unsigned n) { | |
1138 | struct timer_info *t; | |
1139 | unsigned | |
fbd0b64f LP |
1140 | nextlen = STRLEN("NEXT"), |
1141 | leftlen = STRLEN("LEFT"), | |
1142 | lastlen = STRLEN("LAST"), | |
1143 | passedlen = STRLEN("PASSED"), | |
1144 | unitlen = STRLEN("UNIT"), | |
1145 | activatelen = STRLEN("ACTIVATES"); | |
cbb76c29 LP |
1146 | |
1147 | const char *on, *off; | |
1148 | ||
1149 | assert(timer_infos || n == 0); | |
1150 | ||
1151 | for (t = timer_infos; t < timer_infos + n; t++) { | |
1152 | unsigned ul = 0; | |
1153 | char **a; | |
1154 | ||
1155 | if (t->next_elapse > 0) { | |
1156 | char tstamp[FORMAT_TIMESTAMP_MAX] = "", trel[FORMAT_TIMESTAMP_RELATIVE_MAX] = ""; | |
1157 | ||
1158 | format_timestamp(tstamp, sizeof(tstamp), t->next_elapse); | |
1159 | nextlen = MAX(nextlen, strlen(tstamp) + 1); | |
1160 | ||
1161 | format_timestamp_relative(trel, sizeof(trel), t->next_elapse); | |
1162 | leftlen = MAX(leftlen, strlen(trel)); | |
1163 | } | |
1164 | ||
d784e2db LP |
1165 | if (t->last_trigger > 0) { |
1166 | char tstamp[FORMAT_TIMESTAMP_MAX] = "", trel[FORMAT_TIMESTAMP_RELATIVE_MAX] = ""; | |
1167 | ||
1168 | format_timestamp(tstamp, sizeof(tstamp), t->last_trigger); | |
1169 | lastlen = MAX(lastlen, strlen(tstamp) + 1); | |
1170 | ||
1171 | format_timestamp_relative(trel, sizeof(trel), t->last_trigger); | |
1172 | passedlen = MAX(passedlen, strlen(trel)); | |
1173 | } | |
1174 | ||
806a37e7 | 1175 | unitlen = MAX(unitlen, strlen(t->id) + (t->machine ? strlen(t->machine)+1 : 0)); |
cbb76c29 LP |
1176 | |
1177 | STRV_FOREACH(a, t->triggered) | |
1178 | ul += strlen(*a) + 2*(a != t->triggered); | |
d784e2db | 1179 | |
cbb76c29 LP |
1180 | activatelen = MAX(activatelen, ul); |
1181 | } | |
1182 | ||
1183 | if (n > 0) { | |
1184 | if (!arg_no_legend) | |
d784e2db LP |
1185 | printf("%-*s %-*s %-*s %-*s %-*s %s\n", |
1186 | nextlen, "NEXT", | |
1187 | leftlen, "LEFT", | |
1188 | lastlen, "LAST", | |
1189 | passedlen, "PASSED", | |
1190 | unitlen, "UNIT", | |
1191 | "ACTIVATES"); | |
cbb76c29 LP |
1192 | |
1193 | for (t = timer_infos; t < timer_infos + n; t++) { | |
806a37e7 TA |
1194 | _cleanup_free_ char *j = NULL; |
1195 | const char *unit; | |
d784e2db LP |
1196 | char tstamp1[FORMAT_TIMESTAMP_MAX] = "n/a", trel1[FORMAT_TIMESTAMP_RELATIVE_MAX] = "n/a"; |
1197 | char tstamp2[FORMAT_TIMESTAMP_MAX] = "n/a", trel2[FORMAT_TIMESTAMP_RELATIVE_MAX] = "n/a"; | |
cbb76c29 LP |
1198 | char **a; |
1199 | ||
d784e2db LP |
1200 | format_timestamp(tstamp1, sizeof(tstamp1), t->next_elapse); |
1201 | format_timestamp_relative(trel1, sizeof(trel1), t->next_elapse); | |
cbb76c29 | 1202 | |
d784e2db LP |
1203 | format_timestamp(tstamp2, sizeof(tstamp2), t->last_trigger); |
1204 | format_timestamp_relative(trel2, sizeof(trel2), t->last_trigger); | |
1205 | ||
806a37e7 | 1206 | if (t->machine) { |
605405c6 | 1207 | j = strjoin(t->machine, ":", t->id); |
806a37e7 TA |
1208 | if (!j) |
1209 | return log_oom(); | |
1210 | unit = j; | |
1211 | } else | |
1212 | unit = t->id; | |
1213 | ||
d784e2db | 1214 | printf("%-*s %-*s %-*s %-*s %-*s", |
806a37e7 | 1215 | nextlen, tstamp1, leftlen, trel1, lastlen, tstamp2, passedlen, trel2, unitlen, unit); |
cbb76c29 LP |
1216 | |
1217 | STRV_FOREACH(a, t->triggered) | |
1218 | printf("%s %s", | |
1219 | a == t->triggered ? "" : ",", *a); | |
1220 | printf("\n"); | |
1221 | } | |
1222 | ||
1223 | on = ansi_highlight(); | |
1fc464f6 | 1224 | off = ansi_normal(); |
cbb76c29 LP |
1225 | if (!arg_no_legend) |
1226 | printf("\n"); | |
1227 | } else { | |
1228 | on = ansi_highlight_red(); | |
1fc464f6 | 1229 | off = ansi_normal(); |
cbb76c29 LP |
1230 | } |
1231 | ||
1232 | if (!arg_no_legend) { | |
1233 | printf("%s%u timers listed.%s\n", on, n, off); | |
1234 | if (!arg_all) | |
1235 | printf("Pass --all to see loaded but inactive timers, too.\n"); | |
1236 | } | |
1237 | ||
1238 | return 0; | |
1239 | } | |
1240 | ||
f5080e73 DH |
1241 | static usec_t calc_next_elapse(dual_timestamp *nw, dual_timestamp *next) { |
1242 | usec_t next_elapse; | |
1243 | ||
1244 | assert(nw); | |
1245 | assert(next); | |
1246 | ||
3a43da28 | 1247 | if (next->monotonic != USEC_INFINITY && next->monotonic > 0) { |
f5080e73 DH |
1248 | usec_t converted; |
1249 | ||
1250 | if (next->monotonic > nw->monotonic) | |
1251 | converted = nw->realtime + (next->monotonic - nw->monotonic); | |
1252 | else | |
1253 | converted = nw->realtime - (nw->monotonic - next->monotonic); | |
1254 | ||
3a43da28 | 1255 | if (next->realtime != USEC_INFINITY && next->realtime > 0) |
f5080e73 DH |
1256 | next_elapse = MIN(converted, next->realtime); |
1257 | else | |
1258 | next_elapse = converted; | |
1259 | ||
1260 | } else | |
1261 | next_elapse = next->realtime; | |
1262 | ||
1263 | return next_elapse; | |
1264 | } | |
1265 | ||
e449de87 | 1266 | static int list_timers(int argc, char *argv[], void *userdata) { |
806a37e7 TA |
1267 | _cleanup_(message_set_freep) Set *replies = NULL; |
1268 | _cleanup_strv_free_ char **machines = NULL; | |
cbb76c29 LP |
1269 | _cleanup_free_ struct timer_info *timer_infos = NULL; |
1270 | _cleanup_free_ UnitInfo *unit_infos = NULL; | |
1271 | struct timer_info *t; | |
1272 | const UnitInfo *u; | |
1273 | size_t size = 0; | |
1823b86e | 1274 | int n, c = 0; |
cbb76c29 | 1275 | dual_timestamp nw; |
4fbd7192 | 1276 | sd_bus *bus; |
1823b86e | 1277 | int r = 0; |
cbb76c29 | 1278 | |
4fbd7192 LP |
1279 | r = acquire_bus(BUS_MANAGER, &bus); |
1280 | if (r < 0) | |
1281 | return r; | |
1282 | ||
0221d68a | 1283 | (void) pager_open(arg_pager_flags); |
d2ad7e1b | 1284 | |
e449de87 | 1285 | n = get_unit_list_recursive(bus, strv_skip(argv, 1), &unit_infos, &replies, &machines); |
cbb76c29 LP |
1286 | if (n < 0) |
1287 | return n; | |
1288 | ||
1289 | dual_timestamp_get(&nw); | |
1290 | ||
1291 | for (u = unit_infos; u < unit_infos + n; u++) { | |
1292 | _cleanup_strv_free_ char **triggered = NULL; | |
5cb14b37 | 1293 | dual_timestamp next = DUAL_TIMESTAMP_NULL; |
d784e2db | 1294 | usec_t m, last = 0; |
cbb76c29 | 1295 | |
cbb76c29 LP |
1296 | if (!endswith(u->id, ".timer")) |
1297 | continue; | |
1298 | ||
1299 | r = get_triggered_units(bus, u->unit_path, &triggered); | |
1300 | if (r < 0) | |
1301 | goto cleanup; | |
1302 | ||
1303 | r = get_next_elapse(bus, u->unit_path, &next); | |
1304 | if (r < 0) | |
1305 | goto cleanup; | |
1306 | ||
d784e2db LP |
1307 | get_last_trigger(bus, u->unit_path, &last); |
1308 | ||
cbb76c29 LP |
1309 | if (!GREEDY_REALLOC(timer_infos, size, c+1)) { |
1310 | r = log_oom(); | |
1311 | goto cleanup; | |
1312 | } | |
1313 | ||
f5080e73 DH |
1314 | m = calc_next_elapse(&nw, &next); |
1315 | ||
cbb76c29 | 1316 | timer_infos[c++] = (struct timer_info) { |
806a37e7 | 1317 | .machine = u->machine, |
cbb76c29 LP |
1318 | .id = u->id, |
1319 | .next_elapse = m, | |
d784e2db | 1320 | .last_trigger = last, |
1cc6c93a | 1321 | .triggered = TAKE_PTR(triggered), |
cbb76c29 | 1322 | }; |
cbb76c29 LP |
1323 | } |
1324 | ||
93bab288 | 1325 | typesafe_qsort(timer_infos, c, timer_info_compare); |
cbb76c29 LP |
1326 | |
1327 | output_timers_list(timer_infos, c); | |
1328 | ||
1329 | cleanup: | |
1330 | for (t = timer_infos; t < timer_infos + c; t++) | |
1331 | strv_free(t->triggered); | |
1332 | ||
1333 | return r; | |
1334 | } | |
1335 | ||
93bab288 | 1336 | static int compare_unit_file_list(const UnitFileList *a, const UnitFileList *b) { |
729e3769 | 1337 | const char *d1, *d2; |
729e3769 | 1338 | |
93bab288 YW |
1339 | d1 = strrchr(a->path, '.'); |
1340 | d2 = strrchr(b->path, '.'); | |
729e3769 LP |
1341 | |
1342 | if (d1 && d2) { | |
1343 | int r; | |
1344 | ||
1345 | r = strcasecmp(d1, d2); | |
1346 | if (r != 0) | |
1347 | return r; | |
1348 | } | |
1349 | ||
93bab288 | 1350 | return strcasecmp(basename(a->path), basename(b->path)); |
729e3769 LP |
1351 | } |
1352 | ||
313fe66f | 1353 | static bool output_show_unit_file(const UnitFileList *u, char **states, char **patterns) { |
f8591ee1 LP |
1354 | assert(u); |
1355 | ||
2404701e | 1356 | if (!strv_fnmatch_or_empty(patterns, basename(u->path), FNM_NOESCAPE)) |
d8fba7c6 | 1357 | return false; |
d8fba7c6 | 1358 | |
6c71341a ZJS |
1359 | if (!strv_isempty(arg_types)) { |
1360 | const char *dot; | |
1361 | ||
1362 | dot = strrchr(u->path, '.'); | |
1363 | if (!dot) | |
1364 | return false; | |
1365 | ||
1366 | if (!strv_find(arg_types, dot+1)) | |
1367 | return false; | |
1368 | } | |
1369 | ||
313fe66f | 1370 | if (!strv_isempty(states) && |
1371 | !strv_find(states, unit_file_state_to_string(u->state))) | |
bceccd5e | 1372 | return false; |
fec1530e | 1373 | |
6c71341a | 1374 | return true; |
729e3769 LP |
1375 | } |
1376 | ||
8d5ba5a9 | 1377 | static void output_unit_file_list(const UnitFileList *units, unsigned c) { |
0d292f5e | 1378 | unsigned max_id_len, id_cols, state_cols; |
729e3769 LP |
1379 | const UnitFileList *u; |
1380 | ||
fbd0b64f LP |
1381 | max_id_len = STRLEN("UNIT FILE"); |
1382 | state_cols = STRLEN("STATE"); | |
cbc9fbd1 | 1383 | |
1c0a113f | 1384 | for (u = units; u < units + c; u++) { |
2b6bf07d | 1385 | max_id_len = MAX(max_id_len, strlen(basename(u->path))); |
1c0a113f ZJS |
1386 | state_cols = MAX(state_cols, strlen(unit_file_state_to_string(u->state))); |
1387 | } | |
1388 | ||
1389 | if (!arg_full) { | |
1390 | unsigned basic_cols; | |
cbc9fbd1 | 1391 | |
9607d947 | 1392 | id_cols = MIN(max_id_len, 25u); |
1c0a113f ZJS |
1393 | basic_cols = 1 + id_cols + state_cols; |
1394 | if (basic_cols < (unsigned) columns()) | |
1395 | id_cols += MIN(columns() - basic_cols, max_id_len - id_cols); | |
1396 | } else | |
1397 | id_cols = max_id_len; | |
1398 | ||
11690bcc | 1399 | if (!arg_no_legend && c > 0) |
6ae3e62a ZJS |
1400 | printf("%s%-*s %-*s%s\n", |
1401 | ansi_underline(), | |
cbc9fbd1 | 1402 | id_cols, "UNIT FILE", |
6ae3e62a ZJS |
1403 | state_cols, "STATE", |
1404 | ansi_normal()); | |
729e3769 LP |
1405 | |
1406 | for (u = units; u < units + c; u++) { | |
aaa6732d | 1407 | const char *on_underline = NULL, *on_color = NULL, *off = NULL, *id; |
7fd1b19b | 1408 | _cleanup_free_ char *e = NULL; |
aaa6732d LP |
1409 | bool underline; |
1410 | ||
1411 | underline = u + 1 < units + c && | |
1412 | !streq(unit_type_suffix(u->path), unit_type_suffix((u + 1)->path)); | |
6ae3e62a | 1413 | |
aaa6732d | 1414 | if (underline) |
6ae3e62a | 1415 | on_underline = ansi_underline(); |
729e3769 | 1416 | |
4c315c2c IS |
1417 | if (IN_SET(u->state, |
1418 | UNIT_FILE_MASKED, | |
1419 | UNIT_FILE_MASKED_RUNTIME, | |
1420 | UNIT_FILE_DISABLED, | |
6ae3e62a | 1421 | UNIT_FILE_BAD)) |
aaa6732d | 1422 | on_color = underline ? ansi_highlight_red_underline() : ansi_highlight_red(); |
6ae3e62a | 1423 | else if (u->state == UNIT_FILE_ENABLED) |
aaa6732d LP |
1424 | on_color = underline ? ansi_highlight_green_underline() : ansi_highlight_green(); |
1425 | ||
1426 | if (on_underline || on_color) | |
1427 | off = ansi_normal(); | |
729e3769 | 1428 | |
2b6bf07d | 1429 | id = basename(u->path); |
729e3769 | 1430 | |
1c0a113f | 1431 | e = arg_full ? NULL : ellipsize(id, id_cols, 33); |
729e3769 | 1432 | |
aaa6732d LP |
1433 | printf("%s%-*s %s%-*s%s\n", |
1434 | strempty(on_underline), | |
1c0a113f | 1435 | id_cols, e ? e : id, |
aaa6732d | 1436 | strempty(on_color), state_cols, unit_file_state_to_string(u->state), strempty(off)); |
729e3769 LP |
1437 | } |
1438 | ||
1c0a113f | 1439 | if (!arg_no_legend) |
0d292f5e | 1440 | printf("\n%u unit files listed.\n", c); |
729e3769 LP |
1441 | } |
1442 | ||
e449de87 | 1443 | static int list_unit_files(int argc, char *argv[], void *userdata) { |
4afd3348 | 1444 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
f84190d8 | 1445 | _cleanup_free_ UnitFileList *units = NULL; |
8d5ba5a9 ZJS |
1446 | UnitFileList *unit; |
1447 | size_t size = 0; | |
f459b602 MAP |
1448 | unsigned c = 0; |
1449 | const char *state; | |
1450 | char *path; | |
f84190d8 | 1451 | int r; |
313fe66f | 1452 | bool fallback = false; |
729e3769 | 1453 | |
4fbd7192 | 1454 | if (install_client_side()) { |
729e3769 LP |
1455 | Hashmap *h; |
1456 | UnitFileList *u; | |
1457 | Iterator i; | |
f459b602 | 1458 | unsigned n_units; |
729e3769 | 1459 | |
d5099efc | 1460 | h = hashmap_new(&string_hash_ops); |
0d0f0c50 SL |
1461 | if (!h) |
1462 | return log_oom(); | |
729e3769 | 1463 | |
313fe66f | 1464 | r = unit_file_get_list(arg_scope, arg_root, h, arg_states, strv_skip(argv, 1)); |
729e3769 | 1465 | if (r < 0) { |
8ea913b2 | 1466 | unit_file_list_free(h); |
691395d8 | 1467 | return log_error_errno(r, "Failed to get unit file list: %m"); |
729e3769 LP |
1468 | } |
1469 | ||
1470 | n_units = hashmap_size(h); | |
fdbdf6ec | 1471 | |
0da999fa ZJS |
1472 | units = new(UnitFileList, n_units ?: 1); /* avoid malloc(0) */ |
1473 | if (!units) { | |
729e3769 | 1474 | unit_file_list_free(h); |
0d0f0c50 | 1475 | return log_oom(); |
729e3769 LP |
1476 | } |
1477 | ||
1478 | HASHMAP_FOREACH(u, h, i) { | |
313fe66f | 1479 | if (!output_show_unit_file(u, NULL, NULL)) |
8d5ba5a9 ZJS |
1480 | continue; |
1481 | ||
1482 | units[c++] = *u; | |
729e3769 LP |
1483 | free(u); |
1484 | } | |
1485 | ||
8d5ba5a9 | 1486 | assert(c <= n_units); |
729e3769 | 1487 | hashmap_free(h); |
5f056378 CH |
1488 | |
1489 | r = 0; | |
729e3769 | 1490 | } else { |
313fe66f | 1491 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
4afd3348 | 1492 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
4fbd7192 LP |
1493 | sd_bus *bus; |
1494 | ||
1495 | r = acquire_bus(BUS_MANAGER, &bus); | |
1496 | if (r < 0) | |
1497 | return r; | |
6e646d22 | 1498 | |
313fe66f | 1499 | r = sd_bus_message_new_method_call( |
f22f08cd | 1500 | bus, |
313fe66f | 1501 | &m, |
729e3769 LP |
1502 | "org.freedesktop.systemd1", |
1503 | "/org/freedesktop/systemd1", | |
1504 | "org.freedesktop.systemd1.Manager", | |
313fe66f | 1505 | "ListUnitFilesByPatterns"); |
1506 | if (r < 0) | |
1507 | return bus_log_create_error(r); | |
1508 | ||
1509 | r = sd_bus_message_append_strv(m, arg_states); | |
1510 | if (r < 0) | |
1511 | return bus_log_create_error(r); | |
1512 | ||
1513 | r = sd_bus_message_append_strv(m, strv_skip(argv, 1)); | |
1514 | if (r < 0) | |
1515 | return bus_log_create_error(r); | |
1516 | ||
1517 | r = sd_bus_call(bus, m, 0, &error, &reply); | |
1518 | if (r < 0 && sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_METHOD)) { | |
1519 | /* Fallback to legacy ListUnitFiles method */ | |
1520 | fallback = true; | |
1521 | log_debug_errno(r, "Failed to list unit files: %s Falling back to ListUnitsFiles method.", bus_error_message(&error, r)); | |
1522 | m = sd_bus_message_unref(m); | |
1523 | sd_bus_error_free(&error); | |
1524 | ||
1525 | r = sd_bus_message_new_method_call( | |
1526 | bus, | |
1527 | &m, | |
1528 | "org.freedesktop.systemd1", | |
1529 | "/org/freedesktop/systemd1", | |
1530 | "org.freedesktop.systemd1.Manager", | |
1531 | "ListUnitFiles"); | |
1532 | if (r < 0) | |
1533 | return bus_log_create_error(r); | |
1534 | ||
1535 | r = sd_bus_call(bus, m, 0, &error, &reply); | |
1536 | } | |
691395d8 LP |
1537 | if (r < 0) |
1538 | return log_error_errno(r, "Failed to list unit files: %s", bus_error_message(&error, r)); | |
729e3769 | 1539 | |
f459b602 MAP |
1540 | r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ss)"); |
1541 | if (r < 0) | |
1542 | return bus_log_parse_error(r); | |
729e3769 | 1543 | |
f459b602 | 1544 | while ((r = sd_bus_message_read(reply, "(ss)", &path, &state)) > 0) { |
729e3769 | 1545 | |
f459b602 MAP |
1546 | if (!GREEDY_REALLOC(units, size, c + 1)) |
1547 | return log_oom(); | |
729e3769 | 1548 | |
8d5ba5a9 | 1549 | units[c] = (struct UnitFileList) { |
f459b602 MAP |
1550 | path, |
1551 | unit_file_state_from_string(state) | |
1552 | }; | |
8d5ba5a9 | 1553 | |
313fe66f | 1554 | if (output_show_unit_file(&units[c], |
1555 | fallback ? arg_states : NULL, | |
1556 | fallback ? strv_skip(argv, 1) : NULL)) | |
313cefa1 | 1557 | c++; |
8d5ba5a9 | 1558 | |
729e3769 | 1559 | } |
f459b602 MAP |
1560 | if (r < 0) |
1561 | return bus_log_parse_error(r); | |
1562 | ||
1563 | r = sd_bus_message_exit_container(reply); | |
1564 | if (r < 0) | |
1565 | return bus_log_parse_error(r); | |
729e3769 LP |
1566 | } |
1567 | ||
0221d68a | 1568 | (void) pager_open(arg_pager_flags); |
d2ad7e1b | 1569 | |
93bab288 | 1570 | typesafe_qsort(units, c, compare_unit_file_list); |
4fe1be9c | 1571 | output_unit_file_list(units, c); |
729e3769 | 1572 | |
0da999fa | 1573 | if (install_client_side()) |
8d5ba5a9 ZJS |
1574 | for (unit = units; unit < units + c; unit++) |
1575 | free(unit->path); | |
1576 | ||
f84190d8 | 1577 | return 0; |
729e3769 LP |
1578 | } |
1579 | ||
14cb109d | 1580 | static int list_dependencies_print(const char *name, int level, unsigned branches, bool last) { |
55c0b89c | 1581 | _cleanup_free_ char *n = NULL; |
9607d947 | 1582 | size_t max_len = MAX(columns(),20u); |
cbc9fbd1 LP |
1583 | size_t len = 0; |
1584 | int i; | |
55c0b89c | 1585 | |
5d0c05e5 | 1586 | if (!arg_plain) { |
cbc9fbd1 | 1587 | |
5d0c05e5 LN |
1588 | for (i = level - 1; i >= 0; i--) { |
1589 | len += 2; | |
f168c273 | 1590 | if (len > max_len - 3 && !arg_full) { |
5d0c05e5 LN |
1591 | printf("%s...\n",max_len % 2 ? "" : " "); |
1592 | return 0; | |
1593 | } | |
9a6f746f | 1594 | printf("%s", special_glyph(branches & (1 << i) ? SPECIAL_GLYPH_TREE_VERTICAL : SPECIAL_GLYPH_TREE_SPACE)); |
5d0c05e5 | 1595 | } |
55c0b89c | 1596 | len += 2; |
cbc9fbd1 | 1597 | |
f168c273 | 1598 | if (len > max_len - 3 && !arg_full) { |
55c0b89c LN |
1599 | printf("%s...\n",max_len % 2 ? "" : " "); |
1600 | return 0; | |
1601 | } | |
cbc9fbd1 | 1602 | |
9a6f746f | 1603 | printf("%s", special_glyph(last ? SPECIAL_GLYPH_TREE_RIGHT : SPECIAL_GLYPH_TREE_BRANCH)); |
55c0b89c | 1604 | } |
55c0b89c | 1605 | |
9ed794a3 | 1606 | if (arg_full) { |
55c0b89c LN |
1607 | printf("%s\n", name); |
1608 | return 0; | |
1609 | } | |
1610 | ||
1611 | n = ellipsize(name, max_len-len, 100); | |
f168c273 | 1612 | if (!n) |
55c0b89c LN |
1613 | return log_oom(); |
1614 | ||
1615 | printf("%s\n", n); | |
1616 | return 0; | |
1617 | } | |
1618 | ||
f459b602 | 1619 | static int list_dependencies_get_dependencies(sd_bus *bus, const char *name, char ***deps) { |
32b47279 YW |
1620 | struct DependencyStatusInfo { |
1621 | char **dep[5]; | |
1622 | } info = {}; | |
1623 | ||
1624 | static const struct bus_properties_map map[_DEPENDENCY_MAX][6] = { | |
1625 | [DEPENDENCY_FORWARD] = { | |
1626 | { "Requires", "as", NULL, offsetof(struct DependencyStatusInfo, dep[0]) }, | |
1627 | { "Requisite", "as", NULL, offsetof(struct DependencyStatusInfo, dep[1]) }, | |
1628 | { "Wants", "as", NULL, offsetof(struct DependencyStatusInfo, dep[2]) }, | |
1629 | { "ConsistsOf", "as", NULL, offsetof(struct DependencyStatusInfo, dep[3]) }, | |
1630 | { "BindsTo", "as", NULL, offsetof(struct DependencyStatusInfo, dep[4]) }, | |
1631 | {} | |
1632 | }, | |
1633 | [DEPENDENCY_REVERSE] = { | |
1634 | { "RequiredBy", "as", NULL, offsetof(struct DependencyStatusInfo, dep[0]) }, | |
1635 | { "RequisiteOf", "as", NULL, offsetof(struct DependencyStatusInfo, dep[1]) }, | |
1636 | { "WantedBy", "as", NULL, offsetof(struct DependencyStatusInfo, dep[2]) }, | |
1637 | { "PartOf", "as", NULL, offsetof(struct DependencyStatusInfo, dep[3]) }, | |
1638 | { "BoundBy", "as", NULL, offsetof(struct DependencyStatusInfo, dep[4]) }, | |
1639 | {} | |
1640 | }, | |
1641 | [DEPENDENCY_AFTER] = { | |
1642 | { "After", "as", NULL, offsetof(struct DependencyStatusInfo, dep[0]) }, | |
1643 | {} | |
1644 | }, | |
1645 | [DEPENDENCY_BEFORE] = { | |
1646 | { "Before", "as", NULL, offsetof(struct DependencyStatusInfo, dep[0]) }, | |
1647 | {} | |
1648 | }, | |
afba4199 | 1649 | }; |
55c0b89c | 1650 | |
4afd3348 | 1651 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
f459b602 MAP |
1652 | _cleanup_strv_free_ char **ret = NULL; |
1653 | _cleanup_free_ char *path = NULL; | |
32b47279 | 1654 | int i, r; |
55c0b89c LN |
1655 | |
1656 | assert(bus); | |
1657 | assert(name); | |
1658 | assert(deps); | |
1659 | ||
1660 | path = unit_dbus_path_from_name(name); | |
f459b602 MAP |
1661 | if (!path) |
1662 | return log_oom(); | |
55c0b89c | 1663 | |
32b47279 YW |
1664 | r = bus_map_all_properties(bus, |
1665 | "org.freedesktop.systemd1", | |
1666 | path, | |
1667 | map[arg_dependency], | |
f39cbb6e | 1668 | BUS_MAP_STRDUP, |
32b47279 | 1669 | &error, |
f37f8a61 | 1670 | NULL, |
32b47279 | 1671 | &info); |
691395d8 LP |
1672 | if (r < 0) |
1673 | return log_error_errno(r, "Failed to get properties of %s: %s", name, bus_error_message(&error, r)); | |
55c0b89c | 1674 | |
32b47279 YW |
1675 | if (IN_SET(arg_dependency, DEPENDENCY_AFTER, DEPENDENCY_BEFORE)) { |
1676 | *deps = info.dep[0]; | |
1677 | return 0; | |
1678 | } | |
55c0b89c | 1679 | |
32b47279 YW |
1680 | for (i = 0; i < 5; i++) { |
1681 | r = strv_extend_strv(&ret, info.dep[i], true); | |
f459b602 | 1682 | if (r < 0) |
32b47279 YW |
1683 | return log_oom(); |
1684 | info.dep[i] = strv_free(info.dep[i]); | |
f459b602 | 1685 | } |
540e7dbe | 1686 | |
1cc6c93a | 1687 | *deps = TAKE_PTR(ret); |
540e7dbe | 1688 | |
f459b602 | 1689 | return 0; |
55c0b89c LN |
1690 | } |
1691 | ||
93bab288 | 1692 | static int list_dependencies_compare(char * const *a, char * const *b) { |
55c0b89c LN |
1693 | if (unit_name_to_type(*a) == UNIT_TARGET && unit_name_to_type(*b) != UNIT_TARGET) |
1694 | return 1; | |
1695 | if (unit_name_to_type(*a) != UNIT_TARGET && unit_name_to_type(*b) == UNIT_TARGET) | |
1696 | return -1; | |
cbc9fbd1 | 1697 | |
55c0b89c LN |
1698 | return strcasecmp(*a, *b); |
1699 | } | |
1700 | ||
f459b602 MAP |
1701 | static int list_dependencies_one( |
1702 | sd_bus *bus, | |
1703 | const char *name, | |
1704 | int level, | |
1705 | char ***units, | |
14cb109d | 1706 | unsigned branches) { |
f459b602 | 1707 | |
e3e45d4f | 1708 | _cleanup_strv_free_ char **deps = NULL; |
55c0b89c | 1709 | char **c; |
55c0b89c LN |
1710 | int r = 0; |
1711 | ||
cbc9fbd1 LP |
1712 | assert(bus); |
1713 | assert(name); | |
1714 | assert(units); | |
1715 | ||
e3e45d4f SP |
1716 | r = strv_extend(units, name); |
1717 | if (r < 0) | |
55c0b89c LN |
1718 | return log_oom(); |
1719 | ||
1720 | r = list_dependencies_get_dependencies(bus, name, &deps); | |
1721 | if (r < 0) | |
cec7eda5 | 1722 | return r; |
55c0b89c | 1723 | |
93bab288 | 1724 | typesafe_qsort(deps, strv_length(deps), list_dependencies_compare); |
55c0b89c LN |
1725 | |
1726 | STRV_FOREACH(c, deps) { | |
e3e45d4f | 1727 | if (strv_contains(*units, *c)) { |
5d0c05e5 | 1728 | if (!arg_plain) { |
60705040 | 1729 | printf(" "); |
5d0c05e5 LN |
1730 | r = list_dependencies_print("...", level + 1, (branches << 1) | (c[1] == NULL ? 0 : 1), 1); |
1731 | if (r < 0) | |
1732 | return r; | |
1733 | } | |
55c0b89c LN |
1734 | continue; |
1735 | } | |
1736 | ||
250ba664 ZJS |
1737 | if (arg_plain) |
1738 | printf(" "); | |
1739 | else { | |
fa0d5878 | 1740 | UnitActiveState active_state = _UNIT_ACTIVE_STATE_INVALID; |
250ba664 ZJS |
1741 | const char *on; |
1742 | ||
fa0d5878 | 1743 | (void) get_state_one_unit(bus, *c, &active_state); |
baa9ecc1 | 1744 | |
fa0d5878 | 1745 | switch (active_state) { |
baa9ecc1 LP |
1746 | case UNIT_ACTIVE: |
1747 | case UNIT_RELOADING: | |
1748 | case UNIT_ACTIVATING: | |
1749 | on = ansi_highlight_green(); | |
1750 | break; | |
1751 | ||
1752 | case UNIT_INACTIVE: | |
1753 | case UNIT_DEACTIVATING: | |
1754 | on = ansi_normal(); | |
1755 | break; | |
1756 | ||
1757 | default: | |
1758 | on = ansi_highlight_red(); | |
1759 | break; | |
fa0d5878 BR |
1760 | } |
1761 | ||
9a6f746f | 1762 | printf("%s%s%s ", on, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE), ansi_normal()); |
250ba664 | 1763 | } |
dbc2c080 | 1764 | |
55c0b89c | 1765 | r = list_dependencies_print(*c, level, branches, c[1] == NULL); |
cec7eda5 ZJS |
1766 | if (r < 0) |
1767 | return r; | |
55c0b89c LN |
1768 | |
1769 | if (arg_all || unit_name_to_type(*c) == UNIT_TARGET) { | |
e3e45d4f | 1770 | r = list_dependencies_one(bus, *c, level + 1, units, (branches << 1) | (c[1] == NULL ? 0 : 1)); |
f168c273 | 1771 | if (r < 0) |
cec7eda5 | 1772 | return r; |
55c0b89c LN |
1773 | } |
1774 | } | |
f459b602 | 1775 | |
e3e45d4f SP |
1776 | if (!arg_plain) |
1777 | strv_remove(*units, name); | |
f459b602 | 1778 | |
cec7eda5 | 1779 | return 0; |
55c0b89c LN |
1780 | } |
1781 | ||
e449de87 | 1782 | static int list_dependencies(int argc, char *argv[], void *userdata) { |
5d0c05e5 | 1783 | _cleanup_strv_free_ char **units = NULL; |
f459b602 | 1784 | _cleanup_free_ char *unit = NULL; |
e31165b2 | 1785 | const char *u; |
4fbd7192 | 1786 | sd_bus *bus; |
7410616c | 1787 | int r; |
55c0b89c | 1788 | |
e449de87 | 1789 | if (argv[1]) { |
37cbc1d5 | 1790 | r = unit_name_mangle(argv[1], arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN, &unit); |
7410616c LP |
1791 | if (r < 0) |
1792 | return log_error_errno(r, "Failed to mangle unit name: %m"); | |
1793 | ||
e31165b2 LP |
1794 | u = unit; |
1795 | } else | |
1796 | u = SPECIAL_DEFAULT_TARGET; | |
55c0b89c | 1797 | |
4fbd7192 LP |
1798 | r = acquire_bus(BUS_MANAGER, &bus); |
1799 | if (r < 0) | |
1800 | return r; | |
1801 | ||
0221d68a | 1802 | (void) pager_open(arg_pager_flags); |
d2ad7e1b | 1803 | |
e31165b2 LP |
1804 | puts(u); |
1805 | ||
5d0c05e5 | 1806 | return list_dependencies_one(bus, u, 0, &units, 0); |
55c0b89c LN |
1807 | } |
1808 | ||
0d292f5e LP |
1809 | struct machine_info { |
1810 | bool is_host; | |
1811 | char *name; | |
0d292f5e | 1812 | char *state; |
8fcf784d LP |
1813 | char *control_group; |
1814 | uint32_t n_failed_units; | |
1815 | uint32_t n_jobs; | |
1816 | usec_t timestamp; | |
1817 | }; | |
1818 | ||
1819 | static const struct bus_properties_map machine_info_property_map[] = { | |
1820 | { "SystemState", "s", NULL, offsetof(struct machine_info, state) }, | |
1821 | { "NJobs", "u", NULL, offsetof(struct machine_info, n_jobs) }, | |
1822 | { "NFailedUnits", "u", NULL, offsetof(struct machine_info, n_failed_units) }, | |
1823 | { "ControlGroup", "s", NULL, offsetof(struct machine_info, control_group) }, | |
1824 | { "UserspaceTimestamp", "t", NULL, offsetof(struct machine_info, timestamp) }, | |
1825 | {} | |
0d292f5e LP |
1826 | }; |
1827 | ||
e7e55dbd | 1828 | static void machine_info_clear(struct machine_info *info) { |
f8654baa ZJS |
1829 | assert(info); |
1830 | ||
1831 | free(info->name); | |
1832 | free(info->state); | |
1833 | free(info->control_group); | |
1834 | zero(*info); | |
e7e55dbd DH |
1835 | } |
1836 | ||
0d292f5e LP |
1837 | static void free_machines_list(struct machine_info *machine_infos, int n) { |
1838 | int i; | |
1839 | ||
1840 | if (!machine_infos) | |
1841 | return; | |
1842 | ||
e7e55dbd DH |
1843 | for (i = 0; i < n; i++) |
1844 | machine_info_clear(&machine_infos[i]); | |
0d292f5e LP |
1845 | |
1846 | free(machine_infos); | |
1847 | } | |
1848 | ||
93bab288 YW |
1849 | static int compare_machine_info(const struct machine_info *a, const struct machine_info *b) { |
1850 | int r; | |
0d292f5e | 1851 | |
93bab288 YW |
1852 | r = CMP(b->is_host, a->is_host); |
1853 | if (r != 0) | |
1854 | return r; | |
0d292f5e | 1855 | |
93bab288 | 1856 | return strcasecmp(a->name, b->name); |
0d292f5e LP |
1857 | } |
1858 | ||
1859 | static int get_machine_properties(sd_bus *bus, struct machine_info *mi) { | |
4afd3348 | 1860 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *container = NULL; |
0d292f5e LP |
1861 | int r; |
1862 | ||
1863 | assert(mi); | |
1864 | ||
1865 | if (!bus) { | |
de33fc62 | 1866 | r = sd_bus_open_system_machine(&container, mi->name); |
0d292f5e LP |
1867 | if (r < 0) |
1868 | return r; | |
1869 | ||
1870 | bus = container; | |
1871 | } | |
1872 | ||
a7e4861c YW |
1873 | r = bus_map_all_properties( |
1874 | bus, | |
1875 | "org.freedesktop.systemd1", | |
1876 | "/org/freedesktop/systemd1", | |
1877 | machine_info_property_map, | |
1878 | BUS_MAP_STRDUP, | |
1879 | NULL, | |
1880 | NULL, | |
1881 | mi); | |
0d292f5e LP |
1882 | if (r < 0) |
1883 | return r; | |
1884 | ||
1885 | return 0; | |
1886 | } | |
1887 | ||
1888 | static bool output_show_machine(const char *name, char **patterns) { | |
2404701e | 1889 | return strv_fnmatch_or_empty(patterns, name, FNM_NOESCAPE); |
0d292f5e LP |
1890 | } |
1891 | ||
1892 | static int get_machine_list( | |
1893 | sd_bus *bus, | |
1894 | struct machine_info **_machine_infos, | |
1895 | char **patterns) { | |
1896 | ||
1897 | struct machine_info *machine_infos = NULL; | |
1898 | _cleanup_strv_free_ char **m = NULL; | |
1899 | _cleanup_free_ char *hn = NULL; | |
1900 | size_t sz = 0; | |
1901 | char **i; | |
4c3e8e39 | 1902 | int c = 0, r; |
0d292f5e LP |
1903 | |
1904 | hn = gethostname_malloc(); | |
1905 | if (!hn) | |
1906 | return log_oom(); | |
1907 | ||
1908 | if (output_show_machine(hn, patterns)) { | |
1909 | if (!GREEDY_REALLOC0(machine_infos, sz, c+1)) | |
1910 | return log_oom(); | |
1911 | ||
1912 | machine_infos[c].is_host = true; | |
ae2a15bc | 1913 | machine_infos[c].name = TAKE_PTR(hn); |
0d292f5e | 1914 | |
f9e0eefc | 1915 | (void) get_machine_properties(bus, &machine_infos[c]); |
0d292f5e LP |
1916 | c++; |
1917 | } | |
1918 | ||
4c3e8e39 LP |
1919 | r = sd_get_machine_names(&m); |
1920 | if (r < 0) | |
1921 | return log_error_errno(r, "Failed to get machine list: %m"); | |
1922 | ||
0d292f5e LP |
1923 | STRV_FOREACH(i, m) { |
1924 | _cleanup_free_ char *class = NULL; | |
1925 | ||
1926 | if (!output_show_machine(*i, patterns)) | |
1927 | continue; | |
1928 | ||
1929 | sd_machine_get_class(*i, &class); | |
1930 | if (!streq_ptr(class, "container")) | |
1931 | continue; | |
1932 | ||
1933 | if (!GREEDY_REALLOC0(machine_infos, sz, c+1)) { | |
1934 | free_machines_list(machine_infos, c); | |
1935 | return log_oom(); | |
1936 | } | |
1937 | ||
1938 | machine_infos[c].is_host = false; | |
1939 | machine_infos[c].name = strdup(*i); | |
1940 | if (!machine_infos[c].name) { | |
1941 | free_machines_list(machine_infos, c); | |
1942 | return log_oom(); | |
1943 | } | |
1944 | ||
f9e0eefc | 1945 | (void) get_machine_properties(NULL, &machine_infos[c]); |
0d292f5e LP |
1946 | c++; |
1947 | } | |
1948 | ||
1949 | *_machine_infos = machine_infos; | |
1950 | return c; | |
1951 | } | |
1952 | ||
1953 | static void output_machines_list(struct machine_info *machine_infos, unsigned n) { | |
1954 | struct machine_info *m; | |
1955 | unsigned | |
90c3f79d | 1956 | circle_len = 0, |
fbd0b64f LP |
1957 | namelen = STRLEN("NAME"), |
1958 | statelen = STRLEN("STATE"), | |
1959 | failedlen = STRLEN("FAILED"), | |
1960 | jobslen = STRLEN("JOBS"); | |
0d292f5e LP |
1961 | |
1962 | assert(machine_infos || n == 0); | |
1963 | ||
1964 | for (m = machine_infos; m < machine_infos + n; m++) { | |
fbd0b64f LP |
1965 | namelen = MAX(namelen, |
1966 | strlen(m->name) + (m->is_host ? STRLEN(" (host)") : 0)); | |
7bf7ce28 | 1967 | statelen = MAX(statelen, strlen_ptr(m->state)); |
0d292f5e LP |
1968 | failedlen = MAX(failedlen, DECIMAL_STR_WIDTH(m->n_failed_units)); |
1969 | jobslen = MAX(jobslen, DECIMAL_STR_WIDTH(m->n_jobs)); | |
90c3f79d | 1970 | |
250ba664 | 1971 | if (!arg_plain && !streq_ptr(m->state, "running")) |
90c3f79d | 1972 | circle_len = 2; |
0d292f5e LP |
1973 | } |
1974 | ||
90c3f79d LP |
1975 | if (!arg_no_legend) { |
1976 | if (circle_len > 0) | |
1977 | fputs(" ", stdout); | |
1978 | ||
0d292f5e LP |
1979 | printf("%-*s %-*s %-*s %-*s\n", |
1980 | namelen, "NAME", | |
1981 | statelen, "STATE", | |
1982 | failedlen, "FAILED", | |
1983 | jobslen, "JOBS"); | |
90c3f79d | 1984 | } |
0d292f5e LP |
1985 | |
1986 | for (m = machine_infos; m < machine_infos + n; m++) { | |
90c3f79d LP |
1987 | const char *on_state = "", *off_state = ""; |
1988 | const char *on_failed = "", *off_failed = ""; | |
1989 | bool circle = false; | |
0d292f5e LP |
1990 | |
1991 | if (streq_ptr(m->state, "degraded")) { | |
1992 | on_state = ansi_highlight_red(); | |
1fc464f6 | 1993 | off_state = ansi_normal(); |
90c3f79d | 1994 | circle = true; |
0d292f5e LP |
1995 | } else if (!streq_ptr(m->state, "running")) { |
1996 | on_state = ansi_highlight_yellow(); | |
1fc464f6 | 1997 | off_state = ansi_normal(); |
90c3f79d LP |
1998 | circle = true; |
1999 | } | |
0d292f5e LP |
2000 | |
2001 | if (m->n_failed_units > 0) { | |
2002 | on_failed = ansi_highlight_red(); | |
1fc464f6 | 2003 | off_failed = ansi_normal(); |
0d292f5e LP |
2004 | } else |
2005 | on_failed = off_failed = ""; | |
2006 | ||
90c3f79d | 2007 | if (circle_len > 0) |
9a6f746f | 2008 | printf("%s%s%s ", on_state, circle ? special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE) : " ", off_state); |
90c3f79d | 2009 | |
0d292f5e | 2010 | if (m->is_host) |
c6ba5b80 | 2011 | printf("%-*s (host) %s%-*s%s %s%*" PRIu32 "%s %*" PRIu32 "\n", |
fbd0b64f LP |
2012 | (int) (namelen - (STRLEN(" (host)"))), |
2013 | strna(m->name), | |
0d292f5e LP |
2014 | on_state, statelen, strna(m->state), off_state, |
2015 | on_failed, failedlen, m->n_failed_units, off_failed, | |
2016 | jobslen, m->n_jobs); | |
2017 | else | |
c6ba5b80 | 2018 | printf("%-*s %s%-*s%s %s%*" PRIu32 "%s %*" PRIu32 "\n", |
0d292f5e LP |
2019 | namelen, strna(m->name), |
2020 | on_state, statelen, strna(m->state), off_state, | |
2021 | on_failed, failedlen, m->n_failed_units, off_failed, | |
2022 | jobslen, m->n_jobs); | |
2023 | } | |
2024 | ||
2025 | if (!arg_no_legend) | |
2026 | printf("\n%u machines listed.\n", n); | |
2027 | } | |
2028 | ||
e449de87 | 2029 | static int list_machines(int argc, char *argv[], void *userdata) { |
0d292f5e | 2030 | struct machine_info *machine_infos = NULL; |
4fbd7192 | 2031 | sd_bus *bus; |
0d292f5e LP |
2032 | int r; |
2033 | ||
4fbd7192 LP |
2034 | r = acquire_bus(BUS_MANAGER, &bus); |
2035 | if (r < 0) | |
2036 | return r; | |
2037 | ||
e449de87 | 2038 | r = get_machine_list(bus, &machine_infos, strv_skip(argv, 1)); |
0d292f5e LP |
2039 | if (r < 0) |
2040 | return r; | |
2041 | ||
0221d68a | 2042 | (void) pager_open(arg_pager_flags); |
d2ad7e1b | 2043 | |
93bab288 | 2044 | typesafe_qsort(machine_infos, r, compare_machine_info); |
0d292f5e LP |
2045 | output_machines_list(machine_infos, r); |
2046 | free_machines_list(machine_infos, r); | |
2047 | ||
2048 | return 0; | |
2049 | } | |
2050 | ||
e449de87 | 2051 | static int get_default(int argc, char *argv[], void *userdata) { |
4afd3348 | 2052 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
f459b602 MAP |
2053 | _cleanup_free_ char *_path = NULL; |
2054 | const char *path; | |
99504dd4 | 2055 | int r; |
99504dd4 | 2056 | |
4fbd7192 | 2057 | if (install_client_side()) { |
f459b602 | 2058 | r = unit_file_get_default(arg_scope, arg_root, &_path); |
f647962d MS |
2059 | if (r < 0) |
2060 | return log_error_errno(r, "Failed to get default target: %m"); | |
f459b602 | 2061 | path = _path; |
99504dd4 | 2062 | |
5f056378 | 2063 | r = 0; |
99504dd4 | 2064 | } else { |
4afd3348 | 2065 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
4fbd7192 LP |
2066 | sd_bus *bus; |
2067 | ||
2068 | r = acquire_bus(BUS_MANAGER, &bus); | |
2069 | if (r < 0) | |
2070 | return r; | |
6e646d22 | 2071 | |
f459b602 MAP |
2072 | r = sd_bus_call_method( |
2073 | bus, | |
2074 | "org.freedesktop.systemd1", | |
2075 | "/org/freedesktop/systemd1", | |
2076 | "org.freedesktop.systemd1.Manager", | |
2077 | "GetDefaultTarget", | |
2078 | &error, | |
2079 | &reply, | |
2080 | NULL); | |
691395d8 LP |
2081 | if (r < 0) |
2082 | return log_error_errno(r, "Failed to get default target: %s", bus_error_message(&error, r)); | |
99504dd4 | 2083 | |
f459b602 MAP |
2084 | r = sd_bus_message_read(reply, "s", &path); |
2085 | if (r < 0) | |
2086 | return bus_log_parse_error(r); | |
99504dd4 VP |
2087 | } |
2088 | ||
2089 | if (path) | |
2090 | printf("%s\n", path); | |
2091 | ||
f459b602 | 2092 | return 0; |
99504dd4 VP |
2093 | } |
2094 | ||
e449de87 | 2095 | static int set_default(int argc, char *argv[], void *userdata) { |
718db961 | 2096 | _cleanup_free_ char *unit = NULL; |
acc0269c | 2097 | UnitFileChange *changes = NULL; |
da6053d0 | 2098 | size_t n_changes = 0; |
718db961 LP |
2099 | int r; |
2100 | ||
e449de87 LP |
2101 | assert(argc >= 2); |
2102 | assert(argv); | |
2103 | ||
37cbc1d5 | 2104 | r = unit_name_mangle_with_suffix(argv[1], arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN, ".target", &unit); |
7410616c LP |
2105 | if (r < 0) |
2106 | return log_error_errno(r, "Failed to mangle unit name: %m"); | |
718db961 | 2107 | |
4fbd7192 | 2108 | if (install_client_side()) { |
b3796dd8 | 2109 | r = unit_file_set_default(arg_scope, UNIT_FILE_FORCE, arg_root, unit, &changes, &n_changes); |
af3d8113 | 2110 | unit_file_dump_changes(r, "set default", changes, n_changes, arg_quiet); |
5f056378 CH |
2111 | |
2112 | if (r > 0) | |
2113 | r = 0; | |
718db961 | 2114 | } else { |
4afd3348 LP |
2115 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
2116 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
4fbd7192 | 2117 | sd_bus *bus; |
718db961 | 2118 | |
8a4b13c5 | 2119 | polkit_agent_open_maybe(); |
6e646d22 | 2120 | |
4fbd7192 LP |
2121 | r = acquire_bus(BUS_MANAGER, &bus); |
2122 | if (r < 0) | |
2123 | return r; | |
2124 | ||
6e646d22 | 2125 | r = sd_bus_call_method( |
718db961 LP |
2126 | bus, |
2127 | "org.freedesktop.systemd1", | |
2128 | "/org/freedesktop/systemd1", | |
2129 | "org.freedesktop.systemd1.Manager", | |
6e646d22 LP |
2130 | "SetDefaultTarget", |
2131 | &error, | |
2132 | &reply, | |
2133 | "sb", unit, 1); | |
691395d8 LP |
2134 | if (r < 0) |
2135 | return log_error_errno(r, "Failed to set default target: %s", bus_error_message(&error, r)); | |
718db961 | 2136 | |
acc0269c | 2137 | r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet, &changes, &n_changes); |
718db961 | 2138 | if (r < 0) |
acc0269c | 2139 | goto finish; |
718db961 | 2140 | |
93c941e3 | 2141 | /* Try to reload if enabled */ |
718db961 | 2142 | if (!arg_no_reload) |
e449de87 | 2143 | r = daemon_reload(argc, argv, userdata); |
718db961 LP |
2144 | else |
2145 | r = 0; | |
2146 | } | |
2147 | ||
acc0269c CH |
2148 | finish: |
2149 | unit_file_changes_free(changes, n_changes); | |
2150 | ||
718db961 LP |
2151 | return r; |
2152 | } | |
2153 | ||
76d8ca22 | 2154 | static int output_waiting_jobs(sd_bus *bus, uint32_t id, const char *method, const char *prefix) { |
82948f6c LP |
2155 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
2156 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
31d99bd1 | 2157 | const char *name, *type; |
82948f6c LP |
2158 | uint32_t other_id; |
2159 | int r; | |
2160 | ||
2161 | assert(bus); | |
2162 | ||
2163 | r = sd_bus_call_method( | |
2164 | bus, | |
2165 | "org.freedesktop.systemd1", | |
2166 | "/org/freedesktop/systemd1", | |
2167 | "org.freedesktop.systemd1.Manager", | |
2168 | method, | |
2169 | &error, | |
2170 | &reply, | |
2171 | "u", id); | |
76d8ca22 ZJS |
2172 | if (r < 0) |
2173 | return log_debug_errno(r, "Failed to get waiting jobs for job %" PRIu32, id); | |
82948f6c LP |
2174 | |
2175 | r = sd_bus_message_enter_container(reply, 'a', "(usssoo)"); | |
76d8ca22 ZJS |
2176 | if (r < 0) |
2177 | return bus_log_parse_error(r); | |
82948f6c | 2178 | |
31d99bd1 | 2179 | while ((r = sd_bus_message_read(reply, "(usssoo)", &other_id, &name, &type, NULL, NULL, NULL)) > 0) |
bc357ce5 | 2180 | printf("%s %u (%s/%s)\n", prefix, other_id, name, type); |
76d8ca22 ZJS |
2181 | if (r < 0) |
2182 | return bus_log_parse_error(r); | |
82948f6c LP |
2183 | |
2184 | r = sd_bus_message_exit_container(reply); | |
76d8ca22 ZJS |
2185 | if (r < 0) |
2186 | return bus_log_parse_error(r); | |
2187 | ||
2188 | return 0; | |
82948f6c LP |
2189 | } |
2190 | ||
75add28a ZJS |
2191 | struct job_info { |
2192 | uint32_t id; | |
f459b602 | 2193 | const char *name, *type, *state; |
75add28a ZJS |
2194 | }; |
2195 | ||
82948f6c | 2196 | static void output_jobs_list(sd_bus *bus, const struct job_info* jobs, unsigned n, bool skipped) { |
f459b602 MAP |
2197 | unsigned id_len, unit_len, type_len, state_len; |
2198 | const struct job_info *j; | |
75add28a ZJS |
2199 | const char *on, *off; |
2200 | bool shorten = false; | |
2201 | ||
2202 | assert(n == 0 || jobs); | |
2203 | ||
2204 | if (n == 0) { | |
250ba664 ZJS |
2205 | if (!arg_no_legend) { |
2206 | on = ansi_highlight_green(); | |
1fc464f6 | 2207 | off = ansi_normal(); |
75add28a | 2208 | |
250ba664 ZJS |
2209 | printf("%sNo jobs %s.%s\n", on, skipped ? "listed" : "running", off); |
2210 | } | |
75add28a ZJS |
2211 | return; |
2212 | } | |
2213 | ||
0221d68a | 2214 | (void) pager_open(arg_pager_flags); |
75add28a | 2215 | |
fbd0b64f LP |
2216 | id_len = STRLEN("JOB"); |
2217 | unit_len = STRLEN("UNIT"); | |
2218 | type_len = STRLEN("TYPE"); | |
2219 | state_len = STRLEN("STATE"); | |
6d6d40c9 | 2220 | |
f459b602 MAP |
2221 | for (j = jobs; j < jobs + n; j++) { |
2222 | uint32_t id = j->id; | |
2223 | assert(j->name && j->type && j->state); | |
75add28a | 2224 | |
f459b602 MAP |
2225 | id_len = MAX(id_len, DECIMAL_STR_WIDTH(id)); |
2226 | unit_len = MAX(unit_len, strlen(j->name)); | |
2227 | type_len = MAX(type_len, strlen(j->type)); | |
2228 | state_len = MAX(state_len, strlen(j->state)); | |
2229 | } | |
75add28a | 2230 | |
f459b602 MAP |
2231 | if (!arg_full && id_len + 1 + unit_len + type_len + 1 + state_len > columns()) { |
2232 | unit_len = MAX(33u, columns() - id_len - type_len - state_len - 3); | |
2233 | shorten = true; | |
2234 | } | |
75add28a | 2235 | |
6ce774fd MM |
2236 | if (!arg_no_legend) |
2237 | printf("%*s %-*s %-*s %-*s\n", | |
2238 | id_len, "JOB", | |
2239 | unit_len, "UNIT", | |
2240 | type_len, "TYPE", | |
2241 | state_len, "STATE"); | |
75add28a | 2242 | |
f459b602 MAP |
2243 | for (j = jobs; j < jobs + n; j++) { |
2244 | _cleanup_free_ char *e = NULL; | |
75add28a | 2245 | |
f459b602 MAP |
2246 | if (streq(j->state, "running")) { |
2247 | on = ansi_highlight(); | |
1fc464f6 | 2248 | off = ansi_normal(); |
f459b602 MAP |
2249 | } else |
2250 | on = off = ""; | |
2251 | ||
2252 | e = shorten ? ellipsize(j->name, unit_len, 33) : NULL; | |
2253 | printf("%*u %s%-*s%s %-*s %s%-*s%s\n", | |
2254 | id_len, j->id, | |
2255 | on, unit_len, e ? e : j->name, off, | |
2256 | type_len, j->type, | |
2257 | on, state_len, j->state, off); | |
82948f6c LP |
2258 | |
2259 | if (arg_jobs_after) | |
bc357ce5 | 2260 | output_waiting_jobs(bus, j->id, "GetJobAfter", "\twaiting for job"); |
82948f6c | 2261 | if (arg_jobs_before) |
bc357ce5 | 2262 | output_waiting_jobs(bus, j->id, "GetJobBefore", "\tblocking job"); |
75add28a ZJS |
2263 | } |
2264 | ||
6ce774fd MM |
2265 | if (!arg_no_legend) { |
2266 | on = ansi_highlight(); | |
1fc464f6 | 2267 | off = ansi_normal(); |
75add28a | 2268 | |
6ce774fd MM |
2269 | printf("\n%s%u jobs listed%s.\n", on, n, off); |
2270 | } | |
75add28a ZJS |
2271 | } |
2272 | ||
d8fba7c6 | 2273 | static bool output_show_job(struct job_info *job, char **patterns) { |
2404701e | 2274 | return strv_fnmatch_or_empty(patterns, job->name, FNM_NOESCAPE); |
d8fba7c6 ZJS |
2275 | } |
2276 | ||
e449de87 | 2277 | static int list_jobs(int argc, char *argv[], void *userdata) { |
4afd3348 LP |
2278 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
2279 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
f459b602 | 2280 | _cleanup_free_ struct job_info *jobs = NULL; |
31d99bd1 YW |
2281 | const char *name, *type, *state; |
2282 | bool skipped = false; | |
f459b602 MAP |
2283 | size_t size = 0; |
2284 | unsigned c = 0; | |
4fbd7192 | 2285 | sd_bus *bus; |
f459b602 | 2286 | uint32_t id; |
f84190d8 | 2287 | int r; |
ec14911e | 2288 | |
4fbd7192 LP |
2289 | r = acquire_bus(BUS_MANAGER, &bus); |
2290 | if (r < 0) | |
2291 | return r; | |
2292 | ||
f459b602 | 2293 | r = sd_bus_call_method( |
f22f08cd SP |
2294 | bus, |
2295 | "org.freedesktop.systemd1", | |
2296 | "/org/freedesktop/systemd1", | |
2297 | "org.freedesktop.systemd1.Manager", | |
2298 | "ListJobs", | |
f459b602 | 2299 | &error, |
f22f08cd | 2300 | &reply, |
f459b602 | 2301 | NULL); |
691395d8 LP |
2302 | if (r < 0) |
2303 | return log_error_errno(r, "Failed to list jobs: %s", bus_error_message(&error, r)); | |
7e4249b9 | 2304 | |
f459b602 MAP |
2305 | r = sd_bus_message_enter_container(reply, 'a', "(usssoo)"); |
2306 | if (r < 0) | |
2307 | return bus_log_parse_error(r); | |
7e4249b9 | 2308 | |
31d99bd1 | 2309 | while ((r = sd_bus_message_read(reply, "(usssoo)", &id, &name, &type, &state, NULL, NULL)) > 0) { |
d8fba7c6 ZJS |
2310 | struct job_info job = { id, name, type, state }; |
2311 | ||
e449de87 | 2312 | if (!output_show_job(&job, strv_skip(argv, 1))) { |
d8fba7c6 ZJS |
2313 | skipped = true; |
2314 | continue; | |
2315 | } | |
8fe914ec | 2316 | |
f459b602 MAP |
2317 | if (!GREEDY_REALLOC(jobs, size, c + 1)) |
2318 | return log_oom(); | |
7e4249b9 | 2319 | |
d8fba7c6 | 2320 | jobs[c++] = job; |
7e4249b9 | 2321 | } |
f459b602 MAP |
2322 | if (r < 0) |
2323 | return bus_log_parse_error(r); | |
7e4249b9 | 2324 | |
f459b602 MAP |
2325 | r = sd_bus_message_exit_container(reply); |
2326 | if (r < 0) | |
2327 | return bus_log_parse_error(r); | |
f73e33d9 | 2328 | |
0221d68a | 2329 | (void) pager_open(arg_pager_flags); |
d2ad7e1b | 2330 | |
82948f6c | 2331 | output_jobs_list(bus, jobs, c, skipped); |
91d0f17e | 2332 | return 0; |
7e4249b9 LP |
2333 | } |
2334 | ||
e449de87 | 2335 | static int cancel_job(int argc, char *argv[], void *userdata) { |
4fbd7192 | 2336 | sd_bus *bus; |
729e3769 | 2337 | char **name; |
342641fb | 2338 | int r = 0; |
7e4249b9 | 2339 | |
e449de87 | 2340 | if (argc <= 1) |
2853b60a | 2341 | return trivial_method(argc, argv, userdata); |
ee5762e3 | 2342 | |
4fbd7192 LP |
2343 | r = acquire_bus(BUS_MANAGER, &bus); |
2344 | if (r < 0) | |
2345 | return r; | |
2346 | ||
8a4b13c5 | 2347 | polkit_agent_open_maybe(); |
d2ad7e1b | 2348 | |
e449de87 | 2349 | STRV_FOREACH(name, strv_skip(argv, 1)) { |
4afd3348 | 2350 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
5dd9014f | 2351 | uint32_t id; |
342641fb | 2352 | int q; |
7e4249b9 | 2353 | |
342641fb | 2354 | q = safe_atou32(*name, &id); |
f647962d MS |
2355 | if (q < 0) |
2356 | return log_error_errno(q, "Failed to parse job id \"%s\": %m", *name); | |
7e4249b9 | 2357 | |
6e646d22 | 2358 | q = sd_bus_call_method( |
f22f08cd SP |
2359 | bus, |
2360 | "org.freedesktop.systemd1", | |
2361 | "/org/freedesktop/systemd1", | |
2362 | "org.freedesktop.systemd1.Manager", | |
6e646d22 LP |
2363 | "CancelJob", |
2364 | &error, | |
2365 | NULL, | |
2366 | "u", id); | |
342641fb | 2367 | if (q < 0) { |
691395d8 | 2368 | log_error_errno(q, "Failed to cancel job %"PRIu32": %s", id, bus_error_message(&error, q)); |
342641fb LP |
2369 | if (r == 0) |
2370 | r = q; | |
f459b602 | 2371 | } |
7e4249b9 LP |
2372 | } |
2373 | ||
342641fb | 2374 | return r; |
7e4249b9 LP |
2375 | } |
2376 | ||
f459b602 | 2377 | static int need_daemon_reload(sd_bus *bus, const char *unit) { |
4afd3348 | 2378 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
f459b602 MAP |
2379 | const char *path; |
2380 | int b, r; | |
94c01aeb | 2381 | |
f459b602 MAP |
2382 | /* We ignore all errors here, since this is used to show a |
2383 | * warning only */ | |
45fb0699 | 2384 | |
f459b602 MAP |
2385 | /* We don't use unit_dbus_path_from_name() directly since we |
2386 | * don't want to load the unit if it isn't loaded. */ | |
f84190d8 | 2387 | |
f459b602 | 2388 | r = sd_bus_call_method( |
f22f08cd SP |
2389 | bus, |
2390 | "org.freedesktop.systemd1", | |
2391 | "/org/freedesktop/systemd1", | |
2392 | "org.freedesktop.systemd1.Manager", | |
2393 | "GetUnit", | |
f459b602 | 2394 | NULL, |
f22f08cd | 2395 | &reply, |
e3e0314b | 2396 | "s", unit); |
f84190d8 LP |
2397 | if (r < 0) |
2398 | return r; | |
45fb0699 | 2399 | |
f459b602 MAP |
2400 | r = sd_bus_message_read(reply, "o", &path); |
2401 | if (r < 0) | |
2402 | return r; | |
f84190d8 | 2403 | |
f459b602 | 2404 | r = sd_bus_get_property_trivial( |
f22f08cd | 2405 | bus, |
b0193f1c LP |
2406 | "org.freedesktop.systemd1", |
2407 | path, | |
f459b602 MAP |
2408 | "org.freedesktop.systemd1.Unit", |
2409 | "NeedDaemonReload", | |
2410 | NULL, | |
2411 | 'b', &b); | |
f84190d8 LP |
2412 | if (r < 0) |
2413 | return r; | |
45fb0699 | 2414 | |
45fb0699 LP |
2415 | return b; |
2416 | } | |
2417 | ||
3f36991e | 2418 | static void warn_unit_file_changed(const char *name) { |
87ec20ef LP |
2419 | assert(name); |
2420 | ||
e9f4aabd | 2421 | 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 | 2422 | ansi_highlight_red(), |
1fc464f6 | 2423 | ansi_normal(), |
3f36991e ZJS |
2424 | name, |
2425 | arg_scope == UNIT_FILE_SYSTEM ? "" : " --user"); | |
2426 | } | |
2427 | ||
c45e7e0c | 2428 | static int unit_file_find_path(LookupPaths *lp, const char *unit_name, char **ret_unit_path) { |
33f6c497 ZJS |
2429 | char **p; |
2430 | ||
2431 | assert(lp); | |
2432 | assert(unit_name); | |
33f6c497 | 2433 | |
a3c4eb07 | 2434 | STRV_FOREACH(p, lp->search_path) { |
c6dd36b6 FS |
2435 | _cleanup_free_ char *path = NULL, *lpath = NULL; |
2436 | int r; | |
33f6c497 | 2437 | |
62a85ee0 | 2438 | path = path_join(*p, unit_name); |
33f6c497 ZJS |
2439 | if (!path) |
2440 | return log_oom(); | |
2441 | ||
c4f4fce7 | 2442 | r = chase_symlinks(path, arg_root, 0, &lpath); |
c6dd36b6 FS |
2443 | if (r == -ENOENT) |
2444 | continue; | |
2445 | if (r == -ENOMEM) | |
2446 | return log_oom(); | |
2447 | if (r < 0) | |
86f004fb | 2448 | return log_error_errno(r, "Failed to access path \"%s\": %m", path); |
c6dd36b6 | 2449 | |
c45e7e0c LP |
2450 | if (ret_unit_path) |
2451 | *ret_unit_path = TAKE_PTR(lpath); | |
ae2a15bc | 2452 | |
c6dd36b6 | 2453 | return 1; |
33f6c497 ZJS |
2454 | } |
2455 | ||
f67cb270 LP |
2456 | if (ret_unit_path) |
2457 | *ret_unit_path = NULL; | |
2458 | ||
33f6c497 ZJS |
2459 | return 0; |
2460 | } | |
2461 | ||
173471b7 ZJS |
2462 | static int unit_find_template_path( |
2463 | const char *unit_name, | |
2464 | LookupPaths *lp, | |
b03677e2 LP |
2465 | char **ret_fragment_path, |
2466 | char **ret_template) { | |
173471b7 | 2467 | |
b03677e2 | 2468 | _cleanup_free_ char *t = NULL, *f = NULL; |
173471b7 ZJS |
2469 | int r; |
2470 | ||
2471 | /* Returns 1 if a fragment was found, 0 if not found, negative on error. */ | |
2472 | ||
b03677e2 LP |
2473 | r = unit_file_find_path(lp, unit_name, &f); |
2474 | if (r < 0) | |
2475 | return r; | |
2476 | if (r > 0) { | |
2477 | if (ret_fragment_path) | |
2478 | *ret_fragment_path = TAKE_PTR(f); | |
2479 | if (ret_template) | |
2480 | *ret_template = NULL; | |
2481 | return r; /* found a real unit */ | |
2482 | } | |
2483 | ||
2484 | r = unit_name_template(unit_name, &t); | |
2485 | if (r == -EINVAL) { | |
2486 | if (ret_fragment_path) | |
2487 | *ret_fragment_path = NULL; | |
2488 | if (ret_template) | |
2489 | *ret_template = NULL; | |
173471b7 | 2490 | |
173471b7 | 2491 | return 0; /* not a template, does not exist */ |
b03677e2 | 2492 | } |
173471b7 ZJS |
2493 | if (r < 0) |
2494 | return log_error_errno(r, "Failed to determine template name: %m"); | |
2495 | ||
b03677e2 | 2496 | r = unit_file_find_path(lp, t, ret_fragment_path); |
173471b7 ZJS |
2497 | if (r < 0) |
2498 | return r; | |
2499 | ||
b03677e2 LP |
2500 | if (ret_template) |
2501 | *ret_template = r > 0 ? TAKE_PTR(t) : NULL; | |
ae2a15bc | 2502 | |
173471b7 ZJS |
2503 | return r; |
2504 | } | |
2505 | ||
6e646d22 LP |
2506 | static int unit_find_paths( |
2507 | sd_bus *bus, | |
2508 | const char *unit_name, | |
6e646d22 | 2509 | LookupPaths *lp, |
02a126a3 LP |
2510 | bool force_client_side, |
2511 | char **ret_fragment_path, | |
2512 | char ***ret_dropin_paths) { | |
dab2bce8 | 2513 | |
dab2bce8 | 2514 | _cleanup_strv_free_ char **dropins = NULL; |
02a126a3 | 2515 | _cleanup_free_ char *path = NULL; |
33f6c497 ZJS |
2516 | int r; |
2517 | ||
ad2a0358 | 2518 | /** |
02a126a3 LP |
2519 | * Finds where the unit is defined on disk. Returns 0 if the unit is not found. Returns 1 if it is found, and |
2520 | * sets: | |
2521 | * - the path to the unit in *ret_frament_path, if it exists on disk, | |
2522 | * - and a strv of existing drop-ins in *ret_dropin_paths, if the arg is not NULL and any dropins were found. | |
2523 | * | |
2524 | * Returns -ERFKILL if the unit is masked, and -EKEYREJECTED if the unit file could not be loaded for some | |
2525 | * reason (the latter only applies if we are going through the service manager) | |
ad2a0358 ZJS |
2526 | */ |
2527 | ||
33f6c497 | 2528 | assert(unit_name); |
02a126a3 | 2529 | assert(ret_fragment_path); |
33f6c497 ZJS |
2530 | assert(lp); |
2531 | ||
02a126a3 LP |
2532 | /* Go via the bus to acquire the path, unless we are explicitly told not to, or when the unit name is a template */ |
2533 | if (!force_client_side && | |
2534 | !install_client_side() && | |
2535 | !unit_name_is_valid(unit_name, UNIT_NAME_TEMPLATE)) { | |
4afd3348 | 2536 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
02a126a3 | 2537 | _cleanup_free_ char *load_state = NULL, *unit = NULL; |
33f6c497 ZJS |
2538 | |
2539 | unit = unit_dbus_path_from_name(unit_name); | |
2540 | if (!unit) | |
2541 | return log_oom(); | |
2542 | ||
02a126a3 LP |
2543 | r = sd_bus_get_property_string( |
2544 | bus, | |
2545 | "org.freedesktop.systemd1", | |
2546 | unit, | |
2547 | "org.freedesktop.systemd1.Unit", | |
2548 | "LoadState", | |
2549 | &error, | |
2550 | &load_state); | |
2551 | if (r < 0) | |
2552 | return log_error_errno(r, "Failed to get LoadState: %s", bus_error_message(&error, r)); | |
2553 | ||
2554 | if (streq(load_state, "masked")) | |
2555 | return -ERFKILL; | |
2556 | if (streq(load_state, "not-found")) { | |
2557 | r = 0; | |
2558 | goto not_found; | |
2559 | } | |
2560 | if (!streq(load_state, "loaded")) | |
2561 | return -EKEYREJECTED; | |
2562 | ||
33f6c497 ZJS |
2563 | r = sd_bus_get_property_string( |
2564 | bus, | |
2565 | "org.freedesktop.systemd1", | |
2566 | unit, | |
2567 | "org.freedesktop.systemd1.Unit", | |
2568 | "FragmentPath", | |
2569 | &error, | |
ad2a0358 ZJS |
2570 | &path); |
2571 | if (r < 0) | |
2572 | return log_error_errno(r, "Failed to get FragmentPath: %s", bus_error_message(&error, r)); | |
2573 | ||
02a126a3 | 2574 | if (ret_dropin_paths) { |
dab2bce8 IS |
2575 | r = sd_bus_get_property_strv( |
2576 | bus, | |
2577 | "org.freedesktop.systemd1", | |
2578 | unit, | |
2579 | "org.freedesktop.systemd1.Unit", | |
2580 | "DropInPaths", | |
2581 | &error, | |
2582 | &dropins); | |
2583 | if (r < 0) | |
2584 | return log_error_errno(r, "Failed to get DropInPaths: %s", bus_error_message(&error, r)); | |
33f6c497 | 2585 | } |
ad2a0358 | 2586 | } else { |
173471b7 | 2587 | _cleanup_set_free_ Set *names = NULL; |
6483ad89 | 2588 | _cleanup_free_ char *template = NULL; |
33f6c497 | 2589 | |
ad2a0358 ZJS |
2590 | names = set_new(NULL); |
2591 | if (!names) | |
7410616c | 2592 | return log_oom(); |
33f6c497 | 2593 | |
173471b7 | 2594 | r = unit_find_template_path(unit_name, lp, &path, &template); |
ad2a0358 ZJS |
2595 | if (r < 0) |
2596 | return r; | |
86f004fb ZJS |
2597 | if (r > 0) { |
2598 | if (null_or_empty_path(path)) | |
2599 | /* The template is masked. Let's cut the process short. */ | |
2600 | return -ERFKILL; | |
2601 | ||
6483ad89 ZJS |
2602 | /* We found the unit file. If we followed symlinks, this name might be |
2603 | * different then the unit_name with started with. Look for dropins matching | |
2604 | * that "final" name. */ | |
2605 | r = set_put(names, basename(path)); | |
86f004fb | 2606 | } else if (!template) |
6483ad89 ZJS |
2607 | /* No unit file, let's look for dropins matching the original name. |
2608 | * systemd has fairly complicated rules (based on unit type and provenience), | |
2609 | * which units are allowed not to have the main unit file. We err on the | |
2610 | * side of including too many files, and always try to load dropins. */ | |
2611 | r = set_put(names, unit_name); | |
2612 | else | |
2613 | /* The cases where we allow a unit to exist without the main file are | |
2614 | * never valid for templates. Don't try to load dropins in this case. */ | |
2615 | goto not_found; | |
2616 | ||
c6dd36b6 FS |
2617 | if (r < 0) |
2618 | return log_error_errno(r, "Failed to add unit name: %m"); | |
2619 | ||
02a126a3 LP |
2620 | if (ret_dropin_paths) { |
2621 | r = unit_file_find_dropin_conf_paths(arg_root, lp->search_path, NULL, names, &dropins); | |
dab2bce8 IS |
2622 | if (r < 0) |
2623 | return r; | |
2624 | } | |
2625 | } | |
2626 | ||
02a126a3 | 2627 | r = 0; |
dab2bce8 IS |
2628 | |
2629 | if (!isempty(path)) { | |
02a126a3 | 2630 | *ret_fragment_path = TAKE_PTR(path); |
dab2bce8 | 2631 | r = 1; |
02a126a3 LP |
2632 | } else |
2633 | *ret_fragment_path = NULL; | |
dab2bce8 | 2634 | |
02a126a3 LP |
2635 | if (ret_dropin_paths) { |
2636 | if (!strv_isempty(dropins)) { | |
2637 | *ret_dropin_paths = TAKE_PTR(dropins); | |
2638 | r = 1; | |
2639 | } else | |
2640 | *ret_dropin_paths = NULL; | |
33f6c497 | 2641 | } |
02a126a3 | 2642 | |
6483ad89 | 2643 | not_found: |
39c38ce1 | 2644 | if (r == 0 && !arg_force) |
b5e6a600 IS |
2645 | log_error("No files found for %s.", unit_name); |
2646 | ||
ad2a0358 | 2647 | return r; |
33f6c497 ZJS |
2648 | } |
2649 | ||
fa0d5878 | 2650 | static int get_state_one_unit(sd_bus *bus, const char *name, UnitActiveState *active_state) { |
b430f85f | 2651 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
3d083b22 | 2652 | _cleanup_free_ char *buf = NULL, *path = NULL; |
fa0d5878 | 2653 | UnitActiveState state; |
f22f08cd | 2654 | int r; |
701cdcb9 | 2655 | |
31be1221 | 2656 | assert(name); |
fa0d5878 | 2657 | assert(active_state); |
701cdcb9 | 2658 | |
3d083b22 LP |
2659 | path = unit_dbus_path_from_name(name); |
2660 | if (!path) | |
2661 | return log_oom(); | |
2662 | ||
2663 | r = sd_bus_get_property_string( | |
f22f08cd SP |
2664 | bus, |
2665 | "org.freedesktop.systemd1", | |
3d083b22 LP |
2666 | path, |
2667 | "org.freedesktop.systemd1.Unit", | |
2668 | "ActiveState", | |
b430f85f | 2669 | &error, |
3d083b22 LP |
2670 | &buf); |
2671 | if (r < 0) | |
2672 | return log_error_errno(r, "Failed to retrieve unit state: %s", bus_error_message(&error, r)); | |
b430f85f | 2673 | |
3d083b22 LP |
2674 | state = unit_active_state_from_string(buf); |
2675 | if (state == _UNIT_ACTIVE_STATE_INVALID) { | |
2676 | log_error("Invalid unit state '%s' for: %s", buf, name); | |
2677 | return -EINVAL; | |
60f9ba0b | 2678 | } |
701cdcb9 | 2679 | |
fa0d5878 BR |
2680 | *active_state = state; |
2681 | return 0; | |
701cdcb9 MS |
2682 | } |
2683 | ||
86f004fb | 2684 | static int unit_is_masked(sd_bus *bus, LookupPaths *lp, const char *name) { |
bd062910 | 2685 | _cleanup_free_ char *load_state = NULL; |
f22f08cd | 2686 | int r; |
701cdcb9 | 2687 | |
86f004fb ZJS |
2688 | if (unit_name_is_valid(name, UNIT_NAME_TEMPLATE)) { |
2689 | _cleanup_free_ char *path = NULL; | |
2690 | ||
2691 | /* A template cannot be loaded, but it can be still masked, so | |
2692 | * we need to use a different method. */ | |
2693 | ||
2694 | r = unit_file_find_path(lp, name, &path); | |
2695 | if (r < 0) | |
2696 | return r; | |
2697 | if (r == 0) | |
2698 | return false; | |
2699 | return null_or_empty_path(path); | |
2700 | } | |
2701 | ||
bd062910 | 2702 | r = unit_load_state(bus, name, &load_state); |
691395d8 | 2703 | if (r < 0) |
bd062910 | 2704 | return r; |
d0a5cdb2 | 2705 | |
9d9dd746 ZJS |
2706 | return streq(load_state, "masked"); |
2707 | } | |
2708 | ||
2709 | static int check_triggering_units(sd_bus *bus, const char *name) { | |
2710 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
86f004fb | 2711 | _cleanup_free_ char *n = NULL, *path = NULL, *load_state = NULL; |
9d9dd746 ZJS |
2712 | _cleanup_strv_free_ char **triggered_by = NULL; |
2713 | bool print_warning_label = true; | |
2714 | UnitActiveState active_state; | |
2715 | char **i; | |
2716 | int r; | |
2717 | ||
2718 | r = unit_name_mangle(name, 0, &n); | |
2719 | if (r < 0) | |
2720 | return log_error_errno(r, "Failed to mangle unit name: %m"); | |
2721 | ||
86f004fb ZJS |
2722 | r = unit_load_state(bus, name, &load_state); |
2723 | if (r < 0) | |
2724 | return r; | |
2725 | ||
2726 | if (streq(load_state, "masked")) | |
2727 | return 0; | |
9d9dd746 ZJS |
2728 | |
2729 | path = unit_dbus_path_from_name(n); | |
2730 | if (!path) | |
2731 | return log_oom(); | |
701cdcb9 | 2732 | |
f459b602 MAP |
2733 | r = sd_bus_get_property_strv( |
2734 | bus, | |
2735 | "org.freedesktop.systemd1", | |
2736 | path, | |
2737 | "org.freedesktop.systemd1.Unit", | |
2738 | "TriggeredBy", | |
2739 | &error, | |
2740 | &triggered_by); | |
691395d8 LP |
2741 | if (r < 0) |
2742 | return log_error_errno(r, "Failed to get triggered by array of %s: %s", n, bus_error_message(&error, r)); | |
701cdcb9 | 2743 | |
f459b602 | 2744 | STRV_FOREACH(i, triggered_by) { |
fa0d5878 | 2745 | r = get_state_one_unit(bus, *i, &active_state); |
f647962d | 2746 | if (r < 0) |
fa0d5878 | 2747 | return r; |
701cdcb9 | 2748 | |
fa0d5878 | 2749 | if (!IN_SET(active_state, UNIT_ACTIVE, UNIT_RELOADING)) |
f459b602 | 2750 | continue; |
60f9ba0b | 2751 | |
f459b602 MAP |
2752 | if (print_warning_label) { |
2753 | log_warning("Warning: Stopping %s, but it can still be activated by:", n); | |
2754 | print_warning_label = false; | |
701cdcb9 | 2755 | } |
1c291cf3 | 2756 | |
f459b602 | 2757 | log_warning(" %s", *i); |
701cdcb9 | 2758 | } |
f459b602 MAP |
2759 | |
2760 | return 0; | |
701cdcb9 MS |
2761 | } |
2762 | ||
2fc9a280 LP |
2763 | static const struct { |
2764 | const char *verb; | |
2765 | const char *method; | |
2766 | } unit_actions[] = { | |
2767 | { "start", "StartUnit" }, | |
2768 | { "stop", "StopUnit" }, | |
2769 | { "condstop", "StopUnit" }, | |
2770 | { "reload", "ReloadUnit" }, | |
2771 | { "restart", "RestartUnit" }, | |
2772 | { "try-restart", "TryRestartUnit" }, | |
2773 | { "condrestart", "TryRestartUnit" }, | |
2774 | { "reload-or-restart", "ReloadOrRestartUnit" }, | |
aabf5d42 | 2775 | { "try-reload-or-restart", "ReloadOrTryRestartUnit" }, |
2fc9a280 LP |
2776 | { "reload-or-try-restart", "ReloadOrTryRestartUnit" }, |
2777 | { "condreload", "ReloadOrTryRestartUnit" }, | |
2778 | { "force-reload", "ReloadOrTryRestartUnit" } | |
2779 | }; | |
2780 | ||
39602c39 TA |
2781 | static const char *verb_to_method(const char *verb) { |
2782 | uint i; | |
2783 | ||
2784 | for (i = 0; i < ELEMENTSOF(unit_actions); i++) | |
2785 | if (streq_ptr(unit_actions[i].verb, verb)) | |
2786 | return unit_actions[i].method; | |
2787 | ||
2788 | return "StartUnit"; | |
2789 | } | |
2790 | ||
2791 | static const char *method_to_verb(const char *method) { | |
2792 | uint i; | |
2793 | ||
2794 | for (i = 0; i < ELEMENTSOF(unit_actions); i++) | |
2795 | if (streq_ptr(unit_actions[i].method, method)) | |
2796 | return unit_actions[i].verb; | |
2797 | ||
2798 | return "n/a"; | |
2799 | } | |
2800 | ||
93a08841 MP |
2801 | typedef struct { |
2802 | sd_bus_slot *match; | |
2803 | sd_event *event; | |
2804 | Set *unit_paths; | |
2805 | bool any_failed; | |
2806 | } WaitContext; | |
2807 | ||
2808 | static void wait_context_free(WaitContext *c) { | |
2809 | c->match = sd_bus_slot_unref(c->match); | |
2810 | c->event = sd_event_unref(c->event); | |
6fa4160d | 2811 | c->unit_paths = set_free_free(c->unit_paths); |
93a08841 MP |
2812 | } |
2813 | ||
2814 | static int on_properties_changed(sd_bus_message *m, void *userdata, sd_bus_error *error) { | |
109b0cbf | 2815 | const char *path, *interface, *active_state = NULL, *job_path = NULL; |
93a08841 | 2816 | WaitContext *c = userdata; |
109b0cbf | 2817 | bool is_failed; |
93a08841 MP |
2818 | int r; |
2819 | ||
109b0cbf LP |
2820 | /* Called whenever we get a PropertiesChanged signal. Checks if ActiveState changed to inactive/failed. |
2821 | * | |
2822 | * Signal parameters: (s interface, a{sv} changed_properties, as invalidated_properties) */ | |
2823 | ||
93a08841 MP |
2824 | path = sd_bus_message_get_path(m); |
2825 | if (!set_contains(c->unit_paths, path)) | |
2826 | return 0; | |
2827 | ||
109b0cbf | 2828 | r = sd_bus_message_read(m, "s", &interface); |
93a08841 MP |
2829 | if (r < 0) |
2830 | return bus_log_parse_error(r); | |
6fa4160d | 2831 | |
109b0cbf LP |
2832 | if (!streq(interface, "org.freedesktop.systemd1.Unit")) /* ActiveState is on the Unit interface */ |
2833 | return 0; | |
2834 | ||
93a08841 MP |
2835 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "{sv}"); |
2836 | if (r < 0) | |
2837 | return bus_log_parse_error(r); | |
2838 | ||
109b0cbf | 2839 | for (;;) { |
93a08841 | 2840 | const char *s; |
93a08841 | 2841 | |
109b0cbf | 2842 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_DICT_ENTRY, "sv"); |
93a08841 MP |
2843 | if (r < 0) |
2844 | return bus_log_parse_error(r); | |
109b0cbf LP |
2845 | if (r == 0) /* end of array */ |
2846 | break; | |
6fa4160d | 2847 | |
109b0cbf LP |
2848 | r = sd_bus_message_read(m, "s", &s); /* Property name */ |
2849 | if (r < 0) | |
2850 | return bus_log_parse_error(r); | |
6fa4160d | 2851 | |
109b0cbf LP |
2852 | if (streq(s, "ActiveState")) { |
2853 | r = sd_bus_message_read(m, "v", "s", &active_state); | |
93a08841 MP |
2854 | if (r < 0) |
2855 | return bus_log_parse_error(r); | |
6fa4160d | 2856 | |
109b0cbf LP |
2857 | if (job_path) /* Found everything we need */ |
2858 | break; | |
2859 | ||
2860 | } else if (streq(s, "Job")) { | |
2861 | uint32_t job_id; | |
2862 | ||
2863 | r = sd_bus_message_read(m, "v", "(uo)", &job_id, &job_path); | |
93a08841 MP |
2864 | if (r < 0) |
2865 | return bus_log_parse_error(r); | |
6fa4160d | 2866 | |
109b0cbf LP |
2867 | /* There's still a job pending for this unit, let's ignore this for now, and return right-away. */ |
2868 | if (job_id != 0) | |
2869 | return 0; | |
2870 | ||
2871 | if (active_state) /* Found everything we need */ | |
2872 | break; | |
6fa4160d | 2873 | |
93a08841 | 2874 | } else { |
109b0cbf | 2875 | r = sd_bus_message_skip(m, "v"); /* Other property */ |
93a08841 MP |
2876 | if (r < 0) |
2877 | return bus_log_parse_error(r); | |
2878 | } | |
109b0cbf | 2879 | |
93a08841 MP |
2880 | r = sd_bus_message_exit_container(m); |
2881 | if (r < 0) | |
2882 | return bus_log_parse_error(r); | |
2883 | } | |
109b0cbf LP |
2884 | |
2885 | /* If this didn't contain the ActiveState property we can't do anything */ | |
2886 | if (!active_state) | |
2887 | return 0; | |
2888 | ||
2889 | is_failed = streq(active_state, "failed"); | |
2890 | if (streq(active_state, "inactive") || is_failed) { | |
2891 | log_debug("%s became %s, dropping from --wait tracking", path, active_state); | |
2892 | free(set_remove(c->unit_paths, path)); | |
2893 | c->any_failed = c->any_failed || is_failed; | |
2894 | } else | |
2895 | log_debug("ActiveState on %s changed to %s", path, active_state); | |
93a08841 MP |
2896 | |
2897 | if (set_isempty(c->unit_paths)) | |
2898 | sd_event_exit(c->event, EXIT_SUCCESS); | |
2899 | ||
2900 | return 0; | |
2901 | } | |
2902 | ||
9a199645 LP |
2903 | static int wait_context_watch( |
2904 | WaitContext *wait_context, | |
2905 | sd_bus *bus, | |
2906 | const char *name) { | |
2907 | ||
2908 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
2909 | _cleanup_free_ char *unit_path = NULL; | |
2910 | int r; | |
2911 | ||
2912 | assert(wait_context); | |
2913 | assert(name); | |
2914 | ||
2915 | log_debug("Watching for property changes of %s", name); | |
2916 | r = sd_bus_call_method( | |
2917 | bus, | |
2918 | "org.freedesktop.systemd1", | |
2919 | "/org/freedesktop/systemd1", | |
2920 | "org.freedesktop.systemd1.Manager", | |
2921 | "RefUnit", | |
2922 | &error, | |
2923 | NULL, | |
2924 | "s", name); | |
2925 | if (r < 0) | |
2926 | return log_error_errno(r, "Failed to add reference to unit %s: %s", name, bus_error_message(&error, r)); | |
2927 | ||
2928 | unit_path = unit_dbus_path_from_name(name); | |
2929 | if (!unit_path) | |
2930 | return log_oom(); | |
2931 | ||
2932 | r = set_ensure_allocated(&wait_context->unit_paths, &string_hash_ops); | |
2933 | if (r < 0) | |
2934 | return log_oom(); | |
2935 | ||
2936 | r = set_put_strdup(wait_context->unit_paths, unit_path); | |
2937 | if (r < 0) | |
2938 | return log_error_errno(r, "Failed to add unit path %s to set: %m", unit_path); | |
2939 | ||
2940 | r = sd_bus_match_signal_async(bus, | |
2941 | &wait_context->match, | |
2942 | NULL, | |
2943 | unit_path, | |
2944 | "org.freedesktop.DBus.Properties", | |
2945 | "PropertiesChanged", | |
2946 | on_properties_changed, NULL, wait_context); | |
2947 | if (r < 0) | |
2948 | return log_error_errno(r, "Failed to request match for PropertiesChanged signal: %m"); | |
2949 | ||
2950 | return 0; | |
2951 | } | |
2952 | ||
e4b61340 | 2953 | static int start_unit_one( |
f459b602 | 2954 | sd_bus *bus, |
e4b61340 LP |
2955 | const char *method, |
2956 | const char *name, | |
2957 | const char *mode, | |
f459b602 | 2958 | sd_bus_error *error, |
93a08841 MP |
2959 | BusWaitForJobs *w, |
2960 | WaitContext *wait_context) { | |
7e4249b9 | 2961 | |
4afd3348 | 2962 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
45fb0699 | 2963 | const char *path; |
7e4249b9 | 2964 | int r; |
7e4249b9 | 2965 | |
e4b61340 LP |
2966 | assert(method); |
2967 | assert(name); | |
2968 | assert(mode); | |
22f4096c | 2969 | assert(error); |
7e4249b9 | 2970 | |
93a08841 | 2971 | if (wait_context) { |
9a199645 | 2972 | r = wait_context_watch(wait_context, bus, name); |
93a08841 | 2973 | if (r < 0) |
9a199645 | 2974 | return r; |
93a08841 MP |
2975 | } |
2976 | ||
dbc9830c ZJS |
2977 | log_debug("%s dbus call org.freedesktop.systemd1.Manager %s(%s, %s)", |
2978 | arg_dry_run ? "Would execute" : "Executing", | |
1ae17672 ZJS |
2979 | method, name, mode); |
2980 | if (arg_dry_run) | |
2981 | return 0; | |
342641fb | 2982 | |
6e646d22 | 2983 | r = sd_bus_call_method( |
f22f08cd | 2984 | bus, |
b0193f1c LP |
2985 | "org.freedesktop.systemd1", |
2986 | "/org/freedesktop/systemd1", | |
2987 | "org.freedesktop.systemd1.Manager", | |
6e646d22 LP |
2988 | method, |
2989 | error, | |
2990 | &reply, | |
2991 | "ss", name, mode); | |
f459b602 | 2992 | if (r < 0) { |
39602c39 TA |
2993 | const char *verb; |
2994 | ||
033f0ab8 LP |
2995 | /* There's always a fallback possible for legacy actions. */ |
2996 | if (arg_action != ACTION_SYSTEMCTL) | |
2997 | return r; | |
67f3c402 | 2998 | |
39602c39 TA |
2999 | verb = method_to_verb(method); |
3000 | ||
ee87525c FB |
3001 | log_error("Failed to %s %s: %s", verb, name, bus_error_message(error, r)); |
3002 | ||
3003 | if (!sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT) && | |
5b464a18 ZJS |
3004 | !sd_bus_error_has_name(error, BUS_ERROR_UNIT_MASKED) && |
3005 | !sd_bus_error_has_name(error, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE)) | |
a6405ca2 | 3006 | log_error("See %s logs and 'systemctl%s status%s %s' for details.", |
0b8505b7 RC |
3007 | arg_scope == UNIT_FILE_SYSTEM ? "system" : "user", |
3008 | arg_scope == UNIT_FILE_SYSTEM ? "" : " --user", | |
a6405ca2 | 3009 | name[0] == '-' ? " --" : "", |
0b8505b7 | 3010 | name); |
ee87525c FB |
3011 | |
3012 | return r; | |
7e4249b9 LP |
3013 | } |
3014 | ||
f459b602 MAP |
3015 | r = sd_bus_message_read(reply, "o", &path); |
3016 | if (r < 0) | |
3017 | return bus_log_parse_error(r); | |
45fb0699 | 3018 | |
e3e0314b | 3019 | if (need_daemon_reload(bus, name) > 0) |
3f36991e | 3020 | warn_unit_file_changed(name); |
45fb0699 | 3021 | |
ebd011d9 LP |
3022 | if (w) { |
3023 | log_debug("Adding %s to the set", path); | |
3024 | r = bus_wait_for_jobs_add(w, path); | |
cbc9fbd1 LP |
3025 | if (r < 0) |
3026 | return log_oom(); | |
e4b61340 | 3027 | } |
7e4249b9 | 3028 | |
46eddbb5 | 3029 | return 0; |
7e4249b9 LP |
3030 | } |
3031 | ||
e3e0314b | 3032 | static int expand_names(sd_bus *bus, char **names, const char* suffix, char ***ret) { |
e3e0314b ZJS |
3033 | _cleanup_strv_free_ char **mangled = NULL, **globs = NULL; |
3034 | char **name; | |
7410616c | 3035 | int r, i; |
e3e0314b | 3036 | |
4fbd7192 LP |
3037 | assert(bus); |
3038 | assert(ret); | |
3039 | ||
e3e0314b ZJS |
3040 | STRV_FOREACH(name, names) { |
3041 | char *t; | |
37cbc1d5 | 3042 | UnitNameMangle options = UNIT_NAME_MANGLE_GLOB | (arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN); |
e3e0314b | 3043 | |
e80733be | 3044 | if (suffix) |
37cbc1d5 | 3045 | r = unit_name_mangle_with_suffix(*name, options, suffix, &t); |
e80733be | 3046 | else |
37cbc1d5 | 3047 | r = unit_name_mangle(*name, options, &t); |
7410616c LP |
3048 | if (r < 0) |
3049 | return log_error_errno(r, "Failed to mangle name: %m"); | |
e3e0314b ZJS |
3050 | |
3051 | if (string_is_glob(t)) | |
6e18964d | 3052 | r = strv_consume(&globs, t); |
e3e0314b | 3053 | else |
6e18964d ZJS |
3054 | r = strv_consume(&mangled, t); |
3055 | if (r < 0) | |
e3e0314b | 3056 | return log_oom(); |
e3e0314b ZJS |
3057 | } |
3058 | ||
3059 | /* Query the manager only if any of the names are a glob, since | |
3060 | * this is fairly expensive */ | |
3061 | if (!strv_isempty(globs)) { | |
4afd3348 | 3062 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
e3e0314b | 3063 | _cleanup_free_ UnitInfo *unit_infos = NULL; |
1b53f64b | 3064 | size_t allocated, n; |
e3e0314b | 3065 | |
1238ee09 | 3066 | r = get_unit_list(bus, NULL, globs, &unit_infos, 0, &reply); |
e3e0314b ZJS |
3067 | if (r < 0) |
3068 | return r; | |
3069 | ||
1b53f64b LP |
3070 | n = strv_length(mangled); |
3071 | allocated = n + 1; | |
3072 | ||
3073 | for (i = 0; i < r; i++) { | |
3074 | if (!GREEDY_REALLOC(mangled, allocated, n+2)) | |
3075 | return log_oom(); | |
3076 | ||
3077 | mangled[n] = strdup(unit_infos[i].id); | |
3078 | if (!mangled[n]) | |
e3e0314b | 3079 | return log_oom(); |
1b53f64b LP |
3080 | |
3081 | mangled[++n] = NULL; | |
3082 | } | |
e3e0314b ZJS |
3083 | } |
3084 | ||
1cc6c93a | 3085 | *ret = TAKE_PTR(mangled); |
1238ee09 | 3086 | |
e3e0314b ZJS |
3087 | return 0; |
3088 | } | |
3089 | ||
47a0eaa6 MS |
3090 | static const struct { |
3091 | const char *target; | |
3092 | const char *verb; | |
3093 | const char *mode; | |
3094 | } action_table[_ACTION_MAX] = { | |
e68c79db ML |
3095 | [ACTION_HALT] = { SPECIAL_HALT_TARGET, "halt", "replace-irreversibly" }, |
3096 | [ACTION_POWEROFF] = { SPECIAL_POWEROFF_TARGET, "poweroff", "replace-irreversibly" }, | |
3097 | [ACTION_REBOOT] = { SPECIAL_REBOOT_TARGET, "reboot", "replace-irreversibly" }, | |
3098 | [ACTION_KEXEC] = { SPECIAL_KEXEC_TARGET, "kexec", "replace-irreversibly" }, | |
3099 | [ACTION_RUNLEVEL2] = { SPECIAL_MULTI_USER_TARGET, NULL, "isolate" }, | |
3100 | [ACTION_RUNLEVEL3] = { SPECIAL_MULTI_USER_TARGET, NULL, "isolate" }, | |
3101 | [ACTION_RUNLEVEL4] = { SPECIAL_MULTI_USER_TARGET, NULL, "isolate" }, | |
3102 | [ACTION_RUNLEVEL5] = { SPECIAL_GRAPHICAL_TARGET, NULL, "isolate" }, | |
3103 | [ACTION_RESCUE] = { SPECIAL_RESCUE_TARGET, "rescue", "isolate" }, | |
3104 | [ACTION_EMERGENCY] = { SPECIAL_EMERGENCY_TARGET, "emergency", "isolate" }, | |
3105 | [ACTION_DEFAULT] = { SPECIAL_DEFAULT_TARGET, "default", "isolate" }, | |
3106 | [ACTION_EXIT] = { SPECIAL_EXIT_TARGET, "exit", "replace-irreversibly" }, | |
3107 | [ACTION_SUSPEND] = { SPECIAL_SUSPEND_TARGET, "suspend", "replace-irreversibly" }, | |
3108 | [ACTION_HIBERNATE] = { SPECIAL_HIBERNATE_TARGET, "hibernate", "replace-irreversibly" }, | |
3109 | [ACTION_HYBRID_SLEEP] = { SPECIAL_HYBRID_SLEEP_TARGET, "hybrid-sleep", "replace-irreversibly" }, | |
3110 | [ACTION_SUSPEND_THEN_HIBERNATE] = { SPECIAL_SUSPEND_THEN_HIBERNATE_TARGET, "suspend-then-hibernate", "replace-irreversibly" }, | |
47a0eaa6 MS |
3111 | }; |
3112 | ||
514f4ef5 | 3113 | static enum action verb_to_action(const char *verb) { |
47a0eaa6 MS |
3114 | enum action i; |
3115 | ||
913c1916 | 3116 | for (i = 0; i < _ACTION_MAX; i++) |
f459b602 | 3117 | if (streq_ptr(action_table[i].verb, verb)) |
47a0eaa6 | 3118 | return i; |
514f4ef5 | 3119 | |
f459b602 MAP |
3120 | return _ACTION_INVALID; |
3121 | } | |
e4b61340 | 3122 | |
e449de87 | 3123 | static int start_unit(int argc, char *argv[], void *userdata) { |
ebd011d9 | 3124 | _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL; |
0e8d9c0c | 3125 | _cleanup_(wait_context_free) WaitContext wait_context = {}; |
08073121 | 3126 | const char *method, *mode, *one_name, *suffix = NULL; |
416b8801 | 3127 | _cleanup_free_ char **stopped_units = NULL; /* Do not use _cleanup_strv_free_ */ |
ebd011d9 | 3128 | _cleanup_strv_free_ char **names = NULL; |
0e8d9c0c | 3129 | int r, ret = EXIT_SUCCESS; |
4fbd7192 | 3130 | sd_bus *bus; |
729e3769 | 3131 | char **name; |
e4b61340 | 3132 | |
f886603b AJ |
3133 | if (arg_wait && !STR_IN_SET(argv[0], "start", "restart")) { |
3134 | log_error("--wait may only be used with the 'start' or 'restart' commands."); | |
93a08841 MP |
3135 | return -EINVAL; |
3136 | } | |
3137 | ||
3138 | /* we cannot do sender tracking on the private bus, so we need the full | |
3139 | * one for RefUnit to implement --wait */ | |
3140 | r = acquire_bus(arg_wait ? BUS_FULL : BUS_MANAGER, &bus); | |
4fbd7192 LP |
3141 | if (r < 0) |
3142 | return r; | |
3143 | ||
d2ad7e1b | 3144 | ask_password_agent_open_if_enabled(); |
8a4b13c5 | 3145 | polkit_agent_open_maybe(); |
d2ad7e1b | 3146 | |
e4b61340 | 3147 | if (arg_action == ACTION_SYSTEMCTL) { |
47a0eaa6 | 3148 | enum action action; |
e4b61340 | 3149 | |
e449de87 LP |
3150 | action = verb_to_action(argv[0]); |
3151 | ||
913c1916 AJ |
3152 | if (action != _ACTION_INVALID) { |
3153 | method = "StartUnit"; | |
3154 | mode = action_table[action].mode; | |
3155 | one_name = action_table[action].target; | |
3156 | } else { | |
3157 | if (streq(argv[0], "isolate")) { | |
3158 | method = "StartUnit"; | |
3159 | mode = "isolate"; | |
3160 | ||
3161 | suffix = ".target"; | |
3162 | } else { | |
3163 | method = verb_to_method(argv[0]); | |
3164 | mode = arg_job_mode; | |
3165 | } | |
3166 | one_name = NULL; | |
3167 | } | |
e4b61340 | 3168 | } else { |
78ca9099 | 3169 | assert(arg_action >= 0 && arg_action < _ACTION_MAX); |
47a0eaa6 | 3170 | assert(action_table[arg_action].target); |
913c1916 | 3171 | assert(action_table[arg_action].mode); |
e4b61340 LP |
3172 | |
3173 | method = "StartUnit"; | |
47a0eaa6 | 3174 | mode = action_table[arg_action].mode; |
e3e0314b | 3175 | one_name = action_table[arg_action].target; |
514f4ef5 LP |
3176 | } |
3177 | ||
8b7ec7bb | 3178 | if (one_name) { |
bea1a013 | 3179 | names = strv_new(one_name); |
8b7ec7bb LP |
3180 | if (!names) |
3181 | return log_oom(); | |
3182 | } else { | |
e449de87 | 3183 | r = expand_names(bus, strv_skip(argv, 1), suffix, &names); |
e3e0314b | 3184 | if (r < 0) |
691395d8 | 3185 | return log_error_errno(r, "Failed to expand names: %m"); |
e3e0314b | 3186 | } |
b6520546 | 3187 | |
6e905d93 | 3188 | if (!arg_no_block) { |
ebd011d9 | 3189 | r = bus_wait_for_jobs_new(bus, &w); |
f647962d MS |
3190 | if (r < 0) |
3191 | return log_error_errno(r, "Could not watch jobs: %m"); | |
e4b61340 LP |
3192 | } |
3193 | ||
93a08841 | 3194 | if (arg_wait) { |
31b2cd5d | 3195 | r = sd_bus_call_method_async( |
93a08841 | 3196 | bus, |
31b2cd5d | 3197 | NULL, |
93a08841 MP |
3198 | "org.freedesktop.systemd1", |
3199 | "/org/freedesktop/systemd1", | |
3200 | "org.freedesktop.systemd1.Manager", | |
3201 | "Subscribe", | |
31b2cd5d LP |
3202 | NULL, NULL, |
3203 | NULL); | |
93a08841 | 3204 | if (r < 0) |
31b2cd5d | 3205 | return log_error_errno(r, "Failed to enable subscription: %m"); |
93a08841 MP |
3206 | r = sd_event_default(&wait_context.event); |
3207 | if (r < 0) | |
3208 | return log_error_errno(r, "Failed to allocate event loop: %m"); | |
3209 | r = sd_bus_attach_event(bus, wait_context.event, 0); | |
3210 | if (r < 0) | |
3211 | return log_error_errno(r, "Failed to attach bus to event loop: %m"); | |
3212 | } | |
3213 | ||
b6520546 | 3214 | STRV_FOREACH(name, names) { |
4afd3348 | 3215 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
f459b602 | 3216 | |
0e8d9c0c LP |
3217 | r = start_unit_one(bus, method, *name, mode, &error, w, arg_wait ? &wait_context : NULL); |
3218 | if (ret == EXIT_SUCCESS && r < 0) | |
3219 | ret = translate_bus_error_to_exit_status(r, &error); | |
416b8801 YW |
3220 | |
3221 | if (r >= 0 && streq(method, "StopUnit")) { | |
3222 | r = strv_push(&stopped_units, *name); | |
3223 | if (r < 0) | |
3224 | return log_oom(); | |
3225 | } | |
e4b61340 LP |
3226 | } |
3227 | ||
67f3c402 | 3228 | if (!arg_no_block) { |
c11bda1e | 3229 | const char* extra_args[4] = {}; |
0e8d9c0c | 3230 | int arg_count = 0; |
4524439e | 3231 | |
4524439e ZJS |
3232 | if (arg_scope != UNIT_FILE_SYSTEM) |
3233 | extra_args[arg_count++] = "--user"; | |
3234 | ||
3235 | assert(IN_SET(arg_transport, BUS_TRANSPORT_LOCAL, BUS_TRANSPORT_REMOTE, BUS_TRANSPORT_MACHINE)); | |
3236 | if (arg_transport == BUS_TRANSPORT_REMOTE) { | |
3237 | extra_args[arg_count++] = "-H"; | |
3238 | extra_args[arg_count++] = arg_host; | |
3239 | } else if (arg_transport == BUS_TRANSPORT_MACHINE) { | |
3240 | extra_args[arg_count++] = "-M"; | |
3241 | extra_args[arg_count++] = arg_host; | |
3242 | } | |
f459b602 | 3243 | |
0e8d9c0c LP |
3244 | r = bus_wait_for_jobs(w, arg_quiet, extra_args); |
3245 | if (r < 0) | |
3246 | return r; | |
49111a70 ZJS |
3247 | |
3248 | /* When stopping units, warn if they can still be triggered by | |
3249 | * another active unit (socket, path, timer) */ | |
416b8801 YW |
3250 | if (!arg_quiet) |
3251 | STRV_FOREACH(name, stopped_units) | |
86f004fb | 3252 | (void) check_triggering_units(bus, *name); |
67f3c402 | 3253 | } |
514f4ef5 | 3254 | |
0e8d9c0c LP |
3255 | if (ret == EXIT_SUCCESS && arg_wait && !set_isempty(wait_context.unit_paths)) { |
3256 | r = sd_event_loop(wait_context.event); | |
3257 | if (r < 0) | |
3258 | return log_error_errno(r, "Failed to run event loop: %m"); | |
93a08841 | 3259 | if (wait_context.any_failed) |
0e8d9c0c | 3260 | ret = EXIT_FAILURE; |
93a08841 MP |
3261 | } |
3262 | ||
0e8d9c0c | 3263 | return ret; |
e4b61340 LP |
3264 | } |
3265 | ||
349cc4a5 | 3266 | #if ENABLE_LOGIND |
4fbd7192 | 3267 | static int logind_set_wall_message(void) { |
4afd3348 | 3268 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
4fbd7192 | 3269 | sd_bus *bus; |
f2d11d35 LP |
3270 | _cleanup_free_ char *m = NULL; |
3271 | int r; | |
3272 | ||
4fbd7192 LP |
3273 | r = acquire_bus(BUS_FULL, &bus); |
3274 | if (r < 0) | |
3275 | return r; | |
f2d11d35 LP |
3276 | |
3277 | m = strv_join(arg_wall, " "); | |
3278 | if (!m) | |
3279 | return log_oom(); | |
3280 | ||
dbc9830c ZJS |
3281 | log_debug("%s wall message \"%s\".", arg_dry_run ? "Would set" : "Setting", m); |
3282 | if (arg_dry_run) | |
3283 | return 0; | |
3284 | ||
f2d11d35 LP |
3285 | r = sd_bus_call_method( |
3286 | bus, | |
3287 | "org.freedesktop.login1", | |
3288 | "/org/freedesktop/login1", | |
3289 | "org.freedesktop.login1.Manager", | |
3290 | "SetWallMessage", | |
3291 | &error, | |
3292 | NULL, | |
3293 | "sb", | |
3294 | m, | |
3295 | !arg_no_wall); | |
3296 | ||
3297 | if (r < 0) | |
3298 | return log_warning_errno(r, "Failed to set wall message, ignoring: %s", bus_error_message(&error, r)); | |
f2d11d35 LP |
3299 | return 0; |
3300 | } | |
79f1c8f6 | 3301 | #endif |
f2d11d35 | 3302 | |
7e59bfcb | 3303 | /* Ask systemd-logind, which might grant access to unprivileged users |
d35f51ea | 3304 | * through polkit */ |
4fbd7192 | 3305 | static int logind_reboot(enum action a) { |
349cc4a5 | 3306 | #if ENABLE_LOGIND |
4afd3348 | 3307 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
58158dc7 | 3308 | const char *method, *description; |
4fbd7192 | 3309 | sd_bus *bus; |
f459b602 | 3310 | int r; |
4c80c73c | 3311 | |
4fbd7192 LP |
3312 | r = acquire_bus(BUS_FULL, &bus); |
3313 | if (r < 0) | |
3314 | return r; | |
f2d11d35 | 3315 | |
4c80c73c KS |
3316 | switch (a) { |
3317 | ||
36b69c31 LP |
3318 | case ACTION_POWEROFF: |
3319 | method = "PowerOff"; | |
3320 | description = "power off system"; | |
3321 | break; | |
3322 | ||
4c80c73c KS |
3323 | case ACTION_REBOOT: |
3324 | method = "Reboot"; | |
58158dc7 | 3325 | description = "reboot system"; |
4c80c73c KS |
3326 | break; |
3327 | ||
36b69c31 LP |
3328 | case ACTION_HALT: |
3329 | method = "Halt"; | |
3330 | description = "halt system"; | |
4c80c73c KS |
3331 | break; |
3332 | ||
d889a206 LP |
3333 | case ACTION_SUSPEND: |
3334 | method = "Suspend"; | |
58158dc7 | 3335 | description = "suspend system"; |
d889a206 LP |
3336 | break; |
3337 | ||
3338 | case ACTION_HIBERNATE: | |
3339 | method = "Hibernate"; | |
58158dc7 | 3340 | description = "hibernate system"; |
d889a206 LP |
3341 | break; |
3342 | ||
6524990f LP |
3343 | case ACTION_HYBRID_SLEEP: |
3344 | method = "HybridSleep"; | |
58158dc7 | 3345 | description = "put system into hybrid sleep"; |
6524990f LP |
3346 | break; |
3347 | ||
e68c79db ML |
3348 | case ACTION_SUSPEND_THEN_HIBERNATE: |
3349 | method = "SuspendThenHibernate"; | |
c58493c0 ML |
3350 | description = "put system into suspend followed by hibernate"; |
3351 | break; | |
3352 | ||
4c80c73c KS |
3353 | default: |
3354 | return -EINVAL; | |
3355 | } | |
3356 | ||
8a4b13c5 | 3357 | polkit_agent_open_maybe(); |
d2ad7e1b ZJS |
3358 | (void) logind_set_wall_message(); |
3359 | ||
dbc9830c ZJS |
3360 | log_debug("%s org.freedesktop.login1.Manager %s dbus call.", arg_dry_run ? "Would execute" : "Executing", method); |
3361 | if (arg_dry_run) | |
3362 | return 0; | |
3363 | ||
f459b602 | 3364 | r = sd_bus_call_method( |
f22f08cd SP |
3365 | bus, |
3366 | "org.freedesktop.login1", | |
3367 | "/org/freedesktop/login1", | |
3368 | "org.freedesktop.login1.Manager", | |
3369 | method, | |
f459b602 | 3370 | &error, |
f22f08cd | 3371 | NULL, |
342641fb | 3372 | "b", arg_ask_password); |
f459b602 | 3373 | if (r < 0) |
691395d8 | 3374 | return log_error_errno(r, "Failed to %s via logind: %s", description, bus_error_message(&error, r)); |
f459b602 | 3375 | |
691395d8 | 3376 | return 0; |
4c80c73c KS |
3377 | #else |
3378 | return -ENOSYS; | |
3379 | #endif | |
3380 | } | |
3381 | ||
4fbd7192 | 3382 | static int logind_check_inhibitors(enum action a) { |
349cc4a5 | 3383 | #if ENABLE_LOGIND |
4afd3348 | 3384 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
59164be4 | 3385 | _cleanup_strv_free_ char **sessions = NULL; |
f459b602 MAP |
3386 | const char *what, *who, *why, *mode; |
3387 | uint32_t uid, pid; | |
4fbd7192 | 3388 | sd_bus *bus; |
f459b602 | 3389 | unsigned c = 0; |
59164be4 | 3390 | char **s; |
f459b602 | 3391 | int r; |
b37844d3 | 3392 | |
748ebafa LP |
3393 | if (arg_ignore_inhibitors || arg_force > 0) |
3394 | return 0; | |
3395 | ||
3396 | if (arg_when > 0) | |
3397 | return 0; | |
3398 | ||
3399 | if (geteuid() == 0) | |
b37844d3 LP |
3400 | return 0; |
3401 | ||
3402 | if (!on_tty()) | |
3403 | return 0; | |
e08ab379 LP |
3404 | |
3405 | if (arg_transport != BUS_TRANSPORT_LOCAL) | |
3406 | return 0; | |
b37844d3 | 3407 | |
4fbd7192 LP |
3408 | r = acquire_bus(BUS_FULL, &bus); |
3409 | if (r < 0) | |
3410 | return r; | |
3411 | ||
f459b602 | 3412 | r = sd_bus_call_method( |
b37844d3 LP |
3413 | bus, |
3414 | "org.freedesktop.login1", | |
3415 | "/org/freedesktop/login1", | |
3416 | "org.freedesktop.login1.Manager", | |
3417 | "ListInhibitors", | |
b37844d3 | 3418 | NULL, |
f459b602 MAP |
3419 | &reply, |
3420 | NULL); | |
b37844d3 LP |
3421 | if (r < 0) |
3422 | /* If logind is not around, then there are no inhibitors... */ | |
3423 | return 0; | |
3424 | ||
4aa2beac | 3425 | r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssuu)"); |
f459b602 MAP |
3426 | if (r < 0) |
3427 | return bus_log_parse_error(r); | |
b37844d3 | 3428 | |
4aa2beac | 3429 | while ((r = sd_bus_message_read(reply, "(ssssuu)", &what, &who, &why, &mode, &uid, &pid)) > 0) { |
59164be4 | 3430 | _cleanup_free_ char *comm = NULL, *user = NULL; |
f459b602 | 3431 | _cleanup_strv_free_ char **sv = NULL; |
b37844d3 LP |
3432 | |
3433 | if (!streq(mode, "block")) | |
f459b602 | 3434 | continue; |
b37844d3 LP |
3435 | |
3436 | sv = strv_split(what, ":"); | |
3437 | if (!sv) | |
3438 | return log_oom(); | |
3439 | ||
ee043777 | 3440 | if (!pid_is_valid((pid_t) pid)) { |
72b3f82e | 3441 | log_error("Invalid PID "PID_FMT".", (pid_t) pid); |
ee043777 LP |
3442 | return -ERANGE; |
3443 | } | |
d028e018 | 3444 | |
b37844d3 | 3445 | if (!strv_contains(sv, |
4c315c2c IS |
3446 | IN_SET(a, |
3447 | ACTION_HALT, | |
3448 | ACTION_POWEROFF, | |
3449 | ACTION_REBOOT, | |
3450 | ACTION_KEXEC) ? "shutdown" : "sleep")) | |
f459b602 | 3451 | continue; |
b37844d3 LP |
3452 | |
3453 | get_process_comm(pid, &comm); | |
59164be4 | 3454 | user = uid_to_name(uid); |
f459b602 | 3455 | |
de0671ee | 3456 | log_warning("Operation inhibited by \"%s\" (PID "PID_FMT" \"%s\", user %s), reason is \"%s\".", |
d028e018 | 3457 | who, (pid_t) pid, strna(comm), strna(user), why); |
b37844d3 | 3458 | |
f459b602 | 3459 | c++; |
b37844d3 | 3460 | } |
f459b602 MAP |
3461 | if (r < 0) |
3462 | return bus_log_parse_error(r); | |
b37844d3 | 3463 | |
f459b602 MAP |
3464 | r = sd_bus_message_exit_container(reply); |
3465 | if (r < 0) | |
3466 | return bus_log_parse_error(r); | |
b37844d3 | 3467 | |
59164be4 LP |
3468 | /* Check for current sessions */ |
3469 | sd_get_sessions(&sessions); | |
3470 | STRV_FOREACH(s, sessions) { | |
59164be4 LP |
3471 | _cleanup_free_ char *type = NULL, *tty = NULL, *seat = NULL, *user = NULL, *service = NULL, *class = NULL; |
3472 | ||
3473 | if (sd_session_get_uid(*s, &uid) < 0 || uid == getuid()) | |
3474 | continue; | |
3475 | ||
3476 | if (sd_session_get_class(*s, &class) < 0 || !streq(class, "user")) | |
3477 | continue; | |
3478 | ||
28abce5d | 3479 | if (sd_session_get_type(*s, &type) < 0 || !STR_IN_SET(type, "x11", "wayland", "tty", "mir")) |
59164be4 LP |
3480 | continue; |
3481 | ||
3482 | sd_session_get_tty(*s, &tty); | |
3483 | sd_session_get_seat(*s, &seat); | |
3484 | sd_session_get_service(*s, &service); | |
3485 | user = uid_to_name(uid); | |
3486 | ||
3487 | log_warning("User %s is logged in on %s.", strna(user), isempty(tty) ? (isempty(seat) ? strna(service) : seat) : tty); | |
3488 | c++; | |
3489 | } | |
3490 | ||
b37844d3 LP |
3491 | if (c <= 0) |
3492 | return 0; | |
3493 | ||
59164be4 | 3494 | log_error("Please retry operation after closing inhibitors and logging out other users.\nAlternatively, ignore inhibitors and users with 'systemctl %s -i'.", |
47a0eaa6 | 3495 | action_table[a].verb); |
b37844d3 LP |
3496 | |
3497 | return -EPERM; | |
3498 | #else | |
3499 | return 0; | |
3500 | #endif | |
3501 | } | |
3502 | ||
97af80c5 LP |
3503 | static int prepare_firmware_setup(void) { |
3504 | ||
3505 | if (!arg_firmware_setup) | |
3506 | return 0; | |
3507 | ||
349cc4a5 | 3508 | #if ENABLE_LOGIND |
4afd3348 | 3509 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
4fbd7192 | 3510 | sd_bus *bus; |
a4921cf4 JJ |
3511 | int r; |
3512 | ||
4fbd7192 LP |
3513 | r = acquire_bus(BUS_FULL, &bus); |
3514 | if (r < 0) | |
3515 | return r; | |
a4921cf4 | 3516 | |
a4921cf4 JJ |
3517 | r = sd_bus_call_method( |
3518 | bus, | |
3519 | "org.freedesktop.login1", | |
3520 | "/org/freedesktop/login1", | |
3521 | "org.freedesktop.login1.Manager", | |
3522 | "SetRebootToFirmwareSetup", | |
3523 | &error, | |
3524 | NULL, | |
3525 | "b", true); | |
4fbd7192 LP |
3526 | if (r < 0) |
3527 | return log_error_errno(r, "Cannot indicate to EFI to boot into setup mode: %s", bus_error_message(&error, r)); | |
a4921cf4 JJ |
3528 | |
3529 | return 0; | |
3530 | #else | |
97af80c5 | 3531 | log_error("Booting into firmware setup not supported."); |
4fbd7192 | 3532 | return -ENOSYS; |
a4921cf4 JJ |
3533 | #endif |
3534 | } | |
3535 | ||
97af80c5 LP |
3536 | static int prepare_boot_loader_menu(void) { |
3537 | ||
3538 | if (arg_boot_loader_menu == USEC_INFINITY) | |
3539 | return 0; | |
3540 | ||
3541 | #if ENABLE_LOGIND | |
3542 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
3543 | sd_bus *bus; | |
4fbd7192 LP |
3544 | int r; |
3545 | ||
97af80c5 LP |
3546 | r = acquire_bus(BUS_FULL, &bus); |
3547 | if (r < 0) | |
3548 | return r; | |
3549 | ||
3550 | r = sd_bus_call_method( | |
3551 | bus, | |
3552 | "org.freedesktop.login1", | |
3553 | "/org/freedesktop/login1", | |
3554 | "org.freedesktop.login1.Manager", | |
3555 | "SetRebootToBootLoaderMenu", | |
3556 | &error, | |
3557 | NULL, | |
3558 | "t", arg_boot_loader_menu); | |
3559 | if (r < 0) | |
3560 | return log_error_errno(r, "Cannot indicate to boot loader to enter boot loader entry menu: %s", bus_error_message(&error, r)); | |
3561 | ||
3562 | return 0; | |
3563 | #else | |
3564 | log_error("Booting into boot loader menu not supported."); | |
3565 | return -ENOSYS; | |
3566 | #endif | |
3567 | } | |
3568 | ||
3569 | static int prepare_boot_loader_entry(void) { | |
3570 | ||
3571 | if (!arg_boot_loader_entry) | |
4fbd7192 LP |
3572 | return 0; |
3573 | ||
97af80c5 LP |
3574 | #if ENABLE_LOGIND |
3575 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
3576 | sd_bus *bus; | |
3577 | int r; | |
4fbd7192 | 3578 | |
97af80c5 LP |
3579 | r = acquire_bus(BUS_FULL, &bus); |
3580 | if (r < 0) | |
3581 | return r; | |
3582 | ||
3583 | r = sd_bus_call_method( | |
3584 | bus, | |
3585 | "org.freedesktop.login1", | |
3586 | "/org/freedesktop/login1", | |
3587 | "org.freedesktop.login1.Manager", | |
3588 | "SetRebootToBootLoaderEntry", | |
3589 | &error, | |
3590 | NULL, | |
3591 | "s", arg_boot_loader_entry); | |
3592 | if (r < 0) | |
3593 | return log_error_errno(r, "Cannot set boot into loader entry '%s': %s", arg_boot_loader_entry, bus_error_message(&error, r)); | |
4fbd7192 | 3594 | |
97af80c5 LP |
3595 | return 0; |
3596 | #else | |
3597 | log_error("Booting into boot loader entry not supported."); | |
3598 | return -ENOSYS; | |
3599 | #endif | |
4fbd7192 LP |
3600 | } |
3601 | ||
4bb2e9d4 ZJS |
3602 | static int load_kexec_kernel(void) { |
3603 | _cleanup_(boot_config_free) BootConfig config = {}; | |
43b736a8 | 3604 | _cleanup_free_ char *kernel = NULL, *initrd = NULL, *options = NULL; |
4bb2e9d4 ZJS |
3605 | const BootEntry *e; |
3606 | pid_t pid; | |
3607 | int r; | |
3608 | ||
3609 | if (kexec_loaded()) { | |
3610 | log_debug("Kexec kernel already loaded."); | |
3611 | return 0; | |
3612 | } | |
3613 | ||
c0d73214 ZJS |
3614 | if (access(KEXEC, X_OK) < 0) |
3615 | return log_error_errno(errno, KEXEC" is not available: %m"); | |
3616 | ||
38bd74d6 | 3617 | r = boot_entries_load_config_auto(NULL, NULL, &config); |
cc5957dc LP |
3618 | if (r == -ENOKEY) /* The call doesn't log about ENOKEY, let's do so here. */ |
3619 | return log_error_errno(r, "Cannot find the ESP partition mount point."); | |
4bb2e9d4 | 3620 | if (r < 0) |
1b20d889 | 3621 | return r; |
4bb2e9d4 | 3622 | |
38bd74d6 LP |
3623 | e = boot_config_default_entry(&config); |
3624 | if (!e) | |
3625 | return log_error_errno(SYNTHETIC_ERRNO(ENOENT), | |
3626 | "No boot loader entry suitable as default, refusing to guess."); | |
3627 | ||
3628 | log_debug("Found default boot loader entry in file \"%s\"", e->path); | |
3629 | ||
78c38fe7 LP |
3630 | if (!e->kernel) |
3631 | return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), | |
3632 | "Boot entry does not refer to Linux kernel, which is not supported currently."); | |
8a1ef3bf | 3633 | if (strv_length(e->initrd) > 1) |
0f88d829 | 3634 | return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), |
8a1ef3bf | 3635 | "Boot entry specifies multiple initrds, which is not supported currently."); |
4bb2e9d4 | 3636 | |
43b736a8 | 3637 | kernel = path_join(e->root, e->kernel); |
9fc32cd9 LP |
3638 | if (!kernel) |
3639 | return log_oom(); | |
3640 | ||
3641 | if (!strv_isempty(e->initrd)) { | |
0f88d829 | 3642 | initrd = path_join(e->root, e->initrd[0]); |
9fc32cd9 LP |
3643 | if (!initrd) |
3644 | return log_oom(); | |
3645 | } | |
3646 | ||
4bb2e9d4 ZJS |
3647 | options = strv_join(e->options, " "); |
3648 | if (!options) | |
3649 | return log_oom(); | |
3650 | ||
cd086a01 ZJS |
3651 | log_full(arg_quiet ? LOG_DEBUG : LOG_INFO, |
3652 | "%s "KEXEC" --load \"%s\" --append \"%s\"%s%s%s", | |
3653 | arg_dry_run ? "Would run" : "Running", | |
3654 | kernel, | |
3655 | options, | |
3656 | initrd ? " --initrd \"" : NULL, strempty(initrd), initrd ? "\"" : ""); | |
4bb2e9d4 ZJS |
3657 | if (arg_dry_run) |
3658 | return 0; | |
3659 | ||
c943e95f | 3660 | r = safe_fork("(kexec)", FORK_WAIT|FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG, &pid); |
4c253ed1 | 3661 | if (r < 0) |
b6e1fff1 | 3662 | return r; |
4c253ed1 | 3663 | if (r == 0) { |
4bb2e9d4 ZJS |
3664 | const char* const args[] = { |
3665 | KEXEC, | |
3666 | "--load", kernel, | |
3667 | "--append", options, | |
3668 | initrd ? "--initrd" : NULL, initrd, | |
c943e95f LP |
3669 | NULL |
3670 | }; | |
4bb2e9d4 ZJS |
3671 | |
3672 | /* Child */ | |
4bb2e9d4 ZJS |
3673 | execv(args[0], (char * const *) args); |
3674 | _exit(EXIT_FAILURE); | |
4c253ed1 LP |
3675 | } |
3676 | ||
df685d57 | 3677 | return 0; |
4bb2e9d4 ZJS |
3678 | } |
3679 | ||
57ab9006 | 3680 | static int set_exit_code(uint8_t code) { |
4afd3348 | 3681 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
57ab9006 LP |
3682 | sd_bus *bus; |
3683 | int r; | |
3684 | ||
3685 | r = acquire_bus(BUS_MANAGER, &bus); | |
3686 | if (r < 0) | |
3687 | return r; | |
3688 | ||
3689 | r = sd_bus_call_method( | |
3690 | bus, | |
3691 | "org.freedesktop.systemd1", | |
3692 | "/org/freedesktop/systemd1", | |
3693 | "org.freedesktop.systemd1.Manager", | |
3694 | "SetExitCode", | |
3695 | &error, | |
3696 | NULL, | |
3697 | "y", code); | |
3698 | if (r < 0) | |
af3d8113 | 3699 | return log_error_errno(r, "Failed to set exit code: %s", bus_error_message(&error, r)); |
57ab9006 LP |
3700 | |
3701 | return 0; | |
3702 | } | |
3703 | ||
e449de87 | 3704 | static int start_special(int argc, char *argv[], void *userdata) { |
4c80c73c | 3705 | enum action a; |
983d9c90 | 3706 | int r; |
2b0f4e6f ZJS |
3707 | bool termination_action; /* an action that terminates the manager, |
3708 | * can be performed also by signal. */ | |
983d9c90 | 3709 | |
e449de87 | 3710 | assert(argv); |
514f4ef5 | 3711 | |
e449de87 | 3712 | a = verb_to_action(argv[0]); |
4c80c73c | 3713 | |
4fbd7192 | 3714 | r = logind_check_inhibitors(a); |
748ebafa LP |
3715 | if (r < 0) |
3716 | return r; | |
3717 | ||
fba868fa LP |
3718 | if (arg_force >= 2) { |
3719 | r = must_be_root(); | |
3720 | if (r < 0) | |
3721 | return r; | |
c32b90de LP |
3722 | } |
3723 | ||
4fbd7192 | 3724 | r = prepare_firmware_setup(); |
a4921cf4 JJ |
3725 | if (r < 0) |
3726 | return r; | |
5bdf2243 | 3727 | |
97af80c5 LP |
3728 | r = prepare_boot_loader_menu(); |
3729 | if (r < 0) | |
3730 | return r; | |
3731 | ||
3732 | r = prepare_boot_loader_entry(); | |
3733 | if (r < 0) | |
3734 | return r; | |
3735 | ||
e449de87 | 3736 | if (a == ACTION_REBOOT && argc > 1) { |
27c06cb5 | 3737 | r = update_reboot_parameter_and_warn(argv[1]); |
b986229e SW |
3738 | if (r < 0) |
3739 | return r; | |
57ab9006 | 3740 | |
4bb2e9d4 ZJS |
3741 | } else if (a == ACTION_KEXEC) { |
3742 | r = load_kexec_kernel(); | |
d23b5ce2 ZJS |
3743 | if (r < 0 && arg_force >= 1) |
3744 | log_notice("Failed to load kexec kernel, continuing without."); | |
3745 | else if (r < 0) | |
4bb2e9d4 ZJS |
3746 | return r; |
3747 | ||
e449de87 | 3748 | } else if (a == ACTION_EXIT && argc > 1) { |
7bbb5359 | 3749 | uint8_t code; |
287419c1 | 3750 | |
57ab9006 LP |
3751 | /* If the exit code is not given on the command line, |
3752 | * don't reset it to zero: just keep it as it might | |
3753 | * have been set previously. */ | |
287419c1 | 3754 | |
e449de87 | 3755 | r = safe_atou8(argv[1], &code); |
4fbd7192 | 3756 | if (r < 0) |
57ab9006 | 3757 | return log_error_errno(r, "Invalid exit code."); |
4fbd7192 | 3758 | |
57ab9006 | 3759 | r = set_exit_code(code); |
691395d8 | 3760 | if (r < 0) |
57ab9006 | 3761 | return r; |
b986229e SW |
3762 | } |
3763 | ||
2b0f4e6f ZJS |
3764 | termination_action = IN_SET(a, |
3765 | ACTION_HALT, | |
3766 | ACTION_POWEROFF, | |
3767 | ACTION_REBOOT); | |
3768 | if (termination_action && arg_force >= 2) | |
477def80 | 3769 | return halt_now(a); |
e606bb61 | 3770 | |
7e59bfcb | 3771 | if (arg_force >= 1 && |
5955df2c | 3772 | (termination_action || IN_SET(a, ACTION_KEXEC, ACTION_EXIT))) |
2b0f4e6f ZJS |
3773 | r = trivial_method(argc, argv, userdata); |
3774 | else { | |
3775 | /* First try logind, to allow authentication with polkit */ | |
3776 | if (IN_SET(a, | |
3777 | ACTION_POWEROFF, | |
3778 | ACTION_REBOOT, | |
36b69c31 | 3779 | ACTION_HALT, |
2b0f4e6f ZJS |
3780 | ACTION_SUSPEND, |
3781 | ACTION_HIBERNATE, | |
c58493c0 | 3782 | ACTION_HYBRID_SLEEP, |
e68c79db | 3783 | ACTION_SUSPEND_THEN_HIBERNATE)) { |
2b0f4e6f ZJS |
3784 | |
3785 | r = logind_reboot(a); | |
3786 | if (r >= 0) | |
3787 | return r; | |
3788 | if (IN_SET(r, -EOPNOTSUPP, -EINPROGRESS)) | |
3789 | /* requested operation is not supported or already in progress */ | |
3790 | return r; | |
20b09ca7 | 3791 | |
130246d2 LP |
3792 | /* On all other errors, try low-level operation. In order to minimize the difference between |
3793 | * operation with and without logind, we explicitly enable non-blocking mode for this, as | |
3794 | * logind's shutdown operations are always non-blocking. */ | |
3795 | ||
3796 | arg_no_block = true; | |
3797 | ||
3798 | } else if (IN_SET(a, ACTION_EXIT, ACTION_KEXEC)) | |
3799 | /* Since exit/kexec are so close in behaviour to power-off/reboot, let's also make them | |
3800 | * asynchronous, in order to not confuse the user needlessly with unexpected behaviour. */ | |
3801 | arg_no_block = true; | |
7089051f | 3802 | |
2b0f4e6f | 3803 | r = start_unit(argc, argv, userdata); |
4c80c73c | 3804 | } |
983d9c90 | 3805 | |
2b0f4e6f ZJS |
3806 | if (termination_action && arg_force < 2 && |
3807 | IN_SET(r, -ENOENT, -ETIMEDOUT)) | |
cc7de2ba | 3808 | log_notice("It is possible to perform action directly, see discussion of --force --force in man:systemctl(1)."); |
2b0f4e6f ZJS |
3809 | |
3810 | return r; | |
514f4ef5 LP |
3811 | } |
3812 | ||
988b3b17 ZJS |
3813 | static int start_system_special(int argc, char *argv[], void *userdata) { |
3814 | /* Like start_special above, but raises an error when running in user mode */ | |
3815 | ||
baaa35ad ZJS |
3816 | if (arg_scope != UNIT_FILE_SYSTEM) |
3817 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
3818 | "Bad action for %s mode.", | |
3819 | arg_scope == UNIT_FILE_GLOBAL ? "--global" : "--user"); | |
988b3b17 ZJS |
3820 | |
3821 | return start_special(argc, argv, userdata); | |
3822 | } | |
3823 | ||
fa0d5878 | 3824 | static int check_unit_generic(int code, const UnitActiveState good_states[], int nb_states, char **args) { |
e3e0314b | 3825 | _cleanup_strv_free_ char **names = NULL; |
fa0d5878 | 3826 | UnitActiveState active_state; |
4fbd7192 | 3827 | sd_bus *bus; |
729e3769 | 3828 | char **name; |
fa0d5878 | 3829 | int r, i; |
d60f6ad0 | 3830 | bool found = false; |
0183528f | 3831 | |
4fbd7192 LP |
3832 | r = acquire_bus(BUS_MANAGER, &bus); |
3833 | if (r < 0) | |
3834 | return r; | |
3835 | ||
e3e0314b | 3836 | r = expand_names(bus, args, NULL, &names); |
f647962d MS |
3837 | if (r < 0) |
3838 | return log_error_errno(r, "Failed to expand names: %m"); | |
e3e0314b ZJS |
3839 | |
3840 | STRV_FOREACH(name, names) { | |
fa0d5878 BR |
3841 | r = get_state_one_unit(bus, *name, &active_state); |
3842 | if (r < 0) | |
3843 | return r; | |
3844 | ||
3845 | if (!arg_quiet) | |
3846 | puts(unit_active_state_to_string(active_state)); | |
60f9ba0b | 3847 | |
fa0d5878 BR |
3848 | for (i = 0; i < nb_states; ++i) |
3849 | if (good_states[i] == active_state) | |
3850 | found = true; | |
1a0fce45 TA |
3851 | } |
3852 | ||
d60f6ad0 LN |
3853 | /* use the given return code for the case that we won't find |
3854 | * any unit which matches the list */ | |
3855 | return found ? 0 : code; | |
1a0fce45 TA |
3856 | } |
3857 | ||
e449de87 | 3858 | static int check_unit_active(int argc, char *argv[], void *userdata) { |
9897b09b LP |
3859 | static const UnitActiveState states[] = { |
3860 | UNIT_ACTIVE, | |
3861 | UNIT_RELOADING, | |
3862 | }; | |
3863 | ||
e3e0314b | 3864 | /* According to LSB: 3, "program is not running" */ |
b613907e | 3865 | return check_unit_generic(EXIT_PROGRAM_NOT_RUNNING, states, ELEMENTSOF(states), strv_skip(argv, 1)); |
e3e0314b | 3866 | } |
0183528f | 3867 | |
e449de87 | 3868 | static int check_unit_failed(int argc, char *argv[], void *userdata) { |
9897b09b LP |
3869 | static const UnitActiveState states[] = { |
3870 | UNIT_FAILED, | |
3871 | }; | |
3872 | ||
b613907e | 3873 | return check_unit_generic(EXIT_PROGRAM_DEAD_AND_PID_EXISTS, states, ELEMENTSOF(states), strv_skip(argv, 1)); |
48220598 LP |
3874 | } |
3875 | ||
e449de87 | 3876 | static int kill_unit(int argc, char *argv[], void *userdata) { |
e3e0314b | 3877 | _cleanup_strv_free_ char **names = NULL; |
ac5e3a50 | 3878 | char *kill_who = NULL, **name; |
4fbd7192 | 3879 | sd_bus *bus; |
e3e0314b | 3880 | int r, q; |
8a0867d6 | 3881 | |
4fbd7192 LP |
3882 | r = acquire_bus(BUS_MANAGER, &bus); |
3883 | if (r < 0) | |
3884 | return r; | |
3885 | ||
8a4b13c5 | 3886 | polkit_agent_open_maybe(); |
d2ad7e1b | 3887 | |
8a0867d6 LP |
3888 | if (!arg_kill_who) |
3889 | arg_kill_who = "all"; | |
3890 | ||
ac5e3a50 JS |
3891 | /* --fail was specified */ |
3892 | if (streq(arg_job_mode, "fail")) | |
81d62103 | 3893 | kill_who = strjoina(arg_kill_who, "-fail"); |
ac5e3a50 | 3894 | |
e449de87 | 3895 | r = expand_names(bus, strv_skip(argv, 1), NULL, &names); |
e3e0314b | 3896 | if (r < 0) |
691395d8 | 3897 | return log_error_errno(r, "Failed to expand names: %m"); |
60f9ba0b | 3898 | |
e3e0314b | 3899 | STRV_FOREACH(name, names) { |
4afd3348 | 3900 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
342641fb | 3901 | |
6e646d22 | 3902 | q = sd_bus_call_method( |
f22f08cd | 3903 | bus, |
b0193f1c LP |
3904 | "org.freedesktop.systemd1", |
3905 | "/org/freedesktop/systemd1", | |
3906 | "org.freedesktop.systemd1.Manager", | |
6e646d22 LP |
3907 | "KillUnit", |
3908 | &error, | |
3909 | NULL, | |
169d7fb6 | 3910 | "ssi", *name, kill_who ? kill_who : arg_kill_who, arg_signal); |
e3e0314b | 3911 | if (q < 0) { |
169d7fb6 | 3912 | log_error_errno(q, "Failed to kill unit %s: %s", *name, bus_error_message(&error, q)); |
e3e0314b ZJS |
3913 | if (r == 0) |
3914 | r = q; | |
f459b602 | 3915 | } |
8a0867d6 | 3916 | } |
f459b602 | 3917 | |
e3e0314b | 3918 | return r; |
8a0867d6 LP |
3919 | } |
3920 | ||
582a507f | 3921 | typedef struct ExecStatusInfo { |
0129173a LP |
3922 | char *name; |
3923 | ||
582a507f LP |
3924 | char *path; |
3925 | char **argv; | |
3926 | ||
b708e7ce LP |
3927 | bool ignore; |
3928 | ||
582a507f LP |
3929 | usec_t start_timestamp; |
3930 | usec_t exit_timestamp; | |
3931 | pid_t pid; | |
3932 | int code; | |
3933 | int status; | |
3934 | ||
3935 | LIST_FIELDS(struct ExecStatusInfo, exec); | |
3936 | } ExecStatusInfo; | |
3937 | ||
3938 | static void exec_status_info_free(ExecStatusInfo *i) { | |
3939 | assert(i); | |
3940 | ||
0129173a | 3941 | free(i->name); |
582a507f LP |
3942 | free(i->path); |
3943 | strv_free(i->argv); | |
3944 | free(i); | |
3945 | } | |
3946 | ||
f459b602 | 3947 | static int exec_status_info_deserialize(sd_bus_message *m, ExecStatusInfo *i) { |
b21a0ef8 | 3948 | uint64_t start_timestamp, exit_timestamp, start_timestamp_monotonic, exit_timestamp_monotonic; |
f459b602 | 3949 | const char *path; |
582a507f LP |
3950 | uint32_t pid; |
3951 | int32_t code, status; | |
f459b602 | 3952 | int ignore, r; |
582a507f | 3953 | |
f459b602 | 3954 | assert(m); |
582a507f | 3955 | assert(i); |
582a507f | 3956 | |
f459b602 MAP |
3957 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_STRUCT, "sasbttttuii"); |
3958 | if (r < 0) | |
3959 | return bus_log_parse_error(r); | |
3960 | else if (r == 0) | |
3961 | return 0; | |
582a507f | 3962 | |
f459b602 MAP |
3963 | r = sd_bus_message_read(m, "s", &path); |
3964 | if (r < 0) | |
3965 | return bus_log_parse_error(r); | |
582a507f | 3966 | |
f84190d8 LP |
3967 | i->path = strdup(path); |
3968 | if (!i->path) | |
f459b602 | 3969 | return log_oom(); |
582a507f | 3970 | |
f459b602 MAP |
3971 | r = sd_bus_message_read_strv(m, &i->argv); |
3972 | if (r < 0) | |
3973 | return bus_log_parse_error(r); | |
3974 | ||
3975 | r = sd_bus_message_read(m, | |
3976 | "bttttuii", | |
3977 | &ignore, | |
3978 | &start_timestamp, &start_timestamp_monotonic, | |
3979 | &exit_timestamp, &exit_timestamp_monotonic, | |
3980 | &pid, | |
3981 | &code, &status); | |
3982 | if (r < 0) | |
3983 | return bus_log_parse_error(r); | |
582a507f | 3984 | |
b708e7ce | 3985 | i->ignore = ignore; |
582a507f LP |
3986 | i->start_timestamp = (usec_t) start_timestamp; |
3987 | i->exit_timestamp = (usec_t) exit_timestamp; | |
3988 | i->pid = (pid_t) pid; | |
3989 | i->code = code; | |
3990 | i->status = status; | |
3991 | ||
f459b602 MAP |
3992 | r = sd_bus_message_exit_container(m); |
3993 | if (r < 0) | |
3994 | return bus_log_parse_error(r); | |
3995 | ||
3996 | return 1; | |
582a507f LP |
3997 | } |
3998 | ||
d5db7fe6 WC |
3999 | typedef struct UnitCondition { |
4000 | char *name; | |
b1ed76ae | 4001 | char *param; |
d5db7fe6 WC |
4002 | bool trigger; |
4003 | bool negate; | |
d5db7fe6 WC |
4004 | int tristate; |
4005 | ||
b1ed76ae | 4006 | LIST_FIELDS(struct UnitCondition, conditions); |
d5db7fe6 WC |
4007 | } UnitCondition; |
4008 | ||
4009 | static void unit_condition_free(UnitCondition *c) { | |
9bb71940 ZJS |
4010 | if (!c) |
4011 | return; | |
d5db7fe6 WC |
4012 | |
4013 | free(c->name); | |
4014 | free(c->param); | |
4015 | free(c); | |
4016 | } | |
4017 | ||
9bb71940 ZJS |
4018 | DEFINE_TRIVIAL_CLEANUP_FUNC(UnitCondition*, unit_condition_free); |
4019 | ||
61cbdc4b LP |
4020 | typedef struct UnitStatusInfo { |
4021 | const char *id; | |
4022 | const char *load_state; | |
4023 | const char *active_state; | |
4024 | const char *sub_state; | |
a4375746 | 4025 | const char *unit_file_state; |
d2dc52db | 4026 | const char *unit_file_preset; |
61cbdc4b LP |
4027 | |
4028 | const char *description; | |
4a9e2fff | 4029 | const char *following; |
61cbdc4b | 4030 | |
49dbfa7b LP |
4031 | char **documentation; |
4032 | ||
1b64d026 LP |
4033 | const char *fragment_path; |
4034 | const char *source_path; | |
4ad49000 | 4035 | const char *control_group; |
61cbdc4b | 4036 | |
76d14b87 OS |
4037 | char **dropin_paths; |
4038 | ||
9f39404c | 4039 | const char *load_error; |
f42806df | 4040 | const char *result; |
9f39404c | 4041 | |
584be568 | 4042 | usec_t inactive_exit_timestamp; |
df50185b | 4043 | usec_t inactive_exit_timestamp_monotonic; |
584be568 LP |
4044 | usec_t active_enter_timestamp; |
4045 | usec_t active_exit_timestamp; | |
4046 | usec_t inactive_enter_timestamp; | |
4047 | ||
45fb0699 | 4048 | bool need_daemon_reload; |
055ef36b | 4049 | bool transient; |
45fb0699 | 4050 | |
61cbdc4b LP |
4051 | /* Service */ |
4052 | pid_t main_pid; | |
4053 | pid_t control_pid; | |
4054 | const char *status_text; | |
175728c4 | 4055 | const char *pid_file; |
d06dacd0 | 4056 | bool running:1; |
b4af5a80 | 4057 | int status_errno; |
61cbdc4b LP |
4058 | |
4059 | usec_t start_timestamp; | |
4060 | usec_t exit_timestamp; | |
4061 | ||
4062 | int exit_code, exit_status; | |
4063 | ||
90bbc946 LP |
4064 | usec_t condition_timestamp; |
4065 | bool condition_result; | |
b1ed76ae | 4066 | LIST_HEAD(UnitCondition, conditions); |
59fccdc5 LP |
4067 | |
4068 | usec_t assert_timestamp; | |
4069 | bool assert_result; | |
4070 | bool failed_assert_trigger; | |
4071 | bool failed_assert_negate; | |
4072 | const char *failed_assert; | |
4073 | const char *failed_assert_parameter; | |
601b0842 GS |
4074 | usec_t next_elapse_real; |
4075 | usec_t next_elapse_monotonic; | |
90bbc946 | 4076 | |
61cbdc4b LP |
4077 | /* Socket */ |
4078 | unsigned n_accepted; | |
4079 | unsigned n_connections; | |
a98f7575 | 4080 | unsigned n_refused; |
b8131a87 | 4081 | bool accept; |
61cbdc4b | 4082 | |
13160134 | 4083 | /* Pairs of type, path */ |
67419600 OS |
4084 | char **listen; |
4085 | ||
61cbdc4b LP |
4086 | /* Device */ |
4087 | const char *sysfs_path; | |
4088 | ||
4089 | /* Mount, Automount */ | |
4090 | const char *where; | |
4091 | ||
4092 | /* Swap */ | |
4093 | const char *what; | |
582a507f | 4094 | |
934277fe LP |
4095 | /* CGroup */ |
4096 | uint64_t memory_current; | |
48422635 | 4097 | uint64_t memory_min; |
da4d897e TH |
4098 | uint64_t memory_low; |
4099 | uint64_t memory_high; | |
4100 | uint64_t memory_max; | |
96e131ea | 4101 | uint64_t memory_swap_max; |
934277fe | 4102 | uint64_t memory_limit; |
5ad096b3 | 4103 | uint64_t cpu_usage_nsec; |
03a7b521 LP |
4104 | uint64_t tasks_current; |
4105 | uint64_t tasks_max; | |
934277fe | 4106 | |
0e97c93f DM |
4107 | uint64_t ip_ingress_bytes; |
4108 | uint64_t ip_egress_bytes; | |
4109 | ||
582a507f | 4110 | LIST_HEAD(ExecStatusInfo, exec); |
61cbdc4b LP |
4111 | } UnitStatusInfo; |
4112 | ||
a7335518 ZJS |
4113 | static void unit_status_info_free(UnitStatusInfo *info) { |
4114 | ExecStatusInfo *p; | |
4115 | UnitCondition *c; | |
4116 | ||
4117 | strv_free(info->documentation); | |
4118 | strv_free(info->dropin_paths); | |
4119 | strv_free(info->listen); | |
4120 | ||
4121 | while ((c = info->conditions)) { | |
4122 | LIST_REMOVE(conditions, info->conditions, c); | |
4123 | unit_condition_free(c); | |
4124 | } | |
4125 | ||
4126 | while ((p = info->exec)) { | |
4127 | LIST_REMOVE(exec, info->exec, p); | |
4128 | exec_status_info_free(p); | |
4129 | } | |
4130 | } | |
4131 | ||
f459b602 | 4132 | static void print_status_info( |
291d565a | 4133 | sd_bus *bus, |
f459b602 MAP |
4134 | UnitStatusInfo *i, |
4135 | bool *ellipsized) { | |
4136 | ||
4d9685be ZJS |
4137 | char since1[FORMAT_TIMESTAMP_RELATIVE_MAX], since2[FORMAT_TIMESTAMP_MAX]; |
4138 | const char *s1, *s2, *active_on, *active_off, *on, *off, *ss; | |
c7080257 LP |
4139 | _cleanup_free_ char *formatted_path = NULL; |
4140 | ExecStatusInfo *p; | |
584be568 | 4141 | usec_t timestamp; |
1b64d026 | 4142 | const char *path; |
13160134 | 4143 | char **t, **t2; |
291d565a | 4144 | int r; |
582a507f | 4145 | |
61cbdc4b LP |
4146 | assert(i); |
4147 | ||
4148 | /* This shows pretty information about a unit. See | |
4149 | * print_property() for a low-level property printer */ | |
4150 | ||
b0d14c69 LP |
4151 | if (streq_ptr(i->active_state, "failed")) { |
4152 | active_on = ansi_highlight_red(); | |
1fc464f6 | 4153 | active_off = ansi_normal(); |
1cf03a4f | 4154 | } else if (STRPTR_IN_SET(i->active_state, "active", "reloading")) { |
b0d14c69 | 4155 | active_on = ansi_highlight_green(); |
1fc464f6 | 4156 | active_off = ansi_normal(); |
b0d14c69 LP |
4157 | } else |
4158 | active_on = active_off = ""; | |
4159 | ||
9a6f746f | 4160 | printf("%s%s%s %s", active_on, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE), active_off, strna(i->id)); |
61cbdc4b LP |
4161 | |
4162 | if (i->description && !streq_ptr(i->id, i->description)) | |
4163 | printf(" - %s", i->description); | |
4164 | ||
4165 | printf("\n"); | |
4166 | ||
4a9e2fff | 4167 | if (i->following) |
856323c9 | 4168 | printf(" Follow: unit currently follows state of %s\n", i->following); |
4a9e2fff | 4169 | |
c4555ad8 | 4170 | if (STRPTR_IN_SET(i->load_state, "error", "not-found", "bad-setting")) { |
0b5a519c | 4171 | on = ansi_highlight_red(); |
1fc464f6 | 4172 | off = ansi_normal(); |
c31b4423 LP |
4173 | } else |
4174 | on = off = ""; | |
4175 | ||
c7080257 LP |
4176 | path = i->source_path ?: i->fragment_path; |
4177 | if (path && terminal_urlify_path(path, NULL, &formatted_path) >= 0) | |
4178 | path = formatted_path; | |
1b64d026 | 4179 | |
9a0abfa8 | 4180 | if (!isempty(i->load_error)) |
856323c9 ZJS |
4181 | printf(" Loaded: %s%s%s (Reason: %s)\n", |
4182 | on, strna(i->load_state), off, i->load_error); | |
15ce1647 LP |
4183 | else if (path && !isempty(i->unit_file_state) && !isempty(i->unit_file_preset) && |
4184 | !STR_IN_SET(i->unit_file_state, "generated", "transient")) | |
d2dc52db LP |
4185 | printf(" Loaded: %s%s%s (%s; %s; vendor preset: %s)\n", |
4186 | on, strna(i->load_state), off, path, i->unit_file_state, i->unit_file_preset); | |
4187 | else if (path && !isempty(i->unit_file_state)) | |
856323c9 ZJS |
4188 | printf(" Loaded: %s%s%s (%s; %s)\n", |
4189 | on, strna(i->load_state), off, path, i->unit_file_state); | |
1b64d026 | 4190 | else if (path) |
856323c9 ZJS |
4191 | printf(" Loaded: %s%s%s (%s)\n", |
4192 | on, strna(i->load_state), off, path); | |
61cbdc4b | 4193 | else |
856323c9 ZJS |
4194 | printf(" Loaded: %s%s%s\n", |
4195 | on, strna(i->load_state), off); | |
61cbdc4b | 4196 | |
055ef36b LP |
4197 | if (i->transient) |
4198 | printf("Transient: yes\n"); | |
4199 | ||
76d14b87 | 4200 | if (!strv_isempty(i->dropin_paths)) { |
f459b602 | 4201 | _cleanup_free_ char *dir = NULL; |
76d14b87 | 4202 | bool last = false; |
f459b602 | 4203 | char ** dropin; |
76d14b87 OS |
4204 | |
4205 | STRV_FOREACH(dropin, i->dropin_paths) { | |
c7080257 LP |
4206 | _cleanup_free_ char *dropin_formatted = NULL; |
4207 | const char *df; | |
4208 | ||
eb2c3192 LP |
4209 | if (!dir || last) { |
4210 | printf(dir ? " " : | |
4211 | " Drop-In: "); | |
76d14b87 | 4212 | |
97b11eed | 4213 | dir = mfree(dir); |
76d14b87 | 4214 | |
5f311f8c LP |
4215 | dir = dirname_malloc(*dropin); |
4216 | if (!dir) { | |
76d14b87 OS |
4217 | log_oom(); |
4218 | return; | |
4219 | } | |
4220 | ||
eb2c3192 LP |
4221 | printf("%s\n" |
4222 | " %s", dir, | |
9a6f746f | 4223 | special_glyph(SPECIAL_GLYPH_TREE_RIGHT)); |
76d14b87 OS |
4224 | } |
4225 | ||
4226 | last = ! (*(dropin + 1) && startswith(*(dropin + 1), dir)); | |
4227 | ||
c7080257 LP |
4228 | if (terminal_urlify_path(*dropin, basename(*dropin), &dropin_formatted) >= 0) |
4229 | df = dropin_formatted; | |
4230 | else | |
4231 | df = *dropin; | |
4232 | ||
4233 | printf("%s%s", df, last ? "\n" : ", "); | |
76d14b87 | 4234 | } |
76d14b87 OS |
4235 | } |
4236 | ||
2ee68f72 | 4237 | ss = streq_ptr(i->active_state, i->sub_state) ? NULL : i->sub_state; |
2ee68f72 | 4238 | if (ss) |
856323c9 | 4239 | printf(" Active: %s%s (%s)%s", |
b0d14c69 | 4240 | active_on, strna(i->active_state), ss, active_off); |
2ee68f72 | 4241 | else |
856323c9 | 4242 | printf(" Active: %s%s%s", |
b0d14c69 | 4243 | active_on, strna(i->active_state), active_off); |
61cbdc4b | 4244 | |
f42806df LP |
4245 | if (!isempty(i->result) && !streq(i->result, "success")) |
4246 | printf(" (Result: %s)", i->result); | |
4247 | ||
1cf03a4f ZJS |
4248 | timestamp = STRPTR_IN_SET(i->active_state, "active", "reloading") ? i->active_enter_timestamp : |
4249 | STRPTR_IN_SET(i->active_state, "inactive", "failed") ? i->inactive_enter_timestamp : | |
4250 | STRPTR_IN_SET(i->active_state, "activating") ? i->inactive_exit_timestamp : | |
4251 | i->active_exit_timestamp; | |
584be568 | 4252 | |
bbb8486e | 4253 | s1 = format_timestamp_relative(since1, sizeof(since1), timestamp); |
584be568 LP |
4254 | s2 = format_timestamp(since2, sizeof(since2), timestamp); |
4255 | ||
4256 | if (s1) | |
538da63d | 4257 | printf(" since %s; %s\n", s2, s1); |
584be568 | 4258 | else if (s2) |
538da63d | 4259 | printf(" since %s\n", s2); |
584be568 LP |
4260 | else |
4261 | printf("\n"); | |
4262 | ||
601b0842 GS |
4263 | if (endswith(i->id, ".timer")) { |
4264 | char tstamp1[FORMAT_TIMESTAMP_RELATIVE_MAX], | |
4265 | tstamp2[FORMAT_TIMESTAMP_MAX]; | |
4d9685be | 4266 | const char *next_rel_time, *next_time; |
601b0842 GS |
4267 | dual_timestamp nw, next = {i->next_elapse_real, |
4268 | i->next_elapse_monotonic}; | |
4269 | usec_t next_elapse; | |
4270 | ||
4271 | printf(" Trigger: "); | |
4272 | ||
4273 | dual_timestamp_get(&nw); | |
4274 | next_elapse = calc_next_elapse(&nw, &next); | |
4d9685be ZJS |
4275 | next_rel_time = format_timestamp_relative(tstamp1, sizeof tstamp1, next_elapse); |
4276 | next_time = format_timestamp(tstamp2, sizeof tstamp2, next_elapse); | |
601b0842 GS |
4277 | |
4278 | if (next_time && next_rel_time) | |
4279 | printf("%s; %s\n", next_time, next_rel_time); | |
4280 | else | |
4281 | printf("n/a\n"); | |
4282 | } | |
4283 | ||
90bbc946 | 4284 | if (!i->condition_result && i->condition_timestamp > 0) { |
d5db7fe6 WC |
4285 | UnitCondition *c; |
4286 | int n = 0; | |
4287 | ||
bbb8486e | 4288 | s1 = format_timestamp_relative(since1, sizeof(since1), i->condition_timestamp); |
90bbc946 LP |
4289 | s2 = format_timestamp(since2, sizeof(since2), i->condition_timestamp); |
4290 | ||
59fccdc5 | 4291 | printf("Condition: start %scondition failed%s at %s%s%s\n", |
1fc464f6 | 4292 | ansi_highlight_yellow(), ansi_normal(), |
5cfee414 | 4293 | s2, s1 ? "; " : "", strempty(s1)); |
d5db7fe6 | 4294 | |
b1ed76ae | 4295 | LIST_FOREACH(conditions, c, i->conditions) |
d5db7fe6 WC |
4296 | if (c->tristate < 0) |
4297 | n++; | |
d5db7fe6 | 4298 | |
b1ed76ae ZJS |
4299 | LIST_FOREACH(conditions, c, i->conditions) |
4300 | if (c->tristate < 0) | |
4301 | printf(" %s %s=%s%s%s was not met\n", | |
9a6f746f | 4302 | --n ? special_glyph(SPECIAL_GLYPH_TREE_BRANCH) : special_glyph(SPECIAL_GLYPH_TREE_RIGHT), |
b1ed76ae ZJS |
4303 | c->name, |
4304 | c->trigger ? "|" : "", | |
4305 | c->negate ? "!" : "", | |
4306 | c->param); | |
59fccdc5 LP |
4307 | } |
4308 | ||
4309 | if (!i->assert_result && i->assert_timestamp > 0) { | |
4310 | s1 = format_timestamp_relative(since1, sizeof(since1), i->assert_timestamp); | |
4311 | s2 = format_timestamp(since2, sizeof(since2), i->assert_timestamp); | |
4312 | ||
4313 | printf(" Assert: start %sassertion failed%s at %s%s%s\n", | |
1fc464f6 | 4314 | ansi_highlight_red(), ansi_normal(), |
5cfee414 | 4315 | s2, s1 ? "; " : "", strempty(s1)); |
59fccdc5 LP |
4316 | if (i->failed_assert_trigger) |
4317 | printf(" none of the trigger assertions were met\n"); | |
4318 | else if (i->failed_assert) | |
4319 | printf(" %s=%s%s was not met\n", | |
4320 | i->failed_assert, | |
4321 | i->failed_assert_negate ? "!" : "", | |
4322 | i->failed_assert_parameter); | |
90bbc946 LP |
4323 | } |
4324 | ||
61cbdc4b | 4325 | if (i->sysfs_path) |
856323c9 | 4326 | printf(" Device: %s\n", i->sysfs_path); |
9feeba4b | 4327 | if (i->where) |
856323c9 | 4328 | printf(" Where: %s\n", i->where); |
9feeba4b | 4329 | if (i->what) |
856323c9 | 4330 | printf(" What: %s\n", i->what); |
49dbfa7b | 4331 | |
78c7d20e LP |
4332 | STRV_FOREACH(t, i->documentation) { |
4333 | _cleanup_free_ char *formatted = NULL; | |
4334 | const char *q; | |
4335 | ||
4336 | if (terminal_urlify(*t, NULL, &formatted) >= 0) | |
4337 | q = formatted; | |
4338 | else | |
4339 | q = *t; | |
4340 | ||
4341 | printf(" %*s %s\n", 9, t == i->documentation ? "Docs:" : "", q); | |
4342 | } | |
49dbfa7b | 4343 | |
13160134 | 4344 | STRV_FOREACH_PAIR(t, t2, i->listen) |
856323c9 | 4345 | printf(" %*s %s (%s)\n", 9, t == i->listen ? "Listen:" : "", *t2, *t); |
67419600 | 4346 | |
a98f7575 MM |
4347 | if (i->accept) { |
4348 | printf(" Accepted: %u; Connected: %u;", i->n_accepted, i->n_connections); | |
4349 | if (i->n_refused) | |
4350 | printf(" Refused: %u", i->n_refused); | |
4351 | printf("\n"); | |
4352 | } | |
4353 | ||
582a507f | 4354 | LIST_FOREACH(exec, p, i->exec) { |
13160134 | 4355 | _cleanup_free_ char *argv = NULL; |
9a57c629 | 4356 | bool good; |
582a507f LP |
4357 | |
4358 | /* Only show exited processes here */ | |
4359 | if (p->code == 0) | |
4360 | continue; | |
4361 | ||
13160134 | 4362 | argv = strv_join(p->argv, " "); |
1fa2f38f | 4363 | printf(" Process: "PID_FMT" %s=%s ", p->pid, p->name, strna(argv)); |
582a507f | 4364 | |
1f0958f6 | 4365 | good = is_clean_exit(p->code, p->status, EXIT_CLEAN_DAEMON, NULL); |
9a57c629 | 4366 | if (!good) { |
0b5a519c | 4367 | on = ansi_highlight_red(); |
1fc464f6 | 4368 | off = ansi_normal(); |
9a57c629 LP |
4369 | } else |
4370 | on = off = ""; | |
4371 | ||
4372 | printf("%s(code=%s, ", on, sigchld_code_to_string(p->code)); | |
4373 | ||
d06dacd0 LP |
4374 | if (p->code == CLD_EXITED) { |
4375 | const char *c; | |
4376 | ||
582a507f | 4377 | printf("status=%i", p->status); |
d06dacd0 | 4378 | |
1b64d026 LP |
4379 | c = exit_status_to_string(p->status, EXIT_STATUS_SYSTEMD); |
4380 | if (c) | |
d06dacd0 LP |
4381 | printf("/%s", c); |
4382 | ||
4383 | } else | |
582a507f | 4384 | printf("signal=%s", signal_to_string(p->status)); |
9a57c629 LP |
4385 | |
4386 | printf(")%s\n", off); | |
4387 | ||
582a507f LP |
4388 | if (i->main_pid == p->pid && |
4389 | i->start_timestamp == p->start_timestamp && | |
4390 | i->exit_timestamp == p->start_timestamp) | |
4391 | /* Let's not show this twice */ | |
4392 | i->main_pid = 0; | |
4393 | ||
4394 | if (p->pid == i->control_pid) | |
4395 | i->control_pid = 0; | |
4396 | } | |
4397 | ||
61cbdc4b | 4398 | if (i->main_pid > 0 || i->control_pid > 0) { |
61cbdc4b | 4399 | if (i->main_pid > 0) { |
8c06592f | 4400 | printf(" Main PID: "PID_FMT, i->main_pid); |
61cbdc4b LP |
4401 | |
4402 | if (i->running) { | |
a081b9ce LP |
4403 | |
4404 | if (arg_transport == BUS_TRANSPORT_LOCAL) { | |
4405 | _cleanup_free_ char *comm = NULL; | |
4406 | ||
4407 | (void) get_process_comm(i->main_pid, &comm); | |
4408 | if (comm) | |
4409 | printf(" (%s)", comm); | |
4410 | } | |
4411 | ||
6d4fc029 | 4412 | } else if (i->exit_code > 0) { |
61cbdc4b LP |
4413 | printf(" (code=%s, ", sigchld_code_to_string(i->exit_code)); |
4414 | ||
d06dacd0 LP |
4415 | if (i->exit_code == CLD_EXITED) { |
4416 | const char *c; | |
4417 | ||
61cbdc4b | 4418 | printf("status=%i", i->exit_status); |
d06dacd0 | 4419 | |
1b64d026 LP |
4420 | c = exit_status_to_string(i->exit_status, EXIT_STATUS_SYSTEMD); |
4421 | if (c) | |
d06dacd0 LP |
4422 | printf("/%s", c); |
4423 | ||
4424 | } else | |
582a507f | 4425 | printf("signal=%s", signal_to_string(i->exit_status)); |
6d4fc029 LP |
4426 | printf(")"); |
4427 | } | |
13160134 | 4428 | } |
61cbdc4b LP |
4429 | |
4430 | if (i->control_pid > 0) { | |
13160134 | 4431 | _cleanup_free_ char *c = NULL; |
61cbdc4b | 4432 | |
1089dcd4 LP |
4433 | if (i->main_pid > 0) |
4434 | fputs("; Control PID: ", stdout); | |
4435 | else | |
4436 | fputs("Cntrl PID: ", stdout); /* if first in column, abbreviated so it fits alignment */ | |
4437 | ||
4438 | printf(PID_FMT, i->control_pid); | |
61cbdc4b | 4439 | |
a081b9ce LP |
4440 | if (arg_transport == BUS_TRANSPORT_LOCAL) { |
4441 | (void) get_process_comm(i->control_pid, &c); | |
4442 | if (c) | |
4443 | printf(" (%s)", c); | |
4444 | } | |
61cbdc4b LP |
4445 | } |
4446 | ||
4447 | printf("\n"); | |
4448 | } | |
4449 | ||
17bb7382 | 4450 | if (i->status_text) |
856323c9 | 4451 | printf(" Status: \"%s\"\n", i->status_text); |
b4af5a80 LP |
4452 | if (i->status_errno > 0) |
4453 | printf(" Error: %i (%s)\n", i->status_errno, strerror(i->status_errno)); | |
17bb7382 | 4454 | |
0e97c93f DM |
4455 | if (i->ip_ingress_bytes != (uint64_t) -1 && i->ip_egress_bytes != (uint64_t) -1) { |
4456 | char buf_in[FORMAT_BYTES_MAX], buf_out[FORMAT_BYTES_MAX]; | |
4457 | ||
4458 | printf(" IP: %s in, %s out\n", | |
4459 | format_bytes(buf_in, sizeof(buf_in), i->ip_ingress_bytes), | |
4460 | format_bytes(buf_out, sizeof(buf_out), i->ip_egress_bytes)); | |
4461 | } | |
4462 | ||
03a7b521 LP |
4463 | if (i->tasks_current != (uint64_t) -1) { |
4464 | printf(" Tasks: %" PRIu64, i->tasks_current); | |
4465 | ||
4466 | if (i->tasks_max != (uint64_t) -1) | |
2a6736dd | 4467 | printf(" (limit: %" PRIu64 ")\n", i->tasks_max); |
03a7b521 LP |
4468 | else |
4469 | printf("\n"); | |
4470 | } | |
4471 | ||
934277fe LP |
4472 | if (i->memory_current != (uint64_t) -1) { |
4473 | char buf[FORMAT_BYTES_MAX]; | |
4474 | ||
4475 | printf(" Memory: %s", format_bytes(buf, sizeof(buf), i->memory_current)); | |
4476 | ||
48422635 TH |
4477 | if (i->memory_min > 0 || i->memory_low > 0 || |
4478 | i->memory_high != CGROUP_LIMIT_MAX || i->memory_max != CGROUP_LIMIT_MAX || | |
4479 | i->memory_swap_max != CGROUP_LIMIT_MAX || | |
da4d897e TH |
4480 | i->memory_limit != CGROUP_LIMIT_MAX) { |
4481 | const char *prefix = ""; | |
4482 | ||
4483 | printf(" ("); | |
48422635 TH |
4484 | if (i->memory_min > 0) { |
4485 | printf("%smin: %s", prefix, format_bytes(buf, sizeof(buf), i->memory_min)); | |
4486 | prefix = " "; | |
4487 | } | |
da4d897e TH |
4488 | if (i->memory_low > 0) { |
4489 | printf("%slow: %s", prefix, format_bytes(buf, sizeof(buf), i->memory_low)); | |
4490 | prefix = " "; | |
4491 | } | |
4492 | if (i->memory_high != CGROUP_LIMIT_MAX) { | |
4493 | printf("%shigh: %s", prefix, format_bytes(buf, sizeof(buf), i->memory_high)); | |
4494 | prefix = " "; | |
4495 | } | |
4496 | if (i->memory_max != CGROUP_LIMIT_MAX) { | |
4497 | printf("%smax: %s", prefix, format_bytes(buf, sizeof(buf), i->memory_max)); | |
4498 | prefix = " "; | |
4499 | } | |
96e131ea WC |
4500 | if (i->memory_swap_max != CGROUP_LIMIT_MAX) { |
4501 | printf("%sswap max: %s", prefix, format_bytes(buf, sizeof(buf), i->memory_swap_max)); | |
4502 | prefix = " "; | |
4503 | } | |
da4d897e TH |
4504 | if (i->memory_limit != CGROUP_LIMIT_MAX) { |
4505 | printf("%slimit: %s", prefix, format_bytes(buf, sizeof(buf), i->memory_limit)); | |
4506 | prefix = " "; | |
4507 | } | |
4508 | printf(")"); | |
4509 | } | |
4510 | printf("\n"); | |
934277fe LP |
4511 | } |
4512 | ||
5ad096b3 LP |
4513 | if (i->cpu_usage_nsec != (uint64_t) -1) { |
4514 | char buf[FORMAT_TIMESPAN_MAX]; | |
4515 | printf(" CPU: %s\n", format_timespan(buf, sizeof(buf), i->cpu_usage_nsec / NSEC_PER_USEC, USEC_PER_MSEC)); | |
4516 | } | |
4517 | ||
51e22f88 | 4518 | if (i->control_group) { |
291d565a LP |
4519 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
4520 | static const char prefix[] = " "; | |
ab35fb1b LP |
4521 | unsigned c; |
4522 | ||
51e22f88 LP |
4523 | printf(" CGroup: %s\n", i->control_group); |
4524 | ||
291d565a LP |
4525 | c = columns(); |
4526 | if (c > sizeof(prefix) - 1) | |
4527 | c -= sizeof(prefix) - 1; | |
4528 | else | |
4529 | c = 0; | |
ab35fb1b | 4530 | |
291d565a LP |
4531 | r = unit_show_processes(bus, i->id, i->control_group, prefix, c, get_output_flags(), &error); |
4532 | if (r == -EBADR) { | |
b69d29ce LP |
4533 | unsigned k = 0; |
4534 | pid_t extra[2]; | |
4535 | ||
291d565a | 4536 | /* Fallback for older systemd versions where the GetUnitProcesses() call is not yet available */ |
ab35fb1b | 4537 | |
b69d29ce LP |
4538 | if (i->main_pid > 0) |
4539 | extra[k++] = i->main_pid; | |
4540 | ||
4541 | if (i->control_pid > 0) | |
4542 | extra[k++] = i->control_pid; | |
4543 | ||
0ff308c8 | 4544 | show_cgroup_and_extra(SYSTEMD_CGROUP_CONTROLLER, i->control_group, prefix, c, extra, k, get_output_flags()); |
291d565a | 4545 | } else if (r < 0) |
950d6889 | 4546 | log_warning_errno(r, "Failed to dump process list for '%s', ignoring: %s", i->id, bus_error_message(&error, r)); |
c59760ee | 4547 | } |
45fb0699 | 4548 | |
ece174c5 | 4549 | if (i->id && arg_transport == BUS_TRANSPORT_LOCAL) |
3c756001 LP |
4550 | show_journal_by_unit( |
4551 | stdout, | |
4552 | i->id, | |
4553 | arg_output, | |
4554 | 0, | |
4555 | i->inactive_exit_timestamp_monotonic, | |
4556 | arg_lines, | |
4557 | getuid(), | |
4558 | get_output_flags() | OUTPUT_BEGIN_NEWLINE, | |
4559 | SD_JOURNAL_LOCAL_ONLY, | |
4560 | arg_scope == UNIT_FILE_SYSTEM, | |
4561 | ellipsized); | |
86aa7ba4 | 4562 | |
45fb0699 | 4563 | if (i->need_daemon_reload) |
3f36991e | 4564 | warn_unit_file_changed(i->id); |
61cbdc4b LP |
4565 | } |
4566 | ||
b43f208f | 4567 | static void show_unit_help(UnitStatusInfo *i) { |
256425cc LP |
4568 | char **p; |
4569 | ||
4570 | assert(i); | |
4571 | ||
4572 | if (!i->documentation) { | |
4573 | log_info("Documentation for %s not known.", i->id); | |
4574 | return; | |
4575 | } | |
4576 | ||
78002a67 ZJS |
4577 | STRV_FOREACH(p, i->documentation) |
4578 | if (startswith(*p, "man:")) | |
4579 | show_man_page(*p + 4, false); | |
4580 | else | |
0315fe37 | 4581 | log_info("Can't show: %s", *p); |
256425cc LP |
4582 | } |
4583 | ||
4679a8c3 YW |
4584 | static int map_main_pid(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { |
4585 | UnitStatusInfo *i = userdata; | |
4586 | uint32_t u; | |
f459b602 | 4587 | int r; |
61cbdc4b | 4588 | |
4679a8c3 YW |
4589 | r = sd_bus_message_read(m, "u", &u); |
4590 | if (r < 0) | |
4591 | return r; | |
61cbdc4b | 4592 | |
4679a8c3 YW |
4593 | i->main_pid = (pid_t) u; |
4594 | i->running = u > 0; | |
b8131a87 | 4595 | |
4679a8c3 YW |
4596 | return 0; |
4597 | } | |
b8131a87 | 4598 | |
4679a8c3 | 4599 | static int map_load_error(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { |
31d99bd1 | 4600 | const char *message, **p = userdata; |
4679a8c3 | 4601 | int r; |
b8131a87 | 4602 | |
31d99bd1 | 4603 | r = sd_bus_message_read(m, "(ss)", NULL, &message); |
4679a8c3 YW |
4604 | if (r < 0) |
4605 | return r; | |
b8131a87 | 4606 | |
4679a8c3 YW |
4607 | if (!isempty(message)) |
4608 | *p = message; | |
61cbdc4b | 4609 | |
4679a8c3 YW |
4610 | return 0; |
4611 | } | |
61cbdc4b | 4612 | |
4679a8c3 YW |
4613 | static int map_listen(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { |
4614 | const char *type, *path; | |
4615 | char ***p = userdata; | |
4616 | int r; | |
61cbdc4b | 4617 | |
4679a8c3 YW |
4618 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(ss)"); |
4619 | if (r < 0) | |
4620 | return r; | |
61cbdc4b | 4621 | |
4679a8c3 | 4622 | while ((r = sd_bus_message_read(m, "(ss)", &type, &path)) > 0) { |
61cbdc4b | 4623 | |
4679a8c3 | 4624 | r = strv_extend(p, type); |
f459b602 | 4625 | if (r < 0) |
4679a8c3 | 4626 | return r; |
61cbdc4b | 4627 | |
4679a8c3 | 4628 | r = strv_extend(p, path); |
f459b602 | 4629 | if (r < 0) |
4679a8c3 | 4630 | return r; |
61cbdc4b | 4631 | } |
4679a8c3 YW |
4632 | if (r < 0) |
4633 | return r; | |
582a507f | 4634 | |
4679a8c3 YW |
4635 | r = sd_bus_message_exit_container(m); |
4636 | if (r < 0) | |
4637 | return r; | |
49dbfa7b | 4638 | |
4679a8c3 YW |
4639 | return 0; |
4640 | } | |
52990c2e | 4641 | |
4679a8c3 YW |
4642 | static int map_conditions(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { |
4643 | UnitStatusInfo *i = userdata; | |
4644 | const char *cond, *param; | |
4645 | int trigger, negate; | |
4646 | int32_t state; | |
4647 | int r; | |
52990c2e | 4648 | |
4679a8c3 YW |
4649 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(sbbsi)"); |
4650 | if (r < 0) | |
4651 | return r; | |
f459b602 | 4652 | |
4679a8c3 YW |
4653 | while ((r = sd_bus_message_read(m, "(sbbsi)", &cond, &trigger, &negate, ¶m, &state)) > 0) { |
4654 | _cleanup_(unit_condition_freep) UnitCondition *c = NULL; | |
d5db7fe6 | 4655 | |
4679a8c3 YW |
4656 | c = new0(UnitCondition, 1); |
4657 | if (!c) | |
4658 | return -ENOMEM; | |
d5db7fe6 | 4659 | |
4679a8c3 YW |
4660 | c->name = strdup(cond); |
4661 | c->param = strdup(param); | |
4662 | if (!c->name || !c->param) | |
4663 | return -ENOMEM; | |
d5db7fe6 | 4664 | |
4679a8c3 YW |
4665 | c->trigger = trigger; |
4666 | c->negate = negate; | |
4667 | c->tristate = state; | |
d5db7fe6 | 4668 | |
4679a8c3 YW |
4669 | LIST_PREPEND(conditions, i->conditions, c); |
4670 | c = NULL; | |
4671 | } | |
4672 | if (r < 0) | |
4673 | return r; | |
d5db7fe6 | 4674 | |
4679a8c3 YW |
4675 | r = sd_bus_message_exit_container(m); |
4676 | if (r < 0) | |
4677 | return r; | |
59fccdc5 | 4678 | |
4679a8c3 YW |
4679 | return 0; |
4680 | } | |
59fccdc5 | 4681 | |
4679a8c3 YW |
4682 | static int map_asserts(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { |
4683 | UnitStatusInfo *i = userdata; | |
4684 | const char *cond, *param; | |
4685 | int trigger, negate; | |
4686 | int32_t state; | |
4687 | int r; | |
59fccdc5 | 4688 | |
4679a8c3 YW |
4689 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(sbbsi)"); |
4690 | if (r < 0) | |
4691 | return r; | |
59fccdc5 | 4692 | |
4679a8c3 YW |
4693 | while ((r = sd_bus_message_read(m, "(sbbsi)", &cond, &trigger, &negate, ¶m, &state)) > 0) { |
4694 | if (state < 0 && (!trigger || !i->failed_assert)) { | |
4695 | i->failed_assert = cond; | |
4696 | i->failed_assert_trigger = trigger; | |
4697 | i->failed_assert_negate = negate; | |
4698 | i->failed_assert_parameter = param; | |
4699 | } | |
4700 | } | |
4701 | if (r < 0) | |
4702 | return r; | |
f459b602 | 4703 | |
4679a8c3 YW |
4704 | r = sd_bus_message_exit_container(m); |
4705 | if (r < 0) | |
4706 | return r; | |
f459b602 | 4707 | |
4679a8c3 YW |
4708 | return 0; |
4709 | } | |
582a507f | 4710 | |
4679a8c3 YW |
4711 | static int map_exec(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { |
4712 | _cleanup_free_ ExecStatusInfo *info = NULL; | |
3e14d36a | 4713 | ExecStatusInfo *last; |
4679a8c3 YW |
4714 | UnitStatusInfo *i = userdata; |
4715 | int r; | |
9f39404c | 4716 | |
4679a8c3 YW |
4717 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(sasbttttuii)"); |
4718 | if (r < 0) | |
4719 | return r; | |
9f39404c | 4720 | |
4679a8c3 YW |
4721 | info = new0(ExecStatusInfo, 1); |
4722 | if (!info) | |
4723 | return -ENOMEM; | |
9f39404c | 4724 | |
3e14d36a LP |
4725 | LIST_FIND_TAIL(exec, i->exec, last); |
4726 | ||
4679a8c3 | 4727 | while ((r = exec_status_info_deserialize(m, info)) > 0) { |
9f39404c | 4728 | |
4679a8c3 YW |
4729 | info->name = strdup(member); |
4730 | if (!info->name) | |
4731 | return -ENOMEM; | |
9f39404c | 4732 | |
3e14d36a LP |
4733 | LIST_INSERT_AFTER(exec, i->exec, last, info); |
4734 | last = info; | |
f459b602 | 4735 | |
4679a8c3 YW |
4736 | info = new0(ExecStatusInfo, 1); |
4737 | if (!info) | |
4738 | return -ENOMEM; | |
9f39404c | 4739 | } |
4679a8c3 YW |
4740 | if (r < 0) |
4741 | return r; | |
f459b602 | 4742 | |
4679a8c3 | 4743 | r = sd_bus_message_exit_container(m); |
f459b602 | 4744 | if (r < 0) |
4679a8c3 | 4745 | return r; |
61cbdc4b LP |
4746 | |
4747 | return 0; | |
4748 | } | |
4749 | ||
eda19357 | 4750 | static int print_property(const char *name, const char *expected_value, sd_bus_message *m, bool value, bool all) { |
07636114 YW |
4751 | char bus_type; |
4752 | const char *contents; | |
f459b602 MAP |
4753 | int r; |
4754 | ||
48220598 | 4755 | assert(name); |
f459b602 | 4756 | assert(m); |
48220598 | 4757 | |
61cbdc4b LP |
4758 | /* This is a low-level property printer, see |
4759 | * print_status_info() for the nicer output */ | |
4760 | ||
07636114 YW |
4761 | r = sd_bus_message_peek_type(m, &bus_type, &contents); |
4762 | if (r < 0) | |
852c1b4d | 4763 | return r; |
48220598 | 4764 | |
07636114 | 4765 | switch (bus_type) { |
48220598 | 4766 | |
a3ddd457 YW |
4767 | case SD_BUS_TYPE_INT32: |
4768 | if (endswith(name, "ActionExitStatus")) { | |
4769 | int32_t i; | |
4770 | ||
4771 | r = sd_bus_message_read_basic(m, bus_type, &i); | |
4772 | if (r < 0) | |
4773 | return r; | |
4774 | ||
4775 | if (i >= 0 && i <= 255) | |
4776 | bus_print_property_valuef(name, expected_value, value, "%"PRIi32, i); | |
4777 | else if (all) | |
4778 | bus_print_property_value(name, expected_value, value, "[not set]"); | |
4779 | ||
4780 | return 1; | |
4781 | } | |
4782 | break; | |
4783 | ||
07636114 | 4784 | case SD_BUS_TYPE_STRUCT: |
48220598 | 4785 | |
07636114 | 4786 | if (contents[0] == SD_BUS_TYPE_UINT32 && streq(name, "Job")) { |
48220598 LP |
4787 | uint32_t u; |
4788 | ||
f459b602 MAP |
4789 | r = sd_bus_message_read(m, "(uo)", &u, NULL); |
4790 | if (r < 0) | |
4791 | return bus_log_parse_error(r); | |
48220598 | 4792 | |
f459b602 | 4793 | if (u > 0) |
102b0214 | 4794 | bus_print_property_valuef(name, expected_value, value, "%"PRIu32, u); |
07636114 | 4795 | else if (all) |
102b0214 | 4796 | bus_print_property_value(name, expected_value, value, ""); |
48220598 | 4797 | |
07636114 | 4798 | return 1; |
f459b602 | 4799 | |
07636114 | 4800 | } else if (contents[0] == SD_BUS_TYPE_STRING && streq(name, "Unit")) { |
48220598 LP |
4801 | const char *s; |
4802 | ||
f459b602 MAP |
4803 | r = sd_bus_message_read(m, "(so)", &s, NULL); |
4804 | if (r < 0) | |
4805 | return bus_log_parse_error(r); | |
48220598 | 4806 | |
07636114 | 4807 | if (all || !isempty(s)) |
102b0214 | 4808 | bus_print_property_value(name, expected_value, value, s); |
48220598 | 4809 | |
07636114 | 4810 | return 1; |
f459b602 | 4811 | |
07636114 | 4812 | } else if (contents[0] == SD_BUS_TYPE_STRING && streq(name, "LoadError")) { |
9f39404c LP |
4813 | const char *a = NULL, *b = NULL; |
4814 | ||
f459b602 MAP |
4815 | r = sd_bus_message_read(m, "(ss)", &a, &b); |
4816 | if (r < 0) | |
4817 | return bus_log_parse_error(r); | |
9f39404c | 4818 | |
48d3358d | 4819 | if (!isempty(a) || !isempty(b)) |
102b0214 | 4820 | bus_print_property_valuef(name, expected_value, value, "%s \"%s\"", strempty(a), strempty(b)); |
48d3358d YW |
4821 | else if (all) |
4822 | bus_print_property_value(name, expected_value, value, ""); | |
f786e80d | 4823 | |
07636114 | 4824 | return 1; |
04749e40 YW |
4825 | |
4826 | } else if (STR_IN_SET(name, "SystemCallFilter", "RestrictAddressFamilies")) { | |
57183d11 LP |
4827 | _cleanup_strv_free_ char **l = NULL; |
4828 | int whitelist; | |
4829 | ||
4830 | r = sd_bus_message_enter_container(m, 'r', "bas"); | |
4831 | if (r < 0) | |
4832 | return bus_log_parse_error(r); | |
4833 | ||
4834 | r = sd_bus_message_read(m, "b", &whitelist); | |
4835 | if (r < 0) | |
4836 | return bus_log_parse_error(r); | |
4837 | ||
4838 | r = sd_bus_message_read_strv(m, &l); | |
4839 | if (r < 0) | |
4840 | return bus_log_parse_error(r); | |
4841 | ||
4842 | r = sd_bus_message_exit_container(m); | |
4843 | if (r < 0) | |
4844 | return bus_log_parse_error(r); | |
4845 | ||
07636114 | 4846 | if (all || whitelist || !strv_isempty(l)) { |
57183d11 LP |
4847 | bool first = true; |
4848 | char **i; | |
4849 | ||
07636114 | 4850 | if (!value) { |
4f9a9105 ZJS |
4851 | fputs(name, stdout); |
4852 | fputc('=', stdout); | |
4853 | } | |
57183d11 LP |
4854 | |
4855 | if (!whitelist) | |
4856 | fputc('~', stdout); | |
4857 | ||
4858 | STRV_FOREACH(i, l) { | |
4859 | if (first) | |
4860 | first = false; | |
4861 | else | |
4862 | fputc(' ', stdout); | |
4863 | ||
4864 | fputs(*i, stdout); | |
4865 | } | |
4866 | fputc('\n', stdout); | |
4867 | } | |
4868 | ||
c7bb2fec | 4869 | return 1; |
102b0214 | 4870 | |
c7bb2fec IP |
4871 | } else if (STR_IN_SET(name, "SELinuxContext", "AppArmorProfile", "SmackProcessLabel")) { |
4872 | int ignore; | |
4873 | const char *s; | |
4874 | ||
4875 | r = sd_bus_message_read(m, "(bs)", &ignore, &s); | |
4876 | if (r < 0) | |
4877 | return bus_log_parse_error(r); | |
4878 | ||
4879 | if (!isempty(s)) | |
102b0214 | 4880 | bus_print_property_valuef(name, expected_value, value, "%s%s", ignore ? "-" : "", s); |
c7bb2fec | 4881 | else if (all) |
102b0214 | 4882 | bus_print_property_value(name, expected_value, value, ""); |
c7bb2fec | 4883 | |
07636114 | 4884 | return 1; |
4a220712 YW |
4885 | |
4886 | } else if (endswith(name, "ExitStatus") && streq(contents, "aiai")) { | |
4887 | const int32_t *status, *signal; | |
4888 | size_t sz_status, sz_signal, i; | |
4889 | ||
4890 | r = sd_bus_message_enter_container(m, 'r', "aiai"); | |
4891 | if (r < 0) | |
4892 | return bus_log_parse_error(r); | |
4893 | ||
4894 | r = sd_bus_message_read_array(m, 'i', (const void **) &status, &sz_status); | |
4895 | if (r < 0) | |
4896 | return bus_log_parse_error(r); | |
4897 | ||
4898 | r = sd_bus_message_read_array(m, 'i', (const void **) &signal, &sz_signal); | |
4899 | if (r < 0) | |
4900 | return bus_log_parse_error(r); | |
4901 | ||
4902 | r = sd_bus_message_exit_container(m); | |
4903 | if (r < 0) | |
4904 | return bus_log_parse_error(r); | |
4905 | ||
4906 | sz_status /= sizeof(int32_t); | |
4907 | sz_signal /= sizeof(int32_t); | |
4908 | ||
4909 | if (all || sz_status > 0 || sz_signal > 0) { | |
4910 | bool first = true; | |
4911 | ||
4912 | if (!value) { | |
4913 | fputs(name, stdout); | |
4914 | fputc('=', stdout); | |
4915 | } | |
4916 | ||
4917 | for (i = 0; i < sz_status; i++) { | |
4918 | if (status[i] < 0 || status[i] > 255) | |
4919 | continue; | |
4920 | ||
4921 | if (first) | |
4922 | first = false; | |
4923 | else | |
4924 | fputc(' ', stdout); | |
4925 | ||
4926 | printf("%"PRIi32, status[i]); | |
4927 | } | |
4928 | ||
4929 | for (i = 0; i < sz_signal; i++) { | |
4930 | const char *str; | |
4931 | ||
4932 | str = signal_to_string((int) signal[i]); | |
4933 | if (!str) | |
4934 | continue; | |
4935 | ||
4936 | if (first) | |
4937 | first = false; | |
4938 | else | |
4939 | fputc(' ', stdout); | |
4940 | ||
4941 | fputs(str, stdout); | |
4942 | } | |
4943 | ||
4944 | fputc('\n', stdout); | |
4945 | } | |
4946 | return 1; | |
48220598 LP |
4947 | } |
4948 | ||
4949 | break; | |
48220598 | 4950 | |
f459b602 | 4951 | case SD_BUS_TYPE_ARRAY: |
48220598 | 4952 | |
07636114 | 4953 | if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "EnvironmentFiles")) { |
f459b602 MAP |
4954 | const char *path; |
4955 | int ignore; | |
8c7be95e | 4956 | |
f459b602 MAP |
4957 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(sb)"); |
4958 | if (r < 0) | |
4959 | return bus_log_parse_error(r); | |
8c7be95e | 4960 | |
f459b602 | 4961 | while ((r = sd_bus_message_read(m, "(sb)", &path, &ignore)) > 0) |
102b0214 | 4962 | bus_print_property_valuef(name, expected_value, value, "%s (ignore_errors=%s)", path, yes_no(ignore)); |
8c7be95e | 4963 | |
f459b602 MAP |
4964 | if (r < 0) |
4965 | return bus_log_parse_error(r); | |
8c7be95e | 4966 | |
f459b602 MAP |
4967 | r = sd_bus_message_exit_container(m); |
4968 | if (r < 0) | |
4969 | return bus_log_parse_error(r); | |
8c7be95e | 4970 | |
07636114 | 4971 | return 1; |
8c7be95e | 4972 | |
07636114 | 4973 | } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "Paths")) { |
f459b602 | 4974 | const char *type, *path; |
67419600 | 4975 | |
f459b602 MAP |
4976 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(ss)"); |
4977 | if (r < 0) | |
4978 | return bus_log_parse_error(r); | |
ebf57b80 | 4979 | |
f459b602 | 4980 | while ((r = sd_bus_message_read(m, "(ss)", &type, &path)) > 0) |
102b0214 | 4981 | bus_print_property_valuef(name, expected_value, value, "%s (%s)", path, type); |
f459b602 MAP |
4982 | if (r < 0) |
4983 | return bus_log_parse_error(r); | |
ebf57b80 | 4984 | |
f459b602 MAP |
4985 | r = sd_bus_message_exit_container(m); |
4986 | if (r < 0) | |
4987 | return bus_log_parse_error(r); | |
ebf57b80 | 4988 | |
07636114 | 4989 | return 1; |
582a507f | 4990 | |
07636114 | 4991 | } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "Listen")) { |
f459b602 | 4992 | const char *type, *path; |
67419600 | 4993 | |
f459b602 MAP |
4994 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(ss)"); |
4995 | if (r < 0) | |
4996 | return bus_log_parse_error(r); | |
67419600 | 4997 | |
f459b602 | 4998 | while ((r = sd_bus_message_read(m, "(ss)", &type, &path)) > 0) |
102b0214 | 4999 | bus_print_property_valuef(name, expected_value, value, "%s (%s)", path, type); |
f459b602 MAP |
5000 | if (r < 0) |
5001 | return bus_log_parse_error(r); | |
67419600 | 5002 | |
f459b602 MAP |
5003 | r = sd_bus_message_exit_container(m); |
5004 | if (r < 0) | |
5005 | return bus_log_parse_error(r); | |
67419600 | 5006 | |
07636114 | 5007 | return 1; |
67419600 | 5008 | |
07636114 | 5009 | } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "TimersMonotonic")) { |
f459b602 | 5010 | const char *base; |
07636114 | 5011 | uint64_t v, next_elapse; |
707e5e52 | 5012 | |
f459b602 MAP |
5013 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(stt)"); |
5014 | if (r < 0) | |
5015 | return bus_log_parse_error(r); | |
552e4331 | 5016 | |
07636114 | 5017 | while ((r = sd_bus_message_read(m, "(stt)", &base, &v, &next_elapse)) > 0) { |
f459b602 | 5018 | char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX]; |
fe68089d | 5019 | |
102b0214 YW |
5020 | bus_print_property_valuef(name, expected_value, value, "{ %s=%s ; next_elapse=%s }", base, |
5021 | format_timespan(timespan1, sizeof(timespan1), v, 0), | |
5022 | format_timespan(timespan2, sizeof(timespan2), next_elapse, 0)); | |
fe68089d | 5023 | } |
f459b602 MAP |
5024 | if (r < 0) |
5025 | return bus_log_parse_error(r); | |
5026 | ||
5027 | r = sd_bus_message_exit_container(m); | |
5028 | if (r < 0) | |
5029 | return bus_log_parse_error(r); | |
fe68089d | 5030 | |
07636114 | 5031 | return 1; |
fe68089d | 5032 | |
07636114 | 5033 | } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "TimersCalendar")) { |
fa6dee52 YW |
5034 | const char *base, *spec; |
5035 | uint64_t next_elapse; | |
5036 | ||
5037 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(sst)"); | |
5038 | if (r < 0) | |
5039 | return bus_log_parse_error(r); | |
5040 | ||
5041 | while ((r = sd_bus_message_read(m, "(sst)", &base, &spec, &next_elapse)) > 0) { | |
5042 | char timestamp[FORMAT_TIMESTAMP_MAX]; | |
5043 | ||
102b0214 YW |
5044 | bus_print_property_valuef(name, expected_value, value, "{ %s=%s ; next_elapse=%s }", base, spec, |
5045 | format_timestamp(timestamp, sizeof(timestamp), next_elapse)); | |
fa6dee52 YW |
5046 | } |
5047 | if (r < 0) | |
5048 | return bus_log_parse_error(r); | |
5049 | ||
5050 | r = sd_bus_message_exit_container(m); | |
5051 | if (r < 0) | |
5052 | return bus_log_parse_error(r); | |
5053 | ||
07636114 | 5054 | return 1; |
fa6dee52 | 5055 | |
07636114 | 5056 | } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && startswith(name, "Exec")) { |
f459b602 MAP |
5057 | ExecStatusInfo info = {}; |
5058 | ||
5059 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(sasbttttuii)"); | |
5060 | if (r < 0) | |
5061 | return bus_log_parse_error(r); | |
5062 | ||
5063 | while ((r = exec_status_info_deserialize(m, &info)) > 0) { | |
5064 | char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX]; | |
5065 | _cleanup_free_ char *tt; | |
5066 | ||
5067 | tt = strv_join(info.argv, " "); | |
5068 | ||
102b0214 YW |
5069 | bus_print_property_valuef(name, expected_value, value, |
5070 | "{ path=%s ; argv[]=%s ; ignore_errors=%s ; start_time=[%s] ; stop_time=[%s] ; pid="PID_FMT" ; code=%s ; status=%i%s%s }", | |
5071 | strna(info.path), | |
5072 | strna(tt), | |
5073 | yes_no(info.ignore), | |
5074 | strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)), | |
5075 | strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)), | |
5076 | info.pid, | |
5077 | sigchld_code_to_string(info.code), | |
5078 | info.status, | |
5079 | info.code == CLD_EXITED ? "" : "/", | |
5080 | strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status))); | |
fe68089d | 5081 | |
582a507f LP |
5082 | free(info.path); |
5083 | strv_free(info.argv); | |
f459b602 | 5084 | zero(info); |
707e5e52 LP |
5085 | } |
5086 | ||
f459b602 MAP |
5087 | r = sd_bus_message_exit_container(m); |
5088 | if (r < 0) | |
5089 | return bus_log_parse_error(r); | |
5090 | ||
07636114 | 5091 | return 1; |
4ad49000 | 5092 | |
07636114 | 5093 | } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "DeviceAllow")) { |
f459b602 | 5094 | const char *path, *rwm; |
4ad49000 | 5095 | |
f459b602 MAP |
5096 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(ss)"); |
5097 | if (r < 0) | |
5098 | return bus_log_parse_error(r); | |
4ad49000 | 5099 | |
f459b602 | 5100 | while ((r = sd_bus_message_read(m, "(ss)", &path, &rwm)) > 0) |
102b0214 | 5101 | bus_print_property_valuef(name, expected_value, value, "%s %s", strna(path), strna(rwm)); |
f459b602 MAP |
5102 | if (r < 0) |
5103 | return bus_log_parse_error(r); | |
4ad49000 | 5104 | |
f459b602 MAP |
5105 | r = sd_bus_message_exit_container(m); |
5106 | if (r < 0) | |
5107 | return bus_log_parse_error(r); | |
4ad49000 | 5108 | |
07636114 | 5109 | return 1; |
4ad49000 | 5110 | |
07636114 | 5111 | } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && |
72240b52 | 5112 | STR_IN_SET(name, "IODeviceWeight", "BlockIODeviceWeight")) { |
f459b602 MAP |
5113 | const char *path; |
5114 | uint64_t weight; | |
b8ab2dc6 | 5115 | |
f459b602 MAP |
5116 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(st)"); |
5117 | if (r < 0) | |
5118 | return bus_log_parse_error(r); | |
b8ab2dc6 | 5119 | |
f459b602 | 5120 | while ((r = sd_bus_message_read(m, "(st)", &path, &weight)) > 0) |
102b0214 | 5121 | bus_print_property_valuef(name, expected_value, value, "%s %"PRIu64, strna(path), weight); |
f459b602 MAP |
5122 | if (r < 0) |
5123 | return bus_log_parse_error(r); | |
b8ab2dc6 | 5124 | |
f459b602 MAP |
5125 | r = sd_bus_message_exit_container(m); |
5126 | if (r < 0) | |
5127 | return bus_log_parse_error(r); | |
b8ab2dc6 | 5128 | |
07636114 | 5129 | return 1; |
b8ab2dc6 | 5130 | |
07636114 | 5131 | } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && |
72240b52 ZJS |
5132 | (cgroup_io_limit_type_from_string(name) >= 0 || |
5133 | STR_IN_SET(name, "BlockIOReadBandwidth", "BlockIOWriteBandwidth"))) { | |
f459b602 MAP |
5134 | const char *path; |
5135 | uint64_t bandwidth; | |
4ad49000 | 5136 | |
f459b602 MAP |
5137 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(st)"); |
5138 | if (r < 0) | |
5139 | return bus_log_parse_error(r); | |
4ad49000 | 5140 | |
f459b602 | 5141 | while ((r = sd_bus_message_read(m, "(st)", &path, &bandwidth)) > 0) |
102b0214 | 5142 | bus_print_property_valuef(name, expected_value, value, "%s %"PRIu64, strna(path), bandwidth); |
f459b602 MAP |
5143 | if (r < 0) |
5144 | return bus_log_parse_error(r); | |
4ad49000 | 5145 | |
f459b602 MAP |
5146 | r = sd_bus_message_exit_container(m); |
5147 | if (r < 0) | |
5148 | return bus_log_parse_error(r); | |
4ad49000 | 5149 | |
07636114 | 5150 | return 1; |
6ae4283c TH |
5151 | |
5152 | } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && | |
5153 | streq(name, "IODeviceLatencyTargetUSec")) { | |
5154 | char ts[FORMAT_TIMESPAN_MAX]; | |
5155 | const char *path; | |
5156 | uint64_t target; | |
5157 | ||
5158 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(st)"); | |
5159 | if (r < 0) | |
5160 | return bus_log_parse_error(r); | |
5161 | ||
5162 | while ((r = sd_bus_message_read(m, "(st)", &path, &target)) > 0) | |
102b0214 YW |
5163 | bus_print_property_valuef(name, expected_value, value, "%s %s", strna(path), |
5164 | format_timespan(ts, sizeof(ts), target, 1)); | |
6ae4283c TH |
5165 | if (r < 0) |
5166 | return bus_log_parse_error(r); | |
5167 | ||
5168 | r = sd_bus_message_exit_container(m); | |
5169 | if (r < 0) | |
5170 | return bus_log_parse_error(r); | |
5171 | ||
5172 | return 1; | |
08f3be7a | 5173 | |
07636114 | 5174 | } else if (contents[0] == SD_BUS_TYPE_BYTE && streq(name, "StandardInputData")) { |
08f3be7a LP |
5175 | _cleanup_free_ char *h = NULL; |
5176 | const void *p; | |
5177 | size_t sz; | |
5178 | ssize_t n; | |
5179 | ||
5180 | r = sd_bus_message_read_array(m, 'y', &p, &sz); | |
5181 | if (r < 0) | |
5182 | return bus_log_parse_error(r); | |
5183 | ||
5184 | n = base64mem(p, sz, &h); | |
5185 | if (n < 0) | |
5186 | return log_oom(); | |
5187 | ||
102b0214 | 5188 | bus_print_property_value(name, expected_value, value, h); |
08f3be7a | 5189 | |
07636114 | 5190 | return 1; |
9c1f9465 YW |
5191 | |
5192 | } else if (STR_IN_SET(name, "IPAddressAllow", "IPAddressDeny")) { | |
5193 | _cleanup_free_ char *addresses = NULL; | |
5194 | ||
5195 | r = sd_bus_message_enter_container(m, 'a', "(iayu)"); | |
5196 | if (r < 0) | |
5197 | return bus_log_parse_error(r); | |
5198 | ||
5199 | for (;;) { | |
5200 | _cleanup_free_ char *str = NULL; | |
5201 | uint32_t prefixlen; | |
5202 | int32_t family; | |
5203 | const void *ap; | |
5204 | size_t an; | |
5205 | ||
5206 | r = sd_bus_message_enter_container(m, 'r', "iayu"); | |
5207 | if (r < 0) | |
5208 | return bus_log_parse_error(r); | |
5209 | if (r == 0) | |
5210 | break; | |
5211 | ||
5212 | r = sd_bus_message_read(m, "i", &family); | |
5213 | if (r < 0) | |
5214 | return bus_log_parse_error(r); | |
5215 | ||
5216 | r = sd_bus_message_read_array(m, 'y', &ap, &an); | |
5217 | if (r < 0) | |
5218 | return bus_log_parse_error(r); | |
5219 | ||
5220 | r = sd_bus_message_read(m, "u", &prefixlen); | |
5221 | if (r < 0) | |
5222 | return bus_log_parse_error(r); | |
5223 | ||
5224 | r = sd_bus_message_exit_container(m); | |
5225 | if (r < 0) | |
5226 | return bus_log_parse_error(r); | |
5227 | ||
5228 | if (!IN_SET(family, AF_INET, AF_INET6)) | |
5229 | continue; | |
5230 | ||
5231 | if (an != FAMILY_ADDRESS_SIZE(family)) | |
5232 | continue; | |
5233 | ||
5234 | if (prefixlen > FAMILY_ADDRESS_SIZE(family) * 8) | |
5235 | continue; | |
5236 | ||
5237 | if (in_addr_prefix_to_string(family, (union in_addr_union *) ap, prefixlen, &str) < 0) | |
5238 | continue; | |
5239 | ||
5240 | if (!strextend_with_separator(&addresses, " ", str, NULL)) | |
5241 | return log_oom(); | |
5242 | } | |
5243 | ||
5244 | r = sd_bus_message_exit_container(m); | |
5245 | if (r < 0) | |
5246 | return bus_log_parse_error(r); | |
5247 | ||
5248 | if (all || !isempty(addresses)) | |
5249 | bus_print_property_value(name, expected_value, value, strempty(addresses)); | |
5250 | ||
5251 | return 1; | |
4c787df7 YW |
5252 | |
5253 | } else if (STR_IN_SET(name, "BindPaths", "BindReadOnlyPaths")) { | |
5254 | _cleanup_free_ char *paths = NULL; | |
5255 | const char *source, *dest; | |
5256 | int ignore_enoent; | |
5257 | uint64_t rbind; | |
5258 | ||
5259 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(ssbt)"); | |
5260 | if (r < 0) | |
5261 | return bus_log_parse_error(r); | |
5262 | ||
5263 | while ((r = sd_bus_message_read(m, "(ssbt)", &source, &dest, &ignore_enoent, &rbind)) > 0) { | |
5264 | _cleanup_free_ char *str = NULL; | |
5265 | ||
5266 | if (isempty(source)) | |
5267 | continue; | |
5268 | ||
5269 | if (asprintf(&str, "%s%s%s%s%s", | |
5270 | ignore_enoent ? "-" : "", | |
5271 | source, | |
5272 | isempty(dest) ? "" : ":", | |
5273 | strempty(dest), | |
5274 | rbind == MS_REC ? ":rbind" : "") < 0) | |
5275 | return log_oom(); | |
5276 | ||
5277 | if (!strextend_with_separator(&paths, " ", str, NULL)) | |
5278 | return log_oom(); | |
5279 | } | |
5280 | if (r < 0) | |
5281 | return bus_log_parse_error(r); | |
5282 | ||
5283 | r = sd_bus_message_exit_container(m); | |
5284 | if (r < 0) | |
5285 | return bus_log_parse_error(r); | |
5286 | ||
5287 | if (all || !isempty(paths)) | |
5288 | bus_print_property_value(name, expected_value, value, strempty(paths)); | |
5289 | ||
5290 | return 1; | |
5291 | ||
5292 | } else if (streq(name, "TemporaryFileSystem")) { | |
5293 | _cleanup_free_ char *paths = NULL; | |
5294 | const char *target, *option; | |
5295 | ||
5296 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(ss)"); | |
5297 | if (r < 0) | |
5298 | return bus_log_parse_error(r); | |
5299 | ||
5300 | while ((r = sd_bus_message_read(m, "(ss)", &target, &option)) > 0) { | |
5301 | _cleanup_free_ char *str = NULL; | |
5302 | ||
5303 | if (isempty(target)) | |
5304 | continue; | |
5305 | ||
5306 | if (asprintf(&str, "%s%s%s", target, isempty(option) ? "" : ":", strempty(option)) < 0) | |
5307 | return log_oom(); | |
5308 | ||
5309 | if (!strextend_with_separator(&paths, " ", str, NULL)) | |
5310 | return log_oom(); | |
5311 | } | |
5312 | if (r < 0) | |
5313 | return bus_log_parse_error(r); | |
5314 | ||
5315 | r = sd_bus_message_exit_container(m); | |
5316 | if (r < 0) | |
5317 | return bus_log_parse_error(r); | |
5318 | ||
5319 | if (all || !isempty(paths)) | |
5320 | bus_print_property_value(name, expected_value, value, strempty(paths)); | |
5321 | ||
5322 | return 1; | |
305a2850 YW |
5323 | |
5324 | } else if (streq(name, "LogExtraFields")) { | |
5325 | _cleanup_free_ char *fields = NULL; | |
5326 | const void *p; | |
5327 | size_t sz; | |
5328 | ||
5329 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "ay"); | |
5330 | if (r < 0) | |
5331 | return bus_log_parse_error(r); | |
5332 | ||
5333 | while ((r = sd_bus_message_read_array(m, 'y', &p, &sz)) > 0) { | |
5334 | _cleanup_free_ char *str = NULL; | |
5335 | const char *eq; | |
5336 | ||
5337 | if (memchr(p, 0, sz)) | |
5338 | continue; | |
5339 | ||
5340 | eq = memchr(p, '=', sz); | |
5341 | if (!eq) | |
5342 | continue; | |
5343 | ||
5344 | if (!journal_field_valid(p, eq - (const char*) p, false)) | |
5345 | continue; | |
5346 | ||
5347 | str = malloc(sz + 1); | |
5348 | if (!str) | |
5349 | return log_oom(); | |
5350 | ||
5351 | memcpy(str, p, sz); | |
5352 | str[sz] = '\0'; | |
5353 | ||
5354 | if (!utf8_is_valid(str)) | |
5355 | continue; | |
5356 | ||
5357 | if (!strextend_with_separator(&fields, " ", str, NULL)) | |
5358 | return log_oom(); | |
5359 | } | |
5360 | if (r < 0) | |
5361 | return bus_log_parse_error(r); | |
5362 | ||
5363 | r = sd_bus_message_exit_container(m); | |
5364 | if (r < 0) | |
5365 | return bus_log_parse_error(r); | |
5366 | ||
5367 | if (all || !isempty(fields)) | |
5368 | bus_print_property_value(name, expected_value, value, strempty(fields)); | |
5369 | ||
5370 | return 1; | |
48220598 LP |
5371 | } |
5372 | ||
5373 | break; | |
5374 | } | |
5375 | ||
48220598 LP |
5376 | return 0; |
5377 | } | |
5378 | ||
8559e61d YW |
5379 | typedef enum SystemctlShowMode{ |
5380 | SYSTEMCTL_SHOW_PROPERTIES, | |
5381 | SYSTEMCTL_SHOW_STATUS, | |
5382 | SYSTEMCTL_SHOW_HELP, | |
5383 | _SYSTEMCTL_SHOW_MODE_MAX, | |
5384 | _SYSTEMCTL_SHOW_MODE_INVALID = -1, | |
5385 | } SystemctlShowMode; | |
5386 | ||
aa8d423e | 5387 | static const char* const systemctl_show_mode_table[_SYSTEMCTL_SHOW_MODE_MAX] = { |
8559e61d YW |
5388 | [SYSTEMCTL_SHOW_PROPERTIES] = "show", |
5389 | [SYSTEMCTL_SHOW_STATUS] = "status", | |
5390 | [SYSTEMCTL_SHOW_HELP] = "help", | |
5391 | }; | |
5392 | ||
5393 | DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(systemctl_show_mode, SystemctlShowMode); | |
5394 | ||
f459b602 | 5395 | static int show_one( |
f459b602 MAP |
5396 | sd_bus *bus, |
5397 | const char *path, | |
3dced37b | 5398 | const char *unit, |
8559e61d | 5399 | SystemctlShowMode show_mode, |
f459b602 MAP |
5400 | bool *new_line, |
5401 | bool *ellipsized) { | |
5402 | ||
3dced37b | 5403 | static const struct bus_properties_map property_map[] = { |
4679a8c3 YW |
5404 | { "LoadState", "s", NULL, offsetof(UnitStatusInfo, load_state) }, |
5405 | { "ActiveState", "s", NULL, offsetof(UnitStatusInfo, active_state) }, | |
5406 | { "Documentation", "as", NULL, offsetof(UnitStatusInfo, documentation) }, | |
5407 | {} | |
5408 | }, status_map[] = { | |
5409 | { "Id", "s", NULL, offsetof(UnitStatusInfo, id) }, | |
5410 | { "LoadState", "s", NULL, offsetof(UnitStatusInfo, load_state) }, | |
5411 | { "ActiveState", "s", NULL, offsetof(UnitStatusInfo, active_state) }, | |
5412 | { "SubState", "s", NULL, offsetof(UnitStatusInfo, sub_state) }, | |
5413 | { "UnitFileState", "s", NULL, offsetof(UnitStatusInfo, unit_file_state) }, | |
5414 | { "UnitFilePreset", "s", NULL, offsetof(UnitStatusInfo, unit_file_preset) }, | |
5415 | { "Description", "s", NULL, offsetof(UnitStatusInfo, description) }, | |
5416 | { "Following", "s", NULL, offsetof(UnitStatusInfo, following) }, | |
5417 | { "Documentation", "as", NULL, offsetof(UnitStatusInfo, documentation) }, | |
5418 | { "FragmentPath", "s", NULL, offsetof(UnitStatusInfo, fragment_path) }, | |
5419 | { "SourcePath", "s", NULL, offsetof(UnitStatusInfo, source_path) }, | |
5420 | { "ControlGroup", "s", NULL, offsetof(UnitStatusInfo, control_group) }, | |
5421 | { "DropInPaths", "as", NULL, offsetof(UnitStatusInfo, dropin_paths) }, | |
5422 | { "LoadError", "(ss)", map_load_error, offsetof(UnitStatusInfo, load_error) }, | |
5423 | { "Result", "s", NULL, offsetof(UnitStatusInfo, result) }, | |
5424 | { "InactiveExitTimestamp", "t", NULL, offsetof(UnitStatusInfo, inactive_exit_timestamp) }, | |
5425 | { "InactiveExitTimestampMonotonic", "t", NULL, offsetof(UnitStatusInfo, inactive_exit_timestamp_monotonic) }, | |
5426 | { "ActiveEnterTimestamp", "t", NULL, offsetof(UnitStatusInfo, active_enter_timestamp) }, | |
5427 | { "ActiveExitTimestamp", "t", NULL, offsetof(UnitStatusInfo, active_exit_timestamp) }, | |
5428 | { "InactiveEnterTimestamp", "t", NULL, offsetof(UnitStatusInfo, inactive_enter_timestamp) }, | |
5429 | { "NeedDaemonReload", "b", NULL, offsetof(UnitStatusInfo, need_daemon_reload) }, | |
5430 | { "Transient", "b", NULL, offsetof(UnitStatusInfo, transient) }, | |
5431 | { "ExecMainPID", "u", NULL, offsetof(UnitStatusInfo, main_pid) }, | |
5432 | { "MainPID", "u", map_main_pid, 0 }, | |
5433 | { "ControlPID", "u", NULL, offsetof(UnitStatusInfo, control_pid) }, | |
5434 | { "StatusText", "s", NULL, offsetof(UnitStatusInfo, status_text) }, | |
5435 | { "PIDFile", "s", NULL, offsetof(UnitStatusInfo, pid_file) }, | |
5436 | { "StatusErrno", "i", NULL, offsetof(UnitStatusInfo, status_errno) }, | |
5437 | { "ExecMainStartTimestamp", "t", NULL, offsetof(UnitStatusInfo, start_timestamp) }, | |
5438 | { "ExecMainExitTimestamp", "t", NULL, offsetof(UnitStatusInfo, exit_timestamp) }, | |
5439 | { "ExecMainCode", "i", NULL, offsetof(UnitStatusInfo, exit_code) }, | |
5440 | { "ExecMainStatus", "i", NULL, offsetof(UnitStatusInfo, exit_status) }, | |
5441 | { "ConditionTimestamp", "t", NULL, offsetof(UnitStatusInfo, condition_timestamp) }, | |
5442 | { "ConditionResult", "b", NULL, offsetof(UnitStatusInfo, condition_result) }, | |
5443 | { "Conditions", "a(sbbsi)", map_conditions, 0 }, | |
5444 | { "AssertTimestamp", "t", NULL, offsetof(UnitStatusInfo, assert_timestamp) }, | |
5445 | { "AssertResult", "b", NULL, offsetof(UnitStatusInfo, assert_result) }, | |
5446 | { "Asserts", "a(sbbsi)", map_asserts, 0 }, | |
5447 | { "NextElapseUSecRealtime", "t", NULL, offsetof(UnitStatusInfo, next_elapse_real) }, | |
5448 | { "NextElapseUSecMonotonic", "t", NULL, offsetof(UnitStatusInfo, next_elapse_monotonic) }, | |
5449 | { "NAccepted", "u", NULL, offsetof(UnitStatusInfo, n_accepted) }, | |
5450 | { "NConnections", "u", NULL, offsetof(UnitStatusInfo, n_connections) }, | |
a98f7575 | 5451 | { "NRefused", "u", NULL, offsetof(UnitStatusInfo, n_refused) }, |
4679a8c3 YW |
5452 | { "Accept", "b", NULL, offsetof(UnitStatusInfo, accept) }, |
5453 | { "Listen", "a(ss)", map_listen, offsetof(UnitStatusInfo, listen) }, | |
5454 | { "SysFSPath", "s", NULL, offsetof(UnitStatusInfo, sysfs_path) }, | |
5455 | { "Where", "s", NULL, offsetof(UnitStatusInfo, where) }, | |
5456 | { "What", "s", NULL, offsetof(UnitStatusInfo, what) }, | |
5457 | { "MemoryCurrent", "t", NULL, offsetof(UnitStatusInfo, memory_current) }, | |
48422635 | 5458 | { "MemoryMin", "t", NULL, offsetof(UnitStatusInfo, memory_min) }, |
4679a8c3 YW |
5459 | { "MemoryLow", "t", NULL, offsetof(UnitStatusInfo, memory_low) }, |
5460 | { "MemoryHigh", "t", NULL, offsetof(UnitStatusInfo, memory_high) }, | |
5461 | { "MemoryMax", "t", NULL, offsetof(UnitStatusInfo, memory_max) }, | |
5462 | { "MemorySwapMax", "t", NULL, offsetof(UnitStatusInfo, memory_swap_max) }, | |
5463 | { "MemoryLimit", "t", NULL, offsetof(UnitStatusInfo, memory_limit) }, | |
5464 | { "CPUUsageNSec", "t", NULL, offsetof(UnitStatusInfo, cpu_usage_nsec) }, | |
5465 | { "TasksCurrent", "t", NULL, offsetof(UnitStatusInfo, tasks_current) }, | |
5466 | { "TasksMax", "t", NULL, offsetof(UnitStatusInfo, tasks_max) }, | |
5467 | { "IPIngressBytes", "t", NULL, offsetof(UnitStatusInfo, ip_ingress_bytes) }, | |
5468 | { "IPEgressBytes", "t", NULL, offsetof(UnitStatusInfo, ip_egress_bytes) }, | |
5469 | { "ExecStartPre", "a(sasbttttuii)", map_exec, 0 }, | |
5470 | { "ExecStart", "a(sasbttttuii)", map_exec, 0 }, | |
5471 | { "ExecStartPost", "a(sasbttttuii)", map_exec, 0 }, | |
5472 | { "ExecReload", "a(sasbttttuii)", map_exec, 0 }, | |
5473 | { "ExecStopPre", "a(sasbttttuii)", map_exec, 0 }, | |
5474 | { "ExecStop", "a(sasbttttuii)", map_exec, 0 }, | |
5475 | { "ExecStopPost", "a(sasbttttuii)", map_exec, 0 }, | |
3dced37b LP |
5476 | {} |
5477 | }; | |
5478 | ||
4afd3348 LP |
5479 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
5480 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
3dced37b | 5481 | _cleanup_set_free_ Set *found_properties = NULL; |
a7335518 | 5482 | _cleanup_(unit_status_info_free) UnitStatusInfo info = { |
934277fe | 5483 | .memory_current = (uint64_t) -1, |
da4d897e TH |
5484 | .memory_high = CGROUP_LIMIT_MAX, |
5485 | .memory_max = CGROUP_LIMIT_MAX, | |
96e131ea | 5486 | .memory_swap_max = CGROUP_LIMIT_MAX, |
934277fe | 5487 | .memory_limit = (uint64_t) -1, |
5ad096b3 | 5488 | .cpu_usage_nsec = (uint64_t) -1, |
03a7b521 LP |
5489 | .tasks_current = (uint64_t) -1, |
5490 | .tasks_max = (uint64_t) -1, | |
0e97c93f DM |
5491 | .ip_ingress_bytes = (uint64_t) -1, |
5492 | .ip_egress_bytes = (uint64_t) -1, | |
934277fe | 5493 | }; |
4679a8c3 | 5494 | char **pp; |
f459b602 | 5495 | int r; |
48220598 | 5496 | |
48220598 | 5497 | assert(path); |
61cbdc4b | 5498 | assert(new_line); |
48220598 | 5499 | |
e3e0314b ZJS |
5500 | log_debug("Showing one %s", path); |
5501 | ||
4679a8c3 | 5502 | r = bus_map_all_properties( |
f22f08cd SP |
5503 | bus, |
5504 | "org.freedesktop.systemd1", | |
5505 | path, | |
4679a8c3 | 5506 | show_mode == SYSTEMCTL_SHOW_STATUS ? status_map : property_map, |
a7e4861c | 5507 | BUS_MAP_BOOLEAN_AS_BOOL, |
f459b602 | 5508 | &error, |
f22f08cd | 5509 | &reply, |
4679a8c3 | 5510 | &info); |
4c3e8e39 LP |
5511 | if (r < 0) |
5512 | return log_error_errno(r, "Failed to get properties: %s", bus_error_message(&error, r)); | |
48220598 | 5513 | |
4679a8c3 YW |
5514 | if (unit && streq_ptr(info.load_state, "not-found") && streq_ptr(info.active_state, "inactive")) { |
5515 | log_full(show_mode == SYSTEMCTL_SHOW_STATUS ? LOG_ERR : LOG_DEBUG, | |
5516 | "Unit %s could not be found.", unit); | |
e33a06a1 | 5517 | |
4679a8c3 YW |
5518 | if (show_mode == SYSTEMCTL_SHOW_STATUS) |
5519 | return EXIT_PROGRAM_OR_SERVICES_STATUS_UNKNOWN; | |
5520 | else if (show_mode == SYSTEMCTL_SHOW_HELP) | |
5521 | return -ENOENT; | |
5522 | } | |
e33a06a1 | 5523 | |
4679a8c3 YW |
5524 | if (*new_line) |
5525 | printf("\n"); | |
3dced37b | 5526 | |
4679a8c3 YW |
5527 | *new_line = true; |
5528 | ||
5529 | if (show_mode == SYSTEMCTL_SHOW_STATUS) { | |
5530 | print_status_info(bus, &info, ellipsized); | |
5531 | ||
5532 | if (info.active_state && !STR_IN_SET(info.active_state, "active", "reloading")) | |
5533 | return EXIT_PROGRAM_NOT_RUNNING; | |
5534 | ||
5535 | return EXIT_PROGRAM_RUNNING_OR_SERVICE_OK; | |
5536 | ||
5537 | } else if (show_mode == SYSTEMCTL_SHOW_HELP) { | |
5538 | show_unit_help(&info); | |
5539 | return 0; | |
3dced37b | 5540 | } |
e33a06a1 | 5541 | |
4679a8c3 YW |
5542 | r = sd_bus_message_rewind(reply, true); |
5543 | if (r < 0) | |
5544 | return log_error_errno(r, "Failed to rewind: %s", bus_error_message(&error, r)); | |
5545 | ||
07636114 | 5546 | r = bus_message_print_all_properties(reply, print_property, arg_properties, arg_value, arg_all, &found_properties); |
f459b602 MAP |
5547 | if (r < 0) |
5548 | return bus_log_parse_error(r); | |
48220598 | 5549 | |
4679a8c3 YW |
5550 | STRV_FOREACH(pp, arg_properties) |
5551 | if (!set_contains(found_properties, *pp)) | |
5552 | log_debug("Property %s does not exist.", *pp); | |
3dced37b | 5553 | |
4679a8c3 | 5554 | return 0; |
48220598 LP |
5555 | } |
5556 | ||
f74294c1 | 5557 | static int get_unit_dbus_path_by_pid( |
f459b602 MAP |
5558 | sd_bus *bus, |
5559 | uint32_t pid, | |
f74294c1 | 5560 | char **unit) { |
f459b602 | 5561 | |
4afd3348 LP |
5562 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
5563 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
373d32c9 | 5564 | char *u; |
a223b325 MS |
5565 | int r; |
5566 | ||
f459b602 | 5567 | r = sd_bus_call_method( |
f22f08cd SP |
5568 | bus, |
5569 | "org.freedesktop.systemd1", | |
5570 | "/org/freedesktop/systemd1", | |
5571 | "org.freedesktop.systemd1.Manager", | |
5572 | "GetUnitByPID", | |
f459b602 | 5573 | &error, |
f22f08cd | 5574 | &reply, |
f459b602 | 5575 | "u", pid); |
691395d8 LP |
5576 | if (r < 0) |
5577 | return log_error_errno(r, "Failed to get unit for PID %"PRIu32": %s", pid, bus_error_message(&error, r)); | |
a223b325 | 5578 | |
373d32c9 | 5579 | r = sd_bus_message_read(reply, "o", &u); |
f459b602 MAP |
5580 | if (r < 0) |
5581 | return bus_log_parse_error(r); | |
5582 | ||
373d32c9 LP |
5583 | u = strdup(u); |
5584 | if (!u) | |
5585 | return log_oom(); | |
5586 | ||
5587 | *unit = u; | |
f74294c1 | 5588 | return 0; |
a223b325 MS |
5589 | } |
5590 | ||
f459b602 | 5591 | static int show_all( |
f459b602 | 5592 | sd_bus *bus, |
f459b602 MAP |
5593 | bool *new_line, |
5594 | bool *ellipsized) { | |
5595 | ||
4afd3348 | 5596 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
f459b602 MAP |
5597 | _cleanup_free_ UnitInfo *unit_infos = NULL; |
5598 | const UnitInfo *u; | |
5599 | unsigned c; | |
5bb75bc7 | 5600 | int r, ret = 0; |
265a7a2a | 5601 | |
1238ee09 | 5602 | r = get_unit_list(bus, NULL, NULL, &unit_infos, 0, &reply); |
265a7a2a ZJS |
5603 | if (r < 0) |
5604 | return r; | |
5605 | ||
0221d68a | 5606 | (void) pager_open(arg_pager_flags); |
dbed408b | 5607 | |
f459b602 MAP |
5608 | c = (unsigned) r; |
5609 | ||
93bab288 | 5610 | typesafe_qsort(unit_infos, c, compare_unit_info); |
991f2a39 | 5611 | |
265a7a2a | 5612 | for (u = unit_infos; u < unit_infos + c; u++) { |
7fd1b19b | 5613 | _cleanup_free_ char *p = NULL; |
265a7a2a | 5614 | |
265a7a2a ZJS |
5615 | p = unit_dbus_path_from_name(u->id); |
5616 | if (!p) | |
5617 | return log_oom(); | |
5618 | ||
8559e61d | 5619 | r = show_one(bus, p, u->id, SYSTEMCTL_SHOW_STATUS, new_line, ellipsized); |
3df538da | 5620 | if (r < 0) |
265a7a2a | 5621 | return r; |
5bb75bc7 ZJS |
5622 | else if (r > 0 && ret == 0) |
5623 | ret = r; | |
265a7a2a ZJS |
5624 | } |
5625 | ||
5bb75bc7 | 5626 | return ret; |
265a7a2a ZJS |
5627 | } |
5628 | ||
8fcf784d LP |
5629 | static int show_system_status(sd_bus *bus) { |
5630 | char since1[FORMAT_TIMESTAMP_RELATIVE_MAX], since2[FORMAT_TIMESTAMP_MAX]; | |
f9e0eefc | 5631 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
e7e55dbd | 5632 | _cleanup_(machine_info_clear) struct machine_info mi = {}; |
f9e0eefc | 5633 | _cleanup_free_ char *hn = NULL; |
8fcf784d LP |
5634 | const char *on, *off; |
5635 | int r; | |
5636 | ||
5637 | hn = gethostname_malloc(); | |
5638 | if (!hn) | |
5639 | return log_oom(); | |
5640 | ||
a7e4861c YW |
5641 | r = bus_map_all_properties( |
5642 | bus, | |
5643 | "org.freedesktop.systemd1", | |
5644 | "/org/freedesktop/systemd1", | |
5645 | machine_info_property_map, | |
5646 | BUS_MAP_STRDUP, | |
5647 | &error, | |
5648 | NULL, | |
5649 | &mi); | |
f647962d | 5650 | if (r < 0) |
f9e0eefc | 5651 | return log_error_errno(r, "Failed to read server status: %s", bus_error_message(&error, r)); |
8fcf784d | 5652 | |
8fcf784d LP |
5653 | if (streq_ptr(mi.state, "degraded")) { |
5654 | on = ansi_highlight_red(); | |
1fc464f6 | 5655 | off = ansi_normal(); |
92f34a9c JR |
5656 | } else if (streq_ptr(mi.state, "running")) { |
5657 | on = ansi_highlight_green(); | |
5658 | off = ansi_normal(); | |
5659 | } else { | |
8fcf784d | 5660 | on = ansi_highlight_yellow(); |
1fc464f6 | 5661 | off = ansi_normal(); |
92f34a9c | 5662 | } |
8fcf784d | 5663 | |
9a6f746f | 5664 | printf("%s%s%s %s\n", on, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE), off, arg_host ? arg_host : hn); |
b0d14c69 | 5665 | |
8fcf784d LP |
5666 | printf(" State: %s%s%s\n", |
5667 | on, strna(mi.state), off); | |
5668 | ||
c6ba5b80 MP |
5669 | printf(" Jobs: %" PRIu32 " queued\n", mi.n_jobs); |
5670 | printf(" Failed: %" PRIu32 " units\n", mi.n_failed_units); | |
8fcf784d LP |
5671 | |
5672 | printf(" Since: %s; %s\n", | |
5673 | format_timestamp(since2, sizeof(since2), mi.timestamp), | |
5674 | format_timestamp_relative(since1, sizeof(since1), mi.timestamp)); | |
5675 | ||
5676 | printf(" CGroup: %s\n", mi.control_group ?: "/"); | |
4c315c2c IS |
5677 | if (IN_SET(arg_transport, |
5678 | BUS_TRANSPORT_LOCAL, | |
5679 | BUS_TRANSPORT_MACHINE)) { | |
8fcf784d LP |
5680 | static const char prefix[] = " "; |
5681 | unsigned c; | |
5682 | ||
5683 | c = columns(); | |
5684 | if (c > sizeof(prefix) - 1) | |
5685 | c -= sizeof(prefix) - 1; | |
5686 | else | |
5687 | c = 0; | |
5688 | ||
0ff308c8 | 5689 | show_cgroup(SYSTEMD_CGROUP_CONTROLLER, strempty(mi.control_group), prefix, c, get_output_flags()); |
8fcf784d LP |
5690 | } |
5691 | ||
8fcf784d LP |
5692 | return 0; |
5693 | } | |
5694 | ||
e449de87 | 5695 | static int show(int argc, char *argv[], void *userdata) { |
8559e61d YW |
5696 | bool new_line = false, ellipsized = false; |
5697 | SystemctlShowMode show_mode; | |
e3e0314b | 5698 | int r, ret = 0; |
4fbd7192 | 5699 | sd_bus *bus; |
48220598 | 5700 | |
e449de87 | 5701 | assert(argv); |
48220598 | 5702 | |
8559e61d | 5703 | show_mode = systemctl_show_mode_from_string(argv[0]); |
baaa35ad ZJS |
5704 | if (show_mode < 0) |
5705 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
5706 | "Invalid argument."); | |
e449de87 | 5707 | |
baaa35ad ZJS |
5708 | if (show_mode == SYSTEMCTL_SHOW_HELP && argc <= 1) |
5709 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
5710 | "This command expects one or more unit names. Did you mean --help?"); | |
61cbdc4b | 5711 | |
d2ad7e1b ZJS |
5712 | r = acquire_bus(BUS_MANAGER, &bus); |
5713 | if (r < 0) | |
5714 | return r; | |
5715 | ||
0221d68a | 5716 | (void) pager_open(arg_pager_flags); |
ec14911e | 5717 | |
4fbd7192 | 5718 | /* If no argument is specified inspect the manager itself */ |
8559e61d YW |
5719 | if (show_mode == SYSTEMCTL_SHOW_PROPERTIES && argc <= 1) |
5720 | return show_one(bus, "/org/freedesktop/systemd1", NULL, show_mode, &new_line, &ellipsized); | |
48220598 | 5721 | |
8559e61d | 5722 | if (show_mode == SYSTEMCTL_SHOW_STATUS && argc <= 1) { |
8fcf784d | 5723 | |
8fcf784d LP |
5724 | show_system_status(bus); |
5725 | new_line = true; | |
5726 | ||
5727 | if (arg_all) | |
8559e61d | 5728 | ret = show_all(bus, &new_line, &ellipsized); |
8fcf784d | 5729 | } else { |
e3e0314b ZJS |
5730 | _cleanup_free_ char **patterns = NULL; |
5731 | char **name; | |
5732 | ||
e449de87 | 5733 | STRV_FOREACH(name, strv_skip(argv, 1)) { |
3dced37b | 5734 | _cleanup_free_ char *path = NULL, *unit = NULL; |
94e0bd7d | 5735 | uint32_t id; |
48220598 | 5736 | |
94e0bd7d | 5737 | if (safe_atou32(*name, &id) < 0) { |
e3e0314b | 5738 | if (strv_push(&patterns, *name) < 0) |
94e0bd7d | 5739 | return log_oom(); |
48220598 | 5740 | |
e3e0314b | 5741 | continue; |
8559e61d | 5742 | } else if (show_mode == SYSTEMCTL_SHOW_PROPERTIES) { |
94e0bd7d | 5743 | /* Interpret as job id */ |
3dced37b | 5744 | if (asprintf(&path, "/org/freedesktop/systemd1/job/%u", id) < 0) |
94e0bd7d | 5745 | return log_oom(); |
48220598 | 5746 | |
94e0bd7d ZJS |
5747 | } else { |
5748 | /* Interpret as PID */ | |
3dced37b | 5749 | r = get_unit_dbus_path_by_pid(bus, id, &path); |
373d32c9 | 5750 | if (r < 0) { |
94e0bd7d | 5751 | ret = r; |
373d32c9 LP |
5752 | continue; |
5753 | } | |
3dced37b LP |
5754 | |
5755 | r = unit_name_from_dbus_path(path, &unit); | |
5756 | if (r < 0) | |
5757 | return log_oom(); | |
94e0bd7d | 5758 | } |
f74294c1 | 5759 | |
8559e61d | 5760 | r = show_one(bus, path, unit, show_mode, &new_line, &ellipsized); |
5bb75bc7 ZJS |
5761 | if (r < 0) |
5762 | return r; | |
5763 | else if (r > 0 && ret == 0) | |
5764 | ret = r; | |
48220598 | 5765 | } |
94e0bd7d | 5766 | |
e3e0314b ZJS |
5767 | if (!strv_isempty(patterns)) { |
5768 | _cleanup_strv_free_ char **names = NULL; | |
5769 | ||
5770 | r = expand_names(bus, patterns, NULL, &names); | |
5771 | if (r < 0) | |
691395d8 | 5772 | return log_error_errno(r, "Failed to expand names: %m"); |
e3e0314b ZJS |
5773 | |
5774 | STRV_FOREACH(name, names) { | |
3dced37b | 5775 | _cleanup_free_ char *path; |
e3e0314b | 5776 | |
3dced37b LP |
5777 | path = unit_dbus_path_from_name(*name); |
5778 | if (!path) | |
e3e0314b ZJS |
5779 | return log_oom(); |
5780 | ||
8559e61d | 5781 | r = show_one(bus, path, *name, show_mode, &new_line, &ellipsized); |
5bb75bc7 ZJS |
5782 | if (r < 0) |
5783 | return r; | |
3dced37b | 5784 | if (r > 0 && ret == 0) |
5bb75bc7 | 5785 | ret = r; |
e3e0314b ZJS |
5786 | } |
5787 | } | |
5788 | } | |
5789 | ||
94e0bd7d ZJS |
5790 | if (ellipsized && !arg_quiet) |
5791 | printf("Hint: Some lines were ellipsized, use -l to show in full.\n"); | |
48220598 | 5792 | |
22f4096c | 5793 | return ret; |
0183528f LP |
5794 | } |
5795 | ||
e449de87 | 5796 | static int cat(int argc, char *argv[], void *userdata) { |
8e766630 | 5797 | _cleanup_(lookup_paths_free) LookupPaths lp = {}; |
15ef1144 LP |
5798 | _cleanup_strv_free_ char **names = NULL; |
5799 | char **name; | |
4fbd7192 LP |
5800 | sd_bus *bus; |
5801 | bool first = true; | |
25586912 | 5802 | int r; |
15ef1144 | 5803 | |
3e7eed84 | 5804 | if (arg_transport != BUS_TRANSPORT_LOCAL) { |
4fbd7192 | 5805 | log_error("Cannot remotely cat units."); |
3e495a66 ZJS |
5806 | return -EINVAL; |
5807 | } | |
5808 | ||
4943d143 | 5809 | r = lookup_paths_init(&lp, arg_scope, 0, arg_root); |
ad2a0358 | 5810 | if (r < 0) |
c51932be | 5811 | return log_error_errno(r, "Failed to determine unit paths: %m"); |
ad2a0358 | 5812 | |
4fbd7192 | 5813 | r = acquire_bus(BUS_MANAGER, &bus); |
15ef1144 | 5814 | if (r < 0) |
4fbd7192 | 5815 | return r; |
15ef1144 | 5816 | |
e449de87 | 5817 | r = expand_names(bus, strv_skip(argv, 1), NULL, &names); |
4fbd7192 LP |
5818 | if (r < 0) |
5819 | return log_error_errno(r, "Failed to expand names: %m"); | |
ad2a0358 | 5820 | |
0221d68a | 5821 | (void) pager_open(arg_pager_flags); |
15ef1144 LP |
5822 | |
5823 | STRV_FOREACH(name, names) { | |
ad2a0358 | 5824 | _cleanup_free_ char *fragment_path = NULL; |
15ef1144 | 5825 | _cleanup_strv_free_ char **dropin_paths = NULL; |
15ef1144 | 5826 | |
02a126a3 | 5827 | r = unit_find_paths(bus, *name, &lp, false, &fragment_path, &dropin_paths); |
86f004fb | 5828 | if (r == -ERFKILL) { |
02a126a3 LP |
5829 | printf("%s# Unit %s is masked%s.\n", |
5830 | ansi_highlight_magenta(), | |
5831 | *name, | |
5832 | ansi_normal()); | |
5833 | continue; | |
5834 | } | |
5835 | if (r == -EKEYREJECTED) { | |
5836 | printf("%s# Unit %s could not be loaded.%s\n", | |
86f004fb ZJS |
5837 | ansi_highlight_magenta(), |
5838 | *name, | |
5839 | ansi_normal()); | |
5840 | continue; | |
5841 | } | |
ad2a0358 ZJS |
5842 | if (r < 0) |
5843 | return r; | |
02a126a3 | 5844 | if (r == 0) |
b5e6a600 | 5845 | return -ENOENT; |
15ef1144 LP |
5846 | |
5847 | if (first) | |
5848 | first = false; | |
5849 | else | |
5850 | puts(""); | |
5851 | ||
5b9635d1 | 5852 | if (need_daemon_reload(bus, *name) > 0) /* ignore errors (<0), this is informational output */ |
e100155d LW |
5853 | fprintf(stderr, |
5854 | "%s# Warning: %s changed on disk, the version systemd has loaded is outdated.\n" | |
5855 | "%s# This output shows the current version of the unit's original fragment and drop-in files.\n" | |
5856 | "%s# If fragments or drop-ins were added or removed, they are not properly reflected in this output.\n" | |
5857 | "%s# Run 'systemctl%s daemon-reload' to reload units.%s\n", | |
5858 | ansi_highlight_red(), | |
5859 | *name, | |
5860 | ansi_highlight_red(), | |
5861 | ansi_highlight_red(), | |
5862 | ansi_highlight_red(), | |
5863 | arg_scope == UNIT_FILE_SYSTEM ? "" : " --user", | |
5864 | ansi_normal()); | |
5865 | ||
854a42fb | 5866 | r = cat_files(fragment_path, dropin_paths, 0); |
81f5e513 ZJS |
5867 | if (r < 0) |
5868 | return r; | |
15ef1144 LP |
5869 | } |
5870 | ||
25586912 | 5871 | return 0; |
15ef1144 LP |
5872 | } |
5873 | ||
e449de87 | 5874 | static int set_property(int argc, char *argv[], void *userdata) { |
4afd3348 LP |
5875 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
5876 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
68372da6 | 5877 | _cleanup_free_ char *n = NULL; |
89ada3ba | 5878 | UnitType t; |
4fbd7192 | 5879 | sd_bus *bus; |
8e2af478 LP |
5880 | int r; |
5881 | ||
4fbd7192 LP |
5882 | r = acquire_bus(BUS_MANAGER, &bus); |
5883 | if (r < 0) | |
5884 | return r; | |
5885 | ||
8a4b13c5 | 5886 | polkit_agent_open_maybe(); |
d2ad7e1b | 5887 | |
f459b602 MAP |
5888 | r = sd_bus_message_new_method_call( |
5889 | bus, | |
151b9b96 | 5890 | &m, |
8e2af478 LP |
5891 | "org.freedesktop.systemd1", |
5892 | "/org/freedesktop/systemd1", | |
5893 | "org.freedesktop.systemd1.Manager", | |
151b9b96 | 5894 | "SetUnitProperties"); |
f459b602 MAP |
5895 | if (r < 0) |
5896 | return bus_log_create_error(r); | |
8e2af478 | 5897 | |
37cbc1d5 | 5898 | r = unit_name_mangle(argv[1], arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN, &n); |
7410616c LP |
5899 | if (r < 0) |
5900 | return log_error_errno(r, "Failed to mangle unit name: %m"); | |
68372da6 | 5901 | |
89ada3ba YW |
5902 | t = unit_name_to_type(n); |
5903 | if (t < 0) { | |
5904 | log_error("Invalid unit type: %s", n); | |
5905 | return -EINVAL; | |
5906 | } | |
5907 | ||
f459b602 MAP |
5908 | r = sd_bus_message_append(m, "sb", n, arg_runtime); |
5909 | if (r < 0) | |
5910 | return bus_log_create_error(r); | |
8e2af478 | 5911 | |
f459b602 MAP |
5912 | r = sd_bus_message_open_container(m, SD_BUS_TYPE_ARRAY, "(sv)"); |
5913 | if (r < 0) | |
5914 | return bus_log_create_error(r); | |
8e2af478 | 5915 | |
89ada3ba | 5916 | r = bus_append_unit_property_assignment_many(m, t, strv_skip(argv, 2)); |
8673cf13 LP |
5917 | if (r < 0) |
5918 | return r; | |
8e2af478 | 5919 | |
f459b602 MAP |
5920 | r = sd_bus_message_close_container(m); |
5921 | if (r < 0) | |
5922 | return bus_log_create_error(r); | |
8e2af478 | 5923 | |
c49b30a2 | 5924 | r = sd_bus_call(bus, m, 0, &error, NULL); |
691395d8 LP |
5925 | if (r < 0) |
5926 | return log_error_errno(r, "Failed to set unit properties on %s: %s", n, bus_error_message(&error, r)); | |
8e2af478 LP |
5927 | |
5928 | return 0; | |
5929 | } | |
5930 | ||
e449de87 | 5931 | static int daemon_reload(int argc, char *argv[], void *userdata) { |
4afd3348 | 5932 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
2853b60a | 5933 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
7e4249b9 | 5934 | const char *method; |
4fbd7192 | 5935 | sd_bus *bus; |
f459b602 | 5936 | int r; |
7e4249b9 | 5937 | |
4fbd7192 LP |
5938 | r = acquire_bus(BUS_MANAGER, &bus); |
5939 | if (r < 0) | |
5940 | return r; | |
5941 | ||
8a4b13c5 | 5942 | polkit_agent_open_maybe(); |
d2ad7e1b | 5943 | |
2853b60a LP |
5944 | switch (arg_action) { |
5945 | ||
5946 | case ACTION_RELOAD: | |
e4b61340 | 5947 | method = "Reload"; |
2853b60a LP |
5948 | break; |
5949 | ||
5950 | case ACTION_REEXEC: | |
e4b61340 | 5951 | method = "Reexecute"; |
2853b60a LP |
5952 | break; |
5953 | ||
5954 | case ACTION_SYSTEMCTL: | |
5955 | method = streq(argv[0], "daemon-reexec") ? "Reexecute" : | |
5956 | /* "daemon-reload" */ "Reload"; | |
5957 | break; | |
e4b61340 | 5958 | |
2853b60a LP |
5959 | default: |
5960 | assert_not_reached("Unexpected action"); | |
e4b61340 | 5961 | } |
7e4249b9 | 5962 | |
2853b60a LP |
5963 | r = sd_bus_message_new_method_call( |
5964 | bus, | |
5965 | &m, | |
5966 | "org.freedesktop.systemd1", | |
5967 | "/org/freedesktop/systemd1", | |
5968 | "org.freedesktop.systemd1.Manager", | |
5969 | method); | |
5970 | if (r < 0) | |
5971 | return bus_log_create_error(r); | |
5972 | ||
5973 | /* Note we use an extra-long timeout here. This is because a reload or reexec means generators are rerun which | |
5974 | * are timed out after DEFAULT_TIMEOUT_USEC. Let's use twice that time here, so that the generators can have | |
5975 | * their timeout, and for everything else there's the same time budget in place. */ | |
5976 | ||
5977 | r = sd_bus_call(bus, m, DEFAULT_TIMEOUT_USEC * 2, &error, NULL); | |
5978 | ||
5979 | /* On reexecution, we expect a disconnect, not a reply */ | |
5980 | if (IN_SET(r, -ETIMEDOUT, -ECONNRESET) && streq(method, "Reexecute")) | |
5981 | r = 0; | |
5982 | ||
5983 | if (r < 0 && arg_action == ACTION_SYSTEMCTL) | |
5984 | return log_error_errno(r, "Failed to reload daemon: %s", bus_error_message(&error, r)); | |
5985 | ||
5986 | /* Note that for the legacy commands (i.e. those with action != ACTION_SYSTEMCTL) we support fallbacks to the | |
5987 | * old ways of doing things, hence don't log any error in that case here. */ | |
5988 | ||
5989 | return r < 0 ? r : 0; | |
5990 | } | |
5991 | ||
5992 | static int trivial_method(int argc, char *argv[], void *userdata) { | |
5993 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
5994 | const char *method; | |
5995 | sd_bus *bus; | |
5996 | int r; | |
5997 | ||
1ae17672 ZJS |
5998 | if (arg_dry_run) |
5999 | return 0; | |
6000 | ||
2853b60a LP |
6001 | r = acquire_bus(BUS_MANAGER, &bus); |
6002 | if (r < 0) | |
6003 | return r; | |
6004 | ||
8a4b13c5 | 6005 | polkit_agent_open_maybe(); |
d2ad7e1b | 6006 | |
2853b60a LP |
6007 | method = |
6008 | streq(argv[0], "clear-jobs") || | |
6009 | streq(argv[0], "cancel") ? "ClearJobs" : | |
6010 | streq(argv[0], "reset-failed") ? "ResetFailed" : | |
6011 | streq(argv[0], "halt") ? "Halt" : | |
6012 | streq(argv[0], "reboot") ? "Reboot" : | |
6013 | streq(argv[0], "kexec") ? "KExec" : | |
6014 | streq(argv[0], "exit") ? "Exit" : | |
6015 | /* poweroff */ "PowerOff"; | |
6016 | ||
6e646d22 | 6017 | r = sd_bus_call_method( |
f22f08cd SP |
6018 | bus, |
6019 | "org.freedesktop.systemd1", | |
6020 | "/org/freedesktop/systemd1", | |
6021 | "org.freedesktop.systemd1.Manager", | |
6e646d22 LP |
6022 | method, |
6023 | &error, | |
6024 | NULL, | |
6025 | NULL); | |
2853b60a LP |
6026 | if (r < 0 && arg_action == ACTION_SYSTEMCTL) |
6027 | return log_error_errno(r, "Failed to execute operation: %s", bus_error_message(&error, r)); | |
6028 | ||
6029 | /* Note that for the legacy commands (i.e. those with action != ACTION_SYSTEMCTL) we support fallbacks to the | |
6030 | * old ways of doing things, hence don't log any error in that case here. */ | |
7e4249b9 | 6031 | |
0a9776c2 | 6032 | return r < 0 ? r : 0; |
7e4249b9 LP |
6033 | } |
6034 | ||
e449de87 | 6035 | static int reset_failed(int argc, char *argv[], void *userdata) { |
e3e0314b | 6036 | _cleanup_strv_free_ char **names = NULL; |
4fbd7192 | 6037 | sd_bus *bus; |
f84190d8 | 6038 | char **name; |
e3e0314b | 6039 | int r, q; |
5632e374 | 6040 | |
e449de87 | 6041 | if (argc <= 1) |
2853b60a | 6042 | return trivial_method(argc, argv, userdata); |
5632e374 | 6043 | |
4fbd7192 LP |
6044 | r = acquire_bus(BUS_MANAGER, &bus); |
6045 | if (r < 0) | |
6046 | return r; | |
6047 | ||
8a4b13c5 | 6048 | polkit_agent_open_maybe(); |
d2ad7e1b | 6049 | |
e449de87 | 6050 | r = expand_names(bus, strv_skip(argv, 1), NULL, &names); |
e3e0314b | 6051 | if (r < 0) |
691395d8 | 6052 | return log_error_errno(r, "Failed to expand names: %m"); |
f84190d8 | 6053 | |
e3e0314b | 6054 | STRV_FOREACH(name, names) { |
4afd3348 | 6055 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
342641fb | 6056 | |
6e646d22 | 6057 | q = sd_bus_call_method( |
f22f08cd | 6058 | bus, |
b0193f1c LP |
6059 | "org.freedesktop.systemd1", |
6060 | "/org/freedesktop/systemd1", | |
6061 | "org.freedesktop.systemd1.Manager", | |
6e646d22 LP |
6062 | "ResetFailedUnit", |
6063 | &error, | |
6064 | NULL, | |
6065 | "s", *name); | |
e3e0314b | 6066 | if (q < 0) { |
691395d8 | 6067 | log_error_errno(q, "Failed to reset failed state of unit %s: %s", *name, bus_error_message(&error, q)); |
e3e0314b ZJS |
6068 | if (r == 0) |
6069 | r = q; | |
f459b602 | 6070 | } |
5632e374 LP |
6071 | } |
6072 | ||
e3e0314b | 6073 | return r; |
5632e374 LP |
6074 | } |
6075 | ||
804ee07c ZJS |
6076 | static int print_variable(const char *s) { |
6077 | const char *sep; | |
6078 | _cleanup_free_ char *esc = NULL; | |
6079 | ||
6080 | sep = strchr(s, '='); | |
baaa35ad ZJS |
6081 | if (!sep) |
6082 | return log_error_errno(SYNTHETIC_ERRNO(EUCLEAN), | |
6083 | "Invalid environment block"); | |
804ee07c ZJS |
6084 | |
6085 | esc = shell_maybe_quote(sep + 1, ESCAPE_POSIX); | |
6086 | if (!esc) | |
6087 | return log_oom(); | |
6088 | ||
6089 | printf("%.*s=%s\n", (int)(sep-s), s, esc); | |
6090 | return 0; | |
6091 | } | |
6092 | ||
e449de87 | 6093 | static int show_environment(int argc, char *argv[], void *userdata) { |
4afd3348 LP |
6094 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
6095 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
f459b602 | 6096 | const char *text; |
4fbd7192 | 6097 | sd_bus *bus; |
7e4249b9 | 6098 | int r; |
7e4249b9 | 6099 | |
4fbd7192 LP |
6100 | r = acquire_bus(BUS_MANAGER, &bus); |
6101 | if (r < 0) | |
6102 | return r; | |
6103 | ||
0221d68a | 6104 | (void) pager_open(arg_pager_flags); |
d2ad7e1b | 6105 | |
f459b602 | 6106 | r = sd_bus_get_property( |
f22f08cd SP |
6107 | bus, |
6108 | "org.freedesktop.systemd1", | |
6109 | "/org/freedesktop/systemd1", | |
f459b602 MAP |
6110 | "org.freedesktop.systemd1.Manager", |
6111 | "Environment", | |
6112 | &error, | |
f22f08cd | 6113 | &reply, |
f459b602 | 6114 | "as"); |
691395d8 LP |
6115 | if (r < 0) |
6116 | return log_error_errno(r, "Failed to get environment: %s", bus_error_message(&error, r)); | |
7e4249b9 | 6117 | |
f459b602 MAP |
6118 | r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "s"); |
6119 | if (r < 0) | |
6120 | return bus_log_parse_error(r); | |
7e4249b9 | 6121 | |
804ee07c ZJS |
6122 | while ((r = sd_bus_message_read_basic(reply, SD_BUS_TYPE_STRING, &text)) > 0) { |
6123 | r = print_variable(text); | |
6124 | if (r < 0) | |
6125 | return r; | |
6126 | } | |
f459b602 MAP |
6127 | if (r < 0) |
6128 | return bus_log_parse_error(r); | |
7e4249b9 | 6129 | |
f459b602 MAP |
6130 | r = sd_bus_message_exit_container(reply); |
6131 | if (r < 0) | |
6132 | return bus_log_parse_error(r); | |
7e4249b9 | 6133 | |
f84190d8 | 6134 | return 0; |
7e4249b9 LP |
6135 | } |
6136 | ||
e449de87 | 6137 | static int switch_root(int argc, char *argv[], void *userdata) { |
4afd3348 | 6138 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
f39d4a08 HH |
6139 | _cleanup_free_ char *cmdline_init = NULL; |
6140 | const char *root, *init; | |
4fbd7192 | 6141 | sd_bus *bus; |
f459b602 | 6142 | int r; |
957eb8ca | 6143 | |
4fbd7192 LP |
6144 | if (arg_transport != BUS_TRANSPORT_LOCAL) { |
6145 | log_error("Cannot switch root remotely."); | |
6146 | return -EINVAL; | |
6147 | } | |
6148 | ||
e449de87 | 6149 | if (argc < 2 || argc > 3) { |
957eb8ca LP |
6150 | log_error("Wrong number of arguments."); |
6151 | return -EINVAL; | |
6152 | } | |
6153 | ||
e449de87 | 6154 | root = argv[1]; |
13068da8 | 6155 | |
e449de87 LP |
6156 | if (argc >= 3) |
6157 | init = argv[2]; | |
13068da8 | 6158 | else { |
01771226 | 6159 | r = proc_cmdline_get_key("init", 0, &cmdline_init); |
f39d4a08 | 6160 | if (r < 0) |
da927ba9 | 6161 | log_debug_errno(r, "Failed to parse /proc/cmdline: %m"); |
13068da8 | 6162 | |
f39d4a08 | 6163 | init = cmdline_init; |
13068da8 | 6164 | } |
f459b602 | 6165 | |
3c6f7c34 | 6166 | init = empty_to_null(init); |
f39d4a08 HH |
6167 | if (init) { |
6168 | const char *root_systemd_path = NULL, *root_init_path = NULL; | |
6169 | ||
63c372cb LP |
6170 | root_systemd_path = strjoina(root, "/" SYSTEMD_BINARY_PATH); |
6171 | root_init_path = strjoina(root, "/", init); | |
f39d4a08 HH |
6172 | |
6173 | /* If the passed init is actually the same as the | |
6174 | * systemd binary, then let's suppress it. */ | |
e3f791a2 | 6175 | if (files_same(root_init_path, root_systemd_path, 0) > 0) |
f39d4a08 HH |
6176 | init = NULL; |
6177 | } | |
13068da8 | 6178 | |
d86e4c97 LP |
6179 | /* Instruct PID1 to exclude us from its killing spree applied during the transition. Otherwise we |
6180 | * would exit with a failure status even though the switch to the new root has succeed. */ | |
6181 | assert(saved_argv); | |
6182 | assert(saved_argv[0]); | |
6183 | saved_argv[0][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", | |
3483460c LP |
6353 | NULL, /* --root= */ |
6354 | NULL, /* verb */ | |
6355 | NULL, /* service */ | |
e735decc LP |
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 | |
b1dffbb9 LP |
6400 | if (!isempty(arg_root)) { |
6401 | q = strappend("--root=", arg_root); | |
6402 | if (!q) | |
6403 | return log_oom(); | |
6404 | ||
6405 | argv[c++] = q; | |
6406 | } | |
ee5762e3 | 6407 | |
0f0467e6 | 6408 | argv[c++] = verb; |
2b6bf07d | 6409 | argv[c++] = basename(p); |
729e3769 | 6410 | argv[c] = NULL; |
ee5762e3 | 6411 | |
729e3769 | 6412 | l = strv_join((char**)argv, " "); |
60731f32 ZJS |
6413 | if (!l) |
6414 | return log_oom(); | |
ee5762e3 | 6415 | |
8ecc68f4 LP |
6416 | if (!arg_quiet) |
6417 | log_info("Executing: %s", l); | |
ee5762e3 | 6418 | |
0672e2c6 | 6419 | j = safe_fork("(sysv-install)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG, &pid); |
4c253ed1 | 6420 | if (j < 0) |
b6e1fff1 | 6421 | return j; |
4c253ed1 | 6422 | if (j == 0) { |
729e3769 | 6423 | /* Child */ |
729e3769 | 6424 | execv(argv[0], (char**) argv); |
cc7cb0ca | 6425 | log_error_errno(errno, "Failed to execute %s: %m", argv[0]); |
729e3769 LP |
6426 | _exit(EXIT_FAILURE); |
6427 | } | |
ee5762e3 | 6428 | |
2e87a1fd | 6429 | j = wait_for_terminate_and_check("sysv-install", pid, WAIT_LOG_ABNORMAL); |
d710aaf7 | 6430 | if (j < 0) |
2e87a1fd LP |
6431 | return j; |
6432 | if (streq(verb, "is-enabled")) { | |
b4a34311 | 6433 | if (j == EXIT_SUCCESS) { |
2e87a1fd LP |
6434 | if (!arg_quiet) |
6435 | puts("enabled"); | |
6436 | r = 1; | |
6437 | } else { | |
6438 | if (!arg_quiet) | |
6439 | puts("disabled"); | |
6440 | } | |
ee5762e3 | 6441 | |
b4a34311 | 6442 | } else if (j != EXIT_SUCCESS) |
2e87a1fd | 6443 | return -EBADE; /* We don't warn here, under the assumption the script already showed an explanation */ |
ee5762e3 | 6444 | |
355ff449 MP |
6445 | if (found_native) |
6446 | continue; | |
6447 | ||
a644abed | 6448 | /* Remove this entry, so that we don't try enabling it as native unit */ |
aba84331 LP |
6449 | assert(f > 0); |
6450 | f--; | |
6451 | assert(args[f] == name); | |
6452 | strv_remove(args, name); | |
729e3769 | 6453 | } |
ee5762e3 | 6454 | |
729e3769 LP |
6455 | #endif |
6456 | return r; | |
6457 | } | |
ee5762e3 | 6458 | |
37370d0c | 6459 | static int mangle_names(char **original_names, char ***mangled_names) { |
a33fdebb | 6460 | char **i, **l, **name; |
7410616c | 6461 | int r; |
37370d0c | 6462 | |
7410616c | 6463 | l = i = new(char*, strv_length(original_names) + 1); |
a33fdebb | 6464 | if (!l) |
37370d0c VP |
6465 | return log_oom(); |
6466 | ||
37370d0c | 6467 | STRV_FOREACH(name, original_names) { |
44386fc1 LN |
6468 | |
6469 | /* When enabling units qualified path names are OK, | |
6470 | * too, hence allow them explicitly. */ | |
6471 | ||
7410616c | 6472 | if (is_path(*name)) { |
44386fc1 | 6473 | *i = strdup(*name); |
7410616c LP |
6474 | if (!*i) { |
6475 | strv_free(l); | |
6476 | return log_oom(); | |
6477 | } | |
6478 | } else { | |
37cbc1d5 | 6479 | r = unit_name_mangle(*name, arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN, i); |
7410616c | 6480 | if (r < 0) { |
523f8cde | 6481 | *i = NULL; |
7410616c LP |
6482 | strv_free(l); |
6483 | return log_error_errno(r, "Failed to mangle unit name: %m"); | |
6484 | } | |
a33fdebb LP |
6485 | } |
6486 | ||
6487 | i++; | |
37370d0c | 6488 | } |
a33fdebb LP |
6489 | |
6490 | *i = NULL; | |
6491 | *mangled_names = l; | |
37370d0c VP |
6492 | |
6493 | return 0; | |
6494 | } | |
6495 | ||
decd7982 JR |
6496 | static int normalize_filenames(char **names) { |
6497 | char **u; | |
6498 | int r; | |
6499 | ||
6500 | STRV_FOREACH(u, names) | |
6501 | if (!path_is_absolute(*u)) { | |
6502 | char* normalized_path; | |
6503 | ||
baaa35ad ZJS |
6504 | if (!isempty(arg_root)) |
6505 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
6506 | "Non-absolute paths are not allowed when --root is used: %s", | |
6507 | *u); | |
decd7982 | 6508 | |
baaa35ad ZJS |
6509 | if (!strchr(*u,'/')) |
6510 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
6511 | "Link argument does contain at least one directory separator: %s", | |
6512 | *u); | |
decd7982 JR |
6513 | |
6514 | r = path_make_absolute_cwd(*u, &normalized_path); | |
6515 | if (r < 0) | |
6516 | return r; | |
6517 | ||
6518 | free_and_replace(*u, normalized_path); | |
6519 | } | |
6520 | ||
6521 | return 0; | |
6522 | } | |
6523 | ||
1d3c86c0 MS |
6524 | static int normalize_names(char **names, bool warn_if_path) { |
6525 | char **u; | |
6526 | bool was_path = false; | |
6527 | ||
6528 | STRV_FOREACH(u, names) { | |
6529 | int r; | |
6530 | ||
6531 | if (!is_path(*u)) | |
6532 | continue; | |
6533 | ||
6534 | r = free_and_strdup(u, basename(*u)); | |
6535 | if (r < 0) | |
6536 | return log_error_errno(r, "Failed to normalize unit file path: %m"); | |
6537 | ||
6538 | was_path = true; | |
6539 | } | |
6540 | ||
6541 | if (warn_if_path && was_path) | |
6542 | log_warning("Warning: Can't execute disable on the unit file path. Proceeding with the unit name."); | |
6543 | ||
6544 | return 0; | |
6545 | } | |
6546 | ||
173471b7 | 6547 | static int unit_exists(LookupPaths *lp, const char *unit) { |
d6568222 | 6548 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
f37f8a61 | 6549 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
d6568222 SS |
6550 | _cleanup_free_ char *path = NULL; |
6551 | static const struct bus_properties_map property_map[] = { | |
f37f8a61 YW |
6552 | { "LoadState", "s", NULL, offsetof(UnitStatusInfo, load_state) }, |
6553 | { "ActiveState", "s", NULL, offsetof(UnitStatusInfo, active_state)}, | |
d6568222 SS |
6554 | {}, |
6555 | }; | |
6556 | UnitStatusInfo info = {}; | |
6557 | sd_bus *bus; | |
6558 | int r; | |
6559 | ||
173471b7 ZJS |
6560 | if (unit_name_is_valid(unit, UNIT_NAME_TEMPLATE)) |
6561 | return unit_find_template_path(unit, lp, NULL, NULL); | |
6562 | ||
d6568222 SS |
6563 | path = unit_dbus_path_from_name(unit); |
6564 | if (!path) | |
6565 | return log_oom(); | |
6566 | ||
6567 | r = acquire_bus(BUS_MANAGER, &bus); | |
6568 | if (r < 0) | |
6569 | return r; | |
6570 | ||
a7e4861c | 6571 | r = bus_map_all_properties(bus, "org.freedesktop.systemd1", path, property_map, 0, &error, &m, &info); |
d6568222 SS |
6572 | if (r < 0) |
6573 | return log_error_errno(r, "Failed to get properties: %s", bus_error_message(&error, r)); | |
6574 | ||
d6568222 SS |
6575 | return !streq_ptr(info.load_state, "not-found") || !streq_ptr(info.active_state, "inactive"); |
6576 | } | |
6577 | ||
e449de87 | 6578 | static int enable_unit(int argc, char *argv[], void *userdata) { |
e3e0314b | 6579 | _cleanup_strv_free_ char **names = NULL; |
e449de87 | 6580 | const char *verb = argv[0]; |
729e3769 | 6581 | UnitFileChange *changes = NULL; |
da6053d0 | 6582 | size_t n_changes = 0; |
729e3769 | 6583 | int carries_install_info = -1; |
35b132e8 | 6584 | bool ignore_carries_install_info = arg_quiet; |
729e3769 | 6585 | int r; |
ee5762e3 | 6586 | |
e449de87 | 6587 | if (!argv[1]) |
ab5919fa MS |
6588 | return 0; |
6589 | ||
e449de87 | 6590 | r = mangle_names(strv_skip(argv, 1), &names); |
3a05c0f9 | 6591 | if (r < 0) |
cbb13b2a VP |
6592 | return r; |
6593 | ||
e3e0314b | 6594 | r = enable_sysv_units(verb, names); |
cbb13b2a VP |
6595 | if (r < 0) |
6596 | return r; | |
3a05c0f9 | 6597 | |
c61b443d | 6598 | /* If the operation was fully executed by the SysV compat, let's finish early */ |
823e5fab FB |
6599 | if (strv_isempty(names)) { |
6600 | if (arg_no_reload || install_client_side()) | |
6601 | return 0; | |
6602 | return daemon_reload(argc, argv, userdata); | |
6603 | } | |
67d66210 | 6604 | |
1d3c86c0 MS |
6605 | if (streq(verb, "disable")) { |
6606 | r = normalize_names(names, true); | |
6607 | if (r < 0) | |
6608 | return r; | |
6609 | } | |
6610 | ||
decd7982 JR |
6611 | if (streq(verb, "link")) { |
6612 | r = normalize_filenames(names); | |
6613 | if (r < 0) | |
6614 | return r; | |
6615 | } | |
6616 | ||
4fbd7192 | 6617 | if (install_client_side()) { |
b3796dd8 JS |
6618 | UnitFileFlags flags; |
6619 | ||
6620 | flags = args_to_flags(); | |
729e3769 | 6621 | if (streq(verb, "enable")) { |
b3796dd8 | 6622 | r = unit_file_enable(arg_scope, flags, arg_root, names, &changes, &n_changes); |
729e3769 LP |
6623 | carries_install_info = r; |
6624 | } else if (streq(verb, "disable")) | |
b3796dd8 | 6625 | r = unit_file_disable(arg_scope, flags, arg_root, names, &changes, &n_changes); |
729e3769 | 6626 | else if (streq(verb, "reenable")) { |
b3796dd8 | 6627 | r = unit_file_reenable(arg_scope, flags, arg_root, names, &changes, &n_changes); |
729e3769 LP |
6628 | carries_install_info = r; |
6629 | } else if (streq(verb, "link")) | |
b3796dd8 | 6630 | r = unit_file_link(arg_scope, flags, arg_root, names, &changes, &n_changes); |
729e3769 | 6631 | else if (streq(verb, "preset")) { |
b3796dd8 | 6632 | r = unit_file_preset(arg_scope, flags, arg_root, names, arg_preset_mode, &changes, &n_changes); |
729e3769 | 6633 | } else if (streq(verb, "mask")) |
b3796dd8 | 6634 | r = unit_file_mask(arg_scope, flags, arg_root, names, &changes, &n_changes); |
729e3769 | 6635 | else if (streq(verb, "unmask")) |
b3796dd8 | 6636 | r = unit_file_unmask(arg_scope, flags, arg_root, names, &changes, &n_changes); |
344ca755 LP |
6637 | else if (streq(verb, "revert")) |
6638 | r = unit_file_revert(arg_scope, arg_root, names, &changes, &n_changes); | |
729e3769 LP |
6639 | else |
6640 | assert_not_reached("Unknown verb"); | |
ee5762e3 | 6641 | |
af3d8113 | 6642 | unit_file_dump_changes(r, verb, changes, n_changes, arg_quiet); |
d073dea0 | 6643 | if (r < 0) |
85b78539 | 6644 | goto finish; |
df77cdf0 | 6645 | r = 0; |
729e3769 | 6646 | } else { |
4afd3348 LP |
6647 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL, *m = NULL; |
6648 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
39207373 | 6649 | bool expect_carries_install_info = false; |
344ca755 | 6650 | bool send_runtime = true, send_force = true, send_preset_mode = false; |
718db961 | 6651 | const char *method; |
4fbd7192 | 6652 | sd_bus *bus; |
729e3769 | 6653 | |
d6568222 | 6654 | if (STR_IN_SET(verb, "mask", "unmask")) { |
f8d6cb48 | 6655 | char **name; |
8e766630 | 6656 | _cleanup_(lookup_paths_free) LookupPaths lp = {}; |
173471b7 ZJS |
6657 | |
6658 | r = lookup_paths_init(&lp, arg_scope, 0, arg_root); | |
d6568222 SS |
6659 | if (r < 0) |
6660 | return r; | |
f8d6cb48 ZJS |
6661 | |
6662 | STRV_FOREACH(name, names) { | |
173471b7 | 6663 | r = unit_exists(&lp, *name); |
f8d6cb48 ZJS |
6664 | if (r < 0) |
6665 | return r; | |
6666 | if (r == 0) | |
25c59b5d | 6667 | log_notice("Unit %s does not exist, proceeding anyway.", *name); |
f8d6cb48 | 6668 | } |
d6568222 SS |
6669 | } |
6670 | ||
4fbd7192 LP |
6671 | r = acquire_bus(BUS_MANAGER, &bus); |
6672 | if (r < 0) | |
6673 | return r; | |
6674 | ||
8a4b13c5 | 6675 | polkit_agent_open_maybe(); |
d2ad7e1b | 6676 | |
729e3769 LP |
6677 | if (streq(verb, "enable")) { |
6678 | method = "EnableUnitFiles"; | |
6679 | expect_carries_install_info = true; | |
6680 | } else if (streq(verb, "disable")) { | |
6681 | method = "DisableUnitFiles"; | |
6682 | send_force = false; | |
6683 | } else if (streq(verb, "reenable")) { | |
6684 | method = "ReenableUnitFiles"; | |
6685 | expect_carries_install_info = true; | |
6686 | } else if (streq(verb, "link")) | |
6687 | method = "LinkUnitFiles"; | |
6688 | else if (streq(verb, "preset")) { | |
d309c1c3 LP |
6689 | |
6690 | if (arg_preset_mode != UNIT_FILE_PRESET_FULL) { | |
6691 | method = "PresetUnitFilesWithMode"; | |
6692 | send_preset_mode = true; | |
6693 | } else | |
6694 | method = "PresetUnitFiles"; | |
6695 | ||
729e3769 | 6696 | expect_carries_install_info = true; |
39207373 | 6697 | ignore_carries_install_info = true; |
729e3769 LP |
6698 | } else if (streq(verb, "mask")) |
6699 | method = "MaskUnitFiles"; | |
6700 | else if (streq(verb, "unmask")) { | |
6701 | method = "UnmaskUnitFiles"; | |
6702 | send_force = false; | |
344ca755 LP |
6703 | } else if (streq(verb, "revert")) { |
6704 | method = "RevertUnitFiles"; | |
6705 | send_runtime = send_force = false; | |
729e3769 LP |
6706 | } else |
6707 | assert_not_reached("Unknown verb"); | |
6708 | ||
f459b602 MAP |
6709 | r = sd_bus_message_new_method_call( |
6710 | bus, | |
151b9b96 | 6711 | &m, |
729e3769 LP |
6712 | "org.freedesktop.systemd1", |
6713 | "/org/freedesktop/systemd1", | |
6714 | "org.freedesktop.systemd1.Manager", | |
151b9b96 | 6715 | method); |
f459b602 MAP |
6716 | if (r < 0) |
6717 | return bus_log_create_error(r); | |
ee5762e3 | 6718 | |
e3e0314b | 6719 | r = sd_bus_message_append_strv(m, names); |
f459b602 MAP |
6720 | if (r < 0) |
6721 | return bus_log_create_error(r); | |
ee5762e3 | 6722 | |
d309c1c3 LP |
6723 | if (send_preset_mode) { |
6724 | r = sd_bus_message_append(m, "s", unit_file_preset_mode_to_string(arg_preset_mode)); | |
6725 | if (r < 0) | |
6726 | return bus_log_create_error(r); | |
6727 | } | |
6728 | ||
344ca755 LP |
6729 | if (send_runtime) { |
6730 | r = sd_bus_message_append(m, "b", arg_runtime); | |
6731 | if (r < 0) | |
6732 | return bus_log_create_error(r); | |
6733 | } | |
ee5762e3 | 6734 | |
729e3769 | 6735 | if (send_force) { |
f459b602 MAP |
6736 | r = sd_bus_message_append(m, "b", arg_force); |
6737 | if (r < 0) | |
6738 | return bus_log_create_error(r); | |
ee5762e3 LP |
6739 | } |
6740 | ||
c49b30a2 | 6741 | r = sd_bus_call(bus, m, 0, &error, &reply); |
691395d8 | 6742 | if (r < 0) |
af3d8113 | 6743 | return log_error_errno(r, "Failed to %s unit: %s", verb, bus_error_message(&error, r)); |
be394c48 | 6744 | |
729e3769 | 6745 | if (expect_carries_install_info) { |
f459b602 MAP |
6746 | r = sd_bus_message_read(reply, "b", &carries_install_info); |
6747 | if (r < 0) | |
6748 | return bus_log_parse_error(r); | |
ee5762e3 LP |
6749 | } |
6750 | ||
57ab2eab | 6751 | r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet, &changes, &n_changes); |
f459b602 | 6752 | if (r < 0) |
85b78539 | 6753 | goto finish; |
b77398f7 | 6754 | |
93c941e3 | 6755 | /* Try to reload if enabled */ |
d6cb60c7 | 6756 | if (!arg_no_reload) |
e449de87 | 6757 | r = daemon_reload(argc, argv, userdata); |
f459b602 MAP |
6758 | else |
6759 | r = 0; | |
b647f10d | 6760 | } |
3d3961f2 | 6761 | |
39207373 | 6762 | if (carries_install_info == 0 && !ignore_carries_install_info) |
46efc978 LP |
6763 | log_notice("The unit files have no installation config (WantedBy=, RequiredBy=, Also=,\n" |
6764 | "Alias= settings in the [Install] section, and DefaultInstance= for template\n" | |
2811184a ZJS |
6765 | "units). This means they are not meant to be enabled using systemctl.\n" |
6766 | " \n" /* trick: the space is needed so that the line does not get stripped from output */ | |
46efc978 | 6767 | "Possible reasons for having this kind of units are:\n" |
18c93ee3 LP |
6768 | "%1$s A unit may be statically enabled by being symlinked from another unit's\n" |
6769 | " .wants/ or .requires/ directory.\n" | |
6770 | "%1$s A unit's purpose may be to act as a helper for some other unit which has\n" | |
6771 | " a requirement dependency on it.\n" | |
6772 | "%1$s A unit may be started when needed via activation (socket, path, timer,\n" | |
6773 | " D-Bus, udev, scripted systemctl call, ...).\n" | |
6774 | "%1$s In case of template units, the unit is meant to be enabled with some\n" | |
6775 | " instance name specified.", | |
9a6f746f | 6776 | special_glyph(SPECIAL_GLYPH_BULLET)); |
ee5762e3 | 6777 | |
6bc30691 | 6778 | if (arg_now && STR_IN_SET(argv[0], "enable", "disable", "mask")) { |
4fbd7192 | 6779 | sd_bus *bus; |
da6053d0 | 6780 | size_t len, i; |
57ab2eab | 6781 | |
4fbd7192 LP |
6782 | r = acquire_bus(BUS_MANAGER, &bus); |
6783 | if (r < 0) | |
d073dea0 | 6784 | goto finish; |
4fbd7192 | 6785 | |
6bc30691 JS |
6786 | len = strv_length(names); |
6787 | { | |
6788 | char *new_args[len + 2]; | |
57ab2eab | 6789 | |
6bc30691 JS |
6790 | new_args[0] = (char*) (streq(argv[0], "enable") ? "start" : "stop"); |
6791 | for (i = 0; i < len; i++) | |
6792 | new_args[i + 1] = basename(names[i]); | |
6793 | new_args[i + 1] = NULL; | |
6794 | ||
6795 | r = start_unit(len + 1, new_args, userdata); | |
6796 | } | |
57ab2eab JS |
6797 | } |
6798 | ||
729e3769 | 6799 | finish: |
729e3769 | 6800 | unit_file_changes_free(changes, n_changes); |
ee5762e3 | 6801 | |
729e3769 | 6802 | return r; |
ee5762e3 LP |
6803 | } |
6804 | ||
e449de87 | 6805 | static int add_dependency(int argc, char *argv[], void *userdata) { |
e94937df LN |
6806 | _cleanup_strv_free_ char **names = NULL; |
6807 | _cleanup_free_ char *target = NULL; | |
e449de87 | 6808 | const char *verb = argv[0]; |
acc0269c | 6809 | UnitFileChange *changes = NULL; |
da6053d0 | 6810 | size_t n_changes = 0; |
e94937df LN |
6811 | UnitDependency dep; |
6812 | int r = 0; | |
6813 | ||
e449de87 | 6814 | if (!argv[1]) |
e94937df LN |
6815 | return 0; |
6816 | ||
37cbc1d5 | 6817 | r = unit_name_mangle_with_suffix(argv[1], arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN, ".target", &target); |
7410616c LP |
6818 | if (r < 0) |
6819 | return log_error_errno(r, "Failed to mangle unit name: %m"); | |
e94937df | 6820 | |
e449de87 | 6821 | r = mangle_names(strv_skip(argv, 2), &names); |
e94937df LN |
6822 | if (r < 0) |
6823 | return r; | |
6824 | ||
6825 | if (streq(verb, "add-wants")) | |
6826 | dep = UNIT_WANTS; | |
6827 | else if (streq(verb, "add-requires")) | |
6828 | dep = UNIT_REQUIRES; | |
6829 | else | |
6830 | assert_not_reached("Unknown verb"); | |
6831 | ||
4fbd7192 | 6832 | if (install_client_side()) { |
b3796dd8 | 6833 | r = unit_file_add_dependency(arg_scope, args_to_flags(), arg_root, names, target, dep, &changes, &n_changes); |
af3d8113 | 6834 | unit_file_dump_changes(r, "add dependency on", changes, n_changes, arg_quiet); |
5f056378 CH |
6835 | |
6836 | if (r > 0) | |
6837 | r = 0; | |
e94937df | 6838 | } else { |
4afd3348 LP |
6839 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL, *m = NULL; |
6840 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
4fbd7192 | 6841 | sd_bus *bus; |
e94937df | 6842 | |
4fbd7192 LP |
6843 | r = acquire_bus(BUS_MANAGER, &bus); |
6844 | if (r < 0) | |
6845 | return r; | |
6846 | ||
8a4b13c5 | 6847 | polkit_agent_open_maybe(); |
d2ad7e1b | 6848 | |
e94937df LN |
6849 | r = sd_bus_message_new_method_call( |
6850 | bus, | |
6851 | &m, | |
6852 | "org.freedesktop.systemd1", | |
6853 | "/org/freedesktop/systemd1", | |
6854 | "org.freedesktop.systemd1.Manager", | |
6855 | "AddDependencyUnitFiles"); | |
6856 | if (r < 0) | |
6857 | return bus_log_create_error(r); | |
6858 | ||
342641fb | 6859 | r = sd_bus_message_append_strv(m, names); |
e94937df LN |
6860 | if (r < 0) |
6861 | return bus_log_create_error(r); | |
6862 | ||
342641fb | 6863 | r = sd_bus_message_append(m, "ssbb", target, unit_dependency_to_string(dep), arg_runtime, arg_force); |
e94937df LN |
6864 | if (r < 0) |
6865 | return bus_log_create_error(r); | |
6866 | ||
6867 | r = sd_bus_call(bus, m, 0, &error, &reply); | |
691395d8 | 6868 | if (r < 0) |
af3d8113 | 6869 | return log_error_errno(r, "Failed to add dependency: %s", bus_error_message(&error, r)); |
e94937df | 6870 | |
acc0269c | 6871 | r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet, &changes, &n_changes); |
e94937df | 6872 | if (r < 0) |
acc0269c | 6873 | goto finish; |
e94937df | 6874 | |
acc0269c CH |
6875 | if (arg_no_reload) { |
6876 | r = 0; | |
6877 | goto finish; | |
6878 | } | |
6879 | ||
6880 | r = daemon_reload(argc, argv, userdata); | |
e94937df | 6881 | } |
acc0269c CH |
6882 | |
6883 | finish: | |
6884 | unit_file_changes_free(changes, n_changes); | |
6885 | ||
6886 | return r; | |
e94937df LN |
6887 | } |
6888 | ||
e449de87 | 6889 | static int preset_all(int argc, char *argv[], void *userdata) { |
acc0269c | 6890 | UnitFileChange *changes = NULL; |
da6053d0 | 6891 | size_t n_changes = 0; |
d309c1c3 LP |
6892 | int r; |
6893 | ||
4fbd7192 | 6894 | if (install_client_side()) { |
b3796dd8 | 6895 | r = unit_file_preset_all(arg_scope, args_to_flags(), arg_root, arg_preset_mode, &changes, &n_changes); |
af3d8113 | 6896 | unit_file_dump_changes(r, "preset", changes, n_changes, arg_quiet); |
5f056378 CH |
6897 | |
6898 | if (r > 0) | |
6899 | r = 0; | |
d309c1c3 | 6900 | } else { |
4afd3348 LP |
6901 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
6902 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
4fbd7192 | 6903 | sd_bus *bus; |
d309c1c3 | 6904 | |
4fbd7192 LP |
6905 | r = acquire_bus(BUS_MANAGER, &bus); |
6906 | if (r < 0) | |
6907 | return r; | |
6908 | ||
8a4b13c5 | 6909 | polkit_agent_open_maybe(); |
d2ad7e1b | 6910 | |
6e646d22 | 6911 | r = sd_bus_call_method( |
d309c1c3 LP |
6912 | bus, |
6913 | "org.freedesktop.systemd1", | |
6914 | "/org/freedesktop/systemd1", | |
6915 | "org.freedesktop.systemd1.Manager", | |
6e646d22 LP |
6916 | "PresetAllUnitFiles", |
6917 | &error, | |
6918 | &reply, | |
d309c1c3 LP |
6919 | "sbb", |
6920 | unit_file_preset_mode_to_string(arg_preset_mode), | |
6921 | arg_runtime, | |
6922 | arg_force); | |
691395d8 | 6923 | if (r < 0) |
af3d8113 | 6924 | return log_error_errno(r, "Failed to preset all units: %s", bus_error_message(&error, r)); |
d309c1c3 | 6925 | |
acc0269c | 6926 | r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet, &changes, &n_changes); |
d309c1c3 | 6927 | if (r < 0) |
acc0269c | 6928 | goto finish; |
d309c1c3 | 6929 | |
acc0269c CH |
6930 | if (arg_no_reload) { |
6931 | r = 0; | |
6932 | goto finish; | |
6933 | } | |
6934 | ||
6935 | r = daemon_reload(argc, argv, userdata); | |
d309c1c3 | 6936 | } |
acc0269c CH |
6937 | |
6938 | finish: | |
6939 | unit_file_changes_free(changes, n_changes); | |
6940 | ||
6941 | return r; | |
d309c1c3 LP |
6942 | } |
6943 | ||
3b3557c4 JS |
6944 | static int show_installation_targets_client_side(const char *name) { |
6945 | UnitFileChange *changes = NULL; | |
da6053d0 | 6946 | size_t n_changes = 0, i; |
3b3557c4 JS |
6947 | UnitFileFlags flags; |
6948 | char **p; | |
6949 | int r; | |
6950 | ||
6951 | p = STRV_MAKE(name); | |
6952 | flags = UNIT_FILE_DRY_RUN | | |
6953 | (arg_runtime ? UNIT_FILE_RUNTIME : 0); | |
6954 | ||
6955 | r = unit_file_disable(UNIT_FILE_SYSTEM, flags, NULL, p, &changes, &n_changes); | |
6956 | if (r < 0) | |
6957 | return log_error_errno(r, "Failed to get file links for %s: %m", name); | |
6958 | ||
6959 | for (i = 0; i < n_changes; i++) | |
6960 | if (changes[i].type == UNIT_FILE_UNLINK) | |
6961 | printf(" %s\n", changes[i].path); | |
6962 | ||
6963 | return 0; | |
6964 | } | |
6965 | ||
6966 | static int show_installation_targets(sd_bus *bus, const char *name) { | |
6967 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
6968 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
6969 | const char *link; | |
6970 | int r; | |
6971 | ||
6972 | r = sd_bus_call_method( | |
6973 | bus, | |
6974 | "org.freedesktop.systemd1", | |
6975 | "/org/freedesktop/systemd1", | |
6976 | "org.freedesktop.systemd1.Manager", | |
6977 | "GetUnitFileLinks", | |
6978 | &error, | |
6979 | &reply, | |
6980 | "sb", name, arg_runtime); | |
6981 | if (r < 0) | |
6982 | return log_error_errno(r, "Failed to get unit file links for %s: %s", name, bus_error_message(&error, r)); | |
6983 | ||
6984 | r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "s"); | |
6985 | if (r < 0) | |
6986 | return bus_log_parse_error(r); | |
6987 | ||
6988 | while ((r = sd_bus_message_read(reply, "s", &link)) > 0) | |
6989 | printf(" %s\n", link); | |
6990 | ||
6991 | if (r < 0) | |
6992 | return bus_log_parse_error(r); | |
6993 | ||
6994 | r = sd_bus_message_exit_container(reply); | |
6995 | if (r < 0) | |
6996 | return bus_log_parse_error(r); | |
6997 | ||
6998 | return 0; | |
6999 | } | |
7000 | ||
e449de87 | 7001 | static int unit_is_enabled(int argc, char *argv[], void *userdata) { |
e3e0314b | 7002 | _cleanup_strv_free_ char **names = NULL; |
729e3769 LP |
7003 | bool enabled; |
7004 | char **name; | |
f459b602 | 7005 | int r; |
ee5762e3 | 7006 | |
e449de87 | 7007 | r = mangle_names(strv_skip(argv, 1), &names); |
cbb13b2a VP |
7008 | if (r < 0) |
7009 | return r; | |
7010 | ||
e449de87 | 7011 | r = enable_sysv_units(argv[0], names); |
729e3769 LP |
7012 | if (r < 0) |
7013 | return r; | |
ee5762e3 | 7014 | |
729e3769 | 7015 | enabled = r > 0; |
ee5762e3 | 7016 | |
4fbd7192 | 7017 | if (install_client_side()) { |
e3e0314b | 7018 | STRV_FOREACH(name, names) { |
729e3769 | 7019 | UnitFileState state; |
ee5762e3 | 7020 | |
0ec0deaa LP |
7021 | r = unit_file_get_state(arg_scope, arg_root, *name, &state); |
7022 | if (r < 0) | |
c5024cd0 | 7023 | return log_error_errno(r, "Failed to get unit file state for %s: %m", *name); |
ee5762e3 | 7024 | |
4c315c2c IS |
7025 | if (IN_SET(state, |
7026 | UNIT_FILE_ENABLED, | |
7027 | UNIT_FILE_ENABLED_RUNTIME, | |
7028 | UNIT_FILE_STATIC, | |
f4139308 LP |
7029 | UNIT_FILE_INDIRECT, |
7030 | UNIT_FILE_GENERATED)) | |
729e3769 LP |
7031 | enabled = true; |
7032 | ||
3b3557c4 | 7033 | if (!arg_quiet) { |
729e3769 | 7034 | puts(unit_file_state_to_string(state)); |
3b3557c4 JS |
7035 | if (arg_full) { |
7036 | r = show_installation_targets_client_side(*name); | |
7037 | if (r < 0) | |
7038 | return r; | |
7039 | } | |
7040 | } | |
71fad675 | 7041 | } |
ee5762e3 | 7042 | |
5f056378 | 7043 | r = 0; |
729e3769 | 7044 | } else { |
4afd3348 | 7045 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
4fbd7192 LP |
7046 | sd_bus *bus; |
7047 | ||
7048 | r = acquire_bus(BUS_MANAGER, &bus); | |
7049 | if (r < 0) | |
7050 | return r; | |
7051 | ||
e3e0314b | 7052 | STRV_FOREACH(name, names) { |
4afd3348 | 7053 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
729e3769 | 7054 | const char *s; |
63a723f3 | 7055 | |
f459b602 | 7056 | r = sd_bus_call_method( |
f22f08cd | 7057 | bus, |
729e3769 LP |
7058 | "org.freedesktop.systemd1", |
7059 | "/org/freedesktop/systemd1", | |
7060 | "org.freedesktop.systemd1.Manager", | |
f22f08cd | 7061 | "GetUnitFileState", |
f459b602 | 7062 | &error, |
f22f08cd | 7063 | &reply, |
04504f93 | 7064 | "s", *name); |
691395d8 LP |
7065 | if (r < 0) |
7066 | return log_error_errno(r, "Failed to get unit file state for %s: %s", *name, bus_error_message(&error, r)); | |
ee5762e3 | 7067 | |
f459b602 MAP |
7068 | r = sd_bus_message_read(reply, "s", &s); |
7069 | if (r < 0) | |
7070 | return bus_log_parse_error(r); | |
ee5762e3 | 7071 | |
f4139308 | 7072 | if (STR_IN_SET(s, "enabled", "enabled-runtime", "static", "indirect", "generated")) |
729e3769 LP |
7073 | enabled = true; |
7074 | ||
3b3557c4 | 7075 | if (!arg_quiet) { |
729e3769 | 7076 | puts(s); |
3b3557c4 JS |
7077 | if (arg_full) { |
7078 | r = show_installation_targets(bus, *name); | |
7079 | if (r < 0) | |
7080 | return r; | |
7081 | } | |
7082 | } | |
560d8f23 | 7083 | } |
ee5762e3 LP |
7084 | } |
7085 | ||
f4139308 | 7086 | return enabled ? EXIT_SUCCESS : EXIT_FAILURE; |
ee5762e3 LP |
7087 | } |
7088 | ||
adb6cd9b FB |
7089 | static int match_startup_finished(sd_bus_message *m, void *userdata, sd_bus_error *error) { |
7090 | char **state = userdata; | |
7091 | int r; | |
7092 | ||
7093 | assert(state); | |
7094 | ||
7095 | r = sd_bus_get_property_string( | |
7096 | sd_bus_message_get_bus(m), | |
7097 | "org.freedesktop.systemd1", | |
7098 | "/org/freedesktop/systemd1", | |
7099 | "org.freedesktop.systemd1.Manager", | |
7100 | "SystemState", | |
7101 | NULL, | |
7102 | state); | |
7103 | ||
7104 | sd_event_exit(sd_bus_get_event(sd_bus_message_get_bus(m)), r); | |
7105 | return 0; | |
7106 | } | |
7107 | ||
e449de87 | 7108 | static int is_system_running(int argc, char *argv[], void *userdata) { |
e686a616 | 7109 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
adb6cd9b FB |
7110 | _cleanup_(sd_bus_slot_unrefp) sd_bus_slot *slot_startup_finished = NULL; |
7111 | _cleanup_(sd_event_unrefp) sd_event* event = NULL; | |
99813a19 | 7112 | _cleanup_free_ char *state = NULL; |
4fbd7192 | 7113 | sd_bus *bus; |
99813a19 LP |
7114 | int r; |
7115 | ||
040524b4 | 7116 | if (running_in_chroot() > 0 || (arg_transport == BUS_TRANSPORT_LOCAL && !sd_booted())) { |
94f099d8 LP |
7117 | if (!arg_quiet) |
7118 | puts("offline"); | |
7119 | return EXIT_FAILURE; | |
7120 | } | |
7121 | ||
4fbd7192 LP |
7122 | r = acquire_bus(BUS_MANAGER, &bus); |
7123 | if (r < 0) | |
7124 | return r; | |
7125 | ||
adb6cd9b FB |
7126 | if (arg_wait) { |
7127 | r = sd_event_default(&event); | |
7128 | if (r >= 0) | |
7129 | r = sd_bus_attach_event(bus, event, 0); | |
7130 | if (r >= 0) | |
7131 | r = sd_bus_match_signal_async( | |
7132 | bus, | |
7133 | &slot_startup_finished, | |
7134 | "org.freedesktop.systemd1", | |
7135 | "/org/freedesktop/systemd1", | |
7136 | "org.freedesktop.systemd1.Manager", | |
7137 | "StartupFinished", | |
7138 | match_startup_finished, NULL, &state); | |
7139 | if (r < 0) { | |
7140 | log_warning_errno(r, "Failed to request match for StartupFinished: %m"); | |
7141 | arg_wait = false; | |
7142 | } | |
7143 | } | |
7144 | ||
99813a19 LP |
7145 | r = sd_bus_get_property_string( |
7146 | bus, | |
7147 | "org.freedesktop.systemd1", | |
7148 | "/org/freedesktop/systemd1", | |
7149 | "org.freedesktop.systemd1.Manager", | |
7150 | "SystemState", | |
e686a616 | 7151 | &error, |
99813a19 LP |
7152 | &state); |
7153 | if (r < 0) { | |
adb6cd9b | 7154 | log_warning_errno(r, "Failed to query system state: %s", bus_error_message(&error, r)); |
e686a616 | 7155 | |
99813a19 LP |
7156 | if (!arg_quiet) |
7157 | puts("unknown"); | |
bfeec178 | 7158 | return EXIT_FAILURE; |
99813a19 LP |
7159 | } |
7160 | ||
adb6cd9b FB |
7161 | if (arg_wait && STR_IN_SET(state, "initializing", "starting")) { |
7162 | r = sd_event_loop(event); | |
7163 | if (r < 0) { | |
7164 | log_warning_errno(r, "Failed to get property from event loop: %m"); | |
7165 | if (!arg_quiet) | |
7166 | puts("unknown"); | |
7167 | return EXIT_FAILURE; | |
7168 | } | |
7169 | } | |
7170 | ||
99813a19 LP |
7171 | if (!arg_quiet) |
7172 | puts(state); | |
7173 | ||
7174 | return streq(state, "running") ? EXIT_SUCCESS : EXIT_FAILURE; | |
7175 | } | |
7176 | ||
7d4fb3b1 | 7177 | static int create_edit_temp_file(const char *new_path, const char *original_path, char **ret_tmp_fn) { |
45519fd6 | 7178 | _cleanup_free_ char *t = NULL; |
ae6c3cc0 | 7179 | int r; |
7d4fb3b1 RC |
7180 | |
7181 | assert(new_path); | |
7182 | assert(original_path); | |
7183 | assert(ret_tmp_fn); | |
7184 | ||
14bcf25c | 7185 | r = tempfn_random(new_path, NULL, &t); |
ae6c3cc0 | 7186 | if (r < 0) |
029009d4 | 7187 | return log_error_errno(r, "Failed to determine temporary filename for \"%s\": %m", new_path); |
7d4fb3b1 RC |
7188 | |
7189 | r = mkdir_parents(new_path, 0755); | |
45519fd6 | 7190 | if (r < 0) |
691395d8 | 7191 | return log_error_errno(r, "Failed to create directories for \"%s\": %m", new_path); |
7d4fb3b1 | 7192 | |
1c876927 | 7193 | r = copy_file(original_path, t, 0, 0644, 0, COPY_REFLINK); |
7d4fb3b1 | 7194 | if (r == -ENOENT) { |
45519fd6 | 7195 | |
7d4fb3b1 | 7196 | r = touch(t); |
45519fd6 LP |
7197 | if (r < 0) |
7198 | return log_error_errno(r, "Failed to create temporary file \"%s\": %m", t); | |
7199 | ||
7200 | } else if (r < 0) | |
39c38ce1 | 7201 | return log_error_errno(r, "Failed to create temporary file for \"%s\": %m", new_path); |
7d4fb3b1 | 7202 | |
ae2a15bc | 7203 | *ret_tmp_fn = TAKE_PTR(t); |
7d4fb3b1 RC |
7204 | |
7205 | return 0; | |
7206 | } | |
7207 | ||
c51932be LP |
7208 | static int get_file_to_edit( |
7209 | const LookupPaths *paths, | |
7210 | const char *name, | |
7211 | char **ret_path) { | |
7212 | ||
7213 | _cleanup_free_ char *path = NULL, *run = NULL; | |
7d4fb3b1 | 7214 | |
45519fd6 LP |
7215 | assert(name); |
7216 | assert(ret_path); | |
7217 | ||
605405c6 | 7218 | path = strjoin(paths->persistent_config, "/", name); |
c51932be | 7219 | if (!path) |
7d4fb3b1 RC |
7220 | return log_oom(); |
7221 | ||
c51932be | 7222 | if (arg_runtime) { |
605405c6 | 7223 | run = strjoin(paths->runtime_config, "/", name); |
c51932be LP |
7224 | if (!run) |
7225 | return log_oom(); | |
7226 | } | |
7227 | ||
bc854dc7 | 7228 | if (arg_runtime) { |
baaa35ad ZJS |
7229 | if (access(path, F_OK) >= 0) |
7230 | return log_error_errno(SYNTHETIC_ERRNO(EEXIST), | |
7231 | "Refusing to create \"%s\" because it would be overridden by \"%s\" anyway.", | |
7232 | run, path); | |
691395d8 | 7233 | |
ae2a15bc LP |
7234 | *ret_path = TAKE_PTR(run); |
7235 | } else | |
7236 | *ret_path = TAKE_PTR(path); | |
7d4fb3b1 RC |
7237 | |
7238 | return 0; | |
7239 | } | |
7240 | ||
39c38ce1 | 7241 | static int unit_file_create_new( |
c51932be LP |
7242 | const LookupPaths *paths, |
7243 | const char *unit_name, | |
39c38ce1 | 7244 | const char *suffix, |
c51932be LP |
7245 | char **ret_new_path, |
7246 | char **ret_tmp_path) { | |
7247 | ||
919d2720 LP |
7248 | _cleanup_free_ char *new_path = NULL, *tmp_path = NULL; |
7249 | const char *ending; | |
7d4fb3b1 RC |
7250 | int r; |
7251 | ||
7252 | assert(unit_name); | |
7253 | assert(ret_new_path); | |
7254 | assert(ret_tmp_path); | |
7255 | ||
39c38ce1 | 7256 | ending = strjoina(unit_name, suffix); |
919d2720 | 7257 | r = get_file_to_edit(paths, ending, &new_path); |
7d4fb3b1 RC |
7258 | if (r < 0) |
7259 | return r; | |
7260 | ||
919d2720 LP |
7261 | r = create_edit_temp_file(new_path, new_path, &tmp_path); |
7262 | if (r < 0) | |
7d4fb3b1 | 7263 | return r; |
7d4fb3b1 | 7264 | |
919d2720 LP |
7265 | *ret_new_path = TAKE_PTR(new_path); |
7266 | *ret_tmp_path = TAKE_PTR(tmp_path); | |
7d4fb3b1 RC |
7267 | |
7268 | return 0; | |
7269 | } | |
7270 | ||
1cfa9a4c | 7271 | static int unit_file_create_copy( |
c51932be | 7272 | const LookupPaths *paths, |
1cfa9a4c LP |
7273 | const char *unit_name, |
7274 | const char *fragment_path, | |
1cfa9a4c LP |
7275 | char **ret_new_path, |
7276 | char **ret_tmp_path) { | |
7277 | ||
92d6b0bf | 7278 | _cleanup_free_ char *new_path = NULL, *tmp_path = NULL; |
7d4fb3b1 RC |
7279 | int r; |
7280 | ||
7281 | assert(fragment_path); | |
7282 | assert(unit_name); | |
7283 | assert(ret_new_path); | |
7284 | assert(ret_tmp_path); | |
7285 | ||
92d6b0bf | 7286 | r = get_file_to_edit(paths, unit_name, &new_path); |
7d4fb3b1 RC |
7287 | if (r < 0) |
7288 | return r; | |
7289 | ||
92d6b0bf | 7290 | if (!path_equal(fragment_path, new_path) && access(new_path, F_OK) >= 0) { |
7d4fb3b1 RC |
7291 | char response; |
7292 | ||
92d6b0bf LP |
7293 | r = ask_char(&response, "yn", "\"%s\" already exists. Overwrite with \"%s\"? [(y)es, (n)o] ", new_path, fragment_path); |
7294 | if (r < 0) | |
7d4fb3b1 | 7295 | return r; |
5daacba2 LP |
7296 | if (response != 'y') |
7297 | return log_warning_errno(SYNTHETIC_ERRNO(EKEYREJECTED), "%s skipped.", unit_name); | |
7d4fb3b1 RC |
7298 | } |
7299 | ||
92d6b0bf LP |
7300 | r = create_edit_temp_file(new_path, fragment_path, &tmp_path); |
7301 | if (r < 0) | |
7d4fb3b1 | 7302 | return r; |
7d4fb3b1 | 7303 | |
92d6b0bf LP |
7304 | *ret_new_path = TAKE_PTR(new_path); |
7305 | *ret_tmp_path = TAKE_PTR(tmp_path); | |
7d4fb3b1 RC |
7306 | |
7307 | return 0; | |
7308 | } | |
7309 | ||
7310 | static int run_editor(char **paths) { | |
7d4fb3b1 RC |
7311 | int r; |
7312 | ||
7313 | assert(paths); | |
7314 | ||
0672e2c6 | 7315 | r = safe_fork("(editor)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG|FORK_WAIT, NULL); |
4c253ed1 | 7316 | if (r < 0) |
b6e1fff1 | 7317 | return r; |
4c253ed1 | 7318 | if (r == 0) { |
b2238e38 | 7319 | char **editor_args = NULL, **tmp_path, **original_path; |
70d6e5bd | 7320 | size_t n_editor_args = 0, i = 1, argc; |
b2238e38 | 7321 | const char **args, *editor, *p; |
7d4fb3b1 RC |
7322 | |
7323 | argc = strv_length(paths)/2 + 1; | |
7d4fb3b1 RC |
7324 | |
7325 | /* SYSTEMD_EDITOR takes precedence over EDITOR which takes precedence over VISUAL | |
7326 | * If neither SYSTEMD_EDITOR nor EDITOR nor VISUAL are present, | |
7327 | * we try to execute well known editors | |
7328 | */ | |
7329 | editor = getenv("SYSTEMD_EDITOR"); | |
7330 | if (!editor) | |
7331 | editor = getenv("EDITOR"); | |
7332 | if (!editor) | |
7333 | editor = getenv("VISUAL"); | |
7334 | ||
7335 | if (!isempty(editor)) { | |
9ef5d8f2 JS |
7336 | editor_args = strv_split(editor, WHITESPACE); |
7337 | if (!editor_args) { | |
7338 | (void) log_oom(); | |
7339 | _exit(EXIT_FAILURE); | |
7340 | } | |
7341 | n_editor_args = strv_length(editor_args); | |
7342 | argc += n_editor_args - 1; | |
7343 | } | |
02a126a3 | 7344 | |
9ef5d8f2 JS |
7345 | args = newa(const char*, argc + 1); |
7346 | ||
7347 | if (n_editor_args > 0) { | |
7348 | args[0] = editor_args[0]; | |
7349 | for (; i < n_editor_args; i++) | |
7350 | args[i] = editor_args[i]; | |
7351 | } | |
7352 | ||
df79fdab LP |
7353 | STRV_FOREACH_PAIR(original_path, tmp_path, paths) |
7354 | args[i++] = *tmp_path; | |
9ef5d8f2 JS |
7355 | args[i] = NULL; |
7356 | ||
7357 | if (n_editor_args > 0) | |
7358 | execvp(args[0], (char* const*) args); | |
7d4fb3b1 | 7359 | |
9391a1c3 | 7360 | FOREACH_STRING(p, "editor", "nano", "vim", "vi") { |
1cfa9a4c LP |
7361 | args[0] = p; |
7362 | execvp(p, (char* const*) args); | |
7d4fb3b1 RC |
7363 | /* We do not fail if the editor doesn't exist |
7364 | * because we want to try each one of them before | |
7365 | * failing. | |
7366 | */ | |
7367 | if (errno != ENOENT) { | |
691395d8 | 7368 | log_error_errno(errno, "Failed to execute %s: %m", editor); |
7d4fb3b1 RC |
7369 | _exit(EXIT_FAILURE); |
7370 | } | |
7371 | } | |
7372 | ||
1cfa9a4c | 7373 | log_error("Cannot edit unit(s), no editor available. Please set either $SYSTEMD_EDITOR, $EDITOR or $VISUAL."); |
7d4fb3b1 RC |
7374 | _exit(EXIT_FAILURE); |
7375 | } | |
7376 | ||
45519fd6 | 7377 | return 0; |
7d4fb3b1 RC |
7378 | } |
7379 | ||
7380 | static int find_paths_to_edit(sd_bus *bus, char **names, char ***paths) { | |
8e766630 | 7381 | _cleanup_(lookup_paths_free) LookupPaths lp = {}; |
7d4fb3b1 RC |
7382 | char **name; |
7383 | int r; | |
7384 | ||
7385 | assert(names); | |
7386 | assert(paths); | |
7387 | ||
4943d143 | 7388 | r = lookup_paths_init(&lp, arg_scope, 0, arg_root); |
5b013a2f | 7389 | if (r < 0) |
8df18507 | 7390 | return r; |
7d4fb3b1 | 7391 | |
e9e310f8 | 7392 | STRV_FOREACH(name, names) { |
b74df547 ZJS |
7393 | _cleanup_free_ char *path = NULL, *new_path = NULL, *tmp_path = NULL, *tmp_name = NULL; |
7394 | const char *unit_name; | |
7d4fb3b1 | 7395 | |
02a126a3 LP |
7396 | r = unit_find_paths(bus, *name, &lp, false, &path, NULL); |
7397 | if (r == -EKEYREJECTED) { | |
7398 | /* If loading of the unit failed server side complete, then the server won't tell us the unit | |
7399 | * file path. In that case, find the file client side. */ | |
7400 | log_debug_errno(r, "Unit '%s' was not loaded correctly, retrying client-side.", *name); | |
7401 | r = unit_find_paths(bus, *name, &lp, true, &path, NULL); | |
7402 | } | |
7403 | if (r == -ERFKILL) | |
7404 | return log_error_errno(r, "Unit '%s' masked, cannot edit.", *name); | |
e9e310f8 RC |
7405 | if (r < 0) |
7406 | return r; | |
b3734d98 ZJS |
7407 | |
7408 | if (r == 0) { | |
7409 | assert(!path); | |
7410 | ||
7411 | if (!arg_force) { | |
85163756 LP |
7412 | log_info("Run 'systemctl edit%s --force --full %s' to create a new unit.", |
7413 | arg_scope == UNIT_FILE_GLOBAL ? " --global" : | |
7414 | arg_scope == UNIT_FILE_USER ? " --user" : "", | |
7415 | *name); | |
39c38ce1 DC |
7416 | return -ENOENT; |
7417 | } | |
39c38ce1 | 7418 | |
b3734d98 ZJS |
7419 | /* Create a new unit from scratch */ |
7420 | unit_name = *name; | |
7421 | r = unit_file_create_new(&lp, unit_name, | |
7422 | arg_full ? NULL : ".d/override.conf", | |
7423 | &new_path, &tmp_path); | |
7424 | } else { | |
7425 | assert(path); | |
b74df547 | 7426 | |
b3734d98 ZJS |
7427 | unit_name = basename(path); |
7428 | /* We follow unit aliases, but we need to propagate the instance */ | |
7429 | if (unit_name_is_valid(*name, UNIT_NAME_INSTANCE) && | |
7430 | unit_name_is_valid(unit_name, UNIT_NAME_TEMPLATE)) { | |
7431 | _cleanup_free_ char *instance = NULL; | |
b74df547 | 7432 | |
b3734d98 ZJS |
7433 | r = unit_name_to_instance(*name, &instance); |
7434 | if (r < 0) | |
7435 | return r; | |
b74df547 | 7436 | |
b3734d98 ZJS |
7437 | r = unit_name_replace_instance(unit_name, instance, &tmp_name); |
7438 | if (r < 0) | |
7439 | return r; | |
7440 | ||
7441 | unit_name = tmp_name; | |
7442 | } | |
b74df547 | 7443 | |
39c38ce1 | 7444 | if (arg_full) |
b74df547 | 7445 | r = unit_file_create_copy(&lp, unit_name, path, &new_path, &tmp_path); |
39c38ce1 | 7446 | else |
b74df547 | 7447 | r = unit_file_create_new(&lp, unit_name, ".d/override.conf", &new_path, &tmp_path); |
b3734d98 | 7448 | } |
e9e310f8 | 7449 | if (r < 0) |
ad2a0358 | 7450 | return r; |
7d4fb3b1 | 7451 | |
e9e310f8 RC |
7452 | r = strv_push_pair(paths, new_path, tmp_path); |
7453 | if (r < 0) | |
7454 | return log_oom(); | |
02a126a3 | 7455 | |
78df0edc | 7456 | new_path = tmp_path = NULL; |
7d4fb3b1 RC |
7457 | } |
7458 | ||
7459 | return 0; | |
7460 | } | |
7461 | ||
e449de87 | 7462 | static int edit(int argc, char *argv[], void *userdata) { |
86f004fb | 7463 | _cleanup_(lookup_paths_free) LookupPaths lp = {}; |
7d4fb3b1 RC |
7464 | _cleanup_strv_free_ char **names = NULL; |
7465 | _cleanup_strv_free_ char **paths = NULL; | |
7466 | char **original, **tmp; | |
4fbd7192 | 7467 | sd_bus *bus; |
7d4fb3b1 RC |
7468 | int r; |
7469 | ||
7d4fb3b1 | 7470 | if (!on_tty()) { |
4fbd7192 | 7471 | log_error("Cannot edit units if not on a tty."); |
7d4fb3b1 RC |
7472 | return -EINVAL; |
7473 | } | |
7474 | ||
7475 | if (arg_transport != BUS_TRANSPORT_LOCAL) { | |
4fbd7192 | 7476 | log_error("Cannot edit units remotely."); |
7d4fb3b1 RC |
7477 | return -EINVAL; |
7478 | } | |
7479 | ||
86f004fb ZJS |
7480 | r = lookup_paths_init(&lp, arg_scope, 0, arg_root); |
7481 | if (r < 0) | |
7482 | return log_error_errno(r, "Failed to determine unit paths: %m"); | |
7483 | ||
4fbd7192 LP |
7484 | r = acquire_bus(BUS_MANAGER, &bus); |
7485 | if (r < 0) | |
7486 | return r; | |
7487 | ||
e449de87 | 7488 | r = expand_names(bus, strv_skip(argv, 1), NULL, &names); |
7d4fb3b1 RC |
7489 | if (r < 0) |
7490 | return log_error_errno(r, "Failed to expand names: %m"); | |
7491 | ||
9d9dd746 | 7492 | STRV_FOREACH(tmp, names) { |
86f004fb | 7493 | r = unit_is_masked(bus, &lp, *tmp); |
9d9dd746 ZJS |
7494 | if (r < 0) |
7495 | return r; | |
9d9dd746 ZJS |
7496 | if (r > 0) { |
7497 | log_error("Cannot edit %s: unit is masked.", *tmp); | |
7498 | return -EINVAL; | |
7499 | } | |
7500 | } | |
7501 | ||
7d4fb3b1 RC |
7502 | r = find_paths_to_edit(bus, names, &paths); |
7503 | if (r < 0) | |
7504 | return r; | |
7505 | ||
b5e6a600 | 7506 | if (strv_isempty(paths)) |
7d4fb3b1 | 7507 | return -ENOENT; |
7d4fb3b1 RC |
7508 | |
7509 | r = run_editor(paths); | |
7510 | if (r < 0) | |
7511 | goto end; | |
7512 | ||
7513 | STRV_FOREACH_PAIR(original, tmp, paths) { | |
9d9dd746 ZJS |
7514 | /* If the temporary file is empty we ignore it. |
7515 | * This allows the user to cancel the modification. | |
7d4fb3b1 RC |
7516 | */ |
7517 | if (null_or_empty_path(*tmp)) { | |
45519fd6 | 7518 | log_warning("Editing \"%s\" canceled: temporary file is empty.", *original); |
7d4fb3b1 RC |
7519 | continue; |
7520 | } | |
45519fd6 | 7521 | |
7d4fb3b1 RC |
7522 | r = rename(*tmp, *original); |
7523 | if (r < 0) { | |
029009d4 | 7524 | r = log_error_errno(errno, "Failed to rename \"%s\" to \"%s\": %m", *tmp, *original); |
7d4fb3b1 RC |
7525 | goto end; |
7526 | } | |
7527 | } | |
7528 | ||
45519fd6 LP |
7529 | r = 0; |
7530 | ||
7531 | if (!arg_no_reload && !install_client_side()) | |
e449de87 | 7532 | r = daemon_reload(argc, argv, userdata); |
7d4fb3b1 RC |
7533 | |
7534 | end: | |
5f18271e | 7535 | STRV_FOREACH_PAIR(original, tmp, paths) { |
45519fd6 | 7536 | (void) unlink(*tmp); |
7d4fb3b1 | 7537 | |
5f18271e RC |
7538 | /* Removing empty dropin dirs */ |
7539 | if (!arg_full) { | |
043717f9 RC |
7540 | _cleanup_free_ char *dir; |
7541 | ||
7542 | dir = dirname_malloc(*original); | |
7543 | if (!dir) | |
7544 | return log_oom(); | |
7545 | ||
5f18271e RC |
7546 | /* no need to check if the dir is empty, rmdir |
7547 | * does nothing if it is not the case. | |
7548 | */ | |
7549 | (void) rmdir(dir); | |
7550 | } | |
7551 | } | |
7552 | ||
7d4fb3b1 RC |
7553 | return r; |
7554 | } | |
7555 | ||
37ec0fdd LP |
7556 | static int systemctl_help(void) { |
7557 | _cleanup_free_ char *link = NULL; | |
7558 | int r; | |
7559 | ||
0221d68a | 7560 | (void) pager_open(arg_pager_flags); |
729e3769 | 7561 | |
37ec0fdd LP |
7562 | r = terminal_urlify_man("systemctl", "1", &link); |
7563 | if (r < 0) | |
7564 | return log_oom(); | |
7565 | ||
2e33c433 | 7566 | printf("%s [OPTIONS...] {COMMAND} ...\n\n" |
729e3769 | 7567 | "Query or send control commands to the systemd manager.\n\n" |
8a0867d6 LP |
7568 | " -h --help Show this help\n" |
7569 | " --version Show package version\n" | |
f459b602 MAP |
7570 | " --system Connect to system manager\n" |
7571 | " --user Connect to user service manager\n" | |
7572 | " -H --host=[USER@]HOST\n" | |
7573 | " Operate on remote host\n" | |
7574 | " -M --machine=CONTAINER\n" | |
7575 | " Operate on local container\n" | |
3fb90db2 ZJS |
7576 | " -t --type=TYPE List units of a particular type\n" |
7577 | " --state=STATE List units with particular LOAD or SUB or ACTIVE state\n" | |
8a0867d6 | 7578 | " -p --property=NAME Show only properties by this name\n" |
2cdbbc9a LP |
7579 | " -a --all Show all properties/all units currently in memory,\n" |
7580 | " including dead/empty ones. To list all units installed on\n" | |
7581 | " the system, use the 'list-unit-files' command instead.\n" | |
bef19548 | 7582 | " --failed Same as --state=failed\n" |
98a6e132 | 7583 | " -l --full Don't ellipsize unit names on output\n" |
1238ee09 | 7584 | " -r --recursive Show unit list of host and local containers\n" |
4dc5b821 LP |
7585 | " --reverse Show reverse dependencies with 'list-dependencies'\n" |
7586 | " --job-mode=MODE Specify how to deal with already queued jobs, when\n" | |
7587 | " queueing a new job\n" | |
a521ae4a | 7588 | " --show-types When showing sockets, explicitly show their type\n" |
4f9a9105 | 7589 | " --value When showing properties, only print the value\n" |
b37844d3 LP |
7590 | " -i --ignore-inhibitors\n" |
7591 | " When shutting down or sleeping, ignore inhibitors\n" | |
a8f11321 LP |
7592 | " --kill-who=WHO Who to send signal to\n" |
7593 | " -s --signal=SIGNAL Which signal to send\n" | |
57ab2eab | 7594 | " --now Start or stop unit in addition to enabling or disabling it\n" |
1ae17672 | 7595 | " --dry-run Only print what would be done\n" |
8a0867d6 | 7596 | " -q --quiet Suppress output\n" |
93a08841 | 7597 | " --wait For (re)start, wait until service stopped again\n" |
adb6cd9b | 7598 | " For is-system-running, wait until startup is completed\n" |
8a0867d6 | 7599 | " --no-block Do not wait until operation finished\n" |
8a0867d6 | 7600 | " --no-wall Don't send wall message before halt/power-off/reboot\n" |
3fb90db2 | 7601 | " --no-reload Don't reload daemon after en-/dis-abling unit files\n" |
ebed32bf | 7602 | " --no-legend Do not print a legend (column headers and hints)\n" |
69fc152f | 7603 | " --no-pager Do not pipe output into a pager\n" |
501fc174 LP |
7604 | " --no-ask-password\n" |
7605 | " Do not ask for system passwords\n" | |
4e93d369 LR |
7606 | " --global Enable/disable/mask unit files globally\n" |
7607 | " --runtime Enable/disable/mask unit files temporarily until next\n" | |
7608 | " reboot\n" | |
8a0867d6 LP |
7609 | " -f --force When enabling unit files, override existing symlinks\n" |
7610 | " When shutting down, execute action immediately\n" | |
3fb90db2 | 7611 | " --preset-mode= Apply only enable, only disable, or all presets\n" |
4e93d369 LR |
7612 | " --root=PATH Enable/disable/mask unit files in the specified root\n" |
7613 | " directory\n" | |
76fdc966 | 7614 | " -n --lines=INTEGER Number of journal entries to show\n" |
584c6e70 | 7615 | " -o --output=STRING Change journal output mode (short, short-precise,\n" |
7e563bfc IW |
7616 | " short-iso, short-iso-precise, short-full,\n" |
7617 | " short-monotonic, short-unix,\n" | |
584c6e70 | 7618 | " verbose, export, json, json-pretty, json-sse, cat)\n" |
5bdf2243 | 7619 | " --firmware-setup Tell the firmware to show the setup menu on next boot\n" |
97af80c5 LP |
7620 | " --boot-loader-menu=TIME\n" |
7621 | " Boot into boot loader menu on next boot\n" | |
7622 | " --boot-loader-entry=NAME\n" | |
7623 | " Boot into a specific boot loader entry on next boot\n" | |
815ebc54 | 7624 | " --plain Print unit dependencies as a list instead of a tree\n\n" |
34c4b47b | 7625 | "Unit Commands:\n" |
fbf3283b ZJS |
7626 | " list-units [PATTERN...] List units currently in memory\n" |
7627 | " list-sockets [PATTERN...] List socket units currently in memory,\n" | |
7628 | " ordered by address\n" | |
7629 | " list-timers [PATTERN...] List timer units currently in memory,\n" | |
7630 | " ordered by next elapse\n" | |
7631 | " start UNIT... Start (activate) one or more units\n" | |
7632 | " stop UNIT... Stop (deactivate) one or more units\n" | |
7633 | " reload UNIT... Reload one or more units\n" | |
7634 | " restart UNIT... Start or restart one or more units\n" | |
7635 | " try-restart UNIT... Restart one or more units if active\n" | |
7636 | " reload-or-restart UNIT... Reload one or more units if possible,\n" | |
7637 | " otherwise start or restart\n" | |
7638 | " try-reload-or-restart UNIT... If active, reload one or more units,\n" | |
7639 | " if supported, otherwise restart\n" | |
7640 | " isolate UNIT Start one unit and stop all others\n" | |
7641 | " kill UNIT... Send signal to processes of a unit\n" | |
7642 | " is-active PATTERN... Check whether units are active\n" | |
7643 | " is-failed PATTERN... Check whether units are failed\n" | |
7644 | " status [PATTERN...|PID...] Show runtime status of one or more units\n" | |
7645 | " show [PATTERN...|JOB...] Show properties of one or more\n" | |
7646 | " units/jobs or the manager\n" | |
7647 | " cat PATTERN... Show files and drop-ins of specified units\n" | |
7648 | " set-property UNIT PROPERTY=VALUE... Sets one or more properties of a unit\n" | |
7649 | " help PATTERN...|PID... Show manual for one or more units\n" | |
7650 | " reset-failed [PATTERN...] Reset failed state for all, one, or more\n" | |
7651 | " units\n" | |
7652 | " list-dependencies [UNIT] Recursively show units which are required\n" | |
7653 | " or wanted by this unit or by which this\n" | |
7654 | " unit is required or wanted\n\n" | |
34c4b47b | 7655 | "Unit File Commands:\n" |
fbf3283b ZJS |
7656 | " list-unit-files [PATTERN...] List installed unit files\n" |
7657 | " enable [UNIT...|PATH...] Enable one or more unit files\n" | |
7658 | " disable UNIT... Disable one or more unit files\n" | |
7659 | " reenable UNIT... Reenable one or more unit files\n" | |
7660 | " preset UNIT... Enable/disable one or more unit files\n" | |
7661 | " based on preset configuration\n" | |
7662 | " preset-all Enable/disable all unit files based on\n" | |
7663 | " preset configuration\n" | |
7664 | " is-enabled UNIT... Check whether unit files are enabled\n" | |
7665 | " mask UNIT... Mask one or more units\n" | |
7666 | " unmask UNIT... Unmask one or more units\n" | |
7667 | " link PATH... Link one or more units files into\n" | |
7668 | " the search path\n" | |
7669 | " revert UNIT... Revert one or more unit files to vendor\n" | |
7670 | " version\n" | |
7671 | " add-wants TARGET UNIT... Add 'Wants' dependency for the target\n" | |
7672 | " on specified one or more units\n" | |
7673 | " add-requires TARGET UNIT... Add 'Requires' dependency for the target\n" | |
7674 | " on specified one or more units\n" | |
7675 | " edit UNIT... Edit one or more unit files\n" | |
7676 | " get-default Get the name of the default target\n" | |
7677 | " set-default TARGET Set the default target\n\n" | |
0d292f5e | 7678 | "Machine Commands:\n" |
fbf3283b | 7679 | " list-machines [PATTERN...] List local containers and host\n\n" |
34c4b47b | 7680 | "Job Commands:\n" |
fbf3283b ZJS |
7681 | " list-jobs [PATTERN...] List jobs\n" |
7682 | " cancel [JOB...] Cancel all, one, or more jobs\n\n" | |
34c4b47b | 7683 | "Environment Commands:\n" |
fbf3283b | 7684 | " show-environment Dump environment\n" |
135775c1 ZJS |
7685 | " set-environment VARIABLE=VALUE... Set one or more environment variables\n" |
7686 | " unset-environment VARIABLE... Unset one or more environment variables\n" | |
7687 | " import-environment [VARIABLE...] Import all or some environment variables\n\n" | |
34c4b47b | 7688 | "Manager Lifecycle Commands:\n" |
fbf3283b ZJS |
7689 | " daemon-reload Reload systemd manager configuration\n" |
7690 | " daemon-reexec Reexecute systemd manager\n\n" | |
34c4b47b | 7691 | "System Commands:\n" |
fbf3283b ZJS |
7692 | " is-system-running Check whether system is fully running\n" |
7693 | " default Enter system default mode\n" | |
7694 | " rescue Enter system rescue mode\n" | |
7695 | " emergency Enter system emergency mode\n" | |
7696 | " halt Shut down and halt the system\n" | |
7697 | " poweroff Shut down and power-off the system\n" | |
7698 | " reboot [ARG] Shut down and reboot the system\n" | |
7699 | " kexec Shut down and reboot the system with kexec\n" | |
7700 | " exit [EXIT_CODE] Request user instance or container exit\n" | |
7701 | " switch-root ROOT [INIT] Change to a different root file system\n" | |
7702 | " suspend Suspend the system\n" | |
7703 | " hibernate Hibernate the system\n" | |
c58493c0 | 7704 | " hybrid-sleep Hibernate and suspend the system\n" |
e68c79db | 7705 | " suspend-then-hibernate Suspend the system, wake after a period of\n" |
37ec0fdd LP |
7706 | " time and put it into hibernate\n" |
7707 | "\nSee the %s for details.\n" | |
7708 | , program_invocation_short_name | |
7709 | , link | |
7710 | ); | |
7711 | ||
7712 | return 0; | |
7e4249b9 LP |
7713 | } |
7714 | ||
37ec0fdd LP |
7715 | static int halt_help(void) { |
7716 | _cleanup_free_ char *link = NULL; | |
7717 | int r; | |
7718 | ||
7719 | r = terminal_urlify_man("halt", "8", &link); | |
7720 | if (r < 0) | |
7721 | return log_oom(); | |
7722 | ||
37185ec8 | 7723 | printf("%s [OPTIONS...]%s\n\n" |
e4b61340 LP |
7724 | "%s the system.\n\n" |
7725 | " --help Show this help\n" | |
7726 | " --halt Halt the machine\n" | |
7727 | " -p --poweroff Switch off the machine\n" | |
7728 | " --reboot Reboot the machine\n" | |
2e33c433 LP |
7729 | " -f --force Force immediate halt/power-off/reboot\n" |
7730 | " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n" | |
e4b61340 | 7731 | " -d --no-wtmp Don't write wtmp record\n" |
37ec0fdd LP |
7732 | " --no-wall Don't send wall message before halt/power-off/reboot\n" |
7733 | "\nSee the %s for details.\n" | |
7734 | , program_invocation_short_name | |
7735 | , arg_action == ACTION_REBOOT ? " [ARG]" : "", | |
7736 | arg_action == ACTION_REBOOT ? "Reboot" : | |
7737 | arg_action == ACTION_POWEROFF ? "Power off" : | |
7738 | "Halt" | |
7739 | , link | |
7740 | ); | |
7741 | ||
7742 | return 0; | |
e4b61340 LP |
7743 | } |
7744 | ||
37ec0fdd LP |
7745 | static int shutdown_help(void) { |
7746 | _cleanup_free_ char *link = NULL; | |
7747 | int r; | |
7748 | ||
7749 | r = terminal_urlify_man("shutdown", "8", &link); | |
7750 | if (r < 0) | |
7751 | return log_oom(); | |
7752 | ||
08e4b1c5 | 7753 | printf("%s [OPTIONS...] [TIME] [WALL...]\n\n" |
e4b61340 LP |
7754 | "Shut down the system.\n\n" |
7755 | " --help Show this help\n" | |
7756 | " -H --halt Halt the machine\n" | |
7757 | " -P --poweroff Power-off the machine\n" | |
7758 | " -r --reboot Reboot the machine\n" | |
386da858 | 7759 | " -h Equivalent to --poweroff, overridden by --halt\n" |
2e33c433 | 7760 | " -k Don't halt/power-off/reboot, just send warnings\n" |
f6144808 | 7761 | " --no-wall Don't send wall message before halt/power-off/reboot\n" |
37ec0fdd LP |
7762 | " -c Cancel a pending shutdown\n" |
7763 | "\nSee the %s for details.\n" | |
7764 | , program_invocation_short_name | |
7765 | , link | |
7766 | ); | |
7767 | ||
7768 | return 0; | |
e4b61340 LP |
7769 | } |
7770 | ||
37ec0fdd LP |
7771 | static int telinit_help(void) { |
7772 | _cleanup_free_ char *link = NULL; | |
7773 | int r; | |
7774 | ||
7775 | r = terminal_urlify_man("telinit", "8", &link); | |
7776 | if (r < 0) | |
7777 | return log_oom(); | |
7778 | ||
2e33c433 | 7779 | printf("%s [OPTIONS...] {COMMAND}\n\n" |
514f4ef5 LP |
7780 | "Send control commands to the init daemon.\n\n" |
7781 | " --help Show this help\n" | |
2e33c433 | 7782 | " --no-wall Don't send wall message before halt/power-off/reboot\n\n" |
e4b61340 LP |
7783 | "Commands:\n" |
7784 | " 0 Power-off the machine\n" | |
7785 | " 6 Reboot the machine\n" | |
514f4ef5 LP |
7786 | " 2, 3, 4, 5 Start runlevelX.target unit\n" |
7787 | " 1, s, S Enter rescue mode\n" | |
7788 | " q, Q Reload init daemon configuration\n" | |
37ec0fdd LP |
7789 | " u, U Reexecute init daemon\n" |
7790 | "\nSee the %s for details.\n" | |
7791 | , program_invocation_short_name | |
7792 | , link | |
7793 | ); | |
7794 | ||
7795 | return 0; | |
e4b61340 LP |
7796 | } |
7797 | ||
37ec0fdd LP |
7798 | static int runlevel_help(void) { |
7799 | _cleanup_free_ char *link = NULL; | |
7800 | int r; | |
7801 | ||
7802 | r = terminal_urlify_man("runlevel", "8", &link); | |
7803 | if (r < 0) | |
7804 | return log_oom(); | |
7805 | ||
2e33c433 | 7806 | printf("%s [OPTIONS...]\n\n" |
e4b61340 | 7807 | "Prints the previous and current runlevel of the init system.\n\n" |
37ec0fdd LP |
7808 | " --help Show this help\n" |
7809 | "\nSee the %s for details.\n" | |
7810 | , program_invocation_short_name | |
7811 | , link | |
7812 | ); | |
7813 | ||
7814 | return 0; | |
e4b61340 LP |
7815 | } |
7816 | ||
b93312f5 | 7817 | static void help_types(void) { |
b93312f5 ZJS |
7818 | if (!arg_no_legend) |
7819 | puts("Available unit types:"); | |
5c828e66 LP |
7820 | |
7821 | DUMP_STRING_TABLE(unit_type, UnitType, _UNIT_TYPE_MAX); | |
e16972e6 ZJS |
7822 | } |
7823 | ||
7824 | static void help_states(void) { | |
e16972e6 ZJS |
7825 | if (!arg_no_legend) |
7826 | puts("Available unit load states:"); | |
5c828e66 | 7827 | DUMP_STRING_TABLE(unit_load_state, UnitLoadState, _UNIT_LOAD_STATE_MAX); |
e16972e6 ZJS |
7828 | |
7829 | if (!arg_no_legend) | |
7830 | puts("\nAvailable unit active states:"); | |
5c828e66 | 7831 | DUMP_STRING_TABLE(unit_active_state, UnitActiveState, _UNIT_ACTIVE_STATE_MAX); |
7e55de3b ZJS |
7832 | |
7833 | if (!arg_no_legend) | |
7834 | puts("\nAvailable automount unit substates:"); | |
5c828e66 | 7835 | DUMP_STRING_TABLE(automount_state, AutomountState, _AUTOMOUNT_STATE_MAX); |
7e55de3b | 7836 | |
7e55de3b ZJS |
7837 | if (!arg_no_legend) |
7838 | puts("\nAvailable device unit substates:"); | |
5c828e66 | 7839 | DUMP_STRING_TABLE(device_state, DeviceState, _DEVICE_STATE_MAX); |
7e55de3b ZJS |
7840 | |
7841 | if (!arg_no_legend) | |
7842 | puts("\nAvailable mount unit substates:"); | |
5c828e66 | 7843 | DUMP_STRING_TABLE(mount_state, MountState, _MOUNT_STATE_MAX); |
7e55de3b ZJS |
7844 | |
7845 | if (!arg_no_legend) | |
7846 | puts("\nAvailable path unit substates:"); | |
5c828e66 | 7847 | DUMP_STRING_TABLE(path_state, PathState, _PATH_STATE_MAX); |
7e55de3b ZJS |
7848 | |
7849 | if (!arg_no_legend) | |
7850 | puts("\nAvailable scope unit substates:"); | |
5c828e66 | 7851 | DUMP_STRING_TABLE(scope_state, ScopeState, _SCOPE_STATE_MAX); |
7e55de3b ZJS |
7852 | |
7853 | if (!arg_no_legend) | |
7854 | puts("\nAvailable service unit substates:"); | |
5c828e66 | 7855 | DUMP_STRING_TABLE(service_state, ServiceState, _SERVICE_STATE_MAX); |
7e55de3b ZJS |
7856 | |
7857 | if (!arg_no_legend) | |
7858 | puts("\nAvailable slice unit substates:"); | |
5c828e66 | 7859 | DUMP_STRING_TABLE(slice_state, SliceState, _SLICE_STATE_MAX); |
7e55de3b | 7860 | |
7e55de3b ZJS |
7861 | if (!arg_no_legend) |
7862 | puts("\nAvailable socket unit substates:"); | |
5c828e66 | 7863 | DUMP_STRING_TABLE(socket_state, SocketState, _SOCKET_STATE_MAX); |
7e55de3b ZJS |
7864 | |
7865 | if (!arg_no_legend) | |
7866 | puts("\nAvailable swap unit substates:"); | |
5c828e66 | 7867 | DUMP_STRING_TABLE(swap_state, SwapState, _SWAP_STATE_MAX); |
7e55de3b ZJS |
7868 | |
7869 | if (!arg_no_legend) | |
7870 | puts("\nAvailable target unit substates:"); | |
5c828e66 | 7871 | DUMP_STRING_TABLE(target_state, TargetState, _TARGET_STATE_MAX); |
7e55de3b ZJS |
7872 | |
7873 | if (!arg_no_legend) | |
7874 | puts("\nAvailable timer unit substates:"); | |
5c828e66 | 7875 | DUMP_STRING_TABLE(timer_state, TimerState, _TIMER_STATE_MAX); |
45c0c61d ZJS |
7876 | } |
7877 | ||
97af80c5 LP |
7878 | static int help_boot_loader_entry(void) { |
7879 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
7880 | _cleanup_free_ char **l = NULL; | |
7881 | sd_bus *bus; | |
7882 | char **i; | |
7883 | int r; | |
7884 | ||
7885 | r = acquire_bus(BUS_FULL, &bus); | |
7886 | if (r < 0) | |
7887 | return r; | |
7888 | ||
7889 | r = sd_bus_get_property_strv( | |
7890 | bus, | |
7891 | "org.freedesktop.login1", | |
7892 | "/org/freedesktop/login1", | |
7893 | "org.freedesktop.login1.Manager", | |
7894 | "BootLoaderEntries", | |
7895 | &error, | |
7896 | &l); | |
7897 | if (r < 0) | |
7898 | return log_error_errno(r, "Failed to enumerate boot loader entries: %s", bus_error_message(&error, r)); | |
7899 | ||
7900 | if (strv_isempty(l)) { | |
7901 | log_error("No boot loader entries discovered."); | |
7902 | return -ENODATA; | |
7903 | } | |
7904 | ||
7905 | STRV_FOREACH(i, l) | |
7906 | puts(*i); | |
7907 | ||
7908 | return 0; | |
7909 | } | |
7910 | ||
e4b61340 | 7911 | static int systemctl_parse_argv(int argc, char *argv[]) { |
7e4249b9 | 7912 | enum { |
90d473a1 | 7913 | ARG_FAIL = 0x100, |
afba4199 ZJS |
7914 | ARG_REVERSE, |
7915 | ARG_AFTER, | |
7916 | ARG_BEFORE, | |
1ae17672 | 7917 | ARG_DRY_RUN, |
991f2a39 | 7918 | ARG_SHOW_TYPES, |
23ade460 | 7919 | ARG_IRREVERSIBLE, |
e67c3609 | 7920 | ARG_IGNORE_DEPENDENCIES, |
4f9a9105 | 7921 | ARG_VALUE, |
35df8f27 | 7922 | ARG_VERSION, |
af2d49f7 | 7923 | ARG_USER, |
7e4249b9 | 7924 | ARG_SYSTEM, |
ee5762e3 | 7925 | ARG_GLOBAL, |
6e905d93 | 7926 | ARG_NO_BLOCK, |
ebed32bf | 7927 | ARG_NO_LEGEND, |
611efaac | 7928 | ARG_NO_PAGER, |
4445a875 | 7929 | ARG_NO_WALL, |
be394c48 | 7930 | ARG_ROOT, |
ee5762e3 | 7931 | ARG_NO_RELOAD, |
501fc174 | 7932 | ARG_KILL_WHO, |
30732560 | 7933 | ARG_NO_ASK_PASSWORD, |
729e3769 | 7934 | ARG_FAILED, |
df50185b | 7935 | ARG_RUNTIME, |
9b9b3d36 | 7936 | ARG_PLAIN, |
4dc5b821 | 7937 | ARG_STATE, |
d309c1c3 LP |
7938 | ARG_JOB_MODE, |
7939 | ARG_PRESET_MODE, | |
5bdf2243 | 7940 | ARG_FIRMWARE_SETUP, |
97af80c5 LP |
7941 | ARG_BOOT_LOADER_MENU, |
7942 | ARG_BOOT_LOADER_ENTRY, | |
57ab2eab | 7943 | ARG_NOW, |
9ef15026 | 7944 | ARG_MESSAGE, |
93a08841 | 7945 | ARG_WAIT, |
7e4249b9 LP |
7946 | }; |
7947 | ||
7948 | static const struct option options[] = { | |
9ea9d4cf LP |
7949 | { "help", no_argument, NULL, 'h' }, |
7950 | { "version", no_argument, NULL, ARG_VERSION }, | |
7951 | { "type", required_argument, NULL, 't' }, | |
7952 | { "property", required_argument, NULL, 'p' }, | |
7953 | { "all", no_argument, NULL, 'a' }, | |
7954 | { "reverse", no_argument, NULL, ARG_REVERSE }, | |
7955 | { "after", no_argument, NULL, ARG_AFTER }, | |
7956 | { "before", no_argument, NULL, ARG_BEFORE }, | |
7957 | { "show-types", no_argument, NULL, ARG_SHOW_TYPES }, | |
7958 | { "failed", no_argument, NULL, ARG_FAILED }, /* compatibility only */ | |
7959 | { "full", no_argument, NULL, 'l' }, | |
4dc5b821 LP |
7960 | { "job-mode", required_argument, NULL, ARG_JOB_MODE }, |
7961 | { "fail", no_argument, NULL, ARG_FAIL }, /* compatibility only */ | |
7962 | { "irreversible", no_argument, NULL, ARG_IRREVERSIBLE }, /* compatibility only */ | |
7963 | { "ignore-dependencies", no_argument, NULL, ARG_IGNORE_DEPENDENCIES }, /* compatibility only */ | |
9ea9d4cf | 7964 | { "ignore-inhibitors", no_argument, NULL, 'i' }, |
4f9a9105 | 7965 | { "value", no_argument, NULL, ARG_VALUE }, |
9ea9d4cf LP |
7966 | { "user", no_argument, NULL, ARG_USER }, |
7967 | { "system", no_argument, NULL, ARG_SYSTEM }, | |
7968 | { "global", no_argument, NULL, ARG_GLOBAL }, | |
93a08841 | 7969 | { "wait", no_argument, NULL, ARG_WAIT }, |
9ea9d4cf LP |
7970 | { "no-block", no_argument, NULL, ARG_NO_BLOCK }, |
7971 | { "no-legend", no_argument, NULL, ARG_NO_LEGEND }, | |
7972 | { "no-pager", no_argument, NULL, ARG_NO_PAGER }, | |
7973 | { "no-wall", no_argument, NULL, ARG_NO_WALL }, | |
1ae17672 | 7974 | { "dry-run", no_argument, NULL, ARG_DRY_RUN }, |
9ea9d4cf LP |
7975 | { "quiet", no_argument, NULL, 'q' }, |
7976 | { "root", required_argument, NULL, ARG_ROOT }, | |
68da321f | 7977 | { "force", no_argument, NULL, 'f' }, |
9ea9d4cf LP |
7978 | { "no-reload", no_argument, NULL, ARG_NO_RELOAD }, |
7979 | { "kill-who", required_argument, NULL, ARG_KILL_WHO }, | |
7980 | { "signal", required_argument, NULL, 's' }, | |
7981 | { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD }, | |
7982 | { "host", required_argument, NULL, 'H' }, | |
f459b602 | 7983 | { "machine", required_argument, NULL, 'M' }, |
9ea9d4cf LP |
7984 | { "runtime", no_argument, NULL, ARG_RUNTIME }, |
7985 | { "lines", required_argument, NULL, 'n' }, | |
7986 | { "output", required_argument, NULL, 'o' }, | |
7987 | { "plain", no_argument, NULL, ARG_PLAIN }, | |
7988 | { "state", required_argument, NULL, ARG_STATE }, | |
1238ee09 | 7989 | { "recursive", no_argument, NULL, 'r' }, |
d309c1c3 | 7990 | { "preset-mode", required_argument, NULL, ARG_PRESET_MODE }, |
5bdf2243 | 7991 | { "firmware-setup", no_argument, NULL, ARG_FIRMWARE_SETUP }, |
97af80c5 LP |
7992 | { "boot-loader-menu", required_argument, NULL, ARG_BOOT_LOADER_MENU }, |
7993 | { "boot-loader-entry", required_argument, NULL, ARG_BOOT_LOADER_ENTRY }, | |
57ab2eab | 7994 | { "now", no_argument, NULL, ARG_NOW }, |
9ef15026 | 7995 | { "message", required_argument, NULL, ARG_MESSAGE }, |
eb9da376 | 7996 | {} |
7e4249b9 LP |
7997 | }; |
7998 | ||
0f03c2a4 | 7999 | int c, r; |
7e4249b9 | 8000 | |
e4b61340 | 8001 | assert(argc >= 0); |
7e4249b9 LP |
8002 | assert(argv); |
8003 | ||
16f017fa IS |
8004 | /* we default to allowing interactive authorization only in systemctl (not in the legacy commands) */ |
8005 | arg_ask_password = true; | |
8006 | ||
2e7e19ca | 8007 | while ((c = getopt_long(argc, argv, "ht:p:alqfs:H:M:n:o:ir.::", options, NULL)) >= 0) |
7e4249b9 LP |
8008 | |
8009 | switch (c) { | |
8010 | ||
8011 | case 'h': | |
37ec0fdd | 8012 | return systemctl_help(); |
35df8f27 LP |
8013 | |
8014 | case ARG_VERSION: | |
3f6fd1ba | 8015 | return version(); |
7e4249b9 | 8016 | |
20b3f379 | 8017 | case 't': { |
fb1ac50e LP |
8018 | const char *p; |
8019 | ||
baaa35ad ZJS |
8020 | if (isempty(optarg)) |
8021 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8022 | "--type= requires arguments."); | |
45c0c61d | 8023 | |
c58bd76a | 8024 | for (p = optarg;;) { |
5ab22f33 | 8025 | _cleanup_free_ char *type = NULL; |
20b3f379 | 8026 | |
5ab22f33 SS |
8027 | r = extract_first_word(&p, &type, ",", 0); |
8028 | if (r < 0) | |
8029 | return log_error_errno(r, "Failed to parse type: %s", optarg); | |
5ab22f33 SS |
8030 | if (r == 0) |
8031 | break; | |
20b3f379 ZJS |
8032 | |
8033 | if (streq(type, "help")) { | |
8034 | help_types(); | |
8035 | return 0; | |
8036 | } | |
8037 | ||
8038 | if (unit_type_from_string(type) >= 0) { | |
96e895ef | 8039 | if (strv_consume(&arg_types, TAKE_PTR(type)) < 0) |
20b3f379 | 8040 | return log_oom(); |
20b3f379 ZJS |
8041 | continue; |
8042 | } | |
8043 | ||
9b9b3d36 MW |
8044 | /* It's much nicer to use --state= for |
8045 | * load states, but let's support this | |
8046 | * in --types= too for compatibility | |
8047 | * with old versions */ | |
7e9d36e0 | 8048 | if (unit_load_state_from_string(type) >= 0) { |
96e895ef | 8049 | if (strv_consume(&arg_states, TAKE_PTR(type)) < 0) |
20b3f379 | 8050 | return log_oom(); |
20b3f379 ZJS |
8051 | continue; |
8052 | } | |
8053 | ||
ab06eef8 | 8054 | log_error("Unknown unit type or load state '%s'.", type); |
baaa35ad ZJS |
8055 | return log_info_errno(SYNTHETIC_ERRNO(EINVAL), |
8056 | "Use -t help to see a list of allowed values."); | |
c147dc42 | 8057 | } |
20b3f379 ZJS |
8058 | |
8059 | break; | |
8060 | } | |
8061 | ||
aee7082f LP |
8062 | case 'p': |
8063 | /* Make sure that if the empty property list was specified, we won't show any | |
8064 | properties. */ | |
20b3f379 | 8065 | if (isempty(optarg) && !arg_properties) { |
cbc9fbd1 | 8066 | arg_properties = new0(char*, 1); |
20b3f379 ZJS |
8067 | if (!arg_properties) |
8068 | return log_oom(); | |
fb1ac50e LP |
8069 | } else { |
8070 | const char *p; | |
8071 | ||
c58bd76a | 8072 | for (p = optarg;;) { |
5ab22f33 | 8073 | _cleanup_free_ char *prop = NULL; |
033a842c | 8074 | |
5ab22f33 SS |
8075 | r = extract_first_word(&p, &prop, ",", 0); |
8076 | if (r < 0) | |
8077 | return log_error_errno(r, "Failed to parse property: %s", optarg); | |
5ab22f33 SS |
8078 | if (r == 0) |
8079 | break; | |
ea4a240d | 8080 | |
9dccc6d3 | 8081 | if (strv_consume(&arg_properties, TAKE_PTR(prop)) < 0) |
20b3f379 | 8082 | return log_oom(); |
20b3f379 | 8083 | } |
fb1ac50e | 8084 | } |
48220598 LP |
8085 | |
8086 | /* If the user asked for a particular | |
8087 | * property, show it to him, even if it is | |
8088 | * empty. */ | |
8089 | arg_all = true; | |
033a842c | 8090 | |
48220598 LP |
8091 | break; |
8092 | ||
7e4249b9 LP |
8093 | case 'a': |
8094 | arg_all = true; | |
8095 | break; | |
8096 | ||
afba4199 ZJS |
8097 | case ARG_REVERSE: |
8098 | arg_dependency = DEPENDENCY_REVERSE; | |
8099 | break; | |
8100 | ||
8101 | case ARG_AFTER: | |
8102 | arg_dependency = DEPENDENCY_AFTER; | |
82948f6c | 8103 | arg_jobs_after = true; |
afba4199 ZJS |
8104 | break; |
8105 | ||
8106 | case ARG_BEFORE: | |
8107 | arg_dependency = DEPENDENCY_BEFORE; | |
82948f6c | 8108 | arg_jobs_before = true; |
afba4199 ZJS |
8109 | break; |
8110 | ||
991f2a39 ZJS |
8111 | case ARG_SHOW_TYPES: |
8112 | arg_show_types = true; | |
8113 | break; | |
8114 | ||
4f9a9105 ZJS |
8115 | case ARG_VALUE: |
8116 | arg_value = true; | |
8117 | break; | |
8118 | ||
4dc5b821 LP |
8119 | case ARG_JOB_MODE: |
8120 | arg_job_mode = optarg; | |
8121 | break; | |
8122 | ||
90d473a1 | 8123 | case ARG_FAIL: |
e67c3609 LP |
8124 | arg_job_mode = "fail"; |
8125 | break; | |
8126 | ||
23ade460 MS |
8127 | case ARG_IRREVERSIBLE: |
8128 | arg_job_mode = "replace-irreversibly"; | |
8129 | break; | |
8130 | ||
e67c3609 LP |
8131 | case ARG_IGNORE_DEPENDENCIES: |
8132 | arg_job_mode = "ignore-dependencies"; | |
7e4249b9 LP |
8133 | break; |
8134 | ||
af2d49f7 | 8135 | case ARG_USER: |
729e3769 | 8136 | arg_scope = UNIT_FILE_USER; |
7e4249b9 LP |
8137 | break; |
8138 | ||
8139 | case ARG_SYSTEM: | |
729e3769 LP |
8140 | arg_scope = UNIT_FILE_SYSTEM; |
8141 | break; | |
8142 | ||
8143 | case ARG_GLOBAL: | |
8144 | arg_scope = UNIT_FILE_GLOBAL; | |
7e4249b9 LP |
8145 | break; |
8146 | ||
93a08841 MP |
8147 | case ARG_WAIT: |
8148 | arg_wait = true; | |
8149 | break; | |
8150 | ||
6e905d93 LP |
8151 | case ARG_NO_BLOCK: |
8152 | arg_no_block = true; | |
7e4249b9 LP |
8153 | break; |
8154 | ||
ebed32bf MS |
8155 | case ARG_NO_LEGEND: |
8156 | arg_no_legend = true; | |
8157 | break; | |
8158 | ||
611efaac | 8159 | case ARG_NO_PAGER: |
0221d68a | 8160 | arg_pager_flags |= PAGER_DISABLE; |
611efaac | 8161 | break; |
0736af98 | 8162 | |
514f4ef5 LP |
8163 | case ARG_NO_WALL: |
8164 | arg_no_wall = true; | |
8165 | break; | |
8166 | ||
be394c48 | 8167 | case ARG_ROOT: |
bac75eb3 | 8168 | r = parse_path_argument_and_warn(optarg, false, &arg_root); |
0f03c2a4 LP |
8169 | if (r < 0) |
8170 | return r; | |
be394c48 FC |
8171 | break; |
8172 | ||
98a6e132 | 8173 | case 'l': |
8fe914ec LP |
8174 | arg_full = true; |
8175 | break; | |
8176 | ||
30732560 | 8177 | case ARG_FAILED: |
9b9b3d36 MW |
8178 | if (strv_extend(&arg_states, "failed") < 0) |
8179 | return log_oom(); | |
8180 | ||
30732560 LP |
8181 | break; |
8182 | ||
1ae17672 ZJS |
8183 | case ARG_DRY_RUN: |
8184 | arg_dry_run = true; | |
8185 | break; | |
8186 | ||
0183528f LP |
8187 | case 'q': |
8188 | arg_quiet = true; | |
8189 | break; | |
8190 | ||
b4f27ccc | 8191 | case 'f': |
313cefa1 | 8192 | arg_force++; |
ee5762e3 LP |
8193 | break; |
8194 | ||
8195 | case ARG_NO_RELOAD: | |
8196 | arg_no_reload = true; | |
8197 | break; | |
8198 | ||
8a0867d6 LP |
8199 | case ARG_KILL_WHO: |
8200 | arg_kill_who = optarg; | |
8201 | break; | |
8202 | ||
8a0867d6 | 8203 | case 's': |
5c828e66 LP |
8204 | if (streq(optarg, "help")) { |
8205 | DUMP_STRING_TABLE(signal, int, _NSIG); | |
8206 | return 0; | |
8207 | } | |
8208 | ||
29a3db75 | 8209 | arg_signal = signal_from_string(optarg); |
baaa35ad ZJS |
8210 | if (arg_signal < 0) |
8211 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8212 | "Failed to parse signal string %s.", | |
8213 | optarg); | |
8a0867d6 LP |
8214 | break; |
8215 | ||
501fc174 LP |
8216 | case ARG_NO_ASK_PASSWORD: |
8217 | arg_ask_password = false; | |
8218 | break; | |
8219 | ||
f459b602 MAP |
8220 | case 'H': |
8221 | arg_transport = BUS_TRANSPORT_REMOTE; | |
8222 | arg_host = optarg; | |
a8f11321 LP |
8223 | break; |
8224 | ||
f459b602 | 8225 | case 'M': |
de33fc62 | 8226 | arg_transport = BUS_TRANSPORT_MACHINE; |
f459b602 | 8227 | arg_host = optarg; |
a8f11321 LP |
8228 | break; |
8229 | ||
729e3769 LP |
8230 | case ARG_RUNTIME: |
8231 | arg_runtime = true; | |
8232 | break; | |
8233 | ||
df50185b | 8234 | case 'n': |
baaa35ad ZJS |
8235 | if (safe_atou(optarg, &arg_lines) < 0) |
8236 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8237 | "Failed to parse lines '%s'", | |
8238 | optarg); | |
df50185b LP |
8239 | break; |
8240 | ||
df50185b | 8241 | case 'o': |
5c828e66 LP |
8242 | if (streq(optarg, "help")) { |
8243 | DUMP_STRING_TABLE(output_mode, OutputMode, _OUTPUT_MODE_MAX); | |
8244 | return 0; | |
8245 | } | |
8246 | ||
df50185b | 8247 | arg_output = output_mode_from_string(optarg); |
baaa35ad ZJS |
8248 | if (arg_output < 0) |
8249 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8250 | "Unknown output '%s'.", | |
8251 | optarg); | |
df50185b LP |
8252 | break; |
8253 | ||
b37844d3 LP |
8254 | case 'i': |
8255 | arg_ignore_inhibitors = true; | |
8256 | break; | |
8257 | ||
5d0c05e5 LN |
8258 | case ARG_PLAIN: |
8259 | arg_plain = true; | |
8260 | break; | |
8261 | ||
5bdf2243 JJ |
8262 | case ARG_FIRMWARE_SETUP: |
8263 | arg_firmware_setup = true; | |
8264 | break; | |
8265 | ||
97af80c5 LP |
8266 | case ARG_BOOT_LOADER_MENU: |
8267 | ||
8268 | r = parse_sec(optarg, &arg_boot_loader_menu); | |
8269 | if (r < 0) | |
8270 | return log_error_errno(r, "Failed to parse --boot-loader-menu= argument '%s': %m", optarg); | |
8271 | ||
8272 | break; | |
8273 | ||
8274 | case ARG_BOOT_LOADER_ENTRY: | |
8275 | ||
8276 | if (streq(optarg, "help")) { /* Yes, this means, "help" is not a valid boot loader entry name we can deal with */ | |
8277 | r = help_boot_loader_entry(); | |
8278 | if (r < 0) | |
8279 | return r; | |
8280 | ||
8281 | return 0; | |
8282 | } | |
8283 | ||
8284 | arg_boot_loader_entry = empty_to_null(optarg); | |
8285 | break; | |
8286 | ||
9b9b3d36 | 8287 | case ARG_STATE: { |
fb1ac50e LP |
8288 | const char *p; |
8289 | ||
baaa35ad ZJS |
8290 | if (isempty(optarg)) |
8291 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8292 | "--state= requires arguments."); | |
9b9b3d36 | 8293 | |
c58bd76a | 8294 | for (p = optarg;;) { |
bc6c18fe | 8295 | _cleanup_free_ char *s = NULL; |
9b9b3d36 | 8296 | |
5ab22f33 SS |
8297 | r = extract_first_word(&p, &s, ",", 0); |
8298 | if (r < 0) | |
6862111e | 8299 | return log_error_errno(r, "Failed to parse state: %s", optarg); |
5ab22f33 SS |
8300 | if (r == 0) |
8301 | break; | |
9b9b3d36 | 8302 | |
e16972e6 ZJS |
8303 | if (streq(s, "help")) { |
8304 | help_states(); | |
8305 | return 0; | |
8306 | } | |
8307 | ||
9dccc6d3 | 8308 | if (strv_consume(&arg_states, TAKE_PTR(s)) < 0) |
9b9b3d36 | 8309 | return log_oom(); |
9b9b3d36 MW |
8310 | } |
8311 | break; | |
8312 | } | |
8313 | ||
1238ee09 | 8314 | case 'r': |
baaa35ad ZJS |
8315 | if (geteuid() != 0) |
8316 | return log_error_errno(SYNTHETIC_ERRNO(EPERM), | |
8317 | "--recursive requires root privileges."); | |
1238ee09 LP |
8318 | |
8319 | arg_recursive = true; | |
8320 | break; | |
8321 | ||
d309c1c3 | 8322 | case ARG_PRESET_MODE: |
5c828e66 LP |
8323 | if (streq(optarg, "help")) { |
8324 | DUMP_STRING_TABLE(unit_file_preset_mode, UnitFilePresetMode, _UNIT_FILE_PRESET_MAX); | |
8325 | return 0; | |
8326 | } | |
d309c1c3 LP |
8327 | |
8328 | arg_preset_mode = unit_file_preset_mode_from_string(optarg); | |
baaa35ad ZJS |
8329 | if (arg_preset_mode < 0) |
8330 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8331 | "Failed to parse preset mode: %s.", optarg); | |
d309c1c3 LP |
8332 | |
8333 | break; | |
8334 | ||
57ab2eab JS |
8335 | case ARG_NOW: |
8336 | arg_now = true; | |
8337 | break; | |
8338 | ||
9ef15026 JS |
8339 | case ARG_MESSAGE: |
8340 | if (strv_extend(&arg_wall, optarg) < 0) | |
8341 | return log_oom(); | |
8342 | break; | |
8343 | ||
2e7e19ca ZJS |
8344 | case '.': |
8345 | /* Output an error mimicking getopt, and print a hint afterwards */ | |
8346 | log_error("%s: invalid option -- '.'", program_invocation_name); | |
8347 | log_notice("Hint: to specify units starting with a dash, use \"--\":\n" | |
8348 | " %s [OPTIONS...] {COMMAND} -- -.%s ...", | |
8349 | program_invocation_name, optarg ?: "mount"); | |
8350 | _fallthrough_; | |
8351 | ||
7e4249b9 LP |
8352 | case '?': |
8353 | return -EINVAL; | |
8354 | ||
8355 | default: | |
eb9da376 | 8356 | assert_not_reached("Unhandled option"); |
7e4249b9 | 8357 | } |
7e4249b9 | 8358 | |
baaa35ad ZJS |
8359 | if (arg_transport != BUS_TRANSPORT_LOCAL && arg_scope != UNIT_FILE_SYSTEM) |
8360 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8361 | "Cannot access user instance remotely."); | |
a8f11321 | 8362 | |
baaa35ad ZJS |
8363 | if (arg_wait && arg_no_block) |
8364 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8365 | "--wait may not be combined with --no-block."); | |
93a08841 | 8366 | |
7e4249b9 LP |
8367 | return 1; |
8368 | } | |
8369 | ||
e4b61340 | 8370 | static int halt_parse_argv(int argc, char *argv[]) { |
e4b61340 LP |
8371 | enum { |
8372 | ARG_HELP = 0x100, | |
8373 | ARG_HALT, | |
514f4ef5 LP |
8374 | ARG_REBOOT, |
8375 | ARG_NO_WALL | |
e4b61340 LP |
8376 | }; |
8377 | ||
8378 | static const struct option options[] = { | |
8379 | { "help", no_argument, NULL, ARG_HELP }, | |
8380 | { "halt", no_argument, NULL, ARG_HALT }, | |
8381 | { "poweroff", no_argument, NULL, 'p' }, | |
8382 | { "reboot", no_argument, NULL, ARG_REBOOT }, | |
8383 | { "force", no_argument, NULL, 'f' }, | |
8384 | { "wtmp-only", no_argument, NULL, 'w' }, | |
8385 | { "no-wtmp", no_argument, NULL, 'd' }, | |
f3f054f0 | 8386 | { "no-sync", no_argument, NULL, 'n' }, |
514f4ef5 | 8387 | { "no-wall", no_argument, NULL, ARG_NO_WALL }, |
eb9da376 | 8388 | {} |
e4b61340 LP |
8389 | }; |
8390 | ||
37185ec8 | 8391 | int c, r, runlevel; |
e4b61340 LP |
8392 | |
8393 | assert(argc >= 0); | |
8394 | assert(argv); | |
8395 | ||
8396 | if (utmp_get_runlevel(&runlevel, NULL) >= 0) | |
4c701096 | 8397 | if (IN_SET(runlevel, '0', '6')) |
65491fd8 | 8398 | arg_force = 2; |
e4b61340 | 8399 | |
601185b4 | 8400 | while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) |
e4b61340 LP |
8401 | switch (c) { |
8402 | ||
8403 | case ARG_HELP: | |
37ec0fdd | 8404 | return halt_help(); |
e4b61340 LP |
8405 | |
8406 | case ARG_HALT: | |
8407 | arg_action = ACTION_HALT; | |
8408 | break; | |
8409 | ||
8410 | case 'p': | |
a042efad MS |
8411 | if (arg_action != ACTION_REBOOT) |
8412 | arg_action = ACTION_POWEROFF; | |
e4b61340 LP |
8413 | break; |
8414 | ||
8415 | case ARG_REBOOT: | |
8416 | arg_action = ACTION_REBOOT; | |
8417 | break; | |
8418 | ||
8419 | case 'f': | |
65491fd8 | 8420 | arg_force = 2; |
e4b61340 LP |
8421 | break; |
8422 | ||
8423 | case 'w': | |
1ae17672 | 8424 | arg_dry_run = true; |
e4b61340 LP |
8425 | break; |
8426 | ||
8427 | case 'd': | |
8428 | arg_no_wtmp = true; | |
8429 | break; | |
8430 | ||
f3f054f0 TB |
8431 | case 'n': |
8432 | arg_no_sync = true; | |
8433 | break; | |
8434 | ||
514f4ef5 LP |
8435 | case ARG_NO_WALL: |
8436 | arg_no_wall = true; | |
8437 | break; | |
8438 | ||
e4b61340 LP |
8439 | case 'i': |
8440 | case 'h': | |
8441 | /* Compatibility nops */ | |
8442 | break; | |
8443 | ||
8444 | case '?': | |
8445 | return -EINVAL; | |
8446 | ||
8447 | default: | |
eb9da376 | 8448 | assert_not_reached("Unhandled option"); |
e4b61340 | 8449 | } |
e4b61340 | 8450 | |
c5220a94 | 8451 | if (arg_action == ACTION_REBOOT && (argc == optind || argc == optind + 1)) { |
27c06cb5 | 8452 | r = update_reboot_parameter_and_warn(argc == optind + 1 ? argv[optind] : NULL); |
c5220a94 | 8453 | if (r < 0) |
37185ec8 | 8454 | return r; |
baaa35ad ZJS |
8455 | } else if (optind < argc) |
8456 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8457 | "Too many arguments."); | |
e4b61340 LP |
8458 | |
8459 | return 1; | |
8460 | } | |
8461 | ||
2cc7b0a2 | 8462 | static int parse_shutdown_time_spec(const char *t, usec_t *_u) { |
f6144808 LP |
8463 | assert(t); |
8464 | assert(_u); | |
8465 | ||
8466 | if (streq(t, "now")) | |
8467 | *_u = 0; | |
1a639877 | 8468 | else if (!strchr(t, ':')) { |
f6144808 LP |
8469 | uint64_t u; |
8470 | ||
1a639877 | 8471 | if (safe_atou64(t, &u) < 0) |
f6144808 LP |
8472 | return -EINVAL; |
8473 | ||
8474 | *_u = now(CLOCK_REALTIME) + USEC_PER_MINUTE * u; | |
8475 | } else { | |
8476 | char *e = NULL; | |
8477 | long hour, minute; | |
b92bea5d | 8478 | struct tm tm = {}; |
f6144808 LP |
8479 | time_t s; |
8480 | usec_t n; | |
8481 | ||
8482 | errno = 0; | |
8483 | hour = strtol(t, &e, 10); | |
8333c77e | 8484 | if (errno > 0 || *e != ':' || hour < 0 || hour > 23) |
f6144808 LP |
8485 | return -EINVAL; |
8486 | ||
8487 | minute = strtol(e+1, &e, 10); | |
8333c77e | 8488 | if (errno > 0 || *e != 0 || minute < 0 || minute > 59) |
f6144808 LP |
8489 | return -EINVAL; |
8490 | ||
8491 | n = now(CLOCK_REALTIME); | |
08e4b1c5 LP |
8492 | s = (time_t) (n / USEC_PER_SEC); |
8493 | ||
f6144808 LP |
8494 | assert_se(localtime_r(&s, &tm)); |
8495 | ||
8496 | tm.tm_hour = (int) hour; | |
8497 | tm.tm_min = (int) minute; | |
08e4b1c5 | 8498 | tm.tm_sec = 0; |
f6144808 | 8499 | |
855a86a3 ZJS |
8500 | s = mktime(&tm); |
8501 | assert(s >= 0); | |
f6144808 LP |
8502 | |
8503 | *_u = (usec_t) s * USEC_PER_SEC; | |
8504 | ||
8505 | while (*_u <= n) | |
8506 | *_u += USEC_PER_DAY; | |
8507 | } | |
8508 | ||
8509 | return 0; | |
8510 | } | |
8511 | ||
e4b61340 | 8512 | static int shutdown_parse_argv(int argc, char *argv[]) { |
e4b61340 LP |
8513 | enum { |
8514 | ARG_HELP = 0x100, | |
514f4ef5 | 8515 | ARG_NO_WALL |
e4b61340 LP |
8516 | }; |
8517 | ||
8518 | static const struct option options[] = { | |
8519 | { "help", no_argument, NULL, ARG_HELP }, | |
8520 | { "halt", no_argument, NULL, 'H' }, | |
8521 | { "poweroff", no_argument, NULL, 'P' }, | |
8522 | { "reboot", no_argument, NULL, 'r' }, | |
04ebb595 | 8523 | { "kexec", no_argument, NULL, 'K' }, /* not documented extension */ |
514f4ef5 | 8524 | { "no-wall", no_argument, NULL, ARG_NO_WALL }, |
eb9da376 | 8525 | {} |
e4b61340 LP |
8526 | }; |
8527 | ||
172d7abf | 8528 | char **wall = NULL; |
f6144808 | 8529 | int c, r; |
e4b61340 LP |
8530 | |
8531 | assert(argc >= 0); | |
8532 | assert(argv); | |
8533 | ||
b068c6f5 | 8534 | while ((c = getopt_long(argc, argv, "HPrhkKat:fFc", options, NULL)) >= 0) |
e4b61340 LP |
8535 | switch (c) { |
8536 | ||
8537 | case ARG_HELP: | |
37ec0fdd | 8538 | return shutdown_help(); |
e4b61340 LP |
8539 | |
8540 | case 'H': | |
8541 | arg_action = ACTION_HALT; | |
8542 | break; | |
8543 | ||
8544 | case 'P': | |
8545 | arg_action = ACTION_POWEROFF; | |
8546 | break; | |
8547 | ||
8548 | case 'r': | |
5622dde3 KS |
8549 | if (kexec_loaded()) |
8550 | arg_action = ACTION_KEXEC; | |
8551 | else | |
8552 | arg_action = ACTION_REBOOT; | |
e4b61340 LP |
8553 | break; |
8554 | ||
04ebb595 LP |
8555 | case 'K': |
8556 | arg_action = ACTION_KEXEC; | |
8557 | break; | |
8558 | ||
e4b61340 LP |
8559 | case 'h': |
8560 | if (arg_action != ACTION_HALT) | |
8561 | arg_action = ACTION_POWEROFF; | |
8562 | break; | |
8563 | ||
8564 | case 'k': | |
1ae17672 | 8565 | arg_dry_run = true; |
e4b61340 LP |
8566 | break; |
8567 | ||
514f4ef5 LP |
8568 | case ARG_NO_WALL: |
8569 | arg_no_wall = true; | |
8570 | break; | |
8571 | ||
e4b61340 | 8572 | case 'a': |
b068c6f5 | 8573 | case 't': /* Note that we also ignore any passed argument to -t, not just the -t itself */ |
75836b9d JS |
8574 | case 'f': |
8575 | case 'F': | |
e4b61340 LP |
8576 | /* Compatibility nops */ |
8577 | break; | |
8578 | ||
f6144808 LP |
8579 | case 'c': |
8580 | arg_action = ACTION_CANCEL_SHUTDOWN; | |
8581 | break; | |
8582 | ||
e4b61340 LP |
8583 | case '?': |
8584 | return -EINVAL; | |
8585 | ||
8586 | default: | |
eb9da376 | 8587 | assert_not_reached("Unhandled option"); |
e4b61340 | 8588 | } |
e4b61340 | 8589 | |
dfcc5c33 | 8590 | if (argc > optind && arg_action != ACTION_CANCEL_SHUTDOWN) { |
2cc7b0a2 | 8591 | r = parse_shutdown_time_spec(argv[optind], &arg_when); |
7e59bfcb | 8592 | if (r < 0) { |
f6144808 LP |
8593 | log_error("Failed to parse time specification: %s", argv[optind]); |
8594 | return r; | |
8595 | } | |
6b5ad000 | 8596 | } else |
08e4b1c5 | 8597 | arg_when = now(CLOCK_REALTIME) + USEC_PER_MINUTE; |
442b9094 | 8598 | |
dfcc5c33 MS |
8599 | if (argc > optind && arg_action == ACTION_CANCEL_SHUTDOWN) |
8600 | /* No time argument for shutdown cancel */ | |
172d7abf | 8601 | wall = argv + optind; |
dfcc5c33 MS |
8602 | else if (argc > optind + 1) |
8603 | /* We skip the time argument */ | |
172d7abf LP |
8604 | wall = argv + optind + 1; |
8605 | ||
8606 | if (wall) { | |
8607 | arg_wall = strv_copy(wall); | |
8608 | if (!arg_wall) | |
8609 | return log_oom(); | |
8610 | } | |
e4b61340 LP |
8611 | |
8612 | optind = argc; | |
8613 | ||
8614 | return 1; | |
e4b61340 LP |
8615 | } |
8616 | ||
8617 | static int telinit_parse_argv(int argc, char *argv[]) { | |
e4b61340 LP |
8618 | enum { |
8619 | ARG_HELP = 0x100, | |
514f4ef5 | 8620 | ARG_NO_WALL |
e4b61340 LP |
8621 | }; |
8622 | ||
8623 | static const struct option options[] = { | |
8624 | { "help", no_argument, NULL, ARG_HELP }, | |
514f4ef5 | 8625 | { "no-wall", no_argument, NULL, ARG_NO_WALL }, |
eb9da376 | 8626 | {} |
e4b61340 LP |
8627 | }; |
8628 | ||
8629 | static const struct { | |
8630 | char from; | |
8631 | enum action to; | |
8632 | } table[] = { | |
8633 | { '0', ACTION_POWEROFF }, | |
8634 | { '6', ACTION_REBOOT }, | |
ef2f1067 | 8635 | { '1', ACTION_RESCUE }, |
e4b61340 LP |
8636 | { '2', ACTION_RUNLEVEL2 }, |
8637 | { '3', ACTION_RUNLEVEL3 }, | |
8638 | { '4', ACTION_RUNLEVEL4 }, | |
8639 | { '5', ACTION_RUNLEVEL5 }, | |
8640 | { 's', ACTION_RESCUE }, | |
8641 | { 'S', ACTION_RESCUE }, | |
8642 | { 'q', ACTION_RELOAD }, | |
8643 | { 'Q', ACTION_RELOAD }, | |
8644 | { 'u', ACTION_REEXEC }, | |
8645 | { 'U', ACTION_REEXEC } | |
8646 | }; | |
8647 | ||
8648 | unsigned i; | |
8649 | int c; | |
8650 | ||
8651 | assert(argc >= 0); | |
8652 | assert(argv); | |
8653 | ||
601185b4 | 8654 | while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) |
e4b61340 LP |
8655 | switch (c) { |
8656 | ||
8657 | case ARG_HELP: | |
37ec0fdd | 8658 | return telinit_help(); |
e4b61340 | 8659 | |
514f4ef5 LP |
8660 | case ARG_NO_WALL: |
8661 | arg_no_wall = true; | |
8662 | break; | |
8663 | ||
e4b61340 LP |
8664 | case '?': |
8665 | return -EINVAL; | |
8666 | ||
8667 | default: | |
eb9da376 | 8668 | assert_not_reached("Unhandled option"); |
e4b61340 | 8669 | } |
e4b61340 | 8670 | |
baaa35ad ZJS |
8671 | if (optind >= argc) |
8672 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8673 | "%s: required argument missing.", | |
8674 | program_invocation_short_name); | |
e4b61340 | 8675 | |
baaa35ad ZJS |
8676 | if (optind + 1 < argc) |
8677 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8678 | "Too many arguments."); | |
e4b61340 | 8679 | |
baaa35ad ZJS |
8680 | if (strlen(argv[optind]) != 1) |
8681 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8682 | "Expected single character argument."); | |
e4b61340 LP |
8683 | |
8684 | for (i = 0; i < ELEMENTSOF(table); i++) | |
8685 | if (table[i].from == argv[optind][0]) | |
8686 | break; | |
8687 | ||
baaa35ad ZJS |
8688 | if (i >= ELEMENTSOF(table)) |
8689 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8690 | "Unknown command '%s'.", argv[optind]); | |
e4b61340 LP |
8691 | |
8692 | arg_action = table[i].to; | |
8693 | ||
313cefa1 | 8694 | optind++; |
e4b61340 LP |
8695 | |
8696 | return 1; | |
8697 | } | |
8698 | ||
8699 | static int runlevel_parse_argv(int argc, char *argv[]) { | |
e4b61340 LP |
8700 | enum { |
8701 | ARG_HELP = 0x100, | |
8702 | }; | |
8703 | ||
8704 | static const struct option options[] = { | |
8705 | { "help", no_argument, NULL, ARG_HELP }, | |
eb9da376 | 8706 | {} |
e4b61340 LP |
8707 | }; |
8708 | ||
8709 | int c; | |
8710 | ||
8711 | assert(argc >= 0); | |
8712 | assert(argv); | |
8713 | ||
601185b4 | 8714 | while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) |
e4b61340 LP |
8715 | switch (c) { |
8716 | ||
8717 | case ARG_HELP: | |
37ec0fdd | 8718 | return runlevel_help(); |
e4b61340 LP |
8719 | |
8720 | case '?': | |
8721 | return -EINVAL; | |
8722 | ||
8723 | default: | |
eb9da376 | 8724 | assert_not_reached("Unhandled option"); |
e4b61340 | 8725 | } |
e4b61340 | 8726 | |
baaa35ad ZJS |
8727 | if (optind < argc) |
8728 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
8729 | "Too many arguments."); | |
e4b61340 LP |
8730 | |
8731 | return 1; | |
8732 | } | |
8733 | ||
8734 | static int parse_argv(int argc, char *argv[]) { | |
8735 | assert(argc >= 0); | |
8736 | assert(argv); | |
8737 | ||
8738 | if (program_invocation_short_name) { | |
8739 | ||
8740 | if (strstr(program_invocation_short_name, "halt")) { | |
8741 | arg_action = ACTION_HALT; | |
8742 | return halt_parse_argv(argc, argv); | |
c54819ca | 8743 | |
e4b61340 LP |
8744 | } else if (strstr(program_invocation_short_name, "poweroff")) { |
8745 | arg_action = ACTION_POWEROFF; | |
8746 | return halt_parse_argv(argc, argv); | |
c54819ca | 8747 | |
e4b61340 | 8748 | } else if (strstr(program_invocation_short_name, "reboot")) { |
5622dde3 KS |
8749 | if (kexec_loaded()) |
8750 | arg_action = ACTION_KEXEC; | |
8751 | else | |
8752 | arg_action = ACTION_REBOOT; | |
e4b61340 | 8753 | return halt_parse_argv(argc, argv); |
c54819ca | 8754 | |
e4b61340 LP |
8755 | } else if (strstr(program_invocation_short_name, "shutdown")) { |
8756 | arg_action = ACTION_POWEROFF; | |
8757 | return shutdown_parse_argv(argc, argv); | |
c54819ca | 8758 | |
e4b61340 | 8759 | } else if (strstr(program_invocation_short_name, "init")) { |
d5ca5f11 | 8760 | |
c54819ca LP |
8761 | /* Matches invocations as "init" as well as "telinit", which are synonymous when run as PID != |
8762 | * 1 on SysV. | |
8763 | * | |
8764 | * On SysV "telinit" was the official command to communicate with PID 1, but "init" would | |
8765 | * redirect itself to "telinit" if called with PID != 1. We follow the same logic here still, | |
8766 | * though we add one level of indirection, as we implement "telinit" in "systemctl". Hence, for | |
8767 | * us if you invoke "init" you get "systemd", but it will execve() "systemctl" immediately with | |
8768 | * argv[] unmodified if PID is != 1. If you invoke "telinit" you directly get "systemctl". In | |
8769 | * both cases we shall do the same thing, which is why we do strstr(p_i_s_n, "init") here, as a | |
8770 | * quick way to match both. | |
8771 | * | |
8772 | * Also see redirect_telinit() in src/core/main.c. */ | |
8773 | ||
d5ca5f11 | 8774 | if (sd_booted() > 0) { |
f459b602 | 8775 | arg_action = _ACTION_INVALID; |
d5ca5f11 LP |
8776 | return telinit_parse_argv(argc, argv); |
8777 | } else { | |
c54819ca LP |
8778 | /* Hmm, so some other init system is running, we need to forward this request to |
8779 | * it. For now we simply guess that it is Upstart. */ | |
d5ca5f11 | 8780 | |
595225af | 8781 | (void) rlimit_nofile_safe(); |
4ad61fd1 | 8782 | execv(TELINIT, argv); |
d5ca5f11 | 8783 | |
baaa35ad ZJS |
8784 | return log_error_errno(SYNTHETIC_ERRNO(EIO), |
8785 | "Couldn't find an alternative telinit implementation to spawn."); | |
d5ca5f11 LP |
8786 | } |
8787 | ||
e4b61340 LP |
8788 | } else if (strstr(program_invocation_short_name, "runlevel")) { |
8789 | arg_action = ACTION_RUNLEVEL; | |
8790 | return runlevel_parse_argv(argc, argv); | |
8791 | } | |
8792 | } | |
8793 | ||
8794 | arg_action = ACTION_SYSTEMCTL; | |
8795 | return systemctl_parse_argv(argc, argv); | |
8796 | } | |
8797 | ||
349cc4a5 | 8798 | #if HAVE_SYSV_COMPAT |
44a6b1b6 | 8799 | _pure_ static int action_to_runlevel(void) { |
eb22ac37 LP |
8800 | static const char table[_ACTION_MAX] = { |
8801 | [ACTION_HALT] = '0', | |
8802 | [ACTION_POWEROFF] = '0', | |
8803 | [ACTION_REBOOT] = '6', | |
8804 | [ACTION_RUNLEVEL2] = '2', | |
8805 | [ACTION_RUNLEVEL3] = '3', | |
8806 | [ACTION_RUNLEVEL4] = '4', | |
8807 | [ACTION_RUNLEVEL5] = '5', | |
8808 | [ACTION_RESCUE] = '1' | |
8809 | }; | |
8810 | ||
78ca9099 | 8811 | assert(arg_action >= 0 && arg_action < _ACTION_MAX); |
d55ae9e6 LP |
8812 | |
8813 | return table[arg_action]; | |
8814 | } | |
d2e79673 | 8815 | #endif |
d55ae9e6 | 8816 | |
d55ae9e6 | 8817 | static int talk_initctl(void) { |
349cc4a5 | 8818 | #if HAVE_SYSV_COMPAT |
cbc9fbd1 LP |
8819 | struct init_request request = { |
8820 | .magic = INIT_MAGIC, | |
8821 | .sleeptime = 0, | |
8822 | .cmd = INIT_CMD_RUNLVL | |
8823 | }; | |
8824 | ||
7fd1b19b | 8825 | _cleanup_close_ int fd = -1; |
d55ae9e6 | 8826 | char rl; |
cbc9fbd1 | 8827 | int r; |
83c76e8c | 8828 | const char *p; |
eb22ac37 | 8829 | |
427b47c4 ZJS |
8830 | rl = action_to_runlevel(); |
8831 | if (!rl) | |
eb22ac37 LP |
8832 | return 0; |
8833 | ||
d55ae9e6 LP |
8834 | request.runlevel = rl; |
8835 | ||
83c76e8c MG |
8836 | FOREACH_STRING(p, "/run/initctl", "/dev/initctl") { |
8837 | fd = open(p, O_WRONLY|O_NONBLOCK|O_CLOEXEC|O_NOCTTY); | |
8838 | if (fd >= 0 || errno != ENOENT) | |
8839 | break; | |
8840 | } | |
427b47c4 | 8841 | if (fd < 0) { |
d55ae9e6 LP |
8842 | if (errno == ENOENT) |
8843 | return 0; | |
eb22ac37 | 8844 | |
83c76e8c | 8845 | return log_error_errno(errno, "Failed to open initctl fifo: %m"); |
d55ae9e6 | 8846 | } |
eb22ac37 | 8847 | |
553acb7b ZJS |
8848 | r = loop_write(fd, &request, sizeof(request), false); |
8849 | if (r < 0) | |
83c76e8c | 8850 | return log_error_errno(r, "Failed to write to %s: %m", p); |
eb22ac37 LP |
8851 | |
8852 | return 1; | |
eca830be LP |
8853 | #else |
8854 | return 0; | |
8855 | #endif | |
e4b61340 LP |
8856 | } |
8857 | ||
e449de87 | 8858 | static int systemctl_main(int argc, char *argv[]) { |
e449de87 | 8859 | static const Verb verbs[] = { |
c56d1e2c CW |
8860 | { "list-units", VERB_ANY, VERB_ANY, VERB_DEFAULT|VERB_ONLINE_ONLY, list_units }, |
8861 | { "list-unit-files", VERB_ANY, VERB_ANY, 0, list_unit_files }, | |
8862 | { "list-sockets", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, list_sockets }, | |
8863 | { "list-timers", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, list_timers }, | |
8864 | { "list-jobs", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, list_jobs }, | |
8865 | { "list-machines", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY|VERB_MUST_BE_ROOT, list_machines }, | |
8866 | { "clear-jobs", VERB_ANY, 1, VERB_ONLINE_ONLY, trivial_method }, | |
8867 | { "cancel", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, cancel_job }, | |
8868 | { "start", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, | |
8869 | { "stop", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, | |
8870 | { "condstop", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, /* For compatibility with ALTLinux */ | |
8871 | { "reload", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, | |
8872 | { "restart", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, | |
8873 | { "try-restart", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, | |
8874 | { "reload-or-restart", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, | |
8875 | { "reload-or-try-restart", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, /* For compatbility with old systemctl <= 228 */ | |
8876 | { "try-reload-or-restart", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, | |
8877 | { "force-reload", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, /* For compatibility with SysV */ | |
8878 | { "condreload", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, /* For compatibility with ALTLinux */ | |
8879 | { "condrestart", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, /* For compatibility with RH */ | |
8880 | { "isolate", 2, 2, VERB_ONLINE_ONLY, start_unit }, | |
8881 | { "kill", 2, VERB_ANY, VERB_ONLINE_ONLY, kill_unit }, | |
8882 | { "is-active", 2, VERB_ANY, VERB_ONLINE_ONLY, check_unit_active }, | |
3e7e587d | 8883 | { "check", 2, VERB_ANY, VERB_ONLINE_ONLY, check_unit_active }, /* deprecated alias of is-active */ |
c56d1e2c CW |
8884 | { "is-failed", 2, VERB_ANY, VERB_ONLINE_ONLY, check_unit_failed }, |
8885 | { "show", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, show }, | |
8886 | { "cat", 2, VERB_ANY, VERB_ONLINE_ONLY, cat }, | |
8887 | { "status", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, show }, | |
8888 | { "help", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, show }, | |
8889 | { "daemon-reload", VERB_ANY, 1, VERB_ONLINE_ONLY, daemon_reload }, | |
8890 | { "daemon-reexec", VERB_ANY, 1, VERB_ONLINE_ONLY, daemon_reload }, | |
8891 | { "show-environment", VERB_ANY, 1, VERB_ONLINE_ONLY, show_environment }, | |
8892 | { "set-environment", 2, VERB_ANY, VERB_ONLINE_ONLY, set_environment }, | |
8893 | { "unset-environment", 2, VERB_ANY, VERB_ONLINE_ONLY, set_environment }, | |
8894 | { "import-environment", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, import_environment }, | |
8895 | { "halt", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, | |
8896 | { "poweroff", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, | |
8897 | { "reboot", VERB_ANY, 2, VERB_ONLINE_ONLY, start_system_special }, | |
8898 | { "kexec", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, | |
8899 | { "suspend", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, | |
8900 | { "hibernate", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, | |
8901 | { "hybrid-sleep", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, | |
e68c79db | 8902 | { "suspend-then-hibernate",VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, |
c56d1e2c CW |
8903 | { "default", VERB_ANY, 1, VERB_ONLINE_ONLY, start_special }, |
8904 | { "rescue", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, | |
8905 | { "emergency", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, | |
8906 | { "exit", VERB_ANY, 2, VERB_ONLINE_ONLY, start_special }, | |
8907 | { "reset-failed", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, reset_failed }, | |
8908 | { "enable", 2, VERB_ANY, 0, enable_unit }, | |
8909 | { "disable", 2, VERB_ANY, 0, enable_unit }, | |
8910 | { "is-enabled", 2, VERB_ANY, 0, unit_is_enabled }, | |
8911 | { "reenable", 2, VERB_ANY, 0, enable_unit }, | |
8912 | { "preset", 2, VERB_ANY, 0, enable_unit }, | |
8913 | { "preset-all", VERB_ANY, 1, 0, preset_all }, | |
8914 | { "mask", 2, VERB_ANY, 0, enable_unit }, | |
8915 | { "unmask", 2, VERB_ANY, 0, enable_unit }, | |
8916 | { "link", 2, VERB_ANY, 0, enable_unit }, | |
8917 | { "revert", 2, VERB_ANY, 0, enable_unit }, | |
8918 | { "switch-root", 2, VERB_ANY, VERB_ONLINE_ONLY, switch_root }, | |
8919 | { "list-dependencies", VERB_ANY, 2, VERB_ONLINE_ONLY, list_dependencies }, | |
8920 | { "set-default", 2, 2, 0, set_default }, | |
8921 | { "get-default", VERB_ANY, 1, 0, get_default }, | |
8922 | { "set-property", 3, VERB_ANY, VERB_ONLINE_ONLY, set_property }, | |
8923 | { "is-system-running", VERB_ANY, 1, 0, is_system_running }, | |
8924 | { "add-wants", 3, VERB_ANY, 0, add_dependency }, | |
8925 | { "add-requires", 3, VERB_ANY, 0, add_dependency }, | |
8926 | { "edit", 2, VERB_ANY, VERB_ONLINE_ONLY, edit }, | |
d08e75ed | 8927 | {} |
e449de87 | 8928 | }; |
7e4249b9 | 8929 | |
e449de87 | 8930 | return dispatch_verb(argc, argv, verbs, NULL); |
e4b61340 LP |
8931 | } |
8932 | ||
4fbd7192 | 8933 | static int reload_with_fallback(void) { |
4fbd7192 | 8934 | /* First, try systemd via D-Bus. */ |
e449de87 | 8935 | if (daemon_reload(0, NULL, NULL) >= 0) |
4fbd7192 | 8936 | return 0; |
e4b61340 LP |
8937 | |
8938 | /* Nothing else worked, so let's try signals */ | |
2853b60a | 8939 | assert(IN_SET(arg_action, ACTION_RELOAD, ACTION_REEXEC)); |
e4b61340 | 8940 | |
4a62c710 MS |
8941 | if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) |
8942 | return log_error_errno(errno, "kill() failed: %m"); | |
e4b61340 LP |
8943 | |
8944 | return 0; | |
8945 | } | |
8946 | ||
4fbd7192 | 8947 | static int start_with_fallback(void) { |
4fbd7192 | 8948 | /* First, try systemd via D-Bus. */ |
2e13c0c3 | 8949 | if (start_unit(0, NULL, NULL) == 0) |
4fbd7192 | 8950 | return 0; |
e4b61340 | 8951 | |
2853b60a | 8952 | /* Nothing else worked, so let's try /dev/initctl */ |
fbc43921 | 8953 | if (talk_initctl() > 0) |
48ec22bc | 8954 | return 0; |
d55ae9e6 | 8955 | |
baaa35ad ZJS |
8956 | return log_error_errno(SYNTHETIC_ERRNO(EIO), |
8957 | "Failed to talk to init daemon."); | |
e4b61340 LP |
8958 | } |
8959 | ||
477def80 | 8960 | static int halt_now(enum action a) { |
c01dcddf | 8961 | /* The kernel will automatically flush ATA disks and suchlike on reboot(), but the file systems need to be |
f03ec2b1 | 8962 | * synced explicitly in advance. */ |
1ae17672 | 8963 | if (!arg_no_sync && !arg_dry_run) |
f3f054f0 | 8964 | (void) sync(); |
4a3ad399 | 8965 | |
1ae17672 ZJS |
8966 | /* Make sure C-A-D is handled by the kernel from this point on... */ |
8967 | if (!arg_dry_run) | |
8968 | (void) reboot(RB_ENABLE_CAD); | |
e606bb61 | 8969 | |
4c80c73c | 8970 | switch (a) { |
e606bb61 LP |
8971 | |
8972 | case ACTION_HALT: | |
60675884 LP |
8973 | if (!arg_quiet) |
8974 | log_info("Halting."); | |
1ae17672 ZJS |
8975 | if (arg_dry_run) |
8976 | return 0; | |
19578bb2 | 8977 | (void) reboot(RB_HALT_SYSTEM); |
477def80 | 8978 | return -errno; |
e606bb61 LP |
8979 | |
8980 | case ACTION_POWEROFF: | |
60675884 LP |
8981 | if (!arg_quiet) |
8982 | log_info("Powering off."); | |
1ae17672 ZJS |
8983 | if (arg_dry_run) |
8984 | return 0; | |
19578bb2 | 8985 | (void) reboot(RB_POWER_OFF); |
477def80 | 8986 | return -errno; |
e606bb61 | 8987 | |
98d52feb | 8988 | case ACTION_KEXEC: |
c01dcddf LP |
8989 | case ACTION_REBOOT: |
8990 | return reboot_with_parameter(REBOOT_FALLBACK | | |
8991 | (arg_quiet ? 0 : REBOOT_LOG) | | |
8992 | (arg_dry_run ? REBOOT_DRY_RUN : 0)); | |
e606bb61 | 8993 | |
477def80 LP |
8994 | default: |
8995 | assert_not_reached("Unknown action."); | |
8996 | } | |
e606bb61 LP |
8997 | } |
8998 | ||
56a730fa | 8999 | static int logind_schedule_shutdown(void) { |
7f56c3d4 | 9000 | |
cf9cda44 | 9001 | #if ENABLE_LOGIND |
4afd3348 | 9002 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
56a730fa LP |
9003 | char date[FORMAT_TIMESTAMP_MAX]; |
9004 | const char *action; | |
4fbd7192 | 9005 | sd_bus *bus; |
56a730fa LP |
9006 | int r; |
9007 | ||
4fbd7192 | 9008 | r = acquire_bus(BUS_FULL, &bus); |
56a730fa | 9009 | if (r < 0) |
4fbd7192 | 9010 | return r; |
56a730fa LP |
9011 | |
9012 | switch (arg_action) { | |
9013 | case ACTION_HALT: | |
9014 | action = "halt"; | |
9015 | break; | |
9016 | case ACTION_POWEROFF: | |
9017 | action = "poweroff"; | |
9018 | break; | |
9019 | case ACTION_KEXEC: | |
9020 | action = "kexec"; | |
9021 | break; | |
a4420f7b LP |
9022 | case ACTION_EXIT: |
9023 | action = "exit"; | |
9024 | break; | |
9025 | case ACTION_REBOOT: | |
56a730fa LP |
9026 | default: |
9027 | action = "reboot"; | |
9028 | break; | |
9029 | } | |
9030 | ||
1ae17672 | 9031 | if (arg_dry_run) |
56a730fa LP |
9032 | action = strjoina("dry-", action); |
9033 | ||
d2ad7e1b ZJS |
9034 | (void) logind_set_wall_message(); |
9035 | ||
56a730fa | 9036 | r = sd_bus_call_method( |
4fbd7192 | 9037 | bus, |
56a730fa LP |
9038 | "org.freedesktop.login1", |
9039 | "/org/freedesktop/login1", | |
9040 | "org.freedesktop.login1.Manager", | |
9041 | "ScheduleShutdown", | |
9042 | &error, | |
9043 | NULL, | |
9044 | "st", | |
9045 | action, | |
9046 | arg_when); | |
9047 | if (r < 0) | |
9048 | return log_warning_errno(r, "Failed to call ScheduleShutdown in logind, proceeding with immediate shutdown: %s", bus_error_message(&error, r)); | |
9049 | ||
60675884 LP |
9050 | if (!arg_quiet) |
9051 | log_info("Shutdown scheduled for %s, use 'shutdown -c' to cancel.", format_timestamp(date, sizeof(date), arg_when)); | |
56a730fa | 9052 | return 0; |
cf9cda44 DJL |
9053 | #else |
9054 | log_error("Cannot schedule shutdown without logind support, proceeding with immediate shutdown."); | |
9055 | return -ENOSYS; | |
807690fb | 9056 | #endif |
56a730fa LP |
9057 | } |
9058 | ||
4fbd7192 | 9059 | static int halt_main(void) { |
e4b61340 LP |
9060 | int r; |
9061 | ||
4fbd7192 | 9062 | r = logind_check_inhibitors(arg_action); |
748ebafa LP |
9063 | if (r < 0) |
9064 | return r; | |
b37844d3 | 9065 | |
940bec70 DJL |
9066 | /* Delayed shutdown requested, and was successful */ |
9067 | if (arg_when > 0 && logind_schedule_shutdown() == 0) | |
9068 | return 0; | |
9069 | /* no delay, or logind failed or is not at all available */ | |
7f96539d | 9070 | |
bc8c2f5c | 9071 | if (geteuid() != 0) { |
1ae17672 | 9072 | if (arg_dry_run || arg_force > 0) { |
fba868fa | 9073 | (void) must_be_root(); |
2ac3930f IS |
9074 | return -EPERM; |
9075 | } | |
9076 | ||
7e59bfcb | 9077 | /* Try logind if we are a normal user and no special |
d35f51ea | 9078 | * mode applies. Maybe polkit allows us to shutdown |
7e59bfcb | 9079 | * the machine. */ |
36b69c31 | 9080 | if (IN_SET(arg_action, ACTION_POWEROFF, ACTION_REBOOT, ACTION_HALT)) { |
4fbd7192 | 9081 | r = logind_reboot(arg_action); |
4c80c73c KS |
9082 | if (r >= 0) |
9083 | return r; | |
a9085ea3 | 9084 | if (IN_SET(r, -EOPNOTSUPP, -EINPROGRESS)) |
7f96539d LP |
9085 | /* requested operation is not |
9086 | * supported on the local system or | |
9087 | * already in progress */ | |
a9085ea3 IS |
9088 | return r; |
9089 | /* on all other errors, try low-level operation */ | |
4c80c73c | 9090 | } |
bc8c2f5c LP |
9091 | } |
9092 | ||
2955e0d4 GJ |
9093 | /* In order to minimize the difference between operation with and |
9094 | * without logind, we explicitly enable non-blocking mode for this, | |
9095 | * as logind's shutdown operations are always non-blocking. */ | |
9096 | arg_no_block = true; | |
9097 | ||
1ae17672 | 9098 | if (!arg_dry_run && !arg_force) |
4fbd7192 | 9099 | return start_with_fallback(); |
e4b61340 | 9100 | |
2ac3930f IS |
9101 | assert(geteuid() == 0); |
9102 | ||
d90e1a30 LP |
9103 | if (!arg_no_wtmp) { |
9104 | if (sd_booted() > 0) | |
9105 | log_debug("Not writing utmp record, assuming that systemd-update-utmp is used."); | |
7e59bfcb LP |
9106 | else { |
9107 | r = utmp_put_shutdown(); | |
9108 | if (r < 0) | |
da927ba9 | 9109 | log_warning_errno(r, "Failed to write utmp record: %m"); |
7e59bfcb | 9110 | } |
d90e1a30 | 9111 | } |
e4b61340 | 9112 | |
1ae17672 | 9113 | if (arg_dry_run) |
e4b61340 LP |
9114 | return 0; |
9115 | ||
477def80 | 9116 | r = halt_now(arg_action); |
691395d8 | 9117 | return log_error_errno(r, "Failed to reboot: %m"); |
e4b61340 LP |
9118 | } |
9119 | ||
9120 | static int runlevel_main(void) { | |
9121 | int r, runlevel, previous; | |
9122 | ||
729e3769 LP |
9123 | r = utmp_get_runlevel(&runlevel, &previous); |
9124 | if (r < 0) { | |
9125 | puts("unknown"); | |
e4b61340 LP |
9126 | return r; |
9127 | } | |
9128 | ||
9129 | printf("%c %c\n", | |
9130 | previous <= 0 ? 'N' : previous, | |
9131 | runlevel <= 0 ? 'N' : runlevel); | |
9132 | ||
9133 | return 0; | |
9134 | } | |
9135 | ||
2cf05793 | 9136 | static int logind_cancel_shutdown(void) { |
349cc4a5 | 9137 | #if ENABLE_LOGIND |
4afd3348 | 9138 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
4fbd7192 | 9139 | sd_bus *bus; |
949d9ce9 LP |
9140 | int r; |
9141 | ||
4fbd7192 | 9142 | r = acquire_bus(BUS_FULL, &bus); |
949d9ce9 | 9143 | if (r < 0) |
4fbd7192 | 9144 | return r; |
949d9ce9 | 9145 | |
4fbd7192 | 9146 | (void) logind_set_wall_message(); |
949d9ce9 LP |
9147 | |
9148 | r = sd_bus_call_method( | |
4fbd7192 | 9149 | bus, |
949d9ce9 LP |
9150 | "org.freedesktop.login1", |
9151 | "/org/freedesktop/login1", | |
9152 | "org.freedesktop.login1.Manager", | |
9153 | "CancelScheduledShutdown", | |
9154 | &error, | |
9155 | NULL, NULL); | |
9156 | if (r < 0) | |
9157 | return log_warning_errno(r, "Failed to talk to logind, shutdown hasn't been cancelled: %s", bus_error_message(&error, r)); | |
9158 | ||
9159 | return 0; | |
2cf05793 LP |
9160 | #else |
9161 | log_error("Not compiled with logind support, cannot cancel scheduled shutdowns."); | |
9162 | return -ENOSYS; | |
9163 | #endif | |
949d9ce9 LP |
9164 | } |
9165 | ||
f2a3de01 | 9166 | static int run(int argc, char *argv[]) { |
f459b602 | 9167 | int r; |
e4b61340 | 9168 | |
a9cdc94f | 9169 | setlocale(LC_ALL, ""); |
e4b61340 | 9170 | log_parse_environment(); |
2396fb04 | 9171 | log_open(); |
1abaf488 LP |
9172 | |
9173 | /* The journal merging logic potentially needs a lot of fds. */ | |
9174 | (void) rlimit_nofile_bump(HIGH_RLIMIT_NOFILE); | |
9175 | ||
592705f2 | 9176 | sigbus_install(); |
e4b61340 | 9177 | |
184ecaf7 DR |
9178 | /* Explicitly not on_tty() to avoid setting cached value. |
9179 | * This becomes relevant for piping output which might be | |
9180 | * ellipsized. */ | |
9181 | original_stdout_is_tty = isatty(STDOUT_FILENO); | |
9182 | ||
04ebb595 | 9183 | r = parse_argv(argc, argv); |
f459b602 | 9184 | if (r <= 0) |
e4b61340 | 9185 | goto finish; |
7e4249b9 | 9186 | |
040524b4 | 9187 | if (arg_action != ACTION_SYSTEMCTL && running_in_chroot() > 0) { |
60675884 LP |
9188 | if (!arg_quiet) |
9189 | log_info("Running in chroot, ignoring request."); | |
f459b602 | 9190 | r = 0; |
82e23ddd LP |
9191 | goto finish; |
9192 | } | |
9193 | ||
41dd15e4 LP |
9194 | /* systemctl_main() will print an error message for the bus |
9195 | * connection, but only if it needs to */ | |
e4b61340 LP |
9196 | |
9197 | switch (arg_action) { | |
9198 | ||
22f4096c | 9199 | case ACTION_SYSTEMCTL: |
e449de87 | 9200 | r = systemctl_main(argc, argv); |
e4b61340 | 9201 | break; |
e4b61340 | 9202 | |
081dc638 AJ |
9203 | /* Legacy command aliases set arg_action. They provide some fallbacks, |
9204 | * e.g. to tell sysvinit to reboot after you have installed systemd | |
9205 | * binaries. */ | |
9206 | ||
e4b61340 LP |
9207 | case ACTION_HALT: |
9208 | case ACTION_POWEROFF: | |
9209 | case ACTION_REBOOT: | |
5622dde3 | 9210 | case ACTION_KEXEC: |
4fbd7192 | 9211 | r = halt_main(); |
e4b61340 LP |
9212 | break; |
9213 | ||
e4b61340 LP |
9214 | case ACTION_RUNLEVEL2: |
9215 | case ACTION_RUNLEVEL3: | |
9216 | case ACTION_RUNLEVEL4: | |
9217 | case ACTION_RUNLEVEL5: | |
9218 | case ACTION_RESCUE: | |
4fbd7192 | 9219 | r = start_with_fallback(); |
e4b61340 | 9220 | break; |
7e4249b9 | 9221 | |
e4b61340 LP |
9222 | case ACTION_RELOAD: |
9223 | case ACTION_REEXEC: | |
4fbd7192 | 9224 | r = reload_with_fallback(); |
e4b61340 LP |
9225 | break; |
9226 | ||
949d9ce9 | 9227 | case ACTION_CANCEL_SHUTDOWN: |
2cf05793 | 9228 | r = logind_cancel_shutdown(); |
f6144808 LP |
9229 | break; |
9230 | ||
eb22ac37 | 9231 | case ACTION_RUNLEVEL: |
4f16c1f4 LP |
9232 | r = runlevel_main(); |
9233 | break; | |
9234 | ||
081dc638 AJ |
9235 | case ACTION_EXIT: |
9236 | case ACTION_SUSPEND: | |
9237 | case ACTION_HIBERNATE: | |
9238 | case ACTION_HYBRID_SLEEP: | |
e68c79db | 9239 | case ACTION_SUSPEND_THEN_HIBERNATE: |
081dc638 AJ |
9240 | case ACTION_EMERGENCY: |
9241 | case ACTION_DEFAULT: | |
9242 | /* systemctl verbs with no equivalent in the legacy commands. | |
9243 | * These cannot appear in arg_action. Fall through. */ | |
9244 | ||
f459b602 | 9245 | case _ACTION_INVALID: |
e4b61340 LP |
9246 | default: |
9247 | assert_not_reached("Unknown action"); | |
9248 | } | |
7e4249b9 LP |
9249 | |
9250 | finish: | |
cf647b69 LP |
9251 | release_busses(); |
9252 | ||
d1405af3 ZJS |
9253 | /* Note that we return r here, not 0, so that we can implement the LSB-like return codes */ |
9254 | return r; | |
7e4249b9 | 9255 | } |
d1405af3 ZJS |
9256 | |
9257 | DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run); |