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_no_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 bool arg_quiet = false;
56 static char **arg_wall = NULL;
74 } arg_action = ACTION_SYSTEMCTL;
76 static bool error_is_no_service(DBusError *error) {
80 if (!dbus_error_is_set(error))
83 if (dbus_error_has_name(error, DBUS_ERROR_NAME_HAS_NO_OWNER))
86 if (dbus_error_has_name(error, DBUS_ERROR_SERVICE_UNKNOWN))
89 return startswith(error->name, "org.freedesktop.DBus.Error.Spawn.");
92 static int bus_iter_get_basic_and_next(DBusMessageIter *iter, int type, void *data, bool next) {
97 if (dbus_message_iter_get_arg_type(iter) != type)
100 dbus_message_iter_get_basic(iter, data);
102 if (!dbus_message_iter_next(iter) != !next)
108 static int bus_check_peercred(DBusConnection *c) {
115 assert_se(dbus_connection_get_unix_fd(c, &fd));
117 l = sizeof(struct ucred);
118 if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &ucred, &l) < 0) {
119 log_error("SO_PEERCRED failed: %m");
123 if (l != sizeof(struct ucred)) {
124 log_error("SO_PEERCRED returned wrong size.");
134 static int columns(void) {
135 static int parsed_columns = 0;
138 if (parsed_columns > 0)
139 return parsed_columns;
141 if ((e = getenv("COLUMNS")))
142 parsed_columns = atoi(e);
144 if (parsed_columns <= 0) {
148 if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) >= 0)
149 parsed_columns = ws.ws_col;
152 if (parsed_columns <= 0)
155 return parsed_columns;
159 static void warn_wall(enum action action) {
160 static const char *table[_ACTION_MAX] = {
161 [ACTION_HALT] = "The system is going down for system halt NOW!",
162 [ACTION_REBOOT] = "The system is going down for reboot NOW!",
163 [ACTION_POWEROFF] = "The system is going down for power-off NOW!",
164 [ACTION_RESCUE] = "The system is going down to rescue mode NOW!",
165 [ACTION_EMERGENCY] = "The system is going down to emergency mode NOW!"
174 if (!(p = strv_join(arg_wall, " "))) {
175 log_error("Failed to join strings.");
191 utmp_wall(table[action]);
194 static int list_units(DBusConnection *bus, char **args, unsigned n) {
195 DBusMessage *m = NULL, *reply = NULL;
198 DBusMessageIter iter, sub, sub2;
201 dbus_error_init(&error);
205 if (!(m = dbus_message_new_method_call(
206 "org.freedesktop.systemd1",
207 "/org/freedesktop/systemd1",
208 "org.freedesktop.systemd1.Manager",
210 log_error("Could not allocate message.");
214 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
215 log_error("Failed to issue method call: %s", error.message);
220 if (!dbus_message_iter_init(reply, &iter) ||
221 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
222 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
223 log_error("Failed to parse reply.");
228 dbus_message_iter_recurse(&iter, &sub);
230 printf("%-45s %-6s %-12s %-12s %-15s %s\n", "UNIT", "LOAD", "ACTIVE", "SUB", "JOB", "DESCRIPTION");
232 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
233 const char *id, *description, *load_state, *active_state, *sub_state, *unit_state, *job_type, *job_path, *dot;
236 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
237 log_error("Failed to parse reply.");
242 dbus_message_iter_recurse(&sub, &sub2);
244 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
245 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &description, true) < 0 ||
246 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &load_state, true) < 0 ||
247 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &active_state, true) < 0 ||
248 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &sub_state, true) < 0 ||
249 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_state, true) < 0 ||
250 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &job_id, true) < 0 ||
251 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &job_type, true) < 0 ||
252 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, false) < 0) {
253 log_error("Failed to parse reply.");
258 if ((!arg_type || ((dot = strrchr(id, '.')) &&
259 streq(dot+1, arg_type))) &&
260 (arg_all || !streq(active_state, "inactive"))) {
264 printf("%-45s %-6s %-12s %-12s%n", id, load_state, active_state, sub_state, &a);
267 printf(" %-15s%n", job_type, &b);
271 if (a + b + 2 < columns()) {
275 printf("%.*s", columns() - a - b - 2, description);
282 dbus_message_iter_next(&sub);
286 printf("\n%u units listed.\n", k);
288 printf("\n%u live units listed. Pass --all to see dead units, too.\n", k);
294 dbus_message_unref(m);
297 dbus_message_unref(reply);
299 dbus_error_free(&error);
304 static int list_jobs(DBusConnection *bus, char **args, unsigned n) {
305 DBusMessage *m = NULL, *reply = NULL;
308 DBusMessageIter iter, sub, sub2;
311 dbus_error_init(&error);
315 if (!(m = dbus_message_new_method_call(
316 "org.freedesktop.systemd1",
317 "/org/freedesktop/systemd1",
318 "org.freedesktop.systemd1.Manager",
320 log_error("Could not allocate message.");
324 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
325 log_error("Failed to issue method call: %s", error.message);
330 if (!dbus_message_iter_init(reply, &iter) ||
331 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
332 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
333 log_error("Failed to parse reply.");
338 dbus_message_iter_recurse(&iter, &sub);
340 printf("%4s %-45s %-17s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
342 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
343 const char *name, *type, *state, *job_path, *unit_path;
346 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
347 log_error("Failed to parse reply.");
352 dbus_message_iter_recurse(&sub, &sub2);
354 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &id, true) < 0 ||
355 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0 ||
356 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
357 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, true) < 0 ||
358 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, true) < 0 ||
359 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, false) < 0) {
360 log_error("Failed to parse reply.");
365 printf("%4u %-45s %-17s %-7s\n", id, name, type, state);
368 dbus_message_iter_next(&sub);
371 printf("\n%u jobs listed.\n", k);
376 dbus_message_unref(m);
379 dbus_message_unref(reply);
381 dbus_error_free(&error);
386 static int load_unit(DBusConnection *bus, char **args, unsigned n) {
387 DBusMessage *m = NULL, *reply = NULL;
392 dbus_error_init(&error);
397 for (i = 1; i < n; i++) {
399 if (!(m = dbus_message_new_method_call(
400 "org.freedesktop.systemd1",
401 "/org/freedesktop/systemd1",
402 "org.freedesktop.systemd1.Manager",
404 log_error("Could not allocate message.");
409 if (!dbus_message_append_args(m,
410 DBUS_TYPE_STRING, &args[i],
411 DBUS_TYPE_INVALID)) {
412 log_error("Could not append arguments to message.");
417 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
418 log_error("Failed to issue method call: %s", error.message);
423 dbus_message_unref(m);
424 dbus_message_unref(reply);
433 dbus_message_unref(m);
436 dbus_message_unref(reply);
438 dbus_error_free(&error);
443 static int cancel_job(DBusConnection *bus, char **args, unsigned n) {
444 DBusMessage *m = NULL, *reply = NULL;
449 dbus_error_init(&error);
454 for (i = 1; i < n; i++) {
458 if (!(m = dbus_message_new_method_call(
459 "org.freedesktop.systemd1",
460 "/org/freedesktop/systemd1",
461 "org.freedesktop.systemd1.Manager",
463 log_error("Could not allocate message.");
468 if ((r = safe_atou(args[i], &id)) < 0) {
469 log_error("Failed to parse job id: %s", strerror(-r));
473 assert_cc(sizeof(uint32_t) == sizeof(id));
474 if (!dbus_message_append_args(m,
475 DBUS_TYPE_UINT32, &id,
476 DBUS_TYPE_INVALID)) {
477 log_error("Could not append arguments to message.");
482 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
483 log_error("Failed to issue method call: %s", error.message);
488 if (!dbus_message_get_args(reply, &error,
489 DBUS_TYPE_OBJECT_PATH, &path,
490 DBUS_TYPE_INVALID)) {
491 log_error("Failed to parse reply: %s", error.message);
496 dbus_message_unref(m);
497 if (!(m = dbus_message_new_method_call(
498 "org.freedesktop.systemd1",
500 "org.freedesktop.systemd1.Job",
502 log_error("Could not allocate message.");
507 dbus_message_unref(reply);
508 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
509 log_error("Failed to issue method call: %s", error.message);
514 dbus_message_unref(m);
515 dbus_message_unref(reply);
523 dbus_message_unref(m);
526 dbus_message_unref(reply);
528 dbus_error_free(&error);
533 typedef struct WaitData {
538 static DBusHandlerResult wait_filter(DBusConnection *connection, DBusMessage *message, void *data) {
546 dbus_error_init(&error);
548 /* log_debug("Got D-Bus request: %s.%s() on %s", */
549 /* dbus_message_get_interface(message), */
550 /* dbus_message_get_member(message), */
551 /* dbus_message_get_path(message)); */
553 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
554 log_error("Warning! D-Bus connection terminated.");
555 dbus_connection_close(connection);
557 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
560 dbus_bool_t success = true;
562 if (!dbus_message_get_args(message, &error,
563 DBUS_TYPE_UINT32, &id,
564 DBUS_TYPE_OBJECT_PATH, &path,
565 DBUS_TYPE_BOOLEAN, &success,
567 log_error("Failed to parse message: %s", error.message);
571 if ((p = set_remove(d->set, (char*) path)))
579 dbus_error_free(&error);
580 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
583 static int enable_wait_for_jobs(DBusConnection *bus) {
585 DBusMessage *m = NULL, *reply = NULL;
590 dbus_error_init(&error);
592 dbus_bus_add_match(bus,
594 "sender='org.freedesktop.systemd1',"
595 "interface='org.freedesktop.systemd1.Manager',"
596 "member='JobRemoved',"
597 "path='/org/freedesktop/systemd1'",
600 if (dbus_error_is_set(&error)) {
601 log_error("Failed to add match: %s", error.message);
606 if (!(m = dbus_message_new_method_call(
607 "org.freedesktop.systemd1",
608 "/org/freedesktop/systemd1",
609 "org.freedesktop.systemd1.Manager",
611 log_error("Could not allocate message.");
616 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
617 log_error("Failed to issue method call: %s", error.message);
625 /* This is slightly dirty, since we don't undo the match registrations. */
628 dbus_message_unref(m);
631 dbus_message_unref(reply);
633 dbus_error_free(&error);
638 static int wait_for_jobs(DBusConnection *bus, Set *s) {
649 if (!dbus_connection_add_filter(bus, wait_filter, &d, NULL)) {
650 log_error("Failed to add filter.");
655 while (!set_isempty(s) &&
656 dbus_connection_read_write_dispatch(bus, -1))
659 if (!arg_quiet && d.failed)
660 log_error("Job failed, see logs for details.");
662 r = d.failed ? -EIO : 0;
665 /* This is slightly dirty, since we don't undo the filter registration. */
670 static int start_unit_one(
677 DBusMessage *m = NULL, *reply = NULL;
685 assert(arg_no_block || s);
687 dbus_error_init(&error);
689 if (!(m = dbus_message_new_method_call(
690 "org.freedesktop.systemd1",
691 "/org/freedesktop/systemd1",
692 "org.freedesktop.systemd1.Manager",
694 log_error("Could not allocate message.");
699 if (!dbus_message_append_args(m,
700 DBUS_TYPE_STRING, &name,
701 DBUS_TYPE_STRING, &mode,
702 DBUS_TYPE_INVALID)) {
703 log_error("Could not append arguments to message.");
708 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
710 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
711 /* There's always a fallback possible for
717 log_error("Failed to issue method call: %s", error.message);
726 if (!dbus_message_get_args(reply, &error,
727 DBUS_TYPE_OBJECT_PATH, &path,
728 DBUS_TYPE_INVALID)) {
729 log_error("Failed to parse reply: %s", error.message);
734 if (!(p = strdup(path))) {
735 log_error("Failed to duplicate path.");
740 if ((r = set_put(s, p)) < 0) {
742 log_error("Failed to add path to set.");
751 dbus_message_unref(m);
754 dbus_message_unref(reply);
756 dbus_error_free(&error);
761 static enum action verb_to_action(const char *verb) {
762 if (streq(verb, "halt"))
764 else if (streq(verb, "poweroff"))
765 return ACTION_POWEROFF;
766 else if (streq(verb, "reboot"))
767 return ACTION_REBOOT;
768 else if (streq(verb, "rescue"))
769 return ACTION_RESCUE;
770 else if (streq(verb, "emergency"))
771 return ACTION_EMERGENCY;
772 else if (streq(verb, "default"))
773 return ACTION_DEFAULT;
775 return ACTION_INVALID;
778 static int start_unit(DBusConnection *bus, char **args, unsigned n) {
780 static const char * const table[_ACTION_MAX] = {
781 [ACTION_HALT] = SPECIAL_HALT_TARGET,
782 [ACTION_POWEROFF] = SPECIAL_POWEROFF_TARGET,
783 [ACTION_REBOOT] = SPECIAL_REBOOT_TARGET,
784 [ACTION_RUNLEVEL2] = SPECIAL_RUNLEVEL2_TARGET,
785 [ACTION_RUNLEVEL3] = SPECIAL_RUNLEVEL3_TARGET,
786 [ACTION_RUNLEVEL4] = SPECIAL_RUNLEVEL4_TARGET,
787 [ACTION_RUNLEVEL5] = SPECIAL_RUNLEVEL5_TARGET,
788 [ACTION_RESCUE] = SPECIAL_RESCUE_TARGET,
789 [ACTION_EMERGENCY] = SPECIAL_EMERGENCY_SERVICE,
790 [ACTION_DEFAULT] = SPECIAL_DEFAULT_TARGET
795 const char *method, *mode, *one_name;
800 if (arg_action == ACTION_SYSTEMCTL) {
802 streq(args[0], "stop") ? "StopUnit" :
803 streq(args[0], "reload") ? "ReloadUnit" :
804 streq(args[0], "restart") ? "RestartUnit" :
808 (streq(args[0], "isolate") ||
809 streq(args[0], "rescue") ||
810 streq(args[0], "emergency")) ? "isolate" :
811 arg_replace ? "replace" :
814 one_name = table[verb_to_action(args[0])];
817 assert(arg_action < ELEMENTSOF(table));
818 assert(table[arg_action]);
820 method = "StartUnit";
822 mode = (arg_action == ACTION_EMERGENCY ||
823 arg_action == ACTION_RESCUE) ? "isolate" : "replace";
825 one_name = table[arg_action];
829 if ((r = enable_wait_for_jobs(bus)) < 0) {
830 log_error("Could not watch jobs: %s", strerror(-r));
834 if (!(s = set_new(string_hash_func, string_compare_func))) {
835 log_error("Failed to allocate set.");
844 if ((r = start_unit_one(bus, method, one_name, mode, s)) <= 0)
847 for (i = 1; i < n; i++)
848 if ((r = start_unit_one(bus, method, args[i], mode, s)) < 0)
853 r = wait_for_jobs(bus, s);
862 static int start_special(DBusConnection *bus, char **args, unsigned n) {
866 warn_wall(verb_to_action(args[0]));
868 return start_unit(bus, args, n);
871 static int check_unit(DBusConnection *bus, char **args, unsigned n) {
872 DBusMessage *m = NULL, *reply = NULL;
874 *interface = "org.freedesktop.systemd1.Unit",
875 *property = "ActiveState";
876 int r = -EADDRNOTAVAIL;
883 dbus_error_init(&error);
885 for (i = 1; i < n; i++) {
886 const char *path = NULL;
888 DBusMessageIter iter, sub;
890 if (!(m = dbus_message_new_method_call(
891 "org.freedesktop.systemd1",
892 "/org/freedesktop/systemd1",
893 "org.freedesktop.systemd1.Manager",
895 log_error("Could not allocate message.");
900 if (!dbus_message_append_args(m,
901 DBUS_TYPE_STRING, &args[i],
902 DBUS_TYPE_INVALID)) {
903 log_error("Could not append arguments to message.");
908 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
910 /* Hmm, cannot figure out anything about this unit... */
917 if (!dbus_message_get_args(reply, &error,
918 DBUS_TYPE_OBJECT_PATH, &path,
919 DBUS_TYPE_INVALID)) {
920 log_error("Failed to parse reply: %s", error.message);
925 dbus_message_unref(m);
926 if (!(m = dbus_message_new_method_call(
927 "org.freedesktop.systemd1",
929 "org.freedesktop.DBus.Properties",
931 log_error("Could not allocate message.");
936 if (!dbus_message_append_args(m,
937 DBUS_TYPE_STRING, &interface,
938 DBUS_TYPE_STRING, &property,
939 DBUS_TYPE_INVALID)) {
940 log_error("Could not append arguments to message.");
945 dbus_message_unref(reply);
946 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
947 log_error("Failed to issue method call: %s", error.message);
952 if (!dbus_message_iter_init(reply, &iter) ||
953 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
954 log_error("Failed to parse reply.");
959 dbus_message_iter_recurse(&iter, &sub);
961 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
962 log_error("Failed to parse reply.");
967 dbus_message_iter_get_basic(&sub, &state);
972 if (streq(state, "active") || startswith(state, "reloading"))
975 dbus_message_unref(m);
976 dbus_message_unref(reply);
982 dbus_message_unref(m);
985 dbus_message_unref(reply);
987 dbus_error_free(&error);
993 static DBusHandlerResult monitor_filter(DBusConnection *connection, DBusMessage *message, void *data) {
995 DBusMessage *m = NULL, *reply = NULL;
1000 dbus_error_init(&error);
1002 /* log_debug("Got D-Bus request: %s.%s() on %s", */
1003 /* dbus_message_get_interface(message), */
1004 /* dbus_message_get_member(message), */
1005 /* dbus_message_get_path(message)); */
1007 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
1008 log_error("Warning! D-Bus connection terminated.");
1009 dbus_connection_close(connection);
1011 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitNew") ||
1012 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitRemoved")) {
1013 const char *id, *path;
1015 if (!dbus_message_get_args(message, &error,
1016 DBUS_TYPE_STRING, &id,
1017 DBUS_TYPE_OBJECT_PATH, &path,
1019 log_error("Failed to parse message: %s", error.message);
1020 else if (streq(dbus_message_get_member(message), "UnitNew"))
1021 printf("Unit %s added.\n", id);
1023 printf("Unit %s removed.\n", id);
1025 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobNew") ||
1026 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
1030 if (!dbus_message_get_args(message, &error,
1031 DBUS_TYPE_UINT32, &id,
1032 DBUS_TYPE_OBJECT_PATH, &path,
1034 log_error("Failed to parse message: %s", error.message);
1035 else if (streq(dbus_message_get_member(message), "JobNew"))
1036 printf("Job %u added.\n", id);
1038 printf("Job %u removed.\n", id);
1041 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Unit", "Changed") ||
1042 dbus_message_is_signal(message, "org.freedesktop.systemd1.Job", "Changed")) {
1044 const char *path, *interface, *property = "Id";
1045 DBusMessageIter iter, sub;
1047 path = dbus_message_get_path(message);
1048 interface = dbus_message_get_interface(message);
1050 if (!(m = dbus_message_new_method_call(
1051 "org.freedesktop.systemd1",
1053 "org.freedesktop.DBus.Properties",
1055 log_error("Could not allocate message.");
1059 if (!dbus_message_append_args(m,
1060 DBUS_TYPE_STRING, &interface,
1061 DBUS_TYPE_STRING, &property,
1062 DBUS_TYPE_INVALID)) {
1063 log_error("Could not append arguments to message.");
1067 if (!(reply = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) {
1068 log_error("Failed to issue method call: %s", error.message);
1072 if (!dbus_message_iter_init(reply, &iter) ||
1073 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1074 log_error("Failed to parse reply.");
1078 dbus_message_iter_recurse(&iter, &sub);
1080 if (streq(interface, "org.freedesktop.systemd1.Unit")) {
1083 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1084 log_error("Failed to parse reply.");
1088 dbus_message_iter_get_basic(&sub, &id);
1089 printf("Unit %s changed.\n", id);
1093 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT32) {
1094 log_error("Failed to parse reply.");
1098 dbus_message_iter_get_basic(&sub, &id);
1099 printf("Job %u changed.\n", id);
1105 dbus_message_unref(m);
1108 dbus_message_unref(reply);
1110 dbus_error_free(&error);
1111 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1115 dbus_message_unref(m);
1118 dbus_message_unref(reply);
1120 dbus_error_free(&error);
1121 return DBUS_HANDLER_RESULT_NEED_MEMORY;
1124 static int monitor(DBusConnection *bus, char **args, unsigned n) {
1125 DBusMessage *m = NULL, *reply = NULL;
1129 dbus_error_init(&error);
1131 dbus_bus_add_match(bus,
1133 "sender='org.freedesktop.systemd1',"
1134 "interface='org.freedesktop.systemd1.Manager',"
1135 "path='/org/freedesktop/systemd1'",
1138 if (dbus_error_is_set(&error)) {
1139 log_error("Failed to add match: %s", error.message);
1144 dbus_bus_add_match(bus,
1146 "sender='org.freedesktop.systemd1',"
1147 "interface='org.freedesktop.systemd1.Unit',"
1151 if (dbus_error_is_set(&error)) {
1152 log_error("Failed to add match: %s", error.message);
1157 dbus_bus_add_match(bus,
1159 "sender='org.freedesktop.systemd1',"
1160 "interface='org.freedesktop.systemd1.Job',"
1164 if (dbus_error_is_set(&error)) {
1165 log_error("Failed to add match: %s", error.message);
1170 if (!dbus_connection_add_filter(bus, monitor_filter, NULL, NULL)) {
1171 log_error("Failed to add filter.");
1176 if (!(m = dbus_message_new_method_call(
1177 "org.freedesktop.systemd1",
1178 "/org/freedesktop/systemd1",
1179 "org.freedesktop.systemd1.Manager",
1181 log_error("Could not allocate message.");
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 while (dbus_connection_read_write_dispatch(bus, -1))
1199 /* This is slightly dirty, since we don't undo the filter or the matches. */
1202 dbus_message_unref(m);
1205 dbus_message_unref(reply);
1207 dbus_error_free(&error);
1212 static int dump(DBusConnection *bus, char **args, unsigned n) {
1213 DBusMessage *m = NULL, *reply = NULL;
1218 dbus_error_init(&error);
1220 if (!(m = dbus_message_new_method_call(
1221 "org.freedesktop.systemd1",
1222 "/org/freedesktop/systemd1",
1223 "org.freedesktop.systemd1.Manager",
1225 log_error("Could not allocate message.");
1229 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1230 log_error("Failed to issue method call: %s", error.message);
1235 if (!dbus_message_get_args(reply, &error,
1236 DBUS_TYPE_STRING, &text,
1237 DBUS_TYPE_INVALID)) {
1238 log_error("Failed to parse reply: %s", error.message);
1243 fputs(text, stdout);
1249 dbus_message_unref(m);
1252 dbus_message_unref(reply);
1254 dbus_error_free(&error);
1259 static int snapshot(DBusConnection *bus, char **args, unsigned n) {
1260 DBusMessage *m = NULL, *reply = NULL;
1263 const char *name = "", *path, *id;
1264 dbus_bool_t cleanup = FALSE;
1265 DBusMessageIter iter, sub;
1267 *interface = "org.freedesktop.systemd1.Unit",
1270 dbus_error_init(&error);
1272 if (!(m = dbus_message_new_method_call(
1273 "org.freedesktop.systemd1",
1274 "/org/freedesktop/systemd1",
1275 "org.freedesktop.systemd1.Manager",
1276 "CreateSnapshot"))) {
1277 log_error("Could not allocate message.");
1284 if (!dbus_message_append_args(m,
1285 DBUS_TYPE_STRING, &name,
1286 DBUS_TYPE_BOOLEAN, &cleanup,
1287 DBUS_TYPE_INVALID)) {
1288 log_error("Could not append arguments to message.");
1293 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1294 log_error("Failed to issue method call: %s", error.message);
1299 if (!dbus_message_get_args(reply, &error,
1300 DBUS_TYPE_OBJECT_PATH, &path,
1301 DBUS_TYPE_INVALID)) {
1302 log_error("Failed to parse reply: %s", error.message);
1307 dbus_message_unref(m);
1308 if (!(m = dbus_message_new_method_call(
1309 "org.freedesktop.systemd1",
1311 "org.freedesktop.DBus.Properties",
1313 log_error("Could not allocate message.");
1317 if (!dbus_message_append_args(m,
1318 DBUS_TYPE_STRING, &interface,
1319 DBUS_TYPE_STRING, &property,
1320 DBUS_TYPE_INVALID)) {
1321 log_error("Could not append arguments to message.");
1326 dbus_message_unref(reply);
1327 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1328 log_error("Failed to issue method call: %s", error.message);
1333 if (!dbus_message_iter_init(reply, &iter) ||
1334 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1335 log_error("Failed to parse reply.");
1340 dbus_message_iter_recurse(&iter, &sub);
1342 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1343 log_error("Failed to parse reply.");
1348 dbus_message_iter_get_basic(&sub, &id);
1356 dbus_message_unref(m);
1359 dbus_message_unref(reply);
1361 dbus_error_free(&error);
1366 static int delete_snapshot(DBusConnection *bus, char **args, unsigned n) {
1367 DBusMessage *m = NULL, *reply = NULL;
1375 dbus_error_init(&error);
1377 for (i = 1; i < n; i++) {
1378 const char *path = NULL;
1380 if (!(m = dbus_message_new_method_call(
1381 "org.freedesktop.systemd1",
1382 "/org/freedesktop/systemd1",
1383 "org.freedesktop.systemd1.Manager",
1385 log_error("Could not allocate message.");
1390 if (!dbus_message_append_args(m,
1391 DBUS_TYPE_STRING, &args[i],
1392 DBUS_TYPE_INVALID)) {
1393 log_error("Could not append arguments to message.");
1398 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1399 log_error("Failed to issue method call: %s", error.message);
1404 if (!dbus_message_get_args(reply, &error,
1405 DBUS_TYPE_OBJECT_PATH, &path,
1406 DBUS_TYPE_INVALID)) {
1407 log_error("Failed to parse reply: %s", error.message);
1412 dbus_message_unref(m);
1413 if (!(m = dbus_message_new_method_call(
1414 "org.freedesktop.systemd1",
1416 "org.freedesktop.systemd1.Snapshot",
1418 log_error("Could not allocate message.");
1423 dbus_message_unref(reply);
1424 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1425 log_error("Failed to issue method call: %s", error.message);
1430 dbus_message_unref(m);
1431 dbus_message_unref(reply);
1439 dbus_message_unref(m);
1442 dbus_message_unref(reply);
1444 dbus_error_free(&error);
1449 static int clear_jobs(DBusConnection *bus, char **args, unsigned n) {
1450 DBusMessage *m = NULL, *reply = NULL;
1455 dbus_error_init(&error);
1457 if (arg_action == ACTION_RELOAD)
1459 else if (arg_action == ACTION_REEXEC)
1460 method = "Reexecute";
1462 assert(arg_action == ACTION_SYSTEMCTL);
1465 streq(args[0], "clear-jobs") ? "ClearJobs" :
1466 streq(args[0], "daemon-reload") ? "Reload" :
1467 streq(args[0], "daemon-reexec") ? "Reexecute" :
1471 if (!(m = dbus_message_new_method_call(
1472 "org.freedesktop.systemd1",
1473 "/org/freedesktop/systemd1",
1474 "org.freedesktop.systemd1.Manager",
1476 log_error("Could not allocate message.");
1480 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1482 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
1483 /* There's always a fallback possible for
1484 * legacy actions. */
1489 log_error("Failed to issue method call: %s", error.message);
1498 dbus_message_unref(m);
1501 dbus_message_unref(reply);
1503 dbus_error_free(&error);
1508 static int show_enviroment(DBusConnection *bus, char **args, unsigned n) {
1509 DBusMessage *m = NULL, *reply = NULL;
1511 DBusMessageIter iter, sub, sub2;
1514 *interface = "org.freedesktop.systemd1.Manager",
1515 *property = "Environment";
1517 dbus_error_init(&error);
1519 if (!(m = dbus_message_new_method_call(
1520 "org.freedesktop.systemd1",
1521 "/org/freedesktop/systemd1",
1522 "org.freedesktop.DBus.Properties",
1524 log_error("Could not allocate message.");
1528 if (!dbus_message_append_args(m,
1529 DBUS_TYPE_STRING, &interface,
1530 DBUS_TYPE_STRING, &property,
1531 DBUS_TYPE_INVALID)) {
1532 log_error("Could not append arguments to message.");
1537 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1538 log_error("Failed to issue method call: %s", error.message);
1543 if (!dbus_message_iter_init(reply, &iter) ||
1544 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1545 log_error("Failed to parse reply.");
1550 dbus_message_iter_recurse(&iter, &sub);
1552 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
1553 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
1554 log_error("Failed to parse reply.");
1559 dbus_message_iter_recurse(&sub, &sub2);
1561 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
1564 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
1565 log_error("Failed to parse reply.");
1570 dbus_message_iter_get_basic(&sub2, &text);
1571 printf("%s\n", text);
1573 dbus_message_iter_next(&sub2);
1580 dbus_message_unref(m);
1583 dbus_message_unref(reply);
1585 dbus_error_free(&error);
1590 static int set_environment(DBusConnection *bus, char **args, unsigned n) {
1591 DBusMessage *m = NULL, *reply = NULL;
1595 DBusMessageIter iter, sub;
1598 dbus_error_init(&error);
1600 method = streq(args[0], "set-environment")
1602 : "UnsetEnvironment";
1604 if (!(m = dbus_message_new_method_call(
1605 "org.freedesktop.systemd1",
1606 "/org/freedesktop/systemd1",
1607 "org.freedesktop.systemd1.Manager",
1610 log_error("Could not allocate message.");
1614 dbus_message_iter_init_append(m, &iter);
1616 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
1617 log_error("Could not append arguments to message.");
1622 for (i = 1; i < n; i++)
1623 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &args[i])) {
1624 log_error("Could not append arguments to message.");
1629 if (!dbus_message_iter_close_container(&iter, &sub)) {
1630 log_error("Could not append arguments to message.");
1635 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1636 log_error("Failed to issue method call: %s", error.message);
1645 dbus_message_unref(m);
1648 dbus_message_unref(reply);
1650 dbus_error_free(&error);
1655 static int systemctl_help(void) {
1657 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
1658 "Send control commands to the systemd manager.\n\n"
1659 " -h --help Show this help\n"
1660 " -t --type=TYPE List only units of a particular type\n"
1661 " -a --all Show all units, including dead ones\n"
1662 " --replace When installing a new job, replace existing conflicting ones\n"
1663 " --system Connect to system bus\n"
1664 " --session Connect to session bus\n"
1665 " -q --quiet Suppress output\n"
1666 " --no-block Do not wait until operation finished\n"
1667 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
1669 " list-units List units\n"
1670 " list-jobs List jobs\n"
1671 " clear-jobs Cancel all jobs\n"
1672 " load [NAME...] Load one or more units\n"
1673 " cancel [JOB...] Cancel one or more jobs\n"
1674 " start [NAME...] Start one or more units\n"
1675 " stop [NAME...] Stop one or more units\n"
1676 " restart [NAME...] Restart one or more units\n"
1677 " reload [NAME...] Reload one or more units\n"
1678 " isolate [NAME] Start one unit and stop all others\n"
1679 " check [NAME...] Check whether any of the passed units are active\n"
1680 " monitor Monitor unit/job changes\n"
1681 " dump Dump server status\n"
1682 " snapshot [NAME] Create a snapshot\n"
1683 " delete [NAME...] Remove one or more snapshots\n"
1684 " daemon-reload Reload systemd manager configuration\n"
1685 " daemon-reexec Reexecute systemd manager\n"
1686 " daemon-exit Ask the systemd manager to quit\n"
1687 " show-environment Dump environment\n"
1688 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
1689 " unset-environment [NAME...] Unset one or more environment variables\n"
1690 " halt Shut down and halt the system\n"
1691 " poweroff Shut down and power-off the system\n"
1692 " reboot Shut down and reboot the system\n"
1693 " default Enter default mode\n"
1694 " rescue Enter rescue mode\n"
1695 " emergency Enter emergency mode\n",
1696 program_invocation_short_name);
1701 static int halt_help(void) {
1703 printf("%s [OPTIONS...]\n\n"
1704 "%s the system.\n\n"
1705 " --help Show this help\n"
1706 " --halt Halt the machine\n"
1707 " -p --poweroff Switch off the machine\n"
1708 " --reboot Reboot the machine\n"
1709 " -f --force Force immediate halt/power-off/reboot\n"
1710 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
1711 " -d --no-wtmp Don't write wtmp record\n"
1712 " -n --no-sync Don't sync before halt/power-off/reboot\n"
1713 " --no-wall Don't send wall message before halt/power-off/reboot\n",
1714 program_invocation_short_name,
1715 arg_action == ACTION_REBOOT ? "Reboot" :
1716 arg_action == ACTION_POWEROFF ? "Power off" :
1722 static int shutdown_help(void) {
1724 printf("%s [OPTIONS...] [now] [WALL...]\n\n"
1725 "Shut down the system.\n\n"
1726 " --help Show this help\n"
1727 " -H --halt Halt the machine\n"
1728 " -P --poweroff Power-off the machine\n"
1729 " -r --reboot Reboot the machine\n"
1730 " -h Equivalent to --poweroff, overriden by --halt\n"
1731 " -k Don't halt/power-off/reboot, just send warnings\n"
1732 " --no-wall Don't send wall message before halt/power-off/reboot\n",
1733 program_invocation_short_name);
1738 static int telinit_help(void) {
1740 printf("%s [OPTIONS...] {COMMAND}\n\n"
1741 "Send control commands to the init daemon.\n\n"
1742 " --help Show this help\n"
1743 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
1745 " 0 Power-off the machine\n"
1746 " 6 Reboot the machine\n"
1747 " 2, 3, 4, 5 Start runlevelX.target unit\n"
1748 " 1, s, S Enter rescue mode\n"
1749 " q, Q Reload init daemon configuration\n"
1750 " u, U Reexecute init daemon\n",
1751 program_invocation_short_name);
1756 static int runlevel_help(void) {
1758 printf("%s [OPTIONS...]\n\n"
1759 "Prints the previous and current runlevel of the init system.\n\n"
1760 " --help Show this help\n",
1761 program_invocation_short_name);
1766 static int systemctl_parse_argv(int argc, char *argv[]) {
1769 ARG_REPLACE = 0x100,
1776 static const struct option options[] = {
1777 { "help", no_argument, NULL, 'h' },
1778 { "type", required_argument, NULL, 't' },
1779 { "all", no_argument, NULL, 'a' },
1780 { "replace", no_argument, NULL, ARG_REPLACE },
1781 { "session", no_argument, NULL, ARG_SESSION },
1782 { "system", no_argument, NULL, ARG_SYSTEM },
1783 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
1784 { "no-wall", no_argument, NULL, ARG_NO_WALL },
1785 { "quiet", no_argument, NULL, 'q' },
1786 { NULL, 0, NULL, 0 }
1794 while ((c = getopt_long(argc, argv, "htaq", options, NULL)) >= 0) {
1819 arg_session = false;
1823 arg_no_block = true;
1838 log_error("Unknown option code %c", c);
1846 static int halt_parse_argv(int argc, char *argv[]) {
1855 static const struct option options[] = {
1856 { "help", no_argument, NULL, ARG_HELP },
1857 { "halt", no_argument, NULL, ARG_HALT },
1858 { "poweroff", no_argument, NULL, 'p' },
1859 { "reboot", no_argument, NULL, ARG_REBOOT },
1860 { "force", no_argument, NULL, 'f' },
1861 { "wtmp-only", no_argument, NULL, 'w' },
1862 { "no-wtmp", no_argument, NULL, 'd' },
1863 { "no-sync", no_argument, NULL, 'n' },
1864 { "no-wall", no_argument, NULL, ARG_NO_WALL },
1865 { NULL, 0, NULL, 0 }
1873 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
1874 if (runlevel == '0' || runlevel == '6')
1875 arg_immediate = true;
1877 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
1885 arg_action = ACTION_HALT;
1889 arg_action = ACTION_POWEROFF;
1893 arg_action = ACTION_REBOOT;
1897 arg_immediate = true;
1918 /* Compatibility nops */
1925 log_error("Unknown option code %c", c);
1930 if (optind < argc) {
1931 log_error("Too many arguments.");
1938 static int shutdown_parse_argv(int argc, char *argv[]) {
1945 static const struct option options[] = {
1946 { "help", no_argument, NULL, ARG_HELP },
1947 { "halt", no_argument, NULL, 'H' },
1948 { "poweroff", no_argument, NULL, 'P' },
1949 { "reboot", no_argument, NULL, 'r' },
1950 { "no-wall", no_argument, NULL, ARG_NO_WALL },
1951 { NULL, 0, NULL, 0 }
1959 while ((c = getopt_long(argc, argv, "HPrhkt:a", options, NULL)) >= 0) {
1967 arg_action = ACTION_HALT;
1971 arg_action = ACTION_POWEROFF;
1975 arg_action = ACTION_REBOOT;
1979 if (arg_action != ACTION_HALT)
1980 arg_action = ACTION_POWEROFF;
1993 /* Compatibility nops */
2000 log_error("Unknown option code %c", c);
2005 if (argc > optind && !streq(argv[optind], "now"))
2006 log_warning("First argument '%s' isn't 'now'. Ignoring.", argv[optind]);
2008 /* We ignore the time argument */
2009 if (argc > optind + 1)
2010 arg_wall = argv + optind + 1;
2017 static int telinit_parse_argv(int argc, char *argv[]) {
2024 static const struct option options[] = {
2025 { "help", no_argument, NULL, ARG_HELP },
2026 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2027 { NULL, 0, NULL, 0 }
2030 static const struct {
2034 { '0', ACTION_POWEROFF },
2035 { '6', ACTION_REBOOT },
2036 { '1', ACTION_RESCUE },
2037 { '2', ACTION_RUNLEVEL2 },
2038 { '3', ACTION_RUNLEVEL3 },
2039 { '4', ACTION_RUNLEVEL4 },
2040 { '5', ACTION_RUNLEVEL5 },
2041 { 's', ACTION_RESCUE },
2042 { 'S', ACTION_RESCUE },
2043 { 'q', ACTION_RELOAD },
2044 { 'Q', ACTION_RELOAD },
2045 { 'u', ACTION_REEXEC },
2046 { 'U', ACTION_REEXEC }
2055 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
2070 log_error("Unknown option code %c", c);
2075 if (optind >= argc) {
2080 if (optind + 1 < argc) {
2081 log_error("Too many arguments.");
2085 if (strlen(argv[optind]) != 1) {
2086 log_error("Expected single character argument.");
2090 for (i = 0; i < ELEMENTSOF(table); i++)
2091 if (table[i].from == argv[optind][0])
2094 if (i >= ELEMENTSOF(table)) {
2095 log_error("Unknown command %s.", argv[optind]);
2099 arg_action = table[i].to;
2106 static int runlevel_parse_argv(int argc, char *argv[]) {
2112 static const struct option options[] = {
2113 { "help", no_argument, NULL, ARG_HELP },
2114 { NULL, 0, NULL, 0 }
2122 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
2133 log_error("Unknown option code %c", c);
2138 if (optind < argc) {
2139 log_error("Too many arguments.");
2146 static int parse_argv(int argc, char *argv[]) {
2150 if (program_invocation_short_name) {
2152 if (strstr(program_invocation_short_name, "halt")) {
2153 arg_action = ACTION_HALT;
2154 return halt_parse_argv(argc, argv);
2155 } else if (strstr(program_invocation_short_name, "poweroff")) {
2156 arg_action = ACTION_POWEROFF;
2157 return halt_parse_argv(argc, argv);
2158 } else if (strstr(program_invocation_short_name, "reboot")) {
2159 arg_action = ACTION_REBOOT;
2160 return halt_parse_argv(argc, argv);
2161 } else if (strstr(program_invocation_short_name, "shutdown")) {
2162 arg_action = ACTION_POWEROFF;
2163 return shutdown_parse_argv(argc, argv);
2164 } else if (strstr(program_invocation_short_name, "init")) {
2165 arg_action = ACTION_INVALID;
2166 return telinit_parse_argv(argc, argv);
2167 } else if (strstr(program_invocation_short_name, "runlevel")) {
2168 arg_action = ACTION_RUNLEVEL;
2169 return runlevel_parse_argv(argc, argv);
2173 arg_action = ACTION_SYSTEMCTL;
2174 return systemctl_parse_argv(argc, argv);
2177 static int action_to_runlevel(void) {
2179 static const char table[_ACTION_MAX] = {
2180 [ACTION_HALT] = '0',
2181 [ACTION_POWEROFF] = '0',
2182 [ACTION_REBOOT] = '6',
2183 [ACTION_RUNLEVEL2] = '2',
2184 [ACTION_RUNLEVEL3] = '3',
2185 [ACTION_RUNLEVEL4] = '4',
2186 [ACTION_RUNLEVEL5] = '5',
2187 [ACTION_RESCUE] = '1'
2190 assert(arg_action < _ACTION_MAX);
2192 return table[arg_action];
2195 static int talk_upstart(void) {
2196 DBusMessage *m = NULL, *reply = NULL;
2198 int previous, rl, r;
2200 env1_buf[] = "RUNLEVEL=X",
2201 env2_buf[] = "PREVLEVEL=X";
2202 char *env1 = env1_buf, *env2 = env2_buf;
2203 const char *emit = "runlevel";
2204 dbus_bool_t b_false = FALSE;
2205 DBusMessageIter iter, sub;
2206 DBusConnection *bus;
2208 dbus_error_init(&error);
2210 if (!(rl = action_to_runlevel()))
2213 if (utmp_get_runlevel(&previous, NULL) < 0)
2216 if (!(bus = dbus_connection_open("unix:abstract=/com/ubuntu/upstart", &error))) {
2217 if (dbus_error_has_name(&error, DBUS_ERROR_NO_SERVER)) {
2222 log_error("Failed to connect to Upstart bus: %s", error.message);
2227 if ((r = bus_check_peercred(bus)) < 0) {
2228 log_error("Failed to verify owner of bus.");
2232 if (!(m = dbus_message_new_method_call(
2233 "com.ubuntu.Upstart",
2234 "/com/ubuntu/Upstart",
2235 "com.ubuntu.Upstart0_6",
2238 log_error("Could not allocate message.");
2243 dbus_message_iter_init_append(m, &iter);
2245 env1_buf[sizeof(env1_buf)-2] = rl;
2246 env2_buf[sizeof(env2_buf)-2] = previous;
2248 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
2249 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
2250 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
2251 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
2252 !dbus_message_iter_close_container(&iter, &sub) ||
2253 !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
2254 log_error("Could not append arguments to message.");
2259 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2261 if (error_is_no_service(&error)) {
2266 log_error("Failed to issue method call: %s", error.message);
2275 dbus_message_unref(m);
2278 dbus_message_unref(reply);
2281 dbus_connection_unref(bus);
2283 dbus_error_free(&error);
2288 static int talk_initctl(void) {
2289 struct init_request request;
2293 if (!(rl = action_to_runlevel()))
2297 request.magic = INIT_MAGIC;
2298 request.sleeptime = 0;
2299 request.cmd = INIT_CMD_RUNLVL;
2300 request.runlevel = rl;
2302 if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
2304 if (errno == ENOENT)
2307 log_error("Failed to open "INIT_FIFO": %m");
2312 r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
2313 close_nointr_nofail(fd);
2316 log_error("Failed to write to "INIT_FIFO": %m");
2317 return errno ? -errno : -EIO;
2323 static int systemctl_main(DBusConnection *bus, int argc, char *argv[]) {
2325 static const struct {
2333 int (* const dispatch)(DBusConnection *bus, char **args, unsigned n);
2335 { "list-units", LESS, 1, list_units },
2336 { "list-jobs", EQUAL, 1, list_jobs },
2337 { "clear-jobs", EQUAL, 1, clear_jobs },
2338 { "load", MORE, 2, load_unit },
2339 { "cancel", MORE, 2, cancel_job },
2340 { "start", MORE, 2, start_unit },
2341 { "stop", MORE, 2, start_unit },
2342 { "reload", MORE, 2, start_unit },
2343 { "restart", MORE, 2, start_unit },
2344 { "isolate", EQUAL, 2, start_unit },
2345 { "check", MORE, 2, check_unit },
2346 { "monitor", EQUAL, 1, monitor },
2347 { "dump", EQUAL, 1, dump },
2348 { "snapshot", LESS, 2, snapshot },
2349 { "delete", MORE, 2, delete_snapshot },
2350 { "daemon-reload", EQUAL, 1, clear_jobs },
2351 { "daemon-reexec", EQUAL, 1, clear_jobs },
2352 { "daemon-exit", EQUAL, 1, clear_jobs },
2353 { "show-environment", EQUAL, 1, show_enviroment },
2354 { "set-environment", MORE, 2, set_environment },
2355 { "unset-environment", MORE, 2, set_environment },
2356 { "halt", EQUAL, 1, start_special },
2357 { "poweroff", EQUAL, 1, start_special },
2358 { "reboot", EQUAL, 1, start_special },
2359 { "default", EQUAL, 1, start_special },
2360 { "rescue", EQUAL, 1, start_special },
2361 { "emergency", EQUAL, 1, start_special },
2371 left = argc - optind;
2374 /* Special rule: no arguments means "list-units" */
2377 if (streq(argv[optind], "help")) {
2382 for (i = 0; i < ELEMENTSOF(verbs); i++)
2383 if (streq(argv[optind], verbs[i].verb))
2386 if (i >= ELEMENTSOF(verbs)) {
2387 log_error("Unknown operation %s", argv[optind]);
2392 switch (verbs[i].argc_cmp) {
2395 if (left != verbs[i].argc) {
2396 log_error("Invalid number of arguments.");
2403 if (left < verbs[i].argc) {
2404 log_error("Too few arguments.");
2411 if (left > verbs[i].argc) {
2412 log_error("Too many arguments.");
2419 assert_not_reached("Unknown comparison operator.");
2422 return verbs[i].dispatch(bus, argv + optind, left);
2425 static int reload_with_fallback(DBusConnection *bus) {
2429 /* First, try systemd via D-Bus. */
2430 if ((r = clear_jobs(bus, NULL, 0)) > 0)
2434 /* Nothing else worked, so let's try signals */
2435 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
2437 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
2438 log_error("kill() failed: %m");
2445 static int start_with_fallback(DBusConnection *bus) {
2448 warn_wall(arg_action);
2451 /* First, try systemd via D-Bus. */
2452 if ((r = start_unit(bus, NULL, 0)) > 0)
2455 /* Hmm, talking to systemd via D-Bus didn't work. Then
2456 * let's try to talk to Upstart via D-Bus. */
2457 if ((r = talk_upstart()) > 0)
2461 /* Nothing else worked, so let's try
2463 if ((r = talk_initctl()) != 0)
2466 log_error("Failed to talk to init daemon.");
2470 static int halt_main(DBusConnection *bus) {
2474 return start_with_fallback(bus);
2477 if ((r = utmp_put_shutdown(0)) < 0)
2478 log_warning("Failed to write utmp record: %s", strerror(-r));
2486 /* Make sure C-A-D is handled by the kernel from this
2488 reboot(RB_ENABLE_CAD);
2490 switch (arg_action) {
2493 log_info("Halting");
2494 reboot(RB_HALT_SYSTEM);
2497 case ACTION_POWEROFF:
2498 log_info("Powering off");
2499 reboot(RB_POWER_OFF);
2503 log_info("Rebooting");
2504 reboot(RB_AUTOBOOT);
2508 assert_not_reached("Unknown halt action.");
2511 /* We should never reach this. */
2515 static int runlevel_main(void) {
2516 int r, runlevel, previous;
2518 if ((r = utmp_get_runlevel(&runlevel, &previous)) < 0) {
2524 previous <= 0 ? 'N' : previous,
2525 runlevel <= 0 ? 'N' : runlevel);
2530 int main(int argc, char*argv[]) {
2532 DBusConnection *bus = NULL;
2535 dbus_error_init(&error);
2537 log_parse_environment();
2539 if ((r = parse_argv(argc, argv)) < 0)
2546 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
2547 * let's shortcut this */
2548 if (arg_action == ACTION_RUNLEVEL) {
2549 retval = runlevel_main() < 0;
2553 /* If we are root, then let's not go via the bus */
2554 if (geteuid() == 0 && !arg_session) {
2555 bus = dbus_connection_open("unix:abstract=/org/freedesktop/systemd1/private", &error);
2557 if (bus && bus_check_peercred(bus) < 0) {
2558 log_error("Failed to verify owner of bus.");
2562 bus = dbus_bus_get(arg_session ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &error);
2565 dbus_connection_set_exit_on_disconnect(bus, FALSE);
2567 switch (arg_action) {
2569 case ACTION_SYSTEMCTL: {
2572 log_error("Failed to get D-Bus connection: %s", error.message);
2576 retval = systemctl_main(bus, argc, argv) < 0;
2581 case ACTION_POWEROFF:
2583 retval = halt_main(bus) < 0;
2586 case ACTION_RUNLEVEL2:
2587 case ACTION_RUNLEVEL3:
2588 case ACTION_RUNLEVEL4:
2589 case ACTION_RUNLEVEL5:
2591 case ACTION_EMERGENCY:
2592 case ACTION_DEFAULT:
2593 retval = start_with_fallback(bus) < 0;
2598 retval = reload_with_fallback(bus) < 0;
2601 case ACTION_INVALID:
2602 case ACTION_RUNLEVEL:
2604 assert_not_reached("Unknown action");
2610 dbus_connection_unref(bus);
2612 dbus_error_free(&error);