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 <dbus/dbus.h>
38 #include "utmp-wtmp.h"
41 static const char *arg_type = NULL;
42 static bool arg_all = false;
43 static bool arg_replace = false;
44 static bool arg_session = false;
45 static bool arg_block = false;
46 static bool arg_immediate = false;
47 static bool arg_no_wtmp = false;
48 static bool arg_no_sync = false;
49 static bool arg_no_wall = false;
50 static bool arg_dry = false;
51 static char **arg_wall = NULL;
69 } arg_action = ACTION_SYSTEMCTL;
71 static bool error_is_no_service(DBusError *error) {
75 if (!dbus_error_is_set(error))
78 if (dbus_error_has_name(error, DBUS_ERROR_NAME_HAS_NO_OWNER))
81 if (dbus_error_has_name(error, DBUS_ERROR_SERVICE_UNKNOWN))
84 return startswith(error->name, "org.freedesktop.DBus.Error.Spawn.");
87 static int bus_iter_get_basic_and_next(DBusMessageIter *iter, int type, void *data, bool next) {
92 if (dbus_message_iter_get_arg_type(iter) != type)
95 dbus_message_iter_get_basic(iter, data);
97 if (!dbus_message_iter_next(iter) != !next)
103 static int columns(void) {
104 static int parsed_columns = 0;
107 if (parsed_columns > 0)
108 return parsed_columns;
110 if ((e = getenv("COLUMNS")))
111 parsed_columns = atoi(e);
113 if (parsed_columns <= 0) {
117 if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) >= 0)
118 parsed_columns = ws.ws_col;
121 if (parsed_columns <= 0)
124 return parsed_columns;
128 static void warn_wall(enum action action) {
129 static const char *table[_ACTION_MAX] = {
130 [ACTION_HALT] = "The system is going down for system halt NOW!",
131 [ACTION_REBOOT] = "The system is going down for reboot NOW!",
132 [ACTION_POWEROFF] = "The system is going down for power-off NOW!",
133 [ACTION_RESCUE] = "The system is going down to rescue mode NOW!",
134 [ACTION_EMERGENCY] = "The system is going down to emergency mode NOW!"
143 utmp_wall(table[action]);
146 static int list_units(DBusConnection *bus, char **args, unsigned n) {
147 DBusMessage *m = NULL, *reply = NULL;
150 DBusMessageIter iter, sub, sub2;
153 dbus_error_init(&error);
157 if (!(m = dbus_message_new_method_call(
158 "org.freedesktop.systemd1",
159 "/org/freedesktop/systemd1",
160 "org.freedesktop.systemd1.Manager",
162 log_error("Could not allocate message.");
166 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
167 log_error("Failed to issue method call: %s", error.message);
172 if (!dbus_message_iter_init(reply, &iter) ||
173 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
174 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
175 log_error("Failed to parse reply.");
180 dbus_message_iter_recurse(&iter, &sub);
182 printf("%-45s %-6s %-12s %-12s %-15s %s\n", "UNIT", "LOAD", "ACTIVE", "SUB", "JOB", "DESCRIPTION");
184 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
185 const char *id, *description, *load_state, *active_state, *sub_state, *unit_state, *job_type, *job_path, *dot;
188 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
189 log_error("Failed to parse reply.");
194 dbus_message_iter_recurse(&sub, &sub2);
196 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
197 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &description, true) < 0 ||
198 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &load_state, true) < 0 ||
199 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &active_state, true) < 0 ||
200 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &sub_state, true) < 0 ||
201 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_state, true) < 0 ||
202 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &job_id, true) < 0 ||
203 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &job_type, true) < 0 ||
204 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, false) < 0) {
205 log_error("Failed to parse reply.");
210 if ((!arg_type || ((dot = strrchr(id, '.')) &&
211 streq(dot+1, arg_type))) &&
212 (arg_all || !streq(active_state, "inactive"))) {
216 printf("%-45s %-6s %-12s %-12s%n", id, load_state, active_state, sub_state, &a);
219 printf(" %-15s%n", job_type, &b);
223 if (a + b + 2 < columns()) {
227 printf("%.*s", columns() - a - b - 2, description);
234 dbus_message_iter_next(&sub);
238 printf("\n%u units listed.\n", k);
240 printf("\n%u live units listed. Pass --all to see dead units, too.\n", k);
246 dbus_message_unref(m);
249 dbus_message_unref(reply);
251 dbus_error_free(&error);
256 static int list_jobs(DBusConnection *bus, char **args, unsigned n) {
257 DBusMessage *m = NULL, *reply = NULL;
260 DBusMessageIter iter, sub, sub2;
263 dbus_error_init(&error);
267 if (!(m = dbus_message_new_method_call(
268 "org.freedesktop.systemd1",
269 "/org/freedesktop/systemd1",
270 "org.freedesktop.systemd1.Manager",
272 log_error("Could not allocate message.");
276 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
277 log_error("Failed to issue method call: %s", error.message);
282 if (!dbus_message_iter_init(reply, &iter) ||
283 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
284 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
285 log_error("Failed to parse reply.");
290 dbus_message_iter_recurse(&iter, &sub);
292 printf("%4s %-45s %-17s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
294 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
295 const char *name, *type, *state, *job_path, *unit_path;
298 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
299 log_error("Failed to parse reply.");
304 dbus_message_iter_recurse(&sub, &sub2);
306 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &id, true) < 0 ||
307 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0 ||
308 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
309 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, true) < 0 ||
310 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, true) < 0 ||
311 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, false) < 0) {
312 log_error("Failed to parse reply.");
317 printf("%4u %-45s %-17s %-7s\n", id, name, type, state);
320 dbus_message_iter_next(&sub);
323 printf("\n%u jobs listed.\n", k);
328 dbus_message_unref(m);
331 dbus_message_unref(reply);
333 dbus_error_free(&error);
338 static int load_unit(DBusConnection *bus, char **args, unsigned n) {
339 DBusMessage *m = NULL, *reply = NULL;
344 dbus_error_init(&error);
349 for (i = 1; i < n; i++) {
351 if (!(m = dbus_message_new_method_call(
352 "org.freedesktop.systemd1",
353 "/org/freedesktop/systemd1",
354 "org.freedesktop.systemd1.Manager",
356 log_error("Could not allocate message.");
361 if (!dbus_message_append_args(m,
362 DBUS_TYPE_STRING, &args[i],
363 DBUS_TYPE_INVALID)) {
364 log_error("Could not append arguments to message.");
369 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
370 log_error("Failed to issue method call: %s", error.message);
375 dbus_message_unref(m);
376 dbus_message_unref(reply);
385 dbus_message_unref(m);
388 dbus_message_unref(reply);
390 dbus_error_free(&error);
395 static int cancel_job(DBusConnection *bus, char **args, unsigned n) {
396 DBusMessage *m = NULL, *reply = NULL;
401 dbus_error_init(&error);
406 for (i = 1; i < n; i++) {
410 if (!(m = dbus_message_new_method_call(
411 "org.freedesktop.systemd1",
412 "/org/freedesktop/systemd1",
413 "org.freedesktop.systemd1.Manager",
415 log_error("Could not allocate message.");
420 if ((r = safe_atou(args[i], &id)) < 0) {
421 log_error("Failed to parse job id: %s", strerror(-r));
425 assert_cc(sizeof(uint32_t) == sizeof(id));
426 if (!dbus_message_append_args(m,
427 DBUS_TYPE_UINT32, &id,
428 DBUS_TYPE_INVALID)) {
429 log_error("Could not append arguments to message.");
434 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
435 log_error("Failed to issue method call: %s", error.message);
440 if (!dbus_message_get_args(reply, &error,
441 DBUS_TYPE_OBJECT_PATH, &path,
442 DBUS_TYPE_INVALID)) {
443 log_error("Failed to parse reply: %s", error.message);
448 dbus_message_unref(m);
449 if (!(m = dbus_message_new_method_call(
450 "org.freedesktop.systemd1",
452 "org.freedesktop.systemd1.Job",
454 log_error("Could not allocate message.");
459 dbus_message_unref(reply);
460 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
461 log_error("Failed to issue method call: %s", error.message);
466 dbus_message_unref(m);
467 dbus_message_unref(reply);
475 dbus_message_unref(m);
478 dbus_message_unref(reply);
480 dbus_error_free(&error);
485 static DBusHandlerResult wait_filter(DBusConnection *connection, DBusMessage *message, void *data) {
493 dbus_error_init(&error);
495 /* log_debug("Got D-Bus request: %s.%s() on %s", */
496 /* dbus_message_get_interface(message), */
497 /* dbus_message_get_member(message), */
498 /* dbus_message_get_path(message)); */
500 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
501 log_error("Warning! D-Bus connection terminated.");
502 dbus_connection_close(connection);
504 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
508 if (!dbus_message_get_args(message, &error,
509 DBUS_TYPE_UINT32, &id,
510 DBUS_TYPE_OBJECT_PATH, &path,
512 log_error("Failed to parse message: %s", error.message);
516 if ((p = set_remove(s, (char*) path)))
521 dbus_error_free(&error);
522 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
525 static int enable_wait_for_jobs(DBusConnection *bus) {
527 DBusMessage *m = NULL, *reply = NULL;
532 dbus_error_init(&error);
534 dbus_bus_add_match(bus,
536 "sender='org.freedesktop.systemd1',"
537 "interface='org.freedesktop.systemd1.Manager',"
538 "member='JobRemoved',"
539 "path='/org/freedesktop/systemd1'",
542 if (dbus_error_is_set(&error)) {
543 log_error("Failed to add match: %s", error.message);
548 if (!(m = dbus_message_new_method_call(
549 "org.freedesktop.systemd1",
550 "/org/freedesktop/systemd1",
551 "org.freedesktop.systemd1.Manager",
553 log_error("Could not allocate message.");
558 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
559 log_error("Failed to issue method call: %s", error.message);
567 /* This is slightly dirty, since we don't undo the match registrations. */
570 dbus_message_unref(m);
573 dbus_message_unref(reply);
575 dbus_error_free(&error);
580 static int wait_for_jobs(DBusConnection *bus, Set *s) {
586 if (!dbus_connection_add_filter(bus, wait_filter, s, NULL)) {
587 log_error("Failed to add filter.");
592 while (!set_isempty(s) &&
593 dbus_connection_read_write_dispatch(bus, -1))
599 /* This is slightly dirty, since we don't undo the filter registration. */
604 static int start_unit_one(
611 DBusMessage *m = NULL, *reply = NULL;
619 assert(!arg_block || s);
621 dbus_error_init(&error);
623 if (!(m = dbus_message_new_method_call(
624 "org.freedesktop.systemd1",
625 "/org/freedesktop/systemd1",
626 "org.freedesktop.systemd1.Manager",
628 log_error("Could not allocate message.");
633 if (!dbus_message_append_args(m,
634 DBUS_TYPE_STRING, &name,
635 DBUS_TYPE_STRING, &mode,
636 DBUS_TYPE_INVALID)) {
637 log_error("Could not append arguments to message.");
642 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
644 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
645 /* There's always a fallback possible for
651 log_error("Failed to issue method call: %s", error.message);
660 if (!dbus_message_get_args(reply, &error,
661 DBUS_TYPE_OBJECT_PATH, &path,
662 DBUS_TYPE_INVALID)) {
663 log_error("Failed to parse reply: %s", error.message);
668 if (!(p = strdup(path))) {
669 log_error("Failed to duplicate path.");
674 if ((r = set_put(s, p)) < 0) {
676 log_error("Failed to add path to set.");
685 dbus_message_unref(m);
688 dbus_message_unref(reply);
690 dbus_error_free(&error);
695 static enum action verb_to_action(const char *verb) {
696 if (streq(verb, "halt"))
698 else if (streq(verb, "poweroff"))
699 return ACTION_POWEROFF;
700 else if (streq(verb, "reboot"))
701 return ACTION_REBOOT;
702 else if (streq(verb, "rescue"))
703 return ACTION_RESCUE;
704 else if (streq(verb, "emergency"))
705 return ACTION_EMERGENCY;
706 else if (streq(verb, "default"))
707 return ACTION_DEFAULT;
709 return ACTION_INVALID;
712 static int start_unit(DBusConnection *bus, char **args, unsigned n) {
714 static const char * const table[_ACTION_MAX] = {
715 [ACTION_HALT] = SPECIAL_HALT_TARGET,
716 [ACTION_POWEROFF] = SPECIAL_POWEROFF_TARGET,
717 [ACTION_REBOOT] = SPECIAL_REBOOT_TARGET,
718 [ACTION_RUNLEVEL2] = SPECIAL_RUNLEVEL2_TARGET,
719 [ACTION_RUNLEVEL3] = SPECIAL_RUNLEVEL3_TARGET,
720 [ACTION_RUNLEVEL4] = SPECIAL_RUNLEVEL4_TARGET,
721 [ACTION_RUNLEVEL5] = SPECIAL_RUNLEVEL5_TARGET,
722 [ACTION_RESCUE] = SPECIAL_RESCUE_TARGET,
723 [ACTION_EMERGENCY] = SPECIAL_EMERGENCY_SERVICE,
724 [ACTION_DEFAULT] = SPECIAL_DEFAULT_TARGET
729 const char *method, *mode, *one_name;
734 if (arg_action == ACTION_SYSTEMCTL) {
736 streq(args[0], "stop") ? "StopUnit" :
737 streq(args[0], "reload") ? "ReloadUnit" :
738 streq(args[0], "restart") ? "RestartUnit" :
742 (streq(args[0], "isolate") ||
743 streq(args[0], "rescue") ||
744 streq(args[0], "emergency")) ? "isolate" :
745 arg_replace ? "replace" :
748 one_name = table[verb_to_action(args[0])];
751 assert(arg_action < ELEMENTSOF(table));
752 assert(table[arg_action]);
754 method = "StartUnit";
756 mode = (arg_action == ACTION_EMERGENCY ||
757 arg_action == ACTION_RESCUE) ? "isolate" : "replace";
759 one_name = table[arg_action];
763 if ((r = enable_wait_for_jobs(bus)) < 0) {
764 log_error("Could not watch jobs: %s", strerror(-r));
768 if (!(s = set_new(string_hash_func, string_compare_func))) {
769 log_error("Failed to allocate set.");
778 if ((r = start_unit_one(bus, method, one_name, mode, s)) <= 0)
781 for (i = 1; i < n; i++)
782 if ((r = start_unit_one(bus, method, args[i], mode, s)) < 0)
787 r = wait_for_jobs(bus, s);
796 static int start_special(DBusConnection *bus, char **args, unsigned n) {
800 warn_wall(verb_to_action(args[0]));
802 return start_unit(bus, args, n);
805 static DBusHandlerResult monitor_filter(DBusConnection *connection, DBusMessage *message, void *data) {
807 DBusMessage *m = NULL, *reply = NULL;
812 dbus_error_init(&error);
814 /* log_debug("Got D-Bus request: %s.%s() on %s", */
815 /* dbus_message_get_interface(message), */
816 /* dbus_message_get_member(message), */
817 /* dbus_message_get_path(message)); */
819 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
820 log_error("Warning! D-Bus connection terminated.");
821 dbus_connection_close(connection);
823 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitNew") ||
824 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitRemoved")) {
825 const char *id, *path;
827 if (!dbus_message_get_args(message, &error,
828 DBUS_TYPE_STRING, &id,
829 DBUS_TYPE_OBJECT_PATH, &path,
831 log_error("Failed to parse message: %s", error.message);
832 else if (streq(dbus_message_get_member(message), "UnitNew"))
833 printf("Unit %s added.\n", id);
835 printf("Unit %s removed.\n", id);
837 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobNew") ||
838 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
842 if (!dbus_message_get_args(message, &error,
843 DBUS_TYPE_UINT32, &id,
844 DBUS_TYPE_OBJECT_PATH, &path,
846 log_error("Failed to parse message: %s", error.message);
847 else if (streq(dbus_message_get_member(message), "JobNew"))
848 printf("Job %u added.\n", id);
850 printf("Job %u removed.\n", id);
853 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Unit", "Changed") ||
854 dbus_message_is_signal(message, "org.freedesktop.systemd1.Job", "Changed")) {
856 const char *path, *interface, *property = "Id";
857 DBusMessageIter iter, sub;
859 path = dbus_message_get_path(message);
860 interface = dbus_message_get_interface(message);
862 if (!(m = dbus_message_new_method_call(
863 "org.freedesktop.systemd1",
865 "org.freedesktop.DBus.Properties",
867 log_error("Could not allocate message.");
871 if (!dbus_message_append_args(m,
872 DBUS_TYPE_STRING, &interface,
873 DBUS_TYPE_STRING, &property,
874 DBUS_TYPE_INVALID)) {
875 log_error("Could not append arguments to message.");
879 if (!(reply = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) {
880 log_error("Failed to issue method call: %s", error.message);
884 if (!dbus_message_iter_init(reply, &iter) ||
885 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
886 log_error("Failed to parse reply.");
890 dbus_message_iter_recurse(&iter, &sub);
892 if (streq(interface, "org.freedesktop.systemd1.Unit")) {
895 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
896 log_error("Failed to parse reply.");
900 dbus_message_iter_get_basic(&sub, &id);
901 printf("Unit %s changed.\n", id);
905 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT32) {
906 log_error("Failed to parse reply.");
910 dbus_message_iter_get_basic(&sub, &id);
911 printf("Job %u changed.\n", id);
917 dbus_message_unref(m);
920 dbus_message_unref(reply);
922 dbus_error_free(&error);
923 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
927 dbus_message_unref(m);
930 dbus_message_unref(reply);
932 dbus_error_free(&error);
933 return DBUS_HANDLER_RESULT_NEED_MEMORY;
936 static int monitor(DBusConnection *bus, char **args, unsigned n) {
937 DBusMessage *m = NULL, *reply = NULL;
941 dbus_error_init(&error);
943 dbus_bus_add_match(bus,
945 "sender='org.freedesktop.systemd1',"
946 "interface='org.freedesktop.systemd1.Manager',"
947 "path='/org/freedesktop/systemd1'",
950 if (dbus_error_is_set(&error)) {
951 log_error("Failed to add match: %s", error.message);
956 dbus_bus_add_match(bus,
958 "sender='org.freedesktop.systemd1',"
959 "interface='org.freedesktop.systemd1.Unit',"
963 if (dbus_error_is_set(&error)) {
964 log_error("Failed to add match: %s", error.message);
969 dbus_bus_add_match(bus,
971 "sender='org.freedesktop.systemd1',"
972 "interface='org.freedesktop.systemd1.Job',"
976 if (dbus_error_is_set(&error)) {
977 log_error("Failed to add match: %s", error.message);
982 if (!dbus_connection_add_filter(bus, monitor_filter, NULL, NULL)) {
983 log_error("Failed to add filter.");
988 if (!(m = dbus_message_new_method_call(
989 "org.freedesktop.systemd1",
990 "/org/freedesktop/systemd1",
991 "org.freedesktop.systemd1.Manager",
993 log_error("Could not allocate message.");
998 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
999 log_error("Failed to issue method call: %s", error.message);
1004 while (dbus_connection_read_write_dispatch(bus, -1))
1011 /* This is slightly dirty, since we don't undo the filter or the matches. */
1014 dbus_message_unref(m);
1017 dbus_message_unref(reply);
1019 dbus_error_free(&error);
1024 static int dump(DBusConnection *bus, char **args, unsigned n) {
1025 DBusMessage *m = NULL, *reply = NULL;
1030 dbus_error_init(&error);
1032 if (!(m = dbus_message_new_method_call(
1033 "org.freedesktop.systemd1",
1034 "/org/freedesktop/systemd1",
1035 "org.freedesktop.systemd1.Manager",
1037 log_error("Could not allocate message.");
1041 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1042 log_error("Failed to issue method call: %s", error.message);
1047 if (!dbus_message_get_args(reply, &error,
1048 DBUS_TYPE_STRING, &text,
1049 DBUS_TYPE_INVALID)) {
1050 log_error("Failed to parse reply: %s", error.message);
1055 fputs(text, stdout);
1061 dbus_message_unref(m);
1064 dbus_message_unref(reply);
1066 dbus_error_free(&error);
1071 static int snapshot(DBusConnection *bus, char **args, unsigned n) {
1072 DBusMessage *m = NULL, *reply = NULL;
1075 const char *name = "", *path, *id;
1076 dbus_bool_t cleanup = FALSE;
1077 DBusMessageIter iter, sub;
1079 *interface = "org.freedesktop.systemd1.Unit",
1082 dbus_error_init(&error);
1084 if (!(m = dbus_message_new_method_call(
1085 "org.freedesktop.systemd1",
1086 "/org/freedesktop/systemd1",
1087 "org.freedesktop.systemd1.Manager",
1088 "CreateSnapshot"))) {
1089 log_error("Could not allocate message.");
1096 if (!dbus_message_append_args(m,
1097 DBUS_TYPE_STRING, &name,
1098 DBUS_TYPE_BOOLEAN, &cleanup,
1099 DBUS_TYPE_INVALID)) {
1100 log_error("Could not append arguments to message.");
1105 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1106 log_error("Failed to issue method call: %s", error.message);
1111 if (!dbus_message_get_args(reply, &error,
1112 DBUS_TYPE_OBJECT_PATH, &path,
1113 DBUS_TYPE_INVALID)) {
1114 log_error("Failed to parse reply: %s", error.message);
1119 dbus_message_unref(m);
1120 if (!(m = dbus_message_new_method_call(
1121 "org.freedesktop.systemd1",
1123 "org.freedesktop.DBus.Properties",
1125 log_error("Could not allocate message.");
1129 if (!dbus_message_append_args(m,
1130 DBUS_TYPE_STRING, &interface,
1131 DBUS_TYPE_STRING, &property,
1132 DBUS_TYPE_INVALID)) {
1133 log_error("Could not append arguments to message.");
1138 dbus_message_unref(reply);
1139 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1140 log_error("Failed to issue method call: %s", error.message);
1145 if (!dbus_message_iter_init(reply, &iter) ||
1146 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1147 log_error("Failed to parse reply.");
1152 dbus_message_iter_recurse(&iter, &sub);
1154 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1155 log_error("Failed to parse reply.");
1160 dbus_message_iter_get_basic(&sub, &id);
1166 dbus_message_unref(m);
1169 dbus_message_unref(reply);
1171 dbus_error_free(&error);
1176 static int clear_jobs(DBusConnection *bus, char **args, unsigned n) {
1177 DBusMessage *m = NULL, *reply = NULL;
1182 dbus_error_init(&error);
1184 if (arg_action == ACTION_RELOAD)
1186 else if (arg_action == ACTION_REEXEC)
1187 method = "Reexecute";
1189 assert(arg_action == ACTION_SYSTEMCTL);
1192 streq(args[0], "clear-jobs") ? "ClearJobs" :
1193 streq(args[0], "daemon-reload") ? "Reload" :
1194 streq(args[0], "daemon-reexec") ? "Reexecute" :
1198 if (!(m = dbus_message_new_method_call(
1199 "org.freedesktop.systemd1",
1200 "/org/freedesktop/systemd1",
1201 "org.freedesktop.systemd1.Manager",
1203 log_error("Could not allocate message.");
1207 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1209 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
1210 /* There's always a fallback possible for
1211 * legacy actions. */
1216 log_error("Failed to issue method call: %s", error.message);
1225 dbus_message_unref(m);
1228 dbus_message_unref(reply);
1230 dbus_error_free(&error);
1235 static int show_enviroment(DBusConnection *bus, char **args, unsigned n) {
1236 DBusMessage *m = NULL, *reply = NULL;
1238 DBusMessageIter iter, sub, sub2;
1241 *interface = "org.freedesktop.systemd1.Manager",
1242 *property = "Environment";
1244 dbus_error_init(&error);
1246 if (!(m = dbus_message_new_method_call(
1247 "org.freedesktop.systemd1",
1248 "/org/freedesktop/systemd1",
1249 "org.freedesktop.DBus.Properties",
1251 log_error("Could not allocate message.");
1255 if (!dbus_message_append_args(m,
1256 DBUS_TYPE_STRING, &interface,
1257 DBUS_TYPE_STRING, &property,
1258 DBUS_TYPE_INVALID)) {
1259 log_error("Could not append arguments to message.");
1264 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1265 log_error("Failed to issue method call: %s", error.message);
1270 if (!dbus_message_iter_init(reply, &iter) ||
1271 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1272 log_error("Failed to parse reply.");
1277 dbus_message_iter_recurse(&iter, &sub);
1279 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
1280 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
1281 log_error("Failed to parse reply.");
1286 dbus_message_iter_recurse(&sub, &sub2);
1288 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
1291 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
1292 log_error("Failed to parse reply.");
1297 dbus_message_iter_get_basic(&sub2, &text);
1298 printf("%s\n", text);
1300 dbus_message_iter_next(&sub2);
1307 dbus_message_unref(m);
1310 dbus_message_unref(reply);
1312 dbus_error_free(&error);
1317 static int set_environment(DBusConnection *bus, char **args, unsigned n) {
1318 DBusMessage *m = NULL, *reply = NULL;
1322 DBusMessageIter iter, sub;
1325 dbus_error_init(&error);
1327 method = streq(args[0], "set-environment")
1329 : "UnsetEnvironment";
1331 if (!(m = dbus_message_new_method_call(
1332 "org.freedesktop.systemd1",
1333 "/org/freedesktop/systemd1",
1334 "org.freedesktop.systemd1.Manager",
1337 log_error("Could not allocate message.");
1341 dbus_message_iter_init_append(m, &iter);
1343 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
1344 log_error("Could not append arguments to message.");
1349 for (i = 1; i < n; i++)
1350 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &args[i])) {
1351 log_error("Could not append arguments to message.");
1356 if (!dbus_message_iter_close_container(&iter, &sub)) {
1357 log_error("Could not append arguments to message.");
1362 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1363 log_error("Failed to issue method call: %s", error.message);
1372 dbus_message_unref(m);
1375 dbus_message_unref(reply);
1377 dbus_error_free(&error);
1382 static int systemctl_help(void) {
1384 printf("%s [options]\n\n"
1385 "Send control commands to the init daemon.\n\n"
1386 " -h --help Show this help\n"
1387 " -t --type=TYPE List only units of a particular type\n"
1388 " -a --all Show all units, including dead ones\n"
1389 " --replace When installing a new job, replace existing conflicting ones\n"
1390 " --system Connect to system bus\n"
1391 " --session Connect to session bus\n"
1392 " --block Wait until operation finished\n"
1393 " --no-wall Don't send wall message before reboot/halt/power-off\n\n"
1395 " list-units List units\n"
1396 " list-jobs List jobs\n"
1397 " clear-jobs Cancel all jobs\n"
1398 " load [NAME...] Load one or more units\n"
1399 " cancel [JOB...] Cancel one or more jobs\n"
1400 " start [NAME...] Start one or more units\n"
1401 " stop [NAME...] Stop one or more units\n"
1402 " restart [NAME...] Restart one or more units\n"
1403 " reload [NAME...] Reload one or more units\n"
1404 " isolate [NAME] Start one unit and stop all others\n"
1405 " monitor Monitor unit/job changes\n"
1406 " dump Dump server status\n"
1407 " snapshot [NAME] Create a snapshot\n"
1408 " daemon-reload Reload init daemon configuration\n"
1409 " daemon-reexecute Reexecute init daemon\n"
1410 " daemon-exit Ask the init daemon to quit\n"
1411 " show-environment Dump environment\n"
1412 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
1413 " unset-environment [NAME...] Unset one or more environment variables\n"
1414 " halt Shut down and halt the system\n"
1415 " reboot Shut down and reboot the system\n"
1416 " poweroff Shut down and power off the system\n"
1417 " default Enter default mode\n"
1418 " rescue Enter rescue mode\n"
1419 " emergency Enter emergency mode\n",
1420 program_invocation_short_name);
1425 static int halt_help(void) {
1427 printf("%s [options]\n\n"
1428 "%s the system.\n\n"
1429 " --help Show this help\n"
1430 " --halt Halt the machine\n"
1431 " -p --poweroff Switch off the machine\n"
1432 " --reboot Reboot the machine\n"
1433 " -f --force Force immediate reboot/halt/power-off\n"
1434 " -w --wtmp-only Don't reboot/halt/power-off, just write wtmp record\n"
1435 " -d --no-wtmp Don't write wtmp record\n"
1436 " -n --no-sync Don't sync before reboot/halt/power-off\n"
1437 " --no-wall Don't send wall message before reboot/halt/power-off\n",
1438 program_invocation_short_name,
1439 arg_action == ACTION_REBOOT ? "Reboot" :
1440 arg_action == ACTION_POWEROFF ? "Power off" :
1446 static int shutdown_help(void) {
1448 printf("%s [options] [IGNORED] [WALL...]\n\n"
1449 "Shut down the system.\n\n"
1450 " --help Show this help\n"
1451 " -H --halt Halt the machine\n"
1452 " -P --poweroff Power-off the machine\n"
1453 " -r --reboot Reboot the machine\n"
1454 " -h Equivalent to --poweroff, overriden by --halt\n"
1455 " -k Don't reboot/halt/power-off, just send warnings\n"
1456 " --no-wall Don't send wall message before reboot/halt/power-off\n",
1457 program_invocation_short_name);
1462 static int telinit_help(void) {
1464 printf("%s [options]\n\n"
1465 "Send control commands to the init daemon.\n\n"
1466 " --help Show this help\n"
1467 " --no-wall Don't send wall message before reboot/halt/power-off\n\n"
1469 " 0 Power-off the machine\n"
1470 " 6 Reboot the machine\n"
1471 " 2, 3, 4, 5 Start runlevelX.target unit\n"
1472 " 1, s, S Enter rescue mode\n"
1473 " q, Q Reload init daemon configuration\n"
1474 " u, U Reexecute init daemon\n",
1475 program_invocation_short_name);
1480 static int runlevel_help(void) {
1482 printf("%s [options]\n\n"
1483 "Prints the previous and current runlevel of the init system.\n\n"
1484 " --help Show this help\n",
1485 program_invocation_short_name);
1490 static int systemctl_parse_argv(int argc, char *argv[]) {
1493 ARG_REPLACE = 0x100,
1500 static const struct option options[] = {
1501 { "help", no_argument, NULL, 'h' },
1502 { "type", required_argument, NULL, 't' },
1503 { "all", no_argument, NULL, 'a' },
1504 { "replace", no_argument, NULL, ARG_REPLACE },
1505 { "session", no_argument, NULL, ARG_SESSION },
1506 { "system", no_argument, NULL, ARG_SYSTEM },
1507 { "block", no_argument, NULL, ARG_BLOCK },
1508 { "no-wall", no_argument, NULL, ARG_NO_WALL },
1509 { NULL, 0, NULL, 0 }
1517 while ((c = getopt_long(argc, argv, "hta", options, NULL)) >= 0) {
1542 arg_session = false;
1557 log_error("Unknown option code %c", c);
1565 static int halt_parse_argv(int argc, char *argv[]) {
1574 static const struct option options[] = {
1575 { "help", no_argument, NULL, ARG_HELP },
1576 { "halt", no_argument, NULL, ARG_HALT },
1577 { "poweroff", no_argument, NULL, 'p' },
1578 { "reboot", no_argument, NULL, ARG_REBOOT },
1579 { "force", no_argument, NULL, 'f' },
1580 { "wtmp-only", no_argument, NULL, 'w' },
1581 { "no-wtmp", no_argument, NULL, 'd' },
1582 { "no-sync", no_argument, NULL, 'n' },
1583 { "no-wall", no_argument, NULL, ARG_NO_WALL },
1584 { NULL, 0, NULL, 0 }
1592 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
1593 if (runlevel == '0' || runlevel == '6')
1594 arg_immediate = true;
1596 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
1604 arg_action = ACTION_HALT;
1608 arg_action = ACTION_POWEROFF;
1612 arg_action = ACTION_REBOOT;
1616 arg_immediate = true;
1637 /* Compatibility nops */
1644 log_error("Unknown option code %c", c);
1649 if (optind < argc) {
1650 log_error("Too many arguments.");
1657 static int shutdown_parse_argv(int argc, char *argv[]) {
1664 static const struct option options[] = {
1665 { "help", no_argument, NULL, ARG_HELP },
1666 { "halt", no_argument, NULL, 'H' },
1667 { "poweroff", no_argument, NULL, 'P' },
1668 { "reboot", no_argument, NULL, 'r' },
1669 { "no-wall", no_argument, NULL, ARG_NO_WALL },
1670 { NULL, 0, NULL, 0 }
1678 while ((c = getopt_long(argc, argv, "HPrhkt:a", options, NULL)) >= 0) {
1686 arg_action = ACTION_HALT;
1690 arg_action = ACTION_POWEROFF;
1694 arg_action = ACTION_REBOOT;
1698 if (arg_action != ACTION_HALT)
1699 arg_action = ACTION_POWEROFF;
1712 /* Compatibility nops */
1719 log_error("Unknown option code %c", c);
1724 /* We ignore the time argument */
1725 if (argc > optind + 1)
1726 arg_wall = argv + optind + 1;
1734 static int telinit_parse_argv(int argc, char *argv[]) {
1741 static const struct option options[] = {
1742 { "help", no_argument, NULL, ARG_HELP },
1743 { "no-wall", no_argument, NULL, ARG_NO_WALL },
1744 { NULL, 0, NULL, 0 }
1747 static const struct {
1751 { '0', ACTION_POWEROFF },
1752 { '6', ACTION_REBOOT },
1753 { '1', ACTION_RESCUE },
1754 { '2', ACTION_RUNLEVEL2 },
1755 { '3', ACTION_RUNLEVEL3 },
1756 { '4', ACTION_RUNLEVEL4 },
1757 { '5', ACTION_RUNLEVEL5 },
1758 { 's', ACTION_RESCUE },
1759 { 'S', ACTION_RESCUE },
1760 { 'q', ACTION_RELOAD },
1761 { 'Q', ACTION_RELOAD },
1762 { 'u', ACTION_REEXEC },
1763 { 'U', ACTION_REEXEC }
1772 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
1787 log_error("Unknown option code %c", c);
1792 if (optind >= argc) {
1793 log_error("Argument missing.");
1797 if (optind + 1 < argc) {
1798 log_error("Too many arguments.");
1802 if (strlen(argv[optind]) != 1) {
1803 log_error("Expected single character argument.");
1807 for (i = 0; i < ELEMENTSOF(table); i++)
1808 if (table[i].from == argv[optind][0])
1811 if (i >= ELEMENTSOF(table)) {
1812 log_error("Unknown command %s.", argv[optind]);
1816 arg_action = table[i].to;
1823 static int runlevel_parse_argv(int argc, char *argv[]) {
1829 static const struct option options[] = {
1830 { "help", no_argument, NULL, ARG_HELP },
1831 { NULL, 0, NULL, 0 }
1839 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
1850 log_error("Unknown option code %c", c);
1855 if (optind < argc) {
1856 log_error("Too many arguments.");
1863 static int parse_argv(int argc, char *argv[]) {
1867 if (program_invocation_short_name) {
1869 if (strstr(program_invocation_short_name, "halt")) {
1870 arg_action = ACTION_HALT;
1871 return halt_parse_argv(argc, argv);
1872 } else if (strstr(program_invocation_short_name, "poweroff")) {
1873 arg_action = ACTION_POWEROFF;
1874 return halt_parse_argv(argc, argv);
1875 } else if (strstr(program_invocation_short_name, "reboot")) {
1876 arg_action = ACTION_REBOOT;
1877 return halt_parse_argv(argc, argv);
1878 } else if (strstr(program_invocation_short_name, "shutdown")) {
1879 arg_action = ACTION_POWEROFF;
1880 return shutdown_parse_argv(argc, argv);
1881 } else if (strstr(program_invocation_short_name, "init")) {
1882 arg_action = ACTION_INVALID;
1883 return telinit_parse_argv(argc, argv);
1884 } else if (strstr(program_invocation_short_name, "runlevel")) {
1885 arg_action = ACTION_RUNLEVEL;
1886 return runlevel_parse_argv(argc, argv);
1890 arg_action = ACTION_SYSTEMCTL;
1891 return systemctl_parse_argv(argc, argv);
1894 static int talk_upstart(DBusConnection *bus) {
1895 log_error("Talking upstart");
1899 static int talk_initctl(void) {
1900 log_error("Talking initctl");
1904 static int systemctl_main(DBusConnection *bus, int argc, char *argv[]) {
1906 static const struct {
1914 int (* const dispatch)(DBusConnection *bus, char **args, unsigned n);
1916 { "list-units", LESS, 1, list_units },
1917 { "list-jobs", EQUAL, 1, list_jobs },
1918 { "clear-jobs", EQUAL, 1, clear_jobs },
1919 { "load", MORE, 2, load_unit },
1920 { "cancel", MORE, 2, cancel_job },
1921 { "start", MORE, 2, start_unit },
1922 { "stop", MORE, 2, start_unit },
1923 { "reload", MORE, 2, start_unit },
1924 { "restart", MORE, 2, start_unit },
1925 { "isolate", EQUAL, 2, start_unit },
1926 { "monitor", EQUAL, 1, monitor },
1927 { "dump", EQUAL, 1, dump },
1928 { "snapshot", LESS, 2, snapshot },
1929 { "daemon-reload", EQUAL, 1, clear_jobs },
1930 { "daemon-reexec", EQUAL, 1, clear_jobs },
1931 { "daemon-exit", EQUAL, 1, clear_jobs },
1932 { "show-environment", EQUAL, 1, show_enviroment },
1933 { "set-environment", MORE, 2, set_environment },
1934 { "unset-environment", MORE, 2, set_environment },
1935 { "halt", EQUAL, 1, start_special },
1936 { "poweroff", EQUAL, 1, start_special },
1937 { "reboot", EQUAL, 1, start_special },
1938 { "default", EQUAL, 1, start_special },
1939 { "rescue", EQUAL, 1, start_special },
1940 { "emergency", EQUAL, 1, start_special },
1950 left = argc - optind;
1953 /* Special rule: no arguments means "list-units" */
1956 for (i = 0; i < ELEMENTSOF(verbs); i++)
1957 if (streq(argv[optind], verbs[i].verb))
1960 if (i >= ELEMENTSOF(verbs)) {
1961 log_error("Unknown operation %s", argv[optind]);
1966 switch (verbs[i].argc_cmp) {
1969 if (left != verbs[i].argc) {
1970 log_error("Invalid number of arguments.");
1977 if (left < verbs[i].argc) {
1978 log_error("Too few arguments.");
1985 if (left > verbs[i].argc) {
1986 log_error("Too many arguments.");
1993 assert_not_reached("Unknown comparison operator.");
1996 return verbs[i].dispatch(bus, argv + optind, left);
1999 static int reload_with_fallback(DBusConnection *bus) {
2003 /* First, try systemd via D-Bus. */
2004 if ((r = clear_jobs(bus, NULL, 0)) > 0)
2008 /* Nothing else worked, so let's try signals */
2009 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
2011 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
2012 log_error("kill() failed: %m");
2019 static int start_with_fallback(DBusConnection *bus) {
2022 warn_wall(arg_action);
2025 /* First, try systemd via D-Bus. */
2026 if ((r = start_unit(bus, NULL, 0)) > 0)
2029 /* Hmm, talking to systemd via D-Bus didn't work. Then
2030 * let's try to talk to Upstart via D-Bus. */
2031 if ((r = talk_upstart(bus)) > 0)
2035 /* Nothing else worked, so let's try
2037 return talk_initctl();
2040 static int halt_main(DBusConnection *bus) {
2044 return start_with_fallback(bus);
2047 if ((r = utmp_put_shutdown(0)) < 0)
2048 log_warning("Failed to write utmp record: %s", strerror(-r));
2056 /* Make sure C-A-D is handled by the kernel from this
2058 reboot(RB_ENABLE_CAD);
2060 switch (arg_action) {
2063 log_info("Halting");
2064 reboot(RB_HALT_SYSTEM);
2067 case ACTION_POWEROFF:
2068 log_info("Powering off");
2069 reboot(RB_POWER_OFF);
2073 log_info("Rebooting");
2074 reboot(RB_AUTOBOOT);
2078 assert_not_reached("Unknown halt action.");
2081 /* We should never reach this. */
2085 static int runlevel_main(void) {
2086 int r, runlevel, previous;
2088 if ((r = utmp_get_runlevel(&runlevel, &previous)) < 0) {
2094 previous <= 0 ? 'N' : previous,
2095 runlevel <= 0 ? 'N' : runlevel);
2100 int main(int argc, char*argv[]) {
2102 DBusConnection *bus = NULL;
2105 dbus_error_init(&error);
2107 log_parse_environment();
2109 if ((r = parse_argv(argc, argv)) < 0)
2116 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
2117 * let's shortcut this */
2118 if (arg_action == ACTION_RUNLEVEL) {
2119 retval = runlevel_main() < 0;
2123 if ((bus = dbus_bus_get(arg_session ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &error)))
2124 dbus_connection_set_exit_on_disconnect(bus, FALSE);
2126 switch (arg_action) {
2128 case ACTION_SYSTEMCTL: {
2131 log_error("Failed to get D-Bus connection: %s", error.message);
2135 retval = systemctl_main(bus, argc, argv) < 0;
2140 case ACTION_POWEROFF:
2142 retval = halt_main(bus) < 0;
2145 case ACTION_RUNLEVEL2:
2146 case ACTION_RUNLEVEL3:
2147 case ACTION_RUNLEVEL4:
2148 case ACTION_RUNLEVEL5:
2150 case ACTION_EMERGENCY:
2151 retval = start_with_fallback(bus) < 0;
2156 retval = reload_with_fallback(bus) < 0;
2160 assert_not_reached("Unknown action");
2166 dbus_connection_unref(bus);
2168 dbus_error_free(&error);