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 const char *arg_property = NULL;
47 static bool arg_all = false;
48 static bool arg_replace = false;
49 static bool arg_session = false;
50 static bool arg_no_block = false;
51 static bool arg_immediate = false;
52 static bool arg_no_wtmp = false;
53 static bool arg_no_sync = false;
54 static bool arg_no_wall = false;
55 static bool arg_dry = false;
56 static bool arg_quiet = false;
57 static char **arg_wall = NULL;
75 } arg_action = ACTION_SYSTEMCTL;
77 static bool error_is_no_service(DBusError *error) {
81 if (!dbus_error_is_set(error))
84 if (dbus_error_has_name(error, DBUS_ERROR_NAME_HAS_NO_OWNER))
87 if (dbus_error_has_name(error, DBUS_ERROR_SERVICE_UNKNOWN))
90 return startswith(error->name, "org.freedesktop.DBus.Error.Spawn.");
93 static int bus_iter_get_basic_and_next(DBusMessageIter *iter, int type, void *data, bool next) {
98 if (dbus_message_iter_get_arg_type(iter) != type)
101 dbus_message_iter_get_basic(iter, data);
103 if (!dbus_message_iter_next(iter) != !next)
109 static int bus_check_peercred(DBusConnection *c) {
116 assert_se(dbus_connection_get_unix_fd(c, &fd));
118 l = sizeof(struct ucred);
119 if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &ucred, &l) < 0) {
120 log_error("SO_PEERCRED failed: %m");
124 if (l != sizeof(struct ucred)) {
125 log_error("SO_PEERCRED returned wrong size.");
135 static int columns(void) {
136 static int parsed_columns = 0;
139 if (parsed_columns > 0)
140 return parsed_columns;
142 if ((e = getenv("COLUMNS")))
143 parsed_columns = atoi(e);
145 if (parsed_columns <= 0) {
149 if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) >= 0)
150 parsed_columns = ws.ws_col;
153 if (parsed_columns <= 0)
156 return parsed_columns;
160 static void warn_wall(enum action action) {
161 static const char *table[_ACTION_MAX] = {
162 [ACTION_HALT] = "The system is going down for system halt NOW!",
163 [ACTION_REBOOT] = "The system is going down for reboot NOW!",
164 [ACTION_POWEROFF] = "The system is going down for power-off NOW!",
165 [ACTION_RESCUE] = "The system is going down to rescue mode NOW!",
166 [ACTION_EMERGENCY] = "The system is going down to emergency mode NOW!"
175 if (!(p = strv_join(arg_wall, " "))) {
176 log_error("Failed to join strings.");
192 utmp_wall(table[action]);
195 static int list_units(DBusConnection *bus, char **args, unsigned n) {
196 DBusMessage *m = NULL, *reply = NULL;
199 DBusMessageIter iter, sub, sub2;
202 dbus_error_init(&error);
206 if (!(m = dbus_message_new_method_call(
207 "org.freedesktop.systemd1",
208 "/org/freedesktop/systemd1",
209 "org.freedesktop.systemd1.Manager",
211 log_error("Could not allocate message.");
215 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
216 log_error("Failed to issue method call: %s", error.message);
221 if (!dbus_message_iter_init(reply, &iter) ||
222 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
223 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
224 log_error("Failed to parse reply.");
229 dbus_message_iter_recurse(&iter, &sub);
231 printf("%-45s %-6s %-12s %-12s %-15s %s\n", "UNIT", "LOAD", "ACTIVE", "SUB", "JOB", "DESCRIPTION");
233 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
234 const char *id, *description, *load_state, *active_state, *sub_state, *unit_state, *job_type, *job_path, *dot;
237 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
238 log_error("Failed to parse reply.");
243 dbus_message_iter_recurse(&sub, &sub2);
245 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
246 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &description, true) < 0 ||
247 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &load_state, true) < 0 ||
248 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &active_state, true) < 0 ||
249 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &sub_state, true) < 0 ||
250 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_state, true) < 0 ||
251 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &job_id, true) < 0 ||
252 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &job_type, true) < 0 ||
253 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, false) < 0) {
254 log_error("Failed to parse reply.");
259 if ((!arg_type || ((dot = strrchr(id, '.')) &&
260 streq(dot+1, arg_type))) &&
261 (arg_all || !streq(active_state, "inactive"))) {
265 printf("%-45s %-6s %-12s %-12s%n", id, load_state, active_state, sub_state, &a);
268 printf(" %-15s%n", job_type, &b);
272 if (a + b + 2 < columns()) {
276 printf("%.*s", columns() - a - b - 2, description);
283 dbus_message_iter_next(&sub);
287 printf("\n%u units listed.\n", k);
289 printf("\n%u live units listed. Pass --all to see dead units, too.\n", k);
295 dbus_message_unref(m);
298 dbus_message_unref(reply);
300 dbus_error_free(&error);
305 static int list_jobs(DBusConnection *bus, char **args, unsigned n) {
306 DBusMessage *m = NULL, *reply = NULL;
309 DBusMessageIter iter, sub, sub2;
312 dbus_error_init(&error);
316 if (!(m = dbus_message_new_method_call(
317 "org.freedesktop.systemd1",
318 "/org/freedesktop/systemd1",
319 "org.freedesktop.systemd1.Manager",
321 log_error("Could not allocate message.");
325 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
326 log_error("Failed to issue method call: %s", error.message);
331 if (!dbus_message_iter_init(reply, &iter) ||
332 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
333 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
334 log_error("Failed to parse reply.");
339 dbus_message_iter_recurse(&iter, &sub);
341 printf("%4s %-45s %-17s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
343 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
344 const char *name, *type, *state, *job_path, *unit_path;
347 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
348 log_error("Failed to parse reply.");
353 dbus_message_iter_recurse(&sub, &sub2);
355 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &id, true) < 0 ||
356 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0 ||
357 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
358 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, true) < 0 ||
359 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, true) < 0 ||
360 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, false) < 0) {
361 log_error("Failed to parse reply.");
366 printf("%4u %-45s %-17s %-7s\n", id, name, type, state);
369 dbus_message_iter_next(&sub);
372 printf("\n%u jobs listed.\n", k);
377 dbus_message_unref(m);
380 dbus_message_unref(reply);
382 dbus_error_free(&error);
387 static int load_unit(DBusConnection *bus, char **args, unsigned n) {
388 DBusMessage *m = NULL, *reply = NULL;
393 dbus_error_init(&error);
398 for (i = 1; i < n; i++) {
400 if (!(m = dbus_message_new_method_call(
401 "org.freedesktop.systemd1",
402 "/org/freedesktop/systemd1",
403 "org.freedesktop.systemd1.Manager",
405 log_error("Could not allocate message.");
410 if (!dbus_message_append_args(m,
411 DBUS_TYPE_STRING, &args[i],
412 DBUS_TYPE_INVALID)) {
413 log_error("Could not append arguments to message.");
418 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
419 log_error("Failed to issue method call: %s", error.message);
424 dbus_message_unref(m);
425 dbus_message_unref(reply);
434 dbus_message_unref(m);
437 dbus_message_unref(reply);
439 dbus_error_free(&error);
444 static int cancel_job(DBusConnection *bus, char **args, unsigned n) {
445 DBusMessage *m = NULL, *reply = NULL;
450 dbus_error_init(&error);
455 for (i = 1; i < n; i++) {
459 if (!(m = dbus_message_new_method_call(
460 "org.freedesktop.systemd1",
461 "/org/freedesktop/systemd1",
462 "org.freedesktop.systemd1.Manager",
464 log_error("Could not allocate message.");
469 if ((r = safe_atou(args[i], &id)) < 0) {
470 log_error("Failed to parse job id: %s", strerror(-r));
474 assert_cc(sizeof(uint32_t) == sizeof(id));
475 if (!dbus_message_append_args(m,
476 DBUS_TYPE_UINT32, &id,
477 DBUS_TYPE_INVALID)) {
478 log_error("Could not append arguments to message.");
483 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
484 log_error("Failed to issue method call: %s", error.message);
489 if (!dbus_message_get_args(reply, &error,
490 DBUS_TYPE_OBJECT_PATH, &path,
491 DBUS_TYPE_INVALID)) {
492 log_error("Failed to parse reply: %s", error.message);
497 dbus_message_unref(m);
498 if (!(m = dbus_message_new_method_call(
499 "org.freedesktop.systemd1",
501 "org.freedesktop.systemd1.Job",
503 log_error("Could not allocate message.");
508 dbus_message_unref(reply);
509 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
510 log_error("Failed to issue method call: %s", error.message);
515 dbus_message_unref(m);
516 dbus_message_unref(reply);
524 dbus_message_unref(m);
527 dbus_message_unref(reply);
529 dbus_error_free(&error);
534 typedef struct WaitData {
539 static DBusHandlerResult wait_filter(DBusConnection *connection, DBusMessage *message, void *data) {
547 dbus_error_init(&error);
549 /* log_debug("Got D-Bus request: %s.%s() on %s", */
550 /* dbus_message_get_interface(message), */
551 /* dbus_message_get_member(message), */
552 /* dbus_message_get_path(message)); */
554 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
555 log_error("Warning! D-Bus connection terminated.");
556 dbus_connection_close(connection);
558 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
561 dbus_bool_t success = true;
563 if (!dbus_message_get_args(message, &error,
564 DBUS_TYPE_UINT32, &id,
565 DBUS_TYPE_OBJECT_PATH, &path,
566 DBUS_TYPE_BOOLEAN, &success,
568 log_error("Failed to parse message: %s", error.message);
572 if ((p = set_remove(d->set, (char*) path)))
580 dbus_error_free(&error);
581 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
584 static int enable_wait_for_jobs(DBusConnection *bus) {
586 DBusMessage *m = NULL, *reply = NULL;
591 dbus_error_init(&error);
593 dbus_bus_add_match(bus,
595 "sender='org.freedesktop.systemd1',"
596 "interface='org.freedesktop.systemd1.Manager',"
597 "member='JobRemoved',"
598 "path='/org/freedesktop/systemd1'",
601 if (dbus_error_is_set(&error)) {
602 log_error("Failed to add match: %s", error.message);
607 if (!(m = dbus_message_new_method_call(
608 "org.freedesktop.systemd1",
609 "/org/freedesktop/systemd1",
610 "org.freedesktop.systemd1.Manager",
612 log_error("Could not allocate message.");
617 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
618 log_error("Failed to issue method call: %s", error.message);
626 /* This is slightly dirty, since we don't undo the match registrations. */
629 dbus_message_unref(m);
632 dbus_message_unref(reply);
634 dbus_error_free(&error);
639 static int wait_for_jobs(DBusConnection *bus, Set *s) {
650 if (!dbus_connection_add_filter(bus, wait_filter, &d, NULL)) {
651 log_error("Failed to add filter.");
656 while (!set_isempty(s) &&
657 dbus_connection_read_write_dispatch(bus, -1))
660 if (!arg_quiet && d.failed)
661 log_error("Job failed, see logs for details.");
663 r = d.failed ? -EIO : 0;
666 /* This is slightly dirty, since we don't undo the filter registration. */
671 static int start_unit_one(
678 DBusMessage *m = NULL, *reply = NULL;
686 assert(arg_no_block || s);
688 dbus_error_init(&error);
690 if (!(m = dbus_message_new_method_call(
691 "org.freedesktop.systemd1",
692 "/org/freedesktop/systemd1",
693 "org.freedesktop.systemd1.Manager",
695 log_error("Could not allocate message.");
700 if (!dbus_message_append_args(m,
701 DBUS_TYPE_STRING, &name,
702 DBUS_TYPE_STRING, &mode,
703 DBUS_TYPE_INVALID)) {
704 log_error("Could not append arguments to message.");
709 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
711 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
712 /* There's always a fallback possible for
718 log_error("Failed to issue method call: %s", error.message);
727 if (!dbus_message_get_args(reply, &error,
728 DBUS_TYPE_OBJECT_PATH, &path,
729 DBUS_TYPE_INVALID)) {
730 log_error("Failed to parse reply: %s", error.message);
735 if (!(p = strdup(path))) {
736 log_error("Failed to duplicate path.");
741 if ((r = set_put(s, p)) < 0) {
743 log_error("Failed to add path to set.");
752 dbus_message_unref(m);
755 dbus_message_unref(reply);
757 dbus_error_free(&error);
762 static enum action verb_to_action(const char *verb) {
763 if (streq(verb, "halt"))
765 else if (streq(verb, "poweroff"))
766 return ACTION_POWEROFF;
767 else if (streq(verb, "reboot"))
768 return ACTION_REBOOT;
769 else if (streq(verb, "rescue"))
770 return ACTION_RESCUE;
771 else if (streq(verb, "emergency"))
772 return ACTION_EMERGENCY;
773 else if (streq(verb, "default"))
774 return ACTION_DEFAULT;
776 return ACTION_INVALID;
779 static int start_unit(DBusConnection *bus, char **args, unsigned n) {
781 static const char * const table[_ACTION_MAX] = {
782 [ACTION_HALT] = SPECIAL_HALT_TARGET,
783 [ACTION_POWEROFF] = SPECIAL_POWEROFF_TARGET,
784 [ACTION_REBOOT] = SPECIAL_REBOOT_TARGET,
785 [ACTION_RUNLEVEL2] = SPECIAL_RUNLEVEL2_TARGET,
786 [ACTION_RUNLEVEL3] = SPECIAL_RUNLEVEL3_TARGET,
787 [ACTION_RUNLEVEL4] = SPECIAL_RUNLEVEL4_TARGET,
788 [ACTION_RUNLEVEL5] = SPECIAL_RUNLEVEL5_TARGET,
789 [ACTION_RESCUE] = SPECIAL_RESCUE_TARGET,
790 [ACTION_EMERGENCY] = SPECIAL_EMERGENCY_SERVICE,
791 [ACTION_DEFAULT] = SPECIAL_DEFAULT_TARGET
796 const char *method, *mode, *one_name;
801 if (arg_action == ACTION_SYSTEMCTL) {
803 streq(args[0], "stop") ? "StopUnit" :
804 streq(args[0], "reload") ? "ReloadUnit" :
805 streq(args[0], "restart") ? "RestartUnit" :
809 (streq(args[0], "isolate") ||
810 streq(args[0], "rescue") ||
811 streq(args[0], "emergency")) ? "isolate" :
812 arg_replace ? "replace" :
815 one_name = table[verb_to_action(args[0])];
818 assert(arg_action < ELEMENTSOF(table));
819 assert(table[arg_action]);
821 method = "StartUnit";
823 mode = (arg_action == ACTION_EMERGENCY ||
824 arg_action == ACTION_RESCUE) ? "isolate" : "replace";
826 one_name = table[arg_action];
830 if ((r = enable_wait_for_jobs(bus)) < 0) {
831 log_error("Could not watch jobs: %s", strerror(-r));
835 if (!(s = set_new(string_hash_func, string_compare_func))) {
836 log_error("Failed to allocate set.");
845 if ((r = start_unit_one(bus, method, one_name, mode, s)) <= 0)
848 for (i = 1; i < n; i++)
849 if ((r = start_unit_one(bus, method, args[i], mode, s)) < 0)
854 r = wait_for_jobs(bus, s);
863 static int start_special(DBusConnection *bus, char **args, unsigned n) {
867 warn_wall(verb_to_action(args[0]));
869 return start_unit(bus, args, n);
872 static int check_unit(DBusConnection *bus, char **args, unsigned n) {
873 DBusMessage *m = NULL, *reply = NULL;
875 *interface = "org.freedesktop.systemd1.Unit",
876 *property = "ActiveState";
877 int r = -EADDRNOTAVAIL;
884 dbus_error_init(&error);
886 for (i = 1; i < n; i++) {
887 const char *path = NULL;
889 DBusMessageIter iter, sub;
891 if (!(m = dbus_message_new_method_call(
892 "org.freedesktop.systemd1",
893 "/org/freedesktop/systemd1",
894 "org.freedesktop.systemd1.Manager",
896 log_error("Could not allocate message.");
901 if (!dbus_message_append_args(m,
902 DBUS_TYPE_STRING, &args[i],
903 DBUS_TYPE_INVALID)) {
904 log_error("Could not append arguments to message.");
909 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
911 /* Hmm, cannot figure out anything about this unit... */
918 if (!dbus_message_get_args(reply, &error,
919 DBUS_TYPE_OBJECT_PATH, &path,
920 DBUS_TYPE_INVALID)) {
921 log_error("Failed to parse reply: %s", error.message);
926 dbus_message_unref(m);
927 if (!(m = dbus_message_new_method_call(
928 "org.freedesktop.systemd1",
930 "org.freedesktop.DBus.Properties",
932 log_error("Could not allocate message.");
937 if (!dbus_message_append_args(m,
938 DBUS_TYPE_STRING, &interface,
939 DBUS_TYPE_STRING, &property,
940 DBUS_TYPE_INVALID)) {
941 log_error("Could not append arguments to message.");
946 dbus_message_unref(reply);
947 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
948 log_error("Failed to issue method call: %s", error.message);
953 if (!dbus_message_iter_init(reply, &iter) ||
954 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
955 log_error("Failed to parse reply.");
960 dbus_message_iter_recurse(&iter, &sub);
962 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
963 log_error("Failed to parse reply.");
968 dbus_message_iter_get_basic(&sub, &state);
973 if (streq(state, "active") || startswith(state, "reloading"))
976 dbus_message_unref(m);
977 dbus_message_unref(reply);
983 dbus_message_unref(m);
986 dbus_message_unref(reply);
988 dbus_error_free(&error);
993 static int print_property(const char *name, DBusMessageIter *iter) {
997 if (arg_property && !streq(name, arg_property))
1000 switch (dbus_message_iter_get_arg_type(iter)) {
1002 case DBUS_TYPE_STRING: {
1004 dbus_message_iter_get_basic(iter, &s);
1006 if (arg_all || s[0])
1007 printf("%s=%s\n", name, s);
1012 case DBUS_TYPE_BOOLEAN: {
1014 dbus_message_iter_get_basic(iter, &b);
1015 printf("%s=%s\n", name, yes_no(b));
1020 case DBUS_TYPE_UINT64: {
1022 dbus_message_iter_get_basic(iter, &u);
1024 /* Yes, heuristics! But we can change this check
1025 * should it turn out to not be sufficient */
1027 if (strstr(name, "Timestamp")) {
1028 char timestamp[FORMAT_TIMESTAMP_MAX], *t;
1030 if ((t = format_timestamp(timestamp, sizeof(timestamp), u)) || arg_all)
1031 printf("%s=%s\n", name, strempty(t));
1033 printf("%s=%llu\n", name, (unsigned long long) u);
1038 case DBUS_TYPE_UINT32: {
1040 dbus_message_iter_get_basic(iter, &u);
1042 if (strstr(name, "UMask"))
1043 printf("%s=%04o\n", name, u);
1045 printf("%s=%u\n", name, (unsigned) u);
1050 case DBUS_TYPE_INT32: {
1052 dbus_message_iter_get_basic(iter, &i);
1054 printf("%s=%i\n", name, (int) i);
1058 case DBUS_TYPE_STRUCT: {
1059 DBusMessageIter sub;
1060 dbus_message_iter_recurse(iter, &sub);
1062 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && strstr(name, "Job")) {
1065 dbus_message_iter_get_basic(&sub, &u);
1068 printf("%s=%u\n", name, (unsigned) u);
1070 printf("%s=\n", name);
1073 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && strstr(name, "Unit")) {
1076 dbus_message_iter_get_basic(&sub, &s);
1078 if (arg_all || s[0])
1079 printf("%s=%s\n", name, s);
1087 case DBUS_TYPE_ARRAY:
1089 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
1090 DBusMessageIter sub;
1093 dbus_message_iter_recurse(iter, &sub);
1096 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1097 printf("%s=", name);
1099 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1102 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
1103 dbus_message_iter_get_basic(&sub, &s);
1104 printf("%s%s", space ? " " : "", s);
1107 dbus_message_iter_next(&sub);
1120 printf("%s=[unprintable]\n", name);
1125 static int show_one(DBusConnection *bus, const char *path) {
1126 DBusMessage *m = NULL, *reply = NULL;
1127 const char *interface = "";
1130 DBusMessageIter iter, sub, sub2, sub3;
1135 dbus_error_init(&error);
1137 if (!(m = dbus_message_new_method_call(
1138 "org.freedesktop.systemd1",
1140 "org.freedesktop.DBus.Properties",
1142 log_error("Could not allocate message.");
1147 if (!dbus_message_append_args(m,
1148 DBUS_TYPE_STRING, &interface,
1149 DBUS_TYPE_INVALID)) {
1150 log_error("Could not append arguments to message.");
1155 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1156 log_error("Failed to issue method call: %s", error.message);
1161 if (!dbus_message_iter_init(reply, &iter) ||
1162 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
1163 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
1164 log_error("Failed to parse reply.");
1169 dbus_message_iter_recurse(&iter, &sub);
1171 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1174 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
1175 log_error("Failed to parse reply.");
1180 dbus_message_iter_recurse(&sub, &sub2);
1182 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
1183 log_error("Failed to parse reply.");
1188 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
1189 log_error("Failed to parse reply.");
1194 dbus_message_iter_recurse(&sub2, &sub3);
1196 if (print_property(name, &sub3) < 0) {
1197 log_error("Failed to parse reply.");
1202 dbus_message_iter_next(&sub);
1209 dbus_message_unref(m);
1212 dbus_message_unref(reply);
1214 dbus_error_free(&error);
1219 static int show(DBusConnection *bus, char **args, unsigned n) {
1220 DBusMessage *m = NULL, *reply = NULL;
1228 dbus_error_init(&error);
1231 /* If not argument is specified inspect the manager
1234 r = show_one(bus, "/org/freedesktop/systemd1");
1238 for (i = 1; i < n; i++) {
1239 const char *path = NULL;
1242 if (safe_atou32(args[i], &id) < 0) {
1244 if (!(m = dbus_message_new_method_call(
1245 "org.freedesktop.systemd1",
1246 "/org/freedesktop/systemd1",
1247 "org.freedesktop.systemd1.Manager",
1249 log_error("Could not allocate message.");
1254 if (!dbus_message_append_args(m,
1255 DBUS_TYPE_STRING, &args[i],
1256 DBUS_TYPE_INVALID)) {
1257 log_error("Could not append arguments to message.");
1264 if (!(m = dbus_message_new_method_call(
1265 "org.freedesktop.systemd1",
1266 "/org/freedesktop/systemd1",
1267 "org.freedesktop.systemd1.Manager",
1269 log_error("Could not allocate message.");
1274 if (!dbus_message_append_args(m,
1275 DBUS_TYPE_UINT32, &id,
1276 DBUS_TYPE_INVALID)) {
1277 log_error("Could not append arguments to message.");
1283 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1284 log_error("Failed to issue method call: %s", error.message);
1289 if (!dbus_message_get_args(reply, &error,
1290 DBUS_TYPE_OBJECT_PATH, &path,
1291 DBUS_TYPE_INVALID)) {
1292 log_error("Failed to parse reply: %s", error.message);
1297 if ((r = show_one(bus, path)) < 0)
1300 dbus_message_unref(m);
1301 dbus_message_unref(reply);
1309 dbus_message_unref(m);
1312 dbus_message_unref(reply);
1314 dbus_error_free(&error);
1319 static DBusHandlerResult monitor_filter(DBusConnection *connection, DBusMessage *message, void *data) {
1321 DBusMessage *m = NULL, *reply = NULL;
1326 dbus_error_init(&error);
1328 /* log_debug("Got D-Bus request: %s.%s() on %s", */
1329 /* dbus_message_get_interface(message), */
1330 /* dbus_message_get_member(message), */
1331 /* dbus_message_get_path(message)); */
1333 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
1334 log_error("Warning! D-Bus connection terminated.");
1335 dbus_connection_close(connection);
1337 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitNew") ||
1338 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitRemoved")) {
1339 const char *id, *path;
1341 if (!dbus_message_get_args(message, &error,
1342 DBUS_TYPE_STRING, &id,
1343 DBUS_TYPE_OBJECT_PATH, &path,
1345 log_error("Failed to parse message: %s", error.message);
1346 else if (streq(dbus_message_get_member(message), "UnitNew"))
1347 printf("Unit %s added.\n", id);
1349 printf("Unit %s removed.\n", id);
1351 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobNew") ||
1352 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
1356 if (!dbus_message_get_args(message, &error,
1357 DBUS_TYPE_UINT32, &id,
1358 DBUS_TYPE_OBJECT_PATH, &path,
1360 log_error("Failed to parse message: %s", error.message);
1361 else if (streq(dbus_message_get_member(message), "JobNew"))
1362 printf("Job %u added.\n", id);
1364 printf("Job %u removed.\n", id);
1367 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Unit", "Changed") ||
1368 dbus_message_is_signal(message, "org.freedesktop.systemd1.Job", "Changed")) {
1370 const char *path, *interface, *property = "Id";
1371 DBusMessageIter iter, sub;
1373 path = dbus_message_get_path(message);
1374 interface = dbus_message_get_interface(message);
1376 if (!(m = dbus_message_new_method_call(
1377 "org.freedesktop.systemd1",
1379 "org.freedesktop.DBus.Properties",
1381 log_error("Could not allocate message.");
1385 if (!dbus_message_append_args(m,
1386 DBUS_TYPE_STRING, &interface,
1387 DBUS_TYPE_STRING, &property,
1388 DBUS_TYPE_INVALID)) {
1389 log_error("Could not append arguments to message.");
1393 if (!(reply = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) {
1394 log_error("Failed to issue method call: %s", error.message);
1398 if (!dbus_message_iter_init(reply, &iter) ||
1399 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1400 log_error("Failed to parse reply.");
1404 dbus_message_iter_recurse(&iter, &sub);
1406 if (streq(interface, "org.freedesktop.systemd1.Unit")) {
1409 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1410 log_error("Failed to parse reply.");
1414 dbus_message_iter_get_basic(&sub, &id);
1415 printf("Unit %s changed.\n", id);
1419 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT32) {
1420 log_error("Failed to parse reply.");
1424 dbus_message_iter_get_basic(&sub, &id);
1425 printf("Job %u changed.\n", id);
1431 dbus_message_unref(m);
1434 dbus_message_unref(reply);
1436 dbus_error_free(&error);
1437 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1441 dbus_message_unref(m);
1444 dbus_message_unref(reply);
1446 dbus_error_free(&error);
1447 return DBUS_HANDLER_RESULT_NEED_MEMORY;
1450 static int monitor(DBusConnection *bus, char **args, unsigned n) {
1451 DBusMessage *m = NULL, *reply = NULL;
1455 dbus_error_init(&error);
1457 dbus_bus_add_match(bus,
1459 "sender='org.freedesktop.systemd1',"
1460 "interface='org.freedesktop.systemd1.Manager',"
1461 "path='/org/freedesktop/systemd1'",
1464 if (dbus_error_is_set(&error)) {
1465 log_error("Failed to add match: %s", error.message);
1470 dbus_bus_add_match(bus,
1472 "sender='org.freedesktop.systemd1',"
1473 "interface='org.freedesktop.systemd1.Unit',"
1477 if (dbus_error_is_set(&error)) {
1478 log_error("Failed to add match: %s", error.message);
1483 dbus_bus_add_match(bus,
1485 "sender='org.freedesktop.systemd1',"
1486 "interface='org.freedesktop.systemd1.Job',"
1490 if (dbus_error_is_set(&error)) {
1491 log_error("Failed to add match: %s", error.message);
1496 if (!dbus_connection_add_filter(bus, monitor_filter, NULL, NULL)) {
1497 log_error("Failed to add filter.");
1502 if (!(m = dbus_message_new_method_call(
1503 "org.freedesktop.systemd1",
1504 "/org/freedesktop/systemd1",
1505 "org.freedesktop.systemd1.Manager",
1507 log_error("Could not allocate message.");
1512 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1513 log_error("Failed to issue method call: %s", error.message);
1518 while (dbus_connection_read_write_dispatch(bus, -1))
1525 /* This is slightly dirty, since we don't undo the filter or the matches. */
1528 dbus_message_unref(m);
1531 dbus_message_unref(reply);
1533 dbus_error_free(&error);
1538 static int dump(DBusConnection *bus, char **args, unsigned n) {
1539 DBusMessage *m = NULL, *reply = NULL;
1544 dbus_error_init(&error);
1546 if (!(m = dbus_message_new_method_call(
1547 "org.freedesktop.systemd1",
1548 "/org/freedesktop/systemd1",
1549 "org.freedesktop.systemd1.Manager",
1551 log_error("Could not allocate message.");
1555 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1556 log_error("Failed to issue method call: %s", error.message);
1561 if (!dbus_message_get_args(reply, &error,
1562 DBUS_TYPE_STRING, &text,
1563 DBUS_TYPE_INVALID)) {
1564 log_error("Failed to parse reply: %s", error.message);
1569 fputs(text, stdout);
1575 dbus_message_unref(m);
1578 dbus_message_unref(reply);
1580 dbus_error_free(&error);
1585 static int snapshot(DBusConnection *bus, char **args, unsigned n) {
1586 DBusMessage *m = NULL, *reply = NULL;
1589 const char *name = "", *path, *id;
1590 dbus_bool_t cleanup = FALSE;
1591 DBusMessageIter iter, sub;
1593 *interface = "org.freedesktop.systemd1.Unit",
1596 dbus_error_init(&error);
1598 if (!(m = dbus_message_new_method_call(
1599 "org.freedesktop.systemd1",
1600 "/org/freedesktop/systemd1",
1601 "org.freedesktop.systemd1.Manager",
1602 "CreateSnapshot"))) {
1603 log_error("Could not allocate message.");
1610 if (!dbus_message_append_args(m,
1611 DBUS_TYPE_STRING, &name,
1612 DBUS_TYPE_BOOLEAN, &cleanup,
1613 DBUS_TYPE_INVALID)) {
1614 log_error("Could not append arguments to message.");
1619 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1620 log_error("Failed to issue method call: %s", error.message);
1625 if (!dbus_message_get_args(reply, &error,
1626 DBUS_TYPE_OBJECT_PATH, &path,
1627 DBUS_TYPE_INVALID)) {
1628 log_error("Failed to parse reply: %s", error.message);
1633 dbus_message_unref(m);
1634 if (!(m = dbus_message_new_method_call(
1635 "org.freedesktop.systemd1",
1637 "org.freedesktop.DBus.Properties",
1639 log_error("Could not allocate message.");
1643 if (!dbus_message_append_args(m,
1644 DBUS_TYPE_STRING, &interface,
1645 DBUS_TYPE_STRING, &property,
1646 DBUS_TYPE_INVALID)) {
1647 log_error("Could not append arguments to message.");
1652 dbus_message_unref(reply);
1653 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1654 log_error("Failed to issue method call: %s", error.message);
1659 if (!dbus_message_iter_init(reply, &iter) ||
1660 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1661 log_error("Failed to parse reply.");
1666 dbus_message_iter_recurse(&iter, &sub);
1668 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1669 log_error("Failed to parse reply.");
1674 dbus_message_iter_get_basic(&sub, &id);
1682 dbus_message_unref(m);
1685 dbus_message_unref(reply);
1687 dbus_error_free(&error);
1692 static int delete_snapshot(DBusConnection *bus, char **args, unsigned n) {
1693 DBusMessage *m = NULL, *reply = NULL;
1701 dbus_error_init(&error);
1703 for (i = 1; i < n; i++) {
1704 const char *path = NULL;
1706 if (!(m = dbus_message_new_method_call(
1707 "org.freedesktop.systemd1",
1708 "/org/freedesktop/systemd1",
1709 "org.freedesktop.systemd1.Manager",
1711 log_error("Could not allocate message.");
1716 if (!dbus_message_append_args(m,
1717 DBUS_TYPE_STRING, &args[i],
1718 DBUS_TYPE_INVALID)) {
1719 log_error("Could not append arguments to message.");
1724 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1725 log_error("Failed to issue method call: %s", error.message);
1730 if (!dbus_message_get_args(reply, &error,
1731 DBUS_TYPE_OBJECT_PATH, &path,
1732 DBUS_TYPE_INVALID)) {
1733 log_error("Failed to parse reply: %s", error.message);
1738 dbus_message_unref(m);
1739 if (!(m = dbus_message_new_method_call(
1740 "org.freedesktop.systemd1",
1742 "org.freedesktop.systemd1.Snapshot",
1744 log_error("Could not allocate message.");
1749 dbus_message_unref(reply);
1750 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1751 log_error("Failed to issue method call: %s", error.message);
1756 dbus_message_unref(m);
1757 dbus_message_unref(reply);
1765 dbus_message_unref(m);
1768 dbus_message_unref(reply);
1770 dbus_error_free(&error);
1775 static int clear_jobs(DBusConnection *bus, char **args, unsigned n) {
1776 DBusMessage *m = NULL, *reply = NULL;
1781 dbus_error_init(&error);
1783 if (arg_action == ACTION_RELOAD)
1785 else if (arg_action == ACTION_REEXEC)
1786 method = "Reexecute";
1788 assert(arg_action == ACTION_SYSTEMCTL);
1791 streq(args[0], "clear-jobs") ? "ClearJobs" :
1792 streq(args[0], "daemon-reload") ? "Reload" :
1793 streq(args[0], "daemon-reexec") ? "Reexecute" :
1797 if (!(m = dbus_message_new_method_call(
1798 "org.freedesktop.systemd1",
1799 "/org/freedesktop/systemd1",
1800 "org.freedesktop.systemd1.Manager",
1802 log_error("Could not allocate message.");
1806 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1808 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
1809 /* There's always a fallback possible for
1810 * legacy actions. */
1815 log_error("Failed to issue method call: %s", error.message);
1824 dbus_message_unref(m);
1827 dbus_message_unref(reply);
1829 dbus_error_free(&error);
1834 static int show_enviroment(DBusConnection *bus, char **args, unsigned n) {
1835 DBusMessage *m = NULL, *reply = NULL;
1837 DBusMessageIter iter, sub, sub2;
1840 *interface = "org.freedesktop.systemd1.Manager",
1841 *property = "Environment";
1843 dbus_error_init(&error);
1845 if (!(m = dbus_message_new_method_call(
1846 "org.freedesktop.systemd1",
1847 "/org/freedesktop/systemd1",
1848 "org.freedesktop.DBus.Properties",
1850 log_error("Could not allocate message.");
1854 if (!dbus_message_append_args(m,
1855 DBUS_TYPE_STRING, &interface,
1856 DBUS_TYPE_STRING, &property,
1857 DBUS_TYPE_INVALID)) {
1858 log_error("Could not append arguments to message.");
1863 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1864 log_error("Failed to issue method call: %s", error.message);
1869 if (!dbus_message_iter_init(reply, &iter) ||
1870 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1871 log_error("Failed to parse reply.");
1876 dbus_message_iter_recurse(&iter, &sub);
1878 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
1879 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
1880 log_error("Failed to parse reply.");
1885 dbus_message_iter_recurse(&sub, &sub2);
1887 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
1890 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
1891 log_error("Failed to parse reply.");
1896 dbus_message_iter_get_basic(&sub2, &text);
1897 printf("%s\n", text);
1899 dbus_message_iter_next(&sub2);
1906 dbus_message_unref(m);
1909 dbus_message_unref(reply);
1911 dbus_error_free(&error);
1916 static int set_environment(DBusConnection *bus, char **args, unsigned n) {
1917 DBusMessage *m = NULL, *reply = NULL;
1921 DBusMessageIter iter, sub;
1924 dbus_error_init(&error);
1926 method = streq(args[0], "set-environment")
1928 : "UnsetEnvironment";
1930 if (!(m = dbus_message_new_method_call(
1931 "org.freedesktop.systemd1",
1932 "/org/freedesktop/systemd1",
1933 "org.freedesktop.systemd1.Manager",
1936 log_error("Could not allocate message.");
1940 dbus_message_iter_init_append(m, &iter);
1942 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
1943 log_error("Could not append arguments to message.");
1948 for (i = 1; i < n; i++)
1949 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &args[i])) {
1950 log_error("Could not append arguments to message.");
1955 if (!dbus_message_iter_close_container(&iter, &sub)) {
1956 log_error("Could not append arguments to message.");
1961 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1962 log_error("Failed to issue method call: %s", error.message);
1971 dbus_message_unref(m);
1974 dbus_message_unref(reply);
1976 dbus_error_free(&error);
1981 static int systemctl_help(void) {
1983 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
1984 "Send control commands to the systemd manager.\n\n"
1985 " -h --help Show this help\n"
1986 " -t --type=TYPE List only units of a particular type\n"
1987 " -p --property=NAME Show only property by this name\n"
1988 " -a --all Show all units/properties, including dead/empty ones\n"
1989 " --replace When installing a new job, replace existing conflicting ones\n"
1990 " --system Connect to system bus\n"
1991 " --session Connect to session bus\n"
1992 " -q --quiet Suppress output\n"
1993 " --no-block Do not wait until operation finished\n"
1994 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
1996 " list-units List units\n"
1997 " start [NAME...] Start one or more units\n"
1998 " stop [NAME...] Stop one or more units\n"
1999 " restart [NAME...] Restart one or more units\n"
2000 " reload [NAME...] Reload one or more units\n"
2001 " isolate [NAME] Start one unit and stop all others\n"
2002 " check [NAME...] Check whether any of the passed units are active\n"
2003 " show [NAME...|JOB...] Show information about one or more units\n"
2004 " load [NAME...] Load one or more units\n"
2005 " list-jobs List jobs\n"
2006 " cancel [JOB...] Cancel one or more jobs\n"
2007 " clear-jobs Cancel all jobs\n"
2008 " monitor Monitor unit/job changes\n"
2009 " dump Dump server status\n"
2010 " snapshot [NAME] Create a snapshot\n"
2011 " delete [NAME...] Remove one or more snapshots\n"
2012 " daemon-reload Reload systemd manager configuration\n"
2013 " daemon-reexec Reexecute systemd manager\n"
2014 " daemon-exit Ask the systemd manager to quit\n"
2015 " show-environment Dump environment\n"
2016 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
2017 " unset-environment [NAME...] Unset one or more environment variables\n"
2018 " halt Shut down and halt the system\n"
2019 " poweroff Shut down and power-off the system\n"
2020 " reboot Shut down and reboot the system\n"
2021 " default Enter default mode\n"
2022 " rescue Enter rescue mode\n"
2023 " emergency Enter emergency mode\n",
2024 program_invocation_short_name);
2029 static int halt_help(void) {
2031 printf("%s [OPTIONS...]\n\n"
2032 "%s the system.\n\n"
2033 " --help Show this help\n"
2034 " --halt Halt the machine\n"
2035 " -p --poweroff Switch off the machine\n"
2036 " --reboot Reboot the machine\n"
2037 " -f --force Force immediate halt/power-off/reboot\n"
2038 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
2039 " -d --no-wtmp Don't write wtmp record\n"
2040 " -n --no-sync Don't sync before halt/power-off/reboot\n"
2041 " --no-wall Don't send wall message before halt/power-off/reboot\n",
2042 program_invocation_short_name,
2043 arg_action == ACTION_REBOOT ? "Reboot" :
2044 arg_action == ACTION_POWEROFF ? "Power off" :
2050 static int shutdown_help(void) {
2052 printf("%s [OPTIONS...] [now] [WALL...]\n\n"
2053 "Shut down the system.\n\n"
2054 " --help Show this help\n"
2055 " -H --halt Halt the machine\n"
2056 " -P --poweroff Power-off the machine\n"
2057 " -r --reboot Reboot the machine\n"
2058 " -h Equivalent to --poweroff, overriden by --halt\n"
2059 " -k Don't halt/power-off/reboot, just send warnings\n"
2060 " --no-wall Don't send wall message before halt/power-off/reboot\n",
2061 program_invocation_short_name);
2066 static int telinit_help(void) {
2068 printf("%s [OPTIONS...] {COMMAND}\n\n"
2069 "Send control commands to the init daemon.\n\n"
2070 " --help Show this help\n"
2071 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
2073 " 0 Power-off the machine\n"
2074 " 6 Reboot the machine\n"
2075 " 2, 3, 4, 5 Start runlevelX.target unit\n"
2076 " 1, s, S Enter rescue mode\n"
2077 " q, Q Reload init daemon configuration\n"
2078 " u, U Reexecute init daemon\n",
2079 program_invocation_short_name);
2084 static int runlevel_help(void) {
2086 printf("%s [OPTIONS...]\n\n"
2087 "Prints the previous and current runlevel of the init system.\n\n"
2088 " --help Show this help\n",
2089 program_invocation_short_name);
2094 static int systemctl_parse_argv(int argc, char *argv[]) {
2097 ARG_REPLACE = 0x100,
2104 static const struct option options[] = {
2105 { "help", no_argument, NULL, 'h' },
2106 { "type", required_argument, NULL, 't' },
2107 { "property", required_argument, NULL, 'p' },
2108 { "all", no_argument, NULL, 'a' },
2109 { "replace", no_argument, NULL, ARG_REPLACE },
2110 { "session", no_argument, NULL, ARG_SESSION },
2111 { "system", no_argument, NULL, ARG_SYSTEM },
2112 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
2113 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2114 { "quiet", no_argument, NULL, 'q' },
2115 { NULL, 0, NULL, 0 }
2123 while ((c = getopt_long(argc, argv, "ht:p:aq", options, NULL)) >= 0) {
2136 arg_property = optarg;
2138 /* If the user asked for a particular
2139 * property, show it to him, even if it is
2157 arg_session = false;
2161 arg_no_block = true;
2176 log_error("Unknown option code %c", c);
2184 static int halt_parse_argv(int argc, char *argv[]) {
2193 static const struct option options[] = {
2194 { "help", no_argument, NULL, ARG_HELP },
2195 { "halt", no_argument, NULL, ARG_HALT },
2196 { "poweroff", no_argument, NULL, 'p' },
2197 { "reboot", no_argument, NULL, ARG_REBOOT },
2198 { "force", no_argument, NULL, 'f' },
2199 { "wtmp-only", no_argument, NULL, 'w' },
2200 { "no-wtmp", no_argument, NULL, 'd' },
2201 { "no-sync", no_argument, NULL, 'n' },
2202 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2203 { NULL, 0, NULL, 0 }
2211 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
2212 if (runlevel == '0' || runlevel == '6')
2213 arg_immediate = true;
2215 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
2223 arg_action = ACTION_HALT;
2227 arg_action = ACTION_POWEROFF;
2231 arg_action = ACTION_REBOOT;
2235 arg_immediate = true;
2256 /* Compatibility nops */
2263 log_error("Unknown option code %c", c);
2268 if (optind < argc) {
2269 log_error("Too many arguments.");
2276 static int shutdown_parse_argv(int argc, char *argv[]) {
2283 static const struct option options[] = {
2284 { "help", no_argument, NULL, ARG_HELP },
2285 { "halt", no_argument, NULL, 'H' },
2286 { "poweroff", no_argument, NULL, 'P' },
2287 { "reboot", no_argument, NULL, 'r' },
2288 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2289 { NULL, 0, NULL, 0 }
2297 while ((c = getopt_long(argc, argv, "HPrhkt:a", options, NULL)) >= 0) {
2305 arg_action = ACTION_HALT;
2309 arg_action = ACTION_POWEROFF;
2313 arg_action = ACTION_REBOOT;
2317 if (arg_action != ACTION_HALT)
2318 arg_action = ACTION_POWEROFF;
2331 /* Compatibility nops */
2338 log_error("Unknown option code %c", c);
2343 if (argc > optind && !streq(argv[optind], "now"))
2344 log_warning("First argument '%s' isn't 'now'. Ignoring.", argv[optind]);
2346 /* We ignore the time argument */
2347 if (argc > optind + 1)
2348 arg_wall = argv + optind + 1;
2355 static int telinit_parse_argv(int argc, char *argv[]) {
2362 static const struct option options[] = {
2363 { "help", no_argument, NULL, ARG_HELP },
2364 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2365 { NULL, 0, NULL, 0 }
2368 static const struct {
2372 { '0', ACTION_POWEROFF },
2373 { '6', ACTION_REBOOT },
2374 { '1', ACTION_RESCUE },
2375 { '2', ACTION_RUNLEVEL2 },
2376 { '3', ACTION_RUNLEVEL3 },
2377 { '4', ACTION_RUNLEVEL4 },
2378 { '5', ACTION_RUNLEVEL5 },
2379 { 's', ACTION_RESCUE },
2380 { 'S', ACTION_RESCUE },
2381 { 'q', ACTION_RELOAD },
2382 { 'Q', ACTION_RELOAD },
2383 { 'u', ACTION_REEXEC },
2384 { 'U', ACTION_REEXEC }
2393 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
2408 log_error("Unknown option code %c", c);
2413 if (optind >= argc) {
2418 if (optind + 1 < argc) {
2419 log_error("Too many arguments.");
2423 if (strlen(argv[optind]) != 1) {
2424 log_error("Expected single character argument.");
2428 for (i = 0; i < ELEMENTSOF(table); i++)
2429 if (table[i].from == argv[optind][0])
2432 if (i >= ELEMENTSOF(table)) {
2433 log_error("Unknown command %s.", argv[optind]);
2437 arg_action = table[i].to;
2444 static int runlevel_parse_argv(int argc, char *argv[]) {
2450 static const struct option options[] = {
2451 { "help", no_argument, NULL, ARG_HELP },
2452 { NULL, 0, NULL, 0 }
2460 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
2471 log_error("Unknown option code %c", c);
2476 if (optind < argc) {
2477 log_error("Too many arguments.");
2484 static int parse_argv(int argc, char *argv[]) {
2488 if (program_invocation_short_name) {
2490 if (strstr(program_invocation_short_name, "halt")) {
2491 arg_action = ACTION_HALT;
2492 return halt_parse_argv(argc, argv);
2493 } else if (strstr(program_invocation_short_name, "poweroff")) {
2494 arg_action = ACTION_POWEROFF;
2495 return halt_parse_argv(argc, argv);
2496 } else if (strstr(program_invocation_short_name, "reboot")) {
2497 arg_action = ACTION_REBOOT;
2498 return halt_parse_argv(argc, argv);
2499 } else if (strstr(program_invocation_short_name, "shutdown")) {
2500 arg_action = ACTION_POWEROFF;
2501 return shutdown_parse_argv(argc, argv);
2502 } else if (strstr(program_invocation_short_name, "init")) {
2503 arg_action = ACTION_INVALID;
2504 return telinit_parse_argv(argc, argv);
2505 } else if (strstr(program_invocation_short_name, "runlevel")) {
2506 arg_action = ACTION_RUNLEVEL;
2507 return runlevel_parse_argv(argc, argv);
2511 arg_action = ACTION_SYSTEMCTL;
2512 return systemctl_parse_argv(argc, argv);
2515 static int action_to_runlevel(void) {
2517 static const char table[_ACTION_MAX] = {
2518 [ACTION_HALT] = '0',
2519 [ACTION_POWEROFF] = '0',
2520 [ACTION_REBOOT] = '6',
2521 [ACTION_RUNLEVEL2] = '2',
2522 [ACTION_RUNLEVEL3] = '3',
2523 [ACTION_RUNLEVEL4] = '4',
2524 [ACTION_RUNLEVEL5] = '5',
2525 [ACTION_RESCUE] = '1'
2528 assert(arg_action < _ACTION_MAX);
2530 return table[arg_action];
2533 static int talk_upstart(void) {
2534 DBusMessage *m = NULL, *reply = NULL;
2536 int previous, rl, r;
2538 env1_buf[] = "RUNLEVEL=X",
2539 env2_buf[] = "PREVLEVEL=X";
2540 char *env1 = env1_buf, *env2 = env2_buf;
2541 const char *emit = "runlevel";
2542 dbus_bool_t b_false = FALSE;
2543 DBusMessageIter iter, sub;
2544 DBusConnection *bus;
2546 dbus_error_init(&error);
2548 if (!(rl = action_to_runlevel()))
2551 if (utmp_get_runlevel(&previous, NULL) < 0)
2554 if (!(bus = dbus_connection_open("unix:abstract=/com/ubuntu/upstart", &error))) {
2555 if (dbus_error_has_name(&error, DBUS_ERROR_NO_SERVER)) {
2560 log_error("Failed to connect to Upstart bus: %s", error.message);
2565 if ((r = bus_check_peercred(bus)) < 0) {
2566 log_error("Failed to verify owner of bus.");
2570 if (!(m = dbus_message_new_method_call(
2571 "com.ubuntu.Upstart",
2572 "/com/ubuntu/Upstart",
2573 "com.ubuntu.Upstart0_6",
2576 log_error("Could not allocate message.");
2581 dbus_message_iter_init_append(m, &iter);
2583 env1_buf[sizeof(env1_buf)-2] = rl;
2584 env2_buf[sizeof(env2_buf)-2] = previous;
2586 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
2587 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
2588 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
2589 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
2590 !dbus_message_iter_close_container(&iter, &sub) ||
2591 !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
2592 log_error("Could not append arguments to message.");
2597 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2599 if (error_is_no_service(&error)) {
2604 log_error("Failed to issue method call: %s", error.message);
2613 dbus_message_unref(m);
2616 dbus_message_unref(reply);
2619 dbus_connection_unref(bus);
2621 dbus_error_free(&error);
2626 static int talk_initctl(void) {
2627 struct init_request request;
2631 if (!(rl = action_to_runlevel()))
2635 request.magic = INIT_MAGIC;
2636 request.sleeptime = 0;
2637 request.cmd = INIT_CMD_RUNLVL;
2638 request.runlevel = rl;
2640 if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
2642 if (errno == ENOENT)
2645 log_error("Failed to open "INIT_FIFO": %m");
2650 r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
2651 close_nointr_nofail(fd);
2654 log_error("Failed to write to "INIT_FIFO": %m");
2655 return errno ? -errno : -EIO;
2661 static int systemctl_main(DBusConnection *bus, int argc, char *argv[]) {
2663 static const struct {
2671 int (* const dispatch)(DBusConnection *bus, char **args, unsigned n);
2673 { "list-units", LESS, 1, list_units },
2674 { "list-jobs", EQUAL, 1, list_jobs },
2675 { "clear-jobs", EQUAL, 1, clear_jobs },
2676 { "load", MORE, 2, load_unit },
2677 { "cancel", MORE, 2, cancel_job },
2678 { "start", MORE, 2, start_unit },
2679 { "stop", MORE, 2, start_unit },
2680 { "reload", MORE, 2, start_unit },
2681 { "restart", MORE, 2, start_unit },
2682 { "isolate", EQUAL, 2, start_unit },
2683 { "check", MORE, 2, check_unit },
2684 { "show", MORE, 1, show },
2685 { "monitor", EQUAL, 1, monitor },
2686 { "dump", EQUAL, 1, dump },
2687 { "snapshot", LESS, 2, snapshot },
2688 { "delete", MORE, 2, delete_snapshot },
2689 { "daemon-reload", EQUAL, 1, clear_jobs },
2690 { "daemon-reexec", EQUAL, 1, clear_jobs },
2691 { "daemon-exit", EQUAL, 1, clear_jobs },
2692 { "show-environment", EQUAL, 1, show_enviroment },
2693 { "set-environment", MORE, 2, set_environment },
2694 { "unset-environment", MORE, 2, set_environment },
2695 { "halt", EQUAL, 1, start_special },
2696 { "poweroff", EQUAL, 1, start_special },
2697 { "reboot", EQUAL, 1, start_special },
2698 { "default", EQUAL, 1, start_special },
2699 { "rescue", EQUAL, 1, start_special },
2700 { "emergency", EQUAL, 1, start_special },
2710 left = argc - optind;
2713 /* Special rule: no arguments means "list-units" */
2716 if (streq(argv[optind], "help")) {
2721 for (i = 0; i < ELEMENTSOF(verbs); i++)
2722 if (streq(argv[optind], verbs[i].verb))
2725 if (i >= ELEMENTSOF(verbs)) {
2726 log_error("Unknown operation %s", argv[optind]);
2731 switch (verbs[i].argc_cmp) {
2734 if (left != verbs[i].argc) {
2735 log_error("Invalid number of arguments.");
2742 if (left < verbs[i].argc) {
2743 log_error("Too few arguments.");
2750 if (left > verbs[i].argc) {
2751 log_error("Too many arguments.");
2758 assert_not_reached("Unknown comparison operator.");
2761 return verbs[i].dispatch(bus, argv + optind, left);
2764 static int reload_with_fallback(DBusConnection *bus) {
2768 /* First, try systemd via D-Bus. */
2769 if ((r = clear_jobs(bus, NULL, 0)) > 0)
2773 /* Nothing else worked, so let's try signals */
2774 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
2776 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
2777 log_error("kill() failed: %m");
2784 static int start_with_fallback(DBusConnection *bus) {
2787 warn_wall(arg_action);
2790 /* First, try systemd via D-Bus. */
2791 if ((r = start_unit(bus, NULL, 0)) > 0)
2794 /* Hmm, talking to systemd via D-Bus didn't work. Then
2795 * let's try to talk to Upstart via D-Bus. */
2796 if ((r = talk_upstart()) > 0)
2800 /* Nothing else worked, so let's try
2802 if ((r = talk_initctl()) != 0)
2805 log_error("Failed to talk to init daemon.");
2809 static int halt_main(DBusConnection *bus) {
2813 return start_with_fallback(bus);
2816 if ((r = utmp_put_shutdown(0)) < 0)
2817 log_warning("Failed to write utmp record: %s", strerror(-r));
2825 /* Make sure C-A-D is handled by the kernel from this
2827 reboot(RB_ENABLE_CAD);
2829 switch (arg_action) {
2832 log_info("Halting");
2833 reboot(RB_HALT_SYSTEM);
2836 case ACTION_POWEROFF:
2837 log_info("Powering off");
2838 reboot(RB_POWER_OFF);
2842 log_info("Rebooting");
2843 reboot(RB_AUTOBOOT);
2847 assert_not_reached("Unknown halt action.");
2850 /* We should never reach this. */
2854 static int runlevel_main(void) {
2855 int r, runlevel, previous;
2857 if ((r = utmp_get_runlevel(&runlevel, &previous)) < 0) {
2863 previous <= 0 ? 'N' : previous,
2864 runlevel <= 0 ? 'N' : runlevel);
2869 int main(int argc, char*argv[]) {
2871 DBusConnection *bus = NULL;
2874 dbus_error_init(&error);
2876 log_parse_environment();
2878 if ((r = parse_argv(argc, argv)) < 0)
2885 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
2886 * let's shortcut this */
2887 if (arg_action == ACTION_RUNLEVEL) {
2888 retval = runlevel_main() < 0;
2892 /* If we are root, then let's not go via the bus */
2893 if (geteuid() == 0 && !arg_session) {
2894 bus = dbus_connection_open("unix:abstract=/org/freedesktop/systemd1/private", &error);
2896 if (bus && bus_check_peercred(bus) < 0) {
2897 log_error("Failed to verify owner of bus.");
2901 bus = dbus_bus_get(arg_session ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &error);
2904 dbus_connection_set_exit_on_disconnect(bus, FALSE);
2906 switch (arg_action) {
2908 case ACTION_SYSTEMCTL: {
2911 log_error("Failed to get D-Bus connection: %s", error.message);
2915 retval = systemctl_main(bus, argc, argv) < 0;
2920 case ACTION_POWEROFF:
2922 retval = halt_main(bus) < 0;
2925 case ACTION_RUNLEVEL2:
2926 case ACTION_RUNLEVEL3:
2927 case ACTION_RUNLEVEL4:
2928 case ACTION_RUNLEVEL5:
2930 case ACTION_EMERGENCY:
2931 case ACTION_DEFAULT:
2932 retval = start_with_fallback(bus) < 0;
2937 retval = reload_with_fallback(bus) < 0;
2940 case ACTION_INVALID:
2941 case ACTION_RUNLEVEL:
2943 assert_not_reached("Unknown action");
2949 dbus_connection_unref(bus);
2951 dbus_error_free(&error);