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>
36 #include <dbus/dbus.h>
42 #include "utmp-wtmp.h"
46 #include "dbus-common.h"
47 #include "cgroup-show.h"
48 #include "cgroup-util.h"
50 #include "path-lookup.h"
51 #include "conf-parser.h"
52 #include "sd-daemon.h"
53 #include "shutdownd.h"
54 #include "exit-status.h"
55 #include "bus-errors.h"
57 #include "unit-name.h"
59 #include "spawn-agent.h"
62 static const char *arg_type = NULL;
63 static char **arg_property = NULL;
64 static bool arg_all = false;
65 static const char *arg_job_mode = "replace";
66 static UnitFileScope arg_scope = UNIT_FILE_SYSTEM;
67 static bool arg_immediate = false;
68 static bool arg_no_block = false;
69 static bool arg_no_legend = false;
70 static bool arg_no_pager = false;
71 static bool arg_no_wtmp = false;
72 static bool arg_no_sync = false;
73 static bool arg_no_wall = false;
74 static bool arg_no_reload = false;
75 static bool arg_dry = false;
76 static bool arg_quiet = false;
77 static bool arg_full = false;
78 static bool arg_force = false;
79 static bool arg_ask_password = false;
80 static bool arg_failed = false;
81 static bool arg_runtime = false;
82 static char **arg_wall = NULL;
83 static const char *arg_kill_who = NULL;
84 static const char *arg_kill_mode = NULL;
85 static int arg_signal = SIGTERM;
86 static const char *arg_root = NULL;
87 static usec_t arg_when = 0;
106 ACTION_CANCEL_SHUTDOWN,
108 } arg_action = ACTION_SYSTEMCTL;
114 static enum transport {
118 } arg_transport = TRANSPORT_NORMAL;
119 static const char *arg_host = NULL;
121 static bool private_bus = false;
123 static int daemon_reload(DBusConnection *bus, char **args);
125 static bool on_tty(void) {
128 /* Note that this is invoked relatively early, before we start
129 * the pager. That means the value we return reflects whether
130 * we originally were started on a tty, not if we currently
131 * are. But this is intended, since we want colour and so on
132 * when run in our own pager. */
134 if (_unlikely_(t < 0))
135 t = isatty(STDOUT_FILENO) > 0;
140 static void pager_open_if_enabled(void) {
142 /* Cache result before we open the pager */
151 static void agent_open_if_enabled(void) {
153 /* Open the password agent as a child process if necessary */
155 if (!arg_ask_password)
158 if (arg_scope != UNIT_FILE_SYSTEM)
164 static const char *ansi_highlight(bool b) {
169 return b ? ANSI_HIGHLIGHT_ON : ANSI_HIGHLIGHT_OFF;
172 static const char *ansi_highlight_green(bool b) {
177 return b ? ANSI_HIGHLIGHT_GREEN_ON : ANSI_HIGHLIGHT_OFF;
180 static bool error_is_no_service(const DBusError *error) {
183 if (!dbus_error_is_set(error))
186 if (dbus_error_has_name(error, DBUS_ERROR_NAME_HAS_NO_OWNER))
189 if (dbus_error_has_name(error, DBUS_ERROR_SERVICE_UNKNOWN))
192 return startswith(error->name, "org.freedesktop.DBus.Error.Spawn.");
195 static int translate_bus_error_to_exit_status(int r, const DBusError *error) {
198 if (!dbus_error_is_set(error))
201 if (dbus_error_has_name(error, DBUS_ERROR_ACCESS_DENIED) ||
202 dbus_error_has_name(error, BUS_ERROR_ONLY_BY_DEPENDENCY) ||
203 dbus_error_has_name(error, BUS_ERROR_NO_ISOLATION) ||
204 dbus_error_has_name(error, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE))
205 return EXIT_NOPERMISSION;
207 if (dbus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT))
208 return EXIT_NOTINSTALLED;
210 if (dbus_error_has_name(error, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE) ||
211 dbus_error_has_name(error, BUS_ERROR_NOT_SUPPORTED))
212 return EXIT_NOTIMPLEMENTED;
214 if (dbus_error_has_name(error, BUS_ERROR_LOAD_FAILED))
215 return EXIT_NOTCONFIGURED;
223 static void warn_wall(enum action action) {
224 static const char *table[_ACTION_MAX] = {
225 [ACTION_HALT] = "The system is going down for system halt NOW!",
226 [ACTION_REBOOT] = "The system is going down for reboot NOW!",
227 [ACTION_POWEROFF] = "The system is going down for power-off NOW!",
228 [ACTION_KEXEC] = "The system is going down for kexec reboot NOW!",
229 [ACTION_RESCUE] = "The system is going down to rescue mode NOW!",
230 [ACTION_EMERGENCY] = "The system is going down to emergency mode NOW!"
239 if (!(p = strv_join(arg_wall, " "))) {
240 log_error("Failed to join strings.");
256 utmp_wall(table[action], NULL);
259 static bool avoid_bus(void) {
261 if (running_in_chroot() > 0)
264 if (sd_booted() <= 0)
267 if (!isempty(arg_root))
270 if (arg_scope == UNIT_FILE_GLOBAL)
278 const char *description;
279 const char *load_state;
280 const char *active_state;
281 const char *sub_state;
282 const char *following;
283 const char *unit_path;
285 const char *job_type;
286 const char *job_path;
289 static int compare_unit_info(const void *a, const void *b) {
291 const struct unit_info *u = a, *v = b;
293 d1 = strrchr(u->id, '.');
294 d2 = strrchr(v->id, '.');
299 if ((r = strcasecmp(d1, d2)) != 0)
303 return strcasecmp(u->id, v->id);
306 static bool output_show_unit(const struct unit_info *u) {
310 return streq(u->active_state, "failed");
312 return (!arg_type || ((dot = strrchr(u->id, '.')) &&
313 streq(dot+1, arg_type))) &&
314 (arg_all || !(streq(u->active_state, "inactive") || u->following[0]) || u->job_id > 0);
317 static void output_units_list(const struct unit_info *unit_infos, unsigned c) {
318 unsigned id_len, max_id_len, active_len, sub_len, job_len, desc_len, n_shown = 0;
319 const struct unit_info *u;
321 max_id_len = sizeof("UNIT")-1;
322 active_len = sizeof("ACTIVE")-1;
323 sub_len = sizeof("SUB")-1;
324 job_len = sizeof("JOB")-1;
327 for (u = unit_infos; u < unit_infos + c; u++) {
328 if (!output_show_unit(u))
331 max_id_len = MAX(max_id_len, strlen(u->id));
332 active_len = MAX(active_len, strlen(u->active_state));
333 sub_len = MAX(sub_len, strlen(u->sub_state));
335 job_len = MAX(job_len, strlen(u->job_type));
340 id_len = MIN(max_id_len, 25);
341 basic_len = 5 + id_len + 6 + active_len + sub_len + job_len;
342 if (basic_len < (unsigned) columns()) {
343 unsigned extra_len, incr;
344 extra_len = columns() - basic_len;
345 /* Either UNIT already got 25, or is fully satisfied.
346 * Grant up to 25 to DESC now. */
347 incr = MIN(extra_len, 25);
350 /* split the remaining space between UNIT and DESC,
351 * but do not give UNIT more than it needs. */
353 incr = MIN(extra_len / 2, max_id_len - id_len);
355 desc_len += extra_len - incr;
361 if (!arg_no_legend) {
362 printf("%-*s %-6s %-*s %-*s %-*s ", id_len, "UNIT", "LOAD",
363 active_len, "ACTIVE", sub_len, "SUB", job_len, "JOB");
364 if (!arg_full && arg_no_pager)
365 printf("%.*s\n", desc_len, "DESCRIPTION");
367 printf("%s\n", "DESCRIPTION");
370 for (u = unit_infos; u < unit_infos + c; u++) {
372 const char *on_loaded, *off_loaded;
373 const char *on_active, *off_active;
375 if (!output_show_unit(u))
380 if (!streq(u->load_state, "loaded") &&
381 !streq(u->load_state, "banned")) {
382 on_loaded = ansi_highlight(true);
383 off_loaded = ansi_highlight(false);
385 on_loaded = off_loaded = "";
387 if (streq(u->active_state, "failed")) {
388 on_active = ansi_highlight(true);
389 off_active = ansi_highlight(false);
391 on_active = off_active = "";
393 e = arg_full ? NULL : ellipsize(u->id, id_len, 33);
395 printf("%-*s %s%-6s%s %s%-*s %-*s%s %-*s ",
396 id_len, e ? e : u->id,
397 on_loaded, u->load_state, off_loaded,
398 on_active, active_len, u->active_state,
399 sub_len, u->sub_state, off_active,
400 job_len, u->job_id ? u->job_type : "");
401 if (!arg_full && arg_no_pager)
402 printf("%.*s\n", desc_len, u->description);
404 printf("%s\n", u->description);
409 if (!arg_no_legend) {
410 printf("\nLOAD = Reflects whether the unit definition was properly loaded.\n"
411 "ACTIVE = The high-level unit activation state, i.e. generalization of SUB.\n"
412 "SUB = The low-level unit activation state, values depend on unit type.\n"
413 "JOB = Pending job for the unit.\n");
416 printf("\n%u units listed.\n", n_shown);
418 printf("\n%u units listed. Pass --all to see inactive units, too.\n", n_shown);
422 static int list_units(DBusConnection *bus, char **args) {
423 DBusMessage *m = NULL, *reply = NULL;
426 DBusMessageIter iter, sub, sub2;
427 unsigned c = 0, n_units = 0;
428 struct unit_info *unit_infos = NULL;
430 dbus_error_init(&error);
434 pager_open_if_enabled();
436 if (!(m = dbus_message_new_method_call(
437 "org.freedesktop.systemd1",
438 "/org/freedesktop/systemd1",
439 "org.freedesktop.systemd1.Manager",
441 log_error("Could not allocate message.");
445 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
446 log_error("Failed to issue method call: %s", bus_error_message(&error));
451 if (!dbus_message_iter_init(reply, &iter) ||
452 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
453 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
454 log_error("Failed to parse reply.");
459 dbus_message_iter_recurse(&iter, &sub);
461 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
464 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
465 log_error("Failed to parse reply.");
473 n_units = MAX(2*c, 16);
474 w = realloc(unit_infos, sizeof(struct unit_info) * n_units);
477 log_error("Failed to allocate unit array.");
487 dbus_message_iter_recurse(&sub, &sub2);
489 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->id, true) < 0 ||
490 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->description, true) < 0 ||
491 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->load_state, true) < 0 ||
492 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->active_state, true) < 0 ||
493 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->sub_state, true) < 0 ||
494 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->following, true) < 0 ||
495 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &u->unit_path, true) < 0 ||
496 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &u->job_id, true) < 0 ||
497 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->job_type, true) < 0 ||
498 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &u->job_path, false) < 0) {
499 log_error("Failed to parse reply.");
504 dbus_message_iter_next(&sub);
509 qsort(unit_infos, c, sizeof(struct unit_info), compare_unit_info);
510 output_units_list(unit_infos, c);
517 dbus_message_unref(m);
520 dbus_message_unref(reply);
524 dbus_error_free(&error);
529 static int compare_unit_file_list(const void *a, const void *b) {
531 const UnitFileList *u = a, *v = b;
533 d1 = strrchr(u->path, '.');
534 d2 = strrchr(v->path, '.');
539 r = strcasecmp(d1, d2);
544 return strcasecmp(file_name_from_path(u->path), file_name_from_path(v->path));
547 static bool output_show_unit_file(const UnitFileList *u) {
550 return !arg_type || ((dot = strrchr(u->path, '.')) && streq(dot+1, arg_type));
553 static void output_unit_file_list(const UnitFileList *units, unsigned c) {
554 unsigned max_id_len, id_cols, state_cols, n_shown = 0;
555 const UnitFileList *u;
557 max_id_len = sizeof("UNIT FILE")-1;
558 state_cols = sizeof("STATE")-1;
559 for (u = units; u < units + c; u++) {
560 if (!output_show_unit_file(u))
563 max_id_len = MAX(max_id_len, strlen(file_name_from_path(u->path)));
564 state_cols = MAX(state_cols, strlen(unit_file_state_to_string(u->state)));
569 id_cols = MIN(max_id_len, 25);
570 basic_cols = 1 + id_cols + state_cols;
571 if (basic_cols < (unsigned) columns())
572 id_cols += MIN(columns() - basic_cols, max_id_len - id_cols);
574 id_cols = max_id_len;
577 printf("%-*s %-*s\n", id_cols, "UNIT FILE", state_cols, "STATE");
579 for (u = units; u < units + c; u++) {
581 const char *on, *off;
584 if (!output_show_unit_file(u))
589 if (u->state == UNIT_FILE_MASKED ||
590 u->state == UNIT_FILE_MASKED_RUNTIME ||
591 u->state == UNIT_FILE_DISABLED) {
592 on = ansi_highlight(true);
593 off = ansi_highlight(false);
594 } else if (u->state == UNIT_FILE_ENABLED) {
595 on = ansi_highlight_green(true);
596 off = ansi_highlight_green(false);
600 id = file_name_from_path(u->path);
602 e = arg_full ? NULL : ellipsize(id, id_cols, 33);
604 printf("%-*s %s%-*s%s\n",
606 on, state_cols, unit_file_state_to_string(u->state), off);
612 printf("\n%u unit files listed.\n", n_shown);
615 static int list_unit_files(DBusConnection *bus, char **args) {
616 DBusMessage *m = NULL, *reply = NULL;
619 DBusMessageIter iter, sub, sub2;
620 unsigned c = 0, n_units = 0;
621 UnitFileList *units = NULL;
623 dbus_error_init(&error);
627 pager_open_if_enabled();
634 h = hashmap_new(string_hash_func, string_compare_func);
636 log_error("Out of memory");
640 r = unit_file_get_list(arg_scope, arg_root, h);
642 unit_file_list_free(h);
643 log_error("Failed to get unit file list: %s", strerror(-r));
647 n_units = hashmap_size(h);
648 units = new(UnitFileList, n_units);
650 unit_file_list_free(h);
651 log_error("Out of memory");
655 HASHMAP_FOREACH(u, h, i) {
656 memcpy(units + c++, u, sizeof(UnitFileList));
662 m = dbus_message_new_method_call(
663 "org.freedesktop.systemd1",
664 "/org/freedesktop/systemd1",
665 "org.freedesktop.systemd1.Manager",
668 log_error("Could not allocate message.");
672 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
674 log_error("Failed to issue method call: %s", bus_error_message(&error));
679 if (!dbus_message_iter_init(reply, &iter) ||
680 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
681 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
682 log_error("Failed to parse reply.");
687 dbus_message_iter_recurse(&iter, &sub);
689 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
693 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
694 log_error("Failed to parse reply.");
702 n_units = MAX(2*c, 16);
703 w = realloc(units, sizeof(struct UnitFileList) * n_units);
706 log_error("Failed to allocate unit array.");
716 dbus_message_iter_recurse(&sub, &sub2);
718 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->path, true) < 0 ||
719 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, false) < 0) {
720 log_error("Failed to parse reply.");
725 u->state = unit_file_state_from_string(state);
727 dbus_message_iter_next(&sub);
733 qsort(units, c, sizeof(UnitFileList), compare_unit_file_list);
734 output_unit_file_list(units, c);
741 dbus_message_unref(m);
744 dbus_message_unref(reply);
748 dbus_error_free(&error);
753 static int dot_one_property(const char *name, const char *prop, DBusMessageIter *iter) {
754 static const char * const colors[] = {
755 "Requires", "[color=\"black\"]",
756 "RequiresOverridable", "[color=\"black\"]",
757 "Requisite", "[color=\"darkblue\"]",
758 "RequisiteOverridable", "[color=\"darkblue\"]",
759 "Wants", "[color=\"darkgrey\"]",
760 "Conflicts", "[color=\"red\"]",
761 "ConflictedBy", "[color=\"red\"]",
762 "After", "[color=\"green\"]"
765 const char *c = NULL;
772 for (i = 0; i < ELEMENTSOF(colors); i += 2)
773 if (streq(colors[i], prop)) {
781 if (arg_dot != DOT_ALL)
782 if ((arg_dot == DOT_ORDER) != streq(prop, "After"))
785 switch (dbus_message_iter_get_arg_type(iter)) {
787 case DBUS_TYPE_ARRAY:
789 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
792 dbus_message_iter_recurse(iter, &sub);
794 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
797 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
798 dbus_message_iter_get_basic(&sub, &s);
799 printf("\t\"%s\"->\"%s\" %s;\n", name, s, c);
801 dbus_message_iter_next(&sub);
811 static int dot_one(DBusConnection *bus, const char *name, const char *path) {
812 DBusMessage *m = NULL, *reply = NULL;
813 const char *interface = "org.freedesktop.systemd1.Unit";
816 DBusMessageIter iter, sub, sub2, sub3;
821 dbus_error_init(&error);
823 if (!(m = dbus_message_new_method_call(
824 "org.freedesktop.systemd1",
826 "org.freedesktop.DBus.Properties",
828 log_error("Could not allocate message.");
833 if (!dbus_message_append_args(m,
834 DBUS_TYPE_STRING, &interface,
835 DBUS_TYPE_INVALID)) {
836 log_error("Could not append arguments to message.");
841 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
842 log_error("Failed to issue method call: %s", bus_error_message(&error));
847 if (!dbus_message_iter_init(reply, &iter) ||
848 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
849 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
850 log_error("Failed to parse reply.");
855 dbus_message_iter_recurse(&iter, &sub);
857 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
860 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
861 log_error("Failed to parse reply.");
866 dbus_message_iter_recurse(&sub, &sub2);
868 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &prop, true) < 0) {
869 log_error("Failed to parse reply.");
874 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
875 log_error("Failed to parse reply.");
880 dbus_message_iter_recurse(&sub2, &sub3);
882 if (dot_one_property(name, prop, &sub3)) {
883 log_error("Failed to parse reply.");
888 dbus_message_iter_next(&sub);
895 dbus_message_unref(m);
898 dbus_message_unref(reply);
900 dbus_error_free(&error);
905 static int dot(DBusConnection *bus, char **args) {
906 DBusMessage *m = NULL, *reply = NULL;
909 DBusMessageIter iter, sub, sub2;
911 dbus_error_init(&error);
915 if (!(m = dbus_message_new_method_call(
916 "org.freedesktop.systemd1",
917 "/org/freedesktop/systemd1",
918 "org.freedesktop.systemd1.Manager",
920 log_error("Could not allocate message.");
924 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
925 log_error("Failed to issue method call: %s", bus_error_message(&error));
930 if (!dbus_message_iter_init(reply, &iter) ||
931 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
932 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
933 log_error("Failed to parse reply.");
938 printf("digraph systemd {\n");
940 dbus_message_iter_recurse(&iter, &sub);
941 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
942 const char *id, *description, *load_state, *active_state, *sub_state, *following, *unit_path;
944 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
945 log_error("Failed to parse reply.");
950 dbus_message_iter_recurse(&sub, &sub2);
952 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
953 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &description, true) < 0 ||
954 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &load_state, true) < 0 ||
955 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &active_state, true) < 0 ||
956 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &sub_state, true) < 0 ||
957 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &following, true) < 0 ||
958 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, true) < 0) {
959 log_error("Failed to parse reply.");
964 if ((r = dot_one(bus, id, unit_path)) < 0)
967 /* printf("\t\"%s\";\n", id); */
968 dbus_message_iter_next(&sub);
973 log_info(" Color legend: black = Requires\n"
974 " dark blue = Requisite\n"
975 " dark grey = Wants\n"
980 log_notice("-- You probably want to process this output with graphviz' dot tool.\n"
981 "-- Try a shell pipeline like 'systemctl dot | dot -Tsvg > systemd.svg'!\n");
987 dbus_message_unref(m);
990 dbus_message_unref(reply);
992 dbus_error_free(&error);
997 static int list_jobs(DBusConnection *bus, char **args) {
998 DBusMessage *m = NULL, *reply = NULL;
1001 DBusMessageIter iter, sub, sub2;
1004 dbus_error_init(&error);
1008 pager_open_if_enabled();
1010 if (!(m = dbus_message_new_method_call(
1011 "org.freedesktop.systemd1",
1012 "/org/freedesktop/systemd1",
1013 "org.freedesktop.systemd1.Manager",
1015 log_error("Could not allocate message.");
1019 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1020 log_error("Failed to issue method call: %s", bus_error_message(&error));
1025 if (!dbus_message_iter_init(reply, &iter) ||
1026 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
1027 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
1028 log_error("Failed to parse reply.");
1033 dbus_message_iter_recurse(&iter, &sub);
1036 printf("%4s %-25s %-15s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
1038 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1039 const char *name, *type, *state, *job_path, *unit_path;
1043 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
1044 log_error("Failed to parse reply.");
1049 dbus_message_iter_recurse(&sub, &sub2);
1051 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &id, true) < 0 ||
1052 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0 ||
1053 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
1054 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, true) < 0 ||
1055 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, true) < 0 ||
1056 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, false) < 0) {
1057 log_error("Failed to parse reply.");
1062 e = arg_full ? NULL : ellipsize(name, 25, 33);
1063 printf("%4u %-25s %-15s %-7s\n", id, e ? e : name, type, state);
1068 dbus_message_iter_next(&sub);
1072 printf("\n%u jobs listed.\n", k);
1078 dbus_message_unref(m);
1081 dbus_message_unref(reply);
1083 dbus_error_free(&error);
1088 static int load_unit(DBusConnection *bus, char **args) {
1089 DBusMessage *m = NULL;
1094 dbus_error_init(&error);
1099 STRV_FOREACH(name, args+1) {
1102 if (!(m = dbus_message_new_method_call(
1103 "org.freedesktop.systemd1",
1104 "/org/freedesktop/systemd1",
1105 "org.freedesktop.systemd1.Manager",
1107 log_error("Could not allocate message.");
1112 if (!dbus_message_append_args(m,
1113 DBUS_TYPE_STRING, name,
1114 DBUS_TYPE_INVALID)) {
1115 log_error("Could not append arguments to message.");
1120 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1121 log_error("Failed to issue method call: %s", bus_error_message(&error));
1126 dbus_message_unref(m);
1127 dbus_message_unref(reply);
1136 dbus_message_unref(m);
1138 dbus_error_free(&error);
1143 static int cancel_job(DBusConnection *bus, char **args) {
1144 DBusMessage *m = NULL, *reply = NULL;
1149 dbus_error_init(&error);
1154 if (strv_length(args) <= 1)
1155 return daemon_reload(bus, args);
1157 STRV_FOREACH(name, args+1) {
1161 if (!(m = dbus_message_new_method_call(
1162 "org.freedesktop.systemd1",
1163 "/org/freedesktop/systemd1",
1164 "org.freedesktop.systemd1.Manager",
1166 log_error("Could not allocate message.");
1171 if ((r = safe_atou(*name, &id)) < 0) {
1172 log_error("Failed to parse job id: %s", strerror(-r));
1176 assert_cc(sizeof(uint32_t) == sizeof(id));
1177 if (!dbus_message_append_args(m,
1178 DBUS_TYPE_UINT32, &id,
1179 DBUS_TYPE_INVALID)) {
1180 log_error("Could not append arguments to message.");
1185 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1186 log_error("Failed to issue method call: %s", bus_error_message(&error));
1191 if (!dbus_message_get_args(reply, &error,
1192 DBUS_TYPE_OBJECT_PATH, &path,
1193 DBUS_TYPE_INVALID)) {
1194 log_error("Failed to parse reply: %s", bus_error_message(&error));
1199 dbus_message_unref(m);
1200 if (!(m = dbus_message_new_method_call(
1201 "org.freedesktop.systemd1",
1203 "org.freedesktop.systemd1.Job",
1205 log_error("Could not allocate message.");
1210 dbus_message_unref(reply);
1211 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1212 log_error("Failed to issue method call: %s", bus_error_message(&error));
1217 dbus_message_unref(m);
1218 dbus_message_unref(reply);
1226 dbus_message_unref(m);
1229 dbus_message_unref(reply);
1231 dbus_error_free(&error);
1236 static bool need_daemon_reload(DBusConnection *bus, const char *unit) {
1237 DBusMessage *m = NULL, *reply = NULL;
1238 dbus_bool_t b = FALSE;
1239 DBusMessageIter iter, sub;
1241 *interface = "org.freedesktop.systemd1.Unit",
1242 *property = "NeedDaemonReload",
1245 /* We ignore all errors here, since this is used to show a warning only */
1247 if (!(m = dbus_message_new_method_call(
1248 "org.freedesktop.systemd1",
1249 "/org/freedesktop/systemd1",
1250 "org.freedesktop.systemd1.Manager",
1254 if (!dbus_message_append_args(m,
1255 DBUS_TYPE_STRING, &unit,
1259 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL)))
1262 if (!dbus_message_get_args(reply, NULL,
1263 DBUS_TYPE_OBJECT_PATH, &path,
1267 dbus_message_unref(m);
1268 if (!(m = dbus_message_new_method_call(
1269 "org.freedesktop.systemd1",
1271 "org.freedesktop.DBus.Properties",
1275 if (!dbus_message_append_args(m,
1276 DBUS_TYPE_STRING, &interface,
1277 DBUS_TYPE_STRING, &property,
1278 DBUS_TYPE_INVALID)) {
1282 dbus_message_unref(reply);
1283 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL)))
1286 if (!dbus_message_iter_init(reply, &iter) ||
1287 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
1290 dbus_message_iter_recurse(&iter, &sub);
1292 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_BOOLEAN)
1295 dbus_message_iter_get_basic(&sub, &b);
1299 dbus_message_unref(m);
1302 dbus_message_unref(reply);
1307 typedef struct WaitData {
1312 static DBusHandlerResult wait_filter(DBusConnection *connection, DBusMessage *message, void *data) {
1320 dbus_error_init(&error);
1322 log_debug("Got D-Bus request: %s.%s() on %s",
1323 dbus_message_get_interface(message),
1324 dbus_message_get_member(message),
1325 dbus_message_get_path(message));
1327 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
1328 log_error("Warning! D-Bus connection terminated.");
1329 dbus_connection_close(connection);
1331 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
1333 const char *path, *result;
1334 dbus_bool_t success = true;
1336 if (dbus_message_get_args(message, &error,
1337 DBUS_TYPE_UINT32, &id,
1338 DBUS_TYPE_OBJECT_PATH, &path,
1339 DBUS_TYPE_STRING, &result,
1340 DBUS_TYPE_INVALID)) {
1343 if ((p = set_remove(d->set, (char*) path)))
1347 d->result = strdup(result);
1352 dbus_error_free(&error);
1354 if (dbus_message_get_args(message, &error,
1355 DBUS_TYPE_UINT32, &id,
1356 DBUS_TYPE_OBJECT_PATH, &path,
1357 DBUS_TYPE_BOOLEAN, &success,
1358 DBUS_TYPE_INVALID)) {
1361 /* Compatibility with older systemd versions <
1362 * 19 during upgrades. This should be dropped
1365 if ((p = set_remove(d->set, (char*) path)))
1369 d->result = strdup("failed");
1375 log_error("Failed to parse message: %s", bus_error_message(&error));
1379 dbus_error_free(&error);
1380 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1383 static int enable_wait_for_jobs(DBusConnection *bus) {
1391 dbus_error_init(&error);
1392 dbus_bus_add_match(bus,
1394 "sender='org.freedesktop.systemd1',"
1395 "interface='org.freedesktop.systemd1.Manager',"
1396 "member='JobRemoved',"
1397 "path='/org/freedesktop/systemd1'",
1400 if (dbus_error_is_set(&error)) {
1401 log_error("Failed to add match: %s", bus_error_message(&error));
1402 dbus_error_free(&error);
1406 /* This is slightly dirty, since we don't undo the match registrations. */
1410 static int wait_for_jobs(DBusConnection *bus, Set *s) {
1420 if (!dbus_connection_add_filter(bus, wait_filter, &d, NULL)) {
1421 log_error("Failed to add filter.");
1426 while (!set_isempty(s) &&
1427 dbus_connection_read_write_dispatch(bus, -1))
1430 if (!arg_quiet && d.result) {
1431 if (streq(d.result, "timeout"))
1432 log_error("Job timed out.");
1433 else if (streq(d.result, "canceled"))
1434 log_error("Job canceled.");
1435 else if (streq(d.result, "dependency"))
1436 log_error("A dependency job failed. See system logs for details.");
1437 else if (!streq(d.result, "done") && !streq(d.result, "skipped"))
1438 log_error("Job failed. See system logs and 'systemctl status' for details.");
1441 if (streq_ptr(d.result, "timeout"))
1443 else if (streq_ptr(d.result, "canceled"))
1445 else if (!streq_ptr(d.result, "done") && !streq_ptr(d.result, "skipped"))
1453 /* This is slightly dirty, since we don't undo the filter registration. */
1458 static int start_unit_one(
1459 DBusConnection *bus,
1466 DBusMessage *m = NULL, *reply = NULL;
1475 assert(arg_no_block || s);
1477 if (!(m = dbus_message_new_method_call(
1478 "org.freedesktop.systemd1",
1479 "/org/freedesktop/systemd1",
1480 "org.freedesktop.systemd1.Manager",
1482 log_error("Could not allocate message.");
1487 if (!dbus_message_append_args(m,
1488 DBUS_TYPE_STRING, &name,
1489 DBUS_TYPE_STRING, &mode,
1490 DBUS_TYPE_INVALID)) {
1491 log_error("Could not append arguments to message.");
1496 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, error))) {
1498 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(error)) {
1499 /* There's always a fallback possible for
1500 * legacy actions. */
1505 log_error("Failed to issue method call: %s", bus_error_message(error));
1510 if (!dbus_message_get_args(reply, error,
1511 DBUS_TYPE_OBJECT_PATH, &path,
1512 DBUS_TYPE_INVALID)) {
1513 log_error("Failed to parse reply: %s", bus_error_message(error));
1518 if (need_daemon_reload(bus, name))
1519 log_warning("Warning: Unit file of created job changed on disk, 'systemctl %s daemon-reload' recommended.",
1520 arg_scope == UNIT_FILE_SYSTEM ? "--system" : "--user");
1522 if (!arg_no_block) {
1525 if (!(p = strdup(path))) {
1526 log_error("Failed to duplicate path.");
1531 if ((r = set_put(s, p)) < 0) {
1533 log_error("Failed to add path to set.");
1542 dbus_message_unref(m);
1545 dbus_message_unref(reply);
1550 static enum action verb_to_action(const char *verb) {
1551 if (streq(verb, "halt"))
1553 else if (streq(verb, "poweroff"))
1554 return ACTION_POWEROFF;
1555 else if (streq(verb, "reboot"))
1556 return ACTION_REBOOT;
1557 else if (streq(verb, "kexec"))
1558 return ACTION_KEXEC;
1559 else if (streq(verb, "rescue"))
1560 return ACTION_RESCUE;
1561 else if (streq(verb, "emergency"))
1562 return ACTION_EMERGENCY;
1563 else if (streq(verb, "default"))
1564 return ACTION_DEFAULT;
1565 else if (streq(verb, "exit"))
1568 return ACTION_INVALID;
1571 static int start_unit(DBusConnection *bus, char **args) {
1573 static const char * const table[_ACTION_MAX] = {
1574 [ACTION_HALT] = SPECIAL_HALT_TARGET,
1575 [ACTION_POWEROFF] = SPECIAL_POWEROFF_TARGET,
1576 [ACTION_REBOOT] = SPECIAL_REBOOT_TARGET,
1577 [ACTION_KEXEC] = SPECIAL_KEXEC_TARGET,
1578 [ACTION_RUNLEVEL2] = SPECIAL_RUNLEVEL2_TARGET,
1579 [ACTION_RUNLEVEL3] = SPECIAL_RUNLEVEL3_TARGET,
1580 [ACTION_RUNLEVEL4] = SPECIAL_RUNLEVEL4_TARGET,
1581 [ACTION_RUNLEVEL5] = SPECIAL_RUNLEVEL5_TARGET,
1582 [ACTION_RESCUE] = SPECIAL_RESCUE_TARGET,
1583 [ACTION_EMERGENCY] = SPECIAL_EMERGENCY_TARGET,
1584 [ACTION_DEFAULT] = SPECIAL_DEFAULT_TARGET,
1585 [ACTION_EXIT] = SPECIAL_EXIT_TARGET
1589 const char *method, *mode, *one_name;
1594 dbus_error_init(&error);
1598 agent_open_if_enabled();
1600 if (arg_action == ACTION_SYSTEMCTL) {
1602 streq(args[0], "stop") ||
1603 streq(args[0], "condstop") ? "StopUnit" :
1604 streq(args[0], "reload") ? "ReloadUnit" :
1605 streq(args[0], "restart") ? "RestartUnit" :
1607 streq(args[0], "try-restart") ||
1608 streq(args[0], "condrestart") ? "TryRestartUnit" :
1610 streq(args[0], "reload-or-restart") ? "ReloadOrRestartUnit" :
1612 streq(args[0], "reload-or-try-restart") ||
1613 streq(args[0], "condreload") ||
1615 streq(args[0], "force-reload") ? "ReloadOrTryRestartUnit" :
1619 (streq(args[0], "isolate") ||
1620 streq(args[0], "rescue") ||
1621 streq(args[0], "emergency")) ? "isolate" : arg_job_mode;
1623 one_name = table[verb_to_action(args[0])];
1626 assert(arg_action < ELEMENTSOF(table));
1627 assert(table[arg_action]);
1629 method = "StartUnit";
1631 mode = (arg_action == ACTION_EMERGENCY ||
1632 arg_action == ACTION_RESCUE ||
1633 arg_action == ACTION_RUNLEVEL2 ||
1634 arg_action == ACTION_RUNLEVEL3 ||
1635 arg_action == ACTION_RUNLEVEL4 ||
1636 arg_action == ACTION_RUNLEVEL5) ? "isolate" : "replace";
1638 one_name = table[arg_action];
1641 if (!arg_no_block) {
1642 if ((ret = enable_wait_for_jobs(bus)) < 0) {
1643 log_error("Could not watch jobs: %s", strerror(-ret));
1647 if (!(s = set_new(string_hash_func, string_compare_func))) {
1648 log_error("Failed to allocate set.");
1655 if ((ret = start_unit_one(bus, method, one_name, mode, &error, s)) <= 0)
1658 STRV_FOREACH(name, args+1)
1659 if ((r = start_unit_one(bus, method, *name, mode, &error, s)) != 0) {
1660 ret = translate_bus_error_to_exit_status(r, &error);
1661 dbus_error_free(&error);
1666 if ((r = wait_for_jobs(bus, s)) < 0) {
1675 dbus_error_free(&error);
1680 static int start_special(DBusConnection *bus, char **args) {
1687 (streq(args[0], "halt") ||
1688 streq(args[0], "poweroff") ||
1689 streq(args[0], "reboot") ||
1690 streq(args[0], "kexec") ||
1691 streq(args[0], "exit")))
1692 return daemon_reload(bus, args);
1694 r = start_unit(bus, args);
1697 warn_wall(verb_to_action(args[0]));
1702 static int check_unit(DBusConnection *bus, char **args) {
1703 DBusMessage *m = NULL, *reply = NULL;
1705 *interface = "org.freedesktop.systemd1.Unit",
1706 *property = "ActiveState";
1707 int r = 3; /* According to LSB: "program is not running" */
1714 dbus_error_init(&error);
1716 STRV_FOREACH(name, args+1) {
1717 const char *path = NULL;
1719 DBusMessageIter iter, sub;
1721 if (!(m = dbus_message_new_method_call(
1722 "org.freedesktop.systemd1",
1723 "/org/freedesktop/systemd1",
1724 "org.freedesktop.systemd1.Manager",
1726 log_error("Could not allocate message.");
1731 if (!dbus_message_append_args(m,
1732 DBUS_TYPE_STRING, name,
1733 DBUS_TYPE_INVALID)) {
1734 log_error("Could not append arguments to message.");
1739 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1741 /* Hmm, cannot figure out anything about this unit... */
1745 dbus_error_free(&error);
1746 dbus_message_unref(m);
1751 if (!dbus_message_get_args(reply, &error,
1752 DBUS_TYPE_OBJECT_PATH, &path,
1753 DBUS_TYPE_INVALID)) {
1754 log_error("Failed to parse reply: %s", bus_error_message(&error));
1759 dbus_message_unref(m);
1760 if (!(m = dbus_message_new_method_call(
1761 "org.freedesktop.systemd1",
1763 "org.freedesktop.DBus.Properties",
1765 log_error("Could not allocate message.");
1770 if (!dbus_message_append_args(m,
1771 DBUS_TYPE_STRING, &interface,
1772 DBUS_TYPE_STRING, &property,
1773 DBUS_TYPE_INVALID)) {
1774 log_error("Could not append arguments to message.");
1779 dbus_message_unref(reply);
1780 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1781 log_error("Failed to issue method call: %s", bus_error_message(&error));
1786 if (!dbus_message_iter_init(reply, &iter) ||
1787 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1788 log_error("Failed to parse reply.");
1793 dbus_message_iter_recurse(&iter, &sub);
1795 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1796 log_error("Failed to parse reply.");
1801 dbus_message_iter_get_basic(&sub, &state);
1806 if (streq(state, "active") || streq(state, "reloading"))
1809 dbus_message_unref(m);
1810 dbus_message_unref(reply);
1816 dbus_message_unref(m);
1819 dbus_message_unref(reply);
1821 dbus_error_free(&error);
1826 static int kill_unit(DBusConnection *bus, char **args) {
1827 DBusMessage *m = NULL;
1835 dbus_error_init(&error);
1838 arg_kill_who = "all";
1841 arg_kill_mode = streq(arg_kill_who, "all") ? "control-group" : "process";
1843 STRV_FOREACH(name, args+1) {
1846 if (!(m = dbus_message_new_method_call(
1847 "org.freedesktop.systemd1",
1848 "/org/freedesktop/systemd1",
1849 "org.freedesktop.systemd1.Manager",
1851 log_error("Could not allocate message.");
1856 if (!dbus_message_append_args(m,
1857 DBUS_TYPE_STRING, name,
1858 DBUS_TYPE_STRING, &arg_kill_who,
1859 DBUS_TYPE_STRING, &arg_kill_mode,
1860 DBUS_TYPE_INT32, &arg_signal,
1861 DBUS_TYPE_INVALID)) {
1862 log_error("Could not append arguments to message.");
1867 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1868 log_error("Failed to issue method call: %s", bus_error_message(&error));
1869 dbus_error_free(&error);
1873 dbus_message_unref(m);
1876 dbus_message_unref(reply);
1882 dbus_message_unref(m);
1884 dbus_error_free(&error);
1889 typedef struct ExecStatusInfo {
1897 usec_t start_timestamp;
1898 usec_t exit_timestamp;
1903 LIST_FIELDS(struct ExecStatusInfo, exec);
1906 static void exec_status_info_free(ExecStatusInfo *i) {
1915 static int exec_status_info_deserialize(DBusMessageIter *sub, ExecStatusInfo *i) {
1916 uint64_t start_timestamp, exit_timestamp, start_timestamp_monotonic, exit_timestamp_monotonic;
1917 DBusMessageIter sub2, sub3;
1921 int32_t code, status;
1927 if (dbus_message_iter_get_arg_type(sub) != DBUS_TYPE_STRUCT)
1930 dbus_message_iter_recurse(sub, &sub2);
1932 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0)
1935 if (!(i->path = strdup(path)))
1938 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_ARRAY ||
1939 dbus_message_iter_get_element_type(&sub2) != DBUS_TYPE_STRING)
1943 dbus_message_iter_recurse(&sub2, &sub3);
1944 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1945 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1946 dbus_message_iter_next(&sub3);
1951 if (!(i->argv = new0(char*, n+1)))
1955 dbus_message_iter_recurse(&sub2, &sub3);
1956 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1959 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1960 dbus_message_iter_get_basic(&sub3, &s);
1961 dbus_message_iter_next(&sub3);
1963 if (!(i->argv[n++] = strdup(s)))
1967 if (!dbus_message_iter_next(&sub2) ||
1968 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_BOOLEAN, &ignore, true) < 0 ||
1969 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_timestamp, true) < 0 ||
1970 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_timestamp_monotonic, true) < 0 ||
1971 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_timestamp, true) < 0 ||
1972 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_timestamp_monotonic, true) < 0 ||
1973 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, true) < 0 ||
1974 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &code, true) < 0 ||
1975 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &status, false) < 0)
1979 i->start_timestamp = (usec_t) start_timestamp;
1980 i->exit_timestamp = (usec_t) exit_timestamp;
1981 i->pid = (pid_t) pid;
1988 typedef struct UnitStatusInfo {
1990 const char *load_state;
1991 const char *active_state;
1992 const char *sub_state;
1993 const char *unit_file_state;
1995 const char *description;
1996 const char *following;
1999 const char *default_control_group;
2001 const char *load_error;
2003 usec_t inactive_exit_timestamp;
2004 usec_t active_enter_timestamp;
2005 usec_t active_exit_timestamp;
2006 usec_t inactive_enter_timestamp;
2008 bool need_daemon_reload;
2013 const char *status_text;
2015 #ifdef HAVE_SYSV_COMPAT
2019 usec_t start_timestamp;
2020 usec_t exit_timestamp;
2022 int exit_code, exit_status;
2024 usec_t condition_timestamp;
2025 bool condition_result;
2028 unsigned n_accepted;
2029 unsigned n_connections;
2033 const char *sysfs_path;
2035 /* Mount, Automount */
2041 LIST_HEAD(ExecStatusInfo, exec);
2044 static void print_status_info(UnitStatusInfo *i) {
2046 const char *on, *off, *ss;
2048 char since1[FORMAT_TIMESTAMP_PRETTY_MAX], *s1;
2049 char since2[FORMAT_TIMESTAMP_MAX], *s2;
2053 /* This shows pretty information about a unit. See
2054 * print_property() for a low-level property printer */
2056 printf("%s", strna(i->id));
2058 if (i->description && !streq_ptr(i->id, i->description))
2059 printf(" - %s", i->description);
2064 printf("\t Follow: unit currently follows state of %s\n", i->following);
2066 if (streq_ptr(i->load_state, "failed") ||
2067 streq_ptr(i->load_state, "banned")) {
2068 on = ansi_highlight(true);
2069 off = ansi_highlight(false);
2074 printf("\t Loaded: %s%s%s (Reason: %s)\n", on, strna(i->load_state), off, i->load_error);
2075 else if (i->path && i->unit_file_state)
2076 printf("\t Loaded: %s%s%s (%s; %s)\n", on, strna(i->load_state), off, i->path, i->unit_file_state);
2078 printf("\t Loaded: %s%s%s (%s)\n", on, strna(i->load_state), off, i->path);
2080 printf("\t Loaded: %s%s%s\n", on, strna(i->load_state), off);
2082 ss = streq_ptr(i->active_state, i->sub_state) ? NULL : i->sub_state;
2084 if (streq_ptr(i->active_state, "failed")) {
2085 on = ansi_highlight(true);
2086 off = ansi_highlight(false);
2087 } else if (streq_ptr(i->active_state, "active") || streq_ptr(i->active_state, "reloading")) {
2088 on = ansi_highlight_green(true);
2089 off = ansi_highlight_green(false);
2094 printf("\t Active: %s%s (%s)%s",
2096 strna(i->active_state),
2100 printf("\t Active: %s%s%s",
2102 strna(i->active_state),
2105 timestamp = (streq_ptr(i->active_state, "active") ||
2106 streq_ptr(i->active_state, "reloading")) ? i->active_enter_timestamp :
2107 (streq_ptr(i->active_state, "inactive") ||
2108 streq_ptr(i->active_state, "failed")) ? i->inactive_enter_timestamp :
2109 streq_ptr(i->active_state, "activating") ? i->inactive_exit_timestamp :
2110 i->active_exit_timestamp;
2112 s1 = format_timestamp_pretty(since1, sizeof(since1), timestamp);
2113 s2 = format_timestamp(since2, sizeof(since2), timestamp);
2116 printf(" since %s; %s\n", s2, s1);
2118 printf(" since %s\n", s2);
2122 if (!i->condition_result && i->condition_timestamp > 0) {
2123 s1 = format_timestamp_pretty(since1, sizeof(since1), i->condition_timestamp);
2124 s2 = format_timestamp(since2, sizeof(since2), i->condition_timestamp);
2127 printf("\t start condition failed at %s; %s\n", s2, s1);
2129 printf("\t start condition failed at %s\n", s2);
2133 printf("\t Device: %s\n", i->sysfs_path);
2135 printf("\t Where: %s\n", i->where);
2137 printf("\t What: %s\n", i->what);
2140 printf("\tAccepted: %u; Connected: %u\n", i->n_accepted, i->n_connections);
2142 LIST_FOREACH(exec, p, i->exec) {
2146 /* Only show exited processes here */
2150 t = strv_join(p->argv, " ");
2151 printf("\t Process: %u %s=%s ", p->pid, p->name, strna(t));
2154 #ifdef HAVE_SYSV_COMPAT
2156 good = is_clean_exit_lsb(p->code, p->status);
2159 good = is_clean_exit(p->code, p->status);
2162 on = ansi_highlight(true);
2163 off = ansi_highlight(false);
2167 printf("%s(code=%s, ", on, sigchld_code_to_string(p->code));
2169 if (p->code == CLD_EXITED) {
2172 printf("status=%i", p->status);
2174 #ifdef HAVE_SYSV_COMPAT
2175 if ((c = exit_status_to_string(p->status, i->is_sysv ? EXIT_STATUS_LSB : EXIT_STATUS_SYSTEMD)))
2177 if ((c = exit_status_to_string(p->status, EXIT_STATUS_SYSTEMD)))
2182 printf("signal=%s", signal_to_string(p->status));
2184 printf(")%s\n", off);
2186 if (i->main_pid == p->pid &&
2187 i->start_timestamp == p->start_timestamp &&
2188 i->exit_timestamp == p->start_timestamp)
2189 /* Let's not show this twice */
2192 if (p->pid == i->control_pid)
2196 if (i->main_pid > 0 || i->control_pid > 0) {
2199 if (i->main_pid > 0) {
2200 printf("Main PID: %u", (unsigned) i->main_pid);
2204 get_process_comm(i->main_pid, &t);
2209 } else if (i->exit_code > 0) {
2210 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
2212 if (i->exit_code == CLD_EXITED) {
2215 printf("status=%i", i->exit_status);
2217 #ifdef HAVE_SYSV_COMPAT
2218 if ((c = exit_status_to_string(i->exit_status, i->is_sysv ? EXIT_STATUS_LSB : EXIT_STATUS_SYSTEMD)))
2220 if ((c = exit_status_to_string(i->exit_status, EXIT_STATUS_SYSTEMD)))
2225 printf("signal=%s", signal_to_string(i->exit_status));
2230 if (i->main_pid > 0 && i->control_pid > 0)
2233 if (i->control_pid > 0) {
2236 printf(" Control: %u", (unsigned) i->control_pid);
2238 get_process_comm(i->control_pid, &t);
2249 printf("\t Status: \"%s\"\n", i->status_text);
2251 if (i->default_control_group) {
2254 printf("\t CGroup: %s\n", i->default_control_group);
2256 if (arg_transport != TRANSPORT_SSH) {
2257 if ((c = columns()) > 18)
2262 show_cgroup_by_path(i->default_control_group, "\t\t ", c);
2266 if (i->need_daemon_reload)
2267 printf("\n%sWarning:%s Unit file changed on disk, 'systemctl %s daemon-reload' recommended.\n",
2268 ansi_highlight(true),
2269 ansi_highlight(false),
2270 arg_scope == UNIT_FILE_SYSTEM ? "--system" : "--user");
2273 static int status_property(const char *name, DBusMessageIter *iter, UnitStatusInfo *i) {
2279 switch (dbus_message_iter_get_arg_type(iter)) {
2281 case DBUS_TYPE_STRING: {
2284 dbus_message_iter_get_basic(iter, &s);
2287 if (streq(name, "Id"))
2289 else if (streq(name, "LoadState"))
2291 else if (streq(name, "ActiveState"))
2292 i->active_state = s;
2293 else if (streq(name, "SubState"))
2295 else if (streq(name, "Description"))
2297 else if (streq(name, "FragmentPath"))
2299 #ifdef HAVE_SYSV_COMPAT
2300 else if (streq(name, "SysVPath")) {
2305 else if (streq(name, "DefaultControlGroup"))
2306 i->default_control_group = s;
2307 else if (streq(name, "StatusText"))
2309 else if (streq(name, "SysFSPath"))
2311 else if (streq(name, "Where"))
2313 else if (streq(name, "What"))
2315 else if (streq(name, "Following"))
2317 else if (streq(name, "UnitFileState"))
2318 i->unit_file_state = s;
2324 case DBUS_TYPE_BOOLEAN: {
2327 dbus_message_iter_get_basic(iter, &b);
2329 if (streq(name, "Accept"))
2331 else if (streq(name, "NeedDaemonReload"))
2332 i->need_daemon_reload = b;
2333 else if (streq(name, "ConditionResult"))
2334 i->condition_result = b;
2339 case DBUS_TYPE_UINT32: {
2342 dbus_message_iter_get_basic(iter, &u);
2344 if (streq(name, "MainPID")) {
2346 i->main_pid = (pid_t) u;
2349 } else if (streq(name, "ControlPID"))
2350 i->control_pid = (pid_t) u;
2351 else if (streq(name, "ExecMainPID")) {
2353 i->main_pid = (pid_t) u;
2354 } else if (streq(name, "NAccepted"))
2356 else if (streq(name, "NConnections"))
2357 i->n_connections = u;
2362 case DBUS_TYPE_INT32: {
2365 dbus_message_iter_get_basic(iter, &j);
2367 if (streq(name, "ExecMainCode"))
2368 i->exit_code = (int) j;
2369 else if (streq(name, "ExecMainStatus"))
2370 i->exit_status = (int) j;
2375 case DBUS_TYPE_UINT64: {
2378 dbus_message_iter_get_basic(iter, &u);
2380 if (streq(name, "ExecMainStartTimestamp"))
2381 i->start_timestamp = (usec_t) u;
2382 else if (streq(name, "ExecMainExitTimestamp"))
2383 i->exit_timestamp = (usec_t) u;
2384 else if (streq(name, "ActiveEnterTimestamp"))
2385 i->active_enter_timestamp = (usec_t) u;
2386 else if (streq(name, "InactiveEnterTimestamp"))
2387 i->inactive_enter_timestamp = (usec_t) u;
2388 else if (streq(name, "InactiveExitTimestamp"))
2389 i->inactive_exit_timestamp = (usec_t) u;
2390 else if (streq(name, "ActiveExitTimestamp"))
2391 i->active_exit_timestamp = (usec_t) u;
2392 else if (streq(name, "ConditionTimestamp"))
2393 i->condition_timestamp = (usec_t) u;
2398 case DBUS_TYPE_ARRAY: {
2400 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT &&
2401 startswith(name, "Exec")) {
2402 DBusMessageIter sub;
2404 dbus_message_iter_recurse(iter, &sub);
2405 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2406 ExecStatusInfo *info;
2409 if (!(info = new0(ExecStatusInfo, 1)))
2412 if (!(info->name = strdup(name))) {
2417 if ((r = exec_status_info_deserialize(&sub, info)) < 0) {
2422 LIST_PREPEND(ExecStatusInfo, exec, i->exec, info);
2424 dbus_message_iter_next(&sub);
2431 case DBUS_TYPE_STRUCT: {
2433 if (streq(name, "LoadError")) {
2434 DBusMessageIter sub;
2435 const char *n, *message;
2438 dbus_message_iter_recurse(iter, &sub);
2440 r = bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &n, true);
2444 r = bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &message, false);
2448 if (!isempty(message))
2449 i->load_error = message;
2459 static int print_property(const char *name, DBusMessageIter *iter) {
2463 /* This is a low-level property printer, see
2464 * print_status_info() for the nicer output */
2466 if (arg_property && !strv_find(arg_property, name))
2469 switch (dbus_message_iter_get_arg_type(iter)) {
2471 case DBUS_TYPE_STRUCT: {
2472 DBusMessageIter sub;
2473 dbus_message_iter_recurse(iter, &sub);
2475 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "Job")) {
2478 dbus_message_iter_get_basic(&sub, &u);
2481 printf("%s=%u\n", name, (unsigned) u);
2483 printf("%s=\n", name);
2486 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Unit")) {
2489 dbus_message_iter_get_basic(&sub, &s);
2491 if (arg_all || s[0])
2492 printf("%s=%s\n", name, s);
2495 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "LoadError")) {
2496 const char *a = NULL, *b = NULL;
2498 if (bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &a, true) >= 0)
2499 bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &b, false);
2501 if (arg_all || !isempty(a) || !isempty(b))
2502 printf("%s=%s \"%s\"\n", name, strempty(a), strempty(b));
2510 case DBUS_TYPE_ARRAY:
2512 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "EnvironmentFiles")) {
2513 DBusMessageIter sub, sub2;
2515 dbus_message_iter_recurse(iter, &sub);
2516 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2520 dbus_message_iter_recurse(&sub, &sub2);
2522 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) >= 0 &&
2523 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_BOOLEAN, &ignore, false) >= 0)
2524 printf("EnvironmentFile=%s (ignore_errors=%s)\n", path, yes_no(ignore));
2526 dbus_message_iter_next(&sub);
2531 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Paths")) {
2532 DBusMessageIter sub, sub2;
2534 dbus_message_iter_recurse(iter, &sub);
2535 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2536 const char *type, *path;
2538 dbus_message_iter_recurse(&sub, &sub2);
2540 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
2541 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0)
2542 printf("%s=%s\n", type, path);
2544 dbus_message_iter_next(&sub);
2549 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Timers")) {
2550 DBusMessageIter sub, sub2;
2552 dbus_message_iter_recurse(iter, &sub);
2553 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2555 uint64_t value, next_elapse;
2557 dbus_message_iter_recurse(&sub, &sub2);
2559 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &base, true) >= 0 &&
2560 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &value, true) >= 0 &&
2561 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &next_elapse, false) >= 0) {
2562 char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX];
2564 printf("%s={ value=%s ; next_elapse=%s }\n",
2566 format_timespan(timespan1, sizeof(timespan1), value),
2567 format_timespan(timespan2, sizeof(timespan2), next_elapse));
2570 dbus_message_iter_next(&sub);
2575 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "ControlGroupAttributes")) {
2576 DBusMessageIter sub, sub2;
2578 dbus_message_iter_recurse(iter, &sub);
2579 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2580 const char *controller, *attr, *value;
2582 dbus_message_iter_recurse(&sub, &sub2);
2584 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &controller, true) >= 0 &&
2585 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &attr, true) >= 0 &&
2586 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &value, false) >= 0) {
2588 printf("ControlGroupAttribute={ controller=%s ; attribute=%s ; value=\"%s\" }\n",
2594 dbus_message_iter_next(&sub);
2599 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && startswith(name, "Exec")) {
2600 DBusMessageIter sub;
2602 dbus_message_iter_recurse(iter, &sub);
2603 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2604 ExecStatusInfo info;
2607 if (exec_status_info_deserialize(&sub, &info) >= 0) {
2608 char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
2611 t = strv_join(info.argv, " ");
2613 printf("%s={ path=%s ; argv[]=%s ; ignore_errors=%s ; start_time=[%s] ; stop_time=[%s] ; pid=%u ; code=%s ; status=%i%s%s }\n",
2617 yes_no(info.ignore),
2618 strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)),
2619 strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)),
2620 (unsigned) info. pid,
2621 sigchld_code_to_string(info.code),
2623 info.code == CLD_EXITED ? "" : "/",
2624 strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status)));
2630 strv_free(info.argv);
2632 dbus_message_iter_next(&sub);
2641 if (generic_print_property(name, iter, arg_all) > 0)
2645 printf("%s=[unprintable]\n", name);
2650 static int show_one(const char *verb, DBusConnection *bus, const char *path, bool show_properties, bool *new_line) {
2651 DBusMessage *m = NULL, *reply = NULL;
2652 const char *interface = "";
2655 DBusMessageIter iter, sub, sub2, sub3;
2656 UnitStatusInfo info;
2664 dbus_error_init(&error);
2666 if (!(m = dbus_message_new_method_call(
2667 "org.freedesktop.systemd1",
2669 "org.freedesktop.DBus.Properties",
2671 log_error("Could not allocate message.");
2676 if (!dbus_message_append_args(m,
2677 DBUS_TYPE_STRING, &interface,
2678 DBUS_TYPE_INVALID)) {
2679 log_error("Could not append arguments to message.");
2684 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2685 log_error("Failed to issue method call: %s", bus_error_message(&error));
2690 if (!dbus_message_iter_init(reply, &iter) ||
2691 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
2692 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
2693 log_error("Failed to parse reply.");
2698 dbus_message_iter_recurse(&iter, &sub);
2705 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2708 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
2709 log_error("Failed to parse reply.");
2714 dbus_message_iter_recurse(&sub, &sub2);
2716 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
2717 log_error("Failed to parse reply.");
2722 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
2723 log_error("Failed to parse reply.");
2728 dbus_message_iter_recurse(&sub2, &sub3);
2730 if (show_properties)
2731 r = print_property(name, &sub3);
2733 r = status_property(name, &sub3, &info);
2736 log_error("Failed to parse reply.");
2741 dbus_message_iter_next(&sub);
2746 if (!show_properties)
2747 print_status_info(&info);
2749 if (!streq_ptr(info.active_state, "active") &&
2750 !streq_ptr(info.active_state, "reloading") &&
2751 streq(verb, "status"))
2752 /* According to LSB: "program not running" */
2755 while ((p = info.exec)) {
2756 LIST_REMOVE(ExecStatusInfo, exec, info.exec, p);
2757 exec_status_info_free(p);
2762 dbus_message_unref(m);
2765 dbus_message_unref(reply);
2767 dbus_error_free(&error);
2772 static int show(DBusConnection *bus, char **args) {
2773 DBusMessage *m = NULL, *reply = NULL;
2776 bool show_properties, new_line = false;
2782 dbus_error_init(&error);
2784 show_properties = !streq(args[0], "status");
2786 if (show_properties)
2787 pager_open_if_enabled();
2789 if (show_properties && strv_length(args) <= 1) {
2790 /* If not argument is specified inspect the manager
2793 ret = show_one(args[0], bus, "/org/freedesktop/systemd1", show_properties, &new_line);
2797 STRV_FOREACH(name, args+1) {
2798 const char *path = NULL;
2801 if (safe_atou32(*name, &id) < 0) {
2803 /* Interpret as unit name */
2805 if (!(m = dbus_message_new_method_call(
2806 "org.freedesktop.systemd1",
2807 "/org/freedesktop/systemd1",
2808 "org.freedesktop.systemd1.Manager",
2810 log_error("Could not allocate message.");
2815 if (!dbus_message_append_args(m,
2816 DBUS_TYPE_STRING, name,
2817 DBUS_TYPE_INVALID)) {
2818 log_error("Could not append arguments to message.");
2823 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2825 if (!dbus_error_has_name(&error, DBUS_ERROR_ACCESS_DENIED)) {
2826 log_error("Failed to issue method call: %s", bus_error_message(&error));
2831 dbus_error_free(&error);
2833 dbus_message_unref(m);
2834 if (!(m = dbus_message_new_method_call(
2835 "org.freedesktop.systemd1",
2836 "/org/freedesktop/systemd1",
2837 "org.freedesktop.systemd1.Manager",
2839 log_error("Could not allocate message.");
2844 if (!dbus_message_append_args(m,
2845 DBUS_TYPE_STRING, name,
2846 DBUS_TYPE_INVALID)) {
2847 log_error("Could not append arguments to message.");
2852 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2853 log_error("Failed to issue method call: %s", bus_error_message(&error));
2855 if (dbus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT))
2856 ret = 4; /* According to LSB: "program or service status is unknown" */
2863 } else if (show_properties) {
2865 /* Interpret as job id */
2867 if (!(m = dbus_message_new_method_call(
2868 "org.freedesktop.systemd1",
2869 "/org/freedesktop/systemd1",
2870 "org.freedesktop.systemd1.Manager",
2872 log_error("Could not allocate message.");
2877 if (!dbus_message_append_args(m,
2878 DBUS_TYPE_UINT32, &id,
2879 DBUS_TYPE_INVALID)) {
2880 log_error("Could not append arguments to message.");
2885 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2886 log_error("Failed to issue method call: %s", bus_error_message(&error));
2892 /* Interpret as PID */
2894 if (!(m = dbus_message_new_method_call(
2895 "org.freedesktop.systemd1",
2896 "/org/freedesktop/systemd1",
2897 "org.freedesktop.systemd1.Manager",
2899 log_error("Could not allocate message.");
2904 if (!dbus_message_append_args(m,
2905 DBUS_TYPE_UINT32, &id,
2906 DBUS_TYPE_INVALID)) {
2907 log_error("Could not append arguments to message.");
2912 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2913 log_error("Failed to issue method call: %s", bus_error_message(&error));
2919 if (!dbus_message_get_args(reply, &error,
2920 DBUS_TYPE_OBJECT_PATH, &path,
2921 DBUS_TYPE_INVALID)) {
2922 log_error("Failed to parse reply: %s", bus_error_message(&error));
2927 if ((r = show_one(args[0], bus, path, show_properties, &new_line)) != 0)
2930 dbus_message_unref(m);
2931 dbus_message_unref(reply);
2937 dbus_message_unref(m);
2940 dbus_message_unref(reply);
2942 dbus_error_free(&error);
2947 static int dump(DBusConnection *bus, char **args) {
2948 DBusMessage *m = NULL, *reply = NULL;
2953 dbus_error_init(&error);
2955 pager_open_if_enabled();
2957 if (!(m = dbus_message_new_method_call(
2958 "org.freedesktop.systemd1",
2959 "/org/freedesktop/systemd1",
2960 "org.freedesktop.systemd1.Manager",
2962 log_error("Could not allocate message.");
2966 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2967 log_error("Failed to issue method call: %s", bus_error_message(&error));
2972 if (!dbus_message_get_args(reply, &error,
2973 DBUS_TYPE_STRING, &text,
2974 DBUS_TYPE_INVALID)) {
2975 log_error("Failed to parse reply: %s", bus_error_message(&error));
2980 fputs(text, stdout);
2986 dbus_message_unref(m);
2989 dbus_message_unref(reply);
2991 dbus_error_free(&error);
2996 static int snapshot(DBusConnection *bus, char **args) {
2997 DBusMessage *m = NULL, *reply = NULL;
3000 const char *name = "", *path, *id;
3001 dbus_bool_t cleanup = FALSE;
3002 DBusMessageIter iter, sub;
3004 *interface = "org.freedesktop.systemd1.Unit",
3007 dbus_error_init(&error);
3009 if (!(m = dbus_message_new_method_call(
3010 "org.freedesktop.systemd1",
3011 "/org/freedesktop/systemd1",
3012 "org.freedesktop.systemd1.Manager",
3013 "CreateSnapshot"))) {
3014 log_error("Could not allocate message.");
3018 if (strv_length(args) > 1)
3021 if (!dbus_message_append_args(m,
3022 DBUS_TYPE_STRING, &name,
3023 DBUS_TYPE_BOOLEAN, &cleanup,
3024 DBUS_TYPE_INVALID)) {
3025 log_error("Could not append arguments to message.");
3030 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3031 log_error("Failed to issue method call: %s", bus_error_message(&error));
3036 if (!dbus_message_get_args(reply, &error,
3037 DBUS_TYPE_OBJECT_PATH, &path,
3038 DBUS_TYPE_INVALID)) {
3039 log_error("Failed to parse reply: %s", bus_error_message(&error));
3044 dbus_message_unref(m);
3045 if (!(m = dbus_message_new_method_call(
3046 "org.freedesktop.systemd1",
3048 "org.freedesktop.DBus.Properties",
3050 log_error("Could not allocate message.");
3054 if (!dbus_message_append_args(m,
3055 DBUS_TYPE_STRING, &interface,
3056 DBUS_TYPE_STRING, &property,
3057 DBUS_TYPE_INVALID)) {
3058 log_error("Could not append arguments to message.");
3063 dbus_message_unref(reply);
3064 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3065 log_error("Failed to issue method call: %s", bus_error_message(&error));
3070 if (!dbus_message_iter_init(reply, &iter) ||
3071 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
3072 log_error("Failed to parse reply.");
3077 dbus_message_iter_recurse(&iter, &sub);
3079 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
3080 log_error("Failed to parse reply.");
3085 dbus_message_iter_get_basic(&sub, &id);
3093 dbus_message_unref(m);
3096 dbus_message_unref(reply);
3098 dbus_error_free(&error);
3103 static int delete_snapshot(DBusConnection *bus, char **args) {
3104 DBusMessage *m = NULL, *reply = NULL;
3112 dbus_error_init(&error);
3114 STRV_FOREACH(name, args+1) {
3115 const char *path = NULL;
3117 if (!(m = dbus_message_new_method_call(
3118 "org.freedesktop.systemd1",
3119 "/org/freedesktop/systemd1",
3120 "org.freedesktop.systemd1.Manager",
3122 log_error("Could not allocate message.");
3127 if (!dbus_message_append_args(m,
3128 DBUS_TYPE_STRING, name,
3129 DBUS_TYPE_INVALID)) {
3130 log_error("Could not append arguments to message.");
3135 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3136 log_error("Failed to issue method call: %s", bus_error_message(&error));
3141 if (!dbus_message_get_args(reply, &error,
3142 DBUS_TYPE_OBJECT_PATH, &path,
3143 DBUS_TYPE_INVALID)) {
3144 log_error("Failed to parse reply: %s", bus_error_message(&error));