]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
7e4249b9 LP |
2 | /*** |
3 | This file is part of systemd. | |
4 | ||
5 | Copyright 2010 Lennart Poettering | |
f459b602 | 6 | Copyright 2013 Marc-Antoine Perennou |
7e4249b9 LP |
7 | ***/ |
8 | ||
03a7b521 LP |
9 | #include <errno.h> |
10 | #include <fcntl.h> | |
7e4249b9 | 11 | #include <getopt.h> |
a9cdc94f | 12 | #include <locale.h> |
7e4249b9 | 13 | #include <stdbool.h> |
03a7b521 LP |
14 | #include <stddef.h> |
15 | #include <stdio.h> | |
7e4249b9 | 16 | #include <string.h> |
4bb2e9d4 | 17 | #include <sys/prctl.h> |
03a7b521 | 18 | #include <sys/reboot.h> |
f1c5860b | 19 | #include <sys/socket.h> |
03a7b521 | 20 | #include <unistd.h> |
81527be1 | 21 | |
03a7b521 | 22 | #include "sd-bus.h" |
f459b602 | 23 | #include "sd-daemon.h" |
f459b602 | 24 | #include "sd-login.h" |
03a7b521 | 25 | |
b5efdb8a | 26 | #include "alloc-util.h" |
4bb2e9d4 | 27 | #include "bootspec.h" |
03a7b521 LP |
28 | #include "bus-common-errors.h" |
29 | #include "bus-error.h" | |
30 | #include "bus-message.h" | |
291d565a | 31 | #include "bus-unit-util.h" |
03a7b521 | 32 | #include "bus-util.h" |
ab35fb1b | 33 | #include "cgroup-show.h" |
c6c18be3 | 34 | #include "cgroup-util.h" |
cda134ab | 35 | #include "copy.h" |
ad2a0358 | 36 | #include "dropin.h" |
5bdf2243 | 37 | #include "efivars.h" |
03a7b521 | 38 | #include "env-util.h" |
804ee07c | 39 | #include "escape.h" |
03a7b521 | 40 | #include "exit-status.h" |
3ffd4af2 | 41 | #include "fd-util.h" |
03a7b521 | 42 | #include "fileio.h" |
f97b34a6 | 43 | #include "format-util.h" |
f4f15635 | 44 | #include "fs-util.h" |
7d50b32a | 45 | #include "glob-util.h" |
08f3be7a | 46 | #include "hexdecoct.h" |
c52a937b | 47 | #include "hostname-util.h" |
03a7b521 LP |
48 | #include "initreq.h" |
49 | #include "install.h" | |
c004493c | 50 | #include "io-util.h" |
03a7b521 | 51 | #include "list.h" |
8752c575 | 52 | #include "locale-util.h" |
03a7b521 LP |
53 | #include "log.h" |
54 | #include "logs-show.h" | |
55 | #include "macro.h" | |
56 | #include "mkdir.h" | |
57 | #include "pager.h" | |
6bedfcbb | 58 | #include "parse-util.h" |
03a7b521 LP |
59 | #include "path-lookup.h" |
60 | #include "path-util.h" | |
61 | #include "process-util.h" | |
e3631d1c | 62 | #include "reboot-util.h" |
78f22b97 | 63 | #include "rlimit-util.h" |
03a7b521 | 64 | #include "set.h" |
592705f2 | 65 | #include "sigbus.h" |
24882e06 | 66 | #include "signal-util.h" |
03a7b521 LP |
67 | #include "socket-util.h" |
68 | #include "spawn-ask-password-agent.h" | |
69 | #include "spawn-polkit-agent.h" | |
70 | #include "special.h" | |
8fcde012 | 71 | #include "stat-util.h" |
8559e61d | 72 | #include "string-table.h" |
03a7b521 LP |
73 | #include "strv.h" |
74 | #include "terminal-util.h" | |
89ada3ba | 75 | #include "unit-def.h" |
03a7b521 | 76 | #include "unit-name.h" |
b1d4f8e1 | 77 | #include "user-util.h" |
03a7b521 LP |
78 | #include "util.h" |
79 | #include "utmp-wtmp.h" | |
e449de87 | 80 | #include "verbs.h" |
7f4b3c5e | 81 | #include "virt.h" |
7e4249b9 | 82 | |
b613907e SS |
83 | /* The init script exit status codes |
84 | 0 program is running or service is OK | |
85 | 1 program is dead and /var/run pid file exists | |
86 | 2 program is dead and /var/lock lock file exists | |
87 | 3 program is not running | |
88 | 4 program or service status is unknown | |
89 | 5-99 reserved for future LSB use | |
90 | 100-149 reserved for distribution use | |
91 | 150-199 reserved for application use | |
92 | 200-254 reserved | |
93 | */ | |
94 | enum { | |
95 | EXIT_PROGRAM_RUNNING_OR_SERVICE_OK = 0, | |
96 | EXIT_PROGRAM_DEAD_AND_PID_EXISTS = 1, | |
97 | EXIT_PROGRAM_DEAD_AND_LOCK_FILE_EXISTS = 2, | |
98 | EXIT_PROGRAM_NOT_RUNNING = 3, | |
99 | EXIT_PROGRAM_OR_SERVICES_STATUS_UNKNOWN = 4, | |
100 | }; | |
101 | ||
20b3f379 | 102 | static char **arg_types = NULL; |
9b9b3d36 | 103 | static char **arg_states = NULL; |
20b3f379 | 104 | static char **arg_properties = NULL; |
7e4249b9 | 105 | static bool arg_all = false; |
afba4199 ZJS |
106 | static enum dependency { |
107 | DEPENDENCY_FORWARD, | |
108 | DEPENDENCY_REVERSE, | |
109 | DEPENDENCY_AFTER, | |
110 | DEPENDENCY_BEFORE, | |
071066a5 | 111 | _DEPENDENCY_MAX |
afba4199 | 112 | } arg_dependency = DEPENDENCY_FORWARD; |
e67c3609 | 113 | static const char *arg_job_mode = "replace"; |
729e3769 | 114 | static UnitFileScope arg_scope = UNIT_FILE_SYSTEM; |
93a08841 | 115 | static bool arg_wait = false; |
ee5762e3 | 116 | static bool arg_no_block = false; |
ebed32bf | 117 | static bool arg_no_legend = false; |
0736af98 | 118 | static bool arg_no_pager = false; |
e4b61340 | 119 | static bool arg_no_wtmp = false; |
f3f054f0 | 120 | static bool arg_no_sync = false; |
514f4ef5 | 121 | static bool arg_no_wall = false; |
ee5762e3 | 122 | static bool arg_no_reload = false; |
4f9a9105 | 123 | static bool arg_value = false; |
991f2a39 | 124 | static bool arg_show_types = false; |
b37844d3 | 125 | static bool arg_ignore_inhibitors = false; |
1ae17672 | 126 | static bool arg_dry_run = false; |
0183528f | 127 | static bool arg_quiet = false; |
ee5762e3 | 128 | static bool arg_full = false; |
1238ee09 | 129 | static bool arg_recursive = false; |
e606bb61 | 130 | static int arg_force = 0; |
16f017fa | 131 | static bool arg_ask_password = false; |
729e3769 | 132 | static bool arg_runtime = false; |
d309c1c3 | 133 | static UnitFilePresetMode arg_preset_mode = UNIT_FILE_PRESET_FULL; |
e4b61340 | 134 | static char **arg_wall = NULL; |
8a0867d6 | 135 | static const char *arg_kill_who = NULL; |
8a0867d6 | 136 | static int arg_signal = SIGTERM; |
0f03c2a4 | 137 | static char *arg_root = NULL; |
f6144808 | 138 | static usec_t arg_when = 0; |
4bb2e9d4 | 139 | static char *arg_esp_path = NULL; |
acc28e2e | 140 | static char *argv_cmdline = NULL; |
4445a875 | 141 | static enum action { |
e4b61340 LP |
142 | ACTION_SYSTEMCTL, |
143 | ACTION_HALT, | |
144 | ACTION_POWEROFF, | |
145 | ACTION_REBOOT, | |
20b09ca7 LP |
146 | ACTION_KEXEC, |
147 | ACTION_EXIT, | |
6edd7d0a LP |
148 | ACTION_SUSPEND, |
149 | ACTION_HIBERNATE, | |
6524990f | 150 | ACTION_HYBRID_SLEEP, |
e68c79db | 151 | ACTION_SUSPEND_THEN_HIBERNATE, |
e4b61340 LP |
152 | ACTION_RUNLEVEL2, |
153 | ACTION_RUNLEVEL3, | |
154 | ACTION_RUNLEVEL4, | |
155 | ACTION_RUNLEVEL5, | |
156 | ACTION_RESCUE, | |
514f4ef5 LP |
157 | ACTION_EMERGENCY, |
158 | ACTION_DEFAULT, | |
e4b61340 LP |
159 | ACTION_RELOAD, |
160 | ACTION_REEXEC, | |
161 | ACTION_RUNLEVEL, | |
f6144808 | 162 | ACTION_CANCEL_SHUTDOWN, |
913c1916 AJ |
163 | _ACTION_MAX, |
164 | _ACTION_INVALID = -1 | |
e4b61340 | 165 | } arg_action = ACTION_SYSTEMCTL; |
f459b602 | 166 | static BusTransport arg_transport = BUS_TRANSPORT_LOCAL; |
fc2ffaf1 | 167 | static const char *arg_host = NULL; |
df50185b LP |
168 | static unsigned arg_lines = 10; |
169 | static OutputMode arg_output = OUTPUT_SHORT; | |
5d0c05e5 | 170 | static bool arg_plain = false; |
5bdf2243 | 171 | static bool arg_firmware_setup = false; |
57ab2eab | 172 | static bool arg_now = false; |
82948f6c LP |
173 | static bool arg_jobs_before = false; |
174 | static bool arg_jobs_after = false; | |
1238ee09 | 175 | |
e449de87 | 176 | static int daemon_reload(int argc, char *argv[], void* userdata); |
2853b60a | 177 | static int trivial_method(int argc, char *argv[], void *userdata); |
477def80 | 178 | static int halt_now(enum action a); |
fa0d5878 | 179 | static int get_state_one_unit(sd_bus *bus, const char *name, UnitActiveState *active_state); |
dbc2c080 | 180 | |
4fbd7192 LP |
181 | static bool original_stdout_is_tty; |
182 | ||
183 | typedef enum BusFocus { | |
184 | BUS_FULL, /* The full bus indicated via --system or --user */ | |
185 | BUS_MANAGER, /* The manager itself, possibly directly, possibly via the bus */ | |
186 | _BUS_FOCUS_MAX | |
187 | } BusFocus; | |
188 | ||
189 | static sd_bus *busses[_BUS_FOCUS_MAX] = {}; | |
190 | ||
b3796dd8 JS |
191 | static UnitFileFlags args_to_flags(void) { |
192 | return (arg_runtime ? UNIT_FILE_RUNTIME : 0) | | |
193 | (arg_force ? UNIT_FILE_FORCE : 0); | |
194 | } | |
195 | ||
4fbd7192 LP |
196 | static int acquire_bus(BusFocus focus, sd_bus **ret) { |
197 | int r; | |
198 | ||
199 | assert(focus < _BUS_FOCUS_MAX); | |
200 | assert(ret); | |
201 | ||
202 | /* We only go directly to the manager, if we are using a local transport */ | |
203 | if (arg_transport != BUS_TRANSPORT_LOCAL) | |
204 | focus = BUS_FULL; | |
205 | ||
d4015567 LP |
206 | if (getenv_bool("SYSTEMCTL_FORCE_BUS") > 0) |
207 | focus = BUS_FULL; | |
208 | ||
4fbd7192 LP |
209 | if (!busses[focus]) { |
210 | bool user; | |
211 | ||
212 | user = arg_scope != UNIT_FILE_SYSTEM; | |
213 | ||
214 | if (focus == BUS_MANAGER) | |
215 | r = bus_connect_transport_systemd(arg_transport, arg_host, user, &busses[focus]); | |
216 | else | |
217 | r = bus_connect_transport(arg_transport, arg_host, user, &busses[focus]); | |
218 | if (r < 0) | |
219 | return log_error_errno(r, "Failed to connect to bus: %m"); | |
220 | ||
221 | (void) sd_bus_set_allow_interactive_authorization(busses[focus], arg_ask_password); | |
222 | } | |
223 | ||
224 | *ret = busses[focus]; | |
225 | return 0; | |
226 | } | |
227 | ||
228 | static void release_busses(void) { | |
229 | BusFocus w; | |
230 | ||
231 | for (w = 0; w < _BUS_FOCUS_MAX; w++) | |
232 | busses[w] = sd_bus_flush_close_unref(busses[w]); | |
233 | } | |
234 | ||
6bb92a16 | 235 | static void ask_password_agent_open_if_enabled(void) { |
501fc174 | 236 | |
729e3769 | 237 | /* Open the password agent as a child process if necessary */ |
501fc174 | 238 | |
1ae17672 ZJS |
239 | if (arg_dry_run) |
240 | return; | |
241 | ||
501fc174 LP |
242 | if (!arg_ask_password) |
243 | return; | |
715554e7 | 244 | |
729e3769 | 245 | if (arg_scope != UNIT_FILE_SYSTEM) |
501fc174 LP |
246 | return; |
247 | ||
cbc9fbd1 LP |
248 | if (arg_transport != BUS_TRANSPORT_LOCAL) |
249 | return; | |
250 | ||
6bb92a16 LP |
251 | ask_password_agent_open(); |
252 | } | |
253 | ||
8a4b13c5 | 254 | static void polkit_agent_open_maybe(void) { |
6bb92a16 LP |
255 | /* Open the polkit agent as a child process if necessary */ |
256 | ||
6bb92a16 LP |
257 | if (arg_scope != UNIT_FILE_SYSTEM) |
258 | return; | |
259 | ||
8a4b13c5 | 260 | polkit_agent_open_if_enabled(arg_transport, arg_ask_password); |
501fc174 LP |
261 | } |
262 | ||
3c756001 LP |
263 | static OutputFlags get_output_flags(void) { |
264 | return | |
265 | arg_all * OUTPUT_SHOW_ALL | | |
459b9f9f | 266 | (arg_full || !on_tty() || pager_have()) * OUTPUT_FULL_WIDTH | |
40c9fe4c | 267 | colors_enabled() * OUTPUT_COLOR | |
8b0cc9a3 | 268 | !arg_quiet * OUTPUT_WARN_CUTOFF; |
3c756001 LP |
269 | } |
270 | ||
f459b602 | 271 | static int translate_bus_error_to_exit_status(int r, const sd_bus_error *error) { |
22f4096c LP |
272 | assert(error); |
273 | ||
f459b602 | 274 | if (!sd_bus_error_is_set(error)) |
22f4096c LP |
275 | return r; |
276 | ||
f459b602 MAP |
277 | if (sd_bus_error_has_name(error, SD_BUS_ERROR_ACCESS_DENIED) || |
278 | sd_bus_error_has_name(error, BUS_ERROR_ONLY_BY_DEPENDENCY) || | |
279 | sd_bus_error_has_name(error, BUS_ERROR_NO_ISOLATION) || | |
280 | sd_bus_error_has_name(error, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE)) | |
22f4096c LP |
281 | return EXIT_NOPERMISSION; |
282 | ||
f459b602 | 283 | if (sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT)) |
22f4096c LP |
284 | return EXIT_NOTINSTALLED; |
285 | ||
f459b602 | 286 | if (sd_bus_error_has_name(error, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE) || |
718db961 | 287 | sd_bus_error_has_name(error, SD_BUS_ERROR_NOT_SUPPORTED)) |
22f4096c LP |
288 | return EXIT_NOTIMPLEMENTED; |
289 | ||
f459b602 | 290 | if (sd_bus_error_has_name(error, BUS_ERROR_LOAD_FAILED)) |
22f4096c LP |
291 | return EXIT_NOTCONFIGURED; |
292 | ||
293 | if (r != 0) | |
294 | return r; | |
295 | ||
296 | return EXIT_FAILURE; | |
297 | } | |
298 | ||
4fbd7192 | 299 | static bool install_client_side(void) { |
729e3769 | 300 | |
4fbd7192 LP |
301 | /* Decides when to execute enable/disable/... operations |
302 | * client-side rather than server-side. */ | |
4f16c1f4 | 303 | |
f38951a6 | 304 | if (running_in_chroot_or_offline()) |
729e3769 LP |
305 | return true; |
306 | ||
307 | if (sd_booted() <= 0) | |
308 | return true; | |
309 | ||
310 | if (!isempty(arg_root)) | |
311 | return true; | |
312 | ||
313 | if (arg_scope == UNIT_FILE_GLOBAL) | |
314 | return true; | |
315 | ||
b41b9d2a LP |
316 | /* Unsupported environment variable, mostly for debugging purposes */ |
317 | if (getenv_bool("SYSTEMCTL_INSTALL_CLIENT_SIDE") > 0) | |
318 | return true; | |
319 | ||
729e3769 LP |
320 | return false; |
321 | } | |
322 | ||
36c32ba2 | 323 | static int compare_unit_info(const void *a, const void *b) { |
f459b602 | 324 | const UnitInfo *u = a, *v = b; |
36c32ba2 | 325 | const char *d1, *d2; |
1238ee09 LP |
326 | int r; |
327 | ||
328 | /* First, order by machine */ | |
329 | if (!u->machine && v->machine) | |
330 | return -1; | |
331 | if (u->machine && !v->machine) | |
332 | return 1; | |
333 | if (u->machine && v->machine) { | |
334 | r = strcasecmp(u->machine, v->machine); | |
335 | if (r != 0) | |
336 | return r; | |
337 | } | |
36c32ba2 | 338 | |
1238ee09 | 339 | /* Second, order by unit type */ |
36c32ba2 LP |
340 | d1 = strrchr(u->id, '.'); |
341 | d2 = strrchr(v->id, '.'); | |
36c32ba2 | 342 | if (d1 && d2) { |
f84190d8 LP |
343 | r = strcasecmp(d1, d2); |
344 | if (r != 0) | |
36c32ba2 LP |
345 | return r; |
346 | } | |
347 | ||
1238ee09 | 348 | /* Third, order by name */ |
a2a3a5b9 | 349 | return strcasecmp(u->id, v->id); |
36c32ba2 LP |
350 | } |
351 | ||
16484a8a ZJS |
352 | static const char* unit_type_suffix(const char *name) { |
353 | const char *dot; | |
354 | ||
355 | dot = strrchr(name, '.'); | |
356 | if (!dot) | |
357 | return ""; | |
358 | ||
359 | return dot + 1; | |
360 | } | |
361 | ||
d8fba7c6 | 362 | static bool output_show_unit(const UnitInfo *u, char **patterns) { |
f8591ee1 LP |
363 | assert(u); |
364 | ||
2404701e | 365 | if (!strv_fnmatch_or_empty(patterns, u->id, FNM_NOESCAPE)) |
d8fba7c6 | 366 | return false; |
d8fba7c6 | 367 | |
16484a8a ZJS |
368 | if (arg_types && !strv_find(arg_types, unit_type_suffix(u->id))) |
369 | return false; | |
6c71341a ZJS |
370 | |
371 | if (arg_all) | |
372 | return true; | |
373 | ||
409472cb FB |
374 | /* Note that '--all' is not purely a state filter, but also a |
375 | * filter that hides units that "follow" other units (which is | |
376 | * used for device units that appear under different names). */ | |
377 | if (!isempty(u->following)) | |
378 | return false; | |
379 | ||
ebc96265 FB |
380 | if (!strv_isempty(arg_states)) |
381 | return true; | |
382 | ||
383 | /* By default show all units except the ones in inactive | |
384 | * state and with no pending job */ | |
6c71341a ZJS |
385 | if (u->job_id > 0) |
386 | return true; | |
387 | ||
409472cb | 388 | if (streq(u->active_state, "inactive")) |
6c71341a ZJS |
389 | return false; |
390 | ||
391 | return true; | |
33330222 ZJS |
392 | } |
393 | ||
1238ee09 | 394 | static int output_units_list(const UnitInfo *unit_infos, unsigned c) { |
b5d7f1bb | 395 | unsigned circle_len = 0, id_len, max_id_len, load_len, active_len, sub_len, job_len, desc_len, max_desc_len; |
f459b602 | 396 | const UnitInfo *u; |
a1074881 | 397 | unsigned n_shown = 0; |
b5d7f1bb | 398 | int job_count = 0; |
33330222 | 399 | |
fbd0b64f LP |
400 | max_id_len = STRLEN("UNIT"); |
401 | load_len = STRLEN("LOAD"); | |
402 | active_len = STRLEN("ACTIVE"); | |
403 | sub_len = STRLEN("SUB"); | |
404 | job_len = STRLEN("JOB"); | |
405 | max_desc_len = STRLEN("DESCRIPTION"); | |
b036fc00 LP |
406 | |
407 | for (u = unit_infos; u < unit_infos + c; u++) { | |
1238ee09 | 408 | max_id_len = MAX(max_id_len, strlen(u->id) + (u->machine ? strlen(u->machine)+1 : 0)); |
a1074881 | 409 | load_len = MAX(load_len, strlen(u->load_state)); |
b036fc00 LP |
410 | active_len = MAX(active_len, strlen(u->active_state)); |
411 | sub_len = MAX(sub_len, strlen(u->sub_state)); | |
b5d7f1bb | 412 | max_desc_len = MAX(max_desc_len, strlen(u->description)); |
f459b602 | 413 | |
ccd41387 | 414 | if (u->job_id != 0) { |
b036fc00 | 415 | job_len = MAX(job_len, strlen(u->job_type)); |
ccd41387 ZJS |
416 | job_count++; |
417 | } | |
90c3f79d LP |
418 | |
419 | if (!arg_no_legend && | |
420 | (streq(u->active_state, "failed") || | |
421 | STR_IN_SET(u->load_state, "error", "not-found", "masked"))) | |
422 | circle_len = 2; | |
33330222 ZJS |
423 | } |
424 | ||
184ecaf7 | 425 | if (!arg_full && original_stdout_is_tty) { |
4deb3b93 | 426 | unsigned basic_len; |
cbc9fbd1 | 427 | |
b5d7f1bb | 428 | id_len = MIN(max_id_len, 25u); /* as much as it needs, but at most 25 for now */ |
a9bd056a | 429 | basic_len = circle_len + 1 + id_len + 1 + load_len + 1 + active_len + 1 + sub_len + 1; |
cbc9fbd1 | 430 | |
ccd41387 ZJS |
431 | if (job_count) |
432 | basic_len += job_len + 1; | |
cbc9fbd1 | 433 | |
4deb3b93 MS |
434 | if (basic_len < (unsigned) columns()) { |
435 | unsigned extra_len, incr; | |
436 | extra_len = columns() - basic_len; | |
cbc9fbd1 | 437 | |
4deb3b93 MS |
438 | /* Either UNIT already got 25, or is fully satisfied. |
439 | * Grant up to 25 to DESC now. */ | |
9607d947 | 440 | incr = MIN(extra_len, 25u); |
b5d7f1bb | 441 | desc_len = incr; |
4deb3b93 | 442 | extra_len -= incr; |
cbc9fbd1 | 443 | |
b5d7f1bb LP |
444 | /* Of the remainder give as much as the ID needs to the ID, and give the rest to the |
445 | * description but not more than it needs. */ | |
4deb3b93 | 446 | if (extra_len > 0) { |
b5d7f1bb | 447 | incr = MIN(max_id_len - id_len, extra_len); |
4deb3b93 | 448 | id_len += incr; |
b5d7f1bb | 449 | desc_len += MIN(extra_len - incr, max_desc_len - desc_len); |
4deb3b93 | 450 | } |
43479f8d ZJS |
451 | } else |
452 | desc_len = 0; | |
b5d7f1bb | 453 | } else { |
4deb3b93 | 454 | id_len = max_id_len; |
b5d7f1bb LP |
455 | desc_len = max_desc_len; |
456 | } | |
4deb3b93 | 457 | |
b036fc00 | 458 | for (u = unit_infos; u < unit_infos + c; u++) { |
1238ee09 | 459 | _cleanup_free_ char *e = NULL, *j = NULL; |
16484a8a | 460 | const char *on_underline = "", *off_underline = ""; |
90c3f79d LP |
461 | const char *on_loaded = "", *off_loaded = ""; |
462 | const char *on_active = "", *off_active = ""; | |
463 | const char *on_circle = "", *off_circle = ""; | |
1238ee09 | 464 | const char *id; |
16484a8a | 465 | bool circle = false, underline = false; |
b036fc00 | 466 | |
ad94ad63 | 467 | if (!n_shown && !arg_no_legend) { |
90c3f79d LP |
468 | |
469 | if (circle_len > 0) | |
470 | fputs(" ", stdout); | |
471 | ||
16484a8a ZJS |
472 | printf("%s%-*s %-*s %-*s %-*s ", |
473 | ansi_underline(), | |
cbc9fbd1 LP |
474 | id_len, "UNIT", |
475 | load_len, "LOAD", | |
476 | active_len, "ACTIVE", | |
477 | sub_len, "SUB"); | |
478 | ||
ccd41387 ZJS |
479 | if (job_count) |
480 | printf("%-*s ", job_len, "JOB"); | |
cbc9fbd1 | 481 | |
b5d7f1bb LP |
482 | printf("%-*.*s%s\n", |
483 | desc_len, | |
484 | !arg_full && arg_no_pager ? (int) desc_len : -1, | |
16484a8a ZJS |
485 | "DESCRIPTION", |
486 | ansi_normal()); | |
ad94ad63 ZJS |
487 | } |
488 | ||
688c6725 LP |
489 | n_shown++; |
490 | ||
16484a8a ZJS |
491 | if (u + 1 < unit_infos + c && |
492 | !streq(unit_type_suffix(u->id), unit_type_suffix((u + 1)->id))) { | |
493 | on_underline = ansi_underline(); | |
494 | off_underline = ansi_normal(); | |
495 | underline = true; | |
496 | } | |
497 | ||
250ba664 | 498 | if (STR_IN_SET(u->load_state, "error", "not-found", "masked") && !arg_plain) { |
90c3f79d | 499 | on_circle = ansi_highlight_yellow(); |
16484a8a | 500 | off_circle = ansi_normal(); |
90c3f79d | 501 | circle = true; |
16484a8a | 502 | on_loaded = underline ? ansi_highlight_red_underline() : ansi_highlight_red(); |
835a19e0 | 503 | off_loaded = underline ? on_underline : ansi_normal(); |
250ba664 | 504 | } else if (streq(u->active_state, "failed") && !arg_plain) { |
16484a8a ZJS |
505 | on_circle = ansi_highlight_red(); |
506 | off_circle = ansi_normal(); | |
90c3f79d | 507 | circle = true; |
16484a8a | 508 | on_active = underline ? ansi_highlight_red_underline() : ansi_highlight_red(); |
835a19e0 | 509 | off_active = underline ? on_underline : ansi_normal(); |
90c3f79d | 510 | } |
eb68c413 | 511 | |
1238ee09 | 512 | if (u->machine) { |
605405c6 | 513 | j = strjoin(u->machine, ":", u->id); |
1238ee09 LP |
514 | if (!j) |
515 | return log_oom(); | |
516 | ||
517 | id = j; | |
518 | } else | |
519 | id = u->id; | |
520 | ||
521 | if (arg_full) { | |
522 | e = ellipsize(id, id_len, 33); | |
523 | if (!e) | |
524 | return log_oom(); | |
525 | ||
526 | id = e; | |
527 | } | |
eb68c413 | 528 | |
90c3f79d | 529 | if (circle_len > 0) |
323b7dc9 | 530 | printf("%s%s%s ", on_circle, circle ? special_glyph(BLACK_CIRCLE) : " ", off_circle); |
90c3f79d | 531 | |
16484a8a ZJS |
532 | printf("%s%s%-*s%s %s%-*s%s %s%-*s %-*s%s %-*s", |
533 | on_underline, | |
90c3f79d | 534 | on_active, id_len, id, off_active, |
a1074881 | 535 | on_loaded, load_len, u->load_state, off_loaded, |
b036fc00 LP |
536 | on_active, active_len, u->active_state, |
537 | sub_len, u->sub_state, off_active, | |
ccd41387 | 538 | job_count ? job_len + 1 : 0, u->job_id ? u->job_type : ""); |
cbc9fbd1 | 539 | |
b5d7f1bb LP |
540 | printf("%-*.*s%s\n", |
541 | desc_len, | |
542 | !arg_full && arg_no_pager ? (int) desc_len : -1, | |
16484a8a ZJS |
543 | u->description, |
544 | off_underline); | |
eb68c413 ZJS |
545 | } |
546 | ||
ebed32bf | 547 | if (!arg_no_legend) { |
57f7ae4f ZJS |
548 | const char *on, *off; |
549 | ||
550 | if (n_shown) { | |
90c3f79d LP |
551 | puts("\n" |
552 | "LOAD = Reflects whether the unit definition was properly loaded.\n" | |
e3e0314b ZJS |
553 | "ACTIVE = The high-level unit activation state, i.e. generalization of SUB.\n" |
554 | "SUB = The low-level unit activation state, values depend on unit type."); | |
555 | puts(job_count ? "JOB = Pending job for the unit.\n" : ""); | |
0b5a519c | 556 | on = ansi_highlight(); |
1fc464f6 | 557 | off = ansi_normal(); |
57f7ae4f | 558 | } else { |
0b5a519c | 559 | on = ansi_highlight_red(); |
1fc464f6 | 560 | off = ansi_normal(); |
57f7ae4f | 561 | } |
eb68c413 ZJS |
562 | |
563 | if (arg_all) | |
48c2826b | 564 | printf("%s%u loaded units listed.%s\n" |
57f7ae4f ZJS |
565 | "To show all installed unit files use 'systemctl list-unit-files'.\n", |
566 | on, n_shown, off); | |
eb68c413 | 567 | else |
48c2826b | 568 | printf("%s%u loaded units listed.%s Pass --all to see loaded but inactive units, too.\n" |
57f7ae4f ZJS |
569 | "To show all installed unit files use 'systemctl list-unit-files'.\n", |
570 | on, n_shown, off); | |
eb68c413 | 571 | } |
1238ee09 LP |
572 | |
573 | return 0; | |
eb68c413 ZJS |
574 | } |
575 | ||
a00963a2 | 576 | static int get_unit_list( |
f459b602 | 577 | sd_bus *bus, |
1238ee09 LP |
578 | const char *machine, |
579 | char **patterns, | |
580 | UnitInfo **unit_infos, | |
581 | int c, | |
582 | sd_bus_message **_reply) { | |
a00963a2 | 583 | |
4afd3348 LP |
584 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
585 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
586 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
ca2d3784 | 587 | size_t size = c; |
1238ee09 | 588 | int r; |
f459b602 | 589 | UnitInfo u; |
313fe66f | 590 | bool fallback = false; |
7e4249b9 | 591 | |
265a7a2a | 592 | assert(bus); |
1238ee09 | 593 | assert(unit_infos); |
f459b602 | 594 | assert(_reply); |
1238ee09 | 595 | |
cdc06ed7 | 596 | r = sd_bus_message_new_method_call( |
f22f08cd | 597 | bus, |
cdc06ed7 | 598 | &m, |
f22f08cd SP |
599 | "org.freedesktop.systemd1", |
600 | "/org/freedesktop/systemd1", | |
601 | "org.freedesktop.systemd1.Manager", | |
313fe66f | 602 | "ListUnitsByPatterns"); |
cdc06ed7 DS |
603 | if (r < 0) |
604 | return bus_log_create_error(r); | |
605 | ||
606 | r = sd_bus_message_append_strv(m, arg_states); | |
607 | if (r < 0) | |
608 | return bus_log_create_error(r); | |
609 | ||
313fe66f | 610 | r = sd_bus_message_append_strv(m, patterns); |
611 | if (r < 0) | |
612 | return bus_log_create_error(r); | |
613 | ||
cdc06ed7 | 614 | r = sd_bus_call(bus, m, 0, &error, &reply); |
33d52725 ZJS |
615 | if (r < 0 && (sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_METHOD) || |
616 | sd_bus_error_has_name(&error, SD_BUS_ERROR_ACCESS_DENIED))) { | |
313fe66f | 617 | /* Fallback to legacy ListUnitsFiltered method */ |
618 | fallback = true; | |
619 | log_debug_errno(r, "Failed to list units: %s Falling back to ListUnitsFiltered method.", bus_error_message(&error, r)); | |
620 | m = sd_bus_message_unref(m); | |
621 | sd_bus_error_free(&error); | |
622 | ||
623 | r = sd_bus_message_new_method_call( | |
624 | bus, | |
625 | &m, | |
626 | "org.freedesktop.systemd1", | |
627 | "/org/freedesktop/systemd1", | |
628 | "org.freedesktop.systemd1.Manager", | |
629 | "ListUnitsFiltered"); | |
630 | if (r < 0) | |
631 | return bus_log_create_error(r); | |
632 | ||
633 | r = sd_bus_message_append_strv(m, arg_states); | |
634 | if (r < 0) | |
635 | return bus_log_create_error(r); | |
636 | ||
637 | r = sd_bus_call(bus, m, 0, &error, &reply); | |
638 | } | |
4c3e8e39 LP |
639 | if (r < 0) |
640 | return log_error_errno(r, "Failed to list units: %s", bus_error_message(&error, r)); | |
7e4249b9 | 641 | |
f459b602 MAP |
642 | r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssssouso)"); |
643 | if (r < 0) | |
644 | return bus_log_parse_error(r); | |
7e4249b9 | 645 | |
f459b602 | 646 | while ((r = bus_parse_unit_info(reply, &u)) > 0) { |
1238ee09 LP |
647 | u.machine = machine; |
648 | ||
313fe66f | 649 | if (!output_show_unit(&u, fallback ? patterns : NULL)) |
8d5ba5a9 | 650 | continue; |
7e4249b9 | 651 | |
1238ee09 | 652 | if (!GREEDY_REALLOC(*unit_infos, size, c+1)) |
f459b602 | 653 | return log_oom(); |
36c32ba2 | 654 | |
1238ee09 | 655 | (*unit_infos)[c++] = u; |
991f2a39 | 656 | } |
f459b602 MAP |
657 | if (r < 0) |
658 | return bus_log_parse_error(r); | |
991f2a39 | 659 | |
f459b602 MAP |
660 | r = sd_bus_message_exit_container(reply); |
661 | if (r < 0) | |
662 | return bus_log_parse_error(r); | |
663 | ||
1cc6c93a | 664 | *_reply = TAKE_PTR(reply); |
f459b602 | 665 | |
1238ee09 LP |
666 | return c; |
667 | } | |
668 | ||
669 | static void message_set_freep(Set **set) { | |
224b0e7a | 670 | set_free_with_destructor(*set, sd_bus_message_unref); |
1238ee09 LP |
671 | } |
672 | ||
673 | static int get_unit_list_recursive( | |
674 | sd_bus *bus, | |
675 | char **patterns, | |
676 | UnitInfo **_unit_infos, | |
677 | Set **_replies, | |
678 | char ***_machines) { | |
679 | ||
680 | _cleanup_free_ UnitInfo *unit_infos = NULL; | |
681 | _cleanup_(message_set_freep) Set *replies; | |
682 | sd_bus_message *reply; | |
683 | int c, r; | |
684 | ||
685 | assert(bus); | |
686 | assert(_replies); | |
687 | assert(_unit_infos); | |
688 | assert(_machines); | |
689 | ||
d5099efc | 690 | replies = set_new(NULL); |
1238ee09 LP |
691 | if (!replies) |
692 | return log_oom(); | |
693 | ||
694 | c = get_unit_list(bus, NULL, patterns, &unit_infos, 0, &reply); | |
695 | if (c < 0) | |
696 | return c; | |
697 | ||
698 | r = set_put(replies, reply); | |
699 | if (r < 0) { | |
700 | sd_bus_message_unref(reply); | |
4c3e8e39 | 701 | return log_oom(); |
1238ee09 LP |
702 | } |
703 | ||
704 | if (arg_recursive) { | |
705 | _cleanup_strv_free_ char **machines = NULL; | |
706 | char **i; | |
707 | ||
708 | r = sd_get_machine_names(&machines); | |
709 | if (r < 0) | |
4c3e8e39 | 710 | return log_error_errno(r, "Failed to get machine names: %m"); |
1238ee09 LP |
711 | |
712 | STRV_FOREACH(i, machines) { | |
4afd3348 | 713 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *container = NULL; |
1238ee09 LP |
714 | int k; |
715 | ||
de33fc62 | 716 | r = sd_bus_open_system_machine(&container, *i); |
1238ee09 | 717 | if (r < 0) { |
4c3e8e39 | 718 | log_warning_errno(r, "Failed to connect to container %s, ignoring: %m", *i); |
1238ee09 LP |
719 | continue; |
720 | } | |
721 | ||
722 | k = get_unit_list(container, *i, patterns, &unit_infos, c, &reply); | |
723 | if (k < 0) | |
724 | return k; | |
725 | ||
726 | c = k; | |
727 | ||
728 | r = set_put(replies, reply); | |
729 | if (r < 0) { | |
730 | sd_bus_message_unref(reply); | |
4c3e8e39 | 731 | return log_oom(); |
1238ee09 LP |
732 | } |
733 | } | |
734 | ||
1cc6c93a | 735 | *_machines = TAKE_PTR(machines); |
1238ee09 LP |
736 | } else |
737 | *_machines = NULL; | |
738 | ||
1cc6c93a | 739 | *_unit_infos = TAKE_PTR(unit_infos); |
f459b602 | 740 | |
1cc6c93a | 741 | *_replies = TAKE_PTR(replies); |
1238ee09 | 742 | |
f459b602 | 743 | return c; |
991f2a39 ZJS |
744 | } |
745 | ||
e449de87 | 746 | static int list_units(int argc, char *argv[], void *userdata) { |
f459b602 | 747 | _cleanup_free_ UnitInfo *unit_infos = NULL; |
1238ee09 LP |
748 | _cleanup_(message_set_freep) Set *replies = NULL; |
749 | _cleanup_strv_free_ char **machines = NULL; | |
4fbd7192 | 750 | sd_bus *bus; |
991f2a39 ZJS |
751 | int r; |
752 | ||
4fbd7192 LP |
753 | r = acquire_bus(BUS_MANAGER, &bus); |
754 | if (r < 0) | |
755 | return r; | |
756 | ||
ee5324aa | 757 | (void) pager_open(arg_no_pager, false); |
d2ad7e1b | 758 | |
e449de87 | 759 | r = get_unit_list_recursive(bus, strv_skip(argv, 1), &unit_infos, &replies, &machines); |
991f2a39 ZJS |
760 | if (r < 0) |
761 | return r; | |
762 | ||
f459b602 | 763 | qsort_safe(unit_infos, r, sizeof(UnitInfo), compare_unit_info); |
1238ee09 | 764 | return output_units_list(unit_infos, r); |
991f2a39 ZJS |
765 | } |
766 | ||
a00963a2 | 767 | static int get_triggered_units( |
f459b602 MAP |
768 | sd_bus *bus, |
769 | const char* path, | |
770 | char*** ret) { | |
a00963a2 | 771 | |
4afd3348 | 772 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
991f2a39 ZJS |
773 | int r; |
774 | ||
4fbd7192 LP |
775 | assert(bus); |
776 | assert(path); | |
777 | assert(ret); | |
778 | ||
f459b602 MAP |
779 | r = sd_bus_get_property_strv( |
780 | bus, | |
781 | "org.freedesktop.systemd1", | |
782 | path, | |
783 | "org.freedesktop.systemd1.Unit", | |
784 | "Triggers", | |
785 | &error, | |
786 | ret); | |
f459b602 | 787 | if (r < 0) |
691395d8 | 788 | return log_error_errno(r, "Failed to determine triggers: %s", bus_error_message(&error, r)); |
991f2a39 ZJS |
789 | |
790 | return 0; | |
791 | } | |
792 | ||
f459b602 MAP |
793 | static int get_listening( |
794 | sd_bus *bus, | |
795 | const char* unit_path, | |
cbb76c29 | 796 | char*** listening) { |
f459b602 | 797 | |
4afd3348 LP |
798 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
799 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
f459b602 | 800 | const char *type, *path; |
cbb76c29 | 801 | int r, n = 0; |
991f2a39 | 802 | |
f459b602 MAP |
803 | r = sd_bus_get_property( |
804 | bus, | |
805 | "org.freedesktop.systemd1", | |
806 | unit_path, | |
807 | "org.freedesktop.systemd1.Socket", | |
808 | "Listen", | |
809 | &error, | |
810 | &reply, | |
811 | "a(ss)"); | |
691395d8 LP |
812 | if (r < 0) |
813 | return log_error_errno(r, "Failed to get list of listening sockets: %s", bus_error_message(&error, r)); | |
991f2a39 | 814 | |
f459b602 MAP |
815 | r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ss)"); |
816 | if (r < 0) | |
817 | return bus_log_parse_error(r); | |
991f2a39 | 818 | |
f459b602 | 819 | while ((r = sd_bus_message_read(reply, "(ss)", &type, &path)) > 0) { |
36c32ba2 | 820 | |
0d95178e | 821 | r = strv_extend(listening, type); |
f459b602 MAP |
822 | if (r < 0) |
823 | return log_oom(); | |
991f2a39 | 824 | |
0d95178e | 825 | r = strv_extend(listening, path); |
f459b602 MAP |
826 | if (r < 0) |
827 | return log_oom(); | |
7e4249b9 | 828 | |
cbb76c29 | 829 | n++; |
36c32ba2 | 830 | } |
f459b602 MAP |
831 | if (r < 0) |
832 | return bus_log_parse_error(r); | |
833 | ||
834 | r = sd_bus_message_exit_container(reply); | |
835 | if (r < 0) | |
836 | return bus_log_parse_error(r); | |
36c32ba2 | 837 | |
cbb76c29 | 838 | return n; |
991f2a39 ZJS |
839 | } |
840 | ||
841 | struct socket_info { | |
0cfc3525 | 842 | const char *machine; |
991f2a39 ZJS |
843 | const char* id; |
844 | ||
845 | char* type; | |
846 | char* path; | |
847 | ||
848 | /* Note: triggered is a list here, although it almost certainly | |
849 | * will always be one unit. Nevertheless, dbus API allows for multiple | |
f7340ab2 | 850 | * values, so let's follow that. */ |
991f2a39 ZJS |
851 | char** triggered; |
852 | ||
853 | /* The strv above is shared. free is set only in the first one. */ | |
854 | bool own_triggered; | |
855 | }; | |
856 | ||
cbb76c29 | 857 | static int socket_info_compare(const struct socket_info *a, const struct socket_info *b) { |
cbc9fbd1 LP |
858 | int o; |
859 | ||
cbb76c29 LP |
860 | assert(a); |
861 | assert(b); | |
862 | ||
0cfc3525 TA |
863 | if (!a->machine && b->machine) |
864 | return -1; | |
865 | if (a->machine && !b->machine) | |
866 | return 1; | |
867 | if (a->machine && b->machine) { | |
868 | o = strcasecmp(a->machine, b->machine); | |
869 | if (o != 0) | |
870 | return o; | |
871 | } | |
872 | ||
cbc9fbd1 | 873 | o = strcmp(a->path, b->path); |
991f2a39 ZJS |
874 | if (o == 0) |
875 | o = strcmp(a->type, b->type); | |
cbc9fbd1 | 876 | |
991f2a39 ZJS |
877 | return o; |
878 | } | |
879 | ||
880 | static int output_sockets_list(struct socket_info *socket_infos, unsigned cs) { | |
881 | struct socket_info *s; | |
fbd0b64f LP |
882 | unsigned pathlen = STRLEN("LISTEN"), |
883 | typelen = STRLEN("TYPE") * arg_show_types, | |
884 | socklen = STRLEN("UNIT"), | |
885 | servlen = STRLEN("ACTIVATES"); | |
991f2a39 ZJS |
886 | const char *on, *off; |
887 | ||
888 | for (s = socket_infos; s < socket_infos + cs; s++) { | |
991f2a39 | 889 | unsigned tmp = 0; |
cbc9fbd1 | 890 | char **a; |
991f2a39 ZJS |
891 | |
892 | socklen = MAX(socklen, strlen(s->id)); | |
893 | if (arg_show_types) | |
894 | typelen = MAX(typelen, strlen(s->type)); | |
0cfc3525 | 895 | pathlen = MAX(pathlen, strlen(s->path) + (s->machine ? strlen(s->machine)+1 : 0)); |
991f2a39 ZJS |
896 | |
897 | STRV_FOREACH(a, s->triggered) | |
898 | tmp += strlen(*a) + 2*(a != s->triggered); | |
899 | servlen = MAX(servlen, tmp); | |
900 | } | |
901 | ||
902 | if (cs) { | |
571bfc6c MM |
903 | if (!arg_no_legend) |
904 | printf("%-*s %-*.*s%-*s %s\n", | |
905 | pathlen, "LISTEN", | |
906 | typelen + arg_show_types, typelen + arg_show_types, "TYPE ", | |
907 | socklen, "UNIT", | |
908 | "ACTIVATES"); | |
991f2a39 ZJS |
909 | |
910 | for (s = socket_infos; s < socket_infos + cs; s++) { | |
0cfc3525 TA |
911 | _cleanup_free_ char *j = NULL; |
912 | const char *path; | |
991f2a39 ZJS |
913 | char **a; |
914 | ||
0cfc3525 | 915 | if (s->machine) { |
605405c6 | 916 | j = strjoin(s->machine, ":", s->path); |
0cfc3525 TA |
917 | if (!j) |
918 | return log_oom(); | |
919 | path = j; | |
920 | } else | |
921 | path = s->path; | |
922 | ||
991f2a39 ZJS |
923 | if (arg_show_types) |
924 | printf("%-*s %-*s %-*s", | |
0cfc3525 | 925 | pathlen, path, typelen, s->type, socklen, s->id); |
991f2a39 ZJS |
926 | else |
927 | printf("%-*s %-*s", | |
0cfc3525 | 928 | pathlen, path, socklen, s->id); |
991f2a39 ZJS |
929 | STRV_FOREACH(a, s->triggered) |
930 | printf("%s %s", | |
931 | a == s->triggered ? "" : ",", *a); | |
932 | printf("\n"); | |
933 | } | |
934 | ||
0b5a519c | 935 | on = ansi_highlight(); |
1fc464f6 | 936 | off = ansi_normal(); |
571bfc6c MM |
937 | if (!arg_no_legend) |
938 | printf("\n"); | |
991f2a39 | 939 | } else { |
0b5a519c | 940 | on = ansi_highlight_red(); |
1fc464f6 | 941 | off = ansi_normal(); |
991f2a39 ZJS |
942 | } |
943 | ||
571bfc6c MM |
944 | if (!arg_no_legend) { |
945 | printf("%s%u sockets listed.%s\n", on, cs, off); | |
946 | if (!arg_all) | |
947 | printf("Pass --all to see loaded but inactive sockets, too.\n"); | |
948 | } | |
265a7a2a ZJS |
949 | |
950 | return 0; | |
951 | } | |
952 | ||
e449de87 | 953 | static int list_sockets(int argc, char *argv[], void *userdata) { |
0cfc3525 TA |
954 | _cleanup_(message_set_freep) Set *replies = NULL; |
955 | _cleanup_strv_free_ char **machines = NULL; | |
f459b602 | 956 | _cleanup_free_ UnitInfo *unit_infos = NULL; |
8d5ba5a9 | 957 | _cleanup_free_ struct socket_info *socket_infos = NULL; |
f459b602 | 958 | const UnitInfo *u; |
991f2a39 | 959 | struct socket_info *s; |
cbb76c29 | 960 | unsigned cs = 0; |
991f2a39 | 961 | size_t size = 0; |
ad83b4c4 | 962 | int r = 0, n; |
4fbd7192 | 963 | sd_bus *bus; |
265a7a2a | 964 | |
4fbd7192 LP |
965 | r = acquire_bus(BUS_MANAGER, &bus); |
966 | if (r < 0) | |
967 | return r; | |
968 | ||
ee5324aa | 969 | (void) pager_open(arg_no_pager, false); |
d2ad7e1b | 970 | |
e449de87 | 971 | n = get_unit_list_recursive(bus, strv_skip(argv, 1), &unit_infos, &replies, &machines); |
cbb76c29 LP |
972 | if (n < 0) |
973 | return n; | |
265a7a2a | 974 | |
cbb76c29 | 975 | for (u = unit_infos; u < unit_infos + n; u++) { |
0d95178e | 976 | _cleanup_strv_free_ char **listening = NULL, **triggered = NULL; |
cbb76c29 | 977 | int i, c; |
991f2a39 | 978 | |
cbc9fbd1 | 979 | if (!endswith(u->id, ".socket")) |
991f2a39 ZJS |
980 | continue; |
981 | ||
982 | r = get_triggered_units(bus, u->unit_path, &triggered); | |
983 | if (r < 0) | |
984 | goto cleanup; | |
985 | ||
cbb76c29 LP |
986 | c = get_listening(bus, u->unit_path, &listening); |
987 | if (c < 0) { | |
988 | r = c; | |
991f2a39 | 989 | goto cleanup; |
cbb76c29 | 990 | } |
991f2a39 ZJS |
991 | |
992 | if (!GREEDY_REALLOC(socket_infos, size, cs + c)) { | |
993 | r = log_oom(); | |
994 | goto cleanup; | |
995 | } | |
996 | ||
997 | for (i = 0; i < c; i++) | |
998 | socket_infos[cs + i] = (struct socket_info) { | |
0cfc3525 | 999 | .machine = u->machine, |
991f2a39 | 1000 | .id = u->id, |
0d95178e KS |
1001 | .type = listening[i*2], |
1002 | .path = listening[i*2 + 1], | |
991f2a39 ZJS |
1003 | .triggered = triggered, |
1004 | .own_triggered = i==0, | |
1005 | }; | |
1006 | ||
1007 | /* from this point on we will cleanup those socket_infos */ | |
1008 | cs += c; | |
0d95178e KS |
1009 | free(listening); |
1010 | listening = triggered = NULL; /* avoid cleanup */ | |
991f2a39 ZJS |
1011 | } |
1012 | ||
7ff7394d ZJS |
1013 | qsort_safe(socket_infos, cs, sizeof(struct socket_info), |
1014 | (__compar_fn_t) socket_info_compare); | |
991f2a39 ZJS |
1015 | |
1016 | output_sockets_list(socket_infos, cs); | |
1017 | ||
1018 | cleanup: | |
1019 | assert(cs == 0 || socket_infos); | |
1020 | for (s = socket_infos; s < socket_infos + cs; s++) { | |
1021 | free(s->type); | |
1022 | free(s->path); | |
1023 | if (s->own_triggered) | |
1024 | strv_free(s->triggered); | |
1025 | } | |
4445a875 | 1026 | |
872c8faa | 1027 | return r; |
4445a875 LP |
1028 | } |
1029 | ||
cbb76c29 LP |
1030 | static int get_next_elapse( |
1031 | sd_bus *bus, | |
1032 | const char *path, | |
1033 | dual_timestamp *next) { | |
1034 | ||
4afd3348 | 1035 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
cbb76c29 LP |
1036 | dual_timestamp t; |
1037 | int r; | |
1038 | ||
1039 | assert(bus); | |
1040 | assert(path); | |
1041 | assert(next); | |
1042 | ||
1043 | r = sd_bus_get_property_trivial( | |
1044 | bus, | |
1045 | "org.freedesktop.systemd1", | |
1046 | path, | |
1047 | "org.freedesktop.systemd1.Timer", | |
1048 | "NextElapseUSecMonotonic", | |
1049 | &error, | |
1050 | 't', | |
1051 | &t.monotonic); | |
691395d8 | 1052 | if (r < 0) |
021723c6 | 1053 | return log_error_errno(r, "Failed to get next elapse time: %s", bus_error_message(&error, r)); |
cbb76c29 LP |
1054 | |
1055 | r = sd_bus_get_property_trivial( | |
1056 | bus, | |
1057 | "org.freedesktop.systemd1", | |
1058 | path, | |
1059 | "org.freedesktop.systemd1.Timer", | |
1060 | "NextElapseUSecRealtime", | |
1061 | &error, | |
1062 | 't', | |
1063 | &t.realtime); | |
691395d8 | 1064 | if (r < 0) |
021723c6 | 1065 | return log_error_errno(r, "Failed to get next elapse time: %s", bus_error_message(&error, r)); |
cbb76c29 LP |
1066 | |
1067 | *next = t; | |
1068 | return 0; | |
1069 | } | |
1070 | ||
d784e2db LP |
1071 | static int get_last_trigger( |
1072 | sd_bus *bus, | |
1073 | const char *path, | |
1074 | usec_t *last) { | |
1075 | ||
4afd3348 | 1076 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
d784e2db LP |
1077 | int r; |
1078 | ||
1079 | assert(bus); | |
1080 | assert(path); | |
1081 | assert(last); | |
1082 | ||
1083 | r = sd_bus_get_property_trivial( | |
1084 | bus, | |
1085 | "org.freedesktop.systemd1", | |
1086 | path, | |
1087 | "org.freedesktop.systemd1.Timer", | |
dedabea4 | 1088 | "LastTriggerUSec", |
d784e2db LP |
1089 | &error, |
1090 | 't', | |
1091 | last); | |
691395d8 LP |
1092 | if (r < 0) |
1093 | return log_error_errno(r, "Failed to get last trigger time: %s", bus_error_message(&error, r)); | |
d784e2db LP |
1094 | |
1095 | return 0; | |
1096 | } | |
1097 | ||
cbb76c29 | 1098 | struct timer_info { |
806a37e7 | 1099 | const char* machine; |
cbb76c29 LP |
1100 | const char* id; |
1101 | usec_t next_elapse; | |
d784e2db | 1102 | usec_t last_trigger; |
cbb76c29 LP |
1103 | char** triggered; |
1104 | }; | |
1105 | ||
1106 | static int timer_info_compare(const struct timer_info *a, const struct timer_info *b) { | |
806a37e7 TA |
1107 | int o; |
1108 | ||
cbb76c29 LP |
1109 | assert(a); |
1110 | assert(b); | |
1111 | ||
806a37e7 TA |
1112 | if (!a->machine && b->machine) |
1113 | return -1; | |
1114 | if (a->machine && !b->machine) | |
1115 | return 1; | |
1116 | if (a->machine && b->machine) { | |
1117 | o = strcasecmp(a->machine, b->machine); | |
1118 | if (o != 0) | |
1119 | return o; | |
1120 | } | |
1121 | ||
cbb76c29 LP |
1122 | if (a->next_elapse < b->next_elapse) |
1123 | return -1; | |
1124 | if (a->next_elapse > b->next_elapse) | |
1125 | return 1; | |
1126 | ||
1127 | return strcmp(a->id, b->id); | |
1128 | } | |
1129 | ||
1130 | static int output_timers_list(struct timer_info *timer_infos, unsigned n) { | |
1131 | struct timer_info *t; | |
1132 | unsigned | |
fbd0b64f LP |
1133 | nextlen = STRLEN("NEXT"), |
1134 | leftlen = STRLEN("LEFT"), | |
1135 | lastlen = STRLEN("LAST"), | |
1136 | passedlen = STRLEN("PASSED"), | |
1137 | unitlen = STRLEN("UNIT"), | |
1138 | activatelen = STRLEN("ACTIVATES"); | |
cbb76c29 LP |
1139 | |
1140 | const char *on, *off; | |
1141 | ||
1142 | assert(timer_infos || n == 0); | |
1143 | ||
1144 | for (t = timer_infos; t < timer_infos + n; t++) { | |
1145 | unsigned ul = 0; | |
1146 | char **a; | |
1147 | ||
1148 | if (t->next_elapse > 0) { | |
1149 | char tstamp[FORMAT_TIMESTAMP_MAX] = "", trel[FORMAT_TIMESTAMP_RELATIVE_MAX] = ""; | |
1150 | ||
1151 | format_timestamp(tstamp, sizeof(tstamp), t->next_elapse); | |
1152 | nextlen = MAX(nextlen, strlen(tstamp) + 1); | |
1153 | ||
1154 | format_timestamp_relative(trel, sizeof(trel), t->next_elapse); | |
1155 | leftlen = MAX(leftlen, strlen(trel)); | |
1156 | } | |
1157 | ||
d784e2db LP |
1158 | if (t->last_trigger > 0) { |
1159 | char tstamp[FORMAT_TIMESTAMP_MAX] = "", trel[FORMAT_TIMESTAMP_RELATIVE_MAX] = ""; | |
1160 | ||
1161 | format_timestamp(tstamp, sizeof(tstamp), t->last_trigger); | |
1162 | lastlen = MAX(lastlen, strlen(tstamp) + 1); | |
1163 | ||
1164 | format_timestamp_relative(trel, sizeof(trel), t->last_trigger); | |
1165 | passedlen = MAX(passedlen, strlen(trel)); | |
1166 | } | |
1167 | ||
806a37e7 | 1168 | unitlen = MAX(unitlen, strlen(t->id) + (t->machine ? strlen(t->machine)+1 : 0)); |
cbb76c29 LP |
1169 | |
1170 | STRV_FOREACH(a, t->triggered) | |
1171 | ul += strlen(*a) + 2*(a != t->triggered); | |
d784e2db | 1172 | |
cbb76c29 LP |
1173 | activatelen = MAX(activatelen, ul); |
1174 | } | |
1175 | ||
1176 | if (n > 0) { | |
1177 | if (!arg_no_legend) | |
d784e2db LP |
1178 | printf("%-*s %-*s %-*s %-*s %-*s %s\n", |
1179 | nextlen, "NEXT", | |
1180 | leftlen, "LEFT", | |
1181 | lastlen, "LAST", | |
1182 | passedlen, "PASSED", | |
1183 | unitlen, "UNIT", | |
1184 | "ACTIVATES"); | |
cbb76c29 LP |
1185 | |
1186 | for (t = timer_infos; t < timer_infos + n; t++) { | |
806a37e7 TA |
1187 | _cleanup_free_ char *j = NULL; |
1188 | const char *unit; | |
d784e2db LP |
1189 | char tstamp1[FORMAT_TIMESTAMP_MAX] = "n/a", trel1[FORMAT_TIMESTAMP_RELATIVE_MAX] = "n/a"; |
1190 | char tstamp2[FORMAT_TIMESTAMP_MAX] = "n/a", trel2[FORMAT_TIMESTAMP_RELATIVE_MAX] = "n/a"; | |
cbb76c29 LP |
1191 | char **a; |
1192 | ||
d784e2db LP |
1193 | format_timestamp(tstamp1, sizeof(tstamp1), t->next_elapse); |
1194 | format_timestamp_relative(trel1, sizeof(trel1), t->next_elapse); | |
cbb76c29 | 1195 | |
d784e2db LP |
1196 | format_timestamp(tstamp2, sizeof(tstamp2), t->last_trigger); |
1197 | format_timestamp_relative(trel2, sizeof(trel2), t->last_trigger); | |
1198 | ||
806a37e7 | 1199 | if (t->machine) { |
605405c6 | 1200 | j = strjoin(t->machine, ":", t->id); |
806a37e7 TA |
1201 | if (!j) |
1202 | return log_oom(); | |
1203 | unit = j; | |
1204 | } else | |
1205 | unit = t->id; | |
1206 | ||
d784e2db | 1207 | printf("%-*s %-*s %-*s %-*s %-*s", |
806a37e7 | 1208 | nextlen, tstamp1, leftlen, trel1, lastlen, tstamp2, passedlen, trel2, unitlen, unit); |
cbb76c29 LP |
1209 | |
1210 | STRV_FOREACH(a, t->triggered) | |
1211 | printf("%s %s", | |
1212 | a == t->triggered ? "" : ",", *a); | |
1213 | printf("\n"); | |
1214 | } | |
1215 | ||
1216 | on = ansi_highlight(); | |
1fc464f6 | 1217 | off = ansi_normal(); |
cbb76c29 LP |
1218 | if (!arg_no_legend) |
1219 | printf("\n"); | |
1220 | } else { | |
1221 | on = ansi_highlight_red(); | |
1fc464f6 | 1222 | off = ansi_normal(); |
cbb76c29 LP |
1223 | } |
1224 | ||
1225 | if (!arg_no_legend) { | |
1226 | printf("%s%u timers listed.%s\n", on, n, off); | |
1227 | if (!arg_all) | |
1228 | printf("Pass --all to see loaded but inactive timers, too.\n"); | |
1229 | } | |
1230 | ||
1231 | return 0; | |
1232 | } | |
1233 | ||
f5080e73 DH |
1234 | static usec_t calc_next_elapse(dual_timestamp *nw, dual_timestamp *next) { |
1235 | usec_t next_elapse; | |
1236 | ||
1237 | assert(nw); | |
1238 | assert(next); | |
1239 | ||
3a43da28 | 1240 | if (next->monotonic != USEC_INFINITY && next->monotonic > 0) { |
f5080e73 DH |
1241 | usec_t converted; |
1242 | ||
1243 | if (next->monotonic > nw->monotonic) | |
1244 | converted = nw->realtime + (next->monotonic - nw->monotonic); | |
1245 | else | |
1246 | converted = nw->realtime - (nw->monotonic - next->monotonic); | |
1247 | ||
3a43da28 | 1248 | if (next->realtime != USEC_INFINITY && next->realtime > 0) |
f5080e73 DH |
1249 | next_elapse = MIN(converted, next->realtime); |
1250 | else | |
1251 | next_elapse = converted; | |
1252 | ||
1253 | } else | |
1254 | next_elapse = next->realtime; | |
1255 | ||
1256 | return next_elapse; | |
1257 | } | |
1258 | ||
e449de87 | 1259 | static int list_timers(int argc, char *argv[], void *userdata) { |
806a37e7 TA |
1260 | _cleanup_(message_set_freep) Set *replies = NULL; |
1261 | _cleanup_strv_free_ char **machines = NULL; | |
cbb76c29 LP |
1262 | _cleanup_free_ struct timer_info *timer_infos = NULL; |
1263 | _cleanup_free_ UnitInfo *unit_infos = NULL; | |
1264 | struct timer_info *t; | |
1265 | const UnitInfo *u; | |
1266 | size_t size = 0; | |
1823b86e | 1267 | int n, c = 0; |
cbb76c29 | 1268 | dual_timestamp nw; |
4fbd7192 | 1269 | sd_bus *bus; |
1823b86e | 1270 | int r = 0; |
cbb76c29 | 1271 | |
4fbd7192 LP |
1272 | r = acquire_bus(BUS_MANAGER, &bus); |
1273 | if (r < 0) | |
1274 | return r; | |
1275 | ||
ee5324aa | 1276 | (void) pager_open(arg_no_pager, false); |
d2ad7e1b | 1277 | |
e449de87 | 1278 | n = get_unit_list_recursive(bus, strv_skip(argv, 1), &unit_infos, &replies, &machines); |
cbb76c29 LP |
1279 | if (n < 0) |
1280 | return n; | |
1281 | ||
1282 | dual_timestamp_get(&nw); | |
1283 | ||
1284 | for (u = unit_infos; u < unit_infos + n; u++) { | |
1285 | _cleanup_strv_free_ char **triggered = NULL; | |
5cb14b37 | 1286 | dual_timestamp next = DUAL_TIMESTAMP_NULL; |
d784e2db | 1287 | usec_t m, last = 0; |
cbb76c29 | 1288 | |
cbb76c29 LP |
1289 | if (!endswith(u->id, ".timer")) |
1290 | continue; | |
1291 | ||
1292 | r = get_triggered_units(bus, u->unit_path, &triggered); | |
1293 | if (r < 0) | |
1294 | goto cleanup; | |
1295 | ||
1296 | r = get_next_elapse(bus, u->unit_path, &next); | |
1297 | if (r < 0) | |
1298 | goto cleanup; | |
1299 | ||
d784e2db LP |
1300 | get_last_trigger(bus, u->unit_path, &last); |
1301 | ||
cbb76c29 LP |
1302 | if (!GREEDY_REALLOC(timer_infos, size, c+1)) { |
1303 | r = log_oom(); | |
1304 | goto cleanup; | |
1305 | } | |
1306 | ||
f5080e73 DH |
1307 | m = calc_next_elapse(&nw, &next); |
1308 | ||
cbb76c29 | 1309 | timer_infos[c++] = (struct timer_info) { |
806a37e7 | 1310 | .machine = u->machine, |
cbb76c29 LP |
1311 | .id = u->id, |
1312 | .next_elapse = m, | |
d784e2db | 1313 | .last_trigger = last, |
1cc6c93a | 1314 | .triggered = TAKE_PTR(triggered), |
cbb76c29 | 1315 | }; |
cbb76c29 LP |
1316 | } |
1317 | ||
1318 | qsort_safe(timer_infos, c, sizeof(struct timer_info), | |
1319 | (__compar_fn_t) timer_info_compare); | |
1320 | ||
1321 | output_timers_list(timer_infos, c); | |
1322 | ||
1323 | cleanup: | |
1324 | for (t = timer_infos; t < timer_infos + c; t++) | |
1325 | strv_free(t->triggered); | |
1326 | ||
1327 | return r; | |
1328 | } | |
1329 | ||
729e3769 LP |
1330 | static int compare_unit_file_list(const void *a, const void *b) { |
1331 | const char *d1, *d2; | |
1332 | const UnitFileList *u = a, *v = b; | |
1333 | ||
1334 | d1 = strrchr(u->path, '.'); | |
1335 | d2 = strrchr(v->path, '.'); | |
1336 | ||
1337 | if (d1 && d2) { | |
1338 | int r; | |
1339 | ||
1340 | r = strcasecmp(d1, d2); | |
1341 | if (r != 0) | |
1342 | return r; | |
1343 | } | |
1344 | ||
2b6bf07d | 1345 | return strcasecmp(basename(u->path), basename(v->path)); |
729e3769 LP |
1346 | } |
1347 | ||
313fe66f | 1348 | static bool output_show_unit_file(const UnitFileList *u, char **states, char **patterns) { |
f8591ee1 LP |
1349 | assert(u); |
1350 | ||
2404701e | 1351 | if (!strv_fnmatch_or_empty(patterns, basename(u->path), FNM_NOESCAPE)) |
d8fba7c6 | 1352 | return false; |
d8fba7c6 | 1353 | |
6c71341a ZJS |
1354 | if (!strv_isempty(arg_types)) { |
1355 | const char *dot; | |
1356 | ||
1357 | dot = strrchr(u->path, '.'); | |
1358 | if (!dot) | |
1359 | return false; | |
1360 | ||
1361 | if (!strv_find(arg_types, dot+1)) | |
1362 | return false; | |
1363 | } | |
1364 | ||
313fe66f | 1365 | if (!strv_isempty(states) && |
1366 | !strv_find(states, unit_file_state_to_string(u->state))) | |
bceccd5e | 1367 | return false; |
fec1530e | 1368 | |
6c71341a | 1369 | return true; |
729e3769 LP |
1370 | } |
1371 | ||
8d5ba5a9 | 1372 | static void output_unit_file_list(const UnitFileList *units, unsigned c) { |
0d292f5e | 1373 | unsigned max_id_len, id_cols, state_cols; |
729e3769 LP |
1374 | const UnitFileList *u; |
1375 | ||
fbd0b64f LP |
1376 | max_id_len = STRLEN("UNIT FILE"); |
1377 | state_cols = STRLEN("STATE"); | |
cbc9fbd1 | 1378 | |
1c0a113f | 1379 | for (u = units; u < units + c; u++) { |
2b6bf07d | 1380 | max_id_len = MAX(max_id_len, strlen(basename(u->path))); |
1c0a113f ZJS |
1381 | state_cols = MAX(state_cols, strlen(unit_file_state_to_string(u->state))); |
1382 | } | |
1383 | ||
1384 | if (!arg_full) { | |
1385 | unsigned basic_cols; | |
cbc9fbd1 | 1386 | |
9607d947 | 1387 | id_cols = MIN(max_id_len, 25u); |
1c0a113f ZJS |
1388 | basic_cols = 1 + id_cols + state_cols; |
1389 | if (basic_cols < (unsigned) columns()) | |
1390 | id_cols += MIN(columns() - basic_cols, max_id_len - id_cols); | |
1391 | } else | |
1392 | id_cols = max_id_len; | |
1393 | ||
11690bcc | 1394 | if (!arg_no_legend && c > 0) |
6ae3e62a ZJS |
1395 | printf("%s%-*s %-*s%s\n", |
1396 | ansi_underline(), | |
cbc9fbd1 | 1397 | id_cols, "UNIT FILE", |
6ae3e62a ZJS |
1398 | state_cols, "STATE", |
1399 | ansi_normal()); | |
729e3769 LP |
1400 | |
1401 | for (u = units; u < units + c; u++) { | |
aaa6732d | 1402 | const char *on_underline = NULL, *on_color = NULL, *off = NULL, *id; |
7fd1b19b | 1403 | _cleanup_free_ char *e = NULL; |
aaa6732d LP |
1404 | bool underline; |
1405 | ||
1406 | underline = u + 1 < units + c && | |
1407 | !streq(unit_type_suffix(u->path), unit_type_suffix((u + 1)->path)); | |
6ae3e62a | 1408 | |
aaa6732d | 1409 | if (underline) |
6ae3e62a | 1410 | on_underline = ansi_underline(); |
729e3769 | 1411 | |
4c315c2c IS |
1412 | if (IN_SET(u->state, |
1413 | UNIT_FILE_MASKED, | |
1414 | UNIT_FILE_MASKED_RUNTIME, | |
1415 | UNIT_FILE_DISABLED, | |
6ae3e62a | 1416 | UNIT_FILE_BAD)) |
aaa6732d | 1417 | on_color = underline ? ansi_highlight_red_underline() : ansi_highlight_red(); |
6ae3e62a | 1418 | else if (u->state == UNIT_FILE_ENABLED) |
aaa6732d LP |
1419 | on_color = underline ? ansi_highlight_green_underline() : ansi_highlight_green(); |
1420 | ||
1421 | if (on_underline || on_color) | |
1422 | off = ansi_normal(); | |
729e3769 | 1423 | |
2b6bf07d | 1424 | id = basename(u->path); |
729e3769 | 1425 | |
1c0a113f | 1426 | e = arg_full ? NULL : ellipsize(id, id_cols, 33); |
729e3769 | 1427 | |
aaa6732d LP |
1428 | printf("%s%-*s %s%-*s%s\n", |
1429 | strempty(on_underline), | |
1c0a113f | 1430 | id_cols, e ? e : id, |
aaa6732d | 1431 | strempty(on_color), state_cols, unit_file_state_to_string(u->state), strempty(off)); |
729e3769 LP |
1432 | } |
1433 | ||
1c0a113f | 1434 | if (!arg_no_legend) |
0d292f5e | 1435 | printf("\n%u unit files listed.\n", c); |
729e3769 LP |
1436 | } |
1437 | ||
e449de87 | 1438 | static int list_unit_files(int argc, char *argv[], void *userdata) { |
4afd3348 | 1439 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
f84190d8 | 1440 | _cleanup_free_ UnitFileList *units = NULL; |
8d5ba5a9 ZJS |
1441 | UnitFileList *unit; |
1442 | size_t size = 0; | |
f459b602 MAP |
1443 | unsigned c = 0; |
1444 | const char *state; | |
1445 | char *path; | |
f84190d8 | 1446 | int r; |
313fe66f | 1447 | bool fallback = false; |
729e3769 | 1448 | |
4fbd7192 | 1449 | if (install_client_side()) { |
729e3769 LP |
1450 | Hashmap *h; |
1451 | UnitFileList *u; | |
1452 | Iterator i; | |
f459b602 | 1453 | unsigned n_units; |
729e3769 | 1454 | |
d5099efc | 1455 | h = hashmap_new(&string_hash_ops); |
0d0f0c50 SL |
1456 | if (!h) |
1457 | return log_oom(); | |
729e3769 | 1458 | |
313fe66f | 1459 | r = unit_file_get_list(arg_scope, arg_root, h, arg_states, strv_skip(argv, 1)); |
729e3769 | 1460 | if (r < 0) { |
8ea913b2 | 1461 | unit_file_list_free(h); |
691395d8 | 1462 | return log_error_errno(r, "Failed to get unit file list: %m"); |
729e3769 LP |
1463 | } |
1464 | ||
1465 | n_units = hashmap_size(h); | |
fdbdf6ec | 1466 | |
0da999fa ZJS |
1467 | units = new(UnitFileList, n_units ?: 1); /* avoid malloc(0) */ |
1468 | if (!units) { | |
729e3769 | 1469 | unit_file_list_free(h); |
0d0f0c50 | 1470 | return log_oom(); |
729e3769 LP |
1471 | } |
1472 | ||
1473 | HASHMAP_FOREACH(u, h, i) { | |
313fe66f | 1474 | if (!output_show_unit_file(u, NULL, NULL)) |
8d5ba5a9 ZJS |
1475 | continue; |
1476 | ||
1477 | units[c++] = *u; | |
729e3769 LP |
1478 | free(u); |
1479 | } | |
1480 | ||
8d5ba5a9 | 1481 | assert(c <= n_units); |
729e3769 | 1482 | hashmap_free(h); |
5f056378 CH |
1483 | |
1484 | r = 0; | |
729e3769 | 1485 | } else { |
313fe66f | 1486 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
4afd3348 | 1487 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
4fbd7192 LP |
1488 | sd_bus *bus; |
1489 | ||
1490 | r = acquire_bus(BUS_MANAGER, &bus); | |
1491 | if (r < 0) | |
1492 | return r; | |
6e646d22 | 1493 | |
313fe66f | 1494 | r = sd_bus_message_new_method_call( |
f22f08cd | 1495 | bus, |
313fe66f | 1496 | &m, |
729e3769 LP |
1497 | "org.freedesktop.systemd1", |
1498 | "/org/freedesktop/systemd1", | |
1499 | "org.freedesktop.systemd1.Manager", | |
313fe66f | 1500 | "ListUnitFilesByPatterns"); |
1501 | if (r < 0) | |
1502 | return bus_log_create_error(r); | |
1503 | ||
1504 | r = sd_bus_message_append_strv(m, arg_states); | |
1505 | if (r < 0) | |
1506 | return bus_log_create_error(r); | |
1507 | ||
1508 | r = sd_bus_message_append_strv(m, strv_skip(argv, 1)); | |
1509 | if (r < 0) | |
1510 | return bus_log_create_error(r); | |
1511 | ||
1512 | r = sd_bus_call(bus, m, 0, &error, &reply); | |
1513 | if (r < 0 && sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_METHOD)) { | |
1514 | /* Fallback to legacy ListUnitFiles method */ | |
1515 | fallback = true; | |
1516 | log_debug_errno(r, "Failed to list unit files: %s Falling back to ListUnitsFiles method.", bus_error_message(&error, r)); | |
1517 | m = sd_bus_message_unref(m); | |
1518 | sd_bus_error_free(&error); | |
1519 | ||
1520 | r = sd_bus_message_new_method_call( | |
1521 | bus, | |
1522 | &m, | |
1523 | "org.freedesktop.systemd1", | |
1524 | "/org/freedesktop/systemd1", | |
1525 | "org.freedesktop.systemd1.Manager", | |
1526 | "ListUnitFiles"); | |
1527 | if (r < 0) | |
1528 | return bus_log_create_error(r); | |
1529 | ||
1530 | r = sd_bus_call(bus, m, 0, &error, &reply); | |
1531 | } | |
691395d8 LP |
1532 | if (r < 0) |
1533 | return log_error_errno(r, "Failed to list unit files: %s", bus_error_message(&error, r)); | |
729e3769 | 1534 | |
f459b602 MAP |
1535 | r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ss)"); |
1536 | if (r < 0) | |
1537 | return bus_log_parse_error(r); | |
729e3769 | 1538 | |
f459b602 | 1539 | while ((r = sd_bus_message_read(reply, "(ss)", &path, &state)) > 0) { |
729e3769 | 1540 | |
f459b602 MAP |
1541 | if (!GREEDY_REALLOC(units, size, c + 1)) |
1542 | return log_oom(); | |
729e3769 | 1543 | |
8d5ba5a9 | 1544 | units[c] = (struct UnitFileList) { |
f459b602 MAP |
1545 | path, |
1546 | unit_file_state_from_string(state) | |
1547 | }; | |
8d5ba5a9 | 1548 | |
313fe66f | 1549 | if (output_show_unit_file(&units[c], |
1550 | fallback ? arg_states : NULL, | |
1551 | fallback ? strv_skip(argv, 1) : NULL)) | |
313cefa1 | 1552 | c++; |
8d5ba5a9 | 1553 | |
729e3769 | 1554 | } |
f459b602 MAP |
1555 | if (r < 0) |
1556 | return bus_log_parse_error(r); | |
1557 | ||
1558 | r = sd_bus_message_exit_container(reply); | |
1559 | if (r < 0) | |
1560 | return bus_log_parse_error(r); | |
729e3769 LP |
1561 | } |
1562 | ||
ee5324aa | 1563 | (void) pager_open(arg_no_pager, false); |
d2ad7e1b | 1564 | |
4fe1be9c LP |
1565 | qsort_safe(units, c, sizeof(UnitFileList), compare_unit_file_list); |
1566 | output_unit_file_list(units, c); | |
729e3769 | 1567 | |
0da999fa | 1568 | if (install_client_side()) |
8d5ba5a9 ZJS |
1569 | for (unit = units; unit < units + c; unit++) |
1570 | free(unit->path); | |
1571 | ||
f84190d8 | 1572 | return 0; |
729e3769 LP |
1573 | } |
1574 | ||
55c0b89c | 1575 | static int list_dependencies_print(const char *name, int level, unsigned int branches, bool last) { |
55c0b89c | 1576 | _cleanup_free_ char *n = NULL; |
9607d947 | 1577 | size_t max_len = MAX(columns(),20u); |
cbc9fbd1 LP |
1578 | size_t len = 0; |
1579 | int i; | |
55c0b89c | 1580 | |
5d0c05e5 | 1581 | if (!arg_plain) { |
cbc9fbd1 | 1582 | |
5d0c05e5 LN |
1583 | for (i = level - 1; i >= 0; i--) { |
1584 | len += 2; | |
f168c273 | 1585 | if (len > max_len - 3 && !arg_full) { |
5d0c05e5 LN |
1586 | printf("%s...\n",max_len % 2 ? "" : " "); |
1587 | return 0; | |
1588 | } | |
323b7dc9 | 1589 | printf("%s", special_glyph(branches & (1 << i) ? TREE_VERTICAL : TREE_SPACE)); |
5d0c05e5 | 1590 | } |
55c0b89c | 1591 | len += 2; |
cbc9fbd1 | 1592 | |
f168c273 | 1593 | if (len > max_len - 3 && !arg_full) { |
55c0b89c LN |
1594 | printf("%s...\n",max_len % 2 ? "" : " "); |
1595 | return 0; | |
1596 | } | |
cbc9fbd1 | 1597 | |
323b7dc9 | 1598 | printf("%s", special_glyph(last ? TREE_RIGHT : TREE_BRANCH)); |
55c0b89c | 1599 | } |
55c0b89c | 1600 | |
9ed794a3 | 1601 | if (arg_full) { |
55c0b89c LN |
1602 | printf("%s\n", name); |
1603 | return 0; | |
1604 | } | |
1605 | ||
1606 | n = ellipsize(name, max_len-len, 100); | |
f168c273 | 1607 | if (!n) |
55c0b89c LN |
1608 | return log_oom(); |
1609 | ||
1610 | printf("%s\n", n); | |
1611 | return 0; | |
1612 | } | |
1613 | ||
f459b602 MAP |
1614 | static int list_dependencies_get_dependencies(sd_bus *bus, const char *name, char ***deps) { |
1615 | ||
32b47279 YW |
1616 | struct DependencyStatusInfo { |
1617 | char **dep[5]; | |
1618 | } info = {}; | |
1619 | ||
1620 | static const struct bus_properties_map map[_DEPENDENCY_MAX][6] = { | |
1621 | [DEPENDENCY_FORWARD] = { | |
1622 | { "Requires", "as", NULL, offsetof(struct DependencyStatusInfo, dep[0]) }, | |
1623 | { "Requisite", "as", NULL, offsetof(struct DependencyStatusInfo, dep[1]) }, | |
1624 | { "Wants", "as", NULL, offsetof(struct DependencyStatusInfo, dep[2]) }, | |
1625 | { "ConsistsOf", "as", NULL, offsetof(struct DependencyStatusInfo, dep[3]) }, | |
1626 | { "BindsTo", "as", NULL, offsetof(struct DependencyStatusInfo, dep[4]) }, | |
1627 | {} | |
1628 | }, | |
1629 | [DEPENDENCY_REVERSE] = { | |
1630 | { "RequiredBy", "as", NULL, offsetof(struct DependencyStatusInfo, dep[0]) }, | |
1631 | { "RequisiteOf", "as", NULL, offsetof(struct DependencyStatusInfo, dep[1]) }, | |
1632 | { "WantedBy", "as", NULL, offsetof(struct DependencyStatusInfo, dep[2]) }, | |
1633 | { "PartOf", "as", NULL, offsetof(struct DependencyStatusInfo, dep[3]) }, | |
1634 | { "BoundBy", "as", NULL, offsetof(struct DependencyStatusInfo, dep[4]) }, | |
1635 | {} | |
1636 | }, | |
1637 | [DEPENDENCY_AFTER] = { | |
1638 | { "After", "as", NULL, offsetof(struct DependencyStatusInfo, dep[0]) }, | |
1639 | {} | |
1640 | }, | |
1641 | [DEPENDENCY_BEFORE] = { | |
1642 | { "Before", "as", NULL, offsetof(struct DependencyStatusInfo, dep[0]) }, | |
1643 | {} | |
1644 | }, | |
afba4199 | 1645 | }; |
55c0b89c | 1646 | |
4afd3348 | 1647 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
f459b602 MAP |
1648 | _cleanup_strv_free_ char **ret = NULL; |
1649 | _cleanup_free_ char *path = NULL; | |
32b47279 | 1650 | int i, r; |
55c0b89c LN |
1651 | |
1652 | assert(bus); | |
1653 | assert(name); | |
1654 | assert(deps); | |
1655 | ||
1656 | path = unit_dbus_path_from_name(name); | |
f459b602 MAP |
1657 | if (!path) |
1658 | return log_oom(); | |
55c0b89c | 1659 | |
32b47279 YW |
1660 | r = bus_map_all_properties(bus, |
1661 | "org.freedesktop.systemd1", | |
1662 | path, | |
1663 | map[arg_dependency], | |
f39cbb6e | 1664 | BUS_MAP_STRDUP, |
32b47279 | 1665 | &error, |
f37f8a61 | 1666 | NULL, |
32b47279 | 1667 | &info); |
691395d8 LP |
1668 | if (r < 0) |
1669 | return log_error_errno(r, "Failed to get properties of %s: %s", name, bus_error_message(&error, r)); | |
55c0b89c | 1670 | |
32b47279 YW |
1671 | if (IN_SET(arg_dependency, DEPENDENCY_AFTER, DEPENDENCY_BEFORE)) { |
1672 | *deps = info.dep[0]; | |
1673 | return 0; | |
1674 | } | |
55c0b89c | 1675 | |
32b47279 YW |
1676 | for (i = 0; i < 5; i++) { |
1677 | r = strv_extend_strv(&ret, info.dep[i], true); | |
f459b602 | 1678 | if (r < 0) |
32b47279 YW |
1679 | return log_oom(); |
1680 | info.dep[i] = strv_free(info.dep[i]); | |
f459b602 | 1681 | } |
540e7dbe | 1682 | |
1cc6c93a | 1683 | *deps = TAKE_PTR(ret); |
540e7dbe | 1684 | |
f459b602 | 1685 | return 0; |
55c0b89c LN |
1686 | } |
1687 | ||
1688 | static int list_dependencies_compare(const void *_a, const void *_b) { | |
1689 | const char **a = (const char**) _a, **b = (const char**) _b; | |
cbc9fbd1 | 1690 | |
55c0b89c LN |
1691 | if (unit_name_to_type(*a) == UNIT_TARGET && unit_name_to_type(*b) != UNIT_TARGET) |
1692 | return 1; | |
1693 | if (unit_name_to_type(*a) != UNIT_TARGET && unit_name_to_type(*b) == UNIT_TARGET) | |
1694 | return -1; | |
cbc9fbd1 | 1695 | |
55c0b89c LN |
1696 | return strcasecmp(*a, *b); |
1697 | } | |
1698 | ||
f459b602 MAP |
1699 | static int list_dependencies_one( |
1700 | sd_bus *bus, | |
1701 | const char *name, | |
1702 | int level, | |
1703 | char ***units, | |
1704 | unsigned int branches) { | |
1705 | ||
e3e45d4f | 1706 | _cleanup_strv_free_ char **deps = NULL; |
55c0b89c | 1707 | char **c; |
55c0b89c LN |
1708 | int r = 0; |
1709 | ||
cbc9fbd1 LP |
1710 | assert(bus); |
1711 | assert(name); | |
1712 | assert(units); | |
1713 | ||
e3e45d4f SP |
1714 | r = strv_extend(units, name); |
1715 | if (r < 0) | |
55c0b89c LN |
1716 | return log_oom(); |
1717 | ||
1718 | r = list_dependencies_get_dependencies(bus, name, &deps); | |
1719 | if (r < 0) | |
cec7eda5 | 1720 | return r; |
55c0b89c | 1721 | |
7ff7394d | 1722 | qsort_safe(deps, strv_length(deps), sizeof (char*), list_dependencies_compare); |
55c0b89c LN |
1723 | |
1724 | STRV_FOREACH(c, deps) { | |
e3e45d4f | 1725 | if (strv_contains(*units, *c)) { |
5d0c05e5 | 1726 | if (!arg_plain) { |
60705040 | 1727 | printf(" "); |
5d0c05e5 LN |
1728 | r = list_dependencies_print("...", level + 1, (branches << 1) | (c[1] == NULL ? 0 : 1), 1); |
1729 | if (r < 0) | |
1730 | return r; | |
1731 | } | |
55c0b89c LN |
1732 | continue; |
1733 | } | |
1734 | ||
250ba664 ZJS |
1735 | if (arg_plain) |
1736 | printf(" "); | |
1737 | else { | |
fa0d5878 | 1738 | UnitActiveState active_state = _UNIT_ACTIVE_STATE_INVALID; |
250ba664 ZJS |
1739 | const char *on; |
1740 | ||
fa0d5878 | 1741 | (void) get_state_one_unit(bus, *c, &active_state); |
baa9ecc1 | 1742 | |
fa0d5878 | 1743 | switch (active_state) { |
baa9ecc1 LP |
1744 | case UNIT_ACTIVE: |
1745 | case UNIT_RELOADING: | |
1746 | case UNIT_ACTIVATING: | |
1747 | on = ansi_highlight_green(); | |
1748 | break; | |
1749 | ||
1750 | case UNIT_INACTIVE: | |
1751 | case UNIT_DEACTIVATING: | |
1752 | on = ansi_normal(); | |
1753 | break; | |
1754 | ||
1755 | default: | |
1756 | on = ansi_highlight_red(); | |
1757 | break; | |
fa0d5878 BR |
1758 | } |
1759 | ||
323b7dc9 | 1760 | printf("%s%s%s ", on, special_glyph(BLACK_CIRCLE), ansi_normal()); |
250ba664 | 1761 | } |
dbc2c080 | 1762 | |
55c0b89c | 1763 | r = list_dependencies_print(*c, level, branches, c[1] == NULL); |
cec7eda5 ZJS |
1764 | if (r < 0) |
1765 | return r; | |
55c0b89c LN |
1766 | |
1767 | if (arg_all || unit_name_to_type(*c) == UNIT_TARGET) { | |
e3e45d4f | 1768 | r = list_dependencies_one(bus, *c, level + 1, units, (branches << 1) | (c[1] == NULL ? 0 : 1)); |
f168c273 | 1769 | if (r < 0) |
cec7eda5 | 1770 | return r; |
55c0b89c LN |
1771 | } |
1772 | } | |
f459b602 | 1773 | |
e3e45d4f SP |
1774 | if (!arg_plain) |
1775 | strv_remove(*units, name); | |
f459b602 | 1776 | |
cec7eda5 | 1777 | return 0; |
55c0b89c LN |
1778 | } |
1779 | ||
e449de87 | 1780 | static int list_dependencies(int argc, char *argv[], void *userdata) { |
5d0c05e5 | 1781 | _cleanup_strv_free_ char **units = NULL; |
f459b602 | 1782 | _cleanup_free_ char *unit = NULL; |
e31165b2 | 1783 | const char *u; |
4fbd7192 | 1784 | sd_bus *bus; |
7410616c | 1785 | int r; |
55c0b89c | 1786 | |
e449de87 | 1787 | if (argv[1]) { |
37cbc1d5 | 1788 | r = unit_name_mangle(argv[1], arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN, &unit); |
7410616c LP |
1789 | if (r < 0) |
1790 | return log_error_errno(r, "Failed to mangle unit name: %m"); | |
1791 | ||
e31165b2 LP |
1792 | u = unit; |
1793 | } else | |
1794 | u = SPECIAL_DEFAULT_TARGET; | |
55c0b89c | 1795 | |
4fbd7192 LP |
1796 | r = acquire_bus(BUS_MANAGER, &bus); |
1797 | if (r < 0) | |
1798 | return r; | |
1799 | ||
ee5324aa | 1800 | (void) pager_open(arg_no_pager, false); |
d2ad7e1b | 1801 | |
e31165b2 LP |
1802 | puts(u); |
1803 | ||
5d0c05e5 | 1804 | return list_dependencies_one(bus, u, 0, &units, 0); |
55c0b89c LN |
1805 | } |
1806 | ||
0d292f5e LP |
1807 | struct machine_info { |
1808 | bool is_host; | |
1809 | char *name; | |
0d292f5e | 1810 | char *state; |
8fcf784d LP |
1811 | char *control_group; |
1812 | uint32_t n_failed_units; | |
1813 | uint32_t n_jobs; | |
1814 | usec_t timestamp; | |
1815 | }; | |
1816 | ||
1817 | static const struct bus_properties_map machine_info_property_map[] = { | |
1818 | { "SystemState", "s", NULL, offsetof(struct machine_info, state) }, | |
1819 | { "NJobs", "u", NULL, offsetof(struct machine_info, n_jobs) }, | |
1820 | { "NFailedUnits", "u", NULL, offsetof(struct machine_info, n_failed_units) }, | |
1821 | { "ControlGroup", "s", NULL, offsetof(struct machine_info, control_group) }, | |
1822 | { "UserspaceTimestamp", "t", NULL, offsetof(struct machine_info, timestamp) }, | |
1823 | {} | |
0d292f5e LP |
1824 | }; |
1825 | ||
e7e55dbd | 1826 | static void machine_info_clear(struct machine_info *info) { |
f8654baa ZJS |
1827 | assert(info); |
1828 | ||
1829 | free(info->name); | |
1830 | free(info->state); | |
1831 | free(info->control_group); | |
1832 | zero(*info); | |
e7e55dbd DH |
1833 | } |
1834 | ||
0d292f5e LP |
1835 | static void free_machines_list(struct machine_info *machine_infos, int n) { |
1836 | int i; | |
1837 | ||
1838 | if (!machine_infos) | |
1839 | return; | |
1840 | ||
e7e55dbd DH |
1841 | for (i = 0; i < n; i++) |
1842 | machine_info_clear(&machine_infos[i]); | |
0d292f5e LP |
1843 | |
1844 | free(machine_infos); | |
1845 | } | |
1846 | ||
1847 | static int compare_machine_info(const void *a, const void *b) { | |
1848 | const struct machine_info *u = a, *v = b; | |
1849 | ||
1850 | if (u->is_host != v->is_host) | |
50933da0 | 1851 | return u->is_host > v->is_host ? -1 : 1; |
0d292f5e LP |
1852 | |
1853 | return strcasecmp(u->name, v->name); | |
1854 | } | |
1855 | ||
1856 | static int get_machine_properties(sd_bus *bus, struct machine_info *mi) { | |
4afd3348 | 1857 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *container = NULL; |
0d292f5e LP |
1858 | int r; |
1859 | ||
1860 | assert(mi); | |
1861 | ||
1862 | if (!bus) { | |
de33fc62 | 1863 | r = sd_bus_open_system_machine(&container, mi->name); |
0d292f5e LP |
1864 | if (r < 0) |
1865 | return r; | |
1866 | ||
1867 | bus = container; | |
1868 | } | |
1869 | ||
a7e4861c YW |
1870 | r = bus_map_all_properties( |
1871 | bus, | |
1872 | "org.freedesktop.systemd1", | |
1873 | "/org/freedesktop/systemd1", | |
1874 | machine_info_property_map, | |
1875 | BUS_MAP_STRDUP, | |
1876 | NULL, | |
1877 | NULL, | |
1878 | mi); | |
0d292f5e LP |
1879 | if (r < 0) |
1880 | return r; | |
1881 | ||
1882 | return 0; | |
1883 | } | |
1884 | ||
1885 | static bool output_show_machine(const char *name, char **patterns) { | |
2404701e | 1886 | return strv_fnmatch_or_empty(patterns, name, FNM_NOESCAPE); |
0d292f5e LP |
1887 | } |
1888 | ||
1889 | static int get_machine_list( | |
1890 | sd_bus *bus, | |
1891 | struct machine_info **_machine_infos, | |
1892 | char **patterns) { | |
1893 | ||
1894 | struct machine_info *machine_infos = NULL; | |
1895 | _cleanup_strv_free_ char **m = NULL; | |
1896 | _cleanup_free_ char *hn = NULL; | |
1897 | size_t sz = 0; | |
1898 | char **i; | |
4c3e8e39 | 1899 | int c = 0, r; |
0d292f5e LP |
1900 | |
1901 | hn = gethostname_malloc(); | |
1902 | if (!hn) | |
1903 | return log_oom(); | |
1904 | ||
1905 | if (output_show_machine(hn, patterns)) { | |
1906 | if (!GREEDY_REALLOC0(machine_infos, sz, c+1)) | |
1907 | return log_oom(); | |
1908 | ||
1909 | machine_infos[c].is_host = true; | |
ae2a15bc | 1910 | machine_infos[c].name = TAKE_PTR(hn); |
0d292f5e | 1911 | |
f9e0eefc | 1912 | (void) get_machine_properties(bus, &machine_infos[c]); |
0d292f5e LP |
1913 | c++; |
1914 | } | |
1915 | ||
4c3e8e39 LP |
1916 | r = sd_get_machine_names(&m); |
1917 | if (r < 0) | |
1918 | return log_error_errno(r, "Failed to get machine list: %m"); | |
1919 | ||
0d292f5e LP |
1920 | STRV_FOREACH(i, m) { |
1921 | _cleanup_free_ char *class = NULL; | |
1922 | ||
1923 | if (!output_show_machine(*i, patterns)) | |
1924 | continue; | |
1925 | ||
1926 | sd_machine_get_class(*i, &class); | |
1927 | if (!streq_ptr(class, "container")) | |
1928 | continue; | |
1929 | ||
1930 | if (!GREEDY_REALLOC0(machine_infos, sz, c+1)) { | |
1931 | free_machines_list(machine_infos, c); | |
1932 | return log_oom(); | |
1933 | } | |
1934 | ||
1935 | machine_infos[c].is_host = false; | |
1936 | machine_infos[c].name = strdup(*i); | |
1937 | if (!machine_infos[c].name) { | |
1938 | free_machines_list(machine_infos, c); | |
1939 | return log_oom(); | |
1940 | } | |
1941 | ||
f9e0eefc | 1942 | (void) get_machine_properties(NULL, &machine_infos[c]); |
0d292f5e LP |
1943 | c++; |
1944 | } | |
1945 | ||
1946 | *_machine_infos = machine_infos; | |
1947 | return c; | |
1948 | } | |
1949 | ||
1950 | static void output_machines_list(struct machine_info *machine_infos, unsigned n) { | |
1951 | struct machine_info *m; | |
1952 | unsigned | |
90c3f79d | 1953 | circle_len = 0, |
fbd0b64f LP |
1954 | namelen = STRLEN("NAME"), |
1955 | statelen = STRLEN("STATE"), | |
1956 | failedlen = STRLEN("FAILED"), | |
1957 | jobslen = STRLEN("JOBS"); | |
0d292f5e LP |
1958 | |
1959 | assert(machine_infos || n == 0); | |
1960 | ||
1961 | for (m = machine_infos; m < machine_infos + n; m++) { | |
fbd0b64f LP |
1962 | namelen = MAX(namelen, |
1963 | strlen(m->name) + (m->is_host ? STRLEN(" (host)") : 0)); | |
7bf7ce28 | 1964 | statelen = MAX(statelen, strlen_ptr(m->state)); |
0d292f5e LP |
1965 | failedlen = MAX(failedlen, DECIMAL_STR_WIDTH(m->n_failed_units)); |
1966 | jobslen = MAX(jobslen, DECIMAL_STR_WIDTH(m->n_jobs)); | |
90c3f79d | 1967 | |
250ba664 | 1968 | if (!arg_plain && !streq_ptr(m->state, "running")) |
90c3f79d | 1969 | circle_len = 2; |
0d292f5e LP |
1970 | } |
1971 | ||
90c3f79d LP |
1972 | if (!arg_no_legend) { |
1973 | if (circle_len > 0) | |
1974 | fputs(" ", stdout); | |
1975 | ||
0d292f5e LP |
1976 | printf("%-*s %-*s %-*s %-*s\n", |
1977 | namelen, "NAME", | |
1978 | statelen, "STATE", | |
1979 | failedlen, "FAILED", | |
1980 | jobslen, "JOBS"); | |
90c3f79d | 1981 | } |
0d292f5e LP |
1982 | |
1983 | for (m = machine_infos; m < machine_infos + n; m++) { | |
90c3f79d LP |
1984 | const char *on_state = "", *off_state = ""; |
1985 | const char *on_failed = "", *off_failed = ""; | |
1986 | bool circle = false; | |
0d292f5e LP |
1987 | |
1988 | if (streq_ptr(m->state, "degraded")) { | |
1989 | on_state = ansi_highlight_red(); | |
1fc464f6 | 1990 | off_state = ansi_normal(); |
90c3f79d | 1991 | circle = true; |
0d292f5e LP |
1992 | } else if (!streq_ptr(m->state, "running")) { |
1993 | on_state = ansi_highlight_yellow(); | |
1fc464f6 | 1994 | off_state = ansi_normal(); |
90c3f79d LP |
1995 | circle = true; |
1996 | } | |
0d292f5e LP |
1997 | |
1998 | if (m->n_failed_units > 0) { | |
1999 | on_failed = ansi_highlight_red(); | |
1fc464f6 | 2000 | off_failed = ansi_normal(); |
0d292f5e LP |
2001 | } else |
2002 | on_failed = off_failed = ""; | |
2003 | ||
90c3f79d | 2004 | if (circle_len > 0) |
323b7dc9 | 2005 | printf("%s%s%s ", on_state, circle ? special_glyph(BLACK_CIRCLE) : " ", off_state); |
90c3f79d | 2006 | |
0d292f5e | 2007 | if (m->is_host) |
c6ba5b80 | 2008 | printf("%-*s (host) %s%-*s%s %s%*" PRIu32 "%s %*" PRIu32 "\n", |
fbd0b64f LP |
2009 | (int) (namelen - (STRLEN(" (host)"))), |
2010 | strna(m->name), | |
0d292f5e LP |
2011 | on_state, statelen, strna(m->state), off_state, |
2012 | on_failed, failedlen, m->n_failed_units, off_failed, | |
2013 | jobslen, m->n_jobs); | |
2014 | else | |
c6ba5b80 | 2015 | printf("%-*s %s%-*s%s %s%*" PRIu32 "%s %*" PRIu32 "\n", |
0d292f5e LP |
2016 | namelen, strna(m->name), |
2017 | on_state, statelen, strna(m->state), off_state, | |
2018 | on_failed, failedlen, m->n_failed_units, off_failed, | |
2019 | jobslen, m->n_jobs); | |
2020 | } | |
2021 | ||
2022 | if (!arg_no_legend) | |
2023 | printf("\n%u machines listed.\n", n); | |
2024 | } | |
2025 | ||
e449de87 | 2026 | static int list_machines(int argc, char *argv[], void *userdata) { |
0d292f5e | 2027 | struct machine_info *machine_infos = NULL; |
4fbd7192 | 2028 | sd_bus *bus; |
0d292f5e LP |
2029 | int r; |
2030 | ||
4fbd7192 LP |
2031 | r = acquire_bus(BUS_MANAGER, &bus); |
2032 | if (r < 0) | |
2033 | return r; | |
2034 | ||
e449de87 | 2035 | r = get_machine_list(bus, &machine_infos, strv_skip(argv, 1)); |
0d292f5e LP |
2036 | if (r < 0) |
2037 | return r; | |
2038 | ||
ee5324aa | 2039 | (void) pager_open(arg_no_pager, false); |
d2ad7e1b | 2040 | |
0d292f5e LP |
2041 | qsort_safe(machine_infos, r, sizeof(struct machine_info), compare_machine_info); |
2042 | output_machines_list(machine_infos, r); | |
2043 | free_machines_list(machine_infos, r); | |
2044 | ||
2045 | return 0; | |
2046 | } | |
2047 | ||
e449de87 | 2048 | static int get_default(int argc, char *argv[], void *userdata) { |
4afd3348 | 2049 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
f459b602 MAP |
2050 | _cleanup_free_ char *_path = NULL; |
2051 | const char *path; | |
99504dd4 | 2052 | int r; |
99504dd4 | 2053 | |
4fbd7192 | 2054 | if (install_client_side()) { |
f459b602 | 2055 | r = unit_file_get_default(arg_scope, arg_root, &_path); |
f647962d MS |
2056 | if (r < 0) |
2057 | return log_error_errno(r, "Failed to get default target: %m"); | |
f459b602 | 2058 | path = _path; |
99504dd4 | 2059 | |
5f056378 | 2060 | r = 0; |
99504dd4 | 2061 | } else { |
4afd3348 | 2062 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
4fbd7192 LP |
2063 | sd_bus *bus; |
2064 | ||
2065 | r = acquire_bus(BUS_MANAGER, &bus); | |
2066 | if (r < 0) | |
2067 | return r; | |
6e646d22 | 2068 | |
f459b602 MAP |
2069 | r = sd_bus_call_method( |
2070 | bus, | |
2071 | "org.freedesktop.systemd1", | |
2072 | "/org/freedesktop/systemd1", | |
2073 | "org.freedesktop.systemd1.Manager", | |
2074 | "GetDefaultTarget", | |
2075 | &error, | |
2076 | &reply, | |
2077 | NULL); | |
691395d8 LP |
2078 | if (r < 0) |
2079 | return log_error_errno(r, "Failed to get default target: %s", bus_error_message(&error, r)); | |
99504dd4 | 2080 | |
f459b602 MAP |
2081 | r = sd_bus_message_read(reply, "s", &path); |
2082 | if (r < 0) | |
2083 | return bus_log_parse_error(r); | |
99504dd4 VP |
2084 | } |
2085 | ||
2086 | if (path) | |
2087 | printf("%s\n", path); | |
2088 | ||
f459b602 | 2089 | return 0; |
99504dd4 VP |
2090 | } |
2091 | ||
e449de87 | 2092 | static int set_default(int argc, char *argv[], void *userdata) { |
718db961 | 2093 | _cleanup_free_ char *unit = NULL; |
acc0269c | 2094 | UnitFileChange *changes = NULL; |
da6053d0 | 2095 | size_t n_changes = 0; |
718db961 LP |
2096 | int r; |
2097 | ||
e449de87 LP |
2098 | assert(argc >= 2); |
2099 | assert(argv); | |
2100 | ||
37cbc1d5 | 2101 | r = unit_name_mangle_with_suffix(argv[1], arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN, ".target", &unit); |
7410616c LP |
2102 | if (r < 0) |
2103 | return log_error_errno(r, "Failed to mangle unit name: %m"); | |
718db961 | 2104 | |
4fbd7192 | 2105 | if (install_client_side()) { |
b3796dd8 | 2106 | r = unit_file_set_default(arg_scope, UNIT_FILE_FORCE, arg_root, unit, &changes, &n_changes); |
af3d8113 | 2107 | unit_file_dump_changes(r, "set default", changes, n_changes, arg_quiet); |
5f056378 CH |
2108 | |
2109 | if (r > 0) | |
2110 | r = 0; | |
718db961 | 2111 | } else { |
4afd3348 LP |
2112 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
2113 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
4fbd7192 | 2114 | sd_bus *bus; |
718db961 | 2115 | |
8a4b13c5 | 2116 | polkit_agent_open_maybe(); |
6e646d22 | 2117 | |
4fbd7192 LP |
2118 | r = acquire_bus(BUS_MANAGER, &bus); |
2119 | if (r < 0) | |
2120 | return r; | |
2121 | ||
6e646d22 | 2122 | r = sd_bus_call_method( |
718db961 LP |
2123 | bus, |
2124 | "org.freedesktop.systemd1", | |
2125 | "/org/freedesktop/systemd1", | |
2126 | "org.freedesktop.systemd1.Manager", | |
6e646d22 LP |
2127 | "SetDefaultTarget", |
2128 | &error, | |
2129 | &reply, | |
2130 | "sb", unit, 1); | |
691395d8 LP |
2131 | if (r < 0) |
2132 | return log_error_errno(r, "Failed to set default target: %s", bus_error_message(&error, r)); | |
718db961 | 2133 | |
acc0269c | 2134 | r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet, &changes, &n_changes); |
718db961 | 2135 | if (r < 0) |
acc0269c | 2136 | goto finish; |
718db961 | 2137 | |
93c941e3 | 2138 | /* Try to reload if enabled */ |
718db961 | 2139 | if (!arg_no_reload) |
e449de87 | 2140 | r = daemon_reload(argc, argv, userdata); |
718db961 LP |
2141 | else |
2142 | r = 0; | |
2143 | } | |
2144 | ||
acc0269c CH |
2145 | finish: |
2146 | unit_file_changes_free(changes, n_changes); | |
2147 | ||
718db961 LP |
2148 | return r; |
2149 | } | |
2150 | ||
76d8ca22 | 2151 | static int output_waiting_jobs(sd_bus *bus, uint32_t id, const char *method, const char *prefix) { |
82948f6c LP |
2152 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
2153 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
2154 | const char *name, *type, *state, *job_path, *unit_path; | |
2155 | uint32_t other_id; | |
2156 | int r; | |
2157 | ||
2158 | assert(bus); | |
2159 | ||
2160 | r = sd_bus_call_method( | |
2161 | bus, | |
2162 | "org.freedesktop.systemd1", | |
2163 | "/org/freedesktop/systemd1", | |
2164 | "org.freedesktop.systemd1.Manager", | |
2165 | method, | |
2166 | &error, | |
2167 | &reply, | |
2168 | "u", id); | |
76d8ca22 ZJS |
2169 | if (r < 0) |
2170 | return log_debug_errno(r, "Failed to get waiting jobs for job %" PRIu32, id); | |
82948f6c LP |
2171 | |
2172 | r = sd_bus_message_enter_container(reply, 'a', "(usssoo)"); | |
76d8ca22 ZJS |
2173 | if (r < 0) |
2174 | return bus_log_parse_error(r); | |
82948f6c LP |
2175 | |
2176 | while ((r = sd_bus_message_read(reply, "(usssoo)", &other_id, &name, &type, &state, &job_path, &unit_path)) > 0) | |
bc357ce5 | 2177 | printf("%s %u (%s/%s)\n", prefix, other_id, name, type); |
76d8ca22 ZJS |
2178 | if (r < 0) |
2179 | return bus_log_parse_error(r); | |
82948f6c LP |
2180 | |
2181 | r = sd_bus_message_exit_container(reply); | |
76d8ca22 ZJS |
2182 | if (r < 0) |
2183 | return bus_log_parse_error(r); | |
2184 | ||
2185 | return 0; | |
82948f6c LP |
2186 | } |
2187 | ||
75add28a ZJS |
2188 | struct job_info { |
2189 | uint32_t id; | |
f459b602 | 2190 | const char *name, *type, *state; |
75add28a ZJS |
2191 | }; |
2192 | ||
82948f6c | 2193 | static void output_jobs_list(sd_bus *bus, const struct job_info* jobs, unsigned n, bool skipped) { |
f459b602 MAP |
2194 | unsigned id_len, unit_len, type_len, state_len; |
2195 | const struct job_info *j; | |
75add28a ZJS |
2196 | const char *on, *off; |
2197 | bool shorten = false; | |
2198 | ||
2199 | assert(n == 0 || jobs); | |
2200 | ||
2201 | if (n == 0) { | |
250ba664 ZJS |
2202 | if (!arg_no_legend) { |
2203 | on = ansi_highlight_green(); | |
1fc464f6 | 2204 | off = ansi_normal(); |
75add28a | 2205 | |
250ba664 ZJS |
2206 | printf("%sNo jobs %s.%s\n", on, skipped ? "listed" : "running", off); |
2207 | } | |
75add28a ZJS |
2208 | return; |
2209 | } | |
2210 | ||
ee5324aa | 2211 | (void) pager_open(arg_no_pager, false); |
75add28a | 2212 | |
fbd0b64f LP |
2213 | id_len = STRLEN("JOB"); |
2214 | unit_len = STRLEN("UNIT"); | |
2215 | type_len = STRLEN("TYPE"); | |
2216 | state_len = STRLEN("STATE"); | |
6d6d40c9 | 2217 | |
f459b602 MAP |
2218 | for (j = jobs; j < jobs + n; j++) { |
2219 | uint32_t id = j->id; | |
2220 | assert(j->name && j->type && j->state); | |
75add28a | 2221 | |
f459b602 MAP |
2222 | id_len = MAX(id_len, DECIMAL_STR_WIDTH(id)); |
2223 | unit_len = MAX(unit_len, strlen(j->name)); | |
2224 | type_len = MAX(type_len, strlen(j->type)); | |
2225 | state_len = MAX(state_len, strlen(j->state)); | |
2226 | } | |
75add28a | 2227 | |
f459b602 MAP |
2228 | if (!arg_full && id_len + 1 + unit_len + type_len + 1 + state_len > columns()) { |
2229 | unit_len = MAX(33u, columns() - id_len - type_len - state_len - 3); | |
2230 | shorten = true; | |
2231 | } | |
75add28a | 2232 | |
6ce774fd MM |
2233 | if (!arg_no_legend) |
2234 | printf("%*s %-*s %-*s %-*s\n", | |
2235 | id_len, "JOB", | |
2236 | unit_len, "UNIT", | |
2237 | type_len, "TYPE", | |
2238 | state_len, "STATE"); | |
75add28a | 2239 | |
f459b602 MAP |
2240 | for (j = jobs; j < jobs + n; j++) { |
2241 | _cleanup_free_ char *e = NULL; | |
75add28a | 2242 | |
f459b602 MAP |
2243 | if (streq(j->state, "running")) { |
2244 | on = ansi_highlight(); | |
1fc464f6 | 2245 | off = ansi_normal(); |
f459b602 MAP |
2246 | } else |
2247 | on = off = ""; | |
2248 | ||
2249 | e = shorten ? ellipsize(j->name, unit_len, 33) : NULL; | |
2250 | printf("%*u %s%-*s%s %-*s %s%-*s%s\n", | |
2251 | id_len, j->id, | |
2252 | on, unit_len, e ? e : j->name, off, | |
2253 | type_len, j->type, | |
2254 | on, state_len, j->state, off); | |
82948f6c LP |
2255 | |
2256 | if (arg_jobs_after) | |
bc357ce5 | 2257 | output_waiting_jobs(bus, j->id, "GetJobAfter", "\twaiting for job"); |
82948f6c | 2258 | if (arg_jobs_before) |
bc357ce5 | 2259 | output_waiting_jobs(bus, j->id, "GetJobBefore", "\tblocking job"); |
75add28a ZJS |
2260 | } |
2261 | ||
6ce774fd MM |
2262 | if (!arg_no_legend) { |
2263 | on = ansi_highlight(); | |
1fc464f6 | 2264 | off = ansi_normal(); |
75add28a | 2265 | |
6ce774fd MM |
2266 | printf("\n%s%u jobs listed%s.\n", on, n, off); |
2267 | } | |
75add28a ZJS |
2268 | } |
2269 | ||
d8fba7c6 | 2270 | static bool output_show_job(struct job_info *job, char **patterns) { |
2404701e | 2271 | return strv_fnmatch_or_empty(patterns, job->name, FNM_NOESCAPE); |
d8fba7c6 ZJS |
2272 | } |
2273 | ||
e449de87 | 2274 | static int list_jobs(int argc, char *argv[], void *userdata) { |
4afd3348 LP |
2275 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
2276 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
f459b602 MAP |
2277 | const char *name, *type, *state, *job_path, *unit_path; |
2278 | _cleanup_free_ struct job_info *jobs = NULL; | |
2279 | size_t size = 0; | |
2280 | unsigned c = 0; | |
4fbd7192 | 2281 | sd_bus *bus; |
f459b602 | 2282 | uint32_t id; |
f84190d8 | 2283 | int r; |
d8fba7c6 | 2284 | bool skipped = false; |
ec14911e | 2285 | |
4fbd7192 LP |
2286 | r = acquire_bus(BUS_MANAGER, &bus); |
2287 | if (r < 0) | |
2288 | return r; | |
2289 | ||
f459b602 | 2290 | r = sd_bus_call_method( |
f22f08cd SP |
2291 | bus, |
2292 | "org.freedesktop.systemd1", | |
2293 | "/org/freedesktop/systemd1", | |
2294 | "org.freedesktop.systemd1.Manager", | |
2295 | "ListJobs", | |
f459b602 | 2296 | &error, |
f22f08cd | 2297 | &reply, |
f459b602 | 2298 | NULL); |
691395d8 LP |
2299 | if (r < 0) |
2300 | return log_error_errno(r, "Failed to list jobs: %s", bus_error_message(&error, r)); | |
7e4249b9 | 2301 | |
f459b602 MAP |
2302 | r = sd_bus_message_enter_container(reply, 'a', "(usssoo)"); |
2303 | if (r < 0) | |
2304 | return bus_log_parse_error(r); | |
7e4249b9 | 2305 | |
f459b602 | 2306 | while ((r = sd_bus_message_read(reply, "(usssoo)", &id, &name, &type, &state, &job_path, &unit_path)) > 0) { |
d8fba7c6 ZJS |
2307 | struct job_info job = { id, name, type, state }; |
2308 | ||
e449de87 | 2309 | if (!output_show_job(&job, strv_skip(argv, 1))) { |
d8fba7c6 ZJS |
2310 | skipped = true; |
2311 | continue; | |
2312 | } | |
8fe914ec | 2313 | |
f459b602 MAP |
2314 | if (!GREEDY_REALLOC(jobs, size, c + 1)) |
2315 | return log_oom(); | |
7e4249b9 | 2316 | |
d8fba7c6 | 2317 | jobs[c++] = job; |
7e4249b9 | 2318 | } |
f459b602 MAP |
2319 | if (r < 0) |
2320 | return bus_log_parse_error(r); | |
7e4249b9 | 2321 | |
f459b602 MAP |
2322 | r = sd_bus_message_exit_container(reply); |
2323 | if (r < 0) | |
2324 | return bus_log_parse_error(r); | |
f73e33d9 | 2325 | |
ee5324aa | 2326 | (void) pager_open(arg_no_pager, false); |
d2ad7e1b | 2327 | |
82948f6c | 2328 | output_jobs_list(bus, jobs, c, skipped); |
91d0f17e | 2329 | return 0; |
7e4249b9 LP |
2330 | } |
2331 | ||
e449de87 | 2332 | static int cancel_job(int argc, char *argv[], void *userdata) { |
4fbd7192 | 2333 | sd_bus *bus; |
729e3769 | 2334 | char **name; |
342641fb | 2335 | int r = 0; |
7e4249b9 | 2336 | |
e449de87 | 2337 | if (argc <= 1) |
2853b60a | 2338 | return trivial_method(argc, argv, userdata); |
ee5762e3 | 2339 | |
4fbd7192 LP |
2340 | r = acquire_bus(BUS_MANAGER, &bus); |
2341 | if (r < 0) | |
2342 | return r; | |
2343 | ||
8a4b13c5 | 2344 | polkit_agent_open_maybe(); |
d2ad7e1b | 2345 | |
e449de87 | 2346 | STRV_FOREACH(name, strv_skip(argv, 1)) { |
4afd3348 | 2347 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
5dd9014f | 2348 | uint32_t id; |
342641fb | 2349 | int q; |
7e4249b9 | 2350 | |
342641fb | 2351 | q = safe_atou32(*name, &id); |
f647962d MS |
2352 | if (q < 0) |
2353 | return log_error_errno(q, "Failed to parse job id \"%s\": %m", *name); | |
7e4249b9 | 2354 | |
6e646d22 | 2355 | q = sd_bus_call_method( |
f22f08cd SP |
2356 | bus, |
2357 | "org.freedesktop.systemd1", | |
2358 | "/org/freedesktop/systemd1", | |
2359 | "org.freedesktop.systemd1.Manager", | |
6e646d22 LP |
2360 | "CancelJob", |
2361 | &error, | |
2362 | NULL, | |
2363 | "u", id); | |
342641fb | 2364 | if (q < 0) { |
691395d8 | 2365 | log_error_errno(q, "Failed to cancel job %"PRIu32": %s", id, bus_error_message(&error, q)); |
342641fb LP |
2366 | if (r == 0) |
2367 | r = q; | |
f459b602 | 2368 | } |
7e4249b9 LP |
2369 | } |
2370 | ||
342641fb | 2371 | return r; |
7e4249b9 LP |
2372 | } |
2373 | ||
f459b602 | 2374 | static int need_daemon_reload(sd_bus *bus, const char *unit) { |
4afd3348 | 2375 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
f459b602 MAP |
2376 | const char *path; |
2377 | int b, r; | |
94c01aeb | 2378 | |
f459b602 MAP |
2379 | /* We ignore all errors here, since this is used to show a |
2380 | * warning only */ | |
45fb0699 | 2381 | |
f459b602 MAP |
2382 | /* We don't use unit_dbus_path_from_name() directly since we |
2383 | * don't want to load the unit if it isn't loaded. */ | |
f84190d8 | 2384 | |
f459b602 | 2385 | r = sd_bus_call_method( |
f22f08cd SP |
2386 | bus, |
2387 | "org.freedesktop.systemd1", | |
2388 | "/org/freedesktop/systemd1", | |
2389 | "org.freedesktop.systemd1.Manager", | |
2390 | "GetUnit", | |
f459b602 | 2391 | NULL, |
f22f08cd | 2392 | &reply, |
e3e0314b | 2393 | "s", unit); |
f84190d8 LP |
2394 | if (r < 0) |
2395 | return r; | |
45fb0699 | 2396 | |
f459b602 MAP |
2397 | r = sd_bus_message_read(reply, "o", &path); |
2398 | if (r < 0) | |
2399 | return r; | |
f84190d8 | 2400 | |
f459b602 | 2401 | r = sd_bus_get_property_trivial( |
f22f08cd | 2402 | bus, |
b0193f1c LP |
2403 | "org.freedesktop.systemd1", |
2404 | path, | |
f459b602 MAP |
2405 | "org.freedesktop.systemd1.Unit", |
2406 | "NeedDaemonReload", | |
2407 | NULL, | |
2408 | 'b', &b); | |
f84190d8 LP |
2409 | if (r < 0) |
2410 | return r; | |
45fb0699 | 2411 | |
45fb0699 LP |
2412 | return b; |
2413 | } | |
2414 | ||
3f36991e | 2415 | static void warn_unit_file_changed(const char *name) { |
87ec20ef LP |
2416 | assert(name); |
2417 | ||
e9f4aabd | 2418 | 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 | 2419 | ansi_highlight_red(), |
1fc464f6 | 2420 | ansi_normal(), |
3f36991e ZJS |
2421 | name, |
2422 | arg_scope == UNIT_FILE_SYSTEM ? "" : " --user"); | |
2423 | } | |
2424 | ||
33f6c497 ZJS |
2425 | static int unit_file_find_path(LookupPaths *lp, const char *unit_name, char **unit_path) { |
2426 | char **p; | |
2427 | ||
2428 | assert(lp); | |
2429 | assert(unit_name); | |
33f6c497 | 2430 | |
a3c4eb07 | 2431 | STRV_FOREACH(p, lp->search_path) { |
c6dd36b6 FS |
2432 | _cleanup_free_ char *path = NULL, *lpath = NULL; |
2433 | int r; | |
33f6c497 | 2434 | |
3ef21542 | 2435 | path = path_join(NULL, *p, unit_name); |
33f6c497 ZJS |
2436 | if (!path) |
2437 | return log_oom(); | |
2438 | ||
c4f4fce7 | 2439 | r = chase_symlinks(path, arg_root, 0, &lpath); |
c6dd36b6 FS |
2440 | if (r == -ENOENT) |
2441 | continue; | |
2442 | if (r == -ENOMEM) | |
2443 | return log_oom(); | |
2444 | if (r < 0) | |
2445 | return log_error_errno(r, "Failed to access path '%s': %m", path); | |
2446 | ||
ae2a15bc LP |
2447 | if (unit_path) |
2448 | *unit_path = TAKE_PTR(lpath); | |
2449 | ||
c6dd36b6 | 2450 | return 1; |
33f6c497 ZJS |
2451 | } |
2452 | ||
2453 | return 0; | |
2454 | } | |
2455 | ||
173471b7 ZJS |
2456 | static int unit_find_template_path( |
2457 | const char *unit_name, | |
2458 | LookupPaths *lp, | |
2459 | char **fragment_path, | |
2460 | char **template) { | |
2461 | ||
2462 | _cleanup_free_ char *_template = NULL; | |
2463 | int r; | |
2464 | ||
2465 | /* Returns 1 if a fragment was found, 0 if not found, negative on error. */ | |
2466 | ||
2467 | r = unit_file_find_path(lp, unit_name, fragment_path); | |
2468 | if (r != 0) | |
2469 | return r; /* error or found a real unit */ | |
2470 | ||
2471 | r = unit_name_template(unit_name, &_template); | |
2472 | if (r == -EINVAL) | |
2473 | return 0; /* not a template, does not exist */ | |
2474 | if (r < 0) | |
2475 | return log_error_errno(r, "Failed to determine template name: %m"); | |
2476 | ||
2477 | r = unit_file_find_path(lp, _template, fragment_path); | |
2478 | if (r < 0) | |
2479 | return r; | |
2480 | ||
ae2a15bc LP |
2481 | if (template) |
2482 | *template = TAKE_PTR(_template); | |
2483 | ||
173471b7 ZJS |
2484 | return r; |
2485 | } | |
2486 | ||
6e646d22 LP |
2487 | static int unit_find_paths( |
2488 | sd_bus *bus, | |
2489 | const char *unit_name, | |
6e646d22 LP |
2490 | LookupPaths *lp, |
2491 | char **fragment_path, | |
2492 | char ***dropin_paths) { | |
dab2bce8 IS |
2493 | |
2494 | _cleanup_free_ char *path = NULL; | |
2495 | _cleanup_strv_free_ char **dropins = NULL; | |
33f6c497 ZJS |
2496 | int r; |
2497 | ||
ad2a0358 ZJS |
2498 | /** |
2499 | * Finds where the unit is defined on disk. Returns 0 if the unit | |
2500 | * is not found. Returns 1 if it is found, and sets | |
2501 | * - the path to the unit in *path, if it exists on disk, | |
2502 | * - and a strv of existing drop-ins in *dropins, | |
2503 | * if the arg is not NULL and any dropins were found. | |
2504 | */ | |
2505 | ||
33f6c497 | 2506 | assert(unit_name); |
ad2a0358 | 2507 | assert(fragment_path); |
33f6c497 ZJS |
2508 | assert(lp); |
2509 | ||
4fbd7192 | 2510 | if (!install_client_side() && !unit_name_is_valid(unit_name, UNIT_NAME_TEMPLATE)) { |
4afd3348 | 2511 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
33f6c497 | 2512 | _cleanup_free_ char *unit = NULL; |
33f6c497 ZJS |
2513 | |
2514 | unit = unit_dbus_path_from_name(unit_name); | |
2515 | if (!unit) | |
2516 | return log_oom(); | |
2517 | ||
33f6c497 ZJS |
2518 | r = sd_bus_get_property_string( |
2519 | bus, | |
2520 | "org.freedesktop.systemd1", | |
2521 | unit, | |
2522 | "org.freedesktop.systemd1.Unit", | |
2523 | "FragmentPath", | |
2524 | &error, | |
ad2a0358 ZJS |
2525 | &path); |
2526 | if (r < 0) | |
2527 | return log_error_errno(r, "Failed to get FragmentPath: %s", bus_error_message(&error, r)); | |
2528 | ||
dab2bce8 IS |
2529 | if (dropin_paths) { |
2530 | r = sd_bus_get_property_strv( | |
2531 | bus, | |
2532 | "org.freedesktop.systemd1", | |
2533 | unit, | |
2534 | "org.freedesktop.systemd1.Unit", | |
2535 | "DropInPaths", | |
2536 | &error, | |
2537 | &dropins); | |
2538 | if (r < 0) | |
2539 | return log_error_errno(r, "Failed to get DropInPaths: %s", bus_error_message(&error, r)); | |
33f6c497 | 2540 | } |
ad2a0358 | 2541 | } else { |
173471b7 | 2542 | _cleanup_set_free_ Set *names = NULL; |
6483ad89 | 2543 | _cleanup_free_ char *template = NULL; |
33f6c497 | 2544 | |
ad2a0358 ZJS |
2545 | names = set_new(NULL); |
2546 | if (!names) | |
7410616c | 2547 | return log_oom(); |
33f6c497 | 2548 | |
173471b7 | 2549 | r = unit_find_template_path(unit_name, lp, &path, &template); |
ad2a0358 ZJS |
2550 | if (r < 0) |
2551 | return r; | |
2552 | ||
173471b7 | 2553 | if (r > 0) |
6483ad89 ZJS |
2554 | /* We found the unit file. If we followed symlinks, this name might be |
2555 | * different then the unit_name with started with. Look for dropins matching | |
2556 | * that "final" name. */ | |
2557 | r = set_put(names, basename(path)); | |
2558 | else if (!template) | |
2559 | /* No unit file, let's look for dropins matching the original name. | |
2560 | * systemd has fairly complicated rules (based on unit type and provenience), | |
2561 | * which units are allowed not to have the main unit file. We err on the | |
2562 | * side of including too many files, and always try to load dropins. */ | |
2563 | r = set_put(names, unit_name); | |
2564 | else | |
2565 | /* The cases where we allow a unit to exist without the main file are | |
2566 | * never valid for templates. Don't try to load dropins in this case. */ | |
2567 | goto not_found; | |
2568 | ||
c6dd36b6 FS |
2569 | if (r < 0) |
2570 | return log_error_errno(r, "Failed to add unit name: %m"); | |
2571 | ||
dab2bce8 | 2572 | if (dropin_paths) { |
95778782 ZJS |
2573 | r = unit_file_find_dropin_conf_paths(arg_root, lp->search_path, |
2574 | NULL, names, &dropins); | |
dab2bce8 IS |
2575 | if (r < 0) |
2576 | return r; | |
2577 | } | |
2578 | } | |
2579 | ||
2580 | r = 0; | |
2581 | ||
2582 | if (!isempty(path)) { | |
1cc6c93a | 2583 | *fragment_path = TAKE_PTR(path); |
dab2bce8 IS |
2584 | r = 1; |
2585 | } | |
2586 | ||
2587 | if (dropin_paths && !strv_isempty(dropins)) { | |
1cc6c93a | 2588 | *dropin_paths = TAKE_PTR(dropins); |
dab2bce8 | 2589 | r = 1; |
33f6c497 | 2590 | } |
6483ad89 | 2591 | not_found: |
39c38ce1 | 2592 | if (r == 0 && !arg_force) |
b5e6a600 IS |
2593 | log_error("No files found for %s.", unit_name); |
2594 | ||
ad2a0358 | 2595 | return r; |
33f6c497 ZJS |
2596 | } |
2597 | ||
fa0d5878 | 2598 | static int get_state_one_unit(sd_bus *bus, const char *name, UnitActiveState *active_state) { |
b430f85f | 2599 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
3d083b22 | 2600 | _cleanup_free_ char *buf = NULL, *path = NULL; |
fa0d5878 | 2601 | UnitActiveState state; |
f22f08cd | 2602 | int r; |
701cdcb9 | 2603 | |
31be1221 | 2604 | assert(name); |
fa0d5878 | 2605 | assert(active_state); |
701cdcb9 | 2606 | |
3d083b22 LP |
2607 | path = unit_dbus_path_from_name(name); |
2608 | if (!path) | |
2609 | return log_oom(); | |
2610 | ||
2611 | r = sd_bus_get_property_string( | |
f22f08cd SP |
2612 | bus, |
2613 | "org.freedesktop.systemd1", | |
3d083b22 LP |
2614 | path, |
2615 | "org.freedesktop.systemd1.Unit", | |
2616 | "ActiveState", | |
b430f85f | 2617 | &error, |
3d083b22 LP |
2618 | &buf); |
2619 | if (r < 0) | |
2620 | return log_error_errno(r, "Failed to retrieve unit state: %s", bus_error_message(&error, r)); | |
b430f85f | 2621 | |
3d083b22 LP |
2622 | state = unit_active_state_from_string(buf); |
2623 | if (state == _UNIT_ACTIVE_STATE_INVALID) { | |
2624 | log_error("Invalid unit state '%s' for: %s", buf, name); | |
2625 | return -EINVAL; | |
60f9ba0b | 2626 | } |
701cdcb9 | 2627 | |
fa0d5878 BR |
2628 | *active_state = state; |
2629 | return 0; | |
701cdcb9 MS |
2630 | } |
2631 | ||
9d9dd746 | 2632 | static int unit_is_masked(sd_bus *bus, const char *name) { |
bd062910 | 2633 | _cleanup_free_ char *load_state = NULL; |
f22f08cd | 2634 | int r; |
701cdcb9 | 2635 | |
bd062910 | 2636 | r = unit_load_state(bus, name, &load_state); |
691395d8 | 2637 | if (r < 0) |
bd062910 | 2638 | return r; |
d0a5cdb2 | 2639 | |
9d9dd746 ZJS |
2640 | return streq(load_state, "masked"); |
2641 | } | |
2642 | ||
2643 | static int check_triggering_units(sd_bus *bus, const char *name) { | |
2644 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
2645 | _cleanup_free_ char *n = NULL, *path = NULL; | |
2646 | _cleanup_strv_free_ char **triggered_by = NULL; | |
2647 | bool print_warning_label = true; | |
2648 | UnitActiveState active_state; | |
2649 | char **i; | |
2650 | int r; | |
2651 | ||
2652 | r = unit_name_mangle(name, 0, &n); | |
2653 | if (r < 0) | |
2654 | return log_error_errno(r, "Failed to mangle unit name: %m"); | |
2655 | ||
2656 | r = unit_is_masked(bus, n); | |
2657 | if (r != 0) | |
2658 | return r < 0 ? r : 0; | |
2659 | ||
2660 | path = unit_dbus_path_from_name(n); | |
2661 | if (!path) | |
2662 | return log_oom(); | |
701cdcb9 | 2663 | |
f459b602 MAP |
2664 | r = sd_bus_get_property_strv( |
2665 | bus, | |
2666 | "org.freedesktop.systemd1", | |
2667 | path, | |
2668 | "org.freedesktop.systemd1.Unit", | |
2669 | "TriggeredBy", | |
2670 | &error, | |
2671 | &triggered_by); | |
691395d8 LP |
2672 | if (r < 0) |
2673 | return log_error_errno(r, "Failed to get triggered by array of %s: %s", n, bus_error_message(&error, r)); | |
701cdcb9 | 2674 | |
f459b602 | 2675 | STRV_FOREACH(i, triggered_by) { |
fa0d5878 | 2676 | r = get_state_one_unit(bus, *i, &active_state); |
f647962d | 2677 | if (r < 0) |
fa0d5878 | 2678 | return r; |
701cdcb9 | 2679 | |
fa0d5878 | 2680 | if (!IN_SET(active_state, UNIT_ACTIVE, UNIT_RELOADING)) |
f459b602 | 2681 | continue; |
60f9ba0b | 2682 | |
f459b602 MAP |
2683 | if (print_warning_label) { |
2684 | log_warning("Warning: Stopping %s, but it can still be activated by:", n); | |
2685 | print_warning_label = false; | |
701cdcb9 | 2686 | } |
1c291cf3 | 2687 | |
f459b602 | 2688 | log_warning(" %s", *i); |
701cdcb9 | 2689 | } |
f459b602 MAP |
2690 | |
2691 | return 0; | |
701cdcb9 MS |
2692 | } |
2693 | ||
2fc9a280 LP |
2694 | static const struct { |
2695 | const char *verb; | |
2696 | const char *method; | |
2697 | } unit_actions[] = { | |
2698 | { "start", "StartUnit" }, | |
2699 | { "stop", "StopUnit" }, | |
2700 | { "condstop", "StopUnit" }, | |
2701 | { "reload", "ReloadUnit" }, | |
2702 | { "restart", "RestartUnit" }, | |
2703 | { "try-restart", "TryRestartUnit" }, | |
2704 | { "condrestart", "TryRestartUnit" }, | |
2705 | { "reload-or-restart", "ReloadOrRestartUnit" }, | |
aabf5d42 | 2706 | { "try-reload-or-restart", "ReloadOrTryRestartUnit" }, |
2fc9a280 LP |
2707 | { "reload-or-try-restart", "ReloadOrTryRestartUnit" }, |
2708 | { "condreload", "ReloadOrTryRestartUnit" }, | |
2709 | { "force-reload", "ReloadOrTryRestartUnit" } | |
2710 | }; | |
2711 | ||
39602c39 TA |
2712 | static const char *verb_to_method(const char *verb) { |
2713 | uint i; | |
2714 | ||
2715 | for (i = 0; i < ELEMENTSOF(unit_actions); i++) | |
2716 | if (streq_ptr(unit_actions[i].verb, verb)) | |
2717 | return unit_actions[i].method; | |
2718 | ||
2719 | return "StartUnit"; | |
2720 | } | |
2721 | ||
2722 | static const char *method_to_verb(const char *method) { | |
2723 | uint i; | |
2724 | ||
2725 | for (i = 0; i < ELEMENTSOF(unit_actions); i++) | |
2726 | if (streq_ptr(unit_actions[i].method, method)) | |
2727 | return unit_actions[i].verb; | |
2728 | ||
2729 | return "n/a"; | |
2730 | } | |
2731 | ||
93a08841 MP |
2732 | typedef struct { |
2733 | sd_bus_slot *match; | |
2734 | sd_event *event; | |
2735 | Set *unit_paths; | |
2736 | bool any_failed; | |
2737 | } WaitContext; | |
2738 | ||
2739 | static void wait_context_free(WaitContext *c) { | |
2740 | c->match = sd_bus_slot_unref(c->match); | |
2741 | c->event = sd_event_unref(c->event); | |
6fa4160d | 2742 | c->unit_paths = set_free_free(c->unit_paths); |
93a08841 MP |
2743 | } |
2744 | ||
2745 | static int on_properties_changed(sd_bus_message *m, void *userdata, sd_bus_error *error) { | |
2746 | WaitContext *c = userdata; | |
2747 | const char *path; | |
2748 | int r; | |
2749 | ||
2750 | path = sd_bus_message_get_path(m); | |
2751 | if (!set_contains(c->unit_paths, path)) | |
2752 | return 0; | |
2753 | ||
2754 | /* Check if ActiveState changed to inactive/failed */ | |
2755 | /* (s interface, a{sv} changed_properties, as invalidated_properties) */ | |
2756 | r = sd_bus_message_skip(m, "s"); | |
2757 | if (r < 0) | |
2758 | return bus_log_parse_error(r); | |
6fa4160d | 2759 | |
93a08841 MP |
2760 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "{sv}"); |
2761 | if (r < 0) | |
2762 | return bus_log_parse_error(r); | |
2763 | ||
2764 | while ((r = sd_bus_message_enter_container(m, SD_BUS_TYPE_DICT_ENTRY, "sv")) > 0) { | |
2765 | const char *s; | |
93a08841 MP |
2766 | |
2767 | r = sd_bus_message_read(m, "s", &s); | |
2768 | if (r < 0) | |
2769 | return bus_log_parse_error(r); | |
6fa4160d | 2770 | |
93a08841 | 2771 | if (streq(s, "ActiveState")) { |
6fa4160d LP |
2772 | bool is_failed; |
2773 | ||
93a08841 MP |
2774 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_VARIANT, "s"); |
2775 | if (r < 0) | |
2776 | return bus_log_parse_error(r); | |
6fa4160d | 2777 | |
93a08841 MP |
2778 | r = sd_bus_message_read(m, "s", &s); |
2779 | if (r < 0) | |
2780 | return bus_log_parse_error(r); | |
6fa4160d | 2781 | |
93a08841 MP |
2782 | is_failed = streq(s, "failed"); |
2783 | if (streq(s, "inactive") || is_failed) { | |
2784 | log_debug("%s became %s, dropping from --wait tracking", path, s); | |
6fa4160d LP |
2785 | free(set_remove(c->unit_paths, path)); |
2786 | c->any_failed = c->any_failed || is_failed; | |
93a08841 MP |
2787 | } else |
2788 | log_debug("ActiveState on %s changed to %s", path, s); | |
6fa4160d | 2789 | |
93a08841 MP |
2790 | break; /* no need to dissect the rest of the message */ |
2791 | } else { | |
2792 | /* other property */ | |
2793 | r = sd_bus_message_skip(m, "v"); | |
2794 | if (r < 0) | |
2795 | return bus_log_parse_error(r); | |
2796 | } | |
2797 | r = sd_bus_message_exit_container(m); | |
2798 | if (r < 0) | |
2799 | return bus_log_parse_error(r); | |
2800 | } | |
2801 | if (r < 0) | |
2802 | return bus_log_parse_error(r); | |
2803 | ||
2804 | if (set_isempty(c->unit_paths)) | |
2805 | sd_event_exit(c->event, EXIT_SUCCESS); | |
2806 | ||
2807 | return 0; | |
2808 | } | |
2809 | ||
e4b61340 | 2810 | static int start_unit_one( |
f459b602 | 2811 | sd_bus *bus, |
e4b61340 LP |
2812 | const char *method, |
2813 | const char *name, | |
2814 | const char *mode, | |
f459b602 | 2815 | sd_bus_error *error, |
93a08841 MP |
2816 | BusWaitForJobs *w, |
2817 | WaitContext *wait_context) { | |
7e4249b9 | 2818 | |
4afd3348 | 2819 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
45fb0699 | 2820 | const char *path; |
7e4249b9 | 2821 | int r; |
7e4249b9 | 2822 | |
e4b61340 LP |
2823 | assert(method); |
2824 | assert(name); | |
2825 | assert(mode); | |
22f4096c | 2826 | assert(error); |
7e4249b9 | 2827 | |
93a08841 MP |
2828 | if (wait_context) { |
2829 | _cleanup_free_ char *unit_path = NULL; | |
93a08841 MP |
2830 | |
2831 | log_debug("Watching for property changes of %s", name); | |
2832 | r = sd_bus_call_method( | |
2833 | bus, | |
2834 | "org.freedesktop.systemd1", | |
2835 | "/org/freedesktop/systemd1", | |
2836 | "org.freedesktop.systemd1.Manager", | |
2837 | "RefUnit", | |
2838 | error, | |
2839 | NULL, | |
2840 | "s", name); | |
2841 | if (r < 0) | |
2842 | return log_error_errno(r, "Failed to RefUnit %s: %s", name, bus_error_message(error, r)); | |
2843 | ||
2844 | unit_path = unit_dbus_path_from_name(name); | |
2845 | if (!unit_path) | |
2846 | return log_oom(); | |
2847 | ||
2848 | r = set_put_strdup(wait_context->unit_paths, unit_path); | |
2849 | if (r < 0) | |
2850 | return log_error_errno(r, "Failed to add unit path %s to set: %m", unit_path); | |
2851 | ||
75152a4d LP |
2852 | r = sd_bus_match_signal_async(bus, |
2853 | &wait_context->match, | |
2854 | NULL, | |
2855 | unit_path, | |
2856 | "org.freedesktop.DBus.Properties", | |
2857 | "PropertiesChanged", | |
2858 | on_properties_changed, NULL, wait_context); | |
93a08841 | 2859 | if (r < 0) |
75152a4d | 2860 | return log_error_errno(r, "Failed to request match for PropertiesChanged signal: %m"); |
93a08841 MP |
2861 | } |
2862 | ||
dbc9830c ZJS |
2863 | log_debug("%s dbus call org.freedesktop.systemd1.Manager %s(%s, %s)", |
2864 | arg_dry_run ? "Would execute" : "Executing", | |
1ae17672 ZJS |
2865 | method, name, mode); |
2866 | if (arg_dry_run) | |
2867 | return 0; | |
342641fb | 2868 | |
6e646d22 | 2869 | r = sd_bus_call_method( |
f22f08cd | 2870 | bus, |
b0193f1c LP |
2871 | "org.freedesktop.systemd1", |
2872 | "/org/freedesktop/systemd1", | |
2873 | "org.freedesktop.systemd1.Manager", | |
6e646d22 LP |
2874 | method, |
2875 | error, | |
2876 | &reply, | |
2877 | "ss", name, mode); | |
f459b602 | 2878 | if (r < 0) { |
39602c39 TA |
2879 | const char *verb; |
2880 | ||
033f0ab8 LP |
2881 | /* There's always a fallback possible for legacy actions. */ |
2882 | if (arg_action != ACTION_SYSTEMCTL) | |
2883 | return r; | |
67f3c402 | 2884 | |
39602c39 TA |
2885 | verb = method_to_verb(method); |
2886 | ||
ee87525c FB |
2887 | log_error("Failed to %s %s: %s", verb, name, bus_error_message(error, r)); |
2888 | ||
2889 | if (!sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT) && | |
5b464a18 ZJS |
2890 | !sd_bus_error_has_name(error, BUS_ERROR_UNIT_MASKED) && |
2891 | !sd_bus_error_has_name(error, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE)) | |
a6405ca2 | 2892 | log_error("See %s logs and 'systemctl%s status%s %s' for details.", |
0b8505b7 RC |
2893 | arg_scope == UNIT_FILE_SYSTEM ? "system" : "user", |
2894 | arg_scope == UNIT_FILE_SYSTEM ? "" : " --user", | |
a6405ca2 | 2895 | name[0] == '-' ? " --" : "", |
0b8505b7 | 2896 | name); |
ee87525c FB |
2897 | |
2898 | return r; | |
7e4249b9 LP |
2899 | } |
2900 | ||
f459b602 MAP |
2901 | r = sd_bus_message_read(reply, "o", &path); |
2902 | if (r < 0) | |
2903 | return bus_log_parse_error(r); | |
45fb0699 | 2904 | |
e3e0314b | 2905 | if (need_daemon_reload(bus, name) > 0) |
3f36991e | 2906 | warn_unit_file_changed(name); |
45fb0699 | 2907 | |
ebd011d9 LP |
2908 | if (w) { |
2909 | log_debug("Adding %s to the set", path); | |
2910 | r = bus_wait_for_jobs_add(w, path); | |
cbc9fbd1 LP |
2911 | if (r < 0) |
2912 | return log_oom(); | |
e4b61340 | 2913 | } |
7e4249b9 | 2914 | |
46eddbb5 | 2915 | return 0; |
7e4249b9 LP |
2916 | } |
2917 | ||
e3e0314b | 2918 | static int expand_names(sd_bus *bus, char **names, const char* suffix, char ***ret) { |
e3e0314b ZJS |
2919 | _cleanup_strv_free_ char **mangled = NULL, **globs = NULL; |
2920 | char **name; | |
7410616c | 2921 | int r, i; |
e3e0314b | 2922 | |
4fbd7192 LP |
2923 | assert(bus); |
2924 | assert(ret); | |
2925 | ||
e3e0314b ZJS |
2926 | STRV_FOREACH(name, names) { |
2927 | char *t; | |
37cbc1d5 | 2928 | UnitNameMangle options = UNIT_NAME_MANGLE_GLOB | (arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN); |
e3e0314b | 2929 | |
e80733be | 2930 | if (suffix) |
37cbc1d5 | 2931 | r = unit_name_mangle_with_suffix(*name, options, suffix, &t); |
e80733be | 2932 | else |
37cbc1d5 | 2933 | r = unit_name_mangle(*name, options, &t); |
7410616c LP |
2934 | if (r < 0) |
2935 | return log_error_errno(r, "Failed to mangle name: %m"); | |
e3e0314b ZJS |
2936 | |
2937 | if (string_is_glob(t)) | |
6e18964d | 2938 | r = strv_consume(&globs, t); |
e3e0314b | 2939 | else |
6e18964d ZJS |
2940 | r = strv_consume(&mangled, t); |
2941 | if (r < 0) | |
e3e0314b | 2942 | return log_oom(); |
e3e0314b ZJS |
2943 | } |
2944 | ||
2945 | /* Query the manager only if any of the names are a glob, since | |
2946 | * this is fairly expensive */ | |
2947 | if (!strv_isempty(globs)) { | |
4afd3348 | 2948 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
e3e0314b | 2949 | _cleanup_free_ UnitInfo *unit_infos = NULL; |
1b53f64b | 2950 | size_t allocated, n; |
e3e0314b | 2951 | |
1238ee09 | 2952 | r = get_unit_list(bus, NULL, globs, &unit_infos, 0, &reply); |
e3e0314b ZJS |
2953 | if (r < 0) |
2954 | return r; | |
2955 | ||
1b53f64b LP |
2956 | n = strv_length(mangled); |
2957 | allocated = n + 1; | |
2958 | ||
2959 | for (i = 0; i < r; i++) { | |
2960 | if (!GREEDY_REALLOC(mangled, allocated, n+2)) | |
2961 | return log_oom(); | |
2962 | ||
2963 | mangled[n] = strdup(unit_infos[i].id); | |
2964 | if (!mangled[n]) | |
e3e0314b | 2965 | return log_oom(); |
1b53f64b LP |
2966 | |
2967 | mangled[++n] = NULL; | |
2968 | } | |
e3e0314b ZJS |
2969 | } |
2970 | ||
1cc6c93a | 2971 | *ret = TAKE_PTR(mangled); |
1238ee09 | 2972 | |
e3e0314b ZJS |
2973 | return 0; |
2974 | } | |
2975 | ||
47a0eaa6 MS |
2976 | static const struct { |
2977 | const char *target; | |
2978 | const char *verb; | |
2979 | const char *mode; | |
2980 | } action_table[_ACTION_MAX] = { | |
e68c79db ML |
2981 | [ACTION_HALT] = { SPECIAL_HALT_TARGET, "halt", "replace-irreversibly" }, |
2982 | [ACTION_POWEROFF] = { SPECIAL_POWEROFF_TARGET, "poweroff", "replace-irreversibly" }, | |
2983 | [ACTION_REBOOT] = { SPECIAL_REBOOT_TARGET, "reboot", "replace-irreversibly" }, | |
2984 | [ACTION_KEXEC] = { SPECIAL_KEXEC_TARGET, "kexec", "replace-irreversibly" }, | |
2985 | [ACTION_RUNLEVEL2] = { SPECIAL_MULTI_USER_TARGET, NULL, "isolate" }, | |
2986 | [ACTION_RUNLEVEL3] = { SPECIAL_MULTI_USER_TARGET, NULL, "isolate" }, | |
2987 | [ACTION_RUNLEVEL4] = { SPECIAL_MULTI_USER_TARGET, NULL, "isolate" }, | |
2988 | [ACTION_RUNLEVEL5] = { SPECIAL_GRAPHICAL_TARGET, NULL, "isolate" }, | |
2989 | [ACTION_RESCUE] = { SPECIAL_RESCUE_TARGET, "rescue", "isolate" }, | |
2990 | [ACTION_EMERGENCY] = { SPECIAL_EMERGENCY_TARGET, "emergency", "isolate" }, | |
2991 | [ACTION_DEFAULT] = { SPECIAL_DEFAULT_TARGET, "default", "isolate" }, | |
2992 | [ACTION_EXIT] = { SPECIAL_EXIT_TARGET, "exit", "replace-irreversibly" }, | |
2993 | [ACTION_SUSPEND] = { SPECIAL_SUSPEND_TARGET, "suspend", "replace-irreversibly" }, | |
2994 | [ACTION_HIBERNATE] = { SPECIAL_HIBERNATE_TARGET, "hibernate", "replace-irreversibly" }, | |
2995 | [ACTION_HYBRID_SLEEP] = { SPECIAL_HYBRID_SLEEP_TARGET, "hybrid-sleep", "replace-irreversibly" }, | |
2996 | [ACTION_SUSPEND_THEN_HIBERNATE] = { SPECIAL_SUSPEND_THEN_HIBERNATE_TARGET, "suspend-then-hibernate", "replace-irreversibly" }, | |
47a0eaa6 MS |
2997 | }; |
2998 | ||
514f4ef5 | 2999 | static enum action verb_to_action(const char *verb) { |
47a0eaa6 MS |
3000 | enum action i; |
3001 | ||
913c1916 | 3002 | for (i = 0; i < _ACTION_MAX; i++) |
f459b602 | 3003 | if (streq_ptr(action_table[i].verb, verb)) |
47a0eaa6 | 3004 | return i; |
514f4ef5 | 3005 | |
f459b602 MAP |
3006 | return _ACTION_INVALID; |
3007 | } | |
e4b61340 | 3008 | |
e449de87 | 3009 | static int start_unit(int argc, char *argv[], void *userdata) { |
ebd011d9 | 3010 | _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL; |
08073121 | 3011 | const char *method, *mode, *one_name, *suffix = NULL; |
ebd011d9 | 3012 | _cleanup_strv_free_ char **names = NULL; |
4fbd7192 | 3013 | sd_bus *bus; |
93a08841 | 3014 | _cleanup_(wait_context_free) WaitContext wait_context = {}; |
729e3769 | 3015 | char **name; |
b6520546 | 3016 | int r = 0; |
e4b61340 | 3017 | |
f886603b AJ |
3018 | if (arg_wait && !STR_IN_SET(argv[0], "start", "restart")) { |
3019 | log_error("--wait may only be used with the 'start' or 'restart' commands."); | |
93a08841 MP |
3020 | return -EINVAL; |
3021 | } | |
3022 | ||
3023 | /* we cannot do sender tracking on the private bus, so we need the full | |
3024 | * one for RefUnit to implement --wait */ | |
3025 | r = acquire_bus(arg_wait ? BUS_FULL : BUS_MANAGER, &bus); | |
4fbd7192 LP |
3026 | if (r < 0) |
3027 | return r; | |
3028 | ||
d2ad7e1b | 3029 | ask_password_agent_open_if_enabled(); |
8a4b13c5 | 3030 | polkit_agent_open_maybe(); |
d2ad7e1b | 3031 | |
e4b61340 | 3032 | if (arg_action == ACTION_SYSTEMCTL) { |
47a0eaa6 | 3033 | enum action action; |
e4b61340 | 3034 | |
e449de87 LP |
3035 | action = verb_to_action(argv[0]); |
3036 | ||
913c1916 AJ |
3037 | if (action != _ACTION_INVALID) { |
3038 | method = "StartUnit"; | |
3039 | mode = action_table[action].mode; | |
3040 | one_name = action_table[action].target; | |
3041 | } else { | |
3042 | if (streq(argv[0], "isolate")) { | |
3043 | method = "StartUnit"; | |
3044 | mode = "isolate"; | |
3045 | ||
3046 | suffix = ".target"; | |
3047 | } else { | |
3048 | method = verb_to_method(argv[0]); | |
3049 | mode = arg_job_mode; | |
3050 | } | |
3051 | one_name = NULL; | |
3052 | } | |
e4b61340 | 3053 | } else { |
78ca9099 | 3054 | assert(arg_action >= 0 && arg_action < _ACTION_MAX); |
47a0eaa6 | 3055 | assert(action_table[arg_action].target); |
913c1916 | 3056 | assert(action_table[arg_action].mode); |
e4b61340 LP |
3057 | |
3058 | method = "StartUnit"; | |
47a0eaa6 | 3059 | mode = action_table[arg_action].mode; |
e3e0314b | 3060 | one_name = action_table[arg_action].target; |
514f4ef5 LP |
3061 | } |
3062 | ||
e3e0314b ZJS |
3063 | if (one_name) |
3064 | names = strv_new(one_name, NULL); | |
3065 | else { | |
e449de87 | 3066 | r = expand_names(bus, strv_skip(argv, 1), suffix, &names); |
e3e0314b | 3067 | if (r < 0) |
691395d8 | 3068 | return log_error_errno(r, "Failed to expand names: %m"); |
e3e0314b | 3069 | } |
b6520546 | 3070 | |
6e905d93 | 3071 | if (!arg_no_block) { |
ebd011d9 | 3072 | r = bus_wait_for_jobs_new(bus, &w); |
f647962d MS |
3073 | if (r < 0) |
3074 | return log_error_errno(r, "Could not watch jobs: %m"); | |
e4b61340 LP |
3075 | } |
3076 | ||
93a08841 | 3077 | if (arg_wait) { |
93a08841 MP |
3078 | wait_context.unit_paths = set_new(&string_hash_ops); |
3079 | if (!wait_context.unit_paths) | |
3080 | return log_oom(); | |
3081 | ||
31b2cd5d | 3082 | r = sd_bus_call_method_async( |
93a08841 | 3083 | bus, |
31b2cd5d | 3084 | NULL, |
93a08841 MP |
3085 | "org.freedesktop.systemd1", |
3086 | "/org/freedesktop/systemd1", | |
3087 | "org.freedesktop.systemd1.Manager", | |
3088 | "Subscribe", | |
31b2cd5d LP |
3089 | NULL, NULL, |
3090 | NULL); | |
93a08841 | 3091 | if (r < 0) |
31b2cd5d | 3092 | return log_error_errno(r, "Failed to enable subscription: %m"); |
93a08841 MP |
3093 | r = sd_event_default(&wait_context.event); |
3094 | if (r < 0) | |
3095 | return log_error_errno(r, "Failed to allocate event loop: %m"); | |
3096 | r = sd_bus_attach_event(bus, wait_context.event, 0); | |
3097 | if (r < 0) | |
3098 | return log_error_errno(r, "Failed to attach bus to event loop: %m"); | |
3099 | } | |
3100 | ||
b6520546 | 3101 | STRV_FOREACH(name, names) { |
4afd3348 | 3102 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
b6520546 | 3103 | int q; |
f459b602 | 3104 | |
93a08841 | 3105 | q = start_unit_one(bus, method, *name, mode, &error, w, arg_wait ? &wait_context : NULL); |
e3e0314b | 3106 | if (r >= 0 && q < 0) |
b6520546 | 3107 | r = translate_bus_error_to_exit_status(q, &error); |
e4b61340 LP |
3108 | } |
3109 | ||
67f3c402 | 3110 | if (!arg_no_block) { |
c11bda1e ZJS |
3111 | int q, arg_count = 0; |
3112 | const char* extra_args[4] = {}; | |
4524439e | 3113 | |
4524439e ZJS |
3114 | if (arg_scope != UNIT_FILE_SYSTEM) |
3115 | extra_args[arg_count++] = "--user"; | |
3116 | ||
3117 | assert(IN_SET(arg_transport, BUS_TRANSPORT_LOCAL, BUS_TRANSPORT_REMOTE, BUS_TRANSPORT_MACHINE)); | |
3118 | if (arg_transport == BUS_TRANSPORT_REMOTE) { | |
3119 | extra_args[arg_count++] = "-H"; | |
3120 | extra_args[arg_count++] = arg_host; | |
3121 | } else if (arg_transport == BUS_TRANSPORT_MACHINE) { | |
3122 | extra_args[arg_count++] = "-M"; | |
3123 | extra_args[arg_count++] = arg_host; | |
3124 | } | |
f459b602 | 3125 | |
4524439e | 3126 | q = bus_wait_for_jobs(w, arg_quiet, extra_args); |
f459b602 MAP |
3127 | if (q < 0) |
3128 | return q; | |
49111a70 ZJS |
3129 | |
3130 | /* When stopping units, warn if they can still be triggered by | |
3131 | * another active unit (socket, path, timer) */ | |
b6520546 ZJS |
3132 | if (!arg_quiet && streq(method, "StopUnit")) |
3133 | STRV_FOREACH(name, names) | |
3134 | check_triggering_units(bus, *name); | |
67f3c402 | 3135 | } |
514f4ef5 | 3136 | |
93a08841 MP |
3137 | if (r >= 0 && arg_wait) { |
3138 | int q; | |
3139 | q = sd_event_loop(wait_context.event); | |
3140 | if (q < 0) | |
3141 | return log_error_errno(q, "Failed to run event loop: %m"); | |
3142 | if (wait_context.any_failed) | |
3143 | r = EXIT_FAILURE; | |
3144 | } | |
3145 | ||
f459b602 | 3146 | return r; |
e4b61340 LP |
3147 | } |
3148 | ||
349cc4a5 | 3149 | #if ENABLE_LOGIND |
4fbd7192 | 3150 | static int logind_set_wall_message(void) { |
4afd3348 | 3151 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
4fbd7192 | 3152 | sd_bus *bus; |
f2d11d35 LP |
3153 | _cleanup_free_ char *m = NULL; |
3154 | int r; | |
3155 | ||
4fbd7192 LP |
3156 | r = acquire_bus(BUS_FULL, &bus); |
3157 | if (r < 0) | |
3158 | return r; | |
f2d11d35 LP |
3159 | |
3160 | m = strv_join(arg_wall, " "); | |
3161 | if (!m) | |
3162 | return log_oom(); | |
3163 | ||
dbc9830c ZJS |
3164 | log_debug("%s wall message \"%s\".", arg_dry_run ? "Would set" : "Setting", m); |
3165 | if (arg_dry_run) | |
3166 | return 0; | |
3167 | ||
f2d11d35 LP |
3168 | r = sd_bus_call_method( |
3169 | bus, | |
3170 | "org.freedesktop.login1", | |
3171 | "/org/freedesktop/login1", | |
3172 | "org.freedesktop.login1.Manager", | |
3173 | "SetWallMessage", | |
3174 | &error, | |
3175 | NULL, | |
3176 | "sb", | |
3177 | m, | |
3178 | !arg_no_wall); | |
3179 | ||
3180 | if (r < 0) | |
3181 | return log_warning_errno(r, "Failed to set wall message, ignoring: %s", bus_error_message(&error, r)); | |
f2d11d35 LP |
3182 | return 0; |
3183 | } | |
79f1c8f6 | 3184 | #endif |
f2d11d35 | 3185 | |
7e59bfcb LP |
3186 | /* Ask systemd-logind, which might grant access to unprivileged users |
3187 | * through PolicyKit */ | |
4fbd7192 | 3188 | static int logind_reboot(enum action a) { |
349cc4a5 | 3189 | #if ENABLE_LOGIND |
4afd3348 | 3190 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
58158dc7 | 3191 | const char *method, *description; |
4fbd7192 | 3192 | sd_bus *bus; |
f459b602 | 3193 | int r; |
4c80c73c | 3194 | |
4fbd7192 LP |
3195 | r = acquire_bus(BUS_FULL, &bus); |
3196 | if (r < 0) | |
3197 | return r; | |
f2d11d35 | 3198 | |
4c80c73c KS |
3199 | switch (a) { |
3200 | ||
36b69c31 LP |
3201 | case ACTION_POWEROFF: |
3202 | method = "PowerOff"; | |
3203 | description = "power off system"; | |
3204 | break; | |
3205 | ||
4c80c73c KS |
3206 | case ACTION_REBOOT: |
3207 | method = "Reboot"; | |
58158dc7 | 3208 | description = "reboot system"; |
4c80c73c KS |
3209 | break; |
3210 | ||
36b69c31 LP |
3211 | case ACTION_HALT: |
3212 | method = "Halt"; | |
3213 | description = "halt system"; | |
4c80c73c KS |
3214 | break; |
3215 | ||
d889a206 LP |
3216 | case ACTION_SUSPEND: |
3217 | method = "Suspend"; | |
58158dc7 | 3218 | description = "suspend system"; |
d889a206 LP |
3219 | break; |
3220 | ||
3221 | case ACTION_HIBERNATE: | |
3222 | method = "Hibernate"; | |
58158dc7 | 3223 | description = "hibernate system"; |
d889a206 LP |
3224 | break; |
3225 | ||
6524990f LP |
3226 | case ACTION_HYBRID_SLEEP: |
3227 | method = "HybridSleep"; | |
58158dc7 | 3228 | description = "put system into hybrid sleep"; |
6524990f LP |
3229 | break; |
3230 | ||
e68c79db ML |
3231 | case ACTION_SUSPEND_THEN_HIBERNATE: |
3232 | method = "SuspendThenHibernate"; | |
c58493c0 ML |
3233 | description = "put system into suspend followed by hibernate"; |
3234 | break; | |
3235 | ||
4c80c73c KS |
3236 | default: |
3237 | return -EINVAL; | |
3238 | } | |
3239 | ||
8a4b13c5 | 3240 | polkit_agent_open_maybe(); |
d2ad7e1b ZJS |
3241 | (void) logind_set_wall_message(); |
3242 | ||
dbc9830c ZJS |
3243 | log_debug("%s org.freedesktop.login1.Manager %s dbus call.", arg_dry_run ? "Would execute" : "Executing", method); |
3244 | if (arg_dry_run) | |
3245 | return 0; | |
3246 | ||
f459b602 | 3247 | r = sd_bus_call_method( |
f22f08cd SP |
3248 | bus, |
3249 | "org.freedesktop.login1", | |
3250 | "/org/freedesktop/login1", | |
3251 | "org.freedesktop.login1.Manager", | |
3252 | method, | |
f459b602 | 3253 | &error, |
f22f08cd | 3254 | NULL, |
342641fb | 3255 | "b", arg_ask_password); |
f459b602 | 3256 | if (r < 0) |
691395d8 | 3257 | return log_error_errno(r, "Failed to %s via logind: %s", description, bus_error_message(&error, r)); |
f459b602 | 3258 | |
691395d8 | 3259 | return 0; |
4c80c73c KS |
3260 | #else |
3261 | return -ENOSYS; | |
3262 | #endif | |
3263 | } | |
3264 | ||
4fbd7192 | 3265 | static int logind_check_inhibitors(enum action a) { |
349cc4a5 | 3266 | #if ENABLE_LOGIND |
4afd3348 | 3267 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
59164be4 | 3268 | _cleanup_strv_free_ char **sessions = NULL; |
f459b602 MAP |
3269 | const char *what, *who, *why, *mode; |
3270 | uint32_t uid, pid; | |
4fbd7192 | 3271 | sd_bus *bus; |
f459b602 | 3272 | unsigned c = 0; |
59164be4 | 3273 | char **s; |
f459b602 | 3274 | int r; |
b37844d3 | 3275 | |
748ebafa LP |
3276 | if (arg_ignore_inhibitors || arg_force > 0) |
3277 | return 0; | |
3278 | ||
3279 | if (arg_when > 0) | |
3280 | return 0; | |
3281 | ||
3282 | if (geteuid() == 0) | |
b37844d3 LP |
3283 | return 0; |
3284 | ||
3285 | if (!on_tty()) | |
3286 | return 0; | |
e08ab379 LP |
3287 | |
3288 | if (arg_transport != BUS_TRANSPORT_LOCAL) | |
3289 | return 0; | |
b37844d3 | 3290 | |
4fbd7192 LP |
3291 | r = acquire_bus(BUS_FULL, &bus); |
3292 | if (r < 0) | |
3293 | return r; | |
3294 | ||
f459b602 | 3295 | r = sd_bus_call_method( |
b37844d3 LP |
3296 | bus, |
3297 | "org.freedesktop.login1", | |
3298 | "/org/freedesktop/login1", | |
3299 | "org.freedesktop.login1.Manager", | |
3300 | "ListInhibitors", | |
b37844d3 | 3301 | NULL, |
f459b602 MAP |
3302 | &reply, |
3303 | NULL); | |
b37844d3 LP |
3304 | if (r < 0) |
3305 | /* If logind is not around, then there are no inhibitors... */ | |
3306 | return 0; | |
3307 | ||
4aa2beac | 3308 | r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssuu)"); |
f459b602 MAP |
3309 | if (r < 0) |
3310 | return bus_log_parse_error(r); | |
b37844d3 | 3311 | |
4aa2beac | 3312 | while ((r = sd_bus_message_read(reply, "(ssssuu)", &what, &who, &why, &mode, &uid, &pid)) > 0) { |
59164be4 | 3313 | _cleanup_free_ char *comm = NULL, *user = NULL; |
f459b602 | 3314 | _cleanup_strv_free_ char **sv = NULL; |
b37844d3 LP |
3315 | |
3316 | if (!streq(mode, "block")) | |
f459b602 | 3317 | continue; |
b37844d3 LP |
3318 | |
3319 | sv = strv_split(what, ":"); | |
3320 | if (!sv) | |
3321 | return log_oom(); | |
3322 | ||
ee043777 | 3323 | if (!pid_is_valid((pid_t) pid)) { |
72b3f82e | 3324 | log_error("Invalid PID "PID_FMT".", (pid_t) pid); |
ee043777 LP |
3325 | return -ERANGE; |
3326 | } | |
d028e018 | 3327 | |
b37844d3 | 3328 | if (!strv_contains(sv, |
4c315c2c IS |
3329 | IN_SET(a, |
3330 | ACTION_HALT, | |
3331 | ACTION_POWEROFF, | |
3332 | ACTION_REBOOT, | |
3333 | ACTION_KEXEC) ? "shutdown" : "sleep")) | |
f459b602 | 3334 | continue; |
b37844d3 LP |
3335 | |
3336 | get_process_comm(pid, &comm); | |
59164be4 | 3337 | user = uid_to_name(uid); |
f459b602 | 3338 | |
de0671ee | 3339 | log_warning("Operation inhibited by \"%s\" (PID "PID_FMT" \"%s\", user %s), reason is \"%s\".", |
d028e018 | 3340 | who, (pid_t) pid, strna(comm), strna(user), why); |
b37844d3 | 3341 | |
f459b602 | 3342 | c++; |
b37844d3 | 3343 | } |
f459b602 MAP |
3344 | if (r < 0) |
3345 | return bus_log_parse_error(r); | |
b37844d3 | 3346 | |
f459b602 MAP |
3347 | r = sd_bus_message_exit_container(reply); |
3348 | if (r < 0) | |
3349 | return bus_log_parse_error(r); | |
b37844d3 | 3350 | |
59164be4 LP |
3351 | /* Check for current sessions */ |
3352 | sd_get_sessions(&sessions); | |
3353 | STRV_FOREACH(s, sessions) { | |
59164be4 LP |
3354 | _cleanup_free_ char *type = NULL, *tty = NULL, *seat = NULL, *user = NULL, *service = NULL, *class = NULL; |
3355 | ||
3356 | if (sd_session_get_uid(*s, &uid) < 0 || uid == getuid()) | |
3357 | continue; | |
3358 | ||
3359 | if (sd_session_get_class(*s, &class) < 0 || !streq(class, "user")) | |
3360 | continue; | |
3361 | ||
28abce5d | 3362 | if (sd_session_get_type(*s, &type) < 0 || !STR_IN_SET(type, "x11", "wayland", "tty", "mir")) |
59164be4 LP |
3363 | continue; |
3364 | ||
3365 | sd_session_get_tty(*s, &tty); | |
3366 | sd_session_get_seat(*s, &seat); | |
3367 | sd_session_get_service(*s, &service); | |
3368 | user = uid_to_name(uid); | |
3369 | ||
3370 | log_warning("User %s is logged in on %s.", strna(user), isempty(tty) ? (isempty(seat) ? strna(service) : seat) : tty); | |
3371 | c++; | |
3372 | } | |
3373 | ||
b37844d3 LP |
3374 | if (c <= 0) |
3375 | return 0; | |
3376 | ||
59164be4 | 3377 | log_error("Please retry operation after closing inhibitors and logging out other users.\nAlternatively, ignore inhibitors and users with 'systemctl %s -i'.", |
47a0eaa6 | 3378 | action_table[a].verb); |
b37844d3 LP |
3379 | |
3380 | return -EPERM; | |
3381 | #else | |
3382 | return 0; | |
3383 | #endif | |
3384 | } | |
3385 | ||
4fbd7192 | 3386 | static int logind_prepare_firmware_setup(void) { |
349cc4a5 | 3387 | #if ENABLE_LOGIND |
4afd3348 | 3388 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
4fbd7192 | 3389 | sd_bus *bus; |
a4921cf4 JJ |
3390 | int r; |
3391 | ||
4fbd7192 LP |
3392 | r = acquire_bus(BUS_FULL, &bus); |
3393 | if (r < 0) | |
3394 | return r; | |
a4921cf4 | 3395 | |
a4921cf4 JJ |
3396 | r = sd_bus_call_method( |
3397 | bus, | |
3398 | "org.freedesktop.login1", | |
3399 | "/org/freedesktop/login1", | |
3400 | "org.freedesktop.login1.Manager", | |
3401 | "SetRebootToFirmwareSetup", | |
3402 | &error, | |
3403 | NULL, | |
3404 | "b", true); | |
4fbd7192 LP |
3405 | if (r < 0) |
3406 | return log_error_errno(r, "Cannot indicate to EFI to boot into setup mode: %s", bus_error_message(&error, r)); | |
a4921cf4 JJ |
3407 | |
3408 | return 0; | |
3409 | #else | |
3410 | log_error("Cannot remotely indicate to EFI to boot into setup mode."); | |
4fbd7192 | 3411 | return -ENOSYS; |
a4921cf4 JJ |
3412 | #endif |
3413 | } | |
3414 | ||
4fbd7192 LP |
3415 | static int prepare_firmware_setup(void) { |
3416 | int r; | |
3417 | ||
3418 | if (!arg_firmware_setup) | |
3419 | return 0; | |
3420 | ||
3421 | if (arg_transport == BUS_TRANSPORT_LOCAL) { | |
3422 | ||
3423 | r = efi_set_reboot_to_firmware(true); | |
3424 | if (r < 0) | |
3425 | log_debug_errno(r, "Cannot indicate to EFI to boot into setup mode, will retry via logind: %m"); | |
3426 | else | |
3427 | return r; | |
3428 | } | |
3429 | ||
3430 | return logind_prepare_firmware_setup(); | |
3431 | } | |
3432 | ||
4bb2e9d4 ZJS |
3433 | static int load_kexec_kernel(void) { |
3434 | _cleanup_(boot_config_free) BootConfig config = {}; | |
5caa3167 | 3435 | _cleanup_free_ char *where = NULL, *kernel = NULL, *initrd = NULL, *options = NULL; |
4bb2e9d4 ZJS |
3436 | const BootEntry *e; |
3437 | pid_t pid; | |
3438 | int r; | |
3439 | ||
3440 | if (kexec_loaded()) { | |
3441 | log_debug("Kexec kernel already loaded."); | |
3442 | return 0; | |
3443 | } | |
3444 | ||
c0d73214 ZJS |
3445 | if (access(KEXEC, X_OK) < 0) |
3446 | return log_error_errno(errno, KEXEC" is not available: %m"); | |
3447 | ||
5caa3167 LP |
3448 | r = find_esp_and_warn(arg_esp_path, false, &where, NULL, NULL, NULL, NULL); |
3449 | if (r == -ENOKEY) /* find_esp_and_warn() doesn't warn about this case */ | |
3450 | return log_error_errno(r, "Cannot find the ESP partition mount point."); | |
3451 | if (r < 0) /* But it logs about all these cases, hence don't log here again */ | |
3452 | return r; | |
4bb2e9d4 | 3453 | |
5caa3167 | 3454 | r = boot_entries_load_config(where, &config); |
4bb2e9d4 | 3455 | if (r < 0) |
5caa3167 | 3456 | return log_error_errno(r, "Failed to load bootspec config from \"%s/loader\": %m", where); |
4bb2e9d4 ZJS |
3457 | |
3458 | if (config.default_entry < 0) { | |
3459 | log_error("No entry suitable as default, refusing to guess."); | |
3460 | return -ENOENT; | |
3461 | } | |
3462 | e = &config.entries[config.default_entry]; | |
3463 | ||
3464 | if (strv_length(e->initrd) > 1) { | |
3465 | log_error("Boot entry specifies multiple initrds, which is not supported currently."); | |
3466 | return -EINVAL; | |
3467 | } | |
3468 | ||
5caa3167 | 3469 | kernel = path_join(NULL, where, e->kernel); |
4bb2e9d4 | 3470 | if (!strv_isempty(e->initrd)) |
5caa3167 | 3471 | initrd = path_join(NULL, where, *e->initrd); |
4bb2e9d4 ZJS |
3472 | options = strv_join(e->options, " "); |
3473 | if (!options) | |
3474 | return log_oom(); | |
3475 | ||
cd086a01 ZJS |
3476 | log_full(arg_quiet ? LOG_DEBUG : LOG_INFO, |
3477 | "%s "KEXEC" --load \"%s\" --append \"%s\"%s%s%s", | |
3478 | arg_dry_run ? "Would run" : "Running", | |
3479 | kernel, | |
3480 | options, | |
3481 | initrd ? " --initrd \"" : NULL, strempty(initrd), initrd ? "\"" : ""); | |
4bb2e9d4 ZJS |
3482 | if (arg_dry_run) |
3483 | return 0; | |
3484 | ||
b6e1fff1 | 3485 | r = safe_fork("(kexec)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_LOG, &pid); |
4c253ed1 | 3486 | if (r < 0) |
b6e1fff1 | 3487 | return r; |
4c253ed1 | 3488 | if (r == 0) { |
4bb2e9d4 ZJS |
3489 | const char* const args[] = { |
3490 | KEXEC, | |
3491 | "--load", kernel, | |
3492 | "--append", options, | |
3493 | initrd ? "--initrd" : NULL, initrd, | |
3494 | NULL }; | |
3495 | ||
3496 | /* Child */ | |
4bb2e9d4 ZJS |
3497 | execv(args[0], (char * const *) args); |
3498 | _exit(EXIT_FAILURE); | |
4c253ed1 LP |
3499 | } |
3500 | ||
df685d57 ZJS |
3501 | r = wait_for_terminate_and_check("kexec", pid, WAIT_LOG); |
3502 | if (r < 0) | |
3503 | return r; | |
3504 | if (r > 0) | |
3505 | /* Command failed */ | |
3506 | return -EPROTO; | |
3507 | return 0; | |
4bb2e9d4 ZJS |
3508 | } |
3509 | ||
57ab9006 | 3510 | static int set_exit_code(uint8_t code) { |
4afd3348 | 3511 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
57ab9006 LP |
3512 | sd_bus *bus; |
3513 | int r; | |
3514 | ||
3515 | r = acquire_bus(BUS_MANAGER, &bus); | |
3516 | if (r < 0) | |
3517 | return r; | |
3518 | ||
3519 | r = sd_bus_call_method( | |
3520 | bus, | |
3521 | "org.freedesktop.systemd1", | |
3522 | "/org/freedesktop/systemd1", | |
3523 | "org.freedesktop.systemd1.Manager", | |
3524 | "SetExitCode", | |
3525 | &error, | |
3526 | NULL, | |
3527 | "y", code); | |
3528 | if (r < 0) | |
af3d8113 | 3529 | return log_error_errno(r, "Failed to set exit code: %s", bus_error_message(&error, r)); |
57ab9006 LP |
3530 | |
3531 | return 0; | |
3532 | } | |
3533 | ||
e449de87 | 3534 | static int start_special(int argc, char *argv[], void *userdata) { |
4c80c73c | 3535 | enum action a; |
983d9c90 | 3536 | int r; |
2b0f4e6f ZJS |
3537 | bool termination_action; /* an action that terminates the manager, |
3538 | * can be performed also by signal. */ | |
983d9c90 | 3539 | |
e449de87 | 3540 | assert(argv); |
514f4ef5 | 3541 | |
e449de87 | 3542 | a = verb_to_action(argv[0]); |
4c80c73c | 3543 | |
4fbd7192 | 3544 | r = logind_check_inhibitors(a); |
748ebafa LP |
3545 | if (r < 0) |
3546 | return r; | |
3547 | ||
fba868fa LP |
3548 | if (arg_force >= 2) { |
3549 | r = must_be_root(); | |
3550 | if (r < 0) | |
3551 | return r; | |
c32b90de LP |
3552 | } |
3553 | ||
4fbd7192 | 3554 | r = prepare_firmware_setup(); |
a4921cf4 JJ |
3555 | if (r < 0) |
3556 | return r; | |
5bdf2243 | 3557 | |
e449de87 | 3558 | if (a == ACTION_REBOOT && argc > 1) { |
27c06cb5 | 3559 | r = update_reboot_parameter_and_warn(argv[1]); |
b986229e SW |
3560 | if (r < 0) |
3561 | return r; | |
57ab9006 | 3562 | |
4bb2e9d4 ZJS |
3563 | } else if (a == ACTION_KEXEC) { |
3564 | r = load_kexec_kernel(); | |
d23b5ce2 ZJS |
3565 | if (r < 0 && arg_force >= 1) |
3566 | log_notice("Failed to load kexec kernel, continuing without."); | |
3567 | else if (r < 0) | |
4bb2e9d4 ZJS |
3568 | return r; |
3569 | ||
e449de87 | 3570 | } else if (a == ACTION_EXIT && argc > 1) { |
7bbb5359 | 3571 | uint8_t code; |
287419c1 | 3572 | |
57ab9006 LP |
3573 | /* If the exit code is not given on the command line, |
3574 | * don't reset it to zero: just keep it as it might | |
3575 | * have been set previously. */ | |
287419c1 | 3576 | |
e449de87 | 3577 | r = safe_atou8(argv[1], &code); |
4fbd7192 | 3578 | if (r < 0) |
57ab9006 | 3579 | return log_error_errno(r, "Invalid exit code."); |
4fbd7192 | 3580 | |
57ab9006 | 3581 | r = set_exit_code(code); |
691395d8 | 3582 | if (r < 0) |
57ab9006 | 3583 | return r; |
b986229e SW |
3584 | } |
3585 | ||
2b0f4e6f ZJS |
3586 | termination_action = IN_SET(a, |
3587 | ACTION_HALT, | |
3588 | ACTION_POWEROFF, | |
3589 | ACTION_REBOOT); | |
3590 | if (termination_action && arg_force >= 2) | |
477def80 | 3591 | return halt_now(a); |
e606bb61 | 3592 | |
7e59bfcb | 3593 | if (arg_force >= 1 && |
5955df2c | 3594 | (termination_action || IN_SET(a, ACTION_KEXEC, ACTION_EXIT))) |
2b0f4e6f ZJS |
3595 | r = trivial_method(argc, argv, userdata); |
3596 | else { | |
3597 | /* First try logind, to allow authentication with polkit */ | |
3598 | if (IN_SET(a, | |
3599 | ACTION_POWEROFF, | |
3600 | ACTION_REBOOT, | |
36b69c31 | 3601 | ACTION_HALT, |
2b0f4e6f ZJS |
3602 | ACTION_SUSPEND, |
3603 | ACTION_HIBERNATE, | |
c58493c0 | 3604 | ACTION_HYBRID_SLEEP, |
e68c79db | 3605 | ACTION_SUSPEND_THEN_HIBERNATE)) { |
2b0f4e6f ZJS |
3606 | |
3607 | r = logind_reboot(a); | |
3608 | if (r >= 0) | |
3609 | return r; | |
3610 | if (IN_SET(r, -EOPNOTSUPP, -EINPROGRESS)) | |
3611 | /* requested operation is not supported or already in progress */ | |
3612 | return r; | |
20b09ca7 | 3613 | |
130246d2 LP |
3614 | /* On all other errors, try low-level operation. In order to minimize the difference between |
3615 | * operation with and without logind, we explicitly enable non-blocking mode for this, as | |
3616 | * logind's shutdown operations are always non-blocking. */ | |
3617 | ||
3618 | arg_no_block = true; | |
3619 | ||
3620 | } else if (IN_SET(a, ACTION_EXIT, ACTION_KEXEC)) | |
3621 | /* Since exit/kexec are so close in behaviour to power-off/reboot, let's also make them | |
3622 | * asynchronous, in order to not confuse the user needlessly with unexpected behaviour. */ | |
3623 | arg_no_block = true; | |
7089051f | 3624 | |
2b0f4e6f | 3625 | r = start_unit(argc, argv, userdata); |
4c80c73c | 3626 | } |
983d9c90 | 3627 | |
2b0f4e6f ZJS |
3628 | if (termination_action && arg_force < 2 && |
3629 | IN_SET(r, -ENOENT, -ETIMEDOUT)) | |
cc7de2ba | 3630 | log_notice("It is possible to perform action directly, see discussion of --force --force in man:systemctl(1)."); |
2b0f4e6f ZJS |
3631 | |
3632 | return r; | |
514f4ef5 LP |
3633 | } |
3634 | ||
988b3b17 ZJS |
3635 | static int start_system_special(int argc, char *argv[], void *userdata) { |
3636 | /* Like start_special above, but raises an error when running in user mode */ | |
3637 | ||
3638 | if (arg_scope != UNIT_FILE_SYSTEM) { | |
3639 | log_error("Bad action for %s mode.", | |
3640 | arg_scope == UNIT_FILE_GLOBAL ? "--global" : "--user"); | |
3641 | return -EINVAL; | |
3642 | } | |
3643 | ||
3644 | return start_special(argc, argv, userdata); | |
3645 | } | |
3646 | ||
fa0d5878 | 3647 | static int check_unit_generic(int code, const UnitActiveState good_states[], int nb_states, char **args) { |
e3e0314b | 3648 | _cleanup_strv_free_ char **names = NULL; |
fa0d5878 | 3649 | UnitActiveState active_state; |
4fbd7192 | 3650 | sd_bus *bus; |
729e3769 | 3651 | char **name; |
fa0d5878 | 3652 | int r, i; |
d60f6ad0 | 3653 | bool found = false; |
0183528f | 3654 | |
4fbd7192 LP |
3655 | r = acquire_bus(BUS_MANAGER, &bus); |
3656 | if (r < 0) | |
3657 | return r; | |
3658 | ||
e3e0314b | 3659 | r = expand_names(bus, args, NULL, &names); |
f647962d MS |
3660 | if (r < 0) |
3661 | return log_error_errno(r, "Failed to expand names: %m"); | |
e3e0314b ZJS |
3662 | |
3663 | STRV_FOREACH(name, names) { | |
fa0d5878 BR |
3664 | r = get_state_one_unit(bus, *name, &active_state); |
3665 | if (r < 0) | |
3666 | return r; | |
3667 | ||
3668 | if (!arg_quiet) | |
3669 | puts(unit_active_state_to_string(active_state)); | |
60f9ba0b | 3670 | |
fa0d5878 BR |
3671 | for (i = 0; i < nb_states; ++i) |
3672 | if (good_states[i] == active_state) | |
3673 | found = true; | |
1a0fce45 TA |
3674 | } |
3675 | ||
d60f6ad0 LN |
3676 | /* use the given return code for the case that we won't find |
3677 | * any unit which matches the list */ | |
3678 | return found ? 0 : code; | |
1a0fce45 TA |
3679 | } |
3680 | ||
e449de87 | 3681 | static int check_unit_active(int argc, char *argv[], void *userdata) { |
fa0d5878 | 3682 | const UnitActiveState states[] = { UNIT_ACTIVE, UNIT_RELOADING }; |
e3e0314b | 3683 | /* According to LSB: 3, "program is not running" */ |
b613907e | 3684 | return check_unit_generic(EXIT_PROGRAM_NOT_RUNNING, states, ELEMENTSOF(states), strv_skip(argv, 1)); |
e3e0314b | 3685 | } |
0183528f | 3686 | |
e449de87 | 3687 | static int check_unit_failed(int argc, char *argv[], void *userdata) { |
fa0d5878 | 3688 | const UnitActiveState states[] = { UNIT_FAILED }; |
b613907e | 3689 | return check_unit_generic(EXIT_PROGRAM_DEAD_AND_PID_EXISTS, states, ELEMENTSOF(states), strv_skip(argv, 1)); |
48220598 LP |
3690 | } |
3691 | ||
e449de87 | 3692 | static int kill_unit(int argc, char *argv[], void *userdata) { |
e3e0314b | 3693 | _cleanup_strv_free_ char **names = NULL; |
ac5e3a50 | 3694 | char *kill_who = NULL, **name; |
4fbd7192 | 3695 | sd_bus *bus; |
e3e0314b | 3696 | int r, q; |
8a0867d6 | 3697 | |
4fbd7192 LP |
3698 | r = acquire_bus(BUS_MANAGER, &bus); |
3699 | if (r < 0) | |
3700 | return r; | |
3701 | ||
8a4b13c5 | 3702 | polkit_agent_open_maybe(); |
d2ad7e1b | 3703 | |
8a0867d6 LP |
3704 | if (!arg_kill_who) |
3705 | arg_kill_who = "all"; | |
3706 | ||
ac5e3a50 JS |
3707 | /* --fail was specified */ |
3708 | if (streq(arg_job_mode, "fail")) | |
81d62103 | 3709 | kill_who = strjoina(arg_kill_who, "-fail"); |
ac5e3a50 | 3710 | |
e449de87 | 3711 | r = expand_names(bus, strv_skip(argv, 1), NULL, &names); |
e3e0314b | 3712 | if (r < 0) |
691395d8 | 3713 | return log_error_errno(r, "Failed to expand names: %m"); |
60f9ba0b | 3714 | |
e3e0314b | 3715 | STRV_FOREACH(name, names) { |
4afd3348 | 3716 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
342641fb | 3717 | |
6e646d22 | 3718 | q = sd_bus_call_method( |
f22f08cd | 3719 | bus, |
b0193f1c LP |
3720 | "org.freedesktop.systemd1", |
3721 | "/org/freedesktop/systemd1", | |
3722 | "org.freedesktop.systemd1.Manager", | |
6e646d22 LP |
3723 | "KillUnit", |
3724 | &error, | |
3725 | NULL, | |
169d7fb6 | 3726 | "ssi", *name, kill_who ? kill_who : arg_kill_who, arg_signal); |
e3e0314b | 3727 | if (q < 0) { |
169d7fb6 | 3728 | log_error_errno(q, "Failed to kill unit %s: %s", *name, bus_error_message(&error, q)); |
e3e0314b ZJS |
3729 | if (r == 0) |
3730 | r = q; | |
f459b602 | 3731 | } |
8a0867d6 | 3732 | } |
f459b602 | 3733 | |
e3e0314b | 3734 | return r; |
8a0867d6 LP |
3735 | } |
3736 | ||
582a507f | 3737 | typedef struct ExecStatusInfo { |
0129173a LP |
3738 | char *name; |
3739 | ||
582a507f LP |
3740 | char *path; |
3741 | char **argv; | |
3742 | ||
b708e7ce LP |
3743 | bool ignore; |
3744 | ||
582a507f LP |
3745 | usec_t start_timestamp; |
3746 | usec_t exit_timestamp; | |
3747 | pid_t pid; | |
3748 | int code; | |
3749 | int status; | |
3750 | ||
3751 | LIST_FIELDS(struct ExecStatusInfo, exec); | |
3752 | } ExecStatusInfo; | |
3753 | ||
3754 | static void exec_status_info_free(ExecStatusInfo *i) { | |
3755 | assert(i); | |
3756 | ||
0129173a | 3757 | free(i->name); |
582a507f LP |
3758 | free(i->path); |
3759 | strv_free(i->argv); | |
3760 | free(i); | |
3761 | } | |
3762 | ||
f459b602 | 3763 | static int exec_status_info_deserialize(sd_bus_message *m, ExecStatusInfo *i) { |
b21a0ef8 | 3764 | uint64_t start_timestamp, exit_timestamp, start_timestamp_monotonic, exit_timestamp_monotonic; |
f459b602 | 3765 | const char *path; |
582a507f LP |
3766 | uint32_t pid; |
3767 | int32_t code, status; | |
f459b602 | 3768 | int ignore, r; |
582a507f | 3769 | |
f459b602 | 3770 | assert(m); |
582a507f | 3771 | assert(i); |
582a507f | 3772 | |
f459b602 MAP |
3773 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_STRUCT, "sasbttttuii"); |
3774 | if (r < 0) | |
3775 | return bus_log_parse_error(r); | |
3776 | else if (r == 0) | |
3777 | return 0; | |
582a507f | 3778 | |
f459b602 MAP |
3779 | r = sd_bus_message_read(m, "s", &path); |
3780 | if (r < 0) | |
3781 | return bus_log_parse_error(r); | |
582a507f | 3782 | |
f84190d8 LP |
3783 | i->path = strdup(path); |
3784 | if (!i->path) | |
f459b602 | 3785 | return log_oom(); |
582a507f | 3786 | |
f459b602 MAP |
3787 | r = sd_bus_message_read_strv(m, &i->argv); |
3788 | if (r < 0) | |
3789 | return bus_log_parse_error(r); | |
3790 | ||
3791 | r = sd_bus_message_read(m, | |
3792 | "bttttuii", | |
3793 | &ignore, | |
3794 | &start_timestamp, &start_timestamp_monotonic, | |
3795 | &exit_timestamp, &exit_timestamp_monotonic, | |
3796 | &pid, | |
3797 | &code, &status); | |
3798 | if (r < 0) | |
3799 | return bus_log_parse_error(r); | |
582a507f | 3800 | |
b708e7ce | 3801 | i->ignore = ignore; |
582a507f LP |
3802 | i->start_timestamp = (usec_t) start_timestamp; |
3803 | i->exit_timestamp = (usec_t) exit_timestamp; | |
3804 | i->pid = (pid_t) pid; | |
3805 | i->code = code; | |
3806 | i->status = status; | |
3807 | ||
f459b602 MAP |
3808 | r = sd_bus_message_exit_container(m); |
3809 | if (r < 0) | |
3810 | return bus_log_parse_error(r); | |
3811 | ||
3812 | return 1; | |
582a507f LP |
3813 | } |
3814 | ||
d5db7fe6 WC |
3815 | typedef struct UnitCondition { |
3816 | char *name; | |
b1ed76ae | 3817 | char *param; |
d5db7fe6 WC |
3818 | bool trigger; |
3819 | bool negate; | |
d5db7fe6 WC |
3820 | int tristate; |
3821 | ||
b1ed76ae | 3822 | LIST_FIELDS(struct UnitCondition, conditions); |
d5db7fe6 WC |
3823 | } UnitCondition; |
3824 | ||
3825 | static void unit_condition_free(UnitCondition *c) { | |
9bb71940 ZJS |
3826 | if (!c) |
3827 | return; | |
d5db7fe6 WC |
3828 | |
3829 | free(c->name); | |
3830 | free(c->param); | |
3831 | free(c); | |
3832 | } | |
3833 | ||
9bb71940 ZJS |
3834 | DEFINE_TRIVIAL_CLEANUP_FUNC(UnitCondition*, unit_condition_free); |
3835 | ||
61cbdc4b LP |
3836 | typedef struct UnitStatusInfo { |
3837 | const char *id; | |
3838 | const char *load_state; | |
3839 | const char *active_state; | |
3840 | const char *sub_state; | |
a4375746 | 3841 | const char *unit_file_state; |
d2dc52db | 3842 | const char *unit_file_preset; |
61cbdc4b LP |
3843 | |
3844 | const char *description; | |
4a9e2fff | 3845 | const char *following; |
61cbdc4b | 3846 | |
49dbfa7b LP |
3847 | char **documentation; |
3848 | ||
1b64d026 LP |
3849 | const char *fragment_path; |
3850 | const char *source_path; | |
4ad49000 | 3851 | const char *control_group; |
61cbdc4b | 3852 | |
76d14b87 OS |
3853 | char **dropin_paths; |
3854 | ||
9f39404c | 3855 | const char *load_error; |
f42806df | 3856 | const char *result; |
9f39404c | 3857 | |
584be568 | 3858 | usec_t inactive_exit_timestamp; |
df50185b | 3859 | usec_t inactive_exit_timestamp_monotonic; |
584be568 LP |
3860 | usec_t active_enter_timestamp; |
3861 | usec_t active_exit_timestamp; | |
3862 | usec_t inactive_enter_timestamp; | |
3863 | ||
45fb0699 | 3864 | bool need_daemon_reload; |
055ef36b | 3865 | bool transient; |
45fb0699 | 3866 | |
61cbdc4b LP |
3867 | /* Service */ |
3868 | pid_t main_pid; | |
3869 | pid_t control_pid; | |
3870 | const char *status_text; | |
175728c4 | 3871 | const char *pid_file; |
d06dacd0 | 3872 | bool running:1; |
b4af5a80 | 3873 | int status_errno; |
61cbdc4b LP |
3874 | |
3875 | usec_t start_timestamp; | |
3876 | usec_t exit_timestamp; | |
3877 | ||
3878 | int exit_code, exit_status; | |
3879 | ||
90bbc946 LP |
3880 | usec_t condition_timestamp; |
3881 | bool condition_result; | |
b1ed76ae | 3882 | LIST_HEAD(UnitCondition, conditions); |
59fccdc5 LP |
3883 | |
3884 | usec_t assert_timestamp; | |
3885 | bool assert_result; | |
3886 | bool failed_assert_trigger; | |
3887 | bool failed_assert_negate; | |
3888 | const char *failed_assert; | |
3889 | const char *failed_assert_parameter; | |
601b0842 GS |
3890 | usec_t next_elapse_real; |
3891 | usec_t next_elapse_monotonic; | |
90bbc946 | 3892 | |
61cbdc4b LP |
3893 | /* Socket */ |
3894 | unsigned n_accepted; | |
3895 | unsigned n_connections; | |
b8131a87 | 3896 | bool accept; |
61cbdc4b | 3897 | |
13160134 | 3898 | /* Pairs of type, path */ |
67419600 OS |
3899 | char **listen; |
3900 | ||
61cbdc4b LP |
3901 | /* Device */ |
3902 | const char *sysfs_path; | |
3903 | ||
3904 | /* Mount, Automount */ | |
3905 | const char *where; | |
3906 | ||
3907 | /* Swap */ | |
3908 | const char *what; | |
582a507f | 3909 | |
934277fe LP |
3910 | /* CGroup */ |
3911 | uint64_t memory_current; | |
da4d897e TH |
3912 | uint64_t memory_low; |
3913 | uint64_t memory_high; | |
3914 | uint64_t memory_max; | |
96e131ea | 3915 | uint64_t memory_swap_max; |
934277fe | 3916 | uint64_t memory_limit; |
5ad096b3 | 3917 | uint64_t cpu_usage_nsec; |
03a7b521 LP |
3918 | uint64_t tasks_current; |
3919 | uint64_t tasks_max; | |
934277fe | 3920 | |
0e97c93f DM |
3921 | uint64_t ip_ingress_bytes; |
3922 | uint64_t ip_egress_bytes; | |
3923 | ||
582a507f | 3924 | LIST_HEAD(ExecStatusInfo, exec); |
61cbdc4b LP |
3925 | } UnitStatusInfo; |
3926 | ||
a7335518 ZJS |
3927 | static void unit_status_info_free(UnitStatusInfo *info) { |
3928 | ExecStatusInfo *p; | |
3929 | UnitCondition *c; | |
3930 | ||
3931 | strv_free(info->documentation); | |
3932 | strv_free(info->dropin_paths); | |
3933 | strv_free(info->listen); | |
3934 | ||
3935 | while ((c = info->conditions)) { | |
3936 | LIST_REMOVE(conditions, info->conditions, c); | |
3937 | unit_condition_free(c); | |
3938 | } | |
3939 | ||
3940 | while ((p = info->exec)) { | |
3941 | LIST_REMOVE(exec, info->exec, p); | |
3942 | exec_status_info_free(p); | |
3943 | } | |
3944 | } | |
3945 | ||
f459b602 | 3946 | static void print_status_info( |
291d565a | 3947 | sd_bus *bus, |
f459b602 MAP |
3948 | UnitStatusInfo *i, |
3949 | bool *ellipsized) { | |
3950 | ||
c7080257 | 3951 | char since1[FORMAT_TIMESTAMP_RELATIVE_MAX], *s1, since2[FORMAT_TIMESTAMP_MAX], *s2; |
b0d14c69 | 3952 | const char *active_on, *active_off, *on, *off, *ss; |
c7080257 LP |
3953 | _cleanup_free_ char *formatted_path = NULL; |
3954 | ExecStatusInfo *p; | |
584be568 | 3955 | usec_t timestamp; |
1b64d026 | 3956 | const char *path; |
13160134 | 3957 | char **t, **t2; |
291d565a | 3958 | int r; |
582a507f | 3959 | |
61cbdc4b LP |
3960 | assert(i); |
3961 | ||
3962 | /* This shows pretty information about a unit. See | |
3963 | * print_property() for a low-level property printer */ | |
3964 | ||
b0d14c69 LP |
3965 | if (streq_ptr(i->active_state, "failed")) { |
3966 | active_on = ansi_highlight_red(); | |
1fc464f6 | 3967 | active_off = ansi_normal(); |
1cf03a4f | 3968 | } else if (STRPTR_IN_SET(i->active_state, "active", "reloading")) { |
b0d14c69 | 3969 | active_on = ansi_highlight_green(); |
1fc464f6 | 3970 | active_off = ansi_normal(); |
b0d14c69 LP |
3971 | } else |
3972 | active_on = active_off = ""; | |
3973 | ||
323b7dc9 | 3974 | printf("%s%s%s %s", active_on, special_glyph(BLACK_CIRCLE), active_off, strna(i->id)); |
61cbdc4b LP |
3975 | |
3976 | if (i->description && !streq_ptr(i->id, i->description)) | |
3977 | printf(" - %s", i->description); | |
3978 | ||
3979 | printf("\n"); | |
3980 | ||
4a9e2fff | 3981 | if (i->following) |
856323c9 | 3982 | printf(" Follow: unit currently follows state of %s\n", i->following); |
4a9e2fff | 3983 | |
f7b9e331 | 3984 | if (streq_ptr(i->load_state, "error")) { |
0b5a519c | 3985 | on = ansi_highlight_red(); |
1fc464f6 | 3986 | off = ansi_normal(); |
c31b4423 LP |
3987 | } else |
3988 | on = off = ""; | |
3989 | ||
c7080257 LP |
3990 | path = i->source_path ?: i->fragment_path; |
3991 | if (path && terminal_urlify_path(path, NULL, &formatted_path) >= 0) | |
3992 | path = formatted_path; | |
1b64d026 | 3993 | |
055ef36b | 3994 | if (i->load_error != 0) |
856323c9 ZJS |
3995 | printf(" Loaded: %s%s%s (Reason: %s)\n", |
3996 | on, strna(i->load_state), off, i->load_error); | |
15ce1647 LP |
3997 | else if (path && !isempty(i->unit_file_state) && !isempty(i->unit_file_preset) && |
3998 | !STR_IN_SET(i->unit_file_state, "generated", "transient")) | |
d2dc52db LP |
3999 | printf(" Loaded: %s%s%s (%s; %s; vendor preset: %s)\n", |
4000 | on, strna(i->load_state), off, path, i->unit_file_state, i->unit_file_preset); | |
4001 | else if (path && !isempty(i->unit_file_state)) | |
856323c9 ZJS |
4002 | printf(" Loaded: %s%s%s (%s; %s)\n", |
4003 | on, strna(i->load_state), off, path, i->unit_file_state); | |
1b64d026 | 4004 | else if (path) |
856323c9 ZJS |
4005 | printf(" Loaded: %s%s%s (%s)\n", |
4006 | on, strna(i->load_state), off, path); | |
61cbdc4b | 4007 | else |
856323c9 ZJS |
4008 | printf(" Loaded: %s%s%s\n", |
4009 | on, strna(i->load_state), off); | |
61cbdc4b | 4010 | |
055ef36b LP |
4011 | if (i->transient) |
4012 | printf("Transient: yes\n"); | |
4013 | ||
76d14b87 | 4014 | if (!strv_isempty(i->dropin_paths)) { |
f459b602 | 4015 | _cleanup_free_ char *dir = NULL; |
76d14b87 | 4016 | bool last = false; |
f459b602 | 4017 | char ** dropin; |
76d14b87 OS |
4018 | |
4019 | STRV_FOREACH(dropin, i->dropin_paths) { | |
c7080257 LP |
4020 | _cleanup_free_ char *dropin_formatted = NULL; |
4021 | const char *df; | |
4022 | ||
eb2c3192 LP |
4023 | if (!dir || last) { |
4024 | printf(dir ? " " : | |
4025 | " Drop-In: "); | |
76d14b87 | 4026 | |
97b11eed | 4027 | dir = mfree(dir); |
76d14b87 | 4028 | |
5f311f8c LP |
4029 | dir = dirname_malloc(*dropin); |
4030 | if (!dir) { | |
76d14b87 OS |
4031 | log_oom(); |
4032 | return; | |
4033 | } | |
4034 | ||
eb2c3192 LP |
4035 | printf("%s\n" |
4036 | " %s", dir, | |
323b7dc9 | 4037 | special_glyph(TREE_RIGHT)); |
76d14b87 OS |
4038 | } |
4039 | ||
4040 | last = ! (*(dropin + 1) && startswith(*(dropin + 1), dir)); | |
4041 | ||
c7080257 LP |
4042 | if (terminal_urlify_path(*dropin, basename(*dropin), &dropin_formatted) >= 0) |
4043 | df = dropin_formatted; | |
4044 | else | |
4045 | df = *dropin; | |
4046 | ||
4047 | printf("%s%s", df, last ? "\n" : ", "); | |
76d14b87 | 4048 | } |
76d14b87 OS |
4049 | } |
4050 | ||
2ee68f72 | 4051 | ss = streq_ptr(i->active_state, i->sub_state) ? NULL : i->sub_state; |
2ee68f72 | 4052 | if (ss) |
856323c9 | 4053 | printf(" Active: %s%s (%s)%s", |
b0d14c69 | 4054 | active_on, strna(i->active_state), ss, active_off); |
2ee68f72 | 4055 | else |
856323c9 | 4056 | printf(" Active: %s%s%s", |
b0d14c69 | 4057 | active_on, strna(i->active_state), active_off); |
61cbdc4b | 4058 | |
f42806df LP |
4059 | if (!isempty(i->result) && !streq(i->result, "success")) |
4060 | printf(" (Result: %s)", i->result); | |
4061 | ||
1cf03a4f ZJS |
4062 | timestamp = STRPTR_IN_SET(i->active_state, "active", "reloading") ? i->active_enter_timestamp : |
4063 | STRPTR_IN_SET(i->active_state, "inactive", "failed") ? i->inactive_enter_timestamp : | |
4064 | STRPTR_IN_SET(i->active_state, "activating") ? i->inactive_exit_timestamp : | |
4065 | i->active_exit_timestamp; | |
584be568 | 4066 | |
bbb8486e | 4067 | s1 = format_timestamp_relative(since1, sizeof(since1), timestamp); |
584be568 LP |
4068 | s2 = format_timestamp(since2, sizeof(since2), timestamp); |
4069 | ||
4070 | if (s1) | |
538da63d | 4071 | printf(" since %s; %s\n", s2, s1); |
584be568 | 4072 | else if (s2) |
538da63d | 4073 | printf(" since %s\n", s2); |
584be568 LP |
4074 | else |
4075 | printf("\n"); | |
4076 | ||
601b0842 GS |
4077 | if (endswith(i->id, ".timer")) { |
4078 | char tstamp1[FORMAT_TIMESTAMP_RELATIVE_MAX], | |
4079 | tstamp2[FORMAT_TIMESTAMP_MAX]; | |
4080 | char *next_rel_time, *next_time; | |
4081 | dual_timestamp nw, next = {i->next_elapse_real, | |
4082 | i->next_elapse_monotonic}; | |
4083 | usec_t next_elapse; | |
4084 | ||
4085 | printf(" Trigger: "); | |
4086 | ||
4087 | dual_timestamp_get(&nw); | |
4088 | next_elapse = calc_next_elapse(&nw, &next); | |
4089 | next_rel_time = format_timestamp_relative(tstamp1, | |
4090 | sizeof(tstamp1), | |
4091 | next_elapse); | |
4092 | next_time = format_timestamp(tstamp2, | |
4093 | sizeof(tstamp2), | |
4094 | next_elapse); | |
4095 | ||
4096 | if (next_time && next_rel_time) | |
4097 | printf("%s; %s\n", next_time, next_rel_time); | |
4098 | else | |
4099 | printf("n/a\n"); | |
4100 | } | |
4101 | ||
90bbc946 | 4102 | if (!i->condition_result && i->condition_timestamp > 0) { |
d5db7fe6 WC |
4103 | UnitCondition *c; |
4104 | int n = 0; | |
4105 | ||
bbb8486e | 4106 | s1 = format_timestamp_relative(since1, sizeof(since1), i->condition_timestamp); |
90bbc946 LP |
4107 | s2 = format_timestamp(since2, sizeof(since2), i->condition_timestamp); |
4108 | ||
59fccdc5 | 4109 | printf("Condition: start %scondition failed%s at %s%s%s\n", |
1fc464f6 | 4110 | ansi_highlight_yellow(), ansi_normal(), |
5cfee414 | 4111 | s2, s1 ? "; " : "", strempty(s1)); |
d5db7fe6 | 4112 | |
b1ed76ae | 4113 | LIST_FOREACH(conditions, c, i->conditions) |
d5db7fe6 WC |
4114 | if (c->tristate < 0) |
4115 | n++; | |
d5db7fe6 | 4116 | |
b1ed76ae ZJS |
4117 | LIST_FOREACH(conditions, c, i->conditions) |
4118 | if (c->tristate < 0) | |
4119 | printf(" %s %s=%s%s%s was not met\n", | |
4120 | --n ? special_glyph(TREE_BRANCH) : special_glyph(TREE_RIGHT), | |
4121 | c->name, | |
4122 | c->trigger ? "|" : "", | |
4123 | c->negate ? "!" : "", | |
4124 | c->param); | |
59fccdc5 LP |
4125 | } |
4126 | ||
4127 | if (!i->assert_result && i->assert_timestamp > 0) { | |
4128 | s1 = format_timestamp_relative(since1, sizeof(since1), i->assert_timestamp); | |
4129 | s2 = format_timestamp(since2, sizeof(since2), i->assert_timestamp); | |
4130 | ||
4131 | printf(" Assert: start %sassertion failed%s at %s%s%s\n", | |
1fc464f6 | 4132 | ansi_highlight_red(), ansi_normal(), |
5cfee414 | 4133 | s2, s1 ? "; " : "", strempty(s1)); |
59fccdc5 LP |
4134 | if (i->failed_assert_trigger) |
4135 | printf(" none of the trigger assertions were met\n"); | |
4136 | else if (i->failed_assert) | |
4137 | printf(" %s=%s%s was not met\n", | |
4138 | i->failed_assert, | |
4139 | i->failed_assert_negate ? "!" : "", | |
4140 | i->failed_assert_parameter); | |
90bbc946 LP |
4141 | } |
4142 | ||
61cbdc4b | 4143 | if (i->sysfs_path) |
856323c9 | 4144 | printf(" Device: %s\n", i->sysfs_path); |
9feeba4b | 4145 | if (i->where) |
856323c9 | 4146 | printf(" Where: %s\n", i->where); |
9feeba4b | 4147 | if (i->what) |
856323c9 | 4148 | printf(" What: %s\n", i->what); |
49dbfa7b | 4149 | |
78c7d20e LP |
4150 | STRV_FOREACH(t, i->documentation) { |
4151 | _cleanup_free_ char *formatted = NULL; | |
4152 | const char *q; | |
4153 | ||
4154 | if (terminal_urlify(*t, NULL, &formatted) >= 0) | |
4155 | q = formatted; | |
4156 | else | |
4157 | q = *t; | |
4158 | ||
4159 | printf(" %*s %s\n", 9, t == i->documentation ? "Docs:" : "", q); | |
4160 | } | |
49dbfa7b | 4161 | |
13160134 | 4162 | STRV_FOREACH_PAIR(t, t2, i->listen) |
856323c9 | 4163 | printf(" %*s %s (%s)\n", 9, t == i->listen ? "Listen:" : "", *t2, *t); |
67419600 | 4164 | |
b8131a87 | 4165 | if (i->accept) |
856323c9 | 4166 | printf(" Accepted: %u; Connected: %u\n", i->n_accepted, i->n_connections); |
61cbdc4b | 4167 | |
582a507f | 4168 | LIST_FOREACH(exec, p, i->exec) { |
13160134 | 4169 | _cleanup_free_ char *argv = NULL; |
9a57c629 | 4170 | bool good; |
582a507f LP |
4171 | |
4172 | /* Only show exited processes here */ | |
4173 | if (p->code == 0) | |
4174 | continue; | |
4175 | ||
13160134 | 4176 | argv = strv_join(p->argv, " "); |
1fa2f38f | 4177 | printf(" Process: "PID_FMT" %s=%s ", p->pid, p->name, strna(argv)); |
582a507f | 4178 | |
1f0958f6 | 4179 | good = is_clean_exit(p->code, p->status, EXIT_CLEAN_DAEMON, NULL); |
9a57c629 | 4180 | if (!good) { |
0b5a519c | 4181 | on = ansi_highlight_red(); |
1fc464f6 | 4182 | off = ansi_normal(); |
9a57c629 LP |
4183 | } else |
4184 | on = off = ""; | |
4185 | ||
4186 | printf("%s(code=%s, ", on, sigchld_code_to_string(p->code)); | |
4187 | ||
d06dacd0 LP |
4188 | if (p->code == CLD_EXITED) { |
4189 | const char *c; | |
4190 | ||
582a507f | 4191 | printf("status=%i", p->status); |
d06dacd0 | 4192 | |
1b64d026 LP |
4193 | c = exit_status_to_string(p->status, EXIT_STATUS_SYSTEMD); |
4194 | if (c) | |
d06dacd0 LP |
4195 | printf("/%s", c); |
4196 | ||
4197 | } else | |
582a507f | 4198 | printf("signal=%s", signal_to_string(p->status)); |
9a57c629 LP |
4199 | |
4200 | printf(")%s\n", off); | |
4201 | ||
582a507f LP |
4202 | if (i->main_pid == p->pid && |
4203 | i->start_timestamp == p->start_timestamp && | |
4204 | i->exit_timestamp == p->start_timestamp) | |
4205 | /* Let's not show this twice */ | |
4206 | i->main_pid = 0; | |
4207 | ||
4208 | if (p->pid == i->control_pid) | |
4209 | i->control_pid = 0; | |
4210 | } | |
4211 | ||
61cbdc4b | 4212 | if (i->main_pid > 0 || i->control_pid > 0) { |
61cbdc4b | 4213 | if (i->main_pid > 0) { |
8c06592f | 4214 | printf(" Main PID: "PID_FMT, i->main_pid); |
61cbdc4b LP |
4215 | |
4216 | if (i->running) { | |
a081b9ce LP |
4217 | |
4218 | if (arg_transport == BUS_TRANSPORT_LOCAL) { | |
4219 | _cleanup_free_ char *comm = NULL; | |
4220 | ||
4221 | (void) get_process_comm(i->main_pid, &comm); | |
4222 | if (comm) | |
4223 | printf(" (%s)", comm); | |
4224 | } | |
4225 | ||
6d4fc029 | 4226 | } else if (i->exit_code > 0) { |
61cbdc4b LP |
4227 | printf(" (code=%s, ", sigchld_code_to_string(i->exit_code)); |
4228 | ||
d06dacd0 LP |
4229 | if (i->exit_code == CLD_EXITED) { |
4230 | const char *c; | |
4231 | ||
61cbdc4b | 4232 | printf("status=%i", i->exit_status); |
d06dacd0 | 4233 | |
1b64d026 LP |
4234 | c = exit_status_to_string(i->exit_status, EXIT_STATUS_SYSTEMD); |
4235 | if (c) | |
d06dacd0 LP |
4236 | printf("/%s", c); |
4237 | ||
4238 | } else | |
582a507f | 4239 | printf("signal=%s", signal_to_string(i->exit_status)); |
6d4fc029 LP |
4240 | printf(")"); |
4241 | } | |
13160134 | 4242 | } |
61cbdc4b LP |
4243 | |
4244 | if (i->control_pid > 0) { | |
13160134 | 4245 | _cleanup_free_ char *c = NULL; |
61cbdc4b | 4246 | |
1089dcd4 LP |
4247 | if (i->main_pid > 0) |
4248 | fputs("; Control PID: ", stdout); | |
4249 | else | |
4250 | fputs("Cntrl PID: ", stdout); /* if first in column, abbreviated so it fits alignment */ | |
4251 | ||
4252 | printf(PID_FMT, i->control_pid); | |
61cbdc4b | 4253 | |
a081b9ce LP |
4254 | if (arg_transport == BUS_TRANSPORT_LOCAL) { |
4255 | (void) get_process_comm(i->control_pid, &c); | |
4256 | if (c) | |
4257 | printf(" (%s)", c); | |
4258 | } | |
61cbdc4b LP |
4259 | } |
4260 | ||
4261 | printf("\n"); | |
4262 | } | |
4263 | ||
17bb7382 | 4264 | if (i->status_text) |
856323c9 | 4265 | printf(" Status: \"%s\"\n", i->status_text); |
b4af5a80 LP |
4266 | if (i->status_errno > 0) |
4267 | printf(" Error: %i (%s)\n", i->status_errno, strerror(i->status_errno)); | |
17bb7382 | 4268 | |
0e97c93f DM |
4269 | if (i->ip_ingress_bytes != (uint64_t) -1 && i->ip_egress_bytes != (uint64_t) -1) { |
4270 | char buf_in[FORMAT_BYTES_MAX], buf_out[FORMAT_BYTES_MAX]; | |
4271 | ||
4272 | printf(" IP: %s in, %s out\n", | |
4273 | format_bytes(buf_in, sizeof(buf_in), i->ip_ingress_bytes), | |
4274 | format_bytes(buf_out, sizeof(buf_out), i->ip_egress_bytes)); | |
4275 | } | |
4276 | ||
03a7b521 LP |
4277 | if (i->tasks_current != (uint64_t) -1) { |
4278 | printf(" Tasks: %" PRIu64, i->tasks_current); | |
4279 | ||
4280 | if (i->tasks_max != (uint64_t) -1) | |
2a6736dd | 4281 | printf(" (limit: %" PRIu64 ")\n", i->tasks_max); |
03a7b521 LP |
4282 | else |
4283 | printf("\n"); | |
4284 | } | |
4285 | ||
934277fe LP |
4286 | if (i->memory_current != (uint64_t) -1) { |
4287 | char buf[FORMAT_BYTES_MAX]; | |
4288 | ||
4289 | printf(" Memory: %s", format_bytes(buf, sizeof(buf), i->memory_current)); | |
4290 | ||
96e131ea WC |
4291 | if (i->memory_low > 0 || i->memory_high != CGROUP_LIMIT_MAX || |
4292 | i->memory_max != CGROUP_LIMIT_MAX || i->memory_swap_max != CGROUP_LIMIT_MAX || | |
da4d897e TH |
4293 | i->memory_limit != CGROUP_LIMIT_MAX) { |
4294 | const char *prefix = ""; | |
4295 | ||
4296 | printf(" ("); | |
4297 | if (i->memory_low > 0) { | |
4298 | printf("%slow: %s", prefix, format_bytes(buf, sizeof(buf), i->memory_low)); | |
4299 | prefix = " "; | |
4300 | } | |
4301 | if (i->memory_high != CGROUP_LIMIT_MAX) { | |
4302 | printf("%shigh: %s", prefix, format_bytes(buf, sizeof(buf), i->memory_high)); | |
4303 | prefix = " "; | |
4304 | } | |
4305 | if (i->memory_max != CGROUP_LIMIT_MAX) { | |
4306 | printf("%smax: %s", prefix, format_bytes(buf, sizeof(buf), i->memory_max)); | |
4307 | prefix = " "; | |
4308 | } | |
96e131ea WC |
4309 | if (i->memory_swap_max != CGROUP_LIMIT_MAX) { |
4310 | printf("%sswap max: %s", prefix, format_bytes(buf, sizeof(buf), i->memory_swap_max)); | |
4311 | prefix = " "; | |
4312 | } | |
da4d897e TH |
4313 | if (i->memory_limit != CGROUP_LIMIT_MAX) { |
4314 | printf("%slimit: %s", prefix, format_bytes(buf, sizeof(buf), i->memory_limit)); | |
4315 | prefix = " "; | |
4316 | } | |
4317 | printf(")"); | |
4318 | } | |
4319 | printf("\n"); | |
934277fe LP |
4320 | } |
4321 | ||
5ad096b3 LP |
4322 | if (i->cpu_usage_nsec != (uint64_t) -1) { |
4323 | char buf[FORMAT_TIMESPAN_MAX]; | |
4324 | printf(" CPU: %s\n", format_timespan(buf, sizeof(buf), i->cpu_usage_nsec / NSEC_PER_USEC, USEC_PER_MSEC)); | |
4325 | } | |
4326 | ||
51e22f88 | 4327 | if (i->control_group) { |
291d565a LP |
4328 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
4329 | static const char prefix[] = " "; | |
ab35fb1b LP |
4330 | unsigned c; |
4331 | ||
51e22f88 LP |
4332 | printf(" CGroup: %s\n", i->control_group); |
4333 | ||
291d565a LP |
4334 | c = columns(); |
4335 | if (c > sizeof(prefix) - 1) | |
4336 | c -= sizeof(prefix) - 1; | |
4337 | else | |
4338 | c = 0; | |
ab35fb1b | 4339 | |
291d565a LP |
4340 | r = unit_show_processes(bus, i->id, i->control_group, prefix, c, get_output_flags(), &error); |
4341 | if (r == -EBADR) { | |
b69d29ce LP |
4342 | unsigned k = 0; |
4343 | pid_t extra[2]; | |
4344 | ||
291d565a | 4345 | /* Fallback for older systemd versions where the GetUnitProcesses() call is not yet available */ |
ab35fb1b | 4346 | |
b69d29ce LP |
4347 | if (i->main_pid > 0) |
4348 | extra[k++] = i->main_pid; | |
4349 | ||
4350 | if (i->control_pid > 0) | |
4351 | extra[k++] = i->control_pid; | |
4352 | ||
0ff308c8 | 4353 | show_cgroup_and_extra(SYSTEMD_CGROUP_CONTROLLER, i->control_group, prefix, c, extra, k, get_output_flags()); |
291d565a LP |
4354 | } else if (r < 0) |
4355 | log_warning_errno(r, "Failed to dump process list, ignoring: %s", bus_error_message(&error, r)); | |
c59760ee | 4356 | } |
45fb0699 | 4357 | |
ece174c5 | 4358 | if (i->id && arg_transport == BUS_TRANSPORT_LOCAL) |
3c756001 LP |
4359 | show_journal_by_unit( |
4360 | stdout, | |
4361 | i->id, | |
4362 | arg_output, | |
4363 | 0, | |
4364 | i->inactive_exit_timestamp_monotonic, | |
4365 | arg_lines, | |
4366 | getuid(), | |
4367 | get_output_flags() | OUTPUT_BEGIN_NEWLINE, | |
4368 | SD_JOURNAL_LOCAL_ONLY, | |
4369 | arg_scope == UNIT_FILE_SYSTEM, | |
4370 | ellipsized); | |
86aa7ba4 | 4371 | |
45fb0699 | 4372 | if (i->need_daemon_reload) |
3f36991e | 4373 | warn_unit_file_changed(i->id); |
61cbdc4b LP |
4374 | } |
4375 | ||
b43f208f | 4376 | static void show_unit_help(UnitStatusInfo *i) { |
256425cc LP |
4377 | char **p; |
4378 | ||
4379 | assert(i); | |
4380 | ||
4381 | if (!i->documentation) { | |
4382 | log_info("Documentation for %s not known.", i->id); | |
4383 | return; | |
4384 | } | |
4385 | ||
78002a67 ZJS |
4386 | STRV_FOREACH(p, i->documentation) |
4387 | if (startswith(*p, "man:")) | |
4388 | show_man_page(*p + 4, false); | |
4389 | else | |
0315fe37 | 4390 | log_info("Can't show: %s", *p); |
256425cc LP |
4391 | } |
4392 | ||
4679a8c3 YW |
4393 | static int map_main_pid(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { |
4394 | UnitStatusInfo *i = userdata; | |
4395 | uint32_t u; | |
f459b602 | 4396 | int r; |
61cbdc4b | 4397 | |
4679a8c3 YW |
4398 | r = sd_bus_message_read(m, "u", &u); |
4399 | if (r < 0) | |
4400 | return r; | |
61cbdc4b | 4401 | |
4679a8c3 YW |
4402 | i->main_pid = (pid_t) u; |
4403 | i->running = u > 0; | |
b8131a87 | 4404 | |
4679a8c3 YW |
4405 | return 0; |
4406 | } | |
b8131a87 | 4407 | |
4679a8c3 YW |
4408 | static int map_load_error(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { |
4409 | const char *n, *message; | |
4410 | const char **p = userdata; | |
4411 | int r; | |
b8131a87 | 4412 | |
4679a8c3 YW |
4413 | r = sd_bus_message_read(m, "(ss)", &n, &message); |
4414 | if (r < 0) | |
4415 | return r; | |
b8131a87 | 4416 | |
4679a8c3 YW |
4417 | if (!isempty(message)) |
4418 | *p = message; | |
61cbdc4b | 4419 | |
4679a8c3 YW |
4420 | return 0; |
4421 | } | |
61cbdc4b | 4422 | |
4679a8c3 YW |
4423 | static int map_listen(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { |
4424 | const char *type, *path; | |
4425 | char ***p = userdata; | |
4426 | int r; | |
61cbdc4b | 4427 | |
4679a8c3 YW |
4428 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(ss)"); |
4429 | if (r < 0) | |
4430 | return r; | |
61cbdc4b | 4431 | |
4679a8c3 | 4432 | while ((r = sd_bus_message_read(m, "(ss)", &type, &path)) > 0) { |
61cbdc4b | 4433 | |
4679a8c3 | 4434 | r = strv_extend(p, type); |
f459b602 | 4435 | if (r < 0) |
4679a8c3 | 4436 | return r; |
61cbdc4b | 4437 | |
4679a8c3 | 4438 | r = strv_extend(p, path); |
f459b602 | 4439 | if (r < 0) |
4679a8c3 | 4440 | return r; |
61cbdc4b | 4441 | } |
4679a8c3 YW |
4442 | if (r < 0) |
4443 | return r; | |
582a507f | 4444 | |
4679a8c3 YW |
4445 | r = sd_bus_message_exit_container(m); |
4446 | if (r < 0) | |
4447 | return r; | |
49dbfa7b | 4448 | |
4679a8c3 YW |
4449 | return 0; |
4450 | } | |
52990c2e | 4451 | |
4679a8c3 YW |
4452 | static int map_conditions(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { |
4453 | UnitStatusInfo *i = userdata; | |
4454 | const char *cond, *param; | |
4455 | int trigger, negate; | |
4456 | int32_t state; | |
4457 | int r; | |
52990c2e | 4458 | |
4679a8c3 YW |
4459 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(sbbsi)"); |
4460 | if (r < 0) | |
4461 | return r; | |
f459b602 | 4462 | |
4679a8c3 YW |
4463 | while ((r = sd_bus_message_read(m, "(sbbsi)", &cond, &trigger, &negate, ¶m, &state)) > 0) { |
4464 | _cleanup_(unit_condition_freep) UnitCondition *c = NULL; | |
d5db7fe6 | 4465 | |
4679a8c3 YW |
4466 | c = new0(UnitCondition, 1); |
4467 | if (!c) | |
4468 | return -ENOMEM; | |
d5db7fe6 | 4469 | |
4679a8c3 YW |
4470 | c->name = strdup(cond); |
4471 | c->param = strdup(param); | |
4472 | if (!c->name || !c->param) | |
4473 | return -ENOMEM; | |
d5db7fe6 | 4474 | |
4679a8c3 YW |
4475 | c->trigger = trigger; |
4476 | c->negate = negate; | |
4477 | c->tristate = state; | |
d5db7fe6 | 4478 | |
4679a8c3 YW |
4479 | LIST_PREPEND(conditions, i->conditions, c); |
4480 | c = NULL; | |
4481 | } | |
4482 | if (r < 0) | |
4483 | return r; | |
d5db7fe6 | 4484 | |
4679a8c3 YW |
4485 | r = sd_bus_message_exit_container(m); |
4486 | if (r < 0) | |
4487 | return r; | |
59fccdc5 | 4488 | |
4679a8c3 YW |
4489 | return 0; |
4490 | } | |
59fccdc5 | 4491 | |
4679a8c3 YW |
4492 | static int map_asserts(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { |
4493 | UnitStatusInfo *i = userdata; | |
4494 | const char *cond, *param; | |
4495 | int trigger, negate; | |
4496 | int32_t state; | |
4497 | int r; | |
59fccdc5 | 4498 | |
4679a8c3 YW |
4499 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(sbbsi)"); |
4500 | if (r < 0) | |
4501 | return r; | |
59fccdc5 | 4502 | |
4679a8c3 YW |
4503 | while ((r = sd_bus_message_read(m, "(sbbsi)", &cond, &trigger, &negate, ¶m, &state)) > 0) { |
4504 | if (state < 0 && (!trigger || !i->failed_assert)) { | |
4505 | i->failed_assert = cond; | |
4506 | i->failed_assert_trigger = trigger; | |
4507 | i->failed_assert_negate = negate; | |
4508 | i->failed_assert_parameter = param; | |
4509 | } | |
4510 | } | |
4511 | if (r < 0) | |
4512 | return r; | |
f459b602 | 4513 | |
4679a8c3 YW |
4514 | r = sd_bus_message_exit_container(m); |
4515 | if (r < 0) | |
4516 | return r; | |
f459b602 | 4517 | |
4679a8c3 YW |
4518 | return 0; |
4519 | } | |
582a507f | 4520 | |
4679a8c3 YW |
4521 | static int map_exec(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { |
4522 | _cleanup_free_ ExecStatusInfo *info = NULL; | |
4523 | UnitStatusInfo *i = userdata; | |
4524 | int r; | |
9f39404c | 4525 | |
4679a8c3 YW |
4526 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(sasbttttuii)"); |
4527 | if (r < 0) | |
4528 | return r; | |
9f39404c | 4529 | |
4679a8c3 YW |
4530 | info = new0(ExecStatusInfo, 1); |
4531 | if (!info) | |
4532 | return -ENOMEM; | |
9f39404c | 4533 | |
4679a8c3 | 4534 | while ((r = exec_status_info_deserialize(m, info)) > 0) { |
9f39404c | 4535 | |
4679a8c3 YW |
4536 | info->name = strdup(member); |
4537 | if (!info->name) | |
4538 | return -ENOMEM; | |
9f39404c | 4539 | |
4679a8c3 | 4540 | LIST_PREPEND(exec, i->exec, info); |
f459b602 | 4541 | |
4679a8c3 YW |
4542 | info = new0(ExecStatusInfo, 1); |
4543 | if (!info) | |
4544 | return -ENOMEM; | |
9f39404c | 4545 | } |
4679a8c3 YW |
4546 | if (r < 0) |
4547 | return r; | |
f459b602 | 4548 | |
4679a8c3 | 4549 | r = sd_bus_message_exit_container(m); |
f459b602 | 4550 | if (r < 0) |
4679a8c3 | 4551 | return r; |
61cbdc4b LP |
4552 | |
4553 | return 0; | |
4554 | } | |
4555 | ||
4f9a9105 ZJS |
4556 | #define print_prop(name, fmt, ...) \ |
4557 | do { \ | |
4558 | if (arg_value) \ | |
4559 | printf(fmt "\n", __VA_ARGS__); \ | |
4560 | else \ | |
4561 | printf("%s=" fmt "\n", name, __VA_ARGS__); \ | |
508f63b4 | 4562 | } while (0) |
4f9a9105 | 4563 | |
07636114 YW |
4564 | static int print_property(const char *name, sd_bus_message *m, bool value, bool all) { |
4565 | char bus_type; | |
4566 | const char *contents; | |
f459b602 MAP |
4567 | int r; |
4568 | ||
48220598 | 4569 | assert(name); |
f459b602 | 4570 | assert(m); |
48220598 | 4571 | |
61cbdc4b LP |
4572 | /* This is a low-level property printer, see |
4573 | * print_status_info() for the nicer output */ | |
4574 | ||
07636114 YW |
4575 | r = sd_bus_message_peek_type(m, &bus_type, &contents); |
4576 | if (r < 0) | |
852c1b4d | 4577 | return r; |
48220598 | 4578 | |
07636114 | 4579 | switch (bus_type) { |
48220598 | 4580 | |
07636114 | 4581 | case SD_BUS_TYPE_STRUCT: |
48220598 | 4582 | |
07636114 | 4583 | if (contents[0] == SD_BUS_TYPE_UINT32 && streq(name, "Job")) { |
48220598 LP |
4584 | uint32_t u; |
4585 | ||
f459b602 MAP |
4586 | r = sd_bus_message_read(m, "(uo)", &u, NULL); |
4587 | if (r < 0) | |
4588 | return bus_log_parse_error(r); | |
48220598 | 4589 | |
f459b602 | 4590 | if (u > 0) |
4f9a9105 | 4591 | print_prop(name, "%"PRIu32, u); |
07636114 | 4592 | else if (all) |
4f9a9105 | 4593 | print_prop(name, "%s", ""); |
48220598 | 4594 | |
07636114 | 4595 | return 1; |
f459b602 | 4596 | |
07636114 | 4597 | } else if (contents[0] == SD_BUS_TYPE_STRING && streq(name, "Unit")) { |
48220598 LP |
4598 | const char *s; |
4599 | ||
f459b602 MAP |
4600 | r = sd_bus_message_read(m, "(so)", &s, NULL); |
4601 | if (r < 0) | |
4602 | return bus_log_parse_error(r); | |
48220598 | 4603 | |
07636114 | 4604 | if (all || !isempty(s)) |
4f9a9105 | 4605 | print_prop(name, "%s", s); |
48220598 | 4606 | |
07636114 | 4607 | return 1; |
f459b602 | 4608 | |
07636114 | 4609 | } else if (contents[0] == SD_BUS_TYPE_STRING && streq(name, "LoadError")) { |
9f39404c LP |
4610 | const char *a = NULL, *b = NULL; |
4611 | ||
f459b602 MAP |
4612 | r = sd_bus_message_read(m, "(ss)", &a, &b); |
4613 | if (r < 0) | |
4614 | return bus_log_parse_error(r); | |
9f39404c | 4615 | |
07636114 | 4616 | if (all || !isempty(a) || !isempty(b)) |
4f9a9105 | 4617 | print_prop(name, "%s \"%s\"", strempty(a), strempty(b)); |
f786e80d | 4618 | |
07636114 | 4619 | return 1; |
57183d11 LP |
4620 | } else if (streq_ptr(name, "SystemCallFilter")) { |
4621 | _cleanup_strv_free_ char **l = NULL; | |
4622 | int whitelist; | |
4623 | ||
4624 | r = sd_bus_message_enter_container(m, 'r', "bas"); | |
4625 | if (r < 0) | |
4626 | return bus_log_parse_error(r); | |
4627 | ||
4628 | r = sd_bus_message_read(m, "b", &whitelist); | |
4629 | if (r < 0) | |
4630 | return bus_log_parse_error(r); | |
4631 | ||
4632 | r = sd_bus_message_read_strv(m, &l); | |
4633 | if (r < 0) | |
4634 | return bus_log_parse_error(r); | |
4635 | ||
4636 | r = sd_bus_message_exit_container(m); | |
4637 | if (r < 0) | |
4638 | return bus_log_parse_error(r); | |
4639 | ||
07636114 | 4640 | if (all || whitelist || !strv_isempty(l)) { |
57183d11 LP |
4641 | bool first = true; |
4642 | char **i; | |
4643 | ||
07636114 | 4644 | if (!value) { |
4f9a9105 ZJS |
4645 | fputs(name, stdout); |
4646 | fputc('=', stdout); | |
4647 | } | |
57183d11 LP |
4648 | |
4649 | if (!whitelist) | |
4650 | fputc('~', stdout); | |
4651 | ||
4652 | STRV_FOREACH(i, l) { | |
4653 | if (first) | |
4654 | first = false; | |
4655 | else | |
4656 | fputc(' ', stdout); | |
4657 | ||
4658 | fputs(*i, stdout); | |
4659 | } | |
4660 | fputc('\n', stdout); | |
4661 | } | |
4662 | ||
07636114 | 4663 | return 1; |
48220598 LP |
4664 | } |
4665 | ||
4666 | break; | |
48220598 | 4667 | |
f459b602 | 4668 | case SD_BUS_TYPE_ARRAY: |
48220598 | 4669 | |
07636114 | 4670 | if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "EnvironmentFiles")) { |
f459b602 MAP |
4671 | const char *path; |
4672 | int ignore; | |
8c7be95e | 4673 | |
f459b602 MAP |
4674 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(sb)"); |
4675 | if (r < 0) | |
4676 | return bus_log_parse_error(r); | |
8c7be95e | 4677 | |
f459b602 | 4678 | while ((r = sd_bus_message_read(m, "(sb)", &path, &ignore)) > 0) |
009192bb | 4679 | print_prop(name, "%s (ignore_errors=%s)", path, yes_no(ignore)); |
8c7be95e | 4680 | |
f459b602 MAP |
4681 | if (r < 0) |
4682 | return bus_log_parse_error(r); | |
8c7be95e | 4683 | |
f459b602 MAP |
4684 | r = sd_bus_message_exit_container(m); |
4685 | if (r < 0) | |
4686 | return bus_log_parse_error(r); | |
8c7be95e | 4687 | |
07636114 | 4688 | return 1; |
8c7be95e | 4689 | |
07636114 | 4690 | } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "Paths")) { |
f459b602 | 4691 | const char *type, *path; |
67419600 | 4692 | |
f459b602 MAP |
4693 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(ss)"); |
4694 | if (r < 0) | |
4695 | return bus_log_parse_error(r); | |
ebf57b80 | 4696 | |
f459b602 | 4697 | while ((r = sd_bus_message_read(m, "(ss)", &type, &path)) > 0) |
c7366cf1 | 4698 | print_prop(name, "%s (%s)", path, type); |
f459b602 MAP |
4699 | if (r < 0) |
4700 | return bus_log_parse_error(r); | |
ebf57b80 | 4701 | |
f459b602 MAP |
4702 | r = sd_bus_message_exit_container(m); |
4703 | if (r < 0) | |
4704 | return bus_log_parse_error(r); | |
ebf57b80 | 4705 | |
07636114 | 4706 | return 1; |
582a507f | 4707 | |
07636114 | 4708 | } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "Listen")) { |
f459b602 | 4709 | const char *type, *path; |
67419600 | 4710 | |
f459b602 MAP |
4711 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(ss)"); |
4712 | if (r < 0) | |
4713 | return bus_log_parse_error(r); | |
67419600 | 4714 | |
f459b602 | 4715 | while ((r = sd_bus_message_read(m, "(ss)", &type, &path)) > 0) |
eabc13c7 | 4716 | print_prop(name, "%s (%s)", path, type); |
f459b602 MAP |
4717 | if (r < 0) |
4718 | return bus_log_parse_error(r); | |
67419600 | 4719 | |
f459b602 MAP |
4720 | r = sd_bus_message_exit_container(m); |
4721 | if (r < 0) | |
4722 | return bus_log_parse_error(r); | |
67419600 | 4723 | |
07636114 | 4724 | return 1; |
67419600 | 4725 | |
07636114 | 4726 | } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "TimersMonotonic")) { |
f459b602 | 4727 | const char *base; |
07636114 | 4728 | uint64_t v, next_elapse; |
707e5e52 | 4729 | |
f459b602 MAP |
4730 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(stt)"); |
4731 | if (r < 0) | |
4732 | return bus_log_parse_error(r); | |
552e4331 | 4733 | |
07636114 | 4734 | while ((r = sd_bus_message_read(m, "(stt)", &base, &v, &next_elapse)) > 0) { |
f459b602 | 4735 | char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX]; |
fe68089d | 4736 | |
fa6dee52 | 4737 | print_prop(name, "{ %s=%s ; next_elapse=%s }", base, |
07636114 | 4738 | format_timespan(timespan1, sizeof(timespan1), v, 0), |
4f9a9105 | 4739 | format_timespan(timespan2, sizeof(timespan2), next_elapse, 0)); |
fe68089d | 4740 | } |
f459b602 MAP |
4741 | if (r < 0) |
4742 | return bus_log_parse_error(r); | |
4743 | ||
4744 | r = sd_bus_message_exit_container(m); | |
4745 | if (r < 0) | |
4746 | return bus_log_parse_error(r); | |
fe68089d | 4747 | |
07636114 | 4748 | return 1; |
fe68089d | 4749 | |
07636114 | 4750 | } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "TimersCalendar")) { |
fa6dee52 YW |
4751 | const char *base, *spec; |
4752 | uint64_t next_elapse; | |
4753 | ||
4754 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(sst)"); | |
4755 | if (r < 0) | |
4756 | return bus_log_parse_error(r); | |
4757 | ||
4758 | while ((r = sd_bus_message_read(m, "(sst)", &base, &spec, &next_elapse)) > 0) { | |
4759 | char timestamp[FORMAT_TIMESTAMP_MAX]; | |
4760 | ||
4761 | print_prop(name, "{ %s=%s ; next_elapse=%s }", base, spec, | |
4762 | format_timestamp(timestamp, sizeof(timestamp), next_elapse)); | |
4763 | } | |
4764 | if (r < 0) | |
4765 | return bus_log_parse_error(r); | |
4766 | ||
4767 | r = sd_bus_message_exit_container(m); | |
4768 | if (r < 0) | |
4769 | return bus_log_parse_error(r); | |
4770 | ||
07636114 | 4771 | return 1; |
fa6dee52 | 4772 | |
07636114 | 4773 | } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && startswith(name, "Exec")) { |
f459b602 MAP |
4774 | ExecStatusInfo info = {}; |
4775 | ||
4776 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(sasbttttuii)"); | |
4777 | if (r < 0) | |
4778 | return bus_log_parse_error(r); | |
4779 | ||
4780 | while ((r = exec_status_info_deserialize(m, &info)) > 0) { | |
4781 | char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX]; | |
4782 | _cleanup_free_ char *tt; | |
4783 | ||
4784 | tt = strv_join(info.argv, " "); | |
4785 | ||
4f9a9105 ZJS |
4786 | print_prop(name, |
4787 | "{ path=%s ; argv[]=%s ; ignore_errors=%s ; start_time=[%s] ; stop_time=[%s] ; pid="PID_FMT" ; code=%s ; status=%i%s%s }", | |
4788 | strna(info.path), | |
4789 | strna(tt), | |
4790 | yes_no(info.ignore), | |
4791 | strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)), | |
4792 | strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)), | |
4793 | info.pid, | |
4794 | sigchld_code_to_string(info.code), | |
4795 | info.status, | |
4796 | info.code == CLD_EXITED ? "" : "/", | |
4797 | strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status))); | |
fe68089d | 4798 | |
582a507f LP |
4799 | free(info.path); |
4800 | strv_free(info.argv); | |
f459b602 | 4801 | zero(info); |
707e5e52 LP |
4802 | } |
4803 | ||
f459b602 MAP |
4804 | r = sd_bus_message_exit_container(m); |
4805 | if (r < 0) | |
4806 | return bus_log_parse_error(r); | |
4807 | ||
07636114 | 4808 | return 1; |
4ad49000 | 4809 | |
07636114 | 4810 | } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "DeviceAllow")) { |
f459b602 | 4811 | const char *path, *rwm; |
4ad49000 | 4812 | |
f459b602 MAP |
4813 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(ss)"); |
4814 | if (r < 0) | |
4815 | return bus_log_parse_error(r); | |
4ad49000 | 4816 | |
f459b602 | 4817 | while ((r = sd_bus_message_read(m, "(ss)", &path, &rwm)) > 0) |
4f9a9105 | 4818 | print_prop(name, "%s %s", strna(path), strna(rwm)); |
f459b602 MAP |
4819 | if (r < 0) |
4820 | return bus_log_parse_error(r); | |
4ad49000 | 4821 | |
f459b602 MAP |
4822 | r = sd_bus_message_exit_container(m); |
4823 | if (r < 0) | |
4824 | return bus_log_parse_error(r); | |
4ad49000 | 4825 | |
07636114 | 4826 | return 1; |
4ad49000 | 4827 | |
07636114 | 4828 | } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && |
72240b52 | 4829 | STR_IN_SET(name, "IODeviceWeight", "BlockIODeviceWeight")) { |
f459b602 MAP |
4830 | const char *path; |
4831 | uint64_t weight; | |
b8ab2dc6 | 4832 | |
f459b602 MAP |
4833 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(st)"); |
4834 | if (r < 0) | |
4835 | return bus_log_parse_error(r); | |
b8ab2dc6 | 4836 | |
f459b602 | 4837 | while ((r = sd_bus_message_read(m, "(st)", &path, &weight)) > 0) |
4f9a9105 | 4838 | print_prop(name, "%s %"PRIu64, strna(path), weight); |
f459b602 MAP |
4839 | if (r < 0) |
4840 | return bus_log_parse_error(r); | |
b8ab2dc6 | 4841 | |
f459b602 MAP |
4842 | r = sd_bus_message_exit_container(m); |
4843 | if (r < 0) | |
4844 | return bus_log_parse_error(r); | |
b8ab2dc6 | 4845 | |
07636114 | 4846 | return 1; |
b8ab2dc6 | 4847 | |
07636114 | 4848 | } else if (contents[0] == SD_BUS_TYPE_STRUCT_BEGIN && |
72240b52 ZJS |
4849 | (cgroup_io_limit_type_from_string(name) >= 0 || |
4850 | STR_IN_SET(name, "BlockIOReadBandwidth", "BlockIOWriteBandwidth"))) { | |
f459b602 MAP |
4851 | const char *path; |
4852 | uint64_t bandwidth; | |
4ad49000 | 4853 | |
f459b602 MAP |
4854 | r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(st)"); |
4855 | if (r < 0) | |
4856 | return bus_log_parse_error(r); | |
4ad49000 | 4857 | |
f459b602 | 4858 | while ((r = sd_bus_message_read(m, "(st)", &path, &bandwidth)) > 0) |
4f9a9105 | 4859 | print_prop(name, "%s %"PRIu64, strna(path), bandwidth); |
f459b602 MAP |
4860 | if (r < 0) |
4861 | return bus_log_parse_error(r); | |
4ad49000 | 4862 | |
f459b602 MAP |
4863 | r = sd_bus_message_exit_container(m); |
4864 | if (r < 0) | |
4865 | return bus_log_parse_error(r); | |
4ad49000 | 4866 | |
07636114 | 4867 | return 1; |
08f3be7a | 4868 | |
07636114 | 4869 | } else if (contents[0] == SD_BUS_TYPE_BYTE && streq(name, "StandardInputData")) { |
08f3be7a LP |
4870 | _cleanup_free_ char *h = NULL; |
4871 | const void *p; | |
4872 | size_t sz; | |
4873 | ssize_t n; | |
4874 | ||
4875 | r = sd_bus_message_read_array(m, 'y', &p, &sz); | |
4876 | if (r < 0) | |
4877 | return bus_log_parse_error(r); | |
4878 | ||
4879 | n = base64mem(p, sz, &h); | |
4880 | if (n < 0) | |
4881 | return log_oom(); | |
4882 | ||
4883 | print_prop(name, "%s", h); | |
4884 | ||
07636114 | 4885 | return 1; |
48220598 LP |
4886 | } |
4887 | ||
4888 | break; | |
4889 | } | |
4890 | ||
48220598 LP |
4891 | return 0; |
4892 | } | |
4893 | ||
8559e61d YW |
4894 | typedef enum SystemctlShowMode{ |
4895 | SYSTEMCTL_SHOW_PROPERTIES, | |
4896 | SYSTEMCTL_SHOW_STATUS, | |
4897 | SYSTEMCTL_SHOW_HELP, | |
4898 | _SYSTEMCTL_SHOW_MODE_MAX, | |
4899 | _SYSTEMCTL_SHOW_MODE_INVALID = -1, | |
4900 | } SystemctlShowMode; | |
4901 | ||
4902 | static const char* const systemctl_show_mode_table[] = { | |
4903 | [SYSTEMCTL_SHOW_PROPERTIES] = "show", | |
4904 | [SYSTEMCTL_SHOW_STATUS] = "status", | |
4905 | [SYSTEMCTL_SHOW_HELP] = "help", | |
4906 | }; | |
4907 | ||
4908 | DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(systemctl_show_mode, SystemctlShowMode); | |
4909 | ||
f459b602 | 4910 | static int show_one( |
f459b602 MAP |
4911 | sd_bus *bus, |
4912 | const char *path, | |
3dced37b | 4913 | const char *unit, |
8559e61d | 4914 | SystemctlShowMode show_mode, |
f459b602 MAP |
4915 | bool *new_line, |
4916 | bool *ellipsized) { | |
4917 | ||
3dced37b | 4918 | static const struct bus_properties_map property_map[] = { |
4679a8c3 YW |
4919 | { "LoadState", "s", NULL, offsetof(UnitStatusInfo, load_state) }, |
4920 | { "ActiveState", "s", NULL, offsetof(UnitStatusInfo, active_state) }, | |
4921 | { "Documentation", "as", NULL, offsetof(UnitStatusInfo, documentation) }, | |
4922 | {} | |
4923 | }, status_map[] = { | |
4924 | { "Id", "s", NULL, offsetof(UnitStatusInfo, id) }, | |
4925 | { "LoadState", "s", NULL, offsetof(UnitStatusInfo, load_state) }, | |
4926 | { "ActiveState", "s", NULL, offsetof(UnitStatusInfo, active_state) }, | |
4927 | { "SubState", "s", NULL, offsetof(UnitStatusInfo, sub_state) }, | |
4928 | { "UnitFileState", "s", NULL, offsetof(UnitStatusInfo, unit_file_state) }, | |
4929 | { "UnitFilePreset", "s", NULL, offsetof(UnitStatusInfo, unit_file_preset) }, | |
4930 | { "Description", "s", NULL, offsetof(UnitStatusInfo, description) }, | |
4931 | { "Following", "s", NULL, offsetof(UnitStatusInfo, following) }, | |
4932 | { "Documentation", "as", NULL, offsetof(UnitStatusInfo, documentation) }, | |
4933 | { "FragmentPath", "s", NULL, offsetof(UnitStatusInfo, fragment_path) }, | |
4934 | { "SourcePath", "s", NULL, offsetof(UnitStatusInfo, source_path) }, | |
4935 | { "ControlGroup", "s", NULL, offsetof(UnitStatusInfo, control_group) }, | |
4936 | { "DropInPaths", "as", NULL, offsetof(UnitStatusInfo, dropin_paths) }, | |
4937 | { "LoadError", "(ss)", map_load_error, offsetof(UnitStatusInfo, load_error) }, | |
4938 | { "Result", "s", NULL, offsetof(UnitStatusInfo, result) }, | |
4939 | { "InactiveExitTimestamp", "t", NULL, offsetof(UnitStatusInfo, inactive_exit_timestamp) }, | |
4940 | { "InactiveExitTimestampMonotonic", "t", NULL, offsetof(UnitStatusInfo, inactive_exit_timestamp_monotonic) }, | |
4941 | { "ActiveEnterTimestamp", "t", NULL, offsetof(UnitStatusInfo, active_enter_timestamp) }, | |
4942 | { "ActiveExitTimestamp", "t", NULL, offsetof(UnitStatusInfo, active_exit_timestamp) }, | |
4943 | { "InactiveEnterTimestamp", "t", NULL, offsetof(UnitStatusInfo, inactive_enter_timestamp) }, | |
4944 | { "NeedDaemonReload", "b", NULL, offsetof(UnitStatusInfo, need_daemon_reload) }, | |
4945 | { "Transient", "b", NULL, offsetof(UnitStatusInfo, transient) }, | |
4946 | { "ExecMainPID", "u", NULL, offsetof(UnitStatusInfo, main_pid) }, | |
4947 | { "MainPID", "u", map_main_pid, 0 }, | |
4948 | { "ControlPID", "u", NULL, offsetof(UnitStatusInfo, control_pid) }, | |
4949 | { "StatusText", "s", NULL, offsetof(UnitStatusInfo, status_text) }, | |
4950 | { "PIDFile", "s", NULL, offsetof(UnitStatusInfo, pid_file) }, | |
4951 | { "StatusErrno", "i", NULL, offsetof(UnitStatusInfo, status_errno) }, | |
4952 | { "ExecMainStartTimestamp", "t", NULL, offsetof(UnitStatusInfo, start_timestamp) }, | |
4953 | { "ExecMainExitTimestamp", "t", NULL, offsetof(UnitStatusInfo, exit_timestamp) }, | |
4954 | { "ExecMainCode", "i", NULL, offsetof(UnitStatusInfo, exit_code) }, | |
4955 | { "ExecMainStatus", "i", NULL, offsetof(UnitStatusInfo, exit_status) }, | |
4956 | { "ConditionTimestamp", "t", NULL, offsetof(UnitStatusInfo, condition_timestamp) }, | |
4957 | { "ConditionResult", "b", NULL, offsetof(UnitStatusInfo, condition_result) }, | |
4958 | { "Conditions", "a(sbbsi)", map_conditions, 0 }, | |
4959 | { "AssertTimestamp", "t", NULL, offsetof(UnitStatusInfo, assert_timestamp) }, | |
4960 | { "AssertResult", "b", NULL, offsetof(UnitStatusInfo, assert_result) }, | |
4961 | { "Asserts", "a(sbbsi)", map_asserts, 0 }, | |
4962 | { "NextElapseUSecRealtime", "t", NULL, offsetof(UnitStatusInfo, next_elapse_real) }, | |
4963 | { "NextElapseUSecMonotonic", "t", NULL, offsetof(UnitStatusInfo, next_elapse_monotonic) }, | |
4964 | { "NAccepted", "u", NULL, offsetof(UnitStatusInfo, n_accepted) }, | |
4965 | { "NConnections", "u", NULL, offsetof(UnitStatusInfo, n_connections) }, | |
4966 | { "Accept", "b", NULL, offsetof(UnitStatusInfo, accept) }, | |
4967 | { "Listen", "a(ss)", map_listen, offsetof(UnitStatusInfo, listen) }, | |
4968 | { "SysFSPath", "s", NULL, offsetof(UnitStatusInfo, sysfs_path) }, | |
4969 | { "Where", "s", NULL, offsetof(UnitStatusInfo, where) }, | |
4970 | { "What", "s", NULL, offsetof(UnitStatusInfo, what) }, | |
4971 | { "MemoryCurrent", "t", NULL, offsetof(UnitStatusInfo, memory_current) }, | |
4972 | { "MemoryLow", "t", NULL, offsetof(UnitStatusInfo, memory_low) }, | |
4973 | { "MemoryHigh", "t", NULL, offsetof(UnitStatusInfo, memory_high) }, | |
4974 | { "MemoryMax", "t", NULL, offsetof(UnitStatusInfo, memory_max) }, | |
4975 | { "MemorySwapMax", "t", NULL, offsetof(UnitStatusInfo, memory_swap_max) }, | |
4976 | { "MemoryLimit", "t", NULL, offsetof(UnitStatusInfo, memory_limit) }, | |
4977 | { "CPUUsageNSec", "t", NULL, offsetof(UnitStatusInfo, cpu_usage_nsec) }, | |
4978 | { "TasksCurrent", "t", NULL, offsetof(UnitStatusInfo, tasks_current) }, | |
4979 | { "TasksMax", "t", NULL, offsetof(UnitStatusInfo, tasks_max) }, | |
4980 | { "IPIngressBytes", "t", NULL, offsetof(UnitStatusInfo, ip_ingress_bytes) }, | |
4981 | { "IPEgressBytes", "t", NULL, offsetof(UnitStatusInfo, ip_egress_bytes) }, | |
4982 | { "ExecStartPre", "a(sasbttttuii)", map_exec, 0 }, | |
4983 | { "ExecStart", "a(sasbttttuii)", map_exec, 0 }, | |
4984 | { "ExecStartPost", "a(sasbttttuii)", map_exec, 0 }, | |
4985 | { "ExecReload", "a(sasbttttuii)", map_exec, 0 }, | |
4986 | { "ExecStopPre", "a(sasbttttuii)", map_exec, 0 }, | |
4987 | { "ExecStop", "a(sasbttttuii)", map_exec, 0 }, | |
4988 | { "ExecStopPost", "a(sasbttttuii)", map_exec, 0 }, | |
3dced37b LP |
4989 | {} |
4990 | }; | |
4991 | ||
4afd3348 LP |
4992 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
4993 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
3dced37b | 4994 | _cleanup_set_free_ Set *found_properties = NULL; |
a7335518 | 4995 | _cleanup_(unit_status_info_free) UnitStatusInfo info = { |
934277fe | 4996 | .memory_current = (uint64_t) -1, |
da4d897e TH |
4997 | .memory_high = CGROUP_LIMIT_MAX, |
4998 | .memory_max = CGROUP_LIMIT_MAX, | |
96e131ea | 4999 | .memory_swap_max = CGROUP_LIMIT_MAX, |
934277fe | 5000 | .memory_limit = (uint64_t) -1, |
5ad096b3 | 5001 | .cpu_usage_nsec = (uint64_t) -1, |
03a7b521 LP |
5002 | .tasks_current = (uint64_t) -1, |
5003 | .tasks_max = (uint64_t) -1, | |
0e97c93f DM |
5004 | .ip_ingress_bytes = (uint64_t) -1, |
5005 | .ip_egress_bytes = (uint64_t) -1, | |
934277fe | 5006 | }; |
4679a8c3 | 5007 | char **pp; |
f459b602 | 5008 | int r; |
48220598 | 5009 | |
48220598 | 5010 | assert(path); |
61cbdc4b | 5011 | assert(new_line); |
48220598 | 5012 | |
e3e0314b ZJS |
5013 | log_debug("Showing one %s", path); |
5014 | ||
4679a8c3 | 5015 | r = bus_map_all_properties( |
f22f08cd SP |
5016 | bus, |
5017 | "org.freedesktop.systemd1", | |
5018 | path, | |
4679a8c3 | 5019 | show_mode == SYSTEMCTL_SHOW_STATUS ? status_map : property_map, |
a7e4861c | 5020 | BUS_MAP_BOOLEAN_AS_BOOL, |
f459b602 | 5021 | &error, |
f22f08cd | 5022 | &reply, |
4679a8c3 | 5023 | &info); |
4c3e8e39 LP |
5024 | if (r < 0) |
5025 | return log_error_errno(r, "Failed to get properties: %s", bus_error_message(&error, r)); | |
48220598 | 5026 | |
4679a8c3 YW |
5027 | if (unit && streq_ptr(info.load_state, "not-found") && streq_ptr(info.active_state, "inactive")) { |
5028 | log_full(show_mode == SYSTEMCTL_SHOW_STATUS ? LOG_ERR : LOG_DEBUG, | |
5029 | "Unit %s could not be found.", unit); | |
e33a06a1 | 5030 | |
4679a8c3 YW |
5031 | if (show_mode == SYSTEMCTL_SHOW_STATUS) |
5032 | return EXIT_PROGRAM_OR_SERVICES_STATUS_UNKNOWN; | |
5033 | else if (show_mode == SYSTEMCTL_SHOW_HELP) | |
5034 | return -ENOENT; | |
5035 | } | |
e33a06a1 | 5036 | |
4679a8c3 YW |
5037 | if (*new_line) |
5038 | printf("\n"); | |
3dced37b | 5039 | |
4679a8c3 YW |
5040 | *new_line = true; |
5041 | ||
5042 | if (show_mode == SYSTEMCTL_SHOW_STATUS) { | |
5043 | print_status_info(bus, &info, ellipsized); | |
5044 | ||
5045 | if (info.active_state && !STR_IN_SET(info.active_state, "active", "reloading")) | |
5046 | return EXIT_PROGRAM_NOT_RUNNING; | |
5047 | ||
5048 | return EXIT_PROGRAM_RUNNING_OR_SERVICE_OK; | |
5049 | ||
5050 | } else if (show_mode == SYSTEMCTL_SHOW_HELP) { | |
5051 | show_unit_help(&info); | |
5052 | return 0; | |
3dced37b | 5053 | } |
e33a06a1 | 5054 | |
4679a8c3 YW |
5055 | r = sd_bus_message_rewind(reply, true); |
5056 | if (r < 0) | |
5057 | return log_error_errno(r, "Failed to rewind: %s", bus_error_message(&error, r)); | |
5058 | ||
07636114 | 5059 | r = bus_message_print_all_properties(reply, print_property, arg_properties, arg_value, arg_all, &found_properties); |
f459b602 MAP |
5060 | if (r < 0) |
5061 | return bus_log_parse_error(r); | |
48220598 | 5062 | |
4679a8c3 YW |
5063 | STRV_FOREACH(pp, arg_properties) |
5064 | if (!set_contains(found_properties, *pp)) | |
5065 | log_debug("Property %s does not exist.", *pp); | |
3dced37b | 5066 | |
4679a8c3 | 5067 | return 0; |
48220598 LP |
5068 | } |
5069 | ||
f74294c1 | 5070 | static int get_unit_dbus_path_by_pid( |
f459b602 MAP |
5071 | sd_bus *bus, |
5072 | uint32_t pid, | |
f74294c1 | 5073 | char **unit) { |
f459b602 | 5074 | |
4afd3348 LP |
5075 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
5076 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
373d32c9 | 5077 | char *u; |
a223b325 MS |
5078 | int r; |
5079 | ||
f459b602 | 5080 | r = sd_bus_call_method( |
f22f08cd SP |
5081 | bus, |
5082 | "org.freedesktop.systemd1", | |
5083 | "/org/freedesktop/systemd1", | |
5084 | "org.freedesktop.systemd1.Manager", | |
5085 | "GetUnitByPID", | |
f459b602 | 5086 | &error, |
f22f08cd | 5087 | &reply, |
f459b602 | 5088 | "u", pid); |
691395d8 LP |
5089 | if (r < 0) |
5090 | return log_error_errno(r, "Failed to get unit for PID %"PRIu32": %s", pid, bus_error_message(&error, r)); | |
a223b325 | 5091 | |
373d32c9 | 5092 | r = sd_bus_message_read(reply, "o", &u); |
f459b602 MAP |
5093 | if (r < 0) |
5094 | return bus_log_parse_error(r); | |
5095 | ||
373d32c9 LP |
5096 | u = strdup(u); |
5097 | if (!u) | |
5098 | return log_oom(); | |
5099 | ||
5100 | *unit = u; | |
f74294c1 | 5101 | return 0; |
a223b325 MS |
5102 | } |
5103 | ||
f459b602 | 5104 | static int show_all( |
f459b602 | 5105 | sd_bus *bus, |
f459b602 MAP |
5106 | bool *new_line, |
5107 | bool *ellipsized) { | |
5108 | ||
4afd3348 | 5109 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
f459b602 MAP |
5110 | _cleanup_free_ UnitInfo *unit_infos = NULL; |
5111 | const UnitInfo *u; | |
5112 | unsigned c; | |
5bb75bc7 | 5113 | int r, ret = 0; |
265a7a2a | 5114 | |
1238ee09 | 5115 | r = get_unit_list(bus, NULL, NULL, &unit_infos, 0, &reply); |
265a7a2a ZJS |
5116 | if (r < 0) |
5117 | return r; | |
5118 | ||
ee5324aa | 5119 | (void) pager_open(arg_no_pager, false); |
dbed408b | 5120 | |
f459b602 MAP |
5121 | c = (unsigned) r; |
5122 | ||
5123 | qsort_safe(unit_infos, c, sizeof(UnitInfo), compare_unit_info); | |
991f2a39 | 5124 | |
265a7a2a | 5125 | for (u = unit_infos; u < unit_infos + c; u++) { |
7fd1b19b | 5126 | _cleanup_free_ char *p = NULL; |
265a7a2a | 5127 | |
265a7a2a ZJS |
5128 | p = unit_dbus_path_from_name(u->id); |
5129 | if (!p) | |
5130 | return log_oom(); | |
5131 | ||
8559e61d | 5132 | r = show_one(bus, p, u->id, SYSTEMCTL_SHOW_STATUS, new_line, ellipsized); |
3df538da | 5133 | if (r < 0) |
265a7a2a | 5134 | return r; |
5bb75bc7 ZJS |
5135 | else if (r > 0 && ret == 0) |
5136 | ret = r; | |
265a7a2a ZJS |
5137 | } |
5138 | ||
5bb75bc7 | 5139 | return ret; |
265a7a2a ZJS |
5140 | } |
5141 | ||
8fcf784d LP |
5142 | static int show_system_status(sd_bus *bus) { |
5143 | char since1[FORMAT_TIMESTAMP_RELATIVE_MAX], since2[FORMAT_TIMESTAMP_MAX]; | |
f9e0eefc | 5144 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
e7e55dbd | 5145 | _cleanup_(machine_info_clear) struct machine_info mi = {}; |
f9e0eefc | 5146 | _cleanup_free_ char *hn = NULL; |
8fcf784d LP |
5147 | const char *on, *off; |
5148 | int r; | |
5149 | ||
5150 | hn = gethostname_malloc(); | |
5151 | if (!hn) | |
5152 | return log_oom(); | |
5153 | ||
a7e4861c YW |
5154 | r = bus_map_all_properties( |
5155 | bus, | |
5156 | "org.freedesktop.systemd1", | |
5157 | "/org/freedesktop/systemd1", | |
5158 | machine_info_property_map, | |
5159 | BUS_MAP_STRDUP, | |
5160 | &error, | |
5161 | NULL, | |
5162 | &mi); | |
f647962d | 5163 | if (r < 0) |
f9e0eefc | 5164 | return log_error_errno(r, "Failed to read server status: %s", bus_error_message(&error, r)); |
8fcf784d | 5165 | |
8fcf784d LP |
5166 | if (streq_ptr(mi.state, "degraded")) { |
5167 | on = ansi_highlight_red(); | |
1fc464f6 | 5168 | off = ansi_normal(); |
92f34a9c JR |
5169 | } else if (streq_ptr(mi.state, "running")) { |
5170 | on = ansi_highlight_green(); | |
5171 | off = ansi_normal(); | |
5172 | } else { | |
8fcf784d | 5173 | on = ansi_highlight_yellow(); |
1fc464f6 | 5174 | off = ansi_normal(); |
92f34a9c | 5175 | } |
8fcf784d | 5176 | |
323b7dc9 | 5177 | printf("%s%s%s %s\n", on, special_glyph(BLACK_CIRCLE), off, arg_host ? arg_host : hn); |
b0d14c69 | 5178 | |
8fcf784d LP |
5179 | printf(" State: %s%s%s\n", |
5180 | on, strna(mi.state), off); | |
5181 | ||
c6ba5b80 MP |
5182 | printf(" Jobs: %" PRIu32 " queued\n", mi.n_jobs); |
5183 | printf(" Failed: %" PRIu32 " units\n", mi.n_failed_units); | |
8fcf784d LP |
5184 | |
5185 | printf(" Since: %s; %s\n", | |
5186 | format_timestamp(since2, sizeof(since2), mi.timestamp), | |
5187 | format_timestamp_relative(since1, sizeof(since1), mi.timestamp)); | |
5188 | ||
5189 | printf(" CGroup: %s\n", mi.control_group ?: "/"); | |
4c315c2c IS |
5190 | if (IN_SET(arg_transport, |
5191 | BUS_TRANSPORT_LOCAL, | |
5192 | BUS_TRANSPORT_MACHINE)) { | |
8fcf784d LP |
5193 | static const char prefix[] = " "; |
5194 | unsigned c; | |
5195 | ||
5196 | c = columns(); | |
5197 | if (c > sizeof(prefix) - 1) | |
5198 | c -= sizeof(prefix) - 1; | |
5199 | else | |
5200 | c = 0; | |
5201 | ||
0ff308c8 | 5202 | show_cgroup(SYSTEMD_CGROUP_CONTROLLER, strempty(mi.control_group), prefix, c, get_output_flags()); |
8fcf784d LP |
5203 | } |
5204 | ||
8fcf784d LP |
5205 | return 0; |
5206 | } | |
5207 | ||
e449de87 | 5208 | static int show(int argc, char *argv[], void *userdata) { |
8559e61d YW |
5209 | bool new_line = false, ellipsized = false; |
5210 | SystemctlShowMode show_mode; | |
e3e0314b | 5211 | int r, ret = 0; |
4fbd7192 | 5212 | sd_bus *bus; |
48220598 | 5213 | |
e449de87 | 5214 | assert(argv); |
48220598 | 5215 | |
8559e61d YW |
5216 | show_mode = systemctl_show_mode_from_string(argv[0]); |
5217 | if (show_mode < 0) { | |
5218 | log_error("Invalid argument."); | |
5219 | return -EINVAL; | |
5220 | } | |
e449de87 | 5221 | |
8559e61d | 5222 | if (show_mode == SYSTEMCTL_SHOW_HELP && argc <= 1) { |
e449de87 LP |
5223 | log_error("This command expects one or more unit names. Did you mean --help?"); |
5224 | return -EINVAL; | |
5225 | } | |
61cbdc4b | 5226 | |
d2ad7e1b ZJS |
5227 | r = acquire_bus(BUS_MANAGER, &bus); |
5228 | if (r < 0) | |
5229 | return r; | |
5230 | ||
ee5324aa | 5231 | (void) pager_open(arg_no_pager, false); |
ec14911e | 5232 | |
8559e61d | 5233 | if (show_mode == SYSTEMCTL_SHOW_STATUS) |
40acc203 ZJS |
5234 | /* Increase max number of open files to 16K if we can, we |
5235 | * might needs this when browsing journal files, which might | |
5236 | * be split up into many files. */ | |
5237 | setrlimit_closest(RLIMIT_NOFILE, &RLIMIT_MAKE_CONST(16384)); | |
5238 | ||
4fbd7192 | 5239 | /* If no argument is specified inspect the manager itself */ |
8559e61d YW |
5240 | if (show_mode == SYSTEMCTL_SHOW_PROPERTIES && argc <= 1) |
5241 | return show_one(bus, "/org/freedesktop/systemd1", NULL, show_mode, &new_line, &ellipsized); | |
48220598 | 5242 | |
8559e61d | 5243 | if (show_mode == SYSTEMCTL_SHOW_STATUS && argc <= 1) { |
8fcf784d | 5244 | |
8fcf784d LP |
5245 | show_system_status(bus); |
5246 | new_line = true; | |
5247 | ||
5248 | if (arg_all) | |
8559e61d | 5249 | ret = show_all(bus, &new_line, &ellipsized); |
8fcf784d | 5250 | } else { |
e3e0314b ZJS |
5251 | _cleanup_free_ char **patterns = NULL; |
5252 | char **name; | |
5253 | ||
e449de87 | 5254 | STRV_FOREACH(name, strv_skip(argv, 1)) { |
3dced37b | 5255 | _cleanup_free_ char *path = NULL, *unit = NULL; |
94e0bd7d | 5256 | uint32_t id; |
48220598 | 5257 | |
94e0bd7d | 5258 | if (safe_atou32(*name, &id) < 0) { |
e3e0314b | 5259 | if (strv_push(&patterns, *name) < 0) |
94e0bd7d | 5260 | return log_oom(); |
48220598 | 5261 | |
e3e0314b | 5262 | continue; |
8559e61d | 5263 | } else if (show_mode == SYSTEMCTL_SHOW_PROPERTIES) { |
94e0bd7d | 5264 | /* Interpret as job id */ |
3dced37b | 5265 | if (asprintf(&path, "/org/freedesktop/systemd1/job/%u", id) < 0) |
94e0bd7d | 5266 | return log_oom(); |
48220598 | 5267 | |
94e0bd7d ZJS |
5268 | } else { |
5269 | /* Interpret as PID */ | |
3dced37b | 5270 | r = get_unit_dbus_path_by_pid(bus, id, &path); |
373d32c9 | 5271 | if (r < 0) { |
94e0bd7d | 5272 | ret = r; |
373d32c9 LP |
5273 | continue; |
5274 | } | |
3dced37b LP |
5275 | |
5276 | r = unit_name_from_dbus_path(path, &unit); | |
5277 | if (r < 0) | |
5278 | return log_oom(); | |
94e0bd7d | 5279 | } |
f74294c1 | 5280 | |
8559e61d | 5281 | r = show_one(bus, path, unit, show_mode, &new_line, &ellipsized); |
5bb75bc7 ZJS |
5282 | if (r < 0) |
5283 | return r; | |
5284 | else if (r > 0 && ret == 0) | |
5285 | ret = r; | |
48220598 | 5286 | } |
94e0bd7d | 5287 | |
e3e0314b ZJS |
5288 | if (!strv_isempty(patterns)) { |
5289 | _cleanup_strv_free_ char **names = NULL; | |
5290 | ||
5291 | r = expand_names(bus, patterns, NULL, &names); | |
5292 | if (r < 0) | |
691395d8 | 5293 | return log_error_errno(r, "Failed to expand names: %m"); |
e3e0314b ZJS |
5294 | |
5295 | STRV_FOREACH(name, names) { | |
3dced37b | 5296 | _cleanup_free_ char *path; |
e3e0314b | 5297 | |
3dced37b LP |
5298 | path = unit_dbus_path_from_name(*name); |
5299 | if (!path) | |
e3e0314b ZJS |
5300 | return log_oom(); |
5301 | ||
8559e61d | 5302 | r = show_one(bus, path, *name, show_mode, &new_line, &ellipsized); |
5bb75bc7 ZJS |
5303 | if (r < 0) |
5304 | return r; | |
3dced37b | 5305 | if (r > 0 && ret == 0) |
5bb75bc7 | 5306 | ret = r; |
e3e0314b ZJS |
5307 | } |
5308 | } | |
5309 | } | |
5310 | ||
94e0bd7d ZJS |
5311 | if (ellipsized && !arg_quiet) |
5312 | printf("Hint: Some lines were ellipsized, use -l to show in full.\n"); | |
48220598 | 5313 | |
22f4096c | 5314 | return ret; |
0183528f LP |
5315 | } |
5316 | ||
e449de87 | 5317 | static int cat(int argc, char *argv[], void *userdata) { |
8e766630 | 5318 | _cleanup_(lookup_paths_free) LookupPaths lp = {}; |
15ef1144 LP |
5319 | _cleanup_strv_free_ char **names = NULL; |
5320 | char **name; | |
4fbd7192 LP |
5321 | sd_bus *bus; |
5322 | bool first = true; | |
25586912 | 5323 | int r; |
15ef1144 | 5324 | |
3e7eed84 | 5325 | if (arg_transport != BUS_TRANSPORT_LOCAL) { |
4fbd7192 | 5326 | log_error("Cannot remotely cat units."); |
3e495a66 ZJS |
5327 | return -EINVAL; |
5328 | } | |
5329 | ||
4943d143 | 5330 | r = lookup_paths_init(&lp, arg_scope, 0, arg_root); |
ad2a0358 | 5331 | if (r < 0) |
c51932be | 5332 | return log_error_errno(r, "Failed to determine unit paths: %m"); |
ad2a0358 | 5333 | |
4fbd7192 | 5334 | r = acquire_bus(BUS_MANAGER, &bus); |
15ef1144 | 5335 | if (r < 0) |
4fbd7192 | 5336 | return r; |
15ef1144 | 5337 | |
e449de87 | 5338 | r = expand_names(bus, strv_skip(argv, 1), NULL, &names); |
4fbd7192 LP |
5339 | if (r < 0) |
5340 | return log_error_errno(r, "Failed to expand names: %m"); | |
ad2a0358 | 5341 | |
ee5324aa | 5342 | (void) pager_open(arg_no_pager, false); |
15ef1144 LP |
5343 | |
5344 | STRV_FOREACH(name, names) { | |
ad2a0358 | 5345 | _cleanup_free_ char *fragment_path = NULL; |
15ef1144 | 5346 | _cleanup_strv_free_ char **dropin_paths = NULL; |
15ef1144 | 5347 | |
4fbd7192 | 5348 | r = unit_find_paths(bus, *name, &lp, &fragment_path, &dropin_paths); |
ad2a0358 ZJS |
5349 | if (r < 0) |
5350 | return r; | |
b5e6a600 IS |
5351 | else if (r == 0) |
5352 | return -ENOENT; | |
15ef1144 LP |
5353 | |
5354 | if (first) | |
5355 | first = false; | |
5356 | else | |
5357 | puts(""); | |
5358 | ||
5b9635d1 | 5359 | if (need_daemon_reload(bus, *name) > 0) /* ignore errors (<0), this is informational output */ |
e100155d LW |
5360 | fprintf(stderr, |
5361 | "%s# Warning: %s changed on disk, the version systemd has loaded is outdated.\n" | |
5362 | "%s# This output shows the current version of the unit's original fragment and drop-in files.\n" | |
5363 | "%s# If fragments or drop-ins were added or removed, they are not properly reflected in this output.\n" | |
5364 | "%s# Run 'systemctl%s daemon-reload' to reload units.%s\n", | |
5365 | ansi_highlight_red(), | |
5366 | *name, | |
5367 | ansi_highlight_red(), | |
5368 | ansi_highlight_red(), | |
5369 | ansi_highlight_red(), | |
5370 | arg_scope == UNIT_FILE_SYSTEM ? "" : " --user", | |
5371 | ansi_normal()); | |
5372 | ||
854a42fb | 5373 | r = cat_files(fragment_path, dropin_paths, 0); |
81f5e513 ZJS |
5374 | if (r < 0) |
5375 | return r; | |
15ef1144 LP |
5376 | } |
5377 | ||
25586912 | 5378 | return 0; |
15ef1144 LP |
5379 | } |
5380 | ||
e449de87 | 5381 | static int set_property(int argc, char *argv[], void *userdata) { |
4afd3348 LP |
5382 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
5383 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
68372da6 | 5384 | _cleanup_free_ char *n = NULL; |
89ada3ba | 5385 | UnitType t; |
4fbd7192 | 5386 | sd_bus *bus; |
8e2af478 LP |
5387 | int r; |
5388 | ||
4fbd7192 LP |
5389 | r = acquire_bus(BUS_MANAGER, &bus); |
5390 | if (r < 0) | |
5391 | return r; | |
5392 | ||
8a4b13c5 | 5393 | polkit_agent_open_maybe(); |
d2ad7e1b | 5394 | |
f459b602 MAP |
5395 | r = sd_bus_message_new_method_call( |
5396 | bus, | |
151b9b96 | 5397 | &m, |
8e2af478 LP |
5398 | "org.freedesktop.systemd1", |
5399 | "/org/freedesktop/systemd1", | |
5400 | "org.freedesktop.systemd1.Manager", | |
151b9b96 | 5401 | "SetUnitProperties"); |
f459b602 MAP |
5402 | if (r < 0) |
5403 | return bus_log_create_error(r); | |
8e2af478 | 5404 | |
37cbc1d5 | 5405 | r = unit_name_mangle(argv[1], arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN, &n); |
7410616c LP |
5406 | if (r < 0) |
5407 | return log_error_errno(r, "Failed to mangle unit name: %m"); | |
68372da6 | 5408 | |
89ada3ba YW |
5409 | t = unit_name_to_type(n); |
5410 | if (t < 0) { | |
5411 | log_error("Invalid unit type: %s", n); | |
5412 | return -EINVAL; | |
5413 | } | |
5414 | ||
f459b602 MAP |
5415 | r = sd_bus_message_append(m, "sb", n, arg_runtime); |
5416 | if (r < 0) | |
5417 | return bus_log_create_error(r); | |
8e2af478 | 5418 | |
f459b602 MAP |
5419 | r = sd_bus_message_open_container(m, SD_BUS_TYPE_ARRAY, "(sv)"); |
5420 | if (r < 0) | |
5421 | return bus_log_create_error(r); | |
8e2af478 | 5422 | |
89ada3ba | 5423 | r = bus_append_unit_property_assignment_many(m, t, strv_skip(argv, 2)); |
8673cf13 LP |
5424 | if (r < 0) |
5425 | return r; | |
8e2af478 | 5426 | |
f459b602 MAP |
5427 | r = sd_bus_message_close_container(m); |
5428 | if (r < 0) | |
5429 | return bus_log_create_error(r); | |
8e2af478 | 5430 | |
c49b30a2 | 5431 | r = sd_bus_call(bus, m, 0, &error, NULL); |
691395d8 LP |
5432 | if (r < 0) |
5433 | return log_error_errno(r, "Failed to set unit properties on %s: %s", n, bus_error_message(&error, r)); | |
8e2af478 LP |
5434 | |
5435 | return 0; | |
5436 | } | |
5437 | ||
e449de87 | 5438 | static int daemon_reload(int argc, char *argv[], void *userdata) { |
4afd3348 | 5439 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
2853b60a | 5440 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
7e4249b9 | 5441 | const char *method; |
4fbd7192 | 5442 | sd_bus *bus; |
f459b602 | 5443 | int r; |
7e4249b9 | 5444 | |
4fbd7192 LP |
5445 | r = acquire_bus(BUS_MANAGER, &bus); |
5446 | if (r < 0) | |
5447 | return r; | |
5448 | ||
8a4b13c5 | 5449 | polkit_agent_open_maybe(); |
d2ad7e1b | 5450 | |
2853b60a LP |
5451 | switch (arg_action) { |
5452 | ||
5453 | case ACTION_RELOAD: | |
e4b61340 | 5454 | method = "Reload"; |
2853b60a LP |
5455 | break; |
5456 | ||
5457 | case ACTION_REEXEC: | |
e4b61340 | 5458 | method = "Reexecute"; |
2853b60a LP |
5459 | break; |
5460 | ||
5461 | case ACTION_SYSTEMCTL: | |
5462 | method = streq(argv[0], "daemon-reexec") ? "Reexecute" : | |
5463 | /* "daemon-reload" */ "Reload"; | |
5464 | break; | |
e4b61340 | 5465 | |
2853b60a LP |
5466 | default: |
5467 | assert_not_reached("Unexpected action"); | |
e4b61340 | 5468 | } |
7e4249b9 | 5469 | |
2853b60a LP |
5470 | r = sd_bus_message_new_method_call( |
5471 | bus, | |
5472 | &m, | |
5473 | "org.freedesktop.systemd1", | |
5474 | "/org/freedesktop/systemd1", | |
5475 | "org.freedesktop.systemd1.Manager", | |
5476 | method); | |
5477 | if (r < 0) | |
5478 | return bus_log_create_error(r); | |
5479 | ||
5480 | /* Note we use an extra-long timeout here. This is because a reload or reexec means generators are rerun which | |
5481 | * are timed out after DEFAULT_TIMEOUT_USEC. Let's use twice that time here, so that the generators can have | |
5482 | * their timeout, and for everything else there's the same time budget in place. */ | |
5483 | ||
5484 | r = sd_bus_call(bus, m, DEFAULT_TIMEOUT_USEC * 2, &error, NULL); | |
5485 | ||
5486 | /* On reexecution, we expect a disconnect, not a reply */ | |
5487 | if (IN_SET(r, -ETIMEDOUT, -ECONNRESET) && streq(method, "Reexecute")) | |
5488 | r = 0; | |
5489 | ||
5490 | if (r < 0 && arg_action == ACTION_SYSTEMCTL) | |
5491 | return log_error_errno(r, "Failed to reload daemon: %s", bus_error_message(&error, r)); | |
5492 | ||
5493 | /* Note that for the legacy commands (i.e. those with action != ACTION_SYSTEMCTL) we support fallbacks to the | |
5494 | * old ways of doing things, hence don't log any error in that case here. */ | |
5495 | ||
5496 | return r < 0 ? r : 0; | |
5497 | } | |
5498 | ||
5499 | static int trivial_method(int argc, char *argv[], void *userdata) { | |
5500 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
5501 | const char *method; | |
5502 | sd_bus *bus; | |
5503 | int r; | |
5504 | ||
1ae17672 ZJS |
5505 | if (arg_dry_run) |
5506 | return 0; | |
5507 | ||
2853b60a LP |
5508 | r = acquire_bus(BUS_MANAGER, &bus); |
5509 | if (r < 0) | |
5510 | return r; | |
5511 | ||
8a4b13c5 | 5512 | polkit_agent_open_maybe(); |
d2ad7e1b | 5513 | |
2853b60a LP |
5514 | method = |
5515 | streq(argv[0], "clear-jobs") || | |
5516 | streq(argv[0], "cancel") ? "ClearJobs" : | |
5517 | streq(argv[0], "reset-failed") ? "ResetFailed" : | |
5518 | streq(argv[0], "halt") ? "Halt" : | |
5519 | streq(argv[0], "reboot") ? "Reboot" : | |
5520 | streq(argv[0], "kexec") ? "KExec" : | |
5521 | streq(argv[0], "exit") ? "Exit" : | |
5522 | /* poweroff */ "PowerOff"; | |
5523 | ||
6e646d22 | 5524 | r = sd_bus_call_method( |
f22f08cd SP |
5525 | bus, |
5526 | "org.freedesktop.systemd1", | |
5527 | "/org/freedesktop/systemd1", | |
5528 | "org.freedesktop.systemd1.Manager", | |
6e646d22 LP |
5529 | method, |
5530 | &error, | |
5531 | NULL, | |
5532 | NULL); | |
2853b60a LP |
5533 | if (r < 0 && arg_action == ACTION_SYSTEMCTL) |
5534 | return log_error_errno(r, "Failed to execute operation: %s", bus_error_message(&error, r)); | |
5535 | ||
5536 | /* Note that for the legacy commands (i.e. those with action != ACTION_SYSTEMCTL) we support fallbacks to the | |
5537 | * old ways of doing things, hence don't log any error in that case here. */ | |
7e4249b9 | 5538 | |
0a9776c2 | 5539 | return r < 0 ? r : 0; |
7e4249b9 LP |
5540 | } |
5541 | ||
e449de87 | 5542 | static int reset_failed(int argc, char *argv[], void *userdata) { |
e3e0314b | 5543 | _cleanup_strv_free_ char **names = NULL; |
4fbd7192 | 5544 | sd_bus *bus; |
f84190d8 | 5545 | char **name; |
e3e0314b | 5546 | int r, q; |
5632e374 | 5547 | |
e449de87 | 5548 | if (argc <= 1) |
2853b60a | 5549 | return trivial_method(argc, argv, userdata); |
5632e374 | 5550 | |
4fbd7192 LP |
5551 | r = acquire_bus(BUS_MANAGER, &bus); |
5552 | if (r < 0) | |
5553 | return r; | |
5554 | ||
8a4b13c5 | 5555 | polkit_agent_open_maybe(); |
d2ad7e1b | 5556 | |
e449de87 | 5557 | r = expand_names(bus, strv_skip(argv, 1), NULL, &names); |
e3e0314b | 5558 | if (r < 0) |
691395d8 | 5559 | return log_error_errno(r, "Failed to expand names: %m"); |
f84190d8 | 5560 | |
e3e0314b | 5561 | STRV_FOREACH(name, names) { |
4afd3348 | 5562 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
342641fb | 5563 | |
6e646d22 | 5564 | q = sd_bus_call_method( |
f22f08cd | 5565 | bus, |
b0193f1c LP |
5566 | "org.freedesktop.systemd1", |
5567 | "/org/freedesktop/systemd1", | |
5568 | "org.freedesktop.systemd1.Manager", | |
6e646d22 LP |
5569 | "ResetFailedUnit", |
5570 | &error, | |
5571 | NULL, | |
5572 | "s", *name); | |
e3e0314b | 5573 | if (q < 0) { |
691395d8 | 5574 | log_error_errno(q, "Failed to reset failed state of unit %s: %s", *name, bus_error_message(&error, q)); |
e3e0314b ZJS |
5575 | if (r == 0) |
5576 | r = q; | |
f459b602 | 5577 | } |
5632e374 LP |
5578 | } |
5579 | ||
e3e0314b | 5580 | return r; |
5632e374 LP |
5581 | } |
5582 | ||
804ee07c ZJS |
5583 | static int print_variable(const char *s) { |
5584 | const char *sep; | |
5585 | _cleanup_free_ char *esc = NULL; | |
5586 | ||
5587 | sep = strchr(s, '='); | |
5588 | if (!sep) { | |
5589 | log_error("Invalid environment block"); | |
5590 | return -EUCLEAN; | |
5591 | } | |
5592 | ||
5593 | esc = shell_maybe_quote(sep + 1, ESCAPE_POSIX); | |
5594 | if (!esc) | |
5595 | return log_oom(); | |
5596 | ||
5597 | printf("%.*s=%s\n", (int)(sep-s), s, esc); | |
5598 | return 0; | |
5599 | } | |
5600 | ||
e449de87 | 5601 | static int show_environment(int argc, char *argv[], void *userdata) { |
4afd3348 LP |
5602 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
5603 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
f459b602 | 5604 | const char *text; |
4fbd7192 | 5605 | sd_bus *bus; |
7e4249b9 | 5606 | int r; |
7e4249b9 | 5607 | |
4fbd7192 LP |
5608 | r = acquire_bus(BUS_MANAGER, &bus); |
5609 | if (r < 0) | |
5610 | return r; | |
5611 | ||
ee5324aa | 5612 | (void) pager_open(arg_no_pager, false); |
d2ad7e1b | 5613 | |
f459b602 | 5614 | r = sd_bus_get_property( |
f22f08cd SP |
5615 | bus, |
5616 | "org.freedesktop.systemd1", | |
5617 | "/org/freedesktop/systemd1", | |
f459b602 MAP |
5618 | "org.freedesktop.systemd1.Manager", |
5619 | "Environment", | |
5620 | &error, | |
f22f08cd | 5621 | &reply, |
f459b602 | 5622 | "as"); |
691395d8 LP |
5623 | if (r < 0) |
5624 | return log_error_errno(r, "Failed to get environment: %s", bus_error_message(&error, r)); | |
7e4249b9 | 5625 | |
f459b602 MAP |
5626 | r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "s"); |
5627 | if (r < 0) | |
5628 | return bus_log_parse_error(r); | |
7e4249b9 | 5629 | |
804ee07c ZJS |
5630 | while ((r = sd_bus_message_read_basic(reply, SD_BUS_TYPE_STRING, &text)) > 0) { |
5631 | r = print_variable(text); | |
5632 | if (r < 0) | |
5633 | return r; | |
5634 | } | |
f459b602 MAP |
5635 | if (r < 0) |
5636 | return bus_log_parse_error(r); | |
7e4249b9 | 5637 | |
f459b602 MAP |
5638 | r = sd_bus_message_exit_container(reply); |
5639 | if (r < 0) | |
5640 | return bus_log_parse_error(r); | |
7e4249b9 | 5641 | |
f84190d8 | 5642 | return 0; |
7e4249b9 LP |
5643 | } |
5644 | ||
e449de87 | 5645 | static int switch_root(int argc, char *argv[], void *userdata) { |
4afd3348 | 5646 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
f39d4a08 HH |
5647 | _cleanup_free_ char *cmdline_init = NULL; |
5648 | const char *root, *init; | |
4fbd7192 | 5649 | sd_bus *bus; |
f459b602 | 5650 | int r; |
957eb8ca | 5651 | |
4fbd7192 LP |
5652 | if (arg_transport != BUS_TRANSPORT_LOCAL) { |
5653 | log_error("Cannot switch root remotely."); | |
5654 | return -EINVAL; | |
5655 | } | |
5656 | ||
e449de87 | 5657 | if (argc < 2 || argc > 3) { |
957eb8ca LP |
5658 | log_error("Wrong number of arguments."); |
5659 | return -EINVAL; | |
5660 | } | |
5661 | ||
e449de87 | 5662 | root = argv[1]; |
13068da8 | 5663 | |
e449de87 LP |
5664 | if (argc >= 3) |
5665 | init = argv[2]; | |
13068da8 | 5666 | else { |
f39d4a08 HH |
5667 | r = parse_env_file("/proc/cmdline", WHITESPACE, |
5668 | "init", &cmdline_init, | |
5669 | NULL); | |
5670 | if (r < 0) | |
da927ba9 | 5671 | log_debug_errno(r, "Failed to parse /proc/cmdline: %m"); |
13068da8 | 5672 | |
f39d4a08 | 5673 | init = cmdline_init; |
13068da8 | 5674 | } |
f459b602 | 5675 | |
3c6f7c34 | 5676 | init = empty_to_null(init); |
f39d4a08 HH |
5677 | if (init) { |
5678 | const char *root_systemd_path = NULL, *root_init_path = NULL; | |
5679 | ||
63c372cb LP |
5680 | root_systemd_path = strjoina(root, "/" SYSTEMD_BINARY_PATH); |
5681 | root_init_path = strjoina(root, "/", init); | |
f39d4a08 HH |
5682 | |
5683 | /* If the passed init is actually the same as the | |
5684 | * systemd binary, then let's suppress it. */ | |
e3f791a2 | 5685 | if (files_same(root_init_path, root_systemd_path, 0) > 0) |
f39d4a08 HH |
5686 | init = NULL; |
5687 | } | |
13068da8 | 5688 | |
acc28e2e | 5689 | /* Instruct PID1 to exclude us from its killing spree applied during |
b3ad0ff4 ZJS |
5690 | * the transition. Otherwise we would exit with a failure status even |
5691 | * though the switch to the new root has succeed. */ | |
5692 | argv_cmdline[0] = '@'; | |
acc28e2e | 5693 | |
4fbd7192 LP |
5694 | r = acquire_bus(BUS_MANAGER, &bus); |
5695 | if (r < 0) | |
5696 | return r; | |
5697 | ||
8a7a9cea ZJS |
5698 | /* If we are slow to exit after the root switch, the new systemd instance |
5699 | * will send us a signal to terminate. Just ignore it and exit normally. | |
5700 | * This way the unit does not end up as failed. | |
5701 | */ | |
5702 | r = ignore_signals(SIGTERM, -1); | |
5703 | if (r < 0) | |
5704 | log_warning_errno(r, "Failed to change disposition of SIGTERM to ignore: %m"); | |
5705 | ||
f39d4a08 | 5706 | log_debug("Switching root - root: %s; init: %s", root, strna(init)); |
957eb8ca | 5707 | |
f459b602 | 5708 | r = sd_bus_call_method( |
f22f08cd | 5709 | bus, |
957eb8ca LP |
5710 | "org.freedesktop.systemd1", |
5711 | "/org/freedesktop/systemd1", | |
5712 | "org.freedesktop.systemd1.Manager", | |
f22f08cd | 5713 | "SwitchRoot", |
f459b602 | 5714 | &error, |
f22f08cd | 5715 | NULL, |
f459b602 | 5716 | "ss", root, init); |
8a7a9cea ZJS |
5717 | if (r < 0) { |
5718 | (void) default_signals(SIGTERM, -1); | |
5719 | ||
691395d8 | 5720 | return log_error_errno(r, "Failed to switch root: %s", bus_error_message(&error, r)); |
8a7a9cea | 5721 | } |
f459b602 MAP |
5722 | |
5723 | return 0; | |
957eb8ca LP |
5724 | } |
5725 | ||
e449de87 | 5726 | static int set_environment(int argc, char *argv[], void *userdata) { |
4afd3348 LP |
5727 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
5728 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; | |
7e4249b9 | 5729 | const char *method; |
4fbd7192 | 5730 | sd_bus *bus; |
31e767f7 LP |
5731 | int r; |
5732 | ||
e449de87 LP |
5733 | assert(argc > 1); |
5734 | assert(argv); | |
7e4249b9 | 5735 | |
4fbd7192 LP |
5736 | r = acquire_bus(BUS_MANAGER, &bus); |
5737 | if (r < 0) | |
5738 | return r; | |
5739 | ||
8a4b13c5 | 5740 | polkit_agent_open_maybe(); |
d2ad7e1b | 5741 | |
e449de87 | 5742 | method = streq(argv[0], "set-environment") |
7e4249b9 LP |
5743 | ? "SetEnvironment" |
5744 | : "UnsetEnvironment"; | |
5745 | ||
f459b602 MAP |
5746 | r = sd_bus_message_new_method_call( |
5747 | bus, | |
151b9b96 | 5748 | &m, |
31e767f7 LP |
5749 | "org.freedesktop.systemd1", |
5750 | "/org/freedesktop/systemd1", | |
5751 | "org.freedesktop.systemd1.Manager", | |
151b9b96 | 5752 | method); |
f459b602 MAP |
5753 | if (r < 0) |
5754 | return bus_log_create_error(r); | |
7e4249b9 | 5755 | |
e449de87 | 5756 | r = sd_bus_message_append_strv(m, strv_skip(argv, 1)); |
31e767f7 | 5757 | if (r < 0) |
f459b602 | 5758 | return bus_log_create_error(r); |
7e4249b9 | 5759 | |
c49b30a2 | 5760 | r = sd_bus_call(bus, m, 0, &error, NULL); |
691395d8 LP |
5761 | if (r < 0) |
5762 | return log_error_errno(r, "Failed to set environment: %s", bus_error_message(&error, r)); | |
7e4249b9 | 5763 | |
f84190d8 | 5764 | return 0; |
7e4249b9 LP |
5765 | } |
5766 | ||
e449de87 | 5767 | static int import_environment(int argc, char *argv[], void *userdata) { |
4afd3348 LP |
5768 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
5769 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; | |
4fbd7192 | 5770 | sd_bus *bus; |
ac3efa8a LP |
5771 | int r; |
5772 | ||
4fbd7192 LP |
5773 | r = acquire_bus(BUS_MANAGER, &bus); |
5774 | if (r < 0) | |
5775 | return r; | |
5776 | ||
8a4b13c5 | 5777 | polkit_agent_open_maybe(); |
d2ad7e1b | 5778 | |
ac3efa8a LP |
5779 | r = sd_bus_message_new_method_call( |
5780 | bus, | |
151b9b96 | 5781 | &m, |
ac3efa8a LP |
5782 | "org.freedesktop.systemd1", |
5783 | "/org/freedesktop/systemd1", | |
5784 | "org.freedesktop.systemd1.Manager", | |
151b9b96 | 5785 | "SetEnvironment"); |
ac3efa8a LP |
5786 | if (r < 0) |
5787 | return bus_log_create_error(r); | |
5788 | ||
e449de87 | 5789 | if (argc < 2) |
ac3efa8a LP |
5790 | r = sd_bus_message_append_strv(m, environ); |
5791 | else { | |
5792 | char **a, **b; | |
5793 | ||
5794 | r = sd_bus_message_open_container(m, 'a', "s"); | |
5795 | if (r < 0) | |
5796 | return bus_log_create_error(r); | |
5797 | ||
e449de87 | 5798 | STRV_FOREACH(a, strv_skip(argv, 1)) { |
ac3efa8a LP |
5799 | |
5800 | if (!env_name_is_valid(*a)) { | |
5801 | log_error("Not a valid environment variable name: %s", *a); | |
5802 | return -EINVAL; | |
5803 | } | |
5804 | ||
5805 | STRV_FOREACH(b, environ) { | |
5806 | const char *eq; | |
5807 | ||
5808 | eq = startswith(*b, *a); | |
5809 | if (eq && *eq == '=') { | |
5810 | ||
5811 | r = sd_bus_message_append(m, "s", *b); | |
5812 | if (r < 0) | |
5813 | return bus_log_create_error(r); | |
5814 | ||
5815 | break; | |
5816 | } | |
5817 | } | |
5818 | } | |
5819 | ||
5820 | r = sd_bus_message_close_container(m); | |
5821 | } | |
5822 | if (r < 0) | |
5823 | return bus_log_create_error(r); | |
5824 | ||
5825 | r = sd_bus_call(bus, m, 0, &error, NULL); | |
691395d8 LP |
5826 | if (r < 0) |
5827 | return log_error_errno(r, "Failed to import environment: %s", bus_error_message(&error, r)); | |
ac3efa8a LP |
5828 | |
5829 | return 0; | |
5830 | } | |
5831 | ||
cbb13b2a | 5832 | static int enable_sysv_units(const char *verb, char **args) { |
729e3769 | 5833 | int r = 0; |
ee5762e3 | 5834 | |
349cc4a5 | 5835 | #if HAVE_SYSV_COMPAT |
8e766630 | 5836 | _cleanup_(lookup_paths_free) LookupPaths paths = {}; |
e735decc LP |
5837 | unsigned f = 0; |
5838 | ||
5839 | /* Processes all SysV units, and reshuffles the array so that afterwards only the native units remain */ | |
ee5762e3 | 5840 | |
729e3769 LP |
5841 | if (arg_scope != UNIT_FILE_SYSTEM) |
5842 | return 0; | |
ee5762e3 | 5843 | |
b41b9d2a LP |
5844 | if (getenv_bool("SYSTEMCTL_SKIP_SYSV") > 0) |
5845 | return 0; | |
5846 | ||
4c315c2c IS |
5847 | if (!STR_IN_SET(verb, |
5848 | "enable", | |
5849 | "disable", | |
5850 | "is-enabled")) | |
729e3769 | 5851 | return 0; |
ee5762e3 | 5852 | |
4943d143 | 5853 | r = lookup_paths_init(&paths, arg_scope, LOOKUP_PATHS_EXCLUDE_GENERATED, arg_root); |
729e3769 LP |
5854 | if (r < 0) |
5855 | return r; | |
ee5762e3 | 5856 | |
729e3769 | 5857 | r = 0; |
a644abed | 5858 | while (args[f]) { |
e735decc LP |
5859 | |
5860 | const char *argv[] = { | |
5861 | ROOTLIBEXECDIR "/systemd-sysv-install", | |
5862 | NULL, | |
5863 | NULL, | |
5864 | NULL, | |
5865 | NULL, | |
5866 | }; | |
5867 | ||
05cae7f3 | 5868 | _cleanup_free_ char *p = NULL, *q = NULL, *l = NULL; |
729e3769 | 5869 | bool found_native = false, found_sysv; |
e735decc | 5870 | const char *name; |
729e3769 | 5871 | unsigned c = 1; |
729e3769 | 5872 | pid_t pid; |
e735decc | 5873 | int j; |
ee5762e3 | 5874 | |
a644abed | 5875 | name = args[f++]; |
ee5762e3 | 5876 | |
729e3769 LP |
5877 | if (!endswith(name, ".service")) |
5878 | continue; | |
ee5762e3 | 5879 | |
729e3769 LP |
5880 | if (path_is_absolute(name)) |
5881 | continue; | |
ee5762e3 | 5882 | |
e735decc | 5883 | j = unit_file_exists(arg_scope, &paths, name); |
76ec966f | 5884 | if (j < 0 && !IN_SET(j, -ELOOP, -ERFKILL, -EADDRNOTAVAIL)) |
e735decc | 5885 | return log_error_errno(j, "Failed to lookup unit file state: %m"); |
3c6d8e57 | 5886 | found_native = j != 0; |
ee5762e3 | 5887 | |
e735decc LP |
5888 | /* If we have both a native unit and a SysV script, enable/disable them both (below); for is-enabled, |
5889 | * prefer the native unit */ | |
355ff449 | 5890 | if (found_native && streq(verb, "is-enabled")) |
729e3769 | 5891 | continue; |
ee5762e3 | 5892 | |
0c6ea3a4 ZJS |
5893 | p = path_join(arg_root, SYSTEM_SYSVINIT_PATH, name); |
5894 | if (!p) | |
60731f32 | 5895 | return log_oom(); |
ee5762e3 | 5896 | |
fbd0b64f | 5897 | p[strlen(p) - STRLEN(".service")] = 0; |
729e3769 | 5898 | found_sysv = access(p, F_OK) >= 0; |
4b6756a8 | 5899 | if (!found_sysv) |
729e3769 | 5900 | continue; |
71fad675 | 5901 | |
689e4e6a CR |
5902 | if (!arg_quiet) { |
5903 | if (found_native) | |
5904 | log_info("Synchronizing state of %s with SysV service script with %s.", name, argv[0]); | |
5905 | else | |
5906 | log_info("%s is not a native service, redirecting to systemd-sysv-install.", name); | |
5907 | } | |
ee5762e3 | 5908 | |
729e3769 LP |
5909 | if (!isempty(arg_root)) |
5910 | argv[c++] = q = strappend("--root=", arg_root); | |
ee5762e3 | 5911 | |
0f0467e6 | 5912 | argv[c++] = verb; |
2b6bf07d | 5913 | argv[c++] = basename(p); |
729e3769 | 5914 | argv[c] = NULL; |
ee5762e3 | 5915 | |
729e3769 | 5916 | l = strv_join((char**)argv, " "); |
60731f32 ZJS |
5917 | if (!l) |
5918 | return log_oom(); | |
ee5762e3 | 5919 | |
8ecc68f4 LP |
5920 | if (!arg_quiet) |
5921 | log_info("Executing: %s", l); | |
ee5762e3 | 5922 | |
d2e0ac3d | 5923 | j = safe_fork("(sysv-install)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_LOG, &pid); |
4c253ed1 | 5924 | if (j < 0) |
b6e1fff1 | 5925 | return j; |
4c253ed1 | 5926 | if (j == 0) { |
729e3769 | 5927 | /* Child */ |
729e3769 | 5928 | execv(argv[0], (char**) argv); |
cc7cb0ca | 5929 | log_error_errno(errno, "Failed to execute %s: %m", argv[0]); |
729e3769 LP |
5930 | _exit(EXIT_FAILURE); |
5931 | } | |
ee5762e3 | 5932 | |
2e87a1fd | 5933 | j = wait_for_terminate_and_check("sysv-install", pid, WAIT_LOG_ABNORMAL); |
d710aaf7 | 5934 | if (j < 0) |
2e87a1fd LP |
5935 | return j; |
5936 | if (streq(verb, "is-enabled")) { | |
b4a34311 | 5937 | if (j == EXIT_SUCCESS) { |
2e87a1fd LP |
5938 | if (!arg_quiet) |
5939 | puts("enabled"); | |
5940 | r = 1; | |
5941 | } else { | |
5942 | if (!arg_quiet) | |
5943 | puts("disabled"); | |
5944 | } | |
ee5762e3 | 5945 | |
b4a34311 | 5946 | } else if (j != EXIT_SUCCESS) |
2e87a1fd | 5947 | return -EBADE; /* We don't warn here, under the assumption the script already showed an explanation */ |
ee5762e3 | 5948 | |
355ff449 MP |
5949 | if (found_native) |
5950 | continue; | |
5951 | ||
a644abed | 5952 | /* Remove this entry, so that we don't try enabling it as native unit */ |
aba84331 LP |
5953 | assert(f > 0); |
5954 | f--; | |
5955 | assert(args[f] == name); | |
5956 | strv_remove(args, name); | |
729e3769 | 5957 | } |
ee5762e3 | 5958 | |
729e3769 LP |
5959 | #endif |
5960 | return r; | |
5961 | } | |
ee5762e3 | 5962 | |
37370d0c | 5963 | static int mangle_names(char **original_names, char ***mangled_names) { |
a33fdebb | 5964 | char **i, **l, **name; |
7410616c | 5965 | int r; |
37370d0c | 5966 | |
7410616c | 5967 | l = i = new(char*, strv_length(original_names) + 1); |
a33fdebb | 5968 | if (!l) |
37370d0c VP |
5969 | return log_oom(); |
5970 | ||
37370d0c | 5971 | STRV_FOREACH(name, original_names) { |
44386fc1 LN |
5972 | |
5973 | /* When enabling units qualified path names are OK, | |
5974 | * too, hence allow them explicitly. */ | |
5975 | ||
7410616c | 5976 | if (is_path(*name)) { |
44386fc1 | 5977 | *i = strdup(*name); |
7410616c LP |
5978 | if (!*i) { |
5979 | strv_free(l); | |
5980 | return log_oom(); | |
5981 | } | |
5982 | } else { | |
37cbc1d5 | 5983 | r = unit_name_mangle(*name, arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN, i); |
7410616c | 5984 | if (r < 0) { |
523f8cde | 5985 | *i = NULL; |
7410616c LP |
5986 | strv_free(l); |
5987 | return log_error_errno(r, "Failed to mangle unit name: %m"); | |
5988 | } | |
a33fdebb LP |
5989 | } |
5990 | ||
5991 | i++; | |
37370d0c | 5992 | } |
a33fdebb LP |
5993 | |
5994 | *i = NULL; | |
5995 | *mangled_names = l; | |
37370d0c VP |
5996 | |
5997 | return 0; | |
5998 | } | |
5999 | ||
decd7982 JR |
6000 | static int normalize_filenames(char **names) { |
6001 | char **u; | |
6002 | int r; | |
6003 | ||
6004 | STRV_FOREACH(u, names) | |
6005 | if (!path_is_absolute(*u)) { | |
6006 | char* normalized_path; | |
6007 | ||
6008 | if (!isempty(arg_root)) { | |
6009 | log_error("Non-absolute paths are not allowed when --root is used: %s", *u); | |
6010 | return -EINVAL; | |
6011 | } | |
6012 | ||
6013 | if (!strchr(*u,'/')) { | |
6014 | log_error("Link argument does contain at least one directory separator: %s", *u); | |
6015 | return -EINVAL; | |
6016 | } | |
6017 | ||
6018 | r = path_make_absolute_cwd(*u, &normalized_path); | |
6019 | if (r < 0) | |
6020 | return r; | |
6021 | ||
6022 | free_and_replace(*u, normalized_path); | |
6023 | } | |
6024 | ||
6025 | return 0; | |
6026 | } | |
6027 | ||
1d3c86c0 MS |
6028 | static int normalize_names(char **names, bool warn_if_path) { |
6029 | char **u; | |
6030 | bool was_path = false; | |
6031 | ||
6032 | STRV_FOREACH(u, names) { | |
6033 | int r; | |
6034 | ||
6035 | if (!is_path(*u)) | |
6036 | continue; | |
6037 | ||
6038 | r = free_and_strdup(u, basename(*u)); | |
6039 | if (r < 0) | |
6040 | return log_error_errno(r, "Failed to normalize unit file path: %m"); | |
6041 | ||
6042 | was_path = true; | |
6043 | } | |
6044 | ||
6045 | if (warn_if_path && was_path) | |
6046 | log_warning("Warning: Can't execute disable on the unit file path. Proceeding with the unit name."); | |
6047 | ||
6048 | return 0; | |
6049 | } | |
6050 | ||
173471b7 | 6051 | static int unit_exists(LookupPaths *lp, const char *unit) { |
d6568222 | 6052 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
f37f8a61 | 6053 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
d6568222 SS |
6054 | _cleanup_free_ char *path = NULL; |
6055 | static const struct bus_properties_map property_map[] = { | |
f37f8a61 YW |
6056 | { "LoadState", "s", NULL, offsetof(UnitStatusInfo, load_state) }, |
6057 | { "ActiveState", "s", NULL, offsetof(UnitStatusInfo, active_state)}, | |
d6568222 SS |
6058 | {}, |
6059 | }; | |
6060 | UnitStatusInfo info = {}; | |
6061 | sd_bus *bus; | |
6062 | int r; | |
6063 | ||
173471b7 ZJS |
6064 | if (unit_name_is_valid(unit, UNIT_NAME_TEMPLATE)) |
6065 | return unit_find_template_path(unit, lp, NULL, NULL); | |
6066 | ||
d6568222 SS |
6067 | path = unit_dbus_path_from_name(unit); |
6068 | if (!path) | |
6069 | return log_oom(); | |
6070 | ||
6071 | r = acquire_bus(BUS_MANAGER, &bus); | |
6072 | if (r < 0) | |
6073 | return r; | |
6074 | ||
a7e4861c | 6075 | r = bus_map_all_properties(bus, "org.freedesktop.systemd1", path, property_map, 0, &error, &m, &info); |
d6568222 SS |
6076 | if (r < 0) |
6077 | return log_error_errno(r, "Failed to get properties: %s", bus_error_message(&error, r)); | |
6078 | ||
d6568222 SS |
6079 | return !streq_ptr(info.load_state, "not-found") || !streq_ptr(info.active_state, "inactive"); |
6080 | } | |
6081 | ||
e449de87 | 6082 | static int enable_unit(int argc, char *argv[], void *userdata) { |
e3e0314b | 6083 | _cleanup_strv_free_ char **names = NULL; |
e449de87 | 6084 | const char *verb = argv[0]; |
729e3769 | 6085 | UnitFileChange *changes = NULL; |
da6053d0 | 6086 | size_t n_changes = 0; |
729e3769 | 6087 | int carries_install_info = -1; |
35b132e8 | 6088 | bool ignore_carries_install_info = arg_quiet; |
729e3769 | 6089 | int r; |
ee5762e3 | 6090 | |
e449de87 | 6091 | if (!argv[1]) |
ab5919fa MS |
6092 | return 0; |
6093 | ||
e449de87 | 6094 | r = mangle_names(strv_skip(argv, 1), &names); |
3a05c0f9 | 6095 | if (r < 0) |
cbb13b2a VP |
6096 | return r; |
6097 | ||
e3e0314b | 6098 | r = enable_sysv_units(verb, names); |
cbb13b2a VP |
6099 | if (r < 0) |
6100 | return r; | |
3a05c0f9 | 6101 | |
c61b443d | 6102 | /* If the operation was fully executed by the SysV compat, let's finish early */ |
823e5fab FB |
6103 | if (strv_isempty(names)) { |
6104 | if (arg_no_reload || install_client_side()) | |
6105 | return 0; | |
6106 | return daemon_reload(argc, argv, userdata); | |
6107 | } | |
67d66210 | 6108 | |
1d3c86c0 MS |
6109 | if (streq(verb, "disable")) { |
6110 | r = normalize_names(names, true); | |
6111 | if (r < 0) | |
6112 | return r; | |
6113 | } | |
6114 | ||
decd7982 JR |
6115 | if (streq(verb, "link")) { |
6116 | r = normalize_filenames(names); | |
6117 | if (r < 0) | |
6118 | return r; | |
6119 | } | |
6120 | ||
4fbd7192 | 6121 | if (install_client_side()) { |
b3796dd8 JS |
6122 | UnitFileFlags flags; |
6123 | ||
6124 | flags = args_to_flags(); | |
729e3769 | 6125 | if (streq(verb, "enable")) { |
b3796dd8 | 6126 | r = unit_file_enable(arg_scope, flags, arg_root, names, &changes, &n_changes); |
729e3769 LP |
6127 | carries_install_info = r; |
6128 | } else if (streq(verb, "disable")) | |
b3796dd8 | 6129 | r = unit_file_disable(arg_scope, flags, arg_root, names, &changes, &n_changes); |
729e3769 | 6130 | else if (streq(verb, "reenable")) { |
b3796dd8 | 6131 | r = unit_file_reenable(arg_scope, flags, arg_root, names, &changes, &n_changes); |
729e3769 LP |
6132 | carries_install_info = r; |
6133 | } else if (streq(verb, "link")) | |
b3796dd8 | 6134 | r = unit_file_link(arg_scope, flags, arg_root, names, &changes, &n_changes); |
729e3769 | 6135 | else if (streq(verb, "preset")) { |
b3796dd8 | 6136 | r = unit_file_preset(arg_scope, flags, arg_root, names, arg_preset_mode, &changes, &n_changes); |
729e3769 | 6137 | } else if (streq(verb, "mask")) |
b3796dd8 | 6138 | r = unit_file_mask(arg_scope, flags, arg_root, names, &changes, &n_changes); |
729e3769 | 6139 | else if (streq(verb, "unmask")) |
b3796dd8 | 6140 | r = unit_file_unmask(arg_scope, flags, arg_root, names, &changes, &n_changes); |
344ca755 LP |
6141 | else if (streq(verb, "revert")) |
6142 | r = unit_file_revert(arg_scope, arg_root, names, &changes, &n_changes); | |
729e3769 LP |
6143 | else |
6144 | assert_not_reached("Unknown verb"); | |
ee5762e3 | 6145 | |
af3d8113 | 6146 | unit_file_dump_changes(r, verb, changes, n_changes, arg_quiet); |
d073dea0 | 6147 | if (r < 0) |
85b78539 | 6148 | goto finish; |
df77cdf0 | 6149 | r = 0; |
729e3769 | 6150 | } else { |
4afd3348 LP |
6151 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL, *m = NULL; |
6152 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
39207373 | 6153 | bool expect_carries_install_info = false; |
344ca755 | 6154 | bool send_runtime = true, send_force = true, send_preset_mode = false; |
718db961 | 6155 | const char *method; |
4fbd7192 | 6156 | sd_bus *bus; |
729e3769 | 6157 | |
d6568222 | 6158 | if (STR_IN_SET(verb, "mask", "unmask")) { |
f8d6cb48 | 6159 | char **name; |
8e766630 | 6160 | _cleanup_(lookup_paths_free) LookupPaths lp = {}; |
173471b7 ZJS |
6161 | |
6162 | r = lookup_paths_init(&lp, arg_scope, 0, arg_root); | |
d6568222 SS |
6163 | if (r < 0) |
6164 | return r; | |
f8d6cb48 ZJS |
6165 | |
6166 | STRV_FOREACH(name, names) { | |
173471b7 | 6167 | r = unit_exists(&lp, *name); |
f8d6cb48 ZJS |
6168 | if (r < 0) |
6169 | return r; | |
6170 | if (r == 0) | |
6171 | log_notice("Unit %s does not exist, proceeding anyway.", *names); | |
6172 | } | |
d6568222 SS |
6173 | } |
6174 | ||
4fbd7192 LP |
6175 | r = acquire_bus(BUS_MANAGER, &bus); |
6176 | if (r < 0) | |
6177 | return r; | |
6178 | ||
8a4b13c5 | 6179 | polkit_agent_open_maybe(); |
d2ad7e1b | 6180 | |
729e3769 LP |
6181 | if (streq(verb, "enable")) { |
6182 | method = "EnableUnitFiles"; | |
6183 | expect_carries_install_info = true; | |
6184 | } else if (streq(verb, "disable")) { | |
6185 | method = "DisableUnitFiles"; | |
6186 | send_force = false; | |
6187 | } else if (streq(verb, "reenable")) { | |
6188 | method = "ReenableUnitFiles"; | |
6189 | expect_carries_install_info = true; | |
6190 | } else if (streq(verb, "link")) | |
6191 | method = "LinkUnitFiles"; | |
6192 | else if (streq(verb, "preset")) { | |
d309c1c3 LP |
6193 | |
6194 | if (arg_preset_mode != UNIT_FILE_PRESET_FULL) { | |
6195 | method = "PresetUnitFilesWithMode"; | |
6196 | send_preset_mode = true; | |
6197 | } else | |
6198 | method = "PresetUnitFiles"; | |
6199 | ||
729e3769 | 6200 | expect_carries_install_info = true; |
39207373 | 6201 | ignore_carries_install_info = true; |
729e3769 LP |
6202 | } else if (streq(verb, "mask")) |
6203 | method = "MaskUnitFiles"; | |
6204 | else if (streq(verb, "unmask")) { | |
6205 | method = "UnmaskUnitFiles"; | |
6206 | send_force = false; | |
344ca755 LP |
6207 | } else if (streq(verb, "revert")) { |
6208 | method = "RevertUnitFiles"; | |
6209 | send_runtime = send_force = false; | |
729e3769 LP |
6210 | } else |
6211 | assert_not_reached("Unknown verb"); | |
6212 | ||
f459b602 MAP |
6213 | r = sd_bus_message_new_method_call( |
6214 | bus, | |
151b9b96 | 6215 | &m, |
729e3769 LP |
6216 | "org.freedesktop.systemd1", |
6217 | "/org/freedesktop/systemd1", | |
6218 | "org.freedesktop.systemd1.Manager", | |
151b9b96 | 6219 | method); |
f459b602 MAP |
6220 | if (r < 0) |
6221 | return bus_log_create_error(r); | |
ee5762e3 | 6222 | |
e3e0314b | 6223 | r = sd_bus_message_append_strv(m, names); |
f459b602 MAP |
6224 | if (r < 0) |
6225 | return bus_log_create_error(r); | |
ee5762e3 | 6226 | |
d309c1c3 LP |
6227 | if (send_preset_mode) { |
6228 | r = sd_bus_message_append(m, "s", unit_file_preset_mode_to_string(arg_preset_mode)); | |
6229 | if (r < 0) | |
6230 | return bus_log_create_error(r); | |
6231 | } | |
6232 | ||
344ca755 LP |
6233 | if (send_runtime) { |
6234 | r = sd_bus_message_append(m, "b", arg_runtime); | |
6235 | if (r < 0) | |
6236 | return bus_log_create_error(r); | |
6237 | } | |
ee5762e3 | 6238 | |
729e3769 | 6239 | if (send_force) { |
f459b602 MAP |
6240 | r = sd_bus_message_append(m, "b", arg_force); |
6241 | if (r < 0) | |
6242 | return bus_log_create_error(r); | |
ee5762e3 LP |
6243 | } |
6244 | ||
c49b30a2 | 6245 | r = sd_bus_call(bus, m, 0, &error, &reply); |
691395d8 | 6246 | if (r < 0) |
af3d8113 | 6247 | return log_error_errno(r, "Failed to %s unit: %s", verb, bus_error_message(&error, r)); |
be394c48 | 6248 | |
729e3769 | 6249 | if (expect_carries_install_info) { |
f459b602 MAP |
6250 | r = sd_bus_message_read(reply, "b", &carries_install_info); |
6251 | if (r < 0) | |
6252 | return bus_log_parse_error(r); | |
ee5762e3 LP |
6253 | } |
6254 | ||
57ab2eab | 6255 | r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet, &changes, &n_changes); |
f459b602 | 6256 | if (r < 0) |
85b78539 | 6257 | goto finish; |
b77398f7 | 6258 | |
93c941e3 | 6259 | /* Try to reload if enabled */ |
d6cb60c7 | 6260 | if (!arg_no_reload) |
e449de87 | 6261 | r = daemon_reload(argc, argv, userdata); |
f459b602 MAP |
6262 | else |
6263 | r = 0; | |
b647f10d | 6264 | } |
3d3961f2 | 6265 | |
39207373 | 6266 | if (carries_install_info == 0 && !ignore_carries_install_info) |
fe4aede9 ZJS |
6267 | log_warning("The unit files have no installation config (WantedBy, RequiredBy, Also, Alias\n" |
6268 | "settings in the [Install] section, and DefaultInstance for template units).\n" | |
6269 | "This means they are not meant to be enabled using systemctl.\n" | |
416389f7 LP |
6270 | "Possible reasons for having this kind of units are:\n" |
6271 | "1) A unit may be statically enabled by being symlinked from another unit's\n" | |
6272 | " .wants/ or .requires/ directory.\n" | |
6273 | "2) A unit's purpose may be to act as a helper for some other unit which has\n" | |
6274 | " a requirement dependency on it.\n" | |
6275 | "3) A unit may be started when needed via activation (socket, path, timer,\n" | |
fe4aede9 ZJS |
6276 | " D-Bus, udev, scripted systemctl call, ...).\n" |
6277 | "4) In case of template units, the unit is meant to be enabled with some\n" | |
6278 | " instance name specified."); | |
ee5762e3 | 6279 | |
6bc30691 | 6280 | if (arg_now && STR_IN_SET(argv[0], "enable", "disable", "mask")) { |
4fbd7192 | 6281 | sd_bus *bus; |
da6053d0 | 6282 | size_t len, i; |
57ab2eab | 6283 | |
4fbd7192 LP |
6284 | r = acquire_bus(BUS_MANAGER, &bus); |
6285 | if (r < 0) | |
d073dea0 | 6286 | goto finish; |
4fbd7192 | 6287 | |
6bc30691 JS |
6288 | len = strv_length(names); |
6289 | { | |
6290 | char *new_args[len + 2]; | |
57ab2eab | 6291 | |
6bc30691 JS |
6292 | new_args[0] = (char*) (streq(argv[0], "enable") ? "start" : "stop"); |
6293 | for (i = 0; i < len; i++) | |
6294 | new_args[i + 1] = basename(names[i]); | |
6295 | new_args[i + 1] = NULL; | |
6296 | ||
6297 | r = start_unit(len + 1, new_args, userdata); | |
6298 | } | |
57ab2eab JS |
6299 | } |
6300 | ||
729e3769 | 6301 | finish: |
729e3769 | 6302 | unit_file_changes_free(changes, n_changes); |
ee5762e3 | 6303 | |
729e3769 | 6304 | return r; |
ee5762e3 LP |
6305 | } |
6306 | ||
e449de87 | 6307 | static int add_dependency(int argc, char *argv[], void *userdata) { |
e94937df LN |
6308 | _cleanup_strv_free_ char **names = NULL; |
6309 | _cleanup_free_ char *target = NULL; | |
e449de87 | 6310 | const char *verb = argv[0]; |
acc0269c | 6311 | UnitFileChange *changes = NULL; |
da6053d0 | 6312 | size_t n_changes = 0; |
e94937df LN |
6313 | UnitDependency dep; |
6314 | int r = 0; | |
6315 | ||
e449de87 | 6316 | if (!argv[1]) |
e94937df LN |
6317 | return 0; |
6318 | ||
37cbc1d5 | 6319 | r = unit_name_mangle_with_suffix(argv[1], arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN, ".target", &target); |
7410616c LP |
6320 | if (r < 0) |
6321 | return log_error_errno(r, "Failed to mangle unit name: %m"); | |
e94937df | 6322 | |
e449de87 | 6323 | r = mangle_names(strv_skip(argv, 2), &names); |
e94937df LN |
6324 | if (r < 0) |
6325 | return r; | |
6326 | ||
6327 | if (streq(verb, "add-wants")) | |
6328 | dep = UNIT_WANTS; | |
6329 | else if (streq(verb, "add-requires")) | |
6330 | dep = UNIT_REQUIRES; | |
6331 | else | |
6332 | assert_not_reached("Unknown verb"); | |
6333 | ||
4fbd7192 | 6334 | if (install_client_side()) { |
b3796dd8 | 6335 | r = unit_file_add_dependency(arg_scope, args_to_flags(), arg_root, names, target, dep, &changes, &n_changes); |
af3d8113 | 6336 | unit_file_dump_changes(r, "add dependency on", changes, n_changes, arg_quiet); |
5f056378 CH |
6337 | |
6338 | if (r > 0) | |
6339 | r = 0; | |
e94937df | 6340 | } else { |
4afd3348 LP |
6341 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL, *m = NULL; |
6342 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
4fbd7192 | 6343 | sd_bus *bus; |
e94937df | 6344 | |
4fbd7192 LP |
6345 | r = acquire_bus(BUS_MANAGER, &bus); |
6346 | if (r < 0) | |
6347 | return r; | |
6348 | ||
8a4b13c5 | 6349 | polkit_agent_open_maybe(); |
d2ad7e1b | 6350 | |
e94937df LN |
6351 | r = sd_bus_message_new_method_call( |
6352 | bus, | |
6353 | &m, | |
6354 | "org.freedesktop.systemd1", | |
6355 | "/org/freedesktop/systemd1", | |
6356 | "org.freedesktop.systemd1.Manager", | |
6357 | "AddDependencyUnitFiles"); | |
6358 | if (r < 0) | |
6359 | return bus_log_create_error(r); | |
6360 | ||
342641fb | 6361 | r = sd_bus_message_append_strv(m, names); |
e94937df LN |
6362 | if (r < 0) |
6363 | return bus_log_create_error(r); | |
6364 | ||
342641fb | 6365 | r = sd_bus_message_append(m, "ssbb", target, unit_dependency_to_string(dep), arg_runtime, arg_force); |
e94937df LN |
6366 | if (r < 0) |
6367 | return bus_log_create_error(r); | |
6368 | ||
6369 | r = sd_bus_call(bus, m, 0, &error, &reply); | |
691395d8 | 6370 | if (r < 0) |
af3d8113 | 6371 | return log_error_errno(r, "Failed to add dependency: %s", bus_error_message(&error, r)); |
e94937df | 6372 | |
acc0269c | 6373 | r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet, &changes, &n_changes); |
e94937df | 6374 | if (r < 0) |
acc0269c | 6375 | goto finish; |
e94937df | 6376 | |
acc0269c CH |
6377 | if (arg_no_reload) { |
6378 | r = 0; | |
6379 | goto finish; | |
6380 | } | |
6381 | ||
6382 | r = daemon_reload(argc, argv, userdata); | |
e94937df | 6383 | } |
acc0269c CH |
6384 | |
6385 | finish: | |
6386 | unit_file_changes_free(changes, n_changes); | |
6387 | ||
6388 | return r; | |
e94937df LN |
6389 | } |
6390 | ||
e449de87 | 6391 | static int preset_all(int argc, char *argv[], void *userdata) { |
acc0269c | 6392 | UnitFileChange *changes = NULL; |
da6053d0 | 6393 | size_t n_changes = 0; |
d309c1c3 LP |
6394 | int r; |
6395 | ||
4fbd7192 | 6396 | if (install_client_side()) { |
b3796dd8 | 6397 | r = unit_file_preset_all(arg_scope, args_to_flags(), arg_root, arg_preset_mode, &changes, &n_changes); |
af3d8113 | 6398 | unit_file_dump_changes(r, "preset", changes, n_changes, arg_quiet); |
5f056378 CH |
6399 | |
6400 | if (r > 0) | |
6401 | r = 0; | |
d309c1c3 | 6402 | } else { |
4afd3348 LP |
6403 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
6404 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
4fbd7192 | 6405 | sd_bus *bus; |
d309c1c3 | 6406 | |
4fbd7192 LP |
6407 | r = acquire_bus(BUS_MANAGER, &bus); |
6408 | if (r < 0) | |
6409 | return r; | |
6410 | ||
8a4b13c5 | 6411 | polkit_agent_open_maybe(); |
d2ad7e1b | 6412 | |
6e646d22 | 6413 | r = sd_bus_call_method( |
d309c1c3 LP |
6414 | bus, |
6415 | "org.freedesktop.systemd1", | |
6416 | "/org/freedesktop/systemd1", | |
6417 | "org.freedesktop.systemd1.Manager", | |
6e646d22 LP |
6418 | "PresetAllUnitFiles", |
6419 | &error, | |
6420 | &reply, | |
d309c1c3 LP |
6421 | "sbb", |
6422 | unit_file_preset_mode_to_string(arg_preset_mode), | |
6423 | arg_runtime, | |
6424 | arg_force); | |
691395d8 | 6425 | if (r < 0) |
af3d8113 | 6426 | return log_error_errno(r, "Failed to preset all units: %s", bus_error_message(&error, r)); |
d309c1c3 | 6427 | |
acc0269c | 6428 | r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet, &changes, &n_changes); |
d309c1c3 | 6429 | if (r < 0) |
acc0269c | 6430 | goto finish; |
d309c1c3 | 6431 | |
acc0269c CH |
6432 | if (arg_no_reload) { |
6433 | r = 0; | |
6434 | goto finish; | |
6435 | } | |
6436 | ||
6437 | r = daemon_reload(argc, argv, userdata); | |
d309c1c3 | 6438 | } |
acc0269c CH |
6439 | |
6440 | finish: | |
6441 | unit_file_changes_free(changes, n_changes); | |
6442 | ||
6443 | return r; | |
d309c1c3 LP |
6444 | } |
6445 | ||
3b3557c4 JS |
6446 | static int show_installation_targets_client_side(const char *name) { |
6447 | UnitFileChange *changes = NULL; | |
da6053d0 | 6448 | size_t n_changes = 0, i; |
3b3557c4 JS |
6449 | UnitFileFlags flags; |
6450 | char **p; | |
6451 | int r; | |
6452 | ||
6453 | p = STRV_MAKE(name); | |
6454 | flags = UNIT_FILE_DRY_RUN | | |
6455 | (arg_runtime ? UNIT_FILE_RUNTIME : 0); | |
6456 | ||
6457 | r = unit_file_disable(UNIT_FILE_SYSTEM, flags, NULL, p, &changes, &n_changes); | |
6458 | if (r < 0) | |
6459 | return log_error_errno(r, "Failed to get file links for %s: %m", name); | |
6460 | ||
6461 | for (i = 0; i < n_changes; i++) | |
6462 | if (changes[i].type == UNIT_FILE_UNLINK) | |
6463 | printf(" %s\n", changes[i].path); | |
6464 | ||
6465 | return 0; | |
6466 | } | |
6467 | ||
6468 | static int show_installation_targets(sd_bus *bus, const char *name) { | |
6469 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
6470 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
6471 | const char *link; | |
6472 | int r; | |
6473 | ||
6474 | r = sd_bus_call_method( | |
6475 | bus, | |
6476 | "org.freedesktop.systemd1", | |
6477 | "/org/freedesktop/systemd1", | |
6478 | "org.freedesktop.systemd1.Manager", | |
6479 | "GetUnitFileLinks", | |
6480 | &error, | |
6481 | &reply, | |
6482 | "sb", name, arg_runtime); | |
6483 | if (r < 0) | |
6484 | return log_error_errno(r, "Failed to get unit file links for %s: %s", name, bus_error_message(&error, r)); | |
6485 | ||
6486 | r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "s"); | |
6487 | if (r < 0) | |
6488 | return bus_log_parse_error(r); | |
6489 | ||
6490 | while ((r = sd_bus_message_read(reply, "s", &link)) > 0) | |
6491 | printf(" %s\n", link); | |
6492 | ||
6493 | if (r < 0) | |
6494 | return bus_log_parse_error(r); | |
6495 | ||
6496 | r = sd_bus_message_exit_container(reply); | |
6497 | if (r < 0) | |
6498 | return bus_log_parse_error(r); | |
6499 | ||
6500 | return 0; | |
6501 | } | |
6502 | ||
e449de87 | 6503 | static int unit_is_enabled(int argc, char *argv[], void *userdata) { |
f459b602 | 6504 | |
e3e0314b | 6505 | _cleanup_strv_free_ char **names = NULL; |
729e3769 LP |
6506 | bool enabled; |
6507 | char **name; | |
f459b602 | 6508 | int r; |
ee5762e3 | 6509 | |
e449de87 | 6510 | r = mangle_names(strv_skip(argv, 1), &names); |
cbb13b2a VP |
6511 | if (r < 0) |
6512 | return r; | |
6513 | ||
e449de87 | 6514 | r = enable_sysv_units(argv[0], names); |
729e3769 LP |
6515 | if (r < 0) |
6516 | return r; | |
ee5762e3 | 6517 | |
729e3769 | 6518 | enabled = r > 0; |
ee5762e3 | 6519 | |
4fbd7192 | 6520 | if (install_client_side()) { |
e3e0314b | 6521 | STRV_FOREACH(name, names) { |
729e3769 | 6522 | UnitFileState state; |
ee5762e3 | 6523 | |
0ec0deaa LP |
6524 | r = unit_file_get_state(arg_scope, arg_root, *name, &state); |
6525 | if (r < 0) | |
c5024cd0 | 6526 | return log_error_errno(r, "Failed to get unit file state for %s: %m", *name); |
ee5762e3 | 6527 | |
4c315c2c IS |
6528 | if (IN_SET(state, |
6529 | UNIT_FILE_ENABLED, | |
6530 | UNIT_FILE_ENABLED_RUNTIME, | |
6531 | UNIT_FILE_STATIC, | |
f4139308 LP |
6532 | UNIT_FILE_INDIRECT, |
6533 | UNIT_FILE_GENERATED)) | |
729e3769 LP |
6534 | enabled = true; |
6535 | ||
3b3557c4 | 6536 | if (!arg_quiet) { |
729e3769 | 6537 | puts(unit_file_state_to_string(state)); |
3b3557c4 JS |
6538 | if (arg_full) { |
6539 | r = show_installation_targets_client_side(*name); | |
6540 | if (r < 0) | |
6541 | return r; | |
6542 | } | |
6543 | } | |
71fad675 | 6544 | } |
ee5762e3 | 6545 | |
5f056378 | 6546 | r = 0; |
729e3769 | 6547 | } else { |
4afd3348 | 6548 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
4fbd7192 LP |
6549 | sd_bus *bus; |
6550 | ||
6551 | r = acquire_bus(BUS_MANAGER, &bus); | |
6552 | if (r < 0) | |
6553 | return r; | |
6554 | ||
e3e0314b | 6555 | STRV_FOREACH(name, names) { |
4afd3348 | 6556 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
729e3769 | 6557 | const char *s; |
63a723f3 | 6558 | |
f459b602 | 6559 | r = sd_bus_call_method( |
f22f08cd | 6560 | bus, |
729e3769 LP |
6561 | "org.freedesktop.systemd1", |
6562 | "/org/freedesktop/systemd1", | |
6563 | "org.freedesktop.systemd1.Manager", | |
f22f08cd | 6564 | "GetUnitFileState", |
f459b602 | 6565 | &error, |
f22f08cd | 6566 | &reply, |
04504f93 | 6567 | "s", *name); |
691395d8 LP |
6568 | if (r < 0) |
6569 | return log_error_errno(r, "Failed to get unit file state for %s: %s", *name, bus_error_message(&error, r)); | |
ee5762e3 | 6570 | |
f459b602 MAP |
6571 | r = sd_bus_message_read(reply, "s", &s); |
6572 | if (r < 0) | |
6573 | return bus_log_parse_error(r); | |
ee5762e3 | 6574 | |
f4139308 | 6575 | if (STR_IN_SET(s, "enabled", "enabled-runtime", "static", "indirect", "generated")) |
729e3769 LP |
6576 | enabled = true; |
6577 | ||
3b3557c4 | 6578 | if (!arg_quiet) { |
729e3769 | 6579 | puts(s); |
3b3557c4 JS |
6580 | if (arg_full) { |
6581 | r = show_installation_targets(bus, *name); | |
6582 | if (r < 0) | |
6583 | return r; | |
6584 | } | |
6585 | } | |
560d8f23 | 6586 | } |
ee5762e3 LP |
6587 | } |
6588 | ||
f4139308 | 6589 | return enabled ? EXIT_SUCCESS : EXIT_FAILURE; |
ee5762e3 LP |
6590 | } |
6591 | ||
e449de87 | 6592 | static int is_system_running(int argc, char *argv[], void *userdata) { |
99813a19 | 6593 | _cleanup_free_ char *state = NULL; |
4fbd7192 | 6594 | sd_bus *bus; |
99813a19 LP |
6595 | int r; |
6596 | ||
040524b4 | 6597 | if (running_in_chroot() > 0 || (arg_transport == BUS_TRANSPORT_LOCAL && !sd_booted())) { |
94f099d8 LP |
6598 | if (!arg_quiet) |
6599 | puts("offline"); | |
6600 | return EXIT_FAILURE; | |
6601 | } | |
6602 | ||
4fbd7192 LP |
6603 | r = acquire_bus(BUS_MANAGER, &bus); |
6604 | if (r < 0) | |
6605 | return r; | |
6606 | ||
99813a19 LP |
6607 | r = sd_bus_get_property_string( |
6608 | bus, | |
6609 | "org.freedesktop.systemd1", | |
6610 | "/org/freedesktop/systemd1", | |
6611 | "org.freedesktop.systemd1.Manager", | |
6612 | "SystemState", | |
6613 | NULL, | |
6614 | &state); | |
6615 | if (r < 0) { | |
6616 | if (!arg_quiet) | |
6617 | puts("unknown"); | |
6618 | return 0; | |
6619 | } | |
6620 | ||
6621 | if (!arg_quiet) | |
6622 | puts(state); | |
6623 | ||
6624 | return streq(state, "running") ? EXIT_SUCCESS : EXIT_FAILURE; | |
6625 | } | |
6626 | ||
7d4fb3b1 | 6627 | static int create_edit_temp_file(const char *new_path, const char *original_path, char **ret_tmp_fn) { |
45519fd6 | 6628 | _cleanup_free_ char *t = NULL; |
ae6c3cc0 | 6629 | int r; |
7d4fb3b1 RC |
6630 | |
6631 | assert(new_path); | |
6632 | assert(original_path); | |
6633 | assert(ret_tmp_fn); | |
6634 | ||
14bcf25c | 6635 | r = tempfn_random(new_path, NULL, &t); |
ae6c3cc0 | 6636 | if (r < 0) |
029009d4 | 6637 | return log_error_errno(r, "Failed to determine temporary filename for \"%s\": %m", new_path); |
7d4fb3b1 RC |
6638 | |
6639 | r = mkdir_parents(new_path, 0755); | |
45519fd6 | 6640 | if (r < 0) |
691395d8 | 6641 | return log_error_errno(r, "Failed to create directories for \"%s\": %m", new_path); |
7d4fb3b1 | 6642 | |
1c876927 | 6643 | r = copy_file(original_path, t, 0, 0644, 0, COPY_REFLINK); |
7d4fb3b1 | 6644 | if (r == -ENOENT) { |
45519fd6 | 6645 | |
7d4fb3b1 | 6646 | r = touch(t); |
45519fd6 LP |
6647 | if (r < 0) |
6648 | return log_error_errno(r, "Failed to create temporary file \"%s\": %m", t); | |
6649 | ||
6650 | } else if (r < 0) | |
39c38ce1 | 6651 | return log_error_errno(r, "Failed to create temporary file for \"%s\": %m", new_path); |
7d4fb3b1 | 6652 | |
ae2a15bc | 6653 | *ret_tmp_fn = TAKE_PTR(t); |
7d4fb3b1 RC |
6654 | |
6655 | return 0; | |
6656 | } | |
6657 | ||
c51932be LP |
6658 | static int get_file_to_edit( |
6659 | const LookupPaths *paths, | |
6660 | const char *name, | |
6661 | char **ret_path) { | |
6662 | ||
6663 | _cleanup_free_ char *path = NULL, *run = NULL; | |
7d4fb3b1 | 6664 | |
45519fd6 LP |
6665 | assert(name); |
6666 | assert(ret_path); | |
6667 | ||
605405c6 | 6668 | path = strjoin(paths->persistent_config, "/", name); |
c51932be | 6669 | if (!path) |
7d4fb3b1 RC |
6670 | return log_oom(); |
6671 | ||
c51932be | 6672 | if (arg_runtime) { |
605405c6 | 6673 | run = strjoin(paths->runtime_config, "/", name); |
c51932be LP |
6674 | if (!run) |
6675 | return log_oom(); | |
6676 | } | |
6677 | ||
bc854dc7 | 6678 | if (arg_runtime) { |
691395d8 LP |
6679 | if (access(path, F_OK) >= 0) { |
6680 | log_error("Refusing to create \"%s\" because it would be overridden by \"%s\" anyway.", run, path); | |
6681 | return -EEXIST; | |
6682 | } | |
6683 | ||
ae2a15bc LP |
6684 | *ret_path = TAKE_PTR(run); |
6685 | } else | |
6686 | *ret_path = TAKE_PTR(path); | |
7d4fb3b1 RC |
6687 | |
6688 | return 0; | |
6689 | } | |
6690 | ||
39c38ce1 | 6691 | static int unit_file_create_new( |
c51932be LP |
6692 | const LookupPaths *paths, |
6693 | const char *unit_name, | |
39c38ce1 | 6694 | const char *suffix, |
c51932be LP |
6695 | char **ret_new_path, |
6696 | char **ret_tmp_path) { | |
6697 | ||
45519fd6 | 6698 | char *tmp_new_path, *tmp_tmp_path, *ending; |
7d4fb3b1 RC |
6699 | int r; |
6700 | ||
6701 | assert(unit_name); | |
6702 | assert(ret_new_path); | |
6703 | assert(ret_tmp_path); | |
6704 | ||
39c38ce1 | 6705 | ending = strjoina(unit_name, suffix); |
c51932be | 6706 | r = get_file_to_edit(paths, ending, &tmp_new_path); |
7d4fb3b1 RC |
6707 | if (r < 0) |
6708 | return r; | |
6709 | ||
6710 | r = create_edit_temp_file(tmp_new_path, tmp_new_path, &tmp_tmp_path); | |
6711 | if (r < 0) { | |
6712 | free(tmp_new_path); | |
6713 | return r; | |
6714 | } | |
6715 | ||
6716 | *ret_new_path = tmp_new_path; | |
6717 | *ret_tmp_path = tmp_tmp_path; | |
6718 | ||
6719 | return 0; | |
6720 | } | |
6721 | ||
1cfa9a4c | 6722 | static int unit_file_create_copy( |
c51932be | 6723 | const LookupPaths *paths, |
1cfa9a4c LP |
6724 | const char *unit_name, |
6725 | const char *fragment_path, | |
1cfa9a4c LP |
6726 | char **ret_new_path, |
6727 | char **ret_tmp_path) { | |
6728 | ||
45519fd6 | 6729 | char *tmp_new_path, *tmp_tmp_path; |
7d4fb3b1 RC |
6730 | int r; |
6731 | ||
6732 | assert(fragment_path); | |
6733 | assert(unit_name); | |
6734 | assert(ret_new_path); | |
6735 | assert(ret_tmp_path); | |
6736 | ||
c51932be | 6737 | r = get_file_to_edit(paths, unit_name, &tmp_new_path); |
7d4fb3b1 RC |
6738 | if (r < 0) |
6739 | return r; | |
6740 | ||
6741 | if (!path_equal(fragment_path, tmp_new_path) && access(tmp_new_path, F_OK) == 0) { | |
6742 | char response; | |
6743 | ||
029009d4 | 6744 | r = ask_char(&response, "yn", "\"%s\" already exists. Overwrite with \"%s\"? [(y)es, (n)o] ", tmp_new_path, fragment_path); |
7d4fb3b1 RC |
6745 | if (r < 0) { |
6746 | free(tmp_new_path); | |
6747 | return r; | |
6748 | } | |
6749 | if (response != 'y') { | |
6750 | log_warning("%s ignored", unit_name); | |
6751 | free(tmp_new_path); | |
dbf43a1b | 6752 | return -EKEYREJECTED; |
7d4fb3b1 RC |
6753 | } |
6754 | } | |
6755 | ||
6756 | r = create_edit_temp_file(tmp_new_path, fragment_path, &tmp_tmp_path); | |
6757 | if (r < 0) { | |
7d4fb3b1 RC |
6758 | free(tmp_new_path); |
6759 | return r; | |
6760 | } | |
6761 | ||
6762 | *ret_new_path = tmp_new_path; | |
6763 | *ret_tmp_path = tmp_tmp_path; | |
6764 | ||
6765 | return 0; | |
6766 | } | |
6767 | ||
6768 | static int run_editor(char **paths) { | |
7d4fb3b1 RC |
6769 | int r; |
6770 | ||
6771 | assert(paths); | |
6772 | ||
1f5d1e02 | 6773 | r = safe_fork("(editor)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_LOG|FORK_WAIT, NULL); |
4c253ed1 | 6774 | if (r < 0) |
b6e1fff1 | 6775 | return r; |
4c253ed1 | 6776 | if (r == 0) { |
7d4fb3b1 | 6777 | const char **args; |
9ef5d8f2 | 6778 | char *editor, **editor_args = NULL; |
1cfa9a4c | 6779 | char **tmp_path, **original_path, *p; |
da6053d0 | 6780 | size_t n_editor_args = 0, i = 1; |
7d4fb3b1 RC |
6781 | size_t argc; |
6782 | ||
6783 | argc = strv_length(paths)/2 + 1; | |
7d4fb3b1 RC |
6784 | |
6785 | /* SYSTEMD_EDITOR takes precedence over EDITOR which takes precedence over VISUAL | |
6786 | * If neither SYSTEMD_EDITOR nor EDITOR nor VISUAL are present, | |
6787 | * we try to execute well known editors | |
6788 | */ | |
6789 | editor = getenv("SYSTEMD_EDITOR"); | |
6790 | if (!editor) | |
6791 | editor = getenv("EDITOR"); | |
6792 | if (!editor) | |
6793 | editor = getenv("VISUAL"); | |
6794 | ||
6795 | if (!isempty(editor)) { | |
9ef5d8f2 JS |
6796 | editor_args = strv_split(editor, WHITESPACE); |
6797 | if (!editor_args) { | |
6798 | (void) log_oom(); | |
6799 | _exit(EXIT_FAILURE); | |
6800 | } | |
6801 | n_editor_args = strv_length(editor_args); | |
6802 | argc += n_editor_args - 1; | |
6803 | } | |
6804 | args = newa(const char*, argc + 1); | |
6805 | ||
6806 | if (n_editor_args > 0) { | |
6807 | args[0] = editor_args[0]; | |
6808 | for (; i < n_editor_args; i++) | |
6809 | args[i] = editor_args[i]; | |
6810 | } | |
6811 | ||
6812 | STRV_FOREACH_PAIR(original_path, tmp_path, paths) { | |
6813 | args[i] = *tmp_path; | |
6814 | i++; | |
7d4fb3b1 | 6815 | } |
9ef5d8f2 JS |
6816 | args[i] = NULL; |
6817 | ||
6818 | if (n_editor_args > 0) | |
6819 | execvp(args[0], (char* const*) args); | |
7d4fb3b1 | 6820 | |
9391a1c3 | 6821 | FOREACH_STRING(p, "editor", "nano", "vim", "vi") { |
1cfa9a4c LP |
6822 | args[0] = p; |
6823 | execvp(p, (char* const*) args); | |
7d4fb3b1 RC |
6824 | /* We do not fail if the editor doesn't exist |
6825 | * because we want to try each one of them before | |
6826 | * failing. | |
6827 | */ | |
6828 | if (errno != ENOENT) { | |
691395d8 | 6829 | log_error_errno(errno, "Failed to execute %s: %m", editor); |
7d4fb3b1 RC |
6830 | _exit(EXIT_FAILURE); |
6831 | } | |
6832 | } | |
6833 | ||
1cfa9a4c | 6834 | log_error("Cannot edit unit(s), no editor available. Please set either $SYSTEMD_EDITOR, $EDITOR or $VISUAL."); |
7d4fb3b1 RC |
6835 | _exit(EXIT_FAILURE); |
6836 | } | |
6837 | ||
45519fd6 | 6838 | return 0; |
7d4fb3b1 RC |
6839 | } |
6840 | ||
6841 | static int find_paths_to_edit(sd_bus *bus, char **names, char ***paths) { | |
8e766630 | 6842 | _cleanup_(lookup_paths_free) LookupPaths lp = {}; |
7d4fb3b1 RC |
6843 | char **name; |
6844 | int r; | |
6845 | ||
6846 | assert(names); | |
6847 | assert(paths); | |
6848 | ||
4943d143 | 6849 | r = lookup_paths_init(&lp, arg_scope, 0, arg_root); |
5b013a2f | 6850 | if (r < 0) |
8df18507 | 6851 | return r; |
7d4fb3b1 | 6852 | |
e9e310f8 | 6853 | STRV_FOREACH(name, names) { |
b74df547 ZJS |
6854 | _cleanup_free_ char *path = NULL, *new_path = NULL, *tmp_path = NULL, *tmp_name = NULL; |
6855 | const char *unit_name; | |
7d4fb3b1 | 6856 | |
4fbd7192 | 6857 | r = unit_find_paths(bus, *name, &lp, &path, NULL); |
e9e310f8 RC |
6858 | if (r < 0) |
6859 | return r; | |
b3734d98 ZJS |
6860 | |
6861 | if (r == 0) { | |
6862 | assert(!path); | |
6863 | ||
6864 | if (!arg_force) { | |
0dcabf09 ZJS |
6865 | log_error("Run 'systemctl edit%s --force %s' to create a new unit.", |
6866 | arg_scope == UNIT_FILE_GLOBAL ? " --global" : | |
6867 | arg_scope == UNIT_FILE_USER ? " --user" : "", | |
6868 | *name); | |
39c38ce1 DC |
6869 | return -ENOENT; |
6870 | } | |
39c38ce1 | 6871 | |
b3734d98 ZJS |
6872 | /* Create a new unit from scratch */ |
6873 | unit_name = *name; | |
6874 | r = unit_file_create_new(&lp, unit_name, | |
6875 | arg_full ? NULL : ".d/override.conf", | |
6876 | &new_path, &tmp_path); | |
6877 | } else { | |
6878 | assert(path); | |
b74df547 | 6879 | |
b3734d98 ZJS |
6880 | unit_name = basename(path); |
6881 | /* We follow unit aliases, but we need to propagate the instance */ | |
6882 | if (unit_name_is_valid(*name, UNIT_NAME_INSTANCE) && | |
6883 | unit_name_is_valid(unit_name, UNIT_NAME_TEMPLATE)) { | |
6884 | _cleanup_free_ char *instance = NULL; | |
b74df547 | 6885 | |
b3734d98 ZJS |
6886 | r = unit_name_to_instance(*name, &instance); |
6887 | if (r < 0) | |
6888 | return r; | |
b74df547 | 6889 | |
b3734d98 ZJS |
6890 | r = unit_name_replace_instance(unit_name, instance, &tmp_name); |
6891 | if (r < 0) | |
6892 | return r; | |
6893 | ||
6894 | unit_name = tmp_name; | |
6895 | } | |
b74df547 | 6896 | |
39c38ce1 | 6897 | if (arg_full) |
b74df547 | 6898 | r = unit_file_create_copy(&lp, unit_name, path, &new_path, &tmp_path); |
39c38ce1 | 6899 | else |
b74df547 | 6900 | r = unit_file_create_new(&lp, unit_name, ".d/override.conf", &new_path, &tmp_path); |
b3734d98 | 6901 | } |
e9e310f8 | 6902 | if (r < 0) |
ad2a0358 | 6903 | return r; |
7d4fb3b1 | 6904 | |
e9e310f8 RC |
6905 | r = strv_push_pair(paths, new_path, tmp_path); |
6906 | if (r < 0) | |
6907 | return log_oom(); | |
78df0edc | 6908 | new_path = tmp_path = NULL; |
7d4fb3b1 RC |
6909 | } |
6910 | ||
6911 | return 0; | |
6912 | } | |
6913 | ||
e449de87 | 6914 | static int edit(int argc, char *argv[], void *userdata) { |
7d4fb3b1 RC |
6915 | _cleanup_strv_free_ char **names = NULL; |
6916 | _cleanup_strv_free_ char **paths = NULL; | |
6917 | char **original, **tmp; | |
4fbd7192 | 6918 | sd_bus *bus; |
7d4fb3b1 RC |
6919 | int r; |
6920 | ||
7d4fb3b1 | 6921 | if (!on_tty()) { |
4fbd7192 | 6922 | log_error("Cannot edit units if not on a tty."); |
7d4fb3b1 RC |
6923 | return -EINVAL; |
6924 | } | |
6925 | ||
6926 | if (arg_transport != BUS_TRANSPORT_LOCAL) { | |
4fbd7192 | 6927 | log_error("Cannot edit units remotely."); |
7d4fb3b1 RC |
6928 | return -EINVAL; |
6929 | } | |
6930 | ||
4fbd7192 LP |
6931 | r = acquire_bus(BUS_MANAGER, &bus); |
6932 | if (r < 0) | |
6933 | return r; | |
6934 | ||
e449de87 | 6935 | r = expand_names(bus, strv_skip(argv, 1), NULL, &names); |
7d4fb3b1 RC |
6936 | if (r < 0) |
6937 | return log_error_errno(r, "Failed to expand names: %m"); | |
6938 | ||
9d9dd746 ZJS |
6939 | STRV_FOREACH(tmp, names) { |
6940 | r = unit_is_masked(bus, *tmp); | |
6941 | if (r < 0) | |
6942 | return r; | |
6943 | ||
6944 | if (r > 0) { | |
6945 | log_error("Cannot edit %s: unit is masked.", *tmp); | |
6946 | return -EINVAL; | |
6947 | } | |
6948 | } | |
6949 | ||
7d4fb3b1 RC |
6950 | r = find_paths_to_edit(bus, names, &paths); |
6951 | if (r < 0) | |
6952 | return r; | |
6953 | ||
b5e6a600 | 6954 | if (strv_isempty(paths)) |
7d4fb3b1 | 6955 | return -ENOENT; |
7d4fb3b1 RC |
6956 | |
6957 | r = run_editor(paths); | |
6958 | if (r < 0) | |
6959 | goto end; | |
6960 | ||
6961 | STRV_FOREACH_PAIR(original, tmp, paths) { | |
9d9dd746 ZJS |
6962 | /* If the temporary file is empty we ignore it. |
6963 | * This allows the user to cancel the modification. | |
7d4fb3b1 RC |
6964 | */ |
6965 | if (null_or_empty_path(*tmp)) { | |
45519fd6 | 6966 | log_warning("Editing \"%s\" canceled: temporary file is empty.", *original); |
7d4fb3b1 RC |
6967 | continue; |
6968 | } | |
45519fd6 | 6969 | |
7d4fb3b1 RC |
6970 | r = rename(*tmp, *original); |
6971 | if (r < 0) { | |
029009d4 | 6972 | r = log_error_errno(errno, "Failed to rename \"%s\" to \"%s\": %m", *tmp, *original); |
7d4fb3b1 RC |
6973 | goto end; |
6974 | } | |
6975 | } | |
6976 | ||
45519fd6 LP |
6977 | r = 0; |
6978 | ||
6979 | if (!arg_no_reload && !install_client_side()) | |
e449de87 | 6980 | r = daemon_reload(argc, argv, userdata); |
7d4fb3b1 RC |
6981 | |
6982 | end: | |
5f18271e | 6983 | STRV_FOREACH_PAIR(original, tmp, paths) { |
45519fd6 | 6984 | (void) unlink(*tmp); |
7d4fb3b1 | 6985 | |
5f18271e RC |
6986 | /* Removing empty dropin dirs */ |
6987 | if (!arg_full) { | |
043717f9 RC |
6988 | _cleanup_free_ char *dir; |
6989 | ||
6990 | dir = dirname_malloc(*original); | |
6991 | if (!dir) | |
6992 | return log_oom(); | |
6993 | ||
5f18271e RC |
6994 | /* no need to check if the dir is empty, rmdir |
6995 | * does nothing if it is not the case. | |
6996 | */ | |
6997 | (void) rmdir(dir); | |
6998 | } | |
6999 | } | |
7000 | ||
7d4fb3b1 RC |
7001 | return r; |
7002 | } | |
7003 | ||
601185b4 | 7004 | static void systemctl_help(void) { |
7e4249b9 | 7005 | |
ee5324aa | 7006 | (void) pager_open(arg_no_pager, false); |
729e3769 | 7007 | |
2e33c433 | 7008 | printf("%s [OPTIONS...] {COMMAND} ...\n\n" |
729e3769 | 7009 | "Query or send control commands to the systemd manager.\n\n" |
8a0867d6 LP |
7010 | " -h --help Show this help\n" |
7011 | " --version Show package version\n" | |
f459b602 MAP |
7012 | " --system Connect to system manager\n" |
7013 | " --user Connect to user service manager\n" | |
7014 | " -H --host=[USER@]HOST\n" | |
7015 | " Operate on remote host\n" | |
7016 | " -M --machine=CONTAINER\n" | |
7017 | " Operate on local container\n" | |
3fb90db2 ZJS |
7018 | " -t --type=TYPE List units of a particular type\n" |
7019 | " --state=STATE List units with particular LOAD or SUB or ACTIVE state\n" | |
8a0867d6 | 7020 | " -p --property=NAME Show only properties by this name\n" |
2cdbbc9a LP |
7021 | " -a --all Show all properties/all units currently in memory,\n" |
7022 | " including dead/empty ones. To list all units installed on\n" | |
7023 | " the system, use the 'list-unit-files' command instead.\n" | |
bef19548 | 7024 | " --failed Same as --state=failed\n" |
98a6e132 | 7025 | " -l --full Don't ellipsize unit names on output\n" |
1238ee09 | 7026 | " -r --recursive Show unit list of host and local containers\n" |
4dc5b821 LP |
7027 | " --reverse Show reverse dependencies with 'list-dependencies'\n" |
7028 | " --job-mode=MODE Specify how to deal with already queued jobs, when\n" | |
7029 | " queueing a new job\n" | |
a521ae4a | 7030 | " --show-types When showing sockets, explicitly show their type\n" |
4f9a9105 | 7031 | " --value When showing properties, only print the value\n" |
b37844d3 LP |
7032 | " -i --ignore-inhibitors\n" |
7033 | " When shutting down or sleeping, ignore inhibitors\n" | |
a8f11321 LP |
7034 | " --kill-who=WHO Who to send signal to\n" |
7035 | " -s --signal=SIGNAL Which signal to send\n" | |
57ab2eab | 7036 | " --now Start or stop unit in addition to enabling or disabling it\n" |
1ae17672 | 7037 | " --dry-run Only print what would be done\n" |
8a0867d6 | 7038 | " -q --quiet Suppress output\n" |
93a08841 | 7039 | " --wait For (re)start, wait until service stopped again\n" |
8a0867d6 | 7040 | " --no-block Do not wait until operation finished\n" |
8a0867d6 | 7041 | " --no-wall Don't send wall message before halt/power-off/reboot\n" |
3fb90db2 | 7042 | " --no-reload Don't reload daemon after en-/dis-abling unit files\n" |
ebed32bf | 7043 | " --no-legend Do not print a legend (column headers and hints)\n" |
69fc152f | 7044 | " --no-pager Do not pipe output into a pager\n" |
501fc174 LP |
7045 | " --no-ask-password\n" |
7046 | " Do not ask for system passwords\n" | |
4e93d369 LR |
7047 | " --global Enable/disable/mask unit files globally\n" |
7048 | " --runtime Enable/disable/mask unit files temporarily until next\n" | |
7049 | " reboot\n" | |
8a0867d6 LP |
7050 | " -f --force When enabling unit files, override existing symlinks\n" |
7051 | " When shutting down, execute action immediately\n" | |
3fb90db2 | 7052 | " --preset-mode= Apply only enable, only disable, or all presets\n" |
4e93d369 LR |
7053 | " --root=PATH Enable/disable/mask unit files in the specified root\n" |
7054 | " directory\n" | |
76fdc966 | 7055 | " -n --lines=INTEGER Number of journal entries to show\n" |
584c6e70 | 7056 | " -o --output=STRING Change journal output mode (short, short-precise,\n" |
7e563bfc IW |
7057 | " short-iso, short-iso-precise, short-full,\n" |
7058 | " short-monotonic, short-unix,\n" | |
584c6e70 | 7059 | " verbose, export, json, json-pretty, json-sse, cat)\n" |
5bdf2243 | 7060 | " --firmware-setup Tell the firmware to show the setup menu on next boot\n" |
815ebc54 | 7061 | " --plain Print unit dependencies as a list instead of a tree\n\n" |
34c4b47b | 7062 | "Unit Commands:\n" |
fbf3283b ZJS |
7063 | " list-units [PATTERN...] List units currently in memory\n" |
7064 | " list-sockets [PATTERN...] List socket units currently in memory,\n" | |
7065 | " ordered by address\n" | |
7066 | " list-timers [PATTERN...] List timer units currently in memory,\n" | |
7067 | " ordered by next elapse\n" | |
7068 | " start UNIT... Start (activate) one or more units\n" | |
7069 | " stop UNIT... Stop (deactivate) one or more units\n" | |
7070 | " reload UNIT... Reload one or more units\n" | |
7071 | " restart UNIT... Start or restart one or more units\n" | |
7072 | " try-restart UNIT... Restart one or more units if active\n" | |
7073 | " reload-or-restart UNIT... Reload one or more units if possible,\n" | |
7074 | " otherwise start or restart\n" | |
7075 | " try-reload-or-restart UNIT... If active, reload one or more units,\n" | |
7076 | " if supported, otherwise restart\n" | |
7077 | " isolate UNIT Start one unit and stop all others\n" | |
7078 | " kill UNIT... Send signal to processes of a unit\n" | |
7079 | " is-active PATTERN... Check whether units are active\n" | |
7080 | " is-failed PATTERN... Check whether units are failed\n" | |
7081 | " status [PATTERN...|PID...] Show runtime status of one or more units\n" | |
7082 | " show [PATTERN...|JOB...] Show properties of one or more\n" | |
7083 | " units/jobs or the manager\n" | |
7084 | " cat PATTERN... Show files and drop-ins of specified units\n" | |
7085 | " set-property UNIT PROPERTY=VALUE... Sets one or more properties of a unit\n" | |
7086 | " help PATTERN...|PID... Show manual for one or more units\n" | |
7087 | " reset-failed [PATTERN...] Reset failed state for all, one, or more\n" | |
7088 | " units\n" | |
7089 | " list-dependencies [UNIT] Recursively show units which are required\n" | |
7090 | " or wanted by this unit or by which this\n" | |
7091 | " unit is required or wanted\n\n" | |
34c4b47b | 7092 | "Unit File Commands:\n" |
fbf3283b ZJS |
7093 | " list-unit-files [PATTERN...] List installed unit files\n" |
7094 | " enable [UNIT...|PATH...] Enable one or more unit files\n" | |
7095 | " disable UNIT... Disable one or more unit files\n" | |
7096 | " reenable UNIT... Reenable one or more unit files\n" | |
7097 | " preset UNIT... Enable/disable one or more unit files\n" | |
7098 | " based on preset configuration\n" | |
7099 | " preset-all Enable/disable all unit files based on\n" | |
7100 | " preset configuration\n" | |
7101 | " is-enabled UNIT... Check whether unit files are enabled\n" | |
7102 | " mask UNIT... Mask one or more units\n" | |
7103 | " unmask UNIT... Unmask one or more units\n" | |
7104 | " link PATH... Link one or more units files into\n" | |
7105 | " the search path\n" | |
7106 | " revert UNIT... Revert one or more unit files to vendor\n" | |
7107 | " version\n" | |
7108 | " add-wants TARGET UNIT... Add 'Wants' dependency for the target\n" | |
7109 | " on specified one or more units\n" | |
7110 | " add-requires TARGET UNIT... Add 'Requires' dependency for the target\n" | |
7111 | " on specified one or more units\n" | |
7112 | " edit UNIT... Edit one or more unit files\n" | |
7113 | " get-default Get the name of the default target\n" | |
7114 | " set-default TARGET Set the default target\n\n" | |
0d292f5e | 7115 | "Machine Commands:\n" |
fbf3283b | 7116 | " list-machines [PATTERN...] List local containers and host\n\n" |
34c4b47b | 7117 | "Job Commands:\n" |
fbf3283b ZJS |
7118 | " list-jobs [PATTERN...] List jobs\n" |
7119 | " cancel [JOB...] Cancel all, one, or more jobs\n\n" | |
34c4b47b | 7120 | "Environment Commands:\n" |
fbf3283b | 7121 | " show-environment Dump environment\n" |
135775c1 ZJS |
7122 | " set-environment VARIABLE=VALUE... Set one or more environment variables\n" |
7123 | " unset-environment VARIABLE... Unset one or more environment variables\n" | |
7124 | " import-environment [VARIABLE...] Import all or some environment variables\n\n" | |
34c4b47b | 7125 | "Manager Lifecycle Commands:\n" |
fbf3283b ZJS |
7126 | " daemon-reload Reload systemd manager configuration\n" |
7127 | " daemon-reexec Reexecute systemd manager\n\n" | |
34c4b47b | 7128 | "System Commands:\n" |
fbf3283b ZJS |
7129 | " is-system-running Check whether system is fully running\n" |
7130 | " default Enter system default mode\n" | |
7131 | " rescue Enter system rescue mode\n" | |
7132 | " emergency Enter system emergency mode\n" | |
7133 | " halt Shut down and halt the system\n" | |
7134 | " poweroff Shut down and power-off the system\n" | |
7135 | " reboot [ARG] Shut down and reboot the system\n" | |
7136 | " kexec Shut down and reboot the system with kexec\n" | |
7137 | " exit [EXIT_CODE] Request user instance or container exit\n" | |
7138 | " switch-root ROOT [INIT] Change to a different root file system\n" | |
7139 | " suspend Suspend the system\n" | |
7140 | " hibernate Hibernate the system\n" | |
c58493c0 | 7141 | " hybrid-sleep Hibernate and suspend the system\n" |
e68c79db | 7142 | " suspend-then-hibernate Suspend the system, wake after a period of\n" |
c58493c0 | 7143 | " time and put it into hibernate\n", |
5b6319dc | 7144 | program_invocation_short_name); |
7e4249b9 LP |
7145 | } |
7146 | ||
601185b4 | 7147 | static void halt_help(void) { |
37185ec8 | 7148 | printf("%s [OPTIONS...]%s\n\n" |
e4b61340 LP |
7149 | "%s the system.\n\n" |
7150 | " --help Show this help\n" | |
7151 | " --halt Halt the machine\n" | |
7152 | " -p --poweroff Switch off the machine\n" | |
7153 | " --reboot Reboot the machine\n" | |
2e33c433 LP |
7154 | " -f --force Force immediate halt/power-off/reboot\n" |
7155 | " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n" | |
e4b61340 | 7156 | " -d --no-wtmp Don't write wtmp record\n" |
2e33c433 | 7157 | " --no-wall Don't send wall message before halt/power-off/reboot\n", |
e4b61340 | 7158 | program_invocation_short_name, |
37185ec8 | 7159 | arg_action == ACTION_REBOOT ? " [ARG]" : "", |
e4b61340 LP |
7160 | arg_action == ACTION_REBOOT ? "Reboot" : |
7161 | arg_action == ACTION_POWEROFF ? "Power off" : | |
7162 | "Halt"); | |
e4b61340 LP |
7163 | } |
7164 | ||
601185b4 | 7165 | static void shutdown_help(void) { |
08e4b1c5 | 7166 | printf("%s [OPTIONS...] [TIME] [WALL...]\n\n" |
e4b61340 LP |
7167 | "Shut down the system.\n\n" |
7168 | " --help Show this help\n" | |
7169 | " -H --halt Halt the machine\n" | |
7170 | " -P --poweroff Power-off the machine\n" | |
7171 | " -r --reboot Reboot the machine\n" | |
386da858 | 7172 | " -h Equivalent to --poweroff, overridden by --halt\n" |
2e33c433 | 7173 | " -k Don't halt/power-off/reboot, just send warnings\n" |
f6144808 | 7174 | " --no-wall Don't send wall message before halt/power-off/reboot\n" |
f6144808 | 7175 | " -c Cancel a pending shutdown\n", |
e4b61340 | 7176 | program_invocation_short_name); |
e4b61340 LP |
7177 | } |
7178 | ||
601185b4 | 7179 | static void telinit_help(void) { |
2e33c433 | 7180 | printf("%s [OPTIONS...] {COMMAND}\n\n" |
514f4ef5 LP |
7181 | "Send control commands to the init daemon.\n\n" |
7182 | " --help Show this help\n" | |
2e33c433 | 7183 | " --no-wall Don't send wall message before halt/power-off/reboot\n\n" |
e4b61340 LP |
7184 | "Commands:\n" |
7185 | " 0 Power-off the machine\n" | |
7186 | " 6 Reboot the machine\n" | |
514f4ef5 LP |
7187 | " 2, 3, 4, 5 Start runlevelX.target unit\n" |
7188 | " 1, s, S Enter rescue mode\n" | |
7189 | " q, Q Reload init daemon configuration\n" | |
7190 | " u, U Reexecute init daemon\n", | |
e4b61340 | 7191 | program_invocation_short_name); |
e4b61340 LP |
7192 | } |
7193 | ||
601185b4 | 7194 | static void runlevel_help(void) { |
2e33c433 | 7195 | printf("%s [OPTIONS...]\n\n" |
e4b61340 LP |
7196 | "Prints the previous and current runlevel of the init system.\n\n" |
7197 | " --help Show this help\n", | |
7198 | program_invocation_short_name); | |
e4b61340 LP |
7199 | } |
7200 | ||
b93312f5 | 7201 | static void help_types(void) { |
b93312f5 ZJS |
7202 | if (!arg_no_legend) |
7203 | puts("Available unit types:"); | |
5c828e66 LP |
7204 | |
7205 | DUMP_STRING_TABLE(unit_type, UnitType, _UNIT_TYPE_MAX); | |
e16972e6 ZJS |
7206 | } |
7207 | ||
7208 | static void help_states(void) { | |
e16972e6 ZJS |
7209 | if (!arg_no_legend) |
7210 | puts("Available unit load states:"); | |
5c828e66 | 7211 | DUMP_STRING_TABLE(unit_load_state, UnitLoadState, _UNIT_LOAD_STATE_MAX); |
e16972e6 ZJS |
7212 | |
7213 | if (!arg_no_legend) | |
7214 | puts("\nAvailable unit active states:"); | |
5c828e66 | 7215 | DUMP_STRING_TABLE(unit_active_state, UnitActiveState, _UNIT_ACTIVE_STATE_MAX); |
7e55de3b ZJS |
7216 | |
7217 | if (!arg_no_legend) | |
7218 | puts("\nAvailable automount unit substates:"); | |
5c828e66 | 7219 | DUMP_STRING_TABLE(automount_state, AutomountState, _AUTOMOUNT_STATE_MAX); |
7e55de3b | 7220 | |
7e55de3b ZJS |
7221 | if (!arg_no_legend) |
7222 | puts("\nAvailable device unit substates:"); | |
5c828e66 | 7223 | DUMP_STRING_TABLE(device_state, DeviceState, _DEVICE_STATE_MAX); |
7e55de3b ZJS |
7224 | |
7225 | if (!arg_no_legend) | |
7226 | puts("\nAvailable mount unit substates:"); | |
5c828e66 | 7227 | DUMP_STRING_TABLE(mount_state, MountState, _MOUNT_STATE_MAX); |
7e55de3b ZJS |
7228 | |
7229 | if (!arg_no_legend) | |
7230 | puts("\nAvailable path unit substates:"); | |
5c828e66 | 7231 | DUMP_STRING_TABLE(path_state, PathState, _PATH_STATE_MAX); |
7e55de3b ZJS |
7232 | |
7233 | if (!arg_no_legend) | |
7234 | puts("\nAvailable scope unit substates:"); | |
5c828e66 | 7235 | DUMP_STRING_TABLE(scope_state, ScopeState, _SCOPE_STATE_MAX); |
7e55de3b ZJS |
7236 | |
7237 | if (!arg_no_legend) | |
7238 | puts("\nAvailable service unit substates:"); | |
5c828e66 | 7239 | DUMP_STRING_TABLE(service_state, ServiceState, _SERVICE_STATE_MAX); |
7e55de3b ZJS |
7240 | |
7241 | if (!arg_no_legend) | |
7242 | puts("\nAvailable slice unit substates:"); | |
5c828e66 | 7243 | DUMP_STRING_TABLE(slice_state, SliceState, _SLICE_STATE_MAX); |
7e55de3b | 7244 | |
7e55de3b ZJS |
7245 | if (!arg_no_legend) |
7246 | puts("\nAvailable socket unit substates:"); | |
5c828e66 | 7247 | DUMP_STRING_TABLE(socket_state, SocketState, _SOCKET_STATE_MAX); |
7e55de3b ZJS |
7248 | |
7249 | if (!arg_no_legend) | |
7250 | puts("\nAvailable swap unit substates:"); | |
5c828e66 | 7251 | DUMP_STRING_TABLE(swap_state, SwapState, _SWAP_STATE_MAX); |
7e55de3b ZJS |
7252 | |
7253 | if (!arg_no_legend) | |
7254 | puts("\nAvailable target unit substates:"); | |
5c828e66 | 7255 | DUMP_STRING_TABLE(target_state, TargetState, _TARGET_STATE_MAX); |
7e55de3b ZJS |
7256 | |
7257 | if (!arg_no_legend) | |
7258 | puts("\nAvailable timer unit substates:"); | |
5c828e66 | 7259 | DUMP_STRING_TABLE(timer_state, TimerState, _TIMER_STATE_MAX); |
45c0c61d ZJS |
7260 | } |
7261 | ||
e4b61340 | 7262 | static int systemctl_parse_argv(int argc, char *argv[]) { |
7e4249b9 LP |
7263 | |
7264 | enum { | |
90d473a1 | 7265 | ARG_FAIL = 0x100, |
afba4199 ZJS |
7266 | ARG_REVERSE, |
7267 | ARG_AFTER, | |
7268 | ARG_BEFORE, | |
1ae17672 | 7269 | ARG_DRY_RUN, |
991f2a39 | 7270 | ARG_SHOW_TYPES, |
23ade460 | 7271 | ARG_IRREVERSIBLE, |
e67c3609 | 7272 | ARG_IGNORE_DEPENDENCIES, |
4f9a9105 | 7273 | ARG_VALUE, |
35df8f27 | 7274 | ARG_VERSION, |
af2d49f7 | 7275 | ARG_USER, |
7e4249b9 | 7276 | ARG_SYSTEM, |
ee5762e3 | 7277 | ARG_GLOBAL, |
6e905d93 | 7278 | ARG_NO_BLOCK, |
ebed32bf | 7279 | ARG_NO_LEGEND, |
611efaac | 7280 | ARG_NO_PAGER, |
4445a875 | 7281 | ARG_NO_WALL, |
be394c48 | 7282 | ARG_ROOT, |
ee5762e3 | 7283 | ARG_NO_RELOAD, |
501fc174 | 7284 | ARG_KILL_WHO, |
30732560 | 7285 | ARG_NO_ASK_PASSWORD, |
729e3769 | 7286 | ARG_FAILED, |
df50185b | 7287 | ARG_RUNTIME, |
9b9b3d36 | 7288 | ARG_PLAIN, |
4dc5b821 | 7289 | ARG_STATE, |
d309c1c3 LP |
7290 | ARG_JOB_MODE, |
7291 | ARG_PRESET_MODE, | |
5bdf2243 | 7292 | ARG_FIRMWARE_SETUP, |
57ab2eab | 7293 | ARG_NOW, |
9ef15026 | 7294 | ARG_MESSAGE, |
93a08841 | 7295 | ARG_WAIT, |
7e4249b9 LP |
7296 | }; |
7297 | ||
7298 | static const struct option options[] = { | |
9ea9d4cf LP |
7299 | { "help", no_argument, NULL, 'h' }, |
7300 | { "version", no_argument, NULL, ARG_VERSION }, | |
7301 | { "type", required_argument, NULL, 't' }, | |
7302 | { "property", required_argument, NULL, 'p' }, | |
7303 | { "all", no_argument, NULL, 'a' }, | |
7304 | { "reverse", no_argument, NULL, ARG_REVERSE }, | |
7305 | { "after", no_argument, NULL, ARG_AFTER }, | |
7306 | { "before", no_argument, NULL, ARG_BEFORE }, | |
7307 | { "show-types", no_argument, NULL, ARG_SHOW_TYPES }, | |
7308 | { "failed", no_argument, NULL, ARG_FAILED }, /* compatibility only */ | |
7309 | { "full", no_argument, NULL, 'l' }, | |
4dc5b821 LP |
7310 | { "job-mode", required_argument, NULL, ARG_JOB_MODE }, |
7311 | { "fail", no_argument, NULL, ARG_FAIL }, /* compatibility only */ | |
7312 | { "irreversible", no_argument, NULL, ARG_IRREVERSIBLE }, /* compatibility only */ | |
7313 | { "ignore-dependencies", no_argument, NULL, ARG_IGNORE_DEPENDENCIES }, /* compatibility only */ | |
9ea9d4cf | 7314 | { "ignore-inhibitors", no_argument, NULL, 'i' }, |
4f9a9105 | 7315 | { "value", no_argument, NULL, ARG_VALUE }, |
9ea9d4cf LP |
7316 | { "user", no_argument, NULL, ARG_USER }, |
7317 | { "system", no_argument, NULL, ARG_SYSTEM }, | |
7318 | { "global", no_argument, NULL, ARG_GLOBAL }, | |
93a08841 | 7319 | { "wait", no_argument, NULL, ARG_WAIT }, |
9ea9d4cf LP |
7320 | { "no-block", no_argument, NULL, ARG_NO_BLOCK }, |
7321 | { "no-legend", no_argument, NULL, ARG_NO_LEGEND }, | |
7322 | { "no-pager", no_argument, NULL, ARG_NO_PAGER }, | |
7323 | { "no-wall", no_argument, NULL, ARG_NO_WALL }, | |
1ae17672 | 7324 | { "dry-run", no_argument, NULL, ARG_DRY_RUN }, |
9ea9d4cf LP |
7325 | { "quiet", no_argument, NULL, 'q' }, |
7326 | { "root", required_argument, NULL, ARG_ROOT }, | |
68da321f | 7327 | { "force", no_argument, NULL, 'f' }, |
9ea9d4cf LP |
7328 | { "no-reload", no_argument, NULL, ARG_NO_RELOAD }, |
7329 | { "kill-who", required_argument, NULL, ARG_KILL_WHO }, | |
7330 | { "signal", required_argument, NULL, 's' }, | |
7331 | { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD }, | |
7332 | { "host", required_argument, NULL, 'H' }, | |
f459b602 | 7333 | { "machine", required_argument, NULL, 'M' }, |
9ea9d4cf LP |
7334 | { "runtime", no_argument, NULL, ARG_RUNTIME }, |
7335 | { "lines", required_argument, NULL, 'n' }, | |
7336 | { "output", required_argument, NULL, 'o' }, | |
7337 | { "plain", no_argument, NULL, ARG_PLAIN }, | |
7338 | { "state", required_argument, NULL, ARG_STATE }, | |
1238ee09 | 7339 | { "recursive", no_argument, NULL, 'r' }, |
d309c1c3 | 7340 | { "preset-mode", required_argument, NULL, ARG_PRESET_MODE }, |
5bdf2243 | 7341 | { "firmware-setup", no_argument, NULL, ARG_FIRMWARE_SETUP }, |
57ab2eab | 7342 | { "now", no_argument, NULL, ARG_NOW }, |
9ef15026 | 7343 | { "message", required_argument, NULL, ARG_MESSAGE }, |
eb9da376 | 7344 | {} |
7e4249b9 LP |
7345 | }; |
7346 | ||
5ab22f33 | 7347 | const char *p; |
0f03c2a4 | 7348 | int c, r; |
7e4249b9 | 7349 | |
e4b61340 | 7350 | assert(argc >= 0); |
7e4249b9 LP |
7351 | assert(argv); |
7352 | ||
16f017fa IS |
7353 | /* we default to allowing interactive authorization only in systemctl (not in the legacy commands) */ |
7354 | arg_ask_password = true; | |
7355 | ||
601185b4 | 7356 | while ((c = getopt_long(argc, argv, "ht:p:alqfs:H:M:n:o:ir", options, NULL)) >= 0) |
7e4249b9 LP |
7357 | |
7358 | switch (c) { | |
7359 | ||
7360 | case 'h': | |
601185b4 ZJS |
7361 | systemctl_help(); |
7362 | return 0; | |
35df8f27 LP |
7363 | |
7364 | case ARG_VERSION: | |
3f6fd1ba | 7365 | return version(); |
7e4249b9 | 7366 | |
20b3f379 | 7367 | case 't': { |
bf409580 | 7368 | if (isempty(optarg)) { |
10ab1831 | 7369 | log_error("--type= requires arguments."); |
bf409580 TA |
7370 | return -EINVAL; |
7371 | } | |
45c0c61d | 7372 | |
c58bd76a | 7373 | for (p = optarg;;) { |
5ab22f33 | 7374 | _cleanup_free_ char *type = NULL; |
20b3f379 | 7375 | |
5ab22f33 SS |
7376 | r = extract_first_word(&p, &type, ",", 0); |
7377 | if (r < 0) | |
7378 | return log_error_errno(r, "Failed to parse type: %s", optarg); | |
5ab22f33 SS |
7379 | if (r == 0) |
7380 | break; | |
20b3f379 ZJS |
7381 | |
7382 | if (streq(type, "help")) { | |
7383 | help_types(); | |
7384 | return 0; | |
7385 | } | |
7386 | ||
7387 | if (unit_type_from_string(type) >= 0) { | |
7e9d36e0 | 7388 | if (strv_push(&arg_types, type) < 0) |
20b3f379 ZJS |
7389 | return log_oom(); |
7390 | type = NULL; | |
7391 | continue; | |
7392 | } | |
7393 | ||
9b9b3d36 MW |
7394 | /* It's much nicer to use --state= for |
7395 | * load states, but let's support this | |
7396 | * in --types= too for compatibility | |
7397 | * with old versions */ | |
7e9d36e0 | 7398 | if (unit_load_state_from_string(type) >= 0) { |
9b9b3d36 | 7399 | if (strv_push(&arg_states, type) < 0) |
20b3f379 ZJS |
7400 | return log_oom(); |
7401 | type = NULL; | |
7402 | continue; | |
7403 | } | |
7404 | ||
ab06eef8 | 7405 | log_error("Unknown unit type or load state '%s'.", type); |
20b3f379 ZJS |
7406 | log_info("Use -t help to see a list of allowed values."); |
7407 | return -EINVAL; | |
c147dc42 | 7408 | } |
20b3f379 ZJS |
7409 | |
7410 | break; | |
7411 | } | |
7412 | ||
ea4a240d | 7413 | case 'p': { |
033a842c ZJS |
7414 | /* Make sure that if the empty property list |
7415 | was specified, we won't show any properties. */ | |
20b3f379 | 7416 | if (isempty(optarg) && !arg_properties) { |
cbc9fbd1 | 7417 | arg_properties = new0(char*, 1); |
20b3f379 ZJS |
7418 | if (!arg_properties) |
7419 | return log_oom(); | |
c58bd76a ZJS |
7420 | } else |
7421 | for (p = optarg;;) { | |
5ab22f33 | 7422 | _cleanup_free_ char *prop = NULL; |
033a842c | 7423 | |
5ab22f33 SS |
7424 | r = extract_first_word(&p, &prop, ",", 0); |
7425 | if (r < 0) | |
7426 | return log_error_errno(r, "Failed to parse property: %s", optarg); | |
5ab22f33 SS |
7427 | if (r == 0) |
7428 | break; | |
ea4a240d | 7429 | |
5ab22f33 | 7430 | if (strv_push(&arg_properties, prop) < 0) |
20b3f379 | 7431 | return log_oom(); |
5ab22f33 SS |
7432 | |
7433 | prop = NULL; | |
20b3f379 | 7434 | } |
48220598 LP |
7435 | |
7436 | /* If the user asked for a particular | |
7437 | * property, show it to him, even if it is | |
7438 | * empty. */ | |
7439 | arg_all = true; | |
033a842c | 7440 | |
48220598 | 7441 | break; |
ea4a240d | 7442 | } |
48220598 | 7443 | |
7e4249b9 LP |
7444 | case 'a': |
7445 | arg_all = true; | |
7446 | break; | |
7447 | ||
afba4199 ZJS |
7448 | case ARG_REVERSE: |
7449 | arg_dependency = DEPENDENCY_REVERSE; | |
7450 | break; | |
7451 | ||
7452 | case ARG_AFTER: | |
7453 | arg_dependency = DEPENDENCY_AFTER; | |
82948f6c | 7454 | arg_jobs_after = true; |
afba4199 ZJS |
7455 | break; |
7456 | ||
7457 | case ARG_BEFORE: | |
7458 | arg_dependency = DEPENDENCY_BEFORE; | |
82948f6c | 7459 | arg_jobs_before = true; |
afba4199 ZJS |
7460 | break; |
7461 | ||
991f2a39 ZJS |
7462 | case ARG_SHOW_TYPES: |
7463 | arg_show_types = true; | |
7464 | break; | |
7465 | ||
4f9a9105 ZJS |
7466 | case ARG_VALUE: |
7467 | arg_value = true; | |
7468 | break; | |
7469 | ||
4dc5b821 LP |
7470 | case ARG_JOB_MODE: |
7471 | arg_job_mode = optarg; | |
7472 | break; | |
7473 | ||
90d473a1 | 7474 | case ARG_FAIL: |
e67c3609 LP |
7475 | arg_job_mode = "fail"; |
7476 | break; | |
7477 | ||
23ade460 MS |
7478 | case ARG_IRREVERSIBLE: |
7479 | arg_job_mode = "replace-irreversibly"; | |
7480 | break; | |
7481 | ||
e67c3609 LP |
7482 | case ARG_IGNORE_DEPENDENCIES: |
7483 | arg_job_mode = "ignore-dependencies"; | |
7e4249b9 LP |
7484 | break; |
7485 | ||
af2d49f7 | 7486 | case ARG_USER: |
729e3769 | 7487 | arg_scope = UNIT_FILE_USER; |
7e4249b9 LP |
7488 | break; |
7489 | ||
7490 | case ARG_SYSTEM: | |
729e3769 LP |
7491 | arg_scope = UNIT_FILE_SYSTEM; |
7492 | break; | |
7493 | ||
7494 | case ARG_GLOBAL: | |
7495 | arg_scope = UNIT_FILE_GLOBAL; | |
7e4249b9 LP |
7496 | break; |
7497 | ||
93a08841 MP |
7498 | case ARG_WAIT: |
7499 | arg_wait = true; | |
7500 | break; | |
7501 | ||
6e905d93 LP |
7502 | case ARG_NO_BLOCK: |
7503 | arg_no_block = true; | |
7e4249b9 LP |
7504 | break; |
7505 | ||
ebed32bf MS |
7506 | case ARG_NO_LEGEND: |
7507 | arg_no_legend = true; | |
7508 | break; | |
7509 | ||
611efaac LP |
7510 | case ARG_NO_PAGER: |
7511 | arg_no_pager = true; | |
7512 | break; | |
0736af98 | 7513 | |
514f4ef5 LP |
7514 | case ARG_NO_WALL: |
7515 | arg_no_wall = true; | |
7516 | break; | |
7517 | ||
be394c48 | 7518 | case ARG_ROOT: |
bac75eb3 | 7519 | r = parse_path_argument_and_warn(optarg, false, &arg_root); |
0f03c2a4 LP |
7520 | if (r < 0) |
7521 | return r; | |
be394c48 FC |
7522 | break; |
7523 | ||
98a6e132 | 7524 | case 'l': |
8fe914ec LP |
7525 | arg_full = true; |
7526 | break; | |
7527 | ||
30732560 | 7528 | case ARG_FAILED: |
9b9b3d36 MW |
7529 | if (strv_extend(&arg_states, "failed") < 0) |
7530 | return log_oom(); | |
7531 | ||
30732560 LP |
7532 | break; |
7533 | ||
1ae17672 ZJS |
7534 | case ARG_DRY_RUN: |
7535 | arg_dry_run = true; | |
7536 | break; | |
7537 | ||
0183528f LP |
7538 | case 'q': |
7539 | arg_quiet = true; | |
7540 | break; | |
7541 | ||
b4f27ccc | 7542 | case 'f': |
313cefa1 | 7543 | arg_force++; |
ee5762e3 LP |
7544 | break; |
7545 | ||
7546 | case ARG_NO_RELOAD: | |
7547 | arg_no_reload = true; | |
7548 | break; | |
7549 | ||
8a0867d6 LP |
7550 | case ARG_KILL_WHO: |
7551 | arg_kill_who = optarg; | |
7552 | break; | |
7553 | ||
8a0867d6 | 7554 | case 's': |
5c828e66 LP |
7555 | if (streq(optarg, "help")) { |
7556 | DUMP_STRING_TABLE(signal, int, _NSIG); | |
7557 | return 0; | |
7558 | } | |
7559 | ||
29a3db75 | 7560 | arg_signal = signal_from_string(optarg); |
691395d8 | 7561 | if (arg_signal < 0) { |
8a0867d6 LP |
7562 | log_error("Failed to parse signal string %s.", optarg); |
7563 | return -EINVAL; | |
7564 | } | |
7565 | break; | |
7566 | ||
501fc174 LP |
7567 | case ARG_NO_ASK_PASSWORD: |
7568 | arg_ask_password = false; | |
7569 | break; | |
7570 | ||
f459b602 MAP |
7571 | case 'H': |
7572 | arg_transport = BUS_TRANSPORT_REMOTE; | |
7573 | arg_host = optarg; | |
a8f11321 LP |
7574 | break; |
7575 | ||
f459b602 | 7576 | case 'M': |
de33fc62 | 7577 | arg_transport = BUS_TRANSPORT_MACHINE; |
f459b602 | 7578 | arg_host = optarg; |
a8f11321 LP |
7579 | break; |
7580 | ||
729e3769 LP |
7581 | case ARG_RUNTIME: |
7582 | arg_runtime = true; | |
7583 | break; | |
7584 | ||
df50185b LP |
7585 | case 'n': |
7586 | if (safe_atou(optarg, &arg_lines) < 0) { | |
7587 | log_error("Failed to parse lines '%s'", optarg); | |
7588 | return -EINVAL; | |
7589 | } | |
7590 | break; | |
7591 | ||
df50185b | 7592 | case 'o': |
5c828e66 LP |
7593 | if (streq(optarg, "help")) { |
7594 | DUMP_STRING_TABLE(output_mode, OutputMode, _OUTPUT_MODE_MAX); | |
7595 | return 0; | |
7596 | } | |
7597 | ||
df50185b LP |
7598 | arg_output = output_mode_from_string(optarg); |
7599 | if (arg_output < 0) { | |
7600 | log_error("Unknown output '%s'.", optarg); | |
7601 | return -EINVAL; | |
7602 | } | |
7603 | break; | |
7604 | ||
b37844d3 LP |
7605 | case 'i': |
7606 | arg_ignore_inhibitors = true; | |
7607 | break; | |
7608 | ||
5d0c05e5 LN |
7609 | case ARG_PLAIN: |
7610 | arg_plain = true; | |
7611 | break; | |
7612 | ||
5bdf2243 JJ |
7613 | case ARG_FIRMWARE_SETUP: |
7614 | arg_firmware_setup = true; | |
7615 | break; | |
7616 | ||
9b9b3d36 | 7617 | case ARG_STATE: { |
bf409580 | 7618 | if (isempty(optarg)) { |
10ab1831 | 7619 | log_error("--state= requires arguments."); |
bf409580 TA |
7620 | return -EINVAL; |
7621 | } | |
9b9b3d36 | 7622 | |
c58bd76a | 7623 | for (p = optarg;;) { |
bc6c18fe | 7624 | _cleanup_free_ char *s = NULL; |
9b9b3d36 | 7625 | |
5ab22f33 SS |
7626 | r = extract_first_word(&p, &s, ",", 0); |
7627 | if (r < 0) | |
6862111e | 7628 | return log_error_errno(r, "Failed to parse state: %s", optarg); |
5ab22f33 SS |
7629 | if (r == 0) |
7630 | break; | |
9b9b3d36 | 7631 | |
e16972e6 ZJS |
7632 | if (streq(s, "help")) { |
7633 | help_states(); | |
7634 | return 0; | |
7635 | } | |
7636 | ||
7e9d36e0 | 7637 | if (strv_push(&arg_states, s) < 0) |
9b9b3d36 | 7638 | return log_oom(); |
7e9d36e0 LP |
7639 | |
7640 | s = NULL; | |
9b9b3d36 MW |
7641 | } |
7642 | break; | |
7643 | } | |
7644 | ||
1238ee09 LP |
7645 | case 'r': |
7646 | if (geteuid() != 0) { | |
f1721625 | 7647 | log_error("--recursive requires root privileges."); |
1238ee09 LP |
7648 | return -EPERM; |
7649 | } | |
7650 | ||
7651 | arg_recursive = true; | |
7652 | break; | |
7653 | ||
d309c1c3 | 7654 | case ARG_PRESET_MODE: |
5c828e66 LP |
7655 | if (streq(optarg, "help")) { |
7656 | DUMP_STRING_TABLE(unit_file_preset_mode, UnitFilePresetMode, _UNIT_FILE_PRESET_MAX); | |
7657 | return 0; | |
7658 | } | |
d309c1c3 LP |
7659 | |
7660 | arg_preset_mode = unit_file_preset_mode_from_string(optarg); | |
7661 | if (arg_preset_mode < 0) { | |
7662 | log_error("Failed to parse preset mode: %s.", optarg); | |
7663 | return -EINVAL; | |
7664 | } | |
7665 | ||
7666 | break; | |
7667 | ||
57ab2eab JS |
7668 | case ARG_NOW: |
7669 | arg_now = true; | |
7670 | break; | |
7671 | ||
9ef15026 JS |
7672 | case ARG_MESSAGE: |
7673 | if (strv_extend(&arg_wall, optarg) < 0) | |
7674 | return log_oom(); | |
7675 | break; | |
7676 | ||
7e4249b9 LP |
7677 | case '?': |
7678 | return -EINVAL; | |
7679 | ||
7680 | default: | |
eb9da376 | 7681 | assert_not_reached("Unhandled option"); |
7e4249b9 | 7682 | } |
7e4249b9 | 7683 | |
f459b602 | 7684 | if (arg_transport != BUS_TRANSPORT_LOCAL && arg_scope != UNIT_FILE_SYSTEM) { |
a8f11321 LP |
7685 | log_error("Cannot access user instance remotely."); |
7686 | return -EINVAL; | |
7687 | } | |
7688 | ||
93a08841 MP |
7689 | if (arg_wait && arg_no_block) { |
7690 | log_error("--wait may not be combined with --no-block."); | |
7691 | return -EINVAL; | |
7692 | } | |
7693 | ||
7e4249b9 LP |
7694 | return 1; |
7695 | } | |
7696 | ||
e4b61340 LP |
7697 | static int halt_parse_argv(int argc, char *argv[]) { |
7698 | ||
7699 | enum { | |
7700 | ARG_HELP = 0x100, | |
7701 | ARG_HALT, | |
514f4ef5 LP |
7702 | ARG_REBOOT, |
7703 | ARG_NO_WALL | |
e4b61340 LP |
7704 | }; |
7705 | ||
7706 | static const struct option options[] = { | |
7707 | { "help", no_argument, NULL, ARG_HELP }, | |
7708 | { "halt", no_argument, NULL, ARG_HALT }, | |
7709 | { "poweroff", no_argument, NULL, 'p' }, | |
7710 | { "reboot", no_argument, NULL, ARG_REBOOT }, | |
7711 | { "force", no_argument, NULL, 'f' }, | |
7712 | { "wtmp-only", no_argument, NULL, 'w' }, | |
7713 | { "no-wtmp", no_argument, NULL, 'd' }, | |
f3f054f0 | 7714 | { "no-sync", no_argument, NULL, 'n' }, |
514f4ef5 | 7715 | { "no-wall", no_argument, NULL, ARG_NO_WALL }, |
eb9da376 | 7716 | {} |
e4b61340 LP |
7717 | }; |
7718 | ||
37185ec8 | 7719 | int c, r, runlevel; |
e4b61340 LP |
7720 | |
7721 | assert(argc >= 0); | |
7722 | assert(argv); | |
7723 | ||
7724 | if (utmp_get_runlevel(&runlevel, NULL) >= 0) | |
4c701096 | 7725 | if (IN_SET(runlevel, '0', '6')) |
65491fd8 | 7726 | arg_force = 2; |
e4b61340 | 7727 | |
601185b4 | 7728 | while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) |
e4b61340 LP |
7729 | switch (c) { |
7730 | ||
7731 | case ARG_HELP: | |
601185b4 ZJS |
7732 | halt_help(); |
7733 | return 0; | |
e4b61340 LP |
7734 | |
7735 | case ARG_HALT: | |
7736 | arg_action = ACTION_HALT; | |
7737 | break; | |
7738 | ||
7739 | case 'p': | |
a042efad MS |
7740 | if (arg_action != ACTION_REBOOT) |
7741 | arg_action = ACTION_POWEROFF; | |
e4b61340 LP |
7742 | break; |
7743 | ||
7744 | case ARG_REBOOT: | |
7745 | arg_action = ACTION_REBOOT; | |
7746 | break; | |
7747 | ||
7748 | case 'f': | |
65491fd8 | 7749 | arg_force = 2; |
e4b61340 LP |
7750 | break; |
7751 | ||
7752 | case 'w': | |
1ae17672 | 7753 | arg_dry_run = true; |
e4b61340 LP |
7754 | break; |
7755 | ||
7756 | case 'd': | |
7757 | arg_no_wtmp = true; | |
7758 | break; | |
7759 | ||
f3f054f0 TB |
7760 | case 'n': |
7761 | arg_no_sync = true; | |
7762 | break; | |
7763 | ||
514f4ef5 LP |
7764 | case ARG_NO_WALL: |
7765 | arg_no_wall = true; | |
7766 | break; | |
7767 | ||
e4b61340 LP |
7768 | case 'i': |
7769 | case 'h': | |
7770 | /* Compatibility nops */ | |
7771 | break; | |
7772 | ||
7773 | case '?': | |
7774 | return -EINVAL; | |
7775 | ||
7776 | default: | |
eb9da376 | 7777 | assert_not_reached("Unhandled option"); |
e4b61340 | 7778 | } |
e4b61340 | 7779 | |
c5220a94 | 7780 | if (arg_action == ACTION_REBOOT && (argc == optind || argc == optind + 1)) { |
27c06cb5 | 7781 | r = update_reboot_parameter_and_warn(argc == optind + 1 ? argv[optind] : NULL); |
c5220a94 | 7782 | if (r < 0) |
37185ec8 | 7783 | return r; |
37185ec8 | 7784 | } else if (optind < argc) { |
e4b61340 LP |
7785 | log_error("Too many arguments."); |
7786 | return -EINVAL; | |
7787 | } | |
7788 | ||
7789 | return 1; | |
7790 | } | |
7791 | ||
2cc7b0a2 | 7792 | static int parse_shutdown_time_spec(const char *t, usec_t *_u) { |
f6144808 LP |
7793 | assert(t); |
7794 | assert(_u); | |
7795 | ||
7796 | if (streq(t, "now")) | |
7797 | *_u = 0; | |
1a639877 | 7798 | else if (!strchr(t, ':')) { |
f6144808 LP |
7799 | uint64_t u; |
7800 | ||
1a639877 | 7801 | if (safe_atou64(t, &u) < 0) |
f6144808 LP |
7802 | return -EINVAL; |
7803 | ||
7804 | *_u = now(CLOCK_REALTIME) + USEC_PER_MINUTE * u; | |
7805 | } else { | |
7806 | char *e = NULL; | |
7807 | long hour, minute; | |
b92bea5d | 7808 | struct tm tm = {}; |
f6144808 LP |
7809 | time_t s; |
7810 | usec_t n; | |
7811 | ||
7812 | errno = 0; | |
7813 | hour = strtol(t, &e, 10); | |
8333c77e | 7814 | if (errno > 0 || *e != ':' || hour < 0 || hour > 23) |
f6144808 LP |
7815 | return -EINVAL; |
7816 | ||
7817 | minute = strtol(e+1, &e, 10); | |
8333c77e | 7818 | if (errno > 0 || *e != 0 || minute < 0 || minute > 59) |
f6144808 LP |
7819 | return -EINVAL; |
7820 | ||
7821 | n = now(CLOCK_REALTIME); | |
08e4b1c5 LP |
7822 | s = (time_t) (n / USEC_PER_SEC); |
7823 | ||
f6144808 LP |
7824 | assert_se(localtime_r(&s, &tm)); |
7825 | ||
7826 | tm.tm_hour = (int) hour; | |
7827 | tm.tm_min = (int) minute; | |
08e4b1c5 | 7828 | tm.tm_sec = 0; |
f6144808 LP |
7829 | |
7830 | assert_se(s = mktime(&tm)); | |
7831 | ||
7832 | *_u = (usec_t) s * USEC_PER_SEC; | |
7833 | ||
7834 | while (*_u <= n) | |
7835 | *_u += USEC_PER_DAY; | |
7836 | } | |
7837 | ||
7838 | return 0; | |
7839 | } | |
7840 | ||
e4b61340 LP |
7841 | static int shutdown_parse_argv(int argc, char *argv[]) { |
7842 | ||
7843 | enum { | |
7844 | ARG_HELP = 0x100, | |
514f4ef5 | 7845 | ARG_NO_WALL |
e4b61340 LP |
7846 | }; |
7847 | ||
7848 | static const struct option options[] = { | |
7849 | { "help", no_argument, NULL, ARG_HELP }, | |
7850 | { "halt", no_argument, NULL, 'H' }, | |
7851 | { "poweroff", no_argument, NULL, 'P' }, | |
7852 | { "reboot", no_argument, NULL, 'r' }, | |
04ebb595 | 7853 | { "kexec", no_argument, NULL, 'K' }, /* not documented extension */ |
514f4ef5 | 7854 | { "no-wall", no_argument, NULL, ARG_NO_WALL }, |
eb9da376 | 7855 | {} |
e4b61340 LP |
7856 | }; |
7857 | ||
172d7abf | 7858 | char **wall = NULL; |
f6144808 | 7859 | int c, r; |
e4b61340 LP |
7860 | |
7861 | assert(argc >= 0); | |
7862 | assert(argv); | |
7863 | ||
b068c6f5 | 7864 | while ((c = getopt_long(argc, argv, "HPrhkKat:fFc", options, NULL)) >= 0) |
e4b61340 LP |
7865 | switch (c) { |
7866 | ||
7867 | case ARG_HELP: | |
601185b4 ZJS |
7868 | shutdown_help(); |
7869 | return 0; | |
e4b61340 LP |
7870 | |
7871 | case 'H': | |
7872 | arg_action = ACTION_HALT; | |
7873 | break; | |
7874 | ||
7875 | case 'P': | |
7876 | arg_action = ACTION_POWEROFF; | |
7877 | break; | |
7878 | ||
7879 | case 'r': | |
5622dde3 KS |
7880 | if (kexec_loaded()) |
7881 | arg_action = ACTION_KEXEC; | |
7882 | else | |
7883 | arg_action = ACTION_REBOOT; | |
e4b61340 LP |
7884 | break; |
7885 | ||
04ebb595 LP |
7886 | case 'K': |
7887 | arg_action = ACTION_KEXEC; | |
7888 | break; | |
7889 | ||
e4b61340 LP |
7890 | case 'h': |
7891 | if (arg_action != ACTION_HALT) | |
7892 | arg_action = ACTION_POWEROFF; | |
7893 | break; | |
7894 | ||
7895 | case 'k': | |
1ae17672 | 7896 | arg_dry_run = true; |
e4b61340 LP |
7897 | break; |
7898 | ||
514f4ef5 LP |
7899 | case ARG_NO_WALL: |
7900 | arg_no_wall = true; | |
7901 | break; | |
7902 | ||
e4b61340 | 7903 | case 'a': |
b068c6f5 | 7904 | case 't': /* Note that we also ignore any passed argument to -t, not just the -t itself */ |
75836b9d JS |
7905 | case 'f': |
7906 | case 'F': | |
e4b61340 LP |
7907 | /* Compatibility nops */ |
7908 | break; | |
7909 | ||
f6144808 LP |
7910 | case 'c': |
7911 | arg_action = ACTION_CANCEL_SHUTDOWN; | |
7912 | break; | |
7913 | ||
e4b61340 LP |
7914 | case '?': |
7915 | return -EINVAL; | |
7916 | ||
7917 | default: | |
eb9da376 | 7918 | assert_not_reached("Unhandled option"); |
e4b61340 | 7919 | } |
e4b61340 | 7920 | |
dfcc5c33 | 7921 | if (argc > optind && arg_action != ACTION_CANCEL_SHUTDOWN) { |
2cc7b0a2 | 7922 | r = parse_shutdown_time_spec(argv[optind], &arg_when); |
7e59bfcb | 7923 | if (r < 0) { |
f6144808 LP |
7924 | log_error("Failed to parse time specification: %s", argv[optind]); |
7925 | return r; | |
7926 | } | |
6b5ad000 | 7927 | } else |
08e4b1c5 | 7928 | arg_when = now(CLOCK_REALTIME) + USEC_PER_MINUTE; |
442b9094 | 7929 | |
dfcc5c33 MS |
7930 | if (argc > optind && arg_action == ACTION_CANCEL_SHUTDOWN) |
7931 | /* No time argument for shutdown cancel */ | |
172d7abf | 7932 | wall = argv + optind; |
dfcc5c33 MS |
7933 | else if (argc > optind + 1) |
7934 | /* We skip the time argument */ | |
172d7abf LP |
7935 | wall = argv + optind + 1; |
7936 | ||
7937 | if (wall) { | |
7938 | arg_wall = strv_copy(wall); | |
7939 | if (!arg_wall) | |
7940 | return log_oom(); | |
7941 | } | |
e4b61340 LP |
7942 | |
7943 | optind = argc; | |
7944 | ||
7945 | return 1; | |
e4b61340 LP |
7946 | } |
7947 | ||
7948 | static int telinit_parse_argv(int argc, char *argv[]) { | |
7949 | ||
7950 | enum { | |
7951 | ARG_HELP = 0x100, | |
514f4ef5 | 7952 | ARG_NO_WALL |
e4b61340 LP |
7953 | }; |
7954 | ||
7955 | static const struct option options[] = { | |
7956 | { "help", no_argument, NULL, ARG_HELP }, | |
514f4ef5 | 7957 | { "no-wall", no_argument, NULL, ARG_NO_WALL }, |
eb9da376 | 7958 | {} |
e4b61340 LP |
7959 | }; |
7960 | ||
7961 | static const struct { | |
7962 | char from; | |
7963 | enum action to; | |
7964 | } table[] = { | |
7965 | { '0', ACTION_POWEROFF }, | |
7966 | { '6', ACTION_REBOOT }, | |
ef2f1067 | 7967 | { '1', ACTION_RESCUE }, |
e4b61340 LP |
7968 | { '2', ACTION_RUNLEVEL2 }, |
7969 | { '3', ACTION_RUNLEVEL3 }, | |
7970 | { '4', ACTION_RUNLEVEL4 }, | |
7971 | { '5', ACTION_RUNLEVEL5 }, | |
7972 | { 's', ACTION_RESCUE }, | |
7973 | { 'S', ACTION_RESCUE }, | |
7974 | { 'q', ACTION_RELOAD }, | |
7975 | { 'Q', ACTION_RELOAD }, | |
7976 | { 'u', ACTION_REEXEC }, | |
7977 | { 'U', ACTION_REEXEC } | |
7978 | }; | |
7979 | ||
7980 | unsigned i; | |
7981 | int c; | |
7982 | ||
7983 | assert(argc >= 0); | |
7984 | assert(argv); | |
7985 | ||
601185b4 | 7986 | while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) |
e4b61340 LP |
7987 | switch (c) { |
7988 | ||
7989 | case ARG_HELP: | |
601185b4 ZJS |
7990 | telinit_help(); |
7991 | return 0; | |
e4b61340 | 7992 | |
514f4ef5 LP |
7993 | case ARG_NO_WALL: |
7994 | arg_no_wall = true; | |
7995 | break; | |
7996 | ||
e4b61340 LP |
7997 | case '?': |
7998 | return -EINVAL; | |
7999 | ||
8000 | default: | |
eb9da376 | 8001 | assert_not_reached("Unhandled option"); |
e4b61340 | 8002 | } |
e4b61340 LP |
8003 | |
8004 | if (optind >= argc) { | |
691395d8 | 8005 | log_error("%s: required argument missing.", program_invocation_short_name); |
e4b61340 LP |
8006 | return -EINVAL; |
8007 | } | |
8008 | ||
8009 | if (optind + 1 < argc) { | |
8010 | log_error("Too many arguments."); | |
8011 | return -EINVAL; | |
8012 | } | |
8013 | ||
8014 | if (strlen(argv[optind]) != 1) { | |
8015 | log_error("Expected single character argument."); | |
8016 | return -EINVAL; | |
8017 | } | |
8018 | ||
8019 | for (i = 0; i < ELEMENTSOF(table); i++) | |
8020 | if (table[i].from == argv[optind][0]) | |
8021 | break; | |
8022 | ||
8023 | if (i >= ELEMENTSOF(table)) { | |
b0193f1c | 8024 | log_error("Unknown command '%s'.", argv[optind]); |
e4b61340 LP |
8025 | return -EINVAL; |
8026 | } | |
8027 | ||
8028 | arg_action = table[i].to; | |
8029 | ||
313cefa1 | 8030 | optind++; |
e4b61340 LP |
8031 | |
8032 | return 1; | |
8033 | } | |
8034 | ||
8035 | static int runlevel_parse_argv(int argc, char *argv[]) { | |
8036 | ||
8037 | enum { | |
8038 | ARG_HELP = 0x100, | |
8039 | }; | |
8040 | ||
8041 | static const struct option options[] = { | |
8042 | { "help", no_argument, NULL, ARG_HELP }, | |
eb9da376 | 8043 | {} |
e4b61340 LP |
8044 | }; |
8045 | ||
8046 | int c; | |
8047 | ||
8048 | assert(argc >= 0); | |
8049 | assert(argv); | |
8050 | ||
601185b4 | 8051 | while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) |
e4b61340 LP |
8052 | switch (c) { |
8053 | ||
8054 | case ARG_HELP: | |
601185b4 ZJS |
8055 | runlevel_help(); |
8056 | return 0; | |
e4b61340 LP |
8057 | |
8058 | case '?': | |
8059 | return -EINVAL; | |
8060 | ||
8061 | default: | |
eb9da376 | 8062 | assert_not_reached("Unhandled option"); |
e4b61340 | 8063 | } |
e4b61340 LP |
8064 | |
8065 | if (optind < argc) { | |
8066 | log_error("Too many arguments."); | |
8067 | return -EINVAL; | |
8068 | } | |
8069 | ||
8070 | return 1; | |
8071 | } | |
8072 | ||
8073 | static int parse_argv(int argc, char *argv[]) { | |
8074 | assert(argc >= 0); | |
8075 | assert(argv); | |
8076 | ||
8077 | if (program_invocation_short_name) { | |
8078 | ||
8079 | if (strstr(program_invocation_short_name, "halt")) { | |
8080 | arg_action = ACTION_HALT; | |
8081 | return halt_parse_argv(argc, argv); | |
c54819ca | 8082 | |
e4b61340 LP |
8083 | } else if (strstr(program_invocation_short_name, "poweroff")) { |
8084 | arg_action = ACTION_POWEROFF; | |
8085 | return halt_parse_argv(argc, argv); | |
c54819ca | 8086 | |
e4b61340 | 8087 | } else if (strstr(program_invocation_short_name, "reboot")) { |
5622dde3 KS |
8088 | if (kexec_loaded()) |
8089 | arg_action = ACTION_KEXEC; | |
8090 | else | |
8091 | arg_action = ACTION_REBOOT; | |
e4b61340 | 8092 | return halt_parse_argv(argc, argv); |
c54819ca | 8093 | |
e4b61340 LP |
8094 | } else if (strstr(program_invocation_short_name, "shutdown")) { |
8095 | arg_action = ACTION_POWEROFF; | |
8096 | return shutdown_parse_argv(argc, argv); | |
c54819ca | 8097 | |
e4b61340 | 8098 | } else if (strstr(program_invocation_short_name, "init")) { |
d5ca5f11 | 8099 | |
c54819ca LP |
8100 | /* Matches invocations as "init" as well as "telinit", which are synonymous when run as PID != |
8101 | * 1 on SysV. | |
8102 | * | |
8103 | * On SysV "telinit" was the official command to communicate with PID 1, but "init" would | |
8104 | * redirect itself to "telinit" if called with PID != 1. We follow the same logic here still, | |
8105 | * though we add one level of indirection, as we implement "telinit" in "systemctl". Hence, for | |
8106 | * us if you invoke "init" you get "systemd", but it will execve() "systemctl" immediately with | |
8107 | * argv[] unmodified if PID is != 1. If you invoke "telinit" you directly get "systemctl". In | |
8108 | * both cases we shall do the same thing, which is why we do strstr(p_i_s_n, "init") here, as a | |
8109 | * quick way to match both. | |
8110 | * | |
8111 | * Also see redirect_telinit() in src/core/main.c. */ | |
8112 | ||
d5ca5f11 | 8113 | if (sd_booted() > 0) { |
f459b602 | 8114 | arg_action = _ACTION_INVALID; |
d5ca5f11 LP |
8115 | return telinit_parse_argv(argc, argv); |
8116 | } else { | |
c54819ca LP |
8117 | /* Hmm, so some other init system is running, we need to forward this request to |
8118 | * it. For now we simply guess that it is Upstart. */ | |
d5ca5f11 | 8119 | |
4ad61fd1 | 8120 | execv(TELINIT, argv); |
d5ca5f11 LP |
8121 | |
8122 | log_error("Couldn't find an alternative telinit implementation to spawn."); | |
8123 | return -EIO; | |
8124 | } | |
8125 | ||
e4b61340 LP |
8126 | } else if (strstr(program_invocation_short_name, "runlevel")) { |
8127 | arg_action = ACTION_RUNLEVEL; | |
8128 | return runlevel_parse_argv(argc, argv); | |
8129 | } | |
8130 | } | |
8131 | ||
8132 | arg_action = ACTION_SYSTEMCTL; | |
8133 | return systemctl_parse_argv(argc, argv); | |
8134 | } | |
8135 | ||
349cc4a5 | 8136 | #if HAVE_SYSV_COMPAT |
44a6b1b6 | 8137 | _pure_ static int action_to_runlevel(void) { |
eb22ac37 LP |
8138 | |
8139 | static const char table[_ACTION_MAX] = { | |
8140 | [ACTION_HALT] = '0', | |
8141 | [ACTION_POWEROFF] = '0', | |
8142 | [ACTION_REBOOT] = '6', | |
8143 | [ACTION_RUNLEVEL2] = '2', | |
8144 | [ACTION_RUNLEVEL3] = '3', | |
8145 | [ACTION_RUNLEVEL4] = '4', | |
8146 | [ACTION_RUNLEVEL5] = '5', | |
8147 | [ACTION_RESCUE] = '1' | |
8148 | }; | |
8149 | ||
78ca9099 | 8150 | assert(arg_action >= 0 && arg_action < _ACTION_MAX); |
d55ae9e6 LP |
8151 | |
8152 | return table[arg_action]; | |
8153 | } | |
d2e79673 | 8154 | #endif |
d55ae9e6 | 8155 | |
d55ae9e6 | 8156 | static int talk_initctl(void) { |
349cc4a5 | 8157 | #if HAVE_SYSV_COMPAT |
cbc9fbd1 LP |
8158 | struct init_request request = { |
8159 | .magic = INIT_MAGIC, | |
8160 | .sleeptime = 0, | |
8161 | .cmd = INIT_CMD_RUNLVL | |
8162 | }; | |
8163 | ||
7fd1b19b | 8164 | _cleanup_close_ int fd = -1; |
d55ae9e6 | 8165 | char rl; |
cbc9fbd1 | 8166 | int r; |
83c76e8c | 8167 | const char *p; |
eb22ac37 | 8168 | |
427b47c4 ZJS |
8169 | rl = action_to_runlevel(); |
8170 | if (!rl) | |
eb22ac37 LP |
8171 | return 0; |
8172 | ||
d55ae9e6 LP |
8173 | request.runlevel = rl; |
8174 | ||
83c76e8c MG |
8175 | FOREACH_STRING(p, "/run/initctl", "/dev/initctl") { |
8176 | fd = open(p, O_WRONLY|O_NONBLOCK|O_CLOEXEC|O_NOCTTY); | |
8177 | if (fd >= 0 || errno != ENOENT) | |
8178 | break; | |
8179 | } | |
427b47c4 | 8180 | if (fd < 0) { |
d55ae9e6 LP |
8181 | if (errno == ENOENT) |
8182 | return 0; | |
eb22ac37 | 8183 | |
83c76e8c | 8184 | return log_error_errno(errno, "Failed to open initctl fifo: %m"); |
d55ae9e6 | 8185 | } |
eb22ac37 | 8186 | |
553acb7b ZJS |
8187 | r = loop_write(fd, &request, sizeof(request), false); |
8188 | if (r < 0) | |
83c76e8c | 8189 | return log_error_errno(r, "Failed to write to %s: %m", p); |
eb22ac37 LP |
8190 | |
8191 | return 1; | |
eca830be LP |
8192 | #else |
8193 | return 0; | |
8194 | #endif | |
e4b61340 LP |
8195 | } |
8196 | ||
e449de87 LP |
8197 | static int systemctl_main(int argc, char *argv[]) { |
8198 | ||
8199 | static const Verb verbs[] = { | |
c56d1e2c CW |
8200 | { "list-units", VERB_ANY, VERB_ANY, VERB_DEFAULT|VERB_ONLINE_ONLY, list_units }, |
8201 | { "list-unit-files", VERB_ANY, VERB_ANY, 0, list_unit_files }, | |
8202 | { "list-sockets", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, list_sockets }, | |
8203 | { "list-timers", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, list_timers }, | |
8204 | { "list-jobs", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, list_jobs }, | |
8205 | { "list-machines", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY|VERB_MUST_BE_ROOT, list_machines }, | |
8206 | { "clear-jobs", VERB_ANY, 1, VERB_ONLINE_ONLY, trivial_method }, | |
8207 | { "cancel", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, cancel_job }, | |
8208 | { "start", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, | |
8209 | { "stop", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, | |
8210 | { "condstop", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, /* For compatibility with ALTLinux */ | |
8211 | { "reload", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, | |
8212 | { "restart", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, | |
8213 | { "try-restart", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, | |
8214 | { "reload-or-restart", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, | |
8215 | { "reload-or-try-restart", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, /* For compatbility with old systemctl <= 228 */ | |
8216 | { "try-reload-or-restart", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, | |
8217 | { "force-reload", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, /* For compatibility with SysV */ | |
8218 | { "condreload", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, /* For compatibility with ALTLinux */ | |
8219 | { "condrestart", 2, VERB_ANY, VERB_ONLINE_ONLY, start_unit }, /* For compatibility with RH */ | |
8220 | { "isolate", 2, 2, VERB_ONLINE_ONLY, start_unit }, | |
8221 | { "kill", 2, VERB_ANY, VERB_ONLINE_ONLY, kill_unit }, | |
8222 | { "is-active", 2, VERB_ANY, VERB_ONLINE_ONLY, check_unit_active }, | |
3e7e587d | 8223 | { "check", 2, VERB_ANY, VERB_ONLINE_ONLY, check_unit_active }, /* deprecated alias of is-active */ |
c56d1e2c CW |
8224 | { "is-failed", 2, VERB_ANY, VERB_ONLINE_ONLY, check_unit_failed }, |
8225 | { "show", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, show }, | |
8226 | { "cat", 2, VERB_ANY, VERB_ONLINE_ONLY, cat }, | |
8227 | { "status", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, show }, | |
8228 | { "help", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, show }, | |
8229 | { "daemon-reload", VERB_ANY, 1, VERB_ONLINE_ONLY, daemon_reload }, | |
8230 | { "daemon-reexec", VERB_ANY, 1, VERB_ONLINE_ONLY, daemon_reload }, | |
8231 | { "show-environment", VERB_ANY, 1, VERB_ONLINE_ONLY, show_environment }, | |
8232 | { "set-environment", 2, VERB_ANY, VERB_ONLINE_ONLY, set_environment }, | |
8233 | { "unset-environment", 2, VERB_ANY, VERB_ONLINE_ONLY, set_environment }, | |
8234 | { "import-environment", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, import_environment }, | |
8235 | { "halt", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, | |
8236 | { "poweroff", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, | |
8237 | { "reboot", VERB_ANY, 2, VERB_ONLINE_ONLY, start_system_special }, | |
8238 | { "kexec", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, | |
8239 | { "suspend", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, | |
8240 | { "hibernate", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, | |
8241 | { "hybrid-sleep", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, | |
e68c79db | 8242 | { "suspend-then-hibernate",VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, |
c56d1e2c CW |
8243 | { "default", VERB_ANY, 1, VERB_ONLINE_ONLY, start_special }, |
8244 | { "rescue", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, | |
8245 | { "emergency", VERB_ANY, 1, VERB_ONLINE_ONLY, start_system_special }, | |
8246 | { "exit", VERB_ANY, 2, VERB_ONLINE_ONLY, start_special }, | |
8247 | { "reset-failed", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY, reset_failed }, | |
8248 | { "enable", 2, VERB_ANY, 0, enable_unit }, | |
8249 | { "disable", 2, VERB_ANY, 0, enable_unit }, | |
8250 | { "is-enabled", 2, VERB_ANY, 0, unit_is_enabled }, | |
8251 | { "reenable", 2, VERB_ANY, 0, enable_unit }, | |
8252 | { "preset", 2, VERB_ANY, 0, enable_unit }, | |
8253 | { "preset-all", VERB_ANY, 1, 0, preset_all }, | |
8254 | { "mask", 2, VERB_ANY, 0, enable_unit }, | |
8255 | { "unmask", 2, VERB_ANY, 0, enable_unit }, | |
8256 | { "link", 2, VERB_ANY, 0, enable_unit }, | |
8257 | { "revert", 2, VERB_ANY, 0, enable_unit }, | |
8258 | { "switch-root", 2, VERB_ANY, VERB_ONLINE_ONLY, switch_root }, | |
8259 | { "list-dependencies", VERB_ANY, 2, VERB_ONLINE_ONLY, list_dependencies }, | |
8260 | { "set-default", 2, 2, 0, set_default }, | |
8261 | { "get-default", VERB_ANY, 1, 0, get_default }, | |
8262 | { "set-property", 3, VERB_ANY, VERB_ONLINE_ONLY, set_property }, | |
8263 | { "is-system-running", VERB_ANY, 1, 0, is_system_running }, | |
8264 | { "add-wants", 3, VERB_ANY, 0, add_dependency }, | |
8265 | { "add-requires", 3, VERB_ANY, 0, add_dependency }, | |
8266 | { "edit", 2, VERB_ANY, VERB_ONLINE_ONLY, edit }, | |
d08e75ed | 8267 | {} |
e449de87 | 8268 | }; |
7e4249b9 | 8269 | |
e449de87 | 8270 | return dispatch_verb(argc, argv, verbs, NULL); |
e4b61340 LP |
8271 | } |
8272 | ||
4fbd7192 | 8273 | static int reload_with_fallback(void) { |
e4b61340 | 8274 | |
4fbd7192 | 8275 | /* First, try systemd via D-Bus. */ |
e449de87 | 8276 | if (daemon_reload(0, NULL, NULL) >= 0) |
4fbd7192 | 8277 | return 0; |
e4b61340 LP |
8278 | |
8279 | /* Nothing else worked, so let's try signals */ | |
2853b60a | 8280 | assert(IN_SET(arg_action, ACTION_RELOAD, ACTION_REEXEC)); |
e4b61340 | 8281 | |
4a62c710 MS |
8282 | if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) |
8283 | return log_error_errno(errno, "kill() failed: %m"); | |
e4b61340 LP |
8284 | |
8285 | return 0; | |
8286 | } | |
8287 | ||
4fbd7192 | 8288 | static int start_with_fallback(void) { |
e4b61340 | 8289 | |
4fbd7192 | 8290 | /* First, try systemd via D-Bus. */ |
e449de87 | 8291 | if (start_unit(0, NULL, NULL) >= 0) |
4fbd7192 | 8292 | return 0; |
e4b61340 | 8293 | |
2853b60a | 8294 | /* Nothing else worked, so let's try /dev/initctl */ |
fbc43921 | 8295 | if (talk_initctl() > 0) |
48ec22bc | 8296 | return 0; |
d55ae9e6 LP |
8297 | |
8298 | log_error("Failed to talk to init daemon."); | |
8299 | return -EIO; | |
e4b61340 LP |
8300 | } |
8301 | ||
477def80 | 8302 | static int halt_now(enum action a) { |
e606bb61 | 8303 | |
c01dcddf LP |
8304 | /* The kernel will automatically flush ATA disks and suchlike on reboot(), but the file systems need to be |
8305 | * synce'd explicitly in advance. */ | |
1ae17672 | 8306 | if (!arg_no_sync && !arg_dry_run) |
f3f054f0 | 8307 | (void) sync(); |
4a3ad399 | 8308 | |
1ae17672 ZJS |
8309 | /* Make sure C-A-D is handled by the kernel from this point on... */ |
8310 | if (!arg_dry_run) | |
8311 | (void) reboot(RB_ENABLE_CAD); | |
e606bb61 | 8312 | |
4c80c73c | 8313 | switch (a) { |
e606bb61 LP |
8314 | |
8315 | case ACTION_HALT: | |
60675884 LP |
8316 | if (!arg_quiet) |
8317 | log_info("Halting."); | |
1ae17672 ZJS |
8318 | if (arg_dry_run) |
8319 | return 0; | |
19578bb2 | 8320 | (void) reboot(RB_HALT_SYSTEM); |
477def80 | 8321 | return -errno; |
e606bb61 LP |
8322 | |
8323 | case ACTION_POWEROFF: | |
60675884 LP |
8324 | if (!arg_quiet) |
8325 | log_info("Powering off."); | |
1ae17672 ZJS |
8326 | if (arg_dry_run) |
8327 | return 0; | |
19578bb2 | 8328 | (void) reboot(RB_POWER_OFF); |
477def80 | 8329 | return -errno; |
e606bb61 | 8330 | |
98d52feb | 8331 | case ACTION_KEXEC: |
c01dcddf LP |
8332 | case ACTION_REBOOT: |
8333 | return reboot_with_parameter(REBOOT_FALLBACK | | |
8334 | (arg_quiet ? 0 : REBOOT_LOG) | | |
8335 | (arg_dry_run ? REBOOT_DRY_RUN : 0)); | |
e606bb61 | 8336 | |
477def80 LP |
8337 | default: |
8338 | assert_not_reached("Unknown action."); | |
8339 | } | |
e606bb61 LP |
8340 | } |
8341 | ||
56a730fa LP |
8342 | static int logind_schedule_shutdown(void) { |
8343 | ||
349cc4a5 | 8344 | #if ENABLE_LOGIND |
4afd3348 | 8345 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
56a730fa LP |
8346 | char date[FORMAT_TIMESTAMP_MAX]; |
8347 | const char *action; | |
4fbd7192 | 8348 | sd_bus *bus; |
56a730fa LP |
8349 | int r; |
8350 | ||
4fbd7192 | 8351 | r = acquire_bus(BUS_FULL, &bus); |
56a730fa | 8352 | if (r < 0) |
4fbd7192 | 8353 | return r; |
56a730fa LP |
8354 | |
8355 | switch (arg_action) { | |
8356 | case ACTION_HALT: | |
8357 | action = "halt"; | |
8358 | break; | |
8359 | case ACTION_POWEROFF: | |
8360 | action = "poweroff"; | |
8361 | break; | |
8362 | case ACTION_KEXEC: | |
8363 | action = "kexec"; | |
8364 | break; | |
a4420f7b LP |
8365 | case ACTION_EXIT: |
8366 | action = "exit"; | |
8367 | break; | |
8368 | case ACTION_REBOOT: | |
56a730fa LP |
8369 | default: |
8370 | action = "reboot"; | |
8371 | break; | |
8372 | } | |
8373 | ||
1ae17672 | 8374 | if (arg_dry_run) |
56a730fa LP |
8375 | action = strjoina("dry-", action); |
8376 | ||
d2ad7e1b ZJS |
8377 | (void) logind_set_wall_message(); |
8378 | ||
56a730fa | 8379 | r = sd_bus_call_method( |
4fbd7192 | 8380 | bus, |
56a730fa LP |
8381 | "org.freedesktop.login1", |
8382 | "/org/freedesktop/login1", | |
8383 | "org.freedesktop.login1.Manager", | |
8384 | "ScheduleShutdown", | |
8385 | &error, | |
8386 | NULL, | |
8387 | "st", | |
8388 | action, | |
8389 | arg_when); | |
8390 | if (r < 0) | |
8391 | return log_warning_errno(r, "Failed to call ScheduleShutdown in logind, proceeding with immediate shutdown: %s", bus_error_message(&error, r)); | |
8392 | ||
60675884 LP |
8393 | if (!arg_quiet) |
8394 | log_info("Shutdown scheduled for %s, use 'shutdown -c' to cancel.", format_timestamp(date, sizeof(date), arg_when)); | |
56a730fa LP |
8395 | return 0; |
8396 | #else | |
8397 | log_error("Cannot schedule shutdown without logind support, proceeding with immediate shutdown."); | |
8398 | return -ENOSYS; | |
8399 | #endif | |
8400 | } | |
8401 | ||
4fbd7192 | 8402 | static int halt_main(void) { |
e4b61340 LP |
8403 | int r; |
8404 | ||
4fbd7192 | 8405 | r = logind_check_inhibitors(arg_action); |
748ebafa LP |
8406 | if (r < 0) |
8407 | return r; | |
b37844d3 | 8408 | |
7f96539d LP |
8409 | if (arg_when > 0) |
8410 | return logind_schedule_shutdown(); | |
8411 | ||
bc8c2f5c | 8412 | if (geteuid() != 0) { |
1ae17672 | 8413 | if (arg_dry_run || arg_force > 0) { |
fba868fa | 8414 | (void) must_be_root(); |
2ac3930f IS |
8415 | return -EPERM; |
8416 | } | |
8417 | ||
7e59bfcb LP |
8418 | /* Try logind if we are a normal user and no special |
8419 | * mode applies. Maybe PolicyKit allows us to shutdown | |
8420 | * the machine. */ | |
36b69c31 | 8421 | if (IN_SET(arg_action, ACTION_POWEROFF, ACTION_REBOOT, ACTION_HALT)) { |
4fbd7192 | 8422 | r = logind_reboot(arg_action); |
4c80c73c KS |
8423 | if (r >= 0) |
8424 | return r; | |
a9085ea3 | 8425 | if (IN_SET(r, -EOPNOTSUPP, -EINPROGRESS)) |
7f96539d LP |
8426 | /* requested operation is not |
8427 | * supported on the local system or | |
8428 | * already in progress */ | |
a9085ea3 IS |
8429 | return r; |
8430 | /* on all other errors, try low-level operation */ | |
4c80c73c | 8431 | } |
bc8c2f5c LP |
8432 | } |
8433 | ||
2955e0d4 GJ |
8434 | /* In order to minimize the difference between operation with and |
8435 | * without logind, we explicitly enable non-blocking mode for this, | |
8436 | * as logind's shutdown operations are always non-blocking. */ | |
8437 | arg_no_block = true; | |
8438 | ||
1ae17672 | 8439 | if (!arg_dry_run && !arg_force) |
4fbd7192 | 8440 | return start_with_fallback(); |
e4b61340 | 8441 | |
2ac3930f IS |
8442 | assert(geteuid() == 0); |
8443 | ||
d90e1a30 LP |
8444 | if (!arg_no_wtmp) { |
8445 | if (sd_booted() > 0) | |
8446 | log_debug("Not writing utmp record, assuming that systemd-update-utmp is used."); | |
7e59bfcb LP |
8447 | else { |
8448 | r = utmp_put_shutdown(); | |
8449 | if (r < 0) | |
da927ba9 | 8450 | log_warning_errno(r, "Failed to write utmp record: %m"); |
7e59bfcb | 8451 | } |
d90e1a30 | 8452 | } |
e4b61340 | 8453 | |
1ae17672 | 8454 | if (arg_dry_run) |
e4b61340 LP |
8455 | return 0; |
8456 | ||
477def80 | 8457 | r = halt_now(arg_action); |
691395d8 | 8458 | return log_error_errno(r, "Failed to reboot: %m"); |
e4b61340 LP |
8459 | } |
8460 | ||
8461 | static int runlevel_main(void) { | |
8462 | int r, runlevel, previous; | |
8463 | ||
729e3769 LP |
8464 | r = utmp_get_runlevel(&runlevel, &previous); |
8465 | if (r < 0) { | |
8466 | puts("unknown"); | |
e4b61340 LP |
8467 | return r; |
8468 | } | |
8469 | ||
8470 | printf("%c %c\n", | |
8471 | previous <= 0 ? 'N' : previous, | |
8472 | runlevel <= 0 ? 'N' : runlevel); | |
8473 | ||
8474 | return 0; | |
8475 | } | |
8476 | ||
2cf05793 | 8477 | static int logind_cancel_shutdown(void) { |
349cc4a5 | 8478 | #if ENABLE_LOGIND |
4afd3348 | 8479 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
4fbd7192 | 8480 | sd_bus *bus; |
949d9ce9 LP |
8481 | int r; |
8482 | ||
4fbd7192 | 8483 | r = acquire_bus(BUS_FULL, &bus); |
949d9ce9 | 8484 | if (r < 0) |
4fbd7192 | 8485 | return r; |
949d9ce9 | 8486 | |
4fbd7192 | 8487 | (void) logind_set_wall_message(); |
949d9ce9 LP |
8488 | |
8489 | r = sd_bus_call_method( | |
4fbd7192 | 8490 | bus, |
949d9ce9 LP |
8491 | "org.freedesktop.login1", |
8492 | "/org/freedesktop/login1", | |
8493 | "org.freedesktop.login1.Manager", | |
8494 | "CancelScheduledShutdown", | |
8495 | &error, | |
8496 | NULL, NULL); | |
8497 | if (r < 0) | |
8498 | return log_warning_errno(r, "Failed to talk to logind, shutdown hasn't been cancelled: %s", bus_error_message(&error, r)); | |
8499 | ||
8500 | return 0; | |
2cf05793 LP |
8501 | #else |
8502 | log_error("Not compiled with logind support, cannot cancel scheduled shutdowns."); | |
8503 | return -ENOSYS; | |
8504 | #endif | |
949d9ce9 LP |
8505 | } |
8506 | ||
e4b61340 | 8507 | int main(int argc, char*argv[]) { |
f459b602 | 8508 | int r; |
e4b61340 | 8509 | |
acc28e2e FB |
8510 | argv_cmdline = argv[0]; |
8511 | ||
a9cdc94f | 8512 | setlocale(LC_ALL, ""); |
e4b61340 | 8513 | log_parse_environment(); |
2396fb04 | 8514 | log_open(); |
592705f2 | 8515 | sigbus_install(); |
e4b61340 | 8516 | |
184ecaf7 DR |
8517 | /* Explicitly not on_tty() to avoid setting cached value. |
8518 | * This becomes relevant for piping output which might be | |
8519 | * ellipsized. */ | |
8520 | original_stdout_is_tty = isatty(STDOUT_FILENO); | |
8521 | ||
04ebb595 | 8522 | r = parse_argv(argc, argv); |
f459b602 | 8523 | if (r <= 0) |
e4b61340 | 8524 | goto finish; |
7e4249b9 | 8525 | |
040524b4 | 8526 | if (arg_action != ACTION_SYSTEMCTL && running_in_chroot() > 0) { |
60675884 LP |
8527 | |
8528 | if (!arg_quiet) | |
8529 | log_info("Running in chroot, ignoring request."); | |
f459b602 | 8530 | r = 0; |
82e23ddd LP |
8531 | goto finish; |
8532 | } | |
8533 | ||
41dd15e4 LP |
8534 | /* systemctl_main() will print an error message for the bus |
8535 | * connection, but only if it needs to */ | |
e4b61340 LP |
8536 | |
8537 | switch (arg_action) { | |
8538 | ||
22f4096c | 8539 | case ACTION_SYSTEMCTL: |
e449de87 | 8540 | r = systemctl_main(argc, argv); |
e4b61340 | 8541 | break; |
e4b61340 | 8542 | |
081dc638 AJ |
8543 | /* Legacy command aliases set arg_action. They provide some fallbacks, |
8544 | * e.g. to tell sysvinit to reboot after you have installed systemd | |
8545 | * binaries. */ | |
8546 | ||
e4b61340 LP |
8547 | case ACTION_HALT: |
8548 | case ACTION_POWEROFF: | |
8549 | case ACTION_REBOOT: | |
5622dde3 | 8550 | case ACTION_KEXEC: |
4fbd7192 | 8551 | r = halt_main(); |
e4b61340 LP |
8552 | break; |
8553 | ||
e4b61340 LP |
8554 | case ACTION_RUNLEVEL2: |
8555 | case ACTION_RUNLEVEL3: | |
8556 | case ACTION_RUNLEVEL4: | |
8557 | case ACTION_RUNLEVEL5: | |
8558 | case ACTION_RESCUE: | |
4fbd7192 | 8559 | r = start_with_fallback(); |
e4b61340 | 8560 | break; |
7e4249b9 | 8561 | |
e4b61340 LP |
8562 | case ACTION_RELOAD: |
8563 | case ACTION_REEXEC: | |
4fbd7192 | 8564 | r = reload_with_fallback(); |
e4b61340 LP |
8565 | break; |
8566 | ||
949d9ce9 | 8567 | case ACTION_CANCEL_SHUTDOWN: |
2cf05793 | 8568 | r = logind_cancel_shutdown(); |
f6144808 LP |
8569 | break; |
8570 | ||
eb22ac37 | 8571 | case ACTION_RUNLEVEL: |
4f16c1f4 LP |
8572 | r = runlevel_main(); |
8573 | break; | |
8574 | ||
081dc638 AJ |
8575 | case ACTION_EXIT: |
8576 | case ACTION_SUSPEND: | |
8577 | case ACTION_HIBERNATE: | |
8578 | case ACTION_HYBRID_SLEEP: | |
e68c79db | 8579 | case ACTION_SUSPEND_THEN_HIBERNATE: |
081dc638 AJ |
8580 | case ACTION_EMERGENCY: |
8581 | case ACTION_DEFAULT: | |
8582 | /* systemctl verbs with no equivalent in the legacy commands. | |
8583 | * These cannot appear in arg_action. Fall through. */ | |
8584 | ||
f459b602 | 8585 | case _ACTION_INVALID: |
e4b61340 LP |
8586 | default: |
8587 | assert_not_reached("Unknown action"); | |
8588 | } | |
7e4249b9 LP |
8589 | |
8590 | finish: | |
cf647b69 LP |
8591 | release_busses(); |
8592 | ||
f459b602 MAP |
8593 | pager_close(); |
8594 | ask_password_agent_close(); | |
8595 | polkit_agent_close(); | |
7e4249b9 | 8596 | |
20b3f379 | 8597 | strv_free(arg_types); |
9b9b3d36 | 8598 | strv_free(arg_states); |
20b3f379 | 8599 | strv_free(arg_properties); |
ea4a240d | 8600 | |
172d7abf | 8601 | strv_free(arg_wall); |
0f03c2a4 | 8602 | free(arg_root); |
4bb2e9d4 | 8603 | free(arg_esp_path); |
172d7abf | 8604 | |
404f08d3 | 8605 | /* Note that we return r here, not EXIT_SUCCESS, so that we can implement the LSB-like return codes */ |
9eb4a501 | 8606 | return r < 0 ? EXIT_FAILURE : r; |
7e4249b9 | 8607 | } |