1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/reboot.h>
28 #include <sys/ioctl.h>
32 #include <sys/socket.h>
34 #include <dbus/dbus.h>
40 #include "utmp-wtmp.h"
44 #include "dbus-common.h"
46 static const char *arg_type = NULL;
47 static const char *arg_property = NULL;
48 static bool arg_all = false;
49 static bool arg_replace = false;
50 static bool arg_session = false;
51 static bool arg_no_block = false;
52 static bool arg_immediate = false;
53 static bool arg_no_wtmp = false;
54 static bool arg_no_sync = false;
55 static bool arg_no_wall = false;
56 static bool arg_dry = false;
57 static bool arg_quiet = false;
58 static char **arg_wall = NULL;
76 } arg_action = ACTION_SYSTEMCTL;
78 static bool private_bus = false;
80 static bool error_is_no_service(DBusError *error) {
84 if (!dbus_error_is_set(error))
87 if (dbus_error_has_name(error, DBUS_ERROR_NAME_HAS_NO_OWNER))
90 if (dbus_error_has_name(error, DBUS_ERROR_SERVICE_UNKNOWN))
93 return startswith(error->name, "org.freedesktop.DBus.Error.Spawn.");
96 static int bus_iter_get_basic_and_next(DBusMessageIter *iter, int type, void *data, bool next) {
101 if (dbus_message_iter_get_arg_type(iter) != type)
104 dbus_message_iter_get_basic(iter, data);
106 if (!dbus_message_iter_next(iter) != !next)
112 static int columns(void) {
113 static int parsed_columns = 0;
116 if (parsed_columns > 0)
117 return parsed_columns;
119 if ((e = getenv("COLUMNS")))
120 parsed_columns = atoi(e);
122 if (parsed_columns <= 0) {
126 if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) >= 0)
127 parsed_columns = ws.ws_col;
130 if (parsed_columns <= 0)
133 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 printf("%-45s %-6s %-12s %-12s%n", id, load_state, active_state, sub_state, &a);
245 printf(" %-15s%n", job_type, &b);
249 if (a + b + 2 < columns()) {
253 printf("%.*s", columns() - a - b - 2, description);
260 dbus_message_iter_next(&sub);
264 printf("\n%u units listed.\n", k);
266 printf("\n%u live units listed. Pass --all to see dead units, too.\n", k);
272 dbus_message_unref(m);
275 dbus_message_unref(reply);
277 dbus_error_free(&error);
282 static int list_jobs(DBusConnection *bus, char **args, unsigned n) {
283 DBusMessage *m = NULL, *reply = NULL;
286 DBusMessageIter iter, sub, sub2;
289 dbus_error_init(&error);
293 if (!(m = dbus_message_new_method_call(
294 "org.freedesktop.systemd1",
295 "/org/freedesktop/systemd1",
296 "org.freedesktop.systemd1.Manager",
298 log_error("Could not allocate message.");
302 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
303 log_error("Failed to issue method call: %s", error.message);
308 if (!dbus_message_iter_init(reply, &iter) ||
309 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
310 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
311 log_error("Failed to parse reply.");
316 dbus_message_iter_recurse(&iter, &sub);
318 printf("%4s %-45s %-17s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
320 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
321 const char *name, *type, *state, *job_path, *unit_path;
324 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
325 log_error("Failed to parse reply.");
330 dbus_message_iter_recurse(&sub, &sub2);
332 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &id, true) < 0 ||
333 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0 ||
334 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
335 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, true) < 0 ||
336 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, true) < 0 ||
337 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, false) < 0) {
338 log_error("Failed to parse reply.");
343 printf("%4u %-45s %-17s %-7s\n", id, name, type, state);
346 dbus_message_iter_next(&sub);
349 printf("\n%u jobs listed.\n", k);
354 dbus_message_unref(m);
357 dbus_message_unref(reply);
359 dbus_error_free(&error);
364 static int load_unit(DBusConnection *bus, char **args, unsigned n) {
365 DBusMessage *m = NULL, *reply = NULL;
370 dbus_error_init(&error);
375 for (i = 1; i < n; i++) {
377 if (!(m = dbus_message_new_method_call(
378 "org.freedesktop.systemd1",
379 "/org/freedesktop/systemd1",
380 "org.freedesktop.systemd1.Manager",
382 log_error("Could not allocate message.");
387 if (!dbus_message_append_args(m,
388 DBUS_TYPE_STRING, &args[i],
389 DBUS_TYPE_INVALID)) {
390 log_error("Could not append arguments to message.");
395 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
396 log_error("Failed to issue method call: %s", error.message);
401 dbus_message_unref(m);
402 dbus_message_unref(reply);
411 dbus_message_unref(m);
414 dbus_message_unref(reply);
416 dbus_error_free(&error);
421 static int cancel_job(DBusConnection *bus, char **args, unsigned n) {
422 DBusMessage *m = NULL, *reply = NULL;
427 dbus_error_init(&error);
432 for (i = 1; i < n; i++) {
436 if (!(m = dbus_message_new_method_call(
437 "org.freedesktop.systemd1",
438 "/org/freedesktop/systemd1",
439 "org.freedesktop.systemd1.Manager",
441 log_error("Could not allocate message.");
446 if ((r = safe_atou(args[i], &id)) < 0) {
447 log_error("Failed to parse job id: %s", strerror(-r));
451 assert_cc(sizeof(uint32_t) == sizeof(id));
452 if (!dbus_message_append_args(m,
453 DBUS_TYPE_UINT32, &id,
454 DBUS_TYPE_INVALID)) {
455 log_error("Could not append arguments to message.");
460 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
461 log_error("Failed to issue method call: %s", error.message);
466 if (!dbus_message_get_args(reply, &error,
467 DBUS_TYPE_OBJECT_PATH, &path,
468 DBUS_TYPE_INVALID)) {
469 log_error("Failed to parse reply: %s", error.message);
474 dbus_message_unref(m);
475 if (!(m = dbus_message_new_method_call(
476 "org.freedesktop.systemd1",
478 "org.freedesktop.systemd1.Job",
480 log_error("Could not allocate message.");
485 dbus_message_unref(reply);
486 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
487 log_error("Failed to issue method call: %s", error.message);
492 dbus_message_unref(m);
493 dbus_message_unref(reply);
501 dbus_message_unref(m);
504 dbus_message_unref(reply);
506 dbus_error_free(&error);
511 typedef struct WaitData {
516 static DBusHandlerResult wait_filter(DBusConnection *connection, DBusMessage *message, void *data) {
524 dbus_error_init(&error);
526 /* log_debug("Got D-Bus request: %s.%s() on %s", */
527 /* dbus_message_get_interface(message), */
528 /* dbus_message_get_member(message), */
529 /* dbus_message_get_path(message)); */
531 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
532 log_error("Warning! D-Bus connection terminated.");
533 dbus_connection_close(connection);
535 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
538 dbus_bool_t success = true;
540 if (!dbus_message_get_args(message, &error,
541 DBUS_TYPE_UINT32, &id,
542 DBUS_TYPE_OBJECT_PATH, &path,
543 DBUS_TYPE_BOOLEAN, &success,
545 log_error("Failed to parse message: %s", error.message);
549 if ((p = set_remove(d->set, (char*) path)))
557 dbus_error_free(&error);
558 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
561 static int enable_wait_for_jobs(DBusConnection *bus) {
569 dbus_error_init(&error);
570 dbus_bus_add_match(bus,
572 "sender='org.freedesktop.systemd1',"
573 "interface='org.freedesktop.systemd1.Manager',"
574 "member='JobRemoved',"
575 "path='/org/freedesktop/systemd1'",
578 if (dbus_error_is_set(&error)) {
579 log_error("Failed to add match: %s", error.message);
580 dbus_error_free(&error);
584 /* This is slightly dirty, since we don't undo the match registrations. */
588 static int wait_for_jobs(DBusConnection *bus, Set *s) {
599 if (!dbus_connection_add_filter(bus, wait_filter, &d, NULL)) {
600 log_error("Failed to add filter.");
605 while (!set_isempty(s) &&
606 dbus_connection_read_write_dispatch(bus, -1))
609 if (!arg_quiet && d.failed)
610 log_error("Job failed, see logs for details.");
612 r = d.failed ? -EIO : 0;
615 /* This is slightly dirty, since we don't undo the filter registration. */
620 static int start_unit_one(
627 DBusMessage *m = NULL, *reply = NULL;
635 assert(arg_no_block || s);
637 dbus_error_init(&error);
639 if (!(m = dbus_message_new_method_call(
640 "org.freedesktop.systemd1",
641 "/org/freedesktop/systemd1",
642 "org.freedesktop.systemd1.Manager",
644 log_error("Could not allocate message.");
649 if (!dbus_message_append_args(m,
650 DBUS_TYPE_STRING, &name,
651 DBUS_TYPE_STRING, &mode,
652 DBUS_TYPE_INVALID)) {
653 log_error("Could not append arguments to message.");
658 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
660 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
661 /* There's always a fallback possible for
667 log_error("Failed to issue method call: %s", error.message);
676 if (!dbus_message_get_args(reply, &error,
677 DBUS_TYPE_OBJECT_PATH, &path,
678 DBUS_TYPE_INVALID)) {
679 log_error("Failed to parse reply: %s", error.message);
684 if (!(p = strdup(path))) {
685 log_error("Failed to duplicate path.");
690 if ((r = set_put(s, p)) < 0) {
692 log_error("Failed to add path to set.");
701 dbus_message_unref(m);
704 dbus_message_unref(reply);
706 dbus_error_free(&error);
711 static enum action verb_to_action(const char *verb) {
712 if (streq(verb, "halt"))
714 else if (streq(verb, "poweroff"))
715 return ACTION_POWEROFF;
716 else if (streq(verb, "reboot"))
717 return ACTION_REBOOT;
718 else if (streq(verb, "rescue"))
719 return ACTION_RESCUE;
720 else if (streq(verb, "emergency"))
721 return ACTION_EMERGENCY;
722 else if (streq(verb, "default"))
723 return ACTION_DEFAULT;
725 return ACTION_INVALID;
728 static int start_unit(DBusConnection *bus, char **args, unsigned n) {
730 static const char * const table[_ACTION_MAX] = {
731 [ACTION_HALT] = SPECIAL_HALT_TARGET,
732 [ACTION_POWEROFF] = SPECIAL_POWEROFF_TARGET,
733 [ACTION_REBOOT] = SPECIAL_REBOOT_TARGET,
734 [ACTION_RUNLEVEL2] = SPECIAL_RUNLEVEL2_TARGET,
735 [ACTION_RUNLEVEL3] = SPECIAL_RUNLEVEL3_TARGET,
736 [ACTION_RUNLEVEL4] = SPECIAL_RUNLEVEL4_TARGET,
737 [ACTION_RUNLEVEL5] = SPECIAL_RUNLEVEL5_TARGET,
738 [ACTION_RESCUE] = SPECIAL_RESCUE_TARGET,
739 [ACTION_EMERGENCY] = SPECIAL_EMERGENCY_SERVICE,
740 [ACTION_DEFAULT] = SPECIAL_DEFAULT_TARGET
745 const char *method, *mode, *one_name;
750 if (arg_action == ACTION_SYSTEMCTL) {
752 streq(args[0], "stop") ? "StopUnit" :
753 streq(args[0], "reload") ? "ReloadUnit" :
754 streq(args[0], "restart") ? "RestartUnit" :
758 (streq(args[0], "isolate") ||
759 streq(args[0], "rescue") ||
760 streq(args[0], "emergency")) ? "isolate" :
761 arg_replace ? "replace" :
764 one_name = table[verb_to_action(args[0])];
767 assert(arg_action < ELEMENTSOF(table));
768 assert(table[arg_action]);
770 method = "StartUnit";
772 mode = (arg_action == ACTION_EMERGENCY ||
773 arg_action == ACTION_RESCUE) ? "isolate" : "replace";
775 one_name = table[arg_action];
779 if ((r = enable_wait_for_jobs(bus)) < 0) {
780 log_error("Could not watch jobs: %s", strerror(-r));
784 if (!(s = set_new(string_hash_func, string_compare_func))) {
785 log_error("Failed to allocate set.");
794 if ((r = start_unit_one(bus, method, one_name, mode, s)) <= 0)
797 for (i = 1; i < n; i++)
798 if ((r = start_unit_one(bus, method, args[i], mode, s)) < 0)
803 r = wait_for_jobs(bus, s);
812 static int start_special(DBusConnection *bus, char **args, unsigned n) {
816 warn_wall(verb_to_action(args[0]));
818 return start_unit(bus, args, n);
821 static int check_unit(DBusConnection *bus, char **args, unsigned n) {
822 DBusMessage *m = NULL, *reply = NULL;
824 *interface = "org.freedesktop.systemd1.Unit",
825 *property = "ActiveState";
826 int r = -EADDRNOTAVAIL;
833 dbus_error_init(&error);
835 for (i = 1; i < n; i++) {
836 const char *path = NULL;
838 DBusMessageIter iter, sub;
840 if (!(m = dbus_message_new_method_call(
841 "org.freedesktop.systemd1",
842 "/org/freedesktop/systemd1",
843 "org.freedesktop.systemd1.Manager",
845 log_error("Could not allocate message.");
850 if (!dbus_message_append_args(m,
851 DBUS_TYPE_STRING, &args[i],
852 DBUS_TYPE_INVALID)) {
853 log_error("Could not append arguments to message.");
858 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
860 /* Hmm, cannot figure out anything about this unit... */
864 dbus_error_free(&error);
868 if (!dbus_message_get_args(reply, &error,
869 DBUS_TYPE_OBJECT_PATH, &path,
870 DBUS_TYPE_INVALID)) {
871 log_error("Failed to parse reply: %s", error.message);
876 dbus_message_unref(m);
877 if (!(m = dbus_message_new_method_call(
878 "org.freedesktop.systemd1",
880 "org.freedesktop.DBus.Properties",
882 log_error("Could not allocate message.");
887 if (!dbus_message_append_args(m,
888 DBUS_TYPE_STRING, &interface,
889 DBUS_TYPE_STRING, &property,
890 DBUS_TYPE_INVALID)) {
891 log_error("Could not append arguments to message.");
896 dbus_message_unref(reply);
897 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
898 log_error("Failed to issue method call: %s", error.message);
903 if (!dbus_message_iter_init(reply, &iter) ||
904 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
905 log_error("Failed to parse reply.");
910 dbus_message_iter_recurse(&iter, &sub);
912 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
913 log_error("Failed to parse reply.");
918 dbus_message_iter_get_basic(&sub, &state);
923 if (streq(state, "active") || startswith(state, "reloading"))
926 dbus_message_unref(m);
927 dbus_message_unref(reply);
933 dbus_message_unref(m);
936 dbus_message_unref(reply);
938 dbus_error_free(&error);
943 static void show_cgroup(const char *name) {
948 if (!startswith(name, "name=systemd:"))
951 if (asprintf(&fn, "/cgroup/systemd/%s/tasks", name + 13) < 0)
963 if (fscanf(f, "%lu", &ul) != 1)
971 get_process_cmdline(last, 60, &t);
972 printf("\t\t \342\224\234 %lu %s\n", (unsigned long) last, strna(t));
975 printf("\t\t \342\224\202\n");
982 get_process_cmdline(last, 60, &t);
983 printf("\t\t \342\224\224 %lu %s\n", (unsigned long) last, strna(t));
990 typedef struct UnitStatusInfo {
992 const char *load_state;
993 const char *active_state;
994 const char *sub_state;
996 const char *description;
998 const char *fragment_path;
999 const char *default_control_group;
1004 const char *status_text;
1007 usec_t start_timestamp;
1008 usec_t exit_timestamp;
1010 int exit_code, exit_status;
1013 unsigned n_accepted;
1014 unsigned n_connections;
1017 const char *sysfs_path;
1019 /* Mount, Automount */
1026 static void print_status_info(UnitStatusInfo *i) {
1029 /* This shows pretty information about a unit. See
1030 * print_property() for a low-level property printer */
1032 printf("%s", strna(i->id));
1034 if (i->description && !streq_ptr(i->id, i->description))
1035 printf(" - %s", i->description);
1039 if (i->fragment_path)
1040 printf("\t Loaded: %s (%s)\n", strna(i->load_state), i->fragment_path);
1041 else if (streq_ptr(i->load_state, "failed"))
1042 printf("\t Loaded: " ANSI_HIGHLIGHT_ON "%s" ANSI_HIGHLIGHT_OFF "\n", strna(i->load_state));
1044 printf("\t Loaded: %s\n", strna(i->load_state));
1046 if (streq_ptr(i->active_state, "maintenance"))
1047 printf("\t Active: " ANSI_HIGHLIGHT_ON "%s (%s)" ANSI_HIGHLIGHT_OFF "\n",
1048 strna(i->active_state),
1049 strna(i->sub_state));
1051 printf("\t Active: %s (%s)\n",
1052 strna(i->active_state),
1053 strna(i->sub_state));
1056 printf("\t Device: %s\n", i->sysfs_path);
1058 printf("\t Where: %s\n", i->where);
1060 printf("\t What: %s\n", i->what);
1063 printf("\t Status: \"%s\"\n", i->status_text);
1065 if (i->id && endswith(i->id, ".socket"))
1066 printf("\tAccepted: %u; Connected: %u\n", i->n_accepted, i->n_connections);
1068 if (i->main_pid > 0 || i->control_pid > 0) {
1071 if (i->main_pid > 0) {
1072 printf(" Process: %u", (unsigned) i->main_pid);
1076 get_process_name(i->main_pid, &t);
1082 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
1084 if (i->exit_code == CLD_EXITED)
1085 printf("status=%i", i->exit_status);
1087 printf("signal=%s", strsignal(i->exit_status));
1092 if (i->main_pid > 0 && i->control_pid > 0)
1095 if (i->control_pid > 0) {
1098 printf(" Control: %u", (unsigned) i->control_pid);
1100 get_process_name(i->control_pid, &t);
1110 if (i->default_control_group) {
1111 printf("\t CGroup: %s\n", i->default_control_group);
1112 show_cgroup(i->default_control_group);
1116 static int status_property(const char *name, DBusMessageIter *iter, UnitStatusInfo *i) {
1118 switch (dbus_message_iter_get_arg_type(iter)) {
1120 case DBUS_TYPE_STRING: {
1123 dbus_message_iter_get_basic(iter, &s);
1126 if (streq(name, "Id"))
1128 else if (streq(name, "LoadState"))
1130 else if (streq(name, "ActiveState"))
1131 i->active_state = s;
1132 else if (streq(name, "SubState"))
1134 else if (streq(name, "Description"))
1136 else if (streq(name, "FragmentPath"))
1137 i->fragment_path = s;
1138 else if (streq(name, "DefaultControlGroup"))
1139 i->default_control_group = s;
1140 else if (streq(name, "StatusText"))
1142 else if (streq(name, "SysFSPath"))
1144 else if (streq(name, "Where"))
1146 else if (streq(name, "What"))
1153 case DBUS_TYPE_UINT32: {
1156 dbus_message_iter_get_basic(iter, &u);
1158 if (streq(name, "MainPID")) {
1160 i->main_pid = (pid_t) u;
1163 } else if (streq(name, "ControlPID"))
1164 i->control_pid = (pid_t) u;
1165 else if (streq(name, "ExecMainPID")) {
1167 i->main_pid = (pid_t) u;
1168 } else if (streq(name, "NAccepted"))
1170 else if (streq(name, "NConnections"))
1171 i->n_connections = u;
1176 case DBUS_TYPE_INT32: {
1179 dbus_message_iter_get_basic(iter, &j);
1181 if (streq(name, "ExecMainCode"))
1182 i->exit_code = (int) j;
1183 else if (streq(name, "ExecMainStatus"))
1184 i->exit_status = (int) j;
1189 case DBUS_TYPE_UINT64: {
1192 dbus_message_iter_get_basic(iter, &u);
1194 if (streq(name, "ExecMainStartTimestamp"))
1195 i->start_timestamp = (usec_t) u;
1196 else if (streq(name, "ExecMainExitTimestamp"))
1197 i->exit_timestamp = (usec_t) u;
1206 static int print_property(const char *name, DBusMessageIter *iter) {
1210 /* This is a low-level property printer, see
1211 * print_status_info() for the nicer output */
1213 if (arg_property && !streq(name, arg_property))
1216 switch (dbus_message_iter_get_arg_type(iter)) {
1218 case DBUS_TYPE_STRING: {
1220 dbus_message_iter_get_basic(iter, &s);
1222 if (arg_all || s[0])
1223 printf("%s=%s\n", name, s);
1228 case DBUS_TYPE_BOOLEAN: {
1230 dbus_message_iter_get_basic(iter, &b);
1231 printf("%s=%s\n", name, yes_no(b));
1236 case DBUS_TYPE_UINT64: {
1238 dbus_message_iter_get_basic(iter, &u);
1240 /* Yes, heuristics! But we can change this check
1241 * should it turn out to not be sufficient */
1243 if (strstr(name, "Timestamp")) {
1244 char timestamp[FORMAT_TIMESTAMP_MAX], *t;
1246 if ((t = format_timestamp(timestamp, sizeof(timestamp), u)) || arg_all)
1247 printf("%s=%s\n", name, strempty(t));
1248 } else if (strstr(name, "USec")) {
1249 char timespan[FORMAT_TIMESPAN_MAX];
1251 printf("%s=%s\n", name, format_timespan(timespan, sizeof(timespan), u));
1253 printf("%s=%llu\n", name, (unsigned long long) u);
1258 case DBUS_TYPE_UINT32: {
1260 dbus_message_iter_get_basic(iter, &u);
1262 if (strstr(name, "UMask") || strstr(name, "Mode"))
1263 printf("%s=%04o\n", name, u);
1265 printf("%s=%u\n", name, (unsigned) u);
1270 case DBUS_TYPE_INT32: {
1272 dbus_message_iter_get_basic(iter, &i);
1274 printf("%s=%i\n", name, (int) i);
1278 case DBUS_TYPE_STRUCT: {
1279 DBusMessageIter sub;
1280 dbus_message_iter_recurse(iter, &sub);
1282 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "Job")) {
1285 dbus_message_iter_get_basic(&sub, &u);
1288 printf("%s=%u\n", name, (unsigned) u);
1290 printf("%s=\n", name);
1293 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Unit")) {
1296 dbus_message_iter_get_basic(&sub, &s);
1298 if (arg_all || s[0])
1299 printf("%s=%s\n", name, s);
1307 case DBUS_TYPE_ARRAY:
1309 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
1310 DBusMessageIter sub;
1313 dbus_message_iter_recurse(iter, &sub);
1316 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1317 printf("%s=", name);
1319 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1322 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
1323 dbus_message_iter_get_basic(&sub, &s);
1324 printf("%s%s", space ? " " : "", s);
1327 dbus_message_iter_next(&sub);
1334 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_BYTE) {
1335 DBusMessageIter sub;
1337 dbus_message_iter_recurse(iter, &sub);
1340 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1341 printf("%s=", name);
1343 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1346 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_BYTE);
1347 dbus_message_iter_get_basic(&sub, &u);
1350 dbus_message_iter_next(&sub);
1357 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Paths")) {
1358 DBusMessageIter sub, sub2;
1360 dbus_message_iter_recurse(iter, &sub);
1362 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1363 const char *type, *path;
1365 dbus_message_iter_recurse(&sub, &sub2);
1367 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
1368 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0)
1369 printf("%s=%s\n", type, path);
1371 dbus_message_iter_next(&sub);
1375 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Timers")) {
1376 DBusMessageIter sub, sub2;
1378 dbus_message_iter_recurse(iter, &sub);
1380 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1382 uint64_t value, next_elapse;
1384 dbus_message_iter_recurse(&sub, &sub2);
1386 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &base, true) >= 0 &&
1387 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &value, true) >= 0 &&
1388 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &next_elapse, false) >= 0) {
1389 char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX];
1391 printf("%s={ value=%s ; next_elapse=%s }\n",
1393 format_timespan(timespan1, sizeof(timespan1), value),
1394 format_timespan(timespan2, sizeof(timespan2), next_elapse));
1397 dbus_message_iter_next(&sub);
1401 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && startswith(name, "Exec")) {
1403 DBusMessageIter sub, sub2, sub3;
1405 dbus_message_iter_recurse(iter, &sub);
1407 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1409 uint64_t start_time, exit_time;
1411 int32_t code, status;
1413 dbus_message_iter_recurse(&sub, &sub2);
1415 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0)
1418 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_ARRAY ||
1419 dbus_message_iter_get_element_type(&sub2) != DBUS_TYPE_STRING)
1422 printf("%s={ path=%s ; argv[]=", name, path);
1424 dbus_message_iter_recurse(&sub2, &sub3);
1426 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1429 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1430 dbus_message_iter_get_basic(&sub3, &s);
1432 dbus_message_iter_next(&sub3);
1435 if (dbus_message_iter_next(&sub2) &&
1436 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_time, true) >= 0 &&
1437 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_time, true) >= 0 &&
1438 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, true) >= 0 &&
1439 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &code, true) >= 0 &&
1440 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &status, false) >= 0) {
1442 char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
1444 printf("; start=%s ; stop=%s ; pid=%u ; code=%s ; status=%i/%s",
1445 strna(format_timestamp(timestamp1, sizeof(timestamp1), start_time)),
1446 strna(format_timestamp(timestamp2, sizeof(timestamp2), exit_time)),
1448 sigchld_code_to_string(code),
1450 strempty(code == CLD_EXITED ? NULL : strsignal(status)));
1455 dbus_message_iter_next(&sub);
1465 printf("%s=[unprintable]\n", name);
1470 static int show_one(DBusConnection *bus, const char *path, bool show_properties, bool *new_line) {
1471 DBusMessage *m = NULL, *reply = NULL;
1472 const char *interface = "";
1475 DBusMessageIter iter, sub, sub2, sub3;
1476 UnitStatusInfo info;
1483 dbus_error_init(&error);
1485 if (!(m = dbus_message_new_method_call(
1486 "org.freedesktop.systemd1",
1488 "org.freedesktop.DBus.Properties",
1490 log_error("Could not allocate message.");
1495 if (!dbus_message_append_args(m,
1496 DBUS_TYPE_STRING, &interface,
1497 DBUS_TYPE_INVALID)) {
1498 log_error("Could not append arguments to message.");
1503 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1504 log_error("Failed to issue method call: %s", error.message);
1509 if (!dbus_message_iter_init(reply, &iter) ||
1510 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
1511 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
1512 log_error("Failed to parse reply.");
1517 dbus_message_iter_recurse(&iter, &sub);
1524 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1527 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
1528 log_error("Failed to parse reply.");
1533 dbus_message_iter_recurse(&sub, &sub2);
1535 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
1536 log_error("Failed to parse reply.");
1541 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
1542 log_error("Failed to parse reply.");
1547 dbus_message_iter_recurse(&sub2, &sub3);
1549 if (show_properties)
1550 r = print_property(name, &sub3);
1552 r = status_property(name, &sub3, &info);
1555 log_error("Failed to parse reply.");
1560 dbus_message_iter_next(&sub);
1563 if (!show_properties)
1564 print_status_info(&info);
1570 dbus_message_unref(m);
1573 dbus_message_unref(reply);
1575 dbus_error_free(&error);
1580 static int show(DBusConnection *bus, char **args, unsigned n) {
1581 DBusMessage *m = NULL, *reply = NULL;
1585 bool show_properties, new_line = false;
1590 dbus_error_init(&error);
1592 show_properties = !streq(args[0], "status");
1594 if (show_properties && n <= 1) {
1595 /* If not argument is specified inspect the manager
1598 r = show_one(bus, "/org/freedesktop/systemd1", show_properties, &new_line);
1602 for (i = 1; i < n; i++) {
1603 const char *path = NULL;
1606 if (!show_properties || safe_atou32(args[i], &id) < 0) {
1608 if (!(m = dbus_message_new_method_call(
1609 "org.freedesktop.systemd1",
1610 "/org/freedesktop/systemd1",
1611 "org.freedesktop.systemd1.Manager",
1613 log_error("Could not allocate message.");
1618 if (!dbus_message_append_args(m,
1619 DBUS_TYPE_STRING, &args[i],
1620 DBUS_TYPE_INVALID)) {
1621 log_error("Could not append arguments to message.");
1626 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1628 if (!dbus_error_has_name(&error, DBUS_ERROR_ACCESS_DENIED)) {
1629 log_error("Failed to issue method call: %s", error.message);
1634 dbus_error_free(&error);
1636 dbus_message_unref(m);
1637 if (!(m = dbus_message_new_method_call(
1638 "org.freedesktop.systemd1",
1639 "/org/freedesktop/systemd1",
1640 "org.freedesktop.systemd1.Manager",
1642 log_error("Could not allocate message.");
1647 if (!dbus_message_append_args(m,
1648 DBUS_TYPE_STRING, &args[i],
1649 DBUS_TYPE_INVALID)) {
1650 log_error("Could not append arguments to message.");
1655 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1656 log_error("Failed to issue method call: %s", error.message);
1664 if (!(m = dbus_message_new_method_call(
1665 "org.freedesktop.systemd1",
1666 "/org/freedesktop/systemd1",
1667 "org.freedesktop.systemd1.Manager",
1669 log_error("Could not allocate message.");
1674 if (!dbus_message_append_args(m,
1675 DBUS_TYPE_UINT32, &id,
1676 DBUS_TYPE_INVALID)) {
1677 log_error("Could not append arguments to message.");
1682 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1683 log_error("Failed to issue method call: %s", error.message);
1689 if (!dbus_message_get_args(reply, &error,
1690 DBUS_TYPE_OBJECT_PATH, &path,
1691 DBUS_TYPE_INVALID)) {
1692 log_error("Failed to parse reply: %s", error.message);
1697 if ((r = show_one(bus, path, show_properties, &new_line)) < 0)
1700 dbus_message_unref(m);
1701 dbus_message_unref(reply);
1709 dbus_message_unref(m);
1712 dbus_message_unref(reply);
1714 dbus_error_free(&error);
1719 static DBusHandlerResult monitor_filter(DBusConnection *connection, DBusMessage *message, void *data) {
1721 DBusMessage *m = NULL, *reply = NULL;
1726 dbus_error_init(&error);
1728 /* log_debug("Got D-Bus request: %s.%s() on %s", */
1729 /* dbus_message_get_interface(message), */
1730 /* dbus_message_get_member(message), */
1731 /* dbus_message_get_path(message)); */
1733 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
1734 log_error("Warning! D-Bus connection terminated.");
1735 dbus_connection_close(connection);
1737 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitNew") ||
1738 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitRemoved")) {
1739 const char *id, *path;
1741 if (!dbus_message_get_args(message, &error,
1742 DBUS_TYPE_STRING, &id,
1743 DBUS_TYPE_OBJECT_PATH, &path,
1745 log_error("Failed to parse message: %s", error.message);
1746 else if (streq(dbus_message_get_member(message), "UnitNew"))
1747 printf("Unit %s added.\n", id);
1749 printf("Unit %s removed.\n", id);
1751 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobNew") ||
1752 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
1756 if (!dbus_message_get_args(message, &error,
1757 DBUS_TYPE_UINT32, &id,
1758 DBUS_TYPE_OBJECT_PATH, &path,
1760 log_error("Failed to parse message: %s", error.message);
1761 else if (streq(dbus_message_get_member(message), "JobNew"))
1762 printf("Job %u added.\n", id);
1764 printf("Job %u removed.\n", id);
1767 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Unit", "Changed") ||
1768 dbus_message_is_signal(message, "org.freedesktop.systemd1.Job", "Changed")) {
1770 const char *path, *interface, *property = "Id";
1771 DBusMessageIter iter, sub;
1773 path = dbus_message_get_path(message);
1774 interface = dbus_message_get_interface(message);
1776 if (!(m = dbus_message_new_method_call(
1777 "org.freedesktop.systemd1",
1779 "org.freedesktop.DBus.Properties",
1781 log_error("Could not allocate message.");
1785 if (!dbus_message_append_args(m,
1786 DBUS_TYPE_STRING, &interface,
1787 DBUS_TYPE_STRING, &property,
1788 DBUS_TYPE_INVALID)) {
1789 log_error("Could not append arguments to message.");
1793 if (!(reply = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) {
1794 log_error("Failed to issue method call: %s", error.message);
1798 if (!dbus_message_iter_init(reply, &iter) ||
1799 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1800 log_error("Failed to parse reply.");
1804 dbus_message_iter_recurse(&iter, &sub);
1806 if (streq(interface, "org.freedesktop.systemd1.Unit")) {
1809 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1810 log_error("Failed to parse reply.");
1814 dbus_message_iter_get_basic(&sub, &id);
1815 printf("Unit %s changed.\n", id);
1819 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT32) {
1820 log_error("Failed to parse reply.");
1824 dbus_message_iter_get_basic(&sub, &id);
1825 printf("Job %u changed.\n", id);
1831 dbus_message_unref(m);
1834 dbus_message_unref(reply);
1836 dbus_error_free(&error);
1837 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1841 dbus_message_unref(m);
1844 dbus_message_unref(reply);
1846 dbus_error_free(&error);
1847 return DBUS_HANDLER_RESULT_NEED_MEMORY;
1850 static int monitor(DBusConnection *bus, char **args, unsigned n) {
1851 DBusMessage *m = NULL, *reply = NULL;
1855 dbus_error_init(&error);
1858 dbus_bus_add_match(bus,
1860 "sender='org.freedesktop.systemd1',"
1861 "interface='org.freedesktop.systemd1.Manager',"
1862 "path='/org/freedesktop/systemd1'",
1865 if (dbus_error_is_set(&error)) {
1866 log_error("Failed to add match: %s", error.message);
1871 dbus_bus_add_match(bus,
1873 "sender='org.freedesktop.systemd1',"
1874 "interface='org.freedesktop.systemd1.Unit',"
1878 if (dbus_error_is_set(&error)) {
1879 log_error("Failed to add match: %s", error.message);
1884 dbus_bus_add_match(bus,
1886 "sender='org.freedesktop.systemd1',"
1887 "interface='org.freedesktop.systemd1.Job',"
1891 if (dbus_error_is_set(&error)) {
1892 log_error("Failed to add match: %s", error.message);
1898 if (!dbus_connection_add_filter(bus, monitor_filter, NULL, NULL)) {
1899 log_error("Failed to add filter.");
1904 if (!(m = dbus_message_new_method_call(
1905 "org.freedesktop.systemd1",
1906 "/org/freedesktop/systemd1",
1907 "org.freedesktop.systemd1.Manager",
1909 log_error("Could not allocate message.");
1914 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1915 log_error("Failed to issue method call: %s", error.message);
1920 while (dbus_connection_read_write_dispatch(bus, -1))
1927 /* This is slightly dirty, since we don't undo the filter or the matches. */
1930 dbus_message_unref(m);
1933 dbus_message_unref(reply);
1935 dbus_error_free(&error);
1940 static int dump(DBusConnection *bus, char **args, unsigned n) {
1941 DBusMessage *m = NULL, *reply = NULL;
1946 dbus_error_init(&error);
1948 if (!(m = dbus_message_new_method_call(
1949 "org.freedesktop.systemd1",
1950 "/org/freedesktop/systemd1",
1951 "org.freedesktop.systemd1.Manager",
1953 log_error("Could not allocate message.");
1957 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1958 log_error("Failed to issue method call: %s", error.message);
1963 if (!dbus_message_get_args(reply, &error,
1964 DBUS_TYPE_STRING, &text,
1965 DBUS_TYPE_INVALID)) {
1966 log_error("Failed to parse reply: %s", error.message);
1971 fputs(text, stdout);
1977 dbus_message_unref(m);
1980 dbus_message_unref(reply);
1982 dbus_error_free(&error);
1987 static int snapshot(DBusConnection *bus, char **args, unsigned n) {
1988 DBusMessage *m = NULL, *reply = NULL;
1991 const char *name = "", *path, *id;
1992 dbus_bool_t cleanup = FALSE;
1993 DBusMessageIter iter, sub;
1995 *interface = "org.freedesktop.systemd1.Unit",
1998 dbus_error_init(&error);
2000 if (!(m = dbus_message_new_method_call(
2001 "org.freedesktop.systemd1",
2002 "/org/freedesktop/systemd1",
2003 "org.freedesktop.systemd1.Manager",
2004 "CreateSnapshot"))) {
2005 log_error("Could not allocate message.");
2012 if (!dbus_message_append_args(m,
2013 DBUS_TYPE_STRING, &name,
2014 DBUS_TYPE_BOOLEAN, &cleanup,
2015 DBUS_TYPE_INVALID)) {
2016 log_error("Could not append arguments to message.");
2021 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2022 log_error("Failed to issue method call: %s", error.message);
2027 if (!dbus_message_get_args(reply, &error,
2028 DBUS_TYPE_OBJECT_PATH, &path,
2029 DBUS_TYPE_INVALID)) {
2030 log_error("Failed to parse reply: %s", error.message);
2035 dbus_message_unref(m);
2036 if (!(m = dbus_message_new_method_call(
2037 "org.freedesktop.systemd1",
2039 "org.freedesktop.DBus.Properties",
2041 log_error("Could not allocate message.");
2045 if (!dbus_message_append_args(m,
2046 DBUS_TYPE_STRING, &interface,
2047 DBUS_TYPE_STRING, &property,
2048 DBUS_TYPE_INVALID)) {
2049 log_error("Could not append arguments to message.");
2054 dbus_message_unref(reply);
2055 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2056 log_error("Failed to issue method call: %s", error.message);
2061 if (!dbus_message_iter_init(reply, &iter) ||
2062 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2063 log_error("Failed to parse reply.");
2068 dbus_message_iter_recurse(&iter, &sub);
2070 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2071 log_error("Failed to parse reply.");
2076 dbus_message_iter_get_basic(&sub, &id);
2084 dbus_message_unref(m);
2087 dbus_message_unref(reply);
2089 dbus_error_free(&error);
2094 static int delete_snapshot(DBusConnection *bus, char **args, unsigned n) {
2095 DBusMessage *m = NULL, *reply = NULL;
2103 dbus_error_init(&error);
2105 for (i = 1; i < n; i++) {
2106 const char *path = NULL;
2108 if (!(m = dbus_message_new_method_call(
2109 "org.freedesktop.systemd1",
2110 "/org/freedesktop/systemd1",
2111 "org.freedesktop.systemd1.Manager",
2113 log_error("Could not allocate message.");
2118 if (!dbus_message_append_args(m,
2119 DBUS_TYPE_STRING, &args[i],
2120 DBUS_TYPE_INVALID)) {
2121 log_error("Could not append arguments to message.");
2126 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2127 log_error("Failed to issue method call: %s", error.message);
2132 if (!dbus_message_get_args(reply, &error,
2133 DBUS_TYPE_OBJECT_PATH, &path,
2134 DBUS_TYPE_INVALID)) {
2135 log_error("Failed to parse reply: %s", error.message);
2140 dbus_message_unref(m);
2141 if (!(m = dbus_message_new_method_call(
2142 "org.freedesktop.systemd1",
2144 "org.freedesktop.systemd1.Snapshot",
2146 log_error("Could not allocate message.");
2151 dbus_message_unref(reply);
2152 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2153 log_error("Failed to issue method call: %s", error.message);
2158 dbus_message_unref(m);
2159 dbus_message_unref(reply);
2167 dbus_message_unref(m);
2170 dbus_message_unref(reply);
2172 dbus_error_free(&error);
2177 static int clear_jobs(DBusConnection *bus, char **args, unsigned n) {
2178 DBusMessage *m = NULL, *reply = NULL;
2183 dbus_error_init(&error);
2185 if (arg_action == ACTION_RELOAD)
2187 else if (arg_action == ACTION_REEXEC)
2188 method = "Reexecute";
2190 assert(arg_action == ACTION_SYSTEMCTL);
2193 streq(args[0], "clear-jobs") ? "ClearJobs" :
2194 streq(args[0], "daemon-reload") ? "Reload" :
2195 streq(args[0], "daemon-reexec") ? "Reexecute" :
2199 if (!(m = dbus_message_new_method_call(
2200 "org.freedesktop.systemd1",
2201 "/org/freedesktop/systemd1",
2202 "org.freedesktop.systemd1.Manager",
2204 log_error("Could not allocate message.");
2208 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2210 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
2211 /* There's always a fallback possible for
2212 * legacy actions. */
2217 log_error("Failed to issue method call: %s", error.message);
2226 dbus_message_unref(m);
2229 dbus_message_unref(reply);
2231 dbus_error_free(&error);
2236 static int show_enviroment(DBusConnection *bus, char **args, unsigned n) {
2237 DBusMessage *m = NULL, *reply = NULL;
2239 DBusMessageIter iter, sub, sub2;
2242 *interface = "org.freedesktop.systemd1.Manager",
2243 *property = "Environment";
2245 dbus_error_init(&error);
2247 if (!(m = dbus_message_new_method_call(
2248 "org.freedesktop.systemd1",
2249 "/org/freedesktop/systemd1",
2250 "org.freedesktop.DBus.Properties",
2252 log_error("Could not allocate message.");
2256 if (!dbus_message_append_args(m,
2257 DBUS_TYPE_STRING, &interface,
2258 DBUS_TYPE_STRING, &property,
2259 DBUS_TYPE_INVALID)) {
2260 log_error("Could not append arguments to message.");
2265 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2266 log_error("Failed to issue method call: %s", error.message);
2271 if (!dbus_message_iter_init(reply, &iter) ||
2272 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2273 log_error("Failed to parse reply.");
2278 dbus_message_iter_recurse(&iter, &sub);
2280 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
2281 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
2282 log_error("Failed to parse reply.");
2287 dbus_message_iter_recurse(&sub, &sub2);
2289 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
2292 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
2293 log_error("Failed to parse reply.");
2298 dbus_message_iter_get_basic(&sub2, &text);
2299 printf("%s\n", text);
2301 dbus_message_iter_next(&sub2);
2308 dbus_message_unref(m);
2311 dbus_message_unref(reply);
2313 dbus_error_free(&error);
2318 static int set_environment(DBusConnection *bus, char **args, unsigned n) {
2319 DBusMessage *m = NULL, *reply = NULL;
2323 DBusMessageIter iter, sub;
2326 dbus_error_init(&error);
2328 method = streq(args[0], "set-environment")
2330 : "UnsetEnvironment";
2332 if (!(m = dbus_message_new_method_call(
2333 "org.freedesktop.systemd1",
2334 "/org/freedesktop/systemd1",
2335 "org.freedesktop.systemd1.Manager",
2338 log_error("Could not allocate message.");
2342 dbus_message_iter_init_append(m, &iter);
2344 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
2345 log_error("Could not append arguments to message.");
2350 for (i = 1; i < n; i++)
2351 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &args[i])) {
2352 log_error("Could not append arguments to message.");
2357 if (!dbus_message_iter_close_container(&iter, &sub)) {
2358 log_error("Could not append arguments to message.");
2363 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2364 log_error("Failed to issue method call: %s", error.message);
2373 dbus_message_unref(m);
2376 dbus_message_unref(reply);
2378 dbus_error_free(&error);
2383 static int systemctl_help(void) {
2385 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
2386 "Send control commands to the systemd manager.\n\n"
2387 " -h --help Show this help\n"
2388 " -t --type=TYPE List only units of a particular type\n"
2389 " -p --property=NAME Show only properties by this name\n"
2390 " -a --all Show all units/properties, including dead/empty ones\n"
2391 " --replace When installing a new job, replace existing conflicting ones\n"
2392 " --system Connect to system bus\n"
2393 " --session Connect to session bus\n"
2394 " -q --quiet Suppress output\n"
2395 " --no-block Do not wait until operation finished\n"
2396 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
2398 " list-units List units\n"
2399 " start [NAME...] Start one or more units\n"
2400 " stop [NAME...] Stop one or more units\n"
2401 " restart [NAME...] Restart one or more units\n"
2402 " reload [NAME...] Reload one or more units\n"
2403 " isolate [NAME] Start one unit and stop all others\n"
2404 " check [NAME...] Check whether any of the passed units are active\n"
2405 " status [NAME...] Show status of one or more units\n"
2406 " show [NAME...|JOB...] Show properties of one or more units/jobs/manager\n"
2407 " load [NAME...] Load one or more units\n"
2408 " list-jobs List jobs\n"
2409 " cancel [JOB...] Cancel one or more jobs\n"
2410 " clear-jobs Cancel all jobs\n"
2411 " monitor Monitor unit/job changes\n"
2412 " dump Dump server status\n"
2413 " snapshot [NAME] Create a snapshot\n"
2414 " delete [NAME...] Remove one or more snapshots\n"
2415 " daemon-reload Reload systemd manager configuration\n"
2416 " daemon-reexec Reexecute systemd manager\n"
2417 " daemon-exit Ask the systemd manager to quit\n"
2418 " show-environment Dump environment\n"
2419 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
2420 " unset-environment [NAME...] Unset one or more environment variables\n"
2421 " halt Shut down and halt the system\n"
2422 " poweroff Shut down and power-off the system\n"
2423 " reboot Shut down and reboot the system\n"
2424 " default Enter default mode\n"
2425 " rescue Enter rescue mode\n"
2426 " emergency Enter emergency mode\n",
2427 program_invocation_short_name);
2432 static int halt_help(void) {
2434 printf("%s [OPTIONS...]\n\n"
2435 "%s the system.\n\n"
2436 " --help Show this help\n"
2437 " --halt Halt the machine\n"
2438 " -p --poweroff Switch off the machine\n"
2439 " --reboot Reboot the machine\n"
2440 " -f --force Force immediate halt/power-off/reboot\n"
2441 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
2442 " -d --no-wtmp Don't write wtmp record\n"
2443 " -n --no-sync Don't sync before halt/power-off/reboot\n"
2444 " --no-wall Don't send wall message before halt/power-off/reboot\n",
2445 program_invocation_short_name,
2446 arg_action == ACTION_REBOOT ? "Reboot" :
2447 arg_action == ACTION_POWEROFF ? "Power off" :
2453 static int shutdown_help(void) {
2455 printf("%s [OPTIONS...] [now] [WALL...]\n\n"
2456 "Shut down the system.\n\n"
2457 " --help Show this help\n"
2458 " -H --halt Halt the machine\n"
2459 " -P --poweroff Power-off the machine\n"
2460 " -r --reboot Reboot the machine\n"
2461 " -h Equivalent to --poweroff, overriden by --halt\n"
2462 " -k Don't halt/power-off/reboot, just send warnings\n"
2463 " --no-wall Don't send wall message before halt/power-off/reboot\n",
2464 program_invocation_short_name);
2469 static int telinit_help(void) {
2471 printf("%s [OPTIONS...] {COMMAND}\n\n"
2472 "Send control commands to the init daemon.\n\n"
2473 " --help Show this help\n"
2474 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
2476 " 0 Power-off the machine\n"
2477 " 6 Reboot the machine\n"
2478 " 2, 3, 4, 5 Start runlevelX.target unit\n"
2479 " 1, s, S Enter rescue mode\n"
2480 " q, Q Reload init daemon configuration\n"
2481 " u, U Reexecute init daemon\n",
2482 program_invocation_short_name);
2487 static int runlevel_help(void) {
2489 printf("%s [OPTIONS...]\n\n"
2490 "Prints the previous and current runlevel of the init system.\n\n"
2491 " --help Show this help\n",
2492 program_invocation_short_name);
2497 static int systemctl_parse_argv(int argc, char *argv[]) {
2500 ARG_REPLACE = 0x100,
2507 static const struct option options[] = {
2508 { "help", no_argument, NULL, 'h' },
2509 { "type", required_argument, NULL, 't' },
2510 { "property", required_argument, NULL, 'p' },
2511 { "all", no_argument, NULL, 'a' },
2512 { "replace", no_argument, NULL, ARG_REPLACE },
2513 { "session", no_argument, NULL, ARG_SESSION },
2514 { "system", no_argument, NULL, ARG_SYSTEM },
2515 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
2516 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2517 { "quiet", no_argument, NULL, 'q' },
2518 { NULL, 0, NULL, 0 }
2526 while ((c = getopt_long(argc, argv, "ht:p:aq", options, NULL)) >= 0) {
2539 arg_property = optarg;
2541 /* If the user asked for a particular
2542 * property, show it to him, even if it is
2560 arg_session = false;
2564 arg_no_block = true;
2579 log_error("Unknown option code %c", c);
2587 static int halt_parse_argv(int argc, char *argv[]) {
2596 static const struct option options[] = {
2597 { "help", no_argument, NULL, ARG_HELP },
2598 { "halt", no_argument, NULL, ARG_HALT },
2599 { "poweroff", no_argument, NULL, 'p' },
2600 { "reboot", no_argument, NULL, ARG_REBOOT },
2601 { "force", no_argument, NULL, 'f' },
2602 { "wtmp-only", no_argument, NULL, 'w' },
2603 { "no-wtmp", no_argument, NULL, 'd' },
2604 { "no-sync", no_argument, NULL, 'n' },
2605 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2606 { NULL, 0, NULL, 0 }
2614 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
2615 if (runlevel == '0' || runlevel == '6')
2616 arg_immediate = true;
2618 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
2626 arg_action = ACTION_HALT;
2630 arg_action = ACTION_POWEROFF;
2634 arg_action = ACTION_REBOOT;
2638 arg_immediate = true;
2659 /* Compatibility nops */
2666 log_error("Unknown option code %c", c);
2671 if (optind < argc) {
2672 log_error("Too many arguments.");
2679 static int shutdown_parse_argv(int argc, char *argv[]) {
2686 static const struct option options[] = {
2687 { "help", no_argument, NULL, ARG_HELP },
2688 { "halt", no_argument, NULL, 'H' },
2689 { "poweroff", no_argument, NULL, 'P' },
2690 { "reboot", no_argument, NULL, 'r' },
2691 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2692 { NULL, 0, NULL, 0 }
2700 while ((c = getopt_long(argc, argv, "HPrhkt:a", options, NULL)) >= 0) {
2708 arg_action = ACTION_HALT;
2712 arg_action = ACTION_POWEROFF;
2716 arg_action = ACTION_REBOOT;
2720 if (arg_action != ACTION_HALT)
2721 arg_action = ACTION_POWEROFF;
2734 /* Compatibility nops */
2741 log_error("Unknown option code %c", c);
2746 if (argc > optind && !streq(argv[optind], "now"))
2747 log_warning("First argument '%s' isn't 'now'. Ignoring.", argv[optind]);
2749 /* We ignore the time argument */
2750 if (argc > optind + 1)
2751 arg_wall = argv + optind + 1;
2758 static int telinit_parse_argv(int argc, char *argv[]) {
2765 static const struct option options[] = {
2766 { "help", no_argument, NULL, ARG_HELP },
2767 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2768 { NULL, 0, NULL, 0 }
2771 static const struct {
2775 { '0', ACTION_POWEROFF },
2776 { '6', ACTION_REBOOT },
2777 { '1', ACTION_RESCUE },
2778 { '2', ACTION_RUNLEVEL2 },
2779 { '3', ACTION_RUNLEVEL3 },
2780 { '4', ACTION_RUNLEVEL4 },
2781 { '5', ACTION_RUNLEVEL5 },
2782 { 's', ACTION_RESCUE },
2783 { 'S', ACTION_RESCUE },
2784 { 'q', ACTION_RELOAD },
2785 { 'Q', ACTION_RELOAD },
2786 { 'u', ACTION_REEXEC },
2787 { 'U', ACTION_REEXEC }
2796 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
2811 log_error("Unknown option code %c", c);
2816 if (optind >= argc) {
2821 if (optind + 1 < argc) {
2822 log_error("Too many arguments.");
2826 if (strlen(argv[optind]) != 1) {
2827 log_error("Expected single character argument.");
2831 for (i = 0; i < ELEMENTSOF(table); i++)
2832 if (table[i].from == argv[optind][0])
2835 if (i >= ELEMENTSOF(table)) {
2836 log_error("Unknown command %s.", argv[optind]);
2840 arg_action = table[i].to;
2847 static int runlevel_parse_argv(int argc, char *argv[]) {
2853 static const struct option options[] = {
2854 { "help", no_argument, NULL, ARG_HELP },
2855 { NULL, 0, NULL, 0 }
2863 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
2874 log_error("Unknown option code %c", c);
2879 if (optind < argc) {
2880 log_error("Too many arguments.");
2887 static int parse_argv(int argc, char *argv[]) {
2891 if (program_invocation_short_name) {
2893 if (strstr(program_invocation_short_name, "halt")) {
2894 arg_action = ACTION_HALT;
2895 return halt_parse_argv(argc, argv);
2896 } else if (strstr(program_invocation_short_name, "poweroff")) {
2897 arg_action = ACTION_POWEROFF;
2898 return halt_parse_argv(argc, argv);
2899 } else if (strstr(program_invocation_short_name, "reboot")) {
2900 arg_action = ACTION_REBOOT;
2901 return halt_parse_argv(argc, argv);
2902 } else if (strstr(program_invocation_short_name, "shutdown")) {
2903 arg_action = ACTION_POWEROFF;
2904 return shutdown_parse_argv(argc, argv);
2905 } else if (strstr(program_invocation_short_name, "init")) {
2906 arg_action = ACTION_INVALID;
2907 return telinit_parse_argv(argc, argv);
2908 } else if (strstr(program_invocation_short_name, "runlevel")) {
2909 arg_action = ACTION_RUNLEVEL;
2910 return runlevel_parse_argv(argc, argv);
2914 arg_action = ACTION_SYSTEMCTL;
2915 return systemctl_parse_argv(argc, argv);
2918 static int action_to_runlevel(void) {
2920 static const char table[_ACTION_MAX] = {
2921 [ACTION_HALT] = '0',
2922 [ACTION_POWEROFF] = '0',
2923 [ACTION_REBOOT] = '6',
2924 [ACTION_RUNLEVEL2] = '2',
2925 [ACTION_RUNLEVEL3] = '3',
2926 [ACTION_RUNLEVEL4] = '4',
2927 [ACTION_RUNLEVEL5] = '5',
2928 [ACTION_RESCUE] = '1'
2931 assert(arg_action < _ACTION_MAX);
2933 return table[arg_action];
2936 static int talk_upstart(void) {
2937 DBusMessage *m = NULL, *reply = NULL;
2939 int previous, rl, r;
2941 env1_buf[] = "RUNLEVEL=X",
2942 env2_buf[] = "PREVLEVEL=X";
2943 char *env1 = env1_buf, *env2 = env2_buf;
2944 const char *emit = "runlevel";
2945 dbus_bool_t b_false = FALSE;
2946 DBusMessageIter iter, sub;
2947 DBusConnection *bus;
2949 dbus_error_init(&error);
2951 if (!(rl = action_to_runlevel()))
2954 if (utmp_get_runlevel(&previous, NULL) < 0)
2957 if (!(bus = dbus_connection_open("unix:abstract=/com/ubuntu/upstart", &error))) {
2958 if (dbus_error_has_name(&error, DBUS_ERROR_NO_SERVER)) {
2963 log_error("Failed to connect to Upstart bus: %s", error.message);
2968 if ((r = bus_check_peercred(bus)) < 0) {
2969 log_error("Failed to verify owner of bus.");
2973 if (!(m = dbus_message_new_method_call(
2974 "com.ubuntu.Upstart",
2975 "/com/ubuntu/Upstart",
2976 "com.ubuntu.Upstart0_6",
2979 log_error("Could not allocate message.");
2984 dbus_message_iter_init_append(m, &iter);
2986 env1_buf[sizeof(env1_buf)-2] = rl;
2987 env2_buf[sizeof(env2_buf)-2] = previous;
2989 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
2990 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
2991 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
2992 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
2993 !dbus_message_iter_close_container(&iter, &sub) ||
2994 !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
2995 log_error("Could not append arguments to message.");
3000 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3002 if (error_is_no_service(&error)) {
3007 log_error("Failed to issue method call: %s", error.message);
3016 dbus_message_unref(m);
3019 dbus_message_unref(reply);
3022 dbus_connection_unref(bus);
3024 dbus_error_free(&error);
3029 static int talk_initctl(void) {
3030 struct init_request request;
3034 if (!(rl = action_to_runlevel()))
3038 request.magic = INIT_MAGIC;
3039 request.sleeptime = 0;
3040 request.cmd = INIT_CMD_RUNLVL;
3041 request.runlevel = rl;
3043 if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
3045 if (errno == ENOENT)
3048 log_error("Failed to open "INIT_FIFO": %m");
3053 r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
3054 close_nointr_nofail(fd);
3057 log_error("Failed to write to "INIT_FIFO": %m");
3058 return errno ? -errno : -EIO;
3064 static int systemctl_main(DBusConnection *bus, int argc, char *argv[]) {
3066 static const struct {
3074 int (* const dispatch)(DBusConnection *bus, char **args, unsigned n);
3076 { "list-units", LESS, 1, list_units },
3077 { "list-jobs", EQUAL, 1, list_jobs },
3078 { "clear-jobs", EQUAL, 1, clear_jobs },
3079 { "load", MORE, 2, load_unit },
3080 { "cancel", MORE, 2, cancel_job },
3081 { "start", MORE, 2, start_unit },
3082 { "stop", MORE, 2, start_unit },
3083 { "reload", MORE, 2, start_unit },
3084 { "restart", MORE, 2, start_unit },
3085 { "isolate", EQUAL, 2, start_unit },
3086 { "check", MORE, 2, check_unit },
3087 { "show", MORE, 1, show },
3088 { "status", MORE, 2, show },
3089 { "monitor", EQUAL, 1, monitor },
3090 { "dump", EQUAL, 1, dump },
3091 { "snapshot", LESS, 2, snapshot },
3092 { "delete", MORE, 2, delete_snapshot },
3093 { "daemon-reload", EQUAL, 1, clear_jobs },
3094 { "daemon-reexec", EQUAL, 1, clear_jobs },
3095 { "daemon-exit", EQUAL, 1, clear_jobs },
3096 { "show-environment", EQUAL, 1, show_enviroment },
3097 { "set-environment", MORE, 2, set_environment },
3098 { "unset-environment", MORE, 2, set_environment },
3099 { "halt", EQUAL, 1, start_special },
3100 { "poweroff", EQUAL, 1, start_special },
3101 { "reboot", EQUAL, 1, start_special },
3102 { "default", EQUAL, 1, start_special },
3103 { "rescue", EQUAL, 1, start_special },
3104 { "emergency", EQUAL, 1, start_special },
3114 left = argc - optind;
3117 /* Special rule: no arguments means "list-units" */
3120 if (streq(argv[optind], "help")) {
3125 for (i = 0; i < ELEMENTSOF(verbs); i++)
3126 if (streq(argv[optind], verbs[i].verb))
3129 if (i >= ELEMENTSOF(verbs)) {
3130 log_error("Unknown operation %s", argv[optind]);
3135 switch (verbs[i].argc_cmp) {
3138 if (left != verbs[i].argc) {
3139 log_error("Invalid number of arguments.");
3146 if (left < verbs[i].argc) {
3147 log_error("Too few arguments.");
3154 if (left > verbs[i].argc) {
3155 log_error("Too many arguments.");
3162 assert_not_reached("Unknown comparison operator.");
3165 return verbs[i].dispatch(bus, argv + optind, left);
3168 static int reload_with_fallback(DBusConnection *bus) {
3172 /* First, try systemd via D-Bus. */
3173 if ((r = clear_jobs(bus, NULL, 0)) > 0)
3177 /* Nothing else worked, so let's try signals */
3178 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
3180 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
3181 log_error("kill() failed: %m");
3188 static int start_with_fallback(DBusConnection *bus) {
3191 warn_wall(arg_action);
3194 /* First, try systemd via D-Bus. */
3195 if ((r = start_unit(bus, NULL, 0)) > 0)
3198 /* Hmm, talking to systemd via D-Bus didn't work. Then
3199 * let's try to talk to Upstart via D-Bus. */
3200 if ((r = talk_upstart()) > 0)
3204 /* Nothing else worked, so let's try
3206 if ((r = talk_initctl()) != 0)
3209 log_error("Failed to talk to init daemon.");
3213 static int halt_main(DBusConnection *bus) {
3217 return start_with_fallback(bus);
3220 if ((r = utmp_put_shutdown(0)) < 0)
3221 log_warning("Failed to write utmp record: %s", strerror(-r));
3229 /* Make sure C-A-D is handled by the kernel from this
3231 reboot(RB_ENABLE_CAD);
3233 switch (arg_action) {
3236 log_info("Halting");
3237 reboot(RB_HALT_SYSTEM);
3240 case ACTION_POWEROFF:
3241 log_info("Powering off");
3242 reboot(RB_POWER_OFF);
3246 log_info("Rebooting");
3247 reboot(RB_AUTOBOOT);
3251 assert_not_reached("Unknown halt action.");
3254 /* We should never reach this. */
3258 static int runlevel_main(void) {
3259 int r, runlevel, previous;
3261 if ((r = utmp_get_runlevel(&runlevel, &previous)) < 0) {
3267 previous <= 0 ? 'N' : previous,
3268 runlevel <= 0 ? 'N' : runlevel);
3273 int main(int argc, char*argv[]) {
3275 DBusConnection *bus = NULL;
3278 dbus_error_init(&error);
3280 log_parse_environment();
3282 if ((r = parse_argv(argc, argv)) < 0)
3289 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
3290 * let's shortcut this */
3291 if (arg_action == ACTION_RUNLEVEL) {
3292 retval = runlevel_main() < 0;
3296 bus_connect(arg_session ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &bus, &private_bus, &error);
3298 switch (arg_action) {
3300 case ACTION_SYSTEMCTL: {
3303 log_error("Failed to get D-Bus connection: %s", error.message);
3307 retval = systemctl_main(bus, argc, argv) < 0;
3312 case ACTION_POWEROFF:
3314 retval = halt_main(bus) < 0;
3317 case ACTION_RUNLEVEL2:
3318 case ACTION_RUNLEVEL3:
3319 case ACTION_RUNLEVEL4:
3320 case ACTION_RUNLEVEL5:
3322 case ACTION_EMERGENCY:
3323 case ACTION_DEFAULT:
3324 retval = start_with_fallback(bus) < 0;
3329 retval = reload_with_fallback(bus) < 0;
3332 case ACTION_INVALID:
3333 case ACTION_RUNLEVEL:
3335 assert_not_reached("Unknown action");
3341 dbus_connection_unref(bus);
3343 dbus_error_free(&error);