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 static void show_cgroup(const char *name) {
967 if (!startswith(name, "name=systemd:"))
970 if (asprintf(&fn, "/cgroup/systemd/%s/tasks", name + 13) < 0)
973 r = read_one_line_file(fn, &pids);
984 p += strspn(p, WHITESPACE);
987 ul = strtoul(p, &p, 0);
988 if (errno != 0 || ul <= 0)
991 get_process_cmdline((pid_t) ul, 60, &t);
992 printf("\t\t%lu %s\n", ul, strna(t));
999 typedef struct UnitStatusInfo {
1001 const char *load_state;
1002 const char *active_state;
1003 const char *sub_state;
1005 const char *description;
1007 const char *fragment_path;
1008 const char *default_control_group;
1013 const char *status_text;
1016 usec_t start_timestamp;
1017 usec_t exit_timestamp;
1019 int exit_code, exit_status;
1022 unsigned n_accepted;
1023 unsigned n_connections;
1026 const char *sysfs_path;
1028 /* Mount, Automount */
1035 static void print_status_info(UnitStatusInfo *i) {
1038 /* This shows pretty information about a unit. See
1039 * print_property() for a low-level property printer */
1041 printf("%s", strna(i->id));
1043 if (i->description && !streq_ptr(i->id, i->description))
1044 printf(" - %s", i->description);
1048 if (i->fragment_path)
1049 printf("\t Loaded: %s (%s)\n", strna(i->load_state), i->fragment_path);
1050 else if (streq_ptr(i->load_state, "failed"))
1051 printf("\t Loaded: " ANSI_HIGHLIGHT_ON "%s" ANSI_HIGHLIGHT_OFF "\n", strna(i->load_state));
1053 printf("\t Loaded: %s\n", strna(i->load_state));
1055 if (streq_ptr(i->active_state, "maintenance"))
1056 printf("\t Active: " ANSI_HIGHLIGHT_ON "%s (%s)" ANSI_HIGHLIGHT_OFF "\n",
1057 strna(i->active_state),
1058 strna(i->sub_state));
1060 printf("\t Active: %s (%s)\n",
1061 strna(i->active_state),
1062 strna(i->sub_state));
1065 printf("\t Device: %s\n", i->sysfs_path);
1067 printf("\t Where: %s\n", i->where);
1069 printf("\t What: %s\n", i->what);
1072 printf("\t Status: \"%s\"\n", i->status_text);
1074 if (i->id && endswith(i->id, ".socket"))
1075 printf("\tAccepted: %u; Connected: %u\n", i->n_accepted, i->n_connections);
1077 if (i->main_pid > 0 || i->control_pid > 0) {
1080 if (i->main_pid > 0) {
1081 printf(" Process: %u", (unsigned) i->main_pid);
1085 get_process_name(i->main_pid, &t);
1091 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
1093 if (i->exit_code == CLD_EXITED)
1094 printf("status=%i", i->exit_status);
1096 printf("signal=%s", strsignal(i->exit_status));
1101 if (i->main_pid > 0 && i->control_pid > 0)
1104 if (i->control_pid > 0) {
1107 printf(" Control: %u", (unsigned) i->control_pid);
1109 get_process_name(i->control_pid, &t);
1119 if (i->default_control_group) {
1120 printf("\t CGroup: %s\n", i->default_control_group);
1121 show_cgroup(i->default_control_group);
1125 static int status_property(const char *name, DBusMessageIter *iter, UnitStatusInfo *i) {
1127 switch (dbus_message_iter_get_arg_type(iter)) {
1129 case DBUS_TYPE_STRING: {
1132 dbus_message_iter_get_basic(iter, &s);
1135 if (streq(name, "Id"))
1137 else if (streq(name, "LoadState"))
1139 else if (streq(name, "ActiveState"))
1140 i->active_state = s;
1141 else if (streq(name, "SubState"))
1143 else if (streq(name, "Description"))
1145 else if (streq(name, "FragmentPath"))
1146 i->fragment_path = s;
1147 else if (streq(name, "DefaultControlGroup"))
1148 i->default_control_group = s;
1149 else if (streq(name, "StatusText"))
1151 else if (streq(name, "SysFSPath"))
1153 else if (streq(name, "Where"))
1155 else if (streq(name, "What"))
1162 case DBUS_TYPE_UINT32: {
1165 dbus_message_iter_get_basic(iter, &u);
1167 if (streq(name, "MainPID")) {
1169 i->main_pid = (pid_t) u;
1172 } else if (streq(name, "ControlPID"))
1173 i->control_pid = (pid_t) u;
1174 else if (streq(name, "ExecMainPID")) {
1176 i->main_pid = (pid_t) u;
1177 } else if (streq(name, "NAccepted"))
1179 else if (streq(name, "NConnections"))
1180 i->n_connections = u;
1185 case DBUS_TYPE_INT32: {
1188 dbus_message_iter_get_basic(iter, &j);
1190 if (streq(name, "ExecMainCode"))
1191 i->exit_code = (int) j;
1192 else if (streq(name, "ExecMainStatus"))
1193 i->exit_status = (int) j;
1198 case DBUS_TYPE_UINT64: {
1201 dbus_message_iter_get_basic(iter, &u);
1203 if (streq(name, "ExecMainStartTimestamp"))
1204 i->start_timestamp = (usec_t) u;
1205 else if (streq(name, "ExecMainExitTimestamp"))
1206 i->exit_timestamp = (usec_t) u;
1215 static int print_property(const char *name, DBusMessageIter *iter) {
1219 /* This is a low-level property printer, see
1220 * print_status_info() for the nicer output */
1222 if (arg_property && !streq(name, arg_property))
1225 switch (dbus_message_iter_get_arg_type(iter)) {
1227 case DBUS_TYPE_STRING: {
1229 dbus_message_iter_get_basic(iter, &s);
1231 if (arg_all || s[0])
1232 printf("%s=%s\n", name, s);
1237 case DBUS_TYPE_BOOLEAN: {
1239 dbus_message_iter_get_basic(iter, &b);
1240 printf("%s=%s\n", name, yes_no(b));
1245 case DBUS_TYPE_UINT64: {
1247 dbus_message_iter_get_basic(iter, &u);
1249 /* Yes, heuristics! But we can change this check
1250 * should it turn out to not be sufficient */
1252 if (strstr(name, "Timestamp")) {
1253 char timestamp[FORMAT_TIMESTAMP_MAX], *t;
1255 if ((t = format_timestamp(timestamp, sizeof(timestamp), u)) || arg_all)
1256 printf("%s=%s\n", name, strempty(t));
1257 } else if (strstr(name, "USec")) {
1258 char timespan[FORMAT_TIMESPAN_MAX];
1260 printf("%s=%s\n", name, format_timespan(timespan, sizeof(timespan), u));
1262 printf("%s=%llu\n", name, (unsigned long long) u);
1267 case DBUS_TYPE_UINT32: {
1269 dbus_message_iter_get_basic(iter, &u);
1271 if (strstr(name, "UMask") || strstr(name, "Mode"))
1272 printf("%s=%04o\n", name, u);
1274 printf("%s=%u\n", name, (unsigned) u);
1279 case DBUS_TYPE_INT32: {
1281 dbus_message_iter_get_basic(iter, &i);
1283 printf("%s=%i\n", name, (int) i);
1287 case DBUS_TYPE_STRUCT: {
1288 DBusMessageIter sub;
1289 dbus_message_iter_recurse(iter, &sub);
1291 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "Job")) {
1294 dbus_message_iter_get_basic(&sub, &u);
1297 printf("%s=%u\n", name, (unsigned) u);
1299 printf("%s=\n", name);
1302 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Unit")) {
1305 dbus_message_iter_get_basic(&sub, &s);
1307 if (arg_all || s[0])
1308 printf("%s=%s\n", name, s);
1316 case DBUS_TYPE_ARRAY:
1318 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
1319 DBusMessageIter sub;
1322 dbus_message_iter_recurse(iter, &sub);
1325 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1326 printf("%s=", name);
1328 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1331 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
1332 dbus_message_iter_get_basic(&sub, &s);
1333 printf("%s%s", space ? " " : "", s);
1336 dbus_message_iter_next(&sub);
1343 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_BYTE) {
1344 DBusMessageIter sub;
1346 dbus_message_iter_recurse(iter, &sub);
1349 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1350 printf("%s=", name);
1352 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1355 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_BYTE);
1356 dbus_message_iter_get_basic(&sub, &u);
1359 dbus_message_iter_next(&sub);
1366 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Paths")) {
1367 DBusMessageIter sub, sub2;
1369 dbus_message_iter_recurse(iter, &sub);
1371 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1372 const char *type, *path;
1374 dbus_message_iter_recurse(&sub, &sub2);
1376 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
1377 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0)
1378 printf("%s=%s\n", type, path);
1380 dbus_message_iter_next(&sub);
1384 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Timers")) {
1385 DBusMessageIter sub, sub2;
1387 dbus_message_iter_recurse(iter, &sub);
1389 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1391 uint64_t value, next_elapse;
1393 dbus_message_iter_recurse(&sub, &sub2);
1395 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &base, true) >= 0 &&
1396 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &value, true) >= 0 &&
1397 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &next_elapse, false) >= 0) {
1398 char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX];
1400 printf("%s={ value=%s ; next_elapse=%s }\n",
1402 format_timespan(timespan1, sizeof(timespan1), value),
1403 format_timespan(timespan2, sizeof(timespan2), next_elapse));
1406 dbus_message_iter_next(&sub);
1410 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && startswith(name, "Exec")) {
1412 DBusMessageIter sub, sub2, sub3;
1414 dbus_message_iter_recurse(iter, &sub);
1416 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1418 uint64_t start_time, exit_time;
1420 int32_t code, status;
1422 dbus_message_iter_recurse(&sub, &sub2);
1424 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0)
1427 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_ARRAY ||
1428 dbus_message_iter_get_element_type(&sub2) != DBUS_TYPE_STRING)
1431 printf("%s={ path=%s ; argv[]=", name, path);
1433 dbus_message_iter_recurse(&sub2, &sub3);
1435 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1438 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1439 dbus_message_iter_get_basic(&sub3, &s);
1441 dbus_message_iter_next(&sub3);
1444 if (dbus_message_iter_next(&sub2) &&
1445 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_time, true) >= 0 &&
1446 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_time, true) >= 0 &&
1447 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, true) >= 0 &&
1448 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &code, true) >= 0 &&
1449 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &status, false) >= 0) {
1451 char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
1453 printf("; start=%s ; stop=%s ; pid=%u ; code=%s ; status=%i/%s",
1454 strna(format_timestamp(timestamp1, sizeof(timestamp1), start_time)),
1455 strna(format_timestamp(timestamp2, sizeof(timestamp2), exit_time)),
1457 sigchld_code_to_string(code),
1459 strempty(code == CLD_EXITED ? NULL : strsignal(status)));
1464 dbus_message_iter_next(&sub);
1474 printf("%s=[unprintable]\n", name);
1479 static int show_one(DBusConnection *bus, const char *path, bool show_properties, bool *new_line) {
1480 DBusMessage *m = NULL, *reply = NULL;
1481 const char *interface = "";
1484 DBusMessageIter iter, sub, sub2, sub3;
1485 UnitStatusInfo info;
1492 dbus_error_init(&error);
1494 if (!(m = dbus_message_new_method_call(
1495 "org.freedesktop.systemd1",
1497 "org.freedesktop.DBus.Properties",
1499 log_error("Could not allocate message.");
1504 if (!dbus_message_append_args(m,
1505 DBUS_TYPE_STRING, &interface,
1506 DBUS_TYPE_INVALID)) {
1507 log_error("Could not append arguments to 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 if (!dbus_message_iter_init(reply, &iter) ||
1519 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
1520 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
1521 log_error("Failed to parse reply.");
1526 dbus_message_iter_recurse(&iter, &sub);
1533 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1536 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
1537 log_error("Failed to parse reply.");
1542 dbus_message_iter_recurse(&sub, &sub2);
1544 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
1545 log_error("Failed to parse reply.");
1550 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
1551 log_error("Failed to parse reply.");
1556 dbus_message_iter_recurse(&sub2, &sub3);
1558 if (show_properties)
1559 r = print_property(name, &sub3);
1561 r = status_property(name, &sub3, &info);
1564 log_error("Failed to parse reply.");
1569 dbus_message_iter_next(&sub);
1572 if (!show_properties)
1573 print_status_info(&info);
1579 dbus_message_unref(m);
1582 dbus_message_unref(reply);
1584 dbus_error_free(&error);
1589 static int show(DBusConnection *bus, char **args, unsigned n) {
1590 DBusMessage *m = NULL, *reply = NULL;
1594 bool show_properties, new_line = false;
1599 dbus_error_init(&error);
1601 show_properties = !streq(args[0], "status");
1603 if (show_properties && n <= 1) {
1604 /* If not argument is specified inspect the manager
1607 r = show_one(bus, "/org/freedesktop/systemd1", show_properties, &new_line);
1611 for (i = 1; i < n; i++) {
1612 const char *path = NULL;
1615 if (!show_properties || safe_atou32(args[i], &id) < 0) {
1617 if (!(m = dbus_message_new_method_call(
1618 "org.freedesktop.systemd1",
1619 "/org/freedesktop/systemd1",
1620 "org.freedesktop.systemd1.Manager",
1622 log_error("Could not allocate message.");
1627 if (!dbus_message_append_args(m,
1628 DBUS_TYPE_STRING, &args[i],
1629 DBUS_TYPE_INVALID)) {
1630 log_error("Could not append arguments to message.");
1637 if (!(m = dbus_message_new_method_call(
1638 "org.freedesktop.systemd1",
1639 "/org/freedesktop/systemd1",
1640 "org.freedesktop.systemd1.Manager",
1642 log_error("Could not allocate message.");
1647 if (!dbus_message_append_args(m,
1648 DBUS_TYPE_UINT32, &id,
1649 DBUS_TYPE_INVALID)) {
1650 log_error("Could not append arguments to message.");
1656 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1657 log_error("Failed to issue method call: %s", error.message);
1662 if (!dbus_message_get_args(reply, &error,
1663 DBUS_TYPE_OBJECT_PATH, &path,
1664 DBUS_TYPE_INVALID)) {
1665 log_error("Failed to parse reply: %s", error.message);
1670 if ((r = show_one(bus, path, show_properties, &new_line)) < 0)
1673 dbus_message_unref(m);
1674 dbus_message_unref(reply);
1682 dbus_message_unref(m);
1685 dbus_message_unref(reply);
1687 dbus_error_free(&error);
1692 static DBusHandlerResult monitor_filter(DBusConnection *connection, DBusMessage *message, void *data) {
1694 DBusMessage *m = NULL, *reply = NULL;
1699 dbus_error_init(&error);
1701 /* log_debug("Got D-Bus request: %s.%s() on %s", */
1702 /* dbus_message_get_interface(message), */
1703 /* dbus_message_get_member(message), */
1704 /* dbus_message_get_path(message)); */
1706 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
1707 log_error("Warning! D-Bus connection terminated.");
1708 dbus_connection_close(connection);
1710 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitNew") ||
1711 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitRemoved")) {
1712 const char *id, *path;
1714 if (!dbus_message_get_args(message, &error,
1715 DBUS_TYPE_STRING, &id,
1716 DBUS_TYPE_OBJECT_PATH, &path,
1718 log_error("Failed to parse message: %s", error.message);
1719 else if (streq(dbus_message_get_member(message), "UnitNew"))
1720 printf("Unit %s added.\n", id);
1722 printf("Unit %s removed.\n", id);
1724 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobNew") ||
1725 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
1729 if (!dbus_message_get_args(message, &error,
1730 DBUS_TYPE_UINT32, &id,
1731 DBUS_TYPE_OBJECT_PATH, &path,
1733 log_error("Failed to parse message: %s", error.message);
1734 else if (streq(dbus_message_get_member(message), "JobNew"))
1735 printf("Job %u added.\n", id);
1737 printf("Job %u removed.\n", id);
1740 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Unit", "Changed") ||
1741 dbus_message_is_signal(message, "org.freedesktop.systemd1.Job", "Changed")) {
1743 const char *path, *interface, *property = "Id";
1744 DBusMessageIter iter, sub;
1746 path = dbus_message_get_path(message);
1747 interface = dbus_message_get_interface(message);
1749 if (!(m = dbus_message_new_method_call(
1750 "org.freedesktop.systemd1",
1752 "org.freedesktop.DBus.Properties",
1754 log_error("Could not allocate message.");
1758 if (!dbus_message_append_args(m,
1759 DBUS_TYPE_STRING, &interface,
1760 DBUS_TYPE_STRING, &property,
1761 DBUS_TYPE_INVALID)) {
1762 log_error("Could not append arguments to message.");
1766 if (!(reply = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) {
1767 log_error("Failed to issue method call: %s", error.message);
1771 if (!dbus_message_iter_init(reply, &iter) ||
1772 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1773 log_error("Failed to parse reply.");
1777 dbus_message_iter_recurse(&iter, &sub);
1779 if (streq(interface, "org.freedesktop.systemd1.Unit")) {
1782 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1783 log_error("Failed to parse reply.");
1787 dbus_message_iter_get_basic(&sub, &id);
1788 printf("Unit %s changed.\n", id);
1792 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT32) {
1793 log_error("Failed to parse reply.");
1797 dbus_message_iter_get_basic(&sub, &id);
1798 printf("Job %u changed.\n", id);
1804 dbus_message_unref(m);
1807 dbus_message_unref(reply);
1809 dbus_error_free(&error);
1810 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1814 dbus_message_unref(m);
1817 dbus_message_unref(reply);
1819 dbus_error_free(&error);
1820 return DBUS_HANDLER_RESULT_NEED_MEMORY;
1823 static int monitor(DBusConnection *bus, char **args, unsigned n) {
1824 DBusMessage *m = NULL, *reply = NULL;
1828 dbus_error_init(&error);
1830 dbus_bus_add_match(bus,
1832 "sender='org.freedesktop.systemd1',"
1833 "interface='org.freedesktop.systemd1.Manager',"
1834 "path='/org/freedesktop/systemd1'",
1837 if (dbus_error_is_set(&error)) {
1838 log_error("Failed to add match: %s", error.message);
1843 dbus_bus_add_match(bus,
1845 "sender='org.freedesktop.systemd1',"
1846 "interface='org.freedesktop.systemd1.Unit',"
1850 if (dbus_error_is_set(&error)) {
1851 log_error("Failed to add match: %s", error.message);
1856 dbus_bus_add_match(bus,
1858 "sender='org.freedesktop.systemd1',"
1859 "interface='org.freedesktop.systemd1.Job',"
1863 if (dbus_error_is_set(&error)) {
1864 log_error("Failed to add match: %s", error.message);
1869 if (!dbus_connection_add_filter(bus, monitor_filter, NULL, NULL)) {
1870 log_error("Failed to add filter.");
1875 if (!(m = dbus_message_new_method_call(
1876 "org.freedesktop.systemd1",
1877 "/org/freedesktop/systemd1",
1878 "org.freedesktop.systemd1.Manager",
1880 log_error("Could not allocate message.");
1885 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1886 log_error("Failed to issue method call: %s", error.message);
1891 while (dbus_connection_read_write_dispatch(bus, -1))
1898 /* This is slightly dirty, since we don't undo the filter or the matches. */
1901 dbus_message_unref(m);
1904 dbus_message_unref(reply);
1906 dbus_error_free(&error);
1911 static int dump(DBusConnection *bus, char **args, unsigned n) {
1912 DBusMessage *m = NULL, *reply = NULL;
1917 dbus_error_init(&error);
1919 if (!(m = dbus_message_new_method_call(
1920 "org.freedesktop.systemd1",
1921 "/org/freedesktop/systemd1",
1922 "org.freedesktop.systemd1.Manager",
1924 log_error("Could not allocate message.");
1928 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1929 log_error("Failed to issue method call: %s", error.message);
1934 if (!dbus_message_get_args(reply, &error,
1935 DBUS_TYPE_STRING, &text,
1936 DBUS_TYPE_INVALID)) {
1937 log_error("Failed to parse reply: %s", error.message);
1942 fputs(text, stdout);
1948 dbus_message_unref(m);
1951 dbus_message_unref(reply);
1953 dbus_error_free(&error);
1958 static int snapshot(DBusConnection *bus, char **args, unsigned n) {
1959 DBusMessage *m = NULL, *reply = NULL;
1962 const char *name = "", *path, *id;
1963 dbus_bool_t cleanup = FALSE;
1964 DBusMessageIter iter, sub;
1966 *interface = "org.freedesktop.systemd1.Unit",
1969 dbus_error_init(&error);
1971 if (!(m = dbus_message_new_method_call(
1972 "org.freedesktop.systemd1",
1973 "/org/freedesktop/systemd1",
1974 "org.freedesktop.systemd1.Manager",
1975 "CreateSnapshot"))) {
1976 log_error("Could not allocate message.");
1983 if (!dbus_message_append_args(m,
1984 DBUS_TYPE_STRING, &name,
1985 DBUS_TYPE_BOOLEAN, &cleanup,
1986 DBUS_TYPE_INVALID)) {
1987 log_error("Could not append arguments to message.");
1992 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1993 log_error("Failed to issue method call: %s", error.message);
1998 if (!dbus_message_get_args(reply, &error,
1999 DBUS_TYPE_OBJECT_PATH, &path,
2000 DBUS_TYPE_INVALID)) {
2001 log_error("Failed to parse reply: %s", error.message);
2006 dbus_message_unref(m);
2007 if (!(m = dbus_message_new_method_call(
2008 "org.freedesktop.systemd1",
2010 "org.freedesktop.DBus.Properties",
2012 log_error("Could not allocate message.");
2016 if (!dbus_message_append_args(m,
2017 DBUS_TYPE_STRING, &interface,
2018 DBUS_TYPE_STRING, &property,
2019 DBUS_TYPE_INVALID)) {
2020 log_error("Could not append arguments to message.");
2025 dbus_message_unref(reply);
2026 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2027 log_error("Failed to issue method call: %s", error.message);
2032 if (!dbus_message_iter_init(reply, &iter) ||
2033 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2034 log_error("Failed to parse reply.");
2039 dbus_message_iter_recurse(&iter, &sub);
2041 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2042 log_error("Failed to parse reply.");
2047 dbus_message_iter_get_basic(&sub, &id);
2055 dbus_message_unref(m);
2058 dbus_message_unref(reply);
2060 dbus_error_free(&error);
2065 static int delete_snapshot(DBusConnection *bus, char **args, unsigned n) {
2066 DBusMessage *m = NULL, *reply = NULL;
2074 dbus_error_init(&error);
2076 for (i = 1; i < n; i++) {
2077 const char *path = NULL;
2079 if (!(m = dbus_message_new_method_call(
2080 "org.freedesktop.systemd1",
2081 "/org/freedesktop/systemd1",
2082 "org.freedesktop.systemd1.Manager",
2084 log_error("Could not allocate message.");
2089 if (!dbus_message_append_args(m,
2090 DBUS_TYPE_STRING, &args[i],
2091 DBUS_TYPE_INVALID)) {
2092 log_error("Could not append arguments to message.");
2097 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2098 log_error("Failed to issue method call: %s", error.message);
2103 if (!dbus_message_get_args(reply, &error,
2104 DBUS_TYPE_OBJECT_PATH, &path,
2105 DBUS_TYPE_INVALID)) {
2106 log_error("Failed to parse reply: %s", error.message);
2111 dbus_message_unref(m);
2112 if (!(m = dbus_message_new_method_call(
2113 "org.freedesktop.systemd1",
2115 "org.freedesktop.systemd1.Snapshot",
2117 log_error("Could not allocate message.");
2122 dbus_message_unref(reply);
2123 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2124 log_error("Failed to issue method call: %s", error.message);
2129 dbus_message_unref(m);
2130 dbus_message_unref(reply);
2138 dbus_message_unref(m);
2141 dbus_message_unref(reply);
2143 dbus_error_free(&error);
2148 static int clear_jobs(DBusConnection *bus, char **args, unsigned n) {
2149 DBusMessage *m = NULL, *reply = NULL;
2154 dbus_error_init(&error);
2156 if (arg_action == ACTION_RELOAD)
2158 else if (arg_action == ACTION_REEXEC)
2159 method = "Reexecute";
2161 assert(arg_action == ACTION_SYSTEMCTL);
2164 streq(args[0], "clear-jobs") ? "ClearJobs" :
2165 streq(args[0], "daemon-reload") ? "Reload" :
2166 streq(args[0], "daemon-reexec") ? "Reexecute" :
2170 if (!(m = dbus_message_new_method_call(
2171 "org.freedesktop.systemd1",
2172 "/org/freedesktop/systemd1",
2173 "org.freedesktop.systemd1.Manager",
2175 log_error("Could not allocate message.");
2179 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2181 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
2182 /* There's always a fallback possible for
2183 * legacy actions. */
2188 log_error("Failed to issue method call: %s", error.message);
2197 dbus_message_unref(m);
2200 dbus_message_unref(reply);
2202 dbus_error_free(&error);
2207 static int show_enviroment(DBusConnection *bus, char **args, unsigned n) {
2208 DBusMessage *m = NULL, *reply = NULL;
2210 DBusMessageIter iter, sub, sub2;
2213 *interface = "org.freedesktop.systemd1.Manager",
2214 *property = "Environment";
2216 dbus_error_init(&error);
2218 if (!(m = dbus_message_new_method_call(
2219 "org.freedesktop.systemd1",
2220 "/org/freedesktop/systemd1",
2221 "org.freedesktop.DBus.Properties",
2223 log_error("Could not allocate message.");
2227 if (!dbus_message_append_args(m,
2228 DBUS_TYPE_STRING, &interface,
2229 DBUS_TYPE_STRING, &property,
2230 DBUS_TYPE_INVALID)) {
2231 log_error("Could not append arguments to message.");
2236 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2237 log_error("Failed to issue method call: %s", error.message);
2242 if (!dbus_message_iter_init(reply, &iter) ||
2243 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2244 log_error("Failed to parse reply.");
2249 dbus_message_iter_recurse(&iter, &sub);
2251 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
2252 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
2253 log_error("Failed to parse reply.");
2258 dbus_message_iter_recurse(&sub, &sub2);
2260 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
2263 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
2264 log_error("Failed to parse reply.");
2269 dbus_message_iter_get_basic(&sub2, &text);
2270 printf("%s\n", text);
2272 dbus_message_iter_next(&sub2);
2279 dbus_message_unref(m);
2282 dbus_message_unref(reply);
2284 dbus_error_free(&error);
2289 static int set_environment(DBusConnection *bus, char **args, unsigned n) {
2290 DBusMessage *m = NULL, *reply = NULL;
2294 DBusMessageIter iter, sub;
2297 dbus_error_init(&error);
2299 method = streq(args[0], "set-environment")
2301 : "UnsetEnvironment";
2303 if (!(m = dbus_message_new_method_call(
2304 "org.freedesktop.systemd1",
2305 "/org/freedesktop/systemd1",
2306 "org.freedesktop.systemd1.Manager",
2309 log_error("Could not allocate message.");
2313 dbus_message_iter_init_append(m, &iter);
2315 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
2316 log_error("Could not append arguments to message.");
2321 for (i = 1; i < n; i++)
2322 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &args[i])) {
2323 log_error("Could not append arguments to message.");
2328 if (!dbus_message_iter_close_container(&iter, &sub)) {
2329 log_error("Could not append arguments to message.");
2334 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2335 log_error("Failed to issue method call: %s", error.message);
2344 dbus_message_unref(m);
2347 dbus_message_unref(reply);
2349 dbus_error_free(&error);
2354 static int systemctl_help(void) {
2356 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
2357 "Send control commands to the systemd manager.\n\n"
2358 " -h --help Show this help\n"
2359 " -t --type=TYPE List only units of a particular type\n"
2360 " -p --property=NAME Show only properties by this name\n"
2361 " -a --all Show all units/properties, including dead/empty ones\n"
2362 " --replace When installing a new job, replace existing conflicting ones\n"
2363 " --system Connect to system bus\n"
2364 " --session Connect to session bus\n"
2365 " -q --quiet Suppress output\n"
2366 " --no-block Do not wait until operation finished\n"
2367 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
2369 " list-units List units\n"
2370 " start [NAME...] Start one or more units\n"
2371 " stop [NAME...] Stop one or more units\n"
2372 " restart [NAME...] Restart one or more units\n"
2373 " reload [NAME...] Reload one or more units\n"
2374 " isolate [NAME] Start one unit and stop all others\n"
2375 " check [NAME...] Check whether any of the passed units are active\n"
2376 " status [NAME...] Show status of one or more units\n"
2377 " show [NAME...|JOB...] Show properties of one or more units/jobs/manager\n"
2378 " load [NAME...] Load one or more units\n"
2379 " list-jobs List jobs\n"
2380 " cancel [JOB...] Cancel one or more jobs\n"
2381 " clear-jobs Cancel all jobs\n"
2382 " monitor Monitor unit/job changes\n"
2383 " dump Dump server status\n"
2384 " snapshot [NAME] Create a snapshot\n"
2385 " delete [NAME...] Remove one or more snapshots\n"
2386 " daemon-reload Reload systemd manager configuration\n"
2387 " daemon-reexec Reexecute systemd manager\n"
2388 " daemon-exit Ask the systemd manager to quit\n"
2389 " show-environment Dump environment\n"
2390 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
2391 " unset-environment [NAME...] Unset one or more environment variables\n"
2392 " halt Shut down and halt the system\n"
2393 " poweroff Shut down and power-off the system\n"
2394 " reboot Shut down and reboot the system\n"
2395 " default Enter default mode\n"
2396 " rescue Enter rescue mode\n"
2397 " emergency Enter emergency mode\n",
2398 program_invocation_short_name);
2403 static int halt_help(void) {
2405 printf("%s [OPTIONS...]\n\n"
2406 "%s the system.\n\n"
2407 " --help Show this help\n"
2408 " --halt Halt the machine\n"
2409 " -p --poweroff Switch off the machine\n"
2410 " --reboot Reboot the machine\n"
2411 " -f --force Force immediate halt/power-off/reboot\n"
2412 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
2413 " -d --no-wtmp Don't write wtmp record\n"
2414 " -n --no-sync Don't sync before halt/power-off/reboot\n"
2415 " --no-wall Don't send wall message before halt/power-off/reboot\n",
2416 program_invocation_short_name,
2417 arg_action == ACTION_REBOOT ? "Reboot" :
2418 arg_action == ACTION_POWEROFF ? "Power off" :
2424 static int shutdown_help(void) {
2426 printf("%s [OPTIONS...] [now] [WALL...]\n\n"
2427 "Shut down the system.\n\n"
2428 " --help Show this help\n"
2429 " -H --halt Halt the machine\n"
2430 " -P --poweroff Power-off the machine\n"
2431 " -r --reboot Reboot the machine\n"
2432 " -h Equivalent to --poweroff, overriden by --halt\n"
2433 " -k Don't halt/power-off/reboot, just send warnings\n"
2434 " --no-wall Don't send wall message before halt/power-off/reboot\n",
2435 program_invocation_short_name);
2440 static int telinit_help(void) {
2442 printf("%s [OPTIONS...] {COMMAND}\n\n"
2443 "Send control commands to the init daemon.\n\n"
2444 " --help Show this help\n"
2445 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
2447 " 0 Power-off the machine\n"
2448 " 6 Reboot the machine\n"
2449 " 2, 3, 4, 5 Start runlevelX.target unit\n"
2450 " 1, s, S Enter rescue mode\n"
2451 " q, Q Reload init daemon configuration\n"
2452 " u, U Reexecute init daemon\n",
2453 program_invocation_short_name);
2458 static int runlevel_help(void) {
2460 printf("%s [OPTIONS...]\n\n"
2461 "Prints the previous and current runlevel of the init system.\n\n"
2462 " --help Show this help\n",
2463 program_invocation_short_name);
2468 static int systemctl_parse_argv(int argc, char *argv[]) {
2471 ARG_REPLACE = 0x100,
2478 static const struct option options[] = {
2479 { "help", no_argument, NULL, 'h' },
2480 { "type", required_argument, NULL, 't' },
2481 { "property", required_argument, NULL, 'p' },
2482 { "all", no_argument, NULL, 'a' },
2483 { "replace", no_argument, NULL, ARG_REPLACE },
2484 { "session", no_argument, NULL, ARG_SESSION },
2485 { "system", no_argument, NULL, ARG_SYSTEM },
2486 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
2487 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2488 { "quiet", no_argument, NULL, 'q' },
2489 { NULL, 0, NULL, 0 }
2497 while ((c = getopt_long(argc, argv, "ht:p:aq", options, NULL)) >= 0) {
2510 arg_property = optarg;
2512 /* If the user asked for a particular
2513 * property, show it to him, even if it is
2531 arg_session = false;
2535 arg_no_block = true;
2550 log_error("Unknown option code %c", c);
2558 static int halt_parse_argv(int argc, char *argv[]) {
2567 static const struct option options[] = {
2568 { "help", no_argument, NULL, ARG_HELP },
2569 { "halt", no_argument, NULL, ARG_HALT },
2570 { "poweroff", no_argument, NULL, 'p' },
2571 { "reboot", no_argument, NULL, ARG_REBOOT },
2572 { "force", no_argument, NULL, 'f' },
2573 { "wtmp-only", no_argument, NULL, 'w' },
2574 { "no-wtmp", no_argument, NULL, 'd' },
2575 { "no-sync", no_argument, NULL, 'n' },
2576 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2577 { NULL, 0, NULL, 0 }
2585 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
2586 if (runlevel == '0' || runlevel == '6')
2587 arg_immediate = true;
2589 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
2597 arg_action = ACTION_HALT;
2601 arg_action = ACTION_POWEROFF;
2605 arg_action = ACTION_REBOOT;
2609 arg_immediate = true;
2630 /* Compatibility nops */
2637 log_error("Unknown option code %c", c);
2642 if (optind < argc) {
2643 log_error("Too many arguments.");
2650 static int shutdown_parse_argv(int argc, char *argv[]) {
2657 static const struct option options[] = {
2658 { "help", no_argument, NULL, ARG_HELP },
2659 { "halt", no_argument, NULL, 'H' },
2660 { "poweroff", no_argument, NULL, 'P' },
2661 { "reboot", no_argument, NULL, 'r' },
2662 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2663 { NULL, 0, NULL, 0 }
2671 while ((c = getopt_long(argc, argv, "HPrhkt:a", options, NULL)) >= 0) {
2679 arg_action = ACTION_HALT;
2683 arg_action = ACTION_POWEROFF;
2687 arg_action = ACTION_REBOOT;
2691 if (arg_action != ACTION_HALT)
2692 arg_action = ACTION_POWEROFF;
2705 /* Compatibility nops */
2712 log_error("Unknown option code %c", c);
2717 if (argc > optind && !streq(argv[optind], "now"))
2718 log_warning("First argument '%s' isn't 'now'. Ignoring.", argv[optind]);
2720 /* We ignore the time argument */
2721 if (argc > optind + 1)
2722 arg_wall = argv + optind + 1;
2729 static int telinit_parse_argv(int argc, char *argv[]) {
2736 static const struct option options[] = {
2737 { "help", no_argument, NULL, ARG_HELP },
2738 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2739 { NULL, 0, NULL, 0 }
2742 static const struct {
2746 { '0', ACTION_POWEROFF },
2747 { '6', ACTION_REBOOT },
2748 { '1', ACTION_RESCUE },
2749 { '2', ACTION_RUNLEVEL2 },
2750 { '3', ACTION_RUNLEVEL3 },
2751 { '4', ACTION_RUNLEVEL4 },
2752 { '5', ACTION_RUNLEVEL5 },
2753 { 's', ACTION_RESCUE },
2754 { 'S', ACTION_RESCUE },
2755 { 'q', ACTION_RELOAD },
2756 { 'Q', ACTION_RELOAD },
2757 { 'u', ACTION_REEXEC },
2758 { 'U', ACTION_REEXEC }
2767 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
2782 log_error("Unknown option code %c", c);
2787 if (optind >= argc) {
2792 if (optind + 1 < argc) {
2793 log_error("Too many arguments.");
2797 if (strlen(argv[optind]) != 1) {
2798 log_error("Expected single character argument.");
2802 for (i = 0; i < ELEMENTSOF(table); i++)
2803 if (table[i].from == argv[optind][0])
2806 if (i >= ELEMENTSOF(table)) {
2807 log_error("Unknown command %s.", argv[optind]);
2811 arg_action = table[i].to;
2818 static int runlevel_parse_argv(int argc, char *argv[]) {
2824 static const struct option options[] = {
2825 { "help", no_argument, NULL, ARG_HELP },
2826 { NULL, 0, NULL, 0 }
2834 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
2845 log_error("Unknown option code %c", c);
2850 if (optind < argc) {
2851 log_error("Too many arguments.");
2858 static int parse_argv(int argc, char *argv[]) {
2862 if (program_invocation_short_name) {
2864 if (strstr(program_invocation_short_name, "halt")) {
2865 arg_action = ACTION_HALT;
2866 return halt_parse_argv(argc, argv);
2867 } else if (strstr(program_invocation_short_name, "poweroff")) {
2868 arg_action = ACTION_POWEROFF;
2869 return halt_parse_argv(argc, argv);
2870 } else if (strstr(program_invocation_short_name, "reboot")) {
2871 arg_action = ACTION_REBOOT;
2872 return halt_parse_argv(argc, argv);
2873 } else if (strstr(program_invocation_short_name, "shutdown")) {
2874 arg_action = ACTION_POWEROFF;
2875 return shutdown_parse_argv(argc, argv);
2876 } else if (strstr(program_invocation_short_name, "init")) {
2877 arg_action = ACTION_INVALID;
2878 return telinit_parse_argv(argc, argv);
2879 } else if (strstr(program_invocation_short_name, "runlevel")) {
2880 arg_action = ACTION_RUNLEVEL;
2881 return runlevel_parse_argv(argc, argv);
2885 arg_action = ACTION_SYSTEMCTL;
2886 return systemctl_parse_argv(argc, argv);
2889 static int action_to_runlevel(void) {
2891 static const char table[_ACTION_MAX] = {
2892 [ACTION_HALT] = '0',
2893 [ACTION_POWEROFF] = '0',
2894 [ACTION_REBOOT] = '6',
2895 [ACTION_RUNLEVEL2] = '2',
2896 [ACTION_RUNLEVEL3] = '3',
2897 [ACTION_RUNLEVEL4] = '4',
2898 [ACTION_RUNLEVEL5] = '5',
2899 [ACTION_RESCUE] = '1'
2902 assert(arg_action < _ACTION_MAX);
2904 return table[arg_action];
2907 static int talk_upstart(void) {
2908 DBusMessage *m = NULL, *reply = NULL;
2910 int previous, rl, r;
2912 env1_buf[] = "RUNLEVEL=X",
2913 env2_buf[] = "PREVLEVEL=X";
2914 char *env1 = env1_buf, *env2 = env2_buf;
2915 const char *emit = "runlevel";
2916 dbus_bool_t b_false = FALSE;
2917 DBusMessageIter iter, sub;
2918 DBusConnection *bus;
2920 dbus_error_init(&error);
2922 if (!(rl = action_to_runlevel()))
2925 if (utmp_get_runlevel(&previous, NULL) < 0)
2928 if (!(bus = dbus_connection_open("unix:abstract=/com/ubuntu/upstart", &error))) {
2929 if (dbus_error_has_name(&error, DBUS_ERROR_NO_SERVER)) {
2934 log_error("Failed to connect to Upstart bus: %s", error.message);
2939 if ((r = bus_check_peercred(bus)) < 0) {
2940 log_error("Failed to verify owner of bus.");
2944 if (!(m = dbus_message_new_method_call(
2945 "com.ubuntu.Upstart",
2946 "/com/ubuntu/Upstart",
2947 "com.ubuntu.Upstart0_6",
2950 log_error("Could not allocate message.");
2955 dbus_message_iter_init_append(m, &iter);
2957 env1_buf[sizeof(env1_buf)-2] = rl;
2958 env2_buf[sizeof(env2_buf)-2] = previous;
2960 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
2961 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
2962 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
2963 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
2964 !dbus_message_iter_close_container(&iter, &sub) ||
2965 !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
2966 log_error("Could not append arguments to message.");
2971 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2973 if (error_is_no_service(&error)) {
2978 log_error("Failed to issue method call: %s", error.message);
2987 dbus_message_unref(m);
2990 dbus_message_unref(reply);
2993 dbus_connection_unref(bus);
2995 dbus_error_free(&error);
3000 static int talk_initctl(void) {
3001 struct init_request request;
3005 if (!(rl = action_to_runlevel()))
3009 request.magic = INIT_MAGIC;
3010 request.sleeptime = 0;
3011 request.cmd = INIT_CMD_RUNLVL;
3012 request.runlevel = rl;
3014 if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
3016 if (errno == ENOENT)
3019 log_error("Failed to open "INIT_FIFO": %m");
3024 r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
3025 close_nointr_nofail(fd);
3028 log_error("Failed to write to "INIT_FIFO": %m");
3029 return errno ? -errno : -EIO;
3035 static int systemctl_main(DBusConnection *bus, int argc, char *argv[]) {
3037 static const struct {
3045 int (* const dispatch)(DBusConnection *bus, char **args, unsigned n);
3047 { "list-units", LESS, 1, list_units },
3048 { "list-jobs", EQUAL, 1, list_jobs },
3049 { "clear-jobs", EQUAL, 1, clear_jobs },
3050 { "load", MORE, 2, load_unit },
3051 { "cancel", MORE, 2, cancel_job },
3052 { "start", MORE, 2, start_unit },
3053 { "stop", MORE, 2, start_unit },
3054 { "reload", MORE, 2, start_unit },
3055 { "restart", MORE, 2, start_unit },
3056 { "isolate", EQUAL, 2, start_unit },
3057 { "check", MORE, 2, check_unit },
3058 { "show", MORE, 1, show },
3059 { "status", MORE, 2, show },
3060 { "monitor", EQUAL, 1, monitor },
3061 { "dump", EQUAL, 1, dump },
3062 { "snapshot", LESS, 2, snapshot },
3063 { "delete", MORE, 2, delete_snapshot },
3064 { "daemon-reload", EQUAL, 1, clear_jobs },
3065 { "daemon-reexec", EQUAL, 1, clear_jobs },
3066 { "daemon-exit", EQUAL, 1, clear_jobs },
3067 { "show-environment", EQUAL, 1, show_enviroment },
3068 { "set-environment", MORE, 2, set_environment },
3069 { "unset-environment", MORE, 2, set_environment },
3070 { "halt", EQUAL, 1, start_special },
3071 { "poweroff", EQUAL, 1, start_special },
3072 { "reboot", EQUAL, 1, start_special },
3073 { "default", EQUAL, 1, start_special },
3074 { "rescue", EQUAL, 1, start_special },
3075 { "emergency", EQUAL, 1, start_special },
3085 left = argc - optind;
3088 /* Special rule: no arguments means "list-units" */
3091 if (streq(argv[optind], "help")) {
3096 for (i = 0; i < ELEMENTSOF(verbs); i++)
3097 if (streq(argv[optind], verbs[i].verb))
3100 if (i >= ELEMENTSOF(verbs)) {
3101 log_error("Unknown operation %s", argv[optind]);
3106 switch (verbs[i].argc_cmp) {
3109 if (left != verbs[i].argc) {
3110 log_error("Invalid number of arguments.");
3117 if (left < verbs[i].argc) {
3118 log_error("Too few arguments.");
3125 if (left > verbs[i].argc) {
3126 log_error("Too many arguments.");
3133 assert_not_reached("Unknown comparison operator.");
3136 return verbs[i].dispatch(bus, argv + optind, left);
3139 static int reload_with_fallback(DBusConnection *bus) {
3143 /* First, try systemd via D-Bus. */
3144 if ((r = clear_jobs(bus, NULL, 0)) > 0)
3148 /* Nothing else worked, so let's try signals */
3149 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
3151 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
3152 log_error("kill() failed: %m");
3159 static int start_with_fallback(DBusConnection *bus) {
3162 warn_wall(arg_action);
3165 /* First, try systemd via D-Bus. */
3166 if ((r = start_unit(bus, NULL, 0)) > 0)
3169 /* Hmm, talking to systemd via D-Bus didn't work. Then
3170 * let's try to talk to Upstart via D-Bus. */
3171 if ((r = talk_upstart()) > 0)
3175 /* Nothing else worked, so let's try
3177 if ((r = talk_initctl()) != 0)
3180 log_error("Failed to talk to init daemon.");
3184 static int halt_main(DBusConnection *bus) {
3188 return start_with_fallback(bus);
3191 if ((r = utmp_put_shutdown(0)) < 0)
3192 log_warning("Failed to write utmp record: %s", strerror(-r));
3200 /* Make sure C-A-D is handled by the kernel from this
3202 reboot(RB_ENABLE_CAD);
3204 switch (arg_action) {
3207 log_info("Halting");
3208 reboot(RB_HALT_SYSTEM);
3211 case ACTION_POWEROFF:
3212 log_info("Powering off");
3213 reboot(RB_POWER_OFF);
3217 log_info("Rebooting");
3218 reboot(RB_AUTOBOOT);
3222 assert_not_reached("Unknown halt action.");
3225 /* We should never reach this. */
3229 static int runlevel_main(void) {
3230 int r, runlevel, previous;
3232 if ((r = utmp_get_runlevel(&runlevel, &previous)) < 0) {
3238 previous <= 0 ? 'N' : previous,
3239 runlevel <= 0 ? 'N' : runlevel);
3244 int main(int argc, char*argv[]) {
3246 DBusConnection *bus = NULL;
3249 dbus_error_init(&error);
3251 log_parse_environment();
3253 if ((r = parse_argv(argc, argv)) < 0)
3260 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
3261 * let's shortcut this */
3262 if (arg_action == ACTION_RUNLEVEL) {
3263 retval = runlevel_main() < 0;
3267 /* If we are root, then let's not go via the bus */
3268 if (geteuid() == 0 && !arg_session) {
3269 bus = dbus_connection_open("unix:abstract=/org/freedesktop/systemd1/private", &error);
3271 if (bus && bus_check_peercred(bus) < 0) {
3272 log_error("Failed to verify owner of bus.");
3276 bus = dbus_bus_get(arg_session ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &error);
3279 dbus_connection_set_exit_on_disconnect(bus, FALSE);
3281 switch (arg_action) {
3283 case ACTION_SYSTEMCTL: {
3286 log_error("Failed to get D-Bus connection: %s", error.message);
3290 retval = systemctl_main(bus, argc, argv) < 0;
3295 case ACTION_POWEROFF:
3297 retval = halt_main(bus) < 0;
3300 case ACTION_RUNLEVEL2:
3301 case ACTION_RUNLEVEL3:
3302 case ACTION_RUNLEVEL4:
3303 case ACTION_RUNLEVEL5:
3305 case ACTION_EMERGENCY:
3306 case ACTION_DEFAULT:
3307 retval = start_with_fallback(bus) < 0;
3312 retval = reload_with_fallback(bus) < 0;
3315 case ACTION_INVALID:
3316 case ACTION_RUNLEVEL:
3318 assert_not_reached("Unknown action");
3324 dbus_connection_unref(bus);
3326 dbus_error_free(&error);