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>
33 #include <dbus/dbus.h>
39 #include "utmp-wtmp.h"
43 static const char *arg_type = NULL;
44 static bool arg_all = false;
45 static bool arg_replace = false;
46 static bool arg_session = false;
47 static bool arg_block = false;
48 static bool arg_immediate = false;
49 static bool arg_no_wtmp = false;
50 static bool arg_no_sync = false;
51 static bool arg_no_wall = false;
52 static bool arg_dry = false;
53 static char **arg_wall = NULL;
71 } arg_action = ACTION_SYSTEMCTL;
73 static bool error_is_no_service(DBusError *error) {
77 if (!dbus_error_is_set(error))
80 if (dbus_error_has_name(error, DBUS_ERROR_NAME_HAS_NO_OWNER))
83 if (dbus_error_has_name(error, DBUS_ERROR_SERVICE_UNKNOWN))
86 return startswith(error->name, "org.freedesktop.DBus.Error.Spawn.");
89 static int bus_iter_get_basic_and_next(DBusMessageIter *iter, int type, void *data, bool next) {
94 if (dbus_message_iter_get_arg_type(iter) != type)
97 dbus_message_iter_get_basic(iter, data);
99 if (!dbus_message_iter_next(iter) != !next)
105 static int columns(void) {
106 static int parsed_columns = 0;
109 if (parsed_columns > 0)
110 return parsed_columns;
112 if ((e = getenv("COLUMNS")))
113 parsed_columns = atoi(e);
115 if (parsed_columns <= 0) {
119 if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) >= 0)
120 parsed_columns = ws.ws_col;
123 if (parsed_columns <= 0)
126 return parsed_columns;
130 static void warn_wall(enum action action) {
131 static const char *table[_ACTION_MAX] = {
132 [ACTION_HALT] = "The system is going down for system halt NOW!",
133 [ACTION_REBOOT] = "The system is going down for reboot NOW!",
134 [ACTION_POWEROFF] = "The system is going down for power-off NOW!",
135 [ACTION_RESCUE] = "The system is going down to rescue mode NOW!",
136 [ACTION_EMERGENCY] = "The system is going down to emergency mode NOW!"
145 utmp_wall(table[action]);
148 static int list_units(DBusConnection *bus, char **args, unsigned n) {
149 DBusMessage *m = NULL, *reply = NULL;
152 DBusMessageIter iter, sub, sub2;
155 dbus_error_init(&error);
159 if (!(m = dbus_message_new_method_call(
160 "org.freedesktop.systemd1",
161 "/org/freedesktop/systemd1",
162 "org.freedesktop.systemd1.Manager",
164 log_error("Could not allocate message.");
168 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
169 log_error("Failed to issue method call: %s", error.message);
174 if (!dbus_message_iter_init(reply, &iter) ||
175 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
176 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
177 log_error("Failed to parse reply.");
182 dbus_message_iter_recurse(&iter, &sub);
184 printf("%-45s %-6s %-12s %-12s %-15s %s\n", "UNIT", "LOAD", "ACTIVE", "SUB", "JOB", "DESCRIPTION");
186 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
187 const char *id, *description, *load_state, *active_state, *sub_state, *unit_state, *job_type, *job_path, *dot;
190 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
191 log_error("Failed to parse reply.");
196 dbus_message_iter_recurse(&sub, &sub2);
198 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
199 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &description, true) < 0 ||
200 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &load_state, true) < 0 ||
201 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &active_state, true) < 0 ||
202 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &sub_state, true) < 0 ||
203 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_state, true) < 0 ||
204 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &job_id, true) < 0 ||
205 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &job_type, true) < 0 ||
206 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, false) < 0) {
207 log_error("Failed to parse reply.");
212 if ((!arg_type || ((dot = strrchr(id, '.')) &&
213 streq(dot+1, arg_type))) &&
214 (arg_all || !streq(active_state, "inactive"))) {
218 printf("%-45s %-6s %-12s %-12s%n", id, load_state, active_state, sub_state, &a);
221 printf(" %-15s%n", job_type, &b);
225 if (a + b + 2 < columns()) {
229 printf("%.*s", columns() - a - b - 2, description);
236 dbus_message_iter_next(&sub);
240 printf("\n%u units listed.\n", k);
242 printf("\n%u live units listed. Pass --all to see dead units, too.\n", k);
248 dbus_message_unref(m);
251 dbus_message_unref(reply);
253 dbus_error_free(&error);
258 static int list_jobs(DBusConnection *bus, char **args, unsigned n) {
259 DBusMessage *m = NULL, *reply = NULL;
262 DBusMessageIter iter, sub, sub2;
265 dbus_error_init(&error);
269 if (!(m = dbus_message_new_method_call(
270 "org.freedesktop.systemd1",
271 "/org/freedesktop/systemd1",
272 "org.freedesktop.systemd1.Manager",
274 log_error("Could not allocate message.");
278 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
279 log_error("Failed to issue method call: %s", error.message);
284 if (!dbus_message_iter_init(reply, &iter) ||
285 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
286 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
287 log_error("Failed to parse reply.");
292 dbus_message_iter_recurse(&iter, &sub);
294 printf("%4s %-45s %-17s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
296 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
297 const char *name, *type, *state, *job_path, *unit_path;
300 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
301 log_error("Failed to parse reply.");
306 dbus_message_iter_recurse(&sub, &sub2);
308 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &id, true) < 0 ||
309 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0 ||
310 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
311 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, true) < 0 ||
312 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, true) < 0 ||
313 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, false) < 0) {
314 log_error("Failed to parse reply.");
319 printf("%4u %-45s %-17s %-7s\n", id, name, type, state);
322 dbus_message_iter_next(&sub);
325 printf("\n%u jobs listed.\n", k);
330 dbus_message_unref(m);
333 dbus_message_unref(reply);
335 dbus_error_free(&error);
340 static int load_unit(DBusConnection *bus, char **args, unsigned n) {
341 DBusMessage *m = NULL, *reply = NULL;
346 dbus_error_init(&error);
351 for (i = 1; i < n; i++) {
353 if (!(m = dbus_message_new_method_call(
354 "org.freedesktop.systemd1",
355 "/org/freedesktop/systemd1",
356 "org.freedesktop.systemd1.Manager",
358 log_error("Could not allocate message.");
363 if (!dbus_message_append_args(m,
364 DBUS_TYPE_STRING, &args[i],
365 DBUS_TYPE_INVALID)) {
366 log_error("Could not append arguments to message.");
371 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
372 log_error("Failed to issue method call: %s", error.message);
377 dbus_message_unref(m);
378 dbus_message_unref(reply);
387 dbus_message_unref(m);
390 dbus_message_unref(reply);
392 dbus_error_free(&error);
397 static int cancel_job(DBusConnection *bus, char **args, unsigned n) {
398 DBusMessage *m = NULL, *reply = NULL;
403 dbus_error_init(&error);
408 for (i = 1; i < n; i++) {
412 if (!(m = dbus_message_new_method_call(
413 "org.freedesktop.systemd1",
414 "/org/freedesktop/systemd1",
415 "org.freedesktop.systemd1.Manager",
417 log_error("Could not allocate message.");
422 if ((r = safe_atou(args[i], &id)) < 0) {
423 log_error("Failed to parse job id: %s", strerror(-r));
427 assert_cc(sizeof(uint32_t) == sizeof(id));
428 if (!dbus_message_append_args(m,
429 DBUS_TYPE_UINT32, &id,
430 DBUS_TYPE_INVALID)) {
431 log_error("Could not append arguments to message.");
436 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
437 log_error("Failed to issue method call: %s", error.message);
442 if (!dbus_message_get_args(reply, &error,
443 DBUS_TYPE_OBJECT_PATH, &path,
444 DBUS_TYPE_INVALID)) {
445 log_error("Failed to parse reply: %s", error.message);
450 dbus_message_unref(m);
451 if (!(m = dbus_message_new_method_call(
452 "org.freedesktop.systemd1",
454 "org.freedesktop.systemd1.Job",
456 log_error("Could not allocate message.");
461 dbus_message_unref(reply);
462 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
463 log_error("Failed to issue method call: %s", error.message);
468 dbus_message_unref(m);
469 dbus_message_unref(reply);
477 dbus_message_unref(m);
480 dbus_message_unref(reply);
482 dbus_error_free(&error);
487 static DBusHandlerResult wait_filter(DBusConnection *connection, DBusMessage *message, void *data) {
495 dbus_error_init(&error);
497 /* log_debug("Got D-Bus request: %s.%s() on %s", */
498 /* dbus_message_get_interface(message), */
499 /* dbus_message_get_member(message), */
500 /* dbus_message_get_path(message)); */
502 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
503 log_error("Warning! D-Bus connection terminated.");
504 dbus_connection_close(connection);
506 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
510 if (!dbus_message_get_args(message, &error,
511 DBUS_TYPE_UINT32, &id,
512 DBUS_TYPE_OBJECT_PATH, &path,
514 log_error("Failed to parse message: %s", error.message);
518 if ((p = set_remove(s, (char*) path)))
523 dbus_error_free(&error);
524 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
527 static int enable_wait_for_jobs(DBusConnection *bus) {
529 DBusMessage *m = NULL, *reply = NULL;
534 dbus_error_init(&error);
536 dbus_bus_add_match(bus,
538 "sender='org.freedesktop.systemd1',"
539 "interface='org.freedesktop.systemd1.Manager',"
540 "member='JobRemoved',"
541 "path='/org/freedesktop/systemd1'",
544 if (dbus_error_is_set(&error)) {
545 log_error("Failed to add match: %s", error.message);
550 if (!(m = dbus_message_new_method_call(
551 "org.freedesktop.systemd1",
552 "/org/freedesktop/systemd1",
553 "org.freedesktop.systemd1.Manager",
555 log_error("Could not allocate message.");
560 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
561 log_error("Failed to issue method call: %s", error.message);
569 /* This is slightly dirty, since we don't undo the match registrations. */
572 dbus_message_unref(m);
575 dbus_message_unref(reply);
577 dbus_error_free(&error);
582 static int wait_for_jobs(DBusConnection *bus, Set *s) {
588 if (!dbus_connection_add_filter(bus, wait_filter, s, NULL)) {
589 log_error("Failed to add filter.");
594 while (!set_isempty(s) &&
595 dbus_connection_read_write_dispatch(bus, -1))
601 /* This is slightly dirty, since we don't undo the filter registration. */
606 static int start_unit_one(
613 DBusMessage *m = NULL, *reply = NULL;
621 assert(!arg_block || s);
623 dbus_error_init(&error);
625 if (!(m = dbus_message_new_method_call(
626 "org.freedesktop.systemd1",
627 "/org/freedesktop/systemd1",
628 "org.freedesktop.systemd1.Manager",
630 log_error("Could not allocate message.");
635 if (!dbus_message_append_args(m,
636 DBUS_TYPE_STRING, &name,
637 DBUS_TYPE_STRING, &mode,
638 DBUS_TYPE_INVALID)) {
639 log_error("Could not append arguments to message.");
644 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
646 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
647 /* There's always a fallback possible for
653 log_error("Failed to issue method call: %s", error.message);
662 if (!dbus_message_get_args(reply, &error,
663 DBUS_TYPE_OBJECT_PATH, &path,
664 DBUS_TYPE_INVALID)) {
665 log_error("Failed to parse reply: %s", error.message);
670 if (!(p = strdup(path))) {
671 log_error("Failed to duplicate path.");
676 if ((r = set_put(s, p)) < 0) {
678 log_error("Failed to add path to set.");
687 dbus_message_unref(m);
690 dbus_message_unref(reply);
692 dbus_error_free(&error);
697 static enum action verb_to_action(const char *verb) {
698 if (streq(verb, "halt"))
700 else if (streq(verb, "poweroff"))
701 return ACTION_POWEROFF;
702 else if (streq(verb, "reboot"))
703 return ACTION_REBOOT;
704 else if (streq(verb, "rescue"))
705 return ACTION_RESCUE;
706 else if (streq(verb, "emergency"))
707 return ACTION_EMERGENCY;
708 else if (streq(verb, "default"))
709 return ACTION_DEFAULT;
711 return ACTION_INVALID;
714 static int start_unit(DBusConnection *bus, char **args, unsigned n) {
716 static const char * const table[_ACTION_MAX] = {
717 [ACTION_HALT] = SPECIAL_HALT_TARGET,
718 [ACTION_POWEROFF] = SPECIAL_POWEROFF_TARGET,
719 [ACTION_REBOOT] = SPECIAL_REBOOT_TARGET,
720 [ACTION_RUNLEVEL2] = SPECIAL_RUNLEVEL2_TARGET,
721 [ACTION_RUNLEVEL3] = SPECIAL_RUNLEVEL3_TARGET,
722 [ACTION_RUNLEVEL4] = SPECIAL_RUNLEVEL4_TARGET,
723 [ACTION_RUNLEVEL5] = SPECIAL_RUNLEVEL5_TARGET,
724 [ACTION_RESCUE] = SPECIAL_RESCUE_TARGET,
725 [ACTION_EMERGENCY] = SPECIAL_EMERGENCY_SERVICE,
726 [ACTION_DEFAULT] = SPECIAL_DEFAULT_TARGET
731 const char *method, *mode, *one_name;
736 if (arg_action == ACTION_SYSTEMCTL) {
738 streq(args[0], "stop") ? "StopUnit" :
739 streq(args[0], "reload") ? "ReloadUnit" :
740 streq(args[0], "restart") ? "RestartUnit" :
744 (streq(args[0], "isolate") ||
745 streq(args[0], "rescue") ||
746 streq(args[0], "emergency")) ? "isolate" :
747 arg_replace ? "replace" :
750 one_name = table[verb_to_action(args[0])];
753 assert(arg_action < ELEMENTSOF(table));
754 assert(table[arg_action]);
756 method = "StartUnit";
758 mode = (arg_action == ACTION_EMERGENCY ||
759 arg_action == ACTION_RESCUE) ? "isolate" : "replace";
761 one_name = table[arg_action];
765 if ((r = enable_wait_for_jobs(bus)) < 0) {
766 log_error("Could not watch jobs: %s", strerror(-r));
770 if (!(s = set_new(string_hash_func, string_compare_func))) {
771 log_error("Failed to allocate set.");
780 if ((r = start_unit_one(bus, method, one_name, mode, s)) <= 0)
783 for (i = 1; i < n; i++)
784 if ((r = start_unit_one(bus, method, args[i], mode, s)) < 0)
789 r = wait_for_jobs(bus, s);
798 static int start_special(DBusConnection *bus, char **args, unsigned n) {
802 warn_wall(verb_to_action(args[0]));
804 return start_unit(bus, args, n);
807 static DBusHandlerResult monitor_filter(DBusConnection *connection, DBusMessage *message, void *data) {
809 DBusMessage *m = NULL, *reply = NULL;
814 dbus_error_init(&error);
816 /* log_debug("Got D-Bus request: %s.%s() on %s", */
817 /* dbus_message_get_interface(message), */
818 /* dbus_message_get_member(message), */
819 /* dbus_message_get_path(message)); */
821 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
822 log_error("Warning! D-Bus connection terminated.");
823 dbus_connection_close(connection);
825 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitNew") ||
826 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitRemoved")) {
827 const char *id, *path;
829 if (!dbus_message_get_args(message, &error,
830 DBUS_TYPE_STRING, &id,
831 DBUS_TYPE_OBJECT_PATH, &path,
833 log_error("Failed to parse message: %s", error.message);
834 else if (streq(dbus_message_get_member(message), "UnitNew"))
835 printf("Unit %s added.\n", id);
837 printf("Unit %s removed.\n", id);
839 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobNew") ||
840 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
844 if (!dbus_message_get_args(message, &error,
845 DBUS_TYPE_UINT32, &id,
846 DBUS_TYPE_OBJECT_PATH, &path,
848 log_error("Failed to parse message: %s", error.message);
849 else if (streq(dbus_message_get_member(message), "JobNew"))
850 printf("Job %u added.\n", id);
852 printf("Job %u removed.\n", id);
855 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Unit", "Changed") ||
856 dbus_message_is_signal(message, "org.freedesktop.systemd1.Job", "Changed")) {
858 const char *path, *interface, *property = "Id";
859 DBusMessageIter iter, sub;
861 path = dbus_message_get_path(message);
862 interface = dbus_message_get_interface(message);
864 if (!(m = dbus_message_new_method_call(
865 "org.freedesktop.systemd1",
867 "org.freedesktop.DBus.Properties",
869 log_error("Could not allocate message.");
873 if (!dbus_message_append_args(m,
874 DBUS_TYPE_STRING, &interface,
875 DBUS_TYPE_STRING, &property,
876 DBUS_TYPE_INVALID)) {
877 log_error("Could not append arguments to message.");
881 if (!(reply = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) {
882 log_error("Failed to issue method call: %s", error.message);
886 if (!dbus_message_iter_init(reply, &iter) ||
887 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
888 log_error("Failed to parse reply.");
892 dbus_message_iter_recurse(&iter, &sub);
894 if (streq(interface, "org.freedesktop.systemd1.Unit")) {
897 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
898 log_error("Failed to parse reply.");
902 dbus_message_iter_get_basic(&sub, &id);
903 printf("Unit %s changed.\n", id);
907 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT32) {
908 log_error("Failed to parse reply.");
912 dbus_message_iter_get_basic(&sub, &id);
913 printf("Job %u changed.\n", id);
919 dbus_message_unref(m);
922 dbus_message_unref(reply);
924 dbus_error_free(&error);
925 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
929 dbus_message_unref(m);
932 dbus_message_unref(reply);
934 dbus_error_free(&error);
935 return DBUS_HANDLER_RESULT_NEED_MEMORY;
938 static int monitor(DBusConnection *bus, char **args, unsigned n) {
939 DBusMessage *m = NULL, *reply = NULL;
943 dbus_error_init(&error);
945 dbus_bus_add_match(bus,
947 "sender='org.freedesktop.systemd1',"
948 "interface='org.freedesktop.systemd1.Manager',"
949 "path='/org/freedesktop/systemd1'",
952 if (dbus_error_is_set(&error)) {
953 log_error("Failed to add match: %s", error.message);
958 dbus_bus_add_match(bus,
960 "sender='org.freedesktop.systemd1',"
961 "interface='org.freedesktop.systemd1.Unit',"
965 if (dbus_error_is_set(&error)) {
966 log_error("Failed to add match: %s", error.message);
971 dbus_bus_add_match(bus,
973 "sender='org.freedesktop.systemd1',"
974 "interface='org.freedesktop.systemd1.Job',"
978 if (dbus_error_is_set(&error)) {
979 log_error("Failed to add match: %s", error.message);
984 if (!dbus_connection_add_filter(bus, monitor_filter, NULL, NULL)) {
985 log_error("Failed to add filter.");
990 if (!(m = dbus_message_new_method_call(
991 "org.freedesktop.systemd1",
992 "/org/freedesktop/systemd1",
993 "org.freedesktop.systemd1.Manager",
995 log_error("Could not allocate message.");
1000 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1001 log_error("Failed to issue method call: %s", error.message);
1006 while (dbus_connection_read_write_dispatch(bus, -1))
1013 /* This is slightly dirty, since we don't undo the filter or the matches. */
1016 dbus_message_unref(m);
1019 dbus_message_unref(reply);
1021 dbus_error_free(&error);
1026 static int dump(DBusConnection *bus, char **args, unsigned n) {
1027 DBusMessage *m = NULL, *reply = NULL;
1032 dbus_error_init(&error);
1034 if (!(m = dbus_message_new_method_call(
1035 "org.freedesktop.systemd1",
1036 "/org/freedesktop/systemd1",
1037 "org.freedesktop.systemd1.Manager",
1039 log_error("Could not allocate message.");
1043 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1044 log_error("Failed to issue method call: %s", error.message);
1049 if (!dbus_message_get_args(reply, &error,
1050 DBUS_TYPE_STRING, &text,
1051 DBUS_TYPE_INVALID)) {
1052 log_error("Failed to parse reply: %s", error.message);
1057 fputs(text, stdout);
1063 dbus_message_unref(m);
1066 dbus_message_unref(reply);
1068 dbus_error_free(&error);
1073 static int snapshot(DBusConnection *bus, char **args, unsigned n) {
1074 DBusMessage *m = NULL, *reply = NULL;
1077 const char *name = "", *path, *id;
1078 dbus_bool_t cleanup = FALSE;
1079 DBusMessageIter iter, sub;
1081 *interface = "org.freedesktop.systemd1.Unit",
1084 dbus_error_init(&error);
1086 if (!(m = dbus_message_new_method_call(
1087 "org.freedesktop.systemd1",
1088 "/org/freedesktop/systemd1",
1089 "org.freedesktop.systemd1.Manager",
1090 "CreateSnapshot"))) {
1091 log_error("Could not allocate message.");
1098 if (!dbus_message_append_args(m,
1099 DBUS_TYPE_STRING, &name,
1100 DBUS_TYPE_BOOLEAN, &cleanup,
1101 DBUS_TYPE_INVALID)) {
1102 log_error("Could not append arguments to message.");
1107 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1108 log_error("Failed to issue method call: %s", error.message);
1113 if (!dbus_message_get_args(reply, &error,
1114 DBUS_TYPE_OBJECT_PATH, &path,
1115 DBUS_TYPE_INVALID)) {
1116 log_error("Failed to parse reply: %s", error.message);
1121 dbus_message_unref(m);
1122 if (!(m = dbus_message_new_method_call(
1123 "org.freedesktop.systemd1",
1125 "org.freedesktop.DBus.Properties",
1127 log_error("Could not allocate message.");
1131 if (!dbus_message_append_args(m,
1132 DBUS_TYPE_STRING, &interface,
1133 DBUS_TYPE_STRING, &property,
1134 DBUS_TYPE_INVALID)) {
1135 log_error("Could not append arguments to message.");
1140 dbus_message_unref(reply);
1141 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1142 log_error("Failed to issue method call: %s", error.message);
1147 if (!dbus_message_iter_init(reply, &iter) ||
1148 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1149 log_error("Failed to parse reply.");
1154 dbus_message_iter_recurse(&iter, &sub);
1156 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1157 log_error("Failed to parse reply.");
1162 dbus_message_iter_get_basic(&sub, &id);
1168 dbus_message_unref(m);
1171 dbus_message_unref(reply);
1173 dbus_error_free(&error);
1178 static int clear_jobs(DBusConnection *bus, char **args, unsigned n) {
1179 DBusMessage *m = NULL, *reply = NULL;
1184 dbus_error_init(&error);
1186 if (arg_action == ACTION_RELOAD)
1188 else if (arg_action == ACTION_REEXEC)
1189 method = "Reexecute";
1191 assert(arg_action == ACTION_SYSTEMCTL);
1194 streq(args[0], "clear-jobs") ? "ClearJobs" :
1195 streq(args[0], "daemon-reload") ? "Reload" :
1196 streq(args[0], "daemon-reexec") ? "Reexecute" :
1200 if (!(m = dbus_message_new_method_call(
1201 "org.freedesktop.systemd1",
1202 "/org/freedesktop/systemd1",
1203 "org.freedesktop.systemd1.Manager",
1205 log_error("Could not allocate message.");
1209 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1211 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
1212 /* There's always a fallback possible for
1213 * legacy actions. */
1218 log_error("Failed to issue method call: %s", error.message);
1227 dbus_message_unref(m);
1230 dbus_message_unref(reply);
1232 dbus_error_free(&error);
1237 static int show_enviroment(DBusConnection *bus, char **args, unsigned n) {
1238 DBusMessage *m = NULL, *reply = NULL;
1240 DBusMessageIter iter, sub, sub2;
1243 *interface = "org.freedesktop.systemd1.Manager",
1244 *property = "Environment";
1246 dbus_error_init(&error);
1248 if (!(m = dbus_message_new_method_call(
1249 "org.freedesktop.systemd1",
1250 "/org/freedesktop/systemd1",
1251 "org.freedesktop.DBus.Properties",
1253 log_error("Could not allocate message.");
1257 if (!dbus_message_append_args(m,
1258 DBUS_TYPE_STRING, &interface,
1259 DBUS_TYPE_STRING, &property,
1260 DBUS_TYPE_INVALID)) {
1261 log_error("Could not append arguments to message.");
1266 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1267 log_error("Failed to issue method call: %s", error.message);
1272 if (!dbus_message_iter_init(reply, &iter) ||
1273 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1274 log_error("Failed to parse reply.");
1279 dbus_message_iter_recurse(&iter, &sub);
1281 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
1282 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
1283 log_error("Failed to parse reply.");
1288 dbus_message_iter_recurse(&sub, &sub2);
1290 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
1293 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
1294 log_error("Failed to parse reply.");
1299 dbus_message_iter_get_basic(&sub2, &text);
1300 printf("%s\n", text);
1302 dbus_message_iter_next(&sub2);
1309 dbus_message_unref(m);
1312 dbus_message_unref(reply);
1314 dbus_error_free(&error);
1319 static int set_environment(DBusConnection *bus, char **args, unsigned n) {
1320 DBusMessage *m = NULL, *reply = NULL;
1324 DBusMessageIter iter, sub;
1327 dbus_error_init(&error);
1329 method = streq(args[0], "set-environment")
1331 : "UnsetEnvironment";
1333 if (!(m = dbus_message_new_method_call(
1334 "org.freedesktop.systemd1",
1335 "/org/freedesktop/systemd1",
1336 "org.freedesktop.systemd1.Manager",
1339 log_error("Could not allocate message.");
1343 dbus_message_iter_init_append(m, &iter);
1345 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
1346 log_error("Could not append arguments to message.");
1351 for (i = 1; i < n; i++)
1352 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &args[i])) {
1353 log_error("Could not append arguments to message.");
1358 if (!dbus_message_iter_close_container(&iter, &sub)) {
1359 log_error("Could not append arguments to message.");
1364 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1365 log_error("Failed to issue method call: %s", error.message);
1374 dbus_message_unref(m);
1377 dbus_message_unref(reply);
1379 dbus_error_free(&error);
1384 static int systemctl_help(void) {
1386 printf("%s [options]\n\n"
1387 "Send control commands to the init daemon.\n\n"
1388 " -h --help Show this help\n"
1389 " -t --type=TYPE List only units of a particular type\n"
1390 " -a --all Show all units, including dead ones\n"
1391 " --replace When installing a new job, replace existing conflicting ones\n"
1392 " --system Connect to system bus\n"
1393 " --session Connect to session bus\n"
1394 " --block Wait until operation finished\n"
1395 " --no-wall Don't send wall message before reboot/halt/power-off\n\n"
1397 " list-units List units\n"
1398 " list-jobs List jobs\n"
1399 " clear-jobs Cancel all jobs\n"
1400 " load [NAME...] Load one or more units\n"
1401 " cancel [JOB...] Cancel one or more jobs\n"
1402 " start [NAME...] Start one or more units\n"
1403 " stop [NAME...] Stop one or more units\n"
1404 " restart [NAME...] Restart one or more units\n"
1405 " reload [NAME...] Reload one or more units\n"
1406 " isolate [NAME] Start one unit and stop all others\n"
1407 " monitor Monitor unit/job changes\n"
1408 " dump Dump server status\n"
1409 " snapshot [NAME] Create a snapshot\n"
1410 " daemon-reload Reload init daemon configuration\n"
1411 " daemon-reexecute Reexecute init daemon\n"
1412 " daemon-exit Ask the init daemon to quit\n"
1413 " show-environment Dump environment\n"
1414 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
1415 " unset-environment [NAME...] Unset one or more environment variables\n"
1416 " halt Shut down and halt the system\n"
1417 " reboot Shut down and reboot the system\n"
1418 " poweroff Shut down and power off the system\n"
1419 " default Enter default mode\n"
1420 " rescue Enter rescue mode\n"
1421 " emergency Enter emergency mode\n",
1422 program_invocation_short_name);
1427 static int halt_help(void) {
1429 printf("%s [options]\n\n"
1430 "%s the system.\n\n"
1431 " --help Show this help\n"
1432 " --halt Halt the machine\n"
1433 " -p --poweroff Switch off the machine\n"
1434 " --reboot Reboot the machine\n"
1435 " -f --force Force immediate reboot/halt/power-off\n"
1436 " -w --wtmp-only Don't reboot/halt/power-off, just write wtmp record\n"
1437 " -d --no-wtmp Don't write wtmp record\n"
1438 " -n --no-sync Don't sync before reboot/halt/power-off\n"
1439 " --no-wall Don't send wall message before reboot/halt/power-off\n",
1440 program_invocation_short_name,
1441 arg_action == ACTION_REBOOT ? "Reboot" :
1442 arg_action == ACTION_POWEROFF ? "Power off" :
1448 static int shutdown_help(void) {
1450 printf("%s [options] [IGNORED] [WALL...]\n\n"
1451 "Shut down the system.\n\n"
1452 " --help Show this help\n"
1453 " -H --halt Halt the machine\n"
1454 " -P --poweroff Power-off the machine\n"
1455 " -r --reboot Reboot the machine\n"
1456 " -h Equivalent to --poweroff, overriden by --halt\n"
1457 " -k Don't reboot/halt/power-off, just send warnings\n"
1458 " --no-wall Don't send wall message before reboot/halt/power-off\n",
1459 program_invocation_short_name);
1464 static int telinit_help(void) {
1466 printf("%s [options]\n\n"
1467 "Send control commands to the init daemon.\n\n"
1468 " --help Show this help\n"
1469 " --no-wall Don't send wall message before reboot/halt/power-off\n\n"
1471 " 0 Power-off the machine\n"
1472 " 6 Reboot the machine\n"
1473 " 2, 3, 4, 5 Start runlevelX.target unit\n"
1474 " 1, s, S Enter rescue mode\n"
1475 " q, Q Reload init daemon configuration\n"
1476 " u, U Reexecute init daemon\n",
1477 program_invocation_short_name);
1482 static int runlevel_help(void) {
1484 printf("%s [options]\n\n"
1485 "Prints the previous and current runlevel of the init system.\n\n"
1486 " --help Show this help\n",
1487 program_invocation_short_name);
1492 static int systemctl_parse_argv(int argc, char *argv[]) {
1495 ARG_REPLACE = 0x100,
1502 static const struct option options[] = {
1503 { "help", no_argument, NULL, 'h' },
1504 { "type", required_argument, NULL, 't' },
1505 { "all", no_argument, NULL, 'a' },
1506 { "replace", no_argument, NULL, ARG_REPLACE },
1507 { "session", no_argument, NULL, ARG_SESSION },
1508 { "system", no_argument, NULL, ARG_SYSTEM },
1509 { "block", no_argument, NULL, ARG_BLOCK },
1510 { "no-wall", no_argument, NULL, ARG_NO_WALL },
1511 { NULL, 0, NULL, 0 }
1519 while ((c = getopt_long(argc, argv, "hta", options, NULL)) >= 0) {
1544 arg_session = false;
1559 log_error("Unknown option code %c", c);
1567 static int halt_parse_argv(int argc, char *argv[]) {
1576 static const struct option options[] = {
1577 { "help", no_argument, NULL, ARG_HELP },
1578 { "halt", no_argument, NULL, ARG_HALT },
1579 { "poweroff", no_argument, NULL, 'p' },
1580 { "reboot", no_argument, NULL, ARG_REBOOT },
1581 { "force", no_argument, NULL, 'f' },
1582 { "wtmp-only", no_argument, NULL, 'w' },
1583 { "no-wtmp", no_argument, NULL, 'd' },
1584 { "no-sync", no_argument, NULL, 'n' },
1585 { "no-wall", no_argument, NULL, ARG_NO_WALL },
1586 { NULL, 0, NULL, 0 }
1594 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
1595 if (runlevel == '0' || runlevel == '6')
1596 arg_immediate = true;
1598 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
1606 arg_action = ACTION_HALT;
1610 arg_action = ACTION_POWEROFF;
1614 arg_action = ACTION_REBOOT;
1618 arg_immediate = true;
1639 /* Compatibility nops */
1646 log_error("Unknown option code %c", c);
1651 if (optind < argc) {
1652 log_error("Too many arguments.");
1659 static int shutdown_parse_argv(int argc, char *argv[]) {
1666 static const struct option options[] = {
1667 { "help", no_argument, NULL, ARG_HELP },
1668 { "halt", no_argument, NULL, 'H' },
1669 { "poweroff", no_argument, NULL, 'P' },
1670 { "reboot", no_argument, NULL, 'r' },
1671 { "no-wall", no_argument, NULL, ARG_NO_WALL },
1672 { NULL, 0, NULL, 0 }
1680 while ((c = getopt_long(argc, argv, "HPrhkt:a", options, NULL)) >= 0) {
1688 arg_action = ACTION_HALT;
1692 arg_action = ACTION_POWEROFF;
1696 arg_action = ACTION_REBOOT;
1700 if (arg_action != ACTION_HALT)
1701 arg_action = ACTION_POWEROFF;
1714 /* Compatibility nops */
1721 log_error("Unknown option code %c", c);
1726 /* We ignore the time argument */
1727 if (argc > optind + 1)
1728 arg_wall = argv + optind + 1;
1736 static int telinit_parse_argv(int argc, char *argv[]) {
1743 static const struct option options[] = {
1744 { "help", no_argument, NULL, ARG_HELP },
1745 { "no-wall", no_argument, NULL, ARG_NO_WALL },
1746 { NULL, 0, NULL, 0 }
1749 static const struct {
1753 { '0', ACTION_POWEROFF },
1754 { '6', ACTION_REBOOT },
1755 { '1', ACTION_RESCUE },
1756 { '2', ACTION_RUNLEVEL2 },
1757 { '3', ACTION_RUNLEVEL3 },
1758 { '4', ACTION_RUNLEVEL4 },
1759 { '5', ACTION_RUNLEVEL5 },
1760 { 's', ACTION_RESCUE },
1761 { 'S', ACTION_RESCUE },
1762 { 'q', ACTION_RELOAD },
1763 { 'Q', ACTION_RELOAD },
1764 { 'u', ACTION_REEXEC },
1765 { 'U', ACTION_REEXEC }
1774 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
1789 log_error("Unknown option code %c", c);
1794 if (optind >= argc) {
1795 log_error("Argument missing.");
1799 if (optind + 1 < argc) {
1800 log_error("Too many arguments.");
1804 if (strlen(argv[optind]) != 1) {
1805 log_error("Expected single character argument.");
1809 for (i = 0; i < ELEMENTSOF(table); i++)
1810 if (table[i].from == argv[optind][0])
1813 if (i >= ELEMENTSOF(table)) {
1814 log_error("Unknown command %s.", argv[optind]);
1818 arg_action = table[i].to;
1825 static int runlevel_parse_argv(int argc, char *argv[]) {
1831 static const struct option options[] = {
1832 { "help", no_argument, NULL, ARG_HELP },
1833 { NULL, 0, NULL, 0 }
1841 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
1852 log_error("Unknown option code %c", c);
1857 if (optind < argc) {
1858 log_error("Too many arguments.");
1865 static int parse_argv(int argc, char *argv[]) {
1869 if (program_invocation_short_name) {
1871 if (strstr(program_invocation_short_name, "halt")) {
1872 arg_action = ACTION_HALT;
1873 return halt_parse_argv(argc, argv);
1874 } else if (strstr(program_invocation_short_name, "poweroff")) {
1875 arg_action = ACTION_POWEROFF;
1876 return halt_parse_argv(argc, argv);
1877 } else if (strstr(program_invocation_short_name, "reboot")) {
1878 arg_action = ACTION_REBOOT;
1879 return halt_parse_argv(argc, argv);
1880 } else if (strstr(program_invocation_short_name, "shutdown")) {
1881 arg_action = ACTION_POWEROFF;
1882 return shutdown_parse_argv(argc, argv);
1883 } else if (strstr(program_invocation_short_name, "init")) {
1884 arg_action = ACTION_INVALID;
1885 return telinit_parse_argv(argc, argv);
1886 } else if (strstr(program_invocation_short_name, "runlevel")) {
1887 arg_action = ACTION_RUNLEVEL;
1888 return runlevel_parse_argv(argc, argv);
1892 arg_action = ACTION_SYSTEMCTL;
1893 return systemctl_parse_argv(argc, argv);
1896 static int action_to_runlevel(void) {
1898 static const char table[_ACTION_MAX] = {
1899 [ACTION_HALT] = '0',
1900 [ACTION_POWEROFF] = '0',
1901 [ACTION_REBOOT] = '6',
1902 [ACTION_RUNLEVEL2] = '2',
1903 [ACTION_RUNLEVEL3] = '3',
1904 [ACTION_RUNLEVEL4] = '4',
1905 [ACTION_RUNLEVEL5] = '5',
1906 [ACTION_RESCUE] = '1'
1909 assert(arg_action < _ACTION_MAX);
1911 return table[arg_action];
1914 static int talk_upstart(DBusConnection *bus) {
1915 DBusMessage *m = NULL, *reply = NULL;
1917 int previous, rl, r;
1919 env1_buf[] = "RUNLEVEL=X",
1920 env2_buf[] = "PREVLEVEL=X";
1921 char *env1 = env1_buf, *env2 = env2_buf;
1922 const char *emit = "runlevel";
1923 dbus_bool_t b_false = FALSE;
1924 DBusMessageIter iter, sub;
1926 dbus_error_init(&error);
1928 if (!(rl = action_to_runlevel()))
1931 if (utmp_get_runlevel(&previous, NULL) < 0)
1934 if (!(m = dbus_message_new_method_call(
1935 "com.ubuntu.Upstart",
1936 "/com/ubuntu/Upstart",
1937 "com.ubuntu.Upstart0_6",
1940 log_error("Could not allocate message.");
1944 dbus_message_iter_init_append(m, &iter);
1946 env1_buf[sizeof(env1_buf)-2] = rl;
1947 env2_buf[sizeof(env2_buf)-2] = previous;
1949 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
1950 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
1951 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
1952 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
1953 !dbus_message_iter_close_container(&iter, &sub) ||
1954 !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
1955 log_error("Could not append arguments to message.");
1960 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1962 if (error_is_no_service(&error)) {
1967 log_error("Failed to issue method call: %s", error.message);
1976 dbus_message_unref(m);
1979 dbus_message_unref(reply);
1981 dbus_error_free(&error);
1986 static int talk_initctl(void) {
1987 struct init_request request;
1991 if (!(rl = action_to_runlevel()))
1995 request.magic = INIT_MAGIC;
1996 request.sleeptime = 0;
1997 request.cmd = INIT_CMD_RUNLVL;
1998 request.runlevel = rl;
2000 if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
2002 if (errno == ENOENT)
2005 log_error("Failed to open "INIT_FIFO": %m");
2010 r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
2011 close_nointr_nofail(fd);
2014 log_error("Failed to write to "INIT_FIFO": %m");
2015 return errno ? -errno : -EIO;
2021 static int systemctl_main(DBusConnection *bus, int argc, char *argv[]) {
2023 static const struct {
2031 int (* const dispatch)(DBusConnection *bus, char **args, unsigned n);
2033 { "list-units", LESS, 1, list_units },
2034 { "list-jobs", EQUAL, 1, list_jobs },
2035 { "clear-jobs", EQUAL, 1, clear_jobs },
2036 { "load", MORE, 2, load_unit },
2037 { "cancel", MORE, 2, cancel_job },
2038 { "start", MORE, 2, start_unit },
2039 { "stop", MORE, 2, start_unit },
2040 { "reload", MORE, 2, start_unit },
2041 { "restart", MORE, 2, start_unit },
2042 { "isolate", EQUAL, 2, start_unit },
2043 { "monitor", EQUAL, 1, monitor },
2044 { "dump", EQUAL, 1, dump },
2045 { "snapshot", LESS, 2, snapshot },
2046 { "daemon-reload", EQUAL, 1, clear_jobs },
2047 { "daemon-reexec", EQUAL, 1, clear_jobs },
2048 { "daemon-exit", EQUAL, 1, clear_jobs },
2049 { "show-environment", EQUAL, 1, show_enviroment },
2050 { "set-environment", MORE, 2, set_environment },
2051 { "unset-environment", MORE, 2, set_environment },
2052 { "halt", EQUAL, 1, start_special },
2053 { "poweroff", EQUAL, 1, start_special },
2054 { "reboot", EQUAL, 1, start_special },
2055 { "default", EQUAL, 1, start_special },
2056 { "rescue", EQUAL, 1, start_special },
2057 { "emergency", EQUAL, 1, start_special },
2067 left = argc - optind;
2070 /* Special rule: no arguments means "list-units" */
2073 for (i = 0; i < ELEMENTSOF(verbs); i++)
2074 if (streq(argv[optind], verbs[i].verb))
2077 if (i >= ELEMENTSOF(verbs)) {
2078 log_error("Unknown operation %s", argv[optind]);
2083 switch (verbs[i].argc_cmp) {
2086 if (left != verbs[i].argc) {
2087 log_error("Invalid number of arguments.");
2094 if (left < verbs[i].argc) {
2095 log_error("Too few arguments.");
2102 if (left > verbs[i].argc) {
2103 log_error("Too many arguments.");
2110 assert_not_reached("Unknown comparison operator.");
2113 return verbs[i].dispatch(bus, argv + optind, left);
2116 static int reload_with_fallback(DBusConnection *bus) {
2120 /* First, try systemd via D-Bus. */
2121 if ((r = clear_jobs(bus, NULL, 0)) > 0)
2125 /* Nothing else worked, so let's try signals */
2126 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
2128 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
2129 log_error("kill() failed: %m");
2136 static int start_with_fallback(DBusConnection *bus) {
2139 warn_wall(arg_action);
2142 /* First, try systemd via D-Bus. */
2143 if ((r = start_unit(bus, NULL, 0)) > 0)
2146 /* Hmm, talking to systemd via D-Bus didn't work. Then
2147 * let's try to talk to Upstart via D-Bus. */
2148 if ((r = talk_upstart(bus)) > 0)
2152 /* Nothing else worked, so let's try
2154 if ((r = talk_initctl()) != 0)
2157 log_error("Failed to talk to init daemon.");
2161 static int halt_main(DBusConnection *bus) {
2165 return start_with_fallback(bus);
2168 if ((r = utmp_put_shutdown(0)) < 0)
2169 log_warning("Failed to write utmp record: %s", strerror(-r));
2177 /* Make sure C-A-D is handled by the kernel from this
2179 reboot(RB_ENABLE_CAD);
2181 switch (arg_action) {
2184 log_info("Halting");
2185 reboot(RB_HALT_SYSTEM);
2188 case ACTION_POWEROFF:
2189 log_info("Powering off");
2190 reboot(RB_POWER_OFF);
2194 log_info("Rebooting");
2195 reboot(RB_AUTOBOOT);
2199 assert_not_reached("Unknown halt action.");
2202 /* We should never reach this. */
2206 static int runlevel_main(void) {
2207 int r, runlevel, previous;
2209 if ((r = utmp_get_runlevel(&runlevel, &previous)) < 0) {
2215 previous <= 0 ? 'N' : previous,
2216 runlevel <= 0 ? 'N' : runlevel);
2221 int main(int argc, char*argv[]) {
2223 DBusConnection *bus = NULL;
2226 dbus_error_init(&error);
2228 log_parse_environment();
2230 if ((r = parse_argv(argc, argv)) < 0)
2237 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
2238 * let's shortcut this */
2239 if (arg_action == ACTION_RUNLEVEL) {
2240 retval = runlevel_main() < 0;
2244 if ((bus = dbus_bus_get(arg_session ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &error)))
2245 dbus_connection_set_exit_on_disconnect(bus, FALSE);
2247 switch (arg_action) {
2249 case ACTION_SYSTEMCTL: {
2252 log_error("Failed to get D-Bus connection: %s", error.message);
2256 retval = systemctl_main(bus, argc, argv) < 0;
2261 case ACTION_POWEROFF:
2263 retval = halt_main(bus) < 0;
2266 case ACTION_RUNLEVEL2:
2267 case ACTION_RUNLEVEL3:
2268 case ACTION_RUNLEVEL4:
2269 case ACTION_RUNLEVEL5:
2271 case ACTION_EMERGENCY:
2272 case ACTION_DEFAULT:
2273 retval = start_with_fallback(bus) < 0;
2278 retval = reload_with_fallback(bus) < 0;
2281 case ACTION_INVALID:
2282 case ACTION_RUNLEVEL:
2284 assert_not_reached("Unknown action");
2290 dbus_connection_unref(bus);
2292 dbus_error_free(&error);