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 error_is_no_service(DBusError *error) {
82 if (!dbus_error_is_set(error))
85 if (dbus_error_has_name(error, DBUS_ERROR_NAME_HAS_NO_OWNER))
88 if (dbus_error_has_name(error, DBUS_ERROR_SERVICE_UNKNOWN))
91 return startswith(error->name, "org.freedesktop.DBus.Error.Spawn.");
94 static int bus_iter_get_basic_and_next(DBusMessageIter *iter, int type, void *data, bool next) {
99 if (dbus_message_iter_get_arg_type(iter) != type)
102 dbus_message_iter_get_basic(iter, data);
104 if (!dbus_message_iter_next(iter) != !next)
110 static int columns(void) {
111 static int parsed_columns = 0;
114 if (parsed_columns > 0)
115 return parsed_columns;
117 if ((e = getenv("COLUMNS")))
118 parsed_columns = atoi(e);
120 if (parsed_columns <= 0) {
124 if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) >= 0)
125 parsed_columns = ws.ws_col;
128 if (parsed_columns <= 0)
131 return parsed_columns;
135 static void warn_wall(enum action action) {
136 static const char *table[_ACTION_MAX] = {
137 [ACTION_HALT] = "The system is going down for system halt NOW!",
138 [ACTION_REBOOT] = "The system is going down for reboot NOW!",
139 [ACTION_POWEROFF] = "The system is going down for power-off NOW!",
140 [ACTION_RESCUE] = "The system is going down to rescue mode NOW!",
141 [ACTION_EMERGENCY] = "The system is going down to emergency mode NOW!"
150 if (!(p = strv_join(arg_wall, " "))) {
151 log_error("Failed to join strings.");
167 utmp_wall(table[action]);
170 static int list_units(DBusConnection *bus, char **args, unsigned n) {
171 DBusMessage *m = NULL, *reply = NULL;
174 DBusMessageIter iter, sub, sub2;
177 dbus_error_init(&error);
181 if (!(m = dbus_message_new_method_call(
182 "org.freedesktop.systemd1",
183 "/org/freedesktop/systemd1",
184 "org.freedesktop.systemd1.Manager",
186 log_error("Could not allocate message.");
190 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
191 log_error("Failed to issue method call: %s", error.message);
196 if (!dbus_message_iter_init(reply, &iter) ||
197 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
198 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
199 log_error("Failed to parse reply.");
204 dbus_message_iter_recurse(&iter, &sub);
206 printf("%-45s %-6s %-12s %-12s %-15s %s\n", "UNIT", "LOAD", "ACTIVE", "SUB", "JOB", "DESCRIPTION");
208 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
209 const char *id, *description, *load_state, *active_state, *sub_state, *unit_state, *job_type, *job_path, *dot;
212 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
213 log_error("Failed to parse reply.");
218 dbus_message_iter_recurse(&sub, &sub2);
220 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
221 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &description, true) < 0 ||
222 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &load_state, true) < 0 ||
223 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &active_state, true) < 0 ||
224 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &sub_state, true) < 0 ||
225 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_state, true) < 0 ||
226 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &job_id, true) < 0 ||
227 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &job_type, true) < 0 ||
228 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, false) < 0) {
229 log_error("Failed to parse reply.");
234 if ((!arg_type || ((dot = strrchr(id, '.')) &&
235 streq(dot+1, arg_type))) &&
236 (arg_all || !streq(active_state, "inactive"))) {
240 printf("%-45s %-6s %-12s %-12s%n", id, load_state, active_state, sub_state, &a);
243 printf(" %-15s%n", job_type, &b);
247 if (a + b + 2 < columns()) {
251 printf("%.*s", columns() - a - b - 2, description);
258 dbus_message_iter_next(&sub);
262 printf("\n%u units listed.\n", k);
264 printf("\n%u live units listed. Pass --all to see dead units, too.\n", k);
270 dbus_message_unref(m);
273 dbus_message_unref(reply);
275 dbus_error_free(&error);
280 static int list_jobs(DBusConnection *bus, char **args, unsigned n) {
281 DBusMessage *m = NULL, *reply = NULL;
284 DBusMessageIter iter, sub, sub2;
287 dbus_error_init(&error);
291 if (!(m = dbus_message_new_method_call(
292 "org.freedesktop.systemd1",
293 "/org/freedesktop/systemd1",
294 "org.freedesktop.systemd1.Manager",
296 log_error("Could not allocate message.");
300 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
301 log_error("Failed to issue method call: %s", error.message);
306 if (!dbus_message_iter_init(reply, &iter) ||
307 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
308 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
309 log_error("Failed to parse reply.");
314 dbus_message_iter_recurse(&iter, &sub);
316 printf("%4s %-45s %-17s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
318 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
319 const char *name, *type, *state, *job_path, *unit_path;
322 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
323 log_error("Failed to parse reply.");
328 dbus_message_iter_recurse(&sub, &sub2);
330 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &id, true) < 0 ||
331 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0 ||
332 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
333 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, true) < 0 ||
334 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, true) < 0 ||
335 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, false) < 0) {
336 log_error("Failed to parse reply.");
341 printf("%4u %-45s %-17s %-7s\n", id, name, type, state);
344 dbus_message_iter_next(&sub);
347 printf("\n%u jobs listed.\n", k);
352 dbus_message_unref(m);
355 dbus_message_unref(reply);
357 dbus_error_free(&error);
362 static int load_unit(DBusConnection *bus, char **args, unsigned n) {
363 DBusMessage *m = NULL, *reply = NULL;
368 dbus_error_init(&error);
373 for (i = 1; i < n; i++) {
375 if (!(m = dbus_message_new_method_call(
376 "org.freedesktop.systemd1",
377 "/org/freedesktop/systemd1",
378 "org.freedesktop.systemd1.Manager",
380 log_error("Could not allocate message.");
385 if (!dbus_message_append_args(m,
386 DBUS_TYPE_STRING, &args[i],
387 DBUS_TYPE_INVALID)) {
388 log_error("Could not append arguments to message.");
393 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
394 log_error("Failed to issue method call: %s", error.message);
399 dbus_message_unref(m);
400 dbus_message_unref(reply);
409 dbus_message_unref(m);
412 dbus_message_unref(reply);
414 dbus_error_free(&error);
419 static int cancel_job(DBusConnection *bus, char **args, unsigned n) {
420 DBusMessage *m = NULL, *reply = NULL;
425 dbus_error_init(&error);
430 for (i = 1; i < n; i++) {
434 if (!(m = dbus_message_new_method_call(
435 "org.freedesktop.systemd1",
436 "/org/freedesktop/systemd1",
437 "org.freedesktop.systemd1.Manager",
439 log_error("Could not allocate message.");
444 if ((r = safe_atou(args[i], &id)) < 0) {
445 log_error("Failed to parse job id: %s", strerror(-r));
449 assert_cc(sizeof(uint32_t) == sizeof(id));
450 if (!dbus_message_append_args(m,
451 DBUS_TYPE_UINT32, &id,
452 DBUS_TYPE_INVALID)) {
453 log_error("Could not append arguments to message.");
458 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
459 log_error("Failed to issue method call: %s", error.message);
464 if (!dbus_message_get_args(reply, &error,
465 DBUS_TYPE_OBJECT_PATH, &path,
466 DBUS_TYPE_INVALID)) {
467 log_error("Failed to parse reply: %s", error.message);
472 dbus_message_unref(m);
473 if (!(m = dbus_message_new_method_call(
474 "org.freedesktop.systemd1",
476 "org.freedesktop.systemd1.Job",
478 log_error("Could not allocate message.");
483 dbus_message_unref(reply);
484 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
485 log_error("Failed to issue method call: %s", error.message);
490 dbus_message_unref(m);
491 dbus_message_unref(reply);
499 dbus_message_unref(m);
502 dbus_message_unref(reply);
504 dbus_error_free(&error);
509 typedef struct WaitData {
514 static DBusHandlerResult wait_filter(DBusConnection *connection, DBusMessage *message, void *data) {
522 dbus_error_init(&error);
524 /* log_debug("Got D-Bus request: %s.%s() on %s", */
525 /* dbus_message_get_interface(message), */
526 /* dbus_message_get_member(message), */
527 /* dbus_message_get_path(message)); */
529 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
530 log_error("Warning! D-Bus connection terminated.");
531 dbus_connection_close(connection);
533 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
536 dbus_bool_t success = true;
538 if (!dbus_message_get_args(message, &error,
539 DBUS_TYPE_UINT32, &id,
540 DBUS_TYPE_OBJECT_PATH, &path,
541 DBUS_TYPE_BOOLEAN, &success,
543 log_error("Failed to parse message: %s", error.message);
547 if ((p = set_remove(d->set, (char*) path)))
555 dbus_error_free(&error);
556 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
559 static int enable_wait_for_jobs(DBusConnection *bus) {
564 dbus_error_init(&error);
565 dbus_bus_add_match(bus,
567 "sender='org.freedesktop.systemd1',"
568 "interface='org.freedesktop.systemd1.Manager',"
569 "member='JobRemoved',"
570 "path='/org/freedesktop/systemd1'",
573 if (dbus_error_is_set(&error)) {
574 log_error("Failed to add match: %s", error.message);
575 dbus_error_free(&error);
579 /* This is slightly dirty, since we don't undo the match registrations. */
583 static int wait_for_jobs(DBusConnection *bus, Set *s) {
594 if (!dbus_connection_add_filter(bus, wait_filter, &d, NULL)) {
595 log_error("Failed to add filter.");
600 while (!set_isempty(s) &&
601 dbus_connection_read_write_dispatch(bus, -1))
604 if (!arg_quiet && d.failed)
605 log_error("Job failed, see logs for details.");
607 r = d.failed ? -EIO : 0;
610 /* This is slightly dirty, since we don't undo the filter registration. */
615 static int start_unit_one(
622 DBusMessage *m = NULL, *reply = NULL;
630 assert(arg_no_block || s);
632 dbus_error_init(&error);
634 if (!(m = dbus_message_new_method_call(
635 "org.freedesktop.systemd1",
636 "/org/freedesktop/systemd1",
637 "org.freedesktop.systemd1.Manager",
639 log_error("Could not allocate message.");
644 if (!dbus_message_append_args(m,
645 DBUS_TYPE_STRING, &name,
646 DBUS_TYPE_STRING, &mode,
647 DBUS_TYPE_INVALID)) {
648 log_error("Could not append arguments to message.");
653 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
655 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
656 /* There's always a fallback possible for
662 log_error("Failed to issue method call: %s", error.message);
671 if (!dbus_message_get_args(reply, &error,
672 DBUS_TYPE_OBJECT_PATH, &path,
673 DBUS_TYPE_INVALID)) {
674 log_error("Failed to parse reply: %s", error.message);
679 if (!(p = strdup(path))) {
680 log_error("Failed to duplicate path.");
685 if ((r = set_put(s, p)) < 0) {
687 log_error("Failed to add path to set.");
696 dbus_message_unref(m);
699 dbus_message_unref(reply);
701 dbus_error_free(&error);
706 static enum action verb_to_action(const char *verb) {
707 if (streq(verb, "halt"))
709 else if (streq(verb, "poweroff"))
710 return ACTION_POWEROFF;
711 else if (streq(verb, "reboot"))
712 return ACTION_REBOOT;
713 else if (streq(verb, "rescue"))
714 return ACTION_RESCUE;
715 else if (streq(verb, "emergency"))
716 return ACTION_EMERGENCY;
717 else if (streq(verb, "default"))
718 return ACTION_DEFAULT;
720 return ACTION_INVALID;
723 static int start_unit(DBusConnection *bus, char **args, unsigned n) {
725 static const char * const table[_ACTION_MAX] = {
726 [ACTION_HALT] = SPECIAL_HALT_TARGET,
727 [ACTION_POWEROFF] = SPECIAL_POWEROFF_TARGET,
728 [ACTION_REBOOT] = SPECIAL_REBOOT_TARGET,
729 [ACTION_RUNLEVEL2] = SPECIAL_RUNLEVEL2_TARGET,
730 [ACTION_RUNLEVEL3] = SPECIAL_RUNLEVEL3_TARGET,
731 [ACTION_RUNLEVEL4] = SPECIAL_RUNLEVEL4_TARGET,
732 [ACTION_RUNLEVEL5] = SPECIAL_RUNLEVEL5_TARGET,
733 [ACTION_RESCUE] = SPECIAL_RESCUE_TARGET,
734 [ACTION_EMERGENCY] = SPECIAL_EMERGENCY_SERVICE,
735 [ACTION_DEFAULT] = SPECIAL_DEFAULT_TARGET
740 const char *method, *mode, *one_name;
745 if (arg_action == ACTION_SYSTEMCTL) {
747 streq(args[0], "stop") ? "StopUnit" :
748 streq(args[0], "reload") ? "ReloadUnit" :
749 streq(args[0], "restart") ? "RestartUnit" :
753 (streq(args[0], "isolate") ||
754 streq(args[0], "rescue") ||
755 streq(args[0], "emergency")) ? "isolate" :
756 arg_replace ? "replace" :
759 one_name = table[verb_to_action(args[0])];
762 assert(arg_action < ELEMENTSOF(table));
763 assert(table[arg_action]);
765 method = "StartUnit";
767 mode = (arg_action == ACTION_EMERGENCY ||
768 arg_action == ACTION_RESCUE) ? "isolate" : "replace";
770 one_name = table[arg_action];
774 if ((r = enable_wait_for_jobs(bus)) < 0) {
775 log_error("Could not watch jobs: %s", strerror(-r));
779 if (!(s = set_new(string_hash_func, string_compare_func))) {
780 log_error("Failed to allocate set.");
789 if ((r = start_unit_one(bus, method, one_name, mode, s)) <= 0)
792 for (i = 1; i < n; i++)
793 if ((r = start_unit_one(bus, method, args[i], mode, s)) < 0)
798 r = wait_for_jobs(bus, s);
807 static int start_special(DBusConnection *bus, char **args, unsigned n) {
811 warn_wall(verb_to_action(args[0]));
813 return start_unit(bus, args, n);
816 static int check_unit(DBusConnection *bus, char **args, unsigned n) {
817 DBusMessage *m = NULL, *reply = NULL;
819 *interface = "org.freedesktop.systemd1.Unit",
820 *property = "ActiveState";
821 int r = -EADDRNOTAVAIL;
828 dbus_error_init(&error);
830 for (i = 1; i < n; i++) {
831 const char *path = NULL;
833 DBusMessageIter iter, sub;
835 if (!(m = dbus_message_new_method_call(
836 "org.freedesktop.systemd1",
837 "/org/freedesktop/systemd1",
838 "org.freedesktop.systemd1.Manager",
840 log_error("Could not allocate message.");
845 if (!dbus_message_append_args(m,
846 DBUS_TYPE_STRING, &args[i],
847 DBUS_TYPE_INVALID)) {
848 log_error("Could not append arguments to message.");
853 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
855 /* Hmm, cannot figure out anything about this unit... */
862 if (!dbus_message_get_args(reply, &error,
863 DBUS_TYPE_OBJECT_PATH, &path,
864 DBUS_TYPE_INVALID)) {
865 log_error("Failed to parse reply: %s", error.message);
870 dbus_message_unref(m);
871 if (!(m = dbus_message_new_method_call(
872 "org.freedesktop.systemd1",
874 "org.freedesktop.DBus.Properties",
876 log_error("Could not allocate message.");
881 if (!dbus_message_append_args(m,
882 DBUS_TYPE_STRING, &interface,
883 DBUS_TYPE_STRING, &property,
884 DBUS_TYPE_INVALID)) {
885 log_error("Could not append arguments to message.");
890 dbus_message_unref(reply);
891 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
892 log_error("Failed to issue method call: %s", error.message);
897 if (!dbus_message_iter_init(reply, &iter) ||
898 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
899 log_error("Failed to parse reply.");
904 dbus_message_iter_recurse(&iter, &sub);
906 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
907 log_error("Failed to parse reply.");
912 dbus_message_iter_get_basic(&sub, &state);
917 if (streq(state, "active") || startswith(state, "reloading"))
920 dbus_message_unref(m);
921 dbus_message_unref(reply);
927 dbus_message_unref(m);
930 dbus_message_unref(reply);
932 dbus_error_free(&error);
937 static void show_cgroup(const char *name) {
942 if (!startswith(name, "name=systemd:"))
945 if (asprintf(&fn, "/cgroup/systemd/%s/tasks", name + 13) < 0)
954 printf("\t\t \342\224\202\n");
959 if (fscanf(f, "%lu", &ul) != 1)
967 get_process_cmdline(last, 60, &t);
968 printf("\t\t \342\224\234 %lu %s\n", (unsigned long) last, strna(t));
977 get_process_cmdline(last, 60, &t);
978 printf("\t\t \342\224\224 %lu %s\n", (unsigned long) last, strna(t));
985 typedef struct UnitStatusInfo {
987 const char *load_state;
988 const char *active_state;
989 const char *sub_state;
991 const char *description;
993 const char *fragment_path;
994 const char *default_control_group;
999 const char *status_text;
1002 usec_t start_timestamp;
1003 usec_t exit_timestamp;
1005 int exit_code, exit_status;
1008 unsigned n_accepted;
1009 unsigned n_connections;
1012 const char *sysfs_path;
1014 /* Mount, Automount */
1021 static void print_status_info(UnitStatusInfo *i) {
1024 /* This shows pretty information about a unit. See
1025 * print_property() for a low-level property printer */
1027 printf("%s", strna(i->id));
1029 if (i->description && !streq_ptr(i->id, i->description))
1030 printf(" - %s", i->description);
1034 if (i->fragment_path)
1035 printf("\t Loaded: %s (%s)\n", strna(i->load_state), i->fragment_path);
1036 else if (streq_ptr(i->load_state, "failed"))
1037 printf("\t Loaded: " ANSI_HIGHLIGHT_ON "%s" ANSI_HIGHLIGHT_OFF "\n", strna(i->load_state));
1039 printf("\t Loaded: %s\n", strna(i->load_state));
1041 if (streq_ptr(i->active_state, "maintenance"))
1042 printf("\t Active: " ANSI_HIGHLIGHT_ON "%s (%s)" ANSI_HIGHLIGHT_OFF "\n",
1043 strna(i->active_state),
1044 strna(i->sub_state));
1046 printf("\t Active: %s (%s)\n",
1047 strna(i->active_state),
1048 strna(i->sub_state));
1051 printf("\t Device: %s\n", i->sysfs_path);
1053 printf("\t Where: %s\n", i->where);
1055 printf("\t What: %s\n", i->what);
1058 printf("\t Status: \"%s\"\n", i->status_text);
1060 if (i->id && endswith(i->id, ".socket"))
1061 printf("\tAccepted: %u; Connected: %u\n", i->n_accepted, i->n_connections);
1063 if (i->main_pid > 0 || i->control_pid > 0) {
1066 if (i->main_pid > 0) {
1067 printf(" Process: %u", (unsigned) i->main_pid);
1071 get_process_name(i->main_pid, &t);
1077 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
1079 if (i->exit_code == CLD_EXITED)
1080 printf("status=%i", i->exit_status);
1082 printf("signal=%s", strsignal(i->exit_status));
1087 if (i->main_pid > 0 && i->control_pid > 0)
1090 if (i->control_pid > 0) {
1093 printf(" Control: %u", (unsigned) i->control_pid);
1095 get_process_name(i->control_pid, &t);
1105 if (i->default_control_group) {
1106 printf("\t CGroup: %s\n", i->default_control_group);
1107 show_cgroup(i->default_control_group);
1111 static int status_property(const char *name, DBusMessageIter *iter, UnitStatusInfo *i) {
1113 switch (dbus_message_iter_get_arg_type(iter)) {
1115 case DBUS_TYPE_STRING: {
1118 dbus_message_iter_get_basic(iter, &s);
1121 if (streq(name, "Id"))
1123 else if (streq(name, "LoadState"))
1125 else if (streq(name, "ActiveState"))
1126 i->active_state = s;
1127 else if (streq(name, "SubState"))
1129 else if (streq(name, "Description"))
1131 else if (streq(name, "FragmentPath"))
1132 i->fragment_path = s;
1133 else if (streq(name, "DefaultControlGroup"))
1134 i->default_control_group = s;
1135 else if (streq(name, "StatusText"))
1137 else if (streq(name, "SysFSPath"))
1139 else if (streq(name, "Where"))
1141 else if (streq(name, "What"))
1148 case DBUS_TYPE_UINT32: {
1151 dbus_message_iter_get_basic(iter, &u);
1153 if (streq(name, "MainPID")) {
1155 i->main_pid = (pid_t) u;
1158 } else if (streq(name, "ControlPID"))
1159 i->control_pid = (pid_t) u;
1160 else if (streq(name, "ExecMainPID")) {
1162 i->main_pid = (pid_t) u;
1163 } else if (streq(name, "NAccepted"))
1165 else if (streq(name, "NConnections"))
1166 i->n_connections = u;
1171 case DBUS_TYPE_INT32: {
1174 dbus_message_iter_get_basic(iter, &j);
1176 if (streq(name, "ExecMainCode"))
1177 i->exit_code = (int) j;
1178 else if (streq(name, "ExecMainStatus"))
1179 i->exit_status = (int) j;
1184 case DBUS_TYPE_UINT64: {
1187 dbus_message_iter_get_basic(iter, &u);
1189 if (streq(name, "ExecMainStartTimestamp"))
1190 i->start_timestamp = (usec_t) u;
1191 else if (streq(name, "ExecMainExitTimestamp"))
1192 i->exit_timestamp = (usec_t) u;
1201 static int print_property(const char *name, DBusMessageIter *iter) {
1205 /* This is a low-level property printer, see
1206 * print_status_info() for the nicer output */
1208 if (arg_property && !streq(name, arg_property))
1211 switch (dbus_message_iter_get_arg_type(iter)) {
1213 case DBUS_TYPE_STRING: {
1215 dbus_message_iter_get_basic(iter, &s);
1217 if (arg_all || s[0])
1218 printf("%s=%s\n", name, s);
1223 case DBUS_TYPE_BOOLEAN: {
1225 dbus_message_iter_get_basic(iter, &b);
1226 printf("%s=%s\n", name, yes_no(b));
1231 case DBUS_TYPE_UINT64: {
1233 dbus_message_iter_get_basic(iter, &u);
1235 /* Yes, heuristics! But we can change this check
1236 * should it turn out to not be sufficient */
1238 if (strstr(name, "Timestamp")) {
1239 char timestamp[FORMAT_TIMESTAMP_MAX], *t;
1241 if ((t = format_timestamp(timestamp, sizeof(timestamp), u)) || arg_all)
1242 printf("%s=%s\n", name, strempty(t));
1243 } else if (strstr(name, "USec")) {
1244 char timespan[FORMAT_TIMESPAN_MAX];
1246 printf("%s=%s\n", name, format_timespan(timespan, sizeof(timespan), u));
1248 printf("%s=%llu\n", name, (unsigned long long) u);
1253 case DBUS_TYPE_UINT32: {
1255 dbus_message_iter_get_basic(iter, &u);
1257 if (strstr(name, "UMask") || strstr(name, "Mode"))
1258 printf("%s=%04o\n", name, u);
1260 printf("%s=%u\n", name, (unsigned) u);
1265 case DBUS_TYPE_INT32: {
1267 dbus_message_iter_get_basic(iter, &i);
1269 printf("%s=%i\n", name, (int) i);
1273 case DBUS_TYPE_STRUCT: {
1274 DBusMessageIter sub;
1275 dbus_message_iter_recurse(iter, &sub);
1277 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "Job")) {
1280 dbus_message_iter_get_basic(&sub, &u);
1283 printf("%s=%u\n", name, (unsigned) u);
1285 printf("%s=\n", name);
1288 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Unit")) {
1291 dbus_message_iter_get_basic(&sub, &s);
1293 if (arg_all || s[0])
1294 printf("%s=%s\n", name, s);
1302 case DBUS_TYPE_ARRAY:
1304 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
1305 DBusMessageIter sub;
1308 dbus_message_iter_recurse(iter, &sub);
1311 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1312 printf("%s=", name);
1314 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1317 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
1318 dbus_message_iter_get_basic(&sub, &s);
1319 printf("%s%s", space ? " " : "", s);
1322 dbus_message_iter_next(&sub);
1329 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_BYTE) {
1330 DBusMessageIter sub;
1332 dbus_message_iter_recurse(iter, &sub);
1335 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1336 printf("%s=", name);
1338 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1341 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_BYTE);
1342 dbus_message_iter_get_basic(&sub, &u);
1345 dbus_message_iter_next(&sub);
1352 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Paths")) {
1353 DBusMessageIter sub, sub2;
1355 dbus_message_iter_recurse(iter, &sub);
1357 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1358 const char *type, *path;
1360 dbus_message_iter_recurse(&sub, &sub2);
1362 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
1363 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0)
1364 printf("%s=%s\n", type, path);
1366 dbus_message_iter_next(&sub);
1370 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Timers")) {
1371 DBusMessageIter sub, sub2;
1373 dbus_message_iter_recurse(iter, &sub);
1375 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1377 uint64_t value, next_elapse;
1379 dbus_message_iter_recurse(&sub, &sub2);
1381 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &base, true) >= 0 &&
1382 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &value, true) >= 0 &&
1383 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &next_elapse, false) >= 0) {
1384 char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX];
1386 printf("%s={ value=%s ; next_elapse=%s }\n",
1388 format_timespan(timespan1, sizeof(timespan1), value),
1389 format_timespan(timespan2, sizeof(timespan2), next_elapse));
1392 dbus_message_iter_next(&sub);
1396 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && startswith(name, "Exec")) {
1398 DBusMessageIter sub, sub2, sub3;
1400 dbus_message_iter_recurse(iter, &sub);
1402 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1404 uint64_t start_time, exit_time;
1406 int32_t code, status;
1408 dbus_message_iter_recurse(&sub, &sub2);
1410 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0)
1413 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_ARRAY ||
1414 dbus_message_iter_get_element_type(&sub2) != DBUS_TYPE_STRING)
1417 printf("%s={ path=%s ; argv[]=", name, path);
1419 dbus_message_iter_recurse(&sub2, &sub3);
1421 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1424 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1425 dbus_message_iter_get_basic(&sub3, &s);
1427 dbus_message_iter_next(&sub3);
1430 if (dbus_message_iter_next(&sub2) &&
1431 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_time, true) >= 0 &&
1432 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_time, true) >= 0 &&
1433 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, true) >= 0 &&
1434 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &code, true) >= 0 &&
1435 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &status, false) >= 0) {
1437 char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
1439 printf("; start=%s ; stop=%s ; pid=%u ; code=%s ; status=%i/%s",
1440 strna(format_timestamp(timestamp1, sizeof(timestamp1), start_time)),
1441 strna(format_timestamp(timestamp2, sizeof(timestamp2), exit_time)),
1443 sigchld_code_to_string(code),
1445 strempty(code == CLD_EXITED ? NULL : strsignal(status)));
1450 dbus_message_iter_next(&sub);
1460 printf("%s=[unprintable]\n", name);
1465 static int show_one(DBusConnection *bus, const char *path, bool show_properties, bool *new_line) {
1466 DBusMessage *m = NULL, *reply = NULL;
1467 const char *interface = "";
1470 DBusMessageIter iter, sub, sub2, sub3;
1471 UnitStatusInfo info;
1478 dbus_error_init(&error);
1480 if (!(m = dbus_message_new_method_call(
1481 "org.freedesktop.systemd1",
1483 "org.freedesktop.DBus.Properties",
1485 log_error("Could not allocate message.");
1490 if (!dbus_message_append_args(m,
1491 DBUS_TYPE_STRING, &interface,
1492 DBUS_TYPE_INVALID)) {
1493 log_error("Could not append arguments to message.");
1498 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1499 log_error("Failed to issue method call: %s", error.message);
1504 if (!dbus_message_iter_init(reply, &iter) ||
1505 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
1506 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
1507 log_error("Failed to parse reply.");
1512 dbus_message_iter_recurse(&iter, &sub);
1519 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1522 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
1523 log_error("Failed to parse reply.");
1528 dbus_message_iter_recurse(&sub, &sub2);
1530 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
1531 log_error("Failed to parse reply.");
1536 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
1537 log_error("Failed to parse reply.");
1542 dbus_message_iter_recurse(&sub2, &sub3);
1544 if (show_properties)
1545 r = print_property(name, &sub3);
1547 r = status_property(name, &sub3, &info);
1550 log_error("Failed to parse reply.");
1555 dbus_message_iter_next(&sub);
1558 if (!show_properties)
1559 print_status_info(&info);
1565 dbus_message_unref(m);
1568 dbus_message_unref(reply);
1570 dbus_error_free(&error);
1575 static int show(DBusConnection *bus, char **args, unsigned n) {
1576 DBusMessage *m = NULL, *reply = NULL;
1580 bool show_properties, new_line = false;
1585 dbus_error_init(&error);
1587 show_properties = !streq(args[0], "status");
1589 if (show_properties && n <= 1) {
1590 /* If not argument is specified inspect the manager
1593 r = show_one(bus, "/org/freedesktop/systemd1", show_properties, &new_line);
1597 for (i = 1; i < n; i++) {
1598 const char *path = NULL;
1601 if (!show_properties || safe_atou32(args[i], &id) < 0) {
1603 if (!(m = dbus_message_new_method_call(
1604 "org.freedesktop.systemd1",
1605 "/org/freedesktop/systemd1",
1606 "org.freedesktop.systemd1.Manager",
1608 log_error("Could not allocate message.");
1613 if (!dbus_message_append_args(m,
1614 DBUS_TYPE_STRING, &args[i],
1615 DBUS_TYPE_INVALID)) {
1616 log_error("Could not append arguments to message.");
1623 if (!(m = dbus_message_new_method_call(
1624 "org.freedesktop.systemd1",
1625 "/org/freedesktop/systemd1",
1626 "org.freedesktop.systemd1.Manager",
1628 log_error("Could not allocate message.");
1633 if (!dbus_message_append_args(m,
1634 DBUS_TYPE_UINT32, &id,
1635 DBUS_TYPE_INVALID)) {
1636 log_error("Could not append arguments to message.");
1642 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1643 log_error("Failed to issue method call: %s", error.message);
1648 if (!dbus_message_get_args(reply, &error,
1649 DBUS_TYPE_OBJECT_PATH, &path,
1650 DBUS_TYPE_INVALID)) {
1651 log_error("Failed to parse reply: %s", error.message);
1656 if ((r = show_one(bus, path, show_properties, &new_line)) < 0)
1659 dbus_message_unref(m);
1660 dbus_message_unref(reply);
1668 dbus_message_unref(m);
1671 dbus_message_unref(reply);
1673 dbus_error_free(&error);
1678 static DBusHandlerResult monitor_filter(DBusConnection *connection, DBusMessage *message, void *data) {
1680 DBusMessage *m = NULL, *reply = NULL;
1685 dbus_error_init(&error);
1687 /* log_debug("Got D-Bus request: %s.%s() on %s", */
1688 /* dbus_message_get_interface(message), */
1689 /* dbus_message_get_member(message), */
1690 /* dbus_message_get_path(message)); */
1692 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
1693 log_error("Warning! D-Bus connection terminated.");
1694 dbus_connection_close(connection);
1696 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitNew") ||
1697 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitRemoved")) {
1698 const char *id, *path;
1700 if (!dbus_message_get_args(message, &error,
1701 DBUS_TYPE_STRING, &id,
1702 DBUS_TYPE_OBJECT_PATH, &path,
1704 log_error("Failed to parse message: %s", error.message);
1705 else if (streq(dbus_message_get_member(message), "UnitNew"))
1706 printf("Unit %s added.\n", id);
1708 printf("Unit %s removed.\n", id);
1710 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobNew") ||
1711 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
1715 if (!dbus_message_get_args(message, &error,
1716 DBUS_TYPE_UINT32, &id,
1717 DBUS_TYPE_OBJECT_PATH, &path,
1719 log_error("Failed to parse message: %s", error.message);
1720 else if (streq(dbus_message_get_member(message), "JobNew"))
1721 printf("Job %u added.\n", id);
1723 printf("Job %u removed.\n", id);
1726 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Unit", "Changed") ||
1727 dbus_message_is_signal(message, "org.freedesktop.systemd1.Job", "Changed")) {
1729 const char *path, *interface, *property = "Id";
1730 DBusMessageIter iter, sub;
1732 path = dbus_message_get_path(message);
1733 interface = dbus_message_get_interface(message);
1735 if (!(m = dbus_message_new_method_call(
1736 "org.freedesktop.systemd1",
1738 "org.freedesktop.DBus.Properties",
1740 log_error("Could not allocate message.");
1744 if (!dbus_message_append_args(m,
1745 DBUS_TYPE_STRING, &interface,
1746 DBUS_TYPE_STRING, &property,
1747 DBUS_TYPE_INVALID)) {
1748 log_error("Could not append arguments to message.");
1752 if (!(reply = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) {
1753 log_error("Failed to issue method call: %s", error.message);
1757 if (!dbus_message_iter_init(reply, &iter) ||
1758 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1759 log_error("Failed to parse reply.");
1763 dbus_message_iter_recurse(&iter, &sub);
1765 if (streq(interface, "org.freedesktop.systemd1.Unit")) {
1768 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1769 log_error("Failed to parse reply.");
1773 dbus_message_iter_get_basic(&sub, &id);
1774 printf("Unit %s changed.\n", id);
1778 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT32) {
1779 log_error("Failed to parse reply.");
1783 dbus_message_iter_get_basic(&sub, &id);
1784 printf("Job %u changed.\n", id);
1790 dbus_message_unref(m);
1793 dbus_message_unref(reply);
1795 dbus_error_free(&error);
1796 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1800 dbus_message_unref(m);
1803 dbus_message_unref(reply);
1805 dbus_error_free(&error);
1806 return DBUS_HANDLER_RESULT_NEED_MEMORY;
1809 static int monitor(DBusConnection *bus, char **args, unsigned n) {
1810 DBusMessage *m = NULL, *reply = NULL;
1814 dbus_error_init(&error);
1816 dbus_bus_add_match(bus,
1818 "sender='org.freedesktop.systemd1',"
1819 "interface='org.freedesktop.systemd1.Manager',"
1820 "path='/org/freedesktop/systemd1'",
1823 if (dbus_error_is_set(&error)) {
1824 log_error("Failed to add match: %s", error.message);
1829 dbus_bus_add_match(bus,
1831 "sender='org.freedesktop.systemd1',"
1832 "interface='org.freedesktop.systemd1.Unit',"
1836 if (dbus_error_is_set(&error)) {
1837 log_error("Failed to add match: %s", error.message);
1842 dbus_bus_add_match(bus,
1844 "sender='org.freedesktop.systemd1',"
1845 "interface='org.freedesktop.systemd1.Job',"
1849 if (dbus_error_is_set(&error)) {
1850 log_error("Failed to add match: %s", error.message);
1855 if (!dbus_connection_add_filter(bus, monitor_filter, NULL, NULL)) {
1856 log_error("Failed to add filter.");
1861 if (!(m = dbus_message_new_method_call(
1862 "org.freedesktop.systemd1",
1863 "/org/freedesktop/systemd1",
1864 "org.freedesktop.systemd1.Manager",
1866 log_error("Could not allocate message.");
1871 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1872 log_error("Failed to issue method call: %s", error.message);
1877 while (dbus_connection_read_write_dispatch(bus, -1))
1884 /* This is slightly dirty, since we don't undo the filter or the matches. */
1887 dbus_message_unref(m);
1890 dbus_message_unref(reply);
1892 dbus_error_free(&error);
1897 static int dump(DBusConnection *bus, char **args, unsigned n) {
1898 DBusMessage *m = NULL, *reply = NULL;
1903 dbus_error_init(&error);
1905 if (!(m = dbus_message_new_method_call(
1906 "org.freedesktop.systemd1",
1907 "/org/freedesktop/systemd1",
1908 "org.freedesktop.systemd1.Manager",
1910 log_error("Could not allocate message.");
1914 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1915 log_error("Failed to issue method call: %s", error.message);
1920 if (!dbus_message_get_args(reply, &error,
1921 DBUS_TYPE_STRING, &text,
1922 DBUS_TYPE_INVALID)) {
1923 log_error("Failed to parse reply: %s", error.message);
1928 fputs(text, stdout);
1934 dbus_message_unref(m);
1937 dbus_message_unref(reply);
1939 dbus_error_free(&error);
1944 static int snapshot(DBusConnection *bus, char **args, unsigned n) {
1945 DBusMessage *m = NULL, *reply = NULL;
1948 const char *name = "", *path, *id;
1949 dbus_bool_t cleanup = FALSE;
1950 DBusMessageIter iter, sub;
1952 *interface = "org.freedesktop.systemd1.Unit",
1955 dbus_error_init(&error);
1957 if (!(m = dbus_message_new_method_call(
1958 "org.freedesktop.systemd1",
1959 "/org/freedesktop/systemd1",
1960 "org.freedesktop.systemd1.Manager",
1961 "CreateSnapshot"))) {
1962 log_error("Could not allocate message.");
1969 if (!dbus_message_append_args(m,
1970 DBUS_TYPE_STRING, &name,
1971 DBUS_TYPE_BOOLEAN, &cleanup,
1972 DBUS_TYPE_INVALID)) {
1973 log_error("Could not append arguments to message.");
1978 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1979 log_error("Failed to issue method call: %s", error.message);
1984 if (!dbus_message_get_args(reply, &error,
1985 DBUS_TYPE_OBJECT_PATH, &path,
1986 DBUS_TYPE_INVALID)) {
1987 log_error("Failed to parse reply: %s", error.message);
1992 dbus_message_unref(m);
1993 if (!(m = dbus_message_new_method_call(
1994 "org.freedesktop.systemd1",
1996 "org.freedesktop.DBus.Properties",
1998 log_error("Could not allocate message.");
2002 if (!dbus_message_append_args(m,
2003 DBUS_TYPE_STRING, &interface,
2004 DBUS_TYPE_STRING, &property,
2005 DBUS_TYPE_INVALID)) {
2006 log_error("Could not append arguments to message.");
2011 dbus_message_unref(reply);
2012 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2013 log_error("Failed to issue method call: %s", error.message);
2018 if (!dbus_message_iter_init(reply, &iter) ||
2019 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2020 log_error("Failed to parse reply.");
2025 dbus_message_iter_recurse(&iter, &sub);
2027 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2028 log_error("Failed to parse reply.");
2033 dbus_message_iter_get_basic(&sub, &id);
2041 dbus_message_unref(m);
2044 dbus_message_unref(reply);
2046 dbus_error_free(&error);
2051 static int delete_snapshot(DBusConnection *bus, char **args, unsigned n) {
2052 DBusMessage *m = NULL, *reply = NULL;
2060 dbus_error_init(&error);
2062 for (i = 1; i < n; i++) {
2063 const char *path = NULL;
2065 if (!(m = dbus_message_new_method_call(
2066 "org.freedesktop.systemd1",
2067 "/org/freedesktop/systemd1",
2068 "org.freedesktop.systemd1.Manager",
2070 log_error("Could not allocate message.");
2075 if (!dbus_message_append_args(m,
2076 DBUS_TYPE_STRING, &args[i],
2077 DBUS_TYPE_INVALID)) {
2078 log_error("Could not append arguments to message.");
2083 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2084 log_error("Failed to issue method call: %s", error.message);
2089 if (!dbus_message_get_args(reply, &error,
2090 DBUS_TYPE_OBJECT_PATH, &path,
2091 DBUS_TYPE_INVALID)) {
2092 log_error("Failed to parse reply: %s", error.message);
2097 dbus_message_unref(m);
2098 if (!(m = dbus_message_new_method_call(
2099 "org.freedesktop.systemd1",
2101 "org.freedesktop.systemd1.Snapshot",
2103 log_error("Could not allocate message.");
2108 dbus_message_unref(reply);
2109 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2110 log_error("Failed to issue method call: %s", error.message);
2115 dbus_message_unref(m);
2116 dbus_message_unref(reply);
2124 dbus_message_unref(m);
2127 dbus_message_unref(reply);
2129 dbus_error_free(&error);
2134 static int clear_jobs(DBusConnection *bus, char **args, unsigned n) {
2135 DBusMessage *m = NULL, *reply = NULL;
2140 dbus_error_init(&error);
2142 if (arg_action == ACTION_RELOAD)
2144 else if (arg_action == ACTION_REEXEC)
2145 method = "Reexecute";
2147 assert(arg_action == ACTION_SYSTEMCTL);
2150 streq(args[0], "clear-jobs") ? "ClearJobs" :
2151 streq(args[0], "daemon-reload") ? "Reload" :
2152 streq(args[0], "daemon-reexec") ? "Reexecute" :
2156 if (!(m = dbus_message_new_method_call(
2157 "org.freedesktop.systemd1",
2158 "/org/freedesktop/systemd1",
2159 "org.freedesktop.systemd1.Manager",
2161 log_error("Could not allocate message.");
2165 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2167 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
2168 /* There's always a fallback possible for
2169 * legacy actions. */
2174 log_error("Failed to issue method call: %s", error.message);
2183 dbus_message_unref(m);
2186 dbus_message_unref(reply);
2188 dbus_error_free(&error);
2193 static int show_enviroment(DBusConnection *bus, char **args, unsigned n) {
2194 DBusMessage *m = NULL, *reply = NULL;
2196 DBusMessageIter iter, sub, sub2;
2199 *interface = "org.freedesktop.systemd1.Manager",
2200 *property = "Environment";
2202 dbus_error_init(&error);
2204 if (!(m = dbus_message_new_method_call(
2205 "org.freedesktop.systemd1",
2206 "/org/freedesktop/systemd1",
2207 "org.freedesktop.DBus.Properties",
2209 log_error("Could not allocate message.");
2213 if (!dbus_message_append_args(m,
2214 DBUS_TYPE_STRING, &interface,
2215 DBUS_TYPE_STRING, &property,
2216 DBUS_TYPE_INVALID)) {
2217 log_error("Could not append arguments to message.");
2222 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2223 log_error("Failed to issue method call: %s", error.message);
2228 if (!dbus_message_iter_init(reply, &iter) ||
2229 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2230 log_error("Failed to parse reply.");
2235 dbus_message_iter_recurse(&iter, &sub);
2237 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
2238 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
2239 log_error("Failed to parse reply.");
2244 dbus_message_iter_recurse(&sub, &sub2);
2246 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
2249 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
2250 log_error("Failed to parse reply.");
2255 dbus_message_iter_get_basic(&sub2, &text);
2256 printf("%s\n", text);
2258 dbus_message_iter_next(&sub2);
2265 dbus_message_unref(m);
2268 dbus_message_unref(reply);
2270 dbus_error_free(&error);
2275 static int set_environment(DBusConnection *bus, char **args, unsigned n) {
2276 DBusMessage *m = NULL, *reply = NULL;
2280 DBusMessageIter iter, sub;
2283 dbus_error_init(&error);
2285 method = streq(args[0], "set-environment")
2287 : "UnsetEnvironment";
2289 if (!(m = dbus_message_new_method_call(
2290 "org.freedesktop.systemd1",
2291 "/org/freedesktop/systemd1",
2292 "org.freedesktop.systemd1.Manager",
2295 log_error("Could not allocate message.");
2299 dbus_message_iter_init_append(m, &iter);
2301 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
2302 log_error("Could not append arguments to message.");
2307 for (i = 1; i < n; i++)
2308 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &args[i])) {
2309 log_error("Could not append arguments to message.");
2314 if (!dbus_message_iter_close_container(&iter, &sub)) {
2315 log_error("Could not append arguments to message.");
2320 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2321 log_error("Failed to issue method call: %s", error.message);
2330 dbus_message_unref(m);
2333 dbus_message_unref(reply);
2335 dbus_error_free(&error);
2340 static int systemctl_help(void) {
2342 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
2343 "Send control commands to the systemd manager.\n\n"
2344 " -h --help Show this help\n"
2345 " -t --type=TYPE List only units of a particular type\n"
2346 " -p --property=NAME Show only properties by this name\n"
2347 " -a --all Show all units/properties, including dead/empty ones\n"
2348 " --replace When installing a new job, replace existing conflicting ones\n"
2349 " --system Connect to system bus\n"
2350 " --session Connect to session bus\n"
2351 " -q --quiet Suppress output\n"
2352 " --no-block Do not wait until operation finished\n"
2353 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
2355 " list-units List units\n"
2356 " start [NAME...] Start one or more units\n"
2357 " stop [NAME...] Stop one or more units\n"
2358 " restart [NAME...] Restart one or more units\n"
2359 " reload [NAME...] Reload one or more units\n"
2360 " isolate [NAME] Start one unit and stop all others\n"
2361 " check [NAME...] Check whether any of the passed units are active\n"
2362 " status [NAME...] Show status of one or more units\n"
2363 " show [NAME...|JOB...] Show properties of one or more units/jobs/manager\n"
2364 " load [NAME...] Load one or more units\n"
2365 " list-jobs List jobs\n"
2366 " cancel [JOB...] Cancel one or more jobs\n"
2367 " clear-jobs Cancel all jobs\n"
2368 " monitor Monitor unit/job changes\n"
2369 " dump Dump server status\n"
2370 " snapshot [NAME] Create a snapshot\n"
2371 " delete [NAME...] Remove one or more snapshots\n"
2372 " daemon-reload Reload systemd manager configuration\n"
2373 " daemon-reexec Reexecute systemd manager\n"
2374 " daemon-exit Ask the systemd manager to quit\n"
2375 " show-environment Dump environment\n"
2376 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
2377 " unset-environment [NAME...] Unset one or more environment variables\n"
2378 " halt Shut down and halt the system\n"
2379 " poweroff Shut down and power-off the system\n"
2380 " reboot Shut down and reboot the system\n"
2381 " default Enter default mode\n"
2382 " rescue Enter rescue mode\n"
2383 " emergency Enter emergency mode\n",
2384 program_invocation_short_name);
2389 static int halt_help(void) {
2391 printf("%s [OPTIONS...]\n\n"
2392 "%s the system.\n\n"
2393 " --help Show this help\n"
2394 " --halt Halt the machine\n"
2395 " -p --poweroff Switch off the machine\n"
2396 " --reboot Reboot the machine\n"
2397 " -f --force Force immediate halt/power-off/reboot\n"
2398 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
2399 " -d --no-wtmp Don't write wtmp record\n"
2400 " -n --no-sync Don't sync before halt/power-off/reboot\n"
2401 " --no-wall Don't send wall message before halt/power-off/reboot\n",
2402 program_invocation_short_name,
2403 arg_action == ACTION_REBOOT ? "Reboot" :
2404 arg_action == ACTION_POWEROFF ? "Power off" :
2410 static int shutdown_help(void) {
2412 printf("%s [OPTIONS...] [now] [WALL...]\n\n"
2413 "Shut down the system.\n\n"
2414 " --help Show this help\n"
2415 " -H --halt Halt the machine\n"
2416 " -P --poweroff Power-off the machine\n"
2417 " -r --reboot Reboot the machine\n"
2418 " -h Equivalent to --poweroff, overriden by --halt\n"
2419 " -k Don't halt/power-off/reboot, just send warnings\n"
2420 " --no-wall Don't send wall message before halt/power-off/reboot\n",
2421 program_invocation_short_name);
2426 static int telinit_help(void) {
2428 printf("%s [OPTIONS...] {COMMAND}\n\n"
2429 "Send control commands to the init daemon.\n\n"
2430 " --help Show this help\n"
2431 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
2433 " 0 Power-off the machine\n"
2434 " 6 Reboot the machine\n"
2435 " 2, 3, 4, 5 Start runlevelX.target unit\n"
2436 " 1, s, S Enter rescue mode\n"
2437 " q, Q Reload init daemon configuration\n"
2438 " u, U Reexecute init daemon\n",
2439 program_invocation_short_name);
2444 static int runlevel_help(void) {
2446 printf("%s [OPTIONS...]\n\n"
2447 "Prints the previous and current runlevel of the init system.\n\n"
2448 " --help Show this help\n",
2449 program_invocation_short_name);
2454 static int systemctl_parse_argv(int argc, char *argv[]) {
2457 ARG_REPLACE = 0x100,
2464 static const struct option options[] = {
2465 { "help", no_argument, NULL, 'h' },
2466 { "type", required_argument, NULL, 't' },
2467 { "property", required_argument, NULL, 'p' },
2468 { "all", no_argument, NULL, 'a' },
2469 { "replace", no_argument, NULL, ARG_REPLACE },
2470 { "session", no_argument, NULL, ARG_SESSION },
2471 { "system", no_argument, NULL, ARG_SYSTEM },
2472 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
2473 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2474 { "quiet", no_argument, NULL, 'q' },
2475 { NULL, 0, NULL, 0 }
2483 while ((c = getopt_long(argc, argv, "ht:p:aq", options, NULL)) >= 0) {
2496 arg_property = optarg;
2498 /* If the user asked for a particular
2499 * property, show it to him, even if it is
2517 arg_session = false;
2521 arg_no_block = true;
2536 log_error("Unknown option code %c", c);
2544 static int halt_parse_argv(int argc, char *argv[]) {
2553 static const struct option options[] = {
2554 { "help", no_argument, NULL, ARG_HELP },
2555 { "halt", no_argument, NULL, ARG_HALT },
2556 { "poweroff", no_argument, NULL, 'p' },
2557 { "reboot", no_argument, NULL, ARG_REBOOT },
2558 { "force", no_argument, NULL, 'f' },
2559 { "wtmp-only", no_argument, NULL, 'w' },
2560 { "no-wtmp", no_argument, NULL, 'd' },
2561 { "no-sync", no_argument, NULL, 'n' },
2562 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2563 { NULL, 0, NULL, 0 }
2571 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
2572 if (runlevel == '0' || runlevel == '6')
2573 arg_immediate = true;
2575 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
2583 arg_action = ACTION_HALT;
2587 arg_action = ACTION_POWEROFF;
2591 arg_action = ACTION_REBOOT;
2595 arg_immediate = true;
2616 /* Compatibility nops */
2623 log_error("Unknown option code %c", c);
2628 if (optind < argc) {
2629 log_error("Too many arguments.");
2636 static int shutdown_parse_argv(int argc, char *argv[]) {
2643 static const struct option options[] = {
2644 { "help", no_argument, NULL, ARG_HELP },
2645 { "halt", no_argument, NULL, 'H' },
2646 { "poweroff", no_argument, NULL, 'P' },
2647 { "reboot", no_argument, NULL, 'r' },
2648 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2649 { NULL, 0, NULL, 0 }
2657 while ((c = getopt_long(argc, argv, "HPrhkt:a", options, NULL)) >= 0) {
2665 arg_action = ACTION_HALT;
2669 arg_action = ACTION_POWEROFF;
2673 arg_action = ACTION_REBOOT;
2677 if (arg_action != ACTION_HALT)
2678 arg_action = ACTION_POWEROFF;
2691 /* Compatibility nops */
2698 log_error("Unknown option code %c", c);
2703 if (argc > optind && !streq(argv[optind], "now"))
2704 log_warning("First argument '%s' isn't 'now'. Ignoring.", argv[optind]);
2706 /* We ignore the time argument */
2707 if (argc > optind + 1)
2708 arg_wall = argv + optind + 1;
2715 static int telinit_parse_argv(int argc, char *argv[]) {
2722 static const struct option options[] = {
2723 { "help", no_argument, NULL, ARG_HELP },
2724 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2725 { NULL, 0, NULL, 0 }
2728 static const struct {
2732 { '0', ACTION_POWEROFF },
2733 { '6', ACTION_REBOOT },
2734 { '1', ACTION_RESCUE },
2735 { '2', ACTION_RUNLEVEL2 },
2736 { '3', ACTION_RUNLEVEL3 },
2737 { '4', ACTION_RUNLEVEL4 },
2738 { '5', ACTION_RUNLEVEL5 },
2739 { 's', ACTION_RESCUE },
2740 { 'S', ACTION_RESCUE },
2741 { 'q', ACTION_RELOAD },
2742 { 'Q', ACTION_RELOAD },
2743 { 'u', ACTION_REEXEC },
2744 { 'U', ACTION_REEXEC }
2753 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
2768 log_error("Unknown option code %c", c);
2773 if (optind >= argc) {
2778 if (optind + 1 < argc) {
2779 log_error("Too many arguments.");
2783 if (strlen(argv[optind]) != 1) {
2784 log_error("Expected single character argument.");
2788 for (i = 0; i < ELEMENTSOF(table); i++)
2789 if (table[i].from == argv[optind][0])
2792 if (i >= ELEMENTSOF(table)) {
2793 log_error("Unknown command %s.", argv[optind]);
2797 arg_action = table[i].to;
2804 static int runlevel_parse_argv(int argc, char *argv[]) {
2810 static const struct option options[] = {
2811 { "help", no_argument, NULL, ARG_HELP },
2812 { NULL, 0, NULL, 0 }
2820 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
2831 log_error("Unknown option code %c", c);
2836 if (optind < argc) {
2837 log_error("Too many arguments.");
2844 static int parse_argv(int argc, char *argv[]) {
2848 if (program_invocation_short_name) {
2850 if (strstr(program_invocation_short_name, "halt")) {
2851 arg_action = ACTION_HALT;
2852 return halt_parse_argv(argc, argv);
2853 } else if (strstr(program_invocation_short_name, "poweroff")) {
2854 arg_action = ACTION_POWEROFF;
2855 return halt_parse_argv(argc, argv);
2856 } else if (strstr(program_invocation_short_name, "reboot")) {
2857 arg_action = ACTION_REBOOT;
2858 return halt_parse_argv(argc, argv);
2859 } else if (strstr(program_invocation_short_name, "shutdown")) {
2860 arg_action = ACTION_POWEROFF;
2861 return shutdown_parse_argv(argc, argv);
2862 } else if (strstr(program_invocation_short_name, "init")) {
2863 arg_action = ACTION_INVALID;
2864 return telinit_parse_argv(argc, argv);
2865 } else if (strstr(program_invocation_short_name, "runlevel")) {
2866 arg_action = ACTION_RUNLEVEL;
2867 return runlevel_parse_argv(argc, argv);
2871 arg_action = ACTION_SYSTEMCTL;
2872 return systemctl_parse_argv(argc, argv);
2875 static int action_to_runlevel(void) {
2877 static const char table[_ACTION_MAX] = {
2878 [ACTION_HALT] = '0',
2879 [ACTION_POWEROFF] = '0',
2880 [ACTION_REBOOT] = '6',
2881 [ACTION_RUNLEVEL2] = '2',
2882 [ACTION_RUNLEVEL3] = '3',
2883 [ACTION_RUNLEVEL4] = '4',
2884 [ACTION_RUNLEVEL5] = '5',
2885 [ACTION_RESCUE] = '1'
2888 assert(arg_action < _ACTION_MAX);
2890 return table[arg_action];
2893 static int talk_upstart(void) {
2894 DBusMessage *m = NULL, *reply = NULL;
2896 int previous, rl, r;
2898 env1_buf[] = "RUNLEVEL=X",
2899 env2_buf[] = "PREVLEVEL=X";
2900 char *env1 = env1_buf, *env2 = env2_buf;
2901 const char *emit = "runlevel";
2902 dbus_bool_t b_false = FALSE;
2903 DBusMessageIter iter, sub;
2904 DBusConnection *bus;
2906 dbus_error_init(&error);
2908 if (!(rl = action_to_runlevel()))
2911 if (utmp_get_runlevel(&previous, NULL) < 0)
2914 if (!(bus = dbus_connection_open("unix:abstract=/com/ubuntu/upstart", &error))) {
2915 if (dbus_error_has_name(&error, DBUS_ERROR_NO_SERVER)) {
2920 log_error("Failed to connect to Upstart bus: %s", error.message);
2925 if ((r = bus_check_peercred(bus)) < 0) {
2926 log_error("Failed to verify owner of bus.");
2930 if (!(m = dbus_message_new_method_call(
2931 "com.ubuntu.Upstart",
2932 "/com/ubuntu/Upstart",
2933 "com.ubuntu.Upstart0_6",
2936 log_error("Could not allocate message.");
2941 dbus_message_iter_init_append(m, &iter);
2943 env1_buf[sizeof(env1_buf)-2] = rl;
2944 env2_buf[sizeof(env2_buf)-2] = previous;
2946 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
2947 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
2948 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
2949 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
2950 !dbus_message_iter_close_container(&iter, &sub) ||
2951 !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
2952 log_error("Could not append arguments to message.");
2957 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2959 if (error_is_no_service(&error)) {
2964 log_error("Failed to issue method call: %s", error.message);
2973 dbus_message_unref(m);
2976 dbus_message_unref(reply);
2979 dbus_connection_unref(bus);
2981 dbus_error_free(&error);
2986 static int talk_initctl(void) {
2987 struct init_request request;
2991 if (!(rl = action_to_runlevel()))
2995 request.magic = INIT_MAGIC;
2996 request.sleeptime = 0;
2997 request.cmd = INIT_CMD_RUNLVL;
2998 request.runlevel = rl;
3000 if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
3002 if (errno == ENOENT)
3005 log_error("Failed to open "INIT_FIFO": %m");
3010 r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
3011 close_nointr_nofail(fd);
3014 log_error("Failed to write to "INIT_FIFO": %m");
3015 return errno ? -errno : -EIO;
3021 static int systemctl_main(DBusConnection *bus, int argc, char *argv[]) {
3023 static const struct {
3031 int (* const dispatch)(DBusConnection *bus, char **args, unsigned n);
3033 { "list-units", LESS, 1, list_units },
3034 { "list-jobs", EQUAL, 1, list_jobs },
3035 { "clear-jobs", EQUAL, 1, clear_jobs },
3036 { "load", MORE, 2, load_unit },
3037 { "cancel", MORE, 2, cancel_job },
3038 { "start", MORE, 2, start_unit },
3039 { "stop", MORE, 2, start_unit },
3040 { "reload", MORE, 2, start_unit },
3041 { "restart", MORE, 2, start_unit },
3042 { "isolate", EQUAL, 2, start_unit },
3043 { "check", MORE, 2, check_unit },
3044 { "show", MORE, 1, show },
3045 { "status", MORE, 2, show },
3046 { "monitor", EQUAL, 1, monitor },
3047 { "dump", EQUAL, 1, dump },
3048 { "snapshot", LESS, 2, snapshot },
3049 { "delete", MORE, 2, delete_snapshot },
3050 { "daemon-reload", EQUAL, 1, clear_jobs },
3051 { "daemon-reexec", EQUAL, 1, clear_jobs },
3052 { "daemon-exit", EQUAL, 1, clear_jobs },
3053 { "show-environment", EQUAL, 1, show_enviroment },
3054 { "set-environment", MORE, 2, set_environment },
3055 { "unset-environment", MORE, 2, set_environment },
3056 { "halt", EQUAL, 1, start_special },
3057 { "poweroff", EQUAL, 1, start_special },
3058 { "reboot", EQUAL, 1, start_special },
3059 { "default", EQUAL, 1, start_special },
3060 { "rescue", EQUAL, 1, start_special },
3061 { "emergency", EQUAL, 1, start_special },
3071 left = argc - optind;
3074 /* Special rule: no arguments means "list-units" */
3077 if (streq(argv[optind], "help")) {
3082 for (i = 0; i < ELEMENTSOF(verbs); i++)
3083 if (streq(argv[optind], verbs[i].verb))
3086 if (i >= ELEMENTSOF(verbs)) {
3087 log_error("Unknown operation %s", argv[optind]);
3092 switch (verbs[i].argc_cmp) {
3095 if (left != verbs[i].argc) {
3096 log_error("Invalid number of arguments.");
3103 if (left < verbs[i].argc) {
3104 log_error("Too few arguments.");
3111 if (left > verbs[i].argc) {
3112 log_error("Too many arguments.");
3119 assert_not_reached("Unknown comparison operator.");
3122 return verbs[i].dispatch(bus, argv + optind, left);
3125 static int reload_with_fallback(DBusConnection *bus) {
3129 /* First, try systemd via D-Bus. */
3130 if ((r = clear_jobs(bus, NULL, 0)) > 0)
3134 /* Nothing else worked, so let's try signals */
3135 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
3137 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
3138 log_error("kill() failed: %m");
3145 static int start_with_fallback(DBusConnection *bus) {
3148 warn_wall(arg_action);
3151 /* First, try systemd via D-Bus. */
3152 if ((r = start_unit(bus, NULL, 0)) > 0)
3155 /* Hmm, talking to systemd via D-Bus didn't work. Then
3156 * let's try to talk to Upstart via D-Bus. */
3157 if ((r = talk_upstart()) > 0)
3161 /* Nothing else worked, so let's try
3163 if ((r = talk_initctl()) != 0)
3166 log_error("Failed to talk to init daemon.");
3170 static int halt_main(DBusConnection *bus) {
3174 return start_with_fallback(bus);
3177 if ((r = utmp_put_shutdown(0)) < 0)
3178 log_warning("Failed to write utmp record: %s", strerror(-r));
3186 /* Make sure C-A-D is handled by the kernel from this
3188 reboot(RB_ENABLE_CAD);
3190 switch (arg_action) {
3193 log_info("Halting");
3194 reboot(RB_HALT_SYSTEM);
3197 case ACTION_POWEROFF:
3198 log_info("Powering off");
3199 reboot(RB_POWER_OFF);
3203 log_info("Rebooting");
3204 reboot(RB_AUTOBOOT);
3208 assert_not_reached("Unknown halt action.");
3211 /* We should never reach this. */
3215 static int runlevel_main(void) {
3216 int r, runlevel, previous;
3218 if ((r = utmp_get_runlevel(&runlevel, &previous)) < 0) {
3224 previous <= 0 ? 'N' : previous,
3225 runlevel <= 0 ? 'N' : runlevel);
3230 int main(int argc, char*argv[]) {
3232 DBusConnection *bus = NULL;
3235 dbus_error_init(&error);
3237 log_parse_environment();
3239 if ((r = parse_argv(argc, argv)) < 0)
3246 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
3247 * let's shortcut this */
3248 if (arg_action == ACTION_RUNLEVEL) {
3249 retval = runlevel_main() < 0;
3253 bus_connect(arg_session ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &bus, &error);
3255 switch (arg_action) {
3257 case ACTION_SYSTEMCTL: {
3260 log_error("Failed to get D-Bus connection: %s", error.message);
3264 retval = systemctl_main(bus, argc, argv) < 0;
3269 case ACTION_POWEROFF:
3271 retval = halt_main(bus) < 0;
3274 case ACTION_RUNLEVEL2:
3275 case ACTION_RUNLEVEL3:
3276 case ACTION_RUNLEVEL4:
3277 case ACTION_RUNLEVEL5:
3279 case ACTION_EMERGENCY:
3280 case ACTION_DEFAULT:
3281 retval = start_with_fallback(bus) < 0;
3286 retval = reload_with_fallback(bus) < 0;
3289 case ACTION_INVALID:
3290 case ACTION_RUNLEVEL:
3292 assert_not_reached("Unknown action");
3298 dbus_connection_unref(bus);
3300 dbus_error_free(&error);