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) {
589 dbus_error_init(&error);
590 dbus_bus_add_match(bus,
592 "sender='org.freedesktop.systemd1',"
593 "interface='org.freedesktop.systemd1.Manager',"
594 "member='JobRemoved',"
595 "path='/org/freedesktop/systemd1'",
598 if (dbus_error_is_set(&error)) {
599 log_error("Failed to add match: %s", error.message);
600 dbus_error_free(&error);
604 /* This is slightly dirty, since we don't undo the match registrations. */
608 static int wait_for_jobs(DBusConnection *bus, Set *s) {
619 if (!dbus_connection_add_filter(bus, wait_filter, &d, NULL)) {
620 log_error("Failed to add filter.");
625 while (!set_isempty(s) &&
626 dbus_connection_read_write_dispatch(bus, -1))
629 if (!arg_quiet && d.failed)
630 log_error("Job failed, see logs for details.");
632 r = d.failed ? -EIO : 0;
635 /* This is slightly dirty, since we don't undo the filter registration. */
640 static int start_unit_one(
647 DBusMessage *m = NULL, *reply = NULL;
655 assert(arg_no_block || s);
657 dbus_error_init(&error);
659 if (!(m = dbus_message_new_method_call(
660 "org.freedesktop.systemd1",
661 "/org/freedesktop/systemd1",
662 "org.freedesktop.systemd1.Manager",
664 log_error("Could not allocate message.");
669 if (!dbus_message_append_args(m,
670 DBUS_TYPE_STRING, &name,
671 DBUS_TYPE_STRING, &mode,
672 DBUS_TYPE_INVALID)) {
673 log_error("Could not append arguments to message.");
678 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
680 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
681 /* There's always a fallback possible for
687 log_error("Failed to issue method call: %s", error.message);
696 if (!dbus_message_get_args(reply, &error,
697 DBUS_TYPE_OBJECT_PATH, &path,
698 DBUS_TYPE_INVALID)) {
699 log_error("Failed to parse reply: %s", error.message);
704 if (!(p = strdup(path))) {
705 log_error("Failed to duplicate path.");
710 if ((r = set_put(s, p)) < 0) {
712 log_error("Failed to add path to set.");
721 dbus_message_unref(m);
724 dbus_message_unref(reply);
726 dbus_error_free(&error);
731 static enum action verb_to_action(const char *verb) {
732 if (streq(verb, "halt"))
734 else if (streq(verb, "poweroff"))
735 return ACTION_POWEROFF;
736 else if (streq(verb, "reboot"))
737 return ACTION_REBOOT;
738 else if (streq(verb, "rescue"))
739 return ACTION_RESCUE;
740 else if (streq(verb, "emergency"))
741 return ACTION_EMERGENCY;
742 else if (streq(verb, "default"))
743 return ACTION_DEFAULT;
745 return ACTION_INVALID;
748 static int start_unit(DBusConnection *bus, char **args, unsigned n) {
750 static const char * const table[_ACTION_MAX] = {
751 [ACTION_HALT] = SPECIAL_HALT_TARGET,
752 [ACTION_POWEROFF] = SPECIAL_POWEROFF_TARGET,
753 [ACTION_REBOOT] = SPECIAL_REBOOT_TARGET,
754 [ACTION_RUNLEVEL2] = SPECIAL_RUNLEVEL2_TARGET,
755 [ACTION_RUNLEVEL3] = SPECIAL_RUNLEVEL3_TARGET,
756 [ACTION_RUNLEVEL4] = SPECIAL_RUNLEVEL4_TARGET,
757 [ACTION_RUNLEVEL5] = SPECIAL_RUNLEVEL5_TARGET,
758 [ACTION_RESCUE] = SPECIAL_RESCUE_TARGET,
759 [ACTION_EMERGENCY] = SPECIAL_EMERGENCY_SERVICE,
760 [ACTION_DEFAULT] = SPECIAL_DEFAULT_TARGET
765 const char *method, *mode, *one_name;
770 if (arg_action == ACTION_SYSTEMCTL) {
772 streq(args[0], "stop") ? "StopUnit" :
773 streq(args[0], "reload") ? "ReloadUnit" :
774 streq(args[0], "restart") ? "RestartUnit" :
778 (streq(args[0], "isolate") ||
779 streq(args[0], "rescue") ||
780 streq(args[0], "emergency")) ? "isolate" :
781 arg_replace ? "replace" :
784 one_name = table[verb_to_action(args[0])];
787 assert(arg_action < ELEMENTSOF(table));
788 assert(table[arg_action]);
790 method = "StartUnit";
792 mode = (arg_action == ACTION_EMERGENCY ||
793 arg_action == ACTION_RESCUE) ? "isolate" : "replace";
795 one_name = table[arg_action];
799 if ((r = enable_wait_for_jobs(bus)) < 0) {
800 log_error("Could not watch jobs: %s", strerror(-r));
804 if (!(s = set_new(string_hash_func, string_compare_func))) {
805 log_error("Failed to allocate set.");
814 if ((r = start_unit_one(bus, method, one_name, mode, s)) <= 0)
817 for (i = 1; i < n; i++)
818 if ((r = start_unit_one(bus, method, args[i], mode, s)) < 0)
823 r = wait_for_jobs(bus, s);
832 static int start_special(DBusConnection *bus, char **args, unsigned n) {
836 warn_wall(verb_to_action(args[0]));
838 return start_unit(bus, args, n);
841 static int check_unit(DBusConnection *bus, char **args, unsigned n) {
842 DBusMessage *m = NULL, *reply = NULL;
844 *interface = "org.freedesktop.systemd1.Unit",
845 *property = "ActiveState";
846 int r = -EADDRNOTAVAIL;
853 dbus_error_init(&error);
855 for (i = 1; i < n; i++) {
856 const char *path = NULL;
858 DBusMessageIter iter, sub;
860 if (!(m = dbus_message_new_method_call(
861 "org.freedesktop.systemd1",
862 "/org/freedesktop/systemd1",
863 "org.freedesktop.systemd1.Manager",
865 log_error("Could not allocate message.");
870 if (!dbus_message_append_args(m,
871 DBUS_TYPE_STRING, &args[i],
872 DBUS_TYPE_INVALID)) {
873 log_error("Could not append arguments to message.");
878 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
880 /* Hmm, cannot figure out anything about this unit... */
887 if (!dbus_message_get_args(reply, &error,
888 DBUS_TYPE_OBJECT_PATH, &path,
889 DBUS_TYPE_INVALID)) {
890 log_error("Failed to parse reply: %s", error.message);
895 dbus_message_unref(m);
896 if (!(m = dbus_message_new_method_call(
897 "org.freedesktop.systemd1",
899 "org.freedesktop.DBus.Properties",
901 log_error("Could not allocate message.");
906 if (!dbus_message_append_args(m,
907 DBUS_TYPE_STRING, &interface,
908 DBUS_TYPE_STRING, &property,
909 DBUS_TYPE_INVALID)) {
910 log_error("Could not append arguments to message.");
915 dbus_message_unref(reply);
916 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
917 log_error("Failed to issue method call: %s", error.message);
922 if (!dbus_message_iter_init(reply, &iter) ||
923 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
924 log_error("Failed to parse reply.");
929 dbus_message_iter_recurse(&iter, &sub);
931 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
932 log_error("Failed to parse reply.");
937 dbus_message_iter_get_basic(&sub, &state);
942 if (streq(state, "active") || startswith(state, "reloading"))
945 dbus_message_unref(m);
946 dbus_message_unref(reply);
952 dbus_message_unref(m);
955 dbus_message_unref(reply);
957 dbus_error_free(&error);
962 typedef struct UnitStatusInfo {
964 const char *load_state;
965 const char *active_state;
966 const char *sub_state;
968 const char *description;
970 const char *fragment_path;
971 const char *default_control_group;
976 const char *status_text;
979 usec_t start_timestamp;
980 usec_t exit_timestamp;
982 int exit_code, exit_status;
986 unsigned n_connections;
989 const char *sysfs_path;
991 /* Mount, Automount */
998 static void print_status_info(UnitStatusInfo *i) {
1001 /* This shows pretty information about a unit. See
1002 * print_property() for a low-level property printer */
1004 printf("%s", strna(i->id));
1006 if (i->description && !streq_ptr(i->id, i->description))
1007 printf(" - %s", i->description);
1011 if (i->fragment_path)
1012 printf("\t Loaded: %s (%s)\n", strna(i->load_state), i->fragment_path);
1013 else if (streq_ptr(i->load_state, "failed"))
1014 printf("\t Loaded: " ANSI_HIGHLIGHT_ON "%s" ANSI_HIGHLIGHT_OFF "\n", strna(i->load_state));
1016 printf("\t Loaded: %s\n", strna(i->load_state));
1018 if (streq_ptr(i->active_state, "maintenance"))
1019 printf("\t Active: " ANSI_HIGHLIGHT_ON "%s (%s)" ANSI_HIGHLIGHT_OFF "\n",
1020 strna(i->active_state),
1021 strna(i->sub_state));
1023 printf("\t Active: %s (%s)\n",
1024 strna(i->active_state),
1025 strna(i->sub_state));
1028 printf("\t Device: %s\n", i->sysfs_path);
1030 printf("\t Where: %s\n", i->where);
1032 printf("\t What: %s\n", i->what);
1035 printf("\t Status: \"%s\"\n", i->status_text);
1037 if (i->id && endswith(i->id, ".socket"))
1038 printf("\tAccepted: %u; Connected: %u\n", i->n_accepted, i->n_connections);
1040 if (i->main_pid > 0 || i->control_pid > 0) {
1043 if (i->main_pid > 0) {
1044 printf(" Process: %u", (unsigned) i->main_pid);
1048 get_process_name(i->main_pid, &t);
1054 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
1056 if (i->exit_code == CLD_EXITED)
1057 printf("status=%i", i->exit_status);
1059 printf("signal=%s", strsignal(i->exit_status));
1064 if (i->main_pid > 0 && i->control_pid > 0)
1067 if (i->control_pid > 0) {
1070 printf(" Control: %u", (unsigned) i->control_pid);
1072 get_process_name(i->control_pid, &t);
1082 if (i->default_control_group)
1083 printf("\t CGroup: %s\n", i->default_control_group);
1086 static int status_property(const char *name, DBusMessageIter *iter, UnitStatusInfo *i) {
1088 switch (dbus_message_iter_get_arg_type(iter)) {
1090 case DBUS_TYPE_STRING: {
1093 dbus_message_iter_get_basic(iter, &s);
1096 if (streq(name, "Id"))
1098 else if (streq(name, "LoadState"))
1100 else if (streq(name, "ActiveState"))
1101 i->active_state = s;
1102 else if (streq(name, "SubState"))
1104 else if (streq(name, "Description"))
1106 else if (streq(name, "FragmentPath"))
1107 i->fragment_path = s;
1108 else if (streq(name, "DefaultControlGroup"))
1109 i->default_control_group = s;
1110 else if (streq(name, "StatusText"))
1112 else if (streq(name, "SysFSPath"))
1114 else if (streq(name, "Where"))
1116 else if (streq(name, "What"))
1123 case DBUS_TYPE_UINT32: {
1126 dbus_message_iter_get_basic(iter, &u);
1128 if (streq(name, "MainPID")) {
1130 i->main_pid = (pid_t) u;
1133 } else if (streq(name, "ControlPID"))
1134 i->control_pid = (pid_t) u;
1135 else if (streq(name, "ExecMainPID")) {
1137 i->main_pid = (pid_t) u;
1138 } else if (streq(name, "NAccepted"))
1140 else if (streq(name, "NConnections"))
1141 i->n_connections = u;
1146 case DBUS_TYPE_INT32: {
1149 dbus_message_iter_get_basic(iter, &j);
1151 if (streq(name, "ExecMainCode"))
1152 i->exit_code = (int) j;
1153 else if (streq(name, "ExecMainStatus"))
1154 i->exit_status = (int) j;
1159 case DBUS_TYPE_UINT64: {
1162 dbus_message_iter_get_basic(iter, &u);
1164 if (streq(name, "ExecMainStartTimestamp"))
1165 i->start_timestamp = (usec_t) u;
1166 else if (streq(name, "ExecMainExitTimestamp"))
1167 i->exit_timestamp = (usec_t) u;
1176 static int print_property(const char *name, DBusMessageIter *iter) {
1180 /* This is a low-level property printer, see
1181 * print_status_info() for the nicer output */
1183 if (arg_property && !streq(name, arg_property))
1186 switch (dbus_message_iter_get_arg_type(iter)) {
1188 case DBUS_TYPE_STRING: {
1190 dbus_message_iter_get_basic(iter, &s);
1192 if (arg_all || s[0])
1193 printf("%s=%s\n", name, s);
1198 case DBUS_TYPE_BOOLEAN: {
1200 dbus_message_iter_get_basic(iter, &b);
1201 printf("%s=%s\n", name, yes_no(b));
1206 case DBUS_TYPE_UINT64: {
1208 dbus_message_iter_get_basic(iter, &u);
1210 /* Yes, heuristics! But we can change this check
1211 * should it turn out to not be sufficient */
1213 if (strstr(name, "Timestamp")) {
1214 char timestamp[FORMAT_TIMESTAMP_MAX], *t;
1216 if ((t = format_timestamp(timestamp, sizeof(timestamp), u)) || arg_all)
1217 printf("%s=%s\n", name, strempty(t));
1218 } else if (strstr(name, "USec")) {
1219 char timespan[FORMAT_TIMESPAN_MAX];
1221 printf("%s=%s\n", name, format_timespan(timespan, sizeof(timespan), u));
1223 printf("%s=%llu\n", name, (unsigned long long) u);
1228 case DBUS_TYPE_UINT32: {
1230 dbus_message_iter_get_basic(iter, &u);
1232 if (strstr(name, "UMask") || strstr(name, "Mode"))
1233 printf("%s=%04o\n", name, u);
1235 printf("%s=%u\n", name, (unsigned) u);
1240 case DBUS_TYPE_INT32: {
1242 dbus_message_iter_get_basic(iter, &i);
1244 printf("%s=%i\n", name, (int) i);
1248 case DBUS_TYPE_STRUCT: {
1249 DBusMessageIter sub;
1250 dbus_message_iter_recurse(iter, &sub);
1252 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "Job")) {
1255 dbus_message_iter_get_basic(&sub, &u);
1258 printf("%s=%u\n", name, (unsigned) u);
1260 printf("%s=\n", name);
1263 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Unit")) {
1266 dbus_message_iter_get_basic(&sub, &s);
1268 if (arg_all || s[0])
1269 printf("%s=%s\n", name, s);
1277 case DBUS_TYPE_ARRAY:
1279 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
1280 DBusMessageIter sub;
1283 dbus_message_iter_recurse(iter, &sub);
1286 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1287 printf("%s=", name);
1289 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1292 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
1293 dbus_message_iter_get_basic(&sub, &s);
1294 printf("%s%s", space ? " " : "", s);
1297 dbus_message_iter_next(&sub);
1304 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_BYTE) {
1305 DBusMessageIter sub;
1307 dbus_message_iter_recurse(iter, &sub);
1310 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1311 printf("%s=", name);
1313 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1316 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_BYTE);
1317 dbus_message_iter_get_basic(&sub, &u);
1320 dbus_message_iter_next(&sub);
1327 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Paths")) {
1328 DBusMessageIter sub, sub2;
1330 dbus_message_iter_recurse(iter, &sub);
1332 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1333 const char *type, *path;
1335 dbus_message_iter_recurse(&sub, &sub2);
1337 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
1338 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0)
1339 printf("%s=%s\n", type, path);
1341 dbus_message_iter_next(&sub);
1345 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Timers")) {
1346 DBusMessageIter sub, sub2;
1348 dbus_message_iter_recurse(iter, &sub);
1350 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1352 uint64_t value, next_elapse;
1354 dbus_message_iter_recurse(&sub, &sub2);
1356 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &base, true) >= 0 &&
1357 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &value, true) >= 0 &&
1358 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &next_elapse, false) >= 0) {
1359 char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX];
1361 printf("%s={ value=%s ; next_elapse=%s }\n",
1363 format_timespan(timespan1, sizeof(timespan1), value),
1364 format_timespan(timespan2, sizeof(timespan2), next_elapse));
1367 dbus_message_iter_next(&sub);
1371 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && startswith(name, "Exec")) {
1373 DBusMessageIter sub, sub2, sub3;
1375 dbus_message_iter_recurse(iter, &sub);
1377 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1379 uint64_t start_time, exit_time;
1381 int32_t code, status;
1383 dbus_message_iter_recurse(&sub, &sub2);
1385 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0)
1388 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_ARRAY ||
1389 dbus_message_iter_get_element_type(&sub2) != DBUS_TYPE_STRING)
1392 printf("%s={ path=%s ; argv[]=", name, path);
1394 dbus_message_iter_recurse(&sub2, &sub3);
1396 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1399 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1400 dbus_message_iter_get_basic(&sub3, &s);
1402 dbus_message_iter_next(&sub3);
1405 if (dbus_message_iter_next(&sub2) &&
1406 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_time, true) >= 0 &&
1407 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_time, true) >= 0 &&
1408 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, true) >= 0 &&
1409 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &code, true) >= 0 &&
1410 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &status, false) >= 0) {
1412 char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
1414 printf("; start=%s ; stop=%s ; pid=%u ; code=%s ; status=%i/%s",
1415 strna(format_timestamp(timestamp1, sizeof(timestamp1), start_time)),
1416 strna(format_timestamp(timestamp2, sizeof(timestamp2), exit_time)),
1418 sigchld_code_to_string(code),
1420 strempty(code == CLD_EXITED ? NULL : strsignal(status)));
1425 dbus_message_iter_next(&sub);
1435 printf("%s=[unprintable]\n", name);
1440 static int show_one(DBusConnection *bus, const char *path, bool show_properties, bool *new_line) {
1441 DBusMessage *m = NULL, *reply = NULL;
1442 const char *interface = "";
1445 DBusMessageIter iter, sub, sub2, sub3;
1446 UnitStatusInfo info;
1453 dbus_error_init(&error);
1455 if (!(m = dbus_message_new_method_call(
1456 "org.freedesktop.systemd1",
1458 "org.freedesktop.DBus.Properties",
1460 log_error("Could not allocate message.");
1465 if (!dbus_message_append_args(m,
1466 DBUS_TYPE_STRING, &interface,
1467 DBUS_TYPE_INVALID)) {
1468 log_error("Could not append arguments to message.");
1473 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1474 log_error("Failed to issue method call: %s", error.message);
1479 if (!dbus_message_iter_init(reply, &iter) ||
1480 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
1481 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
1482 log_error("Failed to parse reply.");
1487 dbus_message_iter_recurse(&iter, &sub);
1494 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1497 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
1498 log_error("Failed to parse reply.");
1503 dbus_message_iter_recurse(&sub, &sub2);
1505 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
1506 log_error("Failed to parse reply.");
1511 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
1512 log_error("Failed to parse reply.");
1517 dbus_message_iter_recurse(&sub2, &sub3);
1519 if (show_properties)
1520 r = print_property(name, &sub3);
1522 r = status_property(name, &sub3, &info);
1525 log_error("Failed to parse reply.");
1530 dbus_message_iter_next(&sub);
1533 if (!show_properties)
1534 print_status_info(&info);
1540 dbus_message_unref(m);
1543 dbus_message_unref(reply);
1545 dbus_error_free(&error);
1550 static int show(DBusConnection *bus, char **args, unsigned n) {
1551 DBusMessage *m = NULL, *reply = NULL;
1555 bool show_properties, new_line = false;
1560 dbus_error_init(&error);
1562 show_properties = !streq(args[0], "status");
1564 if (show_properties && n <= 1) {
1565 /* If not argument is specified inspect the manager
1568 r = show_one(bus, "/org/freedesktop/systemd1", show_properties, &new_line);
1572 for (i = 1; i < n; i++) {
1573 const char *path = NULL;
1576 if (!show_properties || safe_atou32(args[i], &id) < 0) {
1578 if (!(m = dbus_message_new_method_call(
1579 "org.freedesktop.systemd1",
1580 "/org/freedesktop/systemd1",
1581 "org.freedesktop.systemd1.Manager",
1583 log_error("Could not allocate message.");
1588 if (!dbus_message_append_args(m,
1589 DBUS_TYPE_STRING, &args[i],
1590 DBUS_TYPE_INVALID)) {
1591 log_error("Could not append arguments to message.");
1598 if (!(m = dbus_message_new_method_call(
1599 "org.freedesktop.systemd1",
1600 "/org/freedesktop/systemd1",
1601 "org.freedesktop.systemd1.Manager",
1603 log_error("Could not allocate message.");
1608 if (!dbus_message_append_args(m,
1609 DBUS_TYPE_UINT32, &id,
1610 DBUS_TYPE_INVALID)) {
1611 log_error("Could not append arguments to message.");
1617 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1618 log_error("Failed to issue method call: %s", error.message);
1623 if (!dbus_message_get_args(reply, &error,
1624 DBUS_TYPE_OBJECT_PATH, &path,
1625 DBUS_TYPE_INVALID)) {
1626 log_error("Failed to parse reply: %s", error.message);
1631 if ((r = show_one(bus, path, show_properties, &new_line)) < 0)
1634 dbus_message_unref(m);
1635 dbus_message_unref(reply);
1643 dbus_message_unref(m);
1646 dbus_message_unref(reply);
1648 dbus_error_free(&error);
1653 static DBusHandlerResult monitor_filter(DBusConnection *connection, DBusMessage *message, void *data) {
1655 DBusMessage *m = NULL, *reply = NULL;
1660 dbus_error_init(&error);
1662 /* log_debug("Got D-Bus request: %s.%s() on %s", */
1663 /* dbus_message_get_interface(message), */
1664 /* dbus_message_get_member(message), */
1665 /* dbus_message_get_path(message)); */
1667 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
1668 log_error("Warning! D-Bus connection terminated.");
1669 dbus_connection_close(connection);
1671 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitNew") ||
1672 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitRemoved")) {
1673 const char *id, *path;
1675 if (!dbus_message_get_args(message, &error,
1676 DBUS_TYPE_STRING, &id,
1677 DBUS_TYPE_OBJECT_PATH, &path,
1679 log_error("Failed to parse message: %s", error.message);
1680 else if (streq(dbus_message_get_member(message), "UnitNew"))
1681 printf("Unit %s added.\n", id);
1683 printf("Unit %s removed.\n", id);
1685 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobNew") ||
1686 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
1690 if (!dbus_message_get_args(message, &error,
1691 DBUS_TYPE_UINT32, &id,
1692 DBUS_TYPE_OBJECT_PATH, &path,
1694 log_error("Failed to parse message: %s", error.message);
1695 else if (streq(dbus_message_get_member(message), "JobNew"))
1696 printf("Job %u added.\n", id);
1698 printf("Job %u removed.\n", id);
1701 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Unit", "Changed") ||
1702 dbus_message_is_signal(message, "org.freedesktop.systemd1.Job", "Changed")) {
1704 const char *path, *interface, *property = "Id";
1705 DBusMessageIter iter, sub;
1707 path = dbus_message_get_path(message);
1708 interface = dbus_message_get_interface(message);
1710 if (!(m = dbus_message_new_method_call(
1711 "org.freedesktop.systemd1",
1713 "org.freedesktop.DBus.Properties",
1715 log_error("Could not allocate message.");
1719 if (!dbus_message_append_args(m,
1720 DBUS_TYPE_STRING, &interface,
1721 DBUS_TYPE_STRING, &property,
1722 DBUS_TYPE_INVALID)) {
1723 log_error("Could not append arguments to message.");
1727 if (!(reply = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) {
1728 log_error("Failed to issue method call: %s", error.message);
1732 if (!dbus_message_iter_init(reply, &iter) ||
1733 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1734 log_error("Failed to parse reply.");
1738 dbus_message_iter_recurse(&iter, &sub);
1740 if (streq(interface, "org.freedesktop.systemd1.Unit")) {
1743 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1744 log_error("Failed to parse reply.");
1748 dbus_message_iter_get_basic(&sub, &id);
1749 printf("Unit %s changed.\n", id);
1753 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT32) {
1754 log_error("Failed to parse reply.");
1758 dbus_message_iter_get_basic(&sub, &id);
1759 printf("Job %u changed.\n", id);
1765 dbus_message_unref(m);
1768 dbus_message_unref(reply);
1770 dbus_error_free(&error);
1771 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1775 dbus_message_unref(m);
1778 dbus_message_unref(reply);
1780 dbus_error_free(&error);
1781 return DBUS_HANDLER_RESULT_NEED_MEMORY;
1784 static int monitor(DBusConnection *bus, char **args, unsigned n) {
1785 DBusMessage *m = NULL, *reply = NULL;
1789 dbus_error_init(&error);
1791 dbus_bus_add_match(bus,
1793 "sender='org.freedesktop.systemd1',"
1794 "interface='org.freedesktop.systemd1.Manager',"
1795 "path='/org/freedesktop/systemd1'",
1798 if (dbus_error_is_set(&error)) {
1799 log_error("Failed to add match: %s", error.message);
1804 dbus_bus_add_match(bus,
1806 "sender='org.freedesktop.systemd1',"
1807 "interface='org.freedesktop.systemd1.Unit',"
1811 if (dbus_error_is_set(&error)) {
1812 log_error("Failed to add match: %s", error.message);
1817 dbus_bus_add_match(bus,
1819 "sender='org.freedesktop.systemd1',"
1820 "interface='org.freedesktop.systemd1.Job',"
1824 if (dbus_error_is_set(&error)) {
1825 log_error("Failed to add match: %s", error.message);
1830 if (!dbus_connection_add_filter(bus, monitor_filter, NULL, NULL)) {
1831 log_error("Failed to add filter.");
1836 if (!(m = dbus_message_new_method_call(
1837 "org.freedesktop.systemd1",
1838 "/org/freedesktop/systemd1",
1839 "org.freedesktop.systemd1.Manager",
1841 log_error("Could not allocate message.");
1846 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1847 log_error("Failed to issue method call: %s", error.message);
1852 while (dbus_connection_read_write_dispatch(bus, -1))
1859 /* This is slightly dirty, since we don't undo the filter or the matches. */
1862 dbus_message_unref(m);
1865 dbus_message_unref(reply);
1867 dbus_error_free(&error);
1872 static int dump(DBusConnection *bus, char **args, unsigned n) {
1873 DBusMessage *m = NULL, *reply = NULL;
1878 dbus_error_init(&error);
1880 if (!(m = dbus_message_new_method_call(
1881 "org.freedesktop.systemd1",
1882 "/org/freedesktop/systemd1",
1883 "org.freedesktop.systemd1.Manager",
1885 log_error("Could not allocate message.");
1889 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1890 log_error("Failed to issue method call: %s", error.message);
1895 if (!dbus_message_get_args(reply, &error,
1896 DBUS_TYPE_STRING, &text,
1897 DBUS_TYPE_INVALID)) {
1898 log_error("Failed to parse reply: %s", error.message);
1903 fputs(text, stdout);
1909 dbus_message_unref(m);
1912 dbus_message_unref(reply);
1914 dbus_error_free(&error);
1919 static int snapshot(DBusConnection *bus, char **args, unsigned n) {
1920 DBusMessage *m = NULL, *reply = NULL;
1923 const char *name = "", *path, *id;
1924 dbus_bool_t cleanup = FALSE;
1925 DBusMessageIter iter, sub;
1927 *interface = "org.freedesktop.systemd1.Unit",
1930 dbus_error_init(&error);
1932 if (!(m = dbus_message_new_method_call(
1933 "org.freedesktop.systemd1",
1934 "/org/freedesktop/systemd1",
1935 "org.freedesktop.systemd1.Manager",
1936 "CreateSnapshot"))) {
1937 log_error("Could not allocate message.");
1944 if (!dbus_message_append_args(m,
1945 DBUS_TYPE_STRING, &name,
1946 DBUS_TYPE_BOOLEAN, &cleanup,
1947 DBUS_TYPE_INVALID)) {
1948 log_error("Could not append arguments to message.");
1953 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1954 log_error("Failed to issue method call: %s", error.message);
1959 if (!dbus_message_get_args(reply, &error,
1960 DBUS_TYPE_OBJECT_PATH, &path,
1961 DBUS_TYPE_INVALID)) {
1962 log_error("Failed to parse reply: %s", error.message);
1967 dbus_message_unref(m);
1968 if (!(m = dbus_message_new_method_call(
1969 "org.freedesktop.systemd1",
1971 "org.freedesktop.DBus.Properties",
1973 log_error("Could not allocate message.");
1977 if (!dbus_message_append_args(m,
1978 DBUS_TYPE_STRING, &interface,
1979 DBUS_TYPE_STRING, &property,
1980 DBUS_TYPE_INVALID)) {
1981 log_error("Could not append arguments to message.");
1986 dbus_message_unref(reply);
1987 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1988 log_error("Failed to issue method call: %s", error.message);
1993 if (!dbus_message_iter_init(reply, &iter) ||
1994 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1995 log_error("Failed to parse reply.");
2000 dbus_message_iter_recurse(&iter, &sub);
2002 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2003 log_error("Failed to parse reply.");
2008 dbus_message_iter_get_basic(&sub, &id);
2016 dbus_message_unref(m);
2019 dbus_message_unref(reply);
2021 dbus_error_free(&error);
2026 static int delete_snapshot(DBusConnection *bus, char **args, unsigned n) {
2027 DBusMessage *m = NULL, *reply = NULL;
2035 dbus_error_init(&error);
2037 for (i = 1; i < n; i++) {
2038 const char *path = NULL;
2040 if (!(m = dbus_message_new_method_call(
2041 "org.freedesktop.systemd1",
2042 "/org/freedesktop/systemd1",
2043 "org.freedesktop.systemd1.Manager",
2045 log_error("Could not allocate message.");
2050 if (!dbus_message_append_args(m,
2051 DBUS_TYPE_STRING, &args[i],
2052 DBUS_TYPE_INVALID)) {
2053 log_error("Could not append arguments to message.");
2058 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2059 log_error("Failed to issue method call: %s", error.message);
2064 if (!dbus_message_get_args(reply, &error,
2065 DBUS_TYPE_OBJECT_PATH, &path,
2066 DBUS_TYPE_INVALID)) {
2067 log_error("Failed to parse reply: %s", error.message);
2072 dbus_message_unref(m);
2073 if (!(m = dbus_message_new_method_call(
2074 "org.freedesktop.systemd1",
2076 "org.freedesktop.systemd1.Snapshot",
2078 log_error("Could not allocate message.");
2083 dbus_message_unref(reply);
2084 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2085 log_error("Failed to issue method call: %s", error.message);
2090 dbus_message_unref(m);
2091 dbus_message_unref(reply);
2099 dbus_message_unref(m);
2102 dbus_message_unref(reply);
2104 dbus_error_free(&error);
2109 static int clear_jobs(DBusConnection *bus, char **args, unsigned n) {
2110 DBusMessage *m = NULL, *reply = NULL;
2115 dbus_error_init(&error);
2117 if (arg_action == ACTION_RELOAD)
2119 else if (arg_action == ACTION_REEXEC)
2120 method = "Reexecute";
2122 assert(arg_action == ACTION_SYSTEMCTL);
2125 streq(args[0], "clear-jobs") ? "ClearJobs" :
2126 streq(args[0], "daemon-reload") ? "Reload" :
2127 streq(args[0], "daemon-reexec") ? "Reexecute" :
2131 if (!(m = dbus_message_new_method_call(
2132 "org.freedesktop.systemd1",
2133 "/org/freedesktop/systemd1",
2134 "org.freedesktop.systemd1.Manager",
2136 log_error("Could not allocate message.");
2140 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2142 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
2143 /* There's always a fallback possible for
2144 * legacy actions. */
2149 log_error("Failed to issue method call: %s", error.message);
2158 dbus_message_unref(m);
2161 dbus_message_unref(reply);
2163 dbus_error_free(&error);
2168 static int show_enviroment(DBusConnection *bus, char **args, unsigned n) {
2169 DBusMessage *m = NULL, *reply = NULL;
2171 DBusMessageIter iter, sub, sub2;
2174 *interface = "org.freedesktop.systemd1.Manager",
2175 *property = "Environment";
2177 dbus_error_init(&error);
2179 if (!(m = dbus_message_new_method_call(
2180 "org.freedesktop.systemd1",
2181 "/org/freedesktop/systemd1",
2182 "org.freedesktop.DBus.Properties",
2184 log_error("Could not allocate message.");
2188 if (!dbus_message_append_args(m,
2189 DBUS_TYPE_STRING, &interface,
2190 DBUS_TYPE_STRING, &property,
2191 DBUS_TYPE_INVALID)) {
2192 log_error("Could not append arguments to message.");
2197 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2198 log_error("Failed to issue method call: %s", error.message);
2203 if (!dbus_message_iter_init(reply, &iter) ||
2204 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2205 log_error("Failed to parse reply.");
2210 dbus_message_iter_recurse(&iter, &sub);
2212 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
2213 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
2214 log_error("Failed to parse reply.");
2219 dbus_message_iter_recurse(&sub, &sub2);
2221 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
2224 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
2225 log_error("Failed to parse reply.");
2230 dbus_message_iter_get_basic(&sub2, &text);
2231 printf("%s\n", text);
2233 dbus_message_iter_next(&sub2);
2240 dbus_message_unref(m);
2243 dbus_message_unref(reply);
2245 dbus_error_free(&error);
2250 static int set_environment(DBusConnection *bus, char **args, unsigned n) {
2251 DBusMessage *m = NULL, *reply = NULL;
2255 DBusMessageIter iter, sub;
2258 dbus_error_init(&error);
2260 method = streq(args[0], "set-environment")
2262 : "UnsetEnvironment";
2264 if (!(m = dbus_message_new_method_call(
2265 "org.freedesktop.systemd1",
2266 "/org/freedesktop/systemd1",
2267 "org.freedesktop.systemd1.Manager",
2270 log_error("Could not allocate message.");
2274 dbus_message_iter_init_append(m, &iter);
2276 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
2277 log_error("Could not append arguments to message.");
2282 for (i = 1; i < n; i++)
2283 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &args[i])) {
2284 log_error("Could not append arguments to message.");
2289 if (!dbus_message_iter_close_container(&iter, &sub)) {
2290 log_error("Could not append arguments to message.");
2295 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2296 log_error("Failed to issue method call: %s", error.message);
2305 dbus_message_unref(m);
2308 dbus_message_unref(reply);
2310 dbus_error_free(&error);
2315 static int systemctl_help(void) {
2317 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
2318 "Send control commands to the systemd manager.\n\n"
2319 " -h --help Show this help\n"
2320 " -t --type=TYPE List only units of a particular type\n"
2321 " -p --property=NAME Show only properties by this name\n"
2322 " -a --all Show all units/properties, including dead/empty ones\n"
2323 " --replace When installing a new job, replace existing conflicting ones\n"
2324 " --system Connect to system bus\n"
2325 " --session Connect to session bus\n"
2326 " -q --quiet Suppress output\n"
2327 " --no-block Do not wait until operation finished\n"
2328 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
2330 " list-units List units\n"
2331 " start [NAME...] Start one or more units\n"
2332 " stop [NAME...] Stop one or more units\n"
2333 " restart [NAME...] Restart one or more units\n"
2334 " reload [NAME...] Reload one or more units\n"
2335 " isolate [NAME] Start one unit and stop all others\n"
2336 " check [NAME...] Check whether any of the passed units are active\n"
2337 " status [NAME...] Show status of one or more units\n"
2338 " show [NAME...|JOB...] Show properties of one or more units/jobs/manager\n"
2339 " load [NAME...] Load one or more units\n"
2340 " list-jobs List jobs\n"
2341 " cancel [JOB...] Cancel one or more jobs\n"
2342 " clear-jobs Cancel all jobs\n"
2343 " monitor Monitor unit/job changes\n"
2344 " dump Dump server status\n"
2345 " snapshot [NAME] Create a snapshot\n"
2346 " delete [NAME...] Remove one or more snapshots\n"
2347 " daemon-reload Reload systemd manager configuration\n"
2348 " daemon-reexec Reexecute systemd manager\n"
2349 " daemon-exit Ask the systemd manager to quit\n"
2350 " show-environment Dump environment\n"
2351 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
2352 " unset-environment [NAME...] Unset one or more environment variables\n"
2353 " halt Shut down and halt the system\n"
2354 " poweroff Shut down and power-off the system\n"
2355 " reboot Shut down and reboot the system\n"
2356 " default Enter default mode\n"
2357 " rescue Enter rescue mode\n"
2358 " emergency Enter emergency mode\n",
2359 program_invocation_short_name);
2364 static int halt_help(void) {
2366 printf("%s [OPTIONS...]\n\n"
2367 "%s the system.\n\n"
2368 " --help Show this help\n"
2369 " --halt Halt the machine\n"
2370 " -p --poweroff Switch off the machine\n"
2371 " --reboot Reboot the machine\n"
2372 " -f --force Force immediate halt/power-off/reboot\n"
2373 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
2374 " -d --no-wtmp Don't write wtmp record\n"
2375 " -n --no-sync Don't sync before halt/power-off/reboot\n"
2376 " --no-wall Don't send wall message before halt/power-off/reboot\n",
2377 program_invocation_short_name,
2378 arg_action == ACTION_REBOOT ? "Reboot" :
2379 arg_action == ACTION_POWEROFF ? "Power off" :
2385 static int shutdown_help(void) {
2387 printf("%s [OPTIONS...] [now] [WALL...]\n\n"
2388 "Shut down the system.\n\n"
2389 " --help Show this help\n"
2390 " -H --halt Halt the machine\n"
2391 " -P --poweroff Power-off the machine\n"
2392 " -r --reboot Reboot the machine\n"
2393 " -h Equivalent to --poweroff, overriden by --halt\n"
2394 " -k Don't halt/power-off/reboot, just send warnings\n"
2395 " --no-wall Don't send wall message before halt/power-off/reboot\n",
2396 program_invocation_short_name);
2401 static int telinit_help(void) {
2403 printf("%s [OPTIONS...] {COMMAND}\n\n"
2404 "Send control commands to the init daemon.\n\n"
2405 " --help Show this help\n"
2406 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
2408 " 0 Power-off the machine\n"
2409 " 6 Reboot the machine\n"
2410 " 2, 3, 4, 5 Start runlevelX.target unit\n"
2411 " 1, s, S Enter rescue mode\n"
2412 " q, Q Reload init daemon configuration\n"
2413 " u, U Reexecute init daemon\n",
2414 program_invocation_short_name);
2419 static int runlevel_help(void) {
2421 printf("%s [OPTIONS...]\n\n"
2422 "Prints the previous and current runlevel of the init system.\n\n"
2423 " --help Show this help\n",
2424 program_invocation_short_name);
2429 static int systemctl_parse_argv(int argc, char *argv[]) {
2432 ARG_REPLACE = 0x100,
2439 static const struct option options[] = {
2440 { "help", no_argument, NULL, 'h' },
2441 { "type", required_argument, NULL, 't' },
2442 { "property", required_argument, NULL, 'p' },
2443 { "all", no_argument, NULL, 'a' },
2444 { "replace", no_argument, NULL, ARG_REPLACE },
2445 { "session", no_argument, NULL, ARG_SESSION },
2446 { "system", no_argument, NULL, ARG_SYSTEM },
2447 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
2448 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2449 { "quiet", no_argument, NULL, 'q' },
2450 { NULL, 0, NULL, 0 }
2458 while ((c = getopt_long(argc, argv, "ht:p:aq", options, NULL)) >= 0) {
2471 arg_property = optarg;
2473 /* If the user asked for a particular
2474 * property, show it to him, even if it is
2492 arg_session = false;
2496 arg_no_block = true;
2511 log_error("Unknown option code %c", c);
2519 static int halt_parse_argv(int argc, char *argv[]) {
2528 static const struct option options[] = {
2529 { "help", no_argument, NULL, ARG_HELP },
2530 { "halt", no_argument, NULL, ARG_HALT },
2531 { "poweroff", no_argument, NULL, 'p' },
2532 { "reboot", no_argument, NULL, ARG_REBOOT },
2533 { "force", no_argument, NULL, 'f' },
2534 { "wtmp-only", no_argument, NULL, 'w' },
2535 { "no-wtmp", no_argument, NULL, 'd' },
2536 { "no-sync", no_argument, NULL, 'n' },
2537 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2538 { NULL, 0, NULL, 0 }
2546 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
2547 if (runlevel == '0' || runlevel == '6')
2548 arg_immediate = true;
2550 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
2558 arg_action = ACTION_HALT;
2562 arg_action = ACTION_POWEROFF;
2566 arg_action = ACTION_REBOOT;
2570 arg_immediate = true;
2591 /* Compatibility nops */
2598 log_error("Unknown option code %c", c);
2603 if (optind < argc) {
2604 log_error("Too many arguments.");
2611 static int shutdown_parse_argv(int argc, char *argv[]) {
2618 static const struct option options[] = {
2619 { "help", no_argument, NULL, ARG_HELP },
2620 { "halt", no_argument, NULL, 'H' },
2621 { "poweroff", no_argument, NULL, 'P' },
2622 { "reboot", no_argument, NULL, 'r' },
2623 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2624 { NULL, 0, NULL, 0 }
2632 while ((c = getopt_long(argc, argv, "HPrhkt:a", options, NULL)) >= 0) {
2640 arg_action = ACTION_HALT;
2644 arg_action = ACTION_POWEROFF;
2648 arg_action = ACTION_REBOOT;
2652 if (arg_action != ACTION_HALT)
2653 arg_action = ACTION_POWEROFF;
2666 /* Compatibility nops */
2673 log_error("Unknown option code %c", c);
2678 if (argc > optind && !streq(argv[optind], "now"))
2679 log_warning("First argument '%s' isn't 'now'. Ignoring.", argv[optind]);
2681 /* We ignore the time argument */
2682 if (argc > optind + 1)
2683 arg_wall = argv + optind + 1;
2690 static int telinit_parse_argv(int argc, char *argv[]) {
2697 static const struct option options[] = {
2698 { "help", no_argument, NULL, ARG_HELP },
2699 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2700 { NULL, 0, NULL, 0 }
2703 static const struct {
2707 { '0', ACTION_POWEROFF },
2708 { '6', ACTION_REBOOT },
2709 { '1', ACTION_RESCUE },
2710 { '2', ACTION_RUNLEVEL2 },
2711 { '3', ACTION_RUNLEVEL3 },
2712 { '4', ACTION_RUNLEVEL4 },
2713 { '5', ACTION_RUNLEVEL5 },
2714 { 's', ACTION_RESCUE },
2715 { 'S', ACTION_RESCUE },
2716 { 'q', ACTION_RELOAD },
2717 { 'Q', ACTION_RELOAD },
2718 { 'u', ACTION_REEXEC },
2719 { 'U', ACTION_REEXEC }
2728 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
2743 log_error("Unknown option code %c", c);
2748 if (optind >= argc) {
2753 if (optind + 1 < argc) {
2754 log_error("Too many arguments.");
2758 if (strlen(argv[optind]) != 1) {
2759 log_error("Expected single character argument.");
2763 for (i = 0; i < ELEMENTSOF(table); i++)
2764 if (table[i].from == argv[optind][0])
2767 if (i >= ELEMENTSOF(table)) {
2768 log_error("Unknown command %s.", argv[optind]);
2772 arg_action = table[i].to;
2779 static int runlevel_parse_argv(int argc, char *argv[]) {
2785 static const struct option options[] = {
2786 { "help", no_argument, NULL, ARG_HELP },
2787 { NULL, 0, NULL, 0 }
2795 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
2806 log_error("Unknown option code %c", c);
2811 if (optind < argc) {
2812 log_error("Too many arguments.");
2819 static int parse_argv(int argc, char *argv[]) {
2823 if (program_invocation_short_name) {
2825 if (strstr(program_invocation_short_name, "halt")) {
2826 arg_action = ACTION_HALT;
2827 return halt_parse_argv(argc, argv);
2828 } else if (strstr(program_invocation_short_name, "poweroff")) {
2829 arg_action = ACTION_POWEROFF;
2830 return halt_parse_argv(argc, argv);
2831 } else if (strstr(program_invocation_short_name, "reboot")) {
2832 arg_action = ACTION_REBOOT;
2833 return halt_parse_argv(argc, argv);
2834 } else if (strstr(program_invocation_short_name, "shutdown")) {
2835 arg_action = ACTION_POWEROFF;
2836 return shutdown_parse_argv(argc, argv);
2837 } else if (strstr(program_invocation_short_name, "init")) {
2838 arg_action = ACTION_INVALID;
2839 return telinit_parse_argv(argc, argv);
2840 } else if (strstr(program_invocation_short_name, "runlevel")) {
2841 arg_action = ACTION_RUNLEVEL;
2842 return runlevel_parse_argv(argc, argv);
2846 arg_action = ACTION_SYSTEMCTL;
2847 return systemctl_parse_argv(argc, argv);
2850 static int action_to_runlevel(void) {
2852 static const char table[_ACTION_MAX] = {
2853 [ACTION_HALT] = '0',
2854 [ACTION_POWEROFF] = '0',
2855 [ACTION_REBOOT] = '6',
2856 [ACTION_RUNLEVEL2] = '2',
2857 [ACTION_RUNLEVEL3] = '3',
2858 [ACTION_RUNLEVEL4] = '4',
2859 [ACTION_RUNLEVEL5] = '5',
2860 [ACTION_RESCUE] = '1'
2863 assert(arg_action < _ACTION_MAX);
2865 return table[arg_action];
2868 static int talk_upstart(void) {
2869 DBusMessage *m = NULL, *reply = NULL;
2871 int previous, rl, r;
2873 env1_buf[] = "RUNLEVEL=X",
2874 env2_buf[] = "PREVLEVEL=X";
2875 char *env1 = env1_buf, *env2 = env2_buf;
2876 const char *emit = "runlevel";
2877 dbus_bool_t b_false = FALSE;
2878 DBusMessageIter iter, sub;
2879 DBusConnection *bus;
2881 dbus_error_init(&error);
2883 if (!(rl = action_to_runlevel()))
2886 if (utmp_get_runlevel(&previous, NULL) < 0)
2889 if (!(bus = dbus_connection_open("unix:abstract=/com/ubuntu/upstart", &error))) {
2890 if (dbus_error_has_name(&error, DBUS_ERROR_NO_SERVER)) {
2895 log_error("Failed to connect to Upstart bus: %s", error.message);
2900 if ((r = bus_check_peercred(bus)) < 0) {
2901 log_error("Failed to verify owner of bus.");
2905 if (!(m = dbus_message_new_method_call(
2906 "com.ubuntu.Upstart",
2907 "/com/ubuntu/Upstart",
2908 "com.ubuntu.Upstart0_6",
2911 log_error("Could not allocate message.");
2916 dbus_message_iter_init_append(m, &iter);
2918 env1_buf[sizeof(env1_buf)-2] = rl;
2919 env2_buf[sizeof(env2_buf)-2] = previous;
2921 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
2922 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
2923 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
2924 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
2925 !dbus_message_iter_close_container(&iter, &sub) ||
2926 !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
2927 log_error("Could not append arguments to message.");
2932 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2934 if (error_is_no_service(&error)) {
2939 log_error("Failed to issue method call: %s", error.message);
2948 dbus_message_unref(m);
2951 dbus_message_unref(reply);
2954 dbus_connection_unref(bus);
2956 dbus_error_free(&error);
2961 static int talk_initctl(void) {
2962 struct init_request request;
2966 if (!(rl = action_to_runlevel()))
2970 request.magic = INIT_MAGIC;
2971 request.sleeptime = 0;
2972 request.cmd = INIT_CMD_RUNLVL;
2973 request.runlevel = rl;
2975 if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
2977 if (errno == ENOENT)
2980 log_error("Failed to open "INIT_FIFO": %m");
2985 r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
2986 close_nointr_nofail(fd);
2989 log_error("Failed to write to "INIT_FIFO": %m");
2990 return errno ? -errno : -EIO;
2996 static int systemctl_main(DBusConnection *bus, int argc, char *argv[]) {
2998 static const struct {
3006 int (* const dispatch)(DBusConnection *bus, char **args, unsigned n);
3008 { "list-units", LESS, 1, list_units },
3009 { "list-jobs", EQUAL, 1, list_jobs },
3010 { "clear-jobs", EQUAL, 1, clear_jobs },
3011 { "load", MORE, 2, load_unit },
3012 { "cancel", MORE, 2, cancel_job },
3013 { "start", MORE, 2, start_unit },
3014 { "stop", MORE, 2, start_unit },
3015 { "reload", MORE, 2, start_unit },
3016 { "restart", MORE, 2, start_unit },
3017 { "isolate", EQUAL, 2, start_unit },
3018 { "check", MORE, 2, check_unit },
3019 { "show", MORE, 1, show },
3020 { "status", MORE, 2, show },
3021 { "monitor", EQUAL, 1, monitor },
3022 { "dump", EQUAL, 1, dump },
3023 { "snapshot", LESS, 2, snapshot },
3024 { "delete", MORE, 2, delete_snapshot },
3025 { "daemon-reload", EQUAL, 1, clear_jobs },
3026 { "daemon-reexec", EQUAL, 1, clear_jobs },
3027 { "daemon-exit", EQUAL, 1, clear_jobs },
3028 { "show-environment", EQUAL, 1, show_enviroment },
3029 { "set-environment", MORE, 2, set_environment },
3030 { "unset-environment", MORE, 2, set_environment },
3031 { "halt", EQUAL, 1, start_special },
3032 { "poweroff", EQUAL, 1, start_special },
3033 { "reboot", EQUAL, 1, start_special },
3034 { "default", EQUAL, 1, start_special },
3035 { "rescue", EQUAL, 1, start_special },
3036 { "emergency", EQUAL, 1, start_special },
3046 left = argc - optind;
3049 /* Special rule: no arguments means "list-units" */
3052 if (streq(argv[optind], "help")) {
3057 for (i = 0; i < ELEMENTSOF(verbs); i++)
3058 if (streq(argv[optind], verbs[i].verb))
3061 if (i >= ELEMENTSOF(verbs)) {
3062 log_error("Unknown operation %s", argv[optind]);
3067 switch (verbs[i].argc_cmp) {
3070 if (left != verbs[i].argc) {
3071 log_error("Invalid number of arguments.");
3078 if (left < verbs[i].argc) {
3079 log_error("Too few arguments.");
3086 if (left > verbs[i].argc) {
3087 log_error("Too many arguments.");
3094 assert_not_reached("Unknown comparison operator.");
3097 return verbs[i].dispatch(bus, argv + optind, left);
3100 static int reload_with_fallback(DBusConnection *bus) {
3104 /* First, try systemd via D-Bus. */
3105 if ((r = clear_jobs(bus, NULL, 0)) > 0)
3109 /* Nothing else worked, so let's try signals */
3110 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
3112 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
3113 log_error("kill() failed: %m");
3120 static int start_with_fallback(DBusConnection *bus) {
3123 warn_wall(arg_action);
3126 /* First, try systemd via D-Bus. */
3127 if ((r = start_unit(bus, NULL, 0)) > 0)
3130 /* Hmm, talking to systemd via D-Bus didn't work. Then
3131 * let's try to talk to Upstart via D-Bus. */
3132 if ((r = talk_upstart()) > 0)
3136 /* Nothing else worked, so let's try
3138 if ((r = talk_initctl()) != 0)
3141 log_error("Failed to talk to init daemon.");
3145 static int halt_main(DBusConnection *bus) {
3149 return start_with_fallback(bus);
3152 if ((r = utmp_put_shutdown(0)) < 0)
3153 log_warning("Failed to write utmp record: %s", strerror(-r));
3161 /* Make sure C-A-D is handled by the kernel from this
3163 reboot(RB_ENABLE_CAD);
3165 switch (arg_action) {
3168 log_info("Halting");
3169 reboot(RB_HALT_SYSTEM);
3172 case ACTION_POWEROFF:
3173 log_info("Powering off");
3174 reboot(RB_POWER_OFF);
3178 log_info("Rebooting");
3179 reboot(RB_AUTOBOOT);
3183 assert_not_reached("Unknown halt action.");
3186 /* We should never reach this. */
3190 static int runlevel_main(void) {
3191 int r, runlevel, previous;
3193 if ((r = utmp_get_runlevel(&runlevel, &previous)) < 0) {
3199 previous <= 0 ? 'N' : previous,
3200 runlevel <= 0 ? 'N' : runlevel);
3205 int main(int argc, char*argv[]) {
3207 DBusConnection *bus = NULL;
3210 dbus_error_init(&error);
3212 log_parse_environment();
3214 if ((r = parse_argv(argc, argv)) < 0)
3221 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
3222 * let's shortcut this */
3223 if (arg_action == ACTION_RUNLEVEL) {
3224 retval = runlevel_main() < 0;
3228 /* If we are root, then let's not go via the bus */
3229 if (geteuid() == 0 && !arg_session) {
3230 bus = dbus_connection_open("unix:abstract=/org/freedesktop/systemd1/private", &error);
3232 if (bus && bus_check_peercred(bus) < 0) {
3233 log_error("Failed to verify owner of bus.");
3237 bus = dbus_bus_get(arg_session ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &error);
3240 dbus_connection_set_exit_on_disconnect(bus, FALSE);
3242 switch (arg_action) {
3244 case ACTION_SYSTEMCTL: {
3247 log_error("Failed to get D-Bus connection: %s", error.message);
3251 retval = systemctl_main(bus, argc, argv) < 0;
3256 case ACTION_POWEROFF:
3258 retval = halt_main(bus) < 0;
3261 case ACTION_RUNLEVEL2:
3262 case ACTION_RUNLEVEL3:
3263 case ACTION_RUNLEVEL4:
3264 case ACTION_RUNLEVEL5:
3266 case ACTION_EMERGENCY:
3267 case ACTION_DEFAULT:
3268 retval = start_with_fallback(bus) < 0;
3273 retval = reload_with_fallback(bus) < 0;
3276 case ACTION_INVALID:
3277 case ACTION_RUNLEVEL:
3279 assert_not_reached("Unknown action");
3285 dbus_connection_unref(bus);
3287 dbus_error_free(&error);