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)
958 if (fscanf(f, "%lu", &ul) != 1)
966 get_process_cmdline(last, 60, &t);
967 printf("\t\t \342\224\234 %lu %s\n", (unsigned long) last, strna(t));
970 printf("\t\t \342\224\202\n");
977 get_process_cmdline(last, 60, &t);
978 printf("\t\t \342\224\224 %lu %s\n", (unsigned long) last, strna(t));
985 typedef struct UnitStatusInfo {
987 const char *load_state;
988 const char *active_state;
989 const char *sub_state;
991 const char *description;
993 const char *fragment_path;
994 const char *default_control_group;
999 const char *status_text;
1002 usec_t start_timestamp;
1003 usec_t exit_timestamp;
1005 int exit_code, exit_status;
1008 unsigned n_accepted;
1009 unsigned n_connections;
1012 const char *sysfs_path;
1014 /* Mount, Automount */
1021 static void print_status_info(UnitStatusInfo *i) {
1024 /* This shows pretty information about a unit. See
1025 * print_property() for a low-level property printer */
1027 printf("%s", strna(i->id));
1029 if (i->description && !streq_ptr(i->id, i->description))
1030 printf(" - %s", i->description);
1034 if (i->fragment_path)
1035 printf("\t Loaded: %s (%s)\n", strna(i->load_state), i->fragment_path);
1036 else if (streq_ptr(i->load_state, "failed"))
1037 printf("\t Loaded: " ANSI_HIGHLIGHT_ON "%s" ANSI_HIGHLIGHT_OFF "\n", strna(i->load_state));
1039 printf("\t Loaded: %s\n", strna(i->load_state));
1041 if (streq_ptr(i->active_state, "maintenance"))
1042 printf("\t Active: " ANSI_HIGHLIGHT_ON "%s (%s)" ANSI_HIGHLIGHT_OFF "\n",
1043 strna(i->active_state),
1044 strna(i->sub_state));
1046 printf("\t Active: %s (%s)\n",
1047 strna(i->active_state),
1048 strna(i->sub_state));
1051 printf("\t Device: %s\n", i->sysfs_path);
1053 printf("\t Where: %s\n", i->where);
1055 printf("\t What: %s\n", i->what);
1058 printf("\t Status: \"%s\"\n", i->status_text);
1060 if (i->id && endswith(i->id, ".socket"))
1061 printf("\tAccepted: %u; Connected: %u\n", i->n_accepted, i->n_connections);
1063 if (i->main_pid > 0 || i->control_pid > 0) {
1066 if (i->main_pid > 0) {
1067 printf(" Process: %u", (unsigned) i->main_pid);
1071 get_process_name(i->main_pid, &t);
1077 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
1079 if (i->exit_code == CLD_EXITED)
1080 printf("status=%i", i->exit_status);
1082 printf("signal=%s", strsignal(i->exit_status));
1087 if (i->main_pid > 0 && i->control_pid > 0)
1090 if (i->control_pid > 0) {
1093 printf(" Control: %u", (unsigned) i->control_pid);
1095 get_process_name(i->control_pid, &t);
1105 if (i->default_control_group) {
1106 printf("\t CGroup: %s\n", i->default_control_group);
1107 show_cgroup(i->default_control_group);
1111 static int status_property(const char *name, DBusMessageIter *iter, UnitStatusInfo *i) {
1113 switch (dbus_message_iter_get_arg_type(iter)) {
1115 case DBUS_TYPE_STRING: {
1118 dbus_message_iter_get_basic(iter, &s);
1121 if (streq(name, "Id"))
1123 else if (streq(name, "LoadState"))
1125 else if (streq(name, "ActiveState"))
1126 i->active_state = s;
1127 else if (streq(name, "SubState"))
1129 else if (streq(name, "Description"))
1131 else if (streq(name, "FragmentPath"))
1132 i->fragment_path = s;
1133 else if (streq(name, "DefaultControlGroup"))
1134 i->default_control_group = s;
1135 else if (streq(name, "StatusText"))
1137 else if (streq(name, "SysFSPath"))
1139 else if (streq(name, "Where"))
1141 else if (streq(name, "What"))
1148 case DBUS_TYPE_UINT32: {
1151 dbus_message_iter_get_basic(iter, &u);
1153 if (streq(name, "MainPID")) {
1155 i->main_pid = (pid_t) u;
1158 } else if (streq(name, "ControlPID"))
1159 i->control_pid = (pid_t) u;
1160 else if (streq(name, "ExecMainPID")) {
1162 i->main_pid = (pid_t) u;
1163 } else if (streq(name, "NAccepted"))
1165 else if (streq(name, "NConnections"))
1166 i->n_connections = u;
1171 case DBUS_TYPE_INT32: {
1174 dbus_message_iter_get_basic(iter, &j);
1176 if (streq(name, "ExecMainCode"))
1177 i->exit_code = (int) j;
1178 else if (streq(name, "ExecMainStatus"))
1179 i->exit_status = (int) j;
1184 case DBUS_TYPE_UINT64: {
1187 dbus_message_iter_get_basic(iter, &u);
1189 if (streq(name, "ExecMainStartTimestamp"))
1190 i->start_timestamp = (usec_t) u;
1191 else if (streq(name, "ExecMainExitTimestamp"))
1192 i->exit_timestamp = (usec_t) u;
1201 static int print_property(const char *name, DBusMessageIter *iter) {
1205 /* This is a low-level property printer, see
1206 * print_status_info() for the nicer output */
1208 if (arg_property && !streq(name, arg_property))
1211 switch (dbus_message_iter_get_arg_type(iter)) {
1213 case DBUS_TYPE_STRING: {
1215 dbus_message_iter_get_basic(iter, &s);
1217 if (arg_all || s[0])
1218 printf("%s=%s\n", name, s);
1223 case DBUS_TYPE_BOOLEAN: {
1225 dbus_message_iter_get_basic(iter, &b);
1226 printf("%s=%s\n", name, yes_no(b));
1231 case DBUS_TYPE_UINT64: {
1233 dbus_message_iter_get_basic(iter, &u);
1235 /* Yes, heuristics! But we can change this check
1236 * should it turn out to not be sufficient */
1238 if (strstr(name, "Timestamp")) {
1239 char timestamp[FORMAT_TIMESTAMP_MAX], *t;
1241 if ((t = format_timestamp(timestamp, sizeof(timestamp), u)) || arg_all)
1242 printf("%s=%s\n", name, strempty(t));
1243 } else if (strstr(name, "USec")) {
1244 char timespan[FORMAT_TIMESPAN_MAX];
1246 printf("%s=%s\n", name, format_timespan(timespan, sizeof(timespan), u));
1248 printf("%s=%llu\n", name, (unsigned long long) u);
1253 case DBUS_TYPE_UINT32: {
1255 dbus_message_iter_get_basic(iter, &u);
1257 if (strstr(name, "UMask") || strstr(name, "Mode"))
1258 printf("%s=%04o\n", name, u);
1260 printf("%s=%u\n", name, (unsigned) u);
1265 case DBUS_TYPE_INT32: {
1267 dbus_message_iter_get_basic(iter, &i);
1269 printf("%s=%i\n", name, (int) i);
1273 case DBUS_TYPE_STRUCT: {
1274 DBusMessageIter sub;
1275 dbus_message_iter_recurse(iter, &sub);
1277 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "Job")) {
1280 dbus_message_iter_get_basic(&sub, &u);
1283 printf("%s=%u\n", name, (unsigned) u);
1285 printf("%s=\n", name);
1288 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Unit")) {
1291 dbus_message_iter_get_basic(&sub, &s);
1293 if (arg_all || s[0])
1294 printf("%s=%s\n", name, s);
1302 case DBUS_TYPE_ARRAY:
1304 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
1305 DBusMessageIter sub;
1308 dbus_message_iter_recurse(iter, &sub);
1311 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1312 printf("%s=", name);
1314 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1317 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
1318 dbus_message_iter_get_basic(&sub, &s);
1319 printf("%s%s", space ? " " : "", s);
1322 dbus_message_iter_next(&sub);
1329 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_BYTE) {
1330 DBusMessageIter sub;
1332 dbus_message_iter_recurse(iter, &sub);
1335 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1336 printf("%s=", name);
1338 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1341 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_BYTE);
1342 dbus_message_iter_get_basic(&sub, &u);
1345 dbus_message_iter_next(&sub);
1352 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Paths")) {
1353 DBusMessageIter sub, sub2;
1355 dbus_message_iter_recurse(iter, &sub);
1357 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1358 const char *type, *path;
1360 dbus_message_iter_recurse(&sub, &sub2);
1362 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
1363 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0)
1364 printf("%s=%s\n", type, path);
1366 dbus_message_iter_next(&sub);
1370 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Timers")) {
1371 DBusMessageIter sub, sub2;
1373 dbus_message_iter_recurse(iter, &sub);
1375 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1377 uint64_t value, next_elapse;
1379 dbus_message_iter_recurse(&sub, &sub2);
1381 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &base, true) >= 0 &&
1382 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &value, true) >= 0 &&
1383 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &next_elapse, false) >= 0) {
1384 char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX];
1386 printf("%s={ value=%s ; next_elapse=%s }\n",
1388 format_timespan(timespan1, sizeof(timespan1), value),
1389 format_timespan(timespan2, sizeof(timespan2), next_elapse));
1392 dbus_message_iter_next(&sub);
1396 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && startswith(name, "Exec")) {
1398 DBusMessageIter sub, sub2, sub3;
1400 dbus_message_iter_recurse(iter, &sub);
1402 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1404 uint64_t start_time, exit_time;
1406 int32_t code, status;
1408 dbus_message_iter_recurse(&sub, &sub2);
1410 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0)
1413 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_ARRAY ||
1414 dbus_message_iter_get_element_type(&sub2) != DBUS_TYPE_STRING)
1417 printf("%s={ path=%s ; argv[]=", name, path);
1419 dbus_message_iter_recurse(&sub2, &sub3);
1421 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1424 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1425 dbus_message_iter_get_basic(&sub3, &s);
1427 dbus_message_iter_next(&sub3);
1430 if (dbus_message_iter_next(&sub2) &&
1431 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_time, true) >= 0 &&
1432 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_time, true) >= 0 &&
1433 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, true) >= 0 &&
1434 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &code, true) >= 0 &&
1435 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &status, false) >= 0) {
1437 char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
1439 printf("; start=%s ; stop=%s ; pid=%u ; code=%s ; status=%i/%s",
1440 strna(format_timestamp(timestamp1, sizeof(timestamp1), start_time)),
1441 strna(format_timestamp(timestamp2, sizeof(timestamp2), exit_time)),
1443 sigchld_code_to_string(code),
1445 strempty(code == CLD_EXITED ? NULL : strsignal(status)));
1450 dbus_message_iter_next(&sub);
1460 printf("%s=[unprintable]\n", name);
1465 static int show_one(DBusConnection *bus, const char *path, bool show_properties, bool *new_line) {
1466 DBusMessage *m = NULL, *reply = NULL;
1467 const char *interface = "";
1470 DBusMessageIter iter, sub, sub2, sub3;
1471 UnitStatusInfo info;
1478 dbus_error_init(&error);
1480 if (!(m = dbus_message_new_method_call(
1481 "org.freedesktop.systemd1",
1483 "org.freedesktop.DBus.Properties",
1485 log_error("Could not allocate message.");
1490 if (!dbus_message_append_args(m,
1491 DBUS_TYPE_STRING, &interface,
1492 DBUS_TYPE_INVALID)) {
1493 log_error("Could not append arguments to message.");
1498 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1499 log_error("Failed to issue method call: %s", error.message);
1504 if (!dbus_message_iter_init(reply, &iter) ||
1505 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
1506 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
1507 log_error("Failed to parse reply.");
1512 dbus_message_iter_recurse(&iter, &sub);
1519 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1522 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
1523 log_error("Failed to parse reply.");
1528 dbus_message_iter_recurse(&sub, &sub2);
1530 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
1531 log_error("Failed to parse reply.");
1536 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
1537 log_error("Failed to parse reply.");
1542 dbus_message_iter_recurse(&sub2, &sub3);
1544 if (show_properties)
1545 r = print_property(name, &sub3);
1547 r = status_property(name, &sub3, &info);
1550 log_error("Failed to parse reply.");
1555 dbus_message_iter_next(&sub);
1558 if (!show_properties)
1559 print_status_info(&info);
1565 dbus_message_unref(m);
1568 dbus_message_unref(reply);
1570 dbus_error_free(&error);
1575 static int show(DBusConnection *bus, char **args, unsigned n) {
1576 DBusMessage *m = NULL, *reply = NULL;
1580 bool show_properties, new_line = false;
1585 dbus_error_init(&error);
1587 show_properties = !streq(args[0], "status");
1589 if (show_properties && n <= 1) {
1590 /* If not argument is specified inspect the manager
1593 r = show_one(bus, "/org/freedesktop/systemd1", show_properties, &new_line);
1597 for (i = 1; i < n; i++) {
1598 const char *path = NULL;
1601 if (!show_properties || safe_atou32(args[i], &id) < 0) {
1603 if (!(m = dbus_message_new_method_call(
1604 "org.freedesktop.systemd1",
1605 "/org/freedesktop/systemd1",
1606 "org.freedesktop.systemd1.Manager",
1608 log_error("Could not allocate message.");
1613 if (!dbus_message_append_args(m,
1614 DBUS_TYPE_STRING, &args[i],
1615 DBUS_TYPE_INVALID)) {
1616 log_error("Could not append arguments to message.");
1621 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1623 if (!dbus_error_has_name(&error, DBUS_ERROR_ACCESS_DENIED)) {
1624 log_error("Failed to issue method call: %s", error.message);
1629 dbus_error_free(&error);
1631 dbus_message_unref(m);
1632 if (!(m = dbus_message_new_method_call(
1633 "org.freedesktop.systemd1",
1634 "/org/freedesktop/systemd1",
1635 "org.freedesktop.systemd1.Manager",
1637 log_error("Could not allocate message.");
1642 if (!dbus_message_append_args(m,
1643 DBUS_TYPE_STRING, &args[i],
1644 DBUS_TYPE_INVALID)) {
1645 log_error("Could not append arguments to message.");
1650 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1651 log_error("Failed to issue method call: %s", error.message);
1659 if (!(m = dbus_message_new_method_call(
1660 "org.freedesktop.systemd1",
1661 "/org/freedesktop/systemd1",
1662 "org.freedesktop.systemd1.Manager",
1664 log_error("Could not allocate message.");
1669 if (!dbus_message_append_args(m,
1670 DBUS_TYPE_UINT32, &id,
1671 DBUS_TYPE_INVALID)) {
1672 log_error("Could not append arguments to message.");
1677 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1678 log_error("Failed to issue method call: %s", error.message);
1684 if (!dbus_message_get_args(reply, &error,
1685 DBUS_TYPE_OBJECT_PATH, &path,
1686 DBUS_TYPE_INVALID)) {
1687 log_error("Failed to parse reply: %s", error.message);
1692 if ((r = show_one(bus, path, show_properties, &new_line)) < 0)
1695 dbus_message_unref(m);
1696 dbus_message_unref(reply);
1704 dbus_message_unref(m);
1707 dbus_message_unref(reply);
1709 dbus_error_free(&error);
1714 static DBusHandlerResult monitor_filter(DBusConnection *connection, DBusMessage *message, void *data) {
1716 DBusMessage *m = NULL, *reply = NULL;
1721 dbus_error_init(&error);
1723 /* log_debug("Got D-Bus request: %s.%s() on %s", */
1724 /* dbus_message_get_interface(message), */
1725 /* dbus_message_get_member(message), */
1726 /* dbus_message_get_path(message)); */
1728 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
1729 log_error("Warning! D-Bus connection terminated.");
1730 dbus_connection_close(connection);
1732 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitNew") ||
1733 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitRemoved")) {
1734 const char *id, *path;
1736 if (!dbus_message_get_args(message, &error,
1737 DBUS_TYPE_STRING, &id,
1738 DBUS_TYPE_OBJECT_PATH, &path,
1740 log_error("Failed to parse message: %s", error.message);
1741 else if (streq(dbus_message_get_member(message), "UnitNew"))
1742 printf("Unit %s added.\n", id);
1744 printf("Unit %s removed.\n", id);
1746 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobNew") ||
1747 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
1751 if (!dbus_message_get_args(message, &error,
1752 DBUS_TYPE_UINT32, &id,
1753 DBUS_TYPE_OBJECT_PATH, &path,
1755 log_error("Failed to parse message: %s", error.message);
1756 else if (streq(dbus_message_get_member(message), "JobNew"))
1757 printf("Job %u added.\n", id);
1759 printf("Job %u removed.\n", id);
1762 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Unit", "Changed") ||
1763 dbus_message_is_signal(message, "org.freedesktop.systemd1.Job", "Changed")) {
1765 const char *path, *interface, *property = "Id";
1766 DBusMessageIter iter, sub;
1768 path = dbus_message_get_path(message);
1769 interface = dbus_message_get_interface(message);
1771 if (!(m = dbus_message_new_method_call(
1772 "org.freedesktop.systemd1",
1774 "org.freedesktop.DBus.Properties",
1776 log_error("Could not allocate message.");
1780 if (!dbus_message_append_args(m,
1781 DBUS_TYPE_STRING, &interface,
1782 DBUS_TYPE_STRING, &property,
1783 DBUS_TYPE_INVALID)) {
1784 log_error("Could not append arguments to message.");
1788 if (!(reply = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) {
1789 log_error("Failed to issue method call: %s", error.message);
1793 if (!dbus_message_iter_init(reply, &iter) ||
1794 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1795 log_error("Failed to parse reply.");
1799 dbus_message_iter_recurse(&iter, &sub);
1801 if (streq(interface, "org.freedesktop.systemd1.Unit")) {
1804 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1805 log_error("Failed to parse reply.");
1809 dbus_message_iter_get_basic(&sub, &id);
1810 printf("Unit %s changed.\n", id);
1814 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT32) {
1815 log_error("Failed to parse reply.");
1819 dbus_message_iter_get_basic(&sub, &id);
1820 printf("Job %u changed.\n", id);
1826 dbus_message_unref(m);
1829 dbus_message_unref(reply);
1831 dbus_error_free(&error);
1832 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1836 dbus_message_unref(m);
1839 dbus_message_unref(reply);
1841 dbus_error_free(&error);
1842 return DBUS_HANDLER_RESULT_NEED_MEMORY;
1845 static int monitor(DBusConnection *bus, char **args, unsigned n) {
1846 DBusMessage *m = NULL, *reply = NULL;
1850 dbus_error_init(&error);
1852 dbus_bus_add_match(bus,
1854 "sender='org.freedesktop.systemd1',"
1855 "interface='org.freedesktop.systemd1.Manager',"
1856 "path='/org/freedesktop/systemd1'",
1859 if (dbus_error_is_set(&error)) {
1860 log_error("Failed to add match: %s", error.message);
1865 dbus_bus_add_match(bus,
1867 "sender='org.freedesktop.systemd1',"
1868 "interface='org.freedesktop.systemd1.Unit',"
1872 if (dbus_error_is_set(&error)) {
1873 log_error("Failed to add match: %s", error.message);
1878 dbus_bus_add_match(bus,
1880 "sender='org.freedesktop.systemd1',"
1881 "interface='org.freedesktop.systemd1.Job',"
1885 if (dbus_error_is_set(&error)) {
1886 log_error("Failed to add match: %s", error.message);
1891 if (!dbus_connection_add_filter(bus, monitor_filter, NULL, NULL)) {
1892 log_error("Failed to add filter.");
1897 if (!(m = dbus_message_new_method_call(
1898 "org.freedesktop.systemd1",
1899 "/org/freedesktop/systemd1",
1900 "org.freedesktop.systemd1.Manager",
1902 log_error("Could not allocate message.");
1907 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1908 log_error("Failed to issue method call: %s", error.message);
1913 while (dbus_connection_read_write_dispatch(bus, -1))
1920 /* This is slightly dirty, since we don't undo the filter or the matches. */
1923 dbus_message_unref(m);
1926 dbus_message_unref(reply);
1928 dbus_error_free(&error);
1933 static int dump(DBusConnection *bus, char **args, unsigned n) {
1934 DBusMessage *m = NULL, *reply = NULL;
1939 dbus_error_init(&error);
1941 if (!(m = dbus_message_new_method_call(
1942 "org.freedesktop.systemd1",
1943 "/org/freedesktop/systemd1",
1944 "org.freedesktop.systemd1.Manager",
1946 log_error("Could not allocate message.");
1950 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1951 log_error("Failed to issue method call: %s", error.message);
1956 if (!dbus_message_get_args(reply, &error,
1957 DBUS_TYPE_STRING, &text,
1958 DBUS_TYPE_INVALID)) {
1959 log_error("Failed to parse reply: %s", error.message);
1964 fputs(text, stdout);
1970 dbus_message_unref(m);
1973 dbus_message_unref(reply);
1975 dbus_error_free(&error);
1980 static int snapshot(DBusConnection *bus, char **args, unsigned n) {
1981 DBusMessage *m = NULL, *reply = NULL;
1984 const char *name = "", *path, *id;
1985 dbus_bool_t cleanup = FALSE;
1986 DBusMessageIter iter, sub;
1988 *interface = "org.freedesktop.systemd1.Unit",
1991 dbus_error_init(&error);
1993 if (!(m = dbus_message_new_method_call(
1994 "org.freedesktop.systemd1",
1995 "/org/freedesktop/systemd1",
1996 "org.freedesktop.systemd1.Manager",
1997 "CreateSnapshot"))) {
1998 log_error("Could not allocate message.");
2005 if (!dbus_message_append_args(m,
2006 DBUS_TYPE_STRING, &name,
2007 DBUS_TYPE_BOOLEAN, &cleanup,
2008 DBUS_TYPE_INVALID)) {
2009 log_error("Could not append arguments to message.");
2014 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2015 log_error("Failed to issue method call: %s", error.message);
2020 if (!dbus_message_get_args(reply, &error,
2021 DBUS_TYPE_OBJECT_PATH, &path,
2022 DBUS_TYPE_INVALID)) {
2023 log_error("Failed to parse reply: %s", error.message);
2028 dbus_message_unref(m);
2029 if (!(m = dbus_message_new_method_call(
2030 "org.freedesktop.systemd1",
2032 "org.freedesktop.DBus.Properties",
2034 log_error("Could not allocate message.");
2038 if (!dbus_message_append_args(m,
2039 DBUS_TYPE_STRING, &interface,
2040 DBUS_TYPE_STRING, &property,
2041 DBUS_TYPE_INVALID)) {
2042 log_error("Could not append arguments to message.");
2047 dbus_message_unref(reply);
2048 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2049 log_error("Failed to issue method call: %s", error.message);
2054 if (!dbus_message_iter_init(reply, &iter) ||
2055 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2056 log_error("Failed to parse reply.");
2061 dbus_message_iter_recurse(&iter, &sub);
2063 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2064 log_error("Failed to parse reply.");
2069 dbus_message_iter_get_basic(&sub, &id);
2077 dbus_message_unref(m);
2080 dbus_message_unref(reply);
2082 dbus_error_free(&error);
2087 static int delete_snapshot(DBusConnection *bus, char **args, unsigned n) {
2088 DBusMessage *m = NULL, *reply = NULL;
2096 dbus_error_init(&error);
2098 for (i = 1; i < n; i++) {
2099 const char *path = NULL;
2101 if (!(m = dbus_message_new_method_call(
2102 "org.freedesktop.systemd1",
2103 "/org/freedesktop/systemd1",
2104 "org.freedesktop.systemd1.Manager",
2106 log_error("Could not allocate message.");
2111 if (!dbus_message_append_args(m,
2112 DBUS_TYPE_STRING, &args[i],
2113 DBUS_TYPE_INVALID)) {
2114 log_error("Could not append arguments to message.");
2119 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2120 log_error("Failed to issue method call: %s", error.message);
2125 if (!dbus_message_get_args(reply, &error,
2126 DBUS_TYPE_OBJECT_PATH, &path,
2127 DBUS_TYPE_INVALID)) {
2128 log_error("Failed to parse reply: %s", error.message);
2133 dbus_message_unref(m);
2134 if (!(m = dbus_message_new_method_call(
2135 "org.freedesktop.systemd1",
2137 "org.freedesktop.systemd1.Snapshot",
2139 log_error("Could not allocate message.");
2144 dbus_message_unref(reply);
2145 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2146 log_error("Failed to issue method call: %s", error.message);
2151 dbus_message_unref(m);
2152 dbus_message_unref(reply);
2160 dbus_message_unref(m);
2163 dbus_message_unref(reply);
2165 dbus_error_free(&error);
2170 static int clear_jobs(DBusConnection *bus, char **args, unsigned n) {
2171 DBusMessage *m = NULL, *reply = NULL;
2176 dbus_error_init(&error);
2178 if (arg_action == ACTION_RELOAD)
2180 else if (arg_action == ACTION_REEXEC)
2181 method = "Reexecute";
2183 assert(arg_action == ACTION_SYSTEMCTL);
2186 streq(args[0], "clear-jobs") ? "ClearJobs" :
2187 streq(args[0], "daemon-reload") ? "Reload" :
2188 streq(args[0], "daemon-reexec") ? "Reexecute" :
2192 if (!(m = dbus_message_new_method_call(
2193 "org.freedesktop.systemd1",
2194 "/org/freedesktop/systemd1",
2195 "org.freedesktop.systemd1.Manager",
2197 log_error("Could not allocate message.");
2201 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2203 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
2204 /* There's always a fallback possible for
2205 * legacy actions. */
2210 log_error("Failed to issue method call: %s", error.message);
2219 dbus_message_unref(m);
2222 dbus_message_unref(reply);
2224 dbus_error_free(&error);
2229 static int show_enviroment(DBusConnection *bus, char **args, unsigned n) {
2230 DBusMessage *m = NULL, *reply = NULL;
2232 DBusMessageIter iter, sub, sub2;
2235 *interface = "org.freedesktop.systemd1.Manager",
2236 *property = "Environment";
2238 dbus_error_init(&error);
2240 if (!(m = dbus_message_new_method_call(
2241 "org.freedesktop.systemd1",
2242 "/org/freedesktop/systemd1",
2243 "org.freedesktop.DBus.Properties",
2245 log_error("Could not allocate message.");
2249 if (!dbus_message_append_args(m,
2250 DBUS_TYPE_STRING, &interface,
2251 DBUS_TYPE_STRING, &property,
2252 DBUS_TYPE_INVALID)) {
2253 log_error("Could not append arguments to message.");
2258 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2259 log_error("Failed to issue method call: %s", error.message);
2264 if (!dbus_message_iter_init(reply, &iter) ||
2265 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2266 log_error("Failed to parse reply.");
2271 dbus_message_iter_recurse(&iter, &sub);
2273 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
2274 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
2275 log_error("Failed to parse reply.");
2280 dbus_message_iter_recurse(&sub, &sub2);
2282 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
2285 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
2286 log_error("Failed to parse reply.");
2291 dbus_message_iter_get_basic(&sub2, &text);
2292 printf("%s\n", text);
2294 dbus_message_iter_next(&sub2);
2301 dbus_message_unref(m);
2304 dbus_message_unref(reply);
2306 dbus_error_free(&error);
2311 static int set_environment(DBusConnection *bus, char **args, unsigned n) {
2312 DBusMessage *m = NULL, *reply = NULL;
2316 DBusMessageIter iter, sub;
2319 dbus_error_init(&error);
2321 method = streq(args[0], "set-environment")
2323 : "UnsetEnvironment";
2325 if (!(m = dbus_message_new_method_call(
2326 "org.freedesktop.systemd1",
2327 "/org/freedesktop/systemd1",
2328 "org.freedesktop.systemd1.Manager",
2331 log_error("Could not allocate message.");
2335 dbus_message_iter_init_append(m, &iter);
2337 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
2338 log_error("Could not append arguments to message.");
2343 for (i = 1; i < n; i++)
2344 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &args[i])) {
2345 log_error("Could not append arguments to message.");
2350 if (!dbus_message_iter_close_container(&iter, &sub)) {
2351 log_error("Could not append arguments to message.");
2356 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2357 log_error("Failed to issue method call: %s", error.message);
2366 dbus_message_unref(m);
2369 dbus_message_unref(reply);
2371 dbus_error_free(&error);
2376 static int systemctl_help(void) {
2378 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
2379 "Send control commands to the systemd manager.\n\n"
2380 " -h --help Show this help\n"
2381 " -t --type=TYPE List only units of a particular type\n"
2382 " -p --property=NAME Show only properties by this name\n"
2383 " -a --all Show all units/properties, including dead/empty ones\n"
2384 " --replace When installing a new job, replace existing conflicting ones\n"
2385 " --system Connect to system bus\n"
2386 " --session Connect to session bus\n"
2387 " -q --quiet Suppress output\n"
2388 " --no-block Do not wait until operation finished\n"
2389 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
2391 " list-units List units\n"
2392 " start [NAME...] Start one or more units\n"
2393 " stop [NAME...] Stop one or more units\n"
2394 " restart [NAME...] Restart one or more units\n"
2395 " reload [NAME...] Reload one or more units\n"
2396 " isolate [NAME] Start one unit and stop all others\n"
2397 " check [NAME...] Check whether any of the passed units are active\n"
2398 " status [NAME...] Show status of one or more units\n"
2399 " show [NAME...|JOB...] Show properties of one or more units/jobs/manager\n"
2400 " load [NAME...] Load one or more units\n"
2401 " list-jobs List jobs\n"
2402 " cancel [JOB...] Cancel one or more jobs\n"
2403 " clear-jobs Cancel all jobs\n"
2404 " monitor Monitor unit/job changes\n"
2405 " dump Dump server status\n"
2406 " snapshot [NAME] Create a snapshot\n"
2407 " delete [NAME...] Remove one or more snapshots\n"
2408 " daemon-reload Reload systemd manager configuration\n"
2409 " daemon-reexec Reexecute systemd manager\n"
2410 " daemon-exit Ask the systemd manager to quit\n"
2411 " show-environment Dump environment\n"
2412 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
2413 " unset-environment [NAME...] Unset one or more environment variables\n"
2414 " halt Shut down and halt the system\n"
2415 " poweroff Shut down and power-off the system\n"
2416 " reboot Shut down and reboot the system\n"
2417 " default Enter default mode\n"
2418 " rescue Enter rescue mode\n"
2419 " emergency Enter emergency mode\n",
2420 program_invocation_short_name);
2425 static int halt_help(void) {
2427 printf("%s [OPTIONS...]\n\n"
2428 "%s the system.\n\n"
2429 " --help Show this help\n"
2430 " --halt Halt the machine\n"
2431 " -p --poweroff Switch off the machine\n"
2432 " --reboot Reboot the machine\n"
2433 " -f --force Force immediate halt/power-off/reboot\n"
2434 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
2435 " -d --no-wtmp Don't write wtmp record\n"
2436 " -n --no-sync Don't sync before halt/power-off/reboot\n"
2437 " --no-wall Don't send wall message before halt/power-off/reboot\n",
2438 program_invocation_short_name,
2439 arg_action == ACTION_REBOOT ? "Reboot" :
2440 arg_action == ACTION_POWEROFF ? "Power off" :
2446 static int shutdown_help(void) {
2448 printf("%s [OPTIONS...] [now] [WALL...]\n\n"
2449 "Shut down the system.\n\n"
2450 " --help Show this help\n"
2451 " -H --halt Halt the machine\n"
2452 " -P --poweroff Power-off the machine\n"
2453 " -r --reboot Reboot the machine\n"
2454 " -h Equivalent to --poweroff, overriden by --halt\n"
2455 " -k Don't halt/power-off/reboot, just send warnings\n"
2456 " --no-wall Don't send wall message before halt/power-off/reboot\n",
2457 program_invocation_short_name);
2462 static int telinit_help(void) {
2464 printf("%s [OPTIONS...] {COMMAND}\n\n"
2465 "Send control commands to the init daemon.\n\n"
2466 " --help Show this help\n"
2467 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
2469 " 0 Power-off the machine\n"
2470 " 6 Reboot the machine\n"
2471 " 2, 3, 4, 5 Start runlevelX.target unit\n"
2472 " 1, s, S Enter rescue mode\n"
2473 " q, Q Reload init daemon configuration\n"
2474 " u, U Reexecute init daemon\n",
2475 program_invocation_short_name);
2480 static int runlevel_help(void) {
2482 printf("%s [OPTIONS...]\n\n"
2483 "Prints the previous and current runlevel of the init system.\n\n"
2484 " --help Show this help\n",
2485 program_invocation_short_name);
2490 static int systemctl_parse_argv(int argc, char *argv[]) {
2493 ARG_REPLACE = 0x100,
2500 static const struct option options[] = {
2501 { "help", no_argument, NULL, 'h' },
2502 { "type", required_argument, NULL, 't' },
2503 { "property", required_argument, NULL, 'p' },
2504 { "all", no_argument, NULL, 'a' },
2505 { "replace", no_argument, NULL, ARG_REPLACE },
2506 { "session", no_argument, NULL, ARG_SESSION },
2507 { "system", no_argument, NULL, ARG_SYSTEM },
2508 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
2509 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2510 { "quiet", no_argument, NULL, 'q' },
2511 { NULL, 0, NULL, 0 }
2519 while ((c = getopt_long(argc, argv, "ht:p:aq", options, NULL)) >= 0) {
2532 arg_property = optarg;
2534 /* If the user asked for a particular
2535 * property, show it to him, even if it is
2553 arg_session = false;
2557 arg_no_block = true;
2572 log_error("Unknown option code %c", c);
2580 static int halt_parse_argv(int argc, char *argv[]) {
2589 static const struct option options[] = {
2590 { "help", no_argument, NULL, ARG_HELP },
2591 { "halt", no_argument, NULL, ARG_HALT },
2592 { "poweroff", no_argument, NULL, 'p' },
2593 { "reboot", no_argument, NULL, ARG_REBOOT },
2594 { "force", no_argument, NULL, 'f' },
2595 { "wtmp-only", no_argument, NULL, 'w' },
2596 { "no-wtmp", no_argument, NULL, 'd' },
2597 { "no-sync", no_argument, NULL, 'n' },
2598 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2599 { NULL, 0, NULL, 0 }
2607 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
2608 if (runlevel == '0' || runlevel == '6')
2609 arg_immediate = true;
2611 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
2619 arg_action = ACTION_HALT;
2623 arg_action = ACTION_POWEROFF;
2627 arg_action = ACTION_REBOOT;
2631 arg_immediate = true;
2652 /* Compatibility nops */
2659 log_error("Unknown option code %c", c);
2664 if (optind < argc) {
2665 log_error("Too many arguments.");
2672 static int shutdown_parse_argv(int argc, char *argv[]) {
2679 static const struct option options[] = {
2680 { "help", no_argument, NULL, ARG_HELP },
2681 { "halt", no_argument, NULL, 'H' },
2682 { "poweroff", no_argument, NULL, 'P' },
2683 { "reboot", no_argument, NULL, 'r' },
2684 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2685 { NULL, 0, NULL, 0 }
2693 while ((c = getopt_long(argc, argv, "HPrhkt:a", options, NULL)) >= 0) {
2701 arg_action = ACTION_HALT;
2705 arg_action = ACTION_POWEROFF;
2709 arg_action = ACTION_REBOOT;
2713 if (arg_action != ACTION_HALT)
2714 arg_action = ACTION_POWEROFF;
2727 /* Compatibility nops */
2734 log_error("Unknown option code %c", c);
2739 if (argc > optind && !streq(argv[optind], "now"))
2740 log_warning("First argument '%s' isn't 'now'. Ignoring.", argv[optind]);
2742 /* We ignore the time argument */
2743 if (argc > optind + 1)
2744 arg_wall = argv + optind + 1;
2751 static int telinit_parse_argv(int argc, char *argv[]) {
2758 static const struct option options[] = {
2759 { "help", no_argument, NULL, ARG_HELP },
2760 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2761 { NULL, 0, NULL, 0 }
2764 static const struct {
2768 { '0', ACTION_POWEROFF },
2769 { '6', ACTION_REBOOT },
2770 { '1', ACTION_RESCUE },
2771 { '2', ACTION_RUNLEVEL2 },
2772 { '3', ACTION_RUNLEVEL3 },
2773 { '4', ACTION_RUNLEVEL4 },
2774 { '5', ACTION_RUNLEVEL5 },
2775 { 's', ACTION_RESCUE },
2776 { 'S', ACTION_RESCUE },
2777 { 'q', ACTION_RELOAD },
2778 { 'Q', ACTION_RELOAD },
2779 { 'u', ACTION_REEXEC },
2780 { 'U', ACTION_REEXEC }
2789 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
2804 log_error("Unknown option code %c", c);
2809 if (optind >= argc) {
2814 if (optind + 1 < argc) {
2815 log_error("Too many arguments.");
2819 if (strlen(argv[optind]) != 1) {
2820 log_error("Expected single character argument.");
2824 for (i = 0; i < ELEMENTSOF(table); i++)
2825 if (table[i].from == argv[optind][0])
2828 if (i >= ELEMENTSOF(table)) {
2829 log_error("Unknown command %s.", argv[optind]);
2833 arg_action = table[i].to;
2840 static int runlevel_parse_argv(int argc, char *argv[]) {
2846 static const struct option options[] = {
2847 { "help", no_argument, NULL, ARG_HELP },
2848 { NULL, 0, NULL, 0 }
2856 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
2867 log_error("Unknown option code %c", c);
2872 if (optind < argc) {
2873 log_error("Too many arguments.");
2880 static int parse_argv(int argc, char *argv[]) {
2884 if (program_invocation_short_name) {
2886 if (strstr(program_invocation_short_name, "halt")) {
2887 arg_action = ACTION_HALT;
2888 return halt_parse_argv(argc, argv);
2889 } else if (strstr(program_invocation_short_name, "poweroff")) {
2890 arg_action = ACTION_POWEROFF;
2891 return halt_parse_argv(argc, argv);
2892 } else if (strstr(program_invocation_short_name, "reboot")) {
2893 arg_action = ACTION_REBOOT;
2894 return halt_parse_argv(argc, argv);
2895 } else if (strstr(program_invocation_short_name, "shutdown")) {
2896 arg_action = ACTION_POWEROFF;
2897 return shutdown_parse_argv(argc, argv);
2898 } else if (strstr(program_invocation_short_name, "init")) {
2899 arg_action = ACTION_INVALID;
2900 return telinit_parse_argv(argc, argv);
2901 } else if (strstr(program_invocation_short_name, "runlevel")) {
2902 arg_action = ACTION_RUNLEVEL;
2903 return runlevel_parse_argv(argc, argv);
2907 arg_action = ACTION_SYSTEMCTL;
2908 return systemctl_parse_argv(argc, argv);
2911 static int action_to_runlevel(void) {
2913 static const char table[_ACTION_MAX] = {
2914 [ACTION_HALT] = '0',
2915 [ACTION_POWEROFF] = '0',
2916 [ACTION_REBOOT] = '6',
2917 [ACTION_RUNLEVEL2] = '2',
2918 [ACTION_RUNLEVEL3] = '3',
2919 [ACTION_RUNLEVEL4] = '4',
2920 [ACTION_RUNLEVEL5] = '5',
2921 [ACTION_RESCUE] = '1'
2924 assert(arg_action < _ACTION_MAX);
2926 return table[arg_action];
2929 static int talk_upstart(void) {
2930 DBusMessage *m = NULL, *reply = NULL;
2932 int previous, rl, r;
2934 env1_buf[] = "RUNLEVEL=X",
2935 env2_buf[] = "PREVLEVEL=X";
2936 char *env1 = env1_buf, *env2 = env2_buf;
2937 const char *emit = "runlevel";
2938 dbus_bool_t b_false = FALSE;
2939 DBusMessageIter iter, sub;
2940 DBusConnection *bus;
2942 dbus_error_init(&error);
2944 if (!(rl = action_to_runlevel()))
2947 if (utmp_get_runlevel(&previous, NULL) < 0)
2950 if (!(bus = dbus_connection_open("unix:abstract=/com/ubuntu/upstart", &error))) {
2951 if (dbus_error_has_name(&error, DBUS_ERROR_NO_SERVER)) {
2956 log_error("Failed to connect to Upstart bus: %s", error.message);
2961 if ((r = bus_check_peercred(bus)) < 0) {
2962 log_error("Failed to verify owner of bus.");
2966 if (!(m = dbus_message_new_method_call(
2967 "com.ubuntu.Upstart",
2968 "/com/ubuntu/Upstart",
2969 "com.ubuntu.Upstart0_6",
2972 log_error("Could not allocate message.");
2977 dbus_message_iter_init_append(m, &iter);
2979 env1_buf[sizeof(env1_buf)-2] = rl;
2980 env2_buf[sizeof(env2_buf)-2] = previous;
2982 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
2983 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
2984 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
2985 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
2986 !dbus_message_iter_close_container(&iter, &sub) ||
2987 !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
2988 log_error("Could not append arguments to message.");
2993 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2995 if (error_is_no_service(&error)) {
3000 log_error("Failed to issue method call: %s", error.message);
3009 dbus_message_unref(m);
3012 dbus_message_unref(reply);
3015 dbus_connection_unref(bus);
3017 dbus_error_free(&error);
3022 static int talk_initctl(void) {
3023 struct init_request request;
3027 if (!(rl = action_to_runlevel()))
3031 request.magic = INIT_MAGIC;
3032 request.sleeptime = 0;
3033 request.cmd = INIT_CMD_RUNLVL;
3034 request.runlevel = rl;
3036 if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
3038 if (errno == ENOENT)
3041 log_error("Failed to open "INIT_FIFO": %m");
3046 r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
3047 close_nointr_nofail(fd);
3050 log_error("Failed to write to "INIT_FIFO": %m");
3051 return errno ? -errno : -EIO;
3057 static int systemctl_main(DBusConnection *bus, int argc, char *argv[]) {
3059 static const struct {
3067 int (* const dispatch)(DBusConnection *bus, char **args, unsigned n);
3069 { "list-units", LESS, 1, list_units },
3070 { "list-jobs", EQUAL, 1, list_jobs },
3071 { "clear-jobs", EQUAL, 1, clear_jobs },
3072 { "load", MORE, 2, load_unit },
3073 { "cancel", MORE, 2, cancel_job },
3074 { "start", MORE, 2, start_unit },
3075 { "stop", MORE, 2, start_unit },
3076 { "reload", MORE, 2, start_unit },
3077 { "restart", MORE, 2, start_unit },
3078 { "isolate", EQUAL, 2, start_unit },
3079 { "check", MORE, 2, check_unit },
3080 { "show", MORE, 1, show },
3081 { "status", MORE, 2, show },
3082 { "monitor", EQUAL, 1, monitor },
3083 { "dump", EQUAL, 1, dump },
3084 { "snapshot", LESS, 2, snapshot },
3085 { "delete", MORE, 2, delete_snapshot },
3086 { "daemon-reload", EQUAL, 1, clear_jobs },
3087 { "daemon-reexec", EQUAL, 1, clear_jobs },
3088 { "daemon-exit", EQUAL, 1, clear_jobs },
3089 { "show-environment", EQUAL, 1, show_enviroment },
3090 { "set-environment", MORE, 2, set_environment },
3091 { "unset-environment", MORE, 2, set_environment },
3092 { "halt", EQUAL, 1, start_special },
3093 { "poweroff", EQUAL, 1, start_special },
3094 { "reboot", EQUAL, 1, start_special },
3095 { "default", EQUAL, 1, start_special },
3096 { "rescue", EQUAL, 1, start_special },
3097 { "emergency", EQUAL, 1, start_special },
3107 left = argc - optind;
3110 /* Special rule: no arguments means "list-units" */
3113 if (streq(argv[optind], "help")) {
3118 for (i = 0; i < ELEMENTSOF(verbs); i++)
3119 if (streq(argv[optind], verbs[i].verb))
3122 if (i >= ELEMENTSOF(verbs)) {
3123 log_error("Unknown operation %s", argv[optind]);
3128 switch (verbs[i].argc_cmp) {
3131 if (left != verbs[i].argc) {
3132 log_error("Invalid number of arguments.");
3139 if (left < verbs[i].argc) {
3140 log_error("Too few arguments.");
3147 if (left > verbs[i].argc) {
3148 log_error("Too many arguments.");
3155 assert_not_reached("Unknown comparison operator.");
3158 return verbs[i].dispatch(bus, argv + optind, left);
3161 static int reload_with_fallback(DBusConnection *bus) {
3165 /* First, try systemd via D-Bus. */
3166 if ((r = clear_jobs(bus, NULL, 0)) > 0)
3170 /* Nothing else worked, so let's try signals */
3171 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
3173 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
3174 log_error("kill() failed: %m");
3181 static int start_with_fallback(DBusConnection *bus) {
3184 warn_wall(arg_action);
3187 /* First, try systemd via D-Bus. */
3188 if ((r = start_unit(bus, NULL, 0)) > 0)
3191 /* Hmm, talking to systemd via D-Bus didn't work. Then
3192 * let's try to talk to Upstart via D-Bus. */
3193 if ((r = talk_upstart()) > 0)
3197 /* Nothing else worked, so let's try
3199 if ((r = talk_initctl()) != 0)
3202 log_error("Failed to talk to init daemon.");
3206 static int halt_main(DBusConnection *bus) {
3210 return start_with_fallback(bus);
3213 if ((r = utmp_put_shutdown(0)) < 0)
3214 log_warning("Failed to write utmp record: %s", strerror(-r));
3222 /* Make sure C-A-D is handled by the kernel from this
3224 reboot(RB_ENABLE_CAD);
3226 switch (arg_action) {
3229 log_info("Halting");
3230 reboot(RB_HALT_SYSTEM);
3233 case ACTION_POWEROFF:
3234 log_info("Powering off");
3235 reboot(RB_POWER_OFF);
3239 log_info("Rebooting");
3240 reboot(RB_AUTOBOOT);
3244 assert_not_reached("Unknown halt action.");
3247 /* We should never reach this. */
3251 static int runlevel_main(void) {
3252 int r, runlevel, previous;
3254 if ((r = utmp_get_runlevel(&runlevel, &previous)) < 0) {
3260 previous <= 0 ? 'N' : previous,
3261 runlevel <= 0 ? 'N' : runlevel);
3266 int main(int argc, char*argv[]) {
3268 DBusConnection *bus = NULL;
3271 dbus_error_init(&error);
3273 log_parse_environment();
3275 if ((r = parse_argv(argc, argv)) < 0)
3282 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
3283 * let's shortcut this */
3284 if (arg_action == ACTION_RUNLEVEL) {
3285 retval = runlevel_main() < 0;
3289 bus_connect(arg_session ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &bus, &error);
3291 switch (arg_action) {
3293 case ACTION_SYSTEMCTL: {
3296 log_error("Failed to get D-Bus connection: %s", error.message);
3300 retval = systemctl_main(bus, argc, argv) < 0;
3305 case ACTION_POWEROFF:
3307 retval = halt_main(bus) < 0;
3310 case ACTION_RUNLEVEL2:
3311 case ACTION_RUNLEVEL3:
3312 case ACTION_RUNLEVEL4:
3313 case ACTION_RUNLEVEL5:
3315 case ACTION_EMERGENCY:
3316 case ACTION_DEFAULT:
3317 retval = start_with_fallback(bus) < 0;
3322 retval = reload_with_fallback(bus) < 0;
3325 case ACTION_INVALID:
3326 case ACTION_RUNLEVEL:
3328 assert_not_reached("Unknown action");
3334 dbus_connection_unref(bus);
3336 dbus_error_free(&error);