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 int columns(void) {
114 static int parsed_columns = 0;
117 if (parsed_columns > 0)
118 return parsed_columns;
120 if ((e = getenv("COLUMNS")))
121 parsed_columns = atoi(e);
123 if (parsed_columns <= 0) {
127 if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) >= 0)
128 parsed_columns = ws.ws_col;
131 if (parsed_columns <= 0)
134 return parsed_columns;
137 static void warn_wall(enum action action) {
138 static const char *table[_ACTION_MAX] = {
139 [ACTION_HALT] = "The system is going down for system halt NOW!",
140 [ACTION_REBOOT] = "The system is going down for reboot NOW!",
141 [ACTION_POWEROFF] = "The system is going down for power-off NOW!",
142 [ACTION_RESCUE] = "The system is going down to rescue mode NOW!",
143 [ACTION_EMERGENCY] = "The system is going down to emergency mode NOW!"
152 if (!(p = strv_join(arg_wall, " "))) {
153 log_error("Failed to join strings.");
169 utmp_wall(table[action]);
172 static int list_units(DBusConnection *bus, char **args, unsigned n) {
173 DBusMessage *m = NULL, *reply = NULL;
176 DBusMessageIter iter, sub, sub2;
179 dbus_error_init(&error);
183 if (!(m = dbus_message_new_method_call(
184 "org.freedesktop.systemd1",
185 "/org/freedesktop/systemd1",
186 "org.freedesktop.systemd1.Manager",
188 log_error("Could not allocate message.");
192 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
193 log_error("Failed to issue method call: %s", error.message);
198 if (!dbus_message_iter_init(reply, &iter) ||
199 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
200 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
201 log_error("Failed to parse reply.");
206 dbus_message_iter_recurse(&iter, &sub);
208 printf("%-45s %-6s %-12s %-12s %-15s %s\n", "UNIT", "LOAD", "ACTIVE", "SUB", "JOB", "DESCRIPTION");
210 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
211 const char *id, *description, *load_state, *active_state, *sub_state, *unit_state, *job_type, *job_path, *dot;
214 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
215 log_error("Failed to parse reply.");
220 dbus_message_iter_recurse(&sub, &sub2);
222 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
223 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &description, true) < 0 ||
224 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &load_state, true) < 0 ||
225 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &active_state, true) < 0 ||
226 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &sub_state, true) < 0 ||
227 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_state, true) < 0 ||
228 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &job_id, true) < 0 ||
229 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &job_type, true) < 0 ||
230 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, false) < 0) {
231 log_error("Failed to parse reply.");
236 if ((!arg_type || ((dot = strrchr(id, '.')) &&
237 streq(dot+1, arg_type))) &&
238 (arg_all || !streq(active_state, "inactive"))) {
242 if (streq(active_state, "maintenance"))
243 fputs(ANSI_HIGHLIGHT_ON, stdout);
245 printf("%-45s %-6s %-12s %-12s%n", id, load_state, active_state, sub_state, &a);
248 printf(" %-15s%n", job_type, &b);
252 if (a + b + 2 < columns()) {
256 printf(" %.*s", columns() - a - b - 2, description);
259 if (streq(active_state, "maintenance"))
260 fputs(ANSI_HIGHLIGHT_OFF, stdout);
266 dbus_message_iter_next(&sub);
270 printf("\n%u units listed.\n", k);
272 printf("\n%u live units listed. Pass --all to see dead units, too.\n", k);
278 dbus_message_unref(m);
281 dbus_message_unref(reply);
283 dbus_error_free(&error);
288 static int list_jobs(DBusConnection *bus, char **args, unsigned n) {
289 DBusMessage *m = NULL, *reply = NULL;
292 DBusMessageIter iter, sub, sub2;
295 dbus_error_init(&error);
299 if (!(m = dbus_message_new_method_call(
300 "org.freedesktop.systemd1",
301 "/org/freedesktop/systemd1",
302 "org.freedesktop.systemd1.Manager",
304 log_error("Could not allocate message.");
308 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
309 log_error("Failed to issue method call: %s", error.message);
314 if (!dbus_message_iter_init(reply, &iter) ||
315 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
316 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
317 log_error("Failed to parse reply.");
322 dbus_message_iter_recurse(&iter, &sub);
324 printf("%4s %-45s %-17s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
326 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
327 const char *name, *type, *state, *job_path, *unit_path;
330 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
331 log_error("Failed to parse reply.");
336 dbus_message_iter_recurse(&sub, &sub2);
338 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &id, true) < 0 ||
339 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0 ||
340 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
341 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, true) < 0 ||
342 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, true) < 0 ||
343 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, false) < 0) {
344 log_error("Failed to parse reply.");
349 printf("%4u %-45s %-17s %-7s\n", id, name, type, state);
352 dbus_message_iter_next(&sub);
355 printf("\n%u jobs listed.\n", k);
360 dbus_message_unref(m);
363 dbus_message_unref(reply);
365 dbus_error_free(&error);
370 static int load_unit(DBusConnection *bus, char **args, unsigned n) {
371 DBusMessage *m = NULL, *reply = NULL;
376 dbus_error_init(&error);
381 for (i = 1; i < n; i++) {
383 if (!(m = dbus_message_new_method_call(
384 "org.freedesktop.systemd1",
385 "/org/freedesktop/systemd1",
386 "org.freedesktop.systemd1.Manager",
388 log_error("Could not allocate message.");
393 if (!dbus_message_append_args(m,
394 DBUS_TYPE_STRING, &args[i],
395 DBUS_TYPE_INVALID)) {
396 log_error("Could not append arguments to message.");
401 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
402 log_error("Failed to issue method call: %s", error.message);
407 dbus_message_unref(m);
408 dbus_message_unref(reply);
417 dbus_message_unref(m);
420 dbus_message_unref(reply);
422 dbus_error_free(&error);
427 static int cancel_job(DBusConnection *bus, char **args, unsigned n) {
428 DBusMessage *m = NULL, *reply = NULL;
433 dbus_error_init(&error);
438 for (i = 1; i < n; i++) {
442 if (!(m = dbus_message_new_method_call(
443 "org.freedesktop.systemd1",
444 "/org/freedesktop/systemd1",
445 "org.freedesktop.systemd1.Manager",
447 log_error("Could not allocate message.");
452 if ((r = safe_atou(args[i], &id)) < 0) {
453 log_error("Failed to parse job id: %s", strerror(-r));
457 assert_cc(sizeof(uint32_t) == sizeof(id));
458 if (!dbus_message_append_args(m,
459 DBUS_TYPE_UINT32, &id,
460 DBUS_TYPE_INVALID)) {
461 log_error("Could not append arguments to message.");
466 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
467 log_error("Failed to issue method call: %s", error.message);
472 if (!dbus_message_get_args(reply, &error,
473 DBUS_TYPE_OBJECT_PATH, &path,
474 DBUS_TYPE_INVALID)) {
475 log_error("Failed to parse reply: %s", error.message);
480 dbus_message_unref(m);
481 if (!(m = dbus_message_new_method_call(
482 "org.freedesktop.systemd1",
484 "org.freedesktop.systemd1.Job",
486 log_error("Could not allocate message.");
491 dbus_message_unref(reply);
492 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
493 log_error("Failed to issue method call: %s", error.message);
498 dbus_message_unref(m);
499 dbus_message_unref(reply);
507 dbus_message_unref(m);
510 dbus_message_unref(reply);
512 dbus_error_free(&error);
517 typedef struct WaitData {
522 static DBusHandlerResult wait_filter(DBusConnection *connection, DBusMessage *message, void *data) {
530 dbus_error_init(&error);
532 log_debug("Got D-Bus request: %s.%s() on %s",
533 dbus_message_get_interface(message),
534 dbus_message_get_member(message),
535 dbus_message_get_path(message));
537 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
538 log_error("Warning! D-Bus connection terminated.");
539 dbus_connection_close(connection);
541 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
544 dbus_bool_t success = true;
546 if (!dbus_message_get_args(message, &error,
547 DBUS_TYPE_UINT32, &id,
548 DBUS_TYPE_OBJECT_PATH, &path,
549 DBUS_TYPE_BOOLEAN, &success,
551 log_error("Failed to parse message: %s", error.message);
555 if ((p = set_remove(d->set, (char*) path)))
563 dbus_error_free(&error);
564 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
567 static int enable_wait_for_jobs(DBusConnection *bus) {
575 dbus_error_init(&error);
576 dbus_bus_add_match(bus,
578 "sender='org.freedesktop.systemd1',"
579 "interface='org.freedesktop.systemd1.Manager',"
580 "member='JobRemoved',"
581 "path='/org/freedesktop/systemd1'",
584 if (dbus_error_is_set(&error)) {
585 log_error("Failed to add match: %s", error.message);
586 dbus_error_free(&error);
590 /* This is slightly dirty, since we don't undo the match registrations. */
594 static int wait_for_jobs(DBusConnection *bus, Set *s) {
605 if (!dbus_connection_add_filter(bus, wait_filter, &d, NULL)) {
606 log_error("Failed to add filter.");
611 while (!set_isempty(s) &&
612 dbus_connection_read_write_dispatch(bus, -1))
615 if (!arg_quiet && d.failed)
616 log_error("Job failed, see logs for details.");
618 r = d.failed ? -EIO : 0;
621 /* This is slightly dirty, since we don't undo the filter registration. */
626 static int start_unit_one(
633 DBusMessage *m = NULL, *reply = NULL;
641 assert(arg_no_block || s);
643 dbus_error_init(&error);
645 if (!(m = dbus_message_new_method_call(
646 "org.freedesktop.systemd1",
647 "/org/freedesktop/systemd1",
648 "org.freedesktop.systemd1.Manager",
650 log_error("Could not allocate message.");
655 if (!dbus_message_append_args(m,
656 DBUS_TYPE_STRING, &name,
657 DBUS_TYPE_STRING, &mode,
658 DBUS_TYPE_INVALID)) {
659 log_error("Could not append arguments to message.");
664 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
666 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
667 /* There's always a fallback possible for
673 log_error("Failed to issue method call: %s", error.message);
682 if (!dbus_message_get_args(reply, &error,
683 DBUS_TYPE_OBJECT_PATH, &path,
684 DBUS_TYPE_INVALID)) {
685 log_error("Failed to parse reply: %s", error.message);
690 if (!(p = strdup(path))) {
691 log_error("Failed to duplicate path.");
696 if ((r = set_put(s, p)) < 0) {
698 log_error("Failed to add path to set.");
707 dbus_message_unref(m);
710 dbus_message_unref(reply);
712 dbus_error_free(&error);
717 static enum action verb_to_action(const char *verb) {
718 if (streq(verb, "halt"))
720 else if (streq(verb, "poweroff"))
721 return ACTION_POWEROFF;
722 else if (streq(verb, "reboot"))
723 return ACTION_REBOOT;
724 else if (streq(verb, "rescue"))
725 return ACTION_RESCUE;
726 else if (streq(verb, "emergency"))
727 return ACTION_EMERGENCY;
728 else if (streq(verb, "default"))
729 return ACTION_DEFAULT;
731 return ACTION_INVALID;
734 static int start_unit(DBusConnection *bus, char **args, unsigned n) {
736 static const char * const table[_ACTION_MAX] = {
737 [ACTION_HALT] = SPECIAL_HALT_TARGET,
738 [ACTION_POWEROFF] = SPECIAL_POWEROFF_TARGET,
739 [ACTION_REBOOT] = SPECIAL_REBOOT_TARGET,
740 [ACTION_RUNLEVEL2] = SPECIAL_RUNLEVEL2_TARGET,
741 [ACTION_RUNLEVEL3] = SPECIAL_RUNLEVEL3_TARGET,
742 [ACTION_RUNLEVEL4] = SPECIAL_RUNLEVEL4_TARGET,
743 [ACTION_RUNLEVEL5] = SPECIAL_RUNLEVEL5_TARGET,
744 [ACTION_RESCUE] = SPECIAL_RESCUE_TARGET,
745 [ACTION_EMERGENCY] = SPECIAL_EMERGENCY_SERVICE,
746 [ACTION_DEFAULT] = SPECIAL_DEFAULT_TARGET
751 const char *method, *mode, *one_name;
756 if (arg_action == ACTION_SYSTEMCTL) {
758 streq(args[0], "stop") ? "StopUnit" :
759 streq(args[0], "reload") ? "ReloadUnit" :
760 streq(args[0], "restart") ? "RestartUnit" :
764 (streq(args[0], "isolate") ||
765 streq(args[0], "rescue") ||
766 streq(args[0], "emergency")) ? "isolate" :
767 arg_replace ? "replace" :
770 one_name = table[verb_to_action(args[0])];
773 assert(arg_action < ELEMENTSOF(table));
774 assert(table[arg_action]);
776 method = "StartUnit";
778 mode = (arg_action == ACTION_EMERGENCY ||
779 arg_action == ACTION_RESCUE) ? "isolate" : "replace";
781 one_name = table[arg_action];
785 if ((r = enable_wait_for_jobs(bus)) < 0) {
786 log_error("Could not watch jobs: %s", strerror(-r));
790 if (!(s = set_new(string_hash_func, string_compare_func))) {
791 log_error("Failed to allocate set.");
800 if ((r = start_unit_one(bus, method, one_name, mode, s)) <= 0)
803 for (i = 1; i < n; i++)
804 if ((r = start_unit_one(bus, method, args[i], mode, s)) < 0)
809 r = wait_for_jobs(bus, s);
818 static int start_special(DBusConnection *bus, char **args, unsigned n) {
822 warn_wall(verb_to_action(args[0]));
824 return start_unit(bus, args, n);
827 static int check_unit(DBusConnection *bus, char **args, unsigned n) {
828 DBusMessage *m = NULL, *reply = NULL;
830 *interface = "org.freedesktop.systemd1.Unit",
831 *property = "ActiveState";
832 int r = -EADDRNOTAVAIL;
839 dbus_error_init(&error);
841 for (i = 1; i < n; i++) {
842 const char *path = NULL;
844 DBusMessageIter iter, sub;
846 if (!(m = dbus_message_new_method_call(
847 "org.freedesktop.systemd1",
848 "/org/freedesktop/systemd1",
849 "org.freedesktop.systemd1.Manager",
851 log_error("Could not allocate message.");
856 if (!dbus_message_append_args(m,
857 DBUS_TYPE_STRING, &args[i],
858 DBUS_TYPE_INVALID)) {
859 log_error("Could not append arguments to message.");
864 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
866 /* Hmm, cannot figure out anything about this unit... */
870 dbus_error_free(&error);
874 if (!dbus_message_get_args(reply, &error,
875 DBUS_TYPE_OBJECT_PATH, &path,
876 DBUS_TYPE_INVALID)) {
877 log_error("Failed to parse reply: %s", error.message);
882 dbus_message_unref(m);
883 if (!(m = dbus_message_new_method_call(
884 "org.freedesktop.systemd1",
886 "org.freedesktop.DBus.Properties",
888 log_error("Could not allocate message.");
893 if (!dbus_message_append_args(m,
894 DBUS_TYPE_STRING, &interface,
895 DBUS_TYPE_STRING, &property,
896 DBUS_TYPE_INVALID)) {
897 log_error("Could not append arguments to message.");
902 dbus_message_unref(reply);
903 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
904 log_error("Failed to issue method call: %s", error.message);
909 if (!dbus_message_iter_init(reply, &iter) ||
910 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
911 log_error("Failed to parse reply.");
916 dbus_message_iter_recurse(&iter, &sub);
918 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
919 log_error("Failed to parse reply.");
924 dbus_message_iter_get_basic(&sub, &state);
929 if (streq(state, "active") || startswith(state, "reloading"))
932 dbus_message_unref(m);
933 dbus_message_unref(reply);
939 dbus_message_unref(m);
942 dbus_message_unref(reply);
944 dbus_error_free(&error);
949 typedef struct UnitStatusInfo {
951 const char *load_state;
952 const char *active_state;
953 const char *sub_state;
955 const char *description;
957 const char *fragment_path;
958 const char *default_control_group;
963 const char *status_text;
966 usec_t start_timestamp;
967 usec_t exit_timestamp;
969 int exit_code, exit_status;
973 unsigned n_connections;
976 const char *sysfs_path;
978 /* Mount, Automount */
985 static void print_status_info(UnitStatusInfo *i) {
988 /* This shows pretty information about a unit. See
989 * print_property() for a low-level property printer */
991 printf("%s", strna(i->id));
993 if (i->description && !streq_ptr(i->id, i->description))
994 printf(" - %s", i->description);
998 if (i->fragment_path)
999 printf("\t Loaded: %s (%s)\n", strna(i->load_state), i->fragment_path);
1000 else if (streq_ptr(i->load_state, "failed"))
1001 printf("\t Loaded: " ANSI_HIGHLIGHT_ON "%s" ANSI_HIGHLIGHT_OFF "\n", strna(i->load_state));
1003 printf("\t Loaded: %s\n", strna(i->load_state));
1005 if (streq_ptr(i->active_state, "maintenance"))
1006 printf("\t Active: " ANSI_HIGHLIGHT_ON "%s (%s)" ANSI_HIGHLIGHT_OFF "\n",
1007 strna(i->active_state),
1008 strna(i->sub_state));
1010 printf("\t Active: %s (%s)\n",
1011 strna(i->active_state),
1012 strna(i->sub_state));
1015 printf("\t Device: %s\n", i->sysfs_path);
1017 printf("\t Where: %s\n", i->where);
1019 printf("\t What: %s\n", i->what);
1022 printf("\t Status: \"%s\"\n", i->status_text);
1024 if (i->id && endswith(i->id, ".socket"))
1025 printf("\tAccepted: %u; Connected: %u\n", i->n_accepted, i->n_connections);
1027 if (i->main_pid > 0 || i->control_pid > 0) {
1030 if (i->main_pid > 0) {
1031 printf(" Process: %u", (unsigned) i->main_pid);
1035 get_process_name(i->main_pid, &t);
1041 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
1043 if (i->exit_code == CLD_EXITED)
1044 printf("status=%i", i->exit_status);
1046 printf("signal=%s", strsignal(i->exit_status));
1051 if (i->main_pid > 0 && i->control_pid > 0)
1054 if (i->control_pid > 0) {
1057 printf(" Control: %u", (unsigned) i->control_pid);
1059 get_process_name(i->control_pid, &t);
1069 if (i->default_control_group) {
1072 printf("\t CGroup: %s\n", i->default_control_group);
1074 if ((c = columns()) > 18)
1079 show_cgroup(i->default_control_group, "\t\t ", c);
1083 static int status_property(const char *name, DBusMessageIter *iter, UnitStatusInfo *i) {
1085 switch (dbus_message_iter_get_arg_type(iter)) {
1087 case DBUS_TYPE_STRING: {
1090 dbus_message_iter_get_basic(iter, &s);
1093 if (streq(name, "Id"))
1095 else if (streq(name, "LoadState"))
1097 else if (streq(name, "ActiveState"))
1098 i->active_state = s;
1099 else if (streq(name, "SubState"))
1101 else if (streq(name, "Description"))
1103 else if (streq(name, "FragmentPath"))
1104 i->fragment_path = s;
1105 else if (streq(name, "DefaultControlGroup"))
1106 i->default_control_group = s;
1107 else if (streq(name, "StatusText"))
1109 else if (streq(name, "SysFSPath"))
1111 else if (streq(name, "Where"))
1113 else if (streq(name, "What"))
1120 case DBUS_TYPE_UINT32: {
1123 dbus_message_iter_get_basic(iter, &u);
1125 if (streq(name, "MainPID")) {
1127 i->main_pid = (pid_t) u;
1130 } else if (streq(name, "ControlPID"))
1131 i->control_pid = (pid_t) u;
1132 else if (streq(name, "ExecMainPID")) {
1134 i->main_pid = (pid_t) u;
1135 } else if (streq(name, "NAccepted"))
1137 else if (streq(name, "NConnections"))
1138 i->n_connections = u;
1143 case DBUS_TYPE_INT32: {
1146 dbus_message_iter_get_basic(iter, &j);
1148 if (streq(name, "ExecMainCode"))
1149 i->exit_code = (int) j;
1150 else if (streq(name, "ExecMainStatus"))
1151 i->exit_status = (int) j;
1156 case DBUS_TYPE_UINT64: {
1159 dbus_message_iter_get_basic(iter, &u);
1161 if (streq(name, "ExecMainStartTimestamp"))
1162 i->start_timestamp = (usec_t) u;
1163 else if (streq(name, "ExecMainExitTimestamp"))
1164 i->exit_timestamp = (usec_t) u;
1173 static int print_property(const char *name, DBusMessageIter *iter) {
1177 /* This is a low-level property printer, see
1178 * print_status_info() for the nicer output */
1180 if (arg_property && !streq(name, arg_property))
1183 switch (dbus_message_iter_get_arg_type(iter)) {
1185 case DBUS_TYPE_STRING: {
1187 dbus_message_iter_get_basic(iter, &s);
1189 if (arg_all || s[0])
1190 printf("%s=%s\n", name, s);
1195 case DBUS_TYPE_BOOLEAN: {
1197 dbus_message_iter_get_basic(iter, &b);
1198 printf("%s=%s\n", name, yes_no(b));
1203 case DBUS_TYPE_UINT64: {
1205 dbus_message_iter_get_basic(iter, &u);
1207 /* Yes, heuristics! But we can change this check
1208 * should it turn out to not be sufficient */
1210 if (strstr(name, "Timestamp")) {
1211 char timestamp[FORMAT_TIMESTAMP_MAX], *t;
1213 if ((t = format_timestamp(timestamp, sizeof(timestamp), u)) || arg_all)
1214 printf("%s=%s\n", name, strempty(t));
1215 } else if (strstr(name, "USec")) {
1216 char timespan[FORMAT_TIMESPAN_MAX];
1218 printf("%s=%s\n", name, format_timespan(timespan, sizeof(timespan), u));
1220 printf("%s=%llu\n", name, (unsigned long long) u);
1225 case DBUS_TYPE_UINT32: {
1227 dbus_message_iter_get_basic(iter, &u);
1229 if (strstr(name, "UMask") || strstr(name, "Mode"))
1230 printf("%s=%04o\n", name, u);
1232 printf("%s=%u\n", name, (unsigned) u);
1237 case DBUS_TYPE_INT32: {
1239 dbus_message_iter_get_basic(iter, &i);
1241 printf("%s=%i\n", name, (int) i);
1245 case DBUS_TYPE_STRUCT: {
1246 DBusMessageIter sub;
1247 dbus_message_iter_recurse(iter, &sub);
1249 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "Job")) {
1252 dbus_message_iter_get_basic(&sub, &u);
1255 printf("%s=%u\n", name, (unsigned) u);
1257 printf("%s=\n", name);
1260 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Unit")) {
1263 dbus_message_iter_get_basic(&sub, &s);
1265 if (arg_all || s[0])
1266 printf("%s=%s\n", name, s);
1274 case DBUS_TYPE_ARRAY:
1276 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
1277 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_STRING);
1290 dbus_message_iter_get_basic(&sub, &s);
1291 printf("%s%s", space ? " " : "", s);
1294 dbus_message_iter_next(&sub);
1301 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_BYTE) {
1302 DBusMessageIter sub;
1304 dbus_message_iter_recurse(iter, &sub);
1307 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1308 printf("%s=", name);
1310 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1313 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_BYTE);
1314 dbus_message_iter_get_basic(&sub, &u);
1317 dbus_message_iter_next(&sub);
1324 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Paths")) {
1325 DBusMessageIter sub, sub2;
1327 dbus_message_iter_recurse(iter, &sub);
1329 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1330 const char *type, *path;
1332 dbus_message_iter_recurse(&sub, &sub2);
1334 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
1335 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0)
1336 printf("%s=%s\n", type, path);
1338 dbus_message_iter_next(&sub);
1342 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Timers")) {
1343 DBusMessageIter sub, sub2;
1345 dbus_message_iter_recurse(iter, &sub);
1347 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1349 uint64_t value, next_elapse;
1351 dbus_message_iter_recurse(&sub, &sub2);
1353 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &base, true) >= 0 &&
1354 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &value, true) >= 0 &&
1355 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &next_elapse, false) >= 0) {
1356 char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX];
1358 printf("%s={ value=%s ; next_elapse=%s }\n",
1360 format_timespan(timespan1, sizeof(timespan1), value),
1361 format_timespan(timespan2, sizeof(timespan2), next_elapse));
1364 dbus_message_iter_next(&sub);
1368 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && startswith(name, "Exec")) {
1370 DBusMessageIter sub, sub2, sub3;
1372 dbus_message_iter_recurse(iter, &sub);
1374 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1376 uint64_t start_time, exit_time;
1378 int32_t code, status;
1380 dbus_message_iter_recurse(&sub, &sub2);
1382 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0)
1385 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_ARRAY ||
1386 dbus_message_iter_get_element_type(&sub2) != DBUS_TYPE_STRING)
1389 printf("%s={ path=%s ; argv[]=", name, path);
1391 dbus_message_iter_recurse(&sub2, &sub3);
1393 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1396 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1397 dbus_message_iter_get_basic(&sub3, &s);
1399 dbus_message_iter_next(&sub3);
1402 if (dbus_message_iter_next(&sub2) &&
1403 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_time, true) >= 0 &&
1404 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_time, true) >= 0 &&
1405 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, true) >= 0 &&
1406 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &code, true) >= 0 &&
1407 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &status, false) >= 0) {
1409 char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
1411 printf("; start=%s ; stop=%s ; pid=%u ; code=%s ; status=%i/%s",
1412 strna(format_timestamp(timestamp1, sizeof(timestamp1), start_time)),
1413 strna(format_timestamp(timestamp2, sizeof(timestamp2), exit_time)),
1415 sigchld_code_to_string(code),
1417 strempty(code == CLD_EXITED ? NULL : strsignal(status)));
1422 dbus_message_iter_next(&sub);
1432 printf("%s=[unprintable]\n", name);
1437 static int show_one(DBusConnection *bus, const char *path, bool show_properties, bool *new_line) {
1438 DBusMessage *m = NULL, *reply = NULL;
1439 const char *interface = "";
1442 DBusMessageIter iter, sub, sub2, sub3;
1443 UnitStatusInfo info;
1450 dbus_error_init(&error);
1452 if (!(m = dbus_message_new_method_call(
1453 "org.freedesktop.systemd1",
1455 "org.freedesktop.DBus.Properties",
1457 log_error("Could not allocate message.");
1462 if (!dbus_message_append_args(m,
1463 DBUS_TYPE_STRING, &interface,
1464 DBUS_TYPE_INVALID)) {
1465 log_error("Could not append arguments to message.");
1470 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1471 log_error("Failed to issue method call: %s", error.message);
1476 if (!dbus_message_iter_init(reply, &iter) ||
1477 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
1478 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
1479 log_error("Failed to parse reply.");
1484 dbus_message_iter_recurse(&iter, &sub);
1491 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1494 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
1495 log_error("Failed to parse reply.");
1500 dbus_message_iter_recurse(&sub, &sub2);
1502 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
1503 log_error("Failed to parse reply.");
1508 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
1509 log_error("Failed to parse reply.");
1514 dbus_message_iter_recurse(&sub2, &sub3);
1516 if (show_properties)
1517 r = print_property(name, &sub3);
1519 r = status_property(name, &sub3, &info);
1522 log_error("Failed to parse reply.");
1527 dbus_message_iter_next(&sub);
1530 if (!show_properties)
1531 print_status_info(&info);
1537 dbus_message_unref(m);
1540 dbus_message_unref(reply);
1542 dbus_error_free(&error);
1547 static int show(DBusConnection *bus, char **args, unsigned n) {
1548 DBusMessage *m = NULL, *reply = NULL;
1552 bool show_properties, new_line = false;
1557 dbus_error_init(&error);
1559 show_properties = !streq(args[0], "status");
1561 if (show_properties && n <= 1) {
1562 /* If not argument is specified inspect the manager
1565 r = show_one(bus, "/org/freedesktop/systemd1", show_properties, &new_line);
1569 for (i = 1; i < n; i++) {
1570 const char *path = NULL;
1573 if (!show_properties || safe_atou32(args[i], &id) < 0) {
1575 if (!(m = dbus_message_new_method_call(
1576 "org.freedesktop.systemd1",
1577 "/org/freedesktop/systemd1",
1578 "org.freedesktop.systemd1.Manager",
1580 log_error("Could not allocate message.");
1585 if (!dbus_message_append_args(m,
1586 DBUS_TYPE_STRING, &args[i],
1587 DBUS_TYPE_INVALID)) {
1588 log_error("Could not append arguments to message.");
1593 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1595 if (!dbus_error_has_name(&error, DBUS_ERROR_ACCESS_DENIED)) {
1596 log_error("Failed to issue method call: %s", error.message);
1601 dbus_error_free(&error);
1603 dbus_message_unref(m);
1604 if (!(m = dbus_message_new_method_call(
1605 "org.freedesktop.systemd1",
1606 "/org/freedesktop/systemd1",
1607 "org.freedesktop.systemd1.Manager",
1609 log_error("Could not allocate message.");
1614 if (!dbus_message_append_args(m,
1615 DBUS_TYPE_STRING, &args[i],
1616 DBUS_TYPE_INVALID)) {
1617 log_error("Could not append arguments to message.");
1622 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1623 log_error("Failed to issue method call: %s", error.message);
1631 if (!(m = dbus_message_new_method_call(
1632 "org.freedesktop.systemd1",
1633 "/org/freedesktop/systemd1",
1634 "org.freedesktop.systemd1.Manager",
1636 log_error("Could not allocate message.");
1641 if (!dbus_message_append_args(m,
1642 DBUS_TYPE_UINT32, &id,
1643 DBUS_TYPE_INVALID)) {
1644 log_error("Could not append arguments to message.");
1649 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1650 log_error("Failed to issue method call: %s", error.message);
1656 if (!dbus_message_get_args(reply, &error,
1657 DBUS_TYPE_OBJECT_PATH, &path,
1658 DBUS_TYPE_INVALID)) {
1659 log_error("Failed to parse reply: %s", error.message);
1664 if ((r = show_one(bus, path, show_properties, &new_line)) < 0)
1667 dbus_message_unref(m);
1668 dbus_message_unref(reply);
1676 dbus_message_unref(m);
1679 dbus_message_unref(reply);
1681 dbus_error_free(&error);
1686 static DBusHandlerResult monitor_filter(DBusConnection *connection, DBusMessage *message, void *data) {
1688 DBusMessage *m = NULL, *reply = NULL;
1693 dbus_error_init(&error);
1695 log_debug("Got D-Bus request: %s.%s() on %s",
1696 dbus_message_get_interface(message),
1697 dbus_message_get_member(message),
1698 dbus_message_get_path(message));
1700 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
1701 log_error("Warning! D-Bus connection terminated.");
1702 dbus_connection_close(connection);
1704 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitNew") ||
1705 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitRemoved")) {
1706 const char *id, *path;
1708 if (!dbus_message_get_args(message, &error,
1709 DBUS_TYPE_STRING, &id,
1710 DBUS_TYPE_OBJECT_PATH, &path,
1712 log_error("Failed to parse message: %s", error.message);
1713 else if (streq(dbus_message_get_member(message), "UnitNew"))
1714 printf("Unit %s added.\n", id);
1716 printf("Unit %s removed.\n", id);
1718 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobNew") ||
1719 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
1723 if (!dbus_message_get_args(message, &error,
1724 DBUS_TYPE_UINT32, &id,
1725 DBUS_TYPE_OBJECT_PATH, &path,
1727 log_error("Failed to parse message: %s", error.message);
1728 else if (streq(dbus_message_get_member(message), "JobNew"))
1729 printf("Job %u added.\n", id);
1731 printf("Job %u removed.\n", id);
1734 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Unit", "Changed") ||
1735 dbus_message_is_signal(message, "org.freedesktop.systemd1.Job", "Changed")) {
1737 const char *path, *interface, *property = "Id";
1738 DBusMessageIter iter, sub;
1740 path = dbus_message_get_path(message);
1741 interface = dbus_message_get_interface(message);
1743 if (!(m = dbus_message_new_method_call(
1744 "org.freedesktop.systemd1",
1746 "org.freedesktop.DBus.Properties",
1748 log_error("Could not allocate message.");
1752 if (!dbus_message_append_args(m,
1753 DBUS_TYPE_STRING, &interface,
1754 DBUS_TYPE_STRING, &property,
1755 DBUS_TYPE_INVALID)) {
1756 log_error("Could not append arguments to message.");
1760 if (!(reply = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) {
1761 log_error("Failed to issue method call: %s", error.message);
1765 if (!dbus_message_iter_init(reply, &iter) ||
1766 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1767 log_error("Failed to parse reply.");
1771 dbus_message_iter_recurse(&iter, &sub);
1773 if (streq(interface, "org.freedesktop.systemd1.Unit")) {
1776 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1777 log_error("Failed to parse reply.");
1781 dbus_message_iter_get_basic(&sub, &id);
1782 printf("Unit %s changed.\n", id);
1786 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT32) {
1787 log_error("Failed to parse reply.");
1791 dbus_message_iter_get_basic(&sub, &id);
1792 printf("Job %u changed.\n", id);
1798 dbus_message_unref(m);
1801 dbus_message_unref(reply);
1803 dbus_error_free(&error);
1804 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1808 dbus_message_unref(m);
1811 dbus_message_unref(reply);
1813 dbus_error_free(&error);
1814 return DBUS_HANDLER_RESULT_NEED_MEMORY;
1817 static int monitor(DBusConnection *bus, char **args, unsigned n) {
1818 DBusMessage *m = NULL, *reply = NULL;
1822 dbus_error_init(&error);
1825 dbus_bus_add_match(bus,
1827 "sender='org.freedesktop.systemd1',"
1828 "interface='org.freedesktop.systemd1.Manager',"
1829 "path='/org/freedesktop/systemd1'",
1832 if (dbus_error_is_set(&error)) {
1833 log_error("Failed to add match: %s", error.message);
1838 dbus_bus_add_match(bus,
1840 "sender='org.freedesktop.systemd1',"
1841 "interface='org.freedesktop.systemd1.Unit',"
1845 if (dbus_error_is_set(&error)) {
1846 log_error("Failed to add match: %s", error.message);
1851 dbus_bus_add_match(bus,
1853 "sender='org.freedesktop.systemd1',"
1854 "interface='org.freedesktop.systemd1.Job',"
1858 if (dbus_error_is_set(&error)) {
1859 log_error("Failed to add match: %s", error.message);
1865 if (!dbus_connection_add_filter(bus, monitor_filter, NULL, NULL)) {
1866 log_error("Failed to add filter.");
1871 if (!(m = dbus_message_new_method_call(
1872 "org.freedesktop.systemd1",
1873 "/org/freedesktop/systemd1",
1874 "org.freedesktop.systemd1.Manager",
1876 log_error("Could not allocate message.");
1881 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1882 log_error("Failed to issue method call: %s", error.message);
1887 while (dbus_connection_read_write_dispatch(bus, -1))
1894 /* This is slightly dirty, since we don't undo the filter or the matches. */
1897 dbus_message_unref(m);
1900 dbus_message_unref(reply);
1902 dbus_error_free(&error);
1907 static int dump(DBusConnection *bus, char **args, unsigned n) {
1908 DBusMessage *m = NULL, *reply = NULL;
1913 dbus_error_init(&error);
1915 if (!(m = dbus_message_new_method_call(
1916 "org.freedesktop.systemd1",
1917 "/org/freedesktop/systemd1",
1918 "org.freedesktop.systemd1.Manager",
1920 log_error("Could not allocate message.");
1924 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1925 log_error("Failed to issue method call: %s", error.message);
1930 if (!dbus_message_get_args(reply, &error,
1931 DBUS_TYPE_STRING, &text,
1932 DBUS_TYPE_INVALID)) {
1933 log_error("Failed to parse reply: %s", error.message);
1938 fputs(text, stdout);
1944 dbus_message_unref(m);
1947 dbus_message_unref(reply);
1949 dbus_error_free(&error);
1954 static int snapshot(DBusConnection *bus, char **args, unsigned n) {
1955 DBusMessage *m = NULL, *reply = NULL;
1958 const char *name = "", *path, *id;
1959 dbus_bool_t cleanup = FALSE;
1960 DBusMessageIter iter, sub;
1962 *interface = "org.freedesktop.systemd1.Unit",
1965 dbus_error_init(&error);
1967 if (!(m = dbus_message_new_method_call(
1968 "org.freedesktop.systemd1",
1969 "/org/freedesktop/systemd1",
1970 "org.freedesktop.systemd1.Manager",
1971 "CreateSnapshot"))) {
1972 log_error("Could not allocate message.");
1979 if (!dbus_message_append_args(m,
1980 DBUS_TYPE_STRING, &name,
1981 DBUS_TYPE_BOOLEAN, &cleanup,
1982 DBUS_TYPE_INVALID)) {
1983 log_error("Could not append arguments to message.");
1988 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1989 log_error("Failed to issue method call: %s", error.message);
1994 if (!dbus_message_get_args(reply, &error,
1995 DBUS_TYPE_OBJECT_PATH, &path,
1996 DBUS_TYPE_INVALID)) {
1997 log_error("Failed to parse reply: %s", error.message);
2002 dbus_message_unref(m);
2003 if (!(m = dbus_message_new_method_call(
2004 "org.freedesktop.systemd1",
2006 "org.freedesktop.DBus.Properties",
2008 log_error("Could not allocate message.");
2012 if (!dbus_message_append_args(m,
2013 DBUS_TYPE_STRING, &interface,
2014 DBUS_TYPE_STRING, &property,
2015 DBUS_TYPE_INVALID)) {
2016 log_error("Could not append arguments to message.");
2021 dbus_message_unref(reply);
2022 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2023 log_error("Failed to issue method call: %s", error.message);
2028 if (!dbus_message_iter_init(reply, &iter) ||
2029 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2030 log_error("Failed to parse reply.");
2035 dbus_message_iter_recurse(&iter, &sub);
2037 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2038 log_error("Failed to parse reply.");
2043 dbus_message_iter_get_basic(&sub, &id);
2051 dbus_message_unref(m);
2054 dbus_message_unref(reply);
2056 dbus_error_free(&error);
2061 static int delete_snapshot(DBusConnection *bus, char **args, unsigned n) {
2062 DBusMessage *m = NULL, *reply = NULL;
2070 dbus_error_init(&error);
2072 for (i = 1; i < n; i++) {
2073 const char *path = NULL;
2075 if (!(m = dbus_message_new_method_call(
2076 "org.freedesktop.systemd1",
2077 "/org/freedesktop/systemd1",
2078 "org.freedesktop.systemd1.Manager",
2080 log_error("Could not allocate message.");
2085 if (!dbus_message_append_args(m,
2086 DBUS_TYPE_STRING, &args[i],
2087 DBUS_TYPE_INVALID)) {
2088 log_error("Could not append arguments to message.");
2093 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2094 log_error("Failed to issue method call: %s", error.message);
2099 if (!dbus_message_get_args(reply, &error,
2100 DBUS_TYPE_OBJECT_PATH, &path,
2101 DBUS_TYPE_INVALID)) {
2102 log_error("Failed to parse reply: %s", error.message);
2107 dbus_message_unref(m);
2108 if (!(m = dbus_message_new_method_call(
2109 "org.freedesktop.systemd1",
2111 "org.freedesktop.systemd1.Snapshot",
2113 log_error("Could not allocate message.");
2118 dbus_message_unref(reply);
2119 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2120 log_error("Failed to issue method call: %s", error.message);
2125 dbus_message_unref(m);
2126 dbus_message_unref(reply);
2134 dbus_message_unref(m);
2137 dbus_message_unref(reply);
2139 dbus_error_free(&error);
2144 static int clear_jobs(DBusConnection *bus, char **args, unsigned n) {
2145 DBusMessage *m = NULL, *reply = NULL;
2150 dbus_error_init(&error);
2152 if (arg_action == ACTION_RELOAD)
2154 else if (arg_action == ACTION_REEXEC)
2155 method = "Reexecute";
2157 assert(arg_action == ACTION_SYSTEMCTL);
2160 streq(args[0], "clear-jobs") ? "ClearJobs" :
2161 streq(args[0], "daemon-reload") ? "Reload" :
2162 streq(args[0], "daemon-reexec") ? "Reexecute" :
2166 if (!(m = dbus_message_new_method_call(
2167 "org.freedesktop.systemd1",
2168 "/org/freedesktop/systemd1",
2169 "org.freedesktop.systemd1.Manager",
2171 log_error("Could not allocate message.");
2175 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2177 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
2178 /* There's always a fallback possible for
2179 * legacy actions. */
2184 log_error("Failed to issue method call: %s", error.message);
2193 dbus_message_unref(m);
2196 dbus_message_unref(reply);
2198 dbus_error_free(&error);
2203 static int show_enviroment(DBusConnection *bus, char **args, unsigned n) {
2204 DBusMessage *m = NULL, *reply = NULL;
2206 DBusMessageIter iter, sub, sub2;
2209 *interface = "org.freedesktop.systemd1.Manager",
2210 *property = "Environment";
2212 dbus_error_init(&error);
2214 if (!(m = dbus_message_new_method_call(
2215 "org.freedesktop.systemd1",
2216 "/org/freedesktop/systemd1",
2217 "org.freedesktop.DBus.Properties",
2219 log_error("Could not allocate message.");
2223 if (!dbus_message_append_args(m,
2224 DBUS_TYPE_STRING, &interface,
2225 DBUS_TYPE_STRING, &property,
2226 DBUS_TYPE_INVALID)) {
2227 log_error("Could not append arguments to message.");
2232 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2233 log_error("Failed to issue method call: %s", error.message);
2238 if (!dbus_message_iter_init(reply, &iter) ||
2239 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2240 log_error("Failed to parse reply.");
2245 dbus_message_iter_recurse(&iter, &sub);
2247 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
2248 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
2249 log_error("Failed to parse reply.");
2254 dbus_message_iter_recurse(&sub, &sub2);
2256 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
2259 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
2260 log_error("Failed to parse reply.");
2265 dbus_message_iter_get_basic(&sub2, &text);
2266 printf("%s\n", text);
2268 dbus_message_iter_next(&sub2);
2275 dbus_message_unref(m);
2278 dbus_message_unref(reply);
2280 dbus_error_free(&error);
2285 static int set_environment(DBusConnection *bus, char **args, unsigned n) {
2286 DBusMessage *m = NULL, *reply = NULL;
2290 DBusMessageIter iter, sub;
2293 dbus_error_init(&error);
2295 method = streq(args[0], "set-environment")
2297 : "UnsetEnvironment";
2299 if (!(m = dbus_message_new_method_call(
2300 "org.freedesktop.systemd1",
2301 "/org/freedesktop/systemd1",
2302 "org.freedesktop.systemd1.Manager",
2305 log_error("Could not allocate message.");
2309 dbus_message_iter_init_append(m, &iter);
2311 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
2312 log_error("Could not append arguments to message.");
2317 for (i = 1; i < n; i++)
2318 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &args[i])) {
2319 log_error("Could not append arguments to message.");
2324 if (!dbus_message_iter_close_container(&iter, &sub)) {
2325 log_error("Could not append arguments to message.");
2330 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2331 log_error("Failed to issue method call: %s", error.message);
2340 dbus_message_unref(m);
2343 dbus_message_unref(reply);
2345 dbus_error_free(&error);
2350 static int systemctl_help(void) {
2352 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
2353 "Send control commands to the systemd manager.\n\n"
2354 " -h --help Show this help\n"
2355 " -t --type=TYPE List only units of a particular type\n"
2356 " -p --property=NAME Show only properties by this name\n"
2357 " -a --all Show all units/properties, including dead/empty ones\n"
2358 " --replace When installing a new job, replace existing conflicting ones\n"
2359 " --system Connect to system bus\n"
2360 " --session Connect to session bus\n"
2361 " -q --quiet Suppress output\n"
2362 " --no-block Do not wait until operation finished\n"
2363 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
2365 " list-units List units\n"
2366 " start [NAME...] Start one or more units\n"
2367 " stop [NAME...] Stop one or more units\n"
2368 " restart [NAME...] Restart one or more units\n"
2369 " reload [NAME...] Reload one or more units\n"
2370 " isolate [NAME] Start one unit and stop all others\n"
2371 " check [NAME...] Check whether any of the passed units are active\n"
2372 " status [NAME...] Show status of one or more units\n"
2373 " show [NAME...|JOB...] Show properties of one or more units/jobs/manager\n"
2374 " load [NAME...] Load one or more units\n"
2375 " list-jobs List jobs\n"
2376 " cancel [JOB...] Cancel one or more jobs\n"
2377 " clear-jobs Cancel all jobs\n"
2378 " monitor Monitor unit/job changes\n"
2379 " dump Dump server status\n"
2380 " snapshot [NAME] Create a snapshot\n"
2381 " delete [NAME...] Remove one or more snapshots\n"
2382 " daemon-reload Reload systemd manager configuration\n"
2383 " daemon-reexec Reexecute systemd manager\n"
2384 " daemon-exit Ask the systemd manager to quit\n"
2385 " show-environment Dump environment\n"
2386 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
2387 " unset-environment [NAME...] Unset one or more environment variables\n"
2388 " halt Shut down and halt the system\n"
2389 " poweroff Shut down and power-off the system\n"
2390 " reboot Shut down and reboot the system\n"
2391 " default Enter default mode\n"
2392 " rescue Enter rescue mode\n"
2393 " emergency Enter emergency mode\n",
2394 program_invocation_short_name);
2399 static int halt_help(void) {
2401 printf("%s [OPTIONS...]\n\n"
2402 "%s the system.\n\n"
2403 " --help Show this help\n"
2404 " --halt Halt the machine\n"
2405 " -p --poweroff Switch off the machine\n"
2406 " --reboot Reboot the machine\n"
2407 " -f --force Force immediate halt/power-off/reboot\n"
2408 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
2409 " -d --no-wtmp Don't write wtmp record\n"
2410 " -n --no-sync Don't sync before halt/power-off/reboot\n"
2411 " --no-wall Don't send wall message before halt/power-off/reboot\n",
2412 program_invocation_short_name,
2413 arg_action == ACTION_REBOOT ? "Reboot" :
2414 arg_action == ACTION_POWEROFF ? "Power off" :
2420 static int shutdown_help(void) {
2422 printf("%s [OPTIONS...] [now] [WALL...]\n\n"
2423 "Shut down the system.\n\n"
2424 " --help Show this help\n"
2425 " -H --halt Halt the machine\n"
2426 " -P --poweroff Power-off the machine\n"
2427 " -r --reboot Reboot the machine\n"
2428 " -h Equivalent to --poweroff, overriden by --halt\n"
2429 " -k Don't halt/power-off/reboot, just send warnings\n"
2430 " --no-wall Don't send wall message before halt/power-off/reboot\n",
2431 program_invocation_short_name);
2436 static int telinit_help(void) {
2438 printf("%s [OPTIONS...] {COMMAND}\n\n"
2439 "Send control commands to the init daemon.\n\n"
2440 " --help Show this help\n"
2441 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
2443 " 0 Power-off the machine\n"
2444 " 6 Reboot the machine\n"
2445 " 2, 3, 4, 5 Start runlevelX.target unit\n"
2446 " 1, s, S Enter rescue mode\n"
2447 " q, Q Reload init daemon configuration\n"
2448 " u, U Reexecute init daemon\n",
2449 program_invocation_short_name);
2454 static int runlevel_help(void) {
2456 printf("%s [OPTIONS...]\n\n"
2457 "Prints the previous and current runlevel of the init system.\n\n"
2458 " --help Show this help\n",
2459 program_invocation_short_name);
2464 static int systemctl_parse_argv(int argc, char *argv[]) {
2467 ARG_REPLACE = 0x100,
2474 static const struct option options[] = {
2475 { "help", no_argument, NULL, 'h' },
2476 { "type", required_argument, NULL, 't' },
2477 { "property", required_argument, NULL, 'p' },
2478 { "all", no_argument, NULL, 'a' },
2479 { "replace", no_argument, NULL, ARG_REPLACE },
2480 { "session", no_argument, NULL, ARG_SESSION },
2481 { "system", no_argument, NULL, ARG_SYSTEM },
2482 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
2483 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2484 { "quiet", no_argument, NULL, 'q' },
2485 { NULL, 0, NULL, 0 }
2493 while ((c = getopt_long(argc, argv, "ht:p:aq", options, NULL)) >= 0) {
2506 arg_property = optarg;
2508 /* If the user asked for a particular
2509 * property, show it to him, even if it is
2527 arg_session = false;
2531 arg_no_block = true;
2546 log_error("Unknown option code %c", c);
2554 static int halt_parse_argv(int argc, char *argv[]) {
2563 static const struct option options[] = {
2564 { "help", no_argument, NULL, ARG_HELP },
2565 { "halt", no_argument, NULL, ARG_HALT },
2566 { "poweroff", no_argument, NULL, 'p' },
2567 { "reboot", no_argument, NULL, ARG_REBOOT },
2568 { "force", no_argument, NULL, 'f' },
2569 { "wtmp-only", no_argument, NULL, 'w' },
2570 { "no-wtmp", no_argument, NULL, 'd' },
2571 { "no-sync", no_argument, NULL, 'n' },
2572 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2573 { NULL, 0, NULL, 0 }
2581 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
2582 if (runlevel == '0' || runlevel == '6')
2583 arg_immediate = true;
2585 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
2593 arg_action = ACTION_HALT;
2597 arg_action = ACTION_POWEROFF;
2601 arg_action = ACTION_REBOOT;
2605 arg_immediate = true;
2626 /* Compatibility nops */
2633 log_error("Unknown option code %c", c);
2638 if (optind < argc) {
2639 log_error("Too many arguments.");
2646 static int shutdown_parse_argv(int argc, char *argv[]) {
2653 static const struct option options[] = {
2654 { "help", no_argument, NULL, ARG_HELP },
2655 { "halt", no_argument, NULL, 'H' },
2656 { "poweroff", no_argument, NULL, 'P' },
2657 { "reboot", no_argument, NULL, 'r' },
2658 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2659 { NULL, 0, NULL, 0 }
2667 while ((c = getopt_long(argc, argv, "HPrhkt:a", options, NULL)) >= 0) {
2675 arg_action = ACTION_HALT;
2679 arg_action = ACTION_POWEROFF;
2683 arg_action = ACTION_REBOOT;
2687 if (arg_action != ACTION_HALT)
2688 arg_action = ACTION_POWEROFF;
2701 /* Compatibility nops */
2708 log_error("Unknown option code %c", c);
2713 if (argc > optind && !streq(argv[optind], "now"))
2714 log_warning("First argument '%s' isn't 'now'. Ignoring.", argv[optind]);
2716 /* We ignore the time argument */
2717 if (argc > optind + 1)
2718 arg_wall = argv + optind + 1;
2725 static int telinit_parse_argv(int argc, char *argv[]) {
2732 static const struct option options[] = {
2733 { "help", no_argument, NULL, ARG_HELP },
2734 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2735 { NULL, 0, NULL, 0 }
2738 static const struct {
2742 { '0', ACTION_POWEROFF },
2743 { '6', ACTION_REBOOT },
2744 { '1', ACTION_RESCUE },
2745 { '2', ACTION_RUNLEVEL2 },
2746 { '3', ACTION_RUNLEVEL3 },
2747 { '4', ACTION_RUNLEVEL4 },
2748 { '5', ACTION_RUNLEVEL5 },
2749 { 's', ACTION_RESCUE },
2750 { 'S', ACTION_RESCUE },
2751 { 'q', ACTION_RELOAD },
2752 { 'Q', ACTION_RELOAD },
2753 { 'u', ACTION_REEXEC },
2754 { 'U', ACTION_REEXEC }
2763 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
2778 log_error("Unknown option code %c", c);
2783 if (optind >= argc) {
2788 if (optind + 1 < argc) {
2789 log_error("Too many arguments.");
2793 if (strlen(argv[optind]) != 1) {
2794 log_error("Expected single character argument.");
2798 for (i = 0; i < ELEMENTSOF(table); i++)
2799 if (table[i].from == argv[optind][0])
2802 if (i >= ELEMENTSOF(table)) {
2803 log_error("Unknown command %s.", argv[optind]);
2807 arg_action = table[i].to;
2814 static int runlevel_parse_argv(int argc, char *argv[]) {
2820 static const struct option options[] = {
2821 { "help", no_argument, NULL, ARG_HELP },
2822 { NULL, 0, NULL, 0 }
2830 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
2841 log_error("Unknown option code %c", c);
2846 if (optind < argc) {
2847 log_error("Too many arguments.");
2854 static int parse_argv(int argc, char *argv[]) {
2858 if (program_invocation_short_name) {
2860 if (strstr(program_invocation_short_name, "halt")) {
2861 arg_action = ACTION_HALT;
2862 return halt_parse_argv(argc, argv);
2863 } else if (strstr(program_invocation_short_name, "poweroff")) {
2864 arg_action = ACTION_POWEROFF;
2865 return halt_parse_argv(argc, argv);
2866 } else if (strstr(program_invocation_short_name, "reboot")) {
2867 arg_action = ACTION_REBOOT;
2868 return halt_parse_argv(argc, argv);
2869 } else if (strstr(program_invocation_short_name, "shutdown")) {
2870 arg_action = ACTION_POWEROFF;
2871 return shutdown_parse_argv(argc, argv);
2872 } else if (strstr(program_invocation_short_name, "init")) {
2873 arg_action = ACTION_INVALID;
2874 return telinit_parse_argv(argc, argv);
2875 } else if (strstr(program_invocation_short_name, "runlevel")) {
2876 arg_action = ACTION_RUNLEVEL;
2877 return runlevel_parse_argv(argc, argv);
2881 arg_action = ACTION_SYSTEMCTL;
2882 return systemctl_parse_argv(argc, argv);
2885 static int action_to_runlevel(void) {
2887 static const char table[_ACTION_MAX] = {
2888 [ACTION_HALT] = '0',
2889 [ACTION_POWEROFF] = '0',
2890 [ACTION_REBOOT] = '6',
2891 [ACTION_RUNLEVEL2] = '2',
2892 [ACTION_RUNLEVEL3] = '3',
2893 [ACTION_RUNLEVEL4] = '4',
2894 [ACTION_RUNLEVEL5] = '5',
2895 [ACTION_RESCUE] = '1'
2898 assert(arg_action < _ACTION_MAX);
2900 return table[arg_action];
2903 static int talk_upstart(void) {
2904 DBusMessage *m = NULL, *reply = NULL;
2906 int previous, rl, r;
2908 env1_buf[] = "RUNLEVEL=X",
2909 env2_buf[] = "PREVLEVEL=X";
2910 char *env1 = env1_buf, *env2 = env2_buf;
2911 const char *emit = "runlevel";
2912 dbus_bool_t b_false = FALSE;
2913 DBusMessageIter iter, sub;
2914 DBusConnection *bus;
2916 dbus_error_init(&error);
2918 if (!(rl = action_to_runlevel()))
2921 if (utmp_get_runlevel(&previous, NULL) < 0)
2924 if (!(bus = dbus_connection_open("unix:abstract=/com/ubuntu/upstart", &error))) {
2925 if (dbus_error_has_name(&error, DBUS_ERROR_NO_SERVER)) {
2930 log_error("Failed to connect to Upstart bus: %s", error.message);
2935 if ((r = bus_check_peercred(bus)) < 0) {
2936 log_error("Failed to verify owner of bus.");
2940 if (!(m = dbus_message_new_method_call(
2941 "com.ubuntu.Upstart",
2942 "/com/ubuntu/Upstart",
2943 "com.ubuntu.Upstart0_6",
2946 log_error("Could not allocate message.");
2951 dbus_message_iter_init_append(m, &iter);
2953 env1_buf[sizeof(env1_buf)-2] = rl;
2954 env2_buf[sizeof(env2_buf)-2] = previous;
2956 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
2957 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
2958 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
2959 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
2960 !dbus_message_iter_close_container(&iter, &sub) ||
2961 !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
2962 log_error("Could not append arguments to message.");
2967 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2969 if (error_is_no_service(&error)) {
2974 log_error("Failed to issue method call: %s", error.message);
2983 dbus_message_unref(m);
2986 dbus_message_unref(reply);
2989 dbus_connection_unref(bus);
2991 dbus_error_free(&error);
2996 static int talk_initctl(void) {
2997 struct init_request request;
3001 if (!(rl = action_to_runlevel()))
3005 request.magic = INIT_MAGIC;
3006 request.sleeptime = 0;
3007 request.cmd = INIT_CMD_RUNLVL;
3008 request.runlevel = rl;
3010 if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
3012 if (errno == ENOENT)
3015 log_error("Failed to open "INIT_FIFO": %m");
3020 r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
3021 close_nointr_nofail(fd);
3024 log_error("Failed to write to "INIT_FIFO": %m");
3025 return errno ? -errno : -EIO;
3031 static int systemctl_main(DBusConnection *bus, int argc, char *argv[]) {
3033 static const struct {
3041 int (* const dispatch)(DBusConnection *bus, char **args, unsigned n);
3043 { "list-units", LESS, 1, list_units },
3044 { "list-jobs", EQUAL, 1, list_jobs },
3045 { "clear-jobs", EQUAL, 1, clear_jobs },
3046 { "load", MORE, 2, load_unit },
3047 { "cancel", MORE, 2, cancel_job },
3048 { "start", MORE, 2, start_unit },
3049 { "stop", MORE, 2, start_unit },
3050 { "reload", MORE, 2, start_unit },
3051 { "restart", MORE, 2, start_unit },
3052 { "isolate", EQUAL, 2, start_unit },
3053 { "check", MORE, 2, check_unit },
3054 { "show", MORE, 1, show },
3055 { "status", MORE, 2, show },
3056 { "monitor", EQUAL, 1, monitor },
3057 { "dump", EQUAL, 1, dump },
3058 { "snapshot", LESS, 2, snapshot },
3059 { "delete", MORE, 2, delete_snapshot },
3060 { "daemon-reload", EQUAL, 1, clear_jobs },
3061 { "daemon-reexec", EQUAL, 1, clear_jobs },
3062 { "daemon-exit", EQUAL, 1, clear_jobs },
3063 { "show-environment", EQUAL, 1, show_enviroment },
3064 { "set-environment", MORE, 2, set_environment },
3065 { "unset-environment", MORE, 2, set_environment },
3066 { "halt", EQUAL, 1, start_special },
3067 { "poweroff", EQUAL, 1, start_special },
3068 { "reboot", EQUAL, 1, start_special },
3069 { "default", EQUAL, 1, start_special },
3070 { "rescue", EQUAL, 1, start_special },
3071 { "emergency", EQUAL, 1, start_special },
3081 left = argc - optind;
3084 /* Special rule: no arguments means "list-units" */
3087 if (streq(argv[optind], "help")) {
3092 for (i = 0; i < ELEMENTSOF(verbs); i++)
3093 if (streq(argv[optind], verbs[i].verb))
3096 if (i >= ELEMENTSOF(verbs)) {
3097 log_error("Unknown operation %s", argv[optind]);
3102 switch (verbs[i].argc_cmp) {
3105 if (left != verbs[i].argc) {
3106 log_error("Invalid number of arguments.");
3113 if (left < verbs[i].argc) {
3114 log_error("Too few arguments.");
3121 if (left > verbs[i].argc) {
3122 log_error("Too many arguments.");
3129 assert_not_reached("Unknown comparison operator.");
3132 return verbs[i].dispatch(bus, argv + optind, left);
3135 static int reload_with_fallback(DBusConnection *bus) {
3139 /* First, try systemd via D-Bus. */
3140 if ((r = clear_jobs(bus, NULL, 0)) > 0)
3144 /* Nothing else worked, so let's try signals */
3145 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
3147 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
3148 log_error("kill() failed: %m");
3155 static int start_with_fallback(DBusConnection *bus) {
3158 warn_wall(arg_action);
3161 /* First, try systemd via D-Bus. */
3162 if ((r = start_unit(bus, NULL, 0)) > 0)
3165 /* Hmm, talking to systemd via D-Bus didn't work. Then
3166 * let's try to talk to Upstart via D-Bus. */
3167 if ((r = talk_upstart()) > 0)
3171 /* Nothing else worked, so let's try
3173 if ((r = talk_initctl()) != 0)
3176 log_error("Failed to talk to init daemon.");
3180 static int halt_main(DBusConnection *bus) {
3184 return start_with_fallback(bus);
3187 if ((r = utmp_put_shutdown(0)) < 0)
3188 log_warning("Failed to write utmp record: %s", strerror(-r));
3196 /* Make sure C-A-D is handled by the kernel from this
3198 reboot(RB_ENABLE_CAD);
3200 switch (arg_action) {
3203 log_info("Halting");
3204 reboot(RB_HALT_SYSTEM);
3207 case ACTION_POWEROFF:
3208 log_info("Powering off");
3209 reboot(RB_POWER_OFF);
3213 log_info("Rebooting");
3214 reboot(RB_AUTOBOOT);
3218 assert_not_reached("Unknown halt action.");
3221 /* We should never reach this. */
3225 static int runlevel_main(void) {
3226 int r, runlevel, previous;
3228 if ((r = utmp_get_runlevel(&runlevel, &previous)) < 0) {
3234 previous <= 0 ? 'N' : previous,
3235 runlevel <= 0 ? 'N' : runlevel);
3240 int main(int argc, char*argv[]) {
3242 DBusConnection *bus = NULL;
3245 dbus_error_init(&error);
3247 log_parse_environment();
3249 if ((r = parse_argv(argc, argv)) < 0)
3256 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
3257 * let's shortcut this */
3258 if (arg_action == ACTION_RUNLEVEL) {
3259 retval = runlevel_main() < 0;
3263 bus_connect(arg_session ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &bus, &private_bus, &error);
3265 switch (arg_action) {
3267 case ACTION_SYSTEMCTL: {
3270 log_error("Failed to get D-Bus connection: %s", error.message);
3274 retval = systemctl_main(bus, argc, argv) < 0;
3279 case ACTION_POWEROFF:
3281 retval = halt_main(bus) < 0;
3284 case ACTION_RUNLEVEL2:
3285 case ACTION_RUNLEVEL3:
3286 case ACTION_RUNLEVEL4:
3287 case ACTION_RUNLEVEL5:
3289 case ACTION_EMERGENCY:
3290 case ACTION_DEFAULT:
3291 retval = start_with_fallback(bus) < 0;
3296 retval = reload_with_fallback(bus) < 0;
3299 case ACTION_INVALID:
3300 case ACTION_RUNLEVEL:
3302 assert_not_reached("Unknown action");
3308 dbus_connection_unref(bus);
3310 dbus_error_free(&error);