1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/reboot.h>
28 #include <sys/ioctl.h>
32 #include <sys/socket.h>
35 #include <sys/prctl.h>
36 #include <dbus/dbus.h>
38 #include <systemd/sd-daemon.h>
39 #include <systemd/sd-shutdown.h>
45 #include "utmp-wtmp.h"
48 #include "path-util.h"
50 #include "dbus-common.h"
51 #include "cgroup-show.h"
52 #include "cgroup-util.h"
54 #include "path-lookup.h"
55 #include "conf-parser.h"
56 #include "exit-status.h"
57 #include "bus-errors.h"
59 #include "unit-name.h"
61 #include "spawn-ask-password-agent.h"
62 #include "spawn-polkit-agent.h"
64 #include "logs-show.h"
65 #include "path-util.h"
67 static const char *arg_type = NULL;
68 static char **arg_property = NULL;
69 static bool arg_all = false;
70 static const char *arg_job_mode = "replace";
71 static UnitFileScope arg_scope = UNIT_FILE_SYSTEM;
72 static bool arg_no_block = false;
73 static bool arg_no_legend = false;
74 static bool arg_no_pager = false;
75 static bool arg_no_wtmp = false;
76 static bool arg_no_sync = false;
77 static bool arg_no_wall = false;
78 static bool arg_no_reload = false;
79 static bool arg_dry = false;
80 static bool arg_quiet = false;
81 static bool arg_full = false;
82 static int arg_force = 0;
83 static bool arg_ask_password = true;
84 static bool arg_failed = false;
85 static bool arg_runtime = false;
86 static char **arg_wall = NULL;
87 static const char *arg_kill_who = NULL;
88 static const char *arg_kill_mode = NULL;
89 static int arg_signal = SIGTERM;
90 static const char *arg_root = NULL;
91 static usec_t arg_when = 0;
112 ACTION_CANCEL_SHUTDOWN,
114 } arg_action = ACTION_SYSTEMCTL;
120 static enum transport {
124 } arg_transport = TRANSPORT_NORMAL;
125 static const char *arg_host = NULL;
126 static bool arg_follow = false;
127 static unsigned arg_lines = 10;
128 static OutputMode arg_output = OUTPUT_SHORT;
130 static bool private_bus = false;
132 static int daemon_reload(DBusConnection *bus, char **args);
133 static void halt_now(enum action a);
135 static bool on_tty(void) {
138 /* Note that this is invoked relatively early, before we start
139 * the pager. That means the value we return reflects whether
140 * we originally were started on a tty, not if we currently
141 * are. But this is intended, since we want colour and so on
142 * when run in our own pager. */
144 if (_unlikely_(t < 0))
145 t = isatty(STDOUT_FILENO) > 0;
150 static void pager_open_if_enabled(void) {
152 /* Cache result before we open the pager */
161 static void ask_password_agent_open_if_enabled(void) {
163 /* Open the password agent as a child process if necessary */
165 if (!arg_ask_password)
168 if (arg_scope != UNIT_FILE_SYSTEM)
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)
189 static const char *ansi_highlight_red(bool b) {
194 return b ? ANSI_HIGHLIGHT_RED_ON : ANSI_HIGHLIGHT_OFF;
197 static const char *ansi_highlight_green(bool b) {
202 return b ? ANSI_HIGHLIGHT_GREEN_ON : ANSI_HIGHLIGHT_OFF;
205 static bool error_is_no_service(const DBusError *error) {
208 if (!dbus_error_is_set(error))
211 if (dbus_error_has_name(error, DBUS_ERROR_NAME_HAS_NO_OWNER))
214 if (dbus_error_has_name(error, DBUS_ERROR_SERVICE_UNKNOWN))
217 return startswith(error->name, "org.freedesktop.DBus.Error.Spawn.");
220 static int translate_bus_error_to_exit_status(int r, const DBusError *error) {
223 if (!dbus_error_is_set(error))
226 if (dbus_error_has_name(error, DBUS_ERROR_ACCESS_DENIED) ||
227 dbus_error_has_name(error, BUS_ERROR_ONLY_BY_DEPENDENCY) ||
228 dbus_error_has_name(error, BUS_ERROR_NO_ISOLATION) ||
229 dbus_error_has_name(error, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE))
230 return EXIT_NOPERMISSION;
232 if (dbus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT))
233 return EXIT_NOTINSTALLED;
235 if (dbus_error_has_name(error, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE) ||
236 dbus_error_has_name(error, BUS_ERROR_NOT_SUPPORTED))
237 return EXIT_NOTIMPLEMENTED;
239 if (dbus_error_has_name(error, BUS_ERROR_LOAD_FAILED))
240 return EXIT_NOTCONFIGURED;
248 static void warn_wall(enum action a) {
249 static const char *table[_ACTION_MAX] = {
250 [ACTION_HALT] = "The system is going down for system halt NOW!",
251 [ACTION_REBOOT] = "The system is going down for reboot NOW!",
252 [ACTION_POWEROFF] = "The system is going down for power-off NOW!",
253 [ACTION_KEXEC] = "The system is going down for kexec reboot NOW!",
254 [ACTION_RESCUE] = "The system is going down to rescue mode NOW!",
255 [ACTION_EMERGENCY] = "The system is going down to emergency mode NOW!"
264 p = strv_join(arg_wall, " ");
266 log_error("Failed to join strings.");
282 utmp_wall(table[a], NULL);
285 static bool avoid_bus(void) {
287 if (running_in_chroot() > 0)
290 if (sd_booted() <= 0)
293 if (!isempty(arg_root))
296 if (arg_scope == UNIT_FILE_GLOBAL)
304 const char *description;
305 const char *load_state;
306 const char *active_state;
307 const char *sub_state;
308 const char *following;
309 const char *unit_path;
311 const char *job_type;
312 const char *job_path;
315 static int compare_unit_info(const void *a, const void *b) {
317 const struct unit_info *u = a, *v = b;
319 d1 = strrchr(u->id, '.');
320 d2 = strrchr(v->id, '.');
325 if ((r = strcasecmp(d1, d2)) != 0)
329 return strcasecmp(u->id, v->id);
332 static bool output_show_unit(const struct unit_info *u) {
336 return streq(u->active_state, "failed");
338 return (!arg_type || ((dot = strrchr(u->id, '.')) &&
339 streq(dot+1, arg_type))) &&
340 (arg_all || !(streq(u->active_state, "inactive") || u->following[0]) || u->job_id > 0);
343 static void output_units_list(const struct unit_info *unit_infos, unsigned c) {
344 unsigned id_len, max_id_len, active_len, sub_len, job_len, desc_len, n_shown = 0;
345 const struct unit_info *u;
347 max_id_len = sizeof("UNIT")-1;
348 active_len = sizeof("ACTIVE")-1;
349 sub_len = sizeof("SUB")-1;
350 job_len = sizeof("JOB")-1;
353 for (u = unit_infos; u < unit_infos + c; u++) {
354 if (!output_show_unit(u))
357 max_id_len = MAX(max_id_len, strlen(u->id));
358 active_len = MAX(active_len, strlen(u->active_state));
359 sub_len = MAX(sub_len, strlen(u->sub_state));
361 job_len = MAX(job_len, strlen(u->job_type));
366 id_len = MIN(max_id_len, 25);
367 basic_len = 5 + id_len + 6 + active_len + sub_len + job_len;
368 if (basic_len < (unsigned) columns()) {
369 unsigned extra_len, incr;
370 extra_len = columns() - basic_len;
371 /* Either UNIT already got 25, or is fully satisfied.
372 * Grant up to 25 to DESC now. */
373 incr = MIN(extra_len, 25);
376 /* split the remaining space between UNIT and DESC,
377 * but do not give UNIT more than it needs. */
379 incr = MIN(extra_len / 2, max_id_len - id_len);
381 desc_len += extra_len - incr;
387 if (!arg_no_legend) {
388 printf("%-*s %-6s %-*s %-*s %-*s ", id_len, "UNIT", "LOAD",
389 active_len, "ACTIVE", sub_len, "SUB", job_len, "JOB");
390 if (!arg_full && arg_no_pager)
391 printf("%.*s\n", desc_len, "DESCRIPTION");
393 printf("%s\n", "DESCRIPTION");
396 for (u = unit_infos; u < unit_infos + c; u++) {
398 const char *on_loaded, *off_loaded;
399 const char *on_active, *off_active;
401 if (!output_show_unit(u))
406 if (streq(u->load_state, "error")) {
407 on_loaded = ansi_highlight_red(true);
408 off_loaded = ansi_highlight_red(false);
410 on_loaded = off_loaded = "";
412 if (streq(u->active_state, "failed")) {
413 on_active = ansi_highlight_red(true);
414 off_active = ansi_highlight_red(false);
416 on_active = off_active = "";
418 e = arg_full ? NULL : ellipsize(u->id, id_len, 33);
420 printf("%-*s %s%-6s%s %s%-*s %-*s%s %-*s ",
421 id_len, e ? e : u->id,
422 on_loaded, u->load_state, off_loaded,
423 on_active, active_len, u->active_state,
424 sub_len, u->sub_state, off_active,
425 job_len, u->job_id ? u->job_type : "");
426 if (!arg_full && arg_no_pager)
427 printf("%.*s\n", desc_len, u->description);
429 printf("%s\n", u->description);
434 if (!arg_no_legend) {
435 printf("\nLOAD = Reflects whether the unit definition was properly loaded.\n"
436 "ACTIVE = The high-level unit activation state, i.e. generalization of SUB.\n"
437 "SUB = The low-level unit activation state, values depend on unit type.\n"
438 "JOB = Pending job for the unit.\n");
441 printf("\n%u units listed.\n", n_shown);
443 printf("\n%u units listed. Pass --all to see inactive units, too.\n", n_shown);
447 static int list_units(DBusConnection *bus, char **args) {
448 DBusMessage *m = NULL, *reply = NULL;
451 DBusMessageIter iter, sub, sub2;
452 unsigned c = 0, n_units = 0;
453 struct unit_info *unit_infos = NULL;
455 dbus_error_init(&error);
459 pager_open_if_enabled();
461 if (!(m = dbus_message_new_method_call(
462 "org.freedesktop.systemd1",
463 "/org/freedesktop/systemd1",
464 "org.freedesktop.systemd1.Manager",
466 log_error("Could not allocate message.");
470 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
471 log_error("Failed to issue method call: %s", bus_error_message(&error));
476 if (!dbus_message_iter_init(reply, &iter) ||
477 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
478 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
479 log_error("Failed to parse reply.");
484 dbus_message_iter_recurse(&iter, &sub);
486 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
489 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
490 log_error("Failed to parse reply.");
498 n_units = MAX(2*c, 16);
499 w = realloc(unit_infos, sizeof(struct unit_info) * n_units);
502 log_error("Failed to allocate unit array.");
512 dbus_message_iter_recurse(&sub, &sub2);
514 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->id, true) < 0 ||
515 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->description, true) < 0 ||
516 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->load_state, true) < 0 ||
517 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->active_state, true) < 0 ||
518 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->sub_state, true) < 0 ||
519 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->following, true) < 0 ||
520 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &u->unit_path, true) < 0 ||
521 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &u->job_id, true) < 0 ||
522 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->job_type, true) < 0 ||
523 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &u->job_path, false) < 0) {
524 log_error("Failed to parse reply.");
529 dbus_message_iter_next(&sub);
534 qsort(unit_infos, c, sizeof(struct unit_info), compare_unit_info);
535 output_units_list(unit_infos, c);
542 dbus_message_unref(m);
545 dbus_message_unref(reply);
549 dbus_error_free(&error);
554 static int compare_unit_file_list(const void *a, const void *b) {
556 const UnitFileList *u = a, *v = b;
558 d1 = strrchr(u->path, '.');
559 d2 = strrchr(v->path, '.');
564 r = strcasecmp(d1, d2);
569 return strcasecmp(path_get_file_name(u->path), path_get_file_name(v->path));
572 static bool output_show_unit_file(const UnitFileList *u) {
575 return !arg_type || ((dot = strrchr(u->path, '.')) && streq(dot+1, arg_type));
578 static void output_unit_file_list(const UnitFileList *units, unsigned c) {
579 unsigned max_id_len, id_cols, state_cols, n_shown = 0;
580 const UnitFileList *u;
582 max_id_len = sizeof("UNIT FILE")-1;
583 state_cols = sizeof("STATE")-1;
584 for (u = units; u < units + c; u++) {
585 if (!output_show_unit_file(u))
588 max_id_len = MAX(max_id_len, strlen(path_get_file_name(u->path)));
589 state_cols = MAX(state_cols, strlen(unit_file_state_to_string(u->state)));
594 id_cols = MIN(max_id_len, 25);
595 basic_cols = 1 + id_cols + state_cols;
596 if (basic_cols < (unsigned) columns())
597 id_cols += MIN(columns() - basic_cols, max_id_len - id_cols);
599 id_cols = max_id_len;
602 printf("%-*s %-*s\n", id_cols, "UNIT FILE", state_cols, "STATE");
604 for (u = units; u < units + c; u++) {
606 const char *on, *off;
609 if (!output_show_unit_file(u))
614 if (u->state == UNIT_FILE_MASKED ||
615 u->state == UNIT_FILE_MASKED_RUNTIME ||
616 u->state == UNIT_FILE_DISABLED) {
617 on = ansi_highlight_red(true);
618 off = ansi_highlight_red(false);
619 } else if (u->state == UNIT_FILE_ENABLED) {
620 on = ansi_highlight_green(true);
621 off = ansi_highlight_green(false);
625 id = path_get_file_name(u->path);
627 e = arg_full ? NULL : ellipsize(id, id_cols, 33);
629 printf("%-*s %s%-*s%s\n",
631 on, state_cols, unit_file_state_to_string(u->state), off);
637 printf("\n%u unit files listed.\n", n_shown);
640 static int list_unit_files(DBusConnection *bus, char **args) {
641 DBusMessage *m = NULL, *reply = NULL;
644 DBusMessageIter iter, sub, sub2;
645 unsigned c = 0, n_units = 0;
646 UnitFileList *units = NULL;
648 dbus_error_init(&error);
650 pager_open_if_enabled();
657 h = hashmap_new(string_hash_func, string_compare_func);
659 log_error("Out of memory");
663 r = unit_file_get_list(arg_scope, arg_root, h);
665 unit_file_list_free(h);
666 log_error("Failed to get unit file list: %s", strerror(-r));
670 n_units = hashmap_size(h);
671 units = new(UnitFileList, n_units);
673 unit_file_list_free(h);
674 log_error("Out of memory");
678 HASHMAP_FOREACH(u, h, i) {
679 memcpy(units + c++, u, sizeof(UnitFileList));
687 m = dbus_message_new_method_call(
688 "org.freedesktop.systemd1",
689 "/org/freedesktop/systemd1",
690 "org.freedesktop.systemd1.Manager",
693 log_error("Could not allocate message.");
697 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
699 log_error("Failed to issue method call: %s", bus_error_message(&error));
704 if (!dbus_message_iter_init(reply, &iter) ||
705 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
706 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
707 log_error("Failed to parse reply.");
712 dbus_message_iter_recurse(&iter, &sub);
714 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
718 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
719 log_error("Failed to parse reply.");
727 n_units = MAX(2*c, 16);
728 w = realloc(units, sizeof(struct UnitFileList) * n_units);
731 log_error("Failed to allocate unit array.");
741 dbus_message_iter_recurse(&sub, &sub2);
743 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->path, true) < 0 ||
744 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, false) < 0) {
745 log_error("Failed to parse reply.");
750 u->state = unit_file_state_from_string(state);
752 dbus_message_iter_next(&sub);
758 qsort(units, c, sizeof(UnitFileList), compare_unit_file_list);
759 output_unit_file_list(units, c);
766 dbus_message_unref(m);
769 dbus_message_unref(reply);
773 dbus_error_free(&error);
778 static int dot_one_property(const char *name, const char *prop, DBusMessageIter *iter) {
779 static const char * const colors[] = {
780 "Requires", "[color=\"black\"]",
781 "RequiresOverridable", "[color=\"black\"]",
782 "Requisite", "[color=\"darkblue\"]",
783 "RequisiteOverridable", "[color=\"darkblue\"]",
784 "Wants", "[color=\"darkgrey\"]",
785 "Conflicts", "[color=\"red\"]",
786 "ConflictedBy", "[color=\"red\"]",
787 "After", "[color=\"green\"]"
790 const char *c = NULL;
797 for (i = 0; i < ELEMENTSOF(colors); i += 2)
798 if (streq(colors[i], prop)) {
806 if (arg_dot != DOT_ALL)
807 if ((arg_dot == DOT_ORDER) != streq(prop, "After"))
810 switch (dbus_message_iter_get_arg_type(iter)) {
812 case DBUS_TYPE_ARRAY:
814 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
817 dbus_message_iter_recurse(iter, &sub);
819 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
822 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
823 dbus_message_iter_get_basic(&sub, &s);
824 printf("\t\"%s\"->\"%s\" %s;\n", name, s, c);
826 dbus_message_iter_next(&sub);
836 static int dot_one(DBusConnection *bus, const char *name, const char *path) {
837 DBusMessage *m = NULL, *reply = NULL;
838 const char *interface = "org.freedesktop.systemd1.Unit";
841 DBusMessageIter iter, sub, sub2, sub3;
846 dbus_error_init(&error);
848 if (!(m = dbus_message_new_method_call(
849 "org.freedesktop.systemd1",
851 "org.freedesktop.DBus.Properties",
853 log_error("Could not allocate message.");
858 if (!dbus_message_append_args(m,
859 DBUS_TYPE_STRING, &interface,
860 DBUS_TYPE_INVALID)) {
861 log_error("Could not append arguments to message.");
866 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
867 log_error("Failed to issue method call: %s", bus_error_message(&error));
872 if (!dbus_message_iter_init(reply, &iter) ||
873 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
874 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
875 log_error("Failed to parse reply.");
880 dbus_message_iter_recurse(&iter, &sub);
882 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
885 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
886 log_error("Failed to parse reply.");
891 dbus_message_iter_recurse(&sub, &sub2);
893 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &prop, true) < 0) {
894 log_error("Failed to parse reply.");
899 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
900 log_error("Failed to parse reply.");
905 dbus_message_iter_recurse(&sub2, &sub3);
907 if (dot_one_property(name, prop, &sub3)) {
908 log_error("Failed to parse reply.");
913 dbus_message_iter_next(&sub);
920 dbus_message_unref(m);
923 dbus_message_unref(reply);
925 dbus_error_free(&error);
930 static int dot(DBusConnection *bus, char **args) {
931 DBusMessage *m = NULL, *reply = NULL;
934 DBusMessageIter iter, sub, sub2;
936 dbus_error_init(&error);
940 if (!(m = dbus_message_new_method_call(
941 "org.freedesktop.systemd1",
942 "/org/freedesktop/systemd1",
943 "org.freedesktop.systemd1.Manager",
945 log_error("Could not allocate message.");
949 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
950 log_error("Failed to issue method call: %s", bus_error_message(&error));
955 if (!dbus_message_iter_init(reply, &iter) ||
956 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
957 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
958 log_error("Failed to parse reply.");
963 printf("digraph systemd {\n");
965 dbus_message_iter_recurse(&iter, &sub);
966 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
967 const char *id, *description, *load_state, *active_state, *sub_state, *following, *unit_path;
969 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
970 log_error("Failed to parse reply.");
975 dbus_message_iter_recurse(&sub, &sub2);
977 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
978 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &description, true) < 0 ||
979 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &load_state, true) < 0 ||
980 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &active_state, true) < 0 ||
981 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &sub_state, true) < 0 ||
982 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &following, true) < 0 ||
983 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, true) < 0) {
984 log_error("Failed to parse reply.");
989 if ((r = dot_one(bus, id, unit_path)) < 0)
992 /* printf("\t\"%s\";\n", id); */
993 dbus_message_iter_next(&sub);
998 log_info(" Color legend: black = Requires\n"
999 " dark blue = Requisite\n"
1000 " dark grey = Wants\n"
1001 " red = Conflicts\n"
1002 " green = After\n");
1005 log_notice("-- You probably want to process this output with graphviz' dot tool.\n"
1006 "-- Try a shell pipeline like 'systemctl dot | dot -Tsvg > systemd.svg'!\n");
1012 dbus_message_unref(m);
1015 dbus_message_unref(reply);
1017 dbus_error_free(&error);
1022 static int list_jobs(DBusConnection *bus, char **args) {
1023 DBusMessage *m = NULL, *reply = NULL;
1026 DBusMessageIter iter, sub, sub2;
1029 dbus_error_init(&error);
1033 pager_open_if_enabled();
1035 if (!(m = dbus_message_new_method_call(
1036 "org.freedesktop.systemd1",
1037 "/org/freedesktop/systemd1",
1038 "org.freedesktop.systemd1.Manager",
1040 log_error("Could not allocate message.");
1044 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1045 log_error("Failed to issue method call: %s", bus_error_message(&error));
1050 if (!dbus_message_iter_init(reply, &iter) ||
1051 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
1052 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
1053 log_error("Failed to parse reply.");
1058 dbus_message_iter_recurse(&iter, &sub);
1061 printf("%4s %-25s %-15s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
1063 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1064 const char *name, *type, *state, *job_path, *unit_path;
1068 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
1069 log_error("Failed to parse reply.");
1074 dbus_message_iter_recurse(&sub, &sub2);
1076 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &id, true) < 0 ||
1077 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0 ||
1078 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
1079 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, true) < 0 ||
1080 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, true) < 0 ||
1081 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, false) < 0) {
1082 log_error("Failed to parse reply.");
1087 e = arg_full ? NULL : ellipsize(name, 25, 33);
1088 printf("%4u %-25s %-15s %-7s\n", id, e ? e : name, type, state);
1093 dbus_message_iter_next(&sub);
1097 printf("\n%u jobs listed.\n", k);
1103 dbus_message_unref(m);
1106 dbus_message_unref(reply);
1108 dbus_error_free(&error);
1113 static int load_unit(DBusConnection *bus, char **args) {
1114 DBusMessage *m = NULL;
1119 dbus_error_init(&error);
1124 STRV_FOREACH(name, args+1) {
1127 if (!(m = dbus_message_new_method_call(
1128 "org.freedesktop.systemd1",
1129 "/org/freedesktop/systemd1",
1130 "org.freedesktop.systemd1.Manager",
1132 log_error("Could not allocate message.");
1137 if (!dbus_message_append_args(m,
1138 DBUS_TYPE_STRING, name,
1139 DBUS_TYPE_INVALID)) {
1140 log_error("Could not append arguments to message.");
1145 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1146 log_error("Failed to issue method call: %s", bus_error_message(&error));
1151 dbus_message_unref(m);
1152 dbus_message_unref(reply);
1161 dbus_message_unref(m);
1163 dbus_error_free(&error);
1168 static int cancel_job(DBusConnection *bus, char **args) {
1169 DBusMessage *m = NULL, *reply = NULL;
1174 dbus_error_init(&error);
1179 if (strv_length(args) <= 1)
1180 return daemon_reload(bus, args);
1182 STRV_FOREACH(name, args+1) {
1186 if (!(m = dbus_message_new_method_call(
1187 "org.freedesktop.systemd1",
1188 "/org/freedesktop/systemd1",
1189 "org.freedesktop.systemd1.Manager",
1191 log_error("Could not allocate message.");
1196 if ((r = safe_atou(*name, &id)) < 0) {
1197 log_error("Failed to parse job id: %s", strerror(-r));
1201 assert_cc(sizeof(uint32_t) == sizeof(id));
1202 if (!dbus_message_append_args(m,
1203 DBUS_TYPE_UINT32, &id,
1204 DBUS_TYPE_INVALID)) {
1205 log_error("Could not append arguments to message.");
1210 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1211 log_error("Failed to issue method call: %s", bus_error_message(&error));
1216 if (!dbus_message_get_args(reply, &error,
1217 DBUS_TYPE_OBJECT_PATH, &path,
1218 DBUS_TYPE_INVALID)) {
1219 log_error("Failed to parse reply: %s", bus_error_message(&error));
1224 dbus_message_unref(m);
1225 if (!(m = dbus_message_new_method_call(
1226 "org.freedesktop.systemd1",
1228 "org.freedesktop.systemd1.Job",
1230 log_error("Could not allocate message.");
1235 dbus_message_unref(reply);
1236 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1237 log_error("Failed to issue method call: %s", bus_error_message(&error));
1242 dbus_message_unref(m);
1243 dbus_message_unref(reply);
1251 dbus_message_unref(m);
1254 dbus_message_unref(reply);
1256 dbus_error_free(&error);
1261 static bool need_daemon_reload(DBusConnection *bus, const char *unit) {
1262 DBusMessage *m = NULL, *reply = NULL;
1263 dbus_bool_t b = FALSE;
1264 DBusMessageIter iter, sub;
1266 *interface = "org.freedesktop.systemd1.Unit",
1267 *property = "NeedDaemonReload",
1270 /* We ignore all errors here, since this is used to show a warning only */
1272 if (!(m = dbus_message_new_method_call(
1273 "org.freedesktop.systemd1",
1274 "/org/freedesktop/systemd1",
1275 "org.freedesktop.systemd1.Manager",
1279 if (!dbus_message_append_args(m,
1280 DBUS_TYPE_STRING, &unit,
1284 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL)))
1287 if (!dbus_message_get_args(reply, NULL,
1288 DBUS_TYPE_OBJECT_PATH, &path,
1292 dbus_message_unref(m);
1293 if (!(m = dbus_message_new_method_call(
1294 "org.freedesktop.systemd1",
1296 "org.freedesktop.DBus.Properties",
1300 if (!dbus_message_append_args(m,
1301 DBUS_TYPE_STRING, &interface,
1302 DBUS_TYPE_STRING, &property,
1303 DBUS_TYPE_INVALID)) {
1307 dbus_message_unref(reply);
1308 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL)))
1311 if (!dbus_message_iter_init(reply, &iter) ||
1312 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
1315 dbus_message_iter_recurse(&iter, &sub);
1317 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_BOOLEAN)
1320 dbus_message_iter_get_basic(&sub, &b);
1324 dbus_message_unref(m);
1327 dbus_message_unref(reply);
1332 typedef struct WaitData {
1337 static DBusHandlerResult wait_filter(DBusConnection *connection, DBusMessage *message, void *data) {
1345 dbus_error_init(&error);
1347 log_debug("Got D-Bus request: %s.%s() on %s",
1348 dbus_message_get_interface(message),
1349 dbus_message_get_member(message),
1350 dbus_message_get_path(message));
1352 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
1353 log_error("Warning! D-Bus connection terminated.");
1354 dbus_connection_close(connection);
1356 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
1358 const char *path, *result, *unit;
1359 dbus_bool_t success = true;
1361 if (dbus_message_get_args(message, &error,
1362 DBUS_TYPE_UINT32, &id,
1363 DBUS_TYPE_OBJECT_PATH, &path,
1364 DBUS_TYPE_STRING, &unit,
1365 DBUS_TYPE_STRING, &result,
1366 DBUS_TYPE_INVALID)) {
1369 p = set_remove(d->set, (char*) path);
1373 d->result = strdup(result);
1378 dbus_error_free(&error);
1379 if (dbus_message_get_args(message, &error,
1380 DBUS_TYPE_UINT32, &id,
1381 DBUS_TYPE_OBJECT_PATH, &path,
1382 DBUS_TYPE_STRING, &result,
1383 DBUS_TYPE_INVALID)) {
1386 /* Compatibility with older systemd versions <
1387 * 183 during upgrades. This should be dropped
1389 p = set_remove(d->set, (char*) path);
1393 d->result = strdup(result);
1398 dbus_error_free(&error);
1399 if (dbus_message_get_args(message, &error,
1400 DBUS_TYPE_UINT32, &id,
1401 DBUS_TYPE_OBJECT_PATH, &path,
1402 DBUS_TYPE_BOOLEAN, &success,
1403 DBUS_TYPE_INVALID)) {
1406 /* Compatibility with older systemd versions <
1407 * 19 during upgrades. This should be dropped
1410 p = set_remove(d->set, (char*) path);
1414 d->result = strdup("failed");
1420 log_error("Failed to parse message: %s", bus_error_message(&error));
1424 dbus_error_free(&error);
1425 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1428 static int enable_wait_for_jobs(DBusConnection *bus) {
1436 dbus_error_init(&error);
1437 dbus_bus_add_match(bus,
1439 "sender='org.freedesktop.systemd1',"
1440 "interface='org.freedesktop.systemd1.Manager',"
1441 "member='JobRemoved',"
1442 "path='/org/freedesktop/systemd1'",
1445 if (dbus_error_is_set(&error)) {
1446 log_error("Failed to add match: %s", bus_error_message(&error));
1447 dbus_error_free(&error);
1451 /* This is slightly dirty, since we don't undo the match registrations. */
1455 static int wait_for_jobs(DBusConnection *bus, Set *s) {
1465 if (!dbus_connection_add_filter(bus, wait_filter, &d, NULL)) {
1466 log_error("Failed to add filter.");
1471 while (!set_isempty(s) &&
1472 dbus_connection_read_write_dispatch(bus, -1))
1475 if (!arg_quiet && d.result) {
1476 if (streq(d.result, "timeout"))
1477 log_error("Job timed out.");
1478 else if (streq(d.result, "canceled"))
1479 log_error("Job canceled.");
1480 else if (streq(d.result, "dependency"))
1481 log_error("A dependency job failed. See system journal for details.");
1482 else if (!streq(d.result, "done") && !streq(d.result, "skipped"))
1483 log_error("Job failed. See system journal and 'systemctl status' for details.");
1486 if (streq_ptr(d.result, "timeout"))
1488 else if (streq_ptr(d.result, "canceled"))
1490 else if (!streq_ptr(d.result, "done") && !streq_ptr(d.result, "skipped"))
1498 /* This is slightly dirty, since we don't undo the filter registration. */
1503 static int start_unit_one(
1504 DBusConnection *bus,
1511 DBusMessage *m = NULL, *reply = NULL;
1520 assert(arg_no_block || s);
1522 if (!(m = dbus_message_new_method_call(
1523 "org.freedesktop.systemd1",
1524 "/org/freedesktop/systemd1",
1525 "org.freedesktop.systemd1.Manager",
1527 log_error("Could not allocate message.");
1532 if (!dbus_message_append_args(m,
1533 DBUS_TYPE_STRING, &name,
1534 DBUS_TYPE_STRING, &mode,
1535 DBUS_TYPE_INVALID)) {
1536 log_error("Could not append arguments to message.");
1541 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, error))) {
1543 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(error)) {
1544 /* There's always a fallback possible for
1545 * legacy actions. */
1550 log_error("Failed to issue method call: %s", bus_error_message(error));
1555 if (!dbus_message_get_args(reply, error,
1556 DBUS_TYPE_OBJECT_PATH, &path,
1557 DBUS_TYPE_INVALID)) {
1558 log_error("Failed to parse reply: %s", bus_error_message(error));
1563 if (need_daemon_reload(bus, name))
1564 log_warning("Warning: Unit file of created job changed on disk, 'systemctl %s daemon-reload' recommended.",
1565 arg_scope == UNIT_FILE_SYSTEM ? "--system" : "--user");
1567 if (!arg_no_block) {
1570 if (!(p = strdup(path))) {
1571 log_error("Failed to duplicate path.");
1576 if ((r = set_put(s, p)) < 0) {
1578 log_error("Failed to add path to set.");
1587 dbus_message_unref(m);
1590 dbus_message_unref(reply);
1595 static enum action verb_to_action(const char *verb) {
1596 if (streq(verb, "halt"))
1598 else if (streq(verb, "poweroff"))
1599 return ACTION_POWEROFF;
1600 else if (streq(verb, "reboot"))
1601 return ACTION_REBOOT;
1602 else if (streq(verb, "kexec"))
1603 return ACTION_KEXEC;
1604 else if (streq(verb, "rescue"))
1605 return ACTION_RESCUE;
1606 else if (streq(verb, "emergency"))
1607 return ACTION_EMERGENCY;
1608 else if (streq(verb, "default"))
1609 return ACTION_DEFAULT;
1610 else if (streq(verb, "exit"))
1612 else if (streq(verb, "suspend"))
1613 return ACTION_SUSPEND;
1614 else if (streq(verb, "hibernate"))
1615 return ACTION_HIBERNATE;
1617 return ACTION_INVALID;
1620 static int start_unit(DBusConnection *bus, char **args) {
1622 static const char * const table[_ACTION_MAX] = {
1623 [ACTION_HALT] = SPECIAL_HALT_TARGET,
1624 [ACTION_POWEROFF] = SPECIAL_POWEROFF_TARGET,
1625 [ACTION_REBOOT] = SPECIAL_REBOOT_TARGET,
1626 [ACTION_KEXEC] = SPECIAL_KEXEC_TARGET,
1627 [ACTION_RUNLEVEL2] = SPECIAL_RUNLEVEL2_TARGET,
1628 [ACTION_RUNLEVEL3] = SPECIAL_RUNLEVEL3_TARGET,
1629 [ACTION_RUNLEVEL4] = SPECIAL_RUNLEVEL4_TARGET,
1630 [ACTION_RUNLEVEL5] = SPECIAL_RUNLEVEL5_TARGET,
1631 [ACTION_RESCUE] = SPECIAL_RESCUE_TARGET,
1632 [ACTION_EMERGENCY] = SPECIAL_EMERGENCY_TARGET,
1633 [ACTION_DEFAULT] = SPECIAL_DEFAULT_TARGET,
1634 [ACTION_EXIT] = SPECIAL_EXIT_TARGET,
1635 [ACTION_SUSPEND] = SPECIAL_SUSPEND_TARGET,
1636 [ACTION_HIBERNATE] = SPECIAL_HIBERNATE_TARGET
1640 const char *method, *mode, *one_name;
1645 dbus_error_init(&error);
1649 ask_password_agent_open_if_enabled();
1651 if (arg_action == ACTION_SYSTEMCTL) {
1653 streq(args[0], "stop") ||
1654 streq(args[0], "condstop") ? "StopUnit" :
1655 streq(args[0], "reload") ? "ReloadUnit" :
1656 streq(args[0], "restart") ? "RestartUnit" :
1658 streq(args[0], "try-restart") ||
1659 streq(args[0], "condrestart") ? "TryRestartUnit" :
1661 streq(args[0], "reload-or-restart") ? "ReloadOrRestartUnit" :
1663 streq(args[0], "reload-or-try-restart") ||
1664 streq(args[0], "condreload") ||
1666 streq(args[0], "force-reload") ? "ReloadOrTryRestartUnit" :
1670 (streq(args[0], "isolate") ||
1671 streq(args[0], "rescue") ||
1672 streq(args[0], "emergency")) ? "isolate" : arg_job_mode;
1674 one_name = table[verb_to_action(args[0])];
1677 assert(arg_action < ELEMENTSOF(table));
1678 assert(table[arg_action]);
1680 method = "StartUnit";
1682 mode = (arg_action == ACTION_EMERGENCY ||
1683 arg_action == ACTION_RESCUE ||
1684 arg_action == ACTION_RUNLEVEL2 ||
1685 arg_action == ACTION_RUNLEVEL3 ||
1686 arg_action == ACTION_RUNLEVEL4 ||
1687 arg_action == ACTION_RUNLEVEL5) ? "isolate" : "replace";
1689 one_name = table[arg_action];
1692 if (!arg_no_block) {
1693 if ((ret = enable_wait_for_jobs(bus)) < 0) {
1694 log_error("Could not watch jobs: %s", strerror(-ret));
1698 if (!(s = set_new(string_hash_func, string_compare_func))) {
1699 log_error("Failed to allocate set.");
1706 if ((ret = start_unit_one(bus, method, one_name, mode, &error, s)) <= 0)
1709 STRV_FOREACH(name, args+1)
1710 if ((r = start_unit_one(bus, method, *name, mode, &error, s)) != 0) {
1711 ret = translate_bus_error_to_exit_status(r, &error);
1712 dbus_error_free(&error);
1717 if ((r = wait_for_jobs(bus, s)) < 0) {
1726 dbus_error_free(&error);
1731 /* Ask systemd-logind, which might grant access to unprivileged users
1732 * through PolicyKit */
1733 static int reboot_with_logind(DBusConnection *bus, enum action a) {
1736 DBusMessage *m = NULL, *reply = NULL;
1738 dbus_bool_t interactive = true;
1741 dbus_error_init(&error);
1743 polkit_agent_open_if_enabled();
1751 case ACTION_POWEROFF:
1752 method = "PowerOff";
1755 case ACTION_SUSPEND:
1759 case ACTION_HIBERNATE:
1760 method = "Hibernate";
1767 m = dbus_message_new_method_call(
1768 "org.freedesktop.login1",
1769 "/org/freedesktop/login1",
1770 "org.freedesktop.login1.Manager",
1773 log_error("Could not allocate message.");
1778 if (!dbus_message_append_args(m,
1779 DBUS_TYPE_BOOLEAN, &interactive,
1780 DBUS_TYPE_INVALID)) {
1781 log_error("Could not append arguments to message.");
1786 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
1788 if (error_is_no_service(&error)) {
1789 log_debug("Failed to issue method call: %s", bus_error_message(&error));
1794 if (dbus_error_has_name(&error, DBUS_ERROR_ACCESS_DENIED)) {
1795 log_debug("Failed to issue method call: %s", bus_error_message(&error));
1800 log_info("Failed to issue method call: %s", bus_error_message(&error));
1809 dbus_message_unref(m);
1812 dbus_message_unref(reply);
1814 dbus_error_free(&error);
1822 static int start_special(DBusConnection *bus, char **args) {
1828 a = verb_to_action(args[0]);
1830 if (arg_force >= 2 && geteuid() != 0) {
1831 log_error("Must be root.");
1835 if (arg_force >= 2 &&
1836 (a == ACTION_HALT ||
1837 a == ACTION_POWEROFF ||
1838 a == ACTION_REBOOT))
1841 if (arg_force >= 1 &&
1842 (a == ACTION_HALT ||
1843 a == ACTION_POWEROFF ||
1844 a == ACTION_REBOOT ||
1845 a == ACTION_KEXEC ||
1847 return daemon_reload(bus, args);
1849 /* first try logind, to allow authentication with polkit */
1850 if (geteuid() != 0 &&
1851 (a == ACTION_POWEROFF ||
1852 a == ACTION_REBOOT ||
1853 a == ACTION_SUSPEND ||
1854 a == ACTION_HIBERNATE)) {
1855 r = reboot_with_logind(bus, a);
1860 r = start_unit(bus, args);
1867 static int check_unit(DBusConnection *bus, char **args) {
1868 DBusMessage *m = NULL, *reply = NULL;
1870 *interface = "org.freedesktop.systemd1.Unit",
1871 *property = "ActiveState";
1872 int r = 3; /* According to LSB: "program is not running" */
1879 dbus_error_init(&error);
1881 STRV_FOREACH(name, args+1) {
1882 const char *path = NULL;
1884 DBusMessageIter iter, sub;
1886 if (!(m = dbus_message_new_method_call(
1887 "org.freedesktop.systemd1",
1888 "/org/freedesktop/systemd1",
1889 "org.freedesktop.systemd1.Manager",
1891 log_error("Could not allocate message.");
1896 if (!dbus_message_append_args(m,
1897 DBUS_TYPE_STRING, name,
1898 DBUS_TYPE_INVALID)) {
1899 log_error("Could not append arguments to message.");
1904 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1906 /* Hmm, cannot figure out anything about this unit... */
1910 dbus_error_free(&error);
1911 dbus_message_unref(m);
1916 if (!dbus_message_get_args(reply, &error,
1917 DBUS_TYPE_OBJECT_PATH, &path,
1918 DBUS_TYPE_INVALID)) {
1919 log_error("Failed to parse reply: %s", bus_error_message(&error));
1924 dbus_message_unref(m);
1925 if (!(m = dbus_message_new_method_call(
1926 "org.freedesktop.systemd1",
1928 "org.freedesktop.DBus.Properties",
1930 log_error("Could not allocate message.");
1935 if (!dbus_message_append_args(m,
1936 DBUS_TYPE_STRING, &interface,
1937 DBUS_TYPE_STRING, &property,
1938 DBUS_TYPE_INVALID)) {
1939 log_error("Could not append arguments to message.");
1944 dbus_message_unref(reply);
1945 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1946 log_error("Failed to issue method call: %s", bus_error_message(&error));
1951 if (!dbus_message_iter_init(reply, &iter) ||
1952 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1953 log_error("Failed to parse reply.");
1958 dbus_message_iter_recurse(&iter, &sub);
1960 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1961 log_error("Failed to parse reply.");
1966 dbus_message_iter_get_basic(&sub, &state);
1971 if (streq(state, "active") || streq(state, "reloading"))
1974 dbus_message_unref(m);
1975 dbus_message_unref(reply);
1981 dbus_message_unref(m);
1984 dbus_message_unref(reply);
1986 dbus_error_free(&error);
1991 static int kill_unit(DBusConnection *bus, char **args) {
1992 DBusMessage *m = NULL;
2000 dbus_error_init(&error);
2003 arg_kill_who = "all";
2006 arg_kill_mode = streq(arg_kill_who, "all") ? "control-group" : "process";
2008 STRV_FOREACH(name, args+1) {
2011 if (!(m = dbus_message_new_method_call(
2012 "org.freedesktop.systemd1",
2013 "/org/freedesktop/systemd1",
2014 "org.freedesktop.systemd1.Manager",
2016 log_error("Could not allocate message.");
2021 if (!dbus_message_append_args(m,
2022 DBUS_TYPE_STRING, name,
2023 DBUS_TYPE_STRING, &arg_kill_who,
2024 DBUS_TYPE_STRING, &arg_kill_mode,
2025 DBUS_TYPE_INT32, &arg_signal,
2026 DBUS_TYPE_INVALID)) {
2027 log_error("Could not append arguments to message.");
2032 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2033 log_error("Failed to issue method call: %s", bus_error_message(&error));
2034 dbus_error_free(&error);
2038 dbus_message_unref(m);
2041 dbus_message_unref(reply);
2047 dbus_message_unref(m);
2049 dbus_error_free(&error);
2054 typedef struct ExecStatusInfo {
2062 usec_t start_timestamp;
2063 usec_t exit_timestamp;
2068 LIST_FIELDS(struct ExecStatusInfo, exec);
2071 static void exec_status_info_free(ExecStatusInfo *i) {
2080 static int exec_status_info_deserialize(DBusMessageIter *sub, ExecStatusInfo *i) {
2081 uint64_t start_timestamp, exit_timestamp, start_timestamp_monotonic, exit_timestamp_monotonic;
2082 DBusMessageIter sub2, sub3;
2086 int32_t code, status;
2092 if (dbus_message_iter_get_arg_type(sub) != DBUS_TYPE_STRUCT)
2095 dbus_message_iter_recurse(sub, &sub2);
2097 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0)
2100 if (!(i->path = strdup(path)))
2103 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_ARRAY ||
2104 dbus_message_iter_get_element_type(&sub2) != DBUS_TYPE_STRING)
2108 dbus_message_iter_recurse(&sub2, &sub3);
2109 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
2110 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
2111 dbus_message_iter_next(&sub3);
2116 if (!(i->argv = new0(char*, n+1)))
2120 dbus_message_iter_recurse(&sub2, &sub3);
2121 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
2124 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
2125 dbus_message_iter_get_basic(&sub3, &s);
2126 dbus_message_iter_next(&sub3);
2128 if (!(i->argv[n++] = strdup(s)))
2132 if (!dbus_message_iter_next(&sub2) ||
2133 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_BOOLEAN, &ignore, true) < 0 ||
2134 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_timestamp, true) < 0 ||
2135 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_timestamp_monotonic, true) < 0 ||
2136 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_timestamp, true) < 0 ||
2137 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_timestamp_monotonic, true) < 0 ||
2138 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, true) < 0 ||
2139 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &code, true) < 0 ||
2140 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &status, false) < 0)
2144 i->start_timestamp = (usec_t) start_timestamp;
2145 i->exit_timestamp = (usec_t) exit_timestamp;
2146 i->pid = (pid_t) pid;
2153 typedef struct UnitStatusInfo {
2155 const char *load_state;
2156 const char *active_state;
2157 const char *sub_state;
2158 const char *unit_file_state;
2160 const char *description;
2161 const char *following;
2163 char **documentation;
2165 const char *fragment_path;
2166 const char *source_path;
2167 const char *default_control_group;
2169 const char *load_error;
2172 usec_t inactive_exit_timestamp;
2173 usec_t inactive_exit_timestamp_monotonic;
2174 usec_t active_enter_timestamp;
2175 usec_t active_exit_timestamp;
2176 usec_t inactive_enter_timestamp;
2178 bool need_daemon_reload;
2183 const char *status_text;
2186 usec_t start_timestamp;
2187 usec_t exit_timestamp;
2189 int exit_code, exit_status;
2191 usec_t condition_timestamp;
2192 bool condition_result;
2195 unsigned n_accepted;
2196 unsigned n_connections;
2200 const char *sysfs_path;
2202 /* Mount, Automount */
2208 LIST_HEAD(ExecStatusInfo, exec);
2211 static void print_status_info(UnitStatusInfo *i) {
2213 const char *on, *off, *ss;
2215 char since1[FORMAT_TIMESTAMP_PRETTY_MAX], *s1;
2216 char since2[FORMAT_TIMESTAMP_MAX], *s2;
2221 /* This shows pretty information about a unit. See
2222 * print_property() for a low-level property printer */
2224 printf("%s", strna(i->id));
2226 if (i->description && !streq_ptr(i->id, i->description))
2227 printf(" - %s", i->description);
2232 printf("\t Follow: unit currently follows state of %s\n", i->following);
2234 if (streq_ptr(i->load_state, "error")) {
2235 on = ansi_highlight_red(true);
2236 off = ansi_highlight_red(false);
2240 path = i->source_path ? i->source_path : i->fragment_path;
2243 printf("\t Loaded: %s%s%s (Reason: %s)\n", on, strna(i->load_state), off, i->load_error);
2244 else if (path && i->unit_file_state)
2245 printf("\t Loaded: %s%s%s (%s; %s)\n", on, strna(i->load_state), off, path, i->unit_file_state);
2247 printf("\t Loaded: %s%s%s (%s)\n", on, strna(i->load_state), off, path);
2249 printf("\t Loaded: %s%s%s\n", on, strna(i->load_state), off);
2251 ss = streq_ptr(i->active_state, i->sub_state) ? NULL : i->sub_state;
2253 if (streq_ptr(i->active_state, "failed")) {
2254 on = ansi_highlight_red(true);
2255 off = ansi_highlight_red(false);
2256 } else if (streq_ptr(i->active_state, "active") || streq_ptr(i->active_state, "reloading")) {
2257 on = ansi_highlight_green(true);
2258 off = ansi_highlight_green(false);
2263 printf("\t Active: %s%s (%s)%s",
2265 strna(i->active_state),
2269 printf("\t Active: %s%s%s",
2271 strna(i->active_state),
2274 if (!isempty(i->result) && !streq(i->result, "success"))
2275 printf(" (Result: %s)", i->result);
2277 timestamp = (streq_ptr(i->active_state, "active") ||
2278 streq_ptr(i->active_state, "reloading")) ? i->active_enter_timestamp :
2279 (streq_ptr(i->active_state, "inactive") ||
2280 streq_ptr(i->active_state, "failed")) ? i->inactive_enter_timestamp :
2281 streq_ptr(i->active_state, "activating") ? i->inactive_exit_timestamp :
2282 i->active_exit_timestamp;
2284 s1 = format_timestamp_pretty(since1, sizeof(since1), timestamp);
2285 s2 = format_timestamp(since2, sizeof(since2), timestamp);
2288 printf(" since %s; %s\n", s2, s1);
2290 printf(" since %s\n", s2);
2294 if (!i->condition_result && i->condition_timestamp > 0) {
2295 s1 = format_timestamp_pretty(since1, sizeof(since1), i->condition_timestamp);
2296 s2 = format_timestamp(since2, sizeof(since2), i->condition_timestamp);
2299 printf("\t start condition failed at %s; %s\n", s2, s1);
2301 printf("\t start condition failed at %s\n", s2);
2305 printf("\t Device: %s\n", i->sysfs_path);
2307 printf("\t Where: %s\n", i->where);
2309 printf("\t What: %s\n", i->what);
2311 if (!strv_isempty(i->documentation)) {
2315 STRV_FOREACH(t, i->documentation) {
2317 printf("\t Docs: %s\n", *t);
2320 printf("\t %s\n", *t);
2325 printf("\tAccepted: %u; Connected: %u\n", i->n_accepted, i->n_connections);
2327 LIST_FOREACH(exec, p, i->exec) {
2331 /* Only show exited processes here */
2335 t = strv_join(p->argv, " ");
2336 printf("\t Process: %u %s=%s ", p->pid, p->name, strna(t));
2339 good = is_clean_exit_lsb(p->code, p->status);
2341 on = ansi_highlight_red(true);
2342 off = ansi_highlight_red(false);
2346 printf("%s(code=%s, ", on, sigchld_code_to_string(p->code));
2348 if (p->code == CLD_EXITED) {
2351 printf("status=%i", p->status);
2353 c = exit_status_to_string(p->status, EXIT_STATUS_SYSTEMD);
2358 printf("signal=%s", signal_to_string(p->status));
2360 printf(")%s\n", off);
2362 if (i->main_pid == p->pid &&
2363 i->start_timestamp == p->start_timestamp &&
2364 i->exit_timestamp == p->start_timestamp)
2365 /* Let's not show this twice */
2368 if (p->pid == i->control_pid)
2372 if (i->main_pid > 0 || i->control_pid > 0) {
2375 if (i->main_pid > 0) {
2376 printf("Main PID: %u", (unsigned) i->main_pid);
2380 get_process_comm(i->main_pid, &t);
2385 } else if (i->exit_code > 0) {
2386 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
2388 if (i->exit_code == CLD_EXITED) {
2391 printf("status=%i", i->exit_status);
2393 c = exit_status_to_string(i->exit_status, EXIT_STATUS_SYSTEMD);
2398 printf("signal=%s", signal_to_string(i->exit_status));
2403 if (i->main_pid > 0 && i->control_pid > 0)
2406 if (i->control_pid > 0) {
2409 printf(" Control: %u", (unsigned) i->control_pid);
2411 get_process_comm(i->control_pid, &t);
2422 printf("\t Status: \"%s\"\n", i->status_text);
2424 if (i->default_control_group) {
2427 printf("\t CGroup: %s\n", i->default_control_group);
2429 if (arg_transport != TRANSPORT_SSH) {
2439 if (i->main_pid > 0)
2440 extra[k++] = i->main_pid;
2442 if (i->control_pid > 0)
2443 extra[k++] = i->control_pid;
2445 show_cgroup_and_extra_by_spec(i->default_control_group, "\t\t ", c, false, arg_all, extra, k);
2449 if (i->id && arg_transport != TRANSPORT_SSH) {
2451 show_journal_by_unit(i->id, arg_output, 0, i->inactive_exit_timestamp_monotonic, arg_lines, arg_all, arg_follow);
2454 if (i->need_daemon_reload)
2455 printf("\n%sWarning:%s Unit file changed on disk, 'systemctl %s daemon-reload' recommended.\n",
2456 ansi_highlight_red(true),
2457 ansi_highlight_red(false),
2458 arg_scope == UNIT_FILE_SYSTEM ? "--system" : "--user");
2461 static int status_property(const char *name, DBusMessageIter *iter, UnitStatusInfo *i) {
2467 switch (dbus_message_iter_get_arg_type(iter)) {
2469 case DBUS_TYPE_STRING: {
2472 dbus_message_iter_get_basic(iter, &s);
2475 if (streq(name, "Id"))
2477 else if (streq(name, "LoadState"))
2479 else if (streq(name, "ActiveState"))
2480 i->active_state = s;
2481 else if (streq(name, "SubState"))
2483 else if (streq(name, "Description"))
2485 else if (streq(name, "FragmentPath"))
2486 i->fragment_path = s;
2487 else if (streq(name, "SourcePath"))
2489 else if (streq(name, "DefaultControlGroup"))
2490 i->default_control_group = s;
2491 else if (streq(name, "StatusText"))
2493 else if (streq(name, "SysFSPath"))
2495 else if (streq(name, "Where"))
2497 else if (streq(name, "What"))
2499 else if (streq(name, "Following"))
2501 else if (streq(name, "UnitFileState"))
2502 i->unit_file_state = s;
2503 else if (streq(name, "Result"))
2510 case DBUS_TYPE_BOOLEAN: {
2513 dbus_message_iter_get_basic(iter, &b);
2515 if (streq(name, "Accept"))
2517 else if (streq(name, "NeedDaemonReload"))
2518 i->need_daemon_reload = b;
2519 else if (streq(name, "ConditionResult"))
2520 i->condition_result = b;
2525 case DBUS_TYPE_UINT32: {
2528 dbus_message_iter_get_basic(iter, &u);
2530 if (streq(name, "MainPID")) {
2532 i->main_pid = (pid_t) u;
2535 } else if (streq(name, "ControlPID"))
2536 i->control_pid = (pid_t) u;
2537 else if (streq(name, "ExecMainPID")) {
2539 i->main_pid = (pid_t) u;
2540 } else if (streq(name, "NAccepted"))
2542 else if (streq(name, "NConnections"))
2543 i->n_connections = u;
2548 case DBUS_TYPE_INT32: {
2551 dbus_message_iter_get_basic(iter, &j);
2553 if (streq(name, "ExecMainCode"))
2554 i->exit_code = (int) j;
2555 else if (streq(name, "ExecMainStatus"))
2556 i->exit_status = (int) j;
2561 case DBUS_TYPE_UINT64: {
2564 dbus_message_iter_get_basic(iter, &u);
2566 if (streq(name, "ExecMainStartTimestamp"))
2567 i->start_timestamp = (usec_t) u;
2568 else if (streq(name, "ExecMainExitTimestamp"))
2569 i->exit_timestamp = (usec_t) u;
2570 else if (streq(name, "ActiveEnterTimestamp"))
2571 i->active_enter_timestamp = (usec_t) u;
2572 else if (streq(name, "InactiveEnterTimestamp"))
2573 i->inactive_enter_timestamp = (usec_t) u;
2574 else if (streq(name, "InactiveExitTimestamp"))
2575 i->inactive_exit_timestamp = (usec_t) u;
2576 else if (streq(name, "InactiveExitTimestampMonotonic"))
2577 i->inactive_exit_timestamp_monotonic = (usec_t) u;
2578 else if (streq(name, "ActiveExitTimestamp"))
2579 i->active_exit_timestamp = (usec_t) u;
2580 else if (streq(name, "ConditionTimestamp"))
2581 i->condition_timestamp = (usec_t) u;
2586 case DBUS_TYPE_ARRAY: {
2588 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT &&
2589 startswith(name, "Exec")) {
2590 DBusMessageIter sub;
2592 dbus_message_iter_recurse(iter, &sub);
2593 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2594 ExecStatusInfo *info;
2597 if (!(info = new0(ExecStatusInfo, 1)))
2600 if (!(info->name = strdup(name))) {
2605 if ((r = exec_status_info_deserialize(&sub, info)) < 0) {
2610 LIST_PREPEND(ExecStatusInfo, exec, i->exec, info);
2612 dbus_message_iter_next(&sub);
2614 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING &&
2615 streq(name, "Documentation")) {
2617 DBusMessageIter sub;
2619 dbus_message_iter_recurse(iter, &sub);
2620 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING) {
2624 dbus_message_iter_get_basic(&sub, &s);
2626 l = strv_append(i->documentation, s);
2630 strv_free(i->documentation);
2631 i->documentation = l;
2633 dbus_message_iter_next(&sub);
2640 case DBUS_TYPE_STRUCT: {
2642 if (streq(name, "LoadError")) {
2643 DBusMessageIter sub;
2644 const char *n, *message;
2647 dbus_message_iter_recurse(iter, &sub);
2649 r = bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &n, true);
2653 r = bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &message, false);
2657 if (!isempty(message))
2658 i->load_error = message;
2668 static int print_property(const char *name, DBusMessageIter *iter) {
2672 /* This is a low-level property printer, see
2673 * print_status_info() for the nicer output */
2675 if (arg_property && !strv_find(arg_property, name))
2678 switch (dbus_message_iter_get_arg_type(iter)) {
2680 case DBUS_TYPE_STRUCT: {
2681 DBusMessageIter sub;
2682 dbus_message_iter_recurse(iter, &sub);
2684 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "Job")) {
2687 dbus_message_iter_get_basic(&sub, &u);
2690 printf("%s=%u\n", name, (unsigned) u);
2692 printf("%s=\n", name);
2695 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Unit")) {
2698 dbus_message_iter_get_basic(&sub, &s);
2700 if (arg_all || s[0])
2701 printf("%s=%s\n", name, s);
2704 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "LoadError")) {
2705 const char *a = NULL, *b = NULL;
2707 if (bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &a, true) >= 0)
2708 bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &b, false);
2710 if (arg_all || !isempty(a) || !isempty(b))
2711 printf("%s=%s \"%s\"\n", name, strempty(a), strempty(b));
2719 case DBUS_TYPE_ARRAY:
2721 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "EnvironmentFiles")) {
2722 DBusMessageIter sub, sub2;
2724 dbus_message_iter_recurse(iter, &sub);
2725 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2729 dbus_message_iter_recurse(&sub, &sub2);
2731 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) >= 0 &&
2732 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_BOOLEAN, &ignore, false) >= 0)
2733 printf("EnvironmentFile=%s (ignore_errors=%s)\n", path, yes_no(ignore));
2735 dbus_message_iter_next(&sub);
2740 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Paths")) {
2741 DBusMessageIter sub, sub2;
2743 dbus_message_iter_recurse(iter, &sub);
2744 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2745 const char *type, *path;
2747 dbus_message_iter_recurse(&sub, &sub2);
2749 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
2750 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0)
2751 printf("%s=%s\n", type, path);
2753 dbus_message_iter_next(&sub);
2758 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Timers")) {
2759 DBusMessageIter sub, sub2;
2761 dbus_message_iter_recurse(iter, &sub);
2762 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2764 uint64_t value, next_elapse;
2766 dbus_message_iter_recurse(&sub, &sub2);
2768 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &base, true) >= 0 &&
2769 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &value, true) >= 0 &&
2770 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &next_elapse, false) >= 0) {
2771 char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX];
2773 printf("%s={ value=%s ; next_elapse=%s }\n",
2775 format_timespan(timespan1, sizeof(timespan1), value),
2776 format_timespan(timespan2, sizeof(timespan2), next_elapse));
2779 dbus_message_iter_next(&sub);
2784 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "ControlGroupAttributes")) {
2785 DBusMessageIter sub, sub2;
2787 dbus_message_iter_recurse(iter, &sub);
2788 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2789 const char *controller, *attr, *value;
2791 dbus_message_iter_recurse(&sub, &sub2);
2793 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &controller, true) >= 0 &&
2794 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &attr, true) >= 0 &&
2795 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &value, false) >= 0) {
2797 printf("ControlGroupAttribute={ controller=%s ; attribute=%s ; value=\"%s\" }\n",
2803 dbus_message_iter_next(&sub);
2808 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && startswith(name, "Exec")) {
2809 DBusMessageIter sub;
2811 dbus_message_iter_recurse(iter, &sub);
2812 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2813 ExecStatusInfo info;
2816 if (exec_status_info_deserialize(&sub, &info) >= 0) {
2817 char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
2820 t = strv_join(info.argv, " ");
2822 printf("%s={ path=%s ; argv[]=%s ; ignore_errors=%s ; start_time=[%s] ; stop_time=[%s] ; pid=%u ; code=%s ; status=%i%s%s }\n",
2826 yes_no(info.ignore),
2827 strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)),
2828 strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)),
2829 (unsigned) info. pid,
2830 sigchld_code_to_string(info.code),
2832 info.code == CLD_EXITED ? "" : "/",
2833 strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status)));
2839 strv_free(info.argv);
2841 dbus_message_iter_next(&sub);
2850 if (generic_print_property(name, iter, arg_all) > 0)
2854 printf("%s=[unprintable]\n", name);
2859 static int show_one(const char *verb, DBusConnection *bus, const char *path, bool show_properties, bool *new_line) {
2860 DBusMessage *m = NULL, *reply = NULL;
2861 const char *interface = "";
2864 DBusMessageIter iter, sub, sub2, sub3;
2865 UnitStatusInfo info;
2873 dbus_error_init(&error);
2875 if (!(m = dbus_message_new_method_call(
2876 "org.freedesktop.systemd1",
2878 "org.freedesktop.DBus.Properties",
2880 log_error("Could not allocate message.");
2885 if (!dbus_message_append_args(m,
2886 DBUS_TYPE_STRING, &interface,
2887 DBUS_TYPE_INVALID)) {
2888 log_error("Could not append arguments to message.");
2893 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2894 log_error("Failed to issue method call: %s", bus_error_message(&error));
2899 if (!dbus_message_iter_init(reply, &iter) ||
2900 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
2901 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
2902 log_error("Failed to parse reply.");
2907 dbus_message_iter_recurse(&iter, &sub);
2914 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2917 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
2918 log_error("Failed to parse reply.");
2923 dbus_message_iter_recurse(&sub, &sub2);
2925 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
2926 log_error("Failed to parse reply.");
2931 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
2932 log_error("Failed to parse reply.");
2937 dbus_message_iter_recurse(&sub2, &sub3);
2939 if (show_properties)
2940 r = print_property(name, &sub3);
2942 r = status_property(name, &sub3, &info);
2945 log_error("Failed to parse reply.");
2950 dbus_message_iter_next(&sub);
2955 if (!show_properties)
2956 print_status_info(&info);
2958 strv_free(info.documentation);
2960 if (!streq_ptr(info.active_state, "active") &&
2961 !streq_ptr(info.active_state, "reloading") &&
2962 streq(verb, "status"))
2963 /* According to LSB: "program not running" */
2966 while ((p = info.exec)) {
2967 LIST_REMOVE(ExecStatusInfo, exec, info.exec, p);
2968 exec_status_info_free(p);
2973 dbus_message_unref(m);
2976 dbus_message_unref(reply);
2978 dbus_error_free(&error);
2983 static int show_one_by_pid(const char *verb, DBusConnection *bus, uint32_t pid, bool *new_line) {
2984 DBusMessage *m = NULL, *reply = NULL;
2985 const char *path = NULL;
2989 dbus_error_init(&error);
2991 m = dbus_message_new_method_call(
2992 "org.freedesktop.systemd1",
2993 "/org/freedesktop/systemd1",
2994 "org.freedesktop.systemd1.Manager",
2997 log_error("Could not allocate message.");
3002 if (!dbus_message_append_args(m,
3003 DBUS_TYPE_UINT32, &pid,
3004 DBUS_TYPE_INVALID)) {
3005 log_error("Could not append arguments to message.");
3010 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
3012 log_error("Failed to issue method call: %s", bus_error_message(&error));
3017 if (!dbus_message_get_args(reply, &error,
3018 DBUS_TYPE_OBJECT_PATH, &path,
3019 DBUS_TYPE_INVALID)) {
3020 log_error("Failed to parse reply: %s", bus_error_message(&error));
3025 r = show_one(verb, bus, path, false, new_line);
3029 dbus_message_unref(m);
3032 dbus_message_unref(reply);
3034 dbus_error_free(&error);
3039 static int show(DBusConnection *bus, char **args) {
3041 bool show_properties, new_line = false;
3047 show_properties = !streq(args[0], "status");
3049 if (show_properties)
3050 pager_open_if_enabled();
3052 if (show_properties && strv_length(args) <= 1) {
3053 /* If not argument is specified inspect the manager
3056 return show_one(args[0], bus, "/org/freedesktop/systemd1", show_properties, &new_line);
3059 STRV_FOREACH(name, args+1) {
3062 if (safe_atou32(*name, &id) < 0) {
3064 /* Interpret as unit name */
3067 e = bus_path_escape(*name);
3070 p = strappend("/org/freedesktop/systemd1/unit/", e);
3075 r = show_one(args[0], bus, p, show_properties, &new_line);
3081 } else if (show_properties) {
3083 /* Interpret as job id */
3086 if (asprintf(&p, "/org/freedesktop/systemd1/job/%u", id) < 0)
3089 r = show_one(args[0], bus, p, show_properties, &new_line);
3097 /* Interpret as PID */
3099 r = show_one_by_pid(args[0], bus, id, &new_line);
3108 static int dump(DBusConnection *bus, char **args) {
3109 DBusMessage *m = NULL, *reply = NULL;
3114 dbus_error_init(&error);
3116 pager_open_if_enabled();
3118 if (!(m = dbus_message_new_method_call(
3119 "org.freedesktop.systemd1",
3120 "/org/freedesktop/systemd1",
3121 "org.freedesktop.systemd1.Manager",
3123 log_error("Could not allocate message.");
3127 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3128 log_error("Failed to issue method call: %s", bus_error_message(&error));
3133 if (!dbus_message_get_args(reply, &error,
3134 DBUS_TYPE_STRING, &text,
3135 DBUS_TYPE_INVALID)) {
3136 log_error("Failed to parse reply: %s", bus_error_message(&error));
3141 fputs(text, stdout);
3147 dbus_message_unref(m);
3150 dbus_message_unref(reply);
3152 dbus_error_free(&error);
3157 static int snapshot(DBusConnection *bus, char **args) {
3158 DBusMessage *m = NULL, *reply = NULL;
3161 const char *name = "", *path, *id;
3162 dbus_bool_t cleanup = FALSE;
3163 DBusMessageIter iter, sub;
3165 *interface = "org.freedesktop.systemd1.Unit",
3168 dbus_error_init(&error);
3170 if (!(m = dbus_message_new_method_call(
3171 "org.freedesktop.systemd1",
3172 "/org/freedesktop/systemd1",
3173 "org.freedesktop.systemd1.Manager",
3174 "CreateSnapshot"))) {
3175 log_error("Could not allocate message.");
3179 if (strv_length(args) > 1)
3182 if (!dbus_message_append_args(m,
3183 DBUS_TYPE_STRING, &name,
3184 DBUS_TYPE_BOOLEAN, &cleanup,
3185 DBUS_TYPE_INVALID)) {
3186 log_error("Could not append arguments to message.");
3191 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3192 log_error("Failed to issue method call: %s", bus_error_message(&error));
3197 if (!dbus_message_get_args(reply, &error,
3198 DBUS_TYPE_OBJECT_PATH, &path,
3199 DBUS_TYPE_INVALID)) {
3200 log_error("Failed to parse reply: %s", bus_error_message(&error));
3205 dbus_message_unref(m);
3206 if (!(m = dbus_message_new_method_call(
3207 "org.freedesktop.systemd1",
3209 "org.freedesktop.DBus.Properties",
3211 log_error("Could not allocate message.");
3215 if (!dbus_message_append_args(m,
3216 DBUS_TYPE_STRING, &interface,
3217 DBUS_TYPE_STRING, &property,
3218 DBUS_TYPE_INVALID)) {
3219 log_error("Could not append arguments to message.");
3224 dbus_message_unref(reply);
3225 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3226 log_error("Failed to issue method call: %s", bus_error_message(&error));
3231 if (!dbus_message_iter_init(reply, &iter) ||
3232 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
3233 log_error("Failed to parse reply.");
3238 dbus_message_iter_recurse(&iter, &sub);
3240 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
3241 log_error("Failed to parse reply.");
3246 dbus_message_iter_get_basic(&sub, &id);
3254 dbus_message_unref(m);
3257 dbus_message_unref(reply);
3259 dbus_error_free(&error);
3264 static int delete_snapshot(DBusConnection *bus, char **args) {
3265 DBusMessage *m = NULL, *reply = NULL;
3273 dbus_error_init(&error);
3275 STRV_FOREACH(name, args+1) {
3276 const char *path = NULL;
3278 if (!(m = dbus_message_new_method_call(
3279 "org.freedesktop.systemd1",
3280 "/org/freedesktop/systemd1",
3281 "org.freedesktop.systemd1.Manager",
3283 log_error("Could not allocate message.");
3288 if (!dbus_message_append_args(m,
3289 DBUS_TYPE_STRING, name,
3290 DBUS_TYPE_INVALID)) {
3291 log_error("Could not append arguments to message.");
3296 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3297 log_error("Failed to issue method call: %s", bus_error_message(&error));
3302 if (!dbus_message_get_args(reply, &error,
3303 DBUS_TYPE_OBJECT_PATH, &path,
3304 DBUS_TYPE_INVALID)) {
3305 log_error("Failed to parse reply: %s", bus_error_message(&error));
3310 dbus_message_unref(m);
3311 if (!(m = dbus_message_new_method_call(
3312 "org.freedesktop.systemd1",
3314 "org.freedesktop.systemd1.Snapshot",
3316 log_error("Could not allocate message.");
3321 dbus_message_unref(reply);
3322 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3323 log_error("Failed to issue method call: %s", bus_error_message(&error));
3328 dbus_message_unref(m);
3329 dbus_message_unref(reply);
3337 dbus_message_unref(m);
3340 dbus_message_unref(reply);
3342 dbus_error_free(&error);
3347 static int daemon_reload(DBusConnection *bus, char **args) {
3348 DBusMessage *m = NULL, *reply = NULL;
3353 dbus_error_init(&error);
3355 if (arg_action == ACTION_RELOAD)
3357 else if (arg_action == ACTION_REEXEC)
3358 method = "Reexecute";
3360 assert(arg_action == ACTION_SYSTEMCTL);
3363 streq(args[0], "clear-jobs") ||
3364 streq(args[0], "cancel") ? "ClearJobs" :
3365 streq(args[0], "daemon-reexec") ? "Reexecute" :
3366 streq(args[0], "reset-failed") ? "ResetFailed" :
3367 streq(args[0], "halt") ? "Halt" :
3368 streq(args[0], "poweroff") ? "PowerOff" :
3369 streq(args[0], "reboot") ? "Reboot" :
3370 streq(args[0], "kexec") ? "KExec" :
3371 streq(args[0], "exit") ? "Exit" :
3372 /* "daemon-reload" */ "Reload";
3375 if (!(m = dbus_message_new_method_call(
3376 "org.freedesktop.systemd1",
3377 "/org/freedesktop/systemd1",
3378 "org.freedesktop.systemd1.Manager",
3380 log_error("Could not allocate message.");
3384 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3386 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
3387 /* There's always a fallback possible for
3388 * legacy actions. */
3393 if (streq(method, "Reexecute") && dbus_error_has_name(&error, DBUS_ERROR_NO_REPLY)) {
3394 /* On reexecution, we expect a disconnect, not
3400 log_error("Failed to issue method call: %s", bus_error_message(&error));
3409 dbus_message_unref(m);
3412 dbus_message_unref(reply);
3414 dbus_error_free(&error);
3419 static int reset_failed(DBusConnection *bus, char **args) {
3420 DBusMessage *m = NULL;
3426 dbus_error_init(&error);
3428 if (strv_length(args) <= 1)
3429 return daemon_reload(bus, args);
3431 STRV_FOREACH(name, args+1) {
3434 if (!(m = dbus_message_new_method_call(
3435 "org.freedesktop.systemd1",
3436 "/org/freedesktop/systemd1",
3437 "org.freedesktop.systemd1.Manager",
3438 "ResetFailedUnit"))) {
3439 log_error("Could not allocate message.");
3444 if (!dbus_message_append_args(m,
3445 DBUS_TYPE_STRING, name,
3446 DBUS_TYPE_INVALID)) {
3447 log_error("Could not append arguments to message.");
3452 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3453 log_error("Failed to issue method call: %s", bus_error_message(&error));
3458 dbus_message_unref(m);
3459 dbus_message_unref(reply);
3467 dbus_message_unref(m);
3469 dbus_error_free(&error);
3474 static int show_enviroment(DBusConnection *bus, char **args) {
3475 DBusMessage *m = NULL, *reply = NULL;
3477 DBusMessageIter iter, sub, sub2;
3480 *interface = "org.freedesktop.systemd1.Manager",
3481 *property = "Environment";
3483 dbus_error_init(&error);
3485 pager_open_if_enabled();
3487 if (!(m = dbus_message_new_method_call(
3488 "org.freedesktop.systemd1",
3489 "/org/freedesktop/systemd1",
3490 "org.freedesktop.DBus.Properties",
3492 log_error("Could not allocate message.");
3496 if (!dbus_message_append_args(m,
3497 DBUS_TYPE_STRING, &interface,
3498 DBUS_TYPE_STRING, &property,
3499 DBUS_TYPE_INVALID)) {
3500 log_error("Could not append arguments to message.");
3505 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3506 log_error("Failed to issue method call: %s", bus_error_message(&error));
3511 if (!dbus_message_iter_init(reply, &iter) ||
3512 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
3513 log_error("Failed to parse reply.");
3518 dbus_message_iter_recurse(&iter, &sub);
3520 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
3521 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
3522 log_error("Failed to parse reply.");
3527 dbus_message_iter_recurse(&sub, &sub2);
3529 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
3532 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
3533 log_error("Failed to parse reply.");
3538 dbus_message_iter_get_basic(&sub2, &text);
3539 printf("%s\n", text);
3541 dbus_message_iter_next(&sub2);
3548 dbus_message_unref(m);
3551 dbus_message_unref(reply);
3553 dbus_error_free(&error);