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 int arg_signal = SIGTERM;
84 static usec_t arg_when = 0;
103 ACTION_CANCEL_SHUTDOWN,
105 } arg_action = ACTION_SYSTEMCTL;
111 static enum transport {
115 } arg_transport = TRANSPORT_NORMAL;
116 static const char *arg_host = NULL;
118 static bool private_bus = false;
120 static pid_t pager_pid = 0;
121 static pid_t agent_pid = 0;
123 static int daemon_reload(DBusConnection *bus, char **args, unsigned n);
124 static void pager_open(void);
126 static bool on_tty(void) {
129 /* Note that this is invoked relatively early, before we start
130 * the pager. That means the value we return reflects whether
131 * we originally were started on a tty, not if we currently
132 * are. But this is intended, since we want colour and so on
133 * when run in our own pager. */
135 if (_unlikely_(t < 0))
136 t = isatty(STDOUT_FILENO) > 0;
141 static void spawn_ask_password_agent(void) {
147 /* We check STDIN here, not STDOUT, since this is about input,
149 if (!isatty(STDIN_FILENO))
152 if (!arg_ask_password)
160 /* Spawns a temporary TTY agent, making sure it goes away when
163 if ((agent_pid = fork()) < 0)
166 if (agent_pid == 0) {
170 bool stdout_is_tty, stderr_is_tty;
172 /* Make sure the agent goes away when the parent dies */
173 if (prctl(PR_SET_PDEATHSIG, SIGTERM) < 0)
176 /* Check whether our parent died before we were able
177 * to set the death signal */
178 if (getppid() != parent)
181 /* Don't leak fds to the agent */
182 close_all_fds(NULL, 0);
184 stdout_is_tty = isatty(STDOUT_FILENO);
185 stderr_is_tty = isatty(STDERR_FILENO);
187 if (!stdout_is_tty || !stderr_is_tty) {
188 /* Detach from stdout/stderr. and reopen
189 * /dev/tty for them. This is important to
190 * ensure that when systemctl is started via
191 * popen() or a similar call that expects to
192 * read EOF we actually do generate EOF and
193 * not delay this indefinitely by because we
194 * keep an unused copy of stdin around. */
195 if ((fd = open("/dev/tty", O_WRONLY)) < 0) {
196 log_error("Failed to open /dev/tty: %m");
201 dup2(fd, STDOUT_FILENO);
204 dup2(fd, STDERR_FILENO);
210 execl(SYSTEMD_TTY_ASK_PASSWORD_AGENT_BINARY_PATH, SYSTEMD_TTY_ASK_PASSWORD_AGENT_BINARY_PATH, "--watch", NULL);
212 log_error("Unable to execute agent: %m");
217 static const char *ansi_highlight(bool b) {
222 return b ? ANSI_HIGHLIGHT_ON : ANSI_HIGHLIGHT_OFF;
225 static const char *ansi_highlight_green(bool b) {
230 return b ? ANSI_HIGHLIGHT_GREEN_ON : ANSI_HIGHLIGHT_OFF;
233 static bool error_is_no_service(const DBusError *error) {
236 if (!dbus_error_is_set(error))
239 if (dbus_error_has_name(error, DBUS_ERROR_NAME_HAS_NO_OWNER))
242 if (dbus_error_has_name(error, DBUS_ERROR_SERVICE_UNKNOWN))
245 return startswith(error->name, "org.freedesktop.DBus.Error.Spawn.");
248 static int translate_bus_error_to_exit_status(int r, const DBusError *error) {
251 if (!dbus_error_is_set(error))
254 if (dbus_error_has_name(error, DBUS_ERROR_ACCESS_DENIED) ||
255 dbus_error_has_name(error, BUS_ERROR_ONLY_BY_DEPENDENCY) ||
256 dbus_error_has_name(error, BUS_ERROR_NO_ISOLATION) ||
257 dbus_error_has_name(error, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE))
258 return EXIT_NOPERMISSION;
260 if (dbus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT))
261 return EXIT_NOTINSTALLED;
263 if (dbus_error_has_name(error, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE) ||
264 dbus_error_has_name(error, BUS_ERROR_NOT_SUPPORTED))
265 return EXIT_NOTIMPLEMENTED;
267 if (dbus_error_has_name(error, BUS_ERROR_LOAD_FAILED))
268 return EXIT_NOTCONFIGURED;
276 static int bus_iter_get_basic_and_next(DBusMessageIter *iter, int type, void *data, bool next) {
281 if (dbus_message_iter_get_arg_type(iter) != type)
284 dbus_message_iter_get_basic(iter, data);
286 if (!dbus_message_iter_next(iter) != !next)
292 static void warn_wall(enum action action) {
293 static const char *table[_ACTION_MAX] = {
294 [ACTION_HALT] = "The system is going down for system halt NOW!",
295 [ACTION_REBOOT] = "The system is going down for reboot NOW!",
296 [ACTION_POWEROFF] = "The system is going down for power-off NOW!",
297 [ACTION_KEXEC] = "The system is going down for kexec reboot NOW!",
298 [ACTION_RESCUE] = "The system is going down to rescue mode NOW!",
299 [ACTION_EMERGENCY] = "The system is going down to emergency mode NOW!"
308 if (!(p = strv_join(arg_wall, " "))) {
309 log_error("Failed to join strings.");
325 utmp_wall(table[action], NULL);
330 const char *description;
331 const char *load_state;
332 const char *active_state;
333 const char *sub_state;
334 const char *following;
335 const char *unit_path;
337 const char *job_type;
338 const char *job_path;
341 static int compare_unit_info(const void *a, const void *b) {
343 const struct unit_info *u = a, *v = b;
345 d1 = strrchr(u->id, '.');
346 d2 = strrchr(v->id, '.');
351 if ((r = strcasecmp(d1, d2)) != 0)
355 return strcasecmp(u->id, v->id);
358 static bool output_show_unit(const struct unit_info *u) {
362 return streq(u->active_state, "failed");
364 return (!arg_type || ((dot = strrchr(u->id, '.')) &&
365 streq(dot+1, arg_type))) &&
366 (arg_all || !(streq(u->active_state, "inactive") || u->following[0]) || u->job_id > 0);
369 static void output_units_list(const struct unit_info *unit_infos, unsigned c) {
370 unsigned active_len, sub_len, job_len, n_shown = 0;
371 const struct unit_info *u;
373 active_len = sizeof("ACTIVE")-1;
374 sub_len = sizeof("SUB")-1;
375 job_len = sizeof("JOB")-1;
377 for (u = unit_infos; u < unit_infos + c; u++) {
378 if (!output_show_unit(u))
381 active_len = MAX(active_len, strlen(u->active_state));
382 sub_len = MAX(sub_len, strlen(u->sub_state));
384 job_len = MAX(job_len, strlen(u->job_type));
388 printf("%-25s %-6s %-*s %-*s %-*s", "UNIT", "LOAD",
389 active_len, "ACTIVE", sub_len, "SUB", job_len, "JOB");
390 if (columns() >= 80+12 || arg_full || !arg_no_pager)
391 printf(" %s\n", "DESCRIPTION");
396 for (u = unit_infos; u < unit_infos + c; u++) {
399 const char *on_loaded, *off_loaded;
400 const char *on_active, *off_active;
402 if (!output_show_unit(u))
407 if (!streq(u->load_state, "loaded") &&
408 !streq(u->load_state, "banned")) {
409 on_loaded = ansi_highlight(true);
410 off_loaded = ansi_highlight(false);
412 on_loaded = off_loaded = "";
414 if (streq(u->active_state, "failed")) {
415 on_active = ansi_highlight(true);
416 off_active = ansi_highlight(false);
418 on_active = off_active = "";
420 e = arg_full ? NULL : ellipsize(u->id, 25, 33);
422 printf("%-25s %s%-6s%s %s%-*s %-*s%s%n",
424 on_loaded, u->load_state, off_loaded,
425 on_active, active_len, u->active_state,
426 sub_len, u->sub_state, off_active,
431 a -= strlen(on_loaded) + strlen(off_loaded);
432 a -= strlen(on_active) + strlen(off_active);
435 printf(" %-*s", job_len, u->job_type);
439 if (a + b + 1 < columns()) {
441 printf(" %-*s", job_len, "");
443 if (arg_full || !arg_no_pager)
444 printf(" %s", u->description);
446 printf(" %.*s", columns() - a - b - 1, u->description);
453 printf("\nLOAD = Reflects whether the unit definition was properly loaded.\n"
454 "ACTIVE = The high-level unit activation state, i.e. generalization of SUB.\n"
455 "SUB = The low-level unit activation state, values depend on unit type.\n"
456 "JOB = Pending job for the unit.\n");
459 printf("\n%u units listed.\n", n_shown);
461 printf("\n%u units listed. Pass --all to see inactive units, too.\n", n_shown);
465 static int list_units(DBusConnection *bus, char **args, unsigned n) {
466 DBusMessage *m = NULL, *reply = NULL;
469 DBusMessageIter iter, sub, sub2;
470 unsigned c = 0, n_units = 0;
471 struct unit_info *unit_infos = NULL;
473 dbus_error_init(&error);
479 if (!(m = dbus_message_new_method_call(
480 "org.freedesktop.systemd1",
481 "/org/freedesktop/systemd1",
482 "org.freedesktop.systemd1.Manager",
484 log_error("Could not allocate message.");
488 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
489 log_error("Failed to issue method call: %s", bus_error_message(&error));
494 if (!dbus_message_iter_init(reply, &iter) ||
495 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
496 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
497 log_error("Failed to parse reply.");
502 dbus_message_iter_recurse(&iter, &sub);
504 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
507 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
508 log_error("Failed to parse reply.");
516 n_units = MAX(2*c, 16);
517 w = realloc(unit_infos, sizeof(struct unit_info) * n_units);
520 log_error("Failed to allocate unit array.");
530 dbus_message_iter_recurse(&sub, &sub2);
532 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->id, true) < 0 ||
533 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->description, true) < 0 ||
534 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->load_state, true) < 0 ||
535 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->active_state, true) < 0 ||
536 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->sub_state, true) < 0 ||
537 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->following, true) < 0 ||
538 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &u->unit_path, true) < 0 ||
539 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &u->job_id, true) < 0 ||
540 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->job_type, true) < 0 ||
541 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &u->job_path, false) < 0) {
542 log_error("Failed to parse reply.");
547 dbus_message_iter_next(&sub);
552 qsort(unit_infos, c, sizeof(struct unit_info), compare_unit_info);
553 output_units_list(unit_infos, c);
560 dbus_message_unref(m);
563 dbus_message_unref(reply);
567 dbus_error_free(&error);
572 static int dot_one_property(const char *name, const char *prop, DBusMessageIter *iter) {
573 static const char * const colors[] = {
574 "Requires", "[color=\"black\"]",
575 "RequiresOverridable", "[color=\"black\"]",
576 "Requisite", "[color=\"darkblue\"]",
577 "RequisiteOverridable", "[color=\"darkblue\"]",
578 "Wants", "[color=\"darkgrey\"]",
579 "Conflicts", "[color=\"red\"]",
580 "ConflictedBy", "[color=\"red\"]",
581 "After", "[color=\"green\"]"
584 const char *c = NULL;
591 for (i = 0; i < ELEMENTSOF(colors); i += 2)
592 if (streq(colors[i], prop)) {
600 if (arg_dot != DOT_ALL)
601 if ((arg_dot == DOT_ORDER) != streq(prop, "After"))
604 switch (dbus_message_iter_get_arg_type(iter)) {
606 case DBUS_TYPE_ARRAY:
608 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
611 dbus_message_iter_recurse(iter, &sub);
613 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
616 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
617 dbus_message_iter_get_basic(&sub, &s);
618 printf("\t\"%s\"->\"%s\" %s;\n", name, s, c);
620 dbus_message_iter_next(&sub);
630 static int dot_one(DBusConnection *bus, const char *name, const char *path) {
631 DBusMessage *m = NULL, *reply = NULL;
632 const char *interface = "org.freedesktop.systemd1.Unit";
635 DBusMessageIter iter, sub, sub2, sub3;
640 dbus_error_init(&error);
642 if (!(m = dbus_message_new_method_call(
643 "org.freedesktop.systemd1",
645 "org.freedesktop.DBus.Properties",
647 log_error("Could not allocate message.");
652 if (!dbus_message_append_args(m,
653 DBUS_TYPE_STRING, &interface,
654 DBUS_TYPE_INVALID)) {
655 log_error("Could not append arguments to message.");
660 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
661 log_error("Failed to issue method call: %s", bus_error_message(&error));
666 if (!dbus_message_iter_init(reply, &iter) ||
667 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
668 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
669 log_error("Failed to parse reply.");
674 dbus_message_iter_recurse(&iter, &sub);
676 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
679 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
680 log_error("Failed to parse reply.");
685 dbus_message_iter_recurse(&sub, &sub2);
687 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &prop, true) < 0) {
688 log_error("Failed to parse reply.");
693 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
694 log_error("Failed to parse reply.");
699 dbus_message_iter_recurse(&sub2, &sub3);
701 if (dot_one_property(name, prop, &sub3)) {
702 log_error("Failed to parse reply.");
707 dbus_message_iter_next(&sub);
714 dbus_message_unref(m);
717 dbus_message_unref(reply);
719 dbus_error_free(&error);
724 static int dot(DBusConnection *bus, char **args, unsigned n) {
725 DBusMessage *m = NULL, *reply = NULL;
728 DBusMessageIter iter, sub, sub2;
730 dbus_error_init(&error);
734 if (!(m = dbus_message_new_method_call(
735 "org.freedesktop.systemd1",
736 "/org/freedesktop/systemd1",
737 "org.freedesktop.systemd1.Manager",
739 log_error("Could not allocate message.");
743 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
744 log_error("Failed to issue method call: %s", bus_error_message(&error));
749 if (!dbus_message_iter_init(reply, &iter) ||
750 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
751 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
752 log_error("Failed to parse reply.");
757 printf("digraph systemd {\n");
759 dbus_message_iter_recurse(&iter, &sub);
760 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
761 const char *id, *description, *load_state, *active_state, *sub_state, *following, *unit_path;
763 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
764 log_error("Failed to parse reply.");
769 dbus_message_iter_recurse(&sub, &sub2);
771 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
772 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &description, true) < 0 ||
773 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &load_state, true) < 0 ||
774 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &active_state, true) < 0 ||
775 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &sub_state, true) < 0 ||
776 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &following, true) < 0 ||
777 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, true) < 0) {
778 log_error("Failed to parse reply.");
783 if ((r = dot_one(bus, id, unit_path)) < 0)
786 /* printf("\t\"%s\";\n", id); */
787 dbus_message_iter_next(&sub);
792 log_info(" Color legend: black = Requires\n"
793 " dark blue = Requisite\n"
794 " dark grey = Wants\n"
798 if (isatty(fileno(stdout)))
799 log_notice("-- You probably want to process this output with graphviz' dot tool.\n"
800 "-- Try a shell pipeline like 'systemctl dot | dot -Tsvg > systemd.svg'!\n");
806 dbus_message_unref(m);
809 dbus_message_unref(reply);
811 dbus_error_free(&error);
816 static int list_jobs(DBusConnection *bus, char **args, unsigned n) {
817 DBusMessage *m = NULL, *reply = NULL;
820 DBusMessageIter iter, sub, sub2;
823 dbus_error_init(&error);
829 if (!(m = dbus_message_new_method_call(
830 "org.freedesktop.systemd1",
831 "/org/freedesktop/systemd1",
832 "org.freedesktop.systemd1.Manager",
834 log_error("Could not allocate message.");
838 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
839 log_error("Failed to issue method call: %s", bus_error_message(&error));
844 if (!dbus_message_iter_init(reply, &iter) ||
845 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
846 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
847 log_error("Failed to parse reply.");
852 dbus_message_iter_recurse(&iter, &sub);
854 if (isatty(STDOUT_FILENO))
855 printf("%4s %-25s %-15s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
857 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
858 const char *name, *type, *state, *job_path, *unit_path;
862 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
863 log_error("Failed to parse reply.");
868 dbus_message_iter_recurse(&sub, &sub2);
870 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &id, true) < 0 ||
871 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0 ||
872 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
873 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, true) < 0 ||
874 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, true) < 0 ||
875 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, false) < 0) {
876 log_error("Failed to parse reply.");
881 e = arg_full ? NULL : ellipsize(name, 25, 33);
882 printf("%4u %-25s %-15s %-7s\n", id, e ? e : name, type, state);
887 dbus_message_iter_next(&sub);
890 if (isatty(STDOUT_FILENO))
891 printf("\n%u jobs listed.\n", k);
897 dbus_message_unref(m);
900 dbus_message_unref(reply);
902 dbus_error_free(&error);
907 static int load_unit(DBusConnection *bus, char **args, unsigned n) {
908 DBusMessage *m = NULL, *reply = NULL;
913 dbus_error_init(&error);
918 for (i = 1; i < n; i++) {
920 if (!(m = dbus_message_new_method_call(
921 "org.freedesktop.systemd1",
922 "/org/freedesktop/systemd1",
923 "org.freedesktop.systemd1.Manager",
925 log_error("Could not allocate message.");
930 if (!dbus_message_append_args(m,
931 DBUS_TYPE_STRING, &args[i],
932 DBUS_TYPE_INVALID)) {
933 log_error("Could not append arguments to message.");
938 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
939 log_error("Failed to issue method call: %s", bus_error_message(&error));
944 dbus_message_unref(m);
945 dbus_message_unref(reply);
954 dbus_message_unref(m);
957 dbus_message_unref(reply);
959 dbus_error_free(&error);
964 static int cancel_job(DBusConnection *bus, char **args, unsigned n) {
965 DBusMessage *m = NULL, *reply = NULL;
970 dbus_error_init(&error);
976 return daemon_reload(bus, args, n);
978 for (i = 1; i < n; i++) {
982 if (!(m = dbus_message_new_method_call(
983 "org.freedesktop.systemd1",
984 "/org/freedesktop/systemd1",
985 "org.freedesktop.systemd1.Manager",
987 log_error("Could not allocate message.");
992 if ((r = safe_atou(args[i], &id)) < 0) {
993 log_error("Failed to parse job id: %s", strerror(-r));
997 assert_cc(sizeof(uint32_t) == sizeof(id));
998 if (!dbus_message_append_args(m,
999 DBUS_TYPE_UINT32, &id,
1000 DBUS_TYPE_INVALID)) {
1001 log_error("Could not append arguments to message.");
1006 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1007 log_error("Failed to issue method call: %s", bus_error_message(&error));
1012 if (!dbus_message_get_args(reply, &error,
1013 DBUS_TYPE_OBJECT_PATH, &path,
1014 DBUS_TYPE_INVALID)) {
1015 log_error("Failed to parse reply: %s", bus_error_message(&error));
1020 dbus_message_unref(m);
1021 if (!(m = dbus_message_new_method_call(
1022 "org.freedesktop.systemd1",
1024 "org.freedesktop.systemd1.Job",
1026 log_error("Could not allocate message.");
1031 dbus_message_unref(reply);
1032 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1033 log_error("Failed to issue method call: %s", bus_error_message(&error));
1038 dbus_message_unref(m);
1039 dbus_message_unref(reply);
1047 dbus_message_unref(m);
1050 dbus_message_unref(reply);
1052 dbus_error_free(&error);
1057 static bool need_daemon_reload(DBusConnection *bus, const char *unit) {
1058 DBusMessage *m = NULL, *reply = NULL;
1059 dbus_bool_t b = FALSE;
1060 DBusMessageIter iter, sub;
1062 *interface = "org.freedesktop.systemd1.Unit",
1063 *property = "NeedDaemonReload",
1066 /* We ignore all errors here, since this is used to show a warning only */
1068 if (!(m = dbus_message_new_method_call(
1069 "org.freedesktop.systemd1",
1070 "/org/freedesktop/systemd1",
1071 "org.freedesktop.systemd1.Manager",
1075 if (!dbus_message_append_args(m,
1076 DBUS_TYPE_STRING, &unit,
1080 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL)))
1083 if (!dbus_message_get_args(reply, NULL,
1084 DBUS_TYPE_OBJECT_PATH, &path,
1088 dbus_message_unref(m);
1089 if (!(m = dbus_message_new_method_call(
1090 "org.freedesktop.systemd1",
1092 "org.freedesktop.DBus.Properties",
1096 if (!dbus_message_append_args(m,
1097 DBUS_TYPE_STRING, &interface,
1098 DBUS_TYPE_STRING, &property,
1099 DBUS_TYPE_INVALID)) {
1103 dbus_message_unref(reply);
1104 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL)))
1107 if (!dbus_message_iter_init(reply, &iter) ||
1108 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
1111 dbus_message_iter_recurse(&iter, &sub);
1113 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_BOOLEAN)
1116 dbus_message_iter_get_basic(&sub, &b);
1120 dbus_message_unref(m);
1123 dbus_message_unref(reply);
1128 typedef struct WaitData {
1133 static DBusHandlerResult wait_filter(DBusConnection *connection, DBusMessage *message, void *data) {
1141 dbus_error_init(&error);
1143 log_debug("Got D-Bus request: %s.%s() on %s",
1144 dbus_message_get_interface(message),
1145 dbus_message_get_member(message),
1146 dbus_message_get_path(message));
1148 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
1149 log_error("Warning! D-Bus connection terminated.");
1150 dbus_connection_close(connection);
1152 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
1154 const char *path, *result;
1155 dbus_bool_t success = true;
1157 if (dbus_message_get_args(message, &error,
1158 DBUS_TYPE_UINT32, &id,
1159 DBUS_TYPE_OBJECT_PATH, &path,
1160 DBUS_TYPE_STRING, &result,
1161 DBUS_TYPE_INVALID)) {
1164 if ((p = set_remove(d->set, (char*) path)))
1168 d->result = strdup(result);
1173 dbus_error_free(&error);
1175 if (dbus_message_get_args(message, &error,
1176 DBUS_TYPE_UINT32, &id,
1177 DBUS_TYPE_OBJECT_PATH, &path,
1178 DBUS_TYPE_BOOLEAN, &success,
1179 DBUS_TYPE_INVALID)) {
1182 /* Compatibility with older systemd versions <
1183 * 19 during upgrades. This should be dropped
1186 if ((p = set_remove(d->set, (char*) path)))
1190 d->result = strdup("failed");
1196 log_error("Failed to parse message: %s", bus_error_message(&error));
1200 dbus_error_free(&error);
1201 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1204 static int enable_wait_for_jobs(DBusConnection *bus) {
1212 dbus_error_init(&error);
1213 dbus_bus_add_match(bus,
1215 "sender='org.freedesktop.systemd1',"
1216 "interface='org.freedesktop.systemd1.Manager',"
1217 "member='JobRemoved',"
1218 "path='/org/freedesktop/systemd1'",
1221 if (dbus_error_is_set(&error)) {
1222 log_error("Failed to add match: %s", bus_error_message(&error));
1223 dbus_error_free(&error);
1227 /* This is slightly dirty, since we don't undo the match registrations. */
1231 static int wait_for_jobs(DBusConnection *bus, Set *s) {
1241 if (!dbus_connection_add_filter(bus, wait_filter, &d, NULL)) {
1242 log_error("Failed to add filter.");
1247 while (!set_isempty(s) &&
1248 dbus_connection_read_write_dispatch(bus, -1))
1251 if (!arg_quiet && d.result) {
1252 if (streq(d.result, "timeout"))
1253 log_error("Job timed out.");
1254 else if (streq(d.result, "canceled"))
1255 log_error("Job canceled.");
1256 else if (streq(d.result, "dependency"))
1257 log_error("A dependency job failed. See system logs for details.");
1258 else if (!streq(d.result, "done") && !streq(d.result, "skipped"))
1259 log_error("Job failed. See system logs and 'systemctl status' for details.");
1262 if (streq_ptr(d.result, "timeout"))
1264 else if (streq_ptr(d.result, "canceled"))
1266 else if (!streq_ptr(d.result, "done") && !streq_ptr(d.result, "skipped"))
1274 /* This is slightly dirty, since we don't undo the filter registration. */
1279 static int start_unit_one(
1280 DBusConnection *bus,
1287 DBusMessage *m = NULL, *reply = NULL;
1296 assert(arg_no_block || s);
1298 if (!(m = dbus_message_new_method_call(
1299 "org.freedesktop.systemd1",
1300 "/org/freedesktop/systemd1",
1301 "org.freedesktop.systemd1.Manager",
1303 log_error("Could not allocate message.");
1308 if (!dbus_message_append_args(m,
1309 DBUS_TYPE_STRING, &name,
1310 DBUS_TYPE_STRING, &mode,
1311 DBUS_TYPE_INVALID)) {
1312 log_error("Could not append arguments to message.");
1317 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, error))) {
1319 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(error)) {
1320 /* There's always a fallback possible for
1321 * legacy actions. */
1326 log_error("Failed to issue method call: %s", bus_error_message(error));
1331 if (!dbus_message_get_args(reply, error,
1332 DBUS_TYPE_OBJECT_PATH, &path,
1333 DBUS_TYPE_INVALID)) {
1334 log_error("Failed to parse reply: %s", bus_error_message(error));
1339 if (need_daemon_reload(bus, name))
1340 log_warning("Unit file of created job changed on disk, 'systemctl %s daemon-reload' recommended.",
1341 arg_user ? "--user" : "--system");
1343 if (!arg_no_block) {
1346 if (!(p = strdup(path))) {
1347 log_error("Failed to duplicate path.");
1352 if ((r = set_put(s, p)) < 0) {
1354 log_error("Failed to add path to set.");
1363 dbus_message_unref(m);
1366 dbus_message_unref(reply);
1371 static enum action verb_to_action(const char *verb) {
1372 if (streq(verb, "halt"))
1374 else if (streq(verb, "poweroff"))
1375 return ACTION_POWEROFF;
1376 else if (streq(verb, "reboot"))
1377 return ACTION_REBOOT;
1378 else if (streq(verb, "kexec"))
1379 return ACTION_KEXEC;
1380 else if (streq(verb, "rescue"))
1381 return ACTION_RESCUE;
1382 else if (streq(verb, "emergency"))
1383 return ACTION_EMERGENCY;
1384 else if (streq(verb, "default"))
1385 return ACTION_DEFAULT;
1386 else if (streq(verb, "exit"))
1389 return ACTION_INVALID;
1392 static int start_unit(DBusConnection *bus, char **args, unsigned n) {
1394 static const char * const table[_ACTION_MAX] = {
1395 [ACTION_HALT] = SPECIAL_HALT_TARGET,
1396 [ACTION_POWEROFF] = SPECIAL_POWEROFF_TARGET,
1397 [ACTION_REBOOT] = SPECIAL_REBOOT_TARGET,
1398 [ACTION_KEXEC] = SPECIAL_KEXEC_TARGET,
1399 [ACTION_RUNLEVEL2] = SPECIAL_RUNLEVEL2_TARGET,
1400 [ACTION_RUNLEVEL3] = SPECIAL_RUNLEVEL3_TARGET,
1401 [ACTION_RUNLEVEL4] = SPECIAL_RUNLEVEL4_TARGET,
1402 [ACTION_RUNLEVEL5] = SPECIAL_RUNLEVEL5_TARGET,
1403 [ACTION_RESCUE] = SPECIAL_RESCUE_TARGET,
1404 [ACTION_EMERGENCY] = SPECIAL_EMERGENCY_TARGET,
1405 [ACTION_DEFAULT] = SPECIAL_DEFAULT_TARGET,
1406 [ACTION_EXIT] = SPECIAL_EXIT_TARGET
1411 const char *method, *mode, *one_name;
1415 dbus_error_init(&error);
1419 spawn_ask_password_agent();
1421 if (arg_action == ACTION_SYSTEMCTL) {
1423 streq(args[0], "stop") ||
1424 streq(args[0], "condstop") ? "StopUnit" :
1425 streq(args[0], "reload") ? "ReloadUnit" :
1426 streq(args[0], "restart") ? "RestartUnit" :
1428 streq(args[0], "try-restart") ||
1429 streq(args[0], "condrestart") ? "TryRestartUnit" :
1431 streq(args[0], "reload-or-restart") ? "ReloadOrRestartUnit" :
1433 streq(args[0], "reload-or-try-restart") ||
1434 streq(args[0], "condreload") ||
1436 streq(args[0], "force-reload") ? "ReloadOrTryRestartUnit" :
1440 (streq(args[0], "isolate") ||
1441 streq(args[0], "rescue") ||
1442 streq(args[0], "emergency")) ? "isolate" : arg_job_mode;
1444 one_name = table[verb_to_action(args[0])];
1447 assert(arg_action < ELEMENTSOF(table));
1448 assert(table[arg_action]);
1450 method = "StartUnit";
1452 mode = (arg_action == ACTION_EMERGENCY ||
1453 arg_action == ACTION_RESCUE ||
1454 arg_action == ACTION_RUNLEVEL2 ||
1455 arg_action == ACTION_RUNLEVEL3 ||
1456 arg_action == ACTION_RUNLEVEL4 ||
1457 arg_action == ACTION_RUNLEVEL5) ? "isolate" : "replace";
1459 one_name = table[arg_action];
1462 if (!arg_no_block) {
1463 if ((ret = enable_wait_for_jobs(bus)) < 0) {
1464 log_error("Could not watch jobs: %s", strerror(-ret));
1468 if (!(s = set_new(string_hash_func, string_compare_func))) {
1469 log_error("Failed to allocate set.");
1476 if ((ret = start_unit_one(bus, method, one_name, mode, &error, s)) <= 0)
1479 for (i = 1; i < n; i++)
1480 if ((r = start_unit_one(bus, method, args[i], mode, &error, s)) != 0) {
1481 ret = translate_bus_error_to_exit_status(r, &error);
1482 dbus_error_free(&error);
1487 if ((r = wait_for_jobs(bus, s)) < 0) {
1496 dbus_error_free(&error);
1501 static int start_special(DBusConnection *bus, char **args, unsigned n) {
1508 (streq(args[0], "halt") ||
1509 streq(args[0], "poweroff") ||
1510 streq(args[0], "reboot") ||
1511 streq(args[0], "kexec") ||
1512 streq(args[0], "exit")))
1513 return daemon_reload(bus, args, n);
1515 r = start_unit(bus, args, n);
1518 warn_wall(verb_to_action(args[0]));
1523 static int check_unit(DBusConnection *bus, char **args, unsigned n) {
1524 DBusMessage *m = NULL, *reply = NULL;
1526 *interface = "org.freedesktop.systemd1.Unit",
1527 *property = "ActiveState";
1528 int r = 3; /* According to LSB: "program is not running" */
1535 dbus_error_init(&error);
1537 for (i = 1; i < n; i++) {
1538 const char *path = NULL;
1540 DBusMessageIter iter, sub;
1542 if (!(m = dbus_message_new_method_call(
1543 "org.freedesktop.systemd1",
1544 "/org/freedesktop/systemd1",
1545 "org.freedesktop.systemd1.Manager",
1547 log_error("Could not allocate message.");
1552 if (!dbus_message_append_args(m,
1553 DBUS_TYPE_STRING, &args[i],
1554 DBUS_TYPE_INVALID)) {
1555 log_error("Could not append arguments to message.");
1560 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1562 /* Hmm, cannot figure out anything about this unit... */
1566 dbus_error_free(&error);
1567 dbus_message_unref(m);
1571 if (!dbus_message_get_args(reply, &error,
1572 DBUS_TYPE_OBJECT_PATH, &path,
1573 DBUS_TYPE_INVALID)) {
1574 log_error("Failed to parse reply: %s", bus_error_message(&error));
1579 dbus_message_unref(m);
1580 if (!(m = dbus_message_new_method_call(
1581 "org.freedesktop.systemd1",
1583 "org.freedesktop.DBus.Properties",
1585 log_error("Could not allocate message.");
1590 if (!dbus_message_append_args(m,
1591 DBUS_TYPE_STRING, &interface,
1592 DBUS_TYPE_STRING, &property,
1593 DBUS_TYPE_INVALID)) {
1594 log_error("Could not append arguments to message.");
1599 dbus_message_unref(reply);
1600 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1601 log_error("Failed to issue method call: %s", bus_error_message(&error));
1606 if (!dbus_message_iter_init(reply, &iter) ||
1607 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1608 log_error("Failed to parse reply.");
1613 dbus_message_iter_recurse(&iter, &sub);
1615 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1616 log_error("Failed to parse reply.");
1621 dbus_message_iter_get_basic(&sub, &state);
1626 if (streq(state, "active") || streq(state, "reloading"))
1629 dbus_message_unref(m);
1630 dbus_message_unref(reply);
1636 dbus_message_unref(m);
1639 dbus_message_unref(reply);
1641 dbus_error_free(&error);
1646 static int kill_unit(DBusConnection *bus, char **args, unsigned n) {
1647 DBusMessage *m = NULL, *reply = NULL;
1655 dbus_error_init(&error);
1658 arg_kill_who = "all";
1661 arg_kill_mode = streq(arg_kill_who, "all") ? "control-group" : "process";
1663 for (i = 1; i < n; i++) {
1665 if (!(m = dbus_message_new_method_call(
1666 "org.freedesktop.systemd1",
1667 "/org/freedesktop/systemd1",
1668 "org.freedesktop.systemd1.Manager",
1670 log_error("Could not allocate message.");
1675 if (!dbus_message_append_args(m,
1676 DBUS_TYPE_STRING, &args[i],
1677 DBUS_TYPE_STRING, &arg_kill_who,
1678 DBUS_TYPE_STRING, &arg_kill_mode,
1679 DBUS_TYPE_INT32, &arg_signal,
1680 DBUS_TYPE_INVALID)) {
1681 log_error("Could not append arguments to message.");
1686 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1687 log_error("Failed to issue method call: %s", bus_error_message(&error));
1688 dbus_error_free(&error);
1692 dbus_message_unref(m);
1695 dbus_message_unref(reply);
1701 dbus_message_unref(m);
1704 dbus_message_unref(reply);
1706 dbus_error_free(&error);
1711 typedef struct ExecStatusInfo {
1719 usec_t start_timestamp;
1720 usec_t exit_timestamp;
1725 LIST_FIELDS(struct ExecStatusInfo, exec);
1728 static void exec_status_info_free(ExecStatusInfo *i) {
1737 static int exec_status_info_deserialize(DBusMessageIter *sub, ExecStatusInfo *i) {
1738 uint64_t start_timestamp, exit_timestamp, start_timestamp_monotonic, exit_timestamp_monotonic;
1739 DBusMessageIter sub2, sub3;
1743 int32_t code, status;
1749 if (dbus_message_iter_get_arg_type(sub) != DBUS_TYPE_STRUCT)
1752 dbus_message_iter_recurse(sub, &sub2);
1754 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0)
1757 if (!(i->path = strdup(path)))
1760 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_ARRAY ||
1761 dbus_message_iter_get_element_type(&sub2) != DBUS_TYPE_STRING)
1765 dbus_message_iter_recurse(&sub2, &sub3);
1766 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1767 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1768 dbus_message_iter_next(&sub3);
1773 if (!(i->argv = new0(char*, n+1)))
1777 dbus_message_iter_recurse(&sub2, &sub3);
1778 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1781 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1782 dbus_message_iter_get_basic(&sub3, &s);
1783 dbus_message_iter_next(&sub3);
1785 if (!(i->argv[n++] = strdup(s)))
1789 if (!dbus_message_iter_next(&sub2) ||
1790 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_BOOLEAN, &ignore, true) < 0 ||
1791 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_timestamp, true) < 0 ||
1792 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_timestamp_monotonic, true) < 0 ||
1793 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_timestamp, true) < 0 ||
1794 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_timestamp_monotonic, true) < 0 ||
1795 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, true) < 0 ||
1796 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &code, true) < 0 ||
1797 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &status, false) < 0)
1801 i->start_timestamp = (usec_t) start_timestamp;
1802 i->exit_timestamp = (usec_t) exit_timestamp;
1803 i->pid = (pid_t) pid;
1810 typedef struct UnitStatusInfo {
1812 const char *load_state;
1813 const char *active_state;
1814 const char *sub_state;
1816 const char *description;
1817 const char *following;
1820 const char *default_control_group;
1822 usec_t inactive_exit_timestamp;
1823 usec_t active_enter_timestamp;
1824 usec_t active_exit_timestamp;
1825 usec_t inactive_enter_timestamp;
1827 bool need_daemon_reload;
1832 const char *status_text;
1834 #ifdef HAVE_SYSV_COMPAT
1838 usec_t start_timestamp;
1839 usec_t exit_timestamp;
1841 int exit_code, exit_status;
1843 usec_t condition_timestamp;
1844 bool condition_result;
1847 unsigned n_accepted;
1848 unsigned n_connections;
1852 const char *sysfs_path;
1854 /* Mount, Automount */
1860 LIST_HEAD(ExecStatusInfo, exec);
1863 static void print_status_info(UnitStatusInfo *i) {
1865 const char *on, *off, *ss;
1867 char since1[FORMAT_TIMESTAMP_PRETTY_MAX], *s1;
1868 char since2[FORMAT_TIMESTAMP_MAX], *s2;
1872 /* This shows pretty information about a unit. See
1873 * print_property() for a low-level property printer */
1875 printf("%s", strna(i->id));
1877 if (i->description && !streq_ptr(i->id, i->description))
1878 printf(" - %s", i->description);
1883 printf("\t Follow: unit currently follows state of %s\n", i->following);
1885 if (streq_ptr(i->load_state, "failed") ||
1886 streq_ptr(i->load_state, "banned")) {
1887 on = ansi_highlight(true);
1888 off = ansi_highlight(false);
1893 printf("\t Loaded: %s%s%s (%s)\n", on, strna(i->load_state), off, i->path);
1895 printf("\t Loaded: %s%s%s\n", on, strna(i->load_state), off);
1897 ss = streq_ptr(i->active_state, i->sub_state) ? NULL : i->sub_state;
1899 if (streq_ptr(i->active_state, "failed")) {
1900 on = ansi_highlight(true);
1901 off = ansi_highlight(false);
1902 } else if (streq_ptr(i->active_state, "active") || streq_ptr(i->active_state, "reloading")) {
1903 on = ansi_highlight_green(true);
1904 off = ansi_highlight_green(false);
1909 printf("\t Active: %s%s (%s)%s",
1911 strna(i->active_state),
1915 printf("\t Active: %s%s%s",
1917 strna(i->active_state),
1920 timestamp = (streq_ptr(i->active_state, "active") ||
1921 streq_ptr(i->active_state, "reloading")) ? i->active_enter_timestamp :
1922 (streq_ptr(i->active_state, "inactive") ||
1923 streq_ptr(i->active_state, "failed")) ? i->inactive_enter_timestamp :
1924 streq_ptr(i->active_state, "activating") ? i->inactive_exit_timestamp :
1925 i->active_exit_timestamp;
1927 s1 = format_timestamp_pretty(since1, sizeof(since1), timestamp);
1928 s2 = format_timestamp(since2, sizeof(since2), timestamp);
1931 printf(" since %s; %s\n", s2, s1);
1933 printf(" since %s\n", s2);
1937 if (!i->condition_result && i->condition_timestamp > 0) {
1938 s1 = format_timestamp_pretty(since1, sizeof(since1), i->condition_timestamp);
1939 s2 = format_timestamp(since2, sizeof(since2), i->condition_timestamp);
1942 printf("\t start condition failed at %s; %s\n", s2, s1);
1944 printf("\t start condition failed at %s\n", s2);
1948 printf("\t Device: %s\n", i->sysfs_path);
1950 printf("\t Where: %s\n", i->where);
1952 printf("\t What: %s\n", i->what);
1955 printf("\tAccepted: %u; Connected: %u\n", i->n_accepted, i->n_connections);
1957 LIST_FOREACH(exec, p, i->exec) {
1961 /* Only show exited processes here */
1965 t = strv_join(p->argv, " ");
1966 printf("\t Process: %u %s=%s ", p->pid, p->name, strna(t));
1969 #ifdef HAVE_SYSV_COMPAT
1971 good = is_clean_exit_lsb(p->code, p->status);
1974 good = is_clean_exit(p->code, p->status);
1977 on = ansi_highlight(true);
1978 off = ansi_highlight(false);
1982 printf("%s(code=%s, ", on, sigchld_code_to_string(p->code));
1984 if (p->code == CLD_EXITED) {
1987 printf("status=%i", p->status);
1989 #ifdef HAVE_SYSV_COMPAT
1990 if ((c = exit_status_to_string(p->status, i->is_sysv ? EXIT_STATUS_LSB : EXIT_STATUS_SYSTEMD)))
1992 if ((c = exit_status_to_string(p->status, EXIT_STATUS_SYSTEMD)))
1997 printf("signal=%s", signal_to_string(p->status));
1999 printf(")%s\n", off);
2003 if (i->main_pid == p->pid &&
2004 i->start_timestamp == p->start_timestamp &&
2005 i->exit_timestamp == p->start_timestamp)
2006 /* Let's not show this twice */
2009 if (p->pid == i->control_pid)
2013 if (i->main_pid > 0 || i->control_pid > 0) {
2016 if (i->main_pid > 0) {
2017 printf("Main PID: %u", (unsigned) i->main_pid);
2021 get_process_name(i->main_pid, &t);
2026 } else if (i->exit_code > 0) {
2027 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
2029 if (i->exit_code == CLD_EXITED) {
2032 printf("status=%i", i->exit_status);
2034 #ifdef HAVE_SYSV_COMPAT
2035 if ((c = exit_status_to_string(i->exit_status, i->is_sysv ? EXIT_STATUS_LSB : EXIT_STATUS_SYSTEMD)))
2037 if ((c = exit_status_to_string(i->exit_status, EXIT_STATUS_SYSTEMD)))
2042 printf("signal=%s", signal_to_string(i->exit_status));
2047 if (i->main_pid > 0 && i->control_pid > 0)
2050 if (i->control_pid > 0) {
2053 printf(" Control: %u", (unsigned) i->control_pid);
2055 get_process_name(i->control_pid, &t);
2066 printf("\t Status: \"%s\"\n", i->status_text);
2068 if (i->default_control_group) {
2071 printf("\t CGroup: %s\n", i->default_control_group);
2073 if (arg_transport != TRANSPORT_SSH) {
2074 if ((c = columns()) > 18)
2079 show_cgroup_by_path(i->default_control_group, "\t\t ", c);
2083 if (i->need_daemon_reload)
2084 printf("\n%sWarning:%s Unit file changed on disk, 'systemctl %s daemon-reload' recommended.\n",
2085 ansi_highlight(true),
2086 ansi_highlight(false),
2087 arg_user ? "--user" : "--system");
2090 static int status_property(const char *name, DBusMessageIter *iter, UnitStatusInfo *i) {
2092 switch (dbus_message_iter_get_arg_type(iter)) {
2094 case DBUS_TYPE_STRING: {
2097 dbus_message_iter_get_basic(iter, &s);
2100 if (streq(name, "Id"))
2102 else if (streq(name, "LoadState"))
2104 else if (streq(name, "ActiveState"))
2105 i->active_state = s;
2106 else if (streq(name, "SubState"))
2108 else if (streq(name, "Description"))
2110 else if (streq(name, "FragmentPath"))
2112 #ifdef HAVE_SYSV_COMPAT
2113 else if (streq(name, "SysVPath")) {
2118 else if (streq(name, "DefaultControlGroup"))
2119 i->default_control_group = s;
2120 else if (streq(name, "StatusText"))
2122 else if (streq(name, "SysFSPath"))
2124 else if (streq(name, "Where"))
2126 else if (streq(name, "What"))
2128 else if (streq(name, "Following"))
2135 case DBUS_TYPE_BOOLEAN: {
2138 dbus_message_iter_get_basic(iter, &b);
2140 if (streq(name, "Accept"))
2142 else if (streq(name, "NeedDaemonReload"))
2143 i->need_daemon_reload = b;
2144 else if (streq(name, "ConditionResult"))
2145 i->condition_result = b;
2150 case DBUS_TYPE_UINT32: {
2153 dbus_message_iter_get_basic(iter, &u);
2155 if (streq(name, "MainPID")) {
2157 i->main_pid = (pid_t) u;
2160 } else if (streq(name, "ControlPID"))
2161 i->control_pid = (pid_t) u;
2162 else if (streq(name, "ExecMainPID")) {
2164 i->main_pid = (pid_t) u;
2165 } else if (streq(name, "NAccepted"))
2167 else if (streq(name, "NConnections"))
2168 i->n_connections = u;
2173 case DBUS_TYPE_INT32: {
2176 dbus_message_iter_get_basic(iter, &j);
2178 if (streq(name, "ExecMainCode"))
2179 i->exit_code = (int) j;
2180 else if (streq(name, "ExecMainStatus"))
2181 i->exit_status = (int) j;
2186 case DBUS_TYPE_UINT64: {
2189 dbus_message_iter_get_basic(iter, &u);
2191 if (streq(name, "ExecMainStartTimestamp"))
2192 i->start_timestamp = (usec_t) u;
2193 else if (streq(name, "ExecMainExitTimestamp"))
2194 i->exit_timestamp = (usec_t) u;
2195 else if (streq(name, "ActiveEnterTimestamp"))
2196 i->active_enter_timestamp = (usec_t) u;
2197 else if (streq(name, "InactiveEnterTimestamp"))
2198 i->inactive_enter_timestamp = (usec_t) u;
2199 else if (streq(name, "InactiveExitTimestamp"))
2200 i->inactive_exit_timestamp = (usec_t) u;
2201 else if (streq(name, "ActiveExitTimestamp"))
2202 i->active_exit_timestamp = (usec_t) u;
2203 else if (streq(name, "ConditionTimestamp"))
2204 i->condition_timestamp = (usec_t) u;
2209 case DBUS_TYPE_ARRAY: {
2211 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT &&
2212 startswith(name, "Exec")) {
2213 DBusMessageIter sub;
2215 dbus_message_iter_recurse(iter, &sub);
2216 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2217 ExecStatusInfo *info;
2220 if (!(info = new0(ExecStatusInfo, 1)))
2223 if (!(info->name = strdup(name))) {
2228 if ((r = exec_status_info_deserialize(&sub, info)) < 0) {
2233 LIST_PREPEND(ExecStatusInfo, exec, i->exec, info);
2235 dbus_message_iter_next(&sub);
2246 static int print_property(const char *name, DBusMessageIter *iter) {
2250 /* This is a low-level property printer, see
2251 * print_status_info() for the nicer output */
2253 if (arg_property && !strv_find(arg_property, name))
2256 switch (dbus_message_iter_get_arg_type(iter)) {
2258 case DBUS_TYPE_STRING: {
2260 dbus_message_iter_get_basic(iter, &s);
2262 if (arg_all || s[0])
2263 printf("%s=%s\n", name, s);
2268 case DBUS_TYPE_BOOLEAN: {
2270 dbus_message_iter_get_basic(iter, &b);
2271 printf("%s=%s\n", name, yes_no(b));
2276 case DBUS_TYPE_UINT64: {
2278 dbus_message_iter_get_basic(iter, &u);
2280 /* Yes, heuristics! But we can change this check
2281 * should it turn out to not be sufficient */
2283 if (endswith(name, "Timestamp")) {
2284 char timestamp[FORMAT_TIMESTAMP_MAX], *t;
2286 if ((t = format_timestamp(timestamp, sizeof(timestamp), u)) || arg_all)
2287 printf("%s=%s\n", name, strempty(t));
2288 } else if (strstr(name, "USec")) {
2289 char timespan[FORMAT_TIMESPAN_MAX];
2291 printf("%s=%s\n", name, format_timespan(timespan, sizeof(timespan), u));
2293 printf("%s=%llu\n", name, (unsigned long long) u);
2298 case DBUS_TYPE_UINT32: {
2300 dbus_message_iter_get_basic(iter, &u);
2302 if (strstr(name, "UMask") || strstr(name, "Mode"))
2303 printf("%s=%04o\n", name, u);
2305 printf("%s=%u\n", name, (unsigned) u);
2310 case DBUS_TYPE_INT32: {
2312 dbus_message_iter_get_basic(iter, &i);
2314 printf("%s=%i\n", name, (int) i);
2318 case DBUS_TYPE_DOUBLE: {
2320 dbus_message_iter_get_basic(iter, &d);
2322 printf("%s=%g\n", name, d);
2326 case DBUS_TYPE_STRUCT: {
2327 DBusMessageIter sub;
2328 dbus_message_iter_recurse(iter, &sub);
2330 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "Job")) {
2333 dbus_message_iter_get_basic(&sub, &u);
2336 printf("%s=%u\n", name, (unsigned) u);
2338 printf("%s=\n", name);
2341 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Unit")) {
2344 dbus_message_iter_get_basic(&sub, &s);
2346 if (arg_all || s[0])
2347 printf("%s=%s\n", name, s);
2355 case DBUS_TYPE_ARRAY:
2357 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
2358 DBusMessageIter sub;
2361 dbus_message_iter_recurse(iter, &sub);
2363 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2364 printf("%s=", name);
2366 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2369 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
2370 dbus_message_iter_get_basic(&sub, &s);
2371 printf("%s%s", space ? " " : "", s);
2374 dbus_message_iter_next(&sub);
2382 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_BYTE) {
2383 DBusMessageIter sub;
2385 dbus_message_iter_recurse(iter, &sub);
2387 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2388 printf("%s=", name);
2390 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2393 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_BYTE);
2394 dbus_message_iter_get_basic(&sub, &u);
2397 dbus_message_iter_next(&sub);
2405 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "EnvironmentFiles")) {
2406 DBusMessageIter sub, sub2;
2408 dbus_message_iter_recurse(iter, &sub);
2409 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2413 dbus_message_iter_recurse(&sub, &sub2);
2415 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) >= 0 &&
2416 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_BOOLEAN, &ignore, false) >= 0)
2417 printf("EnvironmentFile=%s (ignore=%s)\n", path, yes_no(ignore));
2419 dbus_message_iter_next(&sub);
2424 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Paths")) {
2425 DBusMessageIter sub, sub2;
2427 dbus_message_iter_recurse(iter, &sub);
2428 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2429 const char *type, *path;
2431 dbus_message_iter_recurse(&sub, &sub2);
2433 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
2434 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0)
2435 printf("%s=%s\n", type, path);
2437 dbus_message_iter_next(&sub);
2442 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Timers")) {
2443 DBusMessageIter sub, sub2;
2445 dbus_message_iter_recurse(iter, &sub);
2446 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2448 uint64_t value, next_elapse;
2450 dbus_message_iter_recurse(&sub, &sub2);
2452 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &base, true) >= 0 &&
2453 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &value, true) >= 0 &&
2454 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &next_elapse, false) >= 0) {
2455 char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX];
2457 printf("%s={ value=%s ; next_elapse=%s }\n",
2459 format_timespan(timespan1, sizeof(timespan1), value),
2460 format_timespan(timespan2, sizeof(timespan2), next_elapse));
2463 dbus_message_iter_next(&sub);
2468 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && startswith(name, "Exec")) {
2469 DBusMessageIter sub;
2471 dbus_message_iter_recurse(iter, &sub);
2472 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2473 ExecStatusInfo info;
2476 if (exec_status_info_deserialize(&sub, &info) >= 0) {
2477 char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
2480 t = strv_join(info.argv, " ");
2482 printf("%s={ path=%s ; argv[]=%s ; ignore=%s ; start_time=[%s] ; stop_time=[%s] ; pid=%u ; code=%s ; status=%i%s%s }\n",
2486 yes_no(info.ignore),
2487 strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)),
2488 strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)),
2489 (unsigned) info. pid,
2490 sigchld_code_to_string(info.code),
2492 info.code == CLD_EXITED ? "" : "/",
2493 strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status)));
2499 strv_free(info.argv);
2501 dbus_message_iter_next(&sub);
2511 printf("%s=[unprintable]\n", name);
2516 static int show_one(const char *verb, DBusConnection *bus, const char *path, bool show_properties, bool *new_line) {
2517 DBusMessage *m = NULL, *reply = NULL;
2518 const char *interface = "";
2521 DBusMessageIter iter, sub, sub2, sub3;
2522 UnitStatusInfo info;
2530 dbus_error_init(&error);
2532 if (!(m = dbus_message_new_method_call(
2533 "org.freedesktop.systemd1",
2535 "org.freedesktop.DBus.Properties",
2537 log_error("Could not allocate message.");
2542 if (!dbus_message_append_args(m,
2543 DBUS_TYPE_STRING, &interface,
2544 DBUS_TYPE_INVALID)) {
2545 log_error("Could not append arguments to message.");
2550 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2551 log_error("Failed to issue method call: %s", bus_error_message(&error));
2556 if (!dbus_message_iter_init(reply, &iter) ||
2557 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
2558 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
2559 log_error("Failed to parse reply.");
2564 dbus_message_iter_recurse(&iter, &sub);
2571 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2574 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
2575 log_error("Failed to parse reply.");
2580 dbus_message_iter_recurse(&sub, &sub2);
2582 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
2583 log_error("Failed to parse reply.");
2588 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
2589 log_error("Failed to parse reply.");
2594 dbus_message_iter_recurse(&sub2, &sub3);
2596 if (show_properties)
2597 r = print_property(name, &sub3);
2599 r = status_property(name, &sub3, &info);
2602 log_error("Failed to parse reply.");
2607 dbus_message_iter_next(&sub);
2612 if (!show_properties)
2613 print_status_info(&info);
2615 if (!streq_ptr(info.active_state, "active") &&
2616 !streq_ptr(info.active_state, "reloading") &&
2617 streq(verb, "status"))
2618 /* According to LSB: "program not running" */
2621 while ((p = info.exec)) {
2622 LIST_REMOVE(ExecStatusInfo, exec, info.exec, p);
2623 exec_status_info_free(p);
2628 dbus_message_unref(m);
2631 dbus_message_unref(reply);
2633 dbus_error_free(&error);
2638 static int show(DBusConnection *bus, char **args, unsigned n) {
2639 DBusMessage *m = NULL, *reply = NULL;
2643 bool show_properties, new_line = false;
2648 dbus_error_init(&error);
2650 show_properties = !streq(args[0], "status");
2652 if (show_properties)
2655 if (show_properties && n <= 1) {
2656 /* If not argument is specified inspect the manager
2659 ret = show_one(args[0], bus, "/org/freedesktop/systemd1", show_properties, &new_line);
2663 for (i = 1; i < n; i++) {
2664 const char *path = NULL;
2667 if (safe_atou32(args[i], &id) < 0) {
2669 /* Interpret as unit name */
2671 if (!(m = dbus_message_new_method_call(
2672 "org.freedesktop.systemd1",
2673 "/org/freedesktop/systemd1",
2674 "org.freedesktop.systemd1.Manager",
2676 log_error("Could not allocate message.");
2681 if (!dbus_message_append_args(m,
2682 DBUS_TYPE_STRING, &args[i],
2683 DBUS_TYPE_INVALID)) {
2684 log_error("Could not append arguments to message.");
2689 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2691 if (!dbus_error_has_name(&error, DBUS_ERROR_ACCESS_DENIED)) {
2692 log_error("Failed to issue method call: %s", bus_error_message(&error));
2697 dbus_error_free(&error);
2699 dbus_message_unref(m);
2700 if (!(m = dbus_message_new_method_call(
2701 "org.freedesktop.systemd1",
2702 "/org/freedesktop/systemd1",
2703 "org.freedesktop.systemd1.Manager",
2705 log_error("Could not allocate message.");
2710 if (!dbus_message_append_args(m,
2711 DBUS_TYPE_STRING, &args[i],
2712 DBUS_TYPE_INVALID)) {
2713 log_error("Could not append arguments to message.");
2718 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2719 log_error("Failed to issue method call: %s", bus_error_message(&error));
2721 if (dbus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT))
2722 ret = 4; /* According to LSB: "program or service status is unknown" */
2729 } else if (show_properties) {
2731 /* Interpret as job id */
2733 if (!(m = dbus_message_new_method_call(
2734 "org.freedesktop.systemd1",
2735 "/org/freedesktop/systemd1",
2736 "org.freedesktop.systemd1.Manager",
2738 log_error("Could not allocate message.");
2743 if (!dbus_message_append_args(m,
2744 DBUS_TYPE_UINT32, &id,
2745 DBUS_TYPE_INVALID)) {
2746 log_error("Could not append arguments to message.");
2751 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2752 log_error("Failed to issue method call: %s", bus_error_message(&error));
2758 /* Interpret as PID */
2760 if (!(m = dbus_message_new_method_call(
2761 "org.freedesktop.systemd1",
2762 "/org/freedesktop/systemd1",
2763 "org.freedesktop.systemd1.Manager",
2765 log_error("Could not allocate message.");
2770 if (!dbus_message_append_args(m,
2771 DBUS_TYPE_UINT32, &id,
2772 DBUS_TYPE_INVALID)) {
2773 log_error("Could not append arguments to message.");
2778 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2779 log_error("Failed to issue method call: %s", bus_error_message(&error));
2785 if (!dbus_message_get_args(reply, &error,
2786 DBUS_TYPE_OBJECT_PATH, &path,
2787 DBUS_TYPE_INVALID)) {
2788 log_error("Failed to parse reply: %s", bus_error_message(&error));
2793 if ((r = show_one(args[0], bus, path, show_properties, &new_line)) != 0)
2796 dbus_message_unref(m);
2797 dbus_message_unref(reply);
2803 dbus_message_unref(m);
2806 dbus_message_unref(reply);
2808 dbus_error_free(&error);
2813 static DBusHandlerResult monitor_filter(DBusConnection *connection, DBusMessage *message, void *data) {
2815 DBusMessage *m = NULL, *reply = NULL;
2820 dbus_error_init(&error);
2822 log_debug("Got D-Bus request: %s.%s() on %s",
2823 dbus_message_get_interface(message),
2824 dbus_message_get_member(message),
2825 dbus_message_get_path(message));
2827 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
2828 log_error("Warning! D-Bus connection terminated.");
2829 dbus_connection_close(connection);
2831 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitNew") ||
2832 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitRemoved")) {
2833 const char *id, *path;
2835 if (!dbus_message_get_args(message, &error,
2836 DBUS_TYPE_STRING, &id,
2837 DBUS_TYPE_OBJECT_PATH, &path,
2839 log_error("Failed to parse message: %s", bus_error_message(&error));
2840 else if (streq(dbus_message_get_member(message), "UnitNew"))
2841 printf("Unit %s added.\n", id);
2843 printf("Unit %s removed.\n", id);
2845 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobNew")) {
2849 if (!dbus_message_get_args(message, &error,
2850 DBUS_TYPE_UINT32, &id,
2851 DBUS_TYPE_OBJECT_PATH, &path,
2853 log_error("Failed to parse message: %s", bus_error_message(&error));
2855 printf("Job %u added.\n", id);
2858 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
2860 const char *path, *result;
2862 if (!dbus_message_get_args(message, &error,
2863 DBUS_TYPE_UINT32, &id,
2864 DBUS_TYPE_OBJECT_PATH, &path,
2865 DBUS_TYPE_STRING, &result,
2867 log_error("Failed to parse message: %s", bus_error_message(&error));
2869 printf("Job %u removed (result=%s).\n", id, result);
2872 } else if (dbus_message_is_signal(message, "org.freedesktop.DBus.Properties", "PropertiesChanged")) {
2874 const char *path, *interface, *property = "Id";
2875 DBusMessageIter iter, sub;
2877 path = dbus_message_get_path(message);
2879 if (!dbus_message_get_args(message, &error,
2880 DBUS_TYPE_STRING, &interface,
2881 DBUS_TYPE_INVALID)) {
2882 log_error("Failed to parse message: %s", bus_error_message(&error));
2886 if (!streq(interface, "org.freedesktop.systemd1.Job") &&
2887 !streq(interface, "org.freedesktop.systemd1.Unit"))
2890 if (!(m = dbus_message_new_method_call(
2891 "org.freedesktop.systemd1",
2893 "org.freedesktop.DBus.Properties",
2895 log_error("Could not allocate message.");
2899 if (!dbus_message_append_args(m,
2900 DBUS_TYPE_STRING, &interface,
2901 DBUS_TYPE_STRING, &property,
2902 DBUS_TYPE_INVALID)) {
2903 log_error("Could not append arguments to message.");
2907 if (!(reply = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) {
2908 log_error("Failed to issue method call: %s", bus_error_message(&error));
2912 if (!dbus_message_iter_init(reply, &iter) ||
2913 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2914 log_error("Failed to parse reply.");
2918 dbus_message_iter_recurse(&iter, &sub);
2920 if (streq(interface, "org.freedesktop.systemd1.Unit")) {
2923 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2924 log_error("Failed to parse reply.");
2928 dbus_message_iter_get_basic(&sub, &id);
2929 printf("Unit %s changed.\n", id);
2933 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT32) {
2934 log_error("Failed to parse reply.");
2938 dbus_message_iter_get_basic(&sub, &id);
2939 printf("Job %u changed.\n", id);
2945 dbus_message_unref(m);
2948 dbus_message_unref(reply);
2950 dbus_error_free(&error);
2951 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
2955 dbus_message_unref(m);
2958 dbus_message_unref(reply);
2960 dbus_error_free(&error);
2961 return DBUS_HANDLER_RESULT_NEED_MEMORY;
2964 static int monitor(DBusConnection *bus, char **args, unsigned n) {
2965 DBusMessage *m = NULL, *reply = NULL;
2969 dbus_error_init(&error);
2972 dbus_bus_add_match(bus,
2974 "sender='org.freedesktop.systemd1',"
2975 "interface='org.freedesktop.systemd1.Manager',"
2976 "path='/org/freedesktop/systemd1'",
2979 if (dbus_error_is_set(&error)) {
2980 log_error("Failed to add match: %s", bus_error_message(&error));
2985 dbus_bus_add_match(bus,
2987 "sender='org.freedesktop.systemd1',"
2988 "interface='org.freedesktop.DBus.Properties',"
2989 "member='PropertiesChanged'",
2992 if (dbus_error_is_set(&error)) {
2993 log_error("Failed to add match: %s", bus_error_message(&error));
2999 if (!dbus_connection_add_filter(bus, monitor_filter, NULL, NULL)) {
3000 log_error("Failed to add filter.");
3005 if (!(m = dbus_message_new_method_call(
3006 "org.freedesktop.systemd1",
3007 "/org/freedesktop/systemd1",
3008 "org.freedesktop.systemd1.Manager",
3010 log_error("Could not allocate message.");
3015 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3016 log_error("Failed to issue method call: %s", bus_error_message(&error));
3021 while (dbus_connection_read_write_dispatch(bus, -1))
3028 /* This is slightly dirty, since we don't undo the filter or the matches. */
3031 dbus_message_unref(m);
3034 dbus_message_unref(reply);
3036 dbus_error_free(&error);
3041 static int dump(DBusConnection *bus, char **args, unsigned n) {
3042 DBusMessage *m = NULL, *reply = NULL;
3047 dbus_error_init(&error);
3051 if (!(m = dbus_message_new_method_call(
3052 "org.freedesktop.systemd1",
3053 "/org/freedesktop/systemd1",
3054 "org.freedesktop.systemd1.Manager",
3056 log_error("Could not allocate message.");
3060 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3061 log_error("Failed to issue method call: %s", bus_error_message(&error));
3066 if (!dbus_message_get_args(reply, &error,
3067 DBUS_TYPE_STRING, &text,
3068 DBUS_TYPE_INVALID)) {
3069 log_error("Failed to parse reply: %s", bus_error_message(&error));
3074 fputs(text, stdout);
3080 dbus_message_unref(m);
3083 dbus_message_unref(reply);
3085 dbus_error_free(&error);
3090 static int snapshot(DBusConnection *bus, char **args, unsigned n) {
3091 DBusMessage *m = NULL, *reply = NULL;
3094 const char *name = "", *path, *id;
3095 dbus_bool_t cleanup = FALSE;
3096 DBusMessageIter iter, sub;
3098 *interface = "org.freedesktop.systemd1.Unit",
3101 dbus_error_init(&error);
3103 if (!(m = dbus_message_new_method_call(
3104 "org.freedesktop.systemd1",
3105 "/org/freedesktop/systemd1",
3106 "org.freedesktop.systemd1.Manager",
3107 "CreateSnapshot"))) {
3108 log_error("Could not allocate message.");
3115 if (!dbus_message_append_args(m,
3116 DBUS_TYPE_STRING, &name,
3117 DBUS_TYPE_BOOLEAN, &cleanup,
3118 DBUS_TYPE_INVALID)) {
3119 log_error("Could not append arguments to message.");
3124 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3125 log_error("Failed to issue method call: %s", bus_error_message(&error));
3130 if (!dbus_message_get_args(reply, &error,
3131 DBUS_TYPE_OBJECT_PATH, &path,
3132 DBUS_TYPE_INVALID)) {
3133 log_error("Failed to parse reply: %s", bus_error_message(&error));
3138 dbus_message_unref(m);
3139 if (!(m = dbus_message_new_method_call(
3140 "org.freedesktop.systemd1",