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"
45 #include "cgroup-show.h"
47 static const char *arg_type = NULL;
48 static const char *arg_property = NULL;
49 static bool arg_all = false;
50 static bool arg_replace = false;
51 static bool arg_session = false;
52 static bool arg_no_block = false;
53 static bool arg_immediate = false;
54 static bool arg_no_wtmp = false;
55 static bool arg_no_sync = false;
56 static bool arg_no_wall = false;
57 static bool arg_dry = false;
58 static bool arg_quiet = false;
59 static char **arg_wall = NULL;
77 } arg_action = ACTION_SYSTEMCTL;
79 static bool private_bus = false;
81 static bool error_is_no_service(DBusError *error) {
85 if (!dbus_error_is_set(error))
88 if (dbus_error_has_name(error, DBUS_ERROR_NAME_HAS_NO_OWNER))
91 if (dbus_error_has_name(error, DBUS_ERROR_SERVICE_UNKNOWN))
94 return startswith(error->name, "org.freedesktop.DBus.Error.Spawn.");
97 static int bus_iter_get_basic_and_next(DBusMessageIter *iter, int type, void *data, bool next) {
102 if (dbus_message_iter_get_arg_type(iter) != type)
105 dbus_message_iter_get_basic(iter, data);
107 if (!dbus_message_iter_next(iter) != !next)
113 static void warn_wall(enum action action) {
114 static const char *table[_ACTION_MAX] = {
115 [ACTION_HALT] = "The system is going down for system halt NOW!",
116 [ACTION_REBOOT] = "The system is going down for reboot NOW!",
117 [ACTION_POWEROFF] = "The system is going down for power-off NOW!",
118 [ACTION_RESCUE] = "The system is going down to rescue mode NOW!",
119 [ACTION_EMERGENCY] = "The system is going down to emergency mode NOW!"
128 if (!(p = strv_join(arg_wall, " "))) {
129 log_error("Failed to join strings.");
145 utmp_wall(table[action]);
148 static int list_units(DBusConnection *bus, char **args, unsigned n) {
149 DBusMessage *m = NULL, *reply = NULL;
152 DBusMessageIter iter, sub, sub2;
155 dbus_error_init(&error);
159 if (!(m = dbus_message_new_method_call(
160 "org.freedesktop.systemd1",
161 "/org/freedesktop/systemd1",
162 "org.freedesktop.systemd1.Manager",
164 log_error("Could not allocate message.");
168 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
169 log_error("Failed to issue method call: %s", error.message);
174 if (!dbus_message_iter_init(reply, &iter) ||
175 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
176 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
177 log_error("Failed to parse reply.");
182 dbus_message_iter_recurse(&iter, &sub);
184 printf("%-45s %-6s %-12s %-12s %-15s %s\n", "UNIT", "LOAD", "ACTIVE", "SUB", "JOB", "DESCRIPTION");
186 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
187 const char *id, *description, *load_state, *active_state, *sub_state, *unit_state, *job_type, *job_path, *dot;
190 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
191 log_error("Failed to parse reply.");
196 dbus_message_iter_recurse(&sub, &sub2);
198 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
199 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &description, true) < 0 ||
200 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &load_state, true) < 0 ||
201 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &active_state, true) < 0 ||
202 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &sub_state, true) < 0 ||
203 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_state, true) < 0 ||
204 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &job_id, true) < 0 ||
205 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &job_type, true) < 0 ||
206 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, false) < 0) {
207 log_error("Failed to parse reply.");
212 if ((!arg_type || ((dot = strrchr(id, '.')) &&
213 streq(dot+1, arg_type))) &&
214 (arg_all || !streq(active_state, "inactive"))) {
218 if (streq(active_state, "maintenance"))
219 fputs(ANSI_HIGHLIGHT_ON, stdout);
221 printf("%-45s %-6s %-12s %-12s%n", id, load_state, active_state, sub_state, &a);
224 printf(" %-15s%n", job_type, &b);
228 if (a + b + 2 < columns()) {
232 printf(" %.*s", columns() - a - b - 2, description);
235 if (streq(active_state, "maintenance"))
236 fputs(ANSI_HIGHLIGHT_OFF, stdout);
242 dbus_message_iter_next(&sub);
246 printf("\n%u units listed.\n", k);
248 printf("\n%u live units listed. Pass --all to see dead units, too.\n", k);
254 dbus_message_unref(m);
257 dbus_message_unref(reply);
259 dbus_error_free(&error);
264 static int list_jobs(DBusConnection *bus, char **args, unsigned n) {
265 DBusMessage *m = NULL, *reply = NULL;
268 DBusMessageIter iter, sub, sub2;
271 dbus_error_init(&error);
275 if (!(m = dbus_message_new_method_call(
276 "org.freedesktop.systemd1",
277 "/org/freedesktop/systemd1",
278 "org.freedesktop.systemd1.Manager",
280 log_error("Could not allocate message.");
284 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
285 log_error("Failed to issue method call: %s", error.message);
290 if (!dbus_message_iter_init(reply, &iter) ||
291 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
292 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
293 log_error("Failed to parse reply.");
298 dbus_message_iter_recurse(&iter, &sub);
300 printf("%4s %-45s %-17s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
302 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
303 const char *name, *type, *state, *job_path, *unit_path;
306 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
307 log_error("Failed to parse reply.");
312 dbus_message_iter_recurse(&sub, &sub2);
314 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &id, true) < 0 ||
315 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0 ||
316 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
317 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, true) < 0 ||
318 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, true) < 0 ||
319 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, false) < 0) {
320 log_error("Failed to parse reply.");
325 printf("%4u %-45s %-17s %-7s\n", id, name, type, state);
328 dbus_message_iter_next(&sub);
331 printf("\n%u jobs listed.\n", k);
336 dbus_message_unref(m);
339 dbus_message_unref(reply);
341 dbus_error_free(&error);
346 static int load_unit(DBusConnection *bus, char **args, unsigned n) {
347 DBusMessage *m = NULL, *reply = NULL;
352 dbus_error_init(&error);
357 for (i = 1; i < n; i++) {
359 if (!(m = dbus_message_new_method_call(
360 "org.freedesktop.systemd1",
361 "/org/freedesktop/systemd1",
362 "org.freedesktop.systemd1.Manager",
364 log_error("Could not allocate message.");
369 if (!dbus_message_append_args(m,
370 DBUS_TYPE_STRING, &args[i],
371 DBUS_TYPE_INVALID)) {
372 log_error("Could not append arguments to message.");
377 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
378 log_error("Failed to issue method call: %s", error.message);
383 dbus_message_unref(m);
384 dbus_message_unref(reply);
393 dbus_message_unref(m);
396 dbus_message_unref(reply);
398 dbus_error_free(&error);
403 static int cancel_job(DBusConnection *bus, char **args, unsigned n) {
404 DBusMessage *m = NULL, *reply = NULL;
409 dbus_error_init(&error);
414 for (i = 1; i < n; i++) {
418 if (!(m = dbus_message_new_method_call(
419 "org.freedesktop.systemd1",
420 "/org/freedesktop/systemd1",
421 "org.freedesktop.systemd1.Manager",
423 log_error("Could not allocate message.");
428 if ((r = safe_atou(args[i], &id)) < 0) {
429 log_error("Failed to parse job id: %s", strerror(-r));
433 assert_cc(sizeof(uint32_t) == sizeof(id));
434 if (!dbus_message_append_args(m,
435 DBUS_TYPE_UINT32, &id,
436 DBUS_TYPE_INVALID)) {
437 log_error("Could not append arguments to message.");
442 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
443 log_error("Failed to issue method call: %s", error.message);
448 if (!dbus_message_get_args(reply, &error,
449 DBUS_TYPE_OBJECT_PATH, &path,
450 DBUS_TYPE_INVALID)) {
451 log_error("Failed to parse reply: %s", error.message);
456 dbus_message_unref(m);
457 if (!(m = dbus_message_new_method_call(
458 "org.freedesktop.systemd1",
460 "org.freedesktop.systemd1.Job",
462 log_error("Could not allocate message.");
467 dbus_message_unref(reply);
468 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
469 log_error("Failed to issue method call: %s", error.message);
474 dbus_message_unref(m);
475 dbus_message_unref(reply);
483 dbus_message_unref(m);
486 dbus_message_unref(reply);
488 dbus_error_free(&error);
493 typedef struct WaitData {
498 static DBusHandlerResult wait_filter(DBusConnection *connection, DBusMessage *message, void *data) {
506 dbus_error_init(&error);
508 log_debug("Got D-Bus request: %s.%s() on %s",
509 dbus_message_get_interface(message),
510 dbus_message_get_member(message),
511 dbus_message_get_path(message));
513 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
514 log_error("Warning! D-Bus connection terminated.");
515 dbus_connection_close(connection);
517 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
520 dbus_bool_t success = true;
522 if (!dbus_message_get_args(message, &error,
523 DBUS_TYPE_UINT32, &id,
524 DBUS_TYPE_OBJECT_PATH, &path,
525 DBUS_TYPE_BOOLEAN, &success,
527 log_error("Failed to parse message: %s", error.message);
531 if ((p = set_remove(d->set, (char*) path)))
539 dbus_error_free(&error);
540 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
543 static int enable_wait_for_jobs(DBusConnection *bus) {
551 dbus_error_init(&error);
552 dbus_bus_add_match(bus,
554 "sender='org.freedesktop.systemd1',"
555 "interface='org.freedesktop.systemd1.Manager',"
556 "member='JobRemoved',"
557 "path='/org/freedesktop/systemd1'",
560 if (dbus_error_is_set(&error)) {
561 log_error("Failed to add match: %s", error.message);
562 dbus_error_free(&error);
566 /* This is slightly dirty, since we don't undo the match registrations. */
570 static int wait_for_jobs(DBusConnection *bus, Set *s) {
581 if (!dbus_connection_add_filter(bus, wait_filter, &d, NULL)) {
582 log_error("Failed to add filter.");
587 while (!set_isempty(s) &&
588 dbus_connection_read_write_dispatch(bus, -1))
591 if (!arg_quiet && d.failed)
592 log_error("Job failed, see logs for details.");
594 r = d.failed ? -EIO : 0;
597 /* This is slightly dirty, since we don't undo the filter registration. */
602 static int start_unit_one(
609 DBusMessage *m = NULL, *reply = NULL;
617 assert(arg_no_block || s);
619 dbus_error_init(&error);
621 if (!(m = dbus_message_new_method_call(
622 "org.freedesktop.systemd1",
623 "/org/freedesktop/systemd1",
624 "org.freedesktop.systemd1.Manager",
626 log_error("Could not allocate message.");
631 if (!dbus_message_append_args(m,
632 DBUS_TYPE_STRING, &name,
633 DBUS_TYPE_STRING, &mode,
634 DBUS_TYPE_INVALID)) {
635 log_error("Could not append arguments to message.");
640 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
642 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
643 /* There's always a fallback possible for
649 log_error("Failed to issue method call: %s", error.message);
658 if (!dbus_message_get_args(reply, &error,
659 DBUS_TYPE_OBJECT_PATH, &path,
660 DBUS_TYPE_INVALID)) {
661 log_error("Failed to parse reply: %s", error.message);
666 if (!(p = strdup(path))) {
667 log_error("Failed to duplicate path.");
672 if ((r = set_put(s, p)) < 0) {
674 log_error("Failed to add path to set.");
683 dbus_message_unref(m);
686 dbus_message_unref(reply);
688 dbus_error_free(&error);
693 static enum action verb_to_action(const char *verb) {
694 if (streq(verb, "halt"))
696 else if (streq(verb, "poweroff"))
697 return ACTION_POWEROFF;
698 else if (streq(verb, "reboot"))
699 return ACTION_REBOOT;
700 else if (streq(verb, "rescue"))
701 return ACTION_RESCUE;
702 else if (streq(verb, "emergency"))
703 return ACTION_EMERGENCY;
704 else if (streq(verb, "default"))
705 return ACTION_DEFAULT;
707 return ACTION_INVALID;
710 static int start_unit(DBusConnection *bus, char **args, unsigned n) {
712 static const char * const table[_ACTION_MAX] = {
713 [ACTION_HALT] = SPECIAL_HALT_TARGET,
714 [ACTION_POWEROFF] = SPECIAL_POWEROFF_TARGET,
715 [ACTION_REBOOT] = SPECIAL_REBOOT_TARGET,
716 [ACTION_RUNLEVEL2] = SPECIAL_RUNLEVEL2_TARGET,
717 [ACTION_RUNLEVEL3] = SPECIAL_RUNLEVEL3_TARGET,
718 [ACTION_RUNLEVEL4] = SPECIAL_RUNLEVEL4_TARGET,
719 [ACTION_RUNLEVEL5] = SPECIAL_RUNLEVEL5_TARGET,
720 [ACTION_RESCUE] = SPECIAL_RESCUE_TARGET,
721 [ACTION_EMERGENCY] = SPECIAL_EMERGENCY_SERVICE,
722 [ACTION_DEFAULT] = SPECIAL_DEFAULT_TARGET
727 const char *method, *mode, *one_name;
732 if (arg_action == ACTION_SYSTEMCTL) {
734 streq(args[0], "stop") ? "StopUnit" :
735 streq(args[0], "reload") ? "ReloadUnit" :
736 streq(args[0], "restart") ? "RestartUnit" :
740 (streq(args[0], "isolate") ||
741 streq(args[0], "rescue") ||
742 streq(args[0], "emergency")) ? "isolate" :
743 arg_replace ? "replace" :
746 one_name = table[verb_to_action(args[0])];
749 assert(arg_action < ELEMENTSOF(table));
750 assert(table[arg_action]);
752 method = "StartUnit";
754 mode = (arg_action == ACTION_EMERGENCY ||
755 arg_action == ACTION_RESCUE) ? "isolate" : "replace";
757 one_name = table[arg_action];
761 if ((r = enable_wait_for_jobs(bus)) < 0) {
762 log_error("Could not watch jobs: %s", strerror(-r));
766 if (!(s = set_new(string_hash_func, string_compare_func))) {
767 log_error("Failed to allocate set.");
776 if ((r = start_unit_one(bus, method, one_name, mode, s)) <= 0)
779 for (i = 1; i < n; i++)
780 if ((r = start_unit_one(bus, method, args[i], mode, s)) < 0)
785 r = wait_for_jobs(bus, s);
794 static int start_special(DBusConnection *bus, char **args, unsigned n) {
798 warn_wall(verb_to_action(args[0]));
800 return start_unit(bus, args, n);
803 static int check_unit(DBusConnection *bus, char **args, unsigned n) {
804 DBusMessage *m = NULL, *reply = NULL;
806 *interface = "org.freedesktop.systemd1.Unit",
807 *property = "ActiveState";
808 int r = -EADDRNOTAVAIL;
815 dbus_error_init(&error);
817 for (i = 1; i < n; i++) {
818 const char *path = NULL;
820 DBusMessageIter iter, sub;
822 if (!(m = dbus_message_new_method_call(
823 "org.freedesktop.systemd1",
824 "/org/freedesktop/systemd1",
825 "org.freedesktop.systemd1.Manager",
827 log_error("Could not allocate message.");
832 if (!dbus_message_append_args(m,
833 DBUS_TYPE_STRING, &args[i],
834 DBUS_TYPE_INVALID)) {
835 log_error("Could not append arguments to message.");
840 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
842 /* Hmm, cannot figure out anything about this unit... */
846 dbus_error_free(&error);
850 if (!dbus_message_get_args(reply, &error,
851 DBUS_TYPE_OBJECT_PATH, &path,
852 DBUS_TYPE_INVALID)) {
853 log_error("Failed to parse reply: %s", error.message);
858 dbus_message_unref(m);
859 if (!(m = dbus_message_new_method_call(
860 "org.freedesktop.systemd1",
862 "org.freedesktop.DBus.Properties",
864 log_error("Could not allocate message.");
869 if (!dbus_message_append_args(m,
870 DBUS_TYPE_STRING, &interface,
871 DBUS_TYPE_STRING, &property,
872 DBUS_TYPE_INVALID)) {
873 log_error("Could not append arguments to message.");
878 dbus_message_unref(reply);
879 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
880 log_error("Failed to issue method call: %s", error.message);
885 if (!dbus_message_iter_init(reply, &iter) ||
886 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
887 log_error("Failed to parse reply.");
892 dbus_message_iter_recurse(&iter, &sub);
894 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
895 log_error("Failed to parse reply.");
900 dbus_message_iter_get_basic(&sub, &state);
905 if (streq(state, "active") || startswith(state, "reloading"))
908 dbus_message_unref(m);
909 dbus_message_unref(reply);
915 dbus_message_unref(m);
918 dbus_message_unref(reply);
920 dbus_error_free(&error);
925 typedef struct UnitStatusInfo {
927 const char *load_state;
928 const char *active_state;
929 const char *sub_state;
931 const char *description;
933 const char *fragment_path;
934 const char *default_control_group;
939 const char *status_text;
942 usec_t start_timestamp;
943 usec_t exit_timestamp;
945 int exit_code, exit_status;
949 unsigned n_connections;
952 const char *sysfs_path;
954 /* Mount, Automount */
961 static void print_status_info(UnitStatusInfo *i) {
964 /* This shows pretty information about a unit. See
965 * print_property() for a low-level property printer */
967 printf("%s", strna(i->id));
969 if (i->description && !streq_ptr(i->id, i->description))
970 printf(" - %s", i->description);
974 if (i->fragment_path)
975 printf("\t Loaded: %s (%s)\n", strna(i->load_state), i->fragment_path);
976 else if (streq_ptr(i->load_state, "failed"))
977 printf("\t Loaded: " ANSI_HIGHLIGHT_ON "%s" ANSI_HIGHLIGHT_OFF "\n", strna(i->load_state));
979 printf("\t Loaded: %s\n", strna(i->load_state));
981 if (streq_ptr(i->active_state, "maintenance"))
982 printf("\t Active: " ANSI_HIGHLIGHT_ON "%s (%s)" ANSI_HIGHLIGHT_OFF "\n",
983 strna(i->active_state),
984 strna(i->sub_state));
986 printf("\t Active: %s (%s)\n",
987 strna(i->active_state),
988 strna(i->sub_state));
991 printf("\t Device: %s\n", i->sysfs_path);
993 printf("\t Where: %s\n", i->where);
995 printf("\t What: %s\n", i->what);
998 printf("\t Status: \"%s\"\n", i->status_text);
1000 if (i->id && endswith(i->id, ".socket"))
1001 printf("\tAccepted: %u; Connected: %u\n", i->n_accepted, i->n_connections);
1003 if (i->main_pid > 0 || i->control_pid > 0) {
1006 if (i->main_pid > 0) {
1007 printf(" Process: %u", (unsigned) i->main_pid);
1011 get_process_name(i->main_pid, &t);
1017 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
1019 if (i->exit_code == CLD_EXITED)
1020 printf("status=%i", i->exit_status);
1022 printf("signal=%s", strsignal(i->exit_status));
1027 if (i->main_pid > 0 && i->control_pid > 0)
1030 if (i->control_pid > 0) {
1033 printf(" Control: %u", (unsigned) i->control_pid);
1035 get_process_name(i->control_pid, &t);
1045 if (i->default_control_group) {
1048 printf("\t CGroup: %s\n", i->default_control_group);
1050 if ((c = columns()) > 18)
1055 show_cgroup_recursive(i->default_control_group, "\t\t ", c);
1059 static int status_property(const char *name, DBusMessageIter *iter, UnitStatusInfo *i) {
1061 switch (dbus_message_iter_get_arg_type(iter)) {
1063 case DBUS_TYPE_STRING: {
1066 dbus_message_iter_get_basic(iter, &s);
1069 if (streq(name, "Id"))
1071 else if (streq(name, "LoadState"))
1073 else if (streq(name, "ActiveState"))
1074 i->active_state = s;
1075 else if (streq(name, "SubState"))
1077 else if (streq(name, "Description"))
1079 else if (streq(name, "FragmentPath"))
1080 i->fragment_path = s;
1081 else if (streq(name, "DefaultControlGroup"))
1082 i->default_control_group = s;
1083 else if (streq(name, "StatusText"))
1085 else if (streq(name, "SysFSPath"))
1087 else if (streq(name, "Where"))
1089 else if (streq(name, "What"))
1096 case DBUS_TYPE_UINT32: {
1099 dbus_message_iter_get_basic(iter, &u);
1101 if (streq(name, "MainPID")) {
1103 i->main_pid = (pid_t) u;
1106 } else if (streq(name, "ControlPID"))
1107 i->control_pid = (pid_t) u;
1108 else if (streq(name, "ExecMainPID")) {
1110 i->main_pid = (pid_t) u;
1111 } else if (streq(name, "NAccepted"))
1113 else if (streq(name, "NConnections"))
1114 i->n_connections = u;
1119 case DBUS_TYPE_INT32: {
1122 dbus_message_iter_get_basic(iter, &j);
1124 if (streq(name, "ExecMainCode"))
1125 i->exit_code = (int) j;
1126 else if (streq(name, "ExecMainStatus"))
1127 i->exit_status = (int) j;
1132 case DBUS_TYPE_UINT64: {
1135 dbus_message_iter_get_basic(iter, &u);
1137 if (streq(name, "ExecMainStartTimestamp"))
1138 i->start_timestamp = (usec_t) u;
1139 else if (streq(name, "ExecMainExitTimestamp"))
1140 i->exit_timestamp = (usec_t) u;
1149 static int print_property(const char *name, DBusMessageIter *iter) {
1153 /* This is a low-level property printer, see
1154 * print_status_info() for the nicer output */
1156 if (arg_property && !streq(name, arg_property))
1159 switch (dbus_message_iter_get_arg_type(iter)) {
1161 case DBUS_TYPE_STRING: {
1163 dbus_message_iter_get_basic(iter, &s);
1165 if (arg_all || s[0])
1166 printf("%s=%s\n", name, s);
1171 case DBUS_TYPE_BOOLEAN: {
1173 dbus_message_iter_get_basic(iter, &b);
1174 printf("%s=%s\n", name, yes_no(b));
1179 case DBUS_TYPE_UINT64: {
1181 dbus_message_iter_get_basic(iter, &u);
1183 /* Yes, heuristics! But we can change this check
1184 * should it turn out to not be sufficient */
1186 if (strstr(name, "Timestamp")) {
1187 char timestamp[FORMAT_TIMESTAMP_MAX], *t;
1189 if ((t = format_timestamp(timestamp, sizeof(timestamp), u)) || arg_all)
1190 printf("%s=%s\n", name, strempty(t));
1191 } else if (strstr(name, "USec")) {
1192 char timespan[FORMAT_TIMESPAN_MAX];
1194 printf("%s=%s\n", name, format_timespan(timespan, sizeof(timespan), u));
1196 printf("%s=%llu\n", name, (unsigned long long) u);
1201 case DBUS_TYPE_UINT32: {
1203 dbus_message_iter_get_basic(iter, &u);
1205 if (strstr(name, "UMask") || strstr(name, "Mode"))
1206 printf("%s=%04o\n", name, u);
1208 printf("%s=%u\n", name, (unsigned) u);
1213 case DBUS_TYPE_INT32: {
1215 dbus_message_iter_get_basic(iter, &i);
1217 printf("%s=%i\n", name, (int) i);
1221 case DBUS_TYPE_STRUCT: {
1222 DBusMessageIter sub;
1223 dbus_message_iter_recurse(iter, &sub);
1225 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "Job")) {
1228 dbus_message_iter_get_basic(&sub, &u);
1231 printf("%s=%u\n", name, (unsigned) u);
1233 printf("%s=\n", name);
1236 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Unit")) {
1239 dbus_message_iter_get_basic(&sub, &s);
1241 if (arg_all || s[0])
1242 printf("%s=%s\n", name, s);
1250 case DBUS_TYPE_ARRAY:
1252 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
1253 DBusMessageIter sub;
1256 dbus_message_iter_recurse(iter, &sub);
1259 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1260 printf("%s=", name);
1262 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1265 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
1266 dbus_message_iter_get_basic(&sub, &s);
1267 printf("%s%s", space ? " " : "", s);
1270 dbus_message_iter_next(&sub);
1277 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_BYTE) {
1278 DBusMessageIter sub;
1280 dbus_message_iter_recurse(iter, &sub);
1283 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1284 printf("%s=", name);
1286 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1289 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_BYTE);
1290 dbus_message_iter_get_basic(&sub, &u);
1293 dbus_message_iter_next(&sub);
1300 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Paths")) {
1301 DBusMessageIter sub, sub2;
1303 dbus_message_iter_recurse(iter, &sub);
1305 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1306 const char *type, *path;
1308 dbus_message_iter_recurse(&sub, &sub2);
1310 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
1311 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0)
1312 printf("%s=%s\n", type, path);
1314 dbus_message_iter_next(&sub);
1318 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Timers")) {
1319 DBusMessageIter sub, sub2;
1321 dbus_message_iter_recurse(iter, &sub);
1323 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1325 uint64_t value, next_elapse;
1327 dbus_message_iter_recurse(&sub, &sub2);
1329 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &base, true) >= 0 &&
1330 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &value, true) >= 0 &&
1331 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &next_elapse, false) >= 0) {
1332 char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX];
1334 printf("%s={ value=%s ; next_elapse=%s }\n",
1336 format_timespan(timespan1, sizeof(timespan1), value),
1337 format_timespan(timespan2, sizeof(timespan2), next_elapse));
1340 dbus_message_iter_next(&sub);
1344 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && startswith(name, "Exec")) {
1346 DBusMessageIter sub, sub2, sub3;
1348 dbus_message_iter_recurse(iter, &sub);
1350 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1352 uint64_t start_time, exit_time;
1354 int32_t code, status;
1356 dbus_message_iter_recurse(&sub, &sub2);
1358 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0)
1361 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_ARRAY ||
1362 dbus_message_iter_get_element_type(&sub2) != DBUS_TYPE_STRING)
1365 printf("%s={ path=%s ; argv[]=", name, path);
1367 dbus_message_iter_recurse(&sub2, &sub3);
1369 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1372 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1373 dbus_message_iter_get_basic(&sub3, &s);
1375 dbus_message_iter_next(&sub3);
1378 if (dbus_message_iter_next(&sub2) &&
1379 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_time, true) >= 0 &&
1380 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_time, true) >= 0 &&
1381 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, true) >= 0 &&
1382 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &code, true) >= 0 &&
1383 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &status, false) >= 0) {
1385 char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
1387 printf("; start=%s ; stop=%s ; pid=%u ; code=%s ; status=%i/%s",
1388 strna(format_timestamp(timestamp1, sizeof(timestamp1), start_time)),
1389 strna(format_timestamp(timestamp2, sizeof(timestamp2), exit_time)),
1391 sigchld_code_to_string(code),
1393 strempty(code == CLD_EXITED ? NULL : strsignal(status)));
1398 dbus_message_iter_next(&sub);
1408 printf("%s=[unprintable]\n", name);
1413 static int show_one(DBusConnection *bus, const char *path, bool show_properties, bool *new_line) {
1414 DBusMessage *m = NULL, *reply = NULL;
1415 const char *interface = "";
1418 DBusMessageIter iter, sub, sub2, sub3;
1419 UnitStatusInfo info;
1426 dbus_error_init(&error);
1428 if (!(m = dbus_message_new_method_call(
1429 "org.freedesktop.systemd1",
1431 "org.freedesktop.DBus.Properties",
1433 log_error("Could not allocate message.");
1438 if (!dbus_message_append_args(m,
1439 DBUS_TYPE_STRING, &interface,
1440 DBUS_TYPE_INVALID)) {
1441 log_error("Could not append arguments to message.");
1446 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1447 log_error("Failed to issue method call: %s", error.message);
1452 if (!dbus_message_iter_init(reply, &iter) ||
1453 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
1454 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
1455 log_error("Failed to parse reply.");
1460 dbus_message_iter_recurse(&iter, &sub);
1467 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1470 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
1471 log_error("Failed to parse reply.");
1476 dbus_message_iter_recurse(&sub, &sub2);
1478 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
1479 log_error("Failed to parse reply.");
1484 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
1485 log_error("Failed to parse reply.");
1490 dbus_message_iter_recurse(&sub2, &sub3);
1492 if (show_properties)
1493 r = print_property(name, &sub3);
1495 r = status_property(name, &sub3, &info);
1498 log_error("Failed to parse reply.");
1503 dbus_message_iter_next(&sub);
1506 if (!show_properties)
1507 print_status_info(&info);
1513 dbus_message_unref(m);
1516 dbus_message_unref(reply);
1518 dbus_error_free(&error);
1523 static int show(DBusConnection *bus, char **args, unsigned n) {
1524 DBusMessage *m = NULL, *reply = NULL;
1528 bool show_properties, new_line = false;
1533 dbus_error_init(&error);
1535 show_properties = !streq(args[0], "status");
1537 if (show_properties && n <= 1) {
1538 /* If not argument is specified inspect the manager
1541 r = show_one(bus, "/org/freedesktop/systemd1", show_properties, &new_line);
1545 for (i = 1; i < n; i++) {
1546 const char *path = NULL;
1549 if (!show_properties || safe_atou32(args[i], &id) < 0) {
1551 if (!(m = dbus_message_new_method_call(
1552 "org.freedesktop.systemd1",
1553 "/org/freedesktop/systemd1",
1554 "org.freedesktop.systemd1.Manager",
1556 log_error("Could not allocate message.");
1561 if (!dbus_message_append_args(m,
1562 DBUS_TYPE_STRING, &args[i],
1563 DBUS_TYPE_INVALID)) {
1564 log_error("Could not append arguments to message.");
1569 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1571 if (!dbus_error_has_name(&error, DBUS_ERROR_ACCESS_DENIED)) {
1572 log_error("Failed to issue method call: %s", error.message);
1577 dbus_error_free(&error);
1579 dbus_message_unref(m);
1580 if (!(m = dbus_message_new_method_call(
1581 "org.freedesktop.systemd1",
1582 "/org/freedesktop/systemd1",
1583 "org.freedesktop.systemd1.Manager",
1585 log_error("Could not allocate message.");
1590 if (!dbus_message_append_args(m,
1591 DBUS_TYPE_STRING, &args[i],
1592 DBUS_TYPE_INVALID)) {
1593 log_error("Could not append arguments to message.");
1598 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1599 log_error("Failed to issue method call: %s", error.message);
1607 if (!(m = dbus_message_new_method_call(
1608 "org.freedesktop.systemd1",
1609 "/org/freedesktop/systemd1",
1610 "org.freedesktop.systemd1.Manager",
1612 log_error("Could not allocate message.");
1617 if (!dbus_message_append_args(m,
1618 DBUS_TYPE_UINT32, &id,
1619 DBUS_TYPE_INVALID)) {
1620 log_error("Could not append arguments to message.");
1625 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1626 log_error("Failed to issue method call: %s", error.message);
1632 if (!dbus_message_get_args(reply, &error,
1633 DBUS_TYPE_OBJECT_PATH, &path,
1634 DBUS_TYPE_INVALID)) {
1635 log_error("Failed to parse reply: %s", error.message);
1640 if ((r = show_one(bus, path, show_properties, &new_line)) < 0)
1643 dbus_message_unref(m);
1644 dbus_message_unref(reply);
1652 dbus_message_unref(m);
1655 dbus_message_unref(reply);
1657 dbus_error_free(&error);
1662 static DBusHandlerResult monitor_filter(DBusConnection *connection, DBusMessage *message, void *data) {
1664 DBusMessage *m = NULL, *reply = NULL;
1669 dbus_error_init(&error);
1671 log_debug("Got D-Bus request: %s.%s() on %s",
1672 dbus_message_get_interface(message),
1673 dbus_message_get_member(message),
1674 dbus_message_get_path(message));
1676 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
1677 log_error("Warning! D-Bus connection terminated.");
1678 dbus_connection_close(connection);
1680 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitNew") ||
1681 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitRemoved")) {
1682 const char *id, *path;
1684 if (!dbus_message_get_args(message, &error,
1685 DBUS_TYPE_STRING, &id,
1686 DBUS_TYPE_OBJECT_PATH, &path,
1688 log_error("Failed to parse message: %s", error.message);
1689 else if (streq(dbus_message_get_member(message), "UnitNew"))
1690 printf("Unit %s added.\n", id);
1692 printf("Unit %s removed.\n", id);
1694 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobNew") ||
1695 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
1699 if (!dbus_message_get_args(message, &error,
1700 DBUS_TYPE_UINT32, &id,
1701 DBUS_TYPE_OBJECT_PATH, &path,
1703 log_error("Failed to parse message: %s", error.message);
1704 else if (streq(dbus_message_get_member(message), "JobNew"))
1705 printf("Job %u added.\n", id);
1707 printf("Job %u removed.\n", id);
1710 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Unit", "Changed") ||
1711 dbus_message_is_signal(message, "org.freedesktop.systemd1.Job", "Changed")) {
1713 const char *path, *interface, *property = "Id";
1714 DBusMessageIter iter, sub;
1716 path = dbus_message_get_path(message);
1717 interface = dbus_message_get_interface(message);
1719 if (!(m = dbus_message_new_method_call(
1720 "org.freedesktop.systemd1",
1722 "org.freedesktop.DBus.Properties",
1724 log_error("Could not allocate message.");
1728 if (!dbus_message_append_args(m,
1729 DBUS_TYPE_STRING, &interface,
1730 DBUS_TYPE_STRING, &property,
1731 DBUS_TYPE_INVALID)) {
1732 log_error("Could not append arguments to message.");
1736 if (!(reply = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) {
1737 log_error("Failed to issue method call: %s", error.message);
1741 if (!dbus_message_iter_init(reply, &iter) ||
1742 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1743 log_error("Failed to parse reply.");
1747 dbus_message_iter_recurse(&iter, &sub);
1749 if (streq(interface, "org.freedesktop.systemd1.Unit")) {
1752 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1753 log_error("Failed to parse reply.");
1757 dbus_message_iter_get_basic(&sub, &id);
1758 printf("Unit %s changed.\n", id);
1762 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT32) {
1763 log_error("Failed to parse reply.");
1767 dbus_message_iter_get_basic(&sub, &id);
1768 printf("Job %u changed.\n", id);
1774 dbus_message_unref(m);
1777 dbus_message_unref(reply);
1779 dbus_error_free(&error);
1780 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1784 dbus_message_unref(m);
1787 dbus_message_unref(reply);
1789 dbus_error_free(&error);
1790 return DBUS_HANDLER_RESULT_NEED_MEMORY;
1793 static int monitor(DBusConnection *bus, char **args, unsigned n) {
1794 DBusMessage *m = NULL, *reply = NULL;
1798 dbus_error_init(&error);
1801 dbus_bus_add_match(bus,
1803 "sender='org.freedesktop.systemd1',"
1804 "interface='org.freedesktop.systemd1.Manager',"
1805 "path='/org/freedesktop/systemd1'",
1808 if (dbus_error_is_set(&error)) {
1809 log_error("Failed to add match: %s", error.message);
1814 dbus_bus_add_match(bus,
1816 "sender='org.freedesktop.systemd1',"
1817 "interface='org.freedesktop.systemd1.Unit',"
1821 if (dbus_error_is_set(&error)) {
1822 log_error("Failed to add match: %s", error.message);
1827 dbus_bus_add_match(bus,
1829 "sender='org.freedesktop.systemd1',"
1830 "interface='org.freedesktop.systemd1.Job',"
1834 if (dbus_error_is_set(&error)) {
1835 log_error("Failed to add match: %s", error.message);
1841 if (!dbus_connection_add_filter(bus, monitor_filter, NULL, NULL)) {
1842 log_error("Failed to add filter.");
1847 if (!(m = dbus_message_new_method_call(
1848 "org.freedesktop.systemd1",
1849 "/org/freedesktop/systemd1",
1850 "org.freedesktop.systemd1.Manager",
1852 log_error("Could not allocate message.");
1857 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1858 log_error("Failed to issue method call: %s", error.message);
1863 while (dbus_connection_read_write_dispatch(bus, -1))
1870 /* This is slightly dirty, since we don't undo the filter or the matches. */
1873 dbus_message_unref(m);
1876 dbus_message_unref(reply);
1878 dbus_error_free(&error);
1883 static int dump(DBusConnection *bus, char **args, unsigned n) {
1884 DBusMessage *m = NULL, *reply = NULL;
1889 dbus_error_init(&error);
1891 if (!(m = dbus_message_new_method_call(
1892 "org.freedesktop.systemd1",
1893 "/org/freedesktop/systemd1",
1894 "org.freedesktop.systemd1.Manager",
1896 log_error("Could not allocate message.");
1900 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1901 log_error("Failed to issue method call: %s", error.message);
1906 if (!dbus_message_get_args(reply, &error,
1907 DBUS_TYPE_STRING, &text,
1908 DBUS_TYPE_INVALID)) {
1909 log_error("Failed to parse reply: %s", error.message);
1914 fputs(text, stdout);
1920 dbus_message_unref(m);
1923 dbus_message_unref(reply);
1925 dbus_error_free(&error);
1930 static int snapshot(DBusConnection *bus, char **args, unsigned n) {
1931 DBusMessage *m = NULL, *reply = NULL;
1934 const char *name = "", *path, *id;
1935 dbus_bool_t cleanup = FALSE;
1936 DBusMessageIter iter, sub;
1938 *interface = "org.freedesktop.systemd1.Unit",
1941 dbus_error_init(&error);
1943 if (!(m = dbus_message_new_method_call(
1944 "org.freedesktop.systemd1",
1945 "/org/freedesktop/systemd1",
1946 "org.freedesktop.systemd1.Manager",
1947 "CreateSnapshot"))) {
1948 log_error("Could not allocate message.");
1955 if (!dbus_message_append_args(m,
1956 DBUS_TYPE_STRING, &name,
1957 DBUS_TYPE_BOOLEAN, &cleanup,
1958 DBUS_TYPE_INVALID)) {
1959 log_error("Could not append arguments to message.");
1964 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1965 log_error("Failed to issue method call: %s", error.message);
1970 if (!dbus_message_get_args(reply, &error,
1971 DBUS_TYPE_OBJECT_PATH, &path,
1972 DBUS_TYPE_INVALID)) {
1973 log_error("Failed to parse reply: %s", error.message);
1978 dbus_message_unref(m);
1979 if (!(m = dbus_message_new_method_call(
1980 "org.freedesktop.systemd1",
1982 "org.freedesktop.DBus.Properties",
1984 log_error("Could not allocate message.");
1988 if (!dbus_message_append_args(m,
1989 DBUS_TYPE_STRING, &interface,
1990 DBUS_TYPE_STRING, &property,
1991 DBUS_TYPE_INVALID)) {
1992 log_error("Could not append arguments to message.");
1997 dbus_message_unref(reply);
1998 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1999 log_error("Failed to issue method call: %s", error.message);
2004 if (!dbus_message_iter_init(reply, &iter) ||
2005 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2006 log_error("Failed to parse reply.");
2011 dbus_message_iter_recurse(&iter, &sub);
2013 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2014 log_error("Failed to parse reply.");
2019 dbus_message_iter_get_basic(&sub, &id);
2027 dbus_message_unref(m);
2030 dbus_message_unref(reply);
2032 dbus_error_free(&error);
2037 static int delete_snapshot(DBusConnection *bus, char **args, unsigned n) {
2038 DBusMessage *m = NULL, *reply = NULL;
2046 dbus_error_init(&error);
2048 for (i = 1; i < n; i++) {
2049 const char *path = NULL;
2051 if (!(m = dbus_message_new_method_call(
2052 "org.freedesktop.systemd1",
2053 "/org/freedesktop/systemd1",
2054 "org.freedesktop.systemd1.Manager",
2056 log_error("Could not allocate message.");
2061 if (!dbus_message_append_args(m,
2062 DBUS_TYPE_STRING, &args[i],
2063 DBUS_TYPE_INVALID)) {
2064 log_error("Could not append arguments to message.");
2069 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2070 log_error("Failed to issue method call: %s", error.message);
2075 if (!dbus_message_get_args(reply, &error,
2076 DBUS_TYPE_OBJECT_PATH, &path,
2077 DBUS_TYPE_INVALID)) {
2078 log_error("Failed to parse reply: %s", error.message);
2083 dbus_message_unref(m);
2084 if (!(m = dbus_message_new_method_call(
2085 "org.freedesktop.systemd1",
2087 "org.freedesktop.systemd1.Snapshot",
2089 log_error("Could not allocate message.");
2094 dbus_message_unref(reply);
2095 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2096 log_error("Failed to issue method call: %s", error.message);
2101 dbus_message_unref(m);
2102 dbus_message_unref(reply);
2110 dbus_message_unref(m);
2113 dbus_message_unref(reply);
2115 dbus_error_free(&error);
2120 static int clear_jobs(DBusConnection *bus, char **args, unsigned n) {
2121 DBusMessage *m = NULL, *reply = NULL;
2126 dbus_error_init(&error);
2128 if (arg_action == ACTION_RELOAD)
2130 else if (arg_action == ACTION_REEXEC)
2131 method = "Reexecute";
2133 assert(arg_action == ACTION_SYSTEMCTL);
2136 streq(args[0], "clear-jobs") ? "ClearJobs" :
2137 streq(args[0], "daemon-reload") ? "Reload" :
2138 streq(args[0], "daemon-reexec") ? "Reexecute" :
2142 if (!(m = dbus_message_new_method_call(
2143 "org.freedesktop.systemd1",
2144 "/org/freedesktop/systemd1",
2145 "org.freedesktop.systemd1.Manager",
2147 log_error("Could not allocate message.");
2151 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2153 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
2154 /* There's always a fallback possible for
2155 * legacy actions. */
2160 log_error("Failed to issue method call: %s", error.message);
2169 dbus_message_unref(m);
2172 dbus_message_unref(reply);
2174 dbus_error_free(&error);
2179 static int show_enviroment(DBusConnection *bus, char **args, unsigned n) {
2180 DBusMessage *m = NULL, *reply = NULL;
2182 DBusMessageIter iter, sub, sub2;
2185 *interface = "org.freedesktop.systemd1.Manager",
2186 *property = "Environment";
2188 dbus_error_init(&error);
2190 if (!(m = dbus_message_new_method_call(
2191 "org.freedesktop.systemd1",
2192 "/org/freedesktop/systemd1",
2193 "org.freedesktop.DBus.Properties",
2195 log_error("Could not allocate message.");
2199 if (!dbus_message_append_args(m,
2200 DBUS_TYPE_STRING, &interface,
2201 DBUS_TYPE_STRING, &property,
2202 DBUS_TYPE_INVALID)) {
2203 log_error("Could not append arguments to message.");
2208 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2209 log_error("Failed to issue method call: %s", error.message);
2214 if (!dbus_message_iter_init(reply, &iter) ||
2215 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2216 log_error("Failed to parse reply.");
2221 dbus_message_iter_recurse(&iter, &sub);
2223 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
2224 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
2225 log_error("Failed to parse reply.");
2230 dbus_message_iter_recurse(&sub, &sub2);
2232 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
2235 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
2236 log_error("Failed to parse reply.");
2241 dbus_message_iter_get_basic(&sub2, &text);
2242 printf("%s\n", text);
2244 dbus_message_iter_next(&sub2);
2251 dbus_message_unref(m);
2254 dbus_message_unref(reply);
2256 dbus_error_free(&error);
2261 static int set_environment(DBusConnection *bus, char **args, unsigned n) {
2262 DBusMessage *m = NULL, *reply = NULL;
2266 DBusMessageIter iter, sub;
2269 dbus_error_init(&error);
2271 method = streq(args[0], "set-environment")
2273 : "UnsetEnvironment";
2275 if (!(m = dbus_message_new_method_call(
2276 "org.freedesktop.systemd1",
2277 "/org/freedesktop/systemd1",
2278 "org.freedesktop.systemd1.Manager",
2281 log_error("Could not allocate message.");
2285 dbus_message_iter_init_append(m, &iter);
2287 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
2288 log_error("Could not append arguments to message.");
2293 for (i = 1; i < n; i++)
2294 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &args[i])) {
2295 log_error("Could not append arguments to message.");
2300 if (!dbus_message_iter_close_container(&iter, &sub)) {
2301 log_error("Could not append arguments to message.");
2306 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2307 log_error("Failed to issue method call: %s", error.message);
2316 dbus_message_unref(m);
2319 dbus_message_unref(reply);
2321 dbus_error_free(&error);
2326 static int systemctl_help(void) {
2328 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
2329 "Send control commands to the systemd manager.\n\n"
2330 " -h --help Show this help\n"
2331 " -t --type=TYPE List only units of a particular type\n"
2332 " -p --property=NAME Show only properties by this name\n"
2333 " -a --all Show all units/properties, including dead/empty ones\n"
2334 " --replace When installing a new job, replace existing conflicting ones\n"
2335 " --system Connect to system bus\n"
2336 " --session Connect to session bus\n"
2337 " -q --quiet Suppress output\n"
2338 " --no-block Do not wait until operation finished\n"
2339 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
2341 " list-units List units\n"
2342 " start [NAME...] Start one or more units\n"
2343 " stop [NAME...] Stop one or more units\n"
2344 " restart [NAME...] Restart one or more units\n"
2345 " reload [NAME...] Reload one or more units\n"
2346 " isolate [NAME] Start one unit and stop all others\n"
2347 " check [NAME...] Check whether any of the passed units are active\n"
2348 " status [NAME...] Show status of one or more units\n"
2349 " show [NAME...|JOB...] Show properties of one or more units/jobs/manager\n"
2350 " load [NAME...] Load one or more units\n"
2351 " list-jobs List jobs\n"
2352 " cancel [JOB...] Cancel one or more jobs\n"
2353 " clear-jobs Cancel all jobs\n"
2354 " monitor Monitor unit/job changes\n"
2355 " dump Dump server status\n"
2356 " snapshot [NAME] Create a snapshot\n"
2357 " delete [NAME...] Remove one or more snapshots\n"
2358 " daemon-reload Reload systemd manager configuration\n"
2359 " daemon-reexec Reexecute systemd manager\n"
2360 " daemon-exit Ask the systemd manager to quit\n"
2361 " show-environment Dump environment\n"
2362 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
2363 " unset-environment [NAME...] Unset one or more environment variables\n"
2364 " halt Shut down and halt the system\n"
2365 " poweroff Shut down and power-off the system\n"
2366 " reboot Shut down and reboot the system\n"
2367 " default Enter default mode\n"
2368 " rescue Enter rescue mode\n"
2369 " emergency Enter emergency mode\n",
2370 program_invocation_short_name);
2375 static int halt_help(void) {
2377 printf("%s [OPTIONS...]\n\n"
2378 "%s the system.\n\n"
2379 " --help Show this help\n"
2380 " --halt Halt the machine\n"
2381 " -p --poweroff Switch off the machine\n"
2382 " --reboot Reboot the machine\n"
2383 " -f --force Force immediate halt/power-off/reboot\n"
2384 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
2385 " -d --no-wtmp Don't write wtmp record\n"
2386 " -n --no-sync Don't sync before halt/power-off/reboot\n"
2387 " --no-wall Don't send wall message before halt/power-off/reboot\n",
2388 program_invocation_short_name,
2389 arg_action == ACTION_REBOOT ? "Reboot" :
2390 arg_action == ACTION_POWEROFF ? "Power off" :
2396 static int shutdown_help(void) {
2398 printf("%s [OPTIONS...] [now] [WALL...]\n\n"
2399 "Shut down the system.\n\n"
2400 " --help Show this help\n"
2401 " -H --halt Halt the machine\n"
2402 " -P --poweroff Power-off the machine\n"
2403 " -r --reboot Reboot the machine\n"
2404 " -h Equivalent to --poweroff, overriden by --halt\n"
2405 " -k Don't halt/power-off/reboot, just send warnings\n"
2406 " --no-wall Don't send wall message before halt/power-off/reboot\n",
2407 program_invocation_short_name);
2412 static int telinit_help(void) {
2414 printf("%s [OPTIONS...] {COMMAND}\n\n"
2415 "Send control commands to the init daemon.\n\n"
2416 " --help Show this help\n"
2417 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
2419 " 0 Power-off the machine\n"
2420 " 6 Reboot the machine\n"
2421 " 2, 3, 4, 5 Start runlevelX.target unit\n"
2422 " 1, s, S Enter rescue mode\n"
2423 " q, Q Reload init daemon configuration\n"
2424 " u, U Reexecute init daemon\n",
2425 program_invocation_short_name);
2430 static int runlevel_help(void) {
2432 printf("%s [OPTIONS...]\n\n"
2433 "Prints the previous and current runlevel of the init system.\n\n"
2434 " --help Show this help\n",
2435 program_invocation_short_name);
2440 static int systemctl_parse_argv(int argc, char *argv[]) {
2443 ARG_REPLACE = 0x100,
2450 static const struct option options[] = {
2451 { "help", no_argument, NULL, 'h' },
2452 { "type", required_argument, NULL, 't' },
2453 { "property", required_argument, NULL, 'p' },
2454 { "all", no_argument, NULL, 'a' },
2455 { "replace", no_argument, NULL, ARG_REPLACE },
2456 { "session", no_argument, NULL, ARG_SESSION },
2457 { "system", no_argument, NULL, ARG_SYSTEM },
2458 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
2459 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2460 { "quiet", no_argument, NULL, 'q' },
2461 { NULL, 0, NULL, 0 }
2469 while ((c = getopt_long(argc, argv, "ht:p:aq", options, NULL)) >= 0) {
2482 arg_property = optarg;
2484 /* If the user asked for a particular
2485 * property, show it to him, even if it is
2503 arg_session = false;
2507 arg_no_block = true;
2522 log_error("Unknown option code %c", c);
2530 static int halt_parse_argv(int argc, char *argv[]) {
2539 static const struct option options[] = {
2540 { "help", no_argument, NULL, ARG_HELP },
2541 { "halt", no_argument, NULL, ARG_HALT },
2542 { "poweroff", no_argument, NULL, 'p' },
2543 { "reboot", no_argument, NULL, ARG_REBOOT },
2544 { "force", no_argument, NULL, 'f' },
2545 { "wtmp-only", no_argument, NULL, 'w' },
2546 { "no-wtmp", no_argument, NULL, 'd' },
2547 { "no-sync", no_argument, NULL, 'n' },
2548 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2549 { NULL, 0, NULL, 0 }
2557 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
2558 if (runlevel == '0' || runlevel == '6')
2559 arg_immediate = true;
2561 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
2569 arg_action = ACTION_HALT;
2573 arg_action = ACTION_POWEROFF;
2577 arg_action = ACTION_REBOOT;
2581 arg_immediate = true;
2602 /* Compatibility nops */
2609 log_error("Unknown option code %c", c);
2614 if (optind < argc) {
2615 log_error("Too many arguments.");
2622 static int shutdown_parse_argv(int argc, char *argv[]) {
2629 static const struct option options[] = {
2630 { "help", no_argument, NULL, ARG_HELP },
2631 { "halt", no_argument, NULL, 'H' },
2632 { "poweroff", no_argument, NULL, 'P' },
2633 { "reboot", no_argument, NULL, 'r' },
2634 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2635 { NULL, 0, NULL, 0 }
2643 while ((c = getopt_long(argc, argv, "HPrhkt:a", options, NULL)) >= 0) {
2651 arg_action = ACTION_HALT;
2655 arg_action = ACTION_POWEROFF;
2659 arg_action = ACTION_REBOOT;
2663 if (arg_action != ACTION_HALT)
2664 arg_action = ACTION_POWEROFF;
2677 /* Compatibility nops */
2684 log_error("Unknown option code %c", c);
2689 if (argc > optind && !streq(argv[optind], "now"))
2690 log_warning("First argument '%s' isn't 'now'. Ignoring.", argv[optind]);
2692 /* We ignore the time argument */
2693 if (argc > optind + 1)
2694 arg_wall = argv + optind + 1;
2701 static int telinit_parse_argv(int argc, char *argv[]) {
2708 static const struct option options[] = {
2709 { "help", no_argument, NULL, ARG_HELP },
2710 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2711 { NULL, 0, NULL, 0 }
2714 static const struct {
2718 { '0', ACTION_POWEROFF },
2719 { '6', ACTION_REBOOT },
2720 { '1', ACTION_RESCUE },
2721 { '2', ACTION_RUNLEVEL2 },
2722 { '3', ACTION_RUNLEVEL3 },
2723 { '4', ACTION_RUNLEVEL4 },
2724 { '5', ACTION_RUNLEVEL5 },
2725 { 's', ACTION_RESCUE },
2726 { 'S', ACTION_RESCUE },
2727 { 'q', ACTION_RELOAD },
2728 { 'Q', ACTION_RELOAD },
2729 { 'u', ACTION_REEXEC },
2730 { 'U', ACTION_REEXEC }
2739 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
2754 log_error("Unknown option code %c", c);
2759 if (optind >= argc) {
2764 if (optind + 1 < argc) {
2765 log_error("Too many arguments.");
2769 if (strlen(argv[optind]) != 1) {
2770 log_error("Expected single character argument.");
2774 for (i = 0; i < ELEMENTSOF(table); i++)
2775 if (table[i].from == argv[optind][0])
2778 if (i >= ELEMENTSOF(table)) {
2779 log_error("Unknown command %s.", argv[optind]);
2783 arg_action = table[i].to;
2790 static int runlevel_parse_argv(int argc, char *argv[]) {
2796 static const struct option options[] = {
2797 { "help", no_argument, NULL, ARG_HELP },
2798 { NULL, 0, NULL, 0 }
2806 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
2817 log_error("Unknown option code %c", c);
2822 if (optind < argc) {
2823 log_error("Too many arguments.");
2830 static int parse_argv(int argc, char *argv[]) {
2834 if (program_invocation_short_name) {
2836 if (strstr(program_invocation_short_name, "halt")) {
2837 arg_action = ACTION_HALT;
2838 return halt_parse_argv(argc, argv);
2839 } else if (strstr(program_invocation_short_name, "poweroff")) {
2840 arg_action = ACTION_POWEROFF;
2841 return halt_parse_argv(argc, argv);
2842 } else if (strstr(program_invocation_short_name, "reboot")) {
2843 arg_action = ACTION_REBOOT;
2844 return halt_parse_argv(argc, argv);
2845 } else if (strstr(program_invocation_short_name, "shutdown")) {
2846 arg_action = ACTION_POWEROFF;
2847 return shutdown_parse_argv(argc, argv);
2848 } else if (strstr(program_invocation_short_name, "init")) {
2849 arg_action = ACTION_INVALID;
2850 return telinit_parse_argv(argc, argv);
2851 } else if (strstr(program_invocation_short_name, "runlevel")) {
2852 arg_action = ACTION_RUNLEVEL;
2853 return runlevel_parse_argv(argc, argv);
2857 arg_action = ACTION_SYSTEMCTL;
2858 return systemctl_parse_argv(argc, argv);
2861 static int action_to_runlevel(void) {
2863 static const char table[_ACTION_MAX] = {
2864 [ACTION_HALT] = '0',
2865 [ACTION_POWEROFF] = '0',
2866 [ACTION_REBOOT] = '6',
2867 [ACTION_RUNLEVEL2] = '2',
2868 [ACTION_RUNLEVEL3] = '3',
2869 [ACTION_RUNLEVEL4] = '4',
2870 [ACTION_RUNLEVEL5] = '5',
2871 [ACTION_RESCUE] = '1'
2874 assert(arg_action < _ACTION_MAX);
2876 return table[arg_action];
2879 static int talk_upstart(void) {
2880 DBusMessage *m = NULL, *reply = NULL;
2882 int previous, rl, r;
2884 env1_buf[] = "RUNLEVEL=X",
2885 env2_buf[] = "PREVLEVEL=X";
2886 char *env1 = env1_buf, *env2 = env2_buf;
2887 const char *emit = "runlevel";
2888 dbus_bool_t b_false = FALSE;
2889 DBusMessageIter iter, sub;
2890 DBusConnection *bus;
2892 dbus_error_init(&error);
2894 if (!(rl = action_to_runlevel()))
2897 if (utmp_get_runlevel(&previous, NULL) < 0)
2900 if (!(bus = dbus_connection_open("unix:abstract=/com/ubuntu/upstart", &error))) {
2901 if (dbus_error_has_name(&error, DBUS_ERROR_NO_SERVER)) {
2906 log_error("Failed to connect to Upstart bus: %s", error.message);
2911 if ((r = bus_check_peercred(bus)) < 0) {
2912 log_error("Failed to verify owner of bus.");
2916 if (!(m = dbus_message_new_method_call(
2917 "com.ubuntu.Upstart",
2918 "/com/ubuntu/Upstart",
2919 "com.ubuntu.Upstart0_6",
2922 log_error("Could not allocate message.");
2927 dbus_message_iter_init_append(m, &iter);
2929 env1_buf[sizeof(env1_buf)-2] = rl;
2930 env2_buf[sizeof(env2_buf)-2] = previous;
2932 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
2933 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
2934 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
2935 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
2936 !dbus_message_iter_close_container(&iter, &sub) ||
2937 !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
2938 log_error("Could not append arguments to message.");
2943 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2945 if (error_is_no_service(&error)) {
2950 log_error("Failed to issue method call: %s", error.message);
2959 dbus_message_unref(m);
2962 dbus_message_unref(reply);
2965 dbus_connection_unref(bus);
2967 dbus_error_free(&error);
2972 static int talk_initctl(void) {
2973 struct init_request request;
2977 if (!(rl = action_to_runlevel()))
2981 request.magic = INIT_MAGIC;
2982 request.sleeptime = 0;
2983 request.cmd = INIT_CMD_RUNLVL;
2984 request.runlevel = rl;
2986 if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
2988 if (errno == ENOENT)
2991 log_error("Failed to open "INIT_FIFO": %m");
2996 r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
2997 close_nointr_nofail(fd);
3000 log_error("Failed to write to "INIT_FIFO": %m");
3001 return errno ? -errno : -EIO;
3007 static int systemctl_main(DBusConnection *bus, int argc, char *argv[]) {
3009 static const struct {
3017 int (* const dispatch)(DBusConnection *bus, char **args, unsigned n);
3019 { "list-units", LESS, 1, list_units },
3020 { "list-jobs", EQUAL, 1, list_jobs },
3021 { "clear-jobs", EQUAL, 1, clear_jobs },
3022 { "load", MORE, 2, load_unit },
3023 { "cancel", MORE, 2, cancel_job },
3024 { "start", MORE, 2, start_unit },
3025 { "stop", MORE, 2, start_unit },
3026 { "reload", MORE, 2, start_unit },
3027 { "restart", MORE, 2, start_unit },
3028 { "isolate", EQUAL, 2, start_unit },
3029 { "check", MORE, 2, check_unit },
3030 { "show", MORE, 1, show },
3031 { "status", MORE, 2, show },
3032 { "monitor", EQUAL, 1, monitor },
3033 { "dump", EQUAL, 1, dump },
3034 { "snapshot", LESS, 2, snapshot },
3035 { "delete", MORE, 2, delete_snapshot },
3036 { "daemon-reload", EQUAL, 1, clear_jobs },
3037 { "daemon-reexec", EQUAL, 1, clear_jobs },
3038 { "daemon-exit", EQUAL, 1, clear_jobs },
3039 { "show-environment", EQUAL, 1, show_enviroment },
3040 { "set-environment", MORE, 2, set_environment },
3041 { "unset-environment", MORE, 2, set_environment },
3042 { "halt", EQUAL, 1, start_special },
3043 { "poweroff", EQUAL, 1, start_special },
3044 { "reboot", EQUAL, 1, start_special },
3045 { "default", EQUAL, 1, start_special },
3046 { "rescue", EQUAL, 1, start_special },
3047 { "emergency", EQUAL, 1, start_special }
3057 left = argc - optind;
3060 /* Special rule: no arguments means "list-units" */
3063 if (streq(argv[optind], "help")) {
3068 for (i = 0; i < ELEMENTSOF(verbs); i++)
3069 if (streq(argv[optind], verbs[i].verb))
3072 if (i >= ELEMENTSOF(verbs)) {
3073 log_error("Unknown operation %s", argv[optind]);
3078 switch (verbs[i].argc_cmp) {
3081 if (left != verbs[i].argc) {
3082 log_error("Invalid number of arguments.");
3089 if (left < verbs[i].argc) {
3090 log_error("Too few arguments.");
3097 if (left > verbs[i].argc) {
3098 log_error("Too many arguments.");
3105 assert_not_reached("Unknown comparison operator.");
3108 return verbs[i].dispatch(bus, argv + optind, left);
3111 static int reload_with_fallback(DBusConnection *bus) {
3115 /* First, try systemd via D-Bus. */
3116 if ((r = clear_jobs(bus, NULL, 0)) > 0)
3120 /* Nothing else worked, so let's try signals */
3121 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
3123 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
3124 log_error("kill() failed: %m");
3131 static int start_with_fallback(DBusConnection *bus) {
3134 warn_wall(arg_action);
3137 /* First, try systemd via D-Bus. */
3138 if ((r = start_unit(bus, NULL, 0)) > 0)
3141 /* Hmm, talking to systemd via D-Bus didn't work. Then
3142 * let's try to talk to Upstart via D-Bus. */
3143 if ((r = talk_upstart()) > 0)
3147 /* Nothing else worked, so let's try
3149 if ((r = talk_initctl()) != 0)
3152 log_error("Failed to talk to init daemon.");
3156 static int halt_main(DBusConnection *bus) {
3159 if (geteuid() != 0) {
3160 log_error("Must to be root.");
3164 if (!arg_dry && !arg_immediate)
3165 return start_with_fallback(bus);
3168 if ((r = utmp_put_shutdown(0)) < 0)
3169 log_warning("Failed to write utmp record: %s", strerror(-r));
3177 /* Make sure C-A-D is handled by the kernel from this
3179 reboot(RB_ENABLE_CAD);
3181 switch (arg_action) {
3184 log_info("Halting");
3185 reboot(RB_HALT_SYSTEM);
3188 case ACTION_POWEROFF:
3189 log_info("Powering off");
3190 reboot(RB_POWER_OFF);
3194 log_info("Rebooting");
3195 reboot(RB_AUTOBOOT);
3199 assert_not_reached("Unknown halt action.");
3202 /* We should never reach this. */
3206 static int runlevel_main(void) {
3207 int r, runlevel, previous;
3209 if ((r = utmp_get_runlevel(&runlevel, &previous)) < 0) {
3215 previous <= 0 ? 'N' : previous,
3216 runlevel <= 0 ? 'N' : runlevel);
3221 int main(int argc, char*argv[]) {
3223 DBusConnection *bus = NULL;
3226 dbus_error_init(&error);
3228 log_parse_environment();
3230 if ((r = parse_argv(argc, argv)) < 0)
3237 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
3238 * let's shortcut this */
3239 if (arg_action == ACTION_RUNLEVEL) {
3240 retval = runlevel_main() < 0;
3244 bus_connect(arg_session ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &bus, &private_bus, &error);
3246 switch (arg_action) {
3248 case ACTION_SYSTEMCTL: {
3251 log_error("Failed to get D-Bus connection: %s", error.message);
3255 retval = systemctl_main(bus, argc, argv) < 0;
3260 case ACTION_POWEROFF:
3262 retval = halt_main(bus) < 0;
3265 case ACTION_RUNLEVEL2:
3266 case ACTION_RUNLEVEL3:
3267 case ACTION_RUNLEVEL4:
3268 case ACTION_RUNLEVEL5:
3270 case ACTION_EMERGENCY:
3271 case ACTION_DEFAULT:
3272 retval = start_with_fallback(bus) < 0;
3277 retval = reload_with_fallback(bus) < 0;
3280 case ACTION_INVALID:
3281 case ACTION_RUNLEVEL:
3283 assert_not_reached("Unknown action");
3289 dbus_connection_unref(bus);
3291 dbus_error_free(&error);