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" :
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) {
1070 /* This shows pretty information about a unit. See
1071 * print_property() for a low-level property printer */
1073 printf("%s", strna(i->id));
1075 if (i->description && !streq_ptr(i->id, i->description))
1076 printf(" - %s", i->description);
1080 if (i->fragment_path)
1081 printf("\t Loaded: %s (%s)\n", strna(i->load_state), i->fragment_path);
1082 else if (streq_ptr(i->load_state, "failed"))
1083 printf("\t Loaded: " ANSI_HIGHLIGHT_ON "%s" ANSI_HIGHLIGHT_OFF "\n", strna(i->load_state));
1085 printf("\t Loaded: %s\n", strna(i->load_state));
1087 if (streq_ptr(i->active_state, "maintenance")) {
1088 if (streq_ptr(i->active_state, i->sub_state))
1089 printf("\t Active: " ANSI_HIGHLIGHT_ON "%s" ANSI_HIGHLIGHT_OFF "\n",
1090 strna(i->active_state));
1092 printf("\t Active: " ANSI_HIGHLIGHT_ON "%s (%s)" ANSI_HIGHLIGHT_OFF "\n",
1093 strna(i->active_state),
1094 strna(i->sub_state));
1096 if (streq_ptr(i->active_state, i->sub_state))
1097 printf("\t Active: %s\n",
1098 strna(i->active_state));
1100 printf("\t Active: %s (%s)\n",
1101 strna(i->active_state),
1102 strna(i->sub_state));
1106 printf("\t Device: %s\n", i->sysfs_path);
1108 printf("\t Where: %s\n", i->where);
1110 printf("\t What: %s\n", i->what);
1113 printf("\tAccepted: %u; Connected: %u\n", i->n_accepted, i->n_connections);
1115 LIST_FOREACH(exec, p, i->exec) {
1118 /* Only show exited processes here */
1122 t = strv_join(p->argv, " ");
1123 printf("\t Exited: %u (%s, code=%s, ", p->pid, strna(t), sigchld_code_to_string(p->code));
1126 if (p->code == CLD_EXITED)
1127 printf("status=%i", p->status);
1129 printf("signal=%s", signal_to_string(p->status));
1132 if (i->main_pid == p->pid &&
1133 i->start_timestamp == p->start_timestamp &&
1134 i->exit_timestamp == p->start_timestamp)
1135 /* Let's not show this twice */
1138 if (p->pid == i->control_pid)
1142 if (i->main_pid > 0 || i->control_pid > 0) {
1145 if (i->main_pid > 0) {
1146 printf(" Main: %u", (unsigned) i->main_pid);
1150 get_process_name(i->main_pid, &t);
1156 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
1158 if (i->exit_code == CLD_EXITED)
1159 printf("status=%i", i->exit_status);
1161 printf("signal=%s", signal_to_string(i->exit_status));
1166 if (i->main_pid > 0 && i->control_pid > 0)
1169 if (i->control_pid > 0) {
1172 printf(" Control: %u", (unsigned) i->control_pid);
1174 get_process_name(i->control_pid, &t);
1185 printf("\t Status: \"%s\"\n", i->status_text);
1187 if (i->default_control_group) {
1190 printf("\t CGroup: %s\n", i->default_control_group);
1192 if ((c = columns()) > 18)
1197 show_cgroup_by_path(i->default_control_group, "\t\t ", c);
1201 static int status_property(const char *name, DBusMessageIter *iter, UnitStatusInfo *i) {
1203 switch (dbus_message_iter_get_arg_type(iter)) {
1205 case DBUS_TYPE_STRING: {
1208 dbus_message_iter_get_basic(iter, &s);
1211 if (streq(name, "Id"))
1213 else if (streq(name, "LoadState"))
1215 else if (streq(name, "ActiveState"))
1216 i->active_state = s;
1217 else if (streq(name, "SubState"))
1219 else if (streq(name, "Description"))
1221 else if (streq(name, "FragmentPath"))
1222 i->fragment_path = s;
1223 else if (streq(name, "DefaultControlGroup"))
1224 i->default_control_group = s;
1225 else if (streq(name, "StatusText"))
1227 else if (streq(name, "SysFSPath"))
1229 else if (streq(name, "Where"))
1231 else if (streq(name, "What"))
1238 case DBUS_TYPE_BOOLEAN: {
1241 dbus_message_iter_get_basic(iter, &b);
1243 if (streq(name, "Accept"))
1249 case DBUS_TYPE_UINT32: {
1252 dbus_message_iter_get_basic(iter, &u);
1254 if (streq(name, "MainPID")) {
1256 i->main_pid = (pid_t) u;
1259 } else if (streq(name, "ControlPID"))
1260 i->control_pid = (pid_t) u;
1261 else if (streq(name, "ExecMainPID")) {
1263 i->main_pid = (pid_t) u;
1264 } else if (streq(name, "NAccepted"))
1266 else if (streq(name, "NConnections"))
1267 i->n_connections = u;
1272 case DBUS_TYPE_INT32: {
1275 dbus_message_iter_get_basic(iter, &j);
1277 if (streq(name, "ExecMainCode"))
1278 i->exit_code = (int) j;
1279 else if (streq(name, "ExecMainStatus"))
1280 i->exit_status = (int) j;
1285 case DBUS_TYPE_UINT64: {
1288 dbus_message_iter_get_basic(iter, &u);
1290 if (streq(name, "ExecMainStartTimestamp"))
1291 i->start_timestamp = (usec_t) u;
1292 else if (streq(name, "ExecMainExitTimestamp"))
1293 i->exit_timestamp = (usec_t) u;
1298 case DBUS_TYPE_ARRAY: {
1300 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT &&
1301 startswith(name, "Exec")) {
1302 DBusMessageIter sub;
1304 dbus_message_iter_recurse(iter, &sub);
1305 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1306 ExecStatusInfo *info;
1309 if (!(info = new0(ExecStatusInfo, 1)))
1312 if ((r = exec_status_info_deserialize(&sub, info)) < 0) {
1317 LIST_PREPEND(ExecStatusInfo, exec, i->exec, info);
1319 dbus_message_iter_next(&sub);
1330 static int print_property(const char *name, DBusMessageIter *iter) {
1334 /* This is a low-level property printer, see
1335 * print_status_info() for the nicer output */
1337 if (arg_property && !streq(name, arg_property))
1340 switch (dbus_message_iter_get_arg_type(iter)) {
1342 case DBUS_TYPE_STRING: {
1344 dbus_message_iter_get_basic(iter, &s);
1346 if (arg_all || s[0])
1347 printf("%s=%s\n", name, s);
1352 case DBUS_TYPE_BOOLEAN: {
1354 dbus_message_iter_get_basic(iter, &b);
1355 printf("%s=%s\n", name, yes_no(b));
1360 case DBUS_TYPE_UINT64: {
1362 dbus_message_iter_get_basic(iter, &u);
1364 /* Yes, heuristics! But we can change this check
1365 * should it turn out to not be sufficient */
1367 if (strstr(name, "Timestamp")) {
1368 char timestamp[FORMAT_TIMESTAMP_MAX], *t;
1370 if ((t = format_timestamp(timestamp, sizeof(timestamp), u)) || arg_all)
1371 printf("%s=%s\n", name, strempty(t));
1372 } else if (strstr(name, "USec")) {
1373 char timespan[FORMAT_TIMESPAN_MAX];
1375 printf("%s=%s\n", name, format_timespan(timespan, sizeof(timespan), u));
1377 printf("%s=%llu\n", name, (unsigned long long) u);
1382 case DBUS_TYPE_UINT32: {
1384 dbus_message_iter_get_basic(iter, &u);
1386 if (strstr(name, "UMask") || strstr(name, "Mode"))
1387 printf("%s=%04o\n", name, u);
1389 printf("%s=%u\n", name, (unsigned) u);
1394 case DBUS_TYPE_INT32: {
1396 dbus_message_iter_get_basic(iter, &i);
1398 printf("%s=%i\n", name, (int) i);
1402 case DBUS_TYPE_STRUCT: {
1403 DBusMessageIter sub;
1404 dbus_message_iter_recurse(iter, &sub);
1406 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "Job")) {
1409 dbus_message_iter_get_basic(&sub, &u);
1412 printf("%s=%u\n", name, (unsigned) u);
1414 printf("%s=\n", name);
1417 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Unit")) {
1420 dbus_message_iter_get_basic(&sub, &s);
1422 if (arg_all || s[0])
1423 printf("%s=%s\n", name, s);
1431 case DBUS_TYPE_ARRAY:
1433 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
1434 DBusMessageIter sub;
1437 dbus_message_iter_recurse(iter, &sub);
1439 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1440 printf("%s=", name);
1442 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1445 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
1446 dbus_message_iter_get_basic(&sub, &s);
1447 printf("%s%s", space ? " " : "", s);
1450 dbus_message_iter_next(&sub);
1458 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_BYTE) {
1459 DBusMessageIter sub;
1461 dbus_message_iter_recurse(iter, &sub);
1463 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1464 printf("%s=", name);
1466 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1469 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_BYTE);
1470 dbus_message_iter_get_basic(&sub, &u);
1473 dbus_message_iter_next(&sub);
1481 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Paths")) {
1482 DBusMessageIter sub, sub2;
1484 dbus_message_iter_recurse(iter, &sub);
1485 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1486 const char *type, *path;
1488 dbus_message_iter_recurse(&sub, &sub2);
1490 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
1491 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0)
1492 printf("%s=%s\n", type, path);
1494 dbus_message_iter_next(&sub);
1499 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Timers")) {
1500 DBusMessageIter sub, sub2;
1502 dbus_message_iter_recurse(iter, &sub);
1503 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1505 uint64_t value, next_elapse;
1507 dbus_message_iter_recurse(&sub, &sub2);
1509 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &base, true) >= 0 &&
1510 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &value, true) >= 0 &&
1511 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &next_elapse, false) >= 0) {
1512 char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX];
1514 printf("%s={ value=%s ; next_elapse=%s }\n",
1516 format_timespan(timespan1, sizeof(timespan1), value),
1517 format_timespan(timespan2, sizeof(timespan2), next_elapse));
1520 dbus_message_iter_next(&sub);
1525 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && startswith(name, "Exec")) {
1526 DBusMessageIter sub;
1528 dbus_message_iter_recurse(iter, &sub);
1529 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1530 ExecStatusInfo info;
1533 if (exec_status_info_deserialize(&sub, &info) >= 0) {
1534 char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
1537 t = strv_join(info.argv, " ");
1539 printf("%s={ path=%s ; argv[]=%s ; ignore=%s ; start_time=[%s] ; stop_time=[%s] ; pid=%u ; code=%s ; status=%i%s%s }\n",
1543 yes_no(info.ignore),
1544 strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)),
1545 strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)),
1546 (unsigned) info. pid,
1547 sigchld_code_to_string(info.code),
1549 info.code == CLD_EXITED ? "" : "/",
1550 strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status)));
1556 strv_free(info.argv);
1558 dbus_message_iter_next(&sub);
1568 printf("%s=[unprintable]\n", name);
1573 static int show_one(DBusConnection *bus, const char *path, bool show_properties, bool *new_line) {
1574 DBusMessage *m = NULL, *reply = NULL;
1575 const char *interface = "";
1578 DBusMessageIter iter, sub, sub2, sub3;
1579 UnitStatusInfo info;
1587 dbus_error_init(&error);
1589 if (!(m = dbus_message_new_method_call(
1590 "org.freedesktop.systemd1",
1592 "org.freedesktop.DBus.Properties",
1594 log_error("Could not allocate message.");
1599 if (!dbus_message_append_args(m,
1600 DBUS_TYPE_STRING, &interface,
1601 DBUS_TYPE_INVALID)) {
1602 log_error("Could not append arguments to message.");
1607 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1608 log_error("Failed to issue method call: %s", error.message);
1613 if (!dbus_message_iter_init(reply, &iter) ||
1614 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
1615 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
1616 log_error("Failed to parse reply.");
1621 dbus_message_iter_recurse(&iter, &sub);
1628 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1631 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
1632 log_error("Failed to parse reply.");
1637 dbus_message_iter_recurse(&sub, &sub2);
1639 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
1640 log_error("Failed to parse reply.");
1645 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
1646 log_error("Failed to parse reply.");
1651 dbus_message_iter_recurse(&sub2, &sub3);
1653 if (show_properties)
1654 r = print_property(name, &sub3);
1656 r = status_property(name, &sub3, &info);
1659 log_error("Failed to parse reply.");
1664 dbus_message_iter_next(&sub);
1667 if (!show_properties)
1668 print_status_info(&info);
1670 while ((p = info.exec)) {
1671 LIST_REMOVE(ExecStatusInfo, exec, info.exec, p);
1672 exec_status_info_free(p);
1679 dbus_message_unref(m);
1682 dbus_message_unref(reply);
1684 dbus_error_free(&error);
1689 static int show(DBusConnection *bus, char **args, unsigned n) {
1690 DBusMessage *m = NULL, *reply = NULL;
1694 bool show_properties, new_line = false;
1699 dbus_error_init(&error);
1701 show_properties = !streq(args[0], "status");
1703 if (show_properties && n <= 1) {
1704 /* If not argument is specified inspect the manager
1707 r = show_one(bus, "/org/freedesktop/systemd1", show_properties, &new_line);
1711 for (i = 1; i < n; i++) {
1712 const char *path = NULL;
1715 if (!show_properties || safe_atou32(args[i], &id) < 0) {
1717 if (!(m = dbus_message_new_method_call(
1718 "org.freedesktop.systemd1",
1719 "/org/freedesktop/systemd1",
1720 "org.freedesktop.systemd1.Manager",
1722 log_error("Could not allocate message.");
1727 if (!dbus_message_append_args(m,
1728 DBUS_TYPE_STRING, &args[i],
1729 DBUS_TYPE_INVALID)) {
1730 log_error("Could not append arguments to message.");
1735 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1737 if (!dbus_error_has_name(&error, DBUS_ERROR_ACCESS_DENIED)) {
1738 log_error("Failed to issue method call: %s", error.message);
1743 dbus_error_free(&error);
1745 dbus_message_unref(m);
1746 if (!(m = dbus_message_new_method_call(
1747 "org.freedesktop.systemd1",
1748 "/org/freedesktop/systemd1",
1749 "org.freedesktop.systemd1.Manager",
1751 log_error("Could not allocate message.");
1756 if (!dbus_message_append_args(m,
1757 DBUS_TYPE_STRING, &args[i],
1758 DBUS_TYPE_INVALID)) {
1759 log_error("Could not append arguments to message.");
1764 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1765 log_error("Failed to issue method call: %s", error.message);
1773 if (!(m = dbus_message_new_method_call(
1774 "org.freedesktop.systemd1",
1775 "/org/freedesktop/systemd1",
1776 "org.freedesktop.systemd1.Manager",
1778 log_error("Could not allocate message.");
1783 if (!dbus_message_append_args(m,
1784 DBUS_TYPE_UINT32, &id,
1785 DBUS_TYPE_INVALID)) {
1786 log_error("Could not append arguments to message.");
1791 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1792 log_error("Failed to issue method call: %s", error.message);
1798 if (!dbus_message_get_args(reply, &error,
1799 DBUS_TYPE_OBJECT_PATH, &path,
1800 DBUS_TYPE_INVALID)) {
1801 log_error("Failed to parse reply: %s", error.message);
1806 if ((r = show_one(bus, path, show_properties, &new_line)) < 0)
1809 dbus_message_unref(m);
1810 dbus_message_unref(reply);
1818 dbus_message_unref(m);
1821 dbus_message_unref(reply);
1823 dbus_error_free(&error);
1828 static DBusHandlerResult monitor_filter(DBusConnection *connection, DBusMessage *message, void *data) {
1830 DBusMessage *m = NULL, *reply = NULL;
1835 dbus_error_init(&error);
1837 log_debug("Got D-Bus request: %s.%s() on %s",
1838 dbus_message_get_interface(message),
1839 dbus_message_get_member(message),
1840 dbus_message_get_path(message));
1842 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
1843 log_error("Warning! D-Bus connection terminated.");
1844 dbus_connection_close(connection);
1846 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitNew") ||
1847 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitRemoved")) {
1848 const char *id, *path;
1850 if (!dbus_message_get_args(message, &error,
1851 DBUS_TYPE_STRING, &id,
1852 DBUS_TYPE_OBJECT_PATH, &path,
1854 log_error("Failed to parse message: %s", error.message);
1855 else if (streq(dbus_message_get_member(message), "UnitNew"))
1856 printf("Unit %s added.\n", id);
1858 printf("Unit %s removed.\n", id);
1860 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobNew") ||
1861 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
1865 if (!dbus_message_get_args(message, &error,
1866 DBUS_TYPE_UINT32, &id,
1867 DBUS_TYPE_OBJECT_PATH, &path,
1869 log_error("Failed to parse message: %s", error.message);
1870 else if (streq(dbus_message_get_member(message), "JobNew"))
1871 printf("Job %u added.\n", id);
1873 printf("Job %u removed.\n", id);
1876 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Unit", "Changed") ||
1877 dbus_message_is_signal(message, "org.freedesktop.systemd1.Job", "Changed")) {
1879 const char *path, *interface, *property = "Id";
1880 DBusMessageIter iter, sub;
1882 path = dbus_message_get_path(message);
1883 interface = dbus_message_get_interface(message);
1885 if (!(m = dbus_message_new_method_call(
1886 "org.freedesktop.systemd1",
1888 "org.freedesktop.DBus.Properties",
1890 log_error("Could not allocate message.");
1894 if (!dbus_message_append_args(m,
1895 DBUS_TYPE_STRING, &interface,
1896 DBUS_TYPE_STRING, &property,
1897 DBUS_TYPE_INVALID)) {
1898 log_error("Could not append arguments to message.");
1902 if (!(reply = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) {
1903 log_error("Failed to issue method call: %s", error.message);
1907 if (!dbus_message_iter_init(reply, &iter) ||
1908 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1909 log_error("Failed to parse reply.");
1913 dbus_message_iter_recurse(&iter, &sub);
1915 if (streq(interface, "org.freedesktop.systemd1.Unit")) {
1918 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1919 log_error("Failed to parse reply.");
1923 dbus_message_iter_get_basic(&sub, &id);
1924 printf("Unit %s changed.\n", id);
1928 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT32) {
1929 log_error("Failed to parse reply.");
1933 dbus_message_iter_get_basic(&sub, &id);
1934 printf("Job %u changed.\n", id);
1940 dbus_message_unref(m);
1943 dbus_message_unref(reply);
1945 dbus_error_free(&error);
1946 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1950 dbus_message_unref(m);
1953 dbus_message_unref(reply);
1955 dbus_error_free(&error);
1956 return DBUS_HANDLER_RESULT_NEED_MEMORY;
1959 static int monitor(DBusConnection *bus, char **args, unsigned n) {
1960 DBusMessage *m = NULL, *reply = NULL;
1964 dbus_error_init(&error);
1967 dbus_bus_add_match(bus,
1969 "sender='org.freedesktop.systemd1',"
1970 "interface='org.freedesktop.systemd1.Manager',"
1971 "path='/org/freedesktop/systemd1'",
1974 if (dbus_error_is_set(&error)) {
1975 log_error("Failed to add match: %s", error.message);
1980 dbus_bus_add_match(bus,
1982 "sender='org.freedesktop.systemd1',"
1983 "interface='org.freedesktop.systemd1.Unit',"
1987 if (dbus_error_is_set(&error)) {
1988 log_error("Failed to add match: %s", error.message);
1993 dbus_bus_add_match(bus,
1995 "sender='org.freedesktop.systemd1',"
1996 "interface='org.freedesktop.systemd1.Job',"
2000 if (dbus_error_is_set(&error)) {
2001 log_error("Failed to add match: %s", error.message);
2007 if (!dbus_connection_add_filter(bus, monitor_filter, NULL, NULL)) {
2008 log_error("Failed to add filter.");
2013 if (!(m = dbus_message_new_method_call(
2014 "org.freedesktop.systemd1",
2015 "/org/freedesktop/systemd1",
2016 "org.freedesktop.systemd1.Manager",
2018 log_error("Could not allocate message.");
2023 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2024 log_error("Failed to issue method call: %s", error.message);
2029 while (dbus_connection_read_write_dispatch(bus, -1))
2036 /* This is slightly dirty, since we don't undo the filter or the matches. */
2039 dbus_message_unref(m);
2042 dbus_message_unref(reply);
2044 dbus_error_free(&error);
2049 static int dump(DBusConnection *bus, char **args, unsigned n) {
2050 DBusMessage *m = NULL, *reply = NULL;
2055 dbus_error_init(&error);
2057 if (!(m = dbus_message_new_method_call(
2058 "org.freedesktop.systemd1",
2059 "/org/freedesktop/systemd1",
2060 "org.freedesktop.systemd1.Manager",
2062 log_error("Could not allocate message.");
2066 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2067 log_error("Failed to issue method call: %s", error.message);
2072 if (!dbus_message_get_args(reply, &error,
2073 DBUS_TYPE_STRING, &text,
2074 DBUS_TYPE_INVALID)) {
2075 log_error("Failed to parse reply: %s", error.message);
2080 fputs(text, stdout);
2086 dbus_message_unref(m);
2089 dbus_message_unref(reply);
2091 dbus_error_free(&error);
2096 static int snapshot(DBusConnection *bus, char **args, unsigned n) {
2097 DBusMessage *m = NULL, *reply = NULL;
2100 const char *name = "", *path, *id;
2101 dbus_bool_t cleanup = FALSE;
2102 DBusMessageIter iter, sub;
2104 *interface = "org.freedesktop.systemd1.Unit",
2107 dbus_error_init(&error);
2109 if (!(m = dbus_message_new_method_call(
2110 "org.freedesktop.systemd1",
2111 "/org/freedesktop/systemd1",
2112 "org.freedesktop.systemd1.Manager",
2113 "CreateSnapshot"))) {
2114 log_error("Could not allocate message.");
2121 if (!dbus_message_append_args(m,
2122 DBUS_TYPE_STRING, &name,
2123 DBUS_TYPE_BOOLEAN, &cleanup,
2124 DBUS_TYPE_INVALID)) {
2125 log_error("Could not append arguments to message.");
2130 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2131 log_error("Failed to issue method call: %s", error.message);
2136 if (!dbus_message_get_args(reply, &error,
2137 DBUS_TYPE_OBJECT_PATH, &path,
2138 DBUS_TYPE_INVALID)) {
2139 log_error("Failed to parse reply: %s", error.message);
2144 dbus_message_unref(m);
2145 if (!(m = dbus_message_new_method_call(
2146 "org.freedesktop.systemd1",
2148 "org.freedesktop.DBus.Properties",
2150 log_error("Could not allocate message.");
2154 if (!dbus_message_append_args(m,
2155 DBUS_TYPE_STRING, &interface,
2156 DBUS_TYPE_STRING, &property,
2157 DBUS_TYPE_INVALID)) {
2158 log_error("Could not append arguments to message.");
2163 dbus_message_unref(reply);
2164 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2165 log_error("Failed to issue method call: %s", error.message);
2170 if (!dbus_message_iter_init(reply, &iter) ||
2171 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2172 log_error("Failed to parse reply.");
2177 dbus_message_iter_recurse(&iter, &sub);
2179 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2180 log_error("Failed to parse reply.");
2185 dbus_message_iter_get_basic(&sub, &id);
2193 dbus_message_unref(m);
2196 dbus_message_unref(reply);
2198 dbus_error_free(&error);
2203 static int delete_snapshot(DBusConnection *bus, char **args, unsigned n) {
2204 DBusMessage *m = NULL, *reply = NULL;
2212 dbus_error_init(&error);
2214 for (i = 1; i < n; i++) {
2215 const char *path = NULL;
2217 if (!(m = dbus_message_new_method_call(
2218 "org.freedesktop.systemd1",
2219 "/org/freedesktop/systemd1",
2220 "org.freedesktop.systemd1.Manager",
2222 log_error("Could not allocate message.");
2227 if (!dbus_message_append_args(m,
2228 DBUS_TYPE_STRING, &args[i],
2229 DBUS_TYPE_INVALID)) {
2230 log_error("Could not append arguments to message.");
2235 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2236 log_error("Failed to issue method call: %s", error.message);
2241 if (!dbus_message_get_args(reply, &error,
2242 DBUS_TYPE_OBJECT_PATH, &path,
2243 DBUS_TYPE_INVALID)) {
2244 log_error("Failed to parse reply: %s", error.message);
2249 dbus_message_unref(m);
2250 if (!(m = dbus_message_new_method_call(
2251 "org.freedesktop.systemd1",
2253 "org.freedesktop.systemd1.Snapshot",
2255 log_error("Could not allocate message.");
2260 dbus_message_unref(reply);
2261 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2262 log_error("Failed to issue method call: %s", error.message);
2267 dbus_message_unref(m);
2268 dbus_message_unref(reply);
2276 dbus_message_unref(m);
2279 dbus_message_unref(reply);
2281 dbus_error_free(&error);
2286 static int clear_jobs(DBusConnection *bus, char **args, unsigned n) {
2287 DBusMessage *m = NULL, *reply = NULL;
2292 dbus_error_init(&error);
2294 if (arg_action == ACTION_RELOAD)
2296 else if (arg_action == ACTION_REEXEC)
2297 method = "Reexecute";
2299 assert(arg_action == ACTION_SYSTEMCTL);
2302 streq(args[0], "clear-jobs") ? "ClearJobs" :
2303 streq(args[0], "daemon-reload") ? "Reload" :
2304 streq(args[0], "daemon-reexec") ? "Reexecute" :
2308 if (!(m = dbus_message_new_method_call(
2309 "org.freedesktop.systemd1",
2310 "/org/freedesktop/systemd1",
2311 "org.freedesktop.systemd1.Manager",
2313 log_error("Could not allocate message.");
2317 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2319 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
2320 /* There's always a fallback possible for
2321 * legacy actions. */
2326 log_error("Failed to issue method call: %s", error.message);
2335 dbus_message_unref(m);
2338 dbus_message_unref(reply);
2340 dbus_error_free(&error);
2345 static int show_enviroment(DBusConnection *bus, char **args, unsigned n) {
2346 DBusMessage *m = NULL, *reply = NULL;
2348 DBusMessageIter iter, sub, sub2;
2351 *interface = "org.freedesktop.systemd1.Manager",
2352 *property = "Environment";
2354 dbus_error_init(&error);
2356 if (!(m = dbus_message_new_method_call(
2357 "org.freedesktop.systemd1",
2358 "/org/freedesktop/systemd1",
2359 "org.freedesktop.DBus.Properties",
2361 log_error("Could not allocate message.");
2365 if (!dbus_message_append_args(m,
2366 DBUS_TYPE_STRING, &interface,
2367 DBUS_TYPE_STRING, &property,
2368 DBUS_TYPE_INVALID)) {
2369 log_error("Could not append arguments to message.");
2374 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2375 log_error("Failed to issue method call: %s", error.message);
2380 if (!dbus_message_iter_init(reply, &iter) ||
2381 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2382 log_error("Failed to parse reply.");
2387 dbus_message_iter_recurse(&iter, &sub);
2389 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
2390 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
2391 log_error("Failed to parse reply.");
2396 dbus_message_iter_recurse(&sub, &sub2);
2398 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
2401 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
2402 log_error("Failed to parse reply.");
2407 dbus_message_iter_get_basic(&sub2, &text);
2408 printf("%s\n", text);
2410 dbus_message_iter_next(&sub2);
2417 dbus_message_unref(m);
2420 dbus_message_unref(reply);
2422 dbus_error_free(&error);
2427 static int set_environment(DBusConnection *bus, char **args, unsigned n) {
2428 DBusMessage *m = NULL, *reply = NULL;
2432 DBusMessageIter iter, sub;
2435 dbus_error_init(&error);
2437 method = streq(args[0], "set-environment")
2439 : "UnsetEnvironment";
2441 if (!(m = dbus_message_new_method_call(
2442 "org.freedesktop.systemd1",
2443 "/org/freedesktop/systemd1",
2444 "org.freedesktop.systemd1.Manager",
2447 log_error("Could not allocate message.");
2451 dbus_message_iter_init_append(m, &iter);
2453 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
2454 log_error("Could not append arguments to message.");
2459 for (i = 1; i < n; i++)
2460 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &args[i])) {
2461 log_error("Could not append arguments to message.");
2466 if (!dbus_message_iter_close_container(&iter, &sub)) {
2467 log_error("Could not append arguments to message.");
2472 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2473 log_error("Failed to issue method call: %s", error.message);
2482 dbus_message_unref(m);
2485 dbus_message_unref(reply);
2487 dbus_error_free(&error);
2492 static int systemctl_help(void) {
2494 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
2495 "Send control commands to the systemd manager.\n\n"
2496 " -h --help Show this help\n"
2497 " -t --type=TYPE List only units of a particular type\n"
2498 " -p --property=NAME Show only properties by this name\n"
2499 " -a --all Show all units/properties, including dead/empty ones\n"
2500 " --fail When installing a new job, fail if conflicting jobs are pending\n"
2501 " --system Connect to system bus\n"
2502 " --session Connect to session bus\n"
2503 " -q --quiet Suppress output\n"
2504 " --no-block Do not wait until operation finished\n"
2505 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
2507 " list-units List units\n"
2508 " start [NAME...] Start one or more units\n"
2509 " stop [NAME...] Stop one or more units\n"
2510 " restart [NAME...] Restart one or more units\n"
2511 " reload [NAME...] Reload one or more units\n"
2512 " isolate [NAME] Start one unit and stop all others\n"
2513 " check [NAME...] Check whether any of the passed units are active\n"
2514 " status [NAME...] Show status of one or more units\n"
2515 " show [NAME...|JOB...] Show properties of one or more units/jobs/manager\n"
2516 " load [NAME...] Load one or more units\n"
2517 " list-jobs List jobs\n"
2518 " cancel [JOB...] Cancel one or more jobs\n"
2519 " clear-jobs Cancel all jobs\n"
2520 " monitor Monitor unit/job changes\n"
2521 " dump Dump server status\n"
2522 " snapshot [NAME] Create a snapshot\n"
2523 " delete [NAME...] Remove one or more snapshots\n"
2524 " daemon-reload Reload systemd manager configuration\n"
2525 " daemon-reexec Reexecute systemd manager\n"
2526 " daemon-exit Ask the systemd manager to quit\n"
2527 " show-environment Dump environment\n"
2528 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
2529 " unset-environment [NAME...] Unset one or more environment variables\n"
2530 " halt Shut down and halt the system\n"
2531 " poweroff Shut down and power-off the system\n"
2532 " reboot Shut down and reboot the system\n"
2533 " default Enter default mode\n"
2534 " rescue Enter rescue mode\n"
2535 " emergency Enter emergency mode\n",
2536 program_invocation_short_name);
2541 static int halt_help(void) {
2543 printf("%s [OPTIONS...]\n\n"
2544 "%s the system.\n\n"
2545 " --help Show this help\n"
2546 " --halt Halt the machine\n"
2547 " -p --poweroff Switch off the machine\n"
2548 " --reboot Reboot the machine\n"
2549 " -f --force Force immediate halt/power-off/reboot\n"
2550 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
2551 " -d --no-wtmp Don't write wtmp record\n"
2552 " -n --no-sync Don't sync before halt/power-off/reboot\n"
2553 " --no-wall Don't send wall message before halt/power-off/reboot\n",
2554 program_invocation_short_name,
2555 arg_action == ACTION_REBOOT ? "Reboot" :
2556 arg_action == ACTION_POWEROFF ? "Power off" :
2562 static int shutdown_help(void) {
2564 printf("%s [OPTIONS...] [now] [WALL...]\n\n"
2565 "Shut down the system.\n\n"
2566 " --help Show this help\n"
2567 " -H --halt Halt the machine\n"
2568 " -P --poweroff Power-off the machine\n"
2569 " -r --reboot Reboot the machine\n"
2570 " -h Equivalent to --poweroff, overriden by --halt\n"
2571 " -k Don't halt/power-off/reboot, just send warnings\n"
2572 " --no-wall Don't send wall message before halt/power-off/reboot\n",
2573 program_invocation_short_name);
2578 static int telinit_help(void) {
2580 printf("%s [OPTIONS...] {COMMAND}\n\n"
2581 "Send control commands to the init daemon.\n\n"
2582 " --help Show this help\n"
2583 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
2585 " 0 Power-off the machine\n"
2586 " 6 Reboot the machine\n"
2587 " 2, 3, 4, 5 Start runlevelX.target unit\n"
2588 " 1, s, S Enter rescue mode\n"
2589 " q, Q Reload init daemon configuration\n"
2590 " u, U Reexecute init daemon\n",
2591 program_invocation_short_name);
2596 static int runlevel_help(void) {
2598 printf("%s [OPTIONS...]\n\n"
2599 "Prints the previous and current runlevel of the init system.\n\n"
2600 " --help Show this help\n",
2601 program_invocation_short_name);
2606 static int systemctl_parse_argv(int argc, char *argv[]) {
2616 static const struct option options[] = {
2617 { "help", no_argument, NULL, 'h' },
2618 { "type", required_argument, NULL, 't' },
2619 { "property", required_argument, NULL, 'p' },
2620 { "all", no_argument, NULL, 'a' },
2621 { "fail", no_argument, NULL, ARG_FAIL },
2622 { "session", no_argument, NULL, ARG_SESSION },
2623 { "system", no_argument, NULL, ARG_SYSTEM },
2624 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
2625 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2626 { "quiet", no_argument, NULL, 'q' },
2627 { NULL, 0, NULL, 0 }
2635 while ((c = getopt_long(argc, argv, "ht:p:aq", options, NULL)) >= 0) {
2648 arg_property = optarg;
2650 /* If the user asked for a particular
2651 * property, show it to him, even if it is
2669 arg_session = false;
2673 arg_no_block = true;
2688 log_error("Unknown option code %c", c);
2696 static int halt_parse_argv(int argc, char *argv[]) {
2705 static const struct option options[] = {
2706 { "help", no_argument, NULL, ARG_HELP },
2707 { "halt", no_argument, NULL, ARG_HALT },
2708 { "poweroff", no_argument, NULL, 'p' },
2709 { "reboot", no_argument, NULL, ARG_REBOOT },
2710 { "force", no_argument, NULL, 'f' },
2711 { "wtmp-only", no_argument, NULL, 'w' },
2712 { "no-wtmp", no_argument, NULL, 'd' },
2713 { "no-sync", no_argument, NULL, 'n' },
2714 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2715 { NULL, 0, NULL, 0 }
2723 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
2724 if (runlevel == '0' || runlevel == '6')
2725 arg_immediate = true;
2727 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
2735 arg_action = ACTION_HALT;
2739 arg_action = ACTION_POWEROFF;
2743 arg_action = ACTION_REBOOT;
2747 arg_immediate = true;
2768 /* Compatibility nops */
2775 log_error("Unknown option code %c", c);
2780 if (optind < argc) {
2781 log_error("Too many arguments.");
2788 static int shutdown_parse_argv(int argc, char *argv[]) {
2795 static const struct option options[] = {
2796 { "help", no_argument, NULL, ARG_HELP },
2797 { "halt", no_argument, NULL, 'H' },
2798 { "poweroff", no_argument, NULL, 'P' },
2799 { "reboot", no_argument, NULL, 'r' },
2800 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2801 { NULL, 0, NULL, 0 }
2809 while ((c = getopt_long(argc, argv, "HPrhkt:a", options, NULL)) >= 0) {
2817 arg_action = ACTION_HALT;
2821 arg_action = ACTION_POWEROFF;
2825 arg_action = ACTION_REBOOT;
2829 if (arg_action != ACTION_HALT)
2830 arg_action = ACTION_POWEROFF;
2843 /* Compatibility nops */
2850 log_error("Unknown option code %c", c);
2855 if (argc > optind && !streq(argv[optind], "now"))
2856 log_warning("First argument '%s' isn't 'now'. Ignoring.", argv[optind]);
2858 /* We ignore the time argument */
2859 if (argc > optind + 1)
2860 arg_wall = argv + optind + 1;
2867 static int telinit_parse_argv(int argc, char *argv[]) {
2874 static const struct option options[] = {
2875 { "help", no_argument, NULL, ARG_HELP },
2876 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2877 { NULL, 0, NULL, 0 }
2880 static const struct {
2884 { '0', ACTION_POWEROFF },
2885 { '6', ACTION_REBOOT },
2886 { '1', ACTION_RESCUE },
2887 { '2', ACTION_RUNLEVEL2 },
2888 { '3', ACTION_RUNLEVEL3 },
2889 { '4', ACTION_RUNLEVEL4 },
2890 { '5', ACTION_RUNLEVEL5 },
2891 { 's', ACTION_RESCUE },
2892 { 'S', ACTION_RESCUE },
2893 { 'q', ACTION_RELOAD },
2894 { 'Q', ACTION_RELOAD },
2895 { 'u', ACTION_REEXEC },
2896 { 'U', ACTION_REEXEC }
2905 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
2920 log_error("Unknown option code %c", c);
2925 if (optind >= argc) {
2930 if (optind + 1 < argc) {
2931 log_error("Too many arguments.");
2935 if (strlen(argv[optind]) != 1) {
2936 log_error("Expected single character argument.");
2940 for (i = 0; i < ELEMENTSOF(table); i++)
2941 if (table[i].from == argv[optind][0])
2944 if (i >= ELEMENTSOF(table)) {
2945 log_error("Unknown command %s.", argv[optind]);
2949 arg_action = table[i].to;
2956 static int runlevel_parse_argv(int argc, char *argv[]) {
2962 static const struct option options[] = {
2963 { "help", no_argument, NULL, ARG_HELP },
2964 { NULL, 0, NULL, 0 }
2972 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
2983 log_error("Unknown option code %c", c);
2988 if (optind < argc) {
2989 log_error("Too many arguments.");
2996 static int parse_argv(int argc, char *argv[]) {
3000 if (program_invocation_short_name) {
3002 if (strstr(program_invocation_short_name, "halt")) {
3003 arg_action = ACTION_HALT;
3004 return halt_parse_argv(argc, argv);
3005 } else if (strstr(program_invocation_short_name, "poweroff")) {
3006 arg_action = ACTION_POWEROFF;
3007 return halt_parse_argv(argc, argv);
3008 } else if (strstr(program_invocation_short_name, "reboot")) {
3009 arg_action = ACTION_REBOOT;
3010 return halt_parse_argv(argc, argv);
3011 } else if (strstr(program_invocation_short_name, "shutdown")) {
3012 arg_action = ACTION_POWEROFF;
3013 return shutdown_parse_argv(argc, argv);
3014 } else if (strstr(program_invocation_short_name, "init")) {
3015 arg_action = ACTION_INVALID;
3016 return telinit_parse_argv(argc, argv);
3017 } else if (strstr(program_invocation_short_name, "runlevel")) {
3018 arg_action = ACTION_RUNLEVEL;
3019 return runlevel_parse_argv(argc, argv);
3023 arg_action = ACTION_SYSTEMCTL;
3024 return systemctl_parse_argv(argc, argv);
3027 static int action_to_runlevel(void) {
3029 static const char table[_ACTION_MAX] = {
3030 [ACTION_HALT] = '0',
3031 [ACTION_POWEROFF] = '0',
3032 [ACTION_REBOOT] = '6',
3033 [ACTION_RUNLEVEL2] = '2',
3034 [ACTION_RUNLEVEL3] = '3',
3035 [ACTION_RUNLEVEL4] = '4',
3036 [ACTION_RUNLEVEL5] = '5',
3037 [ACTION_RESCUE] = '1'
3040 assert(arg_action < _ACTION_MAX);
3042 return table[arg_action];
3045 static int talk_upstart(void) {
3046 DBusMessage *m = NULL, *reply = NULL;
3048 int previous, rl, r;
3050 env1_buf[] = "RUNLEVEL=X",
3051 env2_buf[] = "PREVLEVEL=X";
3052 char *env1 = env1_buf, *env2 = env2_buf;
3053 const char *emit = "runlevel";
3054 dbus_bool_t b_false = FALSE;
3055 DBusMessageIter iter, sub;
3056 DBusConnection *bus;
3058 dbus_error_init(&error);
3060 if (!(rl = action_to_runlevel()))
3063 if (utmp_get_runlevel(&previous, NULL) < 0)
3066 if (!(bus = dbus_connection_open_private("unix:abstract=/com/ubuntu/upstart", &error))) {
3067 if (dbus_error_has_name(&error, DBUS_ERROR_NO_SERVER)) {
3072 log_error("Failed to connect to Upstart bus: %s", error.message);
3077 if ((r = bus_check_peercred(bus)) < 0) {
3078 log_error("Failed to verify owner of bus.");
3082 if (!(m = dbus_message_new_method_call(
3083 "com.ubuntu.Upstart",
3084 "/com/ubuntu/Upstart",
3085 "com.ubuntu.Upstart0_6",
3088 log_error("Could not allocate message.");
3093 dbus_message_iter_init_append(m, &iter);
3095 env1_buf[sizeof(env1_buf)-2] = rl;
3096 env2_buf[sizeof(env2_buf)-2] = previous;
3098 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
3099 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
3100 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
3101 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
3102 !dbus_message_iter_close_container(&iter, &sub) ||
3103 !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
3104 log_error("Could not append arguments to message.");
3109 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3111 if (error_is_no_service(&error)) {
3116 log_error("Failed to issue method call: %s", error.message);
3125 dbus_message_unref(m);
3128 dbus_message_unref(reply);
3131 dbus_connection_close(bus);
3132 dbus_connection_unref(bus);
3135 dbus_error_free(&error);
3140 static int talk_initctl(void) {
3141 struct init_request request;
3145 if (!(rl = action_to_runlevel()))
3149 request.magic = INIT_MAGIC;
3150 request.sleeptime = 0;
3151 request.cmd = INIT_CMD_RUNLVL;
3152 request.runlevel = rl;
3154 if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
3156 if (errno == ENOENT)
3159 log_error("Failed to open "INIT_FIFO": %m");
3164 r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
3165 close_nointr_nofail(fd);
3168 log_error("Failed to write to "INIT_FIFO": %m");
3169 return errno ? -errno : -EIO;
3175 static int systemctl_main(DBusConnection *bus, int argc, char *argv[]) {
3177 static const struct {
3185 int (* const dispatch)(DBusConnection *bus, char **args, unsigned n);
3187 { "list-units", LESS, 1, list_units },
3188 { "list-jobs", EQUAL, 1, list_jobs },
3189 { "clear-jobs", EQUAL, 1, clear_jobs },
3190 { "load", MORE, 2, load_unit },
3191 { "cancel", MORE, 2, cancel_job },
3192 { "start", MORE, 2, start_unit },
3193 { "stop", MORE, 2, start_unit },
3194 { "reload", MORE, 2, start_unit },
3195 { "restart", MORE, 2, start_unit },
3196 { "isolate", EQUAL, 2, start_unit },
3197 { "check", MORE, 2, check_unit },
3198 { "show", MORE, 1, show },
3199 { "status", MORE, 2, show },
3200 { "monitor", EQUAL, 1, monitor },
3201 { "dump", EQUAL, 1, dump },
3202 { "snapshot", LESS, 2, snapshot },
3203 { "delete", MORE, 2, delete_snapshot },
3204 { "daemon-reload", EQUAL, 1, clear_jobs },
3205 { "daemon-reexec", EQUAL, 1, clear_jobs },
3206 { "daemon-exit", EQUAL, 1, clear_jobs },
3207 { "show-environment", EQUAL, 1, show_enviroment },
3208 { "set-environment", MORE, 2, set_environment },
3209 { "unset-environment", MORE, 2, set_environment },
3210 { "halt", EQUAL, 1, start_special },
3211 { "poweroff", EQUAL, 1, start_special },
3212 { "reboot", EQUAL, 1, start_special },
3213 { "default", EQUAL, 1, start_special },
3214 { "rescue", EQUAL, 1, start_special },
3215 { "emergency", EQUAL, 1, start_special }
3225 left = argc - optind;
3228 /* Special rule: no arguments means "list-units" */
3231 if (streq(argv[optind], "help")) {
3236 for (i = 0; i < ELEMENTSOF(verbs); i++)
3237 if (streq(argv[optind], verbs[i].verb))
3240 if (i >= ELEMENTSOF(verbs)) {
3241 log_error("Unknown operation %s", argv[optind]);
3246 switch (verbs[i].argc_cmp) {
3249 if (left != verbs[i].argc) {
3250 log_error("Invalid number of arguments.");
3257 if (left < verbs[i].argc) {
3258 log_error("Too few arguments.");
3265 if (left > verbs[i].argc) {
3266 log_error("Too many arguments.");
3273 assert_not_reached("Unknown comparison operator.");
3276 return verbs[i].dispatch(bus, argv + optind, left);
3279 static int reload_with_fallback(DBusConnection *bus) {
3283 /* First, try systemd via D-Bus. */
3284 if ((r = clear_jobs(bus, NULL, 0)) > 0)
3288 /* Nothing else worked, so let's try signals */
3289 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
3291 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
3292 log_error("kill() failed: %m");
3299 static int start_with_fallback(DBusConnection *bus) {
3304 /* First, try systemd via D-Bus. */
3305 if ((r = start_unit(bus, NULL, 0)) > 0)
3308 /* Hmm, talking to systemd via D-Bus didn't work. Then
3309 * let's try to talk to Upstart via D-Bus. */
3310 if ((r = talk_upstart()) > 0)
3314 /* Nothing else worked, so let's try
3316 if ((r = talk_initctl()) != 0)
3319 log_error("Failed to talk to init daemon.");
3323 warn_wall(arg_action);
3327 static int halt_main(DBusConnection *bus) {
3330 if (geteuid() != 0) {
3331 log_error("Must to be root.");
3335 if (!arg_dry && !arg_immediate)
3336 return start_with_fallback(bus);
3339 if ((r = utmp_put_shutdown(0)) < 0)
3340 log_warning("Failed to write utmp record: %s", strerror(-r));
3348 /* Make sure C-A-D is handled by the kernel from this
3350 reboot(RB_ENABLE_CAD);
3352 switch (arg_action) {
3355 log_info("Halting");
3356 reboot(RB_HALT_SYSTEM);
3359 case ACTION_POWEROFF:
3360 log_info("Powering off");
3361 reboot(RB_POWER_OFF);
3365 log_info("Rebooting");
3366 reboot(RB_AUTOBOOT);
3370 assert_not_reached("Unknown halt action.");
3373 /* We should never reach this. */
3377 static int runlevel_main(void) {
3378 int r, runlevel, previous;
3380 if ((r = utmp_get_runlevel(&runlevel, &previous)) < 0) {
3386 previous <= 0 ? 'N' : previous,
3387 runlevel <= 0 ? 'N' : runlevel);
3392 int main(int argc, char*argv[]) {
3394 DBusConnection *bus = NULL;
3397 dbus_error_init(&error);
3399 log_parse_environment();
3401 if ((r = parse_argv(argc, argv)) < 0)
3408 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
3409 * let's shortcut this */
3410 if (arg_action == ACTION_RUNLEVEL) {
3411 retval = runlevel_main() < 0;
3415 bus_connect(arg_session ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &bus, &private_bus, &error);
3417 switch (arg_action) {
3419 case ACTION_SYSTEMCTL: {
3422 log_error("Failed to get D-Bus connection: %s", error.message);
3426 retval = systemctl_main(bus, argc, argv) < 0;
3431 case ACTION_POWEROFF:
3433 retval = halt_main(bus) < 0;
3436 case ACTION_RUNLEVEL2:
3437 case ACTION_RUNLEVEL3:
3438 case ACTION_RUNLEVEL4:
3439 case ACTION_RUNLEVEL5:
3441 case ACTION_EMERGENCY:
3442 case ACTION_DEFAULT:
3443 retval = start_with_fallback(bus) < 0;
3448 retval = reload_with_fallback(bus) < 0;
3451 case ACTION_INVALID:
3452 case ACTION_RUNLEVEL:
3454 assert_not_reached("Unknown action");
3460 dbus_connection_close(bus);
3461 dbus_connection_unref(bus);
3464 dbus_error_free(&error);