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 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") ||
1091 streq(args[0], "force-reload") ||
1092 streq(args[0], "condrestart") ? "ReloadOrTryRestartUnit" :
1096 (streq(args[0], "isolate") ||
1097 streq(args[0], "rescue") ||
1098 streq(args[0], "emergency")) ? "isolate" :
1102 one_name = table[verb_to_action(args[0])];
1105 assert(arg_action < ELEMENTSOF(table));
1106 assert(table[arg_action]);
1108 method = "StartUnit";
1110 mode = (arg_action == ACTION_EMERGENCY ||
1111 arg_action == ACTION_RESCUE) ? "isolate" : "replace";
1113 one_name = table[arg_action];
1116 if (!arg_no_block) {
1117 if ((r = enable_wait_for_jobs(bus)) < 0) {
1118 log_error("Could not watch jobs: %s", strerror(-r));
1122 if (!(s = set_new(string_hash_func, string_compare_func))) {
1123 log_error("Failed to allocate set.");
1132 if ((r = start_unit_one(bus, method, one_name, mode, s)) <= 0)
1135 for (i = 1; i < n; i++)
1136 if ((r = start_unit_one(bus, method, args[i], mode, s)) < 0)
1141 r = wait_for_jobs(bus, s);
1150 static int start_special(DBusConnection *bus, char **args, unsigned n) {
1156 r = start_unit(bus, args, n);
1159 warn_wall(verb_to_action(args[0]));
1164 static int check_unit(DBusConnection *bus, char **args, unsigned n) {
1165 DBusMessage *m = NULL, *reply = NULL;
1167 *interface = "org.freedesktop.systemd1.Unit",
1168 *property = "ActiveState";
1169 int r = -EADDRNOTAVAIL;
1176 dbus_error_init(&error);
1178 for (i = 1; i < n; i++) {
1179 const char *path = NULL;
1181 DBusMessageIter iter, sub;
1183 if (!(m = dbus_message_new_method_call(
1184 "org.freedesktop.systemd1",
1185 "/org/freedesktop/systemd1",
1186 "org.freedesktop.systemd1.Manager",
1188 log_error("Could not allocate message.");
1193 if (!dbus_message_append_args(m,
1194 DBUS_TYPE_STRING, &args[i],
1195 DBUS_TYPE_INVALID)) {
1196 log_error("Could not append arguments to message.");
1201 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1203 /* Hmm, cannot figure out anything about this unit... */
1207 dbus_error_free(&error);
1211 if (!dbus_message_get_args(reply, &error,
1212 DBUS_TYPE_OBJECT_PATH, &path,
1213 DBUS_TYPE_INVALID)) {
1214 log_error("Failed to parse reply: %s", error.message);
1219 dbus_message_unref(m);
1220 if (!(m = dbus_message_new_method_call(
1221 "org.freedesktop.systemd1",
1223 "org.freedesktop.DBus.Properties",
1225 log_error("Could not allocate message.");
1230 if (!dbus_message_append_args(m,
1231 DBUS_TYPE_STRING, &interface,
1232 DBUS_TYPE_STRING, &property,
1233 DBUS_TYPE_INVALID)) {
1234 log_error("Could not append arguments to message.");
1239 dbus_message_unref(reply);
1240 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1241 log_error("Failed to issue method call: %s", error.message);
1246 if (!dbus_message_iter_init(reply, &iter) ||
1247 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1248 log_error("Failed to parse reply.");
1253 dbus_message_iter_recurse(&iter, &sub);
1255 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1256 log_error("Failed to parse reply.");
1261 dbus_message_iter_get_basic(&sub, &state);
1266 if (streq(state, "active") || startswith(state, "reloading"))
1269 dbus_message_unref(m);
1270 dbus_message_unref(reply);
1276 dbus_message_unref(m);
1279 dbus_message_unref(reply);
1281 dbus_error_free(&error);
1286 typedef struct ExecStatusInfo {
1292 usec_t start_timestamp;
1293 usec_t exit_timestamp;
1298 LIST_FIELDS(struct ExecStatusInfo, exec);
1301 static void exec_status_info_free(ExecStatusInfo *i) {
1309 static int exec_status_info_deserialize(DBusMessageIter *sub, ExecStatusInfo *i) {
1310 uint64_t start_timestamp, exit_timestamp;
1311 DBusMessageIter sub2, sub3;
1315 int32_t code, status;
1321 if (dbus_message_iter_get_arg_type(sub) != DBUS_TYPE_STRUCT)
1324 dbus_message_iter_recurse(sub, &sub2);
1326 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0)
1329 if (!(i->path = strdup(path)))
1332 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_ARRAY ||
1333 dbus_message_iter_get_element_type(&sub2) != DBUS_TYPE_STRING)
1337 dbus_message_iter_recurse(&sub2, &sub3);
1338 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1339 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1340 dbus_message_iter_next(&sub3);
1345 if (!(i->argv = new0(char*, n+1)))
1349 dbus_message_iter_recurse(&sub2, &sub3);
1350 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1353 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1354 dbus_message_iter_get_basic(&sub3, &s);
1355 dbus_message_iter_next(&sub3);
1357 if (!(i->argv[n++] = strdup(s)))
1361 if (!dbus_message_iter_next(&sub2) ||
1362 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_BOOLEAN, &ignore, true) < 0 ||
1363 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_timestamp, true) < 0 ||
1364 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_timestamp, true) < 0 ||
1365 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, true) < 0 ||
1366 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &code, true) < 0 ||
1367 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &status, false) < 0)
1371 i->start_timestamp = (usec_t) start_timestamp;
1372 i->exit_timestamp = (usec_t) exit_timestamp;
1373 i->pid = (pid_t) pid;
1380 typedef struct UnitStatusInfo {
1382 const char *load_state;
1383 const char *active_state;
1384 const char *sub_state;
1386 const char *description;
1388 const char *fragment_path;
1389 const char *default_control_group;
1391 bool need_daemon_reload;
1396 const char *status_text;
1399 usec_t start_timestamp;
1400 usec_t exit_timestamp;
1402 int exit_code, exit_status;
1405 unsigned n_accepted;
1406 unsigned n_connections;
1410 const char *sysfs_path;
1412 /* Mount, Automount */
1418 LIST_HEAD(ExecStatusInfo, exec);
1421 static void print_status_info(UnitStatusInfo *i) {
1426 /* This shows pretty information about a unit. See
1427 * print_property() for a low-level property printer */
1429 printf("%s", strna(i->id));
1431 if (i->description && !streq_ptr(i->id, i->description))
1432 printf(" - %s", i->description);
1436 if (i->fragment_path)
1437 printf("\t Loaded: %s (%s)\n", strna(i->load_state), i->fragment_path);
1438 else if (streq_ptr(i->load_state, "failed"))
1439 printf("\t Loaded: %s%s%s\n",
1440 ansi_highlight(true),
1441 strna(i->load_state),
1442 ansi_highlight(false));
1444 printf("\t Loaded: %s\n", strna(i->load_state));
1446 if (streq_ptr(i->active_state, "maintenance")) {
1447 if (streq_ptr(i->active_state, i->sub_state))
1448 printf("\t Active: %s%s%s\n",
1449 ansi_highlight(true),
1450 strna(i->active_state),
1451 ansi_highlight(false));
1453 printf("\t Active: %s%s (%s)%s\n",
1454 ansi_highlight(true),
1455 strna(i->active_state),
1456 strna(i->sub_state),
1457 ansi_highlight(false));
1459 if (streq_ptr(i->active_state, i->sub_state))
1460 printf("\t Active: %s\n",
1461 strna(i->active_state));
1463 printf("\t Active: %s (%s)\n",
1464 strna(i->active_state),
1465 strna(i->sub_state));
1469 printf("\t Device: %s\n", i->sysfs_path);
1471 printf("\t Where: %s\n", i->where);
1473 printf("\t What: %s\n", i->what);
1476 printf("\tAccepted: %u; Connected: %u\n", i->n_accepted, i->n_connections);
1478 LIST_FOREACH(exec, p, i->exec) {
1481 /* Only show exited processes here */
1485 t = strv_join(p->argv, " ");
1486 printf("\t Exited: %u (%s, code=%s, ", p->pid, strna(t), sigchld_code_to_string(p->code));
1489 if (p->code == CLD_EXITED)
1490 printf("status=%i", p->status);
1492 printf("signal=%s", signal_to_string(p->status));
1495 if (i->main_pid == p->pid &&
1496 i->start_timestamp == p->start_timestamp &&
1497 i->exit_timestamp == p->start_timestamp)
1498 /* Let's not show this twice */
1501 if (p->pid == i->control_pid)
1505 if (i->main_pid > 0 || i->control_pid > 0) {
1508 if (i->main_pid > 0) {
1509 printf(" Main: %u", (unsigned) i->main_pid);
1513 get_process_name(i->main_pid, &t);
1519 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
1521 if (i->exit_code == CLD_EXITED)
1522 printf("status=%i", i->exit_status);
1524 printf("signal=%s", signal_to_string(i->exit_status));
1528 if (i->main_pid > 0 && i->control_pid > 0)
1531 if (i->control_pid > 0) {
1534 printf(" Control: %u", (unsigned) i->control_pid);
1536 get_process_name(i->control_pid, &t);
1547 printf("\t Status: \"%s\"\n", i->status_text);
1549 if (i->default_control_group) {
1552 printf("\t CGroup: %s\n", i->default_control_group);
1554 if ((c = columns()) > 18)
1559 show_cgroup_by_path(i->default_control_group, "\t\t ", c);
1562 if (i->need_daemon_reload)
1563 printf("\n%sWarning:%s Unit file changed on disk, 'systemctl %s daemon-reload' recommended.\n",
1564 ansi_highlight(true),
1565 ansi_highlight(false),
1566 arg_session ? "--session" : "--system");
1569 static int status_property(const char *name, DBusMessageIter *iter, UnitStatusInfo *i) {
1571 switch (dbus_message_iter_get_arg_type(iter)) {
1573 case DBUS_TYPE_STRING: {
1576 dbus_message_iter_get_basic(iter, &s);
1579 if (streq(name, "Id"))
1581 else if (streq(name, "LoadState"))
1583 else if (streq(name, "ActiveState"))
1584 i->active_state = s;
1585 else if (streq(name, "SubState"))
1587 else if (streq(name, "Description"))
1589 else if (streq(name, "FragmentPath"))
1590 i->fragment_path = s;
1591 else if (streq(name, "DefaultControlGroup"))
1592 i->default_control_group = s;
1593 else if (streq(name, "StatusText"))
1595 else if (streq(name, "SysFSPath"))
1597 else if (streq(name, "Where"))
1599 else if (streq(name, "What"))
1606 case DBUS_TYPE_BOOLEAN: {
1609 dbus_message_iter_get_basic(iter, &b);
1611 if (streq(name, "Accept"))
1613 else if (streq(name, "NeedDaemonReload"))
1614 i->need_daemon_reload = b;
1619 case DBUS_TYPE_UINT32: {
1622 dbus_message_iter_get_basic(iter, &u);
1624 if (streq(name, "MainPID")) {
1626 i->main_pid = (pid_t) u;
1629 } else if (streq(name, "ControlPID"))
1630 i->control_pid = (pid_t) u;
1631 else if (streq(name, "ExecMainPID")) {
1633 i->main_pid = (pid_t) u;
1634 } else if (streq(name, "NAccepted"))
1636 else if (streq(name, "NConnections"))
1637 i->n_connections = u;
1642 case DBUS_TYPE_INT32: {
1645 dbus_message_iter_get_basic(iter, &j);
1647 if (streq(name, "ExecMainCode"))
1648 i->exit_code = (int) j;
1649 else if (streq(name, "ExecMainStatus"))
1650 i->exit_status = (int) j;
1655 case DBUS_TYPE_UINT64: {
1658 dbus_message_iter_get_basic(iter, &u);
1660 if (streq(name, "ExecMainStartTimestamp"))
1661 i->start_timestamp = (usec_t) u;
1662 else if (streq(name, "ExecMainExitTimestamp"))
1663 i->exit_timestamp = (usec_t) u;
1668 case DBUS_TYPE_ARRAY: {
1670 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT &&
1671 startswith(name, "Exec")) {
1672 DBusMessageIter sub;
1674 dbus_message_iter_recurse(iter, &sub);
1675 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1676 ExecStatusInfo *info;
1679 if (!(info = new0(ExecStatusInfo, 1)))
1682 if ((r = exec_status_info_deserialize(&sub, info)) < 0) {
1687 LIST_PREPEND(ExecStatusInfo, exec, i->exec, info);
1689 dbus_message_iter_next(&sub);
1700 static int print_property(const char *name, DBusMessageIter *iter) {
1704 /* This is a low-level property printer, see
1705 * print_status_info() for the nicer output */
1707 if (arg_property && !strv_find(arg_property, name))
1710 switch (dbus_message_iter_get_arg_type(iter)) {
1712 case DBUS_TYPE_STRING: {
1714 dbus_message_iter_get_basic(iter, &s);
1716 if (arg_all || s[0])
1717 printf("%s=%s\n", name, s);
1722 case DBUS_TYPE_BOOLEAN: {
1724 dbus_message_iter_get_basic(iter, &b);
1725 printf("%s=%s\n", name, yes_no(b));
1730 case DBUS_TYPE_UINT64: {
1732 dbus_message_iter_get_basic(iter, &u);
1734 /* Yes, heuristics! But we can change this check
1735 * should it turn out to not be sufficient */
1737 if (strstr(name, "Timestamp")) {
1738 char timestamp[FORMAT_TIMESTAMP_MAX], *t;
1740 if ((t = format_timestamp(timestamp, sizeof(timestamp), u)) || arg_all)
1741 printf("%s=%s\n", name, strempty(t));
1742 } else if (strstr(name, "USec")) {
1743 char timespan[FORMAT_TIMESPAN_MAX];
1745 printf("%s=%s\n", name, format_timespan(timespan, sizeof(timespan), u));
1747 printf("%s=%llu\n", name, (unsigned long long) u);
1752 case DBUS_TYPE_UINT32: {
1754 dbus_message_iter_get_basic(iter, &u);
1756 if (strstr(name, "UMask") || strstr(name, "Mode"))
1757 printf("%s=%04o\n", name, u);
1759 printf("%s=%u\n", name, (unsigned) u);
1764 case DBUS_TYPE_INT32: {
1766 dbus_message_iter_get_basic(iter, &i);
1768 printf("%s=%i\n", name, (int) i);
1772 case DBUS_TYPE_STRUCT: {
1773 DBusMessageIter sub;
1774 dbus_message_iter_recurse(iter, &sub);
1776 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "Job")) {
1779 dbus_message_iter_get_basic(&sub, &u);
1782 printf("%s=%u\n", name, (unsigned) u);
1784 printf("%s=\n", name);
1787 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Unit")) {
1790 dbus_message_iter_get_basic(&sub, &s);
1792 if (arg_all || s[0])
1793 printf("%s=%s\n", name, s);
1801 case DBUS_TYPE_ARRAY:
1803 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
1804 DBusMessageIter sub;
1807 dbus_message_iter_recurse(iter, &sub);
1809 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1810 printf("%s=", name);
1812 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1815 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
1816 dbus_message_iter_get_basic(&sub, &s);
1817 printf("%s%s", space ? " " : "", s);
1820 dbus_message_iter_next(&sub);
1828 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_BYTE) {
1829 DBusMessageIter sub;
1831 dbus_message_iter_recurse(iter, &sub);
1833 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1834 printf("%s=", name);
1836 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1839 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_BYTE);
1840 dbus_message_iter_get_basic(&sub, &u);
1843 dbus_message_iter_next(&sub);
1851 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Paths")) {
1852 DBusMessageIter sub, sub2;
1854 dbus_message_iter_recurse(iter, &sub);
1855 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1856 const char *type, *path;
1858 dbus_message_iter_recurse(&sub, &sub2);
1860 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
1861 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0)
1862 printf("%s=%s\n", type, path);
1864 dbus_message_iter_next(&sub);
1869 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Timers")) {
1870 DBusMessageIter sub, sub2;
1872 dbus_message_iter_recurse(iter, &sub);
1873 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1875 uint64_t value, next_elapse;
1877 dbus_message_iter_recurse(&sub, &sub2);
1879 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &base, true) >= 0 &&
1880 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &value, true) >= 0 &&
1881 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &next_elapse, false) >= 0) {
1882 char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX];
1884 printf("%s={ value=%s ; next_elapse=%s }\n",
1886 format_timespan(timespan1, sizeof(timespan1), value),
1887 format_timespan(timespan2, sizeof(timespan2), next_elapse));
1890 dbus_message_iter_next(&sub);
1895 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && startswith(name, "Exec")) {
1896 DBusMessageIter sub;
1898 dbus_message_iter_recurse(iter, &sub);
1899 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1900 ExecStatusInfo info;
1903 if (exec_status_info_deserialize(&sub, &info) >= 0) {
1904 char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
1907 t = strv_join(info.argv, " ");
1909 printf("%s={ path=%s ; argv[]=%s ; ignore=%s ; start_time=[%s] ; stop_time=[%s] ; pid=%u ; code=%s ; status=%i%s%s }\n",
1913 yes_no(info.ignore),
1914 strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)),
1915 strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)),
1916 (unsigned) info. pid,
1917 sigchld_code_to_string(info.code),
1919 info.code == CLD_EXITED ? "" : "/",
1920 strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status)));
1926 strv_free(info.argv);
1928 dbus_message_iter_next(&sub);
1938 printf("%s=[unprintable]\n", name);
1943 static int show_one(DBusConnection *bus, const char *path, bool show_properties, bool *new_line) {
1944 DBusMessage *m = NULL, *reply = NULL;
1945 const char *interface = "";
1948 DBusMessageIter iter, sub, sub2, sub3;
1949 UnitStatusInfo info;
1957 dbus_error_init(&error);
1959 if (!(m = dbus_message_new_method_call(
1960 "org.freedesktop.systemd1",
1962 "org.freedesktop.DBus.Properties",
1964 log_error("Could not allocate message.");
1969 if (!dbus_message_append_args(m,
1970 DBUS_TYPE_STRING, &interface,
1971 DBUS_TYPE_INVALID)) {
1972 log_error("Could not append arguments to message.");
1977 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1978 log_error("Failed to issue method call: %s", error.message);
1983 if (!dbus_message_iter_init(reply, &iter) ||
1984 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
1985 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
1986 log_error("Failed to parse reply.");
1991 dbus_message_iter_recurse(&iter, &sub);
1998 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2001 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
2002 log_error("Failed to parse reply.");
2007 dbus_message_iter_recurse(&sub, &sub2);
2009 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
2010 log_error("Failed to parse reply.");
2015 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
2016 log_error("Failed to parse reply.");
2021 dbus_message_iter_recurse(&sub2, &sub3);
2023 if (show_properties)
2024 r = print_property(name, &sub3);
2026 r = status_property(name, &sub3, &info);
2029 log_error("Failed to parse reply.");
2034 dbus_message_iter_next(&sub);
2037 if (!show_properties)
2038 print_status_info(&info);
2040 while ((p = info.exec)) {
2041 LIST_REMOVE(ExecStatusInfo, exec, info.exec, p);
2042 exec_status_info_free(p);
2049 dbus_message_unref(m);
2052 dbus_message_unref(reply);
2054 dbus_error_free(&error);
2059 static int show(DBusConnection *bus, char **args, unsigned n) {
2060 DBusMessage *m = NULL, *reply = NULL;
2064 bool show_properties, new_line = false;
2069 dbus_error_init(&error);
2071 show_properties = !streq(args[0], "status");
2073 if (show_properties && n <= 1) {
2074 /* If not argument is specified inspect the manager
2077 r = show_one(bus, "/org/freedesktop/systemd1", show_properties, &new_line);
2081 for (i = 1; i < n; i++) {
2082 const char *path = NULL;
2085 if (!show_properties || safe_atou32(args[i], &id) < 0) {
2087 if (!(m = dbus_message_new_method_call(
2088 "org.freedesktop.systemd1",
2089 "/org/freedesktop/systemd1",
2090 "org.freedesktop.systemd1.Manager",
2092 log_error("Could not allocate message.");
2097 if (!dbus_message_append_args(m,
2098 DBUS_TYPE_STRING, &args[i],
2099 DBUS_TYPE_INVALID)) {
2100 log_error("Could not append arguments to message.");
2105 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2107 if (!dbus_error_has_name(&error, DBUS_ERROR_ACCESS_DENIED)) {
2108 log_error("Failed to issue method call: %s", error.message);
2113 dbus_error_free(&error);
2115 dbus_message_unref(m);
2116 if (!(m = dbus_message_new_method_call(
2117 "org.freedesktop.systemd1",
2118 "/org/freedesktop/systemd1",
2119 "org.freedesktop.systemd1.Manager",
2121 log_error("Could not allocate message.");
2126 if (!dbus_message_append_args(m,
2127 DBUS_TYPE_STRING, &args[i],
2128 DBUS_TYPE_INVALID)) {
2129 log_error("Could not append arguments to message.");
2134 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2135 log_error("Failed to issue method call: %s", error.message);
2143 if (!(m = dbus_message_new_method_call(
2144 "org.freedesktop.systemd1",
2145 "/org/freedesktop/systemd1",
2146 "org.freedesktop.systemd1.Manager",
2148 log_error("Could not allocate message.");
2153 if (!dbus_message_append_args(m,
2154 DBUS_TYPE_UINT32, &id,
2155 DBUS_TYPE_INVALID)) {
2156 log_error("Could not append arguments to message.");
2161 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2162 log_error("Failed to issue method call: %s", error.message);
2168 if (!dbus_message_get_args(reply, &error,
2169 DBUS_TYPE_OBJECT_PATH, &path,
2170 DBUS_TYPE_INVALID)) {
2171 log_error("Failed to parse reply: %s", error.message);
2176 if ((r = show_one(bus, path, show_properties, &new_line)) < 0)
2179 dbus_message_unref(m);
2180 dbus_message_unref(reply);
2188 dbus_message_unref(m);
2191 dbus_message_unref(reply);
2193 dbus_error_free(&error);
2198 static DBusHandlerResult monitor_filter(DBusConnection *connection, DBusMessage *message, void *data) {
2200 DBusMessage *m = NULL, *reply = NULL;
2205 dbus_error_init(&error);
2207 log_debug("Got D-Bus request: %s.%s() on %s",
2208 dbus_message_get_interface(message),
2209 dbus_message_get_member(message),
2210 dbus_message_get_path(message));
2212 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
2213 log_error("Warning! D-Bus connection terminated.");
2214 dbus_connection_close(connection);
2216 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitNew") ||
2217 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitRemoved")) {
2218 const char *id, *path;
2220 if (!dbus_message_get_args(message, &error,
2221 DBUS_TYPE_STRING, &id,
2222 DBUS_TYPE_OBJECT_PATH, &path,
2224 log_error("Failed to parse message: %s", error.message);
2225 else if (streq(dbus_message_get_member(message), "UnitNew"))
2226 printf("Unit %s added.\n", id);
2228 printf("Unit %s removed.\n", id);
2230 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobNew") ||
2231 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
2235 if (!dbus_message_get_args(message, &error,
2236 DBUS_TYPE_UINT32, &id,
2237 DBUS_TYPE_OBJECT_PATH, &path,
2239 log_error("Failed to parse message: %s", error.message);
2240 else if (streq(dbus_message_get_member(message), "JobNew"))
2241 printf("Job %u added.\n", id);
2243 printf("Job %u removed.\n", id);
2246 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Unit", "Changed") ||
2247 dbus_message_is_signal(message, "org.freedesktop.systemd1.Job", "Changed")) {
2249 const char *path, *interface, *property = "Id";
2250 DBusMessageIter iter, sub;
2252 path = dbus_message_get_path(message);
2253 interface = dbus_message_get_interface(message);
2255 if (!(m = dbus_message_new_method_call(
2256 "org.freedesktop.systemd1",
2258 "org.freedesktop.DBus.Properties",
2260 log_error("Could not allocate message.");
2264 if (!dbus_message_append_args(m,
2265 DBUS_TYPE_STRING, &interface,
2266 DBUS_TYPE_STRING, &property,
2267 DBUS_TYPE_INVALID)) {
2268 log_error("Could not append arguments to message.");
2272 if (!(reply = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) {
2273 log_error("Failed to issue method call: %s", error.message);
2277 if (!dbus_message_iter_init(reply, &iter) ||
2278 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2279 log_error("Failed to parse reply.");
2283 dbus_message_iter_recurse(&iter, &sub);
2285 if (streq(interface, "org.freedesktop.systemd1.Unit")) {
2288 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2289 log_error("Failed to parse reply.");
2293 dbus_message_iter_get_basic(&sub, &id);
2294 printf("Unit %s changed.\n", id);
2298 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT32) {
2299 log_error("Failed to parse reply.");
2303 dbus_message_iter_get_basic(&sub, &id);
2304 printf("Job %u changed.\n", id);
2310 dbus_message_unref(m);
2313 dbus_message_unref(reply);
2315 dbus_error_free(&error);
2316 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
2320 dbus_message_unref(m);
2323 dbus_message_unref(reply);
2325 dbus_error_free(&error);
2326 return DBUS_HANDLER_RESULT_NEED_MEMORY;
2329 static int monitor(DBusConnection *bus, char **args, unsigned n) {
2330 DBusMessage *m = NULL, *reply = NULL;
2334 dbus_error_init(&error);
2337 dbus_bus_add_match(bus,
2339 "sender='org.freedesktop.systemd1',"
2340 "interface='org.freedesktop.systemd1.Manager',"
2341 "path='/org/freedesktop/systemd1'",
2344 if (dbus_error_is_set(&error)) {
2345 log_error("Failed to add match: %s", error.message);
2350 dbus_bus_add_match(bus,
2352 "sender='org.freedesktop.systemd1',"
2353 "interface='org.freedesktop.systemd1.Unit',"
2357 if (dbus_error_is_set(&error)) {
2358 log_error("Failed to add match: %s", error.message);
2363 dbus_bus_add_match(bus,
2365 "sender='org.freedesktop.systemd1',"
2366 "interface='org.freedesktop.systemd1.Job',"
2370 if (dbus_error_is_set(&error)) {
2371 log_error("Failed to add match: %s", error.message);
2377 if (!dbus_connection_add_filter(bus, monitor_filter, NULL, NULL)) {
2378 log_error("Failed to add filter.");
2383 if (!(m = dbus_message_new_method_call(
2384 "org.freedesktop.systemd1",
2385 "/org/freedesktop/systemd1",
2386 "org.freedesktop.systemd1.Manager",
2388 log_error("Could not allocate message.");
2393 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2394 log_error("Failed to issue method call: %s", error.message);
2399 while (dbus_connection_read_write_dispatch(bus, -1))
2406 /* This is slightly dirty, since we don't undo the filter or the matches. */
2409 dbus_message_unref(m);
2412 dbus_message_unref(reply);
2414 dbus_error_free(&error);
2419 static int dump(DBusConnection *bus, char **args, unsigned n) {
2420 DBusMessage *m = NULL, *reply = NULL;
2425 dbus_error_init(&error);
2427 if (!(m = dbus_message_new_method_call(
2428 "org.freedesktop.systemd1",
2429 "/org/freedesktop/systemd1",
2430 "org.freedesktop.systemd1.Manager",
2432 log_error("Could not allocate message.");
2436 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2437 log_error("Failed to issue method call: %s", error.message);
2442 if (!dbus_message_get_args(reply, &error,
2443 DBUS_TYPE_STRING, &text,
2444 DBUS_TYPE_INVALID)) {
2445 log_error("Failed to parse reply: %s", error.message);
2450 fputs(text, stdout);
2456 dbus_message_unref(m);
2459 dbus_message_unref(reply);
2461 dbus_error_free(&error);
2466 static int snapshot(DBusConnection *bus, char **args, unsigned n) {
2467 DBusMessage *m = NULL, *reply = NULL;
2470 const char *name = "", *path, *id;
2471 dbus_bool_t cleanup = FALSE;
2472 DBusMessageIter iter, sub;
2474 *interface = "org.freedesktop.systemd1.Unit",
2477 dbus_error_init(&error);
2479 if (!(m = dbus_message_new_method_call(
2480 "org.freedesktop.systemd1",
2481 "/org/freedesktop/systemd1",
2482 "org.freedesktop.systemd1.Manager",
2483 "CreateSnapshot"))) {
2484 log_error("Could not allocate message.");
2491 if (!dbus_message_append_args(m,
2492 DBUS_TYPE_STRING, &name,
2493 DBUS_TYPE_BOOLEAN, &cleanup,
2494 DBUS_TYPE_INVALID)) {
2495 log_error("Could not append arguments to message.");
2500 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2501 log_error("Failed to issue method call: %s", error.message);
2506 if (!dbus_message_get_args(reply, &error,
2507 DBUS_TYPE_OBJECT_PATH, &path,
2508 DBUS_TYPE_INVALID)) {
2509 log_error("Failed to parse reply: %s", error.message);
2514 dbus_message_unref(m);
2515 if (!(m = dbus_message_new_method_call(
2516 "org.freedesktop.systemd1",
2518 "org.freedesktop.DBus.Properties",
2520 log_error("Could not allocate message.");
2524 if (!dbus_message_append_args(m,
2525 DBUS_TYPE_STRING, &interface,
2526 DBUS_TYPE_STRING, &property,
2527 DBUS_TYPE_INVALID)) {
2528 log_error("Could not append arguments to message.");
2533 dbus_message_unref(reply);
2534 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2535 log_error("Failed to issue method call: %s", error.message);
2540 if (!dbus_message_iter_init(reply, &iter) ||
2541 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2542 log_error("Failed to parse reply.");
2547 dbus_message_iter_recurse(&iter, &sub);
2549 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2550 log_error("Failed to parse reply.");
2555 dbus_message_iter_get_basic(&sub, &id);
2563 dbus_message_unref(m);
2566 dbus_message_unref(reply);
2568 dbus_error_free(&error);
2573 static int delete_snapshot(DBusConnection *bus, char **args, unsigned n) {
2574 DBusMessage *m = NULL, *reply = NULL;
2582 dbus_error_init(&error);
2584 for (i = 1; i < n; i++) {
2585 const char *path = NULL;
2587 if (!(m = dbus_message_new_method_call(
2588 "org.freedesktop.systemd1",
2589 "/org/freedesktop/systemd1",
2590 "org.freedesktop.systemd1.Manager",
2592 log_error("Could not allocate message.");
2597 if (!dbus_message_append_args(m,
2598 DBUS_TYPE_STRING, &args[i],
2599 DBUS_TYPE_INVALID)) {
2600 log_error("Could not append arguments to message.");
2605 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2606 log_error("Failed to issue method call: %s", error.message);
2611 if (!dbus_message_get_args(reply, &error,
2612 DBUS_TYPE_OBJECT_PATH, &path,
2613 DBUS_TYPE_INVALID)) {
2614 log_error("Failed to parse reply: %s", error.message);
2619 dbus_message_unref(m);
2620 if (!(m = dbus_message_new_method_call(
2621 "org.freedesktop.systemd1",
2623 "org.freedesktop.systemd1.Snapshot",
2625 log_error("Could not allocate message.");
2630 dbus_message_unref(reply);
2631 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2632 log_error("Failed to issue method call: %s", error.message);
2637 dbus_message_unref(m);
2638 dbus_message_unref(reply);
2646 dbus_message_unref(m);
2649 dbus_message_unref(reply);
2651 dbus_error_free(&error);
2656 static int clear_jobs(DBusConnection *bus, char **args, unsigned n) {
2657 DBusMessage *m = NULL, *reply = NULL;
2662 dbus_error_init(&error);
2664 if (arg_action == ACTION_RELOAD)
2666 else if (arg_action == ACTION_REEXEC)
2667 method = "Reexecute";
2669 assert(arg_action == ACTION_SYSTEMCTL);
2672 streq(args[0], "clear-jobs") ? "ClearJobs" :
2673 streq(args[0], "daemon-reload") ? "Reload" :
2674 streq(args[0], "daemon-reexec") ? "Reexecute" :
2675 streq(args[0], "reset-maintenance") ? "ResetMaintenance" :
2679 if (!(m = dbus_message_new_method_call(
2680 "org.freedesktop.systemd1",
2681 "/org/freedesktop/systemd1",
2682 "org.freedesktop.systemd1.Manager",
2684 log_error("Could not allocate message.");
2688 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2690 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
2691 /* There's always a fallback possible for
2692 * legacy actions. */
2697 log_error("Failed to issue method call: %s", error.message);
2706 dbus_message_unref(m);
2709 dbus_message_unref(reply);
2711 dbus_error_free(&error);
2716 static int reset_maintenance(DBusConnection *bus, char **args, unsigned n) {
2717 DBusMessage *m = NULL, *reply = NULL;
2723 dbus_error_init(&error);
2726 return clear_jobs(bus, args, n);
2728 for (i = 1; i < n; i++) {
2730 if (!(m = dbus_message_new_method_call(
2731 "org.freedesktop.systemd1",
2732 "/org/freedesktop/systemd1",
2733 "org.freedesktop.systemd1.Manager",
2734 "ResetMaintenanceUnit"))) {
2735 log_error("Could not allocate message.");
2740 if (!dbus_message_append_args(m,
2741 DBUS_TYPE_STRING, args + i,
2742 DBUS_TYPE_INVALID)) {
2743 log_error("Could not append arguments to message.");
2748 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2749 log_error("Failed to issue method call: %s", error.message);
2754 dbus_message_unref(m);
2755 dbus_message_unref(reply);
2763 dbus_message_unref(m);
2766 dbus_message_unref(reply);
2768 dbus_error_free(&error);
2773 static int show_enviroment(DBusConnection *bus, char **args, unsigned n) {
2774 DBusMessage *m = NULL, *reply = NULL;
2776 DBusMessageIter iter, sub, sub2;
2779 *interface = "org.freedesktop.systemd1.Manager",
2780 *property = "Environment";
2782 dbus_error_init(&error);
2784 if (!(m = dbus_message_new_method_call(
2785 "org.freedesktop.systemd1",
2786 "/org/freedesktop/systemd1",
2787 "org.freedesktop.DBus.Properties",
2789 log_error("Could not allocate message.");
2793 if (!dbus_message_append_args(m,
2794 DBUS_TYPE_STRING, &interface,
2795 DBUS_TYPE_STRING, &property,
2796 DBUS_TYPE_INVALID)) {
2797 log_error("Could not append arguments to message.");
2802 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2803 log_error("Failed to issue method call: %s", error.message);
2808 if (!dbus_message_iter_init(reply, &iter) ||
2809 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2810 log_error("Failed to parse reply.");
2815 dbus_message_iter_recurse(&iter, &sub);
2817 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
2818 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
2819 log_error("Failed to parse reply.");
2824 dbus_message_iter_recurse(&sub, &sub2);
2826 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
2829 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
2830 log_error("Failed to parse reply.");
2835 dbus_message_iter_get_basic(&sub2, &text);
2836 printf("%s\n", text);
2838 dbus_message_iter_next(&sub2);
2845 dbus_message_unref(m);
2848 dbus_message_unref(reply);
2850 dbus_error_free(&error);
2855 static int set_environment(DBusConnection *bus, char **args, unsigned n) {
2856 DBusMessage *m = NULL, *reply = NULL;
2860 DBusMessageIter iter, sub;
2863 dbus_error_init(&error);
2865 method = streq(args[0], "set-environment")
2867 : "UnsetEnvironment";
2869 if (!(m = dbus_message_new_method_call(
2870 "org.freedesktop.systemd1",
2871 "/org/freedesktop/systemd1",
2872 "org.freedesktop.systemd1.Manager",
2875 log_error("Could not allocate message.");
2879 dbus_message_iter_init_append(m, &iter);
2881 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
2882 log_error("Could not append arguments to message.");
2887 for (i = 1; i < n; i++)
2888 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &args[i])) {
2889 log_error("Could not append arguments to message.");
2894 if (!dbus_message_iter_close_container(&iter, &sub)) {
2895 log_error("Could not append arguments to message.");
2900 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2901 log_error("Failed to issue method call: %s", error.message);
2910 dbus_message_unref(m);
2913 dbus_message_unref(reply);
2915 dbus_error_free(&error);
2920 static int systemctl_help(void) {
2922 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
2923 "Send control commands to or query the systemd manager.\n\n"
2924 " -h --help Show this help\n"
2925 " -t --type=TYPE List only units of a particular type\n"
2926 " -p --property=NAME Show only properties by this name\n"
2927 " -a --all Show all units/properties, including dead/empty ones\n"
2928 " --full Don't ellipsize unit names.\n"
2929 " --fail When installing a new job, fail if conflicting jobs are\n"
2931 " --system Connect to system bus\n"
2932 " --session Connect to session bus\n"
2933 " --order When generating graph for dot, show only order\n"
2934 " --require When generating graph for dot, show only requirement\n"
2935 " -q --quiet Suppress output\n"
2936 " --no-block Do not wait until operation finished\n"
2937 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
2939 " list-units List units\n"
2940 " start [NAME...] Start one or more units\n"
2941 " stop [NAME...] Stop one or more units\n"
2942 " reload [NAME...] Reload one or more units\n"
2943 " restart [NAME...] Start or restart one or more units\n"
2944 " try-restart [NAME...] Restart one or more units if active\n"
2945 " reload-or-restart [NAME...] Reload one or more units is possible,\n"
2946 " otherwise start or restart\n"
2947 " reload-or-try-restart [NAME...] Reload one or more units is possible,\n"
2948 " otherwise restart if active\n"
2949 " isolate [NAME] Start one unit and stop all others\n"
2950 " check [NAME...] Check whether units are active\n"
2951 " status [NAME...] Show status of one or more units\n"
2952 " show [NAME...|JOB...] Show properties of one or more\n"
2953 " units/jobs/manager\n"
2954 " reset-maintenance [NAME...] Reset maintenance state for all, one\n"
2956 " load [NAME...] Load one or more units\n"
2957 " list-jobs List jobs\n"
2958 " cancel [JOB...] Cancel one or more jobs\n"
2959 " clear-jobs Cancel all jobs\n"
2960 " monitor Monitor unit/job changes\n"
2961 " dump Dump server status\n"
2962 " dot Dump dependency graph for dot(1)\n"
2963 " snapshot [NAME] Create a snapshot\n"
2964 " delete [NAME...] Remove one or more snapshots\n"
2965 " daemon-reload Reload systemd manager configuration\n"
2966 " daemon-reexec Reexecute systemd manager\n"
2967 " daemon-exit Ask the systemd manager to quit\n"
2968 " show-environment Dump environment\n"
2969 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
2970 " unset-environment [NAME...] Unset one or more environment variables\n"
2971 " halt Shut down and halt the system\n"
2972 " poweroff Shut down and power-off the system\n"
2973 " reboot Shut down and reboot the system\n"
2974 " rescue Enter system rescue mode\n"
2975 " emergency Enter system emergency mode\n"
2976 " default Enter system default mode\n",
2977 program_invocation_short_name);
2982 static int halt_help(void) {
2984 printf("%s [OPTIONS...]\n\n"
2985 "%s the system.\n\n"
2986 " --help Show this help\n"
2987 " --halt Halt the machine\n"
2988 " -p --poweroff Switch off the machine\n"
2989 " --reboot Reboot the machine\n"
2990 " -f --force Force immediate halt/power-off/reboot\n"
2991 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
2992 " -d --no-wtmp Don't write wtmp record\n"
2993 " -n --no-sync Don't sync before halt/power-off/reboot\n"
2994 " --no-wall Don't send wall message before halt/power-off/reboot\n",
2995 program_invocation_short_name,
2996 arg_action == ACTION_REBOOT ? "Reboot" :
2997 arg_action == ACTION_POWEROFF ? "Power off" :
3003 static int shutdown_help(void) {
3005 printf("%s [OPTIONS...] [now] [WALL...]\n\n"
3006 "Shut down the system.\n\n"
3007 " --help Show this help\n"
3008 " -H --halt Halt the machine\n"
3009 " -P --poweroff Power-off the machine\n"
3010 " -r --reboot Reboot the machine\n"
3011 " -h Equivalent to --poweroff, overriden by --halt\n"
3012 " -k Don't halt/power-off/reboot, just send warnings\n"
3013 " --no-wall Don't send wall message before halt/power-off/reboot\n",
3014 program_invocation_short_name);
3019 static int telinit_help(void) {
3021 printf("%s [OPTIONS...] {COMMAND}\n\n"
3022 "Send control commands to the init daemon.\n\n"
3023 " --help Show this help\n"
3024 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
3026 " 0 Power-off the machine\n"
3027 " 6 Reboot the machine\n"
3028 " 2, 3, 4, 5 Start runlevelX.target unit\n"
3029 " 1, s, S Enter rescue mode\n"
3030 " q, Q Reload init daemon configuration\n"
3031 " u, U Reexecute init daemon\n",
3032 program_invocation_short_name);
3037 static int runlevel_help(void) {
3039 printf("%s [OPTIONS...]\n\n"
3040 "Prints the previous and current runlevel of the init system.\n\n"
3041 " --help Show this help\n",
3042 program_invocation_short_name);
3047 static int systemctl_parse_argv(int argc, char *argv[]) {
3060 static const struct option options[] = {
3061 { "help", no_argument, NULL, 'h' },
3062 { "type", required_argument, NULL, 't' },
3063 { "property", required_argument, NULL, 'p' },
3064 { "all", no_argument, NULL, 'a' },
3065 { "full", no_argument, NULL, ARG_FULL },
3066 { "fail", no_argument, NULL, ARG_FAIL },
3067 { "session", no_argument, NULL, ARG_SESSION },
3068 { "system", no_argument, NULL, ARG_SYSTEM },
3069 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
3070 { "no-wall", no_argument, NULL, ARG_NO_WALL },
3071 { "quiet", no_argument, NULL, 'q' },
3072 { "order", no_argument, NULL, ARG_ORDER },
3073 { "require", no_argument, NULL, ARG_REQUIRE },
3074 { NULL, 0, NULL, 0 }
3082 while ((c = getopt_long(argc, argv, "ht:p:aq", options, NULL)) >= 0) {
3097 if (!(l = strv_append(arg_property, optarg)))
3100 strv_free(arg_property);
3103 /* If the user asked for a particular
3104 * property, show it to him, even if it is
3123 arg_session = false;
3127 arg_no_block = true;
3135 arg_dot = DOT_ORDER;
3139 arg_dot = DOT_REQUIRE;
3154 log_error("Unknown option code %c", c);
3162 static int halt_parse_argv(int argc, char *argv[]) {
3171 static const struct option options[] = {
3172 { "help", no_argument, NULL, ARG_HELP },
3173 { "halt", no_argument, NULL, ARG_HALT },
3174 { "poweroff", no_argument, NULL, 'p' },
3175 { "reboot", no_argument, NULL, ARG_REBOOT },
3176 { "force", no_argument, NULL, 'f' },
3177 { "wtmp-only", no_argument, NULL, 'w' },
3178 { "no-wtmp", no_argument, NULL, 'd' },
3179 { "no-sync", no_argument, NULL, 'n' },
3180 { "no-wall", no_argument, NULL, ARG_NO_WALL },
3181 { NULL, 0, NULL, 0 }
3189 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
3190 if (runlevel == '0' || runlevel == '6')
3191 arg_immediate = true;
3193 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
3201 arg_action = ACTION_HALT;
3205 arg_action = ACTION_POWEROFF;
3209 arg_action = ACTION_REBOOT;
3213 arg_immediate = true;
3234 /* Compatibility nops */
3241 log_error("Unknown option code %c", c);
3246 if (optind < argc) {
3247 log_error("Too many arguments.");
3254 static int shutdown_parse_argv(int argc, char *argv[]) {
3261 static const struct option options[] = {
3262 { "help", no_argument, NULL, ARG_HELP },
3263 { "halt", no_argument, NULL, 'H' },
3264 { "poweroff", no_argument, NULL, 'P' },
3265 { "reboot", no_argument, NULL, 'r' },
3266 { "no-wall", no_argument, NULL, ARG_NO_WALL },
3267 { NULL, 0, NULL, 0 }
3275 while ((c = getopt_long(argc, argv, "HPrhkt:a", options, NULL)) >= 0) {
3283 arg_action = ACTION_HALT;
3287 arg_action = ACTION_POWEROFF;
3291 arg_action = ACTION_REBOOT;
3295 if (arg_action != ACTION_HALT)
3296 arg_action = ACTION_POWEROFF;
3309 /* Compatibility nops */
3316 log_error("Unknown option code %c", c);
3321 if (argc > optind && !streq(argv[optind], "now"))
3322 log_warning("First argument '%s' isn't 'now'. Ignoring.", argv[optind]);
3324 /* We ignore the time argument */
3325 if (argc > optind + 1)
3326 arg_wall = argv + optind + 1;
3333 static int telinit_parse_argv(int argc, char *argv[]) {
3340 static const struct option options[] = {
3341 { "help", no_argument, NULL, ARG_HELP },
3342 { "no-wall", no_argument, NULL, ARG_NO_WALL },
3343 { NULL, 0, NULL, 0 }
3346 static const struct {
3350 { '0', ACTION_POWEROFF },
3351 { '6', ACTION_REBOOT },
3352 { '1', ACTION_RESCUE },
3353 { '2', ACTION_RUNLEVEL2 },
3354 { '3', ACTION_RUNLEVEL3 },
3355 { '4', ACTION_RUNLEVEL4 },
3356 { '5', ACTION_RUNLEVEL5 },
3357 { 's', ACTION_RESCUE },
3358 { 'S', ACTION_RESCUE },
3359 { 'q', ACTION_RELOAD },
3360 { 'Q', ACTION_RELOAD },
3361 { 'u', ACTION_REEXEC },
3362 { 'U', ACTION_REEXEC }
3371 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
3386 log_error("Unknown option code %c", c);
3391 if (optind >= argc) {
3396 if (optind + 1 < argc) {
3397 log_error("Too many arguments.");
3401 if (strlen(argv[optind]) != 1) {
3402 log_error("Expected single character argument.");
3406 for (i = 0; i < ELEMENTSOF(table); i++)
3407 if (table[i].from == argv[optind][0])
3410 if (i >= ELEMENTSOF(table)) {
3411 log_error("Unknown command %s.", argv[optind]);
3415 arg_action = table[i].to;
3422 static int runlevel_parse_argv(int argc, char *argv[]) {
3428 static const struct option options[] = {
3429 { "help", no_argument, NULL, ARG_HELP },
3430 { NULL, 0, NULL, 0 }
3438 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
3449 log_error("Unknown option code %c", c);
3454 if (optind < argc) {
3455 log_error("Too many arguments.");
3462 static int parse_argv(int argc, char *argv[]) {
3466 if (program_invocation_short_name) {
3468 if (strstr(program_invocation_short_name, "halt")) {
3469 arg_action = ACTION_HALT;
3470 return halt_parse_argv(argc, argv);
3471 } else if (strstr(program_invocation_short_name, "poweroff")) {
3472 arg_action = ACTION_POWEROFF;
3473 return halt_parse_argv(argc, argv);
3474 } else if (strstr(program_invocation_short_name, "reboot")) {
3475 arg_action = ACTION_REBOOT;
3476 return halt_parse_argv(argc, argv);
3477 } else if (strstr(program_invocation_short_name, "shutdown")) {
3478 arg_action = ACTION_POWEROFF;
3479 return shutdown_parse_argv(argc, argv);
3480 } else if (strstr(program_invocation_short_name, "init")) {
3481 arg_action = ACTION_INVALID;
3482 return telinit_parse_argv(argc, argv);
3483 } else if (strstr(program_invocation_short_name, "runlevel")) {
3484 arg_action = ACTION_RUNLEVEL;
3485 return runlevel_parse_argv(argc, argv);
3489 arg_action = ACTION_SYSTEMCTL;
3490 return systemctl_parse_argv(argc, argv);
3493 static int action_to_runlevel(void) {
3495 static const char table[_ACTION_MAX] = {
3496 [ACTION_HALT] = '0',
3497 [ACTION_POWEROFF] = '0',
3498 [ACTION_REBOOT] = '6',
3499 [ACTION_RUNLEVEL2] = '2',
3500 [ACTION_RUNLEVEL3] = '3',
3501 [ACTION_RUNLEVEL4] = '4',
3502 [ACTION_RUNLEVEL5] = '5',
3503 [ACTION_RESCUE] = '1'
3506 assert(arg_action < _ACTION_MAX);
3508 return table[arg_action];
3511 static int talk_upstart(void) {
3512 DBusMessage *m = NULL, *reply = NULL;
3514 int previous, rl, r;
3516 env1_buf[] = "RUNLEVEL=X",
3517 env2_buf[] = "PREVLEVEL=X";
3518 char *env1 = env1_buf, *env2 = env2_buf;
3519 const char *emit = "runlevel";
3520 dbus_bool_t b_false = FALSE;
3521 DBusMessageIter iter, sub;
3522 DBusConnection *bus;
3524 dbus_error_init(&error);
3526 if (!(rl = action_to_runlevel()))
3529 if (utmp_get_runlevel(&previous, NULL) < 0)
3532 if (!(bus = dbus_connection_open_private("unix:abstract=/com/ubuntu/upstart", &error))) {
3533 if (dbus_error_has_name(&error, DBUS_ERROR_NO_SERVER)) {
3538 log_error("Failed to connect to Upstart bus: %s", error.message);
3543 if ((r = bus_check_peercred(bus)) < 0) {
3544 log_error("Failed to verify owner of bus.");
3548 if (!(m = dbus_message_new_method_call(
3549 "com.ubuntu.Upstart",
3550 "/com/ubuntu/Upstart",
3551 "com.ubuntu.Upstart0_6",
3554 log_error("Could not allocate message.");
3559 dbus_message_iter_init_append(m, &iter);
3561 env1_buf[sizeof(env1_buf)-2] = rl;
3562 env2_buf[sizeof(env2_buf)-2] = previous;
3564 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
3565 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
3566 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
3567 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
3568 !dbus_message_iter_close_container(&iter, &sub) ||
3569 !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
3570 log_error("Could not append arguments to message.");
3575 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3577 if (error_is_no_service(&error)) {
3582 log_error("Failed to issue method call: %s", error.message);
3591 dbus_message_unref(m);
3594 dbus_message_unref(reply);
3597 dbus_connection_close(bus);
3598 dbus_connection_unref(bus);
3601 dbus_error_free(&error);
3606 static int talk_initctl(void) {
3607 struct init_request request;
3611 if (!(rl = action_to_runlevel()))
3615 request.magic = INIT_MAGIC;
3616 request.sleeptime = 0;
3617 request.cmd = INIT_CMD_RUNLVL;
3618 request.runlevel = rl;
3620 if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
3622 if (errno == ENOENT)
3625 log_error("Failed to open "INIT_FIFO": %m");
3630 r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
3631 close_nointr_nofail(fd);
3634 log_error("Failed to write to "INIT_FIFO": %m");
3635 return errno ? -errno : -EIO;
3641 static int systemctl_main(DBusConnection *bus, int argc, char *argv[]) {
3643 static const struct {
3651 int (* const dispatch)(DBusConnection *bus, char **args, unsigned n);
3653 { "list-units", LESS, 1, list_units },
3654 { "list-jobs", EQUAL, 1, list_jobs },
3655 { "clear-jobs", EQUAL, 1, clear_jobs },
3656 { "load", MORE, 2, load_unit },
3657 { "cancel", MORE, 2, cancel_job },
3658 { "start", MORE, 2, start_unit },
3659 { "stop", MORE, 2, start_unit },
3660 { "reload", MORE, 2, start_unit },
3661 { "restart", MORE, 2, start_unit },
3662 { "try-restart", MORE, 2, start_unit },
3663 { "reload-or-restart", MORE, 2, start_unit },
3664 { "reload-or-try-restart", MORE, 2, start_unit },
3665 { "force-reload", MORE, 2, start_unit }, /* For compatibility with SysV */
3666 { "condrestart", MORE, 2, start_unit }, /* For compatibility with RH */
3667 { "isolate", EQUAL, 2, start_unit },
3668 { "check", MORE, 2, check_unit },
3669 { "show", MORE, 1, show },
3670 { "status", MORE, 2, show },
3671 { "monitor", EQUAL, 1, monitor },
3672 { "dump", EQUAL, 1, dump },
3673 { "dot", EQUAL, 1, dot },
3674 { "snapshot", LESS, 2, snapshot },
3675 { "delete", MORE, 2, delete_snapshot },
3676 { "daemon-reload", EQUAL, 1, clear_jobs },
3677 { "daemon-reexec", EQUAL, 1, clear_jobs },
3678 { "daemon-exit", EQUAL, 1, clear_jobs },
3679 { "show-environment", EQUAL, 1, show_enviroment },
3680 { "set-environment", MORE, 2, set_environment },
3681 { "unset-environment", MORE, 2, set_environment },
3682 { "halt", EQUAL, 1, start_special },
3683 { "poweroff", EQUAL, 1, start_special },
3684 { "reboot", EQUAL, 1, start_special },
3685 { "default", EQUAL, 1, start_special },
3686 { "rescue", EQUAL, 1, start_special },
3687 { "emergency", EQUAL, 1, start_special },
3688 { "reset-maintenance", MORE, 1, reset_maintenance },
3698 left = argc - optind;
3701 /* Special rule: no arguments means "list-units" */
3704 if (streq(argv[optind], "help")) {
3709 for (i = 0; i < ELEMENTSOF(verbs); i++)
3710 if (streq(argv[optind], verbs[i].verb))
3713 if (i >= ELEMENTSOF(verbs)) {
3714 log_error("Unknown operation %s", argv[optind]);
3719 switch (verbs[i].argc_cmp) {
3722 if (left != verbs[i].argc) {
3723 log_error("Invalid number of arguments.");
3730 if (left < verbs[i].argc) {
3731 log_error("Too few arguments.");
3738 if (left > verbs[i].argc) {
3739 log_error("Too many arguments.");
3746 assert_not_reached("Unknown comparison operator.");
3749 return verbs[i].dispatch(bus, argv + optind, left);
3752 static int reload_with_fallback(DBusConnection *bus) {
3756 /* First, try systemd via D-Bus. */
3757 if ((r = clear_jobs(bus, NULL, 0)) > 0)
3761 /* Nothing else worked, so let's try signals */
3762 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
3764 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
3765 log_error("kill() failed: %m");
3772 static int start_with_fallback(DBusConnection *bus) {
3777 /* First, try systemd via D-Bus. */
3778 if ((r = start_unit(bus, NULL, 0)) > 0)
3781 /* Hmm, talking to systemd via D-Bus didn't work. Then
3782 * let's try to talk to Upstart via D-Bus. */
3783 if ((r = talk_upstart()) > 0)
3787 /* Nothing else worked, so let's try
3789 if ((r = talk_initctl()) != 0)
3792 log_error("Failed to talk to init daemon.");
3796 warn_wall(arg_action);
3800 static int halt_main(DBusConnection *bus) {
3803 if (geteuid() != 0) {
3804 log_error("Must to be root.");
3808 if (!arg_dry && !arg_immediate)
3809 return start_with_fallback(bus);
3812 if ((r = utmp_put_shutdown(0)) < 0)
3813 log_warning("Failed to write utmp record: %s", strerror(-r));
3821 /* Make sure C-A-D is handled by the kernel from this
3823 reboot(RB_ENABLE_CAD);
3825 switch (arg_action) {
3828 log_info("Halting");
3829 reboot(RB_HALT_SYSTEM);
3832 case ACTION_POWEROFF:
3833 log_info("Powering off");
3834 reboot(RB_POWER_OFF);
3838 log_info("Rebooting");
3839 reboot(RB_AUTOBOOT);
3843 assert_not_reached("Unknown halt action.");
3846 /* We should never reach this. */
3850 static int runlevel_main(void) {
3851 int r, runlevel, previous;
3853 if ((r = utmp_get_runlevel(&runlevel, &previous)) < 0) {
3859 previous <= 0 ? 'N' : previous,
3860 runlevel <= 0 ? 'N' : runlevel);
3865 int main(int argc, char*argv[]) {
3867 DBusConnection *bus = NULL;
3870 dbus_error_init(&error);
3872 log_parse_environment();
3874 if ((r = parse_argv(argc, argv)) < 0)
3881 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
3882 * let's shortcut this */
3883 if (arg_action == ACTION_RUNLEVEL) {
3884 retval = runlevel_main() < 0;
3888 bus_connect(arg_session ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &bus, &private_bus, &error);
3890 switch (arg_action) {
3892 case ACTION_SYSTEMCTL: {
3895 log_error("Failed to get D-Bus connection: %s", error.message);
3899 retval = systemctl_main(bus, argc, argv) < 0;
3904 case ACTION_POWEROFF:
3906 retval = halt_main(bus) < 0;
3909 case ACTION_RUNLEVEL2:
3910 case ACTION_RUNLEVEL3:
3911 case ACTION_RUNLEVEL4:
3912 case ACTION_RUNLEVEL5:
3914 case ACTION_EMERGENCY:
3915 case ACTION_DEFAULT:
3916 retval = start_with_fallback(bus) < 0;
3921 retval = reload_with_fallback(bus) < 0;
3924 case ACTION_INVALID:
3925 case ACTION_RUNLEVEL:
3927 assert_not_reached("Unknown action");
3933 dbus_connection_close(bus);
3934 dbus_connection_unref(bus);
3937 dbus_error_free(&error);
3941 strv_free(arg_property);