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)
948 r = read_one_line_file(fn, &pids);
959 p += strspn(p, WHITESPACE);
962 ul = strtoul(p, &p, 0);
963 if (errno != 0 || ul <= 0)
966 get_process_cmdline((pid_t) ul, 60, &t);
967 printf("\t\t%lu %s\n", ul, strna(t));
974 typedef struct UnitStatusInfo {
976 const char *load_state;
977 const char *active_state;
978 const char *sub_state;
980 const char *description;
982 const char *fragment_path;
983 const char *default_control_group;
988 const char *status_text;
991 usec_t start_timestamp;
992 usec_t exit_timestamp;
994 int exit_code, exit_status;
998 unsigned n_connections;
1001 const char *sysfs_path;
1003 /* Mount, Automount */
1010 static void print_status_info(UnitStatusInfo *i) {
1013 /* This shows pretty information about a unit. See
1014 * print_property() for a low-level property printer */
1016 printf("%s", strna(i->id));
1018 if (i->description && !streq_ptr(i->id, i->description))
1019 printf(" - %s", i->description);
1023 if (i->fragment_path)
1024 printf("\t Loaded: %s (%s)\n", strna(i->load_state), i->fragment_path);
1025 else if (streq_ptr(i->load_state, "failed"))
1026 printf("\t Loaded: " ANSI_HIGHLIGHT_ON "%s" ANSI_HIGHLIGHT_OFF "\n", strna(i->load_state));
1028 printf("\t Loaded: %s\n", strna(i->load_state));
1030 if (streq_ptr(i->active_state, "maintenance"))
1031 printf("\t Active: " ANSI_HIGHLIGHT_ON "%s (%s)" ANSI_HIGHLIGHT_OFF "\n",
1032 strna(i->active_state),
1033 strna(i->sub_state));
1035 printf("\t Active: %s (%s)\n",
1036 strna(i->active_state),
1037 strna(i->sub_state));
1040 printf("\t Device: %s\n", i->sysfs_path);
1042 printf("\t Where: %s\n", i->where);
1044 printf("\t What: %s\n", i->what);
1047 printf("\t Status: \"%s\"\n", i->status_text);
1049 if (i->id && endswith(i->id, ".socket"))
1050 printf("\tAccepted: %u; Connected: %u\n", i->n_accepted, i->n_connections);
1052 if (i->main_pid > 0 || i->control_pid > 0) {
1055 if (i->main_pid > 0) {
1056 printf(" Process: %u", (unsigned) i->main_pid);
1060 get_process_name(i->main_pid, &t);
1066 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
1068 if (i->exit_code == CLD_EXITED)
1069 printf("status=%i", i->exit_status);
1071 printf("signal=%s", strsignal(i->exit_status));
1076 if (i->main_pid > 0 && i->control_pid > 0)
1079 if (i->control_pid > 0) {
1082 printf(" Control: %u", (unsigned) i->control_pid);
1084 get_process_name(i->control_pid, &t);
1094 if (i->default_control_group) {
1095 printf("\t CGroup: %s\n", i->default_control_group);
1096 show_cgroup(i->default_control_group);
1100 static int status_property(const char *name, DBusMessageIter *iter, UnitStatusInfo *i) {
1102 switch (dbus_message_iter_get_arg_type(iter)) {
1104 case DBUS_TYPE_STRING: {
1107 dbus_message_iter_get_basic(iter, &s);
1110 if (streq(name, "Id"))
1112 else if (streq(name, "LoadState"))
1114 else if (streq(name, "ActiveState"))
1115 i->active_state = s;
1116 else if (streq(name, "SubState"))
1118 else if (streq(name, "Description"))
1120 else if (streq(name, "FragmentPath"))
1121 i->fragment_path = s;
1122 else if (streq(name, "DefaultControlGroup"))
1123 i->default_control_group = s;
1124 else if (streq(name, "StatusText"))
1126 else if (streq(name, "SysFSPath"))
1128 else if (streq(name, "Where"))
1130 else if (streq(name, "What"))
1137 case DBUS_TYPE_UINT32: {
1140 dbus_message_iter_get_basic(iter, &u);
1142 if (streq(name, "MainPID")) {
1144 i->main_pid = (pid_t) u;
1147 } else if (streq(name, "ControlPID"))
1148 i->control_pid = (pid_t) u;
1149 else if (streq(name, "ExecMainPID")) {
1151 i->main_pid = (pid_t) u;
1152 } else if (streq(name, "NAccepted"))
1154 else if (streq(name, "NConnections"))
1155 i->n_connections = u;
1160 case DBUS_TYPE_INT32: {
1163 dbus_message_iter_get_basic(iter, &j);
1165 if (streq(name, "ExecMainCode"))
1166 i->exit_code = (int) j;
1167 else if (streq(name, "ExecMainStatus"))
1168 i->exit_status = (int) j;
1173 case DBUS_TYPE_UINT64: {
1176 dbus_message_iter_get_basic(iter, &u);
1178 if (streq(name, "ExecMainStartTimestamp"))
1179 i->start_timestamp = (usec_t) u;
1180 else if (streq(name, "ExecMainExitTimestamp"))
1181 i->exit_timestamp = (usec_t) u;
1190 static int print_property(const char *name, DBusMessageIter *iter) {
1194 /* This is a low-level property printer, see
1195 * print_status_info() for the nicer output */
1197 if (arg_property && !streq(name, arg_property))
1200 switch (dbus_message_iter_get_arg_type(iter)) {
1202 case DBUS_TYPE_STRING: {
1204 dbus_message_iter_get_basic(iter, &s);
1206 if (arg_all || s[0])
1207 printf("%s=%s\n", name, s);
1212 case DBUS_TYPE_BOOLEAN: {
1214 dbus_message_iter_get_basic(iter, &b);
1215 printf("%s=%s\n", name, yes_no(b));
1220 case DBUS_TYPE_UINT64: {
1222 dbus_message_iter_get_basic(iter, &u);
1224 /* Yes, heuristics! But we can change this check
1225 * should it turn out to not be sufficient */
1227 if (strstr(name, "Timestamp")) {
1228 char timestamp[FORMAT_TIMESTAMP_MAX], *t;
1230 if ((t = format_timestamp(timestamp, sizeof(timestamp), u)) || arg_all)
1231 printf("%s=%s\n", name, strempty(t));
1232 } else if (strstr(name, "USec")) {
1233 char timespan[FORMAT_TIMESPAN_MAX];
1235 printf("%s=%s\n", name, format_timespan(timespan, sizeof(timespan), u));
1237 printf("%s=%llu\n", name, (unsigned long long) u);
1242 case DBUS_TYPE_UINT32: {
1244 dbus_message_iter_get_basic(iter, &u);
1246 if (strstr(name, "UMask") || strstr(name, "Mode"))
1247 printf("%s=%04o\n", name, u);
1249 printf("%s=%u\n", name, (unsigned) u);
1254 case DBUS_TYPE_INT32: {
1256 dbus_message_iter_get_basic(iter, &i);
1258 printf("%s=%i\n", name, (int) i);
1262 case DBUS_TYPE_STRUCT: {
1263 DBusMessageIter sub;
1264 dbus_message_iter_recurse(iter, &sub);
1266 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "Job")) {
1269 dbus_message_iter_get_basic(&sub, &u);
1272 printf("%s=%u\n", name, (unsigned) u);
1274 printf("%s=\n", name);
1277 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Unit")) {
1280 dbus_message_iter_get_basic(&sub, &s);
1282 if (arg_all || s[0])
1283 printf("%s=%s\n", name, s);
1291 case DBUS_TYPE_ARRAY:
1293 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
1294 DBusMessageIter sub;
1297 dbus_message_iter_recurse(iter, &sub);
1300 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1301 printf("%s=", name);
1303 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1306 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
1307 dbus_message_iter_get_basic(&sub, &s);
1308 printf("%s%s", space ? " " : "", s);
1311 dbus_message_iter_next(&sub);
1318 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_BYTE) {
1319 DBusMessageIter sub;
1321 dbus_message_iter_recurse(iter, &sub);
1324 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1325 printf("%s=", name);
1327 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1330 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_BYTE);
1331 dbus_message_iter_get_basic(&sub, &u);
1334 dbus_message_iter_next(&sub);
1341 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Paths")) {
1342 DBusMessageIter sub, sub2;
1344 dbus_message_iter_recurse(iter, &sub);
1346 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1347 const char *type, *path;
1349 dbus_message_iter_recurse(&sub, &sub2);
1351 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
1352 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0)
1353 printf("%s=%s\n", type, path);
1355 dbus_message_iter_next(&sub);
1359 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Timers")) {
1360 DBusMessageIter sub, sub2;
1362 dbus_message_iter_recurse(iter, &sub);
1364 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1366 uint64_t value, next_elapse;
1368 dbus_message_iter_recurse(&sub, &sub2);
1370 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &base, true) >= 0 &&
1371 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &value, true) >= 0 &&
1372 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &next_elapse, false) >= 0) {
1373 char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX];
1375 printf("%s={ value=%s ; next_elapse=%s }\n",
1377 format_timespan(timespan1, sizeof(timespan1), value),
1378 format_timespan(timespan2, sizeof(timespan2), next_elapse));
1381 dbus_message_iter_next(&sub);
1385 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && startswith(name, "Exec")) {
1387 DBusMessageIter sub, sub2, sub3;
1389 dbus_message_iter_recurse(iter, &sub);
1391 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1393 uint64_t start_time, exit_time;
1395 int32_t code, status;
1397 dbus_message_iter_recurse(&sub, &sub2);
1399 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0)
1402 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_ARRAY ||
1403 dbus_message_iter_get_element_type(&sub2) != DBUS_TYPE_STRING)
1406 printf("%s={ path=%s ; argv[]=", name, path);
1408 dbus_message_iter_recurse(&sub2, &sub3);
1410 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1413 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1414 dbus_message_iter_get_basic(&sub3, &s);
1416 dbus_message_iter_next(&sub3);
1419 if (dbus_message_iter_next(&sub2) &&
1420 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_time, true) >= 0 &&
1421 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_time, true) >= 0 &&
1422 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, true) >= 0 &&
1423 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &code, true) >= 0 &&
1424 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &status, false) >= 0) {
1426 char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
1428 printf("; start=%s ; stop=%s ; pid=%u ; code=%s ; status=%i/%s",
1429 strna(format_timestamp(timestamp1, sizeof(timestamp1), start_time)),
1430 strna(format_timestamp(timestamp2, sizeof(timestamp2), exit_time)),
1432 sigchld_code_to_string(code),
1434 strempty(code == CLD_EXITED ? NULL : strsignal(status)));
1439 dbus_message_iter_next(&sub);
1449 printf("%s=[unprintable]\n", name);
1454 static int show_one(DBusConnection *bus, const char *path, bool show_properties, bool *new_line) {
1455 DBusMessage *m = NULL, *reply = NULL;
1456 const char *interface = "";
1459 DBusMessageIter iter, sub, sub2, sub3;
1460 UnitStatusInfo info;
1467 dbus_error_init(&error);
1469 if (!(m = dbus_message_new_method_call(
1470 "org.freedesktop.systemd1",
1472 "org.freedesktop.DBus.Properties",
1474 log_error("Could not allocate message.");
1479 if (!dbus_message_append_args(m,
1480 DBUS_TYPE_STRING, &interface,
1481 DBUS_TYPE_INVALID)) {
1482 log_error("Could not append arguments to message.");
1487 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1488 log_error("Failed to issue method call: %s", error.message);
1493 if (!dbus_message_iter_init(reply, &iter) ||
1494 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
1495 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
1496 log_error("Failed to parse reply.");
1501 dbus_message_iter_recurse(&iter, &sub);
1508 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1511 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
1512 log_error("Failed to parse reply.");
1517 dbus_message_iter_recurse(&sub, &sub2);
1519 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
1520 log_error("Failed to parse reply.");
1525 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
1526 log_error("Failed to parse reply.");
1531 dbus_message_iter_recurse(&sub2, &sub3);
1533 if (show_properties)
1534 r = print_property(name, &sub3);
1536 r = status_property(name, &sub3, &info);
1539 log_error("Failed to parse reply.");
1544 dbus_message_iter_next(&sub);
1547 if (!show_properties)
1548 print_status_info(&info);
1554 dbus_message_unref(m);
1557 dbus_message_unref(reply);
1559 dbus_error_free(&error);
1564 static int show(DBusConnection *bus, char **args, unsigned n) {
1565 DBusMessage *m = NULL, *reply = NULL;
1569 bool show_properties, new_line = false;
1574 dbus_error_init(&error);
1576 show_properties = !streq(args[0], "status");
1578 if (show_properties && n <= 1) {
1579 /* If not argument is specified inspect the manager
1582 r = show_one(bus, "/org/freedesktop/systemd1", show_properties, &new_line);
1586 for (i = 1; i < n; i++) {
1587 const char *path = NULL;
1590 if (!show_properties || safe_atou32(args[i], &id) < 0) {
1592 if (!(m = dbus_message_new_method_call(
1593 "org.freedesktop.systemd1",
1594 "/org/freedesktop/systemd1",
1595 "org.freedesktop.systemd1.Manager",
1597 log_error("Could not allocate message.");
1602 if (!dbus_message_append_args(m,
1603 DBUS_TYPE_STRING, &args[i],
1604 DBUS_TYPE_INVALID)) {
1605 log_error("Could not append arguments to message.");
1612 if (!(m = dbus_message_new_method_call(
1613 "org.freedesktop.systemd1",
1614 "/org/freedesktop/systemd1",
1615 "org.freedesktop.systemd1.Manager",
1617 log_error("Could not allocate message.");
1622 if (!dbus_message_append_args(m,
1623 DBUS_TYPE_UINT32, &id,
1624 DBUS_TYPE_INVALID)) {
1625 log_error("Could not append arguments to message.");
1631 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1632 log_error("Failed to issue method call: %s", error.message);
1637 if (!dbus_message_get_args(reply, &error,
1638 DBUS_TYPE_OBJECT_PATH, &path,
1639 DBUS_TYPE_INVALID)) {
1640 log_error("Failed to parse reply: %s", error.message);
1645 if ((r = show_one(bus, path, show_properties, &new_line)) < 0)
1648 dbus_message_unref(m);
1649 dbus_message_unref(reply);
1657 dbus_message_unref(m);
1660 dbus_message_unref(reply);
1662 dbus_error_free(&error);
1667 static DBusHandlerResult monitor_filter(DBusConnection *connection, DBusMessage *message, void *data) {
1669 DBusMessage *m = NULL, *reply = NULL;
1674 dbus_error_init(&error);
1676 /* log_debug("Got D-Bus request: %s.%s() on %s", */
1677 /* dbus_message_get_interface(message), */
1678 /* dbus_message_get_member(message), */
1679 /* dbus_message_get_path(message)); */
1681 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
1682 log_error("Warning! D-Bus connection terminated.");
1683 dbus_connection_close(connection);
1685 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitNew") ||
1686 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitRemoved")) {
1687 const char *id, *path;
1689 if (!dbus_message_get_args(message, &error,
1690 DBUS_TYPE_STRING, &id,
1691 DBUS_TYPE_OBJECT_PATH, &path,
1693 log_error("Failed to parse message: %s", error.message);
1694 else if (streq(dbus_message_get_member(message), "UnitNew"))
1695 printf("Unit %s added.\n", id);
1697 printf("Unit %s removed.\n", id);
1699 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobNew") ||
1700 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
1704 if (!dbus_message_get_args(message, &error,
1705 DBUS_TYPE_UINT32, &id,
1706 DBUS_TYPE_OBJECT_PATH, &path,
1708 log_error("Failed to parse message: %s", error.message);
1709 else if (streq(dbus_message_get_member(message), "JobNew"))
1710 printf("Job %u added.\n", id);
1712 printf("Job %u removed.\n", id);
1715 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Unit", "Changed") ||
1716 dbus_message_is_signal(message, "org.freedesktop.systemd1.Job", "Changed")) {
1718 const char *path, *interface, *property = "Id";
1719 DBusMessageIter iter, sub;
1721 path = dbus_message_get_path(message);
1722 interface = dbus_message_get_interface(message);
1724 if (!(m = dbus_message_new_method_call(
1725 "org.freedesktop.systemd1",
1727 "org.freedesktop.DBus.Properties",
1729 log_error("Could not allocate message.");
1733 if (!dbus_message_append_args(m,
1734 DBUS_TYPE_STRING, &interface,
1735 DBUS_TYPE_STRING, &property,
1736 DBUS_TYPE_INVALID)) {
1737 log_error("Could not append arguments to message.");
1741 if (!(reply = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) {
1742 log_error("Failed to issue method call: %s", error.message);
1746 if (!dbus_message_iter_init(reply, &iter) ||
1747 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1748 log_error("Failed to parse reply.");
1752 dbus_message_iter_recurse(&iter, &sub);
1754 if (streq(interface, "org.freedesktop.systemd1.Unit")) {
1757 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1758 log_error("Failed to parse reply.");
1762 dbus_message_iter_get_basic(&sub, &id);
1763 printf("Unit %s changed.\n", id);
1767 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT32) {
1768 log_error("Failed to parse reply.");
1772 dbus_message_iter_get_basic(&sub, &id);
1773 printf("Job %u changed.\n", id);
1779 dbus_message_unref(m);
1782 dbus_message_unref(reply);
1784 dbus_error_free(&error);
1785 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1789 dbus_message_unref(m);
1792 dbus_message_unref(reply);
1794 dbus_error_free(&error);
1795 return DBUS_HANDLER_RESULT_NEED_MEMORY;
1798 static int monitor(DBusConnection *bus, char **args, unsigned n) {
1799 DBusMessage *m = NULL, *reply = NULL;
1803 dbus_error_init(&error);
1805 dbus_bus_add_match(bus,
1807 "sender='org.freedesktop.systemd1',"
1808 "interface='org.freedesktop.systemd1.Manager',"
1809 "path='/org/freedesktop/systemd1'",
1812 if (dbus_error_is_set(&error)) {
1813 log_error("Failed to add match: %s", error.message);
1818 dbus_bus_add_match(bus,
1820 "sender='org.freedesktop.systemd1',"
1821 "interface='org.freedesktop.systemd1.Unit',"
1825 if (dbus_error_is_set(&error)) {
1826 log_error("Failed to add match: %s", error.message);
1831 dbus_bus_add_match(bus,
1833 "sender='org.freedesktop.systemd1',"
1834 "interface='org.freedesktop.systemd1.Job',"
1838 if (dbus_error_is_set(&error)) {
1839 log_error("Failed to add match: %s", error.message);
1844 if (!dbus_connection_add_filter(bus, monitor_filter, NULL, NULL)) {
1845 log_error("Failed to add filter.");
1850 if (!(m = dbus_message_new_method_call(
1851 "org.freedesktop.systemd1",
1852 "/org/freedesktop/systemd1",
1853 "org.freedesktop.systemd1.Manager",
1855 log_error("Could not allocate message.");
1860 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1861 log_error("Failed to issue method call: %s", error.message);
1866 while (dbus_connection_read_write_dispatch(bus, -1))
1873 /* This is slightly dirty, since we don't undo the filter or the matches. */
1876 dbus_message_unref(m);
1879 dbus_message_unref(reply);
1881 dbus_error_free(&error);
1886 static int dump(DBusConnection *bus, char **args, unsigned n) {
1887 DBusMessage *m = NULL, *reply = NULL;
1892 dbus_error_init(&error);
1894 if (!(m = dbus_message_new_method_call(
1895 "org.freedesktop.systemd1",
1896 "/org/freedesktop/systemd1",
1897 "org.freedesktop.systemd1.Manager",
1899 log_error("Could not allocate message.");
1903 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1904 log_error("Failed to issue method call: %s", error.message);
1909 if (!dbus_message_get_args(reply, &error,
1910 DBUS_TYPE_STRING, &text,
1911 DBUS_TYPE_INVALID)) {
1912 log_error("Failed to parse reply: %s", error.message);
1917 fputs(text, stdout);
1923 dbus_message_unref(m);
1926 dbus_message_unref(reply);
1928 dbus_error_free(&error);
1933 static int snapshot(DBusConnection *bus, char **args, unsigned n) {
1934 DBusMessage *m = NULL, *reply = NULL;
1937 const char *name = "", *path, *id;
1938 dbus_bool_t cleanup = FALSE;
1939 DBusMessageIter iter, sub;
1941 *interface = "org.freedesktop.systemd1.Unit",
1944 dbus_error_init(&error);
1946 if (!(m = dbus_message_new_method_call(
1947 "org.freedesktop.systemd1",
1948 "/org/freedesktop/systemd1",
1949 "org.freedesktop.systemd1.Manager",
1950 "CreateSnapshot"))) {
1951 log_error("Could not allocate message.");
1958 if (!dbus_message_append_args(m,
1959 DBUS_TYPE_STRING, &name,
1960 DBUS_TYPE_BOOLEAN, &cleanup,
1961 DBUS_TYPE_INVALID)) {
1962 log_error("Could not append arguments to message.");
1967 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1968 log_error("Failed to issue method call: %s", error.message);
1973 if (!dbus_message_get_args(reply, &error,
1974 DBUS_TYPE_OBJECT_PATH, &path,
1975 DBUS_TYPE_INVALID)) {
1976 log_error("Failed to parse reply: %s", error.message);
1981 dbus_message_unref(m);
1982 if (!(m = dbus_message_new_method_call(
1983 "org.freedesktop.systemd1",
1985 "org.freedesktop.DBus.Properties",
1987 log_error("Could not allocate message.");
1991 if (!dbus_message_append_args(m,
1992 DBUS_TYPE_STRING, &interface,
1993 DBUS_TYPE_STRING, &property,
1994 DBUS_TYPE_INVALID)) {
1995 log_error("Could not append arguments to message.");
2000 dbus_message_unref(reply);
2001 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2002 log_error("Failed to issue method call: %s", error.message);
2007 if (!dbus_message_iter_init(reply, &iter) ||
2008 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2009 log_error("Failed to parse reply.");
2014 dbus_message_iter_recurse(&iter, &sub);
2016 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2017 log_error("Failed to parse reply.");
2022 dbus_message_iter_get_basic(&sub, &id);
2030 dbus_message_unref(m);
2033 dbus_message_unref(reply);
2035 dbus_error_free(&error);
2040 static int delete_snapshot(DBusConnection *bus, char **args, unsigned n) {
2041 DBusMessage *m = NULL, *reply = NULL;
2049 dbus_error_init(&error);
2051 for (i = 1; i < n; i++) {
2052 const char *path = NULL;
2054 if (!(m = dbus_message_new_method_call(
2055 "org.freedesktop.systemd1",
2056 "/org/freedesktop/systemd1",
2057 "org.freedesktop.systemd1.Manager",
2059 log_error("Could not allocate message.");
2064 if (!dbus_message_append_args(m,
2065 DBUS_TYPE_STRING, &args[i],
2066 DBUS_TYPE_INVALID)) {
2067 log_error("Could not append arguments to message.");
2072 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2073 log_error("Failed to issue method call: %s", error.message);
2078 if (!dbus_message_get_args(reply, &error,
2079 DBUS_TYPE_OBJECT_PATH, &path,
2080 DBUS_TYPE_INVALID)) {
2081 log_error("Failed to parse reply: %s", error.message);
2086 dbus_message_unref(m);
2087 if (!(m = dbus_message_new_method_call(
2088 "org.freedesktop.systemd1",
2090 "org.freedesktop.systemd1.Snapshot",
2092 log_error("Could not allocate message.");
2097 dbus_message_unref(reply);
2098 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2099 log_error("Failed to issue method call: %s", error.message);
2104 dbus_message_unref(m);
2105 dbus_message_unref(reply);
2113 dbus_message_unref(m);
2116 dbus_message_unref(reply);
2118 dbus_error_free(&error);
2123 static int clear_jobs(DBusConnection *bus, char **args, unsigned n) {
2124 DBusMessage *m = NULL, *reply = NULL;
2129 dbus_error_init(&error);
2131 if (arg_action == ACTION_RELOAD)
2133 else if (arg_action == ACTION_REEXEC)
2134 method = "Reexecute";
2136 assert(arg_action == ACTION_SYSTEMCTL);
2139 streq(args[0], "clear-jobs") ? "ClearJobs" :
2140 streq(args[0], "daemon-reload") ? "Reload" :
2141 streq(args[0], "daemon-reexec") ? "Reexecute" :
2145 if (!(m = dbus_message_new_method_call(
2146 "org.freedesktop.systemd1",
2147 "/org/freedesktop/systemd1",
2148 "org.freedesktop.systemd1.Manager",
2150 log_error("Could not allocate message.");
2154 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2156 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
2157 /* There's always a fallback possible for
2158 * legacy actions. */
2163 log_error("Failed to issue method call: %s", error.message);
2172 dbus_message_unref(m);
2175 dbus_message_unref(reply);
2177 dbus_error_free(&error);
2182 static int show_enviroment(DBusConnection *bus, char **args, unsigned n) {
2183 DBusMessage *m = NULL, *reply = NULL;
2185 DBusMessageIter iter, sub, sub2;
2188 *interface = "org.freedesktop.systemd1.Manager",
2189 *property = "Environment";
2191 dbus_error_init(&error);
2193 if (!(m = dbus_message_new_method_call(
2194 "org.freedesktop.systemd1",
2195 "/org/freedesktop/systemd1",
2196 "org.freedesktop.DBus.Properties",
2198 log_error("Could not allocate message.");
2202 if (!dbus_message_append_args(m,
2203 DBUS_TYPE_STRING, &interface,
2204 DBUS_TYPE_STRING, &property,
2205 DBUS_TYPE_INVALID)) {
2206 log_error("Could not append arguments to message.");
2211 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2212 log_error("Failed to issue method call: %s", error.message);
2217 if (!dbus_message_iter_init(reply, &iter) ||
2218 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2219 log_error("Failed to parse reply.");
2224 dbus_message_iter_recurse(&iter, &sub);
2226 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
2227 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
2228 log_error("Failed to parse reply.");
2233 dbus_message_iter_recurse(&sub, &sub2);
2235 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
2238 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
2239 log_error("Failed to parse reply.");
2244 dbus_message_iter_get_basic(&sub2, &text);
2245 printf("%s\n", text);
2247 dbus_message_iter_next(&sub2);
2254 dbus_message_unref(m);
2257 dbus_message_unref(reply);
2259 dbus_error_free(&error);
2264 static int set_environment(DBusConnection *bus, char **args, unsigned n) {
2265 DBusMessage *m = NULL, *reply = NULL;
2269 DBusMessageIter iter, sub;
2272 dbus_error_init(&error);
2274 method = streq(args[0], "set-environment")
2276 : "UnsetEnvironment";
2278 if (!(m = dbus_message_new_method_call(
2279 "org.freedesktop.systemd1",
2280 "/org/freedesktop/systemd1",
2281 "org.freedesktop.systemd1.Manager",
2284 log_error("Could not allocate message.");
2288 dbus_message_iter_init_append(m, &iter);
2290 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
2291 log_error("Could not append arguments to message.");
2296 for (i = 1; i < n; i++)
2297 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &args[i])) {
2298 log_error("Could not append arguments to message.");
2303 if (!dbus_message_iter_close_container(&iter, &sub)) {
2304 log_error("Could not append arguments to message.");
2309 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2310 log_error("Failed to issue method call: %s", error.message);
2319 dbus_message_unref(m);
2322 dbus_message_unref(reply);
2324 dbus_error_free(&error);
2329 static int systemctl_help(void) {
2331 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
2332 "Send control commands to the systemd manager.\n\n"
2333 " -h --help Show this help\n"
2334 " -t --type=TYPE List only units of a particular type\n"
2335 " -p --property=NAME Show only properties by this name\n"
2336 " -a --all Show all units/properties, including dead/empty ones\n"
2337 " --replace When installing a new job, replace existing conflicting ones\n"
2338 " --system Connect to system bus\n"
2339 " --session Connect to session bus\n"
2340 " -q --quiet Suppress output\n"
2341 " --no-block Do not wait until operation finished\n"
2342 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
2344 " list-units List units\n"
2345 " start [NAME...] Start one or more units\n"
2346 " stop [NAME...] Stop one or more units\n"
2347 " restart [NAME...] Restart one or more units\n"
2348 " reload [NAME...] Reload one or more units\n"
2349 " isolate [NAME] Start one unit and stop all others\n"
2350 " check [NAME...] Check whether any of the passed units are active\n"
2351 " status [NAME...] Show status of one or more units\n"
2352 " show [NAME...|JOB...] Show properties of one or more units/jobs/manager\n"
2353 " load [NAME...] Load one or more units\n"
2354 " list-jobs List jobs\n"
2355 " cancel [JOB...] Cancel one or more jobs\n"
2356 " clear-jobs Cancel all jobs\n"
2357 " monitor Monitor unit/job changes\n"
2358 " dump Dump server status\n"
2359 " snapshot [NAME] Create a snapshot\n"
2360 " delete [NAME...] Remove one or more snapshots\n"
2361 " daemon-reload Reload systemd manager configuration\n"
2362 " daemon-reexec Reexecute systemd manager\n"
2363 " daemon-exit Ask the systemd manager to quit\n"
2364 " show-environment Dump environment\n"
2365 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
2366 " unset-environment [NAME...] Unset one or more environment variables\n"
2367 " halt Shut down and halt the system\n"
2368 " poweroff Shut down and power-off the system\n"
2369 " reboot Shut down and reboot the system\n"
2370 " default Enter default mode\n"
2371 " rescue Enter rescue mode\n"
2372 " emergency Enter emergency mode\n",
2373 program_invocation_short_name);
2378 static int halt_help(void) {
2380 printf("%s [OPTIONS...]\n\n"
2381 "%s the system.\n\n"
2382 " --help Show this help\n"
2383 " --halt Halt the machine\n"
2384 " -p --poweroff Switch off the machine\n"
2385 " --reboot Reboot the machine\n"
2386 " -f --force Force immediate halt/power-off/reboot\n"
2387 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
2388 " -d --no-wtmp Don't write wtmp record\n"
2389 " -n --no-sync Don't sync before halt/power-off/reboot\n"
2390 " --no-wall Don't send wall message before halt/power-off/reboot\n",
2391 program_invocation_short_name,
2392 arg_action == ACTION_REBOOT ? "Reboot" :
2393 arg_action == ACTION_POWEROFF ? "Power off" :
2399 static int shutdown_help(void) {
2401 printf("%s [OPTIONS...] [now] [WALL...]\n\n"
2402 "Shut down the system.\n\n"
2403 " --help Show this help\n"
2404 " -H --halt Halt the machine\n"
2405 " -P --poweroff Power-off the machine\n"
2406 " -r --reboot Reboot the machine\n"
2407 " -h Equivalent to --poweroff, overriden by --halt\n"
2408 " -k Don't halt/power-off/reboot, just send warnings\n"
2409 " --no-wall Don't send wall message before halt/power-off/reboot\n",
2410 program_invocation_short_name);
2415 static int telinit_help(void) {
2417 printf("%s [OPTIONS...] {COMMAND}\n\n"
2418 "Send control commands to the init daemon.\n\n"
2419 " --help Show this help\n"
2420 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
2422 " 0 Power-off the machine\n"
2423 " 6 Reboot the machine\n"
2424 " 2, 3, 4, 5 Start runlevelX.target unit\n"
2425 " 1, s, S Enter rescue mode\n"
2426 " q, Q Reload init daemon configuration\n"
2427 " u, U Reexecute init daemon\n",
2428 program_invocation_short_name);
2433 static int runlevel_help(void) {
2435 printf("%s [OPTIONS...]\n\n"
2436 "Prints the previous and current runlevel of the init system.\n\n"
2437 " --help Show this help\n",
2438 program_invocation_short_name);
2443 static int systemctl_parse_argv(int argc, char *argv[]) {
2446 ARG_REPLACE = 0x100,
2453 static const struct option options[] = {
2454 { "help", no_argument, NULL, 'h' },
2455 { "type", required_argument, NULL, 't' },
2456 { "property", required_argument, NULL, 'p' },
2457 { "all", no_argument, NULL, 'a' },
2458 { "replace", no_argument, NULL, ARG_REPLACE },
2459 { "session", no_argument, NULL, ARG_SESSION },
2460 { "system", no_argument, NULL, ARG_SYSTEM },
2461 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
2462 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2463 { "quiet", no_argument, NULL, 'q' },
2464 { NULL, 0, NULL, 0 }
2472 while ((c = getopt_long(argc, argv, "ht:p:aq", options, NULL)) >= 0) {
2485 arg_property = optarg;
2487 /* If the user asked for a particular
2488 * property, show it to him, even if it is
2506 arg_session = false;
2510 arg_no_block = true;
2525 log_error("Unknown option code %c", c);
2533 static int halt_parse_argv(int argc, char *argv[]) {
2542 static const struct option options[] = {
2543 { "help", no_argument, NULL, ARG_HELP },
2544 { "halt", no_argument, NULL, ARG_HALT },
2545 { "poweroff", no_argument, NULL, 'p' },
2546 { "reboot", no_argument, NULL, ARG_REBOOT },
2547 { "force", no_argument, NULL, 'f' },
2548 { "wtmp-only", no_argument, NULL, 'w' },
2549 { "no-wtmp", no_argument, NULL, 'd' },
2550 { "no-sync", no_argument, NULL, 'n' },
2551 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2552 { NULL, 0, NULL, 0 }
2560 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
2561 if (runlevel == '0' || runlevel == '6')
2562 arg_immediate = true;
2564 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
2572 arg_action = ACTION_HALT;
2576 arg_action = ACTION_POWEROFF;
2580 arg_action = ACTION_REBOOT;
2584 arg_immediate = true;
2605 /* Compatibility nops */
2612 log_error("Unknown option code %c", c);
2617 if (optind < argc) {
2618 log_error("Too many arguments.");
2625 static int shutdown_parse_argv(int argc, char *argv[]) {
2632 static const struct option options[] = {
2633 { "help", no_argument, NULL, ARG_HELP },
2634 { "halt", no_argument, NULL, 'H' },
2635 { "poweroff", no_argument, NULL, 'P' },
2636 { "reboot", no_argument, NULL, 'r' },
2637 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2638 { NULL, 0, NULL, 0 }
2646 while ((c = getopt_long(argc, argv, "HPrhkt:a", options, NULL)) >= 0) {
2654 arg_action = ACTION_HALT;
2658 arg_action = ACTION_POWEROFF;
2662 arg_action = ACTION_REBOOT;
2666 if (arg_action != ACTION_HALT)
2667 arg_action = ACTION_POWEROFF;
2680 /* Compatibility nops */
2687 log_error("Unknown option code %c", c);
2692 if (argc > optind && !streq(argv[optind], "now"))
2693 log_warning("First argument '%s' isn't 'now'. Ignoring.", argv[optind]);
2695 /* We ignore the time argument */
2696 if (argc > optind + 1)
2697 arg_wall = argv + optind + 1;
2704 static int telinit_parse_argv(int argc, char *argv[]) {
2711 static const struct option options[] = {
2712 { "help", no_argument, NULL, ARG_HELP },
2713 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2714 { NULL, 0, NULL, 0 }
2717 static const struct {
2721 { '0', ACTION_POWEROFF },
2722 { '6', ACTION_REBOOT },
2723 { '1', ACTION_RESCUE },
2724 { '2', ACTION_RUNLEVEL2 },
2725 { '3', ACTION_RUNLEVEL3 },
2726 { '4', ACTION_RUNLEVEL4 },
2727 { '5', ACTION_RUNLEVEL5 },
2728 { 's', ACTION_RESCUE },
2729 { 'S', ACTION_RESCUE },
2730 { 'q', ACTION_RELOAD },
2731 { 'Q', ACTION_RELOAD },
2732 { 'u', ACTION_REEXEC },
2733 { 'U', ACTION_REEXEC }
2742 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
2757 log_error("Unknown option code %c", c);
2762 if (optind >= argc) {
2767 if (optind + 1 < argc) {
2768 log_error("Too many arguments.");
2772 if (strlen(argv[optind]) != 1) {
2773 log_error("Expected single character argument.");
2777 for (i = 0; i < ELEMENTSOF(table); i++)
2778 if (table[i].from == argv[optind][0])
2781 if (i >= ELEMENTSOF(table)) {
2782 log_error("Unknown command %s.", argv[optind]);
2786 arg_action = table[i].to;
2793 static int runlevel_parse_argv(int argc, char *argv[]) {
2799 static const struct option options[] = {
2800 { "help", no_argument, NULL, ARG_HELP },
2801 { NULL, 0, NULL, 0 }
2809 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
2820 log_error("Unknown option code %c", c);
2825 if (optind < argc) {
2826 log_error("Too many arguments.");
2833 static int parse_argv(int argc, char *argv[]) {
2837 if (program_invocation_short_name) {
2839 if (strstr(program_invocation_short_name, "halt")) {
2840 arg_action = ACTION_HALT;
2841 return halt_parse_argv(argc, argv);
2842 } else if (strstr(program_invocation_short_name, "poweroff")) {
2843 arg_action = ACTION_POWEROFF;
2844 return halt_parse_argv(argc, argv);
2845 } else if (strstr(program_invocation_short_name, "reboot")) {
2846 arg_action = ACTION_REBOOT;
2847 return halt_parse_argv(argc, argv);
2848 } else if (strstr(program_invocation_short_name, "shutdown")) {
2849 arg_action = ACTION_POWEROFF;
2850 return shutdown_parse_argv(argc, argv);
2851 } else if (strstr(program_invocation_short_name, "init")) {
2852 arg_action = ACTION_INVALID;
2853 return telinit_parse_argv(argc, argv);
2854 } else if (strstr(program_invocation_short_name, "runlevel")) {
2855 arg_action = ACTION_RUNLEVEL;
2856 return runlevel_parse_argv(argc, argv);
2860 arg_action = ACTION_SYSTEMCTL;
2861 return systemctl_parse_argv(argc, argv);
2864 static int action_to_runlevel(void) {
2866 static const char table[_ACTION_MAX] = {
2867 [ACTION_HALT] = '0',
2868 [ACTION_POWEROFF] = '0',
2869 [ACTION_REBOOT] = '6',
2870 [ACTION_RUNLEVEL2] = '2',
2871 [ACTION_RUNLEVEL3] = '3',
2872 [ACTION_RUNLEVEL4] = '4',
2873 [ACTION_RUNLEVEL5] = '5',
2874 [ACTION_RESCUE] = '1'
2877 assert(arg_action < _ACTION_MAX);
2879 return table[arg_action];
2882 static int talk_upstart(void) {
2883 DBusMessage *m = NULL, *reply = NULL;
2885 int previous, rl, r;
2887 env1_buf[] = "RUNLEVEL=X",
2888 env2_buf[] = "PREVLEVEL=X";
2889 char *env1 = env1_buf, *env2 = env2_buf;
2890 const char *emit = "runlevel";
2891 dbus_bool_t b_false = FALSE;
2892 DBusMessageIter iter, sub;
2893 DBusConnection *bus;
2895 dbus_error_init(&error);
2897 if (!(rl = action_to_runlevel()))
2900 if (utmp_get_runlevel(&previous, NULL) < 0)
2903 if (!(bus = dbus_connection_open("unix:abstract=/com/ubuntu/upstart", &error))) {
2904 if (dbus_error_has_name(&error, DBUS_ERROR_NO_SERVER)) {
2909 log_error("Failed to connect to Upstart bus: %s", error.message);
2914 if ((r = bus_check_peercred(bus)) < 0) {
2915 log_error("Failed to verify owner of bus.");
2919 if (!(m = dbus_message_new_method_call(
2920 "com.ubuntu.Upstart",
2921 "/com/ubuntu/Upstart",
2922 "com.ubuntu.Upstart0_6",
2925 log_error("Could not allocate message.");
2930 dbus_message_iter_init_append(m, &iter);
2932 env1_buf[sizeof(env1_buf)-2] = rl;
2933 env2_buf[sizeof(env2_buf)-2] = previous;
2935 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
2936 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
2937 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
2938 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
2939 !dbus_message_iter_close_container(&iter, &sub) ||
2940 !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
2941 log_error("Could not append arguments to message.");
2946 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2948 if (error_is_no_service(&error)) {
2953 log_error("Failed to issue method call: %s", error.message);
2962 dbus_message_unref(m);
2965 dbus_message_unref(reply);
2968 dbus_connection_unref(bus);
2970 dbus_error_free(&error);
2975 static int talk_initctl(void) {
2976 struct init_request request;
2980 if (!(rl = action_to_runlevel()))
2984 request.magic = INIT_MAGIC;
2985 request.sleeptime = 0;
2986 request.cmd = INIT_CMD_RUNLVL;
2987 request.runlevel = rl;
2989 if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
2991 if (errno == ENOENT)
2994 log_error("Failed to open "INIT_FIFO": %m");
2999 r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
3000 close_nointr_nofail(fd);
3003 log_error("Failed to write to "INIT_FIFO": %m");
3004 return errno ? -errno : -EIO;
3010 static int systemctl_main(DBusConnection *bus, int argc, char *argv[]) {
3012 static const struct {
3020 int (* const dispatch)(DBusConnection *bus, char **args, unsigned n);
3022 { "list-units", LESS, 1, list_units },
3023 { "list-jobs", EQUAL, 1, list_jobs },
3024 { "clear-jobs", EQUAL, 1, clear_jobs },
3025 { "load", MORE, 2, load_unit },
3026 { "cancel", MORE, 2, cancel_job },
3027 { "start", MORE, 2, start_unit },
3028 { "stop", MORE, 2, start_unit },
3029 { "reload", MORE, 2, start_unit },
3030 { "restart", MORE, 2, start_unit },
3031 { "isolate", EQUAL, 2, start_unit },
3032 { "check", MORE, 2, check_unit },
3033 { "show", MORE, 1, show },
3034 { "status", MORE, 2, show },
3035 { "monitor", EQUAL, 1, monitor },
3036 { "dump", EQUAL, 1, dump },
3037 { "snapshot", LESS, 2, snapshot },
3038 { "delete", MORE, 2, delete_snapshot },
3039 { "daemon-reload", EQUAL, 1, clear_jobs },
3040 { "daemon-reexec", EQUAL, 1, clear_jobs },
3041 { "daemon-exit", EQUAL, 1, clear_jobs },
3042 { "show-environment", EQUAL, 1, show_enviroment },
3043 { "set-environment", MORE, 2, set_environment },
3044 { "unset-environment", MORE, 2, set_environment },
3045 { "halt", EQUAL, 1, start_special },
3046 { "poweroff", EQUAL, 1, start_special },
3047 { "reboot", EQUAL, 1, start_special },
3048 { "default", EQUAL, 1, start_special },
3049 { "rescue", EQUAL, 1, start_special },
3050 { "emergency", EQUAL, 1, start_special },
3060 left = argc - optind;
3063 /* Special rule: no arguments means "list-units" */
3066 if (streq(argv[optind], "help")) {
3071 for (i = 0; i < ELEMENTSOF(verbs); i++)
3072 if (streq(argv[optind], verbs[i].verb))
3075 if (i >= ELEMENTSOF(verbs)) {
3076 log_error("Unknown operation %s", argv[optind]);
3081 switch (verbs[i].argc_cmp) {
3084 if (left != verbs[i].argc) {
3085 log_error("Invalid number of arguments.");
3092 if (left < verbs[i].argc) {
3093 log_error("Too few arguments.");
3100 if (left > verbs[i].argc) {
3101 log_error("Too many arguments.");
3108 assert_not_reached("Unknown comparison operator.");
3111 return verbs[i].dispatch(bus, argv + optind, left);
3114 static int reload_with_fallback(DBusConnection *bus) {
3118 /* First, try systemd via D-Bus. */
3119 if ((r = clear_jobs(bus, NULL, 0)) > 0)
3123 /* Nothing else worked, so let's try signals */
3124 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
3126 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
3127 log_error("kill() failed: %m");
3134 static int start_with_fallback(DBusConnection *bus) {
3137 warn_wall(arg_action);
3140 /* First, try systemd via D-Bus. */
3141 if ((r = start_unit(bus, NULL, 0)) > 0)
3144 /* Hmm, talking to systemd via D-Bus didn't work. Then
3145 * let's try to talk to Upstart via D-Bus. */
3146 if ((r = talk_upstart()) > 0)
3150 /* Nothing else worked, so let's try
3152 if ((r = talk_initctl()) != 0)
3155 log_error("Failed to talk to init daemon.");
3159 static int halt_main(DBusConnection *bus) {
3163 return start_with_fallback(bus);
3166 if ((r = utmp_put_shutdown(0)) < 0)
3167 log_warning("Failed to write utmp record: %s", strerror(-r));
3175 /* Make sure C-A-D is handled by the kernel from this
3177 reboot(RB_ENABLE_CAD);
3179 switch (arg_action) {
3182 log_info("Halting");
3183 reboot(RB_HALT_SYSTEM);
3186 case ACTION_POWEROFF:
3187 log_info("Powering off");
3188 reboot(RB_POWER_OFF);
3192 log_info("Rebooting");
3193 reboot(RB_AUTOBOOT);
3197 assert_not_reached("Unknown halt action.");
3200 /* We should never reach this. */
3204 static int runlevel_main(void) {
3205 int r, runlevel, previous;
3207 if ((r = utmp_get_runlevel(&runlevel, &previous)) < 0) {
3213 previous <= 0 ? 'N' : previous,
3214 runlevel <= 0 ? 'N' : runlevel);
3219 int main(int argc, char*argv[]) {
3221 DBusConnection *bus = NULL;
3224 dbus_error_init(&error);
3226 log_parse_environment();
3228 if ((r = parse_argv(argc, argv)) < 0)
3235 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
3236 * let's shortcut this */
3237 if (arg_action == ACTION_RUNLEVEL) {
3238 retval = runlevel_main() < 0;
3242 bus_connect(arg_session ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &bus, &error);
3244 switch (arg_action) {
3246 case ACTION_SYSTEMCTL: {
3249 log_error("Failed to get D-Bus connection: %s", error.message);
3253 retval = systemctl_main(bus, argc, argv) < 0;
3258 case ACTION_POWEROFF:
3260 retval = halt_main(bus) < 0;
3263 case ACTION_RUNLEVEL2:
3264 case ACTION_RUNLEVEL3:
3265 case ACTION_RUNLEVEL4:
3266 case ACTION_RUNLEVEL5:
3268 case ACTION_EMERGENCY:
3269 case ACTION_DEFAULT:
3270 retval = start_with_fallback(bus) < 0;
3275 retval = reload_with_fallback(bus) < 0;
3278 case ACTION_INVALID:
3279 case ACTION_RUNLEVEL:
3281 assert_not_reached("Unknown action");
3287 dbus_connection_unref(bus);
3289 dbus_error_free(&error);