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"
61 #include "bus-errors.h"
63 #include "unit-name.h"
65 #include "spawn-ask-password-agent.h"
66 #include "spawn-polkit-agent.h"
68 #include "logs-show.h"
69 #include "socket-util.h"
72 #include "bus-message.h"
73 #include "bus-error.h"
74 #include "bus-errors.h"
76 static char **arg_types = NULL;
77 static char **arg_states = NULL;
78 static char **arg_properties = NULL;
79 static bool arg_all = false;
80 static bool original_stdout_is_tty;
81 static enum dependency {
87 } arg_dependency = DEPENDENCY_FORWARD;
88 static const char *arg_job_mode = "replace";
89 static UnitFileScope arg_scope = UNIT_FILE_SYSTEM;
90 static bool arg_no_block = false;
91 static bool arg_no_legend = false;
92 static bool arg_no_pager = false;
93 static bool arg_no_wtmp = false;
94 static bool arg_no_wall = false;
95 static bool arg_no_reload = false;
96 static bool arg_show_types = false;
97 static bool arg_ignore_inhibitors = false;
98 static bool arg_dry = false;
99 static bool arg_quiet = false;
100 static bool arg_full = false;
101 static int arg_force = 0;
102 static bool arg_ask_password = true;
103 static bool arg_runtime = false;
104 static char **arg_wall = NULL;
105 static const char *arg_kill_who = NULL;
106 static int arg_signal = SIGTERM;
107 static const char *arg_root = NULL;
108 static usec_t arg_when = 0;
130 ACTION_CANCEL_SHUTDOWN,
132 } arg_action = ACTION_SYSTEMCTL;
133 static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
134 static char *arg_host = NULL;
135 static unsigned arg_lines = 10;
136 static OutputMode arg_output = OUTPUT_SHORT;
137 static bool arg_plain = false;
139 static int daemon_reload(sd_bus *bus, char **args);
140 static int halt_now(enum action a);
142 static int check_one_unit(sd_bus *bus, const char *name, const char *good_states, bool quiet);
144 static char** strv_skip_first(char **strv) {
145 if (strv_length(strv) > 0)
150 static void pager_open_if_enabled(void) {
158 static void ask_password_agent_open_if_enabled(void) {
160 /* Open the password agent as a child process if necessary */
162 if (!arg_ask_password)
165 if (arg_scope != UNIT_FILE_SYSTEM)
168 if (arg_transport != BUS_TRANSPORT_LOCAL)
171 ask_password_agent_open();
175 static void polkit_agent_open_if_enabled(void) {
177 /* Open the polkit agent as a child process if necessary */
179 if (!arg_ask_password)
182 if (arg_scope != UNIT_FILE_SYSTEM)
185 if (arg_transport != BUS_TRANSPORT_LOCAL)
192 static int translate_bus_error_to_exit_status(int r, const sd_bus_error *error) {
195 if (!sd_bus_error_is_set(error))
198 if (sd_bus_error_has_name(error, SD_BUS_ERROR_ACCESS_DENIED) ||
199 sd_bus_error_has_name(error, BUS_ERROR_ONLY_BY_DEPENDENCY) ||
200 sd_bus_error_has_name(error, BUS_ERROR_NO_ISOLATION) ||
201 sd_bus_error_has_name(error, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE))
202 return EXIT_NOPERMISSION;
204 if (sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT))
205 return EXIT_NOTINSTALLED;
207 if (sd_bus_error_has_name(error, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE) ||
208 sd_bus_error_has_name(error, SD_BUS_ERROR_NOT_SUPPORTED))
209 return EXIT_NOTIMPLEMENTED;
211 if (sd_bus_error_has_name(error, BUS_ERROR_LOAD_FAILED))
212 return EXIT_NOTCONFIGURED;
220 static void warn_wall(enum action a) {
221 static const char *table[_ACTION_MAX] = {
222 [ACTION_HALT] = "The system is going down for system halt NOW!",
223 [ACTION_REBOOT] = "The system is going down for reboot NOW!",
224 [ACTION_POWEROFF] = "The system is going down for power-off NOW!",
225 [ACTION_KEXEC] = "The system is going down for kexec reboot NOW!",
226 [ACTION_RESCUE] = "The system is going down to rescue mode NOW!",
227 [ACTION_EMERGENCY] = "The system is going down to emergency mode NOW!",
228 [ACTION_CANCEL_SHUTDOWN] = "The system shutdown has been cancelled NOW!"
235 _cleanup_free_ char *p;
237 p = strv_join(arg_wall, " ");
252 utmp_wall(table[a], NULL);
255 static bool avoid_bus(void) {
257 if (running_in_chroot() > 0)
260 if (sd_booted() <= 0)
263 if (!isempty(arg_root))
266 if (arg_scope == UNIT_FILE_GLOBAL)
272 static int compare_unit_info(const void *a, const void *b) {
273 const UnitInfo *u = a, *v = b;
276 d1 = strrchr(u->id, '.');
277 d2 = strrchr(v->id, '.');
282 r = strcasecmp(d1, d2);
287 return strcasecmp(u->id, v->id);
290 static bool output_show_unit(const UnitInfo *u, char **patterns) {
293 if (!strv_isempty(arg_states))
295 strv_contains(arg_states, u->load_state) ||
296 strv_contains(arg_states, u->sub_state) ||
297 strv_contains(arg_states, u->active_state);
299 if (!strv_isempty(patterns)) {
302 STRV_FOREACH(pattern, patterns)
303 if (fnmatch(*pattern, u->id, FNM_NOESCAPE) == 0)
308 return (!arg_types || ((dot = strrchr(u->id, '.')) &&
309 strv_find(arg_types, dot+1))) &&
310 (arg_all || !(streq(u->active_state, "inactive")
311 || u->following[0]) || u->job_id > 0);
314 static void output_units_list(const UnitInfo *unit_infos, unsigned c, char** patterns) {
315 unsigned id_len, max_id_len, load_len, active_len, sub_len, job_len, desc_len;
317 unsigned n_shown = 0;
320 max_id_len = sizeof("UNIT")-1;
321 load_len = sizeof("LOAD")-1;
322 active_len = sizeof("ACTIVE")-1;
323 sub_len = sizeof("SUB")-1;
324 job_len = sizeof("JOB")-1;
327 for (u = unit_infos; u < unit_infos + c; u++) {
328 if (!output_show_unit(u, patterns))
331 max_id_len = MAX(max_id_len, strlen(u->id));
332 load_len = MAX(load_len, strlen(u->load_state));
333 active_len = MAX(active_len, strlen(u->active_state));
334 sub_len = MAX(sub_len, strlen(u->sub_state));
336 if (u->job_id != 0) {
337 job_len = MAX(job_len, strlen(u->job_type));
342 if (!arg_full && original_stdout_is_tty) {
345 id_len = MIN(max_id_len, 25u);
346 basic_len = 5 + id_len + 5 + active_len + sub_len;
349 basic_len += job_len + 1;
351 if (basic_len < (unsigned) columns()) {
352 unsigned extra_len, incr;
353 extra_len = columns() - basic_len;
355 /* Either UNIT already got 25, or is fully satisfied.
356 * Grant up to 25 to DESC now. */
357 incr = MIN(extra_len, 25u);
361 /* split the remaining space between UNIT and DESC,
362 * but do not give UNIT more than it needs. */
364 incr = MIN(extra_len / 2, max_id_len - id_len);
366 desc_len += extra_len - incr;
372 for (u = unit_infos; u < unit_infos + c; u++) {
373 _cleanup_free_ char *e = NULL;
374 const char *on_loaded, *off_loaded, *on = "";
375 const char *on_active, *off_active, *off = "";
377 if (!output_show_unit(u, patterns))
380 if (!n_shown && !arg_no_legend) {
381 printf("%-*s %-*s %-*s %-*s ",
384 active_len, "ACTIVE",
388 printf("%-*s ", job_len, "JOB");
390 if (!arg_full && arg_no_pager)
391 printf("%.*s\n", desc_len, "DESCRIPTION");
393 printf("%s\n", "DESCRIPTION");
398 if (streq(u->load_state, "error") ||
399 streq(u->load_state, "not-found")) {
400 on_loaded = on = ansi_highlight_red();
401 off_loaded = off = ansi_highlight_off();
403 on_loaded = off_loaded = "";
405 if (streq(u->active_state, "failed")) {
406 on_active = on = ansi_highlight_red();
407 off_active = off = ansi_highlight_off();
409 on_active = off_active = "";
411 e = arg_full ? NULL : ellipsize(u->id, id_len, 33);
413 printf("%s%-*s%s %s%-*s%s %s%-*s %-*s%s %-*s",
414 on, id_len, e ? e : u->id, off,
415 on_loaded, load_len, u->load_state, off_loaded,
416 on_active, active_len, u->active_state,
417 sub_len, u->sub_state, off_active,
418 job_count ? job_len + 1 : 0, u->job_id ? u->job_type : "");
421 printf("%.*s\n", desc_len, u->description);
423 printf("%s\n", u->description);
426 if (!arg_no_legend) {
427 const char *on, *off;
430 printf("\nLOAD = Reflects whether the unit definition was properly loaded.\n"
431 "ACTIVE = The high-level unit activation state, i.e. generalization of SUB.\n"
432 "SUB = The low-level unit activation state, values depend on unit type.\n");
434 printf("JOB = Pending job for the unit.\n");
436 on = ansi_highlight();
437 off = ansi_highlight_off();
439 on = ansi_highlight_red();
440 off = ansi_highlight_off();
444 printf("%s%u loaded units listed.%s\n"
445 "To show all installed unit files use 'systemctl list-unit-files'.\n",
448 printf("%s%u loaded units listed.%s Pass --all to see loaded but inactive units, too.\n"
449 "To show all installed unit files use 'systemctl list-unit-files'.\n",
454 static int get_unit_list(
456 sd_bus_message **_reply,
457 UnitInfo **_unit_infos) {
459 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
460 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
461 _cleanup_free_ UnitInfo *unit_infos = NULL;
470 r = sd_bus_call_method(
472 "org.freedesktop.systemd1",
473 "/org/freedesktop/systemd1",
474 "org.freedesktop.systemd1.Manager",
480 log_error("Failed to list units: %s", bus_error_message(&error, r));
484 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssssouso)");
486 return bus_log_parse_error(r);
488 while ((r = bus_parse_unit_info(reply, &u)) > 0) {
490 if (!GREEDY_REALLOC(unit_infos, size, c+1))
496 return bus_log_parse_error(r);
498 r = sd_bus_message_exit_container(reply);
500 return bus_log_parse_error(r);
505 *_unit_infos = unit_infos;
511 static int list_units(sd_bus *bus, char **args) {
512 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
513 _cleanup_free_ UnitInfo *unit_infos = NULL;
516 pager_open_if_enabled();
518 r = get_unit_list(bus, &reply, &unit_infos);
522 qsort_safe(unit_infos, r, sizeof(UnitInfo), compare_unit_info);
523 output_units_list(unit_infos, r, strv_skip_first(args));
528 static int get_triggered_units(
533 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
536 r = sd_bus_get_property_strv(
538 "org.freedesktop.systemd1",
540 "org.freedesktop.systemd1.Unit",
546 log_error("Failed to determine triggers: %s", bus_error_message(&error, r));
551 static int get_listening(
553 const char* unit_path,
556 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
557 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
558 const char *type, *path;
561 r = sd_bus_get_property(
563 "org.freedesktop.systemd1",
565 "org.freedesktop.systemd1.Socket",
571 log_error("Failed to get list of listening sockets: %s", bus_error_message(&error, r));
575 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ss)");
577 return bus_log_parse_error(r);
579 while ((r = sd_bus_message_read(reply, "(ss)", &type, &path)) > 0) {
581 r = strv_extend(listening, type);
585 r = strv_extend(listening, path);
592 return bus_log_parse_error(r);
594 r = sd_bus_message_exit_container(reply);
596 return bus_log_parse_error(r);
607 /* Note: triggered is a list here, although it almost certainly
608 * will always be one unit. Nevertheless, dbus API allows for multiple
609 * values, so let's follow that.*/
612 /* The strv above is shared. free is set only in the first one. */
616 static int socket_info_compare(const struct socket_info *a, const struct socket_info *b) {
622 o = strcmp(a->path, b->path);
624 o = strcmp(a->type, b->type);
629 static int output_sockets_list(struct socket_info *socket_infos, unsigned cs) {
630 struct socket_info *s;
631 unsigned pathlen = sizeof("LISTEN") - 1,
632 typelen = (sizeof("TYPE") - 1) * arg_show_types,
633 socklen = sizeof("UNIT") - 1,
634 servlen = sizeof("ACTIVATES") - 1;
635 const char *on, *off;
637 for (s = socket_infos; s < socket_infos + cs; s++) {
641 socklen = MAX(socklen, strlen(s->id));
643 typelen = MAX(typelen, strlen(s->type));
644 pathlen = MAX(pathlen, strlen(s->path));
646 STRV_FOREACH(a, s->triggered)
647 tmp += strlen(*a) + 2*(a != s->triggered);
648 servlen = MAX(servlen, tmp);
653 printf("%-*s %-*.*s%-*s %s\n",
655 typelen + arg_show_types, typelen + arg_show_types, "TYPE ",
659 for (s = socket_infos; s < socket_infos + cs; s++) {
663 printf("%-*s %-*s %-*s",
664 pathlen, s->path, typelen, s->type, socklen, s->id);
667 pathlen, s->path, socklen, s->id);
668 STRV_FOREACH(a, s->triggered)
670 a == s->triggered ? "" : ",", *a);
674 on = ansi_highlight();
675 off = ansi_highlight_off();
679 on = ansi_highlight_red();
680 off = ansi_highlight_off();
683 if (!arg_no_legend) {
684 printf("%s%u sockets listed.%s\n", on, cs, off);
686 printf("Pass --all to see loaded but inactive sockets, too.\n");
692 static int list_sockets(sd_bus *bus, char **args) {
693 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
694 _cleanup_free_ UnitInfo *unit_infos = NULL;
695 struct socket_info *socket_infos = NULL;
697 struct socket_info *s;
702 pager_open_if_enabled();
704 n = get_unit_list(bus, &reply, &unit_infos);
708 for (u = unit_infos; u < unit_infos + n; u++) {
709 _cleanup_strv_free_ char **listening = NULL, **triggered = NULL;
712 if (!output_show_unit(u, strv_skip_first(args)))
715 if (!endswith(u->id, ".socket"))
718 r = get_triggered_units(bus, u->unit_path, &triggered);
722 c = get_listening(bus, u->unit_path, &listening);
728 if (!GREEDY_REALLOC(socket_infos, size, cs + c)) {
733 for (i = 0; i < c; i++)
734 socket_infos[cs + i] = (struct socket_info) {
736 .type = listening[i*2],
737 .path = listening[i*2 + 1],
738 .triggered = triggered,
739 .own_triggered = i==0,
742 /* from this point on we will cleanup those socket_infos */
745 listening = triggered = NULL; /* avoid cleanup */
748 qsort_safe(socket_infos, cs, sizeof(struct socket_info),
749 (__compar_fn_t) socket_info_compare);
751 output_sockets_list(socket_infos, cs);
754 assert(cs == 0 || socket_infos);
755 for (s = socket_infos; s < socket_infos + cs; s++) {
758 if (s->own_triggered)
759 strv_free(s->triggered);
766 static int get_next_elapse(
769 dual_timestamp *next) {
771 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
779 r = sd_bus_get_property_trivial(
781 "org.freedesktop.systemd1",
783 "org.freedesktop.systemd1.Timer",
784 "NextElapseUSecMonotonic",
789 log_error("Failed to get next elapsation time: %s", bus_error_message(&error, r));
793 r = sd_bus_get_property_trivial(
795 "org.freedesktop.systemd1",
797 "org.freedesktop.systemd1.Timer",
798 "NextElapseUSecRealtime",
803 log_error("Failed to get next elapsation time: %s", bus_error_message(&error, r));
817 static int timer_info_compare(const struct timer_info *a, const struct timer_info *b) {
821 if (a->next_elapse < b->next_elapse)
823 if (a->next_elapse > b->next_elapse)
826 return strcmp(a->id, b->id);
829 static int output_timers_list(struct timer_info *timer_infos, unsigned n) {
830 struct timer_info *t;
832 nextlen = sizeof("NEXT") - 1,
833 leftlen = sizeof("LEFT") - 1,
834 unitlen = sizeof("UNIT") - 1,
835 activatelen = sizeof("ACTIVATES") - 1;
837 const char *on, *off;
839 assert(timer_infos || n == 0);
841 for (t = timer_infos; t < timer_infos + n; t++) {
845 if (t->next_elapse > 0) {
846 char tstamp[FORMAT_TIMESTAMP_MAX] = "", trel[FORMAT_TIMESTAMP_RELATIVE_MAX] = "";
848 format_timestamp(tstamp, sizeof(tstamp), t->next_elapse);
849 nextlen = MAX(nextlen, strlen(tstamp) + 1);
851 format_timestamp_relative(trel, sizeof(trel), t->next_elapse);
852 leftlen = MAX(leftlen, strlen(trel));
855 unitlen = MAX(unitlen, strlen(t->id));
857 STRV_FOREACH(a, t->triggered)
858 ul += strlen(*a) + 2*(a != t->triggered);
859 activatelen = MAX(activatelen, ul);
864 printf("%-*s %-*s %-*s %s\n",
870 for (t = timer_infos; t < timer_infos + n; t++) {
871 char tstamp[FORMAT_TIMESTAMP_MAX] = "n/a", trel[FORMAT_TIMESTAMP_RELATIVE_MAX] = "n/a";
874 format_timestamp(tstamp, sizeof(tstamp), t->next_elapse);
875 format_timestamp_relative(trel, sizeof(trel), t->next_elapse);
877 printf("%-*s %-*s %-*s",
878 nextlen, tstamp, leftlen, trel, unitlen, t->id);
880 STRV_FOREACH(a, t->triggered)
882 a == t->triggered ? "" : ",", *a);
886 on = ansi_highlight();
887 off = ansi_highlight_off();
891 on = ansi_highlight_red();
892 off = ansi_highlight_off();
895 if (!arg_no_legend) {
896 printf("%s%u timers listed.%s\n", on, n, off);
898 printf("Pass --all to see loaded but inactive timers, too.\n");
904 static int list_timers(sd_bus *bus, char **args) {
906 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
907 _cleanup_free_ struct timer_info *timer_infos = NULL;
908 _cleanup_free_ UnitInfo *unit_infos = NULL;
909 struct timer_info *t;
916 pager_open_if_enabled();
918 n = get_unit_list(bus, &reply, &unit_infos);
922 dual_timestamp_get(&nw);
924 for (u = unit_infos; u < unit_infos + n; u++) {
925 _cleanup_strv_free_ char **triggered = NULL;
929 if (!output_show_unit(u, strv_skip_first(args)))
932 if (!endswith(u->id, ".timer"))
935 r = get_triggered_units(bus, u->unit_path, &triggered);
939 r = get_next_elapse(bus, u->unit_path, &next);
943 if (next.monotonic != (usec_t) -1 && next.monotonic > 0) {
946 if (next.monotonic > nw.monotonic)
947 converted = nw.realtime + (next.monotonic - nw.monotonic);
949 converted = nw.realtime - (nw.monotonic - next.monotonic);
951 if (next.realtime != (usec_t) -1 && next.realtime > 0)
952 m = MIN(converted, next.realtime);
958 if (!GREEDY_REALLOC(timer_infos, size, c+1)) {
963 timer_infos[c++] = (struct timer_info) {
966 .triggered = triggered,
969 triggered = NULL; /* avoid cleanup */
972 qsort_safe(timer_infos, c, sizeof(struct timer_info),
973 (__compar_fn_t) timer_info_compare);
975 output_timers_list(timer_infos, c);
978 for (t = timer_infos; t < timer_infos + c; t++)
979 strv_free(t->triggered);
984 static int compare_unit_file_list(const void *a, const void *b) {
986 const UnitFileList *u = a, *v = b;
988 d1 = strrchr(u->path, '.');
989 d2 = strrchr(v->path, '.');
994 r = strcasecmp(d1, d2);
999 return strcasecmp(basename(u->path), basename(v->path));
1002 static bool output_show_unit_file(const UnitFileList *u, char **patterns) {
1005 if (!strv_isempty(patterns)) {
1008 STRV_FOREACH(pattern, patterns)
1009 if (fnmatch(*pattern, basename(u->path), FNM_NOESCAPE) == 0)
1014 return !arg_types || ((dot = strrchr(u->path, '.')) && strv_find(arg_types, dot+1));
1017 static void output_unit_file_list(const UnitFileList *units, unsigned c, char **patterns) {
1018 unsigned max_id_len, id_cols, state_cols, n_shown = 0;
1019 const UnitFileList *u;
1021 max_id_len = sizeof("UNIT FILE")-1;
1022 state_cols = sizeof("STATE")-1;
1024 for (u = units; u < units + c; u++) {
1025 if (!output_show_unit_file(u, patterns))
1028 max_id_len = MAX(max_id_len, strlen(basename(u->path)));
1029 state_cols = MAX(state_cols, strlen(unit_file_state_to_string(u->state)));
1033 unsigned basic_cols;
1035 id_cols = MIN(max_id_len, 25u);
1036 basic_cols = 1 + id_cols + state_cols;
1037 if (basic_cols < (unsigned) columns())
1038 id_cols += MIN(columns() - basic_cols, max_id_len - id_cols);
1040 id_cols = max_id_len;
1043 printf("%-*s %-*s\n",
1044 id_cols, "UNIT FILE",
1045 state_cols, "STATE");
1047 for (u = units; u < units + c; u++) {
1048 _cleanup_free_ char *e = NULL;
1049 const char *on, *off;
1052 if (!output_show_unit_file(u, patterns))
1057 if (u->state == UNIT_FILE_MASKED ||
1058 u->state == UNIT_FILE_MASKED_RUNTIME ||
1059 u->state == UNIT_FILE_DISABLED ||
1060 u->state == UNIT_FILE_INVALID) {
1061 on = ansi_highlight_red();
1062 off = ansi_highlight_off();
1063 } else if (u->state == UNIT_FILE_ENABLED) {
1064 on = ansi_highlight_green();
1065 off = ansi_highlight_off();
1069 id = basename(u->path);
1071 e = arg_full ? NULL : ellipsize(id, id_cols, 33);
1073 printf("%-*s %s%-*s%s\n",
1074 id_cols, e ? e : id,
1075 on, state_cols, unit_file_state_to_string(u->state), off);
1079 printf("\n%u unit files listed.\n", n_shown);
1082 static int list_unit_files(sd_bus *bus, char **args) {
1083 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
1084 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
1085 _cleanup_free_ UnitFileList *units = NULL;
1091 pager_open_if_enabled();
1099 h = hashmap_new(string_hash_func, string_compare_func);
1103 r = unit_file_get_list(arg_scope, arg_root, h);
1105 unit_file_list_free(h);
1106 log_error("Failed to get unit file list: %s", strerror(-r));
1110 n_units = hashmap_size(h);
1111 units = new(UnitFileList, n_units);
1113 unit_file_list_free(h);
1117 HASHMAP_FOREACH(u, h, i) {
1118 memcpy(units + c++, u, sizeof(UnitFileList));
1122 assert(c == n_units);
1127 r = sd_bus_call_method(
1129 "org.freedesktop.systemd1",
1130 "/org/freedesktop/systemd1",
1131 "org.freedesktop.systemd1.Manager",
1137 log_error("Failed to list unit files: %s", bus_error_message(&error, r));
1141 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ss)");
1143 return bus_log_parse_error(r);
1145 while ((r = sd_bus_message_read(reply, "(ss)", &path, &state)) > 0) {
1147 if (!GREEDY_REALLOC(units, size, c + 1))
1150 units[c++] = (struct UnitFileList) {
1152 unit_file_state_from_string(state)
1156 return bus_log_parse_error(r);
1158 r = sd_bus_message_exit_container(reply);
1160 return bus_log_parse_error(r);
1164 qsort(units, c, sizeof(UnitFileList), compare_unit_file_list);
1165 output_unit_file_list(units, c, strv_skip_first(args));
1171 static int list_dependencies_print(const char *name, int level, unsigned int branches, bool last) {
1172 _cleanup_free_ char *n = NULL;
1173 size_t max_len = MAX(columns(),20u);
1179 for (i = level - 1; i >= 0; i--) {
1181 if (len > max_len - 3 && !arg_full) {
1182 printf("%s...\n",max_len % 2 ? "" : " ");
1185 printf("%s", draw_special_char(branches & (1 << i) ? DRAW_TREE_VERT : DRAW_TREE_SPACE));
1189 if (len > max_len - 3 && !arg_full) {
1190 printf("%s...\n",max_len % 2 ? "" : " ");
1194 printf("%s", draw_special_char(last ? DRAW_TREE_RIGHT : DRAW_TREE_BRANCH));
1198 printf("%s\n", name);
1202 n = ellipsize(name, max_len-len, 100);
1210 static int list_dependencies_get_dependencies(sd_bus *bus, const char *name, char ***deps) {
1212 static const char *dependencies[_DEPENDENCY_MAX] = {
1213 [DEPENDENCY_FORWARD] = "Requires\0"
1214 "RequiresOverridable\0"
1216 "RequisiteOverridable\0"
1218 [DEPENDENCY_REVERSE] = "RequiredBy\0"
1219 "RequiredByOverridable\0"
1222 [DEPENDENCY_AFTER] = "After\0",
1223 [DEPENDENCY_BEFORE] = "Before\0",
1226 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
1227 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
1228 _cleanup_strv_free_ char **ret = NULL;
1229 _cleanup_free_ char *path = NULL;
1235 assert_cc(ELEMENTSOF(dependencies) == _DEPENDENCY_MAX);
1237 path = unit_dbus_path_from_name(name);
1241 r = sd_bus_call_method(
1243 "org.freedesktop.systemd1",
1245 "org.freedesktop.DBus.Properties",
1249 "s", "org.freedesktop.systemd1.Unit");
1251 log_error("Failed to get properties of %s: %s", name, bus_error_message(&error, r));
1255 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "{sv}");
1257 return bus_log_parse_error(r);
1259 while ((r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_DICT_ENTRY, "sv")) > 0) {
1262 r = sd_bus_message_read(reply, "s", &prop);
1264 return bus_log_parse_error(r);
1266 if (!nulstr_contains(dependencies[arg_dependency], prop)) {
1267 r = sd_bus_message_skip(reply, "v");
1269 return bus_log_parse_error(r);
1272 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_VARIANT, "as");
1274 return bus_log_parse_error(r);
1276 r = bus_message_read_strv_extend(reply, &ret);
1278 return bus_log_parse_error(r);
1280 r = sd_bus_message_exit_container(reply);
1282 return bus_log_parse_error(r);
1285 r = sd_bus_message_exit_container(reply);
1287 return bus_log_parse_error(r);
1291 return bus_log_parse_error(r);
1293 r = sd_bus_message_exit_container(reply);
1295 return bus_log_parse_error(r);
1303 static int list_dependencies_compare(const void *_a, const void *_b) {
1304 const char **a = (const char**) _a, **b = (const char**) _b;
1306 if (unit_name_to_type(*a) == UNIT_TARGET && unit_name_to_type(*b) != UNIT_TARGET)
1308 if (unit_name_to_type(*a) != UNIT_TARGET && unit_name_to_type(*b) == UNIT_TARGET)
1311 return strcasecmp(*a, *b);
1314 static int list_dependencies_one(
1319 unsigned int branches) {
1321 _cleanup_strv_free_ char **deps = NULL, **u;
1329 u = strv_append(*units, name);
1333 r = list_dependencies_get_dependencies(bus, name, &deps);
1337 qsort_safe(deps, strv_length(deps), sizeof (char*), list_dependencies_compare);
1339 STRV_FOREACH(c, deps) {
1342 if (strv_contains(u, *c)) {
1344 r = list_dependencies_print("...", level + 1, (branches << 1) | (c[1] == NULL ? 0 : 1), 1);
1351 state = check_one_unit(bus, *c, "activating\0active\0reloading\0", true);
1353 printf("%s%s%s", ansi_highlight_green(), draw_special_char(DRAW_BLACK_CIRCLE), ansi_highlight_off());
1355 printf("%s%s%s", ansi_highlight_red(), draw_special_char(DRAW_BLACK_CIRCLE), ansi_highlight_off());
1357 r = list_dependencies_print(*c, level, branches, c[1] == NULL);
1361 if (arg_all || unit_name_to_type(*c) == UNIT_TARGET) {
1362 r = list_dependencies_one(bus, *c, level + 1, &u, (branches << 1) | (c[1] == NULL ? 0 : 1));
1377 static int list_dependencies(sd_bus *bus, char **args) {
1378 _cleanup_strv_free_ char **units = NULL;
1379 _cleanup_free_ char *unit = NULL;
1385 unit = unit_name_mangle(args[1]);
1390 u = SPECIAL_DEFAULT_TARGET;
1392 pager_open_if_enabled();
1396 return list_dependencies_one(bus, u, 0, &units, 0);
1399 static int get_default(sd_bus *bus, char **args) {
1400 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
1401 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
1402 _cleanup_free_ char *_path = NULL;
1406 if (!bus || avoid_bus()) {
1407 r = unit_file_get_default(arg_scope, arg_root, &_path);
1409 log_error("Failed to get default target: %s", strerror(-r));
1415 r = sd_bus_call_method(
1417 "org.freedesktop.systemd1",
1418 "/org/freedesktop/systemd1",
1419 "org.freedesktop.systemd1.Manager",
1425 log_error("Failed to get default target: %s", bus_error_message(&error, -r));
1429 r = sd_bus_message_read(reply, "s", &path);
1431 return bus_log_parse_error(r);
1435 printf("%s\n", path);
1440 static void dump_unit_file_changes(const UnitFileChange *changes, unsigned n_changes) {
1443 assert(changes || n_changes == 0);
1445 for (i = 0; i < n_changes; i++) {
1446 if (changes[i].type == UNIT_FILE_SYMLINK)
1447 log_info("ln -s '%s' '%s'", changes[i].source, changes[i].path);
1449 log_info("rm '%s'", changes[i].path);
1453 static int deserialize_and_dump_unit_file_changes(sd_bus_message *m) {
1454 const char *type, *path, *source;
1457 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(sss)");
1459 return bus_log_parse_error(r);
1461 while ((r = sd_bus_message_read(m, "(sss)", &type, &path, &source)) > 0) {
1463 if (streq(type, "symlink"))
1464 log_info("ln -s '%s' '%s'", source, path);
1466 log_info("rm '%s'", path);
1470 return bus_log_parse_error(r);
1472 r = sd_bus_message_exit_container(m);
1474 return bus_log_parse_error(r);
1479 static int set_default(sd_bus *bus, char **args) {
1480 _cleanup_free_ char *unit = NULL;
1481 UnitFileChange *changes = NULL;
1482 unsigned n_changes = 0;
1485 unit = unit_name_mangle_with_suffix(args[1], ".target");
1489 if (!bus || avoid_bus()) {
1490 r = unit_file_set_default(arg_scope, arg_root, unit, arg_force, &changes, &n_changes);
1492 log_error("Failed to set default target: %s", strerror(-r));
1497 dump_unit_file_changes(changes, n_changes);
1501 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
1502 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
1504 r = sd_bus_call_method(
1506 "org.freedesktop.systemd1",
1507 "/org/freedesktop/systemd1",
1508 "org.freedesktop.systemd1.Manager",
1512 "sb", unit, arg_force);
1514 log_error("Failed to set default target: %s", bus_error_message(&error, -r));
1518 r = deserialize_and_dump_unit_file_changes(reply);
1522 /* Try to reload if enabeld */
1524 r = daemon_reload(bus, args);
1529 unit_file_changes_free(changes, n_changes);
1536 const char *name, *type, *state;
1539 static void output_jobs_list(const struct job_info* jobs, unsigned n, bool skipped) {
1540 unsigned id_len, unit_len, type_len, state_len;
1541 const struct job_info *j;
1542 const char *on, *off;
1543 bool shorten = false;
1545 assert(n == 0 || jobs);
1548 on = ansi_highlight_green();
1549 off = ansi_highlight_off();
1551 printf("%sNo jobs %s.%s\n", on, skipped ? "listed" : "running", off);
1555 pager_open_if_enabled();
1557 id_len = sizeof("JOB")-1;
1558 unit_len = sizeof("UNIT")-1;
1559 type_len = sizeof("TYPE")-1;
1560 state_len = sizeof("STATE")-1;
1562 for (j = jobs; j < jobs + n; j++) {
1563 uint32_t id = j->id;
1564 assert(j->name && j->type && j->state);
1566 id_len = MAX(id_len, DECIMAL_STR_WIDTH(id));
1567 unit_len = MAX(unit_len, strlen(j->name));
1568 type_len = MAX(type_len, strlen(j->type));
1569 state_len = MAX(state_len, strlen(j->state));
1572 if (!arg_full && id_len + 1 + unit_len + type_len + 1 + state_len > columns()) {
1573 unit_len = MAX(33u, columns() - id_len - type_len - state_len - 3);
1578 printf("%*s %-*s %-*s %-*s\n",
1582 state_len, "STATE");
1584 for (j = jobs; j < jobs + n; j++) {
1585 _cleanup_free_ char *e = NULL;
1587 if (streq(j->state, "running")) {
1588 on = ansi_highlight();
1589 off = ansi_highlight_off();
1593 e = shorten ? ellipsize(j->name, unit_len, 33) : NULL;
1594 printf("%*u %s%-*s%s %-*s %s%-*s%s\n",
1596 on, unit_len, e ? e : j->name, off,
1598 on, state_len, j->state, off);
1601 if (!arg_no_legend) {
1602 on = ansi_highlight();
1603 off = ansi_highlight_off();
1605 printf("\n%s%u jobs listed%s.\n", on, n, off);
1609 static bool output_show_job(struct job_info *job, char **patterns) {
1610 if (!strv_isempty(patterns)) {
1613 STRV_FOREACH(pattern, patterns)
1614 if (fnmatch(*pattern, job->name, FNM_NOESCAPE) == 0)
1622 static int list_jobs(sd_bus *bus, char **args) {
1623 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
1624 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
1625 const char *name, *type, *state, *job_path, *unit_path;
1626 _cleanup_free_ struct job_info *jobs = NULL;
1631 bool skipped = false;
1633 r = sd_bus_call_method(
1635 "org.freedesktop.systemd1",
1636 "/org/freedesktop/systemd1",
1637 "org.freedesktop.systemd1.Manager",
1643 log_error("Failed to list jobs: %s", bus_error_message(&error, r));
1647 r = sd_bus_message_enter_container(reply, 'a', "(usssoo)");
1649 return bus_log_parse_error(r);
1651 while ((r = sd_bus_message_read(reply, "(usssoo)", &id, &name, &type, &state, &job_path, &unit_path)) > 0) {
1652 struct job_info job = { id, name, type, state };
1654 if (!output_show_job(&job, strv_skip_first(args))) {
1659 if (!GREEDY_REALLOC(jobs, size, c + 1))
1665 return bus_log_parse_error(r);
1667 r = sd_bus_message_exit_container(reply);
1669 return bus_log_parse_error(r);
1671 output_jobs_list(jobs, c, skipped);
1675 static int cancel_job(sd_bus *bus, char **args) {
1676 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
1681 if (strv_length(args) <= 1)
1682 return daemon_reload(bus, args);
1684 STRV_FOREACH(name, args+1) {
1688 r = safe_atou32(*name, &id);
1690 log_error("Failed to parse job id \"%s\": %s", *name, strerror(-r));
1694 r = sd_bus_call_method(
1696 "org.freedesktop.systemd1",
1697 "/org/freedesktop/systemd1",
1698 "org.freedesktop.systemd1.Manager",
1704 log_error("Failed to cancel job %u: %s", (unsigned) id, bus_error_message(&error, r));
1712 static int need_daemon_reload(sd_bus *bus, const char *unit) {
1713 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
1714 _cleanup_free_ char *n = NULL;
1718 /* We ignore all errors here, since this is used to show a
1721 n = unit_name_mangle(unit);
1725 /* We don't use unit_dbus_path_from_name() directly since we
1726 * don't want to load the unit if it isn't loaded. */
1728 r = sd_bus_call_method(
1730 "org.freedesktop.systemd1",
1731 "/org/freedesktop/systemd1",
1732 "org.freedesktop.systemd1.Manager",
1740 r = sd_bus_message_read(reply, "o", &path);
1744 r = sd_bus_get_property_trivial(
1746 "org.freedesktop.systemd1",
1748 "org.freedesktop.systemd1.Unit",
1758 typedef struct WaitData {
1765 static int wait_filter(sd_bus *bus, sd_bus_message *m, void *data, sd_bus_error *error) {
1772 log_debug("Got D-Bus request: %s.%s() on %s",
1773 sd_bus_message_get_interface(m),
1774 sd_bus_message_get_member(m),
1775 sd_bus_message_get_path(m));
1777 if (sd_bus_message_is_signal(m, "org.freedesktop.DBus.Local", "Disconnected")) {
1778 log_error("Warning! D-Bus connection terminated.");
1780 } else if (sd_bus_message_is_signal(m, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
1782 const char *path, *result, *unit;
1786 r = sd_bus_message_read(m, "uoss", &id, &path, &unit, &result);
1788 ret = set_remove(d->set, (char*) path);
1794 if (!isempty(result))
1795 d->result = strdup(result);
1798 d->name = strdup(unit);
1803 r = sd_bus_message_read(m, "uos", &id, &path, &result);
1805 ret = set_remove(d->set, (char*) path);
1812 d->result = strdup(result);
1818 bus_log_parse_error(r);
1824 static int enable_wait_for_jobs(sd_bus *bus) {
1829 r = sd_bus_add_match(
1832 "sender='org.freedesktop.systemd1',"
1833 "interface='org.freedesktop.systemd1.Manager',"
1834 "member='JobRemoved',"
1835 "path='/org/freedesktop/systemd1'",
1838 log_error("Failed to add match");
1842 /* This is slightly dirty, since we don't undo the match registrations. */
1846 static int bus_process_wait(sd_bus *bus) {
1850 r = sd_bus_process(bus, NULL);
1855 r = sd_bus_wait(bus, (uint64_t) -1);
1861 static int check_wait_response(WaitData *d) {
1867 if (streq(d->result, "timeout"))
1868 log_error("Job for %s timed out.", strna(d->name));
1869 else if (streq(d->result, "canceled"))
1870 log_error("Job for %s canceled.", strna(d->name));
1871 else if (streq(d->result, "dependency"))
1872 log_error("A dependency job for %s failed. See 'journalctl -xn' for details.", strna(d->name));
1873 else if (!streq(d->result, "done") && !streq(d->result, "skipped"))
1874 log_error("Job for %s failed. See 'systemctl status %s' and 'journalctl -xn' for details.", strna(d->name), strna(d->name));
1877 if (streq(d->result, "timeout"))
1879 else if (streq(d->result, "canceled"))
1881 else if (streq(d->result, "dependency"))
1883 else if (!streq(d->result, "done") && !streq(d->result, "skipped"))
1889 static int wait_for_jobs(sd_bus *bus, Set *s) {
1890 WaitData d = { .set = s };
1896 q = sd_bus_add_filter(bus, wait_filter, &d);
1900 while (!set_isempty(s)) {
1901 q = bus_process_wait(bus);
1906 q = check_wait_response(&d);
1907 /* Return the first error as it is most likely to be
1909 if (q < 0 && r == 0)
1920 q = sd_bus_remove_filter(bus, wait_filter, &d);
1921 if (q < 0 && r == 0)
1927 static int check_one_unit(sd_bus *bus, const char *name, const char *good_states, bool quiet) {
1928 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
1929 _cleanup_free_ char *n = NULL, *state = NULL;
1935 n = unit_name_mangle(name);
1939 /* We don't use unit_dbus_path_from_name() directly since we
1940 * don't want to load the unit if it isn't loaded. */
1942 r = sd_bus_call_method(
1944 "org.freedesktop.systemd1",
1945 "/org/freedesktop/systemd1",
1946 "org.freedesktop.systemd1.Manager",
1957 r = sd_bus_message_read(reply, "o", &path);
1959 return bus_log_parse_error(r);
1961 r = sd_bus_get_property_string(
1963 "org.freedesktop.systemd1",
1965 "org.freedesktop.systemd1.Unit",
1978 return nulstr_contains(good_states, state);
1981 static int check_triggering_units(
1985 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
1986 _cleanup_free_ char *path = NULL, *n = NULL, *state = NULL;
1987 _cleanup_strv_free_ char **triggered_by = NULL;
1988 bool print_warning_label = true;
1992 n = unit_name_mangle(name);
1996 path = unit_dbus_path_from_name(n);
2000 r = sd_bus_get_property_string(
2002 "org.freedesktop.systemd1",
2004 "org.freedesktop.systemd1.Unit",
2009 log_error("Failed to get load state of %s: %s", n, bus_error_message(&error, r));
2013 if (streq(state, "masked"))
2016 r = sd_bus_get_property_strv(
2018 "org.freedesktop.systemd1",
2020 "org.freedesktop.systemd1.Unit",
2025 log_error("Failed to get triggered by array of %s: %s", n, bus_error_message(&error, r));
2029 STRV_FOREACH(i, triggered_by) {
2030 r = check_one_unit(bus, *i, "active\0reloading\0", true);
2032 log_error("Failed to check unit: %s", strerror(-r));
2039 if (print_warning_label) {
2040 log_warning("Warning: Stopping %s, but it can still be activated by:", n);
2041 print_warning_label = false;
2044 log_warning(" %s", *i);
2050 static int start_unit_one(
2055 sd_bus_error *error,
2058 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
2059 _cleanup_free_ char *n;
2068 n = unit_name_mangle(name);
2072 r = sd_bus_call_method(
2074 "org.freedesktop.systemd1",
2075 "/org/freedesktop/systemd1",
2076 "org.freedesktop.systemd1.Manager",
2082 if (r == -ENOENT && arg_action != ACTION_SYSTEMCTL)
2083 /* There's always a fallback possible for
2084 * legacy actions. */
2085 return -EADDRNOTAVAIL;
2087 log_error("Failed to start %s: %s", name, bus_error_message(error, r));
2091 r = sd_bus_message_read(reply, "o", &path);
2093 return bus_log_parse_error(r);
2095 if (need_daemon_reload(bus, n) > 0)
2096 log_warning("Warning: Unit file of %s changed on disk, 'systemctl%s daemon-reload' recommended.",
2097 n, arg_scope == UNIT_FILE_SYSTEM ? "" : " --user");
2106 r = set_consume(s, p);
2114 static const struct {
2118 } action_table[_ACTION_MAX] = {
2119 [ACTION_HALT] = { SPECIAL_HALT_TARGET, "halt", "replace-irreversibly" },
2120 [ACTION_POWEROFF] = { SPECIAL_POWEROFF_TARGET, "poweroff", "replace-irreversibly" },
2121 [ACTION_REBOOT] = { SPECIAL_REBOOT_TARGET, "reboot", "replace-irreversibly" },
2122 [ACTION_KEXEC] = { SPECIAL_KEXEC_TARGET, "kexec", "replace-irreversibly" },
2123 [ACTION_RUNLEVEL2] = { SPECIAL_RUNLEVEL2_TARGET, NULL, "isolate" },
2124 [ACTION_RUNLEVEL3] = { SPECIAL_RUNLEVEL3_TARGET, NULL, "isolate" },
2125 [ACTION_RUNLEVEL4] = { SPECIAL_RUNLEVEL4_TARGET, NULL, "isolate" },
2126 [ACTION_RUNLEVEL5] = { SPECIAL_RUNLEVEL5_TARGET, NULL, "isolate" },
2127 [ACTION_RESCUE] = { SPECIAL_RESCUE_TARGET, "rescue", "isolate" },
2128 [ACTION_EMERGENCY] = { SPECIAL_EMERGENCY_TARGET, "emergency", "isolate" },
2129 [ACTION_DEFAULT] = { SPECIAL_DEFAULT_TARGET, "default", "isolate" },
2130 [ACTION_EXIT] = { SPECIAL_EXIT_TARGET, "exit", "replace-irreversibly" },
2131 [ACTION_SUSPEND] = { SPECIAL_SUSPEND_TARGET, "suspend", "replace-irreversibly" },
2132 [ACTION_HIBERNATE] = { SPECIAL_HIBERNATE_TARGET, "hibernate", "replace-irreversibly" },
2133 [ACTION_HYBRID_SLEEP] = { SPECIAL_HYBRID_SLEEP_TARGET, "hybrid-sleep", "replace-irreversibly" },
2136 static enum action verb_to_action(const char *verb) {
2139 for (i = _ACTION_INVALID; i < _ACTION_MAX; i++)
2140 if (streq_ptr(action_table[i].verb, verb))
2143 return _ACTION_INVALID;
2146 static int start_unit(sd_bus *bus, char **args) {
2147 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
2148 _cleanup_set_free_free_ Set *s = NULL;
2149 const char *method, *mode, *one_name;
2155 ask_password_agent_open_if_enabled();
2157 if (arg_action == ACTION_SYSTEMCTL) {
2160 streq(args[0], "stop") ||
2161 streq(args[0], "condstop") ? "StopUnit" :
2162 streq(args[0], "reload") ? "ReloadUnit" :
2163 streq(args[0], "restart") ? "RestartUnit" :
2165 streq(args[0], "try-restart") ||
2166 streq(args[0], "condrestart") ? "TryRestartUnit" :
2168 streq(args[0], "reload-or-restart") ? "ReloadOrRestartUnit" :
2170 streq(args[0], "reload-or-try-restart") ||
2171 streq(args[0], "condreload") ||
2172 streq(args[0], "force-reload") ? "ReloadOrTryRestartUnit" :
2174 action = verb_to_action(args[0]);
2176 mode = streq(args[0], "isolate") ? "isolate" :
2177 action_table[action].mode ?: arg_job_mode;
2179 one_name = action_table[action].target;
2181 assert(arg_action < ELEMENTSOF(action_table));
2182 assert(action_table[arg_action].target);
2184 method = "StartUnit";
2186 mode = action_table[arg_action].mode;
2187 one_name = action_table[arg_action].target;
2190 if (!arg_no_block) {
2191 r = enable_wait_for_jobs(bus);
2193 log_error("Could not watch jobs: %s", strerror(-r));
2197 s = set_new(string_hash_func, string_compare_func);
2203 r = start_unit_one(bus, method, one_name, mode, &error, s);
2205 r = translate_bus_error_to_exit_status(r, &error);
2209 STRV_FOREACH(name, args+1) {
2212 q = start_unit_one(bus, method, *name, mode, &error, s);
2214 r = translate_bus_error_to_exit_status(q, &error);
2215 sd_bus_error_free(&error);
2220 if (!arg_no_block) {
2223 q = wait_for_jobs(bus, s);
2227 /* When stopping units, warn if they can still be triggered by
2228 * another active unit (socket, path, timer) */
2229 if (!arg_quiet && streq(method, "StopUnit")) {
2231 check_triggering_units(bus, one_name);
2233 STRV_FOREACH(name, args+1)
2234 check_triggering_units(bus, *name);
2241 /* Ask systemd-logind, which might grant access to unprivileged users
2242 * through PolicyKit */
2243 static int reboot_with_logind(sd_bus *bus, enum action a) {
2245 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
2252 polkit_agent_open_if_enabled();
2260 case ACTION_POWEROFF:
2261 method = "PowerOff";
2264 case ACTION_SUSPEND:
2268 case ACTION_HIBERNATE:
2269 method = "Hibernate";
2272 case ACTION_HYBRID_SLEEP:
2273 method = "HybridSleep";
2280 r = sd_bus_call_method(
2282 "org.freedesktop.login1",
2283 "/org/freedesktop/login1",
2284 "org.freedesktop.login1.Manager",
2290 log_error("Failed to execute operation: %s", bus_error_message(&error, r));
2298 static int check_inhibitors(sd_bus *bus, enum action a) {
2300 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
2301 _cleanup_strv_free_ char **sessions = NULL;
2302 const char *what, *who, *why, *mode;
2311 if (arg_ignore_inhibitors || arg_force > 0)
2323 r = sd_bus_call_method(
2325 "org.freedesktop.login1",
2326 "/org/freedesktop/login1",
2327 "org.freedesktop.login1.Manager",
2333 /* If logind is not around, then there are no inhibitors... */
2336 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssuu)");
2338 return bus_log_parse_error(r);
2340 while ((r = sd_bus_message_read(reply, "(ssssuu)", &what, &who, &why, &mode, &uid, &pid)) > 0) {
2341 _cleanup_free_ char *comm = NULL, *user = NULL;
2342 _cleanup_strv_free_ char **sv = NULL;
2344 if (!streq(mode, "block"))
2347 sv = strv_split(what, ":");
2351 if (!strv_contains(sv,
2353 a == ACTION_POWEROFF ||
2354 a == ACTION_REBOOT ||
2355 a == ACTION_KEXEC ? "shutdown" : "sleep"))
2358 get_process_comm(pid, &comm);
2359 user = uid_to_name(uid);
2361 log_warning("Operation inhibited by \"%s\" (PID %lu \"%s\", user %s), reason is \"%s\".",
2362 who, (unsigned long) pid, strna(comm), strna(user), why);
2367 return bus_log_parse_error(r);
2369 r = sd_bus_message_exit_container(reply);
2371 return bus_log_parse_error(r);
2373 /* Check for current sessions */
2374 sd_get_sessions(&sessions);
2375 STRV_FOREACH(s, sessions) {
2376 _cleanup_free_ char *type = NULL, *tty = NULL, *seat = NULL, *user = NULL, *service = NULL, *class = NULL;
2378 if (sd_session_get_uid(*s, &uid) < 0 || uid == getuid())
2381 if (sd_session_get_class(*s, &class) < 0 || !streq(class, "user"))
2384 if (sd_session_get_type(*s, &type) < 0 || (!streq(type, "x11") && !streq(type, "tty")))
2387 sd_session_get_tty(*s, &tty);
2388 sd_session_get_seat(*s, &seat);
2389 sd_session_get_service(*s, &service);
2390 user = uid_to_name(uid);
2392 log_warning("User %s is logged in on %s.", strna(user), isempty(tty) ? (isempty(seat) ? strna(service) : seat) : tty);
2399 log_error("Please retry operation after closing inhibitors and logging out other users.\nAlternatively, ignore inhibitors and users with 'systemctl %s -i'.",
2400 action_table[a].verb);
2408 static int start_special(sd_bus *bus, char **args) {
2414 a = verb_to_action(args[0]);
2416 r = check_inhibitors(bus, a);
2420 if (arg_force >= 2 && geteuid() != 0) {
2421 log_error("Must be root.");
2425 if (arg_force >= 2 &&
2426 (a == ACTION_HALT ||
2427 a == ACTION_POWEROFF ||
2428 a == ACTION_REBOOT))
2431 if (arg_force >= 1 &&
2432 (a == ACTION_HALT ||
2433 a == ACTION_POWEROFF ||
2434 a == ACTION_REBOOT ||
2435 a == ACTION_KEXEC ||
2437 return daemon_reload(bus, args);
2439 /* first try logind, to allow authentication with polkit */
2440 if (geteuid() != 0 &&
2441 (a == ACTION_POWEROFF ||
2442 a == ACTION_REBOOT ||
2443 a == ACTION_SUSPEND ||
2444 a == ACTION_HIBERNATE ||
2445 a == ACTION_HYBRID_SLEEP)) {
2446 r = reboot_with_logind(bus, a);
2451 r = start_unit(bus, args);
2452 if (r == EXIT_SUCCESS)
2458 static int check_unit_active(sd_bus *bus, char **args) {
2460 int r = 3; /* According to LSB: "program is not running" */
2465 STRV_FOREACH(name, args+1) {
2468 state = check_one_unit(bus, *name, "active\0reloading\0", arg_quiet);
2478 static int check_unit_failed(sd_bus *bus, char **args) {
2485 STRV_FOREACH(name, args+1) {
2488 state = check_one_unit(bus, *name, "failed\0", arg_quiet);
2498 static int kill_unit(sd_bus *bus, char **args) {
2499 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
2507 arg_kill_who = "all";
2509 STRV_FOREACH(name, args+1) {
2510 _cleanup_free_ char *n = NULL;
2512 n = unit_name_mangle(*name);
2516 r = sd_bus_call_method(
2518 "org.freedesktop.systemd1",
2519 "/org/freedesktop/systemd1",
2520 "org.freedesktop.systemd1.Manager",
2524 "ssi", n, arg_kill_who, arg_signal);
2526 log_error("Failed to kill unit %s: %s", n, bus_error_message(&error, r));
2534 typedef struct ExecStatusInfo {
2542 usec_t start_timestamp;
2543 usec_t exit_timestamp;
2548 LIST_FIELDS(struct ExecStatusInfo, exec);
2551 static void exec_status_info_free(ExecStatusInfo *i) {
2560 static int exec_status_info_deserialize(sd_bus_message *m, ExecStatusInfo *i) {
2561 uint64_t start_timestamp, exit_timestamp, start_timestamp_monotonic, exit_timestamp_monotonic;
2564 int32_t code, status;
2570 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_STRUCT, "sasbttttuii");
2572 return bus_log_parse_error(r);
2576 r = sd_bus_message_read(m, "s", &path);
2578 return bus_log_parse_error(r);
2580 i->path = strdup(path);
2584 r = sd_bus_message_read_strv(m, &i->argv);
2586 return bus_log_parse_error(r);
2588 r = sd_bus_message_read(m,
2591 &start_timestamp, &start_timestamp_monotonic,
2592 &exit_timestamp, &exit_timestamp_monotonic,
2596 return bus_log_parse_error(r);
2599 i->start_timestamp = (usec_t) start_timestamp;
2600 i->exit_timestamp = (usec_t) exit_timestamp;
2601 i->pid = (pid_t) pid;
2605 r = sd_bus_message_exit_container(m);
2607 return bus_log_parse_error(r);
2612 typedef struct UnitStatusInfo {
2614 const char *load_state;
2615 const char *active_state;
2616 const char *sub_state;
2617 const char *unit_file_state;
2619 const char *description;
2620 const char *following;
2622 char **documentation;
2624 const char *fragment_path;
2625 const char *source_path;
2626 const char *control_group;
2628 char **dropin_paths;
2630 const char *load_error;
2633 usec_t inactive_exit_timestamp;
2634 usec_t inactive_exit_timestamp_monotonic;
2635 usec_t active_enter_timestamp;
2636 usec_t active_exit_timestamp;
2637 usec_t inactive_enter_timestamp;
2639 bool need_daemon_reload;
2644 const char *status_text;
2645 const char *pid_file;
2648 usec_t start_timestamp;
2649 usec_t exit_timestamp;
2651 int exit_code, exit_status;
2653 usec_t condition_timestamp;
2654 bool condition_result;
2655 bool failed_condition_trigger;
2656 bool failed_condition_negate;
2657 const char *failed_condition;
2658 const char *failed_condition_param;
2661 unsigned n_accepted;
2662 unsigned n_connections;
2665 /* Pairs of type, path */
2669 const char *sysfs_path;
2671 /* Mount, Automount */
2677 LIST_HEAD(ExecStatusInfo, exec);
2680 static void print_status_info(
2685 const char *on, *off, *ss;
2687 char since1[FORMAT_TIMESTAMP_RELATIVE_MAX], *s1;
2688 char since2[FORMAT_TIMESTAMP_MAX], *s2;
2691 arg_all * OUTPUT_SHOW_ALL |
2692 (!on_tty() || pager_have()) * OUTPUT_FULL_WIDTH |
2693 on_tty() * OUTPUT_COLOR |
2694 !arg_quiet * OUTPUT_WARN_CUTOFF |
2695 arg_full * OUTPUT_FULL_WIDTH;
2700 /* This shows pretty information about a unit. See
2701 * print_property() for a low-level property printer */
2703 printf("%s", strna(i->id));
2705 if (i->description && !streq_ptr(i->id, i->description))
2706 printf(" - %s", i->description);
2711 printf(" Follow: unit currently follows state of %s\n", i->following);
2713 if (streq_ptr(i->load_state, "error")) {
2714 on = ansi_highlight_red();
2715 off = ansi_highlight_off();
2719 path = i->source_path ? i->source_path : i->fragment_path;
2722 printf(" Loaded: %s%s%s (Reason: %s)\n",
2723 on, strna(i->load_state), off, i->load_error);
2724 else if (path && i->unit_file_state)
2725 printf(" Loaded: %s%s%s (%s; %s)\n",
2726 on, strna(i->load_state), off, path, i->unit_file_state);
2728 printf(" Loaded: %s%s%s (%s)\n",
2729 on, strna(i->load_state), off, path);
2731 printf(" Loaded: %s%s%s\n",
2732 on, strna(i->load_state), off);
2734 if (!strv_isempty(i->dropin_paths)) {
2735 _cleanup_free_ char *dir = NULL;
2739 STRV_FOREACH(dropin, i->dropin_paths) {
2740 if (! dir || last) {
2741 printf(dir ? " " : " Drop-In: ");
2746 if (path_get_parent(*dropin, &dir) < 0) {
2751 printf("%s\n %s", dir,
2752 draw_special_char(DRAW_TREE_RIGHT));
2755 last = ! (*(dropin + 1) && startswith(*(dropin + 1), dir));
2757 printf("%s%s", basename(*dropin), last ? "\n" : ", ");
2761 ss = streq_ptr(i->active_state, i->sub_state) ? NULL : i->sub_state;
2763 if (streq_ptr(i->active_state, "failed")) {
2764 on = ansi_highlight_red();
2765 off = ansi_highlight_off();
2766 } else if (streq_ptr(i->active_state, "active") || streq_ptr(i->active_state, "reloading")) {
2767 on = ansi_highlight_green();
2768 off = ansi_highlight_off();
2773 printf(" Active: %s%s (%s)%s",
2774 on, strna(i->active_state), ss, off);
2776 printf(" Active: %s%s%s",
2777 on, strna(i->active_state), off);
2779 if (!isempty(i->result) && !streq(i->result, "success"))
2780 printf(" (Result: %s)", i->result);
2782 timestamp = (streq_ptr(i->active_state, "active") ||
2783 streq_ptr(i->active_state, "reloading")) ? i->active_enter_timestamp :
2784 (streq_ptr(i->active_state, "inactive") ||
2785 streq_ptr(i->active_state, "failed")) ? i->inactive_enter_timestamp :
2786 streq_ptr(i->active_state, "activating") ? i->inactive_exit_timestamp :
2787 i->active_exit_timestamp;
2789 s1 = format_timestamp_relative(since1, sizeof(since1), timestamp);
2790 s2 = format_timestamp(since2, sizeof(since2), timestamp);
2793 printf(" since %s; %s\n", s2, s1);
2795 printf(" since %s\n", s2);
2799 if (!i->condition_result && i->condition_timestamp > 0) {
2800 s1 = format_timestamp_relative(since1, sizeof(since1), i->condition_timestamp);
2801 s2 = format_timestamp(since2, sizeof(since2), i->condition_timestamp);
2803 printf(" start condition failed at %s%s%s\n",
2804 s2, s1 ? "; " : "", s1 ? s1 : "");
2805 if (i->failed_condition_trigger)
2806 printf(" none of the trigger conditions were met\n");
2807 else if (i->failed_condition)
2808 printf(" %s=%s%s was not met\n",
2809 i->failed_condition,
2810 i->failed_condition_negate ? "!" : "",
2811 i->failed_condition_param);
2815 printf(" Device: %s\n", i->sysfs_path);
2817 printf(" Where: %s\n", i->where);
2819 printf(" What: %s\n", i->what);
2821 STRV_FOREACH(t, i->documentation)
2822 printf(" %*s %s\n", 9, t == i->documentation ? "Docs:" : "", *t);
2824 STRV_FOREACH_PAIR(t, t2, i->listen)
2825 printf(" %*s %s (%s)\n", 9, t == i->listen ? "Listen:" : "", *t2, *t);
2828 printf(" Accepted: %u; Connected: %u\n", i->n_accepted, i->n_connections);
2830 LIST_FOREACH(exec, p, i->exec) {
2831 _cleanup_free_ char *argv = NULL;
2834 /* Only show exited processes here */
2838 argv = strv_join(p->argv, " ");
2839 printf(" Process: %u %s=%s ", p->pid, p->name, strna(argv));
2841 good = is_clean_exit_lsb(p->code, p->status, NULL);
2843 on = ansi_highlight_red();
2844 off = ansi_highlight_off();
2848 printf("%s(code=%s, ", on, sigchld_code_to_string(p->code));
2850 if (p->code == CLD_EXITED) {
2853 printf("status=%i", p->status);
2855 c = exit_status_to_string(p->status, EXIT_STATUS_SYSTEMD);
2860 printf("signal=%s", signal_to_string(p->status));
2862 printf(")%s\n", off);
2864 if (i->main_pid == p->pid &&
2865 i->start_timestamp == p->start_timestamp &&
2866 i->exit_timestamp == p->start_timestamp)
2867 /* Let's not show this twice */
2870 if (p->pid == i->control_pid)
2874 if (i->main_pid > 0 || i->control_pid > 0) {
2875 if (i->main_pid > 0) {
2876 printf(" Main PID: %u", (unsigned) i->main_pid);
2879 _cleanup_free_ char *comm = NULL;
2880 get_process_comm(i->main_pid, &comm);
2882 printf(" (%s)", comm);
2883 } else if (i->exit_code > 0) {
2884 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
2886 if (i->exit_code == CLD_EXITED) {
2889 printf("status=%i", i->exit_status);
2891 c = exit_status_to_string(i->exit_status, EXIT_STATUS_SYSTEMD);
2896 printf("signal=%s", signal_to_string(i->exit_status));
2900 if (i->control_pid > 0)
2904 if (i->control_pid > 0) {
2905 _cleanup_free_ char *c = NULL;
2907 printf(" %8s: %u", i->main_pid ? "" : " Control", (unsigned) i->control_pid);
2909 get_process_comm(i->control_pid, &c);
2918 printf(" Status: \"%s\"\n", i->status_text);
2920 if (i->control_group &&
2921 (i->main_pid > 0 || i->control_pid > 0 || cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, i->control_group, false) == 0)) {
2924 printf(" CGroup: %s\n", i->control_group);
2926 if (arg_transport == BUS_TRANSPORT_LOCAL || arg_transport == BUS_TRANSPORT_CONTAINER) {
2929 char prefix[] = " ";
2932 if (c > sizeof(prefix) - 1)
2933 c -= sizeof(prefix) - 1;
2937 if (i->main_pid > 0)
2938 extra[k++] = i->main_pid;
2940 if (i->control_pid > 0)
2941 extra[k++] = i->control_pid;
2943 show_cgroup_and_extra(SYSTEMD_CGROUP_CONTROLLER, i->control_group, prefix,
2944 c, false, extra, k, flags);
2948 if (i->id && arg_transport == BUS_TRANSPORT_LOCAL) {
2950 show_journal_by_unit(stdout,
2954 i->inactive_exit_timestamp_monotonic,
2958 arg_scope == UNIT_FILE_SYSTEM,
2962 if (i->need_daemon_reload)
2963 printf("\n%sWarning:%s Unit file changed on disk, 'systemctl %sdaemon-reload' recommended.\n",
2964 ansi_highlight_red(),
2965 ansi_highlight_off(),
2966 arg_scope == UNIT_FILE_SYSTEM ? "" : "--user ");
2969 static void show_unit_help(UnitStatusInfo *i) {
2974 if (!i->documentation) {
2975 log_info("Documentation for %s not known.", i->id);
2979 STRV_FOREACH(p, i->documentation) {
2981 if (startswith(*p, "man:")) {
2982 const char *args[4] = { "man", NULL, NULL, NULL };
2983 _cleanup_free_ char *page = NULL, *section = NULL;
2990 if ((*p)[k-1] == ')')
2991 e = strrchr(*p, '(');
2994 page = strndup((*p) + 4, e - *p - 4);
2995 section = strndup(e + 1, *p + k - e - 2);
2996 if (!page || !section) {
3008 log_error("Failed to fork: %m");
3014 execvp(args[0], (char**) args);
3015 log_error("Failed to execute man: %m");
3016 _exit(EXIT_FAILURE);
3019 wait_for_terminate(pid, NULL);
3021 log_info("Can't show: %s", *p);
3025 static int status_property(const char *name, sd_bus_message *m, UnitStatusInfo *i, const char *contents) {
3032 switch (contents[0]) {
3034 case SD_BUS_TYPE_STRING: {
3037 r = sd_bus_message_read(m, "s", &s);
3039 return bus_log_parse_error(r);
3042 if (streq(name, "Id"))
3044 else if (streq(name, "LoadState"))
3046 else if (streq(name, "ActiveState"))
3047 i->active_state = s;
3048 else if (streq(name, "SubState"))
3050 else if (streq(name, "Description"))
3052 else if (streq(name, "FragmentPath"))
3053 i->fragment_path = s;
3054 else if (streq(name, "SourcePath"))
3057 else if (streq(name, "DefaultControlGroup")) {
3059 e = startswith(s, SYSTEMD_CGROUP_CONTROLLER ":");
3061 i->control_group = e;
3064 else if (streq(name, "ControlGroup"))
3065 i->control_group = s;
3066 else if (streq(name, "StatusText"))
3068 else if (streq(name, "PIDFile"))
3070 else if (streq(name, "SysFSPath"))
3072 else if (streq(name, "Where"))
3074 else if (streq(name, "What"))
3076 else if (streq(name, "Following"))
3078 else if (streq(name, "UnitFileState"))
3079 i->unit_file_state = s;
3080 else if (streq(name, "Result"))
3087 case SD_BUS_TYPE_BOOLEAN: {
3090 r = sd_bus_message_read(m, "b", &b);
3092 return bus_log_parse_error(r);
3094 if (streq(name, "Accept"))
3096 else if (streq(name, "NeedDaemonReload"))
3097 i->need_daemon_reload = b;
3098 else if (streq(name, "ConditionResult"))
3099 i->condition_result = b;
3104 case SD_BUS_TYPE_UINT32: {
3107 r = sd_bus_message_read(m, "u", &u);
3109 return bus_log_parse_error(r);
3111 if (streq(name, "MainPID")) {
3113 i->main_pid = (pid_t) u;
3116 } else if (streq(name, "ControlPID"))
3117 i->control_pid = (pid_t) u;
3118 else if (streq(name, "ExecMainPID")) {
3120 i->main_pid = (pid_t) u;
3121 } else if (streq(name, "NAccepted"))
3123 else if (streq(name, "NConnections"))
3124 i->n_connections = u;
3129 case SD_BUS_TYPE_INT32: {
3132 r = sd_bus_message_read(m, "i", &j);
3134 return bus_log_parse_error(r);
3136 if (streq(name, "ExecMainCode"))
3137 i->exit_code = (int) j;
3138 else if (streq(name, "ExecMainStatus"))
3139 i->exit_status = (int) j;
3144 case SD_BUS_TYPE_UINT64: {
3147 r = sd_bus_message_read(m, "t", &u);
3149 return bus_log_parse_error(r);
3151 if (streq(name, "ExecMainStartTimestamp"))
3152 i->start_timestamp = (usec_t) u;
3153 else if (streq(name, "ExecMainExitTimestamp"))
3154 i->exit_timestamp = (usec_t) u;
3155 else if (streq(name, "ActiveEnterTimestamp"))
3156 i->active_enter_timestamp = (usec_t) u;
3157 else if (streq(name, "InactiveEnterTimestamp"))
3158 i->inactive_enter_timestamp = (usec_t) u;
3159 else if (streq(name, "InactiveExitTimestamp"))
3160 i->inactive_exit_timestamp = (usec_t) u;
3161 else if (streq(name, "InactiveExitTimestampMonotonic"))
3162 i->inactive_exit_timestamp_monotonic = (usec_t) u;
3163 else if (streq(name, "ActiveExitTimestamp"))
3164 i->active_exit_timestamp = (usec_t) u;
3165 else if (streq(name, "ConditionTimestamp"))
3166 i->condition_timestamp = (usec_t) u;
3171 case SD_BUS_TYPE_ARRAY:
3173 if (contents[1] == SD_BUS_TYPE_STRUCT_BEGIN && startswith(name, "Exec")) {
3174 _cleanup_free_ ExecStatusInfo *info = NULL;
3176 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(sasbttttuii)");
3178 return bus_log_parse_error(r);
3180 info = new0(ExecStatusInfo, 1);
3184 while ((r = exec_status_info_deserialize(m, info)) > 0) {
3186 info->name = strdup(name);
3190 LIST_PREPEND(exec, i->exec, info);
3192 info = new0(ExecStatusInfo, 1);
3198 return bus_log_parse_error(r);
3200 r = sd_bus_message_exit_container(m);
3202 return bus_log_parse_error(r);
3206 } else if (contents[1] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "Listen")) {
3207 const char *type, *path;
3209 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(ss)");
3211 return bus_log_parse_error(r);
3213 while ((r = sd_bus_message_read(m, "(ss)", &type, &path)) > 0) {
3215 r = strv_extend(&i->listen, type);
3219 r = strv_extend(&i->listen, path);
3224 return bus_log_parse_error(r);
3226 r = sd_bus_message_exit_container(m);
3228 return bus_log_parse_error(r);
3232 } else if (contents[1] == SD_BUS_TYPE_STRING && streq(name, "DropInPaths")) {
3234 r = sd_bus_message_read_strv(m, &i->dropin_paths);
3236 return bus_log_parse_error(r);
3238 } else if (contents[1] == SD_BUS_TYPE_STRING && streq(name, "Documentation")) {
3240 r = sd_bus_message_read_strv(m, &i->documentation);
3242 return bus_log_parse_error(r);
3244 } else if (contents[1] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "Conditions")) {
3245 const char *cond, *param;
3246 int trigger, negate;
3249 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(sbbsi)");
3251 return bus_log_parse_error(r);
3253 while ((r = sd_bus_message_read(m, "(sbbsi)", &cond, &trigger, &negate, ¶m, &state)) > 0) {
3254 log_debug("%s %d %d %s %d", cond, trigger, negate, param, state);
3255 if (state < 0 && (!trigger || !i->failed_condition)) {
3256 i->failed_condition = cond;
3257 i->failed_condition_trigger = trigger;
3258 i->failed_condition_negate = negate;
3259 i->failed_condition_param = param;
3263 return bus_log_parse_error(r);
3265 r = sd_bus_message_exit_container(m);
3267 return bus_log_parse_error(r);
3274 case SD_BUS_TYPE_STRUCT_BEGIN:
3276 if (streq(name, "LoadError")) {
3277 const char *n, *message;
3279 r = sd_bus_message_read(m, "(ss)", &n, &message);
3281 return bus_log_parse_error(r);
3283 if (!isempty(message))
3284 i->load_error = message;
3297 r = sd_bus_message_skip(m, contents);
3299 return bus_log_parse_error(r);
3304 static int print_property(const char *name, sd_bus_message *m, const char *contents) {
3310 /* This is a low-level property printer, see
3311 * print_status_info() for the nicer output */
3313 if (arg_properties && !strv_find(arg_properties, name)) {
3314 /* skip what we didn't read */
3315 r = sd_bus_message_skip(m, contents);
3319 switch (contents[0]) {
3321 case SD_BUS_TYPE_STRUCT_BEGIN:
3323 if (contents[1] == SD_BUS_TYPE_UINT32 && streq(name, "Job")) {
3326 r = sd_bus_message_read(m, "(uo)", &u, NULL);
3328 return bus_log_parse_error(r);
3331 printf("%s=%u\n", name, (unsigned) u);
3333 printf("%s=\n", name);
3337 } else if (contents[1] == SD_BUS_TYPE_STRING && streq(name, "Unit")) {
3340 r = sd_bus_message_read(m, "(so)", &s, NULL);
3342 return bus_log_parse_error(r);
3344 if (arg_all || !isempty(s))
3345 printf("%s=%s\n", name, s);
3349 } else if (contents[1] == SD_BUS_TYPE_STRING && streq(name, "LoadError")) {
3350 const char *a = NULL, *b = NULL;
3352 r = sd_bus_message_read(m, "(ss)", &a, &b);
3354 return bus_log_parse_error(r);
3356 if (arg_all || !isempty(a) || !isempty(b))
3357 printf("%s=%s \"%s\"\n", name, strempty(a), strempty(b));
3364 case SD_BUS_TYPE_ARRAY:
3366 if (contents[1] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "EnvironmentFiles")) {
3370 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(sb)");
3372 return bus_log_parse_error(r);
3374 while ((r = sd_bus_message_read(m, "(sb)", &path, &ignore)) > 0)
3375 printf("EnvironmentFile=%s (ignore_errors=%s)\n", path, yes_no(ignore));
3378 return bus_log_parse_error(r);
3380 r = sd_bus_message_exit_container(m);
3382 return bus_log_parse_error(r);
3386 } else if (contents[1] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "Paths")) {
3387 const char *type, *path;
3389 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(ss)");
3391 return bus_log_parse_error(r);
3393 while ((r = sd_bus_message_read(m, "(ss)", &type, &path)) > 0)
3394 printf("%s=%s\n", type, path);
3396 return bus_log_parse_error(r);
3398 r = sd_bus_message_exit_container(m);
3400 return bus_log_parse_error(r);
3404 } else if (contents[1] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "Listen")) {
3405 const char *type, *path;
3407 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(ss)");
3409 return bus_log_parse_error(r);
3411 while ((r = sd_bus_message_read(m, "(ss)", &type, &path)) > 0)
3412 printf("Listen%s=%s\n", type, path);
3414 return bus_log_parse_error(r);
3416 r = sd_bus_message_exit_container(m);
3418 return bus_log_parse_error(r);
3422 } else if (contents[1] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "Timers")) {
3424 uint64_t value, next_elapse;
3426 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(stt)");
3428 return bus_log_parse_error(r);
3430 while ((r = sd_bus_message_read(m, "(stt)", &base, &value, &next_elapse)) > 0) {
3431 char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX];
3433 printf("%s={ value=%s ; next_elapse=%s }\n",
3435 format_timespan(timespan1, sizeof(timespan1), value, 0),
3436 format_timespan(timespan2, sizeof(timespan2), next_elapse, 0));
3439 return bus_log_parse_error(r);
3441 r = sd_bus_message_exit_container(m);
3443 return bus_log_parse_error(r);
3447 } else if (contents[1] == SD_BUS_TYPE_STRUCT_BEGIN && startswith(name, "Exec")) {
3448 ExecStatusInfo info = {};
3450 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(sasbttttuii)");
3452 return bus_log_parse_error(r);
3454 while ((r = exec_status_info_deserialize(m, &info)) > 0) {
3455 char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
3456 _cleanup_free_ char *tt;
3458 tt = strv_join(info.argv, " ");
3460 printf("%s={ path=%s ; argv[]=%s ; ignore_errors=%s ; start_time=[%s] ; stop_time=[%s] ; pid=%u ; code=%s ; status=%i%s%s }\n",
3464 yes_no(info.ignore),
3465 strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)),
3466 strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)),
3467 (unsigned) info. pid,
3468 sigchld_code_to_string(info.code),
3470 info.code == CLD_EXITED ? "" : "/",
3471 strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status)));
3474 strv_free(info.argv);
3478 r = sd_bus_message_exit_container(m);
3480 return bus_log_parse_error(r);
3484 } else if (contents[1] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "DeviceAllow")) {
3485 const char *path, *rwm;
3487 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(ss)");
3489 return bus_log_parse_error(r);
3491 while ((r = sd_bus_message_read(m, "(ss)", &path, &rwm)) > 0)
3492 printf("%s=%s %s\n", name, strna(path), strna(rwm));
3494 return bus_log_parse_error(r);
3496 r = sd_bus_message_exit_container(m);
3498 return bus_log_parse_error(r);
3502 } else if (contents[1] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "BlockIODeviceWeight")) {
3506 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(st)");
3508 return bus_log_parse_error(r);
3510 while ((r = sd_bus_message_read(m, "(st)", &path, &weight)) > 0)
3511 printf("%s=%s %" PRIu64 "\n", name, strna(path), weight);
3513 return bus_log_parse_error(r);
3515 r = sd_bus_message_exit_container(m);
3517 return bus_log_parse_error(r);
3521 } else if (contents[1] == SD_BUS_TYPE_STRUCT_BEGIN && (streq(name, "BlockIOReadBandwidth") || streq(name, "BlockIOWriteBandwidth"))) {
3525 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(st)");
3527 return bus_log_parse_error(r);
3529 while ((r = sd_bus_message_read(m, "(st)", &path, &bandwidth)) > 0)
3530 printf("%s=%s %" PRIu64 "\n", name, strna(path), bandwidth);
3532 return bus_log_parse_error(r);
3534 r = sd_bus_message_exit_container(m);
3536 return bus_log_parse_error(r);
3544 r = bus_print_property(name, m, arg_all);
3546 return bus_log_parse_error(r);
3549 r = sd_bus_message_skip(m, contents);
3551 return bus_log_parse_error(r);
3554 printf("%s=[unprintable]\n", name);
3560 static int show_one(
3564 bool show_properties,
3568 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
3569 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
3570 UnitStatusInfo info = {};
3577 r = sd_bus_call_method(
3579 "org.freedesktop.systemd1",
3581 "org.freedesktop.DBus.Properties",
3587 log_error("Failed to get properties: %s", bus_error_message(&error, r));
3591 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "{sv}");
3593 return bus_log_parse_error(r);
3600 while ((r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_DICT_ENTRY, "sv")) > 0) {
3601 const char *name, *contents;
3603 r = sd_bus_message_read(reply, "s", &name);
3605 return bus_log_parse_error(r);
3607 r = sd_bus_message_peek_type(reply, NULL, &contents);
3609 return bus_log_parse_error(r);
3611 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_VARIANT, contents);
3613 return bus_log_parse_error(r);
3615 if (show_properties)
3616 r = print_property(name, reply, contents);
3618 r = status_property(name, reply, &info, contents);
3622 r = sd_bus_message_exit_container(reply);
3624 return bus_log_parse_error(r);
3626 r = sd_bus_message_exit_container(reply);
3628 return bus_log_parse_error(r);
3631 return bus_log_parse_error(r);
3633 r = sd_bus_message_exit_container(reply);
3635 return bus_log_parse_error(r);
3639 if (!show_properties) {
3640 if (streq(verb, "help"))
3641 show_unit_help(&info);
3643 print_status_info(&info, ellipsized);
3646 strv_free(info.documentation);
3647 strv_free(info.dropin_paths);
3648 strv_free(info.listen);
3650 if (!streq_ptr(info.active_state, "active") &&
3651 !streq_ptr(info.active_state, "reloading") &&
3652 streq(verb, "status")) {
3653 /* According to LSB: "program not running" */
3654 /* 0: program is running or service is OK
3655 * 1: program is dead and /var/run pid file exists
3656 * 2: program is dead and /var/lock lock file exists
3657 * 3: program is not running
3658 * 4: program or service status is unknown
3660 if (info.pid_file && access(info.pid_file, F_OK) == 0)
3666 while ((p = info.exec)) {
3667 LIST_REMOVE(exec, info.exec, p);
3668 exec_status_info_free(p);
3674 static int get_unit_dbus_path_by_pid(
3679 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
3680 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
3683 r = sd_bus_call_method(
3685 "org.freedesktop.systemd1",
3686 "/org/freedesktop/systemd1",
3687 "org.freedesktop.systemd1.Manager",
3693 log_error("Failed to get unit for PID %lu: %s", (unsigned long) pid, bus_error_message(&error, r));
3697 r = sd_bus_message_read(reply, "o", unit);
3699 return bus_log_parse_error(r);
3704 static int show_all(
3707 bool show_properties,
3711 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
3712 _cleanup_free_ UnitInfo *unit_infos = NULL;
3717 r = get_unit_list(bus, &reply, &unit_infos);
3721 pager_open_if_enabled();
3725 qsort_safe(unit_infos, c, sizeof(UnitInfo), compare_unit_info);
3727 for (u = unit_infos; u < unit_infos + c; u++) {
3728 _cleanup_free_ char *p = NULL;
3730 if (!output_show_unit(u, NULL))
3733 p = unit_dbus_path_from_name(u->id);
3737 r = show_one(verb, bus, p, show_properties, new_line, ellipsized);
3745 static int cat(sd_bus *bus, char **args) {
3746 _cleanup_free_ char *unit = NULL, *n = NULL;
3754 pager_open_if_enabled();
3756 STRV_FOREACH(name, args+1) {
3757 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
3758 _cleanup_strv_free_ char **dropin_paths = NULL;
3759 _cleanup_free_ char *fragment_path = NULL;
3762 n = unit_name_mangle(*name);
3766 unit = unit_dbus_path_from_name(n);
3770 if (need_daemon_reload(bus, n) > 0)
3771 log_warning("Unit file of %s changed on disk. Run 'systemctl%s daemon-reload'.",
3772 n, arg_scope == UNIT_FILE_SYSTEM ? "" : " --user");
3774 r = sd_bus_get_property_string(
3776 "org.freedesktop.systemd1",
3778 "org.freedesktop.systemd1.Unit",
3783 log_warning("Failed to get FragmentPath: %s", bus_error_message(&error, r));
3787 r = sd_bus_get_property_strv(
3789 "org.freedesktop.systemd1",
3791 "org.freedesktop.systemd1.Unit",
3796 log_warning("Failed to get DropInPaths: %s", bus_error_message(&error, r));
3805 if (!isempty(fragment_path)) {
3806 printf("%s# %s%s\n",
3807 ansi_highlight_blue(),
3809 ansi_highlight_off());
3812 r = sendfile_full(STDOUT_FILENO, fragment_path);
3814 log_warning("Failed to cat %s: %s", fragment_path, strerror(-r));
3819 STRV_FOREACH(path, dropin_paths) {
3821 isempty(fragment_path) && path == dropin_paths ? "" : "\n",
3825 r = sendfile_full(STDOUT_FILENO, *path);
3827 log_warning("Failed to cat %s: %s", *path, strerror(-r));
3833 return r < 0 ? r : 0;
3836 static int show(sd_bus *bus, char **args) {
3838 bool show_properties, show_status, new_line = false;
3840 bool ellipsized = false;
3845 show_properties = streq(args[0], "show");
3846 show_status = streq(args[0], "status");
3848 if (show_properties)
3849 pager_open_if_enabled();
3851 /* If no argument is specified inspect the manager itself */
3853 if (show_properties && strv_length(args) <= 1)
3854 return show_one(args[0], bus, "/org/freedesktop/systemd1", show_properties, &new_line, &ellipsized);
3856 if (show_status && strv_length(args) <= 1)
3857 ret = show_all(args[0], bus, false, &new_line, &ellipsized);
3859 STRV_FOREACH(name, args+1) {
3860 _cleanup_free_ char *unit = NULL;
3863 if (safe_atou32(*name, &id) < 0) {
3864 _cleanup_free_ char *n = NULL;
3865 /* Interpret as unit name */
3867 n = unit_name_mangle(*name);
3871 unit = unit_dbus_path_from_name(n);
3875 } else if (show_properties) {
3876 /* Interpret as job id */
3877 if (asprintf(&unit, "/org/freedesktop/systemd1/job/%u", id) < 0)
3881 /* Interpret as PID */
3882 r = get_unit_dbus_path_by_pid(bus, id, &unit);
3887 show_one(args[0], bus, unit, show_properties, &new_line, &ellipsized);
3890 if (ellipsized && !arg_quiet)
3891 printf("Hint: Some lines were ellipsized, use -l to show in full.\n");
3896 static int append_assignment(sd_bus_message *m, const char *assignment) {
3904 eq = strchr(assignment, '=');
3906 log_error("Not an assignment: %s", assignment);
3910 field = strndupa(assignment, eq - assignment);
3913 r = sd_bus_message_append_basic(m, SD_BUS_TYPE_STRING, field);
3915 return bus_log_create_error(r);
3917 if (streq(field, "CPUAccounting") ||
3918 streq(field, "MemoryAccounting") ||
3919 streq(field, "BlockIOAccounting")) {
3921 r = parse_boolean(eq);
3923 log_error("Failed to parse boolean assignment %s.", assignment);
3927 r = sd_bus_message_append(m, "v", "b", r);
3929 } else if (streq(field, "MemoryLimit")) {
3932 r = parse_bytes(eq, &bytes);
3934 log_error("Failed to parse bytes specification %s", assignment);
3938 r = sd_bus_message_append(m, "v", "t", (uint64_t) bytes);
3940 } else if (streq(field, "CPUShares") || streq(field, "BlockIOWeight")) {
3943 r = safe_atou64(eq, &u);
3945 log_error("Failed to parse %s value %s.", field, eq);
3949 r = sd_bus_message_append(m, "v", "t", u);
3951 } else if (streq(field, "DevicePolicy"))
3952 r = sd_bus_message_append(m, "v", "s", eq);
3954 else if (streq(field, "DeviceAllow")) {
3957 r = sd_bus_message_append(m, "v", "a(ss)", 0);
3959 const char *path, *rwm;
3962 e = strchr(eq, ' ');
3964 path = strndupa(eq, e - eq);
3971 if (!path_startswith(path, "/dev")) {
3972 log_error("%s is not a device file in /dev.", path);
3976 r = sd_bus_message_append(m, "v", "a(ss)", 1, path, rwm);
3979 } else if (streq(field, "BlockIOReadBandwidth") || streq(field, "BlockIOWriteBandwidth")) {
3982 r = sd_bus_message_append(m, "v", "a(st)", 0);
3984 const char *path, *bandwidth;
3988 e = strchr(eq, ' ');
3990 path = strndupa(eq, e - eq);
3993 log_error("Failed to parse %s value %s.", field, eq);
3997 if (!path_startswith(path, "/dev")) {
3998 log_error("%s is not a device file in /dev.", path);
4002 r = parse_bytes(bandwidth, &bytes);
4004 log_error("Failed to parse byte value %s.", bandwidth);
4008 r = sd_bus_message_append(m, "v", "a(st)", 1, path, (uint64_t) bytes);
4011 } else if (streq(field, "BlockIODeviceWeight")) {
4014 r = sd_bus_message_append(m, "v", "a(st)", 0);
4016 const char *path, *weight;
4020 e = strchr(eq, ' ');
4022 path = strndupa(eq, e - eq);
4025 log_error("Failed to parse %s value %s.", field, eq);
4029 if (!path_startswith(path, "/dev")) {
4030 log_error("%s is not a device file in /dev.", path);
4034 r = safe_atou64(weight, &u);
4036 log_error("Failed to parse %s value %s.", field, weight);
4039 r = sd_bus_message_append(m, "v", "a(st)", path, u);
4043 log_error("Unknown assignment %s.", assignment);
4048 return bus_log_create_error(r);
4053 static int set_property(sd_bus *bus, char **args) {
4054 _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
4055 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
4056 _cleanup_free_ char *n = NULL;
4060 r = sd_bus_message_new_method_call(
4062 "org.freedesktop.systemd1",
4063 "/org/freedesktop/systemd1",
4064 "org.freedesktop.systemd1.Manager",
4065 "SetUnitProperties",
4068 return bus_log_create_error(r);
4070 n = unit_name_mangle(args[1]);
4074 r = sd_bus_message_append(m, "sb", n, arg_runtime);
4076 return bus_log_create_error(r);
4078 r = sd_bus_message_open_container(m, SD_BUS_TYPE_ARRAY, "(sv)");
4080 return bus_log_create_error(r);
4082 STRV_FOREACH(i, args + 2) {
4083 r = sd_bus_message_open_container(m, SD_BUS_TYPE_STRUCT, "sv");
4085 return bus_log_create_error(r);
4087 r = append_assignment(m, *i);
4091 r = sd_bus_message_close_container(m);
4093 return bus_log_create_error(r);
4096 r = sd_bus_message_close_container(m);
4098 return bus_log_create_error(r);
4100 r = sd_bus_call(bus, m, 0, &error, NULL);
4102 log_error("Failed to set unit properties on %s: %s", n, bus_error_message(&error, r));
4109 static int snapshot(sd_bus *bus, char **args) {
4110 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
4111 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
4112 _cleanup_free_ char *n = NULL, *id = NULL;
4116 if (strv_length(args) > 1)
4117 n = unit_name_mangle_with_suffix(args[1], ".snapshot");
4123 r = sd_bus_call_method(
4125 "org.freedesktop.systemd1",
4126 "/org/freedesktop/systemd1",
4127 "org.freedesktop.systemd1.Manager",
4133 log_error("Failed to create snapshot: %s", bus_error_message(&error, r));
4137 r = sd_bus_message_read(reply, "o", &path);
4139 return bus_log_parse_error(r);
4141 r = sd_bus_get_property_string(
4143 "org.freedesktop.systemd1",
4145 "org.freedesktop.systemd1.Unit",
4150 log_error("Failed to get ID of snapshot: %s", bus_error_message(&error, r));
4160 static int delete_snapshot(sd_bus *bus, char **args) {
4161 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
4167 STRV_FOREACH(name, args+1) {
4168 _cleanup_free_ char *n = NULL;
4170 n = unit_name_mangle_with_suffix(*name, ".snapshot");
4174 r = sd_bus_call_method(
4176 "org.freedesktop.systemd1",
4177 "/org/freedesktop/systemd1",
4178 "org.freedesktop.systemd1.Manager",
4184 log_error("Failed to remove snapshot %s: %s", n, bus_error_message(&error, r));
4192 static int daemon_reload(sd_bus *bus, char **args) {
4193 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
4197 if (arg_action == ACTION_RELOAD)
4199 else if (arg_action == ACTION_REEXEC)
4200 method = "Reexecute";
4202 assert(arg_action == ACTION_SYSTEMCTL);
4205 streq(args[0], "clear-jobs") ||
4206 streq(args[0], "cancel") ? "ClearJobs" :
4207 streq(args[0], "daemon-reexec") ? "Reexecute" :
4208 streq(args[0], "reset-failed") ? "ResetFailed" :
4209 streq(args[0], "halt") ? "Halt" :
4210 streq(args[0], "poweroff") ? "PowerOff" :
4211 streq(args[0], "reboot") ? "Reboot" :
4212 streq(args[0], "kexec") ? "KExec" :
4213 streq(args[0], "exit") ? "Exit" :
4214 /* "daemon-reload" */ "Reload";
4217 r = sd_bus_call_method(
4219 "org.freedesktop.systemd1",
4220 "/org/freedesktop/systemd1",
4221 "org.freedesktop.systemd1.Manager",
4227 if (r == -ENOENT && arg_action != ACTION_SYSTEMCTL)
4228 /* There's always a fallback possible for
4229 * legacy actions. */
4231 else if ((r == -ETIMEDOUT || r == -ECONNRESET) && streq(method, "Reexecute"))
4232 /* On reexecution, we expect a disconnect, not a
4236 log_error("Failed to execute operation: %s", bus_error_message(&error, r));
4238 return r < 0 ? r : 0;
4241 static int reset_failed(sd_bus *bus, char **args) {
4242 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
4246 if (strv_length(args) <= 1)
4247 return daemon_reload(bus, args);
4249 STRV_FOREACH(name, args+1) {
4250 _cleanup_free_ char *n;
4252 n = unit_name_mangle(*name);
4256 r = sd_bus_call_method(
4258 "org.freedesktop.systemd1",
4259 "/org/freedesktop/systemd1",
4260 "org.freedesktop.systemd1.Manager",
4266 log_error("Failed to reset failed state of unit %s: %s", n, bus_error_message(&error, r));
4274 static int show_environment(sd_bus *bus, char **args) {
4275 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
4276 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
4280 pager_open_if_enabled();
4282 r = sd_bus_get_property(
4284 "org.freedesktop.systemd1",
4285 "/org/freedesktop/systemd1",
4286 "org.freedesktop.systemd1.Manager",
4292 log_error("Failed to get environment: %s", bus_error_message(&error, r));
4296 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "s");
4298 return bus_log_parse_error(r);
4300 while ((r = sd_bus_message_read_basic(reply, SD_BUS_TYPE_STRING, &text)) > 0)
4303 return bus_log_parse_error(r);
4305 r = sd_bus_message_exit_container(reply);
4307 return bus_log_parse_error(r);
4312 static int switch_root(sd_bus *bus, char **args) {
4313 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
4314 _cleanup_free_ char *init = NULL;
4319 l = strv_length(args);
4320 if (l < 2 || l > 3) {
4321 log_error("Wrong number of arguments.");
4328 init = strdup(args[2]);
4330 parse_env_file("/proc/cmdline", WHITESPACE,
4341 log_debug("switching root - root: %s; init: %s", root, init);
4343 r = sd_bus_call_method(
4345 "org.freedesktop.systemd1",
4346 "/org/freedesktop/systemd1",
4347 "org.freedesktop.systemd1.Manager",
4353 log_error("Failed to switch root: %s", bus_error_message(&error, r));
4360 static int set_environment(sd_bus *bus, char **args) {
4361 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
4362 _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
4369 method = streq(args[0], "set-environment")
4371 : "UnsetEnvironment";
4373 r = sd_bus_message_new_method_call(
4375 "org.freedesktop.systemd1",
4376 "/org/freedesktop/systemd1",
4377 "org.freedesktop.systemd1.Manager",
4381 return bus_log_create_error(r);
4383 r = sd_bus_message_append_strv(m, args + 1);
4385 return bus_log_create_error(r);
4387 r = sd_bus_call(bus, m, 0, &error, NULL);
4389 log_error("Failed to set environment: %s", bus_error_message(&error, r));
4396 static int enable_sysv_units(const char *verb, char **args) {
4399 #if defined(HAVE_SYSV_COMPAT) && defined(HAVE_CHKCONFIG)
4400 unsigned f = 1, t = 1;
4401 _cleanup_lookup_paths_free_ LookupPaths paths = {};
4403 if (arg_scope != UNIT_FILE_SYSTEM)
4406 if (!streq(verb, "enable") &&
4407 !streq(verb, "disable") &&
4408 !streq(verb, "is-enabled"))
4411 /* Processes all SysV units, and reshuffles the array so that
4412 * afterwards only the native units remain */
4414 r = lookup_paths_init(&paths, SYSTEMD_SYSTEM, false, NULL, NULL, NULL);
4419 for (f = 0; args[f]; f++) {
4421 _cleanup_free_ char *p = NULL, *q = NULL;
4422 bool found_native = false, found_sysv;
4424 const char *argv[6] = { "/sbin/chkconfig", NULL, NULL, NULL, NULL };
4432 if (!endswith(name, ".service"))
4435 if (path_is_absolute(name))
4438 STRV_FOREACH(k, paths.unit_path) {
4439 if (!isempty(arg_root))
4440 asprintf(&p, "%s/%s/%s", arg_root, *k, name);
4442 asprintf(&p, "%s/%s", *k, name);
4449 found_native = access(p, F_OK) >= 0;
4460 if (!isempty(arg_root))
4461 asprintf(&p, "%s/" SYSTEM_SYSVINIT_PATH "/%s", arg_root, name);
4463 asprintf(&p, SYSTEM_SYSVINIT_PATH "/%s", name);
4469 p[strlen(p) - sizeof(".service") + 1] = 0;
4470 found_sysv = access(p, F_OK) >= 0;
4475 /* Mark this entry, so that we don't try enabling it as native unit */
4476 args[f] = (char*) "";
4478 log_info("%s is not a native service, redirecting to /sbin/chkconfig.", name);
4480 if (!isempty(arg_root))
4481 argv[c++] = q = strappend("--root=", arg_root);
4483 argv[c++] = basename(p);
4485 streq(verb, "enable") ? "on" :
4486 streq(verb, "disable") ? "off" : "--level=5";
4489 l = strv_join((char**)argv, " ");
4495 log_info("Executing %s", l);
4500 log_error("Failed to fork: %m");
4503 } else if (pid == 0) {
4506 execv(argv[0], (char**) argv);
4507 _exit(EXIT_FAILURE);
4510 j = wait_for_terminate(pid, &status);
4512 log_error("Failed to wait for child: %s", strerror(-r));
4517 if (status.si_code == CLD_EXITED) {
4518 if (streq(verb, "is-enabled")) {
4519 if (status.si_status == 0) {
4528 } else if (status.si_status != 0) {
4539 /* Drop all SysV units */
4540 for (f = 0, t = 0; args[f]; f++) {
4542 if (isempty(args[f]))
4545 args[t++] = args[f];
4554 static int mangle_names(char **original_names, char ***mangled_names) {
4555 char **i, **l, **name;
4557 l = new(char*, strv_length(original_names) + 1);
4562 STRV_FOREACH(name, original_names) {
4564 /* When enabling units qualified path names are OK,
4565 * too, hence allow them explicitly. */
4570 *i = unit_name_mangle(*name);
4586 static int enable_unit(sd_bus *bus, char **args) {
4587 _cleanup_strv_free_ char **mangled_names = NULL;
4588 const char *verb = args[0];
4589 UnitFileChange *changes = NULL;
4590 unsigned n_changes = 0;
4591 int carries_install_info = -1;
4597 r = mangle_names(args+1, &mangled_names);
4601 r = enable_sysv_units(verb, mangled_names);
4605 if (!bus || avoid_bus()) {
4606 if (streq(verb, "enable")) {
4607 r = unit_file_enable(arg_scope, arg_runtime, arg_root, mangled_names, arg_force, &changes, &n_changes);
4608 carries_install_info = r;
4609 } else if (streq(verb, "disable"))
4610 r = unit_file_disable(arg_scope, arg_runtime, arg_root, mangled_names, &changes, &n_changes);
4611 else if (streq(verb, "reenable")) {
4612 r = unit_file_reenable(arg_scope, arg_runtime, arg_root, mangled_names, arg_force, &changes, &n_changes);
4613 carries_install_info = r;
4614 } else if (streq(verb, "link"))
4615 r = unit_file_link(arg_scope, arg_runtime, arg_root, mangled_names, arg_force, &changes, &n_changes);
4616 else if (streq(verb, "preset")) {
4617 r = unit_file_preset(arg_scope, arg_runtime, arg_root, mangled_names, arg_force, &changes, &n_changes);
4618 carries_install_info = r;
4619 } else if (streq(verb, "mask"))
4620 r = unit_file_mask(arg_scope, arg_runtime, arg_root, mangled_names, arg_force, &changes, &n_changes);
4621 else if (streq(verb, "unmask"))
4622 r = unit_file_unmask(arg_scope, arg_runtime, arg_root, mangled_names, &changes, &n_changes);
4624 assert_not_reached("Unknown verb");
4627 log_error("Operation failed: %s", strerror(-r));
4632 dump_unit_file_changes(changes, n_changes);
4636 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL, *m = NULL;
4637 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
4638 int expect_carries_install_info = false;
4639 bool send_force = true;
4642 if (streq(verb, "enable")) {
4643 method = "EnableUnitFiles";
4644 expect_carries_install_info = true;
4645 } else if (streq(verb, "disable")) {
4646 method = "DisableUnitFiles";
4648 } else if (streq(verb, "reenable")) {
4649 method = "ReenableUnitFiles";
4650 expect_carries_install_info = true;
4651 } else if (streq(verb, "link"))
4652 method = "LinkUnitFiles";
4653 else if (streq(verb, "preset")) {
4654 method = "PresetUnitFiles";
4655 expect_carries_install_info = true;
4656 } else if (streq(verb, "mask"))
4657 method = "MaskUnitFiles";
4658 else if (streq(verb, "unmask")) {
4659 method = "UnmaskUnitFiles";
4662 assert_not_reached("Unknown verb");
4664 r = sd_bus_message_new_method_call(
4666 "org.freedesktop.systemd1",
4667 "/org/freedesktop/systemd1",
4668 "org.freedesktop.systemd1.Manager",
4672 return bus_log_create_error(r);
4674 r = sd_bus_message_append_strv(m, mangled_names);
4676 return bus_log_create_error(r);
4678 r = sd_bus_message_append(m, "b", arg_runtime);
4680 return bus_log_create_error(r);
4683 r = sd_bus_message_append(m, "b", arg_force);
4685 return bus_log_create_error(r);
4688 r = sd_bus_call(bus, m, 0, &error, &reply);
4690 log_error("Failed to execute operation: %s", bus_error_message(&error, r));
4694 if (expect_carries_install_info) {
4695 r = sd_bus_message_read(reply, "b", &carries_install_info);
4697 return bus_log_parse_error(r);
4700 r = deserialize_and_dump_unit_file_changes(reply);
4704 /* Try to reload if enabeld */
4706 r = daemon_reload(bus, args);
4711 if (carries_install_info == 0)
4712 log_warning("The unit files have no [Install] section. They are not meant to be enabled\n"
4713 "using systemctl.\n"
4714 "Possible reasons for having this kind of units are:\n"
4715 "1) A unit may be statically enabled by being symlinked from another unit's\n"
4716 " .wants/ or .requires/ directory.\n"
4717 "2) A unit's purpose may be to act as a helper for some other unit which has\n"
4718 " a requirement dependency on it.\n"
4719 "3) A unit may be started when needed via activation (socket, path, timer,\n"
4720 " D-Bus, udev, scripted systemctl call, ...).\n");
4723 unit_file_changes_free(changes, n_changes);
4728 static int unit_is_enabled(sd_bus *bus, char **args) {
4730 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
4731 _cleanup_strv_free_ char **mangled_names = NULL;
4736 r = mangle_names(args+1, &mangled_names);
4740 r = enable_sysv_units(args[0], mangled_names);
4746 if (!bus || avoid_bus()) {
4748 STRV_FOREACH(name, mangled_names) {
4749 UnitFileState state;
4751 state = unit_file_get_state(arg_scope, arg_root, *name);
4753 log_error("Failed to get unit file state for %s: %s", *name, strerror(-state));
4757 if (state == UNIT_FILE_ENABLED ||
4758 state == UNIT_FILE_ENABLED_RUNTIME ||
4759 state == UNIT_FILE_STATIC)
4763 puts(unit_file_state_to_string(state));
4767 STRV_FOREACH(name, mangled_names) {
4768 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
4771 r = sd_bus_call_method(
4773 "org.freedesktop.systemd1",
4774 "/org/freedesktop/systemd1",
4775 "org.freedesktop.systemd1.Manager",
4781 log_error("Failed to get unit file state for %s: %s", *name, bus_error_message(&error, r));
4785 r = sd_bus_message_read(reply, "s", &s);
4787 return bus_log_parse_error(r);
4789 if (streq(s, "enabled") ||
4790 streq(s, "enabled-runtime") ||
4802 static int systemctl_help(void) {
4804 pager_open_if_enabled();
4806 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
4807 "Query or send control commands to the systemd manager.\n\n"
4808 " -h --help Show this help\n"
4809 " --version Show package version\n"
4810 " --system Connect to system manager\n"
4811 " --user Connect to user service manager\n"
4812 " -H --host=[USER@]HOST\n"
4813 " Operate on remote host\n"
4814 " -M --machine=CONTAINER\n"
4815 " Operate on local container\n"
4816 " -t --type=TYPE List only units of a particular type\n"
4817 " --state=STATE List only units with particular LOAD or SUB or ACTIVE state\n"
4818 " -p --property=NAME Show only properties by this name\n"
4819 " -a --all Show all loaded units/properties, including dead/empty\n"
4820 " ones. To list all units installed on the system, use\n"
4821 " the 'list-unit-files' command instead.\n"
4822 " -l --full Don't ellipsize unit names on output\n"
4823 " --reverse Show reverse dependencies with 'list-dependencies'\n"
4824 " --job-mode=MODE Specify how to deal with already queued jobs, when\n"
4825 " queueing a new job\n"
4826 " --show-types When showing sockets, explicitly show their type\n"
4827 " -i --ignore-inhibitors\n"
4828 " When shutting down or sleeping, ignore inhibitors\n"
4829 " --kill-who=WHO Who to send signal to\n"
4830 " -s --signal=SIGNAL Which signal to send\n"
4831 " -q --quiet Suppress output\n"
4832 " --no-block Do not wait until operation finished\n"
4833 " --no-wall Don't send wall message before halt/power-off/reboot\n"
4834 " --no-reload When enabling/disabling unit files, don't reload daemon\n"
4836 " --no-legend Do not print a legend (column headers and hints)\n"
4837 " --no-pager Do not pipe output into a pager\n"
4838 " --no-ask-password\n"
4839 " Do not ask for system passwords\n"
4840 " --global Enable/disable unit files globally\n"
4841 " --runtime Enable unit files only temporarily until next reboot\n"
4842 " -f --force When enabling unit files, override existing symlinks\n"
4843 " When shutting down, execute action immediately\n"
4844 " --root=PATH Enable unit files in the specified root directory\n"
4845 " -n --lines=INTEGER Number of journal entries to show\n"
4846 " -o --output=STRING Change journal output mode (short, short-monotonic,\n"
4847 " verbose, export, json, json-pretty, json-sse, cat)\n"
4848 " --plain Print unit dependencies as a list instead of a tree\n\n"
4850 " list-units [PATTERN...] List loaded units\n"
4851 " list-sockets [PATTERN...] List loaded sockets ordered by address\n"
4852 " list-timers [PATTERN...] List loaded timers ordered by next elapse\n"
4853 " start NAME... Start (activate) one or more units\n"
4854 " stop NAME... Stop (deactivate) one or more units\n"
4855 " reload NAME... Reload one or more units\n"
4856 " restart NAME... Start or restart one or more units\n"
4857 " try-restart NAME... Restart one or more units if active\n"
4858 " reload-or-restart NAME... Reload one or more units if possible,\n"
4859 " otherwise start or restart\n"
4860 " reload-or-try-restart NAME... Reload one or more units if possible,\n"
4861 " otherwise restart if active\n"
4862 " isolate NAME Start one unit and stop all others\n"
4863 " kill NAME... Send signal to processes of a unit\n"
4864 " is-active NAME... Check whether units are active\n"
4865 " is-failed NAME... Check whether units are failed\n"
4866 " status [NAME...|PID...] Show runtime status of one or more units\n"
4867 " show [NAME...|JOB...] Show properties of one or more\n"
4868 " units/jobs or the manager\n"
4869 " cat NAME... Show files and drop-ins of one or more units\n"
4870 " set-property NAME ASSIGNMENT... Sets one or more properties of a unit\n"
4871 " help NAME...|PID... Show manual for one or more units\n"
4872 " reset-failed [NAME...] Reset failed state for all, one, or more\n"
4874 " list-dependencies [NAME] Recursively show units which are required\n"
4875 " or wanted by this unit or by which this\n"
4876 " unit is required or wanted\n\n"
4877 "Unit File Commands:\n"
4878 " list-unit-files [PATTERN...] List installed unit files\n"
4879 " enable NAME... Enable one or more unit files\n"
4880 " disable NAME... Disable one or more unit files\n"
4881 " reenable NAME... Reenable one or more unit files\n"
4882 " preset NAME... Enable/disable one or more unit files\n"
4883 " based on preset configuration\n"
4884 " is-enabled NAME... Check whether unit files are enabled\n\n"
4885 " mask NAME... Mask one or more units\n"
4886 " unmask NAME... Unmask one or more units\n"
4887 " link PATH... Link one or more units files into\n"
4888 " the search path\n"
4889 " get-default Get the name of the default target\n"
4890 " set-default NAME Set the default target\n\n"
4892 " list-jobs [PATTERN...] List jobs\n"
4893 " cancel [JOB...] Cancel all, one, or more jobs\n\n"
4894 "Snapshot Commands:\n"
4895 " snapshot [NAME] Create a snapshot\n"
4896 " delete NAME... Remove one or more snapshots\n\n"
4897 "Environment Commands:\n"
4898 " show-environment Dump environment\n"
4899 " set-environment NAME=VALUE... Set one or more environment variables\n"
4900 " unset-environment NAME... Unset one or more environment variables\n\n"
4901 "Manager Lifecycle Commands:\n"
4902 " daemon-reload Reload systemd manager configuration\n"
4903 " daemon-reexec Reexecute systemd manager\n\n"
4904 "System Commands:\n"
4905 " default Enter system default mode\n"
4906 " rescue Enter system rescue mode\n"
4907 " emergency Enter system emergency mode\n"
4908 " halt Shut down and halt the system\n"
4909 " poweroff Shut down and power-off the system\n"
4910 " reboot [ARG] Shut down and reboot the system\n"
4911 " kexec Shut down and reboot the system with kexec\n"
4912 " exit Request user instance exit\n"
4913 " switch-root ROOT [INIT] Change to a different root file system\n"
4914 " suspend Suspend the system\n"
4915 " hibernate Hibernate the system\n"
4916 " hybrid-sleep Hibernate and suspend the system\n",
4917 program_invocation_short_name);
4922 static int halt_help(void) {
4924 printf("%s [OPTIONS...]%s\n\n"
4925 "%s the system.\n\n"
4926 " --help Show this help\n"
4927 " --halt Halt the machine\n"
4928 " -p --poweroff Switch off the machine\n"
4929 " --reboot Reboot the machine\n"
4930 " -f --force Force immediate halt/power-off/reboot\n"
4931 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
4932 " -d --no-wtmp Don't write wtmp record\n"
4933 " --no-wall Don't send wall message before halt/power-off/reboot\n",
4934 program_invocation_short_name,
4935 arg_action == ACTION_REBOOT ? " [ARG]" : "",
4936 arg_action == ACTION_REBOOT ? "Reboot" :
4937 arg_action == ACTION_POWEROFF ? "Power off" :
4943 static int shutdown_help(void) {
4945 printf("%s [OPTIONS...] [TIME] [WALL...]\n\n"
4946 "Shut down the system.\n\n"
4947 " --help Show this help\n"
4948 " -H --halt Halt the machine\n"
4949 " -P --poweroff Power-off the machine\n"
4950 " -r --reboot Reboot the machine\n"
4951 " -h Equivalent to --poweroff, overridden by --halt\n"
4952 " -k Don't halt/power-off/reboot, just send warnings\n"
4953 " --no-wall Don't send wall message before halt/power-off/reboot\n"
4954 " -c Cancel a pending shutdown\n",
4955 program_invocation_short_name);
4960 static int telinit_help(void) {
4962 printf("%s [OPTIONS...] {COMMAND}\n\n"
4963 "Send control commands to the init daemon.\n\n"
4964 " --help Show this help\n"
4965 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
4967 " 0 Power-off the machine\n"
4968 " 6 Reboot the machine\n"
4969 " 2, 3, 4, 5 Start runlevelX.target unit\n"
4970 " 1, s, S Enter rescue mode\n"
4971 " q, Q Reload init daemon configuration\n"
4972 " u, U Reexecute init daemon\n",
4973 program_invocation_short_name);
4978 static int runlevel_help(void) {
4980 printf("%s [OPTIONS...]\n\n"
4981 "Prints the previous and current runlevel of the init system.\n\n"
4982 " --help Show this help\n",
4983 program_invocation_short_name);
4988 static int help_types(void) {
4992 puts("Available unit types:");
4993 for (i = 0; i < _UNIT_TYPE_MAX; i++) {
4994 t = unit_type_to_string(i);
5002 static int systemctl_parse_argv(int argc, char *argv[]) {
5011 ARG_IGNORE_DEPENDENCIES,
5023 ARG_NO_ASK_PASSWORD,
5032 static const struct option options[] = {
5033 { "help", no_argument, NULL, 'h' },
5034 { "version", no_argument, NULL, ARG_VERSION },
5035 { "type", required_argument, NULL, 't' },
5036 { "property", required_argument, NULL, 'p' },
5037 { "all", no_argument, NULL, 'a' },
5038 { "reverse", no_argument, NULL, ARG_REVERSE },
5039 { "after", no_argument, NULL, ARG_AFTER },
5040 { "before", no_argument, NULL, ARG_BEFORE },
5041 { "show-types", no_argument, NULL, ARG_SHOW_TYPES },
5042 { "failed", no_argument, NULL, ARG_FAILED }, /* compatibility only */
5043 { "full", no_argument, NULL, 'l' },
5044 { "job-mode", required_argument, NULL, ARG_JOB_MODE },
5045 { "fail", no_argument, NULL, ARG_FAIL }, /* compatibility only */
5046 { "irreversible", no_argument, NULL, ARG_IRREVERSIBLE }, /* compatibility only */
5047 { "ignore-dependencies", no_argument, NULL, ARG_IGNORE_DEPENDENCIES }, /* compatibility only */
5048 { "ignore-inhibitors", no_argument, NULL, 'i' },
5049 { "user", no_argument, NULL, ARG_USER },
5050 { "system", no_argument, NULL, ARG_SYSTEM },
5051 { "global", no_argument, NULL, ARG_GLOBAL },
5052 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
5053 { "no-legend", no_argument, NULL, ARG_NO_LEGEND },
5054 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
5055 { "no-wall", no_argument, NULL, ARG_NO_WALL },
5056 { "quiet", no_argument, NULL, 'q' },
5057 { "root", required_argument, NULL, ARG_ROOT },
5058 { "force", no_argument, NULL, ARG_FORCE },
5059 { "no-reload", no_argument, NULL, ARG_NO_RELOAD },
5060 { "kill-who", required_argument, NULL, ARG_KILL_WHO },
5061 { "signal", required_argument, NULL, 's' },
5062 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
5063 { "host", required_argument, NULL, 'H' },
5064 { "machine", required_argument, NULL, 'M' },
5065 { "runtime", no_argument, NULL, ARG_RUNTIME },
5066 { "lines", required_argument, NULL, 'n' },
5067 { "output", required_argument, NULL, 'o' },
5068 { "plain", no_argument, NULL, ARG_PLAIN },
5069 { "state", required_argument, NULL, ARG_STATE },
5078 while ((c = getopt_long(argc, argv, "ht:p:alqfs:H:M:n:o:i", options, NULL)) >= 0) {
5083 return systemctl_help();
5086 puts(PACKAGE_STRING);
5087 puts(SYSTEMD_FEATURES);
5094 FOREACH_WORD_SEPARATOR(word, size, optarg, ",", state) {
5095 _cleanup_free_ char *type;
5097 type = strndup(word, size);
5101 if (streq(type, "help")) {
5106 if (unit_type_from_string(type) >= 0) {
5107 if (strv_push(&arg_types, type))
5113 /* It's much nicer to use --state= for
5114 * load states, but let's support this
5115 * in --types= too for compatibility
5116 * with old versions */
5117 if (unit_load_state_from_string(optarg) >= 0) {
5118 if (strv_push(&arg_states, type) < 0)
5124 log_error("Unknown unit type or load state '%s'.", type);
5125 log_info("Use -t help to see a list of allowed values.");
5133 /* Make sure that if the empty property list
5134 was specified, we won't show any properties. */
5135 if (isempty(optarg) && !arg_properties) {
5136 arg_properties = new0(char*, 1);
5137 if (!arg_properties)
5143 FOREACH_WORD_SEPARATOR(word, size, optarg, ",", state) {
5146 prop = strndup(word, size);
5150 if (strv_push(&arg_properties, prop) < 0) {
5157 /* If the user asked for a particular
5158 * property, show it to him, even if it is
5170 arg_dependency = DEPENDENCY_REVERSE;
5174 arg_dependency = DEPENDENCY_AFTER;
5178 arg_dependency = DEPENDENCY_BEFORE;
5181 case ARG_SHOW_TYPES:
5182 arg_show_types = true;
5186 arg_job_mode = optarg;
5190 arg_job_mode = "fail";
5193 case ARG_IRREVERSIBLE:
5194 arg_job_mode = "replace-irreversibly";
5197 case ARG_IGNORE_DEPENDENCIES:
5198 arg_job_mode = "ignore-dependencies";
5202 arg_scope = UNIT_FILE_USER;
5206 arg_scope = UNIT_FILE_SYSTEM;
5210 arg_scope = UNIT_FILE_GLOBAL;
5214 arg_no_block = true;
5218 arg_no_legend = true;
5222 arg_no_pager = true;
5238 if (strv_extend(&arg_states, "failed") < 0)
5256 arg_no_reload = true;
5260 arg_kill_who = optarg;
5264 if ((arg_signal = signal_from_string_try_harder(optarg)) < 0) {
5265 log_error("Failed to parse signal string %s.", optarg);
5270 case ARG_NO_ASK_PASSWORD:
5271 arg_ask_password = false;
5275 arg_transport = BUS_TRANSPORT_REMOTE;
5280 arg_transport = BUS_TRANSPORT_CONTAINER;
5289 if (safe_atou(optarg, &arg_lines) < 0) {
5290 log_error("Failed to parse lines '%s'", optarg);
5296 arg_output = output_mode_from_string(optarg);
5297 if (arg_output < 0) {
5298 log_error("Unknown output '%s'.", optarg);
5304 arg_ignore_inhibitors = true;
5315 FOREACH_WORD_SEPARATOR(word, size, optarg, ",", state) {
5318 s = strndup(word, size);
5322 if (strv_push(&arg_states, s) < 0) {
5334 assert_not_reached("Unhandled option");
5338 if (arg_transport != BUS_TRANSPORT_LOCAL && arg_scope != UNIT_FILE_SYSTEM) {
5339 log_error("Cannot access user instance remotely.");
5346 static int halt_parse_argv(int argc, char *argv[]) {
5355 static const struct option options[] = {
5356 { "help", no_argument, NULL, ARG_HELP },
5357 { "halt", no_argument, NULL, ARG_HALT },
5358 { "poweroff", no_argument, NULL, 'p' },
5359 { "reboot", no_argument, NULL, ARG_REBOOT },
5360 { "force", no_argument, NULL, 'f' },
5361 { "wtmp-only", no_argument, NULL, 'w' },
5362 { "no-wtmp", no_argument, NULL, 'd' },
5363 { "no-wall", no_argument, NULL, ARG_NO_WALL },
5372 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
5373 if (runlevel == '0' || runlevel == '6')
5376 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
5383 arg_action = ACTION_HALT;
5387 if (arg_action != ACTION_REBOOT)
5388 arg_action = ACTION_POWEROFF;
5392 arg_action = ACTION_REBOOT;
5414 /* Compatibility nops */
5421 assert_not_reached("Unhandled option");
5425 if (arg_action == ACTION_REBOOT && argc == optind + 1) {
5426 r = write_string_file(REBOOT_PARAM_FILE, argv[optind]);
5428 log_error("Failed to write reboot param to "
5429 REBOOT_PARAM_FILE": %s", strerror(-r));
5432 } else if (optind < argc) {
5433 log_error("Too many arguments.");
5440 static int parse_time_spec(const char *t, usec_t *_u) {
5444 if (streq(t, "now"))
5446 else if (!strchr(t, ':')) {
5449 if (safe_atou64(t, &u) < 0)
5452 *_u = now(CLOCK_REALTIME) + USEC_PER_MINUTE * u;
5461 hour = strtol(t, &e, 10);
5462 if (errno > 0 || *e != ':' || hour < 0 || hour > 23)
5465 minute = strtol(e+1, &e, 10);
5466 if (errno > 0 || *e != 0 || minute < 0 || minute > 59)
5469 n = now(CLOCK_REALTIME);
5470 s = (time_t) (n / USEC_PER_SEC);
5472 assert_se(localtime_r(&s, &tm));
5474 tm.tm_hour = (int) hour;
5475 tm.tm_min = (int) minute;
5478 assert_se(s = mktime(&tm));
5480 *_u = (usec_t) s * USEC_PER_SEC;
5483 *_u += USEC_PER_DAY;
5489 static int shutdown_parse_argv(int argc, char *argv[]) {
5496 static const struct option options[] = {
5497 { "help", no_argument, NULL, ARG_HELP },
5498 { "halt", no_argument, NULL, 'H' },
5499 { "poweroff", no_argument, NULL, 'P' },
5500 { "reboot", no_argument, NULL, 'r' },
5501 { "kexec", no_argument, NULL, 'K' }, /* not documented extension */
5502 { "no-wall", no_argument, NULL, ARG_NO_WALL },
5511 while ((c = getopt_long(argc, argv, "HPrhkt:afFc", options, NULL)) >= 0) {
5515 return shutdown_help();
5518 arg_action = ACTION_HALT;
5522 arg_action = ACTION_POWEROFF;
5527 arg_action = ACTION_KEXEC;
5529 arg_action = ACTION_REBOOT;
5533 arg_action = ACTION_KEXEC;
5537 if (arg_action != ACTION_HALT)
5538 arg_action = ACTION_POWEROFF;
5551 /* Compatibility nops */
5555 arg_action = ACTION_CANCEL_SHUTDOWN;
5562 assert_not_reached("Unhandled option");
5566 if (argc > optind && arg_action != ACTION_CANCEL_SHUTDOWN) {
5567 r = parse_time_spec(argv[optind], &arg_when);
5569 log_error("Failed to parse time specification: %s", argv[optind]);
5573 arg_when = now(CLOCK_REALTIME) + USEC_PER_MINUTE;
5575 if (argc > optind && arg_action == ACTION_CANCEL_SHUTDOWN)
5576 /* No time argument for shutdown cancel */
5577 arg_wall = argv + optind;
5578 else if (argc > optind + 1)
5579 /* We skip the time argument */
5580 arg_wall = argv + optind + 1;
5587 static int telinit_parse_argv(int argc, char *argv[]) {
5594 static const struct option options[] = {
5595 { "help", no_argument, NULL, ARG_HELP },
5596 { "no-wall", no_argument, NULL, ARG_NO_WALL },
5600 static const struct {
5604 { '0', ACTION_POWEROFF },
5605 { '6', ACTION_REBOOT },
5606 { '1', ACTION_RESCUE },
5607 { '2', ACTION_RUNLEVEL2 },
5608 { '3', ACTION_RUNLEVEL3 },
5609 { '4', ACTION_RUNLEVEL4 },
5610 { '5', ACTION_RUNLEVEL5 },
5611 { 's', ACTION_RESCUE },
5612 { 'S', ACTION_RESCUE },
5613 { 'q', ACTION_RELOAD },
5614 { 'Q', ACTION_RELOAD },
5615 { 'u', ACTION_REEXEC },
5616 { 'U', ACTION_REEXEC }
5625 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
5629 return telinit_help();
5639 assert_not_reached("Unhandled option");
5643 if (optind >= argc) {
5648 if (optind + 1 < argc) {
5649 log_error("Too many arguments.");
5653 if (strlen(argv[optind]) != 1) {
5654 log_error("Expected single character argument.");
5658 for (i = 0; i < ELEMENTSOF(table); i++)
5659 if (table[i].from == argv[optind][0])
5662 if (i >= ELEMENTSOF(table)) {
5663 log_error("Unknown command '%s'.", argv[optind]);
5667 arg_action = table[i].to;
5674 static int runlevel_parse_argv(int argc, char *argv[]) {
5680 static const struct option options[] = {
5681 { "help", no_argument, NULL, ARG_HELP },
5690 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
5694 return runlevel_help();
5701 assert_not_reached("Unhandled option");
5705 if (optind < argc) {
5706 log_error("Too many arguments.");
5713 static int parse_argv(int argc, char *argv[]) {
5717 if (program_invocation_short_name) {
5719 if (strstr(program_invocation_short_name, "halt")) {
5720 arg_action = ACTION_HALT;
5721 return halt_parse_argv(argc, argv);
5722 } else if (strstr(program_invocation_short_name, "poweroff")) {
5723 arg_action = ACTION_POWEROFF;
5724 return halt_parse_argv(argc, argv);
5725 } else if (strstr(program_invocation_short_name, "reboot")) {
5727 arg_action = ACTION_KEXEC;
5729 arg_action = ACTION_REBOOT;
5730 return halt_parse_argv(argc, argv);
5731 } else if (strstr(program_invocation_short_name, "shutdown")) {
5732 arg_action = ACTION_POWEROFF;
5733 return shutdown_parse_argv(argc, argv);
5734 } else if (strstr(program_invocation_short_name, "init")) {
5736 if (sd_booted() > 0) {
5737 arg_action = _ACTION_INVALID;
5738 return telinit_parse_argv(argc, argv);
5740 /* Hmm, so some other init system is
5741 * running, we need to forward this
5742 * request to it. For now we simply
5743 * guess that it is Upstart. */
5745 execv(TELINIT, argv);
5747 log_error("Couldn't find an alternative telinit implementation to spawn.");
5751 } else if (strstr(program_invocation_short_name, "runlevel")) {
5752 arg_action = ACTION_RUNLEVEL;
5753 return runlevel_parse_argv(argc, argv);
5757 arg_action = ACTION_SYSTEMCTL;
5758 return systemctl_parse_argv(argc, argv);
5761 _pure_ static int action_to_runlevel(void) {
5763 static const char table[_ACTION_MAX] = {
5764 [ACTION_HALT] = '0',
5765 [ACTION_POWEROFF] = '0',
5766 [ACTION_REBOOT] = '6',
5767 [ACTION_RUNLEVEL2] = '2',
5768 [ACTION_RUNLEVEL3] = '3',
5769 [ACTION_RUNLEVEL4] = '4',
5770 [ACTION_RUNLEVEL5] = '5',
5771 [ACTION_RESCUE] = '1'
5774 assert(arg_action < _ACTION_MAX);
5776 return table[arg_action];
5779 static int talk_initctl(void) {
5781 struct init_request request = {
5782 .magic = INIT_MAGIC,
5784 .cmd = INIT_CMD_RUNLVL
5787 _cleanup_close_ int fd = -1;
5791 rl = action_to_runlevel();
5795 request.runlevel = rl;
5797 fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY);
5799 if (errno == ENOENT)
5802 log_error("Failed to open "INIT_FIFO": %m");
5807 r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
5809 log_error("Failed to write to "INIT_FIFO": %m");
5810 return errno > 0 ? -errno : -EIO;
5816 static int systemctl_main(sd_bus *bus, int argc, char *argv[], int bus_error) {
5818 static const struct {
5826 int (* const dispatch)(sd_bus *bus, char **args);
5832 { "list-units", MORE, 0, list_units },
5833 { "list-unit-files", MORE, 1, list_unit_files, NOBUS },
5834 { "list-sockets", MORE, 1, list_sockets },
5835 { "list-timers", MORE, 1, list_timers },
5836 { "list-jobs", MORE, 1, list_jobs },
5837 { "clear-jobs", EQUAL, 1, daemon_reload },
5838 { "cancel", MORE, 2, cancel_job },
5839 { "start", MORE, 2, start_unit },
5840 { "stop", MORE, 2, start_unit },
5841 { "condstop", MORE, 2, start_unit }, /* For compatibility with ALTLinux */
5842 { "reload", MORE, 2, start_unit },
5843 { "restart", MORE, 2, start_unit },
5844 { "try-restart", MORE, 2, start_unit },
5845 { "reload-or-restart", MORE, 2, start_unit },
5846 { "reload-or-try-restart", MORE, 2, start_unit },
5847 { "force-reload", MORE, 2, start_unit }, /* For compatibility with SysV */
5848 { "condreload", MORE, 2, start_unit }, /* For compatibility with ALTLinux */
5849 { "condrestart", MORE, 2, start_unit }, /* For compatibility with RH */
5850 { "isolate", EQUAL, 2, start_unit },
5851 { "kill", MORE, 2, kill_unit },
5852 { "is-active", MORE, 2, check_unit_active },
5853 { "check", MORE, 2, check_unit_active },
5854 { "is-failed", MORE, 2, check_unit_failed },
5855 { "show", MORE, 1, show },
5856 { "cat", MORE, 2, cat },
5857 { "status", MORE, 1, show },
5858 { "help", MORE, 2, show },
5859 { "snapshot", LESS, 2, snapshot },
5860 { "delete", MORE, 2, delete_snapshot },
5861 { "daemon-reload", EQUAL, 1, daemon_reload },
5862 { "daemon-reexec", EQUAL, 1, daemon_reload },
5863 { "show-environment", EQUAL, 1, show_environment },
5864 { "set-environment", MORE, 2, set_environment },
5865 { "unset-environment", MORE, 2, set_environment },
5866 { "halt", EQUAL, 1, start_special, FORCE },
5867 { "poweroff", EQUAL, 1, start_special, FORCE },
5868 { "reboot", EQUAL, 1, start_special, FORCE },
5869 { "kexec", EQUAL, 1, start_special },
5870 { "suspend", EQUAL, 1, start_special },
5871 { "hibernate", EQUAL, 1, start_special },
5872 { "hybrid-sleep", EQUAL, 1, start_special },
5873 { "default", EQUAL, 1, start_special },
5874 { "rescue", EQUAL, 1, start_special },
5875 { "emergency", EQUAL, 1, start_special },
5876 { "exit", EQUAL, 1, start_special },
5877 { "reset-failed", MORE, 1, reset_failed },
5878 { "enable", MORE, 2, enable_unit, NOBUS },
5879 { "disable", MORE, 2, enable_unit, NOBUS },
5880 { "is-enabled", MORE, 2, unit_is_enabled, NOBUS },
5881 { "reenable", MORE, 2, enable_unit, NOBUS },
5882 { "preset", MORE, 2, enable_unit, NOBUS },
5883 { "mask", MORE, 2, enable_unit, NOBUS },
5884 { "unmask", MORE, 2, enable_unit, NOBUS },
5885 { "link", MORE, 2, enable_unit, NOBUS },
5886 { "switch-root", MORE, 2, switch_root },
5887 { "list-dependencies", LESS, 2, list_dependencies },
5888 { "set-default", EQUAL, 2, set_default, NOBUS },
5889 { "get-default", EQUAL, 1, get_default, NOBUS },
5890 { "set-property", MORE, 3, set_property },
5899 left = argc - optind;
5901 /* Special rule: no arguments (left == 0) means "list-units" */
5903 if (streq(argv[optind], "help") && !argv[optind+1]) {
5904 log_error("This command expects one or more "
5905 "unit names. Did you mean --help?");
5909 for (; verb->verb; verb++)
5910 if (streq(argv[optind], verb->verb))
5913 log_error("Unknown operation '%s'.", argv[optind]);
5918 switch (verb->argc_cmp) {
5921 if (left != verb->argc) {
5922 log_error("Invalid number of arguments.");
5929 if (left < verb->argc) {
5930 log_error("Too few arguments.");
5937 if (left > verb->argc) {
5938 log_error("Too many arguments.");
5945 assert_not_reached("Unknown comparison operator.");
5948 /* Require a bus connection for all operations but
5950 if (verb->bus == NOBUS) {
5951 if (!bus && !avoid_bus()) {
5952 log_error("Failed to get D-Bus connection: %s", strerror(-bus_error));
5957 if (running_in_chroot() > 0) {
5958 log_info("Running in chroot, ignoring request.");
5962 if ((verb->bus != FORCE || arg_force <= 0) && !bus) {
5963 log_error("Failed to get D-Bus connection: %s", strerror(-bus_error));
5968 return verb->dispatch(bus, argv + optind);
5971 static int send_shutdownd(usec_t t, char mode, bool dry_run, bool warn, const char *message) {
5973 struct sd_shutdown_command c = {
5980 union sockaddr_union sockaddr = {
5981 .un.sun_family = AF_UNIX,
5982 .un.sun_path = "/run/systemd/shutdownd",
5985 struct iovec iovec[2] = {{
5986 .iov_base = (char*) &c,
5987 .iov_len = offsetof(struct sd_shutdown_command, wall_message),
5990 struct msghdr msghdr = {
5991 .msg_name = &sockaddr,
5992 .msg_namelen = offsetof(struct sockaddr_un, sun_path)
5993 + sizeof("/run/systemd/shutdownd") - 1,
5998 _cleanup_close_ int fd;
6000 fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
6004 if (!isempty(message)) {
6005 iovec[1].iov_base = (char*) message;
6006 iovec[1].iov_len = strlen(message);
6007 msghdr.msg_iovlen++;
6010 if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) < 0)
6016 static int reload_with_fallback(sd_bus *bus) {
6019 /* First, try systemd via D-Bus. */
6020 if (daemon_reload(bus, NULL) >= 0)
6024 /* Nothing else worked, so let's try signals */
6025 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
6027 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
6028 log_error("kill() failed: %m");
6035 static int start_with_fallback(sd_bus *bus) {
6038 /* First, try systemd via D-Bus. */
6039 if (start_unit(bus, NULL) >= 0)
6043 /* Nothing else worked, so let's try
6045 if (talk_initctl() > 0)
6048 log_error("Failed to talk to init daemon.");
6052 warn_wall(arg_action);
6056 static int halt_now(enum action a) {
6058 /* Make sure C-A-D is handled by the kernel from this
6060 reboot(RB_ENABLE_CAD);
6065 log_info("Halting.");
6066 reboot(RB_HALT_SYSTEM);
6069 case ACTION_POWEROFF:
6070 log_info("Powering off.");
6071 reboot(RB_POWER_OFF);
6074 case ACTION_REBOOT: {
6075 _cleanup_free_ char *param = NULL;
6077 if (read_one_line_file(REBOOT_PARAM_FILE, ¶m) >= 0) {
6078 log_info("Rebooting with argument '%s'.", param);
6079 syscall(SYS_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
6080 LINUX_REBOOT_CMD_RESTART2, param);
6083 log_info("Rebooting.");
6084 reboot(RB_AUTOBOOT);
6089 assert_not_reached("Unknown action.");
6093 static int halt_main(sd_bus *bus) {
6096 r = check_inhibitors(bus, arg_action);
6100 if (geteuid() != 0) {
6101 /* Try logind if we are a normal user and no special
6102 * mode applies. Maybe PolicyKit allows us to shutdown
6105 if (arg_when <= 0 &&
6108 (arg_action == ACTION_POWEROFF ||
6109 arg_action == ACTION_REBOOT)) {
6110 r = reboot_with_logind(bus, arg_action);
6115 log_error("Must be root.");
6120 _cleanup_free_ char *m;
6122 m = strv_join(arg_wall, " ");
6126 r = send_shutdownd(arg_when,
6127 arg_action == ACTION_HALT ? 'H' :
6128 arg_action == ACTION_POWEROFF ? 'P' :
6129 arg_action == ACTION_KEXEC ? 'K' :
6136 log_warning("Failed to talk to shutdownd, proceeding with immediate shutdown: %s", strerror(-r));
6138 char date[FORMAT_TIMESTAMP_MAX];
6140 log_info("Shutdown scheduled for %s, use 'shutdown -c' to cancel.",
6141 format_timestamp(date, sizeof(date), arg_when));
6146 if (!arg_dry && !arg_force)
6147 return start_with_fallback(bus);
6150 if (sd_booted() > 0)
6151 log_debug("Not writing utmp record, assuming that systemd-update-utmp is used.");
6153 r = utmp_put_shutdown();
6155 log_warning("Failed to write utmp record: %s", strerror(-r));
6162 r = halt_now(arg_action);
6163 log_error("Failed to reboot: %s", strerror(-r));
6168 static int runlevel_main(void) {
6169 int r, runlevel, previous;
6171 r = utmp_get_runlevel(&runlevel, &previous);
6178 previous <= 0 ? 'N' : previous,
6179 runlevel <= 0 ? 'N' : runlevel);
6184 int main(int argc, char*argv[]) {
6185 _cleanup_bus_unref_ sd_bus *bus = NULL;
6188 setlocale(LC_ALL, "");
6189 log_parse_environment();
6192 /* Explicitly not on_tty() to avoid setting cached value.
6193 * This becomes relevant for piping output which might be
6195 original_stdout_is_tty = isatty(STDOUT_FILENO);
6197 r = parse_argv(argc, argv);
6201 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
6202 * let's shortcut this */
6203 if (arg_action == ACTION_RUNLEVEL) {
6204 r = runlevel_main();
6208 if (running_in_chroot() > 0 && arg_action != ACTION_SYSTEMCTL) {
6209 log_info("Running in chroot, ignoring request.");
6215 r = bus_open_transport_systemd(arg_transport, arg_host, arg_scope != UNIT_FILE_SYSTEM, &bus);
6217 /* systemctl_main() will print an error message for the bus
6218 * connection, but only if it needs to */
6220 switch (arg_action) {
6222 case ACTION_SYSTEMCTL:
6223 r = systemctl_main(bus, argc, argv, r);
6227 case ACTION_POWEROFF:
6233 case ACTION_RUNLEVEL2:
6234 case ACTION_RUNLEVEL3:
6235 case ACTION_RUNLEVEL4:
6236 case ACTION_RUNLEVEL5:
6238 case ACTION_EMERGENCY:
6239 case ACTION_DEFAULT:
6240 r = start_with_fallback(bus);
6245 r = reload_with_fallback(bus);
6248 case ACTION_CANCEL_SHUTDOWN: {
6249 _cleanup_free_ char *m = NULL;
6252 m = strv_join(arg_wall, " ");
6259 r = send_shutdownd(arg_when, SD_SHUTDOWN_NONE, false, !arg_no_wall, m);
6261 log_warning("Failed to talk to shutdownd, shutdown hasn't been cancelled: %s", strerror(-r));
6265 case ACTION_RUNLEVEL:
6266 case _ACTION_INVALID:
6268 assert_not_reached("Unknown action");
6273 ask_password_agent_close();
6274 polkit_agent_close();
6276 strv_free(arg_types);
6277 strv_free(arg_states);
6278 strv_free(arg_properties);
6280 return r < 0 ? EXIT_FAILURE : r;