1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
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>
34 #include <dbus/dbus.h>
40 #include "utmp-wtmp.h"
44 #include "dbus-common.h"
45 #include "cgroup-show.h"
46 #include "cgroup-util.h"
49 static const char *arg_type = NULL;
50 static const char *arg_property = NULL;
51 static bool arg_all = false;
52 static bool arg_fail = false;
53 static bool arg_session = false;
54 static bool arg_no_block = false;
55 static bool arg_immediate = false;
56 static bool arg_no_wtmp = false;
57 static bool arg_no_sync = false;
58 static bool arg_no_wall = false;
59 static bool arg_dry = false;
60 static bool arg_quiet = false;
61 static char arg_full = false;
62 static char **arg_wall = NULL;
80 } arg_action = ACTION_SYSTEMCTL;
87 static bool private_bus = false;
89 static const char *ansi_highlight(bool b) {
92 if (_unlikely_(t < 0))
93 t = isatty(STDOUT_FILENO) > 0;
98 return b ? ANSI_HIGHLIGHT_ON : ANSI_HIGHLIGHT_OFF;
101 static bool error_is_no_service(DBusError *error) {
105 if (!dbus_error_is_set(error))
108 if (dbus_error_has_name(error, DBUS_ERROR_NAME_HAS_NO_OWNER))
111 if (dbus_error_has_name(error, DBUS_ERROR_SERVICE_UNKNOWN))
114 return startswith(error->name, "org.freedesktop.DBus.Error.Spawn.");
117 static int bus_iter_get_basic_and_next(DBusMessageIter *iter, int type, void *data, bool next) {
122 if (dbus_message_iter_get_arg_type(iter) != type)
125 dbus_message_iter_get_basic(iter, data);
127 if (!dbus_message_iter_next(iter) != !next)
133 static void warn_wall(enum action action) {
134 static const char *table[_ACTION_MAX] = {
135 [ACTION_HALT] = "The system is going down for system halt NOW!",
136 [ACTION_REBOOT] = "The system is going down for reboot NOW!",
137 [ACTION_POWEROFF] = "The system is going down for power-off NOW!",
138 [ACTION_RESCUE] = "The system is going down to rescue mode NOW!",
139 [ACTION_EMERGENCY] = "The system is going down to emergency mode NOW!"
148 if (!(p = strv_join(arg_wall, " "))) {
149 log_error("Failed to join strings.");
165 utmp_wall(table[action]);
168 static int list_units(DBusConnection *bus, char **args, unsigned n) {
169 DBusMessage *m = NULL, *reply = NULL;
172 DBusMessageIter iter, sub, sub2;
175 dbus_error_init(&error);
179 if (!(m = dbus_message_new_method_call(
180 "org.freedesktop.systemd1",
181 "/org/freedesktop/systemd1",
182 "org.freedesktop.systemd1.Manager",
184 log_error("Could not allocate message.");
188 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
189 log_error("Failed to issue method call: %s", error.message);
194 if (!dbus_message_iter_init(reply, &iter) ||
195 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
196 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
197 log_error("Failed to parse reply.");
202 dbus_message_iter_recurse(&iter, &sub);
204 if (isatty(STDOUT_FILENO))
205 printf("%-45s %-6s %-12s %-12s %-15s %s\n", "UNIT", "LOAD", "ACTIVE", "SUB", "JOB", "DESCRIPTION");
207 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
208 const char *id, *description, *load_state, *active_state, *sub_state, *following, *unit_path, *job_type, *job_path, *dot;
211 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
212 log_error("Failed to parse reply.");
217 dbus_message_iter_recurse(&sub, &sub2);
219 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
220 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &description, true) < 0 ||
221 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &load_state, true) < 0 ||
222 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &active_state, true) < 0 ||
223 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &sub_state, true) < 0 ||
224 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &following, true) < 0 ||
225 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, true) < 0 ||
226 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &job_id, true) < 0 ||
227 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &job_type, true) < 0 ||
228 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, false) < 0) {
229 log_error("Failed to parse reply.");
234 if ((!arg_type || ((dot = strrchr(id, '.')) &&
235 streq(dot+1, arg_type))) &&
236 (arg_all || !(streq(active_state, "inactive") || following[0]) || job_id > 0)) {
240 if (streq(active_state, "maintenance"))
241 fputs(ansi_highlight(true), stdout);
243 e = arg_full ? NULL : ellipsize(id, 45, 33);
244 printf("%-45s %-6s %-12s %-12s%n", e ? e : id, load_state, active_state, sub_state, &a);
248 printf(" => %-12s%n", job_type, &b);
252 if (a + b + 2 < columns()) {
256 printf(" %.*s", columns() - a - b - 2, description);
259 if (streq(active_state, "maintenance"))
260 fputs(ansi_highlight(false), stdout);
266 dbus_message_iter_next(&sub);
269 if (isatty(STDOUT_FILENO)) {
271 printf("\nLOAD = Load State, reflects whether the unit configuration was properly loaded.\n"
272 "ACTIVE = Active State, the high-level unit activation state, i.e. generalization of the substate.\n"
273 "SUB = Substate, the low-level unit activation state, possible values depend on unit type.\n"
274 "JOB = Job, shows pending jobs for the unit.\n");
277 printf("\n%u units listed.\n", k);
279 printf("\n%u units listed. Pass --all to see inactive units, too.\n", k);
286 dbus_message_unref(m);
289 dbus_message_unref(reply);
291 dbus_error_free(&error);
296 static int dot_one_property(const char *name, const char *prop, DBusMessageIter *iter) {
297 static const char * const colors[] = {
298 "Requires", "[color=\"black\"]",
299 "RequiresOverridable", "[color=\"black\"]",
300 "Requisite", "[color=\"darkblue\"]",
301 "RequisiteOverridable", "[color=\"darkblue\"]",
302 "Wants", "[color=\"darkgrey\"]",
303 "Conflicts", "[color=\"red\"]",
304 "After", "[color=\"green\"]"
307 const char *c = NULL;
314 for (i = 0; i < ELEMENTSOF(colors); i += 2)
315 if (streq(colors[i], prop)) {
323 if (arg_dot != DOT_ALL)
324 if ((arg_dot == DOT_ORDER) != streq(prop, "After"))
327 switch (dbus_message_iter_get_arg_type(iter)) {
329 case DBUS_TYPE_ARRAY:
331 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
334 dbus_message_iter_recurse(iter, &sub);
336 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
339 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
340 dbus_message_iter_get_basic(&sub, &s);
341 printf("\t\"%s\"->\"%s\" %s;\n", name, s, c);
343 dbus_message_iter_next(&sub);
353 static int dot_one(DBusConnection *bus, const char *name, const char *path) {
354 DBusMessage *m = NULL, *reply = NULL;
355 const char *interface = "org.freedesktop.systemd1.Unit";
358 DBusMessageIter iter, sub, sub2, sub3;
363 dbus_error_init(&error);
365 if (!(m = dbus_message_new_method_call(
366 "org.freedesktop.systemd1",
368 "org.freedesktop.DBus.Properties",
370 log_error("Could not allocate message.");
375 if (!dbus_message_append_args(m,
376 DBUS_TYPE_STRING, &interface,
377 DBUS_TYPE_INVALID)) {
378 log_error("Could not append arguments to message.");
383 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
384 log_error("Failed to issue method call: %s", error.message);
389 if (!dbus_message_iter_init(reply, &iter) ||
390 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
391 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
392 log_error("Failed to parse reply.");
397 dbus_message_iter_recurse(&iter, &sub);
399 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
402 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
403 log_error("Failed to parse reply.");
408 dbus_message_iter_recurse(&sub, &sub2);
410 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &prop, true) < 0) {
411 log_error("Failed to parse reply.");
416 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
417 log_error("Failed to parse reply.");
422 dbus_message_iter_recurse(&sub2, &sub3);
424 if (dot_one_property(name, prop, &sub3)) {
425 log_error("Failed to parse reply.");
430 dbus_message_iter_next(&sub);
435 dbus_message_unref(m);
438 dbus_message_unref(reply);
440 dbus_error_free(&error);
445 static int dot(DBusConnection *bus, char **args, unsigned n) {
446 DBusMessage *m = NULL, *reply = NULL;
449 DBusMessageIter iter, sub, sub2;
451 dbus_error_init(&error);
455 if (!(m = dbus_message_new_method_call(
456 "org.freedesktop.systemd1",
457 "/org/freedesktop/systemd1",
458 "org.freedesktop.systemd1.Manager",
460 log_error("Could not allocate message.");
464 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
465 log_error("Failed to issue method call: %s", error.message);
470 if (!dbus_message_iter_init(reply, &iter) ||
471 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
472 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
473 log_error("Failed to parse reply.");
478 printf("digraph systemd {\n");
480 dbus_message_iter_recurse(&iter, &sub);
481 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
482 const char *id, *description, *load_state, *active_state, *sub_state, *unit_path;
484 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
485 log_error("Failed to parse reply.");
490 dbus_message_iter_recurse(&sub, &sub2);
492 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
493 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &description, true) < 0 ||
494 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &load_state, true) < 0 ||
495 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &active_state, true) < 0 ||
496 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &sub_state, true) < 0 ||
497 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, true) < 0) {
498 log_error("Failed to parse reply.");
503 if ((r = dot_one(bus, id, unit_path)) < 0)
506 /* printf("\t\"%s\";\n", id); */
507 dbus_message_iter_next(&sub);
512 log_info(" Color legend: black = Requires\n"
513 " dark blue = Requisite\n"
514 " dark grey = Wants\n"
518 if (isatty(fileno(stdout)))
519 log_notice("-- You probably want to process this output with graphviz' dot tool.\n"
520 "-- Try a shell pipeline like 'systemctl dot | dot -Tsvg > systemd.svg'!\n");
526 dbus_message_unref(m);
529 dbus_message_unref(reply);
531 dbus_error_free(&error);
536 static int list_jobs(DBusConnection *bus, char **args, unsigned n) {
537 DBusMessage *m = NULL, *reply = NULL;
540 DBusMessageIter iter, sub, sub2;
543 dbus_error_init(&error);
547 if (!(m = dbus_message_new_method_call(
548 "org.freedesktop.systemd1",
549 "/org/freedesktop/systemd1",
550 "org.freedesktop.systemd1.Manager",
552 log_error("Could not allocate message.");
556 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
557 log_error("Failed to issue method call: %s", error.message);
562 if (!dbus_message_iter_init(reply, &iter) ||
563 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
564 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
565 log_error("Failed to parse reply.");
570 dbus_message_iter_recurse(&iter, &sub);
572 printf("%4s %-45s %-17s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
574 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
575 const char *name, *type, *state, *job_path, *unit_path;
579 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
580 log_error("Failed to parse reply.");
585 dbus_message_iter_recurse(&sub, &sub2);
587 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &id, true) < 0 ||
588 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0 ||
589 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
590 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, true) < 0 ||
591 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, true) < 0 ||
592 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, false) < 0) {
593 log_error("Failed to parse reply.");
598 e = arg_full ? NULL : ellipsize(name, 45, 33);
599 printf("%4u %-45s %-17s %-7s\n", id, e ? e : name, type, state);
604 dbus_message_iter_next(&sub);
607 printf("\n%u jobs listed.\n", k);
612 dbus_message_unref(m);
615 dbus_message_unref(reply);
617 dbus_error_free(&error);
622 static int load_unit(DBusConnection *bus, char **args, unsigned n) {
623 DBusMessage *m = NULL, *reply = NULL;
628 dbus_error_init(&error);
633 for (i = 1; i < n; i++) {
635 if (!(m = dbus_message_new_method_call(
636 "org.freedesktop.systemd1",
637 "/org/freedesktop/systemd1",
638 "org.freedesktop.systemd1.Manager",
640 log_error("Could not allocate message.");
645 if (!dbus_message_append_args(m,
646 DBUS_TYPE_STRING, &args[i],
647 DBUS_TYPE_INVALID)) {
648 log_error("Could not append arguments to message.");
653 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
654 log_error("Failed to issue method call: %s", error.message);
659 dbus_message_unref(m);
660 dbus_message_unref(reply);
669 dbus_message_unref(m);
672 dbus_message_unref(reply);
674 dbus_error_free(&error);
679 static int cancel_job(DBusConnection *bus, char **args, unsigned n) {
680 DBusMessage *m = NULL, *reply = NULL;
685 dbus_error_init(&error);
690 for (i = 1; i < n; i++) {
694 if (!(m = dbus_message_new_method_call(
695 "org.freedesktop.systemd1",
696 "/org/freedesktop/systemd1",
697 "org.freedesktop.systemd1.Manager",
699 log_error("Could not allocate message.");
704 if ((r = safe_atou(args[i], &id)) < 0) {
705 log_error("Failed to parse job id: %s", strerror(-r));
709 assert_cc(sizeof(uint32_t) == sizeof(id));
710 if (!dbus_message_append_args(m,
711 DBUS_TYPE_UINT32, &id,
712 DBUS_TYPE_INVALID)) {
713 log_error("Could not append arguments to message.");
718 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
719 log_error("Failed to issue method call: %s", error.message);
724 if (!dbus_message_get_args(reply, &error,
725 DBUS_TYPE_OBJECT_PATH, &path,
726 DBUS_TYPE_INVALID)) {
727 log_error("Failed to parse reply: %s", error.message);
732 dbus_message_unref(m);
733 if (!(m = dbus_message_new_method_call(
734 "org.freedesktop.systemd1",
736 "org.freedesktop.systemd1.Job",
738 log_error("Could not allocate message.");
743 dbus_message_unref(reply);
744 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
745 log_error("Failed to issue method call: %s", error.message);
750 dbus_message_unref(m);
751 dbus_message_unref(reply);
759 dbus_message_unref(m);
762 dbus_message_unref(reply);
764 dbus_error_free(&error);
769 static bool unit_need_daemon_reload(DBusConnection *bus, const char *unit) {
770 DBusMessage *m, *reply;
771 dbus_bool_t b = FALSE;
772 DBusMessageIter iter, sub;
774 *interface = "org.freedesktop.systemd1.Unit",
775 *property = "NeedDaemonReload",
778 /* We ignore all errors here, since this is used to show a warning only */
780 if (!(m = dbus_message_new_method_call(
781 "org.freedesktop.systemd1",
782 "/org/freedesktop/systemd1",
783 "org.freedesktop.systemd1.Manager",
787 if (!dbus_message_append_args(m,
788 DBUS_TYPE_STRING, &unit,
792 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL)))
795 if (!dbus_message_get_args(reply, NULL,
796 DBUS_TYPE_OBJECT_PATH, &path,
800 dbus_message_unref(m);
801 if (!(m = dbus_message_new_method_call(
802 "org.freedesktop.systemd1",
804 "org.freedesktop.DBus.Properties",
808 if (!dbus_message_append_args(m,
809 DBUS_TYPE_STRING, &interface,
810 DBUS_TYPE_STRING, &property,
811 DBUS_TYPE_INVALID)) {
815 dbus_message_unref(reply);
816 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL)))
819 if (!dbus_message_iter_init(reply, &iter) ||
820 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
823 dbus_message_iter_recurse(&iter, &sub);
825 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_BOOLEAN)
828 dbus_message_iter_get_basic(&sub, &b);
832 dbus_message_unref(m);
835 dbus_message_unref(reply);
840 typedef struct WaitData {
845 static DBusHandlerResult wait_filter(DBusConnection *connection, DBusMessage *message, void *data) {
853 dbus_error_init(&error);
855 log_debug("Got D-Bus request: %s.%s() on %s",
856 dbus_message_get_interface(message),
857 dbus_message_get_member(message),
858 dbus_message_get_path(message));
860 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
861 log_error("Warning! D-Bus connection terminated.");
862 dbus_connection_close(connection);
864 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
867 dbus_bool_t success = true;
869 if (!dbus_message_get_args(message, &error,
870 DBUS_TYPE_UINT32, &id,
871 DBUS_TYPE_OBJECT_PATH, &path,
872 DBUS_TYPE_BOOLEAN, &success,
874 log_error("Failed to parse message: %s", error.message);
878 if ((p = set_remove(d->set, (char*) path)))
886 dbus_error_free(&error);
887 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
890 static int enable_wait_for_jobs(DBusConnection *bus) {
898 dbus_error_init(&error);
899 dbus_bus_add_match(bus,
901 "sender='org.freedesktop.systemd1',"
902 "interface='org.freedesktop.systemd1.Manager',"
903 "member='JobRemoved',"
904 "path='/org/freedesktop/systemd1'",
907 if (dbus_error_is_set(&error)) {
908 log_error("Failed to add match: %s", error.message);
909 dbus_error_free(&error);
913 /* This is slightly dirty, since we don't undo the match registrations. */
917 static int wait_for_jobs(DBusConnection *bus, Set *s) {
928 if (!dbus_connection_add_filter(bus, wait_filter, &d, NULL)) {
929 log_error("Failed to add filter.");
934 while (!set_isempty(s) &&
935 dbus_connection_read_write_dispatch(bus, -1))
938 if (!arg_quiet && d.failed)
939 log_error("Job failed, see logs for details.");
941 r = d.failed ? -EIO : 0;
944 /* This is slightly dirty, since we don't undo the filter registration. */
949 static int start_unit_one(
956 DBusMessage *m = NULL, *reply = NULL;
965 assert(arg_no_block || s);
967 dbus_error_init(&error);
969 if (!(m = dbus_message_new_method_call(
970 "org.freedesktop.systemd1",
971 "/org/freedesktop/systemd1",
972 "org.freedesktop.systemd1.Manager",
974 log_error("Could not allocate message.");
979 if (!dbus_message_append_args(m,
980 DBUS_TYPE_STRING, &name,
981 DBUS_TYPE_STRING, &mode,
982 DBUS_TYPE_INVALID)) {
983 log_error("Could not append arguments to message.");
988 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
990 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
991 /* There's always a fallback possible for
997 log_error("Failed to issue method call: %s", error.message);
1002 if (!dbus_message_get_args(reply, &error,
1003 DBUS_TYPE_OBJECT_PATH, &path,
1004 DBUS_TYPE_INVALID)) {
1005 log_error("Failed to parse reply: %s", error.message);
1010 if (unit_need_daemon_reload(bus, name))
1011 log_warning("Unit file of created job changed on disk, 'systemctl %s daemon-reload' recommended.",
1012 arg_session ? "--session" : "--system");
1014 if (!arg_no_block) {
1017 if (!(p = strdup(path))) {
1018 log_error("Failed to duplicate path.");
1023 if ((r = set_put(s, p)) < 0) {
1025 log_error("Failed to add path to set.");
1034 dbus_message_unref(m);
1037 dbus_message_unref(reply);
1039 dbus_error_free(&error);
1044 static enum action verb_to_action(const char *verb) {
1045 if (streq(verb, "halt"))
1047 else if (streq(verb, "poweroff"))
1048 return ACTION_POWEROFF;
1049 else if (streq(verb, "reboot"))
1050 return ACTION_REBOOT;
1051 else if (streq(verb, "rescue"))
1052 return ACTION_RESCUE;
1053 else if (streq(verb, "emergency"))
1054 return ACTION_EMERGENCY;
1055 else if (streq(verb, "default"))
1056 return ACTION_DEFAULT;
1058 return ACTION_INVALID;
1061 static int start_unit(DBusConnection *bus, char **args, unsigned n) {
1063 static const char * const table[_ACTION_MAX] = {
1064 [ACTION_HALT] = SPECIAL_HALT_TARGET,
1065 [ACTION_POWEROFF] = SPECIAL_POWEROFF_TARGET,
1066 [ACTION_REBOOT] = SPECIAL_REBOOT_TARGET,
1067 [ACTION_RUNLEVEL2] = SPECIAL_RUNLEVEL2_TARGET,
1068 [ACTION_RUNLEVEL3] = SPECIAL_RUNLEVEL3_TARGET,
1069 [ACTION_RUNLEVEL4] = SPECIAL_RUNLEVEL4_TARGET,
1070 [ACTION_RUNLEVEL5] = SPECIAL_RUNLEVEL5_TARGET,
1071 [ACTION_RESCUE] = SPECIAL_RESCUE_TARGET,
1072 [ACTION_EMERGENCY] = SPECIAL_EMERGENCY_TARGET,
1073 [ACTION_DEFAULT] = SPECIAL_DEFAULT_TARGET
1078 const char *method, *mode, *one_name;
1083 if (arg_action == ACTION_SYSTEMCTL) {
1085 streq(args[0], "stop") ? "StopUnit" :
1086 streq(args[0], "reload") ? "ReloadUnit" :
1087 streq(args[0], "restart") ? "RestartUnit" :
1088 streq(args[0], "try-restart") ? "TryRestartUnit" :
1089 streq(args[0], "reload-or-restart") ? "ReloadOrRestartUnit" :
1090 streq(args[0], "reload-or-try-restart") ? "ReloadOrTryRestartUnit" :
1094 (streq(args[0], "isolate") ||
1095 streq(args[0], "rescue") ||
1096 streq(args[0], "emergency")) ? "isolate" :
1100 one_name = table[verb_to_action(args[0])];
1103 assert(arg_action < ELEMENTSOF(table));
1104 assert(table[arg_action]);
1106 method = "StartUnit";
1108 mode = (arg_action == ACTION_EMERGENCY ||
1109 arg_action == ACTION_RESCUE) ? "isolate" : "replace";
1111 one_name = table[arg_action];
1114 if (!arg_no_block) {
1115 if ((r = enable_wait_for_jobs(bus)) < 0) {
1116 log_error("Could not watch jobs: %s", strerror(-r));
1120 if (!(s = set_new(string_hash_func, string_compare_func))) {
1121 log_error("Failed to allocate set.");
1130 if ((r = start_unit_one(bus, method, one_name, mode, s)) <= 0)
1133 for (i = 1; i < n; i++)
1134 if ((r = start_unit_one(bus, method, args[i], mode, s)) < 0)
1139 r = wait_for_jobs(bus, s);
1148 static int start_special(DBusConnection *bus, char **args, unsigned n) {
1154 r = start_unit(bus, args, n);
1157 warn_wall(verb_to_action(args[0]));
1162 static int check_unit(DBusConnection *bus, char **args, unsigned n) {
1163 DBusMessage *m = NULL, *reply = NULL;
1165 *interface = "org.freedesktop.systemd1.Unit",
1166 *property = "ActiveState";
1167 int r = -EADDRNOTAVAIL;
1174 dbus_error_init(&error);
1176 for (i = 1; i < n; i++) {
1177 const char *path = NULL;
1179 DBusMessageIter iter, sub;
1181 if (!(m = dbus_message_new_method_call(
1182 "org.freedesktop.systemd1",
1183 "/org/freedesktop/systemd1",
1184 "org.freedesktop.systemd1.Manager",
1186 log_error("Could not allocate message.");
1191 if (!dbus_message_append_args(m,
1192 DBUS_TYPE_STRING, &args[i],
1193 DBUS_TYPE_INVALID)) {
1194 log_error("Could not append arguments to message.");
1199 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1201 /* Hmm, cannot figure out anything about this unit... */
1205 dbus_error_free(&error);
1209 if (!dbus_message_get_args(reply, &error,
1210 DBUS_TYPE_OBJECT_PATH, &path,
1211 DBUS_TYPE_INVALID)) {
1212 log_error("Failed to parse reply: %s", error.message);
1217 dbus_message_unref(m);
1218 if (!(m = dbus_message_new_method_call(
1219 "org.freedesktop.systemd1",
1221 "org.freedesktop.DBus.Properties",
1223 log_error("Could not allocate message.");
1228 if (!dbus_message_append_args(m,
1229 DBUS_TYPE_STRING, &interface,
1230 DBUS_TYPE_STRING, &property,
1231 DBUS_TYPE_INVALID)) {
1232 log_error("Could not append arguments to message.");
1237 dbus_message_unref(reply);
1238 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1239 log_error("Failed to issue method call: %s", error.message);
1244 if (!dbus_message_iter_init(reply, &iter) ||
1245 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1246 log_error("Failed to parse reply.");
1251 dbus_message_iter_recurse(&iter, &sub);
1253 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1254 log_error("Failed to parse reply.");
1259 dbus_message_iter_get_basic(&sub, &state);
1264 if (streq(state, "active") || startswith(state, "reloading"))
1267 dbus_message_unref(m);
1268 dbus_message_unref(reply);
1274 dbus_message_unref(m);
1277 dbus_message_unref(reply);
1279 dbus_error_free(&error);
1284 typedef struct ExecStatusInfo {
1290 usec_t start_timestamp;
1291 usec_t exit_timestamp;
1296 LIST_FIELDS(struct ExecStatusInfo, exec);
1299 static void exec_status_info_free(ExecStatusInfo *i) {
1307 static int exec_status_info_deserialize(DBusMessageIter *sub, ExecStatusInfo *i) {
1308 uint64_t start_timestamp, exit_timestamp;
1309 DBusMessageIter sub2, sub3;
1313 int32_t code, status;
1319 if (dbus_message_iter_get_arg_type(sub) != DBUS_TYPE_STRUCT)
1322 dbus_message_iter_recurse(sub, &sub2);
1324 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0)
1327 if (!(i->path = strdup(path)))
1330 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_ARRAY ||
1331 dbus_message_iter_get_element_type(&sub2) != DBUS_TYPE_STRING)
1335 dbus_message_iter_recurse(&sub2, &sub3);
1336 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1337 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1338 dbus_message_iter_next(&sub3);
1343 if (!(i->argv = new0(char*, n+1)))
1347 dbus_message_iter_recurse(&sub2, &sub3);
1348 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1351 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1352 dbus_message_iter_get_basic(&sub3, &s);
1353 dbus_message_iter_next(&sub3);
1355 if (!(i->argv[n++] = strdup(s)))
1359 if (!dbus_message_iter_next(&sub2) ||
1360 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_BOOLEAN, &ignore, true) < 0 ||
1361 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_timestamp, true) < 0 ||
1362 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_timestamp, true) < 0 ||
1363 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, true) < 0 ||
1364 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &code, true) < 0 ||
1365 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &status, false) < 0)
1369 i->start_timestamp = (usec_t) start_timestamp;
1370 i->exit_timestamp = (usec_t) exit_timestamp;
1371 i->pid = (pid_t) pid;
1378 typedef struct UnitStatusInfo {
1380 const char *load_state;
1381 const char *active_state;
1382 const char *sub_state;
1384 const char *description;
1386 const char *fragment_path;
1387 const char *default_control_group;
1389 bool need_daemon_reload;
1394 const char *status_text;
1397 usec_t start_timestamp;
1398 usec_t exit_timestamp;
1400 int exit_code, exit_status;
1403 unsigned n_accepted;
1404 unsigned n_connections;
1408 const char *sysfs_path;
1410 /* Mount, Automount */
1416 LIST_HEAD(ExecStatusInfo, exec);
1419 static void print_status_info(UnitStatusInfo *i) {
1424 /* This shows pretty information about a unit. See
1425 * print_property() for a low-level property printer */
1427 printf("%s", strna(i->id));
1429 if (i->description && !streq_ptr(i->id, i->description))
1430 printf(" - %s", i->description);
1434 if (i->fragment_path)
1435 printf("\t Loaded: %s (%s)\n", strna(i->load_state), i->fragment_path);
1436 else if (streq_ptr(i->load_state, "failed"))
1437 printf("\t Loaded: %s%s%s\n",
1438 ansi_highlight(true),
1439 strna(i->load_state),
1440 ansi_highlight(false));
1442 printf("\t Loaded: %s\n", strna(i->load_state));
1444 if (streq_ptr(i->active_state, "maintenance")) {
1445 if (streq_ptr(i->active_state, i->sub_state))
1446 printf("\t Active: %s%s%s\n",
1447 ansi_highlight(true),
1448 strna(i->active_state),
1449 ansi_highlight(false));
1451 printf("\t Active: %s%s (%s)%s\n",
1452 ansi_highlight(true),
1453 strna(i->active_state),
1454 strna(i->sub_state),
1455 ansi_highlight(false));
1457 if (streq_ptr(i->active_state, i->sub_state))
1458 printf("\t Active: %s\n",
1459 strna(i->active_state));
1461 printf("\t Active: %s (%s)\n",
1462 strna(i->active_state),
1463 strna(i->sub_state));
1467 printf("\t Device: %s\n", i->sysfs_path);
1469 printf("\t Where: %s\n", i->where);
1471 printf("\t What: %s\n", i->what);
1474 printf("\tAccepted: %u; Connected: %u\n", i->n_accepted, i->n_connections);
1476 LIST_FOREACH(exec, p, i->exec) {
1479 /* Only show exited processes here */
1483 t = strv_join(p->argv, " ");
1484 printf("\t Exited: %u (%s, code=%s, ", p->pid, strna(t), sigchld_code_to_string(p->code));
1487 if (p->code == CLD_EXITED)
1488 printf("status=%i", p->status);
1490 printf("signal=%s", signal_to_string(p->status));
1493 if (i->main_pid == p->pid &&
1494 i->start_timestamp == p->start_timestamp &&
1495 i->exit_timestamp == p->start_timestamp)
1496 /* Let's not show this twice */
1499 if (p->pid == i->control_pid)
1503 if (i->main_pid > 0 || i->control_pid > 0) {
1506 if (i->main_pid > 0) {
1507 printf(" Main: %u", (unsigned) i->main_pid);
1511 get_process_name(i->main_pid, &t);
1517 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
1519 if (i->exit_code == CLD_EXITED)
1520 printf("status=%i", i->exit_status);
1522 printf("signal=%s", signal_to_string(i->exit_status));
1526 if (i->main_pid > 0 && i->control_pid > 0)
1529 if (i->control_pid > 0) {
1532 printf(" Control: %u", (unsigned) i->control_pid);
1534 get_process_name(i->control_pid, &t);
1545 printf("\t Status: \"%s\"\n", i->status_text);
1547 if (i->default_control_group) {
1550 printf("\t CGroup: %s\n", i->default_control_group);
1552 if ((c = columns()) > 18)
1557 show_cgroup_by_path(i->default_control_group, "\t\t ", c);
1560 if (i->need_daemon_reload)
1561 printf("\n%sWarning:%s Unit file changed on disk, 'systemctl %s daemon-reload' recommended.\n",
1562 ansi_highlight(true),
1563 ansi_highlight(false),
1564 arg_session ? "--session" : "--system");
1567 static int status_property(const char *name, DBusMessageIter *iter, UnitStatusInfo *i) {
1569 switch (dbus_message_iter_get_arg_type(iter)) {
1571 case DBUS_TYPE_STRING: {
1574 dbus_message_iter_get_basic(iter, &s);
1577 if (streq(name, "Id"))
1579 else if (streq(name, "LoadState"))
1581 else if (streq(name, "ActiveState"))
1582 i->active_state = s;
1583 else if (streq(name, "SubState"))
1585 else if (streq(name, "Description"))
1587 else if (streq(name, "FragmentPath"))
1588 i->fragment_path = s;
1589 else if (streq(name, "DefaultControlGroup"))
1590 i->default_control_group = s;
1591 else if (streq(name, "StatusText"))
1593 else if (streq(name, "SysFSPath"))
1595 else if (streq(name, "Where"))
1597 else if (streq(name, "What"))
1604 case DBUS_TYPE_BOOLEAN: {
1607 dbus_message_iter_get_basic(iter, &b);
1609 if (streq(name, "Accept"))
1611 else if (streq(name, "NeedDaemonReload"))
1612 i->need_daemon_reload = b;
1617 case DBUS_TYPE_UINT32: {
1620 dbus_message_iter_get_basic(iter, &u);
1622 if (streq(name, "MainPID")) {
1624 i->main_pid = (pid_t) u;
1627 } else if (streq(name, "ControlPID"))
1628 i->control_pid = (pid_t) u;
1629 else if (streq(name, "ExecMainPID")) {
1631 i->main_pid = (pid_t) u;
1632 } else if (streq(name, "NAccepted"))
1634 else if (streq(name, "NConnections"))
1635 i->n_connections = u;
1640 case DBUS_TYPE_INT32: {
1643 dbus_message_iter_get_basic(iter, &j);
1645 if (streq(name, "ExecMainCode"))
1646 i->exit_code = (int) j;
1647 else if (streq(name, "ExecMainStatus"))
1648 i->exit_status = (int) j;
1653 case DBUS_TYPE_UINT64: {
1656 dbus_message_iter_get_basic(iter, &u);
1658 if (streq(name, "ExecMainStartTimestamp"))
1659 i->start_timestamp = (usec_t) u;
1660 else if (streq(name, "ExecMainExitTimestamp"))
1661 i->exit_timestamp = (usec_t) u;
1666 case DBUS_TYPE_ARRAY: {
1668 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT &&
1669 startswith(name, "Exec")) {
1670 DBusMessageIter sub;
1672 dbus_message_iter_recurse(iter, &sub);
1673 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1674 ExecStatusInfo *info;
1677 if (!(info = new0(ExecStatusInfo, 1)))
1680 if ((r = exec_status_info_deserialize(&sub, info)) < 0) {
1685 LIST_PREPEND(ExecStatusInfo, exec, i->exec, info);
1687 dbus_message_iter_next(&sub);
1698 static int print_property(const char *name, DBusMessageIter *iter) {
1702 /* This is a low-level property printer, see
1703 * print_status_info() for the nicer output */
1705 if (arg_property && !streq(name, arg_property))
1708 switch (dbus_message_iter_get_arg_type(iter)) {
1710 case DBUS_TYPE_STRING: {
1712 dbus_message_iter_get_basic(iter, &s);
1714 if (arg_all || s[0])
1715 printf("%s=%s\n", name, s);
1720 case DBUS_TYPE_BOOLEAN: {
1722 dbus_message_iter_get_basic(iter, &b);
1723 printf("%s=%s\n", name, yes_no(b));
1728 case DBUS_TYPE_UINT64: {
1730 dbus_message_iter_get_basic(iter, &u);
1732 /* Yes, heuristics! But we can change this check
1733 * should it turn out to not be sufficient */
1735 if (strstr(name, "Timestamp")) {
1736 char timestamp[FORMAT_TIMESTAMP_MAX], *t;
1738 if ((t = format_timestamp(timestamp, sizeof(timestamp), u)) || arg_all)
1739 printf("%s=%s\n", name, strempty(t));
1740 } else if (strstr(name, "USec")) {
1741 char timespan[FORMAT_TIMESPAN_MAX];
1743 printf("%s=%s\n", name, format_timespan(timespan, sizeof(timespan), u));
1745 printf("%s=%llu\n", name, (unsigned long long) u);
1750 case DBUS_TYPE_UINT32: {
1752 dbus_message_iter_get_basic(iter, &u);
1754 if (strstr(name, "UMask") || strstr(name, "Mode"))
1755 printf("%s=%04o\n", name, u);
1757 printf("%s=%u\n", name, (unsigned) u);
1762 case DBUS_TYPE_INT32: {
1764 dbus_message_iter_get_basic(iter, &i);
1766 printf("%s=%i\n", name, (int) i);
1770 case DBUS_TYPE_STRUCT: {
1771 DBusMessageIter sub;
1772 dbus_message_iter_recurse(iter, &sub);
1774 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "Job")) {
1777 dbus_message_iter_get_basic(&sub, &u);
1780 printf("%s=%u\n", name, (unsigned) u);
1782 printf("%s=\n", name);
1785 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Unit")) {
1788 dbus_message_iter_get_basic(&sub, &s);
1790 if (arg_all || s[0])
1791 printf("%s=%s\n", name, s);
1799 case DBUS_TYPE_ARRAY:
1801 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
1802 DBusMessageIter sub;
1805 dbus_message_iter_recurse(iter, &sub);
1807 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1808 printf("%s=", name);
1810 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1813 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
1814 dbus_message_iter_get_basic(&sub, &s);
1815 printf("%s%s", space ? " " : "", s);
1818 dbus_message_iter_next(&sub);
1826 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_BYTE) {
1827 DBusMessageIter sub;
1829 dbus_message_iter_recurse(iter, &sub);
1831 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1832 printf("%s=", name);
1834 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1837 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_BYTE);
1838 dbus_message_iter_get_basic(&sub, &u);
1841 dbus_message_iter_next(&sub);
1849 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Paths")) {
1850 DBusMessageIter sub, sub2;
1852 dbus_message_iter_recurse(iter, &sub);
1853 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1854 const char *type, *path;
1856 dbus_message_iter_recurse(&sub, &sub2);
1858 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
1859 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0)
1860 printf("%s=%s\n", type, path);
1862 dbus_message_iter_next(&sub);
1867 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Timers")) {
1868 DBusMessageIter sub, sub2;
1870 dbus_message_iter_recurse(iter, &sub);
1871 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1873 uint64_t value, next_elapse;
1875 dbus_message_iter_recurse(&sub, &sub2);
1877 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &base, true) >= 0 &&
1878 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &value, true) >= 0 &&
1879 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &next_elapse, false) >= 0) {
1880 char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX];
1882 printf("%s={ value=%s ; next_elapse=%s }\n",
1884 format_timespan(timespan1, sizeof(timespan1), value),
1885 format_timespan(timespan2, sizeof(timespan2), next_elapse));
1888 dbus_message_iter_next(&sub);
1893 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && startswith(name, "Exec")) {
1894 DBusMessageIter sub;
1896 dbus_message_iter_recurse(iter, &sub);
1897 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1898 ExecStatusInfo info;
1901 if (exec_status_info_deserialize(&sub, &info) >= 0) {
1902 char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
1905 t = strv_join(info.argv, " ");
1907 printf("%s={ path=%s ; argv[]=%s ; ignore=%s ; start_time=[%s] ; stop_time=[%s] ; pid=%u ; code=%s ; status=%i%s%s }\n",
1911 yes_no(info.ignore),
1912 strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)),
1913 strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)),
1914 (unsigned) info. pid,
1915 sigchld_code_to_string(info.code),
1917 info.code == CLD_EXITED ? "" : "/",
1918 strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status)));
1924 strv_free(info.argv);
1926 dbus_message_iter_next(&sub);
1936 printf("%s=[unprintable]\n", name);
1941 static int show_one(DBusConnection *bus, const char *path, bool show_properties, bool *new_line) {
1942 DBusMessage *m = NULL, *reply = NULL;
1943 const char *interface = "";
1946 DBusMessageIter iter, sub, sub2, sub3;
1947 UnitStatusInfo info;
1955 dbus_error_init(&error);
1957 if (!(m = dbus_message_new_method_call(
1958 "org.freedesktop.systemd1",
1960 "org.freedesktop.DBus.Properties",
1962 log_error("Could not allocate message.");
1967 if (!dbus_message_append_args(m,
1968 DBUS_TYPE_STRING, &interface,
1969 DBUS_TYPE_INVALID)) {
1970 log_error("Could not append arguments to message.");
1975 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1976 log_error("Failed to issue method call: %s", error.message);
1981 if (!dbus_message_iter_init(reply, &iter) ||
1982 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
1983 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
1984 log_error("Failed to parse reply.");
1989 dbus_message_iter_recurse(&iter, &sub);
1996 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1999 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
2000 log_error("Failed to parse reply.");
2005 dbus_message_iter_recurse(&sub, &sub2);
2007 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
2008 log_error("Failed to parse reply.");
2013 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
2014 log_error("Failed to parse reply.");
2019 dbus_message_iter_recurse(&sub2, &sub3);
2021 if (show_properties)
2022 r = print_property(name, &sub3);
2024 r = status_property(name, &sub3, &info);
2027 log_error("Failed to parse reply.");
2032 dbus_message_iter_next(&sub);
2035 if (!show_properties)
2036 print_status_info(&info);
2038 while ((p = info.exec)) {
2039 LIST_REMOVE(ExecStatusInfo, exec, info.exec, p);
2040 exec_status_info_free(p);
2047 dbus_message_unref(m);
2050 dbus_message_unref(reply);
2052 dbus_error_free(&error);
2057 static int show(DBusConnection *bus, char **args, unsigned n) {
2058 DBusMessage *m = NULL, *reply = NULL;
2062 bool show_properties, new_line = false;
2067 dbus_error_init(&error);
2069 show_properties = !streq(args[0], "status");
2071 if (show_properties && n <= 1) {
2072 /* If not argument is specified inspect the manager
2075 r = show_one(bus, "/org/freedesktop/systemd1", show_properties, &new_line);
2079 for (i = 1; i < n; i++) {
2080 const char *path = NULL;
2083 if (!show_properties || safe_atou32(args[i], &id) < 0) {
2085 if (!(m = dbus_message_new_method_call(
2086 "org.freedesktop.systemd1",
2087 "/org/freedesktop/systemd1",
2088 "org.freedesktop.systemd1.Manager",
2090 log_error("Could not allocate message.");
2095 if (!dbus_message_append_args(m,
2096 DBUS_TYPE_STRING, &args[i],
2097 DBUS_TYPE_INVALID)) {
2098 log_error("Could not append arguments to message.");
2103 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2105 if (!dbus_error_has_name(&error, DBUS_ERROR_ACCESS_DENIED)) {
2106 log_error("Failed to issue method call: %s", error.message);
2111 dbus_error_free(&error);
2113 dbus_message_unref(m);
2114 if (!(m = dbus_message_new_method_call(
2115 "org.freedesktop.systemd1",
2116 "/org/freedesktop/systemd1",
2117 "org.freedesktop.systemd1.Manager",
2119 log_error("Could not allocate message.");
2124 if (!dbus_message_append_args(m,
2125 DBUS_TYPE_STRING, &args[i],
2126 DBUS_TYPE_INVALID)) {
2127 log_error("Could not append arguments to message.");
2132 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2133 log_error("Failed to issue method call: %s", error.message);
2141 if (!(m = dbus_message_new_method_call(
2142 "org.freedesktop.systemd1",
2143 "/org/freedesktop/systemd1",
2144 "org.freedesktop.systemd1.Manager",
2146 log_error("Could not allocate message.");
2151 if (!dbus_message_append_args(m,
2152 DBUS_TYPE_UINT32, &id,
2153 DBUS_TYPE_INVALID)) {
2154 log_error("Could not append arguments to message.");
2159 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2160 log_error("Failed to issue method call: %s", error.message);
2166 if (!dbus_message_get_args(reply, &error,
2167 DBUS_TYPE_OBJECT_PATH, &path,
2168 DBUS_TYPE_INVALID)) {
2169 log_error("Failed to parse reply: %s", error.message);
2174 if ((r = show_one(bus, path, show_properties, &new_line)) < 0)
2177 dbus_message_unref(m);
2178 dbus_message_unref(reply);
2186 dbus_message_unref(m);
2189 dbus_message_unref(reply);
2191 dbus_error_free(&error);
2196 static DBusHandlerResult monitor_filter(DBusConnection *connection, DBusMessage *message, void *data) {
2198 DBusMessage *m = NULL, *reply = NULL;
2203 dbus_error_init(&error);
2205 log_debug("Got D-Bus request: %s.%s() on %s",
2206 dbus_message_get_interface(message),
2207 dbus_message_get_member(message),
2208 dbus_message_get_path(message));
2210 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
2211 log_error("Warning! D-Bus connection terminated.");
2212 dbus_connection_close(connection);
2214 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitNew") ||
2215 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitRemoved")) {
2216 const char *id, *path;
2218 if (!dbus_message_get_args(message, &error,
2219 DBUS_TYPE_STRING, &id,
2220 DBUS_TYPE_OBJECT_PATH, &path,
2222 log_error("Failed to parse message: %s", error.message);
2223 else if (streq(dbus_message_get_member(message), "UnitNew"))
2224 printf("Unit %s added.\n", id);
2226 printf("Unit %s removed.\n", id);
2228 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobNew") ||
2229 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
2233 if (!dbus_message_get_args(message, &error,
2234 DBUS_TYPE_UINT32, &id,
2235 DBUS_TYPE_OBJECT_PATH, &path,
2237 log_error("Failed to parse message: %s", error.message);
2238 else if (streq(dbus_message_get_member(message), "JobNew"))
2239 printf("Job %u added.\n", id);
2241 printf("Job %u removed.\n", id);
2244 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Unit", "Changed") ||
2245 dbus_message_is_signal(message, "org.freedesktop.systemd1.Job", "Changed")) {
2247 const char *path, *interface, *property = "Id";
2248 DBusMessageIter iter, sub;
2250 path = dbus_message_get_path(message);
2251 interface = dbus_message_get_interface(message);
2253 if (!(m = dbus_message_new_method_call(
2254 "org.freedesktop.systemd1",
2256 "org.freedesktop.DBus.Properties",
2258 log_error("Could not allocate message.");
2262 if (!dbus_message_append_args(m,
2263 DBUS_TYPE_STRING, &interface,
2264 DBUS_TYPE_STRING, &property,
2265 DBUS_TYPE_INVALID)) {
2266 log_error("Could not append arguments to message.");
2270 if (!(reply = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) {
2271 log_error("Failed to issue method call: %s", error.message);
2275 if (!dbus_message_iter_init(reply, &iter) ||
2276 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2277 log_error("Failed to parse reply.");
2281 dbus_message_iter_recurse(&iter, &sub);
2283 if (streq(interface, "org.freedesktop.systemd1.Unit")) {
2286 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2287 log_error("Failed to parse reply.");
2291 dbus_message_iter_get_basic(&sub, &id);
2292 printf("Unit %s changed.\n", id);
2296 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT32) {
2297 log_error("Failed to parse reply.");
2301 dbus_message_iter_get_basic(&sub, &id);
2302 printf("Job %u changed.\n", id);
2308 dbus_message_unref(m);
2311 dbus_message_unref(reply);
2313 dbus_error_free(&error);
2314 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
2318 dbus_message_unref(m);
2321 dbus_message_unref(reply);
2323 dbus_error_free(&error);
2324 return DBUS_HANDLER_RESULT_NEED_MEMORY;
2327 static int monitor(DBusConnection *bus, char **args, unsigned n) {
2328 DBusMessage *m = NULL, *reply = NULL;
2332 dbus_error_init(&error);
2335 dbus_bus_add_match(bus,
2337 "sender='org.freedesktop.systemd1',"
2338 "interface='org.freedesktop.systemd1.Manager',"
2339 "path='/org/freedesktop/systemd1'",
2342 if (dbus_error_is_set(&error)) {
2343 log_error("Failed to add match: %s", error.message);
2348 dbus_bus_add_match(bus,
2350 "sender='org.freedesktop.systemd1',"
2351 "interface='org.freedesktop.systemd1.Unit',"
2355 if (dbus_error_is_set(&error)) {
2356 log_error("Failed to add match: %s", error.message);
2361 dbus_bus_add_match(bus,
2363 "sender='org.freedesktop.systemd1',"
2364 "interface='org.freedesktop.systemd1.Job',"
2368 if (dbus_error_is_set(&error)) {
2369 log_error("Failed to add match: %s", error.message);
2375 if (!dbus_connection_add_filter(bus, monitor_filter, NULL, NULL)) {
2376 log_error("Failed to add filter.");
2381 if (!(m = dbus_message_new_method_call(
2382 "org.freedesktop.systemd1",
2383 "/org/freedesktop/systemd1",
2384 "org.freedesktop.systemd1.Manager",
2386 log_error("Could not allocate message.");
2391 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2392 log_error("Failed to issue method call: %s", error.message);
2397 while (dbus_connection_read_write_dispatch(bus, -1))
2404 /* This is slightly dirty, since we don't undo the filter or the matches. */
2407 dbus_message_unref(m);
2410 dbus_message_unref(reply);
2412 dbus_error_free(&error);
2417 static int dump(DBusConnection *bus, char **args, unsigned n) {
2418 DBusMessage *m = NULL, *reply = NULL;
2423 dbus_error_init(&error);
2425 if (!(m = dbus_message_new_method_call(
2426 "org.freedesktop.systemd1",
2427 "/org/freedesktop/systemd1",
2428 "org.freedesktop.systemd1.Manager",
2430 log_error("Could not allocate message.");
2434 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2435 log_error("Failed to issue method call: %s", error.message);
2440 if (!dbus_message_get_args(reply, &error,
2441 DBUS_TYPE_STRING, &text,
2442 DBUS_TYPE_INVALID)) {
2443 log_error("Failed to parse reply: %s", error.message);
2448 fputs(text, stdout);
2454 dbus_message_unref(m);
2457 dbus_message_unref(reply);
2459 dbus_error_free(&error);
2464 static int snapshot(DBusConnection *bus, char **args, unsigned n) {
2465 DBusMessage *m = NULL, *reply = NULL;
2468 const char *name = "", *path, *id;
2469 dbus_bool_t cleanup = FALSE;
2470 DBusMessageIter iter, sub;
2472 *interface = "org.freedesktop.systemd1.Unit",
2475 dbus_error_init(&error);
2477 if (!(m = dbus_message_new_method_call(
2478 "org.freedesktop.systemd1",
2479 "/org/freedesktop/systemd1",
2480 "org.freedesktop.systemd1.Manager",
2481 "CreateSnapshot"))) {
2482 log_error("Could not allocate message.");
2489 if (!dbus_message_append_args(m,
2490 DBUS_TYPE_STRING, &name,
2491 DBUS_TYPE_BOOLEAN, &cleanup,
2492 DBUS_TYPE_INVALID)) {
2493 log_error("Could not append arguments to message.");
2498 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2499 log_error("Failed to issue method call: %s", error.message);
2504 if (!dbus_message_get_args(reply, &error,
2505 DBUS_TYPE_OBJECT_PATH, &path,
2506 DBUS_TYPE_INVALID)) {
2507 log_error("Failed to parse reply: %s", error.message);
2512 dbus_message_unref(m);
2513 if (!(m = dbus_message_new_method_call(
2514 "org.freedesktop.systemd1",
2516 "org.freedesktop.DBus.Properties",
2518 log_error("Could not allocate message.");
2522 if (!dbus_message_append_args(m,
2523 DBUS_TYPE_STRING, &interface,
2524 DBUS_TYPE_STRING, &property,
2525 DBUS_TYPE_INVALID)) {
2526 log_error("Could not append arguments to message.");
2531 dbus_message_unref(reply);
2532 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2533 log_error("Failed to issue method call: %s", error.message);
2538 if (!dbus_message_iter_init(reply, &iter) ||
2539 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2540 log_error("Failed to parse reply.");
2545 dbus_message_iter_recurse(&iter, &sub);
2547 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2548 log_error("Failed to parse reply.");
2553 dbus_message_iter_get_basic(&sub, &id);
2561 dbus_message_unref(m);
2564 dbus_message_unref(reply);
2566 dbus_error_free(&error);
2571 static int delete_snapshot(DBusConnection *bus, char **args, unsigned n) {
2572 DBusMessage *m = NULL, *reply = NULL;
2580 dbus_error_init(&error);
2582 for (i = 1; i < n; i++) {
2583 const char *path = NULL;
2585 if (!(m = dbus_message_new_method_call(
2586 "org.freedesktop.systemd1",
2587 "/org/freedesktop/systemd1",
2588 "org.freedesktop.systemd1.Manager",
2590 log_error("Could not allocate message.");
2595 if (!dbus_message_append_args(m,
2596 DBUS_TYPE_STRING, &args[i],
2597 DBUS_TYPE_INVALID)) {
2598 log_error("Could not append arguments to message.");
2603 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2604 log_error("Failed to issue method call: %s", error.message);
2609 if (!dbus_message_get_args(reply, &error,
2610 DBUS_TYPE_OBJECT_PATH, &path,
2611 DBUS_TYPE_INVALID)) {
2612 log_error("Failed to parse reply: %s", error.message);
2617 dbus_message_unref(m);
2618 if (!(m = dbus_message_new_method_call(
2619 "org.freedesktop.systemd1",
2621 "org.freedesktop.systemd1.Snapshot",
2623 log_error("Could not allocate message.");
2628 dbus_message_unref(reply);
2629 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2630 log_error("Failed to issue method call: %s", error.message);
2635 dbus_message_unref(m);
2636 dbus_message_unref(reply);
2644 dbus_message_unref(m);
2647 dbus_message_unref(reply);
2649 dbus_error_free(&error);
2654 static int clear_jobs(DBusConnection *bus, char **args, unsigned n) {
2655 DBusMessage *m = NULL, *reply = NULL;
2660 dbus_error_init(&error);
2662 if (arg_action == ACTION_RELOAD)
2664 else if (arg_action == ACTION_REEXEC)
2665 method = "Reexecute";
2667 assert(arg_action == ACTION_SYSTEMCTL);
2670 streq(args[0], "clear-jobs") ? "ClearJobs" :
2671 streq(args[0], "daemon-reload") ? "Reload" :
2672 streq(args[0], "daemon-reexec") ? "Reexecute" :
2673 streq(args[0], "reset-maintenance") ? "ResetMaintenance" :
2677 if (!(m = dbus_message_new_method_call(
2678 "org.freedesktop.systemd1",
2679 "/org/freedesktop/systemd1",
2680 "org.freedesktop.systemd1.Manager",
2682 log_error("Could not allocate message.");
2686 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2688 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
2689 /* There's always a fallback possible for
2690 * legacy actions. */
2695 log_error("Failed to issue method call: %s", error.message);
2704 dbus_message_unref(m);
2707 dbus_message_unref(reply);
2709 dbus_error_free(&error);
2714 static int reset_maintenance(DBusConnection *bus, char **args, unsigned n) {
2715 DBusMessage *m = NULL, *reply = NULL;
2721 dbus_error_init(&error);
2724 return clear_jobs(bus, args, n);
2726 for (i = 1; i < n; i++) {
2728 if (!(m = dbus_message_new_method_call(
2729 "org.freedesktop.systemd1",
2730 "/org/freedesktop/systemd1",
2731 "org.freedesktop.systemd1.Manager",
2732 "ResetMaintenanceUnit"))) {
2733 log_error("Could not allocate message.");
2738 if (!dbus_message_append_args(m,
2739 DBUS_TYPE_STRING, args + i,
2740 DBUS_TYPE_INVALID)) {
2741 log_error("Could not append arguments to message.");
2746 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2747 log_error("Failed to issue method call: %s", error.message);
2752 dbus_message_unref(m);
2753 dbus_message_unref(reply);
2761 dbus_message_unref(m);
2764 dbus_message_unref(reply);
2766 dbus_error_free(&error);
2771 static int show_enviroment(DBusConnection *bus, char **args, unsigned n) {
2772 DBusMessage *m = NULL, *reply = NULL;
2774 DBusMessageIter iter, sub, sub2;
2777 *interface = "org.freedesktop.systemd1.Manager",
2778 *property = "Environment";
2780 dbus_error_init(&error);
2782 if (!(m = dbus_message_new_method_call(
2783 "org.freedesktop.systemd1",
2784 "/org/freedesktop/systemd1",
2785 "org.freedesktop.DBus.Properties",
2787 log_error("Could not allocate message.");
2791 if (!dbus_message_append_args(m,
2792 DBUS_TYPE_STRING, &interface,
2793 DBUS_TYPE_STRING, &property,
2794 DBUS_TYPE_INVALID)) {
2795 log_error("Could not append arguments to message.");
2800 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2801 log_error("Failed to issue method call: %s", error.message);
2806 if (!dbus_message_iter_init(reply, &iter) ||
2807 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2808 log_error("Failed to parse reply.");
2813 dbus_message_iter_recurse(&iter, &sub);
2815 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
2816 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
2817 log_error("Failed to parse reply.");
2822 dbus_message_iter_recurse(&sub, &sub2);
2824 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
2827 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
2828 log_error("Failed to parse reply.");
2833 dbus_message_iter_get_basic(&sub2, &text);
2834 printf("%s\n", text);
2836 dbus_message_iter_next(&sub2);
2843 dbus_message_unref(m);
2846 dbus_message_unref(reply);
2848 dbus_error_free(&error);
2853 static int set_environment(DBusConnection *bus, char **args, unsigned n) {
2854 DBusMessage *m = NULL, *reply = NULL;
2858 DBusMessageIter iter, sub;
2861 dbus_error_init(&error);
2863 method = streq(args[0], "set-environment")
2865 : "UnsetEnvironment";
2867 if (!(m = dbus_message_new_method_call(
2868 "org.freedesktop.systemd1",
2869 "/org/freedesktop/systemd1",
2870 "org.freedesktop.systemd1.Manager",
2873 log_error("Could not allocate message.");
2877 dbus_message_iter_init_append(m, &iter);
2879 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
2880 log_error("Could not append arguments to message.");
2885 for (i = 1; i < n; i++)
2886 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &args[i])) {
2887 log_error("Could not append arguments to message.");
2892 if (!dbus_message_iter_close_container(&iter, &sub)) {
2893 log_error("Could not append arguments to message.");
2898 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2899 log_error("Failed to issue method call: %s", error.message);
2908 dbus_message_unref(m);
2911 dbus_message_unref(reply);
2913 dbus_error_free(&error);
2918 static int systemctl_help(void) {
2920 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
2921 "Send control commands to or query the systemd manager.\n\n"
2922 " -h --help Show this help\n"
2923 " -t --type=TYPE List only units of a particular type\n"
2924 " -p --property=NAME Show only properties by this name\n"
2925 " -a --all Show all units/properties, including dead/empty ones\n"
2926 " --full Don't ellipsize unit names.\n"
2927 " --fail When installing a new job, fail if conflicting jobs are\n"
2929 " --system Connect to system bus\n"
2930 " --session Connect to session bus\n"
2931 " --order When generating graph for dot, show only order\n"
2932 " --require When generating graph for dot, show only requirement\n"
2933 " -q --quiet Suppress output\n"
2934 " --no-block Do not wait until operation finished\n"
2935 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
2937 " list-units List units\n"
2938 " start [NAME...] Start one or more units\n"
2939 " stop [NAME...] Stop one or more units\n"
2940 " reload [NAME...] Reload one or more units\n"
2941 " restart [NAME...] Start or restart one or more units\n"
2942 " try-restart [NAME...] Restart one or more units if active\n"
2943 " reload-or-restart [NAME...] Reload one or more units is possible,\n"
2944 " otherwise start or restart\n"
2945 " reload-or-try-restart [NAME...] Reload one or more units is possible,\n"
2946 " otherwise restart if active\n"
2947 " isolate [NAME] Start one unit and stop all others\n"
2948 " check [NAME...] Check whether units are active\n"
2949 " status [NAME...] Show status of one or more units\n"
2950 " show [NAME...|JOB...] Show properties of one or more\n"
2951 " units/jobs/manager\n"
2952 " reset-maintenance [NAME...] Reset maintenance state for all, one\n"
2954 " load [NAME...] Load one or more units\n"
2955 " list-jobs List jobs\n"
2956 " cancel [JOB...] Cancel one or more jobs\n"
2957 " clear-jobs Cancel all jobs\n"
2958 " monitor Monitor unit/job changes\n"
2959 " dump Dump server status\n"
2960 " dot Dump dependency graph for dot(1)\n"
2961 " snapshot [NAME] Create a snapshot\n"
2962 " delete [NAME...] Remove one or more snapshots\n"
2963 " daemon-reload Reload systemd manager configuration\n"
2964 " daemon-reexec Reexecute systemd manager\n"
2965 " daemon-exit Ask the systemd manager to quit\n"
2966 " show-environment Dump environment\n"
2967 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
2968 " unset-environment [NAME...] Unset one or more environment variables\n"
2969 " halt Shut down and halt the system\n"
2970 " poweroff Shut down and power-off the system\n"
2971 " reboot Shut down and reboot the system\n"
2972 " rescue Enter system rescue mode\n"
2973 " emergency Enter system emergency mode\n"
2974 " default Enter system default mode\n",
2975 program_invocation_short_name);
2980 static int halt_help(void) {
2982 printf("%s [OPTIONS...]\n\n"
2983 "%s the system.\n\n"
2984 " --help Show this help\n"
2985 " --halt Halt the machine\n"
2986 " -p --poweroff Switch off the machine\n"
2987 " --reboot Reboot the machine\n"
2988 " -f --force Force immediate halt/power-off/reboot\n"
2989 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
2990 " -d --no-wtmp Don't write wtmp record\n"
2991 " -n --no-sync Don't sync before halt/power-off/reboot\n"
2992 " --no-wall Don't send wall message before halt/power-off/reboot\n",
2993 program_invocation_short_name,
2994 arg_action == ACTION_REBOOT ? "Reboot" :
2995 arg_action == ACTION_POWEROFF ? "Power off" :
3001 static int shutdown_help(void) {
3003 printf("%s [OPTIONS...] [now] [WALL...]\n\n"
3004 "Shut down the system.\n\n"
3005 " --help Show this help\n"
3006 " -H --halt Halt the machine\n"
3007 " -P --poweroff Power-off the machine\n"
3008 " -r --reboot Reboot the machine\n"
3009 " -h Equivalent to --poweroff, overriden by --halt\n"
3010 " -k Don't halt/power-off/reboot, just send warnings\n"
3011 " --no-wall Don't send wall message before halt/power-off/reboot\n",
3012 program_invocation_short_name);
3017 static int telinit_help(void) {
3019 printf("%s [OPTIONS...] {COMMAND}\n\n"
3020 "Send control commands to the init daemon.\n\n"
3021 " --help Show this help\n"
3022 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
3024 " 0 Power-off the machine\n"
3025 " 6 Reboot the machine\n"
3026 " 2, 3, 4, 5 Start runlevelX.target unit\n"
3027 " 1, s, S Enter rescue mode\n"
3028 " q, Q Reload init daemon configuration\n"
3029 " u, U Reexecute init daemon\n",
3030 program_invocation_short_name);
3035 static int runlevel_help(void) {
3037 printf("%s [OPTIONS...]\n\n"
3038 "Prints the previous and current runlevel of the init system.\n\n"
3039 " --help Show this help\n",
3040 program_invocation_short_name);
3045 static int systemctl_parse_argv(int argc, char *argv[]) {
3058 static const struct option options[] = {
3059 { "help", no_argument, NULL, 'h' },
3060 { "type", required_argument, NULL, 't' },
3061 { "property", required_argument, NULL, 'p' },
3062 { "all", no_argument, NULL, 'a' },
3063 { "full", no_argument, NULL, ARG_FULL },
3064 { "fail", no_argument, NULL, ARG_FAIL },
3065 { "session", no_argument, NULL, ARG_SESSION },
3066 { "system", no_argument, NULL, ARG_SYSTEM },
3067 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
3068 { "no-wall", no_argument, NULL, ARG_NO_WALL },
3069 { "quiet", no_argument, NULL, 'q' },
3070 { "order", no_argument, NULL, ARG_ORDER },
3071 { "require", no_argument, NULL, ARG_REQUIRE },
3072 { NULL, 0, NULL, 0 }
3080 while ((c = getopt_long(argc, argv, "ht:p:aq", options, NULL)) >= 0) {
3093 arg_property = optarg;
3095 /* If the user asked for a particular
3096 * property, show it to him, even if it is
3114 arg_session = false;
3118 arg_no_block = true;
3126 arg_dot = DOT_ORDER;
3130 arg_dot = DOT_REQUIRE;
3145 log_error("Unknown option code %c", c);
3153 static int halt_parse_argv(int argc, char *argv[]) {
3162 static const struct option options[] = {
3163 { "help", no_argument, NULL, ARG_HELP },
3164 { "halt", no_argument, NULL, ARG_HALT },
3165 { "poweroff", no_argument, NULL, 'p' },
3166 { "reboot", no_argument, NULL, ARG_REBOOT },
3167 { "force", no_argument, NULL, 'f' },
3168 { "wtmp-only", no_argument, NULL, 'w' },
3169 { "no-wtmp", no_argument, NULL, 'd' },
3170 { "no-sync", no_argument, NULL, 'n' },
3171 { "no-wall", no_argument, NULL, ARG_NO_WALL },
3172 { NULL, 0, NULL, 0 }
3180 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
3181 if (runlevel == '0' || runlevel == '6')
3182 arg_immediate = true;
3184 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
3192 arg_action = ACTION_HALT;
3196 arg_action = ACTION_POWEROFF;
3200 arg_action = ACTION_REBOOT;
3204 arg_immediate = true;
3225 /* Compatibility nops */
3232 log_error("Unknown option code %c", c);
3237 if (optind < argc) {
3238 log_error("Too many arguments.");
3245 static int shutdown_parse_argv(int argc, char *argv[]) {
3252 static const struct option options[] = {
3253 { "help", no_argument, NULL, ARG_HELP },
3254 { "halt", no_argument, NULL, 'H' },
3255 { "poweroff", no_argument, NULL, 'P' },
3256 { "reboot", no_argument, NULL, 'r' },
3257 { "no-wall", no_argument, NULL, ARG_NO_WALL },
3258 { NULL, 0, NULL, 0 }
3266 while ((c = getopt_long(argc, argv, "HPrhkt:a", options, NULL)) >= 0) {
3274 arg_action = ACTION_HALT;
3278 arg_action = ACTION_POWEROFF;
3282 arg_action = ACTION_REBOOT;
3286 if (arg_action != ACTION_HALT)
3287 arg_action = ACTION_POWEROFF;
3300 /* Compatibility nops */
3307 log_error("Unknown option code %c", c);
3312 if (argc > optind && !streq(argv[optind], "now"))
3313 log_warning("First argument '%s' isn't 'now'. Ignoring.", argv[optind]);
3315 /* We ignore the time argument */
3316 if (argc > optind + 1)
3317 arg_wall = argv + optind + 1;
3324 static int telinit_parse_argv(int argc, char *argv[]) {
3331 static const struct option options[] = {
3332 { "help", no_argument, NULL, ARG_HELP },
3333 { "no-wall", no_argument, NULL, ARG_NO_WALL },
3334 { NULL, 0, NULL, 0 }
3337 static const struct {
3341 { '0', ACTION_POWEROFF },
3342 { '6', ACTION_REBOOT },
3343 { '1', ACTION_RESCUE },
3344 { '2', ACTION_RUNLEVEL2 },
3345 { '3', ACTION_RUNLEVEL3 },
3346 { '4', ACTION_RUNLEVEL4 },
3347 { '5', ACTION_RUNLEVEL5 },
3348 { 's', ACTION_RESCUE },
3349 { 'S', ACTION_RESCUE },
3350 { 'q', ACTION_RELOAD },
3351 { 'Q', ACTION_RELOAD },
3352 { 'u', ACTION_REEXEC },
3353 { 'U', ACTION_REEXEC }
3362 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
3377 log_error("Unknown option code %c", c);
3382 if (optind >= argc) {
3387 if (optind + 1 < argc) {
3388 log_error("Too many arguments.");
3392 if (strlen(argv[optind]) != 1) {
3393 log_error("Expected single character argument.");
3397 for (i = 0; i < ELEMENTSOF(table); i++)
3398 if (table[i].from == argv[optind][0])
3401 if (i >= ELEMENTSOF(table)) {
3402 log_error("Unknown command %s.", argv[optind]);
3406 arg_action = table[i].to;
3413 static int runlevel_parse_argv(int argc, char *argv[]) {
3419 static const struct option options[] = {
3420 { "help", no_argument, NULL, ARG_HELP },
3421 { NULL, 0, NULL, 0 }
3429 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
3440 log_error("Unknown option code %c", c);
3445 if (optind < argc) {
3446 log_error("Too many arguments.");
3453 static int parse_argv(int argc, char *argv[]) {
3457 if (program_invocation_short_name) {
3459 if (strstr(program_invocation_short_name, "halt")) {
3460 arg_action = ACTION_HALT;
3461 return halt_parse_argv(argc, argv);
3462 } else if (strstr(program_invocation_short_name, "poweroff")) {
3463 arg_action = ACTION_POWEROFF;
3464 return halt_parse_argv(argc, argv);
3465 } else if (strstr(program_invocation_short_name, "reboot")) {
3466 arg_action = ACTION_REBOOT;
3467 return halt_parse_argv(argc, argv);
3468 } else if (strstr(program_invocation_short_name, "shutdown")) {
3469 arg_action = ACTION_POWEROFF;
3470 return shutdown_parse_argv(argc, argv);
3471 } else if (strstr(program_invocation_short_name, "init")) {
3472 arg_action = ACTION_INVALID;
3473 return telinit_parse_argv(argc, argv);
3474 } else if (strstr(program_invocation_short_name, "runlevel")) {
3475 arg_action = ACTION_RUNLEVEL;
3476 return runlevel_parse_argv(argc, argv);
3480 arg_action = ACTION_SYSTEMCTL;
3481 return systemctl_parse_argv(argc, argv);
3484 static int action_to_runlevel(void) {
3486 static const char table[_ACTION_MAX] = {
3487 [ACTION_HALT] = '0',
3488 [ACTION_POWEROFF] = '0',
3489 [ACTION_REBOOT] = '6',
3490 [ACTION_RUNLEVEL2] = '2',
3491 [ACTION_RUNLEVEL3] = '3',
3492 [ACTION_RUNLEVEL4] = '4',
3493 [ACTION_RUNLEVEL5] = '5',
3494 [ACTION_RESCUE] = '1'
3497 assert(arg_action < _ACTION_MAX);
3499 return table[arg_action];
3502 static int talk_upstart(void) {
3503 DBusMessage *m = NULL, *reply = NULL;
3505 int previous, rl, r;
3507 env1_buf[] = "RUNLEVEL=X",
3508 env2_buf[] = "PREVLEVEL=X";
3509 char *env1 = env1_buf, *env2 = env2_buf;
3510 const char *emit = "runlevel";
3511 dbus_bool_t b_false = FALSE;
3512 DBusMessageIter iter, sub;
3513 DBusConnection *bus;
3515 dbus_error_init(&error);
3517 if (!(rl = action_to_runlevel()))
3520 if (utmp_get_runlevel(&previous, NULL) < 0)
3523 if (!(bus = dbus_connection_open_private("unix:abstract=/com/ubuntu/upstart", &error))) {
3524 if (dbus_error_has_name(&error, DBUS_ERROR_NO_SERVER)) {
3529 log_error("Failed to connect to Upstart bus: %s", error.message);
3534 if ((r = bus_check_peercred(bus)) < 0) {
3535 log_error("Failed to verify owner of bus.");
3539 if (!(m = dbus_message_new_method_call(
3540 "com.ubuntu.Upstart",
3541 "/com/ubuntu/Upstart",
3542 "com.ubuntu.Upstart0_6",
3545 log_error("Could not allocate message.");
3550 dbus_message_iter_init_append(m, &iter);
3552 env1_buf[sizeof(env1_buf)-2] = rl;
3553 env2_buf[sizeof(env2_buf)-2] = previous;
3555 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
3556 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
3557 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
3558 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
3559 !dbus_message_iter_close_container(&iter, &sub) ||
3560 !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
3561 log_error("Could not append arguments to message.");
3566 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3568 if (error_is_no_service(&error)) {
3573 log_error("Failed to issue method call: %s", error.message);
3582 dbus_message_unref(m);
3585 dbus_message_unref(reply);
3588 dbus_connection_close(bus);
3589 dbus_connection_unref(bus);
3592 dbus_error_free(&error);
3597 static int talk_initctl(void) {
3598 struct init_request request;
3602 if (!(rl = action_to_runlevel()))
3606 request.magic = INIT_MAGIC;
3607 request.sleeptime = 0;
3608 request.cmd = INIT_CMD_RUNLVL;
3609 request.runlevel = rl;
3611 if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
3613 if (errno == ENOENT)
3616 log_error("Failed to open "INIT_FIFO": %m");
3621 r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
3622 close_nointr_nofail(fd);
3625 log_error("Failed to write to "INIT_FIFO": %m");
3626 return errno ? -errno : -EIO;
3632 static int systemctl_main(DBusConnection *bus, int argc, char *argv[]) {
3634 static const struct {
3642 int (* const dispatch)(DBusConnection *bus, char **args, unsigned n);
3644 { "list-units", LESS, 1, list_units },
3645 { "list-jobs", EQUAL, 1, list_jobs },
3646 { "clear-jobs", EQUAL, 1, clear_jobs },
3647 { "load", MORE, 2, load_unit },
3648 { "cancel", MORE, 2, cancel_job },
3649 { "start", MORE, 2, start_unit },
3650 { "stop", MORE, 2, start_unit },
3651 { "reload", MORE, 2, start_unit },
3652 { "restart", MORE, 2, start_unit },
3653 { "try-restart", MORE, 2, start_unit },
3654 { "reload-or-restart", MORE, 2, start_unit },
3655 { "reload-or-try-restart", MORE, 2, start_unit },
3656 { "isolate", EQUAL, 2, start_unit },
3657 { "check", MORE, 2, check_unit },
3658 { "show", MORE, 1, show },
3659 { "status", MORE, 2, show },
3660 { "monitor", EQUAL, 1, monitor },
3661 { "dump", EQUAL, 1, dump },
3662 { "dot", EQUAL, 1, dot },
3663 { "snapshot", LESS, 2, snapshot },
3664 { "delete", MORE, 2, delete_snapshot },
3665 { "daemon-reload", EQUAL, 1, clear_jobs },
3666 { "daemon-reexec", EQUAL, 1, clear_jobs },
3667 { "daemon-exit", EQUAL, 1, clear_jobs },
3668 { "show-environment", EQUAL, 1, show_enviroment },
3669 { "set-environment", MORE, 2, set_environment },
3670 { "unset-environment", MORE, 2, set_environment },
3671 { "halt", EQUAL, 1, start_special },
3672 { "poweroff", EQUAL, 1, start_special },
3673 { "reboot", EQUAL, 1, start_special },
3674 { "default", EQUAL, 1, start_special },
3675 { "rescue", EQUAL, 1, start_special },
3676 { "emergency", EQUAL, 1, start_special },
3677 { "reset-maintenance", MORE, 1, reset_maintenance },
3687 left = argc - optind;
3690 /* Special rule: no arguments means "list-units" */
3693 if (streq(argv[optind], "help")) {
3698 for (i = 0; i < ELEMENTSOF(verbs); i++)
3699 if (streq(argv[optind], verbs[i].verb))
3702 if (i >= ELEMENTSOF(verbs)) {
3703 log_error("Unknown operation %s", argv[optind]);
3708 switch (verbs[i].argc_cmp) {
3711 if (left != verbs[i].argc) {
3712 log_error("Invalid number of arguments.");
3719 if (left < verbs[i].argc) {
3720 log_error("Too few arguments.");
3727 if (left > verbs[i].argc) {
3728 log_error("Too many arguments.");
3735 assert_not_reached("Unknown comparison operator.");
3738 return verbs[i].dispatch(bus, argv + optind, left);
3741 static int reload_with_fallback(DBusConnection *bus) {
3745 /* First, try systemd via D-Bus. */
3746 if ((r = clear_jobs(bus, NULL, 0)) > 0)
3750 /* Nothing else worked, so let's try signals */
3751 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
3753 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
3754 log_error("kill() failed: %m");
3761 static int start_with_fallback(DBusConnection *bus) {
3766 /* First, try systemd via D-Bus. */
3767 if ((r = start_unit(bus, NULL, 0)) > 0)
3770 /* Hmm, talking to systemd via D-Bus didn't work. Then
3771 * let's try to talk to Upstart via D-Bus. */
3772 if ((r = talk_upstart()) > 0)
3776 /* Nothing else worked, so let's try
3778 if ((r = talk_initctl()) != 0)
3781 log_error("Failed to talk to init daemon.");
3785 warn_wall(arg_action);
3789 static int halt_main(DBusConnection *bus) {
3792 if (geteuid() != 0) {
3793 log_error("Must to be root.");
3797 if (!arg_dry && !arg_immediate)
3798 return start_with_fallback(bus);
3801 if ((r = utmp_put_shutdown(0)) < 0)
3802 log_warning("Failed to write utmp record: %s", strerror(-r));
3810 /* Make sure C-A-D is handled by the kernel from this
3812 reboot(RB_ENABLE_CAD);
3814 switch (arg_action) {
3817 log_info("Halting");
3818 reboot(RB_HALT_SYSTEM);
3821 case ACTION_POWEROFF:
3822 log_info("Powering off");
3823 reboot(RB_POWER_OFF);
3827 log_info("Rebooting");
3828 reboot(RB_AUTOBOOT);
3832 assert_not_reached("Unknown halt action.");
3835 /* We should never reach this. */
3839 static int runlevel_main(void) {
3840 int r, runlevel, previous;
3842 if ((r = utmp_get_runlevel(&runlevel, &previous)) < 0) {
3848 previous <= 0 ? 'N' : previous,
3849 runlevel <= 0 ? 'N' : runlevel);
3854 int main(int argc, char*argv[]) {
3856 DBusConnection *bus = NULL;
3859 dbus_error_init(&error);
3861 log_parse_environment();
3863 if ((r = parse_argv(argc, argv)) < 0)
3870 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
3871 * let's shortcut this */
3872 if (arg_action == ACTION_RUNLEVEL) {
3873 retval = runlevel_main() < 0;
3877 bus_connect(arg_session ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &bus, &private_bus, &error);
3879 switch (arg_action) {
3881 case ACTION_SYSTEMCTL: {
3884 log_error("Failed to get D-Bus connection: %s", error.message);
3888 retval = systemctl_main(bus, argc, argv) < 0;
3893 case ACTION_POWEROFF:
3895 retval = halt_main(bus) < 0;
3898 case ACTION_RUNLEVEL2:
3899 case ACTION_RUNLEVEL3:
3900 case ACTION_RUNLEVEL4:
3901 case ACTION_RUNLEVEL5:
3903 case ACTION_EMERGENCY:
3904 case ACTION_DEFAULT:
3905 retval = start_with_fallback(bus) < 0;
3910 retval = reload_with_fallback(bus) < 0;
3913 case ACTION_INVALID:
3914 case ACTION_RUNLEVEL:
3916 assert_not_reached("Unknown action");
3922 dbus_connection_close(bus);
3923 dbus_connection_unref(bus);
3926 dbus_error_free(&error);