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"
45 static const char *arg_type = NULL;
46 static const char *arg_property = NULL;
47 static bool arg_all = false;
48 static bool arg_replace = false;
49 static bool arg_session = false;
50 static bool arg_no_block = false;
51 static bool arg_immediate = false;
52 static bool arg_no_wtmp = false;
53 static bool arg_no_sync = false;
54 static bool arg_no_wall = false;
55 static bool arg_dry = false;
56 static bool arg_quiet = false;
57 static char **arg_wall = NULL;
75 } arg_action = ACTION_SYSTEMCTL;
77 static bool error_is_no_service(DBusError *error) {
81 if (!dbus_error_is_set(error))
84 if (dbus_error_has_name(error, DBUS_ERROR_NAME_HAS_NO_OWNER))
87 if (dbus_error_has_name(error, DBUS_ERROR_SERVICE_UNKNOWN))
90 return startswith(error->name, "org.freedesktop.DBus.Error.Spawn.");
93 static int bus_iter_get_basic_and_next(DBusMessageIter *iter, int type, void *data, bool next) {
98 if (dbus_message_iter_get_arg_type(iter) != type)
101 dbus_message_iter_get_basic(iter, data);
103 if (!dbus_message_iter_next(iter) != !next)
109 static int bus_check_peercred(DBusConnection *c) {
116 assert_se(dbus_connection_get_unix_fd(c, &fd));
118 l = sizeof(struct ucred);
119 if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &ucred, &l) < 0) {
120 log_error("SO_PEERCRED failed: %m");
124 if (l != sizeof(struct ucred)) {
125 log_error("SO_PEERCRED returned wrong size.");
135 static int columns(void) {
136 static int parsed_columns = 0;
139 if (parsed_columns > 0)
140 return parsed_columns;
142 if ((e = getenv("COLUMNS")))
143 parsed_columns = atoi(e);
145 if (parsed_columns <= 0) {
149 if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) >= 0)
150 parsed_columns = ws.ws_col;
153 if (parsed_columns <= 0)
156 return parsed_columns;
160 static void warn_wall(enum action action) {
161 static const char *table[_ACTION_MAX] = {
162 [ACTION_HALT] = "The system is going down for system halt NOW!",
163 [ACTION_REBOOT] = "The system is going down for reboot NOW!",
164 [ACTION_POWEROFF] = "The system is going down for power-off NOW!",
165 [ACTION_RESCUE] = "The system is going down to rescue mode NOW!",
166 [ACTION_EMERGENCY] = "The system is going down to emergency mode NOW!"
175 if (!(p = strv_join(arg_wall, " "))) {
176 log_error("Failed to join strings.");
192 utmp_wall(table[action]);
195 static int list_units(DBusConnection *bus, char **args, unsigned n) {
196 DBusMessage *m = NULL, *reply = NULL;
199 DBusMessageIter iter, sub, sub2;
202 dbus_error_init(&error);
206 if (!(m = dbus_message_new_method_call(
207 "org.freedesktop.systemd1",
208 "/org/freedesktop/systemd1",
209 "org.freedesktop.systemd1.Manager",
211 log_error("Could not allocate message.");
215 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
216 log_error("Failed to issue method call: %s", error.message);
221 if (!dbus_message_iter_init(reply, &iter) ||
222 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
223 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
224 log_error("Failed to parse reply.");
229 dbus_message_iter_recurse(&iter, &sub);
231 printf("%-45s %-6s %-12s %-12s %-15s %s\n", "UNIT", "LOAD", "ACTIVE", "SUB", "JOB", "DESCRIPTION");
233 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
234 const char *id, *description, *load_state, *active_state, *sub_state, *unit_state, *job_type, *job_path, *dot;
237 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
238 log_error("Failed to parse reply.");
243 dbus_message_iter_recurse(&sub, &sub2);
245 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
246 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &description, true) < 0 ||
247 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &load_state, true) < 0 ||
248 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &active_state, true) < 0 ||
249 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &sub_state, true) < 0 ||
250 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_state, true) < 0 ||
251 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &job_id, true) < 0 ||
252 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &job_type, true) < 0 ||
253 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, false) < 0) {
254 log_error("Failed to parse reply.");
259 if ((!arg_type || ((dot = strrchr(id, '.')) &&
260 streq(dot+1, arg_type))) &&
261 (arg_all || !streq(active_state, "inactive"))) {
265 printf("%-45s %-6s %-12s %-12s%n", id, load_state, active_state, sub_state, &a);
268 printf(" %-15s%n", job_type, &b);
272 if (a + b + 2 < columns()) {
276 printf("%.*s", columns() - a - b - 2, description);
283 dbus_message_iter_next(&sub);
287 printf("\n%u units listed.\n", k);
289 printf("\n%u live units listed. Pass --all to see dead units, too.\n", k);
295 dbus_message_unref(m);
298 dbus_message_unref(reply);
300 dbus_error_free(&error);
305 static int list_jobs(DBusConnection *bus, char **args, unsigned n) {
306 DBusMessage *m = NULL, *reply = NULL;
309 DBusMessageIter iter, sub, sub2;
312 dbus_error_init(&error);
316 if (!(m = dbus_message_new_method_call(
317 "org.freedesktop.systemd1",
318 "/org/freedesktop/systemd1",
319 "org.freedesktop.systemd1.Manager",
321 log_error("Could not allocate message.");
325 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
326 log_error("Failed to issue method call: %s", error.message);
331 if (!dbus_message_iter_init(reply, &iter) ||
332 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
333 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
334 log_error("Failed to parse reply.");
339 dbus_message_iter_recurse(&iter, &sub);
341 printf("%4s %-45s %-17s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
343 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
344 const char *name, *type, *state, *job_path, *unit_path;
347 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
348 log_error("Failed to parse reply.");
353 dbus_message_iter_recurse(&sub, &sub2);
355 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &id, true) < 0 ||
356 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0 ||
357 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
358 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, true) < 0 ||
359 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, true) < 0 ||
360 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, false) < 0) {
361 log_error("Failed to parse reply.");
366 printf("%4u %-45s %-17s %-7s\n", id, name, type, state);
369 dbus_message_iter_next(&sub);
372 printf("\n%u jobs listed.\n", k);
377 dbus_message_unref(m);
380 dbus_message_unref(reply);
382 dbus_error_free(&error);
387 static int load_unit(DBusConnection *bus, char **args, unsigned n) {
388 DBusMessage *m = NULL, *reply = NULL;
393 dbus_error_init(&error);
398 for (i = 1; i < n; i++) {
400 if (!(m = dbus_message_new_method_call(
401 "org.freedesktop.systemd1",
402 "/org/freedesktop/systemd1",
403 "org.freedesktop.systemd1.Manager",
405 log_error("Could not allocate message.");
410 if (!dbus_message_append_args(m,
411 DBUS_TYPE_STRING, &args[i],
412 DBUS_TYPE_INVALID)) {
413 log_error("Could not append arguments to message.");
418 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
419 log_error("Failed to issue method call: %s", error.message);
424 dbus_message_unref(m);
425 dbus_message_unref(reply);
434 dbus_message_unref(m);
437 dbus_message_unref(reply);
439 dbus_error_free(&error);
444 static int cancel_job(DBusConnection *bus, char **args, unsigned n) {
445 DBusMessage *m = NULL, *reply = NULL;
450 dbus_error_init(&error);
455 for (i = 1; i < n; i++) {
459 if (!(m = dbus_message_new_method_call(
460 "org.freedesktop.systemd1",
461 "/org/freedesktop/systemd1",
462 "org.freedesktop.systemd1.Manager",
464 log_error("Could not allocate message.");
469 if ((r = safe_atou(args[i], &id)) < 0) {
470 log_error("Failed to parse job id: %s", strerror(-r));
474 assert_cc(sizeof(uint32_t) == sizeof(id));
475 if (!dbus_message_append_args(m,
476 DBUS_TYPE_UINT32, &id,
477 DBUS_TYPE_INVALID)) {
478 log_error("Could not append arguments to message.");
483 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
484 log_error("Failed to issue method call: %s", error.message);
489 if (!dbus_message_get_args(reply, &error,
490 DBUS_TYPE_OBJECT_PATH, &path,
491 DBUS_TYPE_INVALID)) {
492 log_error("Failed to parse reply: %s", error.message);
497 dbus_message_unref(m);
498 if (!(m = dbus_message_new_method_call(
499 "org.freedesktop.systemd1",
501 "org.freedesktop.systemd1.Job",
503 log_error("Could not allocate message.");
508 dbus_message_unref(reply);
509 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
510 log_error("Failed to issue method call: %s", error.message);
515 dbus_message_unref(m);
516 dbus_message_unref(reply);
524 dbus_message_unref(m);
527 dbus_message_unref(reply);
529 dbus_error_free(&error);
534 typedef struct WaitData {
539 static DBusHandlerResult wait_filter(DBusConnection *connection, DBusMessage *message, void *data) {
547 dbus_error_init(&error);
549 /* log_debug("Got D-Bus request: %s.%s() on %s", */
550 /* dbus_message_get_interface(message), */
551 /* dbus_message_get_member(message), */
552 /* dbus_message_get_path(message)); */
554 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
555 log_error("Warning! D-Bus connection terminated.");
556 dbus_connection_close(connection);
558 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
561 dbus_bool_t success = true;
563 if (!dbus_message_get_args(message, &error,
564 DBUS_TYPE_UINT32, &id,
565 DBUS_TYPE_OBJECT_PATH, &path,
566 DBUS_TYPE_BOOLEAN, &success,
568 log_error("Failed to parse message: %s", error.message);
572 if ((p = set_remove(d->set, (char*) path)))
580 dbus_error_free(&error);
581 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
584 static int enable_wait_for_jobs(DBusConnection *bus) {
589 dbus_error_init(&error);
590 dbus_bus_add_match(bus,
592 "sender='org.freedesktop.systemd1',"
593 "interface='org.freedesktop.systemd1.Manager',"
594 "member='JobRemoved',"
595 "path='/org/freedesktop/systemd1'",
598 if (dbus_error_is_set(&error)) {
599 log_error("Failed to add match: %s", error.message);
600 dbus_error_free(&error);
604 /* This is slightly dirty, since we don't undo the match registrations. */
608 static int wait_for_jobs(DBusConnection *bus, Set *s) {
619 if (!dbus_connection_add_filter(bus, wait_filter, &d, NULL)) {
620 log_error("Failed to add filter.");
625 while (!set_isempty(s) &&
626 dbus_connection_read_write_dispatch(bus, -1))
629 if (!arg_quiet && d.failed)
630 log_error("Job failed, see logs for details.");
632 r = d.failed ? -EIO : 0;
635 /* This is slightly dirty, since we don't undo the filter registration. */
640 static int start_unit_one(
647 DBusMessage *m = NULL, *reply = NULL;
655 assert(arg_no_block || s);
657 dbus_error_init(&error);
659 if (!(m = dbus_message_new_method_call(
660 "org.freedesktop.systemd1",
661 "/org/freedesktop/systemd1",
662 "org.freedesktop.systemd1.Manager",
664 log_error("Could not allocate message.");
669 if (!dbus_message_append_args(m,
670 DBUS_TYPE_STRING, &name,
671 DBUS_TYPE_STRING, &mode,
672 DBUS_TYPE_INVALID)) {
673 log_error("Could not append arguments to message.");
678 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
680 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
681 /* There's always a fallback possible for
687 log_error("Failed to issue method call: %s", error.message);
696 if (!dbus_message_get_args(reply, &error,
697 DBUS_TYPE_OBJECT_PATH, &path,
698 DBUS_TYPE_INVALID)) {
699 log_error("Failed to parse reply: %s", error.message);
704 if (!(p = strdup(path))) {
705 log_error("Failed to duplicate path.");
710 if ((r = set_put(s, p)) < 0) {
712 log_error("Failed to add path to set.");
721 dbus_message_unref(m);
724 dbus_message_unref(reply);
726 dbus_error_free(&error);
731 static enum action verb_to_action(const char *verb) {
732 if (streq(verb, "halt"))
734 else if (streq(verb, "poweroff"))
735 return ACTION_POWEROFF;
736 else if (streq(verb, "reboot"))
737 return ACTION_REBOOT;
738 else if (streq(verb, "rescue"))
739 return ACTION_RESCUE;
740 else if (streq(verb, "emergency"))
741 return ACTION_EMERGENCY;
742 else if (streq(verb, "default"))
743 return ACTION_DEFAULT;
745 return ACTION_INVALID;
748 static int start_unit(DBusConnection *bus, char **args, unsigned n) {
750 static const char * const table[_ACTION_MAX] = {
751 [ACTION_HALT] = SPECIAL_HALT_TARGET,
752 [ACTION_POWEROFF] = SPECIAL_POWEROFF_TARGET,
753 [ACTION_REBOOT] = SPECIAL_REBOOT_TARGET,
754 [ACTION_RUNLEVEL2] = SPECIAL_RUNLEVEL2_TARGET,
755 [ACTION_RUNLEVEL3] = SPECIAL_RUNLEVEL3_TARGET,
756 [ACTION_RUNLEVEL4] = SPECIAL_RUNLEVEL4_TARGET,
757 [ACTION_RUNLEVEL5] = SPECIAL_RUNLEVEL5_TARGET,
758 [ACTION_RESCUE] = SPECIAL_RESCUE_TARGET,
759 [ACTION_EMERGENCY] = SPECIAL_EMERGENCY_SERVICE,
760 [ACTION_DEFAULT] = SPECIAL_DEFAULT_TARGET
765 const char *method, *mode, *one_name;
770 if (arg_action == ACTION_SYSTEMCTL) {
772 streq(args[0], "stop") ? "StopUnit" :
773 streq(args[0], "reload") ? "ReloadUnit" :
774 streq(args[0], "restart") ? "RestartUnit" :
778 (streq(args[0], "isolate") ||
779 streq(args[0], "rescue") ||
780 streq(args[0], "emergency")) ? "isolate" :
781 arg_replace ? "replace" :
784 one_name = table[verb_to_action(args[0])];
787 assert(arg_action < ELEMENTSOF(table));
788 assert(table[arg_action]);
790 method = "StartUnit";
792 mode = (arg_action == ACTION_EMERGENCY ||
793 arg_action == ACTION_RESCUE) ? "isolate" : "replace";
795 one_name = table[arg_action];
799 if ((r = enable_wait_for_jobs(bus)) < 0) {
800 log_error("Could not watch jobs: %s", strerror(-r));
804 if (!(s = set_new(string_hash_func, string_compare_func))) {
805 log_error("Failed to allocate set.");
814 if ((r = start_unit_one(bus, method, one_name, mode, s)) <= 0)
817 for (i = 1; i < n; i++)
818 if ((r = start_unit_one(bus, method, args[i], mode, s)) < 0)
823 r = wait_for_jobs(bus, s);
832 static int start_special(DBusConnection *bus, char **args, unsigned n) {
836 warn_wall(verb_to_action(args[0]));
838 return start_unit(bus, args, n);
841 static int check_unit(DBusConnection *bus, char **args, unsigned n) {
842 DBusMessage *m = NULL, *reply = NULL;
844 *interface = "org.freedesktop.systemd1.Unit",
845 *property = "ActiveState";
846 int r = -EADDRNOTAVAIL;
853 dbus_error_init(&error);
855 for (i = 1; i < n; i++) {
856 const char *path = NULL;
858 DBusMessageIter iter, sub;
860 if (!(m = dbus_message_new_method_call(
861 "org.freedesktop.systemd1",
862 "/org/freedesktop/systemd1",
863 "org.freedesktop.systemd1.Manager",
865 log_error("Could not allocate message.");
870 if (!dbus_message_append_args(m,
871 DBUS_TYPE_STRING, &args[i],
872 DBUS_TYPE_INVALID)) {
873 log_error("Could not append arguments to message.");
878 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
880 /* Hmm, cannot figure out anything about this unit... */
887 if (!dbus_message_get_args(reply, &error,
888 DBUS_TYPE_OBJECT_PATH, &path,
889 DBUS_TYPE_INVALID)) {
890 log_error("Failed to parse reply: %s", error.message);
895 dbus_message_unref(m);
896 if (!(m = dbus_message_new_method_call(
897 "org.freedesktop.systemd1",
899 "org.freedesktop.DBus.Properties",
901 log_error("Could not allocate message.");
906 if (!dbus_message_append_args(m,
907 DBUS_TYPE_STRING, &interface,
908 DBUS_TYPE_STRING, &property,
909 DBUS_TYPE_INVALID)) {
910 log_error("Could not append arguments to message.");
915 dbus_message_unref(reply);
916 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
917 log_error("Failed to issue method call: %s", error.message);
922 if (!dbus_message_iter_init(reply, &iter) ||
923 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
924 log_error("Failed to parse reply.");
929 dbus_message_iter_recurse(&iter, &sub);
931 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
932 log_error("Failed to parse reply.");
937 dbus_message_iter_get_basic(&sub, &state);
942 if (streq(state, "active") || startswith(state, "reloading"))
945 dbus_message_unref(m);
946 dbus_message_unref(reply);
952 dbus_message_unref(m);
955 dbus_message_unref(reply);
957 dbus_error_free(&error);
962 static int print_property(const char *name, DBusMessageIter *iter) {
966 if (arg_property && !streq(name, arg_property))
969 switch (dbus_message_iter_get_arg_type(iter)) {
971 case DBUS_TYPE_STRING: {
973 dbus_message_iter_get_basic(iter, &s);
976 printf("%s=%s\n", name, s);
981 case DBUS_TYPE_BOOLEAN: {
983 dbus_message_iter_get_basic(iter, &b);
984 printf("%s=%s\n", name, yes_no(b));
989 case DBUS_TYPE_UINT64: {
991 dbus_message_iter_get_basic(iter, &u);
993 /* Yes, heuristics! But we can change this check
994 * should it turn out to not be sufficient */
996 if (strstr(name, "Timestamp")) {
997 char timestamp[FORMAT_TIMESTAMP_MAX], *t;
999 if ((t = format_timestamp(timestamp, sizeof(timestamp), u)) || arg_all)
1000 printf("%s=%s\n", name, strempty(t));
1001 } else if (strstr(name, "USec")) {
1002 char timespan[FORMAT_TIMESPAN_MAX];
1004 printf("%s=%s\n", name, format_timespan(timespan, sizeof(timespan), u));
1006 printf("%s=%llu\n", name, (unsigned long long) u);
1011 case DBUS_TYPE_UINT32: {
1013 dbus_message_iter_get_basic(iter, &u);
1015 if (strstr(name, "UMask") || strstr(name, "Mode"))
1016 printf("%s=%04o\n", name, u);
1018 printf("%s=%u\n", name, (unsigned) u);
1023 case DBUS_TYPE_INT32: {
1025 dbus_message_iter_get_basic(iter, &i);
1027 printf("%s=%i\n", name, (int) i);
1031 case DBUS_TYPE_STRUCT: {
1032 DBusMessageIter sub;
1033 dbus_message_iter_recurse(iter, &sub);
1035 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "Job")) {
1038 dbus_message_iter_get_basic(&sub, &u);
1041 printf("%s=%u\n", name, (unsigned) u);
1043 printf("%s=\n", name);
1046 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Unit")) {
1049 dbus_message_iter_get_basic(&sub, &s);
1051 if (arg_all || s[0])
1052 printf("%s=%s\n", name, s);
1060 case DBUS_TYPE_ARRAY:
1062 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
1063 DBusMessageIter sub;
1066 dbus_message_iter_recurse(iter, &sub);
1069 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1070 printf("%s=", name);
1072 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1075 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
1076 dbus_message_iter_get_basic(&sub, &s);
1077 printf("%s%s", space ? " " : "", s);
1080 dbus_message_iter_next(&sub);
1087 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_BYTE) {
1088 DBusMessageIter sub;
1090 dbus_message_iter_recurse(iter, &sub);
1093 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1094 printf("%s=", name);
1096 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1099 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_BYTE);
1100 dbus_message_iter_get_basic(&sub, &u);
1103 dbus_message_iter_next(&sub);
1110 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Paths")) {
1111 DBusMessageIter sub, sub2;
1113 dbus_message_iter_recurse(iter, &sub);
1115 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1116 const char *type, *path;
1118 dbus_message_iter_recurse(&sub, &sub2);
1120 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
1121 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0)
1122 printf("%s=%s\n", type, path);
1124 dbus_message_iter_next(&sub);
1128 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Timers")) {
1129 DBusMessageIter sub, sub2;
1131 dbus_message_iter_recurse(iter, &sub);
1133 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1135 uint64_t value, next_elapse;
1137 dbus_message_iter_recurse(&sub, &sub2);
1139 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &base, true) >= 0 &&
1140 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &value, true) >= 0 &&
1141 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &next_elapse, false) >= 0) {
1142 char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX];
1144 printf("%s={ value=%s ; next_elapse=%s }\n",
1146 format_timespan(timespan1, sizeof(timespan1), value),
1147 format_timespan(timespan2, sizeof(timespan2), next_elapse));
1150 dbus_message_iter_next(&sub);
1154 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && startswith(name, "Exec")) {
1156 DBusMessageIter sub, sub2, sub3;
1158 dbus_message_iter_recurse(iter, &sub);
1160 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1162 uint64_t start_time, exit_time;
1164 int32_t code, status;
1166 dbus_message_iter_recurse(&sub, &sub2);
1168 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0)
1171 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_ARRAY ||
1172 dbus_message_iter_get_element_type(&sub2) != DBUS_TYPE_STRING)
1175 printf("%s={ path=%s ; argv[]=", name, path);
1177 dbus_message_iter_recurse(&sub2, &sub3);
1179 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1182 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1183 dbus_message_iter_get_basic(&sub3, &s);
1185 dbus_message_iter_next(&sub3);
1188 if (dbus_message_iter_next(&sub2) &&
1189 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_time, true) >= 0 &&
1190 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_time, true) >= 0 &&
1191 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, true) >= 0 &&
1192 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &code, true) >= 0 &&
1193 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &status, false) >= 0) {
1195 char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
1197 printf("; start=%s ; stop=%s ; pid=%u ; code=%s ; status=%i/%s",
1198 strna(format_timestamp(timestamp1, sizeof(timestamp1), start_time)),
1199 strna(format_timestamp(timestamp2, sizeof(timestamp2), exit_time)),
1201 sigchld_code_to_string(code),
1203 strna(code == CLD_EXITED ? NULL : strsignal(status)));
1208 dbus_message_iter_next(&sub);
1218 printf("%s=[unprintable]\n", name);
1223 static int show_one(DBusConnection *bus, const char *path) {
1224 DBusMessage *m = NULL, *reply = NULL;
1225 const char *interface = "";
1228 DBusMessageIter iter, sub, sub2, sub3;
1233 dbus_error_init(&error);
1235 if (!(m = dbus_message_new_method_call(
1236 "org.freedesktop.systemd1",
1238 "org.freedesktop.DBus.Properties",
1240 log_error("Could not allocate message.");
1245 if (!dbus_message_append_args(m,
1246 DBUS_TYPE_STRING, &interface,
1247 DBUS_TYPE_INVALID)) {
1248 log_error("Could not append arguments to message.");
1253 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1254 log_error("Failed to issue method call: %s", error.message);
1259 if (!dbus_message_iter_init(reply, &iter) ||
1260 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
1261 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
1262 log_error("Failed to parse reply.");
1267 dbus_message_iter_recurse(&iter, &sub);
1269 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1272 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
1273 log_error("Failed to parse reply.");
1278 dbus_message_iter_recurse(&sub, &sub2);
1280 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
1281 log_error("Failed to parse reply.");
1286 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
1287 log_error("Failed to parse reply.");
1292 dbus_message_iter_recurse(&sub2, &sub3);
1294 if (print_property(name, &sub3) < 0) {
1295 log_error("Failed to parse reply.");
1300 dbus_message_iter_next(&sub);
1307 dbus_message_unref(m);
1310 dbus_message_unref(reply);
1312 dbus_error_free(&error);
1317 static int show(DBusConnection *bus, char **args, unsigned n) {
1318 DBusMessage *m = NULL, *reply = NULL;
1326 dbus_error_init(&error);
1329 /* If not argument is specified inspect the manager
1332 r = show_one(bus, "/org/freedesktop/systemd1");
1336 for (i = 1; i < n; i++) {
1337 const char *path = NULL;
1340 if (safe_atou32(args[i], &id) < 0) {
1342 if (!(m = dbus_message_new_method_call(
1343 "org.freedesktop.systemd1",
1344 "/org/freedesktop/systemd1",
1345 "org.freedesktop.systemd1.Manager",
1347 log_error("Could not allocate message.");
1352 if (!dbus_message_append_args(m,
1353 DBUS_TYPE_STRING, &args[i],
1354 DBUS_TYPE_INVALID)) {
1355 log_error("Could not append arguments to message.");
1362 if (!(m = dbus_message_new_method_call(
1363 "org.freedesktop.systemd1",
1364 "/org/freedesktop/systemd1",
1365 "org.freedesktop.systemd1.Manager",
1367 log_error("Could not allocate message.");
1372 if (!dbus_message_append_args(m,
1373 DBUS_TYPE_UINT32, &id,
1374 DBUS_TYPE_INVALID)) {
1375 log_error("Could not append arguments to message.");
1381 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1382 log_error("Failed to issue method call: %s", error.message);
1387 if (!dbus_message_get_args(reply, &error,
1388 DBUS_TYPE_OBJECT_PATH, &path,
1389 DBUS_TYPE_INVALID)) {
1390 log_error("Failed to parse reply: %s", error.message);
1395 if ((r = show_one(bus, path)) < 0)
1398 dbus_message_unref(m);
1399 dbus_message_unref(reply);
1407 dbus_message_unref(m);
1410 dbus_message_unref(reply);
1412 dbus_error_free(&error);
1417 static DBusHandlerResult monitor_filter(DBusConnection *connection, DBusMessage *message, void *data) {
1419 DBusMessage *m = NULL, *reply = NULL;
1424 dbus_error_init(&error);
1426 /* log_debug("Got D-Bus request: %s.%s() on %s", */
1427 /* dbus_message_get_interface(message), */
1428 /* dbus_message_get_member(message), */
1429 /* dbus_message_get_path(message)); */
1431 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
1432 log_error("Warning! D-Bus connection terminated.");
1433 dbus_connection_close(connection);
1435 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitNew") ||
1436 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitRemoved")) {
1437 const char *id, *path;
1439 if (!dbus_message_get_args(message, &error,
1440 DBUS_TYPE_STRING, &id,
1441 DBUS_TYPE_OBJECT_PATH, &path,
1443 log_error("Failed to parse message: %s", error.message);
1444 else if (streq(dbus_message_get_member(message), "UnitNew"))
1445 printf("Unit %s added.\n", id);
1447 printf("Unit %s removed.\n", id);
1449 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobNew") ||
1450 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
1454 if (!dbus_message_get_args(message, &error,
1455 DBUS_TYPE_UINT32, &id,
1456 DBUS_TYPE_OBJECT_PATH, &path,
1458 log_error("Failed to parse message: %s", error.message);
1459 else if (streq(dbus_message_get_member(message), "JobNew"))
1460 printf("Job %u added.\n", id);
1462 printf("Job %u removed.\n", id);
1465 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Unit", "Changed") ||
1466 dbus_message_is_signal(message, "org.freedesktop.systemd1.Job", "Changed")) {
1468 const char *path, *interface, *property = "Id";
1469 DBusMessageIter iter, sub;
1471 path = dbus_message_get_path(message);
1472 interface = dbus_message_get_interface(message);
1474 if (!(m = dbus_message_new_method_call(
1475 "org.freedesktop.systemd1",
1477 "org.freedesktop.DBus.Properties",
1479 log_error("Could not allocate message.");
1483 if (!dbus_message_append_args(m,
1484 DBUS_TYPE_STRING, &interface,
1485 DBUS_TYPE_STRING, &property,
1486 DBUS_TYPE_INVALID)) {
1487 log_error("Could not append arguments to message.");
1491 if (!(reply = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) {
1492 log_error("Failed to issue method call: %s", error.message);
1496 if (!dbus_message_iter_init(reply, &iter) ||
1497 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1498 log_error("Failed to parse reply.");
1502 dbus_message_iter_recurse(&iter, &sub);
1504 if (streq(interface, "org.freedesktop.systemd1.Unit")) {
1507 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1508 log_error("Failed to parse reply.");
1512 dbus_message_iter_get_basic(&sub, &id);
1513 printf("Unit %s changed.\n", id);
1517 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT32) {
1518 log_error("Failed to parse reply.");
1522 dbus_message_iter_get_basic(&sub, &id);
1523 printf("Job %u changed.\n", id);
1529 dbus_message_unref(m);
1532 dbus_message_unref(reply);
1534 dbus_error_free(&error);
1535 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1539 dbus_message_unref(m);
1542 dbus_message_unref(reply);
1544 dbus_error_free(&error);
1545 return DBUS_HANDLER_RESULT_NEED_MEMORY;
1548 static int monitor(DBusConnection *bus, char **args, unsigned n) {
1549 DBusMessage *m = NULL, *reply = NULL;
1553 dbus_error_init(&error);
1555 dbus_bus_add_match(bus,
1557 "sender='org.freedesktop.systemd1',"
1558 "interface='org.freedesktop.systemd1.Manager',"
1559 "path='/org/freedesktop/systemd1'",
1562 if (dbus_error_is_set(&error)) {
1563 log_error("Failed to add match: %s", error.message);
1568 dbus_bus_add_match(bus,
1570 "sender='org.freedesktop.systemd1',"
1571 "interface='org.freedesktop.systemd1.Unit',"
1575 if (dbus_error_is_set(&error)) {
1576 log_error("Failed to add match: %s", error.message);
1581 dbus_bus_add_match(bus,
1583 "sender='org.freedesktop.systemd1',"
1584 "interface='org.freedesktop.systemd1.Job',"
1588 if (dbus_error_is_set(&error)) {
1589 log_error("Failed to add match: %s", error.message);
1594 if (!dbus_connection_add_filter(bus, monitor_filter, NULL, NULL)) {
1595 log_error("Failed to add filter.");
1600 if (!(m = dbus_message_new_method_call(
1601 "org.freedesktop.systemd1",
1602 "/org/freedesktop/systemd1",
1603 "org.freedesktop.systemd1.Manager",
1605 log_error("Could not allocate message.");
1610 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1611 log_error("Failed to issue method call: %s", error.message);
1616 while (dbus_connection_read_write_dispatch(bus, -1))
1623 /* This is slightly dirty, since we don't undo the filter or the matches. */
1626 dbus_message_unref(m);
1629 dbus_message_unref(reply);
1631 dbus_error_free(&error);
1636 static int dump(DBusConnection *bus, char **args, unsigned n) {
1637 DBusMessage *m = NULL, *reply = NULL;
1642 dbus_error_init(&error);
1644 if (!(m = dbus_message_new_method_call(
1645 "org.freedesktop.systemd1",
1646 "/org/freedesktop/systemd1",
1647 "org.freedesktop.systemd1.Manager",
1649 log_error("Could not allocate message.");
1653 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1654 log_error("Failed to issue method call: %s", error.message);
1659 if (!dbus_message_get_args(reply, &error,
1660 DBUS_TYPE_STRING, &text,
1661 DBUS_TYPE_INVALID)) {
1662 log_error("Failed to parse reply: %s", error.message);
1667 fputs(text, stdout);
1673 dbus_message_unref(m);
1676 dbus_message_unref(reply);
1678 dbus_error_free(&error);
1683 static int snapshot(DBusConnection *bus, char **args, unsigned n) {
1684 DBusMessage *m = NULL, *reply = NULL;
1687 const char *name = "", *path, *id;
1688 dbus_bool_t cleanup = FALSE;
1689 DBusMessageIter iter, sub;
1691 *interface = "org.freedesktop.systemd1.Unit",
1694 dbus_error_init(&error);
1696 if (!(m = dbus_message_new_method_call(
1697 "org.freedesktop.systemd1",
1698 "/org/freedesktop/systemd1",
1699 "org.freedesktop.systemd1.Manager",
1700 "CreateSnapshot"))) {
1701 log_error("Could not allocate message.");
1708 if (!dbus_message_append_args(m,
1709 DBUS_TYPE_STRING, &name,
1710 DBUS_TYPE_BOOLEAN, &cleanup,
1711 DBUS_TYPE_INVALID)) {
1712 log_error("Could not append arguments to message.");
1717 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1718 log_error("Failed to issue method call: %s", error.message);
1723 if (!dbus_message_get_args(reply, &error,
1724 DBUS_TYPE_OBJECT_PATH, &path,
1725 DBUS_TYPE_INVALID)) {
1726 log_error("Failed to parse reply: %s", error.message);
1731 dbus_message_unref(m);
1732 if (!(m = dbus_message_new_method_call(
1733 "org.freedesktop.systemd1",
1735 "org.freedesktop.DBus.Properties",
1737 log_error("Could not allocate message.");
1741 if (!dbus_message_append_args(m,
1742 DBUS_TYPE_STRING, &interface,
1743 DBUS_TYPE_STRING, &property,
1744 DBUS_TYPE_INVALID)) {
1745 log_error("Could not append arguments to message.");
1750 dbus_message_unref(reply);
1751 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1752 log_error("Failed to issue method call: %s", error.message);
1757 if (!dbus_message_iter_init(reply, &iter) ||
1758 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1759 log_error("Failed to parse reply.");
1764 dbus_message_iter_recurse(&iter, &sub);
1766 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1767 log_error("Failed to parse reply.");
1772 dbus_message_iter_get_basic(&sub, &id);
1780 dbus_message_unref(m);
1783 dbus_message_unref(reply);
1785 dbus_error_free(&error);
1790 static int delete_snapshot(DBusConnection *bus, char **args, unsigned n) {
1791 DBusMessage *m = NULL, *reply = NULL;
1799 dbus_error_init(&error);
1801 for (i = 1; i < n; i++) {
1802 const char *path = NULL;
1804 if (!(m = dbus_message_new_method_call(
1805 "org.freedesktop.systemd1",
1806 "/org/freedesktop/systemd1",
1807 "org.freedesktop.systemd1.Manager",
1809 log_error("Could not allocate message.");
1814 if (!dbus_message_append_args(m,
1815 DBUS_TYPE_STRING, &args[i],
1816 DBUS_TYPE_INVALID)) {
1817 log_error("Could not append arguments to message.");
1822 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1823 log_error("Failed to issue method call: %s", error.message);
1828 if (!dbus_message_get_args(reply, &error,
1829 DBUS_TYPE_OBJECT_PATH, &path,
1830 DBUS_TYPE_INVALID)) {
1831 log_error("Failed to parse reply: %s", error.message);
1836 dbus_message_unref(m);
1837 if (!(m = dbus_message_new_method_call(
1838 "org.freedesktop.systemd1",
1840 "org.freedesktop.systemd1.Snapshot",
1842 log_error("Could not allocate message.");
1847 dbus_message_unref(reply);
1848 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1849 log_error("Failed to issue method call: %s", error.message);
1854 dbus_message_unref(m);
1855 dbus_message_unref(reply);
1863 dbus_message_unref(m);
1866 dbus_message_unref(reply);
1868 dbus_error_free(&error);
1873 static int clear_jobs(DBusConnection *bus, char **args, unsigned n) {
1874 DBusMessage *m = NULL, *reply = NULL;
1879 dbus_error_init(&error);
1881 if (arg_action == ACTION_RELOAD)
1883 else if (arg_action == ACTION_REEXEC)
1884 method = "Reexecute";
1886 assert(arg_action == ACTION_SYSTEMCTL);
1889 streq(args[0], "clear-jobs") ? "ClearJobs" :
1890 streq(args[0], "daemon-reload") ? "Reload" :
1891 streq(args[0], "daemon-reexec") ? "Reexecute" :
1895 if (!(m = dbus_message_new_method_call(
1896 "org.freedesktop.systemd1",
1897 "/org/freedesktop/systemd1",
1898 "org.freedesktop.systemd1.Manager",
1900 log_error("Could not allocate message.");
1904 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1906 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
1907 /* There's always a fallback possible for
1908 * legacy actions. */
1913 log_error("Failed to issue method call: %s", error.message);
1922 dbus_message_unref(m);
1925 dbus_message_unref(reply);
1927 dbus_error_free(&error);
1932 static int show_enviroment(DBusConnection *bus, char **args, unsigned n) {
1933 DBusMessage *m = NULL, *reply = NULL;
1935 DBusMessageIter iter, sub, sub2;
1938 *interface = "org.freedesktop.systemd1.Manager",
1939 *property = "Environment";
1941 dbus_error_init(&error);
1943 if (!(m = dbus_message_new_method_call(
1944 "org.freedesktop.systemd1",
1945 "/org/freedesktop/systemd1",
1946 "org.freedesktop.DBus.Properties",
1948 log_error("Could not allocate message.");
1952 if (!dbus_message_append_args(m,
1953 DBUS_TYPE_STRING, &interface,
1954 DBUS_TYPE_STRING, &property,
1955 DBUS_TYPE_INVALID)) {
1956 log_error("Could not append arguments to message.");
1961 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1962 log_error("Failed to issue method call: %s", error.message);
1967 if (!dbus_message_iter_init(reply, &iter) ||
1968 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1969 log_error("Failed to parse reply.");
1974 dbus_message_iter_recurse(&iter, &sub);
1976 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
1977 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
1978 log_error("Failed to parse reply.");
1983 dbus_message_iter_recurse(&sub, &sub2);
1985 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
1988 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
1989 log_error("Failed to parse reply.");
1994 dbus_message_iter_get_basic(&sub2, &text);
1995 printf("%s\n", text);
1997 dbus_message_iter_next(&sub2);
2004 dbus_message_unref(m);
2007 dbus_message_unref(reply);
2009 dbus_error_free(&error);
2014 static int set_environment(DBusConnection *bus, char **args, unsigned n) {
2015 DBusMessage *m = NULL, *reply = NULL;
2019 DBusMessageIter iter, sub;
2022 dbus_error_init(&error);
2024 method = streq(args[0], "set-environment")
2026 : "UnsetEnvironment";
2028 if (!(m = dbus_message_new_method_call(
2029 "org.freedesktop.systemd1",
2030 "/org/freedesktop/systemd1",
2031 "org.freedesktop.systemd1.Manager",
2034 log_error("Could not allocate message.");
2038 dbus_message_iter_init_append(m, &iter);
2040 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
2041 log_error("Could not append arguments to message.");
2046 for (i = 1; i < n; i++)
2047 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &args[i])) {
2048 log_error("Could not append arguments to message.");
2053 if (!dbus_message_iter_close_container(&iter, &sub)) {
2054 log_error("Could not append arguments to 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);
2069 dbus_message_unref(m);
2072 dbus_message_unref(reply);
2074 dbus_error_free(&error);
2079 static int systemctl_help(void) {
2081 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
2082 "Send control commands to the systemd manager.\n\n"
2083 " -h --help Show this help\n"
2084 " -t --type=TYPE List only units of a particular type\n"
2085 " -p --property=NAME Show only properties by this name\n"
2086 " -a --all Show all units/properties, including dead/empty ones\n"
2087 " --replace When installing a new job, replace existing conflicting ones\n"
2088 " --system Connect to system bus\n"
2089 " --session Connect to session bus\n"
2090 " -q --quiet Suppress output\n"
2091 " --no-block Do not wait until operation finished\n"
2092 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
2094 " list-units List units\n"
2095 " start [NAME...] Start one or more units\n"
2096 " stop [NAME...] Stop one or more units\n"
2097 " restart [NAME...] Restart one or more units\n"
2098 " reload [NAME...] Reload one or more units\n"
2099 " isolate [NAME] Start one unit and stop all others\n"
2100 " check [NAME...] Check whether any of the passed units are active\n"
2101 " show [NAME...|JOB...] Show information about one or more units/jobs/manager\n"
2102 " load [NAME...] Load one or more units\n"
2103 " list-jobs List jobs\n"
2104 " cancel [JOB...] Cancel one or more jobs\n"
2105 " clear-jobs Cancel all jobs\n"
2106 " monitor Monitor unit/job changes\n"
2107 " dump Dump server status\n"
2108 " snapshot [NAME] Create a snapshot\n"
2109 " delete [NAME...] Remove one or more snapshots\n"
2110 " daemon-reload Reload systemd manager configuration\n"
2111 " daemon-reexec Reexecute systemd manager\n"
2112 " daemon-exit Ask the systemd manager to quit\n"
2113 " show-environment Dump environment\n"
2114 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
2115 " unset-environment [NAME...] Unset one or more environment variables\n"
2116 " halt Shut down and halt the system\n"
2117 " poweroff Shut down and power-off the system\n"
2118 " reboot Shut down and reboot the system\n"
2119 " default Enter default mode\n"
2120 " rescue Enter rescue mode\n"
2121 " emergency Enter emergency mode\n",
2122 program_invocation_short_name);
2127 static int halt_help(void) {
2129 printf("%s [OPTIONS...]\n\n"
2130 "%s the system.\n\n"
2131 " --help Show this help\n"
2132 " --halt Halt the machine\n"
2133 " -p --poweroff Switch off the machine\n"
2134 " --reboot Reboot the machine\n"
2135 " -f --force Force immediate halt/power-off/reboot\n"
2136 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
2137 " -d --no-wtmp Don't write wtmp record\n"
2138 " -n --no-sync Don't sync before halt/power-off/reboot\n"
2139 " --no-wall Don't send wall message before halt/power-off/reboot\n",
2140 program_invocation_short_name,
2141 arg_action == ACTION_REBOOT ? "Reboot" :
2142 arg_action == ACTION_POWEROFF ? "Power off" :
2148 static int shutdown_help(void) {
2150 printf("%s [OPTIONS...] [now] [WALL...]\n\n"
2151 "Shut down the system.\n\n"
2152 " --help Show this help\n"
2153 " -H --halt Halt the machine\n"
2154 " -P --poweroff Power-off the machine\n"
2155 " -r --reboot Reboot the machine\n"
2156 " -h Equivalent to --poweroff, overriden by --halt\n"
2157 " -k Don't halt/power-off/reboot, just send warnings\n"
2158 " --no-wall Don't send wall message before halt/power-off/reboot\n",
2159 program_invocation_short_name);
2164 static int telinit_help(void) {
2166 printf("%s [OPTIONS...] {COMMAND}\n\n"
2167 "Send control commands to the init daemon.\n\n"
2168 " --help Show this help\n"
2169 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
2171 " 0 Power-off the machine\n"
2172 " 6 Reboot the machine\n"
2173 " 2, 3, 4, 5 Start runlevelX.target unit\n"
2174 " 1, s, S Enter rescue mode\n"
2175 " q, Q Reload init daemon configuration\n"
2176 " u, U Reexecute init daemon\n",
2177 program_invocation_short_name);
2182 static int runlevel_help(void) {
2184 printf("%s [OPTIONS...]\n\n"
2185 "Prints the previous and current runlevel of the init system.\n\n"
2186 " --help Show this help\n",
2187 program_invocation_short_name);
2192 static int systemctl_parse_argv(int argc, char *argv[]) {
2195 ARG_REPLACE = 0x100,
2202 static const struct option options[] = {
2203 { "help", no_argument, NULL, 'h' },
2204 { "type", required_argument, NULL, 't' },
2205 { "property", required_argument, NULL, 'p' },
2206 { "all", no_argument, NULL, 'a' },
2207 { "replace", no_argument, NULL, ARG_REPLACE },
2208 { "session", no_argument, NULL, ARG_SESSION },
2209 { "system", no_argument, NULL, ARG_SYSTEM },
2210 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
2211 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2212 { "quiet", no_argument, NULL, 'q' },
2213 { NULL, 0, NULL, 0 }
2221 while ((c = getopt_long(argc, argv, "ht:p:aq", options, NULL)) >= 0) {
2234 arg_property = optarg;
2236 /* If the user asked for a particular
2237 * property, show it to him, even if it is
2255 arg_session = false;
2259 arg_no_block = true;
2274 log_error("Unknown option code %c", c);
2282 static int halt_parse_argv(int argc, char *argv[]) {
2291 static const struct option options[] = {
2292 { "help", no_argument, NULL, ARG_HELP },
2293 { "halt", no_argument, NULL, ARG_HALT },
2294 { "poweroff", no_argument, NULL, 'p' },
2295 { "reboot", no_argument, NULL, ARG_REBOOT },
2296 { "force", no_argument, NULL, 'f' },
2297 { "wtmp-only", no_argument, NULL, 'w' },
2298 { "no-wtmp", no_argument, NULL, 'd' },
2299 { "no-sync", no_argument, NULL, 'n' },
2300 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2301 { NULL, 0, NULL, 0 }
2309 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
2310 if (runlevel == '0' || runlevel == '6')
2311 arg_immediate = true;
2313 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
2321 arg_action = ACTION_HALT;
2325 arg_action = ACTION_POWEROFF;
2329 arg_action = ACTION_REBOOT;
2333 arg_immediate = true;
2354 /* Compatibility nops */
2361 log_error("Unknown option code %c", c);
2366 if (optind < argc) {
2367 log_error("Too many arguments.");
2374 static int shutdown_parse_argv(int argc, char *argv[]) {
2381 static const struct option options[] = {
2382 { "help", no_argument, NULL, ARG_HELP },
2383 { "halt", no_argument, NULL, 'H' },
2384 { "poweroff", no_argument, NULL, 'P' },
2385 { "reboot", no_argument, NULL, 'r' },
2386 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2387 { NULL, 0, NULL, 0 }
2395 while ((c = getopt_long(argc, argv, "HPrhkt:a", options, NULL)) >= 0) {
2403 arg_action = ACTION_HALT;
2407 arg_action = ACTION_POWEROFF;
2411 arg_action = ACTION_REBOOT;
2415 if (arg_action != ACTION_HALT)
2416 arg_action = ACTION_POWEROFF;
2429 /* Compatibility nops */
2436 log_error("Unknown option code %c", c);
2441 if (argc > optind && !streq(argv[optind], "now"))
2442 log_warning("First argument '%s' isn't 'now'. Ignoring.", argv[optind]);
2444 /* We ignore the time argument */
2445 if (argc > optind + 1)
2446 arg_wall = argv + optind + 1;
2453 static int telinit_parse_argv(int argc, char *argv[]) {
2460 static const struct option options[] = {
2461 { "help", no_argument, NULL, ARG_HELP },
2462 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2463 { NULL, 0, NULL, 0 }
2466 static const struct {
2470 { '0', ACTION_POWEROFF },
2471 { '6', ACTION_REBOOT },
2472 { '1', ACTION_RESCUE },
2473 { '2', ACTION_RUNLEVEL2 },
2474 { '3', ACTION_RUNLEVEL3 },
2475 { '4', ACTION_RUNLEVEL4 },
2476 { '5', ACTION_RUNLEVEL5 },
2477 { 's', ACTION_RESCUE },
2478 { 'S', ACTION_RESCUE },
2479 { 'q', ACTION_RELOAD },
2480 { 'Q', ACTION_RELOAD },
2481 { 'u', ACTION_REEXEC },
2482 { 'U', ACTION_REEXEC }
2491 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
2506 log_error("Unknown option code %c", c);
2511 if (optind >= argc) {
2516 if (optind + 1 < argc) {
2517 log_error("Too many arguments.");
2521 if (strlen(argv[optind]) != 1) {
2522 log_error("Expected single character argument.");
2526 for (i = 0; i < ELEMENTSOF(table); i++)
2527 if (table[i].from == argv[optind][0])
2530 if (i >= ELEMENTSOF(table)) {
2531 log_error("Unknown command %s.", argv[optind]);
2535 arg_action = table[i].to;
2542 static int runlevel_parse_argv(int argc, char *argv[]) {
2548 static const struct option options[] = {
2549 { "help", no_argument, NULL, ARG_HELP },
2550 { NULL, 0, NULL, 0 }
2558 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
2569 log_error("Unknown option code %c", c);
2574 if (optind < argc) {
2575 log_error("Too many arguments.");
2582 static int parse_argv(int argc, char *argv[]) {
2586 if (program_invocation_short_name) {
2588 if (strstr(program_invocation_short_name, "halt")) {
2589 arg_action = ACTION_HALT;
2590 return halt_parse_argv(argc, argv);
2591 } else if (strstr(program_invocation_short_name, "poweroff")) {
2592 arg_action = ACTION_POWEROFF;
2593 return halt_parse_argv(argc, argv);
2594 } else if (strstr(program_invocation_short_name, "reboot")) {
2595 arg_action = ACTION_REBOOT;
2596 return halt_parse_argv(argc, argv);
2597 } else if (strstr(program_invocation_short_name, "shutdown")) {
2598 arg_action = ACTION_POWEROFF;
2599 return shutdown_parse_argv(argc, argv);
2600 } else if (strstr(program_invocation_short_name, "init")) {
2601 arg_action = ACTION_INVALID;
2602 return telinit_parse_argv(argc, argv);
2603 } else if (strstr(program_invocation_short_name, "runlevel")) {
2604 arg_action = ACTION_RUNLEVEL;
2605 return runlevel_parse_argv(argc, argv);
2609 arg_action = ACTION_SYSTEMCTL;
2610 return systemctl_parse_argv(argc, argv);
2613 static int action_to_runlevel(void) {
2615 static const char table[_ACTION_MAX] = {
2616 [ACTION_HALT] = '0',
2617 [ACTION_POWEROFF] = '0',
2618 [ACTION_REBOOT] = '6',
2619 [ACTION_RUNLEVEL2] = '2',
2620 [ACTION_RUNLEVEL3] = '3',
2621 [ACTION_RUNLEVEL4] = '4',
2622 [ACTION_RUNLEVEL5] = '5',
2623 [ACTION_RESCUE] = '1'
2626 assert(arg_action < _ACTION_MAX);
2628 return table[arg_action];
2631 static int talk_upstart(void) {
2632 DBusMessage *m = NULL, *reply = NULL;
2634 int previous, rl, r;
2636 env1_buf[] = "RUNLEVEL=X",
2637 env2_buf[] = "PREVLEVEL=X";
2638 char *env1 = env1_buf, *env2 = env2_buf;
2639 const char *emit = "runlevel";
2640 dbus_bool_t b_false = FALSE;
2641 DBusMessageIter iter, sub;
2642 DBusConnection *bus;
2644 dbus_error_init(&error);
2646 if (!(rl = action_to_runlevel()))
2649 if (utmp_get_runlevel(&previous, NULL) < 0)
2652 if (!(bus = dbus_connection_open("unix:abstract=/com/ubuntu/upstart", &error))) {
2653 if (dbus_error_has_name(&error, DBUS_ERROR_NO_SERVER)) {
2658 log_error("Failed to connect to Upstart bus: %s", error.message);
2663 if ((r = bus_check_peercred(bus)) < 0) {
2664 log_error("Failed to verify owner of bus.");
2668 if (!(m = dbus_message_new_method_call(
2669 "com.ubuntu.Upstart",
2670 "/com/ubuntu/Upstart",
2671 "com.ubuntu.Upstart0_6",
2674 log_error("Could not allocate message.");
2679 dbus_message_iter_init_append(m, &iter);
2681 env1_buf[sizeof(env1_buf)-2] = rl;
2682 env2_buf[sizeof(env2_buf)-2] = previous;
2684 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
2685 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
2686 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
2687 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
2688 !dbus_message_iter_close_container(&iter, &sub) ||
2689 !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
2690 log_error("Could not append arguments to message.");
2695 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2697 if (error_is_no_service(&error)) {
2702 log_error("Failed to issue method call: %s", error.message);
2711 dbus_message_unref(m);
2714 dbus_message_unref(reply);
2717 dbus_connection_unref(bus);
2719 dbus_error_free(&error);
2724 static int talk_initctl(void) {
2725 struct init_request request;
2729 if (!(rl = action_to_runlevel()))
2733 request.magic = INIT_MAGIC;
2734 request.sleeptime = 0;
2735 request.cmd = INIT_CMD_RUNLVL;
2736 request.runlevel = rl;
2738 if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
2740 if (errno == ENOENT)
2743 log_error("Failed to open "INIT_FIFO": %m");
2748 r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
2749 close_nointr_nofail(fd);
2752 log_error("Failed to write to "INIT_FIFO": %m");
2753 return errno ? -errno : -EIO;
2759 static int systemctl_main(DBusConnection *bus, int argc, char *argv[]) {
2761 static const struct {
2769 int (* const dispatch)(DBusConnection *bus, char **args, unsigned n);
2771 { "list-units", LESS, 1, list_units },
2772 { "list-jobs", EQUAL, 1, list_jobs },
2773 { "clear-jobs", EQUAL, 1, clear_jobs },
2774 { "load", MORE, 2, load_unit },
2775 { "cancel", MORE, 2, cancel_job },
2776 { "start", MORE, 2, start_unit },
2777 { "stop", MORE, 2, start_unit },
2778 { "reload", MORE, 2, start_unit },
2779 { "restart", MORE, 2, start_unit },
2780 { "isolate", EQUAL, 2, start_unit },
2781 { "check", MORE, 2, check_unit },
2782 { "show", MORE, 1, show },
2783 { "monitor", EQUAL, 1, monitor },
2784 { "dump", EQUAL, 1, dump },
2785 { "snapshot", LESS, 2, snapshot },
2786 { "delete", MORE, 2, delete_snapshot },
2787 { "daemon-reload", EQUAL, 1, clear_jobs },
2788 { "daemon-reexec", EQUAL, 1, clear_jobs },
2789 { "daemon-exit", EQUAL, 1, clear_jobs },
2790 { "show-environment", EQUAL, 1, show_enviroment },
2791 { "set-environment", MORE, 2, set_environment },
2792 { "unset-environment", MORE, 2, set_environment },
2793 { "halt", EQUAL, 1, start_special },
2794 { "poweroff", EQUAL, 1, start_special },
2795 { "reboot", EQUAL, 1, start_special },
2796 { "default", EQUAL, 1, start_special },
2797 { "rescue", EQUAL, 1, start_special },
2798 { "emergency", EQUAL, 1, start_special },
2808 left = argc - optind;
2811 /* Special rule: no arguments means "list-units" */
2814 if (streq(argv[optind], "help")) {
2819 for (i = 0; i < ELEMENTSOF(verbs); i++)
2820 if (streq(argv[optind], verbs[i].verb))
2823 if (i >= ELEMENTSOF(verbs)) {
2824 log_error("Unknown operation %s", argv[optind]);
2829 switch (verbs[i].argc_cmp) {
2832 if (left != verbs[i].argc) {
2833 log_error("Invalid number of arguments.");
2840 if (left < verbs[i].argc) {
2841 log_error("Too few arguments.");
2848 if (left > verbs[i].argc) {
2849 log_error("Too many arguments.");
2856 assert_not_reached("Unknown comparison operator.");
2859 return verbs[i].dispatch(bus, argv + optind, left);
2862 static int reload_with_fallback(DBusConnection *bus) {
2866 /* First, try systemd via D-Bus. */
2867 if ((r = clear_jobs(bus, NULL, 0)) > 0)
2871 /* Nothing else worked, so let's try signals */
2872 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
2874 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
2875 log_error("kill() failed: %m");
2882 static int start_with_fallback(DBusConnection *bus) {
2885 warn_wall(arg_action);
2888 /* First, try systemd via D-Bus. */
2889 if ((r = start_unit(bus, NULL, 0)) > 0)
2892 /* Hmm, talking to systemd via D-Bus didn't work. Then
2893 * let's try to talk to Upstart via D-Bus. */
2894 if ((r = talk_upstart()) > 0)
2898 /* Nothing else worked, so let's try
2900 if ((r = talk_initctl()) != 0)
2903 log_error("Failed to talk to init daemon.");
2907 static int halt_main(DBusConnection *bus) {
2911 return start_with_fallback(bus);
2914 if ((r = utmp_put_shutdown(0)) < 0)
2915 log_warning("Failed to write utmp record: %s", strerror(-r));
2923 /* Make sure C-A-D is handled by the kernel from this
2925 reboot(RB_ENABLE_CAD);
2927 switch (arg_action) {
2930 log_info("Halting");
2931 reboot(RB_HALT_SYSTEM);
2934 case ACTION_POWEROFF:
2935 log_info("Powering off");
2936 reboot(RB_POWER_OFF);
2940 log_info("Rebooting");
2941 reboot(RB_AUTOBOOT);
2945 assert_not_reached("Unknown halt action.");
2948 /* We should never reach this. */
2952 static int runlevel_main(void) {
2953 int r, runlevel, previous;
2955 if ((r = utmp_get_runlevel(&runlevel, &previous)) < 0) {
2961 previous <= 0 ? 'N' : previous,
2962 runlevel <= 0 ? 'N' : runlevel);
2967 int main(int argc, char*argv[]) {
2969 DBusConnection *bus = NULL;
2972 dbus_error_init(&error);
2974 log_parse_environment();
2976 if ((r = parse_argv(argc, argv)) < 0)
2983 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
2984 * let's shortcut this */
2985 if (arg_action == ACTION_RUNLEVEL) {
2986 retval = runlevel_main() < 0;
2990 /* If we are root, then let's not go via the bus */
2991 if (geteuid() == 0 && !arg_session) {
2992 bus = dbus_connection_open("unix:abstract=/org/freedesktop/systemd1/private", &error);
2994 if (bus && bus_check_peercred(bus) < 0) {
2995 log_error("Failed to verify owner of bus.");
2999 bus = dbus_bus_get(arg_session ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &error);
3002 dbus_connection_set_exit_on_disconnect(bus, FALSE);
3004 switch (arg_action) {
3006 case ACTION_SYSTEMCTL: {
3009 log_error("Failed to get D-Bus connection: %s", error.message);
3013 retval = systemctl_main(bus, argc, argv) < 0;
3018 case ACTION_POWEROFF:
3020 retval = halt_main(bus) < 0;
3023 case ACTION_RUNLEVEL2:
3024 case ACTION_RUNLEVEL3:
3025 case ACTION_RUNLEVEL4:
3026 case ACTION_RUNLEVEL5:
3028 case ACTION_EMERGENCY:
3029 case ACTION_DEFAULT:
3030 retval = start_with_fallback(bus) < 0;
3035 retval = reload_with_fallback(bus) < 0;
3038 case ACTION_INVALID:
3039 case ACTION_RUNLEVEL:
3041 assert_not_reached("Unknown action");
3047 dbus_connection_unref(bus);
3049 dbus_error_free(&error);