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"
48 static const char *arg_type = NULL;
49 static const char *arg_property = NULL;
50 static bool arg_all = false;
51 static bool arg_replace = false;
52 static bool arg_session = false;
53 static bool arg_no_block = false;
54 static bool arg_immediate = false;
55 static bool arg_no_wtmp = false;
56 static bool arg_no_sync = false;
57 static bool arg_no_wall = false;
58 static bool arg_dry = false;
59 static bool arg_quiet = false;
60 static char **arg_wall = NULL;
78 } arg_action = ACTION_SYSTEMCTL;
80 static bool private_bus = false;
82 static bool error_is_no_service(DBusError *error) {
86 if (!dbus_error_is_set(error))
89 if (dbus_error_has_name(error, DBUS_ERROR_NAME_HAS_NO_OWNER))
92 if (dbus_error_has_name(error, DBUS_ERROR_SERVICE_UNKNOWN))
95 return startswith(error->name, "org.freedesktop.DBus.Error.Spawn.");
98 static int bus_iter_get_basic_and_next(DBusMessageIter *iter, int type, void *data, bool next) {
103 if (dbus_message_iter_get_arg_type(iter) != type)
106 dbus_message_iter_get_basic(iter, data);
108 if (!dbus_message_iter_next(iter) != !next)
114 static void warn_wall(enum action action) {
115 static const char *table[_ACTION_MAX] = {
116 [ACTION_HALT] = "The system is going down for system halt NOW!",
117 [ACTION_REBOOT] = "The system is going down for reboot NOW!",
118 [ACTION_POWEROFF] = "The system is going down for power-off NOW!",
119 [ACTION_RESCUE] = "The system is going down to rescue mode NOW!",
120 [ACTION_EMERGENCY] = "The system is going down to emergency mode NOW!"
129 if (!(p = strv_join(arg_wall, " "))) {
130 log_error("Failed to join strings.");
146 utmp_wall(table[action]);
149 static int list_units(DBusConnection *bus, char **args, unsigned n) {
150 DBusMessage *m = NULL, *reply = NULL;
153 DBusMessageIter iter, sub, sub2;
156 dbus_error_init(&error);
160 if (!(m = dbus_message_new_method_call(
161 "org.freedesktop.systemd1",
162 "/org/freedesktop/systemd1",
163 "org.freedesktop.systemd1.Manager",
165 log_error("Could not allocate message.");
169 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
170 log_error("Failed to issue method call: %s", error.message);
175 if (!dbus_message_iter_init(reply, &iter) ||
176 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
177 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
178 log_error("Failed to parse reply.");
183 dbus_message_iter_recurse(&iter, &sub);
185 printf("%-45s %-6s %-12s %-12s %-15s %s\n", "UNIT", "LOAD", "ACTIVE", "SUB", "JOB", "DESCRIPTION");
187 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
188 const char *id, *description, *load_state, *active_state, *sub_state, *unit_state, *job_type, *job_path, *dot;
191 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
192 log_error("Failed to parse reply.");
197 dbus_message_iter_recurse(&sub, &sub2);
199 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
200 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &description, true) < 0 ||
201 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &load_state, true) < 0 ||
202 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &active_state, true) < 0 ||
203 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &sub_state, true) < 0 ||
204 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_state, true) < 0 ||
205 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &job_id, true) < 0 ||
206 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &job_type, true) < 0 ||
207 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, false) < 0) {
208 log_error("Failed to parse reply.");
213 if ((!arg_type || ((dot = strrchr(id, '.')) &&
214 streq(dot+1, arg_type))) &&
215 (arg_all || !streq(active_state, "inactive"))) {
219 if (streq(active_state, "maintenance"))
220 fputs(ANSI_HIGHLIGHT_ON, stdout);
222 printf("%-45s %-6s %-12s %-12s%n", id, load_state, active_state, sub_state, &a);
225 printf(" %-15s%n", job_type, &b);
229 if (a + b + 2 < columns()) {
233 printf(" %.*s", columns() - a - b - 2, description);
236 if (streq(active_state, "maintenance"))
237 fputs(ANSI_HIGHLIGHT_OFF, stdout);
243 dbus_message_iter_next(&sub);
247 printf("\n%u units listed.\n", k);
249 printf("\n%u live units listed. Pass --all to see dead units, too.\n", k);
255 dbus_message_unref(m);
258 dbus_message_unref(reply);
260 dbus_error_free(&error);
265 static int list_jobs(DBusConnection *bus, char **args, unsigned n) {
266 DBusMessage *m = NULL, *reply = NULL;
269 DBusMessageIter iter, sub, sub2;
272 dbus_error_init(&error);
276 if (!(m = dbus_message_new_method_call(
277 "org.freedesktop.systemd1",
278 "/org/freedesktop/systemd1",
279 "org.freedesktop.systemd1.Manager",
281 log_error("Could not allocate message.");
285 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
286 log_error("Failed to issue method call: %s", error.message);
291 if (!dbus_message_iter_init(reply, &iter) ||
292 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
293 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
294 log_error("Failed to parse reply.");
299 dbus_message_iter_recurse(&iter, &sub);
301 printf("%4s %-45s %-17s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
303 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
304 const char *name, *type, *state, *job_path, *unit_path;
307 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
308 log_error("Failed to parse reply.");
313 dbus_message_iter_recurse(&sub, &sub2);
315 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &id, true) < 0 ||
316 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0 ||
317 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
318 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, true) < 0 ||
319 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, true) < 0 ||
320 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, false) < 0) {
321 log_error("Failed to parse reply.");
326 printf("%4u %-45s %-17s %-7s\n", id, name, type, state);
329 dbus_message_iter_next(&sub);
332 printf("\n%u jobs listed.\n", k);
337 dbus_message_unref(m);
340 dbus_message_unref(reply);
342 dbus_error_free(&error);
347 static int load_unit(DBusConnection *bus, char **args, unsigned n) {
348 DBusMessage *m = NULL, *reply = NULL;
353 dbus_error_init(&error);
358 for (i = 1; i < n; i++) {
360 if (!(m = dbus_message_new_method_call(
361 "org.freedesktop.systemd1",
362 "/org/freedesktop/systemd1",
363 "org.freedesktop.systemd1.Manager",
365 log_error("Could not allocate message.");
370 if (!dbus_message_append_args(m,
371 DBUS_TYPE_STRING, &args[i],
372 DBUS_TYPE_INVALID)) {
373 log_error("Could not append arguments to message.");
378 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
379 log_error("Failed to issue method call: %s", error.message);
384 dbus_message_unref(m);
385 dbus_message_unref(reply);
394 dbus_message_unref(m);
397 dbus_message_unref(reply);
399 dbus_error_free(&error);
404 static int cancel_job(DBusConnection *bus, char **args, unsigned n) {
405 DBusMessage *m = NULL, *reply = NULL;
410 dbus_error_init(&error);
415 for (i = 1; i < n; i++) {
419 if (!(m = dbus_message_new_method_call(
420 "org.freedesktop.systemd1",
421 "/org/freedesktop/systemd1",
422 "org.freedesktop.systemd1.Manager",
424 log_error("Could not allocate message.");
429 if ((r = safe_atou(args[i], &id)) < 0) {
430 log_error("Failed to parse job id: %s", strerror(-r));
434 assert_cc(sizeof(uint32_t) == sizeof(id));
435 if (!dbus_message_append_args(m,
436 DBUS_TYPE_UINT32, &id,
437 DBUS_TYPE_INVALID)) {
438 log_error("Could not append arguments to message.");
443 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
444 log_error("Failed to issue method call: %s", error.message);
449 if (!dbus_message_get_args(reply, &error,
450 DBUS_TYPE_OBJECT_PATH, &path,
451 DBUS_TYPE_INVALID)) {
452 log_error("Failed to parse reply: %s", error.message);
457 dbus_message_unref(m);
458 if (!(m = dbus_message_new_method_call(
459 "org.freedesktop.systemd1",
461 "org.freedesktop.systemd1.Job",
463 log_error("Could not allocate message.");
468 dbus_message_unref(reply);
469 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
470 log_error("Failed to issue method call: %s", error.message);
475 dbus_message_unref(m);
476 dbus_message_unref(reply);
484 dbus_message_unref(m);
487 dbus_message_unref(reply);
489 dbus_error_free(&error);
494 typedef struct WaitData {
499 static DBusHandlerResult wait_filter(DBusConnection *connection, DBusMessage *message, void *data) {
507 dbus_error_init(&error);
509 log_debug("Got D-Bus request: %s.%s() on %s",
510 dbus_message_get_interface(message),
511 dbus_message_get_member(message),
512 dbus_message_get_path(message));
514 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
515 log_error("Warning! D-Bus connection terminated.");
516 dbus_connection_close(connection);
518 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
521 dbus_bool_t success = true;
523 if (!dbus_message_get_args(message, &error,
524 DBUS_TYPE_UINT32, &id,
525 DBUS_TYPE_OBJECT_PATH, &path,
526 DBUS_TYPE_BOOLEAN, &success,
528 log_error("Failed to parse message: %s", error.message);
532 if ((p = set_remove(d->set, (char*) path)))
540 dbus_error_free(&error);
541 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
544 static int enable_wait_for_jobs(DBusConnection *bus) {
552 dbus_error_init(&error);
553 dbus_bus_add_match(bus,
555 "sender='org.freedesktop.systemd1',"
556 "interface='org.freedesktop.systemd1.Manager',"
557 "member='JobRemoved',"
558 "path='/org/freedesktop/systemd1'",
561 if (dbus_error_is_set(&error)) {
562 log_error("Failed to add match: %s", error.message);
563 dbus_error_free(&error);
567 /* This is slightly dirty, since we don't undo the match registrations. */
571 static int wait_for_jobs(DBusConnection *bus, Set *s) {
582 if (!dbus_connection_add_filter(bus, wait_filter, &d, NULL)) {
583 log_error("Failed to add filter.");
588 while (!set_isempty(s) &&
589 dbus_connection_read_write_dispatch(bus, -1))
592 if (!arg_quiet && d.failed)
593 log_error("Job failed, see logs for details.");
595 r = d.failed ? -EIO : 0;
598 /* This is slightly dirty, since we don't undo the filter registration. */
603 static int start_unit_one(
610 DBusMessage *m = NULL, *reply = NULL;
618 assert(arg_no_block || s);
620 dbus_error_init(&error);
622 if (!(m = dbus_message_new_method_call(
623 "org.freedesktop.systemd1",
624 "/org/freedesktop/systemd1",
625 "org.freedesktop.systemd1.Manager",
627 log_error("Could not allocate message.");
632 if (!dbus_message_append_args(m,
633 DBUS_TYPE_STRING, &name,
634 DBUS_TYPE_STRING, &mode,
635 DBUS_TYPE_INVALID)) {
636 log_error("Could not append arguments to message.");
641 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
643 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
644 /* There's always a fallback possible for
650 log_error("Failed to issue method call: %s", error.message);
659 if (!dbus_message_get_args(reply, &error,
660 DBUS_TYPE_OBJECT_PATH, &path,
661 DBUS_TYPE_INVALID)) {
662 log_error("Failed to parse reply: %s", error.message);
667 if (!(p = strdup(path))) {
668 log_error("Failed to duplicate path.");
673 if ((r = set_put(s, p)) < 0) {
675 log_error("Failed to add path to set.");
684 dbus_message_unref(m);
687 dbus_message_unref(reply);
689 dbus_error_free(&error);
694 static enum action verb_to_action(const char *verb) {
695 if (streq(verb, "halt"))
697 else if (streq(verb, "poweroff"))
698 return ACTION_POWEROFF;
699 else if (streq(verb, "reboot"))
700 return ACTION_REBOOT;
701 else if (streq(verb, "rescue"))
702 return ACTION_RESCUE;
703 else if (streq(verb, "emergency"))
704 return ACTION_EMERGENCY;
705 else if (streq(verb, "default"))
706 return ACTION_DEFAULT;
708 return ACTION_INVALID;
711 static int start_unit(DBusConnection *bus, char **args, unsigned n) {
713 static const char * const table[_ACTION_MAX] = {
714 [ACTION_HALT] = SPECIAL_HALT_TARGET,
715 [ACTION_POWEROFF] = SPECIAL_POWEROFF_TARGET,
716 [ACTION_REBOOT] = SPECIAL_REBOOT_TARGET,
717 [ACTION_RUNLEVEL2] = SPECIAL_RUNLEVEL2_TARGET,
718 [ACTION_RUNLEVEL3] = SPECIAL_RUNLEVEL3_TARGET,
719 [ACTION_RUNLEVEL4] = SPECIAL_RUNLEVEL4_TARGET,
720 [ACTION_RUNLEVEL5] = SPECIAL_RUNLEVEL5_TARGET,
721 [ACTION_RESCUE] = SPECIAL_RESCUE_TARGET,
722 [ACTION_EMERGENCY] = SPECIAL_EMERGENCY_SERVICE,
723 [ACTION_DEFAULT] = SPECIAL_DEFAULT_TARGET
728 const char *method, *mode, *one_name;
733 if (arg_action == ACTION_SYSTEMCTL) {
735 streq(args[0], "stop") ? "StopUnit" :
736 streq(args[0], "reload") ? "ReloadUnit" :
737 streq(args[0], "restart") ? "RestartUnit" :
741 (streq(args[0], "isolate") ||
742 streq(args[0], "rescue") ||
743 streq(args[0], "emergency")) ? "isolate" :
744 arg_replace ? "replace" :
747 one_name = table[verb_to_action(args[0])];
750 assert(arg_action < ELEMENTSOF(table));
751 assert(table[arg_action]);
753 method = "StartUnit";
755 mode = (arg_action == ACTION_EMERGENCY ||
756 arg_action == ACTION_RESCUE) ? "isolate" : "replace";
758 one_name = table[arg_action];
762 if ((r = enable_wait_for_jobs(bus)) < 0) {
763 log_error("Could not watch jobs: %s", strerror(-r));
767 if (!(s = set_new(string_hash_func, string_compare_func))) {
768 log_error("Failed to allocate set.");
777 if ((r = start_unit_one(bus, method, one_name, mode, s)) <= 0)
780 for (i = 1; i < n; i++)
781 if ((r = start_unit_one(bus, method, args[i], mode, s)) < 0)
786 r = wait_for_jobs(bus, s);
795 static int start_special(DBusConnection *bus, char **args, unsigned n) {
799 warn_wall(verb_to_action(args[0]));
801 return start_unit(bus, args, n);
804 static int check_unit(DBusConnection *bus, char **args, unsigned n) {
805 DBusMessage *m = NULL, *reply = NULL;
807 *interface = "org.freedesktop.systemd1.Unit",
808 *property = "ActiveState";
809 int r = -EADDRNOTAVAIL;
816 dbus_error_init(&error);
818 for (i = 1; i < n; i++) {
819 const char *path = NULL;
821 DBusMessageIter iter, sub;
823 if (!(m = dbus_message_new_method_call(
824 "org.freedesktop.systemd1",
825 "/org/freedesktop/systemd1",
826 "org.freedesktop.systemd1.Manager",
828 log_error("Could not allocate message.");
833 if (!dbus_message_append_args(m,
834 DBUS_TYPE_STRING, &args[i],
835 DBUS_TYPE_INVALID)) {
836 log_error("Could not append arguments to message.");
841 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
843 /* Hmm, cannot figure out anything about this unit... */
847 dbus_error_free(&error);
851 if (!dbus_message_get_args(reply, &error,
852 DBUS_TYPE_OBJECT_PATH, &path,
853 DBUS_TYPE_INVALID)) {
854 log_error("Failed to parse reply: %s", error.message);
859 dbus_message_unref(m);
860 if (!(m = dbus_message_new_method_call(
861 "org.freedesktop.systemd1",
863 "org.freedesktop.DBus.Properties",
865 log_error("Could not allocate message.");
870 if (!dbus_message_append_args(m,
871 DBUS_TYPE_STRING, &interface,
872 DBUS_TYPE_STRING, &property,
873 DBUS_TYPE_INVALID)) {
874 log_error("Could not append arguments to message.");
879 dbus_message_unref(reply);
880 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
881 log_error("Failed to issue method call: %s", error.message);
886 if (!dbus_message_iter_init(reply, &iter) ||
887 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
888 log_error("Failed to parse reply.");
893 dbus_message_iter_recurse(&iter, &sub);
895 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
896 log_error("Failed to parse reply.");
901 dbus_message_iter_get_basic(&sub, &state);
906 if (streq(state, "active") || startswith(state, "reloading"))
909 dbus_message_unref(m);
910 dbus_message_unref(reply);
916 dbus_message_unref(m);
919 dbus_message_unref(reply);
921 dbus_error_free(&error);
926 typedef struct ExecStatusInfo {
930 usec_t start_timestamp;
931 usec_t exit_timestamp;
936 LIST_FIELDS(struct ExecStatusInfo, exec);
939 static void exec_status_info_free(ExecStatusInfo *i) {
947 static int exec_status_info_deserialize(DBusMessageIter *sub, ExecStatusInfo *i) {
948 uint64_t start_timestamp, exit_timestamp;
949 DBusMessageIter sub2, sub3;
953 int32_t code, status;
958 if (dbus_message_iter_get_arg_type(sub) != DBUS_TYPE_STRUCT)
961 dbus_message_iter_recurse(sub, &sub2);
963 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0)
966 if (!(i->path = strdup(path)))
969 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_ARRAY ||
970 dbus_message_iter_get_element_type(&sub2) != DBUS_TYPE_STRING)
974 dbus_message_iter_recurse(&sub2, &sub3);
975 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
976 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
977 dbus_message_iter_next(&sub3);
982 if (!(i->argv = new0(char*, n+1)))
986 dbus_message_iter_recurse(&sub2, &sub3);
987 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
990 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
991 dbus_message_iter_get_basic(&sub3, &s);
992 dbus_message_iter_next(&sub3);
994 if (!(i->argv[n++] = strdup(s)))
998 if (!dbus_message_iter_next(&sub2) ||
999 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_timestamp, true) < 0 ||
1000 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_timestamp, true) < 0 ||
1001 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, true) < 0 ||
1002 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &code, true) < 0 ||
1003 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &status, false) < 0)
1006 i->start_timestamp = (usec_t) start_timestamp;
1007 i->exit_timestamp = (usec_t) exit_timestamp;
1008 i->pid = (pid_t) pid;
1015 typedef struct UnitStatusInfo {
1017 const char *load_state;
1018 const char *active_state;
1019 const char *sub_state;
1021 const char *description;
1023 const char *fragment_path;
1024 const char *default_control_group;
1029 const char *status_text;
1032 usec_t start_timestamp;
1033 usec_t exit_timestamp;
1035 int exit_code, exit_status;
1038 unsigned n_accepted;
1039 unsigned n_connections;
1043 const char *sysfs_path;
1045 /* Mount, Automount */
1051 LIST_HEAD(ExecStatusInfo, exec);
1054 static void print_status_info(UnitStatusInfo *i) {
1059 /* This shows pretty information about a unit. See
1060 * print_property() for a low-level property printer */
1062 printf("%s", strna(i->id));
1064 if (i->description && !streq_ptr(i->id, i->description))
1065 printf(" - %s", i->description);
1069 if (i->fragment_path)
1070 printf("\t Loaded: %s (%s)\n", strna(i->load_state), i->fragment_path);
1071 else if (streq_ptr(i->load_state, "failed"))
1072 printf("\t Loaded: " ANSI_HIGHLIGHT_ON "%s" ANSI_HIGHLIGHT_OFF "\n", strna(i->load_state));
1074 printf("\t Loaded: %s\n", strna(i->load_state));
1076 if (streq_ptr(i->active_state, "maintenance")) {
1077 if (streq_ptr(i->active_state, i->sub_state))
1078 printf("\t Active: " ANSI_HIGHLIGHT_ON "%s" ANSI_HIGHLIGHT_OFF "\n",
1079 strna(i->active_state));
1081 printf("\t Active: " ANSI_HIGHLIGHT_ON "%s (%s)" ANSI_HIGHLIGHT_OFF "\n",
1082 strna(i->active_state),
1083 strna(i->sub_state));
1085 if (streq_ptr(i->active_state, i->sub_state))
1086 printf("\t Active: %s\n",
1087 strna(i->active_state));
1089 printf("\t Active: %s (%s)\n",
1090 strna(i->active_state),
1091 strna(i->sub_state));
1095 printf("\t Device: %s\n", i->sysfs_path);
1097 printf("\t Where: %s\n", i->where);
1099 printf("\t What: %s\n", i->what);
1102 printf("\t Status: \"%s\"\n", i->status_text);
1105 printf("\tAccepted: %u; Connected: %u\n", i->n_accepted, i->n_connections);
1107 LIST_FOREACH(exec, p, i->exec) {
1110 /* Only show exited processes here */
1114 t = strv_join(p->argv, " ");
1115 printf("\t Exited: %u (%s, code=%s, ", p->pid, strna(t), sigchld_code_to_string(p->code));
1118 if (p->code == CLD_EXITED)
1119 printf("status=%i", p->status);
1121 printf("signal=%s", signal_to_string(p->status));
1124 if (i->main_pid == p->pid &&
1125 i->start_timestamp == p->start_timestamp &&
1126 i->exit_timestamp == p->start_timestamp)
1127 /* Let's not show this twice */
1130 if (p->pid == i->control_pid)
1134 if (i->main_pid > 0 || i->control_pid > 0) {
1137 if (i->main_pid > 0) {
1138 printf(" Main: %u", (unsigned) i->main_pid);
1142 get_process_name(i->main_pid, &t);
1148 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
1150 if (i->exit_code == CLD_EXITED)
1151 printf("status=%i", i->exit_status);
1153 printf("signal=%s", signal_to_string(i->exit_status));
1158 if (i->main_pid > 0 && i->control_pid > 0)
1161 if (i->control_pid > 0) {
1164 printf(" Control: %u", (unsigned) i->control_pid);
1166 get_process_name(i->control_pid, &t);
1176 if (i->default_control_group) {
1179 printf("\t CGroup: %s\n", i->default_control_group);
1181 if ((c = columns()) > 18)
1186 show_cgroup_recursive(i->default_control_group, "\t\t ", c);
1190 static int status_property(const char *name, DBusMessageIter *iter, UnitStatusInfo *i) {
1192 switch (dbus_message_iter_get_arg_type(iter)) {
1194 case DBUS_TYPE_STRING: {
1197 dbus_message_iter_get_basic(iter, &s);
1200 if (streq(name, "Id"))
1202 else if (streq(name, "LoadState"))
1204 else if (streq(name, "ActiveState"))
1205 i->active_state = s;
1206 else if (streq(name, "SubState"))
1208 else if (streq(name, "Description"))
1210 else if (streq(name, "FragmentPath"))
1211 i->fragment_path = s;
1212 else if (streq(name, "DefaultControlGroup"))
1213 i->default_control_group = s;
1214 else if (streq(name, "StatusText"))
1216 else if (streq(name, "SysFSPath"))
1218 else if (streq(name, "Where"))
1220 else if (streq(name, "What"))
1227 case DBUS_TYPE_BOOLEAN: {
1230 dbus_message_iter_get_basic(iter, &b);
1232 if (streq(name, "Accept"))
1238 case DBUS_TYPE_UINT32: {
1241 dbus_message_iter_get_basic(iter, &u);
1243 if (streq(name, "MainPID")) {
1245 i->main_pid = (pid_t) u;
1248 } else if (streq(name, "ControlPID"))
1249 i->control_pid = (pid_t) u;
1250 else if (streq(name, "ExecMainPID")) {
1252 i->main_pid = (pid_t) u;
1253 } else if (streq(name, "NAccepted"))
1255 else if (streq(name, "NConnections"))
1256 i->n_connections = u;
1261 case DBUS_TYPE_INT32: {
1264 dbus_message_iter_get_basic(iter, &j);
1266 if (streq(name, "ExecMainCode"))
1267 i->exit_code = (int) j;
1268 else if (streq(name, "ExecMainStatus"))
1269 i->exit_status = (int) j;
1274 case DBUS_TYPE_UINT64: {
1277 dbus_message_iter_get_basic(iter, &u);
1279 if (streq(name, "ExecMainStartTimestamp"))
1280 i->start_timestamp = (usec_t) u;
1281 else if (streq(name, "ExecMainExitTimestamp"))
1282 i->exit_timestamp = (usec_t) u;
1287 case DBUS_TYPE_ARRAY: {
1289 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT &&
1290 startswith(name, "Exec")) {
1291 DBusMessageIter sub;
1293 dbus_message_iter_recurse(iter, &sub);
1294 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1295 ExecStatusInfo *info;
1298 if (!(info = new0(ExecStatusInfo, 1)))
1301 if ((r = exec_status_info_deserialize(&sub, info)) < 0) {
1306 LIST_PREPEND(ExecStatusInfo, exec, i->exec, info);
1308 dbus_message_iter_next(&sub);
1319 static int print_property(const char *name, DBusMessageIter *iter) {
1323 /* This is a low-level property printer, see
1324 * print_status_info() for the nicer output */
1326 if (arg_property && !streq(name, arg_property))
1329 switch (dbus_message_iter_get_arg_type(iter)) {
1331 case DBUS_TYPE_STRING: {
1333 dbus_message_iter_get_basic(iter, &s);
1335 if (arg_all || s[0])
1336 printf("%s=%s\n", name, s);
1341 case DBUS_TYPE_BOOLEAN: {
1343 dbus_message_iter_get_basic(iter, &b);
1344 printf("%s=%s\n", name, yes_no(b));
1349 case DBUS_TYPE_UINT64: {
1351 dbus_message_iter_get_basic(iter, &u);
1353 /* Yes, heuristics! But we can change this check
1354 * should it turn out to not be sufficient */
1356 if (strstr(name, "Timestamp")) {
1357 char timestamp[FORMAT_TIMESTAMP_MAX], *t;
1359 if ((t = format_timestamp(timestamp, sizeof(timestamp), u)) || arg_all)
1360 printf("%s=%s\n", name, strempty(t));
1361 } else if (strstr(name, "USec")) {
1362 char timespan[FORMAT_TIMESPAN_MAX];
1364 printf("%s=%s\n", name, format_timespan(timespan, sizeof(timespan), u));
1366 printf("%s=%llu\n", name, (unsigned long long) u);
1371 case DBUS_TYPE_UINT32: {
1373 dbus_message_iter_get_basic(iter, &u);
1375 if (strstr(name, "UMask") || strstr(name, "Mode"))
1376 printf("%s=%04o\n", name, u);
1378 printf("%s=%u\n", name, (unsigned) u);
1383 case DBUS_TYPE_INT32: {
1385 dbus_message_iter_get_basic(iter, &i);
1387 printf("%s=%i\n", name, (int) i);
1391 case DBUS_TYPE_STRUCT: {
1392 DBusMessageIter sub;
1393 dbus_message_iter_recurse(iter, &sub);
1395 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "Job")) {
1398 dbus_message_iter_get_basic(&sub, &u);
1401 printf("%s=%u\n", name, (unsigned) u);
1403 printf("%s=\n", name);
1406 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Unit")) {
1409 dbus_message_iter_get_basic(&sub, &s);
1411 if (arg_all || s[0])
1412 printf("%s=%s\n", name, s);
1420 case DBUS_TYPE_ARRAY:
1422 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
1423 DBusMessageIter sub;
1426 dbus_message_iter_recurse(iter, &sub);
1428 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1429 printf("%s=", name);
1431 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1434 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
1435 dbus_message_iter_get_basic(&sub, &s);
1436 printf("%s%s", space ? " " : "", s);
1439 dbus_message_iter_next(&sub);
1447 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_BYTE) {
1448 DBusMessageIter sub;
1450 dbus_message_iter_recurse(iter, &sub);
1452 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1453 printf("%s=", name);
1455 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1458 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_BYTE);
1459 dbus_message_iter_get_basic(&sub, &u);
1462 dbus_message_iter_next(&sub);
1470 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Paths")) {
1471 DBusMessageIter sub, sub2;
1473 dbus_message_iter_recurse(iter, &sub);
1474 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1475 const char *type, *path;
1477 dbus_message_iter_recurse(&sub, &sub2);
1479 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
1480 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0)
1481 printf("%s=%s\n", type, path);
1483 dbus_message_iter_next(&sub);
1488 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Timers")) {
1489 DBusMessageIter sub, sub2;
1491 dbus_message_iter_recurse(iter, &sub);
1492 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1494 uint64_t value, next_elapse;
1496 dbus_message_iter_recurse(&sub, &sub2);
1498 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &base, true) >= 0 &&
1499 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &value, true) >= 0 &&
1500 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &next_elapse, false) >= 0) {
1501 char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX];
1503 printf("%s={ value=%s ; next_elapse=%s }\n",
1505 format_timespan(timespan1, sizeof(timespan1), value),
1506 format_timespan(timespan2, sizeof(timespan2), next_elapse));
1509 dbus_message_iter_next(&sub);
1514 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && startswith(name, "Exec")) {
1515 DBusMessageIter sub;
1517 dbus_message_iter_recurse(iter, &sub);
1518 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1519 ExecStatusInfo info;
1522 if (exec_status_info_deserialize(&sub, &info) >= 0) {
1523 char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
1526 t = strv_join(info.argv, " ");
1528 printf("%s={ path=%s ; argv[]=%s; start_time=[%s] ; stop_time=[%s] ; pid=%u ; code=%s ; status=%i%s%s }\n",
1532 strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)),
1533 strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)),
1534 (unsigned) info. pid,
1535 sigchld_code_to_string(info.code),
1537 info.code == CLD_EXITED ? "" : "/",
1538 strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status)));
1544 strv_free(info.argv);
1546 dbus_message_iter_next(&sub);
1556 printf("%s=[unprintable]\n", name);
1561 static int show_one(DBusConnection *bus, const char *path, bool show_properties, bool *new_line) {
1562 DBusMessage *m = NULL, *reply = NULL;
1563 const char *interface = "";
1566 DBusMessageIter iter, sub, sub2, sub3;
1567 UnitStatusInfo info;
1575 dbus_error_init(&error);
1577 if (!(m = dbus_message_new_method_call(
1578 "org.freedesktop.systemd1",
1580 "org.freedesktop.DBus.Properties",
1582 log_error("Could not allocate message.");
1587 if (!dbus_message_append_args(m,
1588 DBUS_TYPE_STRING, &interface,
1589 DBUS_TYPE_INVALID)) {
1590 log_error("Could not append arguments to message.");
1595 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1596 log_error("Failed to issue method call: %s", error.message);
1601 if (!dbus_message_iter_init(reply, &iter) ||
1602 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
1603 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
1604 log_error("Failed to parse reply.");
1609 dbus_message_iter_recurse(&iter, &sub);
1616 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1619 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
1620 log_error("Failed to parse reply.");
1625 dbus_message_iter_recurse(&sub, &sub2);
1627 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
1628 log_error("Failed to parse reply.");
1633 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
1634 log_error("Failed to parse reply.");
1639 dbus_message_iter_recurse(&sub2, &sub3);
1641 if (show_properties)
1642 r = print_property(name, &sub3);
1644 r = status_property(name, &sub3, &info);
1647 log_error("Failed to parse reply.");
1652 dbus_message_iter_next(&sub);
1655 if (!show_properties)
1656 print_status_info(&info);
1658 while ((p = info.exec)) {
1659 LIST_REMOVE(ExecStatusInfo, exec, info.exec, p);
1660 exec_status_info_free(p);
1667 dbus_message_unref(m);
1670 dbus_message_unref(reply);
1672 dbus_error_free(&error);
1677 static int show(DBusConnection *bus, char **args, unsigned n) {
1678 DBusMessage *m = NULL, *reply = NULL;
1682 bool show_properties, new_line = false;
1687 dbus_error_init(&error);
1689 show_properties = !streq(args[0], "status");
1691 if (show_properties && n <= 1) {
1692 /* If not argument is specified inspect the manager
1695 r = show_one(bus, "/org/freedesktop/systemd1", show_properties, &new_line);
1699 for (i = 1; i < n; i++) {
1700 const char *path = NULL;
1703 if (!show_properties || safe_atou32(args[i], &id) < 0) {
1705 if (!(m = dbus_message_new_method_call(
1706 "org.freedesktop.systemd1",
1707 "/org/freedesktop/systemd1",
1708 "org.freedesktop.systemd1.Manager",
1710 log_error("Could not allocate message.");
1715 if (!dbus_message_append_args(m,
1716 DBUS_TYPE_STRING, &args[i],
1717 DBUS_TYPE_INVALID)) {
1718 log_error("Could not append arguments to message.");
1723 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1725 if (!dbus_error_has_name(&error, DBUS_ERROR_ACCESS_DENIED)) {
1726 log_error("Failed to issue method call: %s", error.message);
1731 dbus_error_free(&error);
1733 dbus_message_unref(m);
1734 if (!(m = dbus_message_new_method_call(
1735 "org.freedesktop.systemd1",
1736 "/org/freedesktop/systemd1",
1737 "org.freedesktop.systemd1.Manager",
1739 log_error("Could not allocate message.");
1744 if (!dbus_message_append_args(m,
1745 DBUS_TYPE_STRING, &args[i],
1746 DBUS_TYPE_INVALID)) {
1747 log_error("Could not append arguments to message.");
1752 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1753 log_error("Failed to issue method call: %s", error.message);
1761 if (!(m = dbus_message_new_method_call(
1762 "org.freedesktop.systemd1",
1763 "/org/freedesktop/systemd1",
1764 "org.freedesktop.systemd1.Manager",
1766 log_error("Could not allocate message.");
1771 if (!dbus_message_append_args(m,
1772 DBUS_TYPE_UINT32, &id,
1773 DBUS_TYPE_INVALID)) {
1774 log_error("Could not append arguments to message.");
1779 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1780 log_error("Failed to issue method call: %s", error.message);
1786 if (!dbus_message_get_args(reply, &error,
1787 DBUS_TYPE_OBJECT_PATH, &path,
1788 DBUS_TYPE_INVALID)) {
1789 log_error("Failed to parse reply: %s", error.message);
1794 if ((r = show_one(bus, path, show_properties, &new_line)) < 0)
1797 dbus_message_unref(m);
1798 dbus_message_unref(reply);
1806 dbus_message_unref(m);
1809 dbus_message_unref(reply);
1811 dbus_error_free(&error);
1816 static DBusHandlerResult monitor_filter(DBusConnection *connection, DBusMessage *message, void *data) {
1818 DBusMessage *m = NULL, *reply = NULL;
1823 dbus_error_init(&error);
1825 log_debug("Got D-Bus request: %s.%s() on %s",
1826 dbus_message_get_interface(message),
1827 dbus_message_get_member(message),
1828 dbus_message_get_path(message));
1830 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
1831 log_error("Warning! D-Bus connection terminated.");
1832 dbus_connection_close(connection);
1834 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitNew") ||
1835 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitRemoved")) {
1836 const char *id, *path;
1838 if (!dbus_message_get_args(message, &error,
1839 DBUS_TYPE_STRING, &id,
1840 DBUS_TYPE_OBJECT_PATH, &path,
1842 log_error("Failed to parse message: %s", error.message);
1843 else if (streq(dbus_message_get_member(message), "UnitNew"))
1844 printf("Unit %s added.\n", id);
1846 printf("Unit %s removed.\n", id);
1848 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobNew") ||
1849 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
1853 if (!dbus_message_get_args(message, &error,
1854 DBUS_TYPE_UINT32, &id,
1855 DBUS_TYPE_OBJECT_PATH, &path,
1857 log_error("Failed to parse message: %s", error.message);
1858 else if (streq(dbus_message_get_member(message), "JobNew"))
1859 printf("Job %u added.\n", id);
1861 printf("Job %u removed.\n", id);
1864 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Unit", "Changed") ||
1865 dbus_message_is_signal(message, "org.freedesktop.systemd1.Job", "Changed")) {
1867 const char *path, *interface, *property = "Id";
1868 DBusMessageIter iter, sub;
1870 path = dbus_message_get_path(message);
1871 interface = dbus_message_get_interface(message);
1873 if (!(m = dbus_message_new_method_call(
1874 "org.freedesktop.systemd1",
1876 "org.freedesktop.DBus.Properties",
1878 log_error("Could not allocate message.");
1882 if (!dbus_message_append_args(m,
1883 DBUS_TYPE_STRING, &interface,
1884 DBUS_TYPE_STRING, &property,
1885 DBUS_TYPE_INVALID)) {
1886 log_error("Could not append arguments to message.");
1890 if (!(reply = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) {
1891 log_error("Failed to issue method call: %s", error.message);
1895 if (!dbus_message_iter_init(reply, &iter) ||
1896 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1897 log_error("Failed to parse reply.");
1901 dbus_message_iter_recurse(&iter, &sub);
1903 if (streq(interface, "org.freedesktop.systemd1.Unit")) {
1906 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1907 log_error("Failed to parse reply.");
1911 dbus_message_iter_get_basic(&sub, &id);
1912 printf("Unit %s changed.\n", id);
1916 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT32) {
1917 log_error("Failed to parse reply.");
1921 dbus_message_iter_get_basic(&sub, &id);
1922 printf("Job %u changed.\n", id);
1928 dbus_message_unref(m);
1931 dbus_message_unref(reply);
1933 dbus_error_free(&error);
1934 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1938 dbus_message_unref(m);
1941 dbus_message_unref(reply);
1943 dbus_error_free(&error);
1944 return DBUS_HANDLER_RESULT_NEED_MEMORY;
1947 static int monitor(DBusConnection *bus, char **args, unsigned n) {
1948 DBusMessage *m = NULL, *reply = NULL;
1952 dbus_error_init(&error);
1955 dbus_bus_add_match(bus,
1957 "sender='org.freedesktop.systemd1',"
1958 "interface='org.freedesktop.systemd1.Manager',"
1959 "path='/org/freedesktop/systemd1'",
1962 if (dbus_error_is_set(&error)) {
1963 log_error("Failed to add match: %s", error.message);
1968 dbus_bus_add_match(bus,
1970 "sender='org.freedesktop.systemd1',"
1971 "interface='org.freedesktop.systemd1.Unit',"
1975 if (dbus_error_is_set(&error)) {
1976 log_error("Failed to add match: %s", error.message);
1981 dbus_bus_add_match(bus,
1983 "sender='org.freedesktop.systemd1',"
1984 "interface='org.freedesktop.systemd1.Job',"
1988 if (dbus_error_is_set(&error)) {
1989 log_error("Failed to add match: %s", error.message);
1995 if (!dbus_connection_add_filter(bus, monitor_filter, NULL, NULL)) {
1996 log_error("Failed to add filter.");
2001 if (!(m = dbus_message_new_method_call(
2002 "org.freedesktop.systemd1",
2003 "/org/freedesktop/systemd1",
2004 "org.freedesktop.systemd1.Manager",
2006 log_error("Could not allocate message.");
2011 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2012 log_error("Failed to issue method call: %s", error.message);
2017 while (dbus_connection_read_write_dispatch(bus, -1))
2024 /* This is slightly dirty, since we don't undo the filter or the matches. */
2027 dbus_message_unref(m);
2030 dbus_message_unref(reply);
2032 dbus_error_free(&error);
2037 static int dump(DBusConnection *bus, char **args, unsigned n) {
2038 DBusMessage *m = NULL, *reply = NULL;
2043 dbus_error_init(&error);
2045 if (!(m = dbus_message_new_method_call(
2046 "org.freedesktop.systemd1",
2047 "/org/freedesktop/systemd1",
2048 "org.freedesktop.systemd1.Manager",
2050 log_error("Could not allocate message.");
2054 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2055 log_error("Failed to issue method call: %s", error.message);
2060 if (!dbus_message_get_args(reply, &error,
2061 DBUS_TYPE_STRING, &text,
2062 DBUS_TYPE_INVALID)) {
2063 log_error("Failed to parse reply: %s", error.message);
2068 fputs(text, stdout);
2074 dbus_message_unref(m);
2077 dbus_message_unref(reply);
2079 dbus_error_free(&error);
2084 static int snapshot(DBusConnection *bus, char **args, unsigned n) {
2085 DBusMessage *m = NULL, *reply = NULL;
2088 const char *name = "", *path, *id;
2089 dbus_bool_t cleanup = FALSE;
2090 DBusMessageIter iter, sub;
2092 *interface = "org.freedesktop.systemd1.Unit",
2095 dbus_error_init(&error);
2097 if (!(m = dbus_message_new_method_call(
2098 "org.freedesktop.systemd1",
2099 "/org/freedesktop/systemd1",
2100 "org.freedesktop.systemd1.Manager",
2101 "CreateSnapshot"))) {
2102 log_error("Could not allocate message.");
2109 if (!dbus_message_append_args(m,
2110 DBUS_TYPE_STRING, &name,
2111 DBUS_TYPE_BOOLEAN, &cleanup,
2112 DBUS_TYPE_INVALID)) {
2113 log_error("Could not append arguments to message.");
2118 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2119 log_error("Failed to issue method call: %s", error.message);
2124 if (!dbus_message_get_args(reply, &error,
2125 DBUS_TYPE_OBJECT_PATH, &path,
2126 DBUS_TYPE_INVALID)) {
2127 log_error("Failed to parse reply: %s", error.message);
2132 dbus_message_unref(m);
2133 if (!(m = dbus_message_new_method_call(
2134 "org.freedesktop.systemd1",
2136 "org.freedesktop.DBus.Properties",
2138 log_error("Could not allocate message.");
2142 if (!dbus_message_append_args(m,
2143 DBUS_TYPE_STRING, &interface,
2144 DBUS_TYPE_STRING, &property,
2145 DBUS_TYPE_INVALID)) {
2146 log_error("Could not append arguments to message.");
2151 dbus_message_unref(reply);
2152 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2153 log_error("Failed to issue method call: %s", error.message);
2158 if (!dbus_message_iter_init(reply, &iter) ||
2159 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2160 log_error("Failed to parse reply.");
2165 dbus_message_iter_recurse(&iter, &sub);
2167 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2168 log_error("Failed to parse reply.");
2173 dbus_message_iter_get_basic(&sub, &id);
2181 dbus_message_unref(m);
2184 dbus_message_unref(reply);
2186 dbus_error_free(&error);
2191 static int delete_snapshot(DBusConnection *bus, char **args, unsigned n) {
2192 DBusMessage *m = NULL, *reply = NULL;
2200 dbus_error_init(&error);
2202 for (i = 1; i < n; i++) {
2203 const char *path = NULL;
2205 if (!(m = dbus_message_new_method_call(
2206 "org.freedesktop.systemd1",
2207 "/org/freedesktop/systemd1",
2208 "org.freedesktop.systemd1.Manager",
2210 log_error("Could not allocate message.");
2215 if (!dbus_message_append_args(m,
2216 DBUS_TYPE_STRING, &args[i],
2217 DBUS_TYPE_INVALID)) {
2218 log_error("Could not append arguments to message.");
2223 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2224 log_error("Failed to issue method call: %s", error.message);
2229 if (!dbus_message_get_args(reply, &error,
2230 DBUS_TYPE_OBJECT_PATH, &path,
2231 DBUS_TYPE_INVALID)) {
2232 log_error("Failed to parse reply: %s", error.message);
2237 dbus_message_unref(m);
2238 if (!(m = dbus_message_new_method_call(
2239 "org.freedesktop.systemd1",
2241 "org.freedesktop.systemd1.Snapshot",
2243 log_error("Could not allocate message.");
2248 dbus_message_unref(reply);
2249 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2250 log_error("Failed to issue method call: %s", error.message);
2255 dbus_message_unref(m);
2256 dbus_message_unref(reply);
2264 dbus_message_unref(m);
2267 dbus_message_unref(reply);
2269 dbus_error_free(&error);
2274 static int clear_jobs(DBusConnection *bus, char **args, unsigned n) {
2275 DBusMessage *m = NULL, *reply = NULL;
2280 dbus_error_init(&error);
2282 if (arg_action == ACTION_RELOAD)
2284 else if (arg_action == ACTION_REEXEC)
2285 method = "Reexecute";
2287 assert(arg_action == ACTION_SYSTEMCTL);
2290 streq(args[0], "clear-jobs") ? "ClearJobs" :
2291 streq(args[0], "daemon-reload") ? "Reload" :
2292 streq(args[0], "daemon-reexec") ? "Reexecute" :
2296 if (!(m = dbus_message_new_method_call(
2297 "org.freedesktop.systemd1",
2298 "/org/freedesktop/systemd1",
2299 "org.freedesktop.systemd1.Manager",
2301 log_error("Could not allocate message.");
2305 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2307 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
2308 /* There's always a fallback possible for
2309 * legacy actions. */
2314 log_error("Failed to issue method call: %s", error.message);
2323 dbus_message_unref(m);
2326 dbus_message_unref(reply);
2328 dbus_error_free(&error);
2333 static int show_enviroment(DBusConnection *bus, char **args, unsigned n) {
2334 DBusMessage *m = NULL, *reply = NULL;
2336 DBusMessageIter iter, sub, sub2;
2339 *interface = "org.freedesktop.systemd1.Manager",
2340 *property = "Environment";
2342 dbus_error_init(&error);
2344 if (!(m = dbus_message_new_method_call(
2345 "org.freedesktop.systemd1",
2346 "/org/freedesktop/systemd1",
2347 "org.freedesktop.DBus.Properties",
2349 log_error("Could not allocate message.");
2353 if (!dbus_message_append_args(m,
2354 DBUS_TYPE_STRING, &interface,
2355 DBUS_TYPE_STRING, &property,
2356 DBUS_TYPE_INVALID)) {
2357 log_error("Could not append arguments to message.");
2362 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2363 log_error("Failed to issue method call: %s", error.message);
2368 if (!dbus_message_iter_init(reply, &iter) ||
2369 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2370 log_error("Failed to parse reply.");
2375 dbus_message_iter_recurse(&iter, &sub);
2377 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
2378 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
2379 log_error("Failed to parse reply.");
2384 dbus_message_iter_recurse(&sub, &sub2);
2386 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
2389 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
2390 log_error("Failed to parse reply.");
2395 dbus_message_iter_get_basic(&sub2, &text);
2396 printf("%s\n", text);
2398 dbus_message_iter_next(&sub2);
2405 dbus_message_unref(m);
2408 dbus_message_unref(reply);
2410 dbus_error_free(&error);
2415 static int set_environment(DBusConnection *bus, char **args, unsigned n) {
2416 DBusMessage *m = NULL, *reply = NULL;
2420 DBusMessageIter iter, sub;
2423 dbus_error_init(&error);
2425 method = streq(args[0], "set-environment")
2427 : "UnsetEnvironment";
2429 if (!(m = dbus_message_new_method_call(
2430 "org.freedesktop.systemd1",
2431 "/org/freedesktop/systemd1",
2432 "org.freedesktop.systemd1.Manager",
2435 log_error("Could not allocate message.");
2439 dbus_message_iter_init_append(m, &iter);
2441 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
2442 log_error("Could not append arguments to message.");
2447 for (i = 1; i < n; i++)
2448 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &args[i])) {
2449 log_error("Could not append arguments to message.");
2454 if (!dbus_message_iter_close_container(&iter, &sub)) {
2455 log_error("Could not append arguments to message.");
2460 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2461 log_error("Failed to issue method call: %s", error.message);
2470 dbus_message_unref(m);
2473 dbus_message_unref(reply);
2475 dbus_error_free(&error);
2480 static int systemctl_help(void) {
2482 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
2483 "Send control commands to the systemd manager.\n\n"
2484 " -h --help Show this help\n"
2485 " -t --type=TYPE List only units of a particular type\n"
2486 " -p --property=NAME Show only properties by this name\n"
2487 " -a --all Show all units/properties, including dead/empty ones\n"
2488 " --replace When installing a new job, replace existing conflicting ones\n"
2489 " --system Connect to system bus\n"
2490 " --session Connect to session bus\n"
2491 " -q --quiet Suppress output\n"
2492 " --no-block Do not wait until operation finished\n"
2493 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
2495 " list-units List units\n"
2496 " start [NAME...] Start one or more units\n"
2497 " stop [NAME...] Stop one or more units\n"
2498 " restart [NAME...] Restart one or more units\n"
2499 " reload [NAME...] Reload one or more units\n"
2500 " isolate [NAME] Start one unit and stop all others\n"
2501 " check [NAME...] Check whether any of the passed units are active\n"
2502 " status [NAME...] Show status of one or more units\n"
2503 " show [NAME...|JOB...] Show properties of one or more units/jobs/manager\n"
2504 " load [NAME...] Load one or more units\n"
2505 " list-jobs List jobs\n"
2506 " cancel [JOB...] Cancel one or more jobs\n"
2507 " clear-jobs Cancel all jobs\n"
2508 " monitor Monitor unit/job changes\n"
2509 " dump Dump server status\n"
2510 " snapshot [NAME] Create a snapshot\n"
2511 " delete [NAME...] Remove one or more snapshots\n"
2512 " daemon-reload Reload systemd manager configuration\n"
2513 " daemon-reexec Reexecute systemd manager\n"
2514 " daemon-exit Ask the systemd manager to quit\n"
2515 " show-environment Dump environment\n"
2516 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
2517 " unset-environment [NAME...] Unset one or more environment variables\n"
2518 " halt Shut down and halt the system\n"
2519 " poweroff Shut down and power-off the system\n"
2520 " reboot Shut down and reboot the system\n"
2521 " default Enter default mode\n"
2522 " rescue Enter rescue mode\n"
2523 " emergency Enter emergency mode\n",
2524 program_invocation_short_name);
2529 static int halt_help(void) {
2531 printf("%s [OPTIONS...]\n\n"
2532 "%s the system.\n\n"
2533 " --help Show this help\n"
2534 " --halt Halt the machine\n"
2535 " -p --poweroff Switch off the machine\n"
2536 " --reboot Reboot the machine\n"
2537 " -f --force Force immediate halt/power-off/reboot\n"
2538 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
2539 " -d --no-wtmp Don't write wtmp record\n"
2540 " -n --no-sync Don't sync before halt/power-off/reboot\n"
2541 " --no-wall Don't send wall message before halt/power-off/reboot\n",
2542 program_invocation_short_name,
2543 arg_action == ACTION_REBOOT ? "Reboot" :
2544 arg_action == ACTION_POWEROFF ? "Power off" :
2550 static int shutdown_help(void) {
2552 printf("%s [OPTIONS...] [now] [WALL...]\n\n"
2553 "Shut down the system.\n\n"
2554 " --help Show this help\n"
2555 " -H --halt Halt the machine\n"
2556 " -P --poweroff Power-off the machine\n"
2557 " -r --reboot Reboot the machine\n"
2558 " -h Equivalent to --poweroff, overriden by --halt\n"
2559 " -k Don't halt/power-off/reboot, just send warnings\n"
2560 " --no-wall Don't send wall message before halt/power-off/reboot\n",
2561 program_invocation_short_name);
2566 static int telinit_help(void) {
2568 printf("%s [OPTIONS...] {COMMAND}\n\n"
2569 "Send control commands to the init daemon.\n\n"
2570 " --help Show this help\n"
2571 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
2573 " 0 Power-off the machine\n"
2574 " 6 Reboot the machine\n"
2575 " 2, 3, 4, 5 Start runlevelX.target unit\n"
2576 " 1, s, S Enter rescue mode\n"
2577 " q, Q Reload init daemon configuration\n"
2578 " u, U Reexecute init daemon\n",
2579 program_invocation_short_name);
2584 static int runlevel_help(void) {
2586 printf("%s [OPTIONS...]\n\n"
2587 "Prints the previous and current runlevel of the init system.\n\n"
2588 " --help Show this help\n",
2589 program_invocation_short_name);
2594 static int systemctl_parse_argv(int argc, char *argv[]) {
2597 ARG_REPLACE = 0x100,
2604 static const struct option options[] = {
2605 { "help", no_argument, NULL, 'h' },
2606 { "type", required_argument, NULL, 't' },
2607 { "property", required_argument, NULL, 'p' },
2608 { "all", no_argument, NULL, 'a' },
2609 { "replace", no_argument, NULL, ARG_REPLACE },
2610 { "session", no_argument, NULL, ARG_SESSION },
2611 { "system", no_argument, NULL, ARG_SYSTEM },
2612 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
2613 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2614 { "quiet", no_argument, NULL, 'q' },
2615 { NULL, 0, NULL, 0 }
2623 while ((c = getopt_long(argc, argv, "ht:p:aq", options, NULL)) >= 0) {
2636 arg_property = optarg;
2638 /* If the user asked for a particular
2639 * property, show it to him, even if it is
2657 arg_session = false;
2661 arg_no_block = true;
2676 log_error("Unknown option code %c", c);
2684 static int halt_parse_argv(int argc, char *argv[]) {
2693 static const struct option options[] = {
2694 { "help", no_argument, NULL, ARG_HELP },
2695 { "halt", no_argument, NULL, ARG_HALT },
2696 { "poweroff", no_argument, NULL, 'p' },
2697 { "reboot", no_argument, NULL, ARG_REBOOT },
2698 { "force", no_argument, NULL, 'f' },
2699 { "wtmp-only", no_argument, NULL, 'w' },
2700 { "no-wtmp", no_argument, NULL, 'd' },
2701 { "no-sync", no_argument, NULL, 'n' },
2702 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2703 { NULL, 0, NULL, 0 }
2711 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
2712 if (runlevel == '0' || runlevel == '6')
2713 arg_immediate = true;
2715 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
2723 arg_action = ACTION_HALT;
2727 arg_action = ACTION_POWEROFF;
2731 arg_action = ACTION_REBOOT;
2735 arg_immediate = true;
2756 /* Compatibility nops */
2763 log_error("Unknown option code %c", c);
2768 if (optind < argc) {
2769 log_error("Too many arguments.");
2776 static int shutdown_parse_argv(int argc, char *argv[]) {
2783 static const struct option options[] = {
2784 { "help", no_argument, NULL, ARG_HELP },
2785 { "halt", no_argument, NULL, 'H' },
2786 { "poweroff", no_argument, NULL, 'P' },
2787 { "reboot", no_argument, NULL, 'r' },
2788 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2789 { NULL, 0, NULL, 0 }
2797 while ((c = getopt_long(argc, argv, "HPrhkt:a", options, NULL)) >= 0) {
2805 arg_action = ACTION_HALT;
2809 arg_action = ACTION_POWEROFF;
2813 arg_action = ACTION_REBOOT;
2817 if (arg_action != ACTION_HALT)
2818 arg_action = ACTION_POWEROFF;
2831 /* Compatibility nops */
2838 log_error("Unknown option code %c", c);
2843 if (argc > optind && !streq(argv[optind], "now"))
2844 log_warning("First argument '%s' isn't 'now'. Ignoring.", argv[optind]);
2846 /* We ignore the time argument */
2847 if (argc > optind + 1)
2848 arg_wall = argv + optind + 1;
2855 static int telinit_parse_argv(int argc, char *argv[]) {
2862 static const struct option options[] = {
2863 { "help", no_argument, NULL, ARG_HELP },
2864 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2865 { NULL, 0, NULL, 0 }
2868 static const struct {
2872 { '0', ACTION_POWEROFF },
2873 { '6', ACTION_REBOOT },
2874 { '1', ACTION_RESCUE },
2875 { '2', ACTION_RUNLEVEL2 },
2876 { '3', ACTION_RUNLEVEL3 },
2877 { '4', ACTION_RUNLEVEL4 },
2878 { '5', ACTION_RUNLEVEL5 },
2879 { 's', ACTION_RESCUE },
2880 { 'S', ACTION_RESCUE },
2881 { 'q', ACTION_RELOAD },
2882 { 'Q', ACTION_RELOAD },
2883 { 'u', ACTION_REEXEC },
2884 { 'U', ACTION_REEXEC }
2893 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
2908 log_error("Unknown option code %c", c);
2913 if (optind >= argc) {
2918 if (optind + 1 < argc) {
2919 log_error("Too many arguments.");
2923 if (strlen(argv[optind]) != 1) {
2924 log_error("Expected single character argument.");
2928 for (i = 0; i < ELEMENTSOF(table); i++)
2929 if (table[i].from == argv[optind][0])
2932 if (i >= ELEMENTSOF(table)) {
2933 log_error("Unknown command %s.", argv[optind]);
2937 arg_action = table[i].to;
2944 static int runlevel_parse_argv(int argc, char *argv[]) {
2950 static const struct option options[] = {
2951 { "help", no_argument, NULL, ARG_HELP },
2952 { NULL, 0, NULL, 0 }
2960 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
2971 log_error("Unknown option code %c", c);
2976 if (optind < argc) {
2977 log_error("Too many arguments.");
2984 static int parse_argv(int argc, char *argv[]) {
2988 if (program_invocation_short_name) {
2990 if (strstr(program_invocation_short_name, "halt")) {
2991 arg_action = ACTION_HALT;
2992 return halt_parse_argv(argc, argv);
2993 } else if (strstr(program_invocation_short_name, "poweroff")) {
2994 arg_action = ACTION_POWEROFF;
2995 return halt_parse_argv(argc, argv);
2996 } else if (strstr(program_invocation_short_name, "reboot")) {
2997 arg_action = ACTION_REBOOT;
2998 return halt_parse_argv(argc, argv);
2999 } else if (strstr(program_invocation_short_name, "shutdown")) {
3000 arg_action = ACTION_POWEROFF;
3001 return shutdown_parse_argv(argc, argv);
3002 } else if (strstr(program_invocation_short_name, "init")) {
3003 arg_action = ACTION_INVALID;
3004 return telinit_parse_argv(argc, argv);
3005 } else if (strstr(program_invocation_short_name, "runlevel")) {
3006 arg_action = ACTION_RUNLEVEL;
3007 return runlevel_parse_argv(argc, argv);
3011 arg_action = ACTION_SYSTEMCTL;
3012 return systemctl_parse_argv(argc, argv);
3015 static int action_to_runlevel(void) {
3017 static const char table[_ACTION_MAX] = {
3018 [ACTION_HALT] = '0',
3019 [ACTION_POWEROFF] = '0',
3020 [ACTION_REBOOT] = '6',
3021 [ACTION_RUNLEVEL2] = '2',
3022 [ACTION_RUNLEVEL3] = '3',
3023 [ACTION_RUNLEVEL4] = '4',
3024 [ACTION_RUNLEVEL5] = '5',
3025 [ACTION_RESCUE] = '1'
3028 assert(arg_action < _ACTION_MAX);
3030 return table[arg_action];
3033 static int talk_upstart(void) {
3034 DBusMessage *m = NULL, *reply = NULL;
3036 int previous, rl, r;
3038 env1_buf[] = "RUNLEVEL=X",
3039 env2_buf[] = "PREVLEVEL=X";
3040 char *env1 = env1_buf, *env2 = env2_buf;
3041 const char *emit = "runlevel";
3042 dbus_bool_t b_false = FALSE;
3043 DBusMessageIter iter, sub;
3044 DBusConnection *bus;
3046 dbus_error_init(&error);
3048 if (!(rl = action_to_runlevel()))
3051 if (utmp_get_runlevel(&previous, NULL) < 0)
3054 if (!(bus = dbus_connection_open("unix:abstract=/com/ubuntu/upstart", &error))) {
3055 if (dbus_error_has_name(&error, DBUS_ERROR_NO_SERVER)) {
3060 log_error("Failed to connect to Upstart bus: %s", error.message);
3065 if ((r = bus_check_peercred(bus)) < 0) {
3066 log_error("Failed to verify owner of bus.");
3070 if (!(m = dbus_message_new_method_call(
3071 "com.ubuntu.Upstart",
3072 "/com/ubuntu/Upstart",
3073 "com.ubuntu.Upstart0_6",
3076 log_error("Could not allocate message.");
3081 dbus_message_iter_init_append(m, &iter);
3083 env1_buf[sizeof(env1_buf)-2] = rl;
3084 env2_buf[sizeof(env2_buf)-2] = previous;
3086 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
3087 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
3088 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
3089 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
3090 !dbus_message_iter_close_container(&iter, &sub) ||
3091 !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
3092 log_error("Could not append arguments to message.");
3097 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3099 if (error_is_no_service(&error)) {
3104 log_error("Failed to issue method call: %s", error.message);
3113 dbus_message_unref(m);
3116 dbus_message_unref(reply);
3119 dbus_connection_unref(bus);
3121 dbus_error_free(&error);
3126 static int talk_initctl(void) {
3127 struct init_request request;
3131 if (!(rl = action_to_runlevel()))
3135 request.magic = INIT_MAGIC;
3136 request.sleeptime = 0;
3137 request.cmd = INIT_CMD_RUNLVL;
3138 request.runlevel = rl;
3140 if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
3142 if (errno == ENOENT)
3145 log_error("Failed to open "INIT_FIFO": %m");
3150 r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
3151 close_nointr_nofail(fd);
3154 log_error("Failed to write to "INIT_FIFO": %m");
3155 return errno ? -errno : -EIO;
3161 static int systemctl_main(DBusConnection *bus, int argc, char *argv[]) {
3163 static const struct {
3171 int (* const dispatch)(DBusConnection *bus, char **args, unsigned n);
3173 { "list-units", LESS, 1, list_units },
3174 { "list-jobs", EQUAL, 1, list_jobs },
3175 { "clear-jobs", EQUAL, 1, clear_jobs },
3176 { "load", MORE, 2, load_unit },
3177 { "cancel", MORE, 2, cancel_job },
3178 { "start", MORE, 2, start_unit },
3179 { "stop", MORE, 2, start_unit },
3180 { "reload", MORE, 2, start_unit },
3181 { "restart", MORE, 2, start_unit },
3182 { "isolate", EQUAL, 2, start_unit },
3183 { "check", MORE, 2, check_unit },
3184 { "show", MORE, 1, show },
3185 { "status", MORE, 2, show },
3186 { "monitor", EQUAL, 1, monitor },
3187 { "dump", EQUAL, 1, dump },
3188 { "snapshot", LESS, 2, snapshot },
3189 { "delete", MORE, 2, delete_snapshot },
3190 { "daemon-reload", EQUAL, 1, clear_jobs },
3191 { "daemon-reexec", EQUAL, 1, clear_jobs },
3192 { "daemon-exit", EQUAL, 1, clear_jobs },
3193 { "show-environment", EQUAL, 1, show_enviroment },
3194 { "set-environment", MORE, 2, set_environment },
3195 { "unset-environment", MORE, 2, set_environment },
3196 { "halt", EQUAL, 1, start_special },
3197 { "poweroff", EQUAL, 1, start_special },
3198 { "reboot", EQUAL, 1, start_special },
3199 { "default", EQUAL, 1, start_special },
3200 { "rescue", EQUAL, 1, start_special },
3201 { "emergency", EQUAL, 1, start_special }
3211 left = argc - optind;
3214 /* Special rule: no arguments means "list-units" */
3217 if (streq(argv[optind], "help")) {
3222 for (i = 0; i < ELEMENTSOF(verbs); i++)
3223 if (streq(argv[optind], verbs[i].verb))
3226 if (i >= ELEMENTSOF(verbs)) {
3227 log_error("Unknown operation %s", argv[optind]);
3232 switch (verbs[i].argc_cmp) {
3235 if (left != verbs[i].argc) {
3236 log_error("Invalid number of arguments.");
3243 if (left < verbs[i].argc) {
3244 log_error("Too few arguments.");
3251 if (left > verbs[i].argc) {
3252 log_error("Too many arguments.");
3259 assert_not_reached("Unknown comparison operator.");
3262 return verbs[i].dispatch(bus, argv + optind, left);
3265 static int reload_with_fallback(DBusConnection *bus) {
3269 /* First, try systemd via D-Bus. */
3270 if ((r = clear_jobs(bus, NULL, 0)) > 0)
3274 /* Nothing else worked, so let's try signals */
3275 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
3277 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
3278 log_error("kill() failed: %m");
3285 static int start_with_fallback(DBusConnection *bus) {
3288 warn_wall(arg_action);
3291 /* First, try systemd via D-Bus. */
3292 if ((r = start_unit(bus, NULL, 0)) > 0)
3295 /* Hmm, talking to systemd via D-Bus didn't work. Then
3296 * let's try to talk to Upstart via D-Bus. */
3297 if ((r = talk_upstart()) > 0)
3301 /* Nothing else worked, so let's try
3303 if ((r = talk_initctl()) != 0)
3306 log_error("Failed to talk to init daemon.");
3310 static int halt_main(DBusConnection *bus) {
3313 if (geteuid() != 0) {
3314 log_error("Must to be root.");
3318 if (!arg_dry && !arg_immediate)
3319 return start_with_fallback(bus);
3322 if ((r = utmp_put_shutdown(0)) < 0)
3323 log_warning("Failed to write utmp record: %s", strerror(-r));
3331 /* Make sure C-A-D is handled by the kernel from this
3333 reboot(RB_ENABLE_CAD);
3335 switch (arg_action) {
3338 log_info("Halting");
3339 reboot(RB_HALT_SYSTEM);
3342 case ACTION_POWEROFF:
3343 log_info("Powering off");
3344 reboot(RB_POWER_OFF);
3348 log_info("Rebooting");
3349 reboot(RB_AUTOBOOT);
3353 assert_not_reached("Unknown halt action.");
3356 /* We should never reach this. */
3360 static int runlevel_main(void) {
3361 int r, runlevel, previous;
3363 if ((r = utmp_get_runlevel(&runlevel, &previous)) < 0) {
3369 previous <= 0 ? 'N' : previous,
3370 runlevel <= 0 ? 'N' : runlevel);
3375 int main(int argc, char*argv[]) {
3377 DBusConnection *bus = NULL;
3380 dbus_error_init(&error);
3382 log_parse_environment();
3384 if ((r = parse_argv(argc, argv)) < 0)
3391 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
3392 * let's shortcut this */
3393 if (arg_action == ACTION_RUNLEVEL) {
3394 retval = runlevel_main() < 0;
3398 bus_connect(arg_session ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &bus, &private_bus, &error);
3400 switch (arg_action) {
3402 case ACTION_SYSTEMCTL: {
3405 log_error("Failed to get D-Bus connection: %s", error.message);
3409 retval = systemctl_main(bus, argc, argv) < 0;
3414 case ACTION_POWEROFF:
3416 retval = halt_main(bus) < 0;
3419 case ACTION_RUNLEVEL2:
3420 case ACTION_RUNLEVEL3:
3421 case ACTION_RUNLEVEL4:
3422 case ACTION_RUNLEVEL5:
3424 case ACTION_EMERGENCY:
3425 case ACTION_DEFAULT:
3426 retval = start_with_fallback(bus) < 0;
3431 retval = reload_with_fallback(bus) < 0;
3434 case ACTION_INVALID:
3435 case ACTION_RUNLEVEL:
3437 assert_not_reached("Unknown action");
3443 dbus_connection_unref(bus);
3445 dbus_error_free(&error);