1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
7 Copyright 2013 Marc-Antoine Perennou
9 systemd is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 2.1 of the License, or
12 (at your option) any later version.
14 systemd is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public License
20 along with systemd; If not, see <http://www.gnu.org/licenses/>.
23 #include <sys/reboot.h>
24 #include <linux/reboot.h>
25 #include <sys/syscall.h>
32 #include <sys/ioctl.h>
36 #include <sys/socket.h>
39 #include <sys/prctl.h>
42 #include "sd-daemon.h"
43 #include "sd-shutdown.h"
50 #include "utmp-wtmp.h"
53 #include "path-util.h"
55 #include "cgroup-show.h"
56 #include "cgroup-util.h"
58 #include "path-lookup.h"
59 #include "conf-parser.h"
60 #include "exit-status.h"
62 #include "unit-name.h"
64 #include "spawn-ask-password-agent.h"
65 #include "spawn-polkit-agent.h"
67 #include "logs-show.h"
68 #include "socket-util.h"
73 #include "bus-message.h"
74 #include "bus-error.h"
75 #include "bus-common-errors.h"
79 static char **arg_types = NULL;
80 static char **arg_states = NULL;
81 static char **arg_properties = NULL;
82 static bool arg_all = false;
83 static enum dependency {
89 } arg_dependency = DEPENDENCY_FORWARD;
90 static const char *arg_job_mode = "replace";
91 static UnitFileScope arg_scope = UNIT_FILE_SYSTEM;
92 static bool arg_no_block = false;
93 static bool arg_no_legend = false;
94 static bool arg_no_pager = false;
95 static bool arg_no_wtmp = false;
96 static bool arg_no_wall = false;
97 static bool arg_no_reload = false;
98 static bool arg_show_types = false;
99 static bool arg_ignore_inhibitors = false;
100 static bool arg_dry = false;
101 static bool arg_quiet = false;
102 static bool arg_full = false;
103 static bool arg_recursive = false;
104 static int arg_force = 0;
105 static bool arg_ask_password = true;
106 static bool arg_runtime = false;
107 static UnitFilePresetMode arg_preset_mode = UNIT_FILE_PRESET_FULL;
108 static char **arg_wall = NULL;
109 static const char *arg_kill_who = NULL;
110 static int arg_signal = SIGTERM;
111 static const char *arg_root = NULL;
112 static usec_t arg_when = 0;
134 ACTION_CANCEL_SHUTDOWN,
136 } arg_action = ACTION_SYSTEMCTL;
137 static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
138 static char *arg_host = NULL;
139 static unsigned arg_lines = 10;
140 static OutputMode arg_output = OUTPUT_SHORT;
141 static bool arg_plain = false;
143 static bool original_stdout_is_tty;
145 static int daemon_reload(sd_bus *bus, char **args);
146 static int halt_now(enum action a);
147 static int check_one_unit(sd_bus *bus, const char *name, const char *good_states, bool quiet);
149 static char** strv_skip_first(char **strv) {
150 if (strv_length(strv) > 0)
155 static void pager_open_if_enabled(void) {
163 static void ask_password_agent_open_if_enabled(void) {
165 /* Open the password agent as a child process if necessary */
167 if (!arg_ask_password)
170 if (arg_scope != UNIT_FILE_SYSTEM)
173 if (arg_transport != BUS_TRANSPORT_LOCAL)
176 ask_password_agent_open();
180 static void polkit_agent_open_if_enabled(void) {
182 /* Open the polkit agent as a child process if necessary */
184 if (!arg_ask_password)
187 if (arg_scope != UNIT_FILE_SYSTEM)
190 if (arg_transport != BUS_TRANSPORT_LOCAL)
197 static int translate_bus_error_to_exit_status(int r, const sd_bus_error *error) {
200 if (!sd_bus_error_is_set(error))
203 if (sd_bus_error_has_name(error, SD_BUS_ERROR_ACCESS_DENIED) ||
204 sd_bus_error_has_name(error, BUS_ERROR_ONLY_BY_DEPENDENCY) ||
205 sd_bus_error_has_name(error, BUS_ERROR_NO_ISOLATION) ||
206 sd_bus_error_has_name(error, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE))
207 return EXIT_NOPERMISSION;
209 if (sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT))
210 return EXIT_NOTINSTALLED;
212 if (sd_bus_error_has_name(error, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE) ||
213 sd_bus_error_has_name(error, SD_BUS_ERROR_NOT_SUPPORTED))
214 return EXIT_NOTIMPLEMENTED;
216 if (sd_bus_error_has_name(error, BUS_ERROR_LOAD_FAILED))
217 return EXIT_NOTCONFIGURED;
225 static void warn_wall(enum action a) {
226 static const char *table[_ACTION_MAX] = {
227 [ACTION_HALT] = "The system is going down for system halt NOW!",
228 [ACTION_REBOOT] = "The system is going down for reboot NOW!",
229 [ACTION_POWEROFF] = "The system is going down for power-off NOW!",
230 [ACTION_KEXEC] = "The system is going down for kexec reboot NOW!",
231 [ACTION_RESCUE] = "The system is going down to rescue mode NOW!",
232 [ACTION_EMERGENCY] = "The system is going down to emergency mode NOW!",
233 [ACTION_CANCEL_SHUTDOWN] = "The system shutdown has been cancelled NOW!"
240 _cleanup_free_ char *p;
242 p = strv_join(arg_wall, " ");
249 utmp_wall(p, NULL, NULL);
257 utmp_wall(table[a], NULL, NULL);
260 static bool avoid_bus(void) {
262 if (running_in_chroot() > 0)
265 if (sd_booted() <= 0)
268 if (!isempty(arg_root))
271 if (arg_scope == UNIT_FILE_GLOBAL)
277 static int compare_unit_info(const void *a, const void *b) {
278 const UnitInfo *u = a, *v = b;
282 /* First, order by machine */
283 if (!u->machine && v->machine)
285 if (u->machine && !v->machine)
287 if (u->machine && v->machine) {
288 r = strcasecmp(u->machine, v->machine);
293 /* Second, order by unit type */
294 d1 = strrchr(u->id, '.');
295 d2 = strrchr(v->id, '.');
297 r = strcasecmp(d1, d2);
302 /* Third, order by name */
303 return strcasecmp(u->id, v->id);
306 static bool output_show_unit(const UnitInfo *u, char **patterns) {
307 if (!strv_isempty(patterns)) {
310 STRV_FOREACH(pattern, patterns)
311 if (fnmatch(*pattern, u->id, FNM_NOESCAPE) == 0)
320 dot = strrchr(u->id, '.');
324 if (!strv_find(arg_types, dot+1))
334 if (streq(u->active_state, "inactive") || u->following[0])
340 static int output_units_list(const UnitInfo *unit_infos, unsigned c) {
341 unsigned circle_len = 0, id_len, max_id_len, load_len, active_len, sub_len, job_len, desc_len;
343 unsigned n_shown = 0;
346 max_id_len = strlen("UNIT");
347 load_len = strlen("LOAD");
348 active_len = strlen("ACTIVE");
349 sub_len = strlen("SUB");
350 job_len = strlen("JOB");
353 for (u = unit_infos; u < unit_infos + c; u++) {
354 max_id_len = MAX(max_id_len, strlen(u->id) + (u->machine ? strlen(u->machine)+1 : 0));
355 load_len = MAX(load_len, strlen(u->load_state));
356 active_len = MAX(active_len, strlen(u->active_state));
357 sub_len = MAX(sub_len, strlen(u->sub_state));
359 if (u->job_id != 0) {
360 job_len = MAX(job_len, strlen(u->job_type));
364 if (!arg_no_legend &&
365 (streq(u->active_state, "failed") ||
366 STR_IN_SET(u->load_state, "error", "not-found", "masked")))
370 if (!arg_full && original_stdout_is_tty) {
373 id_len = MIN(max_id_len, 25u);
374 basic_len = circle_len + 5 + id_len + 5 + active_len + sub_len;
377 basic_len += job_len + 1;
379 if (basic_len < (unsigned) columns()) {
380 unsigned extra_len, incr;
381 extra_len = columns() - basic_len;
383 /* Either UNIT already got 25, or is fully satisfied.
384 * Grant up to 25 to DESC now. */
385 incr = MIN(extra_len, 25u);
389 /* split the remaining space between UNIT and DESC,
390 * but do not give UNIT more than it needs. */
392 incr = MIN(extra_len / 2, max_id_len - id_len);
394 desc_len += extra_len - incr;
400 for (u = unit_infos; u < unit_infos + c; u++) {
401 _cleanup_free_ char *e = NULL, *j = NULL;
402 const char *on_loaded = "", *off_loaded = "";
403 const char *on_active = "", *off_active = "";
404 const char *on_circle = "", *off_circle = "";
408 if (!n_shown && !arg_no_legend) {
413 printf("%-*s %-*s %-*s %-*s ",
416 active_len, "ACTIVE",
420 printf("%-*s ", job_len, "JOB");
422 if (!arg_full && arg_no_pager)
423 printf("%.*s\n", desc_len, "DESCRIPTION");
425 printf("%s\n", "DESCRIPTION");
430 if (STR_IN_SET(u->load_state, "error", "not-found", "masked")) {
431 on_loaded = ansi_highlight_red();
432 on_circle = ansi_highlight_yellow();
433 off_loaded = off_circle = ansi_highlight_off();
437 if (streq(u->active_state, "failed")) {
438 on_circle = on_active = ansi_highlight_red();
439 off_circle = off_active = ansi_highlight_off();
444 j = strjoin(u->machine, ":", u->id, NULL);
453 e = ellipsize(id, id_len, 33);
461 printf("%s%s%s ", on_circle, circle ? draw_special_char(DRAW_BLACK_CIRCLE) : " ", off_circle);
463 printf("%s%-*s%s %s%-*s%s %s%-*s %-*s%s %-*s",
464 on_active, id_len, id, off_active,
465 on_loaded, load_len, u->load_state, off_loaded,
466 on_active, active_len, u->active_state,
467 sub_len, u->sub_state, off_active,
468 job_count ? job_len + 1 : 0, u->job_id ? u->job_type : "");
471 printf("%.*s\n", desc_len, u->description);
473 printf("%s\n", u->description);
476 if (!arg_no_legend) {
477 const char *on, *off;
481 "LOAD = Reflects whether the unit definition was properly loaded.\n"
482 "ACTIVE = The high-level unit activation state, i.e. generalization of SUB.\n"
483 "SUB = The low-level unit activation state, values depend on unit type.");
484 puts(job_count ? "JOB = Pending job for the unit.\n" : "");
485 on = ansi_highlight();
486 off = ansi_highlight_off();
488 on = ansi_highlight_red();
489 off = ansi_highlight_off();
493 printf("%s%u loaded units listed.%s\n"
494 "To show all installed unit files use 'systemctl list-unit-files'.\n",
497 printf("%s%u loaded units listed.%s Pass --all to see loaded but inactive units, too.\n"
498 "To show all installed unit files use 'systemctl list-unit-files'.\n",
505 static int get_unit_list(
509 UnitInfo **unit_infos,
511 sd_bus_message **_reply) {
513 _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
514 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
515 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
524 r = sd_bus_message_new_method_call(
527 "org.freedesktop.systemd1",
528 "/org/freedesktop/systemd1",
529 "org.freedesktop.systemd1.Manager",
530 "ListUnitsFiltered");
533 return bus_log_create_error(r);
535 r = sd_bus_message_append_strv(m, arg_states);
537 return bus_log_create_error(r);
539 r = sd_bus_call(bus, m, 0, &error, &reply);
541 log_error("Failed to list units: %s", bus_error_message(&error, r));
545 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssssouso)");
547 return bus_log_parse_error(r);
549 while ((r = bus_parse_unit_info(reply, &u)) > 0) {
552 if (!output_show_unit(&u, patterns))
555 if (!GREEDY_REALLOC(*unit_infos, size, c+1))
558 (*unit_infos)[c++] = u;
561 return bus_log_parse_error(r);
563 r = sd_bus_message_exit_container(reply);
565 return bus_log_parse_error(r);
573 static void message_set_freep(Set **set) {
576 while ((m = set_steal_first(*set)))
577 sd_bus_message_unref(m);
582 static int get_unit_list_recursive(
585 UnitInfo **_unit_infos,
589 _cleanup_free_ UnitInfo *unit_infos = NULL;
590 _cleanup_(message_set_freep) Set *replies;
591 sd_bus_message *reply;
599 replies = set_new(NULL);
603 c = get_unit_list(bus, NULL, patterns, &unit_infos, 0, &reply);
607 r = set_put(replies, reply);
609 sd_bus_message_unref(reply);
614 _cleanup_strv_free_ char **machines = NULL;
617 r = sd_get_machine_names(&machines);
621 STRV_FOREACH(i, machines) {
622 _cleanup_bus_close_unref_ sd_bus *container = NULL;
625 r = sd_bus_open_system_machine(&container, *i);
627 log_error_errno(r, "Failed to connect to container %s: %m", *i);
631 k = get_unit_list(container, *i, patterns, &unit_infos, c, &reply);
637 r = set_put(replies, reply);
639 sd_bus_message_unref(reply);
644 *_machines = machines;
649 *_unit_infos = unit_infos;
658 static int list_units(sd_bus *bus, char **args) {
659 _cleanup_free_ UnitInfo *unit_infos = NULL;
660 _cleanup_(message_set_freep) Set *replies = NULL;
661 _cleanup_strv_free_ char **machines = NULL;
664 pager_open_if_enabled();
666 r = get_unit_list_recursive(bus, strv_skip_first(args), &unit_infos, &replies, &machines);
670 qsort_safe(unit_infos, r, sizeof(UnitInfo), compare_unit_info);
671 return output_units_list(unit_infos, r);
674 static int get_triggered_units(
679 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
682 r = sd_bus_get_property_strv(
684 "org.freedesktop.systemd1",
686 "org.freedesktop.systemd1.Unit",
692 log_error("Failed to determine triggers: %s", bus_error_message(&error, r));
697 static int get_listening(
699 const char* unit_path,
702 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
703 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
704 const char *type, *path;
707 r = sd_bus_get_property(
709 "org.freedesktop.systemd1",
711 "org.freedesktop.systemd1.Socket",
717 log_error("Failed to get list of listening sockets: %s", bus_error_message(&error, r));
721 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ss)");
723 return bus_log_parse_error(r);
725 while ((r = sd_bus_message_read(reply, "(ss)", &type, &path)) > 0) {
727 r = strv_extend(listening, type);
731 r = strv_extend(listening, path);
738 return bus_log_parse_error(r);
740 r = sd_bus_message_exit_container(reply);
742 return bus_log_parse_error(r);
754 /* Note: triggered is a list here, although it almost certainly
755 * will always be one unit. Nevertheless, dbus API allows for multiple
756 * values, so let's follow that. */
759 /* The strv above is shared. free is set only in the first one. */
763 static int socket_info_compare(const struct socket_info *a, const struct socket_info *b) {
769 if (!a->machine && b->machine)
771 if (a->machine && !b->machine)
773 if (a->machine && b->machine) {
774 o = strcasecmp(a->machine, b->machine);
779 o = strcmp(a->path, b->path);
781 o = strcmp(a->type, b->type);
786 static int output_sockets_list(struct socket_info *socket_infos, unsigned cs) {
787 struct socket_info *s;
788 unsigned pathlen = strlen("LISTEN"),
789 typelen = strlen("TYPE") * arg_show_types,
790 socklen = strlen("UNIT"),
791 servlen = strlen("ACTIVATES");
792 const char *on, *off;
794 for (s = socket_infos; s < socket_infos + cs; s++) {
798 socklen = MAX(socklen, strlen(s->id));
800 typelen = MAX(typelen, strlen(s->type));
801 pathlen = MAX(pathlen, strlen(s->path) + (s->machine ? strlen(s->machine)+1 : 0));
803 STRV_FOREACH(a, s->triggered)
804 tmp += strlen(*a) + 2*(a != s->triggered);
805 servlen = MAX(servlen, tmp);
810 printf("%-*s %-*.*s%-*s %s\n",
812 typelen + arg_show_types, typelen + arg_show_types, "TYPE ",
816 for (s = socket_infos; s < socket_infos + cs; s++) {
817 _cleanup_free_ char *j = NULL;
822 j = strjoin(s->machine, ":", s->path, NULL);
830 printf("%-*s %-*s %-*s",
831 pathlen, path, typelen, s->type, socklen, s->id);
834 pathlen, path, socklen, s->id);
835 STRV_FOREACH(a, s->triggered)
837 a == s->triggered ? "" : ",", *a);
841 on = ansi_highlight();
842 off = ansi_highlight_off();
846 on = ansi_highlight_red();
847 off = ansi_highlight_off();
850 if (!arg_no_legend) {
851 printf("%s%u sockets listed.%s\n", on, cs, off);
853 printf("Pass --all to see loaded but inactive sockets, too.\n");
859 static int list_sockets(sd_bus *bus, char **args) {
860 _cleanup_(message_set_freep) Set *replies = NULL;
861 _cleanup_strv_free_ char **machines = NULL;
862 _cleanup_free_ UnitInfo *unit_infos = NULL;
863 _cleanup_free_ struct socket_info *socket_infos = NULL;
865 struct socket_info *s;
870 pager_open_if_enabled();
872 n = get_unit_list_recursive(bus, strv_skip_first(args), &unit_infos, &replies, &machines);
876 for (u = unit_infos; u < unit_infos + n; u++) {
877 _cleanup_strv_free_ char **listening = NULL, **triggered = NULL;
880 if (!endswith(u->id, ".socket"))
883 r = get_triggered_units(bus, u->unit_path, &triggered);
887 c = get_listening(bus, u->unit_path, &listening);
893 if (!GREEDY_REALLOC(socket_infos, size, cs + c)) {
898 for (i = 0; i < c; i++)
899 socket_infos[cs + i] = (struct socket_info) {
900 .machine = u->machine,
902 .type = listening[i*2],
903 .path = listening[i*2 + 1],
904 .triggered = triggered,
905 .own_triggered = i==0,
908 /* from this point on we will cleanup those socket_infos */
911 listening = triggered = NULL; /* avoid cleanup */
914 qsort_safe(socket_infos, cs, sizeof(struct socket_info),
915 (__compar_fn_t) socket_info_compare);
917 output_sockets_list(socket_infos, cs);
920 assert(cs == 0 || socket_infos);
921 for (s = socket_infos; s < socket_infos + cs; s++) {
924 if (s->own_triggered)
925 strv_free(s->triggered);
931 static int get_next_elapse(
934 dual_timestamp *next) {
936 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
944 r = sd_bus_get_property_trivial(
946 "org.freedesktop.systemd1",
948 "org.freedesktop.systemd1.Timer",
949 "NextElapseUSecMonotonic",
954 log_error("Failed to get next elapsation time: %s", bus_error_message(&error, r));
958 r = sd_bus_get_property_trivial(
960 "org.freedesktop.systemd1",
962 "org.freedesktop.systemd1.Timer",
963 "NextElapseUSecRealtime",
968 log_error("Failed to get next elapsation time: %s", bus_error_message(&error, r));
976 static int get_last_trigger(
981 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
988 r = sd_bus_get_property_trivial(
990 "org.freedesktop.systemd1",
992 "org.freedesktop.systemd1.Timer",
998 log_error("Failed to get last trigger time: %s", bus_error_message(&error, r));
1006 const char* machine;
1009 usec_t last_trigger;
1013 static int timer_info_compare(const struct timer_info *a, const struct timer_info *b) {
1019 if (!a->machine && b->machine)
1021 if (a->machine && !b->machine)
1023 if (a->machine && b->machine) {
1024 o = strcasecmp(a->machine, b->machine);
1029 if (a->next_elapse < b->next_elapse)
1031 if (a->next_elapse > b->next_elapse)
1034 return strcmp(a->id, b->id);
1037 static int output_timers_list(struct timer_info *timer_infos, unsigned n) {
1038 struct timer_info *t;
1040 nextlen = strlen("NEXT"),
1041 leftlen = strlen("LEFT"),
1042 lastlen = strlen("LAST"),
1043 passedlen = strlen("PASSED"),
1044 unitlen = strlen("UNIT"),
1045 activatelen = strlen("ACTIVATES");
1047 const char *on, *off;
1049 assert(timer_infos || n == 0);
1051 for (t = timer_infos; t < timer_infos + n; t++) {
1055 if (t->next_elapse > 0) {
1056 char tstamp[FORMAT_TIMESTAMP_MAX] = "", trel[FORMAT_TIMESTAMP_RELATIVE_MAX] = "";
1058 format_timestamp(tstamp, sizeof(tstamp), t->next_elapse);
1059 nextlen = MAX(nextlen, strlen(tstamp) + 1);
1061 format_timestamp_relative(trel, sizeof(trel), t->next_elapse);
1062 leftlen = MAX(leftlen, strlen(trel));
1065 if (t->last_trigger > 0) {
1066 char tstamp[FORMAT_TIMESTAMP_MAX] = "", trel[FORMAT_TIMESTAMP_RELATIVE_MAX] = "";
1068 format_timestamp(tstamp, sizeof(tstamp), t->last_trigger);
1069 lastlen = MAX(lastlen, strlen(tstamp) + 1);
1071 format_timestamp_relative(trel, sizeof(trel), t->last_trigger);
1072 passedlen = MAX(passedlen, strlen(trel));
1075 unitlen = MAX(unitlen, strlen(t->id) + (t->machine ? strlen(t->machine)+1 : 0));
1077 STRV_FOREACH(a, t->triggered)
1078 ul += strlen(*a) + 2*(a != t->triggered);
1080 activatelen = MAX(activatelen, ul);
1085 printf("%-*s %-*s %-*s %-*s %-*s %s\n",
1089 passedlen, "PASSED",
1093 for (t = timer_infos; t < timer_infos + n; t++) {
1094 _cleanup_free_ char *j = NULL;
1096 char tstamp1[FORMAT_TIMESTAMP_MAX] = "n/a", trel1[FORMAT_TIMESTAMP_RELATIVE_MAX] = "n/a";
1097 char tstamp2[FORMAT_TIMESTAMP_MAX] = "n/a", trel2[FORMAT_TIMESTAMP_RELATIVE_MAX] = "n/a";
1100 format_timestamp(tstamp1, sizeof(tstamp1), t->next_elapse);
1101 format_timestamp_relative(trel1, sizeof(trel1), t->next_elapse);
1103 format_timestamp(tstamp2, sizeof(tstamp2), t->last_trigger);
1104 format_timestamp_relative(trel2, sizeof(trel2), t->last_trigger);
1107 j = strjoin(t->machine, ":", t->id, NULL);
1114 printf("%-*s %-*s %-*s %-*s %-*s",
1115 nextlen, tstamp1, leftlen, trel1, lastlen, tstamp2, passedlen, trel2, unitlen, unit);
1117 STRV_FOREACH(a, t->triggered)
1119 a == t->triggered ? "" : ",", *a);
1123 on = ansi_highlight();
1124 off = ansi_highlight_off();
1128 on = ansi_highlight_red();
1129 off = ansi_highlight_off();
1132 if (!arg_no_legend) {
1133 printf("%s%u timers listed.%s\n", on, n, off);
1135 printf("Pass --all to see loaded but inactive timers, too.\n");
1141 static usec_t calc_next_elapse(dual_timestamp *nw, dual_timestamp *next) {
1147 if (next->monotonic != USEC_INFINITY && next->monotonic > 0) {
1150 if (next->monotonic > nw->monotonic)
1151 converted = nw->realtime + (next->monotonic - nw->monotonic);
1153 converted = nw->realtime - (nw->monotonic - next->monotonic);
1155 if (next->realtime != USEC_INFINITY && next->realtime > 0)
1156 next_elapse = MIN(converted, next->realtime);
1158 next_elapse = converted;
1161 next_elapse = next->realtime;
1166 static int list_timers(sd_bus *bus, char **args) {
1167 _cleanup_(message_set_freep) Set *replies = NULL;
1168 _cleanup_strv_free_ char **machines = NULL;
1169 _cleanup_free_ struct timer_info *timer_infos = NULL;
1170 _cleanup_free_ UnitInfo *unit_infos = NULL;
1171 struct timer_info *t;
1178 pager_open_if_enabled();
1180 n = get_unit_list_recursive(bus, strv_skip_first(args), &unit_infos, &replies, &machines);
1184 dual_timestamp_get(&nw);
1186 for (u = unit_infos; u < unit_infos + n; u++) {
1187 _cleanup_strv_free_ char **triggered = NULL;
1188 dual_timestamp next = {};
1191 if (!endswith(u->id, ".timer"))
1194 r = get_triggered_units(bus, u->unit_path, &triggered);
1198 r = get_next_elapse(bus, u->unit_path, &next);
1202 get_last_trigger(bus, u->unit_path, &last);
1204 if (!GREEDY_REALLOC(timer_infos, size, c+1)) {
1209 m = calc_next_elapse(&nw, &next);
1211 timer_infos[c++] = (struct timer_info) {
1212 .machine = u->machine,
1215 .last_trigger = last,
1216 .triggered = triggered,
1219 triggered = NULL; /* avoid cleanup */
1222 qsort_safe(timer_infos, c, sizeof(struct timer_info),
1223 (__compar_fn_t) timer_info_compare);
1225 output_timers_list(timer_infos, c);
1228 for (t = timer_infos; t < timer_infos + c; t++)
1229 strv_free(t->triggered);
1234 static int compare_unit_file_list(const void *a, const void *b) {
1235 const char *d1, *d2;
1236 const UnitFileList *u = a, *v = b;
1238 d1 = strrchr(u->path, '.');
1239 d2 = strrchr(v->path, '.');
1244 r = strcasecmp(d1, d2);
1249 return strcasecmp(basename(u->path), basename(v->path));
1252 static bool output_show_unit_file(const UnitFileList *u, char **patterns) {
1253 if (!strv_isempty(patterns)) {
1256 STRV_FOREACH(pattern, patterns)
1257 if (fnmatch(*pattern, basename(u->path), FNM_NOESCAPE) == 0)
1263 if (!strv_isempty(arg_types)) {
1266 dot = strrchr(u->path, '.');
1270 if (!strv_find(arg_types, dot+1))
1274 if (!strv_isempty(arg_states)) {
1275 if (!strv_find(arg_states, unit_file_state_to_string(u->state)))
1282 static void output_unit_file_list(const UnitFileList *units, unsigned c) {
1283 unsigned max_id_len, id_cols, state_cols;
1284 const UnitFileList *u;
1286 max_id_len = strlen("UNIT FILE");
1287 state_cols = strlen("STATE");
1289 for (u = units; u < units + c; u++) {
1290 max_id_len = MAX(max_id_len, strlen(basename(u->path)));
1291 state_cols = MAX(state_cols, strlen(unit_file_state_to_string(u->state)));
1295 unsigned basic_cols;
1297 id_cols = MIN(max_id_len, 25u);
1298 basic_cols = 1 + id_cols + state_cols;
1299 if (basic_cols < (unsigned) columns())
1300 id_cols += MIN(columns() - basic_cols, max_id_len - id_cols);
1302 id_cols = max_id_len;
1305 printf("%-*s %-*s\n",
1306 id_cols, "UNIT FILE",
1307 state_cols, "STATE");
1309 for (u = units; u < units + c; u++) {
1310 _cleanup_free_ char *e = NULL;
1311 const char *on, *off;
1314 if (u->state == UNIT_FILE_MASKED ||
1315 u->state == UNIT_FILE_MASKED_RUNTIME ||
1316 u->state == UNIT_FILE_DISABLED ||
1317 u->state == UNIT_FILE_INVALID) {
1318 on = ansi_highlight_red();
1319 off = ansi_highlight_off();
1320 } else if (u->state == UNIT_FILE_ENABLED) {
1321 on = ansi_highlight_green();
1322 off = ansi_highlight_off();
1326 id = basename(u->path);
1328 e = arg_full ? NULL : ellipsize(id, id_cols, 33);
1330 printf("%-*s %s%-*s%s\n",
1331 id_cols, e ? e : id,
1332 on, state_cols, unit_file_state_to_string(u->state), off);
1336 printf("\n%u unit files listed.\n", c);
1339 static int list_unit_files(sd_bus *bus, char **args) {
1340 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
1341 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
1342 _cleanup_free_ UnitFileList *units = NULL;
1350 pager_open_if_enabled();
1358 h = hashmap_new(&string_hash_ops);
1362 r = unit_file_get_list(arg_scope, arg_root, h);
1364 unit_file_list_free(h);
1365 log_error_errno(r, "Failed to get unit file list: %m");
1369 n_units = hashmap_size(h);
1371 units = new(UnitFileList, n_units);
1372 if (!units && n_units > 0) {
1373 unit_file_list_free(h);
1377 HASHMAP_FOREACH(u, h, i) {
1378 if (!output_show_unit_file(u, strv_skip_first(args)))
1385 assert(c <= n_units);
1388 r = sd_bus_call_method(
1390 "org.freedesktop.systemd1",
1391 "/org/freedesktop/systemd1",
1392 "org.freedesktop.systemd1.Manager",
1398 log_error("Failed to list unit files: %s", bus_error_message(&error, r));
1402 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ss)");
1404 return bus_log_parse_error(r);
1406 while ((r = sd_bus_message_read(reply, "(ss)", &path, &state)) > 0) {
1408 if (!GREEDY_REALLOC(units, size, c + 1))
1411 units[c] = (struct UnitFileList) {
1413 unit_file_state_from_string(state)
1416 if (output_show_unit_file(&units[c], strv_skip_first(args)))
1421 return bus_log_parse_error(r);
1423 r = sd_bus_message_exit_container(reply);
1425 return bus_log_parse_error(r);
1428 qsort_safe(units, c, sizeof(UnitFileList), compare_unit_file_list);
1429 output_unit_file_list(units, c);
1432 for (unit = units; unit < units + c; unit++)
1439 static int list_dependencies_print(const char *name, int level, unsigned int branches, bool last) {
1440 _cleanup_free_ char *n = NULL;
1441 size_t max_len = MAX(columns(),20u);
1447 for (i = level - 1; i >= 0; i--) {
1449 if (len > max_len - 3 && !arg_full) {
1450 printf("%s...\n",max_len % 2 ? "" : " ");
1453 printf("%s", draw_special_char(branches & (1 << i) ? DRAW_TREE_VERTICAL : DRAW_TREE_SPACE));
1457 if (len > max_len - 3 && !arg_full) {
1458 printf("%s...\n",max_len % 2 ? "" : " ");
1462 printf("%s", draw_special_char(last ? DRAW_TREE_RIGHT : DRAW_TREE_BRANCH));
1466 printf("%s\n", name);
1470 n = ellipsize(name, max_len-len, 100);
1478 static int list_dependencies_get_dependencies(sd_bus *bus, const char *name, char ***deps) {
1480 static const char *dependencies[_DEPENDENCY_MAX] = {
1481 [DEPENDENCY_FORWARD] = "Requires\0"
1482 "RequiresOverridable\0"
1484 "RequisiteOverridable\0"
1487 [DEPENDENCY_REVERSE] = "RequiredBy\0"
1488 "RequiredByOverridable\0"
1492 [DEPENDENCY_AFTER] = "After\0",
1493 [DEPENDENCY_BEFORE] = "Before\0",
1496 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
1497 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
1498 _cleanup_strv_free_ char **ret = NULL;
1499 _cleanup_free_ char *path = NULL;
1505 assert_cc(ELEMENTSOF(dependencies) == _DEPENDENCY_MAX);
1507 path = unit_dbus_path_from_name(name);
1511 r = sd_bus_call_method(
1513 "org.freedesktop.systemd1",
1515 "org.freedesktop.DBus.Properties",
1519 "s", "org.freedesktop.systemd1.Unit");
1521 log_error("Failed to get properties of %s: %s", name, bus_error_message(&error, r));
1525 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "{sv}");
1527 return bus_log_parse_error(r);
1529 while ((r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_DICT_ENTRY, "sv")) > 0) {
1532 r = sd_bus_message_read(reply, "s", &prop);
1534 return bus_log_parse_error(r);
1536 if (!nulstr_contains(dependencies[arg_dependency], prop)) {
1537 r = sd_bus_message_skip(reply, "v");
1539 return bus_log_parse_error(r);
1542 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_VARIANT, "as");
1544 return bus_log_parse_error(r);
1546 r = bus_message_read_strv_extend(reply, &ret);
1548 return bus_log_parse_error(r);
1550 r = sd_bus_message_exit_container(reply);
1552 return bus_log_parse_error(r);
1555 r = sd_bus_message_exit_container(reply);
1557 return bus_log_parse_error(r);
1561 return bus_log_parse_error(r);
1563 r = sd_bus_message_exit_container(reply);
1565 return bus_log_parse_error(r);
1573 static int list_dependencies_compare(const void *_a, const void *_b) {
1574 const char **a = (const char**) _a, **b = (const char**) _b;
1576 if (unit_name_to_type(*a) == UNIT_TARGET && unit_name_to_type(*b) != UNIT_TARGET)
1578 if (unit_name_to_type(*a) != UNIT_TARGET && unit_name_to_type(*b) == UNIT_TARGET)
1581 return strcasecmp(*a, *b);
1584 static int list_dependencies_one(
1589 unsigned int branches) {
1591 _cleanup_strv_free_ char **deps = NULL;
1599 r = strv_extend(units, name);
1603 r = list_dependencies_get_dependencies(bus, name, &deps);
1607 qsort_safe(deps, strv_length(deps), sizeof (char*), list_dependencies_compare);
1609 STRV_FOREACH(c, deps) {
1612 if (strv_contains(*units, *c)) {
1614 r = list_dependencies_print("...", level + 1, (branches << 1) | (c[1] == NULL ? 0 : 1), 1);
1621 state = check_one_unit(bus, *c, "activating\0active\0reloading\0", true);
1623 printf("%s%s%s ", ansi_highlight_green(), draw_special_char(DRAW_BLACK_CIRCLE), ansi_highlight_off());
1625 printf("%s%s%s ", ansi_highlight_red(), draw_special_char(DRAW_BLACK_CIRCLE), ansi_highlight_off());
1627 r = list_dependencies_print(*c, level, branches, c[1] == NULL);
1631 if (arg_all || unit_name_to_type(*c) == UNIT_TARGET) {
1632 r = list_dependencies_one(bus, *c, level + 1, units, (branches << 1) | (c[1] == NULL ? 0 : 1));
1639 strv_remove(*units, name);
1644 static int list_dependencies(sd_bus *bus, char **args) {
1645 _cleanup_strv_free_ char **units = NULL;
1646 _cleanup_free_ char *unit = NULL;
1652 unit = unit_name_mangle(args[1], MANGLE_NOGLOB);
1657 u = SPECIAL_DEFAULT_TARGET;
1659 pager_open_if_enabled();
1663 return list_dependencies_one(bus, u, 0, &units, 0);
1666 struct machine_info {
1670 char *control_group;
1671 uint32_t n_failed_units;
1676 static const struct bus_properties_map machine_info_property_map[] = {
1677 { "SystemState", "s", NULL, offsetof(struct machine_info, state) },
1678 { "NJobs", "u", NULL, offsetof(struct machine_info, n_jobs) },
1679 { "NFailedUnits", "u", NULL, offsetof(struct machine_info, n_failed_units) },
1680 { "ControlGroup", "s", NULL, offsetof(struct machine_info, control_group) },
1681 { "UserspaceTimestamp", "t", NULL, offsetof(struct machine_info, timestamp) },
1685 static void free_machines_list(struct machine_info *machine_infos, int n) {
1691 for (i = 0; i < n; i++) {
1692 free(machine_infos[i].name);
1693 free(machine_infos[i].state);
1694 free(machine_infos[i].control_group);
1697 free(machine_infos);
1700 static int compare_machine_info(const void *a, const void *b) {
1701 const struct machine_info *u = a, *v = b;
1703 if (u->is_host != v->is_host)
1704 return u->is_host > v->is_host ? -1 : 1;
1706 return strcasecmp(u->name, v->name);
1709 static int get_machine_properties(sd_bus *bus, struct machine_info *mi) {
1710 _cleanup_bus_close_unref_ sd_bus *container = NULL;
1716 r = sd_bus_open_system_machine(&container, mi->name);
1723 r = bus_map_all_properties(bus, "org.freedesktop.systemd1", "/org/freedesktop/systemd1", machine_info_property_map, mi);
1730 static bool output_show_machine(const char *name, char **patterns) {
1735 if (strv_isempty(patterns))
1738 STRV_FOREACH(i, patterns)
1739 if (fnmatch(*i, name, FNM_NOESCAPE) == 0)
1745 static int get_machine_list(
1747 struct machine_info **_machine_infos,
1750 struct machine_info *machine_infos = NULL;
1751 _cleanup_strv_free_ char **m = NULL;
1752 _cleanup_free_ char *hn = NULL;
1757 hn = gethostname_malloc();
1761 if (output_show_machine(hn, patterns)) {
1762 if (!GREEDY_REALLOC0(machine_infos, sz, c+1))
1765 machine_infos[c].is_host = true;
1766 machine_infos[c].name = hn;
1769 get_machine_properties(bus, &machine_infos[c]);
1773 sd_get_machine_names(&m);
1774 STRV_FOREACH(i, m) {
1775 _cleanup_free_ char *class = NULL;
1777 if (!output_show_machine(*i, patterns))
1780 sd_machine_get_class(*i, &class);
1781 if (!streq_ptr(class, "container"))
1784 if (!GREEDY_REALLOC0(machine_infos, sz, c+1)) {
1785 free_machines_list(machine_infos, c);
1789 machine_infos[c].is_host = false;
1790 machine_infos[c].name = strdup(*i);
1791 if (!machine_infos[c].name) {
1792 free_machines_list(machine_infos, c);
1796 get_machine_properties(NULL, &machine_infos[c]);
1800 *_machine_infos = machine_infos;
1804 static void output_machines_list(struct machine_info *machine_infos, unsigned n) {
1805 struct machine_info *m;
1808 namelen = sizeof("NAME") - 1,
1809 statelen = sizeof("STATE") - 1,
1810 failedlen = sizeof("FAILED") - 1,
1811 jobslen = sizeof("JOBS") - 1;
1813 assert(machine_infos || n == 0);
1815 for (m = machine_infos; m < machine_infos + n; m++) {
1816 namelen = MAX(namelen, strlen(m->name) + (m->is_host ? sizeof(" (host)") - 1 : 0));
1817 statelen = MAX(statelen, m->state ? strlen(m->state) : 0);
1818 failedlen = MAX(failedlen, DECIMAL_STR_WIDTH(m->n_failed_units));
1819 jobslen = MAX(jobslen, DECIMAL_STR_WIDTH(m->n_jobs));
1821 if (!arg_no_legend && !streq_ptr(m->state, "running"))
1825 if (!arg_no_legend) {
1829 printf("%-*s %-*s %-*s %-*s\n",
1832 failedlen, "FAILED",
1836 for (m = machine_infos; m < machine_infos + n; m++) {
1837 const char *on_state = "", *off_state = "";
1838 const char *on_failed = "", *off_failed = "";
1839 bool circle = false;
1841 if (streq_ptr(m->state, "degraded")) {
1842 on_state = ansi_highlight_red();
1843 off_state = ansi_highlight_off();
1845 } else if (!streq_ptr(m->state, "running")) {
1846 on_state = ansi_highlight_yellow();
1847 off_state = ansi_highlight_off();
1851 if (m->n_failed_units > 0) {
1852 on_failed = ansi_highlight_red();
1853 off_failed = ansi_highlight_off();
1855 on_failed = off_failed = "";
1858 printf("%s%s%s ", on_state, circle ? draw_special_char(DRAW_BLACK_CIRCLE) : " ", off_state);
1861 printf("%-*s (host) %s%-*s%s %s%*u%s %*u\n",
1862 (int) (namelen - (sizeof(" (host)")-1)), strna(m->name),
1863 on_state, statelen, strna(m->state), off_state,
1864 on_failed, failedlen, m->n_failed_units, off_failed,
1865 jobslen, m->n_jobs);
1867 printf("%-*s %s%-*s%s %s%*u%s %*u\n",
1868 namelen, strna(m->name),
1869 on_state, statelen, strna(m->state), off_state,
1870 on_failed, failedlen, m->n_failed_units, off_failed,
1871 jobslen, m->n_jobs);
1875 printf("\n%u machines listed.\n", n);
1878 static int list_machines(sd_bus *bus, char **args) {
1879 struct machine_info *machine_infos = NULL;
1884 if (geteuid() != 0) {
1885 log_error("Must be root.");
1889 pager_open_if_enabled();
1891 r = get_machine_list(bus, &machine_infos, strv_skip_first(args));
1895 qsort_safe(machine_infos, r, sizeof(struct machine_info), compare_machine_info);
1896 output_machines_list(machine_infos, r);
1897 free_machines_list(machine_infos, r);
1902 static int get_default(sd_bus *bus, char **args) {
1903 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
1904 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
1905 _cleanup_free_ char *_path = NULL;
1909 if (!bus || avoid_bus()) {
1910 r = unit_file_get_default(arg_scope, arg_root, &_path);
1912 return log_error_errno(r, "Failed to get default target: %m");
1916 r = sd_bus_call_method(
1918 "org.freedesktop.systemd1",
1919 "/org/freedesktop/systemd1",
1920 "org.freedesktop.systemd1.Manager",
1926 log_error("Failed to get default target: %s", bus_error_message(&error, -r));
1930 r = sd_bus_message_read(reply, "s", &path);
1932 return bus_log_parse_error(r);
1936 printf("%s\n", path);
1941 static void dump_unit_file_changes(const UnitFileChange *changes, unsigned n_changes) {
1944 assert(changes || n_changes == 0);
1946 for (i = 0; i < n_changes; i++) {
1947 if (changes[i].type == UNIT_FILE_SYMLINK)
1948 log_info("Created symlink from %s to %s.", changes[i].path, changes[i].source);
1950 log_info("Removed symlink %s.", changes[i].path);
1954 static int deserialize_and_dump_unit_file_changes(sd_bus_message *m) {
1955 const char *type, *path, *source;
1958 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(sss)");
1960 return bus_log_parse_error(r);
1962 while ((r = sd_bus_message_read(m, "(sss)", &type, &path, &source)) > 0) {
1964 if (streq(type, "symlink"))
1965 log_info("Created symlink from %s to %s.", path, source);
1967 log_info("Removed symlink %s.", path);
1971 return bus_log_parse_error(r);
1973 r = sd_bus_message_exit_container(m);
1975 return bus_log_parse_error(r);
1980 static int set_default(sd_bus *bus, char **args) {
1981 _cleanup_free_ char *unit = NULL;
1982 UnitFileChange *changes = NULL;
1983 unsigned n_changes = 0;
1986 unit = unit_name_mangle_with_suffix(args[1], MANGLE_NOGLOB, ".target");
1990 if (!bus || avoid_bus()) {
1991 r = unit_file_set_default(arg_scope, arg_root, unit, true, &changes, &n_changes);
1993 return log_error_errno(r, "Failed to set default target: %m");
1996 dump_unit_file_changes(changes, n_changes);
2000 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL, *m = NULL;
2001 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
2003 r = sd_bus_message_new_method_call(
2006 "org.freedesktop.systemd1",
2007 "/org/freedesktop/systemd1",
2008 "org.freedesktop.systemd1.Manager",
2009 "SetDefaultTarget");
2011 return bus_log_create_error(r);
2013 r = sd_bus_message_set_allow_interactive_authorization(m, arg_ask_password);
2015 return bus_log_create_error(r);
2017 r = sd_bus_message_append(m, "sb", unit, 1);
2019 return bus_log_create_error(r);
2021 r = sd_bus_call(bus, m, 0, &error, &reply);
2023 log_error("Failed to set default target: %s", bus_error_message(&error, -r));
2027 r = deserialize_and_dump_unit_file_changes(reply);
2031 /* Try to reload if enabled */
2033 r = daemon_reload(bus, args);
2038 unit_file_changes_free(changes, n_changes);
2045 const char *name, *type, *state;
2048 static void output_jobs_list(const struct job_info* jobs, unsigned n, bool skipped) {
2049 unsigned id_len, unit_len, type_len, state_len;
2050 const struct job_info *j;
2051 const char *on, *off;
2052 bool shorten = false;
2054 assert(n == 0 || jobs);
2057 on = ansi_highlight_green();
2058 off = ansi_highlight_off();
2060 printf("%sNo jobs %s.%s\n", on, skipped ? "listed" : "running", off);
2064 pager_open_if_enabled();
2066 id_len = strlen("JOB");
2067 unit_len = strlen("UNIT");
2068 type_len = strlen("TYPE");
2069 state_len = strlen("STATE");
2071 for (j = jobs; j < jobs + n; j++) {
2072 uint32_t id = j->id;
2073 assert(j->name && j->type && j->state);
2075 id_len = MAX(id_len, DECIMAL_STR_WIDTH(id));
2076 unit_len = MAX(unit_len, strlen(j->name));
2077 type_len = MAX(type_len, strlen(j->type));
2078 state_len = MAX(state_len, strlen(j->state));
2081 if (!arg_full && id_len + 1 + unit_len + type_len + 1 + state_len > columns()) {
2082 unit_len = MAX(33u, columns() - id_len - type_len - state_len - 3);
2087 printf("%*s %-*s %-*s %-*s\n",
2091 state_len, "STATE");
2093 for (j = jobs; j < jobs + n; j++) {
2094 _cleanup_free_ char *e = NULL;
2096 if (streq(j->state, "running")) {
2097 on = ansi_highlight();
2098 off = ansi_highlight_off();
2102 e = shorten ? ellipsize(j->name, unit_len, 33) : NULL;
2103 printf("%*u %s%-*s%s %-*s %s%-*s%s\n",
2105 on, unit_len, e ? e : j->name, off,
2107 on, state_len, j->state, off);
2110 if (!arg_no_legend) {
2111 on = ansi_highlight();
2112 off = ansi_highlight_off();
2114 printf("\n%s%u jobs listed%s.\n", on, n, off);
2118 static bool output_show_job(struct job_info *job, char **patterns) {
2123 if (strv_isempty(patterns))
2126 STRV_FOREACH(pattern, patterns)
2127 if (fnmatch(*pattern, job->name, FNM_NOESCAPE) == 0)
2132 static int list_jobs(sd_bus *bus, char **args) {
2133 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
2134 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
2135 const char *name, *type, *state, *job_path, *unit_path;
2136 _cleanup_free_ struct job_info *jobs = NULL;
2141 bool skipped = false;
2143 r = sd_bus_call_method(
2145 "org.freedesktop.systemd1",
2146 "/org/freedesktop/systemd1",
2147 "org.freedesktop.systemd1.Manager",
2153 log_error("Failed to list jobs: %s", bus_error_message(&error, r));
2157 r = sd_bus_message_enter_container(reply, 'a', "(usssoo)");
2159 return bus_log_parse_error(r);
2161 while ((r = sd_bus_message_read(reply, "(usssoo)", &id, &name, &type, &state, &job_path, &unit_path)) > 0) {
2162 struct job_info job = { id, name, type, state };
2164 if (!output_show_job(&job, strv_skip_first(args))) {
2169 if (!GREEDY_REALLOC(jobs, size, c + 1))
2175 return bus_log_parse_error(r);
2177 r = sd_bus_message_exit_container(reply);
2179 return bus_log_parse_error(r);
2181 output_jobs_list(jobs, c, skipped);
2185 static int cancel_job(sd_bus *bus, char **args) {
2186 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
2192 if (strv_length(args) <= 1)
2193 return daemon_reload(bus, args);
2195 STRV_FOREACH(name, args+1) {
2196 _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
2200 q = safe_atou32(*name, &id);
2202 return log_error_errno(q, "Failed to parse job id \"%s\": %m", *name);
2204 q = sd_bus_message_new_method_call(
2207 "org.freedesktop.systemd1",
2208 "/org/freedesktop/systemd1",
2209 "org.freedesktop.systemd1.Manager",
2212 return bus_log_create_error(q);
2214 q = sd_bus_message_set_allow_interactive_authorization(m, arg_ask_password);
2216 return bus_log_create_error(1);
2218 q = sd_bus_message_append(m, "u", id);
2220 return bus_log_create_error(q);
2222 q = sd_bus_call(bus, m, 0, &error, NULL);
2224 log_error("Failed to cancel job %"PRIu32": %s", id, bus_error_message(&error, q));
2233 static int need_daemon_reload(sd_bus *bus, const char *unit) {
2234 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
2238 /* We ignore all errors here, since this is used to show a
2241 /* We don't use unit_dbus_path_from_name() directly since we
2242 * don't want to load the unit if it isn't loaded. */
2244 r = sd_bus_call_method(
2246 "org.freedesktop.systemd1",
2247 "/org/freedesktop/systemd1",
2248 "org.freedesktop.systemd1.Manager",
2256 r = sd_bus_message_read(reply, "o", &path);
2260 r = sd_bus_get_property_trivial(
2262 "org.freedesktop.systemd1",
2264 "org.freedesktop.systemd1.Unit",
2274 static void warn_unit_file_changed(const char *name) {
2275 log_warning("%sWarning:%s %s changed on disk. Run 'systemctl%s daemon-reload' to reload units.",
2276 ansi_highlight_red(),
2277 ansi_highlight_off(),
2279 arg_scope == UNIT_FILE_SYSTEM ? "" : " --user");
2282 static int unit_file_find_path(LookupPaths *lp, const char *unit_name, char **unit_path) {
2289 STRV_FOREACH(p, lp->unit_path) {
2290 _cleanup_free_ char *path;
2292 path = path_join(arg_root, *p, unit_name);
2296 if (access(path, F_OK) == 0) {
2306 static int unit_find_paths(sd_bus *bus,
2307 const char *unit_name,
2308 bool avoid_bus_cache,
2310 char **fragment_path,
2311 char ***dropin_paths) {
2315 * Finds where the unit is defined on disk. Returns 0 if the unit
2316 * is not found. Returns 1 if it is found, and sets
2317 * - the path to the unit in *path, if it exists on disk,
2318 * - and a strv of existing drop-ins in *dropins,
2319 * if the arg is not NULL and any dropins were found.
2323 assert(fragment_path);
2326 if (!avoid_bus_cache && !unit_name_is_template(unit_name)) {
2327 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
2328 _cleanup_free_ char *unit = NULL;
2329 _cleanup_free_ char *path = NULL;
2330 _cleanup_strv_free_ char **dropins = NULL;
2332 unit = unit_dbus_path_from_name(unit_name);
2336 if (need_daemon_reload(bus, unit_name) > 0)
2337 warn_unit_file_changed(unit_name);
2339 r = sd_bus_get_property_string(
2341 "org.freedesktop.systemd1",
2343 "org.freedesktop.systemd1.Unit",
2348 return log_error_errno(r, "Failed to get FragmentPath: %s", bus_error_message(&error, r));
2350 r = sd_bus_get_property_strv(
2352 "org.freedesktop.systemd1",
2354 "org.freedesktop.systemd1.Unit",
2359 return log_error_errno(r, "Failed to get DropInPaths: %s", bus_error_message(&error, r));
2362 if (!isempty(path)) {
2363 *fragment_path = path;
2368 if (dropin_paths && !strv_isempty(dropins)) {
2369 *dropin_paths = dropins;
2374 _cleanup_set_free_ Set *names;
2376 names = set_new(NULL);
2380 r = set_put(names, unit_name);
2384 r = unit_file_find_path(lp, unit_name, fragment_path);
2389 _cleanup_free_ char *template;
2391 template = unit_name_template(unit_name);
2395 if (!streq(template, unit_name)) {
2396 r = unit_file_find_path(lp, template, fragment_path);
2403 r = unit_file_find_dropin_paths(lp->unit_path, NULL, names, dropin_paths);
2409 static int check_one_unit(sd_bus *bus, const char *name, const char *good_states, bool quiet) {
2410 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
2411 _cleanup_free_ char *n = NULL, *state = NULL;
2417 n = unit_name_mangle(name, MANGLE_NOGLOB);
2421 /* We don't use unit_dbus_path_from_name() directly since we
2422 * don't want to load the unit if it isn't loaded. */
2424 r = sd_bus_call_method(
2426 "org.freedesktop.systemd1",
2427 "/org/freedesktop/systemd1",
2428 "org.freedesktop.systemd1.Manager",
2439 r = sd_bus_message_read(reply, "o", &path);
2441 return bus_log_parse_error(r);
2443 r = sd_bus_get_property_string(
2445 "org.freedesktop.systemd1",
2447 "org.freedesktop.systemd1.Unit",
2460 return nulstr_contains(good_states, state);
2463 static int check_triggering_units(
2467 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
2468 _cleanup_free_ char *path = NULL, *n = NULL, *state = NULL;
2469 _cleanup_strv_free_ char **triggered_by = NULL;
2470 bool print_warning_label = true;
2474 n = unit_name_mangle(name, MANGLE_NOGLOB);
2478 path = unit_dbus_path_from_name(n);
2482 r = sd_bus_get_property_string(
2484 "org.freedesktop.systemd1",
2486 "org.freedesktop.systemd1.Unit",
2491 log_error("Failed to get load state of %s: %s", n, bus_error_message(&error, r));
2495 if (streq(state, "masked"))
2498 r = sd_bus_get_property_strv(
2500 "org.freedesktop.systemd1",
2502 "org.freedesktop.systemd1.Unit",
2507 log_error("Failed to get triggered by array of %s: %s", n, bus_error_message(&error, r));
2511 STRV_FOREACH(i, triggered_by) {
2512 r = check_one_unit(bus, *i, "active\0reloading\0", true);
2514 return log_error_errno(r, "Failed to check unit: %m");
2519 if (print_warning_label) {
2520 log_warning("Warning: Stopping %s, but it can still be activated by:", n);
2521 print_warning_label = false;
2524 log_warning(" %s", *i);
2530 static const struct {
2533 } unit_actions[] = {
2534 { "start", "StartUnit" },
2535 { "stop", "StopUnit" },
2536 { "condstop", "StopUnit" },
2537 { "reload", "ReloadUnit" },
2538 { "restart", "RestartUnit" },
2539 { "try-restart", "TryRestartUnit" },
2540 { "condrestart", "TryRestartUnit" },
2541 { "reload-or-restart", "ReloadOrRestartUnit" },
2542 { "reload-or-try-restart", "ReloadOrTryRestartUnit" },
2543 { "condreload", "ReloadOrTryRestartUnit" },
2544 { "force-reload", "ReloadOrTryRestartUnit" }
2547 static const char *verb_to_method(const char *verb) {
2550 for (i = 0; i < ELEMENTSOF(unit_actions); i++)
2551 if (streq_ptr(unit_actions[i].verb, verb))
2552 return unit_actions[i].method;
2557 static const char *method_to_verb(const char *method) {
2560 for (i = 0; i < ELEMENTSOF(unit_actions); i++)
2561 if (streq_ptr(unit_actions[i].method, method))
2562 return unit_actions[i].verb;
2567 static int start_unit_one(
2572 sd_bus_error *error,
2573 BusWaitForJobs *w) {
2575 _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
2584 log_debug("Calling manager for %s on %s, %s", method, name, mode);
2586 r = sd_bus_message_new_method_call(
2589 "org.freedesktop.systemd1",
2590 "/org/freedesktop/systemd1",
2591 "org.freedesktop.systemd1.Manager",
2594 return bus_log_create_error(r);
2596 r = sd_bus_message_set_allow_interactive_authorization(m, arg_ask_password);
2598 return bus_log_create_error(r);
2600 r = sd_bus_message_append(m, "ss", name, mode);
2602 return bus_log_create_error(r);
2604 r = sd_bus_call(bus, m, 0, error, &reply);
2608 if (r == -ENOENT && arg_action != ACTION_SYSTEMCTL)
2609 /* There's always a fallback possible for
2610 * legacy actions. */
2611 return -EADDRNOTAVAIL;
2613 verb = method_to_verb(method);
2615 log_error("Failed to %s %s: %s", verb, name, bus_error_message(error, r));
2619 r = sd_bus_message_read(reply, "o", &path);
2621 return bus_log_parse_error(r);
2623 if (need_daemon_reload(bus, name) > 0)
2624 warn_unit_file_changed(name);
2627 log_debug("Adding %s to the set", path);
2628 r = bus_wait_for_jobs_add(w, path);
2636 static int expand_names(sd_bus *bus, char **names, const char* suffix, char ***ret) {
2638 _cleanup_strv_free_ char **mangled = NULL, **globs = NULL;
2642 STRV_FOREACH(name, names) {
2646 t = unit_name_mangle_with_suffix(*name, MANGLE_GLOB, suffix);
2648 t = unit_name_mangle(*name, MANGLE_GLOB);
2652 if (string_is_glob(t))
2653 r = strv_consume(&globs, t);
2655 r = strv_consume(&mangled, t);
2660 /* Query the manager only if any of the names are a glob, since
2661 * this is fairly expensive */
2662 if (!strv_isempty(globs)) {
2663 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
2664 _cleanup_free_ UnitInfo *unit_infos = NULL;
2667 return log_error_errno(ENOTSUP, "Unit name globbing without bus is not implemented.");
2669 r = get_unit_list(bus, NULL, globs, &unit_infos, 0, &reply);
2673 for (i = 0; i < r; i++)
2674 if (strv_extend(&mangled, unit_infos[i].id) < 0)
2679 mangled = NULL; /* do not free */
2684 static const struct {
2688 } action_table[_ACTION_MAX] = {
2689 [ACTION_HALT] = { SPECIAL_HALT_TARGET, "halt", "replace-irreversibly" },
2690 [ACTION_POWEROFF] = { SPECIAL_POWEROFF_TARGET, "poweroff", "replace-irreversibly" },
2691 [ACTION_REBOOT] = { SPECIAL_REBOOT_TARGET, "reboot", "replace-irreversibly" },
2692 [ACTION_KEXEC] = { SPECIAL_KEXEC_TARGET, "kexec", "replace-irreversibly" },
2693 [ACTION_RUNLEVEL2] = { SPECIAL_RUNLEVEL2_TARGET, NULL, "isolate" },
2694 [ACTION_RUNLEVEL3] = { SPECIAL_RUNLEVEL3_TARGET, NULL, "isolate" },
2695 [ACTION_RUNLEVEL4] = { SPECIAL_RUNLEVEL4_TARGET, NULL, "isolate" },
2696 [ACTION_RUNLEVEL5] = { SPECIAL_RUNLEVEL5_TARGET, NULL, "isolate" },
2697 [ACTION_RESCUE] = { SPECIAL_RESCUE_TARGET, "rescue", "isolate" },
2698 [ACTION_EMERGENCY] = { SPECIAL_EMERGENCY_TARGET, "emergency", "isolate" },
2699 [ACTION_DEFAULT] = { SPECIAL_DEFAULT_TARGET, "default", "isolate" },
2700 [ACTION_EXIT] = { SPECIAL_EXIT_TARGET, "exit", "replace-irreversibly" },
2701 [ACTION_SUSPEND] = { SPECIAL_SUSPEND_TARGET, "suspend", "replace-irreversibly" },
2702 [ACTION_HIBERNATE] = { SPECIAL_HIBERNATE_TARGET, "hibernate", "replace-irreversibly" },
2703 [ACTION_HYBRID_SLEEP] = { SPECIAL_HYBRID_SLEEP_TARGET, "hybrid-sleep", "replace-irreversibly" },
2706 static enum action verb_to_action(const char *verb) {
2709 for (i = _ACTION_INVALID; i < _ACTION_MAX; i++)
2710 if (streq_ptr(action_table[i].verb, verb))
2713 return _ACTION_INVALID;
2716 static int start_unit(sd_bus *bus, char **args) {
2717 _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL;
2718 const char *method, *mode, *one_name, *suffix = NULL;
2719 _cleanup_strv_free_ char **names = NULL;
2725 ask_password_agent_open_if_enabled();
2727 if (arg_action == ACTION_SYSTEMCTL) {
2729 method = verb_to_method(args[0]);
2730 action = verb_to_action(args[0]);
2732 if (streq(args[0], "isolate")) {
2736 mode = action_table[action].mode ?: arg_job_mode;
2738 one_name = action_table[action].target;
2740 assert(arg_action < ELEMENTSOF(action_table));
2741 assert(action_table[arg_action].target);
2743 method = "StartUnit";
2745 mode = action_table[arg_action].mode;
2746 one_name = action_table[arg_action].target;
2750 names = strv_new(one_name, NULL);
2752 r = expand_names(bus, args + 1, suffix, &names);
2754 log_error_errno(r, "Failed to expand names: %m");
2757 if (!arg_no_block) {
2758 r = bus_wait_for_jobs_new(bus, &w);
2760 return log_error_errno(r, "Could not watch jobs: %m");
2763 STRV_FOREACH(name, names) {
2764 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
2767 q = start_unit_one(bus, method, *name, mode, &error, w);
2768 if (r >= 0 && q < 0)
2769 r = translate_bus_error_to_exit_status(q, &error);
2772 if (!arg_no_block) {
2775 q = bus_wait_for_jobs(w, arg_quiet);
2779 /* When stopping units, warn if they can still be triggered by
2780 * another active unit (socket, path, timer) */
2781 if (!arg_quiet && streq(method, "StopUnit"))
2782 STRV_FOREACH(name, names)
2783 check_triggering_units(bus, *name);
2789 /* Ask systemd-logind, which might grant access to unprivileged users
2790 * through PolicyKit */
2791 static int reboot_with_logind(sd_bus *bus, enum action a) {
2793 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
2800 polkit_agent_open_if_enabled();
2808 case ACTION_POWEROFF:
2809 method = "PowerOff";
2812 case ACTION_SUSPEND:
2816 case ACTION_HIBERNATE:
2817 method = "Hibernate";
2820 case ACTION_HYBRID_SLEEP:
2821 method = "HybridSleep";
2828 r = sd_bus_call_method(
2830 "org.freedesktop.login1",
2831 "/org/freedesktop/login1",
2832 "org.freedesktop.login1.Manager",
2836 "b", arg_ask_password);
2838 log_error("Failed to execute operation: %s", bus_error_message(&error, r));
2846 static int check_inhibitors(sd_bus *bus, enum action a) {
2848 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
2849 _cleanup_strv_free_ char **sessions = NULL;
2850 const char *what, *who, *why, *mode;
2859 if (arg_ignore_inhibitors || arg_force > 0)
2871 r = sd_bus_call_method(
2873 "org.freedesktop.login1",
2874 "/org/freedesktop/login1",
2875 "org.freedesktop.login1.Manager",
2881 /* If logind is not around, then there are no inhibitors... */
2884 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssuu)");
2886 return bus_log_parse_error(r);
2888 while ((r = sd_bus_message_read(reply, "(ssssuu)", &what, &who, &why, &mode, &uid, &pid)) > 0) {
2889 _cleanup_free_ char *comm = NULL, *user = NULL;
2890 _cleanup_strv_free_ char **sv = NULL;
2892 if (!streq(mode, "block"))
2895 sv = strv_split(what, ":");
2899 if (!strv_contains(sv,
2901 a == ACTION_POWEROFF ||
2902 a == ACTION_REBOOT ||
2903 a == ACTION_KEXEC ? "shutdown" : "sleep"))
2906 get_process_comm(pid, &comm);
2907 user = uid_to_name(uid);
2909 log_warning("Operation inhibited by \"%s\" (PID "PID_FMT" \"%s\", user %s), reason is \"%s\".",
2910 who, pid, strna(comm), strna(user), why);
2915 return bus_log_parse_error(r);
2917 r = sd_bus_message_exit_container(reply);
2919 return bus_log_parse_error(r);
2921 /* Check for current sessions */
2922 sd_get_sessions(&sessions);
2923 STRV_FOREACH(s, sessions) {
2924 _cleanup_free_ char *type = NULL, *tty = NULL, *seat = NULL, *user = NULL, *service = NULL, *class = NULL;
2926 if (sd_session_get_uid(*s, &uid) < 0 || uid == getuid())
2929 if (sd_session_get_class(*s, &class) < 0 || !streq(class, "user"))
2932 if (sd_session_get_type(*s, &type) < 0 || (!streq(type, "x11") && !streq(type, "tty")))
2935 sd_session_get_tty(*s, &tty);
2936 sd_session_get_seat(*s, &seat);
2937 sd_session_get_service(*s, &service);
2938 user = uid_to_name(uid);
2940 log_warning("User %s is logged in on %s.", strna(user), isempty(tty) ? (isempty(seat) ? strna(service) : seat) : tty);
2947 log_error("Please retry operation after closing inhibitors and logging out other users.\nAlternatively, ignore inhibitors and users with 'systemctl %s -i'.",
2948 action_table[a].verb);
2956 static int start_special(sd_bus *bus, char **args) {
2962 a = verb_to_action(args[0]);
2964 r = check_inhibitors(bus, a);
2968 if (arg_force >= 2 && geteuid() != 0) {
2969 log_error("Must be root.");
2973 if (arg_force >= 2 &&
2974 (a == ACTION_HALT ||
2975 a == ACTION_POWEROFF ||
2976 a == ACTION_REBOOT))
2979 if (arg_force >= 1 &&
2980 (a == ACTION_HALT ||
2981 a == ACTION_POWEROFF ||
2982 a == ACTION_REBOOT ||
2983 a == ACTION_KEXEC ||
2985 return daemon_reload(bus, args);
2987 /* first try logind, to allow authentication with polkit */
2988 if (geteuid() != 0 &&
2989 (a == ACTION_POWEROFF ||
2990 a == ACTION_REBOOT ||
2991 a == ACTION_SUSPEND ||
2992 a == ACTION_HIBERNATE ||
2993 a == ACTION_HYBRID_SLEEP)) {
2994 r = reboot_with_logind(bus, a);
2999 r = start_unit(bus, args);
3000 if (r == EXIT_SUCCESS)
3006 static int check_unit_generic(sd_bus *bus, int code, const char *good_states, char **args) {
3007 _cleanup_strv_free_ char **names = NULL;
3014 r = expand_names(bus, args, NULL, &names);
3016 return log_error_errno(r, "Failed to expand names: %m");
3018 STRV_FOREACH(name, names) {
3021 state = check_one_unit(bus, *name, good_states, arg_quiet);
3031 static int check_unit_active(sd_bus *bus, char **args) {
3032 /* According to LSB: 3, "program is not running" */
3033 return check_unit_generic(bus, 3, "active\0reloading\0", args + 1);
3036 static int check_unit_failed(sd_bus *bus, char **args) {
3037 return check_unit_generic(bus, 1, "failed\0", args + 1);
3040 static int kill_unit(sd_bus *bus, char **args) {
3041 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
3042 _cleanup_strv_free_ char **names = NULL;
3050 arg_kill_who = "all";
3052 r = expand_names(bus, args + 1, NULL, &names);
3054 log_error_errno(r, "Failed to expand names: %m");
3056 STRV_FOREACH(name, names) {
3057 _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
3059 q = sd_bus_message_new_method_call(
3062 "org.freedesktop.systemd1",
3063 "/org/freedesktop/systemd1",
3064 "org.freedesktop.systemd1.Manager",
3067 return bus_log_create_error(q);
3069 q = sd_bus_message_set_allow_interactive_authorization(m, arg_ask_password);
3071 return bus_log_create_error(q);
3073 q = sd_bus_message_append(m, "ssi", *names, arg_kill_who, arg_signal);
3075 return bus_log_create_error(q);
3077 q = sd_bus_call(bus, m, 0, &error, NULL);
3079 log_error("Failed to kill unit %s: %s", *names, bus_error_message(&error, q));
3088 typedef struct ExecStatusInfo {
3096 usec_t start_timestamp;
3097 usec_t exit_timestamp;
3102 LIST_FIELDS(struct ExecStatusInfo, exec);
3105 static void exec_status_info_free(ExecStatusInfo *i) {
3114 static int exec_status_info_deserialize(sd_bus_message *m, ExecStatusInfo *i) {
3115 uint64_t start_timestamp, exit_timestamp, start_timestamp_monotonic, exit_timestamp_monotonic;
3118 int32_t code, status;
3124 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_STRUCT, "sasbttttuii");
3126 return bus_log_parse_error(r);
3130 r = sd_bus_message_read(m, "s", &path);
3132 return bus_log_parse_error(r);
3134 i->path = strdup(path);
3138 r = sd_bus_message_read_strv(m, &i->argv);
3140 return bus_log_parse_error(r);
3142 r = sd_bus_message_read(m,
3145 &start_timestamp, &start_timestamp_monotonic,
3146 &exit_timestamp, &exit_timestamp_monotonic,
3150 return bus_log_parse_error(r);
3153 i->start_timestamp = (usec_t) start_timestamp;
3154 i->exit_timestamp = (usec_t) exit_timestamp;
3155 i->pid = (pid_t) pid;
3159 r = sd_bus_message_exit_container(m);
3161 return bus_log_parse_error(r);
3166 typedef struct UnitStatusInfo {
3168 const char *load_state;
3169 const char *active_state;
3170 const char *sub_state;
3171 const char *unit_file_state;
3172 const char *unit_file_preset;
3174 const char *description;
3175 const char *following;
3177 char **documentation;
3179 const char *fragment_path;
3180 const char *source_path;
3181 const char *control_group;
3183 char **dropin_paths;
3185 const char *load_error;
3188 usec_t inactive_exit_timestamp;
3189 usec_t inactive_exit_timestamp_monotonic;
3190 usec_t active_enter_timestamp;
3191 usec_t active_exit_timestamp;
3192 usec_t inactive_enter_timestamp;
3194 bool need_daemon_reload;
3199 const char *status_text;
3200 const char *pid_file;
3204 usec_t start_timestamp;
3205 usec_t exit_timestamp;
3207 int exit_code, exit_status;
3209 usec_t condition_timestamp;
3210 bool condition_result;
3211 bool failed_condition_trigger;
3212 bool failed_condition_negate;
3213 const char *failed_condition;
3214 const char *failed_condition_parameter;
3216 usec_t assert_timestamp;
3218 bool failed_assert_trigger;
3219 bool failed_assert_negate;
3220 const char *failed_assert;
3221 const char *failed_assert_parameter;
3224 unsigned n_accepted;
3225 unsigned n_connections;
3228 /* Pairs of type, path */
3232 const char *sysfs_path;
3234 /* Mount, Automount */
3240 LIST_HEAD(ExecStatusInfo, exec);
3243 static void print_status_info(
3248 const char *active_on, *active_off, *on, *off, *ss;
3250 char since1[FORMAT_TIMESTAMP_RELATIVE_MAX], *s1;
3251 char since2[FORMAT_TIMESTAMP_MAX], *s2;
3254 arg_all * OUTPUT_SHOW_ALL |
3255 (!on_tty() || pager_have()) * OUTPUT_FULL_WIDTH |
3256 on_tty() * OUTPUT_COLOR |
3257 !arg_quiet * OUTPUT_WARN_CUTOFF |
3258 arg_full * OUTPUT_FULL_WIDTH;
3263 /* This shows pretty information about a unit. See
3264 * print_property() for a low-level property printer */
3266 if (streq_ptr(i->active_state, "failed")) {
3267 active_on = ansi_highlight_red();
3268 active_off = ansi_highlight_off();
3269 } else if (streq_ptr(i->active_state, "active") || streq_ptr(i->active_state, "reloading")) {
3270 active_on = ansi_highlight_green();
3271 active_off = ansi_highlight_off();
3273 active_on = active_off = "";
3275 printf("%s%s%s %s", active_on, draw_special_char(DRAW_BLACK_CIRCLE), active_off, strna(i->id));
3277 if (i->description && !streq_ptr(i->id, i->description))
3278 printf(" - %s", i->description);
3283 printf(" Follow: unit currently follows state of %s\n", i->following);
3285 if (streq_ptr(i->load_state, "error")) {
3286 on = ansi_highlight_red();
3287 off = ansi_highlight_off();
3291 path = i->source_path ? i->source_path : i->fragment_path;
3294 printf(" Loaded: %s%s%s (Reason: %s)\n",
3295 on, strna(i->load_state), off, i->load_error);
3296 else if (path && !isempty(i->unit_file_state) && !isempty(i->unit_file_preset))
3297 printf(" Loaded: %s%s%s (%s; %s; vendor preset: %s)\n",
3298 on, strna(i->load_state), off, path, i->unit_file_state, i->unit_file_preset);
3299 else if (path && !isempty(i->unit_file_state))
3300 printf(" Loaded: %s%s%s (%s; %s)\n",
3301 on, strna(i->load_state), off, path, i->unit_file_state);
3303 printf(" Loaded: %s%s%s (%s)\n",
3304 on, strna(i->load_state), off, path);
3306 printf(" Loaded: %s%s%s\n",
3307 on, strna(i->load_state), off);
3309 if (!strv_isempty(i->dropin_paths)) {
3310 _cleanup_free_ char *dir = NULL;
3314 STRV_FOREACH(dropin, i->dropin_paths) {
3315 if (! dir || last) {
3316 printf(dir ? " " : " Drop-In: ");
3321 if (path_get_parent(*dropin, &dir) < 0) {
3326 printf("%s\n %s", dir,
3327 draw_special_char(DRAW_TREE_RIGHT));
3330 last = ! (*(dropin + 1) && startswith(*(dropin + 1), dir));
3332 printf("%s%s", basename(*dropin), last ? "\n" : ", ");
3336 ss = streq_ptr(i->active_state, i->sub_state) ? NULL : i->sub_state;
3338 printf(" Active: %s%s (%s)%s",
3339 active_on, strna(i->active_state), ss, active_off);
3341 printf(" Active: %s%s%s",
3342 active_on, strna(i->active_state), active_off);
3344 if (!isempty(i->result) && !streq(i->result, "success"))
3345 printf(" (Result: %s)", i->result);
3347 timestamp = (streq_ptr(i->active_state, "active") ||
3348 streq_ptr(i->active_state, "reloading")) ? i->active_enter_timestamp :
3349 (streq_ptr(i->active_state, "inactive") ||
3350 streq_ptr(i->active_state, "failed")) ? i->inactive_enter_timestamp :
3351 streq_ptr(i->active_state, "activating") ? i->inactive_exit_timestamp :
3352 i->active_exit_timestamp;
3354 s1 = format_timestamp_relative(since1, sizeof(since1), timestamp);
3355 s2 = format_timestamp(since2, sizeof(since2), timestamp);
3358 printf(" since %s; %s\n", s2, s1);
3360 printf(" since %s\n", s2);
3364 if (!i->condition_result && i->condition_timestamp > 0) {
3365 s1 = format_timestamp_relative(since1, sizeof(since1), i->condition_timestamp);
3366 s2 = format_timestamp(since2, sizeof(since2), i->condition_timestamp);
3368 printf("Condition: start %scondition failed%s at %s%s%s\n",
3369 ansi_highlight_yellow(), ansi_highlight_off(),
3370 s2, s1 ? "; " : "", s1 ? s1 : "");
3371 if (i->failed_condition_trigger)
3372 printf(" none of the trigger conditions were met\n");
3373 else if (i->failed_condition)
3374 printf(" %s=%s%s was not met\n",
3375 i->failed_condition,
3376 i->failed_condition_negate ? "!" : "",
3377 i->failed_condition_parameter);
3380 if (!i->assert_result && i->assert_timestamp > 0) {
3381 s1 = format_timestamp_relative(since1, sizeof(since1), i->assert_timestamp);
3382 s2 = format_timestamp(since2, sizeof(since2), i->assert_timestamp);
3384 printf(" Assert: start %sassertion failed%s at %s%s%s\n",
3385 ansi_highlight_red(), ansi_highlight_off(),
3386 s2, s1 ? "; " : "", s1 ? s1 : "");
3387 if (i->failed_assert_trigger)
3388 printf(" none of the trigger assertions were met\n");
3389 else if (i->failed_assert)
3390 printf(" %s=%s%s was not met\n",
3392 i->failed_assert_negate ? "!" : "",
3393 i->failed_assert_parameter);
3397 printf(" Device: %s\n", i->sysfs_path);
3399 printf(" Where: %s\n", i->where);
3401 printf(" What: %s\n", i->what);
3403 STRV_FOREACH(t, i->documentation)
3404 printf(" %*s %s\n", 9, t == i->documentation ? "Docs:" : "", *t);
3406 STRV_FOREACH_PAIR(t, t2, i->listen)
3407 printf(" %*s %s (%s)\n", 9, t == i->listen ? "Listen:" : "", *t2, *t);
3410 printf(" Accepted: %u; Connected: %u\n", i->n_accepted, i->n_connections);
3412 LIST_FOREACH(exec, p, i->exec) {
3413 _cleanup_free_ char *argv = NULL;
3416 /* Only show exited processes here */
3420 argv = strv_join(p->argv, " ");
3421 printf(" Process: %u %s=%s ", p->pid, p->name, strna(argv));
3423 good = is_clean_exit_lsb(p->code, p->status, NULL);
3425 on = ansi_highlight_red();
3426 off = ansi_highlight_off();
3430 printf("%s(code=%s, ", on, sigchld_code_to_string(p->code));
3432 if (p->code == CLD_EXITED) {
3435 printf("status=%i", p->status);
3437 c = exit_status_to_string(p->status, EXIT_STATUS_SYSTEMD);
3442 printf("signal=%s", signal_to_string(p->status));
3444 printf(")%s\n", off);
3446 if (i->main_pid == p->pid &&
3447 i->start_timestamp == p->start_timestamp &&
3448 i->exit_timestamp == p->start_timestamp)
3449 /* Let's not show this twice */
3452 if (p->pid == i->control_pid)
3456 if (i->main_pid > 0 || i->control_pid > 0) {
3457 if (i->main_pid > 0) {
3458 printf(" Main PID: "PID_FMT, i->main_pid);
3461 _cleanup_free_ char *comm = NULL;
3462 get_process_comm(i->main_pid, &comm);
3464 printf(" (%s)", comm);
3465 } else if (i->exit_code > 0) {
3466 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
3468 if (i->exit_code == CLD_EXITED) {
3471 printf("status=%i", i->exit_status);
3473 c = exit_status_to_string(i->exit_status, EXIT_STATUS_SYSTEMD);
3478 printf("signal=%s", signal_to_string(i->exit_status));
3482 if (i->control_pid > 0)
3486 if (i->control_pid > 0) {
3487 _cleanup_free_ char *c = NULL;
3489 printf(" %8s: "PID_FMT, i->main_pid ? "" : " Control", i->control_pid);
3491 get_process_comm(i->control_pid, &c);
3500 printf(" Status: \"%s\"\n", i->status_text);
3501 if (i->status_errno > 0)
3502 printf(" Error: %i (%s)\n", i->status_errno, strerror(i->status_errno));
3504 if (i->control_group &&
3505 (i->main_pid > 0 || i->control_pid > 0 ||
3506 ((arg_transport != BUS_TRANSPORT_LOCAL && arg_transport != BUS_TRANSPORT_MACHINE) || cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, i->control_group, false) == 0))) {
3509 printf(" CGroup: %s\n", i->control_group);
3511 if (arg_transport == BUS_TRANSPORT_LOCAL || arg_transport == BUS_TRANSPORT_MACHINE) {
3514 static const char prefix[] = " ";
3517 if (c > sizeof(prefix) - 1)
3518 c -= sizeof(prefix) - 1;
3522 if (i->main_pid > 0)
3523 extra[k++] = i->main_pid;
3525 if (i->control_pid > 0)
3526 extra[k++] = i->control_pid;
3528 show_cgroup_and_extra(SYSTEMD_CGROUP_CONTROLLER, i->control_group, prefix, c, false, extra, k, flags);
3532 if (i->id && arg_transport == BUS_TRANSPORT_LOCAL) {
3533 show_journal_by_unit(stdout,
3537 i->inactive_exit_timestamp_monotonic,
3540 flags | OUTPUT_BEGIN_NEWLINE,
3541 arg_scope == UNIT_FILE_SYSTEM,
3545 if (i->need_daemon_reload)
3546 warn_unit_file_changed(i->id);
3549 static void show_unit_help(UnitStatusInfo *i) {
3554 if (!i->documentation) {
3555 log_info("Documentation for %s not known.", i->id);
3559 STRV_FOREACH(p, i->documentation)
3560 if (startswith(*p, "man:"))
3561 show_man_page(*p + 4, false);
3563 log_info("Can't show: %s", *p);
3566 static int status_property(const char *name, sd_bus_message *m, UnitStatusInfo *i, const char *contents) {
3573 switch (contents[0]) {
3575 case SD_BUS_TYPE_STRING: {
3578 r = sd_bus_message_read(m, "s", &s);
3580 return bus_log_parse_error(r);
3583 if (streq(name, "Id"))
3585 else if (streq(name, "LoadState"))
3587 else if (streq(name, "ActiveState"))
3588 i->active_state = s;
3589 else if (streq(name, "SubState"))
3591 else if (streq(name, "Description"))
3593 else if (streq(name, "FragmentPath"))
3594 i->fragment_path = s;
3595 else if (streq(name, "SourcePath"))
3598 else if (streq(name, "DefaultControlGroup")) {
3600 e = startswith(s, SYSTEMD_CGROUP_CONTROLLER ":");
3602 i->control_group = e;
3605 else if (streq(name, "ControlGroup"))
3606 i->control_group = s;
3607 else if (streq(name, "StatusText"))
3609 else if (streq(name, "PIDFile"))
3611 else if (streq(name, "SysFSPath"))
3613 else if (streq(name, "Where"))
3615 else if (streq(name, "What"))