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 <dbus/dbus.h>
38 #include "utmp-wtmp.h"
40 static const char *arg_type = NULL;
41 static bool arg_all = false;
42 static bool arg_replace = false;
43 static bool arg_session = false;
44 static bool arg_block = false;
45 static bool arg_immediate = false;
46 static bool arg_no_wtmp = false;
47 static bool arg_no_sync = false;
48 static bool arg_dry = false;
49 static char **arg_wall = NULL;
66 } arg_action = ACTION_SYSTEMCTL;
68 static bool error_is_no_service(DBusError *error) {
70 if (!dbus_error_is_set(error))
73 if (dbus_error_has_name(error, DBUS_ERROR_NAME_HAS_NO_OWNER))
76 if (dbus_error_has_name(error, DBUS_ERROR_SERVICE_UNKNOWN))
79 return startswith(error->name, "org.freedesktop.DBus.Error.Spawn.");
82 static int bus_iter_get_basic_and_next(DBusMessageIter *iter, int type, void *data, bool next) {
84 if (dbus_message_iter_get_arg_type(iter) != type)
87 dbus_message_iter_get_basic(iter, data);
89 if (!dbus_message_iter_next(iter) != !next)
95 static int columns(void) {
96 static int parsed_columns = 0;
99 if (parsed_columns > 0)
100 return parsed_columns;
102 if ((e = getenv("COLUMNS")))
103 parsed_columns = atoi(e);
105 if (parsed_columns <= 0) {
109 if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) >= 0)
110 parsed_columns = ws.ws_col;
113 if (parsed_columns <= 0)
116 return parsed_columns;
120 static int list_units(DBusConnection *bus, char **args, unsigned n) {
121 DBusMessage *m = NULL, *reply = NULL;
124 DBusMessageIter iter, sub, sub2;
127 dbus_error_init(&error);
129 if (!(m = dbus_message_new_method_call(
130 "org.freedesktop.systemd1",
131 "/org/freedesktop/systemd1",
132 "org.freedesktop.systemd1.Manager",
134 log_error("Could not allocate message.");
138 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
139 log_error("Failed to issue method call: %s", error.message);
144 if (!dbus_message_iter_init(reply, &iter) ||
145 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
146 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
147 log_error("Failed to parse reply.");
152 dbus_message_iter_recurse(&iter, &sub);
154 printf("%-45s %-6s %-12s %-12s %-15s %s\n", "UNIT", "LOAD", "ACTIVE", "SUB", "JOB", "DESCRIPTION");
156 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
157 const char *id, *description, *load_state, *active_state, *sub_state, *unit_state, *job_type, *job_path, *dot;
160 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
161 log_error("Failed to parse reply.");
166 dbus_message_iter_recurse(&sub, &sub2);
168 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
169 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &description, true) < 0 ||
170 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &load_state, true) < 0 ||
171 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &active_state, true) < 0 ||
172 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &sub_state, true) < 0 ||
173 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_state, true) < 0 ||
174 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &job_id, true) < 0 ||
175 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &job_type, true) < 0 ||
176 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, false) < 0) {
177 log_error("Failed to parse reply.");
182 if ((!arg_type || ((dot = strrchr(id, '.')) &&
183 streq(dot+1, arg_type))) &&
184 (arg_all || !streq(active_state, "inactive"))) {
188 printf("%-45s %-6s %-12s %-12s%n", id, load_state, active_state, sub_state, &a);
191 printf(" %-15s%n", job_type, &b);
195 if (a + b + 2 < columns()) {
199 printf("%.*s", columns() - a - b - 2, description);
206 dbus_message_iter_next(&sub);
210 printf("\n%u units listed.\n", k);
212 printf("\n%u live units listed. Pass --all to see dead units, too.\n", k);
218 dbus_message_unref(m);
221 dbus_message_unref(reply);
223 dbus_error_free(&error);
228 static int list_jobs(DBusConnection *bus, char **args, unsigned n) {
229 DBusMessage *m = NULL, *reply = NULL;
232 DBusMessageIter iter, sub, sub2;
235 dbus_error_init(&error);
237 if (!(m = dbus_message_new_method_call(
238 "org.freedesktop.systemd1",
239 "/org/freedesktop/systemd1",
240 "org.freedesktop.systemd1.Manager",
242 log_error("Could not allocate message.");
246 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
247 log_error("Failed to issue method call: %s", error.message);
252 if (!dbus_message_iter_init(reply, &iter) ||
253 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
254 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
255 log_error("Failed to parse reply.");
260 dbus_message_iter_recurse(&iter, &sub);
262 printf("%4s %-45s %-17s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
264 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
265 const char *name, *type, *state, *job_path, *unit_path;
268 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
269 log_error("Failed to parse reply.");
274 dbus_message_iter_recurse(&sub, &sub2);
276 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &id, true) < 0 ||
277 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0 ||
278 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
279 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, true) < 0 ||
280 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, true) < 0 ||
281 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, false) < 0) {
282 log_error("Failed to parse reply.");
287 printf("%4u %-45s %-17s %-7s\n", id, name, type, state);
290 dbus_message_iter_next(&sub);
293 printf("\n%u jobs listed.\n", k);
298 dbus_message_unref(m);
301 dbus_message_unref(reply);
303 dbus_error_free(&error);
308 static int load_unit(DBusConnection *bus, char **args, unsigned n) {
309 DBusMessage *m = NULL, *reply = NULL;
314 dbus_error_init(&error);
316 for (i = 1; i < n; i++) {
318 if (!(m = dbus_message_new_method_call(
319 "org.freedesktop.systemd1",
320 "/org/freedesktop/systemd1",
321 "org.freedesktop.systemd1.Manager",
323 log_error("Could not allocate message.");
328 if (!dbus_message_append_args(m,
329 DBUS_TYPE_STRING, &args[i],
330 DBUS_TYPE_INVALID)) {
331 log_error("Could not append arguments to message.");
336 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
337 log_error("Failed to issue method call: %s", error.message);
342 dbus_message_unref(m);
343 dbus_message_unref(reply);
352 dbus_message_unref(m);
355 dbus_message_unref(reply);
357 dbus_error_free(&error);
362 static int cancel_job(DBusConnection *bus, char **args, unsigned n) {
363 DBusMessage *m = NULL, *reply = NULL;
368 dbus_error_init(&error);
370 for (i = 1; i < n; i++) {
374 if (!(m = dbus_message_new_method_call(
375 "org.freedesktop.systemd1",
376 "/org/freedesktop/systemd1",
377 "org.freedesktop.systemd1.Manager",
379 log_error("Could not allocate message.");
384 if ((r = safe_atou(args[i], &id)) < 0) {
385 log_error("Failed to parse job id: %s", strerror(-r));
389 assert_cc(sizeof(uint32_t) == sizeof(id));
390 if (!dbus_message_append_args(m,
391 DBUS_TYPE_UINT32, &id,
392 DBUS_TYPE_INVALID)) {
393 log_error("Could not append arguments to message.");
398 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
399 log_error("Failed to issue method call: %s", error.message);
404 if (!dbus_message_get_args(reply, &error,
405 DBUS_TYPE_OBJECT_PATH, &path,
406 DBUS_TYPE_INVALID)) {
407 log_error("Failed to parse reply: %s", error.message);
412 dbus_message_unref(m);
413 if (!(m = dbus_message_new_method_call(
414 "org.freedesktop.systemd1",
416 "org.freedesktop.systemd1.Job",
418 log_error("Could not allocate message.");
423 dbus_message_unref(reply);
424 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
425 log_error("Failed to issue method call: %s", error.message);
430 dbus_message_unref(m);
431 dbus_message_unref(reply);
439 dbus_message_unref(m);
442 dbus_message_unref(reply);
444 dbus_error_free(&error);
449 static DBusHandlerResult wait_filter(DBusConnection *connection, DBusMessage *message, void *data) {
457 dbus_error_init(&error);
459 /* log_debug("Got D-Bus request: %s.%s() on %s", */
460 /* dbus_message_get_interface(message), */
461 /* dbus_message_get_member(message), */
462 /* dbus_message_get_path(message)); */
464 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
465 log_error("Warning! D-Bus connection terminated.");
466 dbus_connection_close(connection);
468 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
472 if (!dbus_message_get_args(message, &error,
473 DBUS_TYPE_UINT32, &id,
474 DBUS_TYPE_OBJECT_PATH, &path,
476 log_error("Failed to parse message: %s", error.message);
480 if ((p = set_remove(s, (char*) path)))
485 dbus_error_free(&error);
486 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
489 static int enable_wait_for_jobs(DBusConnection *bus) {
491 DBusMessage *m = NULL, *reply = NULL;
496 dbus_error_init(&error);
498 dbus_bus_add_match(bus,
500 "sender='org.freedesktop.systemd1',"
501 "interface='org.freedesktop.systemd1.Manager',"
502 "member='JobRemoved',"
503 "path='/org/freedesktop/systemd1'",
506 if (dbus_error_is_set(&error)) {
507 log_error("Failed to add match: %s", error.message);
512 if (!(m = dbus_message_new_method_call(
513 "org.freedesktop.systemd1",
514 "/org/freedesktop/systemd1",
515 "org.freedesktop.systemd1.Manager",
517 log_error("Could not allocate message.");
522 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
523 log_error("Failed to issue method call: %s", error.message);
531 /* This is slightly dirty, since we don't undo the match registrations. */
534 dbus_message_unref(m);
537 dbus_message_unref(reply);
539 dbus_error_free(&error);
544 static int wait_for_jobs(DBusConnection *bus, Set *s) {
550 if (!dbus_connection_add_filter(bus, wait_filter, s, NULL)) {
551 log_error("Failed to add filter.");
556 while (!set_isempty(s) &&
557 dbus_connection_read_write_dispatch(bus, -1))
563 /* This is slightly dirty, since we don't undo the filter registration. */
568 static int start_unit_one(
575 DBusMessage *m = NULL, *reply = NULL;
583 assert(!arg_block || s);
585 dbus_error_init(&error);
587 if (!(m = dbus_message_new_method_call(
588 "org.freedesktop.systemd1",
589 "/org/freedesktop/systemd1",
590 "org.freedesktop.systemd1.Manager",
592 log_error("Could not allocate message.");
597 if (!dbus_message_append_args(m,
598 DBUS_TYPE_STRING, &name,
599 DBUS_TYPE_STRING, &mode,
600 DBUS_TYPE_INVALID)) {
601 log_error("Could not append arguments to message.");
606 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
608 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
609 /* There's always a fallback possible for
615 log_error("Failed to issue method call: %s", error.message);
624 if (!dbus_message_get_args(reply, &error,
625 DBUS_TYPE_OBJECT_PATH, &path,
626 DBUS_TYPE_INVALID)) {
627 log_error("Failed to parse reply: %s", error.message);
632 if (!(p = strdup(path))) {
633 log_error("Failed to duplicate path.");
638 if ((r = set_put(s, p)) < 0) {
640 log_error("Failed to add path to set.");
649 dbus_message_unref(m);
652 dbus_message_unref(reply);
654 dbus_error_free(&error);
659 static int start_unit(DBusConnection *bus, char **args, unsigned n) {
661 static const char * const table[_ACTION_MAX] = {
662 [ACTION_HALT] = "halt.target",
663 [ACTION_POWEROFF] = "poweroff.target",
664 [ACTION_REBOOT] = "reboot.target",
665 [ACTION_RUNLEVEL1] = "runlevel1.target",
666 [ACTION_RUNLEVEL2] = "runlevel2.target",
667 [ACTION_RUNLEVEL3] = "runlevel3.target",
668 [ACTION_RUNLEVEL4] = "runlevel4.target",
669 [ACTION_RUNLEVEL5] = "runlevel5.target",
670 [ACTION_RESCUE] = "rescue.target"
675 const char *method, *mode;
678 if (arg_action == ACTION_SYSTEMCTL) {
680 streq(args[0], "start") ? "StartUnit" :
681 streq(args[0], "stop") ? "StopUnit" :
682 streq(args[0], "reload") ? "ReloadUnit" :
683 streq(args[0], "restart") ? "RestartUnit" :
684 /* isolate */ "StartUnit";
687 streq(args[0], "isolate") ? "isolate" :
688 arg_replace ? "replace" :
692 if ((r = enable_wait_for_jobs(bus)) < 0) {
693 log_error("Could not watch jobs: %s", strerror(-r));
697 if (!(s = set_new(string_hash_func, string_compare_func))) {
698 log_error("Failed to allocate set.");
704 assert(arg_action < ELEMENTSOF(table));
705 assert(table[arg_action]);
707 method = "StartUnit";
708 mode = arg_action == ACTION_RESCUE ? "isolate" : "replace";
713 if (arg_action == ACTION_SYSTEMCTL) {
714 for (i = 1; i < n; i++)
715 if ((r = start_unit_one(bus, method, args[i], mode, s)) < 0)
719 r = wait_for_jobs(bus, s);
722 if ((r = start_unit_one(bus, method, table[arg_action], mode, s)) <= 0)
733 static DBusHandlerResult monitor_filter(DBusConnection *connection, DBusMessage *message, void *data) {
735 DBusMessage *m = NULL, *reply = NULL;
740 dbus_error_init(&error);
742 /* log_debug("Got D-Bus request: %s.%s() on %s", */
743 /* dbus_message_get_interface(message), */
744 /* dbus_message_get_member(message), */
745 /* dbus_message_get_path(message)); */
747 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
748 log_error("Warning! D-Bus connection terminated.");
749 dbus_connection_close(connection);
751 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitNew") ||
752 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitRemoved")) {
753 const char *id, *path;
755 if (!dbus_message_get_args(message, &error,
756 DBUS_TYPE_STRING, &id,
757 DBUS_TYPE_OBJECT_PATH, &path,
759 log_error("Failed to parse message: %s", error.message);
760 else if (streq(dbus_message_get_member(message), "UnitNew"))
761 printf("Unit %s added.\n", id);
763 printf("Unit %s removed.\n", id);
765 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobNew") ||
766 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
770 if (!dbus_message_get_args(message, &error,
771 DBUS_TYPE_UINT32, &id,
772 DBUS_TYPE_OBJECT_PATH, &path,
774 log_error("Failed to parse message: %s", error.message);
775 else if (streq(dbus_message_get_member(message), "JobNew"))
776 printf("Job %u added.\n", id);
778 printf("Job %u removed.\n", id);
781 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Unit", "Changed") ||
782 dbus_message_is_signal(message, "org.freedesktop.systemd1.Job", "Changed")) {
784 const char *path, *interface, *property = "Id";
785 DBusMessageIter iter, sub;
787 path = dbus_message_get_path(message);
788 interface = dbus_message_get_interface(message);
790 if (!(m = dbus_message_new_method_call(
791 "org.freedesktop.systemd1",
793 "org.freedesktop.DBus.Properties",
795 log_error("Could not allocate message.");
799 if (!dbus_message_append_args(m,
800 DBUS_TYPE_STRING, &interface,
801 DBUS_TYPE_STRING, &property,
802 DBUS_TYPE_INVALID)) {
803 log_error("Could not append arguments to message.");
807 if (!(reply = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) {
808 log_error("Failed to issue method call: %s", error.message);
812 if (!dbus_message_iter_init(reply, &iter) ||
813 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
814 log_error("Failed to parse reply.");
818 dbus_message_iter_recurse(&iter, &sub);
820 if (streq(interface, "org.freedesktop.systemd1.Unit")) {
823 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
824 log_error("Failed to parse reply.");
828 dbus_message_iter_get_basic(&sub, &id);
829 printf("Unit %s changed.\n", id);
833 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT32) {
834 log_error("Failed to parse reply.");
838 dbus_message_iter_get_basic(&sub, &id);
839 printf("Job %u changed.\n", id);
845 dbus_message_unref(m);
848 dbus_message_unref(reply);
850 dbus_error_free(&error);
851 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
855 dbus_message_unref(m);
858 dbus_message_unref(reply);
860 dbus_error_free(&error);
861 return DBUS_HANDLER_RESULT_NEED_MEMORY;
864 static int monitor(DBusConnection *bus, char **args, unsigned n) {
865 DBusMessage *m = NULL, *reply = NULL;
869 dbus_error_init(&error);
871 dbus_bus_add_match(bus,
873 "sender='org.freedesktop.systemd1',"
874 "interface='org.freedesktop.systemd1.Manager',"
875 "path='/org/freedesktop/systemd1'",
878 if (dbus_error_is_set(&error)) {
879 log_error("Failed to add match: %s", error.message);
884 dbus_bus_add_match(bus,
886 "sender='org.freedesktop.systemd1',"
887 "interface='org.freedesktop.systemd1.Unit',"
891 if (dbus_error_is_set(&error)) {
892 log_error("Failed to add match: %s", error.message);
897 dbus_bus_add_match(bus,
899 "sender='org.freedesktop.systemd1',"
900 "interface='org.freedesktop.systemd1.Job',"
904 if (dbus_error_is_set(&error)) {
905 log_error("Failed to add match: %s", error.message);
910 if (!dbus_connection_add_filter(bus, monitor_filter, NULL, NULL)) {
911 log_error("Failed to add filter.");
916 if (!(m = dbus_message_new_method_call(
917 "org.freedesktop.systemd1",
918 "/org/freedesktop/systemd1",
919 "org.freedesktop.systemd1.Manager",
921 log_error("Could not allocate message.");
926 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
927 log_error("Failed to issue method call: %s", error.message);
932 while (dbus_connection_read_write_dispatch(bus, -1))
939 /* This is slightly dirty, since we don't undo the filter or the matches. */
942 dbus_message_unref(m);
945 dbus_message_unref(reply);
947 dbus_error_free(&error);
952 static int dump(DBusConnection *bus, char **args, unsigned n) {
953 DBusMessage *m = NULL, *reply = NULL;
958 dbus_error_init(&error);
960 if (!(m = dbus_message_new_method_call(
961 "org.freedesktop.systemd1",
962 "/org/freedesktop/systemd1",
963 "org.freedesktop.systemd1.Manager",
965 log_error("Could not allocate message.");
969 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
970 log_error("Failed to issue method call: %s", error.message);
975 if (!dbus_message_get_args(reply, &error,
976 DBUS_TYPE_STRING, &text,
977 DBUS_TYPE_INVALID)) {
978 log_error("Failed to parse reply: %s", error.message);
989 dbus_message_unref(m);
992 dbus_message_unref(reply);
994 dbus_error_free(&error);
999 static int snapshot(DBusConnection *bus, char **args, unsigned n) {
1000 DBusMessage *m = NULL, *reply = NULL;
1003 const char *name = "", *path, *id;
1004 dbus_bool_t cleanup = FALSE;
1005 DBusMessageIter iter, sub;
1007 *interface = "org.freedesktop.systemd1.Unit",
1010 dbus_error_init(&error);
1012 if (!(m = dbus_message_new_method_call(
1013 "org.freedesktop.systemd1",
1014 "/org/freedesktop/systemd1",
1015 "org.freedesktop.systemd1.Manager",
1016 "CreateSnapshot"))) {
1017 log_error("Could not allocate message.");
1024 if (!dbus_message_append_args(m,
1025 DBUS_TYPE_STRING, &name,
1026 DBUS_TYPE_BOOLEAN, &cleanup,
1027 DBUS_TYPE_INVALID)) {
1028 log_error("Could not append arguments to message.");
1033 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1034 log_error("Failed to issue method call: %s", error.message);
1039 if (!dbus_message_get_args(reply, &error,
1040 DBUS_TYPE_OBJECT_PATH, &path,
1041 DBUS_TYPE_INVALID)) {
1042 log_error("Failed to parse reply: %s", error.message);
1047 dbus_message_unref(m);
1048 if (!(m = dbus_message_new_method_call(
1049 "org.freedesktop.systemd1",
1051 "org.freedesktop.DBus.Properties",
1053 log_error("Could not allocate message.");
1057 if (!dbus_message_append_args(m,
1058 DBUS_TYPE_STRING, &interface,
1059 DBUS_TYPE_STRING, &property,
1060 DBUS_TYPE_INVALID)) {
1061 log_error("Could not append arguments to message.");
1066 dbus_message_unref(reply);
1067 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1068 log_error("Failed to issue method call: %s", error.message);
1073 if (!dbus_message_iter_init(reply, &iter) ||
1074 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1075 log_error("Failed to parse reply.");
1080 dbus_message_iter_recurse(&iter, &sub);
1082 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1083 log_error("Failed to parse reply.");
1088 dbus_message_iter_get_basic(&sub, &id);
1094 dbus_message_unref(m);
1097 dbus_message_unref(reply);
1099 dbus_error_free(&error);
1104 static int clear_jobs(DBusConnection *bus, char **args, unsigned n) {
1105 DBusMessage *m = NULL, *reply = NULL;
1110 dbus_error_init(&error);
1112 if (arg_action == ACTION_RELOAD)
1114 else if (arg_action == ACTION_REEXEC)
1115 method = "Reexecute";
1117 assert(arg_action == ACTION_SYSTEMCTL);
1120 streq(args[0], "clear-jobs") ? "ClearJobs" :
1121 streq(args[0], "daemon-reload") ? "Reload" :
1122 streq(args[0], "daemon-reexec") ? "Reexecute" :
1126 if (!(m = dbus_message_new_method_call(
1127 "org.freedesktop.systemd1",
1128 "/org/freedesktop/systemd1",
1129 "org.freedesktop.systemd1.Manager",
1131 log_error("Could not allocate message.");
1135 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1137 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
1138 /* There's always a fallback possible for
1139 * legacy actions. */
1144 log_error("Failed to issue method call: %s", error.message);
1153 dbus_message_unref(m);
1156 dbus_message_unref(reply);
1158 dbus_error_free(&error);
1163 static int show_enviroment(DBusConnection *bus, char **args, unsigned n) {
1164 DBusMessage *m = NULL, *reply = NULL;
1166 DBusMessageIter iter, sub, sub2;
1169 *interface = "org.freedesktop.systemd1.Manager",
1170 *property = "Environment";
1172 dbus_error_init(&error);
1174 if (!(m = dbus_message_new_method_call(
1175 "org.freedesktop.systemd1",
1176 "/org/freedesktop/systemd1",
1177 "org.freedesktop.DBus.Properties",
1179 log_error("Could not allocate message.");
1183 if (!dbus_message_append_args(m,
1184 DBUS_TYPE_STRING, &interface,
1185 DBUS_TYPE_STRING, &property,
1186 DBUS_TYPE_INVALID)) {
1187 log_error("Could not append arguments to message.");
1192 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1193 log_error("Failed to issue method call: %s", error.message);
1198 if (!dbus_message_iter_init(reply, &iter) ||
1199 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1200 log_error("Failed to parse reply.");
1205 dbus_message_iter_recurse(&iter, &sub);
1207 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
1208 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
1209 log_error("Failed to parse reply.");
1214 dbus_message_iter_recurse(&sub, &sub2);
1216 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
1219 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
1220 log_error("Failed to parse reply.");
1225 dbus_message_iter_get_basic(&sub2, &text);
1226 printf("%s\n", text);
1228 dbus_message_iter_next(&sub2);
1235 dbus_message_unref(m);
1238 dbus_message_unref(reply);
1240 dbus_error_free(&error);
1245 static int set_environment(DBusConnection *bus, char **args, unsigned n) {
1246 DBusMessage *m = NULL, *reply = NULL;
1250 DBusMessageIter iter, sub;
1253 dbus_error_init(&error);
1255 method = streq(args[0], "set-environment")
1257 : "UnsetEnvironment";
1259 if (!(m = dbus_message_new_method_call(
1260 "org.freedesktop.systemd1",
1261 "/org/freedesktop/systemd1",
1262 "org.freedesktop.systemd1.Manager",
1265 log_error("Could not allocate message.");
1269 dbus_message_iter_init_append(m, &iter);
1271 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
1272 log_error("Could not append arguments to message.");
1277 for (i = 1; i < n; i++)
1278 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &args[i])) {
1279 log_error("Could not append arguments to message.");
1284 if (!dbus_message_iter_close_container(&iter, &sub)) {
1285 log_error("Could not append arguments to message.");
1290 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1291 log_error("Failed to issue method call: %s", error.message);
1300 dbus_message_unref(m);
1303 dbus_message_unref(reply);
1305 dbus_error_free(&error);
1310 static int systemctl_help(void) {
1312 printf("%s [options]\n\n"
1313 "Send control commands to the init system.\n\n"
1314 " -h --help Show this help\n"
1315 " -t --type=TYPE List only units of a particular type\n"
1316 " -a --all Show all units, including dead ones\n"
1317 " --replace When installing a new job, replace existing conflicting ones\n"
1318 " --system Connect to system bus\n"
1319 " --session Connect to session bus\n"
1320 " --block Wait until operation finished\n\n"
1322 " list-units List units\n"
1323 " list-jobs List jobs\n"
1324 " clear-jobs Cancel all jobs\n"
1325 " load [NAME...] Load one or more units\n"
1326 " cancel [JOB...] Cancel one or more jobs\n"
1327 " start [NAME...] Start one or more units\n"
1328 " stop [NAME...] Stop one or more units\n"
1329 " restart [NAME...] Restart one or more units\n"
1330 " reload [NAME...] Reload one or more units\n"
1331 " isolate [NAME] Start one unit and stop all others\n"
1332 " monitor Monitor unit/job changes\n"
1333 " dump Dump server status\n"
1334 " snapshot [NAME] Create a snapshot\n"
1335 " daemon-reload Reload daemon configuration\n"
1336 " daemon-reexecute Reexecute daemon\n"
1337 " daemon-exit Ask the daemon to quit\n"
1338 " show-environment Dump environment\n"
1339 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
1340 " unset-environment [NAME...] Unset one or more environment variables\n",
1341 program_invocation_short_name);
1346 static int halt_help(void) {
1348 printf("%s [options]\n\n"
1349 "%s the system.\n\n"
1350 " --help Show this help\n"
1351 " --halt Halt the machine\n"
1352 " -p --poweroff Switch off the machine\n"
1353 " --reboot Reboot the machine\n"
1354 " -f --force Force immediate reboot/halt/power-off\n"
1355 " -w --wtmp-only Don't reboot/halt/power-off, just write wtmp record\n"
1356 " -d --no-wtmp Don't write wtmp record\n"
1357 " -n --no-sync Don't sync before reboot/halt/power-off\n",
1358 program_invocation_short_name,
1359 arg_action == ACTION_REBOOT ? "Reboot" :
1360 arg_action == ACTION_POWEROFF ? "Power off" :
1366 static int shutdown_help(void) {
1368 printf("%s [options] [TIME] [WALL...]\n\n"
1369 "Shut down the system.\n\n"
1370 " --help Show this help\n"
1371 " -H --halt Halt the machine\n"
1372 " -P --poweroff Power-off the machine\n"
1373 " -r --reboot Reboot the machine\n"
1374 " -h Equivalent to --poweroff, overriden by --halt\n"
1375 " -k Don't reboot/halt/power-off, just send warnings\n",
1376 program_invocation_short_name);
1381 static int telinit_help(void) {
1383 printf("%s [options]\n\n"
1384 "Send control commands to the init system.\n\n"
1385 " --help Show this help\n\n"
1387 " 0 Power-off the machine\n"
1388 " 6 Reboot the machine\n"
1389 " 1, 2, 3, 4, 5 Start runlevelX.target unit\n"
1390 " s, S Start the rescue.target unit\n"
1391 " q, Q Ask systemd to reload its configuration\n"
1392 " u, U Ask systemd to reexecute itself\n",
1393 program_invocation_short_name);
1398 static int runlevel_help(void) {
1400 printf("%s [options]\n\n"
1401 "Prints the previous and current runlevel of the init system.\n\n"
1402 " --help Show this help\n",
1403 program_invocation_short_name);
1408 static int systemctl_parse_argv(int argc, char *argv[]) {
1411 ARG_REPLACE = 0x100,
1417 static const struct option options[] = {
1418 { "help", no_argument, NULL, 'h' },
1419 { "type", required_argument, NULL, 't' },
1420 { "all", no_argument, NULL, 'a' },
1421 { "replace", no_argument, NULL, ARG_REPLACE },
1422 { "session", no_argument, NULL, ARG_SESSION },
1423 { "system", no_argument, NULL, ARG_SYSTEM },
1424 { "block", no_argument, NULL, ARG_BLOCK },
1425 { NULL, 0, NULL, 0 }
1433 while ((c = getopt_long(argc, argv, "hta", options, NULL)) >= 0) {
1458 arg_session = false;
1469 log_error("Unknown option code %c", c);
1477 static int halt_parse_argv(int argc, char *argv[]) {
1485 static const struct option options[] = {
1486 { "help", no_argument, NULL, ARG_HELP },
1487 { "halt", no_argument, NULL, ARG_HALT },
1488 { "poweroff", no_argument, NULL, 'p' },
1489 { "reboot", no_argument, NULL, ARG_REBOOT },
1490 { "force", no_argument, NULL, 'f' },
1491 { "wtmp-only", no_argument, NULL, 'w' },
1492 { "no-wtmp", no_argument, NULL, 'd' },
1493 { "no-sync", no_argument, NULL, 'n' },
1494 { NULL, 0, NULL, 0 }
1502 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
1503 if (runlevel == '0' || runlevel == '6')
1504 arg_immediate = true;
1506 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
1514 arg_action = ACTION_HALT;
1518 arg_action = ACTION_POWEROFF;
1522 arg_action = ACTION_REBOOT;
1526 arg_immediate = true;
1543 /* Compatibility nops */
1550 log_error("Unknown option code %c", c);
1555 if (optind < argc) {
1556 log_error("Too many arguments.");
1563 static int shutdown_parse_argv(int argc, char *argv[]) {
1569 static const struct option options[] = {
1570 { "help", no_argument, NULL, ARG_HELP },
1571 { "halt", no_argument, NULL, 'H' },
1572 { "poweroff", no_argument, NULL, 'P' },
1573 { "reboot", no_argument, NULL, 'r' },
1574 { NULL, 0, NULL, 0 }
1582 while ((c = getopt_long(argc, argv, "HPrhkt:a", options, NULL)) >= 0) {
1590 arg_action = ACTION_HALT;
1594 arg_action = ACTION_POWEROFF;
1598 arg_action = ACTION_REBOOT;
1602 if (arg_action != ACTION_HALT)
1603 arg_action = ACTION_POWEROFF;
1612 /* Compatibility nops */
1619 log_error("Unknown option code %c", c);
1624 /* We ignore the time argument */
1625 if (argc > optind + 1)
1626 arg_wall = argv + optind + 1;
1634 static int telinit_parse_argv(int argc, char *argv[]) {
1640 static const struct option options[] = {
1641 { "help", no_argument, NULL, ARG_HELP },
1642 { NULL, 0, NULL, 0 }
1645 static const struct {
1649 { '0', ACTION_POWEROFF },
1650 { '6', ACTION_REBOOT },
1651 { '1', ACTION_RUNLEVEL1 },
1652 { '2', ACTION_RUNLEVEL2 },
1653 { '3', ACTION_RUNLEVEL3 },
1654 { '4', ACTION_RUNLEVEL4 },
1655 { '5', ACTION_RUNLEVEL5 },
1656 { 's', ACTION_RESCUE },
1657 { 'S', ACTION_RESCUE },
1658 { 'q', ACTION_RELOAD },
1659 { 'Q', ACTION_RELOAD },
1660 { 'u', ACTION_REEXEC },
1661 { 'U', ACTION_REEXEC }
1670 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
1681 log_error("Unknown option code %c", c);
1686 if (optind >= argc) {
1687 log_error("Argument missing.");
1691 if (optind + 1 < argc) {
1692 log_error("Too many arguments.");
1696 if (strlen(argv[optind]) != 1) {
1697 log_error("Expected single character argument.");
1701 for (i = 0; i < ELEMENTSOF(table); i++)
1702 if (table[i].from == argv[optind][0])
1705 if (i >= ELEMENTSOF(table)) {
1706 log_error("Unknown command %s.", argv[optind]);
1710 arg_action = table[i].to;
1717 static int runlevel_parse_argv(int argc, char *argv[]) {
1723 static const struct option options[] = {
1724 { "help", no_argument, NULL, ARG_HELP },
1725 { NULL, 0, NULL, 0 }
1733 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
1744 log_error("Unknown option code %c", c);
1749 if (optind < argc) {
1750 log_error("Too many arguments.");
1757 static int parse_argv(int argc, char *argv[]) {
1761 if (program_invocation_short_name) {
1763 if (strstr(program_invocation_short_name, "halt")) {
1764 arg_action = ACTION_HALT;
1765 return halt_parse_argv(argc, argv);
1766 } else if (strstr(program_invocation_short_name, "poweroff")) {
1767 arg_action = ACTION_POWEROFF;
1768 return halt_parse_argv(argc, argv);
1769 } else if (strstr(program_invocation_short_name, "reboot")) {
1770 arg_action = ACTION_REBOOT;
1771 return halt_parse_argv(argc, argv);
1772 } else if (strstr(program_invocation_short_name, "shutdown")) {
1773 arg_action = ACTION_POWEROFF;
1774 return shutdown_parse_argv(argc, argv);
1775 } else if (strstr(program_invocation_short_name, "init")) {
1776 arg_action = ACTION_INVALID;
1777 return telinit_parse_argv(argc, argv);
1778 } else if (strstr(program_invocation_short_name, "runlevel")) {
1779 arg_action = ACTION_RUNLEVEL;
1780 return runlevel_parse_argv(argc, argv);
1784 arg_action = ACTION_SYSTEMCTL;
1785 return systemctl_parse_argv(argc, argv);
1788 static int talk_upstart(DBusConnection *bus) {
1789 log_error("Talking upstart");
1793 static int talk_initctl(void) {
1794 log_error("Talking initctl");
1798 static int systemctl_main(DBusConnection *bus, int argc, char *argv[]) {
1800 static const struct {
1808 int (* const dispatch)(DBusConnection *bus, char **args, unsigned n);
1810 { "list-units", LESS, 1, list_units },
1811 { "list-jobs", EQUAL, 1, list_jobs },
1812 { "clear-jobs", EQUAL, 1, clear_jobs },
1813 { "load", MORE, 2, load_unit },
1814 { "cancel", MORE, 2, cancel_job },
1815 { "start", MORE, 2, start_unit },
1816 { "stop", MORE, 2, start_unit },
1817 { "reload", MORE, 2, start_unit },
1818 { "restart", MORE, 2, start_unit },
1819 { "isolate", EQUAL, 2, start_unit },
1820 { "monitor", EQUAL, 1, monitor },
1821 { "dump", EQUAL, 1, dump },
1822 { "snapshot", LESS, 2, snapshot },
1823 { "daemon-reload", EQUAL, 1, clear_jobs },
1824 { "daemon-reexec", EQUAL, 1, clear_jobs },
1825 { "daemon-exit", EQUAL, 1, clear_jobs },
1826 { "show-environment", EQUAL, 1, show_enviroment },
1827 { "set-environment", MORE, 2, set_environment },
1828 { "unset-environment", MORE, 2, set_environment },
1838 left = argc - optind;
1841 /* Special rule: no arguments means "list-units" */
1844 for (i = 0; i < ELEMENTSOF(verbs); i++)
1845 if (streq(argv[optind], verbs[i].verb))
1848 if (i >= ELEMENTSOF(verbs)) {
1849 log_error("Unknown operation %s", argv[optind]);
1854 switch (verbs[i].argc_cmp) {
1857 if (left != verbs[i].argc) {
1858 log_error("Invalid number of arguments.");
1865 if (left < verbs[i].argc) {
1866 log_error("Too few arguments.");
1873 if (left > verbs[i].argc) {
1874 log_error("Too many arguments.");
1881 assert_not_reached("Unknown comparison operator.");
1884 return verbs[i].dispatch(bus, argv + optind, left);
1887 static int reload_with_fallback(DBusConnection *bus) {
1891 /* First, try systemd via D-Bus. */
1892 if ((r = clear_jobs(bus, NULL, 0)) > 0)
1896 /* Nothing else worked, so let's try signals */
1897 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
1899 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
1900 log_error("kill() failed: %m");
1907 static int start_with_fallback(DBusConnection *bus) {
1911 /* First, try systemd via D-Bus. */
1912 if ((r = start_unit(bus, NULL, 0)) > 0)
1915 /* Hmm, talking to systemd via D-Bus didn't work. Then
1916 * let's try to talk to Upstart via D-Bus. */
1917 if ((r = talk_upstart(bus)) > 0)
1921 /* Nothing else worked, so let's try
1923 return talk_initctl();
1926 static int halt_main(DBusConnection *bus) {
1930 return start_with_fallback(bus);
1933 if ((r = utmp_put_shutdown(0)) < 0)
1934 log_warning("Failed to write utmp record: %s", strerror(-r));
1942 /* Make sure C-A-D is handled by the kernel from this
1944 reboot(RB_ENABLE_CAD);
1946 switch (arg_action) {
1949 log_info("Halting");
1950 reboot(RB_HALT_SYSTEM);
1953 case ACTION_POWEROFF:
1954 log_info("Powering off");
1955 reboot(RB_POWER_OFF);
1959 log_info("Rebooting");
1960 reboot(RB_AUTOBOOT);
1964 assert_not_reached("Unknown halt action.");
1967 /* We should never reach this. */
1971 static int runlevel_main(void) {
1972 int r, runlevel, previous;
1974 if ((r = utmp_get_runlevel(&runlevel, &previous)) < 0) {
1980 previous <= 0 ? 'N' : previous,
1981 runlevel <= 0 ? 'N' : runlevel);
1986 int main(int argc, char*argv[]) {
1988 DBusConnection *bus = NULL;
1991 dbus_error_init(&error);
1993 log_parse_environment();
1995 if ((r = parse_argv(argc, argv)) < 0)
2002 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
2003 * let's shortcut this */
2004 if (arg_action == ACTION_RUNLEVEL) {
2005 retval = runlevel_main() < 0;
2009 if ((bus = dbus_bus_get(arg_session ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &error)))
2010 dbus_connection_set_exit_on_disconnect(bus, FALSE);
2012 switch (arg_action) {
2014 case ACTION_SYSTEMCTL: {
2017 log_error("Failed to get D-Bus connection: %s", error.message);
2021 retval = systemctl_main(bus, argc, argv) < 0;
2026 case ACTION_POWEROFF:
2028 retval = halt_main(bus) < 0;
2031 case ACTION_RUNLEVEL1:
2032 case ACTION_RUNLEVEL2:
2033 case ACTION_RUNLEVEL3:
2034 case ACTION_RUNLEVEL4:
2035 case ACTION_RUNLEVEL5:
2037 retval = start_with_fallback(bus) < 0;
2042 retval = reload_with_fallback(bus) < 0;
2046 assert_not_reached("Unknown action");
2052 dbus_connection_unref(bus);
2054 dbus_error_free(&error);