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 static DBusHandlerResult wait_filter(DBusConnection *connection, DBusMessage *message, void *data) {
541 dbus_error_init(&error);
543 /* log_debug("Got D-Bus request: %s.%s() on %s", */
544 /* dbus_message_get_interface(message), */
545 /* dbus_message_get_member(message), */
546 /* dbus_message_get_path(message)); */
548 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
549 log_error("Warning! D-Bus connection terminated.");
550 dbus_connection_close(connection);
552 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
556 if (!dbus_message_get_args(message, &error,
557 DBUS_TYPE_UINT32, &id,
558 DBUS_TYPE_OBJECT_PATH, &path,
560 log_error("Failed to parse message: %s", error.message);
564 if ((p = set_remove(s, (char*) path)))
569 dbus_error_free(&error);
570 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
573 static int enable_wait_for_jobs(DBusConnection *bus) {
575 DBusMessage *m = NULL, *reply = NULL;
580 dbus_error_init(&error);
582 dbus_bus_add_match(bus,
584 "sender='org.freedesktop.systemd1',"
585 "interface='org.freedesktop.systemd1.Manager',"
586 "member='JobRemoved',"
587 "path='/org/freedesktop/systemd1'",
590 if (dbus_error_is_set(&error)) {
591 log_error("Failed to add match: %s", error.message);
596 if (!(m = dbus_message_new_method_call(
597 "org.freedesktop.systemd1",
598 "/org/freedesktop/systemd1",
599 "org.freedesktop.systemd1.Manager",
601 log_error("Could not allocate message.");
606 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
607 log_error("Failed to issue method call: %s", error.message);
615 /* This is slightly dirty, since we don't undo the match registrations. */
618 dbus_message_unref(m);
621 dbus_message_unref(reply);
623 dbus_error_free(&error);
628 static int wait_for_jobs(DBusConnection *bus, Set *s) {
634 if (!dbus_connection_add_filter(bus, wait_filter, s, NULL)) {
635 log_error("Failed to add filter.");
640 while (!set_isempty(s) &&
641 dbus_connection_read_write_dispatch(bus, -1))
647 /* This is slightly dirty, since we don't undo the filter registration. */
652 static int start_unit_one(
659 DBusMessage *m = NULL, *reply = NULL;
667 assert(arg_no_block || s);
669 dbus_error_init(&error);
671 if (!(m = dbus_message_new_method_call(
672 "org.freedesktop.systemd1",
673 "/org/freedesktop/systemd1",
674 "org.freedesktop.systemd1.Manager",
676 log_error("Could not allocate message.");
681 if (!dbus_message_append_args(m,
682 DBUS_TYPE_STRING, &name,
683 DBUS_TYPE_STRING, &mode,
684 DBUS_TYPE_INVALID)) {
685 log_error("Could not append arguments to message.");
690 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
692 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
693 /* There's always a fallback possible for
699 log_error("Failed to issue method call: %s", error.message);
708 if (!dbus_message_get_args(reply, &error,
709 DBUS_TYPE_OBJECT_PATH, &path,
710 DBUS_TYPE_INVALID)) {
711 log_error("Failed to parse reply: %s", error.message);
716 if (!(p = strdup(path))) {
717 log_error("Failed to duplicate path.");
722 if ((r = set_put(s, p)) < 0) {
724 log_error("Failed to add path to set.");
733 dbus_message_unref(m);
736 dbus_message_unref(reply);
738 dbus_error_free(&error);
743 static enum action verb_to_action(const char *verb) {
744 if (streq(verb, "halt"))
746 else if (streq(verb, "poweroff"))
747 return ACTION_POWEROFF;
748 else if (streq(verb, "reboot"))
749 return ACTION_REBOOT;
750 else if (streq(verb, "rescue"))
751 return ACTION_RESCUE;
752 else if (streq(verb, "emergency"))
753 return ACTION_EMERGENCY;
754 else if (streq(verb, "default"))
755 return ACTION_DEFAULT;
757 return ACTION_INVALID;
760 static int start_unit(DBusConnection *bus, char **args, unsigned n) {
762 static const char * const table[_ACTION_MAX] = {
763 [ACTION_HALT] = SPECIAL_HALT_TARGET,
764 [ACTION_POWEROFF] = SPECIAL_POWEROFF_TARGET,
765 [ACTION_REBOOT] = SPECIAL_REBOOT_TARGET,
766 [ACTION_RUNLEVEL2] = SPECIAL_RUNLEVEL2_TARGET,
767 [ACTION_RUNLEVEL3] = SPECIAL_RUNLEVEL3_TARGET,
768 [ACTION_RUNLEVEL4] = SPECIAL_RUNLEVEL4_TARGET,
769 [ACTION_RUNLEVEL5] = SPECIAL_RUNLEVEL5_TARGET,
770 [ACTION_RESCUE] = SPECIAL_RESCUE_TARGET,
771 [ACTION_EMERGENCY] = SPECIAL_EMERGENCY_SERVICE,
772 [ACTION_DEFAULT] = SPECIAL_DEFAULT_TARGET
777 const char *method, *mode, *one_name;
782 if (arg_action == ACTION_SYSTEMCTL) {
784 streq(args[0], "stop") ? "StopUnit" :
785 streq(args[0], "reload") ? "ReloadUnit" :
786 streq(args[0], "restart") ? "RestartUnit" :
790 (streq(args[0], "isolate") ||
791 streq(args[0], "rescue") ||
792 streq(args[0], "emergency")) ? "isolate" :
793 arg_replace ? "replace" :
796 one_name = table[verb_to_action(args[0])];
799 assert(arg_action < ELEMENTSOF(table));
800 assert(table[arg_action]);
802 method = "StartUnit";
804 mode = (arg_action == ACTION_EMERGENCY ||
805 arg_action == ACTION_RESCUE) ? "isolate" : "replace";
807 one_name = table[arg_action];
811 if ((r = enable_wait_for_jobs(bus)) < 0) {
812 log_error("Could not watch jobs: %s", strerror(-r));
816 if (!(s = set_new(string_hash_func, string_compare_func))) {
817 log_error("Failed to allocate set.");
826 if ((r = start_unit_one(bus, method, one_name, mode, s)) <= 0)
829 for (i = 1; i < n; i++)
830 if ((r = start_unit_one(bus, method, args[i], mode, s)) < 0)
835 r = wait_for_jobs(bus, s);
844 static int start_special(DBusConnection *bus, char **args, unsigned n) {
848 warn_wall(verb_to_action(args[0]));
850 return start_unit(bus, args, n);
853 static int check_unit(DBusConnection *bus, char **args, unsigned n) {
854 DBusMessage *m = NULL, *reply = NULL;
856 *interface = "org.freedesktop.systemd1.Unit",
857 *property = "ActiveState";
858 int r = -EADDRNOTAVAIL;
865 dbus_error_init(&error);
867 for (i = 1; i < n; i++) {
868 const char *path = NULL;
870 DBusMessageIter iter, sub;
872 if (!(m = dbus_message_new_method_call(
873 "org.freedesktop.systemd1",
874 "/org/freedesktop/systemd1",
875 "org.freedesktop.systemd1.Manager",
877 log_error("Could not allocate message.");
882 if (!dbus_message_append_args(m,
883 DBUS_TYPE_STRING, &args[i],
884 DBUS_TYPE_INVALID)) {
885 log_error("Could not append arguments to message.");
890 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
892 /* Hmm, cannot figure out anything about this unit... */
899 if (!dbus_message_get_args(reply, &error,
900 DBUS_TYPE_OBJECT_PATH, &path,
901 DBUS_TYPE_INVALID)) {
902 log_error("Failed to parse reply: %s", error.message);
907 dbus_message_unref(m);
908 if (!(m = dbus_message_new_method_call(
909 "org.freedesktop.systemd1",
911 "org.freedesktop.DBus.Properties",
913 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.");
927 dbus_message_unref(reply);
928 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
929 log_error("Failed to issue method call: %s", error.message);
934 if (!dbus_message_iter_init(reply, &iter) ||
935 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
936 log_error("Failed to parse reply.");
941 dbus_message_iter_recurse(&iter, &sub);
943 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
944 log_error("Failed to parse reply.");
949 dbus_message_iter_get_basic(&sub, &state);
954 if (streq(state, "active") || startswith(state, "active-"))
957 dbus_message_unref(m);
958 dbus_message_unref(reply);
964 dbus_message_unref(m);
967 dbus_message_unref(reply);
969 dbus_error_free(&error);
975 static DBusHandlerResult monitor_filter(DBusConnection *connection, DBusMessage *message, void *data) {
977 DBusMessage *m = NULL, *reply = NULL;
982 dbus_error_init(&error);
984 /* log_debug("Got D-Bus request: %s.%s() on %s", */
985 /* dbus_message_get_interface(message), */
986 /* dbus_message_get_member(message), */
987 /* dbus_message_get_path(message)); */
989 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
990 log_error("Warning! D-Bus connection terminated.");
991 dbus_connection_close(connection);
993 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitNew") ||
994 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitRemoved")) {
995 const char *id, *path;
997 if (!dbus_message_get_args(message, &error,
998 DBUS_TYPE_STRING, &id,
999 DBUS_TYPE_OBJECT_PATH, &path,
1001 log_error("Failed to parse message: %s", error.message);
1002 else if (streq(dbus_message_get_member(message), "UnitNew"))
1003 printf("Unit %s added.\n", id);
1005 printf("Unit %s removed.\n", id);
1007 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobNew") ||
1008 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
1012 if (!dbus_message_get_args(message, &error,
1013 DBUS_TYPE_UINT32, &id,
1014 DBUS_TYPE_OBJECT_PATH, &path,
1016 log_error("Failed to parse message: %s", error.message);
1017 else if (streq(dbus_message_get_member(message), "JobNew"))
1018 printf("Job %u added.\n", id);
1020 printf("Job %u removed.\n", id);
1023 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Unit", "Changed") ||
1024 dbus_message_is_signal(message, "org.freedesktop.systemd1.Job", "Changed")) {
1026 const char *path, *interface, *property = "Id";
1027 DBusMessageIter iter, sub;
1029 path = dbus_message_get_path(message);
1030 interface = dbus_message_get_interface(message);
1032 if (!(m = dbus_message_new_method_call(
1033 "org.freedesktop.systemd1",
1035 "org.freedesktop.DBus.Properties",
1037 log_error("Could not allocate message.");
1041 if (!dbus_message_append_args(m,
1042 DBUS_TYPE_STRING, &interface,
1043 DBUS_TYPE_STRING, &property,
1044 DBUS_TYPE_INVALID)) {
1045 log_error("Could not append arguments to message.");
1049 if (!(reply = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) {
1050 log_error("Failed to issue method call: %s", error.message);
1054 if (!dbus_message_iter_init(reply, &iter) ||
1055 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1056 log_error("Failed to parse reply.");
1060 dbus_message_iter_recurse(&iter, &sub);
1062 if (streq(interface, "org.freedesktop.systemd1.Unit")) {
1065 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1066 log_error("Failed to parse reply.");
1070 dbus_message_iter_get_basic(&sub, &id);
1071 printf("Unit %s changed.\n", id);
1075 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT32) {
1076 log_error("Failed to parse reply.");
1080 dbus_message_iter_get_basic(&sub, &id);
1081 printf("Job %u changed.\n", id);
1087 dbus_message_unref(m);
1090 dbus_message_unref(reply);
1092 dbus_error_free(&error);
1093 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1097 dbus_message_unref(m);
1100 dbus_message_unref(reply);
1102 dbus_error_free(&error);
1103 return DBUS_HANDLER_RESULT_NEED_MEMORY;
1106 static int monitor(DBusConnection *bus, char **args, unsigned n) {
1107 DBusMessage *m = NULL, *reply = NULL;
1111 dbus_error_init(&error);
1113 dbus_bus_add_match(bus,
1115 "sender='org.freedesktop.systemd1',"
1116 "interface='org.freedesktop.systemd1.Manager',"
1117 "path='/org/freedesktop/systemd1'",
1120 if (dbus_error_is_set(&error)) {
1121 log_error("Failed to add match: %s", error.message);
1126 dbus_bus_add_match(bus,
1128 "sender='org.freedesktop.systemd1',"
1129 "interface='org.freedesktop.systemd1.Unit',"
1133 if (dbus_error_is_set(&error)) {
1134 log_error("Failed to add match: %s", error.message);
1139 dbus_bus_add_match(bus,
1141 "sender='org.freedesktop.systemd1',"
1142 "interface='org.freedesktop.systemd1.Job',"
1146 if (dbus_error_is_set(&error)) {
1147 log_error("Failed to add match: %s", error.message);
1152 if (!dbus_connection_add_filter(bus, monitor_filter, NULL, NULL)) {
1153 log_error("Failed to add filter.");
1158 if (!(m = dbus_message_new_method_call(
1159 "org.freedesktop.systemd1",
1160 "/org/freedesktop/systemd1",
1161 "org.freedesktop.systemd1.Manager",
1163 log_error("Could not allocate message.");
1168 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1169 log_error("Failed to issue method call: %s", error.message);
1174 while (dbus_connection_read_write_dispatch(bus, -1))
1181 /* This is slightly dirty, since we don't undo the filter or the matches. */
1184 dbus_message_unref(m);
1187 dbus_message_unref(reply);
1189 dbus_error_free(&error);
1194 static int dump(DBusConnection *bus, char **args, unsigned n) {
1195 DBusMessage *m = NULL, *reply = NULL;
1200 dbus_error_init(&error);
1202 if (!(m = dbus_message_new_method_call(
1203 "org.freedesktop.systemd1",
1204 "/org/freedesktop/systemd1",
1205 "org.freedesktop.systemd1.Manager",
1207 log_error("Could not allocate message.");
1211 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1212 log_error("Failed to issue method call: %s", error.message);
1217 if (!dbus_message_get_args(reply, &error,
1218 DBUS_TYPE_STRING, &text,
1219 DBUS_TYPE_INVALID)) {
1220 log_error("Failed to parse reply: %s", error.message);
1225 fputs(text, stdout);
1231 dbus_message_unref(m);
1234 dbus_message_unref(reply);
1236 dbus_error_free(&error);
1241 static int snapshot(DBusConnection *bus, char **args, unsigned n) {
1242 DBusMessage *m = NULL, *reply = NULL;
1245 const char *name = "", *path, *id;
1246 dbus_bool_t cleanup = FALSE;
1247 DBusMessageIter iter, sub;
1249 *interface = "org.freedesktop.systemd1.Unit",
1252 dbus_error_init(&error);
1254 if (!(m = dbus_message_new_method_call(
1255 "org.freedesktop.systemd1",
1256 "/org/freedesktop/systemd1",
1257 "org.freedesktop.systemd1.Manager",
1258 "CreateSnapshot"))) {
1259 log_error("Could not allocate message.");
1266 if (!dbus_message_append_args(m,
1267 DBUS_TYPE_STRING, &name,
1268 DBUS_TYPE_BOOLEAN, &cleanup,
1269 DBUS_TYPE_INVALID)) {
1270 log_error("Could not append arguments to message.");
1275 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1276 log_error("Failed to issue method call: %s", error.message);
1281 if (!dbus_message_get_args(reply, &error,
1282 DBUS_TYPE_OBJECT_PATH, &path,
1283 DBUS_TYPE_INVALID)) {
1284 log_error("Failed to parse reply: %s", error.message);
1289 dbus_message_unref(m);
1290 if (!(m = dbus_message_new_method_call(
1291 "org.freedesktop.systemd1",
1293 "org.freedesktop.DBus.Properties",
1295 log_error("Could not allocate message.");
1299 if (!dbus_message_append_args(m,
1300 DBUS_TYPE_STRING, &interface,
1301 DBUS_TYPE_STRING, &property,
1302 DBUS_TYPE_INVALID)) {
1303 log_error("Could not append arguments to message.");
1308 dbus_message_unref(reply);
1309 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1310 log_error("Failed to issue method call: %s", error.message);
1315 if (!dbus_message_iter_init(reply, &iter) ||
1316 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1317 log_error("Failed to parse reply.");
1322 dbus_message_iter_recurse(&iter, &sub);
1324 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1325 log_error("Failed to parse reply.");
1330 dbus_message_iter_get_basic(&sub, &id);
1338 dbus_message_unref(m);
1341 dbus_message_unref(reply);
1343 dbus_error_free(&error);
1348 static int clear_jobs(DBusConnection *bus, char **args, unsigned n) {
1349 DBusMessage *m = NULL, *reply = NULL;
1354 dbus_error_init(&error);
1356 if (arg_action == ACTION_RELOAD)
1358 else if (arg_action == ACTION_REEXEC)
1359 method = "Reexecute";
1361 assert(arg_action == ACTION_SYSTEMCTL);
1364 streq(args[0], "clear-jobs") ? "ClearJobs" :
1365 streq(args[0], "daemon-reload") ? "Reload" :
1366 streq(args[0], "daemon-reexec") ? "Reexecute" :
1370 if (!(m = dbus_message_new_method_call(
1371 "org.freedesktop.systemd1",
1372 "/org/freedesktop/systemd1",
1373 "org.freedesktop.systemd1.Manager",
1375 log_error("Could not allocate message.");
1379 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1381 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
1382 /* There's always a fallback possible for
1383 * legacy actions. */
1388 log_error("Failed to issue method call: %s", error.message);
1397 dbus_message_unref(m);
1400 dbus_message_unref(reply);
1402 dbus_error_free(&error);
1407 static int show_enviroment(DBusConnection *bus, char **args, unsigned n) {
1408 DBusMessage *m = NULL, *reply = NULL;
1410 DBusMessageIter iter, sub, sub2;
1413 *interface = "org.freedesktop.systemd1.Manager",
1414 *property = "Environment";
1416 dbus_error_init(&error);
1418 if (!(m = dbus_message_new_method_call(
1419 "org.freedesktop.systemd1",
1420 "/org/freedesktop/systemd1",
1421 "org.freedesktop.DBus.Properties",
1423 log_error("Could not allocate message.");
1427 if (!dbus_message_append_args(m,
1428 DBUS_TYPE_STRING, &interface,
1429 DBUS_TYPE_STRING, &property,
1430 DBUS_TYPE_INVALID)) {
1431 log_error("Could not append arguments to message.");
1436 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1437 log_error("Failed to issue method call: %s", error.message);
1442 if (!dbus_message_iter_init(reply, &iter) ||
1443 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1444 log_error("Failed to parse reply.");
1449 dbus_message_iter_recurse(&iter, &sub);
1451 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
1452 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
1453 log_error("Failed to parse reply.");
1458 dbus_message_iter_recurse(&sub, &sub2);
1460 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
1463 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
1464 log_error("Failed to parse reply.");
1469 dbus_message_iter_get_basic(&sub2, &text);
1470 printf("%s\n", text);
1472 dbus_message_iter_next(&sub2);
1479 dbus_message_unref(m);
1482 dbus_message_unref(reply);
1484 dbus_error_free(&error);
1489 static int set_environment(DBusConnection *bus, char **args, unsigned n) {
1490 DBusMessage *m = NULL, *reply = NULL;
1494 DBusMessageIter iter, sub;
1497 dbus_error_init(&error);
1499 method = streq(args[0], "set-environment")
1501 : "UnsetEnvironment";
1503 if (!(m = dbus_message_new_method_call(
1504 "org.freedesktop.systemd1",
1505 "/org/freedesktop/systemd1",
1506 "org.freedesktop.systemd1.Manager",
1509 log_error("Could not allocate message.");
1513 dbus_message_iter_init_append(m, &iter);
1515 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
1516 log_error("Could not append arguments to message.");
1521 for (i = 1; i < n; i++)
1522 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &args[i])) {
1523 log_error("Could not append arguments to message.");
1528 if (!dbus_message_iter_close_container(&iter, &sub)) {
1529 log_error("Could not append arguments to message.");
1534 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1535 log_error("Failed to issue method call: %s", error.message);
1544 dbus_message_unref(m);
1547 dbus_message_unref(reply);
1549 dbus_error_free(&error);
1554 static int systemctl_help(void) {
1556 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
1557 "Send control commands to the systemd manager.\n\n"
1558 " -h --help Show this help\n"
1559 " -t --type=TYPE List only units of a particular type\n"
1560 " -a --all Show all units, including dead ones\n"
1561 " --replace When installing a new job, replace existing conflicting ones\n"
1562 " --system Connect to system bus\n"
1563 " --session Connect to session bus\n"
1564 " -q --quiet Suppress output\n"
1565 " --no-block Do not wait until operation finished\n"
1566 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
1568 " list-units List units\n"
1569 " list-jobs List jobs\n"
1570 " clear-jobs Cancel all jobs\n"
1571 " load [NAME...] Load one or more units\n"
1572 " cancel [JOB...] Cancel one or more jobs\n"
1573 " start [NAME...] Start one or more units\n"
1574 " stop [NAME...] Stop one or more units\n"
1575 " restart [NAME...] Restart one or more units\n"
1576 " reload [NAME...] Reload one or more units\n"
1577 " isolate [NAME] Start one unit and stop all others\n"
1578 " check [NAME...] Check whether any of the passed units are active\n"
1579 " monitor Monitor unit/job changes\n"
1580 " dump Dump server status\n"
1581 " snapshot [NAME] Create a snapshot\n"
1582 " daemon-reload Reload systemd manager configuration\n"
1583 " daemon-reexec Reexecute systemd manager\n"
1584 " daemon-exit Ask the systemd manager to quit\n"
1585 " show-environment Dump environment\n"
1586 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
1587 " unset-environment [NAME...] Unset one or more environment variables\n"
1588 " halt Shut down and halt the system\n"
1589 " poweroff Shut down and power-off the system\n"
1590 " reboot Shut down and reboot the system\n"
1591 " default Enter default mode\n"
1592 " rescue Enter rescue mode\n"
1593 " emergency Enter emergency mode\n",
1594 program_invocation_short_name);
1599 static int halt_help(void) {
1601 printf("%s [OPTIONS...]\n\n"
1602 "%s the system.\n\n"
1603 " --help Show this help\n"
1604 " --halt Halt the machine\n"
1605 " -p --poweroff Switch off the machine\n"
1606 " --reboot Reboot the machine\n"
1607 " -f --force Force immediate halt/power-off/reboot\n"
1608 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
1609 " -d --no-wtmp Don't write wtmp record\n"
1610 " -n --no-sync Don't sync before halt/power-off/reboot\n"
1611 " --no-wall Don't send wall message before halt/power-off/reboot\n",
1612 program_invocation_short_name,
1613 arg_action == ACTION_REBOOT ? "Reboot" :
1614 arg_action == ACTION_POWEROFF ? "Power off" :
1620 static int shutdown_help(void) {
1622 printf("%s [OPTIONS...] [now] [WALL...]\n\n"
1623 "Shut down the system.\n\n"
1624 " --help Show this help\n"
1625 " -H --halt Halt the machine\n"
1626 " -P --poweroff Power-off the machine\n"
1627 " -r --reboot Reboot the machine\n"
1628 " -h Equivalent to --poweroff, overriden by --halt\n"
1629 " -k Don't halt/power-off/reboot, just send warnings\n"
1630 " --no-wall Don't send wall message before halt/power-off/reboot\n",
1631 program_invocation_short_name);
1636 static int telinit_help(void) {
1638 printf("%s [OPTIONS...] {COMMAND}\n\n"
1639 "Send control commands to the init daemon.\n\n"
1640 " --help Show this help\n"
1641 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
1643 " 0 Power-off the machine\n"
1644 " 6 Reboot the machine\n"
1645 " 2, 3, 4, 5 Start runlevelX.target unit\n"
1646 " 1, s, S Enter rescue mode\n"
1647 " q, Q Reload init daemon configuration\n"
1648 " u, U Reexecute init daemon\n",
1649 program_invocation_short_name);
1654 static int runlevel_help(void) {
1656 printf("%s [OPTIONS...]\n\n"
1657 "Prints the previous and current runlevel of the init system.\n\n"
1658 " --help Show this help\n",
1659 program_invocation_short_name);
1664 static int systemctl_parse_argv(int argc, char *argv[]) {
1667 ARG_REPLACE = 0x100,
1674 static const struct option options[] = {
1675 { "help", no_argument, NULL, 'h' },
1676 { "type", required_argument, NULL, 't' },
1677 { "all", no_argument, NULL, 'a' },
1678 { "replace", no_argument, NULL, ARG_REPLACE },
1679 { "session", no_argument, NULL, ARG_SESSION },
1680 { "system", no_argument, NULL, ARG_SYSTEM },
1681 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
1682 { "no-wall", no_argument, NULL, ARG_NO_WALL },
1683 { "quiet", no_argument, NULL, 'q' },
1684 { NULL, 0, NULL, 0 }
1692 while ((c = getopt_long(argc, argv, "htaq", options, NULL)) >= 0) {
1717 arg_session = false;
1721 arg_no_block = true;
1736 log_error("Unknown option code %c", c);
1744 static int halt_parse_argv(int argc, char *argv[]) {
1753 static const struct option options[] = {
1754 { "help", no_argument, NULL, ARG_HELP },
1755 { "halt", no_argument, NULL, ARG_HALT },
1756 { "poweroff", no_argument, NULL, 'p' },
1757 { "reboot", no_argument, NULL, ARG_REBOOT },
1758 { "force", no_argument, NULL, 'f' },
1759 { "wtmp-only", no_argument, NULL, 'w' },
1760 { "no-wtmp", no_argument, NULL, 'd' },
1761 { "no-sync", no_argument, NULL, 'n' },
1762 { "no-wall", no_argument, NULL, ARG_NO_WALL },
1763 { NULL, 0, NULL, 0 }
1771 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
1772 if (runlevel == '0' || runlevel == '6')
1773 arg_immediate = true;
1775 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
1783 arg_action = ACTION_HALT;
1787 arg_action = ACTION_POWEROFF;
1791 arg_action = ACTION_REBOOT;
1795 arg_immediate = true;
1816 /* Compatibility nops */
1823 log_error("Unknown option code %c", c);
1828 if (optind < argc) {
1829 log_error("Too many arguments.");
1836 static int shutdown_parse_argv(int argc, char *argv[]) {
1843 static const struct option options[] = {
1844 { "help", no_argument, NULL, ARG_HELP },
1845 { "halt", no_argument, NULL, 'H' },
1846 { "poweroff", no_argument, NULL, 'P' },
1847 { "reboot", no_argument, NULL, 'r' },
1848 { "no-wall", no_argument, NULL, ARG_NO_WALL },
1849 { NULL, 0, NULL, 0 }
1857 while ((c = getopt_long(argc, argv, "HPrhkt:a", options, NULL)) >= 0) {
1865 arg_action = ACTION_HALT;
1869 arg_action = ACTION_POWEROFF;
1873 arg_action = ACTION_REBOOT;
1877 if (arg_action != ACTION_HALT)
1878 arg_action = ACTION_POWEROFF;
1891 /* Compatibility nops */
1898 log_error("Unknown option code %c", c);
1903 if (argc > optind && !streq(argv[optind], "now"))
1904 log_warning("First argument '%s' isn't 'now'. Ignoring.", argv[optind]);
1906 /* We ignore the time argument */
1907 if (argc > optind + 1)
1908 arg_wall = argv + optind + 1;
1915 static int telinit_parse_argv(int argc, char *argv[]) {
1922 static const struct option options[] = {
1923 { "help", no_argument, NULL, ARG_HELP },
1924 { "no-wall", no_argument, NULL, ARG_NO_WALL },
1925 { NULL, 0, NULL, 0 }
1928 static const struct {
1932 { '0', ACTION_POWEROFF },
1933 { '6', ACTION_REBOOT },
1934 { '1', ACTION_RESCUE },
1935 { '2', ACTION_RUNLEVEL2 },
1936 { '3', ACTION_RUNLEVEL3 },
1937 { '4', ACTION_RUNLEVEL4 },
1938 { '5', ACTION_RUNLEVEL5 },
1939 { 's', ACTION_RESCUE },
1940 { 'S', ACTION_RESCUE },
1941 { 'q', ACTION_RELOAD },
1942 { 'Q', ACTION_RELOAD },
1943 { 'u', ACTION_REEXEC },
1944 { 'U', ACTION_REEXEC }
1953 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
1968 log_error("Unknown option code %c", c);
1973 if (optind >= argc) {
1978 if (optind + 1 < argc) {
1979 log_error("Too many arguments.");
1983 if (strlen(argv[optind]) != 1) {
1984 log_error("Expected single character argument.");
1988 for (i = 0; i < ELEMENTSOF(table); i++)
1989 if (table[i].from == argv[optind][0])
1992 if (i >= ELEMENTSOF(table)) {
1993 log_error("Unknown command %s.", argv[optind]);
1997 arg_action = table[i].to;
2004 static int runlevel_parse_argv(int argc, char *argv[]) {
2010 static const struct option options[] = {
2011 { "help", no_argument, NULL, ARG_HELP },
2012 { NULL, 0, NULL, 0 }
2020 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
2031 log_error("Unknown option code %c", c);
2036 if (optind < argc) {
2037 log_error("Too many arguments.");
2044 static int parse_argv(int argc, char *argv[]) {
2048 if (program_invocation_short_name) {
2050 if (strstr(program_invocation_short_name, "halt")) {
2051 arg_action = ACTION_HALT;
2052 return halt_parse_argv(argc, argv);
2053 } else if (strstr(program_invocation_short_name, "poweroff")) {
2054 arg_action = ACTION_POWEROFF;
2055 return halt_parse_argv(argc, argv);
2056 } else if (strstr(program_invocation_short_name, "reboot")) {
2057 arg_action = ACTION_REBOOT;
2058 return halt_parse_argv(argc, argv);
2059 } else if (strstr(program_invocation_short_name, "shutdown")) {
2060 arg_action = ACTION_POWEROFF;
2061 return shutdown_parse_argv(argc, argv);
2062 } else if (strstr(program_invocation_short_name, "init")) {
2063 arg_action = ACTION_INVALID;
2064 return telinit_parse_argv(argc, argv);
2065 } else if (strstr(program_invocation_short_name, "runlevel")) {
2066 arg_action = ACTION_RUNLEVEL;
2067 return runlevel_parse_argv(argc, argv);
2071 arg_action = ACTION_SYSTEMCTL;
2072 return systemctl_parse_argv(argc, argv);
2075 static int action_to_runlevel(void) {
2077 static const char table[_ACTION_MAX] = {
2078 [ACTION_HALT] = '0',
2079 [ACTION_POWEROFF] = '0',
2080 [ACTION_REBOOT] = '6',
2081 [ACTION_RUNLEVEL2] = '2',
2082 [ACTION_RUNLEVEL3] = '3',
2083 [ACTION_RUNLEVEL4] = '4',
2084 [ACTION_RUNLEVEL5] = '5',
2085 [ACTION_RESCUE] = '1'
2088 assert(arg_action < _ACTION_MAX);
2090 return table[arg_action];
2093 static int talk_upstart(void) {
2094 DBusMessage *m = NULL, *reply = NULL;
2096 int previous, rl, r;
2098 env1_buf[] = "RUNLEVEL=X",
2099 env2_buf[] = "PREVLEVEL=X";
2100 char *env1 = env1_buf, *env2 = env2_buf;
2101 const char *emit = "runlevel";
2102 dbus_bool_t b_false = FALSE;
2103 DBusMessageIter iter, sub;
2104 DBusConnection *bus;
2106 dbus_error_init(&error);
2108 if (!(rl = action_to_runlevel()))
2111 if (utmp_get_runlevel(&previous, NULL) < 0)
2114 if (!(bus = dbus_connection_open("unix:abstract=/com/ubuntu/upstart", &error))) {
2115 if (dbus_error_has_name(&error, DBUS_ERROR_NO_SERVER)) {
2120 log_error("Failed to connect to Upstart bus: %s", error.message);
2125 if ((r = bus_check_peercred(bus)) < 0) {
2126 log_error("Failed to verify owner of bus.");
2130 if (!(m = dbus_message_new_method_call(
2131 "com.ubuntu.Upstart",
2132 "/com/ubuntu/Upstart",
2133 "com.ubuntu.Upstart0_6",
2136 log_error("Could not allocate message.");
2141 dbus_message_iter_init_append(m, &iter);
2143 env1_buf[sizeof(env1_buf)-2] = rl;
2144 env2_buf[sizeof(env2_buf)-2] = previous;
2146 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
2147 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
2148 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
2149 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
2150 !dbus_message_iter_close_container(&iter, &sub) ||
2151 !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
2152 log_error("Could not append arguments to message.");
2157 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2159 if (error_is_no_service(&error)) {
2164 log_error("Failed to issue method call: %s", error.message);
2173 dbus_message_unref(m);
2176 dbus_message_unref(reply);
2179 dbus_connection_unref(bus);
2181 dbus_error_free(&error);
2186 static int talk_initctl(void) {
2187 struct init_request request;
2191 if (!(rl = action_to_runlevel()))
2195 request.magic = INIT_MAGIC;
2196 request.sleeptime = 0;
2197 request.cmd = INIT_CMD_RUNLVL;
2198 request.runlevel = rl;
2200 if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
2202 if (errno == ENOENT)
2205 log_error("Failed to open "INIT_FIFO": %m");
2210 r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
2211 close_nointr_nofail(fd);
2214 log_error("Failed to write to "INIT_FIFO": %m");
2215 return errno ? -errno : -EIO;
2221 static int systemctl_main(DBusConnection *bus, int argc, char *argv[]) {
2223 static const struct {
2231 int (* const dispatch)(DBusConnection *bus, char **args, unsigned n);
2233 { "list-units", LESS, 1, list_units },
2234 { "list-jobs", EQUAL, 1, list_jobs },
2235 { "clear-jobs", EQUAL, 1, clear_jobs },
2236 { "load", MORE, 2, load_unit },
2237 { "cancel", MORE, 2, cancel_job },
2238 { "start", MORE, 2, start_unit },
2239 { "stop", MORE, 2, start_unit },
2240 { "reload", MORE, 2, start_unit },
2241 { "restart", MORE, 2, start_unit },
2242 { "isolate", EQUAL, 2, start_unit },
2243 { "check", MORE, 2, check_unit },
2244 { "monitor", EQUAL, 1, monitor },
2245 { "dump", EQUAL, 1, dump },
2246 { "snapshot", LESS, 2, snapshot },
2247 { "daemon-reload", EQUAL, 1, clear_jobs },
2248 { "daemon-reexec", EQUAL, 1, clear_jobs },
2249 { "daemon-exit", EQUAL, 1, clear_jobs },
2250 { "show-environment", EQUAL, 1, show_enviroment },
2251 { "set-environment", MORE, 2, set_environment },
2252 { "unset-environment", MORE, 2, set_environment },
2253 { "halt", EQUAL, 1, start_special },
2254 { "poweroff", EQUAL, 1, start_special },
2255 { "reboot", EQUAL, 1, start_special },
2256 { "default", EQUAL, 1, start_special },
2257 { "rescue", EQUAL, 1, start_special },
2258 { "emergency", EQUAL, 1, start_special },
2268 left = argc - optind;
2271 /* Special rule: no arguments means "list-units" */
2274 if (streq(argv[optind], "help")) {
2279 for (i = 0; i < ELEMENTSOF(verbs); i++)
2280 if (streq(argv[optind], verbs[i].verb))
2283 if (i >= ELEMENTSOF(verbs)) {
2284 log_error("Unknown operation %s", argv[optind]);
2289 switch (verbs[i].argc_cmp) {
2292 if (left != verbs[i].argc) {
2293 log_error("Invalid number of arguments.");
2300 if (left < verbs[i].argc) {
2301 log_error("Too few arguments.");
2308 if (left > verbs[i].argc) {
2309 log_error("Too many arguments.");
2316 assert_not_reached("Unknown comparison operator.");
2319 return verbs[i].dispatch(bus, argv + optind, left);
2322 static int reload_with_fallback(DBusConnection *bus) {
2326 /* First, try systemd via D-Bus. */
2327 if ((r = clear_jobs(bus, NULL, 0)) > 0)
2331 /* Nothing else worked, so let's try signals */
2332 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
2334 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
2335 log_error("kill() failed: %m");
2342 static int start_with_fallback(DBusConnection *bus) {
2345 warn_wall(arg_action);
2348 /* First, try systemd via D-Bus. */
2349 if ((r = start_unit(bus, NULL, 0)) > 0)
2352 /* Hmm, talking to systemd via D-Bus didn't work. Then
2353 * let's try to talk to Upstart via D-Bus. */
2354 if ((r = talk_upstart()) > 0)
2358 /* Nothing else worked, so let's try
2360 if ((r = talk_initctl()) != 0)
2363 log_error("Failed to talk to init daemon.");
2367 static int halt_main(DBusConnection *bus) {
2371 return start_with_fallback(bus);
2374 if ((r = utmp_put_shutdown(0)) < 0)
2375 log_warning("Failed to write utmp record: %s", strerror(-r));
2383 /* Make sure C-A-D is handled by the kernel from this
2385 reboot(RB_ENABLE_CAD);
2387 switch (arg_action) {
2390 log_info("Halting");
2391 reboot(RB_HALT_SYSTEM);
2394 case ACTION_POWEROFF:
2395 log_info("Powering off");
2396 reboot(RB_POWER_OFF);
2400 log_info("Rebooting");
2401 reboot(RB_AUTOBOOT);
2405 assert_not_reached("Unknown halt action.");
2408 /* We should never reach this. */
2412 static int runlevel_main(void) {
2413 int r, runlevel, previous;
2415 if ((r = utmp_get_runlevel(&runlevel, &previous)) < 0) {
2421 previous <= 0 ? 'N' : previous,
2422 runlevel <= 0 ? 'N' : runlevel);
2427 int main(int argc, char*argv[]) {
2429 DBusConnection *bus = NULL;
2432 dbus_error_init(&error);
2434 log_parse_environment();
2436 if ((r = parse_argv(argc, argv)) < 0)
2443 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
2444 * let's shortcut this */
2445 if (arg_action == ACTION_RUNLEVEL) {
2446 retval = runlevel_main() < 0;
2450 /* If we are root, then let's not go via the bus */
2451 if (geteuid() == 0 && !arg_session) {
2452 bus = dbus_connection_open("unix:abstract=/org/freedesktop/systemd1/private", &error);
2454 if (bus && bus_check_peercred(bus) < 0) {
2455 log_error("Failed to verify owner of bus.");
2459 bus = dbus_bus_get(arg_session ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &error);
2462 dbus_connection_set_exit_on_disconnect(bus, FALSE);
2464 switch (arg_action) {
2466 case ACTION_SYSTEMCTL: {
2469 log_error("Failed to get D-Bus connection: %s", error.message);
2473 retval = systemctl_main(bus, argc, argv) < 0;
2478 case ACTION_POWEROFF:
2480 retval = halt_main(bus) < 0;
2483 case ACTION_RUNLEVEL2:
2484 case ACTION_RUNLEVEL3:
2485 case ACTION_RUNLEVEL4:
2486 case ACTION_RUNLEVEL5:
2488 case ACTION_EMERGENCY:
2489 case ACTION_DEFAULT:
2490 retval = start_with_fallback(bus) < 0;
2495 retval = reload_with_fallback(bus) < 0;
2498 case ACTION_INVALID:
2499 case ACTION_RUNLEVEL:
2501 assert_not_reached("Unknown action");
2507 dbus_connection_unref(bus);
2509 dbus_error_free(&error);