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"
44 #include "dbus-common.h"
46 static const char *arg_type = NULL;
47 static const char *arg_property = NULL;
48 static bool arg_all = false;
49 static bool arg_replace = false;
50 static bool arg_session = false;
51 static bool arg_no_block = false;
52 static bool arg_immediate = false;
53 static bool arg_no_wtmp = false;
54 static bool arg_no_sync = false;
55 static bool arg_no_wall = false;
56 static bool arg_dry = false;
57 static bool arg_quiet = false;
58 static char **arg_wall = NULL;
76 } arg_action = ACTION_SYSTEMCTL;
78 static bool private_bus = false;
80 static bool error_is_no_service(DBusError *error) {
84 if (!dbus_error_is_set(error))
87 if (dbus_error_has_name(error, DBUS_ERROR_NAME_HAS_NO_OWNER))
90 if (dbus_error_has_name(error, DBUS_ERROR_SERVICE_UNKNOWN))
93 return startswith(error->name, "org.freedesktop.DBus.Error.Spawn.");
96 static int bus_iter_get_basic_and_next(DBusMessageIter *iter, int type, void *data, bool next) {
101 if (dbus_message_iter_get_arg_type(iter) != type)
104 dbus_message_iter_get_basic(iter, data);
106 if (!dbus_message_iter_next(iter) != !next)
112 static int columns(void) {
113 static int parsed_columns = 0;
116 if (parsed_columns > 0)
117 return parsed_columns;
119 if ((e = getenv("COLUMNS")))
120 parsed_columns = atoi(e);
122 if (parsed_columns <= 0) {
126 if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) >= 0)
127 parsed_columns = ws.ws_col;
130 if (parsed_columns <= 0)
133 return parsed_columns;
137 static void warn_wall(enum action action) {
138 static const char *table[_ACTION_MAX] = {
139 [ACTION_HALT] = "The system is going down for system halt NOW!",
140 [ACTION_REBOOT] = "The system is going down for reboot NOW!",
141 [ACTION_POWEROFF] = "The system is going down for power-off NOW!",
142 [ACTION_RESCUE] = "The system is going down to rescue mode NOW!",
143 [ACTION_EMERGENCY] = "The system is going down to emergency mode NOW!"
152 if (!(p = strv_join(arg_wall, " "))) {
153 log_error("Failed to join strings.");
169 utmp_wall(table[action]);
172 static int list_units(DBusConnection *bus, char **args, unsigned n) {
173 DBusMessage *m = NULL, *reply = NULL;
176 DBusMessageIter iter, sub, sub2;
179 dbus_error_init(&error);
183 if (!(m = dbus_message_new_method_call(
184 "org.freedesktop.systemd1",
185 "/org/freedesktop/systemd1",
186 "org.freedesktop.systemd1.Manager",
188 log_error("Could not allocate message.");
192 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
193 log_error("Failed to issue method call: %s", error.message);
198 if (!dbus_message_iter_init(reply, &iter) ||
199 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
200 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
201 log_error("Failed to parse reply.");
206 dbus_message_iter_recurse(&iter, &sub);
208 printf("%-45s %-6s %-12s %-12s %-15s %s\n", "UNIT", "LOAD", "ACTIVE", "SUB", "JOB", "DESCRIPTION");
210 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
211 const char *id, *description, *load_state, *active_state, *sub_state, *unit_state, *job_type, *job_path, *dot;
214 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
215 log_error("Failed to parse reply.");
220 dbus_message_iter_recurse(&sub, &sub2);
222 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
223 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &description, true) < 0 ||
224 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &load_state, true) < 0 ||
225 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &active_state, true) < 0 ||
226 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &sub_state, true) < 0 ||
227 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_state, true) < 0 ||
228 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &job_id, true) < 0 ||
229 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &job_type, true) < 0 ||
230 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, false) < 0) {
231 log_error("Failed to parse reply.");
236 if ((!arg_type || ((dot = strrchr(id, '.')) &&
237 streq(dot+1, arg_type))) &&
238 (arg_all || !streq(active_state, "inactive"))) {
242 if (streq(active_state, "maintenance"))
243 fputs(ANSI_HIGHLIGHT_ON, stdout);
245 printf("%-45s %-6s %-12s %-12s%n", id, load_state, active_state, sub_state, &a);
248 printf(" %-15s%n", job_type, &b);
252 if (a + b + 2 < columns()) {
256 printf(" %.*s", columns() - a - b - 2, description);
259 if (streq(active_state, "maintenance"))
260 fputs(ANSI_HIGHLIGHT_OFF, stdout);
266 dbus_message_iter_next(&sub);
270 printf("\n%u units listed.\n", k);
272 printf("\n%u live units listed. Pass --all to see dead units, too.\n", k);
278 dbus_message_unref(m);
281 dbus_message_unref(reply);
283 dbus_error_free(&error);
288 static int list_jobs(DBusConnection *bus, char **args, unsigned n) {
289 DBusMessage *m = NULL, *reply = NULL;
292 DBusMessageIter iter, sub, sub2;
295 dbus_error_init(&error);
299 if (!(m = dbus_message_new_method_call(
300 "org.freedesktop.systemd1",
301 "/org/freedesktop/systemd1",
302 "org.freedesktop.systemd1.Manager",
304 log_error("Could not allocate message.");
308 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
309 log_error("Failed to issue method call: %s", error.message);
314 if (!dbus_message_iter_init(reply, &iter) ||
315 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
316 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
317 log_error("Failed to parse reply.");
322 dbus_message_iter_recurse(&iter, &sub);
324 printf("%4s %-45s %-17s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
326 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
327 const char *name, *type, *state, *job_path, *unit_path;
330 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
331 log_error("Failed to parse reply.");
336 dbus_message_iter_recurse(&sub, &sub2);
338 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &id, true) < 0 ||
339 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0 ||
340 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
341 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, true) < 0 ||
342 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, true) < 0 ||
343 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, false) < 0) {
344 log_error("Failed to parse reply.");
349 printf("%4u %-45s %-17s %-7s\n", id, name, type, state);
352 dbus_message_iter_next(&sub);
355 printf("\n%u jobs listed.\n", k);
360 dbus_message_unref(m);
363 dbus_message_unref(reply);
365 dbus_error_free(&error);
370 static int load_unit(DBusConnection *bus, char **args, unsigned n) {
371 DBusMessage *m = NULL, *reply = NULL;
376 dbus_error_init(&error);
381 for (i = 1; i < n; i++) {
383 if (!(m = dbus_message_new_method_call(
384 "org.freedesktop.systemd1",
385 "/org/freedesktop/systemd1",
386 "org.freedesktop.systemd1.Manager",
388 log_error("Could not allocate message.");
393 if (!dbus_message_append_args(m,
394 DBUS_TYPE_STRING, &args[i],
395 DBUS_TYPE_INVALID)) {
396 log_error("Could not append arguments to message.");
401 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
402 log_error("Failed to issue method call: %s", error.message);
407 dbus_message_unref(m);
408 dbus_message_unref(reply);
417 dbus_message_unref(m);
420 dbus_message_unref(reply);
422 dbus_error_free(&error);
427 static int cancel_job(DBusConnection *bus, char **args, unsigned n) {
428 DBusMessage *m = NULL, *reply = NULL;
433 dbus_error_init(&error);
438 for (i = 1; i < n; i++) {
442 if (!(m = dbus_message_new_method_call(
443 "org.freedesktop.systemd1",
444 "/org/freedesktop/systemd1",
445 "org.freedesktop.systemd1.Manager",
447 log_error("Could not allocate message.");
452 if ((r = safe_atou(args[i], &id)) < 0) {
453 log_error("Failed to parse job id: %s", strerror(-r));
457 assert_cc(sizeof(uint32_t) == sizeof(id));
458 if (!dbus_message_append_args(m,
459 DBUS_TYPE_UINT32, &id,
460 DBUS_TYPE_INVALID)) {
461 log_error("Could not append arguments to message.");
466 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
467 log_error("Failed to issue method call: %s", error.message);
472 if (!dbus_message_get_args(reply, &error,
473 DBUS_TYPE_OBJECT_PATH, &path,
474 DBUS_TYPE_INVALID)) {
475 log_error("Failed to parse reply: %s", error.message);
480 dbus_message_unref(m);
481 if (!(m = dbus_message_new_method_call(
482 "org.freedesktop.systemd1",
484 "org.freedesktop.systemd1.Job",
486 log_error("Could not allocate message.");
491 dbus_message_unref(reply);
492 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
493 log_error("Failed to issue method call: %s", error.message);
498 dbus_message_unref(m);
499 dbus_message_unref(reply);
507 dbus_message_unref(m);
510 dbus_message_unref(reply);
512 dbus_error_free(&error);
517 typedef struct WaitData {
522 static DBusHandlerResult wait_filter(DBusConnection *connection, DBusMessage *message, void *data) {
530 dbus_error_init(&error);
532 log_debug("Got D-Bus request: %s.%s() on %s",
533 dbus_message_get_interface(message),
534 dbus_message_get_member(message),
535 dbus_message_get_path(message));
537 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
538 log_error("Warning! D-Bus connection terminated.");
539 dbus_connection_close(connection);
541 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
544 dbus_bool_t success = true;
546 if (!dbus_message_get_args(message, &error,
547 DBUS_TYPE_UINT32, &id,
548 DBUS_TYPE_OBJECT_PATH, &path,
549 DBUS_TYPE_BOOLEAN, &success,
551 log_error("Failed to parse message: %s", error.message);
555 if ((p = set_remove(d->set, (char*) path)))
563 dbus_error_free(&error);
564 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
567 static int enable_wait_for_jobs(DBusConnection *bus) {
575 dbus_error_init(&error);
576 dbus_bus_add_match(bus,
578 "sender='org.freedesktop.systemd1',"
579 "interface='org.freedesktop.systemd1.Manager',"
580 "member='JobRemoved',"
581 "path='/org/freedesktop/systemd1'",
584 if (dbus_error_is_set(&error)) {
585 log_error("Failed to add match: %s", error.message);
586 dbus_error_free(&error);
590 /* This is slightly dirty, since we don't undo the match registrations. */
594 static int wait_for_jobs(DBusConnection *bus, Set *s) {
605 if (!dbus_connection_add_filter(bus, wait_filter, &d, NULL)) {
606 log_error("Failed to add filter.");
611 while (!set_isempty(s) &&
612 dbus_connection_read_write_dispatch(bus, -1))
615 if (!arg_quiet && d.failed)
616 log_error("Job failed, see logs for details.");
618 r = d.failed ? -EIO : 0;
621 /* This is slightly dirty, since we don't undo the filter registration. */
626 static int start_unit_one(
633 DBusMessage *m = NULL, *reply = NULL;
641 assert(arg_no_block || s);
643 dbus_error_init(&error);
645 if (!(m = dbus_message_new_method_call(
646 "org.freedesktop.systemd1",
647 "/org/freedesktop/systemd1",
648 "org.freedesktop.systemd1.Manager",
650 log_error("Could not allocate message.");
655 if (!dbus_message_append_args(m,
656 DBUS_TYPE_STRING, &name,
657 DBUS_TYPE_STRING, &mode,
658 DBUS_TYPE_INVALID)) {
659 log_error("Could not append arguments to message.");
664 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
666 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
667 /* There's always a fallback possible for
673 log_error("Failed to issue method call: %s", error.message);
682 if (!dbus_message_get_args(reply, &error,
683 DBUS_TYPE_OBJECT_PATH, &path,
684 DBUS_TYPE_INVALID)) {
685 log_error("Failed to parse reply: %s", error.message);
690 if (!(p = strdup(path))) {
691 log_error("Failed to duplicate path.");
696 if ((r = set_put(s, p)) < 0) {
698 log_error("Failed to add path to set.");
707 dbus_message_unref(m);
710 dbus_message_unref(reply);
712 dbus_error_free(&error);
717 static enum action verb_to_action(const char *verb) {
718 if (streq(verb, "halt"))
720 else if (streq(verb, "poweroff"))
721 return ACTION_POWEROFF;
722 else if (streq(verb, "reboot"))
723 return ACTION_REBOOT;
724 else if (streq(verb, "rescue"))
725 return ACTION_RESCUE;
726 else if (streq(verb, "emergency"))
727 return ACTION_EMERGENCY;
728 else if (streq(verb, "default"))
729 return ACTION_DEFAULT;
731 return ACTION_INVALID;
734 static int start_unit(DBusConnection *bus, char **args, unsigned n) {
736 static const char * const table[_ACTION_MAX] = {
737 [ACTION_HALT] = SPECIAL_HALT_TARGET,
738 [ACTION_POWEROFF] = SPECIAL_POWEROFF_TARGET,
739 [ACTION_REBOOT] = SPECIAL_REBOOT_TARGET,
740 [ACTION_RUNLEVEL2] = SPECIAL_RUNLEVEL2_TARGET,
741 [ACTION_RUNLEVEL3] = SPECIAL_RUNLEVEL3_TARGET,
742 [ACTION_RUNLEVEL4] = SPECIAL_RUNLEVEL4_TARGET,
743 [ACTION_RUNLEVEL5] = SPECIAL_RUNLEVEL5_TARGET,
744 [ACTION_RESCUE] = SPECIAL_RESCUE_TARGET,
745 [ACTION_EMERGENCY] = SPECIAL_EMERGENCY_SERVICE,
746 [ACTION_DEFAULT] = SPECIAL_DEFAULT_TARGET
751 const char *method, *mode, *one_name;
756 if (arg_action == ACTION_SYSTEMCTL) {
758 streq(args[0], "stop") ? "StopUnit" :
759 streq(args[0], "reload") ? "ReloadUnit" :
760 streq(args[0], "restart") ? "RestartUnit" :
764 (streq(args[0], "isolate") ||
765 streq(args[0], "rescue") ||
766 streq(args[0], "emergency")) ? "isolate" :
767 arg_replace ? "replace" :
770 one_name = table[verb_to_action(args[0])];
773 assert(arg_action < ELEMENTSOF(table));
774 assert(table[arg_action]);
776 method = "StartUnit";
778 mode = (arg_action == ACTION_EMERGENCY ||
779 arg_action == ACTION_RESCUE) ? "isolate" : "replace";
781 one_name = table[arg_action];
785 if ((r = enable_wait_for_jobs(bus)) < 0) {
786 log_error("Could not watch jobs: %s", strerror(-r));
790 if (!(s = set_new(string_hash_func, string_compare_func))) {
791 log_error("Failed to allocate set.");
800 if ((r = start_unit_one(bus, method, one_name, mode, s)) <= 0)
803 for (i = 1; i < n; i++)
804 if ((r = start_unit_one(bus, method, args[i], mode, s)) < 0)
809 r = wait_for_jobs(bus, s);
818 static int start_special(DBusConnection *bus, char **args, unsigned n) {
822 warn_wall(verb_to_action(args[0]));
824 return start_unit(bus, args, n);
827 static int check_unit(DBusConnection *bus, char **args, unsigned n) {
828 DBusMessage *m = NULL, *reply = NULL;
830 *interface = "org.freedesktop.systemd1.Unit",
831 *property = "ActiveState";
832 int r = -EADDRNOTAVAIL;
839 dbus_error_init(&error);
841 for (i = 1; i < n; i++) {
842 const char *path = NULL;
844 DBusMessageIter iter, sub;
846 if (!(m = dbus_message_new_method_call(
847 "org.freedesktop.systemd1",
848 "/org/freedesktop/systemd1",
849 "org.freedesktop.systemd1.Manager",
851 log_error("Could not allocate message.");
856 if (!dbus_message_append_args(m,
857 DBUS_TYPE_STRING, &args[i],
858 DBUS_TYPE_INVALID)) {
859 log_error("Could not append arguments to message.");
864 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
866 /* Hmm, cannot figure out anything about this unit... */
870 dbus_error_free(&error);
874 if (!dbus_message_get_args(reply, &error,
875 DBUS_TYPE_OBJECT_PATH, &path,
876 DBUS_TYPE_INVALID)) {
877 log_error("Failed to parse reply: %s", error.message);
882 dbus_message_unref(m);
883 if (!(m = dbus_message_new_method_call(
884 "org.freedesktop.systemd1",
886 "org.freedesktop.DBus.Properties",
888 log_error("Could not allocate message.");
893 if (!dbus_message_append_args(m,
894 DBUS_TYPE_STRING, &interface,
895 DBUS_TYPE_STRING, &property,
896 DBUS_TYPE_INVALID)) {
897 log_error("Could not append arguments to message.");
902 dbus_message_unref(reply);
903 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
904 log_error("Failed to issue method call: %s", error.message);
909 if (!dbus_message_iter_init(reply, &iter) ||
910 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
911 log_error("Failed to parse reply.");
916 dbus_message_iter_recurse(&iter, &sub);
918 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
919 log_error("Failed to parse reply.");
924 dbus_message_iter_get_basic(&sub, &state);
929 if (streq(state, "active") || startswith(state, "reloading"))
932 dbus_message_unref(m);
933 dbus_message_unref(reply);
939 dbus_message_unref(m);
942 dbus_message_unref(reply);
944 dbus_error_free(&error);
949 static void show_cgroup(const char *name) {
954 if (!startswith(name, "name=systemd:"))
957 if (asprintf(&fn, "/cgroup/systemd/%s/tasks", name + 13) < 0)
969 if (fscanf(f, "%lu", &ul) != 1)
977 get_process_cmdline(last, 60, &t);
978 printf("\t\t \342\224\234 %lu %s\n", (unsigned long) last, strna(t));
981 printf("\t\t \342\224\202\n");
988 get_process_cmdline(last, 60, &t);
989 printf("\t\t \342\224\224 %lu %s\n", (unsigned long) last, strna(t));
996 typedef struct UnitStatusInfo {
998 const char *load_state;
999 const char *active_state;
1000 const char *sub_state;
1002 const char *description;
1004 const char *fragment_path;
1005 const char *default_control_group;
1010 const char *status_text;
1013 usec_t start_timestamp;
1014 usec_t exit_timestamp;
1016 int exit_code, exit_status;
1019 unsigned n_accepted;
1020 unsigned n_connections;
1023 const char *sysfs_path;
1025 /* Mount, Automount */
1032 static void print_status_info(UnitStatusInfo *i) {
1035 /* This shows pretty information about a unit. See
1036 * print_property() for a low-level property printer */
1038 printf("%s", strna(i->id));
1040 if (i->description && !streq_ptr(i->id, i->description))
1041 printf(" - %s", i->description);
1045 if (i->fragment_path)
1046 printf("\t Loaded: %s (%s)\n", strna(i->load_state), i->fragment_path);
1047 else if (streq_ptr(i->load_state, "failed"))
1048 printf("\t Loaded: " ANSI_HIGHLIGHT_ON "%s" ANSI_HIGHLIGHT_OFF "\n", strna(i->load_state));
1050 printf("\t Loaded: %s\n", strna(i->load_state));
1052 if (streq_ptr(i->active_state, "maintenance"))
1053 printf("\t Active: " ANSI_HIGHLIGHT_ON "%s (%s)" ANSI_HIGHLIGHT_OFF "\n",
1054 strna(i->active_state),
1055 strna(i->sub_state));
1057 printf("\t Active: %s (%s)\n",
1058 strna(i->active_state),
1059 strna(i->sub_state));
1062 printf("\t Device: %s\n", i->sysfs_path);
1064 printf("\t Where: %s\n", i->where);
1066 printf("\t What: %s\n", i->what);
1069 printf("\t Status: \"%s\"\n", i->status_text);
1071 if (i->id && endswith(i->id, ".socket"))
1072 printf("\tAccepted: %u; Connected: %u\n", i->n_accepted, i->n_connections);
1074 if (i->main_pid > 0 || i->control_pid > 0) {
1077 if (i->main_pid > 0) {
1078 printf(" Process: %u", (unsigned) i->main_pid);
1082 get_process_name(i->main_pid, &t);
1088 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
1090 if (i->exit_code == CLD_EXITED)
1091 printf("status=%i", i->exit_status);
1093 printf("signal=%s", strsignal(i->exit_status));
1098 if (i->main_pid > 0 && i->control_pid > 0)
1101 if (i->control_pid > 0) {
1104 printf(" Control: %u", (unsigned) i->control_pid);
1106 get_process_name(i->control_pid, &t);
1116 if (i->default_control_group) {
1117 printf("\t CGroup: %s\n", i->default_control_group);
1118 show_cgroup(i->default_control_group);
1122 static int status_property(const char *name, DBusMessageIter *iter, UnitStatusInfo *i) {
1124 switch (dbus_message_iter_get_arg_type(iter)) {
1126 case DBUS_TYPE_STRING: {
1129 dbus_message_iter_get_basic(iter, &s);
1132 if (streq(name, "Id"))
1134 else if (streq(name, "LoadState"))
1136 else if (streq(name, "ActiveState"))
1137 i->active_state = s;
1138 else if (streq(name, "SubState"))
1140 else if (streq(name, "Description"))
1142 else if (streq(name, "FragmentPath"))
1143 i->fragment_path = s;
1144 else if (streq(name, "DefaultControlGroup"))
1145 i->default_control_group = s;
1146 else if (streq(name, "StatusText"))
1148 else if (streq(name, "SysFSPath"))
1150 else if (streq(name, "Where"))
1152 else if (streq(name, "What"))
1159 case DBUS_TYPE_UINT32: {
1162 dbus_message_iter_get_basic(iter, &u);
1164 if (streq(name, "MainPID")) {
1166 i->main_pid = (pid_t) u;
1169 } else if (streq(name, "ControlPID"))
1170 i->control_pid = (pid_t) u;
1171 else if (streq(name, "ExecMainPID")) {
1173 i->main_pid = (pid_t) u;
1174 } else if (streq(name, "NAccepted"))
1176 else if (streq(name, "NConnections"))
1177 i->n_connections = u;
1182 case DBUS_TYPE_INT32: {
1185 dbus_message_iter_get_basic(iter, &j);
1187 if (streq(name, "ExecMainCode"))
1188 i->exit_code = (int) j;
1189 else if (streq(name, "ExecMainStatus"))
1190 i->exit_status = (int) j;
1195 case DBUS_TYPE_UINT64: {
1198 dbus_message_iter_get_basic(iter, &u);
1200 if (streq(name, "ExecMainStartTimestamp"))
1201 i->start_timestamp = (usec_t) u;
1202 else if (streq(name, "ExecMainExitTimestamp"))
1203 i->exit_timestamp = (usec_t) u;
1212 static int print_property(const char *name, DBusMessageIter *iter) {
1216 /* This is a low-level property printer, see
1217 * print_status_info() for the nicer output */
1219 if (arg_property && !streq(name, arg_property))
1222 switch (dbus_message_iter_get_arg_type(iter)) {
1224 case DBUS_TYPE_STRING: {
1226 dbus_message_iter_get_basic(iter, &s);
1228 if (arg_all || s[0])
1229 printf("%s=%s\n", name, s);
1234 case DBUS_TYPE_BOOLEAN: {
1236 dbus_message_iter_get_basic(iter, &b);
1237 printf("%s=%s\n", name, yes_no(b));
1242 case DBUS_TYPE_UINT64: {
1244 dbus_message_iter_get_basic(iter, &u);
1246 /* Yes, heuristics! But we can change this check
1247 * should it turn out to not be sufficient */
1249 if (strstr(name, "Timestamp")) {
1250 char timestamp[FORMAT_TIMESTAMP_MAX], *t;
1252 if ((t = format_timestamp(timestamp, sizeof(timestamp), u)) || arg_all)
1253 printf("%s=%s\n", name, strempty(t));
1254 } else if (strstr(name, "USec")) {
1255 char timespan[FORMAT_TIMESPAN_MAX];
1257 printf("%s=%s\n", name, format_timespan(timespan, sizeof(timespan), u));
1259 printf("%s=%llu\n", name, (unsigned long long) u);
1264 case DBUS_TYPE_UINT32: {
1266 dbus_message_iter_get_basic(iter, &u);
1268 if (strstr(name, "UMask") || strstr(name, "Mode"))
1269 printf("%s=%04o\n", name, u);
1271 printf("%s=%u\n", name, (unsigned) u);
1276 case DBUS_TYPE_INT32: {
1278 dbus_message_iter_get_basic(iter, &i);
1280 printf("%s=%i\n", name, (int) i);
1284 case DBUS_TYPE_STRUCT: {
1285 DBusMessageIter sub;
1286 dbus_message_iter_recurse(iter, &sub);
1288 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "Job")) {
1291 dbus_message_iter_get_basic(&sub, &u);
1294 printf("%s=%u\n", name, (unsigned) u);
1296 printf("%s=\n", name);
1299 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Unit")) {
1302 dbus_message_iter_get_basic(&sub, &s);
1304 if (arg_all || s[0])
1305 printf("%s=%s\n", name, s);
1313 case DBUS_TYPE_ARRAY:
1315 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
1316 DBusMessageIter sub;
1319 dbus_message_iter_recurse(iter, &sub);
1322 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1323 printf("%s=", name);
1325 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1328 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
1329 dbus_message_iter_get_basic(&sub, &s);
1330 printf("%s%s", space ? " " : "", s);
1333 dbus_message_iter_next(&sub);
1340 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_BYTE) {
1341 DBusMessageIter sub;
1343 dbus_message_iter_recurse(iter, &sub);
1346 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1347 printf("%s=", name);
1349 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1352 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_BYTE);
1353 dbus_message_iter_get_basic(&sub, &u);
1356 dbus_message_iter_next(&sub);
1363 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Paths")) {
1364 DBusMessageIter sub, sub2;
1366 dbus_message_iter_recurse(iter, &sub);
1368 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1369 const char *type, *path;
1371 dbus_message_iter_recurse(&sub, &sub2);
1373 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
1374 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0)
1375 printf("%s=%s\n", type, path);
1377 dbus_message_iter_next(&sub);
1381 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Timers")) {
1382 DBusMessageIter sub, sub2;
1384 dbus_message_iter_recurse(iter, &sub);
1386 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1388 uint64_t value, next_elapse;
1390 dbus_message_iter_recurse(&sub, &sub2);
1392 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &base, true) >= 0 &&
1393 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &value, true) >= 0 &&
1394 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &next_elapse, false) >= 0) {
1395 char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX];
1397 printf("%s={ value=%s ; next_elapse=%s }\n",
1399 format_timespan(timespan1, sizeof(timespan1), value),
1400 format_timespan(timespan2, sizeof(timespan2), next_elapse));
1403 dbus_message_iter_next(&sub);
1407 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && startswith(name, "Exec")) {
1409 DBusMessageIter sub, sub2, sub3;
1411 dbus_message_iter_recurse(iter, &sub);
1413 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1415 uint64_t start_time, exit_time;
1417 int32_t code, status;
1419 dbus_message_iter_recurse(&sub, &sub2);
1421 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0)
1424 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_ARRAY ||
1425 dbus_message_iter_get_element_type(&sub2) != DBUS_TYPE_STRING)
1428 printf("%s={ path=%s ; argv[]=", name, path);
1430 dbus_message_iter_recurse(&sub2, &sub3);
1432 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1435 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1436 dbus_message_iter_get_basic(&sub3, &s);
1438 dbus_message_iter_next(&sub3);
1441 if (dbus_message_iter_next(&sub2) &&
1442 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_time, true) >= 0 &&
1443 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_time, true) >= 0 &&
1444 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, true) >= 0 &&
1445 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &code, true) >= 0 &&
1446 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &status, false) >= 0) {
1448 char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
1450 printf("; start=%s ; stop=%s ; pid=%u ; code=%s ; status=%i/%s",
1451 strna(format_timestamp(timestamp1, sizeof(timestamp1), start_time)),
1452 strna(format_timestamp(timestamp2, sizeof(timestamp2), exit_time)),
1454 sigchld_code_to_string(code),
1456 strempty(code == CLD_EXITED ? NULL : strsignal(status)));
1461 dbus_message_iter_next(&sub);
1471 printf("%s=[unprintable]\n", name);
1476 static int show_one(DBusConnection *bus, const char *path, bool show_properties, bool *new_line) {
1477 DBusMessage *m = NULL, *reply = NULL;
1478 const char *interface = "";
1481 DBusMessageIter iter, sub, sub2, sub3;
1482 UnitStatusInfo info;
1489 dbus_error_init(&error);
1491 if (!(m = dbus_message_new_method_call(
1492 "org.freedesktop.systemd1",
1494 "org.freedesktop.DBus.Properties",
1496 log_error("Could not allocate message.");
1501 if (!dbus_message_append_args(m,
1502 DBUS_TYPE_STRING, &interface,
1503 DBUS_TYPE_INVALID)) {
1504 log_error("Could not append arguments to message.");
1509 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1510 log_error("Failed to issue method call: %s", error.message);
1515 if (!dbus_message_iter_init(reply, &iter) ||
1516 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
1517 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
1518 log_error("Failed to parse reply.");
1523 dbus_message_iter_recurse(&iter, &sub);
1530 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1533 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
1534 log_error("Failed to parse reply.");
1539 dbus_message_iter_recurse(&sub, &sub2);
1541 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
1542 log_error("Failed to parse reply.");
1547 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
1548 log_error("Failed to parse reply.");
1553 dbus_message_iter_recurse(&sub2, &sub3);
1555 if (show_properties)
1556 r = print_property(name, &sub3);
1558 r = status_property(name, &sub3, &info);
1561 log_error("Failed to parse reply.");
1566 dbus_message_iter_next(&sub);
1569 if (!show_properties)
1570 print_status_info(&info);
1576 dbus_message_unref(m);
1579 dbus_message_unref(reply);
1581 dbus_error_free(&error);
1586 static int show(DBusConnection *bus, char **args, unsigned n) {
1587 DBusMessage *m = NULL, *reply = NULL;
1591 bool show_properties, new_line = false;
1596 dbus_error_init(&error);
1598 show_properties = !streq(args[0], "status");
1600 if (show_properties && n <= 1) {
1601 /* If not argument is specified inspect the manager
1604 r = show_one(bus, "/org/freedesktop/systemd1", show_properties, &new_line);
1608 for (i = 1; i < n; i++) {
1609 const char *path = NULL;
1612 if (!show_properties || safe_atou32(args[i], &id) < 0) {
1614 if (!(m = dbus_message_new_method_call(
1615 "org.freedesktop.systemd1",
1616 "/org/freedesktop/systemd1",
1617 "org.freedesktop.systemd1.Manager",
1619 log_error("Could not allocate message.");
1624 if (!dbus_message_append_args(m,
1625 DBUS_TYPE_STRING, &args[i],
1626 DBUS_TYPE_INVALID)) {
1627 log_error("Could not append arguments to message.");
1632 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1634 if (!dbus_error_has_name(&error, DBUS_ERROR_ACCESS_DENIED)) {
1635 log_error("Failed to issue method call: %s", error.message);
1640 dbus_error_free(&error);
1642 dbus_message_unref(m);
1643 if (!(m = dbus_message_new_method_call(
1644 "org.freedesktop.systemd1",
1645 "/org/freedesktop/systemd1",
1646 "org.freedesktop.systemd1.Manager",
1648 log_error("Could not allocate message.");
1653 if (!dbus_message_append_args(m,
1654 DBUS_TYPE_STRING, &args[i],
1655 DBUS_TYPE_INVALID)) {
1656 log_error("Could not append arguments to message.");
1661 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1662 log_error("Failed to issue method call: %s", error.message);
1670 if (!(m = dbus_message_new_method_call(
1671 "org.freedesktop.systemd1",
1672 "/org/freedesktop/systemd1",
1673 "org.freedesktop.systemd1.Manager",
1675 log_error("Could not allocate message.");
1680 if (!dbus_message_append_args(m,
1681 DBUS_TYPE_UINT32, &id,
1682 DBUS_TYPE_INVALID)) {
1683 log_error("Could not append arguments to message.");
1688 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1689 log_error("Failed to issue method call: %s", error.message);
1695 if (!dbus_message_get_args(reply, &error,
1696 DBUS_TYPE_OBJECT_PATH, &path,
1697 DBUS_TYPE_INVALID)) {
1698 log_error("Failed to parse reply: %s", error.message);
1703 if ((r = show_one(bus, path, show_properties, &new_line)) < 0)
1706 dbus_message_unref(m);
1707 dbus_message_unref(reply);
1715 dbus_message_unref(m);
1718 dbus_message_unref(reply);
1720 dbus_error_free(&error);
1725 static DBusHandlerResult monitor_filter(DBusConnection *connection, DBusMessage *message, void *data) {
1727 DBusMessage *m = NULL, *reply = NULL;
1732 dbus_error_init(&error);
1734 log_debug("Got D-Bus request: %s.%s() on %s",
1735 dbus_message_get_interface(message),
1736 dbus_message_get_member(message),
1737 dbus_message_get_path(message));
1739 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
1740 log_error("Warning! D-Bus connection terminated.");
1741 dbus_connection_close(connection);
1743 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitNew") ||
1744 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitRemoved")) {
1745 const char *id, *path;
1747 if (!dbus_message_get_args(message, &error,
1748 DBUS_TYPE_STRING, &id,
1749 DBUS_TYPE_OBJECT_PATH, &path,
1751 log_error("Failed to parse message: %s", error.message);
1752 else if (streq(dbus_message_get_member(message), "UnitNew"))
1753 printf("Unit %s added.\n", id);
1755 printf("Unit %s removed.\n", id);
1757 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobNew") ||
1758 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
1762 if (!dbus_message_get_args(message, &error,
1763 DBUS_TYPE_UINT32, &id,
1764 DBUS_TYPE_OBJECT_PATH, &path,
1766 log_error("Failed to parse message: %s", error.message);
1767 else if (streq(dbus_message_get_member(message), "JobNew"))
1768 printf("Job %u added.\n", id);
1770 printf("Job %u removed.\n", id);
1773 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Unit", "Changed") ||
1774 dbus_message_is_signal(message, "org.freedesktop.systemd1.Job", "Changed")) {
1776 const char *path, *interface, *property = "Id";
1777 DBusMessageIter iter, sub;
1779 path = dbus_message_get_path(message);
1780 interface = dbus_message_get_interface(message);
1782 if (!(m = dbus_message_new_method_call(
1783 "org.freedesktop.systemd1",
1785 "org.freedesktop.DBus.Properties",
1787 log_error("Could not allocate message.");
1791 if (!dbus_message_append_args(m,
1792 DBUS_TYPE_STRING, &interface,
1793 DBUS_TYPE_STRING, &property,
1794 DBUS_TYPE_INVALID)) {
1795 log_error("Could not append arguments to message.");
1799 if (!(reply = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) {
1800 log_error("Failed to issue method call: %s", error.message);
1804 if (!dbus_message_iter_init(reply, &iter) ||
1805 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1806 log_error("Failed to parse reply.");
1810 dbus_message_iter_recurse(&iter, &sub);
1812 if (streq(interface, "org.freedesktop.systemd1.Unit")) {
1815 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1816 log_error("Failed to parse reply.");
1820 dbus_message_iter_get_basic(&sub, &id);
1821 printf("Unit %s changed.\n", id);
1825 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT32) {
1826 log_error("Failed to parse reply.");
1830 dbus_message_iter_get_basic(&sub, &id);
1831 printf("Job %u changed.\n", id);
1837 dbus_message_unref(m);
1840 dbus_message_unref(reply);
1842 dbus_error_free(&error);
1843 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1847 dbus_message_unref(m);
1850 dbus_message_unref(reply);
1852 dbus_error_free(&error);
1853 return DBUS_HANDLER_RESULT_NEED_MEMORY;
1856 static int monitor(DBusConnection *bus, char **args, unsigned n) {
1857 DBusMessage *m = NULL, *reply = NULL;
1861 dbus_error_init(&error);
1864 dbus_bus_add_match(bus,
1866 "sender='org.freedesktop.systemd1',"
1867 "interface='org.freedesktop.systemd1.Manager',"
1868 "path='/org/freedesktop/systemd1'",
1871 if (dbus_error_is_set(&error)) {
1872 log_error("Failed to add match: %s", error.message);
1877 dbus_bus_add_match(bus,
1879 "sender='org.freedesktop.systemd1',"
1880 "interface='org.freedesktop.systemd1.Unit',"
1884 if (dbus_error_is_set(&error)) {
1885 log_error("Failed to add match: %s", error.message);
1890 dbus_bus_add_match(bus,
1892 "sender='org.freedesktop.systemd1',"
1893 "interface='org.freedesktop.systemd1.Job',"
1897 if (dbus_error_is_set(&error)) {
1898 log_error("Failed to add match: %s", error.message);
1904 if (!dbus_connection_add_filter(bus, monitor_filter, NULL, NULL)) {
1905 log_error("Failed to add filter.");
1910 if (!(m = dbus_message_new_method_call(
1911 "org.freedesktop.systemd1",
1912 "/org/freedesktop/systemd1",
1913 "org.freedesktop.systemd1.Manager",
1915 log_error("Could not allocate message.");
1920 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1921 log_error("Failed to issue method call: %s", error.message);
1926 while (dbus_connection_read_write_dispatch(bus, -1))
1933 /* This is slightly dirty, since we don't undo the filter or the matches. */
1936 dbus_message_unref(m);
1939 dbus_message_unref(reply);
1941 dbus_error_free(&error);
1946 static int dump(DBusConnection *bus, char **args, unsigned n) {
1947 DBusMessage *m = NULL, *reply = NULL;
1952 dbus_error_init(&error);
1954 if (!(m = dbus_message_new_method_call(
1955 "org.freedesktop.systemd1",
1956 "/org/freedesktop/systemd1",
1957 "org.freedesktop.systemd1.Manager",
1959 log_error("Could not allocate message.");
1963 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1964 log_error("Failed to issue method call: %s", error.message);
1969 if (!dbus_message_get_args(reply, &error,
1970 DBUS_TYPE_STRING, &text,
1971 DBUS_TYPE_INVALID)) {
1972 log_error("Failed to parse reply: %s", error.message);
1977 fputs(text, stdout);
1983 dbus_message_unref(m);
1986 dbus_message_unref(reply);
1988 dbus_error_free(&error);
1993 static int snapshot(DBusConnection *bus, char **args, unsigned n) {
1994 DBusMessage *m = NULL, *reply = NULL;
1997 const char *name = "", *path, *id;
1998 dbus_bool_t cleanup = FALSE;
1999 DBusMessageIter iter, sub;
2001 *interface = "org.freedesktop.systemd1.Unit",
2004 dbus_error_init(&error);
2006 if (!(m = dbus_message_new_method_call(
2007 "org.freedesktop.systemd1",
2008 "/org/freedesktop/systemd1",
2009 "org.freedesktop.systemd1.Manager",
2010 "CreateSnapshot"))) {
2011 log_error("Could not allocate message.");
2018 if (!dbus_message_append_args(m,
2019 DBUS_TYPE_STRING, &name,
2020 DBUS_TYPE_BOOLEAN, &cleanup,
2021 DBUS_TYPE_INVALID)) {
2022 log_error("Could not append arguments to message.");
2027 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2028 log_error("Failed to issue method call: %s", error.message);
2033 if (!dbus_message_get_args(reply, &error,
2034 DBUS_TYPE_OBJECT_PATH, &path,
2035 DBUS_TYPE_INVALID)) {
2036 log_error("Failed to parse reply: %s", error.message);
2041 dbus_message_unref(m);
2042 if (!(m = dbus_message_new_method_call(
2043 "org.freedesktop.systemd1",
2045 "org.freedesktop.DBus.Properties",
2047 log_error("Could not allocate message.");
2051 if (!dbus_message_append_args(m,
2052 DBUS_TYPE_STRING, &interface,
2053 DBUS_TYPE_STRING, &property,
2054 DBUS_TYPE_INVALID)) {
2055 log_error("Could not append arguments to message.");
2060 dbus_message_unref(reply);
2061 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2062 log_error("Failed to issue method call: %s", error.message);
2067 if (!dbus_message_iter_init(reply, &iter) ||
2068 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2069 log_error("Failed to parse reply.");
2074 dbus_message_iter_recurse(&iter, &sub);
2076 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2077 log_error("Failed to parse reply.");
2082 dbus_message_iter_get_basic(&sub, &id);
2090 dbus_message_unref(m);
2093 dbus_message_unref(reply);
2095 dbus_error_free(&error);
2100 static int delete_snapshot(DBusConnection *bus, char **args, unsigned n) {
2101 DBusMessage *m = NULL, *reply = NULL;
2109 dbus_error_init(&error);
2111 for (i = 1; i < n; i++) {
2112 const char *path = NULL;
2114 if (!(m = dbus_message_new_method_call(
2115 "org.freedesktop.systemd1",
2116 "/org/freedesktop/systemd1",
2117 "org.freedesktop.systemd1.Manager",
2119 log_error("Could not allocate message.");
2124 if (!dbus_message_append_args(m,
2125 DBUS_TYPE_STRING, &args[i],
2126 DBUS_TYPE_INVALID)) {
2127 log_error("Could not append arguments to message.");
2132 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2133 log_error("Failed to issue method call: %s", error.message);
2138 if (!dbus_message_get_args(reply, &error,
2139 DBUS_TYPE_OBJECT_PATH, &path,
2140 DBUS_TYPE_INVALID)) {
2141 log_error("Failed to parse reply: %s", error.message);
2146 dbus_message_unref(m);
2147 if (!(m = dbus_message_new_method_call(
2148 "org.freedesktop.systemd1",
2150 "org.freedesktop.systemd1.Snapshot",
2152 log_error("Could not allocate message.");
2157 dbus_message_unref(reply);
2158 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2159 log_error("Failed to issue method call: %s", error.message);
2164 dbus_message_unref(m);
2165 dbus_message_unref(reply);
2173 dbus_message_unref(m);
2176 dbus_message_unref(reply);
2178 dbus_error_free(&error);
2183 static int clear_jobs(DBusConnection *bus, char **args, unsigned n) {
2184 DBusMessage *m = NULL, *reply = NULL;
2189 dbus_error_init(&error);
2191 if (arg_action == ACTION_RELOAD)
2193 else if (arg_action == ACTION_REEXEC)
2194 method = "Reexecute";
2196 assert(arg_action == ACTION_SYSTEMCTL);
2199 streq(args[0], "clear-jobs") ? "ClearJobs" :
2200 streq(args[0], "daemon-reload") ? "Reload" :
2201 streq(args[0], "daemon-reexec") ? "Reexecute" :
2205 if (!(m = dbus_message_new_method_call(
2206 "org.freedesktop.systemd1",
2207 "/org/freedesktop/systemd1",
2208 "org.freedesktop.systemd1.Manager",
2210 log_error("Could not allocate message.");
2214 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2216 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
2217 /* There's always a fallback possible for
2218 * legacy actions. */
2223 log_error("Failed to issue method call: %s", error.message);
2232 dbus_message_unref(m);
2235 dbus_message_unref(reply);
2237 dbus_error_free(&error);
2242 static int show_enviroment(DBusConnection *bus, char **args, unsigned n) {
2243 DBusMessage *m = NULL, *reply = NULL;
2245 DBusMessageIter iter, sub, sub2;
2248 *interface = "org.freedesktop.systemd1.Manager",
2249 *property = "Environment";
2251 dbus_error_init(&error);
2253 if (!(m = dbus_message_new_method_call(
2254 "org.freedesktop.systemd1",
2255 "/org/freedesktop/systemd1",
2256 "org.freedesktop.DBus.Properties",
2258 log_error("Could not allocate message.");
2262 if (!dbus_message_append_args(m,
2263 DBUS_TYPE_STRING, &interface,
2264 DBUS_TYPE_STRING, &property,
2265 DBUS_TYPE_INVALID)) {
2266 log_error("Could not append arguments to message.");
2271 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2272 log_error("Failed to issue method call: %s", error.message);
2277 if (!dbus_message_iter_init(reply, &iter) ||
2278 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2279 log_error("Failed to parse reply.");
2284 dbus_message_iter_recurse(&iter, &sub);
2286 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
2287 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
2288 log_error("Failed to parse reply.");
2293 dbus_message_iter_recurse(&sub, &sub2);
2295 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
2298 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
2299 log_error("Failed to parse reply.");
2304 dbus_message_iter_get_basic(&sub2, &text);
2305 printf("%s\n", text);
2307 dbus_message_iter_next(&sub2);
2314 dbus_message_unref(m);
2317 dbus_message_unref(reply);
2319 dbus_error_free(&error);
2324 static int set_environment(DBusConnection *bus, char **args, unsigned n) {
2325 DBusMessage *m = NULL, *reply = NULL;
2329 DBusMessageIter iter, sub;
2332 dbus_error_init(&error);
2334 method = streq(args[0], "set-environment")
2336 : "UnsetEnvironment";
2338 if (!(m = dbus_message_new_method_call(
2339 "org.freedesktop.systemd1",
2340 "/org/freedesktop/systemd1",
2341 "org.freedesktop.systemd1.Manager",
2344 log_error("Could not allocate message.");
2348 dbus_message_iter_init_append(m, &iter);
2350 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
2351 log_error("Could not append arguments to message.");
2356 for (i = 1; i < n; i++)
2357 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &args[i])) {
2358 log_error("Could not append arguments to message.");
2363 if (!dbus_message_iter_close_container(&iter, &sub)) {
2364 log_error("Could not append arguments to message.");
2369 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2370 log_error("Failed to issue method call: %s", error.message);
2379 dbus_message_unref(m);
2382 dbus_message_unref(reply);
2384 dbus_error_free(&error);
2389 static int systemctl_help(void) {
2391 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
2392 "Send control commands to the systemd manager.\n\n"
2393 " -h --help Show this help\n"
2394 " -t --type=TYPE List only units of a particular type\n"
2395 " -p --property=NAME Show only properties by this name\n"
2396 " -a --all Show all units/properties, including dead/empty ones\n"
2397 " --replace When installing a new job, replace existing conflicting ones\n"
2398 " --system Connect to system bus\n"
2399 " --session Connect to session bus\n"
2400 " -q --quiet Suppress output\n"
2401 " --no-block Do not wait until operation finished\n"
2402 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
2404 " list-units List units\n"
2405 " start [NAME...] Start one or more units\n"
2406 " stop [NAME...] Stop one or more units\n"
2407 " restart [NAME...] Restart one or more units\n"
2408 " reload [NAME...] Reload one or more units\n"
2409 " isolate [NAME] Start one unit and stop all others\n"
2410 " check [NAME...] Check whether any of the passed units are active\n"
2411 " status [NAME...] Show status of one or more units\n"
2412 " show [NAME...|JOB...] Show properties of one or more units/jobs/manager\n"
2413 " load [NAME...] Load one or more units\n"
2414 " list-jobs List jobs\n"
2415 " cancel [JOB...] Cancel one or more jobs\n"
2416 " clear-jobs Cancel all jobs\n"
2417 " monitor Monitor unit/job changes\n"
2418 " dump Dump server status\n"
2419 " snapshot [NAME] Create a snapshot\n"
2420 " delete [NAME...] Remove one or more snapshots\n"
2421 " daemon-reload Reload systemd manager configuration\n"
2422 " daemon-reexec Reexecute systemd manager\n"
2423 " daemon-exit Ask the systemd manager to quit\n"
2424 " show-environment Dump environment\n"
2425 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
2426 " unset-environment [NAME...] Unset one or more environment variables\n"
2427 " halt Shut down and halt the system\n"
2428 " poweroff Shut down and power-off the system\n"
2429 " reboot Shut down and reboot the system\n"
2430 " default Enter default mode\n"
2431 " rescue Enter rescue mode\n"
2432 " emergency Enter emergency mode\n",
2433 program_invocation_short_name);
2438 static int halt_help(void) {
2440 printf("%s [OPTIONS...]\n\n"
2441 "%s the system.\n\n"
2442 " --help Show this help\n"
2443 " --halt Halt the machine\n"
2444 " -p --poweroff Switch off the machine\n"
2445 " --reboot Reboot the machine\n"
2446 " -f --force Force immediate halt/power-off/reboot\n"
2447 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
2448 " -d --no-wtmp Don't write wtmp record\n"
2449 " -n --no-sync Don't sync before halt/power-off/reboot\n"
2450 " --no-wall Don't send wall message before halt/power-off/reboot\n",
2451 program_invocation_short_name,
2452 arg_action == ACTION_REBOOT ? "Reboot" :
2453 arg_action == ACTION_POWEROFF ? "Power off" :
2459 static int shutdown_help(void) {
2461 printf("%s [OPTIONS...] [now] [WALL...]\n\n"
2462 "Shut down the system.\n\n"
2463 " --help Show this help\n"
2464 " -H --halt Halt the machine\n"
2465 " -P --poweroff Power-off the machine\n"
2466 " -r --reboot Reboot the machine\n"
2467 " -h Equivalent to --poweroff, overriden by --halt\n"
2468 " -k Don't halt/power-off/reboot, just send warnings\n"
2469 " --no-wall Don't send wall message before halt/power-off/reboot\n",
2470 program_invocation_short_name);
2475 static int telinit_help(void) {
2477 printf("%s [OPTIONS...] {COMMAND}\n\n"
2478 "Send control commands to the init daemon.\n\n"
2479 " --help Show this help\n"
2480 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
2482 " 0 Power-off the machine\n"
2483 " 6 Reboot the machine\n"
2484 " 2, 3, 4, 5 Start runlevelX.target unit\n"
2485 " 1, s, S Enter rescue mode\n"
2486 " q, Q Reload init daemon configuration\n"
2487 " u, U Reexecute init daemon\n",
2488 program_invocation_short_name);
2493 static int runlevel_help(void) {
2495 printf("%s [OPTIONS...]\n\n"
2496 "Prints the previous and current runlevel of the init system.\n\n"
2497 " --help Show this help\n",
2498 program_invocation_short_name);
2503 static int systemctl_parse_argv(int argc, char *argv[]) {
2506 ARG_REPLACE = 0x100,
2513 static const struct option options[] = {
2514 { "help", no_argument, NULL, 'h' },
2515 { "type", required_argument, NULL, 't' },
2516 { "property", required_argument, NULL, 'p' },
2517 { "all", no_argument, NULL, 'a' },
2518 { "replace", no_argument, NULL, ARG_REPLACE },
2519 { "session", no_argument, NULL, ARG_SESSION },
2520 { "system", no_argument, NULL, ARG_SYSTEM },
2521 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
2522 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2523 { "quiet", no_argument, NULL, 'q' },
2524 { NULL, 0, NULL, 0 }
2532 while ((c = getopt_long(argc, argv, "ht:p:aq", options, NULL)) >= 0) {
2545 arg_property = optarg;
2547 /* If the user asked for a particular
2548 * property, show it to him, even if it is
2566 arg_session = false;
2570 arg_no_block = true;
2585 log_error("Unknown option code %c", c);
2593 static int halt_parse_argv(int argc, char *argv[]) {
2602 static const struct option options[] = {
2603 { "help", no_argument, NULL, ARG_HELP },
2604 { "halt", no_argument, NULL, ARG_HALT },
2605 { "poweroff", no_argument, NULL, 'p' },
2606 { "reboot", no_argument, NULL, ARG_REBOOT },
2607 { "force", no_argument, NULL, 'f' },
2608 { "wtmp-only", no_argument, NULL, 'w' },
2609 { "no-wtmp", no_argument, NULL, 'd' },
2610 { "no-sync", no_argument, NULL, 'n' },
2611 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2612 { NULL, 0, NULL, 0 }
2620 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
2621 if (runlevel == '0' || runlevel == '6')
2622 arg_immediate = true;
2624 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
2632 arg_action = ACTION_HALT;
2636 arg_action = ACTION_POWEROFF;
2640 arg_action = ACTION_REBOOT;
2644 arg_immediate = true;
2665 /* Compatibility nops */
2672 log_error("Unknown option code %c", c);
2677 if (optind < argc) {
2678 log_error("Too many arguments.");
2685 static int shutdown_parse_argv(int argc, char *argv[]) {
2692 static const struct option options[] = {
2693 { "help", no_argument, NULL, ARG_HELP },
2694 { "halt", no_argument, NULL, 'H' },
2695 { "poweroff", no_argument, NULL, 'P' },
2696 { "reboot", no_argument, NULL, 'r' },
2697 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2698 { NULL, 0, NULL, 0 }
2706 while ((c = getopt_long(argc, argv, "HPrhkt:a", options, NULL)) >= 0) {
2714 arg_action = ACTION_HALT;
2718 arg_action = ACTION_POWEROFF;
2722 arg_action = ACTION_REBOOT;
2726 if (arg_action != ACTION_HALT)
2727 arg_action = ACTION_POWEROFF;
2740 /* Compatibility nops */
2747 log_error("Unknown option code %c", c);
2752 if (argc > optind && !streq(argv[optind], "now"))
2753 log_warning("First argument '%s' isn't 'now'. Ignoring.", argv[optind]);
2755 /* We ignore the time argument */
2756 if (argc > optind + 1)
2757 arg_wall = argv + optind + 1;
2764 static int telinit_parse_argv(int argc, char *argv[]) {
2771 static const struct option options[] = {
2772 { "help", no_argument, NULL, ARG_HELP },
2773 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2774 { NULL, 0, NULL, 0 }
2777 static const struct {
2781 { '0', ACTION_POWEROFF },
2782 { '6', ACTION_REBOOT },
2783 { '1', ACTION_RESCUE },
2784 { '2', ACTION_RUNLEVEL2 },
2785 { '3', ACTION_RUNLEVEL3 },
2786 { '4', ACTION_RUNLEVEL4 },
2787 { '5', ACTION_RUNLEVEL5 },
2788 { 's', ACTION_RESCUE },
2789 { 'S', ACTION_RESCUE },
2790 { 'q', ACTION_RELOAD },
2791 { 'Q', ACTION_RELOAD },
2792 { 'u', ACTION_REEXEC },
2793 { 'U', ACTION_REEXEC }
2802 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
2817 log_error("Unknown option code %c", c);
2822 if (optind >= argc) {
2827 if (optind + 1 < argc) {
2828 log_error("Too many arguments.");
2832 if (strlen(argv[optind]) != 1) {
2833 log_error("Expected single character argument.");
2837 for (i = 0; i < ELEMENTSOF(table); i++)
2838 if (table[i].from == argv[optind][0])
2841 if (i >= ELEMENTSOF(table)) {
2842 log_error("Unknown command %s.", argv[optind]);
2846 arg_action = table[i].to;
2853 static int runlevel_parse_argv(int argc, char *argv[]) {
2859 static const struct option options[] = {
2860 { "help", no_argument, NULL, ARG_HELP },
2861 { NULL, 0, NULL, 0 }
2869 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
2880 log_error("Unknown option code %c", c);
2885 if (optind < argc) {
2886 log_error("Too many arguments.");
2893 static int parse_argv(int argc, char *argv[]) {
2897 if (program_invocation_short_name) {
2899 if (strstr(program_invocation_short_name, "halt")) {
2900 arg_action = ACTION_HALT;
2901 return halt_parse_argv(argc, argv);
2902 } else if (strstr(program_invocation_short_name, "poweroff")) {
2903 arg_action = ACTION_POWEROFF;
2904 return halt_parse_argv(argc, argv);
2905 } else if (strstr(program_invocation_short_name, "reboot")) {
2906 arg_action = ACTION_REBOOT;
2907 return halt_parse_argv(argc, argv);
2908 } else if (strstr(program_invocation_short_name, "shutdown")) {
2909 arg_action = ACTION_POWEROFF;
2910 return shutdown_parse_argv(argc, argv);
2911 } else if (strstr(program_invocation_short_name, "init")) {
2912 arg_action = ACTION_INVALID;
2913 return telinit_parse_argv(argc, argv);
2914 } else if (strstr(program_invocation_short_name, "runlevel")) {
2915 arg_action = ACTION_RUNLEVEL;
2916 return runlevel_parse_argv(argc, argv);
2920 arg_action = ACTION_SYSTEMCTL;
2921 return systemctl_parse_argv(argc, argv);
2924 static int action_to_runlevel(void) {
2926 static const char table[_ACTION_MAX] = {
2927 [ACTION_HALT] = '0',
2928 [ACTION_POWEROFF] = '0',
2929 [ACTION_REBOOT] = '6',
2930 [ACTION_RUNLEVEL2] = '2',
2931 [ACTION_RUNLEVEL3] = '3',
2932 [ACTION_RUNLEVEL4] = '4',
2933 [ACTION_RUNLEVEL5] = '5',
2934 [ACTION_RESCUE] = '1'
2937 assert(arg_action < _ACTION_MAX);
2939 return table[arg_action];
2942 static int talk_upstart(void) {
2943 DBusMessage *m = NULL, *reply = NULL;
2945 int previous, rl, r;
2947 env1_buf[] = "RUNLEVEL=X",
2948 env2_buf[] = "PREVLEVEL=X";
2949 char *env1 = env1_buf, *env2 = env2_buf;
2950 const char *emit = "runlevel";
2951 dbus_bool_t b_false = FALSE;
2952 DBusMessageIter iter, sub;
2953 DBusConnection *bus;
2955 dbus_error_init(&error);
2957 if (!(rl = action_to_runlevel()))
2960 if (utmp_get_runlevel(&previous, NULL) < 0)
2963 if (!(bus = dbus_connection_open("unix:abstract=/com/ubuntu/upstart", &error))) {
2964 if (dbus_error_has_name(&error, DBUS_ERROR_NO_SERVER)) {
2969 log_error("Failed to connect to Upstart bus: %s", error.message);
2974 if ((r = bus_check_peercred(bus)) < 0) {
2975 log_error("Failed to verify owner of bus.");
2979 if (!(m = dbus_message_new_method_call(
2980 "com.ubuntu.Upstart",
2981 "/com/ubuntu/Upstart",
2982 "com.ubuntu.Upstart0_6",
2985 log_error("Could not allocate message.");
2990 dbus_message_iter_init_append(m, &iter);
2992 env1_buf[sizeof(env1_buf)-2] = rl;
2993 env2_buf[sizeof(env2_buf)-2] = previous;
2995 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
2996 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
2997 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
2998 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
2999 !dbus_message_iter_close_container(&iter, &sub) ||
3000 !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
3001 log_error("Could not append arguments to message.");
3006 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3008 if (error_is_no_service(&error)) {
3013 log_error("Failed to issue method call: %s", error.message);
3022 dbus_message_unref(m);
3025 dbus_message_unref(reply);
3028 dbus_connection_unref(bus);
3030 dbus_error_free(&error);
3035 static int talk_initctl(void) {
3036 struct init_request request;
3040 if (!(rl = action_to_runlevel()))
3044 request.magic = INIT_MAGIC;
3045 request.sleeptime = 0;
3046 request.cmd = INIT_CMD_RUNLVL;
3047 request.runlevel = rl;
3049 if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
3051 if (errno == ENOENT)
3054 log_error("Failed to open "INIT_FIFO": %m");
3059 r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
3060 close_nointr_nofail(fd);
3063 log_error("Failed to write to "INIT_FIFO": %m");
3064 return errno ? -errno : -EIO;
3070 static int systemctl_main(DBusConnection *bus, int argc, char *argv[]) {
3072 static const struct {
3080 int (* const dispatch)(DBusConnection *bus, char **args, unsigned n);
3082 { "list-units", LESS, 1, list_units },
3083 { "list-jobs", EQUAL, 1, list_jobs },
3084 { "clear-jobs", EQUAL, 1, clear_jobs },
3085 { "load", MORE, 2, load_unit },
3086 { "cancel", MORE, 2, cancel_job },
3087 { "start", MORE, 2, start_unit },
3088 { "stop", MORE, 2, start_unit },
3089 { "reload", MORE, 2, start_unit },
3090 { "restart", MORE, 2, start_unit },
3091 { "isolate", EQUAL, 2, start_unit },
3092 { "check", MORE, 2, check_unit },
3093 { "show", MORE, 1, show },
3094 { "status", MORE, 2, show },
3095 { "monitor", EQUAL, 1, monitor },
3096 { "dump", EQUAL, 1, dump },
3097 { "snapshot", LESS, 2, snapshot },
3098 { "delete", MORE, 2, delete_snapshot },
3099 { "daemon-reload", EQUAL, 1, clear_jobs },
3100 { "daemon-reexec", EQUAL, 1, clear_jobs },
3101 { "daemon-exit", EQUAL, 1, clear_jobs },
3102 { "show-environment", EQUAL, 1, show_enviroment },
3103 { "set-environment", MORE, 2, set_environment },
3104 { "unset-environment", MORE, 2, set_environment },
3105 { "halt", EQUAL, 1, start_special },
3106 { "poweroff", EQUAL, 1, start_special },
3107 { "reboot", EQUAL, 1, start_special },
3108 { "default", EQUAL, 1, start_special },
3109 { "rescue", EQUAL, 1, start_special },
3110 { "emergency", EQUAL, 1, start_special },
3120 left = argc - optind;
3123 /* Special rule: no arguments means "list-units" */
3126 if (streq(argv[optind], "help")) {
3131 for (i = 0; i < ELEMENTSOF(verbs); i++)
3132 if (streq(argv[optind], verbs[i].verb))
3135 if (i >= ELEMENTSOF(verbs)) {
3136 log_error("Unknown operation %s", argv[optind]);
3141 switch (verbs[i].argc_cmp) {
3144 if (left != verbs[i].argc) {
3145 log_error("Invalid number of arguments.");
3152 if (left < verbs[i].argc) {
3153 log_error("Too few arguments.");
3160 if (left > verbs[i].argc) {
3161 log_error("Too many arguments.");
3168 assert_not_reached("Unknown comparison operator.");
3171 return verbs[i].dispatch(bus, argv + optind, left);
3174 static int reload_with_fallback(DBusConnection *bus) {
3178 /* First, try systemd via D-Bus. */
3179 if ((r = clear_jobs(bus, NULL, 0)) > 0)
3183 /* Nothing else worked, so let's try signals */
3184 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
3186 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
3187 log_error("kill() failed: %m");
3194 static int start_with_fallback(DBusConnection *bus) {
3197 warn_wall(arg_action);
3200 /* First, try systemd via D-Bus. */
3201 if ((r = start_unit(bus, NULL, 0)) > 0)
3204 /* Hmm, talking to systemd via D-Bus didn't work. Then
3205 * let's try to talk to Upstart via D-Bus. */
3206 if ((r = talk_upstart()) > 0)
3210 /* Nothing else worked, so let's try
3212 if ((r = talk_initctl()) != 0)
3215 log_error("Failed to talk to init daemon.");
3219 static int halt_main(DBusConnection *bus) {
3223 return start_with_fallback(bus);
3226 if ((r = utmp_put_shutdown(0)) < 0)
3227 log_warning("Failed to write utmp record: %s", strerror(-r));
3235 /* Make sure C-A-D is handled by the kernel from this
3237 reboot(RB_ENABLE_CAD);
3239 switch (arg_action) {
3242 log_info("Halting");
3243 reboot(RB_HALT_SYSTEM);
3246 case ACTION_POWEROFF:
3247 log_info("Powering off");
3248 reboot(RB_POWER_OFF);
3252 log_info("Rebooting");
3253 reboot(RB_AUTOBOOT);
3257 assert_not_reached("Unknown halt action.");
3260 /* We should never reach this. */
3264 static int runlevel_main(void) {
3265 int r, runlevel, previous;
3267 if ((r = utmp_get_runlevel(&runlevel, &previous)) < 0) {
3273 previous <= 0 ? 'N' : previous,
3274 runlevel <= 0 ? 'N' : runlevel);
3279 int main(int argc, char*argv[]) {
3281 DBusConnection *bus = NULL;
3284 dbus_error_init(&error);
3286 log_parse_environment();
3288 if ((r = parse_argv(argc, argv)) < 0)
3295 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
3296 * let's shortcut this */
3297 if (arg_action == ACTION_RUNLEVEL) {
3298 retval = runlevel_main() < 0;
3302 bus_connect(arg_session ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &bus, &private_bus, &error);
3304 switch (arg_action) {
3306 case ACTION_SYSTEMCTL: {
3309 log_error("Failed to get D-Bus connection: %s", error.message);
3313 retval = systemctl_main(bus, argc, argv) < 0;
3318 case ACTION_POWEROFF:
3320 retval = halt_main(bus) < 0;
3323 case ACTION_RUNLEVEL2:
3324 case ACTION_RUNLEVEL3:
3325 case ACTION_RUNLEVEL4:
3326 case ACTION_RUNLEVEL5:
3328 case ACTION_EMERGENCY:
3329 case ACTION_DEFAULT:
3330 retval = start_with_fallback(bus) < 0;
3335 retval = reload_with_fallback(bus) < 0;
3338 case ACTION_INVALID:
3339 case ACTION_RUNLEVEL:
3341 assert_not_reached("Unknown action");
3347 dbus_connection_unref(bus);
3349 dbus_error_free(&error);