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"
45 static const char *arg_type = NULL;
46 static bool arg_all = false;
47 static bool arg_replace = false;
48 static bool arg_session = false;
49 static bool arg_block = false;
50 static bool arg_immediate = false;
51 static bool arg_no_wtmp = false;
52 static bool arg_no_sync = false;
53 static bool arg_no_wall = false;
54 static bool arg_dry = false;
55 static char **arg_wall = NULL;
73 } arg_action = ACTION_SYSTEMCTL;
75 static bool error_is_no_service(DBusError *error) {
79 if (!dbus_error_is_set(error))
82 if (dbus_error_has_name(error, DBUS_ERROR_NAME_HAS_NO_OWNER))
85 if (dbus_error_has_name(error, DBUS_ERROR_SERVICE_UNKNOWN))
88 return startswith(error->name, "org.freedesktop.DBus.Error.Spawn.");
91 static int bus_iter_get_basic_and_next(DBusMessageIter *iter, int type, void *data, bool next) {
96 if (dbus_message_iter_get_arg_type(iter) != type)
99 dbus_message_iter_get_basic(iter, data);
101 if (!dbus_message_iter_next(iter) != !next)
107 static int bus_check_peercred(DBusConnection *c) {
114 assert_se(dbus_connection_get_unix_fd(c, &fd));
116 l = sizeof(struct ucred);
117 if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &ucred, &l) < 0) {
118 log_error("SO_PEERCRED failed: %m");
122 if (l != sizeof(struct ucred)) {
123 log_error("SO_PEERCRED returned wrong size.");
133 static int columns(void) {
134 static int parsed_columns = 0;
137 if (parsed_columns > 0)
138 return parsed_columns;
140 if ((e = getenv("COLUMNS")))
141 parsed_columns = atoi(e);
143 if (parsed_columns <= 0) {
147 if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) >= 0)
148 parsed_columns = ws.ws_col;
151 if (parsed_columns <= 0)
154 return parsed_columns;
158 static void warn_wall(enum action action) {
159 static const char *table[_ACTION_MAX] = {
160 [ACTION_HALT] = "The system is going down for system halt NOW!",
161 [ACTION_REBOOT] = "The system is going down for reboot NOW!",
162 [ACTION_POWEROFF] = "The system is going down for power-off NOW!",
163 [ACTION_RESCUE] = "The system is going down to rescue mode NOW!",
164 [ACTION_EMERGENCY] = "The system is going down to emergency mode NOW!"
173 if (!(p = strv_join(arg_wall, " "))) {
174 log_error("Failed to join strings.");
190 utmp_wall(table[action]);
193 static int list_units(DBusConnection *bus, char **args, unsigned n) {
194 DBusMessage *m = NULL, *reply = NULL;
197 DBusMessageIter iter, sub, sub2;
200 dbus_error_init(&error);
204 if (!(m = dbus_message_new_method_call(
205 "org.freedesktop.systemd1",
206 "/org/freedesktop/systemd1",
207 "org.freedesktop.systemd1.Manager",
209 log_error("Could not allocate message.");
213 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
214 log_error("Failed to issue method call: %s", error.message);
219 if (!dbus_message_iter_init(reply, &iter) ||
220 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
221 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
222 log_error("Failed to parse reply.");
227 dbus_message_iter_recurse(&iter, &sub);
229 printf("%-45s %-6s %-12s %-12s %-15s %s\n", "UNIT", "LOAD", "ACTIVE", "SUB", "JOB", "DESCRIPTION");
231 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
232 const char *id, *description, *load_state, *active_state, *sub_state, *unit_state, *job_type, *job_path, *dot;
235 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
236 log_error("Failed to parse reply.");
241 dbus_message_iter_recurse(&sub, &sub2);
243 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
244 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &description, true) < 0 ||
245 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &load_state, true) < 0 ||
246 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &active_state, true) < 0 ||
247 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &sub_state, true) < 0 ||
248 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_state, true) < 0 ||
249 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &job_id, true) < 0 ||
250 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &job_type, true) < 0 ||
251 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, false) < 0) {
252 log_error("Failed to parse reply.");
257 if ((!arg_type || ((dot = strrchr(id, '.')) &&
258 streq(dot+1, arg_type))) &&
259 (arg_all || !streq(active_state, "inactive"))) {
263 printf("%-45s %-6s %-12s %-12s%n", id, load_state, active_state, sub_state, &a);
266 printf(" %-15s%n", job_type, &b);
270 if (a + b + 2 < columns()) {
274 printf("%.*s", columns() - a - b - 2, description);
281 dbus_message_iter_next(&sub);
285 printf("\n%u units listed.\n", k);
287 printf("\n%u live units listed. Pass --all to see dead units, too.\n", k);
293 dbus_message_unref(m);
296 dbus_message_unref(reply);
298 dbus_error_free(&error);
303 static int list_jobs(DBusConnection *bus, char **args, unsigned n) {
304 DBusMessage *m = NULL, *reply = NULL;
307 DBusMessageIter iter, sub, sub2;
310 dbus_error_init(&error);
314 if (!(m = dbus_message_new_method_call(
315 "org.freedesktop.systemd1",
316 "/org/freedesktop/systemd1",
317 "org.freedesktop.systemd1.Manager",
319 log_error("Could not allocate message.");
323 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
324 log_error("Failed to issue method call: %s", error.message);
329 if (!dbus_message_iter_init(reply, &iter) ||
330 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
331 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
332 log_error("Failed to parse reply.");
337 dbus_message_iter_recurse(&iter, &sub);
339 printf("%4s %-45s %-17s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
341 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
342 const char *name, *type, *state, *job_path, *unit_path;
345 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
346 log_error("Failed to parse reply.");
351 dbus_message_iter_recurse(&sub, &sub2);
353 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &id, true) < 0 ||
354 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0 ||
355 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
356 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, true) < 0 ||
357 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, true) < 0 ||
358 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, false) < 0) {
359 log_error("Failed to parse reply.");
364 printf("%4u %-45s %-17s %-7s\n", id, name, type, state);
367 dbus_message_iter_next(&sub);
370 printf("\n%u jobs listed.\n", k);
375 dbus_message_unref(m);
378 dbus_message_unref(reply);
380 dbus_error_free(&error);
385 static int load_unit(DBusConnection *bus, char **args, unsigned n) {
386 DBusMessage *m = NULL, *reply = NULL;
391 dbus_error_init(&error);
396 for (i = 1; i < n; i++) {
398 if (!(m = dbus_message_new_method_call(
399 "org.freedesktop.systemd1",
400 "/org/freedesktop/systemd1",
401 "org.freedesktop.systemd1.Manager",
403 log_error("Could not allocate message.");
408 if (!dbus_message_append_args(m,
409 DBUS_TYPE_STRING, &args[i],
410 DBUS_TYPE_INVALID)) {
411 log_error("Could not append arguments to message.");
416 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
417 log_error("Failed to issue method call: %s", error.message);
422 dbus_message_unref(m);
423 dbus_message_unref(reply);
432 dbus_message_unref(m);
435 dbus_message_unref(reply);
437 dbus_error_free(&error);
442 static int cancel_job(DBusConnection *bus, char **args, unsigned n) {
443 DBusMessage *m = NULL, *reply = NULL;
448 dbus_error_init(&error);
453 for (i = 1; i < n; i++) {
457 if (!(m = dbus_message_new_method_call(
458 "org.freedesktop.systemd1",
459 "/org/freedesktop/systemd1",
460 "org.freedesktop.systemd1.Manager",
462 log_error("Could not allocate message.");
467 if ((r = safe_atou(args[i], &id)) < 0) {
468 log_error("Failed to parse job id: %s", strerror(-r));
472 assert_cc(sizeof(uint32_t) == sizeof(id));
473 if (!dbus_message_append_args(m,
474 DBUS_TYPE_UINT32, &id,
475 DBUS_TYPE_INVALID)) {
476 log_error("Could not append arguments to message.");
481 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
482 log_error("Failed to issue method call: %s", error.message);
487 if (!dbus_message_get_args(reply, &error,
488 DBUS_TYPE_OBJECT_PATH, &path,
489 DBUS_TYPE_INVALID)) {
490 log_error("Failed to parse reply: %s", error.message);
495 dbus_message_unref(m);
496 if (!(m = dbus_message_new_method_call(
497 "org.freedesktop.systemd1",
499 "org.freedesktop.systemd1.Job",
501 log_error("Could not allocate message.");
506 dbus_message_unref(reply);
507 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
508 log_error("Failed to issue method call: %s", error.message);
513 dbus_message_unref(m);
514 dbus_message_unref(reply);
522 dbus_message_unref(m);
525 dbus_message_unref(reply);
527 dbus_error_free(&error);
532 static DBusHandlerResult wait_filter(DBusConnection *connection, DBusMessage *message, void *data) {
540 dbus_error_init(&error);
542 /* log_debug("Got D-Bus request: %s.%s() on %s", */
543 /* dbus_message_get_interface(message), */
544 /* dbus_message_get_member(message), */
545 /* dbus_message_get_path(message)); */
547 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
548 log_error("Warning! D-Bus connection terminated.");
549 dbus_connection_close(connection);
551 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
555 if (!dbus_message_get_args(message, &error,
556 DBUS_TYPE_UINT32, &id,
557 DBUS_TYPE_OBJECT_PATH, &path,
559 log_error("Failed to parse message: %s", error.message);
563 if ((p = set_remove(s, (char*) path)))
568 dbus_error_free(&error);
569 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
572 static int enable_wait_for_jobs(DBusConnection *bus) {
574 DBusMessage *m = NULL, *reply = NULL;
579 dbus_error_init(&error);
581 dbus_bus_add_match(bus,
583 "sender='org.freedesktop.systemd1',"
584 "interface='org.freedesktop.systemd1.Manager',"
585 "member='JobRemoved',"
586 "path='/org/freedesktop/systemd1'",
589 if (dbus_error_is_set(&error)) {
590 log_error("Failed to add match: %s", error.message);
595 if (!(m = dbus_message_new_method_call(
596 "org.freedesktop.systemd1",
597 "/org/freedesktop/systemd1",
598 "org.freedesktop.systemd1.Manager",
600 log_error("Could not allocate message.");
605 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
606 log_error("Failed to issue method call: %s", error.message);
614 /* This is slightly dirty, since we don't undo the match registrations. */
617 dbus_message_unref(m);
620 dbus_message_unref(reply);
622 dbus_error_free(&error);
627 static int wait_for_jobs(DBusConnection *bus, Set *s) {
633 if (!dbus_connection_add_filter(bus, wait_filter, s, NULL)) {
634 log_error("Failed to add filter.");
639 while (!set_isempty(s) &&
640 dbus_connection_read_write_dispatch(bus, -1))
646 /* This is slightly dirty, since we don't undo the filter registration. */
651 static int start_unit_one(
658 DBusMessage *m = NULL, *reply = NULL;
666 assert(!arg_block || s);
668 dbus_error_init(&error);
670 if (!(m = dbus_message_new_method_call(
671 "org.freedesktop.systemd1",
672 "/org/freedesktop/systemd1",
673 "org.freedesktop.systemd1.Manager",
675 log_error("Could not allocate message.");
680 if (!dbus_message_append_args(m,
681 DBUS_TYPE_STRING, &name,
682 DBUS_TYPE_STRING, &mode,
683 DBUS_TYPE_INVALID)) {
684 log_error("Could not append arguments to message.");
689 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
691 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
692 /* There's always a fallback possible for
698 log_error("Failed to issue method call: %s", error.message);
707 if (!dbus_message_get_args(reply, &error,
708 DBUS_TYPE_OBJECT_PATH, &path,
709 DBUS_TYPE_INVALID)) {
710 log_error("Failed to parse reply: %s", error.message);
715 if (!(p = strdup(path))) {
716 log_error("Failed to duplicate path.");
721 if ((r = set_put(s, p)) < 0) {
723 log_error("Failed to add path to set.");
732 dbus_message_unref(m);
735 dbus_message_unref(reply);
737 dbus_error_free(&error);
742 static enum action verb_to_action(const char *verb) {
743 if (streq(verb, "halt"))
745 else if (streq(verb, "poweroff"))
746 return ACTION_POWEROFF;
747 else if (streq(verb, "reboot"))
748 return ACTION_REBOOT;
749 else if (streq(verb, "rescue"))
750 return ACTION_RESCUE;
751 else if (streq(verb, "emergency"))
752 return ACTION_EMERGENCY;
753 else if (streq(verb, "default"))
754 return ACTION_DEFAULT;
756 return ACTION_INVALID;
759 static int start_unit(DBusConnection *bus, char **args, unsigned n) {
761 static const char * const table[_ACTION_MAX] = {
762 [ACTION_HALT] = SPECIAL_HALT_TARGET,
763 [ACTION_POWEROFF] = SPECIAL_POWEROFF_TARGET,
764 [ACTION_REBOOT] = SPECIAL_REBOOT_TARGET,
765 [ACTION_RUNLEVEL2] = SPECIAL_RUNLEVEL2_TARGET,
766 [ACTION_RUNLEVEL3] = SPECIAL_RUNLEVEL3_TARGET,
767 [ACTION_RUNLEVEL4] = SPECIAL_RUNLEVEL4_TARGET,
768 [ACTION_RUNLEVEL5] = SPECIAL_RUNLEVEL5_TARGET,
769 [ACTION_RESCUE] = SPECIAL_RESCUE_TARGET,
770 [ACTION_EMERGENCY] = SPECIAL_EMERGENCY_SERVICE,
771 [ACTION_DEFAULT] = SPECIAL_DEFAULT_TARGET
776 const char *method, *mode, *one_name;
781 if (arg_action == ACTION_SYSTEMCTL) {
783 streq(args[0], "stop") ? "StopUnit" :
784 streq(args[0], "reload") ? "ReloadUnit" :
785 streq(args[0], "restart") ? "RestartUnit" :
789 (streq(args[0], "isolate") ||
790 streq(args[0], "rescue") ||
791 streq(args[0], "emergency")) ? "isolate" :
792 arg_replace ? "replace" :
795 one_name = table[verb_to_action(args[0])];
798 assert(arg_action < ELEMENTSOF(table));
799 assert(table[arg_action]);
801 method = "StartUnit";
803 mode = (arg_action == ACTION_EMERGENCY ||
804 arg_action == ACTION_RESCUE) ? "isolate" : "replace";
806 one_name = table[arg_action];
810 if ((r = enable_wait_for_jobs(bus)) < 0) {
811 log_error("Could not watch jobs: %s", strerror(-r));
815 if (!(s = set_new(string_hash_func, string_compare_func))) {
816 log_error("Failed to allocate set.");
825 if ((r = start_unit_one(bus, method, one_name, mode, s)) <= 0)
828 for (i = 1; i < n; i++)
829 if ((r = start_unit_one(bus, method, args[i], mode, s)) < 0)
834 r = wait_for_jobs(bus, s);
843 static int start_special(DBusConnection *bus, char **args, unsigned n) {
847 warn_wall(verb_to_action(args[0]));
849 return start_unit(bus, args, n);
852 static DBusHandlerResult monitor_filter(DBusConnection *connection, DBusMessage *message, void *data) {
854 DBusMessage *m = NULL, *reply = NULL;
859 dbus_error_init(&error);
861 /* log_debug("Got D-Bus request: %s.%s() on %s", */
862 /* dbus_message_get_interface(message), */
863 /* dbus_message_get_member(message), */
864 /* dbus_message_get_path(message)); */
866 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
867 log_error("Warning! D-Bus connection terminated.");
868 dbus_connection_close(connection);
870 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitNew") ||
871 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitRemoved")) {
872 const char *id, *path;
874 if (!dbus_message_get_args(message, &error,
875 DBUS_TYPE_STRING, &id,
876 DBUS_TYPE_OBJECT_PATH, &path,
878 log_error("Failed to parse message: %s", error.message);
879 else if (streq(dbus_message_get_member(message), "UnitNew"))
880 printf("Unit %s added.\n", id);
882 printf("Unit %s removed.\n", id);
884 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobNew") ||
885 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
889 if (!dbus_message_get_args(message, &error,
890 DBUS_TYPE_UINT32, &id,
891 DBUS_TYPE_OBJECT_PATH, &path,
893 log_error("Failed to parse message: %s", error.message);
894 else if (streq(dbus_message_get_member(message), "JobNew"))
895 printf("Job %u added.\n", id);
897 printf("Job %u removed.\n", id);
900 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Unit", "Changed") ||
901 dbus_message_is_signal(message, "org.freedesktop.systemd1.Job", "Changed")) {
903 const char *path, *interface, *property = "Id";
904 DBusMessageIter iter, sub;
906 path = dbus_message_get_path(message);
907 interface = dbus_message_get_interface(message);
909 if (!(m = dbus_message_new_method_call(
910 "org.freedesktop.systemd1",
912 "org.freedesktop.DBus.Properties",
914 log_error("Could not allocate message.");
918 if (!dbus_message_append_args(m,
919 DBUS_TYPE_STRING, &interface,
920 DBUS_TYPE_STRING, &property,
921 DBUS_TYPE_INVALID)) {
922 log_error("Could not append arguments to message.");
926 if (!(reply = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) {
927 log_error("Failed to issue method call: %s", error.message);
931 if (!dbus_message_iter_init(reply, &iter) ||
932 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
933 log_error("Failed to parse reply.");
937 dbus_message_iter_recurse(&iter, &sub);
939 if (streq(interface, "org.freedesktop.systemd1.Unit")) {
942 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
943 log_error("Failed to parse reply.");
947 dbus_message_iter_get_basic(&sub, &id);
948 printf("Unit %s changed.\n", id);
952 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT32) {
953 log_error("Failed to parse reply.");
957 dbus_message_iter_get_basic(&sub, &id);
958 printf("Job %u changed.\n", id);
964 dbus_message_unref(m);
967 dbus_message_unref(reply);
969 dbus_error_free(&error);
970 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
974 dbus_message_unref(m);
977 dbus_message_unref(reply);
979 dbus_error_free(&error);
980 return DBUS_HANDLER_RESULT_NEED_MEMORY;
983 static int monitor(DBusConnection *bus, char **args, unsigned n) {
984 DBusMessage *m = NULL, *reply = NULL;
988 dbus_error_init(&error);
990 dbus_bus_add_match(bus,
992 "sender='org.freedesktop.systemd1',"
993 "interface='org.freedesktop.systemd1.Manager',"
994 "path='/org/freedesktop/systemd1'",
997 if (dbus_error_is_set(&error)) {
998 log_error("Failed to add match: %s", error.message);
1003 dbus_bus_add_match(bus,
1005 "sender='org.freedesktop.systemd1',"
1006 "interface='org.freedesktop.systemd1.Unit',"
1010 if (dbus_error_is_set(&error)) {
1011 log_error("Failed to add match: %s", error.message);
1016 dbus_bus_add_match(bus,
1018 "sender='org.freedesktop.systemd1',"
1019 "interface='org.freedesktop.systemd1.Job',"
1023 if (dbus_error_is_set(&error)) {
1024 log_error("Failed to add match: %s", error.message);
1029 if (!dbus_connection_add_filter(bus, monitor_filter, NULL, NULL)) {
1030 log_error("Failed to add filter.");
1035 if (!(m = dbus_message_new_method_call(
1036 "org.freedesktop.systemd1",
1037 "/org/freedesktop/systemd1",
1038 "org.freedesktop.systemd1.Manager",
1040 log_error("Could not allocate message.");
1045 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1046 log_error("Failed to issue method call: %s", error.message);
1051 while (dbus_connection_read_write_dispatch(bus, -1))
1058 /* This is slightly dirty, since we don't undo the filter or the matches. */
1061 dbus_message_unref(m);
1064 dbus_message_unref(reply);
1066 dbus_error_free(&error);
1071 static int dump(DBusConnection *bus, char **args, unsigned n) {
1072 DBusMessage *m = NULL, *reply = NULL;
1077 dbus_error_init(&error);
1079 if (!(m = dbus_message_new_method_call(
1080 "org.freedesktop.systemd1",
1081 "/org/freedesktop/systemd1",
1082 "org.freedesktop.systemd1.Manager",
1084 log_error("Could not allocate message.");
1088 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1089 log_error("Failed to issue method call: %s", error.message);
1094 if (!dbus_message_get_args(reply, &error,
1095 DBUS_TYPE_STRING, &text,
1096 DBUS_TYPE_INVALID)) {
1097 log_error("Failed to parse reply: %s", error.message);
1102 fputs(text, stdout);
1108 dbus_message_unref(m);
1111 dbus_message_unref(reply);
1113 dbus_error_free(&error);
1118 static int snapshot(DBusConnection *bus, char **args, unsigned n) {
1119 DBusMessage *m = NULL, *reply = NULL;
1122 const char *name = "", *path, *id;
1123 dbus_bool_t cleanup = FALSE;
1124 DBusMessageIter iter, sub;
1126 *interface = "org.freedesktop.systemd1.Unit",
1129 dbus_error_init(&error);
1131 if (!(m = dbus_message_new_method_call(
1132 "org.freedesktop.systemd1",
1133 "/org/freedesktop/systemd1",
1134 "org.freedesktop.systemd1.Manager",
1135 "CreateSnapshot"))) {
1136 log_error("Could not allocate message.");
1143 if (!dbus_message_append_args(m,
1144 DBUS_TYPE_STRING, &name,
1145 DBUS_TYPE_BOOLEAN, &cleanup,
1146 DBUS_TYPE_INVALID)) {
1147 log_error("Could not append arguments to message.");
1152 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1153 log_error("Failed to issue method call: %s", error.message);
1158 if (!dbus_message_get_args(reply, &error,
1159 DBUS_TYPE_OBJECT_PATH, &path,
1160 DBUS_TYPE_INVALID)) {
1161 log_error("Failed to parse reply: %s", error.message);
1166 dbus_message_unref(m);
1167 if (!(m = dbus_message_new_method_call(
1168 "org.freedesktop.systemd1",
1170 "org.freedesktop.DBus.Properties",
1172 log_error("Could not allocate message.");
1176 if (!dbus_message_append_args(m,
1177 DBUS_TYPE_STRING, &interface,
1178 DBUS_TYPE_STRING, &property,
1179 DBUS_TYPE_INVALID)) {
1180 log_error("Could not append arguments to message.");
1185 dbus_message_unref(reply);
1186 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1187 log_error("Failed to issue method call: %s", error.message);
1192 if (!dbus_message_iter_init(reply, &iter) ||
1193 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1194 log_error("Failed to parse reply.");
1199 dbus_message_iter_recurse(&iter, &sub);
1201 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1202 log_error("Failed to parse reply.");
1207 dbus_message_iter_get_basic(&sub, &id);
1213 dbus_message_unref(m);
1216 dbus_message_unref(reply);
1218 dbus_error_free(&error);
1223 static int clear_jobs(DBusConnection *bus, char **args, unsigned n) {
1224 DBusMessage *m = NULL, *reply = NULL;
1229 dbus_error_init(&error);
1231 if (arg_action == ACTION_RELOAD)
1233 else if (arg_action == ACTION_REEXEC)
1234 method = "Reexecute";
1236 assert(arg_action == ACTION_SYSTEMCTL);
1239 streq(args[0], "clear-jobs") ? "ClearJobs" :
1240 streq(args[0], "daemon-reload") ? "Reload" :
1241 streq(args[0], "daemon-reexec") ? "Reexecute" :
1245 if (!(m = dbus_message_new_method_call(
1246 "org.freedesktop.systemd1",
1247 "/org/freedesktop/systemd1",
1248 "org.freedesktop.systemd1.Manager",
1250 log_error("Could not allocate message.");
1254 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1256 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
1257 /* There's always a fallback possible for
1258 * legacy actions. */
1263 log_error("Failed to issue method call: %s", error.message);
1272 dbus_message_unref(m);
1275 dbus_message_unref(reply);
1277 dbus_error_free(&error);
1282 static int show_enviroment(DBusConnection *bus, char **args, unsigned n) {
1283 DBusMessage *m = NULL, *reply = NULL;
1285 DBusMessageIter iter, sub, sub2;
1288 *interface = "org.freedesktop.systemd1.Manager",
1289 *property = "Environment";
1291 dbus_error_init(&error);
1293 if (!(m = dbus_message_new_method_call(
1294 "org.freedesktop.systemd1",
1295 "/org/freedesktop/systemd1",
1296 "org.freedesktop.DBus.Properties",
1298 log_error("Could not allocate message.");
1302 if (!dbus_message_append_args(m,
1303 DBUS_TYPE_STRING, &interface,
1304 DBUS_TYPE_STRING, &property,
1305 DBUS_TYPE_INVALID)) {
1306 log_error("Could not append arguments to message.");
1311 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1312 log_error("Failed to issue method call: %s", error.message);
1317 if (!dbus_message_iter_init(reply, &iter) ||
1318 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1319 log_error("Failed to parse reply.");
1324 dbus_message_iter_recurse(&iter, &sub);
1326 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
1327 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
1328 log_error("Failed to parse reply.");
1333 dbus_message_iter_recurse(&sub, &sub2);
1335 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
1338 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
1339 log_error("Failed to parse reply.");
1344 dbus_message_iter_get_basic(&sub2, &text);
1345 printf("%s\n", text);
1347 dbus_message_iter_next(&sub2);
1354 dbus_message_unref(m);
1357 dbus_message_unref(reply);
1359 dbus_error_free(&error);
1364 static int set_environment(DBusConnection *bus, char **args, unsigned n) {
1365 DBusMessage *m = NULL, *reply = NULL;
1369 DBusMessageIter iter, sub;
1372 dbus_error_init(&error);
1374 method = streq(args[0], "set-environment")
1376 : "UnsetEnvironment";
1378 if (!(m = dbus_message_new_method_call(
1379 "org.freedesktop.systemd1",
1380 "/org/freedesktop/systemd1",
1381 "org.freedesktop.systemd1.Manager",
1384 log_error("Could not allocate message.");
1388 dbus_message_iter_init_append(m, &iter);
1390 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
1391 log_error("Could not append arguments to message.");
1396 for (i = 1; i < n; i++)
1397 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &args[i])) {
1398 log_error("Could not append arguments to message.");
1403 if (!dbus_message_iter_close_container(&iter, &sub)) {
1404 log_error("Could not append arguments to message.");
1409 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1410 log_error("Failed to issue method call: %s", error.message);
1419 dbus_message_unref(m);
1422 dbus_message_unref(reply);
1424 dbus_error_free(&error);
1429 static int systemctl_help(void) {
1431 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
1432 "Send control commands to the systemd manager.\n\n"
1433 " -h --help Show this help\n"
1434 " -t --type=TYPE List only units of a particular type\n"
1435 " -a --all Show all units, including dead ones\n"
1436 " --replace When installing a new job, replace existing conflicting ones\n"
1437 " --system Connect to system bus\n"
1438 " --session Connect to session bus\n"
1439 " --block Wait until operation finished\n"
1440 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
1442 " list-units List units\n"
1443 " list-jobs List jobs\n"
1444 " clear-jobs Cancel all jobs\n"
1445 " load [NAME...] Load one or more units\n"
1446 " cancel [JOB...] Cancel one or more jobs\n"
1447 " start [NAME...] Start one or more units\n"
1448 " stop [NAME...] Stop one or more units\n"
1449 " restart [NAME...] Restart one or more units\n"
1450 " reload [NAME...] Reload one or more units\n"
1451 " isolate [NAME] Start one unit and stop all others\n"
1452 " monitor Monitor unit/job changes\n"
1453 " dump Dump server status\n"
1454 " snapshot [NAME] Create a snapshot\n"
1455 " daemon-reload Reload systemd manager configuration\n"
1456 " daemon-reexec Reexecute systemd manager\n"
1457 " daemon-exit Ask the systemd manager to quit\n"
1458 " show-environment Dump environment\n"
1459 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
1460 " unset-environment [NAME...] Unset one or more environment variables\n"
1461 " halt Shut down and halt the system\n"
1462 " poweroff Shut down and power-off the system\n"
1463 " reboot Shut down and reboot the system\n"
1464 " default Enter default mode\n"
1465 " rescue Enter rescue mode\n"
1466 " emergency Enter emergency mode\n",
1467 program_invocation_short_name);
1472 static int halt_help(void) {
1474 printf("%s [OPTIONS...]\n\n"
1475 "%s the system.\n\n"
1476 " --help Show this help\n"
1477 " --halt Halt the machine\n"
1478 " -p --poweroff Switch off the machine\n"
1479 " --reboot Reboot the machine\n"
1480 " -f --force Force immediate halt/power-off/reboot\n"
1481 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
1482 " -d --no-wtmp Don't write wtmp record\n"
1483 " -n --no-sync Don't sync before halt/power-off/reboot\n"
1484 " --no-wall Don't send wall message before halt/power-off/reboot\n",
1485 program_invocation_short_name,
1486 arg_action == ACTION_REBOOT ? "Reboot" :
1487 arg_action == ACTION_POWEROFF ? "Power off" :
1493 static int shutdown_help(void) {
1495 printf("%s [OPTIONS...] [now] [WALL...]\n\n"
1496 "Shut down the system.\n\n"
1497 " --help Show this help\n"
1498 " -H --halt Halt the machine\n"
1499 " -P --poweroff Power-off the machine\n"
1500 " -r --reboot Reboot the machine\n"
1501 " -h Equivalent to --poweroff, overriden by --halt\n"
1502 " -k Don't halt/power-off/reboot, just send warnings\n"
1503 " --no-wall Don't send wall message before halt/power-off/reboot\n",
1504 program_invocation_short_name);
1509 static int telinit_help(void) {
1511 printf("%s [OPTIONS...] {COMMAND}\n\n"
1512 "Send control commands to the init daemon.\n\n"
1513 " --help Show this help\n"
1514 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
1516 " 0 Power-off the machine\n"
1517 " 6 Reboot the machine\n"
1518 " 2, 3, 4, 5 Start runlevelX.target unit\n"
1519 " 1, s, S Enter rescue mode\n"
1520 " q, Q Reload init daemon configuration\n"
1521 " u, U Reexecute init daemon\n",
1522 program_invocation_short_name);
1527 static int runlevel_help(void) {
1529 printf("%s [OPTIONS...]\n\n"
1530 "Prints the previous and current runlevel of the init system.\n\n"
1531 " --help Show this help\n",
1532 program_invocation_short_name);
1537 static int systemctl_parse_argv(int argc, char *argv[]) {
1540 ARG_REPLACE = 0x100,
1547 static const struct option options[] = {
1548 { "help", no_argument, NULL, 'h' },
1549 { "type", required_argument, NULL, 't' },
1550 { "all", no_argument, NULL, 'a' },
1551 { "replace", no_argument, NULL, ARG_REPLACE },
1552 { "session", no_argument, NULL, ARG_SESSION },
1553 { "system", no_argument, NULL, ARG_SYSTEM },
1554 { "block", no_argument, NULL, ARG_BLOCK },
1555 { "no-wall", no_argument, NULL, ARG_NO_WALL },
1556 { NULL, 0, NULL, 0 }
1564 while ((c = getopt_long(argc, argv, "hta", options, NULL)) >= 0) {
1589 arg_session = false;
1604 log_error("Unknown option code %c", c);
1612 static int halt_parse_argv(int argc, char *argv[]) {
1621 static const struct option options[] = {
1622 { "help", no_argument, NULL, ARG_HELP },
1623 { "halt", no_argument, NULL, ARG_HALT },
1624 { "poweroff", no_argument, NULL, 'p' },
1625 { "reboot", no_argument, NULL, ARG_REBOOT },
1626 { "force", no_argument, NULL, 'f' },
1627 { "wtmp-only", no_argument, NULL, 'w' },
1628 { "no-wtmp", no_argument, NULL, 'd' },
1629 { "no-sync", no_argument, NULL, 'n' },
1630 { "no-wall", no_argument, NULL, ARG_NO_WALL },
1631 { NULL, 0, NULL, 0 }
1639 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
1640 if (runlevel == '0' || runlevel == '6')
1641 arg_immediate = true;
1643 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
1651 arg_action = ACTION_HALT;
1655 arg_action = ACTION_POWEROFF;
1659 arg_action = ACTION_REBOOT;
1663 arg_immediate = true;
1684 /* Compatibility nops */
1691 log_error("Unknown option code %c", c);
1696 if (optind < argc) {
1697 log_error("Too many arguments.");
1704 static int shutdown_parse_argv(int argc, char *argv[]) {
1711 static const struct option options[] = {
1712 { "help", no_argument, NULL, ARG_HELP },
1713 { "halt", no_argument, NULL, 'H' },
1714 { "poweroff", no_argument, NULL, 'P' },
1715 { "reboot", no_argument, NULL, 'r' },
1716 { "no-wall", no_argument, NULL, ARG_NO_WALL },
1717 { NULL, 0, NULL, 0 }
1725 while ((c = getopt_long(argc, argv, "HPrhkt:a", options, NULL)) >= 0) {
1733 arg_action = ACTION_HALT;
1737 arg_action = ACTION_POWEROFF;
1741 arg_action = ACTION_REBOOT;
1745 if (arg_action != ACTION_HALT)
1746 arg_action = ACTION_POWEROFF;
1759 /* Compatibility nops */
1766 log_error("Unknown option code %c", c);
1771 if (argc > optind && !streq(argv[optind], "now"))
1772 log_warning("First argument '%s' isn't 'now'. Ignoring.", argv[optind]);
1774 /* We ignore the time argument */
1775 if (argc > optind + 1)
1776 arg_wall = argv + optind + 1;
1783 static int telinit_parse_argv(int argc, char *argv[]) {
1790 static const struct option options[] = {
1791 { "help", no_argument, NULL, ARG_HELP },
1792 { "no-wall", no_argument, NULL, ARG_NO_WALL },
1793 { NULL, 0, NULL, 0 }
1796 static const struct {
1800 { '0', ACTION_POWEROFF },
1801 { '6', ACTION_REBOOT },
1802 { '1', ACTION_RESCUE },
1803 { '2', ACTION_RUNLEVEL2 },
1804 { '3', ACTION_RUNLEVEL3 },
1805 { '4', ACTION_RUNLEVEL4 },
1806 { '5', ACTION_RUNLEVEL5 },
1807 { 's', ACTION_RESCUE },
1808 { 'S', ACTION_RESCUE },
1809 { 'q', ACTION_RELOAD },
1810 { 'Q', ACTION_RELOAD },
1811 { 'u', ACTION_REEXEC },
1812 { 'U', ACTION_REEXEC }
1821 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
1836 log_error("Unknown option code %c", c);
1841 if (optind >= argc) {
1846 if (optind + 1 < argc) {
1847 log_error("Too many arguments.");
1851 if (strlen(argv[optind]) != 1) {
1852 log_error("Expected single character argument.");
1856 for (i = 0; i < ELEMENTSOF(table); i++)
1857 if (table[i].from == argv[optind][0])
1860 if (i >= ELEMENTSOF(table)) {
1861 log_error("Unknown command %s.", argv[optind]);
1865 arg_action = table[i].to;
1872 static int runlevel_parse_argv(int argc, char *argv[]) {
1878 static const struct option options[] = {
1879 { "help", no_argument, NULL, ARG_HELP },
1880 { NULL, 0, NULL, 0 }
1888 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
1899 log_error("Unknown option code %c", c);
1904 if (optind < argc) {
1905 log_error("Too many arguments.");
1912 static int parse_argv(int argc, char *argv[]) {
1916 if (program_invocation_short_name) {
1918 if (strstr(program_invocation_short_name, "halt")) {
1919 arg_action = ACTION_HALT;
1920 return halt_parse_argv(argc, argv);
1921 } else if (strstr(program_invocation_short_name, "poweroff")) {
1922 arg_action = ACTION_POWEROFF;
1923 return halt_parse_argv(argc, argv);
1924 } else if (strstr(program_invocation_short_name, "reboot")) {
1925 arg_action = ACTION_REBOOT;
1926 return halt_parse_argv(argc, argv);
1927 } else if (strstr(program_invocation_short_name, "shutdown")) {
1928 arg_action = ACTION_POWEROFF;
1929 return shutdown_parse_argv(argc, argv);
1930 } else if (strstr(program_invocation_short_name, "init")) {
1931 arg_action = ACTION_INVALID;
1932 return telinit_parse_argv(argc, argv);
1933 } else if (strstr(program_invocation_short_name, "runlevel")) {
1934 arg_action = ACTION_RUNLEVEL;
1935 return runlevel_parse_argv(argc, argv);
1939 arg_action = ACTION_SYSTEMCTL;
1940 return systemctl_parse_argv(argc, argv);
1943 static int action_to_runlevel(void) {
1945 static const char table[_ACTION_MAX] = {
1946 [ACTION_HALT] = '0',
1947 [ACTION_POWEROFF] = '0',
1948 [ACTION_REBOOT] = '6',
1949 [ACTION_RUNLEVEL2] = '2',
1950 [ACTION_RUNLEVEL3] = '3',
1951 [ACTION_RUNLEVEL4] = '4',
1952 [ACTION_RUNLEVEL5] = '5',
1953 [ACTION_RESCUE] = '1'
1956 assert(arg_action < _ACTION_MAX);
1958 return table[arg_action];
1961 static int talk_upstart(void) {
1962 DBusMessage *m = NULL, *reply = NULL;
1964 int previous, rl, r;
1966 env1_buf[] = "RUNLEVEL=X",
1967 env2_buf[] = "PREVLEVEL=X";
1968 char *env1 = env1_buf, *env2 = env2_buf;
1969 const char *emit = "runlevel";
1970 dbus_bool_t b_false = FALSE;
1971 DBusMessageIter iter, sub;
1972 DBusConnection *bus;
1974 dbus_error_init(&error);
1976 if (!(rl = action_to_runlevel()))
1979 if (utmp_get_runlevel(&previous, NULL) < 0)
1982 if (!(bus = dbus_connection_open("unix:abstract=/com/ubuntu/upstart", &error))) {
1983 if (dbus_error_has_name(&error, DBUS_ERROR_NO_SERVER)) {
1988 log_error("Failed to connect to Upstart bus: %s", error.message);
1993 if ((r = bus_check_peercred(bus)) < 0) {
1994 log_error("Failed to verify owner of bus.");
1998 if (!(m = dbus_message_new_method_call(
1999 "com.ubuntu.Upstart",
2000 "/com/ubuntu/Upstart",
2001 "com.ubuntu.Upstart0_6",
2004 log_error("Could not allocate message.");
2009 dbus_message_iter_init_append(m, &iter);
2011 env1_buf[sizeof(env1_buf)-2] = rl;
2012 env2_buf[sizeof(env2_buf)-2] = previous;
2014 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
2015 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
2016 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
2017 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
2018 !dbus_message_iter_close_container(&iter, &sub) ||
2019 !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
2020 log_error("Could not append arguments to message.");
2025 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2027 if (error_is_no_service(&error)) {
2032 log_error("Failed to issue method call: %s", error.message);
2041 dbus_message_unref(m);
2044 dbus_message_unref(reply);
2047 dbus_connection_unref(bus);
2049 dbus_error_free(&error);
2054 static int talk_initctl(void) {
2055 struct init_request request;
2059 if (!(rl = action_to_runlevel()))
2063 request.magic = INIT_MAGIC;
2064 request.sleeptime = 0;
2065 request.cmd = INIT_CMD_RUNLVL;
2066 request.runlevel = rl;
2068 if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
2070 if (errno == ENOENT)
2073 log_error("Failed to open "INIT_FIFO": %m");
2078 r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
2079 close_nointr_nofail(fd);
2082 log_error("Failed to write to "INIT_FIFO": %m");
2083 return errno ? -errno : -EIO;
2089 static int systemctl_main(DBusConnection *bus, int argc, char *argv[]) {
2091 static const struct {
2099 int (* const dispatch)(DBusConnection *bus, char **args, unsigned n);
2101 { "list-units", LESS, 1, list_units },
2102 { "list-jobs", EQUAL, 1, list_jobs },
2103 { "clear-jobs", EQUAL, 1, clear_jobs },
2104 { "load", MORE, 2, load_unit },
2105 { "cancel", MORE, 2, cancel_job },
2106 { "start", MORE, 2, start_unit },
2107 { "stop", MORE, 2, start_unit },
2108 { "reload", MORE, 2, start_unit },
2109 { "restart", MORE, 2, start_unit },
2110 { "isolate", EQUAL, 2, start_unit },
2111 { "monitor", EQUAL, 1, monitor },
2112 { "dump", EQUAL, 1, dump },
2113 { "snapshot", LESS, 2, snapshot },
2114 { "daemon-reload", EQUAL, 1, clear_jobs },
2115 { "daemon-reexec", EQUAL, 1, clear_jobs },
2116 { "daemon-exit", EQUAL, 1, clear_jobs },
2117 { "show-environment", EQUAL, 1, show_enviroment },
2118 { "set-environment", MORE, 2, set_environment },
2119 { "unset-environment", MORE, 2, set_environment },
2120 { "halt", EQUAL, 1, start_special },
2121 { "poweroff", EQUAL, 1, start_special },
2122 { "reboot", EQUAL, 1, start_special },
2123 { "default", EQUAL, 1, start_special },
2124 { "rescue", EQUAL, 1, start_special },
2125 { "emergency", EQUAL, 1, start_special },
2135 left = argc - optind;
2138 /* Special rule: no arguments means "list-units" */
2141 for (i = 0; i < ELEMENTSOF(verbs); i++)
2142 if (streq(argv[optind], verbs[i].verb))
2145 if (i >= ELEMENTSOF(verbs)) {
2146 log_error("Unknown operation %s", argv[optind]);
2151 switch (verbs[i].argc_cmp) {
2154 if (left != verbs[i].argc) {
2155 log_error("Invalid number of arguments.");
2162 if (left < verbs[i].argc) {
2163 log_error("Too few arguments.");
2170 if (left > verbs[i].argc) {
2171 log_error("Too many arguments.");
2178 assert_not_reached("Unknown comparison operator.");
2181 return verbs[i].dispatch(bus, argv + optind, left);
2184 static int reload_with_fallback(DBusConnection *bus) {
2188 /* First, try systemd via D-Bus. */
2189 if ((r = clear_jobs(bus, NULL, 0)) > 0)
2193 /* Nothing else worked, so let's try signals */
2194 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
2196 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
2197 log_error("kill() failed: %m");
2204 static int start_with_fallback(DBusConnection *bus) {
2207 warn_wall(arg_action);
2210 /* First, try systemd via D-Bus. */
2211 if ((r = start_unit(bus, NULL, 0)) > 0)
2214 /* Hmm, talking to systemd via D-Bus didn't work. Then
2215 * let's try to talk to Upstart via D-Bus. */
2216 if ((r = talk_upstart()) > 0)
2220 /* Nothing else worked, so let's try
2222 if ((r = talk_initctl()) != 0)
2225 log_error("Failed to talk to init daemon.");
2229 static int halt_main(DBusConnection *bus) {
2233 return start_with_fallback(bus);
2236 if ((r = utmp_put_shutdown(0)) < 0)
2237 log_warning("Failed to write utmp record: %s", strerror(-r));
2245 /* Make sure C-A-D is handled by the kernel from this
2247 reboot(RB_ENABLE_CAD);
2249 switch (arg_action) {
2252 log_info("Halting");
2253 reboot(RB_HALT_SYSTEM);
2256 case ACTION_POWEROFF:
2257 log_info("Powering off");
2258 reboot(RB_POWER_OFF);
2262 log_info("Rebooting");
2263 reboot(RB_AUTOBOOT);
2267 assert_not_reached("Unknown halt action.");
2270 /* We should never reach this. */
2274 static int runlevel_main(void) {
2275 int r, runlevel, previous;
2277 if ((r = utmp_get_runlevel(&runlevel, &previous)) < 0) {
2283 previous <= 0 ? 'N' : previous,
2284 runlevel <= 0 ? 'N' : runlevel);
2289 int main(int argc, char*argv[]) {
2291 DBusConnection *bus = NULL;
2294 dbus_error_init(&error);
2296 log_parse_environment();
2298 if ((r = parse_argv(argc, argv)) < 0)
2305 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
2306 * let's shortcut this */
2307 if (arg_action == ACTION_RUNLEVEL) {
2308 retval = runlevel_main() < 0;
2312 /* If we are root, then let's not go via the bus */
2313 if (geteuid() == 0 && !arg_session) {
2314 bus = dbus_connection_open("unix:abstract=/org/freedesktop/systemd1/private", &error);
2316 if (bus && bus_check_peercred(bus) < 0) {
2317 log_error("Failed to verify owner of bus.");
2321 bus = dbus_bus_get(arg_session ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &error);
2324 dbus_connection_set_exit_on_disconnect(bus, FALSE);
2326 switch (arg_action) {
2328 case ACTION_SYSTEMCTL: {
2331 log_error("Failed to get D-Bus connection: %s", error.message);
2335 retval = systemctl_main(bus, argc, argv) < 0;
2340 case ACTION_POWEROFF:
2342 retval = halt_main(bus) < 0;
2345 case ACTION_RUNLEVEL2:
2346 case ACTION_RUNLEVEL3:
2347 case ACTION_RUNLEVEL4:
2348 case ACTION_RUNLEVEL5:
2350 case ACTION_EMERGENCY:
2351 case ACTION_DEFAULT:
2352 retval = start_with_fallback(bus) < 0;
2357 retval = reload_with_fallback(bus) < 0;
2360 case ACTION_INVALID:
2361 case ACTION_RUNLEVEL:
2363 assert_not_reached("Unknown action");
2369 dbus_connection_unref(bus);
2371 dbus_error_free(&error);