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_TARGET,
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" :
739 streq(args[0], "try-restart") ? "TryRestartUnit" :
740 streq(args[0], "reload-or-restart") ? "ReloadOrRestartUnit" :
741 streq(args[0], "reload-or-try-restart") ? "ReloadOrTryRestartUnit" :
745 (streq(args[0], "isolate") ||
746 streq(args[0], "rescue") ||
747 streq(args[0], "emergency")) ? "isolate" :
751 one_name = table[verb_to_action(args[0])];
754 assert(arg_action < ELEMENTSOF(table));
755 assert(table[arg_action]);
757 method = "StartUnit";
759 mode = (arg_action == ACTION_EMERGENCY ||
760 arg_action == ACTION_RESCUE) ? "isolate" : "replace";
762 one_name = table[arg_action];
766 if ((r = enable_wait_for_jobs(bus)) < 0) {
767 log_error("Could not watch jobs: %s", strerror(-r));
771 if (!(s = set_new(string_hash_func, string_compare_func))) {
772 log_error("Failed to allocate set.");
781 if ((r = start_unit_one(bus, method, one_name, mode, s)) <= 0)
784 for (i = 1; i < n; i++)
785 if ((r = start_unit_one(bus, method, args[i], mode, s)) < 0)
790 r = wait_for_jobs(bus, s);
799 static int start_special(DBusConnection *bus, char **args, unsigned n) {
805 r = start_unit(bus, args, n);
808 warn_wall(verb_to_action(args[0]));
813 static int check_unit(DBusConnection *bus, char **args, unsigned n) {
814 DBusMessage *m = NULL, *reply = NULL;
816 *interface = "org.freedesktop.systemd1.Unit",
817 *property = "ActiveState";
818 int r = -EADDRNOTAVAIL;
825 dbus_error_init(&error);
827 for (i = 1; i < n; i++) {
828 const char *path = NULL;
830 DBusMessageIter iter, sub;
832 if (!(m = dbus_message_new_method_call(
833 "org.freedesktop.systemd1",
834 "/org/freedesktop/systemd1",
835 "org.freedesktop.systemd1.Manager",
837 log_error("Could not allocate message.");
842 if (!dbus_message_append_args(m,
843 DBUS_TYPE_STRING, &args[i],
844 DBUS_TYPE_INVALID)) {
845 log_error("Could not append arguments to message.");
850 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
852 /* Hmm, cannot figure out anything about this unit... */
856 dbus_error_free(&error);
860 if (!dbus_message_get_args(reply, &error,
861 DBUS_TYPE_OBJECT_PATH, &path,
862 DBUS_TYPE_INVALID)) {
863 log_error("Failed to parse reply: %s", error.message);
868 dbus_message_unref(m);
869 if (!(m = dbus_message_new_method_call(
870 "org.freedesktop.systemd1",
872 "org.freedesktop.DBus.Properties",
874 log_error("Could not allocate message.");
879 if (!dbus_message_append_args(m,
880 DBUS_TYPE_STRING, &interface,
881 DBUS_TYPE_STRING, &property,
882 DBUS_TYPE_INVALID)) {
883 log_error("Could not append arguments to message.");
888 dbus_message_unref(reply);
889 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
890 log_error("Failed to issue method call: %s", error.message);
895 if (!dbus_message_iter_init(reply, &iter) ||
896 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
897 log_error("Failed to parse reply.");
902 dbus_message_iter_recurse(&iter, &sub);
904 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
905 log_error("Failed to parse reply.");
910 dbus_message_iter_get_basic(&sub, &state);
915 if (streq(state, "active") || startswith(state, "reloading"))
918 dbus_message_unref(m);
919 dbus_message_unref(reply);
925 dbus_message_unref(m);
928 dbus_message_unref(reply);
930 dbus_error_free(&error);
935 typedef struct ExecStatusInfo {
941 usec_t start_timestamp;
942 usec_t exit_timestamp;
947 LIST_FIELDS(struct ExecStatusInfo, exec);
950 static void exec_status_info_free(ExecStatusInfo *i) {
958 static int exec_status_info_deserialize(DBusMessageIter *sub, ExecStatusInfo *i) {
959 uint64_t start_timestamp, exit_timestamp;
960 DBusMessageIter sub2, sub3;
964 int32_t code, status;
970 if (dbus_message_iter_get_arg_type(sub) != DBUS_TYPE_STRUCT)
973 dbus_message_iter_recurse(sub, &sub2);
975 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0)
978 if (!(i->path = strdup(path)))
981 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_ARRAY ||
982 dbus_message_iter_get_element_type(&sub2) != DBUS_TYPE_STRING)
986 dbus_message_iter_recurse(&sub2, &sub3);
987 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
988 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
989 dbus_message_iter_next(&sub3);
994 if (!(i->argv = new0(char*, n+1)))
998 dbus_message_iter_recurse(&sub2, &sub3);
999 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1002 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1003 dbus_message_iter_get_basic(&sub3, &s);
1004 dbus_message_iter_next(&sub3);
1006 if (!(i->argv[n++] = strdup(s)))
1010 if (!dbus_message_iter_next(&sub2) ||
1011 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_BOOLEAN, &ignore, true) < 0 ||
1012 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_timestamp, true) < 0 ||
1013 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_timestamp, true) < 0 ||
1014 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, true) < 0 ||
1015 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &code, true) < 0 ||
1016 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &status, false) < 0)
1020 i->start_timestamp = (usec_t) start_timestamp;
1021 i->exit_timestamp = (usec_t) exit_timestamp;
1022 i->pid = (pid_t) pid;
1029 typedef struct UnitStatusInfo {
1031 const char *load_state;
1032 const char *active_state;
1033 const char *sub_state;
1035 const char *description;
1037 const char *fragment_path;
1038 const char *default_control_group;
1043 const char *status_text;
1046 usec_t start_timestamp;
1047 usec_t exit_timestamp;
1049 int exit_code, exit_status;
1052 unsigned n_accepted;
1053 unsigned n_connections;
1057 const char *sysfs_path;
1059 /* Mount, Automount */
1065 LIST_HEAD(ExecStatusInfo, exec);
1068 static void print_status_info(UnitStatusInfo *i) {
1073 /* This shows pretty information about a unit. See
1074 * print_property() for a low-level property printer */
1076 printf("%s", strna(i->id));
1078 if (i->description && !streq_ptr(i->id, i->description))
1079 printf(" - %s", i->description);
1083 if (i->fragment_path)
1084 printf("\t Loaded: %s (%s)\n", strna(i->load_state), i->fragment_path);
1085 else if (streq_ptr(i->load_state, "failed"))
1086 printf("\t Loaded: " ANSI_HIGHLIGHT_ON "%s" ANSI_HIGHLIGHT_OFF "\n", strna(i->load_state));
1088 printf("\t Loaded: %s\n", strna(i->load_state));
1090 if (streq_ptr(i->active_state, "maintenance")) {
1091 if (streq_ptr(i->active_state, i->sub_state))
1092 printf("\t Active: " ANSI_HIGHLIGHT_ON "%s" ANSI_HIGHLIGHT_OFF "\n",
1093 strna(i->active_state));
1095 printf("\t Active: " ANSI_HIGHLIGHT_ON "%s (%s)" ANSI_HIGHLIGHT_OFF "\n",
1096 strna(i->active_state),
1097 strna(i->sub_state));
1099 if (streq_ptr(i->active_state, i->sub_state))
1100 printf("\t Active: %s\n",
1101 strna(i->active_state));
1103 printf("\t Active: %s (%s)\n",
1104 strna(i->active_state),
1105 strna(i->sub_state));
1109 printf("\t Device: %s\n", i->sysfs_path);
1111 printf("\t Where: %s\n", i->where);
1113 printf("\t What: %s\n", i->what);
1116 printf("\tAccepted: %u; Connected: %u\n", i->n_accepted, i->n_connections);
1118 LIST_FOREACH(exec, p, i->exec) {
1121 /* Only show exited processes here */
1125 t = strv_join(p->argv, " ");
1126 printf("\t Exited: %u (%s, code=%s, ", p->pid, strna(t), sigchld_code_to_string(p->code));
1129 if (p->code == CLD_EXITED)
1130 printf("status=%i", p->status);
1132 printf("signal=%s", signal_to_string(p->status));
1135 if (i->main_pid == p->pid &&
1136 i->start_timestamp == p->start_timestamp &&
1137 i->exit_timestamp == p->start_timestamp)
1138 /* Let's not show this twice */
1141 if (p->pid == i->control_pid)
1145 if (i->main_pid > 0 || i->control_pid > 0) {
1148 if (i->main_pid > 0) {
1149 printf(" Main: %u", (unsigned) i->main_pid);
1153 get_process_name(i->main_pid, &t);
1159 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
1161 if (i->exit_code == CLD_EXITED)
1162 printf("status=%i", i->exit_status);
1164 printf("signal=%s", signal_to_string(i->exit_status));
1169 if (i->main_pid > 0 && i->control_pid > 0)
1172 if (i->control_pid > 0) {
1175 printf(" Control: %u", (unsigned) i->control_pid);
1177 get_process_name(i->control_pid, &t);
1188 printf("\t Status: \"%s\"\n", i->status_text);
1190 if (i->default_control_group) {
1193 printf("\t CGroup: %s\n", i->default_control_group);
1195 if ((c = columns()) > 18)
1200 show_cgroup_by_path(i->default_control_group, "\t\t ", c);
1204 static int status_property(const char *name, DBusMessageIter *iter, UnitStatusInfo *i) {
1206 switch (dbus_message_iter_get_arg_type(iter)) {
1208 case DBUS_TYPE_STRING: {
1211 dbus_message_iter_get_basic(iter, &s);
1214 if (streq(name, "Id"))
1216 else if (streq(name, "LoadState"))
1218 else if (streq(name, "ActiveState"))
1219 i->active_state = s;
1220 else if (streq(name, "SubState"))
1222 else if (streq(name, "Description"))
1224 else if (streq(name, "FragmentPath"))
1225 i->fragment_path = s;
1226 else if (streq(name, "DefaultControlGroup"))
1227 i->default_control_group = s;
1228 else if (streq(name, "StatusText"))
1230 else if (streq(name, "SysFSPath"))
1232 else if (streq(name, "Where"))
1234 else if (streq(name, "What"))
1241 case DBUS_TYPE_BOOLEAN: {
1244 dbus_message_iter_get_basic(iter, &b);
1246 if (streq(name, "Accept"))
1252 case DBUS_TYPE_UINT32: {
1255 dbus_message_iter_get_basic(iter, &u);
1257 if (streq(name, "MainPID")) {
1259 i->main_pid = (pid_t) u;
1262 } else if (streq(name, "ControlPID"))
1263 i->control_pid = (pid_t) u;
1264 else if (streq(name, "ExecMainPID")) {
1266 i->main_pid = (pid_t) u;
1267 } else if (streq(name, "NAccepted"))
1269 else if (streq(name, "NConnections"))
1270 i->n_connections = u;
1275 case DBUS_TYPE_INT32: {
1278 dbus_message_iter_get_basic(iter, &j);
1280 if (streq(name, "ExecMainCode"))
1281 i->exit_code = (int) j;
1282 else if (streq(name, "ExecMainStatus"))
1283 i->exit_status = (int) j;
1288 case DBUS_TYPE_UINT64: {
1291 dbus_message_iter_get_basic(iter, &u);
1293 if (streq(name, "ExecMainStartTimestamp"))
1294 i->start_timestamp = (usec_t) u;
1295 else if (streq(name, "ExecMainExitTimestamp"))
1296 i->exit_timestamp = (usec_t) u;
1301 case DBUS_TYPE_ARRAY: {
1303 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT &&
1304 startswith(name, "Exec")) {
1305 DBusMessageIter sub;
1307 dbus_message_iter_recurse(iter, &sub);
1308 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1309 ExecStatusInfo *info;
1312 if (!(info = new0(ExecStatusInfo, 1)))
1315 if ((r = exec_status_info_deserialize(&sub, info)) < 0) {
1320 LIST_PREPEND(ExecStatusInfo, exec, i->exec, info);
1322 dbus_message_iter_next(&sub);
1333 static int print_property(const char *name, DBusMessageIter *iter) {
1337 /* This is a low-level property printer, see
1338 * print_status_info() for the nicer output */
1340 if (arg_property && !streq(name, arg_property))
1343 switch (dbus_message_iter_get_arg_type(iter)) {
1345 case DBUS_TYPE_STRING: {
1347 dbus_message_iter_get_basic(iter, &s);
1349 if (arg_all || s[0])
1350 printf("%s=%s\n", name, s);
1355 case DBUS_TYPE_BOOLEAN: {
1357 dbus_message_iter_get_basic(iter, &b);
1358 printf("%s=%s\n", name, yes_no(b));
1363 case DBUS_TYPE_UINT64: {
1365 dbus_message_iter_get_basic(iter, &u);
1367 /* Yes, heuristics! But we can change this check
1368 * should it turn out to not be sufficient */
1370 if (strstr(name, "Timestamp")) {
1371 char timestamp[FORMAT_TIMESTAMP_MAX], *t;
1373 if ((t = format_timestamp(timestamp, sizeof(timestamp), u)) || arg_all)
1374 printf("%s=%s\n", name, strempty(t));
1375 } else if (strstr(name, "USec")) {
1376 char timespan[FORMAT_TIMESPAN_MAX];
1378 printf("%s=%s\n", name, format_timespan(timespan, sizeof(timespan), u));
1380 printf("%s=%llu\n", name, (unsigned long long) u);
1385 case DBUS_TYPE_UINT32: {
1387 dbus_message_iter_get_basic(iter, &u);
1389 if (strstr(name, "UMask") || strstr(name, "Mode"))
1390 printf("%s=%04o\n", name, u);
1392 printf("%s=%u\n", name, (unsigned) u);
1397 case DBUS_TYPE_INT32: {
1399 dbus_message_iter_get_basic(iter, &i);
1401 printf("%s=%i\n", name, (int) i);
1405 case DBUS_TYPE_STRUCT: {
1406 DBusMessageIter sub;
1407 dbus_message_iter_recurse(iter, &sub);
1409 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "Job")) {
1412 dbus_message_iter_get_basic(&sub, &u);
1415 printf("%s=%u\n", name, (unsigned) u);
1417 printf("%s=\n", name);
1420 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Unit")) {
1423 dbus_message_iter_get_basic(&sub, &s);
1425 if (arg_all || s[0])
1426 printf("%s=%s\n", name, s);
1434 case DBUS_TYPE_ARRAY:
1436 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
1437 DBusMessageIter sub;
1440 dbus_message_iter_recurse(iter, &sub);
1442 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1443 printf("%s=", name);
1445 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1448 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
1449 dbus_message_iter_get_basic(&sub, &s);
1450 printf("%s%s", space ? " " : "", s);
1453 dbus_message_iter_next(&sub);
1461 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_BYTE) {
1462 DBusMessageIter sub;
1464 dbus_message_iter_recurse(iter, &sub);
1466 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1467 printf("%s=", name);
1469 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1472 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_BYTE);
1473 dbus_message_iter_get_basic(&sub, &u);
1476 dbus_message_iter_next(&sub);
1484 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Paths")) {
1485 DBusMessageIter sub, sub2;
1487 dbus_message_iter_recurse(iter, &sub);
1488 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1489 const char *type, *path;
1491 dbus_message_iter_recurse(&sub, &sub2);
1493 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
1494 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0)
1495 printf("%s=%s\n", type, path);
1497 dbus_message_iter_next(&sub);
1502 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Timers")) {
1503 DBusMessageIter sub, sub2;
1505 dbus_message_iter_recurse(iter, &sub);
1506 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1508 uint64_t value, next_elapse;
1510 dbus_message_iter_recurse(&sub, &sub2);
1512 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &base, true) >= 0 &&
1513 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &value, true) >= 0 &&
1514 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &next_elapse, false) >= 0) {
1515 char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX];
1517 printf("%s={ value=%s ; next_elapse=%s }\n",
1519 format_timespan(timespan1, sizeof(timespan1), value),
1520 format_timespan(timespan2, sizeof(timespan2), next_elapse));
1523 dbus_message_iter_next(&sub);
1528 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && startswith(name, "Exec")) {
1529 DBusMessageIter sub;
1531 dbus_message_iter_recurse(iter, &sub);
1532 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1533 ExecStatusInfo info;
1536 if (exec_status_info_deserialize(&sub, &info) >= 0) {
1537 char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
1540 t = strv_join(info.argv, " ");
1542 printf("%s={ path=%s ; argv[]=%s ; ignore=%s ; start_time=[%s] ; stop_time=[%s] ; pid=%u ; code=%s ; status=%i%s%s }\n",
1546 yes_no(info.ignore),
1547 strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)),
1548 strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)),
1549 (unsigned) info. pid,
1550 sigchld_code_to_string(info.code),
1552 info.code == CLD_EXITED ? "" : "/",
1553 strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status)));
1559 strv_free(info.argv);
1561 dbus_message_iter_next(&sub);
1571 printf("%s=[unprintable]\n", name);
1576 static int show_one(DBusConnection *bus, const char *path, bool show_properties, bool *new_line) {
1577 DBusMessage *m = NULL, *reply = NULL;
1578 const char *interface = "";
1581 DBusMessageIter iter, sub, sub2, sub3;
1582 UnitStatusInfo info;
1590 dbus_error_init(&error);
1592 if (!(m = dbus_message_new_method_call(
1593 "org.freedesktop.systemd1",
1595 "org.freedesktop.DBus.Properties",
1597 log_error("Could not allocate message.");
1602 if (!dbus_message_append_args(m,
1603 DBUS_TYPE_STRING, &interface,
1604 DBUS_TYPE_INVALID)) {
1605 log_error("Could not append arguments to message.");
1610 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1611 log_error("Failed to issue method call: %s", error.message);
1616 if (!dbus_message_iter_init(reply, &iter) ||
1617 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
1618 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
1619 log_error("Failed to parse reply.");
1624 dbus_message_iter_recurse(&iter, &sub);
1631 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1634 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
1635 log_error("Failed to parse reply.");
1640 dbus_message_iter_recurse(&sub, &sub2);
1642 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
1643 log_error("Failed to parse reply.");
1648 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
1649 log_error("Failed to parse reply.");
1654 dbus_message_iter_recurse(&sub2, &sub3);
1656 if (show_properties)
1657 r = print_property(name, &sub3);
1659 r = status_property(name, &sub3, &info);
1662 log_error("Failed to parse reply.");
1667 dbus_message_iter_next(&sub);
1670 if (!show_properties)
1671 print_status_info(&info);
1673 while ((p = info.exec)) {
1674 LIST_REMOVE(ExecStatusInfo, exec, info.exec, p);
1675 exec_status_info_free(p);
1682 dbus_message_unref(m);
1685 dbus_message_unref(reply);
1687 dbus_error_free(&error);
1692 static int show(DBusConnection *bus, char **args, unsigned n) {
1693 DBusMessage *m = NULL, *reply = NULL;
1697 bool show_properties, new_line = false;
1702 dbus_error_init(&error);
1704 show_properties = !streq(args[0], "status");
1706 if (show_properties && n <= 1) {
1707 /* If not argument is specified inspect the manager
1710 r = show_one(bus, "/org/freedesktop/systemd1", show_properties, &new_line);
1714 for (i = 1; i < n; i++) {
1715 const char *path = NULL;
1718 if (!show_properties || safe_atou32(args[i], &id) < 0) {
1720 if (!(m = dbus_message_new_method_call(
1721 "org.freedesktop.systemd1",
1722 "/org/freedesktop/systemd1",
1723 "org.freedesktop.systemd1.Manager",
1725 log_error("Could not allocate message.");
1730 if (!dbus_message_append_args(m,
1731 DBUS_TYPE_STRING, &args[i],
1732 DBUS_TYPE_INVALID)) {
1733 log_error("Could not append arguments to message.");
1738 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1740 if (!dbus_error_has_name(&error, DBUS_ERROR_ACCESS_DENIED)) {
1741 log_error("Failed to issue method call: %s", error.message);
1746 dbus_error_free(&error);
1748 dbus_message_unref(m);
1749 if (!(m = dbus_message_new_method_call(
1750 "org.freedesktop.systemd1",
1751 "/org/freedesktop/systemd1",
1752 "org.freedesktop.systemd1.Manager",
1754 log_error("Could not allocate message.");
1759 if (!dbus_message_append_args(m,
1760 DBUS_TYPE_STRING, &args[i],
1761 DBUS_TYPE_INVALID)) {
1762 log_error("Could not append arguments to message.");
1767 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1768 log_error("Failed to issue method call: %s", error.message);
1776 if (!(m = dbus_message_new_method_call(
1777 "org.freedesktop.systemd1",
1778 "/org/freedesktop/systemd1",
1779 "org.freedesktop.systemd1.Manager",
1781 log_error("Could not allocate message.");
1786 if (!dbus_message_append_args(m,
1787 DBUS_TYPE_UINT32, &id,
1788 DBUS_TYPE_INVALID)) {
1789 log_error("Could not append arguments to message.");
1794 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1795 log_error("Failed to issue method call: %s", error.message);
1801 if (!dbus_message_get_args(reply, &error,
1802 DBUS_TYPE_OBJECT_PATH, &path,
1803 DBUS_TYPE_INVALID)) {
1804 log_error("Failed to parse reply: %s", error.message);
1809 if ((r = show_one(bus, path, show_properties, &new_line)) < 0)
1812 dbus_message_unref(m);
1813 dbus_message_unref(reply);
1821 dbus_message_unref(m);
1824 dbus_message_unref(reply);
1826 dbus_error_free(&error);
1831 static DBusHandlerResult monitor_filter(DBusConnection *connection, DBusMessage *message, void *data) {
1833 DBusMessage *m = NULL, *reply = NULL;
1838 dbus_error_init(&error);
1840 log_debug("Got D-Bus request: %s.%s() on %s",
1841 dbus_message_get_interface(message),
1842 dbus_message_get_member(message),
1843 dbus_message_get_path(message));
1845 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
1846 log_error("Warning! D-Bus connection terminated.");
1847 dbus_connection_close(connection);
1849 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitNew") ||
1850 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitRemoved")) {
1851 const char *id, *path;
1853 if (!dbus_message_get_args(message, &error,
1854 DBUS_TYPE_STRING, &id,
1855 DBUS_TYPE_OBJECT_PATH, &path,
1857 log_error("Failed to parse message: %s", error.message);
1858 else if (streq(dbus_message_get_member(message), "UnitNew"))
1859 printf("Unit %s added.\n", id);
1861 printf("Unit %s removed.\n", id);
1863 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobNew") ||
1864 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
1868 if (!dbus_message_get_args(message, &error,
1869 DBUS_TYPE_UINT32, &id,
1870 DBUS_TYPE_OBJECT_PATH, &path,
1872 log_error("Failed to parse message: %s", error.message);
1873 else if (streq(dbus_message_get_member(message), "JobNew"))
1874 printf("Job %u added.\n", id);
1876 printf("Job %u removed.\n", id);
1879 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Unit", "Changed") ||
1880 dbus_message_is_signal(message, "org.freedesktop.systemd1.Job", "Changed")) {
1882 const char *path, *interface, *property = "Id";
1883 DBusMessageIter iter, sub;
1885 path = dbus_message_get_path(message);
1886 interface = dbus_message_get_interface(message);
1888 if (!(m = dbus_message_new_method_call(
1889 "org.freedesktop.systemd1",
1891 "org.freedesktop.DBus.Properties",
1893 log_error("Could not allocate message.");
1897 if (!dbus_message_append_args(m,
1898 DBUS_TYPE_STRING, &interface,
1899 DBUS_TYPE_STRING, &property,
1900 DBUS_TYPE_INVALID)) {
1901 log_error("Could not append arguments to message.");
1905 if (!(reply = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) {
1906 log_error("Failed to issue method call: %s", error.message);
1910 if (!dbus_message_iter_init(reply, &iter) ||
1911 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1912 log_error("Failed to parse reply.");
1916 dbus_message_iter_recurse(&iter, &sub);
1918 if (streq(interface, "org.freedesktop.systemd1.Unit")) {
1921 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1922 log_error("Failed to parse reply.");
1926 dbus_message_iter_get_basic(&sub, &id);
1927 printf("Unit %s changed.\n", id);
1931 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT32) {
1932 log_error("Failed to parse reply.");
1936 dbus_message_iter_get_basic(&sub, &id);
1937 printf("Job %u changed.\n", id);
1943 dbus_message_unref(m);
1946 dbus_message_unref(reply);
1948 dbus_error_free(&error);
1949 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1953 dbus_message_unref(m);
1956 dbus_message_unref(reply);
1958 dbus_error_free(&error);
1959 return DBUS_HANDLER_RESULT_NEED_MEMORY;
1962 static int monitor(DBusConnection *bus, char **args, unsigned n) {
1963 DBusMessage *m = NULL, *reply = NULL;
1967 dbus_error_init(&error);
1970 dbus_bus_add_match(bus,
1972 "sender='org.freedesktop.systemd1',"
1973 "interface='org.freedesktop.systemd1.Manager',"
1974 "path='/org/freedesktop/systemd1'",
1977 if (dbus_error_is_set(&error)) {
1978 log_error("Failed to add match: %s", error.message);
1983 dbus_bus_add_match(bus,
1985 "sender='org.freedesktop.systemd1',"
1986 "interface='org.freedesktop.systemd1.Unit',"
1990 if (dbus_error_is_set(&error)) {
1991 log_error("Failed to add match: %s", error.message);
1996 dbus_bus_add_match(bus,
1998 "sender='org.freedesktop.systemd1',"
1999 "interface='org.freedesktop.systemd1.Job',"
2003 if (dbus_error_is_set(&error)) {
2004 log_error("Failed to add match: %s", error.message);
2010 if (!dbus_connection_add_filter(bus, monitor_filter, NULL, NULL)) {
2011 log_error("Failed to add filter.");
2016 if (!(m = dbus_message_new_method_call(
2017 "org.freedesktop.systemd1",
2018 "/org/freedesktop/systemd1",
2019 "org.freedesktop.systemd1.Manager",
2021 log_error("Could not allocate message.");
2026 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2027 log_error("Failed to issue method call: %s", error.message);
2032 while (dbus_connection_read_write_dispatch(bus, -1))
2039 /* This is slightly dirty, since we don't undo the filter or the matches. */
2042 dbus_message_unref(m);
2045 dbus_message_unref(reply);
2047 dbus_error_free(&error);
2052 static int dump(DBusConnection *bus, char **args, unsigned n) {
2053 DBusMessage *m = NULL, *reply = NULL;
2058 dbus_error_init(&error);
2060 if (!(m = dbus_message_new_method_call(
2061 "org.freedesktop.systemd1",
2062 "/org/freedesktop/systemd1",
2063 "org.freedesktop.systemd1.Manager",
2065 log_error("Could not allocate message.");
2069 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2070 log_error("Failed to issue method call: %s", error.message);
2075 if (!dbus_message_get_args(reply, &error,
2076 DBUS_TYPE_STRING, &text,
2077 DBUS_TYPE_INVALID)) {
2078 log_error("Failed to parse reply: %s", error.message);
2083 fputs(text, stdout);
2089 dbus_message_unref(m);
2092 dbus_message_unref(reply);
2094 dbus_error_free(&error);
2099 static int snapshot(DBusConnection *bus, char **args, unsigned n) {
2100 DBusMessage *m = NULL, *reply = NULL;
2103 const char *name = "", *path, *id;
2104 dbus_bool_t cleanup = FALSE;
2105 DBusMessageIter iter, sub;
2107 *interface = "org.freedesktop.systemd1.Unit",
2110 dbus_error_init(&error);
2112 if (!(m = dbus_message_new_method_call(
2113 "org.freedesktop.systemd1",
2114 "/org/freedesktop/systemd1",
2115 "org.freedesktop.systemd1.Manager",
2116 "CreateSnapshot"))) {
2117 log_error("Could not allocate message.");
2124 if (!dbus_message_append_args(m,
2125 DBUS_TYPE_STRING, &name,
2126 DBUS_TYPE_BOOLEAN, &cleanup,
2127 DBUS_TYPE_INVALID)) {
2128 log_error("Could not append arguments to message.");
2133 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2134 log_error("Failed to issue method call: %s", error.message);
2139 if (!dbus_message_get_args(reply, &error,
2140 DBUS_TYPE_OBJECT_PATH, &path,
2141 DBUS_TYPE_INVALID)) {
2142 log_error("Failed to parse reply: %s", error.message);
2147 dbus_message_unref(m);
2148 if (!(m = dbus_message_new_method_call(
2149 "org.freedesktop.systemd1",
2151 "org.freedesktop.DBus.Properties",
2153 log_error("Could not allocate message.");
2157 if (!dbus_message_append_args(m,
2158 DBUS_TYPE_STRING, &interface,
2159 DBUS_TYPE_STRING, &property,
2160 DBUS_TYPE_INVALID)) {
2161 log_error("Could not append arguments to message.");
2166 dbus_message_unref(reply);
2167 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2168 log_error("Failed to issue method call: %s", error.message);
2173 if (!dbus_message_iter_init(reply, &iter) ||
2174 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2175 log_error("Failed to parse reply.");
2180 dbus_message_iter_recurse(&iter, &sub);
2182 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2183 log_error("Failed to parse reply.");
2188 dbus_message_iter_get_basic(&sub, &id);
2196 dbus_message_unref(m);
2199 dbus_message_unref(reply);
2201 dbus_error_free(&error);
2206 static int delete_snapshot(DBusConnection *bus, char **args, unsigned n) {
2207 DBusMessage *m = NULL, *reply = NULL;
2215 dbus_error_init(&error);
2217 for (i = 1; i < n; i++) {
2218 const char *path = NULL;
2220 if (!(m = dbus_message_new_method_call(
2221 "org.freedesktop.systemd1",
2222 "/org/freedesktop/systemd1",
2223 "org.freedesktop.systemd1.Manager",
2225 log_error("Could not allocate message.");
2230 if (!dbus_message_append_args(m,
2231 DBUS_TYPE_STRING, &args[i],
2232 DBUS_TYPE_INVALID)) {
2233 log_error("Could not append arguments to message.");
2238 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2239 log_error("Failed to issue method call: %s", error.message);
2244 if (!dbus_message_get_args(reply, &error,
2245 DBUS_TYPE_OBJECT_PATH, &path,
2246 DBUS_TYPE_INVALID)) {
2247 log_error("Failed to parse reply: %s", error.message);
2252 dbus_message_unref(m);
2253 if (!(m = dbus_message_new_method_call(
2254 "org.freedesktop.systemd1",
2256 "org.freedesktop.systemd1.Snapshot",
2258 log_error("Could not allocate message.");
2263 dbus_message_unref(reply);
2264 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2265 log_error("Failed to issue method call: %s", error.message);
2270 dbus_message_unref(m);
2271 dbus_message_unref(reply);
2279 dbus_message_unref(m);
2282 dbus_message_unref(reply);
2284 dbus_error_free(&error);
2289 static int clear_jobs(DBusConnection *bus, char **args, unsigned n) {
2290 DBusMessage *m = NULL, *reply = NULL;
2295 dbus_error_init(&error);
2297 if (arg_action == ACTION_RELOAD)
2299 else if (arg_action == ACTION_REEXEC)
2300 method = "Reexecute";
2302 assert(arg_action == ACTION_SYSTEMCTL);
2305 streq(args[0], "clear-jobs") ? "ClearJobs" :
2306 streq(args[0], "daemon-reload") ? "Reload" :
2307 streq(args[0], "daemon-reexec") ? "Reexecute" :
2311 if (!(m = dbus_message_new_method_call(
2312 "org.freedesktop.systemd1",
2313 "/org/freedesktop/systemd1",
2314 "org.freedesktop.systemd1.Manager",
2316 log_error("Could not allocate message.");
2320 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2322 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
2323 /* There's always a fallback possible for
2324 * legacy actions. */
2329 log_error("Failed to issue method call: %s", error.message);
2338 dbus_message_unref(m);
2341 dbus_message_unref(reply);
2343 dbus_error_free(&error);
2348 static int show_enviroment(DBusConnection *bus, char **args, unsigned n) {
2349 DBusMessage *m = NULL, *reply = NULL;
2351 DBusMessageIter iter, sub, sub2;
2354 *interface = "org.freedesktop.systemd1.Manager",
2355 *property = "Environment";
2357 dbus_error_init(&error);
2359 if (!(m = dbus_message_new_method_call(
2360 "org.freedesktop.systemd1",
2361 "/org/freedesktop/systemd1",
2362 "org.freedesktop.DBus.Properties",
2364 log_error("Could not allocate message.");
2368 if (!dbus_message_append_args(m,
2369 DBUS_TYPE_STRING, &interface,
2370 DBUS_TYPE_STRING, &property,
2371 DBUS_TYPE_INVALID)) {
2372 log_error("Could not append arguments to message.");
2377 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2378 log_error("Failed to issue method call: %s", error.message);
2383 if (!dbus_message_iter_init(reply, &iter) ||
2384 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2385 log_error("Failed to parse reply.");
2390 dbus_message_iter_recurse(&iter, &sub);
2392 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
2393 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
2394 log_error("Failed to parse reply.");
2399 dbus_message_iter_recurse(&sub, &sub2);
2401 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
2404 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
2405 log_error("Failed to parse reply.");
2410 dbus_message_iter_get_basic(&sub2, &text);
2411 printf("%s\n", text);
2413 dbus_message_iter_next(&sub2);
2420 dbus_message_unref(m);
2423 dbus_message_unref(reply);
2425 dbus_error_free(&error);
2430 static int set_environment(DBusConnection *bus, char **args, unsigned n) {
2431 DBusMessage *m = NULL, *reply = NULL;
2435 DBusMessageIter iter, sub;
2438 dbus_error_init(&error);
2440 method = streq(args[0], "set-environment")
2442 : "UnsetEnvironment";
2444 if (!(m = dbus_message_new_method_call(
2445 "org.freedesktop.systemd1",
2446 "/org/freedesktop/systemd1",
2447 "org.freedesktop.systemd1.Manager",
2450 log_error("Could not allocate message.");
2454 dbus_message_iter_init_append(m, &iter);
2456 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
2457 log_error("Could not append arguments to message.");
2462 for (i = 1; i < n; i++)
2463 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &args[i])) {
2464 log_error("Could not append arguments to message.");
2469 if (!dbus_message_iter_close_container(&iter, &sub)) {
2470 log_error("Could not append arguments to message.");
2475 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2476 log_error("Failed to issue method call: %s", error.message);
2485 dbus_message_unref(m);
2488 dbus_message_unref(reply);
2490 dbus_error_free(&error);
2495 static int systemctl_help(void) {
2497 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
2498 "Send control commands to the systemd manager.\n\n"
2499 " -h --help Show this help\n"
2500 " -t --type=TYPE List only units of a particular type\n"
2501 " -p --property=NAME Show only properties by this name\n"
2502 " -a --all Show all units/properties, including dead/empty ones\n"
2503 " --fail When installing a new job, fail if conflicting jobs are\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 " reload [NAME...] Reload one or more units\n"
2515 " restart [NAME...] Start or restart one or more units\n"
2516 " try-restart [NAME...] Restart one or more units if active\n"
2517 " reload-or-restart [NAME...] Reload one or more units is possible,\n"
2518 " otherwise start or restart\n"
2519 " reload-or-try-restart [NAME...] Reload one or more units is possible,\n"
2520 " otherwise restart if active\n"
2521 " isolate [NAME] Start one unit and stop all others\n"
2522 " check [NAME...] Check whether units are active\n"
2523 " status [NAME...] Show status of one or more units\n"
2524 " show [NAME...|JOB...] Show properties of one or more\n"
2525 " units/jobs/manager\n"
2526 " load [NAME...] Load one or more units\n"
2527 " list-jobs List jobs\n"
2528 " cancel [JOB...] Cancel one or more jobs\n"
2529 " clear-jobs Cancel all jobs\n"
2530 " monitor Monitor unit/job changes\n"
2531 " dump Dump server status\n"
2532 " snapshot [NAME] Create a snapshot\n"
2533 " delete [NAME...] Remove one or more snapshots\n"
2534 " daemon-reload Reload systemd manager configuration\n"
2535 " daemon-reexec Reexecute systemd manager\n"
2536 " daemon-exit Ask the systemd manager to quit\n"
2537 " show-environment Dump environment\n"
2538 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
2539 " unset-environment [NAME...] Unset one or more environment variables\n"
2540 " halt Shut down and halt the system\n"
2541 " poweroff Shut down and power-off the system\n"
2542 " reboot Shut down and reboot the system\n"
2543 " rescue Enter system rescue mode\n"
2544 " emergency Enter system emergency mode\n"
2545 " default Enter system default mode\n",
2546 program_invocation_short_name);
2551 static int halt_help(void) {
2553 printf("%s [OPTIONS...]\n\n"
2554 "%s the system.\n\n"
2555 " --help Show this help\n"
2556 " --halt Halt the machine\n"
2557 " -p --poweroff Switch off the machine\n"
2558 " --reboot Reboot the machine\n"
2559 " -f --force Force immediate halt/power-off/reboot\n"
2560 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
2561 " -d --no-wtmp Don't write wtmp record\n"
2562 " -n --no-sync Don't sync before halt/power-off/reboot\n"
2563 " --no-wall Don't send wall message before halt/power-off/reboot\n",
2564 program_invocation_short_name,
2565 arg_action == ACTION_REBOOT ? "Reboot" :
2566 arg_action == ACTION_POWEROFF ? "Power off" :
2572 static int shutdown_help(void) {
2574 printf("%s [OPTIONS...] [now] [WALL...]\n\n"
2575 "Shut down the system.\n\n"
2576 " --help Show this help\n"
2577 " -H --halt Halt the machine\n"
2578 " -P --poweroff Power-off the machine\n"
2579 " -r --reboot Reboot the machine\n"
2580 " -h Equivalent to --poweroff, overriden by --halt\n"
2581 " -k Don't halt/power-off/reboot, just send warnings\n"
2582 " --no-wall Don't send wall message before halt/power-off/reboot\n",
2583 program_invocation_short_name);
2588 static int telinit_help(void) {
2590 printf("%s [OPTIONS...] {COMMAND}\n\n"
2591 "Send control commands to the init daemon.\n\n"
2592 " --help Show this help\n"
2593 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
2595 " 0 Power-off the machine\n"
2596 " 6 Reboot the machine\n"
2597 " 2, 3, 4, 5 Start runlevelX.target unit\n"
2598 " 1, s, S Enter rescue mode\n"
2599 " q, Q Reload init daemon configuration\n"
2600 " u, U Reexecute init daemon\n",
2601 program_invocation_short_name);
2606 static int runlevel_help(void) {
2608 printf("%s [OPTIONS...]\n\n"
2609 "Prints the previous and current runlevel of the init system.\n\n"
2610 " --help Show this help\n",
2611 program_invocation_short_name);
2616 static int systemctl_parse_argv(int argc, char *argv[]) {
2626 static const struct option options[] = {
2627 { "help", no_argument, NULL, 'h' },
2628 { "type", required_argument, NULL, 't' },
2629 { "property", required_argument, NULL, 'p' },
2630 { "all", no_argument, NULL, 'a' },
2631 { "fail", no_argument, NULL, ARG_FAIL },
2632 { "session", no_argument, NULL, ARG_SESSION },
2633 { "system", no_argument, NULL, ARG_SYSTEM },
2634 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
2635 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2636 { "quiet", no_argument, NULL, 'q' },
2637 { NULL, 0, NULL, 0 }
2645 while ((c = getopt_long(argc, argv, "ht:p:aq", options, NULL)) >= 0) {
2658 arg_property = optarg;
2660 /* If the user asked for a particular
2661 * property, show it to him, even if it is
2679 arg_session = false;
2683 arg_no_block = true;
2698 log_error("Unknown option code %c", c);
2706 static int halt_parse_argv(int argc, char *argv[]) {
2715 static const struct option options[] = {
2716 { "help", no_argument, NULL, ARG_HELP },
2717 { "halt", no_argument, NULL, ARG_HALT },
2718 { "poweroff", no_argument, NULL, 'p' },
2719 { "reboot", no_argument, NULL, ARG_REBOOT },
2720 { "force", no_argument, NULL, 'f' },
2721 { "wtmp-only", no_argument, NULL, 'w' },
2722 { "no-wtmp", no_argument, NULL, 'd' },
2723 { "no-sync", no_argument, NULL, 'n' },
2724 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2725 { NULL, 0, NULL, 0 }
2733 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
2734 if (runlevel == '0' || runlevel == '6')
2735 arg_immediate = true;
2737 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
2745 arg_action = ACTION_HALT;
2749 arg_action = ACTION_POWEROFF;
2753 arg_action = ACTION_REBOOT;
2757 arg_immediate = true;
2778 /* Compatibility nops */
2785 log_error("Unknown option code %c", c);
2790 if (optind < argc) {
2791 log_error("Too many arguments.");
2798 static int shutdown_parse_argv(int argc, char *argv[]) {
2805 static const struct option options[] = {
2806 { "help", no_argument, NULL, ARG_HELP },
2807 { "halt", no_argument, NULL, 'H' },
2808 { "poweroff", no_argument, NULL, 'P' },
2809 { "reboot", no_argument, NULL, 'r' },
2810 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2811 { NULL, 0, NULL, 0 }
2819 while ((c = getopt_long(argc, argv, "HPrhkt:a", options, NULL)) >= 0) {
2827 arg_action = ACTION_HALT;
2831 arg_action = ACTION_POWEROFF;
2835 arg_action = ACTION_REBOOT;
2839 if (arg_action != ACTION_HALT)
2840 arg_action = ACTION_POWEROFF;
2853 /* Compatibility nops */
2860 log_error("Unknown option code %c", c);
2865 if (argc > optind && !streq(argv[optind], "now"))
2866 log_warning("First argument '%s' isn't 'now'. Ignoring.", argv[optind]);
2868 /* We ignore the time argument */
2869 if (argc > optind + 1)
2870 arg_wall = argv + optind + 1;
2877 static int telinit_parse_argv(int argc, char *argv[]) {
2884 static const struct option options[] = {
2885 { "help", no_argument, NULL, ARG_HELP },
2886 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2887 { NULL, 0, NULL, 0 }
2890 static const struct {
2894 { '0', ACTION_POWEROFF },
2895 { '6', ACTION_REBOOT },
2896 { '1', ACTION_RESCUE },
2897 { '2', ACTION_RUNLEVEL2 },
2898 { '3', ACTION_RUNLEVEL3 },
2899 { '4', ACTION_RUNLEVEL4 },
2900 { '5', ACTION_RUNLEVEL5 },
2901 { 's', ACTION_RESCUE },
2902 { 'S', ACTION_RESCUE },
2903 { 'q', ACTION_RELOAD },
2904 { 'Q', ACTION_RELOAD },
2905 { 'u', ACTION_REEXEC },
2906 { 'U', ACTION_REEXEC }
2915 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
2930 log_error("Unknown option code %c", c);
2935 if (optind >= argc) {
2940 if (optind + 1 < argc) {
2941 log_error("Too many arguments.");
2945 if (strlen(argv[optind]) != 1) {
2946 log_error("Expected single character argument.");
2950 for (i = 0; i < ELEMENTSOF(table); i++)
2951 if (table[i].from == argv[optind][0])
2954 if (i >= ELEMENTSOF(table)) {
2955 log_error("Unknown command %s.", argv[optind]);
2959 arg_action = table[i].to;
2966 static int runlevel_parse_argv(int argc, char *argv[]) {
2972 static const struct option options[] = {
2973 { "help", no_argument, NULL, ARG_HELP },
2974 { NULL, 0, NULL, 0 }
2982 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
2993 log_error("Unknown option code %c", c);
2998 if (optind < argc) {
2999 log_error("Too many arguments.");
3006 static int parse_argv(int argc, char *argv[]) {
3010 if (program_invocation_short_name) {
3012 if (strstr(program_invocation_short_name, "halt")) {
3013 arg_action = ACTION_HALT;
3014 return halt_parse_argv(argc, argv);
3015 } else if (strstr(program_invocation_short_name, "poweroff")) {
3016 arg_action = ACTION_POWEROFF;
3017 return halt_parse_argv(argc, argv);
3018 } else if (strstr(program_invocation_short_name, "reboot")) {
3019 arg_action = ACTION_REBOOT;
3020 return halt_parse_argv(argc, argv);
3021 } else if (strstr(program_invocation_short_name, "shutdown")) {
3022 arg_action = ACTION_POWEROFF;
3023 return shutdown_parse_argv(argc, argv);
3024 } else if (strstr(program_invocation_short_name, "init")) {
3025 arg_action = ACTION_INVALID;
3026 return telinit_parse_argv(argc, argv);
3027 } else if (strstr(program_invocation_short_name, "runlevel")) {
3028 arg_action = ACTION_RUNLEVEL;
3029 return runlevel_parse_argv(argc, argv);
3033 arg_action = ACTION_SYSTEMCTL;
3034 return systemctl_parse_argv(argc, argv);
3037 static int action_to_runlevel(void) {
3039 static const char table[_ACTION_MAX] = {
3040 [ACTION_HALT] = '0',
3041 [ACTION_POWEROFF] = '0',
3042 [ACTION_REBOOT] = '6',
3043 [ACTION_RUNLEVEL2] = '2',
3044 [ACTION_RUNLEVEL3] = '3',
3045 [ACTION_RUNLEVEL4] = '4',
3046 [ACTION_RUNLEVEL5] = '5',
3047 [ACTION_RESCUE] = '1'
3050 assert(arg_action < _ACTION_MAX);
3052 return table[arg_action];
3055 static int talk_upstart(void) {
3056 DBusMessage *m = NULL, *reply = NULL;
3058 int previous, rl, r;
3060 env1_buf[] = "RUNLEVEL=X",
3061 env2_buf[] = "PREVLEVEL=X";
3062 char *env1 = env1_buf, *env2 = env2_buf;
3063 const char *emit = "runlevel";
3064 dbus_bool_t b_false = FALSE;
3065 DBusMessageIter iter, sub;
3066 DBusConnection *bus;
3068 dbus_error_init(&error);
3070 if (!(rl = action_to_runlevel()))
3073 if (utmp_get_runlevel(&previous, NULL) < 0)
3076 if (!(bus = dbus_connection_open_private("unix:abstract=/com/ubuntu/upstart", &error))) {
3077 if (dbus_error_has_name(&error, DBUS_ERROR_NO_SERVER)) {
3082 log_error("Failed to connect to Upstart bus: %s", error.message);
3087 if ((r = bus_check_peercred(bus)) < 0) {
3088 log_error("Failed to verify owner of bus.");
3092 if (!(m = dbus_message_new_method_call(
3093 "com.ubuntu.Upstart",
3094 "/com/ubuntu/Upstart",
3095 "com.ubuntu.Upstart0_6",
3098 log_error("Could not allocate message.");
3103 dbus_message_iter_init_append(m, &iter);
3105 env1_buf[sizeof(env1_buf)-2] = rl;
3106 env2_buf[sizeof(env2_buf)-2] = previous;
3108 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
3109 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
3110 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
3111 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
3112 !dbus_message_iter_close_container(&iter, &sub) ||
3113 !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
3114 log_error("Could not append arguments to message.");
3119 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3121 if (error_is_no_service(&error)) {
3126 log_error("Failed to issue method call: %s", error.message);
3135 dbus_message_unref(m);
3138 dbus_message_unref(reply);
3141 dbus_connection_close(bus);
3142 dbus_connection_unref(bus);
3145 dbus_error_free(&error);
3150 static int talk_initctl(void) {
3151 struct init_request request;
3155 if (!(rl = action_to_runlevel()))
3159 request.magic = INIT_MAGIC;
3160 request.sleeptime = 0;
3161 request.cmd = INIT_CMD_RUNLVL;
3162 request.runlevel = rl;
3164 if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
3166 if (errno == ENOENT)
3169 log_error("Failed to open "INIT_FIFO": %m");
3174 r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
3175 close_nointr_nofail(fd);
3178 log_error("Failed to write to "INIT_FIFO": %m");
3179 return errno ? -errno : -EIO;
3185 static int systemctl_main(DBusConnection *bus, int argc, char *argv[]) {
3187 static const struct {
3195 int (* const dispatch)(DBusConnection *bus, char **args, unsigned n);
3197 { "list-units", LESS, 1, list_units },
3198 { "list-jobs", EQUAL, 1, list_jobs },
3199 { "clear-jobs", EQUAL, 1, clear_jobs },
3200 { "load", MORE, 2, load_unit },
3201 { "cancel", MORE, 2, cancel_job },
3202 { "start", MORE, 2, start_unit },
3203 { "stop", MORE, 2, start_unit },
3204 { "reload", MORE, 2, start_unit },
3205 { "restart", MORE, 2, start_unit },
3206 { "try-restart", MORE, 2, start_unit },
3207 { "reload-or-restart", MORE, 2, start_unit },
3208 { "reload-or-try-restart", MORE, 2, start_unit },
3209 { "isolate", EQUAL, 2, start_unit },
3210 { "check", MORE, 2, check_unit },
3211 { "show", MORE, 1, show },
3212 { "status", MORE, 2, show },
3213 { "monitor", EQUAL, 1, monitor },
3214 { "dump", EQUAL, 1, dump },
3215 { "snapshot", LESS, 2, snapshot },
3216 { "delete", MORE, 2, delete_snapshot },
3217 { "daemon-reload", EQUAL, 1, clear_jobs },
3218 { "daemon-reexec", EQUAL, 1, clear_jobs },
3219 { "daemon-exit", EQUAL, 1, clear_jobs },
3220 { "show-environment", EQUAL, 1, show_enviroment },
3221 { "set-environment", MORE, 2, set_environment },
3222 { "unset-environment", MORE, 2, set_environment },
3223 { "halt", EQUAL, 1, start_special },
3224 { "poweroff", EQUAL, 1, start_special },
3225 { "reboot", EQUAL, 1, start_special },
3226 { "default", EQUAL, 1, start_special },
3227 { "rescue", EQUAL, 1, start_special },
3228 { "emergency", EQUAL, 1, start_special }
3238 left = argc - optind;
3241 /* Special rule: no arguments means "list-units" */
3244 if (streq(argv[optind], "help")) {
3249 for (i = 0; i < ELEMENTSOF(verbs); i++)
3250 if (streq(argv[optind], verbs[i].verb))
3253 if (i >= ELEMENTSOF(verbs)) {
3254 log_error("Unknown operation %s", argv[optind]);
3259 switch (verbs[i].argc_cmp) {
3262 if (left != verbs[i].argc) {
3263 log_error("Invalid number of arguments.");
3270 if (left < verbs[i].argc) {
3271 log_error("Too few arguments.");
3278 if (left > verbs[i].argc) {
3279 log_error("Too many arguments.");
3286 assert_not_reached("Unknown comparison operator.");
3289 return verbs[i].dispatch(bus, argv + optind, left);
3292 static int reload_with_fallback(DBusConnection *bus) {
3296 /* First, try systemd via D-Bus. */
3297 if ((r = clear_jobs(bus, NULL, 0)) > 0)
3301 /* Nothing else worked, so let's try signals */
3302 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
3304 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
3305 log_error("kill() failed: %m");
3312 static int start_with_fallback(DBusConnection *bus) {
3317 /* First, try systemd via D-Bus. */
3318 if ((r = start_unit(bus, NULL, 0)) > 0)
3321 /* Hmm, talking to systemd via D-Bus didn't work. Then
3322 * let's try to talk to Upstart via D-Bus. */
3323 if ((r = talk_upstart()) > 0)
3327 /* Nothing else worked, so let's try
3329 if ((r = talk_initctl()) != 0)
3332 log_error("Failed to talk to init daemon.");
3336 warn_wall(arg_action);
3340 static int halt_main(DBusConnection *bus) {
3343 if (geteuid() != 0) {
3344 log_error("Must to be root.");
3348 if (!arg_dry && !arg_immediate)
3349 return start_with_fallback(bus);
3352 if ((r = utmp_put_shutdown(0)) < 0)
3353 log_warning("Failed to write utmp record: %s", strerror(-r));
3361 /* Make sure C-A-D is handled by the kernel from this
3363 reboot(RB_ENABLE_CAD);
3365 switch (arg_action) {
3368 log_info("Halting");
3369 reboot(RB_HALT_SYSTEM);
3372 case ACTION_POWEROFF:
3373 log_info("Powering off");
3374 reboot(RB_POWER_OFF);
3378 log_info("Rebooting");
3379 reboot(RB_AUTOBOOT);
3383 assert_not_reached("Unknown halt action.");
3386 /* We should never reach this. */
3390 static int runlevel_main(void) {
3391 int r, runlevel, previous;
3393 if ((r = utmp_get_runlevel(&runlevel, &previous)) < 0) {
3399 previous <= 0 ? 'N' : previous,
3400 runlevel <= 0 ? 'N' : runlevel);
3405 int main(int argc, char*argv[]) {
3407 DBusConnection *bus = NULL;
3410 dbus_error_init(&error);
3412 log_parse_environment();
3414 if ((r = parse_argv(argc, argv)) < 0)
3421 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
3422 * let's shortcut this */
3423 if (arg_action == ACTION_RUNLEVEL) {
3424 retval = runlevel_main() < 0;
3428 bus_connect(arg_session ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &bus, &private_bus, &error);
3430 switch (arg_action) {
3432 case ACTION_SYSTEMCTL: {
3435 log_error("Failed to get D-Bus connection: %s", error.message);
3439 retval = systemctl_main(bus, argc, argv) < 0;
3444 case ACTION_POWEROFF:
3446 retval = halt_main(bus) < 0;
3449 case ACTION_RUNLEVEL2:
3450 case ACTION_RUNLEVEL3:
3451 case ACTION_RUNLEVEL4:
3452 case ACTION_RUNLEVEL5:
3454 case ACTION_EMERGENCY:
3455 case ACTION_DEFAULT:
3456 retval = start_with_fallback(bus) < 0;
3461 retval = reload_with_fallback(bus) < 0;
3464 case ACTION_INVALID:
3465 case ACTION_RUNLEVEL:
3467 assert_not_reached("Unknown action");
3473 dbus_connection_close(bus);
3474 dbus_connection_unref(bus);
3477 dbus_error_free(&error);