1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/reboot.h>
28 #include <sys/ioctl.h>
32 #include <sys/socket.h>
34 #include <dbus/dbus.h>
40 #include "utmp-wtmp.h"
44 #include "dbus-common.h"
46 static const char *arg_type = NULL;
47 static const char *arg_property = NULL;
48 static bool arg_all = false;
49 static bool arg_replace = false;
50 static bool arg_session = false;
51 static bool arg_no_block = false;
52 static bool arg_immediate = false;
53 static bool arg_no_wtmp = false;
54 static bool arg_no_sync = false;
55 static bool arg_no_wall = false;
56 static bool arg_dry = false;
57 static bool arg_quiet = false;
58 static char **arg_wall = NULL;
76 } arg_action = ACTION_SYSTEMCTL;
78 static bool error_is_no_service(DBusError *error) {
82 if (!dbus_error_is_set(error))
85 if (dbus_error_has_name(error, DBUS_ERROR_NAME_HAS_NO_OWNER))
88 if (dbus_error_has_name(error, DBUS_ERROR_SERVICE_UNKNOWN))
91 return startswith(error->name, "org.freedesktop.DBus.Error.Spawn.");
94 static int bus_iter_get_basic_and_next(DBusMessageIter *iter, int type, void *data, bool next) {
99 if (dbus_message_iter_get_arg_type(iter) != type)
102 dbus_message_iter_get_basic(iter, data);
104 if (!dbus_message_iter_next(iter) != !next)
110 static int columns(void) {
111 static int parsed_columns = 0;
114 if (parsed_columns > 0)
115 return parsed_columns;
117 if ((e = getenv("COLUMNS")))
118 parsed_columns = atoi(e);
120 if (parsed_columns <= 0) {
124 if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) >= 0)
125 parsed_columns = ws.ws_col;
128 if (parsed_columns <= 0)
131 return parsed_columns;
135 static void warn_wall(enum action action) {
136 static const char *table[_ACTION_MAX] = {
137 [ACTION_HALT] = "The system is going down for system halt NOW!",
138 [ACTION_REBOOT] = "The system is going down for reboot NOW!",
139 [ACTION_POWEROFF] = "The system is going down for power-off NOW!",
140 [ACTION_RESCUE] = "The system is going down to rescue mode NOW!",
141 [ACTION_EMERGENCY] = "The system is going down to emergency mode NOW!"
150 if (!(p = strv_join(arg_wall, " "))) {
151 log_error("Failed to join strings.");
167 utmp_wall(table[action]);
170 static int list_units(DBusConnection *bus, char **args, unsigned n) {
171 DBusMessage *m = NULL, *reply = NULL;
174 DBusMessageIter iter, sub, sub2;
177 dbus_error_init(&error);
181 if (!(m = dbus_message_new_method_call(
182 "org.freedesktop.systemd1",
183 "/org/freedesktop/systemd1",
184 "org.freedesktop.systemd1.Manager",
186 log_error("Could not allocate message.");
190 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
191 log_error("Failed to issue method call: %s", error.message);
196 if (!dbus_message_iter_init(reply, &iter) ||
197 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
198 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
199 log_error("Failed to parse reply.");
204 dbus_message_iter_recurse(&iter, &sub);
206 printf("%-45s %-6s %-12s %-12s %-15s %s\n", "UNIT", "LOAD", "ACTIVE", "SUB", "JOB", "DESCRIPTION");
208 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
209 const char *id, *description, *load_state, *active_state, *sub_state, *unit_state, *job_type, *job_path, *dot;
212 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
213 log_error("Failed to parse reply.");
218 dbus_message_iter_recurse(&sub, &sub2);
220 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
221 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &description, true) < 0 ||
222 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &load_state, true) < 0 ||
223 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &active_state, true) < 0 ||
224 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &sub_state, true) < 0 ||
225 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_state, true) < 0 ||
226 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &job_id, true) < 0 ||
227 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &job_type, true) < 0 ||
228 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, false) < 0) {
229 log_error("Failed to parse reply.");
234 if ((!arg_type || ((dot = strrchr(id, '.')) &&
235 streq(dot+1, arg_type))) &&
236 (arg_all || !streq(active_state, "inactive"))) {
240 printf("%-45s %-6s %-12s %-12s%n", id, load_state, active_state, sub_state, &a);
243 printf(" %-15s%n", job_type, &b);
247 if (a + b + 2 < columns()) {
251 printf("%.*s", columns() - a - b - 2, description);
258 dbus_message_iter_next(&sub);
262 printf("\n%u units listed.\n", k);
264 printf("\n%u live units listed. Pass --all to see dead units, too.\n", k);
270 dbus_message_unref(m);
273 dbus_message_unref(reply);
275 dbus_error_free(&error);
280 static int list_jobs(DBusConnection *bus, char **args, unsigned n) {
281 DBusMessage *m = NULL, *reply = NULL;
284 DBusMessageIter iter, sub, sub2;
287 dbus_error_init(&error);
291 if (!(m = dbus_message_new_method_call(
292 "org.freedesktop.systemd1",
293 "/org/freedesktop/systemd1",
294 "org.freedesktop.systemd1.Manager",
296 log_error("Could not allocate message.");
300 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
301 log_error("Failed to issue method call: %s", error.message);
306 if (!dbus_message_iter_init(reply, &iter) ||
307 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
308 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
309 log_error("Failed to parse reply.");
314 dbus_message_iter_recurse(&iter, &sub);
316 printf("%4s %-45s %-17s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
318 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
319 const char *name, *type, *state, *job_path, *unit_path;
322 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
323 log_error("Failed to parse reply.");
328 dbus_message_iter_recurse(&sub, &sub2);
330 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &id, true) < 0 ||
331 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0 ||
332 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
333 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, true) < 0 ||
334 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, true) < 0 ||
335 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, false) < 0) {
336 log_error("Failed to parse reply.");
341 printf("%4u %-45s %-17s %-7s\n", id, name, type, state);
344 dbus_message_iter_next(&sub);
347 printf("\n%u jobs listed.\n", k);
352 dbus_message_unref(m);
355 dbus_message_unref(reply);
357 dbus_error_free(&error);
362 static int load_unit(DBusConnection *bus, char **args, unsigned n) {
363 DBusMessage *m = NULL, *reply = NULL;
368 dbus_error_init(&error);
373 for (i = 1; i < n; i++) {
375 if (!(m = dbus_message_new_method_call(
376 "org.freedesktop.systemd1",
377 "/org/freedesktop/systemd1",
378 "org.freedesktop.systemd1.Manager",
380 log_error("Could not allocate message.");
385 if (!dbus_message_append_args(m,
386 DBUS_TYPE_STRING, &args[i],
387 DBUS_TYPE_INVALID)) {
388 log_error("Could not append arguments to message.");
393 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
394 log_error("Failed to issue method call: %s", error.message);
399 dbus_message_unref(m);
400 dbus_message_unref(reply);
409 dbus_message_unref(m);
412 dbus_message_unref(reply);
414 dbus_error_free(&error);
419 static int cancel_job(DBusConnection *bus, char **args, unsigned n) {
420 DBusMessage *m = NULL, *reply = NULL;
425 dbus_error_init(&error);
430 for (i = 1; i < n; i++) {
434 if (!(m = dbus_message_new_method_call(
435 "org.freedesktop.systemd1",
436 "/org/freedesktop/systemd1",
437 "org.freedesktop.systemd1.Manager",
439 log_error("Could not allocate message.");
444 if ((r = safe_atou(args[i], &id)) < 0) {
445 log_error("Failed to parse job id: %s", strerror(-r));
449 assert_cc(sizeof(uint32_t) == sizeof(id));
450 if (!dbus_message_append_args(m,
451 DBUS_TYPE_UINT32, &id,
452 DBUS_TYPE_INVALID)) {
453 log_error("Could not append arguments to message.");
458 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
459 log_error("Failed to issue method call: %s", error.message);
464 if (!dbus_message_get_args(reply, &error,
465 DBUS_TYPE_OBJECT_PATH, &path,
466 DBUS_TYPE_INVALID)) {
467 log_error("Failed to parse reply: %s", error.message);
472 dbus_message_unref(m);
473 if (!(m = dbus_message_new_method_call(
474 "org.freedesktop.systemd1",
476 "org.freedesktop.systemd1.Job",
478 log_error("Could not allocate message.");
483 dbus_message_unref(reply);
484 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
485 log_error("Failed to issue method call: %s", error.message);
490 dbus_message_unref(m);
491 dbus_message_unref(reply);
499 dbus_message_unref(m);
502 dbus_message_unref(reply);
504 dbus_error_free(&error);
509 typedef struct WaitData {
514 static DBusHandlerResult wait_filter(DBusConnection *connection, DBusMessage *message, void *data) {
522 dbus_error_init(&error);
524 /* log_debug("Got D-Bus request: %s.%s() on %s", */
525 /* dbus_message_get_interface(message), */
526 /* dbus_message_get_member(message), */
527 /* dbus_message_get_path(message)); */
529 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
530 log_error("Warning! D-Bus connection terminated.");
531 dbus_connection_close(connection);
533 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
536 dbus_bool_t success = true;
538 if (!dbus_message_get_args(message, &error,
539 DBUS_TYPE_UINT32, &id,
540 DBUS_TYPE_OBJECT_PATH, &path,
541 DBUS_TYPE_BOOLEAN, &success,
543 log_error("Failed to parse message: %s", error.message);
547 if ((p = set_remove(d->set, (char*) path)))
555 dbus_error_free(&error);
556 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
559 static int enable_wait_for_jobs(DBusConnection *bus) {
564 dbus_error_init(&error);
565 dbus_bus_add_match(bus,
567 "sender='org.freedesktop.systemd1',"
568 "interface='org.freedesktop.systemd1.Manager',"
569 "member='JobRemoved',"
570 "path='/org/freedesktop/systemd1'",
573 if (dbus_error_is_set(&error)) {
574 log_error("Failed to add match: %s", error.message);
575 dbus_error_free(&error);
579 /* This is slightly dirty, since we don't undo the match registrations. */
583 static int wait_for_jobs(DBusConnection *bus, Set *s) {
594 if (!dbus_connection_add_filter(bus, wait_filter, &d, NULL)) {
595 log_error("Failed to add filter.");
600 while (!set_isempty(s) &&
601 dbus_connection_read_write_dispatch(bus, -1))
604 if (!arg_quiet && d.failed)
605 log_error("Job failed, see logs for details.");
607 r = d.failed ? -EIO : 0;
610 /* This is slightly dirty, since we don't undo the filter registration. */
615 static int start_unit_one(
622 DBusMessage *m = NULL, *reply = NULL;
630 assert(arg_no_block || s);
632 dbus_error_init(&error);
634 if (!(m = dbus_message_new_method_call(
635 "org.freedesktop.systemd1",
636 "/org/freedesktop/systemd1",
637 "org.freedesktop.systemd1.Manager",
639 log_error("Could not allocate message.");
644 if (!dbus_message_append_args(m,
645 DBUS_TYPE_STRING, &name,
646 DBUS_TYPE_STRING, &mode,
647 DBUS_TYPE_INVALID)) {
648 log_error("Could not append arguments to message.");
653 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
655 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
656 /* There's always a fallback possible for
662 log_error("Failed to issue method call: %s", error.message);
671 if (!dbus_message_get_args(reply, &error,
672 DBUS_TYPE_OBJECT_PATH, &path,
673 DBUS_TYPE_INVALID)) {
674 log_error("Failed to parse reply: %s", error.message);
679 if (!(p = strdup(path))) {
680 log_error("Failed to duplicate path.");
685 if ((r = set_put(s, p)) < 0) {
687 log_error("Failed to add path to set.");
696 dbus_message_unref(m);
699 dbus_message_unref(reply);
701 dbus_error_free(&error);
706 static enum action verb_to_action(const char *verb) {
707 if (streq(verb, "halt"))
709 else if (streq(verb, "poweroff"))
710 return ACTION_POWEROFF;
711 else if (streq(verb, "reboot"))
712 return ACTION_REBOOT;
713 else if (streq(verb, "rescue"))
714 return ACTION_RESCUE;
715 else if (streq(verb, "emergency"))
716 return ACTION_EMERGENCY;
717 else if (streq(verb, "default"))
718 return ACTION_DEFAULT;
720 return ACTION_INVALID;
723 static int start_unit(DBusConnection *bus, char **args, unsigned n) {
725 static const char * const table[_ACTION_MAX] = {
726 [ACTION_HALT] = SPECIAL_HALT_TARGET,
727 [ACTION_POWEROFF] = SPECIAL_POWEROFF_TARGET,
728 [ACTION_REBOOT] = SPECIAL_REBOOT_TARGET,
729 [ACTION_RUNLEVEL2] = SPECIAL_RUNLEVEL2_TARGET,
730 [ACTION_RUNLEVEL3] = SPECIAL_RUNLEVEL3_TARGET,
731 [ACTION_RUNLEVEL4] = SPECIAL_RUNLEVEL4_TARGET,
732 [ACTION_RUNLEVEL5] = SPECIAL_RUNLEVEL5_TARGET,
733 [ACTION_RESCUE] = SPECIAL_RESCUE_TARGET,
734 [ACTION_EMERGENCY] = SPECIAL_EMERGENCY_SERVICE,
735 [ACTION_DEFAULT] = SPECIAL_DEFAULT_TARGET
740 const char *method, *mode, *one_name;
745 if (arg_action == ACTION_SYSTEMCTL) {
747 streq(args[0], "stop") ? "StopUnit" :
748 streq(args[0], "reload") ? "ReloadUnit" :
749 streq(args[0], "restart") ? "RestartUnit" :
753 (streq(args[0], "isolate") ||
754 streq(args[0], "rescue") ||
755 streq(args[0], "emergency")) ? "isolate" :
756 arg_replace ? "replace" :
759 one_name = table[verb_to_action(args[0])];
762 assert(arg_action < ELEMENTSOF(table));
763 assert(table[arg_action]);
765 method = "StartUnit";
767 mode = (arg_action == ACTION_EMERGENCY ||
768 arg_action == ACTION_RESCUE) ? "isolate" : "replace";
770 one_name = table[arg_action];
774 if ((r = enable_wait_for_jobs(bus)) < 0) {
775 log_error("Could not watch jobs: %s", strerror(-r));
779 if (!(s = set_new(string_hash_func, string_compare_func))) {
780 log_error("Failed to allocate set.");
789 if ((r = start_unit_one(bus, method, one_name, mode, s)) <= 0)
792 for (i = 1; i < n; i++)
793 if ((r = start_unit_one(bus, method, args[i], mode, s)) < 0)
798 r = wait_for_jobs(bus, s);
807 static int start_special(DBusConnection *bus, char **args, unsigned n) {
811 warn_wall(verb_to_action(args[0]));
813 return start_unit(bus, args, n);
816 static int check_unit(DBusConnection *bus, char **args, unsigned n) {
817 DBusMessage *m = NULL, *reply = NULL;
819 *interface = "org.freedesktop.systemd1.Unit",
820 *property = "ActiveState";
821 int r = -EADDRNOTAVAIL;
828 dbus_error_init(&error);
830 for (i = 1; i < n; i++) {
831 const char *path = NULL;
833 DBusMessageIter iter, sub;
835 if (!(m = dbus_message_new_method_call(
836 "org.freedesktop.systemd1",
837 "/org/freedesktop/systemd1",
838 "org.freedesktop.systemd1.Manager",
840 log_error("Could not allocate message.");
845 if (!dbus_message_append_args(m,
846 DBUS_TYPE_STRING, &args[i],
847 DBUS_TYPE_INVALID)) {
848 log_error("Could not append arguments to message.");
853 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
855 /* Hmm, cannot figure out anything about this unit... */
859 dbus_error_free(&error);
863 if (!dbus_message_get_args(reply, &error,
864 DBUS_TYPE_OBJECT_PATH, &path,
865 DBUS_TYPE_INVALID)) {
866 log_error("Failed to parse reply: %s", error.message);
871 dbus_message_unref(m);
872 if (!(m = dbus_message_new_method_call(
873 "org.freedesktop.systemd1",
875 "org.freedesktop.DBus.Properties",
877 log_error("Could not allocate message.");
882 if (!dbus_message_append_args(m,
883 DBUS_TYPE_STRING, &interface,
884 DBUS_TYPE_STRING, &property,
885 DBUS_TYPE_INVALID)) {
886 log_error("Could not append arguments to message.");
891 dbus_message_unref(reply);
892 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
893 log_error("Failed to issue method call: %s", error.message);
898 if (!dbus_message_iter_init(reply, &iter) ||
899 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
900 log_error("Failed to parse reply.");
905 dbus_message_iter_recurse(&iter, &sub);
907 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
908 log_error("Failed to parse reply.");
913 dbus_message_iter_get_basic(&sub, &state);
918 if (streq(state, "active") || startswith(state, "reloading"))
921 dbus_message_unref(m);
922 dbus_message_unref(reply);
928 dbus_message_unref(m);
931 dbus_message_unref(reply);
933 dbus_error_free(&error);
938 static void show_cgroup(const char *name) {
943 if (!startswith(name, "name=systemd:"))
946 if (asprintf(&fn, "/cgroup/systemd/%s/tasks", name + 13) < 0)
955 printf("\t\t \342\224\202\n");
960 if (fscanf(f, "%lu", &ul) != 1)
968 get_process_cmdline(last, 60, &t);
969 printf("\t\t \342\224\234 %lu %s\n", (unsigned long) last, strna(t));
978 get_process_cmdline(last, 60, &t);
979 printf("\t\t \342\224\224 %lu %s\n", (unsigned long) last, strna(t));
986 typedef struct UnitStatusInfo {
988 const char *load_state;
989 const char *active_state;
990 const char *sub_state;
992 const char *description;
994 const char *fragment_path;
995 const char *default_control_group;
1000 const char *status_text;
1003 usec_t start_timestamp;
1004 usec_t exit_timestamp;
1006 int exit_code, exit_status;
1009 unsigned n_accepted;
1010 unsigned n_connections;
1013 const char *sysfs_path;
1015 /* Mount, Automount */
1022 static void print_status_info(UnitStatusInfo *i) {
1025 /* This shows pretty information about a unit. See
1026 * print_property() for a low-level property printer */
1028 printf("%s", strna(i->id));
1030 if (i->description && !streq_ptr(i->id, i->description))
1031 printf(" - %s", i->description);
1035 if (i->fragment_path)
1036 printf("\t Loaded: %s (%s)\n", strna(i->load_state), i->fragment_path);
1037 else if (streq_ptr(i->load_state, "failed"))
1038 printf("\t Loaded: " ANSI_HIGHLIGHT_ON "%s" ANSI_HIGHLIGHT_OFF "\n", strna(i->load_state));
1040 printf("\t Loaded: %s\n", strna(i->load_state));
1042 if (streq_ptr(i->active_state, "maintenance"))
1043 printf("\t Active: " ANSI_HIGHLIGHT_ON "%s (%s)" ANSI_HIGHLIGHT_OFF "\n",
1044 strna(i->active_state),
1045 strna(i->sub_state));
1047 printf("\t Active: %s (%s)\n",
1048 strna(i->active_state),
1049 strna(i->sub_state));
1052 printf("\t Device: %s\n", i->sysfs_path);
1054 printf("\t Where: %s\n", i->where);
1056 printf("\t What: %s\n", i->what);
1059 printf("\t Status: \"%s\"\n", i->status_text);
1061 if (i->id && endswith(i->id, ".socket"))
1062 printf("\tAccepted: %u; Connected: %u\n", i->n_accepted, i->n_connections);
1064 if (i->main_pid > 0 || i->control_pid > 0) {
1067 if (i->main_pid > 0) {
1068 printf(" Process: %u", (unsigned) i->main_pid);
1072 get_process_name(i->main_pid, &t);
1078 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
1080 if (i->exit_code == CLD_EXITED)
1081 printf("status=%i", i->exit_status);
1083 printf("signal=%s", strsignal(i->exit_status));
1088 if (i->main_pid > 0 && i->control_pid > 0)
1091 if (i->control_pid > 0) {
1094 printf(" Control: %u", (unsigned) i->control_pid);
1096 get_process_name(i->control_pid, &t);
1106 if (i->default_control_group) {
1107 printf("\t CGroup: %s\n", i->default_control_group);
1108 show_cgroup(i->default_control_group);
1112 static int status_property(const char *name, DBusMessageIter *iter, UnitStatusInfo *i) {
1114 switch (dbus_message_iter_get_arg_type(iter)) {
1116 case DBUS_TYPE_STRING: {
1119 dbus_message_iter_get_basic(iter, &s);
1122 if (streq(name, "Id"))
1124 else if (streq(name, "LoadState"))
1126 else if (streq(name, "ActiveState"))
1127 i->active_state = s;
1128 else if (streq(name, "SubState"))
1130 else if (streq(name, "Description"))
1132 else if (streq(name, "FragmentPath"))
1133 i->fragment_path = s;
1134 else if (streq(name, "DefaultControlGroup"))
1135 i->default_control_group = s;
1136 else if (streq(name, "StatusText"))
1138 else if (streq(name, "SysFSPath"))
1140 else if (streq(name, "Where"))
1142 else if (streq(name, "What"))
1149 case DBUS_TYPE_UINT32: {
1152 dbus_message_iter_get_basic(iter, &u);
1154 if (streq(name, "MainPID")) {
1156 i->main_pid = (pid_t) u;
1159 } else if (streq(name, "ControlPID"))
1160 i->control_pid = (pid_t) u;
1161 else if (streq(name, "ExecMainPID")) {
1163 i->main_pid = (pid_t) u;
1164 } else if (streq(name, "NAccepted"))
1166 else if (streq(name, "NConnections"))
1167 i->n_connections = u;
1172 case DBUS_TYPE_INT32: {
1175 dbus_message_iter_get_basic(iter, &j);
1177 if (streq(name, "ExecMainCode"))
1178 i->exit_code = (int) j;
1179 else if (streq(name, "ExecMainStatus"))
1180 i->exit_status = (int) j;
1185 case DBUS_TYPE_UINT64: {
1188 dbus_message_iter_get_basic(iter, &u);
1190 if (streq(name, "ExecMainStartTimestamp"))
1191 i->start_timestamp = (usec_t) u;
1192 else if (streq(name, "ExecMainExitTimestamp"))
1193 i->exit_timestamp = (usec_t) u;
1202 static int print_property(const char *name, DBusMessageIter *iter) {
1206 /* This is a low-level property printer, see
1207 * print_status_info() for the nicer output */
1209 if (arg_property && !streq(name, arg_property))
1212 switch (dbus_message_iter_get_arg_type(iter)) {
1214 case DBUS_TYPE_STRING: {
1216 dbus_message_iter_get_basic(iter, &s);
1218 if (arg_all || s[0])
1219 printf("%s=%s\n", name, s);
1224 case DBUS_TYPE_BOOLEAN: {
1226 dbus_message_iter_get_basic(iter, &b);
1227 printf("%s=%s\n", name, yes_no(b));
1232 case DBUS_TYPE_UINT64: {
1234 dbus_message_iter_get_basic(iter, &u);
1236 /* Yes, heuristics! But we can change this check
1237 * should it turn out to not be sufficient */
1239 if (strstr(name, "Timestamp")) {
1240 char timestamp[FORMAT_TIMESTAMP_MAX], *t;
1242 if ((t = format_timestamp(timestamp, sizeof(timestamp), u)) || arg_all)
1243 printf("%s=%s\n", name, strempty(t));
1244 } else if (strstr(name, "USec")) {
1245 char timespan[FORMAT_TIMESPAN_MAX];
1247 printf("%s=%s\n", name, format_timespan(timespan, sizeof(timespan), u));
1249 printf("%s=%llu\n", name, (unsigned long long) u);
1254 case DBUS_TYPE_UINT32: {
1256 dbus_message_iter_get_basic(iter, &u);
1258 if (strstr(name, "UMask") || strstr(name, "Mode"))
1259 printf("%s=%04o\n", name, u);
1261 printf("%s=%u\n", name, (unsigned) u);
1266 case DBUS_TYPE_INT32: {
1268 dbus_message_iter_get_basic(iter, &i);
1270 printf("%s=%i\n", name, (int) i);
1274 case DBUS_TYPE_STRUCT: {
1275 DBusMessageIter sub;
1276 dbus_message_iter_recurse(iter, &sub);
1278 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "Job")) {
1281 dbus_message_iter_get_basic(&sub, &u);
1284 printf("%s=%u\n", name, (unsigned) u);
1286 printf("%s=\n", name);
1289 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Unit")) {
1292 dbus_message_iter_get_basic(&sub, &s);
1294 if (arg_all || s[0])
1295 printf("%s=%s\n", name, s);
1303 case DBUS_TYPE_ARRAY:
1305 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
1306 DBusMessageIter sub;
1309 dbus_message_iter_recurse(iter, &sub);
1312 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1313 printf("%s=", name);
1315 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1318 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
1319 dbus_message_iter_get_basic(&sub, &s);
1320 printf("%s%s", space ? " " : "", s);
1323 dbus_message_iter_next(&sub);
1330 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_BYTE) {
1331 DBusMessageIter sub;
1333 dbus_message_iter_recurse(iter, &sub);
1336 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1337 printf("%s=", name);
1339 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1342 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_BYTE);
1343 dbus_message_iter_get_basic(&sub, &u);
1346 dbus_message_iter_next(&sub);
1353 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Paths")) {
1354 DBusMessageIter sub, sub2;
1356 dbus_message_iter_recurse(iter, &sub);
1358 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1359 const char *type, *path;
1361 dbus_message_iter_recurse(&sub, &sub2);
1363 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
1364 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0)
1365 printf("%s=%s\n", type, path);
1367 dbus_message_iter_next(&sub);
1371 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Timers")) {
1372 DBusMessageIter sub, sub2;
1374 dbus_message_iter_recurse(iter, &sub);
1376 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1378 uint64_t value, next_elapse;
1380 dbus_message_iter_recurse(&sub, &sub2);
1382 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &base, true) >= 0 &&
1383 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &value, true) >= 0 &&
1384 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &next_elapse, false) >= 0) {
1385 char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX];
1387 printf("%s={ value=%s ; next_elapse=%s }\n",
1389 format_timespan(timespan1, sizeof(timespan1), value),
1390 format_timespan(timespan2, sizeof(timespan2), next_elapse));
1393 dbus_message_iter_next(&sub);
1397 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && startswith(name, "Exec")) {
1399 DBusMessageIter sub, sub2, sub3;
1401 dbus_message_iter_recurse(iter, &sub);
1403 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1405 uint64_t start_time, exit_time;
1407 int32_t code, status;
1409 dbus_message_iter_recurse(&sub, &sub2);
1411 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0)
1414 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_ARRAY ||
1415 dbus_message_iter_get_element_type(&sub2) != DBUS_TYPE_STRING)
1418 printf("%s={ path=%s ; argv[]=", name, path);
1420 dbus_message_iter_recurse(&sub2, &sub3);
1422 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1425 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1426 dbus_message_iter_get_basic(&sub3, &s);
1428 dbus_message_iter_next(&sub3);
1431 if (dbus_message_iter_next(&sub2) &&
1432 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_time, true) >= 0 &&
1433 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_time, true) >= 0 &&
1434 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, true) >= 0 &&
1435 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &code, true) >= 0 &&
1436 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &status, false) >= 0) {
1438 char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
1440 printf("; start=%s ; stop=%s ; pid=%u ; code=%s ; status=%i/%s",
1441 strna(format_timestamp(timestamp1, sizeof(timestamp1), start_time)),
1442 strna(format_timestamp(timestamp2, sizeof(timestamp2), exit_time)),
1444 sigchld_code_to_string(code),
1446 strempty(code == CLD_EXITED ? NULL : strsignal(status)));
1451 dbus_message_iter_next(&sub);
1461 printf("%s=[unprintable]\n", name);
1466 static int show_one(DBusConnection *bus, const char *path, bool show_properties, bool *new_line) {
1467 DBusMessage *m = NULL, *reply = NULL;
1468 const char *interface = "";
1471 DBusMessageIter iter, sub, sub2, sub3;
1472 UnitStatusInfo info;
1479 dbus_error_init(&error);
1481 if (!(m = dbus_message_new_method_call(
1482 "org.freedesktop.systemd1",
1484 "org.freedesktop.DBus.Properties",
1486 log_error("Could not allocate message.");
1491 if (!dbus_message_append_args(m,
1492 DBUS_TYPE_STRING, &interface,
1493 DBUS_TYPE_INVALID)) {
1494 log_error("Could not append arguments to message.");
1499 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1500 log_error("Failed to issue method call: %s", error.message);
1505 if (!dbus_message_iter_init(reply, &iter) ||
1506 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
1507 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
1508 log_error("Failed to parse reply.");
1513 dbus_message_iter_recurse(&iter, &sub);
1520 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1523 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
1524 log_error("Failed to parse reply.");
1529 dbus_message_iter_recurse(&sub, &sub2);
1531 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
1532 log_error("Failed to parse reply.");
1537 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
1538 log_error("Failed to parse reply.");
1543 dbus_message_iter_recurse(&sub2, &sub3);
1545 if (show_properties)
1546 r = print_property(name, &sub3);
1548 r = status_property(name, &sub3, &info);
1551 log_error("Failed to parse reply.");
1556 dbus_message_iter_next(&sub);
1559 if (!show_properties)
1560 print_status_info(&info);
1566 dbus_message_unref(m);
1569 dbus_message_unref(reply);
1571 dbus_error_free(&error);
1576 static int show(DBusConnection *bus, char **args, unsigned n) {
1577 DBusMessage *m = NULL, *reply = NULL;
1581 bool show_properties, new_line = false;
1586 dbus_error_init(&error);
1588 show_properties = !streq(args[0], "status");
1590 if (show_properties && n <= 1) {
1591 /* If not argument is specified inspect the manager
1594 r = show_one(bus, "/org/freedesktop/systemd1", show_properties, &new_line);
1598 for (i = 1; i < n; i++) {
1599 const char *path = NULL;
1602 if (!show_properties || safe_atou32(args[i], &id) < 0) {
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))) {
1624 if (!dbus_error_has_name(&error, DBUS_ERROR_ACCESS_DENIED)) {
1625 log_error("Failed to issue method call: %s", error.message);
1630 dbus_error_free(&error);
1632 dbus_message_unref(m);
1633 if (!(m = dbus_message_new_method_call(
1634 "org.freedesktop.systemd1",
1635 "/org/freedesktop/systemd1",
1636 "org.freedesktop.systemd1.Manager",
1638 log_error("Could not allocate message.");
1643 if (!dbus_message_append_args(m,
1644 DBUS_TYPE_STRING, &args[i],
1645 DBUS_TYPE_INVALID)) {
1646 log_error("Could not append arguments to message.");
1651 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1652 log_error("Failed to issue method call: %s", error.message);
1660 if (!(m = dbus_message_new_method_call(
1661 "org.freedesktop.systemd1",
1662 "/org/freedesktop/systemd1",
1663 "org.freedesktop.systemd1.Manager",
1665 log_error("Could not allocate message.");
1670 if (!dbus_message_append_args(m,
1671 DBUS_TYPE_UINT32, &id,
1672 DBUS_TYPE_INVALID)) {
1673 log_error("Could not append arguments to message.");
1678 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1679 log_error("Failed to issue method call: %s", error.message);
1685 if (!dbus_message_get_args(reply, &error,
1686 DBUS_TYPE_OBJECT_PATH, &path,
1687 DBUS_TYPE_INVALID)) {
1688 log_error("Failed to parse reply: %s", error.message);
1693 if ((r = show_one(bus, path, show_properties, &new_line)) < 0)
1696 dbus_message_unref(m);
1697 dbus_message_unref(reply);
1705 dbus_message_unref(m);
1708 dbus_message_unref(reply);
1710 dbus_error_free(&error);
1715 static DBusHandlerResult monitor_filter(DBusConnection *connection, DBusMessage *message, void *data) {
1717 DBusMessage *m = NULL, *reply = NULL;
1722 dbus_error_init(&error);
1724 /* log_debug("Got D-Bus request: %s.%s() on %s", */
1725 /* dbus_message_get_interface(message), */
1726 /* dbus_message_get_member(message), */
1727 /* dbus_message_get_path(message)); */
1729 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
1730 log_error("Warning! D-Bus connection terminated.");
1731 dbus_connection_close(connection);
1733 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitNew") ||
1734 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitRemoved")) {
1735 const char *id, *path;
1737 if (!dbus_message_get_args(message, &error,
1738 DBUS_TYPE_STRING, &id,
1739 DBUS_TYPE_OBJECT_PATH, &path,
1741 log_error("Failed to parse message: %s", error.message);
1742 else if (streq(dbus_message_get_member(message), "UnitNew"))
1743 printf("Unit %s added.\n", id);
1745 printf("Unit %s removed.\n", id);
1747 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobNew") ||
1748 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
1752 if (!dbus_message_get_args(message, &error,
1753 DBUS_TYPE_UINT32, &id,
1754 DBUS_TYPE_OBJECT_PATH, &path,
1756 log_error("Failed to parse message: %s", error.message);
1757 else if (streq(dbus_message_get_member(message), "JobNew"))
1758 printf("Job %u added.\n", id);
1760 printf("Job %u removed.\n", id);
1763 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Unit", "Changed") ||
1764 dbus_message_is_signal(message, "org.freedesktop.systemd1.Job", "Changed")) {
1766 const char *path, *interface, *property = "Id";
1767 DBusMessageIter iter, sub;
1769 path = dbus_message_get_path(message);
1770 interface = dbus_message_get_interface(message);
1772 if (!(m = dbus_message_new_method_call(
1773 "org.freedesktop.systemd1",
1775 "org.freedesktop.DBus.Properties",
1777 log_error("Could not allocate message.");
1781 if (!dbus_message_append_args(m,
1782 DBUS_TYPE_STRING, &interface,
1783 DBUS_TYPE_STRING, &property,
1784 DBUS_TYPE_INVALID)) {
1785 log_error("Could not append arguments to message.");
1789 if (!(reply = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) {
1790 log_error("Failed to issue method call: %s", error.message);
1794 if (!dbus_message_iter_init(reply, &iter) ||
1795 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1796 log_error("Failed to parse reply.");
1800 dbus_message_iter_recurse(&iter, &sub);
1802 if (streq(interface, "org.freedesktop.systemd1.Unit")) {
1805 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1806 log_error("Failed to parse reply.");
1810 dbus_message_iter_get_basic(&sub, &id);
1811 printf("Unit %s changed.\n", id);
1815 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT32) {
1816 log_error("Failed to parse reply.");
1820 dbus_message_iter_get_basic(&sub, &id);
1821 printf("Job %u changed.\n", id);
1827 dbus_message_unref(m);
1830 dbus_message_unref(reply);
1832 dbus_error_free(&error);
1833 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1837 dbus_message_unref(m);
1840 dbus_message_unref(reply);
1842 dbus_error_free(&error);
1843 return DBUS_HANDLER_RESULT_NEED_MEMORY;
1846 static int monitor(DBusConnection *bus, char **args, unsigned n) {
1847 DBusMessage *m = NULL, *reply = NULL;
1851 dbus_error_init(&error);
1853 dbus_bus_add_match(bus,
1855 "sender='org.freedesktop.systemd1',"
1856 "interface='org.freedesktop.systemd1.Manager',"
1857 "path='/org/freedesktop/systemd1'",
1860 if (dbus_error_is_set(&error)) {
1861 log_error("Failed to add match: %s", error.message);
1866 dbus_bus_add_match(bus,
1868 "sender='org.freedesktop.systemd1',"
1869 "interface='org.freedesktop.systemd1.Unit',"
1873 if (dbus_error_is_set(&error)) {
1874 log_error("Failed to add match: %s", error.message);
1879 dbus_bus_add_match(bus,
1881 "sender='org.freedesktop.systemd1',"
1882 "interface='org.freedesktop.systemd1.Job',"
1886 if (dbus_error_is_set(&error)) {
1887 log_error("Failed to add match: %s", error.message);
1892 if (!dbus_connection_add_filter(bus, monitor_filter, NULL, NULL)) {
1893 log_error("Failed to add filter.");
1898 if (!(m = dbus_message_new_method_call(
1899 "org.freedesktop.systemd1",
1900 "/org/freedesktop/systemd1",
1901 "org.freedesktop.systemd1.Manager",
1903 log_error("Could not allocate message.");
1908 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1909 log_error("Failed to issue method call: %s", error.message);
1914 while (dbus_connection_read_write_dispatch(bus, -1))
1921 /* This is slightly dirty, since we don't undo the filter or the matches. */
1924 dbus_message_unref(m);
1927 dbus_message_unref(reply);
1929 dbus_error_free(&error);
1934 static int dump(DBusConnection *bus, char **args, unsigned n) {
1935 DBusMessage *m = NULL, *reply = NULL;
1940 dbus_error_init(&error);
1942 if (!(m = dbus_message_new_method_call(
1943 "org.freedesktop.systemd1",
1944 "/org/freedesktop/systemd1",
1945 "org.freedesktop.systemd1.Manager",
1947 log_error("Could not allocate message.");
1951 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1952 log_error("Failed to issue method call: %s", error.message);
1957 if (!dbus_message_get_args(reply, &error,
1958 DBUS_TYPE_STRING, &text,
1959 DBUS_TYPE_INVALID)) {
1960 log_error("Failed to parse reply: %s", error.message);
1965 fputs(text, stdout);
1971 dbus_message_unref(m);
1974 dbus_message_unref(reply);
1976 dbus_error_free(&error);
1981 static int snapshot(DBusConnection *bus, char **args, unsigned n) {
1982 DBusMessage *m = NULL, *reply = NULL;
1985 const char *name = "", *path, *id;
1986 dbus_bool_t cleanup = FALSE;
1987 DBusMessageIter iter, sub;
1989 *interface = "org.freedesktop.systemd1.Unit",
1992 dbus_error_init(&error);
1994 if (!(m = dbus_message_new_method_call(
1995 "org.freedesktop.systemd1",
1996 "/org/freedesktop/systemd1",
1997 "org.freedesktop.systemd1.Manager",
1998 "CreateSnapshot"))) {
1999 log_error("Could not allocate message.");
2006 if (!dbus_message_append_args(m,
2007 DBUS_TYPE_STRING, &name,
2008 DBUS_TYPE_BOOLEAN, &cleanup,
2009 DBUS_TYPE_INVALID)) {
2010 log_error("Could not append arguments to message.");
2015 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2016 log_error("Failed to issue method call: %s", error.message);
2021 if (!dbus_message_get_args(reply, &error,
2022 DBUS_TYPE_OBJECT_PATH, &path,
2023 DBUS_TYPE_INVALID)) {
2024 log_error("Failed to parse reply: %s", error.message);
2029 dbus_message_unref(m);
2030 if (!(m = dbus_message_new_method_call(
2031 "org.freedesktop.systemd1",
2033 "org.freedesktop.DBus.Properties",
2035 log_error("Could not allocate message.");
2039 if (!dbus_message_append_args(m,
2040 DBUS_TYPE_STRING, &interface,
2041 DBUS_TYPE_STRING, &property,
2042 DBUS_TYPE_INVALID)) {
2043 log_error("Could not append arguments to message.");
2048 dbus_message_unref(reply);
2049 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2050 log_error("Failed to issue method call: %s", error.message);
2055 if (!dbus_message_iter_init(reply, &iter) ||
2056 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2057 log_error("Failed to parse reply.");
2062 dbus_message_iter_recurse(&iter, &sub);
2064 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2065 log_error("Failed to parse reply.");
2070 dbus_message_iter_get_basic(&sub, &id);
2078 dbus_message_unref(m);
2081 dbus_message_unref(reply);
2083 dbus_error_free(&error);
2088 static int delete_snapshot(DBusConnection *bus, char **args, unsigned n) {
2089 DBusMessage *m = NULL, *reply = NULL;
2097 dbus_error_init(&error);
2099 for (i = 1; i < n; i++) {
2100 const char *path = NULL;
2102 if (!(m = dbus_message_new_method_call(
2103 "org.freedesktop.systemd1",
2104 "/org/freedesktop/systemd1",
2105 "org.freedesktop.systemd1.Manager",
2107 log_error("Could not allocate message.");
2112 if (!dbus_message_append_args(m,
2113 DBUS_TYPE_STRING, &args[i],
2114 DBUS_TYPE_INVALID)) {
2115 log_error("Could not append arguments to message.");
2120 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2121 log_error("Failed to issue method call: %s", error.message);
2126 if (!dbus_message_get_args(reply, &error,
2127 DBUS_TYPE_OBJECT_PATH, &path,
2128 DBUS_TYPE_INVALID)) {
2129 log_error("Failed to parse reply: %s", error.message);
2134 dbus_message_unref(m);
2135 if (!(m = dbus_message_new_method_call(
2136 "org.freedesktop.systemd1",
2138 "org.freedesktop.systemd1.Snapshot",
2140 log_error("Could not allocate message.");
2145 dbus_message_unref(reply);
2146 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2147 log_error("Failed to issue method call: %s", error.message);
2152 dbus_message_unref(m);
2153 dbus_message_unref(reply);
2161 dbus_message_unref(m);
2164 dbus_message_unref(reply);
2166 dbus_error_free(&error);
2171 static int clear_jobs(DBusConnection *bus, char **args, unsigned n) {
2172 DBusMessage *m = NULL, *reply = NULL;
2177 dbus_error_init(&error);
2179 if (arg_action == ACTION_RELOAD)
2181 else if (arg_action == ACTION_REEXEC)
2182 method = "Reexecute";
2184 assert(arg_action == ACTION_SYSTEMCTL);
2187 streq(args[0], "clear-jobs") ? "ClearJobs" :
2188 streq(args[0], "daemon-reload") ? "Reload" :
2189 streq(args[0], "daemon-reexec") ? "Reexecute" :
2193 if (!(m = dbus_message_new_method_call(
2194 "org.freedesktop.systemd1",
2195 "/org/freedesktop/systemd1",
2196 "org.freedesktop.systemd1.Manager",
2198 log_error("Could not allocate message.");
2202 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2204 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
2205 /* There's always a fallback possible for
2206 * legacy actions. */
2211 log_error("Failed to issue method call: %s", error.message);
2220 dbus_message_unref(m);
2223 dbus_message_unref(reply);
2225 dbus_error_free(&error);
2230 static int show_enviroment(DBusConnection *bus, char **args, unsigned n) {
2231 DBusMessage *m = NULL, *reply = NULL;
2233 DBusMessageIter iter, sub, sub2;
2236 *interface = "org.freedesktop.systemd1.Manager",
2237 *property = "Environment";
2239 dbus_error_init(&error);
2241 if (!(m = dbus_message_new_method_call(
2242 "org.freedesktop.systemd1",
2243 "/org/freedesktop/systemd1",
2244 "org.freedesktop.DBus.Properties",
2246 log_error("Could not allocate message.");
2250 if (!dbus_message_append_args(m,
2251 DBUS_TYPE_STRING, &interface,
2252 DBUS_TYPE_STRING, &property,
2253 DBUS_TYPE_INVALID)) {
2254 log_error("Could not append arguments to message.");
2259 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2260 log_error("Failed to issue method call: %s", error.message);
2265 if (!dbus_message_iter_init(reply, &iter) ||
2266 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2267 log_error("Failed to parse reply.");
2272 dbus_message_iter_recurse(&iter, &sub);
2274 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
2275 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
2276 log_error("Failed to parse reply.");
2281 dbus_message_iter_recurse(&sub, &sub2);
2283 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
2286 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
2287 log_error("Failed to parse reply.");
2292 dbus_message_iter_get_basic(&sub2, &text);
2293 printf("%s\n", text);
2295 dbus_message_iter_next(&sub2);
2302 dbus_message_unref(m);
2305 dbus_message_unref(reply);
2307 dbus_error_free(&error);
2312 static int set_environment(DBusConnection *bus, char **args, unsigned n) {
2313 DBusMessage *m = NULL, *reply = NULL;
2317 DBusMessageIter iter, sub;
2320 dbus_error_init(&error);
2322 method = streq(args[0], "set-environment")
2324 : "UnsetEnvironment";
2326 if (!(m = dbus_message_new_method_call(
2327 "org.freedesktop.systemd1",
2328 "/org/freedesktop/systemd1",
2329 "org.freedesktop.systemd1.Manager",
2332 log_error("Could not allocate message.");
2336 dbus_message_iter_init_append(m, &iter);
2338 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
2339 log_error("Could not append arguments to message.");
2344 for (i = 1; i < n; i++)
2345 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &args[i])) {
2346 log_error("Could not append arguments to message.");
2351 if (!dbus_message_iter_close_container(&iter, &sub)) {
2352 log_error("Could not append arguments to message.");
2357 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2358 log_error("Failed to issue method call: %s", error.message);
2367 dbus_message_unref(m);
2370 dbus_message_unref(reply);
2372 dbus_error_free(&error);
2377 static int systemctl_help(void) {
2379 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
2380 "Send control commands to the systemd manager.\n\n"
2381 " -h --help Show this help\n"
2382 " -t --type=TYPE List only units of a particular type\n"
2383 " -p --property=NAME Show only properties by this name\n"
2384 " -a --all Show all units/properties, including dead/empty ones\n"
2385 " --replace When installing a new job, replace existing conflicting ones\n"
2386 " --system Connect to system bus\n"
2387 " --session Connect to session bus\n"
2388 " -q --quiet Suppress output\n"
2389 " --no-block Do not wait until operation finished\n"
2390 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
2392 " list-units List units\n"
2393 " start [NAME...] Start one or more units\n"
2394 " stop [NAME...] Stop one or more units\n"
2395 " restart [NAME...] Restart one or more units\n"
2396 " reload [NAME...] Reload one or more units\n"
2397 " isolate [NAME] Start one unit and stop all others\n"
2398 " check [NAME...] Check whether any of the passed units are active\n"
2399 " status [NAME...] Show status of one or more units\n"
2400 " show [NAME...|JOB...] Show properties of one or more units/jobs/manager\n"
2401 " load [NAME...] Load one or more units\n"
2402 " list-jobs List jobs\n"
2403 " cancel [JOB...] Cancel one or more jobs\n"
2404 " clear-jobs Cancel all jobs\n"
2405 " monitor Monitor unit/job changes\n"
2406 " dump Dump server status\n"
2407 " snapshot [NAME] Create a snapshot\n"
2408 " delete [NAME...] Remove one or more snapshots\n"
2409 " daemon-reload Reload systemd manager configuration\n"
2410 " daemon-reexec Reexecute systemd manager\n"
2411 " daemon-exit Ask the systemd manager to quit\n"
2412 " show-environment Dump environment\n"
2413 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
2414 " unset-environment [NAME...] Unset one or more environment variables\n"
2415 " halt Shut down and halt the system\n"
2416 " poweroff Shut down and power-off the system\n"
2417 " reboot Shut down and reboot the system\n"
2418 " default Enter default mode\n"
2419 " rescue Enter rescue mode\n"
2420 " emergency Enter emergency mode\n",
2421 program_invocation_short_name);
2426 static int halt_help(void) {
2428 printf("%s [OPTIONS...]\n\n"
2429 "%s the system.\n\n"
2430 " --help Show this help\n"
2431 " --halt Halt the machine\n"
2432 " -p --poweroff Switch off the machine\n"
2433 " --reboot Reboot the machine\n"
2434 " -f --force Force immediate halt/power-off/reboot\n"
2435 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
2436 " -d --no-wtmp Don't write wtmp record\n"
2437 " -n --no-sync Don't sync before halt/power-off/reboot\n"
2438 " --no-wall Don't send wall message before halt/power-off/reboot\n",
2439 program_invocation_short_name,
2440 arg_action == ACTION_REBOOT ? "Reboot" :
2441 arg_action == ACTION_POWEROFF ? "Power off" :
2447 static int shutdown_help(void) {
2449 printf("%s [OPTIONS...] [now] [WALL...]\n\n"
2450 "Shut down the system.\n\n"
2451 " --help Show this help\n"
2452 " -H --halt Halt the machine\n"
2453 " -P --poweroff Power-off the machine\n"
2454 " -r --reboot Reboot the machine\n"
2455 " -h Equivalent to --poweroff, overriden by --halt\n"
2456 " -k Don't halt/power-off/reboot, just send warnings\n"
2457 " --no-wall Don't send wall message before halt/power-off/reboot\n",
2458 program_invocation_short_name);
2463 static int telinit_help(void) {
2465 printf("%s [OPTIONS...] {COMMAND}\n\n"
2466 "Send control commands to the init daemon.\n\n"
2467 " --help Show this help\n"
2468 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
2470 " 0 Power-off the machine\n"
2471 " 6 Reboot the machine\n"
2472 " 2, 3, 4, 5 Start runlevelX.target unit\n"
2473 " 1, s, S Enter rescue mode\n"
2474 " q, Q Reload init daemon configuration\n"
2475 " u, U Reexecute init daemon\n",
2476 program_invocation_short_name);
2481 static int runlevel_help(void) {
2483 printf("%s [OPTIONS...]\n\n"
2484 "Prints the previous and current runlevel of the init system.\n\n"
2485 " --help Show this help\n",
2486 program_invocation_short_name);
2491 static int systemctl_parse_argv(int argc, char *argv[]) {
2494 ARG_REPLACE = 0x100,
2501 static const struct option options[] = {
2502 { "help", no_argument, NULL, 'h' },
2503 { "type", required_argument, NULL, 't' },
2504 { "property", required_argument, NULL, 'p' },
2505 { "all", no_argument, NULL, 'a' },
2506 { "replace", no_argument, NULL, ARG_REPLACE },
2507 { "session", no_argument, NULL, ARG_SESSION },
2508 { "system", no_argument, NULL, ARG_SYSTEM },
2509 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
2510 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2511 { "quiet", no_argument, NULL, 'q' },
2512 { NULL, 0, NULL, 0 }
2520 while ((c = getopt_long(argc, argv, "ht:p:aq", options, NULL)) >= 0) {
2533 arg_property = optarg;
2535 /* If the user asked for a particular
2536 * property, show it to him, even if it is
2554 arg_session = false;
2558 arg_no_block = true;
2573 log_error("Unknown option code %c", c);
2581 static int halt_parse_argv(int argc, char *argv[]) {
2590 static const struct option options[] = {
2591 { "help", no_argument, NULL, ARG_HELP },
2592 { "halt", no_argument, NULL, ARG_HALT },
2593 { "poweroff", no_argument, NULL, 'p' },
2594 { "reboot", no_argument, NULL, ARG_REBOOT },
2595 { "force", no_argument, NULL, 'f' },
2596 { "wtmp-only", no_argument, NULL, 'w' },
2597 { "no-wtmp", no_argument, NULL, 'd' },
2598 { "no-sync", no_argument, NULL, 'n' },
2599 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2600 { NULL, 0, NULL, 0 }
2608 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
2609 if (runlevel == '0' || runlevel == '6')
2610 arg_immediate = true;
2612 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
2620 arg_action = ACTION_HALT;
2624 arg_action = ACTION_POWEROFF;
2628 arg_action = ACTION_REBOOT;
2632 arg_immediate = true;
2653 /* Compatibility nops */
2660 log_error("Unknown option code %c", c);
2665 if (optind < argc) {
2666 log_error("Too many arguments.");
2673 static int shutdown_parse_argv(int argc, char *argv[]) {
2680 static const struct option options[] = {
2681 { "help", no_argument, NULL, ARG_HELP },
2682 { "halt", no_argument, NULL, 'H' },
2683 { "poweroff", no_argument, NULL, 'P' },
2684 { "reboot", no_argument, NULL, 'r' },
2685 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2686 { NULL, 0, NULL, 0 }
2694 while ((c = getopt_long(argc, argv, "HPrhkt:a", options, NULL)) >= 0) {
2702 arg_action = ACTION_HALT;
2706 arg_action = ACTION_POWEROFF;
2710 arg_action = ACTION_REBOOT;
2714 if (arg_action != ACTION_HALT)
2715 arg_action = ACTION_POWEROFF;
2728 /* Compatibility nops */
2735 log_error("Unknown option code %c", c);
2740 if (argc > optind && !streq(argv[optind], "now"))
2741 log_warning("First argument '%s' isn't 'now'. Ignoring.", argv[optind]);
2743 /* We ignore the time argument */
2744 if (argc > optind + 1)
2745 arg_wall = argv + optind + 1;
2752 static int telinit_parse_argv(int argc, char *argv[]) {
2759 static const struct option options[] = {
2760 { "help", no_argument, NULL, ARG_HELP },
2761 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2762 { NULL, 0, NULL, 0 }
2765 static const struct {
2769 { '0', ACTION_POWEROFF },
2770 { '6', ACTION_REBOOT },
2771 { '1', ACTION_RESCUE },
2772 { '2', ACTION_RUNLEVEL2 },
2773 { '3', ACTION_RUNLEVEL3 },
2774 { '4', ACTION_RUNLEVEL4 },
2775 { '5', ACTION_RUNLEVEL5 },
2776 { 's', ACTION_RESCUE },
2777 { 'S', ACTION_RESCUE },
2778 { 'q', ACTION_RELOAD },
2779 { 'Q', ACTION_RELOAD },
2780 { 'u', ACTION_REEXEC },
2781 { 'U', ACTION_REEXEC }
2790 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
2805 log_error("Unknown option code %c", c);
2810 if (optind >= argc) {
2815 if (optind + 1 < argc) {
2816 log_error("Too many arguments.");
2820 if (strlen(argv[optind]) != 1) {
2821 log_error("Expected single character argument.");
2825 for (i = 0; i < ELEMENTSOF(table); i++)
2826 if (table[i].from == argv[optind][0])
2829 if (i >= ELEMENTSOF(table)) {
2830 log_error("Unknown command %s.", argv[optind]);
2834 arg_action = table[i].to;
2841 static int runlevel_parse_argv(int argc, char *argv[]) {
2847 static const struct option options[] = {
2848 { "help", no_argument, NULL, ARG_HELP },
2849 { NULL, 0, NULL, 0 }
2857 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
2868 log_error("Unknown option code %c", c);
2873 if (optind < argc) {
2874 log_error("Too many arguments.");
2881 static int parse_argv(int argc, char *argv[]) {
2885 if (program_invocation_short_name) {
2887 if (strstr(program_invocation_short_name, "halt")) {
2888 arg_action = ACTION_HALT;
2889 return halt_parse_argv(argc, argv);
2890 } else if (strstr(program_invocation_short_name, "poweroff")) {
2891 arg_action = ACTION_POWEROFF;
2892 return halt_parse_argv(argc, argv);
2893 } else if (strstr(program_invocation_short_name, "reboot")) {
2894 arg_action = ACTION_REBOOT;
2895 return halt_parse_argv(argc, argv);
2896 } else if (strstr(program_invocation_short_name, "shutdown")) {
2897 arg_action = ACTION_POWEROFF;
2898 return shutdown_parse_argv(argc, argv);
2899 } else if (strstr(program_invocation_short_name, "init")) {
2900 arg_action = ACTION_INVALID;
2901 return telinit_parse_argv(argc, argv);
2902 } else if (strstr(program_invocation_short_name, "runlevel")) {
2903 arg_action = ACTION_RUNLEVEL;
2904 return runlevel_parse_argv(argc, argv);
2908 arg_action = ACTION_SYSTEMCTL;
2909 return systemctl_parse_argv(argc, argv);
2912 static int action_to_runlevel(void) {
2914 static const char table[_ACTION_MAX] = {
2915 [ACTION_HALT] = '0',
2916 [ACTION_POWEROFF] = '0',
2917 [ACTION_REBOOT] = '6',
2918 [ACTION_RUNLEVEL2] = '2',
2919 [ACTION_RUNLEVEL3] = '3',
2920 [ACTION_RUNLEVEL4] = '4',
2921 [ACTION_RUNLEVEL5] = '5',
2922 [ACTION_RESCUE] = '1'
2925 assert(arg_action < _ACTION_MAX);
2927 return table[arg_action];
2930 static int talk_upstart(void) {
2931 DBusMessage *m = NULL, *reply = NULL;
2933 int previous, rl, r;
2935 env1_buf[] = "RUNLEVEL=X",
2936 env2_buf[] = "PREVLEVEL=X";
2937 char *env1 = env1_buf, *env2 = env2_buf;
2938 const char *emit = "runlevel";
2939 dbus_bool_t b_false = FALSE;
2940 DBusMessageIter iter, sub;
2941 DBusConnection *bus;
2943 dbus_error_init(&error);
2945 if (!(rl = action_to_runlevel()))
2948 if (utmp_get_runlevel(&previous, NULL) < 0)
2951 if (!(bus = dbus_connection_open("unix:abstract=/com/ubuntu/upstart", &error))) {
2952 if (dbus_error_has_name(&error, DBUS_ERROR_NO_SERVER)) {
2957 log_error("Failed to connect to Upstart bus: %s", error.message);
2962 if ((r = bus_check_peercred(bus)) < 0) {
2963 log_error("Failed to verify owner of bus.");
2967 if (!(m = dbus_message_new_method_call(
2968 "com.ubuntu.Upstart",
2969 "/com/ubuntu/Upstart",
2970 "com.ubuntu.Upstart0_6",
2973 log_error("Could not allocate message.");
2978 dbus_message_iter_init_append(m, &iter);
2980 env1_buf[sizeof(env1_buf)-2] = rl;
2981 env2_buf[sizeof(env2_buf)-2] = previous;
2983 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
2984 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
2985 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
2986 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
2987 !dbus_message_iter_close_container(&iter, &sub) ||
2988 !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
2989 log_error("Could not append arguments to message.");
2994 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2996 if (error_is_no_service(&error)) {
3001 log_error("Failed to issue method call: %s", error.message);
3010 dbus_message_unref(m);
3013 dbus_message_unref(reply);
3016 dbus_connection_unref(bus);
3018 dbus_error_free(&error);
3023 static int talk_initctl(void) {
3024 struct init_request request;
3028 if (!(rl = action_to_runlevel()))
3032 request.magic = INIT_MAGIC;
3033 request.sleeptime = 0;
3034 request.cmd = INIT_CMD_RUNLVL;
3035 request.runlevel = rl;
3037 if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
3039 if (errno == ENOENT)
3042 log_error("Failed to open "INIT_FIFO": %m");
3047 r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
3048 close_nointr_nofail(fd);
3051 log_error("Failed to write to "INIT_FIFO": %m");
3052 return errno ? -errno : -EIO;
3058 static int systemctl_main(DBusConnection *bus, int argc, char *argv[]) {
3060 static const struct {
3068 int (* const dispatch)(DBusConnection *bus, char **args, unsigned n);
3070 { "list-units", LESS, 1, list_units },
3071 { "list-jobs", EQUAL, 1, list_jobs },
3072 { "clear-jobs", EQUAL, 1, clear_jobs },
3073 { "load", MORE, 2, load_unit },
3074 { "cancel", MORE, 2, cancel_job },
3075 { "start", MORE, 2, start_unit },
3076 { "stop", MORE, 2, start_unit },
3077 { "reload", MORE, 2, start_unit },
3078 { "restart", MORE, 2, start_unit },
3079 { "isolate", EQUAL, 2, start_unit },
3080 { "check", MORE, 2, check_unit },
3081 { "show", MORE, 1, show },
3082 { "status", MORE, 2, show },
3083 { "monitor", EQUAL, 1, monitor },
3084 { "dump", EQUAL, 1, dump },
3085 { "snapshot", LESS, 2, snapshot },
3086 { "delete", MORE, 2, delete_snapshot },
3087 { "daemon-reload", EQUAL, 1, clear_jobs },
3088 { "daemon-reexec", EQUAL, 1, clear_jobs },
3089 { "daemon-exit", EQUAL, 1, clear_jobs },
3090 { "show-environment", EQUAL, 1, show_enviroment },
3091 { "set-environment", MORE, 2, set_environment },
3092 { "unset-environment", MORE, 2, set_environment },
3093 { "halt", EQUAL, 1, start_special },
3094 { "poweroff", EQUAL, 1, start_special },
3095 { "reboot", EQUAL, 1, start_special },
3096 { "default", EQUAL, 1, start_special },
3097 { "rescue", EQUAL, 1, start_special },
3098 { "emergency", EQUAL, 1, start_special },
3108 left = argc - optind;
3111 /* Special rule: no arguments means "list-units" */
3114 if (streq(argv[optind], "help")) {
3119 for (i = 0; i < ELEMENTSOF(verbs); i++)
3120 if (streq(argv[optind], verbs[i].verb))
3123 if (i >= ELEMENTSOF(verbs)) {
3124 log_error("Unknown operation %s", argv[optind]);
3129 switch (verbs[i].argc_cmp) {
3132 if (left != verbs[i].argc) {
3133 log_error("Invalid number of arguments.");
3140 if (left < verbs[i].argc) {
3141 log_error("Too few arguments.");
3148 if (left > verbs[i].argc) {
3149 log_error("Too many arguments.");
3156 assert_not_reached("Unknown comparison operator.");
3159 return verbs[i].dispatch(bus, argv + optind, left);
3162 static int reload_with_fallback(DBusConnection *bus) {
3166 /* First, try systemd via D-Bus. */
3167 if ((r = clear_jobs(bus, NULL, 0)) > 0)
3171 /* Nothing else worked, so let's try signals */
3172 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
3174 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
3175 log_error("kill() failed: %m");
3182 static int start_with_fallback(DBusConnection *bus) {
3185 warn_wall(arg_action);
3188 /* First, try systemd via D-Bus. */
3189 if ((r = start_unit(bus, NULL, 0)) > 0)
3192 /* Hmm, talking to systemd via D-Bus didn't work. Then
3193 * let's try to talk to Upstart via D-Bus. */
3194 if ((r = talk_upstart()) > 0)
3198 /* Nothing else worked, so let's try
3200 if ((r = talk_initctl()) != 0)
3203 log_error("Failed to talk to init daemon.");
3207 static int halt_main(DBusConnection *bus) {
3211 return start_with_fallback(bus);
3214 if ((r = utmp_put_shutdown(0)) < 0)
3215 log_warning("Failed to write utmp record: %s", strerror(-r));
3223 /* Make sure C-A-D is handled by the kernel from this
3225 reboot(RB_ENABLE_CAD);
3227 switch (arg_action) {
3230 log_info("Halting");
3231 reboot(RB_HALT_SYSTEM);
3234 case ACTION_POWEROFF:
3235 log_info("Powering off");
3236 reboot(RB_POWER_OFF);
3240 log_info("Rebooting");
3241 reboot(RB_AUTOBOOT);
3245 assert_not_reached("Unknown halt action.");
3248 /* We should never reach this. */
3252 static int runlevel_main(void) {
3253 int r, runlevel, previous;
3255 if ((r = utmp_get_runlevel(&runlevel, &previous)) < 0) {
3261 previous <= 0 ? 'N' : previous,
3262 runlevel <= 0 ? 'N' : runlevel);
3267 int main(int argc, char*argv[]) {
3269 DBusConnection *bus = NULL;
3272 dbus_error_init(&error);
3274 log_parse_environment();
3276 if ((r = parse_argv(argc, argv)) < 0)
3283 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
3284 * let's shortcut this */
3285 if (arg_action == ACTION_RUNLEVEL) {
3286 retval = runlevel_main() < 0;
3290 bus_connect(arg_session ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &bus, &error);
3292 switch (arg_action) {
3294 case ACTION_SYSTEMCTL: {
3297 log_error("Failed to get D-Bus connection: %s", error.message);
3301 retval = systemctl_main(bus, argc, argv) < 0;
3306 case ACTION_POWEROFF:
3308 retval = halt_main(bus) < 0;
3311 case ACTION_RUNLEVEL2:
3312 case ACTION_RUNLEVEL3:
3313 case ACTION_RUNLEVEL4:
3314 case ACTION_RUNLEVEL5:
3316 case ACTION_EMERGENCY:
3317 case ACTION_DEFAULT:
3318 retval = start_with_fallback(bus) < 0;
3323 retval = reload_with_fallback(bus) < 0;
3326 case ACTION_INVALID:
3327 case ACTION_RUNLEVEL:
3329 assert_not_reached("Unknown action");
3335 dbus_connection_unref(bus);
3337 dbus_error_free(&error);