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 General Public License as published by
10 the Free Software Foundation; either version 2 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 General Public License for more details.
18 You should have received a copy of the GNU 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>
37 #include <dbus/dbus.h>
43 #include "utmp-wtmp.h"
47 #include "dbus-common.h"
48 #include "cgroup-show.h"
49 #include "cgroup-util.h"
51 #include "path-lookup.h"
52 #include "conf-parser.h"
53 #include "sd-daemon.h"
54 #include "shutdownd.h"
55 #include "exit-status.h"
56 #include "bus-errors.h"
58 #include "unit-name.h"
60 static const char *arg_type = NULL;
61 static char **arg_property = NULL;
62 static bool arg_all = false;
63 static const char *arg_job_mode = "replace";
64 static bool arg_user = false;
65 static bool arg_global = false;
66 static bool arg_immediate = false;
67 static bool arg_no_block = false;
68 static bool arg_no_pager = false;
69 static bool arg_no_wtmp = false;
70 static bool arg_no_sync = false;
71 static bool arg_no_wall = false;
72 static bool arg_no_reload = false;
73 static bool arg_dry = false;
74 static bool arg_quiet = false;
75 static bool arg_full = false;
76 static bool arg_force = false;
77 static bool arg_defaults = false;
78 static bool arg_ask_password = false;
79 static bool arg_failed = false;
80 static char **arg_wall = NULL;
81 static const char *arg_kill_who = NULL;
82 static const char *arg_kill_mode = NULL;
83 static const char *arg_root = NULL;
84 static int arg_signal = SIGTERM;
85 static usec_t arg_when = 0;
104 ACTION_CANCEL_SHUTDOWN,
106 } arg_action = ACTION_SYSTEMCTL;
112 static enum transport {
116 } arg_transport = TRANSPORT_NORMAL;
117 static const char *arg_host = NULL;
119 static bool private_bus = false;
121 static pid_t pager_pid = 0;
122 static pid_t agent_pid = 0;
124 static int daemon_reload(DBusConnection *bus, char **args, unsigned n);
125 static void pager_open(void);
127 static bool on_tty(void) {
130 /* Note that this is invoked relatively early, before we start
131 * the pager. That means the value we return reflects whether
132 * we originally were started on a tty, not if we currently
133 * are. But this is intended, since we want colour and so on
134 * when run in our own pager. */
136 if (_unlikely_(t < 0))
137 t = isatty(STDOUT_FILENO) > 0;
142 static void spawn_ask_password_agent(void) {
148 /* We check STDIN here, not STDOUT, since this is about input,
150 if (!isatty(STDIN_FILENO))
153 if (!arg_ask_password)
161 /* Spawns a temporary TTY agent, making sure it goes away when
164 if ((agent_pid = fork()) < 0)
167 if (agent_pid == 0) {
171 bool stdout_is_tty, stderr_is_tty;
173 /* Make sure the agent goes away when the parent dies */
174 if (prctl(PR_SET_PDEATHSIG, SIGTERM) < 0)
177 /* Check whether our parent died before we were able
178 * to set the death signal */
179 if (getppid() != parent)
182 /* Don't leak fds to the agent */
183 close_all_fds(NULL, 0);
185 stdout_is_tty = isatty(STDOUT_FILENO);
186 stderr_is_tty = isatty(STDERR_FILENO);
188 if (!stdout_is_tty || !stderr_is_tty) {
189 /* Detach from stdout/stderr. and reopen
190 * /dev/tty for them. This is important to
191 * ensure that when systemctl is started via
192 * popen() or a similar call that expects to
193 * read EOF we actually do generate EOF and
194 * not delay this indefinitely by because we
195 * keep an unused copy of stdin around. */
196 if ((fd = open("/dev/tty", O_WRONLY)) < 0) {
197 log_error("Failed to open /dev/tty: %m");
202 dup2(fd, STDOUT_FILENO);
205 dup2(fd, STDERR_FILENO);
211 execl(SYSTEMD_TTY_ASK_PASSWORD_AGENT_BINARY_PATH, SYSTEMD_TTY_ASK_PASSWORD_AGENT_BINARY_PATH, "--watch", NULL);
213 log_error("Unable to execute agent: %m");
218 static const char *ansi_highlight(bool b) {
223 return b ? ANSI_HIGHLIGHT_ON : ANSI_HIGHLIGHT_OFF;
226 static const char *ansi_highlight_green(bool b) {
231 return b ? ANSI_HIGHLIGHT_GREEN_ON : ANSI_HIGHLIGHT_OFF;
234 static bool error_is_no_service(const DBusError *error) {
237 if (!dbus_error_is_set(error))
240 if (dbus_error_has_name(error, DBUS_ERROR_NAME_HAS_NO_OWNER))
243 if (dbus_error_has_name(error, DBUS_ERROR_SERVICE_UNKNOWN))
246 return startswith(error->name, "org.freedesktop.DBus.Error.Spawn.");
249 static int translate_bus_error_to_exit_status(int r, const DBusError *error) {
252 if (!dbus_error_is_set(error))
255 if (dbus_error_has_name(error, DBUS_ERROR_ACCESS_DENIED) ||
256 dbus_error_has_name(error, BUS_ERROR_ONLY_BY_DEPENDENCY) ||
257 dbus_error_has_name(error, BUS_ERROR_NO_ISOLATION) ||
258 dbus_error_has_name(error, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE))
259 return EXIT_NOPERMISSION;
261 if (dbus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT))
262 return EXIT_NOTINSTALLED;
264 if (dbus_error_has_name(error, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE) ||
265 dbus_error_has_name(error, BUS_ERROR_NOT_SUPPORTED))
266 return EXIT_NOTIMPLEMENTED;
268 if (dbus_error_has_name(error, BUS_ERROR_LOAD_FAILED))
269 return EXIT_NOTCONFIGURED;
277 static int bus_iter_get_basic_and_next(DBusMessageIter *iter, int type, void *data, bool next) {
282 if (dbus_message_iter_get_arg_type(iter) != type)
285 dbus_message_iter_get_basic(iter, data);
287 if (!dbus_message_iter_next(iter) != !next)
293 static void warn_wall(enum action action) {
294 static const char *table[_ACTION_MAX] = {
295 [ACTION_HALT] = "The system is going down for system halt NOW!",
296 [ACTION_REBOOT] = "The system is going down for reboot NOW!",
297 [ACTION_POWEROFF] = "The system is going down for power-off NOW!",
298 [ACTION_KEXEC] = "The system is going down for kexec reboot NOW!",
299 [ACTION_RESCUE] = "The system is going down to rescue mode NOW!",
300 [ACTION_EMERGENCY] = "The system is going down to emergency mode NOW!"
309 if (!(p = strv_join(arg_wall, " "))) {
310 log_error("Failed to join strings.");
326 utmp_wall(table[action], NULL);
331 const char *description;
332 const char *load_state;
333 const char *active_state;
334 const char *sub_state;
335 const char *following;
336 const char *unit_path;
338 const char *job_type;
339 const char *job_path;
342 static int compare_unit_info(const void *a, const void *b) {
344 const struct unit_info *u = a, *v = b;
346 d1 = strrchr(u->id, '.');
347 d2 = strrchr(v->id, '.');
352 if ((r = strcasecmp(d1, d2)) != 0)
356 return strcasecmp(u->id, v->id);
359 static bool output_show_unit(const struct unit_info *u) {
363 return streq(u->active_state, "failed");
365 return (!arg_type || ((dot = strrchr(u->id, '.')) &&
366 streq(dot+1, arg_type))) &&
367 (arg_all || !(streq(u->active_state, "inactive") || u->following[0]) || u->job_id > 0);
370 static void output_units_list(const struct unit_info *unit_infos, unsigned c) {
371 unsigned active_len, sub_len, job_len, n_shown = 0;
372 const struct unit_info *u;
374 active_len = sizeof("ACTIVE")-1;
375 sub_len = sizeof("SUB")-1;
376 job_len = sizeof("JOB")-1;
378 for (u = unit_infos; u < unit_infos + c; u++) {
379 if (!output_show_unit(u))
382 active_len = MAX(active_len, strlen(u->active_state));
383 sub_len = MAX(sub_len, strlen(u->sub_state));
385 job_len = MAX(job_len, strlen(u->job_type));
389 printf("%-25s %-6s %-*s %-*s %-*s", "UNIT", "LOAD",
390 active_len, "ACTIVE", sub_len, "SUB", job_len, "JOB");
391 if (columns() >= 80+12 || arg_full || !arg_no_pager)
392 printf(" %s\n", "DESCRIPTION");
397 for (u = unit_infos; u < unit_infos + c; u++) {
400 const char *on_loaded, *off_loaded;
401 const char *on_active, *off_active;
403 if (!output_show_unit(u))
408 if (!streq(u->load_state, "loaded") &&
409 !streq(u->load_state, "banned")) {
410 on_loaded = ansi_highlight(true);
411 off_loaded = ansi_highlight(false);
413 on_loaded = off_loaded = "";
415 if (streq(u->active_state, "failed")) {
416 on_active = ansi_highlight(true);
417 off_active = ansi_highlight(false);
419 on_active = off_active = "";
421 e = arg_full ? NULL : ellipsize(u->id, 25, 33);
423 printf("%-25s %s%-6s%s %s%-*s %-*s%s%n",
425 on_loaded, u->load_state, off_loaded,
426 on_active, active_len, u->active_state,
427 sub_len, u->sub_state, off_active,
432 a -= strlen(on_loaded) + strlen(off_loaded);
433 a -= strlen(on_active) + strlen(off_active);
436 printf(" %-*s", job_len, u->job_type);
440 if (a + b + 1 < columns()) {
442 printf(" %-*s", job_len, "");
444 if (arg_full || !arg_no_pager)
445 printf(" %s", u->description);
447 printf(" %.*s", columns() - a - b - 1, u->description);
454 printf("\nLOAD = Reflects whether the unit definition was properly loaded.\n"
455 "ACTIVE = The high-level unit activation state, i.e. generalization of SUB.\n"
456 "SUB = The low-level unit activation state, values depend on unit type.\n"
457 "JOB = Pending job for the unit.\n");
460 printf("\n%u units listed.\n", n_shown);
462 printf("\n%u units listed. Pass --all to see inactive units, too.\n", n_shown);
466 static int list_units(DBusConnection *bus, char **args, unsigned n) {
467 DBusMessage *m = NULL, *reply = NULL;
470 DBusMessageIter iter, sub, sub2;
471 unsigned c = 0, n_units = 0;
472 struct unit_info *unit_infos = NULL;
474 dbus_error_init(&error);
480 if (!(m = dbus_message_new_method_call(
481 "org.freedesktop.systemd1",
482 "/org/freedesktop/systemd1",
483 "org.freedesktop.systemd1.Manager",
485 log_error("Could not allocate message.");
489 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
490 log_error("Failed to issue method call: %s", bus_error_message(&error));
495 if (!dbus_message_iter_init(reply, &iter) ||
496 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
497 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
498 log_error("Failed to parse reply.");
503 dbus_message_iter_recurse(&iter, &sub);
505 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
508 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
509 log_error("Failed to parse reply.");
517 n_units = MAX(2*c, 16);
518 w = realloc(unit_infos, sizeof(struct unit_info) * n_units);
521 log_error("Failed to allocate unit array.");
531 dbus_message_iter_recurse(&sub, &sub2);
533 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->id, true) < 0 ||
534 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->description, true) < 0 ||
535 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->load_state, true) < 0 ||
536 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->active_state, true) < 0 ||
537 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->sub_state, true) < 0 ||
538 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->following, true) < 0 ||
539 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &u->unit_path, true) < 0 ||
540 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &u->job_id, true) < 0 ||
541 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->job_type, true) < 0 ||
542 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &u->job_path, false) < 0) {
543 log_error("Failed to parse reply.");
548 dbus_message_iter_next(&sub);
553 qsort(unit_infos, c, sizeof(struct unit_info), compare_unit_info);
554 output_units_list(unit_infos, c);
561 dbus_message_unref(m);
564 dbus_message_unref(reply);
568 dbus_error_free(&error);
573 static int dot_one_property(const char *name, const char *prop, DBusMessageIter *iter) {
574 static const char * const colors[] = {
575 "Requires", "[color=\"black\"]",
576 "RequiresOverridable", "[color=\"black\"]",
577 "Requisite", "[color=\"darkblue\"]",
578 "RequisiteOverridable", "[color=\"darkblue\"]",
579 "Wants", "[color=\"darkgrey\"]",
580 "Conflicts", "[color=\"red\"]",
581 "ConflictedBy", "[color=\"red\"]",
582 "After", "[color=\"green\"]"
585 const char *c = NULL;
592 for (i = 0; i < ELEMENTSOF(colors); i += 2)
593 if (streq(colors[i], prop)) {
601 if (arg_dot != DOT_ALL)
602 if ((arg_dot == DOT_ORDER) != streq(prop, "After"))
605 switch (dbus_message_iter_get_arg_type(iter)) {
607 case DBUS_TYPE_ARRAY:
609 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
612 dbus_message_iter_recurse(iter, &sub);
614 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
617 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
618 dbus_message_iter_get_basic(&sub, &s);
619 printf("\t\"%s\"->\"%s\" %s;\n", name, s, c);
621 dbus_message_iter_next(&sub);
631 static int dot_one(DBusConnection *bus, const char *name, const char *path) {
632 DBusMessage *m = NULL, *reply = NULL;
633 const char *interface = "org.freedesktop.systemd1.Unit";
636 DBusMessageIter iter, sub, sub2, sub3;
641 dbus_error_init(&error);
643 if (!(m = dbus_message_new_method_call(
644 "org.freedesktop.systemd1",
646 "org.freedesktop.DBus.Properties",
648 log_error("Could not allocate message.");
653 if (!dbus_message_append_args(m,
654 DBUS_TYPE_STRING, &interface,
655 DBUS_TYPE_INVALID)) {
656 log_error("Could not append arguments to message.");
661 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
662 log_error("Failed to issue method call: %s", bus_error_message(&error));
667 if (!dbus_message_iter_init(reply, &iter) ||
668 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
669 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
670 log_error("Failed to parse reply.");
675 dbus_message_iter_recurse(&iter, &sub);
677 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
680 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
681 log_error("Failed to parse reply.");
686 dbus_message_iter_recurse(&sub, &sub2);
688 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &prop, true) < 0) {
689 log_error("Failed to parse reply.");
694 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
695 log_error("Failed to parse reply.");
700 dbus_message_iter_recurse(&sub2, &sub3);
702 if (dot_one_property(name, prop, &sub3)) {
703 log_error("Failed to parse reply.");
708 dbus_message_iter_next(&sub);
715 dbus_message_unref(m);
718 dbus_message_unref(reply);
720 dbus_error_free(&error);
725 static int dot(DBusConnection *bus, char **args, unsigned n) {
726 DBusMessage *m = NULL, *reply = NULL;
729 DBusMessageIter iter, sub, sub2;
731 dbus_error_init(&error);
735 if (!(m = dbus_message_new_method_call(
736 "org.freedesktop.systemd1",
737 "/org/freedesktop/systemd1",
738 "org.freedesktop.systemd1.Manager",
740 log_error("Could not allocate message.");
744 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
745 log_error("Failed to issue method call: %s", bus_error_message(&error));
750 if (!dbus_message_iter_init(reply, &iter) ||
751 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
752 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
753 log_error("Failed to parse reply.");
758 printf("digraph systemd {\n");
760 dbus_message_iter_recurse(&iter, &sub);
761 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
762 const char *id, *description, *load_state, *active_state, *sub_state, *following, *unit_path;
764 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
765 log_error("Failed to parse reply.");
770 dbus_message_iter_recurse(&sub, &sub2);
772 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
773 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &description, true) < 0 ||
774 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &load_state, true) < 0 ||
775 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &active_state, true) < 0 ||
776 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &sub_state, true) < 0 ||
777 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &following, true) < 0 ||
778 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, true) < 0) {
779 log_error("Failed to parse reply.");
784 if ((r = dot_one(bus, id, unit_path)) < 0)
787 /* printf("\t\"%s\";\n", id); */
788 dbus_message_iter_next(&sub);
793 log_info(" Color legend: black = Requires\n"
794 " dark blue = Requisite\n"
795 " dark grey = Wants\n"
799 if (isatty(fileno(stdout)))
800 log_notice("-- You probably want to process this output with graphviz' dot tool.\n"
801 "-- Try a shell pipeline like 'systemctl dot | dot -Tsvg > systemd.svg'!\n");
807 dbus_message_unref(m);
810 dbus_message_unref(reply);
812 dbus_error_free(&error);
817 static int list_jobs(DBusConnection *bus, char **args, unsigned n) {
818 DBusMessage *m = NULL, *reply = NULL;
821 DBusMessageIter iter, sub, sub2;
824 dbus_error_init(&error);
830 if (!(m = dbus_message_new_method_call(
831 "org.freedesktop.systemd1",
832 "/org/freedesktop/systemd1",
833 "org.freedesktop.systemd1.Manager",
835 log_error("Could not allocate message.");
839 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
840 log_error("Failed to issue method call: %s", bus_error_message(&error));
845 if (!dbus_message_iter_init(reply, &iter) ||
846 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
847 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
848 log_error("Failed to parse reply.");
853 dbus_message_iter_recurse(&iter, &sub);
855 if (isatty(STDOUT_FILENO))
856 printf("%4s %-25s %-15s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
858 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
859 const char *name, *type, *state, *job_path, *unit_path;
863 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
864 log_error("Failed to parse reply.");
869 dbus_message_iter_recurse(&sub, &sub2);
871 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &id, true) < 0 ||
872 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0 ||
873 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
874 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, true) < 0 ||
875 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, true) < 0 ||
876 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, false) < 0) {
877 log_error("Failed to parse reply.");
882 e = arg_full ? NULL : ellipsize(name, 25, 33);
883 printf("%4u %-25s %-15s %-7s\n", id, e ? e : name, type, state);
888 dbus_message_iter_next(&sub);
891 if (isatty(STDOUT_FILENO))
892 printf("\n%u jobs listed.\n", k);
898 dbus_message_unref(m);
901 dbus_message_unref(reply);
903 dbus_error_free(&error);
908 static int load_unit(DBusConnection *bus, char **args, unsigned n) {
909 DBusMessage *m = NULL, *reply = NULL;
914 dbus_error_init(&error);
919 for (i = 1; i < n; i++) {
921 if (!(m = dbus_message_new_method_call(
922 "org.freedesktop.systemd1",
923 "/org/freedesktop/systemd1",
924 "org.freedesktop.systemd1.Manager",
926 log_error("Could not allocate message.");
931 if (!dbus_message_append_args(m,
932 DBUS_TYPE_STRING, &args[i],
933 DBUS_TYPE_INVALID)) {
934 log_error("Could not append arguments to message.");
939 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
940 log_error("Failed to issue method call: %s", bus_error_message(&error));
945 dbus_message_unref(m);
946 dbus_message_unref(reply);
955 dbus_message_unref(m);
958 dbus_message_unref(reply);
960 dbus_error_free(&error);
965 static int cancel_job(DBusConnection *bus, char **args, unsigned n) {
966 DBusMessage *m = NULL, *reply = NULL;
971 dbus_error_init(&error);
977 return daemon_reload(bus, args, n);
979 for (i = 1; i < n; i++) {
983 if (!(m = dbus_message_new_method_call(
984 "org.freedesktop.systemd1",
985 "/org/freedesktop/systemd1",
986 "org.freedesktop.systemd1.Manager",
988 log_error("Could not allocate message.");
993 if ((r = safe_atou(args[i], &id)) < 0) {
994 log_error("Failed to parse job id: %s", strerror(-r));
998 assert_cc(sizeof(uint32_t) == sizeof(id));
999 if (!dbus_message_append_args(m,
1000 DBUS_TYPE_UINT32, &id,
1001 DBUS_TYPE_INVALID)) {
1002 log_error("Could not append arguments to message.");
1007 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1008 log_error("Failed to issue method call: %s", bus_error_message(&error));
1013 if (!dbus_message_get_args(reply, &error,
1014 DBUS_TYPE_OBJECT_PATH, &path,
1015 DBUS_TYPE_INVALID)) {
1016 log_error("Failed to parse reply: %s", bus_error_message(&error));
1021 dbus_message_unref(m);
1022 if (!(m = dbus_message_new_method_call(
1023 "org.freedesktop.systemd1",
1025 "org.freedesktop.systemd1.Job",
1027 log_error("Could not allocate message.");
1032 dbus_message_unref(reply);
1033 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1034 log_error("Failed to issue method call: %s", bus_error_message(&error));
1039 dbus_message_unref(m);
1040 dbus_message_unref(reply);
1048 dbus_message_unref(m);
1051 dbus_message_unref(reply);
1053 dbus_error_free(&error);
1058 static bool need_daemon_reload(DBusConnection *bus, const char *unit) {
1059 DBusMessage *m = NULL, *reply = NULL;
1060 dbus_bool_t b = FALSE;
1061 DBusMessageIter iter, sub;
1063 *interface = "org.freedesktop.systemd1.Unit",
1064 *property = "NeedDaemonReload",
1067 /* We ignore all errors here, since this is used to show a warning only */
1069 if (!(m = dbus_message_new_method_call(
1070 "org.freedesktop.systemd1",
1071 "/org/freedesktop/systemd1",
1072 "org.freedesktop.systemd1.Manager",
1076 if (!dbus_message_append_args(m,
1077 DBUS_TYPE_STRING, &unit,
1081 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL)))
1084 if (!dbus_message_get_args(reply, NULL,
1085 DBUS_TYPE_OBJECT_PATH, &path,
1089 dbus_message_unref(m);
1090 if (!(m = dbus_message_new_method_call(
1091 "org.freedesktop.systemd1",
1093 "org.freedesktop.DBus.Properties",
1097 if (!dbus_message_append_args(m,
1098 DBUS_TYPE_STRING, &interface,
1099 DBUS_TYPE_STRING, &property,
1100 DBUS_TYPE_INVALID)) {
1104 dbus_message_unref(reply);
1105 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL)))
1108 if (!dbus_message_iter_init(reply, &iter) ||
1109 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
1112 dbus_message_iter_recurse(&iter, &sub);
1114 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_BOOLEAN)
1117 dbus_message_iter_get_basic(&sub, &b);
1121 dbus_message_unref(m);
1124 dbus_message_unref(reply);
1129 typedef struct WaitData {
1134 static DBusHandlerResult wait_filter(DBusConnection *connection, DBusMessage *message, void *data) {
1142 dbus_error_init(&error);
1144 log_debug("Got D-Bus request: %s.%s() on %s",
1145 dbus_message_get_interface(message),
1146 dbus_message_get_member(message),
1147 dbus_message_get_path(message));
1149 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
1150 log_error("Warning! D-Bus connection terminated.");
1151 dbus_connection_close(connection);
1153 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
1155 const char *path, *result;
1156 dbus_bool_t success = true;
1158 if (dbus_message_get_args(message, &error,
1159 DBUS_TYPE_UINT32, &id,
1160 DBUS_TYPE_OBJECT_PATH, &path,
1161 DBUS_TYPE_STRING, &result,
1162 DBUS_TYPE_INVALID)) {
1165 if ((p = set_remove(d->set, (char*) path)))
1169 d->result = strdup(result);
1174 dbus_error_free(&error);
1176 if (dbus_message_get_args(message, &error,
1177 DBUS_TYPE_UINT32, &id,
1178 DBUS_TYPE_OBJECT_PATH, &path,
1179 DBUS_TYPE_BOOLEAN, &success,
1180 DBUS_TYPE_INVALID)) {
1183 /* Compatibility with older systemd versions <
1184 * 19 during upgrades. This should be dropped
1187 if ((p = set_remove(d->set, (char*) path)))
1191 d->result = strdup("failed");
1197 log_error("Failed to parse message: %s", bus_error_message(&error));
1201 dbus_error_free(&error);
1202 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1205 static int enable_wait_for_jobs(DBusConnection *bus) {
1213 dbus_error_init(&error);
1214 dbus_bus_add_match(bus,
1216 "sender='org.freedesktop.systemd1',"
1217 "interface='org.freedesktop.systemd1.Manager',"
1218 "member='JobRemoved',"
1219 "path='/org/freedesktop/systemd1'",
1222 if (dbus_error_is_set(&error)) {
1223 log_error("Failed to add match: %s", bus_error_message(&error));
1224 dbus_error_free(&error);
1228 /* This is slightly dirty, since we don't undo the match registrations. */
1232 static int wait_for_jobs(DBusConnection *bus, Set *s) {
1242 if (!dbus_connection_add_filter(bus, wait_filter, &d, NULL)) {
1243 log_error("Failed to add filter.");
1248 while (!set_isempty(s) &&
1249 dbus_connection_read_write_dispatch(bus, -1))
1252 if (!arg_quiet && d.result) {
1253 if (streq(d.result, "timeout"))
1254 log_error("Job timed out.");
1255 else if (streq(d.result, "canceled"))
1256 log_error("Job canceled.");
1257 else if (streq(d.result, "dependency"))
1258 log_error("A dependency job failed. See system logs for details.");
1259 else if (!streq(d.result, "done") && !streq(d.result, "skipped"))
1260 log_error("Job failed. See system logs and 'systemctl status' for details.");
1263 if (streq_ptr(d.result, "timeout"))
1265 else if (streq_ptr(d.result, "canceled"))
1267 else if (!streq_ptr(d.result, "done") && !streq_ptr(d.result, "skipped"))
1275 /* This is slightly dirty, since we don't undo the filter registration. */
1280 static int start_unit_one(
1281 DBusConnection *bus,
1288 DBusMessage *m = NULL, *reply = NULL;
1297 assert(arg_no_block || s);
1299 if (!(m = dbus_message_new_method_call(
1300 "org.freedesktop.systemd1",
1301 "/org/freedesktop/systemd1",
1302 "org.freedesktop.systemd1.Manager",
1304 log_error("Could not allocate message.");
1309 if (!dbus_message_append_args(m,
1310 DBUS_TYPE_STRING, &name,
1311 DBUS_TYPE_STRING, &mode,
1312 DBUS_TYPE_INVALID)) {
1313 log_error("Could not append arguments to message.");
1318 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, error))) {
1320 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(error)) {
1321 /* There's always a fallback possible for
1322 * legacy actions. */
1327 log_error("Failed to issue method call: %s", bus_error_message(error));
1332 if (!dbus_message_get_args(reply, error,
1333 DBUS_TYPE_OBJECT_PATH, &path,
1334 DBUS_TYPE_INVALID)) {
1335 log_error("Failed to parse reply: %s", bus_error_message(error));
1340 if (need_daemon_reload(bus, name))
1341 log_warning("Unit file of created job changed on disk, 'systemctl %s daemon-reload' recommended.",
1342 arg_user ? "--user" : "--system");
1344 if (!arg_no_block) {
1347 if (!(p = strdup(path))) {
1348 log_error("Failed to duplicate path.");
1353 if ((r = set_put(s, p)) < 0) {
1355 log_error("Failed to add path to set.");
1364 dbus_message_unref(m);
1367 dbus_message_unref(reply);
1372 static enum action verb_to_action(const char *verb) {
1373 if (streq(verb, "halt"))
1375 else if (streq(verb, "poweroff"))
1376 return ACTION_POWEROFF;
1377 else if (streq(verb, "reboot"))
1378 return ACTION_REBOOT;
1379 else if (streq(verb, "kexec"))
1380 return ACTION_KEXEC;
1381 else if (streq(verb, "rescue"))
1382 return ACTION_RESCUE;
1383 else if (streq(verb, "emergency"))
1384 return ACTION_EMERGENCY;
1385 else if (streq(verb, "default"))
1386 return ACTION_DEFAULT;
1387 else if (streq(verb, "exit"))
1390 return ACTION_INVALID;
1393 static int start_unit(DBusConnection *bus, char **args, unsigned n) {
1395 static const char * const table[_ACTION_MAX] = {
1396 [ACTION_HALT] = SPECIAL_HALT_TARGET,
1397 [ACTION_POWEROFF] = SPECIAL_POWEROFF_TARGET,
1398 [ACTION_REBOOT] = SPECIAL_REBOOT_TARGET,
1399 [ACTION_KEXEC] = SPECIAL_KEXEC_TARGET,
1400 [ACTION_RUNLEVEL2] = SPECIAL_RUNLEVEL2_TARGET,
1401 [ACTION_RUNLEVEL3] = SPECIAL_RUNLEVEL3_TARGET,
1402 [ACTION_RUNLEVEL4] = SPECIAL_RUNLEVEL4_TARGET,
1403 [ACTION_RUNLEVEL5] = SPECIAL_RUNLEVEL5_TARGET,
1404 [ACTION_RESCUE] = SPECIAL_RESCUE_TARGET,
1405 [ACTION_EMERGENCY] = SPECIAL_EMERGENCY_TARGET,
1406 [ACTION_DEFAULT] = SPECIAL_DEFAULT_TARGET,
1407 [ACTION_EXIT] = SPECIAL_EXIT_TARGET
1412 const char *method, *mode, *one_name;
1416 dbus_error_init(&error);
1420 spawn_ask_password_agent();
1422 if (arg_action == ACTION_SYSTEMCTL) {
1424 streq(args[0], "stop") ||
1425 streq(args[0], "condstop") ? "StopUnit" :
1426 streq(args[0], "reload") ? "ReloadUnit" :
1427 streq(args[0], "restart") ? "RestartUnit" :
1429 streq(args[0], "try-restart") ||
1430 streq(args[0], "condrestart") ? "TryRestartUnit" :
1432 streq(args[0], "reload-or-restart") ? "ReloadOrRestartUnit" :
1434 streq(args[0], "reload-or-try-restart") ||
1435 streq(args[0], "condreload") ||
1437 streq(args[0], "force-reload") ? "ReloadOrTryRestartUnit" :
1441 (streq(args[0], "isolate") ||
1442 streq(args[0], "rescue") ||
1443 streq(args[0], "emergency")) ? "isolate" : arg_job_mode;
1445 one_name = table[verb_to_action(args[0])];
1448 assert(arg_action < ELEMENTSOF(table));
1449 assert(table[arg_action]);
1451 method = "StartUnit";
1453 mode = (arg_action == ACTION_EMERGENCY ||
1454 arg_action == ACTION_RESCUE ||
1455 arg_action == ACTION_RUNLEVEL2 ||
1456 arg_action == ACTION_RUNLEVEL3 ||
1457 arg_action == ACTION_RUNLEVEL4 ||
1458 arg_action == ACTION_RUNLEVEL5) ? "isolate" : "replace";
1460 one_name = table[arg_action];
1463 if (!arg_no_block) {
1464 if ((ret = enable_wait_for_jobs(bus)) < 0) {
1465 log_error("Could not watch jobs: %s", strerror(-ret));
1469 if (!(s = set_new(string_hash_func, string_compare_func))) {
1470 log_error("Failed to allocate set.");
1477 if ((ret = start_unit_one(bus, method, one_name, mode, &error, s)) <= 0)
1480 for (i = 1; i < n; i++)
1481 if ((r = start_unit_one(bus, method, args[i], mode, &error, s)) != 0) {
1482 ret = translate_bus_error_to_exit_status(r, &error);
1483 dbus_error_free(&error);
1488 if ((r = wait_for_jobs(bus, s)) < 0) {
1497 dbus_error_free(&error);
1502 static int start_special(DBusConnection *bus, char **args, unsigned n) {
1509 (streq(args[0], "halt") ||
1510 streq(args[0], "poweroff") ||
1511 streq(args[0], "reboot") ||
1512 streq(args[0], "kexec") ||
1513 streq(args[0], "exit")))
1514 return daemon_reload(bus, args, n);
1516 r = start_unit(bus, args, n);
1519 warn_wall(verb_to_action(args[0]));
1524 static int check_unit(DBusConnection *bus, char **args, unsigned n) {
1525 DBusMessage *m = NULL, *reply = NULL;
1527 *interface = "org.freedesktop.systemd1.Unit",
1528 *property = "ActiveState";
1529 int r = 3; /* According to LSB: "program is not running" */
1536 dbus_error_init(&error);
1538 for (i = 1; i < n; i++) {
1539 const char *path = NULL;
1541 DBusMessageIter iter, sub;
1543 if (!(m = dbus_message_new_method_call(
1544 "org.freedesktop.systemd1",
1545 "/org/freedesktop/systemd1",
1546 "org.freedesktop.systemd1.Manager",
1548 log_error("Could not allocate message.");
1553 if (!dbus_message_append_args(m,
1554 DBUS_TYPE_STRING, &args[i],
1555 DBUS_TYPE_INVALID)) {
1556 log_error("Could not append arguments to message.");
1561 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1563 /* Hmm, cannot figure out anything about this unit... */
1567 dbus_error_free(&error);
1568 dbus_message_unref(m);
1573 if (!dbus_message_get_args(reply, &error,
1574 DBUS_TYPE_OBJECT_PATH, &path,
1575 DBUS_TYPE_INVALID)) {
1576 log_error("Failed to parse reply: %s", bus_error_message(&error));
1581 dbus_message_unref(m);
1582 if (!(m = dbus_message_new_method_call(
1583 "org.freedesktop.systemd1",
1585 "org.freedesktop.DBus.Properties",
1587 log_error("Could not allocate message.");
1592 if (!dbus_message_append_args(m,
1593 DBUS_TYPE_STRING, &interface,
1594 DBUS_TYPE_STRING, &property,
1595 DBUS_TYPE_INVALID)) {
1596 log_error("Could not append arguments to message.");
1601 dbus_message_unref(reply);
1602 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1603 log_error("Failed to issue method call: %s", bus_error_message(&error));
1608 if (!dbus_message_iter_init(reply, &iter) ||
1609 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1610 log_error("Failed to parse reply.");
1615 dbus_message_iter_recurse(&iter, &sub);
1617 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1618 log_error("Failed to parse reply.");
1623 dbus_message_iter_get_basic(&sub, &state);
1628 if (streq(state, "active") || streq(state, "reloading"))
1631 dbus_message_unref(m);
1632 dbus_message_unref(reply);
1638 dbus_message_unref(m);
1641 dbus_message_unref(reply);
1643 dbus_error_free(&error);
1648 static int kill_unit(DBusConnection *bus, char **args, unsigned n) {
1649 DBusMessage *m = NULL, *reply = NULL;
1657 dbus_error_init(&error);
1660 arg_kill_who = "all";
1663 arg_kill_mode = streq(arg_kill_who, "all") ? "control-group" : "process";
1665 for (i = 1; i < n; i++) {
1667 if (!(m = dbus_message_new_method_call(
1668 "org.freedesktop.systemd1",
1669 "/org/freedesktop/systemd1",
1670 "org.freedesktop.systemd1.Manager",
1672 log_error("Could not allocate message.");
1677 if (!dbus_message_append_args(m,
1678 DBUS_TYPE_STRING, &args[i],
1679 DBUS_TYPE_STRING, &arg_kill_who,
1680 DBUS_TYPE_STRING, &arg_kill_mode,
1681 DBUS_TYPE_INT32, &arg_signal,
1682 DBUS_TYPE_INVALID)) {
1683 log_error("Could not append arguments to message.");
1688 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1689 log_error("Failed to issue method call: %s", bus_error_message(&error));
1690 dbus_error_free(&error);
1694 dbus_message_unref(m);
1697 dbus_message_unref(reply);
1703 dbus_message_unref(m);
1706 dbus_message_unref(reply);
1708 dbus_error_free(&error);
1713 typedef struct ExecStatusInfo {
1721 usec_t start_timestamp;
1722 usec_t exit_timestamp;
1727 LIST_FIELDS(struct ExecStatusInfo, exec);
1730 static void exec_status_info_free(ExecStatusInfo *i) {
1739 static int exec_status_info_deserialize(DBusMessageIter *sub, ExecStatusInfo *i) {
1740 uint64_t start_timestamp, exit_timestamp, start_timestamp_monotonic, exit_timestamp_monotonic;
1741 DBusMessageIter sub2, sub3;
1745 int32_t code, status;
1751 if (dbus_message_iter_get_arg_type(sub) != DBUS_TYPE_STRUCT)
1754 dbus_message_iter_recurse(sub, &sub2);
1756 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0)
1759 if (!(i->path = strdup(path)))
1762 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_ARRAY ||
1763 dbus_message_iter_get_element_type(&sub2) != DBUS_TYPE_STRING)
1767 dbus_message_iter_recurse(&sub2, &sub3);
1768 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1769 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1770 dbus_message_iter_next(&sub3);
1775 if (!(i->argv = new0(char*, n+1)))
1779 dbus_message_iter_recurse(&sub2, &sub3);
1780 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1783 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1784 dbus_message_iter_get_basic(&sub3, &s);
1785 dbus_message_iter_next(&sub3);
1787 if (!(i->argv[n++] = strdup(s)))
1791 if (!dbus_message_iter_next(&sub2) ||
1792 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_BOOLEAN, &ignore, true) < 0 ||
1793 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_timestamp, true) < 0 ||
1794 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_timestamp_monotonic, true) < 0 ||
1795 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_timestamp, true) < 0 ||
1796 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_timestamp_monotonic, true) < 0 ||
1797 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, true) < 0 ||
1798 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &code, true) < 0 ||
1799 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &status, false) < 0)
1803 i->start_timestamp = (usec_t) start_timestamp;
1804 i->exit_timestamp = (usec_t) exit_timestamp;
1805 i->pid = (pid_t) pid;
1812 typedef struct UnitStatusInfo {
1814 const char *load_state;
1815 const char *active_state;
1816 const char *sub_state;
1818 const char *description;
1819 const char *following;
1822 const char *default_control_group;
1824 usec_t inactive_exit_timestamp;
1825 usec_t active_enter_timestamp;
1826 usec_t active_exit_timestamp;
1827 usec_t inactive_enter_timestamp;
1829 bool need_daemon_reload;
1834 const char *status_text;
1836 #ifdef HAVE_SYSV_COMPAT
1840 usec_t start_timestamp;
1841 usec_t exit_timestamp;
1843 int exit_code, exit_status;
1845 usec_t condition_timestamp;
1846 bool condition_result;
1849 unsigned n_accepted;
1850 unsigned n_connections;
1854 const char *sysfs_path;
1856 /* Mount, Automount */
1862 LIST_HEAD(ExecStatusInfo, exec);
1865 static void print_status_info(UnitStatusInfo *i) {
1867 const char *on, *off, *ss;
1869 char since1[FORMAT_TIMESTAMP_PRETTY_MAX], *s1;
1870 char since2[FORMAT_TIMESTAMP_MAX], *s2;
1874 /* This shows pretty information about a unit. See
1875 * print_property() for a low-level property printer */
1877 printf("%s", strna(i->id));
1879 if (i->description && !streq_ptr(i->id, i->description))
1880 printf(" - %s", i->description);
1885 printf("\t Follow: unit currently follows state of %s\n", i->following);
1887 if (streq_ptr(i->load_state, "failed") ||
1888 streq_ptr(i->load_state, "banned")) {
1889 on = ansi_highlight(true);
1890 off = ansi_highlight(false);
1895 printf("\t Loaded: %s%s%s (%s)\n", on, strna(i->load_state), off, i->path);
1897 printf("\t Loaded: %s%s%s\n", on, strna(i->load_state), off);
1899 ss = streq_ptr(i->active_state, i->sub_state) ? NULL : i->sub_state;
1901 if (streq_ptr(i->active_state, "failed")) {
1902 on = ansi_highlight(true);
1903 off = ansi_highlight(false);
1904 } else if (streq_ptr(i->active_state, "active") || streq_ptr(i->active_state, "reloading")) {
1905 on = ansi_highlight_green(true);
1906 off = ansi_highlight_green(false);
1911 printf("\t Active: %s%s (%s)%s",
1913 strna(i->active_state),
1917 printf("\t Active: %s%s%s",
1919 strna(i->active_state),
1922 timestamp = (streq_ptr(i->active_state, "active") ||
1923 streq_ptr(i->active_state, "reloading")) ? i->active_enter_timestamp :
1924 (streq_ptr(i->active_state, "inactive") ||
1925 streq_ptr(i->active_state, "failed")) ? i->inactive_enter_timestamp :
1926 streq_ptr(i->active_state, "activating") ? i->inactive_exit_timestamp :
1927 i->active_exit_timestamp;
1929 s1 = format_timestamp_pretty(since1, sizeof(since1), timestamp);
1930 s2 = format_timestamp(since2, sizeof(since2), timestamp);
1933 printf(" since %s; %s\n", s2, s1);
1935 printf(" since %s\n", s2);
1939 if (!i->condition_result && i->condition_timestamp > 0) {
1940 s1 = format_timestamp_pretty(since1, sizeof(since1), i->condition_timestamp);
1941 s2 = format_timestamp(since2, sizeof(since2), i->condition_timestamp);
1944 printf("\t start condition failed at %s; %s\n", s2, s1);
1946 printf("\t start condition failed at %s\n", s2);
1950 printf("\t Device: %s\n", i->sysfs_path);
1952 printf("\t Where: %s\n", i->where);
1954 printf("\t What: %s\n", i->what);
1957 printf("\tAccepted: %u; Connected: %u\n", i->n_accepted, i->n_connections);
1959 LIST_FOREACH(exec, p, i->exec) {
1963 /* Only show exited processes here */
1967 t = strv_join(p->argv, " ");
1968 printf("\t Process: %u %s=%s ", p->pid, p->name, strna(t));
1971 #ifdef HAVE_SYSV_COMPAT
1973 good = is_clean_exit_lsb(p->code, p->status);
1976 good = is_clean_exit(p->code, p->status);
1979 on = ansi_highlight(true);
1980 off = ansi_highlight(false);
1984 printf("%s(code=%s, ", on, sigchld_code_to_string(p->code));
1986 if (p->code == CLD_EXITED) {
1989 printf("status=%i", p->status);
1991 #ifdef HAVE_SYSV_COMPAT
1992 if ((c = exit_status_to_string(p->status, i->is_sysv ? EXIT_STATUS_LSB : EXIT_STATUS_SYSTEMD)))
1994 if ((c = exit_status_to_string(p->status, EXIT_STATUS_SYSTEMD)))
1999 printf("signal=%s", signal_to_string(p->status));
2001 printf(")%s\n", off);
2005 if (i->main_pid == p->pid &&
2006 i->start_timestamp == p->start_timestamp &&
2007 i->exit_timestamp == p->start_timestamp)
2008 /* Let's not show this twice */
2011 if (p->pid == i->control_pid)
2015 if (i->main_pid > 0 || i->control_pid > 0) {
2018 if (i->main_pid > 0) {
2019 printf("Main PID: %u", (unsigned) i->main_pid);
2023 get_process_name(i->main_pid, &t);
2028 } else if (i->exit_code > 0) {
2029 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
2031 if (i->exit_code == CLD_EXITED) {
2034 printf("status=%i", i->exit_status);
2036 #ifdef HAVE_SYSV_COMPAT
2037 if ((c = exit_status_to_string(i->exit_status, i->is_sysv ? EXIT_STATUS_LSB : EXIT_STATUS_SYSTEMD)))
2039 if ((c = exit_status_to_string(i->exit_status, EXIT_STATUS_SYSTEMD)))
2044 printf("signal=%s", signal_to_string(i->exit_status));
2049 if (i->main_pid > 0 && i->control_pid > 0)
2052 if (i->control_pid > 0) {
2055 printf(" Control: %u", (unsigned) i->control_pid);
2057 get_process_name(i->control_pid, &t);
2068 printf("\t Status: \"%s\"\n", i->status_text);
2070 if (i->default_control_group) {
2073 printf("\t CGroup: %s\n", i->default_control_group);
2075 if (arg_transport != TRANSPORT_SSH) {
2076 if ((c = columns()) > 18)
2081 show_cgroup_by_path(i->default_control_group, "\t\t ", c);
2085 if (i->need_daemon_reload)
2086 printf("\n%sWarning:%s Unit file changed on disk, 'systemctl %s daemon-reload' recommended.\n",
2087 ansi_highlight(true),
2088 ansi_highlight(false),
2089 arg_user ? "--user" : "--system");
2092 static int status_property(const char *name, DBusMessageIter *iter, UnitStatusInfo *i) {
2094 switch (dbus_message_iter_get_arg_type(iter)) {
2096 case DBUS_TYPE_STRING: {
2099 dbus_message_iter_get_basic(iter, &s);
2102 if (streq(name, "Id"))
2104 else if (streq(name, "LoadState"))
2106 else if (streq(name, "ActiveState"))
2107 i->active_state = s;
2108 else if (streq(name, "SubState"))
2110 else if (streq(name, "Description"))
2112 else if (streq(name, "FragmentPath"))
2114 #ifdef HAVE_SYSV_COMPAT
2115 else if (streq(name, "SysVPath")) {
2120 else if (streq(name, "DefaultControlGroup"))
2121 i->default_control_group = s;
2122 else if (streq(name, "StatusText"))
2124 else if (streq(name, "SysFSPath"))
2126 else if (streq(name, "Where"))
2128 else if (streq(name, "What"))
2130 else if (streq(name, "Following"))
2137 case DBUS_TYPE_BOOLEAN: {
2140 dbus_message_iter_get_basic(iter, &b);
2142 if (streq(name, "Accept"))
2144 else if (streq(name, "NeedDaemonReload"))
2145 i->need_daemon_reload = b;
2146 else if (streq(name, "ConditionResult"))
2147 i->condition_result = b;
2152 case DBUS_TYPE_UINT32: {
2155 dbus_message_iter_get_basic(iter, &u);
2157 if (streq(name, "MainPID")) {
2159 i->main_pid = (pid_t) u;
2162 } else if (streq(name, "ControlPID"))
2163 i->control_pid = (pid_t) u;
2164 else if (streq(name, "ExecMainPID")) {
2166 i->main_pid = (pid_t) u;
2167 } else if (streq(name, "NAccepted"))
2169 else if (streq(name, "NConnections"))
2170 i->n_connections = u;
2175 case DBUS_TYPE_INT32: {
2178 dbus_message_iter_get_basic(iter, &j);
2180 if (streq(name, "ExecMainCode"))
2181 i->exit_code = (int) j;
2182 else if (streq(name, "ExecMainStatus"))
2183 i->exit_status = (int) j;
2188 case DBUS_TYPE_UINT64: {
2191 dbus_message_iter_get_basic(iter, &u);
2193 if (streq(name, "ExecMainStartTimestamp"))
2194 i->start_timestamp = (usec_t) u;
2195 else if (streq(name, "ExecMainExitTimestamp"))
2196 i->exit_timestamp = (usec_t) u;
2197 else if (streq(name, "ActiveEnterTimestamp"))
2198 i->active_enter_timestamp = (usec_t) u;
2199 else if (streq(name, "InactiveEnterTimestamp"))
2200 i->inactive_enter_timestamp = (usec_t) u;
2201 else if (streq(name, "InactiveExitTimestamp"))
2202 i->inactive_exit_timestamp = (usec_t) u;
2203 else if (streq(name, "ActiveExitTimestamp"))
2204 i->active_exit_timestamp = (usec_t) u;
2205 else if (streq(name, "ConditionTimestamp"))
2206 i->condition_timestamp = (usec_t) u;
2211 case DBUS_TYPE_ARRAY: {
2213 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT &&
2214 startswith(name, "Exec")) {
2215 DBusMessageIter sub;
2217 dbus_message_iter_recurse(iter, &sub);
2218 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2219 ExecStatusInfo *info;
2222 if (!(info = new0(ExecStatusInfo, 1)))
2225 if (!(info->name = strdup(name))) {
2230 if ((r = exec_status_info_deserialize(&sub, info)) < 0) {
2235 LIST_PREPEND(ExecStatusInfo, exec, i->exec, info);
2237 dbus_message_iter_next(&sub);
2248 static int print_property(const char *name, DBusMessageIter *iter) {
2252 /* This is a low-level property printer, see
2253 * print_status_info() for the nicer output */
2255 if (arg_property && !strv_find(arg_property, name))
2258 switch (dbus_message_iter_get_arg_type(iter)) {
2260 case DBUS_TYPE_STRING: {
2262 dbus_message_iter_get_basic(iter, &s);
2264 if (arg_all || s[0])
2265 printf("%s=%s\n", name, s);
2270 case DBUS_TYPE_BOOLEAN: {
2272 dbus_message_iter_get_basic(iter, &b);
2273 printf("%s=%s\n", name, yes_no(b));
2278 case DBUS_TYPE_UINT64: {
2280 dbus_message_iter_get_basic(iter, &u);
2282 /* Yes, heuristics! But we can change this check
2283 * should it turn out to not be sufficient */
2285 if (endswith(name, "Timestamp")) {
2286 char timestamp[FORMAT_TIMESTAMP_MAX], *t;
2288 if ((t = format_timestamp(timestamp, sizeof(timestamp), u)) || arg_all)
2289 printf("%s=%s\n", name, strempty(t));
2290 } else if (strstr(name, "USec")) {
2291 char timespan[FORMAT_TIMESPAN_MAX];
2293 printf("%s=%s\n", name, format_timespan(timespan, sizeof(timespan), u));
2295 printf("%s=%llu\n", name, (unsigned long long) u);
2300 case DBUS_TYPE_UINT32: {
2302 dbus_message_iter_get_basic(iter, &u);
2304 if (strstr(name, "UMask") || strstr(name, "Mode"))
2305 printf("%s=%04o\n", name, u);
2307 printf("%s=%u\n", name, (unsigned) u);
2312 case DBUS_TYPE_INT32: {
2314 dbus_message_iter_get_basic(iter, &i);
2316 printf("%s=%i\n", name, (int) i);
2320 case DBUS_TYPE_DOUBLE: {
2322 dbus_message_iter_get_basic(iter, &d);
2324 printf("%s=%g\n", name, d);
2328 case DBUS_TYPE_STRUCT: {
2329 DBusMessageIter sub;
2330 dbus_message_iter_recurse(iter, &sub);
2332 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "Job")) {
2335 dbus_message_iter_get_basic(&sub, &u);
2338 printf("%s=%u\n", name, (unsigned) u);
2340 printf("%s=\n", name);
2343 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Unit")) {
2346 dbus_message_iter_get_basic(&sub, &s);
2348 if (arg_all || s[0])
2349 printf("%s=%s\n", name, s);
2357 case DBUS_TYPE_ARRAY:
2359 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
2360 DBusMessageIter sub;
2363 dbus_message_iter_recurse(iter, &sub);
2365 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2366 printf("%s=", name);
2368 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2371 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
2372 dbus_message_iter_get_basic(&sub, &s);
2373 printf("%s%s", space ? " " : "", s);
2376 dbus_message_iter_next(&sub);
2384 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_BYTE) {
2385 DBusMessageIter sub;
2387 dbus_message_iter_recurse(iter, &sub);
2389 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2390 printf("%s=", name);
2392 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2395 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_BYTE);
2396 dbus_message_iter_get_basic(&sub, &u);
2399 dbus_message_iter_next(&sub);
2407 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "EnvironmentFiles")) {
2408 DBusMessageIter sub, sub2;
2410 dbus_message_iter_recurse(iter, &sub);
2411 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2415 dbus_message_iter_recurse(&sub, &sub2);
2417 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) >= 0 &&
2418 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_BOOLEAN, &ignore, false) >= 0)
2419 printf("EnvironmentFile=%s (ignore=%s)\n", path, yes_no(ignore));
2421 dbus_message_iter_next(&sub);
2426 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Paths")) {
2427 DBusMessageIter sub, sub2;
2429 dbus_message_iter_recurse(iter, &sub);
2430 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2431 const char *type, *path;
2433 dbus_message_iter_recurse(&sub, &sub2);
2435 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
2436 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0)
2437 printf("%s=%s\n", type, path);
2439 dbus_message_iter_next(&sub);
2444 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Timers")) {
2445 DBusMessageIter sub, sub2;
2447 dbus_message_iter_recurse(iter, &sub);
2448 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2450 uint64_t value, next_elapse;
2452 dbus_message_iter_recurse(&sub, &sub2);
2454 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &base, true) >= 0 &&
2455 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &value, true) >= 0 &&
2456 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &next_elapse, false) >= 0) {
2457 char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX];
2459 printf("%s={ value=%s ; next_elapse=%s }\n",
2461 format_timespan(timespan1, sizeof(timespan1), value),
2462 format_timespan(timespan2, sizeof(timespan2), next_elapse));
2465 dbus_message_iter_next(&sub);
2470 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && startswith(name, "Exec")) {
2471 DBusMessageIter sub;
2473 dbus_message_iter_recurse(iter, &sub);
2474 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2475 ExecStatusInfo info;
2478 if (exec_status_info_deserialize(&sub, &info) >= 0) {
2479 char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
2482 t = strv_join(info.argv, " ");
2484 printf("%s={ path=%s ; argv[]=%s ; ignore=%s ; start_time=[%s] ; stop_time=[%s] ; pid=%u ; code=%s ; status=%i%s%s }\n",
2488 yes_no(info.ignore),
2489 strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)),
2490 strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)),
2491 (unsigned) info. pid,
2492 sigchld_code_to_string(info.code),
2494 info.code == CLD_EXITED ? "" : "/",
2495 strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status)));
2501 strv_free(info.argv);
2503 dbus_message_iter_next(&sub);
2513 printf("%s=[unprintable]\n", name);
2518 static int show_one(const char *verb, DBusConnection *bus, const char *path, bool show_properties, bool *new_line) {
2519 DBusMessage *m = NULL, *reply = NULL;
2520 const char *interface = "";
2523 DBusMessageIter iter, sub, sub2, sub3;
2524 UnitStatusInfo info;
2532 dbus_error_init(&error);
2534 if (!(m = dbus_message_new_method_call(
2535 "org.freedesktop.systemd1",
2537 "org.freedesktop.DBus.Properties",
2539 log_error("Could not allocate message.");
2544 if (!dbus_message_append_args(m,
2545 DBUS_TYPE_STRING, &interface,
2546 DBUS_TYPE_INVALID)) {
2547 log_error("Could not append arguments to message.");
2552 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2553 log_error("Failed to issue method call: %s", bus_error_message(&error));
2558 if (!dbus_message_iter_init(reply, &iter) ||
2559 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
2560 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
2561 log_error("Failed to parse reply.");
2566 dbus_message_iter_recurse(&iter, &sub);
2573 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2576 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
2577 log_error("Failed to parse reply.");
2582 dbus_message_iter_recurse(&sub, &sub2);
2584 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
2585 log_error("Failed to parse reply.");
2590 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
2591 log_error("Failed to parse reply.");
2596 dbus_message_iter_recurse(&sub2, &sub3);
2598 if (show_properties)
2599 r = print_property(name, &sub3);
2601 r = status_property(name, &sub3, &info);
2604 log_error("Failed to parse reply.");
2609 dbus_message_iter_next(&sub);
2614 if (!show_properties)
2615 print_status_info(&info);
2617 if (!streq_ptr(info.active_state, "active") &&
2618 !streq_ptr(info.active_state, "reloading") &&
2619 streq(verb, "status"))
2620 /* According to LSB: "program not running" */
2623 while ((p = info.exec)) {
2624 LIST_REMOVE(ExecStatusInfo, exec, info.exec, p);
2625 exec_status_info_free(p);
2630 dbus_message_unref(m);
2633 dbus_message_unref(reply);
2635 dbus_error_free(&error);
2640 static int show(DBusConnection *bus, char **args, unsigned n) {
2641 DBusMessage *m = NULL, *reply = NULL;
2645 bool show_properties, new_line = false;
2650 dbus_error_init(&error);
2652 show_properties = !streq(args[0], "status");
2654 if (show_properties)
2657 if (show_properties && n <= 1) {
2658 /* If not argument is specified inspect the manager
2661 ret = show_one(args[0], bus, "/org/freedesktop/systemd1", show_properties, &new_line);
2665 for (i = 1; i < n; i++) {
2666 const char *path = NULL;
2669 if (safe_atou32(args[i], &id) < 0) {
2671 /* Interpret as unit name */
2673 if (!(m = dbus_message_new_method_call(
2674 "org.freedesktop.systemd1",
2675 "/org/freedesktop/systemd1",
2676 "org.freedesktop.systemd1.Manager",
2678 log_error("Could not allocate message.");
2683 if (!dbus_message_append_args(m,
2684 DBUS_TYPE_STRING, &args[i],
2685 DBUS_TYPE_INVALID)) {
2686 log_error("Could not append arguments to message.");
2691 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2693 if (!dbus_error_has_name(&error, DBUS_ERROR_ACCESS_DENIED)) {
2694 log_error("Failed to issue method call: %s", bus_error_message(&error));
2699 dbus_error_free(&error);
2701 dbus_message_unref(m);
2702 if (!(m = dbus_message_new_method_call(
2703 "org.freedesktop.systemd1",
2704 "/org/freedesktop/systemd1",
2705 "org.freedesktop.systemd1.Manager",
2707 log_error("Could not allocate message.");
2712 if (!dbus_message_append_args(m,
2713 DBUS_TYPE_STRING, &args[i],
2714 DBUS_TYPE_INVALID)) {
2715 log_error("Could not append arguments to message.");
2720 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2721 log_error("Failed to issue method call: %s", bus_error_message(&error));
2723 if (dbus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT))
2724 ret = 4; /* According to LSB: "program or service status is unknown" */
2731 } else if (show_properties) {
2733 /* Interpret as job id */
2735 if (!(m = dbus_message_new_method_call(
2736 "org.freedesktop.systemd1",
2737 "/org/freedesktop/systemd1",
2738 "org.freedesktop.systemd1.Manager",
2740 log_error("Could not allocate message.");
2745 if (!dbus_message_append_args(m,
2746 DBUS_TYPE_UINT32, &id,
2747 DBUS_TYPE_INVALID)) {
2748 log_error("Could not append arguments to message.");
2753 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2754 log_error("Failed to issue method call: %s", bus_error_message(&error));
2760 /* Interpret as PID */
2762 if (!(m = dbus_message_new_method_call(
2763 "org.freedesktop.systemd1",
2764 "/org/freedesktop/systemd1",
2765 "org.freedesktop.systemd1.Manager",
2767 log_error("Could not allocate message.");
2772 if (!dbus_message_append_args(m,
2773 DBUS_TYPE_UINT32, &id,
2774 DBUS_TYPE_INVALID)) {
2775 log_error("Could not append arguments to message.");
2780 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2781 log_error("Failed to issue method call: %s", bus_error_message(&error));
2787 if (!dbus_message_get_args(reply, &error,
2788 DBUS_TYPE_OBJECT_PATH, &path,
2789 DBUS_TYPE_INVALID)) {
2790 log_error("Failed to parse reply: %s", bus_error_message(&error));
2795 if ((r = show_one(args[0], bus, path, show_properties, &new_line)) != 0)
2798 dbus_message_unref(m);
2799 dbus_message_unref(reply);
2805 dbus_message_unref(m);
2808 dbus_message_unref(reply);
2810 dbus_error_free(&error);
2815 static DBusHandlerResult monitor_filter(DBusConnection *connection, DBusMessage *message, void *data) {
2817 DBusMessage *m = NULL, *reply = NULL;
2822 dbus_error_init(&error);
2824 log_debug("Got D-Bus request: %s.%s() on %s",
2825 dbus_message_get_interface(message),
2826 dbus_message_get_member(message),
2827 dbus_message_get_path(message));
2829 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
2830 log_error("Warning! D-Bus connection terminated.");
2831 dbus_connection_close(connection);
2833 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitNew") ||
2834 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitRemoved")) {
2835 const char *id, *path;
2837 if (!dbus_message_get_args(message, &error,
2838 DBUS_TYPE_STRING, &id,
2839 DBUS_TYPE_OBJECT_PATH, &path,
2841 log_error("Failed to parse message: %s", bus_error_message(&error));
2842 else if (streq(dbus_message_get_member(message), "UnitNew"))
2843 printf("Unit %s added.\n", id);
2845 printf("Unit %s removed.\n", id);
2847 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobNew")) {
2851 if (!dbus_message_get_args(message, &error,
2852 DBUS_TYPE_UINT32, &id,
2853 DBUS_TYPE_OBJECT_PATH, &path,
2855 log_error("Failed to parse message: %s", bus_error_message(&error));
2857 printf("Job %u added.\n", id);
2860 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
2862 const char *path, *result;
2864 if (!dbus_message_get_args(message, &error,
2865 DBUS_TYPE_UINT32, &id,
2866 DBUS_TYPE_OBJECT_PATH, &path,
2867 DBUS_TYPE_STRING, &result,
2869 log_error("Failed to parse message: %s", bus_error_message(&error));
2871 printf("Job %u removed (result=%s).\n", id, result);
2874 } else if (dbus_message_is_signal(message, "org.freedesktop.DBus.Properties", "PropertiesChanged")) {
2876 const char *path, *interface, *property = "Id";
2877 DBusMessageIter iter, sub;
2879 path = dbus_message_get_path(message);
2881 if (!dbus_message_get_args(message, &error,
2882 DBUS_TYPE_STRING, &interface,
2883 DBUS_TYPE_INVALID)) {
2884 log_error("Failed to parse message: %s", bus_error_message(&error));
2888 if (!streq(interface, "org.freedesktop.systemd1.Job") &&
2889 !streq(interface, "org.freedesktop.systemd1.Unit"))
2892 if (!(m = dbus_message_new_method_call(
2893 "org.freedesktop.systemd1",
2895 "org.freedesktop.DBus.Properties",
2897 log_error("Could not allocate message.");
2901 if (!dbus_message_append_args(m,
2902 DBUS_TYPE_STRING, &interface,
2903 DBUS_TYPE_STRING, &property,
2904 DBUS_TYPE_INVALID)) {
2905 log_error("Could not append arguments to message.");
2909 if (!(reply = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) {
2910 log_error("Failed to issue method call: %s", bus_error_message(&error));
2914 if (!dbus_message_iter_init(reply, &iter) ||
2915 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2916 log_error("Failed to parse reply.");
2920 dbus_message_iter_recurse(&iter, &sub);
2922 if (streq(interface, "org.freedesktop.systemd1.Unit")) {
2925 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2926 log_error("Failed to parse reply.");
2930 dbus_message_iter_get_basic(&sub, &id);
2931 printf("Unit %s changed.\n", id);
2935 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT32) {
2936 log_error("Failed to parse reply.");
2940 dbus_message_iter_get_basic(&sub, &id);
2941 printf("Job %u changed.\n", id);
2947 dbus_message_unref(m);
2950 dbus_message_unref(reply);
2952 dbus_error_free(&error);
2953 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
2957 dbus_message_unref(m);
2960 dbus_message_unref(reply);
2962 dbus_error_free(&error);
2963 return DBUS_HANDLER_RESULT_NEED_MEMORY;
2966 static int monitor(DBusConnection *bus, char **args, unsigned n) {
2967 DBusMessage *m = NULL, *reply = NULL;
2971 dbus_error_init(&error);
2974 dbus_bus_add_match(bus,
2976 "sender='org.freedesktop.systemd1',"
2977 "interface='org.freedesktop.systemd1.Manager',"
2978 "path='/org/freedesktop/systemd1'",
2981 if (dbus_error_is_set(&error)) {
2982 log_error("Failed to add match: %s", bus_error_message(&error));
2987 dbus_bus_add_match(bus,
2989 "sender='org.freedesktop.systemd1',"
2990 "interface='org.freedesktop.DBus.Properties',"
2991 "member='PropertiesChanged'",
2994 if (dbus_error_is_set(&error)) {
2995 log_error("Failed to add match: %s", bus_error_message(&error));
3001 if (!dbus_connection_add_filter(bus, monitor_filter, NULL, NULL)) {
3002 log_error("Failed to add filter.");
3007 if (!(m = dbus_message_new_method_call(
3008 "org.freedesktop.systemd1",
3009 "/org/freedesktop/systemd1",
3010 "org.freedesktop.systemd1.Manager",
3012 log_error("Could not allocate message.");
3017 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3018 log_error("Failed to issue method call: %s", bus_error_message(&error));
3023 while (dbus_connection_read_write_dispatch(bus, -1))
3030 /* This is slightly dirty, since we don't undo the filter or the matches. */
3033 dbus_message_unref(m);
3036 dbus_message_unref(reply);
3038 dbus_error_free(&error);
3043 static int dump(DBusConnection *bus, char **args, unsigned n) {
3044 DBusMessage *m = NULL, *reply = NULL;
3049 dbus_error_init(&error);
3053 if (!(m = dbus_message_new_method_call(
3054 "org.freedesktop.systemd1",
3055 "/org/freedesktop/systemd1",
3056 "org.freedesktop.systemd1.Manager",
3058 log_error("Could not allocate message.");
3062 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3063 log_error("Failed to issue method call: %s", bus_error_message(&error));
3068 if (!dbus_message_get_args(reply, &error,
3069 DBUS_TYPE_STRING, &text,
3070 DBUS_TYPE_INVALID)) {
3071 log_error("Failed to parse reply: %s", bus_error_message(&error));
3076 fputs(text, stdout);
3082 dbus_message_unref(m);
3085 dbus_message_unref(reply);
3087 dbus_error_free(&error);
3092 static int snapshot(DBusConnection *bus, char **args, unsigned n) {
3093 DBusMessage *m = NULL, *reply = NULL;
3096 const char *name = "", *path, *id;
3097 dbus_bool_t cleanup = FALSE;
3098 DBusMessageIter iter, sub;
3100 *interface = "org.freedesktop.systemd1.Unit",
3103 dbus_error_init(&error);
3105 if (!(m = dbus_message_new_method_call(
3106 "org.freedesktop.systemd1",
3107 "/org/freedesktop/systemd1",
3108 "org.freedesktop.systemd1.Manager",
3109 "CreateSnapshot"))) {
3110 log_error("Could not allocate message.");
3117 if (!dbus_message_append_args(m,
3118 DBUS_TYPE_STRING, &name,
3119 DBUS_TYPE_BOOLEAN, &cleanup,
3120 DBUS_TYPE_INVALID)) {
3121 log_error("Could not append arguments to message.");
3126 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3127 log_error("Failed to issue method call: %s", bus_error_message(&error));
3132 if (!dbus_message_get_args(reply, &error,
3133 DBUS_TYPE_OBJECT_PATH, &path,
3134 DBUS_TYPE_INVALID)) {
3135 log_error("Failed to parse reply: %s", bus_error_message(&error));
3140 dbus_message_unref(m);
3141 if (!(m = dbus_message_new_method_call(
3142 "org.freedesktop.systemd1",