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;
953 const char *sysfs_path;
955 /* Mount, Automount */
962 static void print_status_info(UnitStatusInfo *i) {
965 /* This shows pretty information about a unit. See
966 * print_property() for a low-level property printer */
968 printf("%s", strna(i->id));
970 if (i->description && !streq_ptr(i->id, i->description))
971 printf(" - %s", i->description);
975 if (i->fragment_path)
976 printf("\t Loaded: %s (%s)\n", strna(i->load_state), i->fragment_path);
977 else if (streq_ptr(i->load_state, "failed"))
978 printf("\t Loaded: " ANSI_HIGHLIGHT_ON "%s" ANSI_HIGHLIGHT_OFF "\n", strna(i->load_state));
980 printf("\t Loaded: %s\n", strna(i->load_state));
982 if (streq_ptr(i->active_state, "maintenance"))
983 printf("\t Active: " ANSI_HIGHLIGHT_ON "%s (%s)" ANSI_HIGHLIGHT_OFF "\n",
984 strna(i->active_state),
985 strna(i->sub_state));
987 printf("\t Active: %s (%s)\n",
988 strna(i->active_state),
989 strna(i->sub_state));
992 printf("\t Device: %s\n", i->sysfs_path);
994 printf("\t Where: %s\n", i->where);
996 printf("\t What: %s\n", i->what);
999 printf("\t Status: \"%s\"\n", i->status_text);
1002 printf("\tAccepted: %u; Connected: %u\n", i->n_accepted, i->n_connections);
1004 if (i->main_pid > 0 || i->control_pid > 0) {
1007 if (i->main_pid > 0) {
1008 printf(" Process: %u", (unsigned) i->main_pid);
1012 get_process_name(i->main_pid, &t);
1018 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
1020 if (i->exit_code == CLD_EXITED)
1021 printf("status=%i", i->exit_status);
1023 printf("signal=%s", strsignal(i->exit_status));
1028 if (i->main_pid > 0 && i->control_pid > 0)
1031 if (i->control_pid > 0) {
1034 printf(" Control: %u", (unsigned) i->control_pid);
1036 get_process_name(i->control_pid, &t);
1046 if (i->default_control_group) {
1049 printf("\t CGroup: %s\n", i->default_control_group);
1051 if ((c = columns()) > 18)
1056 show_cgroup_recursive(i->default_control_group, "\t\t ", c);
1060 static int status_property(const char *name, DBusMessageIter *iter, UnitStatusInfo *i) {
1062 switch (dbus_message_iter_get_arg_type(iter)) {
1064 case DBUS_TYPE_STRING: {
1067 dbus_message_iter_get_basic(iter, &s);
1070 if (streq(name, "Id"))
1072 else if (streq(name, "LoadState"))
1074 else if (streq(name, "ActiveState"))
1075 i->active_state = s;
1076 else if (streq(name, "SubState"))
1078 else if (streq(name, "Description"))
1080 else if (streq(name, "FragmentPath"))
1081 i->fragment_path = s;
1082 else if (streq(name, "DefaultControlGroup"))
1083 i->default_control_group = s;
1084 else if (streq(name, "StatusText"))
1086 else if (streq(name, "SysFSPath"))
1088 else if (streq(name, "Where"))
1090 else if (streq(name, "What"))
1097 case DBUS_TYPE_BOOLEAN: {
1100 dbus_message_iter_get_basic(iter, &b);
1102 if (streq(name, "Accept"))
1108 case DBUS_TYPE_UINT32: {
1111 dbus_message_iter_get_basic(iter, &u);
1113 if (streq(name, "MainPID")) {
1115 i->main_pid = (pid_t) u;
1118 } else if (streq(name, "ControlPID"))
1119 i->control_pid = (pid_t) u;
1120 else if (streq(name, "ExecMainPID")) {
1122 i->main_pid = (pid_t) u;
1123 } else if (streq(name, "NAccepted"))
1125 else if (streq(name, "NConnections"))
1126 i->n_connections = u;
1131 case DBUS_TYPE_INT32: {
1134 dbus_message_iter_get_basic(iter, &j);
1136 if (streq(name, "ExecMainCode"))
1137 i->exit_code = (int) j;
1138 else if (streq(name, "ExecMainStatus"))
1139 i->exit_status = (int) j;
1144 case DBUS_TYPE_UINT64: {
1147 dbus_message_iter_get_basic(iter, &u);
1149 if (streq(name, "ExecMainStartTimestamp"))
1150 i->start_timestamp = (usec_t) u;
1151 else if (streq(name, "ExecMainExitTimestamp"))
1152 i->exit_timestamp = (usec_t) u;
1161 static int print_property(const char *name, DBusMessageIter *iter) {
1165 /* This is a low-level property printer, see
1166 * print_status_info() for the nicer output */
1168 if (arg_property && !streq(name, arg_property))
1171 switch (dbus_message_iter_get_arg_type(iter)) {
1173 case DBUS_TYPE_STRING: {
1175 dbus_message_iter_get_basic(iter, &s);
1177 if (arg_all || s[0])
1178 printf("%s=%s\n", name, s);
1183 case DBUS_TYPE_BOOLEAN: {
1185 dbus_message_iter_get_basic(iter, &b);
1186 printf("%s=%s\n", name, yes_no(b));
1191 case DBUS_TYPE_UINT64: {
1193 dbus_message_iter_get_basic(iter, &u);
1195 /* Yes, heuristics! But we can change this check
1196 * should it turn out to not be sufficient */
1198 if (strstr(name, "Timestamp")) {
1199 char timestamp[FORMAT_TIMESTAMP_MAX], *t;
1201 if ((t = format_timestamp(timestamp, sizeof(timestamp), u)) || arg_all)
1202 printf("%s=%s\n", name, strempty(t));
1203 } else if (strstr(name, "USec")) {
1204 char timespan[FORMAT_TIMESPAN_MAX];
1206 printf("%s=%s\n", name, format_timespan(timespan, sizeof(timespan), u));
1208 printf("%s=%llu\n", name, (unsigned long long) u);
1213 case DBUS_TYPE_UINT32: {
1215 dbus_message_iter_get_basic(iter, &u);
1217 if (strstr(name, "UMask") || strstr(name, "Mode"))
1218 printf("%s=%04o\n", name, u);
1220 printf("%s=%u\n", name, (unsigned) u);
1225 case DBUS_TYPE_INT32: {
1227 dbus_message_iter_get_basic(iter, &i);
1229 printf("%s=%i\n", name, (int) i);
1233 case DBUS_TYPE_STRUCT: {
1234 DBusMessageIter sub;
1235 dbus_message_iter_recurse(iter, &sub);
1237 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "Job")) {
1240 dbus_message_iter_get_basic(&sub, &u);
1243 printf("%s=%u\n", name, (unsigned) u);
1245 printf("%s=\n", name);
1248 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Unit")) {
1251 dbus_message_iter_get_basic(&sub, &s);
1253 if (arg_all || s[0])
1254 printf("%s=%s\n", name, s);
1262 case DBUS_TYPE_ARRAY:
1264 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
1265 DBusMessageIter sub;
1268 dbus_message_iter_recurse(iter, &sub);
1271 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1272 printf("%s=", name);
1274 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1277 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
1278 dbus_message_iter_get_basic(&sub, &s);
1279 printf("%s%s", space ? " " : "", s);
1282 dbus_message_iter_next(&sub);
1289 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_BYTE) {
1290 DBusMessageIter sub;
1292 dbus_message_iter_recurse(iter, &sub);
1295 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1296 printf("%s=", name);
1298 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1301 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_BYTE);
1302 dbus_message_iter_get_basic(&sub, &u);
1305 dbus_message_iter_next(&sub);
1312 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Paths")) {
1313 DBusMessageIter sub, sub2;
1315 dbus_message_iter_recurse(iter, &sub);
1317 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1318 const char *type, *path;
1320 dbus_message_iter_recurse(&sub, &sub2);
1322 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
1323 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0)
1324 printf("%s=%s\n", type, path);
1326 dbus_message_iter_next(&sub);
1330 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Timers")) {
1331 DBusMessageIter sub, sub2;
1333 dbus_message_iter_recurse(iter, &sub);
1335 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1337 uint64_t value, next_elapse;
1339 dbus_message_iter_recurse(&sub, &sub2);
1341 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &base, true) >= 0 &&
1342 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &value, true) >= 0 &&
1343 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &next_elapse, false) >= 0) {
1344 char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX];
1346 printf("%s={ value=%s ; next_elapse=%s }\n",
1348 format_timespan(timespan1, sizeof(timespan1), value),
1349 format_timespan(timespan2, sizeof(timespan2), next_elapse));
1352 dbus_message_iter_next(&sub);
1356 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && startswith(name, "Exec")) {
1358 DBusMessageIter sub, sub2, sub3;
1360 dbus_message_iter_recurse(iter, &sub);
1362 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1364 uint64_t start_time, exit_time;
1366 int32_t code, status;
1368 dbus_message_iter_recurse(&sub, &sub2);
1370 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0)
1373 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_ARRAY ||
1374 dbus_message_iter_get_element_type(&sub2) != DBUS_TYPE_STRING)
1377 printf("%s={ path=%s ; argv[]=", name, path);
1379 dbus_message_iter_recurse(&sub2, &sub3);
1381 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1384 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1385 dbus_message_iter_get_basic(&sub3, &s);
1387 dbus_message_iter_next(&sub3);
1390 if (dbus_message_iter_next(&sub2) &&
1391 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_time, true) >= 0 &&
1392 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_time, true) >= 0 &&
1393 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, true) >= 0 &&
1394 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &code, true) >= 0 &&
1395 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &status, false) >= 0) {
1397 char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
1399 printf("; start=%s ; stop=%s ; pid=%u ; code=%s ; status=%i/%s",
1400 strna(format_timestamp(timestamp1, sizeof(timestamp1), start_time)),
1401 strna(format_timestamp(timestamp2, sizeof(timestamp2), exit_time)),
1403 sigchld_code_to_string(code),
1405 strempty(code == CLD_EXITED ? NULL : strsignal(status)));
1410 dbus_message_iter_next(&sub);
1420 printf("%s=[unprintable]\n", name);
1425 static int show_one(DBusConnection *bus, const char *path, bool show_properties, bool *new_line) {
1426 DBusMessage *m = NULL, *reply = NULL;
1427 const char *interface = "";
1430 DBusMessageIter iter, sub, sub2, sub3;
1431 UnitStatusInfo info;
1438 dbus_error_init(&error);
1440 if (!(m = dbus_message_new_method_call(
1441 "org.freedesktop.systemd1",
1443 "org.freedesktop.DBus.Properties",
1445 log_error("Could not allocate message.");
1450 if (!dbus_message_append_args(m,
1451 DBUS_TYPE_STRING, &interface,
1452 DBUS_TYPE_INVALID)) {
1453 log_error("Could not append arguments to message.");
1458 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1459 log_error("Failed to issue method call: %s", error.message);
1464 if (!dbus_message_iter_init(reply, &iter) ||
1465 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
1466 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
1467 log_error("Failed to parse reply.");
1472 dbus_message_iter_recurse(&iter, &sub);
1479 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1482 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
1483 log_error("Failed to parse reply.");
1488 dbus_message_iter_recurse(&sub, &sub2);
1490 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
1491 log_error("Failed to parse reply.");
1496 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
1497 log_error("Failed to parse reply.");
1502 dbus_message_iter_recurse(&sub2, &sub3);
1504 if (show_properties)
1505 r = print_property(name, &sub3);
1507 r = status_property(name, &sub3, &info);
1510 log_error("Failed to parse reply.");
1515 dbus_message_iter_next(&sub);
1518 if (!show_properties)
1519 print_status_info(&info);
1525 dbus_message_unref(m);
1528 dbus_message_unref(reply);
1530 dbus_error_free(&error);
1535 static int show(DBusConnection *bus, char **args, unsigned n) {
1536 DBusMessage *m = NULL, *reply = NULL;
1540 bool show_properties, new_line = false;
1545 dbus_error_init(&error);
1547 show_properties = !streq(args[0], "status");
1549 if (show_properties && n <= 1) {
1550 /* If not argument is specified inspect the manager
1553 r = show_one(bus, "/org/freedesktop/systemd1", show_properties, &new_line);
1557 for (i = 1; i < n; i++) {
1558 const char *path = NULL;
1561 if (!show_properties || safe_atou32(args[i], &id) < 0) {
1563 if (!(m = dbus_message_new_method_call(
1564 "org.freedesktop.systemd1",
1565 "/org/freedesktop/systemd1",
1566 "org.freedesktop.systemd1.Manager",
1568 log_error("Could not allocate message.");
1573 if (!dbus_message_append_args(m,
1574 DBUS_TYPE_STRING, &args[i],
1575 DBUS_TYPE_INVALID)) {
1576 log_error("Could not append arguments to message.");
1581 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1583 if (!dbus_error_has_name(&error, DBUS_ERROR_ACCESS_DENIED)) {
1584 log_error("Failed to issue method call: %s", error.message);
1589 dbus_error_free(&error);
1591 dbus_message_unref(m);
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.");
1610 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1611 log_error("Failed to issue method call: %s", error.message);
1619 if (!(m = dbus_message_new_method_call(
1620 "org.freedesktop.systemd1",
1621 "/org/freedesktop/systemd1",
1622 "org.freedesktop.systemd1.Manager",
1624 log_error("Could not allocate message.");
1629 if (!dbus_message_append_args(m,
1630 DBUS_TYPE_UINT32, &id,
1631 DBUS_TYPE_INVALID)) {
1632 log_error("Could not append arguments to message.");
1637 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1638 log_error("Failed to issue method call: %s", error.message);
1644 if (!dbus_message_get_args(reply, &error,
1645 DBUS_TYPE_OBJECT_PATH, &path,
1646 DBUS_TYPE_INVALID)) {
1647 log_error("Failed to parse reply: %s", error.message);
1652 if ((r = show_one(bus, path, show_properties, &new_line)) < 0)
1655 dbus_message_unref(m);
1656 dbus_message_unref(reply);
1664 dbus_message_unref(m);
1667 dbus_message_unref(reply);
1669 dbus_error_free(&error);
1674 static DBusHandlerResult monitor_filter(DBusConnection *connection, DBusMessage *message, void *data) {
1676 DBusMessage *m = NULL, *reply = NULL;
1681 dbus_error_init(&error);
1683 log_debug("Got D-Bus request: %s.%s() on %s",
1684 dbus_message_get_interface(message),
1685 dbus_message_get_member(message),
1686 dbus_message_get_path(message));
1688 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
1689 log_error("Warning! D-Bus connection terminated.");
1690 dbus_connection_close(connection);
1692 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitNew") ||
1693 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitRemoved")) {
1694 const char *id, *path;
1696 if (!dbus_message_get_args(message, &error,
1697 DBUS_TYPE_STRING, &id,
1698 DBUS_TYPE_OBJECT_PATH, &path,
1700 log_error("Failed to parse message: %s", error.message);
1701 else if (streq(dbus_message_get_member(message), "UnitNew"))
1702 printf("Unit %s added.\n", id);
1704 printf("Unit %s removed.\n", id);
1706 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobNew") ||
1707 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
1711 if (!dbus_message_get_args(message, &error,
1712 DBUS_TYPE_UINT32, &id,
1713 DBUS_TYPE_OBJECT_PATH, &path,
1715 log_error("Failed to parse message: %s", error.message);
1716 else if (streq(dbus_message_get_member(message), "JobNew"))
1717 printf("Job %u added.\n", id);
1719 printf("Job %u removed.\n", id);
1722 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Unit", "Changed") ||
1723 dbus_message_is_signal(message, "org.freedesktop.systemd1.Job", "Changed")) {
1725 const char *path, *interface, *property = "Id";
1726 DBusMessageIter iter, sub;
1728 path = dbus_message_get_path(message);
1729 interface = dbus_message_get_interface(message);
1731 if (!(m = dbus_message_new_method_call(
1732 "org.freedesktop.systemd1",
1734 "org.freedesktop.DBus.Properties",
1736 log_error("Could not allocate message.");
1740 if (!dbus_message_append_args(m,
1741 DBUS_TYPE_STRING, &interface,
1742 DBUS_TYPE_STRING, &property,
1743 DBUS_TYPE_INVALID)) {
1744 log_error("Could not append arguments to message.");
1748 if (!(reply = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) {
1749 log_error("Failed to issue method call: %s", error.message);
1753 if (!dbus_message_iter_init(reply, &iter) ||
1754 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1755 log_error("Failed to parse reply.");
1759 dbus_message_iter_recurse(&iter, &sub);
1761 if (streq(interface, "org.freedesktop.systemd1.Unit")) {
1764 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1765 log_error("Failed to parse reply.");
1769 dbus_message_iter_get_basic(&sub, &id);
1770 printf("Unit %s changed.\n", id);
1774 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT32) {
1775 log_error("Failed to parse reply.");
1779 dbus_message_iter_get_basic(&sub, &id);
1780 printf("Job %u changed.\n", id);
1786 dbus_message_unref(m);
1789 dbus_message_unref(reply);
1791 dbus_error_free(&error);
1792 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1796 dbus_message_unref(m);
1799 dbus_message_unref(reply);
1801 dbus_error_free(&error);
1802 return DBUS_HANDLER_RESULT_NEED_MEMORY;
1805 static int monitor(DBusConnection *bus, char **args, unsigned n) {
1806 DBusMessage *m = NULL, *reply = NULL;
1810 dbus_error_init(&error);
1813 dbus_bus_add_match(bus,
1815 "sender='org.freedesktop.systemd1',"
1816 "interface='org.freedesktop.systemd1.Manager',"
1817 "path='/org/freedesktop/systemd1'",
1820 if (dbus_error_is_set(&error)) {
1821 log_error("Failed to add match: %s", error.message);
1826 dbus_bus_add_match(bus,
1828 "sender='org.freedesktop.systemd1',"
1829 "interface='org.freedesktop.systemd1.Unit',"
1833 if (dbus_error_is_set(&error)) {
1834 log_error("Failed to add match: %s", error.message);
1839 dbus_bus_add_match(bus,
1841 "sender='org.freedesktop.systemd1',"
1842 "interface='org.freedesktop.systemd1.Job',"
1846 if (dbus_error_is_set(&error)) {
1847 log_error("Failed to add match: %s", error.message);
1853 if (!dbus_connection_add_filter(bus, monitor_filter, NULL, NULL)) {
1854 log_error("Failed to add filter.");
1859 if (!(m = dbus_message_new_method_call(
1860 "org.freedesktop.systemd1",
1861 "/org/freedesktop/systemd1",
1862 "org.freedesktop.systemd1.Manager",
1864 log_error("Could not allocate message.");
1869 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1870 log_error("Failed to issue method call: %s", error.message);
1875 while (dbus_connection_read_write_dispatch(bus, -1))
1882 /* This is slightly dirty, since we don't undo the filter or the matches. */
1885 dbus_message_unref(m);
1888 dbus_message_unref(reply);
1890 dbus_error_free(&error);
1895 static int dump(DBusConnection *bus, char **args, unsigned n) {
1896 DBusMessage *m = NULL, *reply = NULL;
1901 dbus_error_init(&error);
1903 if (!(m = dbus_message_new_method_call(
1904 "org.freedesktop.systemd1",
1905 "/org/freedesktop/systemd1",
1906 "org.freedesktop.systemd1.Manager",
1908 log_error("Could not allocate message.");
1912 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1913 log_error("Failed to issue method call: %s", error.message);
1918 if (!dbus_message_get_args(reply, &error,
1919 DBUS_TYPE_STRING, &text,
1920 DBUS_TYPE_INVALID)) {
1921 log_error("Failed to parse reply: %s", error.message);
1926 fputs(text, stdout);
1932 dbus_message_unref(m);
1935 dbus_message_unref(reply);
1937 dbus_error_free(&error);
1942 static int snapshot(DBusConnection *bus, char **args, unsigned n) {
1943 DBusMessage *m = NULL, *reply = NULL;
1946 const char *name = "", *path, *id;
1947 dbus_bool_t cleanup = FALSE;
1948 DBusMessageIter iter, sub;
1950 *interface = "org.freedesktop.systemd1.Unit",
1953 dbus_error_init(&error);
1955 if (!(m = dbus_message_new_method_call(
1956 "org.freedesktop.systemd1",
1957 "/org/freedesktop/systemd1",
1958 "org.freedesktop.systemd1.Manager",
1959 "CreateSnapshot"))) {
1960 log_error("Could not allocate message.");
1967 if (!dbus_message_append_args(m,
1968 DBUS_TYPE_STRING, &name,
1969 DBUS_TYPE_BOOLEAN, &cleanup,
1970 DBUS_TYPE_INVALID)) {
1971 log_error("Could not append arguments to message.");
1976 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1977 log_error("Failed to issue method call: %s", error.message);
1982 if (!dbus_message_get_args(reply, &error,
1983 DBUS_TYPE_OBJECT_PATH, &path,
1984 DBUS_TYPE_INVALID)) {
1985 log_error("Failed to parse reply: %s", error.message);
1990 dbus_message_unref(m);
1991 if (!(m = dbus_message_new_method_call(
1992 "org.freedesktop.systemd1",
1994 "org.freedesktop.DBus.Properties",
1996 log_error("Could not allocate message.");
2000 if (!dbus_message_append_args(m,
2001 DBUS_TYPE_STRING, &interface,
2002 DBUS_TYPE_STRING, &property,
2003 DBUS_TYPE_INVALID)) {
2004 log_error("Could not append arguments to message.");
2009 dbus_message_unref(reply);
2010 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2011 log_error("Failed to issue method call: %s", error.message);
2016 if (!dbus_message_iter_init(reply, &iter) ||
2017 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2018 log_error("Failed to parse reply.");
2023 dbus_message_iter_recurse(&iter, &sub);
2025 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2026 log_error("Failed to parse reply.");
2031 dbus_message_iter_get_basic(&sub, &id);
2039 dbus_message_unref(m);
2042 dbus_message_unref(reply);
2044 dbus_error_free(&error);
2049 static int delete_snapshot(DBusConnection *bus, char **args, unsigned n) {
2050 DBusMessage *m = NULL, *reply = NULL;
2058 dbus_error_init(&error);
2060 for (i = 1; i < n; i++) {
2061 const char *path = NULL;
2063 if (!(m = dbus_message_new_method_call(
2064 "org.freedesktop.systemd1",
2065 "/org/freedesktop/systemd1",
2066 "org.freedesktop.systemd1.Manager",
2068 log_error("Could not allocate message.");
2073 if (!dbus_message_append_args(m,
2074 DBUS_TYPE_STRING, &args[i],
2075 DBUS_TYPE_INVALID)) {
2076 log_error("Could not append arguments to message.");
2081 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2082 log_error("Failed to issue method call: %s", error.message);
2087 if (!dbus_message_get_args(reply, &error,
2088 DBUS_TYPE_OBJECT_PATH, &path,
2089 DBUS_TYPE_INVALID)) {
2090 log_error("Failed to parse reply: %s", error.message);
2095 dbus_message_unref(m);
2096 if (!(m = dbus_message_new_method_call(
2097 "org.freedesktop.systemd1",
2099 "org.freedesktop.systemd1.Snapshot",
2101 log_error("Could not allocate message.");
2106 dbus_message_unref(reply);
2107 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2108 log_error("Failed to issue method call: %s", error.message);
2113 dbus_message_unref(m);
2114 dbus_message_unref(reply);
2122 dbus_message_unref(m);
2125 dbus_message_unref(reply);
2127 dbus_error_free(&error);
2132 static int clear_jobs(DBusConnection *bus, char **args, unsigned n) {
2133 DBusMessage *m = NULL, *reply = NULL;
2138 dbus_error_init(&error);
2140 if (arg_action == ACTION_RELOAD)
2142 else if (arg_action == ACTION_REEXEC)
2143 method = "Reexecute";
2145 assert(arg_action == ACTION_SYSTEMCTL);
2148 streq(args[0], "clear-jobs") ? "ClearJobs" :
2149 streq(args[0], "daemon-reload") ? "Reload" :
2150 streq(args[0], "daemon-reexec") ? "Reexecute" :
2154 if (!(m = dbus_message_new_method_call(
2155 "org.freedesktop.systemd1",
2156 "/org/freedesktop/systemd1",
2157 "org.freedesktop.systemd1.Manager",
2159 log_error("Could not allocate message.");
2163 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2165 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
2166 /* There's always a fallback possible for
2167 * legacy actions. */
2172 log_error("Failed to issue method call: %s", error.message);
2181 dbus_message_unref(m);
2184 dbus_message_unref(reply);
2186 dbus_error_free(&error);
2191 static int show_enviroment(DBusConnection *bus, char **args, unsigned n) {
2192 DBusMessage *m = NULL, *reply = NULL;
2194 DBusMessageIter iter, sub, sub2;
2197 *interface = "org.freedesktop.systemd1.Manager",
2198 *property = "Environment";
2200 dbus_error_init(&error);
2202 if (!(m = dbus_message_new_method_call(
2203 "org.freedesktop.systemd1",
2204 "/org/freedesktop/systemd1",
2205 "org.freedesktop.DBus.Properties",
2207 log_error("Could not allocate message.");
2211 if (!dbus_message_append_args(m,
2212 DBUS_TYPE_STRING, &interface,
2213 DBUS_TYPE_STRING, &property,
2214 DBUS_TYPE_INVALID)) {
2215 log_error("Could not append arguments to message.");
2220 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2221 log_error("Failed to issue method call: %s", error.message);
2226 if (!dbus_message_iter_init(reply, &iter) ||
2227 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2228 log_error("Failed to parse reply.");
2233 dbus_message_iter_recurse(&iter, &sub);
2235 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
2236 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
2237 log_error("Failed to parse reply.");
2242 dbus_message_iter_recurse(&sub, &sub2);
2244 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
2247 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
2248 log_error("Failed to parse reply.");
2253 dbus_message_iter_get_basic(&sub2, &text);
2254 printf("%s\n", text);
2256 dbus_message_iter_next(&sub2);
2263 dbus_message_unref(m);
2266 dbus_message_unref(reply);
2268 dbus_error_free(&error);
2273 static int set_environment(DBusConnection *bus, char **args, unsigned n) {
2274 DBusMessage *m = NULL, *reply = NULL;
2278 DBusMessageIter iter, sub;
2281 dbus_error_init(&error);
2283 method = streq(args[0], "set-environment")
2285 : "UnsetEnvironment";
2287 if (!(m = dbus_message_new_method_call(
2288 "org.freedesktop.systemd1",
2289 "/org/freedesktop/systemd1",
2290 "org.freedesktop.systemd1.Manager",
2293 log_error("Could not allocate message.");
2297 dbus_message_iter_init_append(m, &iter);
2299 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
2300 log_error("Could not append arguments to message.");
2305 for (i = 1; i < n; i++)
2306 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &args[i])) {
2307 log_error("Could not append arguments to message.");
2312 if (!dbus_message_iter_close_container(&iter, &sub)) {
2313 log_error("Could not append arguments to message.");
2318 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2319 log_error("Failed to issue method call: %s", error.message);
2328 dbus_message_unref(m);
2331 dbus_message_unref(reply);
2333 dbus_error_free(&error);
2338 static int systemctl_help(void) {
2340 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
2341 "Send control commands to the systemd manager.\n\n"
2342 " -h --help Show this help\n"
2343 " -t --type=TYPE List only units of a particular type\n"
2344 " -p --property=NAME Show only properties by this name\n"
2345 " -a --all Show all units/properties, including dead/empty ones\n"
2346 " --replace When installing a new job, replace existing conflicting ones\n"
2347 " --system Connect to system bus\n"
2348 " --session Connect to session bus\n"
2349 " -q --quiet Suppress output\n"
2350 " --no-block Do not wait until operation finished\n"
2351 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
2353 " list-units List units\n"
2354 " start [NAME...] Start one or more units\n"
2355 " stop [NAME...] Stop one or more units\n"
2356 " restart [NAME...] Restart one or more units\n"
2357 " reload [NAME...] Reload one or more units\n"
2358 " isolate [NAME] Start one unit and stop all others\n"
2359 " check [NAME...] Check whether any of the passed units are active\n"
2360 " status [NAME...] Show status of one or more units\n"
2361 " show [NAME...|JOB...] Show properties of one or more units/jobs/manager\n"
2362 " load [NAME...] Load one or more units\n"
2363 " list-jobs List jobs\n"
2364 " cancel [JOB...] Cancel one or more jobs\n"
2365 " clear-jobs Cancel all jobs\n"
2366 " monitor Monitor unit/job changes\n"
2367 " dump Dump server status\n"
2368 " snapshot [NAME] Create a snapshot\n"
2369 " delete [NAME...] Remove one or more snapshots\n"
2370 " daemon-reload Reload systemd manager configuration\n"
2371 " daemon-reexec Reexecute systemd manager\n"
2372 " daemon-exit Ask the systemd manager to quit\n"
2373 " show-environment Dump environment\n"
2374 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
2375 " unset-environment [NAME...] Unset one or more environment variables\n"
2376 " halt Shut down and halt the system\n"
2377 " poweroff Shut down and power-off the system\n"
2378 " reboot Shut down and reboot the system\n"
2379 " default Enter default mode\n"
2380 " rescue Enter rescue mode\n"
2381 " emergency Enter emergency mode\n",
2382 program_invocation_short_name);
2387 static int halt_help(void) {
2389 printf("%s [OPTIONS...]\n\n"
2390 "%s the system.\n\n"
2391 " --help Show this help\n"
2392 " --halt Halt the machine\n"
2393 " -p --poweroff Switch off the machine\n"
2394 " --reboot Reboot the machine\n"
2395 " -f --force Force immediate halt/power-off/reboot\n"
2396 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
2397 " -d --no-wtmp Don't write wtmp record\n"
2398 " -n --no-sync Don't sync before halt/power-off/reboot\n"
2399 " --no-wall Don't send wall message before halt/power-off/reboot\n",
2400 program_invocation_short_name,
2401 arg_action == ACTION_REBOOT ? "Reboot" :
2402 arg_action == ACTION_POWEROFF ? "Power off" :
2408 static int shutdown_help(void) {
2410 printf("%s [OPTIONS...] [now] [WALL...]\n\n"
2411 "Shut down the system.\n\n"
2412 " --help Show this help\n"
2413 " -H --halt Halt the machine\n"
2414 " -P --poweroff Power-off the machine\n"
2415 " -r --reboot Reboot the machine\n"
2416 " -h Equivalent to --poweroff, overriden by --halt\n"
2417 " -k Don't halt/power-off/reboot, just send warnings\n"
2418 " --no-wall Don't send wall message before halt/power-off/reboot\n",
2419 program_invocation_short_name);
2424 static int telinit_help(void) {
2426 printf("%s [OPTIONS...] {COMMAND}\n\n"
2427 "Send control commands to the init daemon.\n\n"
2428 " --help Show this help\n"
2429 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
2431 " 0 Power-off the machine\n"
2432 " 6 Reboot the machine\n"
2433 " 2, 3, 4, 5 Start runlevelX.target unit\n"
2434 " 1, s, S Enter rescue mode\n"
2435 " q, Q Reload init daemon configuration\n"
2436 " u, U Reexecute init daemon\n",
2437 program_invocation_short_name);
2442 static int runlevel_help(void) {
2444 printf("%s [OPTIONS...]\n\n"
2445 "Prints the previous and current runlevel of the init system.\n\n"
2446 " --help Show this help\n",
2447 program_invocation_short_name);
2452 static int systemctl_parse_argv(int argc, char *argv[]) {
2455 ARG_REPLACE = 0x100,
2462 static const struct option options[] = {
2463 { "help", no_argument, NULL, 'h' },
2464 { "type", required_argument, NULL, 't' },
2465 { "property", required_argument, NULL, 'p' },
2466 { "all", no_argument, NULL, 'a' },
2467 { "replace", no_argument, NULL, ARG_REPLACE },
2468 { "session", no_argument, NULL, ARG_SESSION },
2469 { "system", no_argument, NULL, ARG_SYSTEM },
2470 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
2471 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2472 { "quiet", no_argument, NULL, 'q' },
2473 { NULL, 0, NULL, 0 }
2481 while ((c = getopt_long(argc, argv, "ht:p:aq", options, NULL)) >= 0) {
2494 arg_property = optarg;
2496 /* If the user asked for a particular
2497 * property, show it to him, even if it is
2515 arg_session = false;
2519 arg_no_block = true;
2534 log_error("Unknown option code %c", c);
2542 static int halt_parse_argv(int argc, char *argv[]) {
2551 static const struct option options[] = {
2552 { "help", no_argument, NULL, ARG_HELP },
2553 { "halt", no_argument, NULL, ARG_HALT },
2554 { "poweroff", no_argument, NULL, 'p' },
2555 { "reboot", no_argument, NULL, ARG_REBOOT },
2556 { "force", no_argument, NULL, 'f' },
2557 { "wtmp-only", no_argument, NULL, 'w' },
2558 { "no-wtmp", no_argument, NULL, 'd' },
2559 { "no-sync", no_argument, NULL, 'n' },
2560 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2561 { NULL, 0, NULL, 0 }
2569 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
2570 if (runlevel == '0' || runlevel == '6')
2571 arg_immediate = true;
2573 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
2581 arg_action = ACTION_HALT;
2585 arg_action = ACTION_POWEROFF;
2589 arg_action = ACTION_REBOOT;
2593 arg_immediate = true;
2614 /* Compatibility nops */
2621 log_error("Unknown option code %c", c);
2626 if (optind < argc) {
2627 log_error("Too many arguments.");
2634 static int shutdown_parse_argv(int argc, char *argv[]) {
2641 static const struct option options[] = {
2642 { "help", no_argument, NULL, ARG_HELP },
2643 { "halt", no_argument, NULL, 'H' },
2644 { "poweroff", no_argument, NULL, 'P' },
2645 { "reboot", no_argument, NULL, 'r' },
2646 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2647 { NULL, 0, NULL, 0 }
2655 while ((c = getopt_long(argc, argv, "HPrhkt:a", options, NULL)) >= 0) {
2663 arg_action = ACTION_HALT;
2667 arg_action = ACTION_POWEROFF;
2671 arg_action = ACTION_REBOOT;
2675 if (arg_action != ACTION_HALT)
2676 arg_action = ACTION_POWEROFF;
2689 /* Compatibility nops */
2696 log_error("Unknown option code %c", c);
2701 if (argc > optind && !streq(argv[optind], "now"))
2702 log_warning("First argument '%s' isn't 'now'. Ignoring.", argv[optind]);
2704 /* We ignore the time argument */
2705 if (argc > optind + 1)
2706 arg_wall = argv + optind + 1;
2713 static int telinit_parse_argv(int argc, char *argv[]) {
2720 static const struct option options[] = {
2721 { "help", no_argument, NULL, ARG_HELP },
2722 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2723 { NULL, 0, NULL, 0 }
2726 static const struct {
2730 { '0', ACTION_POWEROFF },
2731 { '6', ACTION_REBOOT },
2732 { '1', ACTION_RESCUE },
2733 { '2', ACTION_RUNLEVEL2 },
2734 { '3', ACTION_RUNLEVEL3 },
2735 { '4', ACTION_RUNLEVEL4 },
2736 { '5', ACTION_RUNLEVEL5 },
2737 { 's', ACTION_RESCUE },
2738 { 'S', ACTION_RESCUE },
2739 { 'q', ACTION_RELOAD },
2740 { 'Q', ACTION_RELOAD },
2741 { 'u', ACTION_REEXEC },
2742 { 'U', ACTION_REEXEC }
2751 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
2766 log_error("Unknown option code %c", c);
2771 if (optind >= argc) {
2776 if (optind + 1 < argc) {
2777 log_error("Too many arguments.");
2781 if (strlen(argv[optind]) != 1) {
2782 log_error("Expected single character argument.");
2786 for (i = 0; i < ELEMENTSOF(table); i++)
2787 if (table[i].from == argv[optind][0])
2790 if (i >= ELEMENTSOF(table)) {
2791 log_error("Unknown command %s.", argv[optind]);
2795 arg_action = table[i].to;
2802 static int runlevel_parse_argv(int argc, char *argv[]) {
2808 static const struct option options[] = {
2809 { "help", no_argument, NULL, ARG_HELP },
2810 { NULL, 0, NULL, 0 }
2818 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
2829 log_error("Unknown option code %c", c);
2834 if (optind < argc) {
2835 log_error("Too many arguments.");
2842 static int parse_argv(int argc, char *argv[]) {
2846 if (program_invocation_short_name) {
2848 if (strstr(program_invocation_short_name, "halt")) {
2849 arg_action = ACTION_HALT;
2850 return halt_parse_argv(argc, argv);
2851 } else if (strstr(program_invocation_short_name, "poweroff")) {
2852 arg_action = ACTION_POWEROFF;
2853 return halt_parse_argv(argc, argv);
2854 } else if (strstr(program_invocation_short_name, "reboot")) {
2855 arg_action = ACTION_REBOOT;
2856 return halt_parse_argv(argc, argv);
2857 } else if (strstr(program_invocation_short_name, "shutdown")) {
2858 arg_action = ACTION_POWEROFF;
2859 return shutdown_parse_argv(argc, argv);
2860 } else if (strstr(program_invocation_short_name, "init")) {
2861 arg_action = ACTION_INVALID;
2862 return telinit_parse_argv(argc, argv);
2863 } else if (strstr(program_invocation_short_name, "runlevel")) {
2864 arg_action = ACTION_RUNLEVEL;
2865 return runlevel_parse_argv(argc, argv);
2869 arg_action = ACTION_SYSTEMCTL;
2870 return systemctl_parse_argv(argc, argv);
2873 static int action_to_runlevel(void) {
2875 static const char table[_ACTION_MAX] = {
2876 [ACTION_HALT] = '0',
2877 [ACTION_POWEROFF] = '0',
2878 [ACTION_REBOOT] = '6',
2879 [ACTION_RUNLEVEL2] = '2',
2880 [ACTION_RUNLEVEL3] = '3',
2881 [ACTION_RUNLEVEL4] = '4',
2882 [ACTION_RUNLEVEL5] = '5',
2883 [ACTION_RESCUE] = '1'
2886 assert(arg_action < _ACTION_MAX);
2888 return table[arg_action];
2891 static int talk_upstart(void) {
2892 DBusMessage *m = NULL, *reply = NULL;
2894 int previous, rl, r;
2896 env1_buf[] = "RUNLEVEL=X",
2897 env2_buf[] = "PREVLEVEL=X";
2898 char *env1 = env1_buf, *env2 = env2_buf;
2899 const char *emit = "runlevel";
2900 dbus_bool_t b_false = FALSE;
2901 DBusMessageIter iter, sub;
2902 DBusConnection *bus;
2904 dbus_error_init(&error);
2906 if (!(rl = action_to_runlevel()))
2909 if (utmp_get_runlevel(&previous, NULL) < 0)
2912 if (!(bus = dbus_connection_open("unix:abstract=/com/ubuntu/upstart", &error))) {
2913 if (dbus_error_has_name(&error, DBUS_ERROR_NO_SERVER)) {
2918 log_error("Failed to connect to Upstart bus: %s", error.message);
2923 if ((r = bus_check_peercred(bus)) < 0) {
2924 log_error("Failed to verify owner of bus.");
2928 if (!(m = dbus_message_new_method_call(
2929 "com.ubuntu.Upstart",
2930 "/com/ubuntu/Upstart",
2931 "com.ubuntu.Upstart0_6",
2934 log_error("Could not allocate message.");
2939 dbus_message_iter_init_append(m, &iter);
2941 env1_buf[sizeof(env1_buf)-2] = rl;
2942 env2_buf[sizeof(env2_buf)-2] = previous;
2944 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
2945 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
2946 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
2947 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
2948 !dbus_message_iter_close_container(&iter, &sub) ||
2949 !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
2950 log_error("Could not append arguments to message.");
2955 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2957 if (error_is_no_service(&error)) {
2962 log_error("Failed to issue method call: %s", error.message);
2971 dbus_message_unref(m);
2974 dbus_message_unref(reply);
2977 dbus_connection_unref(bus);
2979 dbus_error_free(&error);
2984 static int talk_initctl(void) {
2985 struct init_request request;
2989 if (!(rl = action_to_runlevel()))
2993 request.magic = INIT_MAGIC;
2994 request.sleeptime = 0;
2995 request.cmd = INIT_CMD_RUNLVL;
2996 request.runlevel = rl;
2998 if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
3000 if (errno == ENOENT)
3003 log_error("Failed to open "INIT_FIFO": %m");
3008 r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
3009 close_nointr_nofail(fd);
3012 log_error("Failed to write to "INIT_FIFO": %m");
3013 return errno ? -errno : -EIO;
3019 static int systemctl_main(DBusConnection *bus, int argc, char *argv[]) {
3021 static const struct {
3029 int (* const dispatch)(DBusConnection *bus, char **args, unsigned n);
3031 { "list-units", LESS, 1, list_units },
3032 { "list-jobs", EQUAL, 1, list_jobs },
3033 { "clear-jobs", EQUAL, 1, clear_jobs },
3034 { "load", MORE, 2, load_unit },
3035 { "cancel", MORE, 2, cancel_job },
3036 { "start", MORE, 2, start_unit },
3037 { "stop", MORE, 2, start_unit },
3038 { "reload", MORE, 2, start_unit },
3039 { "restart", MORE, 2, start_unit },
3040 { "isolate", EQUAL, 2, start_unit },
3041 { "check", MORE, 2, check_unit },
3042 { "show", MORE, 1, show },
3043 { "status", MORE, 2, show },
3044 { "monitor", EQUAL, 1, monitor },
3045 { "dump", EQUAL, 1, dump },
3046 { "snapshot", LESS, 2, snapshot },
3047 { "delete", MORE, 2, delete_snapshot },
3048 { "daemon-reload", EQUAL, 1, clear_jobs },
3049 { "daemon-reexec", EQUAL, 1, clear_jobs },
3050 { "daemon-exit", EQUAL, 1, clear_jobs },
3051 { "show-environment", EQUAL, 1, show_enviroment },
3052 { "set-environment", MORE, 2, set_environment },
3053 { "unset-environment", MORE, 2, set_environment },
3054 { "halt", EQUAL, 1, start_special },
3055 { "poweroff", EQUAL, 1, start_special },
3056 { "reboot", EQUAL, 1, start_special },
3057 { "default", EQUAL, 1, start_special },
3058 { "rescue", EQUAL, 1, start_special },
3059 { "emergency", EQUAL, 1, start_special }
3069 left = argc - optind;
3072 /* Special rule: no arguments means "list-units" */
3075 if (streq(argv[optind], "help")) {
3080 for (i = 0; i < ELEMENTSOF(verbs); i++)
3081 if (streq(argv[optind], verbs[i].verb))
3084 if (i >= ELEMENTSOF(verbs)) {
3085 log_error("Unknown operation %s", argv[optind]);
3090 switch (verbs[i].argc_cmp) {
3093 if (left != verbs[i].argc) {
3094 log_error("Invalid number of arguments.");
3101 if (left < verbs[i].argc) {
3102 log_error("Too few arguments.");
3109 if (left > verbs[i].argc) {
3110 log_error("Too many arguments.");
3117 assert_not_reached("Unknown comparison operator.");
3120 return verbs[i].dispatch(bus, argv + optind, left);
3123 static int reload_with_fallback(DBusConnection *bus) {
3127 /* First, try systemd via D-Bus. */
3128 if ((r = clear_jobs(bus, NULL, 0)) > 0)
3132 /* Nothing else worked, so let's try signals */
3133 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
3135 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
3136 log_error("kill() failed: %m");
3143 static int start_with_fallback(DBusConnection *bus) {
3146 warn_wall(arg_action);
3149 /* First, try systemd via D-Bus. */
3150 if ((r = start_unit(bus, NULL, 0)) > 0)
3153 /* Hmm, talking to systemd via D-Bus didn't work. Then
3154 * let's try to talk to Upstart via D-Bus. */
3155 if ((r = talk_upstart()) > 0)
3159 /* Nothing else worked, so let's try
3161 if ((r = talk_initctl()) != 0)
3164 log_error("Failed to talk to init daemon.");
3168 static int halt_main(DBusConnection *bus) {
3171 if (geteuid() != 0) {
3172 log_error("Must to be root.");
3176 if (!arg_dry && !arg_immediate)
3177 return start_with_fallback(bus);
3180 if ((r = utmp_put_shutdown(0)) < 0)
3181 log_warning("Failed to write utmp record: %s", strerror(-r));
3189 /* Make sure C-A-D is handled by the kernel from this
3191 reboot(RB_ENABLE_CAD);
3193 switch (arg_action) {
3196 log_info("Halting");
3197 reboot(RB_HALT_SYSTEM);
3200 case ACTION_POWEROFF:
3201 log_info("Powering off");
3202 reboot(RB_POWER_OFF);
3206 log_info("Rebooting");
3207 reboot(RB_AUTOBOOT);
3211 assert_not_reached("Unknown halt action.");
3214 /* We should never reach this. */
3218 static int runlevel_main(void) {
3219 int r, runlevel, previous;
3221 if ((r = utmp_get_runlevel(&runlevel, &previous)) < 0) {
3227 previous <= 0 ? 'N' : previous,
3228 runlevel <= 0 ? 'N' : runlevel);
3233 int main(int argc, char*argv[]) {
3235 DBusConnection *bus = NULL;
3238 dbus_error_init(&error);
3240 log_parse_environment();
3242 if ((r = parse_argv(argc, argv)) < 0)
3249 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
3250 * let's shortcut this */
3251 if (arg_action == ACTION_RUNLEVEL) {
3252 retval = runlevel_main() < 0;
3256 bus_connect(arg_session ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &bus, &private_bus, &error);
3258 switch (arg_action) {
3260 case ACTION_SYSTEMCTL: {
3263 log_error("Failed to get D-Bus connection: %s", error.message);
3267 retval = systemctl_main(bus, argc, argv) < 0;
3272 case ACTION_POWEROFF:
3274 retval = halt_main(bus) < 0;
3277 case ACTION_RUNLEVEL2:
3278 case ACTION_RUNLEVEL3:
3279 case ACTION_RUNLEVEL4:
3280 case ACTION_RUNLEVEL5:
3282 case ACTION_EMERGENCY:
3283 case ACTION_DEFAULT:
3284 retval = start_with_fallback(bus) < 0;
3289 retval = reload_with_fallback(bus) < 0;
3292 case ACTION_INVALID:
3293 case ACTION_RUNLEVEL:
3295 assert_not_reached("Unknown action");
3301 dbus_connection_unref(bus);
3303 dbus_error_free(&error);