1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/reboot.h>
28 #include <sys/ioctl.h>
32 #include <sys/socket.h>
34 #include <dbus/dbus.h>
40 #include "utmp-wtmp.h"
44 #include "dbus-common.h"
45 #include "cgroup-show.h"
46 #include "cgroup-util.h"
49 static const char *arg_type = NULL;
50 static const char *arg_property = NULL;
51 static bool arg_all = false;
52 static bool arg_fail = false;
53 static bool arg_session = false;
54 static bool arg_no_block = false;
55 static bool arg_immediate = false;
56 static bool arg_no_wtmp = false;
57 static bool arg_no_sync = false;
58 static bool arg_no_wall = false;
59 static bool arg_dry = false;
60 static bool arg_quiet = false;
61 static char **arg_wall = NULL;
79 } arg_action = ACTION_SYSTEMCTL;
81 static bool private_bus = false;
83 static bool error_is_no_service(DBusError *error) {
87 if (!dbus_error_is_set(error))
90 if (dbus_error_has_name(error, DBUS_ERROR_NAME_HAS_NO_OWNER))
93 if (dbus_error_has_name(error, DBUS_ERROR_SERVICE_UNKNOWN))
96 return startswith(error->name, "org.freedesktop.DBus.Error.Spawn.");
99 static int bus_iter_get_basic_and_next(DBusMessageIter *iter, int type, void *data, bool next) {
104 if (dbus_message_iter_get_arg_type(iter) != type)
107 dbus_message_iter_get_basic(iter, data);
109 if (!dbus_message_iter_next(iter) != !next)
115 static void warn_wall(enum action action) {
116 static const char *table[_ACTION_MAX] = {
117 [ACTION_HALT] = "The system is going down for system halt NOW!",
118 [ACTION_REBOOT] = "The system is going down for reboot NOW!",
119 [ACTION_POWEROFF] = "The system is going down for power-off NOW!",
120 [ACTION_RESCUE] = "The system is going down to rescue mode NOW!",
121 [ACTION_EMERGENCY] = "The system is going down to emergency mode NOW!"
130 if (!(p = strv_join(arg_wall, " "))) {
131 log_error("Failed to join strings.");
147 utmp_wall(table[action]);
150 static int list_units(DBusConnection *bus, char **args, unsigned n) {
151 DBusMessage *m = NULL, *reply = NULL;
154 DBusMessageIter iter, sub, sub2;
157 dbus_error_init(&error);
161 if (!(m = dbus_message_new_method_call(
162 "org.freedesktop.systemd1",
163 "/org/freedesktop/systemd1",
164 "org.freedesktop.systemd1.Manager",
166 log_error("Could not allocate message.");
170 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
171 log_error("Failed to issue method call: %s", error.message);
176 if (!dbus_message_iter_init(reply, &iter) ||
177 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
178 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
179 log_error("Failed to parse reply.");
184 dbus_message_iter_recurse(&iter, &sub);
186 printf("%-45s %-6s %-12s %-12s %-15s %s\n", "UNIT", "LOAD", "ACTIVE", "SUB", "JOB", "DESCRIPTION");
188 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
189 const char *id, *description, *load_state, *active_state, *sub_state, *unit_state, *job_type, *job_path, *dot;
192 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
193 log_error("Failed to parse reply.");
198 dbus_message_iter_recurse(&sub, &sub2);
200 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
201 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &description, true) < 0 ||
202 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &load_state, true) < 0 ||
203 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &active_state, true) < 0 ||
204 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &sub_state, true) < 0 ||
205 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_state, true) < 0 ||
206 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &job_id, true) < 0 ||
207 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &job_type, true) < 0 ||
208 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, false) < 0) {
209 log_error("Failed to parse reply.");
214 if ((!arg_type || ((dot = strrchr(id, '.')) &&
215 streq(dot+1, arg_type))) &&
216 (arg_all || !streq(active_state, "inactive"))) {
220 if (streq(active_state, "maintenance"))
221 fputs(ANSI_HIGHLIGHT_ON, stdout);
223 printf("%-45s %-6s %-12s %-12s%n", id, load_state, active_state, sub_state, &a);
226 printf(" %-15s%n", job_type, &b);
230 if (a + b + 2 < columns()) {
234 printf(" %.*s", columns() - a - b - 2, description);
237 if (streq(active_state, "maintenance"))
238 fputs(ANSI_HIGHLIGHT_OFF, stdout);
244 dbus_message_iter_next(&sub);
248 printf("\n%u units listed.\n", k);
250 printf("\n%u live units listed. Pass --all to see dead units, too.\n", k);
256 dbus_message_unref(m);
259 dbus_message_unref(reply);
261 dbus_error_free(&error);
266 static int list_jobs(DBusConnection *bus, char **args, unsigned n) {
267 DBusMessage *m = NULL, *reply = NULL;
270 DBusMessageIter iter, sub, sub2;
273 dbus_error_init(&error);
277 if (!(m = dbus_message_new_method_call(
278 "org.freedesktop.systemd1",
279 "/org/freedesktop/systemd1",
280 "org.freedesktop.systemd1.Manager",
282 log_error("Could not allocate message.");
286 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
287 log_error("Failed to issue method call: %s", error.message);
292 if (!dbus_message_iter_init(reply, &iter) ||
293 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
294 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
295 log_error("Failed to parse reply.");
300 dbus_message_iter_recurse(&iter, &sub);
302 printf("%4s %-45s %-17s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
304 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
305 const char *name, *type, *state, *job_path, *unit_path;
308 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
309 log_error("Failed to parse reply.");
314 dbus_message_iter_recurse(&sub, &sub2);
316 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &id, true) < 0 ||
317 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0 ||
318 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
319 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, true) < 0 ||
320 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, true) < 0 ||
321 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, false) < 0) {
322 log_error("Failed to parse reply.");
327 printf("%4u %-45s %-17s %-7s\n", id, name, type, state);
330 dbus_message_iter_next(&sub);
333 printf("\n%u jobs listed.\n", k);
338 dbus_message_unref(m);
341 dbus_message_unref(reply);
343 dbus_error_free(&error);
348 static int load_unit(DBusConnection *bus, char **args, unsigned n) {
349 DBusMessage *m = NULL, *reply = NULL;
354 dbus_error_init(&error);
359 for (i = 1; i < n; i++) {
361 if (!(m = dbus_message_new_method_call(
362 "org.freedesktop.systemd1",
363 "/org/freedesktop/systemd1",
364 "org.freedesktop.systemd1.Manager",
366 log_error("Could not allocate message.");
371 if (!dbus_message_append_args(m,
372 DBUS_TYPE_STRING, &args[i],
373 DBUS_TYPE_INVALID)) {
374 log_error("Could not append arguments to message.");
379 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
380 log_error("Failed to issue method call: %s", error.message);
385 dbus_message_unref(m);
386 dbus_message_unref(reply);
395 dbus_message_unref(m);
398 dbus_message_unref(reply);
400 dbus_error_free(&error);
405 static int cancel_job(DBusConnection *bus, char **args, unsigned n) {
406 DBusMessage *m = NULL, *reply = NULL;
411 dbus_error_init(&error);
416 for (i = 1; i < n; i++) {
420 if (!(m = dbus_message_new_method_call(
421 "org.freedesktop.systemd1",
422 "/org/freedesktop/systemd1",
423 "org.freedesktop.systemd1.Manager",
425 log_error("Could not allocate message.");
430 if ((r = safe_atou(args[i], &id)) < 0) {
431 log_error("Failed to parse job id: %s", strerror(-r));
435 assert_cc(sizeof(uint32_t) == sizeof(id));
436 if (!dbus_message_append_args(m,
437 DBUS_TYPE_UINT32, &id,
438 DBUS_TYPE_INVALID)) {
439 log_error("Could not append arguments to message.");
444 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
445 log_error("Failed to issue method call: %s", error.message);
450 if (!dbus_message_get_args(reply, &error,
451 DBUS_TYPE_OBJECT_PATH, &path,
452 DBUS_TYPE_INVALID)) {
453 log_error("Failed to parse reply: %s", error.message);
458 dbus_message_unref(m);
459 if (!(m = dbus_message_new_method_call(
460 "org.freedesktop.systemd1",
462 "org.freedesktop.systemd1.Job",
464 log_error("Could not allocate message.");
469 dbus_message_unref(reply);
470 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
471 log_error("Failed to issue method call: %s", error.message);
476 dbus_message_unref(m);
477 dbus_message_unref(reply);
485 dbus_message_unref(m);
488 dbus_message_unref(reply);
490 dbus_error_free(&error);
495 typedef struct WaitData {
500 static DBusHandlerResult wait_filter(DBusConnection *connection, DBusMessage *message, void *data) {
508 dbus_error_init(&error);
510 log_debug("Got D-Bus request: %s.%s() on %s",
511 dbus_message_get_interface(message),
512 dbus_message_get_member(message),
513 dbus_message_get_path(message));
515 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
516 log_error("Warning! D-Bus connection terminated.");
517 dbus_connection_close(connection);
519 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
522 dbus_bool_t success = true;
524 if (!dbus_message_get_args(message, &error,
525 DBUS_TYPE_UINT32, &id,
526 DBUS_TYPE_OBJECT_PATH, &path,
527 DBUS_TYPE_BOOLEAN, &success,
529 log_error("Failed to parse message: %s", error.message);
533 if ((p = set_remove(d->set, (char*) path)))
541 dbus_error_free(&error);
542 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
545 static int enable_wait_for_jobs(DBusConnection *bus) {
553 dbus_error_init(&error);
554 dbus_bus_add_match(bus,
556 "sender='org.freedesktop.systemd1',"
557 "interface='org.freedesktop.systemd1.Manager',"
558 "member='JobRemoved',"
559 "path='/org/freedesktop/systemd1'",
562 if (dbus_error_is_set(&error)) {
563 log_error("Failed to add match: %s", error.message);
564 dbus_error_free(&error);
568 /* This is slightly dirty, since we don't undo the match registrations. */
572 static int wait_for_jobs(DBusConnection *bus, Set *s) {
583 if (!dbus_connection_add_filter(bus, wait_filter, &d, NULL)) {
584 log_error("Failed to add filter.");
589 while (!set_isempty(s) &&
590 dbus_connection_read_write_dispatch(bus, -1))
593 if (!arg_quiet && d.failed)
594 log_error("Job failed, see logs for details.");
596 r = d.failed ? -EIO : 0;
599 /* This is slightly dirty, since we don't undo the filter registration. */
604 static int start_unit_one(
611 DBusMessage *m = NULL, *reply = NULL;
619 assert(arg_no_block || s);
621 dbus_error_init(&error);
623 if (!(m = dbus_message_new_method_call(
624 "org.freedesktop.systemd1",
625 "/org/freedesktop/systemd1",
626 "org.freedesktop.systemd1.Manager",
628 log_error("Could not allocate message.");
633 if (!dbus_message_append_args(m,
634 DBUS_TYPE_STRING, &name,
635 DBUS_TYPE_STRING, &mode,
636 DBUS_TYPE_INVALID)) {
637 log_error("Could not append arguments to message.");
642 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
644 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
645 /* There's always a fallback possible for
651 log_error("Failed to issue method call: %s", error.message);
660 if (!dbus_message_get_args(reply, &error,
661 DBUS_TYPE_OBJECT_PATH, &path,
662 DBUS_TYPE_INVALID)) {
663 log_error("Failed to parse reply: %s", error.message);
668 if (!(p = strdup(path))) {
669 log_error("Failed to duplicate path.");
674 if ((r = set_put(s, p)) < 0) {
676 log_error("Failed to add path to set.");
685 dbus_message_unref(m);
688 dbus_message_unref(reply);
690 dbus_error_free(&error);
695 static enum action verb_to_action(const char *verb) {
696 if (streq(verb, "halt"))
698 else if (streq(verb, "poweroff"))
699 return ACTION_POWEROFF;
700 else if (streq(verb, "reboot"))
701 return ACTION_REBOOT;
702 else if (streq(verb, "rescue"))
703 return ACTION_RESCUE;
704 else if (streq(verb, "emergency"))
705 return ACTION_EMERGENCY;
706 else if (streq(verb, "default"))
707 return ACTION_DEFAULT;
709 return ACTION_INVALID;
712 static int start_unit(DBusConnection *bus, char **args, unsigned n) {
714 static const char * const table[_ACTION_MAX] = {
715 [ACTION_HALT] = SPECIAL_HALT_TARGET,
716 [ACTION_POWEROFF] = SPECIAL_POWEROFF_TARGET,
717 [ACTION_REBOOT] = SPECIAL_REBOOT_TARGET,
718 [ACTION_RUNLEVEL2] = SPECIAL_RUNLEVEL2_TARGET,
719 [ACTION_RUNLEVEL3] = SPECIAL_RUNLEVEL3_TARGET,
720 [ACTION_RUNLEVEL4] = SPECIAL_RUNLEVEL4_TARGET,
721 [ACTION_RUNLEVEL5] = SPECIAL_RUNLEVEL5_TARGET,
722 [ACTION_RESCUE] = SPECIAL_RESCUE_TARGET,
723 [ACTION_EMERGENCY] = SPECIAL_EMERGENCY_SERVICE,
724 [ACTION_DEFAULT] = SPECIAL_DEFAULT_TARGET
729 const char *method, *mode, *one_name;
734 if (arg_action == ACTION_SYSTEMCTL) {
736 streq(args[0], "stop") ? "StopUnit" :
737 streq(args[0], "reload") ? "ReloadUnit" :
738 streq(args[0], "restart") ? "RestartUnit" :
742 (streq(args[0], "isolate") ||
743 streq(args[0], "rescue") ||
744 streq(args[0], "emergency")) ? "isolate" :
748 one_name = table[verb_to_action(args[0])];
751 assert(arg_action < ELEMENTSOF(table));
752 assert(table[arg_action]);
754 method = "StartUnit";
756 mode = (arg_action == ACTION_EMERGENCY ||
757 arg_action == ACTION_RESCUE) ? "isolate" : "replace";
759 one_name = table[arg_action];
763 if ((r = enable_wait_for_jobs(bus)) < 0) {
764 log_error("Could not watch jobs: %s", strerror(-r));
768 if (!(s = set_new(string_hash_func, string_compare_func))) {
769 log_error("Failed to allocate set.");
778 if ((r = start_unit_one(bus, method, one_name, mode, s)) <= 0)
781 for (i = 1; i < n; i++)
782 if ((r = start_unit_one(bus, method, args[i], mode, s)) < 0)
787 r = wait_for_jobs(bus, s);
796 static int start_special(DBusConnection *bus, char **args, unsigned n) {
802 r = start_unit(bus, args, n);
805 warn_wall(verb_to_action(args[0]));
810 static int check_unit(DBusConnection *bus, char **args, unsigned n) {
811 DBusMessage *m = NULL, *reply = NULL;
813 *interface = "org.freedesktop.systemd1.Unit",
814 *property = "ActiveState";
815 int r = -EADDRNOTAVAIL;
822 dbus_error_init(&error);
824 for (i = 1; i < n; i++) {
825 const char *path = NULL;
827 DBusMessageIter iter, sub;
829 if (!(m = dbus_message_new_method_call(
830 "org.freedesktop.systemd1",
831 "/org/freedesktop/systemd1",
832 "org.freedesktop.systemd1.Manager",
834 log_error("Could not allocate message.");
839 if (!dbus_message_append_args(m,
840 DBUS_TYPE_STRING, &args[i],
841 DBUS_TYPE_INVALID)) {
842 log_error("Could not append arguments to message.");
847 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
849 /* Hmm, cannot figure out anything about this unit... */
853 dbus_error_free(&error);
857 if (!dbus_message_get_args(reply, &error,
858 DBUS_TYPE_OBJECT_PATH, &path,
859 DBUS_TYPE_INVALID)) {
860 log_error("Failed to parse reply: %s", error.message);
865 dbus_message_unref(m);
866 if (!(m = dbus_message_new_method_call(
867 "org.freedesktop.systemd1",
869 "org.freedesktop.DBus.Properties",
871 log_error("Could not allocate message.");
876 if (!dbus_message_append_args(m,
877 DBUS_TYPE_STRING, &interface,
878 DBUS_TYPE_STRING, &property,
879 DBUS_TYPE_INVALID)) {
880 log_error("Could not append arguments to message.");
885 dbus_message_unref(reply);
886 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
887 log_error("Failed to issue method call: %s", error.message);
892 if (!dbus_message_iter_init(reply, &iter) ||
893 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
894 log_error("Failed to parse reply.");
899 dbus_message_iter_recurse(&iter, &sub);
901 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
902 log_error("Failed to parse reply.");
907 dbus_message_iter_get_basic(&sub, &state);
912 if (streq(state, "active") || startswith(state, "reloading"))
915 dbus_message_unref(m);
916 dbus_message_unref(reply);
922 dbus_message_unref(m);
925 dbus_message_unref(reply);
927 dbus_error_free(&error);
932 typedef struct ExecStatusInfo {
938 usec_t start_timestamp;
939 usec_t exit_timestamp;
944 LIST_FIELDS(struct ExecStatusInfo, exec);
947 static void exec_status_info_free(ExecStatusInfo *i) {
955 static int exec_status_info_deserialize(DBusMessageIter *sub, ExecStatusInfo *i) {
956 uint64_t start_timestamp, exit_timestamp;
957 DBusMessageIter sub2, sub3;
961 int32_t code, status;
967 if (dbus_message_iter_get_arg_type(sub) != DBUS_TYPE_STRUCT)
970 dbus_message_iter_recurse(sub, &sub2);
972 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0)
975 if (!(i->path = strdup(path)))
978 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_ARRAY ||
979 dbus_message_iter_get_element_type(&sub2) != DBUS_TYPE_STRING)
983 dbus_message_iter_recurse(&sub2, &sub3);
984 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
985 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
986 dbus_message_iter_next(&sub3);
991 if (!(i->argv = new0(char*, n+1)))
995 dbus_message_iter_recurse(&sub2, &sub3);
996 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
999 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1000 dbus_message_iter_get_basic(&sub3, &s);
1001 dbus_message_iter_next(&sub3);
1003 if (!(i->argv[n++] = strdup(s)))
1007 if (!dbus_message_iter_next(&sub2) ||
1008 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_BOOLEAN, &ignore, true) < 0 ||
1009 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_timestamp, true) < 0 ||
1010 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_timestamp, true) < 0 ||
1011 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, true) < 0 ||
1012 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &code, true) < 0 ||
1013 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &status, false) < 0)
1017 i->start_timestamp = (usec_t) start_timestamp;
1018 i->exit_timestamp = (usec_t) exit_timestamp;
1019 i->pid = (pid_t) pid;
1026 typedef struct UnitStatusInfo {
1028 const char *load_state;
1029 const char *active_state;
1030 const char *sub_state;
1032 const char *description;
1034 const char *fragment_path;
1035 const char *default_control_group;
1040 const char *status_text;
1043 usec_t start_timestamp;
1044 usec_t exit_timestamp;
1046 int exit_code, exit_status;
1049 unsigned n_accepted;
1050 unsigned n_connections;
1054 const char *sysfs_path;
1056 /* Mount, Automount */
1062 LIST_HEAD(ExecStatusInfo, exec);
1065 static void print_status_info(UnitStatusInfo *i) {
1071 /* This shows pretty information about a unit. See
1072 * print_property() for a low-level property printer */
1074 printf("%s", strna(i->id));
1076 if (i->description && !streq_ptr(i->id, i->description))
1077 printf(" - %s", i->description);
1081 if (i->fragment_path)
1082 printf("\t Loaded: %s (%s)\n", strna(i->load_state), i->fragment_path);
1083 else if (streq_ptr(i->load_state, "failed"))
1084 printf("\t Loaded: " ANSI_HIGHLIGHT_ON "%s" ANSI_HIGHLIGHT_OFF "\n", strna(i->load_state));
1086 printf("\t Loaded: %s\n", strna(i->load_state));
1088 if (streq_ptr(i->active_state, "maintenance")) {
1089 if (streq_ptr(i->active_state, i->sub_state))
1090 printf("\t Active: " ANSI_HIGHLIGHT_ON "%s" ANSI_HIGHLIGHT_OFF "\n",
1091 strna(i->active_state));
1093 printf("\t Active: " ANSI_HIGHLIGHT_ON "%s (%s)" ANSI_HIGHLIGHT_OFF "\n",
1094 strna(i->active_state),
1095 strna(i->sub_state));
1097 if (streq_ptr(i->active_state, i->sub_state))
1098 printf("\t Active: %s\n",
1099 strna(i->active_state));
1101 printf("\t Active: %s (%s)\n",
1102 strna(i->active_state),
1103 strna(i->sub_state));
1107 printf("\t Device: %s\n", i->sysfs_path);
1109 printf("\t Where: %s\n", i->where);
1111 printf("\t What: %s\n", i->what);
1114 printf("\tAccepted: %u; Connected: %u\n", i->n_accepted, i->n_connections);
1116 LIST_FOREACH(exec, p, i->exec) {
1119 /* Only show exited processes here */
1123 t = strv_join(p->argv, " ");
1124 printf("\t Exited: %u (%s, code=%s, ", p->pid, strna(t), sigchld_code_to_string(p->code));
1127 if (p->code == CLD_EXITED)
1128 printf("status=%i", p->status);
1130 printf("signal=%s", signal_to_string(p->status));
1133 if (i->main_pid == p->pid &&
1134 i->start_timestamp == p->start_timestamp &&
1135 i->exit_timestamp == p->start_timestamp)
1136 /* Let's not show this twice */
1139 if (p->pid == i->control_pid)
1143 if (i->main_pid > 0 || i->control_pid > 0) {
1146 if (i->main_pid > 0) {
1147 printf(" Main: %u", (unsigned) i->main_pid);
1151 get_process_name(i->main_pid, &t);
1157 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
1159 if (i->exit_code == CLD_EXITED)
1160 printf("status=%i", i->exit_status);
1162 printf("signal=%s", signal_to_string(i->exit_status));
1167 if (i->main_pid > 0 && i->control_pid > 0)
1170 if (i->control_pid > 0) {
1173 printf(" Control: %u", (unsigned) i->control_pid);
1175 get_process_name(i->control_pid, &t);
1186 printf("\t Status: \"%s\"\n", i->status_text);
1188 if (i->default_control_group) {
1191 printf("\t CGroup: %s\n", i->default_control_group);
1193 if ((c = columns()) > 18)
1198 if ((r = cg_init()) < 0)
1199 log_error("Failed to initialize libcg: %s", strerror(-r));
1201 show_cgroup_recursive(i->default_control_group, "\t\t ", c);
1205 static int status_property(const char *name, DBusMessageIter *iter, UnitStatusInfo *i) {
1207 switch (dbus_message_iter_get_arg_type(iter)) {
1209 case DBUS_TYPE_STRING: {
1212 dbus_message_iter_get_basic(iter, &s);
1215 if (streq(name, "Id"))
1217 else if (streq(name, "LoadState"))
1219 else if (streq(name, "ActiveState"))
1220 i->active_state = s;
1221 else if (streq(name, "SubState"))
1223 else if (streq(name, "Description"))
1225 else if (streq(name, "FragmentPath"))
1226 i->fragment_path = s;
1227 else if (streq(name, "DefaultControlGroup"))
1228 i->default_control_group = s;
1229 else if (streq(name, "StatusText"))
1231 else if (streq(name, "SysFSPath"))
1233 else if (streq(name, "Where"))
1235 else if (streq(name, "What"))
1242 case DBUS_TYPE_BOOLEAN: {
1245 dbus_message_iter_get_basic(iter, &b);
1247 if (streq(name, "Accept"))
1253 case DBUS_TYPE_UINT32: {
1256 dbus_message_iter_get_basic(iter, &u);
1258 if (streq(name, "MainPID")) {
1260 i->main_pid = (pid_t) u;
1263 } else if (streq(name, "ControlPID"))
1264 i->control_pid = (pid_t) u;
1265 else if (streq(name, "ExecMainPID")) {
1267 i->main_pid = (pid_t) u;
1268 } else if (streq(name, "NAccepted"))
1270 else if (streq(name, "NConnections"))
1271 i->n_connections = u;
1276 case DBUS_TYPE_INT32: {
1279 dbus_message_iter_get_basic(iter, &j);
1281 if (streq(name, "ExecMainCode"))
1282 i->exit_code = (int) j;
1283 else if (streq(name, "ExecMainStatus"))
1284 i->exit_status = (int) j;
1289 case DBUS_TYPE_UINT64: {
1292 dbus_message_iter_get_basic(iter, &u);
1294 if (streq(name, "ExecMainStartTimestamp"))
1295 i->start_timestamp = (usec_t) u;
1296 else if (streq(name, "ExecMainExitTimestamp"))
1297 i->exit_timestamp = (usec_t) u;
1302 case DBUS_TYPE_ARRAY: {
1304 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT &&
1305 startswith(name, "Exec")) {
1306 DBusMessageIter sub;
1308 dbus_message_iter_recurse(iter, &sub);
1309 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1310 ExecStatusInfo *info;
1313 if (!(info = new0(ExecStatusInfo, 1)))
1316 if ((r = exec_status_info_deserialize(&sub, info)) < 0) {
1321 LIST_PREPEND(ExecStatusInfo, exec, i->exec, info);
1323 dbus_message_iter_next(&sub);
1334 static int print_property(const char *name, DBusMessageIter *iter) {
1338 /* This is a low-level property printer, see
1339 * print_status_info() for the nicer output */
1341 if (arg_property && !streq(name, arg_property))
1344 switch (dbus_message_iter_get_arg_type(iter)) {
1346 case DBUS_TYPE_STRING: {
1348 dbus_message_iter_get_basic(iter, &s);
1350 if (arg_all || s[0])
1351 printf("%s=%s\n", name, s);
1356 case DBUS_TYPE_BOOLEAN: {
1358 dbus_message_iter_get_basic(iter, &b);
1359 printf("%s=%s\n", name, yes_no(b));
1364 case DBUS_TYPE_UINT64: {
1366 dbus_message_iter_get_basic(iter, &u);
1368 /* Yes, heuristics! But we can change this check
1369 * should it turn out to not be sufficient */
1371 if (strstr(name, "Timestamp")) {
1372 char timestamp[FORMAT_TIMESTAMP_MAX], *t;
1374 if ((t = format_timestamp(timestamp, sizeof(timestamp), u)) || arg_all)
1375 printf("%s=%s\n", name, strempty(t));
1376 } else if (strstr(name, "USec")) {
1377 char timespan[FORMAT_TIMESPAN_MAX];
1379 printf("%s=%s\n", name, format_timespan(timespan, sizeof(timespan), u));
1381 printf("%s=%llu\n", name, (unsigned long long) u);
1386 case DBUS_TYPE_UINT32: {
1388 dbus_message_iter_get_basic(iter, &u);
1390 if (strstr(name, "UMask") || strstr(name, "Mode"))
1391 printf("%s=%04o\n", name, u);
1393 printf("%s=%u\n", name, (unsigned) u);
1398 case DBUS_TYPE_INT32: {
1400 dbus_message_iter_get_basic(iter, &i);
1402 printf("%s=%i\n", name, (int) i);
1406 case DBUS_TYPE_STRUCT: {
1407 DBusMessageIter sub;
1408 dbus_message_iter_recurse(iter, &sub);
1410 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "Job")) {
1413 dbus_message_iter_get_basic(&sub, &u);
1416 printf("%s=%u\n", name, (unsigned) u);
1418 printf("%s=\n", name);
1421 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Unit")) {
1424 dbus_message_iter_get_basic(&sub, &s);
1426 if (arg_all || s[0])
1427 printf("%s=%s\n", name, s);
1435 case DBUS_TYPE_ARRAY:
1437 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
1438 DBusMessageIter sub;
1441 dbus_message_iter_recurse(iter, &sub);
1443 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1444 printf("%s=", name);
1446 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1449 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
1450 dbus_message_iter_get_basic(&sub, &s);
1451 printf("%s%s", space ? " " : "", s);
1454 dbus_message_iter_next(&sub);
1462 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_BYTE) {
1463 DBusMessageIter sub;
1465 dbus_message_iter_recurse(iter, &sub);
1467 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1468 printf("%s=", name);
1470 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1473 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_BYTE);
1474 dbus_message_iter_get_basic(&sub, &u);
1477 dbus_message_iter_next(&sub);
1485 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Paths")) {
1486 DBusMessageIter sub, sub2;
1488 dbus_message_iter_recurse(iter, &sub);
1489 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1490 const char *type, *path;
1492 dbus_message_iter_recurse(&sub, &sub2);
1494 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
1495 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0)
1496 printf("%s=%s\n", type, path);
1498 dbus_message_iter_next(&sub);
1503 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Timers")) {
1504 DBusMessageIter sub, sub2;
1506 dbus_message_iter_recurse(iter, &sub);
1507 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1509 uint64_t value, next_elapse;
1511 dbus_message_iter_recurse(&sub, &sub2);
1513 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &base, true) >= 0 &&
1514 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &value, true) >= 0 &&
1515 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &next_elapse, false) >= 0) {
1516 char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX];
1518 printf("%s={ value=%s ; next_elapse=%s }\n",
1520 format_timespan(timespan1, sizeof(timespan1), value),
1521 format_timespan(timespan2, sizeof(timespan2), next_elapse));
1524 dbus_message_iter_next(&sub);
1529 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && startswith(name, "Exec")) {
1530 DBusMessageIter sub;
1532 dbus_message_iter_recurse(iter, &sub);
1533 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1534 ExecStatusInfo info;
1537 if (exec_status_info_deserialize(&sub, &info) >= 0) {
1538 char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
1541 t = strv_join(info.argv, " ");
1543 printf("%s={ path=%s ; argv[]=%s ; ignore=%s ; start_time=[%s] ; stop_time=[%s] ; pid=%u ; code=%s ; status=%i%s%s }\n",
1547 yes_no(info.ignore),
1548 strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)),
1549 strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)),
1550 (unsigned) info. pid,
1551 sigchld_code_to_string(info.code),
1553 info.code == CLD_EXITED ? "" : "/",
1554 strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status)));
1560 strv_free(info.argv);
1562 dbus_message_iter_next(&sub);
1572 printf("%s=[unprintable]\n", name);
1577 static int show_one(DBusConnection *bus, const char *path, bool show_properties, bool *new_line) {
1578 DBusMessage *m = NULL, *reply = NULL;
1579 const char *interface = "";
1582 DBusMessageIter iter, sub, sub2, sub3;
1583 UnitStatusInfo info;
1591 dbus_error_init(&error);
1593 if (!(m = dbus_message_new_method_call(
1594 "org.freedesktop.systemd1",
1596 "org.freedesktop.DBus.Properties",
1598 log_error("Could not allocate message.");
1603 if (!dbus_message_append_args(m,
1604 DBUS_TYPE_STRING, &interface,
1605 DBUS_TYPE_INVALID)) {
1606 log_error("Could not append arguments to message.");
1611 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1612 log_error("Failed to issue method call: %s", error.message);
1617 if (!dbus_message_iter_init(reply, &iter) ||
1618 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
1619 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
1620 log_error("Failed to parse reply.");
1625 dbus_message_iter_recurse(&iter, &sub);
1632 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1635 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
1636 log_error("Failed to parse reply.");
1641 dbus_message_iter_recurse(&sub, &sub2);
1643 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
1644 log_error("Failed to parse reply.");
1649 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
1650 log_error("Failed to parse reply.");
1655 dbus_message_iter_recurse(&sub2, &sub3);
1657 if (show_properties)
1658 r = print_property(name, &sub3);
1660 r = status_property(name, &sub3, &info);
1663 log_error("Failed to parse reply.");
1668 dbus_message_iter_next(&sub);
1671 if (!show_properties)
1672 print_status_info(&info);
1674 while ((p = info.exec)) {
1675 LIST_REMOVE(ExecStatusInfo, exec, info.exec, p);
1676 exec_status_info_free(p);
1683 dbus_message_unref(m);
1686 dbus_message_unref(reply);
1688 dbus_error_free(&error);
1693 static int show(DBusConnection *bus, char **args, unsigned n) {
1694 DBusMessage *m = NULL, *reply = NULL;
1698 bool show_properties, new_line = false;
1703 dbus_error_init(&error);
1705 show_properties = !streq(args[0], "status");
1707 if (show_properties && n <= 1) {
1708 /* If not argument is specified inspect the manager
1711 r = show_one(bus, "/org/freedesktop/systemd1", show_properties, &new_line);
1715 for (i = 1; i < n; i++) {
1716 const char *path = NULL;
1719 if (!show_properties || safe_atou32(args[i], &id) < 0) {
1721 if (!(m = dbus_message_new_method_call(
1722 "org.freedesktop.systemd1",
1723 "/org/freedesktop/systemd1",
1724 "org.freedesktop.systemd1.Manager",
1726 log_error("Could not allocate message.");
1731 if (!dbus_message_append_args(m,
1732 DBUS_TYPE_STRING, &args[i],
1733 DBUS_TYPE_INVALID)) {
1734 log_error("Could not append arguments to message.");
1739 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1741 if (!dbus_error_has_name(&error, DBUS_ERROR_ACCESS_DENIED)) {
1742 log_error("Failed to issue method call: %s", error.message);
1747 dbus_error_free(&error);
1749 dbus_message_unref(m);
1750 if (!(m = dbus_message_new_method_call(
1751 "org.freedesktop.systemd1",
1752 "/org/freedesktop/systemd1",
1753 "org.freedesktop.systemd1.Manager",
1755 log_error("Could not allocate message.");
1760 if (!dbus_message_append_args(m,
1761 DBUS_TYPE_STRING, &args[i],
1762 DBUS_TYPE_INVALID)) {
1763 log_error("Could not append arguments to message.");
1768 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1769 log_error("Failed to issue method call: %s", error.message);
1777 if (!(m = dbus_message_new_method_call(
1778 "org.freedesktop.systemd1",
1779 "/org/freedesktop/systemd1",
1780 "org.freedesktop.systemd1.Manager",
1782 log_error("Could not allocate message.");
1787 if (!dbus_message_append_args(m,
1788 DBUS_TYPE_UINT32, &id,
1789 DBUS_TYPE_INVALID)) {
1790 log_error("Could not append arguments to message.");
1795 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1796 log_error("Failed to issue method call: %s", error.message);
1802 if (!dbus_message_get_args(reply, &error,
1803 DBUS_TYPE_OBJECT_PATH, &path,
1804 DBUS_TYPE_INVALID)) {
1805 log_error("Failed to parse reply: %s", error.message);
1810 if ((r = show_one(bus, path, show_properties, &new_line)) < 0)
1813 dbus_message_unref(m);
1814 dbus_message_unref(reply);
1822 dbus_message_unref(m);
1825 dbus_message_unref(reply);
1827 dbus_error_free(&error);
1832 static DBusHandlerResult monitor_filter(DBusConnection *connection, DBusMessage *message, void *data) {
1834 DBusMessage *m = NULL, *reply = NULL;
1839 dbus_error_init(&error);
1841 log_debug("Got D-Bus request: %s.%s() on %s",
1842 dbus_message_get_interface(message),
1843 dbus_message_get_member(message),
1844 dbus_message_get_path(message));
1846 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
1847 log_error("Warning! D-Bus connection terminated.");
1848 dbus_connection_close(connection);
1850 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitNew") ||
1851 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitRemoved")) {
1852 const char *id, *path;
1854 if (!dbus_message_get_args(message, &error,
1855 DBUS_TYPE_STRING, &id,
1856 DBUS_TYPE_OBJECT_PATH, &path,
1858 log_error("Failed to parse message: %s", error.message);
1859 else if (streq(dbus_message_get_member(message), "UnitNew"))
1860 printf("Unit %s added.\n", id);
1862 printf("Unit %s removed.\n", id);
1864 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobNew") ||
1865 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
1869 if (!dbus_message_get_args(message, &error,
1870 DBUS_TYPE_UINT32, &id,
1871 DBUS_TYPE_OBJECT_PATH, &path,
1873 log_error("Failed to parse message: %s", error.message);
1874 else if (streq(dbus_message_get_member(message), "JobNew"))
1875 printf("Job %u added.\n", id);
1877 printf("Job %u removed.\n", id);
1880 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Unit", "Changed") ||
1881 dbus_message_is_signal(message, "org.freedesktop.systemd1.Job", "Changed")) {
1883 const char *path, *interface, *property = "Id";
1884 DBusMessageIter iter, sub;
1886 path = dbus_message_get_path(message);
1887 interface = dbus_message_get_interface(message);
1889 if (!(m = dbus_message_new_method_call(
1890 "org.freedesktop.systemd1",
1892 "org.freedesktop.DBus.Properties",
1894 log_error("Could not allocate message.");
1898 if (!dbus_message_append_args(m,
1899 DBUS_TYPE_STRING, &interface,
1900 DBUS_TYPE_STRING, &property,
1901 DBUS_TYPE_INVALID)) {
1902 log_error("Could not append arguments to message.");
1906 if (!(reply = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) {
1907 log_error("Failed to issue method call: %s", error.message);
1911 if (!dbus_message_iter_init(reply, &iter) ||
1912 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1913 log_error("Failed to parse reply.");
1917 dbus_message_iter_recurse(&iter, &sub);
1919 if (streq(interface, "org.freedesktop.systemd1.Unit")) {
1922 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1923 log_error("Failed to parse reply.");
1927 dbus_message_iter_get_basic(&sub, &id);
1928 printf("Unit %s changed.\n", id);
1932 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT32) {
1933 log_error("Failed to parse reply.");
1937 dbus_message_iter_get_basic(&sub, &id);
1938 printf("Job %u changed.\n", id);
1944 dbus_message_unref(m);
1947 dbus_message_unref(reply);
1949 dbus_error_free(&error);
1950 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1954 dbus_message_unref(m);
1957 dbus_message_unref(reply);
1959 dbus_error_free(&error);
1960 return DBUS_HANDLER_RESULT_NEED_MEMORY;
1963 static int monitor(DBusConnection *bus, char **args, unsigned n) {
1964 DBusMessage *m = NULL, *reply = NULL;
1968 dbus_error_init(&error);
1971 dbus_bus_add_match(bus,
1973 "sender='org.freedesktop.systemd1',"
1974 "interface='org.freedesktop.systemd1.Manager',"
1975 "path='/org/freedesktop/systemd1'",
1978 if (dbus_error_is_set(&error)) {
1979 log_error("Failed to add match: %s", error.message);
1984 dbus_bus_add_match(bus,
1986 "sender='org.freedesktop.systemd1',"
1987 "interface='org.freedesktop.systemd1.Unit',"
1991 if (dbus_error_is_set(&error)) {
1992 log_error("Failed to add match: %s", error.message);
1997 dbus_bus_add_match(bus,
1999 "sender='org.freedesktop.systemd1',"
2000 "interface='org.freedesktop.systemd1.Job',"
2004 if (dbus_error_is_set(&error)) {
2005 log_error("Failed to add match: %s", error.message);
2011 if (!dbus_connection_add_filter(bus, monitor_filter, NULL, NULL)) {
2012 log_error("Failed to add filter.");
2017 if (!(m = dbus_message_new_method_call(
2018 "org.freedesktop.systemd1",
2019 "/org/freedesktop/systemd1",
2020 "org.freedesktop.systemd1.Manager",
2022 log_error("Could not allocate message.");
2027 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2028 log_error("Failed to issue method call: %s", error.message);
2033 while (dbus_connection_read_write_dispatch(bus, -1))
2040 /* This is slightly dirty, since we don't undo the filter or the matches. */
2043 dbus_message_unref(m);
2046 dbus_message_unref(reply);
2048 dbus_error_free(&error);
2053 static int dump(DBusConnection *bus, char **args, unsigned n) {
2054 DBusMessage *m = NULL, *reply = NULL;
2059 dbus_error_init(&error);
2061 if (!(m = dbus_message_new_method_call(
2062 "org.freedesktop.systemd1",
2063 "/org/freedesktop/systemd1",
2064 "org.freedesktop.systemd1.Manager",
2066 log_error("Could not allocate message.");
2070 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2071 log_error("Failed to issue method call: %s", error.message);
2076 if (!dbus_message_get_args(reply, &error,
2077 DBUS_TYPE_STRING, &text,
2078 DBUS_TYPE_INVALID)) {
2079 log_error("Failed to parse reply: %s", error.message);
2084 fputs(text, stdout);
2090 dbus_message_unref(m);
2093 dbus_message_unref(reply);
2095 dbus_error_free(&error);
2100 static int snapshot(DBusConnection *bus, char **args, unsigned n) {
2101 DBusMessage *m = NULL, *reply = NULL;
2104 const char *name = "", *path, *id;
2105 dbus_bool_t cleanup = FALSE;
2106 DBusMessageIter iter, sub;
2108 *interface = "org.freedesktop.systemd1.Unit",
2111 dbus_error_init(&error);
2113 if (!(m = dbus_message_new_method_call(
2114 "org.freedesktop.systemd1",
2115 "/org/freedesktop/systemd1",
2116 "org.freedesktop.systemd1.Manager",
2117 "CreateSnapshot"))) {
2118 log_error("Could not allocate message.");
2125 if (!dbus_message_append_args(m,
2126 DBUS_TYPE_STRING, &name,
2127 DBUS_TYPE_BOOLEAN, &cleanup,
2128 DBUS_TYPE_INVALID)) {
2129 log_error("Could not append arguments to message.");
2134 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2135 log_error("Failed to issue method call: %s", error.message);
2140 if (!dbus_message_get_args(reply, &error,
2141 DBUS_TYPE_OBJECT_PATH, &path,
2142 DBUS_TYPE_INVALID)) {
2143 log_error("Failed to parse reply: %s", error.message);
2148 dbus_message_unref(m);
2149 if (!(m = dbus_message_new_method_call(
2150 "org.freedesktop.systemd1",
2152 "org.freedesktop.DBus.Properties",
2154 log_error("Could not allocate message.");
2158 if (!dbus_message_append_args(m,
2159 DBUS_TYPE_STRING, &interface,
2160 DBUS_TYPE_STRING, &property,
2161 DBUS_TYPE_INVALID)) {
2162 log_error("Could not append arguments to message.");
2167 dbus_message_unref(reply);
2168 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2169 log_error("Failed to issue method call: %s", error.message);
2174 if (!dbus_message_iter_init(reply, &iter) ||
2175 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2176 log_error("Failed to parse reply.");
2181 dbus_message_iter_recurse(&iter, &sub);
2183 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2184 log_error("Failed to parse reply.");
2189 dbus_message_iter_get_basic(&sub, &id);
2197 dbus_message_unref(m);
2200 dbus_message_unref(reply);
2202 dbus_error_free(&error);
2207 static int delete_snapshot(DBusConnection *bus, char **args, unsigned n) {
2208 DBusMessage *m = NULL, *reply = NULL;
2216 dbus_error_init(&error);
2218 for (i = 1; i < n; i++) {
2219 const char *path = NULL;
2221 if (!(m = dbus_message_new_method_call(
2222 "org.freedesktop.systemd1",
2223 "/org/freedesktop/systemd1",
2224 "org.freedesktop.systemd1.Manager",
2226 log_error("Could not allocate message.");
2231 if (!dbus_message_append_args(m,
2232 DBUS_TYPE_STRING, &args[i],
2233 DBUS_TYPE_INVALID)) {
2234 log_error("Could not append arguments to message.");
2239 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2240 log_error("Failed to issue method call: %s", error.message);
2245 if (!dbus_message_get_args(reply, &error,
2246 DBUS_TYPE_OBJECT_PATH, &path,
2247 DBUS_TYPE_INVALID)) {
2248 log_error("Failed to parse reply: %s", error.message);
2253 dbus_message_unref(m);
2254 if (!(m = dbus_message_new_method_call(
2255 "org.freedesktop.systemd1",
2257 "org.freedesktop.systemd1.Snapshot",
2259 log_error("Could not allocate message.");
2264 dbus_message_unref(reply);
2265 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2266 log_error("Failed to issue method call: %s", error.message);
2271 dbus_message_unref(m);
2272 dbus_message_unref(reply);
2280 dbus_message_unref(m);
2283 dbus_message_unref(reply);
2285 dbus_error_free(&error);
2290 static int clear_jobs(DBusConnection *bus, char **args, unsigned n) {
2291 DBusMessage *m = NULL, *reply = NULL;
2296 dbus_error_init(&error);
2298 if (arg_action == ACTION_RELOAD)
2300 else if (arg_action == ACTION_REEXEC)
2301 method = "Reexecute";
2303 assert(arg_action == ACTION_SYSTEMCTL);
2306 streq(args[0], "clear-jobs") ? "ClearJobs" :
2307 streq(args[0], "daemon-reload") ? "Reload" :
2308 streq(args[0], "daemon-reexec") ? "Reexecute" :
2312 if (!(m = dbus_message_new_method_call(
2313 "org.freedesktop.systemd1",
2314 "/org/freedesktop/systemd1",
2315 "org.freedesktop.systemd1.Manager",
2317 log_error("Could not allocate message.");
2321 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2323 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
2324 /* There's always a fallback possible for
2325 * legacy actions. */
2330 log_error("Failed to issue method call: %s", error.message);
2339 dbus_message_unref(m);
2342 dbus_message_unref(reply);
2344 dbus_error_free(&error);
2349 static int show_enviroment(DBusConnection *bus, char **args, unsigned n) {
2350 DBusMessage *m = NULL, *reply = NULL;
2352 DBusMessageIter iter, sub, sub2;
2355 *interface = "org.freedesktop.systemd1.Manager",
2356 *property = "Environment";
2358 dbus_error_init(&error);
2360 if (!(m = dbus_message_new_method_call(
2361 "org.freedesktop.systemd1",
2362 "/org/freedesktop/systemd1",
2363 "org.freedesktop.DBus.Properties",
2365 log_error("Could not allocate message.");
2369 if (!dbus_message_append_args(m,
2370 DBUS_TYPE_STRING, &interface,
2371 DBUS_TYPE_STRING, &property,
2372 DBUS_TYPE_INVALID)) {
2373 log_error("Could not append arguments to message.");
2378 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2379 log_error("Failed to issue method call: %s", error.message);
2384 if (!dbus_message_iter_init(reply, &iter) ||
2385 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2386 log_error("Failed to parse reply.");
2391 dbus_message_iter_recurse(&iter, &sub);
2393 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
2394 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
2395 log_error("Failed to parse reply.");
2400 dbus_message_iter_recurse(&sub, &sub2);
2402 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
2405 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
2406 log_error("Failed to parse reply.");
2411 dbus_message_iter_get_basic(&sub2, &text);
2412 printf("%s\n", text);
2414 dbus_message_iter_next(&sub2);
2421 dbus_message_unref(m);
2424 dbus_message_unref(reply);
2426 dbus_error_free(&error);
2431 static int set_environment(DBusConnection *bus, char **args, unsigned n) {
2432 DBusMessage *m = NULL, *reply = NULL;
2436 DBusMessageIter iter, sub;
2439 dbus_error_init(&error);
2441 method = streq(args[0], "set-environment")
2443 : "UnsetEnvironment";
2445 if (!(m = dbus_message_new_method_call(
2446 "org.freedesktop.systemd1",
2447 "/org/freedesktop/systemd1",
2448 "org.freedesktop.systemd1.Manager",
2451 log_error("Could not allocate message.");
2455 dbus_message_iter_init_append(m, &iter);
2457 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
2458 log_error("Could not append arguments to message.");
2463 for (i = 1; i < n; i++)
2464 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &args[i])) {
2465 log_error("Could not append arguments to message.");
2470 if (!dbus_message_iter_close_container(&iter, &sub)) {
2471 log_error("Could not append arguments to message.");
2476 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2477 log_error("Failed to issue method call: %s", error.message);
2486 dbus_message_unref(m);
2489 dbus_message_unref(reply);
2491 dbus_error_free(&error);
2496 static int systemctl_help(void) {
2498 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
2499 "Send control commands to the systemd manager.\n\n"
2500 " -h --help Show this help\n"
2501 " -t --type=TYPE List only units of a particular type\n"
2502 " -p --property=NAME Show only properties by this name\n"
2503 " -a --all Show all units/properties, including dead/empty ones\n"
2504 " --fail When installing a new job, fail if conflicting jobs are pending\n"
2505 " --system Connect to system bus\n"
2506 " --session Connect to session bus\n"
2507 " -q --quiet Suppress output\n"
2508 " --no-block Do not wait until operation finished\n"
2509 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
2511 " list-units List units\n"
2512 " start [NAME...] Start one or more units\n"
2513 " stop [NAME...] Stop one or more units\n"
2514 " restart [NAME...] Restart one or more units\n"
2515 " reload [NAME...] Reload one or more units\n"
2516 " isolate [NAME] Start one unit and stop all others\n"
2517 " check [NAME...] Check whether any of the passed units are active\n"
2518 " status [NAME...] Show status of one or more units\n"
2519 " show [NAME...|JOB...] Show properties of one or more units/jobs/manager\n"
2520 " load [NAME...] Load one or more units\n"
2521 " list-jobs List jobs\n"
2522 " cancel [JOB...] Cancel one or more jobs\n"
2523 " clear-jobs Cancel all jobs\n"
2524 " monitor Monitor unit/job changes\n"
2525 " dump Dump server status\n"
2526 " snapshot [NAME] Create a snapshot\n"
2527 " delete [NAME...] Remove one or more snapshots\n"
2528 " daemon-reload Reload systemd manager configuration\n"
2529 " daemon-reexec Reexecute systemd manager\n"
2530 " daemon-exit Ask the systemd manager to quit\n"
2531 " show-environment Dump environment\n"
2532 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
2533 " unset-environment [NAME...] Unset one or more environment variables\n"
2534 " halt Shut down and halt the system\n"
2535 " poweroff Shut down and power-off the system\n"
2536 " reboot Shut down and reboot the system\n"
2537 " default Enter default mode\n"
2538 " rescue Enter rescue mode\n"
2539 " emergency Enter emergency mode\n",
2540 program_invocation_short_name);
2545 static int halt_help(void) {
2547 printf("%s [OPTIONS...]\n\n"
2548 "%s the system.\n\n"
2549 " --help Show this help\n"
2550 " --halt Halt the machine\n"
2551 " -p --poweroff Switch off the machine\n"
2552 " --reboot Reboot the machine\n"
2553 " -f --force Force immediate halt/power-off/reboot\n"
2554 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
2555 " -d --no-wtmp Don't write wtmp record\n"
2556 " -n --no-sync Don't sync before halt/power-off/reboot\n"
2557 " --no-wall Don't send wall message before halt/power-off/reboot\n",
2558 program_invocation_short_name,
2559 arg_action == ACTION_REBOOT ? "Reboot" :
2560 arg_action == ACTION_POWEROFF ? "Power off" :
2566 static int shutdown_help(void) {
2568 printf("%s [OPTIONS...] [now] [WALL...]\n\n"
2569 "Shut down the system.\n\n"
2570 " --help Show this help\n"
2571 " -H --halt Halt the machine\n"
2572 " -P --poweroff Power-off the machine\n"
2573 " -r --reboot Reboot the machine\n"
2574 " -h Equivalent to --poweroff, overriden by --halt\n"
2575 " -k Don't halt/power-off/reboot, just send warnings\n"
2576 " --no-wall Don't send wall message before halt/power-off/reboot\n",
2577 program_invocation_short_name);
2582 static int telinit_help(void) {
2584 printf("%s [OPTIONS...] {COMMAND}\n\n"
2585 "Send control commands to the init daemon.\n\n"
2586 " --help Show this help\n"
2587 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
2589 " 0 Power-off the machine\n"
2590 " 6 Reboot the machine\n"
2591 " 2, 3, 4, 5 Start runlevelX.target unit\n"
2592 " 1, s, S Enter rescue mode\n"
2593 " q, Q Reload init daemon configuration\n"
2594 " u, U Reexecute init daemon\n",
2595 program_invocation_short_name);
2600 static int runlevel_help(void) {
2602 printf("%s [OPTIONS...]\n\n"
2603 "Prints the previous and current runlevel of the init system.\n\n"
2604 " --help Show this help\n",
2605 program_invocation_short_name);
2610 static int systemctl_parse_argv(int argc, char *argv[]) {
2620 static const struct option options[] = {
2621 { "help", no_argument, NULL, 'h' },
2622 { "type", required_argument, NULL, 't' },
2623 { "property", required_argument, NULL, 'p' },
2624 { "all", no_argument, NULL, 'a' },
2625 { "fail", no_argument, NULL, ARG_FAIL },
2626 { "session", no_argument, NULL, ARG_SESSION },
2627 { "system", no_argument, NULL, ARG_SYSTEM },
2628 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
2629 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2630 { "quiet", no_argument, NULL, 'q' },
2631 { NULL, 0, NULL, 0 }
2639 while ((c = getopt_long(argc, argv, "ht:p:aq", options, NULL)) >= 0) {
2652 arg_property = optarg;
2654 /* If the user asked for a particular
2655 * property, show it to him, even if it is
2673 arg_session = false;
2677 arg_no_block = true;
2692 log_error("Unknown option code %c", c);
2700 static int halt_parse_argv(int argc, char *argv[]) {
2709 static const struct option options[] = {
2710 { "help", no_argument, NULL, ARG_HELP },
2711 { "halt", no_argument, NULL, ARG_HALT },
2712 { "poweroff", no_argument, NULL, 'p' },
2713 { "reboot", no_argument, NULL, ARG_REBOOT },
2714 { "force", no_argument, NULL, 'f' },
2715 { "wtmp-only", no_argument, NULL, 'w' },
2716 { "no-wtmp", no_argument, NULL, 'd' },
2717 { "no-sync", no_argument, NULL, 'n' },
2718 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2719 { NULL, 0, NULL, 0 }
2727 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
2728 if (runlevel == '0' || runlevel == '6')
2729 arg_immediate = true;
2731 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
2739 arg_action = ACTION_HALT;
2743 arg_action = ACTION_POWEROFF;
2747 arg_action = ACTION_REBOOT;
2751 arg_immediate = true;
2772 /* Compatibility nops */
2779 log_error("Unknown option code %c", c);
2784 if (optind < argc) {
2785 log_error("Too many arguments.");
2792 static int shutdown_parse_argv(int argc, char *argv[]) {
2799 static const struct option options[] = {
2800 { "help", no_argument, NULL, ARG_HELP },
2801 { "halt", no_argument, NULL, 'H' },
2802 { "poweroff", no_argument, NULL, 'P' },
2803 { "reboot", no_argument, NULL, 'r' },
2804 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2805 { NULL, 0, NULL, 0 }
2813 while ((c = getopt_long(argc, argv, "HPrhkt:a", options, NULL)) >= 0) {
2821 arg_action = ACTION_HALT;
2825 arg_action = ACTION_POWEROFF;
2829 arg_action = ACTION_REBOOT;
2833 if (arg_action != ACTION_HALT)
2834 arg_action = ACTION_POWEROFF;
2847 /* Compatibility nops */
2854 log_error("Unknown option code %c", c);
2859 if (argc > optind && !streq(argv[optind], "now"))
2860 log_warning("First argument '%s' isn't 'now'. Ignoring.", argv[optind]);
2862 /* We ignore the time argument */
2863 if (argc > optind + 1)
2864 arg_wall = argv + optind + 1;
2871 static int telinit_parse_argv(int argc, char *argv[]) {
2878 static const struct option options[] = {
2879 { "help", no_argument, NULL, ARG_HELP },
2880 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2881 { NULL, 0, NULL, 0 }
2884 static const struct {
2888 { '0', ACTION_POWEROFF },
2889 { '6', ACTION_REBOOT },
2890 { '1', ACTION_RESCUE },
2891 { '2', ACTION_RUNLEVEL2 },
2892 { '3', ACTION_RUNLEVEL3 },
2893 { '4', ACTION_RUNLEVEL4 },
2894 { '5', ACTION_RUNLEVEL5 },
2895 { 's', ACTION_RESCUE },
2896 { 'S', ACTION_RESCUE },
2897 { 'q', ACTION_RELOAD },
2898 { 'Q', ACTION_RELOAD },
2899 { 'u', ACTION_REEXEC },
2900 { 'U', ACTION_REEXEC }
2909 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
2924 log_error("Unknown option code %c", c);
2929 if (optind >= argc) {
2934 if (optind + 1 < argc) {
2935 log_error("Too many arguments.");
2939 if (strlen(argv[optind]) != 1) {
2940 log_error("Expected single character argument.");
2944 for (i = 0; i < ELEMENTSOF(table); i++)
2945 if (table[i].from == argv[optind][0])
2948 if (i >= ELEMENTSOF(table)) {
2949 log_error("Unknown command %s.", argv[optind]);
2953 arg_action = table[i].to;
2960 static int runlevel_parse_argv(int argc, char *argv[]) {
2966 static const struct option options[] = {
2967 { "help", no_argument, NULL, ARG_HELP },
2968 { NULL, 0, NULL, 0 }
2976 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
2987 log_error("Unknown option code %c", c);
2992 if (optind < argc) {
2993 log_error("Too many arguments.");
3000 static int parse_argv(int argc, char *argv[]) {
3004 if (program_invocation_short_name) {
3006 if (strstr(program_invocation_short_name, "halt")) {
3007 arg_action = ACTION_HALT;
3008 return halt_parse_argv(argc, argv);
3009 } else if (strstr(program_invocation_short_name, "poweroff")) {
3010 arg_action = ACTION_POWEROFF;
3011 return halt_parse_argv(argc, argv);
3012 } else if (strstr(program_invocation_short_name, "reboot")) {
3013 arg_action = ACTION_REBOOT;
3014 return halt_parse_argv(argc, argv);
3015 } else if (strstr(program_invocation_short_name, "shutdown")) {
3016 arg_action = ACTION_POWEROFF;
3017 return shutdown_parse_argv(argc, argv);
3018 } else if (strstr(program_invocation_short_name, "init")) {
3019 arg_action = ACTION_INVALID;
3020 return telinit_parse_argv(argc, argv);
3021 } else if (strstr(program_invocation_short_name, "runlevel")) {
3022 arg_action = ACTION_RUNLEVEL;
3023 return runlevel_parse_argv(argc, argv);
3027 arg_action = ACTION_SYSTEMCTL;
3028 return systemctl_parse_argv(argc, argv);
3031 static int action_to_runlevel(void) {
3033 static const char table[_ACTION_MAX] = {
3034 [ACTION_HALT] = '0',
3035 [ACTION_POWEROFF] = '0',
3036 [ACTION_REBOOT] = '6',
3037 [ACTION_RUNLEVEL2] = '2',
3038 [ACTION_RUNLEVEL3] = '3',
3039 [ACTION_RUNLEVEL4] = '4',
3040 [ACTION_RUNLEVEL5] = '5',
3041 [ACTION_RESCUE] = '1'
3044 assert(arg_action < _ACTION_MAX);
3046 return table[arg_action];
3049 static int talk_upstart(void) {
3050 DBusMessage *m = NULL, *reply = NULL;
3052 int previous, rl, r;
3054 env1_buf[] = "RUNLEVEL=X",
3055 env2_buf[] = "PREVLEVEL=X";
3056 char *env1 = env1_buf, *env2 = env2_buf;
3057 const char *emit = "runlevel";
3058 dbus_bool_t b_false = FALSE;
3059 DBusMessageIter iter, sub;
3060 DBusConnection *bus;
3062 dbus_error_init(&error);
3064 if (!(rl = action_to_runlevel()))
3067 if (utmp_get_runlevel(&previous, NULL) < 0)
3070 if (!(bus = dbus_connection_open_private("unix:abstract=/com/ubuntu/upstart", &error))) {
3071 if (dbus_error_has_name(&error, DBUS_ERROR_NO_SERVER)) {
3076 log_error("Failed to connect to Upstart bus: %s", error.message);
3081 if ((r = bus_check_peercred(bus)) < 0) {
3082 log_error("Failed to verify owner of bus.");
3086 if (!(m = dbus_message_new_method_call(
3087 "com.ubuntu.Upstart",
3088 "/com/ubuntu/Upstart",
3089 "com.ubuntu.Upstart0_6",
3092 log_error("Could not allocate message.");
3097 dbus_message_iter_init_append(m, &iter);
3099 env1_buf[sizeof(env1_buf)-2] = rl;
3100 env2_buf[sizeof(env2_buf)-2] = previous;
3102 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
3103 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
3104 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
3105 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
3106 !dbus_message_iter_close_container(&iter, &sub) ||
3107 !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
3108 log_error("Could not append arguments to message.");
3113 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3115 if (error_is_no_service(&error)) {
3120 log_error("Failed to issue method call: %s", error.message);
3129 dbus_message_unref(m);
3132 dbus_message_unref(reply);
3135 dbus_connection_close(bus);
3136 dbus_connection_unref(bus);
3139 dbus_error_free(&error);
3144 static int talk_initctl(void) {
3145 struct init_request request;
3149 if (!(rl = action_to_runlevel()))
3153 request.magic = INIT_MAGIC;
3154 request.sleeptime = 0;
3155 request.cmd = INIT_CMD_RUNLVL;
3156 request.runlevel = rl;
3158 if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
3160 if (errno == ENOENT)
3163 log_error("Failed to open "INIT_FIFO": %m");
3168 r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
3169 close_nointr_nofail(fd);
3172 log_error("Failed to write to "INIT_FIFO": %m");
3173 return errno ? -errno : -EIO;
3179 static int systemctl_main(DBusConnection *bus, int argc, char *argv[]) {
3181 static const struct {
3189 int (* const dispatch)(DBusConnection *bus, char **args, unsigned n);
3191 { "list-units", LESS, 1, list_units },
3192 { "list-jobs", EQUAL, 1, list_jobs },
3193 { "clear-jobs", EQUAL, 1, clear_jobs },
3194 { "load", MORE, 2, load_unit },
3195 { "cancel", MORE, 2, cancel_job },
3196 { "start", MORE, 2, start_unit },
3197 { "stop", MORE, 2, start_unit },
3198 { "reload", MORE, 2, start_unit },
3199 { "restart", MORE, 2, start_unit },
3200 { "isolate", EQUAL, 2, start_unit },
3201 { "check", MORE, 2, check_unit },
3202 { "show", MORE, 1, show },
3203 { "status", MORE, 2, show },
3204 { "monitor", EQUAL, 1, monitor },
3205 { "dump", EQUAL, 1, dump },
3206 { "snapshot", LESS, 2, snapshot },
3207 { "delete", MORE, 2, delete_snapshot },
3208 { "daemon-reload", EQUAL, 1, clear_jobs },
3209 { "daemon-reexec", EQUAL, 1, clear_jobs },
3210 { "daemon-exit", EQUAL, 1, clear_jobs },
3211 { "show-environment", EQUAL, 1, show_enviroment },
3212 { "set-environment", MORE, 2, set_environment },
3213 { "unset-environment", MORE, 2, set_environment },
3214 { "halt", EQUAL, 1, start_special },
3215 { "poweroff", EQUAL, 1, start_special },
3216 { "reboot", EQUAL, 1, start_special },
3217 { "default", EQUAL, 1, start_special },
3218 { "rescue", EQUAL, 1, start_special },
3219 { "emergency", EQUAL, 1, start_special }
3229 left = argc - optind;
3232 /* Special rule: no arguments means "list-units" */
3235 if (streq(argv[optind], "help")) {
3240 for (i = 0; i < ELEMENTSOF(verbs); i++)
3241 if (streq(argv[optind], verbs[i].verb))
3244 if (i >= ELEMENTSOF(verbs)) {
3245 log_error("Unknown operation %s", argv[optind]);
3250 switch (verbs[i].argc_cmp) {
3253 if (left != verbs[i].argc) {
3254 log_error("Invalid number of arguments.");
3261 if (left < verbs[i].argc) {
3262 log_error("Too few arguments.");
3269 if (left > verbs[i].argc) {
3270 log_error("Too many arguments.");
3277 assert_not_reached("Unknown comparison operator.");
3280 return verbs[i].dispatch(bus, argv + optind, left);
3283 static int reload_with_fallback(DBusConnection *bus) {
3287 /* First, try systemd via D-Bus. */
3288 if ((r = clear_jobs(bus, NULL, 0)) > 0)
3292 /* Nothing else worked, so let's try signals */
3293 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
3295 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
3296 log_error("kill() failed: %m");
3303 static int start_with_fallback(DBusConnection *bus) {
3308 /* First, try systemd via D-Bus. */
3309 if ((r = start_unit(bus, NULL, 0)) > 0)
3312 /* Hmm, talking to systemd via D-Bus didn't work. Then
3313 * let's try to talk to Upstart via D-Bus. */
3314 if ((r = talk_upstart()) > 0)
3318 /* Nothing else worked, so let's try
3320 if ((r = talk_initctl()) != 0)
3323 log_error("Failed to talk to init daemon.");
3327 warn_wall(arg_action);
3331 static int halt_main(DBusConnection *bus) {
3334 if (geteuid() != 0) {
3335 log_error("Must to be root.");
3339 if (!arg_dry && !arg_immediate)
3340 return start_with_fallback(bus);
3343 if ((r = utmp_put_shutdown(0)) < 0)
3344 log_warning("Failed to write utmp record: %s", strerror(-r));
3352 /* Make sure C-A-D is handled by the kernel from this
3354 reboot(RB_ENABLE_CAD);
3356 switch (arg_action) {
3359 log_info("Halting");
3360 reboot(RB_HALT_SYSTEM);
3363 case ACTION_POWEROFF:
3364 log_info("Powering off");
3365 reboot(RB_POWER_OFF);
3369 log_info("Rebooting");
3370 reboot(RB_AUTOBOOT);
3374 assert_not_reached("Unknown halt action.");
3377 /* We should never reach this. */
3381 static int runlevel_main(void) {
3382 int r, runlevel, previous;
3384 if ((r = utmp_get_runlevel(&runlevel, &previous)) < 0) {
3390 previous <= 0 ? 'N' : previous,
3391 runlevel <= 0 ? 'N' : runlevel);
3396 int main(int argc, char*argv[]) {
3398 DBusConnection *bus = NULL;
3401 dbus_error_init(&error);
3403 log_parse_environment();
3405 if ((r = parse_argv(argc, argv)) < 0)
3412 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
3413 * let's shortcut this */
3414 if (arg_action == ACTION_RUNLEVEL) {
3415 retval = runlevel_main() < 0;
3419 bus_connect(arg_session ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &bus, &private_bus, &error);
3421 switch (arg_action) {
3423 case ACTION_SYSTEMCTL: {
3426 log_error("Failed to get D-Bus connection: %s", error.message);
3430 retval = systemctl_main(bus, argc, argv) < 0;
3435 case ACTION_POWEROFF:
3437 retval = halt_main(bus) < 0;
3440 case ACTION_RUNLEVEL2:
3441 case ACTION_RUNLEVEL3:
3442 case ACTION_RUNLEVEL4:
3443 case ACTION_RUNLEVEL5:
3445 case ACTION_EMERGENCY:
3446 case ACTION_DEFAULT:
3447 retval = start_with_fallback(bus) < 0;
3452 retval = reload_with_fallback(bus) < 0;
3455 case ACTION_INVALID:
3456 case ACTION_RUNLEVEL:
3458 assert_not_reached("Unknown action");
3464 dbus_connection_close(bus);
3465 dbus_connection_unref(bus);
3468 dbus_error_free(&error);