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_replace = false;
53 static bool arg_session = false;
54 static bool arg_no_block = false;
55 static bool arg_immediate = false;
56 static bool arg_no_wtmp = false;
57 static bool arg_no_sync = false;
58 static bool arg_no_wall = false;
59 static bool arg_dry = false;
60 static bool arg_quiet = false;
61 static char **arg_wall = NULL;
79 } arg_action = ACTION_SYSTEMCTL;
81 static bool private_bus = false;
83 static bool error_is_no_service(DBusError *error) {
87 if (!dbus_error_is_set(error))
90 if (dbus_error_has_name(error, DBUS_ERROR_NAME_HAS_NO_OWNER))
93 if (dbus_error_has_name(error, DBUS_ERROR_SERVICE_UNKNOWN))
96 return startswith(error->name, "org.freedesktop.DBus.Error.Spawn.");
99 static int bus_iter_get_basic_and_next(DBusMessageIter *iter, int type, void *data, bool next) {
104 if (dbus_message_iter_get_arg_type(iter) != type)
107 dbus_message_iter_get_basic(iter, data);
109 if (!dbus_message_iter_next(iter) != !next)
115 static void warn_wall(enum action action) {
116 static const char *table[_ACTION_MAX] = {
117 [ACTION_HALT] = "The system is going down for system halt NOW!",
118 [ACTION_REBOOT] = "The system is going down for reboot NOW!",
119 [ACTION_POWEROFF] = "The system is going down for power-off NOW!",
120 [ACTION_RESCUE] = "The system is going down to rescue mode NOW!",
121 [ACTION_EMERGENCY] = "The system is going down to emergency mode NOW!"
130 if (!(p = strv_join(arg_wall, " "))) {
131 log_error("Failed to join strings.");
147 utmp_wall(table[action]);
150 static int list_units(DBusConnection *bus, char **args, unsigned n) {
151 DBusMessage *m = NULL, *reply = NULL;
154 DBusMessageIter iter, sub, sub2;
157 dbus_error_init(&error);
161 if (!(m = dbus_message_new_method_call(
162 "org.freedesktop.systemd1",
163 "/org/freedesktop/systemd1",
164 "org.freedesktop.systemd1.Manager",
166 log_error("Could not allocate message.");
170 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
171 log_error("Failed to issue method call: %s", error.message);
176 if (!dbus_message_iter_init(reply, &iter) ||
177 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
178 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
179 log_error("Failed to parse reply.");
184 dbus_message_iter_recurse(&iter, &sub);
186 printf("%-45s %-6s %-12s %-12s %-15s %s\n", "UNIT", "LOAD", "ACTIVE", "SUB", "JOB", "DESCRIPTION");
188 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
189 const char *id, *description, *load_state, *active_state, *sub_state, *unit_state, *job_type, *job_path, *dot;
192 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
193 log_error("Failed to parse reply.");
198 dbus_message_iter_recurse(&sub, &sub2);
200 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
201 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &description, true) < 0 ||
202 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &load_state, true) < 0 ||
203 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &active_state, true) < 0 ||
204 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &sub_state, true) < 0 ||
205 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_state, true) < 0 ||
206 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &job_id, true) < 0 ||
207 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &job_type, true) < 0 ||
208 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, false) < 0) {
209 log_error("Failed to parse reply.");
214 if ((!arg_type || ((dot = strrchr(id, '.')) &&
215 streq(dot+1, arg_type))) &&
216 (arg_all || !streq(active_state, "inactive"))) {
220 if (streq(active_state, "maintenance"))
221 fputs(ANSI_HIGHLIGHT_ON, stdout);
223 printf("%-45s %-6s %-12s %-12s%n", id, load_state, active_state, sub_state, &a);
226 printf(" %-15s%n", job_type, &b);
230 if (a + b + 2 < columns()) {
234 printf(" %.*s", columns() - a - b - 2, description);
237 if (streq(active_state, "maintenance"))
238 fputs(ANSI_HIGHLIGHT_OFF, stdout);
244 dbus_message_iter_next(&sub);
248 printf("\n%u units listed.\n", k);
250 printf("\n%u live units listed. Pass --all to see dead units, too.\n", k);
256 dbus_message_unref(m);
259 dbus_message_unref(reply);
261 dbus_error_free(&error);
266 static int list_jobs(DBusConnection *bus, char **args, unsigned n) {
267 DBusMessage *m = NULL, *reply = NULL;
270 DBusMessageIter iter, sub, sub2;
273 dbus_error_init(&error);
277 if (!(m = dbus_message_new_method_call(
278 "org.freedesktop.systemd1",
279 "/org/freedesktop/systemd1",
280 "org.freedesktop.systemd1.Manager",
282 log_error("Could not allocate message.");
286 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
287 log_error("Failed to issue method call: %s", error.message);
292 if (!dbus_message_iter_init(reply, &iter) ||
293 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
294 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
295 log_error("Failed to parse reply.");
300 dbus_message_iter_recurse(&iter, &sub);
302 printf("%4s %-45s %-17s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
304 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
305 const char *name, *type, *state, *job_path, *unit_path;
308 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
309 log_error("Failed to parse reply.");
314 dbus_message_iter_recurse(&sub, &sub2);
316 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &id, true) < 0 ||
317 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0 ||
318 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
319 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, true) < 0 ||
320 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, true) < 0 ||
321 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, false) < 0) {
322 log_error("Failed to parse reply.");
327 printf("%4u %-45s %-17s %-7s\n", id, name, type, state);
330 dbus_message_iter_next(&sub);
333 printf("\n%u jobs listed.\n", k);
338 dbus_message_unref(m);
341 dbus_message_unref(reply);
343 dbus_error_free(&error);
348 static int load_unit(DBusConnection *bus, char **args, unsigned n) {
349 DBusMessage *m = NULL, *reply = NULL;
354 dbus_error_init(&error);
359 for (i = 1; i < n; i++) {
361 if (!(m = dbus_message_new_method_call(
362 "org.freedesktop.systemd1",
363 "/org/freedesktop/systemd1",
364 "org.freedesktop.systemd1.Manager",
366 log_error("Could not allocate message.");
371 if (!dbus_message_append_args(m,
372 DBUS_TYPE_STRING, &args[i],
373 DBUS_TYPE_INVALID)) {
374 log_error("Could not append arguments to message.");
379 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
380 log_error("Failed to issue method call: %s", error.message);
385 dbus_message_unref(m);
386 dbus_message_unref(reply);
395 dbus_message_unref(m);
398 dbus_message_unref(reply);
400 dbus_error_free(&error);
405 static int cancel_job(DBusConnection *bus, char **args, unsigned n) {
406 DBusMessage *m = NULL, *reply = NULL;
411 dbus_error_init(&error);
416 for (i = 1; i < n; i++) {
420 if (!(m = dbus_message_new_method_call(
421 "org.freedesktop.systemd1",
422 "/org/freedesktop/systemd1",
423 "org.freedesktop.systemd1.Manager",
425 log_error("Could not allocate message.");
430 if ((r = safe_atou(args[i], &id)) < 0) {
431 log_error("Failed to parse job id: %s", strerror(-r));
435 assert_cc(sizeof(uint32_t) == sizeof(id));
436 if (!dbus_message_append_args(m,
437 DBUS_TYPE_UINT32, &id,
438 DBUS_TYPE_INVALID)) {
439 log_error("Could not append arguments to message.");
444 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
445 log_error("Failed to issue method call: %s", error.message);
450 if (!dbus_message_get_args(reply, &error,
451 DBUS_TYPE_OBJECT_PATH, &path,
452 DBUS_TYPE_INVALID)) {
453 log_error("Failed to parse reply: %s", error.message);
458 dbus_message_unref(m);
459 if (!(m = dbus_message_new_method_call(
460 "org.freedesktop.systemd1",
462 "org.freedesktop.systemd1.Job",
464 log_error("Could not allocate message.");
469 dbus_message_unref(reply);
470 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
471 log_error("Failed to issue method call: %s", error.message);
476 dbus_message_unref(m);
477 dbus_message_unref(reply);
485 dbus_message_unref(m);
488 dbus_message_unref(reply);
490 dbus_error_free(&error);
495 typedef struct WaitData {
500 static DBusHandlerResult wait_filter(DBusConnection *connection, DBusMessage *message, void *data) {
508 dbus_error_init(&error);
510 log_debug("Got D-Bus request: %s.%s() on %s",
511 dbus_message_get_interface(message),
512 dbus_message_get_member(message),
513 dbus_message_get_path(message));
515 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
516 log_error("Warning! D-Bus connection terminated.");
517 dbus_connection_close(connection);
519 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
522 dbus_bool_t success = true;
524 if (!dbus_message_get_args(message, &error,
525 DBUS_TYPE_UINT32, &id,
526 DBUS_TYPE_OBJECT_PATH, &path,
527 DBUS_TYPE_BOOLEAN, &success,
529 log_error("Failed to parse message: %s", error.message);
533 if ((p = set_remove(d->set, (char*) path)))
541 dbus_error_free(&error);
542 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
545 static int enable_wait_for_jobs(DBusConnection *bus) {
553 dbus_error_init(&error);
554 dbus_bus_add_match(bus,
556 "sender='org.freedesktop.systemd1',"
557 "interface='org.freedesktop.systemd1.Manager',"
558 "member='JobRemoved',"
559 "path='/org/freedesktop/systemd1'",
562 if (dbus_error_is_set(&error)) {
563 log_error("Failed to add match: %s", error.message);
564 dbus_error_free(&error);
568 /* This is slightly dirty, since we don't undo the match registrations. */
572 static int wait_for_jobs(DBusConnection *bus, Set *s) {
583 if (!dbus_connection_add_filter(bus, wait_filter, &d, NULL)) {
584 log_error("Failed to add filter.");
589 while (!set_isempty(s) &&
590 dbus_connection_read_write_dispatch(bus, -1))
593 if (!arg_quiet && d.failed)
594 log_error("Job failed, see logs for details.");
596 r = d.failed ? -EIO : 0;
599 /* This is slightly dirty, since we don't undo the filter registration. */
604 static int start_unit_one(
611 DBusMessage *m = NULL, *reply = NULL;
619 assert(arg_no_block || s);
621 dbus_error_init(&error);
623 if (!(m = dbus_message_new_method_call(
624 "org.freedesktop.systemd1",
625 "/org/freedesktop/systemd1",
626 "org.freedesktop.systemd1.Manager",
628 log_error("Could not allocate message.");
633 if (!dbus_message_append_args(m,
634 DBUS_TYPE_STRING, &name,
635 DBUS_TYPE_STRING, &mode,
636 DBUS_TYPE_INVALID)) {
637 log_error("Could not append arguments to message.");
642 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
644 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
645 /* There's always a fallback possible for
651 log_error("Failed to issue method call: %s", error.message);
660 if (!dbus_message_get_args(reply, &error,
661 DBUS_TYPE_OBJECT_PATH, &path,
662 DBUS_TYPE_INVALID)) {
663 log_error("Failed to parse reply: %s", error.message);
668 if (!(p = strdup(path))) {
669 log_error("Failed to duplicate path.");
674 if ((r = set_put(s, p)) < 0) {
676 log_error("Failed to add path to set.");
685 dbus_message_unref(m);
688 dbus_message_unref(reply);
690 dbus_error_free(&error);
695 static enum action verb_to_action(const char *verb) {
696 if (streq(verb, "halt"))
698 else if (streq(verb, "poweroff"))
699 return ACTION_POWEROFF;
700 else if (streq(verb, "reboot"))
701 return ACTION_REBOOT;
702 else if (streq(verb, "rescue"))
703 return ACTION_RESCUE;
704 else if (streq(verb, "emergency"))
705 return ACTION_EMERGENCY;
706 else if (streq(verb, "default"))
707 return ACTION_DEFAULT;
709 return ACTION_INVALID;
712 static int start_unit(DBusConnection *bus, char **args, unsigned n) {
714 static const char * const table[_ACTION_MAX] = {
715 [ACTION_HALT] = SPECIAL_HALT_TARGET,
716 [ACTION_POWEROFF] = SPECIAL_POWEROFF_TARGET,
717 [ACTION_REBOOT] = SPECIAL_REBOOT_TARGET,
718 [ACTION_RUNLEVEL2] = SPECIAL_RUNLEVEL2_TARGET,
719 [ACTION_RUNLEVEL3] = SPECIAL_RUNLEVEL3_TARGET,
720 [ACTION_RUNLEVEL4] = SPECIAL_RUNLEVEL4_TARGET,
721 [ACTION_RUNLEVEL5] = SPECIAL_RUNLEVEL5_TARGET,
722 [ACTION_RESCUE] = SPECIAL_RESCUE_TARGET,
723 [ACTION_EMERGENCY] = SPECIAL_EMERGENCY_SERVICE,
724 [ACTION_DEFAULT] = SPECIAL_DEFAULT_TARGET
729 const char *method, *mode, *one_name;
734 if (arg_action == ACTION_SYSTEMCTL) {
736 streq(args[0], "stop") ? "StopUnit" :
737 streq(args[0], "reload") ? "ReloadUnit" :
738 streq(args[0], "restart") ? "RestartUnit" :
742 (streq(args[0], "isolate") ||
743 streq(args[0], "rescue") ||
744 streq(args[0], "emergency")) ? "isolate" :
745 arg_replace ? "replace" :
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) {
800 warn_wall(verb_to_action(args[0]));
802 return start_unit(bus, args, n);
805 static int check_unit(DBusConnection *bus, char **args, unsigned n) {
806 DBusMessage *m = NULL, *reply = NULL;
808 *interface = "org.freedesktop.systemd1.Unit",
809 *property = "ActiveState";
810 int r = -EADDRNOTAVAIL;
817 dbus_error_init(&error);
819 for (i = 1; i < n; i++) {
820 const char *path = NULL;
822 DBusMessageIter iter, sub;
824 if (!(m = dbus_message_new_method_call(
825 "org.freedesktop.systemd1",
826 "/org/freedesktop/systemd1",
827 "org.freedesktop.systemd1.Manager",
829 log_error("Could not allocate message.");
834 if (!dbus_message_append_args(m,
835 DBUS_TYPE_STRING, &args[i],
836 DBUS_TYPE_INVALID)) {
837 log_error("Could not append arguments to message.");
842 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
844 /* Hmm, cannot figure out anything about this unit... */
848 dbus_error_free(&error);
852 if (!dbus_message_get_args(reply, &error,
853 DBUS_TYPE_OBJECT_PATH, &path,
854 DBUS_TYPE_INVALID)) {
855 log_error("Failed to parse reply: %s", error.message);
860 dbus_message_unref(m);
861 if (!(m = dbus_message_new_method_call(
862 "org.freedesktop.systemd1",
864 "org.freedesktop.DBus.Properties",
866 log_error("Could not allocate message.");
871 if (!dbus_message_append_args(m,
872 DBUS_TYPE_STRING, &interface,
873 DBUS_TYPE_STRING, &property,
874 DBUS_TYPE_INVALID)) {
875 log_error("Could not append arguments to message.");
880 dbus_message_unref(reply);
881 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
882 log_error("Failed to issue method call: %s", error.message);
887 if (!dbus_message_iter_init(reply, &iter) ||
888 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
889 log_error("Failed to parse reply.");
894 dbus_message_iter_recurse(&iter, &sub);
896 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
897 log_error("Failed to parse reply.");
902 dbus_message_iter_get_basic(&sub, &state);
907 if (streq(state, "active") || startswith(state, "reloading"))
910 dbus_message_unref(m);
911 dbus_message_unref(reply);
917 dbus_message_unref(m);
920 dbus_message_unref(reply);
922 dbus_error_free(&error);
927 typedef struct ExecStatusInfo {
931 usec_t start_timestamp;
932 usec_t exit_timestamp;
937 LIST_FIELDS(struct ExecStatusInfo, exec);
940 static void exec_status_info_free(ExecStatusInfo *i) {
948 static int exec_status_info_deserialize(DBusMessageIter *sub, ExecStatusInfo *i) {
949 uint64_t start_timestamp, exit_timestamp;
950 DBusMessageIter sub2, sub3;
954 int32_t code, status;
959 if (dbus_message_iter_get_arg_type(sub) != DBUS_TYPE_STRUCT)
962 dbus_message_iter_recurse(sub, &sub2);
964 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0)
967 if (!(i->path = strdup(path)))
970 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_ARRAY ||
971 dbus_message_iter_get_element_type(&sub2) != DBUS_TYPE_STRING)
975 dbus_message_iter_recurse(&sub2, &sub3);
976 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
977 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
978 dbus_message_iter_next(&sub3);
983 if (!(i->argv = new0(char*, n+1)))
987 dbus_message_iter_recurse(&sub2, &sub3);
988 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
991 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
992 dbus_message_iter_get_basic(&sub3, &s);
993 dbus_message_iter_next(&sub3);
995 if (!(i->argv[n++] = strdup(s)))
999 if (!dbus_message_iter_next(&sub2) ||
1000 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_timestamp, true) < 0 ||
1001 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_timestamp, true) < 0 ||
1002 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, true) < 0 ||
1003 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &code, true) < 0 ||
1004 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &status, false) < 0)
1007 i->start_timestamp = (usec_t) start_timestamp;
1008 i->exit_timestamp = (usec_t) exit_timestamp;
1009 i->pid = (pid_t) pid;
1016 typedef struct UnitStatusInfo {
1018 const char *load_state;
1019 const char *active_state;
1020 const char *sub_state;
1022 const char *description;
1024 const char *fragment_path;
1025 const char *default_control_group;
1030 const char *status_text;
1033 usec_t start_timestamp;
1034 usec_t exit_timestamp;
1036 int exit_code, exit_status;
1039 unsigned n_accepted;
1040 unsigned n_connections;
1044 const char *sysfs_path;
1046 /* Mount, Automount */
1052 LIST_HEAD(ExecStatusInfo, exec);
1055 static void print_status_info(UnitStatusInfo *i) {
1061 /* This shows pretty information about a unit. See
1062 * print_property() for a low-level property printer */
1064 printf("%s", strna(i->id));
1066 if (i->description && !streq_ptr(i->id, i->description))
1067 printf(" - %s", i->description);
1071 if (i->fragment_path)
1072 printf("\t Loaded: %s (%s)\n", strna(i->load_state), i->fragment_path);
1073 else if (streq_ptr(i->load_state, "failed"))
1074 printf("\t Loaded: " ANSI_HIGHLIGHT_ON "%s" ANSI_HIGHLIGHT_OFF "\n", strna(i->load_state));
1076 printf("\t Loaded: %s\n", strna(i->load_state));
1078 if (streq_ptr(i->active_state, "maintenance")) {
1079 if (streq_ptr(i->active_state, i->sub_state))
1080 printf("\t Active: " ANSI_HIGHLIGHT_ON "%s" ANSI_HIGHLIGHT_OFF "\n",
1081 strna(i->active_state));
1083 printf("\t Active: " ANSI_HIGHLIGHT_ON "%s (%s)" ANSI_HIGHLIGHT_OFF "\n",
1084 strna(i->active_state),
1085 strna(i->sub_state));
1087 if (streq_ptr(i->active_state, i->sub_state))
1088 printf("\t Active: %s\n",
1089 strna(i->active_state));
1091 printf("\t Active: %s (%s)\n",
1092 strna(i->active_state),
1093 strna(i->sub_state));
1097 printf("\t Device: %s\n", i->sysfs_path);
1099 printf("\t Where: %s\n", i->where);
1101 printf("\t What: %s\n", i->what);
1104 printf("\t Status: \"%s\"\n", i->status_text);
1107 printf("\tAccepted: %u; Connected: %u\n", i->n_accepted, i->n_connections);
1109 LIST_FOREACH(exec, p, i->exec) {
1112 /* Only show exited processes here */
1116 t = strv_join(p->argv, " ");
1117 printf("\t Exited: %u (%s, code=%s, ", p->pid, strna(t), sigchld_code_to_string(p->code));
1120 if (p->code == CLD_EXITED)
1121 printf("status=%i", p->status);
1123 printf("signal=%s", signal_to_string(p->status));
1126 if (i->main_pid == p->pid &&
1127 i->start_timestamp == p->start_timestamp &&
1128 i->exit_timestamp == p->start_timestamp)
1129 /* Let's not show this twice */
1132 if (p->pid == i->control_pid)
1136 if (i->main_pid > 0 || i->control_pid > 0) {
1139 if (i->main_pid > 0) {
1140 printf(" Main: %u", (unsigned) i->main_pid);
1144 get_process_name(i->main_pid, &t);
1150 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
1152 if (i->exit_code == CLD_EXITED)
1153 printf("status=%i", i->exit_status);
1155 printf("signal=%s", signal_to_string(i->exit_status));
1160 if (i->main_pid > 0 && i->control_pid > 0)
1163 if (i->control_pid > 0) {
1166 printf(" Control: %u", (unsigned) i->control_pid);
1168 get_process_name(i->control_pid, &t);
1178 if (i->default_control_group) {
1181 printf("\t CGroup: %s\n", i->default_control_group);
1183 if ((c = columns()) > 18)
1188 if ((r = cg_init()) < 0)
1189 log_error("Failed to initialize libcg: %s", strerror(-r));
1191 show_cgroup_recursive(i->default_control_group, "\t\t ", c);
1195 static int status_property(const char *name, DBusMessageIter *iter, UnitStatusInfo *i) {
1197 switch (dbus_message_iter_get_arg_type(iter)) {
1199 case DBUS_TYPE_STRING: {
1202 dbus_message_iter_get_basic(iter, &s);
1205 if (streq(name, "Id"))
1207 else if (streq(name, "LoadState"))
1209 else if (streq(name, "ActiveState"))
1210 i->active_state = s;
1211 else if (streq(name, "SubState"))
1213 else if (streq(name, "Description"))
1215 else if (streq(name, "FragmentPath"))
1216 i->fragment_path = s;
1217 else if (streq(name, "DefaultControlGroup"))
1218 i->default_control_group = s;
1219 else if (streq(name, "StatusText"))
1221 else if (streq(name, "SysFSPath"))
1223 else if (streq(name, "Where"))
1225 else if (streq(name, "What"))
1232 case DBUS_TYPE_BOOLEAN: {
1235 dbus_message_iter_get_basic(iter, &b);
1237 if (streq(name, "Accept"))
1243 case DBUS_TYPE_UINT32: {
1246 dbus_message_iter_get_basic(iter, &u);
1248 if (streq(name, "MainPID")) {
1250 i->main_pid = (pid_t) u;
1253 } else if (streq(name, "ControlPID"))
1254 i->control_pid = (pid_t) u;
1255 else if (streq(name, "ExecMainPID")) {
1257 i->main_pid = (pid_t) u;
1258 } else if (streq(name, "NAccepted"))
1260 else if (streq(name, "NConnections"))
1261 i->n_connections = u;
1266 case DBUS_TYPE_INT32: {
1269 dbus_message_iter_get_basic(iter, &j);
1271 if (streq(name, "ExecMainCode"))
1272 i->exit_code = (int) j;
1273 else if (streq(name, "ExecMainStatus"))
1274 i->exit_status = (int) j;
1279 case DBUS_TYPE_UINT64: {
1282 dbus_message_iter_get_basic(iter, &u);
1284 if (streq(name, "ExecMainStartTimestamp"))
1285 i->start_timestamp = (usec_t) u;
1286 else if (streq(name, "ExecMainExitTimestamp"))
1287 i->exit_timestamp = (usec_t) u;
1292 case DBUS_TYPE_ARRAY: {
1294 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT &&
1295 startswith(name, "Exec")) {
1296 DBusMessageIter sub;
1298 dbus_message_iter_recurse(iter, &sub);
1299 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1300 ExecStatusInfo *info;
1303 if (!(info = new0(ExecStatusInfo, 1)))
1306 if ((r = exec_status_info_deserialize(&sub, info)) < 0) {
1311 LIST_PREPEND(ExecStatusInfo, exec, i->exec, info);
1313 dbus_message_iter_next(&sub);
1324 static int print_property(const char *name, DBusMessageIter *iter) {
1328 /* This is a low-level property printer, see
1329 * print_status_info() for the nicer output */
1331 if (arg_property && !streq(name, arg_property))
1334 switch (dbus_message_iter_get_arg_type(iter)) {
1336 case DBUS_TYPE_STRING: {
1338 dbus_message_iter_get_basic(iter, &s);
1340 if (arg_all || s[0])
1341 printf("%s=%s\n", name, s);
1346 case DBUS_TYPE_BOOLEAN: {
1348 dbus_message_iter_get_basic(iter, &b);
1349 printf("%s=%s\n", name, yes_no(b));
1354 case DBUS_TYPE_UINT64: {
1356 dbus_message_iter_get_basic(iter, &u);
1358 /* Yes, heuristics! But we can change this check
1359 * should it turn out to not be sufficient */
1361 if (strstr(name, "Timestamp")) {
1362 char timestamp[FORMAT_TIMESTAMP_MAX], *t;
1364 if ((t = format_timestamp(timestamp, sizeof(timestamp), u)) || arg_all)
1365 printf("%s=%s\n", name, strempty(t));
1366 } else if (strstr(name, "USec")) {
1367 char timespan[FORMAT_TIMESPAN_MAX];
1369 printf("%s=%s\n", name, format_timespan(timespan, sizeof(timespan), u));
1371 printf("%s=%llu\n", name, (unsigned long long) u);
1376 case DBUS_TYPE_UINT32: {
1378 dbus_message_iter_get_basic(iter, &u);
1380 if (strstr(name, "UMask") || strstr(name, "Mode"))
1381 printf("%s=%04o\n", name, u);
1383 printf("%s=%u\n", name, (unsigned) u);
1388 case DBUS_TYPE_INT32: {
1390 dbus_message_iter_get_basic(iter, &i);
1392 printf("%s=%i\n", name, (int) i);
1396 case DBUS_TYPE_STRUCT: {
1397 DBusMessageIter sub;
1398 dbus_message_iter_recurse(iter, &sub);
1400 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "Job")) {
1403 dbus_message_iter_get_basic(&sub, &u);
1406 printf("%s=%u\n", name, (unsigned) u);
1408 printf("%s=\n", name);
1411 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Unit")) {
1414 dbus_message_iter_get_basic(&sub, &s);
1416 if (arg_all || s[0])
1417 printf("%s=%s\n", name, s);
1425 case DBUS_TYPE_ARRAY:
1427 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
1428 DBusMessageIter sub;
1431 dbus_message_iter_recurse(iter, &sub);
1433 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1434 printf("%s=", name);
1436 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1439 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
1440 dbus_message_iter_get_basic(&sub, &s);
1441 printf("%s%s", space ? " " : "", s);
1444 dbus_message_iter_next(&sub);
1452 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_BYTE) {
1453 DBusMessageIter sub;
1455 dbus_message_iter_recurse(iter, &sub);
1457 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1458 printf("%s=", name);
1460 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1463 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_BYTE);
1464 dbus_message_iter_get_basic(&sub, &u);
1467 dbus_message_iter_next(&sub);
1475 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Paths")) {
1476 DBusMessageIter sub, sub2;
1478 dbus_message_iter_recurse(iter, &sub);
1479 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1480 const char *type, *path;
1482 dbus_message_iter_recurse(&sub, &sub2);
1484 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
1485 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0)
1486 printf("%s=%s\n", type, path);
1488 dbus_message_iter_next(&sub);
1493 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Timers")) {
1494 DBusMessageIter sub, sub2;
1496 dbus_message_iter_recurse(iter, &sub);
1497 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1499 uint64_t value, next_elapse;
1501 dbus_message_iter_recurse(&sub, &sub2);
1503 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &base, true) >= 0 &&
1504 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &value, true) >= 0 &&
1505 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &next_elapse, false) >= 0) {
1506 char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX];
1508 printf("%s={ value=%s ; next_elapse=%s }\n",
1510 format_timespan(timespan1, sizeof(timespan1), value),
1511 format_timespan(timespan2, sizeof(timespan2), next_elapse));
1514 dbus_message_iter_next(&sub);
1519 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && startswith(name, "Exec")) {
1520 DBusMessageIter sub;
1522 dbus_message_iter_recurse(iter, &sub);
1523 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1524 ExecStatusInfo info;
1527 if (exec_status_info_deserialize(&sub, &info) >= 0) {
1528 char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
1531 t = strv_join(info.argv, " ");
1533 printf("%s={ path=%s ; argv[]=%s; start_time=[%s] ; stop_time=[%s] ; pid=%u ; code=%s ; status=%i%s%s }\n",
1537 strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)),
1538 strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)),
1539 (unsigned) info. pid,
1540 sigchld_code_to_string(info.code),
1542 info.code == CLD_EXITED ? "" : "/",
1543 strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status)));
1549 strv_free(info.argv);
1551 dbus_message_iter_next(&sub);
1561 printf("%s=[unprintable]\n", name);
1566 static int show_one(DBusConnection *bus, const char *path, bool show_properties, bool *new_line) {
1567 DBusMessage *m = NULL, *reply = NULL;
1568 const char *interface = "";
1571 DBusMessageIter iter, sub, sub2, sub3;
1572 UnitStatusInfo info;
1580 dbus_error_init(&error);
1582 if (!(m = dbus_message_new_method_call(
1583 "org.freedesktop.systemd1",
1585 "org.freedesktop.DBus.Properties",
1587 log_error("Could not allocate message.");
1592 if (!dbus_message_append_args(m,
1593 DBUS_TYPE_STRING, &interface,
1594 DBUS_TYPE_INVALID)) {
1595 log_error("Could not append arguments to message.");
1600 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1601 log_error("Failed to issue method call: %s", error.message);
1606 if (!dbus_message_iter_init(reply, &iter) ||
1607 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
1608 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
1609 log_error("Failed to parse reply.");
1614 dbus_message_iter_recurse(&iter, &sub);
1621 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1624 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
1625 log_error("Failed to parse reply.");
1630 dbus_message_iter_recurse(&sub, &sub2);
1632 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
1633 log_error("Failed to parse reply.");
1638 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
1639 log_error("Failed to parse reply.");
1644 dbus_message_iter_recurse(&sub2, &sub3);
1646 if (show_properties)
1647 r = print_property(name, &sub3);
1649 r = status_property(name, &sub3, &info);
1652 log_error("Failed to parse reply.");
1657 dbus_message_iter_next(&sub);
1660 if (!show_properties)
1661 print_status_info(&info);
1663 while ((p = info.exec)) {
1664 LIST_REMOVE(ExecStatusInfo, exec, info.exec, p);
1665 exec_status_info_free(p);
1672 dbus_message_unref(m);
1675 dbus_message_unref(reply);
1677 dbus_error_free(&error);
1682 static int show(DBusConnection *bus, char **args, unsigned n) {
1683 DBusMessage *m = NULL, *reply = NULL;
1687 bool show_properties, new_line = false;
1692 dbus_error_init(&error);
1694 show_properties = !streq(args[0], "status");
1696 if (show_properties && n <= 1) {
1697 /* If not argument is specified inspect the manager
1700 r = show_one(bus, "/org/freedesktop/systemd1", show_properties, &new_line);
1704 for (i = 1; i < n; i++) {
1705 const char *path = NULL;
1708 if (!show_properties || safe_atou32(args[i], &id) < 0) {
1710 if (!(m = dbus_message_new_method_call(
1711 "org.freedesktop.systemd1",
1712 "/org/freedesktop/systemd1",
1713 "org.freedesktop.systemd1.Manager",
1715 log_error("Could not allocate message.");
1720 if (!dbus_message_append_args(m,
1721 DBUS_TYPE_STRING, &args[i],
1722 DBUS_TYPE_INVALID)) {
1723 log_error("Could not append arguments to message.");
1728 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1730 if (!dbus_error_has_name(&error, DBUS_ERROR_ACCESS_DENIED)) {
1731 log_error("Failed to issue method call: %s", error.message);
1736 dbus_error_free(&error);
1738 dbus_message_unref(m);
1739 if (!(m = dbus_message_new_method_call(
1740 "org.freedesktop.systemd1",
1741 "/org/freedesktop/systemd1",
1742 "org.freedesktop.systemd1.Manager",
1744 log_error("Could not allocate message.");
1749 if (!dbus_message_append_args(m,
1750 DBUS_TYPE_STRING, &args[i],
1751 DBUS_TYPE_INVALID)) {
1752 log_error("Could not append arguments to message.");
1757 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1758 log_error("Failed to issue method call: %s", error.message);
1766 if (!(m = dbus_message_new_method_call(
1767 "org.freedesktop.systemd1",
1768 "/org/freedesktop/systemd1",
1769 "org.freedesktop.systemd1.Manager",
1771 log_error("Could not allocate message.");
1776 if (!dbus_message_append_args(m,
1777 DBUS_TYPE_UINT32, &id,
1778 DBUS_TYPE_INVALID)) {
1779 log_error("Could not append arguments to message.");
1784 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1785 log_error("Failed to issue method call: %s", error.message);
1791 if (!dbus_message_get_args(reply, &error,
1792 DBUS_TYPE_OBJECT_PATH, &path,
1793 DBUS_TYPE_INVALID)) {
1794 log_error("Failed to parse reply: %s", error.message);
1799 if ((r = show_one(bus, path, show_properties, &new_line)) < 0)
1802 dbus_message_unref(m);
1803 dbus_message_unref(reply);
1811 dbus_message_unref(m);
1814 dbus_message_unref(reply);
1816 dbus_error_free(&error);
1821 static DBusHandlerResult monitor_filter(DBusConnection *connection, DBusMessage *message, void *data) {
1823 DBusMessage *m = NULL, *reply = NULL;
1828 dbus_error_init(&error);
1830 log_debug("Got D-Bus request: %s.%s() on %s",
1831 dbus_message_get_interface(message),
1832 dbus_message_get_member(message),
1833 dbus_message_get_path(message));
1835 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
1836 log_error("Warning! D-Bus connection terminated.");
1837 dbus_connection_close(connection);
1839 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitNew") ||
1840 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitRemoved")) {
1841 const char *id, *path;
1843 if (!dbus_message_get_args(message, &error,
1844 DBUS_TYPE_STRING, &id,
1845 DBUS_TYPE_OBJECT_PATH, &path,
1847 log_error("Failed to parse message: %s", error.message);
1848 else if (streq(dbus_message_get_member(message), "UnitNew"))
1849 printf("Unit %s added.\n", id);
1851 printf("Unit %s removed.\n", id);
1853 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobNew") ||
1854 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
1858 if (!dbus_message_get_args(message, &error,
1859 DBUS_TYPE_UINT32, &id,
1860 DBUS_TYPE_OBJECT_PATH, &path,
1862 log_error("Failed to parse message: %s", error.message);
1863 else if (streq(dbus_message_get_member(message), "JobNew"))
1864 printf("Job %u added.\n", id);
1866 printf("Job %u removed.\n", id);
1869 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Unit", "Changed") ||
1870 dbus_message_is_signal(message, "org.freedesktop.systemd1.Job", "Changed")) {
1872 const char *path, *interface, *property = "Id";
1873 DBusMessageIter iter, sub;
1875 path = dbus_message_get_path(message);
1876 interface = dbus_message_get_interface(message);
1878 if (!(m = dbus_message_new_method_call(
1879 "org.freedesktop.systemd1",
1881 "org.freedesktop.DBus.Properties",
1883 log_error("Could not allocate message.");
1887 if (!dbus_message_append_args(m,
1888 DBUS_TYPE_STRING, &interface,
1889 DBUS_TYPE_STRING, &property,
1890 DBUS_TYPE_INVALID)) {
1891 log_error("Could not append arguments to message.");
1895 if (!(reply = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) {
1896 log_error("Failed to issue method call: %s", error.message);
1900 if (!dbus_message_iter_init(reply, &iter) ||
1901 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1902 log_error("Failed to parse reply.");
1906 dbus_message_iter_recurse(&iter, &sub);
1908 if (streq(interface, "org.freedesktop.systemd1.Unit")) {
1911 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1912 log_error("Failed to parse reply.");
1916 dbus_message_iter_get_basic(&sub, &id);
1917 printf("Unit %s changed.\n", id);
1921 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT32) {
1922 log_error("Failed to parse reply.");
1926 dbus_message_iter_get_basic(&sub, &id);
1927 printf("Job %u changed.\n", id);
1933 dbus_message_unref(m);
1936 dbus_message_unref(reply);
1938 dbus_error_free(&error);
1939 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1943 dbus_message_unref(m);
1946 dbus_message_unref(reply);
1948 dbus_error_free(&error);
1949 return DBUS_HANDLER_RESULT_NEED_MEMORY;
1952 static int monitor(DBusConnection *bus, char **args, unsigned n) {
1953 DBusMessage *m = NULL, *reply = NULL;
1957 dbus_error_init(&error);
1960 dbus_bus_add_match(bus,
1962 "sender='org.freedesktop.systemd1',"
1963 "interface='org.freedesktop.systemd1.Manager',"
1964 "path='/org/freedesktop/systemd1'",
1967 if (dbus_error_is_set(&error)) {
1968 log_error("Failed to add match: %s", error.message);
1973 dbus_bus_add_match(bus,
1975 "sender='org.freedesktop.systemd1',"
1976 "interface='org.freedesktop.systemd1.Unit',"
1980 if (dbus_error_is_set(&error)) {
1981 log_error("Failed to add match: %s", error.message);
1986 dbus_bus_add_match(bus,
1988 "sender='org.freedesktop.systemd1',"
1989 "interface='org.freedesktop.systemd1.Job',"
1993 if (dbus_error_is_set(&error)) {
1994 log_error("Failed to add match: %s", error.message);
2000 if (!dbus_connection_add_filter(bus, monitor_filter, NULL, NULL)) {
2001 log_error("Failed to add filter.");
2006 if (!(m = dbus_message_new_method_call(
2007 "org.freedesktop.systemd1",
2008 "/org/freedesktop/systemd1",
2009 "org.freedesktop.systemd1.Manager",
2011 log_error("Could not allocate message.");
2016 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2017 log_error("Failed to issue method call: %s", error.message);
2022 while (dbus_connection_read_write_dispatch(bus, -1))
2029 /* This is slightly dirty, since we don't undo the filter or the matches. */
2032 dbus_message_unref(m);
2035 dbus_message_unref(reply);
2037 dbus_error_free(&error);
2042 static int dump(DBusConnection *bus, char **args, unsigned n) {
2043 DBusMessage *m = NULL, *reply = NULL;
2048 dbus_error_init(&error);
2050 if (!(m = dbus_message_new_method_call(
2051 "org.freedesktop.systemd1",
2052 "/org/freedesktop/systemd1",
2053 "org.freedesktop.systemd1.Manager",
2055 log_error("Could not allocate message.");
2059 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2060 log_error("Failed to issue method call: %s", error.message);
2065 if (!dbus_message_get_args(reply, &error,
2066 DBUS_TYPE_STRING, &text,
2067 DBUS_TYPE_INVALID)) {
2068 log_error("Failed to parse reply: %s", error.message);
2073 fputs(text, stdout);
2079 dbus_message_unref(m);
2082 dbus_message_unref(reply);
2084 dbus_error_free(&error);
2089 static int snapshot(DBusConnection *bus, char **args, unsigned n) {
2090 DBusMessage *m = NULL, *reply = NULL;
2093 const char *name = "", *path, *id;
2094 dbus_bool_t cleanup = FALSE;
2095 DBusMessageIter iter, sub;
2097 *interface = "org.freedesktop.systemd1.Unit",
2100 dbus_error_init(&error);
2102 if (!(m = dbus_message_new_method_call(
2103 "org.freedesktop.systemd1",
2104 "/org/freedesktop/systemd1",
2105 "org.freedesktop.systemd1.Manager",
2106 "CreateSnapshot"))) {
2107 log_error("Could not allocate message.");
2114 if (!dbus_message_append_args(m,
2115 DBUS_TYPE_STRING, &name,
2116 DBUS_TYPE_BOOLEAN, &cleanup,
2117 DBUS_TYPE_INVALID)) {
2118 log_error("Could not append arguments to message.");
2123 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2124 log_error("Failed to issue method call: %s", error.message);
2129 if (!dbus_message_get_args(reply, &error,
2130 DBUS_TYPE_OBJECT_PATH, &path,
2131 DBUS_TYPE_INVALID)) {
2132 log_error("Failed to parse reply: %s", error.message);
2137 dbus_message_unref(m);
2138 if (!(m = dbus_message_new_method_call(
2139 "org.freedesktop.systemd1",
2141 "org.freedesktop.DBus.Properties",
2143 log_error("Could not allocate message.");
2147 if (!dbus_message_append_args(m,
2148 DBUS_TYPE_STRING, &interface,
2149 DBUS_TYPE_STRING, &property,
2150 DBUS_TYPE_INVALID)) {
2151 log_error("Could not append arguments to message.");
2156 dbus_message_unref(reply);
2157 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2158 log_error("Failed to issue method call: %s", error.message);
2163 if (!dbus_message_iter_init(reply, &iter) ||
2164 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2165 log_error("Failed to parse reply.");
2170 dbus_message_iter_recurse(&iter, &sub);
2172 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2173 log_error("Failed to parse reply.");
2178 dbus_message_iter_get_basic(&sub, &id);
2186 dbus_message_unref(m);
2189 dbus_message_unref(reply);
2191 dbus_error_free(&error);
2196 static int delete_snapshot(DBusConnection *bus, char **args, unsigned n) {
2197 DBusMessage *m = NULL, *reply = NULL;
2205 dbus_error_init(&error);
2207 for (i = 1; i < n; i++) {
2208 const char *path = NULL;
2210 if (!(m = dbus_message_new_method_call(
2211 "org.freedesktop.systemd1",
2212 "/org/freedesktop/systemd1",
2213 "org.freedesktop.systemd1.Manager",
2215 log_error("Could not allocate message.");
2220 if (!dbus_message_append_args(m,
2221 DBUS_TYPE_STRING, &args[i],
2222 DBUS_TYPE_INVALID)) {
2223 log_error("Could not append arguments to message.");
2228 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2229 log_error("Failed to issue method call: %s", error.message);
2234 if (!dbus_message_get_args(reply, &error,
2235 DBUS_TYPE_OBJECT_PATH, &path,
2236 DBUS_TYPE_INVALID)) {
2237 log_error("Failed to parse reply: %s", error.message);
2242 dbus_message_unref(m);
2243 if (!(m = dbus_message_new_method_call(
2244 "org.freedesktop.systemd1",
2246 "org.freedesktop.systemd1.Snapshot",
2248 log_error("Could not allocate message.");
2253 dbus_message_unref(reply);
2254 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2255 log_error("Failed to issue method call: %s", error.message);
2260 dbus_message_unref(m);
2261 dbus_message_unref(reply);
2269 dbus_message_unref(m);
2272 dbus_message_unref(reply);
2274 dbus_error_free(&error);
2279 static int clear_jobs(DBusConnection *bus, char **args, unsigned n) {
2280 DBusMessage *m = NULL, *reply = NULL;
2285 dbus_error_init(&error);
2287 if (arg_action == ACTION_RELOAD)
2289 else if (arg_action == ACTION_REEXEC)
2290 method = "Reexecute";
2292 assert(arg_action == ACTION_SYSTEMCTL);
2295 streq(args[0], "clear-jobs") ? "ClearJobs" :
2296 streq(args[0], "daemon-reload") ? "Reload" :
2297 streq(args[0], "daemon-reexec") ? "Reexecute" :
2301 if (!(m = dbus_message_new_method_call(
2302 "org.freedesktop.systemd1",
2303 "/org/freedesktop/systemd1",
2304 "org.freedesktop.systemd1.Manager",
2306 log_error("Could not allocate message.");
2310 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2312 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
2313 /* There's always a fallback possible for
2314 * legacy actions. */
2319 log_error("Failed to issue method call: %s", error.message);
2328 dbus_message_unref(m);
2331 dbus_message_unref(reply);
2333 dbus_error_free(&error);
2338 static int show_enviroment(DBusConnection *bus, char **args, unsigned n) {
2339 DBusMessage *m = NULL, *reply = NULL;
2341 DBusMessageIter iter, sub, sub2;
2344 *interface = "org.freedesktop.systemd1.Manager",
2345 *property = "Environment";
2347 dbus_error_init(&error);
2349 if (!(m = dbus_message_new_method_call(
2350 "org.freedesktop.systemd1",
2351 "/org/freedesktop/systemd1",
2352 "org.freedesktop.DBus.Properties",
2354 log_error("Could not allocate message.");
2358 if (!dbus_message_append_args(m,
2359 DBUS_TYPE_STRING, &interface,
2360 DBUS_TYPE_STRING, &property,
2361 DBUS_TYPE_INVALID)) {
2362 log_error("Could not append arguments to message.");
2367 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2368 log_error("Failed to issue method call: %s", error.message);
2373 if (!dbus_message_iter_init(reply, &iter) ||
2374 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2375 log_error("Failed to parse reply.");
2380 dbus_message_iter_recurse(&iter, &sub);
2382 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
2383 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
2384 log_error("Failed to parse reply.");
2389 dbus_message_iter_recurse(&sub, &sub2);
2391 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
2394 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
2395 log_error("Failed to parse reply.");
2400 dbus_message_iter_get_basic(&sub2, &text);
2401 printf("%s\n", text);
2403 dbus_message_iter_next(&sub2);
2410 dbus_message_unref(m);
2413 dbus_message_unref(reply);
2415 dbus_error_free(&error);
2420 static int set_environment(DBusConnection *bus, char **args, unsigned n) {
2421 DBusMessage *m = NULL, *reply = NULL;
2425 DBusMessageIter iter, sub;
2428 dbus_error_init(&error);
2430 method = streq(args[0], "set-environment")
2432 : "UnsetEnvironment";
2434 if (!(m = dbus_message_new_method_call(
2435 "org.freedesktop.systemd1",
2436 "/org/freedesktop/systemd1",
2437 "org.freedesktop.systemd1.Manager",
2440 log_error("Could not allocate message.");
2444 dbus_message_iter_init_append(m, &iter);
2446 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
2447 log_error("Could not append arguments to message.");
2452 for (i = 1; i < n; i++)
2453 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &args[i])) {
2454 log_error("Could not append arguments to message.");
2459 if (!dbus_message_iter_close_container(&iter, &sub)) {
2460 log_error("Could not append arguments to message.");
2465 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2466 log_error("Failed to issue method call: %s", error.message);
2475 dbus_message_unref(m);
2478 dbus_message_unref(reply);
2480 dbus_error_free(&error);
2485 static int systemctl_help(void) {
2487 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
2488 "Send control commands to the systemd manager.\n\n"
2489 " -h --help Show this help\n"
2490 " -t --type=TYPE List only units of a particular type\n"
2491 " -p --property=NAME Show only properties by this name\n"
2492 " -a --all Show all units/properties, including dead/empty ones\n"
2493 " --replace When installing a new job, replace existing conflicting ones\n"
2494 " --system Connect to system bus\n"
2495 " --session Connect to session bus\n"
2496 " -q --quiet Suppress output\n"
2497 " --no-block Do not wait until operation finished\n"
2498 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
2500 " list-units List units\n"
2501 " start [NAME...] Start one or more units\n"
2502 " stop [NAME...] Stop one or more units\n"
2503 " restart [NAME...] Restart one or more units\n"
2504 " reload [NAME...] Reload one or more units\n"
2505 " isolate [NAME] Start one unit and stop all others\n"
2506 " check [NAME...] Check whether any of the passed units are active\n"
2507 " status [NAME...] Show status of one or more units\n"
2508 " show [NAME...|JOB...] Show properties of one or more units/jobs/manager\n"
2509 " load [NAME...] Load one or more units\n"
2510 " list-jobs List jobs\n"
2511 " cancel [JOB...] Cancel one or more jobs\n"
2512 " clear-jobs Cancel all jobs\n"
2513 " monitor Monitor unit/job changes\n"
2514 " dump Dump server status\n"
2515 " snapshot [NAME] Create a snapshot\n"
2516 " delete [NAME...] Remove one or more snapshots\n"
2517 " daemon-reload Reload systemd manager configuration\n"
2518 " daemon-reexec Reexecute systemd manager\n"
2519 " daemon-exit Ask the systemd manager to quit\n"
2520 " show-environment Dump environment\n"
2521 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
2522 " unset-environment [NAME...] Unset one or more environment variables\n"
2523 " halt Shut down and halt the system\n"
2524 " poweroff Shut down and power-off the system\n"
2525 " reboot Shut down and reboot the system\n"
2526 " default Enter default mode\n"
2527 " rescue Enter rescue mode\n"
2528 " emergency Enter emergency mode\n",
2529 program_invocation_short_name);
2534 static int halt_help(void) {
2536 printf("%s [OPTIONS...]\n\n"
2537 "%s the system.\n\n"
2538 " --help Show this help\n"
2539 " --halt Halt the machine\n"
2540 " -p --poweroff Switch off the machine\n"
2541 " --reboot Reboot the machine\n"
2542 " -f --force Force immediate halt/power-off/reboot\n"
2543 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
2544 " -d --no-wtmp Don't write wtmp record\n"
2545 " -n --no-sync Don't sync before halt/power-off/reboot\n"
2546 " --no-wall Don't send wall message before halt/power-off/reboot\n",
2547 program_invocation_short_name,
2548 arg_action == ACTION_REBOOT ? "Reboot" :
2549 arg_action == ACTION_POWEROFF ? "Power off" :
2555 static int shutdown_help(void) {
2557 printf("%s [OPTIONS...] [now] [WALL...]\n\n"
2558 "Shut down the system.\n\n"
2559 " --help Show this help\n"
2560 " -H --halt Halt the machine\n"
2561 " -P --poweroff Power-off the machine\n"
2562 " -r --reboot Reboot the machine\n"
2563 " -h Equivalent to --poweroff, overriden by --halt\n"
2564 " -k Don't halt/power-off/reboot, just send warnings\n"
2565 " --no-wall Don't send wall message before halt/power-off/reboot\n",
2566 program_invocation_short_name);
2571 static int telinit_help(void) {
2573 printf("%s [OPTIONS...] {COMMAND}\n\n"
2574 "Send control commands to the init daemon.\n\n"
2575 " --help Show this help\n"
2576 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
2578 " 0 Power-off the machine\n"
2579 " 6 Reboot the machine\n"
2580 " 2, 3, 4, 5 Start runlevelX.target unit\n"
2581 " 1, s, S Enter rescue mode\n"
2582 " q, Q Reload init daemon configuration\n"
2583 " u, U Reexecute init daemon\n",
2584 program_invocation_short_name);
2589 static int runlevel_help(void) {
2591 printf("%s [OPTIONS...]\n\n"
2592 "Prints the previous and current runlevel of the init system.\n\n"
2593 " --help Show this help\n",
2594 program_invocation_short_name);
2599 static int systemctl_parse_argv(int argc, char *argv[]) {
2602 ARG_REPLACE = 0x100,
2609 static const struct option options[] = {
2610 { "help", no_argument, NULL, 'h' },
2611 { "type", required_argument, NULL, 't' },
2612 { "property", required_argument, NULL, 'p' },
2613 { "all", no_argument, NULL, 'a' },
2614 { "replace", no_argument, NULL, ARG_REPLACE },
2615 { "session", no_argument, NULL, ARG_SESSION },
2616 { "system", no_argument, NULL, ARG_SYSTEM },
2617 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
2618 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2619 { "quiet", no_argument, NULL, 'q' },
2620 { NULL, 0, NULL, 0 }
2628 while ((c = getopt_long(argc, argv, "ht:p:aq", options, NULL)) >= 0) {
2641 arg_property = optarg;
2643 /* If the user asked for a particular
2644 * property, show it to him, even if it is
2662 arg_session = false;
2666 arg_no_block = true;
2681 log_error("Unknown option code %c", c);
2689 static int halt_parse_argv(int argc, char *argv[]) {
2698 static const struct option options[] = {
2699 { "help", no_argument, NULL, ARG_HELP },
2700 { "halt", no_argument, NULL, ARG_HALT },
2701 { "poweroff", no_argument, NULL, 'p' },
2702 { "reboot", no_argument, NULL, ARG_REBOOT },
2703 { "force", no_argument, NULL, 'f' },
2704 { "wtmp-only", no_argument, NULL, 'w' },
2705 { "no-wtmp", no_argument, NULL, 'd' },
2706 { "no-sync", no_argument, NULL, 'n' },
2707 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2708 { NULL, 0, NULL, 0 }
2716 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
2717 if (runlevel == '0' || runlevel == '6')
2718 arg_immediate = true;
2720 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
2728 arg_action = ACTION_HALT;
2732 arg_action = ACTION_POWEROFF;
2736 arg_action = ACTION_REBOOT;
2740 arg_immediate = true;
2761 /* Compatibility nops */
2768 log_error("Unknown option code %c", c);
2773 if (optind < argc) {
2774 log_error("Too many arguments.");
2781 static int shutdown_parse_argv(int argc, char *argv[]) {
2788 static const struct option options[] = {
2789 { "help", no_argument, NULL, ARG_HELP },
2790 { "halt", no_argument, NULL, 'H' },
2791 { "poweroff", no_argument, NULL, 'P' },
2792 { "reboot", no_argument, NULL, 'r' },
2793 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2794 { NULL, 0, NULL, 0 }
2802 while ((c = getopt_long(argc, argv, "HPrhkt:a", options, NULL)) >= 0) {
2810 arg_action = ACTION_HALT;
2814 arg_action = ACTION_POWEROFF;
2818 arg_action = ACTION_REBOOT;
2822 if (arg_action != ACTION_HALT)
2823 arg_action = ACTION_POWEROFF;
2836 /* Compatibility nops */
2843 log_error("Unknown option code %c", c);
2848 if (argc > optind && !streq(argv[optind], "now"))
2849 log_warning("First argument '%s' isn't 'now'. Ignoring.", argv[optind]);
2851 /* We ignore the time argument */
2852 if (argc > optind + 1)
2853 arg_wall = argv + optind + 1;
2860 static int telinit_parse_argv(int argc, char *argv[]) {
2867 static const struct option options[] = {
2868 { "help", no_argument, NULL, ARG_HELP },
2869 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2870 { NULL, 0, NULL, 0 }
2873 static const struct {
2877 { '0', ACTION_POWEROFF },
2878 { '6', ACTION_REBOOT },
2879 { '1', ACTION_RESCUE },
2880 { '2', ACTION_RUNLEVEL2 },
2881 { '3', ACTION_RUNLEVEL3 },
2882 { '4', ACTION_RUNLEVEL4 },
2883 { '5', ACTION_RUNLEVEL5 },
2884 { 's', ACTION_RESCUE },
2885 { 'S', ACTION_RESCUE },
2886 { 'q', ACTION_RELOAD },
2887 { 'Q', ACTION_RELOAD },
2888 { 'u', ACTION_REEXEC },
2889 { 'U', ACTION_REEXEC }
2898 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
2913 log_error("Unknown option code %c", c);
2918 if (optind >= argc) {
2923 if (optind + 1 < argc) {
2924 log_error("Too many arguments.");
2928 if (strlen(argv[optind]) != 1) {
2929 log_error("Expected single character argument.");
2933 for (i = 0; i < ELEMENTSOF(table); i++)
2934 if (table[i].from == argv[optind][0])
2937 if (i >= ELEMENTSOF(table)) {
2938 log_error("Unknown command %s.", argv[optind]);
2942 arg_action = table[i].to;
2949 static int runlevel_parse_argv(int argc, char *argv[]) {
2955 static const struct option options[] = {
2956 { "help", no_argument, NULL, ARG_HELP },
2957 { NULL, 0, NULL, 0 }
2965 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
2976 log_error("Unknown option code %c", c);
2981 if (optind < argc) {
2982 log_error("Too many arguments.");
2989 static int parse_argv(int argc, char *argv[]) {
2993 if (program_invocation_short_name) {
2995 if (strstr(program_invocation_short_name, "halt")) {
2996 arg_action = ACTION_HALT;
2997 return halt_parse_argv(argc, argv);
2998 } else if (strstr(program_invocation_short_name, "poweroff")) {
2999 arg_action = ACTION_POWEROFF;
3000 return halt_parse_argv(argc, argv);
3001 } else if (strstr(program_invocation_short_name, "reboot")) {
3002 arg_action = ACTION_REBOOT;
3003 return halt_parse_argv(argc, argv);
3004 } else if (strstr(program_invocation_short_name, "shutdown")) {
3005 arg_action = ACTION_POWEROFF;
3006 return shutdown_parse_argv(argc, argv);
3007 } else if (strstr(program_invocation_short_name, "init")) {
3008 arg_action = ACTION_INVALID;
3009 return telinit_parse_argv(argc, argv);
3010 } else if (strstr(program_invocation_short_name, "runlevel")) {
3011 arg_action = ACTION_RUNLEVEL;
3012 return runlevel_parse_argv(argc, argv);
3016 arg_action = ACTION_SYSTEMCTL;
3017 return systemctl_parse_argv(argc, argv);
3020 static int action_to_runlevel(void) {
3022 static const char table[_ACTION_MAX] = {
3023 [ACTION_HALT] = '0',
3024 [ACTION_POWEROFF] = '0',
3025 [ACTION_REBOOT] = '6',
3026 [ACTION_RUNLEVEL2] = '2',
3027 [ACTION_RUNLEVEL3] = '3',
3028 [ACTION_RUNLEVEL4] = '4',
3029 [ACTION_RUNLEVEL5] = '5',
3030 [ACTION_RESCUE] = '1'
3033 assert(arg_action < _ACTION_MAX);
3035 return table[arg_action];
3038 static int talk_upstart(void) {
3039 DBusMessage *m = NULL, *reply = NULL;
3041 int previous, rl, r;
3043 env1_buf[] = "RUNLEVEL=X",
3044 env2_buf[] = "PREVLEVEL=X";
3045 char *env1 = env1_buf, *env2 = env2_buf;
3046 const char *emit = "runlevel";
3047 dbus_bool_t b_false = FALSE;
3048 DBusMessageIter iter, sub;
3049 DBusConnection *bus;
3051 dbus_error_init(&error);
3053 if (!(rl = action_to_runlevel()))
3056 if (utmp_get_runlevel(&previous, NULL) < 0)
3059 if (!(bus = dbus_connection_open_private("unix:abstract=/com/ubuntu/upstart", &error))) {
3060 if (dbus_error_has_name(&error, DBUS_ERROR_NO_SERVER)) {
3065 log_error("Failed to connect to Upstart bus: %s", error.message);
3070 if ((r = bus_check_peercred(bus)) < 0) {
3071 log_error("Failed to verify owner of bus.");
3075 if (!(m = dbus_message_new_method_call(
3076 "com.ubuntu.Upstart",
3077 "/com/ubuntu/Upstart",
3078 "com.ubuntu.Upstart0_6",
3081 log_error("Could not allocate message.");
3086 dbus_message_iter_init_append(m, &iter);
3088 env1_buf[sizeof(env1_buf)-2] = rl;
3089 env2_buf[sizeof(env2_buf)-2] = previous;
3091 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
3092 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
3093 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
3094 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
3095 !dbus_message_iter_close_container(&iter, &sub) ||
3096 !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
3097 log_error("Could not append arguments to message.");
3102 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3104 if (error_is_no_service(&error)) {
3109 log_error("Failed to issue method call: %s", error.message);
3118 dbus_message_unref(m);
3121 dbus_message_unref(reply);
3124 dbus_connection_close(bus);
3125 dbus_connection_unref(bus);
3128 dbus_error_free(&error);
3133 static int talk_initctl(void) {
3134 struct init_request request;
3138 if (!(rl = action_to_runlevel()))
3142 request.magic = INIT_MAGIC;
3143 request.sleeptime = 0;
3144 request.cmd = INIT_CMD_RUNLVL;
3145 request.runlevel = rl;
3147 if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
3149 if (errno == ENOENT)
3152 log_error("Failed to open "INIT_FIFO": %m");
3157 r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
3158 close_nointr_nofail(fd);
3161 log_error("Failed to write to "INIT_FIFO": %m");
3162 return errno ? -errno : -EIO;
3168 static int systemctl_main(DBusConnection *bus, int argc, char *argv[]) {
3170 static const struct {
3178 int (* const dispatch)(DBusConnection *bus, char **args, unsigned n);
3180 { "list-units", LESS, 1, list_units },
3181 { "list-jobs", EQUAL, 1, list_jobs },
3182 { "clear-jobs", EQUAL, 1, clear_jobs },
3183 { "load", MORE, 2, load_unit },
3184 { "cancel", MORE, 2, cancel_job },
3185 { "start", MORE, 2, start_unit },
3186 { "stop", MORE, 2, start_unit },
3187 { "reload", MORE, 2, start_unit },
3188 { "restart", MORE, 2, start_unit },
3189 { "isolate", EQUAL, 2, start_unit },
3190 { "check", MORE, 2, check_unit },
3191 { "show", MORE, 1, show },
3192 { "status", MORE, 2, show },
3193 { "monitor", EQUAL, 1, monitor },
3194 { "dump", EQUAL, 1, dump },
3195 { "snapshot", LESS, 2, snapshot },
3196 { "delete", MORE, 2, delete_snapshot },
3197 { "daemon-reload", EQUAL, 1, clear_jobs },
3198 { "daemon-reexec", EQUAL, 1, clear_jobs },
3199 { "daemon-exit", EQUAL, 1, clear_jobs },
3200 { "show-environment", EQUAL, 1, show_enviroment },
3201 { "set-environment", MORE, 2, set_environment },
3202 { "unset-environment", MORE, 2, set_environment },
3203 { "halt", EQUAL, 1, start_special },
3204 { "poweroff", EQUAL, 1, start_special },
3205 { "reboot", EQUAL, 1, start_special },
3206 { "default", EQUAL, 1, start_special },
3207 { "rescue", EQUAL, 1, start_special },
3208 { "emergency", EQUAL, 1, start_special }
3218 left = argc - optind;
3221 /* Special rule: no arguments means "list-units" */
3224 if (streq(argv[optind], "help")) {
3229 for (i = 0; i < ELEMENTSOF(verbs); i++)
3230 if (streq(argv[optind], verbs[i].verb))
3233 if (i >= ELEMENTSOF(verbs)) {
3234 log_error("Unknown operation %s", argv[optind]);
3239 switch (verbs[i].argc_cmp) {
3242 if (left != verbs[i].argc) {
3243 log_error("Invalid number of arguments.");
3250 if (left < verbs[i].argc) {
3251 log_error("Too few arguments.");
3258 if (left > verbs[i].argc) {
3259 log_error("Too many arguments.");
3266 assert_not_reached("Unknown comparison operator.");
3269 return verbs[i].dispatch(bus, argv + optind, left);
3272 static int reload_with_fallback(DBusConnection *bus) {
3276 /* First, try systemd via D-Bus. */
3277 if ((r = clear_jobs(bus, NULL, 0)) > 0)
3281 /* Nothing else worked, so let's try signals */
3282 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
3284 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
3285 log_error("kill() failed: %m");
3292 static int start_with_fallback(DBusConnection *bus) {
3295 warn_wall(arg_action);
3298 /* First, try systemd via D-Bus. */
3299 if ((r = start_unit(bus, NULL, 0)) > 0)
3302 /* Hmm, talking to systemd via D-Bus didn't work. Then
3303 * let's try to talk to Upstart via D-Bus. */
3304 if ((r = talk_upstart()) > 0)
3308 /* Nothing else worked, so let's try
3310 if ((r = talk_initctl()) != 0)
3313 log_error("Failed to talk to init daemon.");
3317 static int halt_main(DBusConnection *bus) {
3320 if (geteuid() != 0) {
3321 log_error("Must to be root.");
3325 if (!arg_dry && !arg_immediate)
3326 return start_with_fallback(bus);
3329 if ((r = utmp_put_shutdown(0)) < 0)
3330 log_warning("Failed to write utmp record: %s", strerror(-r));
3338 /* Make sure C-A-D is handled by the kernel from this
3340 reboot(RB_ENABLE_CAD);
3342 switch (arg_action) {
3345 log_info("Halting");
3346 reboot(RB_HALT_SYSTEM);
3349 case ACTION_POWEROFF:
3350 log_info("Powering off");
3351 reboot(RB_POWER_OFF);
3355 log_info("Rebooting");
3356 reboot(RB_AUTOBOOT);
3360 assert_not_reached("Unknown halt action.");
3363 /* We should never reach this. */
3367 static int runlevel_main(void) {
3368 int r, runlevel, previous;
3370 if ((r = utmp_get_runlevel(&runlevel, &previous)) < 0) {
3376 previous <= 0 ? 'N' : previous,
3377 runlevel <= 0 ? 'N' : runlevel);
3382 int main(int argc, char*argv[]) {
3384 DBusConnection *bus = NULL;
3387 dbus_error_init(&error);
3389 log_parse_environment();
3391 if ((r = parse_argv(argc, argv)) < 0)
3398 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
3399 * let's shortcut this */
3400 if (arg_action == ACTION_RUNLEVEL) {
3401 retval = runlevel_main() < 0;
3405 bus_connect(arg_session ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &bus, &private_bus, &error);
3407 switch (arg_action) {
3409 case ACTION_SYSTEMCTL: {
3412 log_error("Failed to get D-Bus connection: %s", error.message);
3416 retval = systemctl_main(bus, argc, argv) < 0;
3421 case ACTION_POWEROFF:
3423 retval = halt_main(bus) < 0;
3426 case ACTION_RUNLEVEL2:
3427 case ACTION_RUNLEVEL3:
3428 case ACTION_RUNLEVEL4:
3429 case ACTION_RUNLEVEL5:
3431 case ACTION_EMERGENCY:
3432 case ACTION_DEFAULT:
3433 retval = start_with_fallback(bus) < 0;
3438 retval = reload_with_fallback(bus) < 0;
3441 case ACTION_INVALID:
3442 case ACTION_RUNLEVEL:
3444 assert_not_reached("Unknown action");
3450 dbus_connection_close(bus);
3451 dbus_connection_unref(bus);
3454 dbus_error_free(&error);